Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2599)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2606)
@@ -83,18 +83,19 @@
 #endif
 
-  for (int i=0; i < vssRays.size(); i++) {
-	if (vssRays[i]->mPvsContribution) {
-	  // reset the counter of unsuccsseful mutation for a generating ray (if it exists)
-	  if (vssRays[i]->mDistribution == MUTATION_BASED_DISTRIBUTION &&
-		  vssRays[i]->mGeneratorId != -1
+  for (int i=0; i < vssRays.size(); i++) 
+	if (vssRays[i]->mTerminationObject) {
+	  if (vssRays[i]->mPvsContribution) {
+		// reset the counter of unsuccsseful mutation for a generating ray (if it exists)
+		if (vssRays[i]->mDistribution == MUTATION_BASED_DISTRIBUTION &&
+			vssRays[i]->mGeneratorId != -1
 		  ) {
-		mRays[vssRays[i]->mGeneratorId].mUnsuccessfulMutations = 0;
+		  mRays[vssRays[i]->mGeneratorId].mUnsuccessfulMutations = 0;
 #if EVALUATE_MUTATION_STATS 
-		mutationRays++;
+		  mutationRays++;
+		  
+		  Intersectable *newObject = vssRays[i]->mTerminationObject;
 		
-		Intersectable *newObject = vssRays[i]->mTerminationObject;
-		
-		Intersectable *oldObject = mRays[vssRays[i]->mGeneratorId].mRay->mTerminationObject;
-		// the ray generated a contribution although it hits the same object
+		  Intersectable *oldObject = mRays[vssRays[i]->mGeneratorId].mRay->mTerminationObject;
+		  // the ray generated a contribution although it hits the same object
 		// mark this using a counter
 		if (oldObject == newObject)
@@ -144,5 +145,4 @@
 			  mRays[newRay->mGeneratorId].SetReverseMutation(origin, termination);
 			}
-			
 			reverseCandidates++;
 			//mReverseCandidates
@@ -311,5 +311,7 @@
 {
 #ifdef GTP_INTERNAL
-
+  static int counter = 0;
+
+  
   // first reconstruct the termination point
   Vector3 oldDir = Normalize(oldRay.GetDir());
@@ -331,5 +333,6 @@
 
   Intersectable *occluder = newRay.mTerminationObject;
-  
+
+
   AxisAlignedBox3 box = occluder->GetBox();
   // consider slightly larger neighborhood of the occluder
@@ -337,4 +340,5 @@
   box.Scale(2.0f);
   //box.Scale(200.0f);
+
   
   const int packetSize = 4;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/run_test_pompeii
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/run_test_pompeii	(revision 2105)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/run_test_pompeii	(revision 2606)
@@ -4,5 +4,5 @@
 #COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer- -preprocessor_evaluate_filter- -preprocessor_detect_empty_viewspace- -total_samples=500000000"
 
-COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer- -preprocessor_evaluate_filter- -preprocessor_detect_empty_viewspace+ -samples_per_evaluation=5000000 -samples_per_pass=1000000 -total_samples=250000000 -view_cells_use_kd_pvs+"
+COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer- -preprocessor_evaluate_filter- -preprocessor_detect_empty_viewspace+ -samples_per_evaluation=5000000 -samples_per_pass=1000000 -total_samples=250000000 -view_cells_use_kd_pvs+ -kd_pvs_area=1e-5"
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 2601)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 2606)
@@ -37,4 +37,6 @@
 	virtual void UpdateBox();
 
+	~SceneGraphInterior();
+
 //protected:
 
@@ -51,4 +53,6 @@
 	virtual bool IsLeaf() const { return true; }
 	virtual void UpdateBox();
+
+	~SceneGraphLeaf();
 
 //protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp	(revision 2575)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp	(revision 2606)
@@ -137,5 +137,5 @@
 
 	Intersectable::NewMail();
-	KdLeaf::NewMail();
+	KdNode::NewMail();
 
 	ObjectPvsIterator pit = GetIterator();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2582)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2606)
@@ -1159,5 +1159,5 @@
 				 optFloat,
 				 "kd_pvs_area=",
-				 "1e-4");
+				 "1e-5");
 
   RegisterOption("KdTree.splitBorder",
@@ -1700,4 +1700,5 @@
 				   "false");
 
+	
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2603)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2606)
@@ -1208,8 +1208,8 @@
 
 	// !!!!!!!!!!!!!!!! VH no sorting
-	if (
-	    rays.size() > 10000)
+	if ( 
+	    rays.size() > 10000
+		)
 	  {
-
 	  mRayCaster->SortRays(rays);
 	  cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" ms."<<endl;
@@ -1565,5 +1565,5 @@
 void Preprocessor::DeterminePvsObjects(VssRayContainer &rays)
 {
-	mViewCellsManager->DeterminePvsObjects(rays, false);
+  mViewCellsManager->DeterminePvsObjects(rays, false);
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 2602)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 2606)
@@ -3,4 +3,5 @@
 # Jiri Bittner 2003
 #############################################################################
+
 
 Scene {
@@ -179,9 +180,10 @@
 	}
 
-	splitMethod spatialMedian
-#	splitMethod SAH
+#	splitMethod spatialMedian
+	splitMethod SAH
 	splitBorder 0.01
-	pvsArea 1e-3
-}
+	pvsArea 1e-5
+}
+
 
 
@@ -202,5 +204,5 @@
 	importRandomViewCells false
 
-	useKdPvs false
+	useKdPvs true
 	useKdPvsAfterFiltering true
 	# samples used for view cell construction
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2601)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2606)
@@ -2474,5 +2474,5 @@
 int ViewCellsManager::GetNumViewCells() const
 {
-	return (int)mViewCells.size();
+  return (int)mViewCells.size();
 }
 
@@ -2493,7 +2493,15 @@
 		  
 		  // set only the termination object
+		  //		  if (vssRay->mTerminationObject == NULL) 
+		  //			cerr<<"Error NULL termination object!"<<endl;
 		  vssRay->mTerminationObject = GetIntersectable(*vssRay, true);
-		}
-	}
+#if 0
+		  if (vssRay->mTerminationObject == NULL) {
+			cerr<<"Error in DeterminePvsObjects - termination object maps to NULL!"<<endl;
+		  }
+#endif
+		}
+	}
+  
 }
 
@@ -3247,14 +3255,14 @@
 		for (int i = 0; it != it_end; ++ it, ++ i)
 		{
-			if ((*it) != viewCell) 
+		  if ((*it) != viewCell) 
 			{
-				//cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
-				basePvs.MergeInPlace((*it)->GetPvs());
+			  //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
+			  basePvs.MergeInPlace((*it)->GetPvs());
 			}
-
-			// update samples and globalC
-			samples = (float)pvs.GetSamples();
-			//	cout<<"neighboring viewcells = "<<i-1<<endl;
-			//	cout<<"Samples' = "<<samples<<endl;
+		  
+		  // update samples and globalC
+		  samples = (float)pvs.GetSamples();
+		  //	cout<<"neighboring viewcells = "<<i-1<<endl;
+		  //	cout<<"Samples' = "<<samples<<endl;
 		}
 	}
@@ -3342,18 +3350,20 @@
 
 			if (filteredBoxes)
-				filteredBoxes->push_back(box);
-
+			  filteredBoxes->push_back(box);
+			
 			objects.clear();
 
 			// $$ warning collect objects takes only unmailed ones!
-			if (mUseKdPvs) 
-				GetPreprocessor()->mKdTree->CollectKdObjects(box, objects);
-			else
-				CollectObjects(box, objects);
-
+			if (mUseKdPvs) {
+			  //			  GetPreprocessor()->mKdTree->CollectKdObjects(box, objects);
+			  GetPreprocessor()->mKdTree->CollectSmallKdObjects(box, objects, 0.1f);
+
+			} else
+			  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!	  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 2582)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 2606)
@@ -15,4 +15,5 @@
 FILE *fileOut = 0;
 bool saveRays = false;
+bool saveMutationRays = false;
 const int saveRaysStart = 3000000;
 int cntSavedRaysFLUSH = 0;
@@ -41,5 +42,5 @@
   if (!InitRayCast(externKdTree))
     cout << "warning: intel ray tracer could not be initialized!" << endl;
-  if (saveRays)
+  if (saveRays || saveMutationRays)
     InitSaving();
 }
@@ -59,4 +60,9 @@
   return mlrtaLoadAS(externKdTree.c_str());
 }
+
+
+// Using packet of 4 rays supposing that these are coherent
+// We give a box to which each ray is clipped to before the
+// ray shooting is computed !
 
 // Using packet of 4 rays supposing that these are coherent
@@ -75,4 +81,28 @@
   
   mlrtaTraverseGroupASEye4(&minBox.x, &maxBox.x, result4, dist4);
+
+  if (saveMutationRays) {
+    fprintf(fileOut, "I %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f\n",
+			minBox.x, minBox.y, minBox.z, maxBox.x, maxBox.y, maxBox.z);
+    for (int i = 0; i < 4; i++) {
+      fprintf(fileOut, "%d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f\n",
+			  cntSavedRays,
+			  origin4[i].x,
+			  origin4[i].y,
+			  origin4[i].z,
+			  dirs4[i].x,
+			  dirs4[i].y,
+			  dirs4[i].z,
+			  (result4[i] != -1) ? 1 : 0,
+			  (result4[i] != -1) ? dist4[i] : 0);
+      cntSavedRays ++;
+      cntSavedRaysFLUSH++;
+    }
+    if (cntSavedRaysFLUSH > intSavedLIMIT) {
+      fflush(fileOut);
+      cntSavedRaysFLUSH = 0;
+    }
+  } // if we are saving rays
+
   return;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 2603)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 2606)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: st 16. I 22:10:45 2008
+# Generated by qmake (2.00a) (Qt 4.1.2) on: ?t 17. I 15:26:52 2008
 # 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/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2575)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2606)
@@ -129,9 +129,14 @@
   delete splitCandidates;
 
-  float area = GetBox().SurfaceArea() * mKdPvsArea;
-
+  float area = GetPvsArea();
+  
   SetPvsTerminationNodes(area);
   
   return true;
+}
+
+float
+KdTree::GetPvsArea() const {
+  return GetBox().SurfaceArea() * mKdPvsArea;
 }
 
@@ -891,4 +896,40 @@
 
 void
+KdTree::CollectSmallKdObjects(const AxisAlignedBox3 &box,
+							  ObjectContainer &objects,
+							  const float maxAreaFrac
+							  )
+{
+  stack<KdNode *> nodeStack;
+  
+  nodeStack.push(mRoot);
+  float maxArea = GetPvsArea()*maxAreaFrac;
+  
+  while (!nodeStack.empty()) {
+    KdNode *node = nodeStack.top();
+    nodeStack.pop();
+	if (!node->Mailed()) {
+	  node->Mail();
+	  if (node->IsLeaf() || GetSurfaceArea(node) <= maxArea)  {
+		Intersectable *object = GetOrCreateKdIntersectable(node);
+		if (!node->Mailed()) {
+		  objects.push_back(object);
+		}
+	  }
+	  else {
+		KdInterior *interior = (KdInterior *)node;
+		
+		if ( box.Max()[interior->mAxis] > interior->mPosition )
+		  nodeStack.push(interior->mFront);
+		
+		if (box.Min()[interior->mAxis] < interior->mPosition)
+		  nodeStack.push(interior->mBack);
+	  }
+	}
+  } // while
+}
+
+
+void
 KdTree::CollectObjects(const AxisAlignedBox3 &box,
 		       ObjectContainer &objects)
@@ -1451,5 +1492,5 @@
     KdNode *node = nodeStack.top();
     nodeStack.pop();
-
+	
     node->mPvsTermination = 0;
     if (node->IsLeaf() || (GetSurfaceArea(node) <= maxArea) ) {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2575)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2606)
@@ -344,4 +344,7 @@
   void
   SetPvsTerminationNodes(const float maxArea);
+
+  float
+  GetPvsArea() const;
   
   KdNode *
@@ -353,4 +356,13 @@
 		   ObjectContainer &objects
 		   );
+
+  // colect objects which are in the bounding box by explictly specifying the
+  // allowed size of the boxes. If this size is smaller than size used for pvs entries
+  // filtering will be more precise
+  void
+  CollectSmallKdObjects(const AxisAlignedBox3 &box,
+						ObjectContainer &objects,
+						const float maxArea
+						);
 
   void
