Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1676)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1679)
@@ -13,6 +13,6 @@
 #SCENE=vsposp_typical
 #SCENE=soda5_vsposp
-SCENE=soda_vsposp
-#SCENE=vienna_vsposp
+#SCENE=soda_vsposp
+SCENE=vienna_vsposp
 #SCENE=arena_vsposp
 #SCENE=vsposp_citymodel
@@ -60,16 +60,16 @@
 
 ##################################################################################
-MEMCONST=0.8
- METHOD=gradient-mem-$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_consider_memory=true \
+MEMCONST=1.0
+ METHOD=gradient-mem2-$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_consider_memory2=true \
  -bvh_construction_render_cost_decrease_weight=0.99 \
  -vsp_construction_render_cost_decrease_weight=0.99 \
@@ -83,15 +83,15 @@
 ##################################################################################
 MEMCONST=0.5
- METHOD=gradient-mem-$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_consider_memory=true \
+ METHOD=gradient-mem2-$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_consider_memory2=true \
  -bvh_construction_render_cost_decrease_weight=0.99 \
  -vsp_construction_render_cost_decrease_weight=0.99 \
@@ -104,15 +104,15 @@
 ##################################################################################
 MEMCONST=0.2
- METHOD=gradient-mem-$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_consider_memory=true \
+ METHOD=gradient-mem2-$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_consider_memory2=true \
  -bvh_construction_render_cost_decrease_weight=0.99 \
  -vsp_construction_render_cost_decrease_weight=0.99 \
@@ -126,10 +126,10 @@
 
 ############################################################################
-NODES=1000
+NODES=10000
 
   METHOD=sequential-$NODES
   echo "$SCENE $METHOD"
 
-# $PROGRAM $ENVIRONMENT \
+ $PROGRAM $ENVIRONMENT \
   -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
   -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
@@ -144,10 +144,10 @@
 ############################################################################
 
-NODES=10000
+NODES=70000
 
   METHOD=sequential-$NODES
   echo "$SCENE $METHOD"
 
- #$PROGRAM $ENVIRONMENT \
+ $PROGRAM $ENVIRONMENT \
   -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
   -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
@@ -168,12 +168,54 @@
   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=0 \
-#   -bvh_term_max_leaves=$NODES
+ $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=0 \
+  -bvh_term_max_leaves=$NODES
+
+sh movefiles.sh $LOG_PREFIX-$METHOD
+
+
+############################################################################
+
+NODES=100000
+
+  METHOD=sequential-$NODES
+  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=0 \
+   -bvh_term_max_leaves=$NODES
+
+sh movefiles.sh $LOG_PREFIX-$METHOD
+
+
+##################################################################################
+MEMCONST=1.0
+ METHOD=gradient-multi-$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_consider_memory2=true \
+ -bvh_construction_render_cost_decrease_weight=0.99 \
+ -vsp_construction_render_cost_decrease_weight=0.99 \
+  -hierarchy_construction_multilevel=true \
+ -hierarchy_construction_levels=4 \
+  -hierarchy_term_memory_const=$MEMCONST \
+  -hierarchy_construction_recompute_split_on_repair=true
 
 sh movefiles.sh $LOG_PREFIX-$METHOD
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1676)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1679)
@@ -634,4 +634,5 @@
 	void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
 
+	virtual void ExportStats(const string &mergeStats);
 
   
@@ -1132,5 +1133,5 @@
 	void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
 
-	void ExportStats(const string &mergeStats);
+	//void ExportStats(const string &mergeStats);
 
 	/////////////////////////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1677)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1679)
@@ -544,4 +544,5 @@
 	bool mConsiderMemory2;
 
+	int mTimeStamp;
 	friend VspTree;
 	friend OspTree;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1677)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1679)
@@ -147,5 +147,5 @@
 public:
 	
-	// types of vsp nodes
+	/// types of vsp nodes
 	enum {Interior, Leaf};
 
@@ -165,16 +165,21 @@
 	*/
 	virtual bool IsRoot() const;
+	
 	/** Returns parent node.
 	*/
 	VspInterior *GetParent();
+	
 	/** Sets parent node.
 	*/
 	void SetParent(VspInterior *parent);
+
 	/** Returns true if this node is a sibling of node n.
 	*/
 	bool IsSibling(VspNode *n) const;
+	
 	/** returns depth of the node.
 	*/
 	int GetDepth() const;
+	
 	/** returns true if the whole subtree is valid
 	*/
@@ -182,5 +187,10 @@
 
 	void SetTreeValid(const bool v);
-
+	
+	/** Cost of mergin this node.
+	*/
+	float GetMergeCost() {return (float)-mTimeStamp; }
+
+	/////////
 	//-- mailing options
 
@@ -195,8 +205,13 @@
 	int mTimeStamp;
 
+	int mPvsEntriesIncr;
+	float mMemoryIncr;
+	float mRenderCostDecr;
+
 protected:
 
 	/// if this sub tree is a completely valid view space region
 	bool mTreeValid;
+	
 	/// parent of this node
 	VspInterior *mParent;
@@ -209,4 +224,5 @@
 {
 public:
+
 	/** Standard contructor taking split plane as argument.
 	*/
@@ -214,4 +230,5 @@
 
 	~VspInterior();
+	
 	/** @return false since it is an interior node 
 	*/
@@ -315,6 +332,8 @@
 	/// Rays piercing this leaf.
 	VssRayContainer mVssRays;
+	
 	/// leaf pvs
 	ObjectPvs *mPvs;
+
 	/// Probability that the view point lies in this leaf
 	float mProbability;
@@ -324,4 +343,5 @@
 	/// pointer to a split plane candidate splitting this leaf
 	SubdivisionCandidate *mSubdivisionCandidate;
+	
 	/// if NULL this does not correspond to feasible viewcell
 	ViewCellLeaf *mViewCell;
@@ -492,9 +512,12 @@
 			if (hm->ConsiderMemory())
 			{
-				//const float rc = mRenderCostDecrease / hm->GetHierarchyStats().mTotalCost;
-				const float rc = mPriority / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
+				const float rc = mRenderCostDecrease / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
 				//const float mc = mMemoryIncr /  / hm->GetHierarchyStats().mMemory;
+				//const float rc = mPriority / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
 				const float mc = (float)mPvsEntriesIncr / (float)hm->GetHierarchyStats().mPvsEntries;	
-			cout << "vsp rc: " << rc << " mc: " << mc << endl;
+				
+				//cout << "\np: " << mPriority << " i: " << hm->mInitialRenderCost << " t: " << hm->GetHierarchyStats().mTotalCost << endl;
+				//cout << "vsp rc: " << rc << " mc: " << mc << endl;
+				
 				return hm->GetMemoryConst() * rc - (1.0f - hm->GetMemoryConst()) * mc;
 			}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1676)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1679)
@@ -44,10 +44,21 @@
 /***************************************************************/
 
-BvhNode::BvhNode(): mParent(NULL), mMailbox(0)
+BvhNode::BvhNode(): 
+mParent(NULL), 
+mMailbox(0),
+mRenderCostDecr(0), 
+mMemoryIncr(0),
+mPvsEntriesIncr(0),
+mTimeStamp(0)
 {
 }
 
 BvhNode::BvhNode(const AxisAlignedBox3 &bbox):
-mParent(NULL), mBoundingBox(bbox), mMailbox(0)
+mParent(NULL), 
+mBoundingBox(bbox), 
+mMailbox(0),
+mMemoryIncr(0),
+mPvsEntriesIncr(0),
+mTimeStamp(0)
 {
 }
@@ -55,5 +66,10 @@
 
 BvhNode::BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent):
-mBoundingBox(bbox), mParent(parent), mMailbox(0)
+mBoundingBox(bbox), 
+mParent(parent), 
+mMailbox(0),
+mMemoryIncr(0),
+mPvsEntriesIncr(0),
+mTimeStamp(0)
 {
 }
@@ -400,4 +416,10 @@
 	backData.mMaxCostMisses = sc.GetMaxCostMisses();
 	
+	// set the time stamp so the order of traversal can be reconstructed
+	node->mTimeStamp = mHierarchyManager->mTimeStamp ++;
+	cout << "here4 " << node->mTimeStamp << endl;
+	node->mRenderCostDecr = sc.GetRenderCostDecrease();
+	node->mPvsEntriesIncr = sc.GetPvsEntriesIncr();
+
 	// assign the objects in sorted order
 	if (mUseGlobalSorting)
@@ -491,9 +513,9 @@
 	if (computeSplitPlane)
 	{
-		const bool sufficientSamples = splitCandidate.mParentData.mNumRays < mMinRaysForVisibility;
+		const bool sufficientSamples = splitCandidate.mParentData.mNumRays > mMinRaysForVisibility;
 
 		const bool useVisibiliyBasedHeuristics = 
 			!mUseSah &&
-			!(mHierarchyManager->GetViewSpaceSubdivisionType() == HierarchyManager::KD_BASED_VIEWSPACE_SUBDIV) &&
+			(mHierarchyManager->GetViewSpaceSubdivisionType() == HierarchyManager::KD_BASED_VIEWSPACE_SUBDIV) &&
 			sufficientSamples;
 
@@ -1259,23 +1281,16 @@
 				if (useVisibilityBasedHeuristics)
 				{
+					//cout << "v";
 					///////////
 					//-- heuristics using objects weighted by view cells volume
 					nCostRatio[axis] =
-						EvalLocalCostHeuristics(
-												tData,
-												axis,
-												nFrontObjects[axis],
-												nBackObjects[axis]);
+						EvalLocalCostHeuristics(tData, axis, nFrontObjects[axis], nBackObjects[axis]);
 				}
 				else
-				{
+				{//cout << "e";
 					//////////////////
 					//-- view cells not constructed yet	=> use surface area heuristic			
 					nCostRatio[axis] =
-						EvalSah(
-								tData,
-								axis,
-								nFrontObjects[axis],
-								nBackObjects[axis]);
+						EvalSah(tData, axis, nFrontObjects[axis], nBackObjects[axis]);
 				}
 			}
@@ -1284,9 +1299,5 @@
 				//-- split objects using some simple criteria
 				nCostRatio[axis] =
-					EvalLocalObjectPartition(
-											 tData,
-											 axis,
-											 nFrontObjects[axis],
-											 nBackObjects[axis]);
+					EvalLocalObjectPartition(tData, axis, nFrontObjects[axis], nBackObjects[axis]);
 			}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1676)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1679)
@@ -615,15 +615,15 @@
 	{
 		//vm->ResetViewCells();
-//hack
-vm->mViewCells.clear();
-			ViewCellContainer leaves;
-	vm->mViewCellsTree->CollectLeaves(vm->mViewCellsTree->GetRoot(), leaves);
-
-	ViewCellContainer::const_iterator it, it_end = leaves.end();
-
-	for (it = leaves.begin(); it != it_end; ++ it)
-	{
-		vm->mViewCells.push_back(*it);
-	}
+		//hack
+		vm->mViewCells.clear();
+		ViewCellContainer leaves;
+		vm->mViewCellsTree->CollectLeaves(vm->mViewCellsTree->GetRoot(), leaves);
+
+		ViewCellContainer::const_iterator it, it_end = leaves.end();
+
+		for (it = leaves.begin(); it != it_end; ++ it)
+		{
+			vm->mViewCells.push_back(*it);
+		}
 		vm->mViewCellsFinished = true;
 		vm->mMaxPvsSize = (int)objects->size();
@@ -856,4 +856,10 @@
 
 
+void ViewCellsManager::ExportStats(const string &fileName)
+{
+	mViewCellsTree->ExportStats(fileName);
+}
+
+
 void ViewCellsManager::EvalViewCellPartition()
 {
@@ -949,5 +955,5 @@
 		string fileName = string(statsPrefix) + string(str);
 
-		mViewCellsTree->ExportStats(fileName);
+		ExportStats(fileName);
 
 		timeDiff = TimeDiff(startTime, GetTime());
@@ -2285,44 +2291,44 @@
   //Vector3 center = viewCell->GetBox().Center();
   //  Vector3 center = m->mBox.Center();
-  
-  //  AxisAlignedBox3 box(center - Vector3(viewSpaceFilterSize/2),
-  //					  center + Vector3(viewSpaceFilterSize/2));
+
+	//  AxisAlignedBox3 box(center - Vector3(viewSpaceFilterSize/2),
+	//					  center + Vector3(viewSpaceFilterSize/2));
 	if (!ViewCellsConstructed())
 		return;
 
-  	if (viewSpaceFilterSize >= 0.0f) {
-
-  const bool usePrVS = false;
-
-  if (!usePrVS) {
-	AxisAlignedBox3 box = GetViewCellBox(viewCell);
-	box.Enlarge(Vector3(viewSpaceFilterSize/2));
-	
-	ViewCellContainer viewCells;
-	ComputeBoxIntersections(box, viewCells);
-	
-  //  cout<<"box="<<box<<endl;
-	ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
-	
-	int i;
-	for (i=0; it != it_end; ++ it, ++ i) {
-	  	//cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
-	  pvs.Merge((*it)->GetPvs());
-	}
-  } else {
-	PrVs prvs;
-	AxisAlignedBox3 box = GetViewCellBox(viewCell);
-
-	//  mViewCellsManager->SetMaxFilterSize(1);
-	GetPrVS(box.Center(), prvs, viewSpaceFilterSize);
-	pvs = prvs.mViewCell->GetPvs();
-	DeleteLocalMergeTree(prvs.mViewCell);
-  }
-  } else
-	pvs = viewCell->GetPvs();
-   
-  if (spatialFilterSize >=0.0f)
-	ApplySpatialFilter(kdTree, spatialFilterSize, pvs);
-  
+	if (viewSpaceFilterSize >= 0.0f) {
+
+		const bool usePrVS = false;
+
+		if (!usePrVS) {
+			AxisAlignedBox3 box = GetViewCellBox(viewCell);
+			box.Enlarge(Vector3(viewSpaceFilterSize/2));
+
+			ViewCellContainer viewCells;
+			ComputeBoxIntersections(box, viewCells);
+
+			//  cout<<"box="<<box<<endl;
+			ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
+
+			int i;
+			for (i=0; it != it_end; ++ it, ++ i) {
+				//cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
+				pvs.Merge((*it)->GetPvs());
+			}
+		} else {
+			PrVs prvs;
+			AxisAlignedBox3 box = GetViewCellBox(viewCell);
+
+			//  mViewCellsManager->SetMaxFilterSize(1);
+			GetPrVS(box.Center(), prvs, viewSpaceFilterSize);
+			pvs = prvs.mViewCell->GetPvs();
+			DeleteLocalMergeTree(prvs.mViewCell);
+		}
+	} else
+		pvs = viewCell->GetPvs();
+
+	if (spatialFilterSize >=0.0f)
+		ApplySpatialFilter(kdTree, spatialFilterSize, pvs);
+
 }
 
@@ -2339,35 +2345,33 @@
 		return;
 
-  ViewCellContainer::const_iterator it, it_end = mViewCells.end();
-
-  ObjectPvs *newPvs;
-  newPvs = new ObjectPvs[mViewCells.size()];
-
-  float viewSpaceFilterSize = Magnitude(mViewSpaceBox.Size())*relViewSpaceFilterSize;
-  float spatialFilterSize = Magnitude(kdTree->GetBox().Size())*relSpatialFilterSize;
-  
-  int i;
-  for (i=0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
-	ApplyFilter(*it,
-				kdTree,
-				viewSpaceFilterSize,
-				spatialFilterSize,
-				newPvs[i]
-				);
-  }
-
-  // now replace all pvss
-  for (i = 0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
-	    
-	ObjectPvs &pvs = (*it)->GetPvs();
-	pvs.Clear();
-	pvs = newPvs[i];
-	newPvs[i].Clear();
-  }
-  
-  delete [] newPvs;
-}
-
-
+	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+
+	ObjectPvs *newPvs;
+	newPvs = new ObjectPvs[mViewCells.size()];
+
+	float viewSpaceFilterSize = Magnitude(mViewSpaceBox.Size())*relViewSpaceFilterSize;
+	float spatialFilterSize = Magnitude(kdTree->GetBox().Size())*relSpatialFilterSize;
+
+	int i;
+	for (i=0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
+		ApplyFilter(*it,
+			kdTree,
+			viewSpaceFilterSize,
+			spatialFilterSize,
+			newPvs[i]
+			);
+	}
+
+	// now replace all pvss
+	for (i = 0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
+
+		ObjectPvs &pvs = (*it)->GetPvs();
+		pvs.Clear();
+		pvs = newPvs[i];
+		newPvs[i].Clear();
+	}
+
+	delete [] newPvs;
+}
 
 
@@ -2379,10 +2383,10 @@
 									 )
 {
-  // now compute a new Pvs by including also objects intersecting the 
-  // extended boxes of visible objects
-  Intersectable::NewMail();
-  
- ObjectPvsMap::const_iterator oi;
-  
+	// now compute a new Pvs by including also objects intersecting the 
+	// extended boxes of visible objects
+	Intersectable::NewMail();
+
+	ObjectPvsMap::const_iterator oi;
+
   for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++ oi) 
   {
@@ -5619,6 +5623,26 @@
 
 typedef priority_queue<BvhNode *, vector<BvhNode *>, 
-					   myless<vector<BvhNode *>::value_type> > ObjectsQueue;
-
+					   myless<vector<BvhNode *>::value_type> > BvhQueue;
+
+typedef priority_queue<VspNode *, vector<VspNode *>, 
+					   myless<vector<VspNode *>::value_type> > VspQueue;
+
+static void UpdateStats(ofstream &stats,
+						const int splits,
+						const float totalRenderCost,
+						const int entriesInPvs,
+						const float memoryCost,
+						const bool vsp)
+{
+	stats << "#Pass\n" << 0 << endl
+		   << "#Splits\n" << splits << endl 
+		   << "#TotalRenderCost\n" << totalRenderCost << endl
+		   << "#TotalEntriesInPvs\n" << entriesInPvs << endl
+		   << "#Memory\n" << memoryCost << endl
+		   << "#Vsp\n" << (vsp ? 1 : 0) << endl
+		   << endl;
+}
+
+/*
 void VspOspViewCellsManager::ExportStats(const string &mergeStats)
 {
@@ -5626,48 +5650,25 @@
 	stats.open(mergeStats.c_str());
 
-	ObjectsQueue objectsQueue;
-	ViewCellsQueue viewCellsQueue;
-
-	objectsQueue.push(mHierarchyManager->mBvHierarchy->GetRoot());
-	viewCellsQueue.push(mViewCellsTree->GetRoot());
-	
-	int numViewCells = 1;
+	BvhQueue bvhQueue;
+	VspQueue vspQueue;
+	//ViewCellsQueue viewCellsQueue;
+
+	bvhQueue.push(mHierarchyManager->mBvHierarchy->GetRoot());
+	vspQueue.push(mHierarchyManager->GetVspTree()->GetRoot());
+	
+	int numSteps = 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;
-
+	float totalRenderCost = (float)mPreprocessor->mObjects.size();//(float)mViewCellsTree->GetPvsSize(mViewCellsTree->GetRoot());
+	int entriesInPvs = 1;//mViewCellsTree->GetPvsEntries(mViewCellsTree->GetRoot());
+
+	cout << "exporting vsposp stats ... " << endl;
 	const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();
 
 	/////////////
 	//-- first view cell
-
-/*	mViewCellsTree->UpdateStats(stats,	
-								0, 
-								numViewCells, 
-								0, 
-								totalRenderCost, 
-								rootPvs, 
-								expectedCost, 
-								avgRenderCost, 
-								deviation,
-								totalPvs, 
-								entriesInPvs,
-								memoryCost,
-								0, 
-								mRoot->GetVolume());
-*/		
+	UpdateStats(stats, 2, totalRenderCost, entriesInPvs, memoryCost, true);
 
 	//-- go through tree in the order of render cost decrease
@@ -5675,82 +5676,79 @@
 	//-- 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()) 
+	int i = 0;
+	while (!(bvhQueue.empty() && vspQueue.empty()))
+	{
+		const float vspCost = !vspQueue.empty() ? vspQueue.top()->GetMergeCost() : -99999999.0;
+		const float ospCost = !bvhQueue.empty() ? bvhQueue.top()->GetMergeCost() : -99999999.0;
+
+		if (vspCost > ospCost)
+		{
+			cout << "vsp: " << (int)-vspCost << endl;
+			cout << "v";
+			VspNode *vspNode = vspQueue.top();
+
+			if (!vspNode)
+			{
+				cerr << "big error!" << endl;
+				break;
+			}
+			vspQueue.pop();
+
+			if (!vspNode->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 memoryCost = (float)entriesInPvs * 
-					(float)ObjectPvs::GetEntrySize();
-
-				mViewCellsTree->UpdateStats(stats,
-											0, 
-											numViewCells, 
-											costDecr, 
-											totalRenderCost,
-											parentPvs, 
-											expectedCost, 
-											avgRenderCost, 
-											deviation,
-											totalPvs, 
-											entriesInPvs, 
-											memoryCost,
-											childPvs - parentPvs,
-											vc->GetVolume());
-				*/
+				totalRenderCost -= vspNode->mRenderCostDecr;
+				entriesInPvs += vspNode->mPvsEntriesIncr;		
+
+				i ++;
+
+				const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();	
+				UpdateStats(stats, i, totalRenderCost, entriesInPvs, memoryCost, true);
+
+				VspNode *front = dynamic_cast<VspInterior *>(vspNode)->GetFront();
+				VspNode *back= dynamic_cast<VspInterior *>(vspNode)->GetBack();
+
+				if (!front->IsLeaf())
+					vspQueue.push(front);
+				if (!back->IsLeaf())
+					vspQueue.push(back);
 			}
-			else // object space split
+		}
+		else // object space split
+		{
+			cout << "o";
+			BvhNode *bvhNode = bvhQueue.top();
+			cout << "bvh: " << (int)-ospCost << endl;
+			if (!bvhNode)
 			{
+				cerr << "big error!" << endl;
+				break;
 			}
+			bvhQueue.pop();
+
+			if (!bvhNode->IsLeaf()) 
+			{	
+				totalRenderCost -= bvhNode->mRenderCostDecr;
+				entriesInPvs += bvhNode->mPvsEntriesIncr;		
+
+				i ++;
+				const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();	
+				UpdateStats(stats, i, totalRenderCost, entriesInPvs, memoryCost, false);
+				
+				BvhNode *front = dynamic_cast<BvhInterior *>(bvhNode)->GetFront();
+				BvhNode *back= dynamic_cast<BvhInterior *>(bvhNode)->GetBack();
+
+				if (!front->IsLeaf())
+					bvhQueue.push(front);
+				if (!back->IsLeaf())
+					bvhQueue.push(back);
+			}
+		}
+		cout << i << endl;
+			
 	}
 
 	stats.close();
 }
-
+*/
 
 #if TEST_EVALUATION
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1677)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1679)
@@ -198,6 +198,13 @@
 	{ mBoundingBox = boundingBox; }
 
-
-	float GetMergeCost() {return 0.0f; };
+	/** Cost of mergin this node.
+	*/
+	float GetMergeCost() {return (float)-mTimeStamp; }
+
+	int mTimeStamp;
+
+	int mPvsEntriesIncr;
+	float mMemoryIncr;
+	float mRenderCostDecr;
 
 	/////////////////////////////////////
@@ -262,5 +269,6 @@
 		return s << A.mBoundingBox;
 	}
-virtual void CollectObjects(ObjectContainer &objects);
+
+	virtual void CollectObjects(ObjectContainer &objects);
 protected:
 
@@ -444,10 +452,12 @@
 			if (hm->ConsiderMemory())
 			{
-				//const float rc = mRenderCostDecrease / hm->GetHierarchyStats().mTotalCost;
-				const float rc = mPriority / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
+				const float rc = mRenderCostDecrease / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
+				//const float mc = mMemoryIncr /  / hm->GetHierarchyStats().mMemory;
+                //const float rc = mPriority / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
+				const float mc = (float)mPvsEntriesIncr / (float)hm->GetHierarchyStats().mPvsEntries;	
+				
 				//cout << "\np: " << mPriority << " i: " << hm->mInitialRenderCost << " t: " << hm->GetHierarchyStats().mTotalCost << endl;
-				//const float mc = mMemoryIncr /  / hm->GetHierarchyStats().mMemory;
-				const float mc = (float)mPvsEntriesIncr / (float)hm->GetHierarchyStats().mPvsEntries;	
-				cout << "osp rc: " << rc << " mc: " << mc << endl;
+				//cout << "osp rc: " << rc << " mc: " << mc << endl;
+
 				return hm->GetMemoryConst() * rc - (1.0f - hm->GetMemoryConst()) * mc;
 			}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1667)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1679)
@@ -238,26 +238,18 @@
 	/// parent view cell in the view cell hierarchy
 	ViewCellInterior *mParent;
-	
 	/// the potentially visible objects
 	ObjectPvs mPvs;
-	
 	/// the volume of this view cell
 	float mVolume;
-	
 	/// the area of this view cell
 	float mArea;
-
 	/// the cost that were paid for merging this view cells from two others.
 	float mMergeCost;
-	
 	/// if the view cell is valid view space
 	bool mValid;
-	
 	/// color used for consistent visualization
 	RgbColor mColor;
-	
 	/// store pvs size, used for evaluation purpose when pvss are stored only in the leaves
 	int mPvsSize;
-
 	/** stores number of entries in pvs
 	    this variable has the same value as mPvsSize for object pvs, 
@@ -265,5 +257,4 @@
 	*/
 	int mEntriesInPvs;
-
 	/** if the pvs size scalar (+ entries into pvs)
 		is up to date and corresponding to the real pvs size
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1673)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1679)
@@ -179,10 +179,19 @@
 
 VspNode::VspNode(): 
-mParent(NULL), mTreeValid(true), mTimeStamp(0)
+mParent(NULL), 
+mTreeValid(true), 
+mTimeStamp(0), 
+mRenderCostDecr(0), 
+mMemoryIncr(0),
+mPvsEntriesIncr(0)
 {}
 
 
 VspNode::VspNode(VspInterior *parent): 
-mParent(parent), mTreeValid(true)
+mParent(parent), 
+mTreeValid(true), 
+mMemoryIncr(0),
+mPvsEntriesIncr(0),
+mTimeStamp(0)
 {}
 
@@ -334,5 +343,6 @@
 
 
-VspLeaf::VspLeaf(): mViewCell(NULL), mPvs(NULL), mSubdivisionCandidate(NULL)
+VspLeaf::VspLeaf(): 
+mViewCell(NULL), mPvs(NULL), mSubdivisionCandidate(NULL)
 {
 }
@@ -711,4 +721,7 @@
 		tBackData.mMaxCostMisses = maxCostMisses;
 			
+		newNode->mRenderCostDecr = sc->GetRenderCostDecrease();
+		newNode->mPvsEntriesIncr = sc->GetPvsEntriesIncr();
+
 		mTotalCost -= sc->GetRenderCostDecrease();
 		mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
@@ -966,6 +979,6 @@
 
 	// set the time stamp so the order of traversal can be reconstructed
-	interior->mTimeStamp = mTimeStamp ++;
-	
+	interior->mTimeStamp = mHierarchyManager->mTimeStamp ++;
+	cout << "here6 " << interior->mTimeStamp << endl;
 	return interior;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1677)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1679)
@@ -319,4 +319,6 @@
 								 AxisAlignedBox3 *forcedViewSpace)
 {
+	mTimeStamp = 1;
+
 	switch (mConstructionType)
 	{
@@ -422,5 +424,5 @@
 	// then optimizate view space partition for the current osp
 	// and vice versa until iteration depth is reached.
-
+bool lastSplitWasOsp = true;
 	while (!(viewSpaceQueue.Empty() && objectSpaceQueue.Empty()))
 	{
@@ -432,6 +434,8 @@
 
 		// should view or object space be subdivided further?
-		if (ospPriority >= vspPriority)
-		{
+		//if (ospPriority >= vspPriority)
+		if (!lastSplitWasOsp)
+		{
+			lastSplitWasOsp = true;
 			cout << "osp" << endl;
 			// dirtied view space candidates
@@ -456,4 +460,5 @@
 		else
 		{
+			lastSplitWasOsp = false;
 			cout << "vsp" << endl;
 			
@@ -912,5 +917,9 @@
 		const float threshold = oldCandidate ? oldCandidate->GetPriority() : 1e20;
 		SubdivisionCandidate *sc = NextSubdivisionCandidate(splitQueue); 
-		//cout << "here2 " << sc->GetPriority() << " " << threshold << endl;
+		//cout << "here2 " << sc->GetRenderCostDecrease() << " " << threshold << endl;
+		float rc = (float)sc->GetRenderCostDecrease() / (mInitialRenderCost - mHierarchyStats.mTotalCost + 1.0f);
+		float mc = (float)sc->GetPvsEntriesIncr() / (float)mHierarchyStats.mPvsEntries;	
+
+		//cout << "dRc: " << sc->GetRenderCostDecrease() << " dMem: " << sc->GetPvsEntriesIncr() << " ratio1 " << rc << " ratio2 " << mc << " const: " << 1.0f - GetMemoryConst() << endl;
 
 		// minimum slope reached
@@ -1505,5 +1514,6 @@
 
 
-void HierarchyManager::FinishObjectSpaceSubdivision(const ObjectContainer &objects, const bool removeRayRefs) const
+void HierarchyManager::FinishObjectSpaceSubdivision(const ObjectContainer &objects, 
+													const bool removeRayRefs) const
 {
 	switch (mObjectSpaceSubdivisionType)
