Index: /GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp	(revision 1581)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp	(revision 1584)
@@ -1247,5 +1247,7 @@
 	  RenderRays(rays);
 	}
-	
+
+	if (mShowRenderCost) 
+	  RenderRenderCost();
   }
   mFrame++;
@@ -1407,35 +1409,7 @@
 }
 
-
-void
-QtGlRendererWidget::RenderInfo()
-{
-  QString s;
-  int vc = 0;
-  if (mViewCellsManager)
-	vc = mViewCellsManager->GetViewCells().size();
-  int filter = 0;
-  if (mViewCellsManager)
-	filter = mViewCellsManager->GetMaxFilterSize();
-
-  s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f) viewcells:%04d filter:%04d pvs:%04d error:%5.5f\%",
-			mFrame,
-			mViewPoint.x,
-			mViewPoint.y,
-			mViewPoint.z,
-			mViewDirection.x,
-			mViewDirection.y,
-			mViewDirection.z,
-			vc,
-
-			filter,
-			mPvsSize,
-			mRenderError*100.0f
-			);
-  
-  glColor3f(0.0f,0.0f,0.0f);
-  renderText(0,20,s);
-  
-  if (mShowRenderCost) {
+void
+QtGlRendererWidget::RenderRenderCost()
+{
 	static vector<float> costFunction;
 	static float maxCost = -1;
@@ -1531,5 +1505,38 @@
 
 	}
-  }
+  
+
+
+}
+
+void
+QtGlRendererWidget::RenderInfo()
+{
+  QString s;
+  int vc = 0;
+  if (mViewCellsManager)
+	vc = mViewCellsManager->GetViewCells().size();
+  int filter = 0;
+  if (mViewCellsManager)
+	filter = mViewCellsManager->GetMaxFilterSize();
+
+  s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f) viewcells:%04d filter:%04d pvs:%04d error:%5.5f\%",
+			mFrame,
+			mViewPoint.x,
+			mViewPoint.y,
+			mViewPoint.z,
+			mViewDirection.x,
+			mViewDirection.y,
+			mViewDirection.z,
+			vc,
+
+			filter,
+			mPvsSize,
+			mRenderError*100.0f
+			);
+  
+  glColor3f(0.0f,0.0f,0.0f);
+  renderText(0,20,s);
+  
 }
 
@@ -1729,10 +1736,10 @@
   connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
 
-  cb = new QCheckBox("Show render cost", hbox);
+  cb = new QCheckBox("Render cost curve", hbox);
   hlayout->addWidget(cb);
   cb->setChecked(false);
   connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
 
-  cb = new QCheckBox("Show pvs sizes", hbox);
+  cb = new QCheckBox("Show render cost", hbox);
   hlayout->addWidget(cb);
   cb->setChecked(false);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 1528)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 1584)
@@ -131,4 +131,5 @@
     depth = 0;
     Init();
+	Precompute();
   }
 
@@ -231,4 +232,6 @@
 					Vector3 &extP) const;
 
+  Vector3 GetInvDir() const { return invDir; }
+  
 private:
   Vector3 loc, dir;		// Describes ray origin and vector
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1582)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1584)
@@ -25,4 +25,5 @@
 
 #define DEBUG_RAYCAST 0
+#define SHOW_RAYCAST_TIMING 1
 
 
@@ -882,29 +883,30 @@
 		{
 			mRayCaster->CastRays16(
-				i,
-				rays,				
-				vssRays,
-				mViewCellsManager->GetViewSpaceBox(),
-				castDoubleRays,
-				pruneInvalidRays);
+								   i,
+								   rays,				
+								   vssRays,
+								   mViewCellsManager->GetViewSpaceBox(),
+								   castDoubleRays,
+								   pruneInvalidRays);
 			i += 16;
 		}
 		else 
-		{
+		  {
 			mRayCaster->CastRay(
-				rays[i],
-				vssRays,
-				mViewCellsManager->GetViewSpaceBox(),
-				castDoubleRays,
-				pruneInvalidRays);
+								rays[i],
+								vssRays,
+								mViewCellsManager->GetViewSpaceBox(),
+								castDoubleRays,
+								pruneInvalidRays);
 			i ++;
-		}
-	}
-
-	if (i % 10000 == 0)
-		cout<<".";
-
-#if DEBUB_RAYCAST
+		  }
+		if (i % 10000 == 0)
+		  cout<<".";
+	}
+	
+
 	long t2 = GetTime();
+
+#if SHOW_RAYCAST_TIMING
 	if (castDoubleRays)
 		cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
@@ -956,6 +958,7 @@
 	// do not store anything else then intersections at the ray
 	ray.Init(point, direction, Ray::LOCAL_RAY);
-}
-
-
-}
+	
+}
+
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.cpp	(revision 1583)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.cpp	(revision 1584)
@@ -29,5 +29,7 @@
   
   // apply the standard precomputation
-  Precompute();
+  //Precompute();
+  // $$ JB precomputation will be applied only when the origin/dir valueas are set
+  SetId();
 }
 
@@ -36,7 +38,7 @@
 {
   // initialize inverted dir
-  invDir.SetValue(0.0, 0.0, 0.0);
-  
+  //  invDir.SetValue(0.0, 0.0, 0.0);
   SetId();
+  ComputeInvertedDir();
 }
 
@@ -83,8 +85,8 @@
 Ray::ComputeInvertedDir() const
 {
-  if ( (invDir.x != 0.0) ||
-       (invDir.y != 0.0) ||
-       (invDir.z != 0.0) )
-    return; // has been already precomputed
+  //  if ( (invDir.x != 0.0) ||
+  //	   (invDir.y != 0.0) ||
+  //	   (invDir.z != 0.0) )
+  //    return; // has been already precomputed
 
   const float eps = 1e-6f;
@@ -240,4 +242,5 @@
   if (vssRay.mTerminationObject)
 	intersections.push_back(Intersection(len, -vssRay.GetDir(), vssRay.mTerminationObject, 0));
+
   
   Precompute();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.h	(revision 1581)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.h	(revision 1584)
@@ -208,4 +208,7 @@
   QtGlRendererWidget() {};
 
+  void
+  RenderRenderCost();
+
   virtual void SetupCamera();
   // setup a transformation for manipulator
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1583)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1584)
@@ -7,16 +7,16 @@
 
 #filename ../data/City4M/City4M.obj
-# filename ../data/CityModel/CityModel.obj
+#filename ../data/CityModel/CityModel.obj
 # filename ../data/GrandCanyon/grandcanyon1_RotXmin90.obj
 # filename ../data/glasgow/glasgow1.x3d
 #	filename vienna.x3d
-# filename ../data/vienna/vienna-simple.x3d
+filename ../data/vienna/vienna-simple.x3d
 # filename ../data/vienna/vienna-buildings.x3d
 # filename ../data/vienna/city1500_flat_1.x3d
-#filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d
+# filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d
 #;../data/vienna/vienna-plane.x3d;../data/vienna/vienna-roads.x3d
 # filename ../data/vienna/viewcells-25-sel.x3d
 #filename ../data/atlanta/atlanta2.x3d
-filename ../data/soda/soda.dat
+#filename ../data/soda/soda.dat
 #filename ../data/test1/test2.x3d
 #filename ../data/soda/soda5.dat
@@ -53,5 +53,5 @@
 	visibilityFilterWidth	0.01
 	visibilityFile visibility.xml
-	loadMeshes false
+	loadMeshes true
 	loadKdTree	false
 	exportKdTree false
@@ -59,5 +59,5 @@
 
 ObjParser {
-	meshGrouping 500
+	meshGrouping 1
 }
 
@@ -142,10 +142,10 @@
 	sahUseFaces false
 	Termination {
-		minCost 1
+		minCost 0.95
 		maxDepth 30
 		maxCostRatio 1.0
 		ct_div_ci 0.5
-		maxNodes 200000
-
+		maxNodes 100000
+#200000
 #500000	
 	}
@@ -173,47 +173,43 @@
 
 
+
+
 ViewCells {
 	# samples used for view cell construction
 	Construction {
-		samples 100000
-		samplesPerPass 100000
-	}
-
-	Filter {
-		maxSize 5
-		width  0.02
+		samples 0
+		samplesPerPass 1500000
 	}
 
 	#number of active view cells
 	active 5000
-	maxStaticMemory	200
+	maxViewCells 5000
+
+	maxStaticMemory	40
 
 	exportToFile true
-	loadFromFile true
-	
+	loadFromFile false
+
 	#type kdTree
 	#type vspKdTree
 	#type bspTree
 	type vspBspTree
-	#type vspOspTree
 	#type sceneDependent
 	
 	height 5.0
-	maxViewCells 5000
 
 	#percentage of total visible objects where pvs is considered invalid
 	maxPvsRatio 1.0
 		
-	pruneEmptyViewCells false
 	processOnlyValidViewCells false
 
 	#stats viewCellStats.log
 
-	samplingType directional
-	#samplingType box
+	#samplingType directional
+	samplingType box
 
 	PostProcess {
 		# how much samples are used for post processing
-		samples 100000
+		samples 0
 		renderCostWeight 1.0
 		maxCostRatio 0.1
@@ -222,12 +218,11 @@
 		maxMergesPerPass 5000
 		useRaysForMerge false
-		refine false
 		compress false
-		merge false
+		merge true
 	}
 
 	Visualization {
 		# how much samples we use for visualization
-		samples 100000
+		samples 0
 		#colorCode PVS
 		#colorCode MergedLeaves
@@ -235,17 +230,22 @@
 		colorCode Random
 		exportRays false
-		exportGeometry false
+		exportGeometry true
 		exportMergedViewCells false
 		useClipPlane true
 		clipPlaneAxis 1
-	}
-
-	showVisualization false	
+		clipPlanePos  0.3
+	}
+
+	showVisualization true
 	evaluateViewCells false
 	
 	Evaluation {
-		samplesPerPass 500
-		samples 1000
+		samplesPerPass 1000000
+		samples 1000000
 		statsPrefix ../scripts/viewCells
+		#samplingType directional
+		samplingType box
+		histogram true
+		histoStepSize 5000
 	}
 
@@ -259,5 +259,5 @@
 #	filename ../data/soda/soda5-viewcells.xml
 #	filename ../scripts/viewcells_atlanta.xml
-	filename ../data/soda/soda-viewcells-5000.xml
+#	filename ../data/soda/soda-viewcells-5000.xml
 #	filename ../data/test1/test-viewcells.xml
 
@@ -268,5 +268,5 @@
 
 
-#	filename ../data/vienna/vsposp-seq-viewCells.xml.gz
+	filename ../data/vienna/vsposp-seq-viewCells.xml.gz
 
 #	filename ../data/atlanta/viewcells_atlanta3.xml
@@ -291,9 +291,8 @@
 
 
-
 VspBspTree {
 	Construction {
-		samples 800000
-		epsilon 0.0005
+		samples 500000
+		epsilon 0.0000001
 		randomize false
 		renderCostWeight 1.0
@@ -310,5 +309,5 @@
 	
 	# maximal candidates for split planes
-	maxPolyCandidates 150
+	maxPolyCandidates 0
 
 	usePolygonSplitIfAvailable false
@@ -334,13 +333,13 @@
 		minPolygons 		-1
 		maxDepth		25
-		minPvs 			-15
-		minProbability		0.00000001
-		maxRayContribution	1.8
-		maxCostRatio		1.8
+		minPvs 			0
+		minProbability		0.000001
+		maxRayContribution	1
+		maxCostRatio		0.9
 		missTolerance		6
 		globalCostMissTolerance 4
-		minGlobalCostRatio      0.0000001
-#		minGlobalCostRatio      0.0001
-# $$MAXVIEWCELLS
+		#minGlobalCostRatio      0.0000001
+		minGlobalCostRatio      0.0001
+# $$ MAX	
 		maxViewCells		5000
 	
@@ -356,5 +355,5 @@
 	
 	useSplitCostQueue true
-	useCostHeuristics true
+	useCostHeuristics false
 
 	splitUseOnlyDrivingAxis false
@@ -362,5 +361,5 @@
 	useRandomAxis false
 	usePolygonSplitIfAvailable false
-	useBreathFirstSplits false
+	nodePriorityQueueType 0
 
 	Visualization {
@@ -370,2 +369,99 @@
 }
 
+BspTree {
+	Construction {
+		samples 100000
+		epsilon 0.005
+	}
+
+
+	# random polygon       = 1
+	# axis aligned         = 2
+	# least splits         = 4
+	# balanced polygons    = 8
+	# balanced view cells  = 16
+	# largest polygon area = 32
+	# vertical axis        = 64
+	# blocked rays         = 128
+	# least ray splits     = 256
+	# balanced rays        = 512
+	# pvs                  = 1024
+
+	# least splits + balanced polygons
+	#splitPlaneStrategy 12
+	
+	#axis aligned + vertical axis
+	#splitPlaneStrategy 66
+	
+	# axis aligned + balanced view cells
+	# splitPlaneStrategy 18
+	
+	# largest polygon area
+	#splitPlaneStrategy 32
+	
+	# axus aligned + balanced polygons
+	#splitPlaneStrategy 72
+	
+	# axis aligned + blocked rays
+	#splitPlaneStrategy 130
+	
+	#splitPlaneStrategy 384
+	#splitPlaneStrategy 130
+	
+	splitPlaneStrategy 32
+	
+	maxPolyCandidates 100
+	maxRayCandidates 0
+	
+	maxTests 10000
+	
+	subdivisionStats ../subDivisionStats.log
+
+	# factors for evaluating split plane costs
+	Factor {
+		verticalSplits 1.0
+	  	largestPolyArea 1.0
+	  	blockedRays 1.0
+	  	leastRaySplits 1.0
+	  	balancedRays 1.0
+	  	pvs 1.0
+	  	leastSplits 1.0
+	  	balancedPolys 1.0
+	  	balancedViewCells 1.0
+	}
+	
+	Termination {
+		# parameters used for autopartition
+		minRays -1
+		minPolygons 0
+		maxDepth 30
+		minPvs -1
+		minProbability 0.00001
+		maxRayContribution 9999
+		maxViewCells 50000
+
+		# used for pvs criterium
+		ct_div_ci 0.0
+	
+		maxCostRatio 0.9
+		
+		# axis aligned splits
+		AxisAligned {
+			minPolys 5000
+			minRays 500
+			minObjects 10
+			ct_div_ci 0.5
+		}
+	}
+	
+	AxisAligned {
+		splitBorder 0.01
+	}
+	
+	
+	Visualization {
+		# x3d visualization of the split planes
+		exportSplits false
+	}
+}
+
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1582)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1584)
@@ -623,5 +623,5 @@
 			// create the meshes and compute volumes
 			vm->FinalizeViewCells(true);
-			vm->mViewCellsTree->AssignRandomColors();
+			//			vm->mViewCellsTree->AssignRandomColors();
 		}
 
@@ -1521,4 +1521,6 @@
 	}
 
+	mViewCellsTree->AssignRandomColors();
+
 	mTotalAreaValid = false;
 }
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1583)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1584)
@@ -192,5 +192,5 @@
       int objectsBack, objectsFront;
       float costRatio;
-      bool mOnlyDrivingAxis = false;
+      bool mOnlyDrivingAxis = true;
 
 	  if (mOnlyDrivingAxis) {
@@ -608,5 +608,5 @@
   float mint = 0;
 
-  ray.ComputeInvertedDir();
+  //  ray.ComputeInvertedDir();
 
   Intersectable::NewMail();
@@ -617,4 +617,5 @@
   if (mint < 0)
     mint = 0;
+
   
   maxt += Limits::Threshold;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1583)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1584)
@@ -10,4 +10,5 @@
 #define DEBUG_RAYCAST 0
 
+#define EXACT_BOX_CLIPPING 1
 
 RayCaster::RayCaster(const Preprocessor &preprocessor):
@@ -43,29 +44,33 @@
 							Intersection &hit)
 {
-	if (!hit.mObject) 
-	{
-		// compute intersection with the scene bounding box
-		static Ray ray;
-		mPreprocessor.SetupRay(ray, origin, direction);
-
-		float tmin, tmax;
-		if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
-		{
-			hit.mPoint = ray.Extrap(tmax);
-		}
-		else
-		{
-			return false;
-		}		
-	} 
-	else if (mPreprocessor.mDetectEmptyViewSpace)
-	{
-		if (DotProd(hit.mNormal, direction) >= 0) 
-		{	
-			hit.mObject = NULL;
-			return false;
-		}
+	if (!hit.mObject)  {
+	  // compute intersection with the scene bounding box
+#if EXACT_BOX_CLIPPING
+	  static Ray ray;
+	  mPreprocessor.SetupRay(ray, origin, direction);
+	  
+	  float tmin, tmax;
+	  if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)) {
+		hit.mPoint = ray.Extrap(tmax);
+	  }
+	  else {
+		// 		cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
+		// 		cout<<" box "<<box<<endl;
+		// 		cout<<" origin "<<origin<<endl;
+		// 		cout<<" direction "<<direction<<endl;
+		// 		cout<< "inv dir"<<ray.GetInvDir()<<endl;
+		return false;
+	  }
+#else
+	  hit.mPoint = origin + direction*Magnitude(box.Diagonal());
+#endif
 	}
-
+	else if (mPreprocessor.mDetectEmptyViewSpace) {
+	  if (DotProd(hit.mNormal, direction) >= 0) {	
+		hit.mObject = NULL;
+		return false;
+	  }
+	}
+	
 	return true;
 }
@@ -88,32 +93,23 @@
 	if (pruneInvalidRays)
 	{
-		if (!hitA.mObject && !hitB.mObject)
-		{
-			return 0;
-		}
+	  if (!hitA.mObject && !hitB.mObject) {
+		return 0;
+	  }
+	  
+	}
 	
-		// inside test for bounding box
-		// enlarge box slightly so the view point fits for sure
-		AxisAlignedBox3 sbox = box;
-		sbox.Enlarge(Vector3(-Limits::Small));
-
-		if (!sbox.IsInside(simpleRay.mOrigin))
-		{
-			return 0;
-		}
-	}
-
 	const bool validA = 
-		ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
+	  ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
+	
 	if (!validA && pruneInvalidRays)
-	{
+	  {
 		return 0;
-	}
-
+	  }
+	
 	const bool validB = castDoubleRay && 
-		ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
-
+	  ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
+	
 	if (!validB && pruneInvalidRays)
-	{
+	  {
 		return 0;
 	}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1583)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1584)
@@ -29,7 +29,16 @@
 {
   //cout << "internal ray" << endl;
+  static Ray ray;
   int hits = 0;
-  static Ray ray;
   Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
+  
+  // inside test for bounding box
+  // enlarge box slightly so the view point fits for sure
+  //  AxisAlignedBox3 sbox = box;
+  //  sbox.Enlarge(Vector3(-Limits::Small));
+  // $$ JB moved here from Validate routine
+  if (!box.IsInside(simpleRay.mOrigin)) {
+	return 0;
+  }
   
   mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
