source: src/tesselation.cpp@ 0a4f7f

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 0a4f7f was 0a4f7f, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made data internal data-structure of vector class private

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