Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 474)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 475)
@@ -13,7 +13,7 @@
 #;../data/vienna/vienna-plane.x3d
 #	filename ../data/vienna/viewcells-25-sel.x3d
-	filename ../data/atlanta/atlanta2.x3d
+#	filename ../data/atlanta/atlanta2.x3d
 #	filename ../data/soda/soda.dat
-#	filename ../data/soda/soda5.dat
+	filename ../data/soda/soda5.dat
 }
 
@@ -153,5 +153,5 @@
 	
 	height 5.0
-	maxViewCells 3000
+	maxViewCells 100
 	
 	PostProcessing {
@@ -240,5 +240,5 @@
 	
 	# maximal tested rays for split cost heuristics
-	maxTests 10000
+	maxTests 1000
 	
 	# factors for evaluating split plane costs
@@ -251,5 +251,5 @@
 	Termination {
 		# parameters used for autopartition
-		minRays 		10
+		minRays 		1
 		minPolygons 		-1
 		maxDepth		30
@@ -258,5 +258,5 @@
 		maxRayContribution	0.005
 		maxCostRatio		0.9
-		missTolerance		3
+		missTolerance		2
 		#maxAccRayLength	100
 		
@@ -282,5 +282,5 @@
 		exportSplits true
 		exportRays true
-		exportGeometry true
+		exportGeometry false
 	}
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 474)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 475)
@@ -116,8 +116,8 @@
 		float GetCost() const
 		{
-#if 0
+#if 1
 			return mPvs * mArea;
 #endif
-#if 1
+#if 0
 			return (float)(mPvs * (int)mRays->size());
 #endif
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 473)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 475)
@@ -851,17 +851,29 @@
 };
 
-struct BspIntersection {
-  // the point of intersection
-  float mT;
+struct BspIntersection 
+{
+	// the point of intersection
+	float mT;
   
-  BspLeaf *mLeaf;
+	BspLeaf *mLeaf;
   
-  BspIntersection(const float t, BspLeaf *l):
+	BspIntersection(const float t, BspLeaf *l):
 	mT(t), mLeaf(l) {}
   
-  BspIntersection() {}
+	BspIntersection() {}
   
-  bool operator<(const BspIntersection &b) const {
-	return mT	<b.mT; }
+	bool operator<(const BspIntersection &b) const 
+	{
+		return mT < b.mT; 
+	}
+};
+
+struct BspRay 
+{
+	VssRay *vssRay;
+
+	std::vector<BspIntersection> intersections;
+
+	BspRay(VssRay *ray): vssRay(ray) {}
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 471)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 475)
@@ -5,4 +5,5 @@
 #include "VssRay.h"
 #include "Containers.h"
+#include "ViewCell.h"
 
 class ViewCell;
@@ -21,4 +22,7 @@
 class AxisAlignedBox3;
 class BspLeaf;
+class ViewCellsStatistics;
+
+struct BspRay;
 
 /**
@@ -62,25 +66,24 @@
 		@param contributingSamples returns the number of contributingSamples
 	*/
-  void  ComputeSampleContributions(const VssRayContainer &rays);
-
-
-  /** Computes sample contribution of a simgle ray to the view cells PVS.
-	  @param ray finds intersections with view cells and holds the contribution
-	  @param castRay true if ray should be cast to gain the information, false if ray
-	  is already holding the information and need not be recast.
+	void  ComputeSampleContributions(const VssRayContainer &rays);
+
+
+	/** Computes sample contribution of a simgle ray to the view cells PVS.
+		@param ray finds intersections with view cells and holds the contribution
+		@param castRay true if ray should be cast to gain the information, false if ray
+		is already holding the information and need not be recast.
 	  
-	  @returns number of sample contributions
-  */
-  virtual void ComputeSampleContributions(VssRay &ray
-										  );
+		@returns number of sample contributions
+	*/
+	virtual void ComputeSampleContributions(VssRay &ray);
   
 	/** Prints out statistics of the view cells.
 	*/
-	virtual void PrintStatistics(ostream &s) const = 0;
+	virtual void PrintStatistics(ostream &s) const;
 
 	/** Post processes view cells givemŽa number of rays.
 	*/
-  virtual int PostProcess(const ObjectContainer &objects, 
-						  const VssRayContainer &rays) = 0;
+	virtual int PostProcess(const ObjectContainer &objects, 
+							const VssRayContainer &rays) = 0;
 
 	/** Show visualization of the view cells.
@@ -224,4 +227,6 @@
 	float mTotalAreaValid;
 	float mTotalArea;
+
+	ViewCellsStatistics mViewCellsStats;
 };
 
@@ -255,5 +260,5 @@
 	bool ViewCellsConstructed() const;
 
-	void PrintStatistics(ostream &s) const;
+	//void PrintStatistics(ostream &s) const;
 
 	int CastLineSegment(const Vector3 &origin,
@@ -278,8 +283,8 @@
 	/// the BSP tree.
 	BspTree *mBspTree;
-
+	
+	vector<BspRay *> mBspRays;
 
 private:
-
 
 	/** Exports visualization of the BSP splits.
@@ -325,5 +330,5 @@
 	/** Prints out statistics of this approach.
 	*/
-	virtual void PrintStatistics(ostream &s) const;
+	//void PrintStatistics(ostream &s) const;
 
 	float GetProbability(ViewCell *viewCell);
@@ -368,5 +373,5 @@
 	bool ViewCellsConstructed() const;
 
-	virtual void PrintStatistics(ostream &s) const;
+	//virtual void PrintStatistics(ostream &s) const;
 
 	ViewCell *GenerateViewCell(Mesh *mesh) const;
@@ -397,4 +402,5 @@
 
 	VspBspViewCellsManager(VspBspTree *tree, int constructionSamples);
+	~VspBspViewCellsManager();
 
 	int Construct(const ObjectContainer &objects, 
@@ -415,5 +421,5 @@
 	bool ViewCellsConstructed() const;
 
-	void PrintStatistics(ostream &s) const;
+	//void PrintStatistics(ostream &s) const;
 	
 	int CastLineSegment(const Vector3 &origin,
@@ -423,7 +429,11 @@
 	float GetProbability(ViewCell *viewCell);
 	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
-
+	float GetArea(ViewCell *viewCell) const;
 
 protected:
+
+	/** Constructs bsp rays for post processing and visualization.
+	*/
+	void ConstructBspRays(const VssRayContainer &rays, const int numSamples);
 
 	/** Merges view cells front and back leaf view cell.
@@ -438,4 +448,5 @@
 	VspBspTree *mVspBspTree;
 
+	vector<BspRay *> mBspRays;
 
 private:
@@ -444,9 +455,9 @@
 	/** Exports visualization of the BSP splits.
 	*/
-	void ExportSplits(const ObjectContainer &objects, const VssRayContainer &sampleRays);
+	void ExportSplits(const ObjectContainer &objects);
 
 	/** Exports visualization of the BSP PVS.
 	*/
-	void ExportBspPvs(const ObjectContainer &objects, const VssRayContainer &sampleRays);
+	void ExportBspPvs(const ObjectContainer &objects);
 
 };
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 474)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 475)
@@ -227,5 +227,5 @@
 	long startTime = GetTime();
 
-	Debug << "**** Extracting polygons from rays ****\n";
+	cout << "Extracting polygons from rays ...";
 
 	Intersectable::NewMail();
@@ -276,7 +276,9 @@
 	mStat.polys = (int)polys.size();
 
-	Debug << "**** Finished polygon extraction ****" << endl;
-	Debug << (int)polys.size() << " polys extracted from " << (int)sampleRays.size() << " rays" << endl;
-	Debug << "extraction time: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
+	cout << "finished" << endl;
+
+	Debug << "\nPolygon extraction: " << (int)polys.size() << " polys extracted from " 
+		  << (int)sampleRays.size() << " rays in "
+		  << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl << endl;
 
 	Construct(polys, rays);
@@ -321,5 +323,5 @@
 		if (r == mRoot)
 			Debug << "VSP BSP tree construction time spent at root: " 
-				  << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
+				  << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl << endl;
 	}
 
@@ -336,5 +338,5 @@
 		 (data.mPvs <= mTermMinPvs)   ||
 		 (data.mArea <= mTermMinArea) ||
-		 (numLeaves >= mMaxViewCells) ||
+		 ((mStat.nodes / 2 + 1) >= mMaxViewCells) ||
 		// (data.GetAvgRayContribution() >= mTermMaxRayContribution) ||
 		 (data.mDepth >= mTermMaxDepth));
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 475)
@@ -5,4 +5,5 @@
 #include "Matrix4x4.h"
 #include "Vector3.h"
+//#include "ViewCellBsp.h"
 
 // forward declarations
@@ -66,5 +67,5 @@
   
   vector<Intersection> intersections;
-  //  vector<BspIntersection> bspIntersections;
+ // vector<BspIntersection> bspIntersections;
   vector<KdLeaf *> kdLeaves;
   vector<Intersectable *> testedObjects;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 471)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 475)
@@ -615,5 +615,5 @@
 	const Vector3 dir = termination - origin;
 
-	stack<RayTraversalData > tStack;
+	stack<RayTraversalData> tStack;
 
 	Intersectable::NewMail();
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 474)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 475)
@@ -742,5 +742,5 @@
 
 		if (r == mRoot)
-			Debug << "BSP tree construction time spent at root: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
+			Debug << "BSP tree construction time spent at root: " << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl;
 	}
 
@@ -1153,5 +1153,5 @@
 		else
 		{
-			const int candidateIdx = (int)RandomValue(0, (int)data.mRays->size() - 1);
+			const int candidateIdx = (int)RandomValue(0, (Real)((int)data.mRays->size() - 1));
 			BoundedRay *bRay = (*data.mRays)[candidateIdx];
 
@@ -1500,5 +1500,5 @@
 	for (int i = 0; i < limit; ++ i)
 	{
-		const int testIdx = useRand ? (int)RandomValue(0, limit - 1) : i;
+		const int testIdx = useRand ? (int)RandomValue(0, (Real)(limit - 1)) : i;
 	
 		BoundedRay *bRay = rays[testIdx];
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 473)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 475)
@@ -9,5 +9,5 @@
 void SimulationStatistics::Print(ostream &app) const
 {
-	app << "===== Simulation statistics ===============\n";
+	app << "===== Render Simulation statistics ===============\n";
 
 	app << setprecision(4);
@@ -21,7 +21,8 @@
 	app << "#AVG_RENDER_TIME ( average render time )\n" << avgRenderTime << "\n";
 
-	app << "#AVG_RENDER_TIME_NO_OVERHEAD ( average render time without overhead )\n" << avgRtWithoutOverhead << "\n";
+	app << "#AVG_RENDER_TIME_NO_OVERHEAD ( average render time without overhead )\n" 
+		<< avgRtWithoutOverhead << "\n";
 	
-	app << "===== END OF Simulation statistics ==========\n";
+	app << "===== END OF Render Simulation statistics ==========\n";
 }
 
@@ -69,4 +70,5 @@
 		it_end = mViewCellsManager->GetViewCells().end();
 
+	
 	for (it = mViewCellsManager->GetViewCells().begin(); it != it_end; ++ it)
 	{
@@ -75,12 +77,14 @@
 		// probability of view cell
 		const float pInVc = mViewCellsManager->GetProbability(vc);
-				
+		Debug << "prop: " << pInVc << endl;
 		// compute render time of PVS times probability that view point is in view cell
 		const float vcCost = pInVc * mViewCellsManager->GetRendercost(vc, mObjRenderCost);
-	
+		Debug << "cost: " << vcCost << " rcost: " << mViewCellsManager->GetRendercost(vc, mObjRenderCost) << endl;
+
 		// crossing the border of a view cell is depending on the move speed
 		// and the probability that a view cell border is crossed
 		loadPvsOverhead += GetCrossVcProbability() * mVcOverhead;
 
+		Debug << "crossvc: " << GetCrossVcProbability() * mVcOverhead << endl;
 		//-- update statistics
 		renderTime += vcCost;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 473)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 475)
@@ -25,5 +25,4 @@
 {
 	DEL_PTR(mViewCellsManager);
-
 	DEL_PTR(mBspTree);
 	DEL_PTR(mKdTree);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 475)
@@ -142,7 +142,7 @@
 void
 PassingRaySet::AddRay2(const Ray &ray,
-											 const int objects,
-											 const int viewcells
-											 )
+					   const int objects,
+					   const int viewcells
+					   )
 {
   int i = GetEntryIndex(ray.GetDir());
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 473)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 475)
@@ -176,5 +176,5 @@
   if (!use2dSampling) {
 	Vector3 normal;
-	int i = RandomValue(0, mObjects.size()-1);
+	int i = (int)RandomValue(0, mObjects.size()-1);
 	Intersectable *object = mObjects[i];
 	object->GetRandomSurfacePoint(point, normal);
@@ -513,4 +513,5 @@
 	
 	//-- several visualizations and statistics
+	Debug << "view cells after post processing: " << endl;
 	mViewCellsManager->PrintStatistics(Debug);
 	
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 474)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 475)
@@ -558,4 +558,5 @@
 
 	//-- several visualizations and statistics
+	Debug << "view cells after post processing: " << endl;
 	mViewCellsManager->PrintStatistics(Debug);
 
@@ -576,5 +577,5 @@
 
   delete vssTree;
-
+  
   return true;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 473)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 475)
@@ -391,4 +391,5 @@
 	
 	//-- several visualizations and statistics
+	Debug << "view cells after post processing: " << endl;
 	mViewCellsManager->PrintStatistics(Debug);
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 474)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 475)
@@ -255,5 +255,5 @@
 		
 		if (added)
-			ray.mPvsContribution++;
+			++ ray.mPvsContribution;
 		
 		ray.mRelativePvsContribution += contribution;
@@ -312,4 +312,9 @@
 }
 
+void ViewCellsManager::PrintStatistics(ostream &s) const
+{
+	s << mViewCellsStats << endl;
+}
+
 /**********************************************************************/
 /*                   BspViewCellsManager implementation               */
@@ -366,5 +371,4 @@
 		// no view cells loaded 
 		mBspTree->Construct(objects, constructionRays);
-
 		// collect final view cells
 		mBspTree->CollectViewCells(mViewCells);
@@ -375,4 +379,6 @@
 	}
 
+	mBspTree->EvaluateViewCellsStats(mViewCellsStats);
+
 	// destroy rays created only for construction
 	CLEAR_CONTAINER(constructionRays);
@@ -434,7 +440,5 @@
 	int pvsSize = 0;
 
-	ViewCellsStatistics vcStats;
-	mBspTree->EvaluateViewCellsStats(vcStats);
-	Debug << "original view cell partition:\n" << vcStats << endl;
+	Debug << "original view cell partition:\n" << mViewCellsStats << endl;
 		
 	if (1) // export view cells before merge
@@ -446,5 +450,5 @@
 		{
 			exporter->SetWireframe();
-			exporter->ExportBspLeaves(*mBspTree, vcStats.maxPvs);
+			exporter->ExportBspLeaves(*mBspTree, mViewCellsStats.maxPvs);
 			//exporter->ExportBspViewCellPartition(*mBspTree, 0);
 				
@@ -463,6 +467,4 @@
 		cout << "finished" << endl;
 
-		//-- render simulation
-		cout << "\nevaluating bsp view cells render time before merge ... ";
 		//-- render simulation after merge
 		cout << "\nevaluating bsp view cells render time after merge ... ";
@@ -490,5 +492,5 @@
 
 	RayContainer::const_iterator rit, rit_end = rays.end();
-	vector<Ray::BspIntersection>::const_iterator iit;
+	vector<BspIntersection>::const_iterator iit;
 
 	int limit = min((int)rays.size(), mPostProcessSamples);
@@ -527,4 +529,11 @@
 	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" << endl;
+
+	//-- recount pvs
+	mViewCellsStats.Reset();
+	mBspTree->EvaluateViewCellsStats(mViewCellsStats);
 
 	return merged;
@@ -640,5 +649,6 @@
   //-- some rays for output
   const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
-  Debug << "visualization using " << raysOut << " samples" << endl;
+
+  Debug << "Output view cells:" << endl;
 
   if (1)
@@ -650,5 +660,4 @@
 		  bspLeaves.push_back(mBspTree->GetRandomLeaf());	
 
-	  
 	  for (int i = 0; i < bspLeaves.size(); ++ i)
 	  {
@@ -876,10 +885,4 @@
 }
 
-void BspViewCellsManager::PrintStatistics(ostream &s) const
-{
-	ViewCellsStatistics vcStats;
-	mBspTree->EvaluateViewCellsStats(vcStats);
-	s << vcStats << endl;
-}
 
 
@@ -920,4 +923,5 @@
 	// create the view cells
 	mKdTree->CreateAndCollectViewCells(mViewCells);
+	//mKdTree->EvaluateViewCellsStats(mViewCellsStats);
 
 	return 0;
@@ -1129,10 +1133,4 @@
 }
 
-
-void KdViewCellsManager::PrintStatistics(ostream &s) const
-{
-}
-
-
 int KdViewCellsManager::CastLineSegment(const Vector3 &origin,
 										const Vector3 &termination,
@@ -1189,7 +1187,6 @@
 
 	mVspKdTree->Construct(constructionRays, sceneBbox);
-	
-	// collect view cells
 	mVspKdTree->CollectViewCells(mViewCells);
+	//mVspBspTree->EvaluateViewCellsStats(mViewCellsStats);
 
 	Debug << mVspKdTree->GetStatistics() << endl;
@@ -1312,8 +1309,4 @@
 	}
 
-	// evaluate maximal pvs
-	ViewCellsStatistics vcStats;
-	mVspKdTree->EvaluateViewCellsStats(vcStats);
-	
 	//-- export final view cells
 	Exporter *exporter = Exporter::GetExporter("vspkdtree_merged.x3d"); 
@@ -1353,12 +1346,4 @@
 }
 
-void VspKdViewCellsManager::PrintStatistics(ostream &s) const
-{
-	ViewCellsStatistics vcStats;
-
-	mVspKdTree->EvaluateViewCellsStats(vcStats);
-	Debug << vcStats << endl;
-}
-
 
 int VspKdViewCellsManager::CastLineSegment(const Vector3 &origin,
@@ -1380,4 +1365,9 @@
 }
 
+VspBspViewCellsManager::~VspBspViewCellsManager()
+{
+	CLEAR_CONTAINER(mBspRays);
+}
+
 float VspBspViewCellsManager::GetProbability(ViewCell *viewCell)
 {
@@ -1386,4 +1376,16 @@
 }
 
+float VspBspViewCellsManager::GetArea(ViewCell *viewCell) const
+{
+	PolygonContainer geom;
+
+	// compute view cell area
+	mVspBspTree->ConstructGeometry(dynamic_cast<BspViewCell *>(viewCell), geom);
+	
+	const float area = Polygon3::GetArea(geom);
+	CLEAR_CONTAINER(geom);
+
+	return area;
+}
 
 float VspBspViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
@@ -1412,6 +1414,4 @@
 		return 0;
 
-	Debug << "Constructing vsp bsp view cells" << endl;
-
 	int sampleContributions = 0;
 	
@@ -1423,4 +1423,5 @@
 	mVspBspTree->Construct(constructionRays);
 	mVspBspTree->CollectViewCells(mViewCells);
+	mVspBspTree->EvaluateViewCellsStats(mViewCellsStats);
 	
 	Debug << mVspBspTree->GetStatistics() << endl;
@@ -1433,8 +1434,37 @@
 
 
+void VspBspViewCellsManager::ConstructBspRays(const VssRayContainer &rays,
+											  const int numSamples)
+{
+	VssRayContainer::const_iterator it, it_end = rays.end();
+
+	for (it = rays.begin(); it != rays.end() && mBspRays.size() < numSamples; ++ it)
+	{
+		VssRay *vssRay = *it;
+		BspRay *ray = new BspRay(vssRay);
+
+		ViewCellContainer viewCells;
+
+		CastLineSegment(vssRay->mTermination, vssRay->mOrigin, viewCells);
+
+		ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
+		
+		for (vit = viewCells.begin(); vit != vit_end; ++ vit)
+		{
+			BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit);
+			ray->intersections.push_back(BspIntersection(0, vc->mLeaves[0]));
+		}
+
+		mBspRays.push_back(ray);
+	}
+}
+
+
 int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects, 
 										const VssRayContainer &rays)
 {
-	Debug << "Post processing view cells" << endl;
+	if (mBspRays.empty())
+		ConstructBspRays(rays, mConstructionSamples);
+	
 	if (!ViewCellsConstructed())
 	{
@@ -1447,8 +1477,5 @@
 	int pvsSize = 0;
 
-	
-	ViewCellsStatistics vcStats;
-	mVspBspTree->EvaluateViewCellsStats(vcStats);
-	Debug << "original view cell partition:\n" << vcStats << endl;
+	Debug << "original view cell partition:\n" << mViewCellsStats << endl;
 		
 	if (1) // export view cells
@@ -1459,6 +1486,7 @@
 		if (exporter)
 		{
-			exporter->SetWireframe();
-			exporter->ExportBspViewCellPartition(*mVspBspTree, vcStats.maxPvs);
+			//exporter->SetWireframe();
+			exporter->SetFilled();
+			exporter->ExportBspViewCellPartition(*mVspBspTree, mViewCellsStats.maxPvs);
 				
 			if (0)
@@ -1487,26 +1515,22 @@
 	//-- merge or subdivide view cells
 	int merged = 0;
-#if 0
-
-	RayContainer::const_iterator rit, rit_end = rays.end();
-	vector<Ray::BspIntersection>::const_iterator iit;
-
-	int limit = min((int)rays.size(), mPostProcessSamples);
-
-	for (int i = 0; i < limit; ++ i)
+
+	vector<BspIntersection>::const_iterator iit;
+
+	for (int i = 0; i < (int)mBspRays.size(); ++ i)
 	{  
-	  VssRay *ray = rays[i];
+		BspRay *ray = mBspRays[i];
 	  
-	  // traverse leaves stored in the rays and compare and merge consecutive
-	  // leaves (i.e., the neighbors in the tree)
-	  if (ray->bspIntersections.size() < 2)
-		continue;
+		// 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->bspIntersections.begin();
+		iit = ray->intersections.begin();
 
 		BspLeaf *previousLeaf = (*iit).mLeaf;
 		++ iit;
 		
-		for (; iit != ray->bspIntersections.end(); ++ iit)
+		for (; iit != ray->intersections.end(); ++ iit)
 		{
 			BspLeaf *leaf = (*iit).mLeaf;
@@ -1522,5 +1546,5 @@
 		}
 	}
-#endif
+
 	//-- stats and visualizations
 	cout << "finished" << endl;
@@ -1528,4 +1552,12 @@
 		 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
 
+	Debug << "Postprocessing: Merged " << merged << " view cells in "
+		  << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
+	
+	// reset view cells and stats
+	mViewCells.clear();
+	mVspBspTree->CollectViewCells(mViewCells);
+	mViewCellsStats.Reset();
+	mVspBspTree->EvaluateViewCellsStats(mViewCellsStats);
 
 	return merged;
@@ -1544,8 +1576,7 @@
 		return;
 
-	//-- recount pvs
-	ViewCellsStatistics vcStats;
-	mVspBspTree->EvaluateViewCellsStats(vcStats);
-
+	if (mBspRays.empty())
+		ConstructBspRays(sampleRays, mConstructionSamples);
+	
 	if (1) // export view cells
 	{
@@ -1555,5 +1586,5 @@
 		if (exporter)
 		{
-			exporter->ExportBspViewCellPartition(*mVspBspTree, vcStats.maxPvs);
+			exporter->ExportBspViewCellPartition(*mVspBspTree, mViewCellsStats.maxPvs);
 			delete exporter;
 		}
@@ -1569,14 +1600,13 @@
 	{
 		cout << "exporting splits ... ";
-	 	ExportSplits(objects, sampleRays);
+	 	ExportSplits(objects);
 		cout << "finished" << endl;
 	}
 
-	ExportBspPvs(objects, sampleRays);
-}
-
-
-void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects,
-										  const VssRayContainer &sampleRays)
+	ExportBspPvs(objects);
+}
+
+
+void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects)
 {
 	Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
@@ -1601,14 +1631,12 @@
 		if (0)
 		{
-		  VssRayContainer outRays;
-		
-			int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
+			VssRayContainer outRays;
+		
+			int raysSize = min((int)mBspRays.size(), mVisualizationSamples);
 
 			for (int i = 0; i < raysSize; ++ i)
-			{
 				// only rays piercing geometry
-			  outRays.push_back(sampleRays[i]);
-			}
-			
+				outRays.push_back(mBspRays[i]->vssRay);
+						
 			// export rays 
 			exporter->ExportRays(outRays, RgbColor(1, 1, 0));
@@ -1622,6 +1650,5 @@
 }
 
-void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
-										  const VssRayContainer &sampleRays)
+void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects)
 {
 	bool exportRays = false;
@@ -1636,7 +1663,8 @@
 
 	//-- some rays for output
-	const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
+	const int raysOut = min((int)mBspRays.size(), mVisualizationSamples);
 	cout << "visualization using " << mVisualizationSamples << " samples" << endl;
-	
+	Debug << "\nOutput view cells: " << endl;
+
 	if (1)
 	{
@@ -1652,22 +1680,20 @@
 			cout << "creating output for view cell " << i << " ... ";
 
-			//$$JB
-#if 0
 			// check whether we can add the current ray to the output rays
 			for (int k = 0; k < raysOut; ++ k) 
 			{
-				VssRay *ray = sampleRays[k];
-
-				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+				BspRay *ray = mBspRays[k];
+
+				for	(int j = 0; j < (int)ray->intersections.size(); ++ j)
 				{
-					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
+					BspLeaf *leaf = ray->intersections[j].mLeaf;
 
 					if (vspBspLeaves[i]->GetViewCell() == leaf->GetViewCell()) 
 					{
-						vcRays.push_back(ray);
+						vcRays.push_back(ray->vssRay);
 					}
 				}
 			}
-#endif
+
 			Intersectable::NewMail();
 
@@ -1683,6 +1709,6 @@
 			ObjectPvsMap::iterator it = vc->GetPvs().mEntries.begin();
 
-			//exporter->SetWireframe();
-			exporter->SetFilled();
+			exporter->SetWireframe();
+			//exporter->SetFilled();
 
 			Material m;//= RandomMaterial();
@@ -1728,5 +1754,5 @@
 				}			
 			}
-				
+			Debug << "here1 exportgeom: " << exportGeometry << endl;
 			// output rest of the objects
 			if (exportGeometry)
@@ -1765,22 +1791,21 @@
 
 			cout << "creating output for view cell " << i << " ... ";
-			//$$ JB
-#if 0
+
 			// check whether we can add the current ray to the output rays
 			for (int k = 0; k < raysOut; ++ k) 
 			{
-			  Ray *ray = sampleRays[k];
-
-				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+				BspRay *ray = mBspRays[k];
+
+				for	(int j = 0; j < (int)ray->intersections.size(); ++ j)
 				{
-					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
+					BspLeaf *leaf = ray->intersections[j].mLeaf;
 
 					if (vc == leaf->GetViewCell()) 
 					{
-						vcRays.push_back(ray);
+						vcRays.push_back(ray->vssRay);
 					}
 				}
 			}
-#endif
+
 			//bspLeaves[j]->Mail();
 			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
@@ -1807,5 +1832,5 @@
 			
 			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
-					<< ", piercing rays=" << (int)vcRays.size() << endl;
+				  << ", piercing rays=" << (int)vcRays.size() << endl;
 
 			
@@ -1839,13 +1864,8 @@
 		}
 	}
-}
-
-
-void VspBspViewCellsManager::PrintStatistics(ostream &s) const
-{
-	ViewCellsStatistics vcStats;
-	mVspBspTree->EvaluateViewCellsStats(vcStats);
-	s << vcStats << endl;
-}
+
+	Debug << endl;
+}
+
 
 bool VspBspViewCellsManager::MergeVspBspLeafViewCells(BspLeaf *front, 
@@ -1859,7 +1879,6 @@
 		return false;
 
-	// change view cells of all leaves associated with the
-	// previous view cells
-
+	// change view cells of all leaves
+	// associated with the previous view cells
 	BspViewCell *fVc = front->GetViewCell();
 	BspViewCell *bVc = back->GetViewCell();
