Index: GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1305)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1313)
@@ -57,8 +57,7 @@
 };
 
+typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
+typedef vector<SubdivisionCandidate *> SubdivisionCandidateContainer;
 
-typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
-
-typedef vector<SubdivisionCandidate *> SubdivisionCandidateContainer;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1311)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1313)
@@ -68,10 +68,10 @@
 	/// total number of nodes
 	int nodes;
-		
 	/// maximal reached depth
 	int maxDepth;
-	
 	/// accumulated depth
 	int accumDepth;
+
+	float repairTime;
 
 	// Constructor
@@ -87,5 +87,4 @@
 	// TODO: computation wrong
 	double AvgDepth() const { return accumDepth / (double)Leaves();}
-	
 
 	void Reset() 
@@ -94,6 +93,6 @@
 		maxDepth = 0;
 		accumDepth = 0;
-	}
-
+		repairTime = 0;
+	}
 
 	void Print(ostream &app) const;
@@ -169,5 +168,5 @@
 	/** The type of object space subdivison
 	*/
-	int HierarchyManager::GetObjectSpaceSubdivisionType() const
+	int GetObjectSpaceSubdivisionType() const
 	{
 		return mObjectSpaceSubdivisionType;
@@ -187,5 +186,5 @@
 		float &contribution) const;
 
-	void PrintObjectSpaceHierarchyStatistics(ofstream &stream) const;
+	void PrintHierarchyStatistics(ofstream &stream) const;
 
 	VspTree *GetVspTree() { return mVspTree; }
@@ -259,4 +258,8 @@
 		AxisAlignedBox3 *forcedViewSpace);
 
+	bool ObjectSpaceSubdivisionConstructed() const;
+
+    void ResetQueue();
+
 
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1308)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1313)
@@ -451,5 +451,5 @@
 	splitCandidate.SetRenderCostDecrease(renderCostDecr);
 
-#if 1
+#if 0
 	const float priority = (float)-splitCandidate.mParentData.mDepth;
 #else	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1311)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1313)
@@ -4814,6 +4814,5 @@
 
 	// print subdivision statistics
-	Debug << mHierarchyManager->GetVspTree()->GetStatistics() << endl;
-	mHierarchyManager->PrintObjectSpaceHierarchyStatistics(Debug);
+	mHierarchyManager->PrintHierarchyStatistics(Debug);
 
 	if (0)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1308)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1313)
@@ -749,5 +749,5 @@
 	splitCandidate.SetRenderCostDecrease(renderCostDecr);
 
-#if 1
+#if 0
 	const float priority = (float)-splitCandidate.mParentData.mDepth;
 #else
@@ -2677,5 +2677,5 @@
 	if (!obj) return;
 
-	switch (mHierarchyManager->GetObjectSpaceSubdivisonType())
+	switch (mHierarchyManager->GetObjectSpaceSubdivisionType())
 	{
 	case HierarchyManager::KD_BASED_OBJ_SUBDIV:
@@ -2746,5 +2746,5 @@
 	int pvs = 0;
 
-	switch (mHierarchyManager->GetObjectSpaceSubdivisonType())
+	switch (mHierarchyManager->GetObjectSpaceSubdivisionType())
 	{
 	case HierarchyManager::NO_OBJ_SUBDIV:
@@ -2791,5 +2791,5 @@
 		return 0;
 
-	switch (mHierarchyManager->GetObjectSpaceSubdivisonType())
+	switch (mHierarchyManager->GetObjectSpaceSubdivisionType())
 	{
 	case HierarchyManager::NO_OBJ_SUBDIV:
@@ -2844,5 +2844,5 @@
 	int pvs = 0;
 
-	switch (mHierarchyManager->GetObjectSpaceSubdivisonType())
+	switch (mHierarchyManager->GetObjectSpaceSubdivisionType())
 	{
 	case HierarchyManager::NO_OBJ_SUBDIV:
@@ -2895,5 +2895,5 @@
 	if (!obj) return;
 
-	switch (mHierarchyManager->GetObjectSpaceSubdivisonType())
+	switch (mHierarchyManager->GetObjectSpaceSubdivisionType())
 	{
 		case HierarchyManager::NO_OBJ_SUBDIV:
@@ -2932,5 +2932,5 @@
 	int pvs = 0;
 
-	switch(mHierarchyManager->GetObjectSpaceSubdivisonType())
+	switch(mHierarchyManager->GetObjectSpaceSubdivisionType())
 	{
 	case HierarchyManager::NO_OBJ_SUBDIV:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 1297)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 1313)
@@ -76,4 +76,5 @@
 	std::vector<T> mBuffer;
 };
+
 
 /*****************************************************************************/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1311)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1313)
@@ -214,4 +214,5 @@
 	
 	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+	mHierarchyStats.Stop();
 	mVspTree->mVspStats.Stop();
 }
@@ -320,10 +321,8 @@
 {
 	const bool ospDepthReached = 
-		(mMinDepthForObjectSpaceSubdivion == mVspTree->mVspStats.maxDepth);
+		(mMinDepthForObjectSpaceSubdivion <= mVspTree->mVspStats.maxDepth);
 	const bool stillNotOsp = (mObjectSpaceSubdivisionType == NO_OBJ_SUBDIV);
-	
-	Debug << "here10 " << mMinDepthForObjectSpaceSubdivion << " " << mVspTree->mVspStats.maxDepth << endl;
     
-	return stillNotOsp && (mTQueue.Empty() || ospDepthReached);
+	return stillNotOsp && (mTQueue.Empty() || ((mConstructionType == 1) && ospDepthReached));
 }
 
@@ -335,28 +334,26 @@
 	mHierarchyStats.nodes = 0;
 	mGlobalCostMisses = 0;
-Debug << "here2" << endl;
+
 	// use objects for evaluating vsp tree construction in the first levels
 	// of the subdivision
 	const int savedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
 	mObjectSpaceSubdivisionType = NO_OBJ_SUBDIV;
-Debug << "here3" << endl;
+
 	// start with view space subdivison: prepare vsp tree for traversal
 	PrepareViewSpaceSubdivision(sampleRays, objects, forcedViewSpace);
 
-	const bool repairQueue = false;
-	//const bool repairQueue = true;
-
-	while (!FinishedConstruction());
-	{Debug << "here7" << endl;
-		// we use objects for evaluating vsp tree construction until 
-		// a certain depth once a certain depth existiert ....
-		if (StartObjectSpaceSubdivision())
-		{
-			Debug << "here8" << endl;
-			mObjectSpaceSubdivisionType = savedObjectSpaceSubdivisionType;
-			PrepareObjectSpaceSubdivision(sampleRays, objects);
-			Debug << "here9" << endl;
-		}
-
+	//const bool repairQueue = false;
+	const bool repairQueue = true;
+cout << "here55 " << mVspTree->mVspStats.maxDepth << " (" << mMinDepthForObjectSpaceSubdivion << ") " << endl;
+	// start object space subdivision immediately?
+	if (StartObjectSpaceSubdivision())
+	{cout << "here1155 ";
+		mObjectSpaceSubdivisionType = savedObjectSpaceSubdivisionType;
+		PrepareObjectSpaceSubdivision(sampleRays, objects);
+	}
+
+	int i = 0;
+	while (!FinishedConstruction())
+	{
 		mCurrentCandidate = NextSubdivisionCandidate();    
 		mTotalCost -= mCurrentCandidate->GetRenderCostDecrease();
@@ -370,8 +367,9 @@
 			++ mGlobalCostMisses;
 		}
-
+		
 		//-- subdivide leaf node
 		if (ApplySubdivisionCandidate(mCurrentCandidate))
 		{
+			cout << "subdividing candidate " << ++ i << " of type " << mCurrentCandidate->Type() << endl;
 			mHierarchyStats.nodes += 2;
 
@@ -384,8 +382,21 @@
 		}
 
+		// we use objects for evaluating vsp tree construction until 
+		// a certain depth once a certain depth existiert ....
+		if (StartObjectSpaceSubdivision())
+		{
+			cout << "reseting queue ... ";
+			ResetQueue();
+			cout << "finished" << endl;
+
+			cout << "starting object space subdivision at maximal view space subdivison depth " << mVspTree->mVspStats.maxDepth << " (" << mMinDepthForObjectSpaceSubdivion << ") " << endl;
+						
+			mObjectSpaceSubdivisionType = savedObjectSpaceSubdivisionType;
+			PrepareObjectSpaceSubdivision(sampleRays, objects);
+		}
+
 		DEL_PTR(mCurrentCandidate);
 	}
 	
-
 	mObjectSpaceSubdivisionType = savedObjectSpaceSubdivisionType;
 }
@@ -398,14 +409,5 @@
 
 
-int HierarchyManager::GetObjectSpaceSubdivisionType()
-{
-	if (!ObjectSpaceSubdivisionConstructed())
-		return NO_OBJECT_SUBDIV;
-
-	return mObjectSpaceSubdivisionType;
-}
-
-
-bool HierarchyManager::ObjectSpaceSubdivisionConstructed()
+bool HierarchyManager::ObjectSpaceSubdivisionConstructed() const
 {
 	switch (mObjectSpaceSubdivisionType)
@@ -489,10 +491,12 @@
 
 	// collect list of "dirty" candidates
+	long startTime = GetTime();
+   
 	vector<SubdivisionCandidate *> dirtyList;
 	CollectDirtyCandidates(dirtyList);
-	Debug << "collected " << (int)dirtyList.size() << " candidates for reevaluation" << endl;
-
+	cout << "repairing " << (int)dirtyList.size() << " candidates ... ";
+	
 	//-- reevaluate the dirty list
-	vector<SubdivisionCandidate *>::const_iterator sit, sit_end = dirtyList.end();
+	SubdivisionCandidateContainer::const_iterator sit, sit_end = dirtyList.end();
 	
 	for (sit = dirtyList.begin(); sit != sit_end; ++ sit)
@@ -515,4 +519,33 @@
 		mTQueue.Push(sc); // reinsert
 	}
+
+	long endTime = GetTime();
+	Real timeDiff = TimeDiff(startTime, endTime);
+
+	mHierarchyStats.repairTime += timeDiff;
+
+	cout << "finished in " << timeDiff * 1e-3f << " secs" << endl;
+}
+
+
+void HierarchyManager::ResetQueue()
+{
+	SubdivisionCandidateContainer mCandidateBuffer;
+
+	// remove from queue
+	while (!mTQueue.Empty())
+	{
+		SubdivisionCandidate *candidate = NextSubdivisionCandidate();
+		candidate->EvalPriority(); // reevaluate
+		cout << ".";
+		mCandidateBuffer.push_back(candidate);
+	}
+
+	// put back into queue
+	SubdivisionCandidateContainer::const_iterator sit, sit_end = mCandidateBuffer.end();
+    for (sit = mCandidateBuffer.begin(); sit != sit_end; ++ sit)
+	{cout << ":";
+		mTQueue.Push(*sit);
+	}
 }
 
@@ -567,16 +600,21 @@
 
 
-void HierarchyManager::PrintObjectSpaceHierarchyStatistics(ofstream &stream) const
-{
+void HierarchyManager::PrintHierarchyStatistics(ofstream &stream) const
+{
+	stream << mHierarchyStats << endl;
+
+	stream << "\nview space:" << endl << endl;
+	stream << mVspTree->GetStatistics() << endl;
+	stream << "\nobject space:" << endl << endl;
 	switch (mObjectSpaceSubdivisionType)
 	{
 	case KD_BASED_OBJ_SUBDIV:
 		{
-			stream << mOspTree->GetStatistics();
+			stream << mOspTree->GetStatistics() << endl;
 			break;
 		}
 	case BV_BASED_OBJ_SUBDIV:
 		{
-			stream << mBvHierarchy->GetStatistics();
+			stream << mBvHierarchy->GetStatistics() << endl;
 			break;
 		}
@@ -626,3 +664,25 @@
 
 
-}
+void HierarchyStatistics::Print(ostream &app) const
+{
+	app << "=========== Hierarchy statistics ===============\n";
+
+	app << setprecision(4);
+
+	app << "#N_CTIME  ( Construction time [s] )\n" << Time() << " \n";
+	
+	app << "#N_RTIME  ( Repair time [s] )\n" << repairTime * 1e-3f << " \n";
+
+	app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
+
+	app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n";
+
+	app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
+
+	app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth << endl;
+	
+	app << "========== END OF Hierarchy statistics ==========\n";
+}
+
+
+}
