Index: src/boundary.cpp
===================================================================
--- src/boundary.cpp	(revision 7c6712a25ea23cdfa7e88b8f2293af4c65278eec)
+++ src/boundary.cpp	(revision 196a5ac4e0597225fe1d79967a945d42ddfed2e6)
@@ -1422,5 +1422,7 @@
  * @param c vector third point of triangle
  * @param Direction vector indicates up/down
+ * @param AlternativeDirection vecotr, needed in case the triangles have 90 deg angle
  * @param Halfplaneindicator double indicates whether Direction is up or down
+ * @param AlternativeIndicator doube indicates in case of orthogonal triangles which direction of AlternativeDirection is suitable
  * @param alpha double angle at a
  * @param beta double, angle at b
@@ -1430,6 +1432,6 @@
  */
 
-  void Get_center_of_sphere(Vector* Center, Vector a, Vector b, Vector c, Vector* Direction, double HalfplaneIndicator,
-      double alpha, double beta, double gamma, double RADIUS, double Umkreisradius)
+  void Get_center_of_sphere(Vector* Center, Vector a, Vector b, Vector c, Vector* Direction, Vector* AlternativeDirection,
+      double HalfplaneIndicator, double AlternativeIndicator, double alpha, double beta, double gamma, double RADIUS, double Umkreisradius)
   {
     Vector TempNormal, helper;
@@ -1437,5 +1439,5 @@
 
     *Center = a * sin(2.*alpha) + b * sin(2.*beta) + c * sin(2.*gamma) ;
-    Center->Scale(1/(sin(2*alpha) + sin(2*beta) + sin(2*gamma)));
+    Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
     // Here we calculated center of circumscribing circle, using barycentric coordinates
 
@@ -1445,7 +1447,17 @@
     helper.SubtractVector(&c);
     TempNormal.VectorProduct(&helper);
-    if (TempNormal.ScalarProduct(Direction)<0 && HalfplaneIndicator >0 || TempNormal.ScalarProduct(Direction)>0 && HalfplaneIndicator<0)
+    if (fabs(HalfplaneIndicator) < MYEPSILON)
       {
-        TempNormal.Scale(-1);
+        if ((TempNormal.ScalarProduct(AlternativeDirection) <0 and AlternativeIndicator >0) or (TempNormal.ScalarProduct(AlternativeDirection) >0 and AlternativeIndicator <0))
+          {
+            TempNormal.Scale(-1);
+          }
+      }
+    else
+      {
+        if (TempNormal.ScalarProduct(Direction)<0 && HalfplaneIndicator >0 || TempNormal.ScalarProduct(Direction)>0 && HalfplaneIndicator<0)
+          {
+            TempNormal.Scale(-1);
+          }
       }
 
@@ -1469,4 +1481,5 @@
  * @param a first point
  * @param b second point
+ * *param c atom old third point of old triangle
  * @param Candidate base point along whose bonds to start looking from
  * @param Parent point to avoid during search as its wrong direction
@@ -1482,11 +1495,9 @@
  */
 
-void Find_next_suitable_point_via_Angle_of_Sphere(atom* a, atom* b, atom* Candidate, atom* Parent,
+void Find_next_suitable_point_via_Angle_of_Sphere(atom* a, atom* b, atom* c, atom* Candidate, atom* Parent,
     int RecursionLevel, Vector *Chord, Vector *direction1, Vector *OldNormal, Vector ReferencePoint,
     atom*& Opt_Candidate, double *Storage, const double RADIUS, molecule* mol)
 {
-
-  cout << Verbose(1) << "Candidate is "<< Candidate->nr << endl;
-  cout << "ReferencePoint is " << ReferencePoint.x[0] << " "<< ReferencePoint.x[1] << " "<< ReferencePoint.x[2] << " "<< endl;
+  //cout << "ReferencePoint is " << ReferencePoint.x[0] << " "<< ReferencePoint.x[1] << " "<< ReferencePoint.x[2] << " "<< endl;
   /* OldNormal is normal vector on the old triangle
    * direction1 is normal on the triangle line, from which we come, as well as on OldNormal.
@@ -1501,5 +1512,5 @@
   double CurrentEpsilon = 0.1;
   double alpha, beta, gamma, SideA, SideB, SideC, sign, Umkreisradius, Restradius, Distance;
-  double BallAngle;
+  double BallAngle, AlternativeSign;
   atom *Walker; // variable atom point
 
@@ -1522,5 +1533,6 @@
   gamma = dif_a.Angle(&dif_b);
 
-  if (a != Candidate and b != Candidate)
+
+  if (a != Candidate and b != Candidate and c != Candidate)
     {
 
@@ -1532,23 +1544,42 @@
       if (Umkreisradius < RADIUS) //Checking whether ball will at least rest on points.
         {
+          cout << Verbose(1) << "Candidate is "<< *Candidate << endl;
           sign = AngleCheck.ScalarProduct(direction1);
-          sign /= fabs(sign); // +1 if in direction of triangle plane, -1 if in other direction...
-
-          Get_center_of_sphere(&Mittelpunkt, (a->x), (b->x), (Candidate->x), OldNormal, sign, alpha, beta, gamma, RADIUS, Umkreisradius);
+          if (fabs(sign)<MYEPSILON)
+            {
+              if (AngleCheck.ScalarProduct(OldNormal)<0)
+                {
+                  sign =0;
+                  AlternativeSign=1;
+                }
+              else
+                {
+                  sign =0;
+                  AlternativeSign=-1;
+                }
+            }
+          else
+            {
+              sign /= fabs(sign);
+            }
+
+
+
+          Get_center_of_sphere(&Mittelpunkt, (a->x), (b->x), (Candidate->x), OldNormal, direction1, sign, AlternativeSign, alpha, beta, gamma, RADIUS, Umkreisradius);
 
           AngleCheck.CopyVector(&ReferencePoint);
           AngleCheck.Scale(-1);
-          cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
+          //cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
           AngleCheck.AddVector(&Mittelpunkt);
-          cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
+          //cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
 
           BallAngle = AngleCheck.Angle(OldNormal);
-          cout << "direction1 is " << direction1->x[0] <<" "<< direction1->x[1] <<" "<< direction1->x[2] <<" " << endl;
-          cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
-          sign = AngleCheck.ScalarProduct(direction1);
-          sign /= fabs(sign);
-
-
-          if (sign >0)
+
+          //cout << "direction1 is " << direction1->x[0] <<" "<< direction1->x[1] <<" "<< direction1->x[2] <<" " << endl;
+          //cout << "AngleCheck is " << AngleCheck.x[0] << " "<< AngleCheck.x[1] << " "<< AngleCheck.x[2] << " "<< endl;
+
+          cout << Verbose(1) << "BallAngle is " << BallAngle << " Sign is " << sign << endl;
+
+          if (AngleCheck.ScalarProduct(direction1) >=0)
             {
               if (Storage[0]< -1.5) // first Candidate at all
@@ -1558,6 +1589,7 @@
                   Opt_Candidate = Candidate;
                   Storage[0] = sign;
-                  Storage[1] = BallAngle;
-                  cout << "Angle is " << Storage[1] << ", Halbraum ist "
+                  Storage[1] = AlternativeSign;
+                  Storage[2] = BallAngle;
+                  cout << "Angle is " << Storage[2] << ", Halbraum ist "
                     << Storage[0] << endl;
 
@@ -1566,19 +1598,19 @@
               else
                 {
-                  if ( Storage[1] > BallAngle)
+                  if ( Storage[2] > BallAngle)
                     {
                       cout << "Next better candidate is " << *Candidate << " with ";
                       Opt_Candidate = Candidate;
                       Storage[0] = sign;
-                      Storage[1] = BallAngle;
-                      cout << "Angle is " << Storage[1] << ", Halbraum ist "
+                      Storage[1] = AlternativeSign;
+                      Storage[2] = BallAngle;
+                      cout << "Angle is " << Storage[2] << ", Halbraum ist "
                         << Storage[0] << endl;
                     }
                   else
                     {
-                      if (DEBUG)
-                        {
-                          cout << "Looses to better candidate" << endl;
-                        }
+                      //if (DEBUG)
+                        cout << "Looses to better candidate" << endl;
+
                     }
                 }
@@ -1586,26 +1618,24 @@
             else
               {
-                cout << "Refused due to sign which is " << sign << endl;
+                //if (DEBUG)
+                  cout << "Refused due to bad direction of ball centre." << endl;
               }
           }
         else
           {
-            if (DEBUG)
-              {
-                cout << "Doesn't satisfy requirements for circumscribing circle" << endl;
-              }
+            //if (DEBUG)
+              cout << "Doesn't satisfy requirements for circumscribing circle" << endl;
           }
       }
     else
       {
-        if (DEBUG)
-          {
-            cout << "identisch mit Ursprungslinie" << endl;
-          }
+        //if (DEBUG)
+          cout << "identisch mit Ursprungslinie" << endl;
+
       }
 
 
 
-  if (RecursionLevel < 7) // Five is the recursion level threshold.
+  if (RecursionLevel < 9) // Seven is the recursion level threshold.
     {
       for (int i = 0; i < mol->NumberOfBondsPerAtom[Candidate->nr]; i++) // go through all bond
@@ -1619,5 +1649,5 @@
           else
             {
-              Find_next_suitable_point_via_Angle_of_Sphere(a, b, Walker, Candidate, RecursionLevel
+              Find_next_suitable_point_via_Angle_of_Sphere(a, b, c, Walker, Candidate, RecursionLevel
                   + 1, Chord, direction1, OldNormal, ReferencePoint, Opt_Candidate, Storage, RADIUS,
                   mol); //call function again
@@ -1910,9 +1940,10 @@
   ofstream *tempstream = NULL;
   char filename[255];
-  //atom* Walker;
-
-  double Storage[2];
+  atom* OldThirdPoint;
+
+  double Storage[3];
   Storage[0] = -2.; // This direction is either +1 or -1 one, so any result will take precedence over initial values
-  Storage[1] = 9999999.; // This is also lower then any value produced by an eligible atom, which are all positive
+  Storage[1] = -2.; // This is also lower then any value produced by an eligible atom, which are all positive
+  Storage[2] = 9999999.;
   atom* Opt_Candidate = NULL;
   Vector Opt_Mittelpunkt;
@@ -1925,4 +1956,5 @@
           && T.endpoints[i]->node->nr != Line.endpoints[1]->node->nr)
         {
+          OldThirdPoint = T.endpoints[i]->node;
           helper.SubtractVector(&T.endpoints[i]->node->x);
           break;
@@ -1957,20 +1989,25 @@
 
     Umkreismittelpunkt = (T.endpoints[0]->node->x) * sin(2.*alpha) + T.endpoints[1]->node->x * sin(2.*beta) + (T.endpoints[2]->node->x) * sin(2.*gamma) ;
-    cout << "UmkreisMittelpunkt is " << Umkreismittelpunkt.x[0] << " "<< Umkreismittelpunkt.x[1] << " "<< Umkreismittelpunkt.x[2] << " "<< endl;
+    //cout << "UmkreisMittelpunkt is " << Umkreismittelpunkt.x[0] << " "<< Umkreismittelpunkt.x[1] << " "<< Umkreismittelpunkt.x[2] << " "<< endl;
     Umkreismittelpunkt.Scale(1/(sin(2*alpha) + sin(2*beta) + sin(2*gamma)));
     cout << "UmkreisMittelpunkt is " << Umkreismittelpunkt.x[0] << " "<< Umkreismittelpunkt.x[1] << " "<< Umkreismittelpunkt.x[2] << " "<< endl;
+    cout << " We look over line " << Line << " in direction " << direction1.x[0] << " " << direction1.x[1] << " " << direction1.x[2] << " " << endl;
+    cout << " Old Normal is " <<  (T.NormalVector.x)[0] << " " << T.NormalVector.x[1] << " " << (T.NormalVector.x)[2] << " " << endl;
 
 
   cout << Verbose(1) << "Looking for third point candidates for triangle ... " << endl;
 
-  Find_next_suitable_point_via_Angle_of_Sphere(Line.endpoints[0]->node, Line.endpoints[1]->node,
+  Find_next_suitable_point_via_Angle_of_Sphere(Line.endpoints[0]->node, Line.endpoints[1]->node, OldThirdPoint,
       Line.endpoints[0]->node, Line.endpoints[1]->node, 0, &Chord, &direction1,
       &(T.NormalVector), Umkreismittelpunkt, Opt_Candidate, Storage, RADIUS, mol);
-  Find_next_suitable_point_via_Angle_of_Sphere(Line.endpoints[0]->node, Line.endpoints[1]->node,
+  Find_next_suitable_point_via_Angle_of_Sphere(Line.endpoints[0]->node, Line.endpoints[1]->node, OldThirdPoint,
       Line.endpoints[1]->node, Line.endpoints[0]->node, 0, &Chord, &direction1,
       &(T.NormalVector), Umkreismittelpunkt, Opt_Candidate, Storage, RADIUS, mol);
 
 
-  if ((TrianglesOnBoundaryCount % 10) == 0)
+      cout << "Letzter Winkel bei " << TrianglesOnBoundaryCount << " Winkel ist " << Storage[2] << endl;
+
+
+  if ((TrianglesOnBoundaryCount % 1) == 0)
     {
     sprintf(filename, "testEnvelope-%d.dat", TriangleFilesWritten);
@@ -1982,5 +2019,5 @@
   }
 
-  if (TrianglesOnBoundaryCount >1000 )
+  if (TrianglesOnBoundaryCount >189 and TrianglesOnBoundaryCount < 200 )
     {
       cout << Verbose(1)
@@ -2010,5 +2047,7 @@
 
   if ((BTS->NormalVector.ScalarProduct(&(T.NormalVector)) < 0 && Storage[0] > 0)  ||
-      (BTS->NormalVector.ScalarProduct(&(T.NormalVector)) > 0 && Storage[0] < 0))
+      (BTS->NormalVector.ScalarProduct(&(T.NormalVector)) > 0 && Storage[0] < 0) ||
+      (fabs(Storage[0]) < MYEPSILON && Storage[1]*BTS->NormalVector.ScalarProduct(&direction1) < 0) )
+
     {
       BTS->NormalVector.Scale(-1);
@@ -2019,5 +2058,5 @@
 
 void Find_second_point_for_Tesselation(atom* a, atom* Candidate, atom* Parent,
-    int RecursionLevel, Vector Oben, atom*& Opt_Candidate, double Storage[2],
+    int RecursionLevel, Vector Oben, atom*& Opt_Candidate, double Storage[3],
     molecule* mol, double RADIUS)
 {
@@ -2041,10 +2080,10 @@
           if (AngleCheck.Angle(&Oben) < Storage[0])
             {
-              //cout << Verbose(1) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]);
+              //cout << Verbose(1) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[2]);
               cout << "Next better candidate is " << *Candidate
                   << " with distance " << norm << ".\n";
               Opt_Candidate = Candidate;
               Storage[0] = AngleCheck.Angle(&Oben);
-              //cout << Verbose(1) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[1]);
+              //cout << Verbose(1) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[2]);
             }
           else
@@ -2125,8 +2164,9 @@
   cout << Verbose(1) << "Coordinates of start atom " << *FirstPoint << ": "
       << FirstPoint->x.x[0] << endl;
-  double Storage[2];
+  double Storage[3];
   atom* Opt_Candidate = NULL;
   Storage[0] = 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.
   Storage[1] = 999999.; // This will be an angle looking for the third point.
+  Storage[2] = 999999.;
   cout << Verbose(1) << "Number of Bonds: "
       << mol->NumberOfBondsPerAtom[FirstPoint->nr] << endl;
@@ -2145,4 +2185,5 @@
   Storage[0] = -2.; // This will indicate the quadrant.
   Storage[1] = 9999999.; // This will be an angle looking for the third point.
+  Storage[2] = 9999999.;
 
   Chord.CopyVector(&(FirstPoint->x)); // bring into calling function
@@ -2151,14 +2192,15 @@
 
   cout << Verbose(1) << "Looking for third point candidates \n";
+  cout << Verbose(1) << " In direction " << helper.x[0] << " " << helper.x[1] << " " << helper.x[2] << " " << endl;
   // look in one direction of baseline for initial candidate
   Opt_Candidate = NULL;
   CenterOfFirstLine.CopyVector(&Chord);
-  CenterOfFirstLine.Scale(-0.5);
+  CenterOfFirstLine.Scale(0.5);
   CenterOfFirstLine.AddVector(&(SecondPoint->x));
 
-  Find_next_suitable_point_via_Angle_of_Sphere(FirstPoint, SecondPoint, SecondPoint, FirstPoint, 0,
+  Find_next_suitable_point_via_Angle_of_Sphere(FirstPoint, SecondPoint, SecondPoint, SecondPoint, FirstPoint, 0,
       &Chord, &helper, &Oben, CenterOfFirstLine,  Opt_Candidate, Storage, RADIUS, mol);
   // look in other direction of baseline for possible better candidate
-  Find_next_suitable_point_via_Angle_of_Sphere(FirstPoint, SecondPoint, FirstPoint, SecondPoint, 0,
+  Find_next_suitable_point_via_Angle_of_Sphere(FirstPoint, SecondPoint, SecondPoint, FirstPoint, SecondPoint, 0,
       &Chord, &helper, &Oben, CenterOfFirstLine, Opt_Candidate, Storage, RADIUS, mol);
   cout << Verbose(1) << "Third Point is " << *Opt_Candidate << endl;
