Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1664)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1666)
@@ -102,5 +102,5 @@
   echo "$SCENE $METHOD"
 
-# $PROGRAM $ENVIRONMENT \
+ $PROGRAM $ENVIRONMENT \
   -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
   -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
@@ -134,6 +134,6 @@
 
 ###############################################################
-
- METHOD=gradient-2
+MEMCONST=1.0
+ METHOD=gradient-$MEMCONST
  echo ********" $SCENE $METHOD ************"
 
@@ -148,4 +148,28 @@
  -hierarchy_construction_levels=4 \
  -hierarchy_construction_consider_memory=true \
+ -hierarchy_term_memory_const=$MEMCONST \
+ -bvh_construction_render_cost_decrease_weight=0.99 \
+ -vsp_construction_render_cost_decrease_weight=0.99 \
+ -hierarchy_construction_recompute_split_on_repair=true
+
+sh movefiles.sh $LOG_PREFIX-$METHOD
+
+#########################################################################
+
+MEMCONST=0.1
+ METHOD=gradient-$MEMCONST
+ echo ********" $SCENE $METHOD ************"
+
+ $PROGRAM $ENVIRONMENT \
+ -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
+ -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+ -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+ -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+ -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+ -hierarchy_construction_type=2 \
+ -hierarchy_construction_multilevel=false \
+ -hierarchy_construction_levels=4 \
+ -hierarchy_construction_consider_memory=true \
+ -hierarchy_term_memory_const=$MEMCONST \
  -bvh_construction_render_cost_decrease_weight=0.99 \
  -vsp_construction_render_cost_decrease_weight=0.99 \
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1662)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1666)
@@ -1083,24 +1083,22 @@
 
 	ViewCellsManager *LoadViewCells(const string &filename, 
-		ObjectContainer *objects,
-		const bool finalizeViewCells,
-		BoundingBoxConverter *bconverter);
-
-	bool AddSampleToPvs(
-		Intersectable *obj, 
-		const Vector3 &hitPoint,
-		ViewCell *vc,
-		const float pdf, 
-        float &contribution) const;
-
-	void ExportSingleViewCells(
-		const ObjectContainer &objects,
-		const int maxViewCells,
-		const bool sortViewCells,
-		const bool exportPvs,
-		const bool exportRays,
-		const int maxRays,
-		const string prefix,
-		VssRayContainer *visRays = NULL);
+									ObjectContainer *objects,
+									const bool finalizeViewCells,
+									BoundingBoxConverter *bconverter);
+
+	bool AddSampleToPvs(Intersectable *obj, 
+						const Vector3 &hitPoint,
+						ViewCell *vc,
+						const float pdf, 
+						float &contribution) const;
+
+	void ExportSingleViewCells(const ObjectContainer &objects,
+							   const int maxViewCells,
+							   const bool sortViewCells,
+							   const bool exportPvs,
+							   const bool exportRays,
+							   const int maxRays,
+							   const string prefix,
+							   VssRayContainer *visRays = NULL);
 
 protected:
@@ -1112,14 +1110,11 @@
 	/** Exports view cell geometry.
 	*/
-	void ExportViewCellGeometry(
-		Exporter *exporter,
-		ViewCell *vc,
-		const AxisAlignedBox3 *box,
-		const AxisAlignedPlane *clipPlane = NULL
-		) const;
-
-	int ComputeBoxIntersections(
-		const AxisAlignedBox3 &box, 
-		ViewCellContainer &viewCells) const;
+	void ExportViewCellGeometry(Exporter *exporter,
+								ViewCell *vc,
+								const AxisAlignedBox3 *box,
+								const AxisAlignedPlane *clipPlane = NULL) const;
+
+	int ComputeBoxIntersections(const AxisAlignedBox3 &box, 
+								ViewCellContainer &viewCells) const;
 	
 	void CollectViewCells();
@@ -1135,8 +1130,7 @@
 	/** Exports visualization of the PVS.
 	*/
-	void ExportPvs(
-		const ObjectContainer &objects,
-		const VssRayContainer &rays);
-
+	void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
+
+	void ExportStats(const string &mergeStats);
 
 	/////////////////////////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1665)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1666)
@@ -2537,4 +2537,9 @@
 					"1");
 
+	RegisterOption("Hierarchy.Termination.memoryConst",
+					optFloat,
+					"hierarchy_term_memory_const=",
+					"1.0");
+
 	RegisterOption("Hierarchy.Construction.useMultiLevel",
 					optBool,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1663)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1666)
@@ -530,4 +530,6 @@
 	*/
 	bool mConsiderMemory;
+
+	float mMemoryConst;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1665)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1666)
@@ -551,5 +551,5 @@
 			//cout << "here7 rc: " << factor * renderCostDecr << " pvs: " << (1.0f - factor) * splitCandidate.GetPvsEntriesIncr() << endl;
 			//priority = factor * renderCostDecr - (1.0f - factor) * splitCandidate.GetPvsEntriesIncr();// / mBvhStats.Leaves();
-			priority /= ((float)splitCandidate.GetPvsEntriesIncr() + 1.0f);
+			priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mHierarchyManager->mMemoryConst);
 		}
 	}
@@ -589,5 +589,5 @@
 		//cout << "here5 rc: " << factor * renderCostDecr << " pvs: " << (1.0f - factor) * splitCandidate.GetPvsEntriesIncr() << endl;
 		//const float priority = factor * renderCostDecr - (1.0f - factor) * (float)splitCandidate.GetPvsEntriesIncr();
-		priority /= ((float)splitCandidate.GetPvsEntriesIncr() + 1.0f);
+		priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mHierarchyManager->mMemoryConst);
 	}
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1660)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1666)
@@ -5512,5 +5512,5 @@
 	{
 		ViewCell *viewcell = *it;
-		
+
 		if (viewcell->GetValid())
 		{	// if ray not outside of view space
@@ -5519,25 +5519,25 @@
 			if (ray.mTerminationObject) 
 			{
-			  // todo: maybe not correct for kd node pvs
-			  Intersectable *obj = mHierarchyManager->GetIntersectable(ray, true);
-			  
-			  if (viewcell->GetPvs().GetSampleContribution(obj,
-														   ray.mPdf, 
-														   contribution))
+				// todo: maybe not correct for kd node pvs
+				Intersectable *obj = mHierarchyManager->GetIntersectable(ray, true);
+
+				if (viewcell->GetPvs().GetSampleContribution(obj,
+					ray.mPdf, 
+					contribution))
 				{
-				  ++ ray.mPvsContribution;
+					++ ray.mPvsContribution;
 				}
-			  
-			  ray.mRelativePvsContribution += contribution;
+
+				ray.mRelativePvsContribution += contribution;
 			}
-			
-			// for directional sampling it is important to count only contributions
-			// made in one direction!
-			// the other contributions of this sample will be counted for the opposite ray!
+
+			//-- for directional sampling it is important to count only contributions 
+			//-- made in one direction!
+			//-- the other contributions of this sample will be counted for the opposite ray!
 #if SAMPLE_ORIGIN_OBJECTS
 			if (ray.mOriginObject && 
 				viewcell->GetPvs().GetSampleContribution(ray.mOriginObject,
-														 ray.mPdf,
-														 contribution))
+				ray.mPdf,
+				contribution))
 			{
 				++ ray.mPvsContribution;
@@ -5616,4 +5616,144 @@
 
 
+typedef priority_queue<ViewCell *, vector<ViewCell *>, 
+					   myless<vector<ViewCell *>::value_type> > ViewCellsQueue;
+
+typedef priority_queue<BvhNode *, vector<BvhNode *>, 
+					   myless<vector<BvhNode *>::value_type> > ObjectsQueue;
+
+void VspOspViewCellsManager::ExportStats(const string &mergeStats)
+{
+	ofstream stats;
+	stats.open(mergeStats.c_str());
+
+	ObjectsQueue objectsQueue;
+	ViewCellsQueue viewCellsQueue;
+
+	objectsQueue.push(mHierarchyManager->mBvHierarchy->GetRoot());
+	viewCellsQueue.push(mViewCellsTree->GetRoot());
+	
+	int numViewCells = 1;
+	
+	const AxisAlignedBox3 box = mViewSpaceBox;
+	const float vol = box.GetVolume();
+
+	const int rootPvs = mViewCellsTree->GetPvsSize(mViewCellsTree->GetRoot());
+	const int rootEntries = mViewCellsTree->GetPvsEntries(mViewCellsTree->GetRoot());
+
+	cout << "exporting stats ... " << endl;
+	
+	float totalRenderCost, avgRenderCost, expectedCost;
+
+	float deviation = 0;
+	int totalPvs = rootPvs;
+	int entriesInPvs = rootEntries;
+
+	totalRenderCost = avgRenderCost = expectedCost = (float)rootPvs;
+
+	const float entrySize = sizeof(PvsData) + sizeof(Intersectable *);
+	const float memoryCost = (float)entriesInPvs *  entrySize / (1024.0f * 1024.0f);
+
+	/////////////
+	//-- first view cell
+
+/*	mViewCellsTree->UpdateStats(stats,	
+								0, 
+								numViewCells, 
+								0, 
+								totalRenderCost, 
+								rootPvs, 
+								expectedCost, 
+								avgRenderCost, 
+								deviation,
+								totalPvs, 
+								entriesInPvs,
+								memoryCost,
+								0, 
+								mRoot->GetVolume());
+*/		
+
+	//-- go through tree in the order of render cost decrease
+	//-- which is the same order as the view cells were merged
+	//-- or the reverse order of subdivision for subdivision-only 
+	//-- view cell hierarchies.
+
+	while (!(objectsQueue.empty() && viewCellsQueue.empty()))
+	{
+		ViewCell *viewCell = viewCellsQueue.top();
+		BvhNode *bvhNode =  objectsQueue.top();
+
+		if (viewCell->GetMergeCost() < bvhNode->GetMergeCost())
+		{
+			viewCellsQueue.pop();
+/*
+			if (!vc->IsLeaf()) 
+			{	
+				ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
+
+				const int parentPvs = GetPvsSize(interior);
+				const int parentPvsEntries = GetPvsEntries(interior);
+				const float parentCost = (float)parentPvs * interior->GetVolume();
+
+				float childCost = 0;
+				int childPvs = 0;
+				int childPvsEntries = 0;
+
+				-- numViewCells;
+
+				ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+
+				for (it = interior->mChildren.begin(); it != it_end; ++ it)
+				{
+					ViewCell *vc = *it;
+
+					const int pvsSize = GetPvsSize(vc);
+					const int pvsEntries = GetPvsEntries(vc);
+	
+					childCost += (float) pvsSize * vc->GetVolume();
+					childPvs += pvsSize;
+					childPvsEntries += pvsEntries;
+
+					tqueue.push(vc);
+					++ numViewCells;
+				}
+
+				// update stats for this view cell
+				const float costDecr = (parentCost - childCost) / vol;
+
+				totalRenderCost -= costDecr;
+				totalPvs += childPvs - parentPvs;
+				entriesInPvs += childPvsEntries - parentPvsEntries;
+
+				expectedCost = totalRenderCost / (float)numViewCells;
+				avgRenderCost = (float)totalPvs / (float)numViewCells;
+
+				const float entrySize = sizeof(PvsData) + sizeof(Intersectable *);
+				const float memoryCost = (float)entriesInPvs * entrySize / (1024.0f * 1024.0f);
+
+				mViewCellsTree->UpdateStats(stats,
+											0, 
+											numViewCells, 
+											costDecr, 
+											totalRenderCost,
+											parentPvs, 
+											expectedCost, 
+											avgRenderCost, 
+											deviation,
+											totalPvs, 
+											entriesInPvs, 
+											memoryCost,
+											childPvs - parentPvs,
+											vc->GetVolume());
+				*/
+			}
+			else // object space split
+			{
+			}
+	}
+
+	stats.close();
+}
+
+
 #if TEST_EVALUATION
 void VspOspViewCellsManager::EvalViewCellPartition()
@@ -5693,25 +5833,4 @@
 	cout << "finished" << endl;
 
-	vector<int> storageFunc;
-	mViewCellsTree->GetStorageFunction(storageFunc);
-
-	vector<float> costFunc;
-	mViewCellsTree->GetCostFunction(costFunc);
-
-	ofstream outstr("out.txt");
-
-	for (int i = 0; i < storageFunc.size(); ++ i)
-		outstr << i << ": " << storageFunc[i] << endl;
-outstr << endl;
-	for (int i = 0; i < costFunc.size(); ++ i)
-		outstr << i << ": " << costFunc[i] << endl;
-	if (0)
-	for (int i = 0; i <50; ++ i)
-	{
-		Vector3 point = GetViewSpaceBox().GetRandomPoint();
-		ViewCell *vc = GetViewCell(point);
-	}
-	//RenderBvhNode(mHierarchyManager->mBvHierarchy->GetRoot());
-
 	disposeRays(evaluationSamples, NULL);
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1660)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1666)
@@ -1525,6 +1525,6 @@
 {
 	TraversalQueue tqueue;
-
 	tqueue.push(mRoot);
+
 	int numViewCells = 1;
 	
@@ -1535,5 +1535,5 @@
 	const int rootEntries = GetPvsEntries(mRoot);
 
-	Debug << "******** Export stats **********" << endl;
+	cout << "exporting stats ... " << endl;
 	
 	float totalRenderCost, avgRenderCost, expectedCost;
@@ -2151,5 +2151,4 @@
 {
 	TraversalQueue tqueue;
-	
 	tqueue.push(mRoot);
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1664)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1666)
@@ -182,6 +182,8 @@
 	void SetParent(BvhInterior *parent);
 
-	// collects all objects under this node
+	/** collects all objects under this node.
+	*/
 	virtual void CollectObjects(ObjectContainer &objects) = 0;
+
 	/** The bounding box specifies the node extent.
 	*/
@@ -190,9 +192,12 @@
 	{ return mBoundingBox; }
 
-
+	/** Sets bouding box of this node.
+	*/
 	inline 
 	void SetBoundingBox(const AxisAlignedBox3 &boundingBox) 
 	{ mBoundingBox = boundingBox; }
 
+
+	float GetMergeCost() {return 0.0f; };
 
 	/////////////////////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1665)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1666)
@@ -828,5 +828,5 @@
 		//cout << "here4 rc: " << factor * renderCostDecr << " pvs: " << (1.0f - factor) * splitCandidate.GetPvsEntriesIncr() << endl;
 		//const float priority = factor * renderCostDecr - (1.0f - factor) * splitCandidate.GetPvsEntriesIncr();
-		priority /= ((float)splitCandidate.GetPvsEntriesIncr() +  + 1.0f);
+		priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mHierarchyManager->mMemoryConst);
 	}
 	
@@ -853,5 +853,5 @@
 		//cout << "here2 rc: " << factor * renderCostDecr << " pvs: " << (1.0f - factor) * splitCandidate.GetPvsEntriesIncr() << endl; 
 		//const float priority = factor * renderCostDecr - (1.0f - factor) * splitCandidate.GetPvsEntriesIncr();
-		priority /= ((float)splitCandidate.GetPvsEntriesIncr() + 1.0f);
+		priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mHierarchyManager->mMemoryConst);
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1664)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1666)
@@ -125,4 +125,7 @@
 		"Hierarchy.Termination.maxMemory", mTermMaxMemory);
 
+	Environment::GetSingleton()->GetFloatValue(
+		"Hierarchy.Termination.memoryConst", mMemoryConst);
+
 	// compare to bytes
 	mTermMaxMemory *= (1024.0f * 1024.0f);
@@ -139,4 +142,5 @@
 	Debug << "maximal allowed memory: " << mTermMaxMemory << endl;
 	Debug << "consider mem: " << mConsiderMemory << endl;
+	Debug << "mem const: " << mMemoryConst << endl;
 
 	switch (mConstructionType)
