Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2689)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2690)
@@ -17,5 +17,5 @@
 VIEWPOINTS=../data/vienna/testwalk.vp
 
-LOG_PREFIX=../src/SG08/testwalk_cropped
+LOG_PREFIX=../src/SG08/try
 #LOG_PREFIX=../src/SG08/city_full_100
 
@@ -30,5 +30,5 @@
 -scene_filename=$SCENE \
 -preprocessor=gvs \
--preprocessor_use_vbos+ \
+-preprocessor_use_vbos- \
 -gvs_epsilon=0.0001 \
 -gvs_total_samples=1000000 \
@@ -36,5 +36,5 @@
 -gvs_initial_samples=16 \
 -gvs_max_viewcells=1 \
--gvs_min_contribution=100 \
+-gvs_min_contribution=1500 \
 -gvs_per_viewcell=true \
 -gvs_stats=$LOG_PREFIX-gvsStats.log \
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2687)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2690)
@@ -231,10 +231,7 @@
 	void StorePvs(const ObjectContainer &objectPvs);
 
-	/** Compute visibility for a given view cell.
-	*/
-	void ComputeViewCell(ViewCell *vc);
-	/** Runs gvs on the current view cell.
-	*/
-	void ProcessViewCell();
+	/** Compute visibility for the current view cell using gvs
+	*/
+	void ComputeViewCell();
 	/** Use this for qt visualization.
 	*/
@@ -245,4 +242,10 @@
 
 	void CompileViewCellsFromPointList();
+
+	bool AddTriangleObject(Intersectable *triObj);
+
+	void ComputeStats();
+
+	int ConvertObjectPvs();
 
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2638)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2690)
@@ -584,5 +584,5 @@
 
 
-	void UpdateStatsForViewCell(ViewCell *viewCell, Intersectable *obj);
+	void UpdateStatsForViewCell(ViewCell *viewCell, Intersectable *obj, int numTriangles);
 
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2678)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2690)
@@ -2373,6 +2373,7 @@
 
 
-void ViewCellsManager::UpdateStatsForViewCell(ViewCell *vc, Intersectable *obj)
-{
+void ViewCellsManager::UpdateStatsForViewCell(ViewCell *vc, Intersectable *obj, int numTriangles)
+{
+#ifdef USE_VERBOSE_PVS
 	KdIntersectable *kdObj = static_cast<KdIntersectable *>(obj);
 
@@ -2399,7 +2400,6 @@
 
 	//cout << "x " << radius << " " << dist << " " << fullRadius << " " << f << " " << f * f << endl;
-
-	const int numTriangles = kdObj->ComputeNumTriangles();
-#ifdef USE_VERBOSE_PVS
+	//const int numTriangles = kdObj->ComputeNumTriangles();
+
 	vc->GetPvs().mStats.mDistanceWeightedTriangles += f * numTriangles; 
 	vc->GetPvs().mStats.mDistanceWeightedPvs += f ;
@@ -2427,5 +2427,5 @@
 	bool hasAbsContribution;
 
-	// todo: maybe not correct for kd node pvs
+	// todo: probably not correct for kd node pvs
 	if (addSamplesToPvs) 
 	{
@@ -2433,8 +2433,6 @@
 		//hasAbsContribution = viewCell->GetPvs().AddSample(obj,ray.mPdf);
 		
-		if (hasAbsContribution)
-		{
-			UpdateStatsForViewCell(viewCell, obj);
-		}
+		//if (hasAbsContribution) 	UpdateStatsForViewCell(viewCell, obj);
+		
 	}
 	else 
@@ -6195,5 +6193,5 @@
 		const int colorCode = 0;
 
-		const float maxRenderCost = -1;//UpdateObjectCosts();
+		const float maxRenderCost = -1;
 		const bool exportBounds = false;
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2689)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2690)
@@ -667,20 +667,4 @@
 	
 	//glFinish();
-#if 0
-	//-- now issue queries for all objects
-	for (int j = 0; j < (int)mObjects.size(); ++ j) 
-	{
-		mOcclusionQueries[j]->BeginQuery();
-		RenderIntersectable(mObjects[j]);
-		mOcclusionQueries[j]->EndQuery();
-
-		unsigned int pixelCount;
-
-		pixelCount = mOcclusionQueries[j]->GetQueryResult();
-		
-		mObjects[j]->mCounter += pixelCount;
-	}
-#else
-
 	int q = 0;
 
@@ -703,15 +687,4 @@
 		
 			//-- reenable other state
-#if 0
-			bool available;
-
-			do 
-			{
-				available = mOcclusionQueries[t]->ResultAvailable();
-				
-				if (!available) cout << "W";
-			} 
-			while (!available);
-#endif
 
 			pixelCount = mOcclusionQueries[t]->GetQueryResult();
@@ -722,8 +695,6 @@
 		
 		}
-
-		//j += q;
-	}
-#endif
+	}
+
 	//glFinish();
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
@@ -873,11 +844,7 @@
 
 
-
-
-void
-GlRendererBuffer::EvalRenderCostSample(RenderCostSample &sample,
-									   const bool useOcclusionQueries,
-									   const int threshold
-									   )
+void GlRendererBuffer::EvalRenderCostSample(RenderCostSample &sample,
+											const bool useOcclusionQueries,
+											const int threshold)
 {
 	// choose a random view point
@@ -902,8 +869,5 @@
 
 	for (it = mObjects.begin(); it != it_end; ++ it) 
-	{
 		(*it)->mCounter = 0;
-
-	}
 
 	++ mFrame;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2677)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2690)
@@ -1155,5 +1155,5 @@
 
 
-void KdTree::CollectObjectsWithoutMail(KdNode *n, ObjectContainer &objects)
+void KdTree::CollectObjectsWithDublicates(KdNode *n, ObjectContainer &objects)
 {
 	stack<KdNode *> nodeStack;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2670)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2690)
@@ -403,8 +403,9 @@
   void
   CollectObjects(KdNode *n, ObjectContainer &objects);
-void
-CollectObjectsWithoutMail(KdNode *n, ObjectContainer &objects);
-  void
-  CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
+  
+  /** Collects objects with dublicates (no mailing going on)
+  */
+  void CollectObjectsWithDublicates(KdNode *n, ObjectContainer &objects);
+  void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
 
   void
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2689)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2690)
@@ -152,5 +152,4 @@
 #endif
 	{
-		//cout << "r";
 		// apply reverse sampling to find the gap
 		VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
@@ -160,5 +159,5 @@
 
 		// set flag for visualization
-		//newRay->mFlags |= VssRay::ReverseSample;
+		if (0) newRay->mFlags |= VssRay::ReverseSample;
 		
 		// check if ray is not further processed (ray not deleted because using ray pool)
@@ -185,5 +184,5 @@
 void GvsPreprocessor::UpdateStatsForVisualization(KdIntersectable *kdInt)
 {
-	mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
+	//mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
 
 	// count new objects in pvs due to kd node conversion	
@@ -222,11 +221,7 @@
 
 		// counter < 2 => not accounted for yet
-		if (obj->mCounter < ACCOUNTED_OBJECT)
+			// test of triangle was accounted for yet
+		if (result = AddTriangleObject(obj))
 		{
-			obj->mCounter += ACCOUNTED_OBJECT;
-			mTrianglePvs.push_back(obj);
-		
-			mGenericStats = (int)mTrianglePvs.size();
-
 			// exchange the triangle with the node in the pvs for kd pvs
 			if (mUseKdPvs)
@@ -664,6 +659,5 @@
 
 	while (!mRayQueue.empty())//&& (mGvsStats.mTotalSamples + castSamples < mTotalSamples) )
-	{//cout<<"r";
-		//cout << "queue size: " << mRayQueue.size() << endl;
+	{
 		// handle next ray
 		VssRay *ray = mRayQueue.top();
@@ -794,63 +788,4 @@
 
 
-void GvsPreprocessor::ProcessViewCell()
-{
-	// compute object that directly intersect view cell
-	IntersectWithViewCell();
-
-	mGvsStats.mPerViewCellSamples = 0;
-
-	int oldContribution = mGvsStats.mTotalContribution;
-	int passSamples = 0;
-
-	mGenericStats = 0;
-	mGenericStats2 = 0;
-
-	while (1)
-	{
-		mRayCaster->InitPass();
-
-		// Ray queue empty => 
-		// cast a number of uniform samples to fill ray queue
-		int newSamples = CastInitialSamples(mInitialSamples, mSamplingType);
-
-		if (!mOnlyRandomSampling)
-		{
-			int samplesPerRun = ProcessQueue();
-			newSamples += samplesPerRun;
-			//if (samplesPerRun > 0) cout << "spr: " << samplesPerRun << " ";
-		}
-
-		passSamples += newSamples;
-        mGvsStats.mPerViewCellSamples += newSamples;
-
-		if (passSamples >= mGvsSamplesPerPass)
-		{
-			++ mPass;
-			mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
-
-			////////
-			//-- stats
-
-			mGvsStats.mPass = mPass;
-
-			cout << "\nPass " << mPass << " #samples: " << mGvsStats.mPerViewCellSamples << endl;
-			cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
-
-			//mGenericStats = mGvsStats.mPassContribution;
-
-			// termination criterium
-			if (mGvsStats.mPassContribution < mMinContribution)
-				break;
-
-			// reset
-			oldContribution = mGvsStats.mTotalContribution;
-			mGvsStats.mPassContribution = 0;
-			passSamples = 0;
-		}
-	}
-}
-
-
 void GvsPreprocessor::CompileViewCellsFromPointList()
 {
@@ -945,5 +880,5 @@
 }
 
-#if 0
+#if 1
 void GvsPreprocessor::IntersectWithViewCell()
 {
@@ -1066,7 +1001,5 @@
 void GvsPreprocessor::PerViewCellComputation()
 {
-	ViewCell *vc;
-
-	while (vc = NextViewCell())
+	while (mCurrentViewCell = NextViewCell())
 	{
 		// hack: reset counters
@@ -1076,5 +1009,5 @@
 			(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
-        ComputeViewCell(vc);
+        ComputeViewCell();
 	}
 }
@@ -1088,8 +1021,8 @@
 			continue;
 
-        ViewCell *vc = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
+        mCurrentViewCell = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
 
 		// no valid view cell or view cell already computed
-		if (!vc || !vc->GetPvs().Empty() || !mRendererWidget->mComputeGVS)
+		if (!mCurrentViewCell || !mCurrentViewCell->GetPvs().Empty() || !mRendererWidget->mComputeGVS)
 			continue;
 
@@ -1101,5 +1034,5 @@
 			(*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
 
-		ComputeViewCell(vc);
+		ComputeViewCell();
 		++ mProcessedViewCells;
 	}
@@ -1322,5 +1255,5 @@
 	app << "#Id\n" << mViewCellId << endl;
 	app << "#Time\n" << mTimePerViewCell << endl;;
-	app << "#TriaePvs\n" << mTrianglePvs << endl;
+	app << "#TriPvs\n" << mTrianglePvs << endl;
 	app << "#ObjectPvs\n" << mPerViewCellPvs << endl;
 	app << "#UpdatedPvs\n" << (int)mPvsCost << endl;
@@ -1351,10 +1284,8 @@
 
 
-void GvsPreprocessor::ComputeViewCell(ViewCell *vc)
+void GvsPreprocessor::ComputeViewCell()
 {
 	KdNode::NewMail();
 
-	mCurrentViewCell = vc;
-	
 	const long startTime = GetTime();
 
@@ -1364,10 +1295,92 @@
 
 	// compute the pvs of the current view cell
-	ProcessViewCell();
-
-	
+	// compute object that directly intersect view cell
+	//IntersectWithViewCell();
+
+	mGvsStats.mPerViewCellSamples = 0;
+
+	int oldContribution = mGvsStats.mTotalContribution;
+	int passSamples = 0;
+
+	mGenericStats = 0;
+	mGenericStats2 = 0;
+
+	while (1)
+	{
+		int oldPvsSize = 0;//mCurrentViewCell->GetPvs().GetSize();
+
+		mRayCaster->InitPass();
+
+		// Ray queue empty => 
+		// cast a number of uniform samples to fill ray queue
+		int newSamples = CastInitialSamples(mInitialSamples, mSamplingType);
+
+		if (!mOnlyRandomSampling)
+		{
+			int samplesPerRun = ProcessQueue();
+			newSamples += samplesPerRun;
+			//if (samplesPerRun > 0) cout << "spr: " << samplesPerRun << " ";
+		}
+
+		passSamples += newSamples;
+        mGvsStats.mPerViewCellSamples += newSamples;
+
+		if (passSamples >= mGvsSamplesPerPass)
+		{
+			if (GVS_DEBUG) VisualizeViewCell(mTrianglePvs);
+
+			const bool convertPerPass = false;
+
+			if (convertPerPass)
+			{	
+				int newObjects = ConvertObjectPvs();
+
+				cout << "added " << newObjects << " triangles to triangle pvs" << endl;
+				mGvsStats.mTotalContribution += newObjects;
+			}
+
+			++ mPass;
+			mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
+
+
+			////////
+			//-- stats
+
+			mGvsStats.mPass = mPass;
+
+			cout << "\nPass " << mPass << " #samples: " << mGvsStats.mPerViewCellSamples << endl;
+			cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
+			cout << "obj contri=" << mCurrentViewCell->GetPvs().GetSize() - oldPvsSize << " (of " << mMinContribution << ")" << endl;
+
+			// termination criterium
+			if (mGvsStats.mPassContribution < mMinContribution)
+				break;
+
+			// reset
+			oldContribution = mGvsStats.mTotalContribution;
+			mGvsStats.mPassContribution = 0;
+			passSamples = 0;
+		}
+	}
+	
+	// finally add objects that directly intersect the view cell
+	//IntersectWithViewCell();
+
+		
 	////////
 	//-- stats
 
+	// timing
+	mGvsStats.mTimePerViewCell = TimeDiff(startTime, GetTime()) * 1e-3f;
+
+	ComputeStats();
+
+	// clean up
+	mTrianglePvs.clear();
+}
+
+
+void GvsPreprocessor::ComputeStats()
+{
 	// compute pvs size using larger (bvh objects)
 	// note: for kd pvs we had to do this during gvs computation
@@ -1402,8 +1415,4 @@
 
 	mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
-	
-	// timing
-	const long currentTime = GetTime();
-	mGvsStats.mTimePerViewCell = TimeDiff(startTime, currentTime) * 1e-3f;
 
 
@@ -1411,5 +1420,5 @@
 	// global values
 
-	mGvsStats.mViewCells = mProcessedViewCells;//mPass;
+	mGvsStats.mViewCells = mProcessedViewCells;
 	mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
 	mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
@@ -1421,9 +1430,51 @@
 
 	cout << "id: " << mCurrentViewCell->GetId() << " pvs cost: " 
-	 << mGvsStats.mPvsCost << " pvs tri: " << mTrianglePvs.size() << endl;
-
-
-	mTrianglePvs.clear();
-}
-
-}
+	     << mGvsStats.mPvsCost << " pvs tri: " << mTrianglePvs.size() << endl;
+}
+
+
+ObjectContainer triangles;
+
+int GvsPreprocessor::ConvertObjectPvs()
+{
+	int newObjects = 0;
+
+	ObjectPvsIterator pit = mCurrentViewCell->GetPvs().GetIterator();
+
+	triangles.clear();
+
+	// output PVS of view cell
+	while (pit.HasMoreEntries())
+	{		
+		KdIntersectable *kdInt = static_cast<KdIntersectable *>(pit.Next());
+
+		mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), triangles);
+
+		ObjectContainer::const_iterator oit, oit_end = triangles.end();
+
+		for (oit = triangles.begin(); oit != oit_end; ++ oit)
+		{
+			if (AddTriangleObject(*oit))
+				++ newObjects;
+		}
+	}
+
+	return newObjects;
+}
+
+
+bool GvsPreprocessor::AddTriangleObject(Intersectable *triObj)
+{
+	if ((triObj->mCounter < ACCOUNTED_OBJECT))
+	{
+		triObj->mCounter += ACCOUNTED_OBJECT;
+
+		mTrianglePvs.push_back(triObj);
+		mGenericStats = (int)mTrianglePvs.size();
+		return true;
+	}
+
+	return false;
+}
+
+}
