source: src/Tesselation/tesselation.cpp@ 98c428

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 98c428 was 0aa122, checked in by Frederik Heber <heber@…>, 13 years ago

Updated all source files's copyright note to current year 2012.

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