Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1679)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1686)
@@ -377,5 +377,6 @@
 	void CollectViewCells(const int n);
 
-	/** Sets current view cells set to active, i.e., the sampling is done in this view cell set.
+	/** Sets current view cells set to active, 
+		i.e., the sampling is done in this view cell set.
 	*/
 	void SetViewCellsActive();
@@ -393,5 +394,6 @@
 	int GetMaxFilterSize() const;
 
-	/** Deletes interior nodes from the tree which have negative merge cost set (local merge).
+	/** Deletes interior nodes from the tree which have negative 
+		merge cost set (local merge).
 	*/  
 	void DeleteLocalMergeTree(ViewCell *vc) const;
@@ -495,10 +497,9 @@
 		@returns true if the sample gives a contribution to the pvs.
 	*/
-	virtual bool AddSampleToPvs(
-		Intersectable *obj, 
-		const Vector3 &hitPoint,
-		ViewCell *vc,
-		const float pdf, 
-		float &contribution) const;
+	virtual bool AddSampleToPvs(Intersectable *obj, 
+								const Vector3 &hitPoint,
+								ViewCell *vc,
+								const float pdf, 
+								float &contribution) const;
 
 	/** Exports single view cells for visualization.
@@ -512,14 +513,12 @@
 		@param visRays additional rays
 	*/
-	virtual 
-		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) = NULL;
+	virtual 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) = NULL;
 
 	
@@ -589,9 +588,8 @@
 	/** Exports the view cell partition.
 	*/
-	void ExportViewCellsForViz(
-		Exporter *exporter, 
-		const AxisAlignedBox3 *box,
-		const AxisAlignedPlane *clipPlane = NULL,
-		const bool colorCode = 0) const;
+	void ExportViewCellsForViz(Exporter *exporter, 
+							   const AxisAlignedBox3 *box,
+							   const AxisAlignedPlane *clipPlane = NULL,
+							   const bool colorCode = 0) const;
 
 	/** Sets exporter color.
@@ -701,5 +699,6 @@
 	
 	// only for debugging
-VssRayContainer storedRays;
+	VssRayContainer storedRays;
+
 	//////////////////
 	//-- visualization options
@@ -787,311 +786,4 @@
 	ViewCell *ConstructSpatialMergeTree(BspNode *root);
 
-	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:
-
-	void CollectViewCells();
-	
-	/// the BSP tree.
-	BspTree *mBspTree;
-	vector<BspRay *> mBspRays;
-
-private:
-
-	/** Constructs a spatial merge tree only 2 levels deep.
-	*/
-	ViewCell *ConstructDummyMergeTree(BspNode *root);
-
-	/** Exports visualization of the BSP splits.
-	*/
-	void ExportSplits(const ObjectContainer &objects);
-
-	/** test if subdivision is valid in terms of volume / area.
-	*/
-	void TestSubdivision();
-};
-
-
-/**
-	Manages different higher order operations on the KD type view cells.
-*/
-class KdViewCellsManager: public ViewCellsManager
-{
-
-public:
-
-	KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
-
-	int ConstructSubdivision(const ObjectContainer &objects, 
-				  const VssRayContainer &rays);
-
-	int CastLineSegment(const Vector3 &origin,
-						const Vector3 &termination,
-						ViewCellContainer &viewcells);
-
-	int PostProcess(const ObjectContainer &objects, 
-					const VssRayContainer &rays);
-
-	void Visualize(const ObjectContainer &objects,
-				   const VssRayContainer &sampleRays);
-
-	int GetType() const;
-
-	bool ViewCellsConstructed() const;
-
-	ViewCell *GenerateViewCell(Mesh *mesh) const;
-
-	/** Prints out statistics of this approach.
-	*/
-	//  virtual void PrintStatistics(ostream &s) const;
-	ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const 
-	{ return NULL; }
-
-	float GetProbability(ViewCell *viewCell);
-	
-	void CreateMesh(ViewCell *vc);
-
-	void ExportViewCellGeometry(
-		Exporter *exporter,
-		ViewCell *vc,
-		const AxisAlignedBox3 *box,
-		const AxisAlignedPlane *clipPlane = NULL
-		) const;
-
-
-	void CollectMergeCandidates(const VssRayContainer &rays, 
-								vector<MergeCandidate> &candidates);
-
-	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:
-
-	/** Collects view cells from a hierarchy.
-	*/
-	void CollectViewCells();
-
-	KdNode *GetNodeForPvs(KdLeaf *leaf);
-
-	////////////////////////////////////////
-
-	/// the BSP tree.
-	KdTree *mKdTree;
-
-	/// depth of the KD tree nodes with represent the view cells
-	int mKdPvsDepth;
-
-
-};
-
-
-/** Manages different higher order operations on the view cells.
-*/
-class VspBspViewCellsManager: public ViewCellsManager
-{
-
-public:
-
-	VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
-	~VspBspViewCellsManager();
-
-	int ConstructSubdivision(const ObjectContainer &objects, 
-				  const VssRayContainer &rays);
-
-	int PostProcess(const ObjectContainer &objects, 
-					const VssRayContainer &rays);
-
-	void Visualize(const ObjectContainer &objects,
-				   const VssRayContainer &sampleRays);
-
-	int GetType() const;
-	
-	ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
-
-	bool ViewCellsConstructed() const;
-
-	
-	int CastLineSegment(const Vector3 &origin,
-						const Vector3 &termination,
-						ViewCellContainer &viewcells);
-
-	float GetProbability(ViewCell *viewCell);
-	
-	ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
-
-	bool GetViewPoint(Vector3 &viewPoint) const;
-
-	bool ViewPointValid(const Vector3 &viewPoint) const;
-
-	void CreateMesh(ViewCell *vc);
-
-	bool ExportViewCells(
-		const string filename, 
-		const bool exportPvs, 
-		const ObjectContainer &objects);
-
-	int CastBeam(Beam &beam);
-	
-	void ExportViewCellGeometry(
-		Exporter *exporter,
-		ViewCell *vc,
-		const AxisAlignedBox3 *box,
-		const AxisAlignedPlane *clipPlane = NULL
-		) const;
-
-
-	void Finalize(ViewCell *viewCell, const bool createMesh);
-
-	void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
-
-	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:
-
-	int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
-	
-	/** Merges view cells according to some criteria
-	*/
-	void MergeViewCells(const VssRayContainer &rays, 
-						const ObjectContainer &objects);
-	
-	void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
-
-	void CollectViewCells();
-
-	/** Returns maximal depth difference of view cell 
-		leaves in tree.
-	*/
-	int GetMaxTreeDiff(ViewCell *vc) const;
-
-	/** Prepare view cells for use after loading them from disc.
-	*/
-	void PrepareLoadedViewCells();
-
-	/** Constructs merge hierarchy which corresponds to the spatial hierarchy.
-	*/
-	ViewCell *ConstructSpatialMergeTree(BspNode *root);
-
-	/// HACK for testing visibility filter functionality
-	void TestFilter(const ObjectContainer &objects);
-
-	/** Visualization of the pvs difference to exact visubility using 
-		from point queries.
-	*/
-	void VisualizeWithFromPointQueries();
-
-	/** Evaluate from point queries for the current scene.
-	*/
-	void EvalFromPointQueries();
-
-	/** Exports visualization of the BSP splits.
-	*/
-	void ExportSplits(const ObjectContainer &objects, 
-					  const VssRayContainer &rays);
-
-	/// the view space partition BSP tree.
-	VspBspTree *mVspBspTree;
-
-
-private:
-
-	/** test if subdivision is valid in terms of volume / area.
-	*/
-	void TestSubdivision();
-};
-
-#define TEST_EVALUATION 0
-
-/**
-	Manages different higher order operations on the view cells.
-*/
-class VspOspViewCellsManager: public ViewCellsManager
-{
-
-public:
-
-	VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
-	
-	~VspOspViewCellsManager();
-
-	int ConstructSubdivision(const ObjectContainer &objects,
-							 const VssRayContainer &rays);
-
-	int PostProcess(const ObjectContainer &objects, 
-					const VssRayContainer &rays);
-
-	void Visualize(const ObjectContainer &objects,
-				   const VssRayContainer &sampleRays);
-
-	int GetType() const;
-	
-	ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
-
-	bool ViewCellsConstructed() const;
-
-	
-	int CastLineSegment(const Vector3 &origin,
-						const Vector3 &termination,
-						ViewCellContainer &viewcells);
-
-	float GetProbability(ViewCell *viewCell);
-	
-	ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
-
-	bool GetViewPoint(Vector3 &viewPoint) const;
-
-	bool ViewPointValid(const Vector3 &viewPoint) const;
-
-	void CreateMesh(ViewCell *vc);
-
-	bool ExportViewCells(const string filename, 
-						 const bool exportPvs, 
-						 const ObjectContainer &objects);
-
-	int CastBeam(Beam &beam);
-
-	void Finalize(ViewCell *viewCell, const bool createMesh);
-
-	/** Stores sample contribution for kd cells or objects.
-	*/
-	virtual float ComputeSampleContribution(VssRay &ray, 
-											const bool addRays,
-											const bool storeViewCells);
-
-	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,
@@ -1105,10 +797,65 @@
 protected:
 
-#if TEST_EVALUATION
-	virtual void EvalViewCellPartition();
-#endif
-
-	/** Exports view cell geometry.
-	*/
+	void CollectViewCells();
+	
+	/// the BSP tree.
+	BspTree *mBspTree;
+	vector<BspRay *> mBspRays;
+
+private:
+
+	/** Constructs a spatial merge tree only 2 levels deep.
+	*/
+	ViewCell *ConstructDummyMergeTree(BspNode *root);
+
+	/** Exports visualization of the BSP splits.
+	*/
+	void ExportSplits(const ObjectContainer &objects);
+
+	/** test if subdivision is valid in terms of volume / area.
+	*/
+	void TestSubdivision();
+};
+
+
+/**
+	Manages different higher order operations on the KD type view cells.
+*/
+class KdViewCellsManager: public ViewCellsManager
+{
+
+public:
+
+	KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
+
+	int ConstructSubdivision(const ObjectContainer &objects, 
+							 const VssRayContainer &rays);
+
+	int CastLineSegment(const Vector3 &origin,
+						const Vector3 &termination,
+						ViewCellContainer &viewcells);
+
+	int PostProcess(const ObjectContainer &objects, 
+					const VssRayContainer &rays);
+
+	void Visualize(const ObjectContainer &objects,
+				   const VssRayContainer &sampleRays);
+
+	int GetType() const;
+
+	bool ViewCellsConstructed() const;
+
+	ViewCell *GenerateViewCell(Mesh *mesh) const;
+
+	/** Prints out statistics of this approach.
+	*/
+	//  virtual void PrintStatistics(ostream &s) const;
+	ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const 
+	{ return NULL; }
+
+	float GetProbability(ViewCell *viewCell);
+	
+	void CreateMesh(ViewCell *vc);
+
 	void ExportViewCellGeometry(Exporter *exporter,
 								ViewCell *vc,
@@ -1116,7 +863,256 @@
 								const AxisAlignedPlane *clipPlane = NULL) const;
 
+
+	void CollectMergeCandidates(const VssRayContainer &rays, 
+								vector<MergeCandidate> &candidates);
+
+	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:
+
+	/** Collects view cells from a hierarchy.
+	*/
+	void CollectViewCells();
+
+	KdNode *GetNodeForPvs(KdLeaf *leaf);
+
+	////////////////////////////////////////
+
+	/// the BSP tree.
+	KdTree *mKdTree;
+
+	/// depth of the KD tree nodes with represent the view cells
+	int mKdPvsDepth;
+
+
+};
+
+
+/** Manages different higher order operations on the view cells.
+*/
+class VspBspViewCellsManager: public ViewCellsManager
+{
+
+public:
+
+	VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
+	~VspBspViewCellsManager();
+
+	int ConstructSubdivision(const ObjectContainer &objects, 
+							 const VssRayContainer &rays);
+
+	int PostProcess(const ObjectContainer &objects, 
+					const VssRayContainer &rays);
+
+	void Visualize(const ObjectContainer &objects,
+				   const VssRayContainer &sampleRays);
+
+	int GetType() const;
+	
+	ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
+
+	bool ViewCellsConstructed() const;
+
+	
+	int CastLineSegment(const Vector3 &origin,
+						const Vector3 &termination,
+						ViewCellContainer &viewcells);
+
+	float GetProbability(ViewCell *viewCell);
+	
+	ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
+
+	bool GetViewPoint(Vector3 &viewPoint) const;
+
+	bool ViewPointValid(const Vector3 &viewPoint) const;
+
+	void CreateMesh(ViewCell *vc);
+
+	bool ExportViewCells(const string filename, 
+						 const bool exportPvs, 
+						 const ObjectContainer &objects);
+
+	int CastBeam(Beam &beam);
+	
+	void ExportViewCellGeometry(Exporter *exporter,
+								ViewCell *vc,
+								const AxisAlignedBox3 *box,
+								const AxisAlignedPlane *clipPlane = NULL) const;
+
+
+	void Finalize(ViewCell *viewCell, const bool createMesh);
+
+	void CollectMergeCandidates(const VssRayContainer &rays, 
+								vector<MergeCandidate> &candidates);
+
+	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:
+
 	int ComputeBoxIntersections(const AxisAlignedBox3 &box, 
 								ViewCellContainer &viewCells) const;
 	
+	/** Merges view cells according to some criteria
+	*/
+	void MergeViewCells(const VssRayContainer &rays, 
+						const ObjectContainer &objects);
+	
+	void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
+
+	void CollectViewCells();
+
+	/** Returns maximal depth difference of view cell 
+		leaves in tree.
+	*/
+	int GetMaxTreeDiff(ViewCell *vc) const;
+
+	/** Prepare view cells for use after loading them from disc.
+	*/
+	void PrepareLoadedViewCells();
+
+	/** Constructs merge hierarchy which corresponds to the spatial hierarchy.
+	*/
+	ViewCell *ConstructSpatialMergeTree(BspNode *root);
+
+	/// HACK for testing visibility filter functionality
+	void TestFilter(const ObjectContainer &objects);
+
+	/** Visualization of the pvs difference to exact visubility using 
+		from point queries.
+	*/
+	void VisualizeWithFromPointQueries();
+
+	/** Evaluate from point queries for the current scene.
+	*/
+	void EvalFromPointQueries();
+
+	/** Exports visualization of the BSP splits.
+	*/
+	void ExportSplits(const ObjectContainer &objects, 
+					  const VssRayContainer &rays);
+
+
+	/////////////////////////
+
+	/// the view space partition BSP tree.
+	VspBspTree *mVspBspTree;
+
+
+private:
+
+	/** test if subdivision is valid in terms of volume / area.
+	*/
+	void TestSubdivision();
+};
+
+#define TEST_EVALUATION 0
+
+/**
+	Manages different higher order operations on the view cells.
+*/
+class VspOspViewCellsManager: public ViewCellsManager
+{
+
+public:
+
+	VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
+	
+	~VspOspViewCellsManager();
+
+	int ConstructSubdivision(const ObjectContainer &objects,
+							 const VssRayContainer &rays);
+
+	int PostProcess(const ObjectContainer &objects, 
+					const VssRayContainer &rays);
+
+	void Visualize(const ObjectContainer &objects, 
+				   const VssRayContainer &sampleRays);
+
+	int GetType() const;
+	
+	ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
+
+	bool ViewCellsConstructed() const;
+
+	
+	int CastLineSegment(const Vector3 &origin,
+						const Vector3 &termination,
+						ViewCellContainer &viewcells);
+
+	float GetProbability(ViewCell *viewCell);
+	
+	ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
+
+	bool GetViewPoint(Vector3 &viewPoint) const;
+
+	bool ViewPointValid(const Vector3 &viewPoint) const;
+
+	void CreateMesh(ViewCell *vc);
+
+	bool ExportViewCells(const string filename, 
+						 const bool exportPvs, 
+						 const ObjectContainer &objects);
+
+	int CastBeam(Beam &beam);
+
+	void Finalize(ViewCell *viewCell, const bool createMesh);
+
+	/** Stores sample contribution for kd cells or objects.
+	*/
+	virtual float ComputeSampleContribution(VssRay &ray, 
+											const bool addRays,
+											const bool storeViewCells);
+
+	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);
+
+protected:
+
+//#if TEST_EVALUATION
+	virtual void EvalViewCellPartition();
+//#endif
+
+	/** 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 CollectViewCells();
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1632)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1686)
@@ -1017,6 +1017,7 @@
 			// add predefined view cell to leaf
 			viewCell = dynamic_cast<BspViewCell *>(tData.mViewCell);
-			// from now on out of bounds cell can be handled as any other cell,
-			// responsibility for deleting has been shifted
+
+			// out of bounds cell can be handled as any other view cell,
+			// responsibility for deleting it has the view cells manager.
 			if (IsOutOfBounds(viewCell))
 			{
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1684)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1686)
@@ -235,5 +235,11 @@
 	inline float GetMemoryConst() const { return mMemoryConst; }
 
-float mInitialRenderCost;
+	
+	void EvaluateSubdivision(const VssRayContainer &sampleRays,											 
+							 const ObjectContainer &objects,
+							 const string &filename);
+
+	float mInitialRenderCost;
+
 
 protected:
@@ -440,9 +446,8 @@
 
 
-void EvaluateSubdivision(const VssRayContainer &sampleRays,											 
-						const ObjectContainer &objects,
-						const string &filename);
-
-void ExportStats(const string &mergeStats, const ObjectContainer &objects, VspTree *oldVspTree, BvHierarchy *oldBvHierarchy);
+	///////////////////////////
+
+	void ExportStats(ofstream &stats, SplitQueue &tQueue, const ObjectContainer &objects);
+
 protected:
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1684)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1686)
@@ -693,10 +693,5 @@
 #endif
 
-	void UpdateNode(const AxisAlignedPlane &splitPlane,
-				    VspTraversalData &tData,
-					VspTraversalData &frontData,
-					VspTraversalData &backData);
-
-	SubdivisionCandidate *CreateSubdivisionCandidate(VspInterior *oldNode);
+	VspNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
 
 protected:
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1684)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1686)
@@ -131,8 +131,9 @@
 }
 
+
 void BvhLeaf::CollectObjects(ObjectContainer &objects)
 {
-	ObjectContainer::const_iterator oit, oit_end = objects.end();
-	for (oit = objects.begin(); oit != oit_end; ++ oit)
+	ObjectContainer::const_iterator oit, oit_end = mObjects.end();
+	for (oit = mObjects.begin(); oit != oit_end; ++ oit)
 	{
 		objects.push_back(*oit);
@@ -330,15 +331,4 @@
 	return nRays;
 }
-
-
-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();
-}
 									
 
@@ -906,8 +896,8 @@
 		else
 		{
-			// just add up areas of the object bbs
+			// just add up areas of the objects itself
 			// (as we are not sampling volumetric visibility,
 			// this should provide better heuristics
-			const float area = obox.SurfaceArea();
+			const float area = //obox.SurfaceArea();
 
 			al += area;
@@ -1905,7 +1895,7 @@
 	//-- add first candidate for object space partition	
 
-	BvhSubdivisionCandidate *oSubdivisionCandidate = 
-		new BvhSubdivisionCandidate(oData);
-
+	BvhSubdivisionCandidate *oSubdivisionCandidate = new BvhSubdivisionCandidate(oData);
+
+	// evaluate priority
 	EvalSubdivisionCandidate(*oSubdivisionCandidate);
 	bvhLeaf->SetSubdivisionCandidate(oSubdivisionCandidate);
@@ -2115,24 +2105,16 @@
 float BvHierarchy::GetMemUsage() const
 {
-	return (float)
-		 (sizeof(BvHierarchy)
-		  + mBvhStats.Leaves() * sizeof(BvhLeaf) 
-		  + mBvhStats.Interior() * sizeof(BvhInterior)
-		  ) / (1024.0f * 1024.0f);
-}
-
-
-SubdivisionCandidate *BvHierarchy::CreateSubdivisionCandidate(BvhInterior *oldNode)
-{
-	return new BvhSubdivisionCandidate(BvhTraversalData());
-}
-
-
-BvhNode *BvHierarchy::SubdivideAndCopy(SplitQueue &tQueue,
-									   SubdivisionCandidate *splitCandidate,
-									   const bool globalCriteriaMet,
-									   BvhNode *originalNode)
-{
-	BvhSubdivisionCandidate *sc = dynamic_cast<BvhSubdivisionCandidate *>(splitCandidate);
+	return (float)(sizeof(BvHierarchy)
+				   + mBvhStats.Leaves() * sizeof(BvhLeaf) 
+				   + mBvhStats.Interior() * sizeof(BvhInterior)
+				   ) / float(1024 * 1024);
+}
+
+
+BvhNode *BvHierarchy::SubdivideAndCopy(SplitQueue &tQueue, 
+									   SubdivisionCandidate *splitCandidate)
+{
+	BvhSubdivisionCandidate *sc = 
+		dynamic_cast<BvhSubdivisionCandidate *>(splitCandidate);
 	BvhTraversalData &tData = sc->mParentData;
 
@@ -2149,16 +2131,14 @@
 			
 		BvhInterior *oldInterior = dynamic_cast<BvhInterior *>(oldNode);
-		//sc->SetSplitPlane(oldInterior->mSplitPlane);
-
+		
 		oldInterior->GetFront()->CollectObjects(sc->mFrontObjects);
 		oldInterior->GetBack()->CollectObjects(sc->mBackObjects);
-		EvalSubdivisionCandidate(*sc);
+		
+		// evaluate the changes in render cost and pvs entries
+		EvalSubdivisionCandidate(*sc, false);
 
 		// create new interior node and two leaf node
 		currentNode = SubdivideNode(*sc, tFrontData, tBackData);
 	
-		// evaluate the changes in render cost and pvs entries
-		EvalSubdivisionCandidate(*sc, false);
-
 		currentNode->mRenderCostDecr = oldNode->mRenderCostDecr + sc->GetRenderCostDecrease();
 		currentNode->mPvsEntriesIncr = oldNode->mPvsEntriesIncr + sc->GetPvsEntriesIncr();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1680)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1686)
@@ -61,5 +61,5 @@
 mViewCellsFinished(false),
 mMaxPvsSize(9999999),
-mMinPvsSize(0), // one means only empty view cells are invalid
+mMinPvsSize(0),
 mMaxPvsRatio(1.0),
 mViewCellPvsIsUpdated(false),
@@ -907,15 +907,12 @@
 
 	while (castSamples < numSamples)
-	{
+	{		
+		///////////////
+		//-- we have to use uniform sampling strategy for construction rays
+
 		VssRayContainer evaluationSamples;
-
 		const int samplingType = mEvaluationSamplingType;
-		/*	dirSamples ? 
-						Preprocessor::DIRECTION_BASED_DISTRIBUTION :
-				 		Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
-		*/
+
 		long startTime = GetTime();
-
-		//-- construction rays => we use uniform samples for this
 
 		cout << "casting " << samplesPerPass << " samples ... ";
@@ -943,5 +940,11 @@
 		startTime = GetTime();
 		cout << "compute new statistics ... " << endl;
-	
+
+		///////////
+		//-- output stats
+
+		sprintf(str, "-%09d-eval.log", castSamples);
+		string fileName = string(statsPrefix) + string(str);
+
 		///////////////
 		//-- propagate pvs or pvs size information
@@ -949,9 +952,4 @@
 		ObjectPvs pvs;
 		UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
-
-		///////////
-		//-- output stats
-		sprintf(str, "-%09d-eval.log", castSamples);
-		string fileName = string(statsPrefix) + string(str);
 
 		ExportStats(fileName);
@@ -5535,4 +5533,5 @@
 			}
 
+			////////////////
 			//-- for directional sampling it is important to count only contributions 
 			//-- made in one direction!
@@ -5618,5 +5617,5 @@
 }
 
-
+#if 1
 #if TEST_EVALUATION
 void VspOspViewCellsManager::EvalViewCellPartition()
@@ -5699,4 +5698,101 @@
 }
 
+#else
+
+void VspOspViewCellsManager::EvalViewCellPartition()
+{
+	int samplesPerPass;
+	int numSamples;
+	int castSamples = 0;
+	char str[64]; 
+
+	Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
+	Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samples", numSamples);
+
+	char statsPrefix[100];
+	Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
+
+	Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
+	Debug << "view cell evaluation samples: " << numSamples << endl;
+	Debug << "view cell stats prefix: " << statsPrefix << endl;
+
+	// should directional sampling be used?
+	bool dirSamples = 
+		(mEvaluationSamplingType == SamplingStrategy::DIRECTION_BASED_DISTRIBUTION);
+
+	cout << "reseting pvs ... ";
+		
+	const bool startFromZero = true;
+
+	// reset pvs and start over from zero
+	if (startFromZero)
+	{
+		mViewCellsTree->ResetPvs();
+	}
+	else // start from current sampless
+	{
+		// statistics before casting more samples
+		cout << "compute new statistics ... ";
+		sprintf(str, "-%09d-eval.log", castSamples);
+		string fName = string(statsPrefix) + string(str);
+
+		mViewCellsTree->ExportStats(fName);
+		cout << "finished" << endl;
+	}
+
+	cout << "finished" << endl;
+    cout << "Evaluating view cell partition ... " << endl;
+
+	while (castSamples < numSamples)
+	{		
+		///////////////
+		//-- we have to use uniform sampling strategy for construction rays
+
+		VssRayContainer evaluationSamples;
+		const int samplingType = mEvaluationSamplingType;
+
+		long startTime = GetTime();
+
+		cout << "casting " << samplesPerPass << " samples ... ";
+		Debug << "casting " << samplesPerPass << " samples ... ";
+
+		CastPassSamples(samplesPerPass, samplingType, evaluationSamples);
+		
+		castSamples += samplesPerPass;
+
+		Real timeDiff = TimeDiff(startTime, GetTime());
+		Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
+		cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
+
+		cout << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
+		Debug << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
+
+		startTime = GetTime();
+
+		ComputeSampleContributions(evaluationSamples, true, false);
+
+		timeDiff = TimeDiff(startTime, GetTime());
+		cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
+		Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
+
+		startTime = GetTime();
+		cout << "compute new statistics ... " << endl;
+
+		///////////
+		//-- output stats
+
+		sprintf(str, "-%09d-eval.log", castSamples);
+		string filename = string(statsPrefix) + string(str);
+
+		mHierarchyManager->EvaluateSubdivision(evaluationSamples, mPreprocessor->mObjects, filename);
+
+		timeDiff = TimeDiff(startTime, GetTime());
+		cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
+		Debug << "statistis compted in " << timeDiff * 1e-3 << " secs" << endl;
+	
+		disposeRays(evaluationSamples, NULL);
+	}
+}
 #endif
-}
+#endif
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1684)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1686)
@@ -604,12 +604,5 @@
 	// hacks in order to provide interleaved heurisitcs
 
-	void UpdateNode(const BvhSubdivisionCandidate &sc);
-
-	SubdivisionCandidate *CreateSubdivisionCandidate(BvhInterior *oldNode);
-
-	BvhNode *SubdivideAndCopy(SplitQueue &tQueue,
-							  SubdivisionCandidate *splitCandidate,
-							  const bool globalCriteriaMet,
-							  BvhNode *originalNode);
+	BvhNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
 
 	/////////////////////////////////
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1684)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1686)
@@ -721,7 +721,10 @@
 		tBackData.mMaxCostMisses = maxCostMisses;
 			
-		newNode->mRenderCostDecr = sc->GetRenderCostDecrease();
-		newNode->mPvsEntriesIncr = sc->GetPvsEntriesIncr();
-
+		//newNode->mRenderCostDecr = sc->GetRenderCostDecrease();
+		//newNode->mPvsEntriesIncr = sc->GetPvsEntriesIncr();
+
+		// set the time stamp so the order of traversal can be reconstructed
+		newNode->mTimeStamp = mHierarchyManager->mTimeStamp ++;
+	
 		mTotalCost -= sc->GetRenderCostDecrease();
 		mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
@@ -978,45 +981,5 @@
 #endif
 
-	// set the time stamp so the order of traversal can be reconstructed
-	interior->mTimeStamp = mHierarchyManager->mTimeStamp ++;
-	
 	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);
-	*/
 }
 
@@ -3321,8 +3284,106 @@
 
 
-SubdivisionCandidate *VspTree::CreateSubdivisionCandidate(VspInterior *oldNode)
-{
-	return new VspSubdivisionCandidate(oldNode->GetPlane(), VspTraversalData());
-}
-
-}
+VspNode *VspTree::SubdivideAndCopy(SplitQueue &tQueue,
+								   SubdivisionCandidate *splitCandidate)
+{
+	// todo remove dynamic cast
+	VspSubdivisionCandidate *sc = dynamic_cast<VspSubdivisionCandidate *>(splitCandidate);
+
+	VspTraversalData &tData = sc->mParentData;
+	VspNode *newNode = tData.mNode;
+	VspNode *oldNode = (VspNode *)splitCandidate->mEvaluationHack;
+
+	if (!oldNode->IsLeaf())
+	{	
+		///////////
+		//-- continue subdivision
+
+		VspTraversalData tFrontData;
+		VspTraversalData tBackData;
+		
+		VspInterior *oldInterior = dynamic_cast<VspInterior *>(oldNode);
+
+		// create new interior node and two leaf node
+		const AxisAlignedPlane splitPlane = oldInterior->GetPlane();
+		const int maxCostMisses = sc->GetMaxCostMisses();
+
+		newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData);
+	
+		// how often was max cost ratio missed in this branch?
+		tFrontData.mMaxCostMisses = maxCostMisses;
+		tBackData.mMaxCostMisses = maxCostMisses;
+			
+		newNode->mRenderCostDecr = oldNode->mRenderCostDecr + sc->GetRenderCostDecrease();
+		newNode->mPvsEntriesIncr = oldNode->mPvsEntriesIncr + sc->GetPvsEntriesIncr();
+
+		/////////////
+		//-- evaluate new split candidates for global greedy cost heuristics
+
+		VspSubdivisionCandidate *frontCandidate = new VspSubdivisionCandidate(tFrontData);
+		VspSubdivisionCandidate *backCandidate = new VspSubdivisionCandidate(tBackData);
+
+		frontCandidate->SetPriority((float)-oldInterior->GetFront()->mTimeStamp);
+		backCandidate->SetPriority((float)-oldInterior->GetFront()->mTimeStamp);
+
+		frontCandidate->mEvaluationHack = oldInterior->GetFront();
+		backCandidate->mEvaluationHack = oldInterior->GetBack();
+
+		// cross reference
+		tFrontData.mNode->SetSubdivisionCandidate(frontCandidate); 
+		tBackData.mNode->SetSubdivisionCandidate(backCandidate);
+
+		tQueue.Push(frontCandidate);
+		tQueue.Push(backCandidate);
+
+		// note: leaf is not destroyed because it is needed to collect 
+		// dirty candidates in hierarchy manager
+	}
+
+	if (newNode->IsLeaf()) // subdivision terminated
+	{
+		// view cell is created during subdivision
+		VspLeaf *leaf = dynamic_cast<VspLeaf *>(newNode);
+		ViewCell *viewCell = leaf->GetViewCell();
+
+		int conSamp = 0;
+		float sampCon = 0.0f;
+
+#if 0
+		/////////////
+		//-- store pvs optained from rays
+
+		AddSamplesToPvs(leaf, *tData.mRays, sampCon, conSamp);
+
+		// update scalar pvs size value
+		ObjectPvs &pvs = viewCell->GetPvs();
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
+
+		mVspStats.contributingSamples += conSamp;
+		mVspStats.sampleContributions += (int)sampCon;
+#endif
+		if (mStoreRays)
+		{
+			//////////
+			//-- store rays piercing this view cell
+			RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
+			for (it = tData.mRays->begin(); it != it_end; ++ it)
+			{
+				(*it).mRay->Ref();			
+				leaf->mVssRays.push_back((*it).mRay);
+				//leaf->mVssRays.push_back(new VssRay(*(*it).mRay));
+			}
+		}
+
+		// finally evaluate statistics for this leaf
+		EvaluateLeafStats(tData);
+		// detach subdivision candidate: this leaf is no candidate for
+		// splitting anymore
+		tData.mNode->SetSubdivisionCandidate(NULL); 
+		// detach node so it won't get deleted
+		tData.mNode = NULL;
+	}
+
+	return newNode;
+}
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1680)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1686)
@@ -110,5 +110,5 @@
   virtual int NumberOfFaces() const = 0;
   virtual int Type() const = 0;
-  
+  virtual float GetArea() const {return 0; }
   virtual int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal) = 0;
   
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1684)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1686)
@@ -361,5 +361,5 @@
 
 
-void HierarchyManager::ConstructInterleavedWithGradient(const VssRayContainer &sampleRays,											 
+void HierarchyManager::ConstructInterleavedWithGradient(const VssRayContainer &sampleRays,
 														const ObjectContainer &objects,
 														AxisAlignedBox3 *forcedViewSpace)
@@ -456,6 +456,6 @@
 	{
 		// decide upon next split type
-		const float vspPriority =  viewSpaceQueue.Top() ? viewSpaceQueue.Top()->GetPriority() : -1e20;
-		const float ospPriority =  objectSpaceQueue.Top() ? objectSpaceQueue.Top()->GetPriority() : -1e20;
+		const float vspPriority =  viewSpaceQueue.Top() ? viewSpaceQueue.Top()->GetPriority() : -1e20f;
+		const float ospPriority =  objectSpaceQueue.Top() ? objectSpaceQueue.Top()->GetPriority() : -1e20f;
 		
 		cout << "new decicion, vsp: " << vspPriority << ", osp: " << ospPriority << endl;
@@ -945,5 +945,5 @@
 	while (!splitQueue.Empty())
 	{
-		const float threshold = oldCandidate ? oldCandidate->GetPriority() : 1e20;
+		const float threshold = oldCandidate ? oldCandidate->GetPriority() : 1e20f;
 		SubdivisionCandidate *sc = NextSubdivisionCandidate(splitQueue); 
 		
@@ -1630,21 +1630,18 @@
 
 
-void HierarchyManager::EvaluateSubdivision(const VssRayContainer &sampleRays,											 
-											const ObjectContainer &objects,
-											const string &filename)
-{
-	mHierarchyStats.Reset();
-	mHierarchyStats.Start();
-	
-	mHierarchyStats.mNodes = 2;
-
+void HierarchyManager::EvaluateSubdivision(const VssRayContainer &sampleRays,											 							   const ObjectContainer &objects,
+										   const string &filename)
+{
 	VspTree *oldVspTree = mVspTree;
+	ViewCellsManager *vm = mVspTree->mViewCellsManager;
 	BvHierarchy *oldHierarchy = mBvHierarchy;
 
 	mBvHierarchy = new BvHierarchy();
 	mBvHierarchy->mHierarchyManager = this;
-	
+	mBvHierarchy->mViewCellsManager = vm;
+
 	mVspTree = new VspTree();
 	mVspTree->mHierarchyManager = this;
+	mVspTree->mViewCellsManager = vm;
 
 	// create first nodes
@@ -1652,35 +1649,9 @@
 	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, oldHierarchy);
-
-	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,
-								   BvHierarchy *oldHierarchy)
-{
+	cout << "Constructing evaluation hierarchies ... \n";
+	
 	ofstream stats;
-	stats.open(mergeStats.c_str());
+	stats.open(filename.c_str());
 	SplitQueue tQueue;
 
@@ -1688,22 +1659,52 @@
 	VspNode *oldVspRoot = oldVspTree->GetRoot();
 
-	SubdivisionCandidate *firstVsp = mVspTree->CreateSubdivisionCandidate(dynamic_cast<VspInterior *>(oldVspRoot));
-	SubdivisionCandidate *firstBvh = mBvHierarchy->CreateSubdivisionCandidate(dynamic_cast<BvhInterior *>(oldBvhRoot));
-
-/*
-	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());
+	RayInfoContainer *viewSpaceRays = new RayInfoContainer();
+	
+	SubdivisionCandidate *firstVsp = mVspTree->PrepareConstruction(sampleRays, *viewSpaceRays);
+	SubdivisionCandidate *firstBvh = mBvHierarchy->PrepareConstruction(sampleRays, objects);
+
+    firstVsp->mEvaluationHack = oldVspRoot;
+	firstBvh->mEvaluationHack = oldBvhRoot;
+
+	tQueue.Push(firstVsp);
+	tQueue.Push(firstBvh);
+
+	ExportStats(stats, tQueue, objects);
+
+	cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+	RemoveRayRefs(objects);
+
+	// view cells needed only for evaluation
+	ViewCellContainer viewCells;
+	mVspTree->CollectViewCells(viewCells, false);
+	CLEAR_CONTAINER(viewCells);
+
+	// helper trees can be destroyed
+	DEL_PTR(mVspTree);
+	DEL_PTR(mBvHierarchy);
+
+	// reset hierarchies
+	mVspTree = oldVspTree;
+	mBvHierarchy = oldHierarchy;
+
+	// reinstall old bv refs
+	vector<BvhLeaf *> leaves;
+	mBvHierarchy->CollectLeaves(leaves);
+	vector<BvhLeaf *>::const_iterator bit, bit_end = leaves.end();
+
+	for (bit = leaves.begin(); bit != bit_end; ++ bit)
+	{
+		mBvHierarchy->AssociateObjectsWithLeaf(*bit);
+	}
+}
+
+
+void HierarchyManager::ExportStats(ofstream &stats, 
+								   SplitQueue &tQueue, 
+								   const ObjectContainer &objects)
+{
+	float totalRenderCost = (float)objects.size();
+	int entriesInPvs = 1;
+	int steps = 0;
 
 	cout << "exporting vsposp stats ... " << endl;
@@ -1718,5 +1719,5 @@
 	//-- or the reverse order of subdivision for subdivision-only 
 	//-- view cell hierarchies.
-	int steps = 0;
+	
 	while (!tQueue.Empty())
 	{
@@ -1724,5 +1725,9 @@
 		
 		cout << "next candidate: " << nextCandidate->GetPriority() << endl;
-		nextCandidate->Apply(tQueue, false);
+
+		if (nextCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
+			mVspTree->SubdivideAndCopy(tQueue, nextCandidate);
+		else
+			mBvHierarchy->SubdivideAndCopy(tQueue, nextCandidate);
 
 		totalRenderCost -= nextCandidate->GetRenderCostDecrease();
@@ -1733,167 +1738,9 @@
 		const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();
 		UpdateStats(stats, steps, totalRenderCost, entriesInPvs, memoryCost, false);
-
-		/*if (nextCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
-		{
-			cout << "v";
-			VspNode *vspNode = NULL;
-
-			//vspNode->mParent->SetupChildLink();
-			if (!vspNode->IsLeaf()) 
-			{	
-				SubdivisionCandidate sc = new VspSubdivisionCandidate();
-
-				mVspTree->EvalPriority(sc, false);
-
-				UpdateStats(stats, splits
-				totalRenderCost -= vspNode->mRenderCostDecr;
-				entriesInPvs += vspNode->mPvsEntriesIncr;		
-
-				steps ++;
-
-				const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();	
-				UpdateStats(stats, steps, 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 = NULL;
-		
-			if (!bvhNode->IsLeaf()) 
-			{	
-				totalRenderCost -= bvhNode->mRenderCostDecr;
-				entriesInPvs += bvhNode->mPvsEntriesIncr;		
-
-				steps ++;
-				const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();	
-				UpdateStats(stats, steps, 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);
-			}
-		}*/
-	}
+	}
+
 	stats.close();
 }
 
-	/*
-	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;
-			
-	}
-*/
-
-
-}
+	
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1615)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1686)
@@ -186,4 +186,5 @@
 	Vector3 GetNormal(const int idx) const;
 
+	float GetArea() const {return mItem.GetArea();}
 	int Type() const
 	{
@@ -193,9 +194,8 @@
 	int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
 
-	int GetRandomVisibleSurfacePoint(
-		Vector3 &point,
-		Vector3 &normal,
-		const Vector3 &viewpoint,
-		const int maxTries);
+	int GetRandomVisibleSurfacePoint(Vector3 &point,
+									 Vector3 &normal,
+									 const Vector3 &viewpoint,
+									 const int maxTries);
 };
 
