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