source: src/tesselation.cpp@ 1ac51b

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 1ac51b was 8f215d, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Simplified tesselation.cpp by allowing direct access to derived triangle internals

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