Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 376)
@@ -103,5 +103,5 @@
 void
 X3dParseHandlers::StartIndexedFaceSet(
-				      AttributeList&  attributes)
+																			AttributeList&  attributes)
 {
   int len = attributes.getLength();
@@ -195,5 +195,5 @@
 void
 X3dParseHandlers::startElement(const XMLCh* const name,
-			       AttributeList&  attributes)
+															 AttributeList&  attributes)
 {
   StrX lname(name);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h	(revision 372)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h	(revision 376)
@@ -84,9 +84,9 @@
     if ( (value >=mMin[axis]) && (value <= mMax[axis]) )
       if (right)
-       mMin[axis] = value;
+				mMin[axis] = value;
       else
         mMax[axis] = value;
   }
-
+	
   // the size of the box along all the axes
   Vector3 Size() const { return mMax - mMin; }
@@ -174,6 +174,6 @@
     Vector3 size = Size();
     return mMin + Vector3(RandomValue(0, size.x),
-			  RandomValue(0, size.y),
-			  RandomValue(0, size.z)); 
+													RandomValue(0, size.y),
+													RandomValue(0, size.z)); 
   }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 376)
@@ -1150,4 +1150,14 @@
 		 "10");
 
+	RegisterOption("VssPreprocessor.totalSamples",
+								 optInt,
+								 "-total_samples=",
+								 "1000000");
+	
+  RegisterOption("VssPreprocessor.samplesPerPass",
+								 optInt,
+								 "-samples_per_pass=",
+								 "100000");
+
   RegisterOption("ViewCells.hierarchy",
 		 optString,
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 376)
@@ -542,25 +542,25 @@
 
       if (entp[axis] <= position) {
-	if (extp[axis] <= position) {
-	  node = in->mBack;
-	  // cases N1,N2,N3,P5,Z2,Z3
-	  continue;
-	} else {
-	  // case N4
-	  node = in->mBack;
-	  farChild = in->mFront;
-	}
+				if (extp[axis] <= 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[axis]) {
-	  node = in->mFront;
-	  // cases P1,P2,P3,N5,Z1
-	  continue;
-	} else {
-	  node = in->mFront;
-	  farChild = in->mBack;
-	  // case P4
-	}
-	  }
+				if (position <= extp[axis]) {
+					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
@@ -569,45 +569,43 @@
       extp = ray.GetLoc() + ray.GetDir()*tdist;
       maxt = tdist;
-	} else {
-		// compute intersection with all objects in this leaf
-		KdLeaf *leaf = (KdLeaf *) node;
-		if (ray.mFlags & Ray::STORE_KDLEAVES)
-			ray.kdLeaves.push_back(leaf);
-		
-		ObjectContainer::const_iterator mi;
-		for ( mi = leaf->mObjects.begin();
-					mi != leaf->mObjects.end();
-					mi++) {
-			Intersectable *object = *mi;
-			if (!object->Mailed() ) {
-				object->Mail();
-				if (ray.mFlags & Ray::STORE_TESTED_OBJECTS)
-					ray.testedObjects.push_back(object);
-				hits += object->CastRay(ray);
+		} else {
+			// compute intersection with all objects in this leaf
+			KdLeaf *leaf = (KdLeaf *) node;
+			if (ray.mFlags & Ray::STORE_KDLEAVES)
+				ray.kdLeaves.push_back(leaf);
+			
+			ObjectContainer::const_iterator mi;
+			for ( mi = leaf->mObjects.begin();
+						mi != leaf->mObjects.end();
+						mi++) {
+				Intersectable *object = *mi;
+				if (!object->Mailed() ) {
+					object->Mail();
+					if (ray.mFlags & Ray::STORE_TESTED_OBJECTS)
+						ray.testedObjects.push_back(object);
+					hits += object->CastRay(ray);
+				}
 			}
+			
+			if (hits && ray.GetType() == Ray::LOCAL_RAY)
+				if (ray.intersections[0].mT <= maxt)
+					break;
+			
+			// get the next node from the stack
+			if (tStack.empty())
+				break;
+			
+			entp = extp;
+			mint = maxt;
+			if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
+				break;
+			
+			RayTraversalData &s  = tStack.top();
+			node = s.mNode;
+			extp = s.mExitPoint;
+			maxt = s.mMaxT;
+			tStack.pop();
 		}
-		
-		if (hits && ray.GetType() == Ray::LOCAL_RAY)
-			if (ray.intersections[0].mT <= maxt)
-				break;
-		
-		// get the next node from the stack
-		if (tStack.empty())
-			break;
-		
-		entp = extp;
-		mint = maxt;
-		if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
-			break;
-		
-		RayTraversalData &s  = tStack.top();
-		node = s.mNode;
-		extp = s.mExitPoint;
-		maxt = s.mMaxT;
-		tStack.pop();
-	}
-  }
-  
-  
+  }
   return hits;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 372)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 376)
@@ -1,4 +1,4 @@
-#ifndef __SIMPLE_RAY_H
-#define __SIMPLE_RAY_H
+#ifndef __VSS_RAY_H
+#define __VSS_RAY_H
 
 #include <vector>
@@ -44,7 +44,7 @@
 	//////////////////////////////
 	VssRay(const Vector3 &origin,
-								const Vector3 &termination,
-								Intersectable *originObject,
-								Intersectable *terminationObject):
+				 const Vector3 &termination,
+				 Intersectable *originObject,
+				 Intersectable *terminationObject):
 		mMailbox(-1),
     mOrigin(origin),
@@ -60,7 +60,17 @@
   void Precompute() {
     mFlags = 0;
-    if (GetDir().x>0.0f) mFlags |= FPosDirX;
-    if (GetDir().y>0.0f) mFlags |= FPosDirY;
-    if (GetDir().z>0.0f) mFlags |= FPosDirZ;
+		Vector3 dir = GetDir();
+
+		if (dir.y < 0) {
+			// swap objects and poits	
+			swap(mOriginObject, mTerminationObject);
+			swap(mOrigin, mTermination);
+			dir = -dir;
+		}
+		
+		if (dir.x > 0.0f) mFlags |= FPosDirX;
+    if (dir.y > 0.0f) mFlags |= FPosDirY;
+    if (dir.z > 0.0f) mFlags |= FPosDirZ;
+
     mInvSize = 1.0f/Magnitude(GetDir());
   }
@@ -74,5 +84,11 @@
   }
 
-	
+	int HitCount() const {
+		if (mOriginObject && mTerminationObject)
+			return 2;
+		if (mOriginObject || mTerminationObject)
+			return 1;
+		return 0;
+	}
 	
 	Vector3 GetOrigin() const { return mOrigin; }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 376)
@@ -7,20 +7,19 @@
 #include "Polygon3.h"
 #include "ViewCell.h"
+#include "VssRay.h"
 
 VssPreprocessor::VssPreprocessor():
 	mPass(0),
-	mSampleRays(NULL)
+	mVssRays()
 {
   // this should increase coherence of the samples
-  environment->GetIntValue("Sampling.samplesPerPass", mSamplesPerPass);
-  environment->GetIntValue("Sampling.totalSamples", mTotalSamples);
-  mKdPvsDepth = 100;
+  environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
+  environment->GetIntValue("VssPreprocessor.totalSamples", mTotalSamples);
   mStats.open("stats.log");
-
 }
 
 VssPreprocessor::~VssPreprocessor()
 {
-	CLEAR_CONTAINER(mSampleRays);
+	CLEAR_CONTAINER(mVssRays);
 }
 
@@ -33,99 +32,80 @@
   ray.intersections.clear();
 	// do not store anything else then intersections at the ray
-	ray.kdLeaves.clear();
-	ray.mFlags = Ray::STORE_KDLEAVES;
-	
-  //  cout<<point<<" "<<direction<<endl;
   ray.Init(point, direction, Ray::LOCAL_RAY);
 }
 
-KdNode *
-VssPreprocessor::GetNodeForPvs(KdLeaf *leaf)
+VssRay * 
+VssPreprocessor::CastRay(
+												 Vector3 &viewPoint,
+												 Vector3 &direction
+												 )
 {
-  KdNode *node = leaf;
-  while (node->mParent && node->mDepth > mKdPvsDepth)
-    node = node->mParent;
-  return node;
+	static Ray ray;
+	AxisAlignedBox3 box = mKdTree->GetBox();
+	
+	SetupRay(ray, viewPoint, direction);
+	// cast ray to KD tree to find intersection with other objects
+	Intersectable *objectA, *objectB;
+	Vector3 pointA, pointB;
+
+	if (mKdTree->CastRay(ray)) {
+		objectA = ray.intersections[0].mObject;
+		pointA = ray.Extrap(ray.intersections[0].mT);
+	} else {
+		objectA = NULL;
+		// compute intersection with the scene bounding box
+		float tmin, tmax;
+		box.ComputeMinMaxT(ray, &tmin, &tmax);
+		pointA = ray.Extrap(tmax);
+	}
+	
+	SetupRay(ray, viewPoint, -direction);
+	
+	if (mKdTree->CastRay(ray)) {
+		objectB = ray.intersections[0].mObject;
+	  pointB = ray.Extrap(ray.intersections[0].mT);
+	} else {
+		objectB = NULL;
+		float tmin, tmax;
+		box.ComputeMinMaxT(ray, &tmin, &tmax);
+		pointB = ray.Extrap(tmax);
+	}
+
+	VssRay *vssRay  = NULL;
+	
+	if (objectA || objectB) {
+		vssRay = new VssRay(pointA,
+												pointB,
+												objectA,
+												objectB);
+	}
+	
+	return vssRay;
 }
 
 
-int
-VssPreprocessor::AddNodeSamples(const Ray &ray,
-																Intersectable *sObject,
-																Intersectable *tObject
-																)
+Vector3
+VssPreprocessor::GetViewpoint()
 {
-  int contributingSamples = 0;
-  int j;
-	int objects = 0;
-	if (sObject)
-		objects++;
-	if (tObject)
-		objects++;
+	AxisAlignedBox3 box = mKdTree->GetBox();
+
+	// shrink the box in the y direction
+	Vector3 diff(0, -box.Size().y*0.4f, 0);
+	box.Enlarge(diff);
 	
-	if (objects) {
-		for (j=0; j < ray.kdLeaves.size(); j++) {
-			KdNode *node = GetNodeForPvs( ray.kdLeaves[j] );
-			if (sObject)
-				contributingSamples += sObject->mKdPvs.AddSample(node);
-			if (tObject)
-				contributingSamples += tObject->mKdPvs.AddSample(node);
-		}
-	}
-	
-	for (j=1; j < ((int)ray.kdLeaves.size() - 1); j++) {
-		ray.kdLeaves[j]->AddPassingRay2(ray,
-																		objects,
-																		ray.kdLeaves.size()
-																		);
-  }
-  
-  return contributingSamples;
+	return box.GetRandomPoint();
+}
+
+Vector3
+VssPreprocessor::GetDirection(const Vector3 &viewpoint)
+{
+	int i = RandomValue(0, mObjects.size());
+	Intersectable *object = mObjects[i];
+	Vector3 point, normal;
+	object->GetRandomSurfacePoint(point, normal);
+	return point - viewpoint;
 }
 
 
-
-
-int
-VssPreprocessor::CastRay(Intersectable *object,
-												 Ray &ray
-												 )
-{
-	int sampleContributions = 0;
-
-	long t1 = GetRealTime();
-	// cast ray to KD tree to find intersection with other objects
-	mKdTree->CastRay(ray);
-	long t2 = GetRealTime();
-
-	if (0 && object && object->GetId() > 2197) {
-		object->Describe(cout)<<endl;
-		cout<<ray<<endl;
-	}
-
-	if (ray.kdLeaves.size()) {
-		Intersectable *terminator =
-			ray.intersections.size() ? ray.intersections[0].mObject: NULL;
-		
-		sampleContributions += AddNodeSamples(ray,
-																					object,
-																					terminator);
-	}
-	
-	
-	return sampleContributions;
-}
-
-
-KdNode *
-VssPreprocessor::GetNodeToSample(Intersectable *object)
-{
-	int pvsSize = object->mKdPvs.GetSize();
-	KdNode *nodeToSample = NULL;
-	
-	// just pickup a random node
-	nodeToSample = mKdTree->GetRandomLeaf();
-	return nodeToSample;
-}
 
 bool
@@ -133,11 +113,6 @@
 {
   
-  // pickup an object
-  ObjectContainer objects;
-  
-  mSceneGraph->CollectObjects(&objects);
-
-  Vector3 point, normal, direction;
-  Ray ray;
+  mSceneGraph->CollectObjects(&mObjects);
+	
 
   long startTime = GetTime();
@@ -146,7 +121,4 @@
   int totalSamples = 0;
 
-  int pvsOut = Min((int)objects.size(), 10);
-
-  vector<Ray> rays[10];
 
   while (totalSamples < mTotalSamples) {
@@ -156,111 +128,41 @@
 		int index = 0;
 		
-		int reverseSamples = 0;
+		int sampleContributions;
 		
+		
+		for (int k=0; k < mSamplesPerPass; k++) {
+
+			Vector3 viewpoint = GetViewpoint();
+			Vector3 direction = GetDirection(viewpoint);
+
+			VssRay *vssRay = CastRay(viewpoint, direction);
 			
-		//cout << "totalSamples: "  << totalSamples << endl;
-
-		for (i = 0; i < objects.size(); i++) {
-						
-			KdNode *nodeToSample = NULL;
-			Intersectable *object = objects[i];
-		
-			int pvsSize = 0;
-			if (ViewCell::sHierarchy == ViewCell::KD)
-				pvsSize = object->mKdPvs.GetSize();
-						
+			if (vssRay) {
+				sampleContributions = vssRay->HitCount();
+				mVssRays.push_back(vssRay);
+			}
 			
-			
-			int faceIndex = object->GetRandomSurfacePoint(point, normal);
-			
-			bool viewcellSample = true;
-			int sampleContributions;
-			bool debug = false; //(object->GetId() >= 2199);
-			if (viewcellSample) {
-				//mKdTree->GetRandomLeaf(Plane3(normal, point));
-
-				nodeToSample = GetNodeToSample(object);
-
-				for (int k=0; k < mSamplesPerPass; k++) {
-					bool reverseSample = false;
-					
-
-					if (nodeToSample) {
-						AxisAlignedBox3 box = mKdTree->GetBox(nodeToSample);
-						Vector3 pointToSample = box.GetRandomPoint();
-						//						pointToSample.y = 0.9*box.Min().y + 0.1*box.Max().y;
-						if (object->GetRandomVisibleSurfacePoint( point, normal, pointToSample, 3 )) {
-							direction = pointToSample - point;
-						} else {
-							reverseSamples++;
-							reverseSample = true;
-							direction = point - pointToSample;
-							point = pointToSample;
-						}
-					}
-					else {
-						direction = UniformRandomVector(normal);
-					}
-					
-					// construct a ray
-					SetupRay(ray, point, direction);
-					
-					sampleContributions = CastRay(reverseSample ? NULL : object, ray);
-					
-					//-- CORR matt: put block inside loop
-					if (sampleContributions) {
-						passContributingSamples ++;
-						passSampleContributions += sampleContributions;
-					}
-
-					if ( i < pvsOut )
-						rays[i].push_back(ray);
-		
-					if (!ray.intersections.empty()) {
-						// check whether we can add this to the rays
-						for (int j = 0; j < pvsOut; j++) {
-							if (objects[j] == ray.intersections[0].mObject) {
-								rays[j].push_back(ray);
-							}
-						}
-					}
-					//-------------------
-				}
-			} else {
-				// edge samples
-				// get random visible mesh
-				//				object->GetRandomVisibleMesh(Plane3(normal, point));
+			//-- CORR matt: put block inside loop
+			if (sampleContributions) {
+				passContributingSamples ++;
+				passSampleContributions += sampleContributions;
 			}
-	
-			// CORR matt: must add all samples
-			passSamples += mSamplesPerPass;
+			passSamples++;
+			totalSamples++;
 		}
-	
-		totalSamples += passSamples; 
-		
-		//    if (pass>10)
-		//      HoleSamplingPass();
     
 		mPass++;
 
 		int pvsSize = 0;
-        
-		for (i=0; i < objects.size(); i++) {
-			Intersectable *object = objects[i];
-			pvsSize += object->mKdPvs.GetSize();
-		}
-		
-		
 		float avgRayContrib = (passContributingSamples > 0) ? 
 			passSampleContributions/(float)passContributingSamples : 0;
-
+		
 		cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
 		cout << "#TotalSamples=" << totalSamples/1000 
 				 << "k   #SampleContributions=" << passSampleContributions << " (" 
 				 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
-				 << pvsSize/(float)objects.size() << endl 
-				 << "avg ray contrib=" << avgRayContrib << endl
-				 << "reverse samples [%]" << reverseSamples/(float)passSamples*100.0f << endl;
-
+				 << pvsSize/(float)mObjects.size() << endl 
+				 << "avg ray contrib=" << avgRayContrib << endl;
+		
 		mStats <<
 			"#Pass\n" <<mPass<<endl<<
@@ -269,73 +171,11 @@
 			"#SampleContributions\n" << passSampleContributions << endl << 
 			"#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
-			"#AvgPVS\n"<< pvsSize/(float)objects.size() << endl <<
+			"#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
 			"#AvgRayContrib\n" << avgRayContrib << endl;
 	}
 	
 	cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
-  
-	
-	if (0) {
-		Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); 
-		exporter->SetExportRayDensity(true);
-		exporter->ExportKdTree(*mKdTree);
+	cout << "#totalRayStackSize=" << mVssRays.size() << endl;
 
-		delete exporter;
-	}
-  
-	bool exportRays = false;
-	if (exportRays) {
-		Exporter *exporter = NULL;
-		exporter = Exporter::GetExporter("sample-rays.x3d");
-		exporter->SetWireframe();
-		exporter->ExportKdTree(*mKdTree);
-
-		for (i=0; i < pvsOut; i++) 
-		exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0));
-		exporter->SetFilled();
-		  
-		delete exporter;
-	}
-
-	//-- several visualizations and statistics
-	if (1) {
-		for (int k=0; k < pvsOut; k++) {
-			Intersectable *object = objects[k];
-			char s[64];
-			sprintf(s, "sample-pvs%04d.x3d", k);
-			Exporter *exporter = Exporter::GetExporter(s);
-			exporter->SetWireframe();
-			
-			
-			KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
-			Intersectable::NewMail();
-			
-			// avoid adding the object to the list
-			object->Mail();
-			ObjectContainer visibleObjects;
-			
-			for (; i != object->mKdPvs.mEntries.end(); i++) 
-				{
-					KdNode *node = (*i).first;
-					exporter->ExportBox(mKdTree->GetBox(node));
-					mKdTree->CollectObjects(node, visibleObjects);
-				}
-			
-			exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
-			exporter->SetFilled();
-			
-			for (int j = 0; j < visibleObjects.size(); j++)
-				exporter->ExportIntersectable(visibleObjects[j]);
-			
-			
-			Material m;
-			m.mDiffuseColor = RgbColor(1, 0, 0);
-			exporter->SetForcedMaterial(m);
-			exporter->ExportIntersectable(object);
-			
-			delete exporter;
-		}
-  }
-  
   return true;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 376)
@@ -8,9 +8,9 @@
 #	filename glasgow1.x3d
 #	filename vienna.x3d
-#	filename ../data/vienna/vienna-simple.x3d
-#	filename ../data/vienna/vienna-buildings.x3d
+# filename ../data/vienna/vienna-simple.x3d
+filename ../data/vienna/vienna-buildings.x3d
 # filename ../data/vienna/viewcells-25-sel.x3d
 # filename ../data/atlanta/atlanta2.x3d
-	filename ../data/soda/soda.dat
+#	filename ../data/soda/soda.dat
 # filename ../data/soda/soda5.dat
 }
@@ -19,4 +19,9 @@
 #	type sampling
 	type vss
+}
+
+VssPreprocessor {
+	totalSamples 1000000
+	samplesPerPass  100000
 }
 
@@ -35,5 +40,5 @@
 	sahUseFaces true
 	Termination {
-		minCost 2
+		minCost 1
 		maxDepth 18
 		maxCostRatio 0.9
@@ -48,5 +53,5 @@
 MeshKdTree {
 	Termination {
-		minCost 4
+		minCost 1
 		maxDepth 18
 		maxCostRatio 0.9
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 376)
@@ -6,5 +6,5 @@
 
 #include "Preprocessor.h"
-
+#include "VssRay.h"
 
 /** Sampling based visibility preprocessing. The implementation is based on heuristical
@@ -13,18 +13,24 @@
 public:
 	int mPass;
-
   int mSamplesPerPass;
   int mTotalSamples;
-  int mKdPvsDepth;
   ofstream mStats;
+	
   ObjectContainer mObjects;
-  RayContainer mSampleRays;
+
+	// rays cast during the processing
+  VssRayContainer mVssRays;
 	
   VssPreprocessor();
-
   ~VssPreprocessor();
 
   virtual bool ComputeVisibility();
 
+	Vector3
+	GetViewpoint();
+
+	Vector3
+	GetDirection(const Vector3 &viewpoint);
+	
   void
   SetupRay(Ray &ray, 
@@ -33,20 +39,14 @@
 					 );
 
-  KdNode *
-  GetNodeForPvs(KdLeaf *leaf);
 
-	int
-	AddNodeSamples(const Ray &ray,
-								 Intersectable *sObject,
-								 Intersectable *tObject
-								 );
+	
+	VssRay * 
+	CastRay(
+					Vector3 &viewPoint,
+					Vector3 &direction
+					);
+		
 
-	int
-	CastRay(Intersectable *object,
-					Ray &ray);
 	
-	KdNode *
-	GetNodeToSample(Intersectable *object);
-
 	virtual bool BuildBspTree() { return false; }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/MeshKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/MeshKdTree.cpp	(revision 308)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/MeshKdTree.cpp	(revision 376)
@@ -291,7 +291,7 @@
 int
 MeshKdTree::CastRay(
-		    Ray &ray,
-		    MeshInstance *instance
-		    )
+										Ray &ray,
+										MeshInstance *instance
+										)
 {
   int hits = 0;
@@ -308,5 +308,11 @@
   if (mint < 0)
     mint = 0;
-  
+
+
+	if (ray.GetType() == Ray::LOCAL_RAY &&
+			ray.intersections.size() && ray.intersections[0].mT < mint) {
+		return 0;
+	}
+
   maxt += Limits::Threshold;
   
@@ -324,26 +330,26 @@
       position = in->mPosition;
       axis = in->mAxis;
-
+			
       if (entp[axis] <= position) {
-	if (extp[axis] <= position) {
-	  node = in->mBack;
-	  // cases N1,N2,N3,P5,Z2,Z3
-	  continue;
-	} else {
-	  // case N4
-	  node = in->mBack;
-	  farChild = in->mFront;
-	}
+				if (extp[axis] <= 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[axis]) {
-	  node = in->mFront;
-	  // cases P1,P2,P3,N5,Z1
-	  continue;
-	} else {
-	  node = in->mFront;
-	  farChild = in->mBack;
-	  // case P4
-	}
+				if (position <= extp[axis]) {
+					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
@@ -359,11 +365,12 @@
       hits += instance->CastRay(ray, leaf->mFaces);
       
-      if (hits && ray.GetType() == Ray::LOCAL_RAY)
-	if (ray.intersections[0].mT <= maxt)
-	  break;
+      if (ray.GetType() == Ray::LOCAL_RAY && ray.intersections.size())
+				if (ray.intersections[0].mT <= maxt) {
+					break;
+				}
       
       // get the next node from the stack
       if (tStack.empty())
-	break;
+				break;
       
       entp = extp;
@@ -371,6 +378,6 @@
 
       if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
-	break;
-
+				break;
+			
       RayTraversalData &s  = tStack.top();
       node = s.mNode;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 359)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 376)
@@ -100,4 +100,5 @@
     nearestT = ray.intersections[0].mT;
 
+	
   for ( ;
 				faceIndex < mFaces.size();
