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;
