source: src/tesselation.cpp@ 0d1ad0

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 0d1ad0 was 6d574a, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Replaced several old-style asserts with more usable ASSERTs

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