source: src/tesselation.cpp@ e0aee2b

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

Merge branch 'MoleculeStartEndSwitch' into StructureRefactoring

Conflicts:

molecuilder/src/Helpers/Assert.cpp
molecuilder/src/Helpers/Assert.hpp
molecuilder/src/Legacy/oldmenu.cpp
molecuilder/src/Makefile.am
molecuilder/src/Patterns/Cacheable.hpp
molecuilder/src/Patterns/Observer.cpp
molecuilder/src/Patterns/Observer.hpp
molecuilder/src/analysis_correlation.cpp
molecuilder/src/boundary.cpp
molecuilder/src/builder.cpp
molecuilder/src/config.cpp
molecuilder/src/helpers.hpp
molecuilder/src/molecule.cpp
molecuilder/src/molecule.hpp
molecuilder/src/molecule_dynamics.cpp
molecuilder/src/molecule_fragmentation.cpp
molecuilder/src/molecule_geometry.cpp
molecuilder/src/molecule_graph.cpp
molecuilder/src/moleculelist.cpp
molecuilder/src/tesselation.cpp
molecuilder/src/unittests/AnalysisCorrelationToSurfaceUnitTest.cpp
molecuilder/src/unittests/ObserverTest.cpp
molecuilder/src/unittests/ObserverTest.hpp

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