Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1161)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1174)
@@ -2320,4 +2320,9 @@
 
 
+	RegisterOption("Hierarchy.subdivisionStats",
+					optString,
+					"hierarchy_subdivision_stats=",
+					"hierarchySubdivisionStats.log");
+
 	//////////////////////////////////////////////////////////////////////////////////
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1145)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1174)
@@ -444,6 +444,7 @@
 		mVspTree = new VspTree();
 		mOspTree = new OspTree();
-		// HACK
-		//mOspTree = new OspTree(*mKdTree);
+		
+		// HACK for testing if per kd evaluation works!!
+		mOspTree = new OspTree(*mKdTree);
 
 		mViewCellsManager = new VspOspViewCellsManager(mVspTree, mOspTree);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1168)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1174)
@@ -637,19 +637,8 @@
 
 
-void VspTree::EvalSubdivisionStats(const VspTraversalData &tData,
-								   const VspTraversalData &tFrontData,
-								   const VspTraversalData &tBackData
-								   )
-{
-	const float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
-	const float cBack = (float)tBackData.mPvs * tBackData.mProbability;
-	const float cData = (float)tData.mPvs * tData.mProbability;
-	
-	const float costDecr = 
-		(cFront + cBack - cData) / mBoundingBox.GetVolume();
-
-	mTotalCost += costDecr;
-	mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
-
+void VspTree::EvalSubdivisionStats(const SplitCandidate &sc)
+{
+	const float costDecr = sc.GetRenderCostDecrease();
+	
 	AddSubdivisionStats(mVspStats.Leaves(),
 						-costDecr,
@@ -663,9 +652,10 @@
 							const bool globalCriteriaMet)
 {
-	// doto remove dynamic cast
+	// todo remove dynamic cast
 	VspSplitCandidate *sc = dynamic_cast<VspSplitCandidate *>(splitCandidate);
 	VspTraversalData &tData = sc->mParentData;
 
 	VspNode *newNode = tData.mNode;
+
 
 	if (!LocalTerminationCriteriaMet(tData) && !globalCriteriaMet)
@@ -687,7 +677,9 @@
 		tBackData.mMaxCostMisses = maxCostMisses;
 			
-	
-		if (1) //-- subdivision statistics
-			EvalSubdivisionStats(tData, tFrontData, tBackData);
+		mTotalCost -= sc->GetRenderCostDecrease();
+		mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
+
+		// subdivision statistics
+		if (1) EvalSubdivisionStats(*sc);
 		
 		//-- evaluate new split candidates for global greedy cost heuristics
@@ -882,11 +874,13 @@
 	CreateViewCell(backData, false);
 
-	
+
+#if WORK_WITH_VIEWCELL_PVS
 	// create front and back view cell
 	// add front and back view cell to "Potentially Visbilie View Cells" 
 	// of the objects in front and back pvs
 
-	//AddViewCellReferences(frontLeaf->GetViewCell());
-	//AddViewCellReferences(backLeaf->GetViewCell());
+	AddViewCellReferences(frontLeaf->GetViewCell());
+	AddViewCellReferences(backLeaf->GetViewCell());
+#endif
 
 	interior->mTimeStamp = mTimeStamp ++;
@@ -2823,5 +2817,4 @@
 {
 	ReadEnvironment();
-
 	mSplitCandidates = new vector<SortableEntry>;
 }
@@ -2994,6 +2987,6 @@
 		
 	// TODO matt: compute pvs
-	frontData.mPvs = 999; // ComputePvsSize(*frontData.mRays);
-	backData.mPvs = 999; //ComputePvsSize(*backData.mRays);
+	frontData.mPvs = 999999; // ComputePvsSize(*frontData.mRays);
+	backData.mPvs = 9999999; //ComputePvsSize(*backData.mRays);
 
 	frontData.mProbability = frontData.mBoundingBox.GetVolume();
@@ -3089,9 +3082,15 @@
 		const int maxCostMisses = sc->mMaxCostMisses;
 
-		// how often was max cost ratio missed in this branch?
+        // how often was max cost ratio missed in this branch?
 		tFrontData.mMaxCostMisses = maxCostMisses;
 		tBackData.mMaxCostMisses = maxCostMisses;
-			
+		
+		mTotalCost -= sc->GetRenderCostDecrease();
+
+		// subdivision statistics
+		if (1) EvalSubdivisionStats(*sc);
+
 		//-- push the new split candidates on the queue
+		
 		OspSplitCandidate *frontCandidate = new OspSplitCandidate(tFrontData);
 		OspSplitCandidate *backCandidate = new OspSplitCandidate(tBackData);
@@ -3231,5 +3230,5 @@
 {
 	RayInfoContainer usedRays;
-int mMaxTests = 5000;
+int mMaxTests = 5000; // HACK
 	if (mMaxTests < (int)tData.mRays->size())
 	{
@@ -3384,4 +3383,8 @@
 		  << "old rc: " << oldRenderCost / viewSpaceVol << " new rc: " << newRenderCost / viewSpaceVol << endl
 		  << "render cost decrease: " << oldRenderCost / viewSpaceVol - newRenderCost / viewSpaceVol << endl;
+
+	if (oldRenderCost < newRenderCost)
+		Debug << "\nwarning!!:\n" << "old rc: " << oldRenderCost * viewSpaceVol << " new rc: " << newRenderCost * viewSpaceVol << endl;
+	
 
 	return ratio;
@@ -3747,27 +3750,28 @@
 
 
-#if TODO
-void OspTree::EvalSubdivisionStats(const VspTraversalData &tData,
-								   const VspTraversalData &tFrontData,
-								   const VspTraversalData &tBackData
-								   )
-{
-	const float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
-	const float cBack = (float)tBackData.mPvs * tBackData.mProbability;
-	const float cData = (float)tData.mPvs * tData.mProbability;
-	
-	const float costDecr = 
-		(cFront + cBack - cData) / mBoundingBox.GetVolume();
-
-	mTotalCost += costDecr;
-	mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
-
-	AddSubdivisionStats(mVspStats.Leaves(),
-						-costDecr,
-						sc->GetPriority(),
-						mTotalCost,
-						(float)mTotalPvsSize / (float)mVspStats.Leaves());
-}
-#endif
+void OspTree::EvalSubdivisionStats(const SplitCandidate &sc)
+{
+	const float costDecr = sc.GetRenderCostDecrease();
+	Debug << "**********************$$$$$$$$$$$$$$"<< endl;
+Debug << "here78 " << mOspStats.Leaves() << " " << mTotalCost << endl;
+	AddSubdivisionStats(mOspStats.Leaves(),
+						costDecr,
+						mTotalCost
+						);
+}
+
+
+
+void OspTree::AddSubdivisionStats(const int leaves,
+								  const float renderCostDecr,
+								  const float totalRenderCost)
+								  //const float avgRenderCost)
+{Debug << "here64 " << totalRenderCost << " " << renderCostDecr << endl;
+	mSubdivisionStats 
+			<< "#Leaves\n" << leaves << endl
+			<< "#RenderCostDecrease\n" << renderCostDecr << endl 
+			<< "#TotalRenderCost\n" << totalRenderCost << endl;
+			//<< "#AvgRenderCost\n" << avgRenderCost << endl;
+}
 
 
@@ -3809,5 +3813,4 @@
 	float pvsFront = 0;
 	float pvsBack = 0;
-	float totalPvs = 0;
 
 	// probability that view point lies in back / front node
@@ -3823,4 +3826,6 @@
 
 	KdLeaf *leaf = tData.mNode;
+	int totalPvs = (int)leaf->mObjects.size();
+
 	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
 
@@ -3829,16 +3834,17 @@
 	for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
 	{
+		int pvsContri = 0;
+
 		Intersectable *obj = *oit;
 		const AxisAlignedBox3 box = obj->GetBox();
-		
-		++ totalPvs;
 
 		//cout << "totalpvs " << totalPvs << endl;
 
 		// test if box falls in left / right child node
-		if (box.Max(candidatePlane.mAxis) > candidatePlane.mPosition)
+		if (box.Max(candidatePlane.mAxis) >= candidatePlane.mPosition)
 		{
 			++ pvsFront;
 		}
+
 		if (box.Min(candidatePlane.mAxis) < candidatePlane.mPosition)
 		{
@@ -3846,5 +3852,4 @@
 		}
 	}
-
 
 	// sum up volume seen from the objects of left and right children
@@ -3889,5 +3894,5 @@
 	const float renderCostDecrease = (oldRenderCost - newRenderCost) / viewSpaceVol;
 
-	Debug << "\n==== eval render cost decrease ===" << endl
+	Debug << "\n==== eval render cost decrease ====" << endl
 		  << "back pvs: " << pvsBack << " front pvs " << pvsFront << " total pvs: " << totalPvs << endl 
 		  << "back p: " << pBack / viewSpaceVol << " front p " << pFront / viewSpaceVol << " p: " << pOverall / viewSpaceVol << endl
@@ -3896,4 +3901,19 @@
 
 	normalizedOldRenderCost = oldRenderCost / viewSpaceVol;
+
+	if (oldRenderCost < newRenderCost)
+		Debug << "\nwarning2!!:\n" << "old rc: " << oldRenderCost * viewSpaceVol << " new rc: " << newRenderCost * viewSpaceVol << endl;
+	else
+		Debug << "here4" << endl;
+
+	if (pBack + pFront < pOverall * 0.9)
+		Debug << "\nwarning3!!:\n" << "p new: " << pBack + pFront << " p old: " << pOverall << endl;
+	else
+		Debug << "here55" << endl;
+
+	if (pvsBack + pvsFront < totalPvs)
+		Debug << "\nwarning4!!:\n" << "pvs new: " << pvsBack + pvsFront << " pvs old: " << totalPvs << endl;
+	else
+		Debug << "here25" << endl;
 
 	return renderCostDecrease;
@@ -4290,4 +4310,8 @@
 	mVspTree.mOspTree = &ospTree;
 	mOspTree.mVspTree = &vspTree;
+
+	char subdivisionStatsLog[100];
+	Environment::GetSingleton()->GetStringValue("Hierarchy.subdivisionStats", subdivisionStatsLog);
+	mSubdivisionStats.open(subdivisionStatsLog);
 }
 
@@ -4345,5 +4369,6 @@
 									mVspTree.mBoundingBox);
 
-
+#if WORK_WITH_VIEWCELL_PVS
+	
 	// create first view cell
 	mVspTree.CreateViewCell(vData, false);
@@ -4359,4 +4384,5 @@
 		obj->mViewCellPvs.AddSample(leaf->GetViewCell(), 1);
 	}
+#endif
 
 	// compute first split candidate
@@ -4365,4 +4391,7 @@
     mVspTree.EvalSplitCandidate(*splitCandidate);
 
+	mVspTree.mTotalCost = (float)pvsSize;
+	mVspTree.EvalSubdivisionStats(*splitCandidate);
+
 	return splitCandidate;
 }
@@ -4370,7 +4399,7 @@
 
 OspTree::OspSplitCandidate * HierarchyManager::PrepareOsp(const VssRayContainer &sampleRays,
-											  const ObjectContainer &objects,
-											  AxisAlignedBox3 *forcedObjectSpace,
-											  RayInfoContainer &rays)
+														  const ObjectContainer &objects,
+														  AxisAlignedBox3 *forcedObjectSpace,
+														  RayInfoContainer &rays)
 {
 	// store pointer to this tree
@@ -4410,5 +4439,9 @@
 		new OspTree::OspSplitCandidate(oData);
 
-    mOspTree.EvalSplitCandidate(*oSplitCandidate);
+	mOspTree.EvalSplitCandidate(*oSplitCandidate);
+
+	mOspTree.mTotalCost = (float)objects.size();
+	Debug << "total cost here553: " << mOspTree.mTotalCost << endl;
+	mOspTree.EvalSubdivisionStats(*oSplitCandidate);
 
 	return oSplitCandidate;
@@ -4433,4 +4466,30 @@
 
 
+void HierarchyManager::EvalSubdivisionStats(const SplitCandidate &tData)
+{
+	const float costDecr = tData.GetRenderCostDecrease();
+
+	//mTotalCost -= costDecr;
+	// mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
+
+	AddSubdivisionStats(mOspTree.mOspStats.Leaves() + mVspTree.mVspStats.Leaves(),
+						costDecr,
+						mTotalCost
+						);
+}
+
+
+void HierarchyManager::AddSubdivisionStats(const int splits,
+										   const float renderCostDecr,
+										   const float totalRenderCost)
+{
+	mSubdivisionStats 
+			<< "#Splits\n" << splits << endl
+			<< "#RenderCostDecrease\n" << renderCostDecr << endl 
+			<< "#TotalRenderCost\n" << totalRenderCost << endl;
+			//<< "#AvgRenderCost\n" << avgRenderCost << endl;
+}
+
+
 bool HierarchyManager::GlobalTerminationCriteriaMet(SplitCandidate *candidate) const
 {
@@ -4473,12 +4532,12 @@
 	if (vspSplit)
 	{
-		mVspTree.Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
-		return true;
+		VspNode *n = mVspTree.Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
 	}
 	else
 	{
-		mOspTree.Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
-		return false;
-	}
+		KdNode *n = mOspTree.Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
+	}
+	
+	return !globalTerminationCriteriaMet;
 }
 
@@ -4492,27 +4551,28 @@
 		SplitCandidate *splitCandidate = NextSplitCandidate();
 	    
-		cout << "nodes: " << ++ numNodes << endl;
-
 		mTotalCost -= splitCandidate->GetRenderCostDecrease();
-
 		// cost ratio of cost decrease / totalCost
 		const float costRatio = splitCandidate->GetRenderCostDecrease() / mTotalCost;
-		
+
+		if (costRatio < mTermMinGlobalCostRatio)
+			++ mGlobalCostMisses;
+
+		/*		cout << "nodes: " << ++ numNodes << endl;
 		Debug << "\n**********" << endl
 			  << "total cost: " << mTotalCost << " render cost decr: " 
 			  << splitCandidate->GetRenderCostDecrease() 
 			  << " cost ratio: " << costRatio << endl << endl;
-
+*/
 		//-- subdivide leaf node
-		SubdivideSplitCandidate(splitCandidate);
-
-		if (costRatio < mTermMinGlobalCostRatio)
-			++ mGlobalCostMisses;
-
-		// reevaluate candidates affected by the split
-		// for view space splits, this would be object space splits
-		// and other way round
-		if (repair)
-			RepairQueue();
+		if (SubdivideSplitCandidate(splitCandidate))
+		{
+			EvalSubdivisionStats(*splitCandidate);
+		
+			// reevaluate candidates affected by the split
+			// for view space splits, this would be object space splits
+			// and other way round
+			if (repair)
+				RepairQueue();
+		}
 
 		DEL_PTR(splitCandidate);
@@ -4544,4 +4604,5 @@
 	VspTree::VspSplitCandidate *vsc = 
 		PrepareVsp(sampleRays, forcedViewSpace, *viewSpaceRays);
+
 	// add to queue
 	mTQueue.Push(vsc);
@@ -4590,5 +4651,5 @@
 	// note: only true for view space = object space
 	mTotalCost = (float)osc->mParentData.mNode->mObjects.size();//(float)sc->mParentData.mPvs;
-
+	
 	Debug << "reseting cost, new total cost: " << mTotalCost << endl;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1168)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1174)
@@ -1130,18 +1130,18 @@
 struct SortableViewCellEntry {
 
-  SortableViewCellEntry() {}
-  SortableViewCellEntry(const float v, ViewCell *cell):mValue(v), mViewCell(cell) {}
-  
-  float mValue;
-  ViewCell *mViewCell;
-
-  friend bool operator<(const SortableViewCellEntry &a, const SortableViewCellEntry &b) {
-	return a.mValue < b.mValue;
-  }
+	SortableViewCellEntry() {}
+	SortableViewCellEntry(const float v, ViewCell *cell):mValue(v), mViewCell(cell) {}
+
+	float mValue;
+	ViewCell *mViewCell;
+
+	friend bool operator<(const SortableViewCellEntry &a, const SortableViewCellEntry &b) {
+		return a.mValue < b.mValue;
+	}
 };
 
-ViewCell *
-ViewCellsManager::ConstructLocalMergeTree2(ViewCell *currentViewCell, 
-										   const ViewCellContainer &viewCells)
+
+ViewCell * ViewCellsManager::ConstructLocalMergeTree2(ViewCell *currentViewCell, 
+													  const ViewCellContainer &viewCells)
 {
   
@@ -2542,4 +2542,7 @@
 }
 
+
+
+
 /*******************************************************************/
 /*               BspViewCellsManager implementation                */
@@ -4885,5 +4888,5 @@
 	if (!mOspTree->GetRoot())
 	{
-		cout << "constructing vsp and osp tree" << endl;
+		Debug << "constructing vsp and osp tree" << endl;
 		//mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox);
 		mHierarchyManager->Construct2(constructionRays, objects, &mViewSpaceBox);
@@ -4891,5 +4894,5 @@
 	else // just build view space partition tree
 	{
-		cout << "constructing only vsp tree" << endl;
+		Debug << "constructing only vsp tree" << endl;
 		mHierarchyManager->Construct3(constructionRays, objects, &mViewSpaceBox);
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1159)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1174)
@@ -66,8 +66,8 @@
 	int mMaxCostMisses;
 
-	SplitCandidate()
+	SplitCandidate(): mRenderCostDecrease(0)
 	{};
 
-	SplitCandidate(const AxisAlignedPlane &plane): mSplitPlane(plane)
+	SplitCandidate(const AxisAlignedPlane &plane): mSplitPlane(plane), mRenderCostDecrease(0)
 	{}
 
@@ -1039,7 +1039,5 @@
 		RayInfoContainer &rays);
 
-	void EvalSubdivisionStats(const VspTraversalData &tData,
-							  const VspTraversalData &tFrontData,
-							  const VspTraversalData &tBackData);
+	void EvalSubdivisionStats(const SplitCandidate &tData);
 protected:
 
@@ -1668,4 +1666,10 @@
 	int ClassifyRay(VssRay *ray, KdLeaf *leaf, const AxisAlignedPlane &plane) const;
 
+	void EvalSubdivisionStats(const SplitCandidate &tData);
+
+	void AddSubdivisionStats(const int viewCells,
+		const float renderCostDecr,
+		const float totalRenderCost);
+
 protected:
 
@@ -1844,4 +1848,10 @@
 		RayInfoContainer &rays);
 
+	void EvalSubdivisionStats(const SplitCandidate &tData);
+
+	void AddSubdivisionStats(const int splits,
+		const float renderCostDecr,
+		const float totalRenderCost);
+
 
 protected:
@@ -1860,4 +1870,6 @@
 	/// keeps track of cost during subdivision
 	float mTotalCost;
+
+	ofstream mSubdivisionStats;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1166)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1174)
@@ -28,4 +28,5 @@
   KdPvs mKdPvs;
   
+  /// view cell based pvs per object
   ViewCellPvs mViewCellPvs;
 
