Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 381)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 383)
@@ -126,8 +126,8 @@
 	#splitPlaneStrategy 130
 	
-	splitPlaneStrategy 128
+	splitPlaneStrategy 1024
 	
-	maxPolyCandidates 5
-	maxRayCandidates 50
+	maxPolyCandidates 50
+	maxRayCandidates 0
 	
 	Termination {
@@ -135,5 +135,5 @@
 		maxRays 1000
 		maxPolygons 1
-		maxDepth 10
+		maxDepth 80
 		
 		# axis aligned splits
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 379)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 383)
@@ -459,10 +459,10 @@
 		@returns true if plane contributes to the cell.
 	*/
-	bool ComputeChildrenGeometry(PolygonContainer &newCell,
-							const PolygonContainer &cell,
-							const vector<Plane3> &planes,
-							const vector<bool> &sides,
-							const Plane3 &splitPlane,
-							const bool side) const;
+	bool ConstructChildGeometry(PolygonContainer &newCell,
+							    const PolygonContainer &cell,
+							    const vector<Plane3 *> &planes,
+							    const vector<bool> &sides,
+							    const Plane3 &splitPlane,
+							    const bool side) const;
 
 
@@ -540,5 +540,6 @@
 		@returns the cost of the candidate split plane
 	*/
-	float SplitPlaneCost(const Plane3 &candidatePlane,
+	float SplitPlaneCost(BspLeaf *leaf, 
+		                 const Plane3 &candidatePlane,
 						 const PolygonContainer &polys, 						 
 						 const BoundedRayContainer &rays) const;
@@ -548,5 +549,6 @@
 		@returns the cost of the candidate split plane
 	*/
-	float SplitPlaneCost(const Plane3 &candidatePlane,
+	float SplitPlaneCost(BspLeaf *leaf, 
+		                 const Plane3 &candidatePlane,
 						 const PolygonContainer &polys) const;
 
@@ -555,5 +557,6 @@
 		@returns the cost of the candidate split plane
 	*/
-	float SplitPlaneCost(const Plane3 &candidatePlane,
+	float SplitPlaneCost(BspLeaf *leaf,
+		                 const Plane3 &candidatePlane,
 						 const BoundedRayContainer &polys) const;
 
@@ -608,5 +611,6 @@
 		@param maxRayCandidates the maximal number of ray candidates
 	*/
-	Plane3 SelectPlaneHeuristics(PolygonContainer &polys, 
+	Plane3 SelectPlaneHeuristics(BspLeaf *leaf,
+		                         PolygonContainer &polys, 
 								 const BoundedRayContainer &rays,
 								 const int maxPolyCandidates,
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 379)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 383)
@@ -71,5 +71,5 @@
   ExportBspSplitPlanes(const BspTree &tree) = 0;
   virtual void 
-  ExportBspSplits(const BspTree &tree) = 0;
+  ExportBspSplits(const BspTree &tree, const bool exportDepth = false) = 0;
   virtual void
   ExportLeavesGeometry(const BspTree &tree, const vector<BspLeaf *> &leaves) = 0;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 380)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 383)
@@ -1084,21 +1084,17 @@
 	if (sSplitPlaneStrategy & RANDOM_POLYGON)
 	{
-		Vector3 pt[3];
-		for (int j = 0; j < 3; j ++)
-		{
-			int idx = Random((int)rays.size());
-			pt[j] = rays[idx]->mRay->Extrap(rays[idx]->mMaxT);
-		}
-
-		return Plane3(pt[0], pt[1], pt[2]);
-
-        //return polys[Random((int)polys.size())]->GetSupportingPlane();
+		return polys[Random((int)polys.size())]->GetSupportingPlane();
 	}
 
 	// use heuristics to find appropriate plane
-	return SelectPlaneHeuristics(polys, rays, sMaxPolyCandidates, sMaxRayCandidates);
-}
-
-Plane3 BspTree::SelectPlaneHeuristics(PolygonContainer &polys,
+	return SelectPlaneHeuristics(leaf, 
+		                         polys, 
+								 rays, 
+								 sMaxPolyCandidates, 
+								 sMaxRayCandidates);
+}
+
+Plane3 BspTree::SelectPlaneHeuristics(BspLeaf *leaf,
+									  PolygonContainer &polys,
 									  const BoundedRayContainer &rays,
 									  const int maxPolyCandidates,
@@ -1108,18 +1104,20 @@
 	int bestPlaneIdx = 0;
 	
-	int limit = maxPolyCandidates > 0 ? Min((int)polys.size(), maxPolyCandidates) : (int)polys.size();
+	int limit = maxPolyCandidates > 0 ? 
+		Min((int)polys.size(), maxPolyCandidates) : (int)polys.size();
 	
 	int candidateIdx = limit;
 
-	//Debug << endl;
-	/*for (int i = 0; i < limit; ++ i)
+	for (int i = 0; i < limit; ++ i)
 	{
 		candidateIdx = GetNextCandidateIdx(candidateIdx, polys);
-
-		Plane3 candidatePlane = polys[candidateIdx]->GetSupportingPlane();
+		
+		// evaluate current candidate
+		const float candidateCost = 
+			SplitPlaneCost(leaf,
+			               polys[candidateIdx]->GetSupportingPlane(), 
+						   polys, rays);
+
 		//Debug << polys[candidateIdx] << endl;
-		// evaluate current candidate
-		float candidateCost = SplitPlaneCost(candidatePlane, polys, rays);
-			
 		if (candidateCost < lowestCost)
 		{
@@ -1127,5 +1125,5 @@
 			lowestCost = candidateCost;
 		}
-	}*/
+	}
 	
 	//limit = maxRaysCandidates > 0 ? Min((int)rays.size(), maxRayCandidates) : (int)rays.size();
@@ -1140,9 +1138,19 @@
 		for (int j = 0; j < 3; j ++)
 		{
-			idx[j] = Random((int)rays.size());
-			pt[j] = rays[idx[j]]->mRay->Extrap(rays[idx[j]]->mMaxT);
-		}
-
-		float candidateCost = SplitPlaneCost(Plane3(pt[0], pt[1], pt[2]), polys, rays);
+			idx[j] = Random((int)rays.size() * 2);
+				
+			if (idx[j] < (int)rays.size())
+				pt[j] = rays[idx[j]]->mRay->Extrap(rays[idx[j]]->mMinT);
+			else
+			{
+				idx[j] -= (int)rays.size();
+				pt[j] = rays[idx[j]]->mRay->Extrap(rays[idx[j]]->mMaxT);
+			}
+					
+		}
+
+		const float candidateCost =
+			SplitPlaneCost(leaf, Plane3(pt[0], pt[1], pt[2]), 
+						   polys, rays);
 
 		if (candidateCost < lowestCost)
@@ -1157,4 +1165,5 @@
 	if (chooseFromRays)
 	{
+		Debug << "choose ray plane: " << lowestCost << endl;
 		return Plane3(rays[bestIdx[0]]->mRay->Extrap(rays[bestIdx[0]]->mMaxT),
 					  rays[bestIdx[1]]->mRay->Extrap(rays[bestIdx[1]]->mMaxT),
@@ -1177,5 +1186,6 @@
 }
 
-float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
+float BspTree::SplitPlaneCost(BspLeaf *leaf, 
+							  const Plane3 &candidatePlane, 
 							  const PolygonContainer &polys) const
 {
@@ -1316,5 +1326,6 @@
 }
 
-float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
+float BspTree::SplitPlaneCost(BspLeaf *leaf,
+							  const Plane3 &candidatePlane, 
 							  const BoundedRayContainer &rays) const
 {
@@ -1333,13 +1344,21 @@
 	Intersectable::NewMail(); const int frontAndBackId = ViewCell::sMailId;
 
-	/*PolygonContainer cell;
+	PolygonContainer cell;
 	PolygonContainer front;
 	PolygonContainer back;
 
-	ConstructGeometry(currentNode, cell);
-	ConstructChildrenGeometry(front, cell, planes, sides);
-	ConstructChildrenGeometry(back, cell, planes, sides);*/
-
-
+	vector<Plane3 *> planes;
+	vector<bool> sides;
+
+	ExtractSplitPlanes(leaf, planes, sides);
+
+	ConstructGeometry(leaf, cell);
+	ConstructChildGeometry(front, cell, planes, sides, candidatePlane, true);
+	ConstructChildGeometry(back, cell, planes, sides, candidatePlane, false);
+
+	CLEAR_CONTAINER(cell);
+	CLEAR_CONTAINER(front);
+	CLEAR_CONTAINER(back);
+	
 	int frontRays = 0;
 	int backRays = 0;
@@ -1475,5 +1494,6 @@
 }
 
-float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
+float BspTree::SplitPlaneCost(BspLeaf *leaf,
+							  const Plane3 &candidatePlane, 
 							  const PolygonContainer &polys, 							  
 							  const BoundedRayContainer &rays) const
@@ -1497,5 +1517,5 @@
 		(sSplitPlaneStrategy & BLOCKED_RAYS))
 	{
-        val += SplitPlaneCost(candidatePlane, polys);
+        val += SplitPlaneCost(leaf, candidatePlane, polys);
 	}
 
@@ -1505,5 +1525,5 @@
 		(sSplitPlaneStrategy & PVS))
 	{
-		val += SplitPlaneCost(candidatePlane, rays);
+		val += SplitPlaneCost(leaf, candidatePlane, rays);
 	}
 
@@ -2019,10 +2039,10 @@
 }
 
-bool BspTree::ComputeChildrenGeometry(PolygonContainer &newCell,
-									  const PolygonContainer &cell,
-									  const vector<Plane3> &planes,
-									  const vector<bool> &sides,
-									  const Plane3 &splitPlane,
-									  const bool side) const
+bool BspTree::ConstructChildGeometry(PolygonContainer &newCell,
+									 const PolygonContainer &cell,
+									 const vector<Plane3 *> &planes,
+									 const vector<bool> &sides,
+									 const Plane3 &splitPlane,
+									 const bool side) const
 {
 	// get cross section of new polygon
@@ -2034,5 +2054,5 @@
 	for (int i = 0; (i < planes.size()) && inside; ++ i)
 	{
-		const int cf = planePoly->ClassifyPlane(planes[i]);
+		const int cf = planePoly->ClassifyPlane(*planes[i]);
 			
 		// split new polygon with all previous planes
@@ -2046,5 +2066,5 @@
 					Polygon3 *backPoly = new Polygon3();
 
-					planePoly->Split(planes[i], *frontPoly, *backPoly, splitPts);
+					planePoly->Split(*planes[i], *frontPoly, *backPoly, splitPts);
 					DEL_PTR(planePoly);
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 379)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 383)
@@ -75,5 +75,5 @@
   
   virtual void 
-  ExportBspSplits(const BspTree &tree);
+  ExportBspSplits(const BspTree &tree, const bool exportDepth);
 
   virtual void
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 382)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 383)
@@ -131,5 +131,4 @@
   long startTime = GetTime();
   
-  int i;
   int totalSamples = 0;
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 380)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 383)
@@ -696,18 +696,22 @@
 	vector<bool> mSides;
 	bool mIsFront;
+	int mDepth;
 
 	BspSplitData(BspNode *node): 
-	mNode(node), mIsFront(false)
+	mNode(node), mIsFront(false), mDepth(0)
 	{};	
+
 	BspSplitData(BspNode *node, 
 				vector<Plane3 *> planes, 
 				vector<bool> sides, 
-				bool isFront): 
-	mNode(node), mPlanes(planes), 
-	mSides(sides), mIsFront(isFront)
+				const bool isFront,
+				const int depth): 
+	mNode(node), mPlanes(planes), mSides(sides), 
+	mIsFront(isFront), mDepth(depth)
 	{};
 };
 
-void X3dExporter::ExportLeavesGeometry(const BspTree &tree, const vector<BspLeaf *> &leaves)
+void X3dExporter::ExportLeavesGeometry(const BspTree &tree, 
+									   const vector<BspLeaf *> &leaves)
 {
 	vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
@@ -724,5 +728,6 @@
 }
 
-void X3dExporter::ExportBspSplits(const BspTree &tree)
+void X3dExporter::ExportBspSplits(const BspTree &tree,
+								  const bool exportDepth)
 {
 	std::stack<BspSplitData> tStack;
@@ -732,4 +737,7 @@
   
 	PolygonContainer polys;
+	vector <int> depths;
+
+	int maxDepth = 0;
 
 	while (!tStack.empty())
@@ -739,13 +747,20 @@
 	    tStack.pop();	
 		
-		if (!tData.mNode->IsLeaf())
+		if (tData.mNode->IsLeaf())
+		{
+			if (tData.mDepth > maxDepth)
+				maxDepth = tData.mDepth;
+		}
+		else
 		{
 			BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
 
+			// add current side of split plane
 			if (tData.mNode != tree.GetRoot())
-				tData.mSides.push_back(tData.mIsFront); // add current side of split plane
+				tData.mSides.push_back(tData.mIsFront);
 
 			// bounded plane is added to the polygons
-			Polygon3 *planePoly = tree.GetBoundingBox().CrossSection(*interior->GetPlane());
+			Polygon3 *planePoly = 
+				tree.GetBoundingBox().CrossSection(*interior->GetPlane());
 		
 			// do all the splits with the previous planes
@@ -778,14 +793,39 @@
 
 			if (planePoly->Valid())
+			{
 				polys.push_back(planePoly);
+				depths.push_back(tData.mDepth);
+			}
 			else
 				DEL_PTR(planePoly);
 			
 			// push the children on the stack
-			tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes, tData.mSides, true));
-			tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes, tData.mSides, false));
-		}
-	}	
-	ExportPolygons(polys);
+			tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes, 
+				                     tData.mSides, true, tData.mDepth + 1));
+			tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes, 
+				                     tData.mSides, false, tData.mDepth + 1));
+		}
+	}
+
+	if (maxDepth > 0)
+	{	
+		mUseForcedMaterial = true;
+			
+		for (int i = 0; i < (int)polys.size(); ++ i)
+		{
+			mForcedMaterial.mDiffuseColor.b = 1.0f;
+			float importance =  (float)depths[i]/ (float)maxDepth;
+            
+			mForcedMaterial.mDiffuseColor.r = importance;
+			mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
+
+			ExportPolygon(polys[i]);
+		}
+	}
+	else
+	{
+		ExportPolygons(polys);
+	}
+
 	CLEAR_CONTAINER(polys);
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 380)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 383)
@@ -600,5 +600,5 @@
 		}
 
-		//-- visualization of the bsp splits
+		//-- visualization of the BSP splits
 		bool exportSplits = false;
 		environment->GetBoolValue("BspTree.exportSplits", exportSplits);
@@ -703,5 +703,4 @@
 							
 			mSampleRays.push_back(new Ray(ray));
-			Debug << "abba " << mSampleRays.size() << endl;
 		}
 		else
@@ -727,5 +726,4 @@
 	else if ((int)mSampleRays.size() < mPostProcessSamples)
 	{
-		Debug << "police " << mSampleRays.size() << endl;
 		mSampleRays.push_back(new Ray(ray));
 	}
@@ -779,5 +777,5 @@
 		exporter->SetForcedMaterial(m);
 		exporter->SetWireframe();
-		exporter->ExportBspSplits(*mBspTree);
+		exporter->ExportBspSplits(*mBspTree, true);
 
 		// take forced material, else big scenes cannot be viewed
