Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/todo_i3d
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/todo_i3d	(revision 1685)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/todo_i3d	(revision 1707)
@@ -1,4 +1,2 @@
-1) implement the computed value for ratio drc /dmem
-2) try soda scene using more samples to avoid samples
 3) test object space subdivision as postprocess
 	do view space subdivison on objects
@@ -9,5 +7,15 @@
 		visibility based heuristics + sah for undersampling
 
+check sah if implemented right!! (bounding volume!!)
+
 4) implement full evaluation framework
 
 5) rerun vienna tests
+
+6) check if memory was computed right!!
+
+7) include tree sizes in memory computation + plots
+
+8) include cost of bvh in drc / rmem heuristics
+
+9) check undersampling in visualization
Index: GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 1501)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 1707)
@@ -206,5 +206,5 @@
 				Name="VCLinkerTool"
 				AdditionalDependencies="xerces-c_2.lib glew32.lib zdll.lib zziplib.lib devil.lib glut32.lib OpenGL32.Lib glu32.lib Preprocessor.lib RTScene.lib RTWorld.lib QtCore4.lib qtmain.lib QtOpenGL4.lib Qt3Support4.lib QtTest4.lib QtGui4.lib QtGlRenderer.lib"
-				OutputFile="../bin/release/Preprocessor.exe"
+				OutputFile="../bin/release/Preprocessor2.exe"
 				LinkIncremental="1"
 				AdditionalLibraryDirectories="..\src\GL;..\lib\release;..\..\Preprocessing\lib\release;..\..\..\..\..\..\NonGTP\Boost\lib;..\..\..\..\..\..\NonGTP\Xerces\xercesc\lib;..\..\..\..\..\..\NonGTP\Zlib\lib;..\..\..\..\..\..\NonGTP\Devil\lib;..\MultiLevelRayTracing\RTScene\Release;..\MultiLevelRayTracing\RTWorld\Release;&quot;$(QTDIR)\lib&quot;;.\QtGlRenderer\Release"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1571)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1707)
@@ -222,5 +222,5 @@
 
 				mForcedMaterial.mDiffuseColor.b = 1.0f;
-  				const float importance = (float)leaf->GetViewCell()->GetPvs().CountObjectsInPvs() / (float)maxPvs;
+  				const float importance = (float)leaf->GetViewCell()->GetPvs().EvalPvsCost() / (float)maxPvs;
 
 				mForcedMaterial.mDiffuseColor.r = importance;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1632)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1707)
@@ -829,5 +829,5 @@
 		// update scalar pvs size lookup
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.EvalPvsCost(), pvs.GetSize());
 	
 
@@ -974,5 +974,5 @@
 		ObjectPvs &pvs = viewCell->GetPvs();
 		mViewCellsManager->UpdateScalarPvsSize(viewCell, 
-											   pvs.CountObjectsInPvs(), 
+											   pvs.EvalPvsCost(), 
 											   pvs.GetSize());
 
@@ -2391,5 +2391,5 @@
 			if (leaf->TreeValid() && 
 				(!onlyUnmailed || !leaf->Mailed()) &&
-				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().CountObjectsInPvs() <= maxPvsSize)))
+				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().EvalPvsCost() <= maxPvsSize)))
 			{
 				leaves.push_back(leaf);
@@ -2468,5 +2468,5 @@
 		  << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
 	  	  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
-		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountObjectsInPvs() << "), "
+		  << "#pvs: " << leaf->GetViewCell()->GetPvs().EvalPvsCost() << "), "
 		  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
 #endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1703)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1707)
@@ -573,4 +573,8 @@
 	void ResetViewCells();
 	
+	/** Sets this view cell to active.
+	*/
+	void SetViewCellActive(ViewCell *vc) const;
+
 	/** Collects the view cells in the view cell container.
 	*/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1686)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1707)
@@ -2025,5 +2025,5 @@
 		  << "#polygons: " << (int)data.mPolygons->size() << " (max: " << mTermMinPolys << "), "
 		  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), " 
-		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountObjectsInPvs() << "=, "
+		  << "#pvs: " << leaf->GetViewCell()->GetPvs().EvalPvsCost() << "=, "
 		  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
 #endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1706)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1707)
@@ -452,5 +452,6 @@
 	void CollectBestSet(const int maxSplits,
 						const float maxMemoryCost,
-						vector<VspNode *> &vspNodes,
+						//vector<VspNode *> &vspNodes,
+						ViewCellContainer &viewCells,
 						vector<BvhNode *> &bvhNodes);
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1707)
@@ -247,5 +247,5 @@
 				mForcedMaterial.mDiffuseColor.b = 1.0f;
   				const float importance = 
-					(float)leaf->GetViewCell()->GetPvs().CountObjectsInPvs() / (float)maxPvs;
+					(float)leaf->GetViewCell()->GetPvs().EvalPvsCost() / (float)maxPvs;
 
 				mForcedMaterial.mDiffuseColor.r = importance;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1705)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1707)
@@ -1506,8 +1506,8 @@
 
 
-void BvHierarchy::CollectLeaves(vector<BvhLeaf *> &leaves) const
+void BvHierarchy::CollectLeaves(BvhNode *root, vector<BvhLeaf *> &leaves) const
 {
 	stack<BvhNode *> nodeStack;
-	nodeStack.push(mRoot);
+	nodeStack.push(root);
 
 	while (!nodeStack.empty()) 
@@ -1763,8 +1763,8 @@
 
 
-BvhIntersectable *BvHierarchy::GetOrCreateBvhIntersectable(BvhNode *node)
+BvhIntersectable *BvHierarchy::GetOrCreateBvhIntersectable(BvhLeaf *node)
 {
 	// search nodes
-	std::map<BvhNode *, BvhIntersectable *>::const_iterator 
+	std::map<BvhLeaf *, BvhIntersectable *>::const_iterator 
 		it = mBvhIntersectables.find(node);
 
@@ -2151,4 +2151,20 @@
 
 
+void BvHierarchy::SetActive(BvhNode *node) const
+{
+	vector<BvhLeaf *> leaves;
+
+	// sets the pointers to the currently active view cells
+	CollectLeaves(node, leaves);
+
+	vector<BvhLeaf *>::const_iterator bit, bit_end = leaves.end();
+
+	for (bit = leaves.begin(); bit != bit_end; ++ bit)
+	{
+		(*bit)->SetActiveNode(node);
+	}
+}
+
+
 BvhNode *BvHierarchy::SubdivideAndCopy(SplitQueue &tQueue, 
 									   SubdivisionCandidate *splitCandidate)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1705)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1707)
@@ -1077,5 +1077,5 @@
 
 /// Returns index of the best view cells of the neighborhood
-int GetBestViewCellIdx(ViewCell *root, const ViewCellContainer &neighborhood)
+static int GetBestViewCellIdx(ViewCell *root, const ViewCellContainer &neighborhood)
 {
 	int bestViewCellIdx = 0;
@@ -1237,6 +1237,6 @@
 {
 
-	if ((vc->GetPvs().CountObjectsInPvs() > maxPvsSize) ||
-		(vc->GetPvs().CountObjectsInPvs() < minPvsSize))
+	if ((vc->GetPvs().EvalPvsCost() > maxPvsSize) ||
+		(vc->GetPvs().EvalPvsCost() < minPvsSize))
 	{
 		return false;
@@ -1510,6 +1510,6 @@
 	{
 		ViewCell *vc = *it;
-		totalRenderCost += vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
-		totalPvs += (int)vc->GetPvs().CountObjectsInPvs();
+		totalRenderCost += vc->GetPvs().EvalPvsCost() * vc->GetVolume();
+		totalPvs += (int)vc->GetPvs().EvalPvsCost();
 	}
 
@@ -1530,9 +1530,9 @@
 		ViewCell *vc = *it;
 
-		float renderCost = vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
+		float renderCost = vc->GetPvs().EvalPvsCost() * vc->GetVolume();
 		float dev;
 
 		if (1)
-			dev = fabs(avgRenderCost - (float)vc->GetPvs().CountObjectsInPvs());
+			dev = fabs(avgRenderCost - (float)vc->GetPvs().EvalPvsCost());
 		else
 			dev = fabs(expectedRenderCost - renderCost);
@@ -1643,5 +1643,5 @@
 
 	// update pvs size
-	UpdateScalarPvsSize(vc, vc->GetPvs().CountObjectsInPvs(), vc->GetPvs().GetSize());
+	UpdateScalarPvsSize(vc, vc->GetPvs().EvalPvsCost(), vc->GetPvs().GetSize());
 
 	return vc;
@@ -2155,20 +2155,27 @@
 
 
+void ViewCellsManager::SetViewCellActive(ViewCell *vc) const
+{
+	ViewCellContainer leaves;
+	// sets the pointers to the currently active view cells
+	mViewCellsTree->CollectLeaves(vc, leaves);
+
+	ViewCellContainer::const_iterator lit, lit_end = leaves.end();
+	for (lit = leaves.begin(); lit != lit_end; ++ lit)
+	{
+		dynamic_cast<ViewCellLeaf *>(*lit)->SetActiveViewCell(vc);
+	}
+}
+
+
 void ViewCellsManager::SetViewCellsActive()
 {
-	// collect leaf view cells and set the pointers to the currently
-	// active view cells
+	// collect leaf view cells and set the pointers to 
+	// the currently active view cells
 	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
 
 	for (it = mViewCells.begin(); it != it_end; ++ it)
 	{
-		ViewCellContainer leaves;
-		mViewCellsTree->CollectLeaves(*it, leaves);
-
-		ViewCellContainer::const_iterator lit, lit_end = leaves.end();
-		for (lit = mViewCells.begin(); lit != lit_end; ++ lit)
-		{
-			dynamic_cast<ViewCellLeaf *>(*lit)->SetActiveViewCell(*it);
-		}
+		SetViewCellActive(*it);
 	}
 }
@@ -2570,5 +2577,5 @@
 		// we assume that pvs is explicitly stored in leaves
 		pvs = root->GetPvs();
-		UpdateScalarPvsSize(root, pvs.CountObjectsInPvs(), pvs.GetSize());
+		UpdateScalarPvsSize(root, pvs.EvalPvsCost(), pvs.GetSize());
 		
 		return;
@@ -2637,5 +2644,5 @@
 	
 	// set new pvs size
-	UpdateScalarPvsSize(interior, pvs.CountObjectsInPvs(), pvs.GetSize());
+	UpdateScalarPvsSize(interior, pvs.EvalPvsCost(), pvs.GetSize());
 	
 #else 
@@ -5720,5 +5727,5 @@
 	{
 		ViewCell *vc = *vit;
-		int pvs = vc->GetPvs().CountObjectsInPvs();
+		int pvs = vc->GetPvs().EvalPvsCost();
 		float vol = vc->GetVolume();
 		rc += pvs * vol;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1701)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1707)
@@ -110,5 +110,5 @@
 {
 	vector<BvhLeaf *> leaves;
-	bvHierarchy.CollectLeaves(leaves);
+	bvHierarchy.CollectLeaves(bvHierarchy.GetRoot(), leaves);
 
 	mUseForcedMaterial = true;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1706)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1707)
@@ -503,5 +503,4 @@
 				tstack.push(*it);
 			}
-			
 		}
 	}
@@ -930,5 +929,5 @@
 		const int upper = mViewCellsManager->GetMaxPvsSize();
 
-		const float penalty = EvalPvsPenalty((*vit)->GetPvs().CountObjectsInPvs(), lower, upper);
+		const float penalty = EvalPvsPenalty((*vit)->GetPvs().EvalPvsCost(), lower, upper);
 		
 		mDeviation += fabs(mAvgRenderCost - penalty);
@@ -1023,6 +1022,6 @@
 	vc->Mail();
 
-	const int pvs1 = left->GetPvs().CountObjectsInPvs();
-	const int pvs2 = right->GetPvs().CountObjectsInPvs();
+	const float pvs1 = left->GetPvs().EvalPvsCost();
+	const float pvs2 = right->GetPvs().EvalPvsCost();
 
 
@@ -1030,5 +1029,5 @@
 	mMergedViewCells.push_back(vc);
 
-	pvsDiff = vc->GetPvs().CountObjectsInPvs() - pvs1 - pvs2;
+	pvsDiff = vc->GetPvs().EvalPvsCost() - pvs1 - pvs2;
 
 
@@ -1038,5 +1037,5 @@
 		// set scalars
 		mViewCellsManager->UpdateScalarPvsSize(left, 
-				left->GetPvs().CountObjectsInPvs(), 
+				left->GetPvs().EvalPvsCost(), 
 				left->GetPvs().GetSize());
 			
@@ -1049,5 +1048,5 @@
 		// set scalars
 		mViewCellsManager->UpdateScalarPvsSize(right, 
-			right->GetPvs().CountObjectsInPvs(), 
+			right->GetPvs().EvalPvsCost(), 
 			right->GetPvs().GetSize());
 
@@ -1336,5 +1335,5 @@
 	if (1)
 	{
-		const float penalty = EvalPvsPenalty(vc->GetPvs().CountObjectsInPvs(), lower, upper);
+		const float penalty = EvalPvsPenalty(vc->GetPvs().EvalPvsCost(), lower, upper);
 		return fabs(mAvgRenderCost - penalty) / (float)mNumActiveViewCells;
 	}
@@ -1349,8 +1348,8 @@
 	if (mUseAreaForPvs)
 	{
-		return vc->GetPvs().CountObjectsInPvs() * vc->GetArea();
-	}
-
-	return vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
+		return vc->GetPvs().EvalPvsCost() * vc->GetArea();
+	}
+
+	return vc->GetPvs().EvalPvsCost() * vc->GetVolume();
 }
 
@@ -1795,9 +1794,9 @@
 	if (vc->IsLeaf())
 	{
-		return vc->GetPvs().CountObjectsInPvs();
-	}
-	
-	// interior nodes: pvs is either stored as a scalar or
-	// has to be reconstructed from the leaves
+		return vc->GetPvs().EvalPvsCost();
+	}
+	
+	// interior nodes: pvs is either stored as a 
+	// scalar or has to be reconstructed from the leaves
 	// the stored pvs size is the valid pvs size => just return scalar
 	if (vc->mPvsSizeValid)
@@ -1833,5 +1832,5 @@
 	}
 
-	return newPvs.CountObjectsInPvs();
+	return newPvs.EvalPvsCost();
 }
 
@@ -2002,5 +2001,6 @@
 	switch (mViewCellsStorage)
 	{
-	case PVS_IN_LEAVES: //-- store pvs only in leaves
+	case PVS_IN_LEAVES: 
+		// pvs is stored only in leaves
 		pvsSize = GetPvsSizeForLeafStorage(vc);			
 		break;
@@ -2012,5 +2012,5 @@
 		// pvs is stored consistently in the tree up to the root
 		// just return pvs size 
-		pvsSize = vc->GetPvs().CountObjectsInPvs();	
+		pvsSize = vc->GetPvs().EvalPvsCost();	
 		break;
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1706)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1707)
@@ -310,9 +310,16 @@
 	virtual void CollectObjects(ObjectContainer &objects);
 
-	/** returns level of the hierarchy that is "active" right now.
+	/** Returns level of the hierarchy that is "active" right now.
 	*/
 	BvhNode *GetActiveNode()
 	{
 		return mActiveNode;
+	}
+
+	/** Returns level of the hierarchy that is "active" right now.
+	*/
+	void SetActiveNode(BvhNode *node)
+	{
+		mActiveNode = node;
 	}
 
@@ -331,10 +338,10 @@
 	SubdivisionCandidate *mSubdivisionCandidate;
 
-	
+	/// the active node which will be accounted for in the pvs
 	BvhNode *mActiveNode;
 };
 
 
-typedef map<BvhNode *, BvhIntersectable *> BvhIntersectableMap;
+typedef map<BvhLeaf *, BvhIntersectable *> BvhIntersectableMap;
 
 
@@ -516,10 +523,7 @@
 								  bool computeSplitPlane = true);
 
-	/** Returns list of leaves with pvs smaller than
-		a certain threshold.
-		@param onlyUnmailed if only the unmailed leaves should be considered
-		@param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
-	*/
-	void CollectLeaves(vector<BvhLeaf *> &leaves) const;
+	/** Returns vector of leaves.
+	*/
+	void CollectLeaves(BvhNode *root, vector<BvhLeaf *> &leaves) const;
 
 	/** Returns bounding box of the whole tree (= bbox of root node)
@@ -564,8 +568,8 @@
 	bool Export(OUT_STREAM &stream);
 
-	/** Returns or creates a new intersectable for use in a kd based pvs.
-		The OspTree is responsible for destruction of the intersectable.
-	*/
-	BvhIntersectable *GetOrCreateBvhIntersectable(BvhNode *node);
+	/** Returns or creates a new intersectable for use in a bv based pvs.
+		The hierarchy is responsible for destruction of the intersectable.
+	*/
+	BvhIntersectable *GetOrCreateBvhIntersectable(BvhLeaf *node);
 
 	/** Collects rays associated with the objects.
@@ -586,4 +590,5 @@
 	*/
 	ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
+	
 	/** See Get
 	*/
@@ -593,4 +598,8 @@
 	*/
 	float GetMemUsage() const;
+
+	/** Sets this node to be an active node.
+	*/
+	void SetActive(BvhNode *node) const;
 
 
@@ -603,4 +612,5 @@
 
 	static float EvalAbsCost(const ObjectContainer &objects);
+
 
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1706)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1707)
@@ -529,5 +529,5 @@
 		but the individual contributions of the entries are summed up.
 	*/
-	int CountObjectsInPvs() const;
+	float EvalPvsCost() const;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1707)
@@ -183,5 +183,5 @@
 		return a->mPvsSize < b->mPvsSize;
 #else
-		return a->GetPvs().CountObjectsInPvs() < b->GetPvs().CountObjectsInPvs();
+		return a->GetPvs().EvalPvsCost() < b->GetPvs().EvalPvsCost();
 #endif
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1698)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1707)
@@ -654,5 +654,5 @@
 		// update scalar pvs size value
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.EvalPvsCost(), pvs.GetSize());
 
 		mVspStats.contributingSamples += conSamp;
@@ -768,5 +768,5 @@
 		// update scalar pvs size value
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.EvalPvsCost(), pvs.GetSize());
 
 		mVspStats.contributingSamples += conSamp;
@@ -1803,5 +1803,5 @@
 			if (leaf->TreeValid() && 
 				(!onlyUnmailed || !leaf->Mailed()) &&
-				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().CountObjectsInPvs() <= maxPvsSize)))
+				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().EvalPvsCost() <= maxPvsSize)))
 			{
 				leaves.push_back(leaf);
@@ -1880,5 +1880,5 @@
 		  << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
 		  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
-		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountObjectsInPvs() << "), "
+		  << "#pvs: " << leaf->GetViewCell()->GetPvs().EvalPvsCost() << "), "
 		  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
 #endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1703)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1707)
@@ -79,28 +79,28 @@
 	We eliminate already accounted bvh nodes and objects using mailboxing. 
 */
-static int EvalBvhNodeContribution(BvhIntersectable *bvhobj)
+static float EvalBvhNodeContribution(BvhIntersectable *bvhobj)
 {
-	BvhNode *node= bvhobj->GetItem();
+	BvhLeaf *leaf = bvhobj->GetItem();
+	BvhNode *node = leaf->GetActiveNode();
 
 	// early exit
-	if (node->IsLeaf())	
+	if (node == leaf)	
 	{
 		// objects already accounted for
-		if (node->Mailed())
+		if (leaf->Mailed())
 			return 0;
 
-		node->Mail();
-		BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
-		return (int)leaf->mObjects.size();
+		leaf->Mail();
+		return BvHierarchy::EvalAbsCost(leaf->mObjects);
 	}			 
 
 	// compute leaf pvs
-	int pvs = 0;
+	float pvs = 0;
 	stack<BvhNode *> tStack;
-	tStack.push(bvhobj->GetItem());
+	tStack.push(node);
 
 	while (!tStack.empty())
 	{
-		BvhNode *node = tStack.top();
+		node = tStack.top();
 		tStack.pop();
 
@@ -112,5 +112,5 @@
 			if (node->IsLeaf())
 			{
-				BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
+				leaf = dynamic_cast<BvhLeaf *>(node);
 
 				// add #objects exclusivly in this node
@@ -132,7 +132,7 @@
 
 
-int ObjectPvs::CountObjectsInPvs() const
+float ObjectPvs::EvalPvsCost() const
 {
-	int pvs = 0;
+	float pvs = 0;
 
 	Intersectable::NewMail();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1706)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1707)
@@ -21,4 +21,5 @@
 #include "OspTree.h"
 #include "BvHierarchy.h"
+#include "ViewCell.h"
 
 
@@ -1683,5 +1684,5 @@
 	// reinstall old bv refs
 	vector<BvhLeaf *> leaves;
-	mBvHierarchy->CollectLeaves(leaves);
+	mBvHierarchy->CollectLeaves(mBvHierarchy->GetRoot(), leaves);
 	vector<BvhLeaf *>::const_iterator bit, bit_end = leaves.end();
 
@@ -1788,5 +1789,5 @@
 {
 public:
-	enum {VSP_NODE, BVH_NODE};
+	enum {VSP_NODE, BVH_NODE, VIEW_CELL};
 
 	virtual float GetMergeCost() const = 0;
@@ -1850,11 +1851,44 @@
 
 
+class ViewCellWrapper: public HierarchyNodeWrapper
+{
+public:
+
+	ViewCellWrapper(ViewCell *vc): mViewCell(vc) {}
+	
+	int Type()  const { return VIEW_CELL; }
+
+	float GetMergeCost() const { return mViewCell->GetMergeCost(); };
+
+	bool IsLeaf() const { return mViewCell->IsLeaf(); }
+
+	void PushChildren(HierarchyNodeQueue &tQueue)
+	{
+		if (!mViewCell->IsLeaf())
+		{
+			ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(mViewCell);
+
+			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+
+			for (it = interior->mChildren.begin(); it != it_end; ++ it)
+			{
+				tQueue.push(new ViewCellWrapper(*it));
+			}
+		}
+	}
+
+	ViewCell *mViewCell;
+};
+
+
 void HierarchyManager::CollectBestSet(const int maxSplits,
 									  const float maxMemoryCost,
-									  vector<VspNode *> &vspNodes,
+				//					  vector<VspNode *> &vspNodes,
+									  ViewCellContainer &viewCells,
 									  vector<BvhNode *> &bvhNodes)
 {
 	HierarchyNodeQueue tqueue;
-	tqueue.push(new VspNodeWrapper(mVspTree->GetRoot()));
+	//tqueue.push(new VspNodeWrapper(mVspTree->GetRoot()));
+	tqueue.push(new ViewCellWrapper(mVspTree->mViewCellsTree->GetRoot()));
 	tqueue.push(new BvhNodeWrapper(mBvHierarchy->GetRoot()));
 	
@@ -1869,5 +1903,5 @@
 		// because of the priority queue, this will be the optimal set of v
 		if (nodeWrapper->IsLeaf() || 
-			((vspNodes.size() + bvhNodes.size() + tqueue.size() + 1) >= maxSplits) ||
+			((viewCells.size() + bvhNodes.size() + tqueue.size() + 1) >= maxSplits) ||
 			(memCost > maxMemoryCost)
 			)
@@ -1875,6 +1909,6 @@
 			if (nodeWrapper->Type() == HierarchyNodeWrapper::VSP_NODE)
 			{
-				VspNodeWrapper *vspNodeWrapper = dynamic_cast<VspNodeWrapper *>(nodeWrapper);
-				vspNodes.push_back(vspNodeWrapper->mNode);
+				ViewCellWrapper *viewCellWrapper = dynamic_cast<ViewCellWrapper *>(nodeWrapper);
+				viewCells.push_back(viewCellWrapper->mViewCell);
 			}
 			else
@@ -1900,4 +1934,51 @@
 										 int &pvsEntries)
 {
+	//vector<VspNode *> vspNodes;
+	ViewCellContainer viewCells;
+	vector<BvhNode *> bvhNodes;
+
+    CollectBestSet(maxSplits, maxMemoryCost, viewCells, bvhNodes);
+
+	vector<BvhNode *>::const_iterator bit, bit_end = bvhNodes.end();
+	
+	for (bit = bvhNodes.begin(); bit != bit_end; ++ bit)
+	{
+		mBvHierarchy->SetActive(*bit);
+	}
+
+	//vector<VspNode *>::const_iterator vit, vit_end = vspNodes.end();
+
+	ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
+
+	int numEntries = 0;
+	float pvsCost = 0.0f;
+
+	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
+	{
+		ViewCell *vc = *vit;
+		ObjectPvs pvs;
+		mVspTree->mViewCellsTree->GetPvs(vc, pvs);
+
+		BvhNode::NewMail();
+
+		// hack: should not be done here
+		ObjectPvsMap::const_iterator oit, oit_end = pvs.mEntries.end();
+
+		for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
+		{
+			BvhIntersectable *intersect = dynamic_cast<BvhIntersectable *>((*oit).first);
+			BvhLeaf *leaf = intersect->GetItem();
+			BvhNode *activeNode = leaf->GetActiveNode();
+
+			if (!activeNode->Mailed())
+			{
+				activeNode->Mail();
+
+				++ numEntries;
+				pvsCost += mBvHierarchy->EvalAbsCost(leaf->mObjects);
+			}
+		}
+	}
+
 	/*TraversalQueue tqueue;
 	tqueue.push(mRoot);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1694)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1707)
@@ -12,9 +12,12 @@
 struct VssRayContainer;
 class KdNode;
-class BvhNode;
+//class BvhNode;
+class BvhLeaf;
+class Ray;
+
 struct Face;
-class Ray;
 struct Triangle3;
-  class KdTree;
+
+class KdTree;
 
 
@@ -166,9 +169,9 @@
 typedef map<KdNode *, KdIntersectable *> KdIntersectableMap;
 
-class BvhIntersectable: public IntersectableWrapper<BvhNode *>
-{
-public:
-	BvhIntersectable(BvhNode *item):
-	IntersectableWrapper<BvhNode *>(item) {}
+class BvhIntersectable: public IntersectableWrapper<BvhLeaf *>
+{
+public:
+	BvhIntersectable(BvhLeaf *item):
+	IntersectableWrapper<BvhLeaf *>(item) {}
 
 	int Type() const
