Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1180)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1181)
@@ -812,6 +812,6 @@
 
 	//-- compute pvs
-	frontData.mPvs = ComputePvsSize(*frontData.mRays);
-	backData.mPvs = ComputePvsSize(*backData.mRays);
+	frontData.mPvs = EvalPvsSize(*frontData.mRays);
+	backData.mPvs = EvalPvsSize(*backData.mRays);
 
 	// split front and back node geometry and compute area
@@ -2271,5 +2271,5 @@
 
 
-int VspTree::ComputePvsSize(const RayInfoContainer &rays) const
+int VspTree::EvalPvsSize(const RayInfoContainer &rays) const
 {
 	int pvsSize = 0;
@@ -2302,5 +2302,5 @@
 		}
 	}
-	else
+	else // compute pvs from kd nodes
 	{
 		KdNode::NewMail();
@@ -3047,5 +3047,7 @@
 		// initialise leaf references of objects
 		if (parent->IsRoot())
+		{
 			object->mReferences = 1;
+		}
 
 		// remove parent ref
@@ -3069,5 +3071,5 @@
 
 	mOspStats.objectRefs -= (int)objects.size();
-	mOspStats.objectRefs += (int)back.size() + (int)front.size();
+	mOspStats.objectRefs += (int)(back.size() + front.size());
 }
 
@@ -3104,11 +3106,4 @@
 	frontData.mDepth = backData.mDepth = tData.mDepth + 1;
 		
-	// TODO matt: compute pvs
-	frontData.mPvs = 999999; // ComputePvsSize(*frontData.mRays);
-	backData.mPvs = 9999999; //ComputePvsSize(*backData.mRays);
-
-	frontData.mProbability = frontData.mBoundingBox.GetVolume();
-
-	
 	//-- subdivide rays
 	frontData.mRays = new RayInfoContainer();
@@ -3139,4 +3134,8 @@
 	}
 
+	// TODO matt: compute pvs
+	frontData.mPvs = objectsFront;
+	backData.mPvs = objectsBack;
+
 	KdLeaf *back = new KdLeaf(node, objectsBack);
 	KdLeaf *front = new KdLeaf(node, objectsFront);
@@ -3166,6 +3165,12 @@
     ProcessMultipleRefs(back);
 
+	frontData.mNode = front;
 	backData.mNode = back;
-	frontData.mNode = front;
+
+
+	// compute probability, i.e., volume of seen view cells
+	frontData.mProbability = EvalViewCellsVolume(front, *frontData.mRays);
+	backData.mProbability =  EvalViewCellsVolume(back, *backData.mRays);
+
 
 	//delete leaf;
@@ -3446,12 +3451,12 @@
 			
 			// HACK: current positition is BETWEEN visibility events
-			if (1 && ((ci + 1) != ci_end))
+			if (1 && ((*ci).mType == SortableEntry::BOX_INTERSECT) && ((ci + 1) != ci_end))
 				currentPos = ((*ci).mPos + (*(ci + 1)).mPos) * 0.5f;
 			else
 				currentPos = (*ci).mPos;			
 
-			if ((totalVol - voll - volr - debugVol) / viewSpaceVol > 0.0001)
+			/*if ((totalVol - voll - volr - debugVol) / viewSpaceVol > 0.0001)
 				Debug << "front and back volume: " << (totalVol - voll - volr) / viewSpaceVol << " error: " << (totalVol - voll - volr - debugVol) / viewSpaceVol << endl;
-			/*	Debug << "pos: " << currentPos 
+				Debug << "pos: " << currentPos 
 				 << "\t (pvsl: " << pvsl << ", pvsr: " << pvsr << ")"
 				 << "\t (voll: " << voll << ", volr: " << volr << ")"
@@ -3961,6 +3966,6 @@
 
 	KdLeaf *leaf = tData.mNode;
-	int totalPvs = (int)leaf->mObjects.size();
-
+	const int totalPvs = (int)leaf->mObjects.size();
+	
 	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
 
@@ -3973,6 +3978,4 @@
 		Intersectable *obj = *oit;
 		const AxisAlignedBox3 box = obj->GetBox();
-
-		//cout << "totalpvs " << totalPvs << endl;
 
 		// test if box falls in left / right child node
@@ -4015,5 +4018,6 @@
 			ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
 
-			// mail view cell
+			// traverse through view cells and classify them according
+			// to them being seen from to back / front / front and back node
 			for (vit = viewCells.begin(); vit != vit_end; ++ vit)
 			{
@@ -4024,4 +4028,5 @@
 					collectedViewCells.push_back(vc);
 
+				// classify / mail the view cell
 				MailViewCell(*vit, classification);	
 			}
@@ -4029,17 +4034,27 @@
 	}
 
+
+	// evaluate view cells volume contribution with respect to the mail box classification
 	ViewCellContainer::const_iterator vit, vit_end = collectedViewCells.end();
 
-	// evaluate view cells volume contribution with respect to mail box
 	for (vit = collectedViewCells.begin(); vit != vit_end; ++ vit)
 	{
 		AddViewCellVolumeContri(*vit, pFront, pBack, pFrontAndBack);
 	}
+
 
 	// these are non-overlapping sets
 	pOverall = pFront + pBack + pFrontAndBack;
 
+
 	//-- pvs rendering heuristics
+
 	const float oldRenderCost = pOverall * totalPvs;
+	
+	// sum up the terms: 
+	// the view cells seeing
+	// a) the left node are weighted by the #left node objects
+	// b) the right node are weighted by the #right node objects
+	// c) both nodes are weighted by the #parent node objects
 	const float newRenderCost = pvsFront * pFront + pvsBack * pBack + totalPvs * pFrontAndBack;
 
@@ -4050,13 +4065,14 @@
 		  << "back pvs: " << pvsBack << " front pvs " << pvsFront << " total pvs: " << totalPvs << endl 
 		  << "back p: " << pBack / viewSpaceVol << " front p " << pFront / viewSpaceVol 
-		  << " front and back p " << pFrontAndBack / viewSpaceVol << " p: " << pOverall / viewSpaceVol << endl
+		  << " front and back p " << pFrontAndBack / viewSpaceVol << " p: " << tData.mProbability / viewSpaceVol << endl
 		  << "old rc: " << oldRenderCost / viewSpaceVol << " new rc: " << newRenderCost / viewSpaceVol << endl
 		  << "render cost decrease: " << renderCostDecrease << endl;
+		  
+	//if ((((pOverall - tData.mProbability) / viewSpaceVol) > 0.00001))
+	//	Debug << "ERROR!!"<<endl;
 
 	normalizedOldRenderCost = oldRenderCost / viewSpaceVol;
 
-	if (oldRenderCost < newRenderCost * 0.99)
-		Debug << "\nwarning2!!:\n" << "old rc: " << oldRenderCost * viewSpaceVol << " new rc: " << newRenderCost * viewSpaceVol << endl;
-	
+		
 	return renderCostDecrease;
 }
@@ -4224,19 +4240,4 @@
 		dirtyList.push_back(leaf->GetSplitCandidate());
 	}
-}
-
-
-int OspTree::ComputePvsSize(const ObjectContainer &objects) const
-{
-	int pvsSize = 0;
-
-	ObjectContainer::const_iterator oit, oit_end = objects.end();
-
-	for (oit = objects.begin(); oit != oit_end; ++ oit)
-	{
-		pvsSize += (*oit)->mViewCellPvs.GetSize();
-	}
-
-	return pvsSize;
 }
 
@@ -4486,13 +4487,19 @@
 {
 	if (vc->Mailed())
+	{
 		frontVol += vc->GetVolume();
+	}
 	else if (vc->Mailed(1))
+	{
 		backVol += vc->GetVolume();
+	}
 	else if (vc->Mailed(2))
+	{
 		frontAndBackVol += vc->GetVolume();
-}
-
-
-float OspTree::EvalViewCellsVolume(const RayInfoContainer &rays) const
+	}
+}
+
+
+float OspTree::EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const
 {
 	float vol = 0;
@@ -4505,5 +4512,8 @@
 		VssRay *ray = (*rit).mRay;
 
-		if (!ray->mTerminationObject && !ray->mOriginObject)
+		// process ray if the hit point with termination / origin object 
+		// is inside this kd leaf
+		if (!((ray->mOriginObject && (GetLeaf(ray->mOrigin, ray->mOriginNode) == leaf)) ||
+			  (ray->mTerminationObject && (GetLeaf(ray->mTermination, ray->mTerminationNode) == leaf))))
 			continue;
 		
@@ -4574,5 +4584,5 @@
 	mVspTree.PreprocessRays(sampleRays, rays);
 
-	const int pvsSize = mVspTree.ComputePvsSize(rays);
+	const int pvsSize = mVspTree.EvalPvsSize(rays);
 	
 	Debug <<  "pvs size: " << (int)pvsSize << endl;
@@ -4647,9 +4657,7 @@
 	mOspTree.mRoot = kdleaf;
 	
-	// add first candidate for view space partition
-
-	// TODO matt: this is something different than pvs size.
-	const int pvsSize = mOspTree.ComputePvsSize(objects);
-	const float prop = mOspTree.mBoundingBox.GetVolume();
+	const float prop = mOspTree.EvalViewCellsVolume(kdleaf, rays);
+
+	//-- add first candidate for view space partition
 
 	// first osp traversal data
@@ -4657,5 +4665,5 @@
 									0,
 									&rays,
-									pvsSize,
+									(int)objects.size(),
 									prop,
 									mOspTree.mBoundingBox);
@@ -4668,7 +4676,5 @@
 	mOspTree.EvalSplitCandidate(*oSplitCandidate);
 
-	const float vol = mOspTree.EvalViewCellsVolume(rays);
-
-	mOspTree.mTotalCost = (float)objects.size() * vol;
+	mOspTree.mTotalCost = (float)objects.size() * prop / mVspTree.GetBoundingBox().GetVolume();
 	mOspTree.EvalSubdivisionStats(*oSplitCandidate);
 
@@ -4780,4 +4786,5 @@
 	    
 		mTotalCost -= splitCandidate->GetRenderCostDecrease();
+
 		// cost ratio of cost decrease / totalCost
 		const float costRatio = splitCandidate->GetRenderCostDecrease() / mTotalCost;
@@ -4785,12 +4792,10 @@
 		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
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1144)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1181)
@@ -57,5 +57,5 @@
 		{
 			mForcedMaterial.mDiffuseColor.b = 1.0f;
-  			const float importance = (float)ospTree.ComputePvsSize(leaf->mObjects) / (float)maxPvs;
+  			const float importance = (float)leaf->mObjects.size() / (float)maxPvs;
 
 			mForcedMaterial.mDiffuseColor.r = importance;
@@ -72,4 +72,5 @@
 	return true;
 }
+
 
 void Exporter::ExportKdIntersectable(const KdIntersectable &kdObj)
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1180)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1181)
@@ -918,4 +918,8 @@
 							 float maxBand);
 
+	/** Evaluate pvs size associated with the rays.
+	*/
+	int EvalPvsSize(const RayInfoContainer &rays) const;
+
 	/** Computes pvs increase with respect to the previous pvs for heuristics.
 	*/
@@ -991,9 +995,4 @@
 		float &totalPvs) const;
 
-
-	/** Computes PVS size induced by the rays.
-	*/
-	int ComputePvsSize(const RayInfoContainer &rays) const;
-	
 	/** Collects pvs from rays.
 	*/
@@ -1056,6 +1055,5 @@
 	/** Rays will be clipped to the bounding box.
 	*/
-	void PreprocessRays(const VssRayContainer &sampleRays,
-		RayInfoContainer &rays);
+	void PreprocessRays(const VssRayContainer &sampleRays, RayInfoContainer &rays);
 
 	/** Evaluate subdivision statistics.
@@ -1406,10 +1404,4 @@
 
 
-	/** Compute "pvs size" of this object container
-		@note not really pvs size just weighted sum of object taking their
-		appearances in pvss into account
-	*/
-	int ComputePvsSize(const ObjectContainer &objects) const;
-
 	/** Returns kd leaf the point pt lies in, starting from root.
 	*/
@@ -1417,6 +1409,7 @@
 
 
-	/// pointer to the hierarchy of view cells
-	ViewCellsTree *mViewCellsTree;
+	ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
+
+	void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
 
 
@@ -1704,8 +1697,11 @@
 		float &volRight);
 
-	float EvalViewCellsVolume(const RayInfoContainer &rays) const;
+	float EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const;
 
 
 protected:
+
+	/// pointer to the hierarchy of view cells
+	ViewCellsTree *mViewCellsTree;
 
 	VspTree *mVspTree;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1154)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1181)
@@ -179,10 +179,8 @@
 	if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
 	{
-		cout << "here99" << endl; 
 		// create and run the preprocessor application in a parallel thread
-		pt.InitThread();	cout << "here77" << endl; 
+		pt.InitThread();
 		//pt.RunThread();
 
-		cout << "here51" << endl; 
 		// display the render widget
 		DisplayWidget();
@@ -193,5 +191,5 @@
 		pt.Main();
 	}
-	cout << "here39" << endl; 
+	
 	Cleanup();
 
