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