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