Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1680)
@@ -440,4 +440,9 @@
 
 
+void EvaluateSubdivision(const VssRayContainer &sampleRays,											 
+						const ObjectContainer &objects,
+						const string &filename);
+
+void ExportStats(const string &mergeStats, const ObjectContainer &objects, VspTree *oldVspTree);
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1680)
@@ -693,4 +693,8 @@
 #endif
 
+	void UpdateNode(const AxisAlignedPlane &splitPlane,
+							 VspTraversalData &tData,
+							VspTraversalData &frontData,
+							 VspTraversalData &backData);
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1680)
@@ -332,4 +332,16 @@
 
 
+void BvHierarchy::UpdateNode(const BvhSubdivisionCandidate &sc)
+{
+	BvhInterior *node = NULL;//sc->mParentData.mNode;
+	//AssociateObjectsWithLeaf(node->GetBack());
+	//AssociateObjectsWithLeaf(node->GetFront());
+   
+	node->mRenderCostDecr += sc.GetRenderCostDecrease();
+	node->mPvsEntriesIncr += sc.GetPvsEntriesIncr();
+
+}
+									
+
 BvhInterior *BvHierarchy::SubdivideNode(const BvhSubdivisionCandidate &sc,
 										BvhTraversalData &frontData,
@@ -418,5 +430,5 @@
 	// set the time stamp so the order of traversal can be reconstructed
 	node->mTimeStamp = mHierarchyManager->mTimeStamp ++;
-	cout << "here4 " << node->mTimeStamp << endl;
+	//cout << "here4 " << node->mTimeStamp << endl;
 	node->mRenderCostDecr = sc.GetRenderCostDecrease();
 	node->mPvsEntriesIncr = sc.GetPvsEntriesIncr();
@@ -1281,5 +1293,5 @@
 				if (useVisibilityBasedHeuristics)
 				{
-					//cout << "v";
+					cout << "v";
 					///////////
 					//-- heuristics using objects weighted by view cells volume
@@ -1288,5 +1300,5 @@
 				}
 				else
-				{//cout << "e";
+				{	cout << "e";
 					//////////////////
 					//-- view cells not constructed yet	=> use surface area heuristic			
@@ -1668,5 +1680,7 @@
 {
 	// rather use the simple version
-	if (!object) return NULL;
+	if (!object) 
+		return NULL;
+	
 	return object->mBvhLeaf;
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1680)
@@ -5619,137 +5619,4 @@
 
 
-typedef priority_queue<ViewCell *, vector<ViewCell *>, 
-					   myless<vector<ViewCell *>::value_type> > ViewCellsQueue;
-
-typedef priority_queue<BvhNode *, vector<BvhNode *>, 
-					   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)
-{
-	ofstream stats;
-	stats.open(mergeStats.c_str());
-
-	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();
-
-	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
-	UpdateStats(stats, 2, totalRenderCost, entriesInPvs, memoryCost, true);
-
-	//-- 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.
-	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()) 
-			{	
-				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
-		{
-			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
 void VspOspViewCellsManager::EvalViewCellPartition()
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1680)
@@ -468,7 +468,7 @@
 		}
 
-
 		/// pointer to parent tree.
 		static BvHierarchy *sBvHierarchy;
+
 		/// parent data
 		BvhTraversalData mParentData;
@@ -516,5 +516,6 @@
 	/** Evaluates candidate for splitting.
 	*/
-	void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData, bool computeSplitPlane = true);
+	void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData, 
+								  bool computeSplitPlane = true);
 
 	/** Returns list of leaves with pvs smaller than
@@ -532,10 +533,4 @@
 	*/
 	BvhNode *GetRoot() const;
-
-	/** A ray is cast possible intersecting the tree.
-		@param the ray that is cast.
-		@returns the number of intersections with objects stored in the tree.
-	*/
-	//int CastRay(Ray &ray);
 
 	/** finds neighbouring leaves of this tree node.
@@ -601,4 +596,6 @@
 	*/
 	float GetMemUsage() const;
+
+void UpdateNode(const BvhSubdivisionCandidate &sc);
 
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1680)
@@ -980,6 +980,43 @@
 	// set the time stamp so the order of traversal can be reconstructed
 	interior->mTimeStamp = mHierarchyManager->mTimeStamp ++;
-	cout << "here6 " << interior->mTimeStamp << endl;
+	
 	return interior;
+}
+
+void VspTree::UpdateNode(const AxisAlignedPlane &splitPlane,
+								 VspTraversalData &tData,
+								 VspTraversalData &frontData,
+								 VspTraversalData &backData)
+{
+	VspLeaf *leaf = dynamic_cast<VspLeaf *>(tData.mNode);
+/*
+	// replace a link from node's parent
+	if (parent)
+	{
+		parent->ReplaceChildLink(leaf, interior);
+		interior->SetParent(parent);
+	}
+	else // new root
+	{
+		mRoot = interior;
+	}
+
+	VspLeaf *frontLeaf = new VspLeaf(interior);
+	VspLeaf *backLeaf = new VspLeaf(interior);
+
+	// and setup child links
+	interior->SetupChildLinks(frontLeaf, backLeaf);
+	
+	// add bounding box
+	interior->SetBoundingBox(tData.mBoundingBox);
+
+	// set front and back leaf
+	frontData.mNode = frontLeaf;
+	backData.mNode = backLeaf;
+
+	// explicitely create front and back view cell
+	CreateViewCell(frontData, false);
+	CreateViewCell(backData, false);
+	*/
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1615)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1680)
@@ -12,5 +12,5 @@
 class KdLeaf;
 class BvhLeaf;
-
+class BvhNode;
 class Intersectable;
 struct Face;
@@ -45,7 +45,7 @@
   int mCounter;
 
-  /// pointer to the container bvh leaf
+  /// pointer to the containing bvh leaf
   BvhLeaf *mBvhLeaf;
-
+//BvhNode *mBvhLeaf;
   /// some rays piercing this intersectable
   VssRayContainer mVssRays;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1679)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1680)
@@ -1575,3 +1575,207 @@
 }
 
-}
+/*typedef priority_queue<ViewCell *, vector<ViewCell *>, 
+					   myless<vector<ViewCell *>::value_type> > ViewCellsQueue;
+
+typedef priority_queue<BvhNode *, vector<BvhNode *>, 
+					   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 HierarchyManager::EvaluateSubdivision(const VssRayContainer &sampleRays,											 
+											const ObjectContainer &objects,
+											const string &filename)
+{
+	mHierarchyStats.Reset();
+	mHierarchyStats.Start();
+	
+	mHierarchyStats.mNodes = 2;
+
+	VspTree *oldVspTree = mVspTree;
+	//BvHierarchy *oldHierarchy = mBvHierarchy;
+
+	//mBvHierarchy = new BvHierarchy();
+	//mBvHierarchy->mHierarchyManager = this;
+	
+	mVspTree = new VspTree();
+	mVspTree->mHierarchyManager = this;
+
+	// create first nodes
+	mVspTree->Initialise(sampleRays, &oldVspTree->mBoundingBox);
+	InitialiseObjectSpaceSubdivision(objects);
+
+	// hack: assume that object space can be seen from view space
+	mHierarchyStats.mTotalCost = mInitialRenderCost = (float)objects.size();
+
+	// only one entry for start
+	mHierarchyStats.mPvsEntries = 1;
+	mHierarchyStats.mMemory = (float)ObjectPvs::GetEntrySizeByte();
+
+	EvalSubdivisionStats();
+	Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
+
+	const long startTime = GetTime();
+	cout << "Constructing view space / object space tree ... \n";
+	
+	ExportStats(filename, objects, oldVspTree);
+
+	cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+	mHierarchyStats.Stop();
+	mVspTree->mVspStats.Stop();
+
+	FinishObjectSpaceSubdivision(objects, !mUseMultiLevelConstruction);
+}
+
+
+void HierarchyManager::ExportStats(const string &mergeStats,
+								   const ObjectContainer &objects,
+								   VspTree *oldVspTree)
+{
+	ofstream stats;
+	stats.open(mergeStats.c_str());
+/*
+	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();
+*/
+	float totalRenderCost = (float)objects.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
+//	UpdateStats(stats, 2, totalRenderCost, entriesInPvs, memoryCost, true);
+
+
+/*
+
+
+	//-- 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.
+	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();
+
+			vspNode->mParent->SetupChildLink();
+
+			if (!vspNode->IsLeaf()) 
+			{	
+				SubdivisionCandidate sc = new VspSubdivisionCandidate();
+
+				mVspTree->EvalPriority(sc, false);
+
+				UpdateStats(stats, splits
+				totalRenderCost -= vspNode->mRenderCostDecr;
+				entriesInPvs += vspNode->mPvsEntriesIncr;		
+
+				splits ++;
+
+				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();
+
+				RecreateLeaves();
+				if (!front->IsLeaf())
+				{
+				SuddivisionCandidate *candidate = new SubdivisionCandidate;
+				candidate->mParentData.mNode ? front;
+				candidate->mParentData.mrays = 0;
+SuddivisionCandidate *candidate2 = new SubdivisionCandidate;
+				candidate->mParentData.mNode = back;
+				candidate->mParentData.mrays = 0;
+
+
+					vspQueue.push(front);
+				if (!back->IsLeaf())
+					vspQueue.push(back);
+				}
+			}
+		}
+		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();
+}
+
+
+}
