Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1614)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1624)
@@ -50,17 +50,22 @@
 public:
 	/// total number of entries in the pvs
-	int pvsEntries;
+	int mPvsEntries;
 	/// storage cost
-	int memory;
+	int mMemory;
 	/// total number of nodes
-	int nodes;
+	int mNodes;
 	/// maximal reached depth
-	int maxDepth;
+	int mMaxDepth;
 	/// accumulated depth
-	int accumDepth;
+	int mAccumDepth;
 	/// time spent for queue repair
-	float repairTime;
+	float mRepairTime;
+
 	// global cost ratio violations
 	int mGlobalCostMisses;
+	/// total cost of subdivision
+	float mTotalCost;
+	/// render cost decrease of subdivision
+	float mRenderCostDecrease;
 
 	// Constructor
@@ -70,20 +75,23 @@
 	}
 
-	int Nodes() const {return nodes;}
-	int Interior() const { return nodes / 2; }
-	int Leaves() const { return (nodes / 2) + 1; }
+	int Nodes() const {return mNodes;}
+	int Interior() const { return mNodes / 2; }
+	int Leaves() const { return (mNodes / 2) + 1; }
 	
 	// TODO: computation wrong
-	double AvgDepth() const { return accumDepth / (double)Leaves();}
+	double AvgDepth() const { return mAccumDepth / (double)Leaves();}
 
 	void Reset()
 	{
 		mGlobalCostMisses = 0;
-		nodes = 0;
-		maxDepth = 0;
-		accumDepth = 0;
-		repairTime = 0;
-		memory = 0;
-		pvsEntries = 0;
+		mTotalCost = 0;
+		mRenderCostDecrease = 0;
+
+		mNodes = 0;
+		mMaxDepth = 0;
+		mAccumDepth = 0;
+		mRepairTime = 0;
+		mMemory = 0;
+		mPvsEntries = 0;
 	}
 
@@ -201,4 +209,5 @@
 	*/
 	//AxisAlignedBox3 GetViewSpaceBox() const;
+
 	/** Returns object space bounding box.
 	*/
@@ -257,14 +266,16 @@
 	/** Repairs the dirty entries of the candidate queue.
 	*/
-	void RepairQueue();
-
-	/** Collect the list of dirty candidates after the current subdivision candidate
-		split.
-	*/
-	void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
+	void RepairQueue(SubdivisionCandidate *sc);
+
+	/** Collect the list of dirty candidates after the current 
+		subdivision candidate split.
+	*/
+	void CollectDirtyCandidates(
+		SubdivisionCandidate *sc,
+		vector<SubdivisionCandidate *> &dirtyList);
 
 	/** Evaluate subdivision stats for log.
 	*/
-	void EvalSubdivisionStats(const float renderCostDecr);
+	void EvalSubdivisionStats();
 
 	void AddSubdivisionStats(
@@ -274,15 +285,20 @@
 		const int totalPvsEntries);
 
-	void CollectObjectSpaceDirtyList();
-	void CollectViewSpaceDirtyList();
-
-	bool AddSampleToPvs(Intersectable *obj, 
-						const float pdf,
-						float &contribution) const;
-
-	void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
-	void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
+	bool AddSampleToPvs(
+		Intersectable *obj, 
+		const float pdf,
+		float &contribution) const;
+
+	void CollectViewSpaceDirtyList(
+		SubdivisionCandidate *sc,
+		SubdivisionCandidateContainer &dirtyList);
+
+	void CollectObjectSpaceDirtyList(
+		SubdivisionCandidate *sc,
+		SubdivisionCandidateContainer &dirtyList);
 		
-	void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
+	void ExportOspTree(
+		Exporter *exporter, 
+		const ObjectContainer &objects) const;
 
 	void ParseEnvironment();
@@ -313,4 +329,9 @@
 
 	void ConstructInterleaved(
+		const VssRayContainer &sampleRays,
+		const ObjectContainer &objects,
+		AxisAlignedBox3 *forcedViewSpace);
+
+	void ConstructInterleaved2(
 		const VssRayContainer &sampleRays,
 		const ObjectContainer &objects,
@@ -359,5 +380,5 @@
 	int mSavedViewSpaceSubdivisionType;
 	/// the current subdivision candidate
-	SubdivisionCandidate *mCurrentCandidate;
+	//SubdivisionCandidate *mCurrentCandidate;
 
 
@@ -389,5 +410,5 @@
 
 	/// keeps track of cost during subdivision
-	float mTotalCost;
+	//float mTotalCost;
 	/// statistics about the hierarchy
 	HierarchyStatistics mHierarchyStats;
@@ -396,4 +417,6 @@
 	int mMinDepthForViewSpaceSubdivion;
 	
+	int mMinRenderCostDecrease;
+
 	ofstream mSubdivisionStats;
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1614)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1624)
@@ -932,5 +932,6 @@
 	const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();
 
-	SortableEntryContainer::const_iterator backObjectsStart = mSubdivisionCandidates->begin();
+	SortableEntryContainer::const_iterator backObjectsStart = 
+		mSubdivisionCandidates->begin();
 
 	/////////////////////////////////
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1614)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1624)
@@ -109,4 +109,10 @@
 		"Hierarchy.Construction.levels", mNumMultiLevels);
 
+	char subdivisionStatsLog[100];
+	Environment::GetSingleton()->GetStringValue("Hierarchy.subdivisionStats", subdivisionStatsLog);
+	mSubdivisionStats.open(subdivisionStatsLog);
+
+	mMinRenderCostDecrease = -1;
+
 	Debug << "******** Hierachy Manager Parameters ***********" << endl;
 	Debug << "max leaves: " << mTermMaxLeaves << endl;
@@ -117,4 +123,5 @@
 	Debug << "repair queue: " << mRepairQueue << endl;
 	Debug << "number of multilevels: " << mNumMultiLevels << endl;
+	Debug << "min render cost " << mMinRenderCostDecrease << endl;
 	Debug << endl;
 }
@@ -198,11 +205,11 @@
 
 
-void HierarchyManager::EvalSubdivisionStats(const float renderCostDecr)
+void HierarchyManager::EvalSubdivisionStats()
 {
 	//cout << "pvs entries " << mHierarchyStats.pvsEntries << endl;
 	AddSubdivisionStats(mHierarchyStats.Leaves(),
-						renderCostDecr,
-						mTotalCost,
-						mHierarchyStats.pvsEntries
+						mHierarchyStats.mRenderCostDecrease,
+						mHierarchyStats.mTotalCost,
+						mHierarchyStats.mPvsEntries
 						);
 }
@@ -229,4 +236,5 @@
 		|| (mHierarchyStats.mGlobalCostMisses >= mTermGlobalCostMissTolerance)
 		|| (candidate->GlobalTerminationCriteriaMet())
+		|| (mHierarchyStats.mRenderCostDecrease < mMinRenderCostDecrease)
 		);
 
@@ -254,12 +262,102 @@
 	else
 	{
+		ConstructInterleaved(sampleRays, objects, forcedViewSpace);
+	}
+}
+
+
+void HierarchyManager::ConstructInterleaved2(const VssRayContainer &sampleRays,											 
+											 const ObjectContainer &objects,
+											 AxisAlignedBox3 *forcedViewSpace)
+{
+	mHierarchyStats.Reset();
+	mHierarchyStats.Start();
+	mHierarchyStats.mNodes = 2;
+	
+	mHierarchyStats.mTotalCost = (float)objects.size();
+	Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
+
+	const long startTime = GetTime();
+	cout << "Constructing view space / object space tree ... \n";
+	
+	// compute view space bounding box
+	mVspTree->ComputeBoundingBox(sampleRays, forcedViewSpace);
+
+	// 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;
+
+	// This method subdivides view space / object space 
+	// in order to converge to some optimal cost for this partition
+	// start with object space partiton
+	// then optimizate view space partition for the current osp
+	// and vice versa until iteration depth is reached.
+	while (1)
+	{
 		char subdivisionStatsLog[100];
-		Environment::GetSingleton()->GetStringValue("Hierarchy.subdivisionStats", 
-		subdivisionStatsLog);
+		sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", i);
 		mSubdivisionStats.open(subdivisionStatsLog);
 
-		ConstructInterleaved(sampleRays, objects, forcedViewSpace);
-	}
-}
+		float oldRenderCost = mHierarchyStats.mRenderCostDecrease;
+		const bool isObjectSpace = true;
+
+		// subdivide object space first
+		ResetObjectSpaceSubdivision(sampleRays, objects);
+
+		// process object space candidates using 
+		// render queue for objects
+		RunConstruction(false, isObjectSpace, oldRenderCost);
+	
+		oldRenderCost = mHierarchyStats.mRenderCostDecrease;
+	
+		// object space subdivision constructed
+		mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
+
+		cout << "iteration " << i << " of " << limit << " finished" << endl;
+
+		mSubdivisionStats.close();
+		
+		sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", i);
+		mSubdivisionStats.open(subdivisionStatsLog);
+
+		/////////////////
+		// subdivide view space with respect to the objects
+
+		ResetViewSpaceSubdivision(sampleRays, objects);
+		
+		// process view space candidates
+		RunConstruction(false, !isObjectSpace, oldRenderCost);
+
+		// view space subdivision constructed
+		mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
+	
+		cout << "iteration " << i << " of " << limit << " finished" << endl;
+
+		mSubdivisionStats.close();
+
+		if ((i ++) >= 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();
+	mVspTree->mVspStats.Stop();
+	FinishObjectSpaceSubdivision(objects);
+}
+
 
 
@@ -270,9 +368,11 @@
 	mHierarchyStats.Reset();
 	mHierarchyStats.Start();
-	mHierarchyStats.nodes = 2; // two nodes for view space and object space
-
-	mTotalCost = (float)objects.size();
-	EvalSubdivisionStats(0);
-	Debug << "setting total cost to " << mTotalCost << endl;
+	mHierarchyStats.mNodes = 2; // two nodes for view space and object space
+
+	mHierarchyStats.mTotalCost = (float)objects.size();
+	mHierarchyStats.mRenderCostDecrease = 0;
+
+	EvalSubdivisionStats();
+	Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
 
 	const long startTime = GetTime();
@@ -290,7 +390,8 @@
 	mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
 
-	// start with view space subdivison: prepare vsp tree for traversal
+	// start with view space subdivison immediately?
 	if (StartViewSpaceSubdivision())
 	{
+		// prepare vsp tree for traversal
 		mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
 		PrepareViewSpaceSubdivision(sampleRays, objects);
@@ -304,5 +405,5 @@
 	}
 
-	// process object space candidates
+	// begin subdivision
 	RunConstruction(mRepairQueue, sampleRays, objects, forcedViewSpace);
 	
@@ -329,6 +430,6 @@
 		mVspTree->PrepareConstruction(sampleRays, *viewSpaceRays);
 
-	mTotalCost = mVspTree->mTotalCost;
-	cout << "\nreseting cost for vsp, new total cost: " << mTotalCost << endl;
+	mHierarchyStats.mTotalCost = mVspTree->mTotalCost;
+	cout << "\nreseting cost for vsp, new total cost: " << mHierarchyStats.mTotalCost << endl;
 
 	mTQueue.Push(vsc);
@@ -363,6 +464,6 @@
 		mBvHierarchy->PrepareConstruction(sampleRays, objects);
 
-	mTotalCost = mBvHierarchy->mTotalCost;
-	Debug << "\nreseting cost, new total cost: " << mTotalCost << endl;
+	mHierarchyStats.mTotalCost = mBvHierarchy->mTotalCost;
+	Debug << "\nreseting cost, new total cost: " << mHierarchyStats.mTotalCost << endl;
 
     mTQueue.Push(sc);
@@ -386,6 +487,6 @@
 		mOspTree->PrepareConstruction(sampleRays, objects, *objectSpaceRays);
 
-	mTotalCost = mOspTree->mTotalCost;
-	Debug << "\nreseting cost, new total cost: " << mTotalCost << endl;
+	mHierarchyStats.mTotalCost = mOspTree->mTotalCost;
+	Debug << "\nreseting cost, new total cost: " << mHierarchyStats.mTotalCost << endl;
 	
     mTQueue.Push(osc);
@@ -393,8 +494,9 @@
 
 
-bool HierarchyManager::ApplySubdivisionCandidate(SubdivisionCandidate *sc, const bool repairQueue)
+bool HierarchyManager::ApplySubdivisionCandidate(SubdivisionCandidate *sc, 
+												 const bool repairQueue)
 {
 	// test if global termination criteria were met before this split
-	const bool globalTerminationCriteriaMet = GlobalTerminationCriteriaMet(sc);
+	const bool terminationCriteriaMet = GlobalTerminationCriteriaMet(sc);
 	const bool vspSplit = (sc->Type() == SubdivisionCandidate::VIEW_SPACE);
 
@@ -405,6 +507,7 @@
 	case SubdivisionCandidate::VIEW_SPACE:
 		{
-			VspNode *n = mVspTree->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
-			success = !n->IsLeaf(); // local or global termination criteria failed?
+			VspNode *n = mVspTree->Subdivide(mTQueue, sc, terminationCriteriaMet);
+			// check if termination criteria failed
+			success = !n->IsLeaf();
 		}
 		break;
@@ -413,5 +516,5 @@
 			if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
 			{
-				KdNode *n = mOspTree->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
+				KdNode *n = mOspTree->Subdivide(mTQueue, sc, terminationCriteriaMet);
 				// local or global termination criteria failed
 				success = !n->IsLeaf();
@@ -419,5 +522,5 @@
 			else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
 			{
-				BvhNode *n = mBvHierarchy->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
+				BvhNode *n = mBvHierarchy->Subdivide(mTQueue, sc, terminationCriteriaMet);
 				// local or global termination criteria failed
 				success = !n->IsLeaf();					
@@ -435,9 +538,8 @@
 
 	///////////////
-	//-- split was successful => 
-	//-- update stats and render queue
+	//-- split was successful => update stats and queue
 
     // cost ratio of cost decrease / totalCost
-	const float costRatio = mCurrentCandidate->GetRenderCostDecrease() / mTotalCost;
+	const float costRatio = sc->GetRenderCostDecrease() / mHierarchyStats.mTotalCost;
 	//Debug << "ratio: " << costRatio << " min ratio: " << mTermMinGlobalCostRatio << endl;
 	
@@ -447,25 +549,27 @@
 	}
 	
-	mTotalCost -= mCurrentCandidate->GetRenderCostDecrease();
+	mHierarchyStats.mTotalCost -= sc->GetRenderCostDecrease();
 	
 	cout << sc->Type() << " ";
 		
 	// update stats
-	mHierarchyStats.nodes += 2;
+	mHierarchyStats.mNodes += 2;
 	
 	const int pvsEntries = sc->GetPvsEntriesIncr();
-	mHierarchyStats.pvsEntries += pvsEntries;
+	mHierarchyStats.mPvsEntries += pvsEntries;
 	
 	//cout << "pvs entries: " << pvsEntries << " " << mHierarchyStats.pvsEntries << endl;
-	mHierarchyStats.memory += 0; // TODO
-	
+	mHierarchyStats.mMemory += 0; // TODO
+	mHierarchyStats.mRenderCostDecrease = sc->GetRenderCostDecrease();
+
 	// subdivision successful
-	EvalSubdivisionStats(sc->GetRenderCostDecrease());
+	EvalSubdivisionStats();
 		
-	// reevaluate candidates affected by the split for view space splits, 
-	// this would be object space splits and other way round
 	if (repairQueue) 
-	{	
-		RepairQueue();
+	{
+		// reevaluate candidates affected by the split for view space splits, 
+		// this would be object space splits and other way round
+
+		RepairQueue(sc);
 	}
 
@@ -541,10 +645,10 @@
 	while (!FinishedConstruction())
 	{
-		mCurrentCandidate = NextSubdivisionCandidate();    
+		SubdivisionCandidate *sc = NextSubdivisionCandidate();    
 			
 		///////////////////
 		//-- subdivide leaf node
 
-		ApplySubdivisionCandidate(mCurrentCandidate, repairQueue);
+		ApplySubdivisionCandidate(sc, repairQueue);
 				
 		// we use objects for evaluating vsp tree construction until 
@@ -580,23 +684,40 @@
 		}
 
-		DEL_PTR(mCurrentCandidate);
-	}
-}
-
+		DEL_PTR(sc);
+	}
+}
 
 
 void HierarchyManager::RunConstruction(const bool repairQueue)
 {
-	int i = 0;
+	//int i = 0;
 	// main loop
 	while (!FinishedConstruction())
 	{
-		mCurrentCandidate = NextSubdivisionCandidate();    
+		SubdivisionCandidate *sc = NextSubdivisionCandidate();    
 			
-		///////
-		//-- subdivide leaf node
-
-		ApplySubdivisionCandidate(mCurrentCandidate, repairQueue);
-		DEL_PTR(mCurrentCandidate);
+		////////
+		//-- subdivide leaf node of either type
+		ApplySubdivisionCandidate(sc, repairQueue);
+
+		DEL_PTR(sc);
+	}
+}
+
+
+void HierarchyManager::RunConstruction(const bool isObjectSpace,
+									   const float oldRenderCostDecr)
+{
+	//int i = 0;
+	// main loop
+	while (!FinishedConstruction())
+	{
+		SubdivisionCandidate *sc = NextSubdivisionCandidate();    
+			
+		////////
+		//-- subdivide leaf node of either type
+		ApplySubdivisionCandidate(sc, repairQueue);
+
+		DEL_PTR(sc);
 	}
 }
@@ -605,14 +726,6 @@
 void HierarchyManager::ResetObjectSpaceSubdivision(const VssRayContainer &sampleRays, 
 												   const ObjectContainer &objects)
+												   //const bool startFromScratch)
 {	
-#if 0	
-		DEL_PTR(mBvHierarchy);
-		mBvHierarchy = new BvHierarchy();
-		mBvHierarchy->mHierarchyManager = this;
-
-		PrepareObjectSpaceSubdivision(sampleRays, objects);
-		return;
-#else
-	
 	// no object space subdivision yet
 	if (!ObjectSpaceSubdivisionConstructed())
@@ -629,13 +742,21 @@
 			Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
 	
-			SubdivisionCandidate *firstCandidate = 
-				mBvHierarchy->Reset(sampleRays, objects);
-
-			mTQueue.Push(firstCandidate);
-			mTotalCost = mBvHierarchy->mTotalCost;
-
-			mHierarchyStats.nodes = 2;
-			mHierarchyStats.pvsEntries = 0;
-			EvalSubdivisionStats(0);
+			//if (startFromScratch)
+			//{
+				SubdivisionCandidate *firstCandidate = 
+					mBvHierarchy->Reset(sampleRays, objects);
+
+				mTQueue.Push(firstCandidate);
+				mHierarchyStats.mTotalCost = mBvHierarchy->mTotalCost;
+
+				mHierarchyStats.mNodes = 2;
+				mHierarchyStats.mPvsEntries = 0;
+				mHierarchyStats.mRenderCostDecrease = 0;
+
+				EvalSubdivisionStats();
+			/*}
+			else
+			{
+			}*/
 		}
 		break;
@@ -647,5 +768,4 @@
 		break;
 	}
-#endif
 }
 
@@ -656,4 +776,5 @@
 	ViewCellsManager *vc = mVspTree->mViewCellsManager;
 
+	// HACK: rather not destroy vsp tree
 	DEL_PTR(mVspTree);
 	mVspTree = new VspTree();
@@ -663,8 +784,9 @@
 	PrepareViewSpaceSubdivision(sampleRays, objects);
 	
-	mHierarchyStats.nodes = 2;
-	mHierarchyStats.pvsEntries = 0;
-	EvalSubdivisionStats(0);
-
+	mHierarchyStats.mNodes = 2;
+	mHierarchyStats.mPvsEntries = 0;
+	mHierarchyStats.mRenderCostDecrease = 0;
+
+	EvalSubdivisionStats();
 }
 
@@ -676,8 +798,8 @@
 	mHierarchyStats.Reset();
 	mHierarchyStats.Start();
-	mHierarchyStats.nodes = 2;
-	
-	mTotalCost = (float)objects.size();
-	Debug << "setting total cost to " << mTotalCost << endl;
+	mHierarchyStats.mNodes = 2;
+	
+	mHierarchyStats.mTotalCost = (float)objects.size();
+	Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
 
 	const long startTime = GetTime();
@@ -699,5 +821,6 @@
 	int i = 0;
 
-	// render cost optimization
+	// This method subdivides view space / object space 
+	// in order to converge to some optimal cost for this partition
 	// start with object space partiton
 	// then optimizate view space partition for the current osp
@@ -709,6 +832,8 @@
 		mSubdivisionStats.open(subdivisionStatsLog);
 
-		// fsubdivide object space first
+		// subdivide object space first
+
 		ResetObjectSpaceSubdivision(sampleRays, objects);
+
 		// process object space candidates
 		RunConstruction(false);
@@ -730,6 +855,8 @@
 
 		ResetViewSpaceSubdivision(sampleRays, objects);
+
 		// process view space candidates
 		RunConstruction(false);
+
 		// view space subdivision constructed
 		mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
@@ -782,5 +909,6 @@
 
 
-void HierarchyManager::CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
+void HierarchyManager::CollectObjectSpaceDirtyList(SubdivisionCandidate *sc,
+												   SubdivisionCandidateContainer &dirtyList)
 {
 	switch (mObjectSpaceSubdivisionType)
@@ -788,16 +916,16 @@
 	case KD_BASED_OBJ_SUBDIV:
 		{
-			OspTree::OspSubdivisionCandidate *sc = 
-				dynamic_cast<OspTree::OspSubdivisionCandidate *>(mCurrentCandidate);
-
-			mOspTree->CollectDirtyCandidates(sc, dirtyList);
+			OspTree::OspSubdivisionCandidate *ospSc = 
+				dynamic_cast<OspTree::OspSubdivisionCandidate *>(sc);
+
+			mOspTree->CollectDirtyCandidates(ospSc, dirtyList);
 			break;
 		}
 	case BV_BASED_OBJ_SUBDIV:
 		{
-			BvHierarchy::BvhSubdivisionCandidate *sc = 
-				dynamic_cast<BvHierarchy::BvhSubdivisionCandidate *>(mCurrentCandidate);
-
-			mBvHierarchy->CollectDirtyCandidates(sc, dirtyList);
+			BvHierarchy::BvhSubdivisionCandidate *bvhSc = 
+				dynamic_cast<BvHierarchy::BvhSubdivisionCandidate *>(sc);
+
+			mBvHierarchy->CollectDirtyCandidates(bvhSc, dirtyList);
 			break;
 		}
@@ -808,28 +936,30 @@
 
 
-void HierarchyManager::CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
-{
-	VspTree::VspSubdivisionCandidate *sc = 
-		dynamic_cast<VspTree::VspSubdivisionCandidate *>(mCurrentCandidate);
-
-	mVspTree->CollectDirtyCandidates(sc, dirtyList);
-}
-
-
-void HierarchyManager::CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList)
+void HierarchyManager::CollectViewSpaceDirtyList(SubdivisionCandidate *sc, 
+												 SubdivisionCandidateContainer &dirtyList)
+{
+	VspTree::VspSubdivisionCandidate *vspSc = 
+		dynamic_cast<VspTree::VspSubdivisionCandidate *>(sc);
+
+	mVspTree->CollectDirtyCandidates(vspSc, dirtyList);
+}
+
+
+void HierarchyManager::CollectDirtyCandidates(SubdivisionCandidate *sc, 
+											  SubdivisionCandidateContainer &dirtyList)
 {
 	// we have either a object space or view space split
-	if (mCurrentCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
-	{
-		CollectViewSpaceDirtyList(dirtyList);
+	if (sc->Type() == SubdivisionCandidate::VIEW_SPACE)
+	{
+		CollectViewSpaceDirtyList(sc, dirtyList);
 	}
 	else // object space split
 	{
-		CollectObjectSpaceDirtyList(dirtyList);
-	}
-}
-
-
-void HierarchyManager::RepairQueue()
+		CollectObjectSpaceDirtyList(sc, dirtyList);
+	}
+}
+
+
+void HierarchyManager::RepairQueue(SubdivisionCandidate *sc)
 {
 	// for each update of the view space partition:
@@ -853,7 +983,8 @@
    
 	vector<SubdivisionCandidate *> dirtyList;
-	CollectDirtyCandidates(dirtyList);
+	CollectDirtyCandidates(sc, dirtyList);
 	if (0) cout << "repairing " << (int)dirtyList.size() << " candidates ... ";
 	
+
 	/////////////////////////////////
 	//-- reevaluate the dirty list
@@ -880,5 +1011,5 @@
 	long endTime = GetTime();
 	Real timeDiff = TimeDiff(startTime, endTime);
-	mHierarchyStats.repairTime += timeDiff;
+	mHierarchyStats.mRepairTime += timeDiff;
 
 	if (1) cout << "repaired in " << timeDiff * 1e-3f << " secs" << endl;
@@ -1057,7 +1188,7 @@
 	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_RTIME  ( Repair time [s] )\n" << mRepairTime * 1e-3f << " \n";
+
+	app << "#N_NODES ( Number of nodes )\n" << mNodes << "\n";
 
 	app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n";
@@ -1065,5 +1196,5 @@
 	app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
 
-	app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth << endl;
+	app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << mMaxDepth << endl;
 
 	app << "#N_GLOBALCOSTMISSES ( Global cost misses )\n" << mGlobalCostMisses << endl;
