Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1745)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1750)
@@ -527,5 +527,5 @@
 
 	void ComputePvs(const ObjectPvs &pvs, float &rc, int &pvsEntries);
-
+	void GetPvsIncrementally(ViewCell *vc, ObjectPvs &pvs) const;
 protected:
 
@@ -639,4 +639,5 @@
 	friend ViewCellsParseHandlers;
 
+	ViewCellContainer mOldViewCells;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1749)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1750)
@@ -2615,4 +2615,5 @@
   float samples = (float)pvs.GetSamples();
   cout<<"Samples = "<<samples<<endl;
+   cout<<"size = "<<pvs.GetSize() <<endl;
   //  cout<<"Filter size = "<<filterSize<<endl;
   //  cout<<"vbox = "<<vbox<<endl;
@@ -6063,5 +6064,5 @@
 			Debug << "statistics computed in " << timeDiff * 1e-3 << " secs" << endl;
 
-#if 1
+#if 0
 			////////////////////////////
 			// filtered stats
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1749)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1750)
@@ -1727,6 +1727,4 @@
 	////////////////
 	//-- pvs is not stored with the interiors => reconstruct
-	Intersectable::NewMail();
-
 	ViewCell *root = vc;
 	
@@ -1748,7 +1746,6 @@
 	
 		// add newly found pvs to merged pvs
-		//cout << "samples vc: " << vc->GetPvs().GetSamples() << " samples pvs: " << pvs.GetSamples() << endl;
 		pvs.MergeInPlace(vc->GetPvs());
-		//cout << "new samples: " << pvs.GetSamples() << endl;
+		
 		if (!vc->IsLeaf()) // interior cells: go down to leaf level
 		{
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1745)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1750)
@@ -244,5 +244,7 @@
 	/** Clears the pvs.
 	*/
-	void Clear();
+	void Clear(const bool trim = true);
+
+	void Trim();
 
 	static int GetEntrySizeByte(); 
@@ -438,10 +440,19 @@
 		mergedPvs.mEntries.push_back(*bit);
 	}
-}
-
-
-template <typename T, typename S> void Pvs<T, S>::Clear()
+	mergedPvs.mSamples = a.mSamples + b.mSamples;
+}
+
+
+template <typename T, typename S> void Pvs<T, S>::Clear(const bool trim = true)
 {
 	mEntries.clear();
+	mSamples = 0;
+	vector<PvsEntry<T,S> >().swap(mEntries);
+}
+
+
+template <typename T, typename S> void Pvs<T, S>::Trim()
+{
+	vector<PvsEntry<T,S> >(mEntries).swap(mEntries);//trim vi
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1749)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1750)
@@ -1859,4 +1859,38 @@
 
 
+// TODO matt: implement this function for different storing methods
+void HierarchyManager::GetPvsIncrementally(ViewCell *vc, ObjectPvs &pvs) const
+{
+	////////////////
+	//-- pvs is not stored with the interiors => reconstruct
+	ViewCell *root = vc;
+	
+	// add pvs from leaves
+	stack<ViewCell *> tstack;
+	tstack.push(vc);
+
+	while (!tstack.empty())
+	{
+		vc = tstack.top();
+		tstack.pop();
+	
+		// add newly found pvs to merged pvs: break here even for interior
+		if (!vc->GetPvs().Empty())
+		{
+			pvs.MergeInPlace(vc->GetPvs());
+		}
+		else if (!vc->IsLeaf()) // interior cells: go down to leaf level
+		{
+			ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
+			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+
+			for (it = interior->mChildren.begin(); it != it_end; ++ it)
+			{
+				tstack.push(*it);
+			}		
+		}
+	}
+}
+
 int HierarchyManager::ExtractStatistics(const int maxSplits,
 										const float maxMemoryCost,
@@ -1887,18 +1921,22 @@
 	renderCost = 0.0f;
 
+	ViewCell::NewMail();
+
 	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
 	{
+		ViewCell *vc = *vit;
+
 		float rc = 0;
-		ViewCell *vc = *vit;
-
-		ObjectPvs pvs;
+        ObjectPvs pvs;
 		mVspTree->mViewCellsTree->GetPvs(vc, pvs);
 
+		vc->SetPvs(pvs);
+
+		vc->Mail();
+
 		if (useFilter)
 		{
-			ViewCellLeaf dummy;
-			dummy.SetPvs(pvs);
 			ObjectPvs filteredPvs;
-			mVspTree->mViewCellsManager->ApplyFilter2(&dummy, false, 1.0f, filteredPvs);
+			mVspTree->mViewCellsManager->ApplyFilter2(vc, false, 1.0f, filteredPvs);
 			ComputePvs(filteredPvs, rc, pvsEntries);
 		}
@@ -1913,11 +1951,22 @@
 
 	renderCost /= mVspTree->mViewCellsManager->GetViewSpaceBox().GetVolume();
-
 	memory = pvsEntries * ObjectPvs::GetEntrySize();
 
 	viewSpaceSplits = (int)viewCells.size();
 	objectSpaceSplits = (int)bvhNodes.size();
-
 	//cout << "viewCells: " << (int)viewCells.size() << " nodes: " << (int)bvhNodes.size() << " rc: " << renderCost << " entries: " << pvsEntries << endl;
+
+	// delete old "base" view cells if they are not leaves
+	ViewCellContainer::const_iterator oit, oit_end = mOldViewCells.end();
+
+	for (oit = mOldViewCells.begin(); oit != oit_end; ++ oit)
+	{
+		if (!(*oit)->Mailed() && !(*oit)->IsLeaf())
+		{
+			(*oit)->GetPvs().Clear();
+		}
+	}
+
+	mOldViewCells = viewCells;
 
 	return viewCells.size() + bvhNodes.size();
@@ -2099,10 +2148,12 @@
 											const bool useFilter)
 {
-	HierarchySubdivisionStats subStats;
-	
-	int splits = 0;
+	vector<HierarchySubdivisionStats> subStatsContainer;
+
+	int splits = (1 + mHierarchyStats.Leaves() / splitsStepSize) * splitsStepSize;
+	cout << "splits: " << splits << endl;
 
 	while (1)
 	{
+		HierarchySubdivisionStats subStats;
 		subStats.mNumSplits = ExtractStatistics(splits, 
 												99999.0, 
@@ -2131,13 +2182,33 @@
 		subStats.mFullMemory = subStats.mMemoryCost + objectSpaceHierarchyMem + viewSpaceHierarchyMem;
 		
-		subStats.Print(splitsStats);
-		splits += splitsStepSize;
-
-		if (subStats.mNumSplits == mHierarchyStats.Leaves())
+		subStatsContainer.push_back(subStats);
+		
+		if (splits == 0)
 			break;
 
+		splits -= splitsStepSize;
+
 		cout << subStats.mNumSplits << " ";
 	}
 
+	vector<HierarchySubdivisionStats>::const_reverse_iterator hit, hit_end = subStatsContainer.rend();
+
+	for (hit = subStatsContainer.rbegin(); hit != hit_end; ++ hit)
+	{
+		(*hit).Print(splitsStats);
+	}
+
+	// delete old "base" view cells: only pvss in the leaves are allowed
+	ViewCellContainer::const_iterator oit, oit_end = mOldViewCells.end();
+	for (oit = mOldViewCells.begin(); oit != oit_end; ++ oit)
+	{
+		if (!(*oit)->IsLeaf())
+		{
+			(*oit)->GetPvs().Clear();
+		}
+	}
+
+	mOldViewCells.clear();
+
 	cout << endl;
 }
