Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1912)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1913)
@@ -531,4 +531,6 @@
 		float mCorrectedFrontRenderCost;
 		float mCorrectedBackRenderCost;
+		float mCorrectedFrontVolume;
+		float mCorrectedBackVolume;
 	};
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1912)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1913)
@@ -374,4 +374,9 @@
 }
 									
+float BvHierarchy::GetViewSpaceVolume() const
+{
+	return mViewCellsManager->GetViewSpaceBox().GetVolume();
+}
+
 
 BvhInterior *BvHierarchy::SubdivideNode(const BvhSubdivisionCandidate &sc,
@@ -444,6 +449,6 @@
    
 	//-- compute pvs correction to cope with undersampling
-	frontData.mPvs = CountViewCells(front->mObjects);
-	backData.mPvs = CountViewCells(back->mObjects);
+	frontData.mPvs = (float)CountViewCells(front->mObjects);
+	backData.mPvs = (float)CountViewCells(back->mObjects);
 
 	frontData.mCorrectedPvs = sc.mCorrectedFrontPvs;
@@ -452,9 +457,9 @@
 	// compute probability of this node being visible, 
 	// i.e., volume of the view cells that can see this node
-	frontData.mRenderCost = EvalRenderCost(sc.mFrontObjects);
-	backData.mRenderCost = EvalRenderCost(sc.mBackObjects);
-
-	frontData.mCorrectedRenderCost = sc.mCorrectedFrontCost;
-	backData.mCorrectedRenderCost = sc.mCorrectedBackCost;
+	frontData.mVolume = EvalViewCellsVolume(sc.mFrontObjects) / GetViewSpaceVolume();
+	backData.mVolume = EvalViewCellsVolume(sc.mBackObjects) / GetViewSpaceVolume();
+
+	frontData.mCorrectedVolume = sc.mCorrectedFrontVolume;
+	backData.mCorrectedVolume = sc.mCorrectedBackVolume;
 
     // how often was max cost ratio missed in this branch?
@@ -628,6 +633,6 @@
 
 	// avg contribution of a ray to a pvs
-	const float pvs = CountViewCells(leaf->mObjects);
-	const float avgRayContri = AvgRayContribution(pvs, tData.mNumRays);
+	const float pvs = (float)CountViewCells(leaf->mObjects);
+	const float avgRayContri = AvgRayContribution((int)pvs, tData.mNumRays);
 
 	cout << "bvh avg ray contri: " << avgRayContri << endl;
@@ -635,30 +640,35 @@
 	splitCandidate.SetAvgRayContribution(avgRayContri);
 
-	const float oldRenderCost = EvalRenderCost(leaf->mObjects);
-	const float oldRatio = oldRenderCost / tData.mRenderCost;
-	const float parentCost = tData.mCorrectedRenderCost * oldRatio;
-
-	cout << "old ratio: " << oldRatio << endl;
+	const float oldVolume = EvalViewCellsVolume(leaf->mObjects) / GetViewSpaceVolume();
+	const float oldRatio = tData.mVolume > 0 ? oldVolume / tData.mVolume : 1;
+	const float parentVol = tData.mCorrectedVolume * oldRatio;
+
 	// this leaf is a pvs entry in all the view cells
 	// that see one of the objects.
-	const int frontCost = EvalRenderCost(splitCandidate.mFrontObjects);
-	const int backCost = EvalRenderCost(splitCandidate.mBackObjects);
-
-	cout <<"here8 ============ "<< parentCost << endl;
-	splitCandidate.mCorrectedFrontCost = 
-		mHierarchyManager->EvalCorrectedPvs(frontCost, parentCost, avgRayContri);
-
-	cout << "bvh front cost: " << frontCost << " corrected: " << splitCandidate.mCorrectedFrontCost << endl;
+	const float frontVol = EvalViewCellsVolume(splitCandidate.mFrontObjects) / GetViewSpaceVolume();
+	const float backVol = EvalViewCellsVolume(splitCandidate.mBackObjects) / GetViewSpaceVolume();
+
+	cout <<"here8 "<< parentVol << " " << " old ratio: " << oldRatio << " frointvol: " << frontVol << endl;
+	splitCandidate.mCorrectedFrontVolume = 
+		mHierarchyManager->EvalCorrectedPvs(frontVol, parentVol, avgRayContri);
+
+	cout << "bvh front vol: " << frontVol << " corrected: " << splitCandidate.mCorrectedFrontVolume << endl;
 	cout <<"here104 "<< endl;
 
-	splitCandidate.mCorrectedBackCost = 
-		mHierarchyManager->EvalCorrectedPvs(backCost, parentCost, avgRayContri);
-	
-	cout << "back cost: " << backCost << " corrected: " << splitCandidate.mCorrectedBackCost << endl;
+	splitCandidate.mCorrectedBackVolume = 
+		mHierarchyManager->EvalCorrectedPvs(backVol, parentVol, avgRayContri);
+	
+	cout << "back vol: " << backVol << " corrected: " << splitCandidate.mCorrectedBackVolume << endl;
+
+	const float relfrontCost = splitCandidate.mCorrectedFrontVolume * 
+		EvalAbsCost(splitCandidate.mFrontObjects);
+	const float relBackCost =  splitCandidate.mCorrectedFrontVolume * 
+		EvalAbsCost(splitCandidate.mBackObjects);
+	const float relParentCost = parentVol * 
+		EvalAbsCost(leaf->mObjects);
 
 	// compute global decrease in render cost
-	const float newRenderCost = 
-		splitCandidate.mCorrectedFrontCost + splitCandidate.mCorrectedBackCost;
-	const float renderCostDecr = parentCost - newRenderCost;
+	const float newRenderCost = relfrontCost + relBackCost;
+	const float renderCostDecr = relParentCost - newRenderCost;
 	
 	splitCandidate.SetRenderCostDecrease(renderCostDecr);
@@ -676,5 +686,5 @@
 	float priority = EvalPriority(splitCandidate, 
 								  renderCostDecr,
-								  oldRenderCost);
+								  relParentCost);
 
   	// compute global decrease in render cost
@@ -686,6 +696,6 @@
 									const float avgRayContri) const
 {
-	const int oldPvsSize = CountViewCells(splitCandidate.mParentData.mNode->mObjects);
-	const float oldPvsRatio = splitCandidate.mParentData.mPvs > 0 ? oldPvsSize / splitCandidate.mParentData.mPvs : 1;
+	const float oldPvsSize = (float)CountViewCells(splitCandidate.mParentData.mNode->mObjects);
+	const float oldPvsRatio = (splitCandidate.mParentData.mPvs > 0) ? oldPvsSize / splitCandidate.mParentData.mPvs : 1;
 
 	const float parentPvs = splitCandidate.mParentData.mCorrectedPvs * oldPvsRatio;
@@ -698,9 +708,9 @@
 
 	splitCandidate.mCorrectedFrontPvs = 
-		mHierarchyManager->EvalCorrectedPvs(frontViewCells, parentPvs, avgRayContri);
+		mHierarchyManager->EvalCorrectedPvs((float)frontViewCells, parentPvs, avgRayContri);
 	splitCandidate.mCorrectedBackPvs =
-		mHierarchyManager->EvalCorrectedPvs(backViewCells, parentPvs, avgRayContri);
-
-	return splitCandidate.mCorrectedFrontPvs + splitCandidate.mCorrectedBackPvs - parentPvs;
+		mHierarchyManager->EvalCorrectedPvs((float)backViewCells, parentPvs, avgRayContri);
+
+	return (int)(splitCandidate.mCorrectedFrontPvs + splitCandidate.mCorrectedBackPvs - parentPvs);
 }
 
@@ -1755,4 +1765,16 @@
 
 
+float BvHierarchy::EvalProb(const ObjectContainer &objects) const
+{	
+	///////////////
+	//-- render cost heuristics
+
+	const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();
+
+	// probability that view point lies in a view cell which sees this node
+	return EvalViewCellsVolume(objects) / viewSpaceVol;
+}
+
+
 AxisAlignedBox3 BvHierarchy::EvalBoundingBox(const ObjectContainer &objects,
 											 const AxisAlignedBox3 *parentBox) const
@@ -2176,17 +2198,6 @@
 	BvhLeaf *bvhLeaf = dynamic_cast<BvhLeaf *>(mRoot);
 
-	// multiply termination criterium for comparison,
-	// so it can be set between zero and one and
-	// no division is necessary during traversal
-
-#if PROBABILIY_IS_BV_VOLUME
-	mTermMinProbability *= mBoundingBox.GetVolume();
-	// probability that bounding volume is seen
-	const float prop = GetBoundingBox().GetVolume();
-#else
-	mTermMinProbability *= mVspTree->GetBoundingBox().GetVolume();
 	// probability that volume is "seen" from the view cells
-	const float prop = EvalViewCellsVolume(objects);
-#endif
+	const float prop = EvalViewCellsVolume(objects) / GetViewSpaceVolume();
 
 	// only rays intersecting objects in node are interesting
@@ -2210,8 +2221,7 @@
 	mPvsEntries = CountViewCells(objects);
 
-	oData.mCorrectedPvs = oData.mPvs = mPvsEntries;
-	oData.mCorrectedRenderCost = oData.mRenderCost = mTotalCost;
-	cout << "here52 " << oData.mCorrectedPvs << " " << oData.mRenderCost << " " << oData.mCorrectedRenderCost << endl;
-
+	oData.mCorrectedPvs = oData.mPvs = (float)mPvsEntries;
+	oData.mCorrectedVolume = oData.mVolume = prop;
+	cout << "here52 " << oData.mCorrectedPvs << " " << oData.mVolume << " " << oData.mCorrectedVolume  << endl;
 
 	BvhSubdivisionCandidate *oSubdivisionCandidate = 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1912)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1913)
@@ -387,6 +387,6 @@
 		mCorrectedPvs(0),
 		mPvs(0),
-		mCorrectedRenderCost(0),
-		mRenderCost(0)
+		mCorrectedVolume(0),
+		mVolume(0)
 		{
 			for (int i = 0; i < 4; ++ i)
@@ -405,6 +405,6 @@
 		mCorrectedPvs(0),
 		mPvs(0),
-		mCorrectedRenderCost(0),
-		mRenderCost(v)
+		mCorrectedVolume(0),
+		mVolume(v)
 		{
 			for (int i = 0; i < 4; ++ i)
@@ -425,8 +425,8 @@
 		/// current depth
 		int mDepth;
-		/// the render cost of the node
-		float mRenderCost;
-		/// the corrected render cost;
-		float mCorrectedRenderCost;
+		/// the volume of the node
+		float mVolume;
+		/// the corrected volume
+		float mCorrectedVolume;
 		/// how often this branch has missed the max-cost ratio
 		int mMaxCostMisses;
@@ -512,6 +512,6 @@
 		float mCorrectedBackPvs;
 
-		float mCorrectedFrontCost;
-		float mCorrectedBackCost;
+		float mCorrectedFrontVolume;
+		float mCorrectedBackVolume;
 	};
 
@@ -597,4 +597,5 @@
 	void SetViewCellsManager(ViewCellsManager *vcm);
 
+	float GetViewSpaceVolume() const;
 	/** Writes tree to output stream
 	*/
@@ -640,4 +641,6 @@
 
 	static float EvalAbsCost(const ObjectContainer &objects);
+
+	float EvalProb(const ObjectContainer &objects) const;
 
 	void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1912)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1913)
@@ -346,5 +346,4 @@
 VspLeaf::VspLeaf(): 
 mViewCell(NULL), mSubdivisionCandidate(NULL)
-//, mPvs(NULL)
 {
 }
@@ -380,5 +379,4 @@
 VspLeaf::VspLeaf(VspInterior *parent): 
 VspNode(parent), mViewCell(NULL)
-//, mPvs(NULL)
 {}
 
@@ -386,5 +384,4 @@
 VspLeaf::VspLeaf(VspInterior *parent, ViewCellLeaf *viewCell): 
 VspNode(parent), mViewCell(viewCell)
-//, mPvs(NULL)
 {
 }
@@ -735,5 +732,5 @@
 			
 		mTotalCost -= sc->GetRenderCostDecrease();
-		mTotalPvsSize += (int)(tFrontData.mPvs + tBackData.mPvs - tData.mPvs);
+		//mTotalPvsSize += (int)(tFrontData.mPvs + tBackData.mPvs - tData.mPvs);
 		mPvsEntries += sc->GetPvsEntriesIncr();
 
@@ -899,15 +896,16 @@
 	}
 
-	const float oldPvsRatio = oldPvsSize / splitCandidate.mParentData.mPvs;
+	const float oldPvsRatio = (splitCandidate.mParentData.mPvs > 0) ? 
+		oldPvsSize / splitCandidate.mParentData.mPvs : 1;
 	const float correctedOldPvs = splitCandidate.mParentData.mCorrectedPvs * oldPvsRatio;
 
 	cout <<"here102 "<< endl;
 	cout << "old pvs ratio: " << oldPvsRatio << endl;
-	const float correctedFrontPvs = 
+	splitCandidate.mCorrectedFrontPvs = 
 		mHierarchyManager->EvalCorrectedPvs(fPvsSize, 
 											correctedOldPvs, 
 											splitCandidate.GetAvgRayContribution());
 	cout <<"here103 "<< endl;
-	const float correctedBackPvs = 
+	splitCandidate.mCorrectedBackPvs = 
 		mHierarchyManager->EvalCorrectedPvs(bPvsSize, 
 											correctedOldPvs, 
@@ -915,8 +913,8 @@
 
 	
-	cout << "vsp front entries " << fPvsSize << " corrected: " << correctedFrontPvs << endl;
-	cout << "vsp back entries " << bPvsSize << " corrected: " << correctedBackPvs << endl;
-
-	return (int)(correctedFrontPvs + correctedBackPvs - correctedOldPvs);
+	cout << "vsp front entries " << fPvsSize << " corrected: " << splitCandidate.mCorrectedFrontPvs << endl;
+	cout << "vsp back entries " << bPvsSize << " corrected: " << splitCandidate.mCorrectedBackPvs << endl;
+
+	return (int)(splitCandidate.mCorrectedFrontPvs + splitCandidate.mCorrectedBackPvs - correctedOldPvs);
 }
 
@@ -961,9 +959,9 @@
 
 	//-- compute render cost
-	frontData.mRenderCost = (float)EvalRenderCost(*frontData.mRays);
-	backData.mRenderCost = (float)EvalRenderCost(*backData.mRays);
-	
-	frontData.mCorrectedRenderCost = sc.mCorrectedFrontPvs;
-	backData.mCorrectedRenderCost = sc.mCorrectedBackPvs;
+	frontData.mRenderCost = (float)EvalPvsCost(*frontData.mRays);
+	backData.mRenderCost = (float)EvalPvsCost(*backData.mRays);
+	
+	frontData.mCorrectedRenderCost = sc.mCorrectedFrontRenderCost;
+	backData.mCorrectedRenderCost = sc.mCorrectedBackRenderCost;
 
 	////////
@@ -1605,18 +1603,20 @@
 	//-- evaluate render cost heuristics
 
-	const float oldRenderCostRatio = tData.mPvs ? (totalPvs / tData.mPvs) : 1;
-
-	const float penaltyOld = tData.mCorrectedPvs * oldRenderCostRatio;
-
-	cout <<"here107 " << penaltyOld << endl;
-	sc.mCorrectedFrontPvs = mHierarchyManager->EvalCorrectedPvs(pvsFront, penaltyOld, avgRayContri);
-	cout <<"here108 " << tData.mCorrectedPvs << " " << oldRenderCostRatio << endl;
-	sc.mCorrectedBackPvs = mHierarchyManager->EvalCorrectedPvs(pvsBack, penaltyOld, avgRayContri);
-
-	cout << "vsp front pvs " << pvsFront << " corrected: " << sc.mCorrectedFrontPvs << endl;
-	cout << "vsp back pvs " << pvsBack << " corrected: " << sc.mCorrectedBackPvs << endl;
+	const float oldRenderCostRatio = (tData.mRenderCost > 0)? 
+		(totalPvs / tData.mRenderCost) : 1;
+
+	const float penaltyOld = tData.mCorrectedRenderCost * oldRenderCostRatio;
+
+	cout <<"here107 old render cost ratio " << oldRenderCostRatio << " old cost: " << penaltyOld << endl;
+	sc.mCorrectedFrontRenderCost = mHierarchyManager->EvalCorrectedPvs(pvsFront, penaltyOld, avgRayContri);
+	cout <<"here108 " << tData.mCorrectedRenderCost << " " << oldRenderCostRatio << endl;
+	sc.mCorrectedBackRenderCost = mHierarchyManager->EvalCorrectedPvs(pvsBack, penaltyOld, avgRayContri);
+
+	cout << "vsp front pvs " << pvsFront << " corrected: " << sc.mCorrectedFrontRenderCost << endl;
+	cout << "vsp back render cost " << pvsBack << " corrected: " << sc.mCorrectedBackRenderCost << endl;
 
 	const float oldRenderCost = pOverall * penaltyOld;
-	const float newRenderCost = sc.mCorrectedFrontPvs * pFront + sc.mCorrectedBackPvs * pBack;
+	const float newRenderCost = sc.mCorrectedFrontRenderCost * pFront + 
+								sc.mCorrectedFrontRenderCost * pBack;
 
 	// we also return the old render cost
@@ -2934,5 +2934,4 @@
 	VspTraversalData vData(leaf, 0, &rays, pvsCost, prop, mBoundingBox);
 
-
 #if WORK_WITH_VIEWCELL_PVS
 	// add first view cell to all the objects view cell pvs
@@ -2947,6 +2946,6 @@
 #endif
 
-	mTotalCost = vData.mCorrectedPvs = pvsCost;
-	mPvsEntries = EvalPvsEntriesSize(rays);
+	mTotalCost = vData.mCorrectedRenderCost = vData.mRenderCost = pvsCost;
+	mPvsEntries = vData.mCorrectedPvs = vData.mPvs = EvalPvsEntriesSize(rays);
 
 	//////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1912)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1913)
@@ -743,18 +743,27 @@
 	// assume pvs sampled sufficiently => take child pvs
 	if (avgRayContri < mMinAvgRayContri)
+	{
+		cout << "here4 child" << endl;
 		return childPvs;
-
+	}
 	// assume pvs not sampled sufficiently => take total pvs
 	if (avgRayContri > mMaxAvgRayContri)
+	{
+		cout << "here9 parent" << endl;
 		return totalPvs;
+	}
 
 	const float alpha = (mMaxAvgRayContri - avgRayContri) / 
 						(mMaxAvgRayContri - mMinAvgRayContri);
 
-	cout << "here41 **************** " << alpha << " " << childPvs << " " << totalPvs << endl;
-
-	const float beta = (1.0f - alpha) * (totalPvs + childPvs) / totalPvs;
-
-	return childPvs + beta;
+	const float beta = (1.0f - alpha) * (totalPvs - childPvs);
+
+	cout << "alpha " << alpha << " beta: " << beta << " child: " << childPvs << " parent: " << totalPvs << endl;
+	
+	const float newPvs = childPvs + beta;
+
+	if ((newPvs < childPvs - Limits::Small) || (newPvs > totalPvs + Limits::Small))
+		cout << "Error!! " << newPvs << endl;
+	return newPvs;
 }
 
