source: src/tesselation.cpp@ 6625c3

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

CodePatterns places all includes now in subfolder CodePatterns/.

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