Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspKdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspKdTree.cpp	(revision 728)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspKdTree.cpp	(revision 744)
@@ -1125,5 +1125,7 @@
 
 	if (data.mDepth >= mTermMaxDepth)
+	{
 		++ mStat.maxDepthNodes;
+	}
 
 	if (leaf->GetPvsSize() < mTermMinPvs)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 736)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 744)
@@ -1251,5 +1251,5 @@
 	//-- compute pvs difference
 	const float newPvs = 
-#if 0
+#if 1 // not valid if not using const cost per object!!
 		ComputeMergedPvsSize(mc.mLeftViewCell->GetPvs(), 
 							 mc.mRightViewCell->GetPvs());
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 735)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 744)
@@ -1032,12 +1032,13 @@
 			backData.mProbability = tData.mProbability - frontData.mProbability;
 
-			if (frontData.mProbability < -0.00001)
-				Debug << "fatal error f: " << frontData.mProbability << endl;
-			if (backData.mProbability < -0.00001)
-				Debug << "fatal error b: " << backData.mProbability << endl;
-
-			// clamp because of precision issues
+			// should never come here: wrong volume !!!
 			if (0)
 			{
+				if (frontData.mProbability < -0.00001)
+					Debug << "fatal error f: " << frontData.mProbability << endl;
+				if (backData.mProbability < -0.00001)
+					Debug << "fatal error b: " << backData.mProbability << endl;
+
+				// clamp because of precision issues
 				if (frontData.mProbability < 0)	frontData.mProbability = 0;
 				if (backData.mProbability < 0) backData.mProbability = 0;
@@ -2089,4 +2090,6 @@
 		return;
 
+	const float renderCost = mViewCellsManager->EvalRenderCost(obj);
+
 	// new object
 	if ((obj->mMailbox != sFrontId) &&
@@ -2094,5 +2097,5 @@
 		(obj->mMailbox != sFrontAndBackId))
 	{
-		totalPvs += mViewCellsManager->EvalRenderCost(obj);
+		totalPvs += renderCost;
 	}
 
@@ -2106,5 +2109,5 @@
 			(obj->mMailbox != sFrontAndBackId))
 		{
-			frontPvs += mViewCellsManager->EvalRenderCost(obj);
+			frontPvs += renderCost;
 		
 			if (obj->mMailbox == sBackId)
@@ -2120,5 +2123,5 @@
 			(obj->mMailbox != sFrontAndBackId))
 		{
-			backPvs += mViewCellsManager->EvalRenderCost(obj);;
+			backPvs += renderCost;
 		
 			if (obj->mMailbox == sFrontId)
@@ -2197,5 +2200,6 @@
 	if (data.mDepth >= mTermMaxDepth)
 	{
-		++ mBspStats.maxDepthNodes;
+        ++ mBspStats.maxDepthNodes;
+		//Debug << "new max depth: " << mBspStats.maxDepthNodes << endl;
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 736)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 744)
@@ -1232,5 +1232,5 @@
 					optInt,
 					"view_cells_evaluation_histo_passes=",
-					"5");
+					"5000");
 
 	RegisterOption("ViewCells.renderCostEvaluationType",
@@ -1974,5 +1974,5 @@
 		optInt,
 		"vsp_bsp_term_max_view_cells=",
-		"1000");
+		"10000");
 
 	RegisterOption("VspBspTree.Termination.maxCostRatio",
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 726)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 744)
@@ -1734,5 +1734,5 @@
 			!geomFront.Valid() || !geomBack.Valid()))
 		{
-			Debug << "error f: " << pFront << " b: " << pBack << endl;
+			//Debug << "error f: " << pFront << " b: " << pBack << endl;
 			return 99999.9f;
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 736)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 744)
@@ -572,32 +572,39 @@
 	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();		
 
+	// count for integral
+	float volSum = 0;
+	int smallerCostSum = 0;
 	
 	// note can skip computations for view cells already evaluated and delete them from vector ...
     while (1)
 	{
-		float vol = 0;
-		int smallerCost = 0;
+		// count for histogram value
+		float volDif = 0;
+		int smallerCostDif = 0;
 
 		while ((i < (int)viewCells.size()) && (viewCells[i]->GetRenderCost() < currentRenderCost))
 		{
-			vol += viewCells[i]->GetVolume();
+			volSum += viewCells[i]->GetVolume();
+			volDif += viewCells[i]->GetVolume();
+
 			++ i;
-			++ smallerCost;
+			++ smallerCostSum;
+			++ smallerCostDif;
 		}
 		
-		if (i < (int)viewCells.size())
-			Debug << "new rc increase: " << viewCells[i]->GetRenderCost() / totalVol << " " << currentRenderCost / totalVol << endl;
-
-		if ((smallerCost >= (int)viewCells.size()) || (currentRenderCost >= maxRenderCost))
+		if ((i >= (int)viewCells.size()) || (currentRenderCost >= maxRenderCost))
 			break;
 		
-		float rcRatio = currentRenderCost / maxRenderCost;
-		float volRatio = vol / totalVol;
+		const float rcRatio = currentRenderCost / maxRenderCost;
+		const float volRatioSum = volSum / totalVol;
+		const float volRatioDif = volDif / totalVol;
 
 		outstream << "#Pass\n" << j << endl;
 		outstream << "#RenderCostRatio\n" << rcRatio << endl;
 		outstream << "#WeightedCost\n" << currentRenderCost / totalVol << endl;
-		outstream << "#ViewCells\n" << smallerCost << endl;		
-		outstream << "#Volume\n" << volRatio << endl << endl;
+		outstream << "#ViewCellsDif\n" << smallerCostDif << endl;
+		outstream << "#ViewCellsSum\n" << smallerCostSum << endl;	
+		outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
+		outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
 
 		// increase current render cost
@@ -644,31 +651,38 @@
 	int j = 0;
 
+	float volSum = 0;
+	int smallerSum = 0;
+
 	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();		
 	
 	while (1)
 	{
-		float vol = 0;
-		int smaller = 0;
+		float volDif = 0;
+		int smallerDif = 0;
 
 		while ((i < (int)viewCells.size()) && (viewCells[i]->GetPvs().GetSize() < currentPvsSize))
 		{
-			vol += viewCells[i]->GetVolume();
+			volDif += viewCells[i]->GetVolume();
+			volSum += viewCells[i]->GetVolume();
 			++ i;
-			++ smaller;
+			++ smallerDif;
+			++ smallerSum;
 		}
 		
-		if (i < (int)viewCells.size())
-			Debug << "new pvs size increase: " << viewCells[i]->GetPvs().GetSize() << " " << currentPvsSize << endl;
+		//if (i < (int)viewCells.size())
+		//	Debug << "new pvs size increase: " << viewCells[i]->GetPvs().GetSize() << " " << currentPvsSize << endl;
 
 		if ((i >= (int)viewCells.size()) || (currentPvsSize >= maxPvsSize))
 			break;
 
-		float volRatio = vol / totalVol;
+		const float volRatioDif = volDif / totalVol;
+		const float volRatioSum = volSum / totalVol;
 
 		outstream << "#Pass\n" << j ++ << endl;
 		outstream << "#Pvs\n" << currentPvsSize << endl;
-		outstream << "#ViewCells\n" << smaller << endl;		
-		outstream << "#Volume\n" << volRatio << endl << 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
@@ -701,9 +715,9 @@
 	bool dirSamples = (mEvaluationSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
 
-	cout << "Evaluating view cell partition" << endl;
+	cout << "collect leaf view cells and reseting pvs ... ";
 
 	ViewCellContainer leaves;
 	mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
-
+	cout << " ... ";
 	bool startFromZero = true;
 
@@ -723,5 +737,8 @@
 	}
 	
-	
+	cout << "finished" << endl;
+
+	cout << "Evaluating view cell partition" << endl;
+
 	while (castSamples < numSamples)
 	{
@@ -734,9 +751,10 @@
 		*/
 		//-- construction rays => we use uniform samples for this
+		cout << "casting " << samplesPerPass << " samples " << endl;
 		CastPassSamples(samplesPerPass, samplingType, evaluationSamples);
 		
 		castSamples += samplesPerPass;
 
-		cout << "casting " << (int)evaluationSamples.size() << " samples " << endl;
+		cout << "computing sample contributions of " << (int)evaluationSamples.size() << " samples " << endl;
 
 		ComputeSampleContributions(evaluationSamples, true, false);
@@ -768,5 +786,5 @@
 	environment->GetIntValue("ViewCells.Evaluation.histoPasses", histoPasses);
 
-	int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
+	const int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
 
 	Debug << "number of leaves: " << numLeaves << endl;
@@ -777,5 +795,5 @@
 		// evaluate view cells in a histogram		
 		char s[64];
-
+#if 0
 		for (int passes = 1; passes <= histoPasses; ++ passes)
 		{
@@ -800,4 +818,26 @@
 			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);
+
+			EvalViewCellHistogram(filename, pass);
+
+			//////////////////////////////////////////
+            // --evaluate histogram for pvs size
+
+			cout << "computing pvs histogram for " << pass << " view cells" << endl;
+
+			sprintf(s, "-%09d-histo-pvs.log", pass);
+			filename = string(statsPrefix) + string(s);
+
+			EvalViewCellHistogramForPvsSize(filename, pass);
+		}
+#endif
 	}
 
@@ -2013,5 +2053,5 @@
 		mColorCode = 0;
 		
-		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
+		Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
 		
 
@@ -2037,5 +2077,5 @@
 		mColorCode = 1;
 
-		Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.x3d");
+		Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
 	
 		cout << "exporting view cells after merge (pvs size) ... ";	
@@ -3235,5 +3275,5 @@
 	case 2: // merged leaves
 		{
-        	int lSize = mViewCellsTree->GetNumInitialViewCells(vc);
+        	const int lSize = mViewCellsTree->GetNumInitialViewCells(vc);
 			importance = (float)lSize /
 				(float)mCurrentViewCellsStats.maxLeaves;
@@ -3474,5 +3514,5 @@
 	{
 		mColorCode = 0;
-		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
+		Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
 		
 		cout << "exporting view cells after merge ... ";
@@ -3506,5 +3546,5 @@
 		mColorCode = 1;
 
-		Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.x3d");
+		Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
 	
 		cout << "exporting view cells after merge (pvs size) ... ";	
@@ -4157,6 +4197,9 @@
 		{
 			importance = (float)vc->GetPvs().GetSize() /
+#if 1 // hack for result			
 				(float)mCurrentViewCellsStats.maxPvs;
-
+#else
+				463.0f;
+#endif
 		}
 		break;
Index: GTP/trunk/Lib/Vis/shared/EvalStats/EvalStats.cpp
===================================================================
--- GTP/trunk/Lib/Vis/shared/EvalStats/EvalStats.cpp	(revision 737)
+++ GTP/trunk/Lib/Vis/shared/EvalStats/EvalStats.cpp	(revision 744)
@@ -8,5 +8,6 @@
 #include <iostream>
 #include <fstream>
-
+#include <math.h>
+#include <algorithm>
 
 using namespace std;
@@ -111,8 +112,14 @@
 
 
+inline bool vlt(const RenderStats &c1, const RenderStats &c2)
+{
+	return c1.mRenderCost > c2.mRenderCost;
+}
+
+
+// evaluate number of view cells needed for same rendercost
 void EvalNumViewCells(ofstream &outstream,
 					  const StatsContainer &firstStats, 
 					  const StatsContainer &currentStats)
-					  // vector<float> &ratios)
 {
 	
@@ -130,9 +137,5 @@
 	for (it = currentStats.begin(); it != it_end; ++ it, ++ i)
 	{
-		//if (i > 200)	break;
-
-		int j = 0;
 		
-
 		const float renderCost = (*it).mRenderCost;
     
@@ -140,10 +143,7 @@
 		// don't stop until cursor is placed one element behind
 		// or end of vector is reached
-		// note: use stl for this
-		while ((j < n) && (renderCost <= firstStats[j].mRenderCost))
-		{
-			//cout << "!!rendercost: " << renderCost << " " << firstStats[j].mRenderCost << endl;
-			++ j;
-		}
+		StatsContainer::const_iterator equalCostIt = std::upper_bound(firstStats.begin(), firstStats.end(), *it, vlt);
+
+		int j = (int)(equalCostIt - firstStats.begin());
 
 		float val;
@@ -154,16 +154,25 @@
 			val = (float)j;
 		}
-		else if (j == n)
+		else if (j >= n)
 		{
 			val = (float)(j - 1);
 		}
-		else // interpolate linearly. NOTE: probably big error because of steep curve
-		{
+		else 
+		// interpolate linearly. NOTE: probably big error because of steep curve
+		// => intepolate logarithmically
+		{
+#if 1
+			const float rcu = log(firstStats[j - 1].mRenderCost);
+			const float rcl = log(firstStats[j].mRenderCost);
+			const float rc = log(renderCost);
+#else
 			const float rcu = firstStats[j - 1].mRenderCost;
 			const float rcl = firstStats[j].mRenderCost;
-
+			const float rc = renderCost;
+#endif
 			const float factor = (rcu - rcl != 0) ?
-				(rcu - renderCost) / (rcu - rcl) : 1;
-
+				(rcu - rc) / (rcu - rcl) : 1;
+
+			// view cells needed for same render cost
 			val = (float)j - 1 + factor;
 		}
@@ -171,15 +180,11 @@
 		// lower bound
 		//int j = max (0, i - 1);
-		cout << "i: " << i << " j: " << j << endl;
+		//cout << "i: " << i << " j: " << j << endl;
 		float ratio = (i && val) ? (float)i / val : 1;
 
-		cout << "ratio: " << ratio << endl;
+		//cout << "ratio: " << ratio << endl;
 		outstream << "#Pass\n" << i << endl;
 		outstream << "#RenderCost\n" << renderCost << endl;
 		outstream << "#ViewCellsRatio\n" << ratio << endl << endl;	
-
-		//cout << "#Pass " << i ++ << endl;
-		//cout << "#RenderCost " << renderCost << endl;
-		//cout << "#ViewCellsRatio " << ratio << endl;	
 	}
 }
@@ -263,11 +268,9 @@
 		for (int i = 0; i < n; ++ i)
 		{
-			cout << "rc: " << (*it)[i].mRenderCost << endl;
+			//cout << "rc: " << (*it)[i].mRenderCost << endl;
 			ComputeStats(statsOut, firstStats[i], (*it)[i], i);
 		}
-#if 0
-		const int vc = ComputeNoViewCells(firstStats, *it);
-		cout << "need " << vc << " view cells to reach rendercost of method " << i << " with " << n << " view cells" << endl;
-#endif
+
+		// evaluate number of view cells needed for same rendercost
 		EvalNumViewCells(statsOut2, firstStats, (*it));
 
