source: src/tesselation.cpp@ d3dff06

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 d3dff06 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

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