Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1294)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1308)
@@ -150,9 +150,4 @@
 	~HierarchyManager();
 
-	void Construct(
-		const VssRayContainer &sampleRays,
-		const ObjectContainer &objects,
-		AxisAlignedBox3 *forcedViewSpace);
-
 	/** Constructs the view space and object space subdivision from a given set of rays
 		and a set of objects.
@@ -160,12 +155,5 @@
 		@param objects the set of objects
 	*/
-	void ConstructInterleaved(
-		const VssRayContainer &sampleRays,
-		const ObjectContainer &objects,
-		AxisAlignedBox3 *forcedViewSpace);
-
-	/** Constructs first view space hierarchy, then object space hierarchy.
-	*/
-	void ConstructSequential(
+	void Construct(
 		const VssRayContainer &sampleRays,
 		const ObjectContainer &objects,
@@ -183,5 +171,5 @@
 	inline int GetObjectSpaceSubdivisonType() const
 	{
-		return mObjectSpaceSubdivisonType;
+		return mObjectSpaceSubdivisionType;
 	}
 	
@@ -215,12 +203,9 @@
 		first split candidates.
 	*/
-	void PrepareConstruction(
-		const VssRayContainer &sampleRays,
-		const ObjectContainer &objects,
-		AxisAlignedBox3 *forcedViewSpace,
-		RayInfoContainer &viewSpaceRays,
-		RayInfoContainer &objectSpaceRays);
-
-	void RunConstruction(const bool repair);
+	void PrepareObjectSpaceSubdivision(
+		const VssRayContainer &sampleRays,
+		const ObjectContainer &objects);
+
+	void RunConstruction();
 	bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
 
@@ -254,9 +239,9 @@
 
 
-	void ConstructBvHierarchy(
+	void PrepareBvHierarchy(
 		const VssRayContainer &sampleRays,
 		const ObjectContainer &objects);
 
-	void ConstructOspTree(
+	void PrepareOspTree(
 		const VssRayContainer &sampleRays,
 		const ObjectContainer &objects);
@@ -264,9 +249,11 @@
 	void ParseEnvironment();
 
+	bool StartObjectSpaceSubdivision() const;
+
 
 protected:
 
+	int mObjectSpaceSubdivisionType;
 	int mConstructionType;
-	int mObjectSpaceSubdivisonType;
 
 	VspTree *mVspTree;
@@ -290,4 +277,6 @@
 	HierarchyStatistics mHierarchyStats;
 
+	int mMinDepthForObjectSpaceSubdivion;
+
 	int mTermMaxLeaves;
 	ofstream mSubdivisionStats;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1305)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1308)
@@ -1304,4 +1304,8 @@
 													   const ObjectContainer &objects)
 {
+	mBvhStats.Reset();
+	mBvhStats.Start();
+	mBvhStats.nodes = 1;
+
 	// note matt: we assume that we have objects sorted by their id
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1305)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1308)
@@ -2604,8 +2604,11 @@
 												   RayInfoContainer &rays)
 {	
+	mVspStats.Reset();
+	mVspStats.Start();
+	mVspStats.nodes = 1;
+
 	// store pointer to this tree
 	VspSubdivisionCandidate::sVspTree = this;
-	mVspStats.nodes = 1;
-
+	
 	// compute view space bounding box
 	ComputeBoundingBox(sampleRays, forcedViewSpace);
@@ -2649,4 +2652,5 @@
 
 	//-- compute first split candidate
+
 	VspSubdivisionCandidate *splitCandidate = new VspSubdivisionCandidate(vData);
     EvalSubdivisionCandidate(*splitCandidate);
@@ -2969,5 +2973,5 @@
 	if (leaf->Mailed()) // leaf already mailed
 		return 0;
-
+	
 	leaf->Mail();
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1305)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1308)
@@ -36,10 +36,10 @@
 HierarchyManager::HierarchyManager(VspTree *vspTree, 
 								   const int objectSpaceSubdivisionType):
-mObjectSpaceSubdivisonType(objectSpaceSubdivisionType),
+mObjectSpaceSubdivisionType(objectSpaceSubdivisionType),
 mVspTree(vspTree), 
 mOspTree(NULL), 
 mBvHierarchy(NULL)
 {
-	switch(mObjectSpaceSubdivisonType)
+	switch(mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
@@ -67,5 +67,5 @@
 
 HierarchyManager::HierarchyManager(VspTree *vspTree, KdTree *kdTree): 
-mObjectSpaceSubdivisonType(KD_BASED_OBJ_SUBDIV),
+mObjectSpaceSubdivisionType(KD_BASED_OBJ_SUBDIV),
 mVspTree(vspTree), 
 mBvHierarchy(NULL)
@@ -102,4 +102,6 @@
 		"Hierarchy.Construction.type", mConstructionType);
 
+	mMinDepthForObjectSpaceSubdivion = -1;
+
 	Debug << "******** Hierachy Manager Parameters ***********" << endl;
 	Debug << "max leaves: " << mTermMaxLeaves << endl;
@@ -143,53 +145,9 @@
 
 
-void HierarchyManager::PrepareConstruction(const VssRayContainer &sampleRays,
-										   const ObjectContainer &objects,
-										   AxisAlignedBox3 *forcedViewSpace,
-										   RayInfoContainer &viewSpaceRays,
-										   RayInfoContainer &objectSpaceRays)
-{
-	mHierarchyStats.Reset();
-	mHierarchyStats.Start();
-
-	switch (mObjectSpaceSubdivisonType)
-	{
-	case KD_BASED_OBJ_SUBDIV:
-		{
-			SubdivisionCandidate *vsc = 
-				mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, viewSpaceRays);
-			mTQueue.Push(vsc);
-
-			SubdivisionCandidate *osc;
-			osc = mOspTree->PrepareConstruction(sampleRays, objects, objectSpaceRays);
-			mTQueue.Push(osc);
-			break;
-		}
-	case BV_BASED_OBJ_SUBDIV:
-		{
-			mBvHierarchy->CreateRoot(objects);
-
-			SubdivisionCandidate *vsc = 
-				mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, viewSpaceRays);
-			mTQueue.Push(vsc);
-
-			SubdivisionCandidate *osc;
-			osc = mBvHierarchy->PrepareConstruction(sampleRays, objects);
-			mTQueue.Push(osc);
-			break;
-		}
-	default:
-		break;
-	}
-
-	mTotalCost = (float)objects.size();
-	Debug << "setting total cost to " << mTotalCost << endl;
-}
-
-
 void HierarchyManager::EvalSubdivisionStats(const SubdivisionCandidate &tData)
 {
 	const float costDecr = tData.GetRenderCostDecrease();
 
-	switch (mObjectSpaceSubdivisonType)
+	switch (mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
@@ -219,5 +177,5 @@
 										   const float totalRenderCost)
 {
-	mSubdivisionStats 
+	mSubdivisionStats
 			<< "#Splits\n" << splits << endl
 			<< "#RenderCostDecrease\n" << renderCostDecr << endl 
@@ -241,51 +199,47 @@
 								AxisAlignedBox3 *forcedViewSpace)
 {
-	switch (mConstructionType)
-	{
-	case 0: 
-		ConstructSequential(sampleRays, objects, forcedViewSpace);
-		break;
-	case 1:
-		ConstructInterleaved(sampleRays, objects, forcedViewSpace);
-		break;
-	default:
-		break;
-	}
-}
-
-
-void HierarchyManager::ConstructInterleaved(const VssRayContainer &sampleRays,
-											const ObjectContainer &objects,
-											AxisAlignedBox3 *forcedViewSpace)
-{
-	RayInfoContainer *objectSpaceRays = new RayInfoContainer();
+	
 	RayInfoContainer *viewSpaceRays = new RayInfoContainer();
 
-	// prepare vsp and osp trees for traversal
-	PrepareConstruction(sampleRays, 
-						objects, 
-						forcedViewSpace, 
-						*viewSpaceRays, 
-						*objectSpaceRays);
-
+	mHierarchyStats.Reset();
+	mHierarchyStats.Start();
+
+	// start with view space subdivison: prepare vsp tree for traversal
+	SubdivisionCandidate *vsc = 
+		mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, *viewSpaceRays);
+	mTQueue.Push(vsc);
+
+	mTotalCost = (float)objects.size();
+	Debug << "setting total cost to " << mTotalCost << endl;
+
+	const long startTime = GetTime();	
 	cout << "Constructing view space / object space tree ... \n";
-	const long startTime = GetTime();	
 	
-	const bool repairQueue = true;
-	//const bool repairQueue = false;
-
 	// process object space candidates
-	RunConstruction(repairQueue);
+	RunConstruction();
+	
 	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-
 	mVspTree->mVspStats.Stop();
 }
 
 
-void HierarchyManager::ConstructBvHierarchy(const VssRayContainer &sampleRays,
-											const ObjectContainer &objects) 
-
-{
-	Debug << "\n$$$$$$$$$ bv hierarchy construction $$$$$$$$$$\n" << endl;
+void HierarchyManager::PrepareObjectSpaceSubdivision(const VssRayContainer &sampleRays,
+													 const ObjectContainer &objects)
+{
+	if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
+	{
+		PrepareOspTree(sampleRays, objects);
+	}
+	else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
+	{
+		PrepareBvHierarchy(sampleRays, objects);
+	}
+}
+
+
+void HierarchyManager::PrepareBvHierarchy(const VssRayContainer &sampleRays,
+										  const ObjectContainer &objects) 
+
+{
 	cout << "starting bv hierarchy construction ... " << endl;
 
@@ -298,28 +252,14 @@
 	mTotalCost = mBvHierarchy->mTotalCost;
 	Debug << "reseting cost, new total cost: " << mTotalCost << endl;
-	
+
     mTQueue.Push(sc);
-
-	mBvHierarchy->mBvhStats.Reset();
-	mBvHierarchy->mBvhStats.Start();
-
-	const long startTime = GetTime();
-
-	const bool repairQueue = false;
-	// process object space candidates
-	RunConstruction(repairQueue);
-
-	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-	mBvHierarchy->mBvhStats.Stop();
-}
-
-
-void HierarchyManager::ConstructOspTree(const VssRayContainer &sampleRays,
-										const ObjectContainer &objects) 
-
+}
+
+
+void HierarchyManager::PrepareOspTree(const VssRayContainer &sampleRays,
+									  const ObjectContainer &objects)
 {
 	RayInfoContainer *objectSpaceRays = new RayInfoContainer();
 
-	Debug << "\n$$$$$$$$$ osp tree construction $$$$$$$$$$\n" << endl;
 	cout << "starting osp tree construction ... " << endl;
 
@@ -335,24 +275,5 @@
 	
     mTQueue.Push(osc);
-
-	mOspTree->mOspStats.Reset();
-	mOspTree->mOspStats.Start();
-
-	const long startTime = GetTime();
-		
-	const bool repairQueue = false;
-	// process object space candidates
-	RunConstruction(repairQueue);
-
-	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-	mOspTree->mOspStats.Stop();
-
-	//////////////////////////
-	// matt: only for debugging purpose
-
-	const float rc = mOspTree->EvalRenderCost(sampleRays);
-	Debug << "My render cost evalulation: " << rc << endl;
-}
-
+}
 
 
@@ -371,5 +292,5 @@
 	else
 	{
-		if (mObjectSpaceSubdivisonType == KD_BASED_OBJ_SUBDIV)
+		if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
 		{
 			KdNode *n = mOspTree->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
@@ -378,5 +299,5 @@
 				return false;
 		}
-		else if (mObjectSpaceSubdivisonType == BV_BASED_OBJ_SUBDIV)
+		else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
 		{
 			BvhNode *n = mBvHierarchy->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
@@ -386,16 +307,39 @@
 		}
 	}
-	
 	return true;//!globalTerminationCriteriaMet;
 }
 
 
-void HierarchyManager::RunConstruction(const bool repair)
+bool HierarchyManager::StartObjectSpaceSubdivision() const
+{
+	const bool constructAfterViewSpace = (mConstructionType == 1) && mTQueue.Empty();
+
+	return 
+		NO_OBJ_SUBDIV &&
+		((mMinDepthForObjectSpaceSubdivion < 0/*mVspTree->mVspStats.mMaxDepth*/) || constructAfterViewSpace);
+}
+
+
+void HierarchyManager::RunConstruction()
 {
 	mHierarchyStats.nodes = 0;
 	mGlobalCostMisses = 0;
 
-	while (!FinishedConstruction())
-	{
+	// use objects for evaluating vsp tree construction
+	const int savedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
+	mObjectSpaceSubdivisionType = NO_OBJ_SUBDIV;
+
+	//const bool repairQueue = false;
+	const bool repairQueue = true;
+
+	do
+	{
+		// use objects for evaluating vsp tree construction until a certain depth
+		if (StartObjectSpaceSubdivision())
+		{
+			mObjectSpaceSubdivisionType = savedObjectSpaceSubdivisionType;
+			//PrepareObjectSpaceSubdivision();
+		}
+
 		mCurrentCandidate = NextSubdivisionCandidate();    
 		mTotalCost -= mCurrentCandidate->GetRenderCostDecrease();
@@ -420,9 +364,11 @@
 			// reevaluate candidates affected by the split for view space splits, 
 			// this would be object space splits and other way round
-			if (repair) RepairQueue();
-		}
-		
+			if (repairQueue) RepairQueue();
+		}	
 		DEL_PTR(mCurrentCandidate);
 	}
+	while (!FinishedConstruction());
+
+	mObjectSpaceSubdivisionType = savedObjectSpaceSubdivisionType;
 }
 
@@ -436,5 +382,5 @@
 void HierarchyManager::CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
 {
-	switch (mObjectSpaceSubdivisonType)
+	switch (mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
@@ -485,5 +431,4 @@
 void HierarchyManager::RepairQueue()
 {
-	// TODO
 	// for each update of the view space partition:
 	// the candidates from object space partition which
@@ -535,5 +480,5 @@
 {
 	// the type of the view cells hierarchy
-	switch (mObjectSpaceSubdivisonType)
+	switch (mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
@@ -559,5 +504,5 @@
 	if (!obj) return false;
 
-	switch (mObjectSpaceSubdivisonType)
+	switch (mObjectSpaceSubdivisionType)
 	{
 	case NO_OBJ_SUBDIV:
@@ -583,5 +528,5 @@
 void HierarchyManager::PrintObjectSpaceHierarchyStatistics(ofstream &stream) const
 {
-	switch (mObjectSpaceSubdivisonType)
+	switch (mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
@@ -604,5 +549,5 @@
 												  const ObjectContainer &objects) const
 {
-	switch (mObjectSpaceSubdivisonType)
+	switch (mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
@@ -640,68 +585,3 @@
 
 
-void HierarchyManager::ConstructSequential(const VssRayContainer &sampleRays,
-										   const ObjectContainer &objects,
-										   AxisAlignedBox3 *forcedViewSpace)
-{
-	// rays clipped in view space and in object space
-	RayInfoContainer *viewSpaceRays = new RayInfoContainer();
-	
-
-	/////////////////////////////////////////////////////////////
-	// view space space partition
-	/////////////////////////////////////////////////////////////
-
-
-	// use objects for evaluating vsp tree construction
-	const int savedobjectSpaceSubdivisionType = mObjectSpaceSubdivisonType;
-	mObjectSpaceSubdivisonType = NO_OBJ_SUBDIV;
-
-	SubdivisionCandidate *vsc = 
-		mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, *viewSpaceRays);
-	
-	// add to subdivision candidate to split candidate queue
-	mTQueue.Push(vsc);
-
-	long startTime = GetTime();
-	cout << "starting vsp construction ... " << endl;
-
-	mVspTree->mVspStats.Reset();
-	mVspTree->mVspStats.Start();
-
-	// all objects can be seen from everywhere
-	mTotalCost = (float)dynamic_cast<VspTree::VspSubdivisionCandidate *>(vsc)->mParentData.mPvs;
-
-	const bool repairQueue = false;
-	// process view space candidates
-	RunConstruction(repairQueue);
-
-	cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl;
-	mVspTree->mVspStats.Stop();
-
-	// reset subdivision type
-	mObjectSpaceSubdivisonType = savedobjectSpaceSubdivisionType;
-
-
-	/////////////////////////////////////////////////////////////
-	// object space partition
-	/////////////////////////////////////////////////////////////
-	
-	switch (mObjectSpaceSubdivisonType)
-	{
-	case KD_BASED_OBJ_SUBDIV:
-		{
-			ConstructOspTree(sampleRays, objects);
-			break;
-		}
-	case BV_BASED_OBJ_SUBDIV:
-		{
-			ConstructBvHierarchy(sampleRays, objects);
-			break;
-		}
-	default:
-		break;
-	}
-}
-
-
-}
+}
