Index: src/unittests/Makefile.am
===================================================================
--- src/unittests/Makefile.am	(revision 8725ed38a099a2b4b3d95307d79ba0bdb7474c3f)
+++ src/unittests/Makefile.am	(revision c15ca26c8aaeb433b6c80f98d5a7967ed4398a4e)
@@ -4,5 +4,5 @@
 AM_CXXFLAGS = $(CPPUNIT_CFLAGS)
 
-TESTS = ActOnAllUnitTest AnalysisBondsUnitTests AnalysisCorrelationToPointUnitTest AnalysisCorrelationToSurfaceUnitTest AnalysisPairCorrelationUnitTest BondGraphUnitTest InfoUnitTest ListOfBondsUnitTest LogUnitTest MemoryUsageObserverUnitTest MemoryAllocatorUnitTest StackClassUnitTest VectorUnitTest
+TESTS = ActOnAllUnitTest AnalysisBondsUnitTests AnalysisCorrelationToPointUnitTest AnalysisCorrelationToSurfaceUnitTest AnalysisPairCorrelationUnitTest BondGraphUnitTest InfoUnitTest ListOfBondsUnitTest LogUnitTest MemoryUsageObserverUnitTest MemoryAllocatorUnitTest StackClassUnitTest TesselationUnitTest TesselationInOutsideUnitTest VectorUnitTest
 check_PROGRAMS = $(TESTS)
 noinst_PROGRAMS = $(TESTS)
@@ -44,4 +44,10 @@
 StackClassUnitTest_LDADD = ../libmolecuilder.a
 
+TesselationUnitTest_SOURCES = tesselationunittest.cpp tesselationunittest.hpp
+TesselationUnitTest_LDADD = ../libmolecuilder.a
+
+TesselationInOutsideUnitTest_SOURCES = tesselation_insideoutsideunittest.cpp tesselation_insideoutsideunittest.hpp
+TesselationInOutsideUnitTest_LDADD = ../libmolecuilder.a
+
 VectorUnitTest_SOURCES = vectorunittest.cpp vectorunittest.hpp 
 VectorUnitTest_LDADD = ../libmolecuilder.a
Index: src/unittests/tesselation_insideoutsideunittest.cpp
===================================================================
--- src/unittests/tesselation_insideoutsideunittest.cpp	(revision c15ca26c8aaeb433b6c80f98d5a7967ed4398a4e)
+++ src/unittests/tesselation_insideoutsideunittest.cpp	(revision c15ca26c8aaeb433b6c80f98d5a7967ed4398a4e)
@@ -0,0 +1,181 @@
+/*
+ * tesselation_insideoutsideunittest.cpp
+ *
+ *  Created on: Dec 28, 2009
+ *      Author: heber
+ */
+
+using namespace std;
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+
+#include "defs.hpp"
+#include "tesselation.hpp"
+#include "tesselation_insideoutsideunittest.hpp"
+
+#define SPHERERADIUS 2.
+
+/********************************************** Test classes **************************************/
+
+// Registers the fixture into the 'registry'
+CPPUNIT_TEST_SUITE_REGISTRATION( TesselationInOutsideTest );
+
+
+void TesselationInOutsideTest::setUp()
+{
+  setVerbosity(3);
+
+  // create corners
+  class TesselPoint *Walker;
+  Walker = new TesselPoint;
+  Walker->node = new Vector(0., 0., 0.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "1");
+  Walker->nr = 1;
+  Corners.push_back(Walker);
+  Walker = new TesselPoint;
+  Walker->node = new Vector(0., 1., 0.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "2");
+  Walker->nr = 2;
+  Corners.push_back(Walker);
+  Walker = new TesselPoint;
+  Walker->node = new Vector(1., 0., 0.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "3");
+  Walker->nr = 3;
+  Corners.push_back(Walker);
+  Walker = new TesselPoint;
+  Walker->node = new Vector(1., 1., 0.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "4");
+  Walker->nr = 4;
+  Corners.push_back(Walker);
+  Walker = new TesselPoint;
+  Walker->node = new Vector(0., 0., 1.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "5");
+  Walker->nr = 5;
+  Corners.push_back(Walker);
+  Walker = new TesselPoint;
+  Walker->node = new Vector(0., 1., 1.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "6");
+  Walker->nr = 6;
+  Corners.push_back(Walker);
+  Walker = new TesselPoint;
+  Walker->node = new Vector(1., 0., 1.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "7");
+  Walker->nr = 7;
+  Corners.push_back(Walker);
+  Walker = new TesselPoint;
+  Walker->node = new Vector(1., 1., 1.);
+  Walker->Name = Malloc<char>(3, "TesselationInOutsideTest::setUp");
+  strcpy(Walker->Name, "8");
+  Walker->nr = 8;
+  Corners.push_back(Walker);
+
+  // create linkedcell
+  LinkedList = new LinkedCell(&Corners, 2.*SPHERERADIUS);
+
+  // create tesselation
+  TesselStruct = new Tesselation;
+  TesselStruct->FindStartingTriangle(SPHERERADIUS, LinkedList);
+
+  CandidateForTesselation *baseline = NULL;
+  BoundaryTriangleSet *T = NULL;
+  bool OneLoopWithoutSuccessFlag = true;
+  bool TesselationFailFlag = false;
+  while ((!TesselStruct->OpenLines.empty()) && (OneLoopWithoutSuccessFlag)) {
+    // 2a. fill all new OpenLines
+    Log() << Verbose(1) << "There are " << TesselStruct->OpenLines.size() << " open lines to scan for candidates:" << endl;
+    for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++)
+      Log() << Verbose(2) << *(Runner->second) << endl;
+
+    for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
+      baseline = Runner->second;
+      if (baseline->pointlist.empty()) {
+        T = (((baseline->BaseLine->triangles.begin()))->second);
+        Log() << Verbose(1) << "Finding best candidate for open line " << *baseline->BaseLine << " of triangle " << *T << endl;
+        TesselationFailFlag = TesselStruct->FindNextSuitableTriangle(*baseline, *T, SPHERERADIUS, LinkedList); //the line is there, so there is a triangle, but only one.
+      }
+    }
+
+    // 2b. search for smallest ShortestAngle among all candidates
+    double ShortestAngle = 4.*M_PI;
+    Log() << Verbose(1) << "There are " << TesselStruct->OpenLines.size() << " open lines to scan for the best candidates:" << endl;
+    for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++)
+      Log() << Verbose(2) << *(Runner->second) << endl;
+
+    for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
+      if (Runner->second->ShortestAngle < ShortestAngle) {
+        baseline = Runner->second;
+        ShortestAngle = baseline->ShortestAngle;
+        //Log() << Verbose(1) << "New best candidate is " << *baseline->BaseLine << " with point " << *baseline->point << " and angle " << baseline->ShortestAngle << endl;
+      }
+    }
+    if ((ShortestAngle == 4.*M_PI) || (baseline->pointlist.empty()))
+      OneLoopWithoutSuccessFlag = false;
+    else {
+      TesselStruct->AddCandidateTriangle(*baseline);
+    }
+  }
+};
+
+
+void TesselationInOutsideTest::tearDown()
+{
+  delete(LinkedList);
+  delete(TesselStruct);
+  for (LinkedNodes::iterator Runner = Corners.begin(); Runner != Corners.end(); Runner++) {
+    delete((*Runner)->node);
+    delete(*Runner);
+  }
+  Corners.clear();
+  MemoryUsageObserver::purgeInstance();
+  logger::purgeInstance();
+  errorLogger::purgeInstance();
+};
+
+/** UnitTest for Tesselation::IsInnerPoint()
+ */
+void TesselationInOutsideTest::IsInnerPointTest()
+{
+  double n[3];
+  const double boundary = 5.;
+  const double step = 1.;
+
+  // go through the mesh and check each point
+  for (n[0] = -boundary; n[0] <= boundary; n[0]+=step)
+    for (n[1] = -boundary; n[1] <= boundary; n[1]+=step)
+      for (n[2] = -boundary; n[2] <= boundary; n[2]+=step) {
+        if ( ((n[0] > 0) && (n[0] > 0) && (n[0] > 0)) && (n[0]+n[1]+n[2] <= 1.))
+          CPPUNIT_ASSERT_EQUAL( true , TesselStruct->IsInnerPoint(Vector(n[0], n[1], n[2]), LinkedList) );
+        else
+          CPPUNIT_ASSERT_EQUAL( false , TesselStruct->IsInnerPoint(Vector(n[0], n[1], n[2]), LinkedList) );
+      }
+};
+
+/********************************************** Main routine **************************************/
+
+int main(int argc, char **argv)
+{
+  // Get the top level suite from the registry
+  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
+
+  // Adds the test to the list of test to run
+  CppUnit::TextUi::TestRunner runner;
+  runner.addTest( suite );
+
+  // Change the default outputter to a compiler error format outputter
+  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
+                                                       std::cerr ) );
+  // Run the tests.
+  bool wasSucessful = runner.run();
+
+  // Return error code 1 if the one of test failed.
+  return wasSucessful ? 0 : 1;
+};
Index: src/unittests/tesselation_insideoutsideunittest.hpp
===================================================================
--- src/unittests/tesselation_insideoutsideunittest.hpp	(revision c15ca26c8aaeb433b6c80f98d5a7967ed4398a4e)
+++ src/unittests/tesselation_insideoutsideunittest.hpp	(revision c15ca26c8aaeb433b6c80f98d5a7967ed4398a4e)
@@ -0,0 +1,38 @@
+/*
+ * tesselation_insideoutsideunittest.hpp
+ *
+ *  Created on: Dec 28, 2009
+ *      Author: heber
+ */
+
+#ifndef TESSELATION_INSIDEOUTSIDEUNITTEST_HPP_
+#define TESSELATION_INSIDEOUTSIDEUNITTEST_HPP_
+
+/*********************************************** includes ***********************************/
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "linkedcell.hpp"
+#include "tesselation.hpp"
+
+/********************************************** Test classes **************************************/
+
+class TesselationInOutsideTest : public CppUnit::TestFixture
+{
+    CPPUNIT_TEST_SUITE( TesselationInOutsideTest) ;
+    CPPUNIT_TEST ( IsInnerPointTest );
+    CPPUNIT_TEST_SUITE_END();
+
+public:
+      void setUp();
+      void tearDown();
+      void IsInnerPointTest();
+
+private:
+      class Tesselation *TesselStruct;
+      LinkedNodes Corners;
+      class LinkedCell *LinkedList;
+};
+
+
+#endif /* TESSELATION_INSIDEOUTSIDEUNITTEST_HPP_ */
Index: src/unittests/tesselationunittest.cpp
===================================================================
--- src/unittests/tesselationunittest.cpp	(revision 8725ed38a099a2b4b3d95307d79ba0bdb7474c3f)
+++ src/unittests/tesselationunittest.cpp	(revision c15ca26c8aaeb433b6c80f98d5a7967ed4398a4e)
@@ -30,18 +30,18 @@
   class TesselPoint *Walker;
   Walker = new TesselPoint;
-  Walker->node = new Vector(1., 0., 0.);
-  Walker->Name = new char[3];
+  Walker->node = new Vector(1., 0., -1.);
+  Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
   strcpy(Walker->Name, "1");
   Walker->nr = 1;
   Corners.push_back(Walker);
   Walker = new TesselPoint;
-  Walker->node = new Vector(-1., 1., 0.);
-  Walker->Name = new char[3];
+  Walker->node = new Vector(-1., 1., -1.);
+  Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
   strcpy(Walker->Name, "2");
   Walker->nr = 2;
   Corners.push_back(Walker);
   Walker = new TesselPoint;
-  Walker->node = new Vector(-1., -1., 0.);
-  Walker->Name = new char[3];
+  Walker->node = new Vector(-1., -1., -1.);
+  Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
   strcpy(Walker->Name, "3");
   Walker->nr = 3;
@@ -49,5 +49,5 @@
   Walker = new TesselPoint;
   Walker->node = new Vector(-1., 0., 1.);
-  Walker->Name = new char[3];
+  Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
   strcpy(Walker->Name, "4");
   Walker->nr = 4;
@@ -59,19 +59,35 @@
   // create tesselation
   TesselStruct = new Tesselation;
-  TesselStruct->PointsOnBoundary.clear();
-  TesselStruct->LinesOnBoundary.clear();
-  TesselStruct->TrianglesOnBoundary.clear();
+  CPPUNIT_ASSERT_EQUAL( true, TesselStruct->PointsOnBoundary.empty() );
+  CPPUNIT_ASSERT_EQUAL( true, TesselStruct->LinesOnBoundary.empty() );
+  CPPUNIT_ASSERT_EQUAL( true, TesselStruct->TrianglesOnBoundary.empty() );
   TesselStruct->FindStartingTriangle(SPHERERADIUS, LinkedList);
-  bool flag = false;
 
-  LineMap::iterator baseline = TesselStruct->LinesOnBoundary.begin();
-  while (baseline != TesselStruct->LinesOnBoundary.end()) {
-    if (baseline->second->triangles.size() == 1) {
-      flag = TesselStruct->FindNextSuitableTriangle(*(baseline->second), *(((baseline->second->triangles.begin()))->second), SPHERERADIUS, LinkedList); //the line is there, so there is a triangle, but only one.
+  CandidateForTesselation *baseline = NULL;
+  BoundaryTriangleSet *T = NULL;
+  bool OneLoopWithoutSuccessFlag = true;
+  bool TesselationFailFlag = false;
+  while ((!TesselStruct->OpenLines.empty()) && (OneLoopWithoutSuccessFlag)) {
+    // 2a. fill all new OpenLines
+    for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
+      baseline = Runner->second;
+      if (baseline->pointlist.empty()) {
+        T = (((baseline->BaseLine->triangles.begin()))->second);
+        TesselationFailFlag = TesselStruct->FindNextSuitableTriangle(*baseline, *T, SPHERERADIUS, LinkedList); //the line is there, so there is a triangle, but only one.
+      }
     }
-    baseline++;
-    if ((baseline == TesselStruct->LinesOnBoundary.end()) && (flag)) {
-      baseline = TesselStruct->LinesOnBoundary.begin();   // restart if we reach end due to newly inserted lines
-      flag = false;
+
+    // 2b. search for smallest ShortestAngle among all candidates
+    double ShortestAngle = 4.*M_PI;
+    for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
+      if (Runner->second->ShortestAngle < ShortestAngle) {
+        baseline = Runner->second;
+        ShortestAngle = baseline->ShortestAngle;
+      }
+    }
+    if ((ShortestAngle == 4.*M_PI) || (baseline->pointlist.empty()))
+      OneLoopWithoutSuccessFlag = false;
+    else {
+      TesselStruct->AddCandidateTriangle(*baseline);
     }
   }
@@ -84,33 +100,11 @@
   delete(TesselStruct);
   for (LinkedNodes::iterator Runner = Corners.begin(); Runner != Corners.end(); Runner++) {
-    delete[]((*Runner)->Name);
     delete((*Runner)->node);
     delete(*Runner);
   }
   Corners.clear();
-};
-
-/** UnitTest for Tesselation::IsInnerPoint()
- */
-void TesselationTest::IsInnerPointTest()
-{
-  // true inside points
-  CPPUNIT_ASSERT_EQUAL( true, TesselStruct->IsInnerPoint(Vector(0.,0.,0.), LinkedList) );
-  CPPUNIT_ASSERT_EQUAL( true, TesselStruct->IsInnerPoint(Vector(0.5,0.,0.), LinkedList) );
-  CPPUNIT_ASSERT_EQUAL( true, TesselStruct->IsInnerPoint(Vector(0.,0.5,0.), LinkedList) );
-  CPPUNIT_ASSERT_EQUAL( true, TesselStruct->IsInnerPoint(Vector(0.,0.,0.5), LinkedList) );
-
-  // corners
-  for (LinkedNodes::iterator Runner = Corners.begin(); Runner != Corners.end(); Runner++)
-    CPPUNIT_ASSERT_EQUAL( true, TesselStruct->IsInnerPoint((*Runner), LinkedList) );
-
-  // true outside points
-  CPPUNIT_ASSERT_EQUAL( false, TesselStruct->IsInnerPoint(Vector(0.,5.,0.), LinkedList) );
-  CPPUNIT_ASSERT_EQUAL( false, TesselStruct->IsInnerPoint(Vector(0.,0.,5.), LinkedList) );
-  CPPUNIT_ASSERT_EQUAL( false, TesselStruct->IsInnerPoint(Vector(1.,1.,1.), LinkedList) );
-
-  // tricky point, there are three equally close triangles
-  CPPUNIT_ASSERT_EQUAL( false, TesselStruct->IsInnerPoint(Vector(5.,0.,0.), LinkedList) );
-
+  MemoryUsageObserver::purgeInstance();
+  logger::purgeInstance();
+  errorLogger::purgeInstance();
 };
 
Index: src/unittests/tesselationunittest.hpp
===================================================================
--- src/unittests/tesselationunittest.hpp	(revision 8725ed38a099a2b4b3d95307d79ba0bdb7474c3f)
+++ src/unittests/tesselationunittest.hpp	(revision c15ca26c8aaeb433b6c80f98d5a7967ed4398a4e)
@@ -21,5 +21,4 @@
 {
     CPPUNIT_TEST_SUITE( TesselationTest) ;
-    CPPUNIT_TEST ( IsInnerPointTest );
     CPPUNIT_TEST ( GetAllTrianglesTest );
     CPPUNIT_TEST ( ContainmentTest );
@@ -29,5 +28,4 @@
       void setUp();
       void tearDown();
-      void IsInnerPointTest();
       void GetAllTrianglesTest();
       void ContainmentTest();
