Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2667)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2669)
@@ -20,5 +20,15 @@
   
 #define GVS_DEBUG 0
-
+#define NOT_ACCOUNTED_OBJECT 0
+#define ACCOUNTED_OBJECT 2
+#define SHOW_QT_VISUALIZATION 0
+
+static const float MIN_DIST = 0.001f;
+
+static ObjectContainer myobjects;
+
+
+/** Visualization structure for the GVS algorithm.
+*/
 struct VizStruct
 {
@@ -28,9 +38,7 @@
 };
 
-
-static const float MIN_DIST = 0.001f;
-
-static ObjectContainer myobjects;
 static vector<VizStruct> vizContainer;
+
+
 
 
@@ -153,9 +161,6 @@
 		//newRay->mFlags |= VssRay::ReverseSample;
 		
-		// if ray is not further processed => delete ray
-		if (!HandleRay(newRay))
-		{
-			//delete newRay;
-		}
+		// check if ray is not further processed (ray not deleted because using ray pool)
+		HandleRay(newRay);
 		
 		return 1;
@@ -164,4 +169,29 @@
 	return 0;
 }
+
+
+void GvsPreprocessor::CountObject(Intersectable *triObj)
+{
+	if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) && (triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
+	{
+		++ triObj->mCounter;
+		++ mGenericStats2;
+	}
+}
+
+
+void GvsPreprocessor::UpdateStatsForVisualization(KdIntersectable *kdInt)
+{
+	mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
+
+	// count new objects in pvs due to kd node conversion	
+	myobjects.clear();
+	mKdTree->CollectObjects(kdInt->GetItem(), myobjects);
+
+	ObjectContainer::const_iterator oit, oit_end = myobjects.end();
+
+	for (oit = myobjects.begin(); oit != oit_end; ++ oit)
+		CountObject(*oit);
+}	
 
 
@@ -189,12 +219,13 @@
 		Intersectable *obj = ray.mTerminationObject;
 
-		if (obj->mCounter < 2)
+		// counter < 2 => not accounted for yet
+		if (obj->mCounter < ACCOUNTED_OBJECT)
 		{
-			obj->mCounter += 2;
+			obj->mCounter += ACCOUNTED_OBJECT;
 			mTrianglePvs.push_back(obj);
 		
 			mGenericStats = mTrianglePvs.size();
 
-			// if using kd pvs, exchange the triangle with the node in the pvs
+			// exchange the triangle with the node in the pvs for kd pvs
 			if (mUseKdPvs)
 			{
@@ -203,27 +234,11 @@
 				if (!node->Mailed())
 				{
+					// add to pvs
 					node->Mail();
 					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;
-						}
-					}
-				}			
+				
+					if (SHOW_QT_VISUALIZATION) UpdateStatsForVisualization(kdInt);	
+				}
 			}
 
@@ -902,7 +917,7 @@
 	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)
 	{
@@ -912,4 +927,5 @@
 		//mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
 		mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
+
 		mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
 
@@ -924,17 +940,12 @@
 			TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
             
-			// account for the overall pvs
-			if ((triObj->mCounter != 1) && (triObj->mCounter != 3))
-			{
-				++ triObj->mCounter;
-				++ mGenericStats2;
-			}
+			CountObject(triObj);
             
 			// the triangle itself intersects
 			if (box.Intersects(triObj->GetItem()))
 			{
-				if ((triObj->mCounter < 2))
+				if ((triObj->mCounter < ACCOUNTED_OBJECT))
 				{
-					triObj->mCounter += 2;
+					triObj->mCounter += ACCOUNTED_OBJECT;
 
 					mTrianglePvs.push_back(triObj);
@@ -957,5 +968,5 @@
 
 		for (oit = mObjects.begin(); oit != oit_end; ++ oit)
-			(*oit)->mCounter = 0;
+			(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
         ComputeViewCell(vc);
@@ -986,5 +997,5 @@
 
 		for (oit = mObjects.begin(); oit != oit_end; ++ oit)
-			(*oit)->mCounter = 0;
+			(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
 		ComputeViewCell(vc);
@@ -1049,5 +1060,5 @@
 
 		// hack: reset counter
-		(*oit)->mCounter = 0;
+		(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
 		if (!bv || bv->Mailed())
@@ -1252,5 +1263,4 @@
 	ProcessViewCell();
 
-	//mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize();
 	mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
 	mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
