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