Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2739)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2740)
@@ -83,5 +83,9 @@
 	ObjectContainer mKdPvs;
 
-	float RaysPerSec() const { if (!mTotalTime) return 0; return (float)(mTotalSamples / mTotalTime) * 1e-6f; }
+	float RaysPerSec() const 
+	{ 
+		if (!mTotalTime) return 0; 
+		return (float)(mTotalSamples / mTotalTime) * 1e-6f; 
+	}
 
 	void Print(ostream &app) const;
@@ -304,5 +308,5 @@
 									SimpleRay &ray);
 
-	inline bool IntersectViewCell(Vector3 &origin, Vector3 &dir) const;
+	inline bool IntersectsViewCell(Vector3 &origin, const Vector3 &dir) const;
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 2720)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 2740)
@@ -392,4 +392,8 @@
 			 AxisAlignedBox3 &right) const;
 
+  bool Intersects(const SimpleRay &ray,  float &tnear, float &tfar) const;
+
+  Rectangle3 GetNearestFace(const Vector3 &pt) const;
+
 #define __EXTENT_HACK
   // get the extent of face
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2739)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2740)
@@ -677,5 +677,7 @@
 	// handle cast rays
 	EnqueueRays(vssRays);
-	
+
+
+#if 0
 	for (size_t i = 0; i < vssRays.size(); ++ i)
 	{
@@ -684,6 +686,5 @@
 	}
 
-#if 0
-	
+
     // recursivly subdivide each edge
 	for (int i = 0; i < numBorderSamples; ++ i)
@@ -791,5 +792,5 @@
     if (mPerViewCell)
 	{
-		if (!IntersectViewCell(newOrigin, newDir))
+		if (!IntersectsViewCell(newOrigin, -newDir))
 			return false;
 	}
@@ -801,21 +802,25 @@
 
 
-bool GvsPreprocessor::IntersectViewCell(Vector3 &origin, Vector3 &dir) const
-{
-	// send ray to view cell
-	static Ray testRay;
-
-	testRay.Clear();
-	testRay.Init(origin, -dir, Ray::LOCAL_RAY);
-
+bool GvsPreprocessor::IntersectsViewCell(Vector3 &origin, const Vector3 &dir) const
+{
+	AxisAlignedBox3 box = mCurrentViewCell->GetBox();
+
+	if (box.IsInside(origin))
+	{
+		//cout<<"z";
+		return true;
+	}
+	
+	// intersect ray with view cell and 
+	SimpleRay ray(origin, dir, 0, 1.0f);
+
+	float tnear, tfar;
+	
 	// check if ray intersects view cell
-	if (!mCurrentViewCell->CastRay(testRay))
+	if (!mCurrentViewCell->GetBox().Intersects(ray, tnear, tfar))
 		return false;
 
-	Ray::Intersection &hit = testRay.intersections[0];
-
-	// the ray starts from the view cell
-	origin = testRay.Extrap(hit.mT);
-
+	origin = origin + dir * tnear;
+	//cout<<"r";
 	return true;
 }
@@ -2043,5 +2048,7 @@
 	size_t currentNumber = rays.size();
 
-	while ((rays.size() - currentNumber) < number)
+	int tries = 0;
+
+	while (((rays.size() - currentNumber) < number) && (tries ++ < 20))
 	{
 		SimpleRay ray;
@@ -2049,4 +2056,6 @@
 			rays.push_back(ray);
 	}
+
+	//cout << "generated " << rays.size() << " rays" << endl;
 }
 
@@ -2057,6 +2066,10 @@
 												 SimpleRay &ray)
 {
-	Vector3 nd = triangle.GetNormal();
-  
+	//Vector3 nd = triangle.GetNormal();
+	const AxisAlignedBox3 box = mCurrentViewCell->GetBox();
+	Vector3 nd = box.GetNearestFace(origin).GetNormal();
+	//cout << "pt: " << origin << " face: " << box.GetNearestFace(origin) << endl;
+	nd.Normalize();
+
 	// Compute right handed coordinate system from direction
 	Vector3 U, V;
@@ -2077,9 +2090,7 @@
 	const Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
 
-	Vector3 newOrigin = origin;
-
-	//cout << "t: " << termination;
-	newOrigin += shift;
-	//cout << " new t: " << termination << endl;
+	Vector3 newOrigin = origin + shift;
+	//cout << "o: " << origin << " new o: " << newOrigin << endl;
+
 	Vector3 direction = termination - newOrigin;
 
@@ -2091,7 +2102,11 @@
 	direction /= len;
 
-	if (!IntersectViewCell(newOrigin, direction))
+	if (!IntersectsViewCell(newOrigin, -direction))
+	{
+		//cout << "x";
 		return false;
-
+	}
+
+	//cout << "y";
 	// $$ jb the pdf is yet not correct for all sampling methods!
 	const float pdf = 1.0f;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp	(revision 2723)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp	(revision 2740)
@@ -9,12 +9,14 @@
 #include "VssRay.h"
 #include "IntersectableWrapper.h"
-
-
+#include "SimpleRay.h"
 #include <cassert>
 
 
+
 using namespace std;
 
-namespace GtpVisibilityPreprocessor {
+
+namespace GtpVisibilityPreprocessor 
+{
 
 #define FATAL Debug
@@ -2632,4 +2634,115 @@
 }
 
-}
-
+
+Rectangle3 AxisAlignedBox3::GetNearestFace(const Vector3 &pt) const
+{
+	float minDist = 1e20f;
+	int face = 0;
+
+	if ((pt.x - mMin.x) < minDist) {minDist = (pt.x - mMin.x); face = 0;}
+	if ((mMax.x - pt.x) < minDist) {minDist = (mMax.x - pt.x); face = 1;}
+	if ((pt.y - mMin.y) < minDist) {minDist = (pt.y - mMin.y); face = 2;}
+	if ((mMax.y - pt.y) < minDist) {minDist = (mMax.y - pt.y); face = 3;}
+	if ((pt.z - mMin.z) < minDist) {minDist = (pt.z - mMin.z); face = 4;}
+	if ((mMax.z - pt.z) < minDist) {minDist = (mMax.z - pt.z); face = 5;}
+
+	return GetFace(face);
+}
+
+
+bool AxisAlignedBox3::Intersects(const SimpleRay &ray, float &tnear, float &tfar) const
+{
+	tnear = -1e20f;
+	tfar = 1e20f;
+
+	const Vector3 origin = ray.mOrigin;
+	const Vector3 dir = ray.mDirection;
+
+	float t1, t2;
+
+	// ray is parallel to the planes
+	if (dir.x == 0) 
+	{
+		if ((origin.x < mMin.x) || (origin.x > mMax.x)) 
+			return false; // origin not between planes
+	}
+	else
+	{
+		// time at which ray intersects minimum X plane
+		t1 = (mMin.x - origin.x) / dir.x;
+		// time at which ray intersects maximum X plane
+		t2 = (mMax.x - origin.x) / dir.x;
+		
+		if (t1 > t2)
+			swap(t1, t2);
+		
+		if (t1 > tnear)
+			tnear = t1;
+		
+		if (t2 < tfar)
+			tfar = t2;
+		
+		if (tnear > tfar)
+			return false;
+		
+		if (tfar < 0)
+			return false;
+	}
+
+	if (dir.y == 0) // ray is parallel to the planes
+	{
+		if ((origin.y < mMin.y) || (origin.y > mMax.y)) 
+			return false; // origin not between planes)
+	}
+	else
+	{
+		t1 = (mMin.y - origin.y) / dir.y; 
+		t2 = (mMax.y - origin.y) / dir.y; 
+		
+		if (t1 > t2)
+			swap(t1, t2);
+		
+		if (t1 > tnear)
+			tnear = t1;
+		
+		if (t2 < tfar)
+			tfar = t2;
+		
+		if (tnear > tfar)
+			return false;
+		
+		if (tfar < 0)
+			return false;
+	}
+
+	if (dir.z == 0) // ray is parallel to the planes
+	{
+		if ((origin.z < mMin.z) || (origin.z > mMax.z)) 
+			return false; // origin not between planes)
+	}
+	else
+	{
+		t1 = (mMin.z - origin.z) / dir.z; 
+		t2 = (mMax.z - origin.z) / dir.z; 
+		
+		if (t1 > t2)
+			swap(t1, t2);
+		
+		if (t1 > tnear)
+			tnear = t1;
+		
+		if (t2 < tfar)
+			tfar = t2;
+		
+		if (tnear > tfar)
+			return false;
+		
+		if (tfar < 0)
+			return false;
+	}
+
+	return true;
+}
+
+}
+
