source: src/tesselation.cpp@ 215df0

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

Simplified some methods.

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