Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1492)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1500)
@@ -29,17 +29,5 @@
 
 protected:
-
-	struct GvsRayInfo
-	{
-		GvsRayInfo(VssRay *ray, const bool d)
-			: mRay(ray), mFoundDiscontinuity(d) 
-		{}
-
-		VssRay *mRay;
-		bool mFoundDiscontinuity;
-	};
-
-	typedef stack<GvsRayInfo> RayQueue;
-
+#if 0
 	struct PendingRay
 	{
@@ -53,4 +41,6 @@
 
 	typedef stack<PendingRay> PendingQueue;
+#endif
+	typedef stack<VssRay *> RayQueue;
 
 	/** Runs the adaptive sampling until the ray queue is empty. 
@@ -82,10 +72,10 @@
 		b) if triangle was found reverse sampling
 	*/
-	int HandleRay(const GvsRayInfo &ray);
+	bool HandleRay(VssRay &ray);
 
 	/** The adaptive border sampling step. It aims to find neighbouring 
-		triangles of the one hit by the previous ray.
+		triangles of the one hit by the current ray.
 	*/	
-	int AdaptiveBorderSampling(const VssRay &prevRay);
+	int AdaptiveBorderSampling(const VssRay &currentRay);
 	
 	/** The reverse sampling step. It is started once the cast
@@ -94,15 +84,36 @@
 		triangle passing through a gap.
 	*/
-	int ReverseSampling(const VssRay &prevRay);
+	VssRay *ReverseSampling(const VssRay &currentRay,
+							const Triangle3 &hitTriangle,
+							const VssRay &oldRay);
 
-	/** Returns true if we sampled a closer triangle than with the previous ray.
+	/** Returns true if we sampled a closer triangle than with the previous ray. 
+		Does reverse sampling if gap found.
 	*/
-	const bool DiscontinuityFound(const VssRay &ray, const VssRay &prevRay) const;
+	bool CheckDiscontinuity(const VssRay &currentRay, 
+							const Triangle3 &hitTriangle,
+							const VssRay &oldRay);
 
 	/** Adds new samples to the ray queue and classifies them
 		with respect to the previous ray.
 	*/
-	void EnqueueRays(VssRayContainer &samples, VssRay *prevRay = NULL);
-	
+	void EnqueueRays(VssRayContainer &samples);
+
+	/** 
+		Hepler function for adaptive border sampling. It finds 
+		new sample points around a triangle in a eps environment
+	*/
+	void EnlargeTriangle(VertexContainer &vertices,
+						 const Triangle3 &hitTriangle,
+						 const VssRay &ray);
+
+	int SubdivideEdge(
+		const Triangle3 &hitTriangle,
+		const Vector3 &p1, 
+		const Vector3 &p2, 
+		const VssRay &x, 
+		const VssRay &y,
+		const VssRay &oldRay);
+
 	//////////////////////
 
@@ -116,4 +127,5 @@
 	AxisAlignedBox3 mViewSpaceBox;
 	float mEps;
+	float mThreshold;
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1489)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1500)
@@ -1275,4 +1275,9 @@
 		 "0.00001");
 
+    RegisterOption("GvsPreprocessor.threshold",
+		 optFloat,
+		 "gvs_threshold",
+		 "1.5");
+
 
   /***********************************************************************************/
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Plane3.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Plane3.cpp	(revision 1076)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Plane3.cpp	(revision 1500)
@@ -1,4 +1,5 @@
 #include "Plane3.h"
 #include "Matrix4x4.h"
+#include "Ray.h"
 
 
@@ -51,4 +52,55 @@
 
 
+ bool PlaneIntersection(const Plane3 &p1, const Plane3 &p2)
+ {
+	 return 
+		 p1.mNormal.x != p2.mNormal.x ||
+		 p1.mNormal.y != p2.mNormal.y ||
+		 p1.mNormal.z != p2.mNormal.z ||
+		 p1.mD == p2.mD;
+ }
+
+
+/*
+If the planes are known to intersect then determine the origin and unit direction vector of a 
+line formed by the intersection of two planes. 
+The direction of that line is just the vector product of the normals of the planes. 
+Finding a point along the intersection line is theoretically easy but numerical precision 
+issues mean that we need to determine which axis it is best to do the calculation in.
+The point will lie on the axis that was used to determine the intersection.
+*/
+SimpleRay GetPlaneIntersection(const Plane3 &plane1, const Plane3 &plane2)
+{
+	Vector3 point, dir;
+	dir = CrossProd(plane1.mNormal, plane2.mNormal);
+    
+	float abs;
+	const int index = dir.DrivingAxis();
+   
+	switch (index)
+	{
+	case 0:
+		point[0] = 0.0f;
+		point[1] = (plane1.mNormal[2] * plane2.mD - plane2.mNormal[2] * plane1.mD) / dir[0] ;
+		point[2] = (plane2.mNormal[1] * plane1.mD - plane1.mNormal[1] * plane2.mD) / dir[0] ;
+		break;
+	case 1:
+		point[0] = (plane2.mNormal[2] * plane1.mD - plane1.mNormal[2] * plane2.mD) / dir[1] ;
+		point[1] = 0.0f ;
+		point[2] = (plane1.mNormal[0] * plane2.mD - plane2.mNormal[0] * plane1.mD) / dir[1] ;
+		break;
+	case 2:
+		point[0] = (plane1.mNormal[1] * plane2.mD - plane2.mNormal[1] * plane1.mD) / dir[2] ;
+		point[1] = (plane2.mNormal[0] * plane1.mD - plane1.mNormal[0] * plane2.mD) / dir[2] ;
+		point[2] = 0.0f ;
+		break;
+	}
+
+	Normalize(dir);
+
+	return SimpleRay(point, dir);
+}
+
+
 Vector3 Plane3::FindIntersection(const Vector3 &a,						   
 								 const Vector3 &b,
@@ -62,8 +114,12 @@
 	{
 		if (coplanar) 
+		{
 			(*coplanar) = true;	
+		}
 
 		if (t) 
+		{
 			(*t) = 0;
+		}
 
 		return a;
@@ -71,11 +127,15 @@
 	
 	if (coplanar) 
+	{
 		(*coplanar) = false;
+	}
 
     float u = - Distance(a) / dv; // TODO: could be done more efficiently
     
 	if (t) 
+	{
 		(*t) = u;
-	
+	}
+
     return a + u * v;
 }
@@ -94,5 +154,4 @@
     
 	// does not intersect or coincident
-
 	if (signum(dv) == 0)
 		return 0;
@@ -101,3 +160,16 @@
 }
 
+
+float Plane3::FindT(const SimpleRay &a) const 
+{	  
+	const Vector3 v = a.mDirection; // line from A to B
+	const float dv = DotProd(v, mNormal);
+    
+	// does not intersect or coincident
+	if (signum(dv) == 0)
+		return 0;
+	
+	return - Distance(a.mOrigin) / dv; // TODO: could be done more efficiently
 }
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Plane3.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Plane3.h	(revision 1418)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Plane3.h	(revision 1500)
@@ -6,6 +6,8 @@
 namespace GtpVisibilityPreprocessor {
 
-  
-/** 3D Plane */
+struct SimpleRay;
+
+/** 3D Plane.
+*/
 class Plane3 {
 public:
@@ -58,7 +60,13 @@
   float FindT(const Vector3 &a, const Vector3 &b) const;
 
+  float FindT(const SimpleRay &a) const;
+
   friend bool
   PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result);
   
+  friend bool PlaneIntersection(const Plane3 &p1, const Plane3 &p2);
+
+  friend SimpleRay GetPlaneIntersection(const Plane3 &plane1, const Plane3 &plane2);
+
   friend ostream &operator<<(ostream &s, const Plane3 p) {
     s<<p.mNormal<<" "<<p.mD;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1328)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1500)
@@ -633,5 +633,5 @@
 	  float sum = info.pvsBack*(info.position - minBox) + info.pvsFront*(maxBox - info.position);
 	  float newCost = ct_div_ci + sum/sizeBox;
-	  float oldCost = pvsSize;
+	  float oldCost = (float)pvsSize;
 	  info.costRatio = newCost/oldCost;
 	  break;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1492)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1500)
@@ -6,4 +6,6 @@
 #include "Triangle3.h"
 #include "IntersectableWrapper.h"
+#include "Plane3.h"
+
 
 
@@ -18,5 +20,5 @@
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
 	Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
-		
+	Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);	
 
 	Debug << "Gvs preprocessor options" << endl;
@@ -29,38 +31,45 @@
 
 
-const bool GvsPreprocessor::DiscontinuityFound(const VssRay &ray, 
-											   const VssRay &oldRay) const
-{
-	//|predicted(x)-xp|-|hit(x)-xp| > ?,
-    //const Plane3 plane = tri->GetPlane();
+bool GvsPreprocessor::CheckDiscontinuity(const VssRay &currentRay,
+										 const Triangle3 &hitTriangle,
+										 const VssRay &oldRay)
+{
+	const float dist = Magnitude(oldRay.GetDir());
+	const float newDist = Magnitude(currentRay.GetDir());
+ 
+#if 0
+	if ((dist - newDist) > mThresHold)
+#else // rather take relative distance
+	if ((dist / newDist) > mThreshold)
+#endif
+	{
+		VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
+		HandleRay(*newRay);
+		return true;
+	}
+
 	return false;
 }
 
 
-int GvsPreprocessor::HandleRay(const GvsRayInfo &gvsRay)
-{
-	VssRay *ray = gvsRay.mRay;
-
-	if (!mViewCellsManager->ComputeSampleContribution(*ray, true, false))
-		return 1;
-
-	if (gvsRay.mFoundDiscontinuity)
-	{
-		return ReverseSampling(*ray);
-	}
-	else
-	{
-		return AdaptiveBorderSampling(*ray);
-	}
-}
-
-/** Hepler function for adaptive border sampling. It finds 
-	new sample points around a triangle in a eps environment
+bool GvsPreprocessor::HandleRay(VssRay &vssRay)
+{
+	if (!mViewCellsManager->ComputeSampleContribution(vssRay, true, false))
+	{
+		mRayQueue.push(&vssRay);
+		return true;
+	}
+
+	return false;
+}
+
+
+/** Creates 3 new vertices for triangle vertex with specified index.
 */
-static void CreateNewRays(SimpleRayContainer &simpleRays, 
-						  const Triangle3 &hitTriangle,
-						  const VssRay &ray, 
-						  const int index,
-						  const float eps)
+static void CreateNewVertices(VertexContainer &vertices,
+							  const Triangle3 &hitTriangle,
+							  const VssRay &ray, 
+							  const int index,
+							  const float eps)
 {
 	const int indexU = (index + 1) % 3;
@@ -75,31 +84,88 @@
 	const Vector3 dir1 = CrossProd(a, b); //N((pi-xp)×(pi+1- pi));
 	const Vector3 dir2 = CrossProd(a, c); // N((pi-xp)×(pi- pi-1))
-	const Vector3 dir3 = DotProd(dir1, dir2) > 0 ? 
-		Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir2) + CrossProd(dir1, a)); // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
+	const Vector3 dir3 = DotProd(dir1, dir2) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
+		Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir2) + CrossProd(dir1, a)); 
 
 	// compute the new three hit points
-	// pi, i + 1
-	const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1; //pi+ e·|pi-xp|·di, j
-	// pi, i - 1
-    const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2; //pi+ e·|pi-xp|·di, j
-	// pi, i
-	const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3; //pi+ e·|pi-xp|·di, j
-	
-	/// create simple rays and store them in container
-	Vector3 rayDir;
-	rayDir = Normalize(pt1 - ray.GetOrigin());
-	simpleRays.push_back(SimpleRay(rayDir, ray.GetOrigin()));
-	rayDir = Normalize(pt2 - ray.GetOrigin());
-	simpleRays.push_back(SimpleRay(rayDir, ray.GetOrigin()));
-	rayDir = Normalize(pt3 - ray.GetOrigin());
-	simpleRays.push_back(SimpleRay(rayDir, ray.GetOrigin()));
-}
-
-
-
-int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &prevRay)
+	// pi, i + 1:  pi+ e·|pi-xp|·di, j
+	const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1;
+	// pi, i - 1:  pi+ e·|pi-xp|·di, j
+    const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2;
+	// pi, i:  pi+ e·|pi-xp|·di, j
+	const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3;
+	
+	vertices.push_back(pt1);
+	vertices.push_back(pt2);
+	vertices.push_back(pt3);
+}
+
+
+void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
+									  const Triangle3 &hitTriangle,
+									  const VssRay &ray)
+{
+	CreateNewVertices(vertices, hitTriangle, ray, 0, mEps);
+	CreateNewVertices(vertices, hitTriangle, ray, 1, mEps);
+	CreateNewVertices(vertices, hitTriangle, ray, 2, mEps);
+}
+
+
+static Vector3 CalcPredictedHitPoint(const VssRay &newRay, 
+									 const Triangle3 &hitTriangle,
+									 const VssRay &oldRay)
+{
+	Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
+
+	const Vector3 hitPt = 
+		plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
+	
+	return hitPt;
+}
+
+
+static bool EqualVisibility(const VssRay &a, const VssRay &b)
+{
+	return a.mTerminationObject == b.mTerminationObject;
+}
+
+
+int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
+								   const Vector3 &p1, 
+								   const Vector3 &p2, 
+								   const VssRay &x, 
+								   const VssRay &y,
+								   const VssRay &oldRay)
+{
+	// the predicted hitpoint expects to hit the same mesh again
+	const Vector3 predictedHitX = CalcPredictedHitPoint(x, hitTriangle, oldRay);
+	const Vector3 predictedHitY = CalcPredictedHitPoint(y, hitTriangle, oldRay);
+
+	CheckDiscontinuity(x, hitTriangle, oldRay);
+	CheckDiscontinuity(y, hitTriangle, oldRay);
+
+	if (EqualVisibility(x, y))
+	{
+		return 2;
+	}
+	else
+	{
+		const Vector3 p = (p1 + p2) * 0.5f;
+		SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin);
+		VssRay *newRay = NULL;
+		//VssRay *newRay = CastSingleRay(sray);
+		HandleRay(*newRay);
+
+		const int s1 = SubdivideEdge(hitTriangle, p1, p, x, *newRay, oldRay);
+		const int s2 = SubdivideEdge(hitTriangle, p, p2, *newRay, y, oldRay);
+								  
+        return s1 + s2;
+	}
+}
+
+
+int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &currentRay)
 {
 	cout << "a";
-	Intersectable *tObj = prevRay.mTerminationObject;
+	Intersectable *tObj = currentRay.mTerminationObject;
 	Triangle3 hitTriangle;
 
@@ -110,26 +176,86 @@
 	}
 
+	VertexContainer enlargedTriangle;
+	
+	/// create 3 new hit points for each vertex
+	EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
+	
+	/// create rays from sample points and handle them
 	SimpleRayContainer simpleRays;
 	simpleRays.reserve(9);
 
-	CreateNewRays(simpleRays, hitTriangle, prevRay, 0, mEps);
-	CreateNewRays(simpleRays, hitTriangle, prevRay, 1, mEps);
-	CreateNewRays(simpleRays, hitTriangle, prevRay, 2, mEps);
-
+	VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
+
+	for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
+	{
+		const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
+		simpleRays.push_back(SimpleRay(*vit, rayDir));
+	}
+
+	// establish visibility
 	VssRayContainer vssRays;
-	
 	CastRays(simpleRays, vssRays);
 	// add to ray queue
 	EnqueueRays(vssRays);
 
+    // recursivly subdivide each edge
+	for (int i = 0; i < 9; ++ i)
+	{
+		SubdivideEdge(
+			hitTriangle,
+			enlargedTriangle[i], 
+			enlargedTriangle[(i + 1) % 9], 
+			*vssRays[i], 
+			*vssRays[(i + 1) % 9],
+			currentRay);
+	}
+
 	return (int)vssRays.size();
 }
 
 
-int GvsPreprocessor::ReverseSampling(const VssRay &oldRay)
+static Vector3 GetPassingPoint(const VssRay &currentRay,
+										 const Triangle3 &hitTriangle,
+										 const VssRay &oldRay)
+{
+	// intersect triangle plane with plane spanned by current samples
+	Plane3 plane(currentRay.GetOrigin(), currentRay.GetTermination(), oldRay.GetTermination());
+	Plane3 triPlane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
+
+	SimpleRay intersectLine = GetPlaneIntersection(plane, triPlane);
+
+	// Evaluate new hitpoint just outside the triangle
+	const float factor = 0.95f;
+	float t = triPlane.FindT(intersectLine);
+
+	const Vector3 newPoint = intersectLine.mOrigin + t * factor * intersectLine.mDirection;
+
+	return newPoint;
+}
+
+
+VssRay *GvsPreprocessor::ReverseSampling(const VssRay &currentRay,
+										 const Triangle3 &hitTriangle,
+										 const VssRay &oldRay)
 {
 	cout << "r" << endl;
-	// TODO
-	return 1;
+	
+	
+	//-- The plane p = (xp, hit(x), hit(xold)) is intersected
+	//-- with the newly found triangle (xold is the previous ray from
+	//-- which x was generated). On the intersecting line, we select a point
+	//-- pnew which lies just outside of the new triangle so the ray
+	//-- just passes by inside the gap
+    const Vector3 newPoint = GetPassingPoint(currentRay, hitTriangle, oldRay);
+	const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
+
+	//-- Construct the mutated ray with xnew,dir = predicted(x)- pnew 
+	//-- as direction vector
+	const Vector3 newDir = predicted - newPoint ;
+	// take xnew,p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
+	// difficult to say!!
+	const Vector3 newOrigin = newDir * -5000.0f;
+
+	return new VssRay(currentRay);
 }
 
@@ -157,13 +283,12 @@
 
 
-void GvsPreprocessor::EnqueueRays(VssRayContainer &samples, VssRay *oldRay)
+void GvsPreprocessor::EnqueueRays(VssRayContainer &samples)
 {
 	// add samples to ray queue
 	VssRayContainer::const_iterator vit, vit_end = samples.end();
+	
 	for (vit = samples.begin(); vit != vit_end; ++ vit)
 	{
-		/// if there is no old ray, no discontinuity
-		const bool gap = oldRay ? DiscontinuityFound(*(*vit), *oldRay) : false;
-		mRayQueue.push(GvsRayInfo(*vit, gap));
+		HandleRay(*(*vit));
 	}
 }
@@ -196,8 +321,8 @@
 	{
 		// handle next ray
-		GvsRayInfo rayInfo = mRayQueue.top();
+		VssRay *ray = mRayQueue.top();
 		mRayQueue.pop();
 		
-		castSamples += HandleRay(rayInfo);
+		castSamples += HandleRay(*ray);
 	}
 
@@ -227,6 +352,6 @@
 		castSamples += passSamples;
 		
-		/////////////
-		// -- stats
+		////////
+		//-- stats
 		cout << "+";
 		cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
