Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1486)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1489)
@@ -12,4 +12,5 @@
 class Exporter;
 class VssRay;
+
 
 
@@ -31,21 +32,26 @@
 	struct GvsRayInfo
 	{
+		GvsRayInfo(VssRay *ray, const bool d)
+			: mRay(ray), mFoundDiscontinuity(d) 
+		{}
+
 		VssRay *mRay;
 		bool mFoundDiscontinuity;
-	}
+	};
 
-	
+
 	typedef stack<GvsRayInfo> RayQueue;
 
-	/** Runs the adaptive sampling. The method starts with a number of random rays given
-		by the queue and continues as long it finds new visible geometry (i.e., the queue is
-		not empty).
+	/** Runs the adaptive sampling until the ray queue is empty. 
+		The method starts with a number of random rays given
+		by the queue and continues as long it finds new visible geometry 
+		(i.e., the queue is	not empty).
 
 		@returns the number of samples cast.
 	*/
-	int RunSampling();
+	int ProcessQueue();
 
-	/** One pass of the sampling preprocessor. Continues as long as at least passSample
-		rays have been cast.
+	/** One pass of the sampling preprocessor. 
+		Continues as long as at least passSample rays have been cast.
 		@returns the number of samples cast.
 	*/
@@ -54,8 +60,5 @@
 	/** Generates the rays starting the adaptive visibility sampling process.
 	*/
-	int CastAvsSamples(
-		const int samplesPerPass, 
-		const int sampleType, 
-		RayQueue &passSamples);
+	int CastInitialSamples(const int numSamples, const int sampleType);
 
 	/** Uses the information gained from the ray for doing adaptive border sampling.
@@ -70,27 +73,25 @@
 	int HandleRay(const GvsRayInfo &ray);
 
-	/** 
+	/** The adaptive border sampling step. It aims to find neighbouring 
+		triangles of the one hit by the previous ray.
 	*/	
-	int AdaptiveBorderSampling(const VssRay &ray);
+	int AdaptiveBorderSampling(const VssRay &prevRay);
 	
-	int ReverseSampling(const VssRay &ray);
+	/** The reverse sampling step. It is started once the cast
+		ray finds a discontinuity, i.e., a closer triangle.
+		Then the process tries to find a ray from the old
+		triangle passing through a gap.
+	*/
+	int ReverseSampling(const VssRay &prevRay);
 
-	/** Cast samples according to a specific sampling strategy.
+	/** Returns true if we sampled a closer triangle than with the previous ray.
 	*/
-	int CastPassSamples(
-		const int samplesPerPass, 
-		const int sampleType, 
-		VssRayContainer &passSamples) const;
+	const bool DiscontinuityFound(const VssRay &ray, const VssRay &prevRay) const;
 
-	void CreateNewSamples(
-		VertexContainer &samples, 
-		const VssRay &ray, 
-		const Triangle3 &hitTriangle, 
-		const int index,
-		const float eps);
-
-	const bool DiscontinuityFound(const VssRay &ray) const;
-
-
+	/** Adds new samples to the ray queue and classifies them
+		with respect to the previous ray.
+	*/
+	void EnqueueSamples(VssRayContainer &samples, VssRay *prevRay = NULL);
+	
 	//////////////////////
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1486)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1489)
@@ -2432,5 +2432,5 @@
 					optBool,
 					"bvh_construction_use_global_sorting=",
-					"false");
+					"true");
 
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1416)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1489)
@@ -1485,5 +1485,4 @@
 {
 	 stats << "#Pass\n" << pass << endl
-		//<< "#Merged\n" << mergeStats.merged << endl 
 		<< "#ViewCells\n" << viewCells << endl 
         << "#RenderCostDecrease\n" << renderCostDecrease << endl // TODO
@@ -1499,4 +1498,5 @@
 		<< endl;
 }
+
 
 void ViewCellsTree::ExportStats(const string &mergeStats)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1486)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1489)
@@ -5,5 +5,5 @@
 #include "ViewCellsManager.h"
 #include "Triangle3.h"
-
+#include "IntersectableWrapper.h"
 
 
@@ -29,6 +29,8 @@
 
 
-const bool GvsPreprocessor::DiscontinuityFound(const VssRay &ray) const
-{
+const bool GvsPreprocessor::DiscontinuityFound(const VssRay &ray, 
+											   const VssRay &oldRay) const
+{
+    //const Plane3 plane = tri->GetPlane();
 	return false;
 }
@@ -52,10 +54,12 @@
 }
 
-
-void GvsPreprocessor::CreateNewSamples(VertexContainer &samples, 
-									   const VssRay &ray, 
-									   const Triangle3 &hitTriangle, 
-									   const int index,
-									   const float eps)
+/** Hepler function for adaptive border sampling. It finds 
+	new sample points around a triangle in a eps environment
+*/
+static void CreateNewSamples(VertexContainer &samples, 
+							 const Triangle3 &hitTriangle,
+							 const VssRay &ray, 
+							 const int index,
+							 const float eps)
 {
 	const int indexU = (index + 1) % 3;
@@ -86,36 +90,54 @@
 
 
-int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &ray)
+/** Generate rays from sample points.
+*/
+static void CreateRays(VssRayContainer &rays,
+					   const VertexContainer &samples)
+{
+	VertexContainer::const_iterator vit, vit_end = samples.end();
+	
+	for (vit = samples.begin(); vit != vit_end; ++ vit)
+	{
+		const Vector3 currentSample = *vit;
+		VssRay *ray;// = new VssRay(ray->mOrigin, currentSample);
+	}
+}
+
+
+int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &prevRay)
 {
 	cout << "a";
-	//Intersectable *tObj = ray->GetTerminationObject;
+	Intersectable *tObj = prevRay.mTerminationObject;
 	Triangle3 hitTriangle;
-	vector<Vector3> samples;
+
+	// other types not implemented yet
+	if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
+	{
+		hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
+	}
+
+	VertexContainer samples;
 	samples.reserve(9);
 
-	CreateNewSamples(samples, ray, hitTriangle, 0, mEps);
-	CreateNewSamples(samples, ray, hitTriangle, 1, mEps);
-	CreateNewSamples(samples, ray, hitTriangle, 2, mEps);
-
-	VertexContainer::const_iterator vit, vit_end = samples.end();
-	
-	for (vit = samples.begin(); vit != vit_end; ++ vit)
-	{
-		VssRay *ray;// = CreateRay(*vit);
-
-		if (DiscontinuityFound(*ray))
-		{
-			// schedule for reverse sampling
-			mRayQueue.push(GvsRayInfo(ray, true));
-		}
-		else
-		{
-			// schedule for adaptive border sampling
-			mRayQueue.push(GvsRayInfo(ray, false));
-		}
-	}
-
-	// TODO
-	return 1;
+	CreateNewSamples(samples, hitTriangle, prevRay, 0, mEps);
+	CreateNewSamples(samples, hitTriangle, prevRay, 1, mEps);
+	CreateNewSamples(samples, hitTriangle, prevRay, 2, mEps);
+
+	VssRayContainer vssRays;
+	CreateRays(vssRays, samples);
+
+	VssRayContainer::const_iterator rit, rit_end = vssRays.end();
+	
+	for (rit = vssRays.begin(); rit != rit_end; ++ rit)
+	{
+		VssRay *ray = *rit;
+
+		// discontinuity found? 
+		// schedule for reverse sampling or adaptive border sampling
+		const bool gap = DiscontinuityFound(*ray, prevRay);
+		mRayQueue.push(GvsRayInfo(ray, gap));
+	}
+
+	return 9;
 }
 
@@ -128,28 +150,38 @@
 }
 
-int GvsPreprocessor::CastAvsSamples(const int samplesPerPass, 
-									const int sampleType, 
-									RayQueue &passSamples)
-{
+
+int GvsPreprocessor::CastInitialSamples(const int numSamples, 
+										const int sampleType)
+{	
+	const long startTime = GetTime();
+
+	// generate simple rays
 	SimpleRayContainer simpleRays;
-	const long startTime = GetTime();
-
-	GenerateRays(samplesPerPass, sampleType, simpleRays);
-	Debug << "generated " <<  samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-
-	// cast the rays and optain hitpoints with geometry
+	GenerateRays(numSamples, sampleType, simpleRays);
+	
+	// generate vss rays
 	VssRayContainer samples;
 	CastRays(simpleRays, samples);
-
-	Debug << "cast " <<  samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-
+	
+	// add to ray queue
+	EnqueueSamples(samples);
+
+	Debug << "generated " <<  numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+	return (int)samples.size();
+}
+
+
+void GvsPreprocessor::EnqueueSamples(VssRayContainer &samples, VssRay *oldRay)
+{
 	// add samples to ray queue
 	VssRayContainer::const_iterator vit, vit_end = samples.end();
 	for (vit = samples.begin(); vit != vit_end; ++ vit)
 	{
-		mRayQueue.push(*vit);
-	}
-
-	return (int)mRayQueue.size();
+		/// if there is no old ray, no discontinuity
+		const bool gap = oldRay ? DiscontinuityFound(*(*vit), *oldRay) : false;
+		mRayQueue.push(GvsRayInfo(*vit, gap));
+	}
+
 }
 
@@ -158,15 +190,14 @@
 {
 	int castSamples = 0;
-	
+	const int mSampleType = 0;
 	while (castSamples < mSamplesPerPass) 
 	{
 		// Ray queue empty => 
 		// cast a number of uniform samples to fill ray Queue
-		CastAvsSamples(mInitialSamples, mSamplingType, mRayQueue);
-		
-		const int gvsSamples = RunSampling();
+		CastInitialSamples(mInitialSamples, mSampleType);
+
+		const int gvsSamples = ProcessQueue();
 		castSamples += gvsSamples;
-		//cout << "\ngvs samples: " << gvsSamples << endl;
-		//cout << "cast " << castSamples << " of " << mSamplesPerPass << endl;
+		//cout << "\ncast " << castSamples << " of " << mSamplesPerPass << endl;
 	}
 
@@ -175,5 +206,5 @@
 
 
-int GvsPreprocessor::RunSampling()
+int GvsPreprocessor::ProcessQueue()
 {
 	int castSamples = 0;
@@ -182,8 +213,8 @@
 	{
 		// handle next ray
-		VssRay *ray = mRayQueue.top();
+		GvsRayInfo rayInfo = mRayQueue.top();
 		mRayQueue.pop();
 		
-		castSamples += HandleRay(*ray);
+		castSamples += HandleRay(rayInfo);
 	}
 
@@ -192,6 +223,5 @@
 
 
-bool
-GvsPreprocessor::ComputeVisibility()
+bool GvsPreprocessor::ComputeVisibility()
 {
 	Randomize(0);
@@ -213,9 +243,12 @@
 		const int passSamples = Pass();
 		castSamples += passSamples;
+		
+		/////////////
+		// -- stats
 		cout << "+";
 		cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
 		//mVssRays.PrintStatistics(mStats);
-		//mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
-		//	   << "#TotalSamples\n" << samples << endl;
+		mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
+			   << "#TotalSamples\n" << castSamples << endl;
 
 		mViewCellsManager->PrintPvsStatistics(mStats);
