Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1415)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1416)
@@ -356,9 +356,9 @@
 	
 	ResetViewCells();
-  	if (0) Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
-
-	//-- optionally export initial view cell partition
-	if (0) 
-	{
+  		
+	if (0) //-- optionally export initial view cell partition
+	{
+        Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
+
 		const string filename("viewcells.wrl");
 		Exporter *exporter = Exporter::GetExporter(filename.c_str());
@@ -374,5 +374,5 @@
 
 			exporter->SetWireframe();
-			ExportViewCellsForViz(exporter);
+			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
 
 			delete exporter;
@@ -429,6 +429,6 @@
 	
 #if 0
-	////////////////////////////////////////////////
-	//-- get stats after the additional sampling step
+	///////////////
+	//-- Get stats after the additional sampling step
 	//-- and before the bottom-up merge step
 
@@ -445,5 +445,5 @@
 
 
-	///////////////////////////////////////////////
+	////////////////////
 	//
 	//-- post processing of the initial construction
@@ -476,5 +476,5 @@
 
 	
-	//////////////////////////////////////////////////////////////
+	////////////////
 	//-- Evaluation of the resulting view cell partition. 
 	//-- We cast a number of new samples and measure the render cost
@@ -486,5 +486,5 @@
 
 	
-	///////////////////////////////////
+	/////////////////
 	//-- Finally, we do some visualization
 
@@ -508,4 +508,10 @@
 
 	return numSamples;
+}
+
+
+AxisAlignedPlane * ViewCellsManager::GetClipPlane()
+{
+	return mUseClipPlaneForViz ? &mClipPlaneForViz : NULL;
 }
 
@@ -1692,11 +1698,9 @@
 
 	Environment::GetSingleton()->GetFloatValue("ViewCells.Visualization.clipPlanePos", pos);
-
-	Vector3 point = mViewSpaceBox.Min() +  mViewSpaceBox.Size() * pos;
-
-	if (mUseClipPlaneForViz)
-        Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.clipPlaneAxis", axis);
-
-	mClipPlane = AxisAlignedPlane(axis, point[axis]);
+	Vector3 absPos = mViewSpaceBox.Min() +  mViewSpaceBox.Size() * pos;
+	
+	Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.clipPlaneAxis", axis);
+
+	mClipPlaneForViz = AxisAlignedPlane(axis, absPos[axis]);
 }
 
@@ -1946,6 +1950,9 @@
 }
 
-// matt: todo
-void ViewCellsManager::ExportViewCellsForViz(Exporter *exporter) const
+
+void ViewCellsManager::ExportViewCellsForViz(Exporter *exporter,
+											 const AxisAlignedBox3 *sceneBox,
+											 const AxisAlignedPlane *clipPlane
+											 ) const
 {
 	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
@@ -1956,7 +1963,5 @@
 		{
 			ExportColor(exporter, *it);	
-
-			ExportViewCellGeometry(exporter, *it, 
-				mUseClipPlaneForViz ? &mClipPlane : NULL);
+			ExportViewCellGeometry(exporter, *it, sceneBox, clipPlane);
 		}
 	}
@@ -2670,9 +2675,7 @@
 	const int savedColorCode = mColorCode;
 	
-	//BspLeaf::NewMail();
 	if (1) // export merged view cells
 	{
-		mColorCode = 0;
-		
+		mColorCode = 0; // hack color code
 		Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
 		
@@ -2683,10 +2686,10 @@
 		{
 			if (mExportGeometry)
+			{
 				exporter->ExportGeometry(objects);
-
-			//exporter->SetWireframe();
+			}
+
 			exporter->SetFilled();
-			ExportViewCellsForViz(exporter);
-
+			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
 
 			delete exporter;
@@ -2695,23 +2698,21 @@
 	}
 
-	if (1) // export merged view cells using pvs color coding
-	{
+	if (1) 
+	{
+		// export merged view cells using pvs color coding
 		mColorCode = 1;
 
 		Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
-	
 		cout << "exporting view cells after merge (pvs size) ... ";	
 
 		if (exporter)
 		{
-			//exporter->SetWireframe();
-			//exporter->SetForcedMaterial(RandomMaterial());
-
 			if (mExportGeometry)
+			{
 				exporter->ExportGeometry(objects);
-
-			//exporter->SetWireframe();
+			}
+			
 			exporter->SetFilled();
-			ExportViewCellsForViz(exporter);
+			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
 
 			delete exporter;
@@ -2769,16 +2770,10 @@
 		if (exporter)
 		{
-			//exporter->SetWireframe();
-			
 			if (mExportGeometry)
+			{
 				exporter->ExportGeometry(objects);
-
-			//exporter->SetFilled();
-			const bool b = mUseClipPlaneForViz;
-			mUseClipPlaneForViz = false;
-
-			ExportViewCellsForViz(exporter);
-			
-			mUseClipPlaneForViz = b;
+			}
+
+			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
 			delete exporter;
 		}
@@ -2788,5 +2783,8 @@
 	mColorCode = savedColorCode;
 
+
+	//////////////////
 	//-- visualization of the BSP splits
+
 	bool exportSplits = false;
 	Environment::GetSingleton()->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
@@ -2890,5 +2888,5 @@
 		exporter->SetForcedMaterial(m);
 
-		ExportViewCellGeometry(exporter, vc);
+		ExportViewCellGeometry(exporter, vc, NULL, NULL);
 		
 		// export rays piercing this view cell
@@ -2962,5 +2960,7 @@
 void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
 												 ViewCell *vc,
-												 const AxisAlignedPlane *clipPlane) const
+												 const AxisAlignedBox3 *sceneBox,
+												 const AxisAlignedPlane *clipPlane
+												) const
 {
 	// export mesh if available
@@ -3437,8 +3437,9 @@
 void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
 												ViewCell *vc,
-												const AxisAlignedPlane *clipPlane) const
+												const AxisAlignedBox3 *sceneBox,
+												const AxisAlignedPlane *clipPlane
+												) const
 {
 	ViewCellContainer leaves;
-
 	mViewCellsTree->CollectLeaves(vc, leaves);
 	ViewCellContainer::const_iterator it, it_end = leaves.end();
@@ -3447,5 +3448,4 @@
 	{
 		KdViewCell *kdVc = dynamic_cast<KdViewCell *>(*it);
-	
 		exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaf));
 	}
@@ -3718,5 +3718,5 @@
 				exporter->SetFilled();
 
-			ExportViewCellsForViz(exporter);
+			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
 
 			if (mExportGeometry)
@@ -3737,5 +3737,6 @@
 	if (1) 
 	{
-		mColorCode = 1; // export merged view cells using pvs coding
+		// use pvs size for color coding
+		mColorCode = 1;
 		Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
 
@@ -3744,10 +3745,7 @@
 		if (exporter)
 		{
-			if (0)
-				exporter->SetWireframe();
-			else
-				exporter->SetFilled();
-
-			ExportViewCellsForViz(exporter);
+			exporter->SetFilled();
+
+			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
 
 			if (mExportGeometry)
@@ -4033,12 +4031,10 @@
 	GetRaySets(sampleRays, mVisualizationSamples, visRays);
 
-	//-- export final view cell partition
-
+	
 	if (0) 
 	{	
-		// hack pvs
-		//const int savedColorCode = mColorCode;
-		//mColorCode = 1;
-	
+		//////////////////
+		//-- export final view cell partition
+
 		Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
 		
@@ -4074,21 +4070,10 @@
 			}
 
-			//exporter->SetFilled();
-
-			// HACK: export without clip plane
-			const bool b = mUseClipPlaneForViz;
-			//mUseClipPlaneForViz = false;
-
-			ExportViewCellsForViz(exporter);
-			
-			mUseClipPlaneForViz = b;
+			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
+
 			delete exporter;
-
 			cout << "finished" << endl;
 		}
-
-		//mColorCode = savedColorCode;
-	}
-
+	}
 
 	if (0)
@@ -4316,6 +4301,7 @@
 		}
 		
-
+		////////////////
 		//-- export view cell geometry
+
 		exporter->SetWireframe();
 
@@ -4324,12 +4310,11 @@
 		exporter->SetForcedMaterial(m);
 
-		ExportViewCellGeometry(exporter, vc);
-	
+		ExportViewCellGeometry(exporter, vc, NULL, NULL);
 		exporter->SetFilled();
 
-
-		//-- export pvs
 		if (1)
 		{
+			////////
+			//-- export pvs
 			ObjectPvsMap::const_iterator oit,
 				oit_end = pvs.mEntries.end();
@@ -4396,9 +4381,9 @@
 
 		exporter->SetForcedMaterial(RgbColor(0,1,0));
-		ExportViewCellGeometry(exporter, GetViewCell(viewPoint));
+		ExportViewCellGeometry(exporter, GetViewCell(viewPoint), NULL, NULL);
 
 		//exporter->ResetForcedMaterial();
 		exporter->SetForcedMaterial(RgbColor(0,0,1));
-		ExportViewCellGeometry(exporter, testPrVs.mViewCell);
+		ExportViewCellGeometry(exporter, testPrVs.mViewCell, NULL, NULL);
 
      	exporter->SetForcedMaterial(RgbColor(1,0,0));
@@ -4491,5 +4476,5 @@
 		m.mDiffuseColor.b = 1.0f;
 		//exporter->SetForcedMaterial(m);
-		//ExportViewCellGeometry(exporter, vc, mClipPlane);
+		//ExportViewCellGeometry(exporter, vc, mClipPlaneForViz);
 
 		/*	// counting the pvss
@@ -4510,6 +4495,8 @@
 
 void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
-                                                    ViewCell *vc,
-													const AxisAlignedPlane *clipPlane) const
+													ViewCell *vc,
+													const AxisAlignedBox3 *sceneBox,
+													const AxisAlignedPlane *clipPlane
+													) const
 {
 	if (clipPlane)
@@ -5013,5 +5000,7 @@
 void VspOspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
 													ViewCell *vc,
-													const AxisAlignedPlane *clipPlane) const
+													const AxisAlignedBox3 *sceneBox,
+													const AxisAlignedPlane *clipPlane
+													) const
 {
 	ViewCellContainer leaves;
@@ -5021,12 +5010,7 @@
 
 	Plane3 plane;
-	
+
 	if (clipPlane)
-	{
 		plane = clipPlane->GetPlane();
-	}
-
-	AxisAlignedBox3 bbox = GetViewSpaceBox();
-	bbox.Scale(Vector3(0.5, 1, 0.5));			
 
 	for (it = leaves.begin(); it != it_end; ++ it)
@@ -5035,7 +5019,8 @@
 		VspLeaf *l = vspVc->mLeaf;
 
-		const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(vspVc->mLeaf);
+		const AxisAlignedBox3 box = 
+			mHierarchyManager->GetVspTree()->GetBoundingBox(vspVc->mLeaf);
 		
-		if (!Overlap(box, bbox))
+		if (!Overlap(*sceneBox, box))
 			continue;
 
@@ -5049,5 +5034,5 @@
 			{
 				AxisAlignedBox3 fbox, bbox;
-				box.Split(clipPlane->mAxis, clipPlane->mPosition, fbox, bbox);
+				bbox.Split(clipPlane->mAxis, clipPlane->mPosition, fbox, bbox);
 
 				exporter->ExportBox(bbox);
@@ -5084,5 +5069,5 @@
 	if (1) 
 	{	
-		/////////////////////////////
+		////////////
 		//-- export final view cells
 
@@ -5098,7 +5083,11 @@
 			cout << "exporting final view cells (after initial construction + post process) ... ";
 
+			// matt: hack
+			AxisAlignedBox3 bbox = mHierarchyManager->GetViewSpaceBox();
+			bbox.Scale(Vector3(0.5, 1, 0.5));
+
 			if (mExportGeometry)
 			{
-				exporter->ExportGeometry(objects, true);
+				exporter->ExportGeometry(objects, true, &bbox);
 			}
 
@@ -5109,13 +5098,7 @@
 			}
 
-			// HACK: export without clip plane
-			const bool b = mUseClipPlaneForViz;
-			if (0) mUseClipPlaneForViz = false;
-
-			ExportViewCellsForViz(exporter);
-			
-			mUseClipPlaneForViz = b;
+			ExportViewCellsForViz(exporter, &bbox, GetClipPlane());
+		
 			delete exporter;
-
 			cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
 		}
@@ -5132,7 +5115,11 @@
 		{
 			const long starttime = GetTime();
+			
+			// matt: hack
+			AxisAlignedBox3 bbox = mHierarchyManager->GetObjectSpaceBox();
+			bbox.Scale(Vector3(0.5, 1, 0.5));
 
 			cout << "exporting object space hierarchy ... ";
-			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects);
+			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, &bbox);
 		
 			delete exporter;
@@ -5187,5 +5174,4 @@
 		mViewCellsTree->GetPvs(vc, pvs);
 
-		//bspLeaves[j]->Mail();
 		char s[64]; sprintf(s, "bsp-pvs%04d.wrl", i);
 		Exporter *exporter = Exporter::GetExporter(s);
@@ -5266,10 +5252,9 @@
 		exporter->SetWireframe();
 
-		Material m;//= RandomMaterial();
+		Material m;
 		m.mDiffuseColor = RgbColor(0, 1, 0);
 		exporter->SetForcedMaterial(m);
 
-		ExportViewCellGeometry(exporter, vc);
-	
+		ExportViewCellGeometry(exporter, vc, NULL, NULL);
 		exporter->SetFilled();
 
