Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1660)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.sh	(revision 1662)
@@ -61,5 +61,5 @@
 ##################################################################################
 
- METHOD=gradient
+ METHOD=gradient-mem
  echo "$SCENE $METHOD"
 
@@ -71,4 +71,5 @@
   -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
   -hierarchy_construction_type=2 \
+  -hierarchy_construction_consider_memory=true \
   -hierarchy_construction_multilevel=false \
   -hierarchy_construction_recompute_split_on_repair=true
@@ -134,17 +135,17 @@
 ########################################################################
 
-# METHOD=gradient_simplesplit
-# echo "$SCENE $METHOD"
+METHOD=gradient_simplesplit
+echo "$SCENE $METHOD"
 
-#$PROGRAM $ENVIRONMENT \
-# -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
-# -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
-#  -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
-#  -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
-#  -bvh_use_sah=true \
-#  -vsp_use_cost_heuristics=false \
-#  -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
-#  -hierarchy_construction_type=2 \
-#  -hierarchy_construction_recompute_split_on_repair=true
+$PROGRAM $ENVIRONMENT \
+ -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
+ -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+  -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+  -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+  -bvh_use_sah=true \
+  -vsp_use_cost_heuristics=true \
+  -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+  -hierarchy_construction_type=2 \
+  -hierarchy_construction_recompute_split_on_repair=true
 
 sh movefiles.sh $LOG_PREFIX-$METHOD
@@ -167,2 +168,24 @@
 
 sh movefiles.sh $LOG_PREFIX-$METHOD
+
+##########################################################
+ METHOD=gradient-500
+ echo "$SCENE $METHOD"
+
+  $PROGRAM $ENVIRONMENT \
+  -view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
+  -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+  -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+  -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+  -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+  -hierarchy_construction_type=2 \
+  -hierarchy_construction_consider_memory=false \
+  -hierarchy_construction_min_steps_same_type=500 \
+  -hierarchy_construction_multilevel=false \
+  -hierarchy_construction_recompute_split_on_repair=true
+
+sh movefiles.sh $LOG_PREFIX-$METHOD
+
+
+############################################################################
+
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1608)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1662)
@@ -1024,5 +1024,5 @@
 };
 
-#define TEST_EVALUATION 1
+#define TEST_EVALUATION 0
 
 /**
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1653)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1662)
@@ -2517,4 +2517,9 @@
 					"true");
 
+	RegisterOption("Hierarchy.Construction.considerMemory",
+					optBool,
+					"hierarchy_construction_consider_memory=",
+					"false");
+
 	RegisterOption("Hierarchy.Construction.repairQueue",
 					optBool,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1660)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1662)
@@ -388,6 +388,4 @@
 			{
 				ExportBinaryObj("../data/test.bin", mSceneGraph->GetRoot());
-				//Exporter *exporter = Exporter::GetExporter("dummy.wrl");
-				//exporter->ExportGeometry(mSceneGraph->GetRoot()->mGeometry);
 			}
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1654)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1662)
@@ -524,4 +524,9 @@
 	*/
 	bool mRecomputeSplitPlaneOnRepair;
+
+	/** If memory should be considered during choosing
+		of the next split type during gradient method.
+	*/
+	bool mConsiderMemory;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1640)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1662)
@@ -1088,4 +1088,5 @@
 	/// keeps track of cost during subdivision
 	float mTotalCost;
+	int mPvsEntries;
 	/// keeps track of overall pvs size during subdivision
 	int mTotalPvsSize;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1654)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1662)
@@ -25,5 +25,7 @@
 #define PROBABILIY_IS_BV_VOLUME 1
 #define USE_FIXEDPOINT_T 0
-//#define COUNT_ORIGIN_OBJECTS 0
+
+#define USE_VOLUMES_FOR_HEURISTICS 1
+
 
 int BvhNode::sMailId = 10000; //2147483647;
@@ -261,4 +263,5 @@
 	Environment::GetSingleton()->GetBoolValue("BvHierarchy.Construction.useGlobalSorting", mUseGlobalSorting);
 
+	mUseBboxAreaForSah = true;
 
 	/////////////
@@ -432,4 +435,5 @@
 		// decrease the weighted average cost of the subdivisoin
 		mTotalCost -= sc->GetRenderCostDecrease();
+		mPvsEntries += sc->GetPvsEntriesIncr();
 
 		// subdivision statistics
@@ -527,7 +531,4 @@
 		//-- surface area heuristics
 
-	//	if (objects.empty())
-	//		return 0.0f;
-
 		const AxisAlignedBox3 box = EvalBoundingBox(leaf->mObjects);
 		const float area = box.SurfaceArea();
@@ -814,9 +815,9 @@
 	int objectsLeft = 0, objectsRight = (int)tData.mNode->mObjects.size();
   
-	AxisAlignedBox3 box = tData.mNode->GetBoundingBox();
-
-	float minBox = box.Min(axis);
-	float maxBox = box.Max(axis);
-	float boxArea = box.SurfaceArea();
+	const AxisAlignedBox3 nodeBbox = tData.mNode->GetBoundingBox();
+
+	const float minBox = nodeBbox.Min(axis);
+	const float maxBox = nodeBbox.Max(axis);
+	const float boxArea = nodeBbox.SurfaceArea();
 
 	float minSum = 1e20f;
@@ -828,28 +829,36 @@
 	SortableEntryContainer::const_iterator currentPos =
 		mSubdivisionCandidates->begin();
-
-	
-	// we keep track of both borders of the bounding boxes =>
-	// store the events in descending order
+	
 	vector<float> bordersRight;
-	bordersRight.resize(mSubdivisionCandidates->size());
-
-	SortableEntryContainer::reverse_iterator rcit = 
-		mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend();
-	
-	vector<float>::reverse_iterator rbit = bordersRight.rbegin();
-	
-	for (; rcit != rcit_end; ++ rcit, ++ rbit) 
-	{
-		Intersectable *obj = (*rcit).mObject;
-		const AxisAlignedBox3 box = obj->GetBox();
-
-		if (box.Min(axis) < minBorder)
-		{
-			minBorder = box.Min(axis);
-		}
-
-		(*rbit) = minBorder;
-	}
+
+	if (mUseBboxAreaForSah)
+	{
+		// we keep track of both borders of the bounding boxes =>
+		// store the events in descending order
+
+		bordersRight.resize(mSubdivisionCandidates->size());
+
+		SortableEntryContainer::reverse_iterator rcit = 
+			mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend();
+	
+		vector<float>::reverse_iterator rbit = bordersRight.rbegin();
+	
+		for (; rcit != rcit_end; ++ rcit, ++ rbit) 
+		{
+			Intersectable *obj = (*rcit).mObject;
+			const AxisAlignedBox3 obox = obj->GetBox();
+
+			if (obox.Min(axis) < minBorder)
+			{
+				minBorder = obox.Min(axis);
+			}
+
+			(*rbit) = minBorder;
+		}
+	}
+
+	// temporary surface areas
+	float al = 0;
+	float ar = boxArea;
 
 	vector<float>::const_iterator bit = bordersRight.begin();
@@ -863,22 +872,35 @@
 		-- objectsRight;
 
-		AxisAlignedBox3 lbox = box;
-		AxisAlignedBox3 rbox = box;
-	
 		const AxisAlignedBox3 obox = obj->GetBox();
 
-		// the borders of the bounding boxes have changed
-		if (obox.Max(axis) > maxBorder)
-		{
-			maxBorder = obox.Max(axis);
-		}
-
-		minBorder = (*bit);
-	
-        lbox.SetMax(axis, maxBorder);
-		rbox.SetMin(axis, minBorder);
-	
-		const float al = lbox.SurfaceArea();
-		const float ar = rbox.SurfaceArea();
+		if (mUseBboxAreaForSah)
+		{
+			AxisAlignedBox3 lbox = nodeBbox;
+			AxisAlignedBox3 rbox = nodeBbox;
+	
+			// the borders of the bounding boxes have changed
+			if (obox.Max(axis) > maxBorder)
+			{
+				maxBorder = obox.Max(axis);
+			}
+
+			minBorder = (*bit);
+
+			lbox.SetMax(axis, maxBorder);
+			rbox.SetMin(axis, minBorder);
+	
+			al = lbox.SurfaceArea();
+			ar = rbox.SurfaceArea();
+		}
+		else
+		{
+			// just add up areas of the object bbs
+			// (as we are not sampling volumetric visibility,
+			// this should provide better heuristics
+			const float area = obox.SurfaceArea();
+
+			al += area;
+			ar -= area;
+		}
 
 		const float sum = objectsLeft * al + objectsRight * ar;
@@ -1151,5 +1173,10 @@
 	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
 	{
-		vol += (*vit)->GetVolume();
+#if USE_VOLUMES_FOR_HEURISTICS
+		const float volIncr = (*vit)->GetVolume();
+#else
+		const float volIncr = 1.0f;
+#endif
+		vol += volIncr;
 	}
 
@@ -1185,7 +1212,9 @@
 		// view cells can also be seen from left child node
 		ViewCell *viewCell = *vit;
-
+#if USE_VOLUMES_FOR_HEURISTICS
 		const float vol = viewCell->GetVolume();
-
+#else
+		const float vol = 1.0f;
+#endif
 		if (!viewCell->Mailed())
 		{
@@ -1352,5 +1381,6 @@
 			<< "#Leaves\n" << mBvhStats.Leaves() << endl
 			<< "#RenderCostDecrease\n" << costDecr << endl 
-			<< "#TotalRenderCost\n" << mTotalCost << endl;
+			<< "#TotalRenderCost\n" << mTotalCost << endl
+			<< "#EntriesInPvs\n" << mPvsEntries << endl;
 }
 
@@ -1881,4 +1911,5 @@
 	const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();
 	mTotalCost = (float)objects.size() * prop / viewSpaceVol;
+	mPvsEntries = CountViewCells(objects);
 
 	PrintSubdivisionStats(*oSubdivisionCandidate);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1643)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1662)
@@ -886,4 +886,5 @@
 	/// keeps track of cost during subdivision
 	float mTotalCost;
+	int mPvsEntries;
 	/// keeps track of overall pvs size during subdivision
 	int mTotalPvsSize;
@@ -899,4 +900,6 @@
 	bool mUseGlobalSorting;
 
+	bool mUseBboxAreaForSah;
+
 	SortableEntryContainer *mSortedObjects[3];
 };
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1649)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1662)
@@ -714,4 +714,5 @@
 		mTotalCost -= sc->GetRenderCostDecrease();
 		mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
+		mPvsEntries += sc->GetPvsEntriesIncr();
 
 		// subdivision statistics
@@ -2914,4 +2915,6 @@
 
 	mTotalCost = (float)pvsSize;
+	mPvsEntries = EvalPvsEntriesSize(rays);
+
 	EvalSubdivisionStats(*splitCandidate);
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1661)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1662)
@@ -119,7 +119,11 @@
 		"Hierarchy.Construction.recomputeSplitPlaneOnRepair", mRecomputeSplitPlaneOnRepair);
 
+	Environment::GetSingleton()->GetBoolValue(
+		"Hierarchy.Construction.considerMemory", mConsiderMemory);
+
 	Environment::GetSingleton()->GetFloatValue(
 		"Hierarchy.Termination.maxMemory", mTermMaxMemory);
 
+	// compare to bytes
 	mTermMaxMemory *= (1024.0f * 1024.0f);
 
@@ -134,4 +138,5 @@
 	Debug << "minimal number of steps from same type: " << mMinStepsOfSameType << endl;
 	Debug << "maximal allowed memory: " << mTermMaxMemory << endl;
+	Debug << "consider mem: " << mConsiderMemory << endl;
 
 	switch (mConstructionType)
@@ -350,5 +355,7 @@
 	mHierarchyStats.mTotalCost = (float)objects.size();
 	mHierarchyStats.mPvsEntries = 1;
-	mHierarchyStats.mMemory = sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
+
+	const int entrySize = sizeof(PvsData) + sizeof(Intersectable *);
+	mHierarchyStats.mMemory = entrySize / (1024.0f * 1024.0f);
 
 	EvalSubdivisionStats();
@@ -365,5 +372,5 @@
 	mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
 	mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
-	//mSavedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
+	mSavedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
 
 	// number of initial splits
@@ -404,7 +411,4 @@
 	ResetQueue(objectSpaceQueue, false);
 
-	
-	// lower number of minsteps: the decicion of which domain to subdivide
-	// should be decided using the render cost decrease from now
 	//minSteps = 1;
 
@@ -417,8 +421,32 @@
 	while (!(viewSpaceQueue.Empty() && objectSpaceQueue.Empty()))
 	{
-		const float vspPriority = viewSpaceQueue.Empty() ? 0 : 
-			viewSpaceQueue.Top()->GetPriority() / ((float)viewSpaceQueue.Top()->GetPvsEntriesIncr() + Limits::Small);
-		const float ospPriority = objectSpaceQueue.Empty() ? 0 : 
-			objectSpaceQueue.Top()->GetPriority() / ((float)objectSpaceQueue.Top()->GetPvsEntriesIncr() + Limits::Small);
+		float vspPriority;
+		
+		// decide upon next split type
+		if (!viewSpaceQueue.Empty())
+		{
+			vspPriority = viewSpaceQueue.Top()->GetPriority();
+
+			if (mConsiderMemory)
+				vspPriority /= ((float)viewSpaceQueue.Top()->GetPvsEntriesIncr() + Limits::Small);
+		}
+		else
+		{
+			vspPriority = 0;
+		}
+
+		float ospPriority = 0;
+		
+		if (!objectSpaceQueue.Empty())
+		{
+			ospPriority = objectSpaceQueue.Top()->GetPriority();
+		
+			if (mConsiderMemory)
+				ospPriority /= ((float)objectSpaceQueue.Top()->GetPvsEntriesIncr() + Limits::Small);
+		}
+		else
+		{
+			ospPriority = 0;
+		}
 
 		cout << "new decicion, vsp: " << vspPriority << ", osp: " << ospPriority << endl;
@@ -567,8 +595,5 @@
 
 	mHierarchyStats.mTotalCost = mVspTree->mTotalCost;
-	mHierarchyStats.mPvsEntries = mVspTree->EvalPvsEntriesSize(*viewSpaceRays);
-	mHierarchyStats.mMemory = mHierarchyStats.mPvsEntries * 
-		sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
-
+	
 	cout << "\nreseting cost for vsp, new total cost: " << mHierarchyStats.mTotalCost << endl;
 
@@ -933,18 +958,15 @@
 	
 			// rather use this: remove previous nodes and add the two new ones
-			//mHierarchyStats.mNodes -= mBvHierarchy->mBvhStats.nodes + 2;
-			mHierarchyStats.mNodes = 2;
-
-			//firstCandidate = mBvHierarchy->PrepareConstruction(sampleRays, objects);
+			mHierarchyStats.mNodes -= mBvHierarchy->mBvhStats.nodes + 1;
+			
 			firstCandidate = mBvHierarchy->Reset(sampleRays, objects);
 
 			mHierarchyStats.mTotalCost = mBvHierarchy->mTotalCost;
-			mHierarchyStats.mRenderCostDecrease = 0;
-
-			// bug!! pvs entries cab be much higher at this stage
-			// (if view space was already subdivided)
+			//mHierarchyStats.mPvsEntries -= mBvHierarchy->mPvsEntries + 1;
 			mHierarchyStats.mPvsEntries = mBvHierarchy->CountViewCells(objects);
 			mHierarchyStats.mMemory = mHierarchyStats.mPvsEntries *
 				sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
+
+			mHierarchyStats.mRenderCostDecrease = 0;
 
 			// evaluate stats before first subdivision
@@ -980,9 +1002,15 @@
 	mVspTree->Initialise(sampleRays, forcedViewSpace);
 	
-	mHierarchyStats.mNodes = -mVspTree->mVspStats.nodes + 1;
+	//-- reset stats
+    mHierarchyStats.mNodes = -mVspTree->mVspStats.nodes + 1;
+	
+	SubdivisionCandidate *vsc = PrepareViewSpaceSubdivision(sampleRays, objects);
+	
+	mHierarchyStats.mPvsEntries = mVspTree->mPvsEntries;
 	mHierarchyStats.mRenderCostDecrease = 0;
 
-	SubdivisionCandidate *vsc = PrepareViewSpaceSubdivision(sampleRays, objects);
-	
+	mHierarchyStats.mMemory = mHierarchyStats.mPvsEntries * 
+		sizeof(PvsData) + sizeof(Intersectable *) / (1024.0f * 1024.0f);
+
 	// evaluate new stats before first subdivsiion
 	EvalSubdivisionStats();
