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