source: src/tesselation.cpp@ 8725ed

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 8725ed was 1e168b, checked in by Frederik Heber <heber@…>, 16 years ago

Now the best (in terms of ShortestAngle) baseline is picked among all open ones instead of the next.

  • Element initialization was done for all constructors in tesselation.cpp
  • Property mode set to 100644
File size: 166.5 KB
RevLine 
[357fba]1/*
2 * tesselation.cpp
3 *
4 * Created on: Aug 3, 2009
5 * Author: heber
6 */
7
[f66195]8#include <fstream>
9
[a2028e]10#include "helpers.hpp"
[57066a]11#include "linkedcell.hpp"
[e138de]12#include "log.hpp"
[357fba]13#include "tesselation.hpp"
[57066a]14#include "tesselationhelpers.hpp"
15#include "vector.hpp"
[f66195]16#include "verbose.hpp"
[57066a]17
18class molecule;
[357fba]19
20// ======================================== Points on Boundary =================================
21
[16d866]22/** Constructor of BoundaryPointSet.
23 */
[1e168b]24BoundaryPointSet::BoundaryPointSet() :
25 LinesCount(0),
26 value(0.),
27 Nr(-1)
[357fba]28{
[16d866]29};
[357fba]30
[16d866]31/** Constructor of BoundaryPointSet with Tesselpoint.
32 * \param *Walker TesselPoint this boundary point represents
33 */
[776b64]34BoundaryPointSet::BoundaryPointSet(TesselPoint * Walker)
[357fba]35{
36 node = Walker;
37 LinesCount = 0;
38 Nr = Walker->nr;
[1d9b7aa]39 value = 0.;
[16d866]40};
[357fba]41
[16d866]42/** Destructor of BoundaryPointSet.
43 * Sets node to NULL to avoid removing the original, represented TesselPoint.
44 * \note When removing point from a class Tesselation, use RemoveTesselationPoint()
45 */
[357fba]46BoundaryPointSet::~BoundaryPointSet()
47{
[e138de]48 //Log() << Verbose(5) << "Erasing point nr. " << Nr << "." << endl;
[357fba]49 if (!lines.empty())
[717e0c]50 eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some lines." << endl;
[357fba]51 node = NULL;
[16d866]52};
[357fba]53
[16d866]54/** Add a line to the LineMap of this point.
55 * \param *line line to add
56 */
[357fba]57void BoundaryPointSet::AddLine(class BoundaryLineSet *line)
58{
[e138de]59 Log() << Verbose(6) << "Adding " << *this << " to line " << *line << "."
[357fba]60 << endl;
61 if (line->endpoints[0] == this)
62 {
63 lines.insert(LinePair(line->endpoints[1]->Nr, line));
64 }
65 else
66 {
67 lines.insert(LinePair(line->endpoints[0]->Nr, line));
68 }
69 LinesCount++;
[16d866]70};
[357fba]71
[16d866]72/** output operator for BoundaryPointSet.
73 * \param &ost output stream
74 * \param &a boundary point
75 */
[776b64]76ostream & operator <<(ostream &ost, const BoundaryPointSet &a)
[357fba]77{
[57066a]78 ost << "[" << a.Nr << "|" << a.node->Name << " at " << *a.node->node << "]";
[357fba]79 return ost;
80}
81;
82
83// ======================================== Lines on Boundary =================================
84
[16d866]85/** Constructor of BoundaryLineSet.
86 */
[1e168b]87BoundaryLineSet::BoundaryLineSet() :
88 Nr(-1)
[357fba]89{
90 for (int i = 0; i < 2; i++)
91 endpoints[i] = NULL;
[16d866]92};
[357fba]93
[16d866]94/** Constructor of BoundaryLineSet with two endpoints.
95 * Adds line automatically to each endpoints' LineMap
96 * \param *Point[2] array of two boundary points
97 * \param number number of the list
98 */
[776b64]99BoundaryLineSet::BoundaryLineSet(class BoundaryPointSet *Point[2], const int number)
[357fba]100{
101 // set number
102 Nr = number;
103 // set endpoints in ascending order
104 SetEndpointsOrdered(endpoints, Point[0], Point[1]);
105 // add this line to the hash maps of both endpoints
106 Point[0]->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
107 Point[1]->AddLine(this); //
[1e168b]108 // set skipped to false
109 skipped = false;
[357fba]110 // clear triangles list
[e138de]111 Log() << Verbose(5) << "New Line with endpoints " << *this << "." << endl;
[16d866]112};
[357fba]113
[16d866]114/** Destructor for BoundaryLineSet.
115 * Removes itself from each endpoints' LineMap, calling RemoveTrianglePoint() when point not connected anymore.
116 * \note When removing lines from a class Tesselation, use RemoveTesselationLine()
117 */
[357fba]118BoundaryLineSet::~BoundaryLineSet()
119{
120 int Numbers[2];
[16d866]121
122 // get other endpoint number of finding copies of same line
123 if (endpoints[1] != NULL)
124 Numbers[0] = endpoints[1]->Nr;
125 else
126 Numbers[0] = -1;
127 if (endpoints[0] != NULL)
128 Numbers[1] = endpoints[0]->Nr;
129 else
130 Numbers[1] = -1;
131
[357fba]132 for (int i = 0; i < 2; i++) {
[16d866]133 if (endpoints[i] != NULL) {
134 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
135 pair<LineMap::iterator, LineMap::iterator> erasor = endpoints[i]->lines.equal_range(Numbers[i]);
136 for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
137 if ((*Runner).second == this) {
[e138de]138 //Log() << Verbose(5) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
[16d866]139 endpoints[i]->lines.erase(Runner);
140 break;
141 }
142 } else { // there's just a single line left
[57066a]143 if (endpoints[i]->lines.erase(Nr)) {
[e138de]144 //Log() << Verbose(5) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
[57066a]145 }
[357fba]146 }
[16d866]147 if (endpoints[i]->lines.empty()) {
[e138de]148 //Log() << Verbose(5) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl;
[16d866]149 if (endpoints[i] != NULL) {
150 delete(endpoints[i]);
151 endpoints[i] = NULL;
152 }
153 }
154 }
[357fba]155 }
156 if (!triangles.empty())
[717e0c]157 eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl;
[16d866]158};
[357fba]159
[16d866]160/** Add triangle to TriangleMap of this boundary line.
161 * \param *triangle to add
162 */
163void BoundaryLineSet::AddTriangle(class BoundaryTriangleSet *triangle)
[357fba]164{
[e138de]165 Log() << Verbose(6) << "Add " << triangle->Nr << " to line " << *this << "." << endl;
[357fba]166 triangles.insert(TrianglePair(triangle->Nr, triangle));
[16d866]167};
[357fba]168
169/** Checks whether we have a common endpoint with given \a *line.
170 * \param *line other line to test
171 * \return true - common endpoint present, false - not connected
172 */
173bool BoundaryLineSet::IsConnectedTo(class BoundaryLineSet *line)
174{
175 if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1]))
176 return true;
177 else
178 return false;
179};
180
181/** Checks whether the adjacent triangles of a baseline are convex or not.
[57066a]182 * We sum the two angles of each height vector with respect to the center of the baseline.
[357fba]183 * If greater/equal M_PI than we are convex.
184 * \param *out output stream for debugging
185 * \return true - triangles are convex, false - concave or less than two triangles connected
186 */
[e138de]187bool BoundaryLineSet::CheckConvexityCriterion()
[357fba]188{
[5c7bf8]189 Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck;
[357fba]190 // get the two triangles
[5c7bf8]191 if (triangles.size() != 2) {
[717e0c]192 eLog() << Verbose(1) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl;
[1d9b7aa]193 return true;
[357fba]194 }
[5c7bf8]195 // check normal vectors
[357fba]196 // have a normal vector on the base line pointing outwards
[e138de]197 //Log() << Verbose(3) << "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << "." << endl;
[62bb91]198 BaseLineCenter.CopyVector(endpoints[0]->node->node);
199 BaseLineCenter.AddVector(endpoints[1]->node->node);
200 BaseLineCenter.Scale(1./2.);
201 BaseLine.CopyVector(endpoints[0]->node->node);
202 BaseLine.SubtractVector(endpoints[1]->node->node);
[e138de]203 //Log() << Verbose(3) << "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << "." << endl;
[357fba]204
[62bb91]205 BaseLineNormal.Zero();
[5c7bf8]206 NormalCheck.Zero();
207 double sign = -1.;
[62bb91]208 int i=0;
209 class BoundaryPointSet *node = NULL;
210 for(TriangleMap::iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
[e138de]211 //Log() << Verbose(3) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl;
[5c7bf8]212 NormalCheck.AddVector(&runner->second->NormalVector);
213 NormalCheck.Scale(sign);
214 sign = -sign;
[57066a]215 if (runner->second->NormalVector.NormSquared() > MYEPSILON)
216 BaseLineNormal.CopyVector(&runner->second->NormalVector); // yes, copy second on top of first
217 else {
[e138de]218 Log() << Verbose(1) << "CRITICAL: Triangle " << *runner->second << " has zero normal vector!" << endl;
[57066a]219 exit(255);
220 }
[62bb91]221 node = runner->second->GetThirdEndpoint(this);
222 if (node != NULL) {
[e138de]223 //Log() << Verbose(3) << "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << "." << endl;
[62bb91]224 helper[i].CopyVector(node->node->node);
225 helper[i].SubtractVector(&BaseLineCenter);
226 helper[i].MakeNormalVector(&BaseLine); // we want to compare the triangle's heights' angles!
[e138de]227 //Log() << Verbose(4) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl;
[62bb91]228 i++;
229 } else {
[717e0c]230 //eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl;
[62bb91]231 return true;
232 }
233 }
[e138de]234 //Log() << Verbose(3) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl;
[5c7bf8]235 if (NormalCheck.NormSquared() < MYEPSILON) {
[e138de]236 Log() << Verbose(3) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl;
[5c7bf8]237 return true;
[62bb91]238 }
[57066a]239 BaseLineNormal.Scale(-1.);
[f1cccd]240 double angle = GetAngle(helper[0], helper[1], BaseLineNormal);
[1d9b7aa]241 if ((angle - M_PI) > -MYEPSILON) {
[e138de]242 Log() << Verbose(3) << "ACCEPT: Angle is greater than pi: convex." << endl;
[357fba]243 return true;
[1d9b7aa]244 } else {
[e138de]245 Log() << Verbose(3) << "REJECT: Angle is less than pi: concave." << endl;
[357fba]246 return false;
[1d9b7aa]247 }
[357fba]248}
249
250/** Checks whether point is any of the two endpoints this line contains.
251 * \param *point point to test
252 * \return true - point is of the line, false - is not
253 */
254bool BoundaryLineSet::ContainsBoundaryPoint(class BoundaryPointSet *point)
255{
256 for(int i=0;i<2;i++)
257 if (point == endpoints[i])
258 return true;
259 return false;
260};
261
[62bb91]262/** Returns other endpoint of the line.
263 * \param *point other endpoint
264 * \return NULL - if endpoint not contained in BoundaryLineSet, or pointer to BoundaryPointSet otherwise
265 */
[08ef35]266class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(class BoundaryPointSet *point)
[62bb91]267{
268 if (endpoints[0] == point)
269 return endpoints[1];
270 else if (endpoints[1] == point)
271 return endpoints[0];
272 else
273 return NULL;
274};
275
[16d866]276/** output operator for BoundaryLineSet.
277 * \param &ost output stream
278 * \param &a boundary line
279 */
[776b64]280ostream & operator <<(ostream &ost, const BoundaryLineSet &a)
[357fba]281{
[57066a]282 ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << "," << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "]";
[357fba]283 return ost;
[16d866]284};
[357fba]285
286// ======================================== Triangles on Boundary =================================
287
[16d866]288/** Constructor for BoundaryTriangleSet.
289 */
[1e168b]290BoundaryTriangleSet::BoundaryTriangleSet() :
291 Nr(-1)
[357fba]292{
293 for (int i = 0; i < 3; i++)
294 {
295 endpoints[i] = NULL;
296 lines[i] = NULL;
297 }
[16d866]298};
[357fba]299
[16d866]300/** Constructor for BoundaryTriangleSet with three lines.
301 * \param *line[3] lines that make up the triangle
302 * \param number number of triangle
303 */
[1e168b]304BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet *line[3], int number) :
305 Nr(number)
[357fba]306{
307 // set number
308 // set lines
[1e168b]309 Log() << Verbose(5) << "New triangle " << Nr << ":";
[357fba]310 for (int i = 0; i < 3; i++)
311 {
312 lines[i] = line[i];
313 lines[i]->AddTriangle(this);
314 }
315 // get ascending order of endpoints
316 map<int, class BoundaryPointSet *> OrderMap;
317 for (int i = 0; i < 3; i++)
318 // for all three lines
319 for (int j = 0; j < 2; j++)
320 { // for both endpoints
321 OrderMap.insert(pair<int, class BoundaryPointSet *> (
322 line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
323 // and we don't care whether insertion fails
324 }
325 // set endpoints
326 int Counter = 0;
[e138de]327 Log() << Verbose(6) << " with end points ";
[357fba]328 for (map<int, class BoundaryPointSet *>::iterator runner = OrderMap.begin(); runner
329 != OrderMap.end(); runner++)
330 {
331 endpoints[Counter] = runner->second;
[e138de]332 Log() << Verbose(0) << " " << *endpoints[Counter];
[357fba]333 Counter++;
334 }
335 if (Counter < 3)
336 {
[e359a8]337 eLog() << Verbose(0) << "ERROR! We have a triangle with only two distinct endpoints!" << endl;
338 performCriticalExit();
[357fba]339 }
[e138de]340 Log() << Verbose(0) << "." << endl;
[16d866]341};
[357fba]342
[16d866]343/** Destructor of BoundaryTriangleSet.
344 * Removes itself from each of its lines' LineMap and removes them if necessary.
345 * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle()
346 */
[357fba]347BoundaryTriangleSet::~BoundaryTriangleSet()
348{
349 for (int i = 0; i < 3; i++) {
[16d866]350 if (lines[i] != NULL) {
[57066a]351 if (lines[i]->triangles.erase(Nr)) {
[e138de]352 //Log() << Verbose(5) << "Triangle Nr." << Nr << " erased in line " << *lines[i] << "." << endl;
[57066a]353 }
[16d866]354 if (lines[i]->triangles.empty()) {
[e138de]355 //Log() << Verbose(5) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
[16d866]356 delete (lines[i]);
357 lines[i] = NULL;
358 }
359 }
[357fba]360 }
[e138de]361 //Log() << Verbose(5) << "Erasing triangle Nr." << Nr << " itself." << endl;
[16d866]362};
[357fba]363
364/** Calculates the normal vector for this triangle.
365 * Is made unique by comparison with \a OtherVector to point in the other direction.
366 * \param &OtherVector direction vector to make normal vector unique.
367 */
368void BoundaryTriangleSet::GetNormalVector(Vector &OtherVector)
369{
370 // get normal vector
371 NormalVector.MakeNormalVector(endpoints[0]->node->node, endpoints[1]->node->node, endpoints[2]->node->node);
372
373 // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
[658efb]374 if (NormalVector.ScalarProduct(&OtherVector) > 0.)
[357fba]375 NormalVector.Scale(-1.);
376};
377
378/** Finds the point on the triangle \a *BTS the line defined by \a *MolCenter and \a *x crosses through.
379 * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane
380 * This we test if it's really on the plane and whether it's inside the triangle on the plane or not.
[7dea7c]381 * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
382 * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between
383 * the first two basepoints) or not.
[357fba]384 * \param *out output stream for debugging
385 * \param *MolCenter offset vector of line
386 * \param *x second endpoint of line, minus \a *MolCenter is directional vector of line
387 * \param *Intersection intersection on plane on return
388 * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle.
389 */
[e138de]390bool BoundaryTriangleSet::GetIntersectionInsideTriangle(Vector *MolCenter, Vector *x, Vector *Intersection)
[357fba]391{
392 Vector CrossPoint;
393 Vector helper;
394
[e138de]395 if (!Intersection->GetIntersectionWithPlane(&NormalVector, endpoints[0]->node->node, MolCenter, x)) {
396 Log() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl;
[357fba]397 return false;
398 }
399
400 // Calculate cross point between one baseline and the line from the third endpoint to intersection
[5c7bf8]401 int i=0;
[357fba]402 do {
[e138de]403 if (CrossPoint.GetIntersectionOfTwoLinesOnPlane(endpoints[i%3]->node->node, endpoints[(i+1)%3]->node->node, endpoints[(i+2)%3]->node->node, Intersection, &NormalVector)) {
[5c7bf8]404 helper.CopyVector(endpoints[(i+1)%3]->node->node);
405 helper.SubtractVector(endpoints[i%3]->node->node);
406 } else
407 i++;
408 if (i>2)
[357fba]409 break;
410 } while (CrossPoint.NormSquared() < MYEPSILON);
[5c7bf8]411 if (i==3) {
[717e0c]412 eLog() << Verbose(1) << "Could not find any cross points, something's utterly wrong here!" << endl;
[357fba]413 exit(255);
414 }
[7dea7c]415 CrossPoint.SubtractVector(endpoints[i%3]->node->node); // cross point was returned as absolute vector
[357fba]416
417 // check whether intersection is inside or not by comparing length of intersection and length of cross point
[7dea7c]418 if ((CrossPoint.NormSquared() - helper.NormSquared()) < MYEPSILON) { // inside
[357fba]419 return true;
420 } else { // outside!
421 Intersection->Zero();
422 return false;
423 }
424};
425
426/** Checks whether lines is any of the three boundary lines this triangle contains.
427 * \param *line line to test
428 * \return true - line is of the triangle, false - is not
429 */
430bool BoundaryTriangleSet::ContainsBoundaryLine(class BoundaryLineSet *line)
431{
432 for(int i=0;i<3;i++)
433 if (line == lines[i])
434 return true;
435 return false;
436};
437
438/** Checks whether point is any of the three endpoints this triangle contains.
439 * \param *point point to test
440 * \return true - point is of the triangle, false - is not
441 */
442bool BoundaryTriangleSet::ContainsBoundaryPoint(class BoundaryPointSet *point)
443{
444 for(int i=0;i<3;i++)
445 if (point == endpoints[i])
446 return true;
447 return false;
448};
449
[7dea7c]450/** Checks whether point is any of the three endpoints this triangle contains.
451 * \param *point TesselPoint to test
452 * \return true - point is of the triangle, false - is not
453 */
454bool BoundaryTriangleSet::ContainsBoundaryPoint(class TesselPoint *point)
455{
456 for(int i=0;i<3;i++)
457 if (point == endpoints[i]->node)
458 return true;
459 return false;
460};
461
[357fba]462/** Checks whether three given \a *Points coincide with triangle's endpoints.
463 * \param *Points[3] pointer to BoundaryPointSet
464 * \return true - is the very triangle, false - is not
465 */
466bool BoundaryTriangleSet::IsPresentTupel(class BoundaryPointSet *Points[3])
467{
468 return (((endpoints[0] == Points[0])
469 || (endpoints[0] == Points[1])
470 || (endpoints[0] == Points[2])
471 ) && (
472 (endpoints[1] == Points[0])
473 || (endpoints[1] == Points[1])
474 || (endpoints[1] == Points[2])
475 ) && (
476 (endpoints[2] == Points[0])
477 || (endpoints[2] == Points[1])
478 || (endpoints[2] == Points[2])
[62bb91]479
[357fba]480 ));
481};
482
[57066a]483/** Checks whether three given \a *Points coincide with triangle's endpoints.
484 * \param *Points[3] pointer to BoundaryPointSet
485 * \return true - is the very triangle, false - is not
486 */
487bool BoundaryTriangleSet::IsPresentTupel(class BoundaryTriangleSet *T)
488{
489 return (((endpoints[0] == T->endpoints[0])
490 || (endpoints[0] == T->endpoints[1])
491 || (endpoints[0] == T->endpoints[2])
492 ) && (
493 (endpoints[1] == T->endpoints[0])
494 || (endpoints[1] == T->endpoints[1])
495 || (endpoints[1] == T->endpoints[2])
496 ) && (
497 (endpoints[2] == T->endpoints[0])
498 || (endpoints[2] == T->endpoints[1])
499 || (endpoints[2] == T->endpoints[2])
500
501 ));
502};
503
[62bb91]504/** Returns the endpoint which is not contained in the given \a *line.
505 * \param *line baseline defining two endpoints
506 * \return pointer third endpoint or NULL if line does not belong to triangle.
507 */
508class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(class BoundaryLineSet *line)
509{
510 // sanity check
511 if (!ContainsBoundaryLine(line))
512 return NULL;
513 for(int i=0;i<3;i++)
514 if (!line->ContainsBoundaryPoint(endpoints[i]))
515 return endpoints[i];
516 // actually, that' impossible :)
517 return NULL;
518};
519
520/** Calculates the center point of the triangle.
521 * Is third of the sum of all endpoints.
522 * \param *center central point on return.
523 */
524void BoundaryTriangleSet::GetCenter(Vector *center)
525{
526 center->Zero();
527 for(int i=0;i<3;i++)
528 center->AddVector(endpoints[i]->node->node);
529 center->Scale(1./3.);
530}
531
[16d866]532/** output operator for BoundaryTriangleSet.
533 * \param &ost output stream
534 * \param &a boundary triangle
535 */
[776b64]536ostream &operator <<(ostream &ost, const BoundaryTriangleSet &a)
[357fba]537{
[57066a]538 ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
539 << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
[357fba]540 return ost;
[16d866]541};
[357fba]542
543// =========================================================== class TESSELPOINT ===========================================
544
545/** Constructor of class TesselPoint.
546 */
547TesselPoint::TesselPoint()
548{
549 node = NULL;
550 nr = -1;
551 Name = NULL;
552};
553
554/** Destructor for class TesselPoint.
555 */
556TesselPoint::~TesselPoint()
557{
558};
559
560/** Prints LCNode to screen.
561 */
562ostream & operator << (ostream &ost, const TesselPoint &a)
563{
[57066a]564 ost << "[" << (a.Name) << "|" << a.Name << " at " << *a.node << "]";
[357fba]565 return ost;
566};
567
[5c7bf8]568/** Prints LCNode to screen.
569 */
570ostream & TesselPoint::operator << (ostream &ost)
571{
572 ost << "[" << (Name) << "|" << this << "]";
573 return ost;
574};
575
[357fba]576
577// =========================================================== class POINTCLOUD ============================================
578
579/** Constructor of class PointCloud.
580 */
581PointCloud::PointCloud()
582{
583};
584
585/** Destructor for class PointCloud.
586 */
587PointCloud::~PointCloud()
588{
589};
590
591// ============================ CandidateForTesselation =============================
592
593/** Constructor of class CandidateForTesselation.
594 */
[1e168b]595CandidateForTesselation::CandidateForTesselation (BoundaryLineSet* line) :
596 point(NULL),
597 BaseLine(line),
598 ShortestAngle(2.*M_PI),
599 OtherShortestAngle(2.*M_PI)
600{
601};
602
603
604/** Constructor of class CandidateForTesselation.
605 */
606CandidateForTesselation::CandidateForTesselation (TesselPoint *candidate, BoundaryLineSet* line, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) :
607 point(candidate),
608 BaseLine(line),
609 ShortestAngle(2.*M_PI),
610 OtherShortestAngle(2.*M_PI)
611{
[357fba]612 OptCenter.CopyVector(&OptCandidateCenter);
613 OtherOptCenter.CopyVector(&OtherOptCandidateCenter);
614};
615
616/** Destructor for class CandidateForTesselation.
617 */
618CandidateForTesselation::~CandidateForTesselation() {
619 point = NULL;
620 BaseLine = NULL;
621};
622
[1e168b]623/** output operator for CandidateForTesselation.
624 * \param &ost output stream
625 * \param &a boundary line
626 */
627ostream & operator <<(ostream &ost, const CandidateForTesselation &a)
628{
629 ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->Name << "," << a.BaseLine->endpoints[1]->node->Name << "] with ";
630 if (a.point == NULL)
631 ost << "no candidate.";
632 else
633 ost << "candidate " << *(a.point) << " at angle " << (a.ShortestAngle)<< ".";
634
635 return ost;
636};
637
638
[357fba]639// =========================================================== class TESSELATION ===========================================
640
641/** Constructor of class Tesselation.
642 */
[1e168b]643Tesselation::Tesselation() :
644 PointsOnBoundaryCount(0),
645 LinesOnBoundaryCount(0),
646 TrianglesOnBoundaryCount(0),
647 LastTriangle(NULL),
648 TriangleFilesWritten(0),
649 InternalPointer(PointsOnBoundary.begin())
[357fba]650{
651}
652;
653
654/** Destructor of class Tesselation.
655 * We have to free all points, lines and triangles.
656 */
657Tesselation::~Tesselation()
658{
[e138de]659 Log() << Verbose(1) << "Free'ing TesselStruct ... " << endl;
[357fba]660 for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner != TrianglesOnBoundary.end(); runner++) {
661 if (runner->second != NULL) {
662 delete (runner->second);
663 runner->second = NULL;
664 } else
[717e0c]665 eLog() << Verbose(1) << "The triangle " << runner->first << " has already been free'd." << endl;
[357fba]666 }
[e138de]667 Log() << Verbose(1) << "This envelope was written to file " << TriangleFilesWritten << " times(s)." << endl;
[357fba]668}
669;
670
[5c7bf8]671/** PointCloud implementation of GetCenter
672 * Uses PointsOnBoundary and STL stuff.
673 */
[776b64]674Vector * Tesselation::GetCenter(ofstream *out) const
[5c7bf8]675{
676 Vector *Center = new Vector(0.,0.,0.);
677 int num=0;
678 for (GoToFirst(); (!IsEnd()); GoToNext()) {
679 Center->AddVector(GetPoint()->node);
680 num++;
681 }
682 Center->Scale(1./num);
683 return Center;
684};
685
686/** PointCloud implementation of GoPoint
687 * Uses PointsOnBoundary and STL stuff.
688 */
[776b64]689TesselPoint * Tesselation::GetPoint() const
[5c7bf8]690{
691 return (InternalPointer->second->node);
692};
693
694/** PointCloud implementation of GetTerminalPoint.
695 * Uses PointsOnBoundary and STL stuff.
696 */
[776b64]697TesselPoint * Tesselation::GetTerminalPoint() const
[5c7bf8]698{
[776b64]699 PointMap::const_iterator Runner = PointsOnBoundary.end();
[5c7bf8]700 Runner--;
701 return (Runner->second->node);
702};
703
704/** PointCloud implementation of GoToNext.
705 * Uses PointsOnBoundary and STL stuff.
706 */
[776b64]707void Tesselation::GoToNext() const
[5c7bf8]708{
709 if (InternalPointer != PointsOnBoundary.end())
710 InternalPointer++;
711};
712
713/** PointCloud implementation of GoToPrevious.
714 * Uses PointsOnBoundary and STL stuff.
715 */
[776b64]716void Tesselation::GoToPrevious() const
[5c7bf8]717{
718 if (InternalPointer != PointsOnBoundary.begin())
719 InternalPointer--;
720};
721
722/** PointCloud implementation of GoToFirst.
723 * Uses PointsOnBoundary and STL stuff.
724 */
[776b64]725void Tesselation::GoToFirst() const
[5c7bf8]726{
727 InternalPointer = PointsOnBoundary.begin();
728};
729
730/** PointCloud implementation of GoToLast.
731 * Uses PointsOnBoundary and STL stuff.
[776b64]732 */
733void Tesselation::GoToLast() const
[5c7bf8]734{
735 InternalPointer = PointsOnBoundary.end();
736 InternalPointer--;
737};
738
739/** PointCloud implementation of IsEmpty.
740 * Uses PointsOnBoundary and STL stuff.
741 */
[776b64]742bool Tesselation::IsEmpty() const
[5c7bf8]743{
744 return (PointsOnBoundary.empty());
745};
746
747/** PointCloud implementation of IsLast.
748 * Uses PointsOnBoundary and STL stuff.
749 */
[776b64]750bool Tesselation::IsEnd() const
[5c7bf8]751{
752 return (InternalPointer == PointsOnBoundary.end());
753};
754
755
[357fba]756/** Gueses first starting triangle of the convex envelope.
757 * We guess the starting triangle by taking the smallest distance between two points and looking for a fitting third.
758 * \param *out output stream for debugging
759 * \param PointsOnBoundary set of boundary points defining the convex envelope of the cluster
760 */
761void
[e138de]762Tesselation::GuessStartingTriangle()
[357fba]763{
764 // 4b. create a starting triangle
765 // 4b1. create all distances
766 DistanceMultiMap DistanceMMap;
767 double distance, tmp;
768 Vector PlaneVector, TrialVector;
769 PointMap::iterator A, B, C; // three nodes of the first triangle
770 A = PointsOnBoundary.begin(); // the first may be chosen arbitrarily
771
772 // with A chosen, take each pair B,C and sort
773 if (A != PointsOnBoundary.end())
774 {
775 B = A;
776 B++;
777 for (; B != PointsOnBoundary.end(); B++)
778 {
779 C = B;
780 C++;
781 for (; C != PointsOnBoundary.end(); C++)
782 {
783 tmp = A->second->node->node->DistanceSquared(B->second->node->node);
784 distance = tmp * tmp;
785 tmp = A->second->node->node->DistanceSquared(C->second->node->node);
786 distance += tmp * tmp;
787 tmp = B->second->node->node->DistanceSquared(C->second->node->node);
788 distance += tmp * tmp;
789 DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C)));
790 }
791 }
792 }
793 // // listing distances
[e138de]794 // Log() << Verbose(1) << "Listing DistanceMMap:";
[357fba]795 // for(DistanceMultiMap::iterator runner = DistanceMMap.begin(); runner != DistanceMMap.end(); runner++) {
[e138de]796 // Log() << Verbose(0) << " " << runner->first << "(" << *runner->second.first->second << ", " << *runner->second.second->second << ")";
[357fba]797 // }
[e138de]798 // Log() << Verbose(0) << endl;
[357fba]799 // 4b2. pick three baselines forming a triangle
800 // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
801 DistanceMultiMap::iterator baseline = DistanceMMap.begin();
802 for (; baseline != DistanceMMap.end(); baseline++)
803 {
804 // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
805 // 2. next, we have to check whether all points reside on only one side of the triangle
806 // 3. construct plane vector
807 PlaneVector.MakeNormalVector(A->second->node->node,
808 baseline->second.first->second->node->node,
809 baseline->second.second->second->node->node);
[e138de]810 Log() << Verbose(2) << "Plane vector of candidate triangle is ";
811 PlaneVector.Output();
812 Log() << Verbose(0) << endl;
[357fba]813 // 4. loop over all points
814 double sign = 0.;
815 PointMap::iterator checker = PointsOnBoundary.begin();
816 for (; checker != PointsOnBoundary.end(); checker++)
817 {
818 // (neglecting A,B,C)
819 if ((checker == A) || (checker == baseline->second.first) || (checker
820 == baseline->second.second))
821 continue;
822 // 4a. project onto plane vector
823 TrialVector.CopyVector(checker->second->node->node);
824 TrialVector.SubtractVector(A->second->node->node);
[658efb]825 distance = TrialVector.ScalarProduct(&PlaneVector);
[357fba]826 if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
827 continue;
[e138de]828 Log() << Verbose(3) << "Projection of " << checker->second->node->Name
[357fba]829 << " yields distance of " << distance << "." << endl;
830 tmp = distance / fabs(distance);
831 // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
832 if ((sign != 0) && (tmp != sign))
833 {
834 // 4c. If so, break 4. loop and continue with next candidate in 1. loop
[e138de]835 Log() << Verbose(2) << "Current candidates: "
[357fba]836 << A->second->node->Name << ","
837 << baseline->second.first->second->node->Name << ","
838 << baseline->second.second->second->node->Name << " leaves "
839 << checker->second->node->Name << " outside the convex hull."
840 << endl;
841 break;
842 }
843 else
844 { // note the sign for later
[e138de]845 Log() << Verbose(2) << "Current candidates: "
[357fba]846 << A->second->node->Name << ","
847 << baseline->second.first->second->node->Name << ","
848 << baseline->second.second->second->node->Name << " leave "
849 << checker->second->node->Name << " inside the convex hull."
850 << endl;
851 sign = tmp;
852 }
853 // 4d. Check whether the point is inside the triangle (check distance to each node
854 tmp = checker->second->node->node->DistanceSquared(A->second->node->node);
855 int innerpoint = 0;
856 if ((tmp < A->second->node->node->DistanceSquared(
857 baseline->second.first->second->node->node)) && (tmp
858 < A->second->node->node->DistanceSquared(
859 baseline->second.second->second->node->node)))
860 innerpoint++;
861 tmp = checker->second->node->node->DistanceSquared(
862 baseline->second.first->second->node->node);
863 if ((tmp < baseline->second.first->second->node->node->DistanceSquared(
864 A->second->node->node)) && (tmp
865 < baseline->second.first->second->node->node->DistanceSquared(
866 baseline->second.second->second->node->node)))
867 innerpoint++;
868 tmp = checker->second->node->node->DistanceSquared(
869 baseline->second.second->second->node->node);
870 if ((tmp < baseline->second.second->second->node->node->DistanceSquared(
871 baseline->second.first->second->node->node)) && (tmp
872 < baseline->second.second->second->node->node->DistanceSquared(
873 A->second->node->node)))
874 innerpoint++;
875 // 4e. If so, break 4. loop and continue with next candidate in 1. loop
876 if (innerpoint == 3)
877 break;
878 }
879 // 5. come this far, all on same side? Then break 1. loop and construct triangle
880 if (checker == PointsOnBoundary.end())
881 {
[e138de]882 Log() << Verbose(0) << "Looks like we have a candidate!" << endl;
[357fba]883 break;
884 }
885 }
886 if (baseline != DistanceMMap.end())
887 {
888 BPS[0] = baseline->second.first->second;
889 BPS[1] = baseline->second.second->second;
890 BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
891 BPS[0] = A->second;
892 BPS[1] = baseline->second.second->second;
893 BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
894 BPS[0] = baseline->second.first->second;
895 BPS[1] = A->second;
896 BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
897
898 // 4b3. insert created triangle
899 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
900 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
901 TrianglesOnBoundaryCount++;
902 for (int i = 0; i < NDIM; i++)
903 {
904 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
905 LinesOnBoundaryCount++;
906 }
907
[e138de]908 Log() << Verbose(1) << "Starting triangle is " << *BTS << "." << endl;
[357fba]909 }
910 else
911 {
[e138de]912 Log() << Verbose(1) << "No starting triangle found." << endl;
[357fba]913 exit(255);
914 }
915}
916;
917
918/** Tesselates the convex envelope of a cluster from a single starting triangle.
919 * The starting triangle is made out of three baselines. Each line in the final tesselated cluster may belong to at most
920 * 2 triangles. Hence, we go through all current lines:
921 * -# if the lines contains to only one triangle
922 * -# We search all points in the boundary
923 * -# if the triangle is in forward direction of the baseline (at most 90 degrees angle between vector orthogonal to
924 * baseline in triangle plane pointing out of the triangle and normal vector of new triangle)
925 * -# if the triangle with the baseline and the current point has the smallest of angles (comparison between normal vectors)
926 * -# then we have a new triangle, whose baselines we again add (or increase their TriangleCount)
927 * \param *out output stream for debugging
928 * \param *configuration for IsAngstroem
929 * \param *cloud cluster of points
930 */
[e138de]931void Tesselation::TesselateOnBoundary(const PointCloud * const cloud)
[357fba]932{
933 bool flag;
934 PointMap::iterator winner;
935 class BoundaryPointSet *peak = NULL;
936 double SmallestAngle, TempAngle;
937 Vector NormalVector, VirtualNormalVector, CenterVector, TempVector, helper, PropagationVector, *Center = NULL;
938 LineMap::iterator LineChecker[2];
939
[e138de]940 Center = cloud->GetCenter();
[357fba]941 // create a first tesselation with the given BoundaryPoints
942 do {
943 flag = false;
944 for (LineMap::iterator baseline = LinesOnBoundary.begin(); baseline != LinesOnBoundary.end(); baseline++)
[5c7bf8]945 if (baseline->second->triangles.size() == 1) {
[357fba]946 // 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)
947 SmallestAngle = M_PI;
948
949 // get peak point with respect to this base line's only triangle
950 BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far
[e138de]951 Log() << Verbose(2) << "Current baseline is between " << *(baseline->second) << "." << endl;
[357fba]952 for (int i = 0; i < 3; i++)
953 if ((BTS->endpoints[i] != baseline->second->endpoints[0]) && (BTS->endpoints[i] != baseline->second->endpoints[1]))
954 peak = BTS->endpoints[i];
[e138de]955 Log() << Verbose(3) << " and has peak " << *peak << "." << endl;
[357fba]956
957 // prepare some auxiliary vectors
958 Vector BaseLineCenter, BaseLine;
959 BaseLineCenter.CopyVector(baseline->second->endpoints[0]->node->node);
960 BaseLineCenter.AddVector(baseline->second->endpoints[1]->node->node);
961 BaseLineCenter.Scale(1. / 2.); // points now to center of base line
962 BaseLine.CopyVector(baseline->second->endpoints[0]->node->node);
963 BaseLine.SubtractVector(baseline->second->endpoints[1]->node->node);
964
965 // offset to center of triangle
966 CenterVector.Zero();
967 for (int i = 0; i < 3; i++)
968 CenterVector.AddVector(BTS->endpoints[i]->node->node);
969 CenterVector.Scale(1. / 3.);
[e138de]970 Log() << Verbose(4) << "CenterVector of base triangle is " << CenterVector << endl;
[357fba]971
972 // normal vector of triangle
973 NormalVector.CopyVector(Center);
974 NormalVector.SubtractVector(&CenterVector);
975 BTS->GetNormalVector(NormalVector);
976 NormalVector.CopyVector(&BTS->NormalVector);
[e138de]977 Log() << Verbose(4) << "NormalVector of base triangle is " << NormalVector << endl;
[357fba]978
979 // vector in propagation direction (out of triangle)
980 // project center vector onto triangle plane (points from intersection plane-NormalVector to plane-CenterVector intersection)
981 PropagationVector.MakeNormalVector(&BaseLine, &NormalVector);
982 TempVector.CopyVector(&CenterVector);
983 TempVector.SubtractVector(baseline->second->endpoints[0]->node->node); // TempVector is vector on triangle plane pointing from one baseline egde towards center!
[e138de]984 //Log() << Verbose(2) << "Projection of propagation onto temp: " << PropagationVector.Projection(&TempVector) << "." << endl;
[658efb]985 if (PropagationVector.ScalarProduct(&TempVector) > 0) // make sure normal propagation vector points outward from baseline
[357fba]986 PropagationVector.Scale(-1.);
[e138de]987 Log() << Verbose(4) << "PropagationVector of base triangle is " << PropagationVector << endl;
[357fba]988 winner = PointsOnBoundary.end();
989
990 // loop over all points and calculate angle between normal vector of new and present triangle
991 for (PointMap::iterator target = PointsOnBoundary.begin(); target != PointsOnBoundary.end(); target++) {
992 if ((target->second != baseline->second->endpoints[0]) && (target->second != baseline->second->endpoints[1])) { // don't take the same endpoints
[e138de]993 Log() << Verbose(3) << "Target point is " << *(target->second) << ":" << endl;
[357fba]994
995 // first check direction, so that triangles don't intersect
996 VirtualNormalVector.CopyVector(target->second->node->node);
997 VirtualNormalVector.SubtractVector(&BaseLineCenter); // points from center of base line to target
998 VirtualNormalVector.ProjectOntoPlane(&NormalVector);
999 TempAngle = VirtualNormalVector.Angle(&PropagationVector);
[e138de]1000 Log() << Verbose(4) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl;
[357fba]1001 if (TempAngle > (M_PI/2.)) { // no bends bigger than Pi/2 (90 degrees)
[e138de]1002 Log() << Verbose(4) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl;
[357fba]1003 continue;
1004 } else
[e138de]1005 Log() << Verbose(4) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl;
[357fba]1006
1007 // check first and second endpoint (if any connecting line goes to target has at least not more than 1 triangle)
1008 LineChecker[0] = baseline->second->endpoints[0]->lines.find(target->first);
1009 LineChecker[1] = baseline->second->endpoints[1]->lines.find(target->first);
[5c7bf8]1010 if (((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[0]->second->triangles.size() == 2))) {
[e138de]1011 Log() << Verbose(4) << *(baseline->second->endpoints[0]) << " has line " << *(LineChecker[0]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[0]->second->triangles.size() << " triangles." << endl;
[357fba]1012 continue;
1013 }
[5c7bf8]1014 if (((LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (LineChecker[1]->second->triangles.size() == 2))) {
[e138de]1015 Log() << Verbose(4) << *(baseline->second->endpoints[1]) << " has line " << *(LineChecker[1]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[1]->second->triangles.size() << " triangles." << endl;
[357fba]1016 continue;
1017 }
1018
1019 // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint)
1020 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)))) {
[e138de]1021 Log() << Verbose(4) << "Current target is peak!" << endl;
[357fba]1022 continue;
1023 }
1024
1025 // check for linear dependence
1026 TempVector.CopyVector(baseline->second->endpoints[0]->node->node);
1027 TempVector.SubtractVector(target->second->node->node);
1028 helper.CopyVector(baseline->second->endpoints[1]->node->node);
1029 helper.SubtractVector(target->second->node->node);
1030 helper.ProjectOntoPlane(&TempVector);
1031 if (fabs(helper.NormSquared()) < MYEPSILON) {
[e138de]1032 Log() << Verbose(4) << "Chosen set of vectors is linear dependent." << endl;
[357fba]1033 continue;
1034 }
1035
1036 // in case NOT both were found, create virtually this triangle, get its normal vector, calculate angle
1037 flag = true;
1038 VirtualNormalVector.MakeNormalVector(baseline->second->endpoints[0]->node->node, baseline->second->endpoints[1]->node->node, target->second->node->node);
1039 TempVector.CopyVector(baseline->second->endpoints[0]->node->node);
1040 TempVector.AddVector(baseline->second->endpoints[1]->node->node);
1041 TempVector.AddVector(target->second->node->node);
1042 TempVector.Scale(1./3.);
1043 TempVector.SubtractVector(Center);
1044 // make it always point outward
[658efb]1045 if (VirtualNormalVector.ScalarProduct(&TempVector) < 0)
[357fba]1046 VirtualNormalVector.Scale(-1.);
1047 // calculate angle
1048 TempAngle = NormalVector.Angle(&VirtualNormalVector);
[e138de]1049 Log() << Verbose(4) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl;
[357fba]1050 if ((SmallestAngle - TempAngle) > MYEPSILON) { // set to new possible winner
1051 SmallestAngle = TempAngle;
1052 winner = target;
[e138de]1053 Log() << Verbose(4) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
[357fba]1054 } else if (fabs(SmallestAngle - TempAngle) < MYEPSILON) { // check the angle to propagation, both possible targets are in one plane! (their normals have same angle)
1055 // hence, check the angles to some normal direction from our base line but in this common plane of both targets...
1056 helper.CopyVector(target->second->node->node);
1057 helper.SubtractVector(&BaseLineCenter);
1058 helper.ProjectOntoPlane(&BaseLine);
1059 // ...the one with the smaller angle is the better candidate
1060 TempVector.CopyVector(target->second->node->node);
1061 TempVector.SubtractVector(&BaseLineCenter);
1062 TempVector.ProjectOntoPlane(&VirtualNormalVector);
1063 TempAngle = TempVector.Angle(&helper);
1064 TempVector.CopyVector(winner->second->node->node);
1065 TempVector.SubtractVector(&BaseLineCenter);
1066 TempVector.ProjectOntoPlane(&VirtualNormalVector);
1067 if (TempAngle < TempVector.Angle(&helper)) {
1068 TempAngle = NormalVector.Angle(&VirtualNormalVector);
1069 SmallestAngle = TempAngle;
1070 winner = target;
[e138de]1071 Log() << Verbose(4) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl;
[357fba]1072 } else
[e138de]1073 Log() << Verbose(4) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl;
[357fba]1074 } else
[e138de]1075 Log() << Verbose(4) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
[357fba]1076 }
1077 } // end of loop over all boundary points
1078
1079 // 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
1080 if (winner != PointsOnBoundary.end()) {
[e138de]1081 Log() << Verbose(2) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl;
[357fba]1082 // create the lins of not yet present
1083 BLS[0] = baseline->second;
1084 // 5c. add lines to the line set if those were new (not yet part of a triangle), delete lines that belong to two triangles)
1085 LineChecker[0] = baseline->second->endpoints[0]->lines.find(winner->first);
1086 LineChecker[1] = baseline->second->endpoints[1]->lines.find(winner->first);
1087 if (LineChecker[0] == baseline->second->endpoints[0]->lines.end()) { // create
1088 BPS[0] = baseline->second->endpoints[0];
1089 BPS[1] = winner->second;
1090 BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1091 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[1]));
1092 LinesOnBoundaryCount++;
1093 } else
1094 BLS[1] = LineChecker[0]->second;
1095 if (LineChecker[1] == baseline->second->endpoints[1]->lines.end()) { // create
1096 BPS[0] = baseline->second->endpoints[1];
1097 BPS[1] = winner->second;
1098 BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
1099 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[2]));
1100 LinesOnBoundaryCount++;
1101 } else
1102 BLS[2] = LineChecker[1]->second;
1103 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
[62bb91]1104 BTS->GetCenter(&helper);
1105 helper.SubtractVector(Center);
1106 helper.Scale(-1);
1107 BTS->GetNormalVector(helper);
[357fba]1108 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1109 TrianglesOnBoundaryCount++;
1110 } else {
[e138de]1111 Log() << Verbose(1) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl;
[357fba]1112 }
1113
1114 // 5d. If the set of lines is not yet empty, go to 5. and continue
1115 } else
[e138de]1116 Log() << Verbose(2) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl;
[357fba]1117 } while (flag);
1118
1119 // exit
1120 delete(Center);
1121};
1122
[62bb91]1123/** Inserts all points outside of the tesselated surface into it by adding new triangles.
[357fba]1124 * \param *out output stream for debugging
1125 * \param *cloud cluster of points
[62bb91]1126 * \param *LC LinkedCell structure to find nearest point quickly
[357fba]1127 * \return true - all straddling points insert, false - something went wrong
1128 */
[e138de]1129bool Tesselation::InsertStraddlingPoints(const PointCloud *cloud, const LinkedCell *LC)
[357fba]1130{
[5c7bf8]1131 Vector Intersection, Normal;
[357fba]1132 TesselPoint *Walker = NULL;
[e138de]1133 Vector *Center = cloud->GetCenter();
[62bb91]1134 list<BoundaryTriangleSet*> *triangles = NULL;
[7dea7c]1135 bool AddFlag = false;
1136 LinkedCell *BoundaryPoints = NULL;
[62bb91]1137
[e138de]1138 Log() << Verbose(1) << "Begin of InsertStraddlingPoints" << endl;
[357fba]1139
1140 cloud->GoToFirst();
[7dea7c]1141 BoundaryPoints = new LinkedCell(this, 5.);
[1999d8]1142 while (!cloud->IsEnd()) { // we only have to go once through all points, as boundary can become only bigger
[7dea7c]1143 if (AddFlag) {
1144 delete(BoundaryPoints);
1145 BoundaryPoints = new LinkedCell(this, 5.);
1146 AddFlag = false;
1147 }
[357fba]1148 Walker = cloud->GetPoint();
[e138de]1149 Log() << Verbose(2) << "Current point is " << *Walker << "." << endl;
[357fba]1150 // get the next triangle
[e138de]1151 triangles = FindClosestTrianglesToPoint(Walker->node, BoundaryPoints);
[7dea7c]1152 BTS = triangles->front();
1153 if ((triangles == NULL) || (BTS->ContainsBoundaryPoint(Walker))) {
[e138de]1154 Log() << Verbose(2) << "No triangles found, probably a tesselation point itself." << endl;
[62bb91]1155 cloud->GoToNext();
1156 continue;
1157 } else {
[357fba]1158 }
[e138de]1159 Log() << Verbose(2) << "Closest triangle is " << *BTS << "." << endl;
[357fba]1160 // get the intersection point
[e138de]1161 if (BTS->GetIntersectionInsideTriangle(Center, Walker->node, &Intersection)) {
1162 Log() << Verbose(2) << "We have an intersection at " << Intersection << "." << endl;
[357fba]1163 // we have the intersection, check whether in- or outside of boundary
1164 if ((Center->DistanceSquared(Walker->node) - Center->DistanceSquared(&Intersection)) < -MYEPSILON) {
1165 // inside, next!
[e138de]1166 Log() << Verbose(2) << *Walker << " is inside wrt triangle " << *BTS << "." << endl;
[357fba]1167 } else {
1168 // outside!
[e138de]1169 Log() << Verbose(2) << *Walker << " is outside wrt triangle " << *BTS << "." << endl;
[357fba]1170 class BoundaryLineSet *OldLines[3], *NewLines[3];
1171 class BoundaryPointSet *OldPoints[3], *NewPoint;
1172 // store the three old lines and old points
1173 for (int i=0;i<3;i++) {
1174 OldLines[i] = BTS->lines[i];
1175 OldPoints[i] = BTS->endpoints[i];
1176 }
[5c7bf8]1177 Normal.CopyVector(&BTS->NormalVector);
[357fba]1178 // add Walker to boundary points
[e138de]1179 Log() << Verbose(2) << "Adding " << *Walker << " to BoundaryPoints." << endl;
[7dea7c]1180 AddFlag = true;
[16d866]1181 if (AddBoundaryPoint(Walker,0))
[357fba]1182 NewPoint = BPS[0];
1183 else
1184 continue;
1185 // remove triangle
[e138de]1186 Log() << Verbose(2) << "Erasing triangle " << *BTS << "." << endl;
[357fba]1187 TrianglesOnBoundary.erase(BTS->Nr);
[5c7bf8]1188 delete(BTS);
[357fba]1189 // create three new boundary lines
1190 for (int i=0;i<3;i++) {
1191 BPS[0] = NewPoint;
1192 BPS[1] = OldPoints[i];
1193 NewLines[i] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
[e138de]1194 Log() << Verbose(3) << "Creating new line " << *NewLines[i] << "." << endl;
[357fba]1195 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, NewLines[i])); // no need for check for unique insertion as BPS[0] is definitely a new one
1196 LinesOnBoundaryCount++;
1197 }
1198 // create three new triangle with new point
1199 for (int i=0;i<3;i++) { // find all baselines
1200 BLS[0] = OldLines[i];
1201 int n = 1;
1202 for (int j=0;j<3;j++) {
1203 if (NewLines[j]->IsConnectedTo(BLS[0])) {
1204 if (n>2) {
[717e0c]1205 Log() << Verbose(1) << BLS[0] << " connects to all of the new lines?!" << endl;
[357fba]1206 return false;
1207 } else
1208 BLS[n++] = NewLines[j];
1209 }
1210 }
1211 // create the triangle
1212 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
[5c7bf8]1213 Normal.Scale(-1.);
1214 BTS->GetNormalVector(Normal);
1215 Normal.Scale(-1.);
[e138de]1216 Log() << Verbose(2) << "Created new triangle " << *BTS << "." << endl;
[357fba]1217 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1218 TrianglesOnBoundaryCount++;
1219 }
1220 }
1221 } else { // something is wrong with FindClosestTriangleToPoint!
[717e0c]1222 eLog() << Verbose(1) << "The closest triangle did not produce an intersection!" << endl;
[357fba]1223 return false;
1224 }
1225 cloud->GoToNext();
1226 }
1227
1228 // exit
1229 delete(Center);
[e138de]1230 Log() << Verbose(1) << "End of InsertStraddlingPoints" << endl;
[357fba]1231 return true;
1232};
1233
[16d866]1234/** Adds a point to the tesselation::PointsOnBoundary list.
[62bb91]1235 * \param *Walker point to add
[08ef35]1236 * \param n TesselStruct::BPS index to put pointer into
1237 * \return true - new point was added, false - point already present
[357fba]1238 */
[776b64]1239bool Tesselation::AddBoundaryPoint(TesselPoint * Walker, const int n)
[357fba]1240{
1241 PointTestPair InsertUnique;
[08ef35]1242 BPS[n] = new class BoundaryPointSet(Walker);
1243 InsertUnique = PointsOnBoundary.insert(PointPair(Walker->nr, BPS[n]));
1244 if (InsertUnique.second) { // if new point was not present before, increase counter
[357fba]1245 PointsOnBoundaryCount++;
[08ef35]1246 return true;
1247 } else {
1248 delete(BPS[n]);
1249 BPS[n] = InsertUnique.first->second;
1250 return false;
[357fba]1251 }
1252}
1253;
1254
1255/** Adds point to Tesselation::PointsOnBoundary if not yet present.
1256 * Tesselation::TPS is set to either this new BoundaryPointSet or to the existing one of not unique.
1257 * @param Candidate point to add
1258 * @param n index for this point in Tesselation::TPS array
1259 */
[776b64]1260void Tesselation::AddTesselationPoint(TesselPoint* Candidate, const int n)
[357fba]1261{
1262 PointTestPair InsertUnique;
1263 TPS[n] = new class BoundaryPointSet(Candidate);
1264 InsertUnique = PointsOnBoundary.insert(PointPair(Candidate->nr, TPS[n]));
1265 if (InsertUnique.second) { // if new point was not present before, increase counter
1266 PointsOnBoundaryCount++;
1267 } else {
1268 delete TPS[n];
[e138de]1269 Log() << Verbose(4) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl;
[357fba]1270 TPS[n] = (InsertUnique.first)->second;
1271 }
1272}
1273;
1274
[f1ef60a]1275/** Sets point to a present Tesselation::PointsOnBoundary.
1276 * Tesselation::TPS is set to the existing one or NULL if not found.
1277 * @param Candidate point to set to
1278 * @param n index for this point in Tesselation::TPS array
1279 */
1280void Tesselation::SetTesselationPoint(TesselPoint* Candidate, const int n) const
1281{
1282 PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidate->nr);
1283 if (FindPoint != PointsOnBoundary.end())
1284 TPS[n] = FindPoint->second;
1285 else
1286 TPS[n] = NULL;
1287};
1288
[357fba]1289/** Function tries to add line from current Points in BPS to BoundaryLineSet.
1290 * If successful it raises the line count and inserts the new line into the BLS,
1291 * if unsuccessful, it writes the line which had been present into the BLS, deleting the new constructed one.
1292 * @param *a first endpoint
1293 * @param *b second endpoint
1294 * @param n index of Tesselation::BLS giving the line with both endpoints
1295 */
[776b64]1296void Tesselation::AddTesselationLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n) {
[357fba]1297 bool insertNewLine = true;
1298
1299 if (a->lines.find(b->node->nr) != a->lines.end()) {
[065e82]1300 LineMap::iterator FindLine = a->lines.find(b->node->nr);
[357fba]1301 pair<LineMap::iterator,LineMap::iterator> FindPair;
1302 FindPair = a->lines.equal_range(b->node->nr);
[e138de]1303 Log() << Verbose(5) << "INFO: There is at least one line between " << *a << " and " << *b << ": " << *(FindLine->second) << "." << endl;
[357fba]1304
[065e82]1305 for (FindLine = FindPair.first; FindLine != FindPair.second; FindLine++) {
[357fba]1306 // If there is a line with less than two attached triangles, we don't need a new line.
[5c7bf8]1307 if (FindLine->second->triangles.size() < 2) {
[357fba]1308 insertNewLine = false;
[e138de]1309 Log() << Verbose(4) << "Using existing line " << *FindLine->second << endl;
[357fba]1310
1311 BPS[0] = FindLine->second->endpoints[0];
1312 BPS[1] = FindLine->second->endpoints[1];
1313 BLS[n] = FindLine->second;
1314
[1e168b]1315 // remove existing line from OpenLines
1316 CandidateMap::iterator CandidateLine = OpenLines.find(BLS[n]);
1317 delete(CandidateLine->second);
1318 OpenLines.erase(CandidateLine);
1319
[357fba]1320 break;
1321 }
1322 }
1323 }
1324
1325 if (insertNewLine) {
[16d866]1326 AlwaysAddTesselationTriangleLine(a, b, n);
[357fba]1327 }
1328}
1329;
1330
1331/**
1332 * Adds lines from each of the current points in the BPS to BoundaryLineSet.
1333 * Raises the line count and inserts the new line into the BLS.
1334 *
1335 * @param *a first endpoint
1336 * @param *b second endpoint
1337 * @param n index of Tesselation::BLS giving the line with both endpoints
1338 */
[776b64]1339void Tesselation::AlwaysAddTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
[357fba]1340{
[1e168b]1341 Log() << Verbose(4) << "Adding open line [" << LinesOnBoundaryCount << "|" << *(a->node) << " and " << *(b->node) << "." << endl;
[357fba]1342 BPS[0] = a;
1343 BPS[1] = b;
1344 BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); // this also adds the line to the local maps
1345 // add line to global map
1346 LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n]));
1347 // increase counter
1348 LinesOnBoundaryCount++;
[1e168b]1349 // also add to open lines
1350 CandidateForTesselation *CFT = new CandidateForTesselation(BLS[n]);
1351 OpenLines.insert(pair< BoundaryLineSet *, CandidateForTesselation *> (BLS[n], CFT));
[357fba]1352};
1353
[7dea7c]1354/** Function adds triangle to global list.
1355 * Furthermore, the triangle receives the next free id and id counter \a TrianglesOnBoundaryCount is increased.
[357fba]1356 */
[16d866]1357void Tesselation::AddTesselationTriangle()
[357fba]1358{
[e138de]1359 Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl;
[357fba]1360
1361 // add triangle to global map
1362 TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
1363 TrianglesOnBoundaryCount++;
1364
[57066a]1365 // set as last new triangle
1366 LastTriangle = BTS;
1367
[357fba]1368 // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
[16d866]1369};
1370
[7dea7c]1371/** Function adds triangle to global list.
1372 * Furthermore, the triangle number is set to \a nr.
1373 * \param nr triangle number
1374 */
[776b64]1375void Tesselation::AddTesselationTriangle(const int nr)
[7dea7c]1376{
[e138de]1377 Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl;
[7dea7c]1378
1379 // add triangle to global map
1380 TrianglesOnBoundary.insert(TrianglePair(nr, BTS));
1381
1382 // set as last new triangle
1383 LastTriangle = BTS;
1384
1385 // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
1386};
1387
[16d866]1388/** Removes a triangle from the tesselation.
1389 * Removes itself from the TriangleMap's of its lines, calls for them RemoveTriangleLine() if they are no more connected.
1390 * Removes itself from memory.
1391 * \param *triangle to remove
1392 */
1393void Tesselation::RemoveTesselationTriangle(class BoundaryTriangleSet *triangle)
1394{
1395 if (triangle == NULL)
1396 return;
1397 for (int i = 0; i < 3; i++) {
1398 if (triangle->lines[i] != NULL) {
[e138de]1399 Log() << Verbose(5) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl;
[16d866]1400 triangle->lines[i]->triangles.erase(triangle->Nr);
1401 if (triangle->lines[i]->triangles.empty()) {
[e138de]1402 Log() << Verbose(5) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl;
[16d866]1403 RemoveTesselationLine(triangle->lines[i]);
[065e82]1404 } else {
[e138de]1405 Log() << Verbose(5) << *triangle->lines[i] << " is still attached to another triangle: ";
[065e82]1406 for(TriangleMap::iterator TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); TriangleRunner++)
[e138de]1407 Log() << Verbose(0) << "[" << (TriangleRunner->second)->Nr << "|" << *((TriangleRunner->second)->endpoints[0]) << ", " << *((TriangleRunner->second)->endpoints[1]) << ", " << *((TriangleRunner->second)->endpoints[2]) << "] \t";
1408 Log() << Verbose(0) << endl;
[065e82]1409// for (int j=0;j<2;j++) {
[e138de]1410// Log() << Verbose(5) << "Lines of endpoint " << *(triangle->lines[i]->endpoints[j]) << ": ";
[065e82]1411// for(LineMap::iterator LineRunner = triangle->lines[i]->endpoints[j]->lines.begin(); LineRunner != triangle->lines[i]->endpoints[j]->lines.end(); LineRunner++)
[e138de]1412// Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
1413// Log() << Verbose(0) << endl;
[065e82]1414// }
1415 }
1416 triangle->lines[i] = NULL; // free'd or not: disconnect
[16d866]1417 } else
[717e0c]1418 eLog() << Verbose(1) << "This line " << i << " has already been free'd." << endl;
[16d866]1419 }
1420
1421 if (TrianglesOnBoundary.erase(triangle->Nr))
[e138de]1422 Log() << Verbose(5) << "Removing triangle Nr. " << triangle->Nr << "." << endl;
[16d866]1423 delete(triangle);
1424};
1425
1426/** Removes a line from the tesselation.
1427 * Removes itself from each endpoints' LineMap, then removes itself from global LinesOnBoundary list and free's the line.
1428 * \param *line line to remove
1429 */
1430void Tesselation::RemoveTesselationLine(class BoundaryLineSet *line)
1431{
1432 int Numbers[2];
1433
1434 if (line == NULL)
1435 return;
[065e82]1436 // get other endpoint number for finding copies of same line
[16d866]1437 if (line->endpoints[1] != NULL)
1438 Numbers[0] = line->endpoints[1]->Nr;
1439 else
1440 Numbers[0] = -1;
1441 if (line->endpoints[0] != NULL)
1442 Numbers[1] = line->endpoints[0]->Nr;
1443 else
1444 Numbers[1] = -1;
1445
1446 for (int i = 0; i < 2; i++) {
1447 if (line->endpoints[i] != NULL) {
1448 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
1449 pair<LineMap::iterator, LineMap::iterator> erasor = line->endpoints[i]->lines.equal_range(Numbers[i]);
1450 for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
1451 if ((*Runner).second == line) {
[e138de]1452 Log() << Verbose(5) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
[16d866]1453 line->endpoints[i]->lines.erase(Runner);
1454 break;
1455 }
1456 } else { // there's just a single line left
1457 if (line->endpoints[i]->lines.erase(line->Nr))
[e138de]1458 Log() << Verbose(5) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
[16d866]1459 }
1460 if (line->endpoints[i]->lines.empty()) {
[e138de]1461 Log() << Verbose(5) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl;
[16d866]1462 RemoveTesselationPoint(line->endpoints[i]);
[065e82]1463 } else {
[e138de]1464 Log() << Verbose(5) << *line->endpoints[i] << " has still lines it's attached to: ";
[065e82]1465 for(LineMap::iterator LineRunner = line->endpoints[i]->lines.begin(); LineRunner != line->endpoints[i]->lines.end(); LineRunner++)
[e138de]1466 Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
1467 Log() << Verbose(0) << endl;
[065e82]1468 }
1469 line->endpoints[i] = NULL; // free'd or not: disconnect
[16d866]1470 } else
[717e0c]1471 eLog() << Verbose(1) << "Endpoint " << i << " has already been free'd." << endl;
[16d866]1472 }
1473 if (!line->triangles.empty())
[717e0c]1474 eLog() << Verbose(2) << "Memory Leak! I " << *line << " am still connected to some triangles." << endl;
[16d866]1475
1476 if (LinesOnBoundary.erase(line->Nr))
[e138de]1477 Log() << Verbose(5) << "Removing line Nr. " << line->Nr << "." << endl;
[16d866]1478 delete(line);
1479};
1480
1481/** Removes a point from the tesselation.
1482 * Checks whether there are still lines connected, removes from global PointsOnBoundary list, then free's the point.
1483 * \note If a point should be removed, while keep the tesselated surface intact (i.e. closed), use RemovePointFromTesselatedSurface()
1484 * \param *point point to remove
1485 */
1486void Tesselation::RemoveTesselationPoint(class BoundaryPointSet *point)
1487{
1488 if (point == NULL)
1489 return;
1490 if (PointsOnBoundary.erase(point->Nr))
[e138de]1491 Log() << Verbose(5) << "Removing point Nr. " << point->Nr << "." << endl;
[16d866]1492 delete(point);
1493};
[357fba]1494
[62bb91]1495/** Checks whether the triangle consisting of the three points is already present.
[357fba]1496 * Searches for the points in Tesselation::PointsOnBoundary and checks their
1497 * lines. If any of the three edges already has two triangles attached, false is
1498 * returned.
1499 * \param *out output stream for debugging
1500 * \param *Candidates endpoints of the triangle candidate
1501 * \return integer 0 if no triangle exists, 1 if one triangle exists, 2 if two
1502 * triangles exist which is the maximum for three points
1503 */
[f1ef60a]1504int Tesselation::CheckPresenceOfTriangle(TesselPoint *Candidates[3]) const
1505{
[357fba]1506 int adjacentTriangleCount = 0;
1507 class BoundaryPointSet *Points[3];
1508
[e138de]1509 Log() << Verbose(2) << "Begin of CheckPresenceOfTriangle" << endl;
[357fba]1510 // builds a triangle point set (Points) of the end points
1511 for (int i = 0; i < 3; i++) {
[f1ef60a]1512 PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr);
[357fba]1513 if (FindPoint != PointsOnBoundary.end()) {
1514 Points[i] = FindPoint->second;
1515 } else {
1516 Points[i] = NULL;
1517 }
1518 }
1519
1520 // checks lines between the points in the Points for their adjacent triangles
1521 for (int i = 0; i < 3; i++) {
1522 if (Points[i] != NULL) {
1523 for (int j = i; j < 3; j++) {
1524 if (Points[j] != NULL) {
[f1ef60a]1525 LineMap::const_iterator FindLine = Points[i]->lines.find(Points[j]->node->nr);
[357fba]1526 for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) {
1527 TriangleMap *triangles = &FindLine->second->triangles;
[e138de]1528 Log() << Verbose(3) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl;
[f1ef60a]1529 for (TriangleMap::const_iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) {
[357fba]1530 if (FindTriangle->second->IsPresentTupel(Points)) {
1531 adjacentTriangleCount++;
1532 }
1533 }
[e138de]1534 Log() << Verbose(3) << "end." << endl;
[357fba]1535 }
1536 // Only one of the triangle lines must be considered for the triangle count.
[e138de]1537 //Log() << Verbose(2) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
[065e82]1538 //return adjacentTriangleCount;
[357fba]1539 }
1540 }
1541 }
1542 }
1543
[e138de]1544 Log() << Verbose(2) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
1545 Log() << Verbose(2) << "End of CheckPresenceOfTriangle" << endl;
[357fba]1546 return adjacentTriangleCount;
1547};
1548
[065e82]1549/** Checks whether the triangle consisting of the three points is already present.
1550 * Searches for the points in Tesselation::PointsOnBoundary and checks their
1551 * lines. If any of the three edges already has two triangles attached, false is
1552 * returned.
1553 * \param *out output stream for debugging
1554 * \param *Candidates endpoints of the triangle candidate
1555 * \return NULL - none found or pointer to triangle
1556 */
[e138de]1557class BoundaryTriangleSet * Tesselation::GetPresentTriangle(TesselPoint *Candidates[3])
[065e82]1558{
1559 class BoundaryTriangleSet *triangle = NULL;
1560 class BoundaryPointSet *Points[3];
1561
1562 // builds a triangle point set (Points) of the end points
1563 for (int i = 0; i < 3; i++) {
1564 PointMap::iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr);
1565 if (FindPoint != PointsOnBoundary.end()) {
1566 Points[i] = FindPoint->second;
1567 } else {
1568 Points[i] = NULL;
1569 }
1570 }
1571
1572 // checks lines between the points in the Points for their adjacent triangles
1573 for (int i = 0; i < 3; i++) {
1574 if (Points[i] != NULL) {
1575 for (int j = i; j < 3; j++) {
1576 if (Points[j] != NULL) {
1577 LineMap::iterator FindLine = Points[i]->lines.find(Points[j]->node->nr);
1578 for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) {
1579 TriangleMap *triangles = &FindLine->second->triangles;
1580 for (TriangleMap::iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) {
1581 if (FindTriangle->second->IsPresentTupel(Points)) {
1582 if ((triangle == NULL) || (triangle->Nr > FindTriangle->second->Nr))
1583 triangle = FindTriangle->second;
1584 }
1585 }
1586 }
1587 // Only one of the triangle lines must be considered for the triangle count.
[e138de]1588 //Log() << Verbose(2) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
[065e82]1589 //return adjacentTriangleCount;
1590 }
1591 }
1592 }
1593 }
1594
1595 return triangle;
1596};
1597
[357fba]1598
[f1cccd]1599/** Finds the starting triangle for FindNonConvexBorder().
1600 * Looks at the outermost point per axis, then FindSecondPointForTesselation()
1601 * for the second and FindNextSuitablePointViaAngleOfSphere() for the third
[357fba]1602 * point are called.
1603 * \param *out output stream for debugging
1604 * \param RADIUS radius of virtual rolling sphere
1605 * \param *LC LinkedCell structure with neighbouring TesselPoint's
1606 */
[e138de]1607void Tesselation::FindStartingTriangle(const double RADIUS, const LinkedCell *LC)
[357fba]1608{
[e138de]1609 Log() << Verbose(1) << "Begin of FindStartingTriangle\n";
[357fba]1610 int i = 0;
1611 TesselPoint* FirstPoint = NULL;
1612 TesselPoint* SecondPoint = NULL;
[62bb91]1613 TesselPoint* MaxPoint[NDIM];
[f1cccd]1614 double maxCoordinate[NDIM];
[357fba]1615 Vector Oben;
1616 Vector helper;
1617 Vector Chord;
1618 Vector SearchDirection;
1619
1620 Oben.Zero();
1621
1622 for (i = 0; i < 3; i++) {
[62bb91]1623 MaxPoint[i] = NULL;
[f1cccd]1624 maxCoordinate[i] = -1;
[357fba]1625 }
1626
[62bb91]1627 // 1. searching topmost point with respect to each axis
[357fba]1628 for (int i=0;i<NDIM;i++) { // each axis
1629 LC->n[i] = LC->N[i]-1; // current axis is topmost cell
1630 for (LC->n[(i+1)%NDIM]=0;LC->n[(i+1)%NDIM]<LC->N[(i+1)%NDIM];LC->n[(i+1)%NDIM]++)
1631 for (LC->n[(i+2)%NDIM]=0;LC->n[(i+2)%NDIM]<LC->N[(i+2)%NDIM];LC->n[(i+2)%NDIM]++) {
[776b64]1632 const LinkedNodes *List = LC->GetCurrentCell();
[e138de]1633 //Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
[357fba]1634 if (List != NULL) {
[776b64]1635 for (LinkedNodes::const_iterator Runner = List->begin();Runner != List->end();Runner++) {
[f1cccd]1636 if ((*Runner)->node->x[i] > maxCoordinate[i]) {
[e138de]1637 Log() << Verbose(2) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl;
[f1cccd]1638 maxCoordinate[i] = (*Runner)->node->x[i];
[62bb91]1639 MaxPoint[i] = (*Runner);
[357fba]1640 }
1641 }
1642 } else {
[717e0c]1643 eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl;
[357fba]1644 }
1645 }
1646 }
1647
[e138de]1648 Log() << Verbose(2) << "Found maximum coordinates: ";
[357fba]1649 for (int i=0;i<NDIM;i++)
[e138de]1650 Log() << Verbose(0) << i << ": " << *MaxPoint[i] << "\t";
1651 Log() << Verbose(0) << endl;
[357fba]1652
1653 BTS = NULL;
[f1cccd]1654 CandidateList *OptCandidates = new CandidateList();
[357fba]1655 for (int k=0;k<NDIM;k++) {
[57066a]1656 Oben.Zero();
[357fba]1657 Oben.x[k] = 1.;
[62bb91]1658 FirstPoint = MaxPoint[k];
[e138de]1659 Log() << Verbose(1) << "Coordinates of start node at " << *FirstPoint->node << "." << endl;
[357fba]1660
1661 double ShortestAngle;
[f1cccd]1662 TesselPoint* OptCandidate = NULL;
[357fba]1663 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.
1664
[57066a]1665 FindSecondPointForTesselation(FirstPoint, Oben, OptCandidate, &ShortestAngle, RADIUS, LC); // we give same point as next candidate as its bonds are looked into in find_second_...
[f1cccd]1666 SecondPoint = OptCandidate;
[357fba]1667 if (SecondPoint == NULL) // have we found a second point?
1668 continue;
1669
1670 helper.CopyVector(FirstPoint->node);
1671 helper.SubtractVector(SecondPoint->node);
1672 helper.Normalize();
1673 Oben.ProjectOntoPlane(&helper);
1674 Oben.Normalize();
1675 helper.VectorProduct(&Oben);
1676 ShortestAngle = 2.*M_PI; // This will indicate the quadrant.
1677
1678 Chord.CopyVector(FirstPoint->node); // bring into calling function
1679 Chord.SubtractVector(SecondPoint->node);
1680 double radius = Chord.ScalarProduct(&Chord);
1681 double CircleRadius = sqrt(RADIUS*RADIUS - radius/4.);
1682 helper.CopyVector(&Oben);
1683 helper.Scale(CircleRadius);
1684 // Now, oben and helper are two orthonormalized vectors in the plane defined by Chord (not normalized)
1685
1686 // look in one direction of baseline for initial candidate
1687 SearchDirection.MakeNormalVector(&Chord, &Oben); // whether we look "left" first or "right" first is not important ...
1688
[5c7bf8]1689 // adding point 1 and point 2 and add the line between them
[e138de]1690 Log() << Verbose(1) << "Coordinates of start node at " << *FirstPoint->node << "." << endl;
[16d866]1691 AddTesselationPoint(FirstPoint, 0);
[e138de]1692 Log() << Verbose(1) << "Found second point is at " << *SecondPoint->node << ".\n";
[16d866]1693 AddTesselationPoint(SecondPoint, 1);
1694 AddTesselationLine(TPS[0], TPS[1], 0);
[357fba]1695
[e138de]1696 //Log() << Verbose(2) << "INFO: OldSphereCenter is at " << helper << ".\n";
[776b64]1697 FindThirdPointForTesselation(Oben, SearchDirection, helper, BLS[0], NULL, *&OptCandidates, &ShortestAngle, RADIUS, LC);
[e138de]1698 Log() << Verbose(1) << "List of third Points is ";
[f1cccd]1699 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
[e138de]1700 Log() << Verbose(0) << " " << *(*it)->point;
[357fba]1701 }
[e138de]1702 Log() << Verbose(0) << endl;
[357fba]1703
[f1cccd]1704 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
[357fba]1705 // add third triangle point
[16d866]1706 AddTesselationPoint((*it)->point, 2);
[357fba]1707 // add the second and third line
[16d866]1708 AddTesselationLine(TPS[1], TPS[2], 1);
1709 AddTesselationLine(TPS[0], TPS[2], 2);
[357fba]1710 // ... and triangles to the Maps of the Tesselation class
1711 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
[16d866]1712 AddTesselationTriangle();
[357fba]1713 // ... and calculate its normal vector (with correct orientation)
1714 (*it)->OptCenter.Scale(-1.);
[e138de]1715 Log() << Verbose(2) << "Anti-Oben is currently " << (*it)->OptCenter << "." << endl;
[357fba]1716 BTS->GetNormalVector((*it)->OptCenter); // vector to compare with should point inwards
[e138de]1717 Log() << Verbose(0) << "==> Found starting triangle consists of " << *FirstPoint << ", " << *SecondPoint << " and "
[357fba]1718 << *(*it)->point << " with normal vector " << BTS->NormalVector << ".\n";
1719
[1e168b]1720// // if we do not reach the end with the next step of iteration, we need to setup a new first line
1721// if (it != OptCandidates->end()--) {
1722// FirstPoint = (*it)->BaseLine->endpoints[0]->node;
1723// SecondPoint = (*it)->point;
1724// // adding point 1 and point 2 and the line between them
1725// AddTesselationPoint(FirstPoint, 0);
1726// AddTesselationPoint(SecondPoint, 1);
1727// AddTesselationLine(TPS[0], TPS[1], 0);
1728// }
[e138de]1729 Log() << Verbose(2) << "Projection is " << BTS->NormalVector.ScalarProduct(&Oben) << "." << endl;
[357fba]1730 }
1731 if (BTS != NULL) // we have created one starting triangle
1732 break;
1733 else {
1734 // remove all candidates from the list and then the list itself
1735 class CandidateForTesselation *remover = NULL;
[f1cccd]1736 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
[357fba]1737 remover = *it;
1738 delete(remover);
1739 }
[f1cccd]1740 OptCandidates->clear();
[357fba]1741 }
1742 }
1743
1744 // remove all candidates from the list and then the list itself
1745 class CandidateForTesselation *remover = NULL;
[f1cccd]1746 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
[357fba]1747 remover = *it;
1748 delete(remover);
1749 }
[f1cccd]1750 delete(OptCandidates);
[e138de]1751 Log() << Verbose(1) << "End of FindStartingTriangle\n";
[357fba]1752};
1753
[f1ef60a]1754/** Checks for a given baseline and a third point candidate whether baselines of the found triangle don't have even better candidates.
1755 * This is supposed to prevent early closing of the tesselation.
1756 * \param *BaseRay baseline, i.e. not \a *OptCandidate
1757 * \param *ThirdNode third point in triangle, not in BoundaryLineSet::endpoints
1758 * \param ShortestAngle path length on this circle band for the current \a *ThirdNode
1759 * \param RADIUS radius of sphere
1760 * \param *LC LinkedCell structure
1761 * \return true - there is a better candidate (smaller angle than \a ShortestAngle), false - no better TesselPoint candidate found
1762 */
1763bool Tesselation::HasOtherBaselineBetterCandidate(const BoundaryLineSet * const BaseRay, const TesselPoint * const ThirdNode, double ShortestAngle, double RADIUS, const LinkedCell * const LC) const
1764{
1765 bool result = false;
1766 Vector CircleCenter;
1767 Vector CirclePlaneNormal;
1768 Vector OldSphereCenter;
1769 Vector SearchDirection;
1770 Vector helper;
1771 TesselPoint *OtherOptCandidate = NULL;
1772 double OtherShortestAngle = 2.*M_PI; // This will indicate the quadrant.
1773 double radius, CircleRadius;
1774 BoundaryLineSet *Line = NULL;
1775 BoundaryTriangleSet *T = NULL;
1776
1777 Log() << Verbose(1) << "Begin of HasOtherBaselineBetterCandidate" << endl;
1778
1779 // check both other lines
1780 PointMap::const_iterator FindPoint = PointsOnBoundary.find(ThirdNode->nr);
1781 if (FindPoint != PointsOnBoundary.end()) {
1782 for (int i=0;i<2;i++) {
1783 LineMap::const_iterator FindLine = (FindPoint->second)->lines.find(BaseRay->endpoints[0]->node->nr);
1784 if (FindLine != (FindPoint->second)->lines.end()) {
1785 Line = FindLine->second;
1786 Log() << Verbose(1) << "Found line " << *Line << "." << endl;
1787 if (Line->triangles.size() == 1) {
1788 T = Line->triangles.begin()->second;
1789 // construct center of circle
1790 CircleCenter.CopyVector(Line->endpoints[0]->node->node);
1791 CircleCenter.AddVector(Line->endpoints[1]->node->node);
1792 CircleCenter.Scale(0.5);
1793
1794 // construct normal vector of circle
1795 CirclePlaneNormal.CopyVector(Line->endpoints[0]->node->node);
1796 CirclePlaneNormal.SubtractVector(Line->endpoints[1]->node->node);
1797
1798 // calculate squared radius of circle
1799 radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
1800 if (radius/4. < RADIUS*RADIUS) {
1801 CircleRadius = RADIUS*RADIUS - radius/4.;
1802 CirclePlaneNormal.Normalize();
1803 //Log() << Verbose(2) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
1804
1805 // construct old center
1806 GetCenterofCircumcircle(&OldSphereCenter, *T->endpoints[0]->node->node, *T->endpoints[1]->node->node, *T->endpoints[2]->node->node);
1807 helper.CopyVector(&T->NormalVector); // normal vector ensures that this is correct center of the two possible ones
1808 radius = Line->endpoints[0]->node->node->DistanceSquared(&OldSphereCenter);
1809 helper.Scale(sqrt(RADIUS*RADIUS - radius));
1810 OldSphereCenter.AddVector(&helper);
1811 OldSphereCenter.SubtractVector(&CircleCenter);
1812 //Log() << Verbose(2) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl;
1813
1814 // construct SearchDirection
1815 SearchDirection.MakeNormalVector(&T->NormalVector, &CirclePlaneNormal);
1816 helper.CopyVector(Line->endpoints[0]->node->node);
1817 helper.SubtractVector(ThirdNode->node);
1818 if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
1819 SearchDirection.Scale(-1.);
1820 SearchDirection.ProjectOntoPlane(&OldSphereCenter);
1821 SearchDirection.Normalize();
1822 Log() << Verbose(2) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
1823 if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
1824 // rotated the wrong way!
[717e0c]1825 eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl;
[f1ef60a]1826 }
1827
1828 // add third point
1829 CandidateList *OptCandidates = new CandidateList();
1830 FindThirdPointForTesselation(T->NormalVector, SearchDirection, OldSphereCenter, Line, ThirdNode, OptCandidates, &OtherShortestAngle, RADIUS, LC);
1831 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
1832 if (((*it)->point == BaseRay->endpoints[0]->node) || ((*it)->point == BaseRay->endpoints[1]->node)) // skip if it's the same triangle than suggested
1833 continue;
1834 Log() << Verbose(1) << " Third point candidate is " << *(*it)->point
1835 << " with circumsphere's center at " << (*it)->OptCenter << "." << endl;
1836 Log() << Verbose(1) << " Baseline is " << *BaseRay << endl;
1837
1838 // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2)
1839 TesselPoint *PointCandidates[3];
1840 PointCandidates[0] = (*it)->point;
1841 PointCandidates[1] = BaseRay->endpoints[0]->node;
1842 PointCandidates[2] = BaseRay->endpoints[1]->node;
1843 bool check=false;
1844 int existentTrianglesCount = CheckPresenceOfTriangle(PointCandidates);
1845 // If there is no triangle, add it regularly.
1846 if (existentTrianglesCount == 0) {
1847 SetTesselationPoint((*it)->point, 0);
1848 SetTesselationPoint(BaseRay->endpoints[0]->node, 1);
1849 SetTesselationPoint(BaseRay->endpoints[1]->node, 2);
1850
1851 if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const )TPS)) {
1852 OtherOptCandidate = (*it)->point;
1853 check = true;
1854 }
1855 } else if ((existentTrianglesCount >= 1) && (existentTrianglesCount <= 3)) { // If there is a planar region within the structure, we need this triangle a second time.
1856 SetTesselationPoint((*it)->point, 0);
1857 SetTesselationPoint(BaseRay->endpoints[0]->node, 1);
1858 SetTesselationPoint(BaseRay->endpoints[1]->node, 2);
1859
1860 // 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)
1861 // i.e. at least one of the three lines must be present with TriangleCount <= 1
1862 if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const)TPS)) {
1863 OtherOptCandidate = (*it)->point;
1864 check = true;
1865 }
1866 }
1867
1868 if (check) {
1869 if (ShortestAngle > OtherShortestAngle) {
1870 Log() << Verbose(1) << "There is a better candidate than " << *ThirdNode << " with " << ShortestAngle << " from baseline " << *Line << ": " << *OtherOptCandidate << " with " << OtherShortestAngle << "." << endl;
1871 result = true;
1872 break;
1873 }
1874 }
1875 }
1876 delete(OptCandidates);
1877 if (result)
1878 break;
1879 } else {
1880 Log() << Verbose(1) << "Circumcircle for base line " << *Line << " and base triangle " << T << " is too big!" << endl;
1881 }
1882 } else {
1883 eLog() << Verbose(2) << "Baseline is connected to two triangles already?" << endl;
1884 }
1885 } else {
1886 Log() << Verbose(2) << "No present baseline between " << BaseRay->endpoints[0] << " and candidate " << *ThirdNode << "." << endl;
1887 }
1888 }
1889 } else {
1890 eLog() << Verbose(1) << "Could not find the TesselPoint " << *ThirdNode << "." << endl;
1891 }
1892
1893 Log() << Verbose(1) << "End of HasOtherBaselineBetterCandidate" << endl;
1894
1895 return result;
1896};
[357fba]1897
1898/** This function finds a triangle to a line, adjacent to an existing one.
1899 * @param out output stream for debugging
[1e168b]1900 * @param CandidateLine current cadndiate baseline to search from
[357fba]1901 * @param T current triangle which \a Line is edge of
1902 * @param RADIUS radius of the rolling ball
1903 * @param N number of found triangles
[62bb91]1904 * @param *LC LinkedCell structure with neighbouring points
[357fba]1905 */
[1e168b]1906bool Tesselation::FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC)
[357fba]1907{
[e138de]1908 Log() << Verbose(0) << "Begin of FindNextSuitableTriangle\n";
[357fba]1909 bool result = true;
[f1cccd]1910 CandidateList *OptCandidates = new CandidateList();
[357fba]1911
1912 Vector CircleCenter;
1913 Vector CirclePlaneNormal;
1914 Vector OldSphereCenter;
1915 Vector SearchDirection;
1916 Vector helper;
1917 TesselPoint *ThirdNode = NULL;
1918 LineMap::iterator testline;
1919 double ShortestAngle = 2.*M_PI; // This will indicate the quadrant.
1920 double radius, CircleRadius;
1921
[1e168b]1922 Log() << Verbose(1) << "Current baseline is " << *CandidateLine.BaseLine << " of triangle " << T << "." << endl;
[357fba]1923 for (int i=0;i<3;i++)
[1e168b]1924 if ((T.endpoints[i]->node != CandidateLine.BaseLine->endpoints[0]->node) && (T.endpoints[i]->node != CandidateLine.BaseLine->endpoints[1]->node))
[357fba]1925 ThirdNode = T.endpoints[i]->node;
1926
1927 // construct center of circle
[1e168b]1928 CircleCenter.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
1929 CircleCenter.AddVector(CandidateLine.BaseLine->endpoints[1]->node->node);
[357fba]1930 CircleCenter.Scale(0.5);
1931
1932 // construct normal vector of circle
[1e168b]1933 CirclePlaneNormal.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
1934 CirclePlaneNormal.SubtractVector(CandidateLine.BaseLine->endpoints[1]->node->node);
[357fba]1935
1936 // calculate squared radius of circle
1937 radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
1938 if (radius/4. < RADIUS*RADIUS) {
1939 CircleRadius = RADIUS*RADIUS - radius/4.;
1940 CirclePlaneNormal.Normalize();
[e138de]1941 //Log() << Verbose(2) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
[357fba]1942
1943 // construct old center
[c0f6c6]1944 GetCenterofCircumcircle(&OldSphereCenter, *T.endpoints[0]->node->node, *T.endpoints[1]->node->node, *T.endpoints[2]->node->node);
[357fba]1945 helper.CopyVector(&T.NormalVector); // normal vector ensures that this is correct center of the two possible ones
[1e168b]1946 radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(&OldSphereCenter);
[357fba]1947 helper.Scale(sqrt(RADIUS*RADIUS - radius));
1948 OldSphereCenter.AddVector(&helper);
1949 OldSphereCenter.SubtractVector(&CircleCenter);
[e138de]1950 //Log() << Verbose(2) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl;
[357fba]1951
1952 // construct SearchDirection
1953 SearchDirection.MakeNormalVector(&T.NormalVector, &CirclePlaneNormal);
[1e168b]1954 helper.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
[357fba]1955 helper.SubtractVector(ThirdNode->node);
1956 if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
1957 SearchDirection.Scale(-1.);
1958 SearchDirection.ProjectOntoPlane(&OldSphereCenter);
1959 SearchDirection.Normalize();
[e138de]1960 Log() << Verbose(2) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
[357fba]1961 if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
1962 // rotated the wrong way!
[717e0c]1963 eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl;
[357fba]1964 }
1965
1966 // add third point
[1e168b]1967 FindThirdPointForTesselation(T.NormalVector, SearchDirection, OldSphereCenter, CandidateLine.BaseLine, ThirdNode, OptCandidates, &ShortestAngle, RADIUS, LC);
[357fba]1968
1969 } else {
[1e168b]1970 Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and base triangle " << T << " is too big!" << endl;
[357fba]1971 }
1972
[f1cccd]1973 if (OptCandidates->begin() == OptCandidates->end()) {
[717e0c]1974 eLog() << Verbose(2) << "Could not find a suitable candidate." << endl;
[357fba]1975 return false;
1976 }
[e138de]1977 Log() << Verbose(1) << "Third Points are ";
[f1cccd]1978 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
[f1ef60a]1979 Log() << Verbose(1) << " " << *(*it)->point << endl;
[357fba]1980 }
1981
[1e168b]1982 BoundaryLineSet *BaseRay = CandidateLine.BaseLine;
[f1cccd]1983 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
[e138de]1984 Log() << Verbose(1) << " Third point candidate is " << *(*it)->point
[357fba]1985 << " with circumsphere's center at " << (*it)->OptCenter << "." << endl;
[e138de]1986 Log() << Verbose(1) << " Baseline is " << *BaseRay << endl;
[357fba]1987
1988 // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2)
[62bb91]1989 TesselPoint *PointCandidates[3];
1990 PointCandidates[0] = (*it)->point;
1991 PointCandidates[1] = BaseRay->endpoints[0]->node;
1992 PointCandidates[2] = BaseRay->endpoints[1]->node;
[e138de]1993 int existentTrianglesCount = CheckPresenceOfTriangle(PointCandidates);
[357fba]1994
1995 BTS = NULL;
[f1ef60a]1996 // check for present edges and whether we reach better candidates from them
[6a7f78c]1997 //if (HasOtherBaselineBetterCandidate(BaseRay, (*it)->point, ShortestAngle, RADIUS, LC) ) {
1998 if (0) {
[f1ef60a]1999 result = false;
2000 break;
2001 } else {
2002 // If there is no triangle, add it regularly.
2003 if (existentTrianglesCount == 0) {
[16d866]2004 AddTesselationPoint((*it)->point, 0);
2005 AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
2006 AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
[357fba]2007
[f1ef60a]2008 if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const )TPS)) {
[1e168b]2009 CandidateLine.point = (*it)->point;
2010 CandidateLine.OptCenter.CopyVector(&((*it)->OptCenter));
2011 CandidateLine.OtherOptCenter.CopyVector(&((*it)->OtherOptCenter));
2012 CandidateLine.ShortestAngle = ShortestAngle;
[357fba]2013 } else {
[1e168b]2014// eLog() << Verbose(2) << "This triangle consisting of ";
2015// Log() << Verbose(0) << *(*it)->point << ", ";
2016// Log() << Verbose(0) << *BaseRay->endpoints[0]->node << " and ";
2017// Log() << Verbose(0) << *BaseRay->endpoints[1]->node << " ";
2018// Log() << Verbose(0) << "exists and is not added, as it 0x80000000006fc150(does not seem helpful!" << endl;
[357fba]2019 result = false;
2020 }
[f1ef60a]2021 } else if ((existentTrianglesCount >= 1) && (existentTrianglesCount <= 3)) { // If there is a planar region within the structure, we need this triangle a second time.
2022 AddTesselationPoint((*it)->point, 0);
2023 AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
2024 AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
2025
2026 // 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)
2027 // i.e. at least one of the three lines must be present with TriangleCount <= 1
[1e168b]2028 if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const)TPS) || CandidateLine.BaseLine->skipped) {
2029 CandidateLine.point = (*it)->point;
2030 CandidateLine.OptCenter.CopyVector(&(*it)->OptCenter);
2031 CandidateLine.OtherOptCenter.CopyVector(&(*it)->OtherOptCenter);
2032 CandidateLine.ShortestAngle = ShortestAngle+2.*M_PI;
[f1ef60a]2033
2034 } else {
[1e168b]2035// eLog() << Verbose(2) << "This triangle consisting of " << *(*it)->point << ", " << *BaseRay->endpoints[0]->node << " and " << *BaseRay->endpoints[1]->node << " " << "exists and is not added, as it does not seem helpful!" << endl;
[f1ef60a]2036 result = false;
2037 }
2038 } else {
[1e168b]2039// Log() << Verbose(1) << "This triangle consisting of ";
2040// Log() << Verbose(0) << *(*it)->point << ", ";
2041// Log() << Verbose(0) << *BaseRay->endpoints[0]->node << " and ";
2042// Log() << Verbose(0) << *BaseRay->endpoints[1]->node << " ";
2043// Log() << Verbose(0) << "is invalid!" << endl;
[f1ef60a]2044 result = false;
2045 }
[357fba]2046 }
2047
2048 // set baseline to new ray from ref point (here endpoints[0]->node) to current candidate (here (*it)->point))
2049 BaseRay = BLS[0];
[57066a]2050 if ((BTS != NULL) && (BTS->NormalVector.NormSquared() < MYEPSILON)) {
[717e0c]2051 eLog() << Verbose(1) << "Triangle " << *BTS << " has zero normal vector!" << endl;
[57066a]2052 exit(255);
2053 }
2054
[357fba]2055 }
2056
2057 // remove all candidates from the list and then the list itself
2058 class CandidateForTesselation *remover = NULL;
[f1cccd]2059 for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
[357fba]2060 remover = *it;
2061 delete(remover);
2062 }
[f1cccd]2063 delete(OptCandidates);
[e138de]2064 Log() << Verbose(0) << "End of FindNextSuitableTriangle\n";
[357fba]2065 return result;
2066};
2067
[1e168b]2068/** Adds the present line and candidate point from \a &CandidateLine to the Tesselation.
2069 * \param &CandidateLine triangle to add
2070 */
2071void Tesselation::AddCandidateTriangle(CandidateForTesselation &CandidateLine)
2072{
2073 Vector Center;
2074 Log() << Verbose(2) << "BaseLine is :" << *(CandidateLine.BaseLine) << " with candidate " << *(CandidateLine.point) << "." << endl;
2075 // add the points
2076 AddTesselationPoint(CandidateLine.point, 0);
2077 AddTesselationPoint(CandidateLine.BaseLine->endpoints[0]->node, 1);
2078 AddTesselationPoint(CandidateLine.BaseLine->endpoints[1]->node, 2);
2079
2080 Center.CopyVector(&CandidateLine.OptCenter);
2081 // add the lines
2082 AddTesselationLine(TPS[0], TPS[1], 0);
2083 AddTesselationLine(TPS[0], TPS[2], 1);
2084 AddTesselationLine(TPS[1], TPS[2], 2);
2085
2086 // add the triangles
2087 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
2088 AddTesselationTriangle();
2089 Center.Scale(-1.);
2090 BTS->GetNormalVector(Center);
2091
2092 Log() << Verbose(0) << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " for this triangle ... " << endl;
2093};
2094
[16d866]2095/** Checks whether the quadragon of the two triangles connect to \a *Base is convex.
2096 * We look whether the closest point on \a *Base with respect to the other baseline is outside
2097 * of the segment formed by both endpoints (concave) or not (convex).
2098 * \param *out output stream for debugging
2099 * \param *Base line to be flipped
[57066a]2100 * \return NULL - convex, otherwise endpoint that makes it concave
[16d866]2101 */
[e138de]2102class BoundaryPointSet *Tesselation::IsConvexRectangle(class BoundaryLineSet *Base)
[16d866]2103{
2104 class BoundaryPointSet *Spot = NULL;
2105 class BoundaryLineSet *OtherBase;
[0077b5]2106 Vector *ClosestPoint;
[16d866]2107
2108 int m=0;
2109 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2110 for (int j=0;j<3;j++) // all of their endpoints and baselines
2111 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
2112 BPS[m++] = runner->second->endpoints[j];
2113 OtherBase = new class BoundaryLineSet(BPS,-1);
2114
[e138de]2115 Log() << Verbose(3) << "INFO: Current base line is " << *Base << "." << endl;
2116 Log() << Verbose(3) << "INFO: Other base line is " << *OtherBase << "." << endl;
[16d866]2117
2118 // get the closest point on each line to the other line
[e138de]2119 ClosestPoint = GetClosestPointBetweenLine(Base, OtherBase);
[16d866]2120
2121 // delete the temporary other base line
2122 delete(OtherBase);
2123
2124 // get the distance vector from Base line to OtherBase line
[0077b5]2125 Vector DistanceToIntersection[2], BaseLine;
2126 double distance[2];
[16d866]2127 BaseLine.CopyVector(Base->endpoints[1]->node->node);
2128 BaseLine.SubtractVector(Base->endpoints[0]->node->node);
[0077b5]2129 for (int i=0;i<2;i++) {
2130 DistanceToIntersection[i].CopyVector(ClosestPoint);
2131 DistanceToIntersection[i].SubtractVector(Base->endpoints[i]->node->node);
2132 distance[i] = BaseLine.ScalarProduct(&DistanceToIntersection[i]);
[16d866]2133 }
[1d9b7aa]2134 delete(ClosestPoint);
2135 if ((distance[0] * distance[1]) > 0) { // have same sign?
[e138de]2136 Log() << Verbose(3) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1] << ". " << *Base << "' rectangle is concave." << endl;
[0077b5]2137 if (distance[0] < distance[1]) {
2138 Spot = Base->endpoints[0];
2139 } else {
2140 Spot = Base->endpoints[1];
2141 }
[16d866]2142 return Spot;
[0077b5]2143 } else { // different sign, i.e. we are in between
[e138de]2144 Log() << Verbose(3) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl;
[16d866]2145 return NULL;
2146 }
2147
2148};
2149
[776b64]2150void Tesselation::PrintAllBoundaryPoints(ofstream *out) const
[0077b5]2151{
2152 // print all lines
[e138de]2153 Log() << Verbose(1) << "Printing all boundary points for debugging:" << endl;
[776b64]2154 for (PointMap::const_iterator PointRunner = PointsOnBoundary.begin();PointRunner != PointsOnBoundary.end(); PointRunner++)
[e138de]2155 Log() << Verbose(2) << *(PointRunner->second) << endl;
[0077b5]2156};
2157
[776b64]2158void Tesselation::PrintAllBoundaryLines(ofstream *out) const
[0077b5]2159{
2160 // print all lines
[e138de]2161 Log() << Verbose(1) << "Printing all boundary lines for debugging:" << endl;
[776b64]2162 for (LineMap::const_iterator LineRunner = LinesOnBoundary.begin(); LineRunner != LinesOnBoundary.end(); LineRunner++)
[e138de]2163 Log() << Verbose(2) << *(LineRunner->second) << endl;
[0077b5]2164};
2165
[776b64]2166void Tesselation::PrintAllBoundaryTriangles(ofstream *out) const
[0077b5]2167{
2168 // print all triangles
[e138de]2169 Log() << Verbose(1) << "Printing all boundary triangles for debugging:" << endl;
[776b64]2170 for (TriangleMap::const_iterator TriangleRunner = TrianglesOnBoundary.begin(); TriangleRunner != TrianglesOnBoundary.end(); TriangleRunner++)
[e138de]2171 Log() << Verbose(2) << *(TriangleRunner->second) << endl;
[0077b5]2172};
[357fba]2173
[16d866]2174/** For a given boundary line \a *Base and its two triangles, picks the central baseline that is "higher".
[357fba]2175 * \param *out output stream for debugging
[16d866]2176 * \param *Base line to be flipped
[57066a]2177 * \return volume change due to flipping (0 - then no flipped occured)
[357fba]2178 */
[e138de]2179double Tesselation::PickFarthestofTwoBaselines(class BoundaryLineSet *Base)
[357fba]2180{
[16d866]2181 class BoundaryLineSet *OtherBase;
2182 Vector *ClosestPoint[2];
[57066a]2183 double volume;
[16d866]2184
2185 int m=0;
2186 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2187 for (int j=0;j<3;j++) // all of their endpoints and baselines
2188 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
2189 BPS[m++] = runner->second->endpoints[j];
2190 OtherBase = new class BoundaryLineSet(BPS,-1);
[62bb91]2191
[e138de]2192 Log() << Verbose(3) << "INFO: Current base line is " << *Base << "." << endl;
2193 Log() << Verbose(3) << "INFO: Other base line is " << *OtherBase << "." << endl;
[62bb91]2194
[16d866]2195 // get the closest point on each line to the other line
[e138de]2196 ClosestPoint[0] = GetClosestPointBetweenLine(Base, OtherBase);
2197 ClosestPoint[1] = GetClosestPointBetweenLine(OtherBase, Base);
[16d866]2198
2199 // get the distance vector from Base line to OtherBase line
2200 Vector Distance;
2201 Distance.CopyVector(ClosestPoint[1]);
2202 Distance.SubtractVector(ClosestPoint[0]);
2203
[57066a]2204 // calculate volume
[c0f6c6]2205 volume = CalculateVolumeofGeneralTetraeder(*Base->endpoints[1]->node->node, *OtherBase->endpoints[0]->node->node, *OtherBase->endpoints[1]->node->node, *Base->endpoints[0]->node->node);
[57066a]2206
[0077b5]2207 // delete the temporary other base line and the closest points
2208 delete(ClosestPoint[0]);
2209 delete(ClosestPoint[1]);
[16d866]2210 delete(OtherBase);
2211
2212 if (Distance.NormSquared() < MYEPSILON) { // check for intersection
[e138de]2213 Log() << Verbose(3) << "REJECT: Both lines have an intersection: Nothing to do." << endl;
[16d866]2214 return false;
2215 } else { // check for sign against BaseLineNormal
2216 Vector BaseLineNormal;
[5c7bf8]2217 BaseLineNormal.Zero();
2218 if (Base->triangles.size() < 2) {
[717e0c]2219 eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl;
[57066a]2220 return 0.;
[5c7bf8]2221 }
2222 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
[e138de]2223 Log() << Verbose(4) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
[5c7bf8]2224 BaseLineNormal.AddVector(&(runner->second->NormalVector));
2225 }
[0077b5]2226 BaseLineNormal.Scale(1./2.);
[357fba]2227
[16d866]2228 if (Distance.ScalarProduct(&BaseLineNormal) > MYEPSILON) { // Distance points outwards, hence OtherBase higher than Base -> flip
[e138de]2229 Log() << Verbose(2) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl;
[57066a]2230 // calculate volume summand as a general tetraeder
2231 return volume;
[16d866]2232 } else { // Base higher than OtherBase -> do nothing
[e138de]2233 Log() << Verbose(2) << "REJECT: Base line is higher: Nothing to do." << endl;
[57066a]2234 return 0.;
[16d866]2235 }
2236 }
2237};
[357fba]2238
[16d866]2239/** For a given baseline and its two connected triangles, flips the baseline.
2240 * I.e. we create the new baseline between the other two endpoints of these four
2241 * endpoints and reconstruct the two triangles accordingly.
2242 * \param *out output stream for debugging
2243 * \param *Base line to be flipped
[57066a]2244 * \return pointer to allocated new baseline - flipping successful, NULL - something went awry
[16d866]2245 */
[e138de]2246class BoundaryLineSet * Tesselation::FlipBaseline(class BoundaryLineSet *Base)
[16d866]2247{
2248 class BoundaryLineSet *OldLines[4], *NewLine;
2249 class BoundaryPointSet *OldPoints[2];
2250 Vector BaseLineNormal;
2251 int OldTriangleNrs[2], OldBaseLineNr;
2252 int i,m;
2253
[e138de]2254 Log() << Verbose(1) << "Begin of FlipBaseline" << endl;
[16d866]2255
2256 // calculate NormalVector for later use
2257 BaseLineNormal.Zero();
2258 if (Base->triangles.size() < 2) {
[717e0c]2259 eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl;
[57066a]2260 return NULL;
[16d866]2261 }
2262 for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
[e138de]2263 Log() << Verbose(4) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
[16d866]2264 BaseLineNormal.AddVector(&(runner->second->NormalVector));
2265 }
2266 BaseLineNormal.Scale(-1./2.); // has to point inside for BoundaryTriangleSet::GetNormalVector()
2267
2268 // get the two triangles
2269 // gather four endpoints and four lines
2270 for (int j=0;j<4;j++)
2271 OldLines[j] = NULL;
2272 for (int j=0;j<2;j++)
2273 OldPoints[j] = NULL;
2274 i=0;
2275 m=0;
[e138de]2276 Log() << Verbose(3) << "The four old lines are: ";
[16d866]2277 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2278 for (int j=0;j<3;j++) // all of their endpoints and baselines
2279 if (runner->second->lines[j] != Base) { // pick not the central baseline
2280 OldLines[i++] = runner->second->lines[j];
[e138de]2281 Log() << Verbose(0) << *runner->second->lines[j] << "\t";
[357fba]2282 }
[e138de]2283 Log() << Verbose(0) << endl;
2284 Log() << Verbose(3) << "The two old points are: ";
[16d866]2285 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
2286 for (int j=0;j<3;j++) // all of their endpoints and baselines
2287 if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) { // and neither of its endpoints
2288 OldPoints[m++] = runner->second->endpoints[j];
[e138de]2289 Log() << Verbose(0) << *runner->second->endpoints[j] << "\t";
[16d866]2290 }
[e138de]2291 Log() << Verbose(0) << endl;
[16d866]2292
2293 // check whether everything is in place to create new lines and triangles
2294 if (i<4) {
[717e0c]2295 eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl;
[57066a]2296 return NULL;
[16d866]2297 }
2298 for (int j=0;j<4;j++)
2299 if (OldLines[j] == NULL) {
[717e0c]2300 eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl;
[57066a]2301 return NULL;
[16d866]2302 }
2303 for (int j=0;j<2;j++)
2304 if (OldPoints[j] == NULL) {
[717e0c]2305 eLog() << Verbose(1) << "We have not gathered enough endpoints!" << endl;
[57066a]2306 return NULL;
[357fba]2307 }
[16d866]2308
2309 // remove triangles and baseline removes itself
[e138de]2310 Log() << Verbose(3) << "INFO: Deleting baseline " << *Base << " from global list." << endl;
[16d866]2311 OldBaseLineNr = Base->Nr;
2312 m=0;
2313 for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
[e138de]2314 Log() << Verbose(3) << "INFO: Deleting triangle " << *(runner->second) << "." << endl;
[16d866]2315 OldTriangleNrs[m++] = runner->second->Nr;
2316 RemoveTesselationTriangle(runner->second);
2317 }
2318
2319 // construct new baseline (with same number as old one)
2320 BPS[0] = OldPoints[0];
2321 BPS[1] = OldPoints[1];
2322 NewLine = new class BoundaryLineSet(BPS, OldBaseLineNr);
2323 LinesOnBoundary.insert(LinePair(OldBaseLineNr, NewLine)); // no need for check for unique insertion as NewLine is definitely a new one
[e138de]2324 Log() << Verbose(3) << "INFO: Created new baseline " << *NewLine << "." << endl;
[16d866]2325
2326 // construct new triangles with flipped baseline
2327 i=-1;
2328 if (OldLines[0]->IsConnectedTo(OldLines[2]))
2329 i=2;
2330 if (OldLines[0]->IsConnectedTo(OldLines[3]))
2331 i=3;
2332 if (i!=-1) {
2333 BLS[0] = OldLines[0];
2334 BLS[1] = OldLines[i];
2335 BLS[2] = NewLine;
2336 BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[0]);
2337 BTS->GetNormalVector(BaseLineNormal);
[7dea7c]2338 AddTesselationTriangle(OldTriangleNrs[0]);
[e138de]2339 Log() << Verbose(3) << "INFO: Created new triangle " << *BTS << "." << endl;
[16d866]2340
2341 BLS[0] = (i==2 ? OldLines[3] : OldLines[2]);
2342 BLS[1] = OldLines[1];
2343 BLS[2] = NewLine;
2344 BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[1]);
2345 BTS->GetNormalVector(BaseLineNormal);
[7dea7c]2346 AddTesselationTriangle(OldTriangleNrs[1]);
[e138de]2347 Log() << Verbose(3) << "INFO: Created new triangle " << *BTS << "." << endl;
[16d866]2348 } else {
[e138de]2349 Log() << Verbose(1) << "The four old lines do not connect, something's utterly wrong here!" << endl;
[57066a]2350 return NULL;
[357fba]2351 }
[16d866]2352
[e138de]2353 Log() << Verbose(1) << "End of FlipBaseline" << endl;
[57066a]2354 return NewLine;
[357fba]2355};
2356
[16d866]2357
[357fba]2358/** Finds the second point of starting triangle.
2359 * \param *a first node
2360 * \param Oben vector indicating the outside
[f1cccd]2361 * \param OptCandidate reference to recommended candidate on return
[357fba]2362 * \param Storage[3] array storing angles and other candidate information
2363 * \param RADIUS radius of virtual sphere
[62bb91]2364 * \param *LC LinkedCell structure with neighbouring points
[357fba]2365 */
[776b64]2366void Tesselation::FindSecondPointForTesselation(TesselPoint* a, Vector Oben, TesselPoint*& OptCandidate, double Storage[3], double RADIUS, const LinkedCell *LC)
[357fba]2367{
[e138de]2368 Log() << Verbose(2) << "Begin of FindSecondPointForTesselation" << endl;
[357fba]2369 Vector AngleCheck;
[57066a]2370 class TesselPoint* Candidate = NULL;
[776b64]2371 double norm = -1.;
2372 double angle = 0.;
2373 int N[NDIM];
2374 int Nlower[NDIM];
2375 int Nupper[NDIM];
[357fba]2376
[62bb91]2377 if (LC->SetIndexToNode(a)) { // get cell for the starting point
[357fba]2378 for(int i=0;i<NDIM;i++) // store indices of this cell
2379 N[i] = LC->n[i];
2380 } else {
[717e0c]2381 eLog() << Verbose(1) << "Point " << *a << " is not found in cell " << LC->index << "." << endl;
[357fba]2382 return;
2383 }
[62bb91]2384 // then go through the current and all neighbouring cells and check the contained points for possible candidates
[357fba]2385 for (int i=0;i<NDIM;i++) {
2386 Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
2387 Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
2388 }
[f1ef60a]2389 Log() << Verbose(3) << "LC Intervals from [" << N[0] << "<->" << LC->N[0] << ", " << N[1] << "<->" << LC->N[1] << ", " << N[2] << "<->" << LC->N[2] << "] :"
2390 << " [" << Nlower[0] << "," << Nupper[0] << "], " << " [" << Nlower[1] << "," << Nupper[1] << "], " << " [" << Nlower[2] << "," << Nupper[2] << "], " << endl;
[357fba]2391
2392 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
2393 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
2394 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
[776b64]2395 const LinkedNodes *List = LC->GetCurrentCell();
[e138de]2396 //Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
[357fba]2397 if (List != NULL) {
[776b64]2398 for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[357fba]2399 Candidate = (*Runner);
2400 // check if we only have one unique point yet ...
2401 if (a != Candidate) {
2402 // Calculate center of the circle with radius RADIUS through points a and Candidate
[f1cccd]2403 Vector OrthogonalizedOben, aCandidate, Center;
[357fba]2404 double distance, scaleFactor;
2405
2406 OrthogonalizedOben.CopyVector(&Oben);
[f1cccd]2407 aCandidate.CopyVector(a->node);
2408 aCandidate.SubtractVector(Candidate->node);
2409 OrthogonalizedOben.ProjectOntoPlane(&aCandidate);
[357fba]2410 OrthogonalizedOben.Normalize();
[f1cccd]2411 distance = 0.5 * aCandidate.Norm();
[357fba]2412 scaleFactor = sqrt(((RADIUS * RADIUS) - (distance * distance)));
2413 OrthogonalizedOben.Scale(scaleFactor);
2414
2415 Center.CopyVector(Candidate->node);
2416 Center.AddVector(a->node);
2417 Center.Scale(0.5);
2418 Center.AddVector(&OrthogonalizedOben);
2419
2420 AngleCheck.CopyVector(&Center);
2421 AngleCheck.SubtractVector(a->node);
[f1cccd]2422 norm = aCandidate.Norm();
[357fba]2423 // second point shall have smallest angle with respect to Oben vector
2424 if (norm < RADIUS*2.) {
2425 angle = AngleCheck.Angle(&Oben);
2426 if (angle < Storage[0]) {
[e138de]2427 //Log() << Verbose(3) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]);
2428 Log() << Verbose(3) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n";
[f1cccd]2429 OptCandidate = Candidate;
[357fba]2430 Storage[0] = angle;
[e138de]2431 //Log() << Verbose(3) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[2]);
[357fba]2432 } else {
[e138de]2433 //Log() << Verbose(3) << "Current candidate is " << *Candidate << ": Looses with angle " << angle << " to a better candidate " << *OptCandidate << endl;
[357fba]2434 }
2435 } else {
[e138de]2436 //Log() << Verbose(3) << "Current candidate is " << *Candidate << ": Refused due to Radius " << norm << endl;
[357fba]2437 }
2438 } else {
[e138de]2439 //Log() << Verbose(3) << "Current candidate is " << *Candidate << ": Candidate is equal to first endpoint." << *a << "." << endl;
[357fba]2440 }
2441 }
2442 } else {
[e138de]2443 Log() << Verbose(3) << "Linked cell list is empty." << endl;
[357fba]2444 }
2445 }
[e138de]2446 Log() << Verbose(2) << "End of FindSecondPointForTesselation" << endl;
[357fba]2447};
2448
2449
2450/** This recursive function finds a third point, to form a triangle with two given ones.
2451 * Note that this function is for the starting triangle.
2452 * The idea is as follows: A sphere with fixed radius is (almost) uniquely defined in space by three points
2453 * that sit on its boundary. Hence, when two points are given and we look for the (next) third point, then
2454 * the center of the sphere is still fixed up to a single parameter. The band of possible values
2455 * describes a circle in 3D-space. The old center of the sphere for the current base triangle gives
2456 * us the "null" on this circle, the new center of the candidate point will be some way along this
2457 * circle. The shorter the way the better is the candidate. Note that the direction is clearly given
2458 * by the normal vector of the base triangle that always points outwards by construction.
2459 * Hence, we construct a Center of this circle which sits right in the middle of the current base line.
2460 * We construct the normal vector that defines the plane this circle lies in, it is just in the
2461 * direction of the baseline. And finally, we need the radius of the circle, which is given by the rest
2462 * with respect to the length of the baseline and the sphere's fixed \a RADIUS.
2463 * Note that there is one difficulty: The circumcircle is uniquely defined, but for the circumsphere's center
2464 * there are two possibilities which becomes clear from the construction as seen below. Hence, we must check
2465 * both.
2466 * Note also that the acos() function is not unique on [0, 2.*M_PI). Hence, we need an additional check
2467 * to decide for one of the two possible angles. Therefore we need a SearchDirection and to make this check
2468 * sensible we need OldSphereCenter to be orthogonal to it. Either we construct SearchDirection orthogonal
2469 * right away, or -- what we do here -- we rotate the relative sphere centers such that this orthogonality
2470 * holds. Then, the normalized projection onto the SearchDirection is either +1 or -1 and thus states whether
2471 * the angle is uniquely in either (0,M_PI] or [M_PI, 2.*M_PI).
[f1cccd]2472 * @param NormalVector normal direction of the base triangle (here the unit axis vector, \sa FindStartingTriangle())
[357fba]2473 * @param SearchDirection general direction where to search for the next point, relative to center of BaseLine
2474 * @param OldSphereCenter center of sphere for base triangle, relative to center of BaseLine, giving null angle for the parameter circle
2475 * @param BaseLine BoundaryLineSet with the current base line
[62bb91]2476 * @param ThirdNode third point to avoid in search
[357fba]2477 * @param candidates list of equally good candidates to return
[f1cccd]2478 * @param ShortestAngle the current path length on this circle band for the current OptCandidate
[357fba]2479 * @param RADIUS radius of sphere
[62bb91]2480 * @param *LC LinkedCell structure with neighbouring points
[357fba]2481 */
[f1ef60a]2482void Tesselation::FindThirdPointForTesselation(Vector &NormalVector, Vector &SearchDirection, Vector &OldSphereCenter, class BoundaryLineSet *BaseLine, const class TesselPoint * const ThirdNode, CandidateList* &candidates, double *ShortestAngle, const double RADIUS, const LinkedCell *LC) const
[357fba]2483{
2484 Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
2485 Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
2486 Vector SphereCenter;
2487 Vector NewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility
2488 Vector OtherNewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility
2489 Vector NewNormalVector; // normal vector of the Candidate's triangle
2490 Vector helper, OptCandidateCenter, OtherOptCandidateCenter;
2491 double CircleRadius; // radius of this circle
2492 double radius;
2493 double alpha, Otheralpha; // angles (i.e. parameter for the circle).
2494 int N[NDIM], Nlower[NDIM], Nupper[NDIM];
2495 TesselPoint *Candidate = NULL;
2496 CandidateForTesselation *optCandidate = NULL;
2497
[e138de]2498 Log() << Verbose(1) << "Begin of FindThirdPointForTesselation" << endl;
[357fba]2499
[e138de]2500 Log() << Verbose(2) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl;
[357fba]2501
2502 // construct center of circle
2503 CircleCenter.CopyVector(BaseLine->endpoints[0]->node->node);
2504 CircleCenter.AddVector(BaseLine->endpoints[1]->node->node);
2505 CircleCenter.Scale(0.5);
2506
2507 // construct normal vector of circle
2508 CirclePlaneNormal.CopyVector(BaseLine->endpoints[0]->node->node);
2509 CirclePlaneNormal.SubtractVector(BaseLine->endpoints[1]->node->node);
2510
[ab1932]2511 // calculate squared radius TesselPoint *ThirdNode,f circle
[357fba]2512 radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
2513 if (radius/4. < RADIUS*RADIUS) {
2514 CircleRadius = RADIUS*RADIUS - radius/4.;
2515 CirclePlaneNormal.Normalize();
[e138de]2516 //Log() << Verbose(2) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
[357fba]2517
2518 // test whether old center is on the band's plane
2519 if (fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) > HULLEPSILON) {
[717e0c]2520 eLog() << Verbose(1) << "Something's very wrong here: OldSphereCenter is not on the band's plane as desired by " << fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) << "!" << endl;
[357fba]2521 OldSphereCenter.ProjectOntoPlane(&CirclePlaneNormal);
2522 }
2523 radius = OldSphereCenter.ScalarProduct(&OldSphereCenter);
2524 if (fabs(radius - CircleRadius) < HULLEPSILON) {
[e138de]2525 //Log() << Verbose(2) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl;
[357fba]2526
2527 // check SearchDirection
[e138de]2528 //Log() << Verbose(2) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
[357fba]2529 if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) { // rotated the wrong way!
[717e0c]2530 eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl;
[357fba]2531 }
2532
[62bb91]2533 // get cell for the starting point
[357fba]2534 if (LC->SetIndexToVector(&CircleCenter)) {
2535 for(int i=0;i<NDIM;i++) // store indices of this cell
2536 N[i] = LC->n[i];
[e138de]2537 //Log() << Verbose(2) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
[357fba]2538 } else {
[717e0c]2539 eLog() << Verbose(1) << "Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl;
[357fba]2540 return;
2541 }
[62bb91]2542 // then go through the current and all neighbouring cells and check the contained points for possible candidates
[e138de]2543 //Log() << Verbose(2) << "LC Intervals:";
[357fba]2544 for (int i=0;i<NDIM;i++) {
2545 Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
2546 Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
[e138de]2547 //Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ";
[357fba]2548 }
[e138de]2549 //Log() << Verbose(0) << endl;
[357fba]2550 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
2551 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
2552 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
[776b64]2553 const LinkedNodes *List = LC->GetCurrentCell();
[e138de]2554 //Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
[357fba]2555 if (List != NULL) {
[776b64]2556 for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[357fba]2557 Candidate = (*Runner);
2558
2559 // check for three unique points
[e138de]2560 //Log() << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " at " << Candidate->node << "." << endl;
[357fba]2561 if ((Candidate != BaseLine->endpoints[0]->node) && (Candidate != BaseLine->endpoints[1]->node) ){
2562
2563 // construct both new centers
[c0f6c6]2564 GetCenterofCircumcircle(&NewSphereCenter, *BaseLine->endpoints[0]->node->node, *BaseLine->endpoints[1]->node->node, *Candidate->node);
[357fba]2565 OtherNewSphereCenter.CopyVector(&NewSphereCenter);
2566
2567 if ((NewNormalVector.MakeNormalVector(BaseLine->endpoints[0]->node->node, BaseLine->endpoints[1]->node->node, Candidate->node))
2568 && (fabs(NewNormalVector.ScalarProduct(&NewNormalVector)) > HULLEPSILON)
2569 ) {
2570 helper.CopyVector(&NewNormalVector);
[e138de]2571 //Log() << Verbose(2) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl;
[357fba]2572 radius = BaseLine->endpoints[0]->node->node->DistanceSquared(&NewSphereCenter);
2573 if (radius < RADIUS*RADIUS) {
2574 helper.Scale(sqrt(RADIUS*RADIUS - radius));
[e138de]2575 //Log() << Verbose(2) << "INFO: Distance of NewCircleCenter to NewSphereCenter is " << helper.Norm() << " with sphere radius " << RADIUS << "." << endl;
[357fba]2576 NewSphereCenter.AddVector(&helper);
2577 NewSphereCenter.SubtractVector(&CircleCenter);
[e138de]2578 //Log() << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl;
[357fba]2579
2580 // OtherNewSphereCenter is created by the same vector just in the other direction
2581 helper.Scale(-1.);
2582 OtherNewSphereCenter.AddVector(&helper);
2583 OtherNewSphereCenter.SubtractVector(&CircleCenter);
[e138de]2584 //Log() << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl;
[357fba]2585
2586 alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
2587 Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
2588 alpha = min(alpha, Otheralpha);
2589 // if there is a better candidate, drop the current list and add the new candidate
2590 // otherwise ignore the new candidate and keep the list
2591 if (*ShortestAngle > (alpha - HULLEPSILON)) {
2592 optCandidate = new CandidateForTesselation(Candidate, BaseLine, OptCandidateCenter, OtherOptCandidateCenter);
2593 if (fabs(alpha - Otheralpha) > MYEPSILON) {
2594 optCandidate->OptCenter.CopyVector(&NewSphereCenter);
2595 optCandidate->OtherOptCenter.CopyVector(&OtherNewSphereCenter);
2596 } else {
2597 optCandidate->OptCenter.CopyVector(&OtherNewSphereCenter);
2598 optCandidate->OtherOptCenter.CopyVector(&NewSphereCenter);
2599 }
2600 // if there is an equal candidate, add it to the list without clearing the list
2601 if ((*ShortestAngle - HULLEPSILON) < alpha) {
2602 candidates->push_back(optCandidate);
[e138de]2603 Log() << Verbose(2) << "ACCEPT: We have found an equally good candidate: " << *(optCandidate->point) << " with "
[357fba]2604 << alpha << " and circumsphere's center at " << optCandidate->OptCenter << "." << endl;
2605 } else {
2606 // remove all candidates from the list and then the list itself
2607 class CandidateForTesselation *remover = NULL;
2608 for (CandidateList::iterator it = candidates->begin(); it != candidates->end(); ++it) {
2609 remover = *it;
2610 delete(remover);
2611 }
2612 candidates->clear();
2613 candidates->push_back(optCandidate);
[e138de]2614 Log() << Verbose(2) << "ACCEPT: We have found a better candidate: " << *(optCandidate->point) << " with "
[357fba]2615 << alpha << " and circumsphere's center at " << optCandidate->OptCenter << "." << endl;
2616 }
2617 *ShortestAngle = alpha;
[e138de]2618 //Log() << Verbose(2) << "INFO: There are " << candidates->size() << " candidates in the list now." << endl;
[357fba]2619 } else {
2620 if ((optCandidate != NULL) && (optCandidate->point != NULL)) {
[e138de]2621 //Log() << Verbose(2) << "REJECT: Old candidate " << *(optCandidate->point) << " with " << *ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl;
[357fba]2622 } else {
[e138de]2623 //Log() << Verbose(2) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl;
[357fba]2624 }
2625 }
2626
2627 } else {
[e138de]2628 //Log() << Verbose(2) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl;
[357fba]2629 }
2630 } else {
[e138de]2631 //Log() << Verbose(2) << "REJECT: Three points from " << *BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl;
[357fba]2632 }
2633 } else {
2634 if (ThirdNode != NULL) {
[e138de]2635 //Log() << Verbose(2) << "REJECT: Base triangle " << *BaseLine << " and " << *ThirdNode << " contains Candidate " << *Candidate << "." << endl;
[357fba]2636 } else {
[e138de]2637 //Log() << Verbose(2) << "REJECT: Base triangle " << *BaseLine << " contains Candidate " << *Candidate << "." << endl;
[357fba]2638 }
2639 }
2640 }
2641 }
2642 }
2643 } else {
[717e0c]2644 eLog() << Verbose(1) << "The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl;
[357fba]2645 }
2646 } else {
2647 if (ThirdNode != NULL)
[e138de]2648 Log() << Verbose(2) << "Circumcircle for base line " << *BaseLine << " and third node " << *ThirdNode << " is too big!" << endl;
[357fba]2649 else
[e138de]2650 Log() << Verbose(2) << "Circumcircle for base line " << *BaseLine << " is too big!" << endl;
[357fba]2651 }
2652
[e138de]2653 //Log() << Verbose(2) << "INFO: Sorting candidate list ..." << endl;
[357fba]2654 if (candidates->size() > 1) {
2655 candidates->unique();
[f1cccd]2656 candidates->sort(SortCandidates);
[357fba]2657 }
2658
[e138de]2659 Log() << Verbose(1) << "End of FindThirdPointForTesselation" << endl;
[357fba]2660};
2661
2662/** Finds the endpoint two lines are sharing.
2663 * \param *line1 first line
2664 * \param *line2 second line
2665 * \return point which is shared or NULL if none
2666 */
[776b64]2667class BoundaryPointSet *Tesselation::GetCommonEndpoint(const BoundaryLineSet * line1, const BoundaryLineSet * line2) const
[357fba]2668{
[776b64]2669 const BoundaryLineSet * lines[2] = { line1, line2 };
[357fba]2670 class BoundaryPointSet *node = NULL;
2671 map<int, class BoundaryPointSet *> OrderMap;
2672 pair<map<int, class BoundaryPointSet *>::iterator, bool> OrderTest;
2673 for (int i = 0; i < 2; i++)
2674 // for both lines
2675 for (int j = 0; j < 2; j++)
2676 { // for both endpoints
2677 OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (
2678 lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
2679 if (!OrderTest.second)
2680 { // if insertion fails, we have common endpoint
2681 node = OrderTest.first->second;
[e138de]2682 Log() << Verbose(5) << "Common endpoint of lines " << *line1
[357fba]2683 << " and " << *line2 << " is: " << *node << "." << endl;
2684 j = 2;
2685 i = 2;
2686 break;
2687 }
2688 }
2689 return node;
2690};
2691
[62bb91]2692/** Finds the triangle that is closest to a given Vector \a *x.
2693 * \param *out output stream for debugging
2694 * \param *x Vector to look from
2695 * \return list of BoundaryTriangleSet of nearest triangles or NULL in degenerate case.
2696 */
[e138de]2697list<BoundaryTriangleSet*> * Tesselation::FindClosestTrianglesToPoint(const Vector *x, const LinkedCell* LC) const
[62bb91]2698{
[5c7bf8]2699 TesselPoint *trianglePoints[3];
2700 TesselPoint *SecondPoint = NULL;
[57066a]2701 list<BoundaryTriangleSet*> *triangles = NULL;
[62bb91]2702
2703 if (LinesOnBoundary.empty()) {
[e138de]2704 Log() << Verbose(0) << "Error: There is no tesselation structure to compare the point with, please create one first.";
[62bb91]2705 return NULL;
2706 }
[e138de]2707 Log() << Verbose(1) << "Finding closest Tesselpoint to " << *x << " ... " << endl;
[f1cccd]2708 trianglePoints[0] = FindClosestPoint(x, SecondPoint, LC);
[5c7bf8]2709
[62bb91]2710 // check whether closest point is "too close" :), then it's inside
[5c7bf8]2711 if (trianglePoints[0] == NULL) {
[e138de]2712 Log() << Verbose(2) << "Is the only point, no one else is closeby." << endl;
[5c7bf8]2713 return NULL;
2714 }
[62bb91]2715 if (trianglePoints[0]->node->DistanceSquared(x) < MYEPSILON) {
[e138de]2716 Log() << Verbose(3) << "Point is right on a tesselation point, no nearest triangle." << endl;
[776b64]2717 PointMap::const_iterator PointRunner = PointsOnBoundary.find(trianglePoints[0]->nr);
[57066a]2718 triangles = new list<BoundaryTriangleSet*>;
2719 if (PointRunner != PointsOnBoundary.end()) {
2720 for(LineMap::iterator LineRunner = PointRunner->second->lines.begin(); LineRunner != PointRunner->second->lines.end(); LineRunner++)
2721 for(TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++)
2722 triangles->push_back(TriangleRunner->second);
2723 triangles->sort();
2724 triangles->unique();
2725 } else {
2726 PointRunner = PointsOnBoundary.find(SecondPoint->nr);
2727 trianglePoints[0] = SecondPoint;
2728 if (PointRunner != PointsOnBoundary.end()) {
2729 for(LineMap::iterator LineRunner = PointRunner->second->lines.begin(); LineRunner != PointRunner->second->lines.end(); LineRunner++)
2730 for(TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++)
2731 triangles->push_back(TriangleRunner->second);
2732 triangles->sort();
2733 triangles->unique();
2734 } else {
[717e0c]2735 eLog() << Verbose(1) << "I cannot find a boundary point to the tessel point " << *trianglePoints[0] << "." << endl;
[57066a]2736 return NULL;
2737 }
2738 }
2739 } else {
[e138de]2740 list<TesselPoint*> *connectedClosestPoints = GetCircleOfConnectedPoints(trianglePoints[0], x);
[99593f]2741 if (connectedClosestPoints != NULL) {
2742 trianglePoints[1] = connectedClosestPoints->front();
2743 trianglePoints[2] = connectedClosestPoints->back();
2744 for (int i=0;i<3;i++) {
2745 if (trianglePoints[i] == NULL) {
[717e0c]2746 eLog() << Verbose(1) << "IsInnerPoint encounters serious error, point " << i << " not found." << endl;
[99593f]2747 }
[e138de]2748 //Log() << Verbose(2) << "List of triangle points:" << endl;
2749 //Log() << Verbose(3) << *trianglePoints[i] << endl;
[57066a]2750 }
[62bb91]2751
[99593f]2752 triangles = FindTriangles(trianglePoints);
[e138de]2753 Log() << Verbose(2) << "List of possible triangles:" << endl;
[99593f]2754 for(list<BoundaryTriangleSet*>::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++)
[e138de]2755 Log() << Verbose(3) << **Runner << endl;
[62bb91]2756
[99593f]2757 delete(connectedClosestPoints);
2758 } else {
2759 triangles = NULL;
[e138de]2760 Log() << Verbose(1) << "There is no circle of connected points!" << endl;
[99593f]2761 }
[57066a]2762 }
[5c7bf8]2763
[99593f]2764 if ((triangles == NULL) || (triangles->empty())) {
[717e0c]2765 eLog() << Verbose(1) << "There is no nearest triangle. Please check the tesselation structure.";
[57066a]2766 delete(triangles);
[62bb91]2767 return NULL;
2768 } else
2769 return triangles;
2770};
2771
2772/** Finds closest triangle to a point.
2773 * This basically just takes care of the degenerate case, which is not handled in FindClosestTrianglesToPoint().
2774 * \param *out output stream for debugging
2775 * \param *x Vector to look from
2776 * \return list of BoundaryTriangleSet of nearest triangles or NULL.
2777 */
[e138de]2778class BoundaryTriangleSet * Tesselation::FindClosestTriangleToPoint(const Vector *x, const LinkedCell* LC) const
[62bb91]2779{
2780 class BoundaryTriangleSet *result = NULL;
[e138de]2781 list<BoundaryTriangleSet*> *triangles = FindClosestTrianglesToPoint(x, LC);
[57066a]2782 Vector Center;
[62bb91]2783
2784 if (triangles == NULL)
2785 return NULL;
2786
[57066a]2787 if (triangles->size() == 1) { // there is no degenerate case
[62bb91]2788 result = triangles->front();
[e138de]2789 Log() << Verbose(2) << "Normal Vector of this triangle is " << result->NormalVector << "." << endl;
[57066a]2790 } else {
2791 result = triangles->front();
2792 result->GetCenter(&Center);
2793 Center.SubtractVector(x);
[e138de]2794 Log() << Verbose(2) << "Normal Vector of this front side is " << result->NormalVector << "." << endl;
[57066a]2795 if (Center.ScalarProduct(&result->NormalVector) < 0) {
2796 result = triangles->back();
[e138de]2797 Log() << Verbose(2) << "Normal Vector of this back side is " << result->NormalVector << "." << endl;
[57066a]2798 if (Center.ScalarProduct(&result->NormalVector) < 0) {
[717e0c]2799 eLog() << Verbose(1) << "Front and back side yield NormalVector in wrong direction!" << endl;
[57066a]2800 }
2801 }
2802 }
[62bb91]2803 delete(triangles);
2804 return result;
2805};
2806
2807/** Checks whether the provided Vector is within the tesselation structure.
2808 *
2809 * @param point of which to check the position
2810 * @param *LC LinkedCell structure
2811 *
2812 * @return true if the point is inside the tesselation structure, false otherwise
2813 */
[e138de]2814bool Tesselation::IsInnerPoint(const Vector &Point, const LinkedCell* const LC) const
[62bb91]2815{
[e138de]2816 class BoundaryTriangleSet *result = FindClosestTriangleToPoint(&Point, LC);
[57066a]2817 Vector Center;
2818
2819 if (result == NULL) {// is boundary point or only point in point cloud?
[e138de]2820 Log() << Verbose(1) << Point << " is the only point in vicinity." << endl;
[57066a]2821 return false;
2822 }
2823
2824 result->GetCenter(&Center);
[e138de]2825 Log() << Verbose(3) << "INFO: Central point of the triangle is " << Center << "." << endl;
[57066a]2826 Center.SubtractVector(&Point);
[e138de]2827 Log() << Verbose(3) << "INFO: Vector from center to point to test is " << Center << "." << endl;
[57066a]2828 if (Center.ScalarProduct(&result->NormalVector) > -MYEPSILON) {
[e138de]2829 Log() << Verbose(1) << Point << " is an inner point." << endl;
[62bb91]2830 return true;
[57066a]2831 } else {
[e138de]2832 Log() << Verbose(1) << Point << " is NOT an inner point." << endl;
[62bb91]2833 return false;
[57066a]2834 }
[62bb91]2835}
2836
2837/** Checks whether the provided TesselPoint is within the tesselation structure.
2838 *
2839 * @param *Point of which to check the position
2840 * @param *LC Linked Cell structure
2841 *
2842 * @return true if the point is inside the tesselation structure, false otherwise
2843 */
[e138de]2844bool Tesselation::IsInnerPoint(const TesselPoint * const Point, const LinkedCell* const LC) const
[62bb91]2845{
[e138de]2846 return IsInnerPoint(*(Point->node), LC);
[62bb91]2847}
2848
2849/** Gets all points connected to the provided point by triangulation lines.
2850 *
2851 * @param *Point of which get all connected points
2852 *
[065e82]2853 * @return set of the all points linked to the provided one
[62bb91]2854 */
[e138de]2855set<TesselPoint*> * Tesselation::GetAllConnectedPoints(const TesselPoint* const Point) const
[62bb91]2856{
[065e82]2857 set<TesselPoint*> *connectedPoints = new set<TesselPoint*>;
[5c7bf8]2858 class BoundaryPointSet *ReferencePoint = NULL;
[62bb91]2859 TesselPoint* current;
2860 bool takePoint = false;
2861
[e138de]2862 Log() << Verbose(3) << "Begin of GetAllConnectedPoints" << endl;
[a2028e]2863
[5c7bf8]2864 // find the respective boundary point
[776b64]2865 PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
[5c7bf8]2866 if (PointRunner != PointsOnBoundary.end()) {
2867 ReferencePoint = PointRunner->second;
2868 } else {
[e138de]2869 Log() << Verbose(2) << "GetAllConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl;
[5c7bf8]2870 ReferencePoint = NULL;
2871 }
[62bb91]2872
[065e82]2873 // little trick so that we look just through lines connect to the BoundaryPoint
[5c7bf8]2874 // OR fall-back to look through all lines if there is no such BoundaryPoint
[776b64]2875 const LineMap *Lines;;
[5c7bf8]2876 if (ReferencePoint != NULL)
2877 Lines = &(ReferencePoint->lines);
[776b64]2878 else
2879 Lines = &LinesOnBoundary;
2880 LineMap::const_iterator findLines = Lines->begin();
[5c7bf8]2881 while (findLines != Lines->end()) {
[065e82]2882 takePoint = false;
2883
2884 if (findLines->second->endpoints[0]->Nr == Point->nr) {
2885 takePoint = true;
2886 current = findLines->second->endpoints[1]->node;
2887 } else if (findLines->second->endpoints[1]->Nr == Point->nr) {
2888 takePoint = true;
2889 current = findLines->second->endpoints[0]->node;
2890 }
2891
2892 if (takePoint) {
[e138de]2893 Log() << Verbose(5) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is enlisted." << endl;
[065e82]2894 connectedPoints->insert(current);
2895 }
[62bb91]2896
[065e82]2897 findLines++;
[62bb91]2898 }
2899
[16d866]2900 if (connectedPoints->size() == 0) { // if have not found any points
[717e0c]2901 eLog() << Verbose(1) << "We have not found any connected points to " << *Point<< "." << endl;
[16d866]2902 return NULL;
2903 }
[065e82]2904
[e138de]2905 Log() << Verbose(3) << "End of GetAllConnectedPoints" << endl;
[16d866]2906 return connectedPoints;
[065e82]2907};
[16d866]2908
[065e82]2909
2910/** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point.
[16d866]2911 * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points
2912 * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given
2913 * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the
2914 * triangle we are looking for.
2915 *
2916 * @param *out output stream for debugging
2917 * @param *Point of which get all connected points
[065e82]2918 * @param *Reference Reference vector for zero angle or NULL for no preference
2919 * @return list of the all points linked to the provided one
[16d866]2920 */
[e138de]2921list<TesselPoint*> * Tesselation::GetCircleOfConnectedPoints(const TesselPoint* const Point, const Vector * const Reference) const
[16d866]2922{
2923 map<double, TesselPoint*> anglesOfPoints;
[e138de]2924 set<TesselPoint*> *connectedPoints = GetAllConnectedPoints(Point);
[065e82]2925 list<TesselPoint*> *connectedCircle = new list<TesselPoint*>;
2926 Vector center;
2927 Vector PlaneNormal;
2928 Vector AngleZero;
2929 Vector OrthogonalVector;
2930 Vector helper;
[62bb91]2931
[99593f]2932 if (connectedPoints == NULL) {
[e138de]2933 Log() << Verbose(2) << "Could not find any connected points!" << endl;
[99593f]2934 delete(connectedCircle);
2935 return NULL;
2936 }
[e138de]2937 Log() << Verbose(2) << "Begin of GetCircleOfConnectedPoints" << endl;
[a2028e]2938
[16d866]2939 // calculate central point
[776b64]2940 for (set<TesselPoint*>::const_iterator TesselRunner = connectedPoints->begin(); TesselRunner != connectedPoints->end(); TesselRunner++)
[16d866]2941 center.AddVector((*TesselRunner)->node);
[e138de]2942 //Log() << Verbose(0) << "Summed vectors " << center << "; number of points " << connectedPoints.size()
[16d866]2943 // << "; scale factor " << 1.0/connectedPoints.size();
2944 center.Scale(1.0/connectedPoints->size());
[e138de]2945 Log() << Verbose(4) << "INFO: Calculated center of all circle points is " << center << "." << endl;
[5c7bf8]2946
2947 // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points
2948 PlaneNormal.CopyVector(Point->node);
2949 PlaneNormal.SubtractVector(&center);
2950 PlaneNormal.Normalize();
[e138de]2951 Log() << Verbose(4) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl;
[62bb91]2952
2953 // construct one orthogonal vector
[a2028e]2954 if (Reference != NULL) {
[065e82]2955 AngleZero.CopyVector(Reference);
[a2028e]2956 AngleZero.SubtractVector(Point->node);
2957 AngleZero.ProjectOntoPlane(&PlaneNormal);
2958 }
2959 if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON )) {
[e138de]2960 Log() << Verbose(4) << "Using alternatively " << *(*connectedPoints->begin())->node << " as angle 0 referencer." << endl;
[065e82]2961 AngleZero.CopyVector((*connectedPoints->begin())->node);
[a2028e]2962 AngleZero.SubtractVector(Point->node);
2963 AngleZero.ProjectOntoPlane(&PlaneNormal);
2964 if (AngleZero.NormSquared() < MYEPSILON) {
[e138de]2965 eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl;
[a2028e]2966 performCriticalExit();
2967 }
2968 }
[e138de]2969 Log() << Verbose(4) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl;
[a2028e]2970 if (AngleZero.NormSquared() > MYEPSILON)
2971 OrthogonalVector.MakeNormalVector(&PlaneNormal, &AngleZero);
2972 else
2973 OrthogonalVector.MakeNormalVector(&PlaneNormal);
[e138de]2974 Log() << Verbose(4) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl;
[16d866]2975
[5c7bf8]2976 // go through all connected points and calculate angle
[065e82]2977 for (set<TesselPoint*>::iterator listRunner = connectedPoints->begin(); listRunner != connectedPoints->end(); listRunner++) {
[5c7bf8]2978 helper.CopyVector((*listRunner)->node);
2979 helper.SubtractVector(Point->node);
2980 helper.ProjectOntoPlane(&PlaneNormal);
[f1cccd]2981 double angle = GetAngle(helper, AngleZero, OrthogonalVector);
[e138de]2982 Log() << Verbose(3) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl;
[62bb91]2983 anglesOfPoints.insert(pair<double, TesselPoint*>(angle, (*listRunner)));
2984 }
2985
[065e82]2986 for(map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
2987 connectedCircle->push_back(AngleRunner->second);
2988 }
[62bb91]2989
[065e82]2990 delete(connectedPoints);
[a2028e]2991
[e138de]2992 Log() << Verbose(2) << "End of GetCircleOfConnectedPoints" << endl;
[a2028e]2993
[065e82]2994 return connectedCircle;
2995}
[62bb91]2996
[065e82]2997/** Gets all points connected to the provided point by triangulation lines, ordered such that we walk along a closed path.
2998 *
2999 * @param *out output stream for debugging
3000 * @param *Point of which get all connected points
3001 * @return list of the all points linked to the provided one
3002 */
[e138de]3003list<list<TesselPoint*> *> * Tesselation::GetPathsOfConnectedPoints(const TesselPoint* const Point) const
[065e82]3004{
3005 map<double, TesselPoint*> anglesOfPoints;
3006 list<list<TesselPoint*> *> *ListOfPaths = new list<list<TesselPoint*> *>;
3007 list<TesselPoint*> *connectedPath = NULL;
3008 Vector center;
3009 Vector PlaneNormal;
3010 Vector AngleZero;
3011 Vector OrthogonalVector;
3012 Vector helper;
3013 class BoundaryPointSet *ReferencePoint = NULL;
3014 class BoundaryPointSet *CurrentPoint = NULL;
3015 class BoundaryTriangleSet *triangle = NULL;
3016 class BoundaryLineSet *CurrentLine = NULL;
3017 class BoundaryLineSet *StartLine = NULL;
3018
3019 // find the respective boundary point
[776b64]3020 PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
[065e82]3021 if (PointRunner != PointsOnBoundary.end()) {
3022 ReferencePoint = PointRunner->second;
3023 } else {
[717e0c]3024 eLog() << Verbose(1) << "GetPathOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl;
[065e82]3025 return NULL;
3026 }
3027
[57066a]3028 map <class BoundaryLineSet *, bool> TouchedLine;
3029 map <class BoundaryTriangleSet *, bool> TouchedTriangle;
3030 map <class BoundaryLineSet *, bool>::iterator LineRunner;
3031 map <class BoundaryTriangleSet *, bool>::iterator TriangleRunner;
3032 for (LineMap::iterator Runner = ReferencePoint->lines.begin(); Runner != ReferencePoint->lines.end(); Runner++) {
3033 TouchedLine.insert( pair <class BoundaryLineSet *, bool>(Runner->second, false) );
3034 for (TriangleMap::iterator Sprinter = Runner->second->triangles.begin(); Sprinter != Runner->second->triangles.end(); Sprinter++)
3035 TouchedTriangle.insert( pair <class BoundaryTriangleSet *, bool>(Sprinter->second, false) );
3036 }
[065e82]3037 if (!ReferencePoint->lines.empty()) {
3038 for (LineMap::iterator runner = ReferencePoint->lines.begin(); runner != ReferencePoint->lines.end(); runner++) {
[57066a]3039 LineRunner = TouchedLine.find(runner->second);
3040 if (LineRunner == TouchedLine.end()) {
[717e0c]3041 eLog() << Verbose(1) << "I could not find " << *runner->second << " in the touched list." << endl;
[57066a]3042 } else if (!LineRunner->second) {
3043 LineRunner->second = true;
[065e82]3044 connectedPath = new list<TesselPoint*>;
3045 triangle = NULL;
3046 CurrentLine = runner->second;
3047 StartLine = CurrentLine;
3048 CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint);
[e138de]3049 Log() << Verbose(3)<< "INFO: Beginning path retrieval at " << *CurrentPoint << " of line " << *CurrentLine << "." << endl;
[065e82]3050 do {
3051 // push current one
[e138de]3052 Log() << Verbose(3) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl;
[065e82]3053 connectedPath->push_back(CurrentPoint->node);
3054
3055 // find next triangle
[57066a]3056 for (TriangleMap::iterator Runner = CurrentLine->triangles.begin(); Runner != CurrentLine->triangles.end(); Runner++) {
[e138de]3057 Log() << Verbose(3) << "INFO: Inspecting triangle " << *Runner->second << "." << endl;
[57066a]3058 if ((Runner->second != triangle)) { // look for first triangle not equal to old one
3059 triangle = Runner->second;
3060 TriangleRunner = TouchedTriangle.find(triangle);
3061 if (TriangleRunner != TouchedTriangle.end()) {
3062 if (!TriangleRunner->second) {
3063 TriangleRunner->second = true;
[e138de]3064 Log() << Verbose(3) << "INFO: Connecting triangle is " << *triangle << "." << endl;
[57066a]3065 break;
3066 } else {
[e138de]3067 Log() << Verbose(3) << "INFO: Skipping " << *triangle << ", as we have already visited it." << endl;
[57066a]3068 triangle = NULL;
3069 }
3070 } else {
[717e0c]3071 eLog() << Verbose(1) << "I could not find " << *triangle << " in the touched list." << endl;
[57066a]3072 triangle = NULL;
3073 }
[065e82]3074 }
3075 }
[57066a]3076 if (triangle == NULL)
3077 break;
[065e82]3078 // find next line
3079 for (int i=0;i<3;i++) {
3080 if ((triangle->lines[i] != CurrentLine) && (triangle->lines[i]->ContainsBoundaryPoint(ReferencePoint))) { // not the current line and still containing Point
3081 CurrentLine = triangle->lines[i];
[e138de]3082 Log() << Verbose(3) << "INFO: Connecting line is " << *CurrentLine << "." << endl;
[065e82]3083 break;
3084 }
3085 }
[57066a]3086 LineRunner = TouchedLine.find(CurrentLine);
3087 if (LineRunner == TouchedLine.end())
[717e0c]3088 eLog() << Verbose(1) << "I could not find " << *CurrentLine << " in the touched list." << endl;
[065e82]3089 else
[57066a]3090 LineRunner->second = true;
[065e82]3091 // find next point
3092 CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint);
3093
3094 } while (CurrentLine != StartLine);
3095 // last point is missing, as it's on start line
[e138de]3096 Log() << Verbose(3) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl;
[57066a]3097 if (StartLine->GetOtherEndpoint(ReferencePoint)->node != connectedPath->back())
3098 connectedPath->push_back(StartLine->GetOtherEndpoint(ReferencePoint)->node);
[065e82]3099
3100 ListOfPaths->push_back(connectedPath);
3101 } else {
[e138de]3102 Log() << Verbose(3) << "INFO: Skipping " << *runner->second << ", as we have already visited it." << endl;
[065e82]3103 }
3104 }
3105 } else {
[717e0c]3106 eLog() << Verbose(1) << "There are no lines attached to " << *ReferencePoint << "." << endl;
[065e82]3107 }
3108
3109 return ListOfPaths;
[62bb91]3110}
3111
[065e82]3112/** Gets all closed paths on the circle of points connected to the provided point by triangulation lines, if this very point is removed.
3113 * From GetPathsOfConnectedPoints() extracts all single loops of intracrossing paths in the list of closed paths.
3114 * @param *out output stream for debugging
3115 * @param *Point of which get all connected points
3116 * @return list of the closed paths
3117 */
[e138de]3118list<list<TesselPoint*> *> * Tesselation::GetClosedPathsOfConnectedPoints(const TesselPoint* const Point) const
[065e82]3119{
[e138de]3120 list<list<TesselPoint*> *> *ListofPaths = GetPathsOfConnectedPoints(Point);
[065e82]3121 list<list<TesselPoint*> *> *ListofClosedPaths = new list<list<TesselPoint*> *>;
3122 list<TesselPoint*> *connectedPath = NULL;
3123 list<TesselPoint*> *newPath = NULL;
3124 int count = 0;
3125
3126
3127 list<TesselPoint*>::iterator CircleRunner;
3128 list<TesselPoint*>::iterator CircleStart;
3129
3130 for(list<list<TesselPoint*> *>::iterator ListRunner = ListofPaths->begin(); ListRunner != ListofPaths->end(); ListRunner++) {
3131 connectedPath = *ListRunner;
3132
[e138de]3133 Log() << Verbose(2) << "INFO: Current path is " << connectedPath << "." << endl;
[065e82]3134
3135 // go through list, look for reappearance of starting Point and count
3136 CircleStart = connectedPath->begin();
3137
3138 // go through list, look for reappearance of starting Point and create list
3139 list<TesselPoint*>::iterator Marker = CircleStart;
3140 for (CircleRunner = CircleStart; CircleRunner != connectedPath->end(); CircleRunner++) {
3141 if ((*CircleRunner == *CircleStart) && (CircleRunner != CircleStart)) { // is not the very first point
3142 // we have a closed circle from Marker to new Marker
[e138de]3143 Log() << Verbose(3) << count+1 << ". closed path consists of: ";
[065e82]3144 newPath = new list<TesselPoint*>;
3145 list<TesselPoint*>::iterator CircleSprinter = Marker;
3146 for (; CircleSprinter != CircleRunner; CircleSprinter++) {
3147 newPath->push_back(*CircleSprinter);
[e138de]3148 Log() << Verbose(0) << (**CircleSprinter) << " <-> ";
[065e82]3149 }
[e138de]3150 Log() << Verbose(0) << ".." << endl;
[065e82]3151 count++;
3152 Marker = CircleRunner;
3153
3154 // add to list
3155 ListofClosedPaths->push_back(newPath);
3156 }
3157 }
3158 }
[e138de]3159 Log() << Verbose(3) << "INFO: " << count << " closed additional path(s) have been created." << endl;
[065e82]3160
3161 // delete list of paths
3162 while (!ListofPaths->empty()) {
3163 connectedPath = *(ListofPaths->begin());
3164 ListofPaths->remove(connectedPath);
3165 delete(connectedPath);
3166 }
3167 delete(ListofPaths);
3168
3169 // exit
3170 return ListofClosedPaths;
3171};
3172
3173
3174/** Gets all belonging triangles for a given BoundaryPointSet.
3175 * \param *out output stream for debugging
3176 * \param *Point BoundaryPoint
3177 * \return pointer to allocated list of triangles
3178 */
[e138de]3179set<BoundaryTriangleSet*> *Tesselation::GetAllTriangles(const BoundaryPointSet * const Point) const
[065e82]3180{
3181 set<BoundaryTriangleSet*> *connectedTriangles = new set<BoundaryTriangleSet*>;
3182
3183 if (Point == NULL) {
[717e0c]3184 eLog() << Verbose(1) << "Point given is NULL." << endl;
[065e82]3185 } else {
3186 // go through its lines and insert all triangles
[776b64]3187 for (LineMap::const_iterator LineRunner = Point->lines.begin(); LineRunner != Point->lines.end(); LineRunner++)
[065e82]3188 for (TriangleMap::iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
3189 connectedTriangles->insert(TriangleRunner->second);
3190 }
3191 }
3192
3193 return connectedTriangles;
3194};
3195
3196
[16d866]3197/** Removes a boundary point from the envelope while keeping it closed.
[57066a]3198 * We remove the old triangles connected to the point and re-create new triangles to close the surface following this ansatz:
3199 * -# a closed path(s) of boundary points surrounding the point to be removed is constructed
3200 * -# on each closed path, we pick three adjacent points, create a triangle with them and subtract the middle point from the path
3201 * -# we advance two points (i.e. the next triangle will start at the ending point of the last triangle) and continue as before
3202 * -# the surface is closed, when the path is empty
3203 * Thereby, we (hopefully) make sure that the removed points remains beneath the surface (this is checked via IsInnerPoint eventually).
[16d866]3204 * \param *out output stream for debugging
3205 * \param *point point to be removed
3206 * \return volume added to the volume inside the tesselated surface by the removal
3207 */
[e138de]3208double Tesselation::RemovePointFromTesselatedSurface(class BoundaryPointSet *point) {
[16d866]3209 class BoundaryLineSet *line = NULL;
3210 class BoundaryTriangleSet *triangle = NULL;
[57066a]3211 Vector OldPoint, NormalVector;
[16d866]3212 double volume = 0;
3213 int count = 0;
3214
[1d9b7aa]3215 if (point == NULL) {
[717e0c]3216 eLog() << Verbose(1) << "Cannot remove the point " << point << ", it's NULL!" << endl;
[1d9b7aa]3217 return 0.;
3218 } else
[e138de]3219 Log() << Verbose(2) << "Removing point " << *point << " from tesselated boundary ..." << endl;
[1d9b7aa]3220
[16d866]3221 // copy old location for the volume
3222 OldPoint.CopyVector(point->node->node);
3223
3224 // get list of connected points
3225 if (point->lines.empty()) {
[717e0c]3226 eLog() << Verbose(1) << "Cannot remove the point " << *point << ", it's connected to no lines!" << endl;
[16d866]3227 return 0.;
3228 }
3229
[e138de]3230 list<list<TesselPoint*> *> *ListOfClosedPaths = GetClosedPathsOfConnectedPoints(point->node);
[065e82]3231 list<TesselPoint*> *connectedPath = NULL;
3232
3233 // gather all triangles
[16d866]3234 for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++)
3235 count+=LineRunner->second->triangles.size();
[065e82]3236 map<class BoundaryTriangleSet *, int> Candidates;
[57066a]3237 for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
[16d866]3238 line = LineRunner->second;
3239 for (TriangleMap::iterator TriangleRunner = line->triangles.begin(); TriangleRunner != line->triangles.end(); TriangleRunner++) {
3240 triangle = TriangleRunner->second;
[065e82]3241 Candidates.insert( pair<class BoundaryTriangleSet *, int> (triangle, triangle->Nr) );
[16d866]3242 }
3243 }
3244
[065e82]3245 // remove all triangles
3246 count=0;
[57066a]3247 NormalVector.Zero();
[065e82]3248 for (map<class BoundaryTriangleSet *, int>::iterator Runner = Candidates.begin(); Runner != Candidates.end(); Runner++) {
[e138de]3249 Log() << Verbose(3) << "INFO: Removing triangle " << *(Runner->first) << "." << endl;
[57066a]3250 NormalVector.SubtractVector(&Runner->first->NormalVector); // has to point inward
[065e82]3251 RemoveTesselationTriangle(Runner->first);
3252 count++;
3253 }
[e138de]3254 Log() << Verbose(1) << count << " triangles were removed." << endl;
[065e82]3255
3256 list<list<TesselPoint*> *>::iterator ListAdvance = ListOfClosedPaths->begin();
3257 list<list<TesselPoint*> *>::iterator ListRunner = ListAdvance;
3258 map<class BoundaryTriangleSet *, int>::iterator NumberRunner = Candidates.begin();
[57066a]3259 list<TesselPoint*>::iterator StartNode, MiddleNode, EndNode;
3260 double angle;
3261 double smallestangle;
3262 Vector Point, Reference, OrthogonalVector;
[065e82]3263 if (count > 2) { // less than three triangles, then nothing will be created
3264 class TesselPoint *TriangleCandidates[3];
3265 count = 0;
3266 for ( ; ListRunner != ListOfClosedPaths->end(); ListRunner = ListAdvance) { // go through all closed paths
3267 if (ListAdvance != ListOfClosedPaths->end())
3268 ListAdvance++;
3269
3270 connectedPath = *ListRunner;
3271
3272 // re-create all triangles by going through connected points list
[57066a]3273 list<class BoundaryLineSet *> NewLines;
3274 for (;!connectedPath->empty();) {
3275 // search middle node with widest angle to next neighbours
3276 EndNode = connectedPath->end();
3277 smallestangle = 0.;
3278 for (MiddleNode = connectedPath->begin(); MiddleNode != connectedPath->end(); MiddleNode++) {
[e138de]3279 Log() << Verbose(3) << "INFO: MiddleNode is " << **MiddleNode << "." << endl;
[57066a]3280 // construct vectors to next and previous neighbour
3281 StartNode = MiddleNode;
3282 if (StartNode == connectedPath->begin())
3283 StartNode = connectedPath->end();
3284 StartNode--;
[e138de]3285 //Log() << Verbose(3) << "INFO: StartNode is " << **StartNode << "." << endl;
[57066a]3286 Point.CopyVector((*StartNode)->node);
3287 Point.SubtractVector((*MiddleNode)->node);
3288 StartNode = MiddleNode;
3289 StartNode++;
3290 if (StartNode == connectedPath->end())
3291 StartNode = connectedPath->begin();
[e138de]3292 //Log() << Verbose(3) << "INFO: EndNode is " << **StartNode << "." << endl;
[57066a]3293 Reference.CopyVector((*StartNode)->node);
3294 Reference.SubtractVector((*MiddleNode)->node);
3295 OrthogonalVector.CopyVector((*MiddleNode)->node);
3296 OrthogonalVector.SubtractVector(&OldPoint);
3297 OrthogonalVector.MakeNormalVector(&Reference);
3298 angle = GetAngle(Point, Reference, OrthogonalVector);
3299 //if (angle < M_PI) // no wrong-sided triangles, please?
3300 if(fabs(angle - M_PI) < fabs(smallestangle - M_PI)) { // get straightest angle (i.e. construct those triangles with smallest area first)
3301 smallestangle = angle;
3302 EndNode = MiddleNode;
3303 }
3304 }
3305 MiddleNode = EndNode;
3306 if (MiddleNode == connectedPath->end()) {
[e138de]3307 Log() << Verbose(1) << "CRITICAL: Could not find a smallest angle!" << endl;
[57066a]3308 exit(255);
3309 }
3310 StartNode = MiddleNode;
3311 if (StartNode == connectedPath->begin())
3312 StartNode = connectedPath->end();
3313 StartNode--;
3314 EndNode++;
3315 if (EndNode == connectedPath->end())
3316 EndNode = connectedPath->begin();
[e138de]3317 Log() << Verbose(4) << "INFO: StartNode is " << **StartNode << "." << endl;
3318 Log() << Verbose(4) << "INFO: MiddleNode is " << **MiddleNode << "." << endl;
3319 Log() << Verbose(4) << "INFO: EndNode is " << **EndNode << "." << endl;
3320 Log() << Verbose(3) << "INFO: Attempting to create triangle " << (*StartNode)->Name << ", " << (*MiddleNode)->Name << " and " << (*EndNode)->Name << "." << endl;
[57066a]3321 TriangleCandidates[0] = *StartNode;
3322 TriangleCandidates[1] = *MiddleNode;
3323 TriangleCandidates[2] = *EndNode;
[e138de]3324 triangle = GetPresentTriangle(TriangleCandidates);
[57066a]3325 if (triangle != NULL) {
[717e0c]3326 eLog() << Verbose(2) << "New triangle already present, skipping!" << endl;
[57066a]3327 StartNode++;
3328 MiddleNode++;
3329 EndNode++;
3330 if (StartNode == connectedPath->end())
3331 StartNode = connectedPath->begin();
3332 if (MiddleNode == connectedPath->end())
3333 MiddleNode = connectedPath->begin();
3334 if (EndNode == connectedPath->end())
3335 EndNode = connectedPath->begin();
3336 continue;
3337 }
[e138de]3338 Log() << Verbose(5) << "Adding new triangle points."<< endl;
[57066a]3339 AddTesselationPoint(*StartNode, 0);
3340 AddTesselationPoint(*MiddleNode, 1);
3341 AddTesselationPoint(*EndNode, 2);
[e138de]3342 Log() << Verbose(5) << "Adding new triangle lines."<< endl;
[065e82]3343 AddTesselationLine(TPS[0], TPS[1], 0);
3344 AddTesselationLine(TPS[0], TPS[2], 1);
[57066a]3345 NewLines.push_back(BLS[1]);
[065e82]3346 AddTesselationLine(TPS[1], TPS[2], 2);
3347 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
[57066a]3348 BTS->GetNormalVector(NormalVector);
[065e82]3349 AddTesselationTriangle();
3350 // calculate volume summand as a general tetraeder
[c0f6c6]3351 volume += CalculateVolumeofGeneralTetraeder(*TPS[0]->node->node, *TPS[1]->node->node, *TPS[2]->node->node, OldPoint);
[065e82]3352 // advance number
3353 count++;
[57066a]3354
3355 // prepare nodes for next triangle
3356 StartNode = EndNode;
[e138de]3357 Log() << Verbose(4) << "Removing " << **MiddleNode << " from closed path, remaining points: " << connectedPath->size() << "." << endl;
[57066a]3358 connectedPath->remove(*MiddleNode); // remove the middle node (it is surrounded by triangles)
3359 if (connectedPath->size() == 2) { // we are done
3360 connectedPath->remove(*StartNode); // remove the start node
3361 connectedPath->remove(*EndNode); // remove the end node
3362 break;
3363 } else if (connectedPath->size() < 2) { // something's gone wrong!
[e138de]3364 Log() << Verbose(1) << "CRITICAL: There are only two endpoints left!" << endl;
[57066a]3365 exit(255);
3366 } else {
3367 MiddleNode = StartNode;
3368 MiddleNode++;
3369 if (MiddleNode == connectedPath->end())
3370 MiddleNode = connectedPath->begin();
3371 EndNode = MiddleNode;
3372 EndNode++;
3373 if (EndNode == connectedPath->end())
3374 EndNode = connectedPath->begin();
3375 }
[065e82]3376 }
[57066a]3377 // maximize the inner lines (we preferentially created lines with a huge angle, which is for the tesselation not wanted though useful for the closing)
3378 if (NewLines.size() > 1) {
3379 list<class BoundaryLineSet *>::iterator Candidate;
3380 class BoundaryLineSet *OtherBase = NULL;
3381 double tmp, maxgain;
3382 do {
3383 maxgain = 0;
3384 for(list<class BoundaryLineSet *>::iterator Runner = NewLines.begin(); Runner != NewLines.end(); Runner++) {
[e138de]3385 tmp = PickFarthestofTwoBaselines(*Runner);
[57066a]3386 if (maxgain < tmp) {
3387 maxgain = tmp;
3388 Candidate = Runner;
3389 }
3390 }
3391 if (maxgain != 0) {
3392 volume += maxgain;
[e138de]3393 Log() << Verbose(3) << "Flipping baseline with highest volume" << **Candidate << "." << endl;
3394 OtherBase = FlipBaseline(*Candidate);
[57066a]3395 NewLines.erase(Candidate);
3396 NewLines.push_back(OtherBase);
3397 }
3398 } while (maxgain != 0.);
3399 }
3400
[065e82]3401 ListOfClosedPaths->remove(connectedPath);
3402 delete(connectedPath);
[16d866]3403 }
[e138de]3404 Log() << Verbose(1) << count << " triangles were created." << endl;
[065e82]3405 } else {
3406 while (!ListOfClosedPaths->empty()) {
3407 ListRunner = ListOfClosedPaths->begin();
3408 connectedPath = *ListRunner;
3409 ListOfClosedPaths->remove(connectedPath);
3410 delete(connectedPath);
3411 }
[e138de]3412 Log() << Verbose(1) << "No need to create any triangles." << endl;
[16d866]3413 }
[065e82]3414 delete(ListOfClosedPaths);
[16d866]3415
[e138de]3416 Log() << Verbose(1) << "Removed volume is " << volume << "." << endl;
[357fba]3417
[57066a]3418 return volume;
[357fba]3419};
[ab1932]3420
[5c7bf8]3421
[ab1932]3422
3423/**
[62bb91]3424 * Finds triangles belonging to the three provided points.
[ab1932]3425 *
[62bb91]3426 * @param *Points[3] list, is expected to contain three points
[ab1932]3427 *
[62bb91]3428 * @return triangles which belong to the provided points, will be empty if there are none,
[ab1932]3429 * will usually be one, in case of degeneration, there will be two
3430 */
[776b64]3431list<BoundaryTriangleSet*> *Tesselation::FindTriangles(const TesselPoint* const Points[3]) const
[ab1932]3432{
3433 list<BoundaryTriangleSet*> *result = new list<BoundaryTriangleSet*>;
[776b64]3434 LineMap::const_iterator FindLine;
3435 TriangleMap::const_iterator FindTriangle;
[ab1932]3436 class BoundaryPointSet *TrianglePoints[3];
3437
3438 for (int i = 0; i < 3; i++) {
[776b64]3439 PointMap::const_iterator FindPoint = PointsOnBoundary.find(Points[i]->nr);
[ab1932]3440 if (FindPoint != PointsOnBoundary.end()) {
3441 TrianglePoints[i] = FindPoint->second;
3442 } else {
3443 TrianglePoints[i] = NULL;
3444 }
3445 }
3446
3447 // checks lines between the points in the Points for their adjacent triangles
3448 for (int i = 0; i < 3; i++) {
3449 if (TrianglePoints[i] != NULL) {
[a2028e]3450 for (int j = i+1; j < 3; j++) {
[ab1932]3451 if (TrianglePoints[j] != NULL) {
[a2028e]3452 for (FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr); // is a multimap!
3453 (FindLine != TrianglePoints[i]->lines.end()) && (FindLine->first == TrianglePoints[j]->node->nr);
3454 FindLine++) {
3455 for (FindTriangle = FindLine->second->triangles.begin();
3456 FindTriangle != FindLine->second->triangles.end();
3457 FindTriangle++) {
3458 if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
3459 result->push_back(FindTriangle->second);
[ab1932]3460 }
3461 }
3462 }
[a2028e]3463 // Is it sufficient to consider one of the triangle lines for this.
3464 return result;
[ab1932]3465 }
3466 }
3467 }
3468 }
3469
3470 return result;
3471}
3472
[7c14ec]3473/**
[57066a]3474 * Finds all degenerated lines within the tesselation structure.
[7c14ec]3475 *
[57066a]3476 * @return map of keys of degenerated line pairs, each line occurs twice
[7c14ec]3477 * in the list, once as key and once as value
3478 */
[57066a]3479map<int, int> * Tesselation::FindAllDegeneratedLines()
[7c14ec]3480{
[57066a]3481 map<int, class BoundaryLineSet *> AllLines;
3482 map<int, int> * DegeneratedLines = new map<int, int>;
[7c14ec]3483
3484 // sanity check
3485 if (LinesOnBoundary.empty()) {
[e138de]3486 Log() << Verbose(1) << "Warning: FindAllDegeneratedTriangles() was called without any tesselation structure.";
[57066a]3487 return DegeneratedLines;
[7c14ec]3488 }
3489
[57066a]3490 LineMap::iterator LineRunner1;
3491 pair<LineMap::iterator, bool> tester;
[7c14ec]3492 for (LineRunner1 = LinesOnBoundary.begin(); LineRunner1 != LinesOnBoundary.end(); ++LineRunner1) {
[57066a]3493 tester = AllLines.insert( pair<int,BoundaryLineSet *> (LineRunner1->second->endpoints[0]->Nr, LineRunner1->second) );
3494 if ((!tester.second) && (tester.first->second->endpoints[1]->Nr == LineRunner1->second->endpoints[1]->Nr)) { // found degenerated line
3495 DegeneratedLines->insert ( pair<int, int> (LineRunner1->second->Nr, tester.first->second->Nr) );
3496 DegeneratedLines->insert ( pair<int, int> (tester.first->second->Nr, LineRunner1->second->Nr) );
3497 }
3498 }
3499
3500 AllLines.clear();
3501
[e138de]3502 Log() << Verbose(1) << "FindAllDegeneratedLines() found " << DegeneratedLines->size() << " lines." << endl;
[57066a]3503 map<int,int>::iterator it;
3504 for (it = DegeneratedLines->begin(); it != DegeneratedLines->end(); it++)
[e138de]3505 Log() << Verbose(2) << (*it).first << " => " << (*it).second << endl;
[57066a]3506
3507 return DegeneratedLines;
3508}
3509
3510/**
3511 * Finds all degenerated triangles within the tesselation structure.
3512 *
3513 * @return map of keys of degenerated triangle pairs, each triangle occurs twice
3514 * in the list, once as key and once as value
3515 */
3516map<int, int> * Tesselation::FindAllDegeneratedTriangles()
3517{
3518 map<int, int> * DegeneratedLines = FindAllDegeneratedLines();
3519 map<int, int> * DegeneratedTriangles = new map<int, int>;
3520
3521 TriangleMap::iterator TriangleRunner1, TriangleRunner2;
3522 LineMap::iterator Liner;
3523 class BoundaryLineSet *line1 = NULL, *line2 = NULL;
3524
3525 for (map<int, int>::iterator LineRunner = DegeneratedLines->begin(); LineRunner != DegeneratedLines->end(); ++LineRunner) {
3526 // run over both lines' triangles
3527 Liner = LinesOnBoundary.find(LineRunner->first);
3528 if (Liner != LinesOnBoundary.end())
3529 line1 = Liner->second;
3530 Liner = LinesOnBoundary.find(LineRunner->second);
3531 if (Liner != LinesOnBoundary.end())
3532 line2 = Liner->second;
3533 for (TriangleRunner1 = line1->triangles.begin(); TriangleRunner1 != line1->triangles.end(); ++TriangleRunner1) {
3534 for (TriangleRunner2 = line2->triangles.begin(); TriangleRunner2 != line2->triangles.end(); ++TriangleRunner2) {
3535 if ((TriangleRunner1->second != TriangleRunner2->second)
3536 && (TriangleRunner1->second->IsPresentTupel(TriangleRunner2->second))) {
3537 DegeneratedTriangles->insert( pair<int, int> (TriangleRunner1->second->Nr, TriangleRunner2->second->Nr) );
3538 DegeneratedTriangles->insert( pair<int, int> (TriangleRunner2->second->Nr, TriangleRunner1->second->Nr) );
[7c14ec]3539 }
3540 }
3541 }
3542 }
[57066a]3543 delete(DegeneratedLines);
[7c14ec]3544
[e138de]3545 Log() << Verbose(1) << "FindAllDegeneratedTriangles() found " << DegeneratedTriangles->size() << " triangles:" << endl;
[7c14ec]3546 map<int,int>::iterator it;
[57066a]3547 for (it = DegeneratedTriangles->begin(); it != DegeneratedTriangles->end(); it++)
[e138de]3548 Log() << Verbose(2) << (*it).first << " => " << (*it).second << endl;
[7c14ec]3549
3550 return DegeneratedTriangles;
3551}
3552
3553/**
3554 * Purges degenerated triangles from the tesselation structure if they are not
3555 * necessary to keep a single point within the structure.
3556 */
3557void Tesselation::RemoveDegeneratedTriangles()
3558{
[57066a]3559 map<int, int> * DegeneratedTriangles = FindAllDegeneratedTriangles();
3560 TriangleMap::iterator finder;
3561 BoundaryTriangleSet *triangle = NULL, *partnerTriangle = NULL;
3562 int count = 0;
[7c14ec]3563
[e138de]3564 Log() << Verbose(1) << "Begin of RemoveDegeneratedTriangles" << endl;
[7dea7c]3565
[57066a]3566 for (map<int, int>::iterator TriangleKeyRunner = DegeneratedTriangles->begin();
3567 TriangleKeyRunner != DegeneratedTriangles->end(); ++TriangleKeyRunner
[7c14ec]3568 ) {
[57066a]3569 finder = TrianglesOnBoundary.find(TriangleKeyRunner->first);
3570 if (finder != TrianglesOnBoundary.end())
3571 triangle = finder->second;
3572 else
3573 break;
3574 finder = TrianglesOnBoundary.find(TriangleKeyRunner->second);
3575 if (finder != TrianglesOnBoundary.end())
3576 partnerTriangle = finder->second;
3577 else
3578 break;
[7c14ec]3579
3580 bool trianglesShareLine = false;
3581 for (int i = 0; i < 3; ++i)
3582 for (int j = 0; j < 3; ++j)
3583 trianglesShareLine = trianglesShareLine || triangle->lines[i] == partnerTriangle->lines[j];
3584
3585 if (trianglesShareLine
3586 && (triangle->endpoints[1]->LinesCount > 2)
3587 && (triangle->endpoints[2]->LinesCount > 2)
3588 && (triangle->endpoints[0]->LinesCount > 2)
3589 ) {
[57066a]3590 // check whether we have to fix lines
3591 BoundaryTriangleSet *Othertriangle = NULL;
3592 BoundaryTriangleSet *OtherpartnerTriangle = NULL;
3593 TriangleMap::iterator TriangleRunner;
3594 for (int i = 0; i < 3; ++i)
3595 for (int j = 0; j < 3; ++j)
3596 if (triangle->lines[i] != partnerTriangle->lines[j]) {
3597 // get the other two triangles
3598 for (TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); ++TriangleRunner)
3599 if (TriangleRunner->second != triangle) {
3600 Othertriangle = TriangleRunner->second;
3601 }
3602 for (TriangleRunner = partnerTriangle->lines[i]->triangles.begin(); TriangleRunner != partnerTriangle->lines[i]->triangles.end(); ++TriangleRunner)
3603 if (TriangleRunner->second != partnerTriangle) {
3604 OtherpartnerTriangle = TriangleRunner->second;
3605 }
3606 /// interchanges their lines so that triangle->lines[i] == partnerTriangle->lines[j]
3607 // the line of triangle receives the degenerated ones
3608 triangle->lines[i]->triangles.erase(Othertriangle->Nr);
3609 triangle->lines[i]->triangles.insert( TrianglePair( partnerTriangle->Nr, partnerTriangle) );
3610 for (int k=0;k<3;k++)
3611 if (triangle->lines[i] == Othertriangle->lines[k]) {
3612 Othertriangle->lines[k] = partnerTriangle->lines[j];
3613 break;
3614 }
3615 // the line of partnerTriangle receives the non-degenerated ones
3616 partnerTriangle->lines[j]->triangles.erase( partnerTriangle->Nr);
3617 partnerTriangle->lines[j]->triangles.insert( TrianglePair( Othertriangle->Nr, Othertriangle) );
3618 partnerTriangle->lines[j] = triangle->lines[i];
3619 }
3620
3621 // erase the pair
3622 count += (int) DegeneratedTriangles->erase(triangle->Nr);
[e138de]3623 Log() << Verbose(1) << "RemoveDegeneratedTriangles() removes triangle " << *triangle << "." << endl;
[7c14ec]3624 RemoveTesselationTriangle(triangle);
[57066a]3625 count += (int) DegeneratedTriangles->erase(partnerTriangle->Nr);
[e138de]3626 Log() << Verbose(1) << "RemoveDegeneratedTriangles() removes triangle " << *partnerTriangle << "." << endl;
[7c14ec]3627 RemoveTesselationTriangle(partnerTriangle);
3628 } else {
[e138de]3629 Log() << Verbose(1) << "RemoveDegeneratedTriangles() does not remove triangle " << *triangle
[7c14ec]3630 << " and its partner " << *partnerTriangle << " because it is essential for at"
3631 << " least one of the endpoints to be kept in the tesselation structure." << endl;
3632 }
3633 }
[57066a]3634 delete(DegeneratedTriangles);
[6a7f78c]3635 if (count > 0)
3636 LastTriangle = NULL;
[57066a]3637
[e138de]3638 Log() << Verbose(1) << "RemoveDegeneratedTriangles() removed " << count << " triangles:" << endl;
3639 Log() << Verbose(1) << "End of RemoveDegeneratedTriangles" << endl;
[7c14ec]3640}
3641
[57066a]3642/** Adds an outside Tesselpoint to the envelope via (two) degenerated triangles.
3643 * We look for the closest point on the boundary, we look through its connected boundary lines and
3644 * seek the one with the minimum angle between its center point and the new point and this base line.
3645 * We open up the line by adding a degenerated triangle, whose other side closes the base line again.
3646 * \param *out output stream for debugging
3647 * \param *point point to add
3648 * \param *LC Linked Cell structure to find nearest point
[ab1932]3649 */
[e138de]3650void Tesselation::AddBoundaryPointByDegeneratedTriangle(class TesselPoint *point, LinkedCell *LC)
[ab1932]3651{
[e138de]3652 Log() << Verbose(2) << "Begin of AddBoundaryPointByDegeneratedTriangle" << endl;
[ab1932]3653
[57066a]3654 // find nearest boundary point
3655 class TesselPoint *BackupPoint = NULL;
3656 class TesselPoint *NearestPoint = FindClosestPoint(point->node, BackupPoint, LC);
3657 class BoundaryPointSet *NearestBoundaryPoint = NULL;
3658 PointMap::iterator PointRunner;
3659
3660 if (NearestPoint == point)
3661 NearestPoint = BackupPoint;
3662 PointRunner = PointsOnBoundary.find(NearestPoint->nr);
3663 if (PointRunner != PointsOnBoundary.end()) {
3664 NearestBoundaryPoint = PointRunner->second;
3665 } else {
[717e0c]3666 eLog() << Verbose(1) << "I cannot find the boundary point." << endl;
[57066a]3667 return;
3668 }
[e138de]3669 Log() << Verbose(2) << "Nearest point on boundary is " << NearestPoint->Name << "." << endl;
[57066a]3670
3671 // go through its lines and find the best one to split
3672 Vector CenterToPoint;
3673 Vector BaseLine;
3674 double angle, BestAngle = 0.;
3675 class BoundaryLineSet *BestLine = NULL;
3676 for (LineMap::iterator Runner = NearestBoundaryPoint->lines.begin(); Runner != NearestBoundaryPoint->lines.end(); Runner++) {
3677 BaseLine.CopyVector(Runner->second->endpoints[0]->node->node);
3678 BaseLine.SubtractVector(Runner->second->endpoints[1]->node->node);
3679 CenterToPoint.CopyVector(Runner->second->endpoints[0]->node->node);
3680 CenterToPoint.AddVector(Runner->second->endpoints[1]->node->node);
3681 CenterToPoint.Scale(0.5);
3682 CenterToPoint.SubtractVector(point->node);
3683 angle = CenterToPoint.Angle(&BaseLine);
3684 if (fabs(angle - M_PI/2.) < fabs(BestAngle - M_PI/2.)) {
3685 BestAngle = angle;
3686 BestLine = Runner->second;
3687 }
[ab1932]3688 }
3689
[57066a]3690 // remove one triangle from the chosen line
3691 class BoundaryTriangleSet *TempTriangle = (BestLine->triangles.begin())->second;
3692 BestLine->triangles.erase(TempTriangle->Nr);
3693 int nr = -1;
3694 for (int i=0;i<3; i++) {
3695 if (TempTriangle->lines[i] == BestLine) {
3696 nr = i;
3697 break;
3698 }
3699 }
[ab1932]3700
[57066a]3701 // create new triangle to connect point (connects automatically with the missing spot of the chosen line)
[e138de]3702 Log() << Verbose(5) << "Adding new triangle points."<< endl;
[57066a]3703 AddTesselationPoint((BestLine->endpoints[0]->node), 0);
3704 AddTesselationPoint((BestLine->endpoints[1]->node), 1);
3705 AddTesselationPoint(point, 2);
[e138de]3706 Log() << Verbose(5) << "Adding new triangle lines."<< endl;
[57066a]3707 AddTesselationLine(TPS[0], TPS[1], 0);
3708 AddTesselationLine(TPS[0], TPS[2], 1);
3709 AddTesselationLine(TPS[1], TPS[2], 2);
3710 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
3711 BTS->GetNormalVector(TempTriangle->NormalVector);
3712 BTS->NormalVector.Scale(-1.);
[e138de]3713 Log() << Verbose(3) << "INFO: NormalVector of new triangle is " << BTS->NormalVector << "." << endl;
[57066a]3714 AddTesselationTriangle();
3715
3716 // create other side of this triangle and close both new sides of the first created triangle
[e138de]3717 Log() << Verbose(5) << "Adding new triangle points."<< endl;
[57066a]3718 AddTesselationPoint((BestLine->endpoints[0]->node), 0);
3719 AddTesselationPoint((BestLine->endpoints[1]->node), 1);
3720 AddTesselationPoint(point, 2);
[e138de]3721 Log() << Verbose(5) << "Adding new triangle lines."<< endl;
[57066a]3722 AddTesselationLine(TPS[0], TPS[1], 0);
3723 AddTesselationLine(TPS[0], TPS[2], 1);
3724 AddTesselationLine(TPS[1], TPS[2], 2);
3725 BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
3726 BTS->GetNormalVector(TempTriangle->NormalVector);
[e138de]3727 Log() << Verbose(3) << "INFO: NormalVector of other new triangle is " << BTS->NormalVector << "." << endl;
[57066a]3728 AddTesselationTriangle();
3729
3730 // add removed triangle to the last open line of the second triangle
3731 for (int i=0;i<3;i++) { // look for the same line as BestLine (only it's its degenerated companion)
3732 if ((BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[0])) && (BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[1]))) {
3733 if (BestLine == BTS->lines[i]){
[e138de]3734 Log() << Verbose(1) << "CRITICAL: BestLine is same as found line, something's wrong here!" << endl;
[57066a]3735 exit(255);
3736 }
3737 BTS->lines[i]->triangles.insert( pair<int, class BoundaryTriangleSet *> (TempTriangle->Nr, TempTriangle) );
3738 TempTriangle->lines[nr] = BTS->lines[i];
3739 break;
3740 }
3741 }
[ab1932]3742
[57066a]3743 // exit
[e138de]3744 Log() << Verbose(2) << "End of AddBoundaryPointByDegeneratedTriangle" << endl;
[57066a]3745};
3746
3747/** Writes the envelope to file.
3748 * \param *out otuput stream for debugging
3749 * \param *filename basename of output file
3750 * \param *cloud PointCloud structure with all nodes
3751 */
[e138de]3752void Tesselation::Output(const char *filename, const PointCloud * const cloud)
[57066a]3753{
3754 ofstream *tempstream = NULL;
3755 string NameofTempFile;
3756 char NumberName[255];
3757
3758 if (LastTriangle != NULL) {
3759 sprintf(NumberName, "-%04d-%s_%s_%s", (int)TrianglesOnBoundary.size(), LastTriangle->endpoints[0]->node->Name, LastTriangle->endpoints[1]->node->Name, LastTriangle->endpoints[2]->node->Name);
3760 if (DoTecplotOutput) {
3761 string NameofTempFile(filename);
3762 NameofTempFile.append(NumberName);
3763 for(size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
3764 NameofTempFile.erase(npos, 1);
3765 NameofTempFile.append(TecplotSuffix);
[e138de]3766 Log() << Verbose(1) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n";
[57066a]3767 tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
[e138de]3768 WriteTecplotFile(tempstream, this, cloud, TriangleFilesWritten);
[57066a]3769 tempstream->close();
3770 tempstream->flush();
3771 delete(tempstream);
3772 }
3773
3774 if (DoRaster3DOutput) {
3775 string NameofTempFile(filename);
3776 NameofTempFile.append(NumberName);
3777 for(size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
3778 NameofTempFile.erase(npos, 1);
3779 NameofTempFile.append(Raster3DSuffix);
[e138de]3780 Log() << Verbose(1) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n";
[57066a]3781 tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
[e138de]3782 WriteRaster3dFile(tempstream, this, cloud);
3783 IncludeSphereinRaster3D(tempstream, this, cloud);
[57066a]3784 tempstream->close();
3785 tempstream->flush();
3786 delete(tempstream);
3787 }
3788 }
3789 if (DoTecplotOutput || DoRaster3DOutput)
3790 TriangleFilesWritten++;
3791};
Note: See TracBrowser for help on using the repository browser.