Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 1557)
@@ -89,6 +89,7 @@
 	Vector3 CenterOfMass() const;
 
+	/** Returns true if this geometry is well shaped.
+	*/
 	bool Valid() const;
-
 	
 	friend ostream &operator<<(ostream &s, const BspNodeGeometry &a)
@@ -101,5 +102,9 @@
 	}
 
+	/** Number of polygons.
+	*/
 	int Size() const;
+	/** Returns the polygons in a container.
+	*/
 	const PolygonContainer &GetPolys();
 
@@ -679,5 +684,9 @@
 	*/
 	void SetViewCellsTree(ViewCellsTree *vct);
-		
+	
+	/** Returns view cell representing the empty view space.
+	*/
+	BspViewCell *GetOrCreateOutOfBoundsCell();
+
 protected:
 
@@ -1071,4 +1080,6 @@
 
 	bool mOutOfBoundsCellPartOfTree;
+
+
 private:
 	
@@ -1093,5 +1104,7 @@
 	static void GenerateUniqueIdsForPvs();
 
+	///////////
 	//-- unique ids for PVS criterium
+
 	static int sFrontId; 
 	static int sBackId;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1557)
@@ -75,5 +75,7 @@
 	if (randomize) Randomize(); // initialise random generator for heuristics
 
+	//////////////////
 	//-- termination criteria for autopartition
+
 	Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.maxDepth", mTermMaxDepth);
 	Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs);
@@ -86,16 +88,19 @@
 	Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.maxViewCells", mMaxViewCells);
 
-	//-- max cost ratio for early tree termination
+	////////////////////////
+	//-- cost ratios for early tree termination
 	Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
-
 	Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.minGlobalCostRatio", mTermMinGlobalCostRatio);
 	Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.globalCostMissTolerance", mTermGlobalCostMissTolerance);
 
+	///////////
 	//-- factors for bsp tree split plane heuristics
+
 	Environment::GetSingleton()->GetFloatValue("VspBspTree.Factor.pvs", mPvsFactor);
 	Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.ct_div_ci", mCtDivCi);
 
-
+	//////////
 	//-- partition criteria
+
 	Environment::GetSingleton()->GetIntValue("VspBspTree.maxPolyCandidates", mMaxPolyCandidates);
 	Environment::GetSingleton()->GetIntValue("VspBspTree.maxRayCandidates", mMaxRayCandidates);
@@ -108,4 +113,5 @@
 	Environment::GetSingleton()->GetBoolValue("VspBspTree.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
 	
+	//////////////////////
 	//-- termination criteria for axis aligned split
 	Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.AxisAligned.maxRayContribution", 
@@ -114,5 +120,4 @@
 							 mTermMinRaysForAxisAligned);
 
-	//Environment::GetSingleton()->GetFloatValue("VspBspTree.maxTotalMemory", mMaxTotalMemory);
 	Environment::GetSingleton()->GetFloatValue("VspBspTree.maxStaticMemory", mMaxMemory);
 
@@ -136,4 +141,5 @@
 	Environment::GetSingleton()->GetBoolValue("VspBspTree.Construction.useDrivingAxisForMaxCost", mUseDrivingAxisIfMaxCostViolated);
 
+	/////////
 	//-- debug output
 
@@ -148,5 +154,4 @@
 	Debug << "max view cells: " << mMaxViewCells << endl;
 	Debug << "max polygon candidates: " << mMaxPolyCandidates << endl;
-	//Debug << "max plane candidates: " << mMaxRayCandidates << endl;
 	Debug << "randomize: " << randomize << endl;
 
@@ -195,8 +200,7 @@
 	}
 
+	Debug << endl;
 
 	mLocalSubdivisionCandidates = new vector<SortableEntry>;
-
-	Debug << endl;
 }
 
@@ -213,5 +217,5 @@
 	{
 		mOutOfBoundsCell = new BspViewCell();
-		mOutOfBoundsCell->SetId(-1);
+		mOutOfBoundsCell->SetId(OUT_OF_BOUNDS_ID);
 		mOutOfBoundsCell->SetValid(false);
 	}
@@ -3003,4 +3007,7 @@
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
+		if ((*it) == mOutOfBoundsCell)
+			continue;
+		
 		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*it);
 		vector<BspLeaf *>::const_iterator bit, bit_end = bspVc->mLeaves.end();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1557)
@@ -54,8 +54,6 @@
   float mRenderBudget;
 
-  /// some characteristic values could be here
-  
+  // some characteristic values could be stored as well
 };
-
 
 /**	Manages different higher order operations on the view cells.
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1553)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1557)
@@ -174,6 +174,6 @@
 {
 	Randomize(); // initialise random generator for heuristics
-	mOutOfBoundsCell = new BspViewCell();
-	mOutOfBoundsCell->SetId(-1);
+	mOutOfBoundsCell = GetOrCreateOutOfBoundsCell();
+
 	/////////
 	//-- termination criteria for autopartition
@@ -277,4 +277,16 @@
 
 
+BspViewCell *BspTree::GetOrCreateOutOfBoundsCell()
+{
+	if (!mOutOfBoundsCell)
+	{
+		mOutOfBoundsCell = new BspViewCell();
+		mOutOfBoundsCell->SetId(OUT_OF_BOUNDS_ID);
+		mOutOfBoundsCell->SetValid(false);
+	}
+
+	return mOutOfBoundsCell;
+}
+
 int BspTree::SplitPolygons(const Plane3 &plane,
 						   PolygonContainer &polys, 
@@ -420,9 +432,8 @@
 	if (mOutOfBoundsCellPartOfTree)
 	{
-		cout << "here223 " << mOutOfBoundsCell << endl;
 		// out of bounds cell not part of tree =>
 		// delete manually
 		DEL_PTR(mOutOfBoundsCell);
-	}else cout << "here991 " << endl;
+	}
 }
 
@@ -999,8 +1010,11 @@
 			// add predefined view cell to leaf
 			viewCell = dynamic_cast<BspViewCell *>(tData.mViewCell);
-		
-			/// out of bounds cell can be handled as any other cell
+
+			// from now on out of bounds cell can be handled as any other cell,
+			// responsibility for deleting has been shifted
 			if (viewCell == mOutOfBoundsCell)
+			{
 				mOutOfBoundsCellPartOfTree = true;
+			}
 		}
 
@@ -2364,5 +2378,4 @@
 
 
-
 void BspTree::ConstructGeometry(ViewCell *vc, 
 								BspNodeGeometry &vcGeom) const
@@ -2379,4 +2392,8 @@
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
+		// per definition out of bounds cell has zero volume
+		if ((*it) == mOutOfBoundsCell)
+			continue;
+
 		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*it);
 		vector<BspLeaf *>::const_iterator bit, bit_end = bspVc->mLeaves.end();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1557)
@@ -315,4 +315,8 @@
 		const ObjectContainer &objects);
 
+	void HierarchyManager::ResetViewSpaceSubdivision(
+		const VssRayContainer &rays, 
+		const ObjectContainer &objects);
+
 protected:
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1557)
@@ -1965,6 +1965,8 @@
 		for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
 		{
-			if ((*vit)->GetId() != -1) 
+			if ((*vit)->GetId() != OUT_OF_BOUNDS_ID) 
+			{
 				mViewCells[i]->SetId(i ++);
+			}
 		}
 	}
@@ -2988,8 +2990,7 @@
 												 const AxisAlignedPlane *clipPlane
 												 ) const
-{cout << "here55543 " << vc->GetId() << endl;
+{
 	// out of bounds cell
-	if (vc->GetId() == -1)
-		return;
+	//if (vc->GetId() == OUT_OF_BOUNDS_ID) return;
 
 	// export mesh if available
@@ -4500,7 +4501,5 @@
 {
 	// out of bounds cell
-	if (vc->GetId() == -1)
-		return;
-
+	//if (vc->GetId() == OUT_OF_BOUNDS_ID)	return;
 	if (clipPlane)
 	{
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/common.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 1287)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 1557)
@@ -511,13 +511,17 @@
 #endif
 
-#endif
-
-
-
-
-
-
-
-
-
-
+/** view cell id belonging to empty view space.
+*/
+#define OUT_OF_BOUNDS_ID -1
+
+#endif
+
+
+
+
+
+
+
+
+
+
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1548)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1557)
@@ -608,10 +608,4 @@
 												   const ObjectContainer &objects)
 {
-	switch (mObjectSpaceSubdivisionType)
-	{
-	case BV_BASED_OBJ_SUBDIV:
-        Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
-		cout << "\nresetting bv hierarchy" << endl;
-		mHierarchyStats.nodes -= mBvHierarchy->mBvhStats.nodes;
 #if 0	
 		DEL_PTR(mBvHierarchy);
@@ -620,8 +614,23 @@
 
 		PrepareObjectSpaceSubdivision(sampleRays, objects);
-#else
-		mBvHierarchy->Reset(sampleRays, objects);
+		return;
 #endif
-		break;
+
+	if (!ObjectSpaceSubdivisionConstructed())
+	{
+		return PrepareObjectSpaceSubdivision(sampleRays, objects);
+	}
+
+	switch (mObjectSpaceSubdivisionType)
+	{
+	case BV_BASED_OBJ_SUBDIV:
+		cout << "\nreseting bv hierarchy" << endl;
+        Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
+	
+		mHierarchyStats.nodes -= mBvHierarchy->mBvhStats.nodes;
+		mTQueue.Push(mBvHierarchy->Reset(sampleRays, objects));
+		mTotalCost = mBvHierarchy->mTotalCost;
+		break;
+
 	case KD_BASED_OBJ_SUBDIV:
 		// TODO
@@ -630,4 +639,16 @@
 		break;
 	}
+}
+
+
+void HierarchyManager::ResetViewSpaceSubdivision(const VssRayContainer &sampleRays, 
+												 const ObjectContainer &objects)
+{
+	DEL_PTR(mBvHierarchy);
+	mBvHierarchy = new BvHierarchy();
+	mBvHierarchy->mHierarchyManager = this;
+
+	PrepareViewSpaceSubdivision(sampleRays, objects);
+	return;
 }
 
@@ -639,8 +660,6 @@
 	mHierarchyStats.Reset();
 	mHierarchyStats.Start();
-
 	mHierarchyStats.nodes = 2;
 	
-
 	mTotalCost = (float)objects.size();
 	Debug << "setting total cost to " << mTotalCost << endl;
@@ -655,30 +674,35 @@
 	mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
 	mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
-
-	// start with object space subdivision
-	PrepareObjectSpaceSubdivision(sampleRays, objects);
-	
-	// process object space candidates
-	RunConstruction(false);
-
-	mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
-
-	const int limit = 2;
-	for (int i = 0; i < limit; ++ i)
-	{
-       	// again run object space subdivision on the view cells
+	
+	const int limit = 4;
+	int i = 0;
+
+	// render cost optimization
+	// start with object space partiton
+	// then optimizate view space partition for the current osp
+	// and vice versa until iteration depth is reached.
+	while (1)
+	{
+       	// first run object space subdivision
 		ResetObjectSpaceSubdivision(sampleRays, objects);
-	
+
 		// process object space candidates
 		RunConstruction(false);
 
-		 /////////////////
-		// now do view space subdivison using the current object space partition
-//		ResetViewSpaceSubdivision(sampleRays, objects);
-	
+		if ((++ i) >= limit)
+			break;
+
+		/////////////////
+		// do view space subdivison with respect to the object space partition
+		ResetViewSpaceSubdivision(sampleRays, objects);
+
 		// process view space candidates
 		RunConstruction(false);
-	
-		cout << "íteration " << i << " of " << limit << " finished" << endl;
+		mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
+		
+		if ((++ i) >= limit)
+			break;
+
+		cout << "iteration " << i << " of " << limit << " finished" << endl;
 	}
 	
