source: src/tesselation.cpp@ 26f75a

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

FIX: Memory corruption in particleInfo class

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