source: molecuilder/src/tesselation.cpp@ 401b97

Last change on this file since 401b97 was 4eee8f, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Declared the Vector class as single point spaces

  • Property mode set to 100644
File size: 230.1 KB
RevLine 
[834ff3]1/*
2 * tesselation.cpp
3 *
4 * Created on: Aug 3, 2009
5 * Author: heber
6 */
7
[17b3a5c]8#include <fstream>
[d22a30]9#include <assert.h>
[17b3a5c]10
[c419f2]11#include "helpers.hpp"
[be2997]12#include "info.hpp"
[60cbe5]13#include "linkedcell.hpp"
[543ce4]14#include "log.hpp"
[834ff3]15#include "tesselation.hpp"
[60cbe5]16#include "tesselationhelpers.hpp"
[f7b476]17#include "triangleintersectionlist.hpp"
[60cbe5]18#include "vector.hpp"
[71910a]19#include "vector_ops.hpp"
[17b3a5c]20#include "verbose.hpp"
[71910a]21#include "Plane.hpp"
22#include "Exceptions/LinearDependenceException.hpp"
[60cbe5]23
24class molecule;
[834ff3]25
26// ======================================== Points on Boundary =================================
27
[eb58a7]28/** Constructor of BoundaryPointSet.
29 */
[4af89b]30BoundaryPointSet::BoundaryPointSet() :
[c43766]31 LinesCount(0), value(0.), Nr(-1)
[834ff3]32{
[c43766]33 Info FunctionInfo(__func__);
[1f2e46]34 DoLog(1) && (Log() << Verbose(1) << "Adding noname." << endl);
[c43766]35}
36;
[834ff3]37
[eb58a7]38/** Constructor of BoundaryPointSet with Tesselpoint.
39 * \param *Walker TesselPoint this boundary point represents
40 */
[1aa2a5]41BoundaryPointSet::BoundaryPointSet(TesselPoint * const Walker) :
[c43766]42 LinesCount(0), node(Walker), value(0.), Nr(Walker->nr)
[834ff3]43{
[c43766]44 Info FunctionInfo(__func__);
[1f2e46]45 DoLog(1) && (Log() << Verbose(1) << "Adding Node " << *Walker << endl);
[c43766]46}
47;
[834ff3]48
[eb58a7]49/** Destructor of BoundaryPointSet.
50 * Sets node to NULL to avoid removing the original, represented TesselPoint.
51 * \note When removing point from a class Tesselation, use RemoveTesselationPoint()
52 */
[834ff3]53BoundaryPointSet::~BoundaryPointSet()
54{
[c43766]55 Info FunctionInfo(__func__);
[be2997]56 //Log() << Verbose(0) << "Erasing point nr. " << Nr << "." << endl;
[834ff3]57 if (!lines.empty())
[c43766]58 DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some lines." << endl);
[834ff3]59 node = NULL;
[c43766]60}
61;
[834ff3]62
[eb58a7]63/** Add a line to the LineMap of this point.
64 * \param *line line to add
65 */
[1aa2a5]66void BoundaryPointSet::AddLine(BoundaryLineSet * const line)
[834ff3]67{
[c43766]68 Info FunctionInfo(__func__);
[1f2e46]69 DoLog(1) && (Log() << Verbose(1) << "Adding " << *this << " to line " << *line << "." << endl);
[c43766]70 if (line->endpoints[0] == this) {
71 lines.insert(LinePair(line->endpoints[1]->Nr, line));
72 } else {
73 lines.insert(LinePair(line->endpoints[0]->Nr, line));
74 }
[834ff3]75 LinesCount++;
[c43766]76}
77;
[834ff3]78
[eb58a7]79/** output operator for BoundaryPointSet.
80 * \param &ost output stream
81 * \param &a boundary point
82 */
[a9b2a0a]83ostream & operator <<(ostream &ost, const BoundaryPointSet &a)
[834ff3]84{
[60cbe5]85 ost << "[" << a.Nr << "|" << a.node->Name << " at " << *a.node->node << "]";
[834ff3]86 return ost;
87}
88;
89
90// ======================================== Lines on Boundary =================================
91
[eb58a7]92/** Constructor of BoundaryLineSet.
93 */
[4af89b]94BoundaryLineSet::BoundaryLineSet() :
[c43766]95 Nr(-1)
[834ff3]96{
[c43766]97 Info FunctionInfo(__func__);
[834ff3]98 for (int i = 0; i < 2; i++)
99 endpoints[i] = NULL;
[c43766]100}
101;
[834ff3]102
[eb58a7]103/** Constructor of BoundaryLineSet with two endpoints.
104 * Adds line automatically to each endpoints' LineMap
105 * \param *Point[2] array of two boundary points
106 * \param number number of the list
107 */
[1aa2a5]108BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point[2], const int number)
[834ff3]109{
[c43766]110 Info FunctionInfo(__func__);
[834ff3]111 // set number
112 Nr = number;
113 // set endpoints in ascending order
114 SetEndpointsOrdered(endpoints, Point[0], Point[1]);
115 // add this line to the hash maps of both endpoints
116 Point[0]->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
117 Point[1]->AddLine(this); //
[4af89b]118 // set skipped to false
119 skipped = false;
[834ff3]120 // clear triangles list
[1f2e46]121 DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
[c43766]122}
123;
[834ff3]124
[1aa2a5]125/** Constructor of BoundaryLineSet with two endpoints.
126 * Adds line automatically to each endpoints' LineMap
127 * \param *Point1 first boundary point
128 * \param *Point2 second boundary point
129 * \param number number of the list
130 */
131BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point1, BoundaryPointSet * const Point2, const int number)
132{
133 Info FunctionInfo(__func__);
134 // set number
135 Nr = number;
136 // set endpoints in ascending order
137 SetEndpointsOrdered(endpoints, Point1, Point2);
138 // add this line to the hash maps of both endpoints
139 Point1->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
140 Point2->AddLine(this); //
141 // set skipped to false
142 skipped = false;
143 // clear triangles list
[1f2e46]144 DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
[c43766]145}
146;
[1aa2a5]147
[eb58a7]148/** Destructor for BoundaryLineSet.
149 * Removes itself from each endpoints' LineMap, calling RemoveTrianglePoint() when point not connected anymore.
150 * \note When removing lines from a class Tesselation, use RemoveTesselationLine()
151 */
[834ff3]152BoundaryLineSet::~BoundaryLineSet()
153{
[c43766]154 Info FunctionInfo(__func__);
[834ff3]155 int Numbers[2];
[eb58a7]156
157 // get other endpoint number of finding copies of same line
158 if (endpoints[1] != NULL)
159 Numbers[0] = endpoints[1]->Nr;
160 else
161 Numbers[0] = -1;
162 if (endpoints[0] != NULL)
163 Numbers[1] = endpoints[0]->Nr;
164 else
165 Numbers[1] = -1;
166
[834ff3]167 for (int i = 0; i < 2; i++) {
[eb58a7]168 if (endpoints[i] != NULL) {
169 if (Numbers[i] != -1) { // as there may be multiple lines with same endpoints, we have to go through each and find in the endpoint's line list this line set
170 pair<LineMap::iterator, LineMap::iterator> erasor = endpoints[i]->lines.equal_range(Numbers[i]);
171 for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
172 if ((*Runner).second == this) {
[be2997]173 //Log() << Verbose(0) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
[eb58a7]174 endpoints[i]->lines.erase(Runner);
175 break;
176 }
177 } else { // there's just a single line left
[60cbe5]178 if (endpoints[i]->lines.erase(Nr)) {
[be2997]179 //Log() << Verbose(0) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
[60cbe5]180 }
[834ff3]181 }
[eb58a7]182 if (endpoints[i]->lines.empty()) {
[be2997]183 //Log() << Verbose(0) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl;
[eb58a7]184 if (endpoints[i] != NULL) {
[c43766]185 delete (endpoints[i]);
[eb58a7]186 endpoints[i] = NULL;
187 }
188 }
189 }
[834ff3]190 }
191 if (!triangles.empty())
[c43766]192 DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl);
193}
194;
[834ff3]195
[eb58a7]196/** Add triangle to TriangleMap of this boundary line.
197 * \param *triangle to add
198 */
[1aa2a5]199void BoundaryLineSet::AddTriangle(BoundaryTriangleSet * const triangle)
[834ff3]200{
[c43766]201 Info FunctionInfo(__func__);
[1f2e46]202 DoLog(0) && (Log() << Verbose(0) << "Add " << triangle->Nr << " to line " << *this << "." << endl);
[834ff3]203 triangles.insert(TrianglePair(triangle->Nr, triangle));
[c43766]204}
205;
[834ff3]206
207/** Checks whether we have a common endpoint with given \a *line.
208 * \param *line other line to test
209 * \return true - common endpoint present, false - not connected
210 */
[1aa2a5]211bool BoundaryLineSet::IsConnectedTo(const BoundaryLineSet * const line) const
[834ff3]212{
[c43766]213 Info FunctionInfo(__func__);
[834ff3]214 if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1]))
215 return true;
216 else
217 return false;
[c43766]218}
219;
[834ff3]220
221/** Checks whether the adjacent triangles of a baseline are convex or not.
[60cbe5]222 * We sum the two angles of each height vector with respect to the center of the baseline.
[834ff3]223 * If greater/equal M_PI than we are convex.
224 * \param *out output stream for debugging
225 * \return true - triangles are convex, false - concave or less than two triangles connected
226 */
[1aa2a5]227bool BoundaryLineSet::CheckConvexityCriterion() const
[834ff3]228{
[c43766]229 Info FunctionInfo(__func__);
[0cf171]230 Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck;
[834ff3]231 // get the two triangles
[0cf171]232 if (triangles.size() != 2) {
[c43766]233 DoeLog(0) && (eLog() << Verbose(0) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl);
[78dac6]234 return true;
[834ff3]235 }
[0cf171]236 // check normal vectors
[834ff3]237 // have a normal vector on the base line pointing outwards
[be2997]238 //Log() << Verbose(0) << "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << "." << endl;
[1f591b]239 BaseLineCenter = (1./2.)*((*endpoints[0]->node->node) + (*endpoints[1]->node->node));
240 BaseLine = (*endpoints[0]->node->node) - (*endpoints[1]->node->node);
[0d111b]241
[be2997]242 //Log() << Verbose(0) << "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << "." << endl;
[834ff3]243
[eeefb3]244 BaseLineNormal.Zero();
[0cf171]245 NormalCheck.Zero();
246 double sign = -1.;
[c43766]247 int i = 0;
[eeefb3]248 class BoundaryPointSet *node = NULL;
[c43766]249 for (TriangleMap::const_iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
[be2997]250 //Log() << Verbose(0) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl;
[1f591b]251 NormalCheck += runner->second->NormalVector;
252 NormalCheck *= sign;
[0cf171]253 sign = -sign;
[60cbe5]254 if (runner->second->NormalVector.NormSquared() > MYEPSILON)
[1f591b]255 BaseLineNormal = runner->second->NormalVector; // yes, copy second on top of first
[60cbe5]256 else {
[c43766]257 DoeLog(0) && (eLog() << Verbose(0) << "Triangle " << *runner->second << " has zero normal vector!" << endl);
[60cbe5]258 }
[eeefb3]259 node = runner->second->GetThirdEndpoint(this);
260 if (node != NULL) {
[be2997]261 //Log() << Verbose(0) << "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << "." << endl;
[1f591b]262 helper[i] = (*node->node->node) - BaseLineCenter;
[71910a]263 helper[i].MakeNormalTo(BaseLine); // we want to compare the triangle's heights' angles!
[be2997]264 //Log() << Verbose(0) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl;
[eeefb3]265 i++;
266 } else {
[c43766]267 DoeLog(1) && (eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl);
[eeefb3]268 return true;
269 }
270 }
[be2997]271 //Log() << Verbose(0) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl;
[0cf171]272 if (NormalCheck.NormSquared() < MYEPSILON) {
[1f2e46]273 DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl);
[0cf171]274 return true;
[eeefb3]275 }
[60cbe5]276 BaseLineNormal.Scale(-1.);
[48cd93]277 double angle = GetAngle(helper[0], helper[1], BaseLineNormal);
[78dac6]278 if ((angle - M_PI) > -MYEPSILON) {
[1f2e46]279 DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Angle is greater than pi: convex." << endl);
[834ff3]280 return true;
[78dac6]281 } else {
[1f2e46]282 DoLog(0) && (Log() << Verbose(0) << "REJECT: Angle is less than pi: concave." << endl);
[834ff3]283 return false;
[78dac6]284 }
[834ff3]285}
286
287/** Checks whether point is any of the two endpoints this line contains.
288 * \param *point point to test
289 * \return true - point is of the line, false - is not
290 */
[1aa2a5]291bool BoundaryLineSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
[834ff3]292{
[c43766]293 Info FunctionInfo(__func__);
294 for (int i = 0; i < 2; i++)
[834ff3]295 if (point == endpoints[i])
296 return true;
297 return false;
[c43766]298}
299;
[834ff3]300
[eeefb3]301/** Returns other endpoint of the line.
302 * \param *point other endpoint
303 * \return NULL - if endpoint not contained in BoundaryLineSet, or pointer to BoundaryPointSet otherwise
304 */
[1aa2a5]305class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(const BoundaryPointSet * const point) const
[eeefb3]306{
[c43766]307 Info FunctionInfo(__func__);
[eeefb3]308 if (endpoints[0] == point)
309 return endpoints[1];
310 else if (endpoints[1] == point)
311 return endpoints[0];
312 else
313 return NULL;
[c43766]314}
315;
[eeefb3]316
[eb58a7]317/** output operator for BoundaryLineSet.
318 * \param &ost output stream
319 * \param &a boundary line
320 */
[c43766]321ostream & operator <<(ostream &ost, const BoundaryLineSet &a)
[834ff3]322{
[60cbe5]323 ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << "," << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "]";
[834ff3]324 return ost;
[c43766]325}
326;
[834ff3]327
328// ======================================== Triangles on Boundary =================================
329
[eb58a7]330/** Constructor for BoundaryTriangleSet.
331 */
[4af89b]332BoundaryTriangleSet::BoundaryTriangleSet() :
333 Nr(-1)
[834ff3]334{
[c43766]335 Info FunctionInfo(__func__);
336 for (int i = 0; i < 3; i++) {
337 endpoints[i] = NULL;
338 lines[i] = NULL;
339 }
340}
341;
[834ff3]342
[eb58a7]343/** Constructor for BoundaryTriangleSet with three lines.
344 * \param *line[3] lines that make up the triangle
345 * \param number number of triangle
346 */
[1aa2a5]347BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet * const line[3], const int number) :
[4af89b]348 Nr(number)
[834ff3]349{
[c43766]350 Info FunctionInfo(__func__);
[834ff3]351 // set number
352 // set lines
[be2997]353 for (int i = 0; i < 3; i++) {
354 lines[i] = line[i];
355 lines[i]->AddTriangle(this);
356 }
[834ff3]357 // get ascending order of endpoints
[be2997]358 PointMap OrderMap;
[834ff3]359 for (int i = 0; i < 3; i++)
360 // for all three lines
[be2997]361 for (int j = 0; j < 2; j++) { // for both endpoints
[c43766]362 OrderMap.insert(pair<int, class BoundaryPointSet *> (line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
[be2997]363 // and we don't care whether insertion fails
364 }
[834ff3]365 // set endpoints
366 int Counter = 0;
[1f2e46]367 DoLog(0) && (Log() << Verbose(0) << "New triangle " << Nr << " with end points: " << endl);
[be2997]368 for (PointMap::iterator runner = OrderMap.begin(); runner != OrderMap.end(); runner++) {
369 endpoints[Counter] = runner->second;
[1f2e46]370 DoLog(0) && (Log() << Verbose(0) << " " << *endpoints[Counter] << endl);
[be2997]371 Counter++;
372 }
373 if (Counter < 3) {
[c43766]374 DoeLog(0) && (eLog() << Verbose(0) << "We have a triangle with only two distinct endpoints!" << endl);
[be2997]375 performCriticalExit();
376 }
[c43766]377}
378;
[834ff3]379
[eb58a7]380/** Destructor of BoundaryTriangleSet.
381 * Removes itself from each of its lines' LineMap and removes them if necessary.
382 * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle()
383 */
[834ff3]384BoundaryTriangleSet::~BoundaryTriangleSet()
385{
[c43766]386 Info FunctionInfo(__func__);
[834ff3]387 for (int i = 0; i < 3; i++) {
[eb58a7]388 if (lines[i] != NULL) {
[60cbe5]389 if (lines[i]->triangles.erase(Nr)) {
[be2997]390 //Log() << Verbose(0) << "Triangle Nr." << Nr << " erased in line " << *lines[i] << "." << endl;
[60cbe5]391 }
[eb58a7]392 if (lines[i]->triangles.empty()) {
[c43766]393 //Log() << Verbose(0) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
394 delete (lines[i]);
395 lines[i] = NULL;
[eb58a7]396 }
397 }
[834ff3]398 }
[be2997]399 //Log() << Verbose(0) << "Erasing triangle Nr." << Nr << " itself." << endl;
[c43766]400}
401;
[834ff3]402
403/** Calculates the normal vector for this triangle.
404 * Is made unique by comparison with \a OtherVector to point in the other direction.
405 * \param &OtherVector direction vector to make normal vector unique.
406 */
[1aa2a5]407void BoundaryTriangleSet::GetNormalVector(const Vector &OtherVector)
[834ff3]408{
[c43766]409 Info FunctionInfo(__func__);
[834ff3]410 // get normal vector
[71910a]411 NormalVector = Plane(*(endpoints[0]->node->node),
412 *(endpoints[1]->node->node),
413 *(endpoints[2]->node->node)).getNormal();
[834ff3]414
415 // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
[1f591b]416 if (NormalVector.ScalarProduct(OtherVector) > 0.)
[834ff3]417 NormalVector.Scale(-1.);
[1f2e46]418 DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << NormalVector << "." << endl);
[c43766]419}
420;
[834ff3]421
[a1acc5]422/** Finds the point on the triangle \a *BTS through which the line defined by \a *MolCenter and \a *x crosses.
[834ff3]423 * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane
[1aa2a5]424 * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not.
[daf956]425 * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
426 * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between
427 * the first two basepoints) or not.
[834ff3]428 * \param *out output stream for debugging
429 * \param *MolCenter offset vector of line
430 * \param *x second endpoint of line, minus \a *MolCenter is directional vector of line
431 * \param *Intersection intersection on plane on return
432 * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle.
433 */
[0d111b]434
[1aa2a5]435bool BoundaryTriangleSet::GetIntersectionInsideTriangle(const Vector * const MolCenter, const Vector * const x, Vector * const Intersection) const
[834ff3]436{
[91b1e79]437 Info FunctionInfo(__func__);
[834ff3]438 Vector CrossPoint;
439 Vector helper;
440
[71910a]441 try {
442 *Intersection = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*MolCenter, *x);
443 }
444 catch (LinearDependenceException &excp) {
445 Log() << Verbose(1) << excp;
[c43766]446 DoeLog(1) && (eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl);
[834ff3]447 return false;
448 }
449
[1f2e46]450 DoLog(1) && (Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl);
451 DoLog(1) && (Log() << Verbose(1) << "INFO: Line is from " << *MolCenter << " to " << *x << "." << endl);
452 DoLog(1) && (Log() << Verbose(1) << "INFO: Intersection is " << *Intersection << "." << endl);
[a1acc5]453
[1f591b]454 if (Intersection->DistanceSquared(*endpoints[0]->node->node) < MYEPSILON) {
[1f2e46]455 DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl);
[91b1e79]456 return true;
[1f591b]457 } else if (Intersection->DistanceSquared(*endpoints[1]->node->node) < MYEPSILON) {
[1f2e46]458 DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl);
[91b1e79]459 return true;
[1f591b]460 } else if (Intersection->DistanceSquared(*endpoints[2]->node->node) < MYEPSILON) {
[1f2e46]461 DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl);
[91b1e79]462 return true;
463 }
[834ff3]464 // Calculate cross point between one baseline and the line from the third endpoint to intersection
[c43766]465 int i = 0;
[834ff3]466 do {
[71910a]467 try {
468 CrossPoint = GetIntersectionOfTwoLinesOnPlane(*(endpoints[i%3]->node->node),
469 *(endpoints[(i+1)%3]->node->node),
470 *(endpoints[(i+2)%3]->node->node),
471 *Intersection);
[1f591b]472 helper = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node);
473 CrossPoint -= (*endpoints[i%3]->node->node); // cross point was returned as absolute vector
474 const double s = CrossPoint.ScalarProduct(helper)/helper.NormSquared();
[1f2e46]475 DoLog(1) && (Log() << Verbose(1) << "INFO: Factor s is " << s << "." << endl);
[91b1e79]476 if ((s < -MYEPSILON) || ((s-1.) > MYEPSILON)) {
[1f2e46]477 DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << "outside of triangle." << endl);
[91b1e79]478 i=4;
479 break;
480 }
[0cf171]481 i++;
[71910a]482 } catch (LinearDependenceException &excp){
[d403a1]483 break;
[71910a]484 }
[c43766]485 } while (i < 3);
486 if (i == 3) {
[1f2e46]487 DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " inside of triangle." << endl);
[834ff3]488 return true;
[d403a1]489 } else {
[1f2e46]490 DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " outside of triangle." << endl);
[834ff3]491 return false;
492 }
[c43766]493}
494;
[834ff3]495
[f7b476]496/** Finds the point on the triangle to the point \a *x.
497 * We call Vector::GetIntersectionWithPlane() with \a * and the center of the triangle to receive an intersection point.
498 * Then we check the in-plane part (the part projected down onto plane). We check whether it crosses one of the
499 * boundary lines. If it does, we return this intersection as closest point, otherwise the projected point down.
[1aa2a5]500 * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not.
501 * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
502 * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between
503 * the first two basepoints) or not.
504 * \param *x point
505 * \param *ClosestPoint desired closest point inside triangle to \a *x, is absolute vector
506 * \return Distance squared between \a *x and closest point inside triangle
507 */
508double BoundaryTriangleSet::GetClosestPointInsideTriangle(const Vector * const x, Vector * const ClosestPoint) const
509{
510 Info FunctionInfo(__func__);
511 Vector Direction;
512
513 // 1. get intersection with plane
[1f2e46]514 DoLog(1) && (Log() << Verbose(1) << "INFO: Looking for closest point of triangle " << *this << " to " << *x << "." << endl);
[1aa2a5]515 GetCenter(&Direction);
[71910a]516 try {
517 *ClosestPoint = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*x, Direction);
518 }
519 catch (LinearDependenceException &excp) {
[1f591b]520 (*ClosestPoint) = (*x);
[1aa2a5]521 }
522
523 // 2. Calculate in plane part of line (x, intersection)
[1f591b]524 Vector InPlane = (*x) - (*ClosestPoint); // points from plane intersection to straight-down point
525 InPlane.ProjectOntoPlane(NormalVector);
526 InPlane += *ClosestPoint;
[1aa2a5]527
[1f2e46]528 DoLog(2) && (Log() << Verbose(2) << "INFO: Triangle is " << *this << "." << endl);
529 DoLog(2) && (Log() << Verbose(2) << "INFO: Line is from " << Direction << " to " << *x << "." << endl);
530 DoLog(2) && (Log() << Verbose(2) << "INFO: In-plane part is " << InPlane << "." << endl);
[1aa2a5]531
532 // Calculate cross point between one baseline and the desired point such that distance is shortest
533 double ShortestDistance = -1.;
534 bool InsideFlag = false;
535 Vector CrossDirection[3];
536 Vector CrossPoint[3];
537 Vector helper;
[c43766]538 for (int i = 0; i < 3; i++) {
[1aa2a5]539 // treat direction of line as normal of a (cut)plane and the desired point x as the plane offset, the intersect line with point
[1f591b]540 Direction = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node);
[1aa2a5]541 // calculate intersection, line can never be parallel to Direction (is the same vector as PlaneNormal);
[71910a]542 CrossPoint[i] = Plane(Direction, InPlane).GetIntersection(*(endpoints[i%3]->node->node), *(endpoints[(i+1)%3]->node->node));
[1f591b]543 CrossDirection[i] = CrossPoint[i] - InPlane;
544 CrossPoint[i] -= (*endpoints[i%3]->node->node); // cross point was returned as absolute vector
545 const double s = CrossPoint[i].ScalarProduct(Direction)/Direction.NormSquared();
[1f2e46]546 DoLog(2) && (Log() << Verbose(2) << "INFO: Factor s is " << s << "." << endl);
[1aa2a5]547 if ((s >= -MYEPSILON) && ((s-1.) <= MYEPSILON)) {
[0d111b]548 CrossPoint[i] += (*endpoints[i%3]->node->node); // make cross point absolute again
[1f2e46]549 DoLog(2) && (Log() << Verbose(2) << "INFO: Crosspoint is " << CrossPoint[i] << ", intersecting BoundaryLine between " << *endpoints[i % 3]->node->node << " and " << *endpoints[(i + 1) % 3]->node->node << "." << endl);
[1f591b]550 const double distance = CrossPoint[i].DistanceSquared(*x);
[1aa2a5]551 if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
552 ShortestDistance = distance;
[1f591b]553 (*ClosestPoint) = CrossPoint[i];
[1aa2a5]554 }
555 } else
556 CrossPoint[i].Zero();
557 }
558 InsideFlag = true;
[c43766]559 for (int i = 0; i < 3; i++) {
[0d111b]560 const double sign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 1) % 3]);
561 const double othersign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 2) % 3]);
562
[c43766]563 if ((sign > -MYEPSILON) && (othersign > -MYEPSILON)) // have different sign
[1aa2a5]564 InsideFlag = false;
565 }
566 if (InsideFlag) {
[1f591b]567 (*ClosestPoint) = InPlane;
568 ShortestDistance = InPlane.DistanceSquared(*x);
[c43766]569 } else { // also check endnodes
570 for (int i = 0; i < 3; i++) {
[1f591b]571 const double distance = x->DistanceSquared(*endpoints[i]->node->node);
[1aa2a5]572 if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
573 ShortestDistance = distance;
[1f591b]574 (*ClosestPoint) = (*endpoints[i]->node->node);
[1aa2a5]575 }
576 }
577 }
[1f2e46]578 DoLog(1) && (Log() << Verbose(1) << "INFO: Closest Point is " << *ClosestPoint << " with shortest squared distance is " << ShortestDistance << "." << endl);
[1aa2a5]579 return ShortestDistance;
[c43766]580}
581;
[1aa2a5]582
[834ff3]583/** Checks whether lines is any of the three boundary lines this triangle contains.
584 * \param *line line to test
585 * \return true - line is of the triangle, false - is not
586 */
[1aa2a5]587bool BoundaryTriangleSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const
[834ff3]588{
[c43766]589 Info FunctionInfo(__func__);
590 for (int i = 0; i < 3; i++)
[834ff3]591 if (line == lines[i])
592 return true;
593 return false;
[c43766]594}
595;
[834ff3]596
597/** Checks whether point is any of the three endpoints this triangle contains.
598 * \param *point point to test
599 * \return true - point is of the triangle, false - is not
600 */
[1aa2a5]601bool BoundaryTriangleSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
[834ff3]602{
[c43766]603 Info FunctionInfo(__func__);
604 for (int i = 0; i < 3; i++)
[834ff3]605 if (point == endpoints[i])
606 return true;
607 return false;
[c43766]608}
609;
[834ff3]610
[daf956]611/** Checks whether point is any of the three endpoints this triangle contains.
612 * \param *point TesselPoint to test
613 * \return true - point is of the triangle, false - is not
614 */
[1aa2a5]615bool BoundaryTriangleSet::ContainsBoundaryPoint(const TesselPoint * const point) const
[daf956]616{
[c43766]617 Info FunctionInfo(__func__);
618 for (int i = 0; i < 3; i++)
[daf956]619 if (point == endpoints[i]->node)
620 return true;
621 return false;
[c43766]622}
623;
[daf956]624
[834ff3]625/** Checks whether three given \a *Points coincide with triangle's endpoints.
626 * \param *Points[3] pointer to BoundaryPointSet
627 * \return true - is the very triangle, false - is not
628 */
[1aa2a5]629bool BoundaryTriangleSet::IsPresentTupel(const BoundaryPointSet * const Points[3]) const
[834ff3]630{
[c43766]631 Info FunctionInfo(__func__);
[1f2e46]632 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking " << Points[0] << "," << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << "." << endl);
[c43766]633 return (((endpoints[0] == Points[0]) || (endpoints[0] == Points[1]) || (endpoints[0] == Points[2])) && ((endpoints[1] == Points[0]) || (endpoints[1] == Points[1]) || (endpoints[1] == Points[2])) && ((endpoints[2] == Points[0]) || (endpoints[2] == Points[1]) || (endpoints[2] == Points[2])
634
635 ));
636}
637;
[834ff3]638
[60cbe5]639/** Checks whether three given \a *Points coincide with triangle's endpoints.
640 * \param *Points[3] pointer to BoundaryPointSet
641 * \return true - is the very triangle, false - is not
642 */
[1aa2a5]643bool BoundaryTriangleSet::IsPresentTupel(const BoundaryTriangleSet * const T) const
[60cbe5]644{
[c43766]645 Info FunctionInfo(__func__);
646 return (((endpoints[0] == T->endpoints[0]) || (endpoints[0] == T->endpoints[1]) || (endpoints[0] == T->endpoints[2])) && ((endpoints[1] == T->endpoints[0]) || (endpoints[1] == T->endpoints[1]) || (endpoints[1] == T->endpoints[2])) && ((endpoints[2] == T->endpoints[0]) || (endpoints[2] == T->endpoints[1]) || (endpoints[2] == T->endpoints[2])
647
648 ));
649}
650;
[60cbe5]651
[eeefb3]652/** Returns the endpoint which is not contained in the given \a *line.
653 * \param *line baseline defining two endpoints
654 * \return pointer third endpoint or NULL if line does not belong to triangle.
655 */
[1aa2a5]656class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(const BoundaryLineSet * const line) const
[eeefb3]657{
[c43766]658 Info FunctionInfo(__func__);
[eeefb3]659 // sanity check
660 if (!ContainsBoundaryLine(line))
661 return NULL;
[c43766]662 for (int i = 0; i < 3; i++)
[eeefb3]663 if (!line->ContainsBoundaryPoint(endpoints[i]))
664 return endpoints[i];
665 // actually, that' impossible :)
666 return NULL;
[c43766]667}
668;
[eeefb3]669
670/** Calculates the center point of the triangle.
671 * Is third of the sum of all endpoints.
672 * \param *center central point on return.
673 */
[1aa2a5]674void BoundaryTriangleSet::GetCenter(Vector * const center) const
[eeefb3]675{
[c43766]676 Info FunctionInfo(__func__);
[eeefb3]677 center->Zero();
[c43766]678 for (int i = 0; i < 3; i++)
[1f591b]679 (*center) += (*endpoints[i]->node->node);
[c43766]680 center->Scale(1. / 3.);
[1f2e46]681 DoLog(1) && (Log() << Verbose(1) << "INFO: Center is at " << *center << "." << endl);
[eeefb3]682}
683
[eb58a7]684/** output operator for BoundaryTriangleSet.
685 * \param &ost output stream
686 * \param &a boundary triangle
687 */
[a9b2a0a]688ostream &operator <<(ostream &ost, const BoundaryTriangleSet &a)
[834ff3]689{
[be2997]690 ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << "," << a.endpoints[1]->node->Name << "," << a.endpoints[2]->node->Name << "]";
[c43766]691 // ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
692 // << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
[834ff3]693 return ost;
[c43766]694}
695;
[834ff3]696
[f7490d]697// ======================================== Polygons on Boundary =================================
698
699/** Constructor for BoundaryPolygonSet.
700 */
701BoundaryPolygonSet::BoundaryPolygonSet() :
702 Nr(-1)
703{
704 Info FunctionInfo(__func__);
[c43766]705}
706;
[f7490d]707
708/** Destructor of BoundaryPolygonSet.
709 * Just clears endpoints.
710 * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle()
711 */
712BoundaryPolygonSet::~BoundaryPolygonSet()
713{
714 Info FunctionInfo(__func__);
715 endpoints.clear();
[1f2e46]716 DoLog(1) && (Log() << Verbose(1) << "Erasing polygon Nr." << Nr << " itself." << endl);
[c43766]717}
718;
[f7490d]719
720/** Calculates the normal vector for this triangle.
721 * Is made unique by comparison with \a OtherVector to point in the other direction.
722 * \param &OtherVector direction vector to make normal vector unique.
723 * \return allocated vector in normal direction
724 */
725Vector * BoundaryPolygonSet::GetNormalVector(const Vector &OtherVector) const
726{
727 Info FunctionInfo(__func__);
728 // get normal vector
729 Vector TemporaryNormal;
730 Vector *TotalNormal = new Vector;
731 PointSet::const_iterator Runner[3];
[c43766]732 for (int i = 0; i < 3; i++) {
[f7490d]733 Runner[i] = endpoints.begin();
[c43766]734 for (int j = 0; j < i; j++) { // go as much further
[f7490d]735 Runner[i]++;
736 if (Runner[i] == endpoints.end()) {
[c43766]737 DoeLog(0) && (eLog() << Verbose(0) << "There are less than three endpoints in the polygon!" << endl);
[f7490d]738 performCriticalExit();
739 }
740 }
741 }
742 TotalNormal->Zero();
[c43766]743 int counter = 0;
744 for (; Runner[2] != endpoints.end();) {
[71910a]745 TemporaryNormal = Plane(*((*Runner[0])->node->node),
746 *((*Runner[1])->node->node),
747 *((*Runner[2])->node->node)).getNormal();
[c43766]748 for (int i = 0; i < 3; i++) // increase each of them
[f7490d]749 Runner[i]++;
[1f591b]750 (*TotalNormal) += TemporaryNormal;
[f7490d]751 }
[c43766]752 TotalNormal->Scale(1. / (double) counter);
[f7490d]753
754 // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
[1f591b]755 if (TotalNormal->ScalarProduct(OtherVector) > 0.)
[f7490d]756 TotalNormal->Scale(-1.);
[1f2e46]757 DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << *TotalNormal << "." << endl);
[f7490d]758
759 return TotalNormal;
[c43766]760}
761;
[f7490d]762
763/** Calculates the center point of the triangle.
764 * Is third of the sum of all endpoints.
765 * \param *center central point on return.
766 */
767void BoundaryPolygonSet::GetCenter(Vector * const center) const
768{
769 Info FunctionInfo(__func__);
770 center->Zero();
771 int counter = 0;
772 for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
[1f591b]773 (*center) += (*(*Runner)->node->node);
[f7490d]774 counter++;
775 }
[c43766]776 center->Scale(1. / (double) counter);
[1f2e46]777 DoLog(1) && (Log() << Verbose(1) << "Center is at " << *center << "." << endl);
[f7490d]778}
779
780/** Checks whether the polygons contains all three endpoints of the triangle.
781 * \param *triangle triangle to test
782 * \return true - triangle is contained polygon, false - is not
783 */
784bool BoundaryPolygonSet::ContainsBoundaryTriangle(const BoundaryTriangleSet * const triangle) const
785{
786 Info FunctionInfo(__func__);
787 return ContainsPresentTupel(triangle->endpoints, 3);
[c43766]788}
789;
[f7490d]790
791/** Checks whether the polygons contains both endpoints of the line.
792 * \param *line line to test
793 * \return true - line is of the triangle, false - is not
794 */
795bool BoundaryPolygonSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const
796{
[ea3627]797 Info FunctionInfo(__func__);
[f7490d]798 return ContainsPresentTupel(line->endpoints, 2);
[c43766]799}
800;
[f7490d]801
802/** Checks whether point is any of the three endpoints this triangle contains.
803 * \param *point point to test
804 * \return true - point is of the triangle, false - is not
805 */
806bool BoundaryPolygonSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
807{
808 Info FunctionInfo(__func__);
[c43766]809 for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
[1f2e46]810 DoLog(0) && (Log() << Verbose(0) << "Checking against " << **Runner << endl);
[ea3627]811 if (point == (*Runner)) {
[1f2e46]812 DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
[f7490d]813 return true;
[ea3627]814 }
815 }
[1f2e46]816 DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
[f7490d]817 return false;
[c43766]818}
819;
[f7490d]820
821/** Checks whether point is any of the three endpoints this triangle contains.
822 * \param *point TesselPoint to test
823 * \return true - point is of the triangle, false - is not
824 */
825bool BoundaryPolygonSet::ContainsBoundaryPoint(const TesselPoint * const point) const
826{
827 Info FunctionInfo(__func__);
[c43766]828 for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
[ea3627]829 if (point == (*Runner)->node) {
[1f2e46]830 DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
[f7490d]831 return true;
[ea3627]832 }
[1f2e46]833 DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
[f7490d]834 return false;
[c43766]835}
836;
[f7490d]837
838/** Checks whether given array of \a *Points coincide with polygons's endpoints.
839 * \param **Points pointer to an array of BoundaryPointSet
840 * \param dim dimension of array
841 * \return true - set of points is contained in polygon, false - is not
842 */
843bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPointSet * const * Points, const int dim) const
844{
[ea3627]845 Info FunctionInfo(__func__);
[f7490d]846 int counter = 0;
[1f2e46]847 DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
[c43766]848 for (int i = 0; i < dim; i++) {
[1f2e46]849 DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << *Points[i] << endl);
[ea3627]850 if (ContainsBoundaryPoint(Points[i])) {
[f7490d]851 counter++;
[ea3627]852 }
853 }
[f7490d]854
855 if (counter == dim)
856 return true;
857 else
858 return false;
[c43766]859}
860;
[f7490d]861
862/** Checks whether given PointList coincide with polygons's endpoints.
863 * \param &endpoints PointList
864 * \return true - set of points is contained in polygon, false - is not
865 */
866bool BoundaryPolygonSet::ContainsPresentTupel(const PointSet &endpoints) const
867{
[ea3627]868 Info FunctionInfo(__func__);
[f7490d]869 size_t counter = 0;
[1f2e46]870 DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
[c43766]871 for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
[1f2e46]872 DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << **Runner << endl);
[f7490d]873 if (ContainsBoundaryPoint(*Runner))
874 counter++;
875 }
876
877 if (counter == endpoints.size())
878 return true;
879 else
880 return false;
[c43766]881}
882;
[f7490d]883
884/** Checks whether given set of \a *Points coincide with polygons's endpoints.
885 * \param *P pointer to BoundaryPolygonSet
886 * \return true - is the very triangle, false - is not
887 */
888bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPolygonSet * const P) const
889{
[c43766]890 return ContainsPresentTupel((const PointSet) P->endpoints);
891}
892;
[f7490d]893
894/** Gathers all the endpoints' triangles in a unique set.
895 * \return set of all triangles
896 */
[ea3627]897TriangleSet * BoundaryPolygonSet::GetAllContainedTrianglesFromEndpoints() const
[f7490d]898{
899 Info FunctionInfo(__func__);
[c43766]900 pair<TriangleSet::iterator, bool> Tester;
[f7490d]901 TriangleSet *triangles = new TriangleSet;
902
[c43766]903 for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
904 for (LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++)
905 for (TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) {
[ea3627]906 //Log() << Verbose(0) << " Testing triangle " << *(Sprinter->second) << endl;
907 if (ContainsBoundaryTriangle(Sprinter->second)) {
908 Tester = triangles->insert(Sprinter->second);
909 if (Tester.second)
[1f2e46]910 DoLog(0) && (Log() << Verbose(0) << "Adding triangle " << *(Sprinter->second) << endl);
[ea3627]911 }
912 }
[f7490d]913
[1f2e46]914 DoLog(1) && (Log() << Verbose(1) << "The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total." << endl);
[f7490d]915 return triangles;
[c43766]916}
917;
[f7490d]918
919/** Fills the endpoints of this polygon from the triangles attached to \a *line.
920 * \param *line lines with triangles attached
[ea3627]921 * \return true - polygon contains endpoints, false - line was NULL
[f7490d]922 */
923bool BoundaryPolygonSet::FillPolygonFromTrianglesOfLine(const BoundaryLineSet * const line)
924{
[ea3627]925 Info FunctionInfo(__func__);
[c43766]926 pair<PointSet::iterator, bool> Tester;
[ea3627]927 if (line == NULL)
928 return false;
[1f2e46]929 DoLog(1) && (Log() << Verbose(1) << "Filling polygon from line " << *line << endl);
[c43766]930 for (TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) {
931 for (int i = 0; i < 3; i++) {
[ea3627]932 Tester = endpoints.insert((Runner->second)->endpoints[i]);
933 if (Tester.second)
[1f2e46]934 DoLog(1) && (Log() << Verbose(1) << " Inserting endpoint " << *((Runner->second)->endpoints[i]) << endl);
[ea3627]935 }
[f7490d]936 }
937
[ea3627]938 return true;
[c43766]939}
940;
[f7490d]941
942/** output operator for BoundaryPolygonSet.
943 * \param &ost output stream
944 * \param &a boundary polygon
945 */
946ostream &operator <<(ostream &ost, const BoundaryPolygonSet &a)
947{
948 ost << "[" << a.Nr << "|";
[c43766]949 for (PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) {
950 ost << (*Runner)->node->Name;
951 Runner++;
952 if (Runner != a.endpoints.end())
953 ost << ",";
[f7490d]954 }
[c43766]955 ost << "]";
[f7490d]956 return ost;
[c43766]957}
958;
[f7490d]959
[834ff3]960// =========================================================== class TESSELPOINT ===========================================
961
962/** Constructor of class TesselPoint.
963 */
964TesselPoint::TesselPoint()
965{
[478683]966 //Info FunctionInfo(__func__);
[834ff3]967 node = NULL;
968 nr = -1;
[c43766]969 Name = NULL;
970}
971;
[834ff3]972
973/** Destructor for class TesselPoint.
974 */
975TesselPoint::~TesselPoint()
976{
[478683]977 //Info FunctionInfo(__func__);
[c43766]978}
979;
[834ff3]980
981/** Prints LCNode to screen.
982 */
[c43766]983ostream & operator <<(ostream &ost, const TesselPoint &a)
[834ff3]984{
[60cbe5]985 ost << "[" << (a.Name) << "|" << a.Name << " at " << *a.node << "]";
[834ff3]986 return ost;
[c43766]987}
988;
[834ff3]989
[0cf171]990/** Prints LCNode to screen.
991 */
[c43766]992ostream & TesselPoint::operator <<(ostream &ost)
[0cf171]993{
[c43766]994 Info FunctionInfo(__func__);
[013ad57]995 ost << "[" << (nr) << "|" << this << "]";
[0cf171]996 return ost;
[c43766]997}
998;
[834ff3]999
1000// =========================================================== class POINTCLOUD ============================================
1001
1002/** Constructor of class PointCloud.
1003 */
1004PointCloud::PointCloud()
1005{
[c43766]1006 //Info FunctionInfo(__func__);
1007}
1008;
[834ff3]1009
1010/** Destructor for class PointCloud.
1011 */
1012PointCloud::~PointCloud()
1013{
[c43766]1014 //Info FunctionInfo(__func__);
1015}
1016;
[834ff3]1017
1018// ============================ CandidateForTesselation =============================
1019
1020/** Constructor of class CandidateForTesselation.
1021 */
[c43766]1022CandidateForTesselation::CandidateForTesselation(BoundaryLineSet* line) :
1023 BaseLine(line), ThirdPoint(NULL), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI)
[4af89b]1024{
[c43766]1025 Info FunctionInfo(__func__);
1026}
1027;
[4af89b]1028
1029/** Constructor of class CandidateForTesselation.
1030 */
[c43766]1031CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, BoundaryPointSet* point, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) :
1032 BaseLine(line), ThirdPoint(point), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI)
[4af89b]1033{
[be2997]1034 Info FunctionInfo(__func__);
[1f591b]1035 OptCenter = OptCandidateCenter;
1036 OtherOptCenter = OtherOptCandidateCenter;
[834ff3]1037};
1038
1039
1040/** Destructor for class CandidateForTesselation.
1041 */
[c43766]1042CandidateForTesselation::~CandidateForTesselation()
1043{
1044}
1045;
[834ff3]1046
[6dd8d3]1047/** Checks validity of a given sphere of a candidate line.
1048 * Sphere must touch all candidates and the baseline endpoints and there must be no other atoms inside.
1049 * \param RADIUS radius of sphere
1050 * \param *LC LinkedCell structure with other atoms
1051 * \return true - sphere is valid, false - sphere contains other points
1052 */
1053bool CandidateForTesselation::CheckValidity(const double RADIUS, const LinkedCell *LC) const
1054{
[a732ff]1055 Info FunctionInfo(__func__);
1056
[c43766]1057 const double radiusSquared = RADIUS * RADIUS;
[6dd8d3]1058 list<const Vector *> VectorList;
1059 VectorList.push_back(&OptCenter);
[a732ff]1060 //VectorList.push_back(&OtherOptCenter); // don't check the other (wrong) center
[6dd8d3]1061
[a732ff]1062 if (!pointlist.empty())
[c43766]1063 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains candidate list and baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl);
[a732ff]1064 else
1065 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere with no candidates contains baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl);
[6dd8d3]1066 // check baseline for OptCenter and OtherOptCenter being on sphere's surface
1067 for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
[c43766]1068 for (int i = 0; i < 2; i++) {
[0d111b]1069 const double distance = fabs((*VRunner)->DistanceSquared(*BaseLine->endpoints[i]->node->node) - radiusSquared);
[086db0]1070 if (distance > HULLEPSILON) {
[c43766]1071 DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << *BaseLine->endpoints[i] << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl);
[6dd8d3]1072 return false;
1073 }
[086db0]1074 }
[6dd8d3]1075 }
1076
1077 // check Candidates for OptCenter and OtherOptCenter being on sphere's surface
[c43766]1078 for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) {
[6dd8d3]1079 const TesselPoint *Walker = *Runner;
1080 for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
[0d111b]1081 const double distance = fabs((*VRunner)->DistanceSquared(*Walker->node) - radiusSquared);
[086db0]1082 if (distance > HULLEPSILON) {
[c43766]1083 DoeLog(1) && (eLog() << Verbose(1) << "Candidate " << *Walker << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl);
[6dd8d3]1084 return false;
[c43766]1085 } else {
[1f2e46]1086 DoLog(1) && (Log() << Verbose(1) << "Candidate " << *Walker << " is inside by " << distance << "." << endl);
[6dd8d3]1087 }
1088 }
1089 }
1090
[a732ff]1091 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl);
[6dd8d3]1092 bool flag = true;
1093 for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
1094 // get all points inside the sphere
1095 TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, (*VRunner));
[c43766]1096
[1f2e46]1097 DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << OtherOptCenter << ":" << endl);
[c43766]1098 for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
[4eee8f]1099 DoLog(1) && (Log() << Verbose(1) << " " << *(*Runner) << " with distance " << (*Runner)->node->distance(OtherOptCenter) << "." << endl);
[c43766]1100
[6dd8d3]1101 // remove baseline's endpoints and candidates
[c43766]1102 for (int i = 0; i < 2; i++) {
[1f2e46]1103 DoLog(1) && (Log() << Verbose(1) << "INFO: removing baseline tesselpoint " << *BaseLine->endpoints[i]->node << "." << endl);
[6dd8d3]1104 ListofPoints->remove(BaseLine->endpoints[i]->node);
[c43766]1105 }
1106 for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) {
[1f2e46]1107 DoLog(1) && (Log() << Verbose(1) << "INFO: removing candidate tesselpoint " << *(*Runner) << "." << endl);
[6dd8d3]1108 ListofPoints->remove(*Runner);
[c43766]1109 }
[6dd8d3]1110 if (!ListofPoints->empty()) {
[c43766]1111 DoeLog(1) && (eLog() << Verbose(1) << "CheckValidity: There are still " << ListofPoints->size() << " points inside the sphere." << endl);
[6dd8d3]1112 flag = false;
[a732ff]1113 DoeLog(1) && (eLog() << Verbose(1) << "External atoms inside of sphere at " << *(*VRunner) << ":" << endl);
1114 for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
1115 DoeLog(1) && (eLog() << Verbose(1) << " " << *(*Runner) << endl);
[6dd8d3]1116 }
[c43766]1117 delete (ListofPoints);
[a732ff]1118
1119 // check with animate_sphere.tcl VMD script
1120 if (ThirdPoint != NULL) {
[0d111b]1121 DoLog(1) && (Log() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " " << ThirdPoint->Nr + 1 << " " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl);
[a732ff]1122 } else {
[1f2e46]1123 DoLog(1) && (Log() << Verbose(1) << "Check by: ... missing third point ..." << endl);
[0d111b]1124 DoLog(1) && (Log() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " ??? " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl);
[a732ff]1125 }
[6dd8d3]1126 }
1127 return flag;
[c43766]1128}
1129;
[834ff3]1130
[4af89b]1131/** output operator for CandidateForTesselation.
1132 * \param &ost output stream
1133 * \param &a boundary line
1134 */
[c43766]1135ostream & operator <<(ostream &ost, const CandidateForTesselation &a)
[4af89b]1136{
1137 ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->Name << "," << a.BaseLine->endpoints[1]->node->Name << "] with ";
[be2997]1138 if (a.pointlist.empty())
[4af89b]1139 ost << "no candidate.";
[be2997]1140 else {
1141 ost << "candidate";
1142 if (a.pointlist.size() != 1)
1143 ost << "s ";
1144 else
1145 ost << " ";
1146 for (TesselPointList::const_iterator Runner = a.pointlist.begin(); Runner != a.pointlist.end(); Runner++)
1147 ost << *(*Runner) << " ";
[c43766]1148 ost << " at angle " << (a.ShortestAngle) << ".";
[be2997]1149 }
[4af89b]1150
1151 return ost;
[c43766]1152}
1153;
[4af89b]1154
[834ff3]1155// =========================================================== class TESSELATION ===========================================
1156
1157/** Constructor of class Tesselation.
1158 */
[4af89b]1159Tesselation::Tesselation() :
[c43766]1160 PointsOnBoundaryCount(0), LinesOnBoundaryCount(0), TrianglesOnBoundaryCount(0), LastTriangle(NULL), TriangleFilesWritten(0), InternalPointer(PointsOnBoundary.begin())
[834ff3]1161{
[c43766]1162 Info FunctionInfo(__func__);
[834ff3]1163}
1164;
1165
1166/** Destructor of class Tesselation.
1167 * We have to free all points, lines and triangles.
1168 */
1169Tesselation::~Tesselation()
1170{
[c43766]1171 Info FunctionInfo(__func__);
[1f2e46]1172 DoLog(0) && (Log() << Verbose(0) << "Free'ing TesselStruct ... " << endl);
[834ff3]1173 for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner != TrianglesOnBoundary.end(); runner++) {
1174 if (runner->second != NULL) {
1175 delete (runner->second);
1176 runner->second = NULL;
1177 } else
[c43766]1178 DoeLog(1) && (eLog() << Verbose(1) << "The triangle " << runner->first << " has already been free'd." << endl);
[834ff3]1179 }
[1f2e46]1180 DoLog(0) && (Log() << Verbose(0) << "This envelope was written to file " << TriangleFilesWritten << " times(s)." << endl);
[834ff3]1181}
1182;
1183
[0cf171]1184/** PointCloud implementation of GetCenter
1185 * Uses PointsOnBoundary and STL stuff.
[c43766]1186 */
[a9b2a0a]1187Vector * Tesselation::GetCenter(ofstream *out) const
[0cf171]1188{
[c43766]1189 Info FunctionInfo(__func__);
1190 Vector *Center = new Vector(0., 0., 0.);
1191 int num = 0;
[0cf171]1192 for (GoToFirst(); (!IsEnd()); GoToNext()) {
[1f591b]1193 (*Center) += (*GetPoint()->node);
[0cf171]1194 num++;
1195 }
[c43766]1196 Center->Scale(1. / num);
[0cf171]1197 return Center;
[c43766]1198}
1199;
[0cf171]1200
1201/** PointCloud implementation of GoPoint
1202 * Uses PointsOnBoundary and STL stuff.
[c43766]1203 */
[a9b2a0a]1204TesselPoint * Tesselation::GetPoint() const
[0cf171]1205{
[c43766]1206 Info FunctionInfo(__func__);
[0cf171]1207 return (InternalPointer->second->node);
[c43766]1208}
1209;
[0cf171]1210
1211/** PointCloud implementation of GetTerminalPoint.
1212 * Uses PointsOnBoundary and STL stuff.
[c43766]1213 */
[a9b2a0a]1214TesselPoint * Tesselation::GetTerminalPoint() const
[0cf171]1215{
[c43766]1216 Info FunctionInfo(__func__);
[a9b2a0a]1217 PointMap::const_iterator Runner = PointsOnBoundary.end();
[0cf171]1218 Runner--;
1219 return (Runner->second->node);
[c43766]1220}
1221;
[0cf171]1222
1223/** PointCloud implementation of GoToNext.
1224 * Uses PointsOnBoundary and STL stuff.
[c43766]1225 */
[a9b2a0a]1226void Tesselation::GoToNext() const
[0cf171]1227{
[c43766]1228 Info FunctionInfo(__func__);
[0cf171]1229 if (InternalPointer != PointsOnBoundary.end())
1230 InternalPointer++;
[c43766]1231}
1232;
[0cf171]1233
1234/** PointCloud implementation of GoToPrevious.
1235 * Uses PointsOnBoundary and STL stuff.
[c43766]1236 */
[a9b2a0a]1237void Tesselation::GoToPrevious() const
[0cf171]1238{
[c43766]1239 Info FunctionInfo(__func__);
[0cf171]1240 if (InternalPointer != PointsOnBoundary.begin())
1241 InternalPointer--;
[c43766]1242}
1243;
[0cf171]1244
1245/** PointCloud implementation of GoToFirst.
1246 * Uses PointsOnBoundary and STL stuff.
[c43766]1247 */
[a9b2a0a]1248void Tesselation::GoToFirst() const
[0cf171]1249{
[c43766]1250 Info FunctionInfo(__func__);
[0cf171]1251 InternalPointer = PointsOnBoundary.begin();
[c43766]1252}
1253;
[0cf171]1254
1255/** PointCloud implementation of GoToLast.
1256 * Uses PointsOnBoundary and STL stuff.
[a9b2a0a]1257 */
1258void Tesselation::GoToLast() const
[0cf171]1259{
[c43766]1260 Info FunctionInfo(__func__);
[0cf171]1261 InternalPointer = PointsOnBoundary.end();
1262 InternalPointer--;
[c43766]1263}
1264;
[0cf171]1265
1266/** PointCloud implementation of IsEmpty.
1267 * Uses PointsOnBoundary and STL stuff.
[c43766]1268 */
[a9b2a0a]1269bool Tesselation::IsEmpty() const
[0cf171]1270{
[c43766]1271 Info FunctionInfo(__func__);
[0cf171]1272 return (PointsOnBoundary.empty());
[c43766]1273}
1274;
[0cf171]1275
1276/** PointCloud implementation of IsLast.
1277 * Uses PointsOnBoundary and STL stuff.
[c43766]1278 */
[a9b2a0a]1279bool Tesselation::IsEnd() const
[0cf171]1280{
[c43766]1281 Info FunctionInfo(__func__);
[0cf171]1282 return (InternalPointer == PointsOnBoundary.end());
[c43766]1283}
1284;
[0cf171]1285
[834ff3]1286/** Gueses first starting triangle of the convex envelope.
1287 * We guess the starting triangle by taking the smallest distance between two points and looking for a fitting third.
1288 * \param *out output stream for debugging
1289 * \param PointsOnBoundary set of boundary points defining the convex envelope of the cluster
1290 */
[478683]1291void Tesselation::GuessStartingTriangle()
[834ff3]1292{
[c43766]1293 Info FunctionInfo(__func__);
[834ff3]1294 // 4b. create a starting triangle
1295 // 4b1. create all distances
1296 DistanceMultiMap DistanceMMap;
1297 double distance, tmp;
1298 Vector PlaneVector, TrialVector;
1299 PointMap::iterator A, B, C; // three nodes of the first triangle
1300 A = PointsOnBoundary.begin(); // the first may be chosen arbitrarily
1301
1302 // with A chosen, take each pair B,C and sort
[c43766]1303 if (A != PointsOnBoundary.end()) {
1304 B = A;
1305 B++;
1306 for (; B != PointsOnBoundary.end(); B++) {
1307 C = B;
1308 C++;
1309 for (; C != PointsOnBoundary.end(); C++) {
[0d111b]1310 tmp = A->second->node->node->DistanceSquared(*B->second->node->node);
[c43766]1311 distance = tmp * tmp;
[0d111b]1312 tmp = A->second->node->node->DistanceSquared(*C->second->node->node);
[c43766]1313 distance += tmp * tmp;
[0d111b]1314 tmp = B->second->node->node->DistanceSquared(*C->second->node->node);
[c43766]1315 distance += tmp * tmp;
1316 DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C)));
1317 }
[834ff3]1318 }
[c43766]1319 }
[834ff3]1320 // // listing distances
[543ce4]1321 // Log() << Verbose(1) << "Listing DistanceMMap:";
[834ff3]1322 // for(DistanceMultiMap::iterator runner = DistanceMMap.begin(); runner != DistanceMMap.end(); runner++) {
[543ce4]1323 // Log() << Verbose(0) << " " << runner->first << "(" << *runner->second.first->second << ", " << *runner->second.second->second << ")";
[834ff3]1324 // }
[543ce4]1325 // Log() << Verbose(0) << endl;
[834ff3]1326 // 4b2. pick three baselines forming a triangle
1327 // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
1328 DistanceMultiMap::iterator baseline = DistanceMMap.begin();
[c43766]1329 for (; baseline != DistanceMMap.end(); baseline++) {
1330 // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
1331 // 2. next, we have to check whether all points reside on only one side of the triangle
1332 // 3. construct plane vector
[0d111b]1333 PlaneVector = Plane(*A->second->node->node,
1334 *baseline->second.first->second->node->node,
1335 *baseline->second.second->second->node->node).getNormal();
[1f2e46]1336 DoLog(2) && (Log() << Verbose(2) << "Plane vector of candidate triangle is " << PlaneVector << endl);
[c43766]1337 // 4. loop over all points
1338 double sign = 0.;
1339 PointMap::iterator checker = PointsOnBoundary.begin();
1340 for (; checker != PointsOnBoundary.end(); checker++) {
1341 // (neglecting A,B,C)
1342 if ((checker == A) || (checker == baseline->second.first) || (checker == baseline->second.second))
1343 continue;
1344 // 4a. project onto plane vector
[0d111b]1345 TrialVector = (*checker->second->node->node);
1346 TrialVector.SubtractVector(*A->second->node->node);
1347 distance = TrialVector.ScalarProduct(PlaneVector);
[c43766]1348 if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
1349 continue;
[1f2e46]1350 DoLog(2) && (Log() << Verbose(2) << "Projection of " << checker->second->node->Name << " yields distance of " << distance << "." << endl);
[c43766]1351 tmp = distance / fabs(distance);
1352 // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
1353 if ((sign != 0) && (tmp != sign)) {
1354 // 4c. If so, break 4. loop and continue with next candidate in 1. loop
[1f2e46]1355 DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->Name << "," << baseline->second.first->second->node->Name << "," << baseline->second.second->second->node->Name << " leaves " << checker->second->node->Name << " outside the convex hull." << endl);
[c43766]1356 break;
1357 } else { // note the sign for later
[1f2e46]1358 DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->Name << "," << baseline->second.first->second->node->Name << "," << baseline->second.second->second->node->Name << " leave " << checker->second->node->Name << " inside the convex hull." << endl);
[c43766]1359 sign = tmp;
1360 }
1361 // 4d. Check whether the point is inside the triangle (check distance to each node
[0d111b]1362 tmp = checker->second->node->node->DistanceSquared(*A->second->node->node);
[c43766]1363 int innerpoint = 0;
[0d111b]1364 if ((tmp < A->second->node->node->DistanceSquared(*baseline->second.first->second->node->node)) && (tmp < A->second->node->node->DistanceSquared(*baseline->second.second->second->node->node)))
[c43766]1365 innerpoint++;
[0d111b]1366 tmp = checker->second->node->node->DistanceSquared(*baseline->second.first->second->node->node);
1367 if ((tmp < baseline->second.first->second->node->node->DistanceSquared(*A->second->node->node)) && (tmp < baseline->second.first->second->node->node->DistanceSquared(*baseline->second.second->second->node->node)))
[c43766]1368 innerpoint++;
[0d111b]1369 tmp = checker->second->node->node->DistanceSquared(*baseline->second.second->second->node->node);
1370 if ((tmp < baseline->second.second->second->node->node->DistanceSquared(*baseline->second.first->second->node->node)) && (tmp < baseline->second.second->second->node->node->DistanceSquared(*A->second->node->node)))
[c43766]1371 innerpoint++;
1372 // 4e. If so, break 4. loop and continue with next candidate in 1. loop
1373 if (innerpoint == 3)
1374 break;
[834ff3]1375 }
[c43766]1376 // 5. come this far, all on same side? Then break 1. loop and construct triangle
1377 if (checker == PointsOnBoundary.end()) {
[1f2e46]1378 DoLog(2) && (Log() << Verbose(2) << "Looks like we have a candidate!" << endl);
[c43766]1379 break;
[834ff3]1380 }
[c43766]1381 }
1382 if (baseline != DistanceMMap.end()) {
1383 BPS[0] = baseline->second.first->second;
1384 BPS[1] = baseline->second.second->second;
1385 BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1386 BPS[0] = A->second;
1387 BPS[1] = baseline->second.second->second;
1388 BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1389 BPS[0] = baseline->second.first->second;
1390 BPS[1] = A->second;
1391 BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1392
1393 // 4b3. insert created triangle
1394 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
1395 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1396 TrianglesOnBoundaryCount++;
1397 for (int i = 0; i < NDIM; i++) {
1398 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
1399 LinesOnBoundaryCount++;
[834ff3]1400 }
[c43766]1401
[1f2e46]1402 DoLog(1) && (Log() << Verbose(1) << "Starting triangle is " << *BTS << "." << endl);
[c43766]1403 } else {
1404 DoeLog(0) && (eLog() << Verbose(0) << "No starting triangle found." << endl);
1405 }
[834ff3]1406}
1407;
1408
1409/** Tesselates the convex envelope of a cluster from a single starting triangle.
1410 * The starting triangle is made out of three baselines. Each line in the final tesselated cluster may belong to at most
1411 * 2 triangles. Hence, we go through all current lines:
1412 * -# if the lines contains to only one triangle
1413 * -# We search all points in the boundary
1414 * -# if the triangle is in forward direction of the baseline (at most 90 degrees angle between vector orthogonal to
1415 * baseline in triangle plane pointing out of the triangle and normal vector of new triangle)
1416 * -# if the triangle with the baseline and the current point has the smallest of angles (comparison between normal vectors)
1417 * -# then we have a new triangle, whose baselines we again add (or increase their TriangleCount)
1418 * \param *out output stream for debugging
1419 * \param *configuration for IsAngstroem
1420 * \param *cloud cluster of points
1421 */
[543ce4]1422void Tesselation::TesselateOnBoundary(const PointCloud * const cloud)
[834ff3]1423{
[c43766]1424 Info FunctionInfo(__func__);
[834ff3]1425 bool flag;
1426 PointMap::iterator winner;
1427 class BoundaryPointSet *peak = NULL;
1428 double SmallestAngle, TempAngle;
1429 Vector NormalVector, VirtualNormalVector, CenterVector, TempVector, helper, PropagationVector, *Center = NULL;
1430 LineMap::iterator LineChecker[2];
1431
[543ce4]1432 Center = cloud->GetCenter();
[834ff3]1433 // create a first tesselation with the given BoundaryPoints
1434 do {
1435 flag = false;
1436 for (LineMap::iterator baseline = LinesOnBoundary.begin(); baseline != LinesOnBoundary.end(); baseline++)
[0cf171]1437 if (baseline->second->triangles.size() == 1) {
[834ff3]1438 // 5a. go through each boundary point if not _both_ edges between either endpoint of the current line and this point exist (and belong to 2 triangles)
1439 SmallestAngle = M_PI;
1440
1441 // get peak point with respect to this base line's only triangle
1442 BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far
[1f2e46]1443 DoLog(0) && (Log() << Verbose(0) << "Current baseline is between " << *(baseline->second) << "." << endl);
[834ff3]1444 for (int i = 0; i < 3; i++)
1445 if ((BTS->endpoints[i] != baseline->second->endpoints[0]) && (BTS->endpoints[i] != baseline->second->endpoints[1]))
1446 peak = BTS->endpoints[i];
[1f2e46]1447 DoLog(1) && (Log() << Verbose(1) << " and has peak " << *peak << "." << endl);
[834ff3]1448
1449 // prepare some auxiliary vectors
1450 Vector BaseLineCenter, BaseLine;
[1f591b]1451 BaseLineCenter = 0.5 * ((*baseline->second->endpoints[0]->node->node) +
1452 (*baseline->second->endpoints[1]->node->node));
1453 BaseLine = (*baseline->second->endpoints[0]->node->node) - (*baseline->second->endpoints[1]->node->node);
[834ff3]1454
1455 // offset to center of triangle
1456 CenterVector.Zero();
1457 for (int i = 0; i < 3; i++)
[1f591b]1458 CenterVector += (*BTS->endpoints[i]->node->node);
[834ff3]1459 CenterVector.Scale(1. / 3.);
[1f2e46]1460 DoLog(2) && (Log() << Verbose(2) << "CenterVector of base triangle is " << CenterVector << endl);
[834ff3]1461
1462 // normal vector of triangle
[1f591b]1463 NormalVector = (*Center) - CenterVector;
[834ff3]1464 BTS->GetNormalVector(NormalVector);
[1f591b]1465 NormalVector = BTS->NormalVector;
[1f2e46]1466 DoLog(2) && (Log() << Verbose(2) << "NormalVector of base triangle is " << NormalVector << endl);
[834ff3]1467
1468 // vector in propagation direction (out of triangle)
1469 // project center vector onto triangle plane (points from intersection plane-NormalVector to plane-CenterVector intersection)
[71910a]1470 PropagationVector = Plane(BaseLine, NormalVector,0).getNormal();
[1f591b]1471 TempVector = CenterVector - (*baseline->second->endpoints[0]->node->node); // TempVector is vector on triangle plane pointing from one baseline egde towards center!
[be2997]1472 //Log() << Verbose(0) << "Projection of propagation onto temp: " << PropagationVector.Projection(&TempVector) << "." << endl;
[1f591b]1473 if (PropagationVector.ScalarProduct(TempVector) > 0) // make sure normal propagation vector points outward from baseline
[834ff3]1474 PropagationVector.Scale(-1.);
[1f2e46]1475 DoLog(2) && (Log() << Verbose(2) << "PropagationVector of base triangle is " << PropagationVector << endl);
[834ff3]1476 winner = PointsOnBoundary.end();
1477
1478 // loop over all points and calculate angle between normal vector of new and present triangle
1479 for (PointMap::iterator target = PointsOnBoundary.begin(); target != PointsOnBoundary.end(); target++) {
1480 if ((target->second != baseline->second->endpoints[0]) && (target->second != baseline->second->endpoints[1])) { // don't take the same endpoints
[1f2e46]1481 DoLog(1) && (Log() << Verbose(1) << "Target point is " << *(target->second) << ":" << endl);
[834ff3]1482
1483 // first check direction, so that triangles don't intersect
[1f591b]1484 VirtualNormalVector = (*target->second->node->node) - BaseLineCenter;
[0d111b]1485 VirtualNormalVector.ProjectOntoPlane(NormalVector);
[1f591b]1486 TempAngle = VirtualNormalVector.Angle(PropagationVector);
[1f2e46]1487 DoLog(2) && (Log() << Verbose(2) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl);
[c43766]1488 if (TempAngle > (M_PI / 2.)) { // no bends bigger than Pi/2 (90 degrees)
[1f2e46]1489 DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl);
[834ff3]1490 continue;
1491 } else
[1f2e46]1492 DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl);
[834ff3]1493
1494 // check first and second endpoint (if any connecting line goes to target has at least not more than 1 triangle)
1495 LineChecker[0] = baseline->second->endpoints[0]->lines.find(target->first);
1496 LineChecker[1] = baseline->second->endpoints[1]->lines.find(target->first);
[0cf171]1497 if (((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[0]->second->triangles.size() == 2))) {
[1f2e46]1498 DoLog(2) && (Log() << Verbose(2) << *(baseline->second->endpoints[0]) << " has line " << *(LineChecker[0]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[0]->second->triangles.size() << " triangles." << endl);
[834ff3]1499 continue;
1500 }
[0cf171]1501 if (((LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (LineChecker[1]->second->triangles.size() == 2))) {
[1f2e46]1502 DoLog(2) && (Log() << Verbose(2) << *(baseline->second->endpoints[1]) << " has line " << *(LineChecker[1]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[1]->second->triangles.size() << " triangles." << endl);
[834ff3]1503 continue;
1504 }
1505
1506 // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint)
1507 if ((((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (GetCommonEndpoint(LineChecker[0]->second, LineChecker[1]->second) == peak)))) {
[1f2e46]1508 DoLog(4) && (Log() << Verbose(4) << "Current target is peak!" << endl);
[834ff3]1509 continue;
1510 }
1511
1512 // check for linear dependence
[1f591b]1513 TempVector = (*baseline->second->endpoints[0]->node->node) - (*target->second->node->node);
1514 helper = (*baseline->second->endpoints[1]->node->node) - (*target->second->node->node);
1515 helper.ProjectOntoPlane(TempVector);
[834ff3]1516 if (fabs(helper.NormSquared()) < MYEPSILON) {
[1f2e46]1517 DoLog(2) && (Log() << Verbose(2) << "Chosen set of vectors is linear dependent." << endl);
[834ff3]1518 continue;
1519 }
1520
1521 // in case NOT both were found, create virtually this triangle, get its normal vector, calculate angle
1522 flag = true;
[71910a]1523 VirtualNormalVector = Plane(*(baseline->second->endpoints[0]->node->node),
1524 *(baseline->second->endpoints[1]->node->node),
1525 *(target->second->node->node)).getNormal();
[1f591b]1526 TempVector = (1./3.) * ((*baseline->second->endpoints[0]->node->node) +
1527 (*baseline->second->endpoints[1]->node->node) +
1528 (*target->second->node->node));
1529 TempVector -= (*Center);
[834ff3]1530 // make it always point outward
[1f591b]1531 if (VirtualNormalVector.ScalarProduct(TempVector) < 0)
[834ff3]1532 VirtualNormalVector.Scale(-1.);
1533 // calculate angle
[1f591b]1534 TempAngle = NormalVector.Angle(VirtualNormalVector);
[1f2e46]1535 DoLog(2) && (Log() << Verbose(2) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl);
[834ff3]1536 if ((SmallestAngle - TempAngle) > MYEPSILON) { // set to new possible winner
1537 SmallestAngle = TempAngle;
1538 winner = target;
[1f2e46]1539 DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl);
[834ff3]1540 } else if (fabs(SmallestAngle - TempAngle) < MYEPSILON) { // check the angle to propagation, both possible targets are in one plane! (their normals have same angle)
1541 // hence, check the angles to some normal direction from our base line but in this common plane of both targets...
[1f591b]1542 helper = (*target->second->node->node) - BaseLineCenter;
1543 helper.ProjectOntoPlane(BaseLine);
[834ff3]1544 // ...the one with the smaller angle is the better candidate
[1f591b]1545 TempVector = (*target->second->node->node) - BaseLineCenter;
1546 TempVector.ProjectOntoPlane(VirtualNormalVector);
1547 TempAngle = TempVector.Angle(helper);
1548 TempVector = (*winner->second->node->node) - BaseLineCenter;
1549 TempVector.ProjectOntoPlane(VirtualNormalVector);
1550 if (TempAngle < TempVector.Angle(helper)) {
1551 TempAngle = NormalVector.Angle(VirtualNormalVector);
[834ff3]1552 SmallestAngle = TempAngle;
1553 winner = target;
[1f2e46]1554 DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl);
[834ff3]1555 } else
[1f2e46]1556 DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl);
[834ff3]1557 } else
[1f2e46]1558 DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl);
[834ff3]1559 }
1560 } // end of loop over all boundary points
1561
1562 // 5b. The point of the above whose triangle has the greatest angle with the triangle the current line belongs to (it only belongs to one, remember!): New triangle
1563 if (winner != PointsOnBoundary.end()) {
[1f2e46]1564 DoLog(0) && (Log() << Verbose(0) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl);
[834ff3]1565 // create the lins of not yet present
1566 BLS[0] = baseline->second;
1567 // 5c. add lines to the line set if those were new (not yet part of a triangle), delete lines that belong to two triangles)
1568 LineChecker[0] = baseline->second->endpoints[0]->lines.find(winner->first);
1569 LineChecker[1] = baseline->second->endpoints[1]->lines.find(winner->first);
1570 if (LineChecker[0] == baseline->second->endpoints[0]->lines.end()) { // create
1571 BPS[0] = baseline->second->endpoints[0];
1572 BPS[1] = winner->second;
1573 BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1574 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[1]));
1575 LinesOnBoundaryCount++;
1576 } else
1577 BLS[1] = LineChecker[0]->second;
1578 if (LineChecker[1] == baseline->second->endpoints[1]->lines.end()) { // create
1579 BPS[0] = baseline->second->endpoints[1];
1580 BPS[1] = winner->second;
1581 BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1582 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[2]));
1583 LinesOnBoundaryCount++;
1584 } else
1585 BLS[2] = LineChecker[1]->second;
1586 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
[eeefb3]1587 BTS->GetCenter(&helper);
[1f591b]1588 helper -= (*Center);
1589 helper *= -1;
[eeefb3]1590 BTS->GetNormalVector(helper);
[834ff3]1591 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1592 TrianglesOnBoundaryCount++;
1593 } else {
[c43766]1594 DoeLog(2) && (eLog() << Verbose(2) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl);
[834ff3]1595 }
1596
1597 // 5d. If the set of lines is not yet empty, go to 5. and continue
1598 } else
[1f2e46]1599 DoLog(0) && (Log() << Verbose(0) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl);
[834ff3]1600 } while (flag);
1601
1602 // exit
[c43766]1603 delete (Center);
1604}
1605;
[834ff3]1606
[eeefb3]1607/** Inserts all points outside of the tesselated surface into it by adding new triangles.
[834ff3]1608 * \param *out output stream for debugging
1609 * \param *cloud cluster of points
[eeefb3]1610 * \param *LC LinkedCell structure to find nearest point quickly
[834ff3]1611 * \return true - all straddling points insert, false - something went wrong
1612 */
[543ce4]1613bool Tesselation::InsertStraddlingPoints(const PointCloud *cloud, const LinkedCell *LC)
[834ff3]1614{
[c43766]1615 Info FunctionInfo(__func__);
[0cf171]1616 Vector Intersection, Normal;
[834ff3]1617 TesselPoint *Walker = NULL;
[543ce4]1618 Vector *Center = cloud->GetCenter();
[eda56a]1619 TriangleList *triangles = NULL;
[daf956]1620 bool AddFlag = false;
1621 LinkedCell *BoundaryPoints = NULL;
[eeefb3]1622
[834ff3]1623 cloud->GoToFirst();
[daf956]1624 BoundaryPoints = new LinkedCell(this, 5.);
[c43766]1625 while (!cloud->IsEnd()) { // we only have to go once through all points, as boundary can become only bigger
[daf956]1626 if (AddFlag) {
[c43766]1627 delete (BoundaryPoints);
[daf956]1628 BoundaryPoints = new LinkedCell(this, 5.);
1629 AddFlag = false;
1630 }
[834ff3]1631 Walker = cloud->GetPoint();
[1f2e46]1632 DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Walker << "." << endl);
[834ff3]1633 // get the next triangle
[eda56a]1634 triangles = FindClosestTrianglesToVector(Walker->node, BoundaryPoints);
[daf956]1635 BTS = triangles->front();
1636 if ((triangles == NULL) || (BTS->ContainsBoundaryPoint(Walker))) {
[1f2e46]1637 DoLog(0) && (Log() << Verbose(0) << "No triangles found, probably a tesselation point itself." << endl);
[eeefb3]1638 cloud->GoToNext();
1639 continue;
1640 } else {
[834ff3]1641 }
[1f2e46]1642 DoLog(0) && (Log() << Verbose(0) << "Closest triangle is " << *BTS << "." << endl);
[834ff3]1643 // get the intersection point
[543ce4]1644 if (BTS->GetIntersectionInsideTriangle(Center, Walker->node, &Intersection)) {
[1f2e46]1645 DoLog(0) && (Log() << Verbose(0) << "We have an intersection at " << Intersection << "." << endl);
[834ff3]1646 // we have the intersection, check whether in- or outside of boundary
[1f591b]1647 if ((Center->DistanceSquared(*Walker->node) - Center->DistanceSquared(Intersection)) < -MYEPSILON) {
[834ff3]1648 // inside, next!
[1f2e46]1649 DoLog(0) && (Log() << Verbose(0) << *Walker << " is inside wrt triangle " << *BTS << "." << endl);
[834ff3]1650 } else {
1651 // outside!
[1f2e46]1652 DoLog(0) && (Log() << Verbose(0) << *Walker << " is outside wrt triangle " << *BTS << "." << endl);
[834ff3]1653 class BoundaryLineSet *OldLines[3], *NewLines[3];
1654 class BoundaryPointSet *OldPoints[3], *NewPoint;
1655 // store the three old lines and old points
[c43766]1656 for (int i = 0; i < 3; i++) {
[834ff3]1657 OldLines[i] = BTS->lines[i];
1658 OldPoints[i] = BTS->endpoints[i];
1659 }
[1f591b]1660 Normal = BTS->NormalVector;
[834ff3]1661 // add Walker to boundary points
[1f2e46]1662 DoLog(0) && (Log() << Verbose(0) << "Adding " << *Walker << " to BoundaryPoints." << endl);
[daf956]1663 AddFlag = true;
[c43766]1664 if (AddBoundaryPoint(Walker, 0))
[834ff3]1665 NewPoint = BPS[0];
1666 else
1667 continue;
1668 // remove triangle
[1f2e46]1669 DoLog(0) && (Log() << Verbose(0) << "Erasing triangle " << *BTS << "." << endl);
[834ff3]1670 TrianglesOnBoundary.erase(BTS->Nr);
[c43766]1671 delete (BTS);
[834ff3]1672 // create three new boundary lines
[c43766]1673 for (int i = 0; i < 3; i++) {
[834ff3]1674 BPS[0] = NewPoint;
1675 BPS[1] = OldPoints[i];
1676 NewLines[i] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
[1f2e46]1677 DoLog(1) && (Log() << Verbose(1) << "Creating new line " << *NewLines[i] << "." << endl);
[834ff3]1678 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, NewLines[i])); // no need for check for unique insertion as BPS[0] is definitely a new one
1679 LinesOnBoundaryCount++;
1680 }
1681 // create three new triangle with new point
[c43766]1682 for (int i = 0; i < 3; i++) { // find all baselines
[834ff3]1683 BLS[0] = OldLines[i];
1684 int n = 1;
[c43766]1685 for (int j = 0; j < 3; j++) {
[834ff3]1686 if (NewLines[j]->IsConnectedTo(BLS[0])) {
[c43766]1687 if (n > 2) {
1688 DoeLog(2) && (eLog() << Verbose(2) << BLS[0] << " connects to all of the new lines?!" << endl);
[834ff3]1689 return false;
1690 } else
1691 BLS[n++] = NewLines[j];
1692 }
1693 }
1694 // create the triangle
1695 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
[0cf171]1696 Normal.Scale(-1.);
1697 BTS->GetNormalVector(Normal);
1698 Normal.Scale(-1.);
[1f2e46]1699 DoLog(0) && (Log() << Verbose(0) << "Created new triangle " << *BTS << "." << endl);
[834ff3]1700 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1701 TrianglesOnBoundaryCount++;
1702 }
1703 }
1704 } else { // something is wrong with FindClosestTriangleToPoint!
[c43766]1705 DoeLog(1) && (eLog() << Verbose(1) << "The closest triangle did not produce an intersection!" << endl);
[834ff3]1706 return false;
1707 }
1708 cloud->GoToNext();
1709 }
1710
1711 // exit
[c43766]1712 delete (Center);
[834ff3]1713 return true;
[c43766]1714}
1715;
[834ff3]1716
[eb58a7]1717/** Adds a point to the tesselation::PointsOnBoundary list.
[eeefb3]1718 * \param *Walker point to add
[70c4567]1719 * \param n TesselStruct::BPS index to put pointer into
1720 * \return true - new point was added, false - point already present
[834ff3]1721 */
[a9b2a0a]1722bool Tesselation::AddBoundaryPoint(TesselPoint * Walker, const int n)
[834ff3]1723{
[c43766]1724 Info FunctionInfo(__func__);
[834ff3]1725 PointTestPair InsertUnique;
[70c4567]1726 BPS[n] = new class BoundaryPointSet(Walker);
1727 InsertUnique = PointsOnBoundary.insert(PointPair(Walker->nr, BPS[n]));
1728 if (InsertUnique.second) { // if new point was not present before, increase counter
[834ff3]1729 PointsOnBoundaryCount++;
[70c4567]1730 return true;
1731 } else {
[c43766]1732 delete (BPS[n]);
[70c4567]1733 BPS[n] = InsertUnique.first->second;
1734 return false;
[834ff3]1735 }
1736}
1737;
1738
1739/** Adds point to Tesselation::PointsOnBoundary if not yet present.
1740 * Tesselation::TPS is set to either this new BoundaryPointSet or to the existing one of not unique.
1741 * @param Candidate point to add
1742 * @param n index for this point in Tesselation::TPS array
1743 */
[a9b2a0a]1744void Tesselation::AddTesselationPoint(TesselPoint* Candidate, const int n)
[834ff3]1745{
[c43766]1746 Info FunctionInfo(__func__);
[834ff3]1747 PointTestPair InsertUnique;
1748 TPS[n] = new class BoundaryPointSet(Candidate);
1749 InsertUnique = PointsOnBoundary.insert(PointPair(Candidate->nr, TPS[n]));
1750 if (InsertUnique.second) { // if new point was not present before, increase counter
1751 PointsOnBoundaryCount++;
1752 } else {
1753 delete TPS[n];
[1f2e46]1754 DoLog(0) && (Log() << Verbose(0) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl);
[834ff3]1755 TPS[n] = (InsertUnique.first)->second;
1756 }
1757}
1758;
1759
[09d3b8]1760/** Sets point to a present Tesselation::PointsOnBoundary.
1761 * Tesselation::TPS is set to the existing one or NULL if not found.
1762 * @param Candidate point to set to
1763 * @param n index for this point in Tesselation::TPS array
1764 */
1765void Tesselation::SetTesselationPoint(TesselPoint* Candidate, const int n) const
1766{
[c43766]1767 Info FunctionInfo(__func__);
[09d3b8]1768 PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidate->nr);
1769 if (FindPoint != PointsOnBoundary.end())
1770 TPS[n] = FindPoint->second;
1771 else
1772 TPS[n] = NULL;
[c43766]1773}
1774;
[09d3b8]1775
[834ff3]1776/** Function tries to add line from current Points in BPS to BoundaryLineSet.
1777 * If successful it raises the line count and inserts the new line into the BLS,
1778 * if unsuccessful, it writes the line which had been present into the BLS, deleting the new constructed one.
[086db0]1779 * @param *OptCenter desired OptCenter if there are more than one candidate line
[adff53]1780 * @param *candidate third point of the triangle to be, for checking between multiple open line candidates
[834ff3]1781 * @param *a first endpoint
1782 * @param *b second endpoint
1783 * @param n index of Tesselation::BLS giving the line with both endpoints
1784 */
[c43766]1785void Tesselation::AddTesselationLine(const Vector * const OptCenter, const BoundaryPointSet * const candidate, class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
1786{
[834ff3]1787 bool insertNewLine = true;
[65f1dba]1788 LineMap::iterator FindLine = a->lines.find(b->node->nr);
[adff53]1789 BoundaryLineSet *WinningLine = NULL;
[65f1dba]1790 if (FindLine != a->lines.end()) {
[1f2e46]1791 DoLog(1) && (Log() << Verbose(1) << "INFO: There is at least one line between " << *a << " and " << *b << ": " << *(FindLine->second) << "." << endl);
[65f1dba]1792
[c43766]1793 pair<LineMap::iterator, LineMap::iterator> FindPair;
[834ff3]1794 FindPair = a->lines.equal_range(b->node->nr);
1795
[c43766]1796 for (FindLine = FindPair.first; (FindLine != FindPair.second) && (insertNewLine); FindLine++) {
[1f2e46]1797 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl);
[834ff3]1798 // If there is a line with less than two attached triangles, we don't need a new line.
[adff53]1799 if (FindLine->second->triangles.size() == 1) {
1800 CandidateMap::iterator Finder = OpenLines.find(FindLine->second);
[086db0]1801 if (!Finder->second->pointlist.empty())
[1f2e46]1802 DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl);
[086db0]1803 else
[1f2e46]1804 DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate." << endl);
[086db0]1805 // get open line
[c43766]1806 for (TesselPointList::const_iterator CandidateChecker = Finder->second->pointlist.begin(); CandidateChecker != Finder->second->pointlist.end(); ++CandidateChecker) {
[0d111b]1807 if ((*(CandidateChecker) == candidate->node) && (OptCenter == NULL || OptCenter->DistanceSquared(Finder->second->OptCenter) < MYEPSILON )) { // stop searching if candidate matches
[447896]1808 DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Candidate " << *(*CandidateChecker) << " has the right center " << Finder->second->OptCenter << "." << endl);
[c43766]1809 insertNewLine = false;
1810 WinningLine = FindLine->second;
1811 break;
[447896]1812 } else {
1813 DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *(*CandidateChecker) << "'s center " << Finder->second->OptCenter << " does not match desired on " << *OptCenter << "." << endl);
[c43766]1814 }
[ea3627]1815 }
[834ff3]1816 }
1817 }
1818 }
1819
1820 if (insertNewLine) {
[7c2ebb]1821 AddNewTesselationTriangleLine(a, b, n);
[adff53]1822 } else {
1823 AddExistingTesselationTriangleLine(WinningLine, n);
[834ff3]1824 }
1825}
1826;
1827
1828/**
1829 * Adds lines from each of the current points in the BPS to BoundaryLineSet.
1830 * Raises the line count and inserts the new line into the BLS.
1831 *
1832 * @param *a first endpoint
1833 * @param *b second endpoint
1834 * @param n index of Tesselation::BLS giving the line with both endpoints
1835 */
[7c2ebb]1836void Tesselation::AddNewTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
[834ff3]1837{
[c43766]1838 Info FunctionInfo(__func__);
[1f2e46]1839 DoLog(0) && (Log() << Verbose(0) << "Adding open line [" << LinesOnBoundaryCount << "|" << *(a->node) << " and " << *(b->node) << "." << endl);
[834ff3]1840 BPS[0] = a;
1841 BPS[1] = b;
[c43766]1842 BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); // this also adds the line to the local maps
[834ff3]1843 // add line to global map
1844 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n]));
1845 // increase counter
1846 LinesOnBoundaryCount++;
[4af89b]1847 // also add to open lines
1848 CandidateForTesselation *CFT = new CandidateForTesselation(BLS[n]);
[c43766]1849 OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (BLS[n], CFT));
1850}
1851;
[834ff3]1852
[7c2ebb]1853/** Uses an existing line for a new triangle.
1854 * Sets Tesselation::BLS[\a n] and removes the lines from Tesselation::OpenLines.
1855 * \param *FindLine the line to add
1856 * \param n index of the line to set in Tesselation::BLS
1857 */
1858void Tesselation::AddExistingTesselationTriangleLine(class BoundaryLineSet *Line, int n)
1859{
1860 Info FunctionInfo(__func__);
[1f2e46]1861 DoLog(0) && (Log() << Verbose(0) << "Using existing line " << *Line << endl);
[7c2ebb]1862
1863 // set endpoints and line
1864 BPS[0] = Line->endpoints[0];
1865 BPS[1] = Line->endpoints[1];
1866 BLS[n] = Line;
1867 // remove existing line from OpenLines
1868 CandidateMap::iterator CandidateLine = OpenLines.find(BLS[n]);
1869 if (CandidateLine != OpenLines.end()) {
[1f2e46]1870 DoLog(1) && (Log() << Verbose(1) << " Removing line from OpenLines." << endl);
[c43766]1871 delete (CandidateLine->second);
[7c2ebb]1872 OpenLines.erase(CandidateLine);
1873 } else {
[c43766]1874 DoeLog(1) && (eLog() << Verbose(1) << "Line exists and is attached to less than two triangles, but not in OpenLines!" << endl);
[7c2ebb]1875 }
[c43766]1876}
1877;
[834ff3]1878
[daf956]1879/** Function adds triangle to global list.
1880 * Furthermore, the triangle receives the next free id and id counter \a TrianglesOnBoundaryCount is increased.
[834ff3]1881 */
[eb58a7]1882void Tesselation::AddTesselationTriangle()
[834ff3]1883{
[c43766]1884 Info FunctionInfo(__func__);
[1f2e46]1885 DoLog(1) && (Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl);
[834ff3]1886
1887 // add triangle to global map
1888 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1889 TrianglesOnBoundaryCount++;
1890
[60cbe5]1891 // set as last new triangle
1892 LastTriangle = BTS;
1893
[834ff3]1894 // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
[c43766]1895}
1896;
[eb58a7]1897
[daf956]1898/** Function adds triangle to global list.
1899 * Furthermore, the triangle number is set to \a nr.
1900 * \param nr triangle number
1901 */
[a9b2a0a]1902void Tesselation::AddTesselationTriangle(const int nr)
[daf956]1903{
[c43766]1904 Info FunctionInfo(__func__);
[1f2e46]1905 DoLog(0) && (Log() << Verbose(0) << "Adding triangle to global TrianglesOnBoundary map." << endl);
[daf956]1906
1907 // add triangle to global map
1908 TrianglesOnBoundary.insert(TrianglePair(nr, BTS));
1909
1910 // set as last new triangle
1911 LastTriangle = BTS;
1912
1913 // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
[c43766]1914}
1915;
[daf956]1916
[eb58a7]1917/** Removes a triangle from the tesselation.
1918 * Removes itself from the TriangleMap's of its lines, calls for them RemoveTriangleLine() if they are no more connected.
1919 * Removes itself from memory.
1920 * \param *triangle to remove
1921 */
1922void Tesselation::RemoveTesselationTriangle(class BoundaryTriangleSet *triangle)
1923{
[c43766]1924 Info FunctionInfo(__func__);
[eb58a7]1925 if (triangle == NULL)
1926 return;
1927 for (int i = 0; i < 3; i++) {
1928 if (triangle->lines[i] != NULL) {
[1f2e46]1929 DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl);
[eb58a7]1930 triangle->lines[i]->triangles.erase(triangle->Nr);
1931 if (triangle->lines[i]->triangles.empty()) {
[1f2e46]1932 DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl);
[c43766]1933 RemoveTesselationLine(triangle->lines[i]);
[b73929]1934 } else {
[1f2e46]1935 DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is still attached to another triangle: ");
[c43766]1936 OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (triangle->lines[i], NULL));
1937 for (TriangleMap::iterator TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); TriangleRunner++)
[1f2e46]1938 DoLog(0) && (Log() << Verbose(0) << "[" << (TriangleRunner->second)->Nr << "|" << *((TriangleRunner->second)->endpoints[0]) << ", " << *((TriangleRunner->second)->endpoints[1]) << ", " << *((TriangleRunner->second)->endpoints[2]) << "] \t");
1939 DoLog(0) && (Log() << Verbose(0) << endl);
[c43766]1940 // for (int j=0;j<2;j++) {
1941 // Log() << Verbose(0) << "Lines of endpoint " << *(triangle->lines[i]->endpoints[j]) << ": ";
1942 // for(LineMap::iterator LineRunner = triangle->lines[i]->endpoints[j]->lines.begin(); LineRunner != triangle->lines[i]->endpoints[j]->lines.end(); LineRunner++)
1943 // Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
1944 // Log() << Verbose(0) << endl;
1945 // }
[b73929]1946 }
[c43766]1947 triangle->lines[i] = NULL; // free'd or not: disconnect
[eb58a7]1948 } else
[c43766]1949 DoeLog(1) && (eLog() << Verbose(1) << "This line " << i << " has already been free'd." << endl);
[eb58a7]1950 }
1951
1952 if (TrianglesOnBoundary.erase(triangle->Nr))
[1f2e46]1953 DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr. " << triangle->Nr << "." << endl);
[c43766]1954 delete (triangle);
1955}
1956;
[eb58a7]1957
1958/** Removes a line from the tesselation.
1959 * Removes itself from each endpoints' LineMap, then removes itself from global LinesOnBoundary list and free's the line.
1960 * \param *line line to remove
1961 */
1962void Tesselation::RemoveTesselationLine(class BoundaryLineSet *line)
1963{
[c43766]1964 Info FunctionInfo(__func__);
[eb58a7]1965 int Numbers[2];
1966
1967 if (line == NULL)
1968 return;
[b73929]1969 // get other endpoint number for finding copies of same line
[eb58a7]1970 if (line->endpoints[1] != NULL)
1971 Numbers[0] = line->endpoints[1]->Nr;
1972 else
1973 Numbers[0] = -1;
1974 if (line->endpoints[0] != NULL)
1975 Numbers[1] = line->endpoints[0]->Nr;
1976 else
1977 Numbers[1] = -1;
1978
1979 for (int i = 0; i < 2; i++) {
1980 if (line->endpoints[i] != NULL) {
1981 if (Numbers[i] != -1) { // as there may be multiple lines with same endpoints, we have to go through each and find in the endpoint's line list this line set
1982 pair<LineMap::iterator, LineMap::iterator> erasor = line->endpoints[i]->lines.equal_range(Numbers[i]);
1983 for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
1984 if ((*Runner).second == line) {
[1f2e46]1985 DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl);
[eb58a7]1986 line->endpoints[i]->lines.erase(Runner);
1987 break;
1988 }
1989 } else { // there's just a single line left
1990 if (line->endpoints[i]->lines.erase(line->Nr))
[1f2e46]1991 DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl);
[eb58a7]1992 }
1993 if (line->endpoints[i]->lines.empty()) {
[1f2e46]1994 DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl);
[eb58a7]1995 RemoveTesselationPoint(line->endpoints[i]);
[b73929]1996 } else {
[1f2e46]1997 DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has still lines it's attached to: ");
[c43766]1998 for (LineMap::iterator LineRunner = line->endpoints[i]->lines.begin(); LineRunner != line->endpoints[i]->lines.end(); LineRunner++)
[1f2e46]1999 DoLog(0) && (Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t");
2000 DoLog(0) && (Log() << Verbose(0) << endl);
[b73929]2001 }
[c43766]2002 line->endpoints[i] = NULL; // free'd or not: disconnect
[eb58a7]2003 } else
[c43766]2004 DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << i << " has already been free'd." << endl);
[eb58a7]2005 }
2006 if (!line->triangles.empty())
[c43766]2007 DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *line << " am still connected to some triangles." << endl);
[eb58a7]2008
2009 if (LinesOnBoundary.erase(line->Nr))
[1f2e46]2010 DoLog(0) && (Log() << Verbose(0) << "Removing line Nr. " << line->Nr << "." << endl);
[c43766]2011 delete (line);
2012}
2013;
[eb58a7]2014
2015/** Removes a point from the tesselation.
2016 * Checks whether there are still lines connected, removes from global PointsOnBoundary list, then free's the point.
2017 * \note If a point should be removed, while keep the tesselated surface intact (i.e. closed), use RemovePointFromTesselatedSurface()
2018 * \param *point point to remove
2019 */
2020void Tesselation::RemoveTesselationPoint(class BoundaryPointSet *point)
2021{
[c43766]2022 Info FunctionInfo(__func__);
[eb58a7]2023 if (point == NULL)
2024 return;
2025 if (PointsOnBoundary.erase(point->Nr))
[1f2e46]2026 DoLog(0) && (Log() << Verbose(0) << "Removing point Nr. " << point->Nr << "." << endl);
[c43766]2027 delete (point);
2028}
2029;
[086db0]2030
2031/** Checks validity of a given sphere of a candidate line.
2032 * \sa CandidateForTesselation::CheckValidity(), which is more evolved.
[c43766]2033 * We check CandidateForTesselation::OtherOptCenter
2034 * \param &CandidateLine contains other degenerated candidates which we have to subtract as well
[086db0]2035 * \param RADIUS radius of sphere
2036 * \param *LC LinkedCell structure with other atoms
2037 * \return true - candidate triangle is degenerated, false - candidate triangle is not degenerated
2038 */
[c43766]2039bool Tesselation::CheckDegeneracy(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC) const
[086db0]2040{
2041 Info FunctionInfo(__func__);
2042
2043 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl);
2044 bool flag = true;
2045
[0d111b]2046 DoLog(1) && (Log() << Verbose(1) << "Check by: draw sphere {" << CandidateLine.OtherOptCenter[0] << " " << CandidateLine.OtherOptCenter[1] << " " << CandidateLine.OtherOptCenter[2] << "} radius " << RADIUS << " resolution 30" << endl);
[086db0]2047 // get all points inside the sphere
[c43766]2048 TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, &CandidateLine.OtherOptCenter);
[086db0]2049
[1f2e46]2050 DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << CandidateLine.OtherOptCenter << ":" << endl);
[086db0]2051 for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
[4eee8f]2052 DoLog(1) && (Log() << Verbose(1) << " " << *(*Runner) << " with distance " << (*Runner)->node->distance(CandidateLine.OtherOptCenter) << "." << endl);
[086db0]2053
2054 // remove triangles's endpoints
[c43766]2055 for (int i = 0; i < 2; i++)
2056 ListofPoints->remove(CandidateLine.BaseLine->endpoints[i]->node);
2057
2058 // remove other candidates
2059 for (TesselPointList::const_iterator Runner = CandidateLine.pointlist.begin(); Runner != CandidateLine.pointlist.end(); ++Runner)
2060 ListofPoints->remove(*Runner);
[086db0]2061
2062 // check for other points
2063 if (!ListofPoints->empty()) {
[1f2e46]2064 DoLog(1) && (Log() << Verbose(1) << "CheckDegeneracy: There are still " << ListofPoints->size() << " points inside the sphere." << endl);
[086db0]2065 flag = false;
[1f2e46]2066 DoLog(1) && (Log() << Verbose(1) << "External atoms inside of sphere at " << CandidateLine.OtherOptCenter << ":" << endl);
[086db0]2067 for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
[4eee8f]2068 DoLog(1) && (Log() << Verbose(1) << " " << *(*Runner) << " with distance " << (*Runner)->node->distance(CandidateLine.OtherOptCenter) << "." << endl);
[086db0]2069 }
[c43766]2070 delete (ListofPoints);
[086db0]2071
2072 return flag;
[c43766]2073}
2074;
[834ff3]2075
[eeefb3]2076/** Checks whether the triangle consisting of the three points is already present.
[834ff3]2077 * Searches for the points in Tesselation::PointsOnBoundary and checks their
2078 * lines. If any of the three edges already has two triangles attached, false is
2079 * returned.
2080 * \param *out output stream for debugging
2081 * \param *Candidates endpoints of the triangle candidate
2082 * \return integer 0 if no triangle exists, 1 if one triangle exists, 2 if two
2083 * triangles exist which is the maximum for three points
2084 */
[09d3b8]2085int Tesselation::CheckPresenceOfTriangle(TesselPoint *Candidates[3]) const
2086{
[c43766]2087 Info FunctionInfo(__func__);
[834ff3]2088 int adjacentTriangleCount = 0;
2089 class BoundaryPointSet *Points[3];
2090
2091 // builds a triangle point set (Points) of the end points
2092 for (int i = 0; i < 3; i++) {
[09d3b8]2093 PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr);
[834ff3]2094 if (FindPoint != PointsOnBoundary.end()) {
2095 Points[i] = FindPoint->second;
2096 } else {
2097 Points[i] = NULL;
2098 }
2099 }
2100
2101 // checks lines between the points in the Points for their adjacent triangles
2102 for (int i = 0; i < 3; i++) {
2103 if (Points[i] != NULL) {
2104 for (int j = i; j < 3; j++) {
2105 if (Points[j] != NULL) {
[09d3b8]2106 LineMap::const_iterator FindLine = Points[i]->lines.find(Points[j]->node->nr);
[834ff3]2107 for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) {
2108 TriangleMap *triangles = &FindLine->second->triangles;
[1f2e46]2109 DoLog(1) && (Log() << Verbose(1) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl);
[09d3b8]2110 for (TriangleMap::const_iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) {
[834ff3]2111 if (FindTriangle->second->IsPresentTupel(Points)) {
2112 adjacentTriangleCount++;
2113 }
2114 }
[1f2e46]2115 DoLog(1) && (Log() << Verbose(1) << "end." << endl);
[834ff3]2116 }
2117 // Only one of the triangle lines must be considered for the triangle count.
[be2997]2118 //Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
[b73929]2119 //return adjacentTriangleCount;
[834ff3]2120 }
2121 }
2122 }
2123 }
2124
[1f2e46]2125 DoLog(0) && (Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl);
[834ff3]2126 return adjacentTriangleCount;
[c43766]2127}
2128;
[834ff3]2129
[b73929]2130/** Checks whether the triangle consisting of the three points is already present.
2131 * Searches for the points in Tesselation::PointsOnBoundary and checks their
2132 * lines. If any of the three edges already has two triangles attached, false is
2133 * returned.
2134 * \param *out output stream for debugging
2135 * \param *Candidates endpoints of the triangle candidate
2136 * \return NULL - none found or pointer to triangle
2137 */
[543ce4]2138class BoundaryTriangleSet * Tesselation::GetPresentTriangle(TesselPoint *Candidates[3])
[b73929]2139{
[c43766]2140 Info FunctionInfo(__func__);
[b73929]2141 class BoundaryTriangleSet *triangle = NULL;
2142 class BoundaryPointSet *Points[3];
2143
2144 // builds a triangle point set (Points) of the end points
2145 for (int i = 0; i < 3; i++) {
2146 PointMap::iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr);
2147 if (FindPoint != PointsOnBoundary.end()) {
2148 Points[i] = FindPoint->second;
2149 } else {
2150 Points[i] = NULL;
2151 }
2152 }
2153
2154 // checks lines between the points in the Points for their adjacent triangles
2155 for (int i = 0; i < 3; i++) {
2156 if (Points[i] != NULL) {
2157 for (int j = i; j < 3; j++) {
2158 if (Points[j] != NULL) {
2159 LineMap::iterator FindLine = Points[i]->lines.find(Points[j]->node->nr);
2160 for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) {
2161 TriangleMap *triangles = &FindLine->second->triangles;
2162 for (TriangleMap::iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) {
2163 if (FindTriangle->second->IsPresentTupel(Points)) {
2164 if ((triangle == NULL) || (triangle->Nr > FindTriangle->second->Nr))
2165 triangle = FindTriangle->second;
2166 }
2167 }
2168 }
2169 // Only one of the triangle lines must be considered for the triangle count.
[be2997]2170 //Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
[b73929]2171 //return adjacentTriangleCount;
2172 }
2173 }
2174 }
2175 }
2176
2177 return triangle;
[c43766]2178}
2179;
[834ff3]2180
[48cd93]2181/** Finds the starting triangle for FindNonConvexBorder().
2182 * Looks at the outermost point per axis, then FindSecondPointForTesselation()
2183 * for the second and FindNextSuitablePointViaAngleOfSphere() for the third
[834ff3]2184 * point are called.
2185 * \param *out output stream for debugging
2186 * \param RADIUS radius of virtual rolling sphere
2187 * \param *LC LinkedCell structure with neighbouring TesselPoint's
[f07455]2188 * \return true - a starting triangle has been created, false - no valid triple of points found
[834ff3]2189 */
[f07455]2190bool Tesselation::FindStartingTriangle(const double RADIUS, const LinkedCell *LC)
[834ff3]2191{
[c43766]2192 Info FunctionInfo(__func__);
[834ff3]2193 int i = 0;
[eeefb3]2194 TesselPoint* MaxPoint[NDIM];
[3aa635]2195 TesselPoint* Temporary;
[48cd93]2196 double maxCoordinate[NDIM];
[086db0]2197 BoundaryLineSet *BaseLine = NULL;
[834ff3]2198 Vector helper;
2199 Vector Chord;
2200 Vector SearchDirection;
[c43766]2201 Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
[65f1dba]2202 Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
2203 Vector SphereCenter;
2204 Vector NormalVector;
[834ff3]2205
[65f1dba]2206 NormalVector.Zero();
[834ff3]2207
2208 for (i = 0; i < 3; i++) {
[eeefb3]2209 MaxPoint[i] = NULL;
[48cd93]2210 maxCoordinate[i] = -1;
[834ff3]2211 }
2212
[eeefb3]2213 // 1. searching topmost point with respect to each axis
[c43766]2214 for (int i = 0; i < NDIM; i++) { // each axis
2215 LC->n[i] = LC->N[i] - 1; // current axis is topmost cell
2216 for (LC->n[(i + 1) % NDIM] = 0; LC->n[(i + 1) % NDIM] < LC->N[(i + 1) % NDIM]; LC->n[(i + 1) % NDIM]++)
2217 for (LC->n[(i + 2) % NDIM] = 0; LC->n[(i + 2) % NDIM] < LC->N[(i + 2) % NDIM]; LC->n[(i + 2) % NDIM]++) {
[6dd8d3]2218 const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
[be2997]2219 //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
[834ff3]2220 if (List != NULL) {
[c43766]2221 for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[71910a]2222 if ((*Runner)->node->at(i) > maxCoordinate[i]) {
[1f2e46]2223 DoLog(1) && (Log() << Verbose(1) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl);
[71910a]2224 maxCoordinate[i] = (*Runner)->node->at(i);
[eeefb3]2225 MaxPoint[i] = (*Runner);
[834ff3]2226 }
2227 }
2228 } else {
[c43766]2229 DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
[834ff3]2230 }
2231 }
2232 }
2233
[1f2e46]2234 DoLog(1) && (Log() << Verbose(1) << "Found maximum coordinates: ");
[c43766]2235 for (int i = 0; i < NDIM; i++)
[1f2e46]2236 DoLog(0) && (Log() << Verbose(0) << i << ": " << *MaxPoint[i] << "\t");
2237 DoLog(0) && (Log() << Verbose(0) << endl);
[834ff3]2238
2239 BTS = NULL;
[c43766]2240 for (int k = 0; k < NDIM; k++) {
[65f1dba]2241 NormalVector.Zero();
[71910a]2242 NormalVector[k] = 1.;
[086db0]2243 BaseLine = new BoundaryLineSet();
2244 BaseLine->endpoints[0] = new BoundaryPointSet(MaxPoint[k]);
[1f2e46]2245 DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl);
[834ff3]2246
2247 double ShortestAngle;
2248 ShortestAngle = 999999.; // This will contain the angle, which will be always positive (when looking for second point), when looking for third point this will be the quadrant.
2249
[424d3fe]2250 Temporary = NULL;
[086db0]2251 FindSecondPointForTesselation(BaseLine->endpoints[0]->node, NormalVector, Temporary, &ShortestAngle, RADIUS, LC); // we give same point as next candidate as its bonds are looked into in find_second_...
[60612f]2252 if (Temporary == NULL) {
2253 // have we found a second point?
2254 delete BaseLine;
[834ff3]2255 continue;
[60612f]2256 }
[086db0]2257 BaseLine->endpoints[1] = new BoundaryPointSet(Temporary);
[834ff3]2258
[65f1dba]2259 // construct center of circle
[0d111b]2260 CircleCenter = 0.5 * ((*BaseLine->endpoints[0]->node->node) + (*BaseLine->endpoints[1]->node->node));
[65f1dba]2261
2262 // construct normal vector of circle
[0d111b]2263 CirclePlaneNormal = (*BaseLine->endpoints[0]->node->node) - (*BaseLine->endpoints[1]->node->node);
[834ff3]2264
[65f1dba]2265 double radius = CirclePlaneNormal.NormSquared();
[c43766]2266 double CircleRadius = sqrt(RADIUS * RADIUS - radius / 4.);
[65f1dba]2267
[1f591b]2268 NormalVector.ProjectOntoPlane(CirclePlaneNormal);
[65f1dba]2269 NormalVector.Normalize();
[c43766]2270 ShortestAngle = 2. * M_PI; // This will indicate the quadrant.
[65f1dba]2271
[1f591b]2272 SphereCenter = (CircleRadius * NormalVector) + CircleCenter;
[65f1dba]2273 // Now, NormalVector and SphereCenter are two orthonormalized vectors in the plane defined by CirclePlaneNormal (not normalized)
[834ff3]2274
2275 // look in one direction of baseline for initial candidate
[71910a]2276 SearchDirection = Plane(CirclePlaneNormal, NormalVector,0).getNormal(); // whether we look "left" first or "right" first is not important ...
[834ff3]2277
[0cf171]2278 // adding point 1 and point 2 and add the line between them
[1f2e46]2279 DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl);
2280 DoLog(0) && (Log() << Verbose(0) << "Found second point is at " << *BaseLine->endpoints[1]->node << ".\n");
[834ff3]2281
[be2997]2282 //Log() << Verbose(1) << "INFO: OldSphereCenter is at " << helper << ".\n";
[086db0]2283 CandidateForTesselation OptCandidates(BaseLine);
[65f1dba]2284 FindThirdPointForTesselation(NormalVector, SearchDirection, SphereCenter, OptCandidates, NULL, RADIUS, LC);
[1f2e46]2285 DoLog(0) && (Log() << Verbose(0) << "List of third Points is:" << endl);
[be2997]2286 for (TesselPointList::iterator it = OptCandidates.pointlist.begin(); it != OptCandidates.pointlist.end(); it++) {
[1f2e46]2287 DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl);
[834ff3]2288 }
[086db0]2289 if (!OptCandidates.pointlist.empty()) {
2290 BTS = NULL;
2291 AddCandidatePolygon(OptCandidates, RADIUS, LC);
2292 } else {
2293 delete BaseLine;
2294 continue;
[834ff3]2295 }
2296
[60612f]2297 if (BTS != NULL) { // we have created one starting triangle
2298 delete BaseLine;
[834ff3]2299 break;
[60612f]2300 } else {
[834ff3]2301 // remove all candidates from the list and then the list itself
[3aa635]2302 OptCandidates.pointlist.clear();
[834ff3]2303 }
[086db0]2304 delete BaseLine;
[834ff3]2305 }
[f07455]2306
2307 return (BTS != NULL);
[c43766]2308}
2309;
[834ff3]2310
[09d3b8]2311/** Checks for a given baseline and a third point candidate whether baselines of the found triangle don't have even better candidates.
2312 * This is supposed to prevent early closing of the tesselation.
[be2997]2313 * \param CandidateLine CandidateForTesselation with baseline and shortestangle , i.e. not \a *OptCandidate
[09d3b8]2314 * \param *ThirdNode third point in triangle, not in BoundaryLineSet::endpoints
2315 * \param RADIUS radius of sphere
2316 * \param *LC LinkedCell structure
2317 * \return true - there is a better candidate (smaller angle than \a ShortestAngle), false - no better TesselPoint candidate found
2318 */
[be2997]2319//bool Tesselation::HasOtherBaselineBetterCandidate(CandidateForTesselation &CandidateLine, const TesselPoint * const ThirdNode, double RADIUS, const LinkedCell * const LC) const
2320//{
2321// Info FunctionInfo(__func__);
2322// bool result = false;
2323// Vector CircleCenter;
2324// Vector CirclePlaneNormal;
2325// Vector OldSphereCenter;
2326// Vector SearchDirection;
2327// Vector helper;
2328// TesselPoint *OtherOptCandidate = NULL;
2329// double OtherShortestAngle = 2.*M_PI; // This will indicate the quadrant.
2330// double radius, CircleRadius;
2331// BoundaryLineSet *Line = NULL;
2332// BoundaryTriangleSet *T = NULL;
2333//
2334// // check both other lines
2335// PointMap::const_iterator FindPoint = PointsOnBoundary.find(ThirdNode->nr);
2336// if (FindPoint != PointsOnBoundary.end()) {
2337// for (int i=0;i<2;i++) {
2338// LineMap::const_iterator FindLine = (FindPoint->second)->lines.find(BaseRay->endpoints[0]->node->nr);
2339// if (FindLine != (FindPoint->second)->lines.end()) {
2340// Line = FindLine->second;
2341// Log() << Verbose(0) << "Found line " << *Line << "." << endl;
2342// if (Line->triangles.size() == 1) {
2343// T = Line->triangles.begin()->second;
2344// // construct center of circle
2345// CircleCenter.CopyVector(Line->endpoints[0]->node->node);
2346// CircleCenter.AddVector(Line->endpoints[1]->node->node);
2347// CircleCenter.Scale(0.5);
2348//
2349// // construct normal vector of circle
2350// CirclePlaneNormal.CopyVector(Line->endpoints[0]->node->node);
2351// CirclePlaneNormal.SubtractVector(Line->endpoints[1]->node->node);
2352//
2353// // calculate squared radius of circle
2354// radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
2355// if (radius/4. < RADIUS*RADIUS) {
2356// CircleRadius = RADIUS*RADIUS - radius/4.;
2357// CirclePlaneNormal.Normalize();
2358// //Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
2359//
2360// // construct old center
2361// GetCenterofCircumcircle(&OldSphereCenter, *T->endpoints[0]->node->node, *T->endpoints[1]->node->node, *T->endpoints[2]->node->node);
2362// helper.CopyVector(&T->NormalVector); // normal vector ensures that this is correct center of the two possible ones
2363// radius = Line->endpoints[0]->node->node->DistanceSquared(&OldSphereCenter);
2364// helper.Scale(sqrt(RADIUS*RADIUS - radius));
2365// OldSphereCenter.AddVector(&helper);
2366// OldSphereCenter.SubtractVector(&CircleCenter);
2367// //Log() << Verbose(1) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl;
2368//
2369// // construct SearchDirection
2370// SearchDirection.MakeNormalVector(&T->NormalVector, &CirclePlaneNormal);
2371// helper.CopyVector(Line->endpoints[0]->node->node);
2372// helper.SubtractVector(ThirdNode->node);
2373// if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
2374// SearchDirection.Scale(-1.);
2375// SearchDirection.ProjectOntoPlane(&OldSphereCenter);
2376// SearchDirection.Normalize();
2377// Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
2378// if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
2379// // rotated the wrong way!
[12f57b]2380// DoeLog(1) && (eLog()<< Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl);
[be2997]2381// }
2382//
2383// // add third point
2384// FindThirdPointForTesselation(T->NormalVector, SearchDirection, OldSphereCenter, OptCandidates, ThirdNode, RADIUS, LC);
2385// for (TesselPointList::iterator it = OptCandidates.pointlist.begin(); it != OptCandidates.pointlist.end(); ++it) {
2386// if (((*it) == BaseRay->endpoints[0]->node) || ((*it) == BaseRay->endpoints[1]->node)) // skip if it's the same triangle than suggested
2387// continue;
2388// Log() << Verbose(0) << " Third point candidate is " << (*it)
2389// << " with circumsphere's center at " << (*it)->OptCenter << "." << endl;
2390// Log() << Verbose(0) << " Baseline is " << *BaseRay << endl;
2391//
2392// // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2)
2393// TesselPoint *PointCandidates[3];
2394// PointCandidates[0] = (*it);
2395// PointCandidates[1] = BaseRay->endpoints[0]->node;
2396// PointCandidates[2] = BaseRay->endpoints[1]->node;
2397// bool check=false;
2398// int existentTrianglesCount = CheckPresenceOfTriangle(PointCandidates);
2399// // If there is no triangle, add it regularly.
2400// if (existentTrianglesCount == 0) {
2401// SetTesselationPoint((*it), 0);
2402// SetTesselationPoint(BaseRay->endpoints[0]->node, 1);
2403// SetTesselationPoint(BaseRay->endpoints[1]->node, 2);
2404//
2405// if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const )TPS)) {
2406// OtherOptCandidate = (*it);
2407// check = true;
2408// }
2409// } else if ((existentTrianglesCount >= 1) && (existentTrianglesCount <= 3)) { // If there is a planar region within the structure, we need this triangle a second time.
2410// SetTesselationPoint((*it), 0);
2411// SetTesselationPoint(BaseRay->endpoints[0]->node, 1);
2412// SetTesselationPoint(BaseRay->endpoints[1]->node, 2);
2413//
2414// // We demand that at most one new degenerate line is created and that this line also already exists (which has to be the case due to existentTrianglesCount == 1)
2415// // i.e. at least one of the three lines must be present with TriangleCount <= 1
2416// if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const)TPS)) {
2417// OtherOptCandidate = (*it);
2418// check = true;
2419// }
2420// }
2421//
2422// if (check) {
2423// if (ShortestAngle > OtherShortestAngle) {
2424// Log() << Verbose(0) << "There is a better candidate than " << *ThirdNode << " with " << ShortestAngle << " from baseline " << *Line << ": " << *OtherOptCandidate << " with " << OtherShortestAngle << "." << endl;
2425// result = true;
2426// break;
2427// }
2428// }
2429// }
2430// delete(OptCandidates);
2431// if (result)
2432// break;
2433// } else {
2434// Log() << Verbose(0) << "Circumcircle for base line " << *Line << " and base triangle " << T << " is too big!" << endl;
2435// }
2436// } else {
[12f57b]2437// DoeLog(2) && (eLog()<< Verbose(2) << "Baseline is connected to two triangles already?" << endl);
[be2997]2438// }
2439// } else {
2440// Log() << Verbose(1) << "No present baseline between " << BaseRay->endpoints[0] << " and candidate " << *ThirdNode << "." << endl;
2441// }
2442// }
2443// } else {
[12f57b]2444// DoeLog(1) && (eLog()<< Verbose(1) << "Could not find the TesselPoint " << *ThirdNode << "." << endl);
[be2997]2445// }
2446//
2447// return result;
2448//};
[834ff3]2449
2450/** This function finds a triangle to a line, adjacent to an existing one.
2451 * @param out output stream for debugging
[4af89b]2452 * @param CandidateLine current cadndiate baseline to search from
[834ff3]2453 * @param T current triangle which \a Line is edge of
2454 * @param RADIUS radius of the rolling ball
2455 * @param N number of found triangles
[eeefb3]2456 * @param *LC LinkedCell structure with neighbouring points
[834ff3]2457 */
[086db0]2458bool Tesselation::FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, const BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC)
[834ff3]2459{
[c43766]2460 Info FunctionInfo(__func__);
[834ff3]2461 Vector CircleCenter;
2462 Vector CirclePlaneNormal;
[65f1dba]2463 Vector RelativeSphereCenter;
[834ff3]2464 Vector SearchDirection;
2465 Vector helper;
[a732ff]2466 BoundaryPointSet *ThirdPoint = NULL;
[834ff3]2467 LineMap::iterator testline;
2468 double radius, CircleRadius;
2469
[c43766]2470 for (int i = 0; i < 3; i++)
[a732ff]2471 if ((T.endpoints[i] != CandidateLine.BaseLine->endpoints[0]) && (T.endpoints[i] != CandidateLine.BaseLine->endpoints[1])) {
2472 ThirdPoint = T.endpoints[i];
[65f1dba]2473 break;
2474 }
[1f2e46]2475 DoLog(0) && (Log() << Verbose(0) << "Current baseline is " << *CandidateLine.BaseLine << " with ThirdPoint " << *ThirdPoint << " of triangle " << T << "." << endl);
[a732ff]2476
2477 CandidateLine.T = &T;
[834ff3]2478
2479 // construct center of circle
[1f591b]2480 CircleCenter = 0.5 * ((*CandidateLine.BaseLine->endpoints[0]->node->node) +
2481 (*CandidateLine.BaseLine->endpoints[1]->node->node));
[834ff3]2482
2483 // construct normal vector of circle
[1f591b]2484 CirclePlaneNormal = (*CandidateLine.BaseLine->endpoints[0]->node->node) -
2485 (*CandidateLine.BaseLine->endpoints[1]->node->node);
[834ff3]2486
2487 // calculate squared radius of circle
[1f591b]2488 radius = CirclePlaneNormal.ScalarProduct(CirclePlaneNormal);
[c43766]2489 if (radius / 4. < RADIUS * RADIUS) {
[65f1dba]2490 // construct relative sphere center with now known CircleCenter
[1f591b]2491 RelativeSphereCenter = T.SphereCenter - CircleCenter;
[65f1dba]2492
[c43766]2493 CircleRadius = RADIUS * RADIUS - radius / 4.;
[834ff3]2494 CirclePlaneNormal.Normalize();
[1f2e46]2495 DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
[834ff3]2496
[1f2e46]2497 DoLog(1) && (Log() << Verbose(1) << "INFO: OldSphereCenter is at " << T.SphereCenter << "." << endl);
[65f1dba]2498
2499 // construct SearchDirection and an "outward pointer"
[71910a]2500 SearchDirection = Plane(RelativeSphereCenter, CirclePlaneNormal,0).getNormal();
[0d111b]2501 helper = CircleCenter - (*ThirdPoint->node->node);
[1f591b]2502 if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
[834ff3]2503 SearchDirection.Scale(-1.);
[1f2e46]2504 DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
[1f591b]2505 if (fabs(RelativeSphereCenter.ScalarProduct(SearchDirection)) > HULLEPSILON) {
[834ff3]2506 // rotated the wrong way!
[c43766]2507 DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl);
[834ff3]2508 }
2509
2510 // add third point
[a732ff]2511 FindThirdPointForTesselation(T.NormalVector, SearchDirection, T.SphereCenter, CandidateLine, ThirdPoint, RADIUS, LC);
[834ff3]2512
2513 } else {
[1f2e46]2514 DoLog(0) && (Log() << Verbose(0) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and base triangle " << T << " is too big!" << endl);
[834ff3]2515 }
2516
[be2997]2517 if (CandidateLine.pointlist.empty()) {
[c43766]2518 DoeLog(2) && (eLog() << Verbose(2) << "Could not find a suitable candidate." << endl);
[834ff3]2519 return false;
2520 }
[1f2e46]2521 DoLog(0) && (Log() << Verbose(0) << "Third Points are: " << endl);
[be2997]2522 for (TesselPointList::iterator it = CandidateLine.pointlist.begin(); it != CandidateLine.pointlist.end(); ++it) {
[1f2e46]2523 DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl);
[834ff3]2524 }
2525
[be2997]2526 return true;
[c43766]2527}
2528;
[be2997]2529
[c43766]2530/** Walks through Tesselation::OpenLines() and finds candidates for newly created ones.
2531 * \param *&LCList atoms in LinkedCell list
2532 * \param RADIUS radius of the virtual sphere
2533 * \return true - for all open lines without candidates so far, a candidate has been found,
2534 * false - at least one open line without candidate still
2535 */
2536bool Tesselation::FindCandidatesforOpenLines(const double RADIUS, const LinkedCell *&LCList)
2537{
2538 bool TesselationFailFlag = true;
2539 CandidateForTesselation *baseline = NULL;
2540 BoundaryTriangleSet *T = NULL;
2541
2542 for (CandidateMap::iterator Runner = OpenLines.begin(); Runner != OpenLines.end(); Runner++) {
2543 baseline = Runner->second;
2544 if (baseline->pointlist.empty()) {
[d22a30]2545 assert((baseline->BaseLine->triangles.size() == 1) && ("Open line without exactly one attached triangle"));
[c43766]2546 T = (((baseline->BaseLine->triangles.begin()))->second);
[1f2e46]2547 DoLog(1) && (Log() << Verbose(1) << "Finding best candidate for open line " << *baseline->BaseLine << " of triangle " << *T << endl);
[c43766]2548 TesselationFailFlag = TesselationFailFlag && FindNextSuitableTriangle(*baseline, *T, RADIUS, LCList); //the line is there, so there is a triangle, but only one.
2549 }
2550 }
2551 return TesselationFailFlag;
2552}
2553;
[834ff3]2554
[4af89b]2555/** Adds the present line and candidate point from \a &CandidateLine to the Tesselation.
[be2997]2556 * \param CandidateLine triangle to add
[7c2ebb]2557 * \param RADIUS Radius of sphere
2558 * \param *LC LinkedCell structure
2559 * \NOTE we need the copy operator here as the original CandidateForTesselation is removed in
2560 * AddTesselationLine() in AddCandidateTriangle()
[4af89b]2561 */
[7c2ebb]2562void Tesselation::AddCandidatePolygon(CandidateForTesselation CandidateLine, const double RADIUS, const LinkedCell *LC)
[4af89b]2563{
[ce0de8]2564 Info FunctionInfo(__func__);
[4af89b]2565 Vector Center;
[013ad57]2566 TesselPoint * const TurningPoint = CandidateLine.BaseLine->endpoints[0]->node;
[a732ff]2567 TesselPointList::iterator Runner;
2568 TesselPointList::iterator Sprinter;
[013ad57]2569
2570 // fill the set of neighbours
[eda56a]2571 TesselPointSet SetOfNeighbours;
[013ad57]2572 SetOfNeighbours.insert(CandidateLine.BaseLine->endpoints[1]->node);
2573 for (TesselPointList::iterator Runner = CandidateLine.pointlist.begin(); Runner != CandidateLine.pointlist.end(); Runner++)
2574 SetOfNeighbours.insert(*Runner);
[eda56a]2575 TesselPointList *connectedClosestPoints = GetCircleOfSetOfPoints(&SetOfNeighbours, TurningPoint, CandidateLine.BaseLine->endpoints[1]->node->node);
[013ad57]2576
[1f2e46]2577 DoLog(0) && (Log() << Verbose(0) << "List of Candidates for Turning Point " << *TurningPoint << ":" << endl);
[eda56a]2578 for (TesselPointList::iterator TesselRunner = connectedClosestPoints->begin(); TesselRunner != connectedClosestPoints->end(); ++TesselRunner)
[1f2e46]2579 DoLog(0) && (Log() << Verbose(0) << " " << **TesselRunner << endl);
[a732ff]2580
2581 // go through all angle-sorted candidates (in degenerate n-nodes case we may have to add multiple triangles)
2582 Runner = connectedClosestPoints->begin();
2583 Sprinter = Runner;
[013ad57]2584 Sprinter++;
[c43766]2585 while (Sprinter != connectedClosestPoints->end()) {
[1f2e46]2586 DoLog(0) && (Log() << Verbose(0) << "Current Runner is " << *(*Runner) << " and sprinter is " << *(*Sprinter) << "." << endl);
[be2997]2587
[086db0]2588 AddTesselationPoint(TurningPoint, 0);
2589 AddTesselationPoint(*Runner, 1);
2590 AddTesselationPoint(*Sprinter, 2);
[4af89b]2591
[c43766]2592 AddCandidateTriangle(CandidateLine, Opt);
[4af89b]2593
[013ad57]2594 Runner = Sprinter;
2595 Sprinter++;
[c43766]2596 if (Sprinter != connectedClosestPoints->end()) {
2597 // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked)
[d22a30]2598 FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OptCenter); // Assume BTS contains last triangle
[1f2e46]2599 DoLog(0) && (Log() << Verbose(0) << " There are still more triangles to add." << endl);
[c43766]2600 }
2601 // pick candidates for other open lines as well
2602 FindCandidatesforOpenLines(RADIUS, LC);
2603
[086db0]2604 // check whether we add a degenerate or a normal triangle
[c43766]2605 if (CheckDegeneracy(CandidateLine, RADIUS, LC)) {
[086db0]2606 // add normal and degenerate triangles
[1f2e46]2607 DoLog(1) && (Log() << Verbose(1) << "Triangle of endpoints " << *TPS[0] << "," << *TPS[1] << " and " << *TPS[2] << " is degenerated, adding both sides." << endl);
[c43766]2608 AddCandidateTriangle(CandidateLine, OtherOpt);
2609
2610 if (Sprinter != connectedClosestPoints->end()) {
2611 // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked)
2612 FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OtherOptCenter);
2613 }
2614 // pick candidates for other open lines as well
2615 FindCandidatesforOpenLines(RADIUS, LC);
[7c2ebb]2616 }
[c43766]2617 }
2618 delete (connectedClosestPoints);
2619};
[7c2ebb]2620
[c43766]2621/** for polygons (multiple candidates for a baseline) sets internal edges to the correct next candidate.
2622 * \param *Sprinter next candidate to which internal open lines are set
2623 * \param *OptCenter OptCenter for this candidate
2624 */
2625void Tesselation::FindDegeneratedCandidatesforOpenLines(TesselPoint * const Sprinter, const Vector * const OptCenter)
2626{
2627 Info FunctionInfo(__func__);
2628
2629 pair<LineMap::iterator, LineMap::iterator> FindPair = TPS[0]->lines.equal_range(TPS[2]->node->nr);
2630 for (LineMap::const_iterator FindLine = FindPair.first; FindLine != FindPair.second; FindLine++) {
[1f2e46]2631 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl);
[c43766]2632 // If there is a line with less than two attached triangles, we don't need a new line.
2633 if (FindLine->second->triangles.size() == 1) {
2634 CandidateMap::iterator Finder = OpenLines.find(FindLine->second);
2635 if (!Finder->second->pointlist.empty())
[1f2e46]2636 DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl);
[c43766]2637 else {
[1f2e46]2638 DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate, setting to next Sprinter" << (*Sprinter) << endl);
[d22a30]2639 Finder->second->T = BTS; // is last triangle
[c43766]2640 Finder->second->pointlist.push_back(Sprinter);
2641 Finder->second->ShortestAngle = 0.;
[0d111b]2642 Finder->second->OptCenter = *OptCenter;
[c43766]2643 }
2644 }
[be2997]2645 }
[4af89b]2646};
2647
[086db0]2648/** If a given \a *triangle is degenerated, this adds both sides.
[7c2ebb]2649 * i.e. the triangle with same BoundaryPointSet's but NormalVector in opposite direction.
[086db0]2650 * Note that endpoints are stored in Tesselation::TPS
2651 * \param CandidateLine CanddiateForTesselation structure for the desired BoundaryLine
[7c2ebb]2652 * \param RADIUS radius of sphere
2653 * \param *LC pointer to LinkedCell structure
2654 */
[60612f]2655void Tesselation::AddDegeneratedTriangle(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC)
[7c2ebb]2656{
2657 Info FunctionInfo(__func__);
[086db0]2658 Vector Center;
2659 CandidateMap::const_iterator CandidateCheck = OpenLines.end();
[60612f]2660 BoundaryTriangleSet *triangle = NULL;
[086db0]2661
[60612f]2662 /// 1. Create or pick the lines for the first triangle
[1f2e46]2663 DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for first triangle ..." << endl);
[c43766]2664 for (int i = 0; i < 3; i++) {
[60612f]2665 BLS[i] = NULL;
[1f2e46]2666 DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
[c43766]2667 AddTesselationLine(&CandidateLine.OptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i);
[7c2ebb]2668 }
[086db0]2669
[60612f]2670 /// 2. create the first triangle and NormalVector and so on
[1f2e46]2671 DoLog(0) && (Log() << Verbose(0) << "INFO: Adding first triangle with center at " << CandidateLine.OptCenter << " ..." << endl);
[086db0]2672 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
2673 AddTesselationTriangle();
[60612f]2674
[086db0]2675 // create normal vector
2676 BTS->GetCenter(&Center);
[0d111b]2677 Center -= CandidateLine.OptCenter;
2678 BTS->SphereCenter = CandidateLine.OptCenter;
[086db0]2679 BTS->GetNormalVector(Center);
2680 // give some verbose output about the whole procedure
2681 if (CandidateLine.T != NULL)
[1f2e46]2682 DoLog(0) && (Log() << Verbose(0) << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl);
[086db0]2683 else
[1f2e46]2684 DoLog(0) && (Log() << Verbose(0) << "--> New starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
[086db0]2685 triangle = BTS;
2686
[60612f]2687 /// 3. Gather candidates for each new line
[1f2e46]2688 DoLog(0) && (Log() << Verbose(0) << "INFO: Adding candidates to new lines ..." << endl);
[c43766]2689 for (int i = 0; i < 3; i++) {
[1f2e46]2690 DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
[086db0]2691 CandidateCheck = OpenLines.find(BLS[i]);
2692 if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) {
2693 if (CandidateCheck->second->T == NULL)
2694 CandidateCheck->second->T = triangle;
2695 FindNextSuitableTriangle(*(CandidateCheck->second), *CandidateCheck->second->T, RADIUS, LC);
[7c2ebb]2696 }
[086db0]2697 }
[adff53]2698
[60612f]2699 /// 4. Create or pick the lines for the second triangle
[1f2e46]2700 DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for second triangle ..." << endl);
[c43766]2701 for (int i = 0; i < 3; i++) {
[1f2e46]2702 DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
[c43766]2703 AddTesselationLine(&CandidateLine.OtherOptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i);
[7c2ebb]2704 }
[086db0]2705
[60612f]2706 /// 5. create the second triangle and NormalVector and so on
[1f2e46]2707 DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangle with center at " << CandidateLine.OtherOptCenter << " ..." << endl);
[086db0]2708 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
2709 AddTesselationTriangle();
[60612f]2710
[0d111b]2711 BTS->SphereCenter = CandidateLine.OtherOptCenter;
[086db0]2712 // create normal vector in other direction
[0d111b]2713 BTS->GetNormalVector(triangle->NormalVector);
[086db0]2714 BTS->NormalVector.Scale(-1.);
2715 // give some verbose output about the whole procedure
2716 if (CandidateLine.T != NULL)
[1f2e46]2717 DoLog(0) && (Log() << Verbose(0) << "--> New degenerate triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl);
[086db0]2718 else
[1f2e46]2719 DoLog(0) && (Log() << Verbose(0) << "--> New degenerate starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
[086db0]2720
[60612f]2721 /// 6. Adding triangle to new lines
[1f2e46]2722 DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangles to new lines ..." << endl);
[c43766]2723 for (int i = 0; i < 3; i++) {
[1f2e46]2724 DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
[60612f]2725 CandidateCheck = OpenLines.find(BLS[i]);
2726 if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) {
2727 if (CandidateCheck->second->T == NULL)
2728 CandidateCheck->second->T = BTS;
2729 }
2730 }
[c43766]2731}
2732;
[7c2ebb]2733
2734/** Adds a triangle to the Tesselation structure from three given TesselPoint's.
[086db0]2735 * Note that endpoints are in Tesselation::TPS.
2736 * \param CandidateLine CandidateForTesselation structure contains other information
[c43766]2737 * \param type which opt center to add (i.e. which side) and thus which NormalVector to take
[7c2ebb]2738 */
[c43766]2739void Tesselation::AddCandidateTriangle(CandidateForTesselation &CandidateLine, enum centers type)
[7c2ebb]2740{
2741 Info FunctionInfo(__func__);
[086db0]2742 Vector Center;
[c43766]2743 Vector *OptCenter = (type == Opt) ? &CandidateLine.OptCenter : &CandidateLine.OtherOptCenter;
[7c2ebb]2744
2745 // add the lines
[c43766]2746 AddTesselationLine(OptCenter, TPS[2], TPS[0], TPS[1], 0);
2747 AddTesselationLine(OptCenter, TPS[1], TPS[0], TPS[2], 1);
2748 AddTesselationLine(OptCenter, TPS[0], TPS[1], TPS[2], 2);
[7c2ebb]2749
2750 // add the triangles
2751 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
2752 AddTesselationTriangle();
[086db0]2753
2754 // create normal vector
2755 BTS->GetCenter(&Center);
[0d111b]2756 Center.SubtractVector(*OptCenter);
2757 BTS->SphereCenter = *OptCenter;
[086db0]2758 BTS->GetNormalVector(Center);
2759
2760 // give some verbose output about the whole procedure
2761 if (CandidateLine.T != NULL)
[1f2e46]2762 DoLog(0) && (Log() << Verbose(0) << "--> New" << ((type == OtherOpt) ? " degenerate " : " ") << "triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl);
[086db0]2763 else
[1f2e46]2764 DoLog(0) && (Log() << Verbose(0) << "--> New" << ((type == OtherOpt) ? " degenerate " : " ") << "starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
[c43766]2765}
2766;
[7c2ebb]2767
[eb58a7]2768/** Checks whether the quadragon of the two triangles connect to \a *Base is convex.
2769 * We look whether the closest point on \a *Base with respect to the other baseline is outside
2770 * of the segment formed by both endpoints (concave) or not (convex).
2771 * \param *out output stream for debugging
2772 * \param *Base line to be flipped
[60cbe5]2773 * \return NULL - convex, otherwise endpoint that makes it concave
[eb58a7]2774 */
[543ce4]2775class BoundaryPointSet *Tesselation::IsConvexRectangle(class BoundaryLineSet *Base)
[eb58a7]2776{
[c43766]2777 Info FunctionInfo(__func__);
[eb58a7]2778 class BoundaryPointSet *Spot = NULL;
2779 class BoundaryLineSet *OtherBase;
[27459a]2780 Vector *ClosestPoint;
[eb58a7]2781
[c43766]2782 int m = 0;
2783 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2784 for (int j = 0; j < 3; j++) // all of their endpoints and baselines
[eb58a7]2785 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
2786 BPS[m++] = runner->second->endpoints[j];
[c43766]2787 OtherBase = new class BoundaryLineSet(BPS, -1);
[eb58a7]2788
[1f2e46]2789 DoLog(1) && (Log() << Verbose(1) << "INFO: Current base line is " << *Base << "." << endl);
2790 DoLog(1) && (Log() << Verbose(1) << "INFO: Other base line is " << *OtherBase << "." << endl);
[eb58a7]2791
2792 // get the closest point on each line to the other line
[543ce4]2793 ClosestPoint = GetClosestPointBetweenLine(Base, OtherBase);
[eb58a7]2794
2795 // delete the temporary other base line
[c43766]2796 delete (OtherBase);
[eb58a7]2797
2798 // get the distance vector from Base line to OtherBase line
[27459a]2799 Vector DistanceToIntersection[2], BaseLine;
2800 double distance[2];
[1f591b]2801 BaseLine = (*Base->endpoints[1]->node->node) - (*Base->endpoints[0]->node->node);
[c43766]2802 for (int i = 0; i < 2; i++) {
[1f591b]2803 DistanceToIntersection[i] = (*ClosestPoint) - (*Base->endpoints[i]->node->node);
2804 distance[i] = BaseLine.ScalarProduct(DistanceToIntersection[i]);
[eb58a7]2805 }
[c43766]2806 delete (ClosestPoint);
2807 if ((distance[0] * distance[1]) > 0) { // have same sign?
[1f2e46]2808 DoLog(1) && (Log() << Verbose(1) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1] << ". " << *Base << "' rectangle is concave." << endl);
[27459a]2809 if (distance[0] < distance[1]) {
2810 Spot = Base->endpoints[0];
2811 } else {
2812 Spot = Base->endpoints[1];
2813 }
[eb58a7]2814 return Spot;
[c43766]2815 } else { // different sign, i.e. we are in between
[1f2e46]2816 DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl);
[eb58a7]2817 return NULL;
2818 }
2819
[c43766]2820}
2821;
[eb58a7]2822
[a9b2a0a]2823void Tesselation::PrintAllBoundaryPoints(ofstream *out) const
[27459a]2824{
[c43766]2825 Info FunctionInfo(__func__);
[27459a]2826 // print all lines
[1f2e46]2827 DoLog(0) && (Log() << Verbose(0) << "Printing all boundary points for debugging:" << endl);
[c43766]2828 for (PointMap::const_iterator PointRunner = PointsOnBoundary.begin(); PointRunner != PointsOnBoundary.end(); PointRunner++)
[1f2e46]2829 DoLog(0) && (Log() << Verbose(0) << *(PointRunner->second) << endl);
[c43766]2830}
2831;
[27459a]2832
[a9b2a0a]2833void Tesselation::PrintAllBoundaryLines(ofstream *out) const
[27459a]2834{
[c43766]2835 Info FunctionInfo(__func__);
[27459a]2836 // print all lines
[1f2e46]2837 DoLog(0) && (Log() << Verbose(0) << "Printing all boundary lines for debugging:" << endl);
[a9b2a0a]2838 for (LineMap::const_iterator LineRunner = LinesOnBoundary.begin(); LineRunner != LinesOnBoundary.end(); LineRunner++)
[1f2e46]2839 DoLog(0) && (Log() << Verbose(0) << *(LineRunner->second) << endl);
[c43766]2840}
2841;
[27459a]2842
[a9b2a0a]2843void Tesselation::PrintAllBoundaryTriangles(ofstream *out) const
[27459a]2844{
[c43766]2845 Info FunctionInfo(__func__);
[27459a]2846 // print all triangles
[1f2e46]2847 DoLog(0) && (Log() << Verbose(0) << "Printing all boundary triangles for debugging:" << endl);
[a9b2a0a]2848 for (TriangleMap::const_iterator TriangleRunner = TrianglesOnBoundary.begin(); TriangleRunner != TrianglesOnBoundary.end(); TriangleRunner++)
[1f2e46]2849 DoLog(0) && (Log() << Verbose(0) << *(TriangleRunner->second) << endl);
[c43766]2850}
2851;
[834ff3]2852
[eb58a7]2853/** For a given boundary line \a *Base and its two triangles, picks the central baseline that is "higher".
[834ff3]2854 * \param *out output stream for debugging
[eb58a7]2855 * \param *Base line to be flipped
[60cbe5]2856 * \return volume change due to flipping (0 - then no flipped occured)
[834ff3]2857 */
[543ce4]2858double Tesselation::PickFarthestofTwoBaselines(class BoundaryLineSet *Base)
[834ff3]2859{
[c43766]2860 Info FunctionInfo(__func__);
[eb58a7]2861 class BoundaryLineSet *OtherBase;
2862 Vector *ClosestPoint[2];
[60cbe5]2863 double volume;
[eb58a7]2864
[c43766]2865 int m = 0;
2866 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2867 for (int j = 0; j < 3; j++) // all of their endpoints and baselines
[eb58a7]2868 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
2869 BPS[m++] = runner->second->endpoints[j];
[c43766]2870 OtherBase = new class BoundaryLineSet(BPS, -1);
[eeefb3]2871
[1f2e46]2872 DoLog(0) && (Log() << Verbose(0) << "INFO: Current base line is " << *Base << "." << endl);
2873 DoLog(0) && (Log() << Verbose(0) << "INFO: Other base line is " << *OtherBase << "." << endl);
[eeefb3]2874
[eb58a7]2875 // get the closest point on each line to the other line
[543ce4]2876 ClosestPoint[0] = GetClosestPointBetweenLine(Base, OtherBase);
2877 ClosestPoint[1] = GetClosestPointBetweenLine(OtherBase, Base);
[eb58a7]2878
2879 // get the distance vector from Base line to OtherBase line
[1f591b]2880 Vector Distance = (*ClosestPoint[1]) - (*ClosestPoint[0]);
[eb58a7]2881
[60cbe5]2882 // calculate volume
[6d7651]2883 volume = CalculateVolumeofGeneralTetraeder(*Base->endpoints[1]->node->node, *OtherBase->endpoints[0]->node->node, *OtherBase->endpoints[1]->node->node, *Base->endpoints[0]->node->node);
[60cbe5]2884
[27459a]2885 // delete the temporary other base line and the closest points
[c43766]2886 delete (ClosestPoint[0]);
2887 delete (ClosestPoint[1]);
2888 delete (OtherBase);
[eb58a7]2889
2890 if (Distance.NormSquared() < MYEPSILON) { // check for intersection
[1f2e46]2891 DoLog(0) && (Log() << Verbose(0) << "REJECT: Both lines have an intersection: Nothing to do." << endl);
[eb58a7]2892 return false;
2893 } else { // check for sign against BaseLineNormal
2894 Vector BaseLineNormal;
[0cf171]2895 BaseLineNormal.Zero();
2896 if (Base->triangles.size() < 2) {
[c43766]2897 DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl);
[60cbe5]2898 return 0.;
[0cf171]2899 }
2900 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
[0d111b]2901 DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl);
[1f591b]2902 BaseLineNormal += (runner->second->NormalVector);
[0cf171]2903 }
[c43766]2904 BaseLineNormal.Scale(1. / 2.);
[834ff3]2905
[1f591b]2906 if (Distance.ScalarProduct(BaseLineNormal) > MYEPSILON) { // Distance points outwards, hence OtherBase higher than Base -> flip
[1f2e46]2907 DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl);
[60cbe5]2908 // calculate volume summand as a general tetraeder
2909 return volume;
[c43766]2910 } else { // Base higher than OtherBase -> do nothing
[1f2e46]2911 DoLog(0) && (Log() << Verbose(0) << "REJECT: Base line is higher: Nothing to do." << endl);
[60cbe5]2912 return 0.;
[eb58a7]2913 }
2914 }
[c43766]2915}
2916;
[834ff3]2917
[eb58a7]2918/** For a given baseline and its two connected triangles, flips the baseline.
2919 * I.e. we create the new baseline between the other two endpoints of these four
2920 * endpoints and reconstruct the two triangles accordingly.
2921 * \param *out output stream for debugging
2922 * \param *Base line to be flipped
[60cbe5]2923 * \return pointer to allocated new baseline - flipping successful, NULL - something went awry
[eb58a7]2924 */
[543ce4]2925class BoundaryLineSet * Tesselation::FlipBaseline(class BoundaryLineSet *Base)
[eb58a7]2926{
[c43766]2927 Info FunctionInfo(__func__);
[eb58a7]2928 class BoundaryLineSet *OldLines[4], *NewLine;
2929 class BoundaryPointSet *OldPoints[2];
2930 Vector BaseLineNormal;
2931 int OldTriangleNrs[2], OldBaseLineNr;
[c43766]2932 int i, m;
[eb58a7]2933
2934 // calculate NormalVector for later use
2935 BaseLineNormal.Zero();
2936 if (Base->triangles.size() < 2) {
[c43766]2937 DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl);
[60cbe5]2938 return NULL;
[eb58a7]2939 }
2940 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
[1f2e46]2941 DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl);
[1f591b]2942 BaseLineNormal += (runner->second->NormalVector);
[eb58a7]2943 }
[c43766]2944 BaseLineNormal.Scale(-1. / 2.); // has to point inside for BoundaryTriangleSet::GetNormalVector()
[eb58a7]2945
2946 // get the two triangles
2947 // gather four endpoints and four lines
[c43766]2948 for (int j = 0; j < 4; j++)
[eb58a7]2949 OldLines[j] = NULL;
[c43766]2950 for (int j = 0; j < 2; j++)
[eb58a7]2951 OldPoints[j] = NULL;
[c43766]2952 i = 0;
2953 m = 0;
[1f2e46]2954 DoLog(0) && (Log() << Verbose(0) << "The four old lines are: ");
[c43766]2955 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2956 for (int j = 0; j < 3; j++) // all of their endpoints and baselines
[eb58a7]2957 if (runner->second->lines[j] != Base) { // pick not the central baseline
2958 OldLines[i++] = runner->second->lines[j];
[1f2e46]2959 DoLog(0) && (Log() << Verbose(0) << *runner->second->lines[j] << "\t");
[834ff3]2960 }
[1f2e46]2961 DoLog(0) && (Log() << Verbose(0) << endl);
2962 DoLog(0) && (Log() << Verbose(0) << "The two old points are: ");
[c43766]2963 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2964 for (int j = 0; j < 3; j++) // all of their endpoints and baselines
[eb58a7]2965 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) { // and neither of its endpoints
2966 OldPoints[m++] = runner->second->endpoints[j];
[1f2e46]2967 DoLog(0) && (Log() << Verbose(0) << *runner->second->endpoints[j] << "\t");
[eb58a7]2968 }
[1f2e46]2969 DoLog(0) && (Log() << Verbose(0) << endl);
[eb58a7]2970
2971 // check whether everything is in place to create new lines and triangles
[c43766]2972 if (i < 4) {
2973 DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl);
[60cbe5]2974 return NULL;
[eb58a7]2975 }
[c43766]2976 for (int j = 0; j < 4; j++)
[eb58a7]2977 if (OldLines[j] == NULL) {
[c43766]2978 DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl);
[60cbe5]2979 return NULL;
[eb58a7]2980 }
[c43766]2981 for (int j = 0; j < 2; j++)
[eb58a7]2982 if (OldPoints[j] == NULL) {
[c43766]2983 DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough endpoints!" << endl);
[60cbe5]2984 return NULL;
[834ff3]2985 }
[eb58a7]2986
2987 // remove triangles and baseline removes itself
[1f2e46]2988 DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting baseline " << *Base << " from global list." << endl);
[eb58a7]2989 OldBaseLineNr = Base->Nr;
[c43766]2990 m = 0;
2991 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
[1f2e46]2992 DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting triangle " << *(runner->second) << "." << endl);
[eb58a7]2993 OldTriangleNrs[m++] = runner->second->Nr;
2994 RemoveTesselationTriangle(runner->second);
2995 }
2996
2997 // construct new baseline (with same number as old one)
2998 BPS[0] = OldPoints[0];
2999 BPS[1] = OldPoints[1];
3000 NewLine = new class BoundaryLineSet(BPS, OldBaseLineNr);
3001 LinesOnBoundary.insert(LinePair(OldBaseLineNr, NewLine)); // no need for check for unique insertion as NewLine is definitely a new one
[1f2e46]3002 DoLog(0) && (Log() << Verbose(0) << "INFO: Created new baseline " << *NewLine << "." << endl);
[eb58a7]3003
3004 // construct new triangles with flipped baseline
[c43766]3005 i = -1;
[eb58a7]3006 if (OldLines[0]->IsConnectedTo(OldLines[2]))
[c43766]3007 i = 2;
[eb58a7]3008 if (OldLines[0]->IsConnectedTo(OldLines[3]))
[c43766]3009 i = 3;
3010 if (i != -1) {
[eb58a7]3011 BLS[0] = OldLines[0];
3012 BLS[1] = OldLines[i];
3013 BLS[2] = NewLine;
3014 BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[0]);
3015 BTS->GetNormalVector(BaseLineNormal);
[daf956]3016 AddTesselationTriangle(OldTriangleNrs[0]);
[1f2e46]3017 DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl);
[eb58a7]3018
[c43766]3019 BLS[0] = (i == 2 ? OldLines[3] : OldLines[2]);
[eb58a7]3020 BLS[1] = OldLines[1];
3021 BLS[2] = NewLine;
3022 BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[1]);
3023 BTS->GetNormalVector(BaseLineNormal);
[daf956]3024 AddTesselationTriangle(OldTriangleNrs[1]);
[1f2e46]3025 DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl);
[eb58a7]3026 } else {
[c43766]3027 DoeLog(0) && (eLog() << Verbose(0) << "The four old lines do not connect, something's utterly wrong here!" << endl);
[60cbe5]3028 return NULL;
[834ff3]3029 }
[eb58a7]3030
[60cbe5]3031 return NewLine;
[c43766]3032}
3033;
[eb58a7]3034
[834ff3]3035/** Finds the second point of starting triangle.
3036 * \param *a first node
3037 * \param Oben vector indicating the outside
[48cd93]3038 * \param OptCandidate reference to recommended candidate on return
[834ff3]3039 * \param Storage[3] array storing angles and other candidate information
3040 * \param RADIUS radius of virtual sphere
[eeefb3]3041 * \param *LC LinkedCell structure with neighbouring points
[834ff3]3042 */
[a9b2a0a]3043void Tesselation::FindSecondPointForTesselation(TesselPoint* a, Vector Oben, TesselPoint*& OptCandidate, double Storage[3], double RADIUS, const LinkedCell *LC)
[834ff3]3044{
[c43766]3045 Info FunctionInfo(__func__);
[834ff3]3046 Vector AngleCheck;
[60cbe5]3047 class TesselPoint* Candidate = NULL;
[a9b2a0a]3048 double norm = -1.;
3049 double angle = 0.;
3050 int N[NDIM];
3051 int Nlower[NDIM];
3052 int Nupper[NDIM];
[834ff3]3053
[c43766]3054 if (LC->SetIndexToNode(a)) { // get cell for the starting point
3055 for (int i = 0; i < NDIM; i++) // store indices of this cell
[834ff3]3056 N[i] = LC->n[i];
3057 } else {
[c43766]3058 DoeLog(1) && (eLog() << Verbose(1) << "Point " << *a << " is not found in cell " << LC->index << "." << endl);
[834ff3]3059 return;
3060 }
[eeefb3]3061 // then go through the current and all neighbouring cells and check the contained points for possible candidates
[c43766]3062 for (int i = 0; i < NDIM; i++) {
3063 Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0;
3064 Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1;
[834ff3]3065 }
[1f2e46]3066 DoLog(0) && (Log() << Verbose(0) << "LC Intervals from [" << N[0] << "<->" << LC->N[0] << ", " << N[1] << "<->" << LC->N[1] << ", " << N[2] << "<->" << LC->N[2] << "] :" << " [" << Nlower[0] << "," << Nupper[0] << "], " << " [" << Nlower[1] << "," << Nupper[1] << "], " << " [" << Nlower[2] << "," << Nupper[2] << "], " << endl);
[834ff3]3067
3068 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
3069 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
3070 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
[6dd8d3]3071 const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
[be2997]3072 //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
[834ff3]3073 if (List != NULL) {
[6dd8d3]3074 for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[834ff3]3075 Candidate = (*Runner);
3076 // check if we only have one unique point yet ...
3077 if (a != Candidate) {
3078 // Calculate center of the circle with radius RADIUS through points a and Candidate
[48cd93]3079 Vector OrthogonalizedOben, aCandidate, Center;
[834ff3]3080 double distance, scaleFactor;
3081
[1f591b]3082 OrthogonalizedOben = Oben;
3083 aCandidate = (*a->node) - (*Candidate->node);
3084 OrthogonalizedOben.ProjectOntoPlane(aCandidate);
[834ff3]3085 OrthogonalizedOben.Normalize();
[48cd93]3086 distance = 0.5 * aCandidate.Norm();
[834ff3]3087 scaleFactor = sqrt(((RADIUS * RADIUS) - (distance * distance)));
3088 OrthogonalizedOben.Scale(scaleFactor);
3089
[1f591b]3090 Center = 0.5 * ((*Candidate->node) + (*a->node));
3091 Center += OrthogonalizedOben;
[834ff3]3092
[1f591b]3093 AngleCheck = Center - (*a->node);
[48cd93]3094 norm = aCandidate.Norm();
[834ff3]3095 // second point shall have smallest angle with respect to Oben vector
[c43766]3096 if (norm < RADIUS * 2.) {
[1f591b]3097 angle = AngleCheck.Angle(Oben);
[834ff3]3098 if (angle < Storage[0]) {
[be2997]3099 //Log() << Verbose(1) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]);
[1f2e46]3100 DoLog(1) && (Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n");
[48cd93]3101 OptCandidate = Candidate;
[834ff3]3102 Storage[0] = angle;
[be2997]3103 //Log() << Verbose(1) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[2]);
[834ff3]3104 } else {
[be2997]3105 //Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Looses with angle " << angle << " to a better candidate " << *OptCandidate << endl;
[834ff3]3106 }
3107 } else {
[be2997]3108 //Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Refused due to Radius " << norm << endl;
[834ff3]3109 }
3110 } else {
[be2997]3111 //Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Candidate is equal to first endpoint." << *a << "." << endl;
[834ff3]3112 }
3113 }
3114 } else {
[1f2e46]3115 DoLog(0) && (Log() << Verbose(0) << "Linked cell list is empty." << endl);
[834ff3]3116 }
3117 }
[c43766]3118}
3119;
[834ff3]3120
3121/** This recursive function finds a third point, to form a triangle with two given ones.
3122 * Note that this function is for the starting triangle.
3123 * The idea is as follows: A sphere with fixed radius is (almost) uniquely defined in space by three points
3124 * that sit on its boundary. Hence, when two points are given and we look for the (next) third point, then
3125 * the center of the sphere is still fixed up to a single parameter. The band of possible values
3126 * describes a circle in 3D-space. The old center of the sphere for the current base triangle gives
3127 * us the "null" on this circle, the new center of the candidate point will be some way along this
3128 * circle. The shorter the way the better is the candidate. Note that the direction is clearly given
3129 * by the normal vector of the base triangle that always points outwards by construction.
3130 * Hence, we construct a Center of this circle which sits right in the middle of the current base line.
3131 * We construct the normal vector that defines the plane this circle lies in, it is just in the
3132 * direction of the baseline. And finally, we need the radius of the circle, which is given by the rest
3133 * with respect to the length of the baseline and the sphere's fixed \a RADIUS.
3134 * Note that there is one difficulty: The circumcircle is uniquely defined, but for the circumsphere's center
3135 * there are two possibilities which becomes clear from the construction as seen below. Hence, we must check
3136 * both.
3137 * Note also that the acos() function is not unique on [0, 2.*M_PI). Hence, we need an additional check
3138 * to decide for one of the two possible angles. Therefore we need a SearchDirection and to make this check
3139 * sensible we need OldSphereCenter to be orthogonal to it. Either we construct SearchDirection orthogonal
3140 * right away, or -- what we do here -- we rotate the relative sphere centers such that this orthogonality
3141 * holds. Then, the normalized projection onto the SearchDirection is either +1 or -1 and thus states whether
3142 * the angle is uniquely in either (0,M_PI] or [M_PI, 2.*M_PI).
[48cd93]3143 * @param NormalVector normal direction of the base triangle (here the unit axis vector, \sa FindStartingTriangle())
[834ff3]3144 * @param SearchDirection general direction where to search for the next point, relative to center of BaseLine
3145 * @param OldSphereCenter center of sphere for base triangle, relative to center of BaseLine, giving null angle for the parameter circle
[be2997]3146 * @param CandidateLine CandidateForTesselation with the current base line and list of candidates and ShortestAngle
[a732ff]3147 * @param ThirdPoint third point to avoid in search
[834ff3]3148 * @param RADIUS radius of sphere
[eeefb3]3149 * @param *LC LinkedCell structure with neighbouring points
[834ff3]3150 */
[c43766]3151void Tesselation::FindThirdPointForTesselation(const Vector &NormalVector, const Vector &SearchDirection, const Vector &OldSphereCenter, CandidateForTesselation &CandidateLine, const class BoundaryPointSet * const ThirdPoint, const double RADIUS, const LinkedCell *LC) const
[834ff3]3152{
[c43766]3153 Info FunctionInfo(__func__);
3154 Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
[834ff3]3155 Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
3156 Vector SphereCenter;
[c43766]3157 Vector NewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility
3158 Vector OtherNewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility
3159 Vector NewNormalVector; // normal vector of the Candidate's triangle
[834ff3]3160 Vector helper, OptCandidateCenter, OtherOptCandidateCenter;
[65f1dba]3161 Vector RelativeOldSphereCenter;
3162 Vector NewPlaneCenter;
[834ff3]3163 double CircleRadius; // radius of this circle
3164 double radius;
[65f1dba]3165 double otherradius;
[834ff3]3166 double alpha, Otheralpha; // angles (i.e. parameter for the circle).
3167 int N[NDIM], Nlower[NDIM], Nupper[NDIM];
3168 TesselPoint *Candidate = NULL;
3169
[1f2e46]3170 DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl);
[834ff3]3171
[a732ff]3172 // copy old center
[0d111b]3173 CandidateLine.OldCenter = OldSphereCenter;
[a732ff]3174 CandidateLine.ThirdPoint = ThirdPoint;
3175 CandidateLine.pointlist.clear();
[834ff3]3176
3177 // construct center of circle
[1f591b]3178 CircleCenter = 0.5 * ((*CandidateLine.BaseLine->endpoints[0]->node->node) +
3179 (*CandidateLine.BaseLine->endpoints[1]->node->node));
[834ff3]3180
3181 // construct normal vector of circle
[1f591b]3182 CirclePlaneNormal = (*CandidateLine.BaseLine->endpoints[0]->node->node) -
3183 (*CandidateLine.BaseLine->endpoints[1]->node->node);
[834ff3]3184
[1f591b]3185 RelativeOldSphereCenter = OldSphereCenter - CircleCenter;
[65f1dba]3186
[a732ff]3187 // calculate squared radius TesselPoint *ThirdPoint,f circle
[c43766]3188 radius = CirclePlaneNormal.NormSquared() / 4.;
3189 if (radius < RADIUS * RADIUS) {
3190 CircleRadius = RADIUS * RADIUS - radius;
[834ff3]3191 CirclePlaneNormal.Normalize();
[1f2e46]3192 DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
[834ff3]3193
3194 // test whether old center is on the band's plane
[1f591b]3195 if (fabs(RelativeOldSphereCenter.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) {
[0d111b]3196 DoeLog(1) && (eLog() << Verbose(1) << "Something's very wrong here: RelativeOldSphereCenter is not on the band's plane as desired by " << fabs(RelativeOldSphereCenter.ScalarProduct(CirclePlaneNormal)) << "!" << endl);
[1f591b]3197 RelativeOldSphereCenter.ProjectOntoPlane(CirclePlaneNormal);
[834ff3]3198 }
[65f1dba]3199 radius = RelativeOldSphereCenter.NormSquared();
[834ff3]3200 if (fabs(radius - CircleRadius) < HULLEPSILON) {
[1f2e46]3201 DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeOldSphereCenter is at " << RelativeOldSphereCenter << "." << endl);
[834ff3]3202
3203 // check SearchDirection
[1f2e46]3204 DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
[0d111b]3205 if (fabs(RelativeOldSphereCenter.ScalarProduct(SearchDirection)) > HULLEPSILON) { // rotated the wrong way!
[c43766]3206 DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl);
[834ff3]3207 }
3208
[eeefb3]3209 // get cell for the starting point
[834ff3]3210 if (LC->SetIndexToVector(&CircleCenter)) {
[c43766]3211 for (int i = 0; i < NDIM; i++) // store indices of this cell
3212 N[i] = LC->n[i];
[be2997]3213 //Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
[834ff3]3214 } else {
[c43766]3215 DoeLog(1) && (eLog() << Verbose(1) << "Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl);
[834ff3]3216 return;
3217 }
[eeefb3]3218 // then go through the current and all neighbouring cells and check the contained points for possible candidates
[be2997]3219 //Log() << Verbose(1) << "LC Intervals:";
[c43766]3220 for (int i = 0; i < NDIM; i++) {
3221 Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0;
3222 Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1;
[543ce4]3223 //Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ";
[834ff3]3224 }
[543ce4]3225 //Log() << Verbose(0) << endl;
[834ff3]3226 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
3227 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
3228 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
[6dd8d3]3229 const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
[be2997]3230 //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
[834ff3]3231 if (List != NULL) {
[6dd8d3]3232 for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[834ff3]3233 Candidate = (*Runner);
3234
3235 // check for three unique points
[1f2e46]3236 DoLog(2) && (Log() << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " for BaseLine " << *CandidateLine.BaseLine << " with OldSphereCenter " << OldSphereCenter << "." << endl);
[c43766]3237 if ((Candidate != CandidateLine.BaseLine->endpoints[0]->node) && (Candidate != CandidateLine.BaseLine->endpoints[1]->node)) {
[834ff3]3238
[65f1dba]3239 // find center on the plane
3240 GetCenterofCircumcircle(&NewPlaneCenter, *CandidateLine.BaseLine->endpoints[0]->node->node, *CandidateLine.BaseLine->endpoints[1]->node->node, *Candidate->node);
[1f2e46]3241 DoLog(1) && (Log() << Verbose(1) << "INFO: NewPlaneCenter is " << NewPlaneCenter << "." << endl);
[834ff3]3242
[71910a]3243 try {
3244 NewNormalVector = Plane(*(CandidateLine.BaseLine->endpoints[0]->node->node),
[0d111b]3245 *(CandidateLine.BaseLine->endpoints[1]->node->node),
3246 *(Candidate->node)).getNormal();
[1f2e46]3247 DoLog(1) && (Log() << Verbose(1) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl);
[1f591b]3248 radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(NewPlaneCenter);
[1f2e46]3249 DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
3250 DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
3251 DoLog(1) && (Log() << Verbose(1) << "INFO: Radius of CircumCenterCircle is " << radius << "." << endl);
[c43766]3252 if (radius < RADIUS * RADIUS) {
[1f591b]3253 otherradius = CandidateLine.BaseLine->endpoints[1]->node->node->DistanceSquared(NewPlaneCenter);
[fda112]3254 if (fabs(radius - otherradius) < HULLEPSILON) {
3255 // construct both new centers
[0d111b]3256 NewSphereCenter = NewPlaneCenter;
3257 OtherNewSphereCenter= NewPlaneCenter;
3258 helper = NewNormalVector;
[fda112]3259 helper.Scale(sqrt(RADIUS * RADIUS - radius));
3260 DoLog(2) && (Log() << Verbose(2) << "INFO: Distance of NewPlaneCenter " << NewPlaneCenter << " to either NewSphereCenter is " << helper.Norm() << " of vector " << helper << " with sphere radius " << RADIUS << "." << endl);
[0d111b]3261 NewSphereCenter += helper;
[fda112]3262 DoLog(2) && (Log() << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl);
3263 // OtherNewSphereCenter is created by the same vector just in the other direction
3264 helper.Scale(-1.);
[0d111b]3265 OtherNewSphereCenter += helper;
[fda112]3266 DoLog(2) && (Log() << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl);
3267 alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
3268 Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
[af20f3]3269 if ((ThirdPoint != NULL) && (Candidate == ThirdPoint->node)) { // in that case only the other circlecenter is valid
[0d111b]3270 if (OldSphereCenter.DistanceSquared(NewSphereCenter) < OldSphereCenter.DistanceSquared(OtherNewSphereCenter))
[af20f3]3271 alpha = Otheralpha;
3272 } else
3273 alpha = min(alpha, Otheralpha);
[fda112]3274 // if there is a better candidate, drop the current list and add the new candidate
3275 // otherwise ignore the new candidate and keep the list
3276 if (CandidateLine.ShortestAngle > (alpha - HULLEPSILON)) {
3277 if (fabs(alpha - Otheralpha) > MYEPSILON) {
[0d111b]3278 CandidateLine.OptCenter = NewSphereCenter;
3279 CandidateLine.OtherOptCenter = OtherNewSphereCenter;
[fda112]3280 } else {
[0d111b]3281 CandidateLine.OptCenter = OtherNewSphereCenter;
3282 CandidateLine.OtherOptCenter = NewSphereCenter;
[fda112]3283 }
3284 // if there is an equal candidate, add it to the list without clearing the list
3285 if ((CandidateLine.ShortestAngle - HULLEPSILON) < alpha) {
3286 CandidateLine.pointlist.push_back(Candidate);
3287 DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found an equally good candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl);
3288 } else {
3289 // remove all candidates from the list and then the list itself
3290 CandidateLine.pointlist.clear();
3291 CandidateLine.pointlist.push_back(Candidate);
3292 DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found a better candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl);
3293 }
3294 CandidateLine.ShortestAngle = alpha;
3295 DoLog(0) && (Log() << Verbose(0) << "INFO: There are " << CandidateLine.pointlist.size() << " candidates in the list now." << endl);
[834ff3]3296 } else {
[fda112]3297 if ((Candidate != NULL) && (CandidateLine.pointlist.begin() != CandidateLine.pointlist.end())) {
3298 DoLog(1) && (Log() << Verbose(1) << "REJECT: Old candidate " << *(*CandidateLine.pointlist.begin()) << " with " << CandidateLine.ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl);
3299 } else {
3300 DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl);
3301 }
[834ff3]3302 }
3303 } else {
[fda112]3304 DoLog(1) && (Log() << Verbose(1) << "REJECT: Distance to center of circumcircle is not the same from each corner of the triangle: " << fabs(radius - otherradius) << endl);
[834ff3]3305 }
3306 } else {
[1f2e46]3307 DoLog(1) && (Log() << Verbose(1) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl);
[834ff3]3308 }
[71910a]3309 }
3310 catch (LinearDependenceException &excp){
3311 Log() << Verbose(1) << excp;
[be2997]3312 Log() << Verbose(1) << "REJECT: Three points from " << *CandidateLine.BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl;
[834ff3]3313 }
3314 } else {
[a732ff]3315 if (ThirdPoint != NULL) {
[1f2e46]3316 DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " and " << *ThirdPoint << " contains Candidate " << *Candidate << "." << endl);
[834ff3]3317 } else {
[1f2e46]3318 DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " contains Candidate " << *Candidate << "." << endl);
[834ff3]3319 }
3320 }
3321 }
3322 }
3323 }
3324 } else {
[c43766]3325 DoeLog(1) && (eLog() << Verbose(1) << "The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl);
[834ff3]3326 }
3327 } else {
[a732ff]3328 if (ThirdPoint != NULL)
[1f2e46]3329 DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and third node " << *ThirdPoint << " is too big!" << endl);
[834ff3]3330 else
[1f2e46]3331 DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " is too big!" << endl);
[834ff3]3332 }
3333
[6dd8d3]3334 DoLog(1) && (Log() << Verbose(1) << "INFO: Sorting candidate list ..." << endl);
[be2997]3335 if (CandidateLine.pointlist.size() > 1) {
3336 CandidateLine.pointlist.unique();
3337 CandidateLine.pointlist.sort(); //SortCandidates);
[834ff3]3338 }
[c43766]3339
3340 if ((!CandidateLine.pointlist.empty()) && (!CandidateLine.CheckValidity(RADIUS, LC))) {
3341 DoeLog(0) && (eLog() << Verbose(0) << "There were other points contained in the rolling sphere as well!" << endl);
3342 performCriticalExit();
3343 }
3344}
3345;
[834ff3]3346
3347/** Finds the endpoint two lines are sharing.
3348 * \param *line1 first line
3349 * \param *line2 second line
3350 * \return point which is shared or NULL if none
3351 */
[a9b2a0a]3352class BoundaryPointSet *Tesselation::GetCommonEndpoint(const BoundaryLineSet * line1, const BoundaryLineSet * line2) const
[834ff3]3353{
[c43766]3354 Info FunctionInfo(__func__);
[a9b2a0a]3355 const BoundaryLineSet * lines[2] = { line1, line2 };
[834ff3]3356 class BoundaryPointSet *node = NULL;
[eda56a]3357 PointMap OrderMap;
3358 PointTestPair OrderTest;
[834ff3]3359 for (int i = 0; i < 2; i++)
3360 // for both lines
[c43766]3361 for (int j = 0; j < 2; j++) { // for both endpoints
3362 OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
3363 if (!OrderTest.second) { // if insertion fails, we have common endpoint
3364 node = OrderTest.first->second;
[1f2e46]3365 DoLog(1) && (Log() << Verbose(1) << "Common endpoint of lines " << *line1 << " and " << *line2 << " is: " << *node << "." << endl);
[c43766]3366 j = 2;
3367 i = 2;
3368 break;
[834ff3]3369 }
[c43766]3370 }
[834ff3]3371 return node;
[c43766]3372}
3373;
[834ff3]3374
[eda56a]3375/** Finds the boundary points that are closest to a given Vector \a *x.
[eeefb3]3376 * \param *out output stream for debugging
3377 * \param *x Vector to look from
[eda56a]3378 * \return map of BoundaryPointSet of closest points sorted by squared distance or NULL.
[eeefb3]3379 */
[a1acc5]3380DistanceToPointMap * Tesselation::FindClosestBoundaryPointsToVector(const Vector *x, const LinkedCell* LC) const
[eeefb3]3381{
[eda56a]3382 Info FunctionInfo(__func__);
[ff4611]3383 PointMap::const_iterator FindPoint;
3384 int N[NDIM], Nlower[NDIM], Nupper[NDIM];
[eeefb3]3385
3386 if (LinesOnBoundary.empty()) {
[c43766]3387 DoeLog(1) && (eLog() << Verbose(1) << "There is no tesselation structure to compare the point with, please create one first." << endl);
[eeefb3]3388 return NULL;
3389 }
[ff4611]3390
3391 // gather all points close to the desired one
3392 LC->SetIndexToVector(x); // ignore status as we calculate bounds below sensibly
[c43766]3393 for (int i = 0; i < NDIM; i++) // store indices of this cell
[ff4611]3394 N[i] = LC->n[i];
[1f2e46]3395 DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
[a1acc5]3396 DistanceToPointMap * points = new DistanceToPointMap;
[ff4611]3397 LC->GetNeighbourBounds(Nlower, Nupper);
3398 //Log() << Verbose(1) << endl;
3399 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
3400 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
3401 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
[6dd8d3]3402 const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
[ff4611]3403 //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
3404 if (List != NULL) {
[6dd8d3]3405 for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[ff4611]3406 FindPoint = PointsOnBoundary.find((*Runner)->nr);
[a1acc5]3407 if (FindPoint != PointsOnBoundary.end()) {
[0d111b]3408 points->insert(DistanceToPointPair(FindPoint->second->node->node->DistanceSquared(*x), FindPoint->second));
[1f2e46]3409 DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *FindPoint->second << " into the list." << endl);
[a1acc5]3410 }
[ff4611]3411 }
3412 } else {
[c43766]3413 DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
[20895b]3414 }
[60cbe5]3415 }
[eeefb3]3416
[ff4611]3417 // check whether we found some points
[eda56a]3418 if (points->empty()) {
[c43766]3419 DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
3420 delete (points);
[eda56a]3421 return NULL;
3422 }
3423 return points;
[c43766]3424}
3425;
[eda56a]3426
3427/** Finds the boundary line that is closest to a given Vector \a *x.
3428 * \param *out output stream for debugging
3429 * \param *x Vector to look from
3430 * \return closest BoundaryLineSet or NULL in degenerate case.
3431 */
3432BoundaryLineSet * Tesselation::FindClosestBoundaryLineToVector(const Vector *x, const LinkedCell* LC) const
3433{
3434 Info FunctionInfo(__func__);
3435 // get closest points
[c43766]3436 DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC);
[eda56a]3437 if (points == NULL) {
[c43766]3438 DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
[ff4611]3439 return NULL;
3440 }
[eeefb3]3441
[ff4611]3442 // for each point, check its lines, remember closest
[1f2e46]3443 DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryLine to " << *x << " ... " << endl);
[ff4611]3444 BoundaryLineSet *ClosestLine = NULL;
3445 double MinDistance = -1.;
3446 Vector helper;
3447 Vector Center;
3448 Vector BaseLine;
[a1acc5]3449 for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++) {
[eda56a]3450 for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) {
[ff4611]3451 // calculate closest point on line to desired point
[1f591b]3452 helper = 0.5 * ((*(LineRunner->second)->endpoints[0]->node->node) +
3453 (*(LineRunner->second)->endpoints[1]->node->node));
3454 Center = (*x) - helper;
3455 BaseLine = (*(LineRunner->second)->endpoints[0]->node->node) -
3456 (*(LineRunner->second)->endpoints[1]->node->node);
3457 Center.ProjectOntoPlane(BaseLine);
[ff4611]3458 const double distance = Center.NormSquared();
3459 if ((ClosestLine == NULL) || (distance < MinDistance)) {
3460 // additionally calculate intersection on line (whether it's on the line section or not)
[1f591b]3461 helper = (*x) - (*(LineRunner->second)->endpoints[0]->node->node) - Center;
3462 const double lengthA = helper.ScalarProduct(BaseLine);
3463 helper = (*x) - (*(LineRunner->second)->endpoints[1]->node->node) - Center;
3464 const double lengthB = helper.ScalarProduct(BaseLine);
[c43766]3465 if (lengthB * lengthA < 0) { // if have different sign
[ff4611]3466 ClosestLine = LineRunner->second;
3467 MinDistance = distance;
[1f2e46]3468 DoLog(1) && (Log() << Verbose(1) << "ACCEPT: New closest line is " << *ClosestLine << " with projected distance " << MinDistance << "." << endl);
[ff4611]3469 } else {
[1f2e46]3470 DoLog(1) && (Log() << Verbose(1) << "REJECT: Intersection is outside of the line section: " << lengthA << " and " << lengthB << "." << endl);
[ff4611]3471 }
3472 } else {
[1f2e46]3473 DoLog(1) && (Log() << Verbose(1) << "REJECT: Point is too further away than present line: " << distance << " >> " << MinDistance << "." << endl);
[ff4611]3474 }
[20895b]3475 }
[60cbe5]3476 }
[c43766]3477 delete (points);
[ff4611]3478 // check whether closest line is "too close" :), then it's inside
3479 if (ClosestLine == NULL) {
[1f2e46]3480 DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl);
[eeefb3]3481 return NULL;
[ff4611]3482 }
[eda56a]3483 return ClosestLine;
[c43766]3484}
3485;
[eda56a]3486
3487/** Finds the triangle that is closest to a given Vector \a *x.
3488 * \param *out output stream for debugging
3489 * \param *x Vector to look from
3490 * \return BoundaryTriangleSet of nearest triangle or NULL.
3491 */
3492TriangleList * Tesselation::FindClosestTrianglesToVector(const Vector *x, const LinkedCell* LC) const
3493{
[c43766]3494 Info FunctionInfo(__func__);
3495 // get closest points
3496 DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC);
[eda56a]3497 if (points == NULL) {
[c43766]3498 DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
[eda56a]3499 return NULL;
3500 }
3501
3502 // for each point, check its lines, remember closest
[1f2e46]3503 DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryTriangle to " << *x << " ... " << endl);
[249522]3504 LineSet ClosestLines;
[a1acc5]3505 double MinDistance = 1e+16;
3506 Vector BaseLineIntersection;
[eda56a]3507 Vector Center;
3508 Vector BaseLine;
[a1acc5]3509 Vector BaseLineCenter;
3510 for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++) {
[eda56a]3511 for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) {
[a1acc5]3512
[1f591b]3513 BaseLine = (*(LineRunner->second)->endpoints[0]->node->node) -
3514 (*(LineRunner->second)->endpoints[1]->node->node);
[a1acc5]3515 const double lengthBase = BaseLine.NormSquared();
3516
[1f591b]3517 BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[0]->node->node);
[a1acc5]3518 const double lengthEndA = BaseLineIntersection.NormSquared();
3519
[1f591b]3520 BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[1]->node->node);
[a1acc5]3521 const double lengthEndB = BaseLineIntersection.NormSquared();
3522
[c43766]3523 if ((lengthEndA > lengthBase) || (lengthEndB > lengthBase) || ((lengthEndA < MYEPSILON) || (lengthEndB < MYEPSILON))) { // intersection would be outside, take closer endpoint
[249522]3524 const double lengthEnd = Min(lengthEndA, lengthEndB);
3525 if (lengthEnd - MinDistance < -MYEPSILON) { // new best line
3526 ClosestLines.clear();
3527 ClosestLines.insert(LineRunner->second);
3528 MinDistance = lengthEnd;
[1f2e46]3529 DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[0]->node << " is closer with " << lengthEnd << "." << endl);
[c43766]3530 } else if (fabs(lengthEnd - MinDistance) < MYEPSILON) { // additional best candidate
[249522]3531 ClosestLines.insert(LineRunner->second);
[1f2e46]3532 DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[1]->node << " is equally good with " << lengthEnd << "." << endl);
[249522]3533 } else { // line is worse
[1f2e46]3534 DoLog(1) && (Log() << Verbose(1) << "REJECT: Line " << *LineRunner->second << " to either endpoints is further away than present closest line candidate: " << lengthEndA << ", " << lengthEndB << ", and distance is longer than baseline:" << lengthBase << "." << endl);
[a1acc5]3535 }
3536 } else { // intersection is closer, calculate
[eda56a]3537 // calculate closest point on line to desired point
[1f591b]3538 BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[1]->node->node);
3539 Center = BaseLineIntersection;
3540 Center.ProjectOntoPlane(BaseLine);
3541 BaseLineIntersection -= Center;
[a1acc5]3542 const double distance = BaseLineIntersection.NormSquared();
3543 if (Center.NormSquared() > BaseLine.NormSquared()) {
[c43766]3544 DoeLog(0) && (eLog() << Verbose(0) << "Algorithmic error: In second case we have intersection outside of baseline!" << endl);
[a1acc5]3545 }
[249522]3546 if ((ClosestLines.empty()) || (distance < MinDistance)) {
3547 ClosestLines.insert(LineRunner->second);
[a1acc5]3548 MinDistance = distance;
[1f2e46]3549 DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Intersection in between endpoints, new closest line " << *LineRunner->second << " is " << *ClosestLines.begin() << " with projected distance " << MinDistance << "." << endl);
[eda56a]3550 } else {
[1f2e46]3551 DoLog(2) && (Log() << Verbose(2) << "REJECT: Point is further away from line " << *LineRunner->second << " than present closest line: " << distance << " >> " << MinDistance << "." << endl);
[eda56a]3552 }
3553 }
3554 }
3555 }
[c43766]3556 delete (points);
[eda56a]3557
3558 // check whether closest line is "too close" :), then it's inside
[249522]3559 if (ClosestLines.empty()) {
[1f2e46]3560 DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl);
[eda56a]3561 return NULL;
3562 }
3563 TriangleList * candidates = new TriangleList;
[249522]3564 for (LineSet::iterator LineRunner = ClosestLines.begin(); LineRunner != ClosestLines.end(); LineRunner++)
3565 for (TriangleMap::iterator Runner = (*LineRunner)->triangles.begin(); Runner != (*LineRunner)->triangles.end(); Runner++) {
[c43766]3566 candidates->push_back(Runner->second);
3567 }
[eda56a]3568 return candidates;
[c43766]3569}
3570;
[eeefb3]3571
3572/** Finds closest triangle to a point.
3573 * This basically just takes care of the degenerate case, which is not handled in FindClosestTrianglesToPoint().
3574 * \param *out output stream for debugging
3575 * \param *x Vector to look from
[f7b476]3576 * \param &distance contains found distance on return
[eeefb3]3577 * \return list of BoundaryTriangleSet of nearest triangles or NULL.
3578 */
[eda56a]3579class BoundaryTriangleSet * Tesselation::FindClosestTriangleToVector(const Vector *x, const LinkedCell* LC) const
[eeefb3]3580{
[c43766]3581 Info FunctionInfo(__func__);
[eeefb3]3582 class BoundaryTriangleSet *result = NULL;
[eda56a]3583 TriangleList *triangles = FindClosestTrianglesToVector(x, LC);
3584 TriangleList candidates;
[60cbe5]3585 Vector Center;
[ff4611]3586 Vector helper;
[eeefb3]3587
[ff4611]3588 if ((triangles == NULL) || (triangles->empty()))
[eeefb3]3589 return NULL;
3590
[a1acc5]3591 // go through all and pick the one with the best alignment to x
[c43766]3592 double MinAlignment = 2. * M_PI;
[eda56a]3593 for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++) {
[ff4611]3594 (*Runner)->GetCenter(&Center);
[1f591b]3595 helper = (*x) - Center;
3596 const double Alignment = helper.Angle((*Runner)->NormalVector);
[a1acc5]3597 if (Alignment < MinAlignment) {
3598 result = *Runner;
3599 MinAlignment = Alignment;
[1f2e46]3600 DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Triangle " << *result << " is better aligned with " << MinAlignment << "." << endl);
[ff4611]3601 } else {
[1f2e46]3602 DoLog(1) && (Log() << Verbose(1) << "REJECT: Triangle " << *result << " is worse aligned with " << MinAlignment << "." << endl);
[60cbe5]3603 }
3604 }
[c43766]3605 delete (triangles);
[a1acc5]3606
[eeefb3]3607 return result;
[c43766]3608}
3609;
[eeefb3]3610
[1aa2a5]3611/** Checks whether the provided Vector is within the Tesselation structure.
3612 * Basically calls Tesselation::GetDistanceToSurface() and checks the sign of the return value.
3613 * @param point of which to check the position
3614 * @param *LC LinkedCell structure
3615 *
3616 * @return true if the point is inside the Tesselation structure, false otherwise
3617 */
3618bool Tesselation::IsInnerPoint(const Vector &Point, const LinkedCell* const LC) const
3619{
[f7b476]3620 Info FunctionInfo(__func__);
[c43766]3621 TriangleIntersectionList Intersections(&Point, this, LC);
[f7b476]3622
3623 return Intersections.IsInside();
[1aa2a5]3624}
[c43766]3625;
[1aa2a5]3626
3627/** Returns the distance to the surface given by the tesselation.
[a1acc5]3628 * Calls FindClosestTriangleToVector() and checks whether the resulting triangle's BoundaryTriangleSet#NormalVector points
[1aa2a5]3629 * towards or away from the given \a &Point. Additionally, we check whether it's normal to the normal vector, i.e. on the
3630 * closest triangle's plane. Then, we have to check whether \a Point is inside the triangle or not to determine whether it's
3631 * an inside or outside point. This is done by calling BoundaryTriangleSet::GetIntersectionInsideTriangle().
3632 * In the end we additionally find the point on the triangle who was smallest distance to \a Point:
3633 * -# Separate distance from point to center in vector in NormalDirection and on the triangle plane.
3634 * -# Check whether vector on triangle plane points inside the triangle or crosses triangle bounds.
3635 * -# If inside, take it to calculate closest distance
3636 * -# If not, take intersection with BoundaryLine as distance
3637 *
3638 * @note distance is squared despite it still contains a sign to determine in-/outside!
[eeefb3]3639 *
3640 * @param point of which to check the position
3641 * @param *LC LinkedCell structure
3642 *
[478683]3643 * @return >0 if outside, ==0 if on surface, <0 if inside
[eeefb3]3644 */
[478683]3645double Tesselation::GetDistanceSquaredToTriangle(const Vector &Point, const BoundaryTriangleSet* const triangle) const
[eeefb3]3646{
[d403a1]3647 Info FunctionInfo(__func__);
[60cbe5]3648 Vector Center;
[ff4611]3649 Vector helper;
[a1acc5]3650 Vector DistanceToCenter;
3651 Vector Intersection;
[1aa2a5]3652 double distance = 0.;
[60cbe5]3653
[478683]3654 if (triangle == NULL) {// is boundary point or only point in point cloud?
[1f2e46]3655 DoLog(1) && (Log() << Verbose(1) << "No triangle given!" << endl);
[478683]3656 return -1.;
[ff4611]3657 } else {
[1f2e46]3658 DoLog(1) && (Log() << Verbose(1) << "INFO: Closest triangle found is " << *triangle << " with normal vector " << triangle->NormalVector << "." << endl);
[60cbe5]3659 }
3660
[478683]3661 triangle->GetCenter(&Center);
[1f2e46]3662 DoLog(2) && (Log() << Verbose(2) << "INFO: Central point of the triangle is " << Center << "." << endl);
[1f591b]3663 DistanceToCenter = Center - Point;
[1f2e46]3664 DoLog(2) && (Log() << Verbose(2) << "INFO: Vector from point to test to center is " << DistanceToCenter << "." << endl);
[a1acc5]3665
3666 // check whether we are on boundary
[1f591b]3667 if (fabs(DistanceToCenter.ScalarProduct(triangle->NormalVector)) < MYEPSILON) {
[a1acc5]3668 // calculate whether inside of triangle
[1f591b]3669 DistanceToCenter = Point + triangle->NormalVector; // points outside
3670 Center = Point - triangle->NormalVector; // points towards MolCenter
[1f2e46]3671 DoLog(1) && (Log() << Verbose(1) << "INFO: Calling Intersection with " << Center << " and " << DistanceToCenter << "." << endl);
[478683]3672 if (triangle->GetIntersectionInsideTriangle(&Center, &DistanceToCenter, &Intersection)) {
[1f2e46]3673 DoLog(1) && (Log() << Verbose(1) << Point << " is inner point: sufficiently close to boundary, " << Intersection << "." << endl);
[1aa2a5]3674 return 0.;
[a1acc5]3675 } else {
[1f2e46]3676 DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point: on triangle plane but outside of triangle bounds." << endl);
[a1acc5]3677 return false;
3678 }
[60cbe5]3679 } else {
[1aa2a5]3680 // calculate smallest distance
[478683]3681 distance = triangle->GetClosestPointInsideTriangle(&Point, &Intersection);
[1f2e46]3682 DoLog(1) && (Log() << Verbose(1) << "Closest point on triangle is " << Intersection << "." << endl);
[1aa2a5]3683
3684 // then check direction to boundary
[1f591b]3685 if (DistanceToCenter.ScalarProduct(triangle->NormalVector) > MYEPSILON) {
[1f2e46]3686 DoLog(1) && (Log() << Verbose(1) << Point << " is an inner point, " << distance << " below surface." << endl);
[1aa2a5]3687 return -distance;
3688 } else {
[1f2e46]3689 DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point, " << distance << " above surface." << endl);
[1aa2a5]3690 return +distance;
3691 }
[60cbe5]3692 }
[c43766]3693}
3694;
[eeefb3]3695
[f7b476]3696/** Calculates minimum distance from \a&Point to a tesselated surface.
[478683]3697 * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle().
3698 * \param &Point point to calculate distance from
3699 * \param *LC needed for finding closest points fast
3700 * \return distance squared to closest point on surface
3701 */
[f7b476]3702double Tesselation::GetDistanceToSurface(const Vector &Point, const LinkedCell* const LC) const
[478683]3703{
[f7b476]3704 Info FunctionInfo(__func__);
[c43766]3705 TriangleIntersectionList Intersections(&Point, this, LC);
[f7b476]3706
3707 return Intersections.GetSmallestDistance();
[c43766]3708}
3709;
[f7b476]3710
3711/** Calculates minimum distance from \a&Point to a tesselated surface.
3712 * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle().
3713 * \param &Point point to calculate distance from
3714 * \param *LC needed for finding closest points fast
3715 * \return distance squared to closest point on surface
3716 */
3717BoundaryTriangleSet * Tesselation::GetClosestTriangleOnSurface(const Vector &Point, const LinkedCell* const LC) const
3718{
3719 Info FunctionInfo(__func__);
[c43766]3720 TriangleIntersectionList Intersections(&Point, this, LC);
[f7b476]3721
3722 return Intersections.GetClosestTriangle();
[c43766]3723}
3724;
[478683]3725
[eeefb3]3726/** Gets all points connected to the provided point by triangulation lines.
3727 *
3728 * @param *Point of which get all connected points
3729 *
[b73929]3730 * @return set of the all points linked to the provided one
[eeefb3]3731 */
[eda56a]3732TesselPointSet * Tesselation::GetAllConnectedPoints(const TesselPoint* const Point) const
[eeefb3]3733{
[c43766]3734 Info FunctionInfo(__func__);
3735 TesselPointSet *connectedPoints = new TesselPointSet;
[0cf171]3736 class BoundaryPointSet *ReferencePoint = NULL;
[eeefb3]3737 TesselPoint* current;
3738 bool takePoint = false;
[0cf171]3739 // find the respective boundary point
[a9b2a0a]3740 PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
[0cf171]3741 if (PointRunner != PointsOnBoundary.end()) {
3742 ReferencePoint = PointRunner->second;
3743 } else {
[c43766]3744 DoeLog(2) && (eLog() << Verbose(2) << "GetAllConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl);
[0cf171]3745 ReferencePoint = NULL;
3746 }
[eeefb3]3747
[b73929]3748 // little trick so that we look just through lines connect to the BoundaryPoint
[0cf171]3749 // OR fall-back to look through all lines if there is no such BoundaryPoint
[c43766]3750 const LineMap *Lines;
3751 ;
[0cf171]3752 if (ReferencePoint != NULL)
3753 Lines = &(ReferencePoint->lines);
[a9b2a0a]3754 else
3755 Lines = &LinesOnBoundary;
3756 LineMap::const_iterator findLines = Lines->begin();
[0cf171]3757 while (findLines != Lines->end()) {
[c43766]3758 takePoint = false;
3759
3760 if (findLines->second->endpoints[0]->Nr == Point->nr) {
3761 takePoint = true;
3762 current = findLines->second->endpoints[1]->node;
3763 } else if (findLines->second->endpoints[1]->Nr == Point->nr) {
3764 takePoint = true;
3765 current = findLines->second->endpoints[0]->node;
3766 }
[b73929]3767
[c43766]3768 if (takePoint) {
[1f2e46]3769 DoLog(1) && (Log() << Verbose(1) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is enlisted." << endl);
[c43766]3770 connectedPoints->insert(current);
3771 }
[eeefb3]3772
[c43766]3773 findLines++;
[eeefb3]3774 }
3775
[ff4611]3776 if (connectedPoints->empty()) { // if have not found any points
[c43766]3777 DoeLog(1) && (eLog() << Verbose(1) << "We have not found any connected points to " << *Point << "." << endl);
[eb58a7]3778 return NULL;
3779 }
[b73929]3780
[eb58a7]3781 return connectedPoints;
[c43766]3782}
3783;
[b73929]3784
3785/** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point.
[eb58a7]3786 * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points
3787 * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given
3788 * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the
3789 * triangle we are looking for.
3790 *
3791 * @param *out output stream for debugging
[013ad57]3792 * @param *SetOfNeighbours all points for which the angle should be calculated
[eb58a7]3793 * @param *Point of which get all connected points
[b73929]3794 * @param *Reference Reference vector for zero angle or NULL for no preference
3795 * @return list of the all points linked to the provided one
[eb58a7]3796 */
[eda56a]3797TesselPointList * Tesselation::GetCircleOfConnectedTriangles(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const
[eb58a7]3798{
[c43766]3799 Info FunctionInfo(__func__);
[eb58a7]3800 map<double, TesselPoint*> anglesOfPoints;
[eda56a]3801 TesselPointList *connectedCircle = new TesselPointList;
[ff4611]3802 Vector PlaneNormal;
3803 Vector AngleZero;
3804 Vector OrthogonalVector;
3805 Vector helper;
[c43766]3806 const TesselPoint * const TrianglePoints[3] = { Point, NULL, NULL };
[eda56a]3807 TriangleList *triangles = NULL;
[ff4611]3808
3809 if (SetOfNeighbours == NULL) {
[c43766]3810 DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl);
3811 delete (connectedCircle);
[ff4611]3812 return NULL;
3813 }
3814
3815 // calculate central point
3816 triangles = FindTriangles(TrianglePoints);
3817 if ((triangles != NULL) && (!triangles->empty())) {
[eda56a]3818 for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++)
[1f591b]3819 PlaneNormal += (*Runner)->NormalVector;
[ff4611]3820 } else {
[c43766]3821 DoeLog(0) && (eLog() << Verbose(0) << "Could not find any triangles for point " << *Point << "." << endl);
[ff4611]3822 performCriticalExit();
3823 }
[c43766]3824 PlaneNormal.Scale(1.0 / triangles->size());
[1f2e46]3825 DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated PlaneNormal of all circle points is " << PlaneNormal << "." << endl);
[ff4611]3826 PlaneNormal.Normalize();
3827
3828 // construct one orthogonal vector
3829 if (Reference != NULL) {
[1f591b]3830 AngleZero = (*Reference) - (*Point->node);
3831 AngleZero.ProjectOntoPlane(PlaneNormal);
[ff4611]3832 }
[c43766]3833 if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON)) {
[1f2e46]3834 DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl);
[1f591b]3835 AngleZero = (*(*SetOfNeighbours->begin())->node) - (*Point->node);
3836 AngleZero.ProjectOntoPlane(PlaneNormal);
[ff4611]3837 if (AngleZero.NormSquared() < MYEPSILON) {
[c43766]3838 DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl);
[ff4611]3839 performCriticalExit();
3840 }
3841 }
[1f2e46]3842 DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl);
[ff4611]3843 if (AngleZero.NormSquared() > MYEPSILON)
[71910a]3844 OrthogonalVector = Plane(PlaneNormal, AngleZero,0).getNormal();
[ff4611]3845 else
[71910a]3846 OrthogonalVector.MakeNormalTo(PlaneNormal);
[1f2e46]3847 DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl);
[ff4611]3848
3849 // go through all connected points and calculate angle
[eda56a]3850 for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) {
[1f591b]3851 helper = (*(*listRunner)->node) - (*Point->node);
3852 helper.ProjectOntoPlane(PlaneNormal);
[ff4611]3853 double angle = GetAngle(helper, AngleZero, OrthogonalVector);
[1f2e46]3854 DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl);
[c43766]3855 anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner)));
[ff4611]3856 }
3857
[c43766]3858 for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
[ff4611]3859 connectedCircle->push_back(AngleRunner->second);
3860 }
3861
3862 return connectedCircle;
3863}
3864
3865/** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point.
3866 * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points
3867 * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given
3868 * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the
3869 * triangle we are looking for.
3870 *
3871 * @param *SetOfNeighbours all points for which the angle should be calculated
3872 * @param *Point of which get all connected points
3873 * @param *Reference Reference vector for zero angle or NULL for no preference
3874 * @return list of the all points linked to the provided one
3875 */
[eda56a]3876TesselPointList * Tesselation::GetCircleOfSetOfPoints(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const
[ff4611]3877{
3878 Info FunctionInfo(__func__);
3879 map<double, TesselPoint*> anglesOfPoints;
[eda56a]3880 TesselPointList *connectedCircle = new TesselPointList;
[b73929]3881 Vector center;
3882 Vector PlaneNormal;
3883 Vector AngleZero;
3884 Vector OrthogonalVector;
3885 Vector helper;
[eeefb3]3886
[013ad57]3887 if (SetOfNeighbours == NULL) {
[c43766]3888 DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl);
3889 delete (connectedCircle);
[20895b]3890 return NULL;
3891 }
[c419f2]3892
[a1acc5]3893 // check whether there's something to do
3894 if (SetOfNeighbours->size() < 3) {
3895 for (TesselPointSet::iterator TesselRunner = SetOfNeighbours->begin(); TesselRunner != SetOfNeighbours->end(); TesselRunner++)
3896 connectedCircle->push_back(*TesselRunner);
3897 return connectedCircle;
3898 }
3899
[1f2e46]3900 DoLog(1) && (Log() << Verbose(1) << "INFO: Point is " << *Point << " and Reference is " << *Reference << "." << endl);
[eb58a7]3901 // calculate central point
[a1acc5]3902 TesselPointSet::const_iterator TesselA = SetOfNeighbours->begin();
3903 TesselPointSet::const_iterator TesselB = SetOfNeighbours->begin();
3904 TesselPointSet::const_iterator TesselC = SetOfNeighbours->begin();
3905 TesselB++;
3906 TesselC++;
3907 TesselC++;
3908 int counter = 0;
3909 while (TesselC != SetOfNeighbours->end()) {
[71910a]3910 helper = Plane(*((*TesselA)->node),
3911 *((*TesselB)->node),
3912 *((*TesselC)->node)).getNormal();
[1f2e46]3913 DoLog(0) && (Log() << Verbose(0) << "Making normal vector out of " << *(*TesselA) << ", " << *(*TesselB) << " and " << *(*TesselC) << ":" << helper << endl);
[a1acc5]3914 counter++;
3915 TesselA++;
3916 TesselB++;
3917 TesselC++;
[1f591b]3918 PlaneNormal += helper;
[a1acc5]3919 }
3920 //Log() << Verbose(0) << "Summed vectors " << center << "; number of points " << connectedPoints.size()
3921 // << "; scale factor " << counter;
[c43766]3922 PlaneNormal.Scale(1.0 / (double) counter);
3923 // Log() << Verbose(1) << "INFO: Calculated center of all circle points is " << center << "." << endl;
3924 //
3925 // // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points
3926 // PlaneNormal.CopyVector(Point->node);
3927 // PlaneNormal.SubtractVector(&center);
3928 // PlaneNormal.Normalize();
[1f2e46]3929 DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl);
[eeefb3]3930
3931 // construct one orthogonal vector
[c419f2]3932 if (Reference != NULL) {
[1f591b]3933 AngleZero = (*Reference) - (*Point->node);
3934 AngleZero.ProjectOntoPlane(PlaneNormal);
[c419f2]3935 }
3936 if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON )) {
[1f2e46]3937 DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl);
[1f591b]3938 AngleZero = (*(*SetOfNeighbours->begin())->node) - (*Point->node);
3939 AngleZero.ProjectOntoPlane(PlaneNormal);
[c419f2]3940 if (AngleZero.NormSquared() < MYEPSILON) {
[c43766]3941 DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl);
[c419f2]3942 performCriticalExit();
3943 }
3944 }
[1f2e46]3945 DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl);
[c419f2]3946 if (AngleZero.NormSquared() > MYEPSILON)
[71910a]3947 OrthogonalVector = Plane(PlaneNormal, AngleZero,0).getNormal();
[c419f2]3948 else
[71910a]3949 OrthogonalVector.MakeNormalTo(PlaneNormal);
[1f2e46]3950 DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl);
[eb58a7]3951
[0cf171]3952 // go through all connected points and calculate angle
[c43766]3953 pair<map<double, TesselPoint*>::iterator, bool> InserterTest;
[eda56a]3954 for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) {
[1f591b]3955 helper = (*(*listRunner)->node) - (*Point->node);
3956 helper.ProjectOntoPlane(PlaneNormal);
[48cd93]3957 double angle = GetAngle(helper, AngleZero, OrthogonalVector);
[a1acc5]3958 if (angle > M_PI) // the correction is of no use here (and not desired)
[c43766]3959 angle = 2. * M_PI - angle;
[1f2e46]3960 DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle between " << helper << " and " << AngleZero << " is " << angle << " for point " << **listRunner << "." << endl);
[c43766]3961 InserterTest = anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner)));
[eda56a]3962 if (!InserterTest.second) {
[c43766]3963 DoeLog(0) && (eLog() << Verbose(0) << "GetCircleOfSetOfPoints() got two atoms with same angle: " << *((InserterTest.first)->second) << " and " << (*listRunner) << endl);
[eda56a]3964 performCriticalExit();
3965 }
[eeefb3]3966 }
3967
[c43766]3968 for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
[b73929]3969 connectedCircle->push_back(AngleRunner->second);
3970 }
[eeefb3]3971
[b73929]3972 return connectedCircle;
3973}
[eeefb3]3974
[b73929]3975/** Gets all points connected to the provided point by triangulation lines, ordered such that we walk along a closed path.
3976 *
3977 * @param *out output stream for debugging
3978 * @param *Point of which get all connected points
3979 * @return list of the all points linked to the provided one
3980 */
[478683]3981ListOfTesselPointList * Tesselation::GetPathsOfConnectedPoints(const TesselPoint* const Point) const
[b73929]3982{
[c43766]3983 Info FunctionInfo(__func__);
[b73929]3984 map<double, TesselPoint*> anglesOfPoints;
[c43766]3985 list<TesselPointList *> *ListOfPaths = new list<TesselPointList *> ;
[eda56a]3986 TesselPointList *connectedPath = NULL;
[b73929]3987 Vector center;
3988 Vector PlaneNormal;
3989 Vector AngleZero;
3990 Vector OrthogonalVector;
3991 Vector helper;
3992 class BoundaryPointSet *ReferencePoint = NULL;
3993 class BoundaryPointSet *CurrentPoint = NULL;
3994 class BoundaryTriangleSet *triangle = NULL;
3995 class BoundaryLineSet *CurrentLine = NULL;
3996 class BoundaryLineSet *StartLine = NULL;
3997 // find the respective boundary point
[a9b2a0a]3998 PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
[b73929]3999 if (PointRunner != PointsOnBoundary.end()) {
4000 ReferencePoint = PointRunner->second;
4001 } else {
[c43766]4002 DoeLog(1) && (eLog() << Verbose(1) << "GetPathOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl);
[b73929]4003 return NULL;
4004 }
4005
[c43766]4006 map<class BoundaryLineSet *, bool> TouchedLine;
4007 map<class BoundaryTriangleSet *, bool> TouchedTriangle;
4008 map<class BoundaryLineSet *, bool>::iterator LineRunner;
4009 map<class BoundaryTriangleSet *, bool>::iterator TriangleRunner;
[60cbe5]4010 for (LineMap::iterator Runner = ReferencePoint->lines.begin(); Runner != ReferencePoint->lines.end(); Runner++) {
[c43766]4011 TouchedLine.insert(pair<class BoundaryLineSet *, bool> (Runner->second, false));
[60cbe5]4012 for (TriangleMap::iterator Sprinter = Runner->second->triangles.begin(); Sprinter != Runner->second->triangles.end(); Sprinter++)
[c43766]4013 TouchedTriangle.insert(pair<class BoundaryTriangleSet *, bool> (Sprinter->second, false));
[60cbe5]4014 }
[b73929]4015 if (!ReferencePoint->lines.empty()) {
4016 for (LineMap::iterator runner = ReferencePoint->lines.begin(); runner != ReferencePoint->lines.end(); runner++) {
[60cbe5]4017 LineRunner = TouchedLine.find(runner->second);
4018 if (LineRunner == TouchedLine.end()) {
[c43766]4019 DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *runner->second << " in the touched list." << endl);
[60cbe5]4020 } else if (!LineRunner->second) {
4021 LineRunner->second = true;
[eda56a]4022 connectedPath = new TesselPointList;
[b73929]4023 triangle = NULL;
4024 CurrentLine = runner->second;
4025 StartLine = CurrentLine;
4026 CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint);
[1f2e46]4027 DoLog(1) && (Log() << Verbose(1) << "INFO: Beginning path retrieval at " << *CurrentPoint << " of line " << *CurrentLine << "." << endl);
[b73929]4028 do {
4029 // push current one
[1f2e46]4030 DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl);
[b73929]4031 connectedPath->push_back(CurrentPoint->node);
4032
4033 // find next triangle
[60cbe5]4034 for (TriangleMap::iterator Runner = CurrentLine->triangles.begin(); Runner != CurrentLine->triangles.end(); Runner++) {
[1f2e46]4035 DoLog(1) && (Log() << Verbose(1) << "INFO: Inspecting triangle " << *Runner->second << "." << endl);
[60cbe5]4036 if ((Runner->second != triangle)) { // look for first triangle not equal to old one
4037 triangle = Runner->second;
4038 TriangleRunner = TouchedTriangle.find(triangle);
4039 if (TriangleRunner != TouchedTriangle.end()) {
4040 if (!TriangleRunner->second) {
4041 TriangleRunner->second = true;
[1f2e46]4042 DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting triangle is " << *triangle << "." << endl);
[60cbe5]4043 break;
4044 } else {
[1f2e46]4045 DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *triangle << ", as we have already visited it." << endl);
[60cbe5]4046 triangle = NULL;
4047 }
4048 } else {
[c43766]4049 DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *triangle << " in the touched list." << endl);
[60cbe5]4050 triangle = NULL;
4051 }
[b73929]4052 }
4053 }
[60cbe5]4054 if (triangle == NULL)
4055 break;
[b73929]4056 // find next line
[c43766]4057 for (int i = 0; i < 3; i++) {
[b73929]4058 if ((triangle->lines[i] != CurrentLine) && (triangle->lines[i]->ContainsBoundaryPoint(ReferencePoint))) { // not the current line and still containing Point
4059 CurrentLine = triangle->lines[i];
[1f2e46]4060 DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting line is " << *CurrentLine << "." << endl);
[b73929]4061 break;
4062 }
4063 }
[60cbe5]4064 LineRunner = TouchedLine.find(CurrentLine);
4065 if (LineRunner == TouchedLine.end())
[c43766]4066 DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *CurrentLine << " in the touched list." << endl);
[b73929]4067 else
[60cbe5]4068 LineRunner->second = true;
[b73929]4069 // find next point
4070 CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint);
4071
4072 } while (CurrentLine != StartLine);
4073 // last point is missing, as it's on start line
[1f2e46]4074 DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl);
[60cbe5]4075 if (StartLine->GetOtherEndpoint(ReferencePoint)->node != connectedPath->back())
4076 connectedPath->push_back(StartLine->GetOtherEndpoint(ReferencePoint)->node);
[b73929]4077
4078 ListOfPaths->push_back(connectedPath);
4079 } else {
[1f2e46]4080 DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *runner->second << ", as we have already visited it." << endl);
[b73929]4081 }
4082 }
4083 } else {
[c43766]4084 DoeLog(1) && (eLog() << Verbose(1) << "There are no lines attached to " << *ReferencePoint << "." << endl);
[b73929]4085 }
4086
4087 return ListOfPaths;
[eeefb3]4088}
4089
[b73929]4090/** Gets all closed paths on the circle of points connected to the provided point by triangulation lines, if this very point is removed.
4091 * From GetPathsOfConnectedPoints() extracts all single loops of intracrossing paths in the list of closed paths.
4092 * @param *out output stream for debugging
4093 * @param *Point of which get all connected points
4094 * @return list of the closed paths
4095 */
[478683]4096ListOfTesselPointList * Tesselation::GetClosedPathsOfConnectedPoints(const TesselPoint* const Point) const
[b73929]4097{
[c43766]4098 Info FunctionInfo(__func__);
[eda56a]4099 list<TesselPointList *> *ListofPaths = GetPathsOfConnectedPoints(Point);
[c43766]4100 list<TesselPointList *> *ListofClosedPaths = new list<TesselPointList *> ;
[eda56a]4101 TesselPointList *connectedPath = NULL;
4102 TesselPointList *newPath = NULL;
[b73929]4103 int count = 0;
[eda56a]4104 TesselPointList::iterator CircleRunner;
4105 TesselPointList::iterator CircleStart;
[b73929]4106
[c43766]4107 for (list<TesselPointList *>::iterator ListRunner = ListofPaths->begin(); ListRunner != ListofPaths->end(); ListRunner++) {
[b73929]4108 connectedPath = *ListRunner;
4109
[1f2e46]4110 DoLog(1) && (Log() << Verbose(1) << "INFO: Current path is " << connectedPath << "." << endl);
[b73929]4111
4112 // go through list, look for reappearance of starting Point and count
4113 CircleStart = connectedPath->begin();
4114 // go through list, look for reappearance of starting Point and create list
[eda56a]4115 TesselPointList::iterator Marker = CircleStart;
[b73929]4116 for (CircleRunner = CircleStart; CircleRunner != connectedPath->end(); CircleRunner++) {
4117 if ((*CircleRunner == *CircleStart) && (CircleRunner != CircleStart)) { // is not the very first point
4118 // we have a closed circle from Marker to new Marker
[1f2e46]4119 DoLog(1) && (Log() << Verbose(1) << count + 1 << ". closed path consists of: ");
[eda56a]4120 newPath = new TesselPointList;
4121 TesselPointList::iterator CircleSprinter = Marker;
[b73929]4122 for (; CircleSprinter != CircleRunner; CircleSprinter++) {
4123 newPath->push_back(*CircleSprinter);
[1f2e46]4124 DoLog(0) && (Log() << Verbose(0) << (**CircleSprinter) << " <-> ");
[b73929]4125 }
[1f2e46]4126 DoLog(0) && (Log() << Verbose(0) << ".." << endl);
[b73929]4127 count++;
4128 Marker = CircleRunner;
4129
4130 // add to list
4131 ListofClosedPaths->push_back(newPath);
4132 }
4133 }
4134 }
[1f2e46]4135 DoLog(1) && (Log() << Verbose(1) << "INFO: " << count << " closed additional path(s) have been created." << endl);
[b73929]4136
4137 // delete list of paths
4138 while (!ListofPaths->empty()) {
4139 connectedPath = *(ListofPaths->begin());
4140 ListofPaths->remove(connectedPath);
[c43766]4141 delete (connectedPath);
[b73929]4142 }
[c43766]4143 delete (ListofPaths);
[b73929]4144
4145 // exit
4146 return ListofClosedPaths;
[c43766]4147}
4148;
[b73929]4149
4150/** Gets all belonging triangles for a given BoundaryPointSet.
4151 * \param *out output stream for debugging
4152 * \param *Point BoundaryPoint
4153 * \return pointer to allocated list of triangles
4154 */
[eda56a]4155TriangleSet *Tesselation::GetAllTriangles(const BoundaryPointSet * const Point) const
[b73929]4156{
[c43766]4157 Info FunctionInfo(__func__);
4158 TriangleSet *connectedTriangles = new TriangleSet;
[b73929]4159
4160 if (Point == NULL) {
[c43766]4161 DoeLog(1) && (eLog() << Verbose(1) << "Point given is NULL." << endl);
[b73929]4162 } else {
4163 // go through its lines and insert all triangles
[a9b2a0a]4164 for (LineMap::const_iterator LineRunner = Point->lines.begin(); LineRunner != Point->lines.end(); LineRunner++)
[b73929]4165 for (TriangleMap::iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
[c43766]4166 connectedTriangles->insert(TriangleRunner->second);
4167 }
[b73929]4168 }
4169
4170 return connectedTriangles;
[c43766]4171}
4172;
[b73929]4173
[eb58a7]4174/** Removes a boundary point from the envelope while keeping it closed.
[60cbe5]4175 * We remove the old triangles connected to the point and re-create new triangles to close the surface following this ansatz:
4176 * -# a closed path(s) of boundary points surrounding the point to be removed is constructed
4177 * -# on each closed path, we pick three adjacent points, create a triangle with them and subtract the middle point from the path
4178 * -# we advance two points (i.e. the next triangle will start at the ending point of the last triangle) and continue as before
4179 * -# the surface is closed, when the path is empty
4180 * Thereby, we (hopefully) make sure that the removed points remains beneath the surface (this is checked via IsInnerPoint eventually).
[eb58a7]4181 * \param *out output stream for debugging
4182 * \param *point point to be removed
4183 * \return volume added to the volume inside the tesselated surface by the removal
4184 */
[c43766]4185double Tesselation::RemovePointFromTesselatedSurface(class BoundaryPointSet *point)
4186{
[eb58a7]4187 class BoundaryLineSet *line = NULL;
4188 class BoundaryTriangleSet *triangle = NULL;
[60cbe5]4189 Vector OldPoint, NormalVector;
[eb58a7]4190 double volume = 0;
4191 int count = 0;
4192
[78dac6]4193 if (point == NULL) {
[c43766]4194 DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << point << ", it's NULL!" << endl);
[78dac6]4195 return 0.;
4196 } else
[1f2e46]4197 DoLog(0) && (Log() << Verbose(0) << "Removing point " << *point << " from tesselated boundary ..." << endl);
[78dac6]4198
[eb58a7]4199 // copy old location for the volume
[1f591b]4200 OldPoint = (*point->node->node);
[eb58a7]4201
4202 // get list of connected points
4203 if (point->lines.empty()) {
[c43766]4204 DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << *point << ", it's connected to no lines!" << endl);
[eb58a7]4205 return 0.;
4206 }
4207
[eda56a]4208 list<TesselPointList *> *ListOfClosedPaths = GetClosedPathsOfConnectedPoints(point->node);
4209 TesselPointList *connectedPath = NULL;
[b73929]4210
4211 // gather all triangles
[eb58a7]4212 for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++)
[c43766]4213 count += LineRunner->second->triangles.size();
[eda56a]4214 TriangleMap Candidates;
[60cbe5]4215 for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
[eb58a7]4216 line = LineRunner->second;
4217 for (TriangleMap::iterator TriangleRunner = line->triangles.begin(); TriangleRunner != line->triangles.end(); TriangleRunner++) {
4218 triangle = TriangleRunner->second;
[c43766]4219 Candidates.insert(TrianglePair(triangle->Nr, triangle));
[eb58a7]4220 }
4221 }
4222
[b73929]4223 // remove all triangles
[c43766]4224 count = 0;
[60cbe5]4225 NormalVector.Zero();
[eda56a]4226 for (TriangleMap::iterator Runner = Candidates.begin(); Runner != Candidates.end(); Runner++) {
[1f2e46]4227 DoLog(1) && (Log() << Verbose(1) << "INFO: Removing triangle " << *(Runner->second) << "." << endl);
[1f591b]4228 NormalVector -= Runner->second->NormalVector; // has to point inward
[eda56a]4229 RemoveTesselationTriangle(Runner->second);
[b73929]4230 count++;
4231 }
[1f2e46]4232 DoLog(1) && (Log() << Verbose(1) << count << " triangles were removed." << endl);
[b73929]4233
[eda56a]4234 list<TesselPointList *>::iterator ListAdvance = ListOfClosedPaths->begin();
4235 list<TesselPointList *>::iterator ListRunner = ListAdvance;
4236 TriangleMap::iterator NumberRunner = Candidates.begin();
4237 TesselPointList::iterator StartNode, MiddleNode, EndNode;
[60cbe5]4238 double angle;
4239 double smallestangle;
4240 Vector Point, Reference, OrthogonalVector;
[c43766]4241 if (count > 2) { // less than three triangles, then nothing will be created
[b73929]4242 class TesselPoint *TriangleCandidates[3];
4243 count = 0;
[c43766]4244 for (; ListRunner != ListOfClosedPaths->end(); ListRunner = ListAdvance) { // go through all closed paths
[b73929]4245 if (ListAdvance != ListOfClosedPaths->end())
4246 ListAdvance++;
4247
4248 connectedPath = *ListRunner;
4249 // re-create all triangles by going through connected points list
[eda56a]4250 LineList NewLines;
[c43766]4251 for (; !connectedPath->empty();) {
[60cbe5]4252 // search middle node with widest angle to next neighbours
4253 EndNode = connectedPath->end();
4254 smallestangle = 0.;
4255 for (MiddleNode = connectedPath->begin(); MiddleNode != connectedPath->end(); MiddleNode++) {
[1f2e46]4256 DoLog(1) && (Log() << Verbose(1) << "INFO: MiddleNode is " << **MiddleNode << "." << endl);
[60cbe5]4257 // construct vectors to next and previous neighbour
4258 StartNode = MiddleNode;
4259 if (StartNode == connectedPath->begin())
4260 StartNode = connectedPath->end();
4261 StartNode--;
[543ce4]4262 //Log() << Verbose(3) << "INFO: StartNode is " << **StartNode << "." << endl;
[1f591b]4263 Point = (*(*StartNode)->node) - (*(*MiddleNode)->node);
[60cbe5]4264 StartNode = MiddleNode;
4265 StartNode++;
4266 if (StartNode == connectedPath->end())
4267 StartNode = connectedPath->begin();
[543ce4]4268 //Log() << Verbose(3) << "INFO: EndNode is " << **StartNode << "." << endl;
[1f591b]4269 Reference = (*(*StartNode)->node) - (*(*MiddleNode)->node);
4270 OrthogonalVector = (*(*MiddleNode)->node) - OldPoint;
[71910a]4271 OrthogonalVector.MakeNormalTo(Reference);
[60cbe5]4272 angle = GetAngle(Point, Reference, OrthogonalVector);
4273 //if (angle < M_PI) // no wrong-sided triangles, please?
[c43766]4274 if (fabs(angle - M_PI) < fabs(smallestangle - M_PI)) { // get straightest angle (i.e. construct those triangles with smallest area first)
4275 smallestangle = angle;
4276 EndNode = MiddleNode;
4277 }
[60cbe5]4278 }
4279 MiddleNode = EndNode;
4280 if (MiddleNode == connectedPath->end()) {
[c43766]4281 DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: Could not find a smallest angle!" << endl);
[be2997]4282 performCriticalExit();
[60cbe5]4283 }
4284 StartNode = MiddleNode;
4285 if (StartNode == connectedPath->begin())
4286 StartNode = connectedPath->end();
4287 StartNode--;
4288 EndNode++;
4289 if (EndNode == connectedPath->end())
4290 EndNode = connectedPath->begin();
[1f2e46]4291 DoLog(2) && (Log() << Verbose(2) << "INFO: StartNode is " << **StartNode << "." << endl);
4292 DoLog(2) && (Log() << Verbose(2) << "INFO: MiddleNode is " << **MiddleNode << "." << endl);
4293 DoLog(2) && (Log() << Verbose(2) << "INFO: EndNode is " << **EndNode << "." << endl);
4294 DoLog(1) && (Log() << Verbose(1) << "INFO: Attempting to create triangle " << (*StartNode)->Name << ", " << (*MiddleNode)->Name << " and " << (*EndNode)->Name << "." << endl);
[60cbe5]4295 TriangleCandidates[0] = *StartNode;
4296 TriangleCandidates[1] = *MiddleNode;
4297 TriangleCandidates[2] = *EndNode;
[543ce4]4298 triangle = GetPresentTriangle(TriangleCandidates);
[60cbe5]4299 if (triangle != NULL) {
[c43766]4300 DoeLog(0) && (eLog() << Verbose(0) << "New triangle already present, skipping!" << endl);
[60cbe5]4301 StartNode++;
4302 MiddleNode++;
4303 EndNode++;
4304 if (StartNode == connectedPath->end())
4305 StartNode = connectedPath->begin();
4306 if (MiddleNode == connectedPath->end())
4307 MiddleNode = connectedPath->begin();
4308 if (EndNode == connectedPath->end())
4309 EndNode = connectedPath->begin();
4310 continue;
4311 }
[1f2e46]4312 DoLog(3) && (Log() << Verbose(3) << "Adding new triangle points." << endl);
[60cbe5]4313 AddTesselationPoint(*StartNode, 0);
4314 AddTesselationPoint(*MiddleNode, 1);
4315 AddTesselationPoint(*EndNode, 2);
[1f2e46]4316 DoLog(3) && (Log() << Verbose(3) << "Adding new triangle lines." << endl);
[086db0]4317 AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
4318 AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
[60cbe5]4319 NewLines.push_back(BLS[1]);
[086db0]4320 AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
[b73929]4321 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
[60cbe5]4322 BTS->GetNormalVector(NormalVector);
[b73929]4323 AddTesselationTriangle();
4324 // calculate volume summand as a general tetraeder
[6d7651]4325 volume += CalculateVolumeofGeneralTetraeder(*TPS[0]->node->node, *TPS[1]->node->node, *TPS[2]->node->node, OldPoint);
[b73929]4326 // advance number
4327 count++;
[60cbe5]4328
4329 // prepare nodes for next triangle
4330 StartNode = EndNode;
[1f2e46]4331 DoLog(2) && (Log() << Verbose(2) << "Removing " << **MiddleNode << " from closed path, remaining points: " << connectedPath->size() << "." << endl);
[60cbe5]4332 connectedPath->remove(*MiddleNode); // remove the middle node (it is surrounded by triangles)
4333 if (connectedPath->size() == 2) { // we are done
4334 connectedPath->remove(*StartNode); // remove the start node
4335 connectedPath->remove(*EndNode); // remove the end node
4336 break;
4337 } else if (connectedPath->size() < 2) { // something's gone wrong!
[c43766]4338 DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: There are only two endpoints left!" << endl);
[be2997]4339 performCriticalExit();
[60cbe5]4340 } else {
4341 MiddleNode = StartNode;
4342 MiddleNode++;
4343 if (MiddleNode == connectedPath->end())
4344 MiddleNode = connectedPath->begin();
4345 EndNode = MiddleNode;
4346 EndNode++;
4347 if (EndNode == connectedPath->end())
4348 EndNode = connectedPath->begin();
4349 }
[b73929]4350 }
[60cbe5]4351 // maximize the inner lines (we preferentially created lines with a huge angle, which is for the tesselation not wanted though useful for the closing)
4352 if (NewLines.size() > 1) {
[eda56a]4353 LineList::iterator Candidate;
[60cbe5]4354 class BoundaryLineSet *OtherBase = NULL;
4355 double tmp, maxgain;
4356 do {
4357 maxgain = 0;
[c43766]4358 for (LineList::iterator Runner = NewLines.begin(); Runner != NewLines.end(); Runner++) {
[543ce4]4359 tmp = PickFarthestofTwoBaselines(*Runner);
[60cbe5]4360 if (maxgain < tmp) {
4361 maxgain = tmp;
4362 Candidate = Runner;
4363 }
4364 }
4365 if (maxgain != 0) {
4366 volume += maxgain;
[1f2e46]4367 DoLog(1) && (Log() << Verbose(1) << "Flipping baseline with highest volume" << **Candidate << "." << endl);
[543ce4]4368 OtherBase = FlipBaseline(*Candidate);
[60cbe5]4369 NewLines.erase(Candidate);
4370 NewLines.push_back(OtherBase);
4371 }
4372 } while (maxgain != 0.);
4373 }
4374
[b73929]4375 ListOfClosedPaths->remove(connectedPath);
[c43766]4376 delete (connectedPath);
[eb58a7]4377 }
[1f2e46]4378 DoLog(0) && (Log() << Verbose(0) << count << " triangles were created." << endl);
[b73929]4379 } else {
4380 while (!ListOfClosedPaths->empty()) {
4381 ListRunner = ListOfClosedPaths->begin();
4382 connectedPath = *ListRunner;
4383 ListOfClosedPaths->remove(connectedPath);
[c43766]4384 delete (connectedPath);
[b73929]4385 }
[1f2e46]4386 DoLog(0) && (Log() << Verbose(0) << "No need to create any triangles." << endl);
[eb58a7]4387 }
[c43766]4388 delete (ListOfClosedPaths);
[eb58a7]4389
[1f2e46]4390 DoLog(0) && (Log() << Verbose(0) << "Removed volume is " << volume << "." << endl);
[834ff3]4391
[60cbe5]4392 return volume;
[c43766]4393}
4394;
[f4a346]4395
4396/**
[eeefb3]4397 * Finds triangles belonging to the three provided points.
[f4a346]4398 *
[ff4611]4399 * @param *Points[3] list, is expected to contain three points (NULL means wildcard)
[f4a346]4400 *
[eeefb3]4401 * @return triangles which belong to the provided points, will be empty if there are none,
[f4a346]4402 * will usually be one, in case of degeneration, there will be two
4403 */
[eda56a]4404TriangleList *Tesselation::FindTriangles(const TesselPoint* const Points[3]) const
[f4a346]4405{
[c43766]4406 Info FunctionInfo(__func__);
4407 TriangleList *result = new TriangleList;
[a9b2a0a]4408 LineMap::const_iterator FindLine;
4409 TriangleMap::const_iterator FindTriangle;
[f4a346]4410 class BoundaryPointSet *TrianglePoints[3];
[ff4611]4411 size_t NoOfWildcards = 0;
[f4a346]4412
4413 for (int i = 0; i < 3; i++) {
[ff4611]4414 if (Points[i] == NULL) {
4415 NoOfWildcards++;
[f4a346]4416 TrianglePoints[i] = NULL;
[ff4611]4417 } else {
4418 PointMap::const_iterator FindPoint = PointsOnBoundary.find(Points[i]->nr);
4419 if (FindPoint != PointsOnBoundary.end()) {
4420 TrianglePoints[i] = FindPoint->second;
4421 } else {
4422 TrianglePoints[i] = NULL;
4423 }
[f4a346]4424 }
4425 }
4426
[ff4611]4427 switch (NoOfWildcards) {
4428 case 0: // checks lines between the points in the Points for their adjacent triangles
4429 for (int i = 0; i < 3; i++) {
4430 if (TrianglePoints[i] != NULL) {
[c43766]4431 for (int j = i + 1; j < 3; j++) {
[ff4611]4432 if (TrianglePoints[j] != NULL) {
4433 for (FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr); // is a multimap!
[c43766]4434 (FindLine != TrianglePoints[i]->lines.end()) && (FindLine->first == TrianglePoints[j]->node->nr); FindLine++) {
4435 for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) {
[ff4611]4436 if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
4437 result->push_back(FindTriangle->second);
4438 }
4439 }
[f4a346]4440 }
[ff4611]4441 // Is it sufficient to consider one of the triangle lines for this.
4442 return result;
[f4a346]4443 }
4444 }
4445 }
4446 }
[ff4611]4447 break;
4448 case 1: // copy all triangles of the respective line
4449 {
[c43766]4450 int i = 0;
[ff4611]4451 for (; i < 3; i++)
4452 if (TrianglePoints[i] == NULL)
4453 break;
[c43766]4454 for (FindLine = TrianglePoints[(i + 1) % 3]->lines.find(TrianglePoints[(i + 2) % 3]->node->nr); // is a multimap!
4455 (FindLine != TrianglePoints[(i + 1) % 3]->lines.end()) && (FindLine->first == TrianglePoints[(i + 2) % 3]->node->nr); FindLine++) {
4456 for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) {
[ff4611]4457 if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
4458 result->push_back(FindTriangle->second);
4459 }
4460 }
4461 }
4462 break;
4463 }
4464 case 2: // copy all triangles of the respective point
4465 {
[c43766]4466 int i = 0;
[ff4611]4467 for (; i < 3; i++)
4468 if (TrianglePoints[i] != NULL)
4469 break;
4470 for (LineMap::const_iterator line = TrianglePoints[i]->lines.begin(); line != TrianglePoints[i]->lines.end(); line++)
4471 for (TriangleMap::const_iterator triangle = line->second->triangles.begin(); triangle != line->second->triangles.end(); triangle++)
4472 result->push_back(triangle->second);
4473 result->sort();
4474 result->unique();
4475 break;
4476 }
4477 case 3: // copy all triangles
4478 {
4479 for (TriangleMap::const_iterator triangle = TrianglesOnBoundary.begin(); triangle != TrianglesOnBoundary.end(); triangle++)
4480 result->push_back(triangle->second);
4481 break;
[f4a346]4482 }
[ff4611]4483 default:
[c43766]4484 DoeLog(0) && (eLog() << Verbose(0) << "Number of wildcards is greater than 3, cannot happen!" << endl);
[ff4611]4485 performCriticalExit();
4486 break;
[f4a346]4487 }
4488
4489 return result;
4490}
4491
[c43766]4492struct BoundaryLineSetCompare
4493{
4494 bool operator()(const BoundaryLineSet * const a, const BoundaryLineSet * const b)
4495 {
[ea3627]4496 int lowerNra = -1;
4497 int lowerNrb = -1;
4498
4499 if (a->endpoints[0] < a->endpoints[1])
4500 lowerNra = 0;
4501 else
4502 lowerNra = 1;
4503
4504 if (b->endpoints[0] < b->endpoints[1])
4505 lowerNrb = 0;
4506 else
4507 lowerNrb = 1;
4508
4509 if (a->endpoints[lowerNra] < b->endpoints[lowerNrb])
4510 return true;
4511 else if (a->endpoints[lowerNra] > b->endpoints[lowerNrb])
4512 return false;
[c43766]4513 else { // both lower-numbered endpoints are the same ...
4514 if (a->endpoints[(lowerNra + 1) % 2] < b->endpoints[(lowerNrb + 1) % 2])
4515 return true;
4516 else if (a->endpoints[(lowerNra + 1) % 2] > b->endpoints[(lowerNrb + 1) % 2])
4517 return false;
[ea3627]4518 }
4519 return false;
[c43766]4520 }
4521 ;
[ea3627]4522};
4523
4524#define UniqueLines set < class BoundaryLineSet *, BoundaryLineSetCompare>
4525
[d6b011]4526/**
[60cbe5]4527 * Finds all degenerated lines within the tesselation structure.
[d6b011]4528 *
[60cbe5]4529 * @return map of keys of degenerated line pairs, each line occurs twice
[d6b011]4530 * in the list, once as key and once as value
4531 */
[eda56a]4532IndexToIndex * Tesselation::FindAllDegeneratedLines()
[d6b011]4533{
[c43766]4534 Info FunctionInfo(__func__);
4535 UniqueLines AllLines;
[eda56a]4536 IndexToIndex * DegeneratedLines = new IndexToIndex;
[d6b011]4537
4538 // sanity check
4539 if (LinesOnBoundary.empty()) {
[c43766]4540 DoeLog(2) && (eLog() << Verbose(2) << "FindAllDegeneratedTriangles() was called without any tesselation structure.");
[60cbe5]4541 return DegeneratedLines;
[d6b011]4542 }
[60cbe5]4543 LineMap::iterator LineRunner1;
[c43766]4544 pair<UniqueLines::iterator, bool> tester;
[d6b011]4545 for (LineRunner1 = LinesOnBoundary.begin(); LineRunner1 != LinesOnBoundary.end(); ++LineRunner1) {
[c43766]4546 tester = AllLines.insert(LineRunner1->second);
[ea3627]4547 if (!tester.second) { // found degenerated line
[c43766]4548 DegeneratedLines->insert(pair<int, int> (LineRunner1->second->Nr, (*tester.first)->Nr));
4549 DegeneratedLines->insert(pair<int, int> ((*tester.first)->Nr, LineRunner1->second->Nr));
[60cbe5]4550 }
4551 }
4552
4553 AllLines.clear();
4554
[1f2e46]4555 DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedLines() found " << DegeneratedLines->size() << " lines." << endl);
[eda56a]4556 IndexToIndex::iterator it;
[ea3627]4557 for (it = DegeneratedLines->begin(); it != DegeneratedLines->end(); it++) {
4558 const LineMap::const_iterator Line1 = LinesOnBoundary.find((*it).first);
4559 const LineMap::const_iterator Line2 = LinesOnBoundary.find((*it).second);
4560 if (Line1 != LinesOnBoundary.end() && Line2 != LinesOnBoundary.end())
[1f2e46]4561 DoLog(0) && (Log() << Verbose(0) << *Line1->second << " => " << *Line2->second << endl);
[ea3627]4562 else
[c43766]4563 DoeLog(1) && (eLog() << Verbose(1) << "Either " << (*it).first << " or " << (*it).second << " are not in LinesOnBoundary!" << endl);
[ea3627]4564 }
[60cbe5]4565
4566 return DegeneratedLines;
4567}
4568
4569/**
4570 * Finds all degenerated triangles within the tesselation structure.
4571 *
4572 * @return map of keys of degenerated triangle pairs, each triangle occurs twice
4573 * in the list, once as key and once as value
4574 */
[eda56a]4575IndexToIndex * Tesselation::FindAllDegeneratedTriangles()
[60cbe5]4576{
[c43766]4577 Info FunctionInfo(__func__);
[eda56a]4578 IndexToIndex * DegeneratedLines = FindAllDegeneratedLines();
4579 IndexToIndex * DegeneratedTriangles = new IndexToIndex;
[60cbe5]4580 TriangleMap::iterator TriangleRunner1, TriangleRunner2;
4581 LineMap::iterator Liner;
4582 class BoundaryLineSet *line1 = NULL, *line2 = NULL;
4583
[eda56a]4584 for (IndexToIndex::iterator LineRunner = DegeneratedLines->begin(); LineRunner != DegeneratedLines->end(); ++LineRunner) {
[60cbe5]4585 // run over both lines' triangles
4586 Liner = LinesOnBoundary.find(LineRunner->first);
4587 if (Liner != LinesOnBoundary.end())
4588 line1 = Liner->second;
4589 Liner = LinesOnBoundary.find(LineRunner->second);
4590 if (Liner != LinesOnBoundary.end())
4591 line2 = Liner->second;
4592 for (TriangleRunner1 = line1->triangles.begin(); TriangleRunner1 != line1->triangles.end(); ++TriangleRunner1) {
4593 for (TriangleRunner2 = line2->triangles.begin(); TriangleRunner2 != line2->triangles.end(); ++TriangleRunner2) {
[c43766]4594 if ((TriangleRunner1->second != TriangleRunner2->second) && (TriangleRunner1->second->IsPresentTupel(TriangleRunner2->second))) {
4595 DegeneratedTriangles->insert(pair<int, int> (TriangleRunner1->second->Nr, TriangleRunner2->second->Nr));
4596 DegeneratedTriangles->insert(pair<int, int> (TriangleRunner2->second->Nr, TriangleRunner1->second->Nr));
[d6b011]4597 }
4598 }
4599 }
4600 }
[c43766]4601 delete (DegeneratedLines);
[d6b011]4602
[1f2e46]4603 DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedTriangles() found " << DegeneratedTriangles->size() << " triangles:" << endl);
[eda56a]4604 IndexToIndex::iterator it;
[60cbe5]4605 for (it = DegeneratedTriangles->begin(); it != DegeneratedTriangles->end(); it++)
[1f2e46]4606 DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl);
[d6b011]4607
4608 return DegeneratedTriangles;
4609}
4610
4611/**
4612 * Purges degenerated triangles from the tesselation structure if they are not
4613 * necessary to keep a single point within the structure.
4614 */
4615void Tesselation::RemoveDegeneratedTriangles()
4616{
[c43766]4617 Info FunctionInfo(__func__);
[eda56a]4618 IndexToIndex * DegeneratedTriangles = FindAllDegeneratedTriangles();
[60cbe5]4619 TriangleMap::iterator finder;
4620 BoundaryTriangleSet *triangle = NULL, *partnerTriangle = NULL;
[c43766]4621 int count = 0;
[d6b011]4622
[c43766]4623 for (IndexToIndex::iterator TriangleKeyRunner = DegeneratedTriangles->begin(); TriangleKeyRunner != DegeneratedTriangles->end(); ++TriangleKeyRunner) {
[60cbe5]4624 finder = TrianglesOnBoundary.find(TriangleKeyRunner->first);
4625 if (finder != TrianglesOnBoundary.end())
4626 triangle = finder->second;
4627 else
4628 break;
4629 finder = TrianglesOnBoundary.find(TriangleKeyRunner->second);
4630 if (finder != TrianglesOnBoundary.end())
4631 partnerTriangle = finder->second;
4632 else
4633 break;
[d6b011]4634
4635 bool trianglesShareLine = false;
4636 for (int i = 0; i < 3; ++i)
4637 for (int j = 0; j < 3; ++j)
4638 trianglesShareLine = trianglesShareLine || triangle->lines[i] == partnerTriangle->lines[j];
4639
[c43766]4640 if (trianglesShareLine && (triangle->endpoints[1]->LinesCount > 2) && (triangle->endpoints[2]->LinesCount > 2) && (triangle->endpoints[0]->LinesCount > 2)) {
[60cbe5]4641 // check whether we have to fix lines
4642 BoundaryTriangleSet *Othertriangle = NULL;
4643 BoundaryTriangleSet *OtherpartnerTriangle = NULL;
4644 TriangleMap::iterator TriangleRunner;
4645 for (int i = 0; i < 3; ++i)
4646 for (int j = 0; j < 3; ++j)
4647 if (triangle->lines[i] != partnerTriangle->lines[j]) {
4648 // get the other two triangles
4649 for (TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); ++TriangleRunner)
4650 if (TriangleRunner->second != triangle) {
4651 Othertriangle = TriangleRunner->second;
4652 }
4653 for (TriangleRunner = partnerTriangle->lines[i]->triangles.begin(); TriangleRunner != partnerTriangle->lines[i]->triangles.end(); ++TriangleRunner)
4654 if (TriangleRunner->second != partnerTriangle) {
4655 OtherpartnerTriangle = TriangleRunner->second;
4656 }
4657 /// interchanges their lines so that triangle->lines[i] == partnerTriangle->lines[j]
4658 // the line of triangle receives the degenerated ones
4659 triangle->lines[i]->triangles.erase(Othertriangle->Nr);
[c43766]4660 triangle->lines[i]->triangles.insert(TrianglePair(partnerTriangle->Nr, partnerTriangle));
4661 for (int k = 0; k < 3; k++)
[60cbe5]4662 if (triangle->lines[i] == Othertriangle->lines[k]) {
4663 Othertriangle->lines[k] = partnerTriangle->lines[j];
4664 break;
4665 }
4666 // the line of partnerTriangle receives the non-degenerated ones
[c43766]4667 partnerTriangle->lines[j]->triangles.erase(partnerTriangle->Nr);
4668 partnerTriangle->lines[j]->triangles.insert(TrianglePair(Othertriangle->Nr, Othertriangle));
[60cbe5]4669 partnerTriangle->lines[j] = triangle->lines[i];
4670 }
4671
4672 // erase the pair
4673 count += (int) DegeneratedTriangles->erase(triangle->Nr);
[1f2e46]4674 DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *triangle << "." << endl);
[d6b011]4675 RemoveTesselationTriangle(triangle);
[60cbe5]4676 count += (int) DegeneratedTriangles->erase(partnerTriangle->Nr);
[1f2e46]4677 DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *partnerTriangle << "." << endl);
[d6b011]4678 RemoveTesselationTriangle(partnerTriangle);
4679 } else {
[1f2e46]4680 DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() does not remove triangle " << *triangle << " and its partner " << *partnerTriangle << " because it is essential for at" << " least one of the endpoints to be kept in the tesselation structure." << endl);
[d6b011]4681 }
4682 }
[c43766]4683 delete (DegeneratedTriangles);
[cc9225]4684 if (count > 0)
4685 LastTriangle = NULL;
[60cbe5]4686
[1f2e46]4687 DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removed " << count << " triangles:" << endl);
[d6b011]4688}
4689
[60cbe5]4690/** Adds an outside Tesselpoint to the envelope via (two) degenerated triangles.
4691 * We look for the closest point on the boundary, we look through its connected boundary lines and
4692 * seek the one with the minimum angle between its center point and the new point and this base line.
4693 * We open up the line by adding a degenerated triangle, whose other side closes the base line again.
4694 * \param *out output stream for debugging
4695 * \param *point point to add
4696 * \param *LC Linked Cell structure to find nearest point
[f4a346]4697 */
[543ce4]4698void Tesselation::AddBoundaryPointByDegeneratedTriangle(class TesselPoint *point, LinkedCell *LC)
[f4a346]4699{
[c43766]4700 Info FunctionInfo(__func__);
[60cbe5]4701 // find nearest boundary point
4702 class TesselPoint *BackupPoint = NULL;
[ff4611]4703 class TesselPoint *NearestPoint = FindClosestTesselPoint(point->node, BackupPoint, LC);
[60cbe5]4704 class BoundaryPointSet *NearestBoundaryPoint = NULL;
4705 PointMap::iterator PointRunner;
4706
4707 if (NearestPoint == point)
4708 NearestPoint = BackupPoint;
4709 PointRunner = PointsOnBoundary.find(NearestPoint->nr);
4710 if (PointRunner != PointsOnBoundary.end()) {
4711 NearestBoundaryPoint = PointRunner->second;
4712 } else {
[c43766]4713 DoeLog(1) && (eLog() << Verbose(1) << "I cannot find the boundary point." << endl);
[60cbe5]4714 return;
4715 }
[1f2e46]4716 DoLog(0) && (Log() << Verbose(0) << "Nearest point on boundary is " << NearestPoint->Name << "." << endl);
[60cbe5]4717
4718 // go through its lines and find the best one to split
4719 Vector CenterToPoint;
4720 Vector BaseLine;
4721 double angle, BestAngle = 0.;
4722 class BoundaryLineSet *BestLine = NULL;
4723 for (LineMap::iterator Runner = NearestBoundaryPoint->lines.begin(); Runner != NearestBoundaryPoint->lines.end(); Runner++) {
[1f591b]4724 BaseLine = (*Runner->second->endpoints[0]->node->node) -
4725 (*Runner->second->endpoints[1]->node->node);
4726 CenterToPoint = 0.5 * ((*Runner->second->endpoints[0]->node->node) +
4727 (*Runner->second->endpoints[1]->node->node));
4728 CenterToPoint -= (*point->node);
4729 angle = CenterToPoint.Angle(BaseLine);
[60cbe5]4730 if (fabs(angle - M_PI/2.) < fabs(BestAngle - M_PI/2.)) {
4731 BestAngle = angle;
4732 BestLine = Runner->second;
4733 }
[f4a346]4734 }
4735
[60cbe5]4736 // remove one triangle from the chosen line
4737 class BoundaryTriangleSet *TempTriangle = (BestLine->triangles.begin())->second;
4738 BestLine->triangles.erase(TempTriangle->Nr);
4739 int nr = -1;
[c43766]4740 for (int i = 0; i < 3; i++) {
[60cbe5]4741 if (TempTriangle->lines[i] == BestLine) {
4742 nr = i;
4743 break;
4744 }
4745 }
[f4a346]4746
[60cbe5]4747 // create new triangle to connect point (connects automatically with the missing spot of the chosen line)
[1f2e46]4748 DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl);
[60cbe5]4749 AddTesselationPoint((BestLine->endpoints[0]->node), 0);
4750 AddTesselationPoint((BestLine->endpoints[1]->node), 1);
4751 AddTesselationPoint(point, 2);
[1f2e46]4752 DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl);
[086db0]4753 AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
4754 AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
4755 AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
[60cbe5]4756 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
4757 BTS->GetNormalVector(TempTriangle->NormalVector);
4758 BTS->NormalVector.Scale(-1.);
[1f2e46]4759 DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of new triangle is " << BTS->NormalVector << "." << endl);
[60cbe5]4760 AddTesselationTriangle();
4761
4762 // create other side of this triangle and close both new sides of the first created triangle
[1f2e46]4763 DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl);
[60cbe5]4764 AddTesselationPoint((BestLine->endpoints[0]->node), 0);
4765 AddTesselationPoint((BestLine->endpoints[1]->node), 1);
4766 AddTesselationPoint(point, 2);
[1f2e46]4767 DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl);
[086db0]4768 AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
4769 AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
4770 AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
[60cbe5]4771 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
4772 BTS->GetNormalVector(TempTriangle->NormalVector);
[1f2e46]4773 DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of other new triangle is " << BTS->NormalVector << "." << endl);
[60cbe5]4774 AddTesselationTriangle();
4775
4776 // add removed triangle to the last open line of the second triangle
[c43766]4777 for (int i = 0; i < 3; i++) { // look for the same line as BestLine (only it's its degenerated companion)
[60cbe5]4778 if ((BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[0])) && (BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[1]))) {
[c43766]4779 if (BestLine == BTS->lines[i]) {
4780 DoeLog(0) && (eLog() << Verbose(0) << "BestLine is same as found line, something's wrong here!" << endl);
[be2997]4781 performCriticalExit();
[60cbe5]4782 }
[c43766]4783 BTS->lines[i]->triangles.insert(pair<int, class BoundaryTriangleSet *> (TempTriangle->Nr, TempTriangle));
[60cbe5]4784 TempTriangle->lines[nr] = BTS->lines[i];
4785 break;
4786 }
4787 }
[c43766]4788}
4789;
[60cbe5]4790
4791/** Writes the envelope to file.
4792 * \param *out otuput stream for debugging
4793 * \param *filename basename of output file
4794 * \param *cloud PointCloud structure with all nodes
4795 */
[543ce4]4796void Tesselation::Output(const char *filename, const PointCloud * const cloud)
[60cbe5]4797{
[c43766]4798 Info FunctionInfo(__func__);
[60cbe5]4799 ofstream *tempstream = NULL;
4800 string NameofTempFile;
4801 char NumberName[255];
4802
4803 if (LastTriangle != NULL) {
[c43766]4804 sprintf(NumberName, "-%04d-%s_%s_%s", (int) TrianglesOnBoundary.size(), LastTriangle->endpoints[0]->node->Name, LastTriangle->endpoints[1]->node->Name, LastTriangle->endpoints[2]->node->Name);
[60cbe5]4805 if (DoTecplotOutput) {
4806 string NameofTempFile(filename);
4807 NameofTempFile.append(NumberName);
[c43766]4808 for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
4809 NameofTempFile.erase(npos, 1);
[60cbe5]4810 NameofTempFile.append(TecplotSuffix);
[1f2e46]4811 DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n");
[60cbe5]4812 tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
[543ce4]4813 WriteTecplotFile(tempstream, this, cloud, TriangleFilesWritten);
[60cbe5]4814 tempstream->close();
4815 tempstream->flush();
[c43766]4816 delete (tempstream);
[60cbe5]4817 }
4818
4819 if (DoRaster3DOutput) {
4820 string NameofTempFile(filename);
4821 NameofTempFile.append(NumberName);
[c43766]4822 for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
4823 NameofTempFile.erase(npos, 1);
[60cbe5]4824 NameofTempFile.append(Raster3DSuffix);
[1f2e46]4825 DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n");
[60cbe5]4826 tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
[543ce4]4827 WriteRaster3dFile(tempstream, this, cloud);
4828 IncludeSphereinRaster3D(tempstream, this, cloud);
[60cbe5]4829 tempstream->close();
4830 tempstream->flush();
[c43766]4831 delete (tempstream);
[60cbe5]4832 }
4833 }
4834 if (DoTecplotOutput || DoRaster3DOutput)
4835 TriangleFilesWritten++;
[c43766]4836}
4837;
[f7490d]4838
[c43766]4839struct BoundaryPolygonSetCompare
4840{
4841 bool operator()(const BoundaryPolygonSet * s1, const BoundaryPolygonSet * s2) const
4842 {
[ea3627]4843 if (s1->endpoints.size() < s2->endpoints.size())
4844 return true;
4845 else if (s1->endpoints.size() > s2->endpoints.size())
4846 return false;
4847 else { // equality of number of endpoints
4848 PointSet::const_iterator Walker1 = s1->endpoints.begin();
4849 PointSet::const_iterator Walker2 = s2->endpoints.begin();
4850 while ((Walker1 != s1->endpoints.end()) || (Walker2 != s2->endpoints.end())) {
4851 if ((*Walker1)->Nr < (*Walker2)->Nr)
4852 return true;
4853 else if ((*Walker1)->Nr > (*Walker2)->Nr)
4854 return false;
4855 Walker1++;
4856 Walker2++;
4857 }
4858 return false;
4859 }
4860 }
4861};
4862
4863#define UniquePolygonSet set < BoundaryPolygonSet *, BoundaryPolygonSetCompare>
4864
[f7490d]4865/** Finds all degenerated polygons and calls ReTesselateDegeneratedPolygon()/
4866 * \return number of polygons found
4867 */
4868int Tesselation::CorrectAllDegeneratedPolygons()
4869{
4870 Info FunctionInfo(__func__);
[246a3c]4871 /// 2. Go through all BoundaryPointSet's, check their triangles' NormalVector
[eda56a]4872 IndexToIndex *DegeneratedTriangles = FindAllDegeneratedTriangles();
[c43766]4873 set<BoundaryPointSet *> EndpointCandidateList;
4874 pair<set<BoundaryPointSet *>::iterator, bool> InsertionTester;
4875 pair<map<int, Vector *>::iterator, bool> TriangleInsertionTester;
[246a3c]4876 for (PointMap::const_iterator Runner = PointsOnBoundary.begin(); Runner != PointsOnBoundary.end(); Runner++) {
[1f2e46]4877 DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Runner->second << "." << endl);
[c43766]4878 map<int, Vector *> TriangleVectors;
[246a3c]4879 // gather all NormalVectors
[1f2e46]4880 DoLog(1) && (Log() << Verbose(1) << "Gathering triangles ..." << endl);
[246a3c]4881 for (LineMap::const_iterator LineRunner = (Runner->second)->lines.begin(); LineRunner != (Runner->second)->lines.end(); LineRunner++)
4882 for (TriangleMap::const_iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
[65f1dba]4883 if (DegeneratedTriangles->find(TriangleRunner->second->Nr) == DegeneratedTriangles->end()) {
[c43766]4884 TriangleInsertionTester = TriangleVectors.insert(pair<int, Vector *> ((TriangleRunner->second)->Nr, &((TriangleRunner->second)->NormalVector)));
[65f1dba]4885 if (TriangleInsertionTester.second)
[1f2e46]4886 DoLog(1) && (Log() << Verbose(1) << " Adding triangle " << *(TriangleRunner->second) << " to triangles to check-list." << endl);
[65f1dba]4887 } else {
[1f2e46]4888 DoLog(1) && (Log() << Verbose(1) << " NOT adding triangle " << *(TriangleRunner->second) << " as it's a simply degenerated one." << endl);
[65f1dba]4889 }
[246a3c]4890 }
4891 // check whether there are two that are parallel
[1f2e46]4892 DoLog(1) && (Log() << Verbose(1) << "Finding two parallel triangles ..." << endl);
[c43766]4893 for (map<int, Vector *>::iterator VectorWalker = TriangleVectors.begin(); VectorWalker != TriangleVectors.end(); VectorWalker++)
4894 for (map<int, Vector *>::iterator VectorRunner = VectorWalker; VectorRunner != TriangleVectors.end(); VectorRunner++)
[246a3c]4895 if (VectorWalker != VectorRunner) { // skip equals
[0d111b]4896 const double SCP = VectorWalker->second->ScalarProduct(*VectorRunner->second); // ScalarProduct should result in -1. for degenerated triangles
[1f2e46]4897 DoLog(1) && (Log() << Verbose(1) << "Checking " << *VectorWalker->second << " against " << *VectorRunner->second << ": " << SCP << endl);
[246a3c]4898 if (fabs(SCP + 1.) < ParallelEpsilon) {
4899 InsertionTester = EndpointCandidateList.insert((Runner->second));
4900 if (InsertionTester.second)
[1f2e46]4901 DoLog(0) && (Log() << Verbose(0) << " Adding " << *Runner->second << " to endpoint candidate list." << endl);
[246a3c]4902 // and break out of both loops
4903 VectorWalker = TriangleVectors.end();
4904 VectorRunner = TriangleVectors.end();
4905 break;
4906 }
4907 }
4908 }
[5c9ba2]4909 delete DegeneratedTriangles;
[ea3627]4910
[246a3c]4911 /// 3. Find connected endpoint candidates and put them into a polygon
4912 UniquePolygonSet ListofDegeneratedPolygons;
4913 BoundaryPointSet *Walker = NULL;
4914 BoundaryPointSet *OtherWalker = NULL;
4915 BoundaryPolygonSet *Current = NULL;
[c43766]4916 stack<BoundaryPointSet*> ToCheckConnecteds;
[246a3c]4917 while (!EndpointCandidateList.empty()) {
4918 Walker = *(EndpointCandidateList.begin());
[c43766]4919 if (Current == NULL) { // create a new polygon with current candidate
[1f2e46]4920 DoLog(0) && (Log() << Verbose(0) << "Starting new polygon set at point " << *Walker << endl);
[246a3c]4921 Current = new BoundaryPolygonSet;
4922 Current->endpoints.insert(Walker);
4923 EndpointCandidateList.erase(Walker);
4924 ToCheckConnecteds.push(Walker);
[ea3627]4925 }
[f7490d]4926
[246a3c]4927 // go through to-check stack
4928 while (!ToCheckConnecteds.empty()) {
4929 Walker = ToCheckConnecteds.top(); // fetch ...
4930 ToCheckConnecteds.pop(); // ... and remove
4931 for (LineMap::const_iterator LineWalker = Walker->lines.begin(); LineWalker != Walker->lines.end(); LineWalker++) {
4932 OtherWalker = (LineWalker->second)->GetOtherEndpoint(Walker);
[1f2e46]4933 DoLog(1) && (Log() << Verbose(1) << "Checking " << *OtherWalker << endl);
[c43766]4934 set<BoundaryPointSet *>::iterator Finder = EndpointCandidateList.find(OtherWalker);
4935 if (Finder != EndpointCandidateList.end()) { // found a connected partner
[1f2e46]4936 DoLog(1) && (Log() << Verbose(1) << " Adding to polygon." << endl);
[246a3c]4937 Current->endpoints.insert(OtherWalker);
[c43766]4938 EndpointCandidateList.erase(Finder); // remove from candidates
4939 ToCheckConnecteds.push(OtherWalker); // but check its partners too
[ea3627]4940 } else {
[1f2e46]4941 DoLog(1) && (Log() << Verbose(1) << " is not connected to " << *Walker << endl);
[ea3627]4942 }
4943 }
4944 }
[f7490d]4945
[1f2e46]4946 DoLog(0) && (Log() << Verbose(0) << "Final polygon is " << *Current << endl);
[246a3c]4947 ListofDegeneratedPolygons.insert(Current);
4948 Current = NULL;
[f7490d]4949 }
4950
[246a3c]4951 const int counter = ListofDegeneratedPolygons.size();
[f7490d]4952
[1f2e46]4953 DoLog(0) && (Log() << Verbose(0) << "The following " << counter << " degenerated polygons have been found: " << endl);
[246a3c]4954 for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++)
[1f2e46]4955 DoLog(0) && (Log() << Verbose(0) << " " << **PolygonRunner << endl);
[ea3627]4956
[f7490d]4957 /// 4. Go through all these degenerated polygons
[246a3c]4958 for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++) {
[c43766]4959 stack<int> TriangleNrs;
[ea3627]4960 Vector NormalVector;
[f7490d]4961 /// 4a. Gather all triangles of this polygon
[ea3627]4962 TriangleSet *T = (*PolygonRunner)->GetAllContainedTrianglesFromEndpoints();
[f7490d]4963
[0310b8]4964 // check whether number is bigger than 2, otherwise it's just a simply degenerated one and nothing to do.
[65f1dba]4965 if (T->size() == 2) {
[1f2e46]4966 DoLog(1) && (Log() << Verbose(1) << " Skipping degenerated polygon, is just a (already simply degenerated) triangle." << endl);
[c43766]4967 delete (T);
[65f1dba]4968 continue;
4969 }
4970
[0310b8]4971 // check whether number is even
4972 // If this case occurs, we have to think about it!
4973 // The Problem is probably due to two degenerated polygons being connected by a bridging, non-degenerated polygon, as somehow one node has
4974 // connections to either polygon ...
4975 if (T->size() % 2 != 0) {
[c43766]4976 DoeLog(0) && (eLog() << Verbose(0) << " degenerated polygon contains an odd number of triangles, probably contains bridging non-degenerated ones, too!" << endl);
[0310b8]4977 performCriticalExit();
4978 }
[c43766]4979 TriangleSet::iterator TriangleWalker = T->begin(); // is the inner iterator
[f7490d]4980 /// 4a. Get NormalVector for one side (this is "front")
[1f591b]4981 NormalVector = (*TriangleWalker)->NormalVector;
[1f2e46]4982 DoLog(1) && (Log() << Verbose(1) << "\"front\" defining triangle is " << **TriangleWalker << " and Normal vector of \"front\" side is " << NormalVector << endl);
[ea3627]4983 TriangleWalker++;
4984 TriangleSet::iterator TriangleSprinter = TriangleWalker; // is the inner advanced iterator
[f7490d]4985 /// 4b. Remove all triangles whose NormalVector is in opposite direction (i.e. "back")
[ea3627]4986 BoundaryTriangleSet *triangle = NULL;
4987 while (TriangleSprinter != T->end()) {
4988 TriangleWalker = TriangleSprinter;
4989 triangle = *TriangleWalker;
4990 TriangleSprinter++;
[1f2e46]4991 DoLog(1) && (Log() << Verbose(1) << "Current triangle to test for removal: " << *triangle << endl);
[1f591b]4992 if (triangle->NormalVector.ScalarProduct(NormalVector) < 0) { // if from other side, then delete and remove from list
[1f2e46]4993 DoLog(1) && (Log() << Verbose(1) << " Removing ... " << endl);
[ea3627]4994 TriangleNrs.push(triangle->Nr);
[f7490d]4995 T->erase(TriangleWalker);
[ea3627]4996 RemoveTesselationTriangle(triangle);
4997 } else
[1f2e46]4998 DoLog(1) && (Log() << Verbose(1) << " Keeping ... " << endl);
[f7490d]4999 }
5000 /// 4c. Copy all "front" triangles but with inverse NormalVector
5001 TriangleWalker = T->begin();
[c43766]5002 while (TriangleWalker != T->end()) { // go through all front triangles
[1f2e46]5003 DoLog(1) && (Log() << Verbose(1) << " Re-creating triangle " << **TriangleWalker << " with NormalVector " << (*TriangleWalker)->NormalVector << endl);
[ea3627]5004 for (int i = 0; i < 3; i++)
5005 AddTesselationPoint((*TriangleWalker)->endpoints[i]->node, i);
[086db0]5006 AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
5007 AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
5008 AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
[246a3c]5009 if (TriangleNrs.empty())
[c43766]5010 DoeLog(0) && (eLog() << Verbose(0) << "No more free triangle numbers!" << endl);
[ea3627]5011 BTS = new BoundaryTriangleSet(BLS, TriangleNrs.top()); // copy triangle ...
5012 AddTesselationTriangle(); // ... and add
5013 TriangleNrs.pop();
[1f591b]5014 BTS->NormalVector = -1 * (*TriangleWalker)->NormalVector;
[f7490d]5015 TriangleWalker++;
5016 }
[ea3627]5017 if (!TriangleNrs.empty()) {
[c43766]5018 DoeLog(0) && (eLog() << Verbose(0) << "There have been less triangles created than removed!" << endl);
[ea3627]5019 }
[c43766]5020 delete (T); // remove the triangleset
[f7490d]5021 }
[eda56a]5022 IndexToIndex * SimplyDegeneratedTriangles = FindAllDegeneratedTriangles();
[1f2e46]5023 DoLog(0) && (Log() << Verbose(0) << "Final list of simply degenerated triangles found, containing " << SimplyDegeneratedTriangles->size() << " triangles:" << endl);
[eda56a]5024 IndexToIndex::iterator it;
[ea3627]5025 for (it = SimplyDegeneratedTriangles->begin(); it != SimplyDegeneratedTriangles->end(); it++)
[1f2e46]5026 DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl);
[c43766]5027 delete (SimplyDegeneratedTriangles);
[f7490d]5028 /// 5. exit
[ea3627]5029 UniquePolygonSet::iterator PolygonRunner;
[246a3c]5030 while (!ListofDegeneratedPolygons.empty()) {
5031 PolygonRunner = ListofDegeneratedPolygons.begin();
[c43766]5032 delete (*PolygonRunner);
[246a3c]5033 ListofDegeneratedPolygons.erase(PolygonRunner);
[f7490d]5034 }
5035
5036 return counter;
[c43766]5037}
5038;
Note: See TracBrowser for help on using the repository browser.