Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1129)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1133)
@@ -324,5 +324,6 @@
 mOutOfBoundsCell(NULL),
 mStoreRays(false),
-mTimeStamp(1)
+mTimeStamp(1),
+mOspTree(NULL)
 {
 	bool randomize = false;
@@ -790,65 +791,31 @@
 
 
-/*void VspTree::RemoveParentViewCellReferences(VspTraversalData &parentData)
-{
-	RayInfoContainer::const_iterator it, it_end = parentData.mRays->end();
-
-	KdLeaf::NewMail();
-	Intersectable::NewMail();
-
-	ViewCell *vc = parentData.mNode->mViewCell;
-
-	const float highestContri = 1e25;
-
-	// add contributions from samples to the PVS
-	for (it = parentData.mRays->begin(); it != it_end; ++ it)
-	{
-		VssRay *ray = (*it).mRay;
-
-		Intersectable *obj = ray->mTerminationObject;
-
-		if (obj && !obj->Mailed())
-		{
-			obj->Mail();
-			// reduce object reference count by one
-			-- obj->mViewCellPvs;
-
-			set<KdLeaf *>::const_iterator kit, kit_end = obj->mKdLeaves.end();
-
-			for (kit = obj->mKdLeaves.begin(); kit != kit_end; ++ kit)
-			{
-				KdLeaf *leaf = *kit;
- 
-				if (!leaf->Mailed())
-				{
-					leaf->Mail();
-					leaf->mViewCellPvs.RemoveSample(vc, highestContri);
-				}
-			}
-		}
-
-		Intersectable *obj = ray->mOriginObject;
-
-		if (obj && !obj->Mailed())
-		{
-			obj->Mail();
-			// reduce object reference count by one
-			-- obj->mViewCellPvs;
-
-			set<KdLeaf *>::const_iterator kit, kit_end = obj->mKdLeaves.end();
-
-			for (kit = obj->mKdLeaves.begin(); kit != kit_end; ++ kit)
-			{
-				KdLeaf *leaf = *kit;
-
-				if (!leaf->Mailed())
-				{
-					leaf->Mail();
-					leaf->mViewCellPvs.RemoveSample(vc, highestContri);
-				}
-			}
-		}
-	}
-}*/
+KdNode *VspTree::GetKdNode(VssRay &ray, const bool termination) const
+{
+	if (termination)
+	{
+		if (!ray.mTerminationNode)
+		{
+			ray.mTerminationNode = mOspTree->GetRoot();
+		}
+
+		ray.mTerminationNode = 
+			mOspTree->GetLeaf(ray.mTermination, ray.mTerminationNode);
+
+		return ray.mTerminationNode;
+	}
+	else
+	{
+		if (!ray.mOriginNode)
+		{
+			ray.mOriginNode = mOspTree->GetRoot();
+		}
+
+		ray.mOriginNode = 
+			mOspTree->GetLeaf(ray.mOrigin, ray.mOriginNode);
+
+		return ray.mOriginNode;
+	}
+}
 
 
@@ -920,4 +887,8 @@
 
 			sc += contribution;
+
+			// potentially visible kd cells
+			const bool termination = true;
+			vc->AddKdPvsSample(GetKdLeaf(ray, termination));
 		}
 
@@ -932,4 +903,8 @@
 
 			sc += contribution;
+
+			// potentially visible kd cells
+			const bool termination = false;
+			vc->AddKdPvsSample(GetKdLeaf(ray, termination));
 		}
 
@@ -2826,6 +2801,5 @@
 
 	SplitObjects(splitPlane, leaf->mObjects, front->mObjects, back->mObjects);
-
-	ProcessLeafObjects(leaf, front, back);
+	//ProcessLeafObjects(leaf, front, back);
     
 	backData.mNode = back;
@@ -2992,5 +2966,6 @@
 	
 	float totalVol = PrepareHeuristics(node->mObjects);
-	int voll = 0, volr = totalVol;
+	float voll = 0;
+	float volr = totalVol;
   
 	const int totalPvs = (int)node->mObjects.size();
@@ -3024,5 +2999,5 @@
 	for (ci = mSplitCandidates->begin(); ci != ci_end; ++ ci)
 	{
-		EvalPvsIncr(*ci, voll, volr);
+		EvalViewCellVolumeIncr(*ci, voll, volr);
 
 		cout << "incr: " << ci->mObject->mViewCellPvs.GetSize() << " obj id " 
@@ -3060,7 +3035,7 @@
 	const float pFront = volFront;
 
-	const float penaltyOld = totalPvs;//EvalPvsPenalty(pvsSize, lowerPvsLimit, upperPvsLimit);
-    const float penaltyFront = totalPvs - pvsBack;//EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
-	const float penaltyBack = pvsBack;//EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
+	const float penaltyOld = (float)totalPvs;//EvalPvsPenalty(pvsSize, lowerPvsLimit, upperPvsLimit);
+    const float penaltyFront = (float)totalPvs - pvsBack;//EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
+	const float penaltyBack = (float)pvsBack;//EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
 	
 	const float oldRenderCost = penaltyOld * pOverall + Limits::Small;
@@ -3163,7 +3138,7 @@
 
 
-void OspTree::EvalPvsIncr(const SortableEntry &ci,
-						  int &pvsLeft,
-						  int &pvsRight) const
+void OspTree::EvalViewCellVolumeIncr(const SortableEntry &ci,
+									 float &volLeft,
+									 float &volRight) const
 {
 	Intersectable *obj = ci.mObject;
@@ -3172,13 +3147,13 @@
 	{
 		case SortableEntry::BOX_MIN:
-			AddContriToPvs(obj, pvsLeft);
+			AddContriToPvs(obj, volLeft);
 			break;
 			
 		case SortableEntry::BOX_MAX:
-			RemoveContriFromPvs(obj, pvsRight);
+			RemoveContriFromPvs(obj, volRight);
 			break;
 	}
 
-	cout << "pvs left: " << pvsLeft << " pvs right " << pvsRight << endl;
+	//cout << "vol left: " << volLeft << " vol right " << volRight << endl;
 }
 
@@ -3196,5 +3171,5 @@
 
 
-void OspTree::RemoveContriFromPvs(Intersectable *object, int &pvs) const
+void OspTree::RemoveContriFromPvs(Intersectable *object, float &pvs) const
 {
 	if (mUseEqualWeightForHeuristics)
@@ -3206,5 +3181,5 @@
 
 
-void OspTree::AddContriToPvs(Intersectable *object, int &pvs) const
+void OspTree::AddContriToPvs(Intersectable *object, float &pvs) const
 {
 	if (mUseEqualWeightForHeuristics)
@@ -3414,5 +3389,5 @@
 }
 
-
+#if DEPRECATED
 void OspTree::ProcessLeafObjects(KdLeaf *parent, KdLeaf *front, KdLeaf *back) const
 {
@@ -3491,5 +3466,5 @@
 	
 }
-
+#endif
 
 void OspTree::CollectLeaves(vector<KdLeaf *> &leaves) const
@@ -3608,4 +3583,45 @@
 
 
+KdLeaf *OspTree::GetLeaf(const Vector3 &pt, KdNode *node) const
+{
+	if (node == NULL)
+		node = mRoot;
+
+	stack<KdNode *> nodeStack;
+	nodeStack.push(node);
+  
+	KdLeaf *leaf = NULL;
+  
+	while (!nodeStack.empty())  
+	{
+		KdNode *node = nodeStack.top();
+		nodeStack.pop();
+	
+		if (node->IsLeaf()) 
+		{
+			leaf = dynamic_cast<KdLeaf *>(node);
+		} 
+		else 	
+		{	
+			KdInterior *interior = dynamic_cast<KdInterior *>(node);
+        
+			// random decision
+			if (interior->mPosition < pt[interior->mAxis])
+				nodeStack.push(interior->mBack);
+			else
+				nodeStack.push(interior->mFront);
+		}
+	}
+  
+	return leaf;
+}
+
+
+KdNode *OspTree::GetRoot() const
+{
+	return mRoot;
+}
+
+
 /********************************************************************/
 /*               class HierarchyManager implementation              */
@@ -3617,4 +3633,5 @@
 mVspTree(vspTree), mOspTree(ospTree)
 {
+	mVspTree.mOspTree = &ospTree;
 }
 
@@ -3634,6 +3651,5 @@
 
 void HierarchyManager::ProcessRays(const VssRayContainer &sampleRays,
-								   RayInfoContainer &rays,
-								   ObjectContainer &sampledObjects)
+								   RayInfoContainer &rays)
 {
 	VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
@@ -3664,17 +3680,4 @@
 			rays.push_back(RayInfo(ray, minT / len, maxT / len));
 		}
-
-		// store objects
-		if (ray->mTerminationObject && !ray->mTerminationObject->Mailed())
-		{
-			ray->mTerminationObject->Mail();
-			sampledObjects.push_back(ray->mTerminationObject);
-		}
-
-		if (ray->mOriginObject && !ray->mOriginObject->Mailed())
-		{
-			ray->mOriginObject->Mail();
-			sampledObjects.push_back(ray->mOriginObject);
-		}
 	}
 
@@ -3683,7 +3686,8 @@
 
 
-void HierarchyManager::PrepareVsp(const VssRayContainer &sampleRays,
-								  AxisAlignedBox3 *forcedViewSpace,
-								  RayInfoContainer &rays)
+SplitCandidate *HierarchyManager::PrepareVsp(const VssRayContainer &sampleRays,
+											 AxisAlignedBox3 *forcedViewSpace,
+											 RayInfoContainer &rays
+											 )
 {
 	// prepare bounding box
@@ -3691,10 +3695,8 @@
 
 	// get clipped rays
-	ObjectContainer sampledObjects;
-	ProcessRays(sampleRays, rays, sampledObjects);
-
-	//const int pvsSize = mVspTree.ComputePvsSize(rays);
-	const int pvsSize = (int)sampledObjects.size();
-
+	ProcessRays(sampleRays, rays);
+
+	const int pvsSize = mVspTree.ComputePvsSize(rays);
+	
 	cout <<  "here450 pvs size: " << pvsSize << endl;
 	// -- prepare view space partition
@@ -3703,4 +3705,5 @@
 	VspLeaf *leaf = new VspLeaf();
 	mVspTree.mRoot = leaf;
+
 	const float prop = mVspTree.mBoundingBox.GetVolume();
 
@@ -3721,23 +3724,22 @@
 		oit_end = leaf->GetViewCell()->GetPvs().mEntries.end();
 
-	cout << "here23 " << leaf->GetViewCell()->GetPvs().mEntries.size();
 
 	for (oit = leaf->GetViewCell()->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
 	{
 		Intersectable *obj = (*oit).first;
-		cout << "obj: " << obj->GetId() << endl;
 		obj->mViewCellPvs.AddSample(leaf->GetViewCell(), 1);
 	}
 
 	// compute first split candidate
-	VspTree::VspSplitCandidate *splitCandidate = new VspTree::VspSplitCandidate(vData);
+	VspTree::VspSplitCandidate *splitCandidate = 
+		new VspTree::VspSplitCandidate(vData);
     mVspTree.EvalSplitCandidate(*splitCandidate);
 
-	mTQueue.Push(splitCandidate);
-}
-
-
-void HierarchyManager::PrepareOsp(const ObjectContainer &objects,										  
-								  AxisAlignedBox3 *forcedViewSpace)
+	return splitCandidate;
+}
+
+
+SplitCandidate * HierarchyManager::PrepareOsp(const ObjectContainer &objects,
+											  AxisAlignedBox3 *forcedViewSpace)
 {
 	mOspTree.PrepareConstruction(objects, forcedViewSpace);
@@ -3762,5 +3764,5 @@
 
 		
-	mOspTree.ProcessLeafObjects(NULL, kdleaf, NULL);
+	//mOspTree.ProcessLeafObjects(NULL, kdleaf, NULL);
 
 	// compute first split candidate
@@ -3769,5 +3771,5 @@
     mOspTree.EvalSplitCandidate(*oSplitCandidate);
 
-	mTQueue.Push(oSplitCandidate);
+	return oSplitCandidate;
 }
 
@@ -3778,6 +3780,6 @@
 										   RayInfoContainer &rays)
 {
-	PrepareVsp(sampleRays, forcedViewSpace, rays);
-	PrepareOsp(objects, forcedViewSpace);
+	mTQueue.Push(PrepareVsp(sampleRays, forcedViewSpace, rays));
+	mTQueue.Push(PrepareOsp(objects, forcedViewSpace));
 }
 
@@ -3798,8 +3800,8 @@
 	// makes no sense otherwise because only one kd cell available
 	// during view space partition
-	const int savedCountMethod =
+	const int savedCountMethod = mVspTree.mPvsCountMethod;
 	mVspTree.mPvsCountMethod = VspTree::PER_OBJECT;
 
-	PrepareVsp(sampleRays, forcedViewSpace, *rays);
+	mTQueue.Push(PrepareVsp(sampleRays, forcedViewSpace, *rays));
 
 	long startTime = GetTime();
@@ -3842,5 +3844,5 @@
 
 	//-- object space partition
-    PrepareOsp(objects, forcedViewSpace);
+    mTQueue.Push(PrepareOsp(objects, forcedViewSpace));
 
 	i = 0;
