Index: GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1166)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1168)
@@ -262,5 +262,5 @@
 
 				mForcedMaterial.mDiffuseColor.b = 1.0f;
-  				const float importance = (float)leaf->GetViewCell()->GetPvs().CountPvs() / (float)maxPvs;
+  				const float importance = (float)leaf->GetViewCell()->GetPvs().CountObjectsInPvs() / (float)maxPvs;
 
 				mForcedMaterial.mDiffuseColor.r = importance;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1160)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1168)
@@ -843,5 +843,5 @@
 		// update scalar pvs size lookup
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
 	
 
@@ -984,5 +984,5 @@
 		// update scalar pvs size value
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
 
 		mBspStats.contributingSamples += conSamp;
@@ -2419,5 +2419,5 @@
 			if (leaf->TreeValid() && 
 				(!onlyUnmailed || !leaf->Mailed()) &&
-				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().CountPvs() <= maxPvsSize)))
+				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().CountObjectsInPvs() <= maxPvsSize)))
 			{
 				leaves.push_back(leaf);
@@ -2496,5 +2496,5 @@
 		  << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
 	  	  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
-		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountPvs() << "), "
+		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountObjectsInPvs() << "), "
 		  << "#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 1160)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1168)
@@ -579,5 +579,5 @@
 	/** Updates pvs of all view cells for statistical evaluation after some more sampling
 	*/
-	virtual void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs) = 0;
+	void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
 
 	
@@ -736,9 +736,5 @@
 	ViewCell *ConstructSpatialMergeTree(BspNode *root);
 
-	/** Updates the pvs in the view cells tree.
-	*/
-	void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
-
-
+	
 protected:
 
@@ -815,5 +811,4 @@
 protected:
 
-	virtual void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs) {};
 	/** Collects view cells from a hierarchy.
 	*/
@@ -924,10 +919,6 @@
 	ViewCell *ConstructSpatialMergeTree(BspNode *root);
 
-	void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
-
-
 	/// HACK for testing visibility filter functionality
 	void TestFilter(const ObjectContainer &objects);
-
 
 	/** Visualization of the pvs difference to exact visubility using 
@@ -1040,6 +1031,4 @@
 	ViewCell *ConstructSpatialMergeTree(VspNode *root);
 
-	void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
-
 	/** Exports visualization of the PVS.
 	*/
@@ -1048,4 +1037,5 @@
 
 
+	/////////////////////////////////////////
 	/// the view space partition tree.
 	VspTree *mVspTree;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1141)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1168)
@@ -1948,5 +1948,5 @@
 		  << "#polygons: " << (int)data.mPolygons->size() << " (max: " << mTermMinPolys << "), "
 		  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), " 
-		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountPvs() << "=, "
+		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountObjectsInPvs() << "=, "
 		  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
 #endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1141)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1168)
@@ -298,5 +298,5 @@
 				mForcedMaterial.mDiffuseColor.b = 1.0f;
   				const float importance = 
-					(float)leaf->GetViewCell()->GetPvs().CountPvs() / (float)maxPvs;
+					(float)leaf->GetViewCell()->GetPvs().CountObjectsInPvs() / (float)maxPvs;
 
 				mForcedMaterial.mDiffuseColor.r = importance;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1166)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1168)
@@ -610,5 +610,5 @@
 		// update scalar pvs size value
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
 
 		mVspStats.contributingSamples += conSamp;
@@ -725,5 +725,5 @@
 		// update scalar pvs size value
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
 
 		mVspStats.contributingSamples += conSamp;
@@ -1797,5 +1797,5 @@
 			if (leaf->TreeValid() && 
 				(!onlyUnmailed || !leaf->Mailed()) &&
-				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().CountPvs() <= maxPvsSize)))
+				((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().CountObjectsInPvs() <= maxPvsSize)))
 			{
 				leaves.push_back(leaf);
@@ -1874,5 +1874,5 @@
 		  << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
 		  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
-		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountPvs() << "), "
+		  << "#pvs: " << leaf->GetViewCell()->GetPvs().CountObjectsInPvs() << "), "
 		  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
 #endif
@@ -4496,7 +4496,4 @@
 		mTotalCost -= splitCandidate->GetRenderCostDecrease();
 
-		//-- subdivide leaf node
-		SubdivideSplitCandidate(splitCandidate);
-		
 		// cost ratio of cost decrease / totalCost
 		const float costRatio = splitCandidate->GetRenderCostDecrease() / mTotalCost;
@@ -4506,4 +4503,7 @@
 			  << splitCandidate->GetRenderCostDecrease() 
 			  << " cost ratio: " << costRatio << endl << endl;
+
+		//-- subdivide leaf node
+		SubdivideSplitCandidate(splitCandidate);
 
 		if (costRatio < mTermMinGlobalCostRatio)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1166)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1168)
@@ -310,5 +310,5 @@
 				Ray *pray = (*it)->mPiercingRays[0];
 				Debug << "view cell " << (*it)->GetId() << " not empty, pvs: " 
-					  << (*it)->GetPvs().CountPvs() << " " << (int)pray->intersections.size() << endl;
+					  << (*it)->GetPvs().CountObjectsInPvs() << " " << (int)pray->intersections.size() << endl;
 
 				exporter->ExportRays((*it)->mPiercingRays);
@@ -1200,6 +1200,6 @@
 {
 
-	if ((vc->GetPvs().CountPvs() > maxPvsSize) ||
-		(vc->GetPvs().CountPvs() < minPvsSize))
+	if ((vc->GetPvs().CountObjectsInPvs() > maxPvsSize) ||
+		(vc->GetPvs().CountObjectsInPvs() < minPvsSize))
 	{
 		return false;
@@ -1417,6 +1417,6 @@
 	{
 		ViewCell *vc = *it;
-		totalRenderCost += vc->GetPvs().CountPvs() * vc->GetVolume();
-		totalPvs += (int)vc->GetPvs().CountPvs();
+		totalRenderCost += vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
+		totalPvs += (int)vc->GetPvs().CountObjectsInPvs();
 	}
 
@@ -1435,9 +1435,9 @@
 		ViewCell *vc = *it;
 
-		float renderCost = vc->GetPvs().CountPvs() * vc->GetVolume();
+		float renderCost = vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
 		float dev;
 
 		if (1)
-			dev = fabs(avgRenderCost - (float)vc->GetPvs().CountPvs());
+			dev = fabs(avgRenderCost - (float)vc->GetPvs().CountObjectsInPvs());
 		else
 			dev = fabs(expectedRenderCost - renderCost);
@@ -1568,5 +1568,6 @@
 
 
-// fast way of merging 2 view cells
+/** fast way of merging 2 view cells.
+*/
 ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCell *left, ViewCell *right) const
 {
@@ -1596,6 +1597,5 @@
 
 	// update pvs size
-	vc->mPvsSize = vc->GetPvs().CountPvs();
-	vc->mPvsSizeValid = true;
+	UpdateScalarPvsSize(vc, vc->GetPvs().CountObjectsInPvs(), vc->GetPvs().GetSize());
 
 	return vc;
@@ -2214,5 +2214,7 @@
 
 
-void ViewCellsManager::UpdateScalarPvsSize(ViewCell *vc, const int pvsSize, const int entriesInPvs) const
+void ViewCellsManager::UpdateScalarPvsSize(ViewCell *vc, 
+										   const int pvsSize, 
+										   const int entriesInPvs) const
 {
 	vc->mPvsSize = pvsSize;
@@ -2466,4 +2468,78 @@
 
 
+void ViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
+{
+	// terminate traversal
+	if (root->IsLeaf())
+	{ 
+		// we assume that pvs is explicitly stored in leaves
+		pvs = root->GetPvs();
+		UpdateScalarPvsSize(root, pvs.CountObjectsInPvs(), pvs.GetSize());
+		
+		return;
+	}
+	
+	//-- interior node => propagate pvs up
+	ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
+	interior->GetPvs().Clear();
+	pvs.Clear();
+	vector<ObjectPvs> pvsList;
+
+	ViewCellContainer::const_iterator vit, vit_end = interior->mChildren.end();
+
+	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit)
+	{
+		ObjectPvs objPvs;
+		
+		//-- recursivly compute child pvss
+		UpdatePvsForEvaluation(*vit, objPvs);
+
+		// store pvs in vector
+		pvsList.push_back(objPvs);
+	}
+
+#if 1
+	Intersectable::NewMail();
+
+	//-- faster way of computing pvs:
+	//   construct merged pvs by adding 
+	//   and only those of the next pvs which were not mailed.
+	//   note: sumpdf is not correct!!
+	vector<ObjectPvs>::iterator oit = pvsList.begin();
+
+	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
+	{
+	    ObjectPvsMap::iterator pit, pit_end = (*oit).mEntries.end();
+	
+		for (pit = (*oit).mEntries.begin(); pit != pit_end; ++ pit)
+		{
+			Intersectable *intersect = (*pit).first;
+
+			if (!intersect->Mailed())
+			{
+				pvs.AddSample(intersect, (*pit).second.mSumPdf);
+				intersect->Mail();
+			}
+		}
+	}
+
+	// store pvs in this node
+	if (mViewCellsTree->ViewCellsStorage() == ViewCellsTree::PVS_IN_INTERIORS)
+	{
+		interior->SetPvs(pvs);
+	}
+	
+	// set new pvs size
+	UpdateScalarPvsSize(interior, pvs.CountObjectsInPvs(), pvs.GetSize());
+	
+
+#else
+
+	// really merge cells: slow put sumpdf is correct
+	viewCellInterior->GetPvs().Merge(backVc->GetPvs());
+	viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
+#endif
+}
+
 /*******************************************************************/
 /*               BspViewCellsManager implementation                */
@@ -3193,78 +3269,4 @@
 
 
-void BspViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
-{
-	// terminate traversal
-	if (root->IsLeaf())
-	{
-		pvs = root->GetPvs();
-		UpdateScalarPvsSize(root, pvs.CountPvs(), pvs.GetSize());
-		
-		return;
-	}
-
-	ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
-	ViewCellContainer::const_iterator vit, vit_end = interior->mChildren.end();
-
-	vector<ObjectPvs> pvsList;
-	
-	
-	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit)
-	{
-		ObjectPvs objPvs;
-		
-		//-- recursivly compute child pvss
-		UpdatePvsForEvaluation(*vit, objPvs);
-
-		// store pvs in vector
-		pvsList.push_back(objPvs);
-	}
-
-#if 1
-	Intersectable::NewMail();
-
-	//-- faster way of computing pvs:
-	//   construct merged pvs by adding 
-	//   and only those of the next pvs which were not mailed.
-	//   note: sumpdf is not correct!!
-	vector<ObjectPvs>::iterator oit = pvsList.begin();
-
-	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
-	{
-		
-        ObjectPvsMap::iterator pit, pit_end = (*oit).mEntries.end();
-	
-		for (pit = (*oit).mEntries.begin(); pit != pit_end; ++ pit)
-		{
-			
-			Intersectable *intersect = (*pit).first;
-
-			if (!intersect->Mailed())
-			{
-				pvs.AddSample(intersect, (*pit).second.mSumPdf);
-				intersect->Mail();
-			}
-		}
-	}
-
-	// store pvs in this node
-	if (mViewCellsTree->ViewCellsStorage() == ViewCellsTree::PVS_IN_INTERIORS)
-	{
-		interior->SetPvs(pvs);
-	}
-	
-	// set correct pvs size for interior
-	UpdateScalarPvsSize(interior, pvs.CountPvs(), pvs.GetSize());
-
-#else
-	// really merge cells: slow nut sumpdf is correct
-	ViewCellInterior *viewCellInterior = new ViewCellInterior();
-
-	viewCellInterior->GetPvs().Merge(backVc->GetPvs());
-	viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
-#endif
-
-}
-
 /************************************************************************/
 /*                   KdViewCellsManager implementation                  */
@@ -4075,81 +4077,4 @@
 
 
-
-void VspBspViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
-{
-	// terminate traversal
-	if (root->IsLeaf())
-	{
-		pvs = root->GetPvs();
-		UpdateScalarPvsSize(root, pvs.CountPvs(), pvs.GetSize());
-		
-		return;
-	}
-	
-	//-- interior node => propagate pvs up
-	ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
-	interior->GetPvs().Clear();
-	pvs.Clear();
-	vector<ObjectPvs> pvsList;
-
-	ViewCellContainer::const_iterator vit, vit_end = interior->mChildren.end();
-
-	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit)
-	{
-		ObjectPvs objPvs;
-		
-		//-- recursivly compute child pvss
-		UpdatePvsForEvaluation(*vit, objPvs);
-
-		// store pvs in vector
-		pvsList.push_back(objPvs);
-	}
-
-#if 1
-
-	Intersectable::NewMail();
-
-	//-- faster way of computing pvs:
-	//   construct merged pvs by adding 
-	//   and only those of the next pvs which were not mailed.
-	//   note: sumpdf is not correct!!
-	vector<ObjectPvs>::iterator oit = pvsList.begin();
-
-	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
-	{
-	    ObjectPvsMap::iterator pit, pit_end = (*oit).mEntries.end();
-	
-		for (pit = (*oit).mEntries.begin(); pit != pit_end; ++ pit)
-		{
-			Intersectable *intersect = (*pit).first;
-
-			if (!intersect->Mailed())
-			{
-				pvs.AddSample(intersect, (*pit).second.mSumPdf);
-				intersect->Mail();
-			}
-		}
-	}
-
-	// store pvs in this node
-	if (mViewCellsTree->ViewCellsStorage() == ViewCellsTree::PVS_IN_INTERIORS)
-	{
-		interior->SetPvs(pvs);
-	}
-	
-	// set new pvs size
-	UpdateScalarPvsSize(interior, pvs.CountPvs(), pvs.GetSize());
-	
-
-#else
-
-	// really merge cells: slow but sumpdf is correct
-	viewCellInterior->GetPvs().Merge(backVc->GetPvs());
-	viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
-#endif
-
-}
-
-
 bool VspBspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
 {
@@ -4646,7 +4571,7 @@
 		
 		const int pvsVc = mViewCellsTree->GetPvsEntries(vc);
-		const int pvs2 = (*vit).second.GetSize();
-
-        m.mDiffuseColor.r = (float) (pvsVc - pvs2);
+		const int pvsPtSamples = (*vit).second.GetSize();
+
+        m.mDiffuseColor.r = (float) (pvsVc - pvsPtSamples);
 		m.mDiffuseColor.b = 1.0f;
 		//exporter->SetForcedMaterial(m);
@@ -5153,80 +5078,4 @@
 
 	return viewCellInterior;
-}
-
-
-
-void VspOspViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
-{
-	// terminate traversal
-	if (root->IsLeaf())
-	{ 
-		// we assume that pvs is explicitly stored in leaves
-		pvs = root->GetPvs();
-		UpdateScalarPvsSize(root, pvs.CountPvs(), pvs.GetSize());
-		
-		return;
-	}
-	
-	//-- interior node => propagate pvs up
-	ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
-	interior->GetPvs().Clear();
-	pvs.Clear();
-	vector<ObjectPvs> pvsList;
-
-	ViewCellContainer::const_iterator vit, vit_end = interior->mChildren.end();
-
-	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit)
-	{
-		ObjectPvs objPvs;
-		
-		//-- recursivly compute child pvss
-		UpdatePvsForEvaluation(*vit, objPvs);
-
-		// store pvs in vector
-		pvsList.push_back(objPvs);
-	}
-
-#if 1
-	Intersectable::NewMail();
-
-	//-- faster way of computing pvs:
-	//   construct merged pvs by adding 
-	//   and only those of the next pvs which were not mailed.
-	//   note: sumpdf is not correct!!
-	vector<ObjectPvs>::iterator oit = pvsList.begin();
-
-	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
-	{
-	    ObjectPvsMap::iterator pit, pit_end = (*oit).mEntries.end();
-	
-		for (pit = (*oit).mEntries.begin(); pit != pit_end; ++ pit)
-		{
-			Intersectable *intersect = (*pit).first;
-
-			if (!intersect->Mailed())
-			{
-				pvs.AddSample(intersect, (*pit).second.mSumPdf);
-				intersect->Mail();
-			}
-		}
-	}
-
-	// store pvs in this node
-	if (mViewCellsTree->ViewCellsStorage() == ViewCellsTree::PVS_IN_INTERIORS)
-	{
-		interior->SetPvs(pvs);
-	}
-	
-	// set new pvs size
-	UpdateScalarPvsSize(interior, pvs.CountPvs(), pvs.GetSize());
-	
-
-#else
-
-	// really merge cells: slow put sumpdf is correct
-	viewCellInterior->GetPvs().Merge(backVc->GetPvs());
-	viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
-#endif
 }
 
@@ -5520,7 +5369,5 @@
 				exporter->ExportRays(vcRays, RgbColor(1, 1, 0));
 			}
-
-		}
-		
+		}
 
 		//-- export view cell geometry
@@ -5797,4 +5644,5 @@
 			if (ray.mTerminationObject) 
 			{
+				// todo: maybe not correct for kd node pvs
 				if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
 					ray.mPdf, contribution))
@@ -5852,5 +5700,18 @@
 #if SAMPLE_ORIGIN_OBJECTS
 				 if (ray.mOriginObject)
-					 viewcell->AddPvsSample(ray.mOriginObject, ray.mPdf, ray.mPvsContribution);
+				 {
+					 if (!mStoreKdPvs)
+					 {
+						 viewcell->AddPvsSample(ray.mOriginObject, ray.mPdf, ray.mPvsContribution);
+					 }
+					 else
+					 {
+						 /// get current leaf the point
+						KdLeaf *leaf = mOspTree->GetLeaf(ray.mOrigin);
+						KdIntersectable *entry = mOspTree->GetOrCreateKdIntersectable(leaf);
+
+						viewcell->AddPvsSample(entry, ray.mPdf, ray.mRelativePvsContribution);
+					 }
+				 }
 #endif
 			}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1166)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1168)
@@ -254,6 +254,6 @@
 
 	// update pvs size scalar
-	mPvsSize = mPvs.GetSize();
-	mPvsSizeValid = true;
+	//mPvsSize = mPvs.GetSize();
+	mPvsSizeValid = false; // recompute pvs size
 
 	return result;
@@ -886,5 +886,5 @@
 		const int upper = mViewCellsManager->GetMaxPvsSize();
 
-		const float penalty = EvalPvsPenalty((*vit)->GetPvs().CountPvs(), lower, upper);
+		const float penalty = EvalPvsPenalty((*vit)->GetPvs().CountObjectsInPvs(), lower, upper);
 		
 		mDeviation += fabs(mAvgRenderCost - penalty);
@@ -978,6 +978,6 @@
 	vc->Mail();
 
-	const int pvs1 = left->GetPvs().CountPvs();
-	const int pvs2 = right->GetPvs().CountPvs();
+	const int pvs1 = left->GetPvs().CountObjectsInPvs();
+	const int pvs2 = right->GetPvs().CountObjectsInPvs();
 
 
@@ -985,5 +985,5 @@
 	mMergedViewCells.push_back(vc);
 
-	pvsDiff = vc->GetPvs().CountPvs() - pvs1 - pvs2;
+	pvsDiff = vc->GetPvs().CountObjectsInPvs() - pvs1 - pvs2;
 
 
@@ -991,7 +991,9 @@
 	if (mViewCellsStorage == PVS_IN_LEAVES)
 	{
-		left->mPvsSize = left->GetPvs().GetSize(); 
-		left->mPvsSizeValid = true;
-		
+		// set scalars
+		mViewCellsManager->UpdateScalarPvsSize(left, 
+			left->GetPvs().CountObjectsInPvs(), 
+			left->GetPvs().GetSize());
+			
 		// remove pvs, we don't store interior pvss
 		if (!left->IsLeaf())
@@ -1000,5 +1002,9 @@
 		}
 
-		right->mPvsSize = right->GetPvs().CountPvs(); 
+		// set scalars
+		mViewCellsManager->UpdateScalarPvsSize(right, 
+			right->GetPvs().CountObjectsInPvs(), 
+			right->GetPvs().GetSize());
+
 		right->mPvsSizeValid = true;
 		
@@ -1285,5 +1291,5 @@
 	if (1)
 	{
-		const float penalty = EvalPvsPenalty(vc->GetPvs().CountPvs(), lower, upper);
+		const float penalty = EvalPvsPenalty(vc->GetPvs().CountObjectsInPvs(), lower, upper);
 		return fabs(mAvgRenderCost - penalty) / (float)mNumActiveViewCells;
 	}
@@ -1298,8 +1304,8 @@
 	if (mUseAreaForPvs)
 	{
-		return vc->GetPvs().CountPvs() * vc->GetArea();
-	}
-
-	return vc->GetPvs().CountPvs() * vc->GetVolume();
+		return vc->GetPvs().CountObjectsInPvs() * vc->GetArea();
+	}
+
+	return vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
 }
 
@@ -1469,4 +1475,5 @@
 	const int rootEntries = GetPvsEntries(mRoot);
 
+	Debug << "******** Export stats **********" << endl;
 	/*Debug << "vsb volume: " << vol << endl;
 	Debug << "root volume: " << mRoot->GetVolume() << endl;
@@ -1517,8 +1524,10 @@
 
 			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
-
+Debug << "\nhere92" << endl;
 			for (it = interior->mChildren.begin(); it != it_end; ++ it)
 			{
+				Debug << "here15" << endl;
 				ViewCell *vc = *it;
+
 				const int pvsSize = GetPvsSize(vc);
 				const int pvsEntries = GetPvsEntries(vc);
@@ -1532,5 +1541,5 @@
 			}
 
-		
+Debug << "\nhere1005" << endl;
 			const float costDecr = (parentCost - childCost) / vol;
 
@@ -1655,4 +1664,5 @@
 }
 
+
 // TODO matt: implement this function for different storing methods
 void ViewCellsTree::GetPvs(ViewCell *vc, ObjectPvs &pvs) const
@@ -1708,41 +1718,52 @@
 int ViewCellsTree::GetPvsSizeForLeafStorage(ViewCell *vc) const
 {
-	int pvsSize = 0;
-
 	// pvs is always stored directly in leaves
 	if (vc->IsLeaf())
 	{
-		pvsSize = vc->GetPvs().CountPvs();
-		//int pvsSize1 = vc->GetPvs().GetSize();
-		//Debug << "entries num: " << pvsSize1 << " size: "" << pvsSize << endl;
-	}
-	else // interior node
-	{
-		// 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)
-		{
-			pvsSize = vc->mPvsSize;
-		}
-		
-		// if no valid pvs size stored as a scalar => 
-		// compute current pvs size of interior from it's leaf nodes
-		ViewCellContainer leaves;
-		CollectLeaves(vc, leaves);
-
-		ViewCellContainer::const_iterator it, it_end = leaves.end();
-
-		Intersectable::NewMail();
-
-		// sum different intersectables
-		for (it = leaves.begin(); it != it_end; ++ it)
-		{
-			pvsSize = GetPvsSizeForLeafStorage(*it);
-		}
-	}
-
-	return pvsSize;
+		return vc->GetPvs().CountObjectsInPvs();
+	}
+	
+	// interior node
+	
+	// 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)
+	{
+		Debug << "here5"<<endl;
+		return vc->mPvsSize;
+	}
+	
+	Debug << "here2"<<endl;
+
+	// if no valid pvs size stored as a scalar => 
+	// compute current pvs size of interior from it's leaf nodes
+	ViewCellContainer leaves;
+	CollectLeaves(vc, leaves);
+
+	ViewCellContainer::const_iterator it, it_end = leaves.end();
+
+	Intersectable::NewMail();
+	ObjectPvs newPvs;
+
+	// sum different intersectables
+	for (it = leaves.begin(); it != it_end; ++ it)
+	{
+		ObjectPvsMap::iterator oit, oit_end = (*it)->GetPvs().mEntries.end();
+
+		for (oit = (*it)->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
+		{
+			Intersectable *intersect = (*oit).first;
+
+			if (!intersect->Mailed())
+			{
+				intersect->Mail();
+				newPvs.AddSample(intersect, (*oit).second.mSumPdf);
+			}
+		}
+	}
+
+	return newPvs.CountObjectsInPvs();
 }
 
@@ -1750,35 +1771,45 @@
 int ViewCellsTree::GetEntriesInPvsForLeafStorage(ViewCell *vc) const
 {
-	int pvsSize = 0;
-
 	// pvs is always stored directly in leaves
 	if (vc->IsLeaf())
 	{
-		pvsSize = vc->GetPvs().GetSize();
-	}
-	else // interior node
-	{
-		// 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)
-		{
-			pvsSize = vc->mEntriesInPvs;
-		}
-		
-		// if no valid pvs size stored as a scalar => 
-		// compute current pvs size of interior from it's leaf nodes
-		ViewCellContainer leaves;
-		CollectLeaves(vc, leaves);
-
-		ViewCellContainer::const_iterator it, it_end = leaves.end();
-
-		Intersectable::NewMail();
-
-		// sum different intersectables
-		for (it = leaves.begin(); it != it_end; ++ it)
-		{
-			pvsSize = GetEntriesInPvsForLeafStorage(*it);
+		return vc->GetPvs().GetSize();
+	}
+	
+	// interior node
+
+	// 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)
+	{
+		return vc->mEntriesInPvs;
+	}
+	
+	int pvsSize = 0;
+
+	// if no valid pvs size stored as a scalar => 
+	// compute current pvs size of interior from it's leaf nodes
+	ViewCellContainer leaves;
+	CollectLeaves(vc, leaves);
+
+	ViewCellContainer::const_iterator it, it_end = leaves.end();
+	Intersectable::NewMail();
+
+	// sum different intersectables
+	for (it = leaves.begin(); it != it_end; ++ it)
+	{
+		ObjectPvsMap::iterator oit, oit_end = (*it)->GetPvs().mEntries.end();
+
+		for (oit = (*it)->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
+		{
+			Intersectable *intersect = (*oit).first;
+
+			if (!intersect->Mailed())
+			{
+				intersect->Mail();
+				++ pvsSize;
+			}
 		}
 	}
@@ -1916,5 +1947,5 @@
 		// pvs is stored consistently in the tree up to the root
 		// just return pvs size 
-		pvsSize = vc->GetPvs().CountPvs();	
+		pvsSize = vc->GetPvs().CountObjectsInPvs();	
 		break;
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1141)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1168)
@@ -421,9 +421,9 @@
 {
 public:
-	/** Counts pvs. Different to GetSize(), not
+	/** Counts object int the pvs. Different to GetSize(), not
 		only the raw container size is returned,
 		but the individual contributions of the entries are summed up.
 	*/
-	int CountPvs() const;
+	int CountObjectsInPvs() const;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1166)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1168)
@@ -206,5 +206,5 @@
 		return a->mPvsSize < b->mPvsSize;
 #else
-		return a->GetPvs().CountPvs() < b->GetPvs().CountPvs();
+		return a->GetPvs().CountObjectsInPvs() < b->GetPvs().CountObjectsInPvs();
 #endif
 	}
@@ -288,6 +288,4 @@
 	/// is up to date and corresponding to the real pvs size
 	bool mPvsSizeValid;
-	
-
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1143)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1168)
@@ -17,5 +17,5 @@
 
 
-int ObjectPvs::CountPvs() const
+int ObjectPvs::CountObjectsInPvs() const
 {
 	int pvs = 0;
@@ -31,7 +31,6 @@
 
 		// found kd node
-		// the pvs is the sum of the objects in the leaves in the subtree
-		// We eliminate already accounted kd nodes and objects
-		// using mailboxing. 
+		// the pvs is the number od different objects in the node leaves
+		// We eliminate already accounted kd nodes and objects using mailboxing. 
 		if (obj->Type() == Intersectable::KD_INTERSECTABLE)
 		{
@@ -47,36 +46,38 @@
 				tStack.pop();
 
-				// already processed node (objects in pvs)
-				if (node->Mailed())
-					continue;
+				// already processed node (= objects in pvs)?
+				if (!node->Mailed())
+				{
+					node->Mail();
 
-				node->Mail();
+					if (node->IsLeaf())
+					{
+						KdLeaf *leaf = dynamic_cast<KdLeaf *>(node);
+                        
+						// add #objects exclusivly in this node
+						pvs += (int)(leaf->mObjects.size() - leaf->mMultipleObjects.size());
 
-				if (node->IsLeaf())
-				{
-					KdLeaf *leaf = dynamic_cast<KdLeaf *>(node);
-
-					pvs += (int)(leaf->mObjects.size() - leaf->mMultipleObjects.size());
-
-					// Objects already accounted for can only be found among those
-					// which are referenced in more than one leaf
-					ObjectContainer::const_iterator oit, oit_end = leaf->mMultipleObjects.end();
-					for (oit = leaf->mMultipleObjects.begin(); oit != oit_end; ++ oit)
-					{
-						Intersectable *object = *oit;						
+						// Objects already accounted for can only be found among those
+						// which are referenced in more than one leaf
+						ObjectContainer::const_iterator oit, oit_end = leaf->mMultipleObjects.end();
 						
-						if (!object->Mailed())
+						for (oit = leaf->mMultipleObjects.begin(); oit != oit_end; ++ oit)
 						{
-							object->Mail();
-							++ pvs;
+							Intersectable *object = *oit;						
+                            
+							if (!object->Mailed())
+							{
+								object->Mail();
+								++ pvs;
+							}
 						}
 					}
-				}
-				else
-				{
-					KdInterior *interior = dynamic_cast<KdInterior *>(node);
+					else // traverse tree
+					{
+						KdInterior *interior = dynamic_cast<KdInterior *>(node);
 
-					tStack.push(interior->mFront);
-					tStack.push(interior->mBack);
+						tStack.push(interior->mFront);
+						tStack.push(interior->mBack);
+					}
 				}
 			}
