Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 332)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 335)
@@ -113,16 +113,16 @@
 	#splitPlaneStrategy 130
 	
-	splitPlaneStrategy 66
+	splitPlaneStrategy 130
 	
-	maxCandidates 130
+	maxCandidates 50
 	
 	Termination {
 		# autopartition
 		maxRays -1
-		maxPolygons 4
+		maxPolygons 20
 		maxDepth 100
 		
 		#axis aligned split
-		maxPolysForAxisAligned 100
+		maxPolysForAxisAligned 200
 		maxCostRatio 0.9
 		ct_div_ci 0.5
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 332)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 335)
@@ -403,5 +403,6 @@
 		@param polys the polygon list on which the split decition is based
 		@param rays ray container on which selection may be based
-		Returns the split plane
+		@note the polygons can be reordered in the process
+		@returns the split plane
 	*/
 	Plane3 SelectPlane(BspLeaf *leaf, 
@@ -410,10 +411,27 @@
 
 	/** Evaluates the contribution of the candidate split plane.
-		@note the polygons can be reordered in the process.
+		
+		@param canditatePlane the candidate split plane
+		@param polys the polygons the split can be based on
+		@param rays the rays the split can be based on
 		@returns the cost of the candidate split plane
 	*/
-	float SplitPlaneCost(PolygonContainer &polys, 
-						 const Plane3 &candidatePlane,
-						 const RayContainer &rays);
+	float SplitPlaneCost(const Plane3 &candidatePlane,
+						 const PolygonContainer &polys, 						 
+						 const RayContainer &rays) const;
+
+	/** Strategies where the effect of the split plane is tested
+	    on all input rays.
+		@returns the cost of the candidate split plane
+	*/
+	float SplitPlaneCost(const Plane3 &candidatePlane,
+						 const PolygonContainer &polys) const;
+
+	/** Strategies where the effect of the split plane is tested
+	    on all input polygons.
+		@returns the cost of the candidate split plane
+	*/
+	float SplitPlaneCost(const Plane3 &candidatePlane,
+						 const RayContainer &polys) const;
 
 	/** Filters next view cell down the tree and inserts it into the appropriate leaves
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 332)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 335)
@@ -1059,5 +1059,5 @@
 		
 		// evaluate current candidate
-		float candidateCost = SplitPlaneCost(polys, candidatePlane, rays);
+		float candidateCost = SplitPlaneCost(candidatePlane, polys, rays);
 			
 		if (candidateCost < lowestCost)
@@ -1085,30 +1085,8 @@
 }
 
-float BspTree::SplitPlaneCost(PolygonContainer &polys, 
-							  const Plane3 &candidatePlane, 
-							  const RayContainer &rays)
+float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
+							  const PolygonContainer &polys) const
 {
 	float val = 0;
-
-	if (sSplitPlaneStrategy & VERTICAL_AXIS)
-	{
-		Vector3 tinyAxis(0,0,0); tinyAxis[mBox.Size().TinyAxis()] = 1.0f;
-		// we put a penalty on the dot product between the "tiny" vertical axis
-		// and the split plane axis
-		val += sVerticalSplitsFactor * 
-			   fabs(DotProd(candidatePlane.mNormal, tinyAxis));
-	}
-
-	if (!((sSplitPlaneStrategy & BALANCED_POLYS)      ||
-		  (sSplitPlaneStrategy & LEAST_SPLITS)        ||
-		  (sSplitPlaneStrategy & LARGEST_POLY_AREA)   ||
-		  (sSplitPlaneStrategy & BALANCED_VIEW_CELLS) ||
-		  (sSplitPlaneStrategy & BLOCKED_RAYS)))
-	{
-		return val;
-	}
-
-	// -- strategies where the effect of the split plane is tested
-	// on all input polygons 
 
 	float sumBalancedPolys = 0;
@@ -1119,9 +1097,8 @@
 	float totalBlockedRays = 0;
 	//float totalArea = 0;
-
-	// container for balanced view cells criterium
-	ObjectContainer frontViewCells;
-	ObjectContainer backViewCells;
-
+	int totalViewCells = 0;
+
+	ViewCell::NewMail();
+	
 	PolygonContainer::const_iterator it, it_end = polys.end();
 
@@ -1159,8 +1136,14 @@
 			MeshInstance *viewCell = (*it)->mParent;
 		
-			if (classification == Plane3::FRONT_SIDE)
-				frontViewCells.push_back(viewCell);
-			else if (viewCell && (classification == Plane3::BACK_SIDE))
-				backViewCells.push_back(viewCell);
+			if (!viewCell->Mailed())
+			{
+				viewCell->Mail();
+
+				if (classification == Plane3::FRONT_SIDE)
+					sumBalancedViewCells += 1.0f;
+				else if (viewCell && (classification == Plane3::BACK_SIDE))
+					sumBalancedViewCells -= 1.0f;
+				++ totalViewCells;
+			}
 		}
 	}
@@ -1179,41 +1162,19 @@
 
 	if (sSplitPlaneStrategy & BLOCKED_RAYS)
-		if (totalBlockedRays > 0)
+		if (totalBlockedRays != 0)
 			val += sBlockedRaysFactor * sumBlockedRays / totalBlockedRays;
 
 	if (sSplitPlaneStrategy & BALANCED_VIEW_CELLS)
-	{
-		// count number of unique view cells
-		sort(frontViewCells.begin(), frontViewCells.end());
-		sort(backViewCells.begin(), backViewCells.end());
-
-		ObjectContainer::const_iterator frontIt, frontIt_end = frontViewCells.end();
-	
-		Intersectable *vc = NULL;
-		// increase counter for view cells in front of plane
-		for (frontIt = frontViewCells.begin(); frontIt != frontIt_end; ++frontIt)
-		{
-			if (*frontIt != vc)
-			{
-				vc = *frontIt;
-				sumBalancedViewCells += 1.0f;
-			}
-		}
-
-		ObjectContainer::const_iterator backIt, backIt_end = backViewCells.end();
-		vc = NULL;
-		// decrease counter for view cells on back side of plane
-		for (backIt = backViewCells.begin(); backIt != backIt_end; ++backIt)
-		{
-			if (*backIt != vc)
-			{
-				vc = *backIt;
-				sumBalancedViewCells -= 1.0f;
-			}
-		}
-
-		val += sBalancedViewCellsFactor * fabs(sumBalancedViewCells) / 
-			(float)(frontViewCells.size() + backViewCells.size());
-	}
+		if (totalViewCells != 0)
+			val += sBalancedViewCellsFactor * fabs(sumBalancedViewCells) / 
+				(float)totalViewCells;
+	
+	return val;
+}
+
+float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
+							  const RayContainer &rays) const
+{
+	float val = 0;
 
 	float sumBalancedRays = 0;
@@ -1232,4 +1193,5 @@
 		}
 	}
+
 	if (sSplitPlaneStrategy & LEAST_RAY_SPLITS)
 		if (!rays.empty())
@@ -1239,4 +1201,37 @@
 		if (!rays.empty())
 			val += sBalancedRaysFactor * fabs(sumBalancedRays) / (float)rays.size();
+
+	return val;
+}
+
+float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
+							  const PolygonContainer &polys, 							  
+							  const RayContainer &rays) const
+{
+	float val = 0;
+
+	if (sSplitPlaneStrategy & VERTICAL_AXIS)
+	{
+		Vector3 tinyAxis(0,0,0); tinyAxis[mBox.Size().TinyAxis()] = 1.0f;
+		// we put a penalty on the dot product between the "tiny" vertical axis
+		// and the split plane axis
+		val += sVerticalSplitsFactor * 
+			   fabs(DotProd(candidatePlane.mNormal, tinyAxis));
+	}
+
+	if ((sSplitPlaneStrategy & BALANCED_POLYS)      ||
+		(sSplitPlaneStrategy & LEAST_SPLITS)        ||
+		(sSplitPlaneStrategy & LARGEST_POLY_AREA)   ||
+		(sSplitPlaneStrategy & BALANCED_VIEW_CELLS) ||
+		(sSplitPlaneStrategy & BLOCKED_RAYS))
+	{
+        val += SplitPlaneCost(candidatePlane, polys);
+	}
+
+	if ((sSplitPlaneStrategy & BALANCED_RAYS)      ||
+		(sSplitPlaneStrategy & LEAST_RAY_SPLITS))
+	{
+		val += SplitPlaneCost(candidatePlane, rays);
+	}
 
 	// return linear combination of the sums
@@ -1370,10 +1365,10 @@
 	}
 	
-//#ifdef _DEBUG
+#ifdef _DEBUG
 	Debug << "BSP stats: "
 		  << "Depth: " << data.mDepth << " (max: " << sTermMaxDepth << "), "
 		  << "#polygons: " << (int)data.mPolygons->size() << " (max: " << sTermMaxPolygons << "), "
 		  << "#rays: " << (int)data.mRays->size() << " (max: " << sTermMaxRays << ")" << endl;
-//#endif
+#endif
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 333)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 335)
@@ -96,5 +96,5 @@
   
    
-  if (0) {
+  if (1) {
     p->ComputeVisibility();
     p->ExportPreprocessedData("scene.vis");
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 333)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 335)
@@ -34,5 +34,8 @@
 
   void
-  SetupRay(Ray &ray, const Vector3 &point, const Vector3 &direction);
+  SetupRay(Ray &ray, 
+		   const Vector3 &point, 
+		   const Vector3 &direction,
+		   const int type);
 
   KdNode *
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 333)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 335)
@@ -9,7 +9,7 @@
 #	filename vienna.x3d
 #	filename ../data/vienna/vienna-simple.x3d
-#	filename ../data/vienna/vienna-buildings.x3d
+	filename ../data/vienna/vienna-buildings.x3d
 #	filename ../data/vienna/viewcells-25-sel.x3d
-	filename ../data/atlanta/atlanta2.x3d
+#	filename ../data/atlanta/atlanta2.x3d
 #	filename ../data/soda/soda.dat
 #	filename ../data/soda/soda5.dat
@@ -30,7 +30,7 @@
 	sahUseFaces true
 	Termination {
-		minCost 1
+		minCost 2
 		maxDepth 18
-		maxCostRatio 0.95
+		maxCostRatio 0.9
 		ct_div_ci 0.5
 	}
@@ -43,7 +43,7 @@
 MeshKdTree {
 	Termination {
-		minCost 1
-		maxDepth 12
-		maxCostRatio 0.95
+		minCost 4
+		maxDepth 18
+		maxCostRatio 0.9
 		ct_div_ci 0.5
 	}
@@ -56,24 +56,31 @@
 
 Sampling {
-	totalSamples	100
+	totalSamples	1000000
 	samplesPerPass  20
 }
 
 ViewCells {
-	hierarchyType bspTree
+	#hierarchy kdTree
+	hierarchy bspTree
+	# hierarchy sceneDependent
+	
 	height 5.0
 	maxViewCells 0
-	#hierarchyType kdTree
-	#hierarchyType sceneDependent
+	
 #	filename ../data/atlanta/atlanta_viewcells_large.x3d
-#	filename ../data/vienna/viewcells-25-sel.x3d
-	filename ../data/vienna/viewcells-25.x3d
+	filename ../data/vienna/viewcells-25-sel.x3d
+#	filename ../data/vienna/viewcells-25.x3d
 #	filename ../data/vienna/viewcells-large-sel.x3d
 }
 
 BspTree {
-#       constructionMethod fromRays
-#	constructionMethod fromViewCells
-	constructionMethod fromSceneGeometry
+	Construction {
+	#	input fromRays
+	#	input fromViewCells
+		input fromSceneGeometry
+		samples 200000
+		sideTolerance 0.002
+	}
+
 
 	# random polygon       = 1
@@ -85,5 +92,7 @@
 	# vertical axis        = 64
 	# blocked rays         = 128
-	
+	# least ray splits     = 256
+	# balanced rays        = 512
+
 	# least splits + balanced polygons
 	#splitPlaneStrategy 12
@@ -101,4 +110,7 @@
 	#splitPlaneStrategy 72
 	
+	# axis aligned + blocked rays
+	#splitPlaneStrategy 130
+	
 	splitPlaneStrategy 66
 	
@@ -106,8 +118,18 @@
 	
 	Termination {
-		maxPolysForAxisAligned 100
+		# autopartition
+		maxRays -1
 		maxPolygons 20
 		maxDepth 100
+		
+		#axis aligned split
+		maxPolysForAxisAligned 100
+		maxCostRatio 0.9
+		ct_div_ci 0.5
 	}
+	
+	#axis aligned split
+	splitBorder 0.01
+	
 	# if split polys are stored for visualization
 	storeSplitPolys false
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 333)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 335)
@@ -26,5 +26,8 @@
 
 void
-SamplingPreprocessor::SetupRay(Ray &ray, const Vector3 &point, const Vector3 &direction)
+SamplingPreprocessor::SetupRay(Ray &ray, 
+							   const Vector3 &point, 
+							   const Vector3 &direction,
+							   const int type)
 {
   ray.intersections.clear();
@@ -34,5 +37,5 @@
 
   //  cout<<point<<" "<<direction<<endl;
-  ray.Init(point, direction, Ray::LOCAL_RAY);
+  ray.Init(point, direction, type);
 }
 
@@ -259,5 +262,5 @@
 		Vector3 target = t*poly.mVertices[edge] + (1.0f-t)*poly.mVertices[(edge + 1)%
 																																		 poly.mVertices.size()];
-		SetupRay(ray, point, target - point);
+		SetupRay(ray, point, target - point, Ray::LOCAL_RAY);
 		if (!mesh->CastRay(ray, mi)) {
 			// the rays which intersect the mesh have been discarded since they are not tangent
@@ -395,5 +398,5 @@
 					
 					// construct a ray
-					SetupRay(ray, point, direction);
+					SetupRay(ray, point, direction, Ray::LOCAL_RAY);
 					
 					sampleContributions = CastRay(object, ray);
