Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 734)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 735)
@@ -810,8 +810,10 @@
 
 	/// if we should use breath first priority for the splits
-	bool mBreathFirstSplits;
-	bool mDepthFirstSplits;
+	int mNodePriorityQueueType;
+
 	bool mEmptyViewCellsMergeAllowed;
 
+	// priority queue strategy
+	enum {BREATH_FIRST, DEPTH_FIRST, COST_BASED};
 private:
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 728)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 735)
@@ -405,4 +405,8 @@
 	void EvalViewCellHistogram(const string filename, const int nViewCells);
 
+	/** Evaluautes histogram for a given number of view cells.
+	*/
+	void EvalViewCellHistogramForPvsSize(const string filename, const int nViewCells);
+
 	/** Evaluates the render cost of a view cell.
 	*/
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 734)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 735)
@@ -120,5 +120,5 @@
 	environment->GetBoolValue("VspBspTree.simulateOctree", mSimulateOctree);
 	environment->GetBoolValue("VspBspTree.useRandomAxis", mUseRandomAxis);
-	environment->GetBoolValue("VspBspTree.useBreathFirstSplits", mBreathFirstSplits);
+	environment->GetIntValue("VspBspTree.nodePriorityQueueType", mNodePriorityQueueType);
 
 	environment->GetBoolValue("ViewCells.PostProcess.emptyViewCellsMerge", mEmptyViewCellsMergeAllowed);
@@ -154,9 +154,6 @@
 	Debug << "subdivision stats log: " << subdivisionStatsLog << endl;
 	Debug << "use random axis: " << mUseRandomAxis << endl;
-	Debug << "breath first splits: " << mBreathFirstSplits << endl;
-	Debug << "depth first splits: " << mDepthFirstSplits << endl;
-
+	Debug << "priority queue type: " << mNodePriorityQueueType << endl;
 	Debug << "empty view cells merge: " << mEmptyViewCellsMergeAllowed << endl;
-
 	Debug << "octree: " << mSimulateOctree << endl;
 
@@ -942,11 +939,17 @@
 void VspBspTree::EvalPriority(VspBspTraversalData &tData) const
 {
-	if (mBreathFirstSplits)
+    switch (mNodePriorityQueueType) 
+	{
+	case BREATH_FIRST:
 		tData.mPriority = (float)-tData.mDepth;
-	else if (mDepthFirstSplits)
+		break;
+	case DEPTH_FIRST:
 		tData.mPriority = (float)tData.mDepth;
-	else
+		break;
+	default:
 		tData.mPriority = tData.mPvs * tData.mProbability;
-	//cout << "priority: " << tData.mPriority << endl;
+		//Debug << "priority: " << tData.mPriority << endl;
+		break;
+	}
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 734)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 735)
@@ -1224,4 +1224,15 @@
 					"viewCells");
 
+	RegisterOption("ViewCells.Evaluation.histogram",
+					optBool,
+					"view_cells_evaluation_histogram=",
+					"false");
+
+	RegisterOption("ViewCells.Evaluation.histoPasses",
+					optInt,
+					"view_cells_evaluation_histo_passes=",
+					"5");
+
+
 	RegisterOption("ViewCells.renderCostEvaluationType",
 					optString,
@@ -2059,13 +2070,8 @@
 		"false");
 
-	RegisterOption("VspBspTree.useBreathFirstSplits",
-		optBool,
-		"vsp_bsp_breath_first_splits=",
-		"false");
-
-	RegisterOption("VspBspTree.useDepthFirstSplits",
-		optBool,
-		"vsp_bsp_depth_first_splits=",
-		"false");
+	RegisterOption("VspBspTree.nodePriorityQueueType",
+		optInt,
+		"vsp_bsp_node_queue_type=",
+		"0");
 
 	RegisterOption("VspBspTree.useRandomAxis",
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 734)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 735)
@@ -541,12 +541,8 @@
 	outstream.open(filename.c_str());
 
-	Debug << "here3" << endl;
 	ViewCellContainer viewCells;
-	mViewCellsTree->CollectBestViewCellSet(mViewCells, nViewCells);
+	mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
 
 	float maxRenderCost, minRenderCost;
-
-	/// find out the range for valid render cost
-	ComputeMinMaxRenderCost(minRenderCost, maxRenderCost, viewCells);
 
 	// sort by render cost
@@ -556,41 +552,55 @@
 	maxRenderCost = viewCells.back()->GetRenderCost();
 
-	const int intervals = 10000;
+	const int intervals = min(10000, (int)viewCells.size());
+
 	const float range = maxRenderCost - minRenderCost;
-	int currentRenderCost = (int)ceil(minRenderCost);
-	const int stepSize = (int)((float)range / (float)intervals);
+	const float stepSize = range / (float)intervals;
+
+	float currentRenderCost = minRenderCost;//(int)ceil(minRenderCost);
+
+	const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
+	const float totalVol = GetViewSpaceBox().GetVolume();
 
 	float vol = 0;
 	int smallerCost = 0;
-
+	int i = 0;
+	int j = 0;
+
+	
 	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();		
 
-	int i = 0;
-
+	
 	// note can skip computations for view cells already evaluated and delete them from vector ...
     while (1)
 	{
-	Debug << "here4" << endl;
 		while ((i < (int)viewCells.size()) && (viewCells[i]->GetRenderCost() < currentRenderCost))
 		{
-			
-	Debug << "here5" << endl;
+			vol += viewCells[i]->GetVolume();
 			++ i;
 			++ smallerCost;
-			vol += viewCells[i]->GetVolume();
-		}
-
+		}
+
+		if ((i >= (int)viewCells.size()) || (currentRenderCost >= maxRenderCost))
+			break;
+
+		float rcRatio = currentRenderCost / maxRenderCost;
+		float volRatio = vol / totalVol;
+
+		outstream << "#Pass\n" << j ++ << endl;
+		outstream << "#RenderCost\n" << rcRatio << endl;
+		//fprintf(outstream, "#RenderCost\n%3.3f", rcRatio);
+		outstream << "#ViewCells\n" << i << endl;		
+		outstream << "#Volume\n" << volRatio << endl << endl;
+
+		// increase current render cost
 		currentRenderCost += stepSize;
-		
-		outstream << "#RenderCost: " << currentRenderCost << endl;
-		outstream << "#ViewCells: " << smallerCost << endl;		
-		outstream << "#Volume: " << vol << endl << endl;
-	}
+	}
+
 	outstream.close();
-	Debug << "here6" << endl;
-}
-
-/*
-void ViewCellsManager::EvalViewCellHistogramForPvs(const string filename, const int nViewCells)
+}
+
+
+void ViewCellsManager::EvalViewCellHistogramForPvsSize(const string filename, 
+													   const int nViewCells)
 {
 	std::ofstream outstream;
@@ -598,49 +608,62 @@
 
 	ViewCellContainer viewCells;
-	mViewCellsTree->CollectBestViewCellSet(mViewCells, nViewCells);
-
-	float maxRenderCost, minRenderCost;
-	/// find out the range for valid render cost
-	ComputeMinMaxRenderCost(minRenderCost, maxRenderCost, viewCells);
-
-	const int intervals = 10000;
-	const float range = maxRenderCost - minRenderCost;
-	int currentRenderCost = (int)ceil(minRenderCost);
-	const int stepSize = (int)((float)range / (float)intervals);
+	mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
+
+	int maxPvsSize, minPvsSize;
+
+	// sort by render cost
+	sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerPvs);
+
+	minPvsSize = viewCells.front()->GetPvs().GetSize();
+	maxPvsSize = viewCells.back()->GetPvs().GetSize();
+
+	const int intervals = min(10000, (int)viewCells.size());
+	const int range = maxPvsSize - minPvsSize;
+	int stepSize = range / intervals;
+
+	if (!stepSize) stepSize = 1;
+
+	const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
+	const float totalVol = GetViewSpaceBox().GetVolume();
+
+	int currentPvsSize = minPvsSize;//(int)ceil(minRenderCost);
+
 	float vol = 0;
-
-	ViewCell::NewMail();
-
-	for (; currentRenderCost < (int)maxRenderCost; currentRenderCost += stepSize)
-	{
-		ViewCellContainer::const_iterator it, it_end = viewCells.end();		
-
-		int smallerCost = 0;
-
-		// note can skip computations for view cells already evaluated and delete them from vector ...
-        for (it = viewCells.begin(); it != viewCells.end(); ++ it)
-		{
-			if (!vc->Mailed())
-			{
-				vc->Mail();
-			ViewCell *vc = *it;
-
-			if (EvalRenderCost(vc) < currentRenderCost)
-			{
-				++ smallerCost;
-				vol = (*it)->GetVolume();
-			}
-		}
-		// note: can remove already found view cells
-		//swap(neighborhood[bestViewCellIdx], neighborhood.back());
-		//neighborhood.pop_back();
-	
-
-		outstream << "#RenderCost: " << currentRenderCost << endl;
-		outstream << "#ViewCells: " << smallerCost << endl;		
-		outstream << "#Volume: " << vol << endl << endl;
-	}
-}
-*/
+	int smallerCost = 0;
+
+	int i = 0;
+	int j = 0;
+
+	
+	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();		
+	
+	// note can skip computations for view cells already evaluated and delete them from vector ...
+    while (1)
+	{
+		while ((i < (int)viewCells.size()) && (viewCells[i]->GetPvs().GetSize() < currentPvsSize))
+		{
+			vol += viewCells[i]->GetVolume();
+			++ i;
+			++ smallerCost;
+		}
+
+		if ((i >= (int)viewCells.size()) || (currentPvsSize >= maxPvsSize))
+			break;
+
+		float volRatio = vol / totalVol;
+
+		outstream << "#Pass\n" << j ++ << endl;
+		outstream << "#Pvs\n" << currentPvsSize << endl;
+		//fprintf(outstream, "#RenderCost\n%3.3f", rcRatio);
+		outstream << "#ViewCells\n" << i << endl;		
+		outstream << "#Volume\n" << volRatio << endl << endl;
+
+
+		// increase current pvs size
+		currentPvsSize += stepSize;
+	}
+	outstream.close();
+}
+
 
 void ViewCellsManager::EvalViewCellPartition(Preprocessor *preprocessor)
@@ -688,5 +711,5 @@
 	}
 	
-
+	
 	while (castSamples < numSamples)
 	{
@@ -727,26 +750,39 @@
 	}
 	
-#if 0
-	// evaluate view cells in a histogram
-	vector<int> nViewCells;
-	nViewCells.push_back(10000);
-	nViewCells.push_back(5000);
-	nViewCells.push_back(1000);
-	nViewCells.push_back(500);
-	nViewCells.push_back(100);
-
-	vector<int>::const_iterator iit, iit_end = nViewCells.end();
-	char filename[64];
-
-	for (iit = nViewCells.begin(); iit != nViewCells.end(); ++ iit)
-	{
-		Debug << "here2" << endl;
-		int n = *iit;
+	bool useHisto;
+	int histoPasses;
+
+	environment->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
+	environment->GetIntValue("ViewCells.Evaluation.histoPasses", histoPasses);
+
+	if (useHisto)
+	{
+		// evaluate view cells in a histogram		
+		char s[64];
+
+		for (int passes = 1; passes <= histoPasses; ++ passes)
+		{
+			int n = (int)mViewCells.size() * passes / histoPasses;
 		
-		sprintf(filename, "histogram%06d.log", n);
-		EvalViewCellHistogram(filename, n);
-	}
-
-#endif
+			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);
+		}
+	}
+
 	// find empty view cells bug
 	if (TEST_EMPTY_VIEW_CELLS)
@@ -802,5 +838,5 @@
 	
 	case PER_TRIANGLE:
-		{
+		{cout << "pertriangle" << endl;
 			// HACK
 			MeshInstance *mi = dynamic_cast<MeshInstance *>(obj);
