Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2022)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2035)
@@ -30,5 +30,5 @@
 // use avg ray contribution as importance
 // if 0 the importance is evaluated from the succ of mutations
-#define USE_AVG_CONTRIBUTION 0
+#define USE_AVG_CONTRIBUTION 1
 
 MutationBasedDistribution::RayEntry &
Index: GTP/trunk/Lib/Vis/Preprocessing/src/run_test_pompeii
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/run_test_pompeii	(revision 2010)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/run_test_pompeii	(revision 2035)
@@ -2,5 +2,5 @@
 
 #COMMAND="./release/preprocessor.exe -preprocessor_quit_on_finish+"
-COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer- -preprocessor_evaluate_filter+ -preprocessor_detect_empty_viewspace-"
+COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer- -preprocessor_evaluate_filter- -preprocessor_detect_empty_viewspace- -total_samples=500000000"
 
 
@@ -8,5 +8,5 @@
 VIEWCELLS=../data/Pompeii/pompeii_big-seq-viewcells.xml.gz
 
-PREFIX=../work/plots/osp-pompeii3-1e5
+PREFIX=../work/plots/osp-pompeii4-1e5
 
 # $COMMAND -preprocessor=combined -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
@@ -16,23 +16,23 @@
 #  -preprocessor_histogram_file=$PREFIX-i-mixed-b1-n4n.hlog
 
-# n - no origin mutation, q=2, reverse samples
+# # n - no origin mutation, q=2, reverse samples
 
-$COMMAND -preprocessor=combined -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
- -rss_distributions=mutation+object_direction+spatial \
- -view_cells_use_kd_pvs- -af_use_kd_pvs- \
- -preprocessor_visibility_file=$PREFIX-i-mixed-bvh-n4n.xml \
- -preprocessor_stats=$PREFIX-i-mixed-bvh-n4n.log \
- -preprocessor_histogram_file=$PREFIX-i-mixed-bvh-n4n.hlog
+# $COMMAND -preprocessor=combined -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
+#  -rss_distributions=mutation+object_direction+spatial \
+#  -view_cells_use_kd_pvs- -af_use_kd_pvs- \
+#  -preprocessor_visibility_file=$PREFIX-i-mixed-bvh-n4n.xml \
+#  -preprocessor_stats=$PREFIX-i-mixed-bvh-n4n.log \
+#  -preprocessor_histogram_file=$PREFIX-i-mixed-bvh-n4n.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 -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_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_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
 
 #mutation+object_direction+
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2023)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2035)
@@ -1110,35 +1110,40 @@
 bool Preprocessor::LoadInternKdTree( const string internKdTree)
 {
-	// always try to load the kd tree
-	cout << "loading kd tree file " << internKdTree << " ... " << endl;
-
-	if (!LoadKdTree(internKdTree))
-	{
-		cout << "error loading kd tree with filename " 
-			 << internKdTree << ", rebuilding it instead ... " << endl;
-		// build new kd tree from scene geometry
-		BuildKdTree();
-
-		// export kd tree?
-		const long startTime = GetTime();
-		cout << "exporting kd tree ... ";
-
-		if (!ExportKdTree(internKdTree))
-		{
-			cout << " error exporting kd tree with filename " 
-				 << internKdTree << endl;
-		}
-		else
-		{
-			cout << "finished in " 
-				 << TimeDiff(startTime, GetTime()) * 1e-3 
-				 << " secs" << endl;
-		}
-	}
-	
-	KdTreeStatistics(cout);
-	cout << mKdTree->GetBox() << endl;
-
+  if (!mUseKdTree) {
+	// create just a dummy KdTree
+	mKdTree = new KdTree;
 	return true;
+  }
+  
+  // always try to load the kd tree
+  cout << "loading kd tree file " << internKdTree << " ... " << endl;
+  
+  if (!LoadKdTree(internKdTree)) {
+	cout << "error loading kd tree with filename " 
+		 << internKdTree << ", rebuilding it instead ... " << endl;
+	// build new kd tree from scene geometry
+	BuildKdTree();
+	
+	// export kd tree?
+	const long startTime = GetTime();
+	cout << "exporting kd tree ... ";
+	
+	if (!ExportKdTree(internKdTree))
+	  {
+		cout << " error exporting kd tree with filename " 
+			 << internKdTree << endl;
+	  }
+	else
+	  {
+		cout << "finished in " 
+			 << TimeDiff(startTime, GetTime()) * 1e-3 
+			 << " secs" << endl;
+	  }
+  }
+  
+  KdTreeStatistics(cout);
+  cout << mKdTree->GetBox() << endl;
+  
+  return true;
 }
 
@@ -1209,27 +1214,27 @@
 					   )
 {
+
   const long t1 = GetTime();
-
-#if 1
-  mRayCaster->SortRays(rays);
-  //cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" ms."<<endl;
-
-  if (0) {
-	VssRayContainer tmpRays;
-	int m = 50000;
-	for (int i=m; i < m+20; i++) {
-	  tmpRays.push_back(new VssRay(rays[i].mOrigin,
-								   rays[i].mOrigin + 100.0f*rays[i].mDirection,
-								   NULL,
-								   NULL
-								   )
-						);
-	  
-	}
-	ExportRays("sorted_rays.x3d", tmpRays, 200);
+  if (rays.size() > 10000) {
+	mRayCaster->SortRays(rays);
+	cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" ms."<<endl;
+	
+	if (0) {
+	  VssRayContainer tmpRays;
+	  int m = 50000;
+	  for (int i=m; i < m+20; i++) {
+		tmpRays.push_back(new VssRay(rays[i].mOrigin,
+									 rays[i].mOrigin + 100.0f*rays[i].mDirection,
+									 NULL,
+									 NULL
+									 )
+						  );
+		
+	  }
+	  ExportRays("sorted_rays.x3d", tmpRays, 200);
+	}
   }
-#endif
-  
-	SimpleRayContainer::const_iterator rit, rit_end = rays.end();
+  
+  SimpleRayContainer::const_iterator rit, rit_end = rays.end();
 
 	SimpleRayContainer rayBucket;
@@ -1261,5 +1266,5 @@
 		}
 	
-		if ((int)rays.size() > 10000 && i % 10000 == 0)
+		if ((int)rays.size() > 100000 && i % 100000 == 0)
 		  cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/run_test_arena
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/run_test_arena	(revision 2035)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/run_test_arena	(revision 2035)
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+#COMMAND="./release/preprocessor.exe -preprocessor_quit_on_finish+"
+COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer- -preprocessor_evaluate_filter- -preprocessor_detect_empty_viewspace- -total_samples=500000000"
+
+
+SCENE=../data/Arena/arena-high-lods.obj
+VIEWCELLS=../data/Arena/viewcells-5000.xml
+
+PREFIX=../work/plots/osp-arena1-1e5
+
+# $COMMAND -preprocessor=combined -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
+#  -rss_distributions=mutation+object_direction+spatial -view_cells_filter_max_size=1 \
+#  -preprocessor_visibility_file=$PREFIX-i-mixed-b1-n4n.xml \
+#  -preprocessor_stats=$PREFIX-i-mixed-b1-n4n.log \
+#  -preprocessor_histogram_file=$PREFIX-i-mixed-b1-n4n.hlog
+
+# # n - no origin mutation, q=2, reverse samples
+
+# $COMMAND -preprocessor=combined -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
+#  -rss_distributions=mutation+object_direction+spatial \
+#  -view_cells_use_kd_pvs- -af_use_kd_pvs- \
+#  -preprocessor_visibility_file=$PREFIX-i-mixed-bvh-n4n.xml \
+#  -preprocessor_stats=$PREFIX-i-mixed-bvh-n4n.log \
+#  -preprocessor_histogram_file=$PREFIX-i-mixed-bvh-n4n.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_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
+
+#mutation+object_direction+
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 2022)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 2035)
@@ -11,4 +11,5 @@
 #include "VssRay.h"
 #include "SamplingStrategy.h"
+#include "RayCaster.h"
 
 
@@ -25,6 +26,4 @@
 SamplingPreprocessor::~SamplingPreprocessor()
 {
-  CLEAR_CONTAINER(mSampleRays);
-  CLEAR_CONTAINER(mVssSampleRays);
 }
 
@@ -91,10 +90,12 @@
 
   map<ViewCell *, HaltonSequence> dirHalton;
-  
+  int lastEvaluation = 0;
+  SimpleRayContainer rays;
+  VssRayContainer vssRays;
+
   while (samples < mTotalSamples) {
-	for (int i=0; i < mSamplesPerEvaluation;) {
-	  SimpleRayContainer rays;
-	  VssRayContainer vssRays;
-	  //	  vector<ViewCell *> viewcells;
+	for (int i=0; i < mSamplesPerPass;) {
+	  
+	  mRayCaster->InitPass();
 	  
 	  float r[5];
@@ -107,15 +108,17 @@
 	  if (!viewcell || !viewcell->GetValid())
 		continue;
-	  
+
+	  rays.clear();
+	  vssRays.clear();
+
 	  for (; rays.size() < 16; ) {
-		if (i%100000 == 0)
-		  cout<<"+";
+		if (i%100000 == 0) {
+		  cout<<samples/1e6f<<"M rays, progress: "<<samples*100/mTotalSamples<<" %   \r";
+		}
 		
-		//		dirHalton.GetNext(2, r);
 		dirHalton[viewcell].GetNext(2, r);
 		direction = UniformRandomVector(r[0],r[1]);
 		//direction = UniformRandomVector();
 		
-		//		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
@@ -133,8 +136,4 @@
 			   true);
 	  
-	  // 	  if (vssRays.size()!=32) {
-	  // 		cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
-	  // 		exit(1);
-	  // 	  }
 
 	  rssSamples += vssRays.size();
@@ -147,5 +146,5 @@
 			float pdf = 1.0f;
 			//			ViewCell *viewcell = viewcells[j/2];
-
+			
 			ObjectPvs &pvs = viewcell->GetPvs();
 			pvs.AddSampleDirtyCheck(obj, pdf);
@@ -157,26 +156,24 @@
 		}
 	  
-	  CLEAR_CONTAINER(vssRays);
-	  
 	  if (samples > mTotalSamples)
 		break;
 	}
 
-#if 0
-	Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
-	mViewCellsManager->SetValidity(0, intersectables/2);
-	Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
-#endif
-	
-	//	mVssRays.PrintStatistics(mStats);
-	mStats <<
-	  "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
-	  "#TotalSamples\n" <<samples<<endl<<
-	  "#RssSamples\n" <<rssSamples<<endl;
-		  
-	mViewCellsManager->PrintPvsStatistics(mStats);
-	// ComputeRenderError();
-  }
-  
+
+	if (samples - lastEvaluation >= mSamplesPerEvaluation) {
+	  mViewCellsManager->PrintPvsStatistics(mStats);
+	  mStats <<
+		"#Pass\n" <<mPass<<endl<<
+		"#Time\n" << TimeDiff(startTime, GetTime())<<endl<<
+		"#TotalSamples\n" <<samples<<endl<<
+		"#RssSamples\n" <<rssSamples<<endl;
+	  lastEvaluation = samples;
+
+	  if (renderer) {
+		ComputeRenderError();
+	  }
+	  
+	}
+  }
   if (0) {
 	Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); 
@@ -185,7 +182,7 @@
 	delete exporter;
   }
-  
-  
-  // $$JB temporary removed
+	
+	
+	// $$JB temporary removed
   //	mViewCellsManager->PostProcess(objects, mSampleRays);
   
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 2021)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 2035)
@@ -106,14 +106,12 @@
 	else
 	{
-		if (mPreprocessor.mDetectEmptyViewSpace) 
-		{
-			if (DotProd(hit.mNormal, direction) >= -Limits::Small) 
-			{	
-				hit.mObject = NULL;
-				return false;
-			}
+	  if (mPreprocessor.mDetectEmptyViewSpace)  {
+		if (DotProd(hit.mNormal, direction) >= -Limits::Small)  {	
+		  hit.mObject = NULL;
+		  return false;
 		}
-	}
-
+	  }
+	}
+	
 	return true;
 }
@@ -251,5 +249,5 @@
 	// regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
 	if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
-		return 0;
+	  return 0;
 	}
 	
