source: src/tesselation.cpp@ ce3d2b

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 ce3d2b was 36166d, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Removed left over parts from old memory-tracker

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