Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 584)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 585)
@@ -50,4 +50,24 @@
 
 	return (float)pvs;
+}
+
+
+
+
+inline int CountDiffPvs(ViewCell *vc)
+{
+	int count = 0;
+
+	ObjectPvsMap::const_iterator it, it_end = vc->GetPvs().mEntries.end();
+	for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
+	{
+		if (!(*it).first->Mailed())
+		{
+			(*it).first->Mail();
+			++ count;
+		}
+	}
+
+	return count;
 }
 
@@ -1257,4 +1277,5 @@
 
         ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+		
 		// compress child sets first
 		for (it = interior->mChildren.begin(); it != it_end; ++ it)
@@ -1263,4 +1284,5 @@
 		}
 
+		// compress root node
 		PropagateUpVisibility(interior);
 	}
@@ -1341,12 +1363,8 @@
 
 		for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			Debug << "mail: " << (*oit).first->mMailbox << endl;
-
+		{		
 			if ((*oit).first->Mailed((int)interior->mChildren.size()))
 			{	
-				//Debug << "adding sample" << endl;
 				interior->GetPvs().AddSample((*oit).first, (*oit).second.mSumPdf);
-				//(*oit)->remove();
 			}
 		}
@@ -1366,32 +1384,54 @@
 		}
 	}
-}
+
+	int dummy = interior->GetPvs().GetSize();
+
+	for (cit = interior->mChildren.begin(); cit != cit_end; ++ cit)
+	{
+		dummy += (*cit)->GetPvs().GetSize();
+	}
+
+}
+
 
 
 void ViewCellsTree::GetPvs(ViewCell *vc, ObjectPvs &pvs) const
 {
-	if (mIsCompressed)
-	{
-		ObjectPvsMap::const_iterator oit, oit_end = vc->GetPvs().mEntries.end();
-
-		for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			pvs.AddSample((*oit).first, (*oit).second.mSumPdf);
-		}
+	Intersectable::NewMail();
+
+	if (!mIsCompressed)
+		pvs = vc->GetPvs();
+
+	int pvsSize = 0;
+	ViewCell *root = vc;
+	
+	// also add pvs from this view cell to root
+	while (root->GetParent())
+	{
+		root = root->GetParent();
+		pvs.AddPvs(root->GetPvs());
+	}
+
+	stack<ViewCell *> tstack;
+	tstack.push(vc);
+
+	while (!tstack.empty())
+	{
+		vc = tstack.top();
+		tstack.pop();
+
+		pvs.AddPvs(vc->GetPvs());
 
 		if (!vc->IsLeaf())
 		{
 			ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
+
 			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
 
 			for (it = interior->mChildren.begin(); it != it_end; ++ it)
 			{
-				GetPvs(*it, pvs);
-			}
-		}
-	}
-	else
-	{
-		pvs = vc->GetPvs();
+				tstack.push(*it);
+			}		
+		}
 	}
 }
@@ -1400,16 +1440,41 @@
 int ViewCellsTree::GetPvsSize(ViewCell *vc) const
 {
-	int pvsSize = vc->GetPvs().GetSize();
-
-	//Debug << "current size: " << pvsSize << endl;
-	if (mIsCompressed && !vc->IsLeaf())
-	{
-		ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
-
-		ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
-
-		for (it = interior->mChildren.begin(); it != it_end; ++ it)
-		{
-			pvsSize += GetPvsSize(*it);
+	Intersectable::NewMail();
+
+	if (!mIsCompressed)
+		return vc->GetPvs().GetSize();
+
+	int pvsSize = 0;
+	ViewCell *root = vc;
+	
+	// also add pvs from this view cell to root
+	while (root->GetParent())
+	{
+		root = root->GetParent();
+		pvsSize += CountDiffPvs(root);
+	}
+
+	stack<ViewCell *> tstack;
+	tstack.push(vc);
+
+	Debug << "current size: " << pvsSize << endl;
+
+	while (!tstack.empty())
+	{
+		vc = tstack.top();
+		tstack.pop();
+
+		pvsSize += CountDiffPvs(vc);
+
+		if (!vc->IsLeaf())
+		{
+			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);
+			}		
 		}
 	}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 584)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 585)
@@ -2366,39 +2366,11 @@
 	RefineViewCells(postProcessRays, objects);
 
-	ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
-
-	int pvsSize = 0;
-	int numPvsEntries = 0;
-
-	int vidx = 0;
-	for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
-	{
-		const int vcPvs = mViewCellsTree->GetPvsSize(*vit);
-		const int pvsEntries = mViewCellsTree->GetNumPvsEntries(*vit);
-
-		pvsSize += vcPvs;
-		numPvsEntries += pvsEntries;
-		Debug << "Viewcell " << vidx ++ << ": " << vcPvs << endl;
-	}
-
-	Debug << "pvs size before compress: " << pvsSize << endl;
-	Debug << "number of entries before compress: " << numPvsEntries << endl;
+	int pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
+	Debug << "number of entries before compress: " << pvsEntries << endl;
 
 	mViewCellsTree->CompressViewCellsPvs();
 
-	pvsSize = numPvsEntries = vidx = 0;
-
-	for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
-	{
-		const int vcPvs = mViewCellsTree->GetPvsSize(*vit);
-		const int pvsEntries = mViewCellsTree->GetNumPvsEntries(*vit);
-		
-		pvsSize += vcPvs;
-		numPvsEntries += pvsEntries;
-		Debug << "Viewcell " << vidx ++ << ": " << vcPvs << endl;	
-	}
-
-	Debug << "pvs size after compress: " << pvsSize << endl;
-	Debug << "number of entries after compress: " << numPvsEntries << endl;
+	pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
+	Debug << "number of entries after compress: " << pvsEntries << endl;
 
 	if (1)
