Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1667)
@@ -12,6 +12,6 @@
 
 #SCENE=vsposp_typical
-#SCENE=soda5_vsposp
-SCENE=soda_vsposp
+SCENE=soda5_vsposp
+#SCENE=soda_vsposp
 #SCENE=vienna_vsposp
 #SCENE=arena_vsposp
@@ -64,5 +64,5 @@
  echo "$SCENE $METHOD"
 
-#  $PROGRAM $ENVIRONMENT \
+  $PROGRAM $ENVIRONMENT \
   -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
   -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
@@ -71,6 +71,7 @@
   -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
   -hierarchy_construction_type=2 \
-  -hierarchy_construction_consider_memory=true \
+  -hierarchy_construction_consider_memory=false \
   -hierarchy_construction_multilevel=false \
+  -hierarchy_term_memory_const=0.9 \
   -hierarchy_construction_recompute_split_on_repair=true
 
@@ -102,5 +103,5 @@
   echo "$SCENE $METHOD"
 
- $PROGRAM $ENVIRONMENT \
+ #$PROGRAM $ENVIRONMENT \
   -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
   -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
@@ -138,5 +139,5 @@
  echo ********" $SCENE $METHOD ************"
 
- $PROGRAM $ENVIRONMENT \
+# $PROGRAM $ENVIRONMENT \
  -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
  -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
@@ -161,5 +162,5 @@
  echo ********" $SCENE $METHOD ************"
 
- $PROGRAM $ENVIRONMENT \
+ #$PROGRAM $ENVIRONMENT \
  -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
  -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
@@ -184,5 +185,5 @@
 echo "$SCENE $METHOD"
 
-$PROGRAM $ENVIRONMENT \
+#$PROGRAM $ENVIRONMENT \
  -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
  -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1667)
@@ -25,9 +25,20 @@
 
 	virtual ~SubdivisionCandidate() {};
-	virtual void EvalPriority(bool computeSplitplane = true) = 0;
+	/** Evaluate this subdivision candidate.
+	*/
+	virtual void EvalCandidate(bool computeSplitplane = true) = 0;
+	/** Returns type of this subdivision candidate.
+	*/
 	virtual int Type() const = 0;
+	/** Evaluate this candidate and put results into queue for further traversal.
+	*/
 	virtual bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet) = 0;
+	/** Returns true of the global termination criteria of this split were met,
+		false otherwise.
+	*/
 	virtual bool GlobalTerminationCriteriaMet() const = 0;
-
+	/** Collects #of subdivision candidates that were affected by the
+		application of this one.
+	*/
 	virtual void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
 										const bool onlyUnmailed) = 0;
@@ -56,8 +67,10 @@
 		mSplitAxis = splitAxis;
 	}
+	
 	inline void SetMaxCostMisses(const int misses)
 	{
 		mMaxCostMisses = misses;
 	}
+
 	inline void SetPvsEntriesIncr(const int pvsEntriesIncr)
 	{
@@ -69,12 +82,15 @@
 		return mSplitAxis;
 	}
+
 	inline int GetMaxCostMisses() const
 	{
 		return mMaxCostMisses;
 	}
+
 	inline int GetPvsEntriesIncr() const
 	{
 		return mPvsEntriesIncr;
 	}
+
 
 	//////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1667)
@@ -41,4 +41,6 @@
 class BvHierarchy;
 class Exporter;
+class ViewCellsParseHandlers;
+
 
 #define COUNT_ORIGIN_OBJECTS 1
@@ -130,9 +132,4 @@
 class HierarchyManager
 {
-	friend VspTree;
-	friend OspTree;
-	friend BvHierarchy;
-	friend ViewCellsParseHandlers;
-
 public:
 	/** Constructor with the view space partition tree and
@@ -233,4 +230,8 @@
 	} 
 
+	HierarchyStatistics &GetHierarchyStats() { return mHierarchyStats; };
+
+	inline bool ConsiderMemory() const { return mConsiderMemory; }
+
 protected:
 
@@ -254,7 +255,7 @@
 	float GetObjectSpaceMemUsage() const;
 
+
 	//////////////////////////////
 	// the main loop
-	//////////////////////
 
 	/** This is for interleaved construction / sequential construction.
@@ -270,5 +271,6 @@
 	int RunConstruction(SplitQueue &splitQueue,
 						SubdivisionCandidateContainer &chosenCandidates,
-						const float minRenderCostDecr,
+						//const float minRenderCostDecr,
+						SubdivisionCandidate *oldCandidate,
 						const int minSteps);
 
@@ -344,7 +346,7 @@
 	bool StartViewSpaceSubdivision() const;
 
+
 	////////////////////////////
 	// Helper function for preparation of subdivision
-	///////
 
 	/** Prepare bv hierarchy for subdivision
@@ -493,6 +495,8 @@
 	float mTermMaxMemory;
 
+
 	////////////////////
 
+	/// number of minimal steps of the same type
 	int mMinStepsOfSameType;
 
@@ -532,4 +536,11 @@
 
 	float mMemoryConst;
+
+
+	friend VspTree;
+	friend OspTree;
+	friend BvHierarchy;
+	friend ViewCellsParseHandlers;
+
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.h	(revision 1633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.h	(revision 1667)
@@ -13,4 +13,6 @@
 #include "IntersectableWrapper.h"
 #include "KdTree.h"
+#include "HierarchyManager.h"
+
 
 
@@ -250,9 +252,8 @@
 		int Type() const { return OBJECT_SPACE; }
 	
-		void EvalPriority(bool computeSplitplane = true)
-		{
-			if (computeSplitplane)
-				sOspTree->EvalSubdivisionCandidate(*this);	
-			// else TODO
+		void EvalCandidate(bool computeSplitplane = true)
+		{
+			//if (computeSplitplane) TODO
+			sOspTree->EvalSubdivisionCandidate(*this);	
 		}
 
@@ -274,4 +275,21 @@
 		{
 			sOspTree->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
+		}
+
+		float GetPriority() const
+		{
+			HierarchyManager *hm = sOspTree->mHierarchyManager;
+			if (hm->ConsiderMemory())
+			{
+				const float rc = hm->GetHierarchyStats().mTotalCost - mRenderCostDecrease;
+				const float mc = hm->GetHierarchyStats().mMemory +
+								 (float)mPvsEntriesIncr * ObjectPvs::GetEntrySizeByte();
+
+				return - (rc * mc);
+			}
+			else
+			{
+				return mPriority;
+			}
 		}
 
@@ -698,53 +716,46 @@
 
 	void AddSubdivisionStats(const int viewCells,
-		const float renderCostDecr,
-		const float totalRenderCost);
+							 const float renderCostDecr,
+							 const float totalRenderCost);
 
 	void EvalViewCellsForHeuristics(const VssRay &ray, 
-		float &volLeft, 
-		float &volRight);
+									float &volLeft, 
+									float &volRight);
 
 	float EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const;
 	
-	int RemoveParentViewCellsPvs(KdLeaf *leaf, 
-									  const RayInfoContainer &rays
-									  ) const;
+	int RemoveParentViewCellsPvs(KdLeaf *leaf,  const RayInfoContainer &rays) const;
 
 	int UpdateViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
-int CheckViewCellsPvs(KdLeaf *leaf, 
-							   const RayInfoContainer &rays) const;
-	bool AddViewCellToObjectPvs(
-		Intersectable *obj,
-		ViewCell *vc, 
-		float &contribution,
-		bool onlyUnmailed) const;
-
-	int ClassifyRays(
-		const RayInfoContainer &rays, 
-		KdLeaf *leaf, 
-		const AxisAlignedPlane &plane,
-		RayInfoContainer &frontRays,
-		RayInfoContainer &backRays) const;
-
-	void CollectTouchedViewCells(
-		const RayInfoContainer &rays, 
-		ViewCellContainer &touchedViewCells) const;
-
-	void AddObjectContribution(
-		KdLeaf *leaf,
-		Intersectable * obj,
-		ViewCellContainer &touchedViewCells,
-		float &renderCost);
-
-	void SubtractObjectContribution(
-		KdLeaf *leaf,
-		Intersectable * obj,
-		ViewCellContainer &touchedViewCells,
-		float &renderCost);
-
-	SubdivisionCandidate *PrepareConstruction(
-		const VssRayContainer &sampleRays,
-		const ObjectContainer &objects,
-		RayInfoContainer &rays);
+	
+	int CheckViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
+
+	bool AddViewCellToObjectPvs(Intersectable *obj,
+								ViewCell *vc, 
+								float &contribution,
+								bool onlyUnmailed) const;
+
+	int ClassifyRays(const RayInfoContainer &rays, 
+					 KdLeaf *leaf, 
+					 const AxisAlignedPlane &plane,
+					 RayInfoContainer &frontRays,
+					 RayInfoContainer &backRays) const;
+
+	void CollectTouchedViewCells(const RayInfoContainer &rays, 
+								 ViewCellContainer &touchedViewCells) const;
+
+	void AddObjectContribution(KdLeaf *leaf,
+							   Intersectable * obj,
+							   ViewCellContainer &touchedViewCells,
+							   float &renderCost);
+
+	void SubtractObjectContribution(KdLeaf *leaf,
+									Intersectable * obj,
+									ViewCellContainer &touchedViewCells,
+									float &renderCost);
+
+	SubdivisionCandidate *PrepareConstruction(const VssRayContainer &sampleRays,
+											  const ObjectContainer &objects,
+											  RayInfoContainer &rays);
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1664)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1667)
@@ -11,4 +11,5 @@
 #include "gzstream.h"
 #include "SubdivisionCandidate.h"
+#include "HierarchyManager.h"
 
 
@@ -457,13 +458,7 @@
 		int Type() const { return VIEW_SPACE; }
 
-		void EvalPriority(bool computeSplitplane = true)
+		void EvalCandidate(bool computeSplitplane = true)
 		{
-			if (computeSplitplane)
-				sVspTree->EvalSubdivisionCandidate(*this);	
-			else
-			{
-				mPvsEntriesIncr = sVspTree->EvalPvsEntriesIncr(*this);
-				mPriority = sVspTree->EvalPriority(*this);
-			}
+			sVspTree->EvalSubdivisionCandidate(*this, computeSplitplane);
 		}
 
@@ -490,4 +485,21 @@
 		mSplitPlane(plane), mParentData(tData)
 		{}
+
+		float GetPriority() const
+		{
+			HierarchyManager *hm = sVspTree->mHierarchyManager;
+			if (hm->ConsiderMemory())
+			{
+				const float rc = hm->GetHierarchyStats().mTotalCost - mRenderCostDecrease;
+				const float mc = hm->GetHierarchyStats().mMemory +
+								 (float)mPvsEntriesIncr * ObjectPvs::GetEntrySizeByte();
+
+				return - (rc * mc) / float(1024 * 1024);
+			}
+			else
+			{
+				return mPriority;
+			}
+		}
 	};
 
@@ -702,5 +714,6 @@
 	/** Evaluates candidate for splitting.
 	*/
-	void EvalSubdivisionCandidate(VspSubdivisionCandidate &splitData);
+	void EvalSubdivisionCandidate(VspSubdivisionCandidate &splitData, 
+								  bool computeSplitPlane = true);
 
 	/** Evaluates render cost decrease of next split.
@@ -854,5 +867,5 @@
 		@returns priority
 	*/
-	float EvalPriority(const VspSubdivisionCandidate &splitCandidate) const;
+	//float EvalPriority(const VspSubdivisionCandidate &splitCandidate) const;
 
 	/////////////////////////////////////////////////////////////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1667)
@@ -411,6 +411,5 @@
 								const bool globalCriteriaMet)
 {
-	BvhSubdivisionCandidate *sc = 
-		dynamic_cast<BvhSubdivisionCandidate *>(splitCandidate);
+	BvhSubdivisionCandidate *sc = dynamic_cast<BvhSubdivisionCandidate *>(splitCandidate);
 	BvhTraversalData &tData = sc->mParentData;
 
@@ -439,8 +438,6 @@
 		//-- push the new split candidates on the queue
 		
-		BvhSubdivisionCandidate *frontCandidate = 
-			new BvhSubdivisionCandidate(tFrontData);
-		BvhSubdivisionCandidate *backCandidate = 
-			new BvhSubdivisionCandidate(tBackData);
+		BvhSubdivisionCandidate *frontCandidate = new BvhSubdivisionCandidate(tFrontData);
+		BvhSubdivisionCandidate *backCandidate = new BvhSubdivisionCandidate(tBackData);
 
 		EvalSubdivisionCandidate(*frontCandidate);
@@ -485,21 +482,25 @@
 
 
-void BvHierarchy::EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitCandidate)
-{
-	// compute best object partition
-	const float ratio =	SelectObjectPartition(
-							splitCandidate.mParentData, 
-							splitCandidate.mFrontObjects, 
-							splitCandidate.mBackObjects);
-	
+void BvHierarchy::EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitCandidate, 
+										   bool computeSplitPlane)
+{
+	if (computeSplitPlane)
+	{
+		// compute best object partition
+		const float ratio =	SelectObjectPartition(splitCandidate.mParentData, 
+												  splitCandidate.mFrontObjects, 
+												  splitCandidate.mBackObjects);
+	
+		// cost ratio violated?
+		const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
+
+		const int previousMisses = splitCandidate.mParentData.mMaxCostMisses;
+
+		splitCandidate.SetMaxCostMisses(maxCostRatioViolated ? 
+										previousMisses + 1 : previousMisses);
+
+	}
+
 	BvhLeaf *leaf = splitCandidate.mParentData.mNode;
-
-	// cost ratio violated?
-	const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
-
-	const int previousMisses = splitCandidate.mParentData.mMaxCostMisses;
-
-	splitCandidate.SetMaxCostMisses(maxCostRatioViolated ? 
-		previousMisses + 1 : previousMisses);
 
 	const float oldProp = EvalViewCellsVolume(leaf->mObjects);
@@ -507,7 +508,6 @@
 		
 	// compute global decrease in render cost
-	const float newRenderCost = 
-		EvalRenderCost(splitCandidate.mFrontObjects) +
-		EvalRenderCost(splitCandidate.mBackObjects);
+	const float newRenderCost = EvalRenderCost(splitCandidate.mFrontObjects) +
+								EvalRenderCost(splitCandidate.mBackObjects);
 
 	const float renderCostDecr = oldRenderCost - newRenderCost;
@@ -527,4 +527,6 @@
 	float priority;
 
+	// surface area heuristics is used when there is no view space subdivision available. 
+	// In order to have some prioritized traversal, use this formula instead
 	if (mHierarchyManager->GetViewSpaceSubdivisionType() == 
 		HierarchyManager::NO_VIEWSPACE_SUBDIV)
@@ -541,14 +543,11 @@
 	else
 	{
+		// take render cost of node into account 
+		// otherwise danger of being stuck in a local minimum!
 		const float factor = mRenderCostDecreaseWeight;
-		
 		priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
 
-		// take render cost of node into account 
-		// otherwise danger of being stuck in a local minimum!!
-		if (mHierarchyManager->mConsiderMemory)
-		{
-			//cout << "here7 rc: " << factor * renderCostDecr << " pvs: " << (1.0f - factor) * splitCandidate.GetPvsEntriesIncr() << endl;
-			//priority = factor * renderCostDecr - (1.0f - factor) * splitCandidate.GetPvsEntriesIncr();// / mBvhStats.Leaves();
+		if (1)//0 && mHierarchyManager->mConsiderMemory)
+		{
 			priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mHierarchyManager->mMemoryConst);
 		}
@@ -557,40 +556,4 @@
   	// compute global decrease in render cost
 	splitCandidate.SetPriority(priority);
-}
-
-
-float BvHierarchy::EvalPriority(const BvhSubdivisionCandidate &splitCandidate) const
-{
-	BvhLeaf *leaf = splitCandidate.mParentData.mNode;
-
-	const float oldRenderCost = EvalRenderCost(leaf->mObjects);
-
-	// compute global decrease in render cost
-	const float newRenderCost = EvalRenderCost(splitCandidate.mFrontObjects) +
-								EvalRenderCost(splitCandidate.mBackObjects);
-
-	const float renderCostDecr = oldRenderCost - newRenderCost;
-
-#ifdef _DEBUG
-	Debug << "old render cost: " << oldRenderCost << endl;
-	Debug << "new render cost: " << newRenderCost << endl;
-	Debug << "render cost decrease: " << renderCostDecr << endl;
-#endif
-
-	// take render cost of node into account 
-	// otherwise danger of being stuck in a local minimum!!
-	const float factor = mRenderCostDecreaseWeight;
-	
-	float priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
-
-	// $$ matt temp
-	if (mHierarchyManager->mConsiderMemory)
-	{
-		//cout << "here5 rc: " << factor * renderCostDecr << " pvs: " << (1.0f - factor) * splitCandidate.GetPvsEntriesIncr() << endl;
-		//const float priority = factor * renderCostDecr - (1.0f - factor) * (float)splitCandidate.GetPvsEntriesIncr();
-		priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mHierarchyManager->mMemoryConst);
-	}
-	
-	return priority;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1667)
@@ -26,6 +26,6 @@
 
 
-// should not count origin object for sampling because it disturbs heuristics
-#define SAMPLE_ORIGIN_OBJECTS 1
+// warning: Should not count origin object for sampling because it disturbs heuristics
+#define SAMPLE_ORIGIN_OBJECTS 1  // matt temp
 
 // $$JB HACK
@@ -5442,6 +5442,5 @@
 
 
-void VspOspViewCellsManager::Finalize(ViewCell *viewCell, 
-									  const bool createMesh)
+void VspOspViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
 {
 	float area = 0;
@@ -5651,6 +5650,5 @@
 	totalRenderCost = avgRenderCost = expectedCost = (float)rootPvs;
 
-	const float entrySize = sizeof(PvsData) + sizeof(Intersectable *);
-	const float memoryCost = (float)entriesInPvs *  entrySize / (1024.0f * 1024.0f);
+	const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();
 
 	/////////////
@@ -5728,6 +5726,6 @@
 				avgRenderCost = (float)totalPvs / (float)numViewCells;
 
-				const float entrySize = sizeof(PvsData) + sizeof(Intersectable *);
-				const float memoryCost = (float)entriesInPvs * entrySize / (1024.0f * 1024.0f);
+				const float memoryCost = (float)entriesInPvs * 
+					(float)ObjectPvs::GetEntrySize();
 
 				mViewCellsTree->UpdateStats(stats,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1632)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1667)
@@ -154,4 +154,5 @@
 	typedef std::priority_queue<VspBspTraversalData> VspBspTraversalQueue;
 	
+	// note: should be inherited from subdivision candidate
 	class VspBspSubdivisionCandidate
 	{  
@@ -172,5 +173,5 @@
 			return mPriority;
 #else
-			return (float) (-mDepth); // for kd tree
+			return (float) (-mDepth); // for standard breath-first traversal
 #endif
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1667)
@@ -1548,6 +1548,5 @@
 	stats.open(mergeStats.c_str());
 
-	const float entrySize = sizeof(PvsData) + sizeof(Intersectable *);
-	const float memoryCost = (float)entriesInPvs *  entrySize / (1024.0f * 1024.0f);
+	const float memoryCost = (float)entriesInPvs * ObjectPvs::GetEntrySize();
 
 	/////////////
@@ -1621,6 +1620,5 @@
 			avgRenderCost = (float)totalPvs / (float)numViewCells;
 
-			const float entrySize = sizeof(PvsData) + sizeof(Intersectable *);
-			const float memoryCost = (float)entriesInPvs * entrySize / (1024.0f * 1024.0f);
+			const float memoryCost = (float)entriesInPvs * ObjectPvs::GetEntrySize();
 
 			UpdateStats(stats,
@@ -2065,8 +2063,5 @@
 float ViewCellsTree::GetMemoryCost(ViewCell *vc) const
 {
-	const float entrySize = 
-		sizeof(PvsData) + sizeof(Intersectable *);
-
-	return (float)CountStoredPvsEntries(vc) * entrySize;
+	return (float)CountStoredPvsEntries(vc) * ObjectPvs::GetEntrySize();
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1667)
@@ -13,5 +13,5 @@
 #include "AxisAlignedBox3.h"
 #include "IntersectableWrapper.h"
-
+#include "HierarchyManager.h"
 
 
@@ -408,21 +408,13 @@
 		int Type() const { return OBJECT_SPACE; }
 	
-		void EvalPriority(bool computeSplitplane = true)
+		void EvalCandidate(bool computeSplitplane = true)
 		{
-			if (computeSplitplane)
-			{
-				sBvHierarchy->EvalSubdivisionCandidate(*this);	
-			}
-			else
-			{
-				mPvsEntriesIncr = sBvHierarchy->EvalPvsEntriesIncr(*this);
-				mPriority = sBvHierarchy->EvalPriority(*this);
-			}
+			sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane);
 		}
 
 		bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
 		{
-			BvhNode *n = 
-				sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
+			BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
+
 			// local or global termination criteria failed
 			return !n->IsLeaf();		
@@ -440,10 +432,27 @@
 		}
 
-		BvhSubdivisionCandidate(
-			const ObjectContainer &frontObjects, 
-			const ObjectContainer &backObjects, 
-			const BvhTraversalData &tData): 
+		BvhSubdivisionCandidate(const ObjectContainer &frontObjects, 
+								const ObjectContainer &backObjects, 
+								const BvhTraversalData &tData): 
 		mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
 		{}
+
+		float GetPriority() const
+		{
+			HierarchyManager *hm = sBvHierarchy->mHierarchyManager;
+			if (hm->ConsiderMemory())
+			{
+				const float rc = hm->GetHierarchyStats().mTotalCost - mRenderCostDecrease;
+				const float mc = hm->GetHierarchyStats().mMemory +
+								 (float)mPvsEntriesIncr * ObjectPvs::GetEntrySizeByte();
+				//cout << "x";
+				return - (rc * mc) / float(1024 * 1024);
+			}
+			else
+			{
+				return mPriority;
+			}
+		}
+
 
 		/// pointer to parent tree.
@@ -494,5 +503,5 @@
 	/** Evaluates candidate for splitting.
 	*/
-	void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData);
+	void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData, bool computeSplitPlane = true);
 
 	/** Returns list of leaves with pvs smaller than
@@ -726,5 +735,5 @@
 		@returns priority
 	*/
-	float EvalPriority(const BvhSubdivisionCandidate &splitCandidate) const;
+	//float EvalPriority(const BvhSubdivisionCandidate &splitCandidate) const;
 
 	////////////////////////////////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1586)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1667)
@@ -177,4 +177,7 @@
   */
   void Clear();
+
+  static int GetEntrySizeByte(); 
+  static float GetEntrySize();
 
   /** Compute continuous PVS difference */
@@ -189,7 +192,5 @@
 	
 	return pvsReduction + pvsEnlargement;
-  }
-
-					  
+  }		  
 					  
   /// Map of PVS entries
@@ -493,4 +494,20 @@
 
 
+template <typename T, typename S>
+float Pvs<T, S>::GetEntrySize()
+{
+	return (float)sizeof(T) + sizeof(S) / float(1024 * 1024);
+}
+
+
+template <typename T, typename S>
+int Pvs<T, S>::GetEntrySizeByte()
+{
+	return sizeof(T) + sizeof(S);
+}
+
+
+///////////////////////////////////////
+
 /** Class instantiating the Pvs template for kd tree nodes.
 */
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1653)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1667)
@@ -35,23 +35,17 @@
 	/// number of view cells
 	int viewCells;
-
 	/// size of the PVS
 	int pvsSize;
-
 	/// largest PVS of all view cells
 	int maxPvs;
-
 	/// smallest PVS of all view cells
 	int minPvs;
-
 	/// view cells with empty PVS
 	int emptyPvs;
-
 	/// number of leaves covering the view space
 	int leaves;
-
 	/// largest number of leaves covered by one view cell
 	int maxLeaves;
-
+	/// number of invalid view cells
 	int invalid;
 
@@ -89,8 +83,9 @@
 
 /**
-	View cell with an optional mesh representation
+	A view cell. View cells are regions in space. The visibility informations,
+	i.e., the primitives seen by the view cell are stored in a PVs.
+	A view cell can be represented in many different ways, e.g.,
+	a mesh representation.
 */
-
-
 class ViewCell: public MeshInstance
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1667)
@@ -786,24 +786,26 @@
 
 
-void VspTree::EvalSubdivisionCandidate(VspSubdivisionCandidate &splitCandidate)
-{
-	float frontProb;
-	float backProb;
+void VspTree::EvalSubdivisionCandidate(VspSubdivisionCandidate &splitCandidate, bool computeSplitPlane)
+{
+	if (computeSplitPlane)
+	{
+		float frontProb;
+		float backProb;
+
+		// compute locally best split plane
+		const float ratio = SelectSplitPlane(splitCandidate.mParentData, 
+									 		 splitCandidate.mSplitPlane, 
+											 frontProb, 
+											 backProb);
+	
+		const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
+
+		const int maxCostMisses = splitCandidate.mParentData.mMaxCostMisses;
+		// max cost threshold violated?
+		splitCandidate.SetMaxCostMisses(maxCostRatioViolated  ? maxCostMisses + 1: maxCostMisses);
+	}
 	
 	VspLeaf *leaf = dynamic_cast<VspLeaf *>(splitCandidate.mParentData.mNode);
 	
-	// compute locally best split plane
-	const float ratio = SelectSplitPlane(splitCandidate.mParentData, 
-								 		 splitCandidate.mSplitPlane, 
-										 frontProb, 
-										 backProb);
-
-	const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
-
-	// max cost threshold violated?
-	splitCandidate.SetMaxCostMisses(maxCostRatioViolated  ? 
-			splitCandidate.mParentData.mMaxCostMisses + 1:
-			splitCandidate.mParentData.mMaxCostMisses);
-
 	// compute global decrease in render cost
 	float oldRenderCost;
@@ -821,11 +823,8 @@
 	// otherwise danger of being stuck in a local minimum!!
 	const float factor = mRenderCostDecreaseWeight;
-
 	float priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
 
-	if (mHierarchyManager->mConsiderMemory)
-	{
-		//cout << "here4 rc: " << factor * renderCostDecr << " pvs: " << (1.0f - factor) * splitCandidate.GetPvsEntriesIncr() << endl;
-		//const float priority = factor * renderCostDecr - (1.0f - factor) * splitCandidate.GetPvsEntriesIncr();
+	if (1)//0 && mHierarchyManager->mConsiderMemory)
+	{
 		priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mHierarchyManager->mMemoryConst);
 	}
@@ -835,5 +834,5 @@
 
 
-float VspTree::EvalPriority(const VspSubdivisionCandidate &splitCandidate) const
+/*float VspTree::EvalPriority(const VspSubdivisionCandidate &splitCandidate) const
 {
 	// compute global decrease in render cost
@@ -857,5 +856,5 @@
 
 	return priority;
-}
+}*/
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 1313)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 1667)
@@ -24,6 +24,6 @@
 
 	inline void  SetPriority(const float k) { mPriority = k; }
-	inline float GetPriority() const  { return mPriority; }
-
+	//inline float GetPriority() const  { return mPriority; }
+	virtual float GetPriority() const = 0;
 protected:
 
@@ -54,5 +54,5 @@
 	unsigned int Size() const { return (unsigned int)mBuffer.size(); }
 	bool Empty() const {return mBuffer.empty(); }
-	T Item(const unsigned int i)       { return mBuffer[i]; }
+	T Item(const unsigned int i) { return mBuffer[i]; }
 	const T Item(const unsigned int i) const { return mBuffer[i]; }
 	T Pop();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1667)
@@ -358,8 +358,7 @@
 	// hack: assume that object space can be seen from view space
 	mHierarchyStats.mTotalCost = (float)objects.size();
+	// only one entry for start
 	mHierarchyStats.mPvsEntries = 1;
-
-	const int entrySize = sizeof(PvsData) + sizeof(Intersectable *);
-	mHierarchyStats.mMemory = entrySize / (1024.0f * 1024.0f);
+	mHierarchyStats.mMemory = (float)ObjectPvs::GetEntrySizeByte();
 
 	EvalSubdivisionStats();
@@ -396,12 +395,10 @@
 	// for first round, use sah splits. Once view space partition
 	// has started, use render cost heuristics instead
-	const int ospSteps = 
-		RunConstruction(objectSpaceQueue, dirtyVspList, renderCostDecr, minSteps);
+	const int ospSteps = RunConstruction(objectSpaceQueue, dirtyVspList, NULL, minSteps);
 
 	cout << "\n" << ospSteps << " object space partition steps taken" << endl;
 
 	// create view space
-	SubdivisionCandidate *vsc = 
-			PrepareViewSpaceSubdivision(sampleRays, objects);
+	SubdivisionCandidate *vsc = PrepareViewSpaceSubdivision(sampleRays, objects);
 
 	viewSpaceQueue.Push(vsc);
@@ -414,6 +411,4 @@
 	// account so we can compare to view space splits
 	ResetQueue(objectSpaceQueue, false);
-
-	//minSteps = 1;
 
 	// This method subdivides view space / object space 
@@ -425,35 +420,8 @@
 	while (!(viewSpaceQueue.Empty() && objectSpaceQueue.Empty()))
 	{
-		float vspPriority;
+		// decide upon next split type
+		const float vspPriority =  viewSpaceQueue.Top() ? viewSpaceQueue.Top()->GetPriority() : -1e20;
+		const float ospPriority =  viewSpaceQueue.Top() ? objectSpaceQueue.Top()->GetPriority() : -1e20;
 		
-		// decide upon next split type
-		if (!viewSpaceQueue.Empty())
-		{
-			vspPriority = viewSpaceQueue.Top()->GetPriority();
-
-			// rather take this into account directly when computing priority
-			if (0 && mConsiderMemory)
-				vspPriority /= ((float)viewSpaceQueue.Top()->GetPvsEntriesIncr() + Limits::Small);
-		}
-		else
-		{
-			vspPriority = 0;
-		}
-
-		float ospPriority;
-		
-		if (!objectSpaceQueue.Empty())
-		{
-			ospPriority = objectSpaceQueue.Top()->GetPriority();
-			
-			// rather take this into account directly when computing priority
-			if (0 && mConsiderMemory)
-				ospPriority /= ((float)objectSpaceQueue.Top()->GetPvsEntriesIncr() + Limits::Small);
-		}
-		else
-		{
-			ospPriority = 0;
-		}
-
 		cout << "new decicion, vsp: " << vspPriority << ", osp: " << ospPriority << endl;
 
@@ -471,5 +439,5 @@
 			// has started, use render cost heuristics instead
 			const int ospSteps = 
-				RunConstruction(objectSpaceQueue, dirtyVspList, renderCostDecr, minSteps);
+				RunConstruction(objectSpaceQueue, dirtyVspList, viewSpaceQueue.Top(), minSteps);
 
 			cout << "\n" << ospSteps << " object space partition steps taken" << endl;
@@ -493,5 +461,5 @@
 			// process view space candidates
 			const int vspSteps = 
-				RunConstruction(viewSpaceQueue, dirtyOspList, renderCostDecr, minSteps);
+				RunConstruction(viewSpaceQueue, dirtyOspList, objectSpaceQueue.Top(), minSteps);
 
 			cout << "\n" << vspSteps << " view space partition steps taken" << endl;
@@ -526,5 +494,5 @@
 	mHierarchyStats.mNodes = 2; 
 	mHierarchyStats.mPvsEntries = 1;
-	mHierarchyStats.mMemory = sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
+	mHierarchyStats.mMemory = (float)ObjectPvs::GetEntrySizeByte();
 	mHierarchyStats.mTotalCost = (float)objects.size();
 
@@ -570,8 +538,5 @@
 
 	// begin subdivision
-	RunConstruction(mRepairQueue, 
-					sampleRays, 
-					objects, 
-					forcedViewSpace);
+	RunConstruction(mRepairQueue, sampleRays, objects, forcedViewSpace);
 	
 	cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
@@ -734,7 +699,6 @@
 	//cout << "pvs entries: " << pvsEntriesIncr << " " << mHierarchyStats.pvsEntries << endl;
 
-	const int sizeOfEntry = sizeof(PvsData) + sizeof(Intersectable *);
 	// memory size in byte
-	mHierarchyStats.mMemory += float(pvsEntriesIncr * sizeOfEntry);
+	mHierarchyStats.mMemory += (float)ObjectPvs::GetEntrySizeByte();
 	mHierarchyStats.mRenderCostDecrease = sc->GetRenderCostDecrease();
 
@@ -744,5 +708,5 @@
 	{
 		memoryCount += 100000;
-		cout << "\nstorage cost: " << mHierarchyStats.mMemory / (1024.0f * 1024.0f) << ", steps: " << mHierarchyStats.Leaves() << endl;
+		cout << "\nstorage cost: " << mHierarchyStats.mMemory / float(1024 * 1024) << " MB, steps: " << mHierarchyStats.Leaves() << endl;
 	}
 
@@ -930,5 +894,5 @@
 int HierarchyManager::RunConstruction(SplitQueue &splitQueue,
 									  SubdivisionCandidateContainer &dirtyCandidates,
-									  const float minRenderCostDecr,
+									  SubdivisionCandidate *oldCandidate,
 									  const int minSteps)
 {
@@ -939,13 +903,15 @@
 	while (!splitQueue.Empty())
 	{
+		const float threshold = oldCandidate ? oldCandidate->GetPriority() : 1e20;
 		SubdivisionCandidate *sc = NextSubdivisionCandidate(splitQueue); 
-		
+		//cout << "here2 " << sc->GetPriority() << " " << threshold << endl;
+
 		// minimum slope reached
-		if ((sc->GetRenderCostDecrease() < minRenderCostDecr) &&
-			!(steps < minSteps))
-		{
-			//cout << "breaking on " << sc->GetRenderCostDecrease() << " smaller than " << minRenderCostDecr << endl;
+		if ((sc->GetPriority() < threshold) && !(steps < minSteps))
+		{
+			cout << "**************** breaking on " << sc->GetPriority() << " smaller than " << threshold << endl;
 			break;
 		}
+
 		////////
 		//-- subdivide leaf node of either type
@@ -993,6 +959,6 @@
 			mHierarchyStats.mPvsEntries = mBvHierarchy->CountViewCells(objects);
 
-			mHierarchyStats.mMemory = mHierarchyStats.mPvsEntries *
-				sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
+			mHierarchyStats.mMemory = 
+				(float)mHierarchyStats.mPvsEntries * ObjectPvs::GetEntrySizeByte();
 
 			mHierarchyStats.mRenderCostDecrease = 0;
@@ -1038,6 +1004,5 @@
 	mHierarchyStats.mRenderCostDecrease = 0;
 
-	mHierarchyStats.mMemory = mHierarchyStats.mPvsEntries * 
-		sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
+	mHierarchyStats.mMemory = (float)mHierarchyStats.mPvsEntries * ObjectPvs::GetEntrySizeByte();
 
 	// evaluate new stats before first subdivsiion
@@ -1309,7 +1274,13 @@
 		const float rcd = sc->GetRenderCostDecrease();
 		
-		splitQueue.Erase(sc); // erase from queue
-		sc->EvalPriority(recomputeSplitPlaneOnRepair); // reevaluate
-		
+		// erase from queue
+		splitQueue.Erase(sc);
+		// reevaluate candidate
+		sc->EvalCandidate(recomputeSplitPlaneOnRepair);
+		 // reinsert
+		splitQueue.Push(sc);
+
+		cout << ".";
+
 #ifdef _DEBUG
 		Debug << "candidate " << sc << " reevaluated\n" 
@@ -1317,6 +1288,4 @@
 			  << " old: " << rcd << " new " << sc->GetRenderCostDecrease() << endl;
 #endif	
-		splitQueue.Push(sc); // reinsert
-		cout << ".";
 	}
 
@@ -1339,5 +1308,5 @@
 		SubdivisionCandidate *candidate = NextSubdivisionCandidate(splitQueue);
 		 // reevaluate local split plane and priority
-		candidate->EvalPriority(recomputeSplitPlane);
+		candidate->EvalCandidate(recomputeSplitPlane);
 		cout << ".";
 		mCandidateBuffer.push_back(candidate);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1667)
@@ -161,8 +161,7 @@
 	{
 		// for a spatial subdivision, it is not necessary to store
-		// the objects with the leaves, they can be classified
-		// geometrically
-		mHierarchyManager->mOspTree->InsertObjects(
-			mHierarchyManager->mOspTree->mRoot, *mObjects);
+		// the objects with the leaves, they can be classified geometrically
+		mHierarchyManager->mOspTree->
+			InsertObjects(mHierarchyManager->mOspTree->mRoot, *mObjects);
 	}
 }
@@ -1173,9 +1172,9 @@
 void ViewCellsParseHandlers::StartOspLeaf(AttributeList& attributes)
 {
-	KdLeaf * leaf = 
-		new KdLeaf(dynamic_cast<KdInterior *>(mCurrentOspNode), NULL);
-
-	if (mCurrentOspNode) // replace front or (if not NULL) back child
-	{
+	KdLeaf * leaf = new KdLeaf(dynamic_cast<KdInterior *>(mCurrentOspNode), NULL);
+
+	if (mCurrentOspNode)
+	{
+		 // replace front or (if not NULL) back child
 		dynamic_cast<KdInterior *>(mCurrentOspNode)->ReplaceChildLink(NULL, leaf);
 	}
