Index: /GTP/trunk/Lib/Vis/Preprocessing/EvalStats/EvalStats.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/EvalStats/EvalStats.cpp	(revision 1291)
+++ /GTP/trunk/Lib/Vis/Preprocessing/EvalStats/EvalStats.cpp	(revision 1586)
@@ -141,5 +141,6 @@
 		// don't stop until cursor is placed one element behind
 		// or end of vector is reached
-		StatsContainer::const_iterator equalCostIt = std::upper_bound(firstStats.begin(), firstStats.end(), *it, vlt);
+		StatsContainer::const_iterator equalCostIt = 
+			std::upper_bound(firstStats.begin(), firstStats.end(), *it, vlt);
 
 		int j = (int)(equalCostIt - firstStats.begin());
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1580)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1586)
@@ -930,4 +930,5 @@
 		VspBspTraversalData tBackData;
 
+		////////////////////
 		//-- continue subdivision
 		
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1584)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1586)
@@ -911,8 +911,11 @@
 		cout << "compute new statistics ... " << endl;
 	
+		///////////////7
 		//-- propagate pvs or pvs size information
+
 		ObjectPvs pvs;
 		UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
 
+		///////////
 		//-- output stats
 		sprintf(str, "-%09d-eval.log", castSamples);
@@ -1128,12 +1131,15 @@
 									   ) const
 {
-  if (!vc->IsLeaf() && vc->GetMergeCost() < 0.0f) {
-	ViewCellInterior *vci = (ViewCellInterior *) vc;
-	ViewCellContainer::const_iterator it, it_end = vci->mChildren.end();
-	
-	for (it = vci->mChildren.begin(); it != it_end; ++ it)
-	  DeleteLocalMergeTree(*it);
-	vci->mChildren.clear();
-	delete vci;
+	if (!vc->IsLeaf() && vc->GetMergeCost() < 0.0f) 
+	{	
+		ViewCellInterior *vci = (ViewCellInterior *) vc;
+		ViewCellContainer::const_iterator it, it_end = vci->mChildren.end();
+
+        for (it = vci->mChildren.begin(); it != it_end; ++ it)
+			DeleteLocalMergeTree(*it);
+		
+		vci->mChildren.clear();
+		
+		delete vci;
   }
 }
@@ -1489,5 +1495,5 @@
 	//-- extrude new vertices for top of prism
 
-	Vector3 triNorm = baseTri.GetNormal();
+	const Vector3 triNorm = baseTri.GetNormal();
 	Triangle3 topTri;
 
@@ -2236,5 +2242,5 @@
   	if (viewSpaceFilterSize >= 0.0f) {
 
-  bool usePrVS = false;
+  const bool usePrVS = false;
 
   if (!usePrVS) {
@@ -2351,13 +2357,13 @@
 
 	// $$ warning collect objects takes only unmailed ones!
-	kdTree->CollectObjects(box,
-						   objects);
+	kdTree->CollectObjects(box, objects);
 	//	cout<<"collected objects="<<objects.size()<<endl;
 	ObjectContainer::const_iterator noi = objects.begin();
-	for (; noi != objects.end(); ++noi) {
-	  Intersectable *o = *noi;
-	  // $$ JB warning: pdfs are not correct at this point!
-	  nPvs.AddSample(o, Limits::Small);
-	  nPvsSize++;
+	for (; noi != objects.end(); ++ noi) 
+	{
+		Intersectable *o = *noi;
+		// $$ JB warning: pdfs are not correct at this point!	  
+		nPvs.AddSample(o, Limits::Small);
+		nPvsSize ++;
 	}
   }
@@ -2455,5 +2461,7 @@
 	}
 	
-	//-- interior node => propagate pvs up
+	////////////////
+	//-- interior node => propagate pvs up the tree
+
 	ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
 	interior->GetPvs().Clear();
@@ -2467,5 +2475,7 @@
 		ObjectPvs objPvs;
 		
+		//////////////////
 		//-- recursivly compute child pvss
+
 		UpdatePvsForEvaluation(*vit, objPvs);
 
@@ -2478,7 +2488,8 @@
 
 	//-- faster way of computing pvs:
-	//   construct merged pvs by adding 
-	//   and only those of the next pvs which were not mailed.
-	//   note: sumpdf is not correct!!
+	//-- construct merged pvs by adding
+	//-- and only those of the next pvs which were not mailed.
+	//-- note: sumpdf is not correct!!
+
 	vector<ObjectPvs>::iterator oit = pvsList.begin();
 
@@ -2508,6 +2519,6 @@
 	UpdateScalarPvsSize(interior, pvs.CountObjectsInPvs(), pvs.GetSize());
 	
-
-#else // really merge cells: slow put sumPdf is correct
+#else 
+	// really merge cells: slow put sumPdf is correct
 	viewCellInterior->GetPvs().Merge(backVc->GetPvs());
 	viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
@@ -2561,4 +2572,5 @@
 
 	const float prop = (float)limit / ((float)rays.size() + Limits::Small);
+
 	for (it = rays.begin(); it != it_end; ++ it)
 	{
@@ -4046,16 +4058,16 @@
 	////////////
 	//-- compression
-
+//#if HAS_TO_BE_REDONE
 	if (ViewCellsTreeConstructed() && mCompressViewCells)
 	{
-		int pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
+		int pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
 		Debug << "number of entries before compress: " << pvsEntries << endl;
 
 		mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
 
-		pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
+		pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
 		Debug << "number of entries after compress: " << pvsEntries << endl;
 	}
-
+//#endif
 
 	// collapse sibling leaves that share the same view cell
@@ -4919,17 +4931,19 @@
 	
 
-	///////////////
+	///////////
 	//-- compression
 
+//#if HAS_TO_BE_REDONE
 	if (ViewCellsTreeConstructed() && mCompressViewCells)
 	{
-		int pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
+		int pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
 		Debug << "number of entries before compress: " << pvsEntries << endl;
 
 		mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
 
-		pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
+		pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
 		Debug << "number of entries after compress: " << pvsEntries << endl;
 	}
+//#endif
 
 	/////////////
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1585)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1586)
@@ -53,6 +53,7 @@
 }
 
-/// Counts differences between pvss.
-inline int CountDiffPvs(ViewCell *vc)
+/** Counts contribution of the view cell to the pvs.
+*/
+inline int CountPvsContribution(ViewCell *vc)
 {
 	int count = 0;
@@ -257,5 +258,6 @@
 {
 	const bool result = mPvs.AddSample(sample, pdf, contribution);
-	mPvsSizeValid = false; // have to recompute pvs size
+	// have to recompute pvs size
+	mPvsSizeValid = false;
 
 	return result;
@@ -1038,6 +1040,6 @@
 		// set scalars
 		mViewCellsManager->UpdateScalarPvsSize(left, 
-			left->GetPvs().CountObjectsInPvs(), 
-			left->GetPvs().GetSize());
+				left->GetPvs().CountObjectsInPvs(), 
+				left->GetPvs().GetSize());
 			
 		// remove pvs, we don't store interior pvss
@@ -1384,10 +1386,17 @@
 void ViewCellsTree::EvalMergeCost(MergeCandidate &mc) const
 {
+	///////////////////
 	//-- compute pvs difference
+
 	int newPvs;
+	
 	if (1) // not valid if not using const cost per object!!
+	{
 		newPvs = ComputeMergedPvsSize(mc.mLeftViewCell->GetPvs(), mc.mRightViewCell->GetPvs());
+	}
 	else
+	{
 		newPvs = (int)ComputeMergedPvsCost(mc.mLeftViewCell->GetPvs(), mc.mRightViewCell->GetPvs());
+	}
 
 	const float newPenalty = EvalPvsPenalty(newPvs, 
@@ -1399,6 +1408,6 @@
 
 	//-- compute ratio of old cost
-	//   (i.e., added size of left and right view cell times pvs size)
-	//   to new rendering cost (i.e, size of merged view cell times pvs size)
+	//-- (i.e., added size of left and right view cell times pvs size)
+	//-- to new rendering cost (i.e, size of merged view cell times pvs size)
 	const float oldCost = GetRenderCost(vc1) + GetRenderCost(vc2);
 
@@ -1419,6 +1428,7 @@
 	}	
 	
-
+	///////////////////////////
 	//-- merge cost also takes deviation into account
+
 	float newDev, oldDev;
 
@@ -1788,5 +1798,5 @@
 	ObjectPvs newPvs;
 
-	// sum different intersectables
+	// sum up uniquely found intersectables
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
@@ -1881,5 +1891,5 @@
 	
 		// matt: bug! must evaluate kd pvss also
-		pvsSize += CountDiffPvs(root);
+		pvsSize += CountPvsContribution(root);
 	}
 
@@ -1892,5 +1902,5 @@
 		tstack.pop();
 		// matt: bug! must evaluate kd pvss also
-		pvsSize += CountDiffPvs(vc);
+		pvsSize += CountPvsContribution(vc);
 
 		if (!vc->IsLeaf())
@@ -1915,4 +1925,5 @@
 	int pvsSize = 0;
 
+	/////////////////
 	//-- compressed pvs
 
@@ -1931,5 +1942,5 @@
 		root = root->GetParent();
 		// count the pvs entries different from the already found ones	
-		pvsSize += CountDiffPvs(root);
+		pvsSize += CountPvsContribution(root);
 	}
 
@@ -1943,5 +1954,5 @@
 	
 		// count the pvs entries different from the already found ones	
-		pvsSize += CountDiffPvs(vc);
+		pvsSize += CountPvsContribution(vc);
 
 		if (!vc->IsLeaf())
@@ -2029,9 +2040,9 @@
 		sizeof(PvsData) + sizeof(Intersectable *);
 
-	return (float)GetStoredPvsEntriesNum(vc) * entrySize;
-}
-
-
-int ViewCellsTree::GetStoredPvsEntriesNum(ViewCell *root) const
+	return (float)CountStoredPvsEntries(vc) * entrySize;
+}
+
+//#if HAS_TO_BE_REDONE
+int ViewCellsTree::CountStoredPvsEntries(ViewCell *root) const
 {
 	int pvsSize = root->GetPvs().GetSize();
@@ -2046,5 +2057,5 @@
 		for (it = interior->mChildren.begin(); it != it_end; ++ it)
 		{
-			pvsSize += GetStoredPvsEntriesNum(*it);
+			pvsSize += CountStoredPvsEntries(*it);
 		}
 	}
@@ -2052,5 +2063,5 @@
 	return pvsSize;    	
 }
-
+//#endif
 
 int ViewCellsTree::ViewCellsStorage() const
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1528)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1586)
@@ -505,5 +505,5 @@
 {
 public:
-	/** Counts object int the pvs. Different to GetSize(), not
+	/** Counts object int the pvs. Different to method "GetSize", not
 		only the raw container size is returned,
 		but the individual contributions of the entries are summed up.
@@ -512,4 +512,6 @@
 };
 
+
+////////////
 //-- typedefs
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 1344)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 1586)
@@ -55,4 +55,21 @@
 }
 
+int
+TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
+{
+	// random barycentric coordinates
+	const float a = RandomValue(0, 1);
+	const float b = RandomValue(0, 1);
+	const float c = 1.0 - a - b;
+	
+	point = mItem.mVertices[0] + a * (mItem.mVertices[1] - mItem.mVertices[0]) +
+			mItem.mVertices[0] + b * (mItem.mVertices[2] - mItem.mVertices[0]) +
+			mItem.mVertices[1] + c * (mItem.mVertices[2] - mItem.mVertices[1]);
+	
+	normal = mItem.GetNormal();
+
+	return 0;
+}
+
  
 }
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1586)
@@ -275,5 +275,4 @@
 	*/
 	bool mPvsSizeValid;
-
 };
 
@@ -432,17 +431,20 @@
 	void GetPvs(ViewCell *vc, ObjectPvs &pvs) const;
 
-	/** Returns pvs size (i.e. the number of stored objects
+	/** Returns pvs size (i.e. the number of stored objects)
 	*/
 	int GetPvsSize(ViewCell *vc) const;
 
 	/** Returns number of entries associated with this view cell. 
-		This returns the same value as the GetPvsSize function for object pvs,
-		but most likely different values for kd node based pvs.
+
+		This returns the same value as the "GetPvsSize" function for object pvs
+		but most likely different values if we use object space grouping.
+		E.g., using bounding volumes.
 	*/
 	int GetPvsEntries(ViewCell *vc) const;
 
-	/** Returns the actual number of stored entries in the view cells sub tree.
-	*/
-	int GetStoredPvsEntriesNum(ViewCell *root) const;
+	/** Returns the number of physically stored entries in the view cells sub tree.
+		This can vary based on the current storage method
+	*/
+	int CountStoredPvsEntries(ViewCell *root) const;
 
 	/** Returns memory cost of this view cell.
@@ -458,5 +460,4 @@
 	enum {PVS_IN_INTERIORS, COMPRESSED, PVS_IN_LEAVES};
 
-	
 	/** If view cells in this tree have compressed pvs.
 	*/
@@ -503,6 +504,9 @@
 	void SetViewCellsManager(ViewCellsManager *vcm);
 
+
 protected:
 
+	/** Reads the environment and sets member variables.
+	*/
 	void ReadEnvironment();
 
@@ -598,9 +602,26 @@
 	void ExportPvs(ViewCell *viewCell, OUT_STREAM &stream);
 
+	/** Counts the logical number of entries in the pvs this view cell.
+		The pvs is assumed to be stored using lossless compression.
+	*/
 	int GetEntriesInPvsForCompressedStorage(ViewCell *vc) const;
+
+	/** Computes pvs size of this view cell.
+		The pvs is assumed to be stored using lossless compression.
+	*/
 	int GetPvsSizeForCompressedStorage(ViewCell *vc) const;
+	
+	/** Computes pvs size of this view cell.
+		The pvs is assumed to be stored in the leaves.
+	*/
 	int GetPvsSizeForLeafStorage(ViewCell *vc) const;
+
+	/** Counts the logical number of entries in the pvs this view cell.
+		The pvs is assumed to be stored using the leaves.
+	*/
 	int GetEntriesInPvsForLeafStorage(ViewCell *vc) const;
 
+	/** Update stats for the log.
+	*/
 	void UpdateStats(
 		ofstream &stats,
@@ -618,4 +639,7 @@
 		const float volume);
 
+
+	//////////////////////////////////////
+
 	/// if the view cell tree hold compressed pvs
 	int mViewCellsStorage;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1580)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1586)
@@ -2339,18 +2339,20 @@
 			// compute intersection with all objects in this leaf
 			VspLeaf *leaf = dynamic_cast<VspLeaf *>(node);
-			ViewCell *vc = leaf->GetViewCell();
+			ViewCell *viewCell;
+			if (0)
+				viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
+			else
+				viewCell = leaf->GetViewCell();
 
 			// don't have to mail if each view cell belongs to exactly one leaf
-			if (!useMailboxing || !vc->Mailed())
+			if (!useMailboxing || !viewCell->Mailed())
 			{
 				if (useMailboxing)
-					vc->Mail();
-
-				viewcells.push_back(vc);
+					viewCell->Mail();
+
+				viewcells.push_back(viewCell);
 				++ hits;
 			}
-#if 0
-			leaf->mRays.push_back(RayInfo(new VssRay(origin, termination, NULL, NULL, 0)));
-#endif
+
 			// get the next node from the stack
 			if (tStack.empty())
@@ -2364,4 +2366,5 @@
 			extp = s.mExitPoint;
 			maxt = s.mMaxT;
+
 			tStack.pop();
 		}
@@ -2511,7 +2514,11 @@
   
 	if (active)
+	{
 		return mViewCellsTree->GetActiveViewCell(viewcell);
+	}
 	else
+	{
 		return viewcell;
+	}
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 1421)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 1586)
@@ -47,7 +47,5 @@
 	//////////////
 	// specialised triangle ray casting version
-
-	///////////
-	//-- calc ray-plane intersection
+	// using ray-plane intersection
 	
 	// get triangle edge vectors and plane normal
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1486)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1586)
@@ -23,5 +23,5 @@
 	We eliminate already accounted kd nodes and objects using mailboxing. 
 */
-static int CountNewObjectsInKdNode(KdIntersectable *kdobj)
+static int EvalKdNodeContribution(KdIntersectable *kdobj)
 {
 	int pvs = 0;
@@ -76,8 +76,8 @@
 
 
-/** the pvs is the number of different objects in the node leaves
+/** Returns the the number of different objects in the leaves of the node.
 	We eliminate already accounted kd nodes and objects using mailboxing. 
 */
-static int CountNewObjectsInBvhNode(BvhIntersectable *bvhobj)
+static int EvalBvhNodeContribution(BvhIntersectable *bvhobj)
 {
 	BvhNode *node= bvhobj->GetItem();
@@ -113,5 +113,5 @@
 			{
 				BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
-                       
+
 				// add #objects exclusivly in this node
 				pvs += (int)leaf->mObjects.size();
@@ -126,4 +126,5 @@
 		}
 	}
+
 	return pvs;
 }
@@ -151,5 +152,5 @@
 					// found kd node
 					KdIntersectable *kdObj = dynamic_cast<KdIntersectable *>(obj);
-					pvs += CountNewObjectsInKdNode(kdObj);	
+					pvs += EvalKdNodeContribution(kdObj);	
 					break;
 				}
@@ -157,5 +158,5 @@
 				{
 					BvhIntersectable *bvhObj = dynamic_cast<BvhIntersectable *>(obj);
-					pvs += CountNewObjectsInBvhNode(bvhObj);
+					pvs += EvalBvhNodeContribution(bvhObj);
 					break;
 				}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1581)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1586)
@@ -983,6 +983,5 @@
 
 	int viewCellId;
-	
-	int len = attributes.getLength();
+	const int len = attributes.getLength();
 	  
 	for (int i = 0; i < len; ++ i) 
@@ -1038,5 +1037,5 @@
 {
 	AxisAlignedPlane plane;
-	int len = attributes.getLength();
+	const int len = attributes.getLength();
 
 	for (int i = 0; i < len; ++ i) 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1581)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1586)
@@ -185,4 +185,6 @@
 		return Intersectable::TRIANGLE_INTERSECTABLE;
 	}
+
+	int GetRandomSurfacePoint(Vector3 &point, Vector3 &normal);
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1449)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1586)
@@ -422,6 +422,7 @@
 Mesh::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
 {
-  const int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
-  
+  //const int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
+	const int faceIndex = (int)RandomValue(0, (Real)mFaces.size() - 0.5f);
+
   // assume the face is convex and generate a convex combination
   //
@@ -450,5 +451,5 @@
 {
   Plane3 plane;
-  int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
+  const int faceIndex = (int)RandomValue(0, (Real)mFaces.size() - 0.5f);
 	int tries;
   for (tries = 0; tries < maxTries; tries++) {
