Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 600)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 605)
@@ -710,4 +710,5 @@
 	ofstream mSubdivsionStats;
 
+	bool mUseRandomAxis;
 private:
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 604)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 605)
@@ -160,26 +160,4 @@
 
 
-void ViewCell::UpdateViewCellsStats(ViewCellsStatistics &vcStat)
-{
-	++ vcStat.viewCells;
-		
-	const int pvsSize = mPvs.GetSize();
-
-	vcStat.pvs += pvsSize;
-
-	if (pvsSize == 0)
-		++ vcStat.emptyPvs;
-
-	if (pvsSize > vcStat.maxPvs)
-		vcStat.maxPvs = pvsSize;
-
-	if (pvsSize < vcStat.minPvs)
-		vcStat.minPvs = pvsSize;
-
-	if (!mValid)
-		++ vcStat.invalid;
-}
-
-
 float ViewCell::GetArea() const
 {
@@ -477,5 +455,5 @@
 	mViewCellsManager->GetPvsStatistics(pvsStats);
 
-	static float expectedValue = pvsStats.avgPvs;
+	//static float expectedValue = pvsStats.avgPvs;
 	
 	// the current view cells are kept in this container
@@ -521,16 +499,15 @@
 
 	mStats << "#Pass\n" << pass << endl
-		<< "#Merged\n" << mergeStats.merged << endl 
-		<< "#Viewcells\n" << realNumActiveViewCells << endl 
-		<< "#RenderCostIncrease\n" << 0 << endl
-		<< "#TotalRenderCost\n" << totalRenderCost << endl
-		<< "#CurrentPvs\n" << 0 << endl
-		<< "#ExpectedCost\n" << realExpectedCost << endl
-		<< "#AvgRenderCost\n" << realAvgRenderCost << endl
-		<< "#Deviation\n" << mDeviation << endl
-		<< "#TotalPvs\n" << totalPvs << endl
-		<< "#PvsSizeDecrease\n" << 0 << endl
-		<< "#Volume\n" << 0 << endl
-		<< "#Dummy\n" << 0 << endl;
+		   << "#Merged\n" << mergeStats.merged << endl 
+		   << "#Viewcells\n" << realNumActiveViewCells << endl 
+		   << "#RenderCostIncrease\n" << 0 << endl
+		   << "#TotalRenderCost\n" << totalRenderCost << endl
+		   << "#CurrentPvs\n" << 0 << endl
+		   << "#ExpectedCost\n" << realExpectedCost << endl
+		   << "#AvgRenderCost\n" << realAvgRenderCost << endl
+		   << "#Deviation\n" << mDeviation << endl
+		   << "#TotalPvs\n" << totalPvs << endl
+		   << "#PvsSizeDecrease\n" << 0 << endl
+		   << "#Volume\n" << endl;
 
 	//-- use priority queue to merge leaf pairs
@@ -584,10 +561,4 @@
 		}
 
-#ifdef _DEBUG
-		Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << 
-			  << " rel mergecost: " << mMergeQueue.top().GetRenderCost() / mExpectedCost << 
-			  << " max ratio: " << mMergeMaxCostRatio << endl
-			  << " expected value: " << realExpectedCost << endl;
-#endif
 
 	
@@ -637,7 +608,7 @@
 				++ mergeStats.siblings;
 #endif
-		//	if (((mergeStats.merged % statsOut) == 0) || 
-		//		(realNumActiveViewCells == mMergeMinViewCells))
-		//	{
+			if (((mergeStats.merged % statsOut) == 0) || 
+			(realNumActiveViewCells == mMergeMinViewCells))
+			{
 				cout << "merged " << mergeStats.merged << " view cells" << endl;
 
@@ -646,15 +617,14 @@
 					<< "#Merged\n" << mergeStats.merged << endl 
 					<< "#Viewcells\n" << realNumActiveViewCells << endl 
-					<< "#RenderCostIncrease\n" << renderCostIncr << endl
+                    << "#RenderCostIncrease\n" << renderCostIncr << endl
 					<< "#TotalRenderCost\n" << totalRenderCost << endl
 					<< "#CurrentPvs\n" << mergedVc->GetPvs().GetSize() << endl
-					<< "#ExpectedCost\n" << realExpectedCost / (float) realNumActiveViewCells << endl
+                    << "#ExpectedCost\n" << realExpectedCost << endl
 					<< "#AvgRenderCost\n" << realAvgRenderCost << endl
 					<< "#Deviation\n" << mDeviation << endl
 					<< "#TotalPvs\n" << totalPvs << endl
 					<< "#PvsSizeDecrease\n" << -pvsDiff << endl
-					<<  "#Volume\n" << mergedVc->GetVolume() << endl
-					<< "#Dummy\n" << (float)mergedVc->GetPvs().GetSize() * mergedVc->GetVolume() / mViewCellsManager->GetViewSpaceBox().GetVolume() << endl;
-		//	}
+					<< "#Volume\n" << mergedVc->GetVolume() << endl;
+			}
 		}
 		else
@@ -1489,6 +1459,4 @@
 	tstack.push(vc);
 
-	Debug << "current size: " << pvsSize << endl;
-
 	while (!tstack.empty())
 	{
@@ -1563,4 +1531,28 @@
 	return vc;
 }
+
+
+
+void  ViewCellsTree::UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat)
+{
+	++ vcStat.viewCells;
+		
+	const int pvsSize = GetPvsSize(vc);
+
+	vcStat.pvs += pvsSize;
+
+	if (pvsSize == 0)
+		++ vcStat.emptyPvs;
+
+	if (pvsSize > vcStat.maxPvs)
+		vcStat.maxPvs = pvsSize;
+
+	if (pvsSize < vcStat.minPvs)
+		vcStat.minPvs = pvsSize;
+
+	if (!vc->GetValid())
+		++ vcStat.invalid;
+}
+
 
 
@@ -1643,4 +1635,5 @@
 
 
+
 /************************************************************************/
 /*                    MergeStatistics implementation                    */
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 600)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 605)
@@ -213,5 +213,5 @@
 	/** Returns render cost of a single view cell given the render cost of an object.
 	*/
-	virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const = 0;
+	virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const ;
 
 	/** Returns container of loaded / generated view cells.
@@ -521,5 +521,5 @@
 	
 	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
+	
 
 	/** Get a viewcell containing the specified point */
@@ -603,5 +603,5 @@
 
 	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
+
 
 	void CreateMesh(ViewCell *vc);
@@ -669,5 +669,5 @@
 
 	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
+	
 
 	void CreateMesh(ViewCell *vc);
@@ -728,5 +728,5 @@
 
 	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
+	
 	
 	ViewCell *GetViewCell(const Vector3 &point) const;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 604)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 605)
@@ -67,5 +67,6 @@
 mOutOfBoundsCell(NULL),
 mStoreRays(false),
-mRenderCostWeight(0.5)
+mRenderCostWeight(0.5),
+mUseRandomAxis(false)
 {
 	bool randomize = false;
@@ -429,8 +430,8 @@
 	tQueue.push(tData);
 
-	mTotalCost = tData.GetCost() / mBox.GetVolume();
+	mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume();
 
 	mSubdivisionStats 
-			<< "#Leaves\n1\n" <<  endl
+			<< "#ViewCells\n1\n" <<  endl
 			<< "#RenderCostDecrease\n0\n" << endl 
 			<< "#TotalRenderCost\n" << mTotalCost << endl;
@@ -528,10 +529,14 @@
 			if (1)
 			{
+				float cFront = tFrontData.mPvs * tFrontData.mProbability;
+				float cBack = tBackData.mPvs * tBackData.mProbability;
+				float cData = tData.mPvs * tData.mProbability;;
+
 				float costDecr = 
-					(tFrontData.GetCost() + tBackData.GetCost() - tData.GetCost()) / mBox.GetVolume();
+					(cFront + cBack - cData) / mBox.GetVolume();
 				mTotalCost += costDecr;
 
 				mSubdivisionStats 
-						<< "#Leaves\n" << mBspStats.Leaves() << endl
+						<< "#ViewCells\n" << mBspStats.Leaves() << endl
 						<< "#RenderCostDecrease\n" << -costDecr << endl 
 						<< "#TotalRenderCost\n" << mTotalCost << endl;
@@ -957,5 +962,5 @@
 	}
 
-	const int sAxis = box.Size().DrivingAxis();
+	const int sAxis = mUseRandomAxis ? Random(3) : box.Size().DrivingAxis();
 
 	for (axis = 0; axis < 3; ++ axis)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 600)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 605)
@@ -1628,5 +1628,5 @@
 		{
 			pFront = frontCell.GetVolume();
-			pBack = backCell.GetVolume();
+			pBack = pOverall - pFront;
 		}
 		
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 604)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 605)
@@ -129,7 +129,4 @@
 	void SetArea(float area);
 
-	/** Updates the view cell statstics for this particular view cell.
-	*/
-	virtual void UpdateViewCellsStats(ViewCellsStatistics &vcStat);
 
 	/** if this view cell is the root of a view cell hierarchy
@@ -238,12 +235,5 @@
 		ViewCell(mesh), mLeaf(NULL) {}
 
-	void UpdateViewCellsStats(ViewCellsStatistics &vcStat)
-	{
-		ViewCell::UpdateViewCellsStats(vcStat);
-// TODO
-		//if ((int)mLeaves.size() > vcStat.maxLeaves)
-		//	vcStat.maxLeaves = (int)mLeaves.size();
-		//vcStat.leaves += (int)mLeaves.size();
-	}
+	
 
 	bool IsLeaf() const
@@ -289,4 +279,7 @@
 	int RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
 
+	/** Updates view cell stats for this particular view cell
+	*/
+	void UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat);
 	
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 580)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 605)
@@ -100,4 +100,6 @@
 		const float pInVc = mViewCellsManager->GetProbability(vc);
 
+		//Debug << "vc prob: " << pInVc << endl;
+
 		// compute render time of PVS times probability
 		// that view point is in view cell
@@ -105,9 +107,10 @@
 			mViewCellsManager->GetRendercost(vc, mObjRenderCost);
 	
+		renderTime += vcCost;
+
 		// 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;
-		renderTime += vcCost;
-
+		
 		//-- update statistics
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 600)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 605)
@@ -791,4 +791,6 @@
   //-- render simulation after merge
   cout << "\nevaluating bsp view cells render time after sampling ... ";
+  Debug << "\nStatistics after sampling: " << endl;
+
   mRenderSimulator->RenderScene();
   SimulationStatistics ss;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 601)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 605)
@@ -16,4 +16,5 @@
 #include "RssPreprocessor.h"
 
+#define SAMPLE_AFTER_SUBDIVISION 1
 
 
@@ -189,5 +190,6 @@
 
 		// TODO: leak?
-		if(0)ComputeSampleContributions(constructionSamples, true, false);
+		if (SAMPLE_AFTER_SUBDIVISION)
+			ComputeSampleContributions(constructionSamples, true, false);
 		cout << "finished" << endl;
 
@@ -211,5 +213,7 @@
 	// store viewCells for postprocessing
 	const bool storeViewCells = true;
-	if(0)ComputeSampleContributions(postProcessSamples, true, storeViewCells);
+
+	if (SAMPLE_AFTER_SUBDIVISION)
+		ComputeSampleContributions(postProcessSamples, true, storeViewCells);
 	// merge the view cells
 	PostProcess(preprocessor->mObjects, postProcessSamples);
@@ -224,5 +228,6 @@
 					visualizationSamples);
 
-	if(0)ComputeSampleContributions(visualizationSamples, true, storeViewCells);
+	if (SAMPLE_AFTER_SUBDIVISION)
+		ComputeSampleContributions(visualizationSamples, true, storeViewCells);
 
 	//Debug << "visualizationsamples: " << mVisualizationSamples << " " << visualizationSamples.size() << endl;
@@ -377,5 +382,5 @@
 	for (it = mViewCells.begin(); it != it_end; ++ it)
 	{
-		(*it)->UpdateViewCellsStats(mViewCellsStats);
+		mViewCellsTree->UpdateViewCellsStats(*it, mViewCellsStats);
 	}
 }
@@ -858,4 +863,10 @@
 
 
+float ViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
+{
+	return mViewCellsTree->GetPvsSize(viewCell) * objRendercost;
+}
+
+
 float ViewCellsManager::GetAccVcArea()
 {
@@ -1082,10 +1093,4 @@
 
 
-float BspViewCellsManager::GetRendercost(ViewCell *viewCell, 
-										 float objRendercost) const
-{
-	return viewCell->GetPvs().GetSize() * objRendercost;
-}
-
 
 int BspViewCellsManager::CastLineSegment(const Vector3 &origin,
@@ -1157,17 +1162,15 @@
     // reset view cells and stats
 	ResetViewCells();
+	Debug << "\nView cells after merge:\n" << mViewCellsStats << endl;
 
 	int savedColorCode  = mColorCode;
-
+	
 	//BspLeaf::NewMail();
 	if (1) // export merged view cells
 	{
 		mColorCode = 0;
-		cout << "reseting view cells ... ";
-		ResetViewCells();
-		cout << "finished" << endl;
-
+		
 		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
-		Debug << "\nView cells after merge:\n" << mViewCellsStats << endl;
+		
 
 		cout << "exporting view cells after merge ... ";
@@ -1387,10 +1390,4 @@
 		ExportViewCellGeometry(exporter, vc);
 		
-
-		Debug << i << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc)
-			  << ", piercing rays=" << (int)vcRays.size() << endl;
-			 // << ", leaves=" << (int)vc->mLeaves.size() << endl;
-
-
 		// export rays piercing this view cell
 		exporter->ExportRays(vcRays, RgbColor(0, 1, 0));
@@ -1677,23 +1674,15 @@
 }
 
+
 float KdViewCellsManager::GetProbability(ViewCell *viewCell)
 {
 	// compute view cell area / volume as subsititute for probability
-#if 0
-	return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
-#endif
-#if 1
-	return GetArea(viewCell) / GetAccVcArea();
-#endif
-#if 0
-	return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
-#endif
-}
-
-
-float KdViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
-{
-	return viewCell->GetPvs().GetSize() * objRendercost;
-}
+	if (0)
+		return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
+	else
+		return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
+}
+
+
 
 
@@ -1810,7 +1799,4 @@
 			AxisAlignedBox3 box = mKdTree->GetBox(leaf);
 			exporter->ExportBox(box);
-
-			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()
-				<< ", piercing rays=" << (int)vcRays.size() << endl;
 
 			// export rays piercing this view cell
@@ -1995,17 +1981,12 @@
 float VspKdViewCellsManager::GetProbability(ViewCell *viewCell)
 {
-	// volume or area substitutes for view point probability
-#if 0
-	return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
-#else
-	return GetArea(viewCell) / GetAccVcArea();
-#endif
-}
-
-
-float VspKdViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
-{
-	return viewCell->GetPvs().GetSize() * objRendercost;
-}
+	// compute view cell area / volume as subsititute for probability
+	if (0)
+		return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
+	else
+		return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
+}
+
+
 
 
@@ -2144,6 +2125,4 @@
 		VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>(mViewCells[idx]);
 
-		cout << "Output view cell " << i << " with pvs size " << vc->GetPvs().GetSize() << endl;
-		Debug << "Output view cell " << i << " with pvs size " << vc->GetPvs().GetSize() << endl;
 		//-- export geometry
 		Material m;
@@ -2393,10 +2372,4 @@
 
 
-float VspBspViewCellsManager::GetRendercost(ViewCell *viewCell,
-											float objRendercost) const
-{
-	return viewCell->GetPvs().GetSize() * objRendercost;
-}
-
 
 bool VspBspViewCellsManager::ViewCellsConstructed() const
@@ -2515,7 +2488,8 @@
 
 	cout << "Computing remaining ray contributions ... ";
+
 	// recast rest of rays
-	if (0)
-	ComputeSampleContributions(savedRays, true, false);
+	if (SAMPLE_AFTER_SUBDIVISION)
+		ComputeSampleContributions(savedRays, true, false);
 	cout << "finished" << endl;
 
@@ -2697,5 +2671,6 @@
 
 	VssRayContainer postProcessRays;
-	if(0)GetRaySets(rays, mPostProcessSamples, postProcessRays);
+
+	GetRaySets(rays, mPostProcessSamples, postProcessRays);
 
 	Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
@@ -2718,30 +2693,14 @@
 		RefineViewCells(postProcessRays, objects);
 
-	
-	if (1)
-	{		
-		float totalCost, erc, var, dev, avg;
-		int totalpvs;
-
-		mViewCellsStats.Reset();
-
-        EvaluateRenderStatistics(totalCost, erc, dev, var, totalpvs, avg);
-		
-		Debug << "statistics after merge "  
-			  << " erc: " << erc 
-			  << " dev: " << dev
-			  << " totalpvs: " << totalpvs 
-			  << " avg: " << avg << endl;
-	}
-
 	//-- render simulation after merge
-  cout << "\nevaluating bsp view cells render time before compress ... ";
-  dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
-  SimulationStatistics ss;
-  dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
+	cout << "\nevaluating bsp view cells render time before compress ... ";
+	dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
+	SimulationStatistics ss;
+	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
   
-  cout << " finished" << endl;
-  cout << ss << endl;
-  Debug << ss << endl;
+
+	cout << " finished" << endl;
+	cout << ss << endl;
+	Debug << ss << endl;
 
 
@@ -2758,69 +2717,4 @@
 		pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
 		Debug << "number of entries after compress: " << pvsEntries << endl;
-	}
-
-
-	//-- export refined (shuffled) view cells
-	if (0)
-	{
-		cout << "exporting shuffled view cells ... ";
-
-		Exporter *exporter = Exporter::GetExporter("shuffled_view_cells.x3d");
-		if (exporter)
-		{
-			if (1)
-			{
-				exporter->SetWireframe();
-				exporter->ExportBox(mViewSpaceBox);
-				exporter->SetFilled();
-			}
-
-			if (mExportGeometry)
-			{
-				exporter->ExportGeometry(objects);
-			}
-
-			ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
-
-			Material vm, lm;
-
-			for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
-			{
-				ViewCell *vc = *vit;
-
-				vm = RandomMaterial();
-
-				lm = vm;
-
-				vm.mDiffuseColor.r -= 0.45f;
-				vm.mDiffuseColor.g -= 0.45f;
-				vm.mDiffuseColor.b -= 0.45f;
-
-
-				ViewCellContainer leaves;
-				mViewCellsTree->CollectLeaves(vc, leaves);
-
-				ViewCellContainer::const_iterator lit, lit_end = leaves.end();
-
-				for (lit = leaves.begin(); lit != lit_end; ++ lit)
-				{
-					BspLeaf *leaf = dynamic_cast<BspViewCell *>(*lit)->mLeaf;
-
-					if (leaf->Mailed())
-						exporter->SetForcedMaterial(lm);
-					else
-						exporter->SetForcedMaterial(vm);
-
-					BspNodeGeometry geom;
-					mVspBspTree->ConstructGeometry(leaf, geom);
-					exporter->ExportPolygons(geom.mPolys);
-				}
-			}
-
-			delete exporter;
-		}
-
-
-		cout << "finished" << endl;
 	}
 
