Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 1020)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 1112)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 860)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 1112)
@@ -317,18 +317,22 @@
 public:
   
-  float mSumPdf;
-  
-  SimpleRayContainer():vector<SimpleRay>(), mSumPdf(0.0f) {}
-  
-  void NormalizePdf() {
+  
+  SimpleRayContainer():vector<SimpleRay>() {}
+  
+  void NormalizePdf(float scale = 1.0f) {
 	iterator it = begin();
-	float c = 1.0f/mSumPdf;
+	float sumPdf = 0.0f;
 	for (; it != end(); it++)
+	  sumPdf += (*it).mPdf;
+
+	float c = scale/sumPdf;
+	
+	for (it = begin(); it != end(); it++) {
 	  (*it).mPdf*=c;
-  }
-
+	}
+  }
+  
   void AddRay(const SimpleRay &ray) {
 	push_back(ray);
-	mSumPdf += ray.mPdf;
   }
 };
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1106)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1112)
@@ -1695,5 +1695,5 @@
 	RegisterOption("Preprocessor.quitOnFinish",
 				   optBool,
-				   "preprocessor_quit_on_finish=",
+				   "preprocessor_quit_on_finish",
 				   "true");
 
@@ -1799,15 +1799,15 @@
 	RegisterOption("RssPreprocessor.useImportanceSampling",
 					optBool,
-					"rss_use_importance=",
+					"rss_use_importance",
 					"true");
 
 	RegisterOption("RssPreprocessor.objectBasedSampling",
 					optBool,
-					"rss_object_based_sampling=",
+					"rss_object_based_sampling",
 					"true");
 
 	RegisterOption("RssPreprocessor.directionalSampling",
 					optBool,
-					"rss_directional_sampling=",
+					"rss_directional_sampling",
 					"false");
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1106)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1112)
@@ -88,5 +88,9 @@
 			 const RgbColor &color = RgbColor(1,1,1)
 			 ) = 0;
-  
+
+  virtual bool
+  ExportRaySets(const vector<VssRayContainer> &rays,
+				const RgbColor &color) = 0;
+
   virtual void 
   ExportViewCells(const ViewCellContainer &viewCells) = 0;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/LogManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/LogManager.h	(revision 1004)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/LogManager.h	(revision 1112)
@@ -34,5 +34,5 @@
 	LogManager();
 
-	~LogManager();
+  ~LogManager() {}
   
 	void LogMessage(const std::string &str);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1022)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1112)
@@ -384,6 +384,11 @@
 		Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);		
 	    mViewCellsManager = CreateViewCellsManager(buf);
-	}
-
+
+		// default view space is the extent of the scene
+		mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
+
+
+	}
+	
 	//-- parameters for render heuristics evaluation
 	float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
@@ -398,6 +403,4 @@
 	mViewCellsManager->SetRenderer(mRenderSimulator);
 
-	// default view space is the extent of the scene
-	mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
 
 	if (mUseGlRenderer || mUseGlDebugger)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.h	(revision 1106)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.h	(revision 1112)
@@ -128,4 +128,8 @@
   virtual void ExportBeam(const Beam &beam, const AxisAlignedBox3 &box);
 
+  bool
+  ExportRaySets(const vector<VssRayContainer> &rays,
+				const RgbColor &color);
+
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1027)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1112)
@@ -23,5 +23,6 @@
 static bool fromBoxVisibility = false;
 
-
+#define ADD_RAYS_IN_PLACE 0
+  
 RssPreprocessor::RssPreprocessor():
   mPass(0),
@@ -67,5 +68,4 @@
 	  GenerateImportanceRays(mRssTree, number, rays);
 	  result = true;
-	  //	  rays.NormalizePdf();
 	}
 	break;
@@ -75,4 +75,6 @@
   }
 
+  //  rays.NormalizePdf();
+  
   return result;
 }
@@ -120,5 +122,7 @@
   int hits = 0;
   static Ray ray;
-  AxisAlignedBox3 box = mKdTree->GetBox();
+  //  AxisAlignedBox3 box = Union(mKdTree->GetBox(), mViewCellsManager->GetViewSpaceBox());
+
+  AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
 
   AxisAlignedBox3 sbox = box;
@@ -284,4 +288,5 @@
   } else {
 	int leaves = rssTree->stat.Leaves()/1;
+	
 	num = rssTree->GenerateRays(desiredSamples, leaves, rays);
   }
@@ -302,6 +307,8 @@
   Exporter *exporter = NULL;
   exporter = Exporter::GetExporter(filename);
-  //	exporter->SetWireframe();
-  //	exporter->ExportKdTree(*mKdTree);
+  if (0) {
+	exporter->SetWireframe();
+	exporter->ExportKdTree(*mKdTree);
+  }
   exporter->SetFilled();
   // $$JB temporarily do not export the scene
@@ -310,15 +317,49 @@
   exporter->SetWireframe();
 
-  if (mViewSpaceBox) {
+  if (1 || mViewSpaceBox) {
 	exporter->SetForcedMaterial(RgbColor(1,0,1));
-	exporter->ExportBox(*mViewSpaceBox);
+	exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
 	exporter->ResetForcedMaterial();
   }
-	
+  
   VssRayContainer rays;
   
-  vssRays.SelectRays( number, rays);
+  vssRays.SelectRays(number, rays);
 
   exporter->ExportRays(rays, RgbColor(1, 0, 0));
+	
+  delete exporter;
+
+  cout<<"done."<<endl<<flush;
+
+  return true;
+}
+
+bool
+RssPreprocessor::ExportRayAnimation(const char *filename,
+									const vector<VssRayContainer> &vssRays
+									)
+{
+  cout<<"Exporting vss rays..."<<endl<<flush;
+	
+  Exporter *exporter = NULL;
+  exporter = Exporter::GetExporter(filename);
+  if (0) {
+	exporter->SetWireframe();
+	exporter->ExportKdTree(*mKdTree);
+  }
+  exporter->SetFilled();
+  // $$JB temporarily do not export the scene
+  if (1)
+	exporter->ExportScene(mSceneGraph->mRoot);
+  exporter->SetWireframe();
+
+  if (1 || mViewSpaceBox) {
+	exporter->SetForcedMaterial(RgbColor(1,0,1));
+	exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
+	exporter->ResetForcedMaterial();
+  }
+  
+  exporter->ExportRaySets(vssRays, RgbColor(1, 0, 0));
 	
   delete exporter;
@@ -459,5 +500,27 @@
 }
 
-
+void
+NormalizeRatios(float ratios[3])
+{
+  int i;
+  float sumRatios;
+  sumRatios = ratios[0] + ratios[1] + ratios[2];
+  if (sumRatios == 0.0f) {
+	ratios[0] = ratios[1] = ratios[2] = 1.0f;
+	sumRatios = 3.0f;
+  }
+  
+  for (i=0 ; i < 3; i++)
+	ratios[i]/=sumRatios;
+#define MIN_RATIO 0.03f
+  for (i=0 ; i < 3; i++)
+	if (ratios[i] < MIN_RATIO)
+	  ratios[i] = MIN_RATIO;
+
+  sumRatios = ratios[0] + ratios[1] + ratios[2];
+  for (i=0 ; i < 3; i++)
+	ratios[i]/=sumRatios;
+  
+}
 
 bool
@@ -472,5 +535,6 @@
   Randomize(0);
   
-	
+  vector<VssRayContainer> rayBuffer(50);
+  
   long startTime = GetTime();
   
@@ -480,27 +544,34 @@
 
   // if not already loaded, construct view cells from file
-  if (!mLoadViewCells) {	
-
-	if (0) 
+  if (!mLoadViewCells)
+  {
+	if (1) 
 	  mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
 	else {
 	  AxisAlignedBox3 box = mKdTree->GetBox();
-	  float s = box.Size(0);
-	  box.Scale(0.1f);
-	  box.SetMin(0, box.Min(0) + s);
-	  box.SetMax(0, box.Max(0) + s);
+	  
+	  if (1) {
+		// use a small box outside of the scene
+		box.Scale(Vector3(0.1f,0.5f,0.5f));
+		box.Translate(Vector3(Magnitude(mKdTree->GetBox().Size())*0.5, 0, 0));
+	  } else {
+		float s = box.Size(0);
+		box.Scale(0.1f);
+		box.SetMin(0, box.Min(0) + s);
+		box.SetMax(0, box.Max(0) + s);
+	  }
 	 
 	  mViewCellsManager->SetViewSpaceBox(box);
 	}
-
-	  // construct view cells using it's own set of samples
-	  mViewCellsManager->Construct(this);
-
-	  //-- several visualizations and statistics
-	  Debug << "view cells construction finished: " << endl;
-	  mViewCellsManager->PrintStatistics(Debug);
-  }
-
-
+	
+	// construct view cells using it's own set of samples
+	mViewCellsManager->Construct(this);
+	
+	//-- several visualizations and statistics
+	Debug << "view cells construction finished: " << endl;
+	mViewCellsManager->PrintStatistics(Debug);
+  }
+  
+  
   int rssPass = 0;
   int rssSamples = 0;
@@ -515,9 +586,9 @@
 	cout<<"Generating initial rays..."<<endl<<flush;
 
-	GenerateRays(mInitialSamples/4, SPATIAL_BOX_BASED_DISTRIBUTION, rays);
-	GenerateRays(mInitialSamples/4, OBJECT_BASED_DISTRIBUTION, rays);
-	GenerateRays(mInitialSamples/4, DIRECTION_BASED_DISTRIBUTION, rays);
-	GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays);
-	
+	GenerateRays(mInitialSamples/3, SPATIAL_BOX_BASED_DISTRIBUTION, rays);
+	GenerateRays(mInitialSamples/3, OBJECT_BASED_DISTRIBUTION, rays);
+	GenerateRays(mInitialSamples/3, DIRECTION_BASED_DISTRIBUTION, rays);
+	//	GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays);
+
 	cout<<"Casting initial rays..."<<endl<<flush;
 	CastRays(rays, mVssRays);
@@ -537,4 +608,6 @@
 	sprintf(filename, "rss-rays-initial.x3d");
 	ExportRays(filename, mVssRays, mExportNumRays);
+
+	mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
 	
   }
@@ -562,6 +635,11 @@
 	{
 	  VssRayContainer contributingRays;
-	  mVssRays.GetContributingRays(contributingRays, mPass);
+	  mVssRays.GetContributingRays(contributingRays, 0);
 	  mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
+	  if (mExportRays) {
+		char filename[64];
+		sprintf(filename, "rss-crays-%04d.x3d", 0);
+		ExportRays(filename, contributingRays, mExportNumRays);
+	  }
 	}
 	
@@ -615,22 +693,48 @@
 	  VssRayContainer tmpVssRays;
 
-	  float ratios[] = {0.8f,0.1f,0.1f};
-
+	  static float ratios[] = {0.9f,0.05f,0.05f};
+	  //float ratios[] = {1.0f,0.0f,0.0f};
+	  
+	  int nrays[3];
+	  float contributions[3];
+	  float times[3];
+	  
+	  long t1, t2;
+
+	  t1 = GetTime();
+	  
 	  GenerateRays(mRssSamplesPerPass*ratios[0], RSS_BASED_DISTRIBUTION, rays);
+
+	  rays.NormalizePdf(rays.size());
+	  
 	  CastRays(rays, tmpVssRays);
 	  castRays += rays.size();
-	  float c1 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
-	  mStats<<"#RssRelContrib"<<endl<<c1/rays.size()<<endl;
-
+#if ADD_RAYS_IN_PLACE 
+	  contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
+#else
+	  contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
+#endif
+	  times[0] = TimeDiff(t1, GetTime());
+	  nrays[0] = rays.size();
+	  
+	  mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
+	  
 	  vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
 	  rays.clear();
 	  tmpVssRays.clear();
-
 	  if (ratios[1]!=0.0f) {
+		t1 = GetTime();
 		GenerateRays(mRssSamplesPerPass*ratios[1], SPATIAL_BOX_BASED_DISTRIBUTION, rays);
 		CastRays(rays, tmpVssRays);
 		castRays += rays.size();
-		float c2 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
-		mStats<<"#SpatialRelContrib"<<endl<<c2/rays.size()<<endl;
+#if ADD_RAYS_IN_PLACE 
+		contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
+#else
+		contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
+#endif
+	  times[1] = TimeDiff(t1, GetTime());
+	  nrays[1] = rays.size();
+
+	  mStats<<"#SpatialRelContrib"<<endl<<contributions[1]/nrays[1]<<endl;
 		
 		vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
@@ -642,9 +746,17 @@
 	  
 	  if (ratios[2]!=0.0f) {
+		t1 = GetTime();
 		GenerateRays(mRssSamplesPerPass*ratios[2], DIRECTION_BASED_DISTRIBUTION, rays);
 		CastRays(rays, tmpVssRays);
 		castRays += rays.size();
-		float c3 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
-		mStats<<"#DirectionalRelContrib"<<endl<<c3/rays.size()<<endl;
+#if ADD_RAYS_IN_PLACE 
+		contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
+#else
+		contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
+#endif
+		times[2] = TimeDiff(t1, GetTime());
+		nrays[2] = rays.size();
+		
+		mStats<<"#DirectionalRelContrib"<<endl<<contributions[2]/nrays[2]<<endl;
 		
 		vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
@@ -654,7 +766,25 @@
 	  }
 	  
+	  
+	  // now evaluate the ratios for the next pass
+#define TIMES 0
+
+#if TIMES
+	  ratios[0] = sqr(contributions[0]/times[0]);
+	  ratios[1] = sqr(contributions[1]/times[1]);
+	  ratios[2] = sqr(contributions[2]/times[2]);
+#else
+	  ratios[0] = sqr(contributions[0]/nrays[0]);
+	  ratios[1] = sqr(contributions[1]/nrays[1]);
+	  ratios[2] = sqr(contributions[2]/nrays[2]);
+#endif
+	  NormalizeRatios(ratios);
+
+	  cout<<"New ratios: "<<ratios[0]<<" "<<ratios[1]<<" "<<ratios[2]<<endl;
+	  
 	  // add contributions of all rays at once...
+#if !ADD_RAYS_IN_PLACE 
 	  mViewCellsManager->AddSampleContributions(vssRays);
-	  
+#endif	  
 	}
 	else {
@@ -723,6 +853,10 @@
 		sprintf(filename, "rss-rays-%04d.x3d", rssPass);
 	  
+	  
+
+	  vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
+	  
 	  ExportRays(filename, vssRays, mExportNumRays);
-
+	  
 	  // now export all contributing rays
 	  VssRayContainer contributingRays;
@@ -731,4 +865,5 @@
 	  sprintf(filename, "rss-crays-%04d.x3d", rssPass);
 	  ExportRays(filename, contributingRays, mExportNumRays);
+	  
 	}
 
@@ -798,5 +933,14 @@
 	
   }
-  
+
+
+  if (mExportRays && mUseImportanceSampling) {
+	char filename[64];
+	sprintf(filename, "rss-rays-i.x3d");
+	
+	rayBuffer.resize(mPass);
+	ExportRayAnimation(filename, rayBuffer);
+  }
+	  
   Debug<<"Deleting RSS tree...\n";
   delete mRssTree;
@@ -804,5 +948,5 @@
 
 
-  //  mViewCellsManager->ExportViewCells("visibility.xml",
+  // mViewCellsManager->ExportViewCells("visibility.xml",
   //									 true);
   
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1107)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1112)
@@ -35,4 +35,5 @@
 
 
+
 bool
 X3dExporter::ExportRays(const RayContainer &rays,
@@ -83,4 +84,57 @@
 
   return true;
+}
+
+
+bool
+X3dExporter::ExportRaySets(const vector<VssRayContainer> &rays,
+						   const RgbColor &color)
+{
+  vector<VssRayContainer>::const_iterator ri = rays.begin();
+  
+  stream<<"<Switch DEF=\"RAYS\" whichChoice=\"0\" >"<<endl;
+
+  bool result = false;
+
+  for (; ri != rays.end(); ri++) {
+	result = ExportRays(*ri, color);
+	if (!result) {
+	  cerr<<"Error while exporting rays!\n";
+	  break;
+	}
+  }
+  
+  stream<<"</Switch>"<<endl;
+
+  stream<<"<TimeSensor DEF='TIMER' loop='true' startTime='0' stopTime='0' cycleInterval='5'>"
+		<<endl;
+  stream<<"</TimeSensor>"<<endl;
+  
+  stream<<"<IntegerSequencer DEF='SEQ'"<<endl;
+  stream<<"key='0.0 ";
+  int i;
+  int size = rays.size() + 2;
+  for (i=1; i < size; i++) {
+	stream<<i/(float)(size-1)<<" ";
+  }
+  stream<<"'\n";
+
+  stream<<"keyValue='";
+  for (i=0; i < size; i++) {
+	stream<<i<<" ";
+  }
+  stream<<"'>\n";
+  
+  stream<<"</IntegerSequencer>"<<endl;
+
+  stream<<
+	"<ROUTE fromNode='TIMER' fromField='fraction_changed' toNode='SEQ' toField='set_fraction'>";
+  stream<<"</ROUTE>"<<endl;
+  
+  stream<<"<ROUTE fromNode='SEQ' fromField='value_changed' toNode='RAYS' toField='set_whichChoice'>";
+  
+  stream<<"</ROUTE>"<<endl;
+  
+  return result;
 }
 
@@ -1262,2 +1316,3 @@
 
 }
+
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 904)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1112)
@@ -14,6 +14,6 @@
 # filename ../data/vienna/viewcells-25-sel.x3d
 #filename ../data/atlanta/atlanta2.x3d
-#filename ../data/soda/soda.dat
-filename ../data/soda/soda5.dat
+# filename ../data/soda/soda.dat
+#filename ../data/soda/soda5.dat
 #filename ../data/PowerPlant/ppsection1/part_a/g0.ply
 #filename ../data/PowerPlant/ppsection1/part_b/g0.ply
@@ -22,5 +22,5 @@
 #filename ../data/PowerPlant/selection3.plb
 #filename ../data/PowerPlant/section10.plb;../data/PowerPlant/section14.plb
-#filename ../data/PowerPlant/selection.plb
+filename ../data/PowerPlant/selection.plb
 #filename ../data/PowerPlant/comps/ppsection1/comps.plb
 }
@@ -35,13 +35,14 @@
 	type rss
 #	type render
-	detectEmptyViewSpace false
-	pvsRenderErrorSamples 0
-#	pvsRenderErrorSamples 1000
-	quitOnFinish false
+	detectEmptyViewSpace true
+#	pvsRenderErrorSamples 0
+	pvsRenderErrorSamples 5000
+	quitOnFinish true
 	computeVisibility true
-	applyVisibilityFilter	false
-	applyVisibilitySpatialFilter	true
+	applyVisibilityFilter false
+	applyVisibilitySpatialFilter false
 	visibilityFilterWidth	0.01
 	visibilityFile visibility.xml
+	loadPolygonsAsMeshes false
 }
 
@@ -53,7 +54,7 @@
 VssPreprocessor {
 	samplesPerPass  100000
-	initialSamples 500000
-	vssSamples 5000000
-	vssSamplesPerPass 500000
+	initialSamples 100000
+	vssSamples 1000000
+	vssSamplesPerPass 100000
 	useImportanceSampling true
 	loadInitialSamples  false
@@ -72,5 +73,5 @@
 	minRays		100
 	minSize		0.001
- 	maxCostRatio	1.5
+ 	maxCostRatio	2.0
 	maxRayContribution 0.5
 	
@@ -79,6 +80,6 @@
 
 #	splitType regular
-#	splitType heuristic
-	splitType hybrid
+	splitType heuristic
+#	splitType hybrid
 	splitUseOnlyDrivingAxis true
 
@@ -94,7 +95,7 @@
 
 RssPreprocessor {
-	samplesPerPass 500000
+	samplesPerPass 1000
 	initialSamples 1000000
-	vssSamples 5000000
+	vssSamples 500000000
 	vssSamplesPerPass 1000000
 	useImportanceSampling true
@@ -106,6 +107,6 @@
 		pvs false
 		rssTree false
-		rays false
-		numRays 20000
+		rays true
+		numRays 2000
 	}
 
@@ -121,20 +122,20 @@
 	perObjectTree false
 
-	maxDepth 	40
-	minPvs		3
+	maxDepth 	60
+	minPvs		1
 # before vienna test it was:
 #	minRays	50
 #	splitType heuristic
 
-	minRays		100
+	minRays		200
 	minSize		0.001
- 	maxCostRatio 1.0
+ 	maxCostRatio 0.98
 	maxRayContribution 0.5
-	maxRays		2000000
+	maxRays		4000000
 	maxTotalMemory	200
 	maxStaticMemory	100
 
-	splitType regular
-#	splitType heuristic
+#	splitType regular
+	splitType heuristic
 #	splitType hybrid
 	splitUseOnlyDrivingAxis false
@@ -165,5 +166,5 @@
 		maxCostRatio 1.0
 		ct_div_ci 0.5
-		maxNodes 100000
+		maxNodes 200000
 
 #500000	
@@ -211,5 +212,5 @@
 	maxStaticMemory	40
 
-	exportToFile false
+	exportToFile true
 	loadFromFile true
 
@@ -280,10 +281,15 @@
 #	filename ../data/soda/soda5-viewcells.xml
 #	filename ../scripts/viewcells_atlanta.xml
-	filename ../data/soda/soda5-viewcells2.xml
+#	filename ../data/soda/soda5-viewcells2.xml
+
+#	filename ../data/soda/soda5-viewcell-single.xm
+#	filename ../data/soda/vienna-viewcell-single.xml
+
+
 #	filename ../data/atlanta/viewcells_atlanta3.xml
 #	filename ../data/vienna/viewcells_vienna.xml
 #	filename ../data/vienna/viewcells_vienna2.xml
 #	filename ../data/vienna/vienna_simple-21-04-avs2-viewCells.xml
-#	filename ../data/PowerPlant/power_plant_viewcells_all3.xml
+	filename ../data/PowerPlant/power_plant_viewcells1.xml
 }
 
@@ -385,5 +391,6 @@
 		minGlobalCostRatio      0.0000001
 #		minGlobalCostRatio      0.0001
-		maxViewCells		50000
+# $$MAXVIEWCELLS
+		maxViewCells		5000
 	
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h	(revision 1106)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h	(revision 1112)
@@ -113,4 +113,8 @@
 			 const RgbColor &color = RgbColor(1,1,1));
 
+  virtual bool
+  ExportRaySets(const vector<VssRayContainer> &rays,
+	const RgbColor &color) { return false; }
+
   virtual void
   ExportBspSplitPlanes(const BspTree &tree);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1004)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1112)
@@ -87,54 +87,4 @@
 }
 
-//  void
-//  SamplingPreprocessor::AvsGenerateRandomRay(Ray &ray)
-//  {
-//    int objId = RandomValue(0, mObjects.size());
-//    Intersectable *object = objects[objId];
-//    object->GetRandomSurfacePoint(point, normal);
-//    direction = UniformRandomVector(normal);
-//    SetupRay(ray, point, direction);
-//  }
-
-//  void
-//  SamplingPreprocessor::AvsHandleRay(Ray &ray)
-//  {
-//    int sampleContributions = 0;
-
-//    mKdTree->CastRay(ray);
-  
-//    if (ray.leaves.size()) {
-//      sampleContributions += AddNodeSamples(object, ray, pass);
-    
-//      if (ray.intersections.size()) {
-//        sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass);
-//      }
-//    }
-//  }
-
-//  void
-//  SamplingPreprocessor::AvsBorderSampling(Ray &ray)
-//  {
-  
-
-//  }
-
-//  void
-//  SamplingPreprocessor::AvsPass()
-//  {
-//    Ray ray;
-//    while (1) {
-//      AvsGenerateRay(ray);
-//      HandleRay(ray);
-//      while ( !mRayQueue.empty() ) {
-//        Ray ray = mRayQueue.pop();
-//        mRayQueue.pop();
-//        AdaptiveBorderSampling(ray);
-//      }
-//    }
-  
-  
-
-//  }
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 863)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 1112)
@@ -90,4 +90,9 @@
 			 );
 
+  bool
+  ExportRayAnimation(const char *filename,
+					 const vector<VssRayContainer> &vssRays
+					 );
+
   void
   ExportObjectRays(VssRayContainer &rays,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1108)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1112)
@@ -720,5 +720,5 @@
 		   << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\" />" << endl;
 
-	
+	if (exportPvs) {
 	//-- export bounding boxes
 	stream << "<BoundingBoxes>" << endl;
@@ -738,5 +738,5 @@
 
 	stream << "</BoundingBoxes>" << endl;
-
+	}
 
 	//-- export the view cells and the pvs
@@ -1222,14 +1222,25 @@
 							   const float filterWidth)
 {
+
+	
+
+	
+  ViewCell *currentViewCell = GetViewCell(viewPoint);
+
+  if (mMaxFilterSize < 1) {
+	prvs.mViewCell = currentViewCell;
+	return;
+  }
+  
   const AxisAlignedBox3 box = GetFilterBBox(viewPoint, filterWidth);
-	
-  ViewCell *currentViewCell = GetViewCell(viewPoint);
-
+  
   if (currentViewCell) {
 	ViewCellContainer viewCells;
 	ComputeBoxIntersections(box, viewCells);
-	
+
+
 	ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells);
 	prvs.mViewCell = root;
+	
   } else
 	prvs.mViewCell = NULL;
@@ -1674,31 +1685,31 @@
 void ViewCellsManager::GetPvsStatistics(PvsStatistics &stat)
 {
-	// update pvs of view cells tree if necessary
-	UpdatePvs();
-
-	ViewCellContainer::const_iterator it = mViewCells.begin();
-
-	stat.viewcells = 0;
-	stat.minPvs = 100000000;
-	stat.maxPvs = 0;
-	stat.avgPvs = 0.0f;
-
-	for (; it != mViewCells.end(); ++ it) 
-	{
-		ViewCell *viewcell = *it;
-		
-		const int pvsSize = mViewCellsTree->GetPvsSize(viewcell);
-		
-		if (pvsSize < stat.minPvs)
-			stat.minPvs = pvsSize;
-		if (pvsSize > stat.maxPvs)
-			stat.maxPvs = pvsSize;
-		stat.avgPvs += pvsSize;
-		
-		++ stat.viewcells;
-	}
-
-	if (stat.viewcells)
-		stat.avgPvs/=stat.viewcells;
+  // update pvs of view cells tree if necessary
+  UpdatePvs();
+  
+  ViewCellContainer::const_iterator it = mViewCells.begin();
+  
+  stat.viewcells = 0;
+  stat.minPvs = 100000000;
+  stat.maxPvs = 0;
+  stat.avgPvs = 0.0f;
+  
+  for (; it != mViewCells.end(); ++ it) 
+	{
+	  ViewCell *viewcell = *it;
+	  
+	  const int pvsSize = mViewCellsTree->GetPvsSize(viewcell);
+	  
+	  if (pvsSize < stat.minPvs)
+		stat.minPvs = pvsSize;
+	  if (pvsSize > stat.maxPvs)
+		stat.maxPvs = pvsSize;
+	  stat.avgPvs += pvsSize;
+	  
+	  ++ stat.viewcells;
+	}
+  
+  if (stat.viewcells)
+	stat.avgPvs/=stat.viewcells;
 }
 
@@ -1869,13 +1880,11 @@
 			// if ray not outside of view space
 			float contribution;
-			if (ray.mTerminationObject &&
-				viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
-														 ray.mPdf,
-														 contribution))
-			{
+			if (ray.mTerminationObject) {
+			  if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
+														   ray.mPdf,
+														   contribution))
 				++ ray.mPvsContribution;
-				ray.mRelativePvsContribution += contribution;
+			  ray.mRelativePvsContribution += contribution;
 			}
-
 			// for directional sampling it is important to count only contributions
 			// made in one direction!!!
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 1004)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 1112)
@@ -73,9 +73,12 @@
 {
   mSceneGraph->CollectObjects(&mObjects);
+
+  //  mViewCellsManager->GetViewPoint(mViewPoint);
+
   mViewPoint = mSceneGraph->GetBox().Center();
   mViewDirection = Vector3(0,0,1);
 
-  mViewPoint = Vector3(991.7, 187.8, -271);
-  mViewDirection = Vector3(0.9, 0, -0.4);
+  //  mViewPoint = Vector3(991.7, 187.8, -271);
+  //  mViewDirection = Vector3(0.9, 0, -0.4);
 
   //  timerId = startTimer(10);
@@ -740,5 +743,5 @@
   // different executions of the algorithm
 
-  //mViewCellsManager->GetViewPoint(mViewPoint);
+  //  mViewCellsManager->GetViewPoint(mViewPoint);
 
   while (1) {
@@ -747,5 +750,5 @@
 							  halton.GetNumber(3));
 	
-	mViewPoint =  mSceneGraph->GetBox().GetPoint(pVector);
+	mViewPoint =  mViewCellsManager->GetViewSpaceBox().GetPoint(pVector);
 	ViewCell *v = mViewCellsManager->GetViewCell(mViewPoint);
 	if (v && v->GetValid())
@@ -754,5 +757,4 @@
 	halton.GenerateNext();
   }
-  
   
   Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4),
@@ -816,4 +818,5 @@
 	glDepthMask(GL_FALSE);
 	glDisable( GL_CULL_FACE );
+
 	
 	query.BeginQuery();
@@ -840,5 +843,6 @@
   
   PrVs prvs;
-  //  mViewCellsManager->SetMaxFilterSize(1);
+  
+  mViewCellsManager->SetMaxFilterSize(0);
   mViewCellsManager->GetPrVS(mViewPoint, prvs, mViewCellsManager->GetFilterWidth());
   viewcell = prvs.mViewCell;
@@ -898,5 +902,5 @@
 			if (qGreen(p) != 255 && index!=0) {
 			  if (index != lastIndex) {
-				Debug<<"ei="<<index<<" ";
+				//				Debug<<"ei="<<index<<" ";
 				lastIndex = index;
 			  }
@@ -931,5 +935,5 @@
   }
 
-  if (viewcell)
+  if (viewcell && mViewCellsManager->GetMaxFilterSize() > 0)
 	mViewCellsManager->DeleteLocalMergeTree(viewcell);
   
@@ -1183,5 +1187,8 @@
 
   makeCurrent();
+
+
   SetupProjection(GetWidth(), GetHeight());
+
   
   for (int i=0; i < mPvsStatFrames; i++) {
@@ -1204,4 +1211,6 @@
 	if (mPvsErrorBuffer[i].mError > 0.0f) {
 	  int pvsSize;
+
+
 	  float error = GetPixelError(pvsSize);
 	  mPvsErrorBuffer[i].mError = error;
@@ -1370,4 +1379,10 @@
 	updateGL();
 	break;
+  case Qt::Key_P:
+	// set random viewpoint
+	mViewCellsManager->GetViewPoint(mViewPoint);
+	updateGL();
+	break;
+
   default:
 	e->ignore();
@@ -1579,6 +1594,15 @@
   mRenderErrors = false;
   mRenderFilter = true;
-  mUseFilter = true;
-  mUseSpatialFilter = true;
+
+  bool tmp;
+
+  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
+  mUseFilter = tmp;
+  
+  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
+							tmp );
+
+  mUseSpatialFilter = tmp;
+
   mShowRenderCost = false;
   mShowPvsSizes = false;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/common.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 971)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 1112)
@@ -484,16 +484,16 @@
 
 // if the view cells should be exported and inported as gzstream
-#define ZIPPED_VIEWCELLS 1
-
-#endif
-
-
-
-
-
-
-
-
-
-
-
+#define ZIPPED_VIEWCELLS 0
+
+#endif
+
+
+
+
+
+
+
+
+
+
+
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1089)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1112)
@@ -281,5 +281,5 @@
   
   if (data)  {
-	data->mSumPdf+=pdf;
+	data->mSumPdf += pdf;
 	return data->mSumPdf;
   }
@@ -299,5 +299,5 @@
   
   if (data)  {
-	data->mSumPdf+=pdf;
+	data->mSumPdf += pdf;
 	contribution = pdf/data->mSumPdf;
 	return false;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 1012)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 1112)
@@ -176,5 +176,19 @@
 	mMax = center + newSize;
   }
-  
+
+  void Scale(const Vector3 &scale) {
+	Vector3 newSize = Size()*(scale*0.5f);
+	Vector3 center = Center();
+	mMin = center - newSize;
+	mMax = center + newSize;
+  }
+
+  /** Translates the box with the factor.
+  */
+  void Translate(const Vector3 &shift) {
+	mMin += shift;
+	mMax += shift;
+  }
+
   /** Returns the square of the minimal and maximal distance to 
 	a point on the box.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 878)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1112)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: út 2. V 11:18:23 2006
+# Generated by qmake (2.00a) (Qt 4.1.2) on: po 10. VII 19:18:45 2006
 # Project:  preprocessor.pro
 # Template: app
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.h	(revision 863)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.h	(revision 1112)
@@ -162,5 +162,5 @@
 	}
 
-#define USE_ORIGIN 0
+#define USE_ORIGIN 1
 	
 	Vector3 GetOrigin() const {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1004)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1112)
@@ -333,7 +333,7 @@
 	  leaf->dirBBox.SetMin(2, 0.0f);
 	  leaf->dirBBox.SetMax(2, 1.0f);
-	  
 	  mRoots[i] = leaf;
 	}
+
 	// init spatial bounding boxes
 	for (i = 0; i < objects.size(); i++) {
@@ -366,10 +366,13 @@
 	  
 	  // leaf bbox contains bbox of origins only
+
+	  // include both origin and terminatin in the global bbox
+#if USE_ORIGIN
 	  leaf->bbox.Include((*ri)->GetOrigin());
-	  
-	  // include both origin and terminatin in the global bbox
 	  bbox.Include((*ri)->GetOrigin());
+#else
 	  bbox.Include((*ri)->GetTermination());
-	  
+	  leaf->bbox.Include((*ri)->GetTermination());
+#endif	  
 	  Vector3 dVec = Vector3(
 							 (*ri)->GetDirParametrization(0),
@@ -396,6 +399,6 @@
   for (int i=0; i < mRoots.size(); i++) {
 	RssTreeLeaf *leaf = (RssTreeLeaf *)mRoots[i];
+	UpdatePvsSize(leaf);
 	stat.initialPvsSize += leaf->GetPvsSize();
-	UpdatePvsSize(leaf);
 	mRoots[i] = Subdivide(TraversalData(leaf, GetBBox(leaf), 0));
   }
@@ -458,5 +461,5 @@
 	  if (!node->IsLeaf()) {
 		subdivided++;
-
+		
 		
 		RssTreeInterior *interior = (RssTreeInterior *) node;
@@ -626,9 +629,10 @@
 	case 1: {
 	  float newContrib =
-		info.contributionBack/(info.position - minBox) + 
-		+
-		info.contributionFront/(maxBox - info.position);
-	  float oldContrib = info.contribution/sizeBox;
-	  info.costRatio = oldContrib/newContrib;
+		(info.contributionBack*(info.position - minBox) + 
+		 +
+		 info.contributionFront*(maxBox - info.position))/sizeBox;
+	  float oldContrib = info.contribution;
+	  info.costRatio = newContrib/oldContrib;
+	  //	  cout<<info.contribution<<" "<<info.contributionBack<<" "<<info.contributionFront<<endl;
 	  break;
 	} 
@@ -757,7 +761,18 @@
 	}
 
-  info.contribution = sumContribution/sumWeights;
-  info.contributionBack = sumContributionBack/sumWeightsBack;
-  info.contributionFront = sumContributionFront/sumWeightsFront;
+  if (sumWeights!=0.0f)
+	info.contribution = sumContribution/sumWeights;
+  else
+	info.contribution = 0.0f;
+
+  if (sumWeightsBack!=0.0f)
+	info.contributionBack = sumContributionBack/sumWeightsBack;
+  else
+	info.contributionBack = 0.0f;
+
+  if (sumWeightsFront!=0.0f)
+	info.contributionFront = sumContributionFront/sumWeightsFront;
+  else
+	info.contributionFront = 0.0f;
   
   GetCostRatio(
@@ -765,8 +780,8 @@
 			   info);
   
-  //	cout<<axis<<" "<<pvsSize<<" "<<pvsBack<<" "<<pvsFront<<endl;
+  //cout<<axis<<" "<<pvsSize<<" "<<pvsBack<<" "<<pvsFront<<endl;
   //  float oldCost = leaf->rays.size();
   
-  //	cout<<"ratio="<<ratio<<endl;
+  //  cout<<"ratio="<<ratio<<endl;
 }
 
@@ -2054,5 +2069,5 @@
 
 
-  const float smoothRange = 0.0f;
+  const float smoothRange = 0.1f;
   if (smoothRange != 0.0f) {
 	box.Scale(1.0f + smoothRange);
@@ -2078,5 +2093,6 @@
   }
   
-  int numberOfTries = numberOfRays*4;
+  //  int numberOfTries = numberOfRays*4;
+  int numberOfTries = numberOfRays;
   int generated = 0;
 
@@ -2104,5 +2120,7 @@
   
   float extendedConvexCombinationProb = 0.0f; //0.7f
-  float silhouetteCheckPercentage = 1.0f; //0.5f
+  //  float silhouetteCheckPercentage = 1.0f; //0.5f
+  float silhouetteCheckPercentage = 0.0f; //0.9f; // 0.5f;
+  float silhouetteObjectCheckPercentage = 0.8f;
   for (int i=0; generated < numberOfRays && i < numberOfTries; i++) {
 	bool useExtendedConvexCombination = ((nrays >= 2) && (Random(1.0f) <
@@ -2179,6 +2197,5 @@
 	bool intersects = false;
 	
-
-	if (i > numberOfRays*(1.0f - silhouetteCheckPercentage)) {
+	if (i >= numberOfRays*(1.0f - silhouetteCheckPercentage)) {
 	  if (exporter) {
 		VssRay *ray = new VssRay(origin, origin + 100*direction, NULL, NULL);
@@ -2194,7 +2211,6 @@
 		Intersectable *object = *oi;
 		// do not test every object
-		if (Random(1.0f) > 0.5f)
-		  break;
-		if ( object->CastRay(traversalRay) ) {
+		if ( Random(1.0f) <= silhouetteObjectCheckPercentage &&
+			 object->CastRay(traversalRay) ) {
 		  intersects = true;
 		  break;
@@ -2211,12 +2227,12 @@
   }
 
-  
   //  cout<<"desired="<<numberOfRays<<" tries="<<i<<endl;
   // assign pdfs to the generated rays
-  float p = 1.0f; //(box.GetVolume()*dirBox.GetVolume())/i;
-  rays.mSumPdf = 0.0f;
+  // float density = generated/(box.SurfaceArea()*dirBox.GetVolume());
+  float density = 1.0f;
+
+  float p = 1.0f/density; //(box.GetVolume()*dirBox.GetVolume())/i;
   for (i=startIndex; i < rays.size(); i++) {
 	rays[i].mPdf = p;
-	rays.mSumPdf += p;
   }
   
@@ -2227,5 +2243,4 @@
 	CLEAR_CONTAINER(selectedRays);
   }
-  
   
 }
@@ -2316,4 +2331,5 @@
 	if (leaf->rays[i].mRay->RefCount() != 0) {
 	  cerr<<"Error: refcount!=0, but"<<leaf->rays[i].mRay->RefCount()<<endl;
+	  cerr<<"desired="<<desired<<"i="<<i<<" size="<<leaf->rays.size()<<endl;
 	  exit(1);
 	}
@@ -2412,7 +2428,8 @@
   CollectLeaves(leaves);
 	
-  sort(leaves.begin(),
-	   leaves.end(),
-	   GreaterContribution);
+  if (numberOfLeaves != leaves.size())
+	sort(leaves.begin(),
+		 leaves.end(),
+		 GreaterContribution);
 			 
 
@@ -2431,6 +2448,7 @@
   
   // always generate at leat n ray per leaf
-  int fixedPerLeaf = 1;
-  int fixed = 1*leaves.size();
+  int fixedPerLeaf = 0;
+  // int fixedPerLeaf = 0;
+  int fixed = fixedPerLeaf*leaves.size();
   int iGenerated = numberOfRays;
   float ratioPerLeaf = iGenerated /(avgContrib*numberOfLeaves);
@@ -2485,18 +2503,6 @@
 // if 1.0f then weighs = 1 1/2 1/3 1/4
 float passSampleWeightDecay = 1.0f;
-
-void
-RssTree::GetRayContribution(const RssTreeNode::RayInfo &info,
-							float &weight,
-							float &contribution)
-{
-  VssRay *ray = info.mRay;
-  weight = 1.0f/(mCurrentPass - ray->mPass + passSampleWeightDecay);
-  contribution =
-	weightAbsContributions*ray->mPvsContribution +
-	(1.0f - weightAbsContributions)*ray->mRelativePvsContribution;
-  // store the computed value
-  info.mRay->mWeightedPvsContribution = weight*contribution;
-}
+//float passSampleWeightDecay = 0.0001f;
+
 
 float
@@ -2506,12 +2512,12 @@
   float weight;
   if (1) 
-	weight = 1.0f/(passDiff + passSampleWeightDecay);
+	weight = 1.0f/sqr(passDiff + passSampleWeightDecay);
   else 
 	switch (passDiff) {
-	case 0:
+	  //	case 0:
+	  //	  weight = 1.0f;
+	  //	  break;
+	default:
 	  weight = 1.0f;
-	  break;
-	default:
-	  weight = 0.0f;
 	  break;
 	  // 	case 1:
@@ -2535,4 +2541,19 @@
 
 void
+RssTree::GetRayContribution(const RssTreeNode::RayInfo &info,
+							float &weight,
+							float &contribution)
+{
+  VssRay *ray = info.mRay;
+  weight = GetSampleWeight(mCurrentPass);
+  contribution =
+	weightAbsContributions*ray->mPvsContribution +
+	(1.0f - weightAbsContributions)*ray->mRelativePvsContribution;
+  // store the computed value
+  info.mRay->mWeightedPvsContribution = weight*contribution;
+}
+
+
+void
 RssTree::ComputeImportance(RssTreeLeaf *leaf) 
 {
@@ -2553,5 +2574,5 @@
 	  sumRelContributions += weight*ray->mRelativePvsContribution;
 	  
-	  //	  sumWeights += weight;
+	  //sumWeights += weight;
 	  sumWeights += 1.0f;
 	}
@@ -2576,4 +2597,5 @@
 RssTreeLeaf::GetImportance()  const
 {
+  //return sqr(mImportance);
   return mImportance;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 863)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1112)
@@ -220,6 +220,5 @@
   bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
 
-  char Flags() const { return mFlags;}
-  void SetFlags(char orFlag) { mFlags |= orFlag;}
+  char Flags() const { return mFlags;}  void SetFlags(char orFlag) { mFlags |= orFlag;}
   
   bool IsActive() const { return mRefCount>0; }
@@ -312,5 +311,5 @@
 {
   void PrintStatistics(ostream &s);
-  int SelectRays(const int number, VssRayContainer &selected) const;
+  int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
   int
   GetContributingRays(VssRayContainer &selected,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1109)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1112)
@@ -13,10 +13,15 @@
 namespace GtpVisibilityPreprocessor {
 
-class KdNode;
-class KdLeaf;
-class KdInterior;
-class Intersectable;
-//class KdViewCell;
-class Beam;
+  
+  class KdNode;
+  class KdLeaf;
+  class KdInterior;
+  class Intersectable;
+  //class KdViewCell;
+  class Beam;
+
+  class KdTree;
+  
+  //  KdTree *SceneKdTree;
 
 // --------------------------------------------------------------
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1076)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1112)
@@ -30,12 +30,14 @@
 {
 
-	//Now just call this function at the start of your program and if you're
-	//compiling in debug mode (F5), any leaks will be displayed in the Output
-	//window when the program shuts down. If you're not in debug mode this will
-	//be ignored. Use it as you will!
-	//note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
-    _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
-    _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
-    _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); 
+  //Now just call this function at the start of your program and if you're
+  //compiling in debug mode (F5), any leaks will be displayed in the Output
+  //window when the program shuts down. If you're not in debug mode this will
+  //be ignored. Use it as you will!
+  //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
+  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
+  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
+  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); 
+
+  InitTiming();
 
   Debug.open("debug.log");
@@ -136,5 +138,5 @@
 	rendererWidget->show();
 	
-	if (p->GetRenderer()) {
+	if (0 && p->GetRenderer()) {
 	  
 	  cout<<"CONNECTING"<<endl;
@@ -142,5 +144,5 @@
 					   SIGNAL(UpdatePvsErrorItem(int i,
 												 GlRendererBuffer::PvsErrorEntry &)),
-
+					   
 					   rendererWidget->mControlWidget,
 					   SLOT(UpdatePvsErrorItem(int i,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp	(revision 863)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp	(revision 1112)
@@ -230,5 +230,6 @@
 int
 VssRayContainer::SelectRays(const int number,
-							VssRayContainer &selected) const
+							VssRayContainer &selected,
+							const bool copy) const
 {
   float p = number/(float)size();
@@ -236,6 +237,10 @@
   
   for (; it != end(); it++)
-	if (Random(1.0f) < p)
-	  selected.push_back(*it);
+	if (Random(1.0f) < p) {
+	  if (!copy)
+		selected.push_back(*it);
+	  else
+		selected.push_back(new VssRay(**it));
+	}
   
   return (int)selected.size();
@@ -249,7 +254,8 @@
 {
   VssRayContainer::const_iterator it = begin();
-  
+  //  ofstream s("contrib.log");
   for (; it != end(); it++) {
 	VssRay *ray = *it;
+	//	s<<"(pass="<<ray->mPass<<", c="<<ray->mPvsContribution<<")"<<endl;
 	if (ray->mPass >= minPass && ray->mPvsContribution > 0)
 	  selected.push_back(ray);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 811)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 1112)
@@ -13,5 +13,7 @@
 
 
-win32:LIBPATH += $$NONGTP/Xerces/xerces/lib $$NONGTP/Devil/lib "d:/Programs/NVIDIA Corporation/Cg/lib"
+win32:LIBPATH += GL $$NONGTP/Xerces/xerces/lib $$NONGTP/Devil/lib \
+  "d:/Programs/NVIDIA Corporation/Cg/lib" $$NONGTP/glut
+  
 unix:LIBPATH += ../support/src/xerces-c-src_2_7_0/lib ../support/devil/lib /usr/lib/qt3/lib64
 
@@ -28,5 +30,6 @@
 QT += opengl
 
-win32:LIBS += xerces-c_2.lib devil.lib ilu.lib ilut.lib cg.lib cgGL.lib
+win32:LIBS += xerces-c_2.lib devil.lib ilu.lib ilut.lib cg.lib cgGL.lib glew32.lib 
+
 unix:LIBS += -lxerces-c -lIL -lILU -lILUT
 
@@ -44,7 +47,11 @@
 MutualVisibility.cpp Triangle3.cpp Rectangle3.cpp Plane3.cpp Polygon3.cpp \
 ViewCell.cpp ViewCellBsp.cpp Halton.cpp VssRay.cpp VssTree.cpp VssPreprocessor.cpp \
-RenderSimulator.cpp VspKdTree.cpp RayInfo.cpp RssTree.cpp RssPreprocessor.cpp \
+RenderSimulator.cpp RayInfo.cpp RssTree.cpp RssPreprocessor.cpp \
 ViewCellsManager.cpp VspBspTree.cpp GlRenderer.cpp \
 PreprocessorThread.cpp Renderer.cpp Beam.cpp ViewCellsParser.cpp Tetrahedron3.cpp \
-VrmlExporter.cpp PlyParser.cpp plyfile.c RenderSampler.cpp
+VrmlExporter.cpp PlyParser.cpp plyfile.c RenderSampler.cpp \
+GzBinFileInputStream.cpp  GzFileInputSource.cpp \
+OcclusionQuery.cpp VspOspTree.cpp LogManager.cpp \
+SamplingStrategy.cpp
 
+#VspKdTree.cpp ResourceManager.cpp 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspKdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspKdTree.h	(revision 1012)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspKdTree.h	(revision 1112)
@@ -35,9 +35,10 @@
 #include "ViewCell.h"
 
+
+namespace GtpVisibilityPreprocessor {
+
 class VspKdLeaf;
 class ViewCellsManager;
 class ViewCellsStatistics;
-
-namespace GtpVisibilityPreprocessor {
 
 /**
