Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1297)
@@ -26,5 +26,5 @@
 static float debugVol = 0;
 
-int BvhNode::sMailId = 2147483647;
+int BvhNode::sMailId = 10000;//2147483647;
 int BvhNode::sReservedMailboxes = 1;
 
@@ -44,10 +44,10 @@
 /***************************************************************/
 
-BvhNode::BvhNode(): mParent(NULL)
+BvhNode::BvhNode(): mParent(NULL), mMailbox(0)
 {
 }
 
 BvhNode::BvhNode(const AxisAlignedBox3 &bbox):
-mParent(NULL), mBoundingBox(bbox)
+mParent(NULL), mBoundingBox(bbox), mMailbox(0)
 {
 }
@@ -55,5 +55,5 @@
 
 BvhNode::BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent):
-mBoundingBox(bbox), mParent(parent)
+mBoundingBox(bbox), mParent(parent), mMailbox(0)
 {
 }
@@ -217,8 +217,4 @@
 
 	//-- factors for bsp tree split plane heuristics
-	Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.ct_div_ci", mCtDivCi);
-
-	//-- partition criteria
-	Environment::GetSingleton()->GetFloatValue("BvHierarchy.Construction.epsilon", mEpsilon);
 
 	// if only the driving axis is used for axis aligned split
@@ -227,10 +223,8 @@
 	Environment::GetSingleton()->GetBoolValue("BvHierarchy.useCostHeuristics", mUseCostHeuristics);
 
-
 	char subdivisionStatsLog[100];
 	Environment::GetSingleton()->GetStringValue("BvHierarchy.subdivisionStats", subdivisionStatsLog);
 	mSubdivisionStats.open(subdivisionStatsLog);
 
-	Environment::GetSingleton()->GetFloatValue("BvHierarchy.Construction.splitBorder", mSplitBorder);
 	Environment::GetSingleton()->GetFloatValue(
 		"BvHierarchy.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
@@ -240,5 +234,4 @@
 
 	Debug << "******* Bvh hierarchy options ******** " << endl;
-
     Debug << "max depth: " << mTermMaxDepth << endl;
 	Debug << "min probabiliy: " << mTermMinProbability<< endl;
@@ -247,7 +240,5 @@
 	Debug << "miss tolerance: " << mTermMissTolerance << endl;
 	Debug << "max leaves: " << mTermMaxLeaves << endl;
-
 	Debug << "randomize: " << randomize << endl;
-
 	Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
 	Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
@@ -387,4 +378,8 @@
 		EvalSubdivisionCandidate(*frontCandidate);
 		EvalSubdivisionCandidate(*backCandidate);
+	
+		// cross reference
+		tFrontData.mNode->SetSubdivisionCandidate(frontCandidate); 
+		tBackData.mNode->SetSubdivisionCandidate(backCandidate);
 
 		tQueue.Push(frontCandidate);
@@ -397,11 +392,10 @@
 
 	//-- terminate traversal
-	if (newNode->IsLeaf())
-	{
+    if (newNode->IsLeaf())
+	{
+		//-- store additional info
 		EvaluateLeafStats(tData);
         
-		const bool mStoreRays= true;
-
-		//-- store additional info
+		const bool mStoreRays = true;
 		if (mStoreRays)
 		{
@@ -429,9 +423,9 @@
 
 	// cost ratio violated?
-	const bool maxCostRatioViolated = ratio < mTermMaxCostRatio;
+	const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
 
 	splitCandidate.mMaxCostMisses = maxCostRatioViolated ? 
-		splitCandidate.mParentData.mMaxCostMisses : 
-		splitCandidate.mParentData.mMaxCostMisses + 1;
+		splitCandidate.mParentData.mMaxCostMisses + 1 :
+		splitCandidate.mParentData.mMaxCostMisses;
 
 	const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
@@ -451,5 +445,5 @@
 	splitCandidate.SetRenderCostDecrease(renderCostDecr);
 
-#if 1
+#if 0
 	const float priority = (float)-splitCandidate.mParentData.mDepth;
 #else	
@@ -520,4 +514,5 @@
 
 
+#if 0
 float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
 											const int axis,
@@ -535,5 +530,5 @@
 	{
 		Intersectable *obj = *oit;
-		AxisAlignedBox3 box = obj->GetBox();
+		const AxisAlignedBox3 box = obj->GetBox();
 
 		const float objMid = (box.Max(axis) + box.Min(axis)) * 0.5f;
@@ -553,4 +548,38 @@
 	return ratio;
 }
+
+#else
+
+float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
+											const int axis,
+											ObjectContainer &objectsFront,
+											ObjectContainer &objectsBack)
+{
+	SortSubdivisionCandidates(tData, axis);
+	
+	vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end();
+
+	int i = 0;
+	const int border = (int)tData.mNode->mObjects.size() / 2;
+
+    for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ i)
+	{
+		Intersectable *obj = (*cit).mObject;
+
+		// object mailed => belongs to back objects
+		if (i < border) 
+			objectsBack.push_back(obj);
+		else
+			objectsFront.push_back(obj);
+	}
+
+	const float oldRenderCost = tData.mProbability * (float)tData.mNode->mObjects.size();
+	const float newRenderCost = 
+		EvalRenderCost(tData, objectsFront, objectsBack);
+
+	const float ratio = newRenderCost / oldRenderCost;
+	return ratio;
+}
+#endif
 
 
@@ -719,5 +748,4 @@
 	// mail view cells on the left side
 	ViewCell::NewMail();
-	
 	// mail the objects on the left side
 	Intersectable::NewMail();
@@ -762,5 +790,4 @@
 			// => add volume to left node
 			volLeft += vol;
-			//volRight -= vol;
 		}
 
@@ -815,5 +842,5 @@
 		{
 			if (mUseCostHeuristics)
-			{
+			{cout << "h";
 				//-- partition objects using heuristics
 				nCostRatio[axis] =
@@ -825,5 +852,5 @@
 			}
 			else
-			{
+			{cout << "m";
 				nCostRatio[axis] =
 					EvalLocalObjectPartition(
@@ -1072,9 +1099,11 @@
 	ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
 
+	Debug << "collecting " << (int)viewCells.size() << " dirty candidates" << endl;
 	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
 	{
 		VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
-
 		VspLeaf *leaf = vc->mLeaf;
+		SubdivisionCandidate *candidate = leaf->GetSubdivisionCandidate();
+		
 		dirtyList.push_back(leaf->GetSubdivisionCandidate());
 	}
@@ -1108,16 +1137,13 @@
 {
 	// rather use the simple version
-	if (object->mBvhLeaf)
-	{
-		return object->mBvhLeaf;
-	}
-
+	if (!object) return NULL;
+	return object->mBvhLeaf;
+	
 	///////////////////////////////////////
 	// start from root of tree
+	
 	if (node == NULL)
-	{
 		node = mRoot;
-	}
-
+	
 	vector<BvhLeaf *> leaves;
 
@@ -1182,73 +1208,4 @@
 
 
-/*
-int BvHierarchy::UpdateViewCellsPvs(BvhLeaf *leaf, 
-								const RayInfoContainer &rays) const
-
-{
-	MailablePvsData::NewMail();
-	
-	ViewCellContainer touchedViewCells;
-	CollectTouchedViewCells(rays, touchedViewCells);
-
-	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
-
-	for (oit = leaf->mObjects.begin(); oit < oit_end; ++ oit)
-	{
-		Intersectable *obj = *oit;
-		ViewCellContainer::const_iterator vit, vit_end = touchedViewCells.end();
-
-		// traverse through view cells and classify them according
-		// to them being seen from to back / front / front and back node
-		for (vit = touchedViewCells.begin(); vit != vit_end; ++ vit)
-		{
-			ViewCell *vc = *vit;
-			float contri;
-			AddViewCellToObjectPvs(obj, vc, contri, true);
-		}
-	}
-
-	return 0;
-}
-
-
-int BvHierarchy::RemoveParentViewCellsPvs(BvhLeaf *leaf, 
-									  const RayInfoContainer &rays
-									  ) const
-
-{
-	MailablePvsData::NewMail();
-	
-	ViewCellContainer touchedViewCells;
-	CollectTouchedViewCells(rays, touchedViewCells);
-
-	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
-
-	for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
-	{
-		Intersectable *obj = *oit;
-
-		// traverse through view cells and classify them according
-		// to them being seen from to back / front / front and back node
-        ViewCellContainer::const_iterator vit, vit_end = touchedViewCells.end();
-
-		for (vit = touchedViewCells.begin(); vit != vit_end; ++ vit)
-		{
-			ViewCell *vc = *vit;
-			
-			MailablePvsData *vdata = obj->mViewCellPvs.Find(vc);
-
-			if (vdata && !vdata->Mailed())
-			{
-				vdata->Mail();
-				obj->mViewCellPvs.RemoveSample(vc, 1);
-			}
-		}
-	}
-
-	return 0;
-}
-*/
-
 bool BvHierarchy::Export(OUT_STREAM &stream)
 {
