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