Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1640)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1642)
@@ -26,54 +26,75 @@
 
 
-METHOD=interleaved_full
-echo "$SCENE $METHOD"
+# METHOD=interleaved_full
+# echo "$SCENE $METHOD"
 
-$PROGRAM $ENVIRONMENT \
--view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
--view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
--vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
--bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
--hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
--hierarchy_construction_type=1 \
--hierarchy_construction_recompute_split_on_repair=true
+# $PROGRAM $ENVIRONMENT \
+# -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
+# -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+# -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+# -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+# -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+# -hierarchy_construction_type=1 \
+# -hierarchy_construction_recompute_split_on_repair=true
 
-mv debug.log $LOG_PREFIX-$METHOD-debug.log
-mv view_cells.wrl $LOG_PREFIX-$METHOD-view_cells.wrl
-mv final_object_partition.wrl $LOG_PREFIX-$METHOD-final_object_partition.wrl
-mv final_view_cells.wrl $LOG_PREFIX-$METHOD-final_view_cells.wrl
+# mv debug.log $LOG_PREFIX-$METHOD-debug.log
+# mv view_cells.wrl $LOG_PREFIX-$METHOD-view_cells.wrl
+# mv final_object_partition.wrl $LOG_PREFIX-$METHOD-final_object_partition.wrl
+# mv final_view_cells.wrl $LOG_PREFIX-$METHOD-final_view_cells.wrl
 
 
-METHOD=interleaved
-echo "$SCENE $METHOD"
+# METHOD=interleaved
+# echo "$SCENE $METHOD"
 
-$PROGRAM $ENVIRONMENT \
--view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
--view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
--vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
--bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
--hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
--hierarchy_construction_type=1 \
--hierarchy_construction_recompute_split_on_repair=false
+# $PROGRAM $ENVIRONMENT \
+# -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
+# -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+# -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+# -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+# -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+# -hierarchy_construction_type=1 \
+# -hierarchy_construction_recompute_split_on_repair=false
 
-mv debug.log $LOG_PREFIX-$METHOD-debug.log
-mv view_cells.wrl $LOG_PREFIX-$METHOD-view_cells.wrl
-mv final_object_partition.wrl $LOG_PREFIX-$METHOD-final_object_partition.wrl
-mv final_view_cells.wrl $LOG_PREFIX-$METHOD-final_view_cells.wrl
+# mv debug.log $LOG_PREFIX-$METHOD-debug.log
+# mv view_cells.wrl $LOG_PREFIX-$METHOD-view_cells.wrl
+# mv final_object_partition.wrl $LOG_PREFIX-$METHOD-final_object_partition.wrl
+# mv final_view_cells.wrl $LOG_PREFIX-$METHOD-final_view_cells.wrl
 
 
-METHOD=gradient
-echo "$SCENE $METHOD"
+# METHOD=gradient
+# echo "$SCENE $METHOD"
 
-$PROGRAM $ENVIRONMENT \
--view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
--view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
--vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
--bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
--hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
--hierarchy_construction_type=2 \
--hierarchy_construction_recompute_split_on_repair=true
+# $PROGRAM $ENVIRONMENT \
+# -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
+# -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+# -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+# -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+# -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+# -hierarchy_construction_type=2 \
+# -hierarchy_construction_recompute_split_on_repair=true
 
-mv debug.log $LOG_PREFIX-$METHOD-debug.log
-mv view_cells.wrl $LOG_PREFIX-$METHOD-view_cells.wrl
-mv final_object_partition.wrl $LOG_PREFIX-$METHOD-final_object_partition.wrl
-mv final_view_cells.wrl $LOG_PREFIX-$METHOD-final_view_cells.wrl
+# mv debug.log $LOG_PREFIX-$METHOD-debug.log
+# mv view_cells.wrl $LOG_PREFIX-$METHOD-view_cells.wrl
+# mv final_object_partition.wrl $LOG_PREFIX-$METHOD-final_object_partition.wrl
+# mv final_view_cells.wrl $LOG_PREFIX-$METHOD-final_view_cells.wrl
+
+ METHOD=multilevel
+ echo "$SCENE $METHOD"
+
+ $PROGRAM $ENVIRONMENT \
+ -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
+ -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+ -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+ -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+ -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+ -hierarchy_construction_type=2 \
+ -hierarchy_construction_multilevel=true \
+ -hierarchy_construction_samples=20000 \
+ -hierarchy_term_max_leaves=500 \
+ -hierarchy_construction_levels=1 \
+ -hierarchy_construction_recompute_split_on_repair=true
+
+ mv debug.log $LOG_PREFIX-$METHOD-debug.log
+ mv view_cells.wrl $LOG_PREFIX-$METHOD-view_cells.wrl
+ mv final_object_partition.wrl $LOG_PREFIX-$METHOD-final_object_partition.wrl
+ mv final_view_cells.wrl $LOG_PREFIX-$METHOD-final_view_cells.wrl
Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.env	(revision 1640)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.env	(revision 1642)
@@ -225,5 +225,5 @@
 	Construction {
 
-		samples 100000
+		samples 200000
 
 		# type 0 = sequential computation, 1 = interleaved, 2 = gradient
@@ -248,5 +248,5 @@
 
 		# number of iteration steps
-		levels 4
+		levels 3
 
 		# minimal steps of same type: for interleaved, this is only valid for the first few splits
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1640)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1642)
@@ -2468,7 +2468,6 @@
 	RegisterOption("Hierarchy.Construction.samples",
 					optInt,
-					"vsp_construction_samples=",
+					"hierarchy_construction_samples=",
 					"100000");
-
 
 	RegisterOption("Hierarchy.subdivisionStats",
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1640)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1642)
@@ -427,5 +427,6 @@
 
 	SubdivisionCandidate *ResetViewSpaceSubdivision(const VssRayContainer &rays, 
-													const ObjectContainer &objects);
+													const ObjectContainer &objects,
+													AxisAlignedBox3 *forcedViewSpace);
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1640)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1642)
@@ -311,4 +311,14 @@
 		break;
 	}
+
+	// hack: should be different parameter name
+	if (mUseMultiLevelConstruction)
+	{
+		cout << "starting optimizing multilevel ... " << endl;
+		// try to optimize on the above hierarchy
+		OptimizeMultiLevel(sampleRays, objects, forcedViewSpace);
+		
+		cout << "finished" << endl;
+	}
 }
 
@@ -376,5 +386,5 @@
 	viewSpaceQueue.Push(vsc);
 	
-	// view space subdivision was constructed
+	// view space subdivision started
 	mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
 
@@ -828,13 +838,15 @@
 void HierarchyManager::RunConstruction(const bool repairQueue)
 {
-	//int i = 0;
 	// main loop
 	while (!FinishedConstruction())
 	{
 		SubdivisionCandidate *sc = NextSubdivisionCandidate(mTQueue);    
-			
+		cout << "p=" << sc->GetPriority() << " ";
+
 		////////
 		//-- subdivide leaf node of either type
-		ApplySubdivisionCandidate(sc, mTQueue, repairQueue);
+        ApplySubdivisionCandidate(sc, mTQueue, repairQueue);
+	//cout << "\nhere45 " << sc->GetPriority() << " " << sc->GetRenderCostDecrease() << " " << mObjectSpaceSubdivisionType << " " << mViewSpaceSubdivisionType << endl;
+		
 		DEL_PTR(sc);
 	}
@@ -883,10 +895,4 @@
 																	const ObjectContainer &objects)
 {	
-	// no object space subdivision yet
-	if (!ObjectSpaceSubdivisionConstructed())
-	{
-		return PrepareObjectSpaceSubdivision(sampleRays, objects);
-	}
-
 	SubdivisionCandidate *firstCandidate;
 
@@ -898,13 +904,20 @@
 			cout << "\nreseting bv hierarchy" << endl;
 			Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
-	
+
+			mBvHierarchy->Initialise(objects);
+
+			// rather use this: remove previous nodes and add the two new ones
+			//mHierarchyStats.mNodes -= mBvHierarchy->mBvhStats.nodes + 2;
+			mHierarchyStats.mNodes = 2;
+
 			firstCandidate = mBvHierarchy->Reset(sampleRays, objects);
 		
 			mHierarchyStats.mTotalCost = mBvHierarchy->mTotalCost;
-			
-			// bug!! pvs entries could be much higher at this stage
-			mHierarchyStats.mNodes = 2;
+			mHierarchyStats.mRenderCostDecrease = 0;
+
+			// bug!! pvs entries cab be much higher at this stage
+			// (if view space was already subdivided)
 			mHierarchyStats.mPvsEntries = 1;
-			mHierarchyStats.mRenderCostDecrease = 0;
+			mHierarchyStats.mMemory = sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
 
 			// evaluate stats before first subdivision
@@ -925,5 +938,6 @@
 
 SubdivisionCandidate *HierarchyManager::ResetViewSpaceSubdivision(const VssRayContainer &sampleRays, 
-																  const ObjectContainer &objects)
+																  const ObjectContainer &objects,
+																  AxisAlignedBox3 *forcedViewSpace)
 {
 	ViewCellsManager *vc = mVspTree->mViewCellsManager;
@@ -933,7 +947,9 @@
 	mVspTree = new VspTree();
 	mVspTree->mHierarchyManager = this;
-	mVspTree->mViewCellsManager = vc;
-
-	SubdivisionCandidate *vsc = PrepareViewSpaceSubdivision(sampleRays, objects);
+
+	mVspTree->Initialise(sampleRays, forcedViewSpace);
+	
+	SubdivisionCandidate *vsc = 
+		PrepareViewSpaceSubdivision(sampleRays, objects);
 	
 	mHierarchyStats.mNodes = 2;
@@ -969,10 +985,14 @@
 	// use sah for evaluating osp tree construction 
 	// in the first iteration of the subdivision
-	
+
 	mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
 	mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
 
-	mSavedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
-	mObjectSpaceSubdivisionType = NO_OBJ_SUBDIV;
+	SubdivisionCandidate *osc = 
+		PrepareObjectSpaceSubdivision(sampleRays, objects);
+	mTQueue.Push(osc);
+
+	//////////////////////////7
+
 
 	const int limit = mNumMultiLevels;
@@ -991,5 +1011,6 @@
 
 		// subdivide object space first
-		ResetObjectSpaceSubdivision(sampleRays, objects);
+		osc = ResetObjectSpaceSubdivision(sampleRays, objects);
+		mTQueue.Push(osc);
 
 		// process object space candidates
@@ -1012,5 +1033,5 @@
 		// subdivide view space with respect to the objects
 
-		SubdivisionCandidate *vspVc = ResetViewSpaceSubdivision(sampleRays, objects);
+		SubdivisionCandidate *vspVc = ResetViewSpaceSubdivision(sampleRays, objects, forcedViewSpace);
 		mTQueue.Push(vspVc);
 
@@ -1018,5 +1039,4 @@
 		mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
 		
-
 		// process view space candidates
 		RunConstruction(false);
@@ -1031,9 +1051,4 @@
 	
 	cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-
-/*#if _DEBUG
-	cout << "view space: " << GetViewSpaceBox() << endl;
-	cout << "object space:  " << GetObjectSpaceBox() << endl;
-#endif*/
 
 	mHierarchyStats.Stop();
@@ -1047,29 +1062,30 @@
 										  AxisAlignedBox3 *forcedViewSpace)
 {
-	mHierarchyStats.Reset();
-	mHierarchyStats.Start();
-	mHierarchyStats.mNodes = 2;
-	
-	mHierarchyStats.mTotalCost = (float)objects.size();
-	Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
+	// assume object space subdivision constructed
+	//mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
+
+	// set the number of leaves 'evaluated' from the previous methods
+	// we go for the same numbers, but we try to optimize both
+	// subdivisions
+	switch (mObjectSpaceSubdivisionType)
+	{
+	case BV_BASED_OBJ_SUBDIV:
+		mBvHierarchy->mTermMaxLeaves = mBvHierarchy->mBvhStats.Leaves();
+		break;
+	case KD_BASED_OBJ_SUBDIV:
+		mOspTree->mTermMaxLeaves = mOspTree->mOspStats.Leaves();
+	default:
+		break;
+	}
+	
+	mVspTree->mMaxViewCells = mVspTree->mVspStats.Leaves();
 
 	const long startTime = GetTime();
-	cout << "Constructing view space / object space tree ... \n";
-	
-	// initialise view / object space
-	mVspTree->Initialise(sampleRays, forcedViewSpace);
-	InitialiseObjectSpaceSubdivision(objects);
-
-	// use sah for evaluating osp tree construction 
-	// in the first iteration of the subdivision
-	
-	mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
-	mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
-
-	mSavedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
-	mObjectSpaceSubdivisionType = NO_OBJ_SUBDIV;
-
 	const int limit = mNumMultiLevels;
-	int i = 0;
+
+	// open up new subdivision
+	mSubdivisionStats.close();
+
+	int steps = 0;
 
 	// This method subdivides view space / object space 
@@ -1081,24 +1097,22 @@
 	{
 		char subdivisionStatsLog[100];
-		sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", i);
+		sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", steps);
 		mSubdivisionStats.open(subdivisionStatsLog);
 
 		// subdivide object space first
-		ResetObjectSpaceSubdivision(sampleRays, objects);
+		SubdivisionCandidate *ospVc = ResetObjectSpaceSubdivision(sampleRays, objects);
+		mTQueue.Push(ospVc);
 
 		// process object space candidates
 		RunConstruction(false);
 
-		// object space subdivision constructed
-		mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
-
-		cout << "iteration " << i << " of " << limit << " finished" << endl;
+		cout << "iteration " << steps << " of " << limit << " finished" << endl;
 
 		mSubdivisionStats.close();
 
-		if ((i ++) >= limit)
+		if ((++ steps) >= limit)
 			break;
 
-		sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", i);
+		sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", steps);
 		mSubdivisionStats.open(subdivisionStatsLog);
 
@@ -1106,28 +1120,19 @@
 		// subdivide view space with respect to the objects
 
-		SubdivisionCandidate *vspVc = ResetViewSpaceSubdivision(sampleRays, objects);
+		SubdivisionCandidate *vspVc = ResetViewSpaceSubdivision(sampleRays, objects, forcedViewSpace);
 		mTQueue.Push(vspVc);
-
-		// view space subdivision constructed
-		mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
-		
 
 		// process view space candidates
 		RunConstruction(false);
 
-		cout << "iteration " << i << " of " << limit << " finished" << endl;
+		cout << "iteration " << steps << " of " << limit << " finished" << endl;
 
 		mSubdivisionStats.close();
 
-		if ((i ++) >= limit)
+		if ((++ steps) >= limit)
 			break;
 	}
 	
 	cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-
-/*#if _DEBUG
-	cout << "view space: " << GetViewSpaceBox() << endl;
-	cout << "object space:  " << GetObjectSpaceBox() << endl;
-#endif*/
 
 	mHierarchyStats.Stop();
@@ -1146,5 +1151,5 @@
 bool HierarchyManager::ObjectSpaceSubdivisionConstructed() const
 {
-	switch (mObjectSpaceSubdivisionType)
+	/*switch (mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
@@ -1154,5 +1159,6 @@
 	default:
 		return false;
-	}
+	}*/
+	return mObjectSpaceSubdivisionType != NO_OBJ_SUBDIV;
 }
 
