Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env	(revision 2069)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env	(revision 2070)
@@ -12,5 +12,5 @@
 
 Preprocessor {
-samplesPerPass 6000000
+samplesPerPass 3000000
 	useGlRenderer false
 	type vss
@@ -19,7 +19,7 @@
 	loadMeshes false
 	# internal raycaster
-	#rayCastMethod 0
+	rayCastMethod 0
 	# intel raycaster
-	rayCastMethod 1
+#	rayCastMethod 1
 	exportVisibility false
 	loadKdTree false
@@ -190,5 +190,5 @@
 VspBspTree {
 	Construction {
-		samples 900000
+		samples 200000
 		epsilon 0.0000001
 		randomize false
@@ -238,5 +238,5 @@
 		minGlobalCostRatio      0.0001
 
-		maxViewCells		6000
+		maxViewCells		3000
 	
 		# used for pvs criterium
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 2069)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 2070)
@@ -331,5 +331,6 @@
 		ExtractPolygons(object, polys);
 
-		mBoundingBox.Include(object->GetBox()); // add to BSP tree aabb
+		 // add to BSP tree aabb
+		mBoundingBox.Include(object->GetBox());
 	}
 
@@ -427,4 +428,5 @@
 	VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
 
+	/////////
 	//-- store rays
 	for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
@@ -928,10 +930,9 @@
 		if (1)
 		{
-			float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
-			float cBack = (float)tBackData.mPvs * tBackData.mProbability;
-			float cData = (float)tData.mPvs * tData.mProbability;
-
+			const float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
+			const float cBack = (float)tBackData.mPvs * tBackData.mProbability;
+			const float cData = (float)tData.mPvs * tData.mProbability;
 			
-			float costDecr = 
+			const float costDecr = 
 				(cFront + cBack - cData) / mBoundingBox.GetVolume();
 
@@ -946,6 +947,7 @@
 		}
 
-	
+		////////////
 		//-- push the new split candidates on the stack
+
 		VspBspSubdivisionCandidate frontCandidate;
 		frontCandidate.mParentData = tFrontData;
@@ -957,4 +959,6 @@
 		EvalSubdivisionCandidate(backCandidate);
 	
+		cout << "f cost: " << frontCandidate.mPriority << endl;
+		cout << "b cost: " << backCandidate.mPriority << endl;
 		tQueue.push(frontCandidate);
 		tQueue.push(backCandidate);
@@ -1017,6 +1021,6 @@
         leaf->mProbability = tData.mProbability;
 
-		// finally evaluate stats until this leaf
-		if (0)
+		// finally evaluate stats for this leaf
+		if (1)
 			EvaluateLeafStats(tData);		
 	}
@@ -1299,5 +1303,6 @@
 	mLocalSubdivisionCandidates->clear();
 
-	int requestedSize = 2 * (int)(rays.size());
+	const int requestedSize = 2 * (int)(rays.size());
+
 	// creates a sorted split candidates array
 	if (mLocalSubdivisionCandidates->capacity() > 500000 &&
@@ -1325,5 +1330,6 @@
 		if (0) ClipValue(pos, minBand, maxBand);
 		
-		mLocalSubdivisionCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
+		mLocalSubdivisionCandidates->
+			push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
 									pos, (*ri).mRay));
 
@@ -1333,9 +1339,10 @@
 		if (0) ClipValue(pos, minBand, maxBand);
 
-		mLocalSubdivisionCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin, 
+		mLocalSubdivisionCandidates->
+			push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin, 
 									pos, (*ri).mRay));
 	}
 
-	stable_sort(mLocalSubdivisionCandidates->begin(), mLocalSubdivisionCandidates->end());
+	sort(mLocalSubdivisionCandidates->begin(), mLocalSubdivisionCandidates->end());
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2069)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2070)
@@ -1246,5 +1246,5 @@
 
 	SimpleRayContainer::const_iterator rit, rit_end = rays.end();
-
+cout << "here1 " << vssRays.size()<<endl;
 	SimpleRayContainer rayBucket;
 	int i = 0;
@@ -1276,7 +1276,8 @@
 	
 		if ((int)rays.size() > 100000 && i % 100000 == 0)
-		  cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
-	}
-    
+			cout << "here2 " << vssRays.size()<<endl;
+		  //cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
+	}
+    cout << "here3 " << vssRays.size()<<endl;
 	// cast rest of rays
 	SimpleRayContainer::const_iterator sit, sit_end = rayBucket.end();
@@ -1305,11 +1306,15 @@
 	cout << "here5 " << vssRays.size()<<endl;
 
-	if (1) {
+	int m = 850000;
+
+	if (1 && (int)rays.size() > m + 50) {
 	  VssRayContainer tmpRays;
-	  int m = 700000;
-	  
+	  	  
 	  for (int i=m; i < m+20; i++) {
 		  if (vssRays[i])
-		tmpRays.push_back(vssRays[i]);		
+		  {cout<<"e";
+		tmpRays.push_back(vssRays[i]);
+		  }
+		  else cout <<"d";
 	  }
 	  ExportRays("sorted_rays2.x3d", tmpRays, 200);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 2070)
@@ -13,4 +13,5 @@
 class Polygon3;
 class Mesh;
+class VssRay;
 struct Triangle3;
 
@@ -122,4 +123,6 @@
   virtual int IsInside(const Vector3 &v) const;
   
+  virtual int IsInside(const VssRay &v) const;
+
   /** Test if the box makes sense.
   */
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 2035)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 2070)
@@ -55,5 +55,5 @@
 	return false;
 
-  if (tmin >= 1.0f || tmax <=0.0f)
+  if (tmin >= 1.0f || tmax <= 0.0f)
 	return false;
   
@@ -71,5 +71,6 @@
 }
 
-/** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
+/** Checks if ray is valid
+	(e.g., not in empty view space or outside the view space)
 */
 bool
@@ -245,11 +246,10 @@
 	  }
 	}
-
-	
-	// regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
+	
+	// regardless of the pruneInvalidRays setting reject 
+	// rays whic degenerate to a point
 	if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
 	  return 0;
 	}
-	
 	
 	const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
@@ -264,5 +264,5 @@
 	// reset both contributions
 	if (!validA || !validB) {
-	  if (pruneInvalidRays)
+	  if (0 || pruneInvalidRays)
 		return 0;
 	  
@@ -313,5 +313,4 @@
 		if (validA)
 		  vssRay->mFlags |= VssRay::Valid;
-
 		
 		vssRay->mDistribution = simpleRay.mDistribution;
@@ -327,20 +326,20 @@
 #endif
 
-	  if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
-		{
-		  VssRay *vssRay = mVssRayPool.Alloc();
-				
-		  *vssRay = VssRay(
-						   clipA,
-						   hitB.mPoint,
-						   hitA.mObject,
-						   hitB.mObject,
-						   mPreprocessor.mPass,
-						   simpleRay.mPdf
-						   );
-		  if (validB)
+	if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
+	{
+		VssRay *vssRay = mVssRayPool.Alloc();
+
+		*vssRay = VssRay(
+			clipA,
+			hitB.mPoint,
+			hitA.mObject,
+			hitB.mObject,
+			mPreprocessor.mPass,
+			simpleRay.mPdf
+			);
+
+		if (validB)
 			vssRay->mFlags |= VssRay::Valid;
 
-		  
 		vssRay->mDistribution = simpleRay.mDistribution;
 		vssRay->mGeneratorId = simpleRay.mGeneratorId;
@@ -348,5 +347,5 @@
 		++ hits;
 		//cout << "vssray 2: " << *vssRay << endl;
-		}
+	}
 #if DEBUG_RAYCAST
 	  Debug<<"PR4"<<flush;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 2003)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 2070)
@@ -207,4 +207,6 @@
 	{
 		CastRay(*sit, vssRays, sbox, castDoubleRays, pruneInvalidRays);
+		if (!vssRays.empty() && (!vssRays.back() || !sbox.IsInside(*vssRays.back())))
+		cout << "error " << *vssRays.back()<<endl;
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp	(revision 2070)
@@ -10,4 +10,5 @@
 #include "Halton.h"
 #include "Triangle3.h"
+#include "VssRay.h"
 
 
@@ -566,4 +567,10 @@
 }
 #endif
+
+int AxisAlignedBox3::IsInside(const VssRay &v) const
+{
+	return IsInside(v.mTermination) && IsInside(v.mOrigin);
+}
+
 
 bool
