Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 238)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 239)
@@ -62,8 +62,8 @@
 	splitPlaneStrategy balancedTree
 #	splitPlaneStrategy nextPolygon
-#	constructionMethod viewCells
-	constructionMethod sceneGeometry
+	constructionMethod viewCells
+#	constructionMethod sceneGeometry
 	maxCandidates 20
-	maxViewCells 100
+	maxViewCells 10
 	Termination {
 		maxPolygons 4
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 237)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 239)
@@ -41,5 +41,5 @@
 float ViewCell::IntersectionComplexity() 
 {
-	return mMesh->mFaces.size();
+	return (float)mMesh->mFaces.size();
 }
 
@@ -56,10 +56,10 @@
 void ViewCell::DeriveViewCells(const ObjectContainer &objects, 
 							   ViewCellContainer &viewCells, 
-							   const int max)
+							   const int maxViewCells)
 {
 	// maximal max viewcells
-	int n = max > 0 ? std::min((int)objects.size(), max) : (int)objects.size();
+	int limit = maxViewCells > 0 ? std::min((int)objects.size(), maxViewCells) : (int)objects.size();
 
-	for (int i = 0; i < n; ++i)
+	for (int i = 0; i < limit; ++i)
 	{
 		Intersectable *object = objects[i];
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 238)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 239)
@@ -254,6 +254,7 @@
 		@param tData traversal data also holding node to be subdivided
 		@param viewCell the view cell that will be represented with this part of the Bsp tree.
-	*/
-	void Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell = NULL);
+		@returns new root of the subtree
+	*/
+	BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell = NULL);
 
 	/** Selects a splitting plane. 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 238)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 239)
@@ -11,4 +11,6 @@
 #include <iomanip>
 
+#define INITIAL_TEST_VAL 999999// unreachable high initial value for heuristic evaluation
+
 int BspTree::sTermMaxPolygons = 10;
 int BspTree::sTermMaxDepth = 20;
@@ -96,9 +98,7 @@
 	{
 		Polygon3 *poly = polys->back();
-
 		polys->pop_back();
 
-		Debug << (*poly);
-
+		// test if split is neccessary
 		int result = poly->ClassifyPlane(mPlane);
 
@@ -109,12 +109,9 @@
 		{
 			case Polygon3::COINCIDENT:
-				Debug << "coincident\n";
 				break; // do nothing
 			case Polygon3::FRONT_SIDE:
-				Debug << "front\n";
 				frontPolys->push_back(poly);
 				break;
 			case Polygon3::BACK_SIDE:
-				Debug << "back\n";
 				backPolys->push_back(poly);
 				break;
@@ -123,16 +120,13 @@
 				back_piece = new Polygon3();
 
-				Debug << "\n\n**************SPLIT***************\n";
-				Debug << "Split plane: " << mPlane << "\noriginal " << (*poly) << endl;
-
 				//-- split polygon
 				poly->Split(&mPlane, front_piece, back_piece, splits);
-			
-				Debug << "new front " << (*front_piece) << endl;
-				Debug << "new back " << (*back_piece) << endl;
 
 				frontPolys->push_back(front_piece);
 				backPolys->push_back(back_piece);
 				
+#ifdef _DEBUG
+				Debug << "split " << poly << endl << front << endl << back << endl;
+#endif
 				// don't need polygon anymore
 				DEL_PTR(poly);
@@ -169,7 +163,6 @@
 /****************************************************************/
 
-BspTree::BspTree(): mTermMaxPolygons(0), mTermMaxDepth(0)
-{
-	mRoot = new BspLeaf();
+BspTree::BspTree(): mTermMaxPolygons(0), mTermMaxDepth(0), mRoot(NULL)
+{
 	Randomize(); // initialise random generator for heuristics
 }
@@ -267,6 +260,5 @@
 
 void BspTree::InsertViewCell(ViewCell *viewCell)
-{
-	Debug << "Inserting view cells\n";
+{	
 	std::stack<BspTraversalData> tStack;
 	
@@ -275,5 +267,7 @@
 	CopyMesh2Polygons(viewCell->GetMesh(), polys);
 
-	tStack.push(BspTraversalData(mRoot, NULL, &polys, 0));
+	BspNode *firstNode = mRoot ? mRoot : new BspLeaf(); // traverse tree or create new one
+
+	tStack.push(BspTraversalData(firstNode, NULL, &polys, 0));
 
 	while (!tStack.empty())
@@ -283,14 +277,15 @@
 
 	    tStack.pop();
-	
+				
         if (!tData.mNode->IsLeaf())
 		{
 			BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
 
-			// filter polygons down the tree.
+			//-- filter view cell polygons down the tree until a leaf is reached
 			PolygonContainer *frontPolys = new PolygonContainer();
 			PolygonContainer *backPolys = new PolygonContainer();
 
 			int splits = 0;
+			// split viecell polygons with respect to split plane
 			interior->SplitPolygons(tData.mPolygons, frontPolys, backPolys, splits);
 			mStat.splits += splits;
@@ -309,7 +304,10 @@
 				delete backPolys;
 		}
-		else // reached leaf => subdivide
-		{
-			Subdivide(tStack, tData, NULL);
+		else // reached leaf => subdivide current viewcell
+		{
+			BspNode *root = Subdivide(tStack, tData, NULL);
+
+			if (!mRoot) // take as root if there is none yet
+				mRoot = root;				
 		}
 	}
@@ -349,7 +347,6 @@
 	ObjectContainer::const_iterator it, it_end = objects.end();
 
-	int limit = maxPolys != 0 ? min((int)objects.size(), maxPolys) : (int)objects.size();
-
-	//for (it = objects.begin(); it != it_end; ++ it)
+	int limit = (maxPolys > 0) ? min((int)objects.size(), maxPolys) : (int)objects.size();
+
 	for (int i = 0; i < limit; ++i)
 	{
@@ -376,5 +373,5 @@
 	}
 
-	Debug << "number of polygons: " << polys.size() << endl;
+	Debug << "Number of polygons: " << polys.size() << endl;
 }
 
@@ -394,5 +391,6 @@
 	Copy2PolygonSoup(objects, *polys, 100);
 
-	BspTraversalData tData(mRoot, mRoot->GetParent(), polys, 0);
+	BspTraversalData tData(new BspLeaf(), mRoot->GetParent(), polys, 0);
+
 	tStack.push(tData);
 
@@ -401,15 +399,18 @@
 		tData = tStack.top();
 	    tStack.pop();
-		
-		Subdivide(tStack, tData);
-	}
-}
-
-void BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell)
-{
-	//Debug << "Subdividing node\n";
+
+		// subdivide leaf node
+		BspNode *root = Subdivide(tStack, tData);
+
+		if (!mRoot)
+			mRoot = root;
+	}
+}
+
+BspNode * BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell)
+{
 	PolygonContainer *backPolys = new PolygonContainer();
 	PolygonContainer *frontPolys = new PolygonContainer();
-int polySize = tData.mPolygons->size();
+
 	BspNode *node = SubdivideNode(dynamic_cast<BspLeaf *>(tData.mNode),
 								  tData.mParent,
@@ -419,5 +420,5 @@
 								  frontPolys,
 								  backPolys);
-Debug << "Level " << tData.mDepth << ", all: " << polySize << ", front: " << frontPolys->size() << ", back: " << backPolys->size() << endl;
+
 	if (!node->IsLeaf()) // node was subdivided
 	{
@@ -434,12 +435,13 @@
 		Polygon3::DeletePolygons(tData.mPolygons);
 	}
+
+	return node;
 }
 
 Plane3 BspTree::SelectPlane(PolygonContainer *polygons)  const
 {
-	// most simple strategy: just take next polygon
+	// simple strategy: just take next polygon
 	if (sSplitPlaneStrategy == NEXT_POLYGON)
 	{
-		Debug << "simple plane selection\n";
 		return polygons->front()->GetSupportingPlane();
 	}
@@ -450,8 +452,5 @@
 Plane3 BspTree::SelectPlaneHeuristics(PolygonContainer *polygons, int maxTests) const
 {
-	Debug << "selecting plane heuristics\n";
-
-	int bestValue = 999999;
-
+	int bestValue = INITIAL_TEST_VAL;
 	Plane3 *bestPlane = NULL;
 	
@@ -462,5 +461,5 @@
 		int candidateIdx = Random((int)polygons->size());
 		Plane3 candidatePlane = (*polygons)[candidateIdx]->GetSupportingPlane();
-		Debug << "choosing new candidate: " << limit << endl;
+		
 		// evaluate current candidate
 		int candidateValue = EvalSplitPlane(polygons, candidatePlane);
@@ -470,8 +469,7 @@
 			bestPlane = &candidatePlane;
 			bestValue = candidateValue;
-			Debug << "new plane value " << bestValue << endl;
-		}
-	}
-	Debug << "%%%%%%%%%%found value: " << bestValue << endl;
+			//Debug << "new plane value " << bestValue << endl;
+		}
+	}
 		
 	return *bestPlane;
@@ -532,5 +530,4 @@
 	if ((polys->size() <= mTermMaxPolygons) || (depth >= mTermMaxDepth))
 	{
-		// Debug << polys->size() << ", " << depth << endl;		
 		return leaf;
 	}
@@ -540,5 +537,8 @@
 	// add the new nodes to the tree + select subdivision plane
 	BspInterior *node = new BspInterior(SelectPlane(polys)); 
-	Debug << "new bspinterior: " << (*node) << endl;
+
+#ifdef _DEBUG
+	Debug << node << endl;
+#endif
 	// split polygon according to current plane
 	int splits = 0;
@@ -546,5 +546,5 @@
 	
 	node->SplitPolygons(polys, frontPolys, backPolys, splits);
-	Debug << "$$ " << polySize << " " << frontPolys->size() << " " << backPolys->size() << endl; 
+	
  	mStat.splits += splits;
 
@@ -595,20 +595,20 @@
 void BspTree::EvaluateLeafStats(const BspTraversalData &data)
 {
-  // the node became a leaf -> evaluate stats for leafs
-  BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
-
-  Debug << "evaluating leaf stats\n";
-
-  if (data.mDepth >= mTermMaxDepth)
-  {
-	  ++ mStat.maxDepthNodes; 
-  }
-
-  // record maximal depth
-  if (data.mDepth > mStat.maxDepth)
-	  mStat.maxDepth = data.mDepth;
-
-  Debug << "BSP Traversal data. Depth: " << data.mDepth << " (max: " << mTermMaxDepth<< "), #polygons: " << 
+	// the node became a leaf -> evaluate stats for leafs
+	BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
+
+	if (data.mDepth >= mTermMaxDepth)
+	{
+		++ mStat.maxDepthNodes;
+	}
+
+	// record maximal depth
+	if (data.mDepth > mStat.maxDepth)
+		mStat.maxDepth = data.mDepth;
+
+#ifdef _DEBUG
+	Debug << "BSP Traversal data. Depth: " << data.mDepth << " (max: " << mTermMaxDepth<< "), #polygons: " << 
 	  data.mPolygons->size() << " (max: " << mTermMaxPolygons << ")" << endl;
+#endif
 }
 //} // GtpVisibilityPreprocessor
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 235)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 239)
@@ -46,9 +46,9 @@
 		@param objects the intersectables the viewcells are derived from
 		@param viewCells the viewcells are returned in this container
-		@param if >0, indicates the maximim number of viewcells that will be created
+		@param maxViewCells if > 0, indicates the maximim number of viewcells that will be created
 	*/
 	static void DeriveViewCells(const ObjectContainer &objects, 
 								ViewCellContainer &viewCells, 
-								const int max);
+								const int maxViewCells);
 protected:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 238)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 239)
@@ -49,14 +49,17 @@
 	  if (signum(dv) != 0)
 	  {
-		  u = - Distance(a) / dv;
-		  Debug << "t: " << u << ", b - a: " << v << ", norm: " << mNormal << ", dist(a): " << - Distance(a) << ", dv: " << dv << endl;
-		  if (coplanar) (*coplanar) = false;
+		  u = - Distance(a) / dv; // NOTE: could be done more efficiently
+		  //Debug << "t: " << u << ", b - a: " << v << ", norm: " << mNormal << ", dist(a): " << - Distance(a) << ", dv: " << dv << endl;
+		  if (coplanar) 
+			  (*coplanar) = false;
 	  }
-	  else {
-		  if (coplanar) (*coplanar) = true;	  
+	  else if (coplanar) 
+	  {
+		  (*coplanar) = true;	  
 	  }
-	  if (t) (*t) = u;
-	  Debug << "result of intersection with A: " << a << ", B: " << b << ":\n" << (a + u * b - u * a) << "\n" << (a + u*v) << "\n\n";
-	  
+
+	  if (t) 
+		  (*t) = u;
+	  	  
 	  return a + u * b - u * a; // 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 238)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 239)
@@ -14,6 +14,4 @@
 	VertexIndexContainer::const_iterator it;
 
-	Debug << "Creating polygon:\n";
-
 	for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it)
 	{
@@ -25,8 +23,9 @@
 Plane3 Polygon3::GetSupportingPlane() const
 {
-	Debug << "creating plane using vertices: " << mVertices[0] << mVertices[1] << mVertices[2] << endl;
 	Vector3 v1 = mVertices[0] - mVertices[1];
-		Vector3 v2 = mVertices[2] - mVertices[1];
-	Debug << "plane has vectors " <<  v1 << v2  << endl;
+	Vector3 v2 = mVertices[2] - mVertices[1];
+#ifdef _DEBUG
+	Debug << "plane spanned by " <<  v1 << ", " << v2  << endl;
+#endif
 	return Plane3(mVertices[0], mVertices[1], mVertices[2]);
 }
@@ -52,6 +51,4 @@
 	VertexContainer::const_iterator it;
 
-	Debug << "\n\nvertex A: " << ptA << ", side A: " << sideA << " (" << partition->Distance(ptA) << ")";
-
 	// find line - plane intersections
 	for (it = mVertices.begin(); it != mVertices.end(); ++ it)
@@ -59,6 +56,5 @@
 		Vector3 ptB = (*it);
 		int sideB = partition->Side(ptB, SIDE_TOLERANCE);
-		Debug << " <=> vertex B: " << ptB << ", side B: " << sideB << " (" << partition->Distance(ptB) << ")\n\n";
-
+	
 		// vertices on different sides => split
 	    if (sideB > 0)
@@ -71,9 +67,9 @@
 				// add vertex to both polygons
 				front->mVertices.push_back(splitPt);
-				back->mVertices.push_back(splitPt); Debug << "front and back polygon + " << splitPt << endl;
+				back->mVertices.push_back(splitPt);
 			
 				++ splits;
 			}
-			front->mVertices.push_back(ptB); Debug << "front polygon + " << ptB << endl;
+			front->mVertices.push_back(ptB);
 		}
 		else if (sideB < 0)
@@ -86,9 +82,9 @@
 				// add vertex to both polygons
 				front->mVertices.push_back(splitPt);
-				back->mVertices.push_back(splitPt); Debug << "front and back polygon + " << splitPt << endl;
+				back->mVertices.push_back(splitPt);
 
 				++ splits;
 			}
-			back->mVertices.push_back(ptB); Debug << "back polygon + " << ptB << endl;
+			back->mVertices.push_back(ptB);
 		}
 		else
@@ -96,12 +92,10 @@
 			// vertex on plane => add vertex to both polygons
 			front->mVertices.push_back(ptB);
-			back->mVertices.push_back(ptB); Debug << "front and back polygon + " << ptB << endl;
+			back->mVertices.push_back(ptB); 
 		}
 	
 		ptA = ptB;
 		sideA = sideB;
-		Debug << "vertex A: " << ptA << ", side A: " << sideA << " (" << partition->Distance(ptA) << ")";
 	}
-	Debug << "\n*********************\n";
 }
 
