Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h	(revision 1974)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h	(revision 1984)
@@ -68,10 +68,10 @@
 
 protected:
-  void
-  _SortRays(SimpleRayContainer &rays,
-			const int l,
-			const int r,
-			const int axis);
-
+  void _SortRays(SimpleRayContainer &rays,
+				 const int l,
+				 const int r,
+				 const int depth,
+				 float box[12]);
+	
   struct Intersection
 	{
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1978)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1984)
@@ -404,5 +404,5 @@
 {
 	if (index == -1)
-		return NULL;
+	  return NULL;
 	
 	if (!mFaceParents.empty())
@@ -419,15 +419,14 @@
 	}
 	else
-	{
-		if (index >= (int)mObjects.size())
-		{
-			cerr << "Warning: triangle index out of range! " << index << endl;
-			return NULL;
-		}
-		else
-		{
-			return mObjects[index];
-		}
-	}
+	  {
+	  if (index >= (int)mObjects.size()) {
+		cerr<<"Warning: triangle  index out of range! "<<index<<endl;
+		return NULL;
+	  }
+	  else
+		{
+		  return mObjects[index];
+		}
+	  }
 }
 
@@ -1156,4 +1155,18 @@
   mRayCaster->SortRays(rays);
   cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" s."<<endl;
+
+  if (1) {
+	VssRayContainer tmpRays;
+	for (int i=0; i < 200; i++) {
+	  tmpRays.push_back(new VssRay(rays[i].mOrigin,
+								   rays[i].mOrigin + 100.0f*rays[i].mDirection,
+								   NULL,
+								   NULL
+								   )
+						);
+	  
+	}
+	ExportRays("sorted_rays.x3d", tmpRays, 200);
+  }
 #endif
   
@@ -1188,5 +1201,5 @@
 	
 		if ((int)rays.size() > 10000 && i % 10000 == 0)
-			cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
+		  cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
 	}
     
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.h	(revision 1983)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.h	(revision 1984)
@@ -11,4 +11,5 @@
 class QtGlViewer : public QGLWidget
 {
+
   Q_OBJECT
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 1983)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 1984)
@@ -28,6 +28,6 @@
 bool IntelRayCaster::InitRayCast(const string externKdTree)
 {
-	cout<<"Ray Cast file: " << externKdTree << endl;
-	return mlrtaLoadAS(externKdTree.c_str());
+  cout<<"Ray Cast file: " << externKdTree << endl;
+  return mlrtaLoadAS(externKdTree.c_str());
 }
 
@@ -51,9 +51,9 @@
 
 	hittriangle = mlrtaIntersectAS(
-		&simpleRay.mOrigin.x,
-		&simpleRay.mDirection.x,
-		normal,
-		dist);
-
+								   &simpleRay.mOrigin.x,
+								   &simpleRay.mDirection.x,
+								   normal,
+								   dist);
+	
 	if (hittriangle != -1 ) {
 		Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
@@ -73,5 +73,6 @@
 	if (castDoubleRay)
 	{
-		Vector3 dir = -simpleRay.mDirection;
+	  cerr<<"HERE"<<endl;
+	  Vector3 dir = -simpleRay.mDirection;
 		hittriangle = mlrtaIntersectAS(
 										&simpleRay.mOrigin.x,
@@ -122,9 +123,9 @@
 #endif
 
-	int forward_hit_triangles[16];
-	float forward_dist[16];
-
-	int backward_hit_triangles[16];
-	float backward_dist[16];
+	static int forward_hit_triangles[16];
+	static float forward_dist[16];
+
+	static int backward_hit_triangles[16];
+	static float backward_dist[16];
 
 	
@@ -185,4 +186,5 @@
 		Debug<<"FH\n"<<flush;
 #endif
+
 		Intersectable *intersect = mPreprocessor.GetParentObject(forward_hit_triangles[i]);
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1983)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1984)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: po 15. I 15:33:12 2007
+# Generated by qmake (2.00a) (Qt 4.1.2) on: po 15. I 21:59:19 2007
 # Project:  preprocessor.pro
 # Template: app
@@ -63,5 +63,5 @@
 	$(MAKE) -f $(MAKEFILE).Debug uninstall
 
-Makefile: preprocessor.pro  C:/Qt/4.1.2/mkspecs/win32-msvc2005\qmake.conf C:/Qt/4.1.2/mkspecs/qconfig.pri \
+Makefile: preprocessor.pro  C:/Qt/4.1.2/mkspecs/win32-msvc.net\qmake.conf C:/Qt/4.1.2/mkspecs/qconfig.pri \
 		C:\Qt\4.1.2\mkspecs\features\qt_config.prf \
 		C:\Qt\4.1.2\mkspecs\features\exclusive_builds.prf \
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1974)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1984)
@@ -606,4 +606,5 @@
 				)
 {
+  
   int hits = 0;
   
@@ -614,5 +615,4 @@
 
   //  ray.ComputeInvertedDir();
-
   Intersectable::NewMail();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1974)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1984)
@@ -123,5 +123,28 @@
 RayCaster::SortRays(SimpleRayContainer &rays)
 {
-  _SortRays(rays, 0, rays.size()-1, 0);
+  AxisAlignedBox3 box =
+	mPreprocessor.mViewCellsManager->GetViewSpaceBox();
+	
+  float b[12]={
+	box.Min().x,
+	box.Min().y,
+	box.Min().z,
+	-1,
+	-1,
+	-1,
+	box.Max().x,
+	box.Max().y,
+	box.Max().z,
+	1,
+	1,
+	1
+  };
+
+  _SortRays(rays,
+			0,
+			rays.size()-1,
+			0,
+			b
+			);
 }
 					
@@ -130,13 +153,34 @@
 					 const int l,
 					 const int r,
-					 const int axis)
+					 const int depth,
+					 float box[12])
 {
   // pick-up a pivot
-  int     i=l,j=r;
-  float x = rays[(l+r)/2].GetParam(axis);
+  int axis;
+  
+  float maxDiff = -1.0f;
+  // get the largest axis
+  int offset = 0;
+
+  if (depth%5==0)
+  	offset = 3;
+  
+  for (int i=offset; i < offset+3; i++) {
+	float diff = box[i+6] - box[i];
+	if (diff > maxDiff) {
+	  maxDiff = diff;
+	  axis = i;
+	}
+  }
+
+  //  cout<<depth<<" "<<axis<<endl;
+  
+  int i=l,j=r;
+  float x = (box[axis] + box[axis+6])*0.5f;
+  //  float x = rays[(l+r)/2].GetParam(axis);
   do {
-	while(rays[i].GetParam(axis) < x)
+	while(rays[i].GetParam(axis) < x && i<=j)
 	  i++;
-	while(x < rays[j].GetParam(axis))
+	while(x < rays[j].GetParam(axis) && i<=j)
 	  j--;
 	
@@ -148,9 +192,17 @@
   } while (i<=j);
   
-  if (l + 16 < j )
-	_SortRays(rays, l, j, (axis+1)%6);
-  
-  if (i + 16 < r)
-    _SortRays(rays, i, r, (axis+1)%6); 
+  if (l + 64 < j ) {
+	// set new max
+	float save = box[axis+6];
+	box[axis+6] = x;
+	_SortRays(rays, l, j, depth+1, box);
+	box[axis+6] = save;
+  }
+  
+  if (i + 64 < r) {
+	// set new min
+	box[axis] = x;
+    _SortRays(rays, i, r, depth+1, box);
+  }
 }
   
@@ -166,5 +218,5 @@
 					  const bool pruneInvalidRays)
 {
-	int hits = 0;
+  int hits = 0;
 
 #if DEBUG_RAYCAST
Index: GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 1966)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 1984)
@@ -74,6 +74,7 @@
 	
 	mMixtureDistribution->GenerateSamples(mSamplesPerPass, rays);
-	
-	CastRays(rays, vssRays, true, pruneInvalidRays);
+
+	bool doubleRays = true;
+	CastRays(rays, vssRays, doubleRays, pruneInvalidRays);
 	totalVssRays += (int)vssRays.size();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1972)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1984)
@@ -49,32 +49,31 @@
   mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
   ray.mFlags &= ~Ray::CULL_BACKFACES;
-  
-  if (mKdTree->CastRay(ray)) 
-	{
-	  hitA.mObject = ray.intersections[0].mObject;
-	  hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
-	  hitA.mNormal = ray.intersections[0].mNormal; 
-	  //	cout << "hita: " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
-	}
-  
-	mPreprocessor.SetupRay(ray, simpleRay.mOrigin, -simpleRay.mDirection);
-	ray.mFlags &= ~Ray::CULL_BACKFACES;
-  
-  if (castDoubleRay && mKdTree->CastRay(ray)) 
-		{
-			hitB.mObject = ray.intersections[0].mObject;
-			hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
-			hitB.mNormal = ray.intersections[0].mNormal;
-		}
+
+  
+  if (mKdTree->CastRay(ray)) {
+	hitA.mObject = ray.intersections[0].mObject;
+	hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
+	hitA.mNormal = ray.intersections[0].mNormal; 
+	//	cout << "hita: " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
+  }
+  
+  mPreprocessor.SetupRay(ray, simpleRay.mOrigin, -simpleRay.mDirection);
+  ray.mFlags &= ~Ray::CULL_BACKFACES;
+  
+  if (castDoubleRay && mKdTree->CastRay(ray)) {
+	hitB.mObject = ray.intersections[0].mObject;
+	hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
+	hitB.mNormal = ray.intersections[0].mNormal;
+  }
   
   return ProcessRay(
-										simpleRay,
-										hitA,
-										hitB,
-										vssRays,
-										box,
-										castDoubleRay,
-										pruneInvalidRays
-										);
+					simpleRay,
+					hitA,
+					hitB,
+					vssRays,
+					box,
+					castDoubleRay,
+					pruneInvalidRays
+					);
 }
 
