Index: GTP/trunk/Lib/Vis/Preprocessing/src/run_test2
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 1756)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 1757)
@@ -2,5 +2,5 @@
 
 #COMMAND="./release/preprocessor.exe -preprocessor_quit_on_finish+"
-COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+"
+COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer-"
 
 #SCENE="../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d"
@@ -27,5 +27,5 @@
 
 SCENE=../data/vienna/vienna_cropped.obj
-VIEWCELLS=../data/vienna/vienna_cropped-gradient-2-viewcells.xml.gz
+VIEWCELLS=../data/vienna/vienna_cropped-gradient-viewcells.xml.gz
 
 PREFIX=../work/plots/osp-rss
@@ -37,9 +37,9 @@
 #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
+#$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 
+#mv stats.log $PREFIX-i-combined-update-ccb3.log 
 
 # $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
@@ -54,28 +54,28 @@
 
 
-# $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 
+$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 
 
 
 
-# #######
-# #######
-# $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
+#######
+#######
+$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
 
 
-# $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
+$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
 
 
 
-#$COMMAND -scene_filename=$SCENE -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling+
-#mv stats.log $PREFIX-r-direction-based-b3.log
+$COMMAND -scene_filename=$SCENE -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling+
+mv stats.log $PREFIX-r-direction-based-b3.log
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp	(revision 1743)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp	(revision 1757)
@@ -15,7 +15,10 @@
 #include "QtPreprocessorThread.h"
 
-
+#define USE_CG 1
+
+#ifdef USE_CG
 #include <Cg/cg.h>
 #include <Cg/cgGL.h>
+#endif
 
 #include <QVBoxLayout>
@@ -48,5 +51,5 @@
 }
 
-
+#if USE_CG
 static void handleCgError() 
 {
@@ -54,4 +57,5 @@
     exit(1);
 }
+#endif
 
 void
@@ -150,8 +154,10 @@
 QtGlRendererBuffer::~QtGlRendererBuffer()
 {
-	if (sCgFragmentProgram)
+#if USE_CG
+  if (sCgFragmentProgram)
 		cgDestroyProgram(sCgFragmentProgram);
 	if (sCgContext)
 		cgDestroyContext(sCgContext);
+#endif
 }
 
@@ -604,8 +610,9 @@
 	glEnable(GL_TEXTURE_2D);
 	
+#if USE_CG
 	// bind pixel shader implementing the front depth buffer functionality
 	cgGLBindProgram(sCgFragmentProgram);
 	cgGLEnableProfile(sCgFragmentProfile);
-
+#endif
 
 	// 5. render all objects inside the beam 
@@ -752,5 +759,7 @@
 	glEnable(GL_CULL_FACE);
 	glDisable(GL_STENCIL_TEST);
+#if USE_CG
 	cgGLDisableProfile(sCgFragmentProfile);
+#endif
 	glDisable(GL_TEXTURE_2D);
 
@@ -822,4 +831,5 @@
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
 
+#if USE_CG
 	// cg initialization
 	cgSetErrorCallback(handleCgError);
@@ -840,5 +850,4 @@
   }
 
-
  sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,
 											   CG_SOURCE, "../src/dual_depth.cg",
@@ -855,4 +864,6 @@
   Debug << "---- PROGRAM BEGIN ----\n" <<
 	  cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n";
+
+#endif
 
 #endif
@@ -1570,5 +1581,5 @@
 	  }
 
-	  for (int i=0;  pow(10, i) < maxCost; i+=1) {
+	  for (int i=0;  pow(10.0f, i) < maxCost; i+=1) {
 		float y = i*scaley;
 		//		QString s;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1756)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1757)
@@ -1732,10 +1732,10 @@
 	RegisterOption("Preprocessor.useGlRenderer",
 					optBool,
-					"preprocessor_use_gl_renderer=",
+					"preprocessor_use_gl_renderer",
 					"false");
 
 	RegisterOption("Preprocessor.useGlDebugger",
 					optBool,
-					"preprocessor_use_gl_debugger=",
+					"preprocessor_use_gl_debugger",
 					"false");
 
@@ -2625,4 +2625,5 @@
     strcpy(filename, "default.env");
   }
+
   
   if (useExePath) {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1749)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1757)
@@ -1059,5 +1059,5 @@
 					   )
 {
-	const long t1 = GetTime();
+  const long t1 = GetTime();
 
 	for (int i = 0; i < (int)rays.size();)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.h	(revision 1743)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.h	(revision 1757)
@@ -174,5 +174,5 @@
 struct PvsCache {
   PvsCache():mViewCell(NULL) {}
-  Reset() { mViewCell = NULL; mPvs.Clear(); }
+  void Reset() { mViewCell = NULL; mPvs.Clear(); }
   ViewCell *mViewCell;
   ObjectPvs mPvs;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1743)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1757)
@@ -75,6 +75,6 @@
 
 SamplingPreprocessor {
-	totalSamples 100000000
-	samplesPerPass 1000000
+	totalSamples 50000000
+	samplesPerPass 3000000
 }
 
@@ -82,5 +82,5 @@
 	samplesPerPass 1000
 	initialSamples 2000000
-	vssSamples 100000000
+	vssSamples 50000000
 #	vssSamples 1000000
 	vssSamplesPerPass 3000000
@@ -289,5 +289,5 @@
 #	filename ../data/vienna/vsposp-seq-viewCells.xml.gz
 
-	filename ../data/vienna/vienna_cropped-gradient-2-viewcells.xml.gz
+	filename ../data/vienna/vienna_cropped-gradient-viewcells.xml.gz
 
 #	filename ../data/vienna/vienna_cropped-2-sequential-30000-viewcells.xml.gz
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1749)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1757)
@@ -2424,5 +2424,6 @@
 
 
-void BvHierarchy::CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects)
+void BvHierarchy::CollectObjects(const AxisAlignedBox3 &box,
+								 ObjectContainer &objects)
 {
   stack<BvhNode *> nodeStack;
@@ -2430,33 +2431,30 @@
   nodeStack.push(mRoot);
 
-  while (!nodeStack.empty()) 
-	{
-	  BvhNode *node = nodeStack.top();
+  while (!nodeStack.empty()) {
+	BvhNode *node = nodeStack.top();
+	
+	nodeStack.pop();
+	
+	if (node->IsLeaf()) {
+	  BvhLeaf *leaf = (BvhLeaf *)node;
+	  if (Overlap(box, leaf->GetBoundingBox())) {
+		Intersectable *object = GetOrCreateBvhIntersectable(leaf);
+		if (!object->Mailed()) {
+		  object->Mail();
+		  objects.push_back(object);
+		}
+	  }
+	} 
+	else  {
+	  BvhInterior *interior = (BvhInterior *)node;
 	  
-	  nodeStack.pop();
+	  if (Overlap(box, interior->GetBoundingBox()))
+		nodeStack.push(interior->GetFront());
 	  
-	  if (node->IsLeaf()) 
-		{
-		  BvhLeaf *leaf = (BvhLeaf *)node;
-		  if (Overlap(box, leaf->GetBoundingBox())) {
-			Intersectable *object = GetOrCreateBvhIntersectable(leaf);
-			if (!object->Mailed()) {
-			  object->Mail();
-			  objects.push_back(object);
-			}
-		  }
-		} 
-	  else 
-		{
-		  BvhInterior *interior = (BvhInterior *)node;
-		  
-		  if (Overlap(box, interior->GetBoundingBox()))
-			nodeStack.push(interior->GetFront());
-		  
-		  if (Overlap(box, interior->GetBoundingBox()))
-			nodeStack.push(interior->GetBack());
-		}
-	}
-}
-
-}
+	  if (Overlap(box, interior->GetBoundingBox()))
+		nodeStack.push(interior->GetBack());
+	}
+  }
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1749)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1757)
@@ -126,5 +126,6 @@
 		
 	  for (int j=0; j < vssRays.size(); j++) {
-		Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]), true);
+		Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
+																 true);
 		if (obj) {
 		  // if ray not outside of view space
@@ -148,8 +149,10 @@
 	}
 
+#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 <<
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1756)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1757)
@@ -1835,5 +1835,7 @@
 	  stat.avgPvs += pvsCost;
 
-	  if (0) {
+	  const bool evaluateFilter = true;
+	  
+	  if (evaluateFilter) {
 		ObjectPvs filteredPvs = viewcell->GetPvs();
 		ApplyFilter2(viewcell, false, 1.0f, filteredPvs);
@@ -1841,4 +1843,7 @@
 		stat.avgFilteredPvs += filteredCost;
 		stat.avgFilterContribution += filteredCost - pvsCost;
+	  } else {
+		stat.avgFilteredPvs += pvsCost;
+		stat.avgFilterContribution += 0;
 	  }
 	  
@@ -2594,26 +2599,26 @@
   AxisAlignedBox3 vbox = GetViewCellBox(viewCell);
   Vector3 center = vbox.Center();
-  // first determine the average size of the filter
-
-  pvs = viewCell->GetPvs();
+  
+  // COpy the PVS
+  ObjectPvs basePvs = viewCell->GetPvs();
   Intersectable::NewMail();
-  
-  ObjectPvsIterator pit = pvs.GetIterator();
+
+  ObjectPvsIterator pit = basePvs.GetIterator();
 
   // first mark all object from this pvs
-  while (pit.HasMoreEntries())
-  {		
-	  ObjectPvsEntry entry = pit.Next();
-
-	  Intersectable *object = entry.mObject;
-	  object->Mail();
+  while (pit.HasMoreEntries()) {		
+	ObjectPvsEntry entry = pit.Next();
+	
+	Intersectable *object = entry.mObject;
+	object->Mail();
   }
 
-  ObjectPvs nPvs;
+
   int pvsSize = 0;
   int nPvsSize = 0;
   float samples = (float)pvs.GetSamples();
-  cout<<"Samples = "<<samples<<endl;
-   cout<<"pvs size = "<<pvs.GetSize() <<endl;
+
+  //  cout<<"#s"<<samples<<endl;
+  //  cout<<"pvs size = "<<pvs.GetSize() <<endl;
   //  cout<<"Filter size = "<<filterSize<<endl;
   //  cout<<"vbox = "<<vbox<<endl;
@@ -2621,41 +2626,18 @@
 
 
+   // Minimal number of local samples to take into account
+   // the local sampling density.
+   // The size of the filter is a minimum of the conservative
+   // local sampling density estimate (#rays intersecting teh viewcell and
+   // the object)
+   // and gobal estimate for the view cell
+   // (total #rays intersecting the viewcell)
 #define MIN_LOCAL_SAMPLES 5
 
-  // compute the average filter radius
   float globalC = 2.0f*filterSize/sqrt(samples);
-
-  float sumRadius = 0.0f;
-
-  ObjectPvsIterator pit2 = pvs.GetIterator();
-
-  // first mark all object from this pvs
-   for (; pit2.HasMoreEntries(); pvsSize++) {
-  ObjectPvsEntry entry = pit2.Next();
-	Intersectable *object = entry.mObject;
-	// compute filter size based on the distance and the numebr of samples
-	AxisAlignedBox3 box = object->GetBox();
-
-	float distance = Distance(center, box.Center());
-	float globalRadius = distance*globalC;
-
-	int objectSamples = (int)entry.mData.mSumPdf;
-	float localRadius = MAX_FLOAT;
-	
-	if (objectSamples > MIN_LOCAL_SAMPLES)
-	  localRadius = 0.5f*Magnitude(box.Diagonal())/sqrt((float)objectSamples);
-	
-	// now compute the filter size
-	float radius = Min(localRadius, globalRadius);
-	
-	sumRadius +=radius;
-  }
-  
-  float avgRadius = sumRadius/pvsSize;
   float viewCellRadius = 0.5f*Magnitude(vbox.Diagonal());
-  cout<<"radius ratio = "<<avgRadius/viewCellRadius<<endl;
   
   // now compute the filter box around the current viewCell
-
+  
   if (useViewSpaceFilter) {
 	//	float radius = Max(viewCellRadius/100.0f, avgRadius - viewCellRadius);
@@ -2666,67 +2648,72 @@
 	ComputeBoxIntersections(vbox, viewCells);
 	
-	//  cout<<"box="<<box<<endl;
-	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
-	
-	int i;
+	ViewCellContainer::const_iterator it = viewCells.begin(),
+	  it_end = viewCells.end();
+	int i = 0;
 	for (i=0; it != it_end; ++ it, ++ i)
 	  if ((*it) != viewCell) {
 		//cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
-		  pvs.MergeInPlace((*it)->GetPvs());
+		basePvs.MergeInPlace((*it)->GetPvs());
 	  }
-
+	
 	// update samples and globalC
 	samples = (float)pvs.GetSamples();
 	globalC = 2.0f*filterSize/sqrt(samples);
-	cout<<"neighboring viewcells = "<<i-1<<endl;
-	cout<<"Samples' = "<<samples<<endl;
+	//	cout<<"neighboring viewcells = "<<i-1<<endl;
+	//	cout<<"Samples' = "<<samples<<endl;
   }
   
-  pit = pvs.GetIterator();
-
-  // first mark all object from this pvs
+  pit = basePvs.GetIterator();
+
+  ObjectContainer objects;
+
   while (pit.HasMoreEntries())
   {		
-	  ObjectPvsEntry entry = pit.Next();
-
-	  Intersectable *object = entry.mObject;
-	  // compute filter size based on the distance and the numebr of samples
-	  AxisAlignedBox3 box = object->GetBox();
-
-	  float distance = Distance(center, box.Center());
-	  float globalRadius = distance*globalC;
-
-	  int objectSamples = (int)entry.mData.mSumPdf;
-	  float localRadius = MAX_FLOAT;
-	  if (objectSamples > MIN_LOCAL_SAMPLES)
-		  localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/sqrt((float)objectSamples);
-
-	  //	cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl;
-
-	  // now compute the filter size
-	  float radius = Min(localRadius, globalRadius);
-
-	  // cout<<"box = "<<box<<endl;
-	  //	cout<<"distance = "<<distance<<endl;
-	  //	cout<<"radiues = "<<radius<<endl;
-
-	  box.Enlarge(Vector3(radius));
-
-	  
-	  ObjectContainer objects;
-	  // $$ warning collect objects takes only unmailed ones!
-	  CollectObjects(box, objects);
-	  //	cout<<"collected objects="<<objects.size()<<endl;
-	  ObjectContainer::const_iterator noi = objects.begin();
-	  for (; noi != objects.end(); ++ noi) {
-		Intersectable *o = *noi;
-		// $$ JB warning: pdfs are not correct at this point!	  
-		nPvs.AddSample(o, Limits::Small);
-		nPvsSize ++;
-	  }
+	ObjectPvsEntry entry = pit.Next();
+
+	Intersectable *object = entry.mObject;
+	// compute filter size based on the distance and the numebr of samples
+	AxisAlignedBox3 box = object->GetBox();
+	
+	float distance = Distance(center, box.Center());
+	float globalRadius = distance*globalC;
+	
+	int objectSamples = (int)entry.mData.mSumPdf;
+	float localRadius = MAX_FLOAT;
+	if (objectSamples > MIN_LOCAL_SAMPLES)
+	  localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/
+		sqrt((float)objectSamples);
+	
+	//	cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl;
+	
+	// now compute the filter size
+	float radius = Min(localRadius, globalRadius);
+	
+	// cout<<"box = "<<box<<endl;
+	//	cout<<"distance = "<<distance<<endl;
+	//	cout<<"radiues = "<<radius<<endl;
+	
+	box.Enlarge(Vector3(radius));
+	
+	objects.clear();
+	// $$ warning collect objects takes only unmailed ones!
+	CollectObjects(box, objects);
+	//	cout<<"collected objects="<<objects.size()<<endl;
+	ObjectContainer::const_iterator noi = objects.begin();
+	for (; noi != objects.end(); ++ noi) {
+	  Intersectable *o = *noi;
+	  // $$ JB warning: pdfs are not correct at this point!	  
+	  pvs.AddSampleDirty(o, Limits::Small);
+	}
   }
-  cout<<"nPvs size = "<<nPvsSize<<endl;
-  
-  pvs.MergeInPlace(nPvs);
+
+  //  cout<<"nPvs size = "<<pvs.GetSize()<<endl;
+
+  // copy the base pvs to the new pvs
+  pit = basePvs.GetIterator();
+  while (pit.HasMoreEntries()) {		
+	ObjectPvsEntry entry = pit.Next();
+	pvs.AddSampleDirty(entry.mObject, entry.mData.mSumPdf);
+  }
   
   Intersectable::NewMail();
@@ -3908,5 +3895,5 @@
 			exporter->ExportKdTree(*mKdTree);
 
-			for (i = 0; i < pvsOut; i++)
+			for (int i = 0; i < pvsOut; i++)
 				exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
 
@@ -3918,5 +3905,5 @@
 		{
 			Intersectable *object = objects[k];
-			char str[64]; sprintf(str, "viewcell%04d.wrl", i);
+			char str[64]; sprintf(str, "viewcell%04d.wrl", k);
 
 			Exporter *exporter = Exporter::GetExporter(str);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/common.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 1717)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 1757)
@@ -54,5 +54,5 @@
 #else // __WATCOMC__
 #define M_PI        3.14159265358979323846
-#define MAXFLOAT    3.40282347e+38F
+#define MAXFLOAT    3.40282347e+37F
 #endif // __WATCOMC__
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1751)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1757)
@@ -159,5 +159,5 @@
 public:
 
-	Pvs(): mSamples(0), mEntries() {}
+	Pvs(): mSamples(0), mEntries(), mLastSorted(0) {}
 
 	/** creates pvs and initializes it with the given entries. 
@@ -205,5 +205,14 @@
 	*/
 	float AddSample(T sample, const float pdf);
-	
+
+  /** Adds sample to PVS without checking for presence of the sample
+	  pvs remians unsorted!
+  */
+  void AddSampleDirty(T sample, const float pdf);
+
+  /** Sort pvs entries - this should always be called after a
+	  sequence of AddSampleDirty calls */
+  void Sort();
+  
 	/** Adds sample to PVS. Assumes that the pvs is sorted
 		@returns contribution of sample (0 or 1)
@@ -271,4 +280,8 @@
 	/// Number of samples used to create the PVS
 	int mSamples;
+  
+  /// Last sorted entry in the pvs (important for find and merge
+  int mLastSorted;
+  
 };
 
@@ -279,6 +292,15 @@
 	mEntries.reserve(samples.size());
 	mEntries = samples;
-}
-
+	mLastSorted = 0;
+}
+
+template <typename T, typename S>
+void Pvs<T, S>::Sort()
+{
+  std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin();
+  it.inc(mLastSorted);
+  sort(it, mEntries.end());
+  mLastSorted = mEntries.size() - 1;
+}
 
 /**
@@ -500,4 +522,12 @@
 
 template <typename T, typename S>
+void Pvs<T, S>::AddSampleDirty(T sample, const float pdf)
+{
+  ++ mSamples;
+  mEntries.push_back(PvsEntry<T, S>(sample, pdf));
+}
+					 
+
+template <typename T, typename S>
 typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample, const float pdf)
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 1533)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 1757)
@@ -6,5 +6,5 @@
 #include "ArchModeler2MLRT.hxx"
 
-#define DEBUG_RAYCAST 0
+#define DEBUG_RAYCAST 0 
 
 
@@ -131,69 +131,92 @@
 
 	for (i=0; i < num; i++) {
-		mlrtaStoreRayAS16(&rays[index + i].mOrigin.x,
-			&rays[index + i].mDirection.x,
-			i);
-	}
+	  mlrtaStoreRayAS16(&rays[index + i].mOrigin.x,
+						&rays[index + i].mDirection.x,
+						i);
+	}
+
+#if DEBUG_RAYCAST
+		Debug<<"TA\n"<<flush;
+#endif
 
 	mlrtaTraverseGroupAS16(&min.x,
-		&max.x,
-		forward_hit_triangles,
-		forward_dist);
-
+						   &max.x,
+						   forward_hit_triangles,
+						   forward_dist);
+	
 	if (castDoubleRay)
-	{
+	  {
 		for (i=0; i < num; i++) 
-		{
+		  {
 			Vector3 dir = -rays[index + i].mDirection;
 			mlrtaStoreRayAS16(&rays[index+i].mOrigin.x,
-				&dir.x,
-				i);
-		}
+							  &dir.x,
+							  i);
+		  }
+
+#if DEBUG_RAYCAST
+		Debug<<"TB\n"<<flush;
+#endif
 
 		mlrtaTraverseGroupAS16(&min.x,
-			&max.x,
-			backward_hit_triangles,
-			backward_dist);
-	}
+							   &max.x,
+							   backward_hit_triangles,
+							   backward_dist);
+	  }
+
+#if DEBUG_RAYCAST
+	Debug<<"BBB\n"<<flush;
+#endif
 
 	for (i=0; i < num; i++) 
-	{
+	  {
 		Intersection hitA(rays[i].mOrigin), hitB(rays[i].mOrigin);
+
+#if DEBUG_RAYCAST
+	  Debug<<"FH\n"<<flush;
+#endif
 		
 		if (forward_hit_triangles[i] != -1 ) {
-			if (forward_hit_triangles[i] >= mPreprocessor.mFaceParents.size())
-				cerr<<"Warning: triangle index out of range! "<<forward_hit_triangles[i]<<endl;
-			else { 
-				hitA.mObject = mPreprocessor.mFaceParents[forward_hit_triangles[i]].mObject;
-				// Get the normal of that face
-				hitA.mNormal = hitA.mObject->GetNormal(mPreprocessor.mFaceParents[forward_hit_triangles[i]].mFaceIndex);
-				//-rays[index+i].mDirection; // $$ temporary
-				hitA.mPoint = rays[index+i].Extrap(forward_dist[i]);
+		  if (forward_hit_triangles[i] >= mPreprocessor.mFaceParents.size())
+			cerr<<"Warning: triangle index out of range! "<<forward_hit_triangles[i]<<endl;
+		  else { 
+			hitA.mObject = mPreprocessor.mFaceParents[forward_hit_triangles[i]].mObject;
+			// Get the normal of that face
+			hitA.mNormal = hitA.mObject->GetNormal(mPreprocessor.mFaceParents[forward_hit_triangles[i]].mFaceIndex);
+			//-rays[index+i].mDirection; // $$ temporary
+			hitA.mPoint = rays[index+i].Extrap(forward_dist[i]);
 			}
+	  }
+#if DEBUG_RAYCAST
+	  Debug<<"BH\n"<<flush;
+#endif
+	  
+	  if (castDoubleRay && (backward_hit_triangles[i] != -1)) {
+		if (backward_hit_triangles[i] >= mPreprocessor.mFaceParents.size())
+		  cerr<<"Warning: triangle  index out of range! "<<backward_hit_triangles[i]<<endl;
+		else { 
+		  hitB.mObject = mPreprocessor.mFaceParents[backward_hit_triangles[i]].mObject;
+		  hitB.mNormal = hitB.mObject->GetNormal(mPreprocessor.mFaceParents[backward_hit_triangles[i]].mFaceIndex);
+		  
+		  // normalB = rays[index+i].mDirection; // $$ temporary
+		  hitB.mPoint = rays[index+i].Extrap(-backward_dist[i]);
 		}
-
-		if (castDoubleRay && (backward_hit_triangles[i] != -1)) {
-			if (backward_hit_triangles[i] >= mPreprocessor.mFaceParents.size())
-				cerr<<"Warning: triangle  index out of range! "<<backward_hit_triangles[i]<<endl;
-			else { 
-				hitB.mObject = mPreprocessor.mFaceParents[backward_hit_triangles[i]].mObject;
-				hitB.mNormal = hitB.mObject->GetNormal(mPreprocessor.mFaceParents[forward_hit_triangles[i]].mFaceIndex);
-
-				// normalB = rays[index+i].mDirection; // $$ temporary
-				hitB.mPoint = rays[index+i].Extrap(-backward_dist[i]);
-			}
-		}
-
-		ProcessRay(
-			rays[index + i],
-			hitA,
-			hitB,
-			vssRays,
-			sbox,
-			castDoubleRay,
-			pruneInvalidRays
-			);
-	}
-
+	  }
+	  
+#if DEBUG_RAYCAST
+	  Debug<<"PR\n"<<flush;
+#endif
+	  
+	  ProcessRay(
+				 rays[index + i],
+				 hitA,
+				 hitB,
+				 vssRays,
+				 sbox,
+				 castDoubleRay,
+				 pruneInvalidRays
+				 );
+	}
+	
 #if DEBUG_RAYCAST
 	Debug<<"C16F\n"<<flush;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1743)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1757)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: po 13. XI 15:12:57 2006
+# Generated by qmake (2.00a) (Qt 4.1.2) on: Tue Nov 14 23:59:07 2006
 # Project:  preprocessor.pro
 # Template: app
@@ -63,5 +63,5 @@
 	$(MAKE) -f $(MAKEFILE).Debug uninstall
 
-Makefile: preprocessor.pro  C:/Qt/4.1.2/mkspecs/win32-msvc.net\qmake.conf C:/Qt/4.1.2/mkspecs/qconfig.pri \
+Makefile: preprocessor.pro  C:/Qt/4.1.2/mkspecs/win32-msvc2005\qmake.conf C:/Qt/4.1.2/mkspecs/qconfig.pri \
 		C:\Qt\4.1.2\mkspecs\features\qt_config.prf \
 		C:\Qt\4.1.2\mkspecs\features\exclusive_builds.prf \
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1715)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1757)
@@ -2272,5 +2272,5 @@
   float density = 1.0f;
   float p = 1.0f/density; //(box.GetVolume()*dirBox.GetVolume())/i;
-  for (i=startIndex; i < rays.size(); i++) {
+  for (int i=startIndex; i < rays.size(); i++) {
 	rays[i].mPdf = p;
   }
@@ -2461,5 +2461,5 @@
 
   float p = 1.0f/density; //(box.GetVolume()*dirBox.GetVolume())/i;
-  for (i=startIndex; i < rays.size(); i++) {
+  for (int i=startIndex; i < rays.size(); i++) {
 	rays[i].mPdf = p;
   }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1584)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1757)
@@ -88,5 +88,5 @@
 
 #if DEBUG_RAYCAST
-	Debug<<"PR ";
+	Debug<<"PRA"<<flush;
 #endif
 	
@@ -101,4 +101,8 @@
 	const bool validA = 
 	  ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
+
+#if DEBUG_RAYCAST
+	Debug<<"PR1"<<flush;
+#endif
 	
 	if (!validA && pruneInvalidRays)
@@ -116,5 +120,9 @@
 
 	const bool validSample = !pruneInvalidRays || (hitA.mObject != hitB.mObject);
-	
+
+#if DEBUG_RAYCAST
+	Debug<<"PR2"<<flush;
+#endif
+
 	if (validSample) 
 	{
@@ -134,5 +142,9 @@
 		  //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
 		}
-	  
+
+#if DEBUG_RAYCAST
+	Debug<<"PR3"<<flush;
+#endif
+
 	  if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
 		{
@@ -151,5 +163,9 @@
 		}
 	}
-	
+
+#if DEBUG_RAYCAST
+	Debug<<"PR4"<<flush;
+#endif
+
 	return hits;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 1715)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 1757)
@@ -1,7 +1,6 @@
-CONFIG -= qt debug
-CONFIG	+= console warn_off thread release mlrt qt
+CONFIG += qt release
+CONFIG += console warn_off thread mlrt qt
 
 TEMPLATE = app
-
 
 TARGET = preprocessor
@@ -20,5 +19,5 @@
 
 CONFIG(qt) {
-win32:INCLUDEPATH += "d:/Programs/NVIDIA Corporation/Cg/include"
+win32:INCLUDEPATH += "c:/Program Files/NVIDIA Corporation/Cg/include"
 }
 
@@ -32,12 +31,5 @@
 
 CONFIG(qt) {
-win32:LIBPATH += "d:/Programs/NVIDIA Corporation/Cg/lib"
-CONFIG(debug) {
-LIBPATH += QtRenderer/debug
-}
-
-CONFIG(release) {
-LIBPATH += QtRenderer/release
-}
+win32:LIBPATH += "c:/Program Files/NVIDIA Corporation/Cg/lib"
 }
 
@@ -47,6 +39,4 @@
 
 
-# RELEASE CONFIG
-#CONFIG		+= windows warn_on thread release
 
 # DEPENDPATH	= ../../include
@@ -88,5 +78,4 @@
 CONFIG(qt) {
 win32:LIBS += cg.lib cgGL.lib
-#LIBS += QtRenderer.lib
 DEFINES += USE_QT
 }
@@ -116,13 +105,16 @@
 OspTree.cpp HierarchyManager.cpp ObjParser.cpp \
 BvHierarchy.cpp  \
-BoostPreprocessorThread.cpp InternalRayCaster.cpp IntelRayCaster.cpp \
+InternalRayCaster.cpp IntelRayCaster.cpp \
 RayCaster.cpp PreprocessorFactory.cpp GvsPreprocessor.cpp \
 Trackball.cpp ObjExporter.cpp SubdivisionCandidate.cpp
 
 
+SOURCES += BoostPreprocessorThread.cpp 
 
 CONFIG(qt) {
 HEADERS += QtPreprocessorThread.h QtGlRenderer.h
 SOURCES += OcclusionQuery.cpp QtPreprocessorThread.cpp QtGlRenderer.cpp
+} else {
+SOURCES += BoostPreprocessorThread.cpp 
 }
 
