Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1680)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1684)
@@ -338,7 +338,6 @@
 	//AssociateObjectsWithLeaf(node->GetFront());
    
-	node->mRenderCostDecr += sc.GetRenderCostDecrease();
-	node->mPvsEntriesIncr += sc.GetPvsEntriesIncr();
-
+	node->mRenderCostDecr = sc.GetRenderCostDecrease();
+	node->mPvsEntriesIncr = sc.GetPvsEntriesIncr();
 }
 									
@@ -370,8 +369,6 @@
 	AxisAlignedBox3 bbox = EvalBoundingBox(sc.mBackObjects, &parentBox);
 
-	BvhLeaf *back = 
-		new BvhLeaf(bbox, node, (int)sc.mBackObjects.size());
-	BvhLeaf *front = 
-		new BvhLeaf(fbox, node, (int)sc.mFrontObjects.size());
+	BvhLeaf *back = new BvhLeaf(bbox, node, (int)sc.mBackObjects.size());
+	BvhLeaf *front = new BvhLeaf(fbox, node, (int)sc.mFrontObjects.size());
 
 	BvhInterior *parent = leaf->GetParent();
@@ -428,9 +425,6 @@
 	backData.mMaxCostMisses = sc.GetMaxCostMisses();
 	
-	// set the time stamp so the order of traversal can be reconstructed
-	node->mTimeStamp = mHierarchyManager->mTimeStamp ++;
-	//cout << "here4 " << node->mTimeStamp << endl;
-	node->mRenderCostDecr = sc.GetRenderCostDecrease();
-	node->mPvsEntriesIncr = sc.GetPvsEntriesIncr();
+	//node->mRenderCostDecr = sc.GetRenderCostDecrease();
+	//node->mPvsEntriesIncr = sc.GetPvsEntriesIncr();
 
 	// assign the objects in sorted order
@@ -465,4 +459,9 @@
 		currentNode = SubdivideNode(*sc, tFrontData, tBackData);
 	
+		// set the time stamp so the order of traversal can be reconstructed
+		currentNode->mTimeStamp = mHierarchyManager->mTimeStamp ++;
+		//currentNode->mRenderCostDecr = sc.GetRenderCostDecrease();
+		//currentNode->mPvsEntriesIncr = sc.GetPvsEntriesIncr();
+
 		// decrease the weighted average cost of the subdivisoin
 		mTotalCost -= sc->GetRenderCostDecrease();
@@ -1293,5 +1292,5 @@
 				if (useVisibilityBasedHeuristics)
 				{
-					cout << "v";
+					//cout << "v rays: " << tData.mNumRays << " " << endl;
 					///////////
 					//-- heuristics using objects weighted by view cells volume
@@ -1300,5 +1299,5 @@
 				}
 				else
-				{	cout << "e";
+				{	cout << "e rays: " << tData.mNumRays << " ";
 					//////////////////
 					//-- view cells not constructed yet	=> use surface area heuristic			
@@ -2124,3 +2123,81 @@
 
 
-}
+SubdivisionCandidate *BvHierarchy::CreateSubdivisionCandidate(BvhInterior *oldNode)
+{
+	return new BvhSubdivisionCandidate(BvhTraversalData());
+}
+
+
+BvhNode *BvHierarchy::SubdivideAndCopy(SplitQueue &tQueue,
+									   SubdivisionCandidate *splitCandidate,
+									   const bool globalCriteriaMet,
+									   BvhNode *originalNode)
+{
+	BvhSubdivisionCandidate *sc = dynamic_cast<BvhSubdivisionCandidate *>(splitCandidate);
+	BvhTraversalData &tData = sc->mParentData;
+
+	BvhNode *currentNode = tData.mNode;
+	BvhNode *oldNode = (BvhNode *)splitCandidate->mEvaluationHack;
+
+	if (!oldNode->IsLeaf())
+	{	
+		//////////////
+		//-- continue subdivision
+
+		BvhTraversalData tFrontData;
+		BvhTraversalData tBackData;
+			
+		BvhInterior *oldInterior = dynamic_cast<BvhInterior *>(oldNode);
+		//sc->SetSplitPlane(oldInterior->mSplitPlane);
+
+		oldInterior->GetFront()->CollectObjects(sc->mFrontObjects);
+		oldInterior->GetBack()->CollectObjects(sc->mBackObjects);
+		EvalSubdivisionCandidate(*sc);
+
+		// create new interior node and two leaf node
+		currentNode = SubdivideNode(*sc, tFrontData, tBackData);
+	
+		// evaluate the changes in render cost and pvs entries
+		EvalSubdivisionCandidate(*sc, false);
+
+		currentNode->mRenderCostDecr = oldNode->mRenderCostDecr + sc->GetRenderCostDecrease();
+		currentNode->mPvsEntriesIncr = oldNode->mPvsEntriesIncr + sc->GetPvsEntriesIncr();
+
+		///////////////////////////
+		//-- push the new split candidates on the queue
+		
+		BvhSubdivisionCandidate *frontCandidate = new BvhSubdivisionCandidate(tFrontData);
+		BvhSubdivisionCandidate *backCandidate = new BvhSubdivisionCandidate(tBackData);
+
+		frontCandidate->SetPriority((float)-oldInterior->GetFront()->mTimeStamp);
+		backCandidate->SetPriority((float)-oldInterior->GetFront()->mTimeStamp);
+
+		frontCandidate->mEvaluationHack = oldInterior->GetFront();
+		backCandidate->mEvaluationHack = oldInterior->GetBack();
+
+		// cross reference
+		tFrontData.mNode->SetSubdivisionCandidate(frontCandidate); 
+		tBackData.mNode->SetSubdivisionCandidate(backCandidate);
+
+		//cout << "f: " << frontCandidate->GetPriority() << " b: " << backCandidate->GetPriority() << endl;
+		tQueue.Push(frontCandidate);
+		tQueue.Push(backCandidate);
+	}
+
+	/////////////////////////////////
+	//-- node is a leaf => terminate traversal
+
+	if (currentNode->IsLeaf())
+	{
+		// this leaf is no candidate for splitting anymore
+		// => detach subdivision candidate
+		tData.mNode->SetSubdivisionCandidate(NULL); 
+		// detach node so we don't delete it with the traversal data
+		tData.mNode = NULL;
+	}
+	
+	return currentNode;
+}
+
+
+}
