Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj	(revision 1148)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj	(revision 1149)
@@ -20,5 +20,5 @@
 				Name="VCCLCompilerTool"
 				Optimization="0"
-				AdditionalIncludeDirectories="..\include;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\src;&quot;$(CG_INC_PATH)&quot;;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces"
+				AdditionalIncludeDirectories="..\include;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\src;&quot;$(CG_INC_PATH)&quot;;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;&quot;$(BOOST_INCLUDE)&quot;"
 				PreprocessorDefinitions="WIN32;_DEBUG;_LIB;"
 				MinimalRebuild="TRUE"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1147)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1149)
@@ -1358,14 +1358,14 @@
                                           float &position)
 {
-	RayInfoContainer usedRays, savedRays;
-
-/*	if (mMaxTests < rays.size())
-	{
-		GetRaySets(rays, mMaxTests, usedRays, savedRays);
+	RayInfoContainer usedRays;
+
+	if (mMaxTests < rays.size())
+	{
+		GetRayInfoSets(rays, mMaxTests, usedRays);
 	}
 	else
-	{*/
+	{
 		usedRays = rays;
-	//}
+	}
 
 	const float minBox = box.Min(axis);
@@ -1405,5 +1405,5 @@
 
 	// set all object as belonging to the front pvs
-	for(ri = rays.begin(); ri != ri_end; ++ ri)
+	for(ri = usedRays.begin(); ri != ri_end; ++ ri)
 	{
 		Intersectable *oObject = (*ri).mRay->mOriginObject;
@@ -1500,8 +1500,10 @@
 			
 			// HACK: current positition is BETWEEN visibility events
-			if ((ci + 1) != ci_end)
+			if (0 && ((ci + 1) != ci_end))
+			{
 				currentPos = ((*ci).value + (*(ci + 1)).value) * 0.5f;
+			}
 			else
-				currentPos = (*ci).value;			
+            	currentPos = (*ci).value;			
 
 			//Debug  << "pos=" << (*ci).value << "\t pvs=(" <<  pvsl << "," << pvsr << ")" << endl;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1147)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1149)
@@ -2217,4 +2217,8 @@
 					"true");
 	
+	RegisterOption("VspTree.maxTests",
+					optInt,
+					"vsp_max_tests=",
+					"2000");
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h	(revision 1147)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h	(revision 1149)
@@ -93,8 +93,8 @@
 	int ComputeRayIntersection(const Plane3 &splitPlane, float &t) const;
 
-	/*friend void GetRaySets(const RayInfoContainer &sourceRays,
-						   const int maxSize,
-						   RayInfoContainer &usedRays,
-						   RayInfoContainer *savedRays) const;*/
+	friend void GetRayInfoSets(const RayInfoContainer &sourceRays,
+		const int maxSize,
+		RayInfoContainer &usedRays,
+		RayInfoContainer *savedRays = NULL);
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.cpp	(revision 863)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.cpp	(revision 1149)
@@ -165,4 +165,5 @@
 }
 
+
 float RayInfo::SqrSegmentLength() const
 {
@@ -170,3 +171,23 @@
 }
 
+
+void GetRayInfoSets(const RayInfoContainer &sourceRays,
+                    const int maxSize,
+					RayInfoContainer &usedRays,
+					RayInfoContainer *savedRays)
+{
+	const int limit = min(maxSize, (int)sourceRays.size());
+	const float prop = (float)limit / ((float)sourceRays.size() + Limits::Small);
+
+	RayInfoContainer::const_iterator it, it_end = sourceRays.end();
+
+	for (it = sourceRays.begin(); it != it_end; ++ it)
+	{
+		if (Random(1.0f) < prop)
+			usedRays.push_back(*it);
+		else if (savedRays)
+			savedRays->push_back(*it);
+	}
 }
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1147)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1149)
@@ -413,5 +413,5 @@
 	Environment::GetSingleton()->GetBoolValue("VspTree.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
 	
-	//Environment::GetSingleton()->GetFloatValue("VspTree.maxTotalMemory", mMaxTotalMemory);
+	Environment::GetSingleton()->GetIntValue("VspTree.maxTests", mMaxTests);
 	Environment::GetSingleton()->GetFloatValue("VspTree.maxStaticMemory", mMaxMemory);
 
@@ -1390,5 +1390,15 @@
 									   float &position)
 {
-	RayInfoContainer *rays = tData.mRays;
+	RayInfoContainer usedRays;
+
+	if (mMaxTests < (int)tData.mRays->size())
+	{
+		GetRayInfoSets(*tData.mRays, mMaxTests, usedRays);
+	}
+	else
+	{
+		usedRays = *tData.mRays;
+	}
+
 	int pvsSize = tData.mPvs;
 
@@ -1401,10 +1411,10 @@
 	const float maxBand = minBox + mMaxBand * sizeBox;
 
-	SortSplitCandidates(*rays, axis, minBand, maxBand);
+	SortSplitCandidates(usedRays, axis, minBand, maxBand);
 
 	// prepare the sweep
 	// (note: returns pvs size, so there would be no need 
 	// to give pvs size as argument)
-	pvsSize = PrepareHeuristics(*rays);
+	pvsSize = PrepareHeuristics(usedRays);
 
 	// go through the lists, count the number of objects left and right
@@ -1448,5 +1458,5 @@
 			
 			// HACK: current positition is BETWEEN visibility events
-			if ((ci + 1) != ci_end)
+			if (0 && ((ci + 1) != ci_end))
 				currentPos = ((*ci).value + (*(ci + 1)).value) * 0.5f;
 			else
@@ -3295,5 +3305,5 @@
 			
 			// HACK: current positition is BETWEEN visibility events
-			if ((ci + 1) != ci_end)
+			if (0 && ((ci + 1) != ci_end))
 				currentPos = ((*ci).mPos + (*(ci + 1)).mPos) * 0.5f;
 			else
@@ -3301,5 +3311,5 @@
 
 			// note matt: can happen that volr is less than zero: bug or numerical error?
-			//if (volr < 0) 	Debug << "warning!! " << totalVol << " " << volRightDecr << " " << volRightDecr - totalVol << endl;
+			//if (volr < 0) Debug << "warning!";
 
 			/*Debug << "pos: " << (*ci).mPos 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1145)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1149)
@@ -1133,4 +1133,6 @@
 	/// weight between render cost decrease and node render cost
 	float mRenderCostDecreaseWeight;
+
+	int mMaxTests;
 };
 
