| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 5 | * | 
|---|
|  | 6 | * | 
|---|
|  | 7 | *   This file is part of MoleCuilder. | 
|---|
|  | 8 | * | 
|---|
|  | 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
|  | 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
|  | 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
|  | 12 | *    (at your option) any later version. | 
|---|
|  | 13 | * | 
|---|
|  | 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
|  | 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 17 | *    GNU General Public License for more details. | 
|---|
|  | 18 | * | 
|---|
|  | 19 | *    You should have received a copy of the GNU General Public License | 
|---|
|  | 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| [bcf653] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
| [d74077] | 23 | /* | 
|---|
|  | 24 | * CandidateForTesselation.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Jul 29, 2010 | 
|---|
|  | 27 | *      Author: heber | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [bbbad5] | 36 |  | 
|---|
| [d74077] | 37 | #include "CandidateForTesselation.hpp" | 
|---|
|  | 38 |  | 
|---|
|  | 39 | #include <iostream> | 
|---|
|  | 40 | #include <iomanip> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | #include "BoundaryLineSet.hpp" | 
|---|
|  | 43 | #include "BoundaryPointSet.hpp" | 
|---|
| [6f0841] | 44 | #include "Atom/TesselPoint.hpp" | 
|---|
| [d74077] | 45 |  | 
|---|
| [ad011c] | 46 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 47 | #include "CodePatterns/Info.hpp" | 
|---|
|  | 48 | #include "CodePatterns/Log.hpp" | 
|---|
| [8f4df1] | 49 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [53c7fc] | 50 | #include "LinkedCell/linkedcell.hpp" | 
|---|
| [d74077] | 51 |  | 
|---|
|  | 52 | using namespace std; | 
|---|
|  | 53 |  | 
|---|
| [88b400] | 54 |  | 
|---|
|  | 55 | const double CandidateForTesselation::HULLEPSILON = 1e-9; | 
|---|
|  | 56 |  | 
|---|
|  | 57 |  | 
|---|
| [d74077] | 58 | /** Constructor of class CandidateForTesselation. | 
|---|
|  | 59 | */ | 
|---|
|  | 60 | CandidateForTesselation::CandidateForTesselation(BoundaryLineSet* line) : | 
|---|
| [97b825] | 61 | BaseLine(line), | 
|---|
|  | 62 | ThirdPoint(NULL), | 
|---|
|  | 63 | T(NULL), | 
|---|
|  | 64 | ShortestAngle(2. * M_PI), | 
|---|
|  | 65 | OtherShortestAngle(2. * M_PI) | 
|---|
| [d74077] | 66 | { | 
|---|
| [ce7bfd] | 67 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 68 | } | 
|---|
|  | 69 | ; | 
|---|
|  | 70 |  | 
|---|
|  | 71 | /** Constructor of class CandidateForTesselation. | 
|---|
|  | 72 | */ | 
|---|
|  | 73 | CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, BoundaryPointSet* point, const Vector &OptCandidateCenter, const Vector &OtherOptCandidateCenter) : | 
|---|
| [97b825] | 74 | BaseLine(line), | 
|---|
|  | 75 | ThirdPoint(point), | 
|---|
|  | 76 | T(NULL), | 
|---|
|  | 77 | OptCenter(OptCandidateCenter), | 
|---|
|  | 78 | OtherOptCenter(OtherOptCandidateCenter), | 
|---|
|  | 79 | ShortestAngle(2. * M_PI), | 
|---|
|  | 80 | OtherShortestAngle(2. * M_PI) | 
|---|
| [d74077] | 81 | { | 
|---|
| [ce7bfd] | 82 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 83 | }; | 
|---|
|  | 84 |  | 
|---|
|  | 85 |  | 
|---|
|  | 86 | /** Destructor for class CandidateForTesselation. | 
|---|
|  | 87 | */ | 
|---|
|  | 88 | CandidateForTesselation::~CandidateForTesselation() | 
|---|
|  | 89 | { | 
|---|
|  | 90 | } | 
|---|
|  | 91 | ; | 
|---|
|  | 92 |  | 
|---|
|  | 93 | /** Checks validity of a given sphere of a candidate line. | 
|---|
|  | 94 | * Sphere must touch all candidates and the baseline endpoints and there must be no other atoms inside. | 
|---|
|  | 95 | * \param RADIUS radius of sphere | 
|---|
| [6bd7e0] | 96 | * \param *LC LinkedCell_deprecated structure with other atoms | 
|---|
| [d74077] | 97 | * \return true - sphere is valid, false - sphere contains other points | 
|---|
|  | 98 | */ | 
|---|
| [6bd7e0] | 99 | bool CandidateForTesselation::CheckValidity(const double RADIUS, const LinkedCell_deprecated *LC) const | 
|---|
| [d74077] | 100 | { | 
|---|
| [ce7bfd] | 101 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 102 |  | 
|---|
|  | 103 | const double radiusSquared = RADIUS * RADIUS; | 
|---|
|  | 104 | list<const Vector *> VectorList; | 
|---|
|  | 105 | VectorList.push_back(&OptCenter); | 
|---|
|  | 106 | //VectorList.push_back(&OtherOptCenter);  // don't check the other (wrong) center | 
|---|
|  | 107 |  | 
|---|
|  | 108 | if (!pointlist.empty()) | 
|---|
| [ce7bfd] | 109 | LOG(3, "DEBUG: Checking whether sphere contains candidate list and baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..."); | 
|---|
| [d74077] | 110 | else | 
|---|
| [ce7bfd] | 111 | LOG(3, "DEBUG: Checking whether sphere with no candidates contains baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..."); | 
|---|
| [d74077] | 112 | // check baseline for OptCenter and OtherOptCenter being on sphere's surface | 
|---|
|  | 113 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
|  | 114 | for (int i = 0; i < 2; i++) { | 
|---|
|  | 115 | const double distance = fabs((*VRunner)->DistanceSquared(BaseLine->endpoints[i]->node->getPosition()) - radiusSquared); | 
|---|
|  | 116 | if (distance > HULLEPSILON) { | 
|---|
| [47d041] | 117 | ELOG(1, "Endpoint " << *BaseLine->endpoints[i] << " is out of sphere at " << *(*VRunner) << " by " << distance << "."); | 
|---|
| [d74077] | 118 | return false; | 
|---|
|  | 119 | } | 
|---|
|  | 120 | } | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | // check Candidates for OptCenter and OtherOptCenter being on sphere's surface | 
|---|
|  | 124 | for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) { | 
|---|
|  | 125 | const TesselPoint *Walker = *Runner; | 
|---|
|  | 126 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
|  | 127 | const double distance = fabs((*VRunner)->DistanceSquared(Walker->getPosition()) - radiusSquared); | 
|---|
|  | 128 | if (distance > HULLEPSILON) { | 
|---|
| [47d041] | 129 | ELOG(1, "Candidate " << *Walker << " is out of sphere at " << *(*VRunner) << " by " << distance << "."); | 
|---|
| [d74077] | 130 | return false; | 
|---|
|  | 131 | } else { | 
|---|
| [ce7bfd] | 132 | LOG(3, "DEBUG: Candidate " << *Walker << " is inside by " << distance << "."); | 
|---|
| [d74077] | 133 | } | 
|---|
|  | 134 | } | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [ce7bfd] | 137 | LOG(2, "DEBUG: Checking whether sphere contains no others points ..."); | 
|---|
| [d74077] | 138 | bool flag = true; | 
|---|
|  | 139 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
|  | 140 | // get all points inside the sphere | 
|---|
|  | 141 | TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, (*VRunner)); | 
|---|
|  | 142 |  | 
|---|
| [ce7bfd] | 143 | { | 
|---|
|  | 144 | LOG(3, "DEBUG: The following atoms are inside sphere at " << (*VRunner) << ":"); | 
|---|
|  | 145 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
|  | 146 | LOG(3, "DEBUG:  " << *(*Runner) << " with distance " << (*Runner)->distance(*(*VRunner)) << "."); | 
|---|
|  | 147 | } | 
|---|
| [d74077] | 148 |  | 
|---|
|  | 149 | // remove baseline's endpoints and candidates | 
|---|
|  | 150 | for (int i = 0; i < 2; i++) { | 
|---|
| [ce7bfd] | 151 | LOG(3, "DEBUG: removing baseline tesselpoint " << *BaseLine->endpoints[i]->node << "."); | 
|---|
| [d74077] | 152 | ListofPoints->remove(BaseLine->endpoints[i]->node); | 
|---|
|  | 153 | } | 
|---|
|  | 154 | for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) { | 
|---|
| [ce7bfd] | 155 | LOG(3, "DEBUG: removing candidate tesselpoint " << *(*Runner) << "."); | 
|---|
| [d74077] | 156 | ListofPoints->remove(*Runner); | 
|---|
|  | 157 | } | 
|---|
|  | 158 | if (!ListofPoints->empty()) { | 
|---|
| [47d041] | 159 | ELOG(1, "CheckValidity: There are still " << ListofPoints->size() << " points inside the sphere."); | 
|---|
| [d74077] | 160 | flag = false; | 
|---|
| [47d041] | 161 | ELOG(1, "External atoms inside of sphere at " << *(*VRunner) << ":"); | 
|---|
| [d74077] | 162 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
| [47d041] | 163 | ELOG(1, "  " << *(*Runner) << " at distance " << setprecision(13) << (*Runner)->distance(*(*VRunner)) << setprecision(6) << "."); | 
|---|
| [d74077] | 164 |  | 
|---|
|  | 165 | // check with animate_sphere.tcl VMD script | 
|---|
|  | 166 | if (ThirdPoint != NULL) { | 
|---|
| [47d041] | 167 | 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] | 168 | } else { | 
|---|
| [47d041] | 169 | ELOG(1, "Check by: ... missing third point ..."); | 
|---|
|  | 170 | 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] | 171 | } | 
|---|
|  | 172 | } | 
|---|
|  | 173 | delete (ListofPoints); | 
|---|
|  | 174 |  | 
|---|
|  | 175 | } | 
|---|
|  | 176 | return flag; | 
|---|
|  | 177 | } | 
|---|
|  | 178 | ; | 
|---|
|  | 179 |  | 
|---|
|  | 180 | /** output operator for CandidateForTesselation. | 
|---|
|  | 181 | * \param &ost output stream | 
|---|
|  | 182 | * \param &a boundary line | 
|---|
|  | 183 | */ | 
|---|
|  | 184 | ostream & operator <<(ostream &ost, const CandidateForTesselation &a) | 
|---|
|  | 185 | { | 
|---|
|  | 186 | ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->getName() << "," << a.BaseLine->endpoints[1]->node->getName() << "] with "; | 
|---|
|  | 187 | if (a.pointlist.empty()) | 
|---|
|  | 188 | ost << "no candidate."; | 
|---|
|  | 189 | else { | 
|---|
|  | 190 | ost << "candidate"; | 
|---|
|  | 191 | if (a.pointlist.size() != 1) | 
|---|
|  | 192 | ost << "s "; | 
|---|
|  | 193 | else | 
|---|
|  | 194 | ost << " "; | 
|---|
|  | 195 | for (TesselPointList::const_iterator Runner = a.pointlist.begin(); Runner != a.pointlist.end(); Runner++) | 
|---|
|  | 196 | ost << *(*Runner) << " "; | 
|---|
|  | 197 | ost << " at angle " << (a.ShortestAngle) << "."; | 
|---|
|  | 198 | } | 
|---|
|  | 199 |  | 
|---|
|  | 200 | return ost; | 
|---|
|  | 201 | } | 
|---|
|  | 202 | ; | 
|---|
|  | 203 |  | 
|---|