source: src/tesselation.cpp@ 84c494

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

Replaced several old-style asserts with more usable ASSERTs

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