Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 590)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 591)
@@ -36,5 +36,5 @@
 	loadInitialSamples  false
 	storeInitialSamples false
-	useViewSpaceBox true
+	useViewSpaceBox false
 #	testBeamSampling true
 }
@@ -181,9 +181,9 @@
 
 	#number of active view cells
-	active 169
+	active 256
 	maxStaticMemory	40
 
 	exportToFile false
-	loadFromFile true
+	loadFromFile false
 
 	#type kdTree
@@ -225,4 +225,6 @@
 		exportGeometry true
 		exportMergedViewCells false
+		useCuttingPlane true
+		cuttingPlaneAxis 1
 	}
 	
@@ -300,6 +302,6 @@
 	
 	# maximal candidates for split planes
-	maxPolyCandidates 100
-	#maxRayCandidates 100
+	maxPolyCandidates 0
+
 	
 	# maximal tested rays for split cost heuristics
@@ -340,5 +342,5 @@
 	}
 	
-	splitUseOnlyDrivingAxis false
+	splitUseOnlyDrivingAxis true
 	
 	Visualization {
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 590)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 591)
@@ -123,6 +123,9 @@
 		float GetCost() const
 		{
+#if 0
+			return mPvs * mProbability;
+#endif
 #if 1
-			return mPvs * mProbability;
+			return - mDepth;
 #endif
 #if 0
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 590)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 591)
@@ -93,7 +93,9 @@
 		@returns number of sample contributions
 	*/
-	virtual float ComputeSampleContributions(VssRay &ray, const bool addRays, const bool storeViewCells);
-
-  virtual void AddSampleContributions(VssRay &ray);
+	virtual float ComputeSampleContributions(VssRay &ray, 
+											 const bool addRays, 
+											 const bool storeViewCells);
+
+	virtual void AddSampleContributions(VssRay &ray);
 
 	/** Prints out statistics of the view cells.
@@ -318,15 +320,18 @@
 	/** Exports view cell geometry.
 	*/
-	virtual void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const = 0;
+	virtual void ExportViewCellGeometry(Exporter *exporter, 
+										ViewCell *vc,
+										const Plane3 *cuttingPlane = NULL) const = 0;
 
 	virtual void FinalizeViewCells(const bool createMesh);
 
 
-	/** Loads view cells from file. The view cells manager is created with respect to the loaded
-		view cells.
+	/** Loads view cells from file. The view cells manager is created with 
+		respect to the loaded view cells.
 
 		@returns the view cells manager if loading was successful, false otherwise
 	*/
-	static ViewCellsManager *LoadViewCells(const string filename, ObjectContainer *objects);
+	static ViewCellsManager *LoadViewCells(const string filename, 
+										   ObjectContainer *objects);
 
 	/** Evaluates statistics values on view cells.
@@ -351,4 +356,5 @@
 protected:
 
+
 	/**
 		if the view cells tree was already constructed or not.
@@ -411,4 +417,11 @@
 	*/
 	virtual void PrepareLoadedViewCells() {};
+
+
+	void CreateCuttingPlane();
+
+	Plane3 mCuttingPlane;
+	bool mUseCuttingPlaneForViz;
+
 
 	/// Renders the view cells.
@@ -446,4 +459,5 @@
 	bool mExportViewCells;
 
+	//bool mMarchTree);
 	bool mOnlyValidViewCells;
 
@@ -451,5 +465,5 @@
 	bool mUseRaysForMerge;
 	
-
+	
 	//-- visualization options
 	
@@ -507,5 +521,7 @@
 	void CreateMesh(ViewCell *vc);
 
-	void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
+	void ExportViewCellGeometry(Exporter *exporter, 
+								ViewCell *vc, 
+								const Plane3 *cuttingPlane = NULL) const;
 	
 	void CollectMergeCandidates(const VssRayContainer &rays, 
@@ -584,7 +600,10 @@
 	void CreateMesh(ViewCell *vc);
 
-	void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
-
-	void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
+	void ExportViewCellGeometry(Exporter *exporter, 
+								ViewCell *vc,
+								const Plane3 *cuttingPlane = NULL) const;
+
+	void CollectMergeCandidates(const VssRayContainer &rays, 
+								vector<MergeCandidate> &candidates);
 
 protected:
@@ -647,5 +666,7 @@
 	void CreateMesh(ViewCell *vc);
 
-	void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
+	void ExportViewCellGeometry(Exporter *exporter, 
+								ViewCell *vc,
+								const Plane3 *cuttingPlane = NULL) const;
 
 	void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
@@ -715,5 +736,7 @@
 	int CastBeam(Beam &beam);
 
-	void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
+	void ExportViewCellGeometry(Exporter *exporter, 
+								ViewCell *vc,
+								const Plane3 *cuttingPlane = NULL) const;
 
 	//float GetVolume(ViewCell *viewCell) const;
@@ -748,7 +771,9 @@
 	void PrepareLoadedViewCells();
 
+	
 	/// the view space partition BSP tree.
 	VspBspTree *mVspBspTree;
 
+
 private:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 587)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 591)
@@ -1330,4 +1330,9 @@
 		"view_cells_post_process_use_rays_for_merge=", 
 		"false");
+	
+	RegisterOption("ViewCells.PostProcess.merge", 
+		optBool,
+		"view_cells_post_merge=", 
+		"true");
 
 	RegisterOption("ViewCells.Visualization.exportMergedViewCells",
@@ -1340,4 +1345,15 @@
 		"view_cells_max_static_mem=", 
 		"8.0");
+
+	RegisterOption("ViewCells.Visualization.useCuttingPlane",
+		optBool, 
+		"view_cells_viz_use_cutting_plane=", 
+		"false");
+
+	
+	RegisterOption("ViewCells.Visualization.cuttingPlaneAxis",
+		optInt, 
+		"view_cells_viz_cutting_plane_axis=", 
+		"0");
 
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 590)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 591)
@@ -54,8 +54,13 @@
 	environment->GetIntValue("ViewCells.Construction.samplesPerPass", mSamplesPerPass);
 	environment->GetBoolValue("ViewCells.exportToFile", mExportViewCells);
-	environment->GetBoolValue("ViewCells.PostProcess.useRaysForMerge", mUseRaysForMerge);
+	environment->GetBoolValue("ViewCells.PostProcess.e", mUseMerge);
 	
 	environment->GetIntValue("ViewCells.active", mNumMergedViewCells);
 	environment->GetBoolValue("ViewCells.PostProcess.compress", mCompressViewCells);
+	environment->GetBoolValue("ViewCells.Visualization.useCuttingPlane", mUseCuttingPlaneForViz);
+
+	environment->GetBoolValue("ViewCells.Visualization.", mUseCuttingPlaneForViz);
+
+	environment->GetBoolValue("ViewCells.PostProcess.merge", mMergeViewCells);
 
 	mMinPvsSize = emptyViewCells ? 1 : 0;
@@ -673,5 +678,24 @@
 {
 	mViewSpaceBox = box;
+
+	CreateCuttingPlane();
+	
 	mTotalAreaValid = false;
+}
+
+
+void ViewCellsManager::CreateCuttingPlane()
+{
+	int axis = 0;
+
+	Vector3 point = mViewSpaceBox.Center();
+
+	if (mUseCuttingPlaneForViz)
+        environment->GetIntValue("ViewCells.Visualization.cuttingPlaneAxis", axis);
+
+	Vector3 normal(0,0,0);
+	normal[axis] = 1;
+
+	mCuttingPlane = Plane3(normal, point);
 }
 
@@ -872,5 +896,6 @@
 		{
 			ExportColor(exporter, *it);		
-			ExportViewCellGeometry(exporter, *it);		
+			ExportViewCellGeometry(exporter, *it, 
+				mUseCuttingPlaneForViz ? &mCuttingPlane : NULL);
 		}
 	}
@@ -1027,13 +1052,12 @@
 	// view cells tree constructed
 	if (!ViewCellsTreeConstructed())
-	{
-		
+	{		
 		mBspTree->CollectViewCells(mViewCells);
 	}
 	else 
 	{
-		
 		// we can use the view cells tree hierarchy to get the right set
-		mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumMergedViewCells);
+		mViewCellsTree->CollectBestViewCellSet(mViewCells, 
+											   mNumMergedViewCells);
 	}
 }
@@ -1043,9 +1067,8 @@
 {
 	// compute view cell area as subsititute for probability
-#if 1
-	return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
-#else
-	return GetArea(viewCell) / GetAccVcArea();
-#endif
+	if (1)
+		return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
+	else
+		return GetArea(viewCell) / GetAccVcArea();
 }
 
@@ -1125,23 +1148,29 @@
 	}
 
-	cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
-
-	long startTime = GetTime();
 	
-	VssRayContainer postProcessRays;
-	GetRaySets(rays, mPostProcessSamples, postProcessRays);
-
-	//-- merge or subdivide view cells
-	int merged = mViewCellsTree->ConstructMergeTree(rays, objects);
-
-	//-- stats and visualizations
-	cout << "finished" << endl;
-	cout << "merged " << merged << " view cells in "
-		 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
-
-	Debug << "Postprocessing: Merged " << merged << " view cells in "
-		  << TimeDiff(startTime, GetTime()) *1e-3 << " secs"
-		  << "using " << (int)rays.size() << " samples" << endl << endl;
-
+
+	//-- merge view cells
+	int merged;
+	
+	const bool mergeBspLeaves = false;
+	if (mergeBspLeaves)
+	{
+		cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
+		long startTime = GetTime();
+	
+		VssRayContainer postProcessRays;
+		GetRaySets(rays, mPostProcessSamples, postProcessRays);
+
+		merged = mViewCellsTree->ConstructMergeTree(rays, objects);
+
+		//-- stats and visualizations
+		cout << "finished" << endl;
+		cout << "merged " << merged << " view cells in "
+			 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
+
+		Debug << "Postprocessing: Merged " << merged << " view cells in "
+			<< TimeDiff(startTime, GetTime()) *1e-3 << " secs"
+			<< "using " << (int)rays.size() << " samples" << endl << endl;
+	}
 
 	// reset view cells and stats
@@ -1415,9 +1444,11 @@
 
 void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
-												 ViewCell *vc) const
+												 ViewCell *vc,
+												 const Plane3 *cuttingPlane) const
 {
 	if (vc->GetMesh())
 	{
 		exporter->ExportMesh(vc->GetMesh());
+	
 		return;
 	}
@@ -1425,5 +1456,23 @@
 	BspNodeGeometry geom;
 	mBspTree->ConstructGeometry(vc, geom);
-	exporter->ExportPolygons(geom.mPolys);
+
+
+	if (cuttingPlane)
+	{
+		BspNodeGeometry front;
+		BspNodeGeometry back;
+
+		geom.SplitGeometry(front,
+						   back,
+						   *cuttingPlane,
+						   mViewSpaceBox, 
+						   0.005);
+
+		exporter->ExportPolygons(back.mPolys);
+	}
+	else
+	{
+		exporter->ExportPolygons(geom.mPolys);
+	}
 }
 
@@ -1828,5 +1877,6 @@
 
 void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
-												ViewCell *vc) const
+												ViewCell *vc,
+												const Plane3 *cuttingPlane) const
 {
 	ViewCellContainer leaves;
@@ -2215,5 +2265,6 @@
 
 void VspKdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
-										   ViewCell *vc) const
+												   ViewCell *vc,
+												   const Plane3 *cuttingPlane) const
 {
 	VspKdViewCell *kdVc = dynamic_cast<VspKdViewCell *>(vc);
@@ -2542,20 +2593,24 @@
 	}
 
-	//-- merge the individual view cells
-	MergeViewCells(postProcessRays, objects);
-
-	//-- refines the merged view cells
-	if (0)
-		RefineViewCells(postProcessRays, objects);
-
-	if (mCompressViewCells)
-	{
-		int pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
-		Debug << "number of entries before compress: " << pvsEntries << endl;
-
-		mViewCellsTree->CompressViewCellsPvs();
-
-		pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
-		Debug << "number of entries after compress: " << pvsEntries << endl;
+	if (mMergeViewCells)
+	{
+		//-- merge the individual view cells
+		MergeViewCells(postProcessRays, objects);
+	
+
+		//-- refines the merged view cells
+		if (0)
+			RefineViewCells(postProcessRays, objects);
+
+		if (mCompressViewCells)
+		{
+			int pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
+			Debug << "number of entries before compress: " << pvsEntries << endl;
+
+			mViewCellsTree->CompressViewCellsPvs();
+
+			pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
+			Debug << "number of entries after compress: " << pvsEntries << endl;
+		}
 	}
 
@@ -3005,5 +3060,7 @@
 			if (vcValid)
 			{
-				m = RandomMaterial();
+				m.mDiffuseColor.r = RandomValue(0.3f, 1.0f);
+				m.mDiffuseColor.g = RandomValue(0.3f, 1.0f);
+				m.mDiffuseColor.b = RandomValue(0.3f, 1.0f);
 			}
 			else
@@ -3013,6 +3070,9 @@
 				m.mDiffuseColor.b = 0.0f;
 			}
-		}
-		return;
+
+			exporter->SetForcedMaterial(m);
+			return;
+		}
+		
 	case 1: // pvs
 		{
@@ -3051,5 +3111,6 @@
 
 void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
-													ViewCell *vc) const
+                                                    ViewCell *vc,
+													const Plane3 *cuttingPlane) const
 {
 	if (vc->GetMesh())
@@ -3060,7 +3121,39 @@
 	}
 
-	BspNodeGeometry geom;
-	mVspBspTree->ConstructGeometry(vc, geom);
-	exporter->ExportPolygons(geom.mPolys);
+	
+	if (cuttingPlane)
+	{
+		ViewCellContainer leaves;
+		mViewCellsTree->CollectLeaves(vc, leaves);
+		ViewCellContainer::const_iterator it, it_end = leaves.end();
+
+		for (it = leaves.begin(); it != it_end; ++ it)
+		{
+			BspNodeGeometry geom;
+
+			BspNodeGeometry front;
+			BspNodeGeometry back;
+
+	
+			BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
+
+			mVspBspTree->ConstructGeometry(leaf, geom);
+			
+			geom.SplitGeometry(front,
+							   back,
+							   *cuttingPlane,
+							   mViewSpaceBox, 
+							   0.005);
+
+			exporter->ExportPolygons(back.mPolys);
+		}
+	}
+	else
+	{
+		BspNodeGeometry geom;
+		mVspBspTree->ConstructGeometry(vc, geom);
+			
+		exporter->ExportPolygons(geom.mPolys);
+	}
 }
 
