Index: src/tesselation.cpp
===================================================================
--- src/tesselation.cpp	(revision 8560986364b882d262e113ac9b52ce6d6b7f4261)
+++ src/tesselation.cpp	(revision fad93cd290889e66970858cf89cbc54519c70742)
@@ -4127,109 +4127,86 @@
   Info FunctionInfo(__func__);
 
-  /// 1. Find all simply degenerated triangles and sort into a list with each endpoint as key
-  map<int, int> * SimplyDegeneratedTriangles = FindAllDegeneratedTriangles();
-
-  /// 2. Go through all lines not contained in degenerated triangles.
-  UniquePolygonSet ListofFours;
-  UniquePolygonSet TestedPolygons;
-  for (LineMap::const_iterator LineRunner = LinesOnBoundary.begin(); LineRunner != LinesOnBoundary.end(); LineRunner++) {
-    Log() << Verbose(1) << "Current Line is: " << *(LineRunner->second) << endl;
-
-    BoundaryPolygonSet *Four = new BoundaryPolygonSet;
-
-    /// 2a. Get all four endpoints of the two connected triangles.
-    Four->FillPolygonFromTrianglesOfLine((LineRunner->second));
-    if (Four->endpoints.size() != 4) {
-      Log() << Verbose(0) << "REJECT: There were no four endpoints:" << *Four << endl;
-      delete(Four);
-      continue;
-    }
-
-    /// 2b. Get the triangles of all those endpoints
-    TriangleSet *T = Four->GetAllContainedTrianglesFromEndpoints();
-
-    /// 2c1. Check whether all triangles have (besides sign) same normal vector)
-    TriangleSet::const_iterator Runner = T->begin();
-    Vector NormalVector;
-    NormalVector.CopyVector(&(*Runner)->NormalVector);
-    Runner++;
-    bool IsPlanar = true;
-    for (; Runner != T->end(); Runner++) {
-      if (fabs(fabs(NormalVector.ScalarProduct(&(*Runner)->NormalVector)) - 1.) > MYEPSILON)  // check whether all NormalVectors are parallel
-        IsPlanar = false;
-    }
-
-    /// 2c2. check whether triangles are not already simply degenerated.
-    size_t DegeneratedCount = 0;
-    for (TriangleSet::const_iterator Runner = T->begin(); Runner != T->end(); Runner++) {
-      if (SimplyDegeneratedTriangles->find((*Runner)->Nr) != SimplyDegeneratedTriangles->end())
-        DegeneratedCount++;
-    }
-    Log() << Verbose(1) << "Found " << DegeneratedCount << " degenerated triangles." << endl;
-
-    if (IsPlanar) {
-      if(DegeneratedCount == 0) {
-        /// 2c. Find all pairs of those triangles that contain the four endpoints
-        const int counter = CountTrianglePairContainingPolygon(Four, T);
-        Log() << Verbose(1) << "There are " << counter << " pairs for this polygon of four." << endl;
-        if (counter %3 == 0) {
-          /// 2d. Check the number of pairs, if greater 1, we have a degenerated polygon (mark down for later use)
-          pair < UniquePolygonSet::iterator, bool > Tester = ListofFours.insert(Four);
-          if (Tester.second) {
-            Log() << Verbose(0) << "ACCEPT: We have a degenerated polygon." << *Four << endl;
-          } else {
-            Log() << Verbose(0) << "REJECT: Degenerated polygon already present." << *Four << endl;
-            delete(Four);
+  /// 2. Go through all BoundaryPointSet's, check their triangles' NormalVector
+  set < BoundaryPointSet *> EndpointCandidateList;
+  pair < set < BoundaryPointSet *>::iterator, bool > InsertionTester;
+  pair < map < int, Vector *>::iterator, bool > TriangleInsertionTester;
+  for (PointMap::const_iterator Runner = PointsOnBoundary.begin(); Runner != PointsOnBoundary.end(); Runner++) {
+    Log() << Verbose(0) << "Current point is " << *Runner->second << "." << endl;
+    map < int, Vector *> TriangleVectors;
+    // gather all NormalVectors
+    Log() << Verbose(1) << "Gathering triangles ..." << endl;
+    for (LineMap::const_iterator LineRunner = (Runner->second)->lines.begin(); LineRunner != (Runner->second)->lines.end(); LineRunner++)
+      for (TriangleMap::const_iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) {
+        TriangleInsertionTester = TriangleVectors.insert( pair< int, Vector *> ((TriangleRunner->second)->Nr, &((TriangleRunner->second)->NormalVector)) );
+        if (TriangleInsertionTester.second)
+          Log() << Verbose(1) << " Adding triangle " << *(TriangleRunner->second) << " to triangles to check-list." << endl;
+      }
+    // check whether there are two that are parallel
+    Log() << Verbose(1) << "Finding two parallel triangles ..." << endl;
+    for (map < int, Vector *>::iterator VectorWalker = TriangleVectors.begin(); VectorWalker != TriangleVectors.end(); VectorWalker++)
+      for (map < int, Vector *>::iterator VectorRunner = VectorWalker; VectorRunner != TriangleVectors.end(); VectorRunner++)
+        if (VectorWalker != VectorRunner) { // skip equals
+          const double SCP = VectorWalker->second->ScalarProduct(VectorRunner->second);  // ScalarProduct should result in -1. for degenerated triangles
+          Log() << Verbose(1) << "Checking " << *VectorWalker->second<< " against " << *VectorRunner->second << ": " << SCP << endl;
+          if (fabs(SCP + 1.) < ParallelEpsilon) {
+            InsertionTester = EndpointCandidateList.insert((Runner->second));
+            if (InsertionTester.second)
+              Log() << Verbose(0) << " Adding " << *Runner->second << " to endpoint candidate list." << endl;
+            // and break out of both loops
+            VectorWalker = TriangleVectors.end();
+            VectorRunner = TriangleVectors.end();
+            break;
           }
+        }
+  }
+
+  /// 3. Find connected endpoint candidates and put them into a polygon
+  UniquePolygonSet ListofDegeneratedPolygons;
+  BoundaryPointSet *Walker = NULL;
+  BoundaryPointSet *OtherWalker = NULL;
+  BoundaryPolygonSet *Current = NULL;
+  stack <BoundaryPointSet*> ToCheckConnecteds;
+  while (!EndpointCandidateList.empty()) {
+    Walker = *(EndpointCandidateList.begin());
+    if (Current == NULL) {  // create a new polygon with current candidate
+      Log() << Verbose(0) << "Starting new polygon set at point " << *Walker << endl;
+      Current = new BoundaryPolygonSet;
+      Current->endpoints.insert(Walker);
+      EndpointCandidateList.erase(Walker);
+      ToCheckConnecteds.push(Walker);
+    }
+
+    // go through to-check stack
+    while (!ToCheckConnecteds.empty()) {
+      Walker = ToCheckConnecteds.top(); // fetch ...
+      ToCheckConnecteds.pop(); // ... and remove
+      for (LineMap::const_iterator LineWalker = Walker->lines.begin(); LineWalker != Walker->lines.end(); LineWalker++) {
+        OtherWalker = (LineWalker->second)->GetOtherEndpoint(Walker);
+        Log() << Verbose(1) << "Checking " << *OtherWalker << endl;
+        set < BoundaryPointSet *>::iterator Finder = EndpointCandidateList.find(OtherWalker);
+        if (Finder != EndpointCandidateList.end()) {  // found a connected partner
+          Log() << Verbose(1) << " Adding to polygon." << endl;
+          Current->endpoints.insert(OtherWalker);
+          EndpointCandidateList.erase(Finder);  // remove from candidates
+          ToCheckConnecteds.push(OtherWalker);  // but check its partners too
         } else {
-          Log() << Verbose(0) << "REJECT: Seems to be the edge line of a degenerated polygon only." << *Four << endl;
-          delete(Four);
+          Log() << Verbose(1) << " is not connected to " << *Walker << endl;
         }
-      } else {
-        Log() << Verbose(0) << "REJECT: This polygon contains some simply degenerated triangles." << endl;
-        delete(Four);
       }
-    } else {
-      Log() << Verbose(0) << "REJECT: This polygon does not lay on a plane." << endl;
-      delete(Four);
-    }
-
-    delete(T);
-  }
-
-  /// 3. Combine all edge-connected degenerated polygons
-  {
-    UniquePolygonSet::iterator PolygonWalker;  // is the inner iterator
-    UniquePolygonSet::iterator PolygonSprinter; // is the inner advanced iterator
-    for (UniquePolygonSet::iterator PolygonRunner = ListofFours.begin(); PolygonRunner != ListofFours.end(); PolygonRunner++) {
-      const BoundaryPolygonSet *P1 = *PolygonRunner;
-      Log() << Verbose(1) << "Current Polygon is : " << *P1 << endl;
-      PolygonWalker = PolygonRunner;
-      PolygonWalker++;
-      PolygonSprinter = PolygonWalker;
-      while (PolygonWalker != ListofFours.end()) {
-        BoundaryPolygonSet *P2 = *PolygonWalker;
-        Log() << Verbose(1) << "Current Other Polygon is : " << *P2 << endl;
-        PolygonSprinter++;
-        Log() << Verbose(1) << "Are " << *P1 << " and " << *P2 << " edge-connected?" << endl;
-        if (ArePolygonsEdgeConnected((*PolygonRunner), (P2))) { // if connected
-          Log() << Verbose(0) << "Yes!" << endl;
-          CombinePolygons((*PolygonRunner), (P2)); // combined and ...
-          ListofFours.erase(PolygonWalker); // ... remove from list
-          Log() << Verbose(1) << "  New current Polygon is : " << *P1 << endl;
-        } else {
-          Log() << Verbose(0) << "No." << endl;
-        }
-        PolygonWalker = PolygonSprinter;
-      }
-    }
-  }
-
-  Log() << Verbose(0) << "The following degenerated polygons have been found: " << endl;
-  for (UniquePolygonSet::iterator PolygonRunner = ListofFours.begin(); PolygonRunner != ListofFours.end(); PolygonRunner++)
+    }
+
+    Log() << Verbose(0) << "Final polygon is " << *Current << endl;
+    ListofDegeneratedPolygons.insert(Current);
+    Current = NULL;
+  }
+
+  const int counter = ListofDegeneratedPolygons.size();
+
+  Log() << Verbose(0) << "The following " << counter << " degenerated polygons have been found: " << endl;
+  for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++)
     Log() << Verbose(0) << " " << **PolygonRunner << endl;
 
   /// 4. Go through all these degenerated polygons
-  for (UniquePolygonSet::iterator PolygonRunner = ListofFours.begin(); PolygonRunner != ListofFours.end(); PolygonRunner++) {
+  for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++) {
     stack <int> TriangleNrs;
     Vector NormalVector;
@@ -4261,5 +4238,5 @@
     TriangleWalker = T->begin();
     while (TriangleWalker != T->end()) {  // go through all front triangles
-      Log() << Verbose(1) << " Re-creating triangle " << **TriangleWalker << " with NormalVector " << BTS->NormalVector << endl;
+      Log() << Verbose(1) << " Re-creating triangle " << **TriangleWalker << " with NormalVector " << (*TriangleWalker)->NormalVector << endl;
       for (int i = 0; i < 3; i++)
         AddTesselationPoint((*TriangleWalker)->endpoints[i]->node, i);
@@ -4267,4 +4244,6 @@
       AddTesselationLine(TPS[0], TPS[2], 1);
       AddTesselationLine(TPS[1], TPS[2], 2);
+      if (TriangleNrs.empty())
+        eLog() << Verbose(0) << "No more free triangle numbers!" << endl;
       BTS = new BoundaryTriangleSet(BLS, TriangleNrs.top()); // copy triangle ...
       AddTesselationTriangle(); // ... and add
@@ -4272,7 +4251,4 @@
       BTS->NormalVector.CopyVector(&(*TriangleWalker)->NormalVector);
       BTS->NormalVector.Scale(-1.);
-      /// 4d. Add all degenerated triangles to the list of simply degenerated triangles
-      SimplyDegeneratedTriangles->insert(pair <int, int> ((*TriangleWalker)->Nr, BTS->Nr) );
-      SimplyDegeneratedTriangles->insert(pair <int, int> (BTS->Nr, (*TriangleWalker)->Nr) );
       TriangleWalker++;
     }
@@ -4283,19 +4259,19 @@
   }
 
+  map<int, int> * SimplyDegeneratedTriangles = FindAllDegeneratedTriangles();
   Log() << Verbose(0) << "Final list of simply degenerated triangles found, containing " << SimplyDegeneratedTriangles->size() << " triangles:" << endl;
   map<int,int>::iterator it;
   for (it = SimplyDegeneratedTriangles->begin(); it != SimplyDegeneratedTriangles->end(); it++)
       Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl;
+  delete(SimplyDegeneratedTriangles);
 
   /// 5. exit
   UniquePolygonSet::iterator PolygonRunner;
-  while (!ListofFours.empty()) {
-    PolygonRunner = ListofFours.begin();
+  while (!ListofDegeneratedPolygons.empty()) {
+    PolygonRunner = ListofDegeneratedPolygons.begin();
     delete(*PolygonRunner);
-    ListofFours.erase(PolygonRunner);
-  }
-
-  const int counter = SimplyDegeneratedTriangles->size();
-  delete(SimplyDegeneratedTriangles);
+    ListofDegeneratedPolygons.erase(PolygonRunner);
+  }
+
   return counter;
 };
Index: src/tesselation.hpp
===================================================================
--- src/tesselation.hpp	(revision 8560986364b882d262e113ac9b52ce6d6b7f4261)
+++ src/tesselation.hpp	(revision fad93cd290889e66970858cf89cbc54519c70742)
@@ -47,4 +47,6 @@
 #define Raster3DSuffix ".r3d"
 #define VRMLSUffix ".wrl"
+
+#define ParallelEpsilon 1e-3
 
 // ======================================================= some template functions =========================================
