1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * triangleintersectionlist.cpp
|
---|
10 | *
|
---|
11 | * This files encapsulates the TriangleIntersectionList class where all matters related to points in space being how close to
|
---|
12 | * a tesselated surface are answered, i.e. distance, is inside, ...
|
---|
13 | *
|
---|
14 | * Created on: Mar 1, 2010
|
---|
15 | * Author: heber
|
---|
16 | */
|
---|
17 |
|
---|
18 | // include config.h
|
---|
19 | #ifdef HAVE_CONFIG_H
|
---|
20 | #include <config.h>
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #include "CodePatterns/MemDebug.hpp"
|
---|
24 |
|
---|
25 | #include <boost/scoped_ptr.hpp>
|
---|
26 |
|
---|
27 | #include "triangleintersectionlist.hpp"
|
---|
28 |
|
---|
29 | #include "tesselation.hpp"
|
---|
30 | #include "BoundaryMaps.hpp"
|
---|
31 | #include "BoundaryPointSet.hpp"
|
---|
32 | #include "BoundaryLineSet.hpp"
|
---|
33 | #include "BoundaryTriangleSet.hpp"
|
---|
34 | #include "CodePatterns/Info.hpp"
|
---|
35 | #include "CodePatterns/Log.hpp"
|
---|
36 | #include "CodePatterns/Verbose.hpp"
|
---|
37 | #include "Helpers/defs.hpp"
|
---|
38 | #include "LinearAlgebra/Vector.hpp"
|
---|
39 |
|
---|
40 | /** Constructor for class TriangleIntersectionList.
|
---|
41 | *
|
---|
42 | */
|
---|
43 | TriangleIntersectionList::TriangleIntersectionList(const Vector &x, const Tesselation *surface, const LinkedCell_deprecated* LC) :
|
---|
44 | Point(x),
|
---|
45 | Tess(surface),
|
---|
46 | Vicinity(LC)
|
---|
47 | {
|
---|
48 | //Info FunctionInfo(__func__);
|
---|
49 | GatherIntersectionsWithTriangles();
|
---|
50 | };
|
---|
51 |
|
---|
52 | /** Destructor for class TriangleIntersectionList.
|
---|
53 | * Free's all allocated intersection vectors
|
---|
54 | */
|
---|
55 | TriangleIntersectionList::~TriangleIntersectionList()
|
---|
56 | {
|
---|
57 | //Info FunctionInfo(__func__);
|
---|
58 | for (TriangleVectorMap::iterator Runner = IntersectionList.begin(); Runner != IntersectionList.end(); Runner++)
|
---|
59 | delete((*Runner).second);
|
---|
60 | };
|
---|
61 |
|
---|
62 | /** Returns the smallest distance between TriangleIntersectionList::Point and the surface given by
|
---|
63 | * TriangleIntersectionList::Tess.
|
---|
64 | * \return distance >=0, -1 if no specific distance could be found
|
---|
65 | */
|
---|
66 | double TriangleIntersectionList::GetSmallestDistance() const
|
---|
67 | {
|
---|
68 | //Info FunctionInfo(__func__);
|
---|
69 | FillDistanceList();
|
---|
70 | if (!DistanceList.empty())
|
---|
71 | return DistanceList.begin()->first;
|
---|
72 | else
|
---|
73 | // return reference, if none can be found
|
---|
74 | return -1.;
|
---|
75 | };
|
---|
76 |
|
---|
77 | /** Returns the vector of closest intersection between TriangleIntersectionList::Point and the surface
|
---|
78 | * TriangleIntersectionList::Tess.
|
---|
79 | * \return Intersection vector or TriangleIntersectionList::Point if none could be found
|
---|
80 | */
|
---|
81 | Vector TriangleIntersectionList::GetClosestIntersection() const
|
---|
82 | {
|
---|
83 | //Info FunctionInfo(__func__);
|
---|
84 | TriangleVectorMap::const_iterator runner;
|
---|
85 | runner = GetIteratortoSmallestDistance();
|
---|
86 | if (runner != IntersectionList.end()) {
|
---|
87 | return *((*runner).second);
|
---|
88 | }
|
---|
89 | // return reference, if none can be found
|
---|
90 | return Point;
|
---|
91 | };
|
---|
92 |
|
---|
93 | /** Returns the triangle closest to TriangleIntersectionList::Point and the surface
|
---|
94 | * TriangleIntersectionList::Tess.
|
---|
95 | * \return pointer to triangle or NULL if none could be found
|
---|
96 | */
|
---|
97 | BoundaryTriangleSet * TriangleIntersectionList::GetClosestTriangle() const
|
---|
98 | {
|
---|
99 | //Info FunctionInfo(__func__);
|
---|
100 | TriangleVectorMap::const_iterator runner;
|
---|
101 | runner = GetIteratortoSmallestDistance();
|
---|
102 | if (runner != IntersectionList.end()) {
|
---|
103 | return ((*runner).first);
|
---|
104 | }
|
---|
105 | // return reference, if none can be found
|
---|
106 | return NULL;
|
---|
107 | };
|
---|
108 |
|
---|
109 | /** Checks whether reference TriangleIntersectionList::Point of this class is inside or outside.
|
---|
110 | * \return true - TriangleIntersectionList::Point is inside, false - is outside
|
---|
111 | */
|
---|
112 | bool TriangleIntersectionList::IsInside() const
|
---|
113 | {
|
---|
114 | //Info FunctionInfo(__func__);
|
---|
115 | TriangleVectorMap::const_iterator runner = GetIteratortoSmallestDistance();
|
---|
116 | if (runner != IntersectionList.end()) {
|
---|
117 | // if we have found one, check Scalarproduct between the vector
|
---|
118 | Vector TestVector = (Point) - (*(*runner).second);
|
---|
119 | LOG(1, "INFO: Distance vector between point " << Point
|
---|
120 | << " and triangle intersection at " << (*(*runner).second) << " is "
|
---|
121 | << TestVector << ".");
|
---|
122 | if (fabs(TestVector.NormSquared()) < MYEPSILON) {//
|
---|
123 | LOG(1, "ACCEPT: Point is on the intersected triangle.");
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 | const double sign = (*runner).first->NormalVector.ScalarProduct(TestVector);
|
---|
127 | LOG(1, "INFO: Checking sign of SKP between Distance vector and triangle's NormalVector "
|
---|
128 | << (*runner).first->NormalVector << ": " << sign << ".");
|
---|
129 | if (sign < 0) {
|
---|
130 | LOG(1, "ACCEPT: Point lies on inner side of intersected triangle.");
|
---|
131 | return true;
|
---|
132 | } else {
|
---|
133 | LOG(1, "REJECT: Point lies on outer side of intersected triangle.");
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | // so far away from surface that there are no triangles in list
|
---|
138 | return false;
|
---|
139 | };
|
---|
140 |
|
---|
141 | /** Puts all triangles in vicinity (by \a *LC) into a hash map with Intersection vectors.
|
---|
142 | * Private function for constructor of TriangleIntersectionList.
|
---|
143 | */
|
---|
144 | void TriangleIntersectionList::GatherIntersectionsWithTriangles()
|
---|
145 | {
|
---|
146 | //Info FunctionInfo(__func__);
|
---|
147 |
|
---|
148 | // get closest points
|
---|
149 | boost::scoped_ptr< DistanceToPointMap > points(Tess->FindClosestBoundaryPointsToVector(Point,Vicinity));
|
---|
150 | if (points == NULL) {
|
---|
151 | ELOG(1, "There is no nearest point: too far away from the surface.");
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // gather all triangles in *LC-neighbourhood
|
---|
156 | TriangleSet triangles;
|
---|
157 | for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++)
|
---|
158 | for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++)
|
---|
159 | for (TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++)
|
---|
160 | triangles.insert(TriangleRunner->second);
|
---|
161 |
|
---|
162 | Vector *Intersection = NULL;
|
---|
163 | for (TriangleSet::const_iterator TriangleRunner = triangles.begin(); TriangleRunner != triangles.end(); TriangleRunner++) {
|
---|
164 | // get intersection with triangle plane
|
---|
165 | Intersection = new Vector;
|
---|
166 | (*TriangleRunner)->GetClosestPointInsideTriangle(Point, *Intersection);
|
---|
167 | //LOG(1, "Intersection between " << Point << " and " << **TriangleRunner << " is at " << *Intersection << ".");
|
---|
168 | IntersectionList.insert( pair<BoundaryTriangleSet *, Vector * > (*TriangleRunner, Intersection) );
|
---|
169 | }
|
---|
170 | };
|
---|
171 |
|
---|
172 | /** Fills the private distance list
|
---|
173 | */
|
---|
174 | void TriangleIntersectionList::FillDistanceList() const
|
---|
175 | {
|
---|
176 | //Info FunctionInfo(__func__);
|
---|
177 | if (DistanceList.empty())
|
---|
178 | for (TriangleVectorMap::const_iterator runner = IntersectionList.begin(); runner != IntersectionList.end(); runner++)
|
---|
179 | DistanceList.insert( pair<double, BoundaryTriangleSet *> (Point.distance(*(*runner).second), (*runner).first) );
|
---|
180 |
|
---|
181 | //for (DistanceTriangleMap::const_iterator runner = DistanceList.begin(); runner != DistanceList.end(); runner++)
|
---|
182 | // LOG(1, (*runner).first << " away from " << *(*runner).second);
|
---|
183 | };
|
---|
184 |
|
---|
185 |
|
---|
186 | /** Returns the iterator in VectorTriangleMap to the smallest distance.
|
---|
187 | * This is a private helper function as the iterator is then evaluated in some other functions.
|
---|
188 | * \return iterator or TriangleIntersectionList::IntersectionList.end() if none could be found
|
---|
189 | */
|
---|
190 | TriangleVectorMap::const_iterator TriangleIntersectionList::GetIteratortoSmallestDistance() const
|
---|
191 | {
|
---|
192 | //Info FunctionInfo(__func__);
|
---|
193 | FillDistanceList();
|
---|
194 |
|
---|
195 | // do we have any intersections?
|
---|
196 | TriangleVectorMap::const_iterator runner = IntersectionList.end();
|
---|
197 | if (!DistanceList.empty()) {
|
---|
198 | // get closest triangle(s)
|
---|
199 | std::pair< DistanceTriangleMap::const_iterator, DistanceTriangleMap::const_iterator > DistanceRange =
|
---|
200 | DistanceList.equal_range(DistanceList.begin()->first);
|
---|
201 | {
|
---|
202 | size_t count = 0;
|
---|
203 | for (DistanceTriangleMap::const_iterator iter = DistanceRange.first;
|
---|
204 | iter != DistanceRange.second; ++iter)
|
---|
205 | ++count;
|
---|
206 | LOG(1, "INFO: There are " << count << " possible triangles at the smallest distance.");
|
---|
207 | }
|
---|
208 | // if there is more than one, check all of their normal vectors
|
---|
209 | // return the one with positive SKP if present because if the point
|
---|
210 | // would be truely inside, all of the closest triangles would have to show
|
---|
211 | // their inner side
|
---|
212 | LOG(1, "INFO: Looking for first SKP greater than zero ...");
|
---|
213 | for (DistanceTriangleMap::const_iterator iter = DistanceRange.first;
|
---|
214 | iter != DistanceRange.second; ++iter) {
|
---|
215 | // find the entry by the triangle key in IntersectionList to get the Intersection point
|
---|
216 | runner = IntersectionList.find(iter->second);
|
---|
217 | Vector TestVector = (Point) - (*(*runner).second);
|
---|
218 | // construct SKP
|
---|
219 | const double sign = (*iter).second->NormalVector.ScalarProduct(TestVector);
|
---|
220 | LOG(1, "INFO: Checking SKP of closest triangle " << *(iter->second) << " = " << sign << ".");
|
---|
221 | // if positive return
|
---|
222 | if (sign > 0)
|
---|
223 | break;
|
---|
224 | }
|
---|
225 | // if there's just one or all are negative, runner is not set yet
|
---|
226 | if (runner == IntersectionList.end())
|
---|
227 | runner = IntersectionList.find(DistanceList.begin()->second);
|
---|
228 | }
|
---|
229 | return runner;
|
---|
230 | };
|
---|
231 |
|
---|