source: src/tesselation.cpp@ fff733

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 fff733 was fff733, checked in by Frederik Heber <heber@…>, 14 years ago

Fixed PointCloud implementation in class Tesselation.

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