Index: trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 531)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 534)
@@ -15,5 +15,5 @@
   // "back plane"
   mPlanes.push_back(Plane3(-VssRay::GetDirection(dCenter.x, dCenter.y), center));
-
+  
   Vector3 directions[4];
   directions[0] = VssRay::GetDirection(dBox.Min().x, dBox.Min().y);
@@ -23,8 +23,8 @@
   
   // side planes
-  mPlanes.push_back(Plane3(CrossProd(directions[0], directions[1]), center));
-  mPlanes.push_back(Plane3(CrossProd(directions[1], directions[2]), center));
-  mPlanes.push_back(Plane3(CrossProd(directions[2], directions[3]), center));
-  mPlanes.push_back(Plane3(CrossProd(directions[3], directions[0]), center));
+  mPlanes.push_back(Plane3(-CrossProd(directions[0], directions[1]), center));
+  mPlanes.push_back(Plane3(-CrossProd(directions[1], directions[2]), center));
+  mPlanes.push_back(Plane3(-CrossProd(directions[2], directions[3]), center));
+  mPlanes.push_back(Plane3(-CrossProd(directions[3], directions[0]), center));
 
 
@@ -122,2 +122,3 @@
 
 }
+
Index: trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h	(revision 534)
@@ -185,7 +185,7 @@
   Vector3 GetRandomPoint() const {
     Vector3 size = Size();
-    return mMin + Vector3(RandomValue(0, size.x),
-													RandomValue(0, size.y),
-													RandomValue(0, size.z)); 
+    return mMin + Vector3(RandomValue(0.0f, size.x),
+						  RandomValue(0.0f, size.y),
+						  RandomValue(0.0f, size.z)); 
   }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 475)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 534)
@@ -71,7 +71,7 @@
   vector<Intersectable *> testedObjects;
 
-	// various flags
-	enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4};
-	int mFlags;
+  // various flags
+  enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4, CULL_BACKFACES=8};
+  int mFlags;
 
 	
@@ -79,5 +79,5 @@
   Ray(const Vector3 &wherefrom,
       const Vector3 &whichdir,
-      const int _type):mFlags(0) {
+      const int _type):mFlags(CULL_BACKFACES) {
     loc = wherefrom;
     if (_type == LINE_SEGMENT)
@@ -96,4 +96,5 @@
   Ray(const VssRay &vssRay) {
 	Init(vssRay);
+	mFlags |= CULL_BACKFACES;
   }
 
@@ -301,8 +302,10 @@
 struct SimpleRay
 {
-	Vector3 mOrigin;
-	Vector3 mDirection;
-	SimpleRay() {}
-	SimpleRay(const Vector3 &o, const Vector3 &d):mOrigin(o), mDirection(d) {}
+  Vector3 mOrigin;
+  Vector3 mDirection;
+  float mProbability;
+
+  SimpleRay() {}
+  SimpleRay(const Vector3 &o, const Vector3 &d):mOrigin(o), mDirection(d) {}
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 534)
@@ -1434,4 +1434,10 @@
 					"false");
 
+	RegisterOption("Preprocessor.detectEmptyViewSpace",
+				   optBool,
+				   "detectEmptyViewSpace",
+				   "false");
+	
+
 	/**************************************************************************************/
 	/*                  View space partition KD tree related options                      */
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 534)
@@ -247,4 +247,5 @@
 	mViewCellsManager->SetRenderer(mRenderSimulator);
 
+	
 	//-- parse view cells construction method
 	environment->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
@@ -257,5 +258,10 @@
 	if (mUseGlRenderer)
 	  renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
-	
+
+
+	environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
+	
+
+
 	return true;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 516)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 534)
@@ -2041,37 +2041,16 @@
   
   int nrays = leaf->rays.size();
-
-
+  int startIndex = rays.size();
+  
   AxisAlignedBox3 box = GetBBox(leaf);
   AxisAlignedBox3 dirBox = GetDirBBox(leaf);
 
 
-  AxisAlignedBox3 sBox(box);
-  AxisAlignedBox3 sDirBox(dirBox);
-  const float smoothRange = 0.5f;
-  sBox.Scale(1.0f + smoothRange);
-  sDirBox.Scale(1.0f + smoothRange);
-  int smoothRays = (int)numberOfRays*0.0f;
-  
-#if AVG_LEN
-  float avgLength = 0.0f;
-
-  if (nrays) {
-	const int numAvg = 5;
-	
-	int step = (nrays-1)/10;
-	if (step<1)
-	  step = 1;
-	
-	int summed = 0;
-	float sumLength = 0.0f;
-	for (int i=0; i < nrays; i+=step) {
-	  sumLength += leaf->rays[i].mRay->GetSize();
-	  summed++;
-	}
-  
-	avgLength = sumLength/summed;
-  }
-#endif
+  const float smoothRange = 0.0f;
+  if (smoothRange != 0.0f) {
+	box.Scale(1.0f + smoothRange);
+	dirBox.Scale(1.0f + smoothRange);
+  }
+  
   
   Exporter *exporter = NULL;
@@ -2149,4 +2128,5 @@
 						  leaf->halton.GetNumber(5),
 						  0.0f);
+		
 		leaf->halton.GenerateNext();
 	  } else {
@@ -2160,11 +2140,7 @@
 	  }
 	  
-	  if (i < smoothRays) {
-		origin = sBox.GetPoint(pVector);
-		dirVector = sDirBox.GetPoint(dVector);
-	  } else {
-		origin = box.GetPoint(pVector);
-		dirVector = dirBox.GetPoint(dVector);
-	  }
+	  origin = box.GetPoint(pVector);
+	  dirVector = dirBox.GetPoint(dVector);
+
 	  direction = Vector3(sin(dirVector.x), sin(dirVector.y), cos(dirVector.x));
 	}
@@ -2216,6 +2192,10 @@
   }
 
+  
   //  cout<<"desired="<<numberOfRays<<" tries="<<i<<endl;
-  
+  // assign probabilitites to the generated rays
+  for (i=startIndex; i < rays.size(); i++)
+	rays[i].mProbability = 1.0f;
+	  
   if (exporter) {
 	exporter->ExportRays(selectedRays, RgbColor(1, 0, 0));
@@ -2223,4 +2203,6 @@
 	CLEAR_CONTAINER(selectedRays);
   }
+  
+  
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 534)
@@ -63,4 +63,7 @@
   // computed by the prperocessor
   float mWeightedPvsContribution;
+
+  // probability of this ray
+  float mProbability;
   
   //////////////////////////////
@@ -81,6 +84,6 @@
 	mPass(pass),
 	mViewCells(0),
-	mWeightedPvsContribution(0)
-	//	mT(1.0f)
+	mWeightedPvsContribution(0),
+	mProbability(1.0f)
   {
     Precompute();
@@ -93,6 +96,6 @@
 	mOriginObject(ray.sourceObject.mObject),
 	mPass(0),
-	mViewCells(0)
-	//	mT(1.0f)
+	mViewCells(0),
+	mProbability(1.0f)
   {
 	if (ray.sourceObject.mObject)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 527)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 534)
@@ -607,5 +607,6 @@
 	if (mUseViewcells) {
 	  // construct view cells
-	  mViewCellsManager->Construct(mObjects, mVssRays);
+	  if (!mLoadViewCells)
+		mViewCellsManager->Construct(mObjects, mVssRays);
 	  // evaluate contributions of the intitial rays
 	  mViewCellsManager->ComputeSampleContributions(mVssRays);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 534)
@@ -15,5 +15,5 @@
 
 bool use2dSampling = false;
-bool useViewspacePlane = true;
+bool useViewspacePlane = false;
 
 VssPreprocessor::VssPreprocessor():
@@ -78,4 +78,7 @@
   float bsize = Magnitude(box.Size());
 
+
+  if (!mDetectEmptyViewSpace)
+	ray.mFlags &= ~Ray::CULL_BACKFACES;
   
   if (mKdTree->CastRay(ray)) {
@@ -92,12 +95,13 @@
   }
 
-  bool detectEmptyViewSpace = true;
 	
-  if (detectEmptyViewSpace) {
+  if (mDetectEmptyViewSpace) {
 	SetupRay(ray, pointA, -direction);
   } else
 	SetupRay(ray, viewPoint, -direction);
-	
-	
+  
+  if (!mDetectEmptyViewSpace)
+	ray.mFlags &= ~Ray::CULL_BACKFACES;
+
   if (mKdTree->CastRay(ray)) {
 	objectB = ray.intersections[0].mObject;
@@ -111,7 +115,7 @@
 	  return 0;
   }
-
+  
   //  if (objectA == NULL && objectB != NULL) {
-  if (1) {
+  if (mDetectEmptyViewSpace) {
 	// cast again to ensure that there is no objectA
 	SetupRay(ray, pointB, direction);
@@ -121,10 +125,10 @@
 	}
   }
-  
+
   
   VssRay *vssRay  = NULL;
 
   bool validSample = (objectA != objectB);
-  if (0 && detectEmptyViewSpace) {   // consider all samples valid
+  if (0 && mDetectEmptyViewSpace) {   // consider all samples valid
 	// check if the viewpoint lies on the line segment AB
 	if (Distance(pointA, pointB) <
@@ -183,8 +187,13 @@
   Vector3 point;
   if (!use2dSampling) {
-	Vector3 normal;
-	int i = (int)RandomValue(0, (Real)((int)mObjects.size()-1));
-	Intersectable *object = mObjects[i];
-	object->GetRandomSurfacePoint(point, normal);
+	if (0) {
+	  Vector3 normal;
+	  int i = Random((int)mObjects.size());
+	  Intersectable *object = mObjects[i];
+	  object->GetRandomSurfacePoint(point, normal);
+	} else
+	  point = mKdTree->GetBox().GetRandomPoint();
+	//	  point = viewpoint + UniformRandomVector();
+
   } else {
 	AxisAlignedBox3 box;
@@ -226,5 +235,5 @@
 	num = vssTree->GenerateRays(p, rays);
   } else {
-	int leaves = vssTree->stat.Leaves()/2;
+	int leaves = vssTree->stat.Leaves();
 	num = vssTree->GenerateRays(desiredSamples, leaves, rays);
   }
@@ -243,7 +252,4 @@
 {
   cout<<"Exporting vss rays..."<<endl<<flush;
-
-  float prob = number/(float)vssRays.size();
-
 
   Exporter *exporter = NULL;
@@ -261,8 +267,7 @@
   }
 
-  VssRayContainer rays;	for (int i=0; i < vssRays.size(); i++)
-	if (RandomValue(0,1) < prob)
-	  rays.push_back(vssRays[i]);
-
+  VssRayContainer rays;
+  vssRays.SelectRays(number, rays);
+  
   exporter->ExportRays(rays, RgbColor(1, 0, 0));
 
@@ -411,5 +416,5 @@
 
   AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
-
+  
   if (!useViewspacePlane) {
 	float size = 0.05f;
@@ -430,4 +435,5 @@
 	box->SetMax(1, box->Min(1));
 
+  cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
   if (mUseViewSpaceBox)
   {
@@ -440,5 +446,5 @@
 	mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
   }
-
+  
   //-- load view cells from file if requested
   if (mLoadViewCells)
@@ -464,5 +470,6 @@
   else
   {
-	  while (totalSamples < mInitialSamples) {
+	
+	while (totalSamples < mInitialSamples) {
 		int passContributingSamples = 0;
 		int passSampleContributions = 0;
@@ -476,9 +483,9 @@
 		for (int k=0; k < s; k++) {
 			// changed by matt
-			//Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
 			Vector3 viewpoint; 
+			//			viewpoint = GetViewpoint(mViewSpaceBox);
 			mViewCellsManager->GetViewPoint(viewpoint);
 			Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
-	        
+
 			sampleContributions = CastRay(viewpoint, direction, mVssRays);
 
@@ -514,4 +521,7 @@
  
 	  cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
+
+
+	  
   }
   
@@ -537,6 +547,7 @@
   }
 
-  //int numExportRays = 10000;
-  int numExportRays = 0;
+  
+  int numExportRays = 5000;
+  //int numExportRays = 0;
 
   if (numExportRays) {
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 529)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 534)
@@ -6,5 +6,5 @@
 Scene {
 
-#	filename glasgow1.x3d
+# filename ../data/glasgow/glasgow1.x3d
 #	filename vienna.x3d
 # filename ../data/vienna/vienna-simple.x3d
@@ -15,5 +15,5 @@
 filename ../data/atlanta/atlanta2.x3d
 # filename ../data/soda/soda.dat
-filename ../data/soda/soda5.dat
+# filename ../data/soda/soda5.dat
 }
 
@@ -23,6 +23,7 @@
 	useGlRenderer true
 #	type sampling
-#	type vss
-	type rss
+	type vss
+#	type rss
+	detectEmptyViewSpace false
 }
 
@@ -30,6 +31,6 @@
 	samplesPerPass  100000
 	initialSamples 500000
-	vssSamples 200000
-	vssSamplesPerPass 100000
+	vssSamples 2000000
+	vssSamplesPerPass 200000
 	useImportanceSampling true
 	loadInitialSamples  false
@@ -44,11 +45,11 @@
 	maxDepth 	40
 	minPvs		30
-	minRays		1000
+	minRays		100
 	minSize		0.001
- 	maxCostRatio	0.9
-	maxRayContribution 0.05
+ 	maxCostRatio	1.5
+	maxRayContribution 0.5
 	
 	maxTotalMemory	200
-	maxStaticMemory	20
+	maxStaticMemory	100
 
 	splitType regular
@@ -57,6 +58,6 @@
 	splitUseOnlyDrivingAxis true
 
-	interleaveDirSplits	false
-    dirSplitDepth 40
+	interleaveDirSplits	true
+    dirSplitDepth 0
 
 	numberOfEndPointDomains	10000
@@ -167,9 +168,9 @@
 
 ViewCells {
-	loadFromFile true
+	loadFromFile false
 	#type kdTree
 	#type vspKdTree
-	type bspTree
-	#type vspBspTree
+	# type bspTree
+	type vspBspTree
 	
 	#type sceneDependent
@@ -199,5 +200,5 @@
 	}
 	
-	filename ../data/soda5/viewcells_soda5.xml
+	filename ../data/soda/viewcells_soda5.xml
 #	filename ../data/atlanta/atlanta_viewcells_large.x3d
 #	filename ../data/vienna/viewcells-25-sel.x3d
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 520)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 534)
@@ -129,5 +129,7 @@
 
   string mViewCellsFilename;
-
+  
+  bool mDetectEmptyViewSpace;
+  
 protected:
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 503)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 534)
@@ -279,8 +279,9 @@
 				float *tmax) const
 {
+  const float dirEps = 1e-8f;
   register float minx, maxx;
   ray.ComputeInvertedDir();
   
-  if (fabs(ray.dir.x) < 0.001) {
+  if (fabs(ray.dir.x) < dirEps) {
     if (mMin.x < ray.loc.x && mMax.x > ray.loc.x) {
       minx = -MAXFLOAT;
@@ -308,5 +309,5 @@
   *tmax = maxx;
   
-  if (fabs(ray.dir.y) < 0.001) {
+  if (fabs(ray.dir.y) < dirEps) {
     if (mMin.y < ray.loc.y && mMax.y > ray.loc.y) {
       minx = -MAXFLOAT;
@@ -336,5 +337,5 @@
     *tmax = maxx;
   
-  if (fabs(ray.dir.z) < 0.001) {
+  if (fabs(ray.dir.z) < dirEps) {
     if (mMin.z < ray.loc.z && mMax.z > ray.loc.z) {
       minx = -MAXFLOAT;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 534)
@@ -216,7 +216,12 @@
   // Watch for near-zero denominator
   // ONLY single sided polygons!!!!!
-  if (dot > -Limits::Small)
-    //  if (fabs(dot) < Limits::Small)
-    return Ray::NO_INTERSECTION;
+  if (ray.mFlags & Ray::CULL_BACKFACES) {
+	if (dot > -Limits::Small)
+	  //  if (fabs(dot) < Limits::Small)
+	  return Ray::NO_INTERSECTION;
+  } else {
+	if (fabs(dot) < Limits::Small)
+	  return Ray::NO_INTERSECTION;
+  }
   
   t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
