Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 578)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 579)
@@ -177,5 +177,5 @@
 	Construction {
 		samples 1500000
-		samplesPerPass 40000
+		samplesPerPass 100000
 	}
 
@@ -193,5 +193,5 @@
 	maxViewCells 3000
 	#percentage of total visible objects where pvs is considered invalid
-	maxPvsRatio 0.9
+	maxPvsRatio 1.0
 		
 	delayedConstruction false
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 578)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 579)
@@ -785,6 +785,6 @@
 	void SetLeaf2(BspLeaf *l);
 
-	BspLeaf *GetLeaf1();
-	BspLeaf *GetLeaf2();
+	BspLeaf *GetLeaf1() const;
+	BspLeaf *GetLeaf2() const;
 
 	/** Merge cost of this candidate pair.
@@ -792,23 +792,43 @@
 	float GetMergeCost() const;
 
+	/** Render cost of this candidate.
+	*/
+	float GetRenderCost() const;
+
+	/** returns increase in variance of this view cell.
+	*/
+	float GetVarianceIncr() const;
+
 	/** Returns cost of leaf 1.
 	*/
 	float GetLeaf1Cost() const;
+	
 	/** Returns cost of leaf 2.
 	*/
 	float GetLeaf2Cost() const;
 
-	/// maximal pvs size
-	static int sMaxPvsSize;
-	/// minimal pvs size
-	//static int sMinPvsSize;
+	/** Variance of leaf1
+	*/
+	float GetLeaf1Variance() const;
+
+	/** Variance of leaf2
+	*/
+	float GetLeaf2Variance() const;
 
 	/// overall cost used to normalize cost ratio
 	static float sOverallCost;
-	// if area or volume should be used for the merge heuristics
+	static float sExpectedCost;
+	static float sVariance;
+
+	static int sNumViewCells;
+
+	// weights between variance and render cost increase (must between zero and one)
+	static float sRenderCostWeight;
+
+	/// if area or volume should be used for the merge heuristics
 	static bool sUseArea;
 
-	static int sUpperPvsLimit;
-	static int sLowerPvsLimit;
+	/// pointer to view cells manager
+	static ViewCellsManager *sViewCellsManager;
 
 protected:
@@ -819,17 +839,16 @@
 	void EvalMergeCost();
 
-	/** penalty for a given pvs size.
-	*/
-	inline float EvalPvsPenalty(int pvs) const;
-
-	/** Cost of a view cell.
-	*/
-	float GetCost(ViewCell *vc) const;
+	/** render cost of a view cell.
+	*/
+	float GetRenderCost(ViewCell *vc) const;
 	
 	int mLeaf1Id;
 	int mLeaf2Id;
 
-	float mMergeCost;
-	
+	/// render cost increase by this merge
+	float mRenderCost;
+	/// increase / decrease of variance
+	float mVarianceIncr;
+
 	BspLeaf *mLeaf1;
 	BspLeaf *mLeaf2;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 577)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 579)
@@ -323,4 +323,10 @@
 	static ViewCellsManager *LoadViewCells(const string filename, ObjectContainer *objects);
 
+	/** Evaluates statistics values on view cells.
+	*/
+	void EvaluateRenderStatistics(float &totalRenderCost, 
+								  float &expectedRenderCost, 
+								  float &variance);
+
 protected:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 578)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 579)
@@ -29,5 +29,6 @@
 const float VspBspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0};
 
-int BspMergeCandidate::sMaxPvsSize = 0;
+ViewCellsManager *BspMergeCandidate::sViewCellsManager = NULL;
+//int BspMergeCandidate::sMaxPvsSize = 0;
 //int BspMergeCandidate::sMinPvsSize = 0;
 
@@ -37,15 +38,48 @@
 
 float BspMergeCandidate::sOverallCost = 0;
+float BspMergeCandidate::sExpectedCost = 0;
+float BspMergeCandidate::sVariance = 0;
+float BspMergeCandidate::sRenderCostWeight = 0.5f;
 bool BspMergeCandidate::sUseArea = false;
-
-
-int BspMergeCandidate::sUpperPvsLimit = 120;
-int BspMergeCandidate::sLowerPvsLimit = 5;
-
-
-
-/********************************************************************/
-/*                  class VspBspTree implementation                 */
-/********************************************************************/
+int BspMergeCandidate::sNumViewCells = 0;
+
+//int upperPvsLimit = 120;
+//int lowerPvsLimit = 5;
+
+
+
+// pvs penalty can be different from pvs size
+inline float EvalPvsPenalty(const int pvs, 
+							const int lower,
+							const int upper)
+{
+	// clamp to minmax values
+	if (pvs < lower)
+		return (float)lower;
+	if (pvs > upper)
+		return (float)upper;
+
+	return (float)pvs;
+}
+
+
+// penalty for pvs durint merge
+inline float EvalPvsPenaltyForMerge(const int pvs, 
+									const int lower,
+									const int upper)
+{
+	// clamp to minmax values
+	if (pvs < lower)
+		return (float)lower;
+	if (pvs > upper)
+		return (float)upper;
+
+	return (float)pvs;
+}
+
+
+/**********************************************************************/
+/*                  class VspBspTree implementation                   */
+/**********************************************************************/
 
 
@@ -192,4 +226,5 @@
 	DEL_PTR(mSplitCandidates);
 }
+
 
 int VspBspTree::AddMeshToPolygons(Mesh *mesh,
@@ -1070,4 +1105,5 @@
 #endif
 
+	
 	const float newCost = pvsBack * pBack + pvsFront * pFront;
 	const float oldCost = (float)pvsSize * pOverall + Limits::Small;
@@ -1388,6 +1424,15 @@
 		if (1)
 		{
-			const float oldCost = pOverall * (float)totalPvs + Limits::Small;
-			cost += mPvsFactor * (pvsFront * pFront + pvsBack * pBack) / oldCost;
+			const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
+			const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
+			
+			const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit);
+            
+			const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
+			const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
+			
+			const float oldCost = pOverall * (float)penaltyOld + Limits::Small;
+			cost += mPvsFactor * (penaltyFront * pFront + penaltyBack * pBack) / oldCost;
+
 		}
 		else
@@ -2673,7 +2718,7 @@
 		leaf->mPvs = new ObjectPvs(leaf->GetViewCell()->GetPvs());
 
-		BspMergeCandidate::sOverallCost += 
-			leaf->mProbability * leaf->mPvs->GetSize();
-
+		//const float rc = leaf->mProbability * (float)leaf->mPvs->GetSize();
+		//BspMergeCandidate::sOverallCost += rc;
+		
 		// the same leaves must not be part of two merge candidates
 		leaf->Mail();
@@ -2741,6 +2786,6 @@
 				new ObjectPvs(leaf->GetViewCell()->GetPvs());
 
-			BspMergeCandidate::sOverallCost += 
-				leaf->mProbability * leaf->mPvs->GetSize();
+			//BspMergeCandidate::sOverallCost += 
+			//	leaf->mProbability * leaf->mPvs->GetSize();
 			
 			++ numLeaves;
@@ -2767,6 +2812,6 @@
 					new ObjectPvs(leaf->GetViewCell()->GetPvs());
 				
-				BspMergeCandidate::sOverallCost += 
-					leaf->mProbability * leaf->mPvs->GetSize();
+				//BspMergeCandidate::sOverallCost += 
+				//	leaf->mProbability * leaf->mPvs->GetSize();
 
 				++ numLeaves;
@@ -2872,10 +2917,26 @@
 
 
+#if 1
 int VspBspTree::MergeViewCells(const VssRayContainer &rays, const ObjectContainer &objects)
 {
-	BspMergeCandidate::sMaxPvsSize = mViewCellsManager->GetMaxPvsSize();
-	//BspMergeCandidate::sMinPvsSize = mViewCellsManager->GetMinPvsSize();
+	BspMergeCandidate::sViewCellsManager = mViewCellsManager;
 	BspMergeCandidate::sUseArea = mUseAreaForPvs;
 
+
+	//-- compute statistics values of initial view cells
+	mViewCellsManager->EvaluateRenderStatistics(BspMergeCandidate::sOverallCost, 
+												BspMergeCandidate::sExpectedCost,
+												BspMergeCandidate::sVariance);
+
+
+	//BspMergeCandidate::sExpectedCost = 
+	//	BspMergeCandidate::sOverallCost / BspMergeCandidate::sNumViewCells;
+
+
+	ViewCellsManager::PvsStatistics pvsStats;
+	mViewCellsManager->GetPvsStatistics(pvsStats);
+
+	static float expectedValue = pvsStats.avgPvs;
+	
 	// the current view cells are kept in this container
 	ViewCellContainer viewCells;
@@ -2885,4 +2946,6 @@
 		CollectViewCells(mRoot, true, viewCells, true);
 	}
+
+
 	ViewCell::NewMail();
 
@@ -2890,5 +2953,4 @@
 	mergeStats.Start();
 	
-	//BspMergeCandidate::sOverallCost = mBox.SurfaceArea() * mBspStats.maxPvs;
 	long startTime = GetTime();
 
@@ -2912,15 +2974,197 @@
 	startTime = GetTime();
 
+	// frequency stats are updated
+	const int statsOut = 100;
+
+	// number of view cells withouth the invalid ones
+	BspMergeCandidate::sNumViewCells = mBspStats.Leaves() - mBspStats.invalidLeaves;
+
+	// passes are needed for statistics, because we don't want to record
+	// every merge
+	const int maxMergesPerPass = 100;
+	int pass = 0;
+
+	// maximal ratio of old expected render cost to expected render
+	// when the the render queue has to be reset.
+	const float ercMaxRatio = 0.7f;
+	
+	cout << "actual merge starts now ... " << endl;
+
+	
+	
+	//-- use priority queue to merge leaf pairs
+
+
+	while (!mMergeQueue.empty() && 
+		   (BspMergeCandidate::sNumViewCells > mMergeMinViewCells) &&
+		   (mMergeQueue.top().GetMergeCost() < mMergeMaxCostRatio * BspMergeCandidate::sOverallCost))
+	{
+
+		int mergedPerPass = 0;
+		const float oldExpectedCost = BspMergeCandidate::sExpectedCost;
+		
+//#ifdef _DEBUG
+		Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << " rel mergecost: "
+			  << mMergeQueue.top().GetMergeCost() / BspMergeCandidate::sOverallCost 
+			  << " max ratio: " << mMergeMaxCostRatio << endl
+			  << " expected value: " << oldExpectedCost << endl;
+//#endif
+
+		while (!mMergeQueue.empty() && 
+			   (BspMergeCandidate::sNumViewCells > mMergeMinViewCells) &&
+			   (ercMaxRatio > oldExpectedCost / BspMergeCandidate::sExpectedCost) &&
+			   (mMergeQueue.top().GetMergeCost() < mMergeMaxCostRatio * BspMergeCandidate::sOverallCost) &&
+			   (maxMergesPerPass < mergedPerPass));
+		{
+				Debug << "erc max ratio" << ercMaxRatio << endl;
+
+				BspMergeCandidate mc = mMergeQueue.top();
+				mMergeQueue.pop();
+
+				// both view cells equal!
+				if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell())
+					continue;
+
+				if (mc.Valid())
+				{
+					ViewCell::NewMail();
+					const float currentMergeCost = mc.GetMergeCost();
+
+					MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2());
+					
+					-- BspMergeCandidate::sNumViewCells;
+					++ mergeStats.merged;
+					++ mergedPerPass;
+
+					// increase absolute merge cost
+					BspMergeCandidate::sOverallCost += mc.GetRenderCost();
+					BspMergeCandidate::sVariance = mc.GetVarianceIncr();
+
+					BspMergeCandidate::sExpectedCost = 
+						BspMergeCandidate::sOverallCost / (float)BspMergeCandidate::sNumViewCells;
+
+					// stats
+					if (mc.GetLeaf1()->IsSibling(mc.GetLeaf2()))
+						++ mergeStats.siblings;
+
+					if (0)
+					{
+						const int dist = 
+							TreeDistance(mc.GetLeaf1(), mc.GetLeaf2());
+						if (dist > mergeStats.maxTreeDist)
+							mergeStats.maxTreeDist = dist;
+						mergeStats.accTreeDist += dist;
+					}
+
+					if ((mergeStats.merged % statsOut) == 0)
+					{
+						cout << "merged " << mergeStats.merged << " view cells" << endl;
+
+						mStats 
+							<< "#Pass\n" << pass << endl
+							<< "#Merged\n" << mergeStats.merged << endl 
+							<< "#Viewcells\n" << BspMergeCandidate::sNumViewCells << endl 
+							<< "#OverallCost\n" << BspMergeCandidate::sOverallCost << endl
+							<< "#CurrentCost\n" << currentMergeCost << endl
+							<< "#RelativeCost\n" << currentMergeCost / BspMergeCandidate::sOverallCost << endl
+							<< "#CurrentPvs\n" << mc.GetLeaf1()->GetViewCell()->GetPvs().GetSize() << endl
+							<< "#MergedSiblings\n" << mergeStats.siblings << endl
+							<< "#AvgTreeDist\n" << mergeStats.AvgTreeDist() << endl
+							<< "#ExpectedCost\n" << BspMergeCandidate::sExpectedCost << endl
+							<< "#RatioExpectedCost\n" << oldExpectedCost / BspMergeCandidate::sExpectedCost << endl
+							<< "#variance\n" << BspMergeCandidate::sVariance << endl;
+
+						if (mExportMergedViewCells)
+							ExportMergedViewCells(viewCells, objects, BspMergeCandidate::sNumViewCells);
+					}
+				}
+				else
+				{ 
+
+					// merge candidate not valid, because one of the leaves was already
+					// merged with another one => validate and reinsert into queue
+					mc.SetValid();
+					mMergeQueue.push(mc);
+				}
+		}
+	
+
+		++ pass;
+	}
+
+	mergeStats.overallCost = BspMergeCandidate::sOverallCost;
+
+	mergeStats.mergeTime = TimeDiff(startTime, GetTime());
+	mergeStats.Stop();
+
+	Debug << mergeStats << endl << endl;
+	
+	// delete the view cells which were already merged
+	CLEAR_CONTAINER(mOldViewCells);
+	
+
+	//TODO: should return sample contributions?
+	return mergeStats.merged;
+}
+
+#else
+
+int VspBspTree::MergeViewCells(const VssRayContainer &rays, const ObjectContainer &objects)
+{
+	BspMergeCandidate::sViewCellsManager = mViewCellsManager;
+	BspMergeCandidate::sUseArea = mUseAreaForPvs;
+
+	ViewCellsManager::PvsStatistics pvsStats;
+	mViewCellsManager->GetPvsStatistics(pvsStats);
+
+	static float expectedValue = pvsStats.avgPvs;
+	// the current view cells are kept in this container
+	ViewCellContainer viewCells;
+	if (mExportMergedViewCells)
+	{
+		ViewCell::NewMail();
+		CollectViewCells(mRoot, true, viewCells, true);
+	}
+	ViewCell::NewMail();
+
+	MergeStatistics mergeStats;
+	mergeStats.Start();
+	
+	//BspMergeCandidate::sOverallCost = mBox.SurfaceArea() * mBspStats.maxPvs;
+	long startTime = GetTime();
+
+	cout << "collecting merge candidates ... " << endl;
+	
+	if (mUseRaysForMerge)
+	{
+		mergeStats.nodes = CollectMergeCandidates(rays);
+	}
+	else
+	{
+		vector<BspLeaf *> leaves;
+		CollectLeaves(leaves);
+		mergeStats.nodes = CollectMergeCandidates(leaves);
+	}
+	
+	cout << "fininshed collecting candidates" << endl;
+
+	mergeStats.collectTime = TimeDiff(startTime, GetTime());
+	mergeStats.candidates = (int)mMergeQueue.size();
+	startTime = GetTime();
+
+	
 	// number of view cells withouth the invalid ones
 	int nViewCells = mBspStats.Leaves() - mBspStats.invalidLeaves;
-	
-	
-	// pass is needed for statistics. the last n passes are
-	// recorded
-	const int maxPasses = 1000;
-	const int nextPass = 50;
-
-	int pass = max(nViewCells - mMergeMinViewCells - maxPasses, 0);
-	
+	BspMergeCandidate::sExpectedCost = BspMergeCandidate::sOverallCost / (float)nViewCells;
+
+	// passes are needed for statistics, because we don't want to record
+	// every merge
+	const int mergesPerPass = 100;
+	
+	int nextPass = 0;
+    int pass = 0;
+	
+	
+	Debug << "stats: " << nextStats << " " << statsIncr << endl;
 	cout << "actual merge starts now ... " << endl;
 
@@ -2939,4 +3183,5 @@
 		mMergeQueue.pop();
 
+
 		// both view cells equal!
 		if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell())
@@ -2966,4 +3211,5 @@
 					++ mergeStats.siblings;
 
+				if (0)
 				const int dist = 
 					TreeDistance(mc.GetLeaf1(), mc.GetLeaf2());
@@ -2972,7 +3218,8 @@
 				mergeStats.accTreeDist += dist;
 
-				if ((mergeStats.merged == pass) || (nViewCells == mMergeMinViewCells))
+				if ((mergeStats.merged == nextPass) || (nViewCells == mMergeMinViewCells))
 				{
-					pass += nextPass;
+					nextPass += mergesPerPass;
+
 					mStats 
 						<< "#Pass\n" << pass ++ << endl
@@ -2987,5 +3234,5 @@
 
 						if (mExportMergedViewCells)
-							ExportMergedViewCells(viewCells, objects, nViewCells);   
+							ExportMergedViewCells(viewCells, objects, nViewCells);
 				}
 			}
@@ -3014,4 +3261,5 @@
 	return mergeStats.merged;
 }
+#endif
 
 
@@ -3467,11 +3715,12 @@
 
 
-/************************************************************************/
-/*                BspMergeCandidate implementation                      */
-/************************************************************************/
+/**************************************************************************/
+/*                  BspMergeCandidate implementation                      */
+/**************************************************************************/
 
 
 BspMergeCandidate::BspMergeCandidate(BspLeaf *l1, BspLeaf *l2):
-mMergeCost(0),
+mRenderCost(0),
+mVarianceIncr(0),
 mLeaf1(l1),
 mLeaf2(l2),
@@ -3483,5 +3732,5 @@
 
 
-float BspMergeCandidate::GetCost(ViewCell *vc) const
+float BspMergeCandidate::GetRenderCost(ViewCell *vc) const
 {
 	if (sUseArea)
@@ -3495,5 +3744,6 @@
 {
 	BspViewCell *vc = mLeaf1->GetViewCell();
-	return GetCost(vc);
+
+	return GetRenderCost(vc);
 }
 
@@ -3502,5 +3752,6 @@
 {
 	BspViewCell *vc = mLeaf2->GetViewCell();
-	return GetCost(vc);
+
+	return GetRenderCost(vc);
 }
 
@@ -3533,15 +3784,4 @@
 
 
-inline float BspMergeCandidate::EvalPvsPenalty(int pvs) const
-{
-	// clamp to minmax values
-	if (pvs > sUpperPvsLimit)
-		return (float)sUpperPvsLimit;
-	if (pvs < sLowerPvsLimit)
-		return (float)sLowerPvsLimit;
-
-	return (float)pvs;
-}
-
 
 void BspMergeCandidate::EvalMergeCost()
@@ -3551,8 +3791,9 @@
 	BspViewCell *vc2 = mLeaf2->GetViewCell();
 
-	//const int diff1 = vc1->GetPvs().Diff(vc2->GetPvs());
-	//const int newPvs = diff1 + vc1->GetPvs().GetSize();
 	const int newPvs = ComputeMergedPvsSize(vc1->GetPvs(), vc2->GetPvs());
-	const float f = EvalPvsPenalty(newPvs);
+	const float newPenalty = 
+		EvalPvsPenalty(newPvs, 
+					   sViewCellsManager->GetMinPvsSize(),
+					   sViewCellsManager->GetMaxPvsSize());
 
 	//-- compute ratio of old cost
@@ -3566,12 +3807,22 @@
 
 
-	if (newPvs > sMaxPvsSize) // strong penalty if pvs size too large
-	{
-		mMergeCost = 1e15;
+	if (newPvs > sViewCellsManager->GetMaxPvsSize()) // strong penalty if pvs size too large
+	{
+		mRenderCost = 1e15;
 	}
 	else
 	{
-		mMergeCost = newCost - oldCost;
-	}
+		mRenderCost = newCost - oldCost;
+	}
+
+	// merge cost also takes variance into account
+
+	const float oldVar1 = GetLeaf1Variance();
+	const float oldVar2 = GetLeaf2Variance();
+
+	const float newVar = (sExpectedCost - mRenderCost) * (sExpectedCost - mRenderCost);
+
+	mVarianceIncr = (newVar - oldVar1 - oldVar2) / sNumViewCells;
+	//mMergeCost = mRenderCost + fabs(newPenalty - BspMergeCandidate::sExpectedCost) ;
 }
 
@@ -3589,5 +3840,5 @@
 
 
-BspLeaf *BspMergeCandidate::GetLeaf1()
+BspLeaf *BspMergeCandidate::GetLeaf1() const
 {
 	return mLeaf1;
@@ -3595,5 +3846,5 @@
 
 
-BspLeaf *BspMergeCandidate::GetLeaf2()
+BspLeaf *BspMergeCandidate::GetLeaf2() const
 {
 	return mLeaf2;
@@ -3611,5 +3862,32 @@
 float BspMergeCandidate::GetMergeCost() const
 {
-	return mMergeCost;
+	return mRenderCost * sRenderCostWeight + mVarianceIncr * (1.0f - sRenderCostWeight);
+}
+
+
+float BspMergeCandidate::GetRenderCost() const
+{
+	return mRenderCost;
+}
+
+
+float BspMergeCandidate::GetVarianceIncr() const
+{
+	return mVarianceIncr;
+}
+
+float BspMergeCandidate::GetLeaf1Variance() const
+{
+	const float leafCost = GetLeaf1Cost();
+	
+	return (sExpectedCost - leafCost) * (sExpectedCost - leafCost);
+}
+
+
+float BspMergeCandidate::GetLeaf2Variance() const
+{
+	const float leafCost = GetLeaf2Cost();
+	
+	return (sExpectedCost - leafCost) * (sExpectedCost - leafCost);
 }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 574)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 579)
@@ -1266,8 +1266,5 @@
 		"false");
 
-	RegisterOption("ViewCells.delayedConstruction",
-		optBool,
-		"-view_cells_delayed_construction",
-		"false");
+	
 
 	RegisterOption("ViewCells.Visualization.exportGeometry",
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 569)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 579)
@@ -2778,5 +2778,5 @@
 	  Debug<<"Error: object Id out of range, Id="<<id<<" roots.size()="<<mRoots.size()<<
 		endl<<flush;
-	  id = mRoots.size()-1; // $$ last tree is used by all unsigned objects
+	  id = (int)mRoots.size()-1; // $$ last tree is used by all unsigned objects
 	}
 	return mRoots[id];
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 577)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 579)
@@ -456,4 +456,6 @@
 {
 
+	Debug << "type: rss" << endl;
+
   cout<<"Rss Preprocessor started\n"<<flush;
   //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 577)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 579)
@@ -494,5 +494,5 @@
 VssPreprocessor::ComputeVisibility()
 {
-
+	Debug << "type: vss" << endl;
 
   long startTime = GetTime();
@@ -523,11 +523,12 @@
   cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
   Debug << "use view space box=" << mUseViewSpaceBox << endl;
+
   if (mUseViewSpaceBox)
   {
-	mViewSpaceBox = box;
+	  mViewSpaceBox = box;
   }
   else
   {
-	mViewSpaceBox = NULL;
+	  mViewSpaceBox = NULL;
   }
   
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 577)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 579)
@@ -356,4 +356,44 @@
 
 
+void ViewCellsManager::EvaluateRenderStatistics(float &totalRenderCost, 
+												float &expectedRenderCost, 
+												float &variance)
+{
+	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+
+	//-- compute expected value
+
+	totalRenderCost = 0;
+
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+	{
+		ViewCell *vc = *it;
+
+		totalRenderCost += vc->GetPvs().GetSize() * vc->GetVolume();
+	}
+
+	expectedRenderCost = totalRenderCost / (float)mViewCells.size();
+
+
+	//-- compute variance
+	
+	variance = 0;
+
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+	{
+		ViewCell *vc = *it;
+
+		float renderCost = vc->GetPvs().GetSize() * vc->GetVolume();
+
+		const float var = (expectedRenderCost - renderCost) * (expectedRenderCost - renderCost);
+
+		variance += var;
+	}
+	
+	variance /= (float)mViewCells.size();
+}
+
+
+
 void ViewCellsManager::AddViewCell(ViewCell *viewCell)
 {
