Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1313)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1473)
@@ -17,11 +17,4 @@
 	enum {OBJECT_SPACE, VIEW_SPACE};
 
-	/// the current split plane
-	//AxisAlignedPlane mSplitPlane;
-	/// split axis of this plane (0, 1, 2, or 3 if non-axis-aligned)
-	int mSplitAxis;
-	/// the number of misses of max cost ratio until this split
-	int mMaxCostMisses;
-
 	SubdivisionCandidate(): mRenderCostDecrease(0) {};
 
@@ -33,10 +26,10 @@
 	/** Set render cost decrease achieved through this split.
 	*/
-	void SetRenderCostDecrease(const float renderCostDecr)
+	inline void SetRenderCostDecrease(const float renderCostDecr)
 	{
 		mRenderCostDecrease = renderCostDecr;
 	}
 	
-	float GetRenderCostDecrease() const
+	inline float GetRenderCostDecrease() const
 	{
 		return mRenderCostDecrease;
@@ -49,4 +42,9 @@
 		return mPosition;
 	}
+
+	/// split axis of this plane (0, 1, 2, or 3 if non-axis-aligned)
+	int mSplitAxis;
+	/// the number of misses of max cost ratio until this split
+	int mMaxCostMisses;
 
 protected:
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1460)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1473)
@@ -3,4 +3,5 @@
 
 #include <fstream>
+#include <stack>
 using namespace std;
 
@@ -10,21 +11,79 @@
 
 class Exporter;
+class VssRay;
 
-/** Sampling based visibility preprocessing. The implementation is based on heuristical
-    sampling of view space */
+
+typedef stack<VssRay *> RayQueue;
+
+/** Sampling based visibility preprocessing. The implementation is 
+	based on heuristical sampling of view space.
+*/
 class GvsPreprocessor : public Preprocessor {
 
 public:
-  int mSamples;
-  
-  GvsPreprocessor();
-  ~GvsPreprocessor() {}
-  
-  virtual bool ComputeVisibility();
-  
-  
+
+	GvsPreprocessor();
+	~GvsPreprocessor() {}
+
+	virtual bool ComputeVisibility();
+	
+
 protected:
-  ofstream mStats;
-  
+
+	/** 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).
+
+		@returns the number of samples cast.
+	*/
+	int RunSampling();
+
+	/** One pass of the sampling preprocessor. Continues as long as at least passSample
+		rays have been cast.
+		@returns the number of samples cast.
+	*/
+	int Pass(const int passSamples);
+
+	/** Generates the rays starting the adaptive visibility sampling process.
+	*/
+	int CastAvsSamples(
+		const int samplesPerPass, 
+		const int sampleType, 
+		RayQueue &passSamples);
+
+	/** Uses the information gained from the ray for doing adaptive border sampling.
+		This function tries to find the border of the triangle found visible by the
+		current ray. New rays are generated which sample this border.
+		
+		We use the following strategies:
+
+		a) if new triangle was found: adaptive border sampling
+		b) if triangle was found reverse sampling
+	*/
+	int HandleRay(VssRay &ray);
+
+	/** 
+	*/	
+	int AdaptiveBorderSampling(VssRay &ray);
+	
+	int ReverseSampling(VssRay &ray);
+
+	/** Cast samples according to a specific sampling strategy.
+	*/
+	int CastPassSamples(
+		const int samplesPerPass, 
+		const int sampleType, 
+		VssRayContainer &passSamples) const;
+
+
+
+	//////////////////////
+
+	ofstream mStats;
+	int mSamples;
+	int mSamplesPerPass;
+	RayQueue mRayQueue; 
+	int mSamplingType;
+	int mTotalSamples;
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 1281)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 1473)
@@ -5,5 +5,5 @@
 #include "Matrix4x4.h"
 #include "Vector3.h"
-//#include "ViewCellBsp.h"
+
 
 namespace GtpVisibilityPreprocessor {
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1315)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1473)
@@ -328,4 +328,10 @@
 			<File
 				RelativePath="..\src\glInterface.h">
+			</File>
+			<File
+				RelativePath=".\GvsPreprocessor.cpp">
+			</File>
+			<File
+				RelativePath=".\GvsPreprocessor.h">
 			</File>
 			<File
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1449)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1473)
@@ -132,5 +132,4 @@
 	of split is chosen for view space / object space, the candidates for the next split of 
 	object space / view space must be reevaluated.
-	
 */
 class HierarchyManager
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1454)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1473)
@@ -267,9 +267,9 @@
 
 	mPreprocessor->GenerateRays(samplesPerPass, sampleType, simpleRays);
-	Debug << "generated " <<  mInitialSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+	Debug << "generated " << samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
 	// shoot simple ray and add it to importance samples
 	mPreprocessor->CastRays(simpleRays, passSamples);
-	Debug << "cast " <<  mInitialSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+	Debug << "cast " <<  samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
 	return (int)passSamples.size();
@@ -1835,8 +1835,8 @@
 			if (ray.mTerminationObject) 
 			{
-			  //				cout << "f";
-				if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
-					ray.mPdf,
-					contribution))
+				if (viewcell->GetPvs().GetSampleContribution(
+						ray.mTerminationObject,
+						ray.mPdf,
+						contribution))
 				{
 					++ ray.mPvsContribution;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1460)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1473)
@@ -2,4 +2,8 @@
 #include "GvsPreprocessor.h"
 #include "GlRenderer.h"
+#include "VssRay.h"
+#include "ViewCellsManager.h"
+
+
 
 namespace GtpVisibilityPreprocessor
@@ -7,57 +11,100 @@
   
 
-GvsPreprocessor::GvsPreprocessor():Preprocessor()
+GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0)
 {
-  environment->GetIntValue("RenderSampler.samples", mSamples);
-  cout << "number of render samples: " << mSamples << endl;
-}
-  
+	Environment::GetSingleton()->GetIntValue("RenderSampler.samples", mSamples);
+	//Environment::GetSingleton()->GetIntValue("Render.samples", mTotalSamples);
 
-void
-GvsPreprocessor::GenerateRandomRay(Ray &ray)
-{
-  int objId = RandomValue(0, mObjects.size());
-  Intersectable *object = objects[objId];
-  object->GetRandomSurfacePoint(point, normal);
-  direction = UniformRandomVector(normal);
-  SetupRay(ray, point, direction);
+	cout << "number of render samples: " << mSamples << endl;
 }
 
-void
-GvsPreprocessor::HandleRay(Ray &ray)
+
+int GvsPreprocessor::HandleRay(VssRay &ray)
 {
-  int sampleContributions = 0;
-  
-  mKdTree->CastRay(ray);
-  
-  if (ray.leaves.size()) {
-	sampleContributions += AddNodeSamples(object, ray, pass);
-    
-	if (ray.intersections.size()) {
-	  sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass);
+	int sampleContributions = 0;
+
+	if (mViewCellsManager->ComputeSampleContribution(ray, true, false))
+	{
+		return AdaptiveBorderSampling(ray);
 	}
-  }
+	else
+	{
+		return ReverseSampling(ray);
+	}
 }
 
-void
-GvsPreprocessor::BorderSampling(Ray &ray)
+
+int GvsPreprocessor::AdaptiveBorderSampling(VssRay &ray)
 {
-  
-  
+	// TODO
+	return 0;
 }
 
-void
-GvsPreprocessor::Pass()
+
+int GvsPreprocessor::ReverseSampling(VssRay &ray)
 {
-  Ray ray;
-  while (1) {
-	AvsGenerateRay(ray);
-	HandleRay(ray);
-	while ( !mRayQueue.empty() ) {
-	  Ray ray = mRayQueue.pop();
-	  mRayQueue.pop();
-	  AdaptiveBorderSampling(ray);
+	// TODO
+	return 0;
+}
+
+int GvsPreprocessor::CastAvsSamples(const int samplesPerPass, 
+									const int sampleType, 
+									RayQueue &passSamples)
+{
+	SimpleRayContainer simpleRays;
+	const long startTime = GetTime();
+
+	GenerateRays(samplesPerPass, sampleType, simpleRays);
+	Debug << "generated " <<  samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+	VssRayContainer samples;
+
+	// cast the rays and optain hitpoints with geometry
+	CastRays(simpleRays, samples);
+	Debug << "cast " <<  samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+	// add 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();
+}
+
+
+int GvsPreprocessor::Pass(const int passSamples)
+{
+	int castSamples = 0;
+	const int uniformSamples = 1000;
+
+	while (castSamples < passSamples) 
+	{
+		// Ray queue empty => 
+		// cast a number of uniform samples to fill ray Queue
+		CastAvsSamples(uniformSamples, mSamplingType, mRayQueue);
+		castSamples += Pass(passSamples);
+	}
+
+	return castSamples;
+}
+
+
+int GvsPreprocessor::RunSampling()
+{
+	int castSamples = 0;
+
+	while (!mRayQueue.empty()) 
+	{
+		// handle next ray
+		VssRay *ray = mRayQueue.top();
+		mRayQueue.pop();
+		
+		castSamples += HandleRay(*ray);
+	}
+
+	return castSamples;
 }
 
@@ -66,102 +113,29 @@
 GvsPreprocessor::ComputeVisibility()
 {
-  long startTime = GetTime();
+	Randomize(0);
+	const long startTime = GetTime();
+	
+	cout<<"Gvs Preprocessor started\n"<<flush;
+	
+	/// construct the view cells first
+	ConstructViewCells();
 
+	int samples = 0;
 
-  Debug << "type: gvs" << endl;
-  
-  cout<<"Gvs Preprocessor started\n"<<flush;
-  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
+	while (samples < mTotalSamples) 
+	{
+		samples += Pass(mSamplesPerPass);
+		
+		cout<<"+";
+		
+		// mVssRays.PrintStatistics(mStats);
+		//mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
+		//	   << "#TotalSamples\n" << samples << endl;
 
-  Randomize(0);
-  
-  long startTime = GetTime();
-  
-  int totalSamples = 0;
-  
-  ConstructViewCells();
-  
-  int samples = 0;
-  int i=0;
-  while (samples < mTotalSamples) {
-	for (i=0; i < mSamplesPerPass; i++, samples++) {
-	  
-	  if (i%10000 == 0)
-		cout<<"+";
-	  
-	  Vector3 origin, direction;
-	  mViewCellsManager->GetViewPoint(origin);
-	  direction = UniformRandomVector();
-	  
-	  ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
-	  
-	  if (viewcell && viewcell->GetValid()) {
-		// cast rays in both directions to make the number of samples comparable
-		// with the global sampling method which also casts a "double" ray per sample
-		for (int j=0; j < 2; j++) {
-		  Intersectable *object = CastRay(origin, direction);
-		  if (object) {
-			// if ray not outside of view space
-			float contribution;
-			int pvsContribution = 0;
-			float relativePvsContribution = 0;
-			if (object) {
-			  float pdf = 1.0f;
-			  if (viewcell->GetPvs().GetSampleContribution(object,
-														   pdf,
-														   contribution))
-				++pvsContribution;
-			  relativePvsContribution += contribution;
-			  viewcell->GetPvs().AddSample(object, pdf);
-			}
-		  }
-		  direction = -direction;
-		}
-	  }
-	  
-	  if (samples > mTotalSamples)
-		break;
+		mViewCellsManager->PrintPvsStatistics(mStats);
+		// ComputeRenderError();
 	}
-	
-	//	mVssRays.PrintStatistics(mStats);
-	mStats <<
-	  "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
-	  "#TotalSamples\n" <<samples<<endl;
 
-	mViewCellsManager->PrintPvsStatistics(mStats);
-	// ComputeRenderError();
-  }
-  
-  //  HoleSamplingPass();
-	if (0) {
-	  Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); 
-	  exporter->SetExportRayDensity(true);
-	  exporter->ExportKdTree(*mKdTree);
-	  delete exporter;
-	}
-	
-	
-	// $$JB temporary removed
-	//	mViewCellsManager->PostProcess(objects, mSampleRays);
-	
-	//-- several visualizations and statistics
-	Debug << "view cells after post processing: " << endl;
-	mViewCellsManager->PrintStatistics(Debug);
-	
-	//-- render simulation after merge
-	cout << "\nevaluating bsp view cells render time after merge ... ";
-	
-	mRenderSimulator->RenderScene();
-	SimulationStatistics ss;
-	mRenderSimulator->GetStatistics(ss);
-	
-	cout << " finished" << endl;
-	cout << ss << endl;
-	Debug << ss << endl;
-	
-	// $$JB temporary removed
-	//mViewCellsManager->Visualize(objects, mSampleRays);	
-
-  return true;
+	return true;
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1449)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1473)
@@ -603,5 +603,6 @@
 void HierarchyManager::ResetObjectSpaceSubdivision(const ObjectContainer &objects)
 {
-	Debug << "old bv hierarchy: " << mBvHierarchy->mBvhStats << endl;
+	Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
+	cout << "resetting bv hierarchy" << endl;
 	mHierarchyStats.nodes -= mBvHierarchy->mBvhStats.nodes;
 	mHierarchyStats.mGlobalCostMisses = 0; // hack: reset global cost misses
