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