Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1108)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1109)
@@ -232,8 +232,8 @@
 
 
-void VspInterior::SetupChildLinks(VspNode *b, VspNode *f) 
-{
-    mBack = b;
-    mFront = f;
+void VspInterior::SetupChildLinks(VspNode *front, VspNode *back) 
+{
+    mBack = back;
+    mFront = front;
 }
 
@@ -551,4 +551,5 @@
 	{
 		RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
+
 		for (it = tData.mRays->begin(); it != it_end; ++ it)
 		{
@@ -564,5 +565,5 @@
 
 	mVspStats.contributingSamples += conSamp;
-	mVspStats.sampleContributions +=(int) sampCon;
+	mVspStats.sampleContributions += (int)sampCon;
 
 	// finally evaluate statistics for this leaf
@@ -638,5 +639,5 @@
 	{
 		//-- create new view cell
-		CreateViewCell(tData);
+		//CreateViewCell(tData);
 	
 		//-- store additional info
@@ -677,5 +678,4 @@
 	splitCandidate.SetPriority(priority);
 	splitCandidate.mMaxCostMisses = success ? splitCandidate.mParentData.mMaxCostMisses : splitCandidate.mParentData.mMaxCostMisses + 1;
-
 	//Debug << "p: " << tData.mNode << " depth: " << tData.mDepth << endl;
 }
@@ -703,4 +703,71 @@
 			  *backData.mRays);
 
+	
+	//Debug << "f: " << frontData.mRays->size() << " b: " << backData.mRays->size() << "d: " << tData.mRays->size() << endl;
+	//-- compute pvs
+	frontData.mPvs = ComputePvsSize(*frontData.mRays);
+	backData.mPvs = ComputePvsSize(*backData.mRays);
+
+	// split front and back node geometry and compute area
+	tData.mBoundingBox.Split(splitPlane.mAxis, splitPlane.mPosition, 
+							 frontData.mBoundingBox, backData.mBoundingBox);
+
+
+	frontData.mProbability = frontData.mBoundingBox.GetVolume();
+	backData.mProbability = tData.mProbability - frontData.mProbability;
+
+	
+    ///////////////////////////////////////////
+	// subdivide further
+
+	// store maximal and minimal depth
+	if (tData.mDepth > mVspStats.maxDepth)
+	{
+		Debug << "max depth increases to " << tData.mDepth << " at " << mVspStats.Leaves() << " leaves" << endl;
+		mVspStats.maxDepth = tData.mDepth;
+	}
+
+	// two more leaves
+	mVspStats.nodes += 2;
+    
+	VspInterior *interior = new VspInterior(splitPlane);
+
+#ifdef _DEBUG
+	Debug << interior << endl;
+#endif
+
+
+	//-- create front and back leaf
+
+	VspInterior *parent = leaf->GetParent();
+
+	// replace a link from node's parent
+	if (parent)
+	{
+		parent->ReplaceChildLink(leaf, interior);
+		interior->SetParent(parent);
+	}
+	else // new root
+	{
+		mRoot = interior;
+	}
+
+	VspLeaf *frontLeaf = new VspLeaf(interior);
+	VspLeaf *backLeaf = new VspLeaf(interior);
+
+	// and setup child links
+	interior->SetupChildLinks(frontLeaf, backLeaf);
+	
+	// add bounding box
+	interior->SetBoundingBox(tData.mBoundingBox);
+
+	// set front and back leaf
+	frontData.mNode = frontLeaf;
+	backData.mNode = backLeaf;
+
+	// explicitely create front and back view cell
+	CreateViewCell(frontData);
+	CreateViewCell(backData);
+
 	// TODO: 
 	// create front and back view cell
@@ -708,63 +775,8 @@
 	// add front and back view cell to "Potentially Visbilie View Cells" 
 	// of the objects in front and back pvs
-
-	//Debug << "f: " << frontData.mRays->size() << " b: " << backData.mRays->size() << "d: " << tData.mRays->size() << endl;
-	//-- compute pvs
-	frontData.mPvs = ComputePvsSize(*frontData.mRays);
-	backData.mPvs = ComputePvsSize(*backData.mRays);
-
-	// split front and back node geometry and compute area
-	tData.mBoundingBox.Split(splitPlane.mAxis, splitPlane.mPosition, 
-							 frontData.mBoundingBox, backData.mBoundingBox);
-
-
-	frontData.mProbability = frontData.mBoundingBox.GetVolume();
-	backData.mProbability = tData.mProbability - frontData.mProbability;
-
-	
-    ///////////////////////////////////////////
-	// subdivide further
-
-	// store maximal and minimal depth
-	if (tData.mDepth > mVspStats.maxDepth)
-	{
-		Debug << "max depth increases to " << tData.mDepth << " at " << mVspStats.Leaves() << " leaves" << endl;
-		mVspStats.maxDepth = tData.mDepth;
-	}
-
-	mVspStats.nodes += 2;
-
-    
-	VspInterior *interior = new VspInterior(splitPlane);
-
-#ifdef _DEBUG
-	Debug << interior << endl;
-#endif
-
-
-	//-- create front and back leaf
-
-	VspInterior *parent = leaf->GetParent();
-
-	// replace a link from node's parent
-	if (parent)
-	{
-		parent->ReplaceChildLink(leaf, interior);
-		interior->SetParent(parent);
-	}
-	else // new root
-	{
-		mRoot = interior;
-	}
-
-	// and setup child links
-	interior->SetupChildLinks(new VspLeaf(interior), new VspLeaf(interior));
-	
-	// add bounding box
-	interior->SetBoundingBox(tData.mBoundingBox);
-
-	// set front and back leaf
-	frontData.mNode = interior->GetFront();
-	backData.mNode = interior->GetBack();
+	RemoveParentViewCellReferences(tData.mNode->mViewCell);
+	AddViewCellReferences(frontLeaf->mViewCell);
+	AddViewCellReferences(backLeaf->mViewCell);
+
 
 	interior->mTimeStamp = mTimeStamp ++;
@@ -774,42 +786,124 @@
 
 
-
-void VspTree::ProcessViewCellObjects(ViewCell *parent, ViewCell *front, ViewCell *back) const
-{
-	if (parent)
-	{
-		// remove the parents from the object pvss
-		ObjectPvsMap::const_iterator oit, oit_end = parent->GetPvs().mEntries.end();
-
-		for (oit = parent->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			Intersectable *object = (*oit).first;
-			// HACK: make sure that the view cell is removed from the pvs
-			const float high_contri = 9999999;
-			object->mViewCellPvs.RemoveSample(parent, high_contri);
-		}
-	}
-
-	if (front)
-	{
-		// Add front view cell to the object pvsss
-		ObjectPvsMap::const_iterator oit, oit_end = front->GetPvs().mEntries.end();
-
-		for (oit = front->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			Intersectable *object = (*oit).first;
-			object->mViewCellPvs.AddSample(front, 1);
-		}
-	}
-
-	if (back)
-	{
-		// Add back view cell to the object pvsss
-		ObjectPvsMap::const_iterator oit, oit_end = back->GetPvs().mEntries.end();
-
-		for (oit = back->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			Intersectable *object = (*oit).first;
-			object->mViewCellPvs.AddSample(back, 1);
+/*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);
+				}
+			}
+		}
+	}
+}*/
+
+
+void VspTree::RemoveParentViewCellReferences(ViewCell *parent) const
+{
+	KdLeaf::NewMail();
+
+	// remove the parents from the object pvss
+	ObjectPvsMap::const_iterator oit, oit_end = parent->GetPvs().mEntries.end();
+
+	for (oit = parent->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
+	{
+		Intersectable *object = (*oit).first;
+		// HACK: make sure that the view cell is removed from the pvs
+		const float high_contri = 9999999;
+
+		// remove reference count of view cells
+		-- object->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->mViewCellPvs.RemoveSample(parent, high_contri);
+				leaf->Mail();
+			}
+		}
+	}
+}
+
+
+void VspTree::AddViewCellReferences(ViewCell *vc) const
+{
+	KdLeaf::NewMail();
+
+	// Add front view cell to the object pvsss
+	ObjectPvsMap::const_iterator oit, oit_end = front->GetPvs().mEntries.end();
+
+	for (oit = front->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
+	{
+		Intersectable *object = (*oit).first;
+
+		// increase reference count of view cells
+		++ object->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->mViewCellPvs.AddSample(front, 1);
+				leaf->Mail();
+			}
 		}
 	}
@@ -838,7 +932,9 @@
 		float contribution;
 
-		if (ray->mTerminationObject) 
-		{
-			if (vc->AddPvsSample(ray->mTerminationObject, ray->mPdf, contribution))
+		Intersectable *obj = ray->mTerminationObject;
+
+		if (obj) 
+		{
+			if (vc->AddPvsSample(obj, ray->mPdf, contribution))
 			{
 				madeContrib = true;
@@ -847,8 +943,10 @@
 			sc += contribution;
 		}
-	  
-		if (ray->mOriginObject) 
-		{
-			if (vc->AddPvsSample(ray->mOriginObject, ray->mPdf, contribution))
+
+		Intersectable *obj = ray->mOriginObject;
+
+		if (obj) 
+		{
+			if (vc->AddPvsSample(obj, ray->mPdf, contribution))
 			{
 				madeContrib = true;
@@ -857,6 +955,5 @@
 			sc += contribution;
 		}
-	 
-		sampleContributions += sc;
+
 
 		if (madeContrib)
@@ -2772,4 +2869,5 @@
 		// delete old leaf node
 		DEL_PTR(tData.mNode);
+		DEL_PTR(tData.mNode->mViewCell);
 	}
 
@@ -3427,9 +3525,10 @@
 
 	// add first candidate for view space partition
-	mVspTree.mRoot = new VspLeaf();
+	VspLeaf *leaf = new VspLeaf();
+	mVspTree.mRoot = leaf;
 	const float prop = mVspTree.mBoundingBox.GetVolume();
 
 	// first vsp traversal data
-	VspTree::VspTraversalData vData(mVspTree.mRoot,
+	VspTree::VspTraversalData vData(leaf,
 									0,
 									&rays,
@@ -3452,12 +3551,12 @@
 
 	// add first candidate for view space partition
-	KdLeaf *leaf = new KdLeaf(NULL, 0);
-	leaf->mObjects = objects;
-
-	mOspTree.mRoot = leaf;
+	KdLeaf *kdleaf = new KdLeaf(NULL, 0);
+	kdleaf->mObjects = objects;
+
+	mOspTree.mRoot = kdleaf;
 	
 
 	// first osp traversal data
-	OspTree::OspTraversalData oData(leaf,
+	OspTree::OspTraversalData oData(kdleaf,
 									0,
 									pvsSize,
@@ -3466,5 +3565,5 @@
 
 		
-	mOspTree.ProcessLeafObjects(leaf, NULL);
+	mOspTree.ProcessLeafObjects(kdleaf, NULL);
 
 	// compute first split candidate
@@ -3529,9 +3628,11 @@
 
 	// add first candidate for view space partition
-	mVspTree.mRoot = new VspLeaf();
+	VspLeaf *vspleaf = new VspLeaf();
+
+	mVspTree.mRoot = vspleaf;
 	const float prop = mVspTree.mBoundingBox.GetVolume();
 
 	// first vsp traversal data
-	VspTree::VspTraversalData vData(mVspTree.mRoot,
+	VspTree::VspTraversalData vData(vspleaf,
 									0,
 									rays,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1108)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1109)
@@ -367,7 +367,8 @@
 	*/
 	void ReplaceChildLink(VspNode *oldChild, VspNode *newChild);
+
 	/** Replace front and back child.
 	*/
-	void SetupChildLinks(VspNode *b, VspNode *f);
+	void SetupChildLinks(VspNode *front, VspNode *back);
 
 	friend ostream &operator<<(ostream &s, const VspInterior &A)
@@ -467,5 +468,5 @@
 	public:
 		/// the current node
-		VspNode *mNode;
+		VspLeaf *mNode;
 		/// current depth
 		int mDepth;
@@ -502,5 +503,5 @@
 		{}
 		
-		VspTraversalData(VspNode *node, 
+		VspTraversalData(VspLeaf *node, 
 						 const int depth, 
 						 RayInfoContainer *rays,
@@ -732,4 +733,12 @@
 	ViewCellsTree *mViewCellsTree;
 
+	/** Remove the references of the parent view cell from the kd nodes associated with
+		the objects.
+	*/
+	void RemoveParentViewCellReferences(ViewCell *parent);
+	/** Adds references to the view cell to the kd nodes associated with the objects.
+	*/
+	void AddViewCellReferences(ViewCell *vc) const;
+
 
 protected:
@@ -751,6 +760,6 @@
   
 		SortableEntry() {}
-		SortableEntry(const int t, const float v, VssRay *r):type(t),
-					  value(v), ray(r) 
+		SortableEntry(const int t, const float v, VssRay *r): 
+		type(t), value(v), ray(r) 
 		{
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1089)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1109)
@@ -201,5 +201,8 @@
   /** PVS consisting of visible KdTree nodes */
   KdPvs mKdPvs;
-	
+
+  /// pvs of view cells seeing this node.
+  // ViewCellPvs mViewCellPvs;
+
   /** pointer to view cell.
   */
