| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [d74077] | 8 | /* | 
|---|
|  | 9 | * CandidateForTesselation.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Jul 29, 2010 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [bbbad5] | 21 |  | 
|---|
| [d74077] | 22 | #include "CandidateForTesselation.hpp" | 
|---|
|  | 23 |  | 
|---|
|  | 24 | #include <iostream> | 
|---|
|  | 25 | #include <iomanip> | 
|---|
|  | 26 |  | 
|---|
|  | 27 | #include "BoundaryLineSet.hpp" | 
|---|
|  | 28 | #include "BoundaryPointSet.hpp" | 
|---|
|  | 29 | #include "TesselPoint.hpp" | 
|---|
|  | 30 |  | 
|---|
| [ad011c] | 31 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 32 | #include "CodePatterns/Info.hpp" | 
|---|
| [d74077] | 33 | //#include "Line.hpp" | 
|---|
|  | 34 | #include "linkedcell.hpp" | 
|---|
| [ad011c] | 35 | #include "CodePatterns/Log.hpp" | 
|---|
| [d74077] | 36 | //#include "Plane.hpp" | 
|---|
| [8f4df1] | 37 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [ad011c] | 38 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [d74077] | 39 |  | 
|---|
|  | 40 | using namespace std; | 
|---|
|  | 41 |  | 
|---|
| [88b400] | 42 |  | 
|---|
|  | 43 | const double CandidateForTesselation::HULLEPSILON = 1e-9; | 
|---|
|  | 44 |  | 
|---|
|  | 45 |  | 
|---|
| [d74077] | 46 | /** Constructor of class CandidateForTesselation. | 
|---|
|  | 47 | */ | 
|---|
|  | 48 | CandidateForTesselation::CandidateForTesselation(BoundaryLineSet* line) : | 
|---|
| [97b825] | 49 | BaseLine(line), | 
|---|
|  | 50 | ThirdPoint(NULL), | 
|---|
|  | 51 | T(NULL), | 
|---|
|  | 52 | ShortestAngle(2. * M_PI), | 
|---|
|  | 53 | OtherShortestAngle(2. * M_PI) | 
|---|
| [d74077] | 54 | { | 
|---|
|  | 55 | Info FunctionInfo(__func__); | 
|---|
|  | 56 | } | 
|---|
|  | 57 | ; | 
|---|
|  | 58 |  | 
|---|
|  | 59 | /** Constructor of class CandidateForTesselation. | 
|---|
|  | 60 | */ | 
|---|
|  | 61 | CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, BoundaryPointSet* point, const Vector &OptCandidateCenter, const Vector &OtherOptCandidateCenter) : | 
|---|
| [97b825] | 62 | BaseLine(line), | 
|---|
|  | 63 | ThirdPoint(point), | 
|---|
|  | 64 | T(NULL), | 
|---|
|  | 65 | OptCenter(OptCandidateCenter), | 
|---|
|  | 66 | OtherOptCenter(OtherOptCandidateCenter), | 
|---|
|  | 67 | ShortestAngle(2. * M_PI), | 
|---|
|  | 68 | OtherShortestAngle(2. * M_PI) | 
|---|
| [d74077] | 69 | { | 
|---|
|  | 70 | Info FunctionInfo(__func__); | 
|---|
|  | 71 | }; | 
|---|
|  | 72 |  | 
|---|
|  | 73 |  | 
|---|
|  | 74 | /** Destructor for class CandidateForTesselation. | 
|---|
|  | 75 | */ | 
|---|
|  | 76 | CandidateForTesselation::~CandidateForTesselation() | 
|---|
|  | 77 | { | 
|---|
|  | 78 | } | 
|---|
|  | 79 | ; | 
|---|
|  | 80 |  | 
|---|
|  | 81 | /** Checks validity of a given sphere of a candidate line. | 
|---|
|  | 82 | * Sphere must touch all candidates and the baseline endpoints and there must be no other atoms inside. | 
|---|
|  | 83 | * \param RADIUS radius of sphere | 
|---|
|  | 84 | * \param *LC LinkedCell structure with other atoms | 
|---|
|  | 85 | * \return true - sphere is valid, false - sphere contains other points | 
|---|
|  | 86 | */ | 
|---|
|  | 87 | bool CandidateForTesselation::CheckValidity(const double RADIUS, const LinkedCell *LC) const | 
|---|
|  | 88 | { | 
|---|
|  | 89 | Info FunctionInfo(__func__); | 
|---|
|  | 90 |  | 
|---|
|  | 91 | const double radiusSquared = RADIUS * RADIUS; | 
|---|
|  | 92 | list<const Vector *> VectorList; | 
|---|
|  | 93 | VectorList.push_back(&OptCenter); | 
|---|
|  | 94 | //VectorList.push_back(&OtherOptCenter);  // don't check the other (wrong) center | 
|---|
|  | 95 |  | 
|---|
|  | 96 | if (!pointlist.empty()) | 
|---|
|  | 97 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains candidate list and baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl); | 
|---|
|  | 98 | else | 
|---|
|  | 99 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere with no candidates contains baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl); | 
|---|
|  | 100 | // check baseline for OptCenter and OtherOptCenter being on sphere's surface | 
|---|
|  | 101 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
|  | 102 | for (int i = 0; i < 2; i++) { | 
|---|
|  | 103 | const double distance = fabs((*VRunner)->DistanceSquared(BaseLine->endpoints[i]->node->getPosition()) - radiusSquared); | 
|---|
|  | 104 | if (distance > HULLEPSILON) { | 
|---|
|  | 105 | DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << *BaseLine->endpoints[i] << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl); | 
|---|
|  | 106 | return false; | 
|---|
|  | 107 | } | 
|---|
|  | 108 | } | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | // check Candidates for OptCenter and OtherOptCenter being on sphere's surface | 
|---|
|  | 112 | for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) { | 
|---|
|  | 113 | const TesselPoint *Walker = *Runner; | 
|---|
|  | 114 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
|  | 115 | const double distance = fabs((*VRunner)->DistanceSquared(Walker->getPosition()) - radiusSquared); | 
|---|
|  | 116 | if (distance > HULLEPSILON) { | 
|---|
|  | 117 | DoeLog(1) && (eLog() << Verbose(1) << "Candidate " << *Walker << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl); | 
|---|
|  | 118 | return false; | 
|---|
|  | 119 | } else { | 
|---|
|  | 120 | DoLog(1) && (Log() << Verbose(1) << "Candidate " << *Walker << " is inside by " << distance << "." << endl); | 
|---|
|  | 121 | } | 
|---|
|  | 122 | } | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl); | 
|---|
|  | 126 | bool flag = true; | 
|---|
|  | 127 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
|  | 128 | // get all points inside the sphere | 
|---|
|  | 129 | TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, (*VRunner)); | 
|---|
|  | 130 |  | 
|---|
|  | 131 | DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << (*VRunner) << ":" << endl); | 
|---|
|  | 132 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
|  | 133 | DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->distance(*(*VRunner)) << "." << endl); | 
|---|
|  | 134 |  | 
|---|
|  | 135 | // remove baseline's endpoints and candidates | 
|---|
|  | 136 | for (int i = 0; i < 2; i++) { | 
|---|
|  | 137 | DoLog(1) && (Log() << Verbose(1) << "INFO: removing baseline tesselpoint " << *BaseLine->endpoints[i]->node << "." << endl); | 
|---|
|  | 138 | ListofPoints->remove(BaseLine->endpoints[i]->node); | 
|---|
|  | 139 | } | 
|---|
|  | 140 | for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) { | 
|---|
|  | 141 | DoLog(1) && (Log() << Verbose(1) << "INFO: removing candidate tesselpoint " << *(*Runner) << "." << endl); | 
|---|
|  | 142 | ListofPoints->remove(*Runner); | 
|---|
|  | 143 | } | 
|---|
|  | 144 | if (!ListofPoints->empty()) { | 
|---|
|  | 145 | DoeLog(1) && (eLog() << Verbose(1) << "CheckValidity: There are still " << ListofPoints->size() << " points inside the sphere." << endl); | 
|---|
|  | 146 | flag = false; | 
|---|
|  | 147 | DoeLog(1) && (eLog() << Verbose(1) << "External atoms inside of sphere at " << *(*VRunner) << ":" << endl); | 
|---|
|  | 148 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
|  | 149 | DoeLog(1) && (eLog() << Verbose(1) << "  " << *(*Runner) << " at distance " << setprecision(13) << (*Runner)->distance(*(*VRunner)) << setprecision(6) << "." << endl); | 
|---|
|  | 150 |  | 
|---|
|  | 151 | // check with animate_sphere.tcl VMD script | 
|---|
|  | 152 | if (ThirdPoint != NULL) { | 
|---|
|  | 153 | DoeLog(1) && (eLog() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " " << ThirdPoint->Nr + 1 << " " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl); | 
|---|
|  | 154 | } else { | 
|---|
|  | 155 | DoeLog(1) && (eLog() << Verbose(1) << "Check by: ... missing third point ..." << endl); | 
|---|
|  | 156 | DoeLog(1) && (eLog() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " ??? " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl); | 
|---|
|  | 157 | } | 
|---|
|  | 158 | } | 
|---|
|  | 159 | delete (ListofPoints); | 
|---|
|  | 160 |  | 
|---|
|  | 161 | } | 
|---|
|  | 162 | return flag; | 
|---|
|  | 163 | } | 
|---|
|  | 164 | ; | 
|---|
|  | 165 |  | 
|---|
|  | 166 | /** output operator for CandidateForTesselation. | 
|---|
|  | 167 | * \param &ost output stream | 
|---|
|  | 168 | * \param &a boundary line | 
|---|
|  | 169 | */ | 
|---|
|  | 170 | ostream & operator <<(ostream &ost, const CandidateForTesselation &a) | 
|---|
|  | 171 | { | 
|---|
|  | 172 | ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->getName() << "," << a.BaseLine->endpoints[1]->node->getName() << "] with "; | 
|---|
|  | 173 | if (a.pointlist.empty()) | 
|---|
|  | 174 | ost << "no candidate."; | 
|---|
|  | 175 | else { | 
|---|
|  | 176 | ost << "candidate"; | 
|---|
|  | 177 | if (a.pointlist.size() != 1) | 
|---|
|  | 178 | ost << "s "; | 
|---|
|  | 179 | else | 
|---|
|  | 180 | ost << " "; | 
|---|
|  | 181 | for (TesselPointList::const_iterator Runner = a.pointlist.begin(); Runner != a.pointlist.end(); Runner++) | 
|---|
|  | 182 | ost << *(*Runner) << " "; | 
|---|
|  | 183 | ost << " at angle " << (a.ShortestAngle) << "."; | 
|---|
|  | 184 | } | 
|---|
|  | 185 |  | 
|---|
|  | 186 | return ost; | 
|---|
|  | 187 | } | 
|---|
|  | 188 | ; | 
|---|
|  | 189 |  | 
|---|