Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/TestPreprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/TestPreprocessor.vcproj	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/TestPreprocessor.vcproj	(revision 997)
@@ -80,5 +80,5 @@
 			<Tool
 				Name="VCLinkerTool"
-				AdditionalDependencies="xerces-c_2.lib zdll.lib zziplib.lib devil.lib qtmain.lib QtGuid4.lib Qt3Support4.lib QAxContainer.lib glut32.lib OpenGL32.Lib glu32.lib cg.lib cgGL.lib Preprocessor.lib QtCore4.lib QtOpenGL4.lib"
+				AdditionalDependencies="xerces-c_2.lib zdll.lib zziplib.lib devil.lib qtmain.lib QtGui4.lib Qt3Support4.lib QAxContainer.lib glut32.lib OpenGL32.Lib glu32.lib cg.lib cgGL.lib Preprocessor.lib QtCore4.lib QtOpenGL4.lib"
 				OutputFile="$(OutDir)/Preprocessor.exe"
 				LinkIncremental="1"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 971)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 997)
@@ -1661,17 +1661,21 @@
 	int pvsSize = 0;
 
-	if (vc->IsLeaf())
-	{
-		pvsSize = vc->GetPvs().GetSize();
-	}
-	
-
 	Intersectable::NewMail();
 
 	//////////////////////////
+	// for interiors, pvs can be stored using different methods
+
 	switch (mViewCellsStorage)
 	{
 	case PVS_IN_LEAVES: //-- store pvs only in leaves
 		{			
+			// pvs is always stored directly in leaves
+			if (vc->IsLeaf())
+			{
+				pvsSize = vc->GetPvs().GetSize();
+				break;
+			}
+	
+			// the stored pvs size is the valid pvs size => just return scalar
 			if (vc->mPvsSizeValid)
 			{
@@ -1680,5 +1684,5 @@
 			}
 	
-			//-- if no valid pvs size stored as a scalar=> compute new pvs size
+			//-- if no valid pvs size stored as a scalar => compute new pvs size
 			ViewCellContainer leaves;
 			CollectLeaves(vc, leaves);
@@ -1711,46 +1715,49 @@
 		{
 			////////////////////////
-	//-- compressed pvs
+			//-- compressed pvs
 
 			if (vc->mPvsSizeValid)
+			{
 				return vc->mPvsSize;
-
-			// if no pvs size stored: compute
-	int pvsSize = 0;
-	ViewCell *root = vc;
-	
-	// also add pvs from this view cell to root
-	while (root->GetParent())
-	{
-		root = root->GetParent();
-		pvsSize += CountDiffPvs(root);
-	}
-
-	stack<ViewCell *> tstack;
-	tstack.push(vc);
-
-	while (!tstack.empty())
-	{
-		vc = tstack.top();
-		tstack.pop();
-
-		pvsSize += CountDiffPvs(vc);
-
-		if (!vc->IsLeaf())
-		{
-			ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
-
-			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
-
-			for (it = interior->mChildren.begin(); it != it_end; ++ it)
+			}
+
+			// if no pvs size stored, compute new one
+			int pvsSize = 0;
+			ViewCell *root = vc;
+	
+			// also add pvs from this view cell to root
+			while (root->GetParent())
 			{
-				tstack.push(*it);
-			}		
-		}
-	}
+				root = root->GetParent();
+				pvsSize += CountDiffPvs(root);
+			}
+
+			stack<ViewCell *> tstack;
+			tstack.push(vc);
+
+			while (!tstack.empty())
+			{
+				vc = tstack.top();
+				tstack.pop();
+	
+				pvsSize += CountDiffPvs(vc);
+
+				if (!vc->IsLeaf())
+				{
+					ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
+
+					ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+
+					for (it = interior->mChildren.begin(); it != it_end; ++ it)
+					{
+						tstack.push(*it);
+					}		
+				}
+			}
 			break;
 		}
 	case PVS_IN_INTERIORS:
-	default:Debug << "in interiors: " << vc->mPvsSize << " $$ " << vc->GetPvs().GetSize() << endl;
+	default:
+		Debug << "in interiors: " << vc->mPvsSize << " $$ " << vc->GetPvs().GetSize() << endl;
 		pvsSize = vc->GetPvs().GetSize();		
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 997)
@@ -1168,5 +1168,10 @@
 				 "1000");
 
-  RegisterOption("VssPreprocessor.initialSamples",
+   RegisterOption("RenderSampler.useOcclusionQueries",
+				 optBool,
+				 "render_sampler_use_occlusion_queries=",
+				 "true");
+
+   RegisterOption("VssPreprocessor.initialSamples",
 				 optInt,
 				 "initial_samples=",
@@ -1247,7 +1252,7 @@
 					"false");
 
-	RegisterOption("ViewCells.Evaluation.histoPasses",
-					optInt,
-					"view_cells_evaluation_histo_passes=",
+	RegisterOption("ViewCells.Evaluation.histoStepSize",
+					optInt,
+					"view_cells_evaluation_histo_step_size=",
 					"5000");
 
@@ -1645,5 +1650,4 @@
 				   "pvsRenderErrorSamples=",
 				   "10000");
-
 	
 	RegisterOption("Preprocessor.useGlRenderer",
@@ -1674,5 +1678,5 @@
 	RegisterOption("Preprocessor.exportVisibility",
 				   optBool,
-				   "preprocessor_export_visibility",
+				   "preprocessor_export_visibility=",
 				   "true");
 
@@ -1684,10 +1688,10 @@
 	RegisterOption("Preprocessor.applyVisibilityFilter",
 				   optBool,
-				   "preprocessor_apply_filter",
+				   "preprocessor_apply_filter=",
 				   "true");
 	
 	RegisterOption("Preprocessor.applyVisibilitySpatialFilter",
 				   optBool,
-				   "preprocessor_apply_spatial_filter",
+				   "preprocessor_apply_spatial_filter=",
 				   "true");
 
@@ -1719,5 +1723,6 @@
 	RegisterOption("VspKdTree.Termination.maxDepth", 
 		optInt, 
-		"vsp_term_maxdepth=", "30");
+		"vsp_term_maxdepth=", 
+		"30");
 
 	RegisterOption("VspKdTree.Termination.minPvs", 
@@ -1858,5 +1863,5 @@
 	
 	RegisterOption("VssTree.queryPosWeight", optFloat, "qposweight=", "0.0");
-	RegisterOption("VssTree.useRefDirSplits", optBool, "refdir", "false");
+	RegisterOption("VssTree.useRefDirSplits", optBool, "refdir=", "false");
 	RegisterOption("VssTree.refDirAngle", optFloat, "refangle=", "10");
 	RegisterOption("VssTree.refDirBoxMaxSize", optFloat, "refboxsize=", "0.1");
@@ -1890,15 +1895,15 @@
 	RegisterOption("RssPreprocessor.useImportanceSampling",
 					optBool,
-					"rss_use_importance",
+					"rss_use_importance=",
 					"true");
 
 	RegisterOption("RssPreprocessor.objectBasedSampling",
 					optBool,
-					"rss_object_based_sampling",
+					"rss_object_based_sampling=",
 					"true");
 
 	RegisterOption("RssPreprocessor.directionalSampling",
 					optBool,
-					"rss_directional_sampling",
+					"rss_directional_sampling=",
 					"false");
 
@@ -1911,5 +1916,5 @@
 	RegisterOption("RssTree.epsilon", optFloat, "kd_eps=", "1e-6");
 	RegisterOption("RssTree.ct_div_ci", optFloat, "kd_ctdivci=", "1.0");
-	RegisterOption("RssTree.randomize", optBool, "randomize", "false");
+	RegisterOption("RssTree.randomize", optBool, "randomize=", "false");
 	RegisterOption("RssTree.splitType", optString, "split=", "queries");
 	RegisterOption("RssTree.splitUseOnlyDrivingAxis", optBool, "splitdriving=", "false");
@@ -1931,19 +1936,19 @@
 	RegisterOption("RssTree.minCollapseDepth", optInt, "colldepth=", "4");
 
-	RegisterOption("RssTree.interleaveDirSplits", optBool, "interleavedirsplits", "true");
+	RegisterOption("RssTree.interleaveDirSplits", optBool, "interleavedirsplits=", "true");
 	RegisterOption("RssTree.dirSplitDepth", optInt, "dirsplidepth=", "10");
-	RegisterOption("RssTree.importanceBasedCost", optBool, "importance_based_cost", "true");
+	RegisterOption("RssTree.importanceBasedCost", optBool, "importance_based_cost=", "true");
 	RegisterOption("RssTree.maxRays", optInt, "rss_max_rays=", "2000000");
 
 	RegisterOption("RssTree.perObjectTree", optBool, "rss_per_object_tree", "false");
 
-	RegisterOption("RssPreprocessor.Export.pvs", optBool, "rss_export_pvs", "false");
-	RegisterOption("RssPreprocessor.Export.rssTree", optBool, "rss_export_rss_tree", "false");
-	RegisterOption("RssPreprocessor.Export.rays", optBool, "rss_export_rays", "false");
+	RegisterOption("RssPreprocessor.Export.pvs", optBool, "rss_export_pvs=", "false");
+	RegisterOption("RssPreprocessor.Export.rssTree", optBool, "rss_export_rss_tree=", "false");
+	RegisterOption("RssPreprocessor.Export.rays", optBool, "rss_export_rays=", "false");
 	RegisterOption("RssPreprocessor.Export.numRays", optInt, "rss_export_num_rays=", "5000");
-	RegisterOption("RssPreprocessor.useViewcells", optBool, "rss_use_viewcells", "false");
+	RegisterOption("RssPreprocessor.useViewcells", optBool, "rss_use_viewcells=", "false");
 	RegisterOption("RssPreprocessor.updateSubdivision",
 				   optBool,
-				   "rss_update_subdivision",
+				   "rss_update_subdivision=",
 				   "false");
 
@@ -1971,5 +1976,5 @@
 	RegisterOption("VspBspTree.Termination.minGlobalCostRatio",
 					optFloat,
-					"vsp_bsp_term_min_global_cost_ratio",
+					"vsp_bsp_term_min_global_cost_ratio=",
 					"0.0001");
 
@@ -1981,5 +1986,5 @@
 	RegisterOption("VspBspTree.Termination.globalCostMissTolerance",
 					optInt,
-					"vsp_bsp_term_global_cost_miss_tolerance",
+					"vsp_bsp_term_global_cost_miss_tolerance=",
 					"4");
 
@@ -2050,7 +2055,8 @@
 
 	RegisterOption("VspBspTree.Termination.missTolerance",
-					optInt,
-					"vsp_bsp_term_miss_tolerance=",
-					"4");
+		optInt,
+		"vsp_bsp_term_miss_tolerance=",
+		"4");
+
 	RegisterOption("VspBspTree.splitPlaneStrategy",
 					optString,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 971)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 997)
@@ -193,23 +193,32 @@
 	static bool SmallerPvs(const ViewCell *a, const ViewCell *b) 
 	{
-	return a->GetPvs().GetSize() < b->GetPvs().GetSize();
-  }
-
- static bool SmallerRenderCost(const ViewCell *a, const ViewCell *b) 
- {
-	 return a->GetRenderCost() < b->GetRenderCost();
- }
-
- static bool LargerRenderCost(const ViewCell *a, const ViewCell *b) 
- {
-	 return a->GetRenderCost() > b->GetRenderCost();
- }
+		// HACK: take scalar value because pvs may not have been stored properly
+#if 1
+		return a->mPvsSize < b->mPvsSize;
+#else
+		return a->GetPvs().GetSize() < b->GetPvs().GetSize();
+#endif
+	}
+
+	static bool SmallerRenderCost(const ViewCell *a, const ViewCell *b) 
+	{
+		return a->GetRenderCost() < b->GetRenderCost();
+	}
+
+	static bool LargerRenderCost(const ViewCell *a, const ViewCell *b) 
+	{
+		return a->GetRenderCost() > b->GetRenderCost();
+	}
 
 	void SetMergeCost(const float mergeCost);
+
 	float GetMergeCost() const;
-	static void NewMail(const int reserve = 1) {
+
+	static void NewMail(const int reserve = 1) 
+	{
 		sMailId += sReservedMailboxes;
 		sReservedMailboxes = reserve;
 	}
+
 	void Mail() { mMailbox = sMailId; }
 	bool Mailed() const { return mMailbox == sMailId; }
@@ -309,5 +318,7 @@
 
 	
-	/// points to the currently active view cell.
+	/** points to the currently active view cell. This is the
+		view cell representing the current brach.
+	*/
 	ViewCell *mActiveViewCell;
 };
@@ -349,4 +360,7 @@
 
 public:
+	/** View cells tree constructor taking a view cell mnanager and
+		an environment as parameters.
+	*/
 	ViewCellsTree(ViewCellsManager *vcm, Environment *env);
 	~ViewCellsTree();
@@ -372,14 +386,15 @@
 	
 	/** Assign colors to the viewcells so that they can be renderered interactively without
-	  color flickering.
-	  */
+	    color flickering.  
+	*/
 	void AssignRandomColors();
 
-	/** Updates view cell stats for this particular view cell
+	/** Updates view cell stats for this particular view cell.
 	*/
 	void UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat);
 
 
-	/** Get costs resulting from each merge step. */
+	/** Get costs resulting from each merge step. 
+	*/
 	void GetCostFunction(vector<float> &costFunction);
 
@@ -465,7 +480,7 @@
 
 
-	//////////////////////////////////////////////////////////////
-	//                 merge related stuff                      //
-	//////////////////////////////////////////////////////////////
+	/////////////////////////////////////////////////////////////////
+	//                    merge related stuff                      //
+	/////////////////////////////////////////////////////////////////
 
 	/** Computes render cost of the merged pvs.
@@ -525,5 +540,5 @@
 							   const int numNewViewCells);
 
-	/** merge queue must be reset after some time because expected value
+	/** Merge queue must be reset after some time because expected value
 		may not be valid.
 	*/
@@ -535,6 +550,10 @@
 	int UpdateActiveViewCells(ViewCellContainer &viewCells);
 
+	/** Helper function pullling pvs as high up in the tree as possible.
+	*/ 
 	void PullUpVisibility(ViewCellInterior *interior);
 
+	/** Compress pvs of view cell and children.
+	*/
 	void CompressViewCellsPvs(ViewCell *root);
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 997)
@@ -275,11 +275,11 @@
   
   if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
-	cout<<"Applying visibility filter...";
-	cout<<"filyter width = "<<mVisibilityFilterWidth<<endl;
+	cout<<"Applying visibility filter ...";
+	cout<<"filyter width = " << mVisibilityFilterWidth << endl;
 	
 	mViewCellsManager->ApplyFilter(mKdTree,
 								   mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f,
 								   mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f);
-	cout<<"done.";
+	cout << "done." << endl;
   }
   
@@ -354,5 +354,5 @@
 	
 	if (mLoadViewCells)
-	{
+	{	
 		environment->GetStringValue("ViewCells.filename", buf);
 		mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, environment, true);
@@ -363,7 +363,9 @@
 		char viewCellsStr[64];
 		environment->GetStringValue("ViewCells.type", viewCellsStr);
+		
 	    mViewCellsManager = CreateViewCellsManager(viewCellsStr);
 	}
 
+	//-- parameters for render heuristics evaluation
 	float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
 
@@ -377,4 +379,6 @@
 	mViewCellsManager->SetRenderer(mRenderSimulator);
 
+	// default view space is the extent of the scene
+	mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox());
 
 	if (mUseGlRenderer || mUseGlDebugger)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RenderSampler.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RenderSampler.cpp	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RenderSampler.cpp	(revision 997)
@@ -30,5 +30,5 @@
   
 	if (renderer)
-	{cout << "here18 " << mSamples << endl;
+	{
 		renderer->SampleRenderCost(mSamples, samples);
 	}
@@ -37,5 +37,5 @@
 
 
-	//-- Evaluate properties
+	//-- Evaluate results
 	
 
@@ -46,15 +46,12 @@
 	vector<int> histogram(intervals);
   
-	int i;
-
-	/// initialise histogram
-	for (i = 0; i < intervals; ++ i)
+	// initialise histogram
+	for (int i = 0; i < intervals; ++ i)
 	{
 		histogram[i] = 0;
 	}
 
-	
-
-	for (i = 0; i < mSamples; ++ i) 
+	// store maximal pvs
+	for (int i = 0; i < mSamples; ++ i) 
 	{
 		if (samples[i].mVisibleObjects > maxPvs)
@@ -67,10 +64,13 @@
 
 	// sometimes need need to unifiy maximum pvs to compare with other method's histograms
-	// => choose histoMaxPvs accordingly (higher than all pvsss)
+	// => choose histoMaxPvs accordingly (higher than all pvss) ( but smaller than highest possible pvs?)
+
 	const int maxVal = max(histoMaxVal, maxPvs);
-	
-	for (i = 0; i < mSamples; ++ i) 
+	//const int maxVal = min(max(histoMaxVal, maxPvs), mObjects.size());
+
+	for (int i = 0; i < mSamples; ++ i) 
 	{
 		const int bin = (samples[i].mVisibleObjects * intervals) / maxVal;
+		//const int bin = samples[i].mVisibleObjects;
 
 		++ histogram[bin];
@@ -85,12 +85,14 @@
 
 
-
 	Debug << "****************************************" << endl;
 	Debug << "From point queries: " << endl;
 
-	for (i = 0; i < intervals; ++ i) 
+	for (int i = 0; i < intervals; ++ i) 
 	{
 		outstream << "#Pass\n" << i << endl;
-		outstream << "#Pvs\n" << histogram[i] << endl;
+		outstream << "#Pvs\n" << (float)(i * maxVal) / (float)intervals << endl;
+
+		// HACK: #point samples substitute for volume
+		outstream << "#VolumeDif\n" << (float)histogram[i] / (float)mSamples << endl;
 		//cout << histogram[i] << endl;
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 997)
@@ -28,22 +28,21 @@
 int
 main(int argc, char **argv)
-{QApplication *app = new QApplication(argc, argv);
+{
 
-	cout << "here222237" << endl;
-//Now just call this function at the start of your program and if you're
-//compiling in debug mode (F5), any leaks will be displayed in the Output
-//window when the program shuts down. If you're not in debug mode this will
-//be ignored. Use it as you will!
-//note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
+	//Now just call this function at the start of your program and if you're
+	//compiling in debug mode (F5), any leaks will be displayed in the Output
+	//window when the program shuts down. If you're not in debug mode this will
+	//be ignored. Use it as you will!
+	//note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
     _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
     _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
     _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); 
-cout << "here3" << endl;
-  Debug.open("debug.log");cout << "here2" << endl;
+
+  Debug.open("debug.log");
   environment = new Environment;
-  cout << "here122223" << endl; cout.flush();
+ 
   environment->Parse(argc, argv, USE_EXE_PATH);
   MeshKdTree::ParseEnvironment();
-cout << "here222283" << endl;
+
   char buff[128];
   environment->GetStringValue("Preprocessor.type", buff);
@@ -51,8 +50,8 @@
 	
   Preprocessor *p;
-cout << "here22223" << endl;
+
   if (preprocessorType == "vss")
-  {cout << "here223" << endl;
-	p = new VssPreprocessor();
+  {
+	  p = new VssPreprocessor();
   }
   else
@@ -68,6 +67,5 @@
 		  if (preprocessorType == "render")
 		  {
-			  cout << "here23" << endl;
-			p = new RenderSampler();
+			  p = new RenderSampler();
 		  }
 		  else {
@@ -78,13 +76,12 @@
   
  
-  //QApplication *app = NULL;
+  QApplication *app = NULL;
 
   if (p->mUseGlRenderer || p->mUseGlDebugger) {
-	  cout << "here1111" << endl;
+	
 	// create a qt application first (must be created before any opengl widget...
-	//app = new QApplication(argc, argv);
-	  cout << "here1112" << endl;
+	app = new QApplication(argc, argv);
+	
 	if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
-		cout << "here1133" << endl;cout.flush();
 	  QMessageBox::information(0, "OpenGL pbuffers", 
 							   "This system does not support OpenGL/pbuffers.",
@@ -92,7 +89,6 @@
 	  return -1;
 	}
-	  cout << "here1119" << endl;
   }
-   cout << "here1123" << endl;
+  
   preprocessor = p;
   
@@ -103,9 +99,9 @@
   p->BuildKdTree();
   p->KdTreeStatistics(cout);
-   cout << "here1199" << endl;
+ 
   // parse view cells related options
   p->PrepareViewCells();
   
- cout << "here1117" << endl;
+
   // create a preprocessor thread
   PreprocessorThread *pt = new PreprocessorThread(p, app);
@@ -116,13 +112,13 @@
     p->Export(filename + "-kdtree.x3d", false, true, false);	
   }
- cout << "here1118" << endl;
+
   if (p->mUseGlRenderer) {
-	  cout << "here1114" << endl;
+	 
 	rendererWidget = new GlRendererWidget(p->mSceneGraph, p->mViewCellsManager, p->mKdTree);
 	//  renderer->resize(640, 480);
-	 cout << "here1115" << endl;
+	 
 	rendererWidget->resize(640, 480);
 	rendererWidget->show();
-	  cout << "here1113" << endl;
+	
 	if (p->GetRenderer()) {
 	  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 997)
@@ -223,4 +223,5 @@
   vector<PvsErrorEntry> mPvsErrorBuffer;
 
+  
 private:
   unsigned int *mPixelBuffer;
@@ -232,4 +233,7 @@
 
 									 Intersectable *sourceObject);
+
+	void EvalQueryWithItemBuffer();
+	void EvalQueryWithOcclusionQueries();
 
 public:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 997)
@@ -568,7 +568,13 @@
 	Debug << "histogram min rc: " << minRenderCost << " max rc: " << maxRenderCost << endl;
 
-
-	const int intervals = min(20, (int)viewCells.size());
-
+    int histoIntervals;
+	mEnvironment->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
+	const int intervals = min(histoIntervals, (int)viewCells.size());
+
+	int histoMaxVal;
+	mEnvironment->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
+	maxRenderCost = max(histoMaxVal, maxRenderCost);
+
+	
 	const float range = maxRenderCost - minRenderCost;
 	const float stepSize = range / (float)intervals;
@@ -580,6 +586,4 @@
 	//const float totalVol = mViewCellsTree->GetRoot()->GetVolume();
 
-	//float vol = 0;
-	//int smallerCost = 0;
 	int j = 0;
 	int i = 0;
@@ -677,9 +681,11 @@
 											 const ObjectContainer &objects)
 {
+	if (!ViewCellsConstructed())
+		return false;
+
 	cout << "exporting view cells to xml ... ";
 	
 #if ZIPPED_VIEWCELLS
 	ogzstream stream(filename.c_str());
-	cout << "!!!!!!!!!!!!!!!!!!!!!!" << endl;
 #else
 	std::ofstream stream(filename.c_str());
@@ -755,36 +761,42 @@
 	mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
 
-	int maxPvsSize, minPvsSize;
-
-	// sort by render cost
+	int maxPvs, maxVal, minVal;
+
+	// sort by pvs size
 	sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerPvs);
 
-	minPvsSize = viewCells.front()->GetPvs().GetSize();
-	maxPvsSize = viewCells.back()->GetPvs().GetSize();
-	minPvsSize = 0;
+	//Debug << "********************" << endl;
+	//for (int i = 0; i < viewCells.size(); ++ i)
+	//	Debug << "pvs: " << mViewCellsTree->GetPvsSize(viewCells[i]) << " ";
+	//Debug << "\n";
+	maxPvs = mViewCellsTree->GetPvsSize(viewCells.back());
+	minVal = 0;
+
 	// hack: normalize pvs size
-	maxPvsSize = 1200;
-	minPvsSize = 0;
-	maxPvsSize = 1200;
-
-	Debug << "histogram minpvssize: " << minPvsSize << " maxpvssize: " << maxPvsSize << endl;
-
-
-	const int intervals = min(20, (int)viewCells.size());
-	const int range = maxPvsSize - minPvsSize;
+	int histoMaxVal;
+	mEnvironment->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
+	maxVal = max(histoMaxVal, maxPvs);
+		
+	Debug << "histogram minpvssize: " << minVal << " maxpvssize: " << maxVal << " real maxpvs " << maxPvs << endl;
+
+	int histoIntervals;
+	mEnvironment->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
+	const int intervals = min(histoIntervals, (int)viewCells.size());
+
+	const int range = maxVal - minVal;
 	int stepSize = range / intervals;
 
-	
+	// set step size to avoid endless loop
 	if (!stepSize) stepSize = 1;
 	
 	Debug << "stepsize: " << stepSize << endl;
-	cout << "stepsize: " << stepSize << endl;
+	
 
 	const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
 	const float totalVol = GetViewSpaceBox().GetVolume();
 
-	int currentPvsSize = minPvsSize;//(int)ceil(minRenderCost);
-
-	//float vol = 0;
+	int currentPvs = minVal;//(int)ceil(minRenderCost);
+
+	
 	int i = 0;
 	int j = 0;
@@ -795,13 +807,15 @@
 	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();		
 	
-	while (1)
+	for (int j = 0; j < intervals; ++ j)
 	{
 		float volDif = 0;
 		int smallerDif = 0;
 
-		while ((i < (int)viewCells.size()) && (viewCells[i]->GetPvs().GetSize() < currentPvsSize))
+		while ((i < (int)viewCells.size()) && 
+			   (mViewCellsTree->GetPvsSize(viewCells[i]) < currentPvs))
 		{
 			volDif += viewCells[i]->GetVolume();
 			volSum += viewCells[i]->GetVolume();
+
 			++ i;
 			++ smallerDif;
@@ -809,22 +823,21 @@
 		}
 		
-		//if (i < (int)viewCells.size())
-		//	Debug << "new pvs size increase: " << viewCells[i]->GetPvs().GetSize() << " " << currentPvsSize << endl;
-
-		if ((i >= (int)viewCells.size()) || (currentPvsSize >= maxPvsSize))
-			break;
-
+		if (0 && (i < (int)viewCells.size()))
+			Debug << "new pvs size increase: " << mViewCellsTree->GetPvsSize(viewCells[i]) << " " << currentPvs << endl;
+	
 		const float volRatioDif = volDif / totalVol;
 		const float volRatioSum = volSum / totalVol;
 
-		outstream << "#Pass\n" << j ++ << endl;
-		outstream << "#Pvs\n" << currentPvsSize << endl;
+		outstream << "#Pass\n" << j << endl;
+		outstream << "#Pvs\n" << currentPvs << endl;
 		outstream << "#ViewCellsDif\n" << smallerDif << endl;
 		outstream << "#ViewCellsSum\n" << smallerSum << endl;	
 		outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
 		outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
-
-		// increase current pvs size
-		currentPvsSize += stepSize;
+	
+		//if ((i >= (int)viewCells.size()) || (currentPvs >= maxPvs))	break;
+
+		//-- increase current pvs size to define next interval
+		currentPvs += stepSize;
 	}
 
@@ -856,7 +869,4 @@
 
 
-/*ViewCellContainer leaves;
-mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
-*/	
 	cout << "reseting pvs ... ";
 		
@@ -922,14 +932,17 @@
 	}
 	
+
+	//-- histogram
+
+
 	bool useHisto;
-	int histoPasses;
+	int histoStepSize;
 
 	mEnvironment->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
-	mEnvironment->GetIntValue("ViewCells.Evaluation.histoPasses", histoPasses);
-
+	mEnvironment->GetIntValue("ViewCells.Evaluation.histoStepSize", histoStepSize);
+
+	Debug << "here21 " << histoStepSize << endl;
 	const int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
 
-	//Debug << "number of leaves: " << numLeaves << endl;
-	//cout << "number of leaves: " << numLeaves << endl;
 
 	if (useHisto)
@@ -937,38 +950,18 @@
 		// evaluate view cells in a histogram		
 		char s[64];
+
+		for (int pass = histoStepSize; pass <= numLeaves; pass += histoStepSize)
+		{
+			string filename;
+
+			cout << "computing histogram for " << pass << " view cells" << endl;
 #if 0
-		for (int passes = 1; passes <= histoPasses; ++ passes)
-		{
-			int n = numLeaves * passes / histoPasses;
-		
-			cout << "computing histogram for " << n << " view cells" << endl;
-
-			//-- evaluate histogram for render cost
-			sprintf(s, "-%09d-histo.log", n);
-			string filename = string(statsPrefix) + string(s);
-
-			EvalViewCellHistogram(filename, n);
-
-			//////////////////////////////////////////
-            // --evaluate histogram for pvs size
-
-			cout << "computing pvs histogram for " << n << " view cells" << endl;
-
-			sprintf(s, "-%09d-histo-pvs.log", n);
-			filename = string(statsPrefix) + string(s);
-
-			EvalViewCellHistogramForPvsSize(filename, n);
-		}
-#else
-		for (int pass = histoPasses; pass <= numLeaves; pass += histoPasses)
-		{
-			cout << "computing histogram for " << pass << " view cells" << endl;
-
 			//-- evaluate histogram for render cost
 			sprintf(s, "-%09d-histo.log", pass);
-			string filename = string(statsPrefix) + string(s);
+			filename = string(statsPrefix) + string(s);
 
 			EvalViewCellHistogram(filename, pass);
 
+#endif
 			//////////////////////////////////////////
             //-- evaluate histogram for pvs size
@@ -981,5 +974,4 @@
 			EvalViewCellHistogramForPvsSize(filename, pass);
 		}
-#endif
 	}
 
@@ -1250,14 +1242,13 @@
 										)
 {
-  sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
-
-  int start = mViewCells.size()*minValid;
-  int end = mViewCells.size()*maxValid;
-
-  for (int i=0; i < mViewCells.size(); i++)
-  {
-	  mViewCells[i]->SetValid(i >= start && i <= end);
-  
-  }
+	sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
+
+	int start = mViewCells.size()*minValid;
+	int end = mViewCells.size()*maxValid;
+
+	for (int i = 0; i < mViewCells.size(); ++ i)
+	{
+		mViewCells[i]->SetValid(i >= start && i <= end);
+	}
 }
 
@@ -1293,5 +1284,4 @@
 {
 	viewPoint = mViewSpaceBox.GetRandomPoint();
-
 	return true;
 }
@@ -2537,5 +2527,5 @@
 	Debug << "\nOutput view cells: " << endl;
 
-	// sort view cells to get largest view cells
+	// sort view cells in order to find the largest view cells
 	if (0)
 		stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
@@ -4289,4 +4279,5 @@
 		}
 	}
+
 	Debug << "failed to find valid view point, taking " << viewPoint << endl;
 	return false;
@@ -4709,5 +4700,5 @@
 	int numSamples;
 
-	environment->GetIntValue("RenderSampler.samples", numSamples);
+	mEnvironment->GetIntValue("RenderSampler.samples", numSamples);
 	cout << "samples" << numSamples << endl;
 
@@ -5022,4 +5013,7 @@
   //					  center + Vector3(viewSpaceFilterSize/2));
 
+	if (!ViewCellsConstructed())
+		return;
+
   if (viewSpaceFilterSize >=0.0f) {
 
@@ -5067,4 +5061,7 @@
 							  )
 {
+
+	if (!ViewCellsConstructed())
+		return;
 
   ViewCellContainer::const_iterator it, it_end = mViewCells.end();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 991)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 997)
@@ -444,128 +444,188 @@
 
 void
+GlRendererBuffer::EvalQueryWithItemBuffer(
+									   //RenderCostSample &sample
+									   )
+{
+	// read back the texture
+	glReadPixels(0, 0,
+				GetWidth(), GetHeight(),
+				GL_RGBA,
+				GL_UNSIGNED_BYTE,
+				mPixelBuffer);
+		
+			
+	unsigned int *p = mPixelBuffer;
+			
+	for (int y = 0; y < GetHeight(); y++)
+	{
+		for (int x = 0; x < GetWidth(); x++, p++) 
+		{
+			unsigned int id = (*p) & 0xFFFFFF;
+
+			if (id != 0xFFFFFF)
+				++ mObjects[id]->mCounter;
+		}
+	}
+}
+
+
+void
+GlRendererBuffer::EvalQueryWithOcclusionQueries(
+									   //RenderCostSample &sample
+									   )
+{
+	glDepthFunc(GL_LEQUAL);
+		
+	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+	glDepthMask(GL_FALSE);
+		
+	// now issue queries for all objects
+	for (int j = 0; j < mObjects.size(); ++ j) 
+	{
+		for (int q = 0; j + q < mObjects.size() && q < mOcclusionQueries.size(); ++ q) 
+		{
+			glBeginOcclusionQueryNV(mOcclusionQueries[q]);
+					
+			RenderIntersectable(mObjects[j + q]);
+			glEndOcclusionQueryNV();
+		}
+		
+		// collect results of the queries
+		for (int q = 0; j + q < mObjects.size() && q < mOcclusionQueries.size(); ++ q) 
+		{
+			unsigned int pixelCount;
+					
+			//-- reenable other state
+#if 1
+			do 
+			{
+				glGetOcclusionQueryuivNV(mOcclusionQueries[q],
+										GL_PIXEL_COUNT_AVAILABLE_NV,
+										&pixelCount);
+					
+				if (0 && !pixelCount) cout << "W";
+			} 
+			while (!pixelCount);
+#endif
+
+			glGetOcclusionQueryuivNV(mOcclusionQueries[q],
+									GL_PIXEL_COUNT_NV,
+									&pixelCount);
+
+			// if (pixelCount)
+			// cout<<"q="<<mOcclusionQueries[q]<<" pc="<<pixelCount<<" ";
+	                
+			mObjects[j + q]->mCounter += pixelCount;
+		}
+
+		j += q;
+	}
+		
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	glDepthMask(GL_TRUE);
+}
+
+
+void
 GlRendererBuffer::EvalRenderCostSample(
 									   RenderCostSample &sample
 									   )
 {
-  mViewCellsManager->GetViewPoint(mViewPoint);
-  sample.mPosition = mViewPoint;
-
-  cout << mViewPoint << endl;
-  
-  // take a render cost sample by rendering a cube
-  Vector3 directions[6];
-  directions[0] = Vector3(1,0,0);
-  directions[1] = Vector3(0,1,0);
-  directions[2] = Vector3(0,0,1);
-  directions[3] = Vector3(-1,0,0);
-  directions[4] = Vector3(0,-1,0);
-  directions[5] = Vector3(0,0,-1);
-
-  sample.mVisibleObjects = 0;
-
-  int i, j;
-  
-  // reset object visibility
-  for (i=0; i < mObjects.size(); i++) {
-	mObjects[i]->mCounter = 0;
-  }
-  mFrame++;
-  for (i=0; i < 6; i++) {
-	mViewDirection = directions[i];
-	SetupCamera();
-	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	
-	glDepthFunc( GL_LESS );
-
-	mUseFalseColors = true;
-
-	RenderScene();
-
-	if (1) {
-	  char filename[256];
-	  sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i);
-	  QImage im = toImage();
-	  im.save(filename, "PNG");
-	}
-	
-	bool useItemBuffer = false;
-	
-	if (useItemBuffer) {
-	  // read back the texture
-	  glReadPixels(0, 0,
-				   GetWidth(), GetHeight(),
-				   GL_RGBA,
-				   GL_UNSIGNED_BYTE,
-				   mPixelBuffer);
-	  
-	  int x, y;
-	  unsigned int *p = mPixelBuffer;
-	  for (y = 0; y < GetHeight(); y++)
-		for (x = 0; x < GetWidth(); x++, p++) {
-		  unsigned int id = (*p) & 0xFFFFFF;
-		  if (id != 0xFFFFFF)
-			mObjects[id]->mCounter++;
+	// choose a random view point
+	mViewCellsManager->GetViewPoint(mViewPoint);
+	sample.mPosition = mViewPoint;
+	//cout << "viewpoint: " << mViewPoint << endl;
+
+	// take a render cost sample by rendering a cube
+	Vector3 directions[6];
+
+	directions[0] = Vector3(1,0,0);
+	directions[1] = Vector3(0,1,0);
+	directions[2] = Vector3(0,0,1);
+	directions[3] = Vector3(-1,0,0);
+	directions[4] = Vector3(0,-1,0);
+	directions[5] = Vector3(0,0,-1);
+
+	sample.mVisibleObjects = 0;
+
+	int i, j;
+
+	// reset object visibility
+	for (i=0; i < mObjects.size(); i++) 
+	{
+		mObjects[i]->mCounter = 0;
+	}
+
+	++ mFrame;
+
+	// either use occlusion queries or item buffer
+	bool useOcclusionQueries;
+	environment->GetBoolValue("RenderSampler.useOcclusionQueries", useOcclusionQueries);
+
+	//if (useOcclusionQueries) cout << "using occlusion queries" << endl;
+	//else cout << "using item buffer" << endl;
+	
+	glCullFace(GL_FRONT);
+
+	if (mDetectEmptyViewSpace) 
+	{
+		glEnable(GL_CULL_FACE);
+		cout << "culling" << endl;
+	}
+	else
+	{
+		cout << "not culling" << endl;
+		glDisable(GL_CULL_FACE);
+	}
+
+	for (i = 0; i < 6; ++ i) 
+	{
+		mViewDirection = directions[i];
+		SetupCamera();
+
+		glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+		glDepthFunc(GL_LESS);
+		mUseFalseColors = true;
+
+
+		// the actual scene rendering
+
+		RenderScene();
+
+
+		if (0) 
+		{
+			char filename[256];
+			sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i);
+			QImage im = toImage();
+			im.save(filename, "PNG");
 		}
-	  
-	} else {
-	
-	  //	  glDepthFunc( GL_LEQUAL );
-	  glDepthFunc( GL_LEQUAL );
-	  
-	  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-	  glDepthMask(GL_FALSE);
-	  
-	  // now issue queries for all objects
-	  for (j = 0; j < mObjects.size(); j++) {
-		int q;
-		for (q = 0; j + q < mObjects.size() && q < mOcclusionQueries.size(); q++) {
-		  glBeginOcclusionQueryNV(mOcclusionQueries[q]);
-		  RenderIntersectable(mObjects[j+q]);
-		  glEndOcclusionQueryNV();
+         
+		if (useOcclusionQueries) 
+		{
+			EvalQueryWithOcclusionQueries();
 		}
-		
-		
-		// collect results of the queries
-		for (q = 0; j + q < mObjects.size() && q < mOcclusionQueries.size(); q++) {
-		  unsigned int pixelCount;
-		  // reenable other state
-
-#if 1
-		  do {
-			glGetOcclusionQueryuivNV(mOcclusionQueries[q],
-									 GL_PIXEL_COUNT_AVAILABLE_NV,
-									 &pixelCount);
-			if (!pixelCount)
-			  cout<<"W";
-		  } while(!pixelCount);
-#endif
-
-		  glGetOcclusionQueryuivNV(mOcclusionQueries[q],
-								   GL_PIXEL_COUNT_NV,
-								   &pixelCount);
-		  //		if (pixelCount)
-		  //		  cout<<"q="<<mOcclusionQueries[q]<<" pc="<<pixelCount<<" ";
-		  mObjects[j+q]->mCounter += pixelCount;
+		else 
+		{
+			EvalQueryWithItemBuffer();
 		}
-		j += q;
-	  }
-	  
-	  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-	  glDepthMask(GL_TRUE);
-	}
-	  
-	
-  }  
-  // now evaluate the statistics over that sample
-  // currently only the number of visible objects is taken into account
-  sample.Reset();
-  for (j = 0; j < mObjects.size(); j++) {
-	if (mObjects[j]->mCounter) {
-	  sample.mVisibleObjects++;
-	  sample.mVisiblePixels += mObjects[j]->mCounter;
-	}
-  }
-  cout<<"RS="<<sample.mVisibleObjects<<" ";
+	}  
+
+	// now evaluate the statistics over that sample
+	// currently only the number of visible objects is taken into account
+	sample.Reset();
+
+	for (j = 0; j < mObjects.size(); j++) 
+	{
+		if (mObjects[j]->mCounter) 
+		{
+			++ sample.mVisibleObjects;
+			sample.mVisiblePixels += mObjects[j]->mCounter;
+		}
+	}
+
+	cout << "RS=" << sample.mVisibleObjects << " ";
 }
 
@@ -575,9 +635,9 @@
 								   vector<RenderCostSample> &samples
 								   )
-{cout << "here1" << endl;
+{
   makeCurrent();
 
   if (mPixelBuffer == NULL)
-	mPixelBuffer = new unsigned int[GetWidth()*GetHeight()];
+	  mPixelBuffer = new unsigned int[GetWidth()*GetHeight()];
   
   SetupProjection(GetHeight(), GetHeight(), 90.0f);
@@ -589,5 +649,4 @@
   int numQ = 500;
 
-  cout << "here2988" << endl;
 
   if (mOcclusionQueries.size() < numQ) 
@@ -605,5 +664,5 @@
 	  mOcclusionQueries.resize(numQ);
 	
-	  for (i=0; i < numQ; ++ i)
+	  for (i = 0; i < numQ; ++ i)
 	  {
 		  mOcclusionQueries[i] = queries[i];
@@ -613,7 +672,6 @@
   }
   
-  for (i=0; i < n; ++ i)
+  for (i = 0; i < n; ++ i)
   {
-	  cout << i << " ";
 	  EvalRenderCostSample(samples[i]);
   }
