source: src/tesselation.cpp@ bab12a

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 bab12a was 68f03d, checked in by Tillmann Crueger <crueger@…>, 15 years ago

FIX: Memory corruption in particleInfo class

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