Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 267)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 268)
@@ -65,11 +65,11 @@
 #	splitPlaneStrategy leastSplits
 #	splitPlaneStrategy balancedTree
-#	splitPlaneStrategy nextPolygon
-	splitPlaneStrategy combined
+	splitPlaneStrategy nextPolygon
+#	splitPlaneStrategy combined
 #       constructionMethod rays
 	constructionMethod viewCells
 #	constructionMethod sceneGeometry
 	maxCandidates 25
-	maxViewCells 9999
+	maxViewCells 999999
 	Termination {
 		maxPolygons 0
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 267)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 268)
@@ -144,5 +144,5 @@
 	*/
 	PolygonContainer *GetPolygons();
-
+int mViewCellIdx;
 protected:
 
@@ -193,5 +193,4 @@
 		return s << A.mPlane;
 	}
-
 
 protected:
@@ -318,5 +317,5 @@
 	bool Export(const string filename);
 
-
+static bool displayDebug;
 protected:
 	
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 260)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 268)
@@ -1184,5 +1184,5 @@
 	  optInt,
 	  "-bsp_max_viewcells=",
-	  "9999");
+	  "999999");
 }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 267)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 268)
@@ -33,12 +33,14 @@
 const int FACTOR_LEAST_SPLITS = 1;
 
+int counter = 0;
+bool BspTree::displayDebug = false;
 /****************************************************************/
 /*                  class BspNode implementation                */
 /****************************************************************/
 
-BspNode::BspNode(): mParent(NULL), mPolygons(NULL)
+BspNode::BspNode(): mParent(NULL), mPolygons(NULL),mViewCellIdx(0)
 {}
 
-BspNode::BspNode(BspInterior *parent): mParent(parent), mPolygons(NULL)
+BspNode::BspNode(BspInterior *parent): mParent(parent), mPolygons(NULL),mViewCellIdx(0)
 {}
 
@@ -147,7 +149,8 @@
 								int &splits, bool storePolys)
 {
-#ifdef _Debug
+//#ifdef _Debug
+	if (BspTree::displayDebug)
 	Debug << "Splitting polygons of node " << this << " with plane " << mPlane << endl;
-#endif
+//#endif
 	bool inside = false;
 
@@ -157,4 +160,6 @@
 		polys->pop_back();
 
+		if (BspTree::displayDebug) Debug << "New polygon with plane: " << poly->GetSupportingPlane() << "\n";
+
 		// test if split is neccessary
 		int classification = poly->ClassifyPlane(mPlane);
@@ -162,5 +167,5 @@
 		Polygon3 *front_piece = NULL;
 		Polygon3 *back_piece = NULL;
-		
+	
 		switch (classification)
 		{
@@ -170,21 +175,20 @@
 					// same surface normal
 					inside = (DotProd(mPlane.mNormal, poly->GetSupportingPlane().mNormal) > 0);
-					
-				//Debug << "coincident" << endl;
+					if (BspTree::displayDebug)	
+				Debug << "coincident" << endl;
 				// discard polygons or saves them in node
 				ProcessPolygon(poly, storePolys);
 				break;
-			case Polygon3::FRONT_SIDE:
-				//Debug << "front" << endl;
+			case Polygon3::FRONT_SIDE:	if (BspTree::displayDebug)
+				Debug << "front" << endl;
 				frontPolys->push_back(poly);
 				break;
 			case Polygon3::BACK_SIDE:
-				inside = true;
-				//Debug << "back" << endl;
+				inside = true;	if (BspTree::displayDebug)
+				Debug << "back" << endl;
 				backPolys->push_back(poly);
 				break;
 			case Polygon3::SPLIT:
-				inside = true;
-				//Debug << "split " << poly << endl;
+				inside = true;				
 
 				front_piece = new Polygon3();
@@ -197,7 +201,7 @@
 				backPolys->push_back(back_piece);
 
-#ifdef _DEBUG
-				Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl;
-#endif
+//#ifdef _DEBUG
+				if (BspTree::displayDebug)Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl;
+//#endif
 				// save or discard polygons
 				ProcessPolygon(poly, storePolys);
@@ -258,5 +262,5 @@
 mRoot(NULL), 
 mIsIncremential(false),
-mStorePolys(false)
+mStorePolys(true)
 {
 	Randomize(); // initialise random generator for heuristics
@@ -440,4 +444,6 @@
 		Polygon3 *poly = new Polygon3((*fi), mesh);
 		polys.push_back(poly);
+		if (displayDebug)
+			Debug << *poly << endl;
 		polysNum ++;
 	}
@@ -531,11 +537,13 @@
 
 	long startTime = GetTime();
-
-	int counter = 0;
+displayDebug = true;
+	counter = 0;
 	Debug << "**** Starting view cell insertion ****" << endl;
 	for (it = viewCells.begin(); it != viewCells.end(); ++ it)
 	{
-		Debug << "** inserting view cell " << counter ++ << " **" << endl;
-		InsertViewCell(*it);
+		if ((counter == 12) || (counter == 14))
+		{Debug << "** inserting view cell " << counter << " **" << endl;
+		InsertViewCell(*it);}
+		counter ++;
 	}
 
@@ -609,5 +617,6 @@
 	{
 //#ifdef _DEBUG
-		Debug << "subdivision terminated at depth " << tData.mDepth << ", #polys: " << (int)tData.mPolygons->size() << endl;
+		if (displayDebug)
+			Debug << "subdivision terminated at depth " << tData.mDepth << ", #polys: " << (int)tData.mPolygons->size() << endl;
 //#endif
 
@@ -620,6 +629,7 @@
 
 			if (leaf->GetViewCell())
-				Debug << "ERROR: leaf already has view cell" << endl;
-
+				Debug << "ERROR: leaf already has view cell " << leaf->mViewCellIdx << endl;
+
+			leaf->mViewCellIdx = counter;
 			Debug << "insert view cell" << endl;
 			leaf->SetViewCell(viewCell);
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 265)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 268)
@@ -48,5 +48,5 @@
 		@param objects the intersectables the viewcells are derived from
 		@param viewCells the viewcells are returned in this container
-		@param maxViewCells if > 0, indicates the maximim number of viewcells that will be created
+		@param maxViewCells the maximum number of viewcells created. if 0 => unbounded
 	*/
 	static void DeriveViewCells(const ObjectContainer &objects, 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 265)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 268)
@@ -50,5 +50,5 @@
 		if (coplanar) 
 			(*coplanar) = true;	
-	
+		Debug << "signum is zero" << endl;
 		return a;
 	}
@@ -62,5 +62,5 @@
       (*t) = u;
     
-    return a + u * b - u * a; // NOTE: gives better precision than calclulating a + u * v
+    return a - Distance(a) * b / dv + Distance(a) * a / dv; // NOTE: gives better precision than calclulating a + u * v
     //return a + (u * v);
   }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 265)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 268)
@@ -1,5 +1,6 @@
 #include "Polygon3.h"
 #include "Mesh.h"
-
+#include "ViewCellBsp.h" // TODO: erase this
+// tolerance value for side relation
 #define SIDE_TOLERANCE 0.002f // TODO: Test different values
 
@@ -114,5 +115,5 @@
 	{
 		int side = plane.Side(*it, SIDE_TOLERANCE);
-		//Debug << "side: " << side << " " << plane.Distance(*it) << endl;
+		if (BspTree::displayDebug) Debug << "side: " << side << " " << plane.Distance(*it) << endl;
 
 		if (side > 0)
