Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2643)
@@ -12,4 +12,6 @@
 #include "BvHierarchy.h"
 #include "Polygon3.h"
+#include "IntersectableWrapper.h"
+
 
 
@@ -18,4 +20,5 @@
   
 #define GVS_DEBUG 0
+static ObjectContainer myobjects;
 
 struct VizStruct
@@ -37,5 +40,4 @@
 mCurrentViewCell(NULL),
 mCurrentViewPoint(Vector3(0.0f, 0.0f, 0.0f))
-//,mGenericStats(0)
 {
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
@@ -75,4 +77,7 @@
 
 	mGvsStats.Reset();
+
+	mGenericStats = 0;
+	mGenericStats2 = 0;
 }
 
@@ -184,9 +189,11 @@
 		Intersectable *obj = ray.mTerminationObject;
 
-		if (!obj->mCounter)
+		if (obj->mCounter < 2)
 		{
-			obj->mCounter = 1;
+			obj->mCounter += 2;
 			mTrianglePvs.push_back(obj);
 		
+			mGenericStats = mTrianglePvs.size();
+
 			// if using kd pvs, exchange the triangle with the node in the pvs
 			if (mUseKdPvs)
@@ -199,8 +206,26 @@
 					KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(node);
 					mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
+					//mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
+					
 					mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
+					myobjects.clear();
+					mKdTree->CollectObjects(node, myobjects);
+
+					// account for kd object pvs
+					ObjectContainer::const_iterator oit, oit_end = myobjects.end();
+
+					for (oit = myobjects.begin(); oit != oit_end; ++ oit)
+					{
+						TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
+
+						if ((triObj->mCounter != 1) && (triObj->mCounter != 3))
+						{
+							++ triObj->mCounter;
+							++ mGenericStats2;
+						}
+					}
 				}			
 			}
-			
+
 			result = true;
 		}
@@ -755,11 +780,12 @@
 
 	mGvsStats.mPerViewCellSamples = 0;
+
 	int oldContribution = mGvsStats.mTotalContribution;
 	int passSamples = 0;
-
 	mGenericStats = 0;
+	mGenericStats2 = 0;
 
 	//while (mGvsStats.mPerViewCellSamples < mTotalSamples) 
-	while (1) 
+	while (1)
 	{
 		mRayCaster->InitPass();
@@ -788,5 +814,5 @@
 			cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
 
-			//mGenericStats = GvsStats.mGvsStats.mPassContribution;
+			//mGenericStats = mGvsStats.mPassContribution;
 
 			// termination criterium
@@ -865,40 +891,53 @@
 	AxisAlignedBox3 box = mCurrentViewCell->GetMesh()->mBox;
 	
-	vector<KdLeaf *> leaves;
-	mKdTree->GetBoxIntersections(box, leaves);
-
-	vector<KdLeaf *>::const_iterator lit, lit_end = leaves.end();
-
-	for (lit = leaves.begin(); lit != leaves.end(); ++ lit)
-	{
-        KdLeaf *leaf = *lit;
+	//vector<KdLeaf *> leaves;
+	//mKdTree->GetBoxIntersections(box, leaves);
+
+	ObjectContainer kdobjects;
+	mKdTree->CollectKdObjects(box, kdobjects);
+	//vector<KdLeaf *>::const_iterator lit, lit_end = leaves.end();
+	//for (lit = leaves.begin(); lit != lit_end; ++ lit)
+	ObjectContainer::const_iterator oit, oit_end = kdobjects.end();
+	for (oit = kdobjects.begin(); oit != oit_end; ++ oit)
+	{
+       	// add to kdnode pvs
+		KdIntersectable *kdInt = static_cast<KdIntersectable *>(*oit);
 		
-		// add to kdnode pvs
-		if (mUseKdPvs)
-		{
-			leaf->Mail();
-			KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(leaf);
-			mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
-	
-			mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
-		}
-
-		ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
-
-		for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
+		//mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
+		mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
+		mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
+
+		myobjects.clear();
+		mKdTree->CollectObjects(kdInt->GetItem(), myobjects);
+
+		// account for kd object pvs
+		ObjectContainer::const_iterator oit, oit_end = myobjects.end();
+
+		for (oit = myobjects.begin(); oit != oit_end; ++ oit)
 		{
 			TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
-
+            
+			// account for the overall pvs
+			if ((triObj->mCounter != 1) && (triObj->mCounter != 3))
+			{
+				++ triObj->mCounter;
+				++ mGenericStats2;
+			}
+            
+			// the triangle itself intersects
 			if (box.Intersects(triObj->GetItem()))
 			{
-				if (!triObj->mCounter)
+				if ((triObj->mCounter < 2))
 				{
-					triObj->mCounter = 1;
+					triObj->mCounter += 2;
+
 					mTrianglePvs.push_back(triObj);
+					mGenericStats = mTrianglePvs.size();
 				}
-			}
+			}			
 		}
 	}
 }
+
 
 
@@ -929,15 +968,11 @@
 			continue;
 
-        //ViewCell *vc = mViewCellsManager->GetViewCell(mCurrentViewPoint);
         ViewCell *vc = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
 
-		//cout << "v " << mRendererWidget->GetViewPoint() << " ";
-
 		// no valid view cell or view cell already computed
-		if (!vc || !vc->GetPvs().Empty())
+		if (!vc || !vc->GetPvs().Empty() || !mRendererWidget->mComputeGVS)
 			continue;
 
-		//cout << "computing new view cell: " << vc->GetId() << endl;
-
+		mRendererWidget->mComputeGVS = false;
 		// hack: reset counters
 		ObjectContainer::const_iterator oit, oit_end = mObjects.end();
@@ -950,6 +985,4 @@
 	}
 }
-
-
 
 
@@ -999,5 +1032,4 @@
 
 	BvhLeaf::NewMail();
-	//KdNode::NewMail();
 
 	ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end();
@@ -1209,14 +1241,9 @@
 {
 	mCurrentViewCell = vc;
-
-	if (mUseKdPvs)
-	{
-		KdNode::NewMail();
-		//mKdPvs.clear();
-	}
+	KdNode::NewMail();
 
 	long startTime = GetTime();
 	cout << "\n***********************\n" 
-		<< "processing view cell " << mProcessedViewCells 
+		<< "computing view cell " << mProcessedViewCells 
 		<< " (id: " << mCurrentViewCell->GetId() << ")" << endl;
 
@@ -1256,5 +1283,7 @@
 			mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
 		*/
-		mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();;
+		mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
+
+		cout << "pvs cost: " << mCurrentViewCell->GetPvs().EvalPvsCost() << " pvs tri: " << mGenericStats2 << endl;
 	}
 
@@ -1279,19 +1308,5 @@
 
 	mTrianglePvs.clear();
-#if 0
-	if (GVS_DEBUG)
-	{
-		//VisualizeViewCell(mCurrentViewCell);
-		VisualizeViewCell(objectPvs);
-		CLEAR_CONTAINER(mVssRays);
-	}
-	cout << "finished" << endl;
-
-	if (mEvaluatePixelError || mExportVisibility)
-	{
-		StorePvs(objectPvs);
-	}
-#endif
-}
-
-}
+}
+
+}
