Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1919)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1920)
@@ -2991,6 +2991,6 @@
 	// special color code for invalid view cells
 	m.mDiffuseColor.r = importance;
-	m.mDiffuseColor.g = vcValid ? 0.0f : 1.0f;
-	m.mDiffuseColor.b = 1.0f - importance;
+	m.mDiffuseColor.b = 1.0f;//vcValid ? 0.0f : 1.0f;
+	m.mDiffuseColor.g = 1.0f - importance;
 
 	//Debug << "importance: " << importance << endl;
@@ -5650,4 +5650,58 @@
 
 
+float VspOspViewCellsManager::UpdateObjectCosts()
+{
+	float maxRenderCost = 0;
+
+	cout << "updating object pvs cost ... ";
+	const long startTime = GetTime();
+
+	ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
+
+	Intersectable::NewMail();
+
+	const float invViewSpaceVol = 1.0f / GetViewSpaceBox().GetVolume();
+
+	for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
+	{
+		ViewCell *vc = *vit;
+
+		ObjectPvsIterator pit = vc->GetPvs().GetIterator();
+
+		// output PVS of view cell
+		while (pit.HasMoreEntries())
+		{		
+			ObjectPvsEntry entry = pit.Next();
+				
+			BvhNode *node = dynamic_cast<BvhNode *>(entry.mObject);
+			
+			// hack!!
+			if (!node->IsLeaf())
+			{
+				cout << "error, can only do leaves" << endl;
+				return 0;
+			}
+	
+			if (!node->Mailed())
+			{
+				node->Mail();
+				node->mRenderCost = 0;
+			}
+
+			const float rc = (float)((BvhLeaf *)node)->mObjects.size();
+
+			node->mRenderCost += rc * vc->GetVolume() * invViewSpaceVol;
+
+			if (node->mRenderCost > maxRenderCost)
+				maxRenderCost = node->mRenderCost;
+		}
+	}
+
+	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3f << " secs" << endl;
+
+	return maxRenderCost;
+}
+
+
 void VspOspViewCellsManager::Visualize(const ObjectContainer &objects,
 									   const VssRayContainer &sampleRays)
@@ -5664,6 +5718,6 @@
 	Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
 
-	Vector3 scale(0.9f, 0.9f, 0.9f);
-	//Vector3 scale(1.0f, 1.0f, 1.0f);
+	//Vector3 scale(0.9f, 0.9f, 0.9f);
+	Vector3 scale(1.0f, 1.0f, 1.0f);
 
 	if (exporter)
@@ -5683,15 +5737,19 @@
 		bbox.Scale(scale);
 
-		if (1 && mExportRays)
+		if (0 && mExportRays)
 		{	
 			exporter->ExportRays(visRays, RgbColor(0, 1, 0));
 		}
 
-		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, 
-													  CLAMP_TO_BOX ? &bbox : NULL, false);
-		
 		// hack color code (show pvs size)
 		const int savedColorCode = mColorCode;
-		mColorCode = 1; // export pvs
+
+		const float maxRenderCost = UpdateObjectCosts();
+		cout << "maxRenderCost: " << maxRenderCost << endl;
+		mColorCode = 0; // 0 = random, 1 = export pvs
+
+		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, 
+													  CLAMP_TO_BOX ? &bbox : NULL, maxRenderCost, false);
+		
 
 		//ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
@@ -5704,5 +5762,4 @@
 	}
 
-	// export final object partition
 	exporter = Exporter::GetExporter("final_object_partition.wrl");
 
@@ -5714,4 +5771,43 @@
 		}
 
+		EvaluateViewCellsStats();
+
+		const long starttime = GetTime();
+		cout << "exporting final objects (after initial construction + post process) ... ";
+
+		// matt: hack for clamping scene
+		AxisAlignedBox3 bbox = mViewSpaceBox;
+		bbox.Scale(scale);
+
+		// hack color code (show pvs size)
+		const int savedColorCode = mColorCode;
+
+		mColorCode = 1; // 0 = random, 1 = export pvs
+		const float maxRenderCost = -1;
+
+		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, 
+													  CLAMP_TO_BOX ? &bbox : NULL, maxRenderCost, false);
+		
+
+		//ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
+		ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
+
+		delete exporter;
+
+		cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
+		mColorCode = savedColorCode;
+	}
+
+#if 0
+	// export final object partition
+	exporter = Exporter::GetExporter("final_object_partition.wrl");
+
+	if (exporter)
+	{
+		if (CLAMP_TO_BOX)
+		{	
+			exporter->mClampToBox = true; 	
+		}
+
 		const long starttime = GetTime();
 
@@ -5721,10 +5817,11 @@
 
 		cout << "exporting object space hierarchy ... ";
-		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, CLAMP_TO_BOX ? &bbox : NULL);
+		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, CLAMP_TO_BOX ? &bbox : NULL, -1);
 
 		delete exporter;
 		cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
 	}
-	
+#endif
+
 	// visualization of the view cells
     if (0) 
