Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 597)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 598)
@@ -36,5 +36,5 @@
 	loadInitialSamples  false
 	storeInitialSamples false
-	useViewSpaceBox false
+	useViewSpaceBox true
 #	testBeamSampling true
 }
@@ -213,5 +213,5 @@
 		useRaysForMerge false
 		refine false
-		compress true
+		compress false
 		merge false
 	}
@@ -322,14 +322,16 @@
 	Termination {
 		# parameters used for autopartition
-		minRays 		150
+		minRays 		-150
 		minPolygons 		-1
 		maxDepth		30
-		minPvs 			10
+		minPvs 			-10
 		#minProbability		0.0001
 		minProbability 		-1
-		maxRayContribution	0.3
-		maxCostRatio		0.9
+#		maxRayContribution	0.3
+		maxRayContribution	2.3
+#		maxCostRatio		0.9
+		maxCostRatio		3.9
 		missTolerance		3
-		#maxAccRayLength	100
+
 		
 		maxViewCells		512
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 589)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 598)
@@ -2273,44 +2273,5 @@
 	}
 }
-#if 0
-void VspKdTree::CollectMergeCandidates(const vector<VspKdRay *> &rays)
-{
-	VspKdMergeCandidate::sOverallCost = 0;
-
-	vector<VspKdIntersection>::const_iterator iit;
-	map<BspLeaf *, vector<BspLeaf*> candidateMap;
-
-	for (int i = 0; i < (int)rays.size(); ++ i)
-	{  
-		//VspKdLeaf::NewMail();
-		VspKdRay *ray = rays[i];
-	  
-		// traverse leaves stored in the rays and compare and merge consecutive
-		// leaves (i.e., the neighbors in the tree)
-		if (ray->intersections.size() < 2)
-			continue;
-	  
-		iit = ray->intersections.begin();
-
-		BspLeaf *previousLeaf = (*iit).mLeaf;
-	
-		++ iit;
-		
-		for (; iit != ray->intersections.end(); ++ iit)
-		{
-            BspLeaf *leaf = (*iit).mLeaf;
-			leaf->Mail
-			// TODO: how to sort out doubles?
-			VspKdMergeCandidate mc = VspKdMergeCandidate(leaf, previousLeaf);
-			mMergeQueue.push(mc);
-
-			VspKdMergeCandidate::sOverallCost += mc.GetLeaf1Cost();
-			VspKdMergeCandidate::sOverallCost += mc.GetLeaf2Cost();
-
-			previousLeaf = leaf;
-        }
-	}
-}
-#endif
+
 
 int VspKdTree::MergeViewCells(const VssRayContainer &rays)
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 590)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 598)
@@ -41,4 +41,5 @@
 {
   CLEAR_CONTAINER(mVssRays);
+  DEL_PTR(mViewSpaceBox);
 }
 
@@ -526,5 +527,19 @@
   if (mUseViewSpaceBox)
   {
-	  mViewSpaceBox = box;
+	  if (0)
+		mViewSpaceBox = box;
+	  else
+	  {
+		// HACK: enlarge in y directon
+		mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
+		//Vector3 pmin = mViewSpaceBox->Min();
+		Vector3 size = mViewSpaceBox->Size();
+		//size[1] *= 1.25;
+		Vector3 enlarge(size[0] * 0.1f, size[1] * 0.0f, size[2] * 0.1);
+
+		mViewSpaceBox->Enlarge(enlarge);
+	  }
+
+	  //Debug << "view space box: " << *mViewSpaceBox << endl;
   }
   else
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 597)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 598)
@@ -85,6 +85,9 @@
 {
 	DEL_PTR(mRenderer);
-	//CLEAR_CONTAINER(mViewCells);
-	DEL_PTR(mViewCellsTree);
+
+	if (!ViewCellsTreeConstructed())
+		CLEAR_CONTAINER(mViewCells);
+	else
+		DEL_PTR(mViewCellsTree);
 
 	CLEAR_CONTAINER(mMeshContainer);
@@ -689,5 +692,6 @@
 	int axis = 0;
 
-	Vector3 point = mViewSpaceBox.Center();
+	const float factor = 0.55;
+	Vector3 point = mViewSpaceBox.Min() +  mViewSpaceBox.Size() * factor;
 
 	if (mUseCuttingPlaneForViz)
@@ -1277,4 +1281,9 @@
 	if (exporter)
 	{
+		//exporter->SetFilled();
+
+		if (mExportGeometry)
+			exporter->ExportGeometry(objects);
+
 		Material m;
 		m.mDiffuseColor = RgbColor(1, 0, 0);
@@ -1288,9 +1297,4 @@
 		exporter->SetForcedMaterial(m);
 		//exporter->ResetForcedMaterial();
-
-		exporter->SetFilled();
-
-		if (mExportGeometry)
-			exporter->ExportGeometry(objects);
 
 		delete exporter;
@@ -1402,29 +1406,50 @@
 									  ViewCell *vc) const
 {
-	if (mColorCode == 0) // Random color
-	{
-		exporter->ResetForcedMaterial();
-		return;
-	}
+		const bool vcValid = CheckValidity(vc, mMinPvsSize, mMaxPvsSize);
 
 	float importance = 0;
+	static Material m;
 
 	switch (mColorCode)
 	{
+	case 0: // Random
+		{
+			if (vcValid)
+			{
+				m.mDiffuseColor.r = 0.5f + RandomValue(0.0f, 0.5f);
+				m.mDiffuseColor.g = 0.5f + RandomValue(0.0f, 0.5f);
+				m.mDiffuseColor.b = 0.5f + RandomValue(0.f, 0.5f);
+			}
+			else
+			{
+				m.mDiffuseColor.r = 0.0f;
+				m.mDiffuseColor.g = 1.0f;
+				m.mDiffuseColor.b = 0.0f;
+			}
+
+			exporter->SetForcedMaterial(m);
+			return;
+		}
+		
 	case 1: // pvs
 		{
 			importance = (float)vc->GetPvs().GetSize() /
 				(float)mViewCellsStats.maxPvs;
+
 		}
 		break;
 	case 2: // merges
 		{
-			importance = (float)mViewCellsTree->GetSize(vc) /
-				(float)mViewCellsStats.maxLeaves;
-		}
-		break;
+            int lSize = mViewCellsTree->GetSize(vc);
+	
+			importance = (float)lSize / (float)mViewCellsStats.maxLeaves;
+		}
+		//break;
 	case 3: // merge tree differene
 		{
 			// TODO
+			//importance = (float)GetMaxTreeDiff(vc) /
+			//	(float)(mVspBspTree->GetStatistics().maxDepth * 2);
+
 		}
 		break;
@@ -1433,9 +1458,10 @@
 	}
 
-	Material m;
-	m.mDiffuseColor.b = 1.0f;
+	// special color code for invalid view cells
 	m.mDiffuseColor.r = importance;
 	m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;
-
+	m.mDiffuseColor.b = vcValid ? 1.0f : 0.0f;
+
+	//Debug << "importance: " << importance << endl;
 	exporter->SetForcedMaterial(m);
 }
@@ -1466,5 +1492,5 @@
 						   *cuttingPlane,
 						   mViewSpaceBox, 
-						   0.005);
+						   0.0001);
 
 		exporter->ExportPolygons(back.mPolys);
@@ -2049,10 +2075,10 @@
 		return;
 
+	if (mExportGeometry)
+		exporter->ExportGeometry(objects);
+	
 	//exporter->SetWireframe();
 	//exporter->ExportVspKdTree(*mVspKdTree, mVspKdTree->GetStatistics().maxPvsSize);
 	exporter->ExportVspKdTree(*mVspKdTree);
-
-	if (mExportGeometry)
-		exporter->ExportGeometry(objects);
 
 	if (mExportRays)
@@ -2416,13 +2442,13 @@
 		if (exporter)
 		{
+			if (0 && mExportRays)
+				exporter->ExportRays(rays, RgbColor(1, 1, 1));
+
+			if (mExportGeometry)
+				exporter->ExportGeometry(objects);
+
 			//exporter->SetWireframe();
 			exporter->SetFilled();
 			ExportViewCellsForViz(exporter);
-
-			if (0 && mExportRays)
-				exporter->ExportRays(rays, RgbColor(1, 1, 1));
-
-			if (mExportGeometry)
-				exporter->ExportGeometry(objects);
 
 			delete exporter;
@@ -2497,10 +2523,4 @@
 		if (exporter)
 		{
-			if (0)
-				exporter->SetWireframe();
-			else
-				exporter->SetFilled();
-			ExportViewCellsForViz(exporter);
-
 			if (mExportGeometry)
 			{
@@ -2513,4 +2533,11 @@
 			}
 
+			if (0)
+				exporter->SetWireframe();
+			else
+				exporter->SetFilled();
+
+			ExportViewCellsForViz(exporter);
+
 			delete exporter;
 		}
@@ -2600,5 +2627,7 @@
 		RefineViewCells(postProcessRays, objects);
 
-	if (mCompressViewCells)
+	//-- compression
+
+	if (ViewCellsTreeConstructed() && mCompressViewCells)
 	{
 		int pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
@@ -2628,5 +2657,5 @@
 
 
-	//-- export shuffled view cells
+	//-- export refined (shuffled) view cells
 	if (0)
 	{
@@ -2701,9 +2730,4 @@
 	// real meshes are only contructed only at this stage
 	FinalizeViewCells(true);
-
-	// HACK: removes view cells in bsp leaves with active ones
-	if (0)
-		AddCurrentViewCellsToHierarchy();
-
 
 	// write view cells to disc
@@ -3056,7 +3080,7 @@
 			if (vcValid)
 			{
-				m.mDiffuseColor.r = RandomValue(0.3f, 1.0f);
-				m.mDiffuseColor.g = RandomValue(0.3f, 1.0f);
-				m.mDiffuseColor.b = RandomValue(0.3f, 1.0f);
+				m.mDiffuseColor.r = 0.5f + RandomValue(0.0f, 0.5f);
+				m.mDiffuseColor.g = 0.5f + RandomValue(0.0f, 0.5f);
+				m.mDiffuseColor.b = 0.5f + RandomValue(0.f, 0.5f);
 			}
 			else
@@ -3140,5 +3164,5 @@
 							   *cuttingPlane,
 							   mViewSpaceBox, 
-							   0.005);
+							   0.0001);
 
 			exporter->ExportPolygons(back.mPolys);
