source: src/tesselation.cpp@ 215df0

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 215df0 was 215df0, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Simplified some methods.

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