Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 260)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 309)
@@ -63,5 +63,5 @@
 				Name="VCCLCompilerTool"
 				AdditionalIncludeDirectories="..\support;..\support\devil\include;..\support\zlib\include;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\Qt&quot;;..\include"
-				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;TEST_BSP_VIEWCELLS"
+				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;"
 				RuntimeLibrary="2"
 				RuntimeTypeInfo="TRUE"
Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 308)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 309)
@@ -63,4 +63,6 @@
 	totalSamples	1000000
 	samplesPerPass  5
+	viewCells    BSP
+	#viewCells    KD
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 297)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 309)
@@ -1151,4 +1151,9 @@
 		 "1000000");
 
+  RegisterOption("Sampling.viewCells",
+		 optString,
+		 "-view_cells",
+		 "BSP");
+
   RegisterOption("Sampling.samplesPerPass",
 		 optInt,
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 308)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 309)
@@ -1249,6 +1249,4 @@
 			}
 
-			//-- split
-
 			// push data for far child
 			tStack.push(BspRayTraversalData(farChild, extp, maxt));
@@ -1270,9 +1268,5 @@
 			}
 
-   			if (hits && ray.GetType() == Ray::LOCAL_RAY)
-				if (ray.intersections[0].mT <= maxt)
-					break;
-      
-			// get the next node from the stack
+   			// get the next node from the stack
 			if (tStack.empty())
 				break;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 308)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 309)
@@ -10,5 +10,6 @@
 mKdTree(NULL),
 mBspTree(NULL),
-mRootViewCell(NULL)
+mRootViewCell(NULL),
+mViewCellsType(BSP_VIEW_CELLS)
 {
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 305)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 309)
@@ -34,48 +34,50 @@
   p->KdTreeStatistics(cout);
 
-#ifdef TEST_BSP_VIEWCELLS
-  environment->GetStringValue("Scene.viewcells", buff);
 
-  string vcFilename(buff);
+  if (p->mViewCellsType == Preprocessor::BSP_VIEW_CELLS)
+  {
+	   // if BSP tree construction method needs predefined view cells
+	  if (BspTree::sConstructionMethod == BspTree::VIEW_CELLS)
+	  {
+		  environment->GetStringValue("Scene.viewcells", buff);
+		  
+		  string vcFilename(buff);
+	  	 
+		  if (vcFilename != "")
+	          p->LoadViewCells(vcFilename);
+		  else
+			  p->GenerateViewCells();
 
-  // if BSP tree construction method needs predefined view cells
-  if (BspTree::sConstructionMethod == BspTree::VIEW_CELLS)
-  {
-	  if (vcFilename != "")
-          p->LoadViewCells(vcFilename);
-	  else
-		  p->GenerateViewCells();
+		   Debug << "Viewcells loaded / generated. Number of view cells: " << (int)p->mViewCells.size() << endl;
+	  }
+	  
+	  p->BuildBspTree();
+	  p->BspTreeStatistics(Debug);
+	  p->Export("vc_bsptree2.x3d", false, false, true);
+ 
+#if 0
+	  //-- export the complementary view cells
+	  // i.e., the view cells not associated with leafs in the tree.
+	  Exporter *exporter = Exporter::GetExporter("viewcells_compl.x3d");
 
-	 Debug << "Viewcells loaded / generated. Number of view cells: " << (int)p->mViewCells.size() << endl;
-  }
+	  ViewCellContainer::iterator vc_compl_it;
+	  ViewCellContainer vc_compl(p->mViewCells.size() + X3dExporter::foundViewCells.size());
+  
+	  sort(p->mViewCells.begin(), p->mViewCells.end());
 
-  p->BuildBspTree();
-  p->BspTreeStatistics(Debug);
-  p->Export("vc_bsptree2.x3d", false, false, true);
+	  vc_compl_it = set_difference(p->mViewCells.begin(), p->mViewCells.end(), 
+		  X3dExporter::foundViewCells.begin(), X3dExporter::foundViewCells.end(), vc_compl.begin());
 
-#if 0
-  //-- export the complementary view cells
-  // i.e., the view cells not associated with leafs in the tree.
-  Exporter *exporter = Exporter::GetExporter("viewcells_compl.x3d");
-
-  ViewCellContainer::iterator vc_compl_it;
-  ViewCellContainer vc_compl(p->mViewCells.size() + X3dExporter::foundViewCells.size());
-  
-  sort(p->mViewCells.begin(), p->mViewCells.end());
-  vc_compl_it = set_difference(p->mViewCells.begin(), p->mViewCells.end(), 
-				 X3dExporter::foundViewCells.begin(), X3dExporter::foundViewCells.end(),
-				 vc_compl.begin());
-  vc_compl.erase(vc_compl_it, vc_compl.end());
+	  vc_compl.erase(vc_compl_it, vc_compl.end());
   
 
-  if (exporter) 
-  {	
-	  Debug << "Exporting complementary view cells" << endl;
-	  exporter->ExportViewCells(&vc_compl); // export view cells
-	  delete exporter;
+	  if (exporter) 
+	  {	
+		  Debug << "Exporting complementary view cells" << endl;
+		  exporter->ExportViewCells(&vc_compl); // export view cells
+		  delete exporter;
+	  }
+#endif
   }
-#endif
-
-#endif
 
   //  p->mSceneGraph->Export("soda.x3d");
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 308)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 309)
@@ -110,4 +110,8 @@
   /// the root view cell of the bsp tree
   ViewCell *mRootViewCell;
+
+  enum {BSP_VIEW_CELLS, KD_VIEW_CELLS};
+
+  int mViewCellsType;
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 308)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 309)
@@ -13,4 +13,20 @@
   environment->GetIntValue("Sampling.samplesPerPass", mSamplesPerPass);
   environment->GetIntValue("Sampling.totalSamples", mTotalSamples);
+  
+  char viewCellsStr[64];
+  environment->GetStringValue("Sampling.viewCells", viewCellsStr);
+
+  mViewCellsType = BSP_VIEW_CELLS;
+
+  if (strcmp(viewCellsStr, "BSP") == 0)
+	  mViewCellsType = BSP_VIEW_CELLS;
+  else if (strcmp(viewCellsStr, "KD") == 0)
+      mViewCellsType= KD_VIEW_CELLS;
+  else 
+  {
+	  cerr<<"Wrong view cells type" << viewCellsStr << endl;
+	  exit(1);
+  }
+
   mKdPvsDepth = 100;
   mStats.open("stats.log");
@@ -54,5 +70,5 @@
     for (j=1; j < ray.leaves.size() - 1; j++) {
       ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
-    }
+  }
   
   return contributingSamples;
@@ -101,30 +117,31 @@
 	int sampleContributions = 0;
 
-#ifndef TEST_BSP_VIEWCELLS
-	mKdTree->CastRay(ray);
+	if (mViewCellsType == KD_VIEW_CELLS)
+	{
+		mKdTree->CastRay(ray);
 	
-	if (ray.leaves.size()) {
-		sampleContributions += AddNodeSamples(object, ray);
-		
-		if (ray.intersections.size()) {
-			sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray);
+		if (ray.leaves.size()) {
+			sampleContributions += AddNodeSamples(object, ray);
+		
+			if (ray.intersections.size()) {
+				sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray);
+			}
+		} 
+	} else	
+	{
+		mBspTree->CastRay(ray);
+
+		if (ray.viewCells.size() > 0) // view cell found
+		{
+			sampleContributions += sampleContributions += AddObjectSamples(object, ray);
+	
+			// cast ray to KD tree to find intersections with other objects
+			mKdTree->CastRay(ray);
+
+			if (ray.intersections.size()) 
+				sampleContributions += AddObjectSamples(ray.intersections[0].mObject, ray);
 		}
 	}
-#else
-	mBspTree->CastRay(ray);
-
-	if (ray.viewCells.size() > 0) // view cell found
-	{
-		++ sampleContributions;
-	
-		//  cast ray to KD tree to find intersections with other objects
-		mKdTree->CastRay(ray);
-
-		if (ray.intersections.size()) 
-		{
-			sampleContributions += AddObjectSamples(ray.intersections[0].mObject, ray);
-		}
-	}
-#endif
+
 	return sampleContributions;
 }
@@ -235,5 +252,5 @@
 		int index = 0;
 		Debug << "************************************" << endl;
-		Debug << "totalSamples: " << totalSamples << " (" << mTotalSamples << ")" << endl;
+		
 		for (i = 0; i < objects.size(); i++) {
 			KdNode *nodeToSample = NULL;
@@ -294,5 +311,4 @@
 			}
 			
-		
 			object->GetRandomSurfacePoint(point, normal);
 			bool viewcellSample = true;
@@ -333,5 +349,4 @@
 			}
 				
-			
 			if ( i < pvsOut )
 				rays[i].push_back(ray);
@@ -363,16 +378,19 @@
 
 		int pvsSize = 0;
-#ifdef TEST_BSP_VIEWCELLS    
-		for (i=0; i < mViewCells.size(); i++) {
-			ViewCell *vc = mViewCells[i];
-			pvsSize += vc->GetPvs().GetSize();
+        
+		if (mViewCellsType == BSP_VIEW_CELLS)
+		{
+			for (i=0; i < mViewCells.size(); i++) {
+				ViewCell *vc = mViewCells[i];
+				pvsSize += vc->GetPvs().GetSize();
+			}
+		} else
+		{
+			for (i=0; i < objects.size(); i++) {
+				Intersectable *object = objects[i];
+				pvsSize += object->mKdPvs.GetSize();
+			}
 		}
-		Debug << "pvs size: " << pvsSize << endl;
-#else
-		for (i=0; i < objects.size(); i++) {
-			Intersectable *object = objects[i];
-			pvsSize += object->mKdPvs.GetSize();
-		}
-#endif		
+
 		cout << "#Pass " << mPass<<" : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
 		cout << "#TotalSamples=" << totalSamples/1000 
@@ -392,7 +410,7 @@
 			"#AvgRayContrib\n" << passSampleContributions/(float)passContributingSamples << endl;
 	}
-  Debug << "Collecting leaf pvs" << endl;
-  int totalPvsSize = mKdTree->CollectLeafPvs();
-  cout << "#totalPvsSize=" << totalPvsSize << endl;
+  
+	if (mViewCellsType == KD_VIEW_CELLS)	
+		cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
   
   //  HoleSamplingPass();
