source: src/tesselation.cpp@ 93dfc7b

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

Removed the DistanceToPlane method in the Vector class in favor of a method that computes the distance to arbitrary spaces

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