Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 2599)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 2602)
@@ -53,5 +53,5 @@
 	useGlDebugger false
 # 0 = INTERNAL 	1 = MLRT 2 = HAVRAN
-	rayCastMethod 1
+	rayCastMethod 2
 	
 #	type sampling
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.cpp	(revision 2599)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.cpp	(revision 2602)
@@ -92,16 +92,15 @@
 // We give a box to which each ray is clipped to before the
 // ray shooting is computed !
-void HavranRayCaster::CastRaysPacket4(const Vector3 &boxmax,
-									  const Vector3 &boxmin,
-									  const Vector3 origin4[],
-									  const Vector3 direction4[],
-									  int     result4[],
-									  float   dist4[])
-{
-#if defined(_USE_HAVRAN_SSE)
-#ifdef USE_HAVRAN_RAYCASTER 
+void HavranRayCaster::CastRaysPacket4(const Vector3 &minBox,
+				      const Vector3 &maxBox,
+				      const Vector3 origin4[],
+				      const Vector3 direction4[],
+				      int     result4[],
+				      float   dist4[])
+{
+#ifdef USE_HAVRAN_RAYCASTER 
+#ifdef _USE_HAVRAN_SSE  
   for (int i = 0; i < 4; i++) {
     result4[i] = -1;
-	//	raypack.SetObject(i, NULL);
     raypack.SetLoc(i, origin4[i]);
     raypack.SetDir(i, direction4[i]);
@@ -111,6 +110,5 @@
   // a particular ASDS, otherwise it is emulated by decomposition
   // of a packet to individual rays and traced individually.
-  mKtbtree->FindNearestI(raypack, (Vector3&)boxmin, (Vector3&)boxmax);
-  //  mKtbtree->FindNearestI(raypack, bmin, bmax);
+  mKtbtree->FindNearestI(raypack, minBox, maxBox);
 
   for (int i = 0; i < 4; i++) {
@@ -120,4 +118,5 @@
     if ( (intersectable = raypack.GetObject(i)) ) {
       // $$JB this is very hacky - > should be replaced with fetching the index of the triangle
+      // $$VH - this is object ID - is this the triangle index ???
       result4[i] = intersectable->mId;
       dist4[i] = raypack.GetT(i);
@@ -126,5 +125,4 @@
   
   return;
-#endif // USE_HAVRAN_RAYCASTER 
 #else // _USE_HAVRAN_SSE
   // Compute the result ray by ray
@@ -133,5 +131,5 @@
     sray.mOrigin = origin4[i];
     sray.mDirection = direction4[i];
-    mKtbtree->FindNearestI(sray, (Vector3&)boxmin, (Vector3&)boxmax);
+    mKtbtree->FindNearestI(sray, minBox, maxBox);
     if (SimpleRay::IntersectionRes[0].intersectable) {
       // This is object ID - is this the triangle index ???
@@ -145,4 +143,5 @@
   
 #endif // _USE_HAVRAN_SSE  
+#endif // USE_HAVRAN_RAYCASTER 
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 2599)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 2602)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: út 15. I 21:38:15 2008
+# Generated by qmake (2.00a) (Qt 4.1.2) on: st 16. I 21:20:04 2008
 # Project:  preprocessor.pro
 # Template: app
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktball.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktball.cpp	(revision 2592)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktball.cpp	(revision 2602)
@@ -402,5 +402,5 @@
 
 int
-CKTB::FindNearestI(const SimpleRay &ray, Vector3 &boxmin, Vector3 &boxmax)
+CKTB::FindNearestI(const SimpleRay &ray, const Vector3 &boxmin, const Vector3 &boxmax)
 {
   const AxisAlignedBox3 localbox(boxmin, boxmax);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktball.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktball.h	(revision 2592)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktball.h	(revision 2602)
@@ -174,5 +174,5 @@
   
   int FindNearestI(const SimpleRay &ray);
-  int FindNearestI(const SimpleRay &ray, Vector3 &boxmin, Vector3 &boxmax);
+  int FindNearestI(const SimpleRay &ray, const Vector3 &boxMin, const Vector3 &boxMax);
   void SetOffset(int offset) { traversalClass->SetOffset(offset); }
   int FindNearestI_16oneDir(SimpleRayContainer &rays, int offset, int copyOffset) {
@@ -192,9 +192,9 @@
   // of a packet to individual rays and traced individually.
   void FindNearestI(RayPacket2x2 &raypack);
-  void FindNearestI(RayPacket2x2 &raypack, Vector3 &boxmin, Vector3 &boxmax);
+  void FindNearestI(RayPacket2x2 &raypack, const Vector3 &boxMin, const Vector3 &boxMax);
 #endif
 #else
   void FindNearestI(RayPacket2x2 &raypack) { }
-  void FindNearestI(RayPacket2x2 &raypack, Vector3 &boxmin, Vector3 &boxmax) { }
+  void FindNearestI(RayPacket2x2 &raypack, const Vector3 &boxMin, const Vector3 &boxMax) { }
 #endif // __SSE__
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/configh.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/configh.h	(revision 2592)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/configh.h	(revision 2602)
@@ -59,5 +59,4 @@
 
 #ifdef _MSC_VER
-#define __SSE__
 // disable some Microsoft Visual Compiler warnings if necessary
 #pragma warning( disable:4305 )
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbtrav.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbtrav.h	(revision 2592)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbtrav.h	(revision 2602)
@@ -150,5 +150,5 @@
   // of a packet to individual rays and traced individually.
   virtual void FindNearestI(RayPacket2x2 &raypack) { }
-  virtual void FindNearestI(RayPacket2x2 &raypack, Vector3 &boxmin, Vector3 &boxmax) { }
+  virtual void FindNearestI(RayPacket2x2 &raypack, const Vector3 &boxMin, const Vector3 &boxMax) { }
 #endif
   
@@ -328,5 +328,5 @@
   // of a packet to individual rays and traced individually.
   virtual void FindNearestI(RayPacket2x2 &raypack);
-  virtual void FindNearestI(RayPacket2x2 &raypack, Vector3 &boxmin, Vector3 &boxmax);
+  virtual void FindNearestI(RayPacket2x2 &raypack, const Vector3 &boxMin, const Vector3 &boxMax);
 #endif // __SSE__
 #endif // _USE_HAVRAN_SSE  
@@ -421,5 +421,5 @@
   // of a packet to individual rays and traced individually.
   virtual void FindNearestI(RayPacket2x2 &raypack);
-  virtual void FindNearestI(RayPacket2x2 &raypack, Vector3 &boxmin, Vector3 &boxmax);
+  virtual void FindNearestI(RayPacket2x2 &raypack, const Vector3 &boxMin, const Vector3 &boxMax);
 #endif // __SSE__
 #endif // _USE_HAVRAN_SSE  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbftrav.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbftrav.cpp	(revision 2592)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbftrav.cpp	(revision 2602)
@@ -222,5 +222,5 @@
 // to be contained in the scene box!!!
 int
-CKTBTraversal::FindNearestI(const SimpleRay &ray, const AxisAlignedBox3 &localbox)
+CKTBTraversal::FindNearestI(const SimpleRay &oray, const AxisAlignedBox3 &localbox)
 {
 #if 0
@@ -238,4 +238,5 @@
   float tmin, tmax;
   SimpleRay::IntersectionRes[0].intersectable = 0;
+  SimpleRay ray(oray.mOrigin, oray.mDirection, 0, 0.f, 0);
   
   // test if the whole CKTB tree is missed by the input ray
@@ -246,4 +247,5 @@
     return 0; // no object can be intersected
   }
+  float tminOffset = 0.f;
 
 //#define _DEBUGKTB
@@ -267,4 +269,12 @@
   if (tmin < 0.f)
     tmin = 0.f;
+  else {
+    // Here the origin of the ray is always zero - it starts
+    // at the box face!!
+    tminOffset = tmin;
+    tmin = 0.f;
+    tmax -= tminOffset;
+    ray.mOrigin += ray.mDirection * tminOffset;
+  }
 
   int index = 1;
@@ -368,4 +378,7 @@
 	    _emptyLeavesTraversed += emptyLeavesTraversed;
 #endif // __TRAVERSAL_STATISTICS
+
+	    // We have to add the distance from the original ray origin
+	    SimpleRay::IntersectionRes[0].tdist += tminOffset;
 	    
 	    // signed distance should be already set in TestFullLeaf
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/testrt.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/testrt.cpp	(revision 2595)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/testrt.cpp	(revision 2602)
@@ -37,5 +37,5 @@
 #else
 	#if USE_THREADS
-		#include "BoostPreprocessorThread.h"
+#include "BoostPreprocessorThread.h"
 	#endif
 #endif
@@ -489,6 +489,6 @@
   int cntIntelOurDistMismatch = 0;
   bool printOut = false;
-  double sumDistIntel = 0.;
-  doublecca sumDistOurAlg = 0.;
+  double sumDistIntel = 0.f;
+  double sumDistOurAlg = 0.f;
   double sumDistAbsDiff = 0.f;
 #endif
@@ -536,5 +536,5 @@
       float T = rays[i].IntersectionRes[0].tdist;
       if ( (res != results[i].hitA) ||
-	   ((res)&&(results[i].hitA)&& (fabs(T - results[i].hitAT)) > 2.0f)
+	   ((res)&&(results[i].hitA)&& (fabs(T - results[i].hitAT)) > 0.1f)
 	   )
 	{
@@ -547,5 +547,5 @@
 	  }
 	  else {
-	    if ((res = 0) && (results[i].hitA)) {
+	    if ((res == 0) && (results[i].hitA)) {
 	      cntIntelYesWeNo++;
 	      if (printOut)
@@ -578,5 +578,4 @@
        << " userTime = " << timer.UserTime() << " realTime = "
        << timer.RealTime() << endl;
-    ;
   cout << "Rays shot per milisecond [userTimer] = "
        << ((double)cntRays * mult/(double)timer.UserTime()) / 1000.f << endl;
@@ -750,4 +749,5 @@
   AxisAlignedBox3 bboxOrig = preprocessor->mSceneGraph->GetBox();
 
+#undef DEBUGRESULTS
 //#define DEBUGRESULTS
 
@@ -758,5 +758,5 @@
   bool printOut = false;
   double sumDistIntel = 0.;
-  doublecca sumDistOurAlg = 0.;
+  double sumDistOurAlg = 0.;
   double sumDistAbsDiff = 0.f;
 #endif
@@ -856,2 +856,240 @@
   return;
 }
+
+// This is for testing RT implementation
+void
+TestRT_4_fromFile(int argc, char **argv)
+{
+  int returnCode = 0;
+
+  InitTiming();
+  Debug.open("debug.log");
+
+  Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
+  MeshKdTree::ParseEnvironment();
+
+  char buff[128];
+  Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
+  string preprocessorType(buff);
+
+  if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
+  {
+    Environment::DelSingleton();
+    cerr << "Unknown preprocessor type" << endl;
+    exit(1);
+  }
+
+
+  Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
+  string filename(buff);
+
+  const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
+  const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
+						".kdm" : ".kdt");
+
+  if (preprocessor->InitRayCast(externKdTree, internKdTree))
+  {
+    cout << "ray casting initialized!" << endl;
+  }
+  else
+  {
+    cout << "ray casting initialization failed!" << endl;
+    Cleanup();
+    exit(1);
+  }
+  
+  //Debug << "using pvs type " << PVS_TYPE << endl;
+
+  /////////////
+  //-- load scene 
+  
+  if (!preprocessor->LoadScene(filename))
+  {
+    cout << "loading file " << filename << " failed" << endl;
+    Cleanup();
+    exit(1);
+  }
+	
+  ////////////
+  //-- initialize external ray caster
+  
+  if (preprocessor->LoadInternKdTree(internKdTree))
+  {
+    cout << "intern kd tree loaded!" << endl;
+  }
+  else
+  {
+    cout << "loading intern kd tree failed!" << endl;
+    Cleanup();
+    exit(1);
+  }
+  
+  // export objects as obj
+  if (preprocessor->mExportObj)
+  {
+    if (strstr(filename.c_str(), ".obj"))
+    {
+      cerr << "already in obj format" << endl;
+      if (0) 	preprocessor->ExportObj("test.obj", preprocessor->mObjects);
+    }
+    else
+    {
+      const string objname = GetInternFilename(filename, ".obj");
+	
+      cout << "exporting scene to " << objname << endl;
+      bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);	
+	
+      if (success)
+	cout << "finished exporting obj" << endl;
+      else
+	cerr << "error exporting " << objname << endl;
+    }
+  }
+
+  Environment::GetSingleton()->GetStringValue("Rays.file", buff);
+  FILE *fp = fopen(buff, "rb");
+  if (!fp) {
+    cerr << "ERROR: file " << buff << " cannot be opened for reading" << endl;
+    cerr << "EXITING" << endl;
+    exit(3);
+  }
+  cout << "File " << buff << " was opened successfully" << endl;
+  int cntMaxRays = 100000;
+  Environment::GetSingleton()->GetIntValue("Rays.cnt", cntMaxRays);
+  vector<SimpleRay> rays;
+  SimpleRay rayTest;
+  vector<RESult> results;
+  vector<AxisAlignedBox3> boxes;
+
+  int cntRays = 0;
+  for (int i = 0; cntRays < cntMaxRays;) {
+    int ch = fgetc(fp);
+    switch (ch) {
+    case 'I': { // two-sided rays of 16
+      Vector3 minv, maxv;
+      if (fscanf(fp, "%f %f %f %f %f %f\n",
+		 &minv.x, &minv.y, &minv.z,
+		 &maxv.x, &maxv.y, &maxv.z) != 6) {
+	cerr << "Problem parsing the ray file" << endl;
+	cerr << "EXITING for ray order" << i << endl;
+	goto FINISH;
+      }
+      boxes.push_back(AxisAlignedBox3(minv, maxv));      
+
+      for (int j = 0; j < 4; j++) {
+	int order;
+	float ox, oy, oz, dx, dy, dz;
+	int hitA; float hitAT;
+	int hitB; float hitBT;	
+	if (fscanf(fp, "%d %f %f %f %f %f %f %d %f\n",
+		   &order, &ox, &oy, &oz, &dx, &dy, &dz,
+		   &hitA, &hitAT) != 9) {
+	  cerr << "Problem parsing the ray file" << endl;
+	  cerr << "EXITING for ray order" << order << endl;
+	  goto FINISH;
+	}
+	Vector3 mPosition(ox, oy, oz);
+	Vector3 mDirection(dx, dy, dz);
+	rayTest.Set(mPosition, mDirection, 0, 1.0, true);
+	rays.push_back(rayTest);
+	results.push_back(RESult(hitA, hitAT, 0, -1.f));
+	cntRays++;
+	i++;
+      }
+      break;
+    }
+    default: {
+      cerr << "Not yet implemented or end of file" << endl;
+      goto FINISH;
+    }
+    } // switch
+  } // for i
+
+FINISH:
+  
+  fclose(fp);
+  cout << "Starting to shoot " << cntRays << " rays" << endl;
+  
+  RayCaster *raycaster = preprocessor->mRayCaster;
+  VssRayContainer vssRays;
+  AxisAlignedBox3 bboxOrig = preprocessor->mSceneGraph->GetBox();
+
+//#define DEBUGRESULTS
+
+#ifdef DEBUGRESULTS
+  int cntIntelYesWeNo = 0;
+  int cntIntelNoWeYes = 0;
+  int cntIntelOurDistMismatch = 0;
+  bool printOut = false;
+  double sumDistIntel = 0.f;
+  double sumDistOurAlg = 0.f;
+  double sumDistAbsDiff = 0.f;
+#endif
+
+  //cout << "Press a key to start ray shooting" << endl;
+  //getchar();
+  cout << "Ray shooting " << cntRays << " rays started - "
+       << (castDoubleRays ? " double " : " single ")
+       << " dir " << endl;
+
+  long t1 = GetTime();
+  CTimer timer;
+  timer.Reset();
+  timer.Start();
+  Vector3 boxMin, boxMax;
+  
+  SimpleRayContainer raysToTest;
+  int boxI = 0;
+  for (int i = 0; i < cntRays - 4; i+= 4, boxI++) {
+    Vector3 origin4[4];
+    Vector3 direction4[4];
+    int     result4[4];
+    float   dist4[4];
+    boxMin = boxes[boxI].Min();
+    boxMax = boxes[boxI].Max();
+    for (int j = 0; j < 4; j++) {
+      int o = i+j;
+      origin4[j] = rays[o].mOrigin;
+      direction4[j] = rays[o].mDirection;
+    }
+    raycaster->CastRaysPacket4(boxMin, boxMax,
+			       origin4, direction4,
+			       result4, dist4);
+    printf("I %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f\n",
+	   boxMin.x, boxMin.y, boxMin.z, boxMax.x, boxMax.y, boxMax.z);
+
+    for (int j = 0; j < 4; j++) {
+      printf("%d %4.7f %4.7f %4.7f %4.7f %4.7f %4.7f %d %4.7f\n",
+	     i+j,
+	     origin4[j].x,
+	     origin4[j].y,
+	     origin4[j].z,
+	     direction4[j].x,
+	     direction4[j].y,
+	     direction4[j].z,
+	     (result4[j] != -1) ? 1 : 0,
+	     (result4[j] != -1) ? dist4[j] : 0);      
+    } // for j    
+  } // for i
+
+  timer.Stop();
+  long t2 = GetTime();
+  cout<<"\n#RAY_CAST_TIME = ";
+  cout << TimeDiff(t1, t2)<<" [mikrosec]"
+       << " userTime = " << timer.UserTime() << " realTime = "
+       << timer.RealTime() << endl;
+  cout << "Rays shot per milisecond [userTimer] = "
+       << ((double)cntRays/(double)timer.UserTime()) / 1000.f << endl;
+
+#ifdef DEBUGRESULTS  
+  cout << "cntIntelYesWeNo = " << cntIntelYesWeNo << endl;
+  cout << "cntIntelNoWeYes = " << cntIntelNoWeYes << endl;
+  cout << "cntIntelOurDistMismatch = " << cntIntelOurDistMismatch++ << endl;
+  cout << " sumDistIntel = " << sumDistIntel/(double)cntIntelOurDistMismatch << endl;
+  cout << " sumDistOur = " << sumDistOurAlg/(double)cntIntelOurDistMismatch << endl;
+  cout << " sumAbsDiffDist = " << sumDistAbsDiff/(double)cntIntelOurDistMismatch
+       << endl;
+#endif  
+  cout << "Done\n" << endl;
+  return;
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/testrt.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/testrt.h	(revision 2595)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/testrt.h	(revision 2602)
@@ -13,4 +13,7 @@
 TestRTfromFilePackets(int argc, char **argv);
 
+void
+TestRT_4_fromFile(int argc, char **argv);
+
 #endif // _TESTRT_H_
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbconf.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbconf.h	(revision 2592)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/havran/ktbconf.h	(revision 2602)
@@ -24,5 +24,5 @@
 
 // If we support the use of SSE instructions for ray shooting
-#define _USE_HAVRAN_SSE
+//#define _USE_HAVRAN_SSE
 
 namespace GtpVisibilityPreprocessor {
@@ -35,5 +35,5 @@
 #define GPREFETCH(A, B)  
 #endif
-  
+
 #if defined(__INTEL_COMPILER)||defined(_MSC_VER)
 // Intel compiler or Microsoft Compiler
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.h	(revision 2599)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.h	(revision 2602)
@@ -76,9 +76,9 @@
   // ray shooting is computed !
   virtual void CastRaysPacket4(const Vector3 &minBox,
-							   const Vector3 &maxBox,
-							   const Vector3 origin4[],
-							   const Vector3 direction4[],
-							   int     result4[],
-							   float   dist4[]);
+			       const Vector3 &maxBox,
+			       const Vector3 origin4[],
+			       const Vector3 direction4[],
+			       int     result4[],
+			       float   dist4[]);
 
 #ifdef _USE_HAVRAN_SSE  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 2592)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 2602)
@@ -121,5 +121,5 @@
 Intersectable.cpp TraversalTree.cpp ObjectPvs.cpp ObjectsParser.cpp \
 FilterBasedDistribution.cpp DifferenceSampling.cpp HavranRayCaster.cpp \
-OcclusionQuery.cpp 
+OcclusionQuery.cpp RandomViewCellsHandler.cpp
 
 
