Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 423)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 425)
@@ -137,4 +137,17 @@
 	maxRayCandidates 100
 	
+	# factors for evaluating split plane costs
+	Factor {
+		verticalSplits 1.0
+	  	largestPolyArea 1.0
+	  	blockedRays 1.0
+	  	leastRaySplits 1.0
+	  	balancedRays 1.0
+	  	pvsFactor 1.0
+	  	leastSplits 1.0
+	  	balancedPolys 1.0
+	  	balancedViewCells 1.0
+	}
+	
 	Termination {
 		# parameters used for autopartition
@@ -146,5 +159,7 @@
 		maxRayContribution 0.005
 		#maxAccRayLength 100
-		
+		# used for pvs criterium
+		ct_div_ci 0.0
+	
 		# axis aligned splits
 		AxisAligned {
@@ -203,10 +218,10 @@
 
 	Construction {
-		samples 100000
-	}
-	
-	Termination {
-		maxDepth 	40
-		minPvs		5
+		samples 200000
+	}
+	
+	Termination {
+		maxDepth 	20
+		minPvs		200
 		minRays		100
 		minSize		0.001
@@ -215,6 +230,6 @@
 	}
 	
-	maxTotalMemory	400
-	maxStaticMemory	200
+	maxTotalMemory	600
+	maxStaticMemory	600
 
 	splitType regular
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 424)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 425)
@@ -172,7 +172,7 @@
 
 
-/*********************************************************/
-/*            class VspKdTreeLeaf implementation         */
-/*********************************************************/
+/**************************************************************/
+/*              class VspKdTreeLeaf implementation            */
+/**************************************************************/
 VspKdTreeLeaf::VspKdTreeLeaf(VspKdTreeInterior *p,	const int nRays):
 VspKdTreeNode(p), mRays(), mPvsSize(0), mValidPvs(false) 
@@ -488,5 +488,5 @@
 		if (lastMem / 10 != ((int)mem) / 10) 
 		{
-			cout << mem << " MB" << endl;
+			Debug << mem << " MB" << endl;
 		}
 		lastMem = (int)mem;
@@ -569,7 +569,7 @@
 	}
 
-	if (costRatio > mTermMaxCostRatio) 
-	{
-		cout << "Too big cost ratio " << costRatio << endl;
+	if (0 && costRatio > mTermMaxCostRatio) 
+	{
+		Debug << "Too big cost ratio " << costRatio << endl;
 		return -1;
 	}
@@ -588,13 +588,11 @@
 
 							
-
-float
-VspKdTree::EvalCostRatio(VspKdTreeLeaf *leaf,
-						 const int axis,
-						 const float position,
-						 int &raysBack,
-						 int &raysFront,
-						 int &pvsBack,
-						 int &pvsFront)
+float VspKdTree::EvalCostRatio(VspKdTreeLeaf *leaf,
+							   const int axis,
+							   const float position,
+							   int &raysBack,
+							   int &raysFront,
+							   int &pvsBack,
+							   int &pvsFront)
 {
 	raysBack = 0;
@@ -632,21 +630,33 @@
 	}	
 
-	AxisAlignedBox3 box = GetBBox(leaf);
-	
-	float minBox = box.Min(axis);
-	float maxBox = box.Max(axis);
-	float sizeBox = maxBox - minBox;
+	float ratio = 0;
+	
+	if (1)
+	{
+		const float sum = pvsBack + pvsFront;
+		const float oldCost = (float)pvsSize * 2;
+
+		return sum / oldCost;
+	}
+	else {
+		AxisAlignedBox3 box = GetBBox(leaf);
+	
+		float minBox = box.Min(axis);
+		float maxBox = box.Max(axis);
+		float sizeBox = maxBox - minBox;
 		
-	//		float sum = raysBack*(position - minBox) + raysFront*(maxBox - position);
-	float sum = pvsBack * (position - minBox) + pvsFront * (maxBox - position);
-
-	newCost = mCtDivCi + sum  / sizeBox;
-	
-	//	cout<<axis<<" "<<pvsSize<<" "<<pvsBack<<" "<<pvsFront<<endl;
-	//  float oldCost = leaf->mRays.size();
-	float oldCost = (float)pvsSize;
-	
-	float ratio = newCost / oldCost;
-	//	cout<<"ratio="<<ratio<<endl;
+		// float sum = raysBack*(position - minBox) + raysFront*(maxBox - position);
+		float sum = pvsBack * (position - minBox) + pvsFront * (maxBox - position);
+
+		newCost = mCtDivCi + sum  / sizeBox;
+	
+		//Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl;
+		//  float oldCost = leaf->mRays.size();
+		float oldCost = (float)pvsSize;
+		
+		float ratio = newCost / oldCost;
+	
+	}
+	
 	return ratio;
 }
@@ -927,5 +937,5 @@
 	
 	// select subdivision axis
-	int axis = SelectPlane(leaf, box, position, raysBack, raysFront, pvsBack, pvsFront);
+	const int axis = SelectPlane(leaf, box, position, raysBack, raysFront, pvsBack, pvsFront);
 
 	Debug << "rays back=" << raysBack << " rays front=" << raysFront << " pvs back=" << pvsBack << " pvs front=" <<	pvsFront << endl;
@@ -1684,2 +1694,16 @@
 	return mStat.rays * (sizeof(VssRay))/(1024.0f * 1024.0f);
 }
+
+int VspKdTree::ComputePvsSize(VspKdTreeNode *node, const RayInfoContainer &globalRays) const
+{
+	int pvsSize = 0;
+
+	const AxisAlignedBox3 box = GetBBox(node);
+
+	RayInfoContainer::const_iterator it, it_end = globalRays.end();
+
+	for (it = globalRays.begin(); it != globalRays.end(); ++ it)
+		;//pvsSize += box.GetMinMaxT((*it).mRay, NULL, NULL);
+
+	return pvsSize;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 418)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 425)
@@ -49,5 +49,5 @@
 
   virtual bool
-  ExportVspKdTree(const VspKdTree &tree) = 0;
+  ExportVspKdTree(const VspKdTree &tree, const int maxPvs = 0) = 0;
 
   virtual bool
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 418)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 425)
@@ -40,5 +40,5 @@
   
   bool 
-  ExportVspKdTree(const VspKdTree &tree);
+  ExportVspKdTree(const VspKdTree &tree, const int maxPvs);
 
   bool ExportBspTree(const BspTree &tree);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 422)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 425)
@@ -583,8 +583,8 @@
 }
 
-bool X3dExporter::ExportVspKdTree(const VspKdTree &tree)
-{
-	 /*if (mExportRayDensity) {	 
-	 return ExportKdTreeRayDensity(tree); }*/
+bool X3dExporter::ExportVspKdTree(const VspKdTree &tree, const int maxPvs)
+{
+	 //if (mExportRayDensity) {	 
+	 //return ExportKdTreeRayDensity(tree); }
 
 	stack<VspKdTreeNode *> tStack;
@@ -592,6 +592,9 @@
 	tStack.push(tree.GetRoot());
 
-	Mesh *mesh = new Mesh;
-  
+	//Mesh *mesh = new Mesh;
+  
+	if (maxPvs > 0)
+		mUseForcedMaterial = true;
+
 	while (!tStack.empty()) 
 	{
@@ -601,7 +604,9 @@
 		AxisAlignedBox3 box = tree.GetBBox(node);
 
+		Mesh *mesh = new Mesh;
+
 		// add 6 vertices of the box
 		int index = (int)mesh->mVertices.size();
-   
+
 		for (int i=0; i < 8; ++ i) 
 		{
@@ -619,4 +624,17 @@
 		mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
 
+		if (maxPvs > 0)
+		{
+            mForcedMaterial.mDiffuseColor.b = 1.0f;
+			const float importance = maxPvs;
+  			//float importance = (float)(*it)->GetPvs().GetSize() / (float)maxPvs;
+
+			mForcedMaterial.mDiffuseColor.r = importance;
+			mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
+		}
+
+		ExportMesh(mesh);
+		DEL_PTR(mesh);
+
         if (!node->IsLeaf()) 
 		{
@@ -627,6 +645,6 @@
 	}
   
-	ExportMesh(mesh);
-	DEL_PTR(mesh);
+	//ExportMesh(mesh);
+	//DEL_PTR(mesh);
 
 	return true;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 423)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 425)
@@ -503,4 +503,7 @@
 					 SimpleRayContainer &rays);
 
+	/** Collapses this subtree and releases the memory.
+		@returns number of collapsed rays.
+	*/
 	int CollapseSubtree(VspKdTreeNode *sroot, const int time);
 	
@@ -509,4 +512,9 @@
 	bool TerminationCriteriaMet(const VspKdTreeLeaf *leaf, 
 								const AxisAlignedBox3 &box) const;
+
+	/** Computes PVS size of this node given a global ray set.
+		@returns PVS size of the intersection of this node bounding box with the rays.
+	*/
+	int ComputePvsSize(VspKdTreeNode *node, const RayInfoContainer &globalRays) const;
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 423)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 425)
@@ -677,5 +677,5 @@
 		Exporter *exporter = Exporter::GetExporter("vspkdtree.x3d"); 
 		exporter->SetWireframe();
-		exporter->ExportVspKdTree(*mVspKdTree);
+		exporter->ExportVspKdTree(*mVspKdTree, 300);
 		delete exporter;
 	}
