Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2730)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2731)
@@ -26,5 +26,5 @@
 #LOG_PREFIX=../src/SG08/rebut-arena-pixel2
 LOG_PREFIX=../src/SG08/speedtest
-LOG_PREFIX=../src/SG08/arena_trees
+LOG_PREFIX=../src/SG08/importance_3.0
 
 ENVIRONMENT=gvs.env
@@ -42,9 +42,10 @@
 -gvs_total_samples=1000000 \
 -gvs_samples_per_pass=1000000 \
--gvs_initial_samples=1 \
+-gvs_initial_samples=16 \
 -gvs_max_viewcells=3 \
--gvs_min_contribution=500 \
+-gvs_min_contribution=50 \
 -gvs_per_viewcell=true \
 -preprocessor_detect_empty_viewspace+ \
+-preprocessor_snap_error_frames+ \
 -kd_pvs_area=1e-4 \
 -view_cells_use_kd_pvs+ \
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2730)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2731)
@@ -274,6 +274,10 @@
 	virtual void ComputeRenderError();
 
-	SimpleRay GenerateJitteredSample(const SimpleRay &ray);
-
+	bool GenerateImportanceSample(const VssRay &ray, const Triangle3 &triangle, SimpleRay &sray);
+
+	void GenerateImportanceSamples(const VssRay &ray, 
+		                           const Triangle3 &triangle, 
+								   int numSamples,
+								   SimpleRayContainer &simpleRays);
 
 	//////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2687)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2731)
@@ -1923,4 +1923,11 @@
 				   "false");
 	
+	
+	RegisterOption("Preprocessor.snapErrorFrames",
+				   optBool,
+				   "preprocessor_snap_error_frames",
+				   "false");
+	
+
 	RegisterOption("Preprocessor.useVbos",
 				   optBool,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2730)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2731)
@@ -148,6 +148,6 @@
 		                                      mDetectEmptyViewSpace);
 
-	//mSnapErrorFrames = false;
-	mSnapErrorFrames = true;
+	Environment::GetSingleton()->GetBoolValue("Preprocessor.snapErrorFrames",
+		                                      mSnapErrorFrames);
 
 	mSnapPrefix = "snap/";
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2730)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2731)
@@ -25,5 +25,5 @@
 #define NOT_ACCOUNTED_OBJECT 0
 #define ACCOUNTED_OBJECT 2
-
+#define DETERMINISTIC_GVS 0
 
 static const float MIN_DIST = 0.001f;
@@ -182,5 +182,5 @@
 	reverseRays.clear();
 
-	if (0)
+	if (DETERMINISTIC_GVS)
 	{
 		VssRay *reverseRay = 
@@ -301,5 +301,5 @@
 bool GvsPreprocessor::HandleRay(VssRay *vssRay)
 {
-	if (!HasContribution(*vssRay))
+	if (!vssRay || !HasContribution(*vssRay))
 		return false;
 
@@ -491,11 +491,21 @@
 
 	const int numRandomRays = 16 - (int)simpleRays.size();
-	SimpleRay mainRay = SimpleRay(currentRay.GetOrigin(), 
-		                          currentRay.GetNormalizedDir(), 
-								  SamplingStrategy::GVS, 
-								  1.0f);
-
-	GenerateJitteredRays(simpleRays, mainRay, numRandomRays, 0);
-	//GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples);
+
+	if (!DETERMINISTIC_GVS)
+	{
+		if (0)
+		{
+			SimpleRay mainRay = SimpleRay(currentRay.GetOrigin(), 
+				                          currentRay.GetNormalizedDir(), 
+										  SamplingStrategy::GVS, 
+										  1.0f);
+			
+			GenerateJitteredRays(simpleRays, mainRay, numRandomRays, 0);
+		}
+		else
+			GenerateImportanceSamples(currentRay, hitTriangle, numRandomRays, simpleRays);
+	}
+	else
+		GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples);
 
 	generationTimer.Exit();
@@ -799,5 +809,5 @@
 	castTimer.Entry();
 
-	if (0)
+	if (DETERMINISTIC_GVS)
 	{
 		const bool castDoubleRays = !mPerViewCell;
@@ -1511,5 +1521,5 @@
 			
 			// compute the pixel error of this visibility solution
-			if (mEvaluatePixelError)
+			if (0 && mEvaluatePixelError)
 				ComputeRenderError();
 		}
@@ -1521,4 +1531,7 @@
 	ComputeViewCellGeometryIntersection();
 		
+	// compute the pixel error of this visibility solution
+	if (mEvaluatePixelError)
+		ComputeRenderError();
 
 	////////
@@ -1865,12 +1878,34 @@
 
 
-SimpleRay GvsPreprocessor::GenerateJitteredSample(const SimpleRay &sray)
-{
-	// mutate the origin
-	Vector3 d = sray.mDirection;
+void GvsPreprocessor::GenerateImportanceSamples(const VssRay &ray, 
+												const Triangle3 &triangle, 
+												int numSamples,
+												SimpleRayContainer &simpleRays)
+{
+	const size_t samplesSize = simpleRays.size();
+
+	while (simpleRays.size() < (samplesSize + numSamples))
+	{
+		SimpleRay sray;
+		if (GenerateImportanceSample(ray, triangle, sray))
+			simpleRays.push_back(sray);
+	}
+}
+
+
+bool GvsPreprocessor::GenerateImportanceSample(const VssRay &ray, 
+											   const Triangle3 &triangle, 
+											   SimpleRay &sray)
+{
+	Vector3 d = ray.GetDir();
   
 	// Compute right handed coordinate system from direction
 	Vector3 U, V;
-	d.RightHandedBase(U, V);
+
+	Vector3 nd = Normalize(d);
+	nd.RightHandedBase(U, V);
+	
+	Vector3 origin = ray.mOrigin;
+	Vector3 termination = ray.mTermination;
 
 	float rr[2];
@@ -1880,20 +1915,31 @@
 
 	Vector2 vr2(rr[0], rr[1]);
+	const float sigma = triangle.GetBoundingBox().Radius() * 3.0f;
 	Vector2 gaussvec2;
-
-	const float sigma = 0.2f;
 
 	GaussianOn2D(vr2, sigma, // input
 		         gaussvec2); // output
 	
-	Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
-	
-	d += shift;
+	const Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
+
+	//cout << "t: " << termination;
+	termination += shift;
+	//cout << " new t: " << termination << endl;
+	Vector3 direction = termination - origin;
+
+	const float len = Magnitude(direction);
+	
+	if (len < Limits::Small)
+		return false;
   
-	d.Normalize();
-
-	return SimpleRay(sray.mOrigin, d, SamplingStrategy::GVS, 1.0f);
-}
-
-
-}
+	direction /= len;
+
+	// $$ jb the pdf is yet not correct for all sampling methods!
+	const float pdf = 1.0f;
+	sray = SimpleRay(origin, direction, SamplingStrategy::GVS, pdf);
+	
+	return true;
+}
+
+
+}
