Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 235)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 237)
@@ -60,10 +60,10 @@
 BspTree {
 #	splitPlaneStrategy leastSplits
-splitPlaneStrategy nextPolygon
+	splitPlaneStrategy nextPolygon
 #	constructionMethod viewCells
 	constructionMethod sceneGeometry
 	Termination {
-		maxPolygons 2
-		maxDepth 18
+		maxPolygons 4
+		maxDepth 35
 	}
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 235)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 237)
@@ -58,5 +58,7 @@
 							   const int max)
 {
+	// maximal max viewcells
 	int n = max > 0 ? std::min((int)objects.size(), max) : (int)objects.size();
+
 	for (int i = 0; i < n; ++i)
 	{
@@ -71,4 +73,5 @@
 			viewCells.push_back(viewCell);
 		}
+		//TODO: transformed meshes
 	}
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 236)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 237)
@@ -57,4 +57,17 @@
 };
 
+// Overload << operator for C++-style output
+inline ostream&
+operator<< (ostream &s, const Polygon3 &A)
+{
+	VertexContainer::const_iterator it;
+
+	s << "Polygon:\n";
+	
+	for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it)
+		s << *it << endl;
+	
+	return s;
+}
 
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 236)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 237)
@@ -145,4 +145,10 @@
 	void SplitPolygons(PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys, int &splits);
 
+	friend ostream &operator<<(ostream &s, const BspInterior &A)
+	{
+		return s << A.mPlane;
+	}
+
+
 protected:
 	
@@ -155,6 +161,6 @@
 };
 
-
-/** BSP leaf node implementation */
+/** BSP leaf node implementation.
+*/
 class BspLeaf : public BspNode 
 {
@@ -276,8 +282,8 @@
 	/// Pointer to the root of the tree
 	BspNode *mRoot;
+
 	/// Pointer to the root cell of the viewspace
 	// ViewCell *mRootCell;
-	
-	
+		
 	BspTreeStatistics mStat;
 
@@ -286,4 +292,5 @@
 	int mTermMaxDepth;
 
+	/// Strategies for choosing next split plane.
 	enum {NEXT_POLYGON, LEAST_SPLITS};
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 236)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 237)
@@ -96,4 +96,6 @@
 		polys->pop();
 
+		Debug << (*poly);
+
 		int result = poly->Side(&mPlane);
 
@@ -104,5 +106,5 @@
 		{
 			case Polygon3::COINCIDENT:
-				break;
+				//break; //TODO: comment in
 			case Polygon3::FRONT_SIDE:
 				frontPolys->push(poly);
@@ -118,4 +120,5 @@
 				poly->Split(&mPlane, front_piece, back_piece, splits);
 			
+				Debug << "SPLIT, plane: " << mPlane << "\norginal: " << (*poly) << "\nback: " << (*back_piece) << "\nfront: " << (*front_piece) << endl;
 				backPolys->push(back_piece);
 				frontPolys->push(front_piece);
@@ -123,4 +126,5 @@
 				// don't need polygon anymore
 				DEL_PTR(poly);
+
 				break;
 			default:
@@ -128,6 +132,4 @@
 		}
 	}
-	// delete old polygons
-//	Polygon3::DeletePolygons(polys);
 }
 
@@ -204,8 +206,7 @@
 
 	app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n"<<
-	maxDepthNodes*100/(double)Leaves()<<endl;
-
-	app << "#N_PMAXDEPTH ( Maximal reached depth )\n"<<
-	maxDepth*100/(double)Leaves()<<endl;
+	maxDepthNodes * 100 / (double)Leaves() << endl;
+
+	app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth <<endl;
 
 	app << "#N_ADDED_RAYREFS  (Number of dynamically added ray references )\n"<<
@@ -334,7 +335,10 @@
 	ObjectContainer::const_iterator it, it_end = objects.end();
 
-	for (it = objects.begin(); it != it_end; ++ it)
-	{
-		Intersectable *object = *it;
+	int limit = min((int)objects.size(), 1);
+
+	//for (it = objects.begin(); it != it_end; ++ it)
+	for (int i = 0; i < limit; ++i)
+	{
+		Intersectable *object = objects[i];//*it;
 		Mesh *mesh = NULL;
 
@@ -343,16 +347,19 @@
 		{
 		case Intersectable::MESH_INSTANCE:
-			
 			mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
-			
+			break;
 		case Intersectable::VIEWCELL:
 			mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
-			// copy the mesh data to polygons
-			CopyMesh2Polygons(mesh, polys);
 			break;
 		default:
 			break;
 		}
-	}
+		
+		if (mesh) // copy the mesh data to polygons
+		{
+			CopyMesh2Polygons(mesh, polys);
+		}
+	}
+
 	Debug << "number of polygons: " << polys.size() << endl;
 }
@@ -360,4 +367,6 @@
 void BspTree::Construct(const ObjectContainer &objects)
 {
+	Debug << "Constructing tree using object container\n";
+
 	mTermMaxPolygons = sTermMaxPolygons;
 	mTermMaxDepth = sTermMaxDepth;
@@ -376,7 +385,7 @@
 	while (!tStack.empty()) 
 	{
-	    tData = tStack.top();
+		tData = tStack.top();
 	    tStack.pop();
-    
+		
 		Subdivide(tStack, tData);
 	}
@@ -385,4 +394,6 @@
 void BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell)
 {
+	//Debug << "Subdividing node\n";
+
 	PolygonQueue *backPolys = new PolygonQueue();
 	PolygonQueue *frontPolys = new PolygonQueue();
@@ -404,6 +415,10 @@
 		tStack.push(BspTraversalData(interior->GetFront(), interior, frontPolys, tData.mDepth + 1));
 	}
-	else
+	else // tree terminates here
+	{
 		EvaluateLeafStats(tData);
+		// don't need to store polygon information => delete polygons
+		Polygon3::DeletePolygons(tData.mPolygons);
+	}
 }
 
@@ -434,6 +449,4 @@
 	if ((polys->size() <= mTermMaxPolygons) || (depth >= mTermMaxDepth))
 	{
-		// don't need to store polygon information => delete polygons
-		Polygon3::DeletePolygons(polys);
 		// Debug << polys->size() << ", " << depth << endl;		
 		return leaf;
@@ -444,5 +457,5 @@
 	// add the new nodes to the tree + select subdivision plane
 	BspInterior *node = new BspInterior(SelectPlane(polys)); 
-
+	Debug << "new bspinterior: " << (*node) << endl;
 	// split polygon according to current plane
 	int splits = 0;
@@ -463,5 +476,5 @@
 	node->SetupChildLinks(back, front);
 
-	DEL_PTR(leaf);
+	DEL_PTR(leaf); // leaf not member of tree anymore
 
 	return node;
@@ -473,22 +486,38 @@
 	environment->GetIntValue("BspTree.Termination.maxPolygons", sTermMaxPolygons);
 
+	//-- extract strategy to choose the next split plane
 	char splitPlaneStrategyStr[60];
 
 	environment->GetStringValue("BspTree.splitPlaneStrategy", splitPlaneStrategyStr);
-	// TODO: Extract nodes
+	
+	sSplitPlaneStrategy = BspTree::NEXT_POLYGON;
+	
+	if (strcmp(splitPlaneStrategyStr, "nextPolygon") == 0)
+		sSplitPlaneStrategy = BspTree::NEXT_POLYGON;
+	else if (strcmp(splitPlaneStrategyStr, "leastSplits") == 0)
+		sSplitPlaneStrategy = BspTree::LEAST_SPLITS;
+	else 
+	{
+		cerr << "Wrong BSP split plane strategy " << splitPlaneStrategyStr << endl;
+		exit(1);
+    }
 }
 
 void BspTree::EvaluateLeafStats(const BspTraversalData &data)
 {
-
   // the node became a leaf -> evaluate stats for leafs
   BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
 
-  if (data.mDepth > mTermMaxDepth)
-    ++ mStat.maxDepthNodes; 
+  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: " << 
+	  data.mPolygons->size() << " (max: " << mTermMaxPolygons << ")" << endl;
 }
 //} // GtpVisibilityPreprocessor
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 235)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 237)
@@ -69,9 +69,8 @@
 	char constructionMethodStr[64];
 
-	environment->GetStringValue("BspTree.constructionMethod",
-			   constructionMethodStr);
+	environment->GetStringValue("BspTree.constructionMethod", constructionMethodStr);
 
 	int constructionMethod = BspTree::VIEWCELLS;
-
+	
 	if (strcmp(constructionMethodStr, "viewCells") == 0)
 		constructionMethod = BspTree::VIEWCELLS;
@@ -84,12 +83,13 @@
     }
 
-	Debug << constructionMethodStr << endl;
-
 	switch (constructionMethod)
 	{
 	case BspTree::VIEWCELLS:
+
 		mViewcells.clear();
+		
 		// derive viewcells from the scene objects
 		ViewCell::DeriveViewCells(objects, mViewcells, 1000);
+		
 		mBspTree->Construct(mViewcells);
 		break;
@@ -102,5 +102,4 @@
 	return true;
 }
-
 
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 225)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 237)
@@ -41,5 +41,26 @@
 			   float *t = NULL,
 			   bool *coplanar = NULL
-			   ) const {return Vector3(0,0,0);} // TODO
+			   ) const 
+  {
+	  const Vector3 v = b - a; // line from A to B
+
+	  float dv = DotProd(mNormal, v);
+  	  float u = 0;
+	
+	  if (dv)
+	  {
+		  u = - Distance(a) / dv;
+		  Debug << "t: " << u << ", dv: " << dv << endl;
+		  if (coplanar) (*coplanar) = false;
+	  }
+	  else {
+		  Debug << "OHOH: " << u << endl;
+		  if (coplanar) (*coplanar) = true;	  
+	  }
+	  if (t) (*t) = u;
+	  Debug << "A: " << a << ", B: " << b << ", result: " << (a + (u * v)) << endl;
+	  
+	  return a + (u * v);
+  }
 
   friend bool
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 235)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 237)
@@ -48,7 +48,9 @@
   }
 
-  if (0) {
+  if (1) {
     Camera camera;
-    camera.LookAtBox(p->mKdTree->GetBox());
+    //camera.LookAtBox(p->mKdTree->GetBox());
+	camera.LookInBox(p->mKdTree->GetBox());
+	camera.SetPosition(camera.mPosition + Vector3(0,300,0));
     camera.SnapImage("camera.jpg", p->mKdTree);
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 235)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 237)
@@ -12,7 +12,10 @@
 	VertexIndexContainer::const_iterator it;
 
+	Debug << "Creating polygon:\n";
+
 	for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it)
 	{
 		mVertices.push_back(parent->mVertices[*it]);
+		Debug << parent->mVertices[*it] << endl;
 	}
 }
@@ -37,5 +40,5 @@
 {
 	splits = 0;
-	Vector3 ptA = mVertices[mVertices.size() - 1];;
+	Vector3 ptA = mVertices[mVertices.size() - 1];
 	
 	int sideA = partition->Side(ptA);
@@ -50,26 +53,41 @@
 
 		// vertices on different sides => split
-		if ((sideA != 0) && (sideB != 0) && (sideA != sideB)) 
+	    if (sideB > 0)
 		{
-			Vector3 v = ptB - ptA; // line from A to B
-			float dv = DotProd(partition->mNormal, v);
-			float t = 0;
+			if (sideA < 0)
+			{
+				//-- plane - line intersection
+				Vector3 splitPt = partition->FindIntersection(ptA, ptB);
 			
-			if (dv)
+				// add vertex to both polygons
+				front->mVertices.push_back(splitPt);
+				back->mVertices.push_back(splitPt);
+			
+				++ splits;
+			}
+			front->mVertices.push_back(ptB);
+		}
+		else if (sideB < 0)
+		{
+			if (sideA > 0)
 			{
-				t = - partition->Distance(ptA) / dv;
+				//-- plane - line intersection
+				Vector3 splitPt = partition->FindIntersection(ptA, ptB);
+			
+				// add vertex to both polygons
+				front->mVertices.push_back(splitPt);
+				back->mVertices.push_back(splitPt);
+
+				++ splits;
 			}
-
-			++ splits;
-		}
-		if (sideB >= 0)
-		{
 			back->mVertices.push_back(ptB);
 		}
-		else if (sideB <= 0)
+		else
 		{
+			// vertex on plane => add vertex to both polygons
 			front->mVertices.push_back(ptB);
+			back->mVertices.push_back(ptB);
 		}
-
+	
 		ptA = ptB;
 		sideA = sideB;
