source: src/tesselation.cpp@ 1ac51b

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

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

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