Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1344)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1370)
@@ -95,4 +95,7 @@
 		"Hierarchy.Termination.globalCostMissTolerance", mTermGlobalCostMissTolerance);
 
+	Environment::GetSingleton()->GetBoolValue(
+		"Hierarchy.Construction.startWithObjectSpace", mStartWithObjectSpace);
+
 	Environment::GetSingleton()->GetIntValue(
 		"Hierarchy.Termination.maxLeaves", mTermMaxLeaves);
@@ -103,4 +106,7 @@
 	Environment::GetSingleton()->GetIntValue(
 		"Hierarchy.Construction.minDepthForOsp", mMinDepthForObjectSpaceSubdivion);
+
+	Environment::GetSingleton()->GetIntValue(
+		"Hierarchy.Construction.minDepthForVsp", mMinDepthForViewSpaceSubdivion);
 	
 	Environment::GetSingleton()->GetBoolValue(
@@ -125,4 +131,16 @@
 
 
+int HierarchyManager::GetObjectSpaceSubdivisionType() const
+{
+	return mObjectSpaceSubdivisionType;
+}
+
+
+int HierarchyManager::GetViewSpaceSubdivisionType() const
+{
+	return mViewSpaceSubdivisionType;
+}
+
+
 void HierarchyManager::SetViewCellsManager(ViewCellsManager *vcm)
 {
@@ -220,4 +238,7 @@
 	mObjectSpaceSubdivisionType = NO_OBJ_SUBDIV;
 
+	mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
+	mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
+
 	// start with view space subdivison: prepare vsp tree for traversal
 	if (StartViewSpaceSubdivision())
@@ -238,6 +259,11 @@
 	
 	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
 	mHierarchyStats.Stop();
 	mVspTree->mVspStats.Stop();
+	FinishObjectSpaceSubdivision();
+
+	mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
+	mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
 }
 
@@ -247,4 +273,6 @@
 												   AxisAlignedBox3 *forcedViewSpace)
 {
+	cout << "starting view space hierarchy construction ... " << endl;
+
 	RayInfoContainer *viewSpaceRays = new RayInfoContainer();
 	SubdivisionCandidate *vsc = 
@@ -291,7 +319,7 @@
 									  const ObjectContainer &objects)
 {
+	cout << "starting osp tree construction ... " << endl;
+
 	RayInfoContainer *objectSpaceRays = new RayInfoContainer();
-
-	cout << "starting osp tree construction ... " << endl;
 
 	// start with one big kd cell - all objects can be seen from everywhere
@@ -342,11 +370,39 @@
 
 
+int HierarchyManager::GetObjectSpaceSubdivisionDepth() const
+{
+	int maxDepth = 0;
+
+	if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
+	{
+		maxDepth = mOspTree->mOspStats.maxDepth;
+	}
+	else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
+	{
+		maxDepth = mBvHierarchy->mBvhStats.maxDepth;
+	}
+
+	return maxDepth;
+}
+
+
 bool HierarchyManager::StartObjectSpaceSubdivision() const
 {
-	const bool ospDepthReached = 
-		(mMinDepthForObjectSpaceSubdivion <= mVspTree->mVspStats.maxDepth);
-    
-	return !ObjectSpaceSubdivisionConstructed() && 
-		   (mTQueue.Empty() || ((mConstructionType == INTERLEAVED) && ospDepthReached));
+	// view space construction already started
+	if (ObjectSpaceSubdivisionConstructed())
+		return false;
+
+	// start immediately with object space subdivision?
+	if (mStartWithObjectSpace)
+		return true;
+
+	// is the queue empty again?
+	if (ViewSpaceSubdivisionConstructed() && mTQueue.Empty())
+		return true;
+
+	// has the depth for subdivision been reached?
+	return 
+		((mConstructionType == INTERLEAVED) && 
+		 (mMinDepthForObjectSpaceSubdivion <= mVspTree->mVspStats.maxDepth));
 }
 
@@ -354,9 +410,20 @@
 bool HierarchyManager::StartViewSpaceSubdivision() const
 {
-	const bool vspDepthReached = 
-		(mMinDepthForObjectSpaceSubdivion <= mVspTree->mVspStats.maxDepth);
-    
-	return !ViewSpaceSubdivisionConstructed() && 
-		   (mTQueue.Empty() || ((mConstructionType == INTERLEAVED) && vspDepthReached));
+	// view space construction already started
+	if (ViewSpaceSubdivisionConstructed())
+		return false;
+
+	// start immediately with view space subdivision?
+	if (!mStartWithObjectSpace)
+		return true;
+
+	// is the queue empty again?
+	if (ObjectSpaceSubdivisionConstructed() && mTQueue.Empty())
+		return true;
+
+	// has the depth for subdivision been reached?
+	return 
+		((mConstructionType == INTERLEAVED) && 
+		 (mMinDepthForViewSpaceSubdivion <= GetObjectSpaceSubdivisionDepth()));
 }
 
@@ -384,4 +451,5 @@
 		}
 		
+		///////////////////////////
 		//-- subdivide leaf node
 		if (ApplySubdivisionCandidate(mCurrentCandidate))
@@ -420,6 +488,6 @@
 
 			cout << "starting view space subdivision at depth " 
-				 << mVspTree->mVspStats.maxDepth << " (" 
-				 << mMinDepthForObjectSpaceSubdivion << ") " << endl;
+				 << GetObjectSpaceSubdivisionDepth() << " (" 
+				 << mMinDepthForViewSpaceSubdivion << ") " << endl;
 
 			PrepareViewSpaceSubdivision(sampleRays, objects, forcedViewSpace);
@@ -432,6 +500,4 @@
 		DEL_PTR(mCurrentCandidate);
 	}
-
-	mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
 }
 
@@ -632,5 +698,7 @@
 		{
 			BvhLeaf *leaf = mBvHierarchy->GetLeaf(obj);
-			return mBvHierarchy->AddLeafToPvs(leaf, vc, pdf, contribution);
+			BvhIntersectable *bvhObj = mBvHierarchy->GetOrCreateBvhIntersectable(leaf);
+			
+			return vc->AddPvsSample(bvhObj, pdf, contribution);
 		}
 	default:
@@ -647,4 +715,5 @@
 	stream << mVspTree->GetStatistics() << endl;
 	stream << "\nobject space:" << endl << endl;
+
 	switch (mObjectSpaceSubdivisionType)
 	{
@@ -726,3 +795,22 @@
 
 
-}
+void HierarchyManager::FinishObjectSpaceSubdivision() const
+{
+	switch (mObjectSpaceSubdivisionType)
+	{
+	case KD_BASED_OBJ_SUBDIV:
+		{
+			mOspTree->mOspStats.Stop();
+			break;
+		}
+	case BV_BASED_OBJ_SUBDIV:
+		{
+			mBvHierarchy->mBvhStats.Stop();
+			break;
+		}
+	default:
+		break;
+	}
+}
+
+}
