Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2617)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2618)
@@ -2587,30 +2587,29 @@
 	ViewCell::NewMail();
 
-#ifdef USE_PERFTIMER  
-	viewCellCastTimer.Entry();
-#endif
 	static ViewCellContainer viewCells;
 	static VssRay *lastVssRay = NULL;
 
 	// check if last ray was not same ray with reverse direction
-	if (1) 
+	//	if (1) 
 		// tmp matt: don't use when origin objects are not accounted for, currently the second ray is lost!!
 	  // $$JB: reenabled again - should use the same viewcells for the next ray ray if
 	  // it goes in the oposite direction
-// 	if (!lastVssRay ||
-// 		!(ray.mOrigin == lastVssRay->mTermination) ||
-// 		!(ray.mTermination == lastVssRay->mOrigin)) 
+	if (!lastVssRay ||
+		!(ray.mOrigin == lastVssRay->mTermination) ||
+		!(ray.mTermination == lastVssRay->mOrigin)) 
 	  {
+#ifdef USE_PERFTIMER  
+		viewCellCastTimer.Entry();
+#endif
 		viewCells.clear();
-
+		
 		// traverse the view space subdivision
 		CastLineSegment(origin, termination, viewCells);
 		lastVssRay = &ray;
-	  } 
-
 #ifdef USE_PERFTIMER  
-	viewCellCastTimer.Exit();
+		viewCellCastTimer.Exit();
 #endif
-
+	  }
+	
 	mSamplesStat.mViewCells += (int)viewCells.size();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2615)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2618)
@@ -762,5 +762,5 @@
 }
 
-int KdTree::CastLineSegment(const Vector3 &origin,
+int KdTree::CastLineSegment2(const Vector3 &origin,
 			    const Vector3 &termination,
 			    ViewCellContainer &viewcells)
@@ -850,4 +850,192 @@
       if (tStack.empty())
 	break;
+      
+      entp = extp;
+      mint = maxt;
+      
+      RayTraversalData &s  = tStack.top();
+      node = s.mNode;
+      extp = s.mExitPoint;
+      maxt = s.mMaxT;
+      tStack.pop();
+    }
+  }
+  return hits;
+}
+
+int KdTree::CastLineSegment(const Vector3 &origin,
+			    const Vector3 &termination,
+			    ViewCellContainer &viewcells)
+{
+  int hits = 0;
+
+  float mint = 0.0f, maxt = 1.0f;
+  const Vector3 dir = termination - origin;
+  
+  stack<RayTraversalData> tStack;
+  
+  Intersectable::NewMail();
+  
+  //maxt += Limits::Threshold;
+  
+  Vector3 entp = origin;
+  Vector3 extp = termination;
+  
+  KdNode *node = mRoot;
+  KdNode *farChild;
+  
+  float position;
+  float tdist;
+
+  while (1)
+  {
+    if (!node->IsLeaf())
+    {
+      KdInterior *in = static_cast<KdInterior *>(node);
+      position = in->mPosition;
+      switch (in->mAxis) {
+	  case 0:
+		if (entp.x <= position)
+		  {
+			if (extp.x <= position)
+			  {
+				node = in->mBack;
+				// cases N1,N2,N3,P5,Z2,Z3
+				continue;
+			  } 
+			else
+			  {
+				// case N4
+				node = in->mBack;
+				farChild = in->mFront;
+			  }
+		  }
+		else
+		  {
+			if (position <= extp.x)
+			  {
+				node = in->mFront;
+				// cases P1,P2,P3,N5,Z1
+				continue;
+			  }
+			else
+			  {
+				node = in->mFront;
+				farChild = in->mBack;
+				// case P4
+			  }
+		  }
+		
+		// $$ modification 3.5.2004 - hints from Kamil Ghais
+		// case N4 or P4
+		tdist = (position - origin.x) / dir.x;
+		//tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
+		extp = origin + dir * tdist;
+		maxt = tdist;
+	  
+	  break;
+	case 1:
+	
+		if (entp.y <= position)
+		  {
+			if (extp.y <= position)
+			  {
+				node = in->mBack;
+				// cases N1,N2,N3,P5,Z2,Z3
+				continue;
+			  } 
+			else
+			  {
+				// case N4
+				node = in->mBack;
+				farChild = in->mFront;
+			  }
+		  }
+		else
+		  {
+			if (position <= extp.y)
+			  {
+				node = in->mFront;
+				// cases P1,P2,P3,N5,Z1
+				continue;
+			  }
+			else
+			  {
+				node = in->mFront;
+				farChild = in->mBack;
+				// case P4
+			  }
+		  }
+		
+		// $$ modification 3.5.2004 - hints from Kamil Ghais
+		// case N4 or P4
+		tdist = (position - origin.y) / dir.y;
+		//tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
+		extp = origin + dir * tdist;
+		maxt = tdist;
+		
+		break;
+	  case 2: 
+		if (entp.z <= position)
+		  {
+			if (extp.z <= position)
+			  {
+				node = in->mBack;
+				// cases N1,N2,N3,P5,Z2,Z3
+				continue;
+			  } 
+			else
+			  {
+				// case N4
+				node = in->mBack;
+				farChild = in->mFront;
+			  }
+		  }
+		else
+		  {
+			if (position <= extp.z)
+			  {
+				node = in->mFront;
+				// cases P1,P2,P3,N5,Z1
+				continue;
+			  }
+			else
+			  {
+				node = in->mFront;
+				farChild = in->mBack;
+				// case P4
+			  }
+		  }
+		
+		// $$ modification 3.5.2004 - hints from Kamil Ghais
+		// case N4 or P4
+		tdist = (position - origin.z) / dir.z;
+		//tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
+		extp = origin + dir * tdist;
+		maxt = tdist;
+	  
+	  break;
+	  
+	  }
+	  
+	}
+    else
+	  {
+      // compute intersection with all objects in this leaf
+      KdLeaf *leaf = static_cast<KdLeaf *>(node);
+      
+      // add view cell to intersections
+      ViewCell *vc = leaf->mViewCell;
+      
+      if (!vc->Mailed())
+      {
+	vc->Mail();
+	viewcells.push_back(vc);
+	++ hits;
+      }
+      
+      // get the next node from the stack
+      if (tStack.empty())
+		break;
       
       entp = extp;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2606)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2618)
@@ -332,4 +332,8 @@
 		      vector<ViewCell *> &viewcells);
 
+  // old version
+  int CastLineSegment2(const Vector3 &origin,
+					   const Vector3 &termination,
+					   vector<ViewCell *> &viewcells);
 
   const KdTreeStatistics &GetStatistics() const {
