| [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 |  | 
|---|
| [8db598] | 8 | /* | 
|---|
|  | 9 | * triangleintersectionlist.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | * This files encapsulates the TriangleIntersectionList class where all matters related to points in space being how close to | 
|---|
|  | 12 | * a tesselated surface are answered, i.e. distance, is inside, ... | 
|---|
|  | 13 | * | 
|---|
|  | 14 | *  Created on: Mar 1, 2010 | 
|---|
|  | 15 | *      Author: heber | 
|---|
|  | 16 | */ | 
|---|
|  | 17 |  | 
|---|
| [bf3817] | 18 | // include config.h | 
|---|
|  | 19 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 20 | #include <config.h> | 
|---|
|  | 21 | #endif | 
|---|
|  | 22 |  | 
|---|
| [ad011c] | 23 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 24 |  | 
|---|
| [bdc91e] | 25 | #include <boost/scoped_ptr.hpp> | 
|---|
|  | 26 |  | 
|---|
| [8db598] | 27 | #include "triangleintersectionlist.hpp" | 
|---|
|  | 28 |  | 
|---|
| [ad011c] | 29 | #include "CodePatterns/Info.hpp" | 
|---|
| [8f4df1] | 30 | #include "BoundaryMaps.hpp" | 
|---|
| [d74077] | 31 | #include "BoundaryPointSet.hpp" | 
|---|
|  | 32 | #include "BoundaryLineSet.hpp" | 
|---|
|  | 33 | #include "BoundaryTriangleSet.hpp" | 
|---|
| [8db598] | 34 | #include "tesselation.hpp" | 
|---|
| [57f243] | 35 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [ad011c] | 36 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [8db598] | 37 |  | 
|---|
|  | 38 | /** Constructor for class TriangleIntersectionList. | 
|---|
|  | 39 | * | 
|---|
|  | 40 | */ | 
|---|
| [d74077] | 41 | TriangleIntersectionList::TriangleIntersectionList(const Vector &x, const Tesselation *surface, const LinkedCell* LC) : | 
|---|
| [8db598] | 42 | Point(x), | 
|---|
|  | 43 | Tess(surface), | 
|---|
|  | 44 | Vicinity(LC) | 
|---|
|  | 45 | { | 
|---|
|  | 46 | Info FunctionInfo(__func__); | 
|---|
|  | 47 | GatherIntersectionsWithTriangles(); | 
|---|
|  | 48 | }; | 
|---|
|  | 49 |  | 
|---|
|  | 50 | /** Destructor for class TriangleIntersectionList. | 
|---|
|  | 51 | * Free's all allocated intersection vectors | 
|---|
|  | 52 | */ | 
|---|
|  | 53 | TriangleIntersectionList::~TriangleIntersectionList() | 
|---|
|  | 54 | { | 
|---|
|  | 55 | Info FunctionInfo(__func__); | 
|---|
|  | 56 | for (TriangleVectorMap::iterator Runner = IntersectionList.begin(); Runner != IntersectionList.end(); Runner++) | 
|---|
|  | 57 | delete((*Runner).second); | 
|---|
|  | 58 | }; | 
|---|
|  | 59 |  | 
|---|
|  | 60 | /** Returns the smallest distance between TriangleIntersectionList::Point and the surface given by | 
|---|
|  | 61 | * TriangleIntersectionList::Tess. | 
|---|
|  | 62 | * \return distance >=0, -1 if no specific distance could be found | 
|---|
|  | 63 | */ | 
|---|
|  | 64 | double TriangleIntersectionList::GetSmallestDistance() const | 
|---|
|  | 65 | { | 
|---|
|  | 66 | Info FunctionInfo(__func__); | 
|---|
|  | 67 | FillDistanceList(); | 
|---|
|  | 68 | if (!DistanceList.empty()) | 
|---|
|  | 69 | return DistanceList.begin()->first; | 
|---|
|  | 70 | else | 
|---|
|  | 71 | // return reference, if none can be found | 
|---|
|  | 72 | return -1.; | 
|---|
|  | 73 | }; | 
|---|
|  | 74 |  | 
|---|
|  | 75 | /** Returns the vector of closest intersection between TriangleIntersectionList::Point and the surface | 
|---|
|  | 76 | * TriangleIntersectionList::Tess. | 
|---|
|  | 77 | * \return Intersection vector or TriangleIntersectionList::Point if none could be found | 
|---|
|  | 78 | */ | 
|---|
|  | 79 | Vector TriangleIntersectionList::GetClosestIntersection() const | 
|---|
|  | 80 | { | 
|---|
|  | 81 | Info FunctionInfo(__func__); | 
|---|
|  | 82 | TriangleVectorMap::const_iterator runner; | 
|---|
|  | 83 | runner = GetIteratortoSmallestDistance(); | 
|---|
|  | 84 | if (runner != IntersectionList.end()) { | 
|---|
|  | 85 | return *((*runner).second); | 
|---|
|  | 86 | } | 
|---|
|  | 87 | // return reference, if none can be found | 
|---|
| [d74077] | 88 | return Point; | 
|---|
| [8db598] | 89 | }; | 
|---|
|  | 90 |  | 
|---|
|  | 91 | /** Returns the triangle closest to TriangleIntersectionList::Point and the surface | 
|---|
|  | 92 | * TriangleIntersectionList::Tess. | 
|---|
|  | 93 | * \return pointer to triangle or NULL if none could be found | 
|---|
|  | 94 | */ | 
|---|
|  | 95 | BoundaryTriangleSet * TriangleIntersectionList::GetClosestTriangle() const | 
|---|
|  | 96 | { | 
|---|
|  | 97 | Info FunctionInfo(__func__); | 
|---|
|  | 98 | TriangleVectorMap::const_iterator runner; | 
|---|
|  | 99 | runner = GetIteratortoSmallestDistance(); | 
|---|
|  | 100 | if (runner != IntersectionList.end()) { | 
|---|
|  | 101 | return ((*runner).first); | 
|---|
|  | 102 | } | 
|---|
|  | 103 | // return reference, if none can be found | 
|---|
|  | 104 | return NULL; | 
|---|
|  | 105 | }; | 
|---|
|  | 106 |  | 
|---|
|  | 107 | /** Checks whether reference TriangleIntersectionList::Point of this class is inside or outside. | 
|---|
|  | 108 | * \return true - TriangleIntersectionList::Point is inside, false - is outside | 
|---|
|  | 109 | */ | 
|---|
|  | 110 | bool TriangleIntersectionList::IsInside() const | 
|---|
|  | 111 | { | 
|---|
|  | 112 | Info FunctionInfo(__func__); | 
|---|
|  | 113 | TriangleVectorMap::const_iterator runner; | 
|---|
|  | 114 | runner = GetIteratortoSmallestDistance(); | 
|---|
|  | 115 | if (runner != IntersectionList.end()) { | 
|---|
|  | 116 | // if we have found one, check Scalarproduct between the vector | 
|---|
| [d74077] | 117 | Vector TestVector = (Point) - (*(*runner).second); | 
|---|
| [8db598] | 118 | if (fabs(TestVector.NormSquared()) < MYEPSILON) // | 
|---|
|  | 119 | return true; | 
|---|
| [8cbb97] | 120 | const double sign = (*runner).first->NormalVector.ScalarProduct(TestVector); | 
|---|
| [8db598] | 121 | if (sign < 0) { | 
|---|
|  | 122 | return true; | 
|---|
|  | 123 | } else { | 
|---|
|  | 124 | return false; | 
|---|
|  | 125 | } | 
|---|
|  | 126 | } | 
|---|
|  | 127 | // so far away from surface that there are no triangles in list | 
|---|
|  | 128 | return false; | 
|---|
|  | 129 | }; | 
|---|
|  | 130 |  | 
|---|
|  | 131 | /** Puts all triangles in vicinity (by \a *LC) into a hash map with Intersection vectors. | 
|---|
|  | 132 | * Private function for constructor of TriangleIntersectionList. | 
|---|
|  | 133 | */ | 
|---|
|  | 134 | void TriangleIntersectionList::GatherIntersectionsWithTriangles() | 
|---|
|  | 135 | { | 
|---|
|  | 136 | Info FunctionInfo(__func__); | 
|---|
|  | 137 |  | 
|---|
|  | 138 | // get closest points | 
|---|
| [bdc91e] | 139 | boost::scoped_ptr< DistanceToPointMap > points(Tess->FindClosestBoundaryPointsToVector(Point,Vicinity)); | 
|---|
| [8db598] | 140 | if (points == NULL) { | 
|---|
| [58ed4a] | 141 | DoeLog(1) && (eLog()<< Verbose(1) << "There is no nearest point: too far away from the surface." << endl); | 
|---|
| [8db598] | 142 | return; | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
|  | 145 | // gather all triangles in *LC-neighbourhood | 
|---|
|  | 146 | TriangleSet triangles; | 
|---|
|  | 147 | for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++) | 
|---|
|  | 148 | for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) | 
|---|
|  | 149 | for (TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++) | 
|---|
|  | 150 | triangles.insert(TriangleRunner->second); | 
|---|
|  | 151 |  | 
|---|
|  | 152 | Vector *Intersection = NULL; | 
|---|
|  | 153 | for (TriangleSet::const_iterator TriangleRunner = triangles.begin(); TriangleRunner != triangles.end(); TriangleRunner++) { | 
|---|
|  | 154 | // get intersection with triangle plane | 
|---|
|  | 155 | Intersection = new Vector; | 
|---|
| [d74077] | 156 | (*TriangleRunner)->GetClosestPointInsideTriangle(Point, *Intersection); | 
|---|
|  | 157 | //Log() << Verbose(1) << "Intersection between " << Point << " and " << **TriangleRunner << " is at " << *Intersection << "." << endl; | 
|---|
| [8db598] | 158 | IntersectionList.insert( pair<BoundaryTriangleSet *, Vector * > (*TriangleRunner, Intersection) ); | 
|---|
|  | 159 | } | 
|---|
|  | 160 | }; | 
|---|
|  | 161 |  | 
|---|
|  | 162 | /** Fills the private distance list | 
|---|
|  | 163 | */ | 
|---|
|  | 164 | void TriangleIntersectionList::FillDistanceList() const | 
|---|
|  | 165 | { | 
|---|
|  | 166 | Info FunctionInfo(__func__); | 
|---|
|  | 167 | if (DistanceList.empty()) | 
|---|
|  | 168 | for (TriangleVectorMap::const_iterator runner = IntersectionList.begin(); runner != IntersectionList.end(); runner++) | 
|---|
| [d74077] | 169 | DistanceList.insert( pair<double, BoundaryTriangleSet *> (Point.distance(*(*runner).second), (*runner).first) ); | 
|---|
| [8db598] | 170 |  | 
|---|
|  | 171 | //for (DistanceTriangleMap::const_iterator runner = DistanceList.begin(); runner != DistanceList.end(); runner++) | 
|---|
| [bc84ffc] | 172 | //  Log() << Verbose(1) << (*runner).first << " away from " << *(*runner).second << endl; | 
|---|
| [8db598] | 173 | }; | 
|---|
|  | 174 |  | 
|---|
|  | 175 |  | 
|---|
|  | 176 | /** Returns the iterator in VectorTriangleMap to the smallest distance. | 
|---|
|  | 177 | * This is a private helper function as the iterator is then evaluated in some other functions. | 
|---|
|  | 178 | * \return iterator or TriangleIntersectionList::IntersectionList.end() if none could be found | 
|---|
|  | 179 | */ | 
|---|
|  | 180 | TriangleVectorMap::const_iterator TriangleIntersectionList::GetIteratortoSmallestDistance() const | 
|---|
|  | 181 | { | 
|---|
|  | 182 | Info FunctionInfo(__func__); | 
|---|
|  | 183 | FillDistanceList(); | 
|---|
|  | 184 |  | 
|---|
|  | 185 | // do we have any intersections? | 
|---|
|  | 186 | TriangleVectorMap::const_iterator runner; | 
|---|
|  | 187 | if (!DistanceList.empty()) { | 
|---|
|  | 188 | // get closest triangle | 
|---|
|  | 189 | runner = IntersectionList.find(DistanceList.begin()->second); | 
|---|
|  | 190 | } | 
|---|
|  | 191 | return runner; | 
|---|
|  | 192 | }; | 
|---|
|  | 193 |  | 
|---|