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