Index: GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 1919)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 1920)
@@ -206,5 +206,5 @@
 				Name="VCLinkerTool"
 				AdditionalDependencies="xerces-c_2.lib glew32.lib zdll.lib zziplib.lib devil.lib glut32.lib OpenGL32.Lib glu32.lib Preprocessor.lib RTScene.lib RTWorld.lib QtCore4.lib qtmain.lib QtOpenGL4.lib Qt3Support4.lib QtTest4.lib QtGui4.lib QtGlRenderer.lib"
-				OutputFile="../bin/release/Preprocessor.exe"
+				OutputFile="../bin/release/Preprocessor2.exe"
 				LinkIncremental="1"
 				AdditionalLibraryDirectories="..\src\GL;..\lib\release;..\..\Preprocessing\lib\release;..\..\..\..\..\..\NonGTP\Boost\lib;..\..\..\..\..\..\NonGTP\Xerces\xercesc\lib;..\..\..\..\..\..\NonGTP\Zlib\lib;..\..\..\..\..\..\NonGTP\Devil\lib;..\MultiLevelRayTracing\RTScene\Release;..\MultiLevelRayTracing\RTWorld\Release;&quot;$(QTDIR)\lib&quot;;.\QtGlRenderer\Release"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1919)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1920)
@@ -1029,5 +1029,5 @@
 							   VssRayContainer *visRays = NULL);
 
-
+	
 protected:
 
@@ -1162,4 +1162,6 @@
 							   const string prefix,
 							   VssRayContainer *visRays = NULL);
+	
+	float UpdateObjectCosts();
 
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1867)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1920)
@@ -154,5 +154,5 @@
   bool ExportBvHierarchy(
 	  const BvHierarchy &bvHierarchy, 
-	  const int maxPvs, 
+	  const float maxPvs, 
 	  AxisAlignedBox3 *box = NULL,
 	  const bool exportBoundingBoxes = true);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1919)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1920)
@@ -247,8 +247,4 @@
 	VspTree *GetVspTree();
 
-	/** Returns view space bounding box.
-	*/
-	//AxisAlignedBox3 GetViewSpaceBox() const;
-
 	/** Returns object space bounding box.
 	*/
@@ -260,4 +256,5 @@
 									const ObjectContainer &objects,
 									AxisAlignedBox3 *bbox,
+									const float maxRenderCost,
 									const bool exportBounds = true) const;
 
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) 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1843)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1920)
@@ -105,5 +105,5 @@
 
 bool Exporter::ExportBvHierarchy(const BvHierarchy &bvHierarchy, 
-								 const int maxPvs,
+								 const float maxPvs,
 								 AxisAlignedBox3 *box,
 								 const bool exportBoundingBoxes)
@@ -135,8 +135,8 @@
 		}
 
-        if (maxPvs) // color code pvs
+        if (maxPvs > 0) // color code pvs
 		{
 			mForcedMaterial.mDiffuseColor.b = 1.0f;
-  			const float importance = (float)leaf->mObjects.size() / (float)maxPvs;
+  			const float importance = (float)leaf->mRenderCost / (float)maxPvs;
 
 			mForcedMaterial.mDiffuseColor.r = importance;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1913)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1920)
@@ -308,5 +308,5 @@
 	*/
 	BvhLeaf(const AxisAlignedBox3 &bbox);
-  BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent);
+	BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent);
 	BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent, const int numObjects);
 
@@ -349,4 +349,5 @@
   /// objects
   ObjectContainer mObjects;
+  
   
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1919)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1920)
@@ -1499,4 +1499,5 @@
 												  const ObjectContainer &objects,
 												  AxisAlignedBox3 *bbox,
+												  const float maxRenderCost,
 												  const bool exportBounds) const
 {
@@ -1510,5 +1511,5 @@
 	case BV_BASED_OBJ_SUBDIV:
 		{
-			exporter->ExportBvHierarchy(*mBvHierarchy, 0, bbox, exportBounds);
+			exporter->ExportBvHierarchy(*mBvHierarchy, maxRenderCost, bbox, exportBounds);
 			break;
 		}
