Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tesselation.cpp

    rb1a6d8 r27bd2f  
    77
    88#include <fstream>
    9 #include <assert.h>
    109
    1110#include "helpers.hpp"
     
    1514#include "tesselation.hpp"
    1615#include "tesselationhelpers.hpp"
    17 #include "triangleintersectionlist.hpp"
    1816#include "vector.hpp"
    1917#include "verbose.hpp"
     
    2624 */
    2725BoundaryPointSet::BoundaryPointSet() :
    28   LinesCount(0), value(0.), Nr(-1)
    29 {
    30   Info FunctionInfo(__func__);
    31   DoLog(1) && (Log() << Verbose(1) << "Adding noname." << endl);
    32 }
    33 ;
     26    LinesCount(0),
     27    value(0.),
     28    Nr(-1)
     29{
     30        Info FunctionInfo(__func__);
     31        Log() << Verbose(1) << "Adding noname." << endl;
     32};
    3433
    3534/** Constructor of BoundaryPointSet with Tesselpoint.
    3635 * \param *Walker TesselPoint this boundary point represents
    3736 */
    38 BoundaryPointSet::BoundaryPointSet(TesselPoint * const Walker) :
    39   LinesCount(0), node(Walker), value(0.), Nr(Walker->nr)
    40 {
    41   Info FunctionInfo(__func__);
    42   DoLog(1) && (Log() << Verbose(1) << "Adding Node " << *Walker << endl);
    43 }
    44 ;
     37BoundaryPointSet::BoundaryPointSet(TesselPoint * Walker) :
     38  LinesCount(0),
     39  node(Walker),
     40  value(0.),
     41  Nr(Walker->nr)
     42{
     43        Info FunctionInfo(__func__);
     44  Log() << Verbose(1) << "Adding Node " << *Walker << endl;
     45};
    4546
    4647/** Destructor of BoundaryPointSet.
     
    5051BoundaryPointSet::~BoundaryPointSet()
    5152{
    52   Info FunctionInfo(__func__);
     53        Info FunctionInfo(__func__);
    5354  //Log() << Verbose(0) << "Erasing point nr. " << Nr << "." << endl;
    5455  if (!lines.empty())
    55     DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some lines." << endl);
     56    eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some lines." << endl;
    5657  node = NULL;
    57 }
    58 ;
     58};
    5959
    6060/** Add a line to the LineMap of this point.
    6161 * \param *line line to add
    6262 */
    63 void BoundaryPointSet::AddLine(BoundaryLineSet * const line)
    64 {
    65   Info FunctionInfo(__func__);
    66   DoLog(1) && (Log() << Verbose(1) << "Adding " << *this << " to line " << *line << "." << endl);
    67   if (line->endpoints[0] == this) {
    68     lines.insert(LinePair(line->endpoints[1]->Nr, line));
    69   } else {
    70     lines.insert(LinePair(line->endpoints[0]->Nr, line));
    71   }
     63void BoundaryPointSet::AddLine(class BoundaryLineSet *line)
     64{
     65        Info FunctionInfo(__func__);
     66  Log() << Verbose(1) << "Adding " << *this << " to line " << *line << "."
     67      << endl;
     68  if (line->endpoints[0] == this)
     69    {
     70      lines.insert(LinePair(line->endpoints[1]->Nr, line));
     71    }
     72  else
     73    {
     74      lines.insert(LinePair(line->endpoints[0]->Nr, line));
     75    }
    7276  LinesCount++;
    73 }
    74 ;
     77};
    7578
    7679/** output operator for BoundaryPointSet.
     
    9093 */
    9194BoundaryLineSet::BoundaryLineSet() :
    92   Nr(-1)
    93 {
    94   Info FunctionInfo(__func__);
     95    Nr(-1)
     96{
     97        Info FunctionInfo(__func__);
    9598  for (int i = 0; i < 2; i++)
    9699    endpoints[i] = NULL;
    97 }
    98 ;
     100};
    99101
    100102/** Constructor of BoundaryLineSet with two endpoints.
     
    103105 * \param number number of the list
    104106 */
    105 BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point[2], const int number)
    106 {
    107   Info FunctionInfo(__func__);
     107BoundaryLineSet::BoundaryLineSet(class BoundaryPointSet *Point[2], const int number)
     108{
     109        Info FunctionInfo(__func__);
    108110  // set number
    109111  Nr = number;
     
    116118  skipped = false;
    117119  // clear triangles list
    118   DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
    119 }
    120 ;
    121 
    122 /** Constructor of BoundaryLineSet with two endpoints.
    123  * Adds line automatically to each endpoints' LineMap
    124  * \param *Point1 first boundary point
    125  * \param *Point2 second boundary point
    126  * \param number number of the list
    127  */
    128 BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point1, BoundaryPointSet * const Point2, const int number)
    129 {
    130   Info FunctionInfo(__func__);
    131   // set number
    132   Nr = number;
    133   // set endpoints in ascending order
    134   SetEndpointsOrdered(endpoints, Point1, Point2);
    135   // add this line to the hash maps of both endpoints
    136   Point1->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
    137   Point2->AddLine(this); //
    138   // set skipped to false
    139   skipped = false;
    140   // clear triangles list
    141   DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
    142 }
    143 ;
     120  Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl;
     121};
    144122
    145123/** Destructor for BoundaryLineSet.
     
    149127BoundaryLineSet::~BoundaryLineSet()
    150128{
    151   Info FunctionInfo(__func__);
     129        Info FunctionInfo(__func__);
    152130  int Numbers[2];
    153131
     
    180158        //Log() << Verbose(0) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl;
    181159        if (endpoints[i] != NULL) {
    182           delete (endpoints[i]);
     160          delete(endpoints[i]);
    183161          endpoints[i] = NULL;
    184162        }
     
    187165  }
    188166  if (!triangles.empty())
    189     DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl);
    190 }
    191 ;
     167    eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl;
     168};
    192169
    193170/** Add triangle to TriangleMap of this boundary line.
    194171 * \param *triangle to add
    195172 */
    196 void BoundaryLineSet::AddTriangle(BoundaryTriangleSet * const triangle)
    197 {
    198   Info FunctionInfo(__func__);
    199   DoLog(0) && (Log() << Verbose(0) << "Add " << triangle->Nr << " to line " << *this << "." << endl);
     173void BoundaryLineSet::AddTriangle(class BoundaryTriangleSet *triangle)
     174{
     175        Info FunctionInfo(__func__);
     176  Log() << Verbose(0) << "Add " << triangle->Nr << " to line " << *this << "." << endl;
    200177  triangles.insert(TrianglePair(triangle->Nr, triangle));
    201 }
    202 ;
     178};
    203179
    204180/** Checks whether we have a common endpoint with given \a *line.
     
    206182 * \return true - common endpoint present, false - not connected
    207183 */
    208 bool BoundaryLineSet::IsConnectedTo(const BoundaryLineSet * const line) const
    209 {
    210   Info FunctionInfo(__func__);
     184bool BoundaryLineSet::IsConnectedTo(class BoundaryLineSet *line)
     185{
     186        Info FunctionInfo(__func__);
    211187  if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1]))
    212188    return true;
    213189  else
    214190    return false;
    215 }
    216 ;
     191};
    217192
    218193/** Checks whether the adjacent triangles of a baseline are convex or not.
     
    222197 * \return true - triangles are convex, false - concave or less than two triangles connected
    223198 */
    224 bool BoundaryLineSet::CheckConvexityCriterion() const
    225 {
    226   Info FunctionInfo(__func__);
     199bool BoundaryLineSet::CheckConvexityCriterion()
     200{
     201        Info FunctionInfo(__func__);
    227202  Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck;
    228203  // get the two triangles
    229204  if (triangles.size() != 2) {
    230     DoeLog(0) && (eLog() << Verbose(0) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl);
     205    eLog() << Verbose(0) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl;
    231206    return true;
    232207  }
     
    236211  BaseLineCenter.CopyVector(endpoints[0]->node->node);
    237212  BaseLineCenter.AddVector(endpoints[1]->node->node);
    238   BaseLineCenter.Scale(1. / 2.);
     213  BaseLineCenter.Scale(1./2.);
    239214  BaseLine.CopyVector(endpoints[0]->node->node);
    240215  BaseLine.SubtractVector(endpoints[1]->node->node);
     
    244219  NormalCheck.Zero();
    245220  double sign = -1.;
    246   int i = 0;
     221  int i=0;
    247222  class BoundaryPointSet *node = NULL;
    248   for (TriangleMap::const_iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
     223  for(TriangleMap::iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
    249224    //Log() << Verbose(0) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl;
    250225    NormalCheck.AddVector(&runner->second->NormalVector);
     
    252227    sign = -sign;
    253228    if (runner->second->NormalVector.NormSquared() > MYEPSILON)
    254       BaseLineNormal.CopyVector(&runner->second->NormalVector); // yes, copy second on top of first
     229      BaseLineNormal.CopyVector(&runner->second->NormalVector);   // yes, copy second on top of first
    255230    else {
    256       DoeLog(0) && (eLog() << Verbose(0) << "Triangle " << *runner->second << " has zero normal vector!" << endl);
     231      eLog() << Verbose(0) << "Triangle " << *runner->second << " has zero normal vector!" << endl;
    257232    }
    258233    node = runner->second->GetThirdEndpoint(this);
     
    261236      helper[i].CopyVector(node->node->node);
    262237      helper[i].SubtractVector(&BaseLineCenter);
    263       helper[i].MakeNormalVector(&BaseLine); // we want to compare the triangle's heights' angles!
     238      helper[i].MakeNormalVector(&BaseLine);  // we want to compare the triangle's heights' angles!
    264239      //Log() << Verbose(0) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl;
    265240      i++;
    266241    } else {
    267       DoeLog(1) && (eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl);
     242      eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl;
    268243      return true;
    269244    }
     
    271246  //Log() << Verbose(0) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl;
    272247  if (NormalCheck.NormSquared() < MYEPSILON) {
    273     DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl);
     248    Log() << Verbose(0) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl;
    274249    return true;
    275250  }
     
    277252  double angle = GetAngle(helper[0], helper[1], BaseLineNormal);
    278253  if ((angle - M_PI) > -MYEPSILON) {
    279     DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Angle is greater than pi: convex." << endl);
     254    Log() << Verbose(0) << "ACCEPT: Angle is greater than pi: convex." << endl;
    280255    return true;
    281256  } else {
    282     DoLog(0) && (Log() << Verbose(0) << "REJECT: Angle is less than pi: concave." << endl);
     257    Log() << Verbose(0) << "REJECT: Angle is less than pi: concave." << endl;
    283258    return false;
    284259  }
     
    289264 * \return true - point is of the line, false - is not
    290265 */
    291 bool BoundaryLineSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
    292 {
    293   Info FunctionInfo(__func__);
    294   for (int i = 0; i < 2; i++)
     266bool BoundaryLineSet::ContainsBoundaryPoint(class BoundaryPointSet *point)
     267{
     268        Info FunctionInfo(__func__);
     269  for(int i=0;i<2;i++)
    295270    if (point == endpoints[i])
    296271      return true;
    297272  return false;
    298 }
    299 ;
     273};
    300274
    301275/** Returns other endpoint of the line.
     
    303277 * \return NULL - if endpoint not contained in BoundaryLineSet, or pointer to BoundaryPointSet otherwise
    304278 */
    305 class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(const BoundaryPointSet * const point) const
    306 {
    307   Info FunctionInfo(__func__);
     279class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(class BoundaryPointSet *point)
     280{
     281        Info FunctionInfo(__func__);
    308282  if (endpoints[0] == point)
    309283    return endpoints[1];
     
    312286  else
    313287    return NULL;
    314 }
    315 ;
     288};
    316289
    317290/** output operator for BoundaryLineSet.
     
    319292 * \param &a boundary line
    320293 */
    321 ostream & operator <<(ostream &ost, const BoundaryLineSet &a)
     294ostream & operator <<(ostream &ost, const  BoundaryLineSet &a)
    322295{
    323296  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << "," << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "]";
    324297  return ost;
    325 }
    326 ;
     298};
    327299
    328300// ======================================== Triangles on Boundary =================================
     
    333305  Nr(-1)
    334306{
    335   Info FunctionInfo(__func__);
    336   for (int i = 0; i < 3; i++) {
    337     endpoints[i] = NULL;
    338     lines[i] = NULL;
    339   }
    340 }
    341 ;
     307        Info FunctionInfo(__func__);
     308  for (int i = 0; i < 3; i++)
     309    {
     310      endpoints[i] = NULL;
     311      lines[i] = NULL;
     312    }
     313};
    342314
    343315/** Constructor for BoundaryTriangleSet with three lines.
     
    345317 * \param number number of triangle
    346318 */
    347 BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet * const line[3], const int number) :
     319BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet *line[3], int number) :
    348320  Nr(number)
    349321{
    350   Info FunctionInfo(__func__);
     322        Info FunctionInfo(__func__);
    351323  // set number
    352324  // set lines
     
    360332    // for all three lines
    361333    for (int j = 0; j < 2; j++) { // for both endpoints
    362       OrderMap.insert(pair<int, class BoundaryPointSet *> (line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
     334      OrderMap.insert(pair<int, class BoundaryPointSet *> (
     335          line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
    363336      // and we don't care whether insertion fails
    364337    }
    365338  // set endpoints
    366339  int Counter = 0;
    367   DoLog(0) && (Log() << Verbose(0) << "New triangle " << Nr << " with end points: " << endl);
     340  Log() << Verbose(0) << "New triangle " << Nr << " with end points: " << endl;
    368341  for (PointMap::iterator runner = OrderMap.begin(); runner != OrderMap.end(); runner++) {
    369342    endpoints[Counter] = runner->second;
    370     DoLog(0) && (Log() << Verbose(0) << " " << *endpoints[Counter] << endl);
     343    Log() << Verbose(0) << " " << *endpoints[Counter] << endl;
    371344    Counter++;
    372345  }
    373346  if (Counter < 3) {
    374     DoeLog(0) && (eLog() << Verbose(0) << "We have a triangle with only two distinct endpoints!" << endl);
     347    eLog() << Verbose(0) << "We have a triangle with only two distinct endpoints!" << endl;
    375348    performCriticalExit();
    376349  }
    377 }
    378 ;
     350};
    379351
    380352/** Destructor of BoundaryTriangleSet.
     
    384356BoundaryTriangleSet::~BoundaryTriangleSet()
    385357{
    386   Info FunctionInfo(__func__);
     358        Info FunctionInfo(__func__);
    387359  for (int i = 0; i < 3; i++) {
    388360    if (lines[i] != NULL) {
     
    391363      }
    392364      if (lines[i]->triangles.empty()) {
    393         //Log() << Verbose(0) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
    394         delete (lines[i]);
    395         lines[i] = NULL;
     365          //Log() << Verbose(0) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
     366          delete (lines[i]);
     367          lines[i] = NULL;
    396368      }
    397369    }
    398370  }
    399371  //Log() << Verbose(0) << "Erasing triangle Nr." << Nr << " itself." << endl;
    400 }
    401 ;
     372};
    402373
    403374/** Calculates the normal vector for this triangle.
     
    405376 * \param &OtherVector direction vector to make normal vector unique.
    406377 */
    407 void BoundaryTriangleSet::GetNormalVector(const Vector &OtherVector)
    408 {
    409   Info FunctionInfo(__func__);
     378void BoundaryTriangleSet::GetNormalVector(Vector &OtherVector)
     379{
     380        Info FunctionInfo(__func__);
    410381  // get normal vector
    411382  NormalVector.MakeNormalVector(endpoints[0]->node->node, endpoints[1]->node->node, endpoints[2]->node->node);
     
    414385  if (NormalVector.ScalarProduct(&OtherVector) > 0.)
    415386    NormalVector.Scale(-1.);
    416   DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << NormalVector << "." << endl);
    417 }
    418 ;
    419 
    420 /** Finds the point on the triangle \a *BTS through which the line defined by \a *MolCenter and \a *x crosses.
     387  Log() << Verbose(1) << "Normal Vector is " << NormalVector << "." << endl;
     388};
     389
     390/** Finds the point on the triangle \a *BTS the line defined by \a *MolCenter and \a *x crosses through.
    421391 * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane
    422  * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not.
     392 * This we test if it's really on the plane and whether it's inside the triangle on the plane or not.
    423393 * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
    424394 * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between
     
    430400 * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle.
    431401 */
    432 bool BoundaryTriangleSet::GetIntersectionInsideTriangle(const Vector * const MolCenter, const Vector * const x, Vector * const Intersection) const
    433 {
    434   Info FunctionInfo(__func__);
     402bool BoundaryTriangleSet::GetIntersectionInsideTriangle(Vector *MolCenter, Vector *x, Vector *Intersection)
     403{
     404        Info FunctionInfo(__func__);
    435405  Vector CrossPoint;
    436406  Vector helper;
    437407
    438408  if (!Intersection->GetIntersectionWithPlane(&NormalVector, endpoints[0]->node->node, MolCenter, x)) {
    439     DoeLog(1) && (eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl);
     409    eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl;
    440410    return false;
    441411  }
    442412
    443   DoLog(1) && (Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl);
    444   DoLog(1) && (Log() << Verbose(1) << "INFO: Line is from " << *MolCenter << " to " << *x << "." << endl);
    445   DoLog(1) && (Log() << Verbose(1) << "INFO: Intersection is " << *Intersection << "." << endl);
    446 
    447   if (Intersection->DistanceSquared(endpoints[0]->node->node) < MYEPSILON) {
    448     DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl);
     413  // Calculate cross point between one baseline and the line from the third endpoint to intersection
     414  int i=0;
     415  do {
     416    if (CrossPoint.GetIntersectionOfTwoLinesOnPlane(endpoints[i%3]->node->node, endpoints[(i+1)%3]->node->node, endpoints[(i+2)%3]->node->node, Intersection, &NormalVector)) {
     417      helper.CopyVector(endpoints[(i+1)%3]->node->node);
     418      helper.SubtractVector(endpoints[i%3]->node->node);
     419    } else
     420      i++;
     421    if (i>2)
     422      break;
     423  } while (CrossPoint.NormSquared() < MYEPSILON);
     424  if (i==3) {
     425    eLog() << Verbose(0) << "Could not find any cross points, something's utterly wrong here!" << endl;
     426  }
     427  CrossPoint.SubtractVector(endpoints[i%3]->node->node);  // cross point was returned as absolute vector
     428
     429  // check whether intersection is inside or not by comparing length of intersection and length of cross point
     430  if ((CrossPoint.NormSquared() - helper.NormSquared()) < MYEPSILON) { // inside
    449431    return true;
    450   } else if (Intersection->DistanceSquared(endpoints[1]->node->node) < MYEPSILON) {
    451     DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl);
    452     return true;
    453   } else if (Intersection->DistanceSquared(endpoints[2]->node->node) < MYEPSILON) {
    454     DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl);
    455     return true;
    456   }
    457   // Calculate cross point between one baseline and the line from the third endpoint to intersection
    458   int i = 0;
    459   do {
    460     if (CrossPoint.GetIntersectionOfTwoLinesOnPlane(endpoints[i % 3]->node->node, endpoints[(i + 1) % 3]->node->node, endpoints[(i + 2) % 3]->node->node, Intersection, &NormalVector)) {
    461       helper.CopyVector(endpoints[(i + 1) % 3]->node->node);
    462       helper.SubtractVector(endpoints[i % 3]->node->node);
    463       CrossPoint.SubtractVector(endpoints[i % 3]->node->node); // cross point was returned as absolute vector
    464       const double s = CrossPoint.ScalarProduct(&helper) / helper.NormSquared();
    465       DoLog(1) && (Log() << Verbose(1) << "INFO: Factor s is " << s << "." << endl);
    466       if ((s < -MYEPSILON) || ((s - 1.) > MYEPSILON)) {
    467         DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << "outside of triangle." << endl);
    468         i = 4;
    469         break;
    470       }
    471       i++;
    472     } else
    473       break;
    474   } while (i < 3);
    475   if (i == 3) {
    476     DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " inside of triangle." << endl);
    477     return true;
    478   } else {
    479     DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " outside of triangle." << endl);
     432  } else { // outside!
     433    Intersection->Zero();
    480434    return false;
    481435  }
    482 }
    483 ;
    484 
    485 /** Finds the point on the triangle to the point \a *x.
    486  * We call Vector::GetIntersectionWithPlane() with \a * and the center of the triangle to receive an intersection point.
    487  * Then we check the in-plane part (the part projected down onto plane). We check whether it crosses one of the
    488  * boundary lines. If it does, we return this intersection as closest point, otherwise the projected point down.
    489  * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not.
    490  * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
    491  * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between
    492  * the first two basepoints) or not.
    493  * \param *x point
    494  * \param *ClosestPoint desired closest point inside triangle to \a *x, is absolute vector
    495  * \return Distance squared between \a *x and closest point inside triangle
    496  */
    497 double BoundaryTriangleSet::GetClosestPointInsideTriangle(const Vector * const x, Vector * const ClosestPoint) const
    498 {
    499   Info FunctionInfo(__func__);
    500   Vector Direction;
    501 
    502   // 1. get intersection with plane
    503   DoLog(1) && (Log() << Verbose(1) << "INFO: Looking for closest point of triangle " << *this << " to " << *x << "." << endl);
    504   GetCenter(&Direction);
    505   if (!ClosestPoint->GetIntersectionWithPlane(&NormalVector, endpoints[0]->node->node, x, &Direction)) {
    506     ClosestPoint->CopyVector(x);
    507   }
    508 
    509   // 2. Calculate in plane part of line (x, intersection)
    510   Vector InPlane;
    511   InPlane.CopyVector(x);
    512   InPlane.SubtractVector(ClosestPoint); // points from plane intersection to straight-down point
    513   InPlane.ProjectOntoPlane(&NormalVector);
    514   InPlane.AddVector(ClosestPoint);
    515 
    516   DoLog(2) && (Log() << Verbose(2) << "INFO: Triangle is " << *this << "." << endl);
    517   DoLog(2) && (Log() << Verbose(2) << "INFO: Line is from " << Direction << " to " << *x << "." << endl);
    518   DoLog(2) && (Log() << Verbose(2) << "INFO: In-plane part is " << InPlane << "." << endl);
    519 
    520   // Calculate cross point between one baseline and the desired point such that distance is shortest
    521   double ShortestDistance = -1.;
    522   bool InsideFlag = false;
    523   Vector CrossDirection[3];
    524   Vector CrossPoint[3];
    525   Vector helper;
    526   for (int i = 0; i < 3; i++) {
    527     // treat direction of line as normal of a (cut)plane and the desired point x as the plane offset, the intersect line with point
    528     Direction.CopyVector(endpoints[(i + 1) % 3]->node->node);
    529     Direction.SubtractVector(endpoints[i % 3]->node->node);
    530     // calculate intersection, line can never be parallel to Direction (is the same vector as PlaneNormal);
    531     CrossPoint[i].GetIntersectionWithPlane(&Direction, &InPlane, endpoints[i % 3]->node->node, endpoints[(i + 1) % 3]->node->node);
    532     CrossDirection[i].CopyVector(&CrossPoint[i]);
    533     CrossDirection[i].SubtractVector(&InPlane);
    534     CrossPoint[i].SubtractVector(endpoints[i % 3]->node->node); // cross point was returned as absolute vector
    535     const double s = CrossPoint[i].ScalarProduct(&Direction) / Direction.NormSquared();
    536     DoLog(2) && (Log() << Verbose(2) << "INFO: Factor s is " << s << "." << endl);
    537     if ((s >= -MYEPSILON) && ((s - 1.) <= MYEPSILON)) {
    538       CrossPoint[i].AddVector(endpoints[i % 3]->node->node); // make cross point absolute again
    539       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);
    540       const double distance = CrossPoint[i].DistanceSquared(x);
    541       if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
    542         ShortestDistance = distance;
    543         ClosestPoint->CopyVector(&CrossPoint[i]);
    544       }
    545     } else
    546       CrossPoint[i].Zero();
    547   }
    548   InsideFlag = true;
    549   for (int i = 0; i < 3; i++) {
    550     const double sign = CrossDirection[i].ScalarProduct(&CrossDirection[(i + 1) % 3]);
    551     const double othersign = CrossDirection[i].ScalarProduct(&CrossDirection[(i + 2) % 3]);
    552     ;
    553     if ((sign > -MYEPSILON) && (othersign > -MYEPSILON)) // have different sign
    554       InsideFlag = false;
    555   }
    556   if (InsideFlag) {
    557     ClosestPoint->CopyVector(&InPlane);
    558     ShortestDistance = InPlane.DistanceSquared(x);
    559   } else { // also check endnodes
    560     for (int i = 0; i < 3; i++) {
    561       const double distance = x->DistanceSquared(endpoints[i]->node->node);
    562       if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
    563         ShortestDistance = distance;
    564         ClosestPoint->CopyVector(endpoints[i]->node->node);
    565       }
    566     }
    567   }
    568   DoLog(1) && (Log() << Verbose(1) << "INFO: Closest Point is " << *ClosestPoint << " with shortest squared distance is " << ShortestDistance << "." << endl);
    569   return ShortestDistance;
    570 }
    571 ;
     436};
    572437
    573438/** Checks whether lines is any of the three boundary lines this triangle contains.
     
    575440 * \return true - line is of the triangle, false - is not
    576441 */
    577 bool BoundaryTriangleSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const
    578 {
    579   Info FunctionInfo(__func__);
    580   for (int i = 0; i < 3; i++)
     442bool BoundaryTriangleSet::ContainsBoundaryLine(class BoundaryLineSet *line)
     443{
     444        Info FunctionInfo(__func__);
     445  for(int i=0;i<3;i++)
    581446    if (line == lines[i])
    582447      return true;
    583448  return false;
    584 }
    585 ;
     449};
    586450
    587451/** Checks whether point is any of the three endpoints this triangle contains.
     
    589453 * \return true - point is of the triangle, false - is not
    590454 */
    591 bool BoundaryTriangleSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
    592 {
    593   Info FunctionInfo(__func__);
    594   for (int i = 0; i < 3; i++)
     455bool BoundaryTriangleSet::ContainsBoundaryPoint(class BoundaryPointSet *point)
     456{
     457        Info FunctionInfo(__func__);
     458  for(int i=0;i<3;i++)
    595459    if (point == endpoints[i])
    596460      return true;
    597461  return false;
    598 }
    599 ;
     462};
    600463
    601464/** Checks whether point is any of the three endpoints this triangle contains.
     
    603466 * \return true - point is of the triangle, false - is not
    604467 */
    605 bool BoundaryTriangleSet::ContainsBoundaryPoint(const TesselPoint * const point) const
    606 {
    607   Info FunctionInfo(__func__);
    608   for (int i = 0; i < 3; i++)
     468bool BoundaryTriangleSet::ContainsBoundaryPoint(class TesselPoint *point)
     469{
     470        Info FunctionInfo(__func__);
     471  for(int i=0;i<3;i++)
    609472    if (point == endpoints[i]->node)
    610473      return true;
    611474  return false;
    612 }
    613 ;
     475};
    614476
    615477/** Checks whether three given \a *Points coincide with triangle's endpoints.
     
    617479 * \return true - is the very triangle, false - is not
    618480 */
    619 bool BoundaryTriangleSet::IsPresentTupel(const BoundaryPointSet * const Points[3]) const
    620 {
    621   Info FunctionInfo(__func__);
    622   DoLog(1) && (Log() << Verbose(1) << "INFO: Checking " << Points[0] << "," << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << "." << endl);
    623   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])
    624 
    625   ));
    626 }
    627 ;
     481bool BoundaryTriangleSet::IsPresentTupel(class BoundaryPointSet *Points[3])
     482{
     483        Info FunctionInfo(__func__);
     484  return (((endpoints[0] == Points[0])
     485            || (endpoints[0] == Points[1])
     486            || (endpoints[0] == Points[2])
     487          ) && (
     488            (endpoints[1] == Points[0])
     489            || (endpoints[1] == Points[1])
     490            || (endpoints[1] == Points[2])
     491          ) && (
     492            (endpoints[2] == Points[0])
     493            || (endpoints[2] == Points[1])
     494            || (endpoints[2] == Points[2])
     495
     496          ));
     497};
    628498
    629499/** Checks whether three given \a *Points coincide with triangle's endpoints.
     
    631501 * \return true - is the very triangle, false - is not
    632502 */
    633 bool BoundaryTriangleSet::IsPresentTupel(const BoundaryTriangleSet * const T) const
    634 {
    635   Info FunctionInfo(__func__);
    636   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])
    637 
    638   ));
    639 }
    640 ;
     503bool BoundaryTriangleSet::IsPresentTupel(class BoundaryTriangleSet *T)
     504{
     505        Info FunctionInfo(__func__);
     506  return (((endpoints[0] == T->endpoints[0])
     507            || (endpoints[0] == T->endpoints[1])
     508            || (endpoints[0] == T->endpoints[2])
     509          ) && (
     510            (endpoints[1] == T->endpoints[0])
     511            || (endpoints[1] == T->endpoints[1])
     512            || (endpoints[1] == T->endpoints[2])
     513          ) && (
     514            (endpoints[2] == T->endpoints[0])
     515            || (endpoints[2] == T->endpoints[1])
     516            || (endpoints[2] == T->endpoints[2])
     517
     518          ));
     519};
    641520
    642521/** Returns the endpoint which is not contained in the given \a *line.
     
    644523 * \return pointer third endpoint or NULL if line does not belong to triangle.
    645524 */
    646 class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(const BoundaryLineSet * const line) const
    647 {
    648   Info FunctionInfo(__func__);
     525class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(class BoundaryLineSet *line)
     526{
     527        Info FunctionInfo(__func__);
    649528  // sanity check
    650529  if (!ContainsBoundaryLine(line))
    651530    return NULL;
    652   for (int i = 0; i < 3; i++)
     531  for(int i=0;i<3;i++)
    653532    if (!line->ContainsBoundaryPoint(endpoints[i]))
    654533      return endpoints[i];
    655534  // actually, that' impossible :)
    656535  return NULL;
    657 }
    658 ;
     536};
    659537
    660538/** Calculates the center point of the triangle.
     
    662540 * \param *center central point on return.
    663541 */
    664 void BoundaryTriangleSet::GetCenter(Vector * const center) const
    665 {
    666   Info FunctionInfo(__func__);
     542void BoundaryTriangleSet::GetCenter(Vector *center)
     543{
     544        Info FunctionInfo(__func__);
    667545  center->Zero();
    668   for (int i = 0; i < 3; i++)
     546  for(int i=0;i<3;i++)
    669547    center->AddVector(endpoints[i]->node->node);
    670   center->Scale(1. / 3.);
    671   DoLog(1) && (Log() << Verbose(1) << "INFO: Center is at " << *center << "." << endl);
     548  center->Scale(1./3.);
    672549}
    673550
     
    679556{
    680557  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << "," << a.endpoints[1]->node->Name << "," << a.endpoints[2]->node->Name << "]";
    681   //  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
    682   //      << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
     558//  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
     559//      << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
    683560  return ost;
    684 }
    685 ;
    686 
    687 // ======================================== Polygons on Boundary =================================
    688 
    689 /** Constructor for BoundaryPolygonSet.
    690  */
    691 BoundaryPolygonSet::BoundaryPolygonSet() :
    692   Nr(-1)
     561};
     562
     563// =========================================================== class TESSELPOINT ===========================================
     564
     565/** Constructor of class TesselPoint.
     566 */
     567TesselPoint::TesselPoint()
    693568{
    694569  Info FunctionInfo(__func__);
    695 }
    696 ;
    697 
    698 /** Destructor of BoundaryPolygonSet.
    699  * Just clears endpoints.
    700  * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle()
    701  */
    702 BoundaryPolygonSet::~BoundaryPolygonSet()
    703 {
    704   Info FunctionInfo(__func__);
    705   endpoints.clear();
    706   DoLog(1) && (Log() << Verbose(1) << "Erasing polygon Nr." << Nr << " itself." << endl);
    707 }
    708 ;
    709 
    710 /** Calculates the normal vector for this triangle.
    711  * Is made unique by comparison with \a OtherVector to point in the other direction.
    712  * \param &OtherVector direction vector to make normal vector unique.
    713  * \return allocated vector in normal direction
    714  */
    715 Vector * BoundaryPolygonSet::GetNormalVector(const Vector &OtherVector) const
    716 {
    717   Info FunctionInfo(__func__);
    718   // get normal vector
    719   Vector TemporaryNormal;
    720   Vector *TotalNormal = new Vector;
    721   PointSet::const_iterator Runner[3];
    722   for (int i = 0; i < 3; i++) {
    723     Runner[i] = endpoints.begin();
    724     for (int j = 0; j < i; j++) { // go as much further
    725       Runner[i]++;
    726       if (Runner[i] == endpoints.end()) {
    727         DoeLog(0) && (eLog() << Verbose(0) << "There are less than three endpoints in the polygon!" << endl);
    728         performCriticalExit();
    729       }
    730     }
    731   }
    732   TotalNormal->Zero();
    733   int counter = 0;
    734   for (; Runner[2] != endpoints.end();) {
    735     TemporaryNormal.MakeNormalVector((*Runner[0])->node->node, (*Runner[1])->node->node, (*Runner[2])->node->node);
    736     for (int i = 0; i < 3; i++) // increase each of them
    737       Runner[i]++;
    738     TotalNormal->AddVector(&TemporaryNormal);
    739   }
    740   TotalNormal->Scale(1. / (double) counter);
    741 
    742   // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
    743   if (TotalNormal->ScalarProduct(&OtherVector) > 0.)
    744     TotalNormal->Scale(-1.);
    745   DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << *TotalNormal << "." << endl);
    746 
    747   return TotalNormal;
    748 }
    749 ;
    750 
    751 /** Calculates the center point of the triangle.
    752  * Is third of the sum of all endpoints.
    753  * \param *center central point on return.
    754  */
    755 void BoundaryPolygonSet::GetCenter(Vector * const center) const
    756 {
    757   Info FunctionInfo(__func__);
    758   center->Zero();
    759   int counter = 0;
    760   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
    761     center->AddVector((*Runner)->node->node);
    762     counter++;
    763   }
    764   center->Scale(1. / (double) counter);
    765   DoLog(1) && (Log() << Verbose(1) << "Center is at " << *center << "." << endl);
    766 }
    767 
    768 /** Checks whether the polygons contains all three endpoints of the triangle.
    769  * \param *triangle triangle to test
    770  * \return true - triangle is contained polygon, false - is not
    771  */
    772 bool BoundaryPolygonSet::ContainsBoundaryTriangle(const BoundaryTriangleSet * const triangle) const
    773 {
    774   Info FunctionInfo(__func__);
    775   return ContainsPresentTupel(triangle->endpoints, 3);
    776 }
    777 ;
    778 
    779 /** Checks whether the polygons contains both endpoints of the line.
    780  * \param *line line to test
    781  * \return true - line is of the triangle, false - is not
    782  */
    783 bool BoundaryPolygonSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const
    784 {
    785   Info FunctionInfo(__func__);
    786   return ContainsPresentTupel(line->endpoints, 2);
    787 }
    788 ;
    789 
    790 /** Checks whether point is any of the three endpoints this triangle contains.
    791  * \param *point point to test
    792  * \return true - point is of the triangle, false - is not
    793  */
    794 bool BoundaryPolygonSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
    795 {
    796   Info FunctionInfo(__func__);
    797   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
    798     DoLog(0) && (Log() << Verbose(0) << "Checking against " << **Runner << endl);
    799     if (point == (*Runner)) {
    800       DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
    801       return true;
    802     }
    803   }
    804   DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
    805   return false;
    806 }
    807 ;
    808 
    809 /** Checks whether point is any of the three endpoints this triangle contains.
    810  * \param *point TesselPoint to test
    811  * \return true - point is of the triangle, false - is not
    812  */
    813 bool BoundaryPolygonSet::ContainsBoundaryPoint(const TesselPoint * const point) const
    814 {
    815   Info FunctionInfo(__func__);
    816   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
    817     if (point == (*Runner)->node) {
    818       DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
    819       return true;
    820     }
    821   DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
    822   return false;
    823 }
    824 ;
    825 
    826 /** Checks whether given array of \a *Points coincide with polygons's endpoints.
    827  * \param **Points pointer to an array of BoundaryPointSet
    828  * \param dim dimension of array
    829  * \return true - set of points is contained in polygon, false - is not
    830  */
    831 bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPointSet * const * Points, const int dim) const
    832 {
    833   Info FunctionInfo(__func__);
    834   int counter = 0;
    835   DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
    836   for (int i = 0; i < dim; i++) {
    837     DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << *Points[i] << endl);
    838     if (ContainsBoundaryPoint(Points[i])) {
    839       counter++;
    840     }
    841   }
    842 
    843   if (counter == dim)
    844     return true;
    845   else
    846     return false;
    847 }
    848 ;
    849 
    850 /** Checks whether given PointList coincide with polygons's endpoints.
    851  * \param &endpoints PointList
    852  * \return true - set of points is contained in polygon, false - is not
    853  */
    854 bool BoundaryPolygonSet::ContainsPresentTupel(const PointSet &endpoints) const
    855 {
    856   Info FunctionInfo(__func__);
    857   size_t counter = 0;
    858   DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
    859   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
    860     DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << **Runner << endl);
    861     if (ContainsBoundaryPoint(*Runner))
    862       counter++;
    863   }
    864 
    865   if (counter == endpoints.size())
    866     return true;
    867   else
    868     return false;
    869 }
    870 ;
    871 
    872 /** Checks whether given set of \a *Points coincide with polygons's endpoints.
    873  * \param *P pointer to BoundaryPolygonSet
    874  * \return true - is the very triangle, false - is not
    875  */
    876 bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPolygonSet * const P) const
    877 {
    878   return ContainsPresentTupel((const PointSet) P->endpoints);
    879 }
    880 ;
    881 
    882 /** Gathers all the endpoints' triangles in a unique set.
    883  * \return set of all triangles
    884  */
    885 TriangleSet * BoundaryPolygonSet::GetAllContainedTrianglesFromEndpoints() const
    886 {
    887   Info FunctionInfo(__func__);
    888   pair<TriangleSet::iterator, bool> Tester;
    889   TriangleSet *triangles = new TriangleSet;
    890 
    891   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
    892     for (LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++)
    893       for (TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) {
    894         //Log() << Verbose(0) << " Testing triangle " << *(Sprinter->second) << endl;
    895         if (ContainsBoundaryTriangle(Sprinter->second)) {
    896           Tester = triangles->insert(Sprinter->second);
    897           if (Tester.second)
    898             DoLog(0) && (Log() << Verbose(0) << "Adding triangle " << *(Sprinter->second) << endl);
    899         }
    900       }
    901 
    902   DoLog(1) && (Log() << Verbose(1) << "The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total." << endl);
    903   return triangles;
    904 }
    905 ;
    906 
    907 /** Fills the endpoints of this polygon from the triangles attached to \a *line.
    908  * \param *line lines with triangles attached
    909  * \return true - polygon contains endpoints, false - line was NULL
    910  */
    911 bool BoundaryPolygonSet::FillPolygonFromTrianglesOfLine(const BoundaryLineSet * const line)
    912 {
    913   Info FunctionInfo(__func__);
    914   pair<PointSet::iterator, bool> Tester;
    915   if (line == NULL)
    916     return false;
    917   DoLog(1) && (Log() << Verbose(1) << "Filling polygon from line " << *line << endl);
    918   for (TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) {
    919     for (int i = 0; i < 3; i++) {
    920       Tester = endpoints.insert((Runner->second)->endpoints[i]);
    921       if (Tester.second)
    922         DoLog(1) && (Log() << Verbose(1) << "  Inserting endpoint " << *((Runner->second)->endpoints[i]) << endl);
    923     }
    924   }
    925 
    926   return true;
    927 }
    928 ;
    929 
    930 /** output operator for BoundaryPolygonSet.
    931  * \param &ost output stream
    932  * \param &a boundary polygon
    933  */
    934 ostream &operator <<(ostream &ost, const BoundaryPolygonSet &a)
    935 {
    936   ost << "[" << a.Nr << "|";
    937   for (PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) {
    938     ost << (*Runner)->node->Name;
    939     Runner++;
    940     if (Runner != a.endpoints.end())
    941       ost << ",";
    942   }
    943   ost << "]";
    944   return ost;
    945 }
    946 ;
    947 
    948 // =========================================================== class TESSELPOINT ===========================================
    949 
    950 /** Constructor of class TesselPoint.
    951  */
    952 TesselPoint::TesselPoint()
    953 {
    954   //Info FunctionInfo(__func__);
    955570  node = NULL;
    956571  nr = -1;
    957   Name = NULL;
    958 }
    959 ;
     572  Name =  NULL;
     573};
    960574
    961575/** Destructor for class TesselPoint.
     
    963577TesselPoint::~TesselPoint()
    964578{
    965   //Info FunctionInfo(__func__);
    966 }
    967 ;
     579  Info FunctionInfo(__func__);
     580};
    968581
    969582/** Prints LCNode to screen.
    970583 */
    971 ostream & operator <<(ostream &ost, const TesselPoint &a)
     584ostream & operator << (ostream &ost, const TesselPoint &a)
    972585{
    973586  ost << "[" << (a.Name) << "|" << a.Name << " at " << *a.node << "]";
    974587  return ost;
    975 }
    976 ;
     588};
    977589
    978590/** Prints LCNode to screen.
    979591 */
    980 ostream & TesselPoint::operator <<(ostream &ost)
    981 {
    982   Info FunctionInfo(__func__);
     592ostream & TesselPoint::operator << (ostream &ost)
     593{
     594        Info FunctionInfo(__func__);
    983595  ost << "[" << (nr) << "|" << this << "]";
    984596  return ost;
    985 }
    986 ;
     597};
     598
    987599
    988600// =========================================================== class POINTCLOUD ============================================
     
    992604PointCloud::PointCloud()
    993605{
    994   //Info FunctionInfo(__func__);
    995 }
    996 ;
     606        Info FunctionInfo(__func__);
     607};
    997608
    998609/** Destructor for class PointCloud.
     
    1000611PointCloud::~PointCloud()
    1001612{
    1002   //Info FunctionInfo(__func__);
    1003 }
    1004 ;
     613        Info FunctionInfo(__func__);
     614};
    1005615
    1006616// ============================ CandidateForTesselation =============================
     
    1008618/** Constructor of class CandidateForTesselation.
    1009619 */
    1010 CandidateForTesselation::CandidateForTesselation(BoundaryLineSet* line) :
    1011   BaseLine(line), ThirdPoint(NULL), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI)
    1012 {
    1013   Info FunctionInfo(__func__);
    1014 }
    1015 ;
     620CandidateForTesselation::CandidateForTesselation (BoundaryLineSet* line) :
     621  BaseLine(line),
     622  ShortestAngle(2.*M_PI),
     623  OtherShortestAngle(2.*M_PI)
     624{
     625        Info FunctionInfo(__func__);
     626};
     627
    1016628
    1017629/** Constructor of class CandidateForTesselation.
    1018630 */
    1019 CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, BoundaryPointSet* point, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) :
    1020   BaseLine(line), ThirdPoint(point), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI)
    1021 {
    1022   Info FunctionInfo(__func__);
     631CandidateForTesselation::CandidateForTesselation (TesselPoint *candidate, BoundaryLineSet* line, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) :
     632    BaseLine(line),
     633    ShortestAngle(2.*M_PI),
     634    OtherShortestAngle(2.*M_PI)
     635{
     636        Info FunctionInfo(__func__);
    1023637  OptCenter.CopyVector(&OptCandidateCenter);
    1024638  OtherOptCenter.CopyVector(&OtherOptCandidateCenter);
    1025 }
    1026 ;
     639};
    1027640
    1028641/** Destructor for class CandidateForTesselation.
    1029642 */
    1030 CandidateForTesselation::~CandidateForTesselation()
    1031 {
    1032 }
    1033 ;
    1034 
    1035 /** Checks validity of a given sphere of a candidate line.
    1036  * Sphere must touch all candidates and the baseline endpoints and there must be no other atoms inside.
    1037  * \param RADIUS radius of sphere
    1038  * \param *LC LinkedCell structure with other atoms
    1039  * \return true - sphere is valid, false - sphere contains other points
    1040  */
    1041 bool CandidateForTesselation::CheckValidity(const double RADIUS, const LinkedCell *LC) const
    1042 {
    1043   Info FunctionInfo(__func__);
    1044 
    1045   const double radiusSquared = RADIUS * RADIUS;
    1046   list<const Vector *> VectorList;
    1047   VectorList.push_back(&OptCenter);
    1048   //VectorList.push_back(&OtherOptCenter);  // don't check the other (wrong) center
    1049 
    1050   if (!pointlist.empty())
    1051     DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains candidate list and baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl);
    1052   else
    1053     DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere with no candidates contains baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl);
    1054   // check baseline for OptCenter and OtherOptCenter being on sphere's surface
    1055   for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
    1056     for (int i = 0; i < 2; i++) {
    1057       const double distance = fabs((*VRunner)->DistanceSquared(BaseLine->endpoints[i]->node->node) - radiusSquared);
    1058       if (distance > HULLEPSILON) {
    1059         DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << *BaseLine->endpoints[i] << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl);
    1060         return false;
    1061       }
    1062     }
    1063   }
    1064 
    1065   // check Candidates for OptCenter and OtherOptCenter being on sphere's surface
    1066   for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) {
    1067     const TesselPoint *Walker = *Runner;
    1068     for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
    1069       const double distance = fabs((*VRunner)->DistanceSquared(Walker->node) - radiusSquared);
    1070       if (distance > HULLEPSILON) {
    1071         DoeLog(1) && (eLog() << Verbose(1) << "Candidate " << *Walker << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl);
    1072         return false;
    1073       } else {
    1074         DoLog(1) && (Log() << Verbose(1) << "Candidate " << *Walker << " is inside by " << distance << "." << endl);
    1075       }
    1076     }
    1077   }
    1078 
    1079   DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl);
    1080   bool flag = true;
    1081   for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
    1082     // get all points inside the sphere
    1083     TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, (*VRunner));
    1084 
    1085     DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << OtherOptCenter << ":" << endl);
    1086     for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
    1087       DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->Distance(&OtherOptCenter) << "." << endl);
    1088 
    1089     // remove baseline's endpoints and candidates
    1090     for (int i = 0; i < 2; i++) {
    1091       DoLog(1) && (Log() << Verbose(1) << "INFO: removing baseline tesselpoint " << *BaseLine->endpoints[i]->node << "." << endl);
    1092       ListofPoints->remove(BaseLine->endpoints[i]->node);
    1093     }
    1094     for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) {
    1095       DoLog(1) && (Log() << Verbose(1) << "INFO: removing candidate tesselpoint " << *(*Runner) << "." << endl);
    1096       ListofPoints->remove(*Runner);
    1097     }
    1098     if (!ListofPoints->empty()) {
    1099       DoeLog(1) && (eLog() << Verbose(1) << "CheckValidity: There are still " << ListofPoints->size() << " points inside the sphere." << endl);
    1100       flag = false;
    1101       DoeLog(1) && (eLog() << Verbose(1) << "External atoms inside of sphere at " << *(*VRunner) << ":" << endl);
    1102       for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
    1103         DoeLog(1) && (eLog() << Verbose(1) << "  " << *(*Runner) << endl);
    1104     }
    1105     delete (ListofPoints);
    1106 
    1107     // check with animate_sphere.tcl VMD script
    1108     if (ThirdPoint != NULL) {
    1109       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.x[0] << " " << OldCenter.x[1] << " " << OldCenter.x[2] << " " << (*VRunner)->x[0] << " " << (*VRunner)->x[1] << " " << (*VRunner)->x[2] << endl);
    1110     } else {
    1111       DoLog(1) && (Log() << Verbose(1) << "Check by: ... missing third point ..." << endl);
    1112       DoLog(1) && (Log() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " ??? " << RADIUS << " " << OldCenter.x[0] << " " << OldCenter.x[1] << " " << OldCenter.x[2] << " " << (*VRunner)->x[0] << " " << (*VRunner)->x[1] << " " << (*VRunner)->x[2] << endl);
    1113     }
    1114   }
    1115   return flag;
    1116 }
    1117 ;
     643CandidateForTesselation::~CandidateForTesselation() {
     644  BaseLine = NULL;
     645};
    1118646
    1119647/** output operator for CandidateForTesselation.
     
    1121649 * \param &a boundary line
    1122650 */
    1123 ostream & operator <<(ostream &ost, const CandidateForTesselation &a)
     651ostream & operator <<(ostream &ost, const  CandidateForTesselation &a)
    1124652{
    1125653  ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->Name << "," << a.BaseLine->endpoints[1]->node->Name << "] with ";
     
    1134662    for (TesselPointList::const_iterator Runner = a.pointlist.begin(); Runner != a.pointlist.end(); Runner++)
    1135663      ost << *(*Runner) << " ";
    1136     ost << " at angle " << (a.ShortestAngle) << ".";
     664    ost << " at angle " << (a.ShortestAngle)<< ".";
    1137665  }
    1138666
    1139667  return ost;
     668};
     669
     670
     671// =========================================================== class TESSELATION ===========================================
     672
     673/** Constructor of class Tesselation.
     674 */
     675Tesselation::Tesselation() :
     676  PointsOnBoundaryCount(0),
     677  LinesOnBoundaryCount(0),
     678  TrianglesOnBoundaryCount(0),
     679  LastTriangle(NULL),
     680  TriangleFilesWritten(0),
     681  InternalPointer(PointsOnBoundary.begin())
     682{
     683        Info FunctionInfo(__func__);
    1140684}
    1141685;
    1142686
    1143 // =========================================================== class TESSELATION ===========================================
    1144 
    1145 /** Constructor of class Tesselation.
    1146  */
    1147 Tesselation::Tesselation() :
    1148   PointsOnBoundaryCount(0), LinesOnBoundaryCount(0), TrianglesOnBoundaryCount(0), LastTriangle(NULL), TriangleFilesWritten(0), InternalPointer(PointsOnBoundary.begin())
    1149 {
    1150   Info FunctionInfo(__func__);
    1151 }
    1152 ;
    1153 
    1154687/** Destructor of class Tesselation.
    1155688 * We have to free all points, lines and triangles.
     
    1157690Tesselation::~Tesselation()
    1158691{
    1159   Info FunctionInfo(__func__);
    1160   DoLog(0) && (Log() << Verbose(0) << "Free'ing TesselStruct ... " << endl);
     692        Info FunctionInfo(__func__);
     693  Log() << Verbose(0) << "Free'ing TesselStruct ... " << endl;
    1161694  for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner != TrianglesOnBoundary.end(); runner++) {
    1162695    if (runner->second != NULL) {
     
    1164697      runner->second = NULL;
    1165698    } else
    1166       DoeLog(1) && (eLog() << Verbose(1) << "The triangle " << runner->first << " has already been free'd." << endl);
    1167   }
    1168   DoLog(0) && (Log() << Verbose(0) << "This envelope was written to file " << TriangleFilesWritten << " times(s)." << endl);
     699      eLog() << Verbose(1) << "The triangle " << runner->first << " has already been free'd." << endl;
     700  }
     701  Log() << Verbose(0) << "This envelope was written to file " << TriangleFilesWritten << " times(s)." << endl;
    1169702}
    1170703;
     
    1172705/** PointCloud implementation of GetCenter
    1173706 * Uses PointsOnBoundary and STL stuff.
    1174  */
     707 */   
    1175708Vector * Tesselation::GetCenter(ofstream *out) const
    1176709{
    1177   Info FunctionInfo(__func__);
    1178   Vector *Center = new Vector(0., 0., 0.);
    1179   int num = 0;
     710        Info FunctionInfo(__func__);
     711  Vector *Center = new Vector(0.,0.,0.);
     712  int num=0;
    1180713  for (GoToFirst(); (!IsEnd()); GoToNext()) {
    1181714    Center->AddVector(GetPoint()->node);
    1182715    num++;
    1183716  }
    1184   Center->Scale(1. / num);
     717  Center->Scale(1./num);
    1185718  return Center;
    1186 }
    1187 ;
     719};
    1188720
    1189721/** PointCloud implementation of GoPoint
    1190722 * Uses PointsOnBoundary and STL stuff.
    1191  */
     723 */   
    1192724TesselPoint * Tesselation::GetPoint() const
    1193725{
    1194   Info FunctionInfo(__func__);
     726        Info FunctionInfo(__func__);
    1195727  return (InternalPointer->second->node);
    1196 }
    1197 ;
     728};
    1198729
    1199730/** PointCloud implementation of GetTerminalPoint.
    1200731 * Uses PointsOnBoundary and STL stuff.
    1201  */
     732 */   
    1202733TesselPoint * Tesselation::GetTerminalPoint() const
    1203734{
    1204   Info FunctionInfo(__func__);
     735        Info FunctionInfo(__func__);
    1205736  PointMap::const_iterator Runner = PointsOnBoundary.end();
    1206737  Runner--;
    1207738  return (Runner->second->node);
    1208 }
    1209 ;
     739};
    1210740
    1211741/** PointCloud implementation of GoToNext.
    1212742 * Uses PointsOnBoundary and STL stuff.
    1213  */
     743 */   
    1214744void Tesselation::GoToNext() const
    1215745{
    1216   Info FunctionInfo(__func__);
     746        Info FunctionInfo(__func__);
    1217747  if (InternalPointer != PointsOnBoundary.end())
    1218748    InternalPointer++;
    1219 }
    1220 ;
     749};
    1221750
    1222751/** PointCloud implementation of GoToPrevious.
    1223752 * Uses PointsOnBoundary and STL stuff.
    1224  */
     753 */   
    1225754void Tesselation::GoToPrevious() const
    1226755{
    1227   Info FunctionInfo(__func__);
     756        Info FunctionInfo(__func__);
    1228757  if (InternalPointer != PointsOnBoundary.begin())
    1229758    InternalPointer--;
    1230 }
    1231 ;
     759};
    1232760
    1233761/** PointCloud implementation of GoToFirst.
    1234762 * Uses PointsOnBoundary and STL stuff.
    1235  */
     763 */   
    1236764void Tesselation::GoToFirst() const
    1237765{
    1238   Info FunctionInfo(__func__);
     766        Info FunctionInfo(__func__);
    1239767  InternalPointer = PointsOnBoundary.begin();
    1240 }
    1241 ;
     768};
    1242769
    1243770/** PointCloud implementation of GoToLast.
     
    1246773void Tesselation::GoToLast() const
    1247774{
    1248   Info FunctionInfo(__func__);
     775        Info FunctionInfo(__func__);
    1249776  InternalPointer = PointsOnBoundary.end();
    1250777  InternalPointer--;
    1251 }
    1252 ;
     778};
    1253779
    1254780/** PointCloud implementation of IsEmpty.
    1255781 * Uses PointsOnBoundary and STL stuff.
    1256  */
     782 */   
    1257783bool Tesselation::IsEmpty() const
    1258784{
    1259   Info FunctionInfo(__func__);
     785        Info FunctionInfo(__func__);
    1260786  return (PointsOnBoundary.empty());
    1261 }
    1262 ;
     787};
    1263788
    1264789/** PointCloud implementation of IsLast.
    1265790 * Uses PointsOnBoundary and STL stuff.
    1266  */
     791 */   
    1267792bool Tesselation::IsEnd() const
    1268793{
    1269   Info FunctionInfo(__func__);
     794        Info FunctionInfo(__func__);
    1270795  return (InternalPointer == PointsOnBoundary.end());
    1271 }
    1272 ;
     796};
     797
    1273798
    1274799/** Gueses first starting triangle of the convex envelope.
     
    1277802 * \param PointsOnBoundary set of boundary points defining the convex envelope of the cluster
    1278803 */
    1279 void Tesselation::GuessStartingTriangle()
    1280 {
    1281   Info FunctionInfo(__func__);
     804void
     805Tesselation::GuessStartingTriangle()
     806{
     807        Info FunctionInfo(__func__);
    1282808  // 4b. create a starting triangle
    1283809  // 4b1. create all distances
     
    1289815
    1290816  // with A chosen, take each pair B,C and sort
    1291   if (A != PointsOnBoundary.end()) {
    1292     B = A;
    1293     B++;
    1294     for (; B != PointsOnBoundary.end(); B++) {
    1295       C = B;
    1296       C++;
    1297       for (; C != PointsOnBoundary.end(); C++) {
    1298         tmp = A->second->node->node->DistanceSquared(B->second->node->node);
    1299         distance = tmp * tmp;
    1300         tmp = A->second->node->node->DistanceSquared(C->second->node->node);
    1301         distance += tmp * tmp;
    1302         tmp = B->second->node->node->DistanceSquared(C->second->node->node);
    1303         distance += tmp * tmp;
    1304         DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C)));
    1305       }
    1306     }
    1307   }
     817  if (A != PointsOnBoundary.end())
     818    {
     819      B = A;
     820      B++;
     821      for (; B != PointsOnBoundary.end(); B++)
     822        {
     823          C = B;
     824          C++;
     825          for (; C != PointsOnBoundary.end(); C++)
     826            {
     827              tmp = A->second->node->node->DistanceSquared(B->second->node->node);
     828              distance = tmp * tmp;
     829              tmp = A->second->node->node->DistanceSquared(C->second->node->node);
     830              distance += tmp * tmp;
     831              tmp = B->second->node->node->DistanceSquared(C->second->node->node);
     832              distance += tmp * tmp;
     833              DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C)));
     834            }
     835        }
     836    }
    1308837  //    // listing distances
    1309838  //    Log() << Verbose(1) << "Listing DistanceMMap:";
     
    1315844  // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
    1316845  DistanceMultiMap::iterator baseline = DistanceMMap.begin();
    1317   for (; baseline != DistanceMMap.end(); baseline++) {
    1318     // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
    1319     // 2. next, we have to check whether all points reside on only one side of the triangle
    1320     // 3. construct plane vector
    1321     PlaneVector.MakeNormalVector(A->second->node->node, baseline->second.first->second->node->node, baseline->second.second->second->node->node);
    1322     DoLog(2) && (Log() << Verbose(2) << "Plane vector of candidate triangle is " << PlaneVector << endl);
    1323     // 4. loop over all points
    1324     double sign = 0.;
    1325     PointMap::iterator checker = PointsOnBoundary.begin();
    1326     for (; checker != PointsOnBoundary.end(); checker++) {
    1327       // (neglecting A,B,C)
    1328       if ((checker == A) || (checker == baseline->second.first) || (checker == baseline->second.second))
    1329         continue;
    1330       // 4a. project onto plane vector
    1331       TrialVector.CopyVector(checker->second->node->node);
    1332       TrialVector.SubtractVector(A->second->node->node);
    1333       distance = TrialVector.ScalarProduct(&PlaneVector);
    1334       if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
    1335         continue;
    1336       DoLog(2) && (Log() << Verbose(2) << "Projection of " << checker->second->node->Name << " yields distance of " << distance << "." << endl);
    1337       tmp = distance / fabs(distance);
    1338       // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
    1339       if ((sign != 0) && (tmp != sign)) {
    1340         // 4c. If so, break 4. loop and continue with next candidate in 1. loop
    1341         DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->Name << "," << baseline->second.first->second->node->Name << "," << baseline->second.second->second->node->Name << " leaves " << checker->second->node->Name << " outside the convex hull." << endl);
    1342         break;
    1343       } else { // note the sign for later
    1344         DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->Name << "," << baseline->second.first->second->node->Name << "," << baseline->second.second->second->node->Name << " leave " << checker->second->node->Name << " inside the convex hull." << endl);
    1345         sign = tmp;
    1346       }
    1347       // 4d. Check whether the point is inside the triangle (check distance to each node
    1348       tmp = checker->second->node->node->DistanceSquared(A->second->node->node);
    1349       int innerpoint = 0;
    1350       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)))
    1351         innerpoint++;
    1352       tmp = checker->second->node->node->DistanceSquared(baseline->second.first->second->node->node);
    1353       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)))
    1354         innerpoint++;
    1355       tmp = checker->second->node->node->DistanceSquared(baseline->second.second->second->node->node);
    1356       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)))
    1357         innerpoint++;
    1358       // 4e. If so, break 4. loop and continue with next candidate in 1. loop
    1359       if (innerpoint == 3)
    1360         break;
    1361     }
    1362     // 5. come this far, all on same side? Then break 1. loop and construct triangle
    1363     if (checker == PointsOnBoundary.end()) {
    1364       DoLog(2) && (Log() << Verbose(2) << "Looks like we have a candidate!" << endl);
    1365       break;
    1366     }
    1367   }
    1368   if (baseline != DistanceMMap.end()) {
    1369     BPS[0] = baseline->second.first->second;
    1370     BPS[1] = baseline->second.second->second;
    1371     BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1372     BPS[0] = A->second;
    1373     BPS[1] = baseline->second.second->second;
    1374     BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1375     BPS[0] = baseline->second.first->second;
    1376     BPS[1] = A->second;
    1377     BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1378 
    1379     // 4b3. insert created triangle
    1380     BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    1381     TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
    1382     TrianglesOnBoundaryCount++;
    1383     for (int i = 0; i < NDIM; i++) {
    1384       LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
    1385       LinesOnBoundaryCount++;
    1386     }
    1387 
    1388     DoLog(1) && (Log() << Verbose(1) << "Starting triangle is " << *BTS << "." << endl);
    1389   } else {
    1390     DoeLog(0) && (eLog() << Verbose(0) << "No starting triangle found." << endl);
    1391   }
     846  for (; baseline != DistanceMMap.end(); baseline++)
     847    {
     848      // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
     849      // 2. next, we have to check whether all points reside on only one side of the triangle
     850      // 3. construct plane vector
     851      PlaneVector.MakeNormalVector(A->second->node->node,
     852          baseline->second.first->second->node->node,
     853          baseline->second.second->second->node->node);
     854      Log() << Verbose(2) << "Plane vector of candidate triangle is " << PlaneVector << endl;
     855      // 4. loop over all points
     856      double sign = 0.;
     857      PointMap::iterator checker = PointsOnBoundary.begin();
     858      for (; checker != PointsOnBoundary.end(); checker++)
     859        {
     860          // (neglecting A,B,C)
     861          if ((checker == A) || (checker == baseline->second.first) || (checker
     862              == baseline->second.second))
     863            continue;
     864          // 4a. project onto plane vector
     865          TrialVector.CopyVector(checker->second->node->node);
     866          TrialVector.SubtractVector(A->second->node->node);
     867          distance = TrialVector.ScalarProduct(&PlaneVector);
     868          if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
     869            continue;
     870          Log() << Verbose(2) << "Projection of " << checker->second->node->Name << " yields distance of " << distance << "." << endl;
     871          tmp = distance / fabs(distance);
     872          // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
     873          if ((sign != 0) && (tmp != sign))
     874            {
     875              // 4c. If so, break 4. loop and continue with next candidate in 1. loop
     876              Log() << Verbose(2) << "Current candidates: "
     877                  << A->second->node->Name << ","
     878                  << baseline->second.first->second->node->Name << ","
     879                  << baseline->second.second->second->node->Name << " leaves "
     880                  << checker->second->node->Name << " outside the convex hull."
     881                  << endl;
     882              break;
     883            }
     884          else
     885            { // note the sign for later
     886              Log() << Verbose(2) << "Current candidates: "
     887                  << A->second->node->Name << ","
     888                  << baseline->second.first->second->node->Name << ","
     889                  << baseline->second.second->second->node->Name << " leave "
     890                  << checker->second->node->Name << " inside the convex hull."
     891                  << endl;
     892              sign = tmp;
     893            }
     894          // 4d. Check whether the point is inside the triangle (check distance to each node
     895          tmp = checker->second->node->node->DistanceSquared(A->second->node->node);
     896          int innerpoint = 0;
     897          if ((tmp < A->second->node->node->DistanceSquared(
     898              baseline->second.first->second->node->node)) && (tmp
     899              < A->second->node->node->DistanceSquared(
     900                  baseline->second.second->second->node->node)))
     901            innerpoint++;
     902          tmp = checker->second->node->node->DistanceSquared(
     903              baseline->second.first->second->node->node);
     904          if ((tmp < baseline->second.first->second->node->node->DistanceSquared(
     905              A->second->node->node)) && (tmp
     906              < baseline->second.first->second->node->node->DistanceSquared(
     907                  baseline->second.second->second->node->node)))
     908            innerpoint++;
     909          tmp = checker->second->node->node->DistanceSquared(
     910              baseline->second.second->second->node->node);
     911          if ((tmp < baseline->second.second->second->node->node->DistanceSquared(
     912              baseline->second.first->second->node->node)) && (tmp
     913              < baseline->second.second->second->node->node->DistanceSquared(
     914                  A->second->node->node)))
     915            innerpoint++;
     916          // 4e. If so, break 4. loop and continue with next candidate in 1. loop
     917          if (innerpoint == 3)
     918            break;
     919        }
     920      // 5. come this far, all on same side? Then break 1. loop and construct triangle
     921      if (checker == PointsOnBoundary.end())
     922        {
     923          Log() << Verbose(2) << "Looks like we have a candidate!" << endl;
     924          break;
     925        }
     926    }
     927  if (baseline != DistanceMMap.end())
     928    {
     929      BPS[0] = baseline->second.first->second;
     930      BPS[1] = baseline->second.second->second;
     931      BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
     932      BPS[0] = A->second;
     933      BPS[1] = baseline->second.second->second;
     934      BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
     935      BPS[0] = baseline->second.first->second;
     936      BPS[1] = A->second;
     937      BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
     938
     939      // 4b3. insert created triangle
     940      BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
     941      TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
     942      TrianglesOnBoundaryCount++;
     943      for (int i = 0; i < NDIM; i++)
     944        {
     945          LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
     946          LinesOnBoundaryCount++;
     947        }
     948
     949      Log() << Verbose(1) << "Starting triangle is " << *BTS << "." << endl;
     950    }
     951  else
     952    {
     953      eLog() << Verbose(0) << "No starting triangle found." << endl;
     954    }
    1392955}
    1393956;
     
    1408971void Tesselation::TesselateOnBoundary(const PointCloud * const cloud)
    1409972{
    1410   Info FunctionInfo(__func__);
     973        Info FunctionInfo(__func__);
    1411974  bool flag;
    1412975  PointMap::iterator winner;
     
    1427990        // get peak point with respect to this base line's only triangle
    1428991        BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far
    1429         DoLog(0) && (Log() << Verbose(0) << "Current baseline is between " << *(baseline->second) << "." << endl);
     992        Log() << Verbose(0) << "Current baseline is between " << *(baseline->second) << "." << endl;
    1430993        for (int i = 0; i < 3; i++)
    1431994          if ((BTS->endpoints[i] != baseline->second->endpoints[0]) && (BTS->endpoints[i] != baseline->second->endpoints[1]))
    1432995            peak = BTS->endpoints[i];
    1433         DoLog(1) && (Log() << Verbose(1) << " and has peak " << *peak << "." << endl);
     996        Log() << Verbose(1) << " and has peak " << *peak << "." << endl;
    1434997
    1435998        // prepare some auxiliary vectors
     
    14461009          CenterVector.AddVector(BTS->endpoints[i]->node->node);
    14471010        CenterVector.Scale(1. / 3.);
    1448         DoLog(2) && (Log() << Verbose(2) << "CenterVector of base triangle is " << CenterVector << endl);
     1011        Log() << Verbose(2) << "CenterVector of base triangle is " << CenterVector << endl;
    14491012
    14501013        // normal vector of triangle
     
    14531016        BTS->GetNormalVector(NormalVector);
    14541017        NormalVector.CopyVector(&BTS->NormalVector);
    1455         DoLog(2) && (Log() << Verbose(2) << "NormalVector of base triangle is " << NormalVector << endl);
     1018        Log() << Verbose(2) << "NormalVector of base triangle is " << NormalVector << endl;
    14561019
    14571020        // vector in propagation direction (out of triangle)
     
    14631026        if (PropagationVector.ScalarProduct(&TempVector) > 0) // make sure normal propagation vector points outward from baseline
    14641027          PropagationVector.Scale(-1.);
    1465         DoLog(2) && (Log() << Verbose(2) << "PropagationVector of base triangle is " << PropagationVector << endl);
     1028        Log() << Verbose(2) << "PropagationVector of base triangle is " << PropagationVector << endl;
    14661029        winner = PointsOnBoundary.end();
    14671030
     
    14691032        for (PointMap::iterator target = PointsOnBoundary.begin(); target != PointsOnBoundary.end(); target++) {
    14701033          if ((target->second != baseline->second->endpoints[0]) && (target->second != baseline->second->endpoints[1])) { // don't take the same endpoints
    1471             DoLog(1) && (Log() << Verbose(1) << "Target point is " << *(target->second) << ":" << endl);
     1034            Log() << Verbose(1) << "Target point is " << *(target->second) << ":" << endl;
    14721035
    14731036            // first check direction, so that triangles don't intersect
     
    14761039            VirtualNormalVector.ProjectOntoPlane(&NormalVector);
    14771040            TempAngle = VirtualNormalVector.Angle(&PropagationVector);
    1478             DoLog(2) && (Log() << Verbose(2) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl);
    1479             if (TempAngle > (M_PI / 2.)) { // no bends bigger than Pi/2 (90 degrees)
    1480               DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl);
     1041            Log() << Verbose(2) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl;
     1042            if (TempAngle > (M_PI/2.)) { // no bends bigger than Pi/2 (90 degrees)
     1043              Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl;
    14811044              continue;
    14821045            } else
    1483               DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl);
     1046              Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl;
    14841047
    14851048            // check first and second endpoint (if any connecting line goes to target has at least not more than 1 triangle)
     
    14871050            LineChecker[1] = baseline->second->endpoints[1]->lines.find(target->first);
    14881051            if (((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[0]->second->triangles.size() == 2))) {
    1489               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);
     1052              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;
    14901053              continue;
    14911054            }
    14921055            if (((LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (LineChecker[1]->second->triangles.size() == 2))) {
    1493               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);
     1056              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;
    14941057              continue;
    14951058            }
     
    14971060            // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint)
    14981061            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)))) {
    1499               DoLog(4) && (Log() << Verbose(4) << "Current target is peak!" << endl);
     1062              Log() << Verbose(4) << "Current target is peak!" << endl;
    15001063              continue;
    15011064            }
     
    15081071            helper.ProjectOntoPlane(&TempVector);
    15091072            if (fabs(helper.NormSquared()) < MYEPSILON) {
    1510               DoLog(2) && (Log() << Verbose(2) << "Chosen set of vectors is linear dependent." << endl);
     1073              Log() << Verbose(2) << "Chosen set of vectors is linear dependent." << endl;
    15111074              continue;
    15121075            }
     
    15181081            TempVector.AddVector(baseline->second->endpoints[1]->node->node);
    15191082            TempVector.AddVector(target->second->node->node);
    1520             TempVector.Scale(1. / 3.);
     1083            TempVector.Scale(1./3.);
    15211084            TempVector.SubtractVector(Center);
    15221085            // make it always point outward
     
    15251088            // calculate angle
    15261089            TempAngle = NormalVector.Angle(&VirtualNormalVector);
    1527             DoLog(2) && (Log() << Verbose(2) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl);
     1090            Log() << Verbose(2) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl;
    15281091            if ((SmallestAngle - TempAngle) > MYEPSILON) { // set to new possible winner
    15291092              SmallestAngle = TempAngle;
    15301093              winner = target;
    1531               DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl);
     1094              Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
    15321095            } else if (fabs(SmallestAngle - TempAngle) < MYEPSILON) { // check the angle to propagation, both possible targets are in one plane! (their normals have same angle)
    15331096              // hence, check the angles to some normal direction from our base line but in this common plane of both targets...
     
    15471110                SmallestAngle = TempAngle;
    15481111                winner = target;
    1549                 DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl);
     1112                Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl;
    15501113              } else
    1551                 DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl);
     1114                Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl;
    15521115            } else
    1553               DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl);
     1116              Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
    15541117          }
    15551118        } // end of loop over all boundary points
     
    15571120        // 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
    15581121        if (winner != PointsOnBoundary.end()) {
    1559           DoLog(0) && (Log() << Verbose(0) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl);
     1122          Log() << Verbose(0) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl;
    15601123          // create the lins of not yet present
    15611124          BLS[0] = baseline->second;
     
    15871150          TrianglesOnBoundaryCount++;
    15881151        } else {
    1589           DoeLog(2) && (eLog() << Verbose(2) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl);
     1152          eLog() << Verbose(2) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl;
    15901153        }
    15911154
    15921155        // 5d. If the set of lines is not yet empty, go to 5. and continue
    15931156      } else
    1594         DoLog(0) && (Log() << Verbose(0) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl);
     1157        Log() << Verbose(0) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl;
    15951158  } while (flag);
    15961159
    15971160  // exit
    1598   delete (Center);
    1599 }
    1600 ;
     1161  delete(Center);
     1162};
    16011163
    16021164/** Inserts all points outside of the tesselated surface into it by adding new triangles.
     
    16081170bool Tesselation::InsertStraddlingPoints(const PointCloud *cloud, const LinkedCell *LC)
    16091171{
    1610   Info FunctionInfo(__func__);
     1172        Info FunctionInfo(__func__);
    16111173  Vector Intersection, Normal;
    16121174  TesselPoint *Walker = NULL;
    16131175  Vector *Center = cloud->GetCenter();
    1614   TriangleList *triangles = NULL;
     1176  list<BoundaryTriangleSet*> *triangles = NULL;
    16151177  bool AddFlag = false;
    16161178  LinkedCell *BoundaryPoints = NULL;
     
    16181180  cloud->GoToFirst();
    16191181  BoundaryPoints = new LinkedCell(this, 5.);
    1620   while (!cloud->IsEnd()) { // we only have to go once through all points, as boundary can become only bigger
     1182  while (!cloud->IsEnd()) {  // we only have to go once through all points, as boundary can become only bigger
    16211183    if (AddFlag) {
    1622       delete (BoundaryPoints);
     1184      delete(BoundaryPoints);
    16231185      BoundaryPoints = new LinkedCell(this, 5.);
    16241186      AddFlag = false;
    16251187    }
    16261188    Walker = cloud->GetPoint();
    1627     DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Walker << "." << endl);
     1189    Log() << Verbose(0) << "Current point is " << *Walker << "." << endl;
    16281190    // get the next triangle
    1629     triangles = FindClosestTrianglesToVector(Walker->node, BoundaryPoints);
     1191    triangles = FindClosestTrianglesToPoint(Walker->node, BoundaryPoints);
    16301192    BTS = triangles->front();
    16311193    if ((triangles == NULL) || (BTS->ContainsBoundaryPoint(Walker))) {
    1632       DoLog(0) && (Log() << Verbose(0) << "No triangles found, probably a tesselation point itself." << endl);
     1194      Log() << Verbose(0) << "No triangles found, probably a tesselation point itself." << endl;
    16331195      cloud->GoToNext();
    16341196      continue;
    16351197    } else {
    16361198    }
    1637     DoLog(0) && (Log() << Verbose(0) << "Closest triangle is " << *BTS << "." << endl);
     1199    Log() << Verbose(0) << "Closest triangle is " << *BTS << "." << endl;
    16381200    // get the intersection point
    16391201    if (BTS->GetIntersectionInsideTriangle(Center, Walker->node, &Intersection)) {
    1640       DoLog(0) && (Log() << Verbose(0) << "We have an intersection at " << Intersection << "." << endl);
     1202      Log() << Verbose(0) << "We have an intersection at " << Intersection << "." << endl;
    16411203      // we have the intersection, check whether in- or outside of boundary
    16421204      if ((Center->DistanceSquared(Walker->node) - Center->DistanceSquared(&Intersection)) < -MYEPSILON) {
    16431205        // inside, next!
    1644         DoLog(0) && (Log() << Verbose(0) << *Walker << " is inside wrt triangle " << *BTS << "." << endl);
     1206        Log() << Verbose(0) << *Walker << " is inside wrt triangle " << *BTS << "." << endl;
    16451207      } else {
    16461208        // outside!
    1647         DoLog(0) && (Log() << Verbose(0) << *Walker << " is outside wrt triangle " << *BTS << "." << endl);
     1209        Log() << Verbose(0) << *Walker << " is outside wrt triangle " << *BTS << "." << endl;
    16481210        class BoundaryLineSet *OldLines[3], *NewLines[3];
    16491211        class BoundaryPointSet *OldPoints[3], *NewPoint;
    16501212        // store the three old lines and old points
    1651         for (int i = 0; i < 3; i++) {
     1213        for (int i=0;i<3;i++) {
    16521214          OldLines[i] = BTS->lines[i];
    16531215          OldPoints[i] = BTS->endpoints[i];
     
    16551217        Normal.CopyVector(&BTS->NormalVector);
    16561218        // add Walker to boundary points
    1657         DoLog(0) && (Log() << Verbose(0) << "Adding " << *Walker << " to BoundaryPoints." << endl);
     1219        Log() << Verbose(0) << "Adding " << *Walker << " to BoundaryPoints." << endl;
    16581220        AddFlag = true;
    1659         if (AddBoundaryPoint(Walker, 0))
     1221        if (AddBoundaryPoint(Walker,0))
    16601222          NewPoint = BPS[0];
    16611223        else
    16621224          continue;
    16631225        // remove triangle
    1664         DoLog(0) && (Log() << Verbose(0) << "Erasing triangle " << *BTS << "." << endl);
     1226        Log() << Verbose(0) << "Erasing triangle " << *BTS << "." << endl;
    16651227        TrianglesOnBoundary.erase(BTS->Nr);
    1666         delete (BTS);
     1228        delete(BTS);
    16671229        // create three new boundary lines
    1668         for (int i = 0; i < 3; i++) {
     1230        for (int i=0;i<3;i++) {
    16691231          BPS[0] = NewPoint;
    16701232          BPS[1] = OldPoints[i];
    16711233          NewLines[i] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1672           DoLog(1) && (Log() << Verbose(1) << "Creating new line " << *NewLines[i] << "." << endl);
     1234          Log() << Verbose(1) << "Creating new line " << *NewLines[i] << "." << endl;
    16731235          LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, NewLines[i])); // no need for check for unique insertion as BPS[0] is definitely a new one
    16741236          LinesOnBoundaryCount++;
    16751237        }
    16761238        // create three new triangle with new point
    1677         for (int i = 0; i < 3; i++) { // find all baselines
     1239        for (int i=0;i<3;i++) { // find all baselines
    16781240          BLS[0] = OldLines[i];
    16791241          int n = 1;
    1680           for (int j = 0; j < 3; j++) {
     1242          for (int j=0;j<3;j++) {
    16811243            if (NewLines[j]->IsConnectedTo(BLS[0])) {
    1682               if (n > 2) {
    1683                 DoeLog(2) && (eLog() << Verbose(2) << BLS[0] << " connects to all of the new lines?!" << endl);
     1244              if (n>2) {
     1245                eLog() << Verbose(2) << BLS[0] << " connects to all of the new lines?!" << endl;
    16841246                return false;
    16851247              } else
     
    16921254          BTS->GetNormalVector(Normal);
    16931255          Normal.Scale(-1.);
    1694           DoLog(0) && (Log() << Verbose(0) << "Created new triangle " << *BTS << "." << endl);
     1256          Log() << Verbose(0) << "Created new triangle " << *BTS << "." << endl;
    16951257          TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
    16961258          TrianglesOnBoundaryCount++;
     
    16981260      }
    16991261    } else { // something is wrong with FindClosestTriangleToPoint!
    1700       DoeLog(1) && (eLog() << Verbose(1) << "The closest triangle did not produce an intersection!" << endl);
     1262      eLog() << Verbose(1) << "The closest triangle did not produce an intersection!" << endl;
    17011263      return false;
    17021264    }
     
    17051267
    17061268  // exit
    1707   delete (Center);
     1269  delete(Center);
    17081270  return true;
    1709 }
    1710 ;
     1271};
    17111272
    17121273/** Adds a point to the tesselation::PointsOnBoundary list.
     
    17171278bool Tesselation::AddBoundaryPoint(TesselPoint * Walker, const int n)
    17181279{
    1719   Info FunctionInfo(__func__);
     1280        Info FunctionInfo(__func__);
    17201281  PointTestPair InsertUnique;
    17211282  BPS[n] = new class BoundaryPointSet(Walker);
     
    17251286    return true;
    17261287  } else {
    1727     delete (BPS[n]);
     1288    delete(BPS[n]);
    17281289    BPS[n] = InsertUnique.first->second;
    17291290    return false;
     
    17391300void Tesselation::AddTesselationPoint(TesselPoint* Candidate, const int n)
    17401301{
    1741   Info FunctionInfo(__func__);
     1302        Info FunctionInfo(__func__);
    17421303  PointTestPair InsertUnique;
    17431304  TPS[n] = new class BoundaryPointSet(Candidate);
     
    17471308  } else {
    17481309    delete TPS[n];
    1749     DoLog(0) && (Log() << Verbose(0) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl);
     1310    Log() << Verbose(0) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl;
    17501311    TPS[n] = (InsertUnique.first)->second;
    17511312  }
     
    17601321void Tesselation::SetTesselationPoint(TesselPoint* Candidate, const int n) const
    17611322{
    1762   Info FunctionInfo(__func__);
     1323        Info FunctionInfo(__func__);
    17631324  PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidate->nr);
    17641325  if (FindPoint != PointsOnBoundary.end())
     
    17661327  else
    17671328    TPS[n] = NULL;
    1768 }
    1769 ;
     1329};
    17701330
    17711331/** Function tries to add line from current Points in BPS to BoundaryLineSet.
    17721332 * If successful it raises the line count and inserts the new line into the BLS,
    17731333 * if unsuccessful, it writes the line which had been present into the BLS, deleting the new constructed one.
    1774  * @param *OptCenter desired OptCenter if there are more than one candidate line
    1775  * @param *candidate third point of the triangle to be, for checking between multiple open line candidates
    17761334 * @param *a first endpoint
    17771335 * @param *b second endpoint
    17781336 * @param n index of Tesselation::BLS giving the line with both endpoints
    17791337 */
    1780 void Tesselation::AddTesselationLine(const Vector * const OptCenter, const BoundaryPointSet * const candidate, class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
    1781 {
     1338void Tesselation::AddTesselationLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n) {
    17821339  bool insertNewLine = true;
    1783   LineMap::iterator FindLine = a->lines.find(b->node->nr);
    1784   BoundaryLineSet *WinningLine = NULL;
    1785   if (FindLine != a->lines.end()) {
    1786     DoLog(1) && (Log() << Verbose(1) << "INFO: There is at least one line between " << *a << " and " << *b << ": " << *(FindLine->second) << "." << endl);
    1787 
    1788     pair<LineMap::iterator, LineMap::iterator> FindPair;
     1340
     1341  if (a->lines.find(b->node->nr) != a->lines.end()) {
     1342    LineMap::iterator FindLine = a->lines.find(b->node->nr);
     1343    pair<LineMap::iterator,LineMap::iterator> FindPair;
    17891344    FindPair = a->lines.equal_range(b->node->nr);
    1790 
    1791     for (FindLine = FindPair.first; (FindLine != FindPair.second) && (insertNewLine); FindLine++) {
    1792       DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl);
     1345    Log() << Verbose(1) << "INFO: There is at least one line between " << *a << " and " << *b << ": " << *(FindLine->second) << "." << endl;
     1346
     1347    for (FindLine = FindPair.first; FindLine != FindPair.second; FindLine++) {
    17931348      // If there is a line with less than two attached triangles, we don't need a new line.
    1794       if (FindLine->second->triangles.size() == 1) {
    1795         CandidateMap::iterator Finder = OpenLines.find(FindLine->second);
    1796         if (!Finder->second->pointlist.empty())
    1797           DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl);
    1798         else
    1799           DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate." << endl);
    1800         // get open line
    1801         for (TesselPointList::const_iterator CandidateChecker = Finder->second->pointlist.begin(); CandidateChecker != Finder->second->pointlist.end(); ++CandidateChecker) {
    1802           if ((*(CandidateChecker) == candidate->node) && (OptCenter == NULL || OptCenter->DistanceSquared(&Finder->second->OptCenter) < MYEPSILON )) { // stop searching if candidate matches
    1803             DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Candidate " << *(*CandidateChecker) << " has the right center " << Finder->second->OptCenter << "." << endl);
    1804             insertNewLine = false;
    1805             WinningLine = FindLine->second;
    1806             break;
    1807           } else {
    1808             DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *(*CandidateChecker) << "'s center " << Finder->second->OptCenter << " does not match desired on " << *OptCenter << "." << endl);
    1809           }
    1810         }
     1349      if (FindLine->second->triangles.size() < 2) {
     1350        insertNewLine = false;
     1351        Log() << Verbose(0) << "Using existing line " << *FindLine->second << endl;
     1352
     1353        BPS[0] = FindLine->second->endpoints[0];
     1354        BPS[1] = FindLine->second->endpoints[1];
     1355        BLS[n] = FindLine->second;
     1356
     1357        // remove existing line from OpenLines
     1358        CandidateMap::iterator CandidateLine = OpenLines.find(BLS[n]);
     1359        delete(CandidateLine->second);
     1360        OpenLines.erase(CandidateLine);
     1361
     1362        break;
    18111363      }
    18121364    }
     
    18141366
    18151367  if (insertNewLine) {
    1816     AddNewTesselationTriangleLine(a, b, n);
    1817   } else {
    1818     AddExistingTesselationTriangleLine(WinningLine, n);
     1368    AlwaysAddTesselationTriangleLine(a, b, n);
    18191369  }
    18201370}
     
    18291379 * @param n index of Tesselation::BLS giving the line with both endpoints
    18301380 */
    1831 void Tesselation::AddNewTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
    1832 {
    1833   Info FunctionInfo(__func__);
    1834   DoLog(0) && (Log() << Verbose(0) << "Adding open line [" << LinesOnBoundaryCount << "|" << *(a->node) << " and " << *(b->node) << "." << endl);
     1381void Tesselation::AlwaysAddTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
     1382{
     1383        Info FunctionInfo(__func__);
     1384  Log() << Verbose(0) << "Adding open line [" << LinesOnBoundaryCount << "|" << *(a->node) << " and " << *(b->node) << "." << endl;
    18351385  BPS[0] = a;
    18361386  BPS[1] = b;
    1837   BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); // this also adds the line to the local maps
     1387  BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);  // this also adds the line to the local maps
    18381388  // add line to global map
    18391389  LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n]));
     
    18421392  // also add to open lines
    18431393  CandidateForTesselation *CFT = new CandidateForTesselation(BLS[n]);
    1844   OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (BLS[n], CFT));
    1845 }
    1846 ;
    1847 
    1848 /** Uses an existing line for a new triangle.
    1849  * Sets Tesselation::BLS[\a n] and removes the lines from Tesselation::OpenLines.
    1850  * \param *FindLine the line to add
    1851  * \param n index of the line to set in Tesselation::BLS
    1852  */
    1853 void Tesselation::AddExistingTesselationTriangleLine(class BoundaryLineSet *Line, int n)
    1854 {
    1855   Info FunctionInfo(__func__);
    1856   DoLog(0) && (Log() << Verbose(0) << "Using existing line " << *Line << endl);
    1857 
    1858   // set endpoints and line
    1859   BPS[0] = Line->endpoints[0];
    1860   BPS[1] = Line->endpoints[1];
    1861   BLS[n] = Line;
    1862   // remove existing line from OpenLines
    1863   CandidateMap::iterator CandidateLine = OpenLines.find(BLS[n]);
    1864   if (CandidateLine != OpenLines.end()) {
    1865     DoLog(1) && (Log() << Verbose(1) << " Removing line from OpenLines." << endl);
    1866     delete (CandidateLine->second);
    1867     OpenLines.erase(CandidateLine);
    1868   } else {
    1869     DoeLog(1) && (eLog() << Verbose(1) << "Line exists and is attached to less than two triangles, but not in OpenLines!" << endl);
    1870   }
    1871 }
    1872 ;
     1394  OpenLines.insert(pair< BoundaryLineSet *, CandidateForTesselation *> (BLS[n], CFT));
     1395};
    18731396
    18741397/** Function adds triangle to global list.
     
    18771400void Tesselation::AddTesselationTriangle()
    18781401{
    1879   Info FunctionInfo(__func__);
    1880   DoLog(1) && (Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl);
     1402        Info FunctionInfo(__func__);
     1403  Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl;
    18811404
    18821405  // add triangle to global map
     
    18881411
    18891412  // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
    1890 }
    1891 ;
     1413};
    18921414
    18931415/** Function adds triangle to global list.
     
    18971419void Tesselation::AddTesselationTriangle(const int nr)
    18981420{
    1899   Info FunctionInfo(__func__);
    1900   DoLog(0) && (Log() << Verbose(0) << "Adding triangle to global TrianglesOnBoundary map." << endl);
     1421        Info FunctionInfo(__func__);
     1422  Log() << Verbose(0) << "Adding triangle to global TrianglesOnBoundary map." << endl;
    19011423
    19021424  // add triangle to global map
     
    19071429
    19081430  // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
    1909 }
    1910 ;
     1431};
    19111432
    19121433/** Removes a triangle from the tesselation.
     
    19171438void Tesselation::RemoveTesselationTriangle(class BoundaryTriangleSet *triangle)
    19181439{
    1919   Info FunctionInfo(__func__);
     1440        Info FunctionInfo(__func__);
    19201441  if (triangle == NULL)
    19211442    return;
    19221443  for (int i = 0; i < 3; i++) {
    19231444    if (triangle->lines[i] != NULL) {
    1924       DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl);
     1445      Log() << Verbose(0) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl;
    19251446      triangle->lines[i]->triangles.erase(triangle->Nr);
    19261447      if (triangle->lines[i]->triangles.empty()) {
    1927         DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl);
    1928         RemoveTesselationLine(triangle->lines[i]);
     1448          Log() << Verbose(0) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl;
     1449          RemoveTesselationLine(triangle->lines[i]);
    19291450      } else {
    1930         DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is still attached to another triangle: ");
    1931         OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (triangle->lines[i], NULL));
    1932         for (TriangleMap::iterator TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); TriangleRunner++)
    1933           DoLog(0) && (Log() << Verbose(0) << "[" << (TriangleRunner->second)->Nr << "|" << *((TriangleRunner->second)->endpoints[0]) << ", " << *((TriangleRunner->second)->endpoints[1]) << ", " << *((TriangleRunner->second)->endpoints[2]) << "] \t");
    1934         DoLog(0) && (Log() << Verbose(0) << endl);
    1935         //        for (int j=0;j<2;j++) {
    1936         //          Log() << Verbose(0) << "Lines of endpoint " << *(triangle->lines[i]->endpoints[j]) << ": ";
    1937         //          for(LineMap::iterator LineRunner = triangle->lines[i]->endpoints[j]->lines.begin(); LineRunner != triangle->lines[i]->endpoints[j]->lines.end(); LineRunner++)
    1938         //            Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
    1939         //          Log() << Verbose(0) << endl;
    1940         //        }
     1451        Log() << Verbose(0) << *triangle->lines[i] << " is still attached to another triangle: ";
     1452        for(TriangleMap::iterator TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); TriangleRunner++)
     1453          Log() << Verbose(0) << "[" << (TriangleRunner->second)->Nr << "|" << *((TriangleRunner->second)->endpoints[0]) << ", " << *((TriangleRunner->second)->endpoints[1]) << ", " << *((TriangleRunner->second)->endpoints[2]) << "] \t";
     1454        Log() << Verbose(0) << endl;
     1455//        for (int j=0;j<2;j++) {
     1456//          Log() << Verbose(0) << "Lines of endpoint " << *(triangle->lines[i]->endpoints[j]) << ": ";
     1457//          for(LineMap::iterator LineRunner = triangle->lines[i]->endpoints[j]->lines.begin(); LineRunner != triangle->lines[i]->endpoints[j]->lines.end(); LineRunner++)
     1458//            Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
     1459//          Log() << Verbose(0) << endl;
     1460//        }
    19411461      }
    1942       triangle->lines[i] = NULL; // free'd or not: disconnect
     1462      triangle->lines[i] = NULL;  // free'd or not: disconnect
    19431463    } else
    1944       DoeLog(1) && (eLog() << Verbose(1) << "This line " << i << " has already been free'd." << endl);
     1464      eLog() << Verbose(1) << "This line " << i << " has already been free'd." << endl;
    19451465  }
    19461466
    19471467  if (TrianglesOnBoundary.erase(triangle->Nr))
    1948     DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr. " << triangle->Nr << "." << endl);
    1949   delete (triangle);
    1950 }
    1951 ;
     1468    Log() << Verbose(0) << "Removing triangle Nr. " << triangle->Nr << "." << endl;
     1469  delete(triangle);
     1470};
    19521471
    19531472/** Removes a line from the tesselation.
     
    19571476void Tesselation::RemoveTesselationLine(class BoundaryLineSet *line)
    19581477{
    1959   Info FunctionInfo(__func__);
     1478        Info FunctionInfo(__func__);
    19601479  int Numbers[2];
    19611480
     
    19781497        for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
    19791498          if ((*Runner).second == line) {
    1980             DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl);
     1499            Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
    19811500            line->endpoints[i]->lines.erase(Runner);
    19821501            break;
     
    19841503      } else { // there's just a single line left
    19851504        if (line->endpoints[i]->lines.erase(line->Nr))
    1986           DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl);
     1505          Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
    19871506      }
    19881507      if (line->endpoints[i]->lines.empty()) {
    1989         DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl);
     1508        Log() << Verbose(0) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl;
    19901509        RemoveTesselationPoint(line->endpoints[i]);
    19911510      } else {
    1992         DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has still lines it's attached to: ");
    1993         for (LineMap::iterator LineRunner = line->endpoints[i]->lines.begin(); LineRunner != line->endpoints[i]->lines.end(); LineRunner++)
    1994           DoLog(0) && (Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t");
    1995         DoLog(0) && (Log() << Verbose(0) << endl);
     1511        Log() << Verbose(0) << *line->endpoints[i] << " has still lines it's attached to: ";
     1512        for(LineMap::iterator LineRunner = line->endpoints[i]->lines.begin(); LineRunner != line->endpoints[i]->lines.end(); LineRunner++)
     1513          Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
     1514        Log() << Verbose(0) << endl;
    19961515      }
    1997       line->endpoints[i] = NULL; // free'd or not: disconnect
     1516      line->endpoints[i] = NULL;  // free'd or not: disconnect
    19981517    } else
    1999       DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << i << " has already been free'd." << endl);
     1518      eLog() << Verbose(1) << "Endpoint " << i << " has already been free'd." << endl;
    20001519  }
    20011520  if (!line->triangles.empty())
    2002     DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *line << " am still connected to some triangles." << endl);
     1521    eLog() << Verbose(2) << "Memory Leak! I " << *line << " am still connected to some triangles." << endl;
    20031522
    20041523  if (LinesOnBoundary.erase(line->Nr))
    2005     DoLog(0) && (Log() << Verbose(0) << "Removing line Nr. " << line->Nr << "." << endl);
    2006   delete (line);
    2007 }
    2008 ;
     1524    Log() << Verbose(0) << "Removing line Nr. " << line->Nr << "." << endl;
     1525  delete(line);
     1526};
    20091527
    20101528/** Removes a point from the tesselation.
     
    20151533void Tesselation::RemoveTesselationPoint(class BoundaryPointSet *point)
    20161534{
    2017   Info FunctionInfo(__func__);
     1535        Info FunctionInfo(__func__);
    20181536  if (point == NULL)
    20191537    return;
    20201538  if (PointsOnBoundary.erase(point->Nr))
    2021     DoLog(0) && (Log() << Verbose(0) << "Removing point Nr. " << point->Nr << "." << endl);
    2022   delete (point);
    2023 }
    2024 ;
    2025 
    2026 /** Checks validity of a given sphere of a candidate line.
    2027  * \sa CandidateForTesselation::CheckValidity(), which is more evolved.
    2028  * We check CandidateForTesselation::OtherOptCenter
    2029  * \param &CandidateLine contains other degenerated candidates which we have to subtract as well
    2030  * \param RADIUS radius of sphere
    2031  * \param *LC LinkedCell structure with other atoms
    2032  * \return true - candidate triangle is degenerated, false - candidate triangle is not degenerated
    2033  */
    2034 bool Tesselation::CheckDegeneracy(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC) const
    2035 {
    2036   Info FunctionInfo(__func__);
    2037 
    2038   DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl);
    2039   bool flag = true;
    2040 
    2041   DoLog(1) && (Log() << Verbose(1) << "Check by: draw sphere {" << CandidateLine.OtherOptCenter.x[0] << " " << CandidateLine.OtherOptCenter.x[1] << " " << CandidateLine.OtherOptCenter.x[2] << "} radius " << RADIUS << " resolution 30" << endl);
    2042   // get all points inside the sphere
    2043   TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, &CandidateLine.OtherOptCenter);
    2044 
    2045   DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << CandidateLine.OtherOptCenter << ":" << endl);
    2046   for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
    2047     DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->Distance(&CandidateLine.OtherOptCenter) << "." << endl);
    2048 
    2049   // remove triangles's endpoints
    2050   for (int i = 0; i < 2; i++)
    2051     ListofPoints->remove(CandidateLine.BaseLine->endpoints[i]->node);
    2052 
    2053   // remove other candidates
    2054   for (TesselPointList::const_iterator Runner = CandidateLine.pointlist.begin(); Runner != CandidateLine.pointlist.end(); ++Runner)
    2055     ListofPoints->remove(*Runner);
    2056 
    2057   // check for other points
    2058   if (!ListofPoints->empty()) {
    2059     DoLog(1) && (Log() << Verbose(1) << "CheckDegeneracy: There are still " << ListofPoints->size() << " points inside the sphere." << endl);
    2060     flag = false;
    2061     DoLog(1) && (Log() << Verbose(1) << "External atoms inside of sphere at " << CandidateLine.OtherOptCenter << ":" << endl);
    2062     for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
    2063       DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->Distance(&CandidateLine.OtherOptCenter) << "." << endl);
    2064   }
    2065   delete (ListofPoints);
    2066 
    2067   return flag;
    2068 }
    2069 ;
     1539    Log() << Verbose(0) << "Removing point Nr. " << point->Nr << "." << endl;
     1540  delete(point);
     1541};
    20701542
    20711543/** Checks whether the triangle consisting of the three points is already present.
     
    20801552int Tesselation::CheckPresenceOfTriangle(TesselPoint *Candidates[3]) const
    20811553{
    2082   Info FunctionInfo(__func__);
     1554        Info FunctionInfo(__func__);
    20831555  int adjacentTriangleCount = 0;
    20841556  class BoundaryPointSet *Points[3];
     
    21021574          for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) {
    21031575            TriangleMap *triangles = &FindLine->second->triangles;
    2104             DoLog(1) && (Log() << Verbose(1) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl);
     1576            Log() << Verbose(1) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl;
    21051577            for (TriangleMap::const_iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) {
    21061578              if (FindTriangle->second->IsPresentTupel(Points)) {
     
    21081580              }
    21091581            }
    2110             DoLog(1) && (Log() << Verbose(1) << "end." << endl);
     1582            Log() << Verbose(1) << "end." << endl;
    21111583          }
    21121584          // Only one of the triangle lines must be considered for the triangle count.
     
    21181590  }
    21191591
    2120   DoLog(0) && (Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl);
     1592  Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
    21211593  return adjacentTriangleCount;
    2122 }
    2123 ;
     1594};
    21241595
    21251596/** Checks whether the triangle consisting of the three points is already present.
     
    21331604class BoundaryTriangleSet * Tesselation::GetPresentTriangle(TesselPoint *Candidates[3])
    21341605{
    2135   Info FunctionInfo(__func__);
     1606        Info FunctionInfo(__func__);
    21361607  class BoundaryTriangleSet *triangle = NULL;
    21371608  class BoundaryPointSet *Points[3];
     
    21711642
    21721643  return triangle;
    2173 }
    2174 ;
     1644};
     1645
    21751646
    21761647/** Finds the starting triangle for FindNonConvexBorder().
     
    21811652 * \param RADIUS radius of virtual rolling sphere
    21821653 * \param *LC LinkedCell structure with neighbouring TesselPoint's
    2183  * \return true - a starting triangle has been created, false - no valid triple of points found
    2184  */
    2185 bool Tesselation::FindStartingTriangle(const double RADIUS, const LinkedCell *LC)
    2186 {
    2187   Info FunctionInfo(__func__);
     1654 */
     1655void Tesselation::FindStartingTriangle(const double RADIUS, const LinkedCell *LC)
     1656{
     1657        Info FunctionInfo(__func__);
    21881658  int i = 0;
    21891659  TesselPoint* MaxPoint[NDIM];
    21901660  TesselPoint* Temporary;
    21911661  double maxCoordinate[NDIM];
    2192   BoundaryLineSet *BaseLine = NULL;
     1662  BoundaryLineSet BaseLine;
     1663  Vector Oben;
    21931664  Vector helper;
    21941665  Vector Chord;
    21951666  Vector SearchDirection;
    2196   Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
    2197   Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
    2198   Vector SphereCenter;
    2199   Vector NormalVector;
    2200 
    2201   NormalVector.Zero();
     1667
     1668  Oben.Zero();
    22021669
    22031670  for (i = 0; i < 3; i++) {
     
    22071674
    22081675  // 1. searching topmost point with respect to each axis
    2209   for (int i = 0; i < NDIM; i++) { // each axis
    2210     LC->n[i] = LC->N[i] - 1; // current axis is topmost cell
    2211     for (LC->n[(i + 1) % NDIM] = 0; LC->n[(i + 1) % NDIM] < LC->N[(i + 1) % NDIM]; LC->n[(i + 1) % NDIM]++)
    2212       for (LC->n[(i + 2) % NDIM] = 0; LC->n[(i + 2) % NDIM] < LC->N[(i + 2) % NDIM]; LC->n[(i + 2) % NDIM]++) {
    2213         const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
     1676  for (int i=0;i<NDIM;i++) { // each axis
     1677    LC->n[i] = LC->N[i]-1; // current axis is topmost cell
     1678    for (LC->n[(i+1)%NDIM]=0;LC->n[(i+1)%NDIM]<LC->N[(i+1)%NDIM];LC->n[(i+1)%NDIM]++)
     1679      for (LC->n[(i+2)%NDIM]=0;LC->n[(i+2)%NDIM]<LC->N[(i+2)%NDIM];LC->n[(i+2)%NDIM]++) {
     1680        const LinkedNodes *List = LC->GetCurrentCell();
    22141681        //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
    22151682        if (List != NULL) {
    2216           for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
     1683          for (LinkedNodes::const_iterator Runner = List->begin();Runner != List->end();Runner++) {
    22171684            if ((*Runner)->node->x[i] > maxCoordinate[i]) {
    2218               DoLog(1) && (Log() << Verbose(1) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl);
     1685              Log() << Verbose(1) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl;
    22191686              maxCoordinate[i] = (*Runner)->node->x[i];
    22201687              MaxPoint[i] = (*Runner);
     
    22221689          }
    22231690        } else {
    2224           DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
     1691          eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl;
    22251692        }
    22261693      }
    22271694  }
    22281695
    2229   DoLog(1) && (Log() << Verbose(1) << "Found maximum coordinates: ");
    2230   for (int i = 0; i < NDIM; i++)
    2231     DoLog(0) && (Log() << Verbose(0) << i << ": " << *MaxPoint[i] << "\t");
    2232   DoLog(0) && (Log() << Verbose(0) << endl);
     1696  Log() << Verbose(1) << "Found maximum coordinates: ";
     1697  for (int i=0;i<NDIM;i++)
     1698    Log() << Verbose(0) << i << ": " << *MaxPoint[i] << "\t";
     1699  Log() << Verbose(0) << endl;
    22331700
    22341701  BTS = NULL;
    2235   for (int k = 0; k < NDIM; k++) {
    2236     NormalVector.Zero();
    2237     NormalVector.x[k] = 1.;
    2238     BaseLine = new BoundaryLineSet();
    2239     BaseLine->endpoints[0] = new BoundaryPointSet(MaxPoint[k]);
    2240     DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl);
     1702  for (int k=0;k<NDIM;k++) {
     1703    Oben.Zero();
     1704    Oben.x[k] = 1.;
     1705    BaseLine.endpoints[0] = new BoundaryPointSet(MaxPoint[k]);
     1706    Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine.endpoints[0]->node << "." << endl;
    22411707
    22421708    double ShortestAngle;
    22431709    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.
    22441710
    2245     Temporary = NULL;
    2246     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_...
    2247     if (Temporary == NULL) {
    2248       // have we found a second point?
    2249       delete BaseLine;
     1711    FindSecondPointForTesselation(BaseLine.endpoints[0]->node, Oben, Temporary, &ShortestAngle, RADIUS, LC); // we give same point as next candidate as its bonds are looked into in find_second_...
     1712    if (Temporary == NULL)  // have we found a second point?
    22501713      continue;
    2251     }
    2252     BaseLine->endpoints[1] = new BoundaryPointSet(Temporary);
    2253 
    2254     // construct center of circle
    2255     CircleCenter.CopyVector(BaseLine->endpoints[0]->node->node);
    2256     CircleCenter.AddVector(BaseLine->endpoints[1]->node->node);
    2257     CircleCenter.Scale(0.5);
    2258 
    2259     // construct normal vector of circle
    2260     CirclePlaneNormal.CopyVector(BaseLine->endpoints[0]->node->node);
    2261     CirclePlaneNormal.SubtractVector(BaseLine->endpoints[1]->node->node);
    2262 
    2263     double radius = CirclePlaneNormal.NormSquared();
    2264     double CircleRadius = sqrt(RADIUS * RADIUS - radius / 4.);
    2265 
    2266     NormalVector.ProjectOntoPlane(&CirclePlaneNormal);
    2267     NormalVector.Normalize();
    2268     ShortestAngle = 2. * M_PI; // This will indicate the quadrant.
    2269 
    2270     SphereCenter.CopyVector(&NormalVector);
    2271     SphereCenter.Scale(CircleRadius);
    2272     SphereCenter.AddVector(&CircleCenter);
    2273     // Now, NormalVector and SphereCenter are two orthonormalized vectors in the plane defined by CirclePlaneNormal (not normalized)
     1714    BaseLine.endpoints[1] = new BoundaryPointSet(Temporary);
     1715
     1716    helper.CopyVector(BaseLine.endpoints[0]->node->node);
     1717    helper.SubtractVector(BaseLine.endpoints[1]->node->node);
     1718    helper.Normalize();
     1719    Oben.ProjectOntoPlane(&helper);
     1720    Oben.Normalize();
     1721    helper.VectorProduct(&Oben);
     1722    ShortestAngle = 2.*M_PI; // This will indicate the quadrant.
     1723
     1724    Chord.CopyVector(BaseLine.endpoints[0]->node->node); // bring into calling function
     1725    Chord.SubtractVector(BaseLine.endpoints[1]->node->node);
     1726    double radius = Chord.ScalarProduct(&Chord);
     1727    double CircleRadius = sqrt(RADIUS*RADIUS - radius/4.);
     1728    helper.CopyVector(&Oben);
     1729    helper.Scale(CircleRadius);
     1730    // Now, oben and helper are two orthonormalized vectors in the plane defined by Chord (not normalized)
    22741731
    22751732    // look in one direction of baseline for initial candidate
    2276     SearchDirection.MakeNormalVector(&CirclePlaneNormal, &NormalVector); // whether we look "left" first or "right" first is not important ...
     1733    SearchDirection.MakeNormalVector(&Chord, &Oben); // whether we look "left" first or "right" first is not important ...
    22771734
    22781735    // adding point 1 and point 2 and add the line between them
    2279     DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl);
    2280     DoLog(0) && (Log() << Verbose(0) << "Found second point is at " << *BaseLine->endpoints[1]->node << ".\n");
     1736    Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine.endpoints[0]->node << "." << endl;
     1737    Log() << Verbose(0) << "Found second point is at " << *BaseLine.endpoints[1]->node << ".\n";
    22811738
    22821739    //Log() << Verbose(1) << "INFO: OldSphereCenter is at " << helper << ".\n";
    2283     CandidateForTesselation OptCandidates(BaseLine);
    2284     FindThirdPointForTesselation(NormalVector, SearchDirection, SphereCenter, OptCandidates, NULL, RADIUS, LC);
    2285     DoLog(0) && (Log() << Verbose(0) << "List of third Points is:" << endl);
     1740    CandidateForTesselation OptCandidates(&BaseLine);
     1741    FindThirdPointForTesselation(Oben, SearchDirection, helper, OptCandidates, NULL, RADIUS, LC);
     1742    Log() << Verbose(0) << "List of third Points is:" << endl;
    22861743    for (TesselPointList::iterator it = OptCandidates.pointlist.begin(); it != OptCandidates.pointlist.end(); it++) {
    2287       DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl);
    2288     }
    2289     if (!OptCandidates.pointlist.empty()) {
    2290       BTS = NULL;
    2291       AddCandidatePolygon(OptCandidates, RADIUS, LC);
    2292     } else {
    2293       delete BaseLine;
    2294       continue;
    2295     }
    2296 
    2297     if (BTS != NULL) { // we have created one starting triangle
    2298       delete BaseLine;
     1744        Log() << Verbose(0) << " " << *(*it) << endl;
     1745    }
     1746
     1747    BTS = NULL;
     1748    AddCandidateTriangle(OptCandidates);
     1749//    delete(BaseLine.endpoints[0]);
     1750//    delete(BaseLine.endpoints[1]);
     1751
     1752    if (BTS != NULL) // we have created one starting triangle
    22991753      break;
    2300     } else {
     1754    else {
    23011755      // remove all candidates from the list and then the list itself
    23021756      OptCandidates.pointlist.clear();
    23031757    }
    2304     delete BaseLine;
    2305   }
    2306 
    2307   return (BTS != NULL);
    2308 }
    2309 ;
     1758  }
     1759};
    23101760
    23111761/** Checks for a given baseline and a third point candidate whether baselines of the found triangle don't have even better candidates.
     
    23781828//            if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
    23791829//              // rotated the wrong way!
    2380 //              DoeLog(1) && (eLog()<< Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl);
     1830//              eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl;
    23811831//            }
    23821832//
     
    24351885//          }
    24361886//        } else {
    2437 //          DoeLog(2) && (eLog()<< Verbose(2) << "Baseline is connected to two triangles already?" << endl);
     1887//          eLog() << Verbose(2) << "Baseline is connected to two triangles already?" << endl;
    24381888//        }
    24391889//      } else {
     
    24421892//    }
    24431893//  } else {
    2444 //    DoeLog(1) && (eLog()<< Verbose(1) << "Could not find the TesselPoint " << *ThirdNode << "." << endl);
     1894//    eLog() << Verbose(1) << "Could not find the TesselPoint " << *ThirdNode << "." << endl;
    24451895//  }
    24461896//
     
    24561906 * @param *LC LinkedCell structure with neighbouring points
    24571907 */
    2458 bool Tesselation::FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, const BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC)
    2459 {
    2460   Info FunctionInfo(__func__);
     1908bool Tesselation::FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC)
     1909{
     1910        Info FunctionInfo(__func__);
     1911  bool result = true;
     1912
    24611913  Vector CircleCenter;
    24621914  Vector CirclePlaneNormal;
    2463   Vector RelativeSphereCenter;
     1915  Vector OldSphereCenter;
    24641916  Vector SearchDirection;
    24651917  Vector helper;
    2466   BoundaryPointSet *ThirdPoint = NULL;
     1918  TesselPoint *ThirdNode = NULL;
    24671919  LineMap::iterator testline;
    24681920  double radius, CircleRadius;
    24691921
    2470   for (int i = 0; i < 3; i++)
    2471     if ((T.endpoints[i] != CandidateLine.BaseLine->endpoints[0]) && (T.endpoints[i] != CandidateLine.BaseLine->endpoints[1])) {
    2472       ThirdPoint = T.endpoints[i];
    2473       break;
    2474     }
    2475   DoLog(0) && (Log() << Verbose(0) << "Current baseline is " << *CandidateLine.BaseLine << " with ThirdPoint " << *ThirdPoint << " of triangle " << T << "." << endl);
    2476 
    2477   CandidateLine.T = &T;
     1922  Log() << Verbose(0) << "Current baseline is " << *CandidateLine.BaseLine << " of triangle " << T << "." << endl;
     1923  for (int i=0;i<3;i++)
     1924    if ((T.endpoints[i]->node != CandidateLine.BaseLine->endpoints[0]->node) && (T.endpoints[i]->node != CandidateLine.BaseLine->endpoints[1]->node))
     1925      ThirdNode = T.endpoints[i]->node;
    24781926
    24791927  // construct center of circle
     
    24881936  // calculate squared radius of circle
    24891937  radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
    2490   if (radius / 4. < RADIUS * RADIUS) {
    2491     // construct relative sphere center with now known CircleCenter
    2492     RelativeSphereCenter.CopyVector(&T.SphereCenter);
    2493     RelativeSphereCenter.SubtractVector(&CircleCenter);
    2494 
    2495     CircleRadius = RADIUS * RADIUS - radius / 4.;
     1938  if (radius/4. < RADIUS*RADIUS) {
     1939    CircleRadius = RADIUS*RADIUS - radius/4.;
    24961940    CirclePlaneNormal.Normalize();
    2497     DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
    2498 
    2499     DoLog(1) && (Log() << Verbose(1) << "INFO: OldSphereCenter is at " << T.SphereCenter << "." << endl);
    2500 
    2501     // construct SearchDirection and an "outward pointer"
    2502     SearchDirection.MakeNormalVector(&RelativeSphereCenter, &CirclePlaneNormal);
    2503     helper.CopyVector(&CircleCenter);
    2504     helper.SubtractVector(ThirdPoint->node->node);
     1941    Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
     1942
     1943    // construct old center
     1944    GetCenterofCircumcircle(&OldSphereCenter, *T.endpoints[0]->node->node, *T.endpoints[1]->node->node, *T.endpoints[2]->node->node);
     1945    helper.CopyVector(&T.NormalVector);  // normal vector ensures that this is correct center of the two possible ones
     1946    radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(&OldSphereCenter);
     1947    helper.Scale(sqrt(RADIUS*RADIUS - radius));
     1948    OldSphereCenter.AddVector(&helper);
     1949    OldSphereCenter.SubtractVector(&CircleCenter);
     1950    Log() << Verbose(1) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl;
     1951
     1952    // construct SearchDirection
     1953    SearchDirection.MakeNormalVector(&T.NormalVector, &CirclePlaneNormal);
     1954    helper.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
     1955    helper.SubtractVector(ThirdNode->node);
    25051956    if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
    25061957      SearchDirection.Scale(-1.);
    2507     DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
    2508     if (fabs(RelativeSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
     1958    SearchDirection.ProjectOntoPlane(&OldSphereCenter);
     1959    SearchDirection.Normalize();
     1960    Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
     1961    if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
    25091962      // rotated the wrong way!
    2510       DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl);
     1963      eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl;
    25111964    }
    25121965
    25131966    // add third point
    2514     FindThirdPointForTesselation(T.NormalVector, SearchDirection, T.SphereCenter, CandidateLine, ThirdPoint, RADIUS, LC);
     1967    FindThirdPointForTesselation(T.NormalVector, SearchDirection, OldSphereCenter, CandidateLine, ThirdNode, RADIUS, LC);
    25151968
    25161969  } else {
    2517     DoLog(0) && (Log() << Verbose(0) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and base triangle " << T << " is too big!" << endl);
     1970    Log() << Verbose(0) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and base triangle " << T << " is too big!" << endl;
    25181971  }
    25191972
    25201973  if (CandidateLine.pointlist.empty()) {
    2521     DoeLog(2) && (eLog() << Verbose(2) << "Could not find a suitable candidate." << endl);
     1974    eLog() << Verbose(2) << "Could not find a suitable candidate." << endl;
    25221975    return false;
    25231976  }
    2524   DoLog(0) && (Log() << Verbose(0) << "Third Points are: " << endl);
     1977  Log() << Verbose(0) << "Third Points are: " << endl;
    25251978  for (TesselPointList::iterator it = CandidateLine.pointlist.begin(); it != CandidateLine.pointlist.end(); ++it) {
    2526     DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl);
     1979    Log() << Verbose(0) << " " << *(*it) << endl;
    25271980  }
    25281981
    25291982  return true;
    2530 }
    2531 ;
    2532 
    2533 /** Walks through Tesselation::OpenLines() and finds candidates for newly created ones.
    2534  * \param *&LCList atoms in LinkedCell list
    2535  * \param RADIUS radius of the virtual sphere
    2536  * \return true - for all open lines without candidates so far, a candidate has been found,
    2537  *         false - at least one open line without candidate still
    2538  */
    2539 bool Tesselation::FindCandidatesforOpenLines(const double RADIUS, const LinkedCell *&LCList)
    2540 {
    2541   bool TesselationFailFlag = true;
    2542   CandidateForTesselation *baseline = NULL;
    2543   BoundaryTriangleSet *T = NULL;
    2544 
    2545   for (CandidateMap::iterator Runner = OpenLines.begin(); Runner != OpenLines.end(); Runner++) {
    2546     baseline = Runner->second;
    2547     if (baseline->pointlist.empty()) {
    2548       assert((baseline->BaseLine->triangles.size() == 1) && ("Open line without exactly one attached triangle"));
    2549       T = (((baseline->BaseLine->triangles.begin()))->second);
    2550       DoLog(1) && (Log() << Verbose(1) << "Finding best candidate for open line " << *baseline->BaseLine << " of triangle " << *T << endl);
    2551       TesselationFailFlag = TesselationFailFlag && FindNextSuitableTriangle(*baseline, *T, RADIUS, LCList); //the line is there, so there is a triangle, but only one.
    2552     }
    2553   }
    2554   return TesselationFailFlag;
    2555 }
    2556 ;
     1983
     1984//  BoundaryLineSet *BaseRay = CandidateLine.BaseLine;
     1985//  for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
     1986//    Log() << Verbose(0) << "Third point candidate is " << *(*it)->point
     1987//    << " with circumsphere's center at " << (*it)->OptCenter << "." << endl;
     1988//    Log() << Verbose(0) << "Baseline is " << *BaseRay << endl;
     1989//
     1990//    // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2)
     1991//    TesselPoint *PointCandidates[3];
     1992//    PointCandidates[0] = (*it)->point;
     1993//    PointCandidates[1] = BaseRay->endpoints[0]->node;
     1994//    PointCandidates[2] = BaseRay->endpoints[1]->node;
     1995//    int existentTrianglesCount = CheckPresenceOfTriangle(PointCandidates);
     1996//
     1997//    BTS = NULL;
     1998//    // check for present edges and whether we reach better candidates from them
     1999//    //if (HasOtherBaselineBetterCandidate(BaseRay, (*it)->point, ShortestAngle, RADIUS, LC) ) {
     2000//    if (0) {
     2001//      result = false;
     2002//      break;
     2003//    } else {
     2004//      // If there is no triangle, add it regularly.
     2005//      if (existentTrianglesCount == 0) {
     2006//        AddTesselationPoint((*it)->point, 0);
     2007//        AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
     2008//        AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
     2009//
     2010//        if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const )TPS)) {
     2011//          CandidateLine.point = (*it)->point;
     2012//          CandidateLine.OptCenter.CopyVector(&((*it)->OptCenter));
     2013//          CandidateLine.OtherOptCenter.CopyVector(&((*it)->OtherOptCenter));
     2014//          CandidateLine.ShortestAngle = ShortestAngle;
     2015//        } else {
     2016////          eLog() << Verbose(1) << "This triangle consisting of ";
     2017////          Log() << Verbose(0) << *(*it)->point << ", ";
     2018////          Log() << Verbose(0) << *BaseRay->endpoints[0]->node << " and ";
     2019////          Log() << Verbose(0) << *BaseRay->endpoints[1]->node << " ";
     2020////          Log() << Verbose(0) << "exists and is not added, as it 0x80000000006fc150(does not seem helpful!" << endl;
     2021//          result = false;
     2022//        }
     2023//      } else if ((existentTrianglesCount >= 1) && (existentTrianglesCount <= 3)) { // If there is a planar region within the structure, we need this triangle a second time.
     2024//          AddTesselationPoint((*it)->point, 0);
     2025//          AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
     2026//          AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
     2027//
     2028//          // 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)
     2029//          // i.e. at least one of the three lines must be present with TriangleCount <= 1
     2030//          if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const)TPS) || CandidateLine.BaseLine->skipped) {
     2031//            CandidateLine.point = (*it)->point;
     2032//            CandidateLine.OptCenter.CopyVector(&(*it)->OptCenter);
     2033//            CandidateLine.OtherOptCenter.CopyVector(&(*it)->OtherOptCenter);
     2034//            CandidateLine.ShortestAngle = ShortestAngle+2.*M_PI;
     2035//
     2036//          } else {
     2037////            eLog() << Verbose(1) << "This triangle consisting of " << *(*it)->point << ", " << *BaseRay->endpoints[0]->node << " and " << *BaseRay->endpoints[1]->node << " " << "exists and is not added, as it does not seem helpful!" << endl;
     2038//            result = false;
     2039//          }
     2040//      } else {
     2041////        Log() << Verbose(1) << "This triangle consisting of ";
     2042////        Log() << Verbose(0) << *(*it)->point << ", ";
     2043////        Log() << Verbose(0) << *BaseRay->endpoints[0]->node << " and ";
     2044////        Log() << Verbose(0) << *BaseRay->endpoints[1]->node << " ";
     2045////        Log() << Verbose(0) << "is invalid!" << endl;
     2046//        result = false;
     2047//      }
     2048//    }
     2049//
     2050//    // set baseline to new ray from ref point (here endpoints[0]->node) to current candidate (here (*it)->point))
     2051//    BaseRay = BLS[0];
     2052//    if ((BTS != NULL) && (BTS->NormalVector.NormSquared() < MYEPSILON)) {
     2053//      eLog() << Verbose(1) << "Triangle " << *BTS << " has zero normal vector!" << endl;
     2054//      exit(255);
     2055//    }
     2056//
     2057//  }
     2058//
     2059//  // remove all candidates from the list and then the list itself
     2060//  class CandidateForTesselation *remover = NULL;
     2061//  for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
     2062//    remover = *it;
     2063//    delete(remover);
     2064//  }
     2065//  delete(OptCandidates);
     2066  return result;
     2067};
    25572068
    25582069/** Adds the present line and candidate point from \a &CandidateLine to the Tesselation.
    25592070 * \param CandidateLine triangle to add
    2560  * \param RADIUS Radius of sphere
    2561  * \param *LC LinkedCell structure
    2562  * \NOTE we need the copy operator here as the original CandidateForTesselation is removed in
    2563  * AddTesselationLine() in AddCandidateTriangle()
    2564  */
    2565 void Tesselation::AddCandidatePolygon(CandidateForTesselation CandidateLine, const double RADIUS, const LinkedCell *LC)
    2566 {
    2567   Info FunctionInfo(__func__);
     2071 * \NOTE we need the copy operator here as the original CandidateForTesselation is removed in AddTesselationLine()
     2072 */
     2073void Tesselation::AddCandidateTriangle(CandidateForTesselation CandidateLine)
     2074{
     2075        Info FunctionInfo(__func__);
    25682076  Vector Center;
    25692077  TesselPoint * const TurningPoint = CandidateLine.BaseLine->endpoints[0]->node;
    2570   TesselPointList::iterator Runner;
    2571   TesselPointList::iterator Sprinter;
    25722078
    25732079  // fill the set of neighbours
    2574   TesselPointSet SetOfNeighbours;
     2080  Center.CopyVector(CandidateLine.BaseLine->endpoints[1]->node->node);
     2081  Center.SubtractVector(TurningPoint->node);
     2082  set<TesselPoint*> SetOfNeighbours;
    25752083  SetOfNeighbours.insert(CandidateLine.BaseLine->endpoints[1]->node);
    25762084  for (TesselPointList::iterator Runner = CandidateLine.pointlist.begin(); Runner != CandidateLine.pointlist.end(); Runner++)
    25772085    SetOfNeighbours.insert(*Runner);
    2578   TesselPointList *connectedClosestPoints = GetCircleOfSetOfPoints(&SetOfNeighbours, TurningPoint, CandidateLine.BaseLine->endpoints[1]->node->node);
    2579 
    2580   DoLog(0) && (Log() << Verbose(0) << "List of Candidates for Turning Point " << *TurningPoint << ":" << endl);
    2581   for (TesselPointList::iterator TesselRunner = connectedClosestPoints->begin(); TesselRunner != connectedClosestPoints->end(); ++TesselRunner)
    2582     DoLog(0) && (Log() << Verbose(0) << " " << **TesselRunner << endl);
     2086  TesselPointList *connectedClosestPoints = GetCircleOfSetOfPoints(&SetOfNeighbours, TurningPoint, &Center);
    25832087
    25842088  // go through all angle-sorted candidates (in degenerate n-nodes case we may have to add multiple triangles)
    2585   Runner = connectedClosestPoints->begin();
    2586   Sprinter = Runner;
     2089  TesselPointList::iterator Runner = connectedClosestPoints->begin();
     2090  TesselPointList::iterator Sprinter = Runner;
    25872091  Sprinter++;
    2588   while (Sprinter != connectedClosestPoints->end()) {
    2589     DoLog(0) && (Log() << Verbose(0) << "Current Runner is " << *(*Runner) << " and sprinter is " << *(*Sprinter) << "." << endl);
    2590 
     2092  while(Sprinter != connectedClosestPoints->end()) {
     2093    // add the points
    25912094    AddTesselationPoint(TurningPoint, 0);
    2592     AddTesselationPoint(*Runner, 1);
    2593     AddTesselationPoint(*Sprinter, 2);
    2594 
    2595     AddCandidateTriangle(CandidateLine, Opt);
    2596 
     2095    AddTesselationPoint((*Runner), 1);
     2096    AddTesselationPoint((*Sprinter), 2);
     2097
     2098    Center.CopyVector(&CandidateLine.OptCenter);
     2099    // add the lines
     2100    AddTesselationLine(TPS[0], TPS[1], 0);
     2101    AddTesselationLine(TPS[0], TPS[2], 1);
     2102    AddTesselationLine(TPS[1], TPS[2], 2);
     2103
     2104    // add the triangles
     2105    BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
     2106    AddTesselationTriangle();
     2107    Center.Scale(-1.);
     2108    BTS->GetNormalVector(Center);
     2109
     2110    Log() << Verbose(0) << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector << "." << endl;
    25972111    Runner = Sprinter;
    25982112    Sprinter++;
    2599     if (Sprinter != connectedClosestPoints->end()) {
    2600       // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked)
    2601       FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OptCenter); // Assume BTS contains last triangle
    2602       DoLog(0) && (Log() << Verbose(0) << " There are still more triangles to add." << endl);
    2603     }
    2604     // pick candidates for other open lines as well
    2605     FindCandidatesforOpenLines(RADIUS, LC);
    2606 
    2607     // check whether we add a degenerate or a normal triangle
    2608     if (CheckDegeneracy(CandidateLine, RADIUS, LC)) {
    2609       // add normal and degenerate triangles
    2610       DoLog(1) && (Log() << Verbose(1) << "Triangle of endpoints " << *TPS[0] << "," << *TPS[1] << " and " << *TPS[2] << " is degenerated, adding both sides." << endl);
    2611       AddCandidateTriangle(CandidateLine, OtherOpt);
    2612 
    2613       if (Sprinter != connectedClosestPoints->end()) {
    2614         // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked)
    2615         FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OtherOptCenter);
    2616       }
    2617       // pick candidates for other open lines as well
    2618       FindCandidatesforOpenLines(RADIUS, LC);
    2619     }
    2620   }
    2621   delete (connectedClosestPoints);
    2622 };
    2623 
    2624 /** for polygons (multiple candidates for a baseline) sets internal edges to the correct next candidate.
    2625  * \param *Sprinter next candidate to which internal open lines are set
    2626  * \param *OptCenter OptCenter for this candidate
    2627  */
    2628 void Tesselation::FindDegeneratedCandidatesforOpenLines(TesselPoint * const Sprinter, const Vector * const OptCenter)
    2629 {
    2630   Info FunctionInfo(__func__);
    2631 
    2632   pair<LineMap::iterator, LineMap::iterator> FindPair = TPS[0]->lines.equal_range(TPS[2]->node->nr);
    2633   for (LineMap::const_iterator FindLine = FindPair.first; FindLine != FindPair.second; FindLine++) {
    2634     DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl);
    2635     // If there is a line with less than two attached triangles, we don't need a new line.
    2636     if (FindLine->second->triangles.size() == 1) {
    2637       CandidateMap::iterator Finder = OpenLines.find(FindLine->second);
    2638       if (!Finder->second->pointlist.empty())
    2639         DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl);
    2640       else {
    2641         DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate, setting to next Sprinter" << (*Sprinter) << endl);
    2642         Finder->second->T = BTS;  // is last triangle
    2643         Finder->second->pointlist.push_back(Sprinter);
    2644         Finder->second->ShortestAngle = 0.;
    2645         Finder->second->OptCenter.CopyVector(OptCenter);
    2646       }
    2647     }
    2648   }
    2649 };
    2650 
    2651 /** If a given \a *triangle is degenerated, this adds both sides.
    2652  * i.e. the triangle with same BoundaryPointSet's but NormalVector in opposite direction.
    2653  * Note that endpoints are stored in Tesselation::TPS
    2654  * \param CandidateLine CanddiateForTesselation structure for the desired BoundaryLine
    2655  * \param RADIUS radius of sphere
    2656  * \param *LC pointer to LinkedCell structure
    2657  */
    2658 void Tesselation::AddDegeneratedTriangle(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC)
    2659 {
    2660   Info FunctionInfo(__func__);
    2661   Vector Center;
    2662   CandidateMap::const_iterator CandidateCheck = OpenLines.end();
    2663   BoundaryTriangleSet *triangle = NULL;
    2664 
    2665   /// 1. Create or pick the lines for the first triangle
    2666   DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for first triangle ..." << endl);
    2667   for (int i = 0; i < 3; i++) {
    2668     BLS[i] = NULL;
    2669     DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
    2670     AddTesselationLine(&CandidateLine.OptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i);
    2671   }
    2672 
    2673   /// 2. create the first triangle and NormalVector and so on
    2674   DoLog(0) && (Log() << Verbose(0) << "INFO: Adding first triangle with center at " << CandidateLine.OptCenter << " ..." << endl);
    2675   BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    2676   AddTesselationTriangle();
    2677 
    2678   // create normal vector
    2679   BTS->GetCenter(&Center);
    2680   Center.SubtractVector(&CandidateLine.OptCenter);
    2681   BTS->SphereCenter.CopyVector(&CandidateLine.OptCenter);
    2682   BTS->GetNormalVector(Center);
    2683   // give some verbose output about the whole procedure
    2684   if (CandidateLine.T != NULL)
    2685     DoLog(0) && (Log() << Verbose(0) << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl);
    2686   else
    2687     DoLog(0) && (Log() << Verbose(0) << "--> New starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
    2688   triangle = BTS;
    2689 
    2690   /// 3. Gather candidates for each new line
    2691   DoLog(0) && (Log() << Verbose(0) << "INFO: Adding candidates to new lines ..." << endl);
    2692   for (int i = 0; i < 3; i++) {
    2693     DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
    2694     CandidateCheck = OpenLines.find(BLS[i]);
    2695     if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) {
    2696       if (CandidateCheck->second->T == NULL)
    2697         CandidateCheck->second->T = triangle;
    2698       FindNextSuitableTriangle(*(CandidateCheck->second), *CandidateCheck->second->T, RADIUS, LC);
    2699     }
    2700   }
    2701 
    2702   /// 4. Create or pick the lines for the second triangle
    2703   DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for second triangle ..." << endl);
    2704   for (int i = 0; i < 3; i++) {
    2705     DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
    2706     AddTesselationLine(&CandidateLine.OtherOptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i);
    2707   }
    2708 
    2709   /// 5. create the second triangle and NormalVector and so on
    2710   DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangle with center at " << CandidateLine.OtherOptCenter << " ..." << endl);
    2711   BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    2712   AddTesselationTriangle();
    2713 
    2714   BTS->SphereCenter.CopyVector(&CandidateLine.OtherOptCenter);
    2715   // create normal vector in other direction
    2716   BTS->GetNormalVector(&triangle->NormalVector);
    2717   BTS->NormalVector.Scale(-1.);
    2718   // give some verbose output about the whole procedure
    2719   if (CandidateLine.T != NULL)
    2720     DoLog(0) && (Log() << Verbose(0) << "--> New degenerate triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl);
    2721   else
    2722     DoLog(0) && (Log() << Verbose(0) << "--> New degenerate starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
    2723 
    2724   /// 6. Adding triangle to new lines
    2725   DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangles to new lines ..." << endl);
    2726   for (int i = 0; i < 3; i++) {
    2727     DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
    2728     CandidateCheck = OpenLines.find(BLS[i]);
    2729     if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) {
    2730       if (CandidateCheck->second->T == NULL)
    2731         CandidateCheck->second->T = BTS;
    2732     }
    2733   }
    2734 }
    2735 ;
    2736 
    2737 /** Adds a triangle to the Tesselation structure from three given TesselPoint's.
    2738  * Note that endpoints are in Tesselation::TPS.
    2739  * \param CandidateLine CandidateForTesselation structure contains other information
    2740  * \param type which opt center to add (i.e. which side) and thus which NormalVector to take
    2741  */
    2742 void Tesselation::AddCandidateTriangle(CandidateForTesselation &CandidateLine, enum centers type)
    2743 {
    2744   Info FunctionInfo(__func__);
    2745   Vector Center;
    2746   Vector *OptCenter = (type == Opt) ? &CandidateLine.OptCenter : &CandidateLine.OtherOptCenter;
    2747 
    2748   // add the lines
    2749   AddTesselationLine(OptCenter, TPS[2], TPS[0], TPS[1], 0);
    2750   AddTesselationLine(OptCenter, TPS[1], TPS[0], TPS[2], 1);
    2751   AddTesselationLine(OptCenter, TPS[0], TPS[1], TPS[2], 2);
    2752 
    2753   // add the triangles
    2754   BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    2755   AddTesselationTriangle();
    2756 
    2757   // create normal vector
    2758   BTS->GetCenter(&Center);
    2759   Center.SubtractVector(OptCenter);
    2760   BTS->SphereCenter.CopyVector(OptCenter);
    2761   BTS->GetNormalVector(Center);
    2762 
    2763   // give some verbose output about the whole procedure
    2764   if (CandidateLine.T != NULL)
    2765     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);
    2766   else
    2767     DoLog(0) && (Log() << Verbose(0) << "--> New" << ((type == OtherOpt) ? " degenerate " : " ") << "starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
    2768 }
    2769 ;
     2113  }
     2114};
    27702115
    27712116/** Checks whether the quadragon of the two triangles connect to \a *Base is convex.
     
    27782123class BoundaryPointSet *Tesselation::IsConvexRectangle(class BoundaryLineSet *Base)
    27792124{
    2780   Info FunctionInfo(__func__);
     2125        Info FunctionInfo(__func__);
    27812126  class BoundaryPointSet *Spot = NULL;
    27822127  class BoundaryLineSet *OtherBase;
    27832128  Vector *ClosestPoint;
    27842129
    2785   int m = 0;
    2786   for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2787     for (int j = 0; j < 3; j++) // all of their endpoints and baselines
     2130  int m=0;
     2131  for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
     2132    for (int j=0;j<3;j++) // all of their endpoints and baselines
    27882133      if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
    27892134        BPS[m++] = runner->second->endpoints[j];
    2790   OtherBase = new class BoundaryLineSet(BPS, -1);
    2791 
    2792   DoLog(1) && (Log() << Verbose(1) << "INFO: Current base line is " << *Base << "." << endl);
    2793   DoLog(1) && (Log() << Verbose(1) << "INFO: Other base line is " << *OtherBase << "." << endl);
     2135  OtherBase = new class BoundaryLineSet(BPS,-1);
     2136
     2137  Log() << Verbose(1) << "INFO: Current base line is " << *Base << "." << endl;
     2138  Log() << Verbose(1) << "INFO: Other base line is " << *OtherBase << "." << endl;
    27942139
    27952140  // get the closest point on each line to the other line
     
    27972142
    27982143  // delete the temporary other base line
    2799   delete (OtherBase);
     2144  delete(OtherBase);
    28002145
    28012146  // get the distance vector from Base line to OtherBase line
     
    28042149  BaseLine.CopyVector(Base->endpoints[1]->node->node);
    28052150  BaseLine.SubtractVector(Base->endpoints[0]->node->node);
    2806   for (int i = 0; i < 2; i++) {
     2151  for (int i=0;i<2;i++) {
    28072152    DistanceToIntersection[i].CopyVector(ClosestPoint);
    28082153    DistanceToIntersection[i].SubtractVector(Base->endpoints[i]->node->node);
    28092154    distance[i] = BaseLine.ScalarProduct(&DistanceToIntersection[i]);
    28102155  }
    2811   delete (ClosestPoint);
    2812   if ((distance[0] * distance[1]) > 0) { // have same sign?
    2813     DoLog(1) && (Log() << Verbose(1) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1] << ". " << *Base << "' rectangle is concave." << endl);
     2156  delete(ClosestPoint);
     2157  if ((distance[0] * distance[1]) > 0)  { // have same sign?
     2158    Log() << Verbose(1) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1]  << ". " << *Base << "' rectangle is concave." << endl;
    28142159    if (distance[0] < distance[1]) {
    28152160      Spot = Base->endpoints[0];
     
    28182163    }
    28192164    return Spot;
    2820   } else { // different sign, i.e. we are in between
    2821     DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl);
     2165  } else {  // different sign, i.e. we are in between
     2166    Log() << Verbose(0) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl;
    28222167    return NULL;
    28232168  }
    28242169
    2825 }
    2826 ;
     2170};
    28272171
    28282172void Tesselation::PrintAllBoundaryPoints(ofstream *out) const
    28292173{
    2830   Info FunctionInfo(__func__);
     2174        Info FunctionInfo(__func__);
    28312175  // print all lines
    2832   DoLog(0) && (Log() << Verbose(0) << "Printing all boundary points for debugging:" << endl);
    2833   for (PointMap::const_iterator PointRunner = PointsOnBoundary.begin(); PointRunner != PointsOnBoundary.end(); PointRunner++)
    2834     DoLog(0) && (Log() << Verbose(0) << *(PointRunner->second) << endl);
    2835 }
    2836 ;
     2176  Log() << Verbose(0) << "Printing all boundary points for debugging:" << endl;
     2177  for (PointMap::const_iterator PointRunner = PointsOnBoundary.begin();PointRunner != PointsOnBoundary.end(); PointRunner++)
     2178    Log() << Verbose(0) << *(PointRunner->second) << endl;
     2179};
    28372180
    28382181void Tesselation::PrintAllBoundaryLines(ofstream *out) const
    28392182{
    2840   Info FunctionInfo(__func__);
     2183        Info FunctionInfo(__func__);
    28412184  // print all lines
    2842   DoLog(0) && (Log() << Verbose(0) << "Printing all boundary lines for debugging:" << endl);
     2185  Log() << Verbose(0) << "Printing all boundary lines for debugging:" << endl;
    28432186  for (LineMap::const_iterator LineRunner = LinesOnBoundary.begin(); LineRunner != LinesOnBoundary.end(); LineRunner++)
    2844     DoLog(0) && (Log() << Verbose(0) << *(LineRunner->second) << endl);
    2845 }
    2846 ;
     2187    Log() << Verbose(0) << *(LineRunner->second) << endl;
     2188};
    28472189
    28482190void Tesselation::PrintAllBoundaryTriangles(ofstream *out) const
    28492191{
    2850   Info FunctionInfo(__func__);
     2192        Info FunctionInfo(__func__);
    28512193  // print all triangles
    2852   DoLog(0) && (Log() << Verbose(0) << "Printing all boundary triangles for debugging:" << endl);
     2194  Log() << Verbose(0) << "Printing all boundary triangles for debugging:" << endl;
    28532195  for (TriangleMap::const_iterator TriangleRunner = TrianglesOnBoundary.begin(); TriangleRunner != TrianglesOnBoundary.end(); TriangleRunner++)
    2854     DoLog(0) && (Log() << Verbose(0) << *(TriangleRunner->second) << endl);
    2855 }
    2856 ;
     2196    Log() << Verbose(0) << *(TriangleRunner->second) << endl;
     2197};
    28572198
    28582199/** For a given boundary line \a *Base and its two triangles, picks the central baseline that is "higher".
     
    28632204double Tesselation::PickFarthestofTwoBaselines(class BoundaryLineSet *Base)
    28642205{
    2865   Info FunctionInfo(__func__);
     2206        Info FunctionInfo(__func__);
    28662207  class BoundaryLineSet *OtherBase;
    28672208  Vector *ClosestPoint[2];
    28682209  double volume;
    28692210
    2870   int m = 0;
    2871   for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2872     for (int j = 0; j < 3; j++) // all of their endpoints and baselines
     2211  int m=0;
     2212  for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
     2213    for (int j=0;j<3;j++) // all of their endpoints and baselines
    28732214      if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
    28742215        BPS[m++] = runner->second->endpoints[j];
    2875   OtherBase = new class BoundaryLineSet(BPS, -1);
    2876 
    2877   DoLog(0) && (Log() << Verbose(0) << "INFO: Current base line is " << *Base << "." << endl);
    2878   DoLog(0) && (Log() << Verbose(0) << "INFO: Other base line is " << *OtherBase << "." << endl);
     2216  OtherBase = new class BoundaryLineSet(BPS,-1);
     2217
     2218  Log() << Verbose(0) << "INFO: Current base line is " << *Base << "." << endl;
     2219  Log() << Verbose(0) << "INFO: Other base line is " << *OtherBase << "." << endl;
    28792220
    28802221  // get the closest point on each line to the other line
     
    28912232
    28922233  // delete the temporary other base line and the closest points
    2893   delete (ClosestPoint[0]);
    2894   delete (ClosestPoint[1]);
    2895   delete (OtherBase);
     2234  delete(ClosestPoint[0]);
     2235  delete(ClosestPoint[1]);
     2236  delete(OtherBase);
    28962237
    28972238  if (Distance.NormSquared() < MYEPSILON) { // check for intersection
    2898     DoLog(0) && (Log() << Verbose(0) << "REJECT: Both lines have an intersection: Nothing to do." << endl);
     2239    Log() << Verbose(0) << "REJECT: Both lines have an intersection: Nothing to do." << endl;
    28992240    return false;
    29002241  } else { // check for sign against BaseLineNormal
     
    29022243    BaseLineNormal.Zero();
    29032244    if (Base->triangles.size() < 2) {
    2904       DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl);
     2245      eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl;
    29052246      return 0.;
    29062247    }
    29072248    for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
    2908       DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl);
     2249      Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
    29092250      BaseLineNormal.AddVector(&(runner->second->NormalVector));
    29102251    }
    2911     BaseLineNormal.Scale(1. / 2.);
     2252    BaseLineNormal.Scale(1./2.);
    29122253
    29132254    if (Distance.ScalarProduct(&BaseLineNormal) > MYEPSILON) { // Distance points outwards, hence OtherBase higher than Base -> flip
    2914       DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl);
     2255      Log() << Verbose(0) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl;
    29152256      // calculate volume summand as a general tetraeder
    29162257      return volume;
    2917     } else { // Base higher than OtherBase -> do nothing
    2918       DoLog(0) && (Log() << Verbose(0) << "REJECT: Base line is higher: Nothing to do." << endl);
     2258    } else {  // Base higher than OtherBase -> do nothing
     2259      Log() << Verbose(0) << "REJECT: Base line is higher: Nothing to do." << endl;
    29192260      return 0.;
    29202261    }
    29212262  }
    2922 }
    2923 ;
     2263};
    29242264
    29252265/** For a given baseline and its two connected triangles, flips the baseline.
     
    29322272class BoundaryLineSet * Tesselation::FlipBaseline(class BoundaryLineSet *Base)
    29332273{
    2934   Info FunctionInfo(__func__);
     2274        Info FunctionInfo(__func__);
    29352275  class BoundaryLineSet *OldLines[4], *NewLine;
    29362276  class BoundaryPointSet *OldPoints[2];
    29372277  Vector BaseLineNormal;
    29382278  int OldTriangleNrs[2], OldBaseLineNr;
    2939   int i, m;
     2279  int i,m;
    29402280
    29412281  // calculate NormalVector for later use
    29422282  BaseLineNormal.Zero();
    29432283  if (Base->triangles.size() < 2) {
    2944     DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl);
     2284    eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl;
    29452285    return NULL;
    29462286  }
    29472287  for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
    2948     DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl);
     2288    Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
    29492289    BaseLineNormal.AddVector(&(runner->second->NormalVector));
    29502290  }
    2951   BaseLineNormal.Scale(-1. / 2.); // has to point inside for BoundaryTriangleSet::GetNormalVector()
     2291  BaseLineNormal.Scale(-1./2.); // has to point inside for BoundaryTriangleSet::GetNormalVector()
    29522292
    29532293  // get the two triangles
    29542294  // gather four endpoints and four lines
    2955   for (int j = 0; j < 4; j++)
     2295  for (int j=0;j<4;j++)
    29562296    OldLines[j] = NULL;
    2957   for (int j = 0; j < 2; j++)
     2297  for (int j=0;j<2;j++)
    29582298    OldPoints[j] = NULL;
    2959   i = 0;
    2960   m = 0;
    2961   DoLog(0) && (Log() << Verbose(0) << "The four old lines are: ");
    2962   for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2963     for (int j = 0; j < 3; j++) // all of their endpoints and baselines
     2299  i=0;
     2300  m=0;
     2301  Log() << Verbose(0) << "The four old lines are: ";
     2302  for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
     2303    for (int j=0;j<3;j++) // all of their endpoints and baselines
    29642304      if (runner->second->lines[j] != Base) { // pick not the central baseline
    29652305        OldLines[i++] = runner->second->lines[j];
    2966         DoLog(0) && (Log() << Verbose(0) << *runner->second->lines[j] << "\t");
     2306        Log() << Verbose(0) << *runner->second->lines[j] << "\t";
    29672307      }
    2968   DoLog(0) && (Log() << Verbose(0) << endl);
    2969   DoLog(0) && (Log() << Verbose(0) << "The two old points are: ");
    2970   for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2971     for (int j = 0; j < 3; j++) // all of their endpoints and baselines
     2308  Log() << Verbose(0) << endl;
     2309  Log() << Verbose(0) << "The two old points are: ";
     2310  for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
     2311    for (int j=0;j<3;j++) // all of their endpoints and baselines
    29722312      if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) { // and neither of its endpoints
    29732313        OldPoints[m++] = runner->second->endpoints[j];
    2974         DoLog(0) && (Log() << Verbose(0) << *runner->second->endpoints[j] << "\t");
     2314        Log() << Verbose(0) << *runner->second->endpoints[j] << "\t";
    29752315      }
    2976   DoLog(0) && (Log() << Verbose(0) << endl);
     2316  Log() << Verbose(0) << endl;
    29772317
    29782318  // check whether everything is in place to create new lines and triangles
    2979   if (i < 4) {
    2980     DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl);
     2319  if (i<4) {
     2320    eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl;
    29812321    return NULL;
    29822322  }
    2983   for (int j = 0; j < 4; j++)
     2323  for (int j=0;j<4;j++)
    29842324    if (OldLines[j] == NULL) {
    2985       DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl);
     2325      eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl;
    29862326      return NULL;
    29872327    }
    2988   for (int j = 0; j < 2; j++)
     2328  for (int j=0;j<2;j++)
    29892329    if (OldPoints[j] == NULL) {
    2990       DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough endpoints!" << endl);
     2330      eLog() << Verbose(1) << "We have not gathered enough endpoints!" << endl;
    29912331      return NULL;
    29922332    }
    29932333
    29942334  // remove triangles and baseline removes itself
    2995   DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting baseline " << *Base << " from global list." << endl);
     2335  Log() << Verbose(0) << "INFO: Deleting baseline " << *Base << " from global list." << endl;
    29962336  OldBaseLineNr = Base->Nr;
    2997   m = 0;
    2998   for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
    2999     DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting triangle " << *(runner->second) << "." << endl);
     2337  m=0;
     2338  for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
     2339    Log() << Verbose(0) << "INFO: Deleting triangle " << *(runner->second) << "." << endl;
    30002340    OldTriangleNrs[m++] = runner->second->Nr;
    30012341    RemoveTesselationTriangle(runner->second);
     
    30072347  NewLine = new class BoundaryLineSet(BPS, OldBaseLineNr);
    30082348  LinesOnBoundary.insert(LinePair(OldBaseLineNr, NewLine)); // no need for check for unique insertion as NewLine is definitely a new one
    3009   DoLog(0) && (Log() << Verbose(0) << "INFO: Created new baseline " << *NewLine << "." << endl);
     2349  Log() << Verbose(0) << "INFO: Created new baseline " << *NewLine << "." << endl;
    30102350
    30112351  // construct new triangles with flipped baseline
    3012   i = -1;
     2352  i=-1;
    30132353  if (OldLines[0]->IsConnectedTo(OldLines[2]))
    3014     i = 2;
     2354    i=2;
    30152355  if (OldLines[0]->IsConnectedTo(OldLines[3]))
    3016     i = 3;
    3017   if (i != -1) {
     2356    i=3;
     2357  if (i!=-1) {
    30182358    BLS[0] = OldLines[0];
    30192359    BLS[1] = OldLines[i];
     
    30222362    BTS->GetNormalVector(BaseLineNormal);
    30232363    AddTesselationTriangle(OldTriangleNrs[0]);
    3024     DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl);
    3025 
    3026     BLS[0] = (i == 2 ? OldLines[3] : OldLines[2]);
     2364    Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl;
     2365
     2366    BLS[0] = (i==2 ? OldLines[3] : OldLines[2]);
    30272367    BLS[1] = OldLines[1];
    30282368    BLS[2] = NewLine;
     
    30302370    BTS->GetNormalVector(BaseLineNormal);
    30312371    AddTesselationTriangle(OldTriangleNrs[1]);
    3032     DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl);
     2372    Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl;
    30332373  } else {
    3034     DoeLog(0) && (eLog() << Verbose(0) << "The four old lines do not connect, something's utterly wrong here!" << endl);
     2374    eLog() << Verbose(0) << "The four old lines do not connect, something's utterly wrong here!" << endl;
    30352375    return NULL;
    30362376  }
    30372377
    30382378  return NewLine;
    3039 }
    3040 ;
     2379};
     2380
    30412381
    30422382/** Finds the second point of starting triangle.
     
    30502390void Tesselation::FindSecondPointForTesselation(TesselPoint* a, Vector Oben, TesselPoint*& OptCandidate, double Storage[3], double RADIUS, const LinkedCell *LC)
    30512391{
    3052   Info FunctionInfo(__func__);
     2392        Info FunctionInfo(__func__);
    30532393  Vector AngleCheck;
    30542394  class TesselPoint* Candidate = NULL;
     
    30592399  int Nupper[NDIM];
    30602400
    3061   if (LC->SetIndexToNode(a)) { // get cell for the starting point
    3062     for (int i = 0; i < NDIM; i++) // store indices of this cell
     2401  if (LC->SetIndexToNode(a)) {  // get cell for the starting point
     2402    for(int i=0;i<NDIM;i++) // store indices of this cell
    30632403      N[i] = LC->n[i];
    30642404  } else {
    3065     DoeLog(1) && (eLog() << Verbose(1) << "Point " << *a << " is not found in cell " << LC->index << "." << endl);
     2405    eLog() << Verbose(1) << "Point " << *a << " is not found in cell " << LC->index << "." << endl;
    30662406    return;
    30672407  }
    30682408  // then go through the current and all neighbouring cells and check the contained points for possible candidates
    3069   for (int i = 0; i < NDIM; i++) {
    3070     Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0;
    3071     Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1;
    3072   }
    3073   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);
     2409  for (int i=0;i<NDIM;i++) {
     2410    Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
     2411    Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
     2412  }
     2413  Log() << Verbose(0) << "LC Intervals from [" << N[0] << "<->" << LC->N[0] << ", " << N[1] << "<->" << LC->N[1] << ", " << N[2] << "<->" << LC->N[2] << "] :"
     2414    << " [" << Nlower[0] << "," << Nupper[0] << "], " << " [" << Nlower[1] << "," << Nupper[1] << "], " << " [" << Nlower[2] << "," << Nupper[2] << "], " << endl;
    30742415
    30752416  for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
    30762417    for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
    30772418      for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
    3078         const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
     2419        const LinkedNodes *List = LC->GetCurrentCell();
    30792420        //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
    30802421        if (List != NULL) {
    3081           for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
     2422          for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
    30822423            Candidate = (*Runner);
    30832424            // check if we only have one unique point yet ...
     
    31052446              norm = aCandidate.Norm();
    31062447              // second point shall have smallest angle with respect to Oben vector
    3107               if (norm < RADIUS * 2.) {
     2448              if (norm < RADIUS*2.) {
    31082449                angle = AngleCheck.Angle(&Oben);
    31092450                if (angle < Storage[0]) {
    31102451                  //Log() << Verbose(1) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]);
    3111                   DoLog(1) && (Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n");
     2452                  Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n";
    31122453                  OptCandidate = Candidate;
    31132454                  Storage[0] = angle;
     
    31242465          }
    31252466        } else {
    3126           DoLog(0) && (Log() << Verbose(0) << "Linked cell list is empty." << endl);
     2467          Log() << Verbose(0) << "Linked cell list is empty." << endl;
    31272468        }
    31282469      }
    3129 }
    3130 ;
     2470};
     2471
    31312472
    31322473/** This recursive function finds a third point, to form a triangle with two given ones.
     
    31562497 * @param OldSphereCenter center of sphere for base triangle, relative to center of BaseLine, giving null angle for the parameter circle
    31572498 * @param CandidateLine CandidateForTesselation with the current base line and list of candidates and ShortestAngle
    3158  * @param ThirdPoint third point to avoid in search
     2499 * @param ThirdNode third point to avoid in search
    31592500 * @param RADIUS radius of sphere
    31602501 * @param *LC LinkedCell structure with neighbouring points
    31612502 */
    3162 void 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
    3163 {
    3164   Info FunctionInfo(__func__);
    3165   Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
     2503void Tesselation::FindThirdPointForTesselation(Vector &NormalVector, Vector &SearchDirection, Vector &OldSphereCenter, CandidateForTesselation &CandidateLine, const class TesselPoint  * const ThirdNode, const double RADIUS, const LinkedCell *LC) const
     2504{
     2505        Info FunctionInfo(__func__);
     2506  Vector CircleCenter;  // center of the circle, i.e. of the band of sphere's centers
    31662507  Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
    31672508  Vector SphereCenter;
    3168   Vector NewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility
    3169   Vector OtherNewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility
    3170   Vector NewNormalVector; // normal vector of the Candidate's triangle
     2509  Vector NewSphereCenter;   // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility
     2510  Vector OtherNewSphereCenter;   // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility
     2511  Vector NewNormalVector;   // normal vector of the Candidate's triangle
    31712512  Vector helper, OptCandidateCenter, OtherOptCandidateCenter;
    3172   Vector RelativeOldSphereCenter;
    3173   Vector NewPlaneCenter;
    31742513  double CircleRadius; // radius of this circle
    31752514  double radius;
    3176   double otherradius;
    31772515  double alpha, Otheralpha; // angles (i.e. parameter for the circle).
    31782516  int N[NDIM], Nlower[NDIM], Nupper[NDIM];
    31792517  TesselPoint *Candidate = NULL;
    31802518
    3181   DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl);
    3182 
    3183   // copy old center
    3184   CandidateLine.OldCenter.CopyVector(&OldSphereCenter);
    3185   CandidateLine.ThirdPoint = ThirdPoint;
    3186   CandidateLine.pointlist.clear();
     2519  Log() << Verbose(1) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl;
    31872520
    31882521  // construct center of circle
     
    31952528  CirclePlaneNormal.SubtractVector(CandidateLine.BaseLine->endpoints[1]->node->node);
    31962529
    3197   RelativeOldSphereCenter.CopyVector(&OldSphereCenter);
    3198   RelativeOldSphereCenter.SubtractVector(&CircleCenter);
    3199 
    3200   // calculate squared radius TesselPoint *ThirdPoint,f circle
    3201   radius = CirclePlaneNormal.NormSquared() / 4.;
    3202   if (radius < RADIUS * RADIUS) {
    3203     CircleRadius = RADIUS * RADIUS - radius;
     2530  // calculate squared radius TesselPoint *ThirdNode,f circle
     2531  radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
     2532  if (radius/4. < RADIUS*RADIUS) {
     2533    CircleRadius = RADIUS*RADIUS - radius/4.;
    32042534    CirclePlaneNormal.Normalize();
    3205     DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
     2535    //Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
    32062536
    32072537    // test whether old center is on the band's plane
    3208     if (fabs(RelativeOldSphereCenter.ScalarProduct(&CirclePlaneNormal)) > HULLEPSILON) {
    3209       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);
    3210       RelativeOldSphereCenter.ProjectOntoPlane(&CirclePlaneNormal);
    3211     }
    3212     radius = RelativeOldSphereCenter.NormSquared();
     2538    if (fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) > HULLEPSILON) {
     2539      eLog() << Verbose(1) << "Something's very wrong here: OldSphereCenter is not on the band's plane as desired by " << fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) << "!" << endl;
     2540      OldSphereCenter.ProjectOntoPlane(&CirclePlaneNormal);
     2541    }
     2542    radius = OldSphereCenter.ScalarProduct(&OldSphereCenter);
    32132543    if (fabs(radius - CircleRadius) < HULLEPSILON) {
    3214       DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeOldSphereCenter is at " << RelativeOldSphereCenter << "." << endl);
     2544      //Log() << Verbose(1) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl;
    32152545
    32162546      // check SearchDirection
    3217       DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
    3218       if (fabs(RelativeOldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) { // rotated the wrong way!
    3219         DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl);
     2547      //Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
     2548      if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) { // rotated the wrong way!
     2549        eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl;
    32202550      }
    32212551
    32222552      // get cell for the starting point
    32232553      if (LC->SetIndexToVector(&CircleCenter)) {
    3224         for (int i = 0; i < NDIM; i++) // store indices of this cell
    3225           N[i] = LC->n[i];
     2554        for(int i=0;i<NDIM;i++) // store indices of this cell
     2555        N[i] = LC->n[i];
    32262556        //Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
    32272557      } else {
    3228         DoeLog(1) && (eLog() << Verbose(1) << "Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl);
     2558        eLog() << Verbose(1) << "Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl;
    32292559        return;
    32302560      }
    32312561      // then go through the current and all neighbouring cells and check the contained points for possible candidates
    32322562      //Log() << Verbose(1) << "LC Intervals:";
    3233       for (int i = 0; i < NDIM; i++) {
    3234         Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0;
    3235         Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1;
     2563      for (int i=0;i<NDIM;i++) {
     2564        Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
     2565        Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
    32362566        //Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ";
    32372567      }
     
    32402570        for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
    32412571          for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
    3242             const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
     2572            const LinkedNodes *List = LC->GetCurrentCell();
    32432573            //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
    32442574            if (List != NULL) {
    3245               for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
     2575              for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
    32462576                Candidate = (*Runner);
    32472577
    32482578                // check for three unique points
    3249                 DoLog(2) && (Log() << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " for BaseLine " << *CandidateLine.BaseLine << " with OldSphereCenter " << OldSphereCenter << "." << endl);
    3250                 if ((Candidate != CandidateLine.BaseLine->endpoints[0]->node) && (Candidate != CandidateLine.BaseLine->endpoints[1]->node)) {
    3251 
    3252                   // find center on the plane
    3253                   GetCenterofCircumcircle(&NewPlaneCenter, *CandidateLine.BaseLine->endpoints[0]->node->node, *CandidateLine.BaseLine->endpoints[1]->node->node, *Candidate->node);
    3254                   DoLog(1) && (Log() << Verbose(1) << "INFO: NewPlaneCenter is " << NewPlaneCenter << "." << endl);
    3255 
    3256                   if (NewNormalVector.MakeNormalVector(CandidateLine.BaseLine->endpoints[0]->node->node, CandidateLine.BaseLine->endpoints[1]->node->node, Candidate->node) && (fabs(NewNormalVector.NormSquared()) > HULLEPSILON)) {
    3257                     DoLog(1) && (Log() << Verbose(1) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl);
    3258                     radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(&NewPlaneCenter);
    3259                     DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
    3260                     DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
    3261                     DoLog(1) && (Log() << Verbose(1) << "INFO: Radius of CircumCenterCircle is " << radius << "." << endl);
    3262                     if (radius < RADIUS * RADIUS) {
    3263                       otherradius = CandidateLine.BaseLine->endpoints[1]->node->node->DistanceSquared(&NewPlaneCenter);
    3264                       if (fabs(radius - otherradius) < HULLEPSILON) {
    3265                         // construct both new centers
    3266                         NewSphereCenter.CopyVector(&NewPlaneCenter);
    3267                         OtherNewSphereCenter.CopyVector(&NewPlaneCenter);
    3268                         helper.CopyVector(&NewNormalVector);
    3269                         helper.Scale(sqrt(RADIUS * RADIUS - radius));
    3270                         DoLog(2) && (Log() << Verbose(2) << "INFO: Distance of NewPlaneCenter " << NewPlaneCenter << " to either NewSphereCenter is " << helper.Norm() << " of vector " << helper << " with sphere radius " << RADIUS << "." << endl);
    3271                         NewSphereCenter.AddVector(&helper);
    3272                         DoLog(2) && (Log() << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl);
    3273                         // OtherNewSphereCenter is created by the same vector just in the other direction
    3274                         helper.Scale(-1.);
    3275                         OtherNewSphereCenter.AddVector(&helper);
    3276                         DoLog(2) && (Log() << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl);
    3277 
    3278                         alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
    3279                         Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
    3280                         if ((ThirdPoint != NULL) && (Candidate == ThirdPoint->node)) { // in that case only the other circlecenter is valid
    3281                           if (OldSphereCenter.DistanceSquared(&NewSphereCenter) < OldSphereCenter.DistanceSquared(&OtherNewSphereCenter))
    3282                             alpha = Otheralpha;
    3283                         } else
    3284                           alpha = min(alpha, Otheralpha);
    3285 
    3286                         // if there is a better candidate, drop the current list and add the new candidate
    3287                         // otherwise ignore the new candidate and keep the list
    3288                         if (CandidateLine.ShortestAngle > (alpha - HULLEPSILON)) {
    3289                           if (fabs(alpha - Otheralpha) > MYEPSILON) {
    3290                             CandidateLine.OptCenter.CopyVector(&NewSphereCenter);
    3291                             CandidateLine.OtherOptCenter.CopyVector(&OtherNewSphereCenter);
    3292                           } else {
    3293                             CandidateLine.OptCenter.CopyVector(&OtherNewSphereCenter);
    3294                             CandidateLine.OtherOptCenter.CopyVector(&NewSphereCenter);
    3295                           }
    3296                           // if there is an equal candidate, add it to the list without clearing the list
    3297                           if ((CandidateLine.ShortestAngle - HULLEPSILON) < alpha) {
    3298                             CandidateLine.pointlist.push_back(Candidate);
    3299                             DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found an equally good candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl);
    3300                           } else {
    3301                             // remove all candidates from the list and then the list itself
    3302                             CandidateLine.pointlist.clear();
    3303                             CandidateLine.pointlist.push_back(Candidate);
    3304                             DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found a better candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl);
    3305                           }
    3306                           CandidateLine.ShortestAngle = alpha;
    3307                           DoLog(0) && (Log() << Verbose(0) << "INFO: There are " << CandidateLine.pointlist.size() << " candidates in the list now." << endl);
     2579                Log() << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " at " << *(Candidate->node) << "." << endl;
     2580                if ((Candidate != CandidateLine.BaseLine->endpoints[0]->node) && (Candidate != CandidateLine.BaseLine->endpoints[1]->node) ){
     2581
     2582                  // construct both new centers
     2583                  GetCenterofCircumcircle(&NewSphereCenter, *CandidateLine.BaseLine->endpoints[0]->node->node, *CandidateLine.BaseLine->endpoints[1]->node->node, *Candidate->node);
     2584                  OtherNewSphereCenter.CopyVector(&NewSphereCenter);
     2585
     2586                  if ((NewNormalVector.MakeNormalVector(CandidateLine.BaseLine->endpoints[0]->node->node, CandidateLine.BaseLine->endpoints[1]->node->node, Candidate->node))
     2587                  && (fabs(NewNormalVector.ScalarProduct(&NewNormalVector)) > HULLEPSILON)
     2588                  ) {
     2589                    helper.CopyVector(&NewNormalVector);
     2590                    Log() << Verbose(1) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl;
     2591                    radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(&NewSphereCenter);
     2592                    if (radius < RADIUS*RADIUS) {
     2593                      helper.Scale(sqrt(RADIUS*RADIUS - radius));
     2594                      Log() << Verbose(2) << "INFO: Distance of NewCircleCenter to NewSphereCenter is " << helper.Norm() << " with sphere radius " << RADIUS << "." << endl;
     2595                      NewSphereCenter.AddVector(&helper);
     2596                      NewSphereCenter.SubtractVector(&CircleCenter);
     2597                      Log() << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl;
     2598
     2599                      // OtherNewSphereCenter is created by the same vector just in the other direction
     2600                      helper.Scale(-1.);
     2601                      OtherNewSphereCenter.AddVector(&helper);
     2602                      OtherNewSphereCenter.SubtractVector(&CircleCenter);
     2603                      Log() << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl;
     2604
     2605                      alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
     2606                      Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
     2607                      alpha = min(alpha, Otheralpha);
     2608                      // if there is a better candidate, drop the current list and add the new candidate
     2609                      // otherwise ignore the new candidate and keep the list
     2610                      if (CandidateLine.ShortestAngle > (alpha - HULLEPSILON)) {
     2611                        if (fabs(alpha - Otheralpha) > MYEPSILON) {
     2612                          CandidateLine.OptCenter.CopyVector(&NewSphereCenter);
     2613                          CandidateLine.OtherOptCenter.CopyVector(&OtherNewSphereCenter);
    33082614                        } else {
    3309                           if ((Candidate != NULL) && (CandidateLine.pointlist.begin() != CandidateLine.pointlist.end())) {
    3310                             DoLog(1) && (Log() << Verbose(1) << "REJECT: Old candidate " << *(*CandidateLine.pointlist.begin()) << " with " << CandidateLine.ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl);
    3311                           } else {
    3312                             DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl);
    3313                           }
     2615                          CandidateLine.OptCenter.CopyVector(&OtherNewSphereCenter);
     2616                          CandidateLine.OtherOptCenter.CopyVector(&NewSphereCenter);
    33142617                        }
     2618                        // if there is an equal candidate, add it to the list without clearing the list
     2619                        if ((CandidateLine.ShortestAngle - HULLEPSILON) < alpha) {
     2620                          CandidateLine.pointlist.push_back(Candidate);
     2621                          Log() << Verbose(0) << "ACCEPT: We have found an equally good candidate: " << *(Candidate) << " with "
     2622                            << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl;
     2623                        } else {
     2624                          // remove all candidates from the list and then the list itself
     2625                          CandidateLine.pointlist.clear();
     2626                          CandidateLine.pointlist.push_back(Candidate);
     2627                          Log() << Verbose(0) << "ACCEPT: We have found a better candidate: " << *(Candidate) << " with "
     2628                            << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl;
     2629                        }
     2630                        CandidateLine.ShortestAngle = alpha;
     2631                        Log() << Verbose(0) << "INFO: There are " << CandidateLine.pointlist.size() << " candidates in the list now." << endl;
    33152632                      } else {
    3316                         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);
     2633                        if ((Candidate != NULL) && (CandidateLine.pointlist.begin() != CandidateLine.pointlist.end())) {
     2634                          Log() << Verbose(1) << "REJECT: Old candidate " << *(Candidate) << " with " << CandidateLine.ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl;
     2635                        } else {
     2636                          Log() << Verbose(1) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl;
     2637                        }
    33172638                      }
     2639
    33182640                    } else {
    3319                       DoLog(1) && (Log() << Verbose(1) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl);
     2641                      Log() << Verbose(1) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl;
    33202642                    }
    33212643                  } else {
    3322                     DoLog(1) && (Log() << Verbose(1) << "REJECT: Three points from " << *CandidateLine.BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl);
     2644                    Log() << Verbose(1) << "REJECT: Three points from " << *CandidateLine.BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl;
    33232645                  }
    33242646                } else {
    3325                   if (ThirdPoint != NULL) {
    3326                     DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " and " << *ThirdPoint << " contains Candidate " << *Candidate << "." << endl);
     2647                  if (ThirdNode != NULL) {
     2648                    Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " and " << *ThirdNode << " contains Candidate " << *Candidate << "." << endl;
    33272649                  } else {
    3328                     DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " contains Candidate " << *Candidate << "." << endl);
     2650                    Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " contains Candidate " << *Candidate << "." << endl;
    33292651                  }
    33302652                }
     
    33332655          }
    33342656    } else {
    3335       DoeLog(1) && (eLog() << Verbose(1) << "The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl);
     2657      eLog() << Verbose(1) << "The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl;
    33362658    }
    33372659  } else {
    3338     if (ThirdPoint != NULL)
    3339       DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and third node " << *ThirdPoint << " is too big!" << endl);
     2660    if (ThirdNode != NULL)
     2661      Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and third node " << *ThirdNode << " is too big!" << endl;
    33402662    else
    3341       DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " is too big!" << endl);
    3342   }
    3343 
    3344   DoLog(1) && (Log() << Verbose(1) << "INFO: Sorting candidate list ..." << endl);
     2663      Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " is too big!" << endl;
     2664  }
     2665
     2666  Log() << Verbose(1) << "INFO: Sorting candidate list ..." << endl;
    33452667  if (CandidateLine.pointlist.size() > 1) {
    33462668    CandidateLine.pointlist.unique();
    33472669    CandidateLine.pointlist.sort(); //SortCandidates);
    33482670  }
    3349 
    3350   if ((!CandidateLine.pointlist.empty()) && (!CandidateLine.CheckValidity(RADIUS, LC))) {
    3351     DoeLog(0) && (eLog() << Verbose(0) << "There were other points contained in the rolling sphere as well!" << endl);
    3352     performCriticalExit();
    3353   }
    3354 }
    3355 ;
     2671};
    33562672
    33572673/** Finds the endpoint two lines are sharing.
     
    33622678class BoundaryPointSet *Tesselation::GetCommonEndpoint(const BoundaryLineSet * line1, const BoundaryLineSet * line2) const
    33632679{
    3364   Info FunctionInfo(__func__);
     2680        Info FunctionInfo(__func__);
    33652681  const BoundaryLineSet * lines[2] = { line1, line2 };
    33662682  class BoundaryPointSet *node = NULL;
    3367   PointMap OrderMap;
    3368   PointTestPair OrderTest;
     2683  map<int, class BoundaryPointSet *> OrderMap;
     2684  pair<map<int, class BoundaryPointSet *>::iterator, bool> OrderTest;
    33692685  for (int i = 0; i < 2; i++)
    33702686    // for both lines
    3371     for (int j = 0; j < 2; j++) { // for both endpoints
    3372       OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
    3373       if (!OrderTest.second) { // if insertion fails, we have common endpoint
    3374         node = OrderTest.first->second;
    3375         DoLog(1) && (Log() << Verbose(1) << "Common endpoint of lines " << *line1 << " and " << *line2 << " is: " << *node << "." << endl);
    3376         j = 2;
    3377         i = 2;
    3378         break;
     2687    for (int j = 0; j < 2; j++)
     2688      { // for both endpoints
     2689        OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (
     2690            lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
     2691        if (!OrderTest.second)
     2692          { // if insertion fails, we have common endpoint
     2693            node = OrderTest.first->second;
     2694            Log() << Verbose(1) << "Common endpoint of lines " << *line1
     2695                << " and " << *line2 << " is: " << *node << "." << endl;
     2696            j = 2;
     2697            i = 2;
     2698            break;
     2699          }
    33792700      }
    3380     }
    33812701  return node;
    3382 }
    3383 ;
    3384 
    3385 /** Finds the boundary points that are closest to a given Vector \a *x.
    3386  * \param *out output stream for debugging
    3387  * \param *x Vector to look from
    3388  * \return map of BoundaryPointSet of closest points sorted by squared distance or NULL.
    3389  */
    3390 DistanceToPointMap * Tesselation::FindClosestBoundaryPointsToVector(const Vector *x, const LinkedCell* LC) const
    3391 {
    3392   Info FunctionInfo(__func__);
    3393   PointMap::const_iterator FindPoint;
    3394   int N[NDIM], Nlower[NDIM], Nupper[NDIM];
    3395 
    3396   if (LinesOnBoundary.empty()) {
    3397     DoeLog(1) && (eLog() << Verbose(1) << "There is no tesselation structure to compare the point with, please create one first." << endl);
    3398     return NULL;
    3399   }
    3400 
    3401   // gather all points close to the desired one
    3402   LC->SetIndexToVector(x); // ignore status as we calculate bounds below sensibly
    3403   for (int i = 0; i < NDIM; i++) // store indices of this cell
    3404     N[i] = LC->n[i];
    3405   DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
    3406   DistanceToPointMap * points = new DistanceToPointMap;
    3407   LC->GetNeighbourBounds(Nlower, Nupper);
    3408   //Log() << Verbose(1) << endl;
    3409   for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
    3410     for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
    3411       for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
    3412         const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
    3413         //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
    3414         if (List != NULL) {
    3415           for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
    3416             FindPoint = PointsOnBoundary.find((*Runner)->nr);
    3417             if (FindPoint != PointsOnBoundary.end()) {
    3418               points->insert(DistanceToPointPair(FindPoint->second->node->node->DistanceSquared(x), FindPoint->second));
    3419               DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *FindPoint->second << " into the list." << endl);
    3420             }
    3421           }
    3422         } else {
    3423           DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
    3424         }
    3425       }
    3426 
    3427   // check whether we found some points
    3428   if (points->empty()) {
    3429     DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
    3430     delete (points);
    3431     return NULL;
    3432   }
    3433   return points;
    3434 }
    3435 ;
    3436 
    3437 /** Finds the boundary line that is closest to a given Vector \a *x.
    3438  * \param *out output stream for debugging
    3439  * \param *x Vector to look from
    3440  * \return closest BoundaryLineSet or NULL in degenerate case.
    3441  */
    3442 BoundaryLineSet * Tesselation::FindClosestBoundaryLineToVector(const Vector *x, const LinkedCell* LC) const
    3443 {
    3444   Info FunctionInfo(__func__);
    3445   // get closest points
    3446   DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC);
    3447   if (points == NULL) {
    3448     DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
    3449     return NULL;
    3450   }
    3451 
    3452   // for each point, check its lines, remember closest
    3453   DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryLine to " << *x << " ... " << endl);
    3454   BoundaryLineSet *ClosestLine = NULL;
    3455   double MinDistance = -1.;
    3456   Vector helper;
    3457   Vector Center;
    3458   Vector BaseLine;
    3459   for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++) {
    3460     for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) {
    3461       // calculate closest point on line to desired point
    3462       helper.CopyVector((LineRunner->second)->endpoints[0]->node->node);
    3463       helper.AddVector((LineRunner->second)->endpoints[1]->node->node);
    3464       helper.Scale(0.5);
    3465       Center.CopyVector(x);
    3466       Center.SubtractVector(&helper);
    3467       BaseLine.CopyVector((LineRunner->second)->endpoints[0]->node->node);
    3468       BaseLine.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3469       Center.ProjectOntoPlane(&BaseLine);
    3470       const double distance = Center.NormSquared();
    3471       if ((ClosestLine == NULL) || (distance < MinDistance)) {
    3472         // additionally calculate intersection on line (whether it's on the line section or not)
    3473         helper.CopyVector(x);
    3474         helper.SubtractVector((LineRunner->second)->endpoints[0]->node->node);
    3475         helper.SubtractVector(&Center);
    3476         const double lengthA = helper.ScalarProduct(&BaseLine);
    3477         helper.CopyVector(x);
    3478         helper.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3479         helper.SubtractVector(&Center);
    3480         const double lengthB = helper.ScalarProduct(&BaseLine);
    3481         if (lengthB * lengthA < 0) { // if have different sign
    3482           ClosestLine = LineRunner->second;
    3483           MinDistance = distance;
    3484           DoLog(1) && (Log() << Verbose(1) << "ACCEPT: New closest line is " << *ClosestLine << " with projected distance " << MinDistance << "." << endl);
    3485         } else {
    3486           DoLog(1) && (Log() << Verbose(1) << "REJECT: Intersection is outside of the line section: " << lengthA << " and " << lengthB << "." << endl);
    3487         }
    3488       } else {
    3489         DoLog(1) && (Log() << Verbose(1) << "REJECT: Point is too further away than present line: " << distance << " >> " << MinDistance << "." << endl);
    3490       }
    3491     }
    3492   }
    3493   delete (points);
    3494   // check whether closest line is "too close" :), then it's inside
    3495   if (ClosestLine == NULL) {
    3496     DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl);
    3497     return NULL;
    3498   }
    3499   return ClosestLine;
    3500 }
    3501 ;
     2702};
    35022703
    35032704/** Finds the triangle that is closest to a given Vector \a *x.
    35042705 * \param *out output stream for debugging
    35052706 * \param *x Vector to look from
    3506  * \return BoundaryTriangleSet of nearest triangle or NULL.
    3507  */
    3508 TriangleList * Tesselation::FindClosestTrianglesToVector(const Vector *x, const LinkedCell* LC) const
    3509 {
    3510   Info FunctionInfo(__func__);
    3511   // get closest points
    3512   DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC);
    3513   if (points == NULL) {
    3514     DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
     2707 * \return list of BoundaryTriangleSet of nearest triangles or NULL in degenerate case.
     2708 */
     2709list<BoundaryTriangleSet*> * Tesselation::FindClosestTrianglesToPoint(const Vector *x, const LinkedCell* LC) const
     2710{
     2711        Info FunctionInfo(__func__);
     2712  TesselPoint *trianglePoints[3];
     2713  TesselPoint *SecondPoint = NULL;
     2714  list<BoundaryTriangleSet*> *triangles = NULL;
     2715
     2716  if (LinesOnBoundary.empty()) {
     2717    eLog() << Verbose(1) << "Error: There is no tesselation structure to compare the point with, please create one first.";
    35152718    return NULL;
    35162719  }
    3517 
    3518   // for each point, check its lines, remember closest
    3519   DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryTriangle to " << *x << " ... " << endl);
    3520   LineSet ClosestLines;
    3521   double MinDistance = 1e+16;
    3522   Vector BaseLineIntersection;
    3523   Vector Center;
    3524   Vector BaseLine;
    3525   Vector BaseLineCenter;
    3526   for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++) {
    3527     for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) {
    3528 
    3529       BaseLine.CopyVector((LineRunner->second)->endpoints[0]->node->node);
    3530       BaseLine.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3531       const double lengthBase = BaseLine.NormSquared();
    3532 
    3533       BaseLineIntersection.CopyVector(x);
    3534       BaseLineIntersection.SubtractVector((LineRunner->second)->endpoints[0]->node->node);
    3535       const double lengthEndA = BaseLineIntersection.NormSquared();
    3536 
    3537       BaseLineIntersection.CopyVector(x);
    3538       BaseLineIntersection.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3539       const double lengthEndB = BaseLineIntersection.NormSquared();
    3540 
    3541       if ((lengthEndA > lengthBase) || (lengthEndB > lengthBase) || ((lengthEndA < MYEPSILON) || (lengthEndB < MYEPSILON))) { // intersection would be outside, take closer endpoint
    3542         const double lengthEnd = Min(lengthEndA, lengthEndB);
    3543         if (lengthEnd - MinDistance < -MYEPSILON) { // new best line
    3544           ClosestLines.clear();
    3545           ClosestLines.insert(LineRunner->second);
    3546           MinDistance = lengthEnd;
    3547           DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[0]->node << " is closer with " << lengthEnd << "." << endl);
    3548         } else if (fabs(lengthEnd - MinDistance) < MYEPSILON) { // additional best candidate
    3549           ClosestLines.insert(LineRunner->second);
    3550           DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[1]->node << " is equally good with " << lengthEnd << "." << endl);
    3551         } else { // line is worse
    3552           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);
     2720  Log() << Verbose(1) << "Finding closest Tesselpoint to " << *x << " ... " << endl;
     2721  trianglePoints[0] = FindClosestPoint(x, SecondPoint, LC);
     2722 
     2723  // check whether closest point is "too close" :), then it's inside
     2724  if (trianglePoints[0] == NULL) {
     2725    Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl;
     2726    return NULL;
     2727  }
     2728  if (trianglePoints[0]->node->DistanceSquared(x) < MYEPSILON) {
     2729    Log() << Verbose(1) << "Point is right on a tesselation point, no nearest triangle." << endl;
     2730    PointMap::const_iterator PointRunner = PointsOnBoundary.find(trianglePoints[0]->nr);
     2731    triangles = new list<BoundaryTriangleSet*>;
     2732    if (PointRunner != PointsOnBoundary.end()) {
     2733      for(LineMap::iterator LineRunner = PointRunner->second->lines.begin(); LineRunner != PointRunner->second->lines.end(); LineRunner++)
     2734        for(TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++)
     2735          triangles->push_back(TriangleRunner->second);
     2736      triangles->sort();
     2737      triangles->unique();
     2738    } else {
     2739      PointRunner = PointsOnBoundary.find(SecondPoint->nr);
     2740      trianglePoints[0] = SecondPoint;
     2741      if (PointRunner != PointsOnBoundary.end()) {
     2742        for(LineMap::iterator LineRunner = PointRunner->second->lines.begin(); LineRunner != PointRunner->second->lines.end(); LineRunner++)
     2743          for(TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++)
     2744            triangles->push_back(TriangleRunner->second);
     2745        triangles->sort();
     2746        triangles->unique();
     2747      } else {
     2748        eLog() << Verbose(1) << "I cannot find a boundary point to the tessel point " << *trianglePoints[0] << "." << endl;
     2749        return NULL;
     2750      }
     2751    }
     2752  } else {
     2753    set<TesselPoint*> *connectedPoints = GetAllConnectedPoints(trianglePoints[0]);
     2754    TesselPointList *connectedClosestPoints = GetCircleOfSetOfPoints(connectedPoints, trianglePoints[0], x);
     2755    delete(connectedPoints);
     2756    if (connectedClosestPoints != NULL) {
     2757      trianglePoints[1] = connectedClosestPoints->front();
     2758      trianglePoints[2] = connectedClosestPoints->back();
     2759      for (int i=0;i<3;i++) {
     2760        if (trianglePoints[i] == NULL) {
     2761          eLog() << Verbose(1) << "IsInnerPoint encounters serious error, point " << i << " not found." << endl;
    35532762        }
    3554       } else { // intersection is closer, calculate
    3555         // calculate closest point on line to desired point
    3556         BaseLineIntersection.CopyVector(x);
    3557         BaseLineIntersection.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3558         Center.CopyVector(&BaseLineIntersection);
    3559         Center.ProjectOntoPlane(&BaseLine);
    3560         BaseLineIntersection.SubtractVector(&Center);
    3561         const double distance = BaseLineIntersection.NormSquared();
    3562         if (Center.NormSquared() > BaseLine.NormSquared()) {
    3563           DoeLog(0) && (eLog() << Verbose(0) << "Algorithmic error: In second case we have intersection outside of baseline!" << endl);
    3564         }
    3565         if ((ClosestLines.empty()) || (distance < MinDistance)) {
    3566           ClosestLines.insert(LineRunner->second);
    3567           MinDistance = distance;
    3568           DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Intersection in between endpoints, new closest line " << *LineRunner->second << " is " << *ClosestLines.begin() << " with projected distance " << MinDistance << "." << endl);
    3569         } else {
    3570           DoLog(2) && (Log() << Verbose(2) << "REJECT: Point is further away from line " << *LineRunner->second << " than present closest line: " << distance << " >> " << MinDistance << "." << endl);
    3571         }
     2763        //Log() << Verbose(1) << "List of triangle points:" << endl;
     2764        //Log() << Verbose(2) << *trianglePoints[i] << endl;
    35722765      }
    3573     }
    3574   }
    3575   delete (points);
    3576 
    3577   // check whether closest line is "too close" :), then it's inside
    3578   if (ClosestLines.empty()) {
    3579     DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl);
     2766
     2767      triangles = FindTriangles(trianglePoints);
     2768      Log() << Verbose(1) << "List of possible triangles:" << endl;
     2769      for(list<BoundaryTriangleSet*>::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++)
     2770        Log() << Verbose(2) << **Runner << endl;
     2771
     2772      delete(connectedClosestPoints);
     2773    } else {
     2774      triangles = NULL;
     2775      eLog() << Verbose(2) << "There is no circle of connected points!" << endl;
     2776    }
     2777  }
     2778 
     2779  if ((triangles == NULL) || (triangles->empty())) {
     2780    eLog() << Verbose(1) << "There is no nearest triangle. Please check the tesselation structure.";
     2781    delete(triangles);
    35802782    return NULL;
    3581   }
    3582   TriangleList * candidates = new TriangleList;
    3583   for (LineSet::iterator LineRunner = ClosestLines.begin(); LineRunner != ClosestLines.end(); LineRunner++)
    3584     for (TriangleMap::iterator Runner = (*LineRunner)->triangles.begin(); Runner != (*LineRunner)->triangles.end(); Runner++) {
    3585       candidates->push_back(Runner->second);
    3586     }
    3587   return candidates;
    3588 }
    3589 ;
     2783  } else
     2784    return triangles;
     2785};
    35902786
    35912787/** Finds closest triangle to a point.
     
    35932789 * \param *out output stream for debugging
    35942790 * \param *x Vector to look from
    3595  * \param &distance contains found distance on return
    35962791 * \return list of BoundaryTriangleSet of nearest triangles or NULL.
    35972792 */
    3598 class BoundaryTriangleSet * Tesselation::FindClosestTriangleToVector(const Vector *x, const LinkedCell* LC) const
    3599 {
    3600   Info FunctionInfo(__func__);
     2793class BoundaryTriangleSet * Tesselation::FindClosestTriangleToPoint(const Vector *x, const LinkedCell* LC) const
     2794{
     2795        Info FunctionInfo(__func__);
    36012796  class BoundaryTriangleSet *result = NULL;
    3602   TriangleList *triangles = FindClosestTrianglesToVector(x, LC);
    3603   TriangleList candidates;
     2797  list<BoundaryTriangleSet*> *triangles = FindClosestTrianglesToPoint(x, LC);
    36042798  Vector Center;
    3605   Vector helper;
    3606 
    3607   if ((triangles == NULL) || (triangles->empty()))
     2799
     2800  if (triangles == NULL)
    36082801    return NULL;
    36092802
    3610   // go through all and pick the one with the best alignment to x
    3611   double MinAlignment = 2. * M_PI;
    3612   for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++) {
    3613     (*Runner)->GetCenter(&Center);
    3614     helper.CopyVector(x);
    3615     helper.SubtractVector(&Center);
    3616     const double Alignment = helper.Angle(&(*Runner)->NormalVector);
    3617     if (Alignment < MinAlignment) {
    3618       result = *Runner;
    3619       MinAlignment = Alignment;
    3620       DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Triangle " << *result << " is better aligned with " << MinAlignment << "." << endl);
    3621     } else {
    3622       DoLog(1) && (Log() << Verbose(1) << "REJECT: Triangle " << *result << " is worse aligned with " << MinAlignment << "." << endl);
    3623     }
    3624   }
    3625   delete (triangles);
    3626 
     2803  if (triangles->size() == 1) { // there is no degenerate case
     2804    result = triangles->front();
     2805    Log() << Verbose(1) << "Normal Vector of this triangle is " << result->NormalVector << "." << endl;
     2806  } else {
     2807    result = triangles->front();
     2808    result->GetCenter(&Center);
     2809    Center.SubtractVector(x);
     2810    Log() << Verbose(1) << "Normal Vector of this front side is " << result->NormalVector << "." << endl;
     2811    if (Center.ScalarProduct(&result->NormalVector) < 0) {
     2812      result = triangles->back();
     2813      Log() << Verbose(1) << "Normal Vector of this back side is " << result->NormalVector << "." << endl;
     2814      if (Center.ScalarProduct(&result->NormalVector) < 0) {
     2815        eLog() << Verbose(1) << "Front and back side yield NormalVector in wrong direction!" << endl;
     2816      }
     2817    }
     2818  }
     2819  delete(triangles);
    36272820  return result;
    3628 }
    3629 ;
    3630 
    3631 /** Checks whether the provided Vector is within the Tesselation structure.
    3632  * Basically calls Tesselation::GetDistanceToSurface() and checks the sign of the return value.
    3633  * @param point of which to check the position
    3634  * @param *LC LinkedCell structure
    3635  *
    3636  * @return true if the point is inside the Tesselation structure, false otherwise
    3637  */
    3638 bool Tesselation::IsInnerPoint(const Vector &Point, const LinkedCell* const LC) const
    3639 {
    3640   Info FunctionInfo(__func__);
    3641   TriangleIntersectionList Intersections(&Point, this, LC);
    3642 
    3643   return Intersections.IsInside();
    3644 }
    3645 ;
    3646 
    3647 /** Returns the distance to the surface given by the tesselation.
    3648  * Calls FindClosestTriangleToVector() and checks whether the resulting triangle's BoundaryTriangleSet#NormalVector points
    3649  * towards or away from the given \a &Point. Additionally, we check whether it's normal to the normal vector, i.e. on the
    3650  * closest triangle's plane. Then, we have to check whether \a Point is inside the triangle or not to determine whether it's
    3651  * an inside or outside point. This is done by calling BoundaryTriangleSet::GetIntersectionInsideTriangle().
    3652  * In the end we additionally find the point on the triangle who was smallest distance to \a Point:
    3653  *  -# Separate distance from point to center in vector in NormalDirection and on the triangle plane.
    3654  *  -# Check whether vector on triangle plane points inside the triangle or crosses triangle bounds.
    3655  *  -# If inside, take it to calculate closest distance
    3656  *  -# If not, take intersection with BoundaryLine as distance
    3657  *
    3658  * @note distance is squared despite it still contains a sign to determine in-/outside!
     2821};
     2822
     2823/** Checks whether the provided Vector is within the tesselation structure.
    36592824 *
    36602825 * @param point of which to check the position
    36612826 * @param *LC LinkedCell structure
    36622827 *
    3663  * @return >0 if outside, ==0 if on surface, <0 if inside
    3664  */
    3665 double Tesselation::GetDistanceSquaredToTriangle(const Vector &Point, const BoundaryTriangleSet* const triangle) const
    3666 {
    3667   Info FunctionInfo(__func__);
     2828 * @return true if the point is inside the tesselation structure, false otherwise
     2829 */
     2830bool Tesselation::IsInnerPoint(const Vector &Point, const LinkedCell* const LC) const
     2831{
     2832        Info FunctionInfo(__func__);
     2833  class BoundaryTriangleSet *result = FindClosestTriangleToPoint(&Point, LC);
    36682834  Vector Center;
    3669   Vector helper;
    3670   Vector DistanceToCenter;
    3671   Vector Intersection;
    3672   double distance = 0.;
    3673 
    3674   if (triangle == NULL) {// is boundary point or only point in point cloud?
    3675     DoLog(1) && (Log() << Verbose(1) << "No triangle given!" << endl);
    3676     return -1.;
     2835
     2836  if (result == NULL) {// is boundary point or only point in point cloud?
     2837    Log() << Verbose(1) << Point << " is the only point in vicinity." << endl;
     2838    return false;
     2839  }
     2840
     2841  result->GetCenter(&Center);
     2842  Log() << Verbose(2) << "INFO: Central point of the triangle is " << Center << "." << endl;
     2843  Center.SubtractVector(&Point);
     2844  Log() << Verbose(2) << "INFO: Vector from center to point to test is " << Center << "." << endl;
     2845  if (Center.ScalarProduct(&result->NormalVector) > -MYEPSILON) {
     2846    Log() << Verbose(1) << Point << " is an inner point." << endl;
     2847    return true;
    36772848  } else {
    3678     DoLog(1) && (Log() << Verbose(1) << "INFO: Closest triangle found is " << *triangle << " with normal vector " << triangle->NormalVector << "." << endl);
    3679   }
    3680 
    3681   triangle->GetCenter(&Center);
    3682   DoLog(2) && (Log() << Verbose(2) << "INFO: Central point of the triangle is " << Center << "." << endl);
    3683   DistanceToCenter.CopyVector(&Center);
    3684   DistanceToCenter.SubtractVector(&Point);
    3685   DoLog(2) && (Log() << Verbose(2) << "INFO: Vector from point to test to center is " << DistanceToCenter << "." << endl);
    3686 
    3687   // check whether we are on boundary
    3688   if (fabs(DistanceToCenter.ScalarProduct(&triangle->NormalVector)) < MYEPSILON) {
    3689     // calculate whether inside of triangle
    3690     DistanceToCenter.CopyVector(&Point);
    3691     Center.CopyVector(&Point);
    3692     Center.SubtractVector(&triangle->NormalVector); // points towards MolCenter
    3693     DistanceToCenter.AddVector(&triangle->NormalVector); // points outside
    3694     DoLog(1) && (Log() << Verbose(1) << "INFO: Calling Intersection with " << Center << " and " << DistanceToCenter << "." << endl);
    3695     if (triangle->GetIntersectionInsideTriangle(&Center, &DistanceToCenter, &Intersection)) {
    3696       DoLog(1) && (Log() << Verbose(1) << Point << " is inner point: sufficiently close to boundary, " << Intersection << "." << endl);
    3697       return 0.;
    3698     } else {
    3699       DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point: on triangle plane but outside of triangle bounds." << endl);
    3700       return false;
    3701     }
    3702   } else {
    3703     // calculate smallest distance
    3704     distance = triangle->GetClosestPointInsideTriangle(&Point, &Intersection);
    3705     DoLog(1) && (Log() << Verbose(1) << "Closest point on triangle is " << Intersection << "." << endl);
    3706 
    3707     // then check direction to boundary
    3708     if (DistanceToCenter.ScalarProduct(&triangle->NormalVector) > MYEPSILON) {
    3709       DoLog(1) && (Log() << Verbose(1) << Point << " is an inner point, " << distance << " below surface." << endl);
    3710       return -distance;
    3711     } else {
    3712       DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point, " << distance << " above surface." << endl);
    3713       return +distance;
    3714     }
     2849    Log() << Verbose(1) << Point << " is NOT an inner point." << endl;
     2850    return false;
    37152851  }
    37162852}
    3717 ;
    3718 
    3719 /** Calculates minimum distance from \a&Point to a tesselated surface.
    3720  * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle().
    3721  * \param &Point point to calculate distance from
    3722  * \param *LC needed for finding closest points fast
    3723  * \return distance squared to closest point on surface
    3724  */
    3725 double Tesselation::GetDistanceToSurface(const Vector &Point, const LinkedCell* const LC) const
    3726 {
    3727   Info FunctionInfo(__func__);
    3728   TriangleIntersectionList Intersections(&Point, this, LC);
    3729 
    3730   return Intersections.GetSmallestDistance();
     2853
     2854/** Checks whether the provided TesselPoint is within the tesselation structure.
     2855 *
     2856 * @param *Point of which to check the position
     2857 * @param *LC Linked Cell structure
     2858 *
     2859 * @return true if the point is inside the tesselation structure, false otherwise
     2860 */
     2861bool Tesselation::IsInnerPoint(const TesselPoint * const Point, const LinkedCell* const LC) const
     2862{
     2863        Info FunctionInfo(__func__);
     2864  return IsInnerPoint(*(Point->node), LC);
    37312865}
    3732 ;
    3733 
    3734 /** Calculates minimum distance from \a&Point to a tesselated surface.
    3735  * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle().
    3736  * \param &Point point to calculate distance from
    3737  * \param *LC needed for finding closest points fast
    3738  * \return distance squared to closest point on surface
    3739  */
    3740 BoundaryTriangleSet * Tesselation::GetClosestTriangleOnSurface(const Vector &Point, const LinkedCell* const LC) const
    3741 {
    3742   Info FunctionInfo(__func__);
    3743   TriangleIntersectionList Intersections(&Point, this, LC);
    3744 
    3745   return Intersections.GetClosestTriangle();
    3746 }
    3747 ;
    37482866
    37492867/** Gets all points connected to the provided point by triangulation lines.
     
    37532871 * @return set of the all points linked to the provided one
    37542872 */
    3755 TesselPointSet * Tesselation::GetAllConnectedPoints(const TesselPoint* const Point) const
    3756 {
    3757   Info FunctionInfo(__func__);
    3758   TesselPointSet *connectedPoints = new TesselPointSet;
     2873set<TesselPoint*> * Tesselation::GetAllConnectedPoints(const TesselPoint* const Point) const
     2874{
     2875        Info FunctionInfo(__func__);
     2876  set<TesselPoint*> *connectedPoints = new set<TesselPoint*>;
    37592877  class BoundaryPointSet *ReferencePoint = NULL;
    37602878  TesselPoint* current;
    37612879  bool takePoint = false;
     2880
    37622881  // find the respective boundary point
    37632882  PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
     
    37652884    ReferencePoint = PointRunner->second;
    37662885  } else {
    3767     DoeLog(2) && (eLog() << Verbose(2) << "GetAllConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl);
     2886    eLog() << Verbose(2) << "GetAllConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl;
    37682887    ReferencePoint = NULL;
    37692888  }
     
    37712890  // little trick so that we look just through lines connect to the BoundaryPoint
    37722891  // OR fall-back to look through all lines if there is no such BoundaryPoint
    3773   const LineMap *Lines;
    3774   ;
     2892  const LineMap *Lines;;
    37752893  if (ReferencePoint != NULL)
    37762894    Lines = &(ReferencePoint->lines);
     
    37792897  LineMap::const_iterator findLines = Lines->begin();
    37802898  while (findLines != Lines->end()) {
    3781     takePoint = false;
    3782 
    3783     if (findLines->second->endpoints[0]->Nr == Point->nr) {
    3784       takePoint = true;
    3785       current = findLines->second->endpoints[1]->node;
    3786     } else if (findLines->second->endpoints[1]->Nr == Point->nr) {
    3787       takePoint = true;
    3788       current = findLines->second->endpoints[0]->node;
    3789     }
    3790 
    3791     if (takePoint) {
    3792       DoLog(1) && (Log() << Verbose(1) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is enlisted." << endl);
    3793       connectedPoints->insert(current);
    3794     }
    3795 
    3796     findLines++;
    3797   }
    3798 
    3799   if (connectedPoints->empty()) { // if have not found any points
    3800     DoeLog(1) && (eLog() << Verbose(1) << "We have not found any connected points to " << *Point << "." << endl);
     2899   takePoint = false;
     2900
     2901   if (findLines->second->endpoints[0]->Nr == Point->nr) {
     2902     takePoint = true;
     2903     current = findLines->second->endpoints[1]->node;
     2904   } else if (findLines->second->endpoints[1]->Nr == Point->nr) {
     2905     takePoint = true;
     2906     current = findLines->second->endpoints[0]->node;
     2907   }
     2908
     2909   if (takePoint) {
     2910     Log() << Verbose(1) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is enlisted." << endl;
     2911     connectedPoints->insert(current);
     2912   }
     2913
     2914   findLines++;
     2915  }
     2916
     2917  if (connectedPoints->size() == 0) { // if have not found any points
     2918    eLog() << Verbose(1) << "We have not found any connected points to " << *Point<< "." << endl;
    38012919    return NULL;
    38022920  }
    38032921
    38042922  return connectedPoints;
    3805 }
    3806 ;
     2923};
     2924
    38072925
    38082926/** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point.
     
    38182936 * @return list of the all points linked to the provided one
    38192937 */
    3820 TesselPointList * Tesselation::GetCircleOfConnectedTriangles(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const
    3821 {
    3822   Info FunctionInfo(__func__);
     2938list<TesselPoint*> * Tesselation::GetCircleOfSetOfPoints(set<TesselPoint*> *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const
     2939{
     2940        Info FunctionInfo(__func__);
    38232941  map<double, TesselPoint*> anglesOfPoints;
    3824   TesselPointList *connectedCircle = new TesselPointList;
     2942  list<TesselPoint*> *connectedCircle = new list<TesselPoint*>;
     2943  Vector center;
    38252944  Vector PlaneNormal;
    38262945  Vector AngleZero;
    38272946  Vector OrthogonalVector;
    38282947  Vector helper;
    3829   const TesselPoint * const TrianglePoints[3] = { Point, NULL, NULL };
    3830   TriangleList *triangles = NULL;
    38312948
    38322949  if (SetOfNeighbours == NULL) {
    3833     DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl);
    3834     delete (connectedCircle);
     2950    eLog() << Verbose(2) << "Could not find any connected points!" << endl;
     2951    delete(connectedCircle);
    38352952    return NULL;
    38362953  }
    38372954
    38382955  // calculate central point
    3839   triangles = FindTriangles(TrianglePoints);
    3840   if ((triangles != NULL) && (!triangles->empty())) {
    3841     for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++)
    3842       PlaneNormal.AddVector(&(*Runner)->NormalVector);
    3843   } else {
    3844     DoeLog(0) && (eLog() << Verbose(0) << "Could not find any triangles for point " << *Point << "." << endl);
    3845     performCriticalExit();
    3846   }
    3847   PlaneNormal.Scale(1.0 / triangles->size());
    3848   DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated PlaneNormal of all circle points is " << PlaneNormal << "." << endl);
     2956  for (set<TesselPoint*>::const_iterator TesselRunner = SetOfNeighbours->begin(); TesselRunner != SetOfNeighbours->end(); TesselRunner++)
     2957    center.AddVector((*TesselRunner)->node);
     2958  //Log() << Verbose(0) << "Summed vectors " << center << "; number of points " << connectedPoints.size()
     2959  //  << "; scale factor " << 1.0/connectedPoints.size();
     2960  center.Scale(1.0/SetOfNeighbours->size());
     2961  Log() << Verbose(1) << "INFO: Calculated center of all circle points is " << center << "." << endl;
     2962
     2963  // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points
     2964  PlaneNormal.CopyVector(Point->node);
     2965  PlaneNormal.SubtractVector(&center);
    38492966  PlaneNormal.Normalize();
     2967  Log() << Verbose(1) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl;
    38502968
    38512969  // construct one orthogonal vector
     
    38552973    AngleZero.ProjectOntoPlane(&PlaneNormal);
    38562974  }
    3857   if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON)) {
    3858     DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl);
     2975  if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON )) {
     2976    Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl;
    38592977    AngleZero.CopyVector((*SetOfNeighbours->begin())->node);
    38602978    AngleZero.SubtractVector(Point->node);
    38612979    AngleZero.ProjectOntoPlane(&PlaneNormal);
    38622980    if (AngleZero.NormSquared() < MYEPSILON) {
    3863       DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl);
     2981      eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl;
    38642982      performCriticalExit();
    38652983    }
    38662984  }
    3867   DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl);
     2985  Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl;
    38682986  if (AngleZero.NormSquared() > MYEPSILON)
    38692987    OrthogonalVector.MakeNormalVector(&PlaneNormal, &AngleZero);
    38702988  else
    38712989    OrthogonalVector.MakeNormalVector(&PlaneNormal);
    3872   DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl);
     2990  Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl;
    38732991
    38742992  // go through all connected points and calculate angle
    3875   for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) {
     2993  for (set<TesselPoint*>::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) {
    38762994    helper.CopyVector((*listRunner)->node);
    38772995    helper.SubtractVector(Point->node);
    38782996    helper.ProjectOntoPlane(&PlaneNormal);
    38792997    double angle = GetAngle(helper, AngleZero, OrthogonalVector);
    3880     DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl);
    3881     anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner)));
    3882   }
    3883 
    3884   for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
    3885     connectedCircle->push_back(AngleRunner->second);
    3886   }
    3887 
    3888   return connectedCircle;
    3889 }
    3890 
    3891 /** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point.
    3892  * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points
    3893  * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given
    3894  * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the
    3895  * triangle we are looking for.
    3896  *
    3897  * @param *SetOfNeighbours all points for which the angle should be calculated
    3898  * @param *Point of which get all connected points
    3899  * @param *Reference Reference vector for zero angle or NULL for no preference
    3900  * @return list of the all points linked to the provided one
    3901  */
    3902 TesselPointList * Tesselation::GetCircleOfSetOfPoints(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const
    3903 {
    3904   Info FunctionInfo(__func__);
    3905   map<double, TesselPoint*> anglesOfPoints;
    3906   TesselPointList *connectedCircle = new TesselPointList;
    3907   Vector center;
    3908   Vector PlaneNormal;
    3909   Vector AngleZero;
    3910   Vector OrthogonalVector;
    3911   Vector helper;
    3912 
    3913   if (SetOfNeighbours == NULL) {
    3914     DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl);
    3915     delete (connectedCircle);
    3916     return NULL;
    3917   }
    3918 
    3919   // check whether there's something to do
    3920   if (SetOfNeighbours->size() < 3) {
    3921     for (TesselPointSet::iterator TesselRunner = SetOfNeighbours->begin(); TesselRunner != SetOfNeighbours->end(); TesselRunner++)
    3922       connectedCircle->push_back(*TesselRunner);
    3923     return connectedCircle;
    3924   }
    3925 
    3926   DoLog(1) && (Log() << Verbose(1) << "INFO: Point is " << *Point << " and Reference is " << *Reference << "." << endl);
    3927   // calculate central point
    3928   TesselPointSet::const_iterator TesselA = SetOfNeighbours->begin();
    3929   TesselPointSet::const_iterator TesselB = SetOfNeighbours->begin();
    3930   TesselPointSet::const_iterator TesselC = SetOfNeighbours->begin();
    3931   TesselB++;
    3932   TesselC++;
    3933   TesselC++;
    3934   int counter = 0;
    3935   while (TesselC != SetOfNeighbours->end()) {
    3936     helper.MakeNormalVector((*TesselA)->node, (*TesselB)->node, (*TesselC)->node);
    3937     DoLog(0) && (Log() << Verbose(0) << "Making normal vector out of " << *(*TesselA) << ", " << *(*TesselB) << " and " << *(*TesselC) << ":" << helper << endl);
    3938     counter++;
    3939     TesselA++;
    3940     TesselB++;
    3941     TesselC++;
    3942     PlaneNormal.AddVector(&helper);
    3943   }
    3944   //Log() << Verbose(0) << "Summed vectors " << center << "; number of points " << connectedPoints.size()
    3945   //  << "; scale factor " << counter;
    3946   PlaneNormal.Scale(1.0 / (double) counter);
    3947   //  Log() << Verbose(1) << "INFO: Calculated center of all circle points is " << center << "." << endl;
    3948   //
    3949   //  // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points
    3950   //  PlaneNormal.CopyVector(Point->node);
    3951   //  PlaneNormal.SubtractVector(&center);
    3952   //  PlaneNormal.Normalize();
    3953   DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl);
    3954 
    3955   // construct one orthogonal vector
    3956   if (Reference != NULL) {
    3957     AngleZero.CopyVector(Reference);
    3958     AngleZero.SubtractVector(Point->node);
    3959     AngleZero.ProjectOntoPlane(&PlaneNormal);
    3960   }
    3961   if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON)) {
    3962     DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl);
    3963     AngleZero.CopyVector((*SetOfNeighbours->begin())->node);
    3964     AngleZero.SubtractVector(Point->node);
    3965     AngleZero.ProjectOntoPlane(&PlaneNormal);
    3966     if (AngleZero.NormSquared() < MYEPSILON) {
    3967       DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl);
    3968       performCriticalExit();
    3969     }
    3970   }
    3971   DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl);
    3972   if (AngleZero.NormSquared() > MYEPSILON)
    3973     OrthogonalVector.MakeNormalVector(&PlaneNormal, &AngleZero);
    3974   else
    3975     OrthogonalVector.MakeNormalVector(&PlaneNormal);
    3976   DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl);
    3977 
    3978   // go through all connected points and calculate angle
    3979   pair<map<double, TesselPoint*>::iterator, bool> InserterTest;
    3980   for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) {
    3981     helper.CopyVector((*listRunner)->node);
    3982     helper.SubtractVector(Point->node);
    3983     helper.ProjectOntoPlane(&PlaneNormal);
    3984     double angle = GetAngle(helper, AngleZero, OrthogonalVector);
    3985     if (angle > M_PI) // the correction is of no use here (and not desired)
    3986       angle = 2. * M_PI - angle;
    3987     DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle between " << helper << " and " << AngleZero << " is " << angle << " for point " << **listRunner << "." << endl);
    3988     InserterTest = anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner)));
    3989     if (!InserterTest.second) {
    3990       DoeLog(0) && (eLog() << Verbose(0) << "GetCircleOfSetOfPoints() got two atoms with same angle: " << *((InserterTest.first)->second) << " and " << (*listRunner) << endl);
    3991       performCriticalExit();
    3992     }
    3993   }
    3994 
    3995   for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
     2998    Log() << Verbose(0) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl;
     2999    anglesOfPoints.insert(pair<double, TesselPoint*>(angle, (*listRunner)));
     3000  }
     3001
     3002  for(map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
    39963003    connectedCircle->push_back(AngleRunner->second);
    39973004  }
     
    40063013 * @return list of the all points linked to the provided one
    40073014 */
    4008 ListOfTesselPointList * Tesselation::GetPathsOfConnectedPoints(const TesselPoint* const Point) const
    4009 {
    4010   Info FunctionInfo(__func__);
     3015list<list<TesselPoint*> *> * Tesselation::GetPathsOfConnectedPoints(const TesselPoint* const Point) const
     3016{
     3017        Info FunctionInfo(__func__);
    40113018  map<double, TesselPoint*> anglesOfPoints;
    4012   list<TesselPointList *> *ListOfPaths = new list<TesselPointList *> ;
    4013   TesselPointList *connectedPath = NULL;
     3019  list<list<TesselPoint*> *> *ListOfPaths = new list<list<TesselPoint*> *>;
     3020  list<TesselPoint*> *connectedPath = NULL;
    40143021  Vector center;
    40153022  Vector PlaneNormal;
     
    40223029  class BoundaryLineSet *CurrentLine = NULL;
    40233030  class BoundaryLineSet *StartLine = NULL;
     3031
    40243032  // find the respective boundary point
    40253033  PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
     
    40273035    ReferencePoint = PointRunner->second;
    40283036  } else {
    4029     DoeLog(1) && (eLog() << Verbose(1) << "GetPathOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl);
     3037    eLog() << Verbose(1) << "GetPathOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl;
    40303038    return NULL;
    40313039  }
    40323040
    4033   map<class BoundaryLineSet *, bool> TouchedLine;
    4034   map<class BoundaryTriangleSet *, bool> TouchedTriangle;
    4035   map<class BoundaryLineSet *, bool>::iterator LineRunner;
    4036   map<class BoundaryTriangleSet *, bool>::iterator TriangleRunner;
     3041  map <class BoundaryLineSet *, bool> TouchedLine;
     3042  map <class BoundaryTriangleSet *, bool> TouchedTriangle;
     3043  map <class BoundaryLineSet *, bool>::iterator LineRunner;
     3044  map <class BoundaryTriangleSet *, bool>::iterator TriangleRunner;
    40373045  for (LineMap::iterator Runner = ReferencePoint->lines.begin(); Runner != ReferencePoint->lines.end(); Runner++) {
    4038     TouchedLine.insert(pair<class BoundaryLineSet *, bool> (Runner->second, false));
     3046    TouchedLine.insert( pair <class BoundaryLineSet *, bool>(Runner->second, false) );
    40393047    for (TriangleMap::iterator Sprinter = Runner->second->triangles.begin(); Sprinter != Runner->second->triangles.end(); Sprinter++)
    4040       TouchedTriangle.insert(pair<class BoundaryTriangleSet *, bool> (Sprinter->second, false));
     3048      TouchedTriangle.insert( pair <class BoundaryTriangleSet *, bool>(Sprinter->second, false) );
    40413049  }
    40423050  if (!ReferencePoint->lines.empty()) {
     
    40443052      LineRunner = TouchedLine.find(runner->second);
    40453053      if (LineRunner == TouchedLine.end()) {
    4046         DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *runner->second << " in the touched list." << endl);
     3054        eLog() << Verbose(1) << "I could not find " << *runner->second << " in the touched list." << endl;
    40473055      } else if (!LineRunner->second) {
    40483056        LineRunner->second = true;
    4049         connectedPath = new TesselPointList;
     3057        connectedPath = new list<TesselPoint*>;
    40503058        triangle = NULL;
    40513059        CurrentLine = runner->second;
    40523060        StartLine = CurrentLine;
    40533061        CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint);
    4054         DoLog(1) && (Log() << Verbose(1) << "INFO: Beginning path retrieval at " << *CurrentPoint << " of line " << *CurrentLine << "." << endl);
     3062        Log() << Verbose(1)<< "INFO: Beginning path retrieval at " << *CurrentPoint << " of line " << *CurrentLine << "." << endl;
    40553063        do {
    40563064          // push current one
    4057           DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl);
     3065          Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl;
    40583066          connectedPath->push_back(CurrentPoint->node);
    40593067
    40603068          // find next triangle
    40613069          for (TriangleMap::iterator Runner = CurrentLine->triangles.begin(); Runner != CurrentLine->triangles.end(); Runner++) {
    4062             DoLog(1) && (Log() << Verbose(1) << "INFO: Inspecting triangle " << *Runner->second << "." << endl);
     3070            Log() << Verbose(1) << "INFO: Inspecting triangle " << *Runner->second << "." << endl;
    40633071            if ((Runner->second != triangle)) { // look for first triangle not equal to old one
    40643072              triangle = Runner->second;
     
    40673075                if (!TriangleRunner->second) {
    40683076                  TriangleRunner->second = true;
    4069                   DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting triangle is " << *triangle << "." << endl);
     3077                  Log() << Verbose(1) << "INFO: Connecting triangle is " << *triangle << "." << endl;
    40703078                  break;
    40713079                } else {
    4072                   DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *triangle << ", as we have already visited it." << endl);
     3080                  Log() << Verbose(1) << "INFO: Skipping " << *triangle << ", as we have already visited it." << endl;
    40733081                  triangle = NULL;
    40743082                }
    40753083              } else {
    4076                 DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *triangle << " in the touched list." << endl);
     3084                eLog() << Verbose(1) << "I could not find " << *triangle << " in the touched list." << endl;
    40773085                triangle = NULL;
    40783086              }
     
    40823090            break;
    40833091          // find next line
    4084           for (int i = 0; i < 3; i++) {
     3092          for (int i=0;i<3;i++) {
    40853093            if ((triangle->lines[i] != CurrentLine) && (triangle->lines[i]->ContainsBoundaryPoint(ReferencePoint))) { // not the current line and still containing Point
    40863094              CurrentLine = triangle->lines[i];
    4087               DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting line is " << *CurrentLine << "." << endl);
     3095              Log() << Verbose(1) << "INFO: Connecting line is " << *CurrentLine << "." << endl;
    40883096              break;
    40893097            }
     
    40913099          LineRunner = TouchedLine.find(CurrentLine);
    40923100          if (LineRunner == TouchedLine.end())
    4093             DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *CurrentLine << " in the touched list." << endl);
     3101            eLog() << Verbose(1) << "I could not find " << *CurrentLine << " in the touched list." << endl;
    40943102          else
    40953103            LineRunner->second = true;
     
    40993107        } while (CurrentLine != StartLine);
    41003108        // last point is missing, as it's on start line
    4101         DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl);
     3109        Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl;
    41023110        if (StartLine->GetOtherEndpoint(ReferencePoint)->node != connectedPath->back())
    41033111          connectedPath->push_back(StartLine->GetOtherEndpoint(ReferencePoint)->node);
     
    41053113        ListOfPaths->push_back(connectedPath);
    41063114      } else {
    4107         DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *runner->second << ", as we have already visited it." << endl);
     3115        Log() << Verbose(1) << "INFO: Skipping " << *runner->second << ", as we have already visited it." << endl;
    41083116      }
    41093117    }
    41103118  } else {
    4111     DoeLog(1) && (eLog() << Verbose(1) << "There are no lines attached to " << *ReferencePoint << "." << endl);
     3119    eLog() << Verbose(1) << "There are no lines attached to " << *ReferencePoint << "." << endl;
    41123120  }
    41133121
     
    41213129 * @return list of the closed paths
    41223130 */
    4123 ListOfTesselPointList * Tesselation::GetClosedPathsOfConnectedPoints(const TesselPoint* const Point) const
    4124 {
    4125   Info FunctionInfo(__func__);
    4126   list<TesselPointList *> *ListofPaths = GetPathsOfConnectedPoints(Point);
    4127   list<TesselPointList *> *ListofClosedPaths = new list<TesselPointList *> ;
    4128   TesselPointList *connectedPath = NULL;
    4129   TesselPointList *newPath = NULL;
     3131list<list<TesselPoint*> *> * Tesselation::GetClosedPathsOfConnectedPoints(const TesselPoint* const Point) const
     3132{
     3133        Info FunctionInfo(__func__);
     3134  list<list<TesselPoint*> *> *ListofPaths = GetPathsOfConnectedPoints(Point);
     3135  list<list<TesselPoint*> *> *ListofClosedPaths = new list<list<TesselPoint*> *>;
     3136  list<TesselPoint*> *connectedPath = NULL;
     3137  list<TesselPoint*> *newPath = NULL;
    41303138  int count = 0;
    4131   TesselPointList::iterator CircleRunner;
    4132   TesselPointList::iterator CircleStart;
    4133 
    4134   for (list<TesselPointList *>::iterator ListRunner = ListofPaths->begin(); ListRunner != ListofPaths->end(); ListRunner++) {
     3139
     3140
     3141  list<TesselPoint*>::iterator CircleRunner;
     3142  list<TesselPoint*>::iterator CircleStart;
     3143
     3144  for(list<list<TesselPoint*> *>::iterator ListRunner = ListofPaths->begin(); ListRunner != ListofPaths->end(); ListRunner++) {
    41353145    connectedPath = *ListRunner;
    41363146
    4137     DoLog(1) && (Log() << Verbose(1) << "INFO: Current path is " << connectedPath << "." << endl);
     3147    Log() << Verbose(1) << "INFO: Current path is " << connectedPath << "." << endl;
    41383148
    41393149    // go through list, look for reappearance of starting Point and count
    41403150    CircleStart = connectedPath->begin();
     3151
    41413152    // go through list, look for reappearance of starting Point and create list
    4142     TesselPointList::iterator Marker = CircleStart;
     3153    list<TesselPoint*>::iterator Marker = CircleStart;
    41433154    for (CircleRunner = CircleStart; CircleRunner != connectedPath->end(); CircleRunner++) {
    41443155      if ((*CircleRunner == *CircleStart) && (CircleRunner != CircleStart)) { // is not the very first point
    41453156        // we have a closed circle from Marker to new Marker
    4146         DoLog(1) && (Log() << Verbose(1) << count + 1 << ". closed path consists of: ");
    4147         newPath = new TesselPointList;
    4148         TesselPointList::iterator CircleSprinter = Marker;
     3157        Log() << Verbose(1) << count+1 << ". closed path consists of: ";
     3158        newPath = new list<TesselPoint*>;
     3159        list<TesselPoint*>::iterator CircleSprinter = Marker;
    41493160        for (; CircleSprinter != CircleRunner; CircleSprinter++) {
    41503161          newPath->push_back(*CircleSprinter);
    4151           DoLog(0) && (Log() << Verbose(0) << (**CircleSprinter) << " <-> ");
     3162          Log() << Verbose(0) << (**CircleSprinter) << " <-> ";
    41523163        }
    4153         DoLog(0) && (Log() << Verbose(0) << ".." << endl);
     3164        Log() << Verbose(0) << ".." << endl;
    41543165        count++;
    41553166        Marker = CircleRunner;
     
    41603171    }
    41613172  }
    4162   DoLog(1) && (Log() << Verbose(1) << "INFO: " << count << " closed additional path(s) have been created." << endl);
     3173  Log() << Verbose(1) << "INFO: " << count << " closed additional path(s) have been created." << endl;
    41633174
    41643175  // delete list of paths
     
    41663177    connectedPath = *(ListofPaths->begin());
    41673178    ListofPaths->remove(connectedPath);
    4168     delete (connectedPath);
    4169   }
    4170   delete (ListofPaths);
     3179    delete(connectedPath);
     3180  }
     3181  delete(ListofPaths);
    41713182
    41723183  // exit
    41733184  return ListofClosedPaths;
    4174 }
    4175 ;
     3185};
     3186
    41763187
    41773188/** Gets all belonging triangles for a given BoundaryPointSet.
     
    41803191 * \return pointer to allocated list of triangles
    41813192 */
    4182 TriangleSet *Tesselation::GetAllTriangles(const BoundaryPointSet * const Point) const
    4183 {
    4184   Info FunctionInfo(__func__);
    4185   TriangleSet *connectedTriangles = new TriangleSet;
     3193set<BoundaryTriangleSet*> *Tesselation::GetAllTriangles(const BoundaryPointSet * const Point) const
     3194{
     3195        Info FunctionInfo(__func__);
     3196  set<BoundaryTriangleSet*> *connectedTriangles = new set<BoundaryTriangleSet*>;
    41863197
    41873198  if (Point == NULL) {
    4188     DoeLog(1) && (eLog() << Verbose(1) << "Point given is NULL." << endl);
     3199    eLog() << Verbose(1) << "Point given is NULL." << endl;
    41893200  } else {
    41903201    // go through its lines and insert all triangles
    41913202    for (LineMap::const_iterator LineRunner = Point->lines.begin(); LineRunner != Point->lines.end(); LineRunner++)
    41923203      for (TriangleMap::iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
    4193         connectedTriangles->insert(TriangleRunner->second);
    4194       }
     3204      connectedTriangles->insert(TriangleRunner->second);
     3205    }
    41953206  }
    41963207
    41973208  return connectedTriangles;
    4198 }
    4199 ;
     3209};
     3210
    42003211
    42013212/** Removes a boundary point from the envelope while keeping it closed.
     
    42103221 * \return volume added to the volume inside the tesselated surface by the removal
    42113222 */
    4212 double Tesselation::RemovePointFromTesselatedSurface(class BoundaryPointSet *point)
    4213 {
     3223double Tesselation::RemovePointFromTesselatedSurface(class BoundaryPointSet *point) {
    42143224  class BoundaryLineSet *line = NULL;
    42153225  class BoundaryTriangleSet *triangle = NULL;
     
    42193229
    42203230  if (point == NULL) {
    4221     DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << point << ", it's NULL!" << endl);
     3231    eLog() << Verbose(1) << "Cannot remove the point " << point << ", it's NULL!" << endl;
    42223232    return 0.;
    42233233  } else
    4224     DoLog(0) && (Log() << Verbose(0) << "Removing point " << *point << " from tesselated boundary ..." << endl);
     3234    Log() << Verbose(0) << "Removing point " << *point << " from tesselated boundary ..." << endl;
    42253235
    42263236  // copy old location for the volume
     
    42293239  // get list of connected points
    42303240  if (point->lines.empty()) {
    4231     DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << *point << ", it's connected to no lines!" << endl);
     3241    eLog() << Verbose(1) << "Cannot remove the point " << *point << ", it's connected to no lines!" << endl;
    42323242    return 0.;
    42333243  }
    42343244
    4235   list<TesselPointList *> *ListOfClosedPaths = GetClosedPathsOfConnectedPoints(point->node);
    4236   TesselPointList *connectedPath = NULL;
     3245  list<list<TesselPoint*> *> *ListOfClosedPaths = GetClosedPathsOfConnectedPoints(point->node);
     3246  list<TesselPoint*> *connectedPath = NULL;
    42373247
    42383248  // gather all triangles
    42393249  for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++)
    4240     count += LineRunner->second->triangles.size();
    4241   TriangleMap Candidates;
     3250    count+=LineRunner->second->triangles.size();
     3251  map<class BoundaryTriangleSet *, int> Candidates;
    42423252  for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
    42433253    line = LineRunner->second;
    42443254    for (TriangleMap::iterator TriangleRunner = line->triangles.begin(); TriangleRunner != line->triangles.end(); TriangleRunner++) {
    42453255      triangle = TriangleRunner->second;
    4246       Candidates.insert(TrianglePair(triangle->Nr, triangle));
     3256      Candidates.insert( pair<class BoundaryTriangleSet *, int> (triangle, triangle->Nr) );
    42473257    }
    42483258  }
    42493259
    42503260  // remove all triangles
    4251   count = 0;
     3261  count=0;
    42523262  NormalVector.Zero();
    4253   for (TriangleMap::iterator Runner = Candidates.begin(); Runner != Candidates.end(); Runner++) {
    4254     DoLog(1) && (Log() << Verbose(1) << "INFO: Removing triangle " << *(Runner->second) << "." << endl);
    4255     NormalVector.SubtractVector(&Runner->second->NormalVector); // has to point inward
    4256     RemoveTesselationTriangle(Runner->second);
     3263  for (map<class BoundaryTriangleSet *, int>::iterator Runner = Candidates.begin(); Runner != Candidates.end(); Runner++) {
     3264    Log() << Verbose(1) << "INFO: Removing triangle " << *(Runner->first) << "." << endl;
     3265    NormalVector.SubtractVector(&Runner->first->NormalVector); // has to point inward
     3266    RemoveTesselationTriangle(Runner->first);
    42573267    count++;
    42583268  }
    4259   DoLog(1) && (Log() << Verbose(1) << count << " triangles were removed." << endl);
    4260 
    4261   list<TesselPointList *>::iterator ListAdvance = ListOfClosedPaths->begin();
    4262   list<TesselPointList *>::iterator ListRunner = ListAdvance;
    4263   TriangleMap::iterator NumberRunner = Candidates.begin();
    4264   TesselPointList::iterator StartNode, MiddleNode, EndNode;
     3269  Log() << Verbose(1) << count << " triangles were removed." << endl;
     3270
     3271  list<list<TesselPoint*> *>::iterator ListAdvance = ListOfClosedPaths->begin();
     3272  list<list<TesselPoint*> *>::iterator ListRunner = ListAdvance;
     3273  map<class BoundaryTriangleSet *, int>::iterator NumberRunner = Candidates.begin();
     3274  list<TesselPoint*>::iterator StartNode, MiddleNode, EndNode;
    42653275  double angle;
    42663276  double smallestangle;
    42673277  Vector Point, Reference, OrthogonalVector;
    4268   if (count > 2) { // less than three triangles, then nothing will be created
     3278  if (count > 2) {  // less than three triangles, then nothing will be created
    42693279    class TesselPoint *TriangleCandidates[3];
    42703280    count = 0;
    4271     for (; ListRunner != ListOfClosedPaths->end(); ListRunner = ListAdvance) { // go through all closed paths
     3281    for ( ; ListRunner != ListOfClosedPaths->end(); ListRunner = ListAdvance) { // go through all closed paths
    42723282      if (ListAdvance != ListOfClosedPaths->end())
    42733283        ListAdvance++;
    42743284
    42753285      connectedPath = *ListRunner;
     3286
    42763287      // re-create all triangles by going through connected points list
    4277       LineList NewLines;
    4278       for (; !connectedPath->empty();) {
     3288      list<class BoundaryLineSet *> NewLines;
     3289      for (;!connectedPath->empty();) {
    42793290        // search middle node with widest angle to next neighbours
    42803291        EndNode = connectedPath->end();
    42813292        smallestangle = 0.;
    42823293        for (MiddleNode = connectedPath->begin(); MiddleNode != connectedPath->end(); MiddleNode++) {
    4283           DoLog(1) && (Log() << Verbose(1) << "INFO: MiddleNode is " << **MiddleNode << "." << endl);
     3294          Log() << Verbose(1) << "INFO: MiddleNode is " << **MiddleNode << "." << endl;
    42843295          // construct vectors to next and previous neighbour
    42853296          StartNode = MiddleNode;
     
    43023313          angle = GetAngle(Point, Reference, OrthogonalVector);
    43033314          //if (angle < M_PI)  // no wrong-sided triangles, please?
    4304           if (fabs(angle - M_PI) < fabs(smallestangle - M_PI)) { // get straightest angle (i.e. construct those triangles with smallest area first)
    4305             smallestangle = angle;
    4306             EndNode = MiddleNode;
    4307           }
     3315            if(fabs(angle - M_PI) < fabs(smallestangle - M_PI)) { // get straightest angle (i.e. construct those triangles with smallest area first)
     3316              smallestangle = angle;
     3317              EndNode = MiddleNode;
     3318            }
    43083319        }
    43093320        MiddleNode = EndNode;
    43103321        if (MiddleNode == connectedPath->end()) {
    4311           DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: Could not find a smallest angle!" << endl);
     3322          eLog() << Verbose(0) << "CRITICAL: Could not find a smallest angle!" << endl;
    43123323          performCriticalExit();
    43133324        }
     
    43193330        if (EndNode == connectedPath->end())
    43203331          EndNode = connectedPath->begin();
    4321         DoLog(2) && (Log() << Verbose(2) << "INFO: StartNode is " << **StartNode << "." << endl);
    4322         DoLog(2) && (Log() << Verbose(2) << "INFO: MiddleNode is " << **MiddleNode << "." << endl);
    4323         DoLog(2) && (Log() << Verbose(2) << "INFO: EndNode is " << **EndNode << "." << endl);
    4324         DoLog(1) && (Log() << Verbose(1) << "INFO: Attempting to create triangle " << (*StartNode)->Name << ", " << (*MiddleNode)->Name << " and " << (*EndNode)->Name << "." << endl);
     3332        Log() << Verbose(2) << "INFO: StartNode is " << **StartNode << "." << endl;
     3333        Log() << Verbose(2) << "INFO: MiddleNode is " << **MiddleNode << "." << endl;
     3334        Log() << Verbose(2) << "INFO: EndNode is " << **EndNode << "." << endl;
     3335        Log() << Verbose(1) << "INFO: Attempting to create triangle " << (*StartNode)->Name << ", " << (*MiddleNode)->Name << " and " << (*EndNode)->Name << "." << endl;
    43253336        TriangleCandidates[0] = *StartNode;
    43263337        TriangleCandidates[1] = *MiddleNode;
     
    43283339        triangle = GetPresentTriangle(TriangleCandidates);
    43293340        if (triangle != NULL) {
    4330           DoeLog(0) && (eLog() << Verbose(0) << "New triangle already present, skipping!" << endl);
     3341          eLog() << Verbose(0) << "New triangle already present, skipping!" << endl;
    43313342          StartNode++;
    43323343          MiddleNode++;
     
    43403351          continue;
    43413352        }
    4342         DoLog(3) && (Log() << Verbose(3) << "Adding new triangle points." << endl);
     3353        Log() << Verbose(3) << "Adding new triangle points."<< endl;
    43433354        AddTesselationPoint(*StartNode, 0);
    43443355        AddTesselationPoint(*MiddleNode, 1);
    43453356        AddTesselationPoint(*EndNode, 2);
    4346         DoLog(3) && (Log() << Verbose(3) << "Adding new triangle lines." << endl);
    4347         AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
    4348         AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
     3357        Log() << Verbose(3) << "Adding new triangle lines."<< endl;
     3358        AddTesselationLine(TPS[0], TPS[1], 0);
     3359        AddTesselationLine(TPS[0], TPS[2], 1);
    43493360        NewLines.push_back(BLS[1]);
    4350         AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
     3361        AddTesselationLine(TPS[1], TPS[2], 2);
    43513362        BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    43523363        BTS->GetNormalVector(NormalVector);
     
    43593370        // prepare nodes for next triangle
    43603371        StartNode = EndNode;
    4361         DoLog(2) && (Log() << Verbose(2) << "Removing " << **MiddleNode << " from closed path, remaining points: " << connectedPath->size() << "." << endl);
     3372        Log() << Verbose(2) << "Removing " << **MiddleNode << " from closed path, remaining points: " << connectedPath->size() << "." << endl;
    43623373        connectedPath->remove(*MiddleNode); // remove the middle node (it is surrounded by triangles)
    43633374        if (connectedPath->size() == 2) { // we are done
     
    43663377          break;
    43673378        } else if (connectedPath->size() < 2) { // something's gone wrong!
    4368           DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: There are only two endpoints left!" << endl);
     3379          eLog() << Verbose(0) << "CRITICAL: There are only two endpoints left!" << endl;
    43693380          performCriticalExit();
    43703381        } else {
     
    43813392      // maximize the inner lines (we preferentially created lines with a huge angle, which is for the tesselation not wanted though useful for the closing)
    43823393      if (NewLines.size() > 1) {
    4383         LineList::iterator Candidate;
     3394        list<class BoundaryLineSet *>::iterator Candidate;
    43843395        class BoundaryLineSet *OtherBase = NULL;
    43853396        double tmp, maxgain;
    43863397        do {
    43873398          maxgain = 0;
    4388           for (LineList::iterator Runner = NewLines.begin(); Runner != NewLines.end(); Runner++) {
     3399          for(list<class BoundaryLineSet *>::iterator Runner = NewLines.begin(); Runner != NewLines.end(); Runner++) {
    43893400            tmp = PickFarthestofTwoBaselines(*Runner);
    43903401            if (maxgain < tmp) {
     
    43953406          if (maxgain != 0) {
    43963407            volume += maxgain;
    4397             DoLog(1) && (Log() << Verbose(1) << "Flipping baseline with highest volume" << **Candidate << "." << endl);
     3408            Log() << Verbose(1) << "Flipping baseline with highest volume" << **Candidate << "." << endl;
    43983409            OtherBase = FlipBaseline(*Candidate);
    43993410            NewLines.erase(Candidate);
     
    44043415
    44053416      ListOfClosedPaths->remove(connectedPath);
    4406       delete (connectedPath);
    4407     }
    4408     DoLog(0) && (Log() << Verbose(0) << count << " triangles were created." << endl);
     3417      delete(connectedPath);
     3418    }
     3419    Log() << Verbose(0) << count << " triangles were created." << endl;
    44093420  } else {
    44103421    while (!ListOfClosedPaths->empty()) {
     
    44123423      connectedPath = *ListRunner;
    44133424      ListOfClosedPaths->remove(connectedPath);
    4414       delete (connectedPath);
    4415     }
    4416     DoLog(0) && (Log() << Verbose(0) << "No need to create any triangles." << endl);
    4417   }
    4418   delete (ListOfClosedPaths);
    4419 
    4420   DoLog(0) && (Log() << Verbose(0) << "Removed volume is " << volume << "." << endl);
     3425      delete(connectedPath);
     3426    }
     3427    Log() << Verbose(0) << "No need to create any triangles." << endl;
     3428  }
     3429  delete(ListOfClosedPaths);
     3430
     3431  Log() << Verbose(0) << "Removed volume is " << volume << "." << endl;
    44213432
    44223433  return volume;
    4423 }
    4424 ;
     3434};
     3435
     3436
    44253437
    44263438/**
    44273439 * Finds triangles belonging to the three provided points.
    44283440 *
    4429  * @param *Points[3] list, is expected to contain three points (NULL means wildcard)
     3441 * @param *Points[3] list, is expected to contain three points
    44303442 *
    44313443 * @return triangles which belong to the provided points, will be empty if there are none,
    44323444 *         will usually be one, in case of degeneration, there will be two
    44333445 */
    4434 TriangleList *Tesselation::FindTriangles(const TesselPoint* const Points[3]) const
    4435 {
    4436   Info FunctionInfo(__func__);
    4437   TriangleList *result = new TriangleList;
     3446list<BoundaryTriangleSet*> *Tesselation::FindTriangles(const TesselPoint* const Points[3]) const
     3447{
     3448        Info FunctionInfo(__func__);
     3449  list<BoundaryTriangleSet*> *result = new list<BoundaryTriangleSet*>;
    44383450  LineMap::const_iterator FindLine;
    44393451  TriangleMap::const_iterator FindTriangle;
    44403452  class BoundaryPointSet *TrianglePoints[3];
    4441   size_t NoOfWildcards = 0;
    44423453
    44433454  for (int i = 0; i < 3; i++) {
    4444     if (Points[i] == NULL) {
    4445       NoOfWildcards++;
     3455    PointMap::const_iterator FindPoint = PointsOnBoundary.find(Points[i]->nr);
     3456    if (FindPoint != PointsOnBoundary.end()) {
     3457      TrianglePoints[i] = FindPoint->second;
     3458    } else {
    44463459      TrianglePoints[i] = NULL;
    4447     } else {
    4448       PointMap::const_iterator FindPoint = PointsOnBoundary.find(Points[i]->nr);
    4449       if (FindPoint != PointsOnBoundary.end()) {
    4450         TrianglePoints[i] = FindPoint->second;
    4451       } else {
    4452         TrianglePoints[i] = NULL;
    4453       }
    4454     }
    4455   }
    4456 
    4457   switch (NoOfWildcards) {
    4458     case 0: // checks lines between the points in the Points for their adjacent triangles
    4459       for (int i = 0; i < 3; i++) {
    4460         if (TrianglePoints[i] != NULL) {
    4461           for (int j = i + 1; j < 3; j++) {
    4462             if (TrianglePoints[j] != NULL) {
    4463               for (FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr); // is a multimap!
    4464               (FindLine != TrianglePoints[i]->lines.end()) && (FindLine->first == TrianglePoints[j]->node->nr); FindLine++) {
    4465                 for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) {
    4466                   if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
    4467                     result->push_back(FindTriangle->second);
    4468                   }
    4469                 }
     3460    }
     3461  }
     3462
     3463  // checks lines between the points in the Points for their adjacent triangles
     3464  for (int i = 0; i < 3; i++) {
     3465    if (TrianglePoints[i] != NULL) {
     3466      for (int j = i+1; j < 3; j++) {
     3467        if (TrianglePoints[j] != NULL) {
     3468          for (FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr); // is a multimap!
     3469              (FindLine != TrianglePoints[i]->lines.end()) && (FindLine->first == TrianglePoints[j]->node->nr);
     3470              FindLine++) {
     3471            for (FindTriangle = FindLine->second->triangles.begin();
     3472                FindTriangle != FindLine->second->triangles.end();
     3473                FindTriangle++) {
     3474              if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
     3475                result->push_back(FindTriangle->second);
    44703476              }
    4471               // Is it sufficient to consider one of the triangle lines for this.
    4472               return result;
    44733477            }
    44743478          }
     3479          // Is it sufficient to consider one of the triangle lines for this.
     3480          return result;
    44753481        }
    44763482      }
    4477       break;
    4478     case 1: // copy all triangles of the respective line
    4479     {
    4480       int i = 0;
    4481       for (; i < 3; i++)
    4482         if (TrianglePoints[i] == NULL)
    4483           break;
    4484       for (FindLine = TrianglePoints[(i + 1) % 3]->lines.find(TrianglePoints[(i + 2) % 3]->node->nr); // is a multimap!
    4485       (FindLine != TrianglePoints[(i + 1) % 3]->lines.end()) && (FindLine->first == TrianglePoints[(i + 2) % 3]->node->nr); FindLine++) {
    4486         for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) {
    4487           if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
    4488             result->push_back(FindTriangle->second);
    4489           }
    4490         }
    4491       }
    4492       break;
    4493     }
    4494     case 2: // copy all triangles of the respective point
    4495     {
    4496       int i = 0;
    4497       for (; i < 3; i++)
    4498         if (TrianglePoints[i] != NULL)
    4499           break;
    4500       for (LineMap::const_iterator line = TrianglePoints[i]->lines.begin(); line != TrianglePoints[i]->lines.end(); line++)
    4501         for (TriangleMap::const_iterator triangle = line->second->triangles.begin(); triangle != line->second->triangles.end(); triangle++)
    4502           result->push_back(triangle->second);
    4503       result->sort();
    4504       result->unique();
    4505       break;
    4506     }
    4507     case 3: // copy all triangles
    4508     {
    4509       for (TriangleMap::const_iterator triangle = TrianglesOnBoundary.begin(); triangle != TrianglesOnBoundary.end(); triangle++)
    4510         result->push_back(triangle->second);
    4511       break;
    4512     }
    4513     default:
    4514       DoeLog(0) && (eLog() << Verbose(0) << "Number of wildcards is greater than 3, cannot happen!" << endl);
    4515       performCriticalExit();
    4516       break;
     3483    }
    45173484  }
    45183485
    45193486  return result;
    45203487}
    4521 
    4522 struct BoundaryLineSetCompare
    4523 {
    4524   bool operator()(const BoundaryLineSet * const a, const BoundaryLineSet * const b)
    4525   {
    4526     int lowerNra = -1;
    4527     int lowerNrb = -1;
    4528 
    4529     if (a->endpoints[0] < a->endpoints[1])
    4530       lowerNra = 0;
    4531     else
    4532       lowerNra = 1;
    4533 
    4534     if (b->endpoints[0] < b->endpoints[1])
    4535       lowerNrb = 0;
    4536     else
    4537       lowerNrb = 1;
    4538 
    4539     if (a->endpoints[lowerNra] < b->endpoints[lowerNrb])
    4540       return true;
    4541     else if (a->endpoints[lowerNra] > b->endpoints[lowerNrb])
    4542       return false;
    4543     else { // both lower-numbered endpoints are the same ...
    4544       if (a->endpoints[(lowerNra + 1) % 2] < b->endpoints[(lowerNrb + 1) % 2])
    4545         return true;
    4546       else if (a->endpoints[(lowerNra + 1) % 2] > b->endpoints[(lowerNrb + 1) % 2])
    4547         return false;
    4548     }
    4549     return false;
    4550   }
    4551   ;
    4552 };
    4553 
    4554 #define UniqueLines set < class BoundaryLineSet *, BoundaryLineSetCompare>
    45553488
    45563489/**
     
    45603493 *         in the list, once as key and once as value
    45613494 */
    4562 IndexToIndex * Tesselation::FindAllDegeneratedLines()
    4563 {
    4564   Info FunctionInfo(__func__);
    4565   UniqueLines AllLines;
    4566   IndexToIndex * DegeneratedLines = new IndexToIndex;
     3495map<int, int> * Tesselation::FindAllDegeneratedLines()
     3496{
     3497        Info FunctionInfo(__func__);
     3498  map<int, class BoundaryLineSet *> AllLines;
     3499  map<int, int> * DegeneratedLines = new map<int, int>;
    45673500
    45683501  // sanity check
    45693502  if (LinesOnBoundary.empty()) {
    4570     DoeLog(2) && (eLog() << Verbose(2) << "FindAllDegeneratedTriangles() was called without any tesselation structure.");
     3503    eLog() << Verbose(2) << "FindAllDegeneratedTriangles() was called without any tesselation structure.";
    45713504    return DegeneratedLines;
    45723505  }
     3506
    45733507  LineMap::iterator LineRunner1;
    4574   pair<UniqueLines::iterator, bool> tester;
     3508  pair<LineMap::iterator, bool> tester;
    45753509  for (LineRunner1 = LinesOnBoundary.begin(); LineRunner1 != LinesOnBoundary.end(); ++LineRunner1) {
    4576     tester = AllLines.insert(LineRunner1->second);
    4577     if (!tester.second) { // found degenerated line
    4578       DegeneratedLines->insert(pair<int, int> (LineRunner1->second->Nr, (*tester.first)->Nr));
    4579       DegeneratedLines->insert(pair<int, int> ((*tester.first)->Nr, LineRunner1->second->Nr));
     3510    tester = AllLines.insert( pair<int,BoundaryLineSet *> (LineRunner1->second->endpoints[0]->Nr, LineRunner1->second) );
     3511    if ((!tester.second) && (tester.first->second->endpoints[1]->Nr == LineRunner1->second->endpoints[1]->Nr)) { // found degenerated line
     3512      DegeneratedLines->insert ( pair<int, int> (LineRunner1->second->Nr, tester.first->second->Nr) );
     3513      DegeneratedLines->insert ( pair<int, int> (tester.first->second->Nr, LineRunner1->second->Nr) );
    45803514    }
    45813515  }
     
    45833517  AllLines.clear();
    45843518
    4585   DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedLines() found " << DegeneratedLines->size() << " lines." << endl);
    4586   IndexToIndex::iterator it;
    4587   for (it = DegeneratedLines->begin(); it != DegeneratedLines->end(); it++) {
    4588     const LineMap::const_iterator Line1 = LinesOnBoundary.find((*it).first);
    4589     const LineMap::const_iterator Line2 = LinesOnBoundary.find((*it).second);
    4590     if (Line1 != LinesOnBoundary.end() && Line2 != LinesOnBoundary.end())
    4591       DoLog(0) && (Log() << Verbose(0) << *Line1->second << " => " << *Line2->second << endl);
    4592     else
    4593       DoeLog(1) && (eLog() << Verbose(1) << "Either " << (*it).first << " or " << (*it).second << " are not in LinesOnBoundary!" << endl);
    4594   }
     3519  Log() << Verbose(0) << "FindAllDegeneratedLines() found " << DegeneratedLines->size() << " lines." << endl;
     3520  map<int,int>::iterator it;
     3521  for (it = DegeneratedLines->begin(); it != DegeneratedLines->end(); it++)
     3522      Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl;
    45953523
    45963524  return DegeneratedLines;
     
    46033531 *         in the list, once as key and once as value
    46043532 */
    4605 IndexToIndex * Tesselation::FindAllDegeneratedTriangles()
    4606 {
    4607   Info FunctionInfo(__func__);
    4608   IndexToIndex * DegeneratedLines = FindAllDegeneratedLines();
    4609   IndexToIndex * DegeneratedTriangles = new IndexToIndex;
     3533map<int, int> * Tesselation::FindAllDegeneratedTriangles()
     3534{
     3535        Info FunctionInfo(__func__);
     3536  map<int, int> * DegeneratedLines = FindAllDegeneratedLines();
     3537  map<int, int> * DegeneratedTriangles = new map<int, int>;
     3538
    46103539  TriangleMap::iterator TriangleRunner1, TriangleRunner2;
    46113540  LineMap::iterator Liner;
    46123541  class BoundaryLineSet *line1 = NULL, *line2 = NULL;
    46133542
    4614   for (IndexToIndex::iterator LineRunner = DegeneratedLines->begin(); LineRunner != DegeneratedLines->end(); ++LineRunner) {
     3543  for (map<int, int>::iterator LineRunner = DegeneratedLines->begin(); LineRunner != DegeneratedLines->end(); ++LineRunner) {
    46153544    // run over both lines' triangles
    46163545    Liner = LinesOnBoundary.find(LineRunner->first);
     
    46223551    for (TriangleRunner1 = line1->triangles.begin(); TriangleRunner1 != line1->triangles.end(); ++TriangleRunner1) {
    46233552      for (TriangleRunner2 = line2->triangles.begin(); TriangleRunner2 != line2->triangles.end(); ++TriangleRunner2) {
    4624         if ((TriangleRunner1->second != TriangleRunner2->second) && (TriangleRunner1->second->IsPresentTupel(TriangleRunner2->second))) {
    4625           DegeneratedTriangles->insert(pair<int, int> (TriangleRunner1->second->Nr, TriangleRunner2->second->Nr));
    4626           DegeneratedTriangles->insert(pair<int, int> (TriangleRunner2->second->Nr, TriangleRunner1->second->Nr));
     3553        if ((TriangleRunner1->second != TriangleRunner2->second)
     3554          && (TriangleRunner1->second->IsPresentTupel(TriangleRunner2->second))) {
     3555          DegeneratedTriangles->insert( pair<int, int> (TriangleRunner1->second->Nr, TriangleRunner2->second->Nr) );
     3556          DegeneratedTriangles->insert( pair<int, int> (TriangleRunner2->second->Nr, TriangleRunner1->second->Nr) );
    46273557        }
    46283558      }
    46293559    }
    46303560  }
    4631   delete (DegeneratedLines);
    4632 
    4633   DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedTriangles() found " << DegeneratedTriangles->size() << " triangles:" << endl);
    4634   IndexToIndex::iterator it;
     3561  delete(DegeneratedLines);
     3562
     3563  Log() << Verbose(0) << "FindAllDegeneratedTriangles() found " << DegeneratedTriangles->size() << " triangles:" << endl;
     3564  map<int,int>::iterator it;
    46353565  for (it = DegeneratedTriangles->begin(); it != DegeneratedTriangles->end(); it++)
    4636     DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl);
     3566      Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl;
    46373567
    46383568  return DegeneratedTriangles;
     
    46453575void Tesselation::RemoveDegeneratedTriangles()
    46463576{
    4647   Info FunctionInfo(__func__);
    4648   IndexToIndex * DegeneratedTriangles = FindAllDegeneratedTriangles();
     3577        Info FunctionInfo(__func__);
     3578  map<int, int> * DegeneratedTriangles = FindAllDegeneratedTriangles();
    46493579  TriangleMap::iterator finder;
    46503580  BoundaryTriangleSet *triangle = NULL, *partnerTriangle = NULL;
    4651   int count = 0;
    4652 
    4653   for (IndexToIndex::iterator TriangleKeyRunner = DegeneratedTriangles->begin(); TriangleKeyRunner != DegeneratedTriangles->end(); ++TriangleKeyRunner) {
     3581  int count  = 0;
     3582
     3583  for (map<int, int>::iterator TriangleKeyRunner = DegeneratedTriangles->begin();
     3584    TriangleKeyRunner != DegeneratedTriangles->end(); ++TriangleKeyRunner
     3585  ) {
    46543586    finder = TrianglesOnBoundary.find(TriangleKeyRunner->first);
    46553587    if (finder != TrianglesOnBoundary.end())
     
    46683600        trianglesShareLine = trianglesShareLine || triangle->lines[i] == partnerTriangle->lines[j];
    46693601
    4670     if (trianglesShareLine && (triangle->endpoints[1]->LinesCount > 2) && (triangle->endpoints[2]->LinesCount > 2) && (triangle->endpoints[0]->LinesCount > 2)) {
     3602    if (trianglesShareLine
     3603      && (triangle->endpoints[1]->LinesCount > 2)
     3604      && (triangle->endpoints[2]->LinesCount > 2)
     3605      && (triangle->endpoints[0]->LinesCount > 2)
     3606    ) {
    46713607      // check whether we have to fix lines
    46723608      BoundaryTriangleSet *Othertriangle = NULL;
     
    46883624            // the line of triangle receives the degenerated ones
    46893625            triangle->lines[i]->triangles.erase(Othertriangle->Nr);
    4690             triangle->lines[i]->triangles.insert(TrianglePair(partnerTriangle->Nr, partnerTriangle));
    4691             for (int k = 0; k < 3; k++)
     3626            triangle->lines[i]->triangles.insert( TrianglePair( partnerTriangle->Nr, partnerTriangle) );
     3627            for (int k=0;k<3;k++)
    46923628              if (triangle->lines[i] == Othertriangle->lines[k]) {
    46933629                Othertriangle->lines[k] = partnerTriangle->lines[j];
     
    46953631              }
    46963632            // the line of partnerTriangle receives the non-degenerated ones
    4697             partnerTriangle->lines[j]->triangles.erase(partnerTriangle->Nr);
    4698             partnerTriangle->lines[j]->triangles.insert(TrianglePair(Othertriangle->Nr, Othertriangle));
     3633            partnerTriangle->lines[j]->triangles.erase( partnerTriangle->Nr);
     3634            partnerTriangle->lines[j]->triangles.insert( TrianglePair( Othertriangle->Nr, Othertriangle) );
    46993635            partnerTriangle->lines[j] = triangle->lines[i];
    47003636          }
     
    47023638      // erase the pair
    47033639      count += (int) DegeneratedTriangles->erase(triangle->Nr);
    4704       DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *triangle << "." << endl);
     3640      Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *triangle << "." << endl;
    47053641      RemoveTesselationTriangle(triangle);
    47063642      count += (int) DegeneratedTriangles->erase(partnerTriangle->Nr);
    4707       DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *partnerTriangle << "." << endl);
     3643      Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *partnerTriangle << "." << endl;
    47083644      RemoveTesselationTriangle(partnerTriangle);
    47093645    } else {
    4710       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);
    4711     }
    4712   }
    4713   delete (DegeneratedTriangles);
     3646      Log() << Verbose(0) << "RemoveDegeneratedTriangles() does not remove triangle " << *triangle
     3647        << " and its partner " << *partnerTriangle << " because it is essential for at"
     3648        << " least one of the endpoints to be kept in the tesselation structure." << endl;
     3649    }
     3650  }
     3651  delete(DegeneratedTriangles);
    47143652  if (count > 0)
    47153653    LastTriangle = NULL;
    47163654
    4717   DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removed " << count << " triangles:" << endl);
     3655  Log() << Verbose(0) << "RemoveDegeneratedTriangles() removed " << count << " triangles:" << endl;
    47183656}
    47193657
     
    47283666void Tesselation::AddBoundaryPointByDegeneratedTriangle(class TesselPoint *point, LinkedCell *LC)
    47293667{
    4730   Info FunctionInfo(__func__);
     3668        Info FunctionInfo(__func__);
    47313669  // find nearest boundary point
    47323670  class TesselPoint *BackupPoint = NULL;
    4733   class TesselPoint *NearestPoint = FindClosestTesselPoint(point->node, BackupPoint, LC);
     3671  class TesselPoint *NearestPoint = FindClosestPoint(point->node, BackupPoint, LC);
    47343672  class BoundaryPointSet *NearestBoundaryPoint = NULL;
    47353673  PointMap::iterator PointRunner;
     
    47413679    NearestBoundaryPoint = PointRunner->second;
    47423680  } else {
    4743     DoeLog(1) && (eLog() << Verbose(1) << "I cannot find the boundary point." << endl);
     3681    eLog() << Verbose(1) << "I cannot find the boundary point." << endl;
    47443682    return;
    47453683  }
    4746   DoLog(0) && (Log() << Verbose(0) << "Nearest point on boundary is " << NearestPoint->Name << "." << endl);
     3684  Log() << Verbose(0) << "Nearest point on boundary is " << NearestPoint->Name << "." << endl;
    47473685
    47483686  // go through its lines and find the best one to split
     
    47593697    CenterToPoint.SubtractVector(point->node);
    47603698    angle = CenterToPoint.Angle(&BaseLine);
    4761     if (fabs(angle - M_PI / 2.) < fabs(BestAngle - M_PI / 2.)) {
     3699    if (fabs(angle - M_PI/2.) < fabs(BestAngle - M_PI/2.)) {
    47623700      BestAngle = angle;
    47633701      BestLine = Runner->second;
     
    47693707  BestLine->triangles.erase(TempTriangle->Nr);
    47703708  int nr = -1;
    4771   for (int i = 0; i < 3; i++) {
     3709  for (int i=0;i<3; i++) {
    47723710    if (TempTriangle->lines[i] == BestLine) {
    47733711      nr = i;
     
    47773715
    47783716  // create new triangle to connect point (connects automatically with the missing spot of the chosen line)
    4779   DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl);
     3717  Log() << Verbose(2) << "Adding new triangle points."<< endl;
    47803718  AddTesselationPoint((BestLine->endpoints[0]->node), 0);
    47813719  AddTesselationPoint((BestLine->endpoints[1]->node), 1);
    47823720  AddTesselationPoint(point, 2);
    4783   DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl);
    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);
     3721  Log() << Verbose(2) << "Adding new triangle lines."<< endl;
     3722  AddTesselationLine(TPS[0], TPS[1], 0);
     3723  AddTesselationLine(TPS[0], TPS[2], 1);
     3724  AddTesselationLine(TPS[1], TPS[2], 2);
    47873725  BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    47883726  BTS->GetNormalVector(TempTriangle->NormalVector);
    47893727  BTS->NormalVector.Scale(-1.);
    4790   DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of new triangle is " << BTS->NormalVector << "." << endl);
     3728  Log() << Verbose(1) << "INFO: NormalVector of new triangle is " << BTS->NormalVector << "." << endl;
    47913729  AddTesselationTriangle();
    47923730
    47933731  // create other side of this triangle and close both new sides of the first created triangle
    4794   DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl);
     3732  Log() << Verbose(2) << "Adding new triangle points."<< endl;
    47953733  AddTesselationPoint((BestLine->endpoints[0]->node), 0);
    47963734  AddTesselationPoint((BestLine->endpoints[1]->node), 1);
    47973735  AddTesselationPoint(point, 2);
    4798   DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl);
    4799   AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
    4800   AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
    4801   AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
     3736  Log() << Verbose(2) << "Adding new triangle lines."<< endl;
     3737  AddTesselationLine(TPS[0], TPS[1], 0);
     3738  AddTesselationLine(TPS[0], TPS[2], 1);
     3739  AddTesselationLine(TPS[1], TPS[2], 2);
    48023740  BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    48033741  BTS->GetNormalVector(TempTriangle->NormalVector);
    4804   DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of other new triangle is " << BTS->NormalVector << "." << endl);
     3742  Log() << Verbose(1) << "INFO: NormalVector of other new triangle is " << BTS->NormalVector << "." << endl;
    48053743  AddTesselationTriangle();
    48063744
    48073745  // add removed triangle to the last open line of the second triangle
    4808   for (int i = 0; i < 3; i++) { // look for the same line as BestLine (only it's its degenerated companion)
     3746  for (int i=0;i<3;i++) { // look for the same line as BestLine (only it's its degenerated companion)
    48093747    if ((BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[0])) && (BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[1]))) {
    4810       if (BestLine == BTS->lines[i]) {
    4811         DoeLog(0) && (eLog() << Verbose(0) << "BestLine is same as found line, something's wrong here!" << endl);
     3748      if (BestLine == BTS->lines[i]){
     3749        eLog() << Verbose(0) << "BestLine is same as found line, something's wrong here!" << endl;
    48123750        performCriticalExit();
    48133751      }
    4814       BTS->lines[i]->triangles.insert(pair<int, class BoundaryTriangleSet *> (TempTriangle->Nr, TempTriangle));
     3752      BTS->lines[i]->triangles.insert( pair<int, class BoundaryTriangleSet *> (TempTriangle->Nr, TempTriangle) );
    48153753      TempTriangle->lines[nr] = BTS->lines[i];
    48163754      break;
    48173755    }
    48183756  }
    4819 }
    4820 ;
     3757};
    48213758
    48223759/** Writes the envelope to file.
     
    48273764void Tesselation::Output(const char *filename, const PointCloud * const cloud)
    48283765{
    4829   Info FunctionInfo(__func__);
     3766        Info FunctionInfo(__func__);
    48303767  ofstream *tempstream = NULL;
    48313768  string NameofTempFile;
     
    48333770
    48343771  if (LastTriangle != NULL) {
    4835     sprintf(NumberName, "-%04d-%s_%s_%s", (int) TrianglesOnBoundary.size(), LastTriangle->endpoints[0]->node->Name, LastTriangle->endpoints[1]->node->Name, LastTriangle->endpoints[2]->node->Name);
     3772    sprintf(NumberName, "-%04d-%s_%s_%s", (int)TrianglesOnBoundary.size(), LastTriangle->endpoints[0]->node->Name, LastTriangle->endpoints[1]->node->Name, LastTriangle->endpoints[2]->node->Name);
    48363773    if (DoTecplotOutput) {
    48373774      string NameofTempFile(filename);
    48383775      NameofTempFile.append(NumberName);
    4839       for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
    4840         NameofTempFile.erase(npos, 1);
     3776      for(size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
     3777      NameofTempFile.erase(npos, 1);
    48413778      NameofTempFile.append(TecplotSuffix);
    4842       DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n");
     3779      Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n";
    48433780      tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
    48443781      WriteTecplotFile(tempstream, this, cloud, TriangleFilesWritten);
    48453782      tempstream->close();
    48463783      tempstream->flush();
    4847       delete (tempstream);
     3784      delete(tempstream);
    48483785    }
    48493786
     
    48513788      string NameofTempFile(filename);
    48523789      NameofTempFile.append(NumberName);
    4853       for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
    4854         NameofTempFile.erase(npos, 1);
     3790      for(size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
     3791      NameofTempFile.erase(npos, 1);
    48553792      NameofTempFile.append(Raster3DSuffix);
    4856       DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n");
     3793      Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n";
    48573794      tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
    48583795      WriteRaster3dFile(tempstream, this, cloud);
     
    48603797      tempstream->close();
    48613798      tempstream->flush();
    4862       delete (tempstream);
     3799      delete(tempstream);
    48633800    }
    48643801  }
    48653802  if (DoTecplotOutput || DoRaster3DOutput)
    48663803    TriangleFilesWritten++;
    4867 }
    4868 ;
    4869 
    4870 struct BoundaryPolygonSetCompare
    4871 {
    4872   bool operator()(const BoundaryPolygonSet * s1, const BoundaryPolygonSet * s2) const
    4873   {
    4874     if (s1->endpoints.size() < s2->endpoints.size())
    4875       return true;
    4876     else if (s1->endpoints.size() > s2->endpoints.size())
    4877       return false;
    4878     else { // equality of number of endpoints
    4879       PointSet::const_iterator Walker1 = s1->endpoints.begin();
    4880       PointSet::const_iterator Walker2 = s2->endpoints.begin();
    4881       while ((Walker1 != s1->endpoints.end()) || (Walker2 != s2->endpoints.end())) {
    4882         if ((*Walker1)->Nr < (*Walker2)->Nr)
    4883           return true;
    4884         else if ((*Walker1)->Nr > (*Walker2)->Nr)
    4885           return false;
    4886         Walker1++;
    4887         Walker2++;
    4888       }
    4889       return false;
    4890     }
    4891   }
    4892 };
    4893 
    4894 #define UniquePolygonSet set < BoundaryPolygonSet *, BoundaryPolygonSetCompare>
    4895 
    4896 /** Finds all degenerated polygons and calls ReTesselateDegeneratedPolygon()/
    4897  * \return number of polygons found
    4898  */
    4899 int Tesselation::CorrectAllDegeneratedPolygons()
    4900 {
    4901   Info FunctionInfo(__func__);
    4902   /// 2. Go through all BoundaryPointSet's, check their triangles' NormalVector
    4903   IndexToIndex *DegeneratedTriangles = FindAllDegeneratedTriangles();
    4904   set<BoundaryPointSet *> EndpointCandidateList;
    4905   pair<set<BoundaryPointSet *>::iterator, bool> InsertionTester;
    4906   pair<map<int, Vector *>::iterator, bool> TriangleInsertionTester;
    4907   for (PointMap::const_iterator Runner = PointsOnBoundary.begin(); Runner != PointsOnBoundary.end(); Runner++) {
    4908     DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Runner->second << "." << endl);
    4909     map<int, Vector *> TriangleVectors;
    4910     // gather all NormalVectors
    4911     DoLog(1) && (Log() << Verbose(1) << "Gathering triangles ..." << endl);
    4912     for (LineMap::const_iterator LineRunner = (Runner->second)->lines.begin(); LineRunner != (Runner->second)->lines.end(); LineRunner++)
    4913       for (TriangleMap::const_iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
    4914         if (DegeneratedTriangles->find(TriangleRunner->second->Nr) == DegeneratedTriangles->end()) {
    4915           TriangleInsertionTester = TriangleVectors.insert(pair<int, Vector *> ((TriangleRunner->second)->Nr, &((TriangleRunner->second)->NormalVector)));
    4916           if (TriangleInsertionTester.second)
    4917             DoLog(1) && (Log() << Verbose(1) << " Adding triangle " << *(TriangleRunner->second) << " to triangles to check-list." << endl);
    4918         } else {
    4919           DoLog(1) && (Log() << Verbose(1) << " NOT adding triangle " << *(TriangleRunner->second) << " as it's a simply degenerated one." << endl);
    4920         }
    4921       }
    4922     // check whether there are two that are parallel
    4923     DoLog(1) && (Log() << Verbose(1) << "Finding two parallel triangles ..." << endl);
    4924     for (map<int, Vector *>::iterator VectorWalker = TriangleVectors.begin(); VectorWalker != TriangleVectors.end(); VectorWalker++)
    4925       for (map<int, Vector *>::iterator VectorRunner = VectorWalker; VectorRunner != TriangleVectors.end(); VectorRunner++)
    4926         if (VectorWalker != VectorRunner) { // skip equals
    4927           const double SCP = VectorWalker->second->ScalarProduct(VectorRunner->second); // ScalarProduct should result in -1. for degenerated triangles
    4928           DoLog(1) && (Log() << Verbose(1) << "Checking " << *VectorWalker->second << " against " << *VectorRunner->second << ": " << SCP << endl);
    4929           if (fabs(SCP + 1.) < ParallelEpsilon) {
    4930             InsertionTester = EndpointCandidateList.insert((Runner->second));
    4931             if (InsertionTester.second)
    4932               DoLog(0) && (Log() << Verbose(0) << " Adding " << *Runner->second << " to endpoint candidate list." << endl);
    4933             // and break out of both loops
    4934             VectorWalker = TriangleVectors.end();
    4935             VectorRunner = TriangleVectors.end();
    4936             break;
    4937           }
    4938         }
    4939   }
    4940   delete (DegeneratedTriangles);
    4941   /// 3. Find connected endpoint candidates and put them into a polygon
    4942   UniquePolygonSet ListofDegeneratedPolygons;
    4943   BoundaryPointSet *Walker = NULL;
    4944   BoundaryPointSet *OtherWalker = NULL;
    4945   BoundaryPolygonSet *Current = NULL;
    4946   stack<BoundaryPointSet*> ToCheckConnecteds;
    4947   while (!EndpointCandidateList.empty()) {
    4948     Walker = *(EndpointCandidateList.begin());
    4949     if (Current == NULL) { // create a new polygon with current candidate
    4950       DoLog(0) && (Log() << Verbose(0) << "Starting new polygon set at point " << *Walker << endl);
    4951       Current = new BoundaryPolygonSet;
    4952       Current->endpoints.insert(Walker);
    4953       EndpointCandidateList.erase(Walker);
    4954       ToCheckConnecteds.push(Walker);
    4955     }
    4956 
    4957     // go through to-check stack
    4958     while (!ToCheckConnecteds.empty()) {
    4959       Walker = ToCheckConnecteds.top(); // fetch ...
    4960       ToCheckConnecteds.pop(); // ... and remove
    4961       for (LineMap::const_iterator LineWalker = Walker->lines.begin(); LineWalker != Walker->lines.end(); LineWalker++) {
    4962         OtherWalker = (LineWalker->second)->GetOtherEndpoint(Walker);
    4963         DoLog(1) && (Log() << Verbose(1) << "Checking " << *OtherWalker << endl);
    4964         set<BoundaryPointSet *>::iterator Finder = EndpointCandidateList.find(OtherWalker);
    4965         if (Finder != EndpointCandidateList.end()) { // found a connected partner
    4966           DoLog(1) && (Log() << Verbose(1) << " Adding to polygon." << endl);
    4967           Current->endpoints.insert(OtherWalker);
    4968           EndpointCandidateList.erase(Finder); // remove from candidates
    4969           ToCheckConnecteds.push(OtherWalker); // but check its partners too
    4970         } else {
    4971           DoLog(1) && (Log() << Verbose(1) << " is not connected to " << *Walker << endl);
    4972         }
    4973       }
    4974     }
    4975 
    4976     DoLog(0) && (Log() << Verbose(0) << "Final polygon is " << *Current << endl);
    4977     ListofDegeneratedPolygons.insert(Current);
    4978     Current = NULL;
    4979   }
    4980 
    4981   const int counter = ListofDegeneratedPolygons.size();
    4982 
    4983   DoLog(0) && (Log() << Verbose(0) << "The following " << counter << " degenerated polygons have been found: " << endl);
    4984   for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++)
    4985     DoLog(0) && (Log() << Verbose(0) << " " << **PolygonRunner << endl);
    4986 
    4987   /// 4. Go through all these degenerated polygons
    4988   for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++) {
    4989     stack<int> TriangleNrs;
    4990     Vector NormalVector;
    4991     /// 4a. Gather all triangles of this polygon
    4992     TriangleSet *T = (*PolygonRunner)->GetAllContainedTrianglesFromEndpoints();
    4993 
    4994     // check whether number is bigger than 2, otherwise it's just a simply degenerated one and nothing to do.
    4995     if (T->size() == 2) {
    4996       DoLog(1) && (Log() << Verbose(1) << " Skipping degenerated polygon, is just a (already simply degenerated) triangle." << endl);
    4997       delete (T);
    4998       continue;
    4999     }
    5000 
    5001     // check whether number is even
    5002     // If this case occurs, we have to think about it!
    5003     // The Problem is probably due to two degenerated polygons being connected by a bridging, non-degenerated polygon, as somehow one node has
    5004     // connections to either polygon ...
    5005     if (T->size() % 2 != 0) {
    5006       DoeLog(0) && (eLog() << Verbose(0) << " degenerated polygon contains an odd number of triangles, probably contains bridging non-degenerated ones, too!" << endl);
    5007       performCriticalExit();
    5008     }
    5009     TriangleSet::iterator TriangleWalker = T->begin(); // is the inner iterator
    5010     /// 4a. Get NormalVector for one side (this is "front")
    5011     NormalVector.CopyVector(&(*TriangleWalker)->NormalVector);
    5012     DoLog(1) && (Log() << Verbose(1) << "\"front\" defining triangle is " << **TriangleWalker << " and Normal vector of \"front\" side is " << NormalVector << endl);
    5013     TriangleWalker++;
    5014     TriangleSet::iterator TriangleSprinter = TriangleWalker; // is the inner advanced iterator
    5015     /// 4b. Remove all triangles whose NormalVector is in opposite direction (i.e. "back")
    5016     BoundaryTriangleSet *triangle = NULL;
    5017     while (TriangleSprinter != T->end()) {
    5018       TriangleWalker = TriangleSprinter;
    5019       triangle = *TriangleWalker;
    5020       TriangleSprinter++;
    5021       DoLog(1) && (Log() << Verbose(1) << "Current triangle to test for removal: " << *triangle << endl);
    5022       if (triangle->NormalVector.ScalarProduct(&NormalVector) < 0) { // if from other side, then delete and remove from list
    5023         DoLog(1) && (Log() << Verbose(1) << " Removing ... " << endl);
    5024         TriangleNrs.push(triangle->Nr);
    5025         T->erase(TriangleWalker);
    5026         RemoveTesselationTriangle(triangle);
    5027       } else
    5028         DoLog(1) && (Log() << Verbose(1) << " Keeping ... " << endl);
    5029     }
    5030     /// 4c. Copy all "front" triangles but with inverse NormalVector
    5031     TriangleWalker = T->begin();
    5032     while (TriangleWalker != T->end()) { // go through all front triangles
    5033       DoLog(1) && (Log() << Verbose(1) << " Re-creating triangle " << **TriangleWalker << " with NormalVector " << (*TriangleWalker)->NormalVector << endl);
    5034       for (int i = 0; i < 3; i++)
    5035         AddTesselationPoint((*TriangleWalker)->endpoints[i]->node, i);
    5036       AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
    5037       AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
    5038       AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
    5039       if (TriangleNrs.empty())
    5040         DoeLog(0) && (eLog() << Verbose(0) << "No more free triangle numbers!" << endl);
    5041       BTS = new BoundaryTriangleSet(BLS, TriangleNrs.top()); // copy triangle ...
    5042       AddTesselationTriangle(); // ... and add
    5043       TriangleNrs.pop();
    5044       BTS->NormalVector.CopyVector(&(*TriangleWalker)->NormalVector);
    5045       BTS->NormalVector.Scale(-1.);
    5046       TriangleWalker++;
    5047     }
    5048     if (!TriangleNrs.empty()) {
    5049       DoeLog(0) && (eLog() << Verbose(0) << "There have been less triangles created than removed!" << endl);
    5050     }
    5051     delete (T); // remove the triangleset
    5052   }
    5053   IndexToIndex * SimplyDegeneratedTriangles = FindAllDegeneratedTriangles();
    5054   DoLog(0) && (Log() << Verbose(0) << "Final list of simply degenerated triangles found, containing " << SimplyDegeneratedTriangles->size() << " triangles:" << endl);
    5055   IndexToIndex::iterator it;
    5056   for (it = SimplyDegeneratedTriangles->begin(); it != SimplyDegeneratedTriangles->end(); it++)
    5057     DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl);
    5058   delete (SimplyDegeneratedTriangles);
    5059   /// 5. exit
    5060   UniquePolygonSet::iterator PolygonRunner;
    5061   while (!ListofDegeneratedPolygons.empty()) {
    5062     PolygonRunner = ListofDegeneratedPolygons.begin();
    5063     delete (*PolygonRunner);
    5064     ListofDegeneratedPolygons.erase(PolygonRunner);
    5065   }
    5066 
    5067   return counter;
    5068 }
    5069 ;
     3804};
Note: See TracChangeset for help on using the changeset viewer.