source: src/tesselation.cpp@ b0a5f1

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

BUGFIX of Tesselation::AddTesselationLine() - comparing Vector against Vector is numerically unstable.

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