Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h	(revision 860)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h	(revision 1133)
@@ -12,5 +12,8 @@
 class Vector3;
 
+
 typedef vector<RayInfo> RayInfoContainer;
+
+
 /** Structure holding additional info about
 	the ray during traversal.
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;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1129)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1133)
@@ -5492,4 +5492,5 @@
 
 				// collect kd leaves
+				//KdLeaf =
 				set<KdLeaf *>::const_iterator kdit, kdit_end = obj->mKdLeaves.end();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1084)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1133)
@@ -715,4 +715,6 @@
 
 	///////////////////////////////////////////////////////////
+
+
 protected:
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1121)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1133)
@@ -252,5 +252,5 @@
 	const bool result = mPvs.AddSample(sample, pdf, contribution);
 
-	// update pvs size
+	// update pvs size scalar
 	mPvsSize = mPvs.GetSize();
 	mPvsSizeValid = true;
@@ -260,4 +260,12 @@
 
 
+bool ViewCell::AddKdPvsSample(KdLeaf *sample,
+							  const float pdf, 
+							  float &contribution)
+{
+	const bool result = mKdPvs.AddSample(sample, pdf, contribution);
+
+	return result;
+}
 
 /************************************************************************/
@@ -1687,9 +1695,5 @@
 		Intersectable *obj = (*oit).first;
 
-		set<KdLeaf *>::const_iterator kit, kit_end = obj->mKdLeaves.end();
-
-		for (kit = obj->mKdLeaves.begin(); kit != kit_end; ++ kit)
-		{
-			KdLeaf *leaf = *kit;
+/*		KdLeaf *leaf = ;
 
 			pvsSize += ((int)leaf->mObjects.size() - (int)leaf->mMultipleObjects.size());
@@ -1709,5 +1713,5 @@
 				}
 			}		
-		}
+		}*/
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1129)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1133)
@@ -32,4 +32,6 @@
 class KdInterior;
 class KdLeaf;
+class OspTree;
+
 
 class KdTreeStatistics;
@@ -752,4 +754,6 @@
 	ViewCellsTree *mViewCellsTree;
 
+	OspTree *mOspTree;
+
 protected:
 
@@ -1004,4 +1008,7 @@
 		vector<SplitCandidate *> &dirtyList);
 
+	KdNode *GetKdNode(VssRay &ray, const bool termination) const;
+
+
 protected:
 
@@ -1010,4 +1017,5 @@
 
 	ViewCellsManager *mViewCellsManager;
+	
 	vector<SortableEntry> *mSplitCandidates;
 
@@ -1091,5 +1099,5 @@
 	int mCreatedViewCells;
 
-	/// weight between  render cost decrease and node render cost
+	/// weight between render cost decrease and node render cost
 	float mRenderCostDecreaseWeight;
 };
@@ -1335,4 +1343,7 @@
 
 
+	KdLeaf *GetLeaf(const Vector3 &pt, KdNode *node) const;
+
+
 	/// pointer to the hierarchy of view cells
 	ViewCellsTree *mViewCellsTree;
@@ -1439,8 +1450,9 @@
 					  ObjectContainer &back);
 
+#if DEPRECATED
 	 /** does some post processing on the objects in the new child leaves.
 	 */
 	void ProcessLeafObjects(KdLeaf *parent, KdLeaf *front, KdLeaf *back) const;
-
+#endif
 	/** Selects an axis aligned for the next split.
 		@returns cost for this split
@@ -1539,10 +1551,10 @@
 		@param rightPvs updates the right pvs
 	*/
-	void EvalPvsIncr(const SortableEntry &ve,
-					  int &pvsLeft,
-					  int &pvsRight) const;
-
-	void RemoveContriFromPvs(Intersectable *object, int &pvs) const;
-	void AddContriToPvs(Intersectable *object, int &pvs) const;
+	void EvalViewCellVolumeIncr(const SortableEntry &ve,
+		float &volLeft,
+		float &volRight) const;
+
+	void RemoveContriFromPvs(Intersectable *object, float &pvs) const;
+	void AddContriToPvs(Intersectable *object, float &pvs) const;
 
 	/** Prepares objects for the cost heuristics.
@@ -1721,13 +1733,13 @@
 	void CollectDirtyCandidates(vector<SplitCandidate *> &dirtyList);
 
-	void PrepareVsp(const VssRayContainer &sampleRays,
+	SplitCandidate * PrepareVsp(const VssRayContainer &sampleRays,
 		AxisAlignedBox3 *forcedViewSpace,
 		RayInfoContainer &rays);
 
-	void PrepareOsp(const ObjectContainer &objects, AxisAlignedBox3 *forcedViewSpace);
+	SplitCandidate * PrepareOsp(const ObjectContainer &objects, 
+		AxisAlignedBox3 *forcedViewSpace);
 
 	void ProcessRays(const VssRayContainer &sampleRays,
-		RayInfoContainer &rays,
-		ObjectContainer &sampledObjects);
+		RayInfoContainer &rays);
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1121)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1133)
@@ -191,5 +191,5 @@
 	bool AddPvsSample(Intersectable *sample, const float pdf, float &contribution);
   
-
+	bool AddKdPvsSample(KdLeaf *sample, const float pdf, float &contribution);
 
 	/// Rays piercing this view cell.
@@ -267,4 +267,6 @@
 	/// the potentially visible objects
 	ObjectPvs mPvs;
+	/// the potentially visible kd leaves
+	KdPvs mKdPvs;
 
 	float mVolume;
@@ -685,4 +687,6 @@
 	*/
 	ViewCell *GetInitialLeftViewCell() const;
+	/** Returns leaf view cell initially associated with this merge candidate.
+	*/
 	ViewCell *GetInitialRightViewCell() const;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1112)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1133)
@@ -12,4 +12,5 @@
 class AxisAlignedBox3;
 class Intersectable;
+class KdNode;
 
 class VssRay {
@@ -50,4 +51,5 @@
   ViewCellContainer mViewCells;
   
+
   ////////////////////////
   // members related to importance sampling
@@ -70,4 +72,12 @@
   
   //////////////////////////////
+
+  
+  /// the kd node holding the termination point
+  KdNode *mTerminationNode;
+  /// the kd node holding the origin point
+  KdNode *mOriginNode;
+
+
   VssRay(
 		 const Vector3 &origin,
@@ -88,8 +98,10 @@
 	mViewCells(0),
 	mWeightedPvsContribution(0),
-	mPdf(pdf)
-  {
-    Precompute();
-  }
+	mPdf(pdf),
+	mTerminationNode(NULL),
+	mOriginNode(NULL)
+	{
+		Precompute();
+	}
 	
   VssRay(const Ray &ray):
@@ -100,5 +112,7 @@
 	mPass(0),
 	mViewCells(0),
-	mPdf(1.0f)
+	mPdf(1.0f),
+	mTerminationNode(NULL),
+	mOriginNode(NULL)
   {
 	if (ray.sourceObject.mObject)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1077)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1133)
@@ -31,5 +31,5 @@
 
   /// kd leaves that this intersectable belongs to
-  set<KdLeaf *> mKdLeaves;
+  //set<KdLeaf *> mKdLeaves;
 
   enum { MESH_INSTANCE, TRANSFORMED_MESH_INSTANCE, SPHERE, VIEW_CELL, OGRE_MESH_INSTANCE };
