source: src/tesselation.cpp@ cd5047

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

Merge branch 'StructureRefactoring' into stable

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/periodentafel.cpp

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