Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1696)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1698)
@@ -240,9 +240,10 @@
 	bool randomize = false;
 	Environment::GetSingleton()->GetBoolValue("VspTree.Construction.randomize", randomize);
+	 
+	// initialise random generator for heuristics
 	if (randomize)
-		Randomize(); // initialise random generator for heuristics
-
-
-	////////////////////////////////////
+		Randomize();
+
+	//////////////////////////////
 	//-- termination criteria for autopartition
 
@@ -251,7 +252,7 @@
 	Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.minObjects", mTermMinObjects);
 	Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.minRays", mTermMinRays);
-	Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.minProbability", mTermMinProbability);
-	
-	Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.missTolerance", mTermMissTolerance);
+	Environment::GetSingleton()->GetFloatValue(
+		"BvHierarchy.Termination.minProbability", mTermMinProbability);
+    Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.missTolerance", mTermMissTolerance);
 
 
@@ -282,9 +283,11 @@
 		"BvHierarchy.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
 	
-	Environment::GetSingleton()->GetBoolValue("BvHierarchy.Construction.useGlobalSorting", mUseGlobalSorting);
+	Environment::GetSingleton()->GetBoolValue(
+		"BvHierarchy.Construction.useGlobalSorting", mUseGlobalSorting);
+
 	Environment::GetSingleton()->GetIntValue("BvHierarchy.minRaysForVisibility", mMinRaysForVisibility);
 	
-
 	mUseBboxAreaForSah = true;
+
 
 	/////////////
@@ -516,9 +519,11 @@
 	if (computeSplitPlane)
 	{
-		const bool sufficientSamples = splitCandidate.mParentData.mNumRays > mMinRaysForVisibility;
+		const bool sufficientSamples = 
+			splitCandidate.mParentData.mNumRays > mMinRaysForVisibility;
 
 		const bool useVisibiliyBasedHeuristics = 
 			!mUseSah &&
-			(mHierarchyManager->GetViewSpaceSubdivisionType() == HierarchyManager::KD_BASED_VIEWSPACE_SUBDIV) &&
+			(mHierarchyManager->GetViewSpaceSubdivisionType() == 
+			HierarchyManager::KD_BASED_VIEWSPACE_SUBDIV) &&
 			sufficientSamples;
 
@@ -531,5 +536,4 @@
 		// cost ratio violated?
 		const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
-
 		const int previousMisses = splitCandidate.mParentData.mMaxCostMisses;
 
@@ -674,5 +678,5 @@
 	// objects related stuff
 
-	// note: this number should always accumulate to the total number of objects
+	// note: the sum should alwaysbe total number of objects for bvh
 	mBvhStats.objectRefs += (int)leaf->mObjects.size();
 
@@ -813,8 +817,9 @@
 	// go through the lists, count the number of objects left and right
 	// and evaluate the following cost funcion:
-	// C = ct_div_ci  + (ol + or)/queries
+	// C = ct_div_ci  + (ol + or) / queries
 	PrepareLocalSubdivisionCandidates(tData, axis);
 
-	int objectsLeft = 0, objectsRight = (int)tData.mNode->mObjects.size();
+	const float totalRenderCost = EvalAbsCost(tData.mNode->mObjects);
+	float objectsLeft = 0, objectsRight = totalRenderCost;
   
 	const AxisAlignedBox3 nodeBbox = tData.mNode->GetBoundingBox();
@@ -872,6 +877,9 @@
 		Intersectable *obj = (*cit).mObject;
 
-		++ objectsLeft;
-		-- objectsRight;
+		const float renderCost = mViewCellsManager->EvalRenderCost(obj);
+		//const int renderCost = 1;
+
+		objectsLeft += renderCost;
+		objectsRight -= renderCost;
 
 		const AxisAlignedBox3 obox = obj->GetBox();
@@ -919,4 +927,5 @@
 			areaLeft = al;
 			areaRight = ar;
+
 			// objects belong to left side now
 			for (; currentPos != (cit + 1); ++ currentPos);
@@ -936,7 +945,6 @@
 		objectsFront.push_back((*cit).mObject);
 
-	float oldCost = (float)tData.mNode->mObjects.size();
 	float newCost = minSum / boxArea;
-	float ratio = newCost / oldCost;
+	float ratio = newCost / totalRenderCost;
   
 #ifdef _DEBUG
@@ -1001,7 +1009,7 @@
 										   ObjectContainer &objectsBack)
 {
-	////////////////////////////////////////////////////////////////
-	// go through the lists, count the number of objects left and right
-	// and evaluate the cost funcion
+	///////////////////////////////////////////////////////
+	// -- go through the lists, count the number of objects left 
+	//-- and right and evaluate the cost funcion
 
 	// prepare the heuristics by setting mailboxes and counters.
@@ -1011,6 +1019,9 @@
 	float volLeft = 0;
 	float volRight = totalVol;
-	int nObjectsLeft = 0;
-	const int nTotalObjects = (int)tData.mNode->mObjects.size();
+	
+	const float nTotalObjects = EvalAbsCost(tData.mNode->mObjects);
+	float nObjectsLeft = 0;
+	float nObjectsRight = nTotalObjects;
+
 	const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();
 
@@ -1038,5 +1049,6 @@
 	//-- traverse through events and find best split plane
 
-	SortableEntryContainer::const_iterator cit, cit_end = cit_end = mSubdivisionCandidates->end();
+	SortableEntryContainer::const_iterator cit, 
+		cit_end = cit_end = mSubdivisionCandidates->end();
 
 	for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit)
@@ -1049,7 +1061,9 @@
 		EvalHeuristicsContribution(object, volLeft, volRight);
 
-		++ nObjectsLeft;
-		const int nObjectsRight = nTotalObjects - nObjectsLeft;
-
+		const float rc = mViewCellsManager->EvalRenderCost(object);
+
+		nObjectsLeft += rc;
+		nObjectsRight -= rc;
+		
 		// the heuristics
 	    const float sum = volLeft * (float)nObjectsLeft + 
@@ -1405,33 +1419,31 @@
 
 
+float BvHierarchy::EvalAbsCost(const ObjectContainer &objects) const
+{
+#if USE_BETTER_RENDERCOST_EST
+	ObjectContainer::const_iterator oit, oit_end = objects.end();
+
+	for (oit = objects.begin(); oit != oit_end; ++ oit)
+	{
+		objRenderCost += mViewCellsManager->GetRendercost(*oit);
+	}
+#else
+	return (float)objects.size();
+#endif
+}
+
+
 float BvHierarchy::EvalRenderCost(const ObjectContainer &objects) const
 {	
-	if (0 && 
-		(mHierarchyManager->GetViewSpaceSubdivisionType() == 
-		HierarchyManager::NO_VIEWSPACE_SUBDIV))
-	{
-		////////////////
-		//-- surface area heuristics
-
-		if (objects.empty())
-			return 0.0f;
-
-		const AxisAlignedBox3 box = EvalBoundingBox(objects);
-		const float area = box.SurfaceArea();
-		const float viewSpaceArea = mViewCellsManager->GetViewSpaceBox().SurfaceArea();
-
-		return (float)objects.size() * area / viewSpaceArea;
-	}
-	else
-	{	///////////////
-		//-- render cost heuristics
-
-		const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();
-
-		// probability that view point lies in a view cell which sees this node
-		const float p = EvalViewCellsVolume(objects) / viewSpaceVol;	
-
-		return (float)objects.size() * p;
-	}
+	///////////////
+	//-- render cost heuristics
+
+	const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();
+
+	// probability that view point lies in a view cell which sees this node
+	const float p = EvalViewCellsVolume(objects) / viewSpaceVol;
+    float objRenderCost = EvalAbsCost(objects);
+	
+	return objRenderCost * p;
 }
 
@@ -1590,13 +1602,6 @@
 int BvHierarchy::CountViewCells(const ObjectContainer &objects) const
 {
-	// no view cells yet
-	if (0 && mHierarchyManager->GetViewSpaceSubdivisionType() == 
-		HierarchyManager::NO_VIEWSPACE_SUBDIV)
-		return 1;
-
 	int nViewCells = 0;
-
 	ViewCell::NewMail();
-
 	ObjectContainer::const_iterator oit, oit_end = objects.end();
 
@@ -1728,6 +1733,6 @@
 {
 	// search nodes
-	std::map<BvhNode *, BvhIntersectable *>::const_iterator it 
-		= mBvhIntersectables.find(node);
+	std::map<BvhNode *, BvhIntersectable *>::const_iterator 
+		it = mBvhIntersectables.find(node);
 
 	if (it != mBvhIntersectables.end()) 
@@ -1816,8 +1821,9 @@
 void BvHierarchy::Initialise(const ObjectContainer &objects)
 {
+	AxisAlignedBox3 box = EvalBoundingBox(objects);
+
 	///////
 	//-- create new root
 
-	AxisAlignedBox3 box = EvalBoundingBox(objects);
 	BvhLeaf *bvhleaf = new BvhLeaf(box, NULL, (int)objects.size());
 	bvhleaf->mObjects = objects;
@@ -1834,5 +1840,4 @@
 /*
 Mesh *BvHierarchy::MergeLeafToMesh()
-void BvHierarchy::MergeLeavesToMeshes()
 {
 	vector<BvhLeaf *> leaves;
@@ -1903,6 +1908,5 @@
 	bvhLeaf->SetSubdivisionCandidate(oSubdivisionCandidate);
 
-	const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();
-	mTotalCost = (float)objects.size() * prop / viewSpaceVol;
+	mTotalCost = EvalRenderCost(objects);
 	mPvsEntries = CountViewCells(objects);
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1695)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1698)
@@ -609,4 +609,6 @@
 	/////////////////////////////////
 
+	float EvalAbsCost(const ObjectContainer &objects) const;
+
 protected:
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1696)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1698)
@@ -1638,6 +1638,5 @@
 	if (!obj) return;
 
-	//const float renderCost = mViewCellsManager->SimpleRay &raynderCost(obj);
-	const int renderCost = 1;
+	const float renderCost = mViewCellsManager->EvalRenderCost(obj);
 
 	// object in no pvs => new
@@ -1650,5 +1649,4 @@
 	// the object belongs to no pvs in this case?
 	//if (cf == Ray::COINCIDENT) return;
-
 	if (cf >= 0) // front pvs
 	{
@@ -1689,5 +1687,7 @@
 {
 	if (!leaf) return;
-	const int renderCost = countEntries ? 1 : (int)leaf->mObjects.size();
+
+	const float renderCost = countEntries ? 1 : 
+		mHierarchyManager->mBvHierarchy->EvalAbsCost(leaf->mObjects);
 	
 	// leaf in no pvs => new
@@ -1727,5 +1727,4 @@
 	}
 }
-
 
 
@@ -1801,4 +1800,5 @@
 			// test if this leaf is in valid view space
 			VspLeaf *leaf = dynamic_cast<VspLeaf *>(node);
+
 			if (leaf->TreeValid() && 
 				(!onlyUnmailed || !leaf->Mailed()) &&
