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