source: src/tesselation.cpp@ 99c484

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 Candidate_v1.7.0 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 99c484 was 99c484, checked in by Frederik Heber <heber@…>, 16 years ago

Merge branch 'VectorUnitTest' into ConcaveHull

  • Property mode set to 100644
File size: 127.1 KB
Line 
1/*
2 * tesselation.cpp
3 *
4 * Created on: Aug 3, 2009
5 * Author: heber
6 */
7
8#include "tesselation.hpp"
9
10// ======================================== Points on Boundary =================================
11
12/** Constructor of BoundaryPointSet.
13 */
14BoundaryPointSet::BoundaryPointSet()
15{
16 LinesCount = 0;
17 Nr = -1;
18};
19
20/** Constructor of BoundaryPointSet with Tesselpoint.
21 * \param *Walker TesselPoint this boundary point represents
22 */
23BoundaryPointSet::BoundaryPointSet(TesselPoint *Walker)
24{
25 node = Walker;
26 LinesCount = 0;
27 Nr = Walker->nr;
28};
29
30/** Destructor of BoundaryPointSet.
31 * Sets node to NULL to avoid removing the original, represented TesselPoint.
32 * \note When removing point from a class Tesselation, use RemoveTesselationPoint()
33 */
34BoundaryPointSet::~BoundaryPointSet()
35{
36 cout << Verbose(5) << "Erasing point nr. " << Nr << "." << endl;
37 if (!lines.empty())
38 cerr << "WARNING: Memory Leak! I " << *this << " am still connected to some lines." << endl;
39 node = NULL;
40};
41
42/** Add a line to the LineMap of this point.
43 * \param *line line to add
44 */
45void BoundaryPointSet::AddLine(class BoundaryLineSet *line)
46{
47 cout << Verbose(6) << "Adding " << *this << " to line " << *line << "."
48 << endl;
49 if (line->endpoints[0] == this)
50 {
51 lines.insert(LinePair(line->endpoints[1]->Nr, line));
52 }
53 else
54 {
55 lines.insert(LinePair(line->endpoints[0]->Nr, line));
56 }
57 LinesCount++;
58};
59
60/** output operator for BoundaryPointSet.
61 * \param &ost output stream
62 * \param &a boundary point
63 */
64ostream & operator <<(ostream &ost, BoundaryPointSet &a)
65{
66 ost << "[" << a.Nr << "|" << a.node->Name << "]";
67 return ost;
68}
69;
70
71// ======================================== Lines on Boundary =================================
72
73/** Constructor of BoundaryLineSet.
74 */
75BoundaryLineSet::BoundaryLineSet()
76{
77 for (int i = 0; i < 2; i++)
78 endpoints[i] = NULL;
79 Nr = -1;
80};
81
82/** Constructor of BoundaryLineSet with two endpoints.
83 * Adds line automatically to each endpoints' LineMap
84 * \param *Point[2] array of two boundary points
85 * \param number number of the list
86 */
87BoundaryLineSet::BoundaryLineSet(class BoundaryPointSet *Point[2], int number)
88{
89 // set number
90 Nr = number;
91 // set endpoints in ascending order
92 SetEndpointsOrdered(endpoints, Point[0], Point[1]);
93 // add this line to the hash maps of both endpoints
94 Point[0]->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
95 Point[1]->AddLine(this); //
96 // clear triangles list
97 cout << Verbose(5) << "New Line with endpoints " << *this << "." << endl;
98};
99
100/** Destructor for BoundaryLineSet.
101 * Removes itself from each endpoints' LineMap, calling RemoveTrianglePoint() when point not connected anymore.
102 * \note When removing lines from a class Tesselation, use RemoveTesselationLine()
103 */
104BoundaryLineSet::~BoundaryLineSet()
105{
106 int Numbers[2];
107
108 // get other endpoint number of finding copies of same line
109 if (endpoints[1] != NULL)
110 Numbers[0] = endpoints[1]->Nr;
111 else
112 Numbers[0] = -1;
113 if (endpoints[0] != NULL)
114 Numbers[1] = endpoints[0]->Nr;
115 else
116 Numbers[1] = -1;
117
118 for (int i = 0; i < 2; i++) {
119 if (endpoints[i] != NULL) {
120 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
121 pair<LineMap::iterator, LineMap::iterator> erasor = endpoints[i]->lines.equal_range(Numbers[i]);
122 for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
123 if ((*Runner).second == this) {
124 cout << Verbose(5) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
125 endpoints[i]->lines.erase(Runner);
126 break;
127 }
128 } else { // there's just a single line left
129 if (endpoints[i]->lines.erase(Nr))
130 cout << Verbose(5) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
131 }
132 if (endpoints[i]->lines.empty()) {
133 cout << Verbose(5) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl;
134 if (endpoints[i] != NULL) {
135 delete(endpoints[i]);
136 endpoints[i] = NULL;
137 }
138 }
139 }
140 }
141 if (!triangles.empty())
142 cerr << "WARNING: Memory Leak! I " << *this << " am still connected to some triangles." << endl;
143};
144
145/** Add triangle to TriangleMap of this boundary line.
146 * \param *triangle to add
147 */
148void BoundaryLineSet::AddTriangle(class BoundaryTriangleSet *triangle)
149{
150 cout << Verbose(6) << "Add " << triangle->Nr << " to line " << *this << "."
151 << endl;
152 triangles.insert(TrianglePair(triangle->Nr, triangle));
153};
154
155/** Checks whether we have a common endpoint with given \a *line.
156 * \param *line other line to test
157 * \return true - common endpoint present, false - not connected
158 */
159bool BoundaryLineSet::IsConnectedTo(class BoundaryLineSet *line)
160{
161 if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1]))
162 return true;
163 else
164 return false;
165};
166
167/** Checks whether the adjacent triangles of a baseline are convex or not.
168 * We sum the two angles of each normal vector with a ficticious normnal vector from this baselinbe pointing outwards.
169 * If greater/equal M_PI than we are convex.
170 * \param *out output stream for debugging
171 * \return true - triangles are convex, false - concave or less than two triangles connected
172 */
173bool BoundaryLineSet::CheckConvexityCriterion(ofstream *out)
174{
175 Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck;
176 // get the two triangles
177 if (triangles.size() != 2) {
178 *out << Verbose(1) << "ERROR: Baseline " << *this << " is connect to less than two triangles, Tesselation incomplete!" << endl;
179 return false;
180 }
181 // check normal vectors
182 // have a normal vector on the base line pointing outwards
183 *out << Verbose(3) << "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << "." << endl;
184 BaseLineCenter.CopyVector(endpoints[0]->node->node);
185 BaseLineCenter.AddVector(endpoints[1]->node->node);
186 BaseLineCenter.Scale(1./2.);
187 BaseLine.CopyVector(endpoints[0]->node->node);
188 BaseLine.SubtractVector(endpoints[1]->node->node);
189 *out << Verbose(3) << "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << "." << endl;
190
191 BaseLineNormal.Zero();
192 NormalCheck.Zero();
193 double sign = -1.;
194 int i=0;
195 class BoundaryPointSet *node = NULL;
196 for(TriangleMap::iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
197 *out << Verbose(3) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl;
198 NormalCheck.AddVector(&runner->second->NormalVector);
199 NormalCheck.Scale(sign);
200 sign = -sign;
201 BaseLineNormal.SubtractVector(&runner->second->NormalVector); // we subtract as BaseLineNormal has to point inward in direction of [pi,2pi]
202 node = runner->second->GetThirdEndpoint(this);
203 if (node != NULL) {
204 *out << Verbose(3) << "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << "." << endl;
205 helper[i].CopyVector(node->node->node);
206 helper[i].SubtractVector(&BaseLineCenter);
207 helper[i].MakeNormalVector(&BaseLine); // we want to compare the triangle's heights' angles!
208 *out << Verbose(4) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl;
209 i++;
210 } else {
211 *out << Verbose(2) << "WARNING: I cannot find third node in triangle, something's wrong." << endl;
212 return true;
213 }
214 }
215 *out << Verbose(3) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl;
216 if (NormalCheck.NormSquared() < MYEPSILON) {
217 *out << Verbose(3) << "Normalvectors of both triangles are the same: convex." << endl;
218 return true;
219 }
220 double angle = getAngle(helper[0], helper[1], BaseLineNormal);
221 if ((angle - M_PI) > -MYEPSILON)
222 return true;
223 else
224 return false;
225}
226
227/** Checks whether point is any of the two endpoints this line contains.
228 * \param *point point to test
229 * \return true - point is of the line, false - is not
230 */
231bool BoundaryLineSet::ContainsBoundaryPoint(class BoundaryPointSet *point)
232{
233 for(int i=0;i<2;i++)
234 if (point == endpoints[i])
235 return true;
236 return false;
237};
238
239/** Returns other endpoint of the line.
240 * \param *point other endpoint
241 * \return NULL - if endpoint not contained in BoundaryLineSet, or pointer to BoundaryPointSet otherwise
242 */
243class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(class BoundaryPointSet *point)
244{
245 if (endpoints[0] == point)
246 return endpoints[1];
247 else if (endpoints[1] == point)
248 return endpoints[0];
249 else
250 return NULL;
251};
252
253/** output operator for BoundaryLineSet.
254 * \param &ost output stream
255 * \param &a boundary line
256 */
257ostream & operator <<(ostream &ost, BoundaryLineSet &a)
258{
259 ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << "," << a.endpoints[1]->node->Name << "]";
260 return ost;
261};
262
263// ======================================== Triangles on Boundary =================================
264
265/** Constructor for BoundaryTriangleSet.
266 */
267BoundaryTriangleSet::BoundaryTriangleSet()
268{
269 for (int i = 0; i < 3; i++)
270 {
271 endpoints[i] = NULL;
272 lines[i] = NULL;
273 }
274 Nr = -1;
275};
276
277/** Constructor for BoundaryTriangleSet with three lines.
278 * \param *line[3] lines that make up the triangle
279 * \param number number of triangle
280 */
281BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet *line[3], int number)
282{
283 // set number
284 Nr = number;
285 // set lines
286 cout << Verbose(5) << "New triangle " << Nr << ":" << endl;
287 for (int i = 0; i < 3; i++)
288 {
289 lines[i] = line[i];
290 lines[i]->AddTriangle(this);
291 }
292 // get ascending order of endpoints
293 map<int, class BoundaryPointSet *> OrderMap;
294 for (int i = 0; i < 3; i++)
295 // for all three lines
296 for (int j = 0; j < 2; j++)
297 { // for both endpoints
298 OrderMap.insert(pair<int, class BoundaryPointSet *> (
299 line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
300 // and we don't care whether insertion fails
301 }
302 // set endpoints
303 int Counter = 0;
304 cout << Verbose(6) << " with end points ";
305 for (map<int, class BoundaryPointSet *>::iterator runner = OrderMap.begin(); runner
306 != OrderMap.end(); runner++)
307 {
308 endpoints[Counter] = runner->second;
309 cout << " " << *endpoints[Counter];
310 Counter++;
311 }
312 if (Counter < 3)
313 {
314 cerr << "ERROR! We have a triangle with only two distinct endpoints!"
315 << endl;
316 //exit(1);
317 }
318 cout << "." << endl;
319};
320
321/** Destructor of BoundaryTriangleSet.
322 * Removes itself from each of its lines' LineMap and removes them if necessary.
323 * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle()
324 */
325BoundaryTriangleSet::~BoundaryTriangleSet()
326{
327 for (int i = 0; i < 3; i++) {
328 if (lines[i] != NULL) {
329 if (lines[i]->triangles.erase(Nr))
330 cout << Verbose(5) << "Triangle Nr." << Nr << " erased in line " << *lines[i] << "." << endl;
331 if (lines[i]->triangles.empty()) {
332 cout << Verbose(5) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
333 delete (lines[i]);
334 lines[i] = NULL;
335 }
336 }
337 }
338 cout << Verbose(5) << "Erasing triangle Nr." << Nr << " itself." << endl;
339};
340
341/** Calculates the normal vector for this triangle.
342 * Is made unique by comparison with \a OtherVector to point in the other direction.
343 * \param &OtherVector direction vector to make normal vector unique.
344 */
345void BoundaryTriangleSet::GetNormalVector(Vector &OtherVector)
346{
347 // get normal vector
348 NormalVector.MakeNormalVector(endpoints[0]->node->node, endpoints[1]->node->node, endpoints[2]->node->node);
349
350 // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
351 if (NormalVector.ScalarProduct(&OtherVector) > 0.)
352 NormalVector.Scale(-1.);
353};
354
355/** Finds the point on the triangle \a *BTS the line defined by \a *MolCenter and \a *x crosses through.
356 * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane
357 * This we test if it's really on the plane and whether it's inside the triangle on the plane or not.
358 * The latter is done as follows: if it's really outside, then for any endpoint of the triangle and it's opposite
359 * base line, the intersection between the line from endpoint to intersection and the base line will have a Vector::NormSquared()
360 * smaller than the first line.
361 * \param *out output stream for debugging
362 * \param *MolCenter offset vector of line
363 * \param *x second endpoint of line, minus \a *MolCenter is directional vector of line
364 * \param *Intersection intersection on plane on return
365 * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle.
366 */
367bool BoundaryTriangleSet::GetIntersectionInsideTriangle(ofstream *out, Vector *MolCenter, Vector *x, Vector *Intersection)
368{
369 Vector CrossPoint;
370 Vector helper;
371
372 if (!Intersection->GetIntersectionWithPlane(out, &NormalVector, endpoints[0]->node->node, MolCenter, x)) {
373 *out << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl;
374 return false;
375 }
376
377 // Calculate cross point between one baseline and the line from the third endpoint to intersection
378 int i=0;
379 do {
380 if (CrossPoint.GetIntersectionOfTwoLinesOnPlane(out, endpoints[i%3]->node->node, endpoints[(i+1)%3]->node->node, endpoints[(i+2)%3]->node->node, Intersection, &NormalVector)) {
381 helper.CopyVector(endpoints[(i+1)%3]->node->node);
382 helper.SubtractVector(endpoints[i%3]->node->node);
383 } else
384 i++;
385 if (i>2)
386 break;
387 } while (CrossPoint.NormSquared() < MYEPSILON);
388 if (i==3) {
389 *out << Verbose(1) << "ERROR: Could not find any cross points, something's utterly wrong here!" << endl;
390 exit(255);
391 }
392 CrossPoint.SubtractVector(endpoints[i%3]->node->node);
393
394 // check whether intersection is inside or not by comparing length of intersection and length of cross point
395 if ((CrossPoint.NormSquared() - helper.NormSquared()) > -MYEPSILON) { // inside
396 return true;
397 } else { // outside!
398 Intersection->Zero();
399 return false;
400 }
401};
402
403/** Checks whether lines is any of the three boundary lines this triangle contains.
404 * \param *line line to test
405 * \return true - line is of the triangle, false - is not
406 */
407bool BoundaryTriangleSet::ContainsBoundaryLine(class BoundaryLineSet *line)
408{
409 for(int i=0;i<3;i++)
410 if (line == lines[i])
411 return true;
412 return false;
413};
414
415/** Checks whether point is any of the three endpoints this triangle contains.
416 * \param *point point to test
417 * \return true - point is of the triangle, false - is not
418 */
419bool BoundaryTriangleSet::ContainsBoundaryPoint(class BoundaryPointSet *point)
420{
421 for(int i=0;i<3;i++)
422 if (point == endpoints[i])
423 return true;
424 return false;
425};
426
427/** Checks whether three given \a *Points coincide with triangle's endpoints.
428 * \param *Points[3] pointer to BoundaryPointSet
429 * \return true - is the very triangle, false - is not
430 */
431bool BoundaryTriangleSet::IsPresentTupel(class BoundaryPointSet *Points[3])
432{
433 return (((endpoints[0] == Points[0])
434 || (endpoints[0] == Points[1])
435 || (endpoints[0] == Points[2])
436 ) && (
437 (endpoints[1] == Points[0])
438 || (endpoints[1] == Points[1])
439 || (endpoints[1] == Points[2])
440 ) && (
441 (endpoints[2] == Points[0])
442 || (endpoints[2] == Points[1])
443 || (endpoints[2] == Points[2])
444
445 ));
446};
447
448/** Returns the endpoint which is not contained in the given \a *line.
449 * \param *line baseline defining two endpoints
450 * \return pointer third endpoint or NULL if line does not belong to triangle.
451 */
452class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(class BoundaryLineSet *line)
453{
454 // sanity check
455 if (!ContainsBoundaryLine(line))
456 return NULL;
457 for(int i=0;i<3;i++)
458 if (!line->ContainsBoundaryPoint(endpoints[i]))
459 return endpoints[i];
460 // actually, that' impossible :)
461 return NULL;
462};
463
464/** Calculates the center point of the triangle.
465 * Is third of the sum of all endpoints.
466 * \param *center central point on return.
467 */
468void BoundaryTriangleSet::GetCenter(Vector *center)
469{
470 center->Zero();
471 for(int i=0;i<3;i++)
472 center->AddVector(endpoints[i]->node->node);
473 center->Scale(1./3.);
474}
475
476/** output operator for BoundaryTriangleSet.
477 * \param &ost output stream
478 * \param &a boundary triangle
479 */
480ostream &operator <<(ostream &ost, BoundaryTriangleSet &a)
481{
482 ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << ","
483 << a.endpoints[1]->node->Name << "," << a.endpoints[2]->node->Name << "]";
484 return ost;
485};
486
487// =========================================================== class TESSELPOINT ===========================================
488
489/** Constructor of class TesselPoint.
490 */
491TesselPoint::TesselPoint()
492{
493 node = NULL;
494 nr = -1;
495 Name = NULL;
496};
497
498/** Destructor for class TesselPoint.
499 */
500TesselPoint::~TesselPoint()
501{
502 Free((void **)&Name, "TesselPoint::~TesselPoint: *Name");
503};
504
505/** Prints LCNode to screen.
506 */
507ostream & operator << (ostream &ost, const TesselPoint &a)
508{
509 ost << "[" << (a.Name) << "|" << &a << "]";
510 return ost;
511};
512
513/** Prints LCNode to screen.
514 */
515ostream & TesselPoint::operator << (ostream &ost)
516{
517 ost << "[" << (Name) << "|" << this << "]";
518 return ost;
519};
520
521
522// =========================================================== class POINTCLOUD ============================================
523
524/** Constructor of class PointCloud.
525 */
526PointCloud::PointCloud()
527{
528
529};
530
531/** Destructor for class PointCloud.
532 */
533PointCloud::~PointCloud()
534{
535
536};
537
538// ============================ CandidateForTesselation =============================
539
540/** Constructor of class CandidateForTesselation.
541 */
542CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) {
543 point = candidate;
544 BaseLine = line;
545 OptCenter.CopyVector(&OptCandidateCenter);
546 OtherOptCenter.CopyVector(&OtherOptCandidateCenter);
547};
548
549/** Destructor for class CandidateForTesselation.
550 */
551CandidateForTesselation::~CandidateForTesselation() {
552 point = NULL;
553 BaseLine = NULL;
554};
555
556// =========================================================== class TESSELATION ===========================================
557
558/** Constructor of class Tesselation.
559 */
560Tesselation::Tesselation()
561{
562 PointsOnBoundaryCount = 0;
563 LinesOnBoundaryCount = 0;
564 TrianglesOnBoundaryCount = 0;
565 InternalPointer = PointsOnBoundary.begin();
566}
567;
568
569/** Destructor of class Tesselation.
570 * We have to free all points, lines and triangles.
571 */
572Tesselation::~Tesselation()
573{
574 cout << Verbose(1) << "Free'ing TesselStruct ... " << endl;
575 for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner != TrianglesOnBoundary.end(); runner++) {
576 if (runner->second != NULL) {
577 delete (runner->second);
578 runner->second = NULL;
579 } else
580 cerr << "ERROR: The triangle " << runner->first << " has already been free'd." << endl;
581 }
582}
583;
584
585/** PointCloud implementation of GetCenter
586 * Uses PointsOnBoundary and STL stuff.
587 */
588Vector * Tesselation::GetCenter(ofstream *out)
589{
590 Vector *Center = new Vector(0.,0.,0.);
591 int num=0;
592 for (GoToFirst(); (!IsEnd()); GoToNext()) {
593 Center->AddVector(GetPoint()->node);
594 num++;
595 }
596 Center->Scale(1./num);
597 return Center;
598};
599
600/** PointCloud implementation of GoPoint
601 * Uses PointsOnBoundary and STL stuff.
602 */
603TesselPoint * Tesselation::GetPoint()
604{
605 return (InternalPointer->second->node);
606};
607
608/** PointCloud implementation of GetTerminalPoint.
609 * Uses PointsOnBoundary and STL stuff.
610 */
611TesselPoint * Tesselation::GetTerminalPoint()
612{
613 PointMap::iterator Runner = PointsOnBoundary.end();
614 Runner--;
615 return (Runner->second->node);
616};
617
618/** PointCloud implementation of GoToNext.
619 * Uses PointsOnBoundary and STL stuff.
620 */
621void Tesselation::GoToNext()
622{
623 if (InternalPointer != PointsOnBoundary.end())
624 InternalPointer++;
625};
626
627/** PointCloud implementation of GoToPrevious.
628 * Uses PointsOnBoundary and STL stuff.
629 */
630void Tesselation::GoToPrevious()
631{
632 if (InternalPointer != PointsOnBoundary.begin())
633 InternalPointer--;
634};
635
636/** PointCloud implementation of GoToFirst.
637 * Uses PointsOnBoundary and STL stuff.
638 */
639void Tesselation::GoToFirst()
640{
641 InternalPointer = PointsOnBoundary.begin();
642};
643
644/** PointCloud implementation of GoToLast.
645 * Uses PointsOnBoundary and STL stuff.
646 */
647void Tesselation::GoToLast()
648{
649 InternalPointer = PointsOnBoundary.end();
650 InternalPointer--;
651};
652
653/** PointCloud implementation of IsEmpty.
654 * Uses PointsOnBoundary and STL stuff.
655 */
656bool Tesselation::IsEmpty()
657{
658 return (PointsOnBoundary.empty());
659};
660
661/** PointCloud implementation of IsLast.
662 * Uses PointsOnBoundary and STL stuff.
663 */
664bool Tesselation::IsEnd()
665{
666 return (InternalPointer == PointsOnBoundary.end());
667};
668
669
670/** Gueses first starting triangle of the convex envelope.
671 * We guess the starting triangle by taking the smallest distance between two points and looking for a fitting third.
672 * \param *out output stream for debugging
673 * \param PointsOnBoundary set of boundary points defining the convex envelope of the cluster
674 */
675void
676Tesselation::GuessStartingTriangle(ofstream *out)
677{
678 // 4b. create a starting triangle
679 // 4b1. create all distances
680 DistanceMultiMap DistanceMMap;
681 double distance, tmp;
682 Vector PlaneVector, TrialVector;
683 PointMap::iterator A, B, C; // three nodes of the first triangle
684 A = PointsOnBoundary.begin(); // the first may be chosen arbitrarily
685
686 // with A chosen, take each pair B,C and sort
687 if (A != PointsOnBoundary.end())
688 {
689 B = A;
690 B++;
691 for (; B != PointsOnBoundary.end(); B++)
692 {
693 C = B;
694 C++;
695 for (; C != PointsOnBoundary.end(); C++)
696 {
697 tmp = A->second->node->node->DistanceSquared(B->second->node->node);
698 distance = tmp * tmp;
699 tmp = A->second->node->node->DistanceSquared(C->second->node->node);
700 distance += tmp * tmp;
701 tmp = B->second->node->node->DistanceSquared(C->second->node->node);
702 distance += tmp * tmp;
703 DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C)));
704 }
705 }
706 }
707 // // listing distances
708 // *out << Verbose(1) << "Listing DistanceMMap:";
709 // for(DistanceMultiMap::iterator runner = DistanceMMap.begin(); runner != DistanceMMap.end(); runner++) {
710 // *out << " " << runner->first << "(" << *runner->second.first->second << ", " << *runner->second.second->second << ")";
711 // }
712 // *out << endl;
713 // 4b2. pick three baselines forming a triangle
714 // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
715 DistanceMultiMap::iterator baseline = DistanceMMap.begin();
716 for (; baseline != DistanceMMap.end(); baseline++)
717 {
718 // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
719 // 2. next, we have to check whether all points reside on only one side of the triangle
720 // 3. construct plane vector
721 PlaneVector.MakeNormalVector(A->second->node->node,
722 baseline->second.first->second->node->node,
723 baseline->second.second->second->node->node);
724 *out << Verbose(2) << "Plane vector of candidate triangle is ";
725 PlaneVector.Output(out);
726 *out << endl;
727 // 4. loop over all points
728 double sign = 0.;
729 PointMap::iterator checker = PointsOnBoundary.begin();
730 for (; checker != PointsOnBoundary.end(); checker++)
731 {
732 // (neglecting A,B,C)
733 if ((checker == A) || (checker == baseline->second.first) || (checker
734 == baseline->second.second))
735 continue;
736 // 4a. project onto plane vector
737 TrialVector.CopyVector(checker->second->node->node);
738 TrialVector.SubtractVector(A->second->node->node);
739 distance = TrialVector.ScalarProduct(&PlaneVector);
740 if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
741 continue;
742 *out << Verbose(3) << "Projection of " << checker->second->node->Name
743 << " yields distance of " << distance << "." << endl;
744 tmp = distance / fabs(distance);
745 // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
746 if ((sign != 0) && (tmp != sign))
747 {
748 // 4c. If so, break 4. loop and continue with next candidate in 1. loop
749 *out << Verbose(2) << "Current candidates: "
750 << A->second->node->Name << ","
751 << baseline->second.first->second->node->Name << ","
752 << baseline->second.second->second->node->Name << " leaves "
753 << checker->second->node->Name << " outside the convex hull."
754 << endl;
755 break;
756 }
757 else
758 { // note the sign for later
759 *out << Verbose(2) << "Current candidates: "
760 << A->second->node->Name << ","
761 << baseline->second.first->second->node->Name << ","
762 << baseline->second.second->second->node->Name << " leave "
763 << checker->second->node->Name << " inside the convex hull."
764 << endl;
765 sign = tmp;
766 }
767 // 4d. Check whether the point is inside the triangle (check distance to each node
768 tmp = checker->second->node->node->DistanceSquared(A->second->node->node);
769 int innerpoint = 0;
770 if ((tmp < A->second->node->node->DistanceSquared(
771 baseline->second.first->second->node->node)) && (tmp
772 < A->second->node->node->DistanceSquared(
773 baseline->second.second->second->node->node)))
774 innerpoint++;
775 tmp = checker->second->node->node->DistanceSquared(
776 baseline->second.first->second->node->node);
777 if ((tmp < baseline->second.first->second->node->node->DistanceSquared(
778 A->second->node->node)) && (tmp
779 < baseline->second.first->second->node->node->DistanceSquared(
780 baseline->second.second->second->node->node)))
781 innerpoint++;
782 tmp = checker->second->node->node->DistanceSquared(
783 baseline->second.second->second->node->node);
784 if ((tmp < baseline->second.second->second->node->node->DistanceSquared(
785 baseline->second.first->second->node->node)) && (tmp
786 < baseline->second.second->second->node->node->DistanceSquared(
787 A->second->node->node)))
788 innerpoint++;
789 // 4e. If so, break 4. loop and continue with next candidate in 1. loop
790 if (innerpoint == 3)
791 break;
792 }
793 // 5. come this far, all on same side? Then break 1. loop and construct triangle
794 if (checker == PointsOnBoundary.end())
795 {
796 *out << "Looks like we have a candidate!" << endl;
797 break;
798 }
799 }
800 if (baseline != DistanceMMap.end())
801 {
802 BPS[0] = baseline->second.first->second;
803 BPS[1] = baseline->second.second->second;
804 BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
805 BPS[0] = A->second;
806 BPS[1] = baseline->second.second->second;
807 BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
808 BPS[0] = baseline->second.first->second;
809 BPS[1] = A->second;
810 BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
811
812 // 4b3. insert created triangle
813 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
814 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
815 TrianglesOnBoundaryCount++;
816 for (int i = 0; i < NDIM; i++)
817 {
818 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
819 LinesOnBoundaryCount++;
820 }
821
822 *out << Verbose(1) << "Starting triangle is " << *BTS << "." << endl;
823 }
824 else
825 {
826 *out << Verbose(1) << "No starting triangle found." << endl;
827 exit(255);
828 }
829}
830;
831
832/** Tesselates the convex envelope of a cluster from a single starting triangle.
833 * The starting triangle is made out of three baselines. Each line in the final tesselated cluster may belong to at most
834 * 2 triangles. Hence, we go through all current lines:
835 * -# if the lines contains to only one triangle
836 * -# We search all points in the boundary
837 * -# if the triangle is in forward direction of the baseline (at most 90 degrees angle between vector orthogonal to
838 * baseline in triangle plane pointing out of the triangle and normal vector of new triangle)
839 * -# if the triangle with the baseline and the current point has the smallest of angles (comparison between normal vectors)
840 * -# then we have a new triangle, whose baselines we again add (or increase their TriangleCount)
841 * \param *out output stream for debugging
842 * \param *configuration for IsAngstroem
843 * \param *cloud cluster of points
844 */
845void Tesselation::TesselateOnBoundary(ofstream *out, PointCloud *cloud)
846{
847 bool flag;
848 PointMap::iterator winner;
849 class BoundaryPointSet *peak = NULL;
850 double SmallestAngle, TempAngle;
851 Vector NormalVector, VirtualNormalVector, CenterVector, TempVector, helper, PropagationVector, *Center = NULL;
852 LineMap::iterator LineChecker[2];
853
854 Center = cloud->GetCenter(out);
855 // create a first tesselation with the given BoundaryPoints
856 do {
857 flag = false;
858 for (LineMap::iterator baseline = LinesOnBoundary.begin(); baseline != LinesOnBoundary.end(); baseline++)
859 if (baseline->second->triangles.size() == 1) {
860 // 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)
861 SmallestAngle = M_PI;
862
863 // get peak point with respect to this base line's only triangle
864 BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far
865 *out << Verbose(2) << "Current baseline is between " << *(baseline->second) << "." << endl;
866 for (int i = 0; i < 3; i++)
867 if ((BTS->endpoints[i] != baseline->second->endpoints[0]) && (BTS->endpoints[i] != baseline->second->endpoints[1]))
868 peak = BTS->endpoints[i];
869 *out << Verbose(3) << " and has peak " << *peak << "." << endl;
870
871 // prepare some auxiliary vectors
872 Vector BaseLineCenter, BaseLine;
873 BaseLineCenter.CopyVector(baseline->second->endpoints[0]->node->node);
874 BaseLineCenter.AddVector(baseline->second->endpoints[1]->node->node);
875 BaseLineCenter.Scale(1. / 2.); // points now to center of base line
876 BaseLine.CopyVector(baseline->second->endpoints[0]->node->node);
877 BaseLine.SubtractVector(baseline->second->endpoints[1]->node->node);
878
879 // offset to center of triangle
880 CenterVector.Zero();
881 for (int i = 0; i < 3; i++)
882 CenterVector.AddVector(BTS->endpoints[i]->node->node);
883 CenterVector.Scale(1. / 3.);
884 *out << Verbose(4) << "CenterVector of base triangle is " << CenterVector << endl;
885
886 // normal vector of triangle
887 NormalVector.CopyVector(Center);
888 NormalVector.SubtractVector(&CenterVector);
889 BTS->GetNormalVector(NormalVector);
890 NormalVector.CopyVector(&BTS->NormalVector);
891 *out << Verbose(4) << "NormalVector of base triangle is " << NormalVector << endl;
892
893 // vector in propagation direction (out of triangle)
894 // project center vector onto triangle plane (points from intersection plane-NormalVector to plane-CenterVector intersection)
895 PropagationVector.MakeNormalVector(&BaseLine, &NormalVector);
896 TempVector.CopyVector(&CenterVector);
897 TempVector.SubtractVector(baseline->second->endpoints[0]->node->node); // TempVector is vector on triangle plane pointing from one baseline egde towards center!
898 //*out << Verbose(2) << "Projection of propagation onto temp: " << PropagationVector.Projection(&TempVector) << "." << endl;
899 if (PropagationVector.ScalarProduct(&TempVector) > 0) // make sure normal propagation vector points outward from baseline
900 PropagationVector.Scale(-1.);
901 *out << Verbose(4) << "PropagationVector of base triangle is " << PropagationVector << endl;
902 winner = PointsOnBoundary.end();
903
904 // loop over all points and calculate angle between normal vector of new and present triangle
905 for (PointMap::iterator target = PointsOnBoundary.begin(); target != PointsOnBoundary.end(); target++) {
906 if ((target->second != baseline->second->endpoints[0]) && (target->second != baseline->second->endpoints[1])) { // don't take the same endpoints
907 *out << Verbose(3) << "Target point is " << *(target->second) << ":" << endl;
908
909 // first check direction, so that triangles don't intersect
910 VirtualNormalVector.CopyVector(target->second->node->node);
911 VirtualNormalVector.SubtractVector(&BaseLineCenter); // points from center of base line to target
912 VirtualNormalVector.ProjectOntoPlane(&NormalVector);
913 TempAngle = VirtualNormalVector.Angle(&PropagationVector);
914 *out << Verbose(4) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl;
915 if (TempAngle > (M_PI/2.)) { // no bends bigger than Pi/2 (90 degrees)
916 *out << Verbose(4) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl;
917 continue;
918 } else
919 *out << Verbose(4) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl;
920
921 // check first and second endpoint (if any connecting line goes to target has at least not more than 1 triangle)
922 LineChecker[0] = baseline->second->endpoints[0]->lines.find(target->first);
923 LineChecker[1] = baseline->second->endpoints[1]->lines.find(target->first);
924 if (((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[0]->second->triangles.size() == 2))) {
925 *out << Verbose(4) << *(baseline->second->endpoints[0]) << " has line " << *(LineChecker[0]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[0]->second->triangles.size() << " triangles." << endl;
926 continue;
927 }
928 if (((LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (LineChecker[1]->second->triangles.size() == 2))) {
929 *out << Verbose(4) << *(baseline->second->endpoints[1]) << " has line " << *(LineChecker[1]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[1]->second->triangles.size() << " triangles." << endl;
930 continue;
931 }
932
933 // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint)
934 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)))) {
935 *out << Verbose(4) << "Current target is peak!" << endl;
936 continue;
937 }
938
939 // check for linear dependence
940 TempVector.CopyVector(baseline->second->endpoints[0]->node->node);
941 TempVector.SubtractVector(target->second->node->node);
942 helper.CopyVector(baseline->second->endpoints[1]->node->node);
943 helper.SubtractVector(target->second->node->node);
944 helper.ProjectOntoPlane(&TempVector);
945 if (fabs(helper.NormSquared()) < MYEPSILON) {
946 *out << Verbose(4) << "Chosen set of vectors is linear dependent." << endl;
947 continue;
948 }
949
950 // in case NOT both were found, create virtually this triangle, get its normal vector, calculate angle
951 flag = true;
952 VirtualNormalVector.MakeNormalVector(baseline->second->endpoints[0]->node->node, baseline->second->endpoints[1]->node->node, target->second->node->node);
953 TempVector.CopyVector(baseline->second->endpoints[0]->node->node);
954 TempVector.AddVector(baseline->second->endpoints[1]->node->node);
955 TempVector.AddVector(target->second->node->node);
956 TempVector.Scale(1./3.);
957 TempVector.SubtractVector(Center);
958 // make it always point outward
959 if (VirtualNormalVector.ScalarProduct(&TempVector) < 0)
960 VirtualNormalVector.Scale(-1.);
961 // calculate angle
962 TempAngle = NormalVector.Angle(&VirtualNormalVector);
963 *out << Verbose(4) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl;
964 if ((SmallestAngle - TempAngle) > MYEPSILON) { // set to new possible winner
965 SmallestAngle = TempAngle;
966 winner = target;
967 *out << Verbose(4) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
968 } else if (fabs(SmallestAngle - TempAngle) < MYEPSILON) { // check the angle to propagation, both possible targets are in one plane! (their normals have same angle)
969 // hence, check the angles to some normal direction from our base line but in this common plane of both targets...
970 helper.CopyVector(target->second->node->node);
971 helper.SubtractVector(&BaseLineCenter);
972 helper.ProjectOntoPlane(&BaseLine);
973 // ...the one with the smaller angle is the better candidate
974 TempVector.CopyVector(target->second->node->node);
975 TempVector.SubtractVector(&BaseLineCenter);
976 TempVector.ProjectOntoPlane(&VirtualNormalVector);
977 TempAngle = TempVector.Angle(&helper);
978 TempVector.CopyVector(winner->second->node->node);
979 TempVector.SubtractVector(&BaseLineCenter);
980 TempVector.ProjectOntoPlane(&VirtualNormalVector);
981 if (TempAngle < TempVector.Angle(&helper)) {
982 TempAngle = NormalVector.Angle(&VirtualNormalVector);
983 SmallestAngle = TempAngle;
984 winner = target;
985 *out << Verbose(4) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl;
986 } else
987 *out << Verbose(4) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl;
988 } else
989 *out << Verbose(4) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
990 }
991 } // end of loop over all boundary points
992
993 // 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
994 if (winner != PointsOnBoundary.end()) {
995 *out << Verbose(2) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl;
996 // create the lins of not yet present
997 BLS[0] = baseline->second;
998 // 5c. add lines to the line set if those were new (not yet part of a triangle), delete lines that belong to two triangles)
999 LineChecker[0] = baseline->second->endpoints[0]->lines.find(winner->first);
1000 LineChecker[1] = baseline->second->endpoints[1]->lines.find(winner->first);
1001 if (LineChecker[0] == baseline->second->endpoints[0]->lines.end()) { // create
1002 BPS[0] = baseline->second->endpoints[0];
1003 BPS[1] = winner->second;
1004 BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1005 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[1]));
1006 LinesOnBoundaryCount++;
1007 } else
1008 BLS[1] = LineChecker[0]->second;
1009 if (LineChecker[1] == baseline->second->endpoints[1]->lines.end()) { // create
1010 BPS[0] = baseline->second->endpoints[1];
1011 BPS[1] = winner->second;
1012 BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1013 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[2]));
1014 LinesOnBoundaryCount++;
1015 } else
1016 BLS[2] = LineChecker[1]->second;
1017 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
1018 BTS->GetCenter(&helper);
1019 helper.SubtractVector(Center);
1020 helper.Scale(-1);
1021 BTS->GetNormalVector(helper);
1022 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1023 TrianglesOnBoundaryCount++;
1024 } else {
1025 *out << Verbose(1) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl;
1026 }
1027
1028 // 5d. If the set of lines is not yet empty, go to 5. and continue
1029 } else
1030 *out << Verbose(2) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl;
1031 } while (flag);
1032
1033 // exit
1034 delete(Center);
1035};
1036
1037/** Inserts all points outside of the tesselated surface into it by adding new triangles.
1038 * \param *out output stream for debugging
1039 * \param *cloud cluster of points
1040 * \param *LC LinkedCell structure to find nearest point quickly
1041 * \return true - all straddling points insert, false - something went wrong
1042 */
1043bool Tesselation::InsertStraddlingPoints(ofstream *out, PointCloud *cloud, LinkedCell *LC)
1044{
1045 Vector Intersection, Normal;
1046 TesselPoint *Walker = NULL;
1047 Vector *Center = cloud->GetCenter(out);
1048 list<BoundaryTriangleSet*> *triangles = NULL;
1049
1050 *out << Verbose(1) << "Begin of InsertStraddlingPoints" << endl;
1051
1052 cloud->GoToFirst();
1053 while (!cloud->IsEnd()) { // we only have to go once through all points, as boundary can become only bigger
1054 LinkedCell BoundaryPoints(this, 5.);
1055 Walker = cloud->GetPoint();
1056 *out << Verbose(2) << "Current point is " << *Walker << "." << endl;
1057 // get the next triangle
1058 triangles = FindClosestTrianglesToPoint(out, Walker->node, &BoundaryPoints);
1059 if (triangles == NULL) {
1060 *out << Verbose(1) << "No triangles found, probably a tesselation point itself." << endl;
1061 cloud->GoToNext();
1062 continue;
1063 } else {
1064 BTS = triangles->front();
1065 }
1066 *out << Verbose(2) << "Closest triangle is " << *BTS << "." << endl;
1067 // get the intersection point
1068 if (BTS->GetIntersectionInsideTriangle(out, Center, Walker->node, &Intersection)) {
1069 *out << Verbose(2) << "We have an intersection at " << Intersection << "." << endl;
1070 // we have the intersection, check whether in- or outside of boundary
1071 if ((Center->DistanceSquared(Walker->node) - Center->DistanceSquared(&Intersection)) < -MYEPSILON) {
1072 // inside, next!
1073 *out << Verbose(2) << *Walker << " is inside wrt triangle " << *BTS << "." << endl;
1074 } else {
1075 // outside!
1076 *out << Verbose(2) << *Walker << " is outside wrt triangle " << *BTS << "." << endl;
1077 class BoundaryLineSet *OldLines[3], *NewLines[3];
1078 class BoundaryPointSet *OldPoints[3], *NewPoint;
1079 // store the three old lines and old points
1080 for (int i=0;i<3;i++) {
1081 OldLines[i] = BTS->lines[i];
1082 OldPoints[i] = BTS->endpoints[i];
1083 }
1084 Normal.CopyVector(&BTS->NormalVector);
1085 // add Walker to boundary points
1086 *out << Verbose(2) << "Adding " << *Walker << " to BoundaryPoints." << endl;
1087 if (AddBoundaryPoint(Walker,0))
1088 NewPoint = BPS[0];
1089 else
1090 continue;
1091 // remove triangle
1092 *out << Verbose(2) << "Erasing triangle " << *BTS << "." << endl;
1093 TrianglesOnBoundary.erase(BTS->Nr);
1094 delete(BTS);
1095 // create three new boundary lines
1096 for (int i=0;i<3;i++) {
1097 BPS[0] = NewPoint;
1098 BPS[1] = OldPoints[i];
1099 NewLines[i] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1100 *out << Verbose(3) << "Creating new line " << *NewLines[i] << "." << endl;
1101 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, NewLines[i])); // no need for check for unique insertion as BPS[0] is definitely a new one
1102 LinesOnBoundaryCount++;
1103 }
1104 // create three new triangle with new point
1105 for (int i=0;i<3;i++) { // find all baselines
1106 BLS[0] = OldLines[i];
1107 int n = 1;
1108 for (int j=0;j<3;j++) {
1109 if (NewLines[j]->IsConnectedTo(BLS[0])) {
1110 if (n>2) {
1111 *out << Verbose(1) << "ERROR: " << BLS[0] << " connects to all of the new lines?!" << endl;
1112 return false;
1113 } else
1114 BLS[n++] = NewLines[j];
1115 }
1116 }
1117 // create the triangle
1118 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
1119 Normal.Scale(-1.);
1120 BTS->GetNormalVector(Normal);
1121 Normal.Scale(-1.);
1122 *out << Verbose(2) << "Created new triangle " << *BTS << "." << endl;
1123 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1124 TrianglesOnBoundaryCount++;
1125 }
1126 }
1127 } else { // something is wrong with FindClosestTriangleToPoint!
1128 *out << Verbose(1) << "ERROR: The closest triangle did not produce an intersection!" << endl;
1129 return false;
1130 }
1131 cloud->GoToNext();
1132 }
1133
1134 // exit
1135 delete(Center);
1136 *out << Verbose(1) << "End of InsertStraddlingPoints" << endl;
1137 return true;
1138};
1139
1140/** Adds a point to the tesselation::PointsOnBoundary list.
1141 * \param *Walker point to add
1142 * \param n TesselStruct::BPS index to put pointer into
1143 * \return true - new point was added, false - point already present
1144 */
1145bool
1146Tesselation::AddBoundaryPoint(TesselPoint *Walker, int n)
1147{
1148 PointTestPair InsertUnique;
1149 BPS[n] = new class BoundaryPointSet(Walker);
1150 InsertUnique = PointsOnBoundary.insert(PointPair(Walker->nr, BPS[n]));
1151 if (InsertUnique.second) { // if new point was not present before, increase counter
1152 PointsOnBoundaryCount++;
1153 return true;
1154 } else {
1155 delete(BPS[n]);
1156 BPS[n] = InsertUnique.first->second;
1157 return false;
1158 }
1159}
1160;
1161
1162/** Adds point to Tesselation::PointsOnBoundary if not yet present.
1163 * Tesselation::TPS is set to either this new BoundaryPointSet or to the existing one of not unique.
1164 * @param Candidate point to add
1165 * @param n index for this point in Tesselation::TPS array
1166 */
1167void
1168Tesselation::AddTesselationPoint(TesselPoint* Candidate, int n)
1169{
1170 PointTestPair InsertUnique;
1171 TPS[n] = new class BoundaryPointSet(Candidate);
1172 InsertUnique = PointsOnBoundary.insert(PointPair(Candidate->nr, TPS[n]));
1173 if (InsertUnique.second) { // if new point was not present before, increase counter
1174 PointsOnBoundaryCount++;
1175 } else {
1176 delete TPS[n];
1177 cout << Verbose(3) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl;
1178 TPS[n] = (InsertUnique.first)->second;
1179 }
1180}
1181;
1182
1183/** Function tries to add line from current Points in BPS to BoundaryLineSet.
1184 * If successful it raises the line count and inserts the new line into the BLS,
1185 * if unsuccessful, it writes the line which had been present into the BLS, deleting the new constructed one.
1186 * @param *a first endpoint
1187 * @param *b second endpoint
1188 * @param n index of Tesselation::BLS giving the line with both endpoints
1189 */
1190void Tesselation::AddTesselationLine(class BoundaryPointSet *a, class BoundaryPointSet *b, int n) {
1191 bool insertNewLine = true;
1192
1193 if (a->lines.find(b->node->nr) != a->lines.end()) {
1194 LineMap::iterator FindLine;
1195 pair<LineMap::iterator,LineMap::iterator> FindPair;
1196 FindPair = a->lines.equal_range(b->node->nr);
1197
1198 for (FindLine = FindPair.first; FindLine != FindPair.second; ++FindLine) {
1199 // If there is a line with less than two attached triangles, we don't need a new line.
1200 if (FindLine->second->triangles.size() < 2) {
1201 insertNewLine = false;
1202 cout << Verbose(3) << "Using existing line " << *FindLine->second << endl;
1203
1204 BPS[0] = FindLine->second->endpoints[0];
1205 BPS[1] = FindLine->second->endpoints[1];
1206 BLS[n] = FindLine->second;
1207
1208 break;
1209 }
1210 }
1211 }
1212
1213 if (insertNewLine) {
1214 AlwaysAddTesselationTriangleLine(a, b, n);
1215 }
1216}
1217;
1218
1219/**
1220 * Adds lines from each of the current points in the BPS to BoundaryLineSet.
1221 * Raises the line count and inserts the new line into the BLS.
1222 *
1223 * @param *a first endpoint
1224 * @param *b second endpoint
1225 * @param n index of Tesselation::BLS giving the line with both endpoints
1226 */
1227void Tesselation::AlwaysAddTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, int n)
1228{
1229 cout << Verbose(3) << "Adding line between " << *(a->node) << " and " << *(b->node) << "." << endl;
1230 BPS[0] = a;
1231 BPS[1] = b;
1232 BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); // this also adds the line to the local maps
1233 // add line to global map
1234 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n]));
1235 // increase counter
1236 LinesOnBoundaryCount++;
1237};
1238
1239/** Function tries to add Triangle just created to Triangle and remarks if already existent (Failure of algorithm).
1240 * Furthermore it adds the triangle to all of its lines, in order to recognize those which are saturated later.
1241 */
1242void Tesselation::AddTesselationTriangle()
1243{
1244 cout << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl;
1245
1246 // add triangle to global map
1247 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1248 TrianglesOnBoundaryCount++;
1249
1250 // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
1251};
1252
1253/** Removes a triangle from the tesselation.
1254 * Removes itself from the TriangleMap's of its lines, calls for them RemoveTriangleLine() if they are no more connected.
1255 * Removes itself from memory.
1256 * \param *triangle to remove
1257 */
1258void Tesselation::RemoveTesselationTriangle(class BoundaryTriangleSet *triangle)
1259{
1260 if (triangle == NULL)
1261 return;
1262 for (int i = 0; i < 3; i++) {
1263 if (triangle->lines[i] != NULL) {
1264 cout << Verbose(5) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl;
1265 triangle->lines[i]->triangles.erase(triangle->Nr);
1266 if (triangle->lines[i]->triangles.empty()) {
1267 cout << Verbose(5) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl;
1268 RemoveTesselationLine(triangle->lines[i]);
1269 triangle->lines[i] = NULL;
1270 } else
1271 cout << Verbose(5) << *triangle->lines[i] << " is still attached to another triangle." << endl;
1272 } else
1273 cerr << "ERROR: This line " << i << " has already been free'd." << endl;
1274 }
1275
1276 if (TrianglesOnBoundary.erase(triangle->Nr))
1277 cout << Verbose(5) << "Removing triangle Nr. " << triangle->Nr << "." << endl;
1278 delete(triangle);
1279};
1280
1281/** Removes a line from the tesselation.
1282 * Removes itself from each endpoints' LineMap, then removes itself from global LinesOnBoundary list and free's the line.
1283 * \param *line line to remove
1284 */
1285void Tesselation::RemoveTesselationLine(class BoundaryLineSet *line)
1286{
1287 int Numbers[2];
1288
1289 if (line == NULL)
1290 return;
1291 // get other endpoint number of finding copies of same line
1292 if (line->endpoints[1] != NULL)
1293 Numbers[0] = line->endpoints[1]->Nr;
1294 else
1295 Numbers[0] = -1;
1296 if (line->endpoints[0] != NULL)
1297 Numbers[1] = line->endpoints[0]->Nr;
1298 else
1299 Numbers[1] = -1;
1300
1301 for (int i = 0; i < 2; i++) {
1302 if (line->endpoints[i] != NULL) {
1303 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
1304 pair<LineMap::iterator, LineMap::iterator> erasor = line->endpoints[i]->lines.equal_range(Numbers[i]);
1305 for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
1306 if ((*Runner).second == line) {
1307 cout << Verbose(5) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
1308 line->endpoints[i]->lines.erase(Runner);
1309 break;
1310 }
1311 } else { // there's just a single line left
1312 if (line->endpoints[i]->lines.erase(line->Nr))
1313 cout << Verbose(5) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
1314 }
1315 if (line->endpoints[i]->lines.empty()) {
1316 cout << Verbose(5) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl;
1317 RemoveTesselationPoint(line->endpoints[i]);
1318 line->endpoints[i] = NULL;
1319 } else
1320 cout << Verbose(5) << *line->endpoints[i] << " has still lines it's attached to." << endl;
1321 } else
1322 cerr << "ERROR: Endpoint " << i << " has already been free'd." << endl;
1323 }
1324 if (!line->triangles.empty())
1325 cerr << "WARNING: Memory Leak! I " << *line << " am still connected to some triangles." << endl;
1326
1327 if (LinesOnBoundary.erase(line->Nr))
1328 cout << Verbose(5) << "Removing line Nr. " << line->Nr << "." << endl;
1329 delete(line);
1330};
1331
1332/** Removes a point from the tesselation.
1333 * Checks whether there are still lines connected, removes from global PointsOnBoundary list, then free's the point.
1334 * \note If a point should be removed, while keep the tesselated surface intact (i.e. closed), use RemovePointFromTesselatedSurface()
1335 * \param *point point to remove
1336 */
1337void Tesselation::RemoveTesselationPoint(class BoundaryPointSet *point)
1338{
1339 if (point == NULL)
1340 return;
1341 if (PointsOnBoundary.erase(point->Nr))
1342 cout << Verbose(5) << "Removing point Nr. " << point->Nr << "." << endl;
1343 delete(point);
1344};
1345
1346/** Checks whether the triangle consisting of the three points is already present.
1347 * Searches for the points in Tesselation::PointsOnBoundary and checks their
1348 * lines. If any of the three edges already has two triangles attached, false is
1349 * returned.
1350 * \param *out output stream for debugging
1351 * \param *Candidates endpoints of the triangle candidate
1352 * \return integer 0 if no triangle exists, 1 if one triangle exists, 2 if two
1353 * triangles exist which is the maximum for three points
1354 */
1355int Tesselation::CheckPresenceOfTriangle(ofstream *out, TesselPoint *Candidates[3]) {
1356 int adjacentTriangleCount = 0;
1357 class BoundaryPointSet *Points[3];
1358
1359 *out << Verbose(2) << "Begin of CheckPresenceOfTriangle" << endl;
1360 // builds a triangle point set (Points) of the end points
1361 for (int i = 0; i < 3; i++) {
1362 PointMap::iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr);
1363 if (FindPoint != PointsOnBoundary.end()) {
1364 Points[i] = FindPoint->second;
1365 } else {
1366 Points[i] = NULL;
1367 }
1368 }
1369
1370 // checks lines between the points in the Points for their adjacent triangles
1371 for (int i = 0; i < 3; i++) {
1372 if (Points[i] != NULL) {
1373 for (int j = i; j < 3; j++) {
1374 if (Points[j] != NULL) {
1375 LineMap::iterator FindLine = Points[i]->lines.find(Points[j]->node->nr);
1376 for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) {
1377 TriangleMap *triangles = &FindLine->second->triangles;
1378 *out << Verbose(3) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl;
1379 for (TriangleMap::iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) {
1380 if (FindTriangle->second->IsPresentTupel(Points)) {
1381 adjacentTriangleCount++;
1382 }
1383 }
1384 *out << Verbose(3) << "end." << endl;
1385 }
1386 // Only one of the triangle lines must be considered for the triangle count.
1387 *out << Verbose(2) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
1388 return adjacentTriangleCount;
1389 }
1390 }
1391 }
1392 }
1393
1394 *out << Verbose(2) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
1395 *out << Verbose(2) << "End of CheckPresenceOfTriangle" << endl;
1396 return adjacentTriangleCount;
1397};
1398
1399
1400/** Finds the starting triangle for find_non_convex_border().
1401 * Looks at the outermost point per axis, then Find_second_point_for_Tesselation()
1402 * for the second and Find_next_suitable_point_via_Angle_of_Sphere() for the third
1403 * point are called.
1404 * \param *out output stream for debugging
1405 * \param RADIUS radius of virtual rolling sphere
1406 * \param *LC LinkedCell structure with neighbouring TesselPoint's
1407 */
1408void Tesselation::Find_starting_triangle(ofstream *out, const double RADIUS, LinkedCell *LC)
1409{
1410 cout << Verbose(1) << "Begin of Find_starting_triangle\n";
1411 int i = 0;
1412 LinkedNodes *List = NULL;
1413 TesselPoint* FirstPoint = NULL;
1414 TesselPoint* SecondPoint = NULL;
1415 TesselPoint* MaxPoint[NDIM];
1416 double max_coordinate[NDIM];
1417 Vector Oben;
1418 Vector helper;
1419 Vector Chord;
1420 Vector SearchDirection;
1421
1422 Oben.Zero();
1423
1424 for (i = 0; i < 3; i++) {
1425 MaxPoint[i] = NULL;
1426 max_coordinate[i] = -1;
1427 }
1428
1429 // 1. searching topmost point with respect to each axis
1430 for (int i=0;i<NDIM;i++) { // each axis
1431 LC->n[i] = LC->N[i]-1; // current axis is topmost cell
1432 for (LC->n[(i+1)%NDIM]=0;LC->n[(i+1)%NDIM]<LC->N[(i+1)%NDIM];LC->n[(i+1)%NDIM]++)
1433 for (LC->n[(i+2)%NDIM]=0;LC->n[(i+2)%NDIM]<LC->N[(i+2)%NDIM];LC->n[(i+2)%NDIM]++) {
1434 List = LC->GetCurrentCell();
1435 //cout << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
1436 if (List != NULL) {
1437 for (LinkedNodes::iterator Runner = List->begin();Runner != List->end();Runner++) {
1438 if ((*Runner)->node->x[i] > max_coordinate[i]) {
1439 cout << Verbose(2) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl;
1440 max_coordinate[i] = (*Runner)->node->x[i];
1441 MaxPoint[i] = (*Runner);
1442 }
1443 }
1444 } else {
1445 cerr << "ERROR: The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl;
1446 }
1447 }
1448 }
1449
1450 cout << Verbose(2) << "Found maximum coordinates: ";
1451 for (int i=0;i<NDIM;i++)
1452 cout << i << ": " << *MaxPoint[i] << "\t";
1453 cout << endl;
1454
1455 BTS = NULL;
1456 CandidateList *Opt_Candidates = new CandidateList();
1457 for (int k=0;k<NDIM;k++) {
1458 Oben.x[k] = 1.;
1459 FirstPoint = MaxPoint[k];
1460 cout << Verbose(1) << "Coordinates of start node at " << *FirstPoint->node << "." << endl;
1461
1462 double ShortestAngle;
1463 TesselPoint* Opt_Candidate = NULL;
1464 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.
1465
1466 Find_second_point_for_Tesselation(FirstPoint, NULL, Oben, Opt_Candidate, &ShortestAngle, RADIUS, LC); // we give same point as next candidate as its bonds are looked into in find_second_...
1467 SecondPoint = Opt_Candidate;
1468 if (SecondPoint == NULL) // have we found a second point?
1469 continue;
1470 else
1471 cout << Verbose(1) << "Found second point is at " << *SecondPoint->node << ".\n";
1472
1473 helper.CopyVector(FirstPoint->node);
1474 helper.SubtractVector(SecondPoint->node);
1475 helper.Normalize();
1476 Oben.ProjectOntoPlane(&helper);
1477 Oben.Normalize();
1478 helper.VectorProduct(&Oben);
1479 ShortestAngle = 2.*M_PI; // This will indicate the quadrant.
1480
1481 Chord.CopyVector(FirstPoint->node); // bring into calling function
1482 Chord.SubtractVector(SecondPoint->node);
1483 double radius = Chord.ScalarProduct(&Chord);
1484 double CircleRadius = sqrt(RADIUS*RADIUS - radius/4.);
1485 helper.CopyVector(&Oben);
1486 helper.Scale(CircleRadius);
1487 // Now, oben and helper are two orthonormalized vectors in the plane defined by Chord (not normalized)
1488
1489 // look in one direction of baseline for initial candidate
1490 SearchDirection.MakeNormalVector(&Chord, &Oben); // whether we look "left" first or "right" first is not important ...
1491
1492 // adding point 1 and point 2 and add the line between them
1493 AddTesselationPoint(FirstPoint, 0);
1494 AddTesselationPoint(SecondPoint, 1);
1495 AddTesselationLine(TPS[0], TPS[1], 0);
1496
1497 //cout << Verbose(2) << "INFO: OldSphereCenter is at " << helper << ".\n";
1498 Find_third_point_for_Tesselation(
1499 Oben, SearchDirection, helper, BLS[0], NULL, *&Opt_Candidates, &ShortestAngle, RADIUS, LC
1500 );
1501 cout << Verbose(1) << "List of third Points is ";
1502 for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) {
1503 cout << " " << *(*it)->point;
1504 }
1505 cout << endl;
1506
1507 for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) {
1508 // add third triangle point
1509 AddTesselationPoint((*it)->point, 2);
1510 // add the second and third line
1511 AddTesselationLine(TPS[1], TPS[2], 1);
1512 AddTesselationLine(TPS[0], TPS[2], 2);
1513 // ... and triangles to the Maps of the Tesselation class
1514 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
1515 AddTesselationTriangle();
1516 // ... and calculate its normal vector (with correct orientation)
1517 (*it)->OptCenter.Scale(-1.);
1518 cout << Verbose(2) << "Anti-Oben is currently " << (*it)->OptCenter << "." << endl;
1519 BTS->GetNormalVector((*it)->OptCenter); // vector to compare with should point inwards
1520 cout << Verbose(0) << "==> Found starting triangle consists of " << *FirstPoint << ", " << *SecondPoint << " and "
1521 << *(*it)->point << " with normal vector " << BTS->NormalVector << ".\n";
1522
1523 // if we do not reach the end with the next step of iteration, we need to setup a new first line
1524 if (it != Opt_Candidates->end()--) {
1525 FirstPoint = (*it)->BaseLine->endpoints[0]->node;
1526 SecondPoint = (*it)->point;
1527 // adding point 1 and point 2 and the line between them
1528 AddTesselationPoint(FirstPoint, 0);
1529 AddTesselationPoint(SecondPoint, 1);
1530 AddTesselationLine(TPS[0], TPS[1], 0);
1531 }
1532 cout << Verbose(2) << "Projection is " << BTS->NormalVector.ScalarProduct(&Oben) << "." << endl;
1533 }
1534 if (BTS != NULL) // we have created one starting triangle
1535 break;
1536 else {
1537 // remove all candidates from the list and then the list itself
1538 class CandidateForTesselation *remover = NULL;
1539 for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) {
1540 remover = *it;
1541 delete(remover);
1542 }
1543 Opt_Candidates->clear();
1544 }
1545 }
1546
1547 // remove all candidates from the list and then the list itself
1548 class CandidateForTesselation *remover = NULL;
1549 for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) {
1550 remover = *it;
1551 delete(remover);
1552 }
1553 delete(Opt_Candidates);
1554 cout << Verbose(1) << "End of Find_starting_triangle\n";
1555};
1556
1557
1558/** This function finds a triangle to a line, adjacent to an existing one.
1559 * @param out output stream for debugging
1560 * @param Line current baseline to search from
1561 * @param T current triangle which \a Line is edge of
1562 * @param RADIUS radius of the rolling ball
1563 * @param N number of found triangles
1564 * @param *LC LinkedCell structure with neighbouring points
1565 */
1566bool Tesselation::Find_next_suitable_triangle(ofstream *out, BoundaryLineSet &Line, BoundaryTriangleSet &T, const double& RADIUS, int N, LinkedCell *LC)
1567{
1568 cout << Verbose(0) << "Begin of Find_next_suitable_triangle\n";
1569 bool result = true;
1570 CandidateList *Opt_Candidates = new CandidateList();
1571
1572 Vector CircleCenter;
1573 Vector CirclePlaneNormal;
1574 Vector OldSphereCenter;
1575 Vector SearchDirection;
1576 Vector helper;
1577 TesselPoint *ThirdNode = NULL;
1578 LineMap::iterator testline;
1579 double ShortestAngle = 2.*M_PI; // This will indicate the quadrant.
1580 double radius, CircleRadius;
1581
1582 cout << Verbose(1) << "Current baseline is " << Line << " of triangle " << T << "." << endl;
1583 for (int i=0;i<3;i++)
1584 if ((T.endpoints[i]->node != Line.endpoints[0]->node) && (T.endpoints[i]->node != Line.endpoints[1]->node))
1585 ThirdNode = T.endpoints[i]->node;
1586
1587 // construct center of circle
1588 CircleCenter.CopyVector(Line.endpoints[0]->node->node);
1589 CircleCenter.AddVector(Line.endpoints[1]->node->node);
1590 CircleCenter.Scale(0.5);
1591
1592 // construct normal vector of circle
1593 CirclePlaneNormal.CopyVector(Line.endpoints[0]->node->node);
1594 CirclePlaneNormal.SubtractVector(Line.endpoints[1]->node->node);
1595
1596 // calculate squared radius of circle
1597 radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
1598 if (radius/4. < RADIUS*RADIUS) {
1599 CircleRadius = RADIUS*RADIUS - radius/4.;
1600 CirclePlaneNormal.Normalize();
1601 cout << Verbose(2) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
1602
1603 // construct old center
1604 GetCenterofCircumcircle(&OldSphereCenter, T.endpoints[0]->node->node, T.endpoints[1]->node->node, T.endpoints[2]->node->node);
1605 helper.CopyVector(&T.NormalVector); // normal vector ensures that this is correct center of the two possible ones
1606 radius = Line.endpoints[0]->node->node->DistanceSquared(&OldSphereCenter);
1607 helper.Scale(sqrt(RADIUS*RADIUS - radius));
1608 OldSphereCenter.AddVector(&helper);
1609 OldSphereCenter.SubtractVector(&CircleCenter);
1610 //cout << Verbose(2) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl;
1611
1612 // construct SearchDirection
1613 SearchDirection.MakeNormalVector(&T.NormalVector, &CirclePlaneNormal);
1614 helper.CopyVector(Line.endpoints[0]->node->node);
1615 helper.SubtractVector(ThirdNode->node);
1616 if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
1617 SearchDirection.Scale(-1.);
1618 SearchDirection.ProjectOntoPlane(&OldSphereCenter);
1619 SearchDirection.Normalize();
1620 cout << Verbose(2) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
1621 if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
1622 // rotated the wrong way!
1623 cerr << "ERROR: SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl;
1624 }
1625
1626 // add third point
1627 Find_third_point_for_Tesselation(
1628 T.NormalVector, SearchDirection, OldSphereCenter, &Line, ThirdNode, Opt_Candidates,
1629 &ShortestAngle, RADIUS, LC
1630 );
1631
1632 } else {
1633 cout << Verbose(1) << "Circumcircle for base line " << Line << " and base triangle " << T << " is too big!" << endl;
1634 }
1635
1636 if (Opt_Candidates->begin() == Opt_Candidates->end()) {
1637 cerr << "WARNING: Could not find a suitable candidate." << endl;
1638 return false;
1639 }
1640 cout << Verbose(1) << "Third Points are ";
1641 for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) {
1642 cout << " " << *(*it)->point;
1643 }
1644 cout << endl;
1645
1646 BoundaryLineSet *BaseRay = &Line;
1647 for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) {
1648 cout << Verbose(1) << " Third point candidate is " << *(*it)->point
1649 << " with circumsphere's center at " << (*it)->OptCenter << "." << endl;
1650 cout << Verbose(1) << " Baseline is " << *BaseRay << endl;
1651
1652 // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2)
1653 TesselPoint *PointCandidates[3];
1654 PointCandidates[0] = (*it)->point;
1655 PointCandidates[1] = BaseRay->endpoints[0]->node;
1656 PointCandidates[2] = BaseRay->endpoints[1]->node;
1657 int existentTrianglesCount = CheckPresenceOfTriangle(out, PointCandidates);
1658
1659 BTS = NULL;
1660 // If there is no triangle, add it regularly.
1661 if (existentTrianglesCount == 0) {
1662 AddTesselationPoint((*it)->point, 0);
1663 AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
1664 AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
1665
1666 if (CheckLineCriteriaforDegeneratedTriangle(TPS)) {
1667 AddTesselationLine(TPS[0], TPS[1], 0);
1668 AddTesselationLine(TPS[0], TPS[2], 1);
1669 AddTesselationLine(TPS[1], TPS[2], 2);
1670
1671 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
1672 AddTesselationTriangle();
1673 (*it)->OptCenter.Scale(-1.);
1674 BTS->GetNormalVector((*it)->OptCenter);
1675 (*it)->OptCenter.Scale(-1.);
1676
1677 cout << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector
1678 << " for this triangle ... " << endl;
1679 //cout << Verbose(1) << "We have "<< TrianglesOnBoundaryCount << " for line " << *BaseRay << "." << endl;
1680 } else {
1681 cout << Verbose(1) << "WARNING: This triangle consisting of ";
1682 cout << *(*it)->point << ", ";
1683 cout << *BaseRay->endpoints[0]->node << " and ";
1684 cout << *BaseRay->endpoints[1]->node << " ";
1685 cout << "exists and is not added, as it does not seem helpful!" << endl;
1686 result = false;
1687 }
1688 } else if (existentTrianglesCount == 1) { // If there is a planar region within the structure, we need this triangle a second time.
1689 AddTesselationPoint((*it)->point, 0);
1690 AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
1691 AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
1692
1693 // 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)
1694 // i.e. at least one of the three lines must be present with TriangleCount <= 1
1695 if (CheckLineCriteriaforDegeneratedTriangle(TPS)) {
1696 AddTesselationLine(TPS[0], TPS[1], 0);
1697 AddTesselationLine(TPS[0], TPS[2], 1);
1698 AddTesselationLine(TPS[1], TPS[2], 2);
1699
1700 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
1701 AddTesselationTriangle(); // add to global map
1702
1703 (*it)->OtherOptCenter.Scale(-1.);
1704 BTS->GetNormalVector((*it)->OtherOptCenter);
1705 (*it)->OtherOptCenter.Scale(-1.);
1706
1707 cout << "--> WARNING: Special new triangle with " << *BTS << " and normal vector " << BTS->NormalVector
1708 << " for this triangle ... " << endl;
1709 cout << Verbose(1) << "We have "<< BaseRay->triangles.size() << " for line " << BaseRay << "." << endl;
1710 } else {
1711 cout << Verbose(1) << "WARNING: This triangle consisting of ";
1712 cout << *(*it)->point << ", ";
1713 cout << *BaseRay->endpoints[0]->node << " and ";
1714 cout << *BaseRay->endpoints[1]->node << " ";
1715 cout << "exists and is not added, as it does not seem helpful!" << endl;
1716 result = false;
1717 }
1718 } else {
1719 cout << Verbose(1) << "This triangle consisting of ";
1720 cout << *(*it)->point << ", ";
1721 cout << *BaseRay->endpoints[0]->node << " and ";
1722 cout << *BaseRay->endpoints[1]->node << " ";
1723 cout << "is invalid!" << endl;
1724 result = false;
1725 }
1726
1727 // set baseline to new ray from ref point (here endpoints[0]->node) to current candidate (here (*it)->point))
1728 BaseRay = BLS[0];
1729 }
1730
1731 // remove all candidates from the list and then the list itself
1732 class CandidateForTesselation *remover = NULL;
1733 for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) {
1734 remover = *it;
1735 delete(remover);
1736 }
1737 delete(Opt_Candidates);
1738 cout << Verbose(0) << "End of Find_next_suitable_triangle\n";
1739 return result;
1740};
1741
1742/** Checks whether the quadragon of the two triangles connect to \a *Base is convex.
1743 * We look whether the closest point on \a *Base with respect to the other baseline is outside
1744 * of the segment formed by both endpoints (concave) or not (convex).
1745 * \param *out output stream for debugging
1746 * \param *Base line to be flipped
1747 * \return NULL - concave, otherwise endpoint that makes it concave
1748 */
1749class BoundaryPointSet *Tesselation::IsConvexRectangle(ofstream *out, class BoundaryLineSet *Base)
1750{
1751 class BoundaryPointSet *Spot = NULL;
1752 class BoundaryLineSet *OtherBase;
1753 Vector *ClosestPoint;
1754
1755 int m=0;
1756 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
1757 for (int j=0;j<3;j++) // all of their endpoints and baselines
1758 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
1759 BPS[m++] = runner->second->endpoints[j];
1760 OtherBase = new class BoundaryLineSet(BPS,-1);
1761
1762 *out << Verbose(3) << "INFO: Current base line is " << *Base << "." << endl;
1763 *out << Verbose(3) << "INFO: Other base line is " << *OtherBase << "." << endl;
1764
1765 // get the closest point on each line to the other line
1766 ClosestPoint = GetClosestPointBetweenLine(out, Base, OtherBase);
1767
1768 // delete the temporary other base line
1769 delete(ClosestPoint);
1770 delete(OtherBase);
1771
1772 // get the distance vector from Base line to OtherBase line
1773 Vector DistanceToIntersection[2], BaseLine;
1774 double distance[2];
1775 BaseLine.CopyVector(Base->endpoints[1]->node->node);
1776 BaseLine.SubtractVector(Base->endpoints[0]->node->node);
1777 for (int i=0;i<2;i++) {
1778 DistanceToIntersection[i].CopyVector(ClosestPoint);
1779 DistanceToIntersection[i].SubtractVector(Base->endpoints[i]->node->node);
1780 distance[i] = BaseLine.ScalarProduct(&DistanceToIntersection[i]);
1781 }
1782 if ((distance[0] * distance[1]) > MYEPSILON) { // have same sign?
1783 *out << Verbose(3) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1] << ". " << *Base << " is concave." << endl;
1784 if (distance[0] < distance[1]) {
1785 Spot = Base->endpoints[0];
1786 } else {
1787 Spot = Base->endpoints[1];
1788 }
1789 return Spot;
1790 } else { // different sign, i.e. we are in between
1791 *out << Verbose(3) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl;
1792 return NULL;
1793 }
1794
1795};
1796
1797void Tesselation::PrintAllBoundaryPoints(ofstream *out)
1798{
1799 // print all lines
1800 *out << Verbose(1) << "Printing all boundary points for debugging:" << endl;
1801 for (PointMap::iterator PointRunner = PointsOnBoundary.begin();PointRunner != PointsOnBoundary.end(); PointRunner++)
1802 *out << Verbose(2) << *(PointRunner->second) << endl;
1803};
1804
1805void Tesselation::PrintAllBoundaryLines(ofstream *out)
1806{
1807 // print all lines
1808 *out << Verbose(1) << "Printing all boundary lines for debugging:" << endl;
1809 for (LineMap::iterator LineRunner = LinesOnBoundary.begin(); LineRunner != LinesOnBoundary.end(); LineRunner++)
1810 *out << Verbose(2) << *(LineRunner->second) << endl;
1811};
1812
1813void Tesselation::PrintAllBoundaryTriangles(ofstream *out)
1814{
1815 // print all triangles
1816 *out << Verbose(1) << "Printing all boundary triangles for debugging:" << endl;
1817 for (TriangleMap::iterator TriangleRunner = TrianglesOnBoundary.begin(); TriangleRunner != TrianglesOnBoundary.end(); TriangleRunner++)
1818 *out << Verbose(2) << *(TriangleRunner->second) << endl;
1819};
1820
1821/** For a given boundary line \a *Base and its two triangles, picks the central baseline that is "higher".
1822 * \param *out output stream for debugging
1823 * \param *Base line to be flipped
1824 * \return true - line was changed, false - same line as before
1825 */
1826bool Tesselation::PickFarthestofTwoBaselines(ofstream *out, class BoundaryLineSet *Base)
1827{
1828 class BoundaryLineSet *OtherBase;
1829 Vector *ClosestPoint[2];
1830
1831 int m=0;
1832 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
1833 for (int j=0;j<3;j++) // all of their endpoints and baselines
1834 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
1835 BPS[m++] = runner->second->endpoints[j];
1836 OtherBase = new class BoundaryLineSet(BPS,-1);
1837
1838 *out << Verbose(3) << "INFO: Current base line is " << *Base << "." << endl;
1839 *out << Verbose(3) << "INFO: Other base line is " << *OtherBase << "." << endl;
1840
1841 // get the closest point on each line to the other line
1842 ClosestPoint[0] = GetClosestPointBetweenLine(out, Base, OtherBase);
1843 ClosestPoint[1] = GetClosestPointBetweenLine(out, OtherBase, Base);
1844
1845 // get the distance vector from Base line to OtherBase line
1846 Vector Distance;
1847 Distance.CopyVector(ClosestPoint[1]);
1848 Distance.SubtractVector(ClosestPoint[0]);
1849
1850 // delete the temporary other base line and the closest points
1851 delete(ClosestPoint[0]);
1852 delete(ClosestPoint[1]);
1853 delete(OtherBase);
1854
1855 if (Distance.NormSquared() < MYEPSILON) { // check for intersection
1856 *out << Verbose(3) << "REJECT: Both lines have an intersection: Nothing to do." << endl;
1857 return false;
1858 } else { // check for sign against BaseLineNormal
1859 Vector BaseLineNormal;
1860 BaseLineNormal.Zero();
1861 if (Base->triangles.size() < 2) {
1862 *out << Verbose(2) << "ERROR: Less than two triangles are attached to this baseline!" << endl;
1863 return false;
1864 }
1865 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
1866 *out << Verbose(4) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
1867 BaseLineNormal.AddVector(&(runner->second->NormalVector));
1868 }
1869 BaseLineNormal.Scale(1./2.);
1870
1871 if (Distance.ScalarProduct(&BaseLineNormal) > MYEPSILON) { // Distance points outwards, hence OtherBase higher than Base -> flip
1872 *out << Verbose(2) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl;
1873 FlipBaseline(out, Base);
1874 return true;
1875 } else { // Base higher than OtherBase -> do nothing
1876 *out << Verbose(2) << "REJECT: Base line is higher: Nothing to do." << endl;
1877 return false;
1878 }
1879 }
1880};
1881
1882/** Returns the closest point on \a *Base with respect to \a *OtherBase.
1883 * \param *out output stream for debugging
1884 * \param *Base reference line
1885 * \param *OtherBase other base line
1886 * \return Vector on reference line that has closest distance
1887 */
1888Vector * GetClosestPointBetweenLine(ofstream *out, class BoundaryLineSet *Base, class BoundaryLineSet *OtherBase)
1889{
1890 // construct the plane of the two baselines (i.e. take both their directional vectors)
1891 Vector Normal;
1892 Vector Baseline, OtherBaseline;
1893 Baseline.CopyVector(Base->endpoints[1]->node->node);
1894 Baseline.SubtractVector(Base->endpoints[0]->node->node);
1895 OtherBaseline.CopyVector(OtherBase->endpoints[1]->node->node);
1896 OtherBaseline.SubtractVector(OtherBase->endpoints[0]->node->node);
1897 Normal.CopyVector(&Baseline);
1898 Normal.VectorProduct(&OtherBaseline);
1899 Normal.Normalize();
1900 *out << Verbose(4) << "First direction is " << Baseline << ", second direction is " << OtherBaseline << ", normal of intersection plane is " << Normal << "." << endl;
1901
1902 // project one offset point of OtherBase onto this plane (and add plane offset vector)
1903 Vector NewOffset;
1904 NewOffset.CopyVector(OtherBase->endpoints[0]->node->node);
1905 NewOffset.ProjectOntoPlane(&Normal);
1906 NewOffset.AddVector(Base->endpoints[0]->node->node);
1907
1908 // calculate the intersection between this projected baseline and Base
1909 Vector *Intersection = new Vector;
1910 Intersection->GetIntersectionOfTwoLinesOnPlane(out, Base->endpoints[0]->node->node, Base->endpoints[1]->node->node, &NewOffset, &OtherBaseline, &Normal);
1911 Normal.CopyVector(Intersection);
1912 Normal.SubtractVector(Base->endpoints[0]->node->node);
1913 *out << Verbose(3) << "Found closest point on " << *Base << " at " << *Intersection << ", factor in line is " << fabs(Normal.ScalarProduct(&Baseline)/Baseline.NormSquared()) << "." << endl;
1914
1915 return Intersection;
1916};
1917
1918/** For a given baseline and its two connected triangles, flips the baseline.
1919 * I.e. we create the new baseline between the other two endpoints of these four
1920 * endpoints and reconstruct the two triangles accordingly.
1921 * \param *out output stream for debugging
1922 * \param *Base line to be flipped
1923 * \return true - flipping successful, false - something went awry
1924 */
1925bool Tesselation::FlipBaseline(ofstream *out, class BoundaryLineSet *Base)
1926{
1927 class BoundaryLineSet *OldLines[4], *NewLine;
1928 class BoundaryPointSet *OldPoints[2];
1929 Vector BaseLineNormal;
1930 int OldTriangleNrs[2], OldBaseLineNr;
1931 int i,m;
1932
1933 *out << Verbose(1) << "Begin of FlipBaseline" << endl;
1934
1935 // calculate NormalVector for later use
1936 BaseLineNormal.Zero();
1937 if (Base->triangles.size() < 2) {
1938 *out << Verbose(2) << "ERROR: Less than two triangles are attached to this baseline!" << endl;
1939 return false;
1940 }
1941 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
1942 *out << Verbose(4) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
1943 BaseLineNormal.AddVector(&(runner->second->NormalVector));
1944 }
1945 BaseLineNormal.Scale(-1./2.); // has to point inside for BoundaryTriangleSet::GetNormalVector()
1946
1947 // get the two triangles
1948 // gather four endpoints and four lines
1949 for (int j=0;j<4;j++)
1950 OldLines[j] = NULL;
1951 for (int j=0;j<2;j++)
1952 OldPoints[j] = NULL;
1953 i=0;
1954 m=0;
1955 *out << Verbose(3) << "The four old lines are: ";
1956 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
1957 for (int j=0;j<3;j++) // all of their endpoints and baselines
1958 if (runner->second->lines[j] != Base) { // pick not the central baseline
1959 OldLines[i++] = runner->second->lines[j];
1960 *out << *runner->second->lines[j] << "\t";
1961 }
1962 *out << endl;
1963 *out << Verbose(3) << "The two old points are: ";
1964 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
1965 for (int j=0;j<3;j++) // all of their endpoints and baselines
1966 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) { // and neither of its endpoints
1967 OldPoints[m++] = runner->second->endpoints[j];
1968 *out << *runner->second->endpoints[j] << "\t";
1969 }
1970 *out << endl;
1971
1972 // check whether everything is in place to create new lines and triangles
1973 if (i<4) {
1974 *out << Verbose(1) << "ERROR: We have not gathered enough baselines!" << endl;
1975 return false;
1976 }
1977 for (int j=0;j<4;j++)
1978 if (OldLines[j] == NULL) {
1979 *out << Verbose(1) << "ERROR: We have not gathered enough baselines!" << endl;
1980 return false;
1981 }
1982 for (int j=0;j<2;j++)
1983 if (OldPoints[j] == NULL) {
1984 *out << Verbose(1) << "ERROR: We have not gathered enough endpoints!" << endl;
1985 return false;
1986 }
1987
1988 // remove triangles and baseline removes itself
1989 *out << Verbose(3) << "INFO: Deleting baseline " << *Base << " from global list." << endl;
1990 OldBaseLineNr = Base->Nr;
1991 m=0;
1992 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
1993 *out << Verbose(3) << "INFO: Deleting triangle " << *(runner->second) << "." << endl;
1994 OldTriangleNrs[m++] = runner->second->Nr;
1995 RemoveTesselationTriangle(runner->second);
1996 }
1997
1998 // construct new baseline (with same number as old one)
1999 BPS[0] = OldPoints[0];
2000 BPS[1] = OldPoints[1];
2001 NewLine = new class BoundaryLineSet(BPS, OldBaseLineNr);
2002 LinesOnBoundary.insert(LinePair(OldBaseLineNr, NewLine)); // no need for check for unique insertion as NewLine is definitely a new one
2003 *out << Verbose(3) << "INFO: Created new baseline " << *NewLine << "." << endl;
2004
2005 // construct new triangles with flipped baseline
2006 i=-1;
2007 if (OldLines[0]->IsConnectedTo(OldLines[2]))
2008 i=2;
2009 if (OldLines[0]->IsConnectedTo(OldLines[3]))
2010 i=3;
2011 if (i!=-1) {
2012 BLS[0] = OldLines[0];
2013 BLS[1] = OldLines[i];
2014 BLS[2] = NewLine;
2015 BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[0]);
2016 BTS->GetNormalVector(BaseLineNormal);
2017 TrianglesOnBoundary.insert(TrianglePair(OldTriangleNrs[0], BTS));
2018 *out << Verbose(3) << "INFO: Created new triangle " << *BTS << "." << endl;
2019
2020 BLS[0] = (i==2 ? OldLines[3] : OldLines[2]);
2021 BLS[1] = OldLines[1];
2022 BLS[2] = NewLine;
2023 BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[1]);
2024 BTS->GetNormalVector(BaseLineNormal);
2025 TrianglesOnBoundary.insert(TrianglePair(OldTriangleNrs[1], BTS));
2026 *out << Verbose(3) << "INFO: Created new triangle " << *BTS << "." << endl;
2027 } else {
2028 *out << Verbose(1) << "The four old lines do not connect, something's utterly wrong here!" << endl;
2029 return false;
2030 }
2031
2032 *out << Verbose(1) << "End of FlipBaseline" << endl;
2033 return true;
2034};
2035
2036
2037/** Finds the second point of starting triangle.
2038 * \param *a first node
2039 * \param *Candidate pointer to candidate node on return
2040 * \param Oben vector indicating the outside
2041 * \param Opt_Candidate reference to recommended candidate on return
2042 * \param Storage[3] array storing angles and other candidate information
2043 * \param RADIUS radius of virtual sphere
2044 * \param *LC LinkedCell structure with neighbouring points
2045 */
2046void Tesselation::Find_second_point_for_Tesselation(TesselPoint* a, TesselPoint* Candidate, Vector Oben, TesselPoint*& Opt_Candidate, double Storage[3], double RADIUS, LinkedCell *LC)
2047{
2048 cout << Verbose(2) << "Begin of Find_second_point_for_Tesselation" << endl;
2049 Vector AngleCheck;
2050 double norm = -1., angle;
2051 LinkedNodes *List = NULL;
2052 int N[NDIM], Nlower[NDIM], Nupper[NDIM];
2053
2054 if (LC->SetIndexToNode(a)) { // get cell for the starting point
2055 for(int i=0;i<NDIM;i++) // store indices of this cell
2056 N[i] = LC->n[i];
2057 } else {
2058 cerr << "ERROR: Point " << *a << " is not found in cell " << LC->index << "." << endl;
2059 return;
2060 }
2061 // then go through the current and all neighbouring cells and check the contained points for possible candidates
2062 cout << Verbose(3) << "LC Intervals from [";
2063 for (int i=0;i<NDIM;i++) {
2064 cout << " " << N[i] << "<->" << LC->N[i];
2065 }
2066 cout << "] :";
2067 for (int i=0;i<NDIM;i++) {
2068 Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
2069 Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
2070 cout << " [" << Nlower[i] << "," << Nupper[i] << "] ";
2071 }
2072 cout << endl;
2073
2074
2075 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
2076 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
2077 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
2078 List = LC->GetCurrentCell();
2079 //cout << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
2080 if (List != NULL) {
2081 for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
2082 Candidate = (*Runner);
2083 // check if we only have one unique point yet ...
2084 if (a != Candidate) {
2085 // Calculate center of the circle with radius RADIUS through points a and Candidate
2086 Vector OrthogonalizedOben, a_Candidate, Center;
2087 double distance, scaleFactor;
2088
2089 OrthogonalizedOben.CopyVector(&Oben);
2090 a_Candidate.CopyVector(a->node);
2091 a_Candidate.SubtractVector(Candidate->node);
2092 OrthogonalizedOben.ProjectOntoPlane(&a_Candidate);
2093 OrthogonalizedOben.Normalize();
2094 distance = 0.5 * a_Candidate.Norm();
2095 scaleFactor = sqrt(((RADIUS * RADIUS) - (distance * distance)));
2096 OrthogonalizedOben.Scale(scaleFactor);
2097
2098 Center.CopyVector(Candidate->node);
2099 Center.AddVector(a->node);
2100 Center.Scale(0.5);
2101 Center.AddVector(&OrthogonalizedOben);
2102
2103 AngleCheck.CopyVector(&Center);
2104 AngleCheck.SubtractVector(a->node);
2105 norm = a_Candidate.Norm();
2106 // second point shall have smallest angle with respect to Oben vector
2107 if (norm < RADIUS*2.) {
2108 angle = AngleCheck.Angle(&Oben);
2109 if (angle < Storage[0]) {
2110 //cout << Verbose(3) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]);
2111 cout << Verbose(3) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n";
2112 Opt_Candidate = Candidate;
2113 Storage[0] = angle;
2114 //cout << Verbose(3) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[2]);
2115 } else {
2116 //cout << Verbose(3) << "Current candidate is " << *Candidate << ": Looses with angle " << angle << " to a better candidate " << *Opt_Candidate << endl;
2117 }
2118 } else {
2119 //cout << Verbose(3) << "Current candidate is " << *Candidate << ": Refused due to Radius " << norm << endl;
2120 }
2121 } else {
2122 //cout << Verbose(3) << "Current candidate is " << *Candidate << ": Candidate is equal to first endpoint." << *a << "." << endl;
2123 }
2124 }
2125 } else {
2126 cout << Verbose(3) << "Linked cell list is empty." << endl;
2127 }
2128 }
2129 cout << Verbose(2) << "End of Find_second_point_for_Tesselation" << endl;
2130};
2131
2132
2133/** This recursive function finds a third point, to form a triangle with two given ones.
2134 * Note that this function is for the starting triangle.
2135 * The idea is as follows: A sphere with fixed radius is (almost) uniquely defined in space by three points
2136 * that sit on its boundary. Hence, when two points are given and we look for the (next) third point, then
2137 * the center of the sphere is still fixed up to a single parameter. The band of possible values
2138 * describes a circle in 3D-space. The old center of the sphere for the current base triangle gives
2139 * us the "null" on this circle, the new center of the candidate point will be some way along this
2140 * circle. The shorter the way the better is the candidate. Note that the direction is clearly given
2141 * by the normal vector of the base triangle that always points outwards by construction.
2142 * Hence, we construct a Center of this circle which sits right in the middle of the current base line.
2143 * We construct the normal vector that defines the plane this circle lies in, it is just in the
2144 * direction of the baseline. And finally, we need the radius of the circle, which is given by the rest
2145 * with respect to the length of the baseline and the sphere's fixed \a RADIUS.
2146 * Note that there is one difficulty: The circumcircle is uniquely defined, but for the circumsphere's center
2147 * there are two possibilities which becomes clear from the construction as seen below. Hence, we must check
2148 * both.
2149 * Note also that the acos() function is not unique on [0, 2.*M_PI). Hence, we need an additional check
2150 * to decide for one of the two possible angles. Therefore we need a SearchDirection and to make this check
2151 * sensible we need OldSphereCenter to be orthogonal to it. Either we construct SearchDirection orthogonal
2152 * right away, or -- what we do here -- we rotate the relative sphere centers such that this orthogonality
2153 * holds. Then, the normalized projection onto the SearchDirection is either +1 or -1 and thus states whether
2154 * the angle is uniquely in either (0,M_PI] or [M_PI, 2.*M_PI).
2155 * @param NormalVector normal direction of the base triangle (here the unit axis vector, \sa Find_starting_triangle())
2156 * @param SearchDirection general direction where to search for the next point, relative to center of BaseLine
2157 * @param OldSphereCenter center of sphere for base triangle, relative to center of BaseLine, giving null angle for the parameter circle
2158 * @param BaseLine BoundaryLineSet with the current base line
2159 * @param ThirdNode third point to avoid in search
2160 * @param candidates list of equally good candidates to return
2161 * @param ShortestAngle the current path length on this circle band for the current Opt_Candidate
2162 * @param RADIUS radius of sphere
2163 * @param *LC LinkedCell structure with neighbouring points
2164 */
2165void Tesselation::Find_third_point_for_Tesselation(Vector NormalVector, Vector SearchDirection, Vector OldSphereCenter, class BoundaryLineSet *BaseLine, class TesselPoint *ThirdNode, CandidateList* &candidates, double *ShortestAngle, const double RADIUS, LinkedCell *LC)
2166{
2167 Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
2168 Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
2169 Vector SphereCenter;
2170 Vector NewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility
2171 Vector OtherNewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility
2172 Vector NewNormalVector; // normal vector of the Candidate's triangle
2173 Vector helper, OptCandidateCenter, OtherOptCandidateCenter;
2174 LinkedNodes *List = NULL;
2175 double CircleRadius; // radius of this circle
2176 double radius;
2177 double alpha, Otheralpha; // angles (i.e. parameter for the circle).
2178 int N[NDIM], Nlower[NDIM], Nupper[NDIM];
2179 TesselPoint *Candidate = NULL;
2180 CandidateForTesselation *optCandidate = NULL;
2181
2182 cout << Verbose(1) << "Begin of Find_third_point_for_Tesselation" << endl;
2183
2184 //cout << Verbose(2) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl;
2185
2186 // construct center of circle
2187 CircleCenter.CopyVector(BaseLine->endpoints[0]->node->node);
2188 CircleCenter.AddVector(BaseLine->endpoints[1]->node->node);
2189 CircleCenter.Scale(0.5);
2190
2191 // construct normal vector of circle
2192 CirclePlaneNormal.CopyVector(BaseLine->endpoints[0]->node->node);
2193 CirclePlaneNormal.SubtractVector(BaseLine->endpoints[1]->node->node);
2194
2195 // calculate squared radius TesselPoint *ThirdNode,f circle
2196 radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
2197 if (radius/4. < RADIUS*RADIUS) {
2198 CircleRadius = RADIUS*RADIUS - radius/4.;
2199 CirclePlaneNormal.Normalize();
2200 //cout << Verbose(2) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
2201
2202 // test whether old center is on the band's plane
2203 if (fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) > HULLEPSILON) {
2204 cerr << "ERROR: Something's very wrong here: OldSphereCenter is not on the band's plane as desired by " << fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) << "!" << endl;
2205 OldSphereCenter.ProjectOntoPlane(&CirclePlaneNormal);
2206 }
2207 radius = OldSphereCenter.ScalarProduct(&OldSphereCenter);
2208 if (fabs(radius - CircleRadius) < HULLEPSILON) {
2209
2210 // check SearchDirection
2211 //cout << Verbose(2) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
2212 if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) { // rotated the wrong way!
2213 cerr << "ERROR: SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl;
2214 }
2215
2216 // get cell for the starting point
2217 if (LC->SetIndexToVector(&CircleCenter)) {
2218 for(int i=0;i<NDIM;i++) // store indices of this cell
2219 N[i] = LC->n[i];
2220 //cout << Verbose(2) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
2221 } else {
2222 cerr << "ERROR: Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl;
2223 return;
2224 }
2225 // then go through the current and all neighbouring cells and check the contained points for possible candidates
2226 //cout << Verbose(2) << "LC Intervals:";
2227 for (int i=0;i<NDIM;i++) {
2228 Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
2229 Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
2230 //cout << " [" << Nlower[i] << "," << Nupper[i] << "] ";
2231 }
2232 //cout << endl;
2233 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
2234 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
2235 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
2236 List = LC->GetCurrentCell();
2237 //cout << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
2238 if (List != NULL) {
2239 for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
2240 Candidate = (*Runner);
2241
2242 // check for three unique points
2243 //cout << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " at " << Candidate->node << "." << endl;
2244 if ((Candidate != BaseLine->endpoints[0]->node) && (Candidate != BaseLine->endpoints[1]->node) ){
2245
2246 // construct both new centers
2247 GetCenterofCircumcircle(&NewSphereCenter, BaseLine->endpoints[0]->node->node, BaseLine->endpoints[1]->node->node, Candidate->node);
2248 OtherNewSphereCenter.CopyVector(&NewSphereCenter);
2249
2250 if ((NewNormalVector.MakeNormalVector(BaseLine->endpoints[0]->node->node, BaseLine->endpoints[1]->node->node, Candidate->node))
2251 && (fabs(NewNormalVector.ScalarProduct(&NewNormalVector)) > HULLEPSILON)
2252 ) {
2253 helper.CopyVector(&NewNormalVector);
2254 //cout << Verbose(2) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl;
2255 radius = BaseLine->endpoints[0]->node->node->DistanceSquared(&NewSphereCenter);
2256 if (radius < RADIUS*RADIUS) {
2257 helper.Scale(sqrt(RADIUS*RADIUS - radius));
2258 //cout << Verbose(2) << "INFO: Distance of NewCircleCenter to NewSphereCenter is " << helper.Norm() << " with sphere radius " << RADIUS << "." << endl;
2259 NewSphereCenter.AddVector(&helper);
2260 NewSphereCenter.SubtractVector(&CircleCenter);
2261 //cout << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl;
2262
2263 // OtherNewSphereCenter is created by the same vector just in the other direction
2264 helper.Scale(-1.);
2265 OtherNewSphereCenter.AddVector(&helper);
2266 OtherNewSphereCenter.SubtractVector(&CircleCenter);
2267 //cout << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl;
2268
2269 alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
2270 Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
2271 alpha = min(alpha, Otheralpha);
2272 // if there is a better candidate, drop the current list and add the new candidate
2273 // otherwise ignore the new candidate and keep the list
2274 if (*ShortestAngle > (alpha - HULLEPSILON)) {
2275 optCandidate = new CandidateForTesselation(Candidate, BaseLine, OptCandidateCenter, OtherOptCandidateCenter);
2276 if (fabs(alpha - Otheralpha) > MYEPSILON) {
2277 optCandidate->OptCenter.CopyVector(&NewSphereCenter);
2278 optCandidate->OtherOptCenter.CopyVector(&OtherNewSphereCenter);
2279 } else {
2280 optCandidate->OptCenter.CopyVector(&OtherNewSphereCenter);
2281 optCandidate->OtherOptCenter.CopyVector(&NewSphereCenter);
2282 }
2283 // if there is an equal candidate, add it to the list without clearing the list
2284 if ((*ShortestAngle - HULLEPSILON) < alpha) {
2285 candidates->push_back(optCandidate);
2286 cout << Verbose(2) << "ACCEPT: We have found an equally good candidate: " << *(optCandidate->point) << " with "
2287 << alpha << " and circumsphere's center at " << optCandidate->OptCenter << "." << endl;
2288 } else {
2289 // remove all candidates from the list and then the list itself
2290 class CandidateForTesselation *remover = NULL;
2291 for (CandidateList::iterator it = candidates->begin(); it != candidates->end(); ++it) {
2292 remover = *it;
2293 delete(remover);
2294 }
2295 candidates->clear();
2296 candidates->push_back(optCandidate);
2297 cout << Verbose(2) << "ACCEPT: We have found a better candidate: " << *(optCandidate->point) << " with "
2298 << alpha << " and circumsphere's center at " << optCandidate->OptCenter << "." << endl;
2299 }
2300 *ShortestAngle = alpha;
2301 //cout << Verbose(2) << "INFO: There are " << candidates->size() << " candidates in the list now." << endl;
2302 } else {
2303 if ((optCandidate != NULL) && (optCandidate->point != NULL)) {
2304 //cout << Verbose(2) << "REJECT: Old candidate " << *(optCandidate->point) << " with " << *ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl;
2305 } else {
2306 //cout << Verbose(2) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl;
2307 }
2308 }
2309
2310 } else {
2311 //cout << Verbose(2) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl;
2312 }
2313 } else {
2314 //cout << Verbose(2) << "REJECT: Three points from " << *BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl;
2315 }
2316 } else {
2317 if (ThirdNode != NULL) {
2318 //cout << Verbose(2) << "REJECT: Base triangle " << *BaseLine << " and " << *ThirdNode << " contains Candidate " << *Candidate << "." << endl;
2319 } else {
2320 //cout << Verbose(2) << "REJECT: Base triangle " << *BaseLine << " contains Candidate " << *Candidate << "." << endl;
2321 }
2322 }
2323 }
2324 }
2325 }
2326 } else {
2327 cerr << Verbose(2) << "ERROR: The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl;
2328 }
2329 } else {
2330 if (ThirdNode != NULL)
2331 cout << Verbose(2) << "Circumcircle for base line " << *BaseLine << " and third node " << *ThirdNode << " is too big!" << endl;
2332 else
2333 cout << Verbose(2) << "Circumcircle for base line " << *BaseLine << " is too big!" << endl;
2334 }
2335
2336 //cout << Verbose(2) << "INFO: Sorting candidate list ..." << endl;
2337 if (candidates->size() > 1) {
2338 candidates->unique();
2339 candidates->sort(sortCandidates);
2340 }
2341
2342 cout << Verbose(1) << "End of Find_third_point_for_Tesselation" << endl;
2343};
2344
2345/** Finds the endpoint two lines are sharing.
2346 * \param *line1 first line
2347 * \param *line2 second line
2348 * \return point which is shared or NULL if none
2349 */
2350class BoundaryPointSet *Tesselation::GetCommonEndpoint(class BoundaryLineSet * line1, class BoundaryLineSet * line2)
2351{
2352 class BoundaryLineSet * lines[2] =
2353 { line1, line2 };
2354 class BoundaryPointSet *node = NULL;
2355 map<int, class BoundaryPointSet *> OrderMap;
2356 pair<map<int, class BoundaryPointSet *>::iterator, bool> OrderTest;
2357 for (int i = 0; i < 2; i++)
2358 // for both lines
2359 for (int j = 0; j < 2; j++)
2360 { // for both endpoints
2361 OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (
2362 lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
2363 if (!OrderTest.second)
2364 { // if insertion fails, we have common endpoint
2365 node = OrderTest.first->second;
2366 cout << Verbose(5) << "Common endpoint of lines " << *line1
2367 << " and " << *line2 << " is: " << *node << "." << endl;
2368 j = 2;
2369 i = 2;
2370 break;
2371 }
2372 }
2373 return node;
2374};
2375
2376/** Finds the triangle that is closest to a given Vector \a *x.
2377 * \param *out output stream for debugging
2378 * \param *x Vector to look from
2379 * \return list of BoundaryTriangleSet of nearest triangles or NULL in degenerate case.
2380 */
2381list<BoundaryTriangleSet*> * Tesselation::FindClosestTrianglesToPoint(ofstream *out, Vector *x, LinkedCell* LC)
2382{
2383 TesselPoint *trianglePoints[3];
2384 TesselPoint *SecondPoint = NULL;
2385
2386 if (LinesOnBoundary.empty()) {
2387 *out << Verbose(0) << "Error: There is no tesselation structure to compare the point with, please create one first.";
2388 return NULL;
2389 }
2390
2391 trianglePoints[0] = findClosestPoint(x, SecondPoint, LC);
2392
2393 // check whether closest point is "too close" :), then it's inside
2394 if (trianglePoints[0] == NULL) {
2395 *out << Verbose(1) << "Is the only point, no one else is closeby." << endl;
2396 return NULL;
2397 }
2398 if (trianglePoints[0]->node->DistanceSquared(x) < MYEPSILON) {
2399 *out << Verbose(1) << "Point is right on a tesselation point, no nearest triangle." << endl;
2400 return NULL;
2401 }
2402 list<TesselPoint*> *connectedPoints = getCircleOfConnectedPoints(out, trianglePoints[0]);
2403 list<TesselPoint*> *connectedClosestPoints = getNeighboursonCircleofConnectedPoints(out, connectedPoints, trianglePoints[0], x);
2404 delete(connectedPoints);
2405 trianglePoints[1] = connectedClosestPoints->front();
2406 trianglePoints[2] = connectedClosestPoints->back();
2407 for (int i=0;i<3;i++) {
2408 if (trianglePoints[i] == NULL) {
2409 *out << Verbose(1) << "IsInnerPoint encounters serious error, point " << i << " not found." << endl;
2410 }
2411 //*out << Verbose(1) << "List of possible points:" << endl;
2412 //*out << Verbose(2) << *trianglePoints[i] << endl;
2413 }
2414
2415 list<BoundaryTriangleSet*> *triangles = FindTriangles(trianglePoints);
2416
2417 delete(connectedClosestPoints);
2418
2419 if (triangles->empty()) {
2420 *out << Verbose(0) << "Error: There is no nearest triangle. Please check the tesselation structure.";
2421 return NULL;
2422 } else
2423 return triangles;
2424};
2425
2426/** Finds closest triangle to a point.
2427 * This basically just takes care of the degenerate case, which is not handled in FindClosestTrianglesToPoint().
2428 * \param *out output stream for debugging
2429 * \param *x Vector to look from
2430 * \return list of BoundaryTriangleSet of nearest triangles or NULL.
2431 */
2432class BoundaryTriangleSet * Tesselation::FindClosestTriangleToPoint(ofstream *out, Vector *x, LinkedCell* LC)
2433{
2434 class BoundaryTriangleSet *result = NULL;
2435 list<BoundaryTriangleSet*> *triangles = FindClosestTrianglesToPoint(out, x, LC);
2436
2437 if (triangles == NULL)
2438 return NULL;
2439
2440 if (x->ScalarProduct(&triangles->front()->NormalVector) < 0)
2441 result = triangles->back();
2442 else
2443 result = triangles->front();
2444
2445 delete(triangles);
2446 return result;
2447};
2448
2449/** Checks whether the provided Vector is within the tesselation structure.
2450 *
2451 * @param point of which to check the position
2452 * @param *LC LinkedCell structure
2453 *
2454 * @return true if the point is inside the tesselation structure, false otherwise
2455 */
2456bool Tesselation::IsInnerPoint(ofstream *out, Vector Point, LinkedCell* LC)
2457{
2458 class BoundaryTriangleSet *result = FindClosestTriangleToPoint(out, &Point, LC);
2459 if (result == NULL)
2460 return true;
2461 if (Point.ScalarProduct(&result->NormalVector) < 0)
2462 return true;
2463 else
2464 return false;
2465}
2466
2467/** Checks whether the provided TesselPoint is within the tesselation structure.
2468 *
2469 * @param *Point of which to check the position
2470 * @param *LC Linked Cell structure
2471 *
2472 * @return true if the point is inside the tesselation structure, false otherwise
2473 */
2474bool Tesselation::IsInnerPoint(ofstream *out, TesselPoint *Point, LinkedCell* LC)
2475{
2476 class BoundaryTriangleSet *result = FindClosestTriangleToPoint(out, Point->node, LC);
2477 if (result == NULL)
2478 return true;
2479 if (Point->node->ScalarProduct(&result->NormalVector) < 0)
2480 return true;
2481 else
2482 return false;
2483}
2484
2485/** Gets all points connected to the provided point by triangulation lines.
2486 *
2487 * @param *Point of which get all connected points
2488 *
2489 * @return list of the all points linked to the provided one
2490 */
2491list<TesselPoint*> * Tesselation::getCircleOfConnectedPoints(ofstream *out, TesselPoint* Point)
2492{
2493 list<TesselPoint*> *connectedPoints = new list<TesselPoint*>;
2494 class BoundaryPointSet *ReferencePoint = NULL;
2495 TesselPoint* current;
2496 bool takePoint = false;
2497
2498 // find the respective boundary point
2499 PointMap::iterator PointRunner = PointsOnBoundary.find(Point->nr);
2500 if (PointRunner != PointsOnBoundary.end()) {
2501 ReferencePoint = PointRunner->second;
2502 } else {
2503 *out << Verbose(2) << "getCircleOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl;
2504 ReferencePoint = NULL;
2505 }
2506
2507 // little trick so that we look just through lines connect to the BoundaryPoint
2508 // OR fall-back to look through all lines if there is no such BoundaryPoint
2509 LineMap *Lines = &LinesOnBoundary;
2510 if (ReferencePoint != NULL)
2511 Lines = &(ReferencePoint->lines);
2512 LineMap::iterator findLines = Lines->begin();
2513 while (findLines != Lines->end()) {
2514 takePoint = false;
2515
2516 if (findLines->second->endpoints[0]->Nr == Point->nr) {
2517 takePoint = true;
2518 current = findLines->second->endpoints[1]->node;
2519 } else if (findLines->second->endpoints[1]->Nr == Point->nr) {
2520 takePoint = true;
2521 current = findLines->second->endpoints[0]->node;
2522 }
2523
2524 if (takePoint) {
2525 *out << Verbose(3) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is taken into the circle." << endl;
2526 connectedPoints->push_back(current);
2527 }
2528
2529 findLines++;
2530 }
2531
2532 if (connectedPoints->size() == 0) { // if have not found any points
2533 *out << Verbose(1) << "ERROR: We have not found any connected points to " << *Point<< "." << endl;
2534 return NULL;
2535 }
2536 return connectedPoints;
2537}
2538
2539/** Gets the two neighbouring points with respect to a reference line to the provided point.
2540 * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points
2541 * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given
2542 * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the
2543 * triangle we are looking for.
2544 *
2545 * @param *out output stream for debugging
2546 * @param *connectedPoints list of connected points to the central \a *Point
2547 * @param *Point of which get all connected points
2548 * @param *Reference Vector to be checked whether it is an inner point
2549 *
2550 * @return list of the two points linked to the provided one and closest to the point to be checked,
2551 */
2552list<TesselPoint*> * Tesselation::getNeighboursonCircleofConnectedPoints(ofstream *out, list<TesselPoint*> *connectedPoints, TesselPoint* Point, Vector* Reference)
2553{
2554 map<double, TesselPoint*> anglesOfPoints;
2555 map<double, TesselPoint*>::iterator runner;
2556 ;
2557 Vector center, PlaneNormal, OrthogonalVector, helper, AngleZero;
2558
2559 if (connectedPoints->size() == 0) { // if have not found any points
2560 *out << Verbose(1) << "ERROR: We have not found any connected points to " << *Point<< "." << endl;
2561 return NULL;
2562 }
2563
2564 // calculate central point
2565 for (list<TesselPoint*>::iterator TesselRunner = connectedPoints->begin(); TesselRunner != connectedPoints->end(); TesselRunner++)
2566 center.AddVector((*TesselRunner)->node);
2567 //*out << "Summed vectors " << center << "; number of points " << connectedPoints.size()
2568 // << "; scale factor " << 1.0/connectedPoints.size();
2569 center.Scale(1.0/connectedPoints->size());
2570 *out << Verbose(4) << "INFO: Calculated center of all circle points is " << center << "." << endl;
2571
2572 // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points
2573 PlaneNormal.CopyVector(Point->node);
2574 PlaneNormal.SubtractVector(&center);
2575 PlaneNormal.Normalize();
2576 *out << Verbose(4) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl;
2577
2578 // construct one orthogonal vector
2579 AngleZero.CopyVector(Reference);
2580 AngleZero.SubtractVector(Point->node);
2581 AngleZero.ProjectOntoPlane(&PlaneNormal);
2582 *out << Verbose(4) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl;
2583 OrthogonalVector.MakeNormalVector(&PlaneNormal, &AngleZero);
2584 *out << Verbose(4) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl;
2585
2586 // go through all connected points and calculate angle
2587 for (list<TesselPoint*>::iterator listRunner = connectedPoints->begin(); listRunner != connectedPoints->end(); listRunner++) {
2588 helper.CopyVector((*listRunner)->node);
2589 helper.SubtractVector(Point->node);
2590 helper.ProjectOntoPlane(&PlaneNormal);
2591 double angle = getAngle(helper, AngleZero, OrthogonalVector);
2592 *out << Verbose(2) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl;
2593 anglesOfPoints.insert(pair<double, TesselPoint*>(angle, (*listRunner)));
2594 }
2595
2596 list<TesselPoint*> *result = new list<TesselPoint*>;
2597 runner = anglesOfPoints.begin();
2598 result->push_back(runner->second);
2599 runner = anglesOfPoints.end();
2600 runner--;
2601 result->push_back(runner->second);
2602
2603 *out << Verbose(2) << "List of closest points has " << result->size() << " elements, which are "
2604 << *(result->front()) << " and " << *(result->back()) << endl;
2605
2606 return result;
2607}
2608
2609/** Removes a boundary point from the envelope while keeping it closed.
2610 * We create new triangles and remove the old ones connected to the point.
2611 * \param *out output stream for debugging
2612 * \param *point point to be removed
2613 * \return volume added to the volume inside the tesselated surface by the removal
2614 */
2615double Tesselation::RemovePointFromTesselatedSurface(ofstream *out, class BoundaryPointSet *point) {
2616 class BoundaryLineSet *line = NULL;
2617 class BoundaryTriangleSet *triangle = NULL;
2618 Vector OldPoint, TetraederVector[3];
2619 double volume = 0;
2620 int *numbers = NULL;
2621 int count = 0;
2622 int i;
2623
2624 // copy old location for the volume
2625 OldPoint.CopyVector(point->node->node);
2626
2627 // get list of connected points
2628 if (point->lines.empty()) {
2629 *out << Verbose(1) << "ERROR: Cannot remove the point " << *point << ", it's connected to no lines!" << endl;
2630 return 0.;
2631 }
2632 list<TesselPoint*> *CircleofPoints = getCircleOfConnectedPoints(out, point->node);
2633
2634 // remove all triangles
2635 for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++)
2636 count+=LineRunner->second->triangles.size();
2637 numbers = new int[count];
2638 i=0;
2639 for (LineMap::iterator LineRunner = point->lines.begin(); (point != NULL) && (LineRunner != point->lines.end()); LineRunner++) {
2640 line = LineRunner->second;
2641 for (TriangleMap::iterator TriangleRunner = line->triangles.begin(); TriangleRunner != line->triangles.end(); TriangleRunner++) {
2642 triangle = TriangleRunner->second;
2643 *out << Verbose(2) << "Erasing triangle " << *triangle << "." << endl;
2644 numbers[i++] = triangle->Nr;
2645 RemoveTesselationTriangle(triangle);
2646 triangle = NULL;
2647 }
2648 }
2649 *out << Verbose(1) << i << " triangles were removed." << endl;
2650
2651 // re-create all triangles by going through connected points list
2652 list<TesselPoint*>::iterator CircleRunner = CircleofPoints->begin();
2653 list<TesselPoint*>::iterator OtherCircleRunner = CircleofPoints->begin();
2654 class TesselPoint *CentralNode = *CircleRunner;
2655 // advance two with CircleRunner and one with OtherCircleRunner
2656 CircleRunner++;
2657 CircleRunner++;
2658 OtherCircleRunner++;
2659 i=0;
2660 cout << Verbose(2) << "INFO: CentralNode is " << *CentralNode << "." << endl;
2661 for (; (OtherCircleRunner != CircleofPoints->end()) && (CircleRunner != CircleofPoints->end()); (CircleRunner++), (OtherCircleRunner++)) {
2662 cout << Verbose(3) << "INFO: CircleRunner's node is " << **CircleRunner << "." << endl;
2663 cout << Verbose(3) << "INFO: OtherCircleRunner's node is " << **OtherCircleRunner << "." << endl;
2664 *out << Verbose(4) << "Adding new triangle points."<< endl;
2665 AddTesselationPoint(CentralNode, 0);
2666 AddTesselationPoint(*OtherCircleRunner, 1);
2667 AddTesselationPoint(*CircleRunner, 2);
2668 *out << Verbose(4) << "Adding new triangle lines."<< endl;
2669 AddTesselationLine(TPS[0], TPS[1], 0);
2670 AddTesselationLine(TPS[0], TPS[2], 1);
2671 AddTesselationLine(TPS[1], TPS[2], 2);
2672 BTS = new class BoundaryTriangleSet(BLS, numbers[i]);
2673 TrianglesOnBoundary.insert(TrianglePair(numbers[i], BTS));
2674 *out << Verbose(4) << "Created triangle " << *BTS << "." << endl;
2675 // calculate volume summand as a general tetraeder
2676 for (int j=0;j<3;j++) {
2677 TetraederVector[j].CopyVector(TPS[j]->node->node);
2678 TetraederVector[j].SubtractVector(&OldPoint);
2679 }
2680 OldPoint.CopyVector(&TetraederVector[0]);
2681 OldPoint.VectorProduct(&TetraederVector[1]);
2682 volume += 1./6. * fabs(OldPoint.ScalarProduct(&TetraederVector[2]));
2683 // advance number
2684 i++;
2685 if (i >= count)
2686 *out << Verbose(2) << "WARNING: Maximum of numbers reached!" << endl;
2687 }
2688 *out << Verbose(1) << i << " triangles were created." << endl;
2689
2690 delete[](numbers);
2691
2692 return volume;
2693};
2694
2695/** Checks for a new special triangle whether one of its edges is already present with one one triangle connected.
2696 * This enforces that special triangles (i.e. degenerated ones) should at last close the open-edge frontier and not
2697 * make it bigger (i.e. closing one (the baseline) and opening two new ones).
2698 * \param TPS[3] nodes of the triangle
2699 * \return true - there is such a line (i.e. creation of degenerated triangle is valid), false - no such line (don't create)
2700 */
2701bool CheckLineCriteriaforDegeneratedTriangle(class BoundaryPointSet *nodes[3])
2702{
2703 bool result = false;
2704 int counter = 0;
2705
2706 // check all three points
2707 for (int i=0;i<3;i++)
2708 for (int j=i+1; j<3; j++) {
2709 if (nodes[i]->lines.find(nodes[j]->node->nr) != nodes[i]->lines.end()) { // there already is a line
2710 LineMap::iterator FindLine;
2711 pair<LineMap::iterator,LineMap::iterator> FindPair;
2712 FindPair = nodes[i]->lines.equal_range(nodes[j]->node->nr);
2713 for (FindLine = FindPair.first; FindLine != FindPair.second; ++FindLine) {
2714 // If there is a line with less than two attached triangles, we don't need a new line.
2715 if (FindLine->second->triangles.size() < 2) {
2716 counter++;
2717 break; // increase counter only once per edge
2718 }
2719 }
2720 } else { // no line
2721 cout << Verbose(1) << "The line between " << nodes[i] << " and " << nodes[j] << " is not yet present, hence no need for a degenerate triangle." << endl;
2722 result = true;
2723 }
2724 }
2725 if (counter > 1) {
2726 cout << Verbose(2) << "INFO: Degenerate triangle is ok, at least two, here " << counter << ", existing lines are used." << endl;
2727 result = true;
2728 }
2729 return result;
2730};
2731
2732
2733/** Sort function for the candidate list.
2734 */
2735bool sortCandidates(CandidateForTesselation* candidate1, CandidateForTesselation* candidate2)
2736{
2737 Vector BaseLineVector, OrthogonalVector, helper;
2738 if (candidate1->BaseLine != candidate2->BaseLine) { // sanity check
2739 cout << Verbose(0) << "ERROR: sortCandidates was called for two different baselines: " << candidate1->BaseLine << " and " << candidate2->BaseLine << "." << endl;
2740 //return false;
2741 exit(1);
2742 }
2743 // create baseline vector
2744 BaseLineVector.CopyVector(candidate1->BaseLine->endpoints[1]->node->node);
2745 BaseLineVector.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
2746 BaseLineVector.Normalize();
2747
2748 // create normal in-plane vector to cope with acos() non-uniqueness on [0,2pi] (note that is pointing in the "right" direction already, hence ">0" test!)
2749 helper.CopyVector(candidate1->BaseLine->endpoints[0]->node->node);
2750 helper.SubtractVector(candidate1->point->node);
2751 OrthogonalVector.CopyVector(&helper);
2752 helper.VectorProduct(&BaseLineVector);
2753 OrthogonalVector.SubtractVector(&helper);
2754 OrthogonalVector.Normalize();
2755
2756 // calculate both angles and correct with in-plane vector
2757 helper.CopyVector(candidate1->point->node);
2758 helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
2759 double phi = BaseLineVector.Angle(&helper);
2760 if (OrthogonalVector.ScalarProduct(&helper) > 0) {
2761 phi = 2.*M_PI - phi;
2762 }
2763 helper.CopyVector(candidate2->point->node);
2764 helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
2765 double psi = BaseLineVector.Angle(&helper);
2766 if (OrthogonalVector.ScalarProduct(&helper) > 0) {
2767 psi = 2.*M_PI - psi;
2768 }
2769
2770 cout << Verbose(2) << *candidate1->point << " has angle " << phi << endl;
2771 cout << Verbose(2) << *candidate2->point << " has angle " << psi << endl;
2772
2773 // return comparison
2774 return phi < psi;
2775};
2776
2777/**
2778 * Finds the point which is second closest to the provided one.
2779 *
2780 * @param Point to which to find the second closest other point
2781 * @param linked cell structure
2782 *
2783 * @return point which is second closest to the provided one
2784 */
2785TesselPoint* findSecondClosestPoint(const Vector* Point, LinkedCell* LC)
2786{
2787 LinkedNodes *List = NULL;
2788 TesselPoint* closestPoint = NULL;
2789 TesselPoint* secondClosestPoint = NULL;
2790 double distance = 1e16;
2791 double secondDistance = 1e16;
2792 Vector helper;
2793 int N[NDIM], Nlower[NDIM], Nupper[NDIM];
2794
2795 LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
2796 for(int i=0;i<NDIM;i++) // store indices of this cell
2797 N[i] = LC->n[i];
2798 cout << Verbose(2) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
2799
2800 LC->GetNeighbourBounds(Nlower, Nupper);
2801 //cout << endl;
2802 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
2803 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
2804 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
2805 List = LC->GetCurrentCell();
2806 cout << Verbose(3) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
2807 if (List != NULL) {
2808 for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
2809 helper.CopyVector(Point);
2810 helper.SubtractVector((*Runner)->node);
2811 double currentNorm = helper. Norm();
2812 if (currentNorm < distance) {
2813 // remember second point
2814 secondDistance = distance;
2815 secondClosestPoint = closestPoint;
2816 // mark down new closest point
2817 distance = currentNorm;
2818 closestPoint = (*Runner);
2819 cout << Verbose(2) << "INFO: New Nearest Neighbour is " << *closestPoint << "." << endl;
2820 }
2821 }
2822 } else {
2823 cerr << "ERROR: The current cell " << LC->n[0] << "," << LC->n[1] << ","
2824 << LC->n[2] << " is invalid!" << endl;
2825 }
2826 }
2827
2828 return secondClosestPoint;
2829};
2830
2831
2832
2833/**
2834 * Finds the point which is closest to the provided one.
2835 *
2836 * @param Point to which to find the closest other point
2837 * @param SecondPoint the second closest other point on return, NULL if none found
2838 * @param linked cell structure
2839 *
2840 * @return point which is closest to the provided one, NULL if none found
2841 */
2842TesselPoint* findClosestPoint(const Vector* Point, TesselPoint *&SecondPoint, LinkedCell* LC)
2843{
2844 LinkedNodes *List = NULL;
2845 TesselPoint* closestPoint = NULL;
2846 SecondPoint = NULL;
2847 double distance = 1e16;
2848 double secondDistance = 1e16;
2849 Vector helper;
2850 int N[NDIM], Nlower[NDIM], Nupper[NDIM];
2851
2852 LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
2853 for(int i=0;i<NDIM;i++) // store indices of this cell
2854 N[i] = LC->n[i];
2855 cout << Verbose(2) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
2856
2857 LC->GetNeighbourBounds(Nlower, Nupper);
2858 //cout << endl;
2859 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
2860 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
2861 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
2862 List = LC->GetCurrentCell();
2863 cout << Verbose(3) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
2864 if (List != NULL) {
2865 for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
2866 helper.CopyVector(Point);
2867 helper.SubtractVector((*Runner)->node);
2868 double currentNorm = helper. Norm();
2869 if (currentNorm < distance) {
2870 secondDistance = distance;
2871 SecondPoint = closestPoint;
2872 distance = currentNorm;
2873 closestPoint = (*Runner);
2874 cout << Verbose(2) << "INFO: New Nearest Neighbour is " << *closestPoint << "." << endl;
2875 } else if (currentNorm < secondDistance) {
2876 secondDistance = currentNorm;
2877 SecondPoint = (*Runner);
2878 cout << Verbose(2) << "INFO: New Second Nearest Neighbour is " << *SecondPoint << "." << endl;
2879 }
2880 }
2881 } else {
2882 cerr << "ERROR: The current cell " << LC->n[0] << "," << LC->n[1] << ","
2883 << LC->n[2] << " is invalid!" << endl;
2884 }
2885 }
2886
2887 return closestPoint;
2888};
2889
2890
2891/**
2892 * Finds triangles belonging to the three provided points.
2893 *
2894 * @param *Points[3] list, is expected to contain three points
2895 *
2896 * @return triangles which belong to the provided points, will be empty if there are none,
2897 * will usually be one, in case of degeneration, there will be two
2898 */
2899list<BoundaryTriangleSet*> *Tesselation::FindTriangles(TesselPoint* Points[3])
2900{
2901 list<BoundaryTriangleSet*> *result = new list<BoundaryTriangleSet*>;
2902 LineMap::iterator FindLine;
2903 PointMap::iterator FindPoint;
2904 TriangleMap::iterator FindTriangle;
2905 class BoundaryPointSet *TrianglePoints[3];
2906
2907 for (int i = 0; i < 3; i++) {
2908 FindPoint = PointsOnBoundary.find(Points[i]->nr);
2909 if (FindPoint != PointsOnBoundary.end()) {
2910 TrianglePoints[i] = FindPoint->second;
2911 } else {
2912 TrianglePoints[i] = NULL;
2913 }
2914 }
2915
2916 // checks lines between the points in the Points for their adjacent triangles
2917 for (int i = 0; i < 3; i++) {
2918 if (TrianglePoints[i] != NULL) {
2919 for (int j = i; j < 3; j++) {
2920 if (TrianglePoints[j] != NULL) {
2921 FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr);
2922 if (FindLine != TrianglePoints[i]->lines.end()) {
2923 for (; FindLine->first == TrianglePoints[j]->node->nr; FindLine++) {
2924 FindTriangle = FindLine->second->triangles.begin();
2925 for (; FindTriangle != FindLine->second->triangles.end(); FindTriangle++) {
2926 if ((
2927 (FindTriangle->second->endpoints[0] == TrianglePoints[0])
2928 || (FindTriangle->second->endpoints[0] == TrianglePoints[1])
2929 || (FindTriangle->second->endpoints[0] == TrianglePoints[2])
2930 ) && (
2931 (FindTriangle->second->endpoints[1] == TrianglePoints[0])
2932 || (FindTriangle->second->endpoints[1] == TrianglePoints[1])
2933 || (FindTriangle->second->endpoints[1] == TrianglePoints[2])
2934 ) && (
2935 (FindTriangle->second->endpoints[2] == TrianglePoints[0])
2936 || (FindTriangle->second->endpoints[2] == TrianglePoints[1])
2937 || (FindTriangle->second->endpoints[2] == TrianglePoints[2])
2938 )
2939 ) {
2940 result->push_back(FindTriangle->second);
2941 }
2942 }
2943 }
2944 // Is it sufficient to consider one of the triangle lines for this.
2945 return result;
2946
2947 }
2948 }
2949 }
2950 }
2951 }
2952
2953 return result;
2954}
2955
2956/** Gets the angle between a point and a reference relative to the provided center.
2957 * We have two shanks point and reference between which the angle is calculated
2958 * and by scalar product with OrthogonalVector we decide the interval.
2959 * @param point to calculate the angle for
2960 * @param reference to which to calculate the angle
2961 * @param OrthogonalVector points in direction of [pi,2pi] interval
2962 *
2963 * @return angle between point and reference
2964 */
2965double getAngle(const Vector &point, const Vector &reference, const Vector OrthogonalVector)
2966{
2967 if (reference.IsZero())
2968 return M_PI;
2969
2970 // calculate both angles and correct with in-plane vector
2971 if (point.IsZero())
2972 return M_PI;
2973 double phi = point.Angle(&reference);
2974 if (OrthogonalVector.ScalarProduct(&point) > 0) {
2975 phi = 2.*M_PI - phi;
2976 }
2977
2978 cout << Verbose(3) << "INFO: " << point << " has angle " << phi << " with respect to reference " << reference << "." << endl;
2979
2980 return phi;
2981}
2982
Note: See TracBrowser for help on using the repository browser.