source: src/tesselation.cpp@ bfd839

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

Removed the DistanceToPlane method in the Vector class in favor of a method that computes the distance to arbitrary spaces

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