Index: GTP/trunk/Lib/Vis/Preprocessing/src/run_test2
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 1942)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 1966)
@@ -29,5 +29,5 @@
 VIEWCELLS=../data/vienna/vienna_cropped-gradient-viewcells.xml.gz
 
-PREFIX=../work/plots/osp-rss2-1e5
+PREFIX=../work/plots/osp2-1e5
 
 #SCENE=../data/atlanta/atlanta2.x3d
@@ -35,37 +35,21 @@
 
 
-#$COMMAND -preprocessor=sampling -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
-#-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_update_subdivision+ -rss_split=hybrid -hybrid_depth=10 \
-#-view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-i-combined-update-ccb12-nn.log \
-#-preprocessor_histogram_file=$PREFIX-i-combined-update-ccb12-nn.hlog
+$COMMAND -preprocessor=sampling -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
+-preprocessor_visibility_file=$PREFIX-r-reference.xml \
+-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_update_subdivision- -rss_split=hybrid -hybrid_depth=10 \
-# -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-i-combined-ccb12-nn.log \
-# -preprocessor_histogram_file=$PREFIX-i-combined-ccb12-nn22.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 -preprocessor_stats=$PREFIX-r-directional-based-b3.log \
-# -preprocessor_histogram_file=$PREFIX-r-directional-based-b3.hlog
+# -rss_distributions=direction -view_cells_filter_max_size=1 \
+# -preprocessor_visibility_file=$PREFIX-r-reference-global.xml \
+# -preprocessor_stats=$PREFIX-r-reference-global.log \
+# -preprocessor_histogram_file=$PREFIX-r-reference-global.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 -preprocessor_stats=$PREFIX-r-spatial-based-bb3.log \
-# -preprocessor_histogram_file=$PREFIX-r-spatial-based-bb3.hlog
-
-
-$COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
--rss_distributions=rss+object_direction+spatial+object+direction -view_cells_filter_max_size=1 \
--preprocessor_stats=$PREFIX-i-mixed-b1-n4.log \
--preprocessor_histogram_file=$PREFIX-i-mixed-b1-n4.hlog
+# -rss_distributions=mutation+object_direction+spatial -view_cells_filter_max_size=1 \
+# -preprocessor_visibility_file=$PREFIX-i-mixed-b1-n4c.xml \
+# -preprocessor_stats=$PREFIX-i-mixed-b1-n4c.log \
+# -preprocessor_histogram_file=$PREFIX-i-mixed-b1-n4c.hlog
 
 # $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
@@ -75,8 +59,3 @@
 
 
-# $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
-#  -rss_use_importance+ -rss_use_rss_tree- -view_cells_filter_max_size=1 \
-#  -rss_initial_samples=500000 -rss_vss_samples_per_pass=500000 -preprocessor_ray_cast_method=0 \
-#   -preprocessor_stats=$PREFIX-i-global-b1.log \
-#  -preprocessor_histogram_file=$PREFIX-i-global-b1.hlog
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1942)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1966)
@@ -1173,13 +1173,18 @@
 		 "0.1");
 
-  RegisterOption("SamplingPreprocessor.totalSamples",
-		 optInt,
-		 "total_samples=",
-		 "1000000");
-
-  RegisterOption("SamplingPreprocessor.samplesPerPass",
-		 optInt,
-		 "samples_per_pass=",
-		 "10");
+  RegisterOption("Preprocessor.totalSamples",
+				 optInt,
+				 "total_samples=",
+				 "10000000");
+
+  RegisterOption("Preprocessor.samplesPerPass",
+				 optInt,
+				 "samples_per_pass=",
+				 "100000");
+  
+  RegisterOption("Preprocessor.samplesPerEvaluation",
+				 optInt,
+				 "samples_per_evaluation=",
+				 "1000000");
 
   RegisterOption("RenderSampler.samples",
@@ -1199,8 +1204,8 @@
 				 "true");
 
-   RegisterOption("VssPreprocessor.initialSamples",
-				 optInt,
-				 "vss_initial_samples=",
-				 "100000");
+//    RegisterOption("VssPreprocessor.initialSamples",
+// 				 optInt,
+// 				 "vss_initial_samples=",
+// 				 "100000");
   
   RegisterOption("VssPreprocessor.testBeamSampling", 
@@ -1209,18 +1214,18 @@
 				"false");
 
-  RegisterOption("VssPreprocessor.vssSamples",
-				 optInt,
-				 "vss_samples=",
-				 "1000000");
-	
-  RegisterOption("VssPreprocessor.vssSamplesPerPass",
-				 optInt,
-				 "vss_samples_per_pass=",
-				 "1000");
-  
-  RegisterOption("VssPreprocessor.samplesPerPass",
-				 optInt,
-				 "vss_samples_per_pass=",
-				 "100000");
+//   RegisterOption("VssPreprocessor.vssSamples",
+// 				 optInt,
+// 				 "vss_samples=",
+// 				 "1000000");
+	
+//   RegisterOption("VssPreprocessor.vssSamplesPerPass",
+// 				 optInt,
+// 				 "vss_samples_per_pass=",
+// 				 "1000");
+  
+//   RegisterOption("VssPreprocessor.samplesPerPass",
+// 				 optInt,
+// 				 "vss_samples_per_pass=",
+// 				 "100000");
 
   RegisterOption("VssPreprocessor.useImportanceSampling",
@@ -1896,23 +1901,23 @@
 
 
-	RegisterOption("RssPreprocessor.initialSamples",
-									optInt,
-									"rss_initial_samples=",
-									"100000");
-
-	RegisterOption("RssPreprocessor.vssSamples",
-					optInt,
-					"rss_vss_samples=",
-					"1000000");
-
-	RegisterOption("RssPreprocessor.vssSamplesPerPass",
-					optInt,
-					"rss_vss_samples_per_pass=",
-					"1000");
-
-	RegisterOption("RssPreprocessor.samplesPerPass",
-					optInt,
-					"rss_samples_per_pass=",
-					"100000");
+// 	RegisterOption("RssPreprocessor.initialSamples",
+// 									optInt,
+// 									"rss_initial_samples=",
+// 									"100000");
+
+// 	RegisterOption("RssPreprocessor.vssSamples",
+// 					optInt,
+// 					"rss_vss_samples=",
+// 					"1000000");
+
+// 	RegisterOption("RssPreprocessor.vssSamplesPerPass",
+// 					optInt,
+// 					"rss_vss_samples_per_pass=",
+// 					"1000");
+
+// 	RegisterOption("RssPreprocessor.samplesPerPass",
+// 					optInt,
+// 					"rss_samples_per_pass=",
+// 					"100000");
 
 	RegisterOption("RssPreprocessor.useImportanceSampling",
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1960)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1966)
@@ -168,4 +168,11 @@
 	Environment::GetSingleton()->GetIntValue("Preprocessor.Export.numRays", mExportNumRays);
 
+
+	Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerPass", mSamplesPerPass);
+	Environment::GetSingleton()->GetIntValue("Preprocessor.totalSamples", mTotalSamples);
+	Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerEvaluation",
+											 mSamplesPerEvaluation);
+
+	
 	Debug << "******* Preprocessor Options **********" << endl;
 	Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1931)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1966)
@@ -29,8 +29,8 @@
 {
   // this should increase coherence of the samples
-  Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
-  Environment::GetSingleton()->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
-  Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
-  Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
+  //  Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
+  //Environment::GetSingleton()->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
+  //Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
+  //Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
   Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1883)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1966)
@@ -29,8 +29,8 @@
 {
   // this should increase coherence of the samples
-  Environment::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
-  Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
-  Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
-  Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
+  //  Environment::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
+  //  Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
+  //  Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
+  //  Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
   Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 1948)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 1966)
@@ -692,5 +692,23 @@
 	updateGL();
 	break;
-
+  case Qt::Key_S: {
+	// set view poitn and direction
+	QString text;
+	bool ok;
+	text.sprintf("%f %f %f", mViewPoint.x, mViewPoint.y, mViewPoint.z);
+	text = QInputDialog::getText(this,
+								 "Enter a view point",
+								 "",
+								 QLineEdit::Normal,
+								 text,
+								 &ok);
+	if (!ok)
+	  break;
+	QTextStream ts(&text);
+	ts>>mViewPoint.x>>mViewPoint.y>>mViewPoint.z;
+	mViewCellsManager->GetViewPoint(mViewPoint);
+	updateGL();
+	break;
+  }
   default:
 	e->ignore();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1952)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1966)
@@ -37,4 +37,9 @@
 
 Preprocessor {
+	totalSamples 250000000
+	samplesPerPass 1000000
+#	initialSamples 2000000
+	samplesPerEvaluation 5000000
+
 	delayVisibilityComputation false
 	# stored sample rays
@@ -69,9 +74,9 @@
 	histogram {
 		intervals 20
-		maxValue  1000
+		maxValue  600000
 	    file	histogram.log
 	}
 	Export {
-		rays false
+		rays true
 		numRays 5000
 	}
@@ -89,6 +94,4 @@
 
 SamplingPreprocessor {
-	totalSamples 100000000
-	samplesPerPass 5000000
 }
 
@@ -98,12 +101,8 @@
 #	distributions rss+spatial+object_direction
 #	distributions rss+object_direction+spatial+object+direction
-	distributions object_direction+spatial+object+direction
+	distributions mutation+spatial+object_direction
 #	distributions rss+object_direction
 #	distributions object_direction
 
-	samplesPerPass 1000000
-	initialSamples 2000000
-	vssSamples 500000000
-	vssSamplesPerPass 5000000
 	useImportanceSampling true
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1958)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1966)
@@ -233,5 +233,8 @@
 	GlRendererBuffer *renderer;
   
-  
+	int mTotalSamples;
+  int mSamplesPerPass;
+  int mSamplesPerEvaluation;
+
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 1964)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 1966)
@@ -79,4 +79,5 @@
   
   int mTotalRays;
+  int mGeneratedRays;
   float mTotalContribution;
   
@@ -231,10 +232,13 @@
 private:
   virtual bool GenerateSample(SimpleRay &ray);
-  static HaltonSequence sHalton;
+
   struct RayEntry {
+	// halton sequence for generatin gmutations of this ray
 	VssRay *mRay;
 	int mSamples;
-	RayEntry();
-	RayEntry(VssRay *r):mRay(r), mSamples(0) {}
+	HaltonSequence mHalton;
+
+	RayEntry() {}
+	RayEntry(VssRay *r):mRay(r), mSamples(0), mHalton() {}
   };
   
@@ -296,5 +300,5 @@
   // container for the distributions
   vector<SamplingStrategy *> mDistributions;
-
+  
   MixtureDistribution(Preprocessor &preprocessor):
 	SamplingStrategy(preprocessor)
@@ -330,5 +334,7 @@
 	return false;
   }
-
+  
+  virtual int GenerateSamples(const int number, SimpleRayContainer &rays);
+  
 private:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1900)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1966)
@@ -20,6 +20,4 @@
 {
   // this should increase coherence of the samples
-  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.samplesPerPass", mSamplesPerPass);
-  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples);
   
 }
@@ -87,36 +85,45 @@
   int intersectables, faces;
   mSceneGraph->GetStatistics(intersectables, faces);
-  HaltonSequence halton;
+  HaltonSequence posHalton;
   
   int samples = 0;
-  int i=0;
+  int rssSamples = 0;
+
+  map<ViewCell *, HaltonSequence> dirHalton;
+  
   while (samples < mTotalSamples) {
-	for (i=0; i < mSamplesPerPass;) {
+	for (int i=0; i < mSamplesPerEvaluation;) {
 	  SimpleRayContainer rays;
 	  VssRayContainer vssRays;
-	  vector<ViewCell *> viewcells;
+	  //	  vector<ViewCell *> viewcells;
+	  
+	  float r[5];
+	  Vector3 origin, direction;
+	  posHalton.GetNext(3, r);
+
+	  mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
+	  ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
+	  
+	  if (!viewcell || !viewcell->GetValid())
+		continue;
+	  
 	  for (; rays.size() < 16; ) {
-		if (i%10000 == 0)
+		if (i%100000 == 0)
 		  cout<<"+";
-
-		float r[5];
-		halton.GetNext(5, r);
-		Vector3 origin, direction;
-		mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
 		
-		direction = UniformRandomVector(r[3],r[4]);
-		ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
+		//		dirHalton.GetNext(2, r);
+		dirHalton[viewcell].GetNext(2, r);
+		direction = UniformRandomVector(r[0],r[1]);
+		//direction = UniformRandomVector();
 		
-		if (viewcell && viewcell->GetValid()) {
-		  viewcells.push_back(viewcell);
-		  // cast rays in both directions to make the number of samples comparable
-		  // with the global sampling method which also casts a "double" ray per sample
-		  rays.push_back(SimpleRay(origin,
-								   direction,
-								   SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
-								   1.0f));
-		  i++;
-		  samples++;
-		}
+		//		viewcells.push_back(viewcell);
+		// cast rays in both directions to make the number of samples comparable
+		// with the global sampling method which also casts a "double" ray per sample
+		rays.push_back(SimpleRay(origin,
+								 direction,
+								 SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
+								 1.0f));
+		i++;
+		samples++;
 	  }
 	  
@@ -124,11 +131,12 @@
 			   vssRays,
 			   true,
-			   false);
-
-	  if (vssRays.size()!=32) {
-		cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
-		exit(1);
-	  }
-		
+			   true);
+	  
+	  // 	  if (vssRays.size()!=32) {
+	  // 		cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
+	  // 		exit(1);
+	  // 	  }
+
+	  rssSamples+=vssRays.size();
 	  for (int j=0; j < vssRays.size(); j++)
 		if (vssRays[j]->mFlags & VssRay::Valid) {
@@ -137,20 +145,18 @@
 		  if (obj) {
 			// if ray not outside of view space
-			float contribution;
-			int pvsContribution = 0;
-			float relativePvsContribution = 0;
 			float pdf = 1.0f;
-			ViewCell *viewcell = viewcells[j/2];
-			if (viewcell->GetPvs().GetSampleContribution(obj,
-														 pdf,
-														 contribution)) 
-			  ++pvsContribution;
-			relativePvsContribution += contribution;
-			viewcell->GetPvs().AddSample(obj, pdf);
+			//			ViewCell *viewcell = viewcells[j/2];
+
+			ObjectPvs &pvs = viewcell->GetPvs();
+			pvs.AddSampleDirtyCheck(obj, pdf);
+			
+			if (pvs.RequiresResort())  {
+			  pvs.SimpleSort();
+			}
 		  }
 		}
 	  
 	  CLEAR_CONTAINER(vssRays);
-
+	  
 	  if (samples > mTotalSamples)
 		break;
@@ -166,6 +172,7 @@
 	mStats <<
 	  "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
-	  "#TotalSamples\n" <<samples<<endl;
-
+	  "#TotalSamples\n" <<samples<<endl<<
+	  "#RssSamples\n" <<rssSamples<<endl;
+		  
 	mViewCellsManager->PrintPvsStatistics(mStats);
 	// ComputeRenderError();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1952)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1966)
@@ -697,5 +697,5 @@
 	int histoMaxVal;
 	Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
-	maxRenderCost = max((float)histoMaxVal, maxRenderCost);
+	maxRenderCost = min((float)histoMaxVal, maxRenderCost);
 
 	
@@ -2039,5 +2039,5 @@
 		  PvsFilterStatistics fstat = ApplyFilter2(viewcell,
 												   false,
-												   4.0f,
+												   1.0f,
 												   filteredPvs);
 		  
@@ -2305,6 +2305,11 @@
 	{	
 	  // copy viewcells memory efficiently
+#if VSS_STORE_VIEWCELLS
 	  ray.mViewCells.reserve(viewCells.size());
 	  ray.mViewCells = viewCells;
+#else
+	  cerr<<"Vss store viewcells not supported."<<endl;
+	  exit(1);
+#endif
 	}
 
@@ -2966,6 +2971,9 @@
 #else
 	  radius = 0.5f*globalRadius + 0.5f*localRadius;
-	  stats.mLocalFilterCount++;
-	  stats.mGlobalFilterCount++;
+
+	  if (localRadius > globalRadius) 
+		stats.mLocalFilterCount++;
+	  else
+		stats.mGlobalFilterCount++;
 #endif
 	  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1952)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1966)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: ne 7. I 23:22:46 2007
+# Generated by qmake (2.00a) (Qt 4.1.2) on: st 10. I 21:30:27 2007
 # Project:  preprocessor.pro
 # Template: app
Index: GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.h	(revision 1891)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.h	(revision 1966)
@@ -27,8 +27,4 @@
   MixtureDistribution *mMixtureDistribution;
   
-  int mTotalSamples;
-  int mSamplesPerPass;
-  int mSamplesPerEvaluation;
-  
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1900)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1966)
@@ -15,5 +15,6 @@
 
 #define ABS_CONTRIBUTION_WEIGHT 0.0f
-
+#define VSS_STORE_VIEWCELLS 1
+  
 class VssRay {
 public:
@@ -59,6 +60,7 @@
   Intersectable *mTerminationObject;
 
+#if VSS_STORE_VIEWCELLS
   ViewCellContainer mViewCells;
-  
+#endif
 
   ////////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1952)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1966)
@@ -142,10 +142,13 @@
 		return 0;
 	  }
-	  if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
-		return 0;
-	  }
-	}
+	}
+
 	
 	// regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
+	if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
+	  return 0;
+	}
+	
+	
 	const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
 	const bool validB = //castDoubleRay && 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.h	(revision 1771)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.h	(revision 1966)
@@ -47,6 +47,4 @@
 
 	int mPass;
-	int mSamplesPerPass;
-	int mTotalSamples;
 	
 	ObjectContainer mObjects;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 1942)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 1966)
@@ -16,5 +16,5 @@
 namespace GtpVisibilityPreprocessor {
 
-const bool pruneInvalidRays = false;
+const bool pruneInvalidRays = true;
 
   
@@ -23,8 +23,8 @@
   {
 	// this should increase coherence of the samples
-	Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mTotalSamples);
-	Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
-	Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass",
-											 mSamplesPerEvaluation);
+// 	Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mTotalSamples);
+// 	Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
+// 	Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass",
+// 											 mSamplesPerEvaluation);
 	
 	
@@ -69,5 +69,4 @@
   for (int i=0; i < mTotalSamples; i += mSamplesPerPass, mPass++) {
 
-	
 	cout<<"Progress : "<<(100.0f*i)/mTotalSamples<<"%"<<endl;
 	rays.clear();
@@ -108,8 +107,15 @@
 	}
 
+	cerr<<"deleting rays"<<endl;
+	//clear all unreferenced rays
+	for (int j=0; j < vssRays.size(); j++) {
+	  if (vssRays[j]->RefCount() == 0)
+		delete vssRays[j];
+	}
+	cerr<<"done."<<endl;
+	  
 	
-	
-	if (!mMixtureDistribution->RequiresRays())
-	  CLEAR_CONTAINER(vssRays);
+	//	if (!mMixtureDistribution->RequiresRays())
+	//	  CLEAR_CONTAINER(vssRays);
 
 	if (renderer) {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp	(revision 1942)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp	(revision 1966)
@@ -26,5 +26,7 @@
     mFlags(0),
 	mPass(pass),
+#if VSS_STORE_VIEWCELLS
 	mViewCells(0),
+#endif
 	mWeightedPvsContribution(0),
 	mPdf(pdf),
@@ -43,5 +45,7 @@
 	mOriginObject(ray.sourceObject.mObject),
 	mPass(0),
+#if VSS_STORE_VIEWCELLS
 	mViewCells(0),
+#endif
 	mPdf(1.0f),
 	mTerminationNode(NULL),
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1952)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1966)
@@ -18,5 +18,4 @@
 HaltonSequence ObjectDirectionBasedDistribution::sHalton;
 HaltonSequence DirectionBasedDistribution::sHalton;
-HaltonSequence MutationBasedDistribution::sHalton;
 
 
@@ -478,4 +477,5 @@
 	// small non-zero value
 	mDistributions[i]->mRays = 1;
+	mDistributions[i]->mGeneratedRays = 1;
 	// unit contribution per ray
 	if (1 || mDistributions[i]->mType != RSS_BASED_DISTRIBUTION)
@@ -511,5 +511,10 @@
 	  break;
 
-  return mDistributions[i]->GenerateSample(ray);
+  bool result = mDistributions[i]->GenerateSample(ray);
+
+  if (result)
+	mDistributions[i]->mGeneratedRays++;
+  
+  return result;
 }
 
@@ -548,4 +553,5 @@
   
   UpdateRatios();
+
 }
 
@@ -558,5 +564,8 @@
   }
 }
-  
+
+#define RAY_CAST_TIME 0.9f
+#define VIEWCELL_CAST_TIME 0.1f
+
 void
 MixtureDistribution::UpdateRatios()
@@ -570,6 +579,8 @@
 	float importance = 0.0f;
 	if (mDistributions[i]->mRays != 0) {
-	  //	  importance = pow(mDistributions[i]->mContribution/mDistributions[i]->mRays, 3);
-	  importance = mDistributions[i]->mContribution/mDistributions[i]->mRays;
+	  //importance = pow(mDistributions[i]->mContribution/mDistributions[i]->mRays, 2);
+	  importance = mDistributions[i]->mContribution/
+		(RAY_CAST_TIME*mDistributions[i]->mGeneratedRays +
+		 VIEWCELL_CAST_TIME*mDistributions[i]->mRays);
 	}
 	mDistributions[i]->mRatio = importance;
@@ -577,19 +588,28 @@
   }
 
-  const float minratio = 0.02f;
+  
+  
+  if (sum == 0.0f)
+	sum = Limits::Small;
+
+  const float minratio = 0.01f;
   float threshold = minratio*sum;
+
+  // recaluate the sum
+  sum = 0.0f;
   for (i=0; i < mDistributions.size(); i++) {
 	if (mDistributions[i]->mRatio < threshold) {
-	  sum += threshold - mDistributions[i]->mRatio;
 	  mDistributions[i]->mRatio = threshold;
 	}
+	sum += mDistributions[i]->mRatio;
   }
   
   mDistributions[0]->mRatio /= sum;
-	
+  
   for (i=1; i < mDistributions.size(); i++) {
 	float r = mDistributions[i]->mRatio / sum;
 	mDistributions[i]->mRatio = mDistributions[i-1]->mRatio + r;
   }
+  
   
   cout<<"ratios: ";
@@ -653,5 +673,13 @@
 }
 
-
+int
+MixtureDistribution::GenerateSamples(const int number,
+									 SimpleRayContainer &rays)
+{
+  for (int i=0; i < mDistributions.size(); i++) 
+	mDistributions[i]->mGeneratedRays = 0;
+
+  return SamplingStrategy::GenerateSamples(number, rays);
+}
 
 void
@@ -661,19 +689,20 @@
   //	cout<<mRays[i].mSamples<<" ";
   //  cout<<endl;
-  
+  cerr<<"Muattion update..."<<endl;
+  cerr<<"rays = "<<mRays.size()<<endl;
+  if (mRays.size())
+	cerr<<"Oversampling factor = "<<mRays[0].mSamples<<endl;
   for (int i=0; i < vssRays.size(); i++) {
-	VssRay *newRay = vssRays[i];
-	if (newRay->mPvsContribution) {
-	  // add this ray
-	  newRay->Ref();
-	  if (mRays.size() < mMaxRays)
+	if (vssRays[i]->mPvsContribution) {
+	  if (mRays.size() < mMaxRays) {
+		VssRay *newRay = new VssRay(*vssRays[i]);
+		// add this ray
+		newRay->Ref();
 		mRays.push_back(RayEntry(newRay));
-	  else {
+	  } else {
 		// unref the old ray
-		VssRay *oldRay = mRays[mBufferStart].mRay;
-		oldRay->Unref();
-		if (oldRay->RefCount() == 0)
-		  delete oldRay;
-		mRays[mBufferStart] = RayEntry(newRay);
+		*mRays[mBufferStart].mRay = *vssRays[i];
+		mRays[mBufferStart].mSamples = 0;
+		//		mRays[mBufferStart] = RayEntry(newRay);
 		mBufferStart++;
 		if (mBufferStart >= mMaxRays)
@@ -682,4 +711,6 @@
 	}
   }
+  cerr<<"Mutation update done."<<endl;
+
 }
 
@@ -688,12 +719,15 @@
 {
   float r[5];
-  sHalton.GetNext(5, r);
 
   if (mRays.size() == 0) {
 	// use direction based distribution
 	Vector3 origin, direction;
+	static HaltonSequence halton;
+	
+	halton.GetNext(5, r);
 	mPreprocessor.mViewCellsManager->GetViewPoint(origin,
 												  Vector3(r[0], r[1], r[2]));
 	
+
 	direction = UniformRandomVector(r[3], r[4]);
 	
@@ -727,4 +761,6 @@
   mRays[index].mSamples++;
   mLastIndex = index;
+
+  mRays[index].mHalton.GetNext(4, r);
   
   Vector3 v;
@@ -732,10 +768,10 @@
   Vector3 d = ray->GetDir();
   if (d.DrivingAxis() == 0)
-	v = Vector3(0, r[1]-0.5f, r[2]-0.5f);
+	v = Vector3(0, r[0]-0.5f, r[1]-0.5f);
   else
 	if (d.DrivingAxis() == 1)
-	  v = Vector3(r[1]-0.5f, 0, r[2]-0.5f);
+	  v = Vector3(r[0]-0.5f, 0, r[1]-0.5f);
 	else
-	  v = Vector3(r[1]-0.5f, r[2]-0.5f, 0);
+	  v = Vector3(r[0]-0.5f, r[1]-0.5f, 0);
 
   v=v*mOriginMutationSize;
@@ -745,12 +781,13 @@
   // mutate the termination
   if (d.DrivingAxis() == 0)
-	v = Vector3(0, r[3]-0.5f, r[4]-0.5f);
+	v = Vector3(0, r[2]-0.5f, r[3]-0.5f);
   else
 	if (d.DrivingAxis() == 1)
-	  v = Vector3(r[3]-0.5f, 0, r[4]-0.5f);
+	  v = Vector3(r[2]-0.5f, 0, r[3]-0.5f);
 	else
-	  v = Vector3(r[3]-0.5f, r[4]-0.5f, 0);
-
-  float size = 1.5f*Magnitude(ray->mTerminationObject->GetBox().Diagonal());
+	  v = Vector3(r[2]-0.5f, r[3]-0.5f, 0);
+
+  AxisAlignedBox3 box = ray->mTerminationObject->GetBox();
+  float size = 2.0f*Magnitude(box.Diagonal());
   if (size < Limits::Small)
 	return false;
@@ -767,5 +804,6 @@
   v = v*size;
 
-  Vector3 termination = ray->mTermination + v;
+  //  Vector3 termination = ray->mTermination + v;
+  Vector3 termination = box.Center() + v;
   
   Vector3 direction = termination - origin;
@@ -775,6 +813,6 @@
 
   // shift the origin a little bit
-  origin += direction*0.05f;
-
+  origin += direction*0.5f;
+  
   direction.Normalize();
   
@@ -786,12 +824,13 @@
 }
 
-MutationBasedDistribution::MutationBasedDistribution(Preprocessor &preprocessor) :
+MutationBasedDistribution::MutationBasedDistribution(Preprocessor &preprocessor
+													 ) :
   SamplingStrategy(preprocessor)
 {
   mType = MUTATION_BASED_DISTRIBUTION;
   mBufferStart = 0;
-  mMaxRays = 1000000;
-
-  mOriginMutationSize = 2.0f;
+  mMaxRays = 500000;
+  mRays.reserve(mMaxRays);
+  mOriginMutationSize = 10.0f;
   mLastIndex = 0;
   //  mOriginMutationSize = Magnitude(preprocessor.mViewCellsManager->
