Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1816)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1824)
@@ -32,5 +32,5 @@
 
 // $$JB HACK
-#define USE_KD_PVS 0
+#define USE_KD_PVS 1
 #define KD_PVS_AREA (1e-5f)
 
@@ -856,5 +856,6 @@
 	viewCells = mViewCells;
 #endif
-	ViewCellContainer::iterator it = viewCells.begin(), it_end = viewCells.end();		
+	ViewCellContainer::iterator it = viewCells.begin(),
+	  it_end = viewCells.end();		
 	for (; it != it_end; ++it) {
 	  //(*it)->UpdatePvsCost();
@@ -872,5 +873,6 @@
 	// hack: normalize pvs size
 	int histoMaxVal;
-	Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
+	Environment::GetSingleton()->GetIntValue(
+											 "Preprocessor.histogram.maxValue", histoMaxVal);
 	maxVal = max((float)histoMaxVal, maxPvs);
 		
@@ -1831,6 +1833,8 @@
 		if (evaluateFilter) {
 		  ObjectPvs filteredPvs = viewcell->GetPvs();
-		  PvsFilterStatistics fstat = ApplyFilter2(viewcell, false,
-												   1.0f, filteredPvs);
+		  PvsFilterStatistics fstat = ApplyFilter2(viewcell,
+																							 false,
+																							 2.0f,
+																							 filteredPvs);
 		  
 		  float filteredCost = filteredPvs.EvalPvsCost();
@@ -1970,6 +1974,6 @@
 
 float ViewCellsManager::ComputeSampleContribution(VssRay &ray,
-												  const bool addRays,
-												  const bool storeViewCells)
+																									const bool addRays,
+																									const bool storeViewCells)
 {
 	ViewCellContainer viewcells;
@@ -2026,4 +2030,7 @@
 					++ ray.mPvsContribution;
 					ray.mRelativePvsContribution += contribution;
+					// $$ test of different contribution measure
+					//					ray.mRelativePvsContribution += 1.0f/(viewcell->GetPvs().GetSize() + 10.0f);
+
 				}
 			}
@@ -2037,10 +2044,10 @@
 			if (ray.mOriginObject && 
 				viewcell->GetPvs().GetSampleContribution(ray.mOriginObject,
-														 ray.mPdf,
-														 contribution))
-			{
-				++ ray.mPvsContribution;
-				ray.mRelativePvsContribution += contribution;
-			}
+																								 ray.mPdf,
+																								 contribution))
+				{
+					++ ray.mPvsContribution;
+					ray.mRelativePvsContribution += contribution;
+				}
 #endif
 		}
@@ -2582,10 +2589,11 @@
 PvsFilterStatistics
 ViewCellsManager::ApplyFilter2(ViewCell *viewCell,
-							   const bool useViewSpaceFilter,
-							   const float filterSize,
-							   ObjectPvs &pvs
-							   )
-{
-	cout<<"y";
+															 const bool useViewSpaceFilter,
+															 const float filterSize,
+															 ObjectPvs &pvs,
+															 vector<AxisAlignedBox3> *filteredBoxes
+															 )
+{
+	//cout<<"y";
   PvsFilterStatistics stats;
 
@@ -2601,8 +2609,8 @@
   // first mark all object from this pvs
   while (pit.HasMoreEntries()) {		
-	ObjectPvsEntry entry = pit.Next();
-	
-	Intersectable *object = entry.mObject;
-	object->Mail();
+		ObjectPvsEntry entry = pit.Next();
+		
+		Intersectable *object = entry.mObject;
+		object->Mail();
   }
 #endif
@@ -2625,6 +2633,6 @@
    // and gobal estimate for the view cell
    // (total #rays intersecting the viewcell)
-#define MIN_LOCAL_SAMPLES 5
-
+  int minLocalSamples = 2;
+  
   float viewCellRadius = 0.5f*Magnitude(vbox.Diagonal());
   
@@ -2632,85 +2640,98 @@
   
   if (useViewSpaceFilter) {
-	//	float radius = Max(viewCellRadius/100.0f, avgRadius - viewCellRadius);
-	float radius = viewCellRadius/100.0f;
-	vbox.Enlarge(radius);
-	cout<<"vbox = "<<vbox<<endl;
-	ViewCellContainer viewCells;
-	ComputeBoxIntersections(vbox, viewCells);
-	
-	ViewCellContainer::const_iterator it = viewCells.begin(),
-	  it_end = viewCells.end();
-	int i = 0;
-	for (i=0; it != it_end; ++ it, ++ i)
-	  if ((*it) != viewCell) {
-		//cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
-		basePvs.MergeInPlace((*it)->GetPvs());
-	  }
-	
-	// update samples and globalC
-	samples = (float)pvs.GetSamples();
-	//	cout<<"neighboring viewcells = "<<i-1<<endl;
-	//	cout<<"Samples' = "<<samples<<endl;
+		//	float radius = Max(viewCellRadius/100.0f, avgRadius - viewCellRadius);
+		float radius = viewCellRadius/100.0f;
+		vbox.Enlarge(radius);
+		cout<<"vbox = "<<vbox<<endl;
+		ViewCellContainer viewCells;
+		ComputeBoxIntersections(vbox, viewCells);
+		
+		ViewCellContainer::const_iterator it = viewCells.begin(),
+			it_end = viewCells.end();
+		int i = 0;
+		for (i=0; it != it_end; ++ it, ++ i)
+			if ((*it) != viewCell) {
+				//cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
+				basePvs.MergeInPlace((*it)->GetPvs());
+			}
+		
+		// update samples and globalC
+		samples = (float)pvs.GetSamples();
+		//	cout<<"neighboring viewcells = "<<i-1<<endl;
+		//	cout<<"Samples' = "<<samples<<endl;
   }
-
+	
   // Minimal number of samples so that filtering takes place
-#define MIN_SAMPLES  100
+#define MIN_SAMPLES  50
+
   if (samples > MIN_SAMPLES) {
-	float globalC = 2.0f*filterSize/sqrt(samples);
-	
-	pit = basePvs.GetIterator();
-	
-	ObjectContainer objects;
-	
-	while (pit.HasMoreEntries())
-	  {		
-		ObjectPvsEntry entry = pit.Next();
-		
-		Intersectable *object = entry.mObject;
-		// compute filter size based on the distance and the numebr of samples
-		AxisAlignedBox3 box = object->GetBox();
-		
-		float distance = Distance(center, box.Center());
-		float globalRadius = distance*globalC;
-		
-		int objectSamples = (int)entry.mData.mSumPdf;
-		float localRadius = MAX_FLOAT;
-		if (objectSamples > MIN_LOCAL_SAMPLES)
-		  localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/
-			sqrt((float)objectSamples);
-		
-		//	cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl;
-		
-		// now compute the filter size
-		float radius;
-
-		if (localRadius < globalRadius) {
-		  radius = localRadius;
-		  stats.mLocalFilterCount++;
-		} else {
-		  radius = globalRadius;
-		  stats.mGlobalFilterCount++;
-		}
-
-		stats.mAvgFilterRadius += radius;
-		
-		// cout<<"box = "<<box<<endl;
-		//	cout<<"distance = "<<distance<<endl;
-		//	cout<<"radiues = "<<radius<<endl;
-		
-		box.Enlarge(Vector3(radius));
-		
-		objects.clear();
-		// $$ warning collect objects takes only unmailed ones!
-		CollectObjects(box, objects);
-		//	cout<<"collected objects="<<objects.size()<<endl;
-		ObjectContainer::const_iterator noi = objects.begin();
-		for (; noi != objects.end(); ++ noi) {
-		  Intersectable *o = *noi;
-		  // $$ JB warning: pdfs are not correct at this point!	  
-		  pvs.AddSampleDirty(o, Limits::Small);
-		}
-	  }
-	stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount);
+		float globalC = 2.0f*filterSize/sqrt(samples);
+		
+		pit = basePvs.GetIterator();
+		
+		ObjectContainer objects;
+		
+		while (pit.HasMoreEntries())
+			{		
+				ObjectPvsEntry entry = pit.Next();
+				
+				Intersectable *object = entry.mObject;
+				// compute filter size based on the distance and the numebr of samples
+				AxisAlignedBox3 box = object->GetBox();
+				
+				float distance = Distance(center, box.Center());
+				float globalRadius = distance*globalC;
+				
+				int objectSamples = (int)entry.mData.mSumPdf;
+				float localRadius = MAX_FLOAT;
+				
+				localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/
+					sqrt((float)objectSamples);
+				
+				//	cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl;
+				
+				// now compute the filter size
+				float radius;
+
+#if 0
+				if (objectSamples <= 1) {
+					if (localRadius > globalRadius) {
+						radius = 0.5flRadius;
+						stats.mLocalFilterCount++;
+					} else {
+						radius = globalRadius;
+						stats.mGlobalFilterCount++;
+					}
+				} else {
+					radius = localRadius;
+					stats.mLocalFilterCount++;
+				}
+#else
+				radius = 0.5f*globalRadius + 0.5f*localRadius;
+				stats.mLocalFilterCount++;
+				stats.mGlobalFilterCount++;
+#endif
+				
+				stats.mAvgFilterRadius += radius;
+				
+				// cout<<"box = "<<box<<endl;
+				//	cout<<"distance = "<<distance<<endl;
+				//	cout<<"radiues = "<<radius<<endl;
+				
+				box.Enlarge(Vector3(radius));
+				if (filteredBoxes)
+					filteredBoxes->push_back(box);
+				objects.clear();
+				// $$ warning collect objects takes only unmailed ones!
+				CollectObjects(box, objects);
+				//	cout<<"collected objects="<<objects.size()<<endl;
+				ObjectContainer::const_iterator noi = objects.begin();
+				for (; noi != objects.end(); ++ noi) {
+					Intersectable *o = *noi;
+					// $$ JB warning: pdfs are not correct at this point!	  
+					pvs.AddSampleDirty(o, Limits::Small);
+				}
+			}
+		stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount);
   }
   
@@ -2721,6 +2742,6 @@
   pit = basePvs.GetIterator();
   while (pit.HasMoreEntries()) {		
-	ObjectPvsEntry entry = pit.Next();
-	pvs.AddSampleDirty(entry.mObject, entry.mData.mSumPdf);
+		ObjectPvsEntry entry = pit.Next();
+		pvs.AddSampleDirty(entry.mObject, entry.mData.mSumPdf);
   }
 #endif
