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 1460)
@@ -0,0 +1,33 @@
+#ifndef _GvsPreprocessor_H__
+#define _GvsPreprocessor_H__
+
+#include <fstream>
+using namespace std;
+
+#include "Preprocessor.h"
+
+namespace GtpVisibilityPreprocessor {
+
+class Exporter;
+
+/** 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();
+  
+  
+protected:
+  ofstream mStats;
+  
+};
+
+};
+
+#endif
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 1460)
@@ -0,0 +1,168 @@
+#include "Environment.h"
+#include "GvsPreprocessor.h"
+#include "GlRenderer.h"
+
+namespace GtpVisibilityPreprocessor
+{
+  
+
+GvsPreprocessor::GvsPreprocessor():Preprocessor()
+{
+  environment->GetIntValue("RenderSampler.samples", mSamples);
+  cout << "number of render samples: " << mSamples << endl;
+}
+  
+
+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);
+}
+
+void
+GvsPreprocessor::HandleRay(Ray &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);
+	}
+  }
+}
+
+void
+GvsPreprocessor::BorderSampling(Ray &ray)
+{
+  
+  
+}
+
+void
+GvsPreprocessor::Pass()
+{
+  Ray ray;
+  while (1) {
+	AvsGenerateRay(ray);
+	HandleRay(ray);
+	while ( !mRayQueue.empty() ) {
+	  Ray ray = mRayQueue.pop();
+	  mRayQueue.pop();
+	  AdaptiveBorderSampling(ray);
+	}
+  }
+}
+
+
+bool
+GvsPreprocessor::ComputeVisibility()
+{
+  long startTime = GetTime();
+
+
+  Debug << "type: gvs" << endl;
+  
+  cout<<"Gvs Preprocessor started\n"<<flush;
+  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<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;
+	}
+	
+	//	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;
+}
+
+}
