Index: GTP/trunk/Lib/Vis/Preprocessing/src/run_test2
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 1757)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 1771)
@@ -29,33 +29,19 @@
 VIEWCELLS=../data/vienna/vienna_cropped-gradient-viewcells.xml.gz
 
-PREFIX=../work/plots/osp-rss
+PREFIX=../work/plots/osp-rss-1e5
 
 #SCENE=../data/atlanta/atlanta2.x3d
 #PREFIX=../work/plots/atlanta2-rss9
 
-#$COMMAND -scene_filename=$SCENE -rss_use_importance+ -rss_update_subdivision-
-#mv stats.log $PREFIX-i-combined-b3.log
-
-#$COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
-#-rss_use_importance+ -rss_update_subdivision+ -rss_split=regular \
-#-view_cells_filter_max_size=1
- 
-#mv stats.log $PREFIX-i-combined-update-ccb3.log 
-
-# $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
-# -rss_use_importance+ -rss_update_subdivision+ -rss_split=heuristic \
-# -view_cells_filter_max_size=1
- 
-# mv stats.log $PREFIX-i-combined-update-ccb4.log 
 
 $COMMAND -preprocessor=sampling -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
--view_cells_filter_max_size=1
-mv stats.log $PREFIX-r-reference.log 
+-view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-reference.log \
+-preprocessor_histogram_file=$PREFIX-r-reference.hlog
 
 
 $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
  -rss_use_importance+ -rss_update_subdivision+ -rss_split=hybrid -hybrid_depth=10 \
- -view_cells_filter_max_size=1
- mv stats.log $PREFIX-i-combined-update-ccb12.log 
+ -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-i-combined-update-ccb12.log \
+-preprocessor_histogram_file=$PREFIX-i-combined-update-ccb12.hlog
 
 
@@ -65,21 +51,12 @@
 $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
 -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling+ \
--view_cells_filter_max_size=1
-mv stats.log $PREFIX-r-directional-based-b3.log
-
+-view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-directional-based-b3.log \
+-preprocessor_histogram_file=$PREFIX-r-directional-based-b3.hlog
 
 $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
 -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling- \
--view_cells_filter_max_size=1
-mv stats.log $PREFIX-r-spatial-based-bb3.log
+-view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-spatial-based-bb3.log \
+-preprocessor_histogram_file=$PREFIX-r-spatial-based-bb3.hlog
 
 
 
-$COMMAND -scene_filename=$SCENE -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling+
-mv stats.log $PREFIX-r-direction-based-b3.log
-
-
-
-#-vss_samples=500000000
-
-
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1563)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1771)
@@ -119,5 +119,4 @@
 	//////////////////////
 
-	ofstream mStats;
 
 	int mSamplesPerPass;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1761)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1771)
@@ -1710,4 +1710,9 @@
 					"sampling");
 
+	RegisterOption("Preprocessor.stats",
+					optString,
+					"preprocessor_stats=",
+					"stats.log");
+
 	RegisterOption("Preprocessor.samplesFilename",
 					optString,
@@ -1794,4 +1799,9 @@
 					"preprocessor_histogram_intervals=",
 					"20");
+
+	RegisterOption("Preprocessor.histogram.file", 
+				   optString,
+				   "preprocessor_histogram_file=",
+				   "histogram.log");
 
 	RegisterOption("Preprocessor.exportKdTree", 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1769)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1771)
@@ -151,6 +151,10 @@
 	char buffer[256];
 	Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile",  buffer);
-    
 	mVisibilityFileName = buffer;
+	
+	Environment::GetSingleton()->GetStringValue("Preprocessor.stats",  buffer);
+	mStats.open(buffer);
+	
+	
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter);
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
@@ -935,36 +939,38 @@
 }
 
-int Preprocessor::GenerateRays(const int number,
-							   const int sampleType,
-							   SimpleRayContainer &rays)
-{
-	const int startSize = (int)rays.size();
-	SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
-
-	if (!strategy)
-	{
-		return 0;
-	}
-
-	int castRays = 0;
-
-	for (int i = 0; (int)rays.size() - startSize < number; ++ i) 
-	{
-		SimpleRay newRay;
-
-		if (strategy->GenerateSample(newRay))
-		{
+
+int
+Preprocessor::GenerateRays(const int number,
+						   const SamplingStrategy &strategy,
+						   SimpleRayContainer &rays)
+{
+  return strategy.GenerateSamples(number, rays);
+}
+
+int
+Preprocessor::GenerateRays(const int number,
+						   const int sampleType,
+						   SimpleRayContainer &rays)
+{
+  const int startSize = (int)rays.size();
+  SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
+  int castRays = 0;
+  
+  if (!strategy)
+	{
+	  return 0;
+	}
+  
 #if 1
-			rays.AddRay(newRay);
-			++ castRays;
+  if (!strategy->GenerateSamples(number, rays))
+	return false;
+  ++ castRays;
 #else
-			GenerateRayBundle(rays, newRay, 16, 0);
-			castRays += 16;
+  GenerateRayBundle(rays, newRay, 16, 0);
+  castRays += 16;
 #endif
-		}	
-	}
-
-	delete strategy;
-    return castRays;
+  
+  delete strategy;
+  return castRays;
 }
 
@@ -1155,4 +1161,14 @@
 }
 
-
-}
+void
+Preprocessor::EvalViewCellHistogram()
+{
+  char filename[256];
+  Environment::GetSingleton()->GetStringValue("Preprocessor.histogram.file",
+											  filename);
+  
+  // mViewCellsManager->EvalViewCellHistogram(filename, 1000000);
+  mViewCellsManager->EvalViewCellHistogramForPvsSize(filename, 1000000);
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1765)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1771)
@@ -51,5 +51,4 @@
   Environment::GetSingleton()->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
   
-  mStats.open("stats.log");
   mRssTree = NULL;
 }
@@ -365,4 +364,41 @@
 
 void
+NormalizeRatios(vector<SamplingStrategy *> distributions)
+{
+  int i;
+  float sumRatios = 0.0f;
+  
+  for (i=0; i < distributions.size(); i++)
+	sumRatios += distributions[i]->mRatio;
+
+  
+  if (sumRatios == 0.0f) {
+	for (i=0; i < distributions.size(); i++) {
+	  distributions[i]->mRatio = 1.0f;
+	  sumRatios += 1.0f;
+	}
+  }
+
+  for (i=0 ; i < distributions.size(); i++)
+	distributions[i]->mRatio/=sumRatios;
+
+#define MIN_RATIO 0.1f
+  
+  for (i = 0; i < distributions.size(); i++)
+	if (distributions[i]->mRatio < MIN_RATIO)
+	  distributions[i]->mRatio = MIN_RATIO;
+
+
+  sumRatios = 0.0f;
+  for (i=0; i < distributions.size(); i++)
+	sumRatios += distributions[i]->mRatio;
+
+  for (i=0 ; i < distributions.size(); i++)
+	distributions[i]->mRatio/=sumRatios;
+  
+
+}
+
+void
 NormalizeRatios(float ratios[4])
 {
@@ -432,8 +468,8 @@
   int rssSamples = 0;
 
-  mDistributions.push_back(SamplingDistribution(SamplingStrategy::RSS_BASED_DISTRIBUTION));
-  mDistributions.push_back(SamplingDistribution(SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION));
-  mDistributions.push_back(SamplingDistribution(SamplingStrategy::DIRECTION_BASED_DISTRIBUTION));
-  mDistributions.push_back(SamplingDistribution(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION));
+  mDistributions.push_back(new RssBasedDistribution(*this));
+  mDistributions.push_back(new SpatialBoxBasedDistribution(*this));
+  mDistributions.push_back(new DirectionBasedDistribution(*this));
+  mDistributions.push_back(new ReverseObjectBasedDistribution(*this));
   
   if (mLoadInitialSamples) {
@@ -871,5 +907,7 @@
 	// view cells after sampling
 	mViewCellsManager->PrintStatistics(Debug);
-	
+
+	EvalViewCellHistogram();
+
 	//-- render simulation after merge
 	cout << "\nevaluating bsp view cells render time after sampling ... ";
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1743)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1771)
@@ -47,5 +47,4 @@
   Debug << "*********** end vss preprocessor options **************" << endl;
 
-  mStats.open("stats.log");
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1761)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1771)
@@ -63,4 +63,10 @@
 	loadKdTree	false
 	exportKdTree false
+
+	histogram {
+		intervals 20
+		maxValue  1000
+	    file	histogram.log
+	}
 }
 
@@ -76,5 +82,5 @@
 SamplingPreprocessor {
 	totalSamples 50000000
-	samplesPerPass 3000000
+	samplesPerPass 5000000
 }
 
@@ -84,5 +90,5 @@
 	vssSamples 50000000
 #	vssSamples 1000000
-	vssSamplesPerPass 3000000
+	vssSamplesPerPass 5000000
 	useImportanceSampling true
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1765)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1771)
@@ -121,18 +121,23 @@
 	bool ExportKdTree(const string filename);
 
-	virtual int GenerateRays(const int number,
-							 const int raysType,
-							 SimpleRayContainer &rays);
-
-	bool GenerateRayBundle(SimpleRayContainer &rayBundle,
-						   const SimpleRay &mainRay,
-						   const int number,
-						   const int shuffleType) const;
-
-	virtual void CastRays(SimpleRayContainer &rays,
-						  VssRayContainer &vssRays,
-						  const bool castDoubleRays,
-						  const bool pruneInvalidRays = true);
-
+  virtual int
+  GenerateRays(const int number,
+			   const SamplingStrategy &strategy,
+			   SimpleRayContainer &rays);
+  
+  virtual int GenerateRays(const int number,
+						   const int raysType,
+						   SimpleRayContainer &rays);
+  
+  bool GenerateRayBundle(SimpleRayContainer &rayBundle,
+						 const SimpleRay &mainRay,
+						 const int number,
+						 const int shuffleType) const;
+  
+  virtual void CastRays(SimpleRayContainer &rays,
+						VssRayContainer &vssRays,
+						const bool castDoubleRays,
+						const bool pruneInvalidRays = true);
+  
 	/** Returns a view cells manager of the given name.
 	*/
@@ -197,4 +202,6 @@
 	bool mExportObj;
 
+  ofstream mStats;
+
 protected:
 
@@ -209,4 +216,7 @@
 
 	void EvalPvsStat();
+
+  virtual void
+  EvalViewCellHistogram();
 
 	/////////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 1769)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 1771)
@@ -8,4 +8,5 @@
 class Preprocessor;
 struct SimpleRay;
+class SimpleRayContainer;
 
 /** This class generates a specific sampling strategy.
@@ -31,20 +32,33 @@
 		 VIEWCELL_BORDER_BASED_DISTRIBUTION
 	 };
+  
+  /** Default constructor
+   */
+  SamplingStrategy(const Preprocessor &preprocessor);
+  
+  virtual ~SamplingStrategy();
+  
+  /** Each strategy has to implement this function.
+	  @returns true if generated valid sample.
+  */
 
-	 /** Default constructor
-     */
-	 SamplingStrategy(const Preprocessor &preprocessor);
+  virtual int GenerateSamples(const int number, SimpleRayContainer &rays) const;
 
-	 virtual ~SamplingStrategy();
+private:
 
-	 /** Each strategy has to implement this function.
-		 @returns true if generated valid sample.
-	 */
-	 virtual bool GenerateSample(SimpleRay &ray) const = 0;
+  virtual bool GenerateSample(SimpleRay &ray) const = 0;
 
+public:
+  /// variables usefull for mixed distribution sampling
+  int mType;
+  int mRays;
+  float mContribution;
+  float mTime;
+  float mRatio;
+  
 protected:
-
-	const Preprocessor &mPreprocessor;
-
+  
+  const Preprocessor &mPreprocessor;
+  
 };
 
@@ -54,8 +68,11 @@
  public:
 	
-	 ObjectBasedDistribution(const Preprocessor &preprocessor):
-	 SamplingStrategy(preprocessor) {}
+  ObjectBasedDistribution(const Preprocessor &preprocessor):
+	SamplingStrategy(preprocessor) {
+	mType = OBJECT_BASED_DISTRIBUTION;
+  }
 
-	 virtual bool GenerateSample(SimpleRay &ray) const;
+private:
+  virtual bool GenerateSample(SimpleRay &ray) const;
 };
 
@@ -66,7 +83,10 @@
 	
 	 ReverseObjectBasedDistribution(const Preprocessor &preprocessor):
-	 SamplingStrategy(preprocessor) {}
+	   SamplingStrategy(preprocessor) {
+	   mType = REVERSE_OBJECT_BASED_DISTRIBUTION;
+	 }
 
-	 virtual bool GenerateSample(SimpleRay &ray) const;
+private:
+  virtual bool GenerateSample(SimpleRay &ray) const;
 };
 
@@ -76,5 +96,8 @@
  public:
 	 ObjectDirectionBasedDistribution(const Preprocessor &preprocessor):
-	 SamplingStrategy(preprocessor) {}
+	 SamplingStrategy(preprocessor) {
+	   mType = OBJECT_DIRECTION_BASED_DISTRIBUTION;
+	 }
+private:
 	virtual bool GenerateSample(SimpleRay &ray) const;
 };
@@ -84,7 +107,10 @@
 {
  public:
-	DirectionBasedDistribution(const Preprocessor &preprocessor):
-	 SamplingStrategy(preprocessor) {}
-	virtual bool GenerateSample(SimpleRay &ray) const;
+  DirectionBasedDistribution(const Preprocessor &preprocessor):
+	SamplingStrategy(preprocessor){
+	mType = DIRECTION_BASED_DISTRIBUTION;
+  }
+private:
+  virtual bool GenerateSample(SimpleRay &ray) const;
 };
 
@@ -94,6 +120,9 @@
  public:
 	 DirectionBoxBasedDistribution(const Preprocessor &preprocessor):
-	   SamplingStrategy(preprocessor) {}
+	   SamplingStrategy(preprocessor){
+	   mType = DIRECTION_BOX_BASED_DISTRIBUTION;
+	 }
 	   
+private:
 	   virtual bool GenerateSample(SimpleRay &ray) const;
 };
@@ -104,17 +133,38 @@
  public:
 	 SpatialBoxBasedDistribution(const Preprocessor &preprocessor):
-	 SamplingStrategy(preprocessor) {}
+	   SamplingStrategy(preprocessor){
+	   mType = SPATIAL_BOX_BASED_DISTRIBUTION;
+	 }
 	 
+private:
 	 virtual bool GenerateSample(SimpleRay &ray) const;
 };
 
+class RssBasedDistribution: public SamplingStrategy
+{
+ public:
+  RssBasedDistribution(const Preprocessor &preprocessor):
+	SamplingStrategy(preprocessor){
+	mType = RSS_BASED_DISTRIBUTION;
+  }
+
+
+  virtual int GenerateSamples(const int number, SimpleRayContainer &ray) const {
+	// TBD!!!
+	return 0;
+  }
+
+private:
+  virtual bool GenerateSample(SimpleRay &ray) const;
+  
+};
 
 class ViewCellBorderBasedDistribution: public SamplingStrategy
 {
  public:
-	 ViewCellBorderBasedDistribution(const Preprocessor &preprocessor):
-	 SamplingStrategy(preprocessor) {}
-	 
-	 virtual bool GenerateSample(SimpleRay &ray) const;
+  ViewCellBorderBasedDistribution(const Preprocessor &preprocessor):
+	SamplingStrategy(preprocessor) {}
+  
+  virtual bool GenerateSample(SimpleRay &ray) const;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1761)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1771)
@@ -22,5 +22,4 @@
   Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples);
   
-  mStats.open("stats.log");
 }
 
@@ -125,9 +124,10 @@
 	  }
 		
-	  for (int j=0; j < vssRays.size(); j++) {
-		Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
-																 true);
-		if (obj) {
-		  // if ray not outside of view space
+	  for (int j=0; j < vssRays.size(); j++)
+		if (vssRays[j]->mFlags & VssRay::Valid) {
+		  Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
+																   true);
+		  if (obj) {
+			// if ray not outside of view space
 			float contribution;
 			int pvsContribution = 0;
@@ -141,6 +141,6 @@
 			relativePvsContribution += contribution;
 			viewcell->GetPvs().AddSample(obj, pdf);
+		  }
 		}
-	  }
 	  
 	  CLEAR_CONTAINER(vssRays);
@@ -180,4 +180,6 @@
   mViewCellsManager->PrintStatistics(Debug);
   
+  EvalViewCellHistogram();
+
   //-- render simulation after merge
   cout << "\nevaluating bsp view cells render time after merge ... ";
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 1765)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 1771)
@@ -14,13 +14,4 @@
 class RssTreeLeaf;
 
-  class SamplingDistribution {
-  public:
-	SamplingDistribution() {}
-	SamplingDistribution(const int t):mType(t) {}
-	int mType;
-	int mRays;
-	float mContribution;
-	float mTime;
-  };
   
 /** Sampling based visibility preprocessing. The implementation is 
@@ -30,5 +21,5 @@
   
 public:
-  vector<SamplingDistribution> mDistributions;
+  vector<SamplingStrategy *> mDistributions;
   int mSamplesPerPass;
   int mRssSamplesPerPass;
@@ -51,5 +42,4 @@
 
   
-  ofstream mStats;
   RssTree *mRssTree;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.h	(revision 1723)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.h	(revision 1771)
@@ -28,5 +28,4 @@
   //AxisAlignedBox3 mViewSpaceBox;
 
-  ofstream mStats;
 	
   ObjectContainer mObjects;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1769)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1771)
@@ -34,5 +34,5 @@
 // $$JB HACK
 #define USE_KD_PVS 0
-#define KD_PVS_AREA (1e-4f)
+#define KD_PVS_AREA (1e-5f)
 
 
@@ -599,5 +599,10 @@
 
 	ViewCellContainer viewCells;
+	// $$ JB hack - the collect best viewcells does not work?
+#if 1
 	mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
+#else
+	viewCells = mViewCells;
+#endif
 
 	float maxRenderCost, minRenderCost;
@@ -823,12 +828,24 @@
 
 	ViewCellContainer viewCells;
+	
+	// $$ JB hack - the collect best viewcells does not work?
+#if 0
 	mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
+#else
+	viewCells = mViewCells;
+#endif
+	ViewCellContainer::iterator it = viewCells.begin(), it_end = viewCells.end();		
+	for (; it != it_end; ++it) {
+	  //(*it)->UpdatePvsCost();
+	  (*it)->SetPvsCost((*it)->GetFilteredPvsSize());
+	}
+	
 
 	float maxPvs, maxVal, minVal;
-
+	
 	// sort by pvs size
 	sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerPvs);
 
-	maxPvs = mViewCellsTree->GetPvsCost(viewCells.back());
+	maxPvs = viewCells.back()->GetPvsCost();
 	minVal = 0;
 
@@ -863,5 +880,5 @@
 	int smallerSum = 0;
 
-	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();		
+	it = viewCells.begin();
 	
 	for (int j = 0; j < intervals; ++ j)
@@ -871,5 +888,5 @@
 
 		while ((i < (int)viewCells.size()) && 
-			   (mViewCellsTree->GetPvsCost(viewCells[i]) < currentPvs))
+			   (viewCells[i]->GetPvsCost() < currentPvs))
 		{
 			volDif += viewCells[i]->GetVolume();
@@ -881,8 +898,8 @@
 		}
 		
-		if (0 && (i < (int)viewCells.size()))
-			Debug << "new pvs cost increase: " << mViewCellsTree->GetPvsCost(viewCells[i]) 
-			<< " " << currentPvs << endl;
-	
+		//		if (0 && (i < (int)viewCells.size()))
+		//		  Debug << "new pvs cost increase: " << mViewCellsTree->GetPvsCost(viewCells[i]) 
+		//				<< " " << currentPvs << endl;
+		
 		const float volRatioDif = volDif / totalVol;
 		const float volRatioSum = volSum / totalVol;
@@ -900,4 +917,5 @@
 
 	outstream.close();
+
 }
 
@@ -1804,5 +1822,9 @@
 
 		  stat.avgFilterRadius += fstat.mAvgFilterRadius;
-		  stat.avgFilterRatio += fstat.mLocalFilterCount / (float) fstat.mGlobalFilterCount;
+		  int sum = fstat.mGlobalFilterCount + fstat.mLocalFilterCount;
+		  if (sum) {
+			stat.avgFilterRatio += fstat.mLocalFilterCount /
+			  (float) sum;
+		  }
 		  
 		  
@@ -2578,5 +2600,5 @@
   float samples = (float)basePvs.GetSamples();
   
-  cout<<"f #s="<<samples<<"pvs size = "<<basePvs.GetSize();
+  Debug<<"f #s="<<samples<<"pvs size = "<<basePvs.GetSize();
   //  cout<<"Filter size = "<<filterSize<<endl;
   //  cout<<"vbox = "<<vbox<<endl;
@@ -2681,5 +2703,5 @@
   }
   
-  cout<<" nPvs size = "<<pvs.GetSize()<<endl;
+  Debug<<" nPvs size = "<<pvs.GetSize()<<endl;
 
 #if !USE_KD_PVS
@@ -2691,4 +2713,5 @@
   }
 #endif
+  viewCell->SetFilteredPvsSize(pvs.GetSize());
   
   Intersectable::NewMail();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1761)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1771)
@@ -108,5 +108,6 @@
 	
 	int mPvsSizeDecr;
-	
+
+  
 	float mVolume;
 
@@ -271,6 +272,13 @@
 	float GetMergeCost() const;
 
-
-  float GetCachedPvsCost() const {
+  void UpdatePvsCost() {
+	mPvsCost = GetPvs().EvalPvsCost();
+  }
+
+  void SetPvsCost(const float c) {
+	mPvsCost = c;
+  }
+
+  float GetPvsCost() const {
 	return mPvsCost;
   }
@@ -299,4 +307,11 @@
 	static int sReservedMailboxes;
 	
+  int GetFilteredPvsSize() const {
+	return mFilteredPvsSize;
+  }
+
+  void SetFilteredPvsSize(const int s) {
+	mFilteredPvsSize = s;
+  }
 
 protected:
@@ -327,4 +342,8 @@
 	*/
 	bool mPvsSizeValid;
+
+  /// Filter cost of the pvs
+  int mFilteredPvsSize;
+
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1715)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1771)
@@ -24,5 +24,7 @@
     FPosDirZ = 4,    // the direction of ray in Z-axis is positive
 	BorderSample = 8,// if this ray is an adaptive border ray
-	ReverseSample = 16  // if this ray is a reverse sample
+	ReverseSample = 16,  // if this ray is a reverse sample
+	Valid = 32  // this ray is a valid ray
+	           //(with respect to detect empty viewspace)
   };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1757)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1771)
@@ -96,5 +96,4 @@
 		return 0;
 	  }
-	  
 	}
 	
@@ -121,10 +120,10 @@
 	const bool validSample = !pruneInvalidRays || (hitA.mObject != hitB.mObject);
 
+	
 #if DEBUG_RAYCAST
 	Debug<<"PR2"<<flush;
 #endif
 
-	if (validSample) 
-	{
+	if (validSample) {
 	  if (!pruneInvalidRays || hitA.mObject) 
 		{
@@ -137,5 +136,6 @@
 									  simpleRay.mPdf
 									  );
-		  
+		  if (validA)
+			vssRay->mFlags |= VssRay::Valid;
 		  vssRays.push_back(vssRay);
 		  ++ hits;
@@ -157,4 +157,6 @@
 									  simpleRay.mPdf
 									  );
+		  if (validB)
+			vssRay->mFlags |= VssRay::Valid;
 		  
 		  vssRays.push_back(vssRay);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.h	(revision 1743)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.h	(revision 1771)
@@ -50,5 +50,4 @@
 	int mTotalSamples;
 	
-	ofstream mStats;
 	ObjectContainer mObjects;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1769)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1771)
@@ -20,4 +20,18 @@
 }
 
+int
+SamplingStrategy::GenerateSamples(const int number,
+								  SimpleRayContainer &rays) const
+{
+  SimpleRay ray;
+  int i = 0;
+  for (; i < number; i++) {
+	if (!GenerateSample(ray))
+	  return i;
+	rays.push_back(ray);
+  }
+
+  return i;
+}
 
 
@@ -264,3 +278,12 @@
 
 #endif
-}
+
+bool
+RssBasedDistribution::GenerateSample(SimpleRay &ray) const
+{
+  return false;
+}
+
+}
+
+
