Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1577)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1705)
@@ -392,5 +392,4 @@
 	}
 
-
 	mSceneGraph->CollectObjects(&mObjects);
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1696)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1705)
@@ -509,22 +509,5 @@
 		float GetPriority() const
 		{
-			HierarchyManager *hm = sVspTree->mHierarchyManager;
-			
-			if (hm->ConsiderMemory())
-			{
-				const float rc = mRenderCostDecrease / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
-				//const float mc = mMemoryIncr /  / hm->GetHierarchyStats().mMemory;
-				//const float rc = mPriority / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
-				const float mc = (float)mPvsEntriesIncr / (float)hm->GetHierarchyStats().mPvsEntries;	
-				
-				//cout << "\np: " << mPriority << " i: " << hm->mInitialRenderCost << " t: " << hm->GetHierarchyStats().mTotalCost << endl;
-				//cout << "vsp rc: " << rc << " mc: " << mc << endl;
-				
-				return hm->GetMemoryConst() * rc - (1.0f - hm->GetMemoryConst()) * mc;
-			}
-			else
-			{
-				return mPriority;
-			}
+			return mPriority;
 		}
 	};
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1703)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1705)
@@ -307,4 +307,5 @@
 	Debug << "max memory: " << mMaxMemory << endl;
 	Debug << "use cost heuristics: " << mUseCostHeuristics << endl;
+	Debug << "use sah (surface area heuristics: " << mUseSah << endl;
 	Debug << "subdivision stats log: " << subdivisionStatsLog << endl;
 	Debug << "split borders: " << mSplitBorder << endl;
@@ -572,12 +573,5 @@
 		HierarchyManager::NO_VIEWSPACE_SUBDIV)
 	{
-		////////////////
-		//-- surface area heuristics
-
-		const AxisAlignedBox3 box = EvalBoundingBox(leaf->mObjects);
-		const float area = box.SurfaceArea();
-		const float viewSpaceArea = mViewCellsManager->GetViewSpaceBox().SurfaceArea();
-
-		priority = (float)leaf->mObjects.size() * area / viewSpaceArea;
+		priority = EvalSahCost(leaf);
 	}
 	else
@@ -612,9 +606,19 @@
 inline bool BvHierarchy::LocalTerminationCriteriaMet(const BvhTraversalData &data) const
 {
-	return ( 0
-			|| ((int)data.mNode->mObjects.size() <= mTermMinObjects)
+	const bool terminationCriteriaMet =
+			(0
+			|| ((int)data.mNode->mObjects.size() <= 1)//mTermMinObjects)
 			//|| (data.mProbability <= mTermMinProbability)
-			|| (data.mNumRays <= mTermMinRays)
+			//|| (data.mNumRays <= mTermMinRays)
 		 );
+
+#ifdef _DEBUG
+	if (terminationCriteriaMet)
+	{
+		cout << "bvh local termination criteria met:" << endl;
+		cout << "objects: " << data.mNode->mObjects.size() << " " << mTermMinObjects << endl;
+	}
+#endif
+	return terminationCriteriaMet; 
 }
 
@@ -722,5 +726,5 @@
 	}
 
-#if 0
+#ifdef _DEBUG
 	cout << "depth: " << data.mDepth << " objects: " << (int)leaf->mObjects.size() 
 		 << " rays: " << data.mNumRays << " rays / objects " 
@@ -764,6 +768,5 @@
 
 	const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects);
-	const float newRenderCost = 
-		EvalRenderCost(objectsFront) * EvalRenderCost(objectsBack);
+	const float newRenderCost = EvalRenderCost(objectsFront) * EvalRenderCost(objectsBack);
 
 	const float ratio = newRenderCost / oldRenderCost;
@@ -801,12 +804,14 @@
 	}
 
-	float heur = 0;
-
-	if (tData.mNode->GetBoundingBox().Size().DrivingAxis() == axis)
-	{
-		heur = -1;
-	}
-
-	return heur;
+#if 1
+	const float cost = (tData.mNode->GetBoundingBox().Size().DrivingAxis() == axis) ? -1.0f : 0.0f;
+#else
+	const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects);
+	const float newRenderCost = EvalRenderCost(objectsFront) * EvalRenderCost(objectsBack);
+
+	const float cost = newRenderCost / oldRenderCost;
+#endif
+
+	return cost;
 }
 #endif
@@ -918,5 +923,7 @@
 		}
 
-		const float sum = objectsLeft * al + objectsRight * ar;
+		const bool noValidSplit = ((objectsLeft <= Limits::Small) || (objectsRight <= Limits::Small));
+
+		const float sum =  noValidSplit ? 1e25 : objectsLeft * al + objectsRight * ar;
       
 		/*cout << "pos=" << (*cit).mPos << "\t q=(" << objectsLeft << "," << objectsRight <<")\t r=(" 
@@ -925,6 +932,7 @@
 	    cout << "cost= " << sum << endl;
 */
+	
 		if (sum < minSum) 
-		{
+		{	//cout <<" sum: " << sum;
 			minSum = sum;
 			areaLeft = al;
@@ -935,5 +943,4 @@
 		}
 	}
-
 
 	////////////////////////////////////////////
@@ -952,8 +959,9 @@
   
 #ifdef _DEBUG
+//	if (objectsBack.empty() ||objectsFront.empty())
 	cout << "\n\nobjects=(" << (int)objectsBack.size() << "," << (int)objectsFront.size() << " of " 
 		 << (int)tData.mNode->mObjects.size() << ")\t area=(" 
-		 << areaLeft << "," << areaRight << ")" << endl
-		 << "cost= " << minSum << endl;
+		 << areaLeft << ", " << areaRight << ", " << boxArea << ")" << endl
+		 << "cost= " << newCost << " oldCost=" << totalRenderCost / boxArea << endl;
 #endif
 
@@ -1069,7 +1077,9 @@
 		nObjectsRight -= rc;
 		
+		const bool noValidSplit = ((nObjectsLeft <= Limits::Small) || (nObjectsRight <= Limits::Small));
+
 		// the heuristics
-	    const float sum = volLeft * (float)nObjectsLeft + 
-						  volRight * (float)nObjectsRight;
+	    const float sum = noValidSplit ? 
+			1e25 : volLeft * (float)nObjectsLeft + volRight * (float)nObjectsRight;
 
 #ifdef _DEBUG
@@ -1442,4 +1452,19 @@
 
 
+float BvHierarchy::EvalSahCost(BvhLeaf *leaf)
+{
+	////////////////
+	//-- surface area heuristics
+	if (leaf->mObjects.empty())
+		return 0.0f;
+
+	const AxisAlignedBox3 box = GetBoundingBox(leaf);
+	const float area = box.SurfaceArea();
+	const float viewSpaceArea = mViewCellsManager->GetViewSpaceBox().SurfaceArea();
+
+	return EvalAbsCost(leaf->mObjects) * area / viewSpaceArea;
+}
+
+
 float BvHierarchy::EvalRenderCost(const ObjectContainer &objects) const
 {	
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1703)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1705)
@@ -40,5 +40,7 @@
 // HACK
 const static bool SAMPLE_AFTER_SUBDIVISION = true;
-const static bool CLAMP_TO_BOX = false;
+//const static bool CLAMP_TO_BOX = false;
+const static bool CLAMP_TO_BOX = true;
+
 
 int ViewCellsManager::sRenderCostEvaluationType = 0;
@@ -511,7 +513,16 @@
 	if (mEvaluateViewCells)
 	{
+cout << "here42 ***********" << endl;
 		EvalViewCellPartition();
 	}
-	
+		
+	// write view cells to disc
+	if (1 && mExportViewCells)
+	{
+		char filename[100];
+		Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
+		ExportViewCells(filename, mExportPvs, mPreprocessor->mObjects);
+	}
+
 	/////////////////
 	//-- Finally, we do some visualization
@@ -2893,12 +2904,4 @@
 	// compute final meshes and volume / area
 	if (1) FinalizeViewCells(true);
-
-	// write view cells to disc
-	if (1 && mExportViewCells)
-	{
-		char filename[100];
-		Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
-		ExportViewCells(filename, mExportPvs, objects);
-	}
 	
 	return 0;
@@ -4153,12 +4156,4 @@
 	if (1) FinalizeViewCells(true);
 
-	// write view cells to disc
-	if (1 && mExportViewCells)
-	{
-		char filename[100];
-		Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
-		ExportViewCells(filename, mExportPvs, objects);
-	}
-
 	return 0;
 }
@@ -5020,12 +5015,4 @@
 
 	if (1) FinalizeViewCells(true);
-
-	// write out view cells (this moved to preprocessor)
-	if (mExportViewCells)
-	{
-		char filename[100];
-		Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
-		ExportViewCells(filename, mExportPvs, objects);
-	}
 
 	return 0;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1703)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1705)
@@ -70,4 +70,5 @@
 		splits = 0;
 		maxDepth = 0;
+
 		minDepth = 99999;
 		accumDepth = 0;
@@ -75,5 +76,4 @@
 		minProbabilityNodes = 0;
 		maxCostNodes = 0;
-			
 		///////////////////
 		minObjectsNodes = 0;
@@ -421,5 +421,5 @@
 		void EvalCandidate(bool computeSplitplane = true)
 		{
-			mDirty = false;
+            mDirty = false;
 			sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane);
 		}
@@ -452,22 +452,5 @@
 		float GetPriority() const
 		{
-			HierarchyManager *hm = sBvHierarchy->mHierarchyManager;
-		
-			if (hm->ConsiderMemory())
-			{
-				const float rc = mRenderCostDecrease / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
-				//const float mc = mMemoryIncr /  / hm->GetHierarchyStats().mMemory;
-                //const float rc = mPriority / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
-				const float mc = (float)mPvsEntriesIncr / (float)hm->GetHierarchyStats().mPvsEntries;	
-				
-				//cout << "\np: " << mPriority << " i: " << hm->mInitialRenderCost << " t: " << hm->GetHierarchyStats().mTotalCost << endl;
-				//cout << "osp rc: " << rc << " mc: " << mc << endl;
-
-				return hm->GetMemoryConst() * rc - (1.0f - hm->GetMemoryConst()) * mc;
-			}
-			else
-			{
-				return mPriority;
-			}
+			return mPriority;
 		}
 
@@ -755,4 +738,7 @@
 	float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
 	
+	/** Evaluates cost for a leaf given the surface area heuristics.
+	*/
+	float EvalSahCost(BvhLeaf *leaf);
 
 	////////////////////////////////////////////////
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1701)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1705)
@@ -144,5 +144,5 @@
 	mTermMaxMemory *= (1024.0f * 1024.0f);
 
-	Debug << "******** Hierachy Manager Options ***********" << endl;
+	Debug << "******** Hierarchy Manager Options ***********" << endl;
 	Debug << "max leaves: " << mTermMaxLeaves << endl;
 	Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1667)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1705)
@@ -789,13 +789,13 @@
 
 	//viewCell = GetOrCreateViewCell(id, isLeaf);
-	
-		if (isLeaf)
-		{
-			viewCell = new ViewCellLeaf();
-		}
-		else
-		{
-			viewCell = new ViewCellInterior();
-		}
+
+	if (isLeaf)
+	{
+		viewCell = new ViewCellLeaf();
+	}
+	else
+	{
+		viewCell = new ViewCellInterior();
+	}
 
 	for (int i = 0; i < len; ++ i) 
