Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tesselation.cpp

    rb8d4a3 r68f03d  
    77
    88#include <fstream>
     9#include <assert.h>
    910
    1011#include "helpers.hpp"
     
    1415#include "tesselation.hpp"
    1516#include "tesselationhelpers.hpp"
     17#include "triangleintersectionlist.hpp"
    1618#include "vector.hpp"
     19#include "vector_ops.hpp"
    1720#include "verbose.hpp"
     21#include "Plane.hpp"
     22#include "Exceptions/LinearDependenceException.hpp"
    1823
    1924class molecule;
     
    2429 */
    2530BoundaryPointSet::BoundaryPointSet() :
    26     LinesCount(0),
    27     value(0.),
    28     Nr(-1)
    29 {
    30         Info FunctionInfo(__func__);
    31         Log() << Verbose(1) << "Adding noname." << endl;
    32 };
     31  LinesCount(0), value(0.), Nr(-1)
     32{
     33  Info FunctionInfo(__func__);
     34  DoLog(1) && (Log() << Verbose(1) << "Adding noname." << endl);
     35}
     36;
    3337
    3438/** Constructor of BoundaryPointSet with Tesselpoint.
     
    3640 */
    3741BoundaryPointSet::BoundaryPointSet(TesselPoint * const 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 };
     42  LinesCount(0), node(Walker), value(0.), Nr(Walker->nr)
     43{
     44  Info FunctionInfo(__func__);
     45  DoLog(1) && (Log() << Verbose(1) << "Adding Node " << *Walker << endl);
     46}
     47;
    4648
    4749/** Destructor of BoundaryPointSet.
     
    5153BoundaryPointSet::~BoundaryPointSet()
    5254{
    53         Info FunctionInfo(__func__);
     55  Info FunctionInfo(__func__);
    5456  //Log() << Verbose(0) << "Erasing point nr. " << Nr << "." << endl;
    5557  if (!lines.empty())
    56     eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some lines." << endl;
     58    DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some lines." << endl);
    5759  node = NULL;
    58 };
     60}
     61;
    5962
    6063/** Add a line to the LineMap of this point.
     
    6366void BoundaryPointSet::AddLine(BoundaryLineSet * const line)
    6467{
    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     }
     68  Info FunctionInfo(__func__);
     69  DoLog(1) && (Log() << Verbose(1) << "Adding " << *this << " to line " << *line << "." << endl);
     70  if (line->endpoints[0] == this) {
     71    lines.insert(LinePair(line->endpoints[1]->Nr, line));
     72  } else {
     73    lines.insert(LinePair(line->endpoints[0]->Nr, line));
     74  }
    7675  LinesCount++;
    77 };
     76}
     77;
    7878
    7979/** output operator for BoundaryPointSet.
     
    8383ostream & operator <<(ostream &ost, const BoundaryPointSet &a)
    8484{
    85   ost << "[" << a.Nr << "|" << a.node->Name << " at " << *a.node->node << "]";
     85  ost << "[" << a.Nr << "|" << a.node->getName() << " at " << *a.node->node << "]";
    8686  return ost;
    8787}
     
    9393 */
    9494BoundaryLineSet::BoundaryLineSet() :
    95     Nr(-1)
    96 {
    97         Info FunctionInfo(__func__);
     95  Nr(-1)
     96{
     97  Info FunctionInfo(__func__);
    9898  for (int i = 0; i < 2; i++)
    9999    endpoints[i] = NULL;
    100 };
     100}
     101;
    101102
    102103/** Constructor of BoundaryLineSet with two endpoints.
     
    107108BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point[2], const int number)
    108109{
    109         Info FunctionInfo(__func__);
     110  Info FunctionInfo(__func__);
    110111  // set number
    111112  Nr = number;
     
    118119  skipped = false;
    119120  // clear triangles list
    120   Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl;
    121 };
     121  DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
     122}
     123;
    122124
    123125/** Constructor of BoundaryLineSet with two endpoints.
     
    140142  skipped = false;
    141143  // clear triangles list
    142   Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl;
    143 };
     144  DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
     145}
     146;
    144147
    145148/** Destructor for BoundaryLineSet.
     
    149152BoundaryLineSet::~BoundaryLineSet()
    150153{
    151         Info FunctionInfo(__func__);
     154  Info FunctionInfo(__func__);
    152155  int Numbers[2];
    153156
     
    180183        //Log() << Verbose(0) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl;
    181184        if (endpoints[i] != NULL) {
    182           delete(endpoints[i]);
     185          delete (endpoints[i]);
    183186          endpoints[i] = NULL;
    184187        }
     
    187190  }
    188191  if (!triangles.empty())
    189     eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl;
    190 };
     192    DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl);
     193}
     194;
    191195
    192196/** Add triangle to TriangleMap of this boundary line.
     
    195199void BoundaryLineSet::AddTriangle(BoundaryTriangleSet * const triangle)
    196200{
    197         Info FunctionInfo(__func__);
    198   Log() << Verbose(0) << "Add " << triangle->Nr << " to line " << *this << "." << endl;
     201  Info FunctionInfo(__func__);
     202  DoLog(0) && (Log() << Verbose(0) << "Add " << triangle->Nr << " to line " << *this << "." << endl);
    199203  triangles.insert(TrianglePair(triangle->Nr, triangle));
    200 };
     204}
     205;
    201206
    202207/** Checks whether we have a common endpoint with given \a *line.
     
    206211bool BoundaryLineSet::IsConnectedTo(const BoundaryLineSet * const line) const
    207212{
    208         Info FunctionInfo(__func__);
     213  Info FunctionInfo(__func__);
    209214  if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1]))
    210215    return true;
    211216  else
    212217    return false;
    213 };
     218}
     219;
    214220
    215221/** Checks whether the adjacent triangles of a baseline are convex or not.
     
    221227bool BoundaryLineSet::CheckConvexityCriterion() const
    222228{
    223         Info FunctionInfo(__func__);
     229  Info FunctionInfo(__func__);
    224230  Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck;
    225231  // get the two triangles
    226232  if (triangles.size() != 2) {
    227     eLog() << Verbose(0) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl;
     233    DoeLog(0) && (eLog() << Verbose(0) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl);
    228234    return true;
    229235  }
     
    231237  // have a normal vector on the base line pointing outwards
    232238  //Log() << Verbose(0) << "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << "." << endl;
    233   BaseLineCenter.CopyVector(endpoints[0]->node->node);
    234   BaseLineCenter.AddVector(endpoints[1]->node->node);
    235   BaseLineCenter.Scale(1./2.);
    236   BaseLine.CopyVector(endpoints[0]->node->node);
    237   BaseLine.SubtractVector(endpoints[1]->node->node);
     239  BaseLineCenter = (1./2.)*((*endpoints[0]->node->node) + (*endpoints[1]->node->node));
     240  BaseLine = (*endpoints[0]->node->node) - (*endpoints[1]->node->node);
     241
    238242  //Log() << Verbose(0) << "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << "." << endl;
    239243
     
    241245  NormalCheck.Zero();
    242246  double sign = -1.;
    243   int i=0;
     247  int i = 0;
    244248  class BoundaryPointSet *node = NULL;
    245   for(TriangleMap::const_iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
     249  for (TriangleMap::const_iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
    246250    //Log() << Verbose(0) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl;
    247     NormalCheck.AddVector(&runner->second->NormalVector);
    248     NormalCheck.Scale(sign);
     251    NormalCheck += runner->second->NormalVector;
     252    NormalCheck *= sign;
    249253    sign = -sign;
    250254    if (runner->second->NormalVector.NormSquared() > MYEPSILON)
    251       BaseLineNormal.CopyVector(&runner->second->NormalVector);   // yes, copy second on top of first
     255      BaseLineNormal = runner->second->NormalVector;   // yes, copy second on top of first
    252256    else {
    253       eLog() << Verbose(0) << "Triangle " << *runner->second << " has zero normal vector!" << endl;
     257      DoeLog(0) && (eLog() << Verbose(0) << "Triangle " << *runner->second << " has zero normal vector!" << endl);
    254258    }
    255259    node = runner->second->GetThirdEndpoint(this);
    256260    if (node != NULL) {
    257261      //Log() << Verbose(0) << "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << "." << endl;
    258       helper[i].CopyVector(node->node->node);
    259       helper[i].SubtractVector(&BaseLineCenter);
    260       helper[i].MakeNormalVector(&BaseLine);  // we want to compare the triangle's heights' angles!
     262      helper[i] = (*node->node->node) - BaseLineCenter;
     263      helper[i].MakeNormalTo(BaseLine);  // we want to compare the triangle's heights' angles!
    261264      //Log() << Verbose(0) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl;
    262265      i++;
    263266    } else {
    264       eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl;
     267      DoeLog(1) && (eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl);
    265268      return true;
    266269    }
     
    268271  //Log() << Verbose(0) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl;
    269272  if (NormalCheck.NormSquared() < MYEPSILON) {
    270     Log() << Verbose(0) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl;
     273    DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl);
    271274    return true;
    272275  }
     
    274277  double angle = GetAngle(helper[0], helper[1], BaseLineNormal);
    275278  if ((angle - M_PI) > -MYEPSILON) {
    276     Log() << Verbose(0) << "ACCEPT: Angle is greater than pi: convex." << endl;
     279    DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Angle is greater than pi: convex." << endl);
    277280    return true;
    278281  } else {
    279     Log() << Verbose(0) << "REJECT: Angle is less than pi: concave." << endl;
     282    DoLog(0) && (Log() << Verbose(0) << "REJECT: Angle is less than pi: concave." << endl);
    280283    return false;
    281284  }
     
    288291bool BoundaryLineSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
    289292{
    290         Info FunctionInfo(__func__);
    291   for(int i=0;i<2;i++)
     293  Info FunctionInfo(__func__);
     294  for (int i = 0; i < 2; i++)
    292295    if (point == endpoints[i])
    293296      return true;
    294297  return false;
    295 };
     298}
     299;
    296300
    297301/** Returns other endpoint of the line.
     
    301305class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(const BoundaryPointSet * const point) const
    302306{
    303         Info FunctionInfo(__func__);
     307  Info FunctionInfo(__func__);
    304308  if (endpoints[0] == point)
    305309    return endpoints[1];
     
    308312  else
    309313    return NULL;
    310 };
     314}
     315;
    311316
    312317/** output operator for BoundaryLineSet.
     
    314319 * \param &a boundary line
    315320 */
    316 ostream & operator <<(ostream &ost, const  BoundaryLineSet &a)
    317 {
    318   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 << "]";
     321ostream & operator <<(ostream &ost, const BoundaryLineSet &a)
     322{
     323  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->getName() << " at " << *a.endpoints[0]->node->node << "," << a.endpoints[1]->node->getName() << " at " << *a.endpoints[1]->node->node << "]";
    319324  return ost;
    320 };
     325}
     326;
    321327
    322328// ======================================== Triangles on Boundary =================================
     
    327333  Nr(-1)
    328334{
    329         Info FunctionInfo(__func__);
    330   for (int i = 0; i < 3; i++)
    331     {
    332       endpoints[i] = NULL;
    333       lines[i] = NULL;
    334     }
    335 };
     335  Info FunctionInfo(__func__);
     336  for (int i = 0; i < 3; i++) {
     337    endpoints[i] = NULL;
     338    lines[i] = NULL;
     339  }
     340}
     341;
    336342
    337343/** Constructor for BoundaryTriangleSet with three lines.
     
    342348  Nr(number)
    343349{
    344         Info FunctionInfo(__func__);
     350  Info FunctionInfo(__func__);
    345351  // set number
    346352  // set lines
     
    354360    // for all three lines
    355361    for (int j = 0; j < 2; j++) { // for both endpoints
    356       OrderMap.insert(pair<int, class BoundaryPointSet *> (
    357           line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
     362      OrderMap.insert(pair<int, class BoundaryPointSet *> (line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
    358363      // and we don't care whether insertion fails
    359364    }
    360365  // set endpoints
    361366  int Counter = 0;
    362   Log() << Verbose(0) << "New triangle " << Nr << " with end points: " << endl;
     367  DoLog(0) && (Log() << Verbose(0) << "New triangle " << Nr << " with end points: " << endl);
    363368  for (PointMap::iterator runner = OrderMap.begin(); runner != OrderMap.end(); runner++) {
    364369    endpoints[Counter] = runner->second;
    365     Log() << Verbose(0) << " " << *endpoints[Counter] << endl;
     370    DoLog(0) && (Log() << Verbose(0) << " " << *endpoints[Counter] << endl);
    366371    Counter++;
    367372  }
    368373  if (Counter < 3) {
    369     eLog() << Verbose(0) << "We have a triangle with only two distinct endpoints!" << endl;
     374    DoeLog(0) && (eLog() << Verbose(0) << "We have a triangle with only two distinct endpoints!" << endl);
    370375    performCriticalExit();
    371376  }
    372 };
     377}
     378;
    373379
    374380/** Destructor of BoundaryTriangleSet.
     
    378384BoundaryTriangleSet::~BoundaryTriangleSet()
    379385{
    380         Info FunctionInfo(__func__);
     386  Info FunctionInfo(__func__);
    381387  for (int i = 0; i < 3; i++) {
    382388    if (lines[i] != NULL) {
     
    385391      }
    386392      if (lines[i]->triangles.empty()) {
    387           //Log() << Verbose(0) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
    388           delete (lines[i]);
    389           lines[i] = NULL;
     393        //Log() << Verbose(0) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
     394        delete (lines[i]);
     395        lines[i] = NULL;
    390396      }
    391397    }
    392398  }
    393399  //Log() << Verbose(0) << "Erasing triangle Nr." << Nr << " itself." << endl;
    394 };
     400}
     401;
    395402
    396403/** Calculates the normal vector for this triangle.
     
    400407void BoundaryTriangleSet::GetNormalVector(const Vector &OtherVector)
    401408{
    402         Info FunctionInfo(__func__);
     409  Info FunctionInfo(__func__);
    403410  // get normal vector
    404   NormalVector.MakeNormalVector(endpoints[0]->node->node, endpoints[1]->node->node, endpoints[2]->node->node);
     411  NormalVector = Plane(*(endpoints[0]->node->node),
     412                       *(endpoints[1]->node->node),
     413                       *(endpoints[2]->node->node)).getNormal();
    405414
    406415  // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
    407   if (NormalVector.ScalarProduct(&OtherVector) > 0.)
     416  if (NormalVector.ScalarProduct(OtherVector) > 0.)
    408417    NormalVector.Scale(-1.);
    409   Log() << Verbose(1) << "Normal Vector is " << NormalVector << "." << endl;
    410 };
     418  DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << NormalVector << "." << endl);
     419}
     420;
    411421
    412422/** Finds the point on the triangle \a *BTS through which the line defined by \a *MolCenter and \a *x crosses.
     
    422432 * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle.
    423433 */
     434
    424435bool BoundaryTriangleSet::GetIntersectionInsideTriangle(const Vector * const MolCenter, const Vector * const x, Vector * const Intersection) const
    425436{
     
    428439  Vector helper;
    429440
    430   if (!Intersection->GetIntersectionWithPlane(&NormalVector, endpoints[0]->node->node, MolCenter, x)) {
    431     eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl;
     441  try {
     442    *Intersection = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*MolCenter, *x);
     443  }
     444  catch (LinearDependenceException &excp) {
     445    Log() << Verbose(1) << excp;
     446    DoeLog(1) && (eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl);
    432447    return false;
    433448  }
    434449
    435   Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl;
    436   Log() << Verbose(1) << "INFO: Line is from " << *MolCenter << " to " << *x << "." << endl;
    437   Log() << Verbose(1) << "INFO: Intersection is " << *Intersection << "." << endl;
    438 
    439   if (Intersection->DistanceSquared(endpoints[0]->node->node) < MYEPSILON) {
    440     Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl;
     450  DoLog(1) && (Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl);
     451  DoLog(1) && (Log() << Verbose(1) << "INFO: Line is from " << *MolCenter << " to " << *x << "." << endl);
     452  DoLog(1) && (Log() << Verbose(1) << "INFO: Intersection is " << *Intersection << "." << endl);
     453
     454  if (Intersection->DistanceSquared(*endpoints[0]->node->node) < MYEPSILON) {
     455    DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl);
    441456    return true;
    442   }   else if (Intersection->DistanceSquared(endpoints[1]->node->node) < MYEPSILON) {
    443     Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl;
     457  }   else if (Intersection->DistanceSquared(*endpoints[1]->node->node) < MYEPSILON) {
     458    DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl);
    444459    return true;
    445   }   else if (Intersection->DistanceSquared(endpoints[2]->node->node) < MYEPSILON) {
    446     Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl;
     460  }   else if (Intersection->DistanceSquared(*endpoints[2]->node->node) < MYEPSILON) {
     461    DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl);
    447462    return true;
    448463  }
    449464  // Calculate cross point between one baseline and the line from the third endpoint to intersection
    450   int i=0;
     465  int i = 0;
    451466  do {
    452     if (CrossPoint.GetIntersectionOfTwoLinesOnPlane(endpoints[i%3]->node->node, endpoints[(i+1)%3]->node->node, endpoints[(i+2)%3]->node->node, Intersection, &NormalVector)) {
    453       helper.CopyVector(endpoints[(i+1)%3]->node->node);
    454       helper.SubtractVector(endpoints[i%3]->node->node);
    455       CrossPoint.SubtractVector(endpoints[i%3]->node->node);  // cross point was returned as absolute vector
    456       const double s = CrossPoint.ScalarProduct(&helper)/helper.NormSquared();
    457       Log() << Verbose(1) << "INFO: Factor s is " << s << "." << endl;
     467    try {
     468      CrossPoint = GetIntersectionOfTwoLinesOnPlane(*(endpoints[i%3]->node->node),
     469                                                    *(endpoints[(i+1)%3]->node->node),
     470                                                    *(endpoints[(i+2)%3]->node->node),
     471                                                    *Intersection);
     472      helper = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node);
     473      CrossPoint -= (*endpoints[i%3]->node->node);  // cross point was returned as absolute vector
     474      const double s = CrossPoint.ScalarProduct(helper)/helper.NormSquared();
     475      DoLog(1) && (Log() << Verbose(1) << "INFO: Factor s is " << s << "." << endl);
    458476      if ((s < -MYEPSILON) || ((s-1.) > MYEPSILON)) {
    459         Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << "outside of triangle." << endl;
     477        DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << "outside of triangle." << endl);
    460478        i=4;
    461479        break;
    462480      }
    463481      i++;
    464     } else
     482    } catch (LinearDependenceException &excp){
    465483      break;
    466   } while (i<3);
    467   if (i==3) {
    468     Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " inside of triangle." << endl;
     484    }
     485  } while (i < 3);
     486  if (i == 3) {
     487    DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " inside of triangle." << endl);
    469488    return true;
    470489  } else {
    471     Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " outside of triangle." << endl;
     490    DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " outside of triangle." << endl);
    472491    return false;
    473492  }
    474 };
    475 
    476 /** Finds the point on the triangle \a *BTS through which the line defined by \a *MolCenter and \a *x crosses.
    477  * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane
     493}
     494;
     495
     496/** Finds the point on the triangle to the point \a *x.
     497 * We call Vector::GetIntersectionWithPlane() with \a * and the center of the triangle to receive an intersection point.
     498 * Then we check the in-plane part (the part projected down onto plane). We check whether it crosses one of the
     499 * boundary lines. If it does, we return this intersection as closest point, otherwise the projected point down.
    478500 * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not.
    479501 * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
     
    490512
    491513  // 1. get intersection with plane
    492   Log() << Verbose(1) << "INFO: Looking for closest point of triangle " << *this << " to " << *x << "." << endl;
     514  DoLog(1) && (Log() << Verbose(1) << "INFO: Looking for closest point of triangle " << *this << " to " << *x << "." << endl);
    493515  GetCenter(&Direction);
    494   if (!ClosestPoint->GetIntersectionWithPlane(&NormalVector, endpoints[0]->node->node, x, &Direction)) {
    495     ClosestPoint->CopyVector(x);
     516  try {
     517    *ClosestPoint = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*x, Direction);
     518  }
     519  catch (LinearDependenceException &excp) {
     520    (*ClosestPoint) = (*x);
    496521  }
    497522
    498523  // 2. Calculate in plane part of line (x, intersection)
    499   Vector InPlane;
    500   InPlane.CopyVector(x);
    501   InPlane.SubtractVector(ClosestPoint);  // points from plane intersection to straight-down point
    502   InPlane.ProjectOntoPlane(&NormalVector);
    503   InPlane.AddVector(ClosestPoint);
    504 
    505   Log() << Verbose(2) << "INFO: Triangle is " << *this << "." << endl;
    506   Log() << Verbose(2) << "INFO: Line is from " << Direction << " to " << *x << "." << endl;
    507   Log() << Verbose(2) << "INFO: In-plane part is " << InPlane << "." << endl;
     524  Vector InPlane = (*x) - (*ClosestPoint); // points from plane intersection to straight-down point
     525  InPlane.ProjectOntoPlane(NormalVector);
     526  InPlane += *ClosestPoint;
     527
     528  DoLog(2) && (Log() << Verbose(2) << "INFO: Triangle is " << *this << "." << endl);
     529  DoLog(2) && (Log() << Verbose(2) << "INFO: Line is from " << Direction << " to " << *x << "." << endl);
     530  DoLog(2) && (Log() << Verbose(2) << "INFO: In-plane part is " << InPlane << "." << endl);
    508531
    509532  // Calculate cross point between one baseline and the desired point such that distance is shortest
     
    513536  Vector CrossPoint[3];
    514537  Vector helper;
    515   for (int i=0;i<3;i++) {
     538  for (int i = 0; i < 3; i++) {
    516539    // treat direction of line as normal of a (cut)plane and the desired point x as the plane offset, the intersect line with point
    517     Direction.CopyVector(endpoints[(i+1)%3]->node->node);
    518     Direction.SubtractVector(endpoints[i%3]->node->node);
     540    Direction = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node);
    519541    // calculate intersection, line can never be parallel to Direction (is the same vector as PlaneNormal);
    520     CrossPoint[i].GetIntersectionWithPlane(&Direction, &InPlane, endpoints[i%3]->node->node, endpoints[(i+1)%3]->node->node);
    521     CrossDirection[i].CopyVector(&CrossPoint[i]);
    522     CrossDirection[i].SubtractVector(&InPlane);
    523     CrossPoint[i].SubtractVector(endpoints[i%3]->node->node);  // cross point was returned as absolute vector
    524     const double s = CrossPoint[i].ScalarProduct(&Direction)/Direction.NormSquared();
    525     Log() << Verbose(2) << "INFO: Factor s is " << s << "." << endl;
     542    CrossPoint[i] = Plane(Direction, InPlane).GetIntersection(*(endpoints[i%3]->node->node), *(endpoints[(i+1)%3]->node->node));
     543    CrossDirection[i] = CrossPoint[i] - InPlane;
     544    CrossPoint[i] -= (*endpoints[i%3]->node->node);  // cross point was returned as absolute vector
     545    const double s = CrossPoint[i].ScalarProduct(Direction)/Direction.NormSquared();
     546    DoLog(2) && (Log() << Verbose(2) << "INFO: Factor s is " << s << "." << endl);
    526547    if ((s >= -MYEPSILON) && ((s-1.) <= MYEPSILON)) {
    527       CrossPoint[i].AddVector(endpoints[i%3]->node->node);  // make cross point absolute again
    528       Log() << Verbose(2) << "INFO: Crosspoint is " << CrossPoint[i] << ", intersecting BoundaryLine between " << *endpoints[i%3]->node->node << " and " << *endpoints[(i+1)%3]->node->node << "." << endl;
    529       const double distance = CrossPoint[i].DistanceSquared(x);
     548          CrossPoint[i] += (*endpoints[i%3]->node->node);  // make cross point absolute again
     549      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);
     550      const double distance = CrossPoint[i].DistanceSquared(*x);
    530551      if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
    531552        ShortestDistance = distance;
    532         ClosestPoint->CopyVector(&CrossPoint[i]);
     553        (*ClosestPoint) = CrossPoint[i];
    533554      }
    534555    } else
     
    536557  }
    537558  InsideFlag = true;
    538   for (int i=0;i<3;i++) {
    539     const double sign = CrossDirection[i].ScalarProduct(&CrossDirection[(i+1)%3]);
    540     const double othersign = CrossDirection[i].ScalarProduct(&CrossDirection[(i+2)%3]);;
    541     if ((sign > -MYEPSILON) && (othersign > -MYEPSILON))  // have different sign
     559  for (int i = 0; i < 3; i++) {
     560    const double sign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 1) % 3]);
     561    const double othersign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 2) % 3]);
     562
     563    if ((sign > -MYEPSILON) && (othersign > -MYEPSILON)) // have different sign
    542564      InsideFlag = false;
    543565  }
    544566  if (InsideFlag) {
    545     ClosestPoint->CopyVector(&InPlane);
    546     ShortestDistance = InPlane.DistanceSquared(x);
    547   } else {  // also check endnodes
    548     for (int i=0;i<3;i++) {
    549       const double distance = x->DistanceSquared(endpoints[i]->node->node);
     567    (*ClosestPoint) = InPlane;
     568    ShortestDistance = InPlane.DistanceSquared(*x);
     569  } else { // also check endnodes
     570    for (int i = 0; i < 3; i++) {
     571      const double distance = x->DistanceSquared(*endpoints[i]->node->node);
    550572      if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
    551573        ShortestDistance = distance;
    552         ClosestPoint->CopyVector(endpoints[i]->node->node);
     574        (*ClosestPoint) = (*endpoints[i]->node->node);
    553575      }
    554576    }
    555577  }
    556   Log() << Verbose(1) << "INFO: Closest Point is " << *ClosestPoint << " with shortest squared distance is " << ShortestDistance << "." << endl;
     578  DoLog(1) && (Log() << Verbose(1) << "INFO: Closest Point is " << *ClosestPoint << " with shortest squared distance is " << ShortestDistance << "." << endl);
    557579  return ShortestDistance;
    558 };
     580}
     581;
    559582
    560583/** Checks whether lines is any of the three boundary lines this triangle contains.
     
    564587bool BoundaryTriangleSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const
    565588{
    566         Info FunctionInfo(__func__);
    567   for(int i=0;i<3;i++)
     589  Info FunctionInfo(__func__);
     590  for (int i = 0; i < 3; i++)
    568591    if (line == lines[i])
    569592      return true;
    570593  return false;
    571 };
     594}
     595;
    572596
    573597/** Checks whether point is any of the three endpoints this triangle contains.
     
    577601bool BoundaryTriangleSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
    578602{
    579         Info FunctionInfo(__func__);
    580   for(int i=0;i<3;i++)
     603  Info FunctionInfo(__func__);
     604  for (int i = 0; i < 3; i++)
    581605    if (point == endpoints[i])
    582606      return true;
    583607  return false;
    584 };
     608}
     609;
    585610
    586611/** Checks whether point is any of the three endpoints this triangle contains.
     
    590615bool BoundaryTriangleSet::ContainsBoundaryPoint(const TesselPoint * const point) const
    591616{
    592         Info FunctionInfo(__func__);
    593   for(int i=0;i<3;i++)
     617  Info FunctionInfo(__func__);
     618  for (int i = 0; i < 3; i++)
    594619    if (point == endpoints[i]->node)
    595620      return true;
    596621  return false;
    597 };
     622}
     623;
    598624
    599625/** Checks whether three given \a *Points coincide with triangle's endpoints.
     
    603629bool BoundaryTriangleSet::IsPresentTupel(const BoundaryPointSet * const Points[3]) const
    604630{
    605         Info FunctionInfo(__func__);
    606         Log() << Verbose(1) << "INFO: Checking " << Points[0] << ","  << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << "." << endl;
    607   return (((endpoints[0] == Points[0])
    608             || (endpoints[0] == Points[1])
    609             || (endpoints[0] == Points[2])
    610           ) && (
    611             (endpoints[1] == Points[0])
    612             || (endpoints[1] == Points[1])
    613             || (endpoints[1] == Points[2])
    614           ) && (
    615             (endpoints[2] == Points[0])
    616             || (endpoints[2] == Points[1])
    617             || (endpoints[2] == Points[2])
    618 
    619           ));
    620 };
     631  Info FunctionInfo(__func__);
     632  DoLog(1) && (Log() << Verbose(1) << "INFO: Checking " << Points[0] << "," << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << "." << endl);
     633  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])
     634
     635  ));
     636}
     637;
    621638
    622639/** Checks whether three given \a *Points coincide with triangle's endpoints.
     
    626643bool BoundaryTriangleSet::IsPresentTupel(const BoundaryTriangleSet * const T) const
    627644{
    628         Info FunctionInfo(__func__);
    629   return (((endpoints[0] == T->endpoints[0])
    630             || (endpoints[0] == T->endpoints[1])
    631             || (endpoints[0] == T->endpoints[2])
    632           ) && (
    633             (endpoints[1] == T->endpoints[0])
    634             || (endpoints[1] == T->endpoints[1])
    635             || (endpoints[1] == T->endpoints[2])
    636           ) && (
    637             (endpoints[2] == T->endpoints[0])
    638             || (endpoints[2] == T->endpoints[1])
    639             || (endpoints[2] == T->endpoints[2])
    640 
    641           ));
    642 };
     645  Info FunctionInfo(__func__);
     646  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])
     647
     648  ));
     649}
     650;
    643651
    644652/** Returns the endpoint which is not contained in the given \a *line.
     
    648656class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(const BoundaryLineSet * const line) const
    649657{
    650         Info FunctionInfo(__func__);
     658  Info FunctionInfo(__func__);
    651659  // sanity check
    652660  if (!ContainsBoundaryLine(line))
    653661    return NULL;
    654   for(int i=0;i<3;i++)
     662  for (int i = 0; i < 3; i++)
    655663    if (!line->ContainsBoundaryPoint(endpoints[i]))
    656664      return endpoints[i];
    657665  // actually, that' impossible :)
    658666  return NULL;
    659 };
     667}
     668;
    660669
    661670/** Calculates the center point of the triangle.
     
    665674void BoundaryTriangleSet::GetCenter(Vector * const center) const
    666675{
    667         Info FunctionInfo(__func__);
     676  Info FunctionInfo(__func__);
    668677  center->Zero();
    669   for(int i=0;i<3;i++)
    670     center->AddVector(endpoints[i]->node->node);
    671   center->Scale(1./3.);
    672   Log() << Verbose(1) << "INFO: Center is at " << *center << "." << endl;
     678  for (int i = 0; i < 3; i++)
     679    (*center) += (*endpoints[i]->node->node);
     680  center->Scale(1. / 3.);
     681  DoLog(1) && (Log() << Verbose(1) << "INFO: Center is at " << *center << "." << endl);
    673682}
    674683
     
    679688ostream &operator <<(ostream &ost, const BoundaryTriangleSet &a)
    680689{
    681   ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << "," << a.endpoints[1]->node->Name << "," << a.endpoints[2]->node->Name << "]";
    682 //  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
    683 //      << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
     690  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->getName() << "," << a.endpoints[1]->node->getName() << "," << a.endpoints[2]->node->getName() << "]";
     691  //  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
     692  //      << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
    684693  return ost;
    685 };
     694}
     695;
    686696
    687697// ======================================== Polygons on Boundary =================================
     
    693703{
    694704  Info FunctionInfo(__func__);
    695 };
     705}
     706;
    696707
    697708/** Destructor of BoundaryPolygonSet.
     
    703714  Info FunctionInfo(__func__);
    704715  endpoints.clear();
    705   Log() << Verbose(1) << "Erasing polygon Nr." << Nr << " itself." << endl;
    706 };
     716  DoLog(1) && (Log() << Verbose(1) << "Erasing polygon Nr." << Nr << " itself." << endl);
     717}
     718;
    707719
    708720/** Calculates the normal vector for this triangle.
     
    718730  Vector *TotalNormal = new Vector;
    719731  PointSet::const_iterator Runner[3];
    720   for (int i=0;i<3; i++) {
     732  for (int i = 0; i < 3; i++) {
    721733    Runner[i] = endpoints.begin();
    722     for (int j = 0; j<i; j++) { // go as much further
     734    for (int j = 0; j < i; j++) { // go as much further
    723735      Runner[i]++;
    724736      if (Runner[i] == endpoints.end()) {
    725         eLog() << Verbose(0) << "There are less than three endpoints in the polygon!" << endl;
     737        DoeLog(0) && (eLog() << Verbose(0) << "There are less than three endpoints in the polygon!" << endl);
    726738        performCriticalExit();
    727739      }
     
    729741  }
    730742  TotalNormal->Zero();
    731   int counter=0;
    732   for (; Runner[2] != endpoints.end(); ) {
    733     TemporaryNormal.MakeNormalVector((*Runner[0])->node->node, (*Runner[1])->node->node, (*Runner[2])->node->node);
    734     for (int i=0;i<3;i++) // increase each of them
     743  int counter = 0;
     744  for (; Runner[2] != endpoints.end();) {
     745    TemporaryNormal = Plane(*((*Runner[0])->node->node),
     746                            *((*Runner[1])->node->node),
     747                            *((*Runner[2])->node->node)).getNormal();
     748    for (int i = 0; i < 3; i++) // increase each of them
    735749      Runner[i]++;
    736     TotalNormal->AddVector(&TemporaryNormal);
    737   }
    738   TotalNormal->Scale(1./(double)counter);
     750    (*TotalNormal) += TemporaryNormal;
     751  }
     752  TotalNormal->Scale(1. / (double) counter);
    739753
    740754  // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
    741   if (TotalNormal->ScalarProduct(&OtherVector) > 0.)
     755  if (TotalNormal->ScalarProduct(OtherVector) > 0.)
    742756    TotalNormal->Scale(-1.);
    743   Log() << Verbose(1) << "Normal Vector is " << *TotalNormal << "." << endl;
     757  DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << *TotalNormal << "." << endl);
    744758
    745759  return TotalNormal;
    746 };
     760}
     761;
    747762
    748763/** Calculates the center point of the triangle.
     
    756771  int counter = 0;
    757772  for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
    758     center->AddVector((*Runner)->node->node);
     773    (*center) += (*(*Runner)->node->node);
    759774    counter++;
    760775  }
    761   center->Scale(1./(double)counter);
    762   Log() << Verbose(1) << "Center is at " << *center << "." << endl;
     776  center->Scale(1. / (double) counter);
     777  DoLog(1) && (Log() << Verbose(1) << "Center is at " << *center << "." << endl);
    763778}
    764779
     
    771786  Info FunctionInfo(__func__);
    772787  return ContainsPresentTupel(triangle->endpoints, 3);
    773 };
     788}
     789;
    774790
    775791/** Checks whether the polygons contains both endpoints of the line.
     
    781797  Info FunctionInfo(__func__);
    782798  return ContainsPresentTupel(line->endpoints, 2);
    783 };
     799}
     800;
    784801
    785802/** Checks whether point is any of the three endpoints this triangle contains.
     
    790807{
    791808  Info FunctionInfo(__func__);
    792   for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
    793     Log() << Verbose(0) << "Checking against " << **Runner << endl;
     809  for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
     810    DoLog(0) && (Log() << Verbose(0) << "Checking against " << **Runner << endl);
    794811    if (point == (*Runner)) {
    795       Log() << Verbose(0) << " Contained." << endl;
     812      DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
    796813      return true;
    797814    }
    798815  }
    799   Log() << Verbose(0) << " Not contained." << endl;
     816  DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
    800817  return false;
    801 };
     818}
     819;
    802820
    803821/** Checks whether point is any of the three endpoints this triangle contains.
     
    808826{
    809827  Info FunctionInfo(__func__);
    810   for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
     828  for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
    811829    if (point == (*Runner)->node) {
    812       Log() << Verbose(0) << " Contained." << endl;
     830      DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
    813831      return true;
    814832    }
    815   Log() << Verbose(0) << " Not contained." << endl;
     833  DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
    816834  return false;
    817 };
     835}
     836;
    818837
    819838/** Checks whether given array of \a *Points coincide with polygons's endpoints.
     
    826845  Info FunctionInfo(__func__);
    827846  int counter = 0;
    828   Log() << Verbose(1) << "Polygon is " << *this << endl;
    829   for(int i=0;i<dim;i++) {
    830     Log() << Verbose(1) << " Testing endpoint " << *Points[i] << endl;
     847  DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
     848  for (int i = 0; i < dim; i++) {
     849    DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << *Points[i] << endl);
    831850    if (ContainsBoundaryPoint(Points[i])) {
    832851      counter++;
     
    838857  else
    839858    return false;
    840 };
     859}
     860;
    841861
    842862/** Checks whether given PointList coincide with polygons's endpoints.
     
    848868  Info FunctionInfo(__func__);
    849869  size_t counter = 0;
    850   Log() << Verbose(1) << "Polygon is " << *this << endl;
    851   for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
    852     Log() << Verbose(1) << " Testing endpoint " << **Runner << endl;
     870  DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
     871  for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
     872    DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << **Runner << endl);
    853873    if (ContainsBoundaryPoint(*Runner))
    854874      counter++;
     
    859879  else
    860880    return false;
    861 };
     881}
     882;
    862883
    863884/** Checks whether given set of \a *Points coincide with polygons's endpoints.
     
    867888bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPolygonSet * const P) const
    868889{
    869   return ContainsPresentTupel((const PointSet)P->endpoints);
    870 };
     890  return ContainsPresentTupel((const PointSet) P->endpoints);
     891}
     892;
    871893
    872894/** Gathers all the endpoints' triangles in a unique set.
     
    876898{
    877899  Info FunctionInfo(__func__);
    878   pair <TriangleSet::iterator, bool> Tester;
     900  pair<TriangleSet::iterator, bool> Tester;
    879901  TriangleSet *triangles = new TriangleSet;
    880902
    881   for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
    882     for(LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++)
    883       for(TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) {
     903  for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
     904    for (LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++)
     905      for (TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) {
    884906        //Log() << Verbose(0) << " Testing triangle " << *(Sprinter->second) << endl;
    885907        if (ContainsBoundaryTriangle(Sprinter->second)) {
    886908          Tester = triangles->insert(Sprinter->second);
    887909          if (Tester.second)
    888             Log() << Verbose(0) << "Adding triangle " << *(Sprinter->second) << endl;
     910            DoLog(0) && (Log() << Verbose(0) << "Adding triangle " << *(Sprinter->second) << endl);
    889911        }
    890912      }
    891913
    892   Log() << Verbose(1) << "The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total." << endl;
     914  DoLog(1) && (Log() << Verbose(1) << "The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total." << endl);
    893915  return triangles;
    894 };
     916}
     917;
    895918
    896919/** Fills the endpoints of this polygon from the triangles attached to \a *line.
     
    901924{
    902925  Info FunctionInfo(__func__);
    903   pair <PointSet::iterator, bool> Tester;
     926  pair<PointSet::iterator, bool> Tester;
    904927  if (line == NULL)
    905928    return false;
    906   Log() << Verbose(1) << "Filling polygon from line " << *line << endl;
    907   for(TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) {
    908     for (int i=0;i<3;i++) {
     929  DoLog(1) && (Log() << Verbose(1) << "Filling polygon from line " << *line << endl);
     930  for (TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) {
     931    for (int i = 0; i < 3; i++) {
    909932      Tester = endpoints.insert((Runner->second)->endpoints[i]);
    910933      if (Tester.second)
    911         Log() << Verbose(1) << "  Inserting endpoint " << *((Runner->second)->endpoints[i]) << endl;
     934        DoLog(1) && (Log() << Verbose(1) << "  Inserting endpoint " << *((Runner->second)->endpoints[i]) << endl);
    912935    }
    913936  }
    914937
    915938  return true;
    916 };
     939}
     940;
    917941
    918942/** output operator for BoundaryPolygonSet.
     
    923947{
    924948  ost << "[" << a.Nr << "|";
    925   for(PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) {
    926    ost << (*Runner)->node->Name;
    927    Runner++;
    928    if (Runner != a.endpoints.end())
    929      ost << ",";
    930   }
    931   ost<< "]";
     949  for (PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) {
     950    ost << (*Runner)->node->getName();
     951    Runner++;
     952    if (Runner != a.endpoints.end())
     953      ost << ",";
     954  }
     955  ost << "]";
    932956  return ost;
    933 };
     957}
     958;
    934959
    935960// =========================================================== class TESSELPOINT ===========================================
     
    942967  node = NULL;
    943968  nr = -1;
    944   Name = Malloc<char> (1, "TesselPoint::TtesselPoint - Name");
    945   Name = strcpy(Name, "-");
    946 };
     969}
     970;
    947971
    948972/** Destructor for class TesselPoint.
     
    951975{
    952976  //Info FunctionInfo(__func__);
    953 };
     977}
     978;
    954979
    955980/** Prints LCNode to screen.
    956981 */
    957 ostream & operator << (ostream &ost, const TesselPoint &a)
    958 {
    959   ost << "[" << (a.Name) << "|" << a.Name << " at " << *a.node << "]";
     982ostream & operator <<(ostream &ost, const TesselPoint &a)
     983{
     984  ost << "[" << a.getName() << "|" << *a.node << "]";
    960985  return ost;
    961 };
     986}
     987;
    962988
    963989/** Prints LCNode to screen.
    964990 */
    965 ostream & TesselPoint::operator << (ostream &ost)
    966 {
    967         Info FunctionInfo(__func__);
     991ostream & TesselPoint::operator <<(ostream &ost)
     992{
     993  Info FunctionInfo(__func__);
    968994  ost << "[" << (nr) << "|" << this << "]";
    969995  return ost;
     996}
     997;
     998
     999// =========================================================== class POINTCLOUD ============================================
     1000
     1001/** Constructor of class PointCloud.
     1002 */
     1003PointCloud::PointCloud()
     1004{
     1005  //Info FunctionInfo(__func__);
     1006}
     1007;
     1008
     1009/** Destructor for class PointCloud.
     1010 */
     1011PointCloud::~PointCloud()
     1012{
     1013  //Info FunctionInfo(__func__);
     1014}
     1015;
     1016
     1017// ============================ CandidateForTesselation =============================
     1018
     1019/** Constructor of class CandidateForTesselation.
     1020 */
     1021CandidateForTesselation::CandidateForTesselation(BoundaryLineSet* line) :
     1022  BaseLine(line), ThirdPoint(NULL), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI)
     1023{
     1024  Info FunctionInfo(__func__);
     1025}
     1026;
     1027
     1028/** Constructor of class CandidateForTesselation.
     1029 */
     1030CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, BoundaryPointSet* point, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) :
     1031  BaseLine(line), ThirdPoint(point), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI)
     1032{
     1033        Info FunctionInfo(__func__);
     1034  OptCenter = OptCandidateCenter;
     1035  OtherOptCenter = OtherOptCandidateCenter;
    9701036};
    9711037
    9721038
    973 // =========================================================== class POINTCLOUD ============================================
    974 
    975 /** Constructor of class PointCloud.
    976  */
    977 PointCloud::PointCloud()
    978 {
    979         //Info FunctionInfo(__func__);
    980 };
    981 
    982 /** Destructor for class PointCloud.
    983  */
    984 PointCloud::~PointCloud()
    985 {
    986         //Info FunctionInfo(__func__);
    987 };
    988 
    989 // ============================ CandidateForTesselation =============================
    990 
    991 /** Constructor of class CandidateForTesselation.
    992  */
    993 CandidateForTesselation::CandidateForTesselation (BoundaryLineSet* line) :
    994   BaseLine(line),
    995   ShortestAngle(2.*M_PI),
    996   OtherShortestAngle(2.*M_PI)
    997 {
    998         Info FunctionInfo(__func__);
    999 };
    1000 
    1001 
    1002 /** Constructor of class CandidateForTesselation.
    1003  */
    1004 CandidateForTesselation::CandidateForTesselation (TesselPoint *candidate, BoundaryLineSet* line, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) :
    1005     BaseLine(line),
    1006     ShortestAngle(2.*M_PI),
    1007     OtherShortestAngle(2.*M_PI)
    1008 {
    1009         Info FunctionInfo(__func__);
    1010   OptCenter.CopyVector(&OptCandidateCenter);
    1011   OtherOptCenter.CopyVector(&OtherOptCandidateCenter);
    1012 };
    1013 
    10141039/** Destructor for class CandidateForTesselation.
    10151040 */
    1016 CandidateForTesselation::~CandidateForTesselation() {
    1017   BaseLine = NULL;
    1018 };
     1041CandidateForTesselation::~CandidateForTesselation()
     1042{
     1043}
     1044;
     1045
     1046/** Checks validity of a given sphere of a candidate line.
     1047 * Sphere must touch all candidates and the baseline endpoints and there must be no other atoms inside.
     1048 * \param RADIUS radius of sphere
     1049 * \param *LC LinkedCell structure with other atoms
     1050 * \return true - sphere is valid, false - sphere contains other points
     1051 */
     1052bool CandidateForTesselation::CheckValidity(const double RADIUS, const LinkedCell *LC) const
     1053{
     1054  Info FunctionInfo(__func__);
     1055
     1056  const double radiusSquared = RADIUS * RADIUS;
     1057  list<const Vector *> VectorList;
     1058  VectorList.push_back(&OptCenter);
     1059  //VectorList.push_back(&OtherOptCenter);  // don't check the other (wrong) center
     1060
     1061  if (!pointlist.empty())
     1062    DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains candidate list and baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl);
     1063  else
     1064    DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere with no candidates contains baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl);
     1065  // check baseline for OptCenter and OtherOptCenter being on sphere's surface
     1066  for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
     1067    for (int i = 0; i < 2; i++) {
     1068      const double distance = fabs((*VRunner)->DistanceSquared(*BaseLine->endpoints[i]->node->node) - radiusSquared);
     1069      if (distance > HULLEPSILON) {
     1070        DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << *BaseLine->endpoints[i] << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl);
     1071        return false;
     1072      }
     1073    }
     1074  }
     1075
     1076  // check Candidates for OptCenter and OtherOptCenter being on sphere's surface
     1077  for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) {
     1078    const TesselPoint *Walker = *Runner;
     1079    for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
     1080      const double distance = fabs((*VRunner)->DistanceSquared(*Walker->node) - radiusSquared);
     1081      if (distance > HULLEPSILON) {
     1082        DoeLog(1) && (eLog() << Verbose(1) << "Candidate " << *Walker << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl);
     1083        return false;
     1084      } else {
     1085        DoLog(1) && (Log() << Verbose(1) << "Candidate " << *Walker << " is inside by " << distance << "." << endl);
     1086      }
     1087    }
     1088  }
     1089
     1090  DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl);
     1091  bool flag = true;
     1092  for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) {
     1093    // get all points inside the sphere
     1094    TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, (*VRunner));
     1095
     1096    DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << OtherOptCenter << ":" << endl);
     1097    for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
     1098      DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->distance(OtherOptCenter) << "." << endl);
     1099
     1100    // remove baseline's endpoints and candidates
     1101    for (int i = 0; i < 2; i++) {
     1102      DoLog(1) && (Log() << Verbose(1) << "INFO: removing baseline tesselpoint " << *BaseLine->endpoints[i]->node << "." << endl);
     1103      ListofPoints->remove(BaseLine->endpoints[i]->node);
     1104    }
     1105    for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) {
     1106      DoLog(1) && (Log() << Verbose(1) << "INFO: removing candidate tesselpoint " << *(*Runner) << "." << endl);
     1107      ListofPoints->remove(*Runner);
     1108    }
     1109    if (!ListofPoints->empty()) {
     1110      DoeLog(1) && (eLog() << Verbose(1) << "CheckValidity: There are still " << ListofPoints->size() << " points inside the sphere." << endl);
     1111      flag = false;
     1112      DoeLog(1) && (eLog() << Verbose(1) << "External atoms inside of sphere at " << *(*VRunner) << ":" << endl);
     1113      for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
     1114        DoeLog(1) && (eLog() << Verbose(1) << "  " << *(*Runner) << endl);
     1115    }
     1116    delete (ListofPoints);
     1117
     1118    // check with animate_sphere.tcl VMD script
     1119    if (ThirdPoint != NULL) {
     1120      DoLog(1) && (Log() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " " << ThirdPoint->Nr + 1 << " " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl);
     1121    } else {
     1122      DoLog(1) && (Log() << Verbose(1) << "Check by: ... missing third point ..." << endl);
     1123      DoLog(1) && (Log() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " ??? " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl);
     1124    }
     1125  }
     1126  return flag;
     1127}
     1128;
    10191129
    10201130/** output operator for CandidateForTesselation.
     
    10221132 * \param &a boundary line
    10231133 */
    1024 ostream & operator <<(ostream &ost, const  CandidateForTesselation &a)
    1025 {
    1026   ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->Name << "," << a.BaseLine->endpoints[1]->node->Name << "] with ";
     1134ostream & operator <<(ostream &ost, const CandidateForTesselation &a)
     1135{
     1136  ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->getName() << "," << a.BaseLine->endpoints[1]->node->getName() << "] with ";
    10271137  if (a.pointlist.empty())
    10281138    ost << "no candidate.";
     
    10351145    for (TesselPointList::const_iterator Runner = a.pointlist.begin(); Runner != a.pointlist.end(); Runner++)
    10361146      ost << *(*Runner) << " ";
    1037     ost << " at angle " << (a.ShortestAngle)<< ".";
     1147    ost << " at angle " << (a.ShortestAngle) << ".";
    10381148  }
    10391149
    10401150  return ost;
    1041 };
    1042 
     1151}
     1152;
    10431153
    10441154// =========================================================== class TESSELATION ===========================================
     
    10471157 */
    10481158Tesselation::Tesselation() :
    1049   PointsOnBoundaryCount(0),
    1050   LinesOnBoundaryCount(0),
    1051   TrianglesOnBoundaryCount(0),
    1052   LastTriangle(NULL),
    1053   TriangleFilesWritten(0),
    1054   InternalPointer(PointsOnBoundary.begin())
    1055 {
    1056         Info FunctionInfo(__func__);
     1159  PointsOnBoundaryCount(0), LinesOnBoundaryCount(0), TrianglesOnBoundaryCount(0), LastTriangle(NULL), TriangleFilesWritten(0), InternalPointer(PointsOnBoundary.begin())
     1160{
     1161  Info FunctionInfo(__func__);
    10571162}
    10581163;
     
    10631168Tesselation::~Tesselation()
    10641169{
    1065         Info FunctionInfo(__func__);
    1066   Log() << Verbose(0) << "Free'ing TesselStruct ... " << endl;
     1170  Info FunctionInfo(__func__);
     1171  DoLog(0) && (Log() << Verbose(0) << "Free'ing TesselStruct ... " << endl);
    10671172  for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner != TrianglesOnBoundary.end(); runner++) {
    10681173    if (runner->second != NULL) {
     
    10701175      runner->second = NULL;
    10711176    } else
    1072       eLog() << Verbose(1) << "The triangle " << runner->first << " has already been free'd." << endl;
    1073   }
    1074   Log() << Verbose(0) << "This envelope was written to file " << TriangleFilesWritten << " times(s)." << endl;
     1177      DoeLog(1) && (eLog() << Verbose(1) << "The triangle " << runner->first << " has already been free'd." << endl);
     1178  }
     1179  DoLog(0) && (Log() << Verbose(0) << "This envelope was written to file " << TriangleFilesWritten << " times(s)." << endl);
    10751180}
    10761181;
     
    10781183/** PointCloud implementation of GetCenter
    10791184 * Uses PointsOnBoundary and STL stuff.
    1080  */   
     1185 */
    10811186Vector * Tesselation::GetCenter(ofstream *out) const
    10821187{
    1083         Info FunctionInfo(__func__);
    1084   Vector *Center = new Vector(0.,0.,0.);
    1085   int num=0;
     1188  Info FunctionInfo(__func__);
     1189  Vector *Center = new Vector(0., 0., 0.);
     1190  int num = 0;
    10861191  for (GoToFirst(); (!IsEnd()); GoToNext()) {
    1087     Center->AddVector(GetPoint()->node);
     1192    (*Center) += (*GetPoint()->node);
    10881193    num++;
    10891194  }
    1090   Center->Scale(1./num);
     1195  Center->Scale(1. / num);
    10911196  return Center;
    1092 };
     1197}
     1198;
    10931199
    10941200/** PointCloud implementation of GoPoint
    10951201 * Uses PointsOnBoundary and STL stuff.
    1096  */   
     1202 */
    10971203TesselPoint * Tesselation::GetPoint() const
    10981204{
    1099         Info FunctionInfo(__func__);
     1205  Info FunctionInfo(__func__);
    11001206  return (InternalPointer->second->node);
    1101 };
     1207}
     1208;
    11021209
    11031210/** PointCloud implementation of GetTerminalPoint.
    11041211 * Uses PointsOnBoundary and STL stuff.
    1105  */   
     1212 */
    11061213TesselPoint * Tesselation::GetTerminalPoint() const
    11071214{
    1108         Info FunctionInfo(__func__);
     1215  Info FunctionInfo(__func__);
    11091216  PointMap::const_iterator Runner = PointsOnBoundary.end();
    11101217  Runner--;
    11111218  return (Runner->second->node);
    1112 };
     1219}
     1220;
    11131221
    11141222/** PointCloud implementation of GoToNext.
    11151223 * Uses PointsOnBoundary and STL stuff.
    1116  */   
     1224 */
    11171225void Tesselation::GoToNext() const
    11181226{
    1119         Info FunctionInfo(__func__);
     1227  Info FunctionInfo(__func__);
    11201228  if (InternalPointer != PointsOnBoundary.end())
    11211229    InternalPointer++;
    1122 };
     1230}
     1231;
    11231232
    11241233/** PointCloud implementation of GoToPrevious.
    11251234 * Uses PointsOnBoundary and STL stuff.
    1126  */   
     1235 */
    11271236void Tesselation::GoToPrevious() const
    11281237{
    1129         Info FunctionInfo(__func__);
     1238  Info FunctionInfo(__func__);
    11301239  if (InternalPointer != PointsOnBoundary.begin())
    11311240    InternalPointer--;
    1132 };
     1241}
     1242;
    11331243
    11341244/** PointCloud implementation of GoToFirst.
    11351245 * Uses PointsOnBoundary and STL stuff.
    1136  */   
     1246 */
    11371247void Tesselation::GoToFirst() const
    11381248{
    1139         Info FunctionInfo(__func__);
     1249  Info FunctionInfo(__func__);
    11401250  InternalPointer = PointsOnBoundary.begin();
    1141 };
     1251}
     1252;
    11421253
    11431254/** PointCloud implementation of GoToLast.
     
    11461257void Tesselation::GoToLast() const
    11471258{
    1148         Info FunctionInfo(__func__);
     1259  Info FunctionInfo(__func__);
    11491260  InternalPointer = PointsOnBoundary.end();
    11501261  InternalPointer--;
    1151 };
     1262}
     1263;
    11521264
    11531265/** PointCloud implementation of IsEmpty.
    11541266 * Uses PointsOnBoundary and STL stuff.
    1155  */   
     1267 */
    11561268bool Tesselation::IsEmpty() const
    11571269{
    1158         Info FunctionInfo(__func__);
     1270  Info FunctionInfo(__func__);
    11591271  return (PointsOnBoundary.empty());
    1160 };
     1272}
     1273;
    11611274
    11621275/** PointCloud implementation of IsLast.
    11631276 * Uses PointsOnBoundary and STL stuff.
    1164  */   
     1277 */
    11651278bool Tesselation::IsEnd() const
    11661279{
    1167         Info FunctionInfo(__func__);
     1280  Info FunctionInfo(__func__);
    11681281  return (InternalPointer == PointsOnBoundary.end());
    1169 };
    1170 
     1282}
     1283;
    11711284
    11721285/** Gueses first starting triangle of the convex envelope.
     
    11771290void Tesselation::GuessStartingTriangle()
    11781291{
    1179         Info FunctionInfo(__func__);
     1292  Info FunctionInfo(__func__);
    11801293  // 4b. create a starting triangle
    11811294  // 4b1. create all distances
     
    11871300
    11881301  // with A chosen, take each pair B,C and sort
    1189   if (A != PointsOnBoundary.end())
    1190     {
    1191       B = A;
    1192       B++;
    1193       for (; B != PointsOnBoundary.end(); B++)
    1194         {
    1195           C = B;
    1196           C++;
    1197           for (; C != PointsOnBoundary.end(); C++)
    1198             {
    1199               tmp = A->second->node->node->DistanceSquared(B->second->node->node);
    1200               distance = tmp * tmp;
    1201               tmp = A->second->node->node->DistanceSquared(C->second->node->node);
    1202               distance += tmp * tmp;
    1203               tmp = B->second->node->node->DistanceSquared(C->second->node->node);
    1204               distance += tmp * tmp;
    1205               DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C)));
    1206             }
    1207         }
    1208     }
     1302  if (A != PointsOnBoundary.end()) {
     1303    B = A;
     1304    B++;
     1305    for (; B != PointsOnBoundary.end(); B++) {
     1306      C = B;
     1307      C++;
     1308      for (; C != PointsOnBoundary.end(); C++) {
     1309        tmp = A->second->node->node->DistanceSquared(*B->second->node->node);
     1310        distance = tmp * tmp;
     1311        tmp = A->second->node->node->DistanceSquared(*C->second->node->node);
     1312        distance += tmp * tmp;
     1313        tmp = B->second->node->node->DistanceSquared(*C->second->node->node);
     1314        distance += tmp * tmp;
     1315        DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C)));
     1316      }
     1317    }
     1318  }
    12091319  //    // listing distances
    12101320  //    Log() << Verbose(1) << "Listing DistanceMMap:";
     
    12161326  // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
    12171327  DistanceMultiMap::iterator baseline = DistanceMMap.begin();
    1218   for (; baseline != DistanceMMap.end(); baseline++)
    1219     {
    1220       // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
    1221       // 2. next, we have to check whether all points reside on only one side of the triangle
    1222       // 3. construct plane vector
    1223       PlaneVector.MakeNormalVector(A->second->node->node,
    1224           baseline->second.first->second->node->node,
    1225           baseline->second.second->second->node->node);
    1226       Log() << Verbose(2) << "Plane vector of candidate triangle is " << PlaneVector << endl;
    1227       // 4. loop over all points
    1228       double sign = 0.;
    1229       PointMap::iterator checker = PointsOnBoundary.begin();
    1230       for (; checker != PointsOnBoundary.end(); checker++)
    1231         {
    1232           // (neglecting A,B,C)
    1233           if ((checker == A) || (checker == baseline->second.first) || (checker
    1234               == baseline->second.second))
    1235             continue;
    1236           // 4a. project onto plane vector
    1237           TrialVector.CopyVector(checker->second->node->node);
    1238           TrialVector.SubtractVector(A->second->node->node);
    1239           distance = TrialVector.ScalarProduct(&PlaneVector);
    1240           if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
    1241             continue;
    1242           Log() << Verbose(2) << "Projection of " << checker->second->node->Name << " yields distance of " << distance << "." << endl;
    1243           tmp = distance / fabs(distance);
    1244           // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
    1245           if ((sign != 0) && (tmp != sign))
    1246             {
    1247               // 4c. If so, break 4. loop and continue with next candidate in 1. loop
    1248               Log() << Verbose(2) << "Current candidates: "
    1249                   << A->second->node->Name << ","
    1250                   << baseline->second.first->second->node->Name << ","
    1251                   << baseline->second.second->second->node->Name << " leaves "
    1252                   << checker->second->node->Name << " outside the convex hull."
    1253                   << endl;
    1254               break;
    1255             }
    1256           else
    1257             { // note the sign for later
    1258               Log() << Verbose(2) << "Current candidates: "
    1259                   << A->second->node->Name << ","
    1260                   << baseline->second.first->second->node->Name << ","
    1261                   << baseline->second.second->second->node->Name << " leave "
    1262                   << checker->second->node->Name << " inside the convex hull."
    1263                   << endl;
    1264               sign = tmp;
    1265             }
    1266           // 4d. Check whether the point is inside the triangle (check distance to each node
    1267           tmp = checker->second->node->node->DistanceSquared(A->second->node->node);
    1268           int innerpoint = 0;
    1269           if ((tmp < A->second->node->node->DistanceSquared(
    1270               baseline->second.first->second->node->node)) && (tmp
    1271               < A->second->node->node->DistanceSquared(
    1272                   baseline->second.second->second->node->node)))
    1273             innerpoint++;
    1274           tmp = checker->second->node->node->DistanceSquared(
    1275               baseline->second.first->second->node->node);
    1276           if ((tmp < baseline->second.first->second->node->node->DistanceSquared(
    1277               A->second->node->node)) && (tmp
    1278               < baseline->second.first->second->node->node->DistanceSquared(
    1279                   baseline->second.second->second->node->node)))
    1280             innerpoint++;
    1281           tmp = checker->second->node->node->DistanceSquared(
    1282               baseline->second.second->second->node->node);
    1283           if ((tmp < baseline->second.second->second->node->node->DistanceSquared(
    1284               baseline->second.first->second->node->node)) && (tmp
    1285               < baseline->second.second->second->node->node->DistanceSquared(
    1286                   A->second->node->node)))
    1287             innerpoint++;
    1288           // 4e. If so, break 4. loop and continue with next candidate in 1. loop
    1289           if (innerpoint == 3)
    1290             break;
    1291         }
    1292       // 5. come this far, all on same side? Then break 1. loop and construct triangle
    1293       if (checker == PointsOnBoundary.end())
    1294         {
    1295           Log() << Verbose(2) << "Looks like we have a candidate!" << endl;
    1296           break;
    1297         }
    1298     }
    1299   if (baseline != DistanceMMap.end())
    1300     {
    1301       BPS[0] = baseline->second.first->second;
    1302       BPS[1] = baseline->second.second->second;
    1303       BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1304       BPS[0] = A->second;
    1305       BPS[1] = baseline->second.second->second;
    1306       BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1307       BPS[0] = baseline->second.first->second;
    1308       BPS[1] = A->second;
    1309       BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1310 
    1311       // 4b3. insert created triangle
    1312       BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    1313       TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
    1314       TrianglesOnBoundaryCount++;
    1315       for (int i = 0; i < NDIM; i++)
    1316         {
    1317           LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
    1318           LinesOnBoundaryCount++;
    1319         }
    1320 
    1321       Log() << Verbose(1) << "Starting triangle is " << *BTS << "." << endl;
    1322     }
    1323   else
    1324     {
    1325       eLog() << Verbose(0) << "No starting triangle found." << endl;
    1326     }
     1328  for (; baseline != DistanceMMap.end(); baseline++) {
     1329    // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate
     1330    // 2. next, we have to check whether all points reside on only one side of the triangle
     1331    // 3. construct plane vector
     1332    PlaneVector = Plane(*A->second->node->node,
     1333                        *baseline->second.first->second->node->node,
     1334                        *baseline->second.second->second->node->node).getNormal();
     1335    DoLog(2) && (Log() << Verbose(2) << "Plane vector of candidate triangle is " << PlaneVector << endl);
     1336    // 4. loop over all points
     1337    double sign = 0.;
     1338    PointMap::iterator checker = PointsOnBoundary.begin();
     1339    for (; checker != PointsOnBoundary.end(); checker++) {
     1340      // (neglecting A,B,C)
     1341      if ((checker == A) || (checker == baseline->second.first) || (checker == baseline->second.second))
     1342        continue;
     1343      // 4a. project onto plane vector
     1344      TrialVector = (*checker->second->node->node);
     1345      TrialVector.SubtractVector(*A->second->node->node);
     1346      distance = TrialVector.ScalarProduct(PlaneVector);
     1347      if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok
     1348        continue;
     1349      DoLog(2) && (Log() << Verbose(2) << "Projection of " << checker->second->node->getName() << " yields distance of " << distance << "." << endl);
     1350      tmp = distance / fabs(distance);
     1351      // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle)
     1352      if ((sign != 0) && (tmp != sign)) {
     1353        // 4c. If so, break 4. loop and continue with next candidate in 1. loop
     1354        DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->getName() << "," << baseline->second.first->second->node->getName() << "," << baseline->second.second->second->node->getName() << " leaves " << checker->second->node->getName() << " outside the convex hull." << endl);
     1355        break;
     1356      } else { // note the sign for later
     1357        DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->getName() << "," << baseline->second.first->second->node->getName() << "," << baseline->second.second->second->node->getName() << " leave " << checker->second->node->getName() << " inside the convex hull." << endl);
     1358        sign = tmp;
     1359      }
     1360      // 4d. Check whether the point is inside the triangle (check distance to each node
     1361      tmp = checker->second->node->node->DistanceSquared(*A->second->node->node);
     1362      int innerpoint = 0;
     1363      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)))
     1364        innerpoint++;
     1365      tmp = checker->second->node->node->DistanceSquared(*baseline->second.first->second->node->node);
     1366      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)))
     1367        innerpoint++;
     1368      tmp = checker->second->node->node->DistanceSquared(*baseline->second.second->second->node->node);
     1369      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)))
     1370        innerpoint++;
     1371      // 4e. If so, break 4. loop and continue with next candidate in 1. loop
     1372      if (innerpoint == 3)
     1373        break;
     1374    }
     1375    // 5. come this far, all on same side? Then break 1. loop and construct triangle
     1376    if (checker == PointsOnBoundary.end()) {
     1377      DoLog(2) && (Log() << Verbose(2) << "Looks like we have a candidate!" << endl);
     1378      break;
     1379    }
     1380  }
     1381  if (baseline != DistanceMMap.end()) {
     1382    BPS[0] = baseline->second.first->second;
     1383    BPS[1] = baseline->second.second->second;
     1384    BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
     1385    BPS[0] = A->second;
     1386    BPS[1] = baseline->second.second->second;
     1387    BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
     1388    BPS[0] = baseline->second.first->second;
     1389    BPS[1] = A->second;
     1390    BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
     1391
     1392    // 4b3. insert created triangle
     1393    BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
     1394    TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
     1395    TrianglesOnBoundaryCount++;
     1396    for (int i = 0; i < NDIM; i++) {
     1397      LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i]));
     1398      LinesOnBoundaryCount++;
     1399    }
     1400
     1401    DoLog(1) && (Log() << Verbose(1) << "Starting triangle is " << *BTS << "." << endl);
     1402  } else {
     1403    DoeLog(0) && (eLog() << Verbose(0) << "No starting triangle found." << endl);
     1404  }
    13271405}
    13281406;
     
    13431421void Tesselation::TesselateOnBoundary(const PointCloud * const cloud)
    13441422{
    1345         Info FunctionInfo(__func__);
     1423  Info FunctionInfo(__func__);
    13461424  bool flag;
    13471425  PointMap::iterator winner;
     
    13621440        // get peak point with respect to this base line's only triangle
    13631441        BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far
    1364         Log() << Verbose(0) << "Current baseline is between " << *(baseline->second) << "." << endl;
     1442        DoLog(0) && (Log() << Verbose(0) << "Current baseline is between " << *(baseline->second) << "." << endl);
    13651443        for (int i = 0; i < 3; i++)
    13661444          if ((BTS->endpoints[i] != baseline->second->endpoints[0]) && (BTS->endpoints[i] != baseline->second->endpoints[1]))
    13671445            peak = BTS->endpoints[i];
    1368         Log() << Verbose(1) << " and has peak " << *peak << "." << endl;
     1446        DoLog(1) && (Log() << Verbose(1) << " and has peak " << *peak << "." << endl);
    13691447
    13701448        // prepare some auxiliary vectors
    13711449        Vector BaseLineCenter, BaseLine;
    1372         BaseLineCenter.CopyVector(baseline->second->endpoints[0]->node->node);
    1373         BaseLineCenter.AddVector(baseline->second->endpoints[1]->node->node);
    1374         BaseLineCenter.Scale(1. / 2.); // points now to center of base line
    1375         BaseLine.CopyVector(baseline->second->endpoints[0]->node->node);
    1376         BaseLine.SubtractVector(baseline->second->endpoints[1]->node->node);
     1450        BaseLineCenter = 0.5 * ((*baseline->second->endpoints[0]->node->node) +
     1451                                (*baseline->second->endpoints[1]->node->node));
     1452        BaseLine = (*baseline->second->endpoints[0]->node->node) - (*baseline->second->endpoints[1]->node->node);
    13771453
    13781454        // offset to center of triangle
    13791455        CenterVector.Zero();
    13801456        for (int i = 0; i < 3; i++)
    1381           CenterVector.AddVector(BTS->endpoints[i]->node->node);
     1457          CenterVector += (*BTS->endpoints[i]->node->node);
    13821458        CenterVector.Scale(1. / 3.);
    1383         Log() << Verbose(2) << "CenterVector of base triangle is " << CenterVector << endl;
     1459        DoLog(2) && (Log() << Verbose(2) << "CenterVector of base triangle is " << CenterVector << endl);
    13841460
    13851461        // normal vector of triangle
    1386         NormalVector.CopyVector(Center);
    1387         NormalVector.SubtractVector(&CenterVector);
     1462        NormalVector = (*Center) - CenterVector;
    13881463        BTS->GetNormalVector(NormalVector);
    1389         NormalVector.CopyVector(&BTS->NormalVector);
    1390         Log() << Verbose(2) << "NormalVector of base triangle is " << NormalVector << endl;
     1464        NormalVector = BTS->NormalVector;
     1465        DoLog(2) && (Log() << Verbose(2) << "NormalVector of base triangle is " << NormalVector << endl);
    13911466
    13921467        // vector in propagation direction (out of triangle)
    13931468        // project center vector onto triangle plane (points from intersection plane-NormalVector to plane-CenterVector intersection)
    1394         PropagationVector.MakeNormalVector(&BaseLine, &NormalVector);
    1395         TempVector.CopyVector(&CenterVector);
    1396         TempVector.SubtractVector(baseline->second->endpoints[0]->node->node); // TempVector is vector on triangle plane pointing from one baseline egde towards center!
     1469        PropagationVector = Plane(BaseLine, NormalVector,0).getNormal();
     1470        TempVector = CenterVector - (*baseline->second->endpoints[0]->node->node); // TempVector is vector on triangle plane pointing from one baseline egde towards center!
    13971471        //Log() << Verbose(0) << "Projection of propagation onto temp: " << PropagationVector.Projection(&TempVector) << "." << endl;
    1398         if (PropagationVector.ScalarProduct(&TempVector) > 0) // make sure normal propagation vector points outward from baseline
     1472        if (PropagationVector.ScalarProduct(TempVector) > 0) // make sure normal propagation vector points outward from baseline
    13991473          PropagationVector.Scale(-1.);
    1400         Log() << Verbose(2) << "PropagationVector of base triangle is " << PropagationVector << endl;
     1474        DoLog(2) && (Log() << Verbose(2) << "PropagationVector of base triangle is " << PropagationVector << endl);
    14011475        winner = PointsOnBoundary.end();
    14021476
     
    14041478        for (PointMap::iterator target = PointsOnBoundary.begin(); target != PointsOnBoundary.end(); target++) {
    14051479          if ((target->second != baseline->second->endpoints[0]) && (target->second != baseline->second->endpoints[1])) { // don't take the same endpoints
    1406             Log() << Verbose(1) << "Target point is " << *(target->second) << ":" << endl;
     1480            DoLog(1) && (Log() << Verbose(1) << "Target point is " << *(target->second) << ":" << endl);
    14071481
    14081482            // first check direction, so that triangles don't intersect
    1409             VirtualNormalVector.CopyVector(target->second->node->node);
    1410             VirtualNormalVector.SubtractVector(&BaseLineCenter); // points from center of base line to target
    1411             VirtualNormalVector.ProjectOntoPlane(&NormalVector);
    1412             TempAngle = VirtualNormalVector.Angle(&PropagationVector);
    1413             Log() << Verbose(2) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl;
    1414             if (TempAngle > (M_PI/2.)) { // no bends bigger than Pi/2 (90 degrees)
    1415               Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl;
     1483            VirtualNormalVector = (*target->second->node->node) - BaseLineCenter;
     1484            VirtualNormalVector.ProjectOntoPlane(NormalVector);
     1485            TempAngle = VirtualNormalVector.Angle(PropagationVector);
     1486            DoLog(2) && (Log() << Verbose(2) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl);
     1487            if (TempAngle > (M_PI / 2.)) { // no bends bigger than Pi/2 (90 degrees)
     1488              DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl);
    14161489              continue;
    14171490            } else
    1418               Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl;
     1491              DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl);
    14191492
    14201493            // check first and second endpoint (if any connecting line goes to target has at least not more than 1 triangle)
     
    14221495            LineChecker[1] = baseline->second->endpoints[1]->lines.find(target->first);
    14231496            if (((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[0]->second->triangles.size() == 2))) {
    1424               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;
     1497              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);
    14251498              continue;
    14261499            }
    14271500            if (((LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (LineChecker[1]->second->triangles.size() == 2))) {
    1428               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;
     1501              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);
    14291502              continue;
    14301503            }
     
    14321505            // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint)
    14331506            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)))) {
    1434               Log() << Verbose(4) << "Current target is peak!" << endl;
     1507              DoLog(4) && (Log() << Verbose(4) << "Current target is peak!" << endl);
    14351508              continue;
    14361509            }
    14371510
    14381511            // check for linear dependence
    1439             TempVector.CopyVector(baseline->second->endpoints[0]->node->node);
    1440             TempVector.SubtractVector(target->second->node->node);
    1441             helper.CopyVector(baseline->second->endpoints[1]->node->node);
    1442             helper.SubtractVector(target->second->node->node);
    1443             helper.ProjectOntoPlane(&TempVector);
     1512            TempVector = (*baseline->second->endpoints[0]->node->node) - (*target->second->node->node);
     1513            helper = (*baseline->second->endpoints[1]->node->node) - (*target->second->node->node);
     1514            helper.ProjectOntoPlane(TempVector);
    14441515            if (fabs(helper.NormSquared()) < MYEPSILON) {
    1445               Log() << Verbose(2) << "Chosen set of vectors is linear dependent." << endl;
     1516              DoLog(2) && (Log() << Verbose(2) << "Chosen set of vectors is linear dependent." << endl);
    14461517              continue;
    14471518            }
     
    14491520            // in case NOT both were found, create virtually this triangle, get its normal vector, calculate angle
    14501521            flag = true;
    1451             VirtualNormalVector.MakeNormalVector(baseline->second->endpoints[0]->node->node, baseline->second->endpoints[1]->node->node, target->second->node->node);
    1452             TempVector.CopyVector(baseline->second->endpoints[0]->node->node);
    1453             TempVector.AddVector(baseline->second->endpoints[1]->node->node);
    1454             TempVector.AddVector(target->second->node->node);
    1455             TempVector.Scale(1./3.);
    1456             TempVector.SubtractVector(Center);
     1522            VirtualNormalVector = Plane(*(baseline->second->endpoints[0]->node->node),
     1523                                        *(baseline->second->endpoints[1]->node->node),
     1524                                        *(target->second->node->node)).getNormal();
     1525            TempVector = (1./3.) * ((*baseline->second->endpoints[0]->node->node) +
     1526                                    (*baseline->second->endpoints[1]->node->node) +
     1527                                    (*target->second->node->node));
     1528            TempVector -= (*Center);
    14571529            // make it always point outward
    1458             if (VirtualNormalVector.ScalarProduct(&TempVector) < 0)
     1530            if (VirtualNormalVector.ScalarProduct(TempVector) < 0)
    14591531              VirtualNormalVector.Scale(-1.);
    14601532            // calculate angle
    1461             TempAngle = NormalVector.Angle(&VirtualNormalVector);
    1462             Log() << Verbose(2) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl;
     1533            TempAngle = NormalVector.Angle(VirtualNormalVector);
     1534            DoLog(2) && (Log() << Verbose(2) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl);
    14631535            if ((SmallestAngle - TempAngle) > MYEPSILON) { // set to new possible winner
    14641536              SmallestAngle = TempAngle;
    14651537              winner = target;
    1466               Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
     1538              DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl);
    14671539            } else if (fabs(SmallestAngle - TempAngle) < MYEPSILON) { // check the angle to propagation, both possible targets are in one plane! (their normals have same angle)
    14681540              // hence, check the angles to some normal direction from our base line but in this common plane of both targets...
    1469               helper.CopyVector(target->second->node->node);
    1470               helper.SubtractVector(&BaseLineCenter);
    1471               helper.ProjectOntoPlane(&BaseLine);
     1541              helper = (*target->second->node->node) - BaseLineCenter;
     1542              helper.ProjectOntoPlane(BaseLine);
    14721543              // ...the one with the smaller angle is the better candidate
    1473               TempVector.CopyVector(target->second->node->node);
    1474               TempVector.SubtractVector(&BaseLineCenter);
    1475               TempVector.ProjectOntoPlane(&VirtualNormalVector);
    1476               TempAngle = TempVector.Angle(&helper);
    1477               TempVector.CopyVector(winner->second->node->node);
    1478               TempVector.SubtractVector(&BaseLineCenter);
    1479               TempVector.ProjectOntoPlane(&VirtualNormalVector);
    1480               if (TempAngle < TempVector.Angle(&helper)) {
    1481                 TempAngle = NormalVector.Angle(&VirtualNormalVector);
     1544              TempVector = (*target->second->node->node) - BaseLineCenter;
     1545              TempVector.ProjectOntoPlane(VirtualNormalVector);
     1546              TempAngle = TempVector.Angle(helper);
     1547              TempVector = (*winner->second->node->node) - BaseLineCenter;
     1548              TempVector.ProjectOntoPlane(VirtualNormalVector);
     1549              if (TempAngle < TempVector.Angle(helper)) {
     1550                TempAngle = NormalVector.Angle(VirtualNormalVector);
    14821551                SmallestAngle = TempAngle;
    14831552                winner = target;
    1484                 Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl;
     1553                DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl);
    14851554              } else
    1486                 Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl;
     1555                DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl);
    14871556            } else
    1488               Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl;
     1557              DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl);
    14891558          }
    14901559        } // end of loop over all boundary points
     
    14921561        // 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
    14931562        if (winner != PointsOnBoundary.end()) {
    1494           Log() << Verbose(0) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl;
     1563          DoLog(0) && (Log() << Verbose(0) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl);
    14951564          // create the lins of not yet present
    14961565          BLS[0] = baseline->second;
     
    15161585          BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    15171586          BTS->GetCenter(&helper);
    1518           helper.SubtractVector(Center);
    1519           helper.Scale(-1);
     1587          helper -= (*Center);
     1588          helper *= -1;
    15201589          BTS->GetNormalVector(helper);
    15211590          TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
    15221591          TrianglesOnBoundaryCount++;
    15231592        } else {
    1524           eLog() << Verbose(2) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl;
     1593          DoeLog(2) && (eLog() << Verbose(2) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl);
    15251594        }
    15261595
    15271596        // 5d. If the set of lines is not yet empty, go to 5. and continue
    15281597      } else
    1529         Log() << Verbose(0) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl;
     1598        DoLog(0) && (Log() << Verbose(0) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl);
    15301599  } while (flag);
    15311600
    15321601  // exit
    1533   delete(Center);
    1534 };
     1602  delete (Center);
     1603}
     1604;
    15351605
    15361606/** Inserts all points outside of the tesselated surface into it by adding new triangles.
     
    15421612bool Tesselation::InsertStraddlingPoints(const PointCloud *cloud, const LinkedCell *LC)
    15431613{
    1544         Info FunctionInfo(__func__);
     1614  Info FunctionInfo(__func__);
    15451615  Vector Intersection, Normal;
    15461616  TesselPoint *Walker = NULL;
     
    15521622  cloud->GoToFirst();
    15531623  BoundaryPoints = new LinkedCell(this, 5.);
    1554   while (!cloud->IsEnd()) {  // we only have to go once through all points, as boundary can become only bigger
     1624  while (!cloud->IsEnd()) { // we only have to go once through all points, as boundary can become only bigger
    15551625    if (AddFlag) {
    1556       delete(BoundaryPoints);
     1626      delete (BoundaryPoints);
    15571627      BoundaryPoints = new LinkedCell(this, 5.);
    15581628      AddFlag = false;
    15591629    }
    15601630    Walker = cloud->GetPoint();
    1561     Log() << Verbose(0) << "Current point is " << *Walker << "." << endl;
     1631    DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Walker << "." << endl);
    15621632    // get the next triangle
    15631633    triangles = FindClosestTrianglesToVector(Walker->node, BoundaryPoints);
    15641634    BTS = triangles->front();
    15651635    if ((triangles == NULL) || (BTS->ContainsBoundaryPoint(Walker))) {
    1566       Log() << Verbose(0) << "No triangles found, probably a tesselation point itself." << endl;
     1636      DoLog(0) && (Log() << Verbose(0) << "No triangles found, probably a tesselation point itself." << endl);
    15671637      cloud->GoToNext();
    15681638      continue;
    15691639    } else {
    15701640    }
    1571     Log() << Verbose(0) << "Closest triangle is " << *BTS << "." << endl;
     1641    DoLog(0) && (Log() << Verbose(0) << "Closest triangle is " << *BTS << "." << endl);
    15721642    // get the intersection point
    15731643    if (BTS->GetIntersectionInsideTriangle(Center, Walker->node, &Intersection)) {
    1574       Log() << Verbose(0) << "We have an intersection at " << Intersection << "." << endl;
     1644      DoLog(0) && (Log() << Verbose(0) << "We have an intersection at " << Intersection << "." << endl);
    15751645      // we have the intersection, check whether in- or outside of boundary
    1576       if ((Center->DistanceSquared(Walker->node) - Center->DistanceSquared(&Intersection)) < -MYEPSILON) {
     1646      if ((Center->DistanceSquared(*Walker->node) - Center->DistanceSquared(Intersection)) < -MYEPSILON) {
    15771647        // inside, next!
    1578         Log() << Verbose(0) << *Walker << " is inside wrt triangle " << *BTS << "." << endl;
     1648        DoLog(0) && (Log() << Verbose(0) << *Walker << " is inside wrt triangle " << *BTS << "." << endl);
    15791649      } else {
    15801650        // outside!
    1581         Log() << Verbose(0) << *Walker << " is outside wrt triangle " << *BTS << "." << endl;
     1651        DoLog(0) && (Log() << Verbose(0) << *Walker << " is outside wrt triangle " << *BTS << "." << endl);
    15821652        class BoundaryLineSet *OldLines[3], *NewLines[3];
    15831653        class BoundaryPointSet *OldPoints[3], *NewPoint;
    15841654        // store the three old lines and old points
    1585         for (int i=0;i<3;i++) {
     1655        for (int i = 0; i < 3; i++) {
    15861656          OldLines[i] = BTS->lines[i];
    15871657          OldPoints[i] = BTS->endpoints[i];
    15881658        }
    1589         Normal.CopyVector(&BTS->NormalVector);
     1659        Normal = BTS->NormalVector;
    15901660        // add Walker to boundary points
    1591         Log() << Verbose(0) << "Adding " << *Walker << " to BoundaryPoints." << endl;
     1661        DoLog(0) && (Log() << Verbose(0) << "Adding " << *Walker << " to BoundaryPoints." << endl);
    15921662        AddFlag = true;
    1593         if (AddBoundaryPoint(Walker,0))
     1663        if (AddBoundaryPoint(Walker, 0))
    15941664          NewPoint = BPS[0];
    15951665        else
    15961666          continue;
    15971667        // remove triangle
    1598         Log() << Verbose(0) << "Erasing triangle " << *BTS << "." << endl;
     1668        DoLog(0) && (Log() << Verbose(0) << "Erasing triangle " << *BTS << "." << endl);
    15991669        TrianglesOnBoundary.erase(BTS->Nr);
    1600         delete(BTS);
     1670        delete (BTS);
    16011671        // create three new boundary lines
    1602         for (int i=0;i<3;i++) {
     1672        for (int i = 0; i < 3; i++) {
    16031673          BPS[0] = NewPoint;
    16041674          BPS[1] = OldPoints[i];
    16051675          NewLines[i] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);
    1606           Log() << Verbose(1) << "Creating new line " << *NewLines[i] << "." << endl;
     1676          DoLog(1) && (Log() << Verbose(1) << "Creating new line " << *NewLines[i] << "." << endl);
    16071677          LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, NewLines[i])); // no need for check for unique insertion as BPS[0] is definitely a new one
    16081678          LinesOnBoundaryCount++;
    16091679        }
    16101680        // create three new triangle with new point
    1611         for (int i=0;i<3;i++) { // find all baselines
     1681        for (int i = 0; i < 3; i++) { // find all baselines
    16121682          BLS[0] = OldLines[i];
    16131683          int n = 1;
    1614           for (int j=0;j<3;j++) {
     1684          for (int j = 0; j < 3; j++) {
    16151685            if (NewLines[j]->IsConnectedTo(BLS[0])) {
    1616               if (n>2) {
    1617                 eLog() << Verbose(2) << BLS[0] << " connects to all of the new lines?!" << endl;
     1686              if (n > 2) {
     1687                DoeLog(2) && (eLog() << Verbose(2) << BLS[0] << " connects to all of the new lines?!" << endl);
    16181688                return false;
    16191689              } else
     
    16261696          BTS->GetNormalVector(Normal);
    16271697          Normal.Scale(-1.);
    1628           Log() << Verbose(0) << "Created new triangle " << *BTS << "." << endl;
     1698          DoLog(0) && (Log() << Verbose(0) << "Created new triangle " << *BTS << "." << endl);
    16291699          TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS));
    16301700          TrianglesOnBoundaryCount++;
     
    16321702      }
    16331703    } else { // something is wrong with FindClosestTriangleToPoint!
    1634       eLog() << Verbose(1) << "The closest triangle did not produce an intersection!" << endl;
     1704      DoeLog(1) && (eLog() << Verbose(1) << "The closest triangle did not produce an intersection!" << endl);
    16351705      return false;
    16361706    }
     
    16391709
    16401710  // exit
    1641   delete(Center);
     1711  delete (Center);
    16421712  return true;
    1643 };
     1713}
     1714;
    16441715
    16451716/** Adds a point to the tesselation::PointsOnBoundary list.
     
    16501721bool Tesselation::AddBoundaryPoint(TesselPoint * Walker, const int n)
    16511722{
    1652         Info FunctionInfo(__func__);
     1723  Info FunctionInfo(__func__);
    16531724  PointTestPair InsertUnique;
    16541725  BPS[n] = new class BoundaryPointSet(Walker);
     
    16581729    return true;
    16591730  } else {
    1660     delete(BPS[n]);
     1731    delete (BPS[n]);
    16611732    BPS[n] = InsertUnique.first->second;
    16621733    return false;
     
    16721743void Tesselation::AddTesselationPoint(TesselPoint* Candidate, const int n)
    16731744{
    1674         Info FunctionInfo(__func__);
     1745  Info FunctionInfo(__func__);
    16751746  PointTestPair InsertUnique;
    16761747  TPS[n] = new class BoundaryPointSet(Candidate);
     
    16801751  } else {
    16811752    delete TPS[n];
    1682     Log() << Verbose(0) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl;
     1753    DoLog(0) && (Log() << Verbose(0) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl);
    16831754    TPS[n] = (InsertUnique.first)->second;
    16841755  }
     
    16931764void Tesselation::SetTesselationPoint(TesselPoint* Candidate, const int n) const
    16941765{
    1695         Info FunctionInfo(__func__);
     1766  Info FunctionInfo(__func__);
    16961767  PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidate->nr);
    16971768  if (FindPoint != PointsOnBoundary.end())
     
    16991770  else
    17001771    TPS[n] = NULL;
    1701 };
     1772}
     1773;
    17021774
    17031775/** Function tries to add line from current Points in BPS to BoundaryLineSet.
    17041776 * If successful it raises the line count and inserts the new line into the BLS,
    17051777 * if unsuccessful, it writes the line which had been present into the BLS, deleting the new constructed one.
     1778 * @param *OptCenter desired OptCenter if there are more than one candidate line
     1779 * @param *candidate third point of the triangle to be, for checking between multiple open line candidates
    17061780 * @param *a first endpoint
    17071781 * @param *b second endpoint
    17081782 * @param n index of Tesselation::BLS giving the line with both endpoints
    17091783 */
    1710 void Tesselation::AddTesselationLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n) {
     1784void Tesselation::AddTesselationLine(const Vector * const OptCenter, const BoundaryPointSet * const candidate, class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
     1785{
    17111786  bool insertNewLine = true;
    1712 
    17131787  LineMap::iterator FindLine = a->lines.find(b->node->nr);
     1788  BoundaryLineSet *WinningLine = NULL;
    17141789  if (FindLine != a->lines.end()) {
    1715     Log() << Verbose(1) << "INFO: There is at least one line between " << *a << " and " << *b << ": " << *(FindLine->second) << "." << endl;
    1716 
    1717     pair<LineMap::iterator,LineMap::iterator> FindPair;
     1790    DoLog(1) && (Log() << Verbose(1) << "INFO: There is at least one line between " << *a << " and " << *b << ": " << *(FindLine->second) << "." << endl);
     1791
     1792    pair<LineMap::iterator, LineMap::iterator> FindPair;
    17181793    FindPair = a->lines.equal_range(b->node->nr);
    17191794
    1720     for (FindLine = FindPair.first; FindLine != FindPair.second; FindLine++) {
     1795    for (FindLine = FindPair.first; (FindLine != FindPair.second) && (insertNewLine); FindLine++) {
     1796      DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl);
    17211797      // If there is a line with less than two attached triangles, we don't need a new line.
    1722       if (FindLine->second->triangles.size() < 2) {
    1723         insertNewLine = false;
    1724         Log() << Verbose(0) << "Using existing line " << *FindLine->second << endl;
    1725 
    1726         BPS[0] = FindLine->second->endpoints[0];
    1727         BPS[1] = FindLine->second->endpoints[1];
    1728         BLS[n] = FindLine->second;
    1729 
    1730         // remove existing line from OpenLines
    1731         CandidateMap::iterator CandidateLine = OpenLines.find(BLS[n]);
    1732         if (CandidateLine != OpenLines.end()) {
    1733           Log() << Verbose(1) << " Removing line from OpenLines." << endl;
    1734           delete(CandidateLine->second);
    1735           OpenLines.erase(CandidateLine);
    1736         } else {
    1737           eLog() << Verbose(1) << "Line exists and is attached to less than two triangles, but not in OpenLines!" << endl;
     1798      if (FindLine->second->triangles.size() == 1) {
     1799        CandidateMap::iterator Finder = OpenLines.find(FindLine->second);
     1800        if (!Finder->second->pointlist.empty())
     1801          DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl);
     1802        else
     1803          DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate." << endl);
     1804        // get open line
     1805        for (TesselPointList::const_iterator CandidateChecker = Finder->second->pointlist.begin(); CandidateChecker != Finder->second->pointlist.end(); ++CandidateChecker) {
     1806          if ((*(CandidateChecker) == candidate->node) && (OptCenter == NULL || OptCenter->DistanceSquared(Finder->second->OptCenter) < MYEPSILON )) { // stop searching if candidate matches
     1807            DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Candidate " << *(*CandidateChecker) << " has the right center " << Finder->second->OptCenter << "." << endl);
     1808            insertNewLine = false;
     1809            WinningLine = FindLine->second;
     1810            break;
     1811          } else {
     1812            DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *(*CandidateChecker) << "'s center " << Finder->second->OptCenter << " does not match desired on " << *OptCenter << "." << endl);
     1813          }
    17381814        }
    1739 
    1740         break;
    17411815      }
    17421816    }
     
    17441818
    17451819  if (insertNewLine) {
    1746     AlwaysAddTesselationTriangleLine(a, b, n);
     1820    AddNewTesselationTriangleLine(a, b, n);
     1821  } else {
     1822    AddExistingTesselationTriangleLine(WinningLine, n);
    17471823  }
    17481824}
     
    17571833 * @param n index of Tesselation::BLS giving the line with both endpoints
    17581834 */
    1759 void Tesselation::AlwaysAddTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
    1760 {
    1761         Info FunctionInfo(__func__);
    1762   Log() << Verbose(0) << "Adding open line [" << LinesOnBoundaryCount << "|" << *(a->node) << " and " << *(b->node) << "." << endl;
     1835void Tesselation::AddNewTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n)
     1836{
     1837  Info FunctionInfo(__func__);
     1838  DoLog(0) && (Log() << Verbose(0) << "Adding open line [" << LinesOnBoundaryCount << "|" << *(a->node) << " and " << *(b->node) << "." << endl);
    17631839  BPS[0] = a;
    17641840  BPS[1] = b;
    1765   BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);  // this also adds the line to the local maps
     1841  BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); // this also adds the line to the local maps
    17661842  // add line to global map
    17671843  LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n]));
     
    17701846  // also add to open lines
    17711847  CandidateForTesselation *CFT = new CandidateForTesselation(BLS[n]);
    1772   OpenLines.insert(pair< BoundaryLineSet *, CandidateForTesselation *> (BLS[n], CFT));
    1773 };
     1848  OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (BLS[n], CFT));
     1849}
     1850;
     1851
     1852/** Uses an existing line for a new triangle.
     1853 * Sets Tesselation::BLS[\a n] and removes the lines from Tesselation::OpenLines.
     1854 * \param *FindLine the line to add
     1855 * \param n index of the line to set in Tesselation::BLS
     1856 */
     1857void Tesselation::AddExistingTesselationTriangleLine(class BoundaryLineSet *Line, int n)
     1858{
     1859  Info FunctionInfo(__func__);
     1860  DoLog(0) && (Log() << Verbose(0) << "Using existing line " << *Line << endl);
     1861
     1862  // set endpoints and line
     1863  BPS[0] = Line->endpoints[0];
     1864  BPS[1] = Line->endpoints[1];
     1865  BLS[n] = Line;
     1866  // remove existing line from OpenLines
     1867  CandidateMap::iterator CandidateLine = OpenLines.find(BLS[n]);
     1868  if (CandidateLine != OpenLines.end()) {
     1869    DoLog(1) && (Log() << Verbose(1) << " Removing line from OpenLines." << endl);
     1870    delete (CandidateLine->second);
     1871    OpenLines.erase(CandidateLine);
     1872  } else {
     1873    DoeLog(1) && (eLog() << Verbose(1) << "Line exists and is attached to less than two triangles, but not in OpenLines!" << endl);
     1874  }
     1875}
     1876;
    17741877
    17751878/** Function adds triangle to global list.
     
    17781881void Tesselation::AddTesselationTriangle()
    17791882{
    1780         Info FunctionInfo(__func__);
    1781   Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl;
     1883  Info FunctionInfo(__func__);
     1884  DoLog(1) && (Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl);
    17821885
    17831886  // add triangle to global map
     
    17891892
    17901893  // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
    1791 };
     1894}
     1895;
    17921896
    17931897/** Function adds triangle to global list.
     
    17971901void Tesselation::AddTesselationTriangle(const int nr)
    17981902{
    1799         Info FunctionInfo(__func__);
    1800   Log() << Verbose(0) << "Adding triangle to global TrianglesOnBoundary map." << endl;
     1903  Info FunctionInfo(__func__);
     1904  DoLog(0) && (Log() << Verbose(0) << "Adding triangle to global TrianglesOnBoundary map." << endl);
    18011905
    18021906  // add triangle to global map
     
    18071911
    18081912  // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet
    1809 };
     1913}
     1914;
    18101915
    18111916/** Removes a triangle from the tesselation.
     
    18161921void Tesselation::RemoveTesselationTriangle(class BoundaryTriangleSet *triangle)
    18171922{
    1818         Info FunctionInfo(__func__);
     1923  Info FunctionInfo(__func__);
    18191924  if (triangle == NULL)
    18201925    return;
    18211926  for (int i = 0; i < 3; i++) {
    18221927    if (triangle->lines[i] != NULL) {
    1823       Log() << Verbose(0) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl;
     1928      DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl);
    18241929      triangle->lines[i]->triangles.erase(triangle->Nr);
    18251930      if (triangle->lines[i]->triangles.empty()) {
    1826           Log() << Verbose(0) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl;
    1827           RemoveTesselationLine(triangle->lines[i]);
     1931        DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl);
     1932        RemoveTesselationLine(triangle->lines[i]);
    18281933      } else {
    1829         Log() << Verbose(0) << *triangle->lines[i] << " is still attached to another triangle: ";
    1830         OpenLines.insert(pair< BoundaryLineSet *, CandidateForTesselation *> (triangle->lines[i], NULL));
    1831         for(TriangleMap::iterator TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); TriangleRunner++)
    1832           Log() << Verbose(0) << "[" << (TriangleRunner->second)->Nr << "|" << *((TriangleRunner->second)->endpoints[0]) << ", " << *((TriangleRunner->second)->endpoints[1]) << ", " << *((TriangleRunner->second)->endpoints[2]) << "] \t";
    1833         Log() << Verbose(0) << endl;
    1834 //        for (int j=0;j<2;j++) {
    1835 //          Log() << Verbose(0) << "Lines of endpoint " << *(triangle->lines[i]->endpoints[j]) << ": ";
    1836 //          for(LineMap::iterator LineRunner = triangle->lines[i]->endpoints[j]->lines.begin(); LineRunner != triangle->lines[i]->endpoints[j]->lines.end(); LineRunner++)
    1837 //            Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
    1838 //          Log() << Verbose(0) << endl;
    1839 //        }
     1934        DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is still attached to another triangle: ");
     1935        OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (triangle->lines[i], NULL));
     1936        for (TriangleMap::iterator TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); TriangleRunner++)
     1937          DoLog(0) && (Log() << Verbose(0) << "[" << (TriangleRunner->second)->Nr << "|" << *((TriangleRunner->second)->endpoints[0]) << ", " << *((TriangleRunner->second)->endpoints[1]) << ", " << *((TriangleRunner->second)->endpoints[2]) << "] \t");
     1938        DoLog(0) && (Log() << Verbose(0) << endl);
     1939        //        for (int j=0;j<2;j++) {
     1940        //          Log() << Verbose(0) << "Lines of endpoint " << *(triangle->lines[i]->endpoints[j]) << ": ";
     1941        //          for(LineMap::iterator LineRunner = triangle->lines[i]->endpoints[j]->lines.begin(); LineRunner != triangle->lines[i]->endpoints[j]->lines.end(); LineRunner++)
     1942        //            Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
     1943        //          Log() << Verbose(0) << endl;
     1944        //        }
    18401945      }
    1841       triangle->lines[i] = NULL;  // free'd or not: disconnect
     1946      triangle->lines[i] = NULL; // free'd or not: disconnect
    18421947    } else
    1843       eLog() << Verbose(1) << "This line " << i << " has already been free'd." << endl;
     1948      DoeLog(1) && (eLog() << Verbose(1) << "This line " << i << " has already been free'd." << endl);
    18441949  }
    18451950
    18461951  if (TrianglesOnBoundary.erase(triangle->Nr))
    1847     Log() << Verbose(0) << "Removing triangle Nr. " << triangle->Nr << "." << endl;
    1848   delete(triangle);
    1849 };
     1952    DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr. " << triangle->Nr << "." << endl);
     1953  delete (triangle);
     1954}
     1955;
    18501956
    18511957/** Removes a line from the tesselation.
     
    18551961void Tesselation::RemoveTesselationLine(class BoundaryLineSet *line)
    18561962{
    1857         Info FunctionInfo(__func__);
     1963  Info FunctionInfo(__func__);
    18581964  int Numbers[2];
    18591965
     
    18761982        for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
    18771983          if ((*Runner).second == line) {
    1878             Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
     1984            DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl);
    18791985            line->endpoints[i]->lines.erase(Runner);
    18801986            break;
     
    18821988      } else { // there's just a single line left
    18831989        if (line->endpoints[i]->lines.erase(line->Nr))
    1884           Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl;
     1990          DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl);
    18851991      }
    18861992      if (line->endpoints[i]->lines.empty()) {
    1887         Log() << Verbose(0) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl;
     1993        DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl);
    18881994        RemoveTesselationPoint(line->endpoints[i]);
    18891995      } else {
    1890         Log() << Verbose(0) << *line->endpoints[i] << " has still lines it's attached to: ";
    1891         for(LineMap::iterator LineRunner = line->endpoints[i]->lines.begin(); LineRunner != line->endpoints[i]->lines.end(); LineRunner++)
    1892           Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t";
    1893         Log() << Verbose(0) << endl;
     1996        DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has still lines it's attached to: ");
     1997        for (LineMap::iterator LineRunner = line->endpoints[i]->lines.begin(); LineRunner != line->endpoints[i]->lines.end(); LineRunner++)
     1998          DoLog(0) && (Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t");
     1999        DoLog(0) && (Log() << Verbose(0) << endl);
    18942000      }
    1895       line->endpoints[i] = NULL;  // free'd or not: disconnect
     2001      line->endpoints[i] = NULL; // free'd or not: disconnect
    18962002    } else
    1897       eLog() << Verbose(1) << "Endpoint " << i << " has already been free'd." << endl;
     2003      DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << i << " has already been free'd." << endl);
    18982004  }
    18992005  if (!line->triangles.empty())
    1900     eLog() << Verbose(2) << "Memory Leak! I " << *line << " am still connected to some triangles." << endl;
     2006    DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *line << " am still connected to some triangles." << endl);
    19012007
    19022008  if (LinesOnBoundary.erase(line->Nr))
    1903     Log() << Verbose(0) << "Removing line Nr. " << line->Nr << "." << endl;
    1904   delete(line);
    1905 };
     2009    DoLog(0) && (Log() << Verbose(0) << "Removing line Nr. " << line->Nr << "." << endl);
     2010  delete (line);
     2011}
     2012;
    19062013
    19072014/** Removes a point from the tesselation.
     
    19122019void Tesselation::RemoveTesselationPoint(class BoundaryPointSet *point)
    19132020{
    1914         Info FunctionInfo(__func__);
     2021  Info FunctionInfo(__func__);
    19152022  if (point == NULL)
    19162023    return;
    19172024  if (PointsOnBoundary.erase(point->Nr))
    1918     Log() << Verbose(0) << "Removing point Nr. " << point->Nr << "." << endl;
    1919   delete(point);
    1920 };
     2025    DoLog(0) && (Log() << Verbose(0) << "Removing point Nr. " << point->Nr << "." << endl);
     2026  delete (point);
     2027}
     2028;
     2029
     2030/** Checks validity of a given sphere of a candidate line.
     2031 * \sa CandidateForTesselation::CheckValidity(), which is more evolved.
     2032 * We check CandidateForTesselation::OtherOptCenter
     2033 * \param &CandidateLine contains other degenerated candidates which we have to subtract as well
     2034 * \param RADIUS radius of sphere
     2035 * \param *LC LinkedCell structure with other atoms
     2036 * \return true - candidate triangle is degenerated, false - candidate triangle is not degenerated
     2037 */
     2038bool Tesselation::CheckDegeneracy(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC) const
     2039{
     2040  Info FunctionInfo(__func__);
     2041
     2042  DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl);
     2043  bool flag = true;
     2044
     2045  DoLog(1) && (Log() << Verbose(1) << "Check by: draw sphere {" << CandidateLine.OtherOptCenter[0] << " " << CandidateLine.OtherOptCenter[1] << " " << CandidateLine.OtherOptCenter[2] << "} radius " << RADIUS << " resolution 30" << endl);
     2046  // get all points inside the sphere
     2047  TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, &CandidateLine.OtherOptCenter);
     2048
     2049  DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << CandidateLine.OtherOptCenter << ":" << endl);
     2050  for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
     2051    DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->distance(CandidateLine.OtherOptCenter) << "." << endl);
     2052
     2053  // remove triangles's endpoints
     2054  for (int i = 0; i < 2; i++)
     2055    ListofPoints->remove(CandidateLine.BaseLine->endpoints[i]->node);
     2056
     2057  // remove other candidates
     2058  for (TesselPointList::const_iterator Runner = CandidateLine.pointlist.begin(); Runner != CandidateLine.pointlist.end(); ++Runner)
     2059    ListofPoints->remove(*Runner);
     2060
     2061  // check for other points
     2062  if (!ListofPoints->empty()) {
     2063    DoLog(1) && (Log() << Verbose(1) << "CheckDegeneracy: There are still " << ListofPoints->size() << " points inside the sphere." << endl);
     2064    flag = false;
     2065    DoLog(1) && (Log() << Verbose(1) << "External atoms inside of sphere at " << CandidateLine.OtherOptCenter << ":" << endl);
     2066    for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner)
     2067      DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->distance(CandidateLine.OtherOptCenter) << "." << endl);
     2068  }
     2069  delete (ListofPoints);
     2070
     2071  return flag;
     2072}
     2073;
    19212074
    19222075/** Checks whether the triangle consisting of the three points is already present.
     
    19312084int Tesselation::CheckPresenceOfTriangle(TesselPoint *Candidates[3]) const
    19322085{
    1933         Info FunctionInfo(__func__);
     2086  Info FunctionInfo(__func__);
    19342087  int adjacentTriangleCount = 0;
    19352088  class BoundaryPointSet *Points[3];
     
    19532106          for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) {
    19542107            TriangleMap *triangles = &FindLine->second->triangles;
    1955             Log() << Verbose(1) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl;
     2108            DoLog(1) && (Log() << Verbose(1) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl);
    19562109            for (TriangleMap::const_iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) {
    19572110              if (FindTriangle->second->IsPresentTupel(Points)) {
     
    19592112              }
    19602113            }
    1961             Log() << Verbose(1) << "end." << endl;
     2114            DoLog(1) && (Log() << Verbose(1) << "end." << endl);
    19622115          }
    19632116          // Only one of the triangle lines must be considered for the triangle count.
     
    19692122  }
    19702123
    1971   Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl;
     2124  DoLog(0) && (Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl);
    19722125  return adjacentTriangleCount;
    1973 };
     2126}
     2127;
    19742128
    19752129/** Checks whether the triangle consisting of the three points is already present.
     
    19832137class BoundaryTriangleSet * Tesselation::GetPresentTriangle(TesselPoint *Candidates[3])
    19842138{
    1985         Info FunctionInfo(__func__);
     2139  Info FunctionInfo(__func__);
    19862140  class BoundaryTriangleSet *triangle = NULL;
    19872141  class BoundaryPointSet *Points[3];
     
    20212175
    20222176  return triangle;
    2023 };
    2024 
     2177}
     2178;
    20252179
    20262180/** Finds the starting triangle for FindNonConvexBorder().
     
    20312185 * \param RADIUS radius of virtual rolling sphere
    20322186 * \param *LC LinkedCell structure with neighbouring TesselPoint's
    2033  */
    2034 void Tesselation::FindStartingTriangle(const double RADIUS, const LinkedCell *LC)
    2035 {
    2036         Info FunctionInfo(__func__);
     2187 * \return true - a starting triangle has been created, false - no valid triple of points found
     2188 */
     2189bool Tesselation::FindStartingTriangle(const double RADIUS, const LinkedCell *LC)
     2190{
     2191  Info FunctionInfo(__func__);
    20372192  int i = 0;
    20382193  TesselPoint* MaxPoint[NDIM];
    20392194  TesselPoint* Temporary;
    20402195  double maxCoordinate[NDIM];
    2041   BoundaryLineSet BaseLine;
     2196  BoundaryLineSet *BaseLine = NULL;
    20422197  Vector helper;
    20432198  Vector Chord;
    20442199  Vector SearchDirection;
    2045   Vector CircleCenter;  // center of the circle, i.e. of the band of sphere's centers
     2200  Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
    20462201  Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
    20472202  Vector SphereCenter;
     
    20562211
    20572212  // 1. searching topmost point with respect to each axis
    2058   for (int i=0;i<NDIM;i++) { // each axis
    2059     LC->n[i] = LC->N[i]-1; // current axis is topmost cell
    2060     for (LC->n[(i+1)%NDIM]=0;LC->n[(i+1)%NDIM]<LC->N[(i+1)%NDIM];LC->n[(i+1)%NDIM]++)
    2061       for (LC->n[(i+2)%NDIM]=0;LC->n[(i+2)%NDIM]<LC->N[(i+2)%NDIM];LC->n[(i+2)%NDIM]++) {
    2062         const LinkedNodes *List = LC->GetCurrentCell();
     2213  for (int i = 0; i < NDIM; i++) { // each axis
     2214    LC->n[i] = LC->N[i] - 1; // current axis is topmost cell
     2215    for (LC->n[(i + 1) % NDIM] = 0; LC->n[(i + 1) % NDIM] < LC->N[(i + 1) % NDIM]; LC->n[(i + 1) % NDIM]++)
     2216      for (LC->n[(i + 2) % NDIM] = 0; LC->n[(i + 2) % NDIM] < LC->N[(i + 2) % NDIM]; LC->n[(i + 2) % NDIM]++) {
     2217        const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
    20632218        //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
    20642219        if (List != NULL) {
    2065           for (LinkedNodes::const_iterator Runner = List->begin();Runner != List->end();Runner++) {
    2066             if ((*Runner)->node->x[i] > maxCoordinate[i]) {
    2067               Log() << Verbose(1) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl;
    2068               maxCoordinate[i] = (*Runner)->node->x[i];
     2220          for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
     2221            if ((*Runner)->node->at(i) > maxCoordinate[i]) {
     2222              DoLog(1) && (Log() << Verbose(1) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl);
     2223              maxCoordinate[i] = (*Runner)->node->at(i);
    20692224              MaxPoint[i] = (*Runner);
    20702225            }
    20712226          }
    20722227        } else {
    2073           eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl;
     2228          DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
    20742229        }
    20752230      }
    20762231  }
    20772232
    2078   Log() << Verbose(1) << "Found maximum coordinates: ";
    2079   for (int i=0;i<NDIM;i++)
    2080     Log() << Verbose(0) << i << ": " << *MaxPoint[i] << "\t";
    2081   Log() << Verbose(0) << endl;
     2233  DoLog(1) && (Log() << Verbose(1) << "Found maximum coordinates: ");
     2234  for (int i = 0; i < NDIM; i++)
     2235    DoLog(0) && (Log() << Verbose(0) << i << ": " << *MaxPoint[i] << "\t");
     2236  DoLog(0) && (Log() << Verbose(0) << endl);
    20822237
    20832238  BTS = NULL;
    2084   for (int k=0;k<NDIM;k++) {
     2239  for (int k = 0; k < NDIM; k++) {
    20852240    NormalVector.Zero();
    2086     NormalVector.x[k] = 1.;
    2087     BaseLine.endpoints[0] = new BoundaryPointSet(MaxPoint[k]);
    2088     Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine.endpoints[0]->node << "." << endl;
     2241    NormalVector[k] = 1.;
     2242    BaseLine = new BoundaryLineSet();
     2243    BaseLine->endpoints[0] = new BoundaryPointSet(MaxPoint[k]);
     2244    DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl);
    20892245
    20902246    double ShortestAngle;
    20912247    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.
    20922248
    2093     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_...
    2094     if (Temporary == NULL)  // have we found a second point?
     2249    Temporary = NULL;
     2250    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_...
     2251    if (Temporary == NULL) {
     2252      // have we found a second point?
     2253      delete BaseLine;
    20952254      continue;
    2096     BaseLine.endpoints[1] = new BoundaryPointSet(Temporary);
     2255    }
     2256    BaseLine->endpoints[1] = new BoundaryPointSet(Temporary);
    20972257
    20982258    // construct center of circle
    2099     CircleCenter.CopyVector(BaseLine.endpoints[0]->node->node);
    2100     CircleCenter.AddVector(BaseLine.endpoints[1]->node->node);
    2101     CircleCenter.Scale(0.5);
     2259    CircleCenter = 0.5 * ((*BaseLine->endpoints[0]->node->node) + (*BaseLine->endpoints[1]->node->node));
    21022260
    21032261    // construct normal vector of circle
    2104     CirclePlaneNormal.CopyVector(BaseLine.endpoints[0]->node->node);
    2105     CirclePlaneNormal.SubtractVector(BaseLine.endpoints[1]->node->node);
     2262    CirclePlaneNormal = (*BaseLine->endpoints[0]->node->node) - (*BaseLine->endpoints[1]->node->node);
    21062263
    21072264    double radius = CirclePlaneNormal.NormSquared();
    2108     double CircleRadius = sqrt(RADIUS*RADIUS - radius/4.);
    2109 
    2110     NormalVector.ProjectOntoPlane(&CirclePlaneNormal);
     2265    double CircleRadius = sqrt(RADIUS * RADIUS - radius / 4.);
     2266
     2267    NormalVector.ProjectOntoPlane(CirclePlaneNormal);
    21112268    NormalVector.Normalize();
    2112     ShortestAngle = 2.*M_PI; // This will indicate the quadrant.
    2113 
    2114     SphereCenter.CopyVector(&NormalVector);
    2115     SphereCenter.Scale(CircleRadius);
    2116     SphereCenter.AddVector(&CircleCenter);
     2269    ShortestAngle = 2. * M_PI; // This will indicate the quadrant.
     2270
     2271    SphereCenter = (CircleRadius * NormalVector) +  CircleCenter;
    21172272    // Now, NormalVector and SphereCenter are two orthonormalized vectors in the plane defined by CirclePlaneNormal (not normalized)
    21182273
    21192274    // look in one direction of baseline for initial candidate
    2120     SearchDirection.MakeNormalVector(&CirclePlaneNormal, &NormalVector);  // whether we look "left" first or "right" first is not important ...
     2275    SearchDirection = Plane(CirclePlaneNormal, NormalVector,0).getNormal();  // whether we look "left" first or "right" first is not important ...
    21212276
    21222277    // adding point 1 and point 2 and add the line between them
    2123     Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine.endpoints[0]->node << "." << endl;
    2124     Log() << Verbose(0) << "Found second point is at " << *BaseLine.endpoints[1]->node << ".\n";
     2278    DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl);
     2279    DoLog(0) && (Log() << Verbose(0) << "Found second point is at " << *BaseLine->endpoints[1]->node << ".\n");
    21252280
    21262281    //Log() << Verbose(1) << "INFO: OldSphereCenter is at " << helper << ".\n";
    2127     CandidateForTesselation OptCandidates(&BaseLine);
     2282    CandidateForTesselation OptCandidates(BaseLine);
    21282283    FindThirdPointForTesselation(NormalVector, SearchDirection, SphereCenter, OptCandidates, NULL, RADIUS, LC);
    2129     Log() << Verbose(0) << "List of third Points is:" << endl;
     2284    DoLog(0) && (Log() << Verbose(0) << "List of third Points is:" << endl);
    21302285    for (TesselPointList::iterator it = OptCandidates.pointlist.begin(); it != OptCandidates.pointlist.end(); it++) {
    2131         Log() << Verbose(0) << " " << *(*it) << endl;
    2132     }
    2133 
    2134     BTS = NULL;
    2135     AddCandidateTriangle(OptCandidates);
    2136 //    delete(BaseLine.endpoints[0]);
    2137 //    delete(BaseLine.endpoints[1]);
    2138 
    2139     if (BTS != NULL) // we have created one starting triangle
     2286      DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl);
     2287    }
     2288    if (!OptCandidates.pointlist.empty()) {
     2289      BTS = NULL;
     2290      AddCandidatePolygon(OptCandidates, RADIUS, LC);
     2291    } else {
     2292      delete BaseLine;
     2293      continue;
     2294    }
     2295
     2296    if (BTS != NULL) { // we have created one starting triangle
     2297      delete BaseLine;
    21402298      break;
    2141     else {
     2299    } else {
    21422300      // remove all candidates from the list and then the list itself
    21432301      OptCandidates.pointlist.clear();
    21442302    }
    2145   }
    2146 };
     2303    delete BaseLine;
     2304  }
     2305
     2306  return (BTS != NULL);
     2307}
     2308;
    21472309
    21482310/** Checks for a given baseline and a third point candidate whether baselines of the found triangle don't have even better candidates.
     
    22152377//            if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
    22162378//              // rotated the wrong way!
    2217 //              eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl;
     2379//              DoeLog(1) && (eLog()<< Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl);
    22182380//            }
    22192381//
     
    22722434//          }
    22732435//        } else {
    2274 //          eLog() << Verbose(2) << "Baseline is connected to two triangles already?" << endl;
     2436//          DoeLog(2) && (eLog()<< Verbose(2) << "Baseline is connected to two triangles already?" << endl);
    22752437//        }
    22762438//      } else {
     
    22792441//    }
    22802442//  } else {
    2281 //    eLog() << Verbose(1) << "Could not find the TesselPoint " << *ThirdNode << "." << endl;
     2443//    DoeLog(1) && (eLog()<< Verbose(1) << "Could not find the TesselPoint " << *ThirdNode << "." << endl);
    22822444//  }
    22832445//
     
    22932455 * @param *LC LinkedCell structure with neighbouring points
    22942456 */
    2295 bool Tesselation::FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC)
    2296 {
    2297         Info FunctionInfo(__func__);
    2298   bool result = true;
    2299 
     2457bool Tesselation::FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, const BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC)
     2458{
     2459  Info FunctionInfo(__func__);
    23002460  Vector CircleCenter;
    23012461  Vector CirclePlaneNormal;
     
    23032463  Vector SearchDirection;
    23042464  Vector helper;
    2305   TesselPoint *ThirdNode = NULL;
     2465  BoundaryPointSet *ThirdPoint = NULL;
    23062466  LineMap::iterator testline;
    23072467  double radius, CircleRadius;
    23082468
    2309   for (int i=0;i<3;i++)
    2310     if ((T.endpoints[i]->node != CandidateLine.BaseLine->endpoints[0]->node) && (T.endpoints[i]->node != CandidateLine.BaseLine->endpoints[1]->node)) {
    2311       ThirdNode = T.endpoints[i]->node;
     2469  for (int i = 0; i < 3; i++)
     2470    if ((T.endpoints[i] != CandidateLine.BaseLine->endpoints[0]) && (T.endpoints[i] != CandidateLine.BaseLine->endpoints[1])) {
     2471      ThirdPoint = T.endpoints[i];
    23122472      break;
    23132473    }
    2314   Log() << Verbose(0) << "Current baseline is " << *CandidateLine.BaseLine << " with ThirdNode " << *ThirdNode << " of triangle " << T << "." << endl;
     2474  DoLog(0) && (Log() << Verbose(0) << "Current baseline is " << *CandidateLine.BaseLine << " with ThirdPoint " << *ThirdPoint << " of triangle " << T << "." << endl);
     2475
     2476  CandidateLine.T = &T;
    23152477
    23162478  // construct center of circle
    2317   CircleCenter.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
    2318   CircleCenter.AddVector(CandidateLine.BaseLine->endpoints[1]->node->node);
    2319   CircleCenter.Scale(0.5);
     2479  CircleCenter = 0.5 * ((*CandidateLine.BaseLine->endpoints[0]->node->node) +
     2480                        (*CandidateLine.BaseLine->endpoints[1]->node->node));
    23202481
    23212482  // construct normal vector of circle
    2322   CirclePlaneNormal.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
    2323   CirclePlaneNormal.SubtractVector(CandidateLine.BaseLine->endpoints[1]->node->node);
     2483  CirclePlaneNormal = (*CandidateLine.BaseLine->endpoints[0]->node->node) -
     2484                      (*CandidateLine.BaseLine->endpoints[1]->node->node);
    23242485
    23252486  // calculate squared radius of circle
    2326   radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal);
    2327   if (radius/4. < RADIUS*RADIUS) {
     2487  radius = CirclePlaneNormal.ScalarProduct(CirclePlaneNormal);
     2488  if (radius / 4. < RADIUS * RADIUS) {
    23282489    // construct relative sphere center with now known CircleCenter
    2329     RelativeSphereCenter.CopyVector(&T.SphereCenter);
    2330     RelativeSphereCenter.SubtractVector(&CircleCenter);
    2331 
    2332     CircleRadius = RADIUS*RADIUS - radius/4.;
     2490    RelativeSphereCenter = T.SphereCenter - CircleCenter;
     2491
     2492    CircleRadius = RADIUS * RADIUS - radius / 4.;
    23332493    CirclePlaneNormal.Normalize();
    2334     Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
    2335 
    2336     Log() << Verbose(1) << "INFO: OldSphereCenter is at " << T.SphereCenter << "." << endl;
     2494    DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
     2495
     2496    DoLog(1) && (Log() << Verbose(1) << "INFO: OldSphereCenter is at " << T.SphereCenter << "." << endl);
    23372497
    23382498    // construct SearchDirection and an "outward pointer"
    2339     SearchDirection.MakeNormalVector(&RelativeSphereCenter, &CirclePlaneNormal);
    2340     helper.CopyVector(&CircleCenter);
    2341     helper.SubtractVector(ThirdNode->node);
    2342     if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
     2499    SearchDirection = Plane(RelativeSphereCenter, CirclePlaneNormal,0).getNormal();
     2500    helper = CircleCenter - (*ThirdPoint->node->node);
     2501    if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards!
    23432502      SearchDirection.Scale(-1.);
    2344     Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
    2345     if (fabs(RelativeSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {
     2503    DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
     2504    if (fabs(RelativeSphereCenter.ScalarProduct(SearchDirection)) > HULLEPSILON) {
    23462505      // rotated the wrong way!
    2347       eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl;
     2506      DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl);
    23482507    }
    23492508
    23502509    // add third point
    2351     FindThirdPointForTesselation(T.NormalVector, SearchDirection, T.SphereCenter, CandidateLine, ThirdNode, RADIUS, LC);
     2510    FindThirdPointForTesselation(T.NormalVector, SearchDirection, T.SphereCenter, CandidateLine, ThirdPoint, RADIUS, LC);
    23522511
    23532512  } else {
    2354     Log() << Verbose(0) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and base triangle " << T << " is too big!" << endl;
     2513    DoLog(0) && (Log() << Verbose(0) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and base triangle " << T << " is too big!" << endl);
    23552514  }
    23562515
    23572516  if (CandidateLine.pointlist.empty()) {
    2358     eLog() << Verbose(2) << "Could not find a suitable candidate." << endl;
     2517    DoeLog(2) && (eLog() << Verbose(2) << "Could not find a suitable candidate." << endl);
    23592518    return false;
    23602519  }
    2361   Log() << Verbose(0) << "Third Points are: " << endl;
     2520  DoLog(0) && (Log() << Verbose(0) << "Third Points are: " << endl);
    23622521  for (TesselPointList::iterator it = CandidateLine.pointlist.begin(); it != CandidateLine.pointlist.end(); ++it) {
    2363     Log() << Verbose(0) << " " << *(*it) << endl;
     2522    DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl);
    23642523  }
    23652524
    23662525  return true;
    2367 
    2368 //  BoundaryLineSet *BaseRay = CandidateLine.BaseLine;
    2369 //  for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
    2370 //    Log() << Verbose(0) << "Third point candidate is " << *(*it)->point
    2371 //    << " with circumsphere's center at " << (*it)->OptCenter << "." << endl;
    2372 //    Log() << Verbose(0) << "Baseline is " << *BaseRay << endl;
    2373 //
    2374 //    // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2)
    2375 //    TesselPoint *PointCandidates[3];
    2376 //    PointCandidates[0] = (*it)->point;
    2377 //    PointCandidates[1] = BaseRay->endpoints[0]->node;
    2378 //    PointCandidates[2] = BaseRay->endpoints[1]->node;
    2379 //    int existentTrianglesCount = CheckPresenceOfTriangle(PointCandidates);
    2380 //
    2381 //    BTS = NULL;
    2382 //    // check for present edges and whether we reach better candidates from them
    2383 //    //if (HasOtherBaselineBetterCandidate(BaseRay, (*it)->point, ShortestAngle, RADIUS, LC) ) {
    2384 //    if (0) {
    2385 //      result = false;
    2386 //      break;
    2387 //    } else {
    2388 //      // If there is no triangle, add it regularly.
    2389 //      if (existentTrianglesCount == 0) {
    2390 //        AddTesselationPoint((*it)->point, 0);
    2391 //        AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
    2392 //        AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
    2393 //
    2394 //        if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const )TPS)) {
    2395 //          CandidateLine.point = (*it)->point;
    2396 //          CandidateLine.OptCenter.CopyVector(&((*it)->OptCenter));
    2397 //          CandidateLine.OtherOptCenter.CopyVector(&((*it)->OtherOptCenter));
    2398 //          CandidateLine.ShortestAngle = ShortestAngle;
    2399 //        } else {
    2400 ////          eLog() << Verbose(1) << "This triangle consisting of ";
    2401 ////          Log() << Verbose(0) << *(*it)->point << ", ";
    2402 ////          Log() << Verbose(0) << *BaseRay->endpoints[0]->node << " and ";
    2403 ////          Log() << Verbose(0) << *BaseRay->endpoints[1]->node << " ";
    2404 ////          Log() << Verbose(0) << "exists and is not added, as it 0x80000000006fc150(does not seem helpful!" << endl;
    2405 //          result = false;
    2406 //        }
    2407 //      } else if ((existentTrianglesCount >= 1) && (existentTrianglesCount <= 3)) { // If there is a planar region within the structure, we need this triangle a second time.
    2408 //          AddTesselationPoint((*it)->point, 0);
    2409 //          AddTesselationPoint(BaseRay->endpoints[0]->node, 1);
    2410 //          AddTesselationPoint(BaseRay->endpoints[1]->node, 2);
    2411 //
    2412 //          // 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)
    2413 //          // i.e. at least one of the three lines must be present with TriangleCount <= 1
    2414 //          if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const)TPS) || CandidateLine.BaseLine->skipped) {
    2415 //            CandidateLine.point = (*it)->point;
    2416 //            CandidateLine.OptCenter.CopyVector(&(*it)->OptCenter);
    2417 //            CandidateLine.OtherOptCenter.CopyVector(&(*it)->OtherOptCenter);
    2418 //            CandidateLine.ShortestAngle = ShortestAngle+2.*M_PI;
    2419 //
    2420 //          } else {
    2421 ////            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;
    2422 //            result = false;
    2423 //          }
    2424 //      } else {
    2425 ////        Log() << Verbose(1) << "This triangle consisting of ";
    2426 ////        Log() << Verbose(0) << *(*it)->point << ", ";
    2427 ////        Log() << Verbose(0) << *BaseRay->endpoints[0]->node << " and ";
    2428 ////        Log() << Verbose(0) << *BaseRay->endpoints[1]->node << " ";
    2429 ////        Log() << Verbose(0) << "is invalid!" << endl;
    2430 //        result = false;
    2431 //      }
    2432 //    }
    2433 //
    2434 //    // set baseline to new ray from ref point (here endpoints[0]->node) to current candidate (here (*it)->point))
    2435 //    BaseRay = BLS[0];
    2436 //    if ((BTS != NULL) && (BTS->NormalVector.NormSquared() < MYEPSILON)) {
    2437 //      eLog() << Verbose(1) << "Triangle " << *BTS << " has zero normal vector!" << endl;
    2438 //      exit(255);
    2439 //    }
    2440 //
    2441 //  }
    2442 //
    2443 //  // remove all candidates from the list and then the list itself
    2444 //  class CandidateForTesselation *remover = NULL;
    2445 //  for (CandidateList::iterator it = OptCandidates->begin(); it != OptCandidates->end(); ++it) {
    2446 //    remover = *it;
    2447 //    delete(remover);
    2448 //  }
    2449 //  delete(OptCandidates);
    2450   return result;
    2451 };
     2526}
     2527;
     2528
     2529/** Walks through Tesselation::OpenLines() and finds candidates for newly created ones.
     2530 * \param *&LCList atoms in LinkedCell list
     2531 * \param RADIUS radius of the virtual sphere
     2532 * \return true - for all open lines without candidates so far, a candidate has been found,
     2533 *         false - at least one open line without candidate still
     2534 */
     2535bool Tesselation::FindCandidatesforOpenLines(const double RADIUS, const LinkedCell *&LCList)
     2536{
     2537  bool TesselationFailFlag = true;
     2538  CandidateForTesselation *baseline = NULL;
     2539  BoundaryTriangleSet *T = NULL;
     2540
     2541  for (CandidateMap::iterator Runner = OpenLines.begin(); Runner != OpenLines.end(); Runner++) {
     2542    baseline = Runner->second;
     2543    if (baseline->pointlist.empty()) {
     2544      assert((baseline->BaseLine->triangles.size() == 1) && ("Open line without exactly one attached triangle"));
     2545      T = (((baseline->BaseLine->triangles.begin()))->second);
     2546      DoLog(1) && (Log() << Verbose(1) << "Finding best candidate for open line " << *baseline->BaseLine << " of triangle " << *T << endl);
     2547      TesselationFailFlag = TesselationFailFlag && FindNextSuitableTriangle(*baseline, *T, RADIUS, LCList); //the line is there, so there is a triangle, but only one.
     2548    }
     2549  }
     2550  return TesselationFailFlag;
     2551}
     2552;
    24522553
    24532554/** Adds the present line and candidate point from \a &CandidateLine to the Tesselation.
    24542555 * \param CandidateLine triangle to add
    2455  * \NOTE we need the copy operator here as the original CandidateForTesselation is removed in AddTesselationLine()
    2456  */
    2457 void Tesselation::AddCandidateTriangle(CandidateForTesselation CandidateLine)
    2458 {
    2459         Info FunctionInfo(__func__);
     2556 * \param RADIUS Radius of sphere
     2557 * \param *LC LinkedCell structure
     2558 * \NOTE we need the copy operator here as the original CandidateForTesselation is removed in
     2559 * AddTesselationLine() in AddCandidateTriangle()
     2560 */
     2561void Tesselation::AddCandidatePolygon(CandidateForTesselation CandidateLine, const double RADIUS, const LinkedCell *LC)
     2562{
     2563  Info FunctionInfo(__func__);
    24602564  Vector Center;
    24612565  TesselPoint * const TurningPoint = CandidateLine.BaseLine->endpoints[0]->node;
     2566  TesselPointList::iterator Runner;
     2567  TesselPointList::iterator Sprinter;
    24622568
    24632569  // fill the set of neighbours
     
    24682574  TesselPointList *connectedClosestPoints = GetCircleOfSetOfPoints(&SetOfNeighbours, TurningPoint, CandidateLine.BaseLine->endpoints[1]->node->node);
    24692575
     2576  DoLog(0) && (Log() << Verbose(0) << "List of Candidates for Turning Point " << *TurningPoint << ":" << endl);
     2577  for (TesselPointList::iterator TesselRunner = connectedClosestPoints->begin(); TesselRunner != connectedClosestPoints->end(); ++TesselRunner)
     2578    DoLog(0) && (Log() << Verbose(0) << " " << **TesselRunner << endl);
     2579
    24702580  // go through all angle-sorted candidates (in degenerate n-nodes case we may have to add multiple triangles)
    2471   Log() << Verbose(0) << "List of Candidates for Turning Point: " << *TurningPoint << "." << endl;
    2472   for (TesselPointList::iterator TesselRunner = connectedClosestPoints->begin(); TesselRunner != connectedClosestPoints->end(); ++TesselRunner)
    2473     Log() << Verbose(0) << **TesselRunner << endl;
    2474   TesselPointList::iterator Runner = connectedClosestPoints->begin();
    2475   TesselPointList::iterator Sprinter = Runner;
     2581  Runner = connectedClosestPoints->begin();
     2582  Sprinter = Runner;
    24762583  Sprinter++;
    2477   while(Sprinter != connectedClosestPoints->end()) {
    2478     // add the points
     2584  while (Sprinter != connectedClosestPoints->end()) {
     2585    DoLog(0) && (Log() << Verbose(0) << "Current Runner is " << *(*Runner) << " and sprinter is " << *(*Sprinter) << "." << endl);
     2586
    24792587    AddTesselationPoint(TurningPoint, 0);
    2480     AddTesselationPoint((*Runner), 1);
    2481     AddTesselationPoint((*Sprinter), 2);
    2482 
    2483     // add the lines
    2484     AddTesselationLine(TPS[0], TPS[1], 0);
    2485     AddTesselationLine(TPS[0], TPS[2], 1);
    2486     AddTesselationLine(TPS[1], TPS[2], 2);
    2487 
    2488     // add the triangles
    2489     BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    2490     AddTesselationTriangle();
    2491     BTS->GetCenter(&Center);
    2492     Center.SubtractVector(&CandidateLine.OptCenter);
    2493     BTS->SphereCenter.CopyVector(&CandidateLine.OptCenter);
    2494     BTS->GetNormalVector(Center);
    2495 
    2496     Log() << Verbose(0) << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector << "." << endl;
     2588    AddTesselationPoint(*Runner, 1);
     2589    AddTesselationPoint(*Sprinter, 2);
     2590
     2591    AddCandidateTriangle(CandidateLine, Opt);
     2592
    24972593    Runner = Sprinter;
    24982594    Sprinter++;
    2499     Log() << Verbose(0) << "Current Runner is " << **Runner << "." << endl;
    2500     if (Sprinter != connectedClosestPoints->end())
    2501       Log() << Verbose(0) << " There are still more triangles to add." << endl;
    2502   }
    2503   delete(connectedClosestPoints);
     2595    if (Sprinter != connectedClosestPoints->end()) {
     2596      // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked)
     2597      FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OptCenter); // Assume BTS contains last triangle
     2598      DoLog(0) && (Log() << Verbose(0) << " There are still more triangles to add." << endl);
     2599    }
     2600    // pick candidates for other open lines as well
     2601    FindCandidatesforOpenLines(RADIUS, LC);
     2602
     2603    // check whether we add a degenerate or a normal triangle
     2604    if (CheckDegeneracy(CandidateLine, RADIUS, LC)) {
     2605      // add normal and degenerate triangles
     2606      DoLog(1) && (Log() << Verbose(1) << "Triangle of endpoints " << *TPS[0] << "," << *TPS[1] << " and " << *TPS[2] << " is degenerated, adding both sides." << endl);
     2607      AddCandidateTriangle(CandidateLine, OtherOpt);
     2608
     2609      if (Sprinter != connectedClosestPoints->end()) {
     2610        // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked)
     2611        FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OtherOptCenter);
     2612      }
     2613      // pick candidates for other open lines as well
     2614      FindCandidatesforOpenLines(RADIUS, LC);
     2615    }
     2616  }
     2617  delete (connectedClosestPoints);
    25042618};
     2619
     2620/** for polygons (multiple candidates for a baseline) sets internal edges to the correct next candidate.
     2621 * \param *Sprinter next candidate to which internal open lines are set
     2622 * \param *OptCenter OptCenter for this candidate
     2623 */
     2624void Tesselation::FindDegeneratedCandidatesforOpenLines(TesselPoint * const Sprinter, const Vector * const OptCenter)
     2625{
     2626  Info FunctionInfo(__func__);
     2627
     2628  pair<LineMap::iterator, LineMap::iterator> FindPair = TPS[0]->lines.equal_range(TPS[2]->node->nr);
     2629  for (LineMap::const_iterator FindLine = FindPair.first; FindLine != FindPair.second; FindLine++) {
     2630    DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl);
     2631    // If there is a line with less than two attached triangles, we don't need a new line.
     2632    if (FindLine->second->triangles.size() == 1) {
     2633      CandidateMap::iterator Finder = OpenLines.find(FindLine->second);
     2634      if (!Finder->second->pointlist.empty())
     2635        DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl);
     2636      else {
     2637        DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate, setting to next Sprinter" << (*Sprinter) << endl);
     2638        Finder->second->T = BTS;  // is last triangle
     2639        Finder->second->pointlist.push_back(Sprinter);
     2640        Finder->second->ShortestAngle = 0.;
     2641        Finder->second->OptCenter = *OptCenter;
     2642      }
     2643    }
     2644  }
     2645};
     2646
     2647/** If a given \a *triangle is degenerated, this adds both sides.
     2648 * i.e. the triangle with same BoundaryPointSet's but NormalVector in opposite direction.
     2649 * Note that endpoints are stored in Tesselation::TPS
     2650 * \param CandidateLine CanddiateForTesselation structure for the desired BoundaryLine
     2651 * \param RADIUS radius of sphere
     2652 * \param *LC pointer to LinkedCell structure
     2653 */
     2654void Tesselation::AddDegeneratedTriangle(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC)
     2655{
     2656  Info FunctionInfo(__func__);
     2657  Vector Center;
     2658  CandidateMap::const_iterator CandidateCheck = OpenLines.end();
     2659  BoundaryTriangleSet *triangle = NULL;
     2660
     2661  /// 1. Create or pick the lines for the first triangle
     2662  DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for first triangle ..." << endl);
     2663  for (int i = 0; i < 3; i++) {
     2664    BLS[i] = NULL;
     2665    DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
     2666    AddTesselationLine(&CandidateLine.OptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i);
     2667  }
     2668
     2669  /// 2. create the first triangle and NormalVector and so on
     2670  DoLog(0) && (Log() << Verbose(0) << "INFO: Adding first triangle with center at " << CandidateLine.OptCenter << " ..." << endl);
     2671  BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
     2672  AddTesselationTriangle();
     2673
     2674  // create normal vector
     2675  BTS->GetCenter(&Center);
     2676  Center -= CandidateLine.OptCenter;
     2677  BTS->SphereCenter = CandidateLine.OptCenter;
     2678  BTS->GetNormalVector(Center);
     2679  // give some verbose output about the whole procedure
     2680  if (CandidateLine.T != NULL)
     2681    DoLog(0) && (Log() << Verbose(0) << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl);
     2682  else
     2683    DoLog(0) && (Log() << Verbose(0) << "--> New starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
     2684  triangle = BTS;
     2685
     2686  /// 3. Gather candidates for each new line
     2687  DoLog(0) && (Log() << Verbose(0) << "INFO: Adding candidates to new lines ..." << endl);
     2688  for (int i = 0; i < 3; i++) {
     2689    DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
     2690    CandidateCheck = OpenLines.find(BLS[i]);
     2691    if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) {
     2692      if (CandidateCheck->second->T == NULL)
     2693        CandidateCheck->second->T = triangle;
     2694      FindNextSuitableTriangle(*(CandidateCheck->second), *CandidateCheck->second->T, RADIUS, LC);
     2695    }
     2696  }
     2697
     2698  /// 4. Create or pick the lines for the second triangle
     2699  DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for second triangle ..." << endl);
     2700  for (int i = 0; i < 3; i++) {
     2701    DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
     2702    AddTesselationLine(&CandidateLine.OtherOptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i);
     2703  }
     2704
     2705  /// 5. create the second triangle and NormalVector and so on
     2706  DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangle with center at " << CandidateLine.OtherOptCenter << " ..." << endl);
     2707  BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
     2708  AddTesselationTriangle();
     2709
     2710  BTS->SphereCenter = CandidateLine.OtherOptCenter;
     2711  // create normal vector in other direction
     2712  BTS->GetNormalVector(triangle->NormalVector);
     2713  BTS->NormalVector.Scale(-1.);
     2714  // give some verbose output about the whole procedure
     2715  if (CandidateLine.T != NULL)
     2716    DoLog(0) && (Log() << Verbose(0) << "--> New degenerate triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl);
     2717  else
     2718    DoLog(0) && (Log() << Verbose(0) << "--> New degenerate starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
     2719
     2720  /// 6. Adding triangle to new lines
     2721  DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangles to new lines ..." << endl);
     2722  for (int i = 0; i < 3; i++) {
     2723    DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl);
     2724    CandidateCheck = OpenLines.find(BLS[i]);
     2725    if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) {
     2726      if (CandidateCheck->second->T == NULL)
     2727        CandidateCheck->second->T = BTS;
     2728    }
     2729  }
     2730}
     2731;
     2732
     2733/** Adds a triangle to the Tesselation structure from three given TesselPoint's.
     2734 * Note that endpoints are in Tesselation::TPS.
     2735 * \param CandidateLine CandidateForTesselation structure contains other information
     2736 * \param type which opt center to add (i.e. which side) and thus which NormalVector to take
     2737 */
     2738void Tesselation::AddCandidateTriangle(CandidateForTesselation &CandidateLine, enum centers type)
     2739{
     2740  Info FunctionInfo(__func__);
     2741  Vector Center;
     2742  Vector *OptCenter = (type == Opt) ? &CandidateLine.OptCenter : &CandidateLine.OtherOptCenter;
     2743
     2744  // add the lines
     2745  AddTesselationLine(OptCenter, TPS[2], TPS[0], TPS[1], 0);
     2746  AddTesselationLine(OptCenter, TPS[1], TPS[0], TPS[2], 1);
     2747  AddTesselationLine(OptCenter, TPS[0], TPS[1], TPS[2], 2);
     2748
     2749  // add the triangles
     2750  BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
     2751  AddTesselationTriangle();
     2752
     2753  // create normal vector
     2754  BTS->GetCenter(&Center);
     2755  Center.SubtractVector(*OptCenter);
     2756  BTS->SphereCenter = *OptCenter;
     2757  BTS->GetNormalVector(Center);
     2758
     2759  // give some verbose output about the whole procedure
     2760  if (CandidateLine.T != NULL)
     2761    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);
     2762  else
     2763    DoLog(0) && (Log() << Verbose(0) << "--> New" << ((type == OtherOpt) ? " degenerate " : " ") << "starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl);
     2764}
     2765;
    25052766
    25062767/** Checks whether the quadragon of the two triangles connect to \a *Base is convex.
     
    25132774class BoundaryPointSet *Tesselation::IsConvexRectangle(class BoundaryLineSet *Base)
    25142775{
    2515         Info FunctionInfo(__func__);
     2776  Info FunctionInfo(__func__);
    25162777  class BoundaryPointSet *Spot = NULL;
    25172778  class BoundaryLineSet *OtherBase;
    25182779  Vector *ClosestPoint;
    25192780
    2520   int m=0;
    2521   for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2522     for (int j=0;j<3;j++) // all of their endpoints and baselines
     2781  int m = 0;
     2782  for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
     2783    for (int j = 0; j < 3; j++) // all of their endpoints and baselines
    25232784      if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
    25242785        BPS[m++] = runner->second->endpoints[j];
    2525   OtherBase = new class BoundaryLineSet(BPS,-1);
    2526 
    2527   Log() << Verbose(1) << "INFO: Current base line is " << *Base << "." << endl;
    2528   Log() << Verbose(1) << "INFO: Other base line is " << *OtherBase << "." << endl;
     2786  OtherBase = new class BoundaryLineSet(BPS, -1);
     2787
     2788  DoLog(1) && (Log() << Verbose(1) << "INFO: Current base line is " << *Base << "." << endl);
     2789  DoLog(1) && (Log() << Verbose(1) << "INFO: Other base line is " << *OtherBase << "." << endl);
    25292790
    25302791  // get the closest point on each line to the other line
     
    25322793
    25332794  // delete the temporary other base line
    2534   delete(OtherBase);
     2795  delete (OtherBase);
    25352796
    25362797  // get the distance vector from Base line to OtherBase line
    25372798  Vector DistanceToIntersection[2], BaseLine;
    25382799  double distance[2];
    2539   BaseLine.CopyVector(Base->endpoints[1]->node->node);
    2540   BaseLine.SubtractVector(Base->endpoints[0]->node->node);
    2541   for (int i=0;i<2;i++) {
    2542     DistanceToIntersection[i].CopyVector(ClosestPoint);
    2543     DistanceToIntersection[i].SubtractVector(Base->endpoints[i]->node->node);
    2544     distance[i] = BaseLine.ScalarProduct(&DistanceToIntersection[i]);
    2545   }
    2546   delete(ClosestPoint);
    2547   if ((distance[0] * distance[1]) > 0)  { // have same sign?
    2548     Log() << Verbose(1) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1]  << ". " << *Base << "' rectangle is concave." << endl;
     2800  BaseLine = (*Base->endpoints[1]->node->node) - (*Base->endpoints[0]->node->node);
     2801  for (int i = 0; i < 2; i++) {
     2802    DistanceToIntersection[i] = (*ClosestPoint) - (*Base->endpoints[i]->node->node);
     2803    distance[i] = BaseLine.ScalarProduct(DistanceToIntersection[i]);
     2804  }
     2805  delete (ClosestPoint);
     2806  if ((distance[0] * distance[1]) > 0) { // have same sign?
     2807    DoLog(1) && (Log() << Verbose(1) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1] << ". " << *Base << "' rectangle is concave." << endl);
    25492808    if (distance[0] < distance[1]) {
    25502809      Spot = Base->endpoints[0];
     
    25532812    }
    25542813    return Spot;
    2555   } else {  // different sign, i.e. we are in between
    2556     Log() << Verbose(0) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl;
     2814  } else { // different sign, i.e. we are in between
     2815    DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl);
    25572816    return NULL;
    25582817  }
    25592818
    2560 };
     2819}
     2820;
    25612821
    25622822void Tesselation::PrintAllBoundaryPoints(ofstream *out) const
    25632823{
    2564         Info FunctionInfo(__func__);
     2824  Info FunctionInfo(__func__);
    25652825  // print all lines
    2566   Log() << Verbose(0) << "Printing all boundary points for debugging:" << endl;
    2567   for (PointMap::const_iterator PointRunner = PointsOnBoundary.begin();PointRunner != PointsOnBoundary.end(); PointRunner++)
    2568     Log() << Verbose(0) << *(PointRunner->second) << endl;
    2569 };
     2826  DoLog(0) && (Log() << Verbose(0) << "Printing all boundary points for debugging:" << endl);
     2827  for (PointMap::const_iterator PointRunner = PointsOnBoundary.begin(); PointRunner != PointsOnBoundary.end(); PointRunner++)
     2828    DoLog(0) && (Log() << Verbose(0) << *(PointRunner->second) << endl);
     2829}
     2830;
    25702831
    25712832void Tesselation::PrintAllBoundaryLines(ofstream *out) const
    25722833{
    2573         Info FunctionInfo(__func__);
     2834  Info FunctionInfo(__func__);
    25742835  // print all lines
    2575   Log() << Verbose(0) << "Printing all boundary lines for debugging:" << endl;
     2836  DoLog(0) && (Log() << Verbose(0) << "Printing all boundary lines for debugging:" << endl);
    25762837  for (LineMap::const_iterator LineRunner = LinesOnBoundary.begin(); LineRunner != LinesOnBoundary.end(); LineRunner++)
    2577     Log() << Verbose(0) << *(LineRunner->second) << endl;
    2578 };
     2838    DoLog(0) && (Log() << Verbose(0) << *(LineRunner->second) << endl);
     2839}
     2840;
    25792841
    25802842void Tesselation::PrintAllBoundaryTriangles(ofstream *out) const
    25812843{
    2582         Info FunctionInfo(__func__);
     2844  Info FunctionInfo(__func__);
    25832845  // print all triangles
    2584   Log() << Verbose(0) << "Printing all boundary triangles for debugging:" << endl;
     2846  DoLog(0) && (Log() << Verbose(0) << "Printing all boundary triangles for debugging:" << endl);
    25852847  for (TriangleMap::const_iterator TriangleRunner = TrianglesOnBoundary.begin(); TriangleRunner != TrianglesOnBoundary.end(); TriangleRunner++)
    2586     Log() << Verbose(0) << *(TriangleRunner->second) << endl;
    2587 };
     2848    DoLog(0) && (Log() << Verbose(0) << *(TriangleRunner->second) << endl);
     2849}
     2850;
    25882851
    25892852/** For a given boundary line \a *Base and its two triangles, picks the central baseline that is "higher".
     
    25942857double Tesselation::PickFarthestofTwoBaselines(class BoundaryLineSet *Base)
    25952858{
    2596         Info FunctionInfo(__func__);
     2859  Info FunctionInfo(__func__);
    25972860  class BoundaryLineSet *OtherBase;
    25982861  Vector *ClosestPoint[2];
    25992862  double volume;
    26002863
    2601   int m=0;
    2602   for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2603     for (int j=0;j<3;j++) // all of their endpoints and baselines
     2864  int m = 0;
     2865  for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
     2866    for (int j = 0; j < 3; j++) // all of their endpoints and baselines
    26042867      if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints
    26052868        BPS[m++] = runner->second->endpoints[j];
    2606   OtherBase = new class BoundaryLineSet(BPS,-1);
    2607 
    2608   Log() << Verbose(0) << "INFO: Current base line is " << *Base << "." << endl;
    2609   Log() << Verbose(0) << "INFO: Other base line is " << *OtherBase << "." << endl;
     2869  OtherBase = new class BoundaryLineSet(BPS, -1);
     2870
     2871  DoLog(0) && (Log() << Verbose(0) << "INFO: Current base line is " << *Base << "." << endl);
     2872  DoLog(0) && (Log() << Verbose(0) << "INFO: Other base line is " << *OtherBase << "." << endl);
    26102873
    26112874  // get the closest point on each line to the other line
     
    26142877
    26152878  // get the distance vector from Base line to OtherBase line
    2616   Vector Distance;
    2617   Distance.CopyVector(ClosestPoint[1]);
    2618   Distance.SubtractVector(ClosestPoint[0]);
     2879  Vector Distance = (*ClosestPoint[1]) - (*ClosestPoint[0]);
    26192880
    26202881  // calculate volume
     
    26222883
    26232884  // delete the temporary other base line and the closest points
    2624   delete(ClosestPoint[0]);
    2625   delete(ClosestPoint[1]);
    2626   delete(OtherBase);
     2885  delete (ClosestPoint[0]);
     2886  delete (ClosestPoint[1]);
     2887  delete (OtherBase);
    26272888
    26282889  if (Distance.NormSquared() < MYEPSILON) { // check for intersection
    2629     Log() << Verbose(0) << "REJECT: Both lines have an intersection: Nothing to do." << endl;
     2890    DoLog(0) && (Log() << Verbose(0) << "REJECT: Both lines have an intersection: Nothing to do." << endl);
    26302891    return false;
    26312892  } else { // check for sign against BaseLineNormal
     
    26332894    BaseLineNormal.Zero();
    26342895    if (Base->triangles.size() < 2) {
    2635       eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl;
     2896      DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl);
    26362897      return 0.;
    26372898    }
    26382899    for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
    2639       Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
    2640       BaseLineNormal.AddVector(&(runner->second->NormalVector));
    2641     }
    2642     BaseLineNormal.Scale(1./2.);
    2643 
    2644     if (Distance.ScalarProduct(&BaseLineNormal) > MYEPSILON) { // Distance points outwards, hence OtherBase higher than Base -> flip
    2645       Log() << Verbose(0) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl;
     2900    DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl);
     2901      BaseLineNormal += (runner->second->NormalVector);
     2902    }
     2903    BaseLineNormal.Scale(1. / 2.);
     2904
     2905    if (Distance.ScalarProduct(BaseLineNormal) > MYEPSILON) { // Distance points outwards, hence OtherBase higher than Base -> flip
     2906      DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl);
    26462907      // calculate volume summand as a general tetraeder
    26472908      return volume;
    2648     } else {  // Base higher than OtherBase -> do nothing
    2649       Log() << Verbose(0) << "REJECT: Base line is higher: Nothing to do." << endl;
     2909    } else { // Base higher than OtherBase -> do nothing
     2910      DoLog(0) && (Log() << Verbose(0) << "REJECT: Base line is higher: Nothing to do." << endl);
    26502911      return 0.;
    26512912    }
    26522913  }
    2653 };
     2914}
     2915;
    26542916
    26552917/** For a given baseline and its two connected triangles, flips the baseline.
     
    26622924class BoundaryLineSet * Tesselation::FlipBaseline(class BoundaryLineSet *Base)
    26632925{
    2664         Info FunctionInfo(__func__);
     2926  Info FunctionInfo(__func__);
    26652927  class BoundaryLineSet *OldLines[4], *NewLine;
    26662928  class BoundaryPointSet *OldPoints[2];
    26672929  Vector BaseLineNormal;
    26682930  int OldTriangleNrs[2], OldBaseLineNr;
    2669   int i,m;
     2931  int i, m;
    26702932
    26712933  // calculate NormalVector for later use
    26722934  BaseLineNormal.Zero();
    26732935  if (Base->triangles.size() < 2) {
    2674     eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl;
     2936    DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl);
    26752937    return NULL;
    26762938  }
    26772939  for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
    2678     Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl;
    2679     BaseLineNormal.AddVector(&(runner->second->NormalVector));
    2680   }
    2681   BaseLineNormal.Scale(-1./2.); // has to point inside for BoundaryTriangleSet::GetNormalVector()
     2940    DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl);
     2941    BaseLineNormal += (runner->second->NormalVector);
     2942  }
     2943  BaseLineNormal.Scale(-1. / 2.); // has to point inside for BoundaryTriangleSet::GetNormalVector()
    26822944
    26832945  // get the two triangles
    26842946  // gather four endpoints and four lines
    2685   for (int j=0;j<4;j++)
     2947  for (int j = 0; j < 4; j++)
    26862948    OldLines[j] = NULL;
    2687   for (int j=0;j<2;j++)
     2949  for (int j = 0; j < 2; j++)
    26882950    OldPoints[j] = NULL;
    2689   i=0;
    2690   m=0;
    2691   Log() << Verbose(0) << "The four old lines are: ";
    2692   for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2693     for (int j=0;j<3;j++) // all of their endpoints and baselines
     2951  i = 0;
     2952  m = 0;
     2953  DoLog(0) && (Log() << Verbose(0) << "The four old lines are: ");
     2954  for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
     2955    for (int j = 0; j < 3; j++) // all of their endpoints and baselines
    26942956      if (runner->second->lines[j] != Base) { // pick not the central baseline
    26952957        OldLines[i++] = runner->second->lines[j];
    2696         Log() << Verbose(0) << *runner->second->lines[j] << "\t";
     2958        DoLog(0) && (Log() << Verbose(0) << *runner->second->lines[j] << "\t");
    26972959      }
    2698   Log() << Verbose(0) << endl;
    2699   Log() << Verbose(0) << "The two old points are: ";
    2700   for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++)
    2701     for (int j=0;j<3;j++) // all of their endpoints and baselines
     2960  DoLog(0) && (Log() << Verbose(0) << endl);
     2961  DoLog(0) && (Log() << Verbose(0) << "The two old points 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
    27022964      if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) { // and neither of its endpoints
    27032965        OldPoints[m++] = runner->second->endpoints[j];
    2704         Log() << Verbose(0) << *runner->second->endpoints[j] << "\t";
     2966        DoLog(0) && (Log() << Verbose(0) << *runner->second->endpoints[j] << "\t");
    27052967      }
    2706   Log() << Verbose(0) << endl;
     2968  DoLog(0) && (Log() << Verbose(0) << endl);
    27072969
    27082970  // check whether everything is in place to create new lines and triangles
    2709   if (i<4) {
    2710     eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl;
     2971  if (i < 4) {
     2972    DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl);
    27112973    return NULL;
    27122974  }
    2713   for (int j=0;j<4;j++)
     2975  for (int j = 0; j < 4; j++)
    27142976    if (OldLines[j] == NULL) {
    2715       eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl;
     2977      DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl);
    27162978      return NULL;
    27172979    }
    2718   for (int j=0;j<2;j++)
     2980  for (int j = 0; j < 2; j++)
    27192981    if (OldPoints[j] == NULL) {
    2720       eLog() << Verbose(1) << "We have not gathered enough endpoints!" << endl;
     2982      DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough endpoints!" << endl);
    27212983      return NULL;
    27222984    }
    27232985
    27242986  // remove triangles and baseline removes itself
    2725   Log() << Verbose(0) << "INFO: Deleting baseline " << *Base << " from global list." << endl;
     2987  DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting baseline " << *Base << " from global list." << endl);
    27262988  OldBaseLineNr = Base->Nr;
    2727   m=0;
    2728   for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
    2729     Log() << Verbose(0) << "INFO: Deleting triangle " << *(runner->second) << "." << endl;
     2989  m = 0;
     2990  for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) {
     2991    DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting triangle " << *(runner->second) << "." << endl);
    27302992    OldTriangleNrs[m++] = runner->second->Nr;
    27312993    RemoveTesselationTriangle(runner->second);
     
    27372999  NewLine = new class BoundaryLineSet(BPS, OldBaseLineNr);
    27383000  LinesOnBoundary.insert(LinePair(OldBaseLineNr, NewLine)); // no need for check for unique insertion as NewLine is definitely a new one
    2739   Log() << Verbose(0) << "INFO: Created new baseline " << *NewLine << "." << endl;
     3001  DoLog(0) && (Log() << Verbose(0) << "INFO: Created new baseline " << *NewLine << "." << endl);
    27403002
    27413003  // construct new triangles with flipped baseline
    2742   i=-1;
     3004  i = -1;
    27433005  if (OldLines[0]->IsConnectedTo(OldLines[2]))
    2744     i=2;
     3006    i = 2;
    27453007  if (OldLines[0]->IsConnectedTo(OldLines[3]))
    2746     i=3;
    2747   if (i!=-1) {
     3008    i = 3;
     3009  if (i != -1) {
    27483010    BLS[0] = OldLines[0];
    27493011    BLS[1] = OldLines[i];
     
    27523014    BTS->GetNormalVector(BaseLineNormal);
    27533015    AddTesselationTriangle(OldTriangleNrs[0]);
    2754     Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl;
    2755 
    2756     BLS[0] = (i==2 ? OldLines[3] : OldLines[2]);
     3016    DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl);
     3017
     3018    BLS[0] = (i == 2 ? OldLines[3] : OldLines[2]);
    27573019    BLS[1] = OldLines[1];
    27583020    BLS[2] = NewLine;
     
    27603022    BTS->GetNormalVector(BaseLineNormal);
    27613023    AddTesselationTriangle(OldTriangleNrs[1]);
    2762     Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl;
     3024    DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl);
    27633025  } else {
    2764     eLog() << Verbose(0) << "The four old lines do not connect, something's utterly wrong here!" << endl;
     3026    DoeLog(0) && (eLog() << Verbose(0) << "The four old lines do not connect, something's utterly wrong here!" << endl);
    27653027    return NULL;
    27663028  }
    27673029
    27683030  return NewLine;
    2769 };
    2770 
     3031}
     3032;
    27713033
    27723034/** Finds the second point of starting triangle.
     
    27803042void Tesselation::FindSecondPointForTesselation(TesselPoint* a, Vector Oben, TesselPoint*& OptCandidate, double Storage[3], double RADIUS, const LinkedCell *LC)
    27813043{
    2782         Info FunctionInfo(__func__);
     3044  Info FunctionInfo(__func__);
    27833045  Vector AngleCheck;
    27843046  class TesselPoint* Candidate = NULL;
     
    27893051  int Nupper[NDIM];
    27903052
    2791   if (LC->SetIndexToNode(a)) {  // get cell for the starting point
    2792     for(int i=0;i<NDIM;i++) // store indices of this cell
     3053  if (LC->SetIndexToNode(a)) { // get cell for the starting point
     3054    for (int i = 0; i < NDIM; i++) // store indices of this cell
    27933055      N[i] = LC->n[i];
    27943056  } else {
    2795     eLog() << Verbose(1) << "Point " << *a << " is not found in cell " << LC->index << "." << endl;
     3057    DoeLog(1) && (eLog() << Verbose(1) << "Point " << *a << " is not found in cell " << LC->index << "." << endl);
    27963058    return;
    27973059  }
    27983060  // then go through the current and all neighbouring cells and check the contained points for possible candidates
    2799   for (int i=0;i<NDIM;i++) {
    2800     Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
    2801     Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
    2802   }
    2803   Log() << Verbose(0) << "LC Intervals from [" << N[0] << "<->" << LC->N[0] << ", " << N[1] << "<->" << LC->N[1] << ", " << N[2] << "<->" << LC->N[2] << "] :"
    2804     << " [" << Nlower[0] << "," << Nupper[0] << "], " << " [" << Nlower[1] << "," << Nupper[1] << "], " << " [" << Nlower[2] << "," << Nupper[2] << "], " << endl;
     3061  for (int i = 0; i < NDIM; i++) {
     3062    Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0;
     3063    Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1;
     3064  }
     3065  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);
    28053066
    28063067  for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
    28073068    for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
    28083069      for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
    2809         const LinkedNodes *List = LC->GetCurrentCell();
     3070        const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
    28103071        //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
    28113072        if (List != NULL) {
    2812           for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
     3073          for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
    28133074            Candidate = (*Runner);
    28143075            // check if we only have one unique point yet ...
     
    28183079              double distance, scaleFactor;
    28193080
    2820               OrthogonalizedOben.CopyVector(&Oben);
    2821               aCandidate.CopyVector(a->node);
    2822               aCandidate.SubtractVector(Candidate->node);
    2823               OrthogonalizedOben.ProjectOntoPlane(&aCandidate);
     3081              OrthogonalizedOben = Oben;
     3082              aCandidate = (*a->node) - (*Candidate->node);
     3083              OrthogonalizedOben.ProjectOntoPlane(aCandidate);
    28243084              OrthogonalizedOben.Normalize();
    28253085              distance = 0.5 * aCandidate.Norm();
     
    28273087              OrthogonalizedOben.Scale(scaleFactor);
    28283088
    2829               Center.CopyVector(Candidate->node);
    2830               Center.AddVector(a->node);
    2831               Center.Scale(0.5);
    2832               Center.AddVector(&OrthogonalizedOben);
    2833 
    2834               AngleCheck.CopyVector(&Center);
    2835               AngleCheck.SubtractVector(a->node);
     3089              Center = 0.5 * ((*Candidate->node) + (*a->node));
     3090              Center += OrthogonalizedOben;
     3091
     3092              AngleCheck = Center - (*a->node);
    28363093              norm = aCandidate.Norm();
    28373094              // second point shall have smallest angle with respect to Oben vector
    2838               if (norm < RADIUS*2.) {
    2839                 angle = AngleCheck.Angle(&Oben);
     3095              if (norm < RADIUS * 2.) {
     3096                angle = AngleCheck.Angle(Oben);
    28403097                if (angle < Storage[0]) {
    28413098                  //Log() << Verbose(1) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]);
    2842                   Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n";
     3099                  DoLog(1) && (Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n");
    28433100                  OptCandidate = Candidate;
    28443101                  Storage[0] = angle;
     
    28553112          }
    28563113        } else {
    2857           Log() << Verbose(0) << "Linked cell list is empty." << endl;
     3114          DoLog(0) && (Log() << Verbose(0) << "Linked cell list is empty." << endl);
    28583115        }
    28593116      }
    2860 };
    2861 
     3117}
     3118;
    28623119
    28633120/** This recursive function finds a third point, to form a triangle with two given ones.
     
    28873144 * @param OldSphereCenter center of sphere for base triangle, relative to center of BaseLine, giving null angle for the parameter circle
    28883145 * @param CandidateLine CandidateForTesselation with the current base line and list of candidates and ShortestAngle
    2889  * @param ThirdNode third point to avoid in search
     3146 * @param ThirdPoint third point to avoid in search
    28903147 * @param RADIUS radius of sphere
    28913148 * @param *LC LinkedCell structure with neighbouring points
    28923149 */
    2893 void Tesselation::FindThirdPointForTesselation(Vector &NormalVector, Vector &SearchDirection, Vector &OldSphereCenter, CandidateForTesselation &CandidateLine, const class TesselPoint  * const ThirdNode, const double RADIUS, const LinkedCell *LC) const
    2894 {
    2895         Info FunctionInfo(__func__);
    2896   Vector CircleCenter;  // center of the circle, i.e. of the band of sphere's centers
     3150void 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
     3151{
     3152  Info FunctionInfo(__func__);
     3153  Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers
    28973154  Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in
    28983155  Vector SphereCenter;
    2899   Vector NewSphereCenter;   // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility
    2900   Vector OtherNewSphereCenter;   // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility
    2901   Vector NewNormalVector;   // normal vector of the Candidate's triangle
     3156  Vector NewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility
     3157  Vector OtherNewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility
     3158  Vector NewNormalVector; // normal vector of the Candidate's triangle
    29023159  Vector helper, OptCandidateCenter, OtherOptCandidateCenter;
    29033160  Vector RelativeOldSphereCenter;
     
    29103167  TesselPoint *Candidate = NULL;
    29113168
    2912   Log() << Verbose(1) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl;
     3169  DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl);
     3170
     3171  // copy old center
     3172  CandidateLine.OldCenter = OldSphereCenter;
     3173  CandidateLine.ThirdPoint = ThirdPoint;
     3174  CandidateLine.pointlist.clear();
    29133175
    29143176  // construct center of circle
    2915   CircleCenter.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
    2916   CircleCenter.AddVector(CandidateLine.BaseLine->endpoints[1]->node->node);
    2917   CircleCenter.Scale(0.5);
     3177  CircleCenter = 0.5 * ((*CandidateLine.BaseLine->endpoints[0]->node->node) +
     3178                        (*CandidateLine.BaseLine->endpoints[1]->node->node));
    29183179
    29193180  // construct normal vector of circle
    2920   CirclePlaneNormal.CopyVector(CandidateLine.BaseLine->endpoints[0]->node->node);
    2921   CirclePlaneNormal.SubtractVector(CandidateLine.BaseLine->endpoints[1]->node->node);
    2922 
    2923   RelativeOldSphereCenter.CopyVector(&OldSphereCenter);
    2924   RelativeOldSphereCenter.SubtractVector(&CircleCenter);
    2925 
    2926   // calculate squared radius TesselPoint *ThirdNode,f circle
    2927   radius = CirclePlaneNormal.NormSquared()/4.;
    2928   if (radius < RADIUS*RADIUS) {
    2929     CircleRadius = RADIUS*RADIUS - radius;
     3181  CirclePlaneNormal = (*CandidateLine.BaseLine->endpoints[0]->node->node) -
     3182                      (*CandidateLine.BaseLine->endpoints[1]->node->node);
     3183
     3184  RelativeOldSphereCenter = OldSphereCenter - CircleCenter;
     3185
     3186  // calculate squared radius TesselPoint *ThirdPoint,f circle
     3187  radius = CirclePlaneNormal.NormSquared() / 4.;
     3188  if (radius < RADIUS * RADIUS) {
     3189    CircleRadius = RADIUS * RADIUS - radius;
    29303190    CirclePlaneNormal.Normalize();
    2931     Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
     3191    DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
    29323192
    29333193    // test whether old center is on the band's plane
    2934     if (fabs(RelativeOldSphereCenter.ScalarProduct(&CirclePlaneNormal)) > HULLEPSILON) {
    2935       eLog() << Verbose(1) << "Something's very wrong here: RelativeOldSphereCenter is not on the band's plane as desired by " << fabs(RelativeOldSphereCenter.ScalarProduct(&CirclePlaneNormal)) << "!" << endl;
    2936       RelativeOldSphereCenter.ProjectOntoPlane(&CirclePlaneNormal);
     3194    if (fabs(RelativeOldSphereCenter.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) {
     3195      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);
     3196      RelativeOldSphereCenter.ProjectOntoPlane(CirclePlaneNormal);
    29373197    }
    29383198    radius = RelativeOldSphereCenter.NormSquared();
    29393199    if (fabs(radius - CircleRadius) < HULLEPSILON) {
    2940       Log() << Verbose(1) << "INFO: RelativeOldSphereCenter is at " << RelativeOldSphereCenter << "." << endl;
     3200      DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeOldSphereCenter is at " << RelativeOldSphereCenter << "." << endl);
    29413201
    29423202      // check SearchDirection
    2943       Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
    2944       if (fabs(RelativeOldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) { // rotated the wrong way!
    2945         eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl;
     3203      DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
     3204      if (fabs(RelativeOldSphereCenter.ScalarProduct(SearchDirection)) > HULLEPSILON) { // rotated the wrong way!
     3205        DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl);
    29463206      }
    29473207
    29483208      // get cell for the starting point
    29493209      if (LC->SetIndexToVector(&CircleCenter)) {
    2950         for(int i=0;i<NDIM;i++) // store indices of this cell
    2951         N[i] = LC->n[i];
     3210        for (int i = 0; i < NDIM; i++) // store indices of this cell
     3211          N[i] = LC->n[i];
    29523212        //Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
    29533213      } else {
    2954         eLog() << Verbose(1) << "Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl;
     3214        DoeLog(1) && (eLog() << Verbose(1) << "Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl);
    29553215        return;
    29563216      }
    29573217      // then go through the current and all neighbouring cells and check the contained points for possible candidates
    29583218      //Log() << Verbose(1) << "LC Intervals:";
    2959       for (int i=0;i<NDIM;i++) {
    2960         Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0;
    2961         Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1;
     3219      for (int i = 0; i < NDIM; i++) {
     3220        Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0;
     3221        Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1;
    29623222        //Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ";
    29633223      }
     
    29663226        for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
    29673227          for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
    2968             const LinkedNodes *List = LC->GetCurrentCell();
     3228            const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
    29693229            //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl;
    29703230            if (List != NULL) {
    2971               for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
     3231              for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
    29723232                Candidate = (*Runner);
    29733233
    29743234                // check for three unique points
    2975                 Log() << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " for BaseLine " << *CandidateLine.BaseLine << " with OldSphereCenter " << OldSphereCenter << "." << endl;
    2976                 if ((Candidate != CandidateLine.BaseLine->endpoints[0]->node) && (Candidate != CandidateLine.BaseLine->endpoints[1]->node) ){
     3235                DoLog(2) && (Log() << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " for BaseLine " << *CandidateLine.BaseLine << " with OldSphereCenter " << OldSphereCenter << "." << endl);
     3236                if ((Candidate != CandidateLine.BaseLine->endpoints[0]->node) && (Candidate != CandidateLine.BaseLine->endpoints[1]->node)) {
    29773237
    29783238                  // find center on the plane
    29793239                  GetCenterofCircumcircle(&NewPlaneCenter, *CandidateLine.BaseLine->endpoints[0]->node->node, *CandidateLine.BaseLine->endpoints[1]->node->node, *Candidate->node);
    2980                   Log() << Verbose(1) << "INFO: NewPlaneCenter is " << NewPlaneCenter << "." << endl;
    2981 
    2982                   if (NewNormalVector.MakeNormalVector(CandidateLine.BaseLine->endpoints[0]->node->node, CandidateLine.BaseLine->endpoints[1]->node->node, Candidate->node)
    2983                   && (fabs(NewNormalVector.NormSquared()) > HULLEPSILON)
    2984                   ) {
    2985                     Log() << Verbose(1) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl;
    2986                     radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(&NewPlaneCenter);
    2987                     Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl;
    2988                     Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl;
    2989                     Log() << Verbose(1) << "INFO: Radius of CircumCenterCircle is " << radius << "." << endl;
    2990                     if (radius < RADIUS*RADIUS) {
    2991                       otherradius = CandidateLine.BaseLine->endpoints[1]->node->node->DistanceSquared(&NewPlaneCenter);
    2992                       if (fabs(radius - otherradius) > HULLEPSILON) {
    2993                         eLog() << Verbose(1) << "Distance to center of circumcircle is not the same from each corner of the triangle: " << fabs(radius-otherradius) << endl;
    2994                       }
    2995                       // construct both new centers
    2996                       NewSphereCenter.CopyVector(&NewPlaneCenter);
    2997                       OtherNewSphereCenter.CopyVector(&NewPlaneCenter);
    2998                       helper.CopyVector(&NewNormalVector);
    2999                       helper.Scale(sqrt(RADIUS*RADIUS - radius));
    3000                       Log() << Verbose(2) << "INFO: Distance of NewPlaneCenter " << NewPlaneCenter << " to either NewSphereCenter is " << helper.Norm() << " of vector " << helper << " with sphere radius " << RADIUS << "." << endl;
    3001                       NewSphereCenter.AddVector(&helper);
    3002                       Log() << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl;
    3003                       // OtherNewSphereCenter is created by the same vector just in the other direction
    3004                       helper.Scale(-1.);
    3005                       OtherNewSphereCenter.AddVector(&helper);
    3006                       Log() << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl;
    3007 
    3008                       alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
    3009                       Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
    3010                       alpha = min(alpha, Otheralpha);
    3011 
    3012                       // if there is a better candidate, drop the current list and add the new candidate
    3013                       // otherwise ignore the new candidate and keep the list
    3014                       if (CandidateLine.ShortestAngle > (alpha - HULLEPSILON)) {
    3015                         if (fabs(alpha - Otheralpha) > MYEPSILON) {
    3016                           CandidateLine.OptCenter.CopyVector(&NewSphereCenter);
    3017                           CandidateLine.OtherOptCenter.CopyVector(&OtherNewSphereCenter);
     3240                  DoLog(1) && (Log() << Verbose(1) << "INFO: NewPlaneCenter is " << NewPlaneCenter << "." << endl);
     3241
     3242                  try {
     3243                    NewNormalVector = Plane(*(CandidateLine.BaseLine->endpoints[0]->node->node),
     3244                                            *(CandidateLine.BaseLine->endpoints[1]->node->node),
     3245                                            *(Candidate->node)).getNormal();
     3246                    DoLog(1) && (Log() << Verbose(1) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl);
     3247                    radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(NewPlaneCenter);
     3248                    DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl);
     3249                    DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl);
     3250                    DoLog(1) && (Log() << Verbose(1) << "INFO: Radius of CircumCenterCircle is " << radius << "." << endl);
     3251                    if (radius < RADIUS * RADIUS) {
     3252                      otherradius = CandidateLine.BaseLine->endpoints[1]->node->node->DistanceSquared(NewPlaneCenter);
     3253                      if (fabs(radius - otherradius) < HULLEPSILON) {
     3254                        // construct both new centers
     3255                        NewSphereCenter = NewPlaneCenter;
     3256                        OtherNewSphereCenter= NewPlaneCenter;
     3257                        helper = NewNormalVector;
     3258                        helper.Scale(sqrt(RADIUS * RADIUS - radius));
     3259                        DoLog(2) && (Log() << Verbose(2) << "INFO: Distance of NewPlaneCenter " << NewPlaneCenter << " to either NewSphereCenter is " << helper.Norm() << " of vector " << helper << " with sphere radius " << RADIUS << "." << endl);
     3260                        NewSphereCenter += helper;
     3261                        DoLog(2) && (Log() << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl);
     3262                        // OtherNewSphereCenter is created by the same vector just in the other direction
     3263                        helper.Scale(-1.);
     3264                        OtherNewSphereCenter += helper;
     3265                        DoLog(2) && (Log() << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl);
     3266                        alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
     3267                        Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection);
     3268                        if ((ThirdPoint != NULL) && (Candidate == ThirdPoint->node)) { // in that case only the other circlecenter is valid
     3269                          if (OldSphereCenter.DistanceSquared(NewSphereCenter) < OldSphereCenter.DistanceSquared(OtherNewSphereCenter))
     3270                            alpha = Otheralpha;
     3271                        } else
     3272                          alpha = min(alpha, Otheralpha);
     3273                        // if there is a better candidate, drop the current list and add the new candidate
     3274                        // otherwise ignore the new candidate and keep the list
     3275                        if (CandidateLine.ShortestAngle > (alpha - HULLEPSILON)) {
     3276                          if (fabs(alpha - Otheralpha) > MYEPSILON) {
     3277                            CandidateLine.OptCenter = NewSphereCenter;
     3278                            CandidateLine.OtherOptCenter = OtherNewSphereCenter;
     3279                          } else {
     3280                            CandidateLine.OptCenter = OtherNewSphereCenter;
     3281                            CandidateLine.OtherOptCenter = NewSphereCenter;
     3282                          }
     3283                          // if there is an equal candidate, add it to the list without clearing the list
     3284                          if ((CandidateLine.ShortestAngle - HULLEPSILON) < alpha) {
     3285                            CandidateLine.pointlist.push_back(Candidate);
     3286                            DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found an equally good candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl);
     3287                          } else {
     3288                            // remove all candidates from the list and then the list itself
     3289                            CandidateLine.pointlist.clear();
     3290                            CandidateLine.pointlist.push_back(Candidate);
     3291                            DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found a better candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl);
     3292                          }
     3293                          CandidateLine.ShortestAngle = alpha;
     3294                          DoLog(0) && (Log() << Verbose(0) << "INFO: There are " << CandidateLine.pointlist.size() << " candidates in the list now." << endl);
    30183295                        } else {
    3019                           CandidateLine.OptCenter.CopyVector(&OtherNewSphereCenter);
    3020                           CandidateLine.OtherOptCenter.CopyVector(&NewSphereCenter);
     3296                          if ((Candidate != NULL) && (CandidateLine.pointlist.begin() != CandidateLine.pointlist.end())) {
     3297                            DoLog(1) && (Log() << Verbose(1) << "REJECT: Old candidate " << *(*CandidateLine.pointlist.begin()) << " with " << CandidateLine.ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl);
     3298                          } else {
     3299                            DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl);
     3300                          }
    30213301                        }
    3022                         // if there is an equal candidate, add it to the list without clearing the list
    3023                         if ((CandidateLine.ShortestAngle - HULLEPSILON) < alpha) {
    3024                           CandidateLine.pointlist.push_back(Candidate);
    3025                           Log() << Verbose(0) << "ACCEPT: We have found an equally good candidate: " << *(Candidate) << " with "
    3026                             << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl;
    3027                         } else {
    3028                           // remove all candidates from the list and then the list itself
    3029                           CandidateLine.pointlist.clear();
    3030                           CandidateLine.pointlist.push_back(Candidate);
    3031                           Log() << Verbose(0) << "ACCEPT: We have found a better candidate: " << *(Candidate) << " with "
    3032                             << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl;
    3033                         }
    3034                         CandidateLine.ShortestAngle = alpha;
    3035                         Log() << Verbose(0) << "INFO: There are " << CandidateLine.pointlist.size() << " candidates in the list now." << endl;
    30363302                      } else {
    3037                         if ((Candidate != NULL) && (CandidateLine.pointlist.begin() != CandidateLine.pointlist.end())) {
    3038                           Log() << Verbose(1) << "REJECT: Old candidate " << *(Candidate) << " with " << CandidateLine.ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl;
    3039                         } else {
    3040                           Log() << Verbose(1) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl;
    3041                         }
     3303                        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);
    30423304                      }
    30433305                    } else {
    3044                       Log() << Verbose(1) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl;
     3306                      DoLog(1) && (Log() << Verbose(1) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl);
    30453307                    }
    3046                   } else {
     3308                  }
     3309                  catch (LinearDependenceException &excp){
     3310                    Log() << Verbose(1) << excp;
    30473311                    Log() << Verbose(1) << "REJECT: Three points from " << *CandidateLine.BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl;
    30483312                  }
    30493313                } else {
    3050                   if (ThirdNode != NULL) {
    3051                     Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " and " << *ThirdNode << " contains Candidate " << *Candidate << "." << endl;
     3314                  if (ThirdPoint != NULL) {
     3315                    DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " and " << *ThirdPoint << " contains Candidate " << *Candidate << "." << endl);
    30523316                  } else {
    3053                     Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " contains Candidate " << *Candidate << "." << endl;
     3317                    DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " contains Candidate " << *Candidate << "." << endl);
    30543318                  }
    30553319                }
     
    30583322          }
    30593323    } else {
    3060       eLog() << Verbose(1) << "The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl;
     3324      DoeLog(1) && (eLog() << Verbose(1) << "The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl);
    30613325    }
    30623326  } else {
    3063     if (ThirdNode != NULL)
    3064       Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and third node " << *ThirdNode << " is too big!" << endl;
     3327    if (ThirdPoint != NULL)
     3328      DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and third node " << *ThirdPoint << " is too big!" << endl);
    30653329    else
    3066       Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " is too big!" << endl;
    3067   }
    3068 
    3069   Log() << Verbose(1) << "INFO: Sorting candidate list ..." << endl;
     3330      DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " is too big!" << endl);
     3331  }
     3332
     3333  DoLog(1) && (Log() << Verbose(1) << "INFO: Sorting candidate list ..." << endl);
    30703334  if (CandidateLine.pointlist.size() > 1) {
    30713335    CandidateLine.pointlist.unique();
    30723336    CandidateLine.pointlist.sort(); //SortCandidates);
    30733337  }
    3074 };
     3338
     3339  if ((!CandidateLine.pointlist.empty()) && (!CandidateLine.CheckValidity(RADIUS, LC))) {
     3340    DoeLog(0) && (eLog() << Verbose(0) << "There were other points contained in the rolling sphere as well!" << endl);
     3341    performCriticalExit();
     3342  }
     3343}
     3344;
    30753345
    30763346/** Finds the endpoint two lines are sharing.
     
    30813351class BoundaryPointSet *Tesselation::GetCommonEndpoint(const BoundaryLineSet * line1, const BoundaryLineSet * line2) const
    30823352{
    3083         Info FunctionInfo(__func__);
     3353  Info FunctionInfo(__func__);
    30843354  const BoundaryLineSet * lines[2] = { line1, line2 };
    30853355  class BoundaryPointSet *node = NULL;
     
    30883358  for (int i = 0; i < 2; i++)
    30893359    // for both lines
    3090     for (int j = 0; j < 2; j++)
    3091       { // for both endpoints
    3092         OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (
    3093             lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
    3094         if (!OrderTest.second)
    3095           { // if insertion fails, we have common endpoint
    3096             node = OrderTest.first->second;
    3097             Log() << Verbose(1) << "Common endpoint of lines " << *line1
    3098                 << " and " << *line2 << " is: " << *node << "." << endl;
    3099             j = 2;
    3100             i = 2;
    3101             break;
    3102           }
     3360    for (int j = 0; j < 2; j++) { // for both endpoints
     3361      OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j]));
     3362      if (!OrderTest.second) { // if insertion fails, we have common endpoint
     3363        node = OrderTest.first->second;
     3364        DoLog(1) && (Log() << Verbose(1) << "Common endpoint of lines " << *line1 << " and " << *line2 << " is: " << *node << "." << endl);
     3365        j = 2;
     3366        i = 2;
     3367        break;
    31033368      }
     3369    }
    31043370  return node;
    3105 };
     3371}
     3372;
    31063373
    31073374/** Finds the boundary points that are closest to a given Vector \a *x.
     
    31173384
    31183385  if (LinesOnBoundary.empty()) {
    3119     eLog() << Verbose(1) << "There is no tesselation structure to compare the point with, please create one first." << endl;
     3386    DoeLog(1) && (eLog() << Verbose(1) << "There is no tesselation structure to compare the point with, please create one first." << endl);
    31203387    return NULL;
    31213388  }
     
    31233390  // gather all points close to the desired one
    31243391  LC->SetIndexToVector(x); // ignore status as we calculate bounds below sensibly
    3125   for(int i=0;i<NDIM;i++) // store indices of this cell
     3392  for (int i = 0; i < NDIM; i++) // store indices of this cell
    31263393    N[i] = LC->n[i];
    3127   Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl;
    3128 
     3394  DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
    31293395  DistanceToPointMap * points = new DistanceToPointMap;
    31303396  LC->GetNeighbourBounds(Nlower, Nupper);
     
    31333399    for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
    31343400      for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
    3135         const LinkedNodes *List = LC->GetCurrentCell();
     3401        const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
    31363402        //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
    31373403        if (List != NULL) {
    3138           for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
     3404          for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
    31393405            FindPoint = PointsOnBoundary.find((*Runner)->nr);
    31403406            if (FindPoint != PointsOnBoundary.end()) {
    3141               points->insert(DistanceToPointPair (FindPoint->second->node->node->DistanceSquared(x), FindPoint->second) );
    3142               Log() << Verbose(1) << "INFO: Putting " << *FindPoint->second << " into the list." << endl;
     3407              points->insert(DistanceToPointPair(FindPoint->second->node->node->DistanceSquared(*x), FindPoint->second));
     3408              DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *FindPoint->second << " into the list." << endl);
    31433409            }
    31443410          }
    31453411        } else {
    3146           eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl;
     3412          DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
    31473413        }
    31483414      }
     
    31503416  // check whether we found some points
    31513417  if (points->empty()) {
    3152     eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl;
    3153     delete(points);
     3418    DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
     3419    delete (points);
    31543420    return NULL;
    31553421  }
    31563422  return points;
    3157 };
     3423}
     3424;
    31583425
    31593426/** Finds the boundary line that is closest to a given Vector \a *x.
     
    31653432{
    31663433  Info FunctionInfo(__func__);
    3167 
    31683434  // get closest points
    3169   DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x,LC);
     3435  DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC);
    31703436  if (points == NULL) {
    3171     eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl;
     3437    DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
    31723438    return NULL;
    31733439  }
    31743440
    31753441  // for each point, check its lines, remember closest
    3176   Log() << Verbose(1) << "Finding closest BoundaryLine to " << *x << " ... " << endl;
     3442  DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryLine to " << *x << " ... " << endl);
    31773443  BoundaryLineSet *ClosestLine = NULL;
    31783444  double MinDistance = -1.;
     
    31833449    for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) {
    31843450      // calculate closest point on line to desired point
    3185       helper.CopyVector((LineRunner->second)->endpoints[0]->node->node);
    3186       helper.AddVector((LineRunner->second)->endpoints[1]->node->node);
    3187       helper.Scale(0.5);
    3188       Center.CopyVector(x);
    3189       Center.SubtractVector(&helper);
    3190       BaseLine.CopyVector((LineRunner->second)->endpoints[0]->node->node);
    3191       BaseLine.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3192       Center.ProjectOntoPlane(&BaseLine);
     3451      helper = 0.5 * ((*(LineRunner->second)->endpoints[0]->node->node) +
     3452                      (*(LineRunner->second)->endpoints[1]->node->node));
     3453      Center = (*x) - helper;
     3454      BaseLine = (*(LineRunner->second)->endpoints[0]->node->node) -
     3455                 (*(LineRunner->second)->endpoints[1]->node->node);
     3456      Center.ProjectOntoPlane(BaseLine);
    31933457      const double distance = Center.NormSquared();
    31943458      if ((ClosestLine == NULL) || (distance < MinDistance)) {
    31953459        // additionally calculate intersection on line (whether it's on the line section or not)
    3196         helper.CopyVector(x);
    3197         helper.SubtractVector((LineRunner->second)->endpoints[0]->node->node);
    3198         helper.SubtractVector(&Center);
    3199         const double lengthA = helper.ScalarProduct(&BaseLine);
    3200         helper.CopyVector(x);
    3201         helper.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3202         helper.SubtractVector(&Center);
    3203         const double lengthB = helper.ScalarProduct(&BaseLine);
    3204         if (lengthB*lengthA < 0) {  // if have different sign
     3460        helper = (*x) - (*(LineRunner->second)->endpoints[0]->node->node) - Center;
     3461        const double lengthA = helper.ScalarProduct(BaseLine);
     3462        helper = (*x) - (*(LineRunner->second)->endpoints[1]->node->node) - Center;
     3463        const double lengthB = helper.ScalarProduct(BaseLine);
     3464        if (lengthB * lengthA < 0) { // if have different sign
    32053465          ClosestLine = LineRunner->second;
    32063466          MinDistance = distance;
    3207           Log() << Verbose(1) << "ACCEPT: New closest line is " << *ClosestLine << " with projected distance " << MinDistance << "." << endl;
     3467          DoLog(1) && (Log() << Verbose(1) << "ACCEPT: New closest line is " << *ClosestLine << " with projected distance " << MinDistance << "." << endl);
    32083468        } else {
    3209           Log() << Verbose(1) << "REJECT: Intersection is outside of the line section: " << lengthA << " and " << lengthB << "." << endl;
     3469          DoLog(1) && (Log() << Verbose(1) << "REJECT: Intersection is outside of the line section: " << lengthA << " and " << lengthB << "." << endl);
    32103470        }
    32113471      } else {
    3212         Log() << Verbose(1) << "REJECT: Point is too further away than present line: " << distance << " >> " << MinDistance << "." << endl;
     3472        DoLog(1) && (Log() << Verbose(1) << "REJECT: Point is too further away than present line: " << distance << " >> " << MinDistance << "." << endl);
    32133473      }
    32143474    }
    32153475  }
    3216   delete(points);
     3476  delete (points);
    32173477  // check whether closest line is "too close" :), then it's inside
    32183478  if (ClosestLine == NULL) {
    3219     Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl;
     3479    DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl);
    32203480    return NULL;
    32213481  }
    32223482  return ClosestLine;
    3223 };
    3224 
     3483}
     3484;
    32253485
    32263486/** Finds the triangle that is closest to a given Vector \a *x.
     
    32313491TriangleList * Tesselation::FindClosestTrianglesToVector(const Vector *x, const LinkedCell* LC) const
    32323492{
    3233         Info FunctionInfo(__func__);
    3234 
    3235         // get closest points
    3236         DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x,LC);
     3493  Info FunctionInfo(__func__);
     3494  // get closest points
     3495  DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC);
    32373496  if (points == NULL) {
    3238     eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl;
     3497    DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl);
    32393498    return NULL;
    32403499  }
    32413500
    32423501  // for each point, check its lines, remember closest
    3243   Log() << Verbose(1) << "Finding closest BoundaryTriangle to " << *x << " ... " << endl;
     3502  DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryTriangle to " << *x << " ... " << endl);
    32443503  LineSet ClosestLines;
    32453504  double MinDistance = 1e+16;
     
    32513510    for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) {
    32523511
    3253       BaseLine.CopyVector((LineRunner->second)->endpoints[0]->node->node);
    3254       BaseLine.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
     3512      BaseLine = (*(LineRunner->second)->endpoints[0]->node->node) -
     3513                 (*(LineRunner->second)->endpoints[1]->node->node);
    32553514      const double lengthBase = BaseLine.NormSquared();
    32563515
    3257       BaseLineIntersection.CopyVector(x);
    3258       BaseLineIntersection.SubtractVector((LineRunner->second)->endpoints[0]->node->node);
     3516      BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[0]->node->node);
    32593517      const double lengthEndA = BaseLineIntersection.NormSquared();
    32603518
    3261       BaseLineIntersection.CopyVector(x);
    3262       BaseLineIntersection.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
     3519      BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[1]->node->node);
    32633520      const double lengthEndB = BaseLineIntersection.NormSquared();
    32643521
    3265       if ((lengthEndA > lengthBase) || (lengthEndB > lengthBase) || ((lengthEndA < MYEPSILON) || (lengthEndB < MYEPSILON))) {  // intersection would be outside, take closer endpoint
     3522      if ((lengthEndA > lengthBase) || (lengthEndB > lengthBase) || ((lengthEndA < MYEPSILON) || (lengthEndB < MYEPSILON))) { // intersection would be outside, take closer endpoint
    32663523        const double lengthEnd = Min(lengthEndA, lengthEndB);
    32673524        if (lengthEnd - MinDistance < -MYEPSILON) { // new best line
     
    32693526          ClosestLines.insert(LineRunner->second);
    32703527          MinDistance = lengthEnd;
    3271           Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[0]->node << " is closer with " << lengthEnd << "." << endl;
    3272         } else if  (fabs(lengthEnd - MinDistance) < MYEPSILON) { // additional best candidate
     3528          DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[0]->node << " is closer with " << lengthEnd << "." << endl);
     3529        } else if (fabs(lengthEnd - MinDistance) < MYEPSILON) { // additional best candidate
    32733530          ClosestLines.insert(LineRunner->second);
    3274           Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[1]->node << " is equally good with " << lengthEnd << "." << endl;
     3531          DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[1]->node << " is equally good with " << lengthEnd << "." << endl);
    32753532        } else { // line is worse
    3276           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;
     3533          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);
    32773534        }
    32783535      } else { // intersection is closer, calculate
    32793536        // calculate closest point on line to desired point
    3280         BaseLineIntersection.CopyVector(x);
    3281         BaseLineIntersection.SubtractVector((LineRunner->second)->endpoints[1]->node->node);
    3282         Center.CopyVector(&BaseLineIntersection);
    3283         Center.ProjectOntoPlane(&BaseLine);
    3284         BaseLineIntersection.SubtractVector(&Center);
     3537        BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[1]->node->node);
     3538        Center = BaseLineIntersection;
     3539        Center.ProjectOntoPlane(BaseLine);
     3540        BaseLineIntersection -= Center;
    32853541        const double distance = BaseLineIntersection.NormSquared();
    32863542        if (Center.NormSquared() > BaseLine.NormSquared()) {
    3287           eLog() << Verbose(0) << "Algorithmic error: In second case we have intersection outside of baseline!" << endl;
     3543          DoeLog(0) && (eLog() << Verbose(0) << "Algorithmic error: In second case we have intersection outside of baseline!" << endl);
    32883544        }
    32893545        if ((ClosestLines.empty()) || (distance < MinDistance)) {
    32903546          ClosestLines.insert(LineRunner->second);
    32913547          MinDistance = distance;
    3292           Log() << Verbose(1) << "ACCEPT: Intersection in between endpoints, new closest line " << *LineRunner->second << " is " << *ClosestLines.begin() << " with projected distance " << MinDistance << "." << endl;
     3548          DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Intersection in between endpoints, new closest line " << *LineRunner->second << " is " << *ClosestLines.begin() << " with projected distance " << MinDistance << "." << endl);
    32933549        } else {
    3294           Log() << Verbose(2) << "REJECT: Point is further away from line " << *LineRunner->second << " than present closest line: " << distance << " >> " << MinDistance << "." << endl;
     3550          DoLog(2) && (Log() << Verbose(2) << "REJECT: Point is further away from line " << *LineRunner->second << " than present closest line: " << distance << " >> " << MinDistance << "." << endl);
    32953551        }
    32963552      }
    32973553    }
    32983554  }
    3299   delete(points);
     3555  delete (points);
    33003556
    33013557  // check whether closest line is "too close" :), then it's inside
    33023558  if (ClosestLines.empty()) {
    3303     Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl;
     3559    DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl);
    33043560    return NULL;
    33053561  }
     
    33073563  for (LineSet::iterator LineRunner = ClosestLines.begin(); LineRunner != ClosestLines.end(); LineRunner++)
    33083564    for (TriangleMap::iterator Runner = (*LineRunner)->triangles.begin(); Runner != (*LineRunner)->triangles.end(); Runner++) {
    3309     candidates->push_back(Runner->second);
    3310   }
     3565      candidates->push_back(Runner->second);
     3566    }
    33113567  return candidates;
    3312 };
     3568}
     3569;
    33133570
    33143571/** Finds closest triangle to a point.
     
    33163573 * \param *out output stream for debugging
    33173574 * \param *x Vector to look from
     3575 * \param &distance contains found distance on return
    33183576 * \return list of BoundaryTriangleSet of nearest triangles or NULL.
    33193577 */
    33203578class BoundaryTriangleSet * Tesselation::FindClosestTriangleToVector(const Vector *x, const LinkedCell* LC) const
    33213579{
    3322         Info FunctionInfo(__func__);
     3580  Info FunctionInfo(__func__);
    33233581  class BoundaryTriangleSet *result = NULL;
    33243582  TriangleList *triangles = FindClosestTrianglesToVector(x, LC);
     
    33313589
    33323590  // go through all and pick the one with the best alignment to x
    3333   double MinAlignment = 2.*M_PI;
     3591  double MinAlignment = 2. * M_PI;
    33343592  for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++) {
    33353593    (*Runner)->GetCenter(&Center);
    3336     helper.CopyVector(x);
    3337     helper.SubtractVector(&Center);
    3338     const double Alignment = helper.Angle(&(*Runner)->NormalVector);
     3594    helper = (*x) - Center;
     3595    const double Alignment = helper.Angle((*Runner)->NormalVector);
    33393596    if (Alignment < MinAlignment) {
    33403597      result = *Runner;
    33413598      MinAlignment = Alignment;
    3342       Log() << Verbose(1) << "ACCEPT: Triangle " << *result << " is better aligned with " << MinAlignment << "." << endl;
     3599      DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Triangle " << *result << " is better aligned with " << MinAlignment << "." << endl);
    33433600    } else {
    3344       Log() << Verbose(1) << "REJECT: Triangle " << *result << " is worse aligned with " << MinAlignment << "." << endl;
    3345     }
    3346   }
    3347   delete(triangles);
     3601      DoLog(1) && (Log() << Verbose(1) << "REJECT: Triangle " << *result << " is worse aligned with " << MinAlignment << "." << endl);
     3602    }
     3603  }
     3604  delete (triangles);
    33483605
    33493606  return result;
    3350 };
     3607}
     3608;
    33513609
    33523610/** Checks whether the provided Vector is within the Tesselation structure.
     
    33593617bool Tesselation::IsInnerPoint(const Vector &Point, const LinkedCell* const LC) const
    33603618{
    3361   return (GetDistanceSquaredToSurface(Point, LC) < MYEPSILON);
    3362 }
     3619  Info FunctionInfo(__func__);
     3620  TriangleIntersectionList Intersections(&Point, this, LC);
     3621
     3622  return Intersections.IsInside();
     3623}
     3624;
    33633625
    33643626/** Returns the distance to the surface given by the tesselation.
     
    33903652
    33913653  if (triangle == NULL) {// is boundary point or only point in point cloud?
    3392     Log() << Verbose(1) << "No triangle given!" << endl;
     3654    DoLog(1) && (Log() << Verbose(1) << "No triangle given!" << endl);
    33933655    return -1.;
    33943656  } else {
    3395     Log() << Verbose(1) << "INFO: Closest triangle found is " << *triangle << " with normal vector " << triangle->NormalVector << "." << endl;
     3657    DoLog(1) && (Log() << Verbose(1) << "INFO: Closest triangle found is " << *triangle << " with normal vector " << triangle->NormalVector << "." << endl);
    33963658  }
    33973659
    33983660  triangle->GetCenter(&Center);
    3399   Log() << Verbose(2) << "INFO: Central point of the triangle is " << Center << "." << endl;
    3400   DistanceToCenter.CopyVector(&Center);
    3401   DistanceToCenter.SubtractVector(&Point);
    3402   Log() << Verbose(2) << "INFO: Vector from point to test to center is " << DistanceToCenter << "." << endl;
     3661  DoLog(2) && (Log() << Verbose(2) << "INFO: Central point of the triangle is " << Center << "." << endl);
     3662  DistanceToCenter = Center - Point;
     3663  DoLog(2) && (Log() << Verbose(2) << "INFO: Vector from point to test to center is " << DistanceToCenter << "." << endl);
    34033664
    34043665  // check whether we are on boundary
    3405   if (fabs(DistanceToCenter.ScalarProduct(&triangle->NormalVector)) < MYEPSILON) {
     3666  if (fabs(DistanceToCenter.ScalarProduct(triangle->NormalVector)) < MYEPSILON) {
    34063667    // calculate whether inside of triangle
    3407     DistanceToCenter.CopyVector(&Point);
    3408     Center.CopyVector(&Point);
    3409     Center.SubtractVector(&triangle->NormalVector); // points towards MolCenter
    3410     DistanceToCenter.AddVector(&triangle->NormalVector); // points outside
    3411     Log() << Verbose(1) << "INFO: Calling Intersection with " << Center << " and " << DistanceToCenter << "." << endl;
     3668    DistanceToCenter = Point + triangle->NormalVector; // points outside
     3669    Center = Point - triangle->NormalVector; // points towards MolCenter
     3670    DoLog(1) && (Log() << Verbose(1) << "INFO: Calling Intersection with " << Center << " and " << DistanceToCenter << "." << endl);
    34123671    if (triangle->GetIntersectionInsideTriangle(&Center, &DistanceToCenter, &Intersection)) {
    3413       Log() << Verbose(1) << Point << " is inner point: sufficiently close to boundary, " << Intersection << "." << endl;
     3672      DoLog(1) && (Log() << Verbose(1) << Point << " is inner point: sufficiently close to boundary, " << Intersection << "." << endl);
    34143673      return 0.;
    34153674    } else {
    3416       Log() << Verbose(1) << Point << " is NOT an inner point: on triangle plane but outside of triangle bounds." << endl;
     3675      DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point: on triangle plane but outside of triangle bounds." << endl);
    34173676      return false;
    34183677    }
     
    34203679    // calculate smallest distance
    34213680    distance = triangle->GetClosestPointInsideTriangle(&Point, &Intersection);
    3422     Log() << Verbose(1) << "Closest point on triangle is " << Intersection << "." << endl;
     3681    DoLog(1) && (Log() << Verbose(1) << "Closest point on triangle is " << Intersection << "." << endl);
    34233682
    34243683    // then check direction to boundary
    3425     if (DistanceToCenter.ScalarProduct(&triangle->NormalVector) > MYEPSILON) {
    3426       Log() << Verbose(1) << Point << " is an inner point, " << distance << " below surface." << endl;
     3684    if (DistanceToCenter.ScalarProduct(triangle->NormalVector) > MYEPSILON) {
     3685      DoLog(1) && (Log() << Verbose(1) << Point << " is an inner point, " << distance << " below surface." << endl);
    34273686      return -distance;
    34283687    } else {
    3429       Log() << Verbose(1) << Point << " is NOT an inner point, " << distance << " above surface." << endl;
     3688      DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point, " << distance << " above surface." << endl);
    34303689      return +distance;
    34313690    }
    34323691  }
    3433 };
    3434 
    3435 /** Calculates distance to a tesselated surface.
     3692}
     3693;
     3694
     3695/** Calculates minimum distance from \a&Point to a tesselated surface.
    34363696 * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle().
    34373697 * \param &Point point to calculate distance from
     
    34393699 * \return distance squared to closest point on surface
    34403700 */
    3441 double Tesselation::GetDistanceSquaredToSurface(const Vector &Point, const LinkedCell* const LC) const
    3442 {
    3443   BoundaryTriangleSet *triangle = FindClosestTriangleToVector(&Point, LC);
    3444   const double distance = GetDistanceSquaredToTriangle(Point, triangle);
    3445   return Min(distance, LC->RADIUS);
    3446 };
     3701double Tesselation::GetDistanceToSurface(const Vector &Point, const LinkedCell* const LC) const
     3702{
     3703  Info FunctionInfo(__func__);
     3704  TriangleIntersectionList Intersections(&Point, this, LC);
     3705
     3706  return Intersections.GetSmallestDistance();
     3707}
     3708;
     3709
     3710/** Calculates minimum distance from \a&Point to a tesselated surface.
     3711 * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle().
     3712 * \param &Point point to calculate distance from
     3713 * \param *LC needed for finding closest points fast
     3714 * \return distance squared to closest point on surface
     3715 */
     3716BoundaryTriangleSet * Tesselation::GetClosestTriangleOnSurface(const Vector &Point, const LinkedCell* const LC) const
     3717{
     3718  Info FunctionInfo(__func__);
     3719  TriangleIntersectionList Intersections(&Point, this, LC);
     3720
     3721  return Intersections.GetClosestTriangle();
     3722}
     3723;
    34473724
    34483725/** Gets all points connected to the provided point by triangulation lines.
     
    34543731TesselPointSet * Tesselation::GetAllConnectedPoints(const TesselPoint* const Point) const
    34553732{
    3456         Info FunctionInfo(__func__);
    3457         TesselPointSet *connectedPoints = new TesselPointSet;
     3733  Info FunctionInfo(__func__);
     3734  TesselPointSet *connectedPoints = new TesselPointSet;
    34583735  class BoundaryPointSet *ReferencePoint = NULL;
    34593736  TesselPoint* current;
    34603737  bool takePoint = false;
    3461 
    34623738  // find the respective boundary point
    34633739  PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
     
    34653741    ReferencePoint = PointRunner->second;
    34663742  } else {
    3467     eLog() << Verbose(2) << "GetAllConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl;
     3743    DoeLog(2) && (eLog() << Verbose(2) << "GetAllConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl);
    34683744    ReferencePoint = NULL;
    34693745  }
     
    34713747  // little trick so that we look just through lines connect to the BoundaryPoint
    34723748  // OR fall-back to look through all lines if there is no such BoundaryPoint
    3473   const LineMap *Lines;;
     3749  const LineMap *Lines;
     3750  ;
    34743751  if (ReferencePoint != NULL)
    34753752    Lines = &(ReferencePoint->lines);
     
    34783755  LineMap::const_iterator findLines = Lines->begin();
    34793756  while (findLines != Lines->end()) {
    3480    takePoint = false;
    3481 
    3482    if (findLines->second->endpoints[0]->Nr == Point->nr) {
    3483      takePoint = true;
    3484      current = findLines->second->endpoints[1]->node;
    3485    } else if (findLines->second->endpoints[1]->Nr == Point->nr) {
    3486      takePoint = true;
    3487      current = findLines->second->endpoints[0]->node;
    3488    }
    3489 
    3490    if (takePoint) {
    3491      Log() << Verbose(1) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is enlisted." << endl;
    3492      connectedPoints->insert(current);
    3493    }
    3494 
    3495    findLines++;
     3757    takePoint = false;
     3758
     3759    if (findLines->second->endpoints[0]->Nr == Point->nr) {
     3760      takePoint = true;
     3761      current = findLines->second->endpoints[1]->node;
     3762    } else if (findLines->second->endpoints[1]->Nr == Point->nr) {
     3763      takePoint = true;
     3764      current = findLines->second->endpoints[0]->node;
     3765    }
     3766
     3767    if (takePoint) {
     3768      DoLog(1) && (Log() << Verbose(1) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is enlisted." << endl);
     3769      connectedPoints->insert(current);
     3770    }
     3771
     3772    findLines++;
    34963773  }
    34973774
    34983775  if (connectedPoints->empty()) { // if have not found any points
    3499     eLog() << Verbose(1) << "We have not found any connected points to " << *Point<< "." << endl;
     3776    DoeLog(1) && (eLog() << Verbose(1) << "We have not found any connected points to " << *Point << "." << endl);
    35003777    return NULL;
    35013778  }
    35023779
    35033780  return connectedPoints;
    3504 };
    3505 
     3781}
     3782;
    35063783
    35073784/** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point.
     
    35193796TesselPointList * Tesselation::GetCircleOfConnectedTriangles(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const
    35203797{
    3521         Info FunctionInfo(__func__);
     3798  Info FunctionInfo(__func__);
    35223799  map<double, TesselPoint*> anglesOfPoints;
    35233800  TesselPointList *connectedCircle = new TesselPointList;
     
    35263803  Vector OrthogonalVector;
    35273804  Vector helper;
    3528   const TesselPoint * const TrianglePoints[3] = {Point, NULL, NULL};
     3805  const TesselPoint * const TrianglePoints[3] = { Point, NULL, NULL };
    35293806  TriangleList *triangles = NULL;
    35303807
    35313808  if (SetOfNeighbours == NULL) {
    3532     eLog() << Verbose(2) << "Could not find any connected points!" << endl;
    3533     delete(connectedCircle);
     3809    DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl);
     3810    delete (connectedCircle);
    35343811    return NULL;
    35353812  }
     
    35393816  if ((triangles != NULL) && (!triangles->empty())) {
    35403817    for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++)
    3541       PlaneNormal.AddVector(&(*Runner)->NormalVector);
     3818      PlaneNormal += (*Runner)->NormalVector;
    35423819  } else {
    3543     eLog() << Verbose(0) << "Could not find any triangles for point " << *Point << "." << endl;
     3820    DoeLog(0) && (eLog() << Verbose(0) << "Could not find any triangles for point " << *Point << "." << endl);
    35443821    performCriticalExit();
    35453822  }
    3546   PlaneNormal.Scale(1.0/triangles->size());
    3547   Log() << Verbose(1) << "INFO: Calculated PlaneNormal of all circle points is " << PlaneNormal << "." << endl;
     3823  PlaneNormal.Scale(1.0 / triangles->size());
     3824  DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated PlaneNormal of all circle points is " << PlaneNormal << "." << endl);
    35483825  PlaneNormal.Normalize();
    35493826
    35503827  // construct one orthogonal vector
    35513828  if (Reference != NULL) {
    3552     AngleZero.CopyVector(Reference);
    3553     AngleZero.SubtractVector(Point->node);
    3554     AngleZero.ProjectOntoPlane(&PlaneNormal);
    3555   }
    3556   if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON )) {
    3557     Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl;
    3558     AngleZero.CopyVector((*SetOfNeighbours->begin())->node);
    3559     AngleZero.SubtractVector(Point->node);
    3560     AngleZero.ProjectOntoPlane(&PlaneNormal);
     3829    AngleZero = (*Reference) - (*Point->node);
     3830    AngleZero.ProjectOntoPlane(PlaneNormal);
     3831  }
     3832  if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON)) {
     3833    DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl);
     3834    AngleZero = (*(*SetOfNeighbours->begin())->node) - (*Point->node);
     3835    AngleZero.ProjectOntoPlane(PlaneNormal);
    35613836    if (AngleZero.NormSquared() < MYEPSILON) {
    3562       eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl;
     3837      DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl);
    35633838      performCriticalExit();
    35643839    }
    35653840  }
    3566   Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl;
     3841  DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl);
    35673842  if (AngleZero.NormSquared() > MYEPSILON)
    3568     OrthogonalVector.MakeNormalVector(&PlaneNormal, &AngleZero);
     3843    OrthogonalVector = Plane(PlaneNormal, AngleZero,0).getNormal();
    35693844  else
    3570     OrthogonalVector.MakeNormalVector(&PlaneNormal);
    3571   Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl;
     3845    OrthogonalVector.MakeNormalTo(PlaneNormal);
     3846  DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl);
    35723847
    35733848  // go through all connected points and calculate angle
    35743849  for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) {
    3575     helper.CopyVector((*listRunner)->node);
    3576     helper.SubtractVector(Point->node);
    3577     helper.ProjectOntoPlane(&PlaneNormal);
     3850    helper = (*(*listRunner)->node) - (*Point->node);
     3851    helper.ProjectOntoPlane(PlaneNormal);
    35783852    double angle = GetAngle(helper, AngleZero, OrthogonalVector);
    3579     Log() << Verbose(0) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl;
    3580     anglesOfPoints.insert(pair<double, TesselPoint*>(angle, (*listRunner)));
    3581   }
    3582 
    3583   for(map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
     3853    DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl);
     3854    anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner)));
     3855  }
     3856
     3857  for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
    35843858    connectedCircle->push_back(AngleRunner->second);
    35853859  }
     
    36113885
    36123886  if (SetOfNeighbours == NULL) {
    3613     eLog() << Verbose(2) << "Could not find any connected points!" << endl;
    3614     delete(connectedCircle);
     3887    DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl);
     3888    delete (connectedCircle);
    36153889    return NULL;
    36163890  }
     
    36233897  }
    36243898
    3625   Log() << Verbose(1) << "INFO: Point is " << *Point << " and Reference is " << *Reference << "." << endl;
     3899  DoLog(1) && (Log() << Verbose(1) << "INFO: Point is " << *Point << " and Reference is " << *Reference << "." << endl);
    36263900  // calculate central point
    3627 
    36283901  TesselPointSet::const_iterator TesselA = SetOfNeighbours->begin();
    36293902  TesselPointSet::const_iterator TesselB = SetOfNeighbours->begin();
     
    36343907  int counter = 0;
    36353908  while (TesselC != SetOfNeighbours->end()) {
    3636     helper.MakeNormalVector((*TesselA)->node, (*TesselB)->node, (*TesselC)->node);
    3637     Log() << Verbose(0) << "Making normal vector out of " << *(*TesselA) << ", " << *(*TesselB) << " and " << *(*TesselC) << ":" << helper << endl;
     3909    helper = Plane(*((*TesselA)->node),
     3910                   *((*TesselB)->node),
     3911                   *((*TesselC)->node)).getNormal();
     3912    DoLog(0) && (Log() << Verbose(0) << "Making normal vector out of " << *(*TesselA) << ", " << *(*TesselB) << " and " << *(*TesselC) << ":" << helper << endl);
    36383913    counter++;
    36393914    TesselA++;
    36403915    TesselB++;
    36413916    TesselC++;
    3642     PlaneNormal.AddVector(&helper);
     3917    PlaneNormal += helper;
    36433918  }
    36443919  //Log() << Verbose(0) << "Summed vectors " << center << "; number of points " << connectedPoints.size()
    36453920  //  << "; scale factor " << counter;
    3646   PlaneNormal.Scale(1.0/(double)counter);
    3647 //  Log() << Verbose(1) << "INFO: Calculated center of all circle points is " << center << "." << endl;
    3648 //
    3649 //  // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points
    3650 //  PlaneNormal.CopyVector(Point->node);
    3651 //  PlaneNormal.SubtractVector(&center);
    3652 //  PlaneNormal.Normalize();
    3653   Log() << Verbose(1) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl;
     3921  PlaneNormal.Scale(1.0 / (double) counter);
     3922  //  Log() << Verbose(1) << "INFO: Calculated center of all circle points is " << center << "." << endl;
     3923  //
     3924  //  // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points
     3925  //  PlaneNormal.CopyVector(Point->node);
     3926  //  PlaneNormal.SubtractVector(&center);
     3927  //  PlaneNormal.Normalize();
     3928  DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl);
    36543929
    36553930  // construct one orthogonal vector
    36563931  if (Reference != NULL) {
    3657     AngleZero.CopyVector(Reference);
    3658     AngleZero.SubtractVector(Point->node);
    3659     AngleZero.ProjectOntoPlane(&PlaneNormal);
     3932    AngleZero = (*Reference) - (*Point->node);
     3933    AngleZero.ProjectOntoPlane(PlaneNormal);
    36603934  }
    36613935  if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON )) {
    3662     Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl;
    3663     AngleZero.CopyVector((*SetOfNeighbours->begin())->node);
    3664     AngleZero.SubtractVector(Point->node);
    3665     AngleZero.ProjectOntoPlane(&PlaneNormal);
     3936    DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl);
     3937    AngleZero = (*(*SetOfNeighbours->begin())->node) - (*Point->node);
     3938    AngleZero.ProjectOntoPlane(PlaneNormal);
    36663939    if (AngleZero.NormSquared() < MYEPSILON) {
    3667       eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl;
     3940      DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl);
    36683941      performCriticalExit();
    36693942    }
    36703943  }
    3671   Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl;
     3944  DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl);
    36723945  if (AngleZero.NormSquared() > MYEPSILON)
    3673     OrthogonalVector.MakeNormalVector(&PlaneNormal, &AngleZero);
     3946    OrthogonalVector = Plane(PlaneNormal, AngleZero,0).getNormal();
    36743947  else
    3675     OrthogonalVector.MakeNormalVector(&PlaneNormal);
    3676   Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl;
     3948    OrthogonalVector.MakeNormalTo(PlaneNormal);
     3949  DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl);
    36773950
    36783951  // go through all connected points and calculate angle
    3679   pair <map<double, TesselPoint*>::iterator, bool > InserterTest;
     3952  pair<map<double, TesselPoint*>::iterator, bool> InserterTest;
    36803953  for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) {
    3681     helper.CopyVector((*listRunner)->node);
    3682     helper.SubtractVector(Point->node);
    3683     helper.ProjectOntoPlane(&PlaneNormal);
     3954    helper = (*(*listRunner)->node) - (*Point->node);
     3955    helper.ProjectOntoPlane(PlaneNormal);
    36843956    double angle = GetAngle(helper, AngleZero, OrthogonalVector);
    36853957    if (angle > M_PI) // the correction is of no use here (and not desired)
    3686       angle = 2.*M_PI - angle;
    3687     Log() << Verbose(0) << "INFO: Calculated angle between " << helper << " and " << AngleZero << " is " << angle << " for point " << **listRunner << "." << endl;
    3688     InserterTest = anglesOfPoints.insert(pair<double, TesselPoint*>(angle, (*listRunner)));
     3958      angle = 2. * M_PI - angle;
     3959    DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle between " << helper << " and " << AngleZero << " is " << angle << " for point " << **listRunner << "." << endl);
     3960    InserterTest = anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner)));
    36893961    if (!InserterTest.second) {
    3690       eLog() << Verbose(0) << "GetCircleOfSetOfPoints() got two atoms with same angle: " << *((InserterTest.first)->second) << " and " << (*listRunner) << endl;
     3962      DoeLog(0) && (eLog() << Verbose(0) << "GetCircleOfSetOfPoints() got two atoms with same angle: " << *((InserterTest.first)->second) << " and " << (*listRunner) << endl);
    36913963      performCriticalExit();
    36923964    }
    36933965  }
    36943966
    3695   for(map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
     3967  for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) {
    36963968    connectedCircle->push_back(AngleRunner->second);
    36973969  }
     
    37083980ListOfTesselPointList * Tesselation::GetPathsOfConnectedPoints(const TesselPoint* const Point) const
    37093981{
    3710         Info FunctionInfo(__func__);
     3982  Info FunctionInfo(__func__);
    37113983  map<double, TesselPoint*> anglesOfPoints;
    3712   list< TesselPointList *> *ListOfPaths = new list< TesselPointList *>;
     3984  list<TesselPointList *> *ListOfPaths = new list<TesselPointList *> ;
    37133985  TesselPointList *connectedPath = NULL;
    37143986  Vector center;
     
    37223994  class BoundaryLineSet *CurrentLine = NULL;
    37233995  class BoundaryLineSet *StartLine = NULL;
    3724 
    37253996  // find the respective boundary point
    37263997  PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr);
     
    37283999    ReferencePoint = PointRunner->second;
    37294000  } else {
    3730     eLog() << Verbose(1) << "GetPathOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl;
     4001    DoeLog(1) && (eLog() << Verbose(1) << "GetPathOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl);
    37314002    return NULL;
    37324003  }
    37334004
    3734   map <class BoundaryLineSet *, bool> TouchedLine;
    3735   map <class BoundaryTriangleSet *, bool> TouchedTriangle;
    3736   map <class BoundaryLineSet *, bool>::iterator LineRunner;
    3737   map <class BoundaryTriangleSet *, bool>::iterator TriangleRunner;
     4005  map<class BoundaryLineSet *, bool> TouchedLine;
     4006  map<class BoundaryTriangleSet *, bool> TouchedTriangle;
     4007  map<class BoundaryLineSet *, bool>::iterator LineRunner;
     4008  map<class BoundaryTriangleSet *, bool>::iterator TriangleRunner;
    37384009  for (LineMap::iterator Runner = ReferencePoint->lines.begin(); Runner != ReferencePoint->lines.end(); Runner++) {
    3739     TouchedLine.insert( pair <class BoundaryLineSet *, bool>(Runner->second, false) );
     4010    TouchedLine.insert(pair<class BoundaryLineSet *, bool> (Runner->second, false));
    37404011    for (TriangleMap::iterator Sprinter = Runner->second->triangles.begin(); Sprinter != Runner->second->triangles.end(); Sprinter++)
    3741       TouchedTriangle.insert( pair <class BoundaryTriangleSet *, bool>(Sprinter->second, false) );
     4012      TouchedTriangle.insert(pair<class BoundaryTriangleSet *, bool> (Sprinter->second, false));
    37424013  }
    37434014  if (!ReferencePoint->lines.empty()) {
     
    37454016      LineRunner = TouchedLine.find(runner->second);
    37464017      if (LineRunner == TouchedLine.end()) {
    3747         eLog() << Verbose(1) << "I could not find " << *runner->second << " in the touched list." << endl;
     4018        DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *runner->second << " in the touched list." << endl);
    37484019      } else if (!LineRunner->second) {
    37494020        LineRunner->second = true;
     
    37534024        StartLine = CurrentLine;
    37544025        CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint);
    3755         Log() << Verbose(1)<< "INFO: Beginning path retrieval at " << *CurrentPoint << " of line " << *CurrentLine << "." << endl;
     4026        DoLog(1) && (Log() << Verbose(1) << "INFO: Beginning path retrieval at " << *CurrentPoint << " of line " << *CurrentLine << "." << endl);
    37564027        do {
    37574028          // push current one
    3758           Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl;
     4029          DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl);
    37594030          connectedPath->push_back(CurrentPoint->node);
    37604031
    37614032          // find next triangle
    37624033          for (TriangleMap::iterator Runner = CurrentLine->triangles.begin(); Runner != CurrentLine->triangles.end(); Runner++) {
    3763             Log() << Verbose(1) << "INFO: Inspecting triangle " << *Runner->second << "." << endl;
     4034            DoLog(1) && (Log() << Verbose(1) << "INFO: Inspecting triangle " << *Runner->second << "." << endl);
    37644035            if ((Runner->second != triangle)) { // look for first triangle not equal to old one
    37654036              triangle = Runner->second;
     
    37684039                if (!TriangleRunner->second) {
    37694040                  TriangleRunner->second = true;
    3770                   Log() << Verbose(1) << "INFO: Connecting triangle is " << *triangle << "." << endl;
     4041                  DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting triangle is " << *triangle << "." << endl);
    37714042                  break;
    37724043                } else {
    3773                   Log() << Verbose(1) << "INFO: Skipping " << *triangle << ", as we have already visited it." << endl;
     4044                  DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *triangle << ", as we have already visited it." << endl);
    37744045                  triangle = NULL;
    37754046                }
    37764047              } else {
    3777                 eLog() << Verbose(1) << "I could not find " << *triangle << " in the touched list." << endl;
     4048                DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *triangle << " in the touched list." << endl);
    37784049                triangle = NULL;
    37794050              }
     
    37834054            break;
    37844055          // find next line
    3785           for (int i=0;i<3;i++) {
     4056          for (int i = 0; i < 3; i++) {
    37864057            if ((triangle->lines[i] != CurrentLine) && (triangle->lines[i]->ContainsBoundaryPoint(ReferencePoint))) { // not the current line and still containing Point
    37874058              CurrentLine = triangle->lines[i];
    3788               Log() << Verbose(1) << "INFO: Connecting line is " << *CurrentLine << "." << endl;
     4059              DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting line is " << *CurrentLine << "." << endl);
    37894060              break;
    37904061            }
     
    37924063          LineRunner = TouchedLine.find(CurrentLine);
    37934064          if (LineRunner == TouchedLine.end())
    3794             eLog() << Verbose(1) << "I could not find " << *CurrentLine << " in the touched list." << endl;
     4065            DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *CurrentLine << " in the touched list." << endl);
    37954066          else
    37964067            LineRunner->second = true;
     
    38004071        } while (CurrentLine != StartLine);
    38014072        // last point is missing, as it's on start line
    3802         Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl;
     4073        DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl);
    38034074        if (StartLine->GetOtherEndpoint(ReferencePoint)->node != connectedPath->back())
    38044075          connectedPath->push_back(StartLine->GetOtherEndpoint(ReferencePoint)->node);
     
    38064077        ListOfPaths->push_back(connectedPath);
    38074078      } else {
    3808         Log() << Verbose(1) << "INFO: Skipping " << *runner->second << ", as we have already visited it." << endl;
     4079        DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *runner->second << ", as we have already visited it." << endl);
    38094080      }
    38104081    }
    38114082  } else {
    3812     eLog() << Verbose(1) << "There are no lines attached to " << *ReferencePoint << "." << endl;
     4083    DoeLog(1) && (eLog() << Verbose(1) << "There are no lines attached to " << *ReferencePoint << "." << endl);
    38134084  }
    38144085
     
    38244095ListOfTesselPointList * Tesselation::GetClosedPathsOfConnectedPoints(const TesselPoint* const Point) const
    38254096{
    3826         Info FunctionInfo(__func__);
     4097  Info FunctionInfo(__func__);
    38274098  list<TesselPointList *> *ListofPaths = GetPathsOfConnectedPoints(Point);
    3828   list<TesselPointList *> *ListofClosedPaths = new list<TesselPointList *>;
     4099  list<TesselPointList *> *ListofClosedPaths = new list<TesselPointList *> ;
    38294100  TesselPointList *connectedPath = NULL;
    38304101  TesselPointList *newPath = NULL;
    38314102  int count = 0;
    3832 
    3833 
    38344103  TesselPointList::iterator CircleRunner;
    38354104  TesselPointList::iterator CircleStart;
    38364105
    3837   for(list<TesselPointList *>::iterator ListRunner = ListofPaths->begin(); ListRunner != ListofPaths->end(); ListRunner++) {
     4106  for (list<TesselPointList *>::iterator ListRunner = ListofPaths->begin(); ListRunner != ListofPaths->end(); ListRunner++) {
    38384107    connectedPath = *ListRunner;
    38394108
    3840     Log() << Verbose(1) << "INFO: Current path is " << connectedPath << "." << endl;
     4109    DoLog(1) && (Log() << Verbose(1) << "INFO: Current path is " << connectedPath << "." << endl);
    38414110
    38424111    // go through list, look for reappearance of starting Point and count
    38434112    CircleStart = connectedPath->begin();
    3844 
    38454113    // go through list, look for reappearance of starting Point and create list
    38464114    TesselPointList::iterator Marker = CircleStart;
     
    38484116      if ((*CircleRunner == *CircleStart) && (CircleRunner != CircleStart)) { // is not the very first point
    38494117        // we have a closed circle from Marker to new Marker
    3850         Log() << Verbose(1) << count+1 << ". closed path consists of: ";
     4118        DoLog(1) && (Log() << Verbose(1) << count + 1 << ". closed path consists of: ");
    38514119        newPath = new TesselPointList;
    38524120        TesselPointList::iterator CircleSprinter = Marker;
    38534121        for (; CircleSprinter != CircleRunner; CircleSprinter++) {
    38544122          newPath->push_back(*CircleSprinter);
    3855           Log() << Verbose(0) << (**CircleSprinter) << " <-> ";
     4123          DoLog(0) && (Log() << Verbose(0) << (**CircleSprinter) << " <-> ");
    38564124        }
    3857         Log() << Verbose(0) << ".." << endl;
     4125        DoLog(0) && (Log() << Verbose(0) << ".." << endl);
    38584126        count++;
    38594127        Marker = CircleRunner;
     
    38644132    }
    38654133  }
    3866   Log() << Verbose(1) << "INFO: " << count << " closed additional path(s) have been created." << endl;
     4134  DoLog(1) && (Log() << Verbose(1) << "INFO: " << count << " closed additional path(s) have been created." << endl);
    38674135
    38684136  // delete list of paths
     
    38704138    connectedPath = *(ListofPaths->begin());
    38714139    ListofPaths->remove(connectedPath);
    3872     delete(connectedPath);
    3873   }
    3874   delete(ListofPaths);
     4140    delete (connectedPath);
     4141  }
     4142  delete (ListofPaths);
    38754143
    38764144  // exit
    38774145  return ListofClosedPaths;
    3878 };
    3879 
     4146}
     4147;
    38804148
    38814149/** Gets all belonging triangles for a given BoundaryPointSet.
     
    38864154TriangleSet *Tesselation::GetAllTriangles(const BoundaryPointSet * const Point) const
    38874155{
    3888         Info FunctionInfo(__func__);
    3889         TriangleSet *connectedTriangles = new TriangleSet;
     4156  Info FunctionInfo(__func__);
     4157  TriangleSet *connectedTriangles = new TriangleSet;
    38904158
    38914159  if (Point == NULL) {
    3892     eLog() << Verbose(1) << "Point given is NULL." << endl;
     4160    DoeLog(1) && (eLog() << Verbose(1) << "Point given is NULL." << endl);
    38934161  } else {
    38944162    // go through its lines and insert all triangles
    38954163    for (LineMap::const_iterator LineRunner = Point->lines.begin(); LineRunner != Point->lines.end(); LineRunner++)
    38964164      for (TriangleMap::iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
    3897       connectedTriangles->insert(TriangleRunner->second);
    3898     }
     4165        connectedTriangles->insert(TriangleRunner->second);
     4166      }
    38994167  }
    39004168
    39014169  return connectedTriangles;
    3902 };
    3903 
     4170}
     4171;
    39044172
    39054173/** Removes a boundary point from the envelope while keeping it closed.
     
    39144182 * \return volume added to the volume inside the tesselated surface by the removal
    39154183 */
    3916 double Tesselation::RemovePointFromTesselatedSurface(class BoundaryPointSet *point) {
     4184double Tesselation::RemovePointFromTesselatedSurface(class BoundaryPointSet *point)
     4185{
    39174186  class BoundaryLineSet *line = NULL;
    39184187  class BoundaryTriangleSet *triangle = NULL;
     
    39224191
    39234192  if (point == NULL) {
    3924     eLog() << Verbose(1) << "Cannot remove the point " << point << ", it's NULL!" << endl;
     4193    DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << point << ", it's NULL!" << endl);
    39254194    return 0.;
    39264195  } else
    3927     Log() << Verbose(0) << "Removing point " << *point << " from tesselated boundary ..." << endl;
     4196    DoLog(0) && (Log() << Verbose(0) << "Removing point " << *point << " from tesselated boundary ..." << endl);
    39284197
    39294198  // copy old location for the volume
    3930   OldPoint.CopyVector(point->node->node);
     4199  OldPoint = (*point->node->node);
    39314200
    39324201  // get list of connected points
    39334202  if (point->lines.empty()) {
    3934     eLog() << Verbose(1) << "Cannot remove the point " << *point << ", it's connected to no lines!" << endl;
     4203    DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << *point << ", it's connected to no lines!" << endl);
    39354204    return 0.;
    39364205  }
     
    39414210  // gather all triangles
    39424211  for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++)
    3943     count+=LineRunner->second->triangles.size();
     4212    count += LineRunner->second->triangles.size();
    39444213  TriangleMap Candidates;
    39454214  for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
     
    39474216    for (TriangleMap::iterator TriangleRunner = line->triangles.begin(); TriangleRunner != line->triangles.end(); TriangleRunner++) {
    39484217      triangle = TriangleRunner->second;
    3949       Candidates.insert( TrianglePair (triangle->Nr, triangle) );
     4218      Candidates.insert(TrianglePair(triangle->Nr, triangle));
    39504219    }
    39514220  }
    39524221
    39534222  // remove all triangles
    3954   count=0;
     4223  count = 0;
    39554224  NormalVector.Zero();
    39564225  for (TriangleMap::iterator Runner = Candidates.begin(); Runner != Candidates.end(); Runner++) {
    3957     Log() << Verbose(1) << "INFO: Removing triangle " << *(Runner->second) << "." << endl;
    3958     NormalVector.SubtractVector(&Runner->second->NormalVector); // has to point inward
     4226    DoLog(1) && (Log() << Verbose(1) << "INFO: Removing triangle " << *(Runner->second) << "." << endl);
     4227    NormalVector -= Runner->second->NormalVector; // has to point inward
    39594228    RemoveTesselationTriangle(Runner->second);
    39604229    count++;
    39614230  }
    3962   Log() << Verbose(1) << count << " triangles were removed." << endl;
     4231  DoLog(1) && (Log() << Verbose(1) << count << " triangles were removed." << endl);
    39634232
    39644233  list<TesselPointList *>::iterator ListAdvance = ListOfClosedPaths->begin();
     
    39694238  double smallestangle;
    39704239  Vector Point, Reference, OrthogonalVector;
    3971   if (count > 2) {  // less than three triangles, then nothing will be created
     4240  if (count > 2) { // less than three triangles, then nothing will be created
    39724241    class TesselPoint *TriangleCandidates[3];
    39734242    count = 0;
    3974     for ( ; ListRunner != ListOfClosedPaths->end(); ListRunner = ListAdvance) { // go through all closed paths
     4243    for (; ListRunner != ListOfClosedPaths->end(); ListRunner = ListAdvance) { // go through all closed paths
    39754244      if (ListAdvance != ListOfClosedPaths->end())
    39764245        ListAdvance++;
    39774246
    39784247      connectedPath = *ListRunner;
    3979 
    39804248      // re-create all triangles by going through connected points list
    39814249      LineList NewLines;
    3982       for (;!connectedPath->empty();) {
     4250      for (; !connectedPath->empty();) {
    39834251        // search middle node with widest angle to next neighbours
    39844252        EndNode = connectedPath->end();
    39854253        smallestangle = 0.;
    39864254        for (MiddleNode = connectedPath->begin(); MiddleNode != connectedPath->end(); MiddleNode++) {
    3987           Log() << Verbose(1) << "INFO: MiddleNode is " << **MiddleNode << "." << endl;
     4255          DoLog(1) && (Log() << Verbose(1) << "INFO: MiddleNode is " << **MiddleNode << "." << endl);
    39884256          // construct vectors to next and previous neighbour
    39894257          StartNode = MiddleNode;
     
    39924260          StartNode--;
    39934261          //Log() << Verbose(3) << "INFO: StartNode is " << **StartNode << "." << endl;
    3994           Point.CopyVector((*StartNode)->node);
    3995           Point.SubtractVector((*MiddleNode)->node);
     4262          Point = (*(*StartNode)->node) - (*(*MiddleNode)->node);
    39964263          StartNode = MiddleNode;
    39974264          StartNode++;
     
    39994266            StartNode = connectedPath->begin();
    40004267          //Log() << Verbose(3) << "INFO: EndNode is " << **StartNode << "." << endl;
    4001           Reference.CopyVector((*StartNode)->node);
    4002           Reference.SubtractVector((*MiddleNode)->node);
    4003           OrthogonalVector.CopyVector((*MiddleNode)->node);
    4004           OrthogonalVector.SubtractVector(&OldPoint);
    4005           OrthogonalVector.MakeNormalVector(&Reference);
     4268          Reference = (*(*StartNode)->node) - (*(*MiddleNode)->node);
     4269          OrthogonalVector = (*(*MiddleNode)->node) - OldPoint;
     4270          OrthogonalVector.MakeNormalTo(Reference);
    40064271          angle = GetAngle(Point, Reference, OrthogonalVector);
    40074272          //if (angle < M_PI)  // no wrong-sided triangles, please?
    4008             if(fabs(angle - M_PI) < fabs(smallestangle - M_PI)) { // get straightest angle (i.e. construct those triangles with smallest area first)
    4009               smallestangle = angle;
    4010               EndNode = MiddleNode;
    4011             }
     4273          if (fabs(angle - M_PI) < fabs(smallestangle - M_PI)) { // get straightest angle (i.e. construct those triangles with smallest area first)
     4274            smallestangle = angle;
     4275            EndNode = MiddleNode;
     4276          }
    40124277        }
    40134278        MiddleNode = EndNode;
    40144279        if (MiddleNode == connectedPath->end()) {
    4015           eLog() << Verbose(0) << "CRITICAL: Could not find a smallest angle!" << endl;
     4280          DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: Could not find a smallest angle!" << endl);
    40164281          performCriticalExit();
    40174282        }
     
    40234288        if (EndNode == connectedPath->end())
    40244289          EndNode = connectedPath->begin();
    4025         Log() << Verbose(2) << "INFO: StartNode is " << **StartNode << "." << endl;
    4026         Log() << Verbose(2) << "INFO: MiddleNode is " << **MiddleNode << "." << endl;
    4027         Log() << Verbose(2) << "INFO: EndNode is " << **EndNode << "." << endl;
    4028         Log() << Verbose(1) << "INFO: Attempting to create triangle " << (*StartNode)->Name << ", " << (*MiddleNode)->Name << " and " << (*EndNode)->Name << "." << endl;
     4290        DoLog(2) && (Log() << Verbose(2) << "INFO: StartNode is " << **StartNode << "." << endl);
     4291        DoLog(2) && (Log() << Verbose(2) << "INFO: MiddleNode is " << **MiddleNode << "." << endl);
     4292        DoLog(2) && (Log() << Verbose(2) << "INFO: EndNode is " << **EndNode << "." << endl);
     4293        DoLog(1) && (Log() << Verbose(1) << "INFO: Attempting to create triangle " << (*StartNode)->getName() << ", " << (*MiddleNode)->getName() << " and " << (*EndNode)->getName() << "." << endl);
    40294294        TriangleCandidates[0] = *StartNode;
    40304295        TriangleCandidates[1] = *MiddleNode;
     
    40324297        triangle = GetPresentTriangle(TriangleCandidates);
    40334298        if (triangle != NULL) {
    4034           eLog() << Verbose(0) << "New triangle already present, skipping!" << endl;
     4299          DoeLog(0) && (eLog() << Verbose(0) << "New triangle already present, skipping!" << endl);
    40354300          StartNode++;
    40364301          MiddleNode++;
     
    40444309          continue;
    40454310        }
    4046         Log() << Verbose(3) << "Adding new triangle points."<< endl;
     4311        DoLog(3) && (Log() << Verbose(3) << "Adding new triangle points." << endl);
    40474312        AddTesselationPoint(*StartNode, 0);
    40484313        AddTesselationPoint(*MiddleNode, 1);
    40494314        AddTesselationPoint(*EndNode, 2);
    4050         Log() << Verbose(3) << "Adding new triangle lines."<< endl;
    4051         AddTesselationLine(TPS[0], TPS[1], 0);
    4052         AddTesselationLine(TPS[0], TPS[2], 1);
     4315        DoLog(3) && (Log() << Verbose(3) << "Adding new triangle lines." << endl);
     4316        AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
     4317        AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
    40534318        NewLines.push_back(BLS[1]);
    4054         AddTesselationLine(TPS[1], TPS[2], 2);
     4319        AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
    40554320        BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    40564321        BTS->GetNormalVector(NormalVector);
     
    40634328        // prepare nodes for next triangle
    40644329        StartNode = EndNode;
    4065         Log() << Verbose(2) << "Removing " << **MiddleNode << " from closed path, remaining points: " << connectedPath->size() << "." << endl;
     4330        DoLog(2) && (Log() << Verbose(2) << "Removing " << **MiddleNode << " from closed path, remaining points: " << connectedPath->size() << "." << endl);
    40664331        connectedPath->remove(*MiddleNode); // remove the middle node (it is surrounded by triangles)
    40674332        if (connectedPath->size() == 2) { // we are done
     
    40704335          break;
    40714336        } else if (connectedPath->size() < 2) { // something's gone wrong!
    4072           eLog() << Verbose(0) << "CRITICAL: There are only two endpoints left!" << endl;
     4337          DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: There are only two endpoints left!" << endl);
    40734338          performCriticalExit();
    40744339        } else {
     
    40904355        do {
    40914356          maxgain = 0;
    4092           for(LineList::iterator Runner = NewLines.begin(); Runner != NewLines.end(); Runner++) {
     4357          for (LineList::iterator Runner = NewLines.begin(); Runner != NewLines.end(); Runner++) {
    40934358            tmp = PickFarthestofTwoBaselines(*Runner);
    40944359            if (maxgain < tmp) {
     
    40994364          if (maxgain != 0) {
    41004365            volume += maxgain;
    4101             Log() << Verbose(1) << "Flipping baseline with highest volume" << **Candidate << "." << endl;
     4366            DoLog(1) && (Log() << Verbose(1) << "Flipping baseline with highest volume" << **Candidate << "." << endl);
    41024367            OtherBase = FlipBaseline(*Candidate);
    41034368            NewLines.erase(Candidate);
     
    41084373
    41094374      ListOfClosedPaths->remove(connectedPath);
    4110       delete(connectedPath);
    4111     }
    4112     Log() << Verbose(0) << count << " triangles were created." << endl;
     4375      delete (connectedPath);
     4376    }
     4377    DoLog(0) && (Log() << Verbose(0) << count << " triangles were created." << endl);
    41134378  } else {
    41144379    while (!ListOfClosedPaths->empty()) {
     
    41164381      connectedPath = *ListRunner;
    41174382      ListOfClosedPaths->remove(connectedPath);
    4118       delete(connectedPath);
    4119     }
    4120     Log() << Verbose(0) << "No need to create any triangles." << endl;
    4121   }
    4122   delete(ListOfClosedPaths);
    4123 
    4124   Log() << Verbose(0) << "Removed volume is " << volume << "." << endl;
     4383      delete (connectedPath);
     4384    }
     4385    DoLog(0) && (Log() << Verbose(0) << "No need to create any triangles." << endl);
     4386  }
     4387  delete (ListOfClosedPaths);
     4388
     4389  DoLog(0) && (Log() << Verbose(0) << "Removed volume is " << volume << "." << endl);
    41254390
    41264391  return volume;
    4127 };
    4128 
    4129 
     4392}
     4393;
    41304394
    41314395/**
     
    41394403TriangleList *Tesselation::FindTriangles(const TesselPoint* const Points[3]) const
    41404404{
    4141         Info FunctionInfo(__func__);
    4142         TriangleList *result = new TriangleList;
     4405  Info FunctionInfo(__func__);
     4406  TriangleList *result = new TriangleList;
    41434407  LineMap::const_iterator FindLine;
    41444408  TriangleMap::const_iterator FindTriangle;
     
    41644428      for (int i = 0; i < 3; i++) {
    41654429        if (TrianglePoints[i] != NULL) {
    4166           for (int j = i+1; j < 3; j++) {
     4430          for (int j = i + 1; j < 3; j++) {
    41674431            if (TrianglePoints[j] != NULL) {
    41684432              for (FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr); // is a multimap!
    4169                   (FindLine != TrianglePoints[i]->lines.end()) && (FindLine->first == TrianglePoints[j]->node->nr);
    4170                   FindLine++) {
    4171                 for (FindTriangle = FindLine->second->triangles.begin();
    4172                     FindTriangle != FindLine->second->triangles.end();
    4173                     FindTriangle++) {
     4433              (FindLine != TrianglePoints[i]->lines.end()) && (FindLine->first == TrianglePoints[j]->node->nr); FindLine++) {
     4434                for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) {
    41744435                  if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
    41754436                    result->push_back(FindTriangle->second);
     
    41864447    case 1: // copy all triangles of the respective line
    41874448    {
    4188       int i=0;
     4449      int i = 0;
    41894450      for (; i < 3; i++)
    41904451        if (TrianglePoints[i] == NULL)
    41914452          break;
    4192       for (FindLine = TrianglePoints[(i+1)%3]->lines.find(TrianglePoints[(i+2)%3]->node->nr); // is a multimap!
    4193           (FindLine != TrianglePoints[(i+1)%3]->lines.end()) && (FindLine->first == TrianglePoints[(i+2)%3]->node->nr);
    4194           FindLine++) {
    4195         for (FindTriangle = FindLine->second->triangles.begin();
    4196             FindTriangle != FindLine->second->triangles.end();
    4197             FindTriangle++) {
     4453      for (FindLine = TrianglePoints[(i + 1) % 3]->lines.find(TrianglePoints[(i + 2) % 3]->node->nr); // is a multimap!
     4454      (FindLine != TrianglePoints[(i + 1) % 3]->lines.end()) && (FindLine->first == TrianglePoints[(i + 2) % 3]->node->nr); FindLine++) {
     4455        for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) {
    41984456          if (FindTriangle->second->IsPresentTupel(TrianglePoints)) {
    41994457            result->push_back(FindTriangle->second);
     
    42054463    case 2: // copy all triangles of the respective point
    42064464    {
    4207       int i=0;
     4465      int i = 0;
    42084466      for (; i < 3; i++)
    42094467        if (TrianglePoints[i] != NULL)
     
    42234481    }
    42244482    default:
    4225       eLog() << Verbose(0) << "Number of wildcards is greater than 3, cannot happen!" << endl;
     4483      DoeLog(0) && (eLog() << Verbose(0) << "Number of wildcards is greater than 3, cannot happen!" << endl);
    42264484      performCriticalExit();
    42274485      break;
     
    42314489}
    42324490
    4233 struct BoundaryLineSetCompare {
    4234   bool operator() (const BoundaryLineSet * const a, const BoundaryLineSet * const b) {
     4491struct BoundaryLineSetCompare
     4492{
     4493  bool operator()(const BoundaryLineSet * const a, const BoundaryLineSet * const b)
     4494  {
    42354495    int lowerNra = -1;
    42364496    int lowerNrb = -1;
     
    42504510    else if (a->endpoints[lowerNra] > b->endpoints[lowerNrb])
    42514511      return false;
    4252     else {  // both lower-numbered endpoints are the same ...
    4253      if (a->endpoints[(lowerNra+1)%2] < b->endpoints[(lowerNrb+1)%2])
    4254        return true;
    4255      else if (a->endpoints[(lowerNra+1)%2] > b->endpoints[(lowerNrb+1)%2])
    4256        return false;
     4512    else { // both lower-numbered endpoints are the same ...
     4513      if (a->endpoints[(lowerNra + 1) % 2] < b->endpoints[(lowerNrb + 1) % 2])
     4514        return true;
     4515      else if (a->endpoints[(lowerNra + 1) % 2] > b->endpoints[(lowerNrb + 1) % 2])
     4516        return false;
    42574517    }
    42584518    return false;
    4259   };
     4519  }
     4520  ;
    42604521};
    42614522
     
    42704531IndexToIndex * Tesselation::FindAllDegeneratedLines()
    42714532{
    4272         Info FunctionInfo(__func__);
    4273         UniqueLines AllLines;
     4533  Info FunctionInfo(__func__);
     4534  UniqueLines AllLines;
    42744535  IndexToIndex * DegeneratedLines = new IndexToIndex;
    42754536
    42764537  // sanity check
    42774538  if (LinesOnBoundary.empty()) {
    4278     eLog() << Verbose(2) << "FindAllDegeneratedTriangles() was called without any tesselation structure.";
     4539    DoeLog(2) && (eLog() << Verbose(2) << "FindAllDegeneratedTriangles() was called without any tesselation structure.");
    42794540    return DegeneratedLines;
    42804541  }
    4281 
    42824542  LineMap::iterator LineRunner1;
    4283   pair< UniqueLines::iterator, bool> tester;
     4543  pair<UniqueLines::iterator, bool> tester;
    42844544  for (LineRunner1 = LinesOnBoundary.begin(); LineRunner1 != LinesOnBoundary.end(); ++LineRunner1) {
    4285     tester = AllLines.insert( LineRunner1->second );
     4545    tester = AllLines.insert(LineRunner1->second);
    42864546    if (!tester.second) { // found degenerated line
    4287       DegeneratedLines->insert ( pair<int, int> (LineRunner1->second->Nr, (*tester.first)->Nr) );
    4288       DegeneratedLines->insert ( pair<int, int> ((*tester.first)->Nr, LineRunner1->second->Nr) );
     4547      DegeneratedLines->insert(pair<int, int> (LineRunner1->second->Nr, (*tester.first)->Nr));
     4548      DegeneratedLines->insert(pair<int, int> ((*tester.first)->Nr, LineRunner1->second->Nr));
    42894549    }
    42904550  }
     
    42924552  AllLines.clear();
    42934553
    4294   Log() << Verbose(0) << "FindAllDegeneratedLines() found " << DegeneratedLines->size() << " lines." << endl;
     4554  DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedLines() found " << DegeneratedLines->size() << " lines." << endl);
    42954555  IndexToIndex::iterator it;
    42964556  for (it = DegeneratedLines->begin(); it != DegeneratedLines->end(); it++) {
     
    42984558    const LineMap::const_iterator Line2 = LinesOnBoundary.find((*it).second);
    42994559    if (Line1 != LinesOnBoundary.end() && Line2 != LinesOnBoundary.end())
    4300       Log() << Verbose(0) << *Line1->second << " => " << *Line2->second << endl;
     4560      DoLog(0) && (Log() << Verbose(0) << *Line1->second << " => " << *Line2->second << endl);
    43014561    else
    4302       eLog() << Verbose(1) << "Either " << (*it).first << " or " << (*it).second << " are not in LinesOnBoundary!" << endl;
     4562      DoeLog(1) && (eLog() << Verbose(1) << "Either " << (*it).first << " or " << (*it).second << " are not in LinesOnBoundary!" << endl);
    43034563  }
    43044564
     
    43144574IndexToIndex * Tesselation::FindAllDegeneratedTriangles()
    43154575{
    4316         Info FunctionInfo(__func__);
     4576  Info FunctionInfo(__func__);
    43174577  IndexToIndex * DegeneratedLines = FindAllDegeneratedLines();
    43184578  IndexToIndex * DegeneratedTriangles = new IndexToIndex;
    4319 
    43204579  TriangleMap::iterator TriangleRunner1, TriangleRunner2;
    43214580  LineMap::iterator Liner;
     
    43324591    for (TriangleRunner1 = line1->triangles.begin(); TriangleRunner1 != line1->triangles.end(); ++TriangleRunner1) {
    43334592      for (TriangleRunner2 = line2->triangles.begin(); TriangleRunner2 != line2->triangles.end(); ++TriangleRunner2) {
    4334         if ((TriangleRunner1->second != TriangleRunner2->second)
    4335           && (TriangleRunner1->second->IsPresentTupel(TriangleRunner2->second))) {
    4336           DegeneratedTriangles->insert( pair<int, int> (TriangleRunner1->second->Nr, TriangleRunner2->second->Nr) );
    4337           DegeneratedTriangles->insert( pair<int, int> (TriangleRunner2->second->Nr, TriangleRunner1->second->Nr) );
     4593        if ((TriangleRunner1->second != TriangleRunner2->second) && (TriangleRunner1->second->IsPresentTupel(TriangleRunner2->second))) {
     4594          DegeneratedTriangles->insert(pair<int, int> (TriangleRunner1->second->Nr, TriangleRunner2->second->Nr));
     4595          DegeneratedTriangles->insert(pair<int, int> (TriangleRunner2->second->Nr, TriangleRunner1->second->Nr));
    43384596        }
    43394597      }
    43404598    }
    43414599  }
    4342   delete(DegeneratedLines);
    4343 
    4344   Log() << Verbose(0) << "FindAllDegeneratedTriangles() found " << DegeneratedTriangles->size() << " triangles:" << endl;
     4600  delete (DegeneratedLines);
     4601
     4602  DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedTriangles() found " << DegeneratedTriangles->size() << " triangles:" << endl);
    43454603  IndexToIndex::iterator it;
    43464604  for (it = DegeneratedTriangles->begin(); it != DegeneratedTriangles->end(); it++)
    4347       Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl;
     4605    DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl);
    43484606
    43494607  return DegeneratedTriangles;
     
    43564614void Tesselation::RemoveDegeneratedTriangles()
    43574615{
    4358         Info FunctionInfo(__func__);
     4616  Info FunctionInfo(__func__);
    43594617  IndexToIndex * DegeneratedTriangles = FindAllDegeneratedTriangles();
    43604618  TriangleMap::iterator finder;
    43614619  BoundaryTriangleSet *triangle = NULL, *partnerTriangle = NULL;
    4362   int count  = 0;
    4363 
    4364   for (IndexToIndex::iterator TriangleKeyRunner = DegeneratedTriangles->begin();
    4365     TriangleKeyRunner != DegeneratedTriangles->end(); ++TriangleKeyRunner
    4366   ) {
     4620  int count = 0;
     4621
     4622  for (IndexToIndex::iterator TriangleKeyRunner = DegeneratedTriangles->begin(); TriangleKeyRunner != DegeneratedTriangles->end(); ++TriangleKeyRunner) {
    43674623    finder = TrianglesOnBoundary.find(TriangleKeyRunner->first);
    43684624    if (finder != TrianglesOnBoundary.end())
     
    43814637        trianglesShareLine = trianglesShareLine || triangle->lines[i] == partnerTriangle->lines[j];
    43824638
    4383     if (trianglesShareLine
    4384       && (triangle->endpoints[1]->LinesCount > 2)
    4385       && (triangle->endpoints[2]->LinesCount > 2)
    4386       && (triangle->endpoints[0]->LinesCount > 2)
    4387     ) {
     4639    if (trianglesShareLine && (triangle->endpoints[1]->LinesCount > 2) && (triangle->endpoints[2]->LinesCount > 2) && (triangle->endpoints[0]->LinesCount > 2)) {
    43884640      // check whether we have to fix lines
    43894641      BoundaryTriangleSet *Othertriangle = NULL;
     
    44054657            // the line of triangle receives the degenerated ones
    44064658            triangle->lines[i]->triangles.erase(Othertriangle->Nr);
    4407             triangle->lines[i]->triangles.insert( TrianglePair( partnerTriangle->Nr, partnerTriangle) );
    4408             for (int k=0;k<3;k++)
     4659            triangle->lines[i]->triangles.insert(TrianglePair(partnerTriangle->Nr, partnerTriangle));
     4660            for (int k = 0; k < 3; k++)
    44094661              if (triangle->lines[i] == Othertriangle->lines[k]) {
    44104662                Othertriangle->lines[k] = partnerTriangle->lines[j];
     
    44124664              }
    44134665            // the line of partnerTriangle receives the non-degenerated ones
    4414             partnerTriangle->lines[j]->triangles.erase( partnerTriangle->Nr);
    4415             partnerTriangle->lines[j]->triangles.insert( TrianglePair( Othertriangle->Nr, Othertriangle) );
     4666            partnerTriangle->lines[j]->triangles.erase(partnerTriangle->Nr);
     4667            partnerTriangle->lines[j]->triangles.insert(TrianglePair(Othertriangle->Nr, Othertriangle));
    44164668            partnerTriangle->lines[j] = triangle->lines[i];
    44174669          }
     
    44194671      // erase the pair
    44204672      count += (int) DegeneratedTriangles->erase(triangle->Nr);
    4421       Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *triangle << "." << endl;
     4673      DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *triangle << "." << endl);
    44224674      RemoveTesselationTriangle(triangle);
    44234675      count += (int) DegeneratedTriangles->erase(partnerTriangle->Nr);
    4424       Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *partnerTriangle << "." << endl;
     4676      DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *partnerTriangle << "." << endl);
    44254677      RemoveTesselationTriangle(partnerTriangle);
    44264678    } else {
    4427       Log() << Verbose(0) << "RemoveDegeneratedTriangles() does not remove triangle " << *triangle
    4428         << " and its partner " << *partnerTriangle << " because it is essential for at"
    4429         << " least one of the endpoints to be kept in the tesselation structure." << endl;
    4430     }
    4431   }
    4432   delete(DegeneratedTriangles);
     4679      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);
     4680    }
     4681  }
     4682  delete (DegeneratedTriangles);
    44334683  if (count > 0)
    44344684    LastTriangle = NULL;
    44354685
    4436   Log() << Verbose(0) << "RemoveDegeneratedTriangles() removed " << count << " triangles:" << endl;
     4686  DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removed " << count << " triangles:" << endl);
    44374687}
    44384688
     
    44474697void Tesselation::AddBoundaryPointByDegeneratedTriangle(class TesselPoint *point, LinkedCell *LC)
    44484698{
    4449         Info FunctionInfo(__func__);
     4699  Info FunctionInfo(__func__);
    44504700  // find nearest boundary point
    44514701  class TesselPoint *BackupPoint = NULL;
     
    44604710    NearestBoundaryPoint = PointRunner->second;
    44614711  } else {
    4462     eLog() << Verbose(1) << "I cannot find the boundary point." << endl;
     4712    DoeLog(1) && (eLog() << Verbose(1) << "I cannot find the boundary point." << endl);
    44634713    return;
    44644714  }
    4465   Log() << Verbose(0) << "Nearest point on boundary is " << NearestPoint->Name << "." << endl;
     4715  DoLog(0) && (Log() << Verbose(0) << "Nearest point on boundary is " << NearestPoint->getName() << "." << endl);
    44664716
    44674717  // go through its lines and find the best one to split
     
    44714721  class BoundaryLineSet *BestLine = NULL;
    44724722  for (LineMap::iterator Runner = NearestBoundaryPoint->lines.begin(); Runner != NearestBoundaryPoint->lines.end(); Runner++) {
    4473     BaseLine.CopyVector(Runner->second->endpoints[0]->node->node);
    4474     BaseLine.SubtractVector(Runner->second->endpoints[1]->node->node);
    4475     CenterToPoint.CopyVector(Runner->second->endpoints[0]->node->node);
    4476     CenterToPoint.AddVector(Runner->second->endpoints[1]->node->node);
    4477     CenterToPoint.Scale(0.5);
    4478     CenterToPoint.SubtractVector(point->node);
    4479     angle = CenterToPoint.Angle(&BaseLine);
     4723    BaseLine = (*Runner->second->endpoints[0]->node->node) -
     4724               (*Runner->second->endpoints[1]->node->node);
     4725    CenterToPoint = 0.5 * ((*Runner->second->endpoints[0]->node->node) +
     4726                           (*Runner->second->endpoints[1]->node->node));
     4727    CenterToPoint -= (*point->node);
     4728    angle = CenterToPoint.Angle(BaseLine);
    44804729    if (fabs(angle - M_PI/2.) < fabs(BestAngle - M_PI/2.)) {
    44814730      BestAngle = angle;
     
    44884737  BestLine->triangles.erase(TempTriangle->Nr);
    44894738  int nr = -1;
    4490   for (int i=0;i<3; i++) {
     4739  for (int i = 0; i < 3; i++) {
    44914740    if (TempTriangle->lines[i] == BestLine) {
    44924741      nr = i;
     
    44964745
    44974746  // create new triangle to connect point (connects automatically with the missing spot of the chosen line)
    4498   Log() << Verbose(2) << "Adding new triangle points."<< endl;
     4747  DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl);
    44994748  AddTesselationPoint((BestLine->endpoints[0]->node), 0);
    45004749  AddTesselationPoint((BestLine->endpoints[1]->node), 1);
    45014750  AddTesselationPoint(point, 2);
    4502   Log() << Verbose(2) << "Adding new triangle lines."<< endl;
    4503   AddTesselationLine(TPS[0], TPS[1], 0);
    4504   AddTesselationLine(TPS[0], TPS[2], 1);
    4505   AddTesselationLine(TPS[1], TPS[2], 2);
     4751  DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl);
     4752  AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
     4753  AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
     4754  AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
    45064755  BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    45074756  BTS->GetNormalVector(TempTriangle->NormalVector);
    45084757  BTS->NormalVector.Scale(-1.);
    4509   Log() << Verbose(1) << "INFO: NormalVector of new triangle is " << BTS->NormalVector << "." << endl;
     4758  DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of new triangle is " << BTS->NormalVector << "." << endl);
    45104759  AddTesselationTriangle();
    45114760
    45124761  // create other side of this triangle and close both new sides of the first created triangle
    4513   Log() << Verbose(2) << "Adding new triangle points."<< endl;
     4762  DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl);
    45144763  AddTesselationPoint((BestLine->endpoints[0]->node), 0);
    45154764  AddTesselationPoint((BestLine->endpoints[1]->node), 1);
    45164765  AddTesselationPoint(point, 2);
    4517   Log() << Verbose(2) << "Adding new triangle lines."<< endl;
    4518   AddTesselationLine(TPS[0], TPS[1], 0);
    4519   AddTesselationLine(TPS[0], TPS[2], 1);
    4520   AddTesselationLine(TPS[1], TPS[2], 2);
     4766  DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl);
     4767  AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
     4768  AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
     4769  AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
    45214770  BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount);
    45224771  BTS->GetNormalVector(TempTriangle->NormalVector);
    4523   Log() << Verbose(1) << "INFO: NormalVector of other new triangle is " << BTS->NormalVector << "." << endl;
     4772  DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of other new triangle is " << BTS->NormalVector << "." << endl);
    45244773  AddTesselationTriangle();
    45254774
    45264775  // add removed triangle to the last open line of the second triangle
    4527   for (int i=0;i<3;i++) { // look for the same line as BestLine (only it's its degenerated companion)
     4776  for (int i = 0; i < 3; i++) { // look for the same line as BestLine (only it's its degenerated companion)
    45284777    if ((BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[0])) && (BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[1]))) {
    4529       if (BestLine == BTS->lines[i]){
    4530         eLog() << Verbose(0) << "BestLine is same as found line, something's wrong here!" << endl;
     4778      if (BestLine == BTS->lines[i]) {
     4779        DoeLog(0) && (eLog() << Verbose(0) << "BestLine is same as found line, something's wrong here!" << endl);
    45314780        performCriticalExit();
    45324781      }
    4533       BTS->lines[i]->triangles.insert( pair<int, class BoundaryTriangleSet *> (TempTriangle->Nr, TempTriangle) );
     4782      BTS->lines[i]->triangles.insert(pair<int, class BoundaryTriangleSet *> (TempTriangle->Nr, TempTriangle));
    45344783      TempTriangle->lines[nr] = BTS->lines[i];
    45354784      break;
    45364785    }
    45374786  }
    4538 };
     4787}
     4788;
    45394789
    45404790/** Writes the envelope to file.
     
    45454795void Tesselation::Output(const char *filename, const PointCloud * const cloud)
    45464796{
    4547         Info FunctionInfo(__func__);
     4797  Info FunctionInfo(__func__);
    45484798  ofstream *tempstream = NULL;
    45494799  string NameofTempFile;
    4550   char NumberName[255];
     4800  string NumberName;
    45514801
    45524802  if (LastTriangle != NULL) {
    4553     sprintf(NumberName, "-%04d-%s_%s_%s", (int)TrianglesOnBoundary.size(), LastTriangle->endpoints[0]->node->Name, LastTriangle->endpoints[1]->node->Name, LastTriangle->endpoints[2]->node->Name);
     4803    stringstream sstr;
     4804    sstr << "-"<< TrianglesOnBoundary.size() << "-" << LastTriangle->endpoints[0]->node->getName() << "_" << LastTriangle->endpoints[1]->node->getName() << "_" << LastTriangle->endpoints[2]->node->getName();
     4805    NumberName = sstr.str();
    45544806    if (DoTecplotOutput) {
    45554807      string NameofTempFile(filename);
    45564808      NameofTempFile.append(NumberName);
    4557       for(size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
    4558       NameofTempFile.erase(npos, 1);
     4809      for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
     4810        NameofTempFile.erase(npos, 1);
    45594811      NameofTempFile.append(TecplotSuffix);
    4560       Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n";
     4812      DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n");
    45614813      tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
    45624814      WriteTecplotFile(tempstream, this, cloud, TriangleFilesWritten);
    45634815      tempstream->close();
    45644816      tempstream->flush();
    4565       delete(tempstream);
     4817      delete (tempstream);
    45664818    }
    45674819
     
    45694821      string NameofTempFile(filename);
    45704822      NameofTempFile.append(NumberName);
    4571       for(size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
    4572       NameofTempFile.erase(npos, 1);
     4823      for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos))
     4824        NameofTempFile.erase(npos, 1);
    45734825      NameofTempFile.append(Raster3DSuffix);
    4574       Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n";
     4826      DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n");
    45754827      tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc);
    45764828      WriteRaster3dFile(tempstream, this, cloud);
     
    45784830      tempstream->close();
    45794831      tempstream->flush();
    4580       delete(tempstream);
     4832      delete (tempstream);
    45814833    }
    45824834  }
    45834835  if (DoTecplotOutput || DoRaster3DOutput)
    45844836    TriangleFilesWritten++;
    4585 };
    4586 
    4587 struct BoundaryPolygonSetCompare {
    4588   bool operator()(const BoundaryPolygonSet * s1, const BoundaryPolygonSet * s2) const {
     4837}
     4838;
     4839
     4840struct BoundaryPolygonSetCompare
     4841{
     4842  bool operator()(const BoundaryPolygonSet * s1, const BoundaryPolygonSet * s2) const
     4843  {
    45894844    if (s1->endpoints.size() < s2->endpoints.size())
    45904845      return true;
     
    46154870{
    46164871  Info FunctionInfo(__func__);
    4617 
    46184872  /// 2. Go through all BoundaryPointSet's, check their triangles' NormalVector
    46194873  IndexToIndex *DegeneratedTriangles = FindAllDegeneratedTriangles();
    4620   set < BoundaryPointSet *> EndpointCandidateList;
    4621   pair < set < BoundaryPointSet *>::iterator, bool > InsertionTester;
    4622   pair < map < int, Vector *>::iterator, bool > TriangleInsertionTester;
     4874  set<BoundaryPointSet *> EndpointCandidateList;
     4875  pair<set<BoundaryPointSet *>::iterator, bool> InsertionTester;
     4876  pair<map<int, Vector *>::iterator, bool> TriangleInsertionTester;
    46234877  for (PointMap::const_iterator Runner = PointsOnBoundary.begin(); Runner != PointsOnBoundary.end(); Runner++) {
    4624     Log() << Verbose(0) << "Current point is " << *Runner->second << "." << endl;
    4625     map < int, Vector *> TriangleVectors;
     4878    DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Runner->second << "." << endl);
     4879    map<int, Vector *> TriangleVectors;
    46264880    // gather all NormalVectors
    4627     Log() << Verbose(1) << "Gathering triangles ..." << endl;
     4881    DoLog(1) && (Log() << Verbose(1) << "Gathering triangles ..." << endl);
    46284882    for (LineMap::const_iterator LineRunner = (Runner->second)->lines.begin(); LineRunner != (Runner->second)->lines.end(); LineRunner++)
    46294883      for (TriangleMap::const_iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
    46304884        if (DegeneratedTriangles->find(TriangleRunner->second->Nr) == DegeneratedTriangles->end()) {
    4631           TriangleInsertionTester = TriangleVectors.insert( pair< int, Vector *> ((TriangleRunner->second)->Nr, &((TriangleRunner->second)->NormalVector)) );
     4885          TriangleInsertionTester = TriangleVectors.insert(pair<int, Vector *> ((TriangleRunner->second)->Nr, &((TriangleRunner->second)->NormalVector)));
    46324886          if (TriangleInsertionTester.second)
    4633             Log() << Verbose(1) << " Adding triangle " << *(TriangleRunner->second) << " to triangles to check-list." << endl;
     4887            DoLog(1) && (Log() << Verbose(1) << " Adding triangle " << *(TriangleRunner->second) << " to triangles to check-list." << endl);
    46344888        } else {
    4635           Log() << Verbose(1) << " NOT adding triangle " << *(TriangleRunner->second) << " as it's a simply degenerated one." << endl;
     4889          DoLog(1) && (Log() << Verbose(1) << " NOT adding triangle " << *(TriangleRunner->second) << " as it's a simply degenerated one." << endl);
    46364890        }
    46374891      }
    46384892    // check whether there are two that are parallel
    4639     Log() << Verbose(1) << "Finding two parallel triangles ..." << endl;
    4640     for (map < int, Vector *>::iterator VectorWalker = TriangleVectors.begin(); VectorWalker != TriangleVectors.end(); VectorWalker++)
    4641       for (map < int, Vector *>::iterator VectorRunner = VectorWalker; VectorRunner != TriangleVectors.end(); VectorRunner++)
     4893    DoLog(1) && (Log() << Verbose(1) << "Finding two parallel triangles ..." << endl);
     4894    for (map<int, Vector *>::iterator VectorWalker = TriangleVectors.begin(); VectorWalker != TriangleVectors.end(); VectorWalker++)
     4895      for (map<int, Vector *>::iterator VectorRunner = VectorWalker; VectorRunner != TriangleVectors.end(); VectorRunner++)
    46424896        if (VectorWalker != VectorRunner) { // skip equals
    4643           const double SCP = VectorWalker->second->ScalarProduct(VectorRunner->second); // ScalarProduct should result in -1. for degenerated triangles
    4644           Log() << Verbose(1) << "Checking " << *VectorWalker->second<< " against " << *VectorRunner->second << ": " << SCP << endl;
     4897          const double SCP = VectorWalker->second->ScalarProduct(*VectorRunner->second); // ScalarProduct should result in -1. for degenerated triangles
     4898          DoLog(1) && (Log() << Verbose(1) << "Checking " << *VectorWalker->second << " against " << *VectorRunner->second << ": " << SCP << endl);
    46454899          if (fabs(SCP + 1.) < ParallelEpsilon) {
    46464900            InsertionTester = EndpointCandidateList.insert((Runner->second));
    46474901            if (InsertionTester.second)
    4648               Log() << Verbose(0) << " Adding " << *Runner->second << " to endpoint candidate list." << endl;
     4902              DoLog(0) && (Log() << Verbose(0) << " Adding " << *Runner->second << " to endpoint candidate list." << endl);
    46494903            // and break out of both loops
    46504904            VectorWalker = TriangleVectors.end();
     
    46614915  BoundaryPointSet *OtherWalker = NULL;
    46624916  BoundaryPolygonSet *Current = NULL;
    4663   stack <BoundaryPointSet*> ToCheckConnecteds;
     4917  stack<BoundaryPointSet*> ToCheckConnecteds;
    46644918  while (!EndpointCandidateList.empty()) {
    46654919    Walker = *(EndpointCandidateList.begin());
    4666     if (Current == NULL) {  // create a new polygon with current candidate
    4667       Log() << Verbose(0) << "Starting new polygon set at point " << *Walker << endl;
     4920    if (Current == NULL) { // create a new polygon with current candidate
     4921      DoLog(0) && (Log() << Verbose(0) << "Starting new polygon set at point " << *Walker << endl);
    46684922      Current = new BoundaryPolygonSet;
    46694923      Current->endpoints.insert(Walker);
     
    46784932      for (LineMap::const_iterator LineWalker = Walker->lines.begin(); LineWalker != Walker->lines.end(); LineWalker++) {
    46794933        OtherWalker = (LineWalker->second)->GetOtherEndpoint(Walker);
    4680         Log() << Verbose(1) << "Checking " << *OtherWalker << endl;
    4681         set < BoundaryPointSet *>::iterator Finder = EndpointCandidateList.find(OtherWalker);
    4682         if (Finder != EndpointCandidateList.end()) {  // found a connected partner
    4683           Log() << Verbose(1) << " Adding to polygon." << endl;
     4934        DoLog(1) && (Log() << Verbose(1) << "Checking " << *OtherWalker << endl);
     4935        set<BoundaryPointSet *>::iterator Finder = EndpointCandidateList.find(OtherWalker);
     4936        if (Finder != EndpointCandidateList.end()) { // found a connected partner
     4937          DoLog(1) && (Log() << Verbose(1) << " Adding to polygon." << endl);
    46844938          Current->endpoints.insert(OtherWalker);
    4685           EndpointCandidateList.erase(Finder);  // remove from candidates
    4686           ToCheckConnecteds.push(OtherWalker);  // but check its partners too
     4939          EndpointCandidateList.erase(Finder); // remove from candidates
     4940          ToCheckConnecteds.push(OtherWalker); // but check its partners too
    46874941        } else {
    4688           Log() << Verbose(1) << " is not connected to " << *Walker << endl;
     4942          DoLog(1) && (Log() << Verbose(1) << " is not connected to " << *Walker << endl);
    46894943        }
    46904944      }
    46914945    }
    46924946
    4693     Log() << Verbose(0) << "Final polygon is " << *Current << endl;
     4947    DoLog(0) && (Log() << Verbose(0) << "Final polygon is " << *Current << endl);
    46944948    ListofDegeneratedPolygons.insert(Current);
    46954949    Current = NULL;
     
    46984952  const int counter = ListofDegeneratedPolygons.size();
    46994953
    4700   Log() << Verbose(0) << "The following " << counter << " degenerated polygons have been found: " << endl;
     4954  DoLog(0) && (Log() << Verbose(0) << "The following " << counter << " degenerated polygons have been found: " << endl);
    47014955  for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++)
    4702     Log() << Verbose(0) << " " << **PolygonRunner << endl;
     4956    DoLog(0) && (Log() << Verbose(0) << " " << **PolygonRunner << endl);
    47034957
    47044958  /// 4. Go through all these degenerated polygons
    47054959  for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++) {
    4706     stack <int> TriangleNrs;
     4960    stack<int> TriangleNrs;
    47074961    Vector NormalVector;
    47084962    /// 4a. Gather all triangles of this polygon
     
    47114965    // check whether number is bigger than 2, otherwise it's just a simply degenerated one and nothing to do.
    47124966    if (T->size() == 2) {
    4713       Log() << Verbose(1) << " Skipping degenerated polygon, is just a (already simply degenerated) triangle." << endl;
    4714       delete(T);
     4967      DoLog(1) && (Log() << Verbose(1) << " Skipping degenerated polygon, is just a (already simply degenerated) triangle." << endl);
     4968      delete (T);
    47154969      continue;
    47164970    }
     
    47214975    // connections to either polygon ...
    47224976    if (T->size() % 2 != 0) {
    4723       eLog() << Verbose(0) << " degenerated polygon contains an odd number of triangles, probably contains bridging non-degenerated ones, too!" << endl;
     4977      DoeLog(0) && (eLog() << Verbose(0) << " degenerated polygon contains an odd number of triangles, probably contains bridging non-degenerated ones, too!" << endl);
    47244978      performCriticalExit();
    47254979    }
    4726 
    4727     TriangleSet::iterator TriangleWalker = T->begin();  // is the inner iterator
     4980    TriangleSet::iterator TriangleWalker = T->begin(); // is the inner iterator
    47284981    /// 4a. Get NormalVector for one side (this is "front")
    4729     NormalVector.CopyVector(&(*TriangleWalker)->NormalVector);
    4730     Log() << Verbose(1) << "\"front\" defining triangle is " << **TriangleWalker << " and Normal vector of \"front\" side is " << NormalVector << endl;
     4982    NormalVector = (*TriangleWalker)->NormalVector;
     4983    DoLog(1) && (Log() << Verbose(1) << "\"front\" defining triangle is " << **TriangleWalker << " and Normal vector of \"front\" side is " << NormalVector << endl);
    47314984    TriangleWalker++;
    47324985    TriangleSet::iterator TriangleSprinter = TriangleWalker; // is the inner advanced iterator
     
    47374990      triangle = *TriangleWalker;
    47384991      TriangleSprinter++;
    4739       Log() << Verbose(1) << "Current triangle to test for removal: " << *triangle << endl;
    4740       if (triangle->NormalVector.ScalarProduct(&NormalVector) < 0) { // if from other side, then delete and remove from list
    4741         Log() << Verbose(1) << " Removing ... " << endl;
     4992      DoLog(1) && (Log() << Verbose(1) << "Current triangle to test for removal: " << *triangle << endl);
     4993      if (triangle->NormalVector.ScalarProduct(NormalVector) < 0) { // if from other side, then delete and remove from list
     4994        DoLog(1) && (Log() << Verbose(1) << " Removing ... " << endl);
    47424995        TriangleNrs.push(triangle->Nr);
    47434996        T->erase(TriangleWalker);
    47444997        RemoveTesselationTriangle(triangle);
    47454998      } else
    4746         Log() << Verbose(1) << " Keeping ... " << endl;
     4999        DoLog(1) && (Log() << Verbose(1) << " Keeping ... " << endl);
    47475000    }
    47485001    /// 4c. Copy all "front" triangles but with inverse NormalVector
    47495002    TriangleWalker = T->begin();
    4750     while (TriangleWalker != T->end()) {  // go through all front triangles
    4751       Log() << Verbose(1) << " Re-creating triangle " << **TriangleWalker << " with NormalVector " << (*TriangleWalker)->NormalVector << endl;
     5003    while (TriangleWalker != T->end()) { // go through all front triangles
     5004      DoLog(1) && (Log() << Verbose(1) << " Re-creating triangle " << **TriangleWalker << " with NormalVector " << (*TriangleWalker)->NormalVector << endl);
    47525005      for (int i = 0; i < 3; i++)
    47535006        AddTesselationPoint((*TriangleWalker)->endpoints[i]->node, i);
    4754       AddTesselationLine(TPS[0], TPS[1], 0);
    4755       AddTesselationLine(TPS[0], TPS[2], 1);
    4756       AddTesselationLine(TPS[1], TPS[2], 2);
     5007      AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0);
     5008      AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1);
     5009      AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2);
    47575010      if (TriangleNrs.empty())
    4758         eLog() << Verbose(0) << "No more free triangle numbers!" << endl;
     5011        DoeLog(0) && (eLog() << Verbose(0) << "No more free triangle numbers!" << endl);
    47595012      BTS = new BoundaryTriangleSet(BLS, TriangleNrs.top()); // copy triangle ...
    47605013      AddTesselationTriangle(); // ... and add
    47615014      TriangleNrs.pop();
    4762       BTS->NormalVector.CopyVector(&(*TriangleWalker)->NormalVector);
    4763       BTS->NormalVector.Scale(-1.);
     5015      BTS->NormalVector = -1 * (*TriangleWalker)->NormalVector;
    47645016      TriangleWalker++;
    47655017    }
    47665018    if (!TriangleNrs.empty()) {
    4767       eLog() << Verbose(0) << "There have been less triangles created than removed!" << endl;
    4768     }
    4769     delete(T);  // remove the triangleset
    4770   }
    4771 
     5019      DoeLog(0) && (eLog() << Verbose(0) << "There have been less triangles created than removed!" << endl);
     5020    }
     5021    delete (T); // remove the triangleset
     5022  }
    47725023  IndexToIndex * SimplyDegeneratedTriangles = FindAllDegeneratedTriangles();
    4773   Log() << Verbose(0) << "Final list of simply degenerated triangles found, containing " << SimplyDegeneratedTriangles->size() << " triangles:" << endl;
     5024  DoLog(0) && (Log() << Verbose(0) << "Final list of simply degenerated triangles found, containing " << SimplyDegeneratedTriangles->size() << " triangles:" << endl);
    47745025  IndexToIndex::iterator it;
    47755026  for (it = SimplyDegeneratedTriangles->begin(); it != SimplyDegeneratedTriangles->end(); it++)
    4776       Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl;
    4777   delete(SimplyDegeneratedTriangles);
    4778 
     5027    DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl);
     5028  delete (SimplyDegeneratedTriangles);
    47795029  /// 5. exit
    47805030  UniquePolygonSet::iterator PolygonRunner;
    47815031  while (!ListofDegeneratedPolygons.empty()) {
    47825032    PolygonRunner = ListofDegeneratedPolygons.begin();
    4783     delete(*PolygonRunner);
     5033    delete (*PolygonRunner);
    47845034    ListofDegeneratedPolygons.erase(PolygonRunner);
    47855035  }
    47865036
    47875037  return counter;
    4788 };
     5038}
     5039;
Note: See TracChangeset for help on using the changeset viewer.