source: src/tesselation.cpp@ 190326

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 190326 was 27ac00, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made the line-plane intersection method take a line instead of two vectors

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