source: src/tesselation.cpp@ 2c8934

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

Merge branch 'StructureRefactoring' into stable

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/periodentafel.cpp

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