Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1789)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1790)
@@ -2694,5 +2694,5 @@
 	for (it = rays.begin(); it != it_end; ++ it)
 	{
-		int contribution = 0;
+		float contribution = 0;
 		Ray *ray = (*it)->mRay;
 		float relContribution;
@@ -2707,7 +2707,7 @@
 												   //relContribution);
 		
-		if (contribution)
-		{
-			sampleContributions += contribution;
+		if (contribution >  0)
+		{
+			sampleContributions += (int)contribution;
 			++ contributingSamples;
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1789)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1790)
@@ -1554,5 +1554,6 @@
 
 			// no good results for degenerate axis split
-			if (0 && tData.mNode->GetBoundingBox().Size(axis) < Limits::Small)
+			if (0 &&
+				(tData.mNode->GetBoundingBox().Size(axis) < Limits::Small))
 					nCostRatio[axis] += 9999;
 
@@ -2549,5 +2550,5 @@
 
 		if (!InitialTerminationCriteriaMet(bsc->mParentData))
-		{cout << "here9"<<endl;
+		{
 			BvhNode *node = Subdivide(tempQueue, bsc, globalCriteriaMet);
 
@@ -2556,5 +2557,5 @@
 		}
 		else // initial preprocessing  finished for this candidate
-		{cout << "here19"<<endl;
+		{
 			// add to "real" traversal queue
 			candidateContainer.push_back(bsc);
@@ -2608,13 +2609,5 @@
 		backObjects.push_back(*oit);
 	}
-	TriangleIntersectable *tObj1 = (TriangleIntersectable *)frontObjects.back();
-	TriangleIntersectable *tObj2 = (TriangleIntersectable *)backObjects.front();
-
-	cout << "here4 " << tObj1->GetItem().GetArea() << " " << tObj2->GetItem().GetArea() << endl;
-
-
-if (maxAreaDiff < 0.0001)
-cout << "big error!!!! " << maxAreaDiff << endl;
-
+	
 	cout << "front: " << (int)frontObjects.size() << " back " << (int)backObjects.size() << " " << backObjects.front()->GetBox().SurfaceArea() - frontObjects.back()->GetBox().SurfaceArea() << endl;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1789)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1790)
@@ -1956,18 +1956,4 @@
 }
 
-/*
-void
-ViewCellsManager::AddSampleContributions(const VssRayContainer &rays)
-{
-  if (!ViewCellsConstructed())
-	return;
-
-  VssRayContainer::const_iterator it, it_end = rays.end();
-cout <<"here60"<<endl;
-  for (it = rays.begin(); it != it_end; ++ it) {
-	AddSampleContributions(*(*it));
-  }
-}
-*/
 
 int ViewCellsManager::GetMinPvsSize() const
@@ -1983,35 +1969,9 @@
 }
 
-/*
-void
-ViewCellsManager::AddSampleContributions(VssRay &ray)
-{cout << "here12" << endl;
-	// assumes viewcells have been stored...
-	ViewCellContainer *viewcells = &ray.mViewCells;
-	ViewCellContainer::const_iterator it;
-
-	if (!ray.mTerminationObject)
-		return;
-
-	Intersectable *obj = GetIntersectable(ray, true);
-
-	for (it = viewcells->begin(); it != viewcells->end(); ++it) 
-	{
-		ViewCell *viewcell = *it;
-		if (viewcell->GetValid()) 
-		{
-			Intersectable *entry = GetIntersectable(ray, true);
-			if (entry)    
-				// if ray not outside of view space
-				viewcell->GetPvs().AddSample(entry, ray.mPdf);
-		}
-	}
-}
-*/
 
 float ViewCellsManager::ComputeSampleContribution(VssRay &ray,
 												  const bool addRays,
 												  const bool storeViewCells)
-{cout << "$$$$$$$$$$$$$4here10" << endl;
+{
 	ViewCellContainer viewcells;
 
@@ -6041,4 +6001,5 @@
 	
 
+#define PVS_ADD_DIRTY 1
 float VspOspViewCellsManager::ComputeSampleContribution(VssRay &ray,
 														const bool addRays,
@@ -6094,10 +6055,11 @@
 			{
 				// todo: maybe not correct for kd node pvs
-			/*	if (viewcell->GetPvs().GetSampleContribution(
-					terminationObj, ray.mPdf, contribution))
+				if (viewcell->GetPvs().GetSampleContribution(
+							terminationObj, ray.mPdf, contribution))
 				{
 					++ ray.mPvsContribution;
 				}
-				ray.mRelativePvsContribution += contribution;*/
+
+				ray.mRelativePvsContribution += contribution;
 			}
 
@@ -6109,5 +6071,5 @@
 #if SAMPLE_ORIGIN_OBJECTS
 
-		/*	if (originObj && 
+			if (originObj && 
 				viewcell->GetPvs().GetSampleContribution(originObj,
 														 ray.mPdf,
@@ -6116,5 +6078,5 @@
 				++ ray.mPvsContribution;
 				ray.mRelativePvsContribution += contribution;
-			}*/
+			}
 #endif
 		}
@@ -6135,44 +6097,22 @@
 
 		//$$JB hack
-		float contrib;
-		//viewCell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf, contrib);
-		viewCell->GetPvs().AddSample(terminationObj, ray.mPdf);//, contrib);
-
-		if (viewCell->GetPvs().GetSize() == 50)
-		//if (viewCell->GetPvs().RequiresResort())
-		{Intersectable::NewMail();
-		cout << "\nbefore sort: " << viewCell->GetPvs().GetLastSorted() << " \n";
-			ObjectPvsIterator pit = viewCell->GetPvs().GetIterator(); int i = 0;
-			while (pit.HasMoreEntries())
-			{		
-				ObjectPvsEntry entry = pit.Next();
-                Intersectable *object = entry.mObject;
-
-				if ((i ++) == viewCell->GetPvs().GetLastSorted())
-					cout << "| ";
-				
-				//if (object->Mailed())	cout << "error!!"<<endl;
-				object->Mail();
-				cout << (int)object << " ";
-			}
-
-			//viewCell->GetPvs().Sort();
-			
-			/*cout << "\nafter sort\n";
-			pit = viewCell->GetPvs().GetIterator(); i = 0;
-			while (pit.HasMoreEntries())
-			{		
-				ObjectPvsEntry entry = pit.Next();
-                Intersectable *object = entry.mObject;
-
-				if ((i ++) == viewCell->GetPvs().GetLastSorted())
-					cout << "| ";	
-				cout << (int)object << " ";
-			}*/
-		}
-		//else cout << "b";
+
+#if PVS_ADD_DIRTY
+		viewCell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf);
+#else
+		viewCell->GetPvs().AddSample(terminationObj, ray.mPdf);
+#endif
+
 #if SAMPLE_ORIGIN_OBJECTS
-		//viewCell->GetPvs().AddSample(originObj, ray.mPdf);
+#if PVS_ADD_DIRTY
+		viewCell->GetPvs().AddSampleDirtyCheck(originObj, ray.mPdf);
+#else
+		viewCell->GetPvs().AddSample(originObj, ray.mPdf);
 #endif
+#endif
+		if (viewCell->GetPvs().RequiresResort())
+		{
+			viewCell->GetPvs().Sort();			
+		}
 	}
 
@@ -6310,4 +6250,5 @@
 			//////////////
 			// filtered stats
+
 			sprintf(suffix, "-%09d-eval-filter.log", castSamples);
 			const string filename2 = string(statsPrefix) + string(suffix);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1789)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1790)
@@ -206,5 +206,7 @@
 		@returns iterator on the sample.
 	*/
-	typename vector<PvsEntry<T, S> >::iterator Find(T sample, const bool checkDirty = true);
+	bool Find(T sample,
+			  typename vector<PvsEntry<T, S> >::iterator &it,
+			  const bool checkDirty = true);
 
 	bool GetSampleContribution(T sample, const float pdf, float &contribution);
@@ -391,7 +393,6 @@
 		float aSumPdf = 0.0f;
 
-		vector<PvsEntry<T, S> >::iterator oit = Find((*it).mObject);		
-
-		const bool entryFound = (it != mEntries.end()) && ((*it).mObject == (*oit).mObject);
+		vector<PvsEntry<T, S> >::iterator oit;
+		const bool entryFound = Find((*it).mObject, oit);		
 
 		if (entryFound)
@@ -427,8 +428,8 @@
 			(*it).mData.mSumPdf = -aSumPdf;
 		} else {
-			vector<PvsEntry<T, S> >::iterator oit = b.Find((*it).mObject);
-
-			const bool entryFound = (it != mEntries.end()) && ((*it).mObject == (*oit).mObject);
-			
+			vector<PvsEntry<T, S> >::iterator oit;
+		
+			const bool entryFound = b.Find((*it).mObject, oit);
+						
 			if (entryFound) {
 				bSumPdf = (*oit).mData.mSumPdf;
@@ -461,6 +462,8 @@
 	for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
 	{
-		std::vector<PvsEntry<T, S> >::const_iterator bit = Find((*it).first);		
-		if (bit == mEntries.end()) ++ dif;
+		vector<PvsEntry<T, S> >::iterator bit;
+		const bool entryFound = Find((*it).first, bit);
+
+		if (!entryFound) ++ dif;
 	}
 
@@ -586,6 +589,10 @@
 
 template <typename T, typename S> 
-typename std::vector<PvsEntry<T, S> >::iterator Pvs<T, S>::Find(T sample, const bool checkDirty)
-{
+bool Pvs<T, S>::Find(T sample,
+					 typename vector<PvsEntry<T, S> >::iterator &it,
+					 const bool checkDirty)
+{
+	bool found = false;
+
 	PvsEntry<T, S> dummy(sample, PvsData());
 
@@ -593,18 +600,23 @@
 	//mLastSorted = 0;
 	vector<PvsEntry<T, S> >::iterator sorted_end = mEntries.begin() + mLastSorted;
-if (sorted_end != mEntries.end())
-cout << "not entries end!! " << endl;
+
 	// binary search
-	vector<PvsEntry<T, S> >::iterator it = lower_bound(mEntries.begin(), sorted_end, dummy);
+	it = lower_bound(mEntries.begin(), sorted_end, dummy);
+
+	if ((it != mEntries.end()) && ((*it).mObject == sample))
+		found = true;
 
 	// sample not found yet => search further in the unsorted part
-	if (checkDirty &&
-		((it == mEntries.end()) || ((*it).mObject != sample)))
-	{
-		vector<PvsEntry<T, S> >::const_iterator it_end = mEntries.end();
-		for (it = sorted_end; (it != it_end) && ((*it).mObject != sample); ++ it);
-	}
-	else cout << "f ";
-	return it;
+	if (!found && checkDirty)
+	{
+		vector<PvsEntry<T, S> >::const_iterator dit, dit_end = mEntries.end();
+
+		for (dit = sorted_end; (dit != dit_end) && ((*dit).mObject != sample); ++ dit);
+
+		if ((dit != mEntries.end()) && ((*dit).mObject == sample))
+			found = true;
+	}
+	
+	return found;
 }
 
@@ -625,9 +637,10 @@
 {
 	++ mSamples;
-	cout << "! ";
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
-	{	cout << "g";
+	
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);		
+
+	if (entryFound)
+	{	
 		S &data = (*it).mData;
 		data.mSumPdf += pdf;
@@ -635,5 +648,5 @@
 	}
 	else 
-	{cout << "t";
+	{
 		PvsEntry<T, S> entry(sample, pdf);
 		mEntries.insert(it, entry);
@@ -641,5 +654,4 @@
 		return pdf;
 	}
-	cout << " $";
 }
 
@@ -654,11 +666,13 @@
 
 template <typename T, typename S>
-typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample, const float pdf)
+typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample, 
+																  const float pdf)
 {
 	++ mSamples;
 	
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound == Find(sample, it);
+
+	if (entryFound)
 	{
 		S &data = (*it).second;
@@ -674,35 +688,4 @@
 	return it;
 }
-
-
-/*template <typename T, typename S> 
-bool Pvs<T, S>::AddSample(T sample,
-						  const float pdf,
-						  float &contribution)
-{
-	++ mSamples;
-
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
-	{
-		S &data = (*it).mData;
-
-		data.mSumPdf += pdf;
-		contribution = pdf / data.mSumPdf;
-
-		return false;
-	}
-	else 
-	{
-		PvsEntry<T, S> entry(sample, pdf);
-
-		mEntries.insert(it, entry);
-		contribution = 1.0f;
-		++ mLastSorted;
-
-		return true;
-	}
-}*/
 
 
@@ -717,7 +700,8 @@
 	++ mSamples;
 
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);
+
+	if (entryFound)
 	{
 		S &data = (*it).mData;
@@ -743,7 +727,8 @@
 									  float &contribution) 
 {
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if (it != mEntries.end() && ((*it).mObject == sample))  
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);
+
+	if (entryFound)  
 	{
 		S &data = (*it).mData;
@@ -764,7 +749,8 @@
 	-- mSamples;
 	
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if (it == mEntries.end())
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);
+
+	if (!entryFound)
 		return false;
 
