source: src/tesselation.cpp@ 2f40c0e

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 2f40c0e was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

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