Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2682)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2687)
@@ -11,10 +11,13 @@
 echo "starting $TARGET version"
 
-SCENE=../data/vienna/vienna_cropped.obj
-#SCENE=../data/vienna/city_full.obj
+#SCENE=../data/vienna/vienna_cropped.obj
+SCENE=../data/vienna/city_full.obj
 
 VIEWCELLS=../data/vienna/vienna_cropped-gradient-viewcells.xml.gz 
+VIEWPOINTS=../data/vienna/testwalk.vp
 
-LOG_PREFIX=../src/SG08/rebut
+#LOG_PREFIX=../src/SG08/vienna_cropped_100c
+LOG_PREFIX=../src/SG08/city_full_100
+
 ENVIRONMENT=gvs.env
 
@@ -27,10 +30,11 @@
 -scene_filename=$SCENE \
 -preprocessor=gvs \
+-preprocessor_use_vbos- \
 -gvs_epsilon=0.0001 \
--gvs_total_samples=100000 \
--gvs_samples_per_pass=100000 \
+-gvs_total_samples=1000000 \
+-gvs_samples_per_pass=1000000 \
 -gvs_initial_samples=16 \
--gvs_max_viewcells=1 \
--gvs_min_contribution=50 \
+-gvs_max_viewcells=20 \
+-gvs_min_contribution=100 \
 -gvs_per_viewcell=true \
 -gvs_stats=$LOG_PREFIX-gvsStats.log \
@@ -41,4 +45,5 @@
 -preprocessor_visibility_file=$LOG_PREFIX-i-gvs-b1-n4l.xml \
 -preprocessor_stats=$LOG_PREFIX-i-gvs-b1-n4l.log \
+-view_cells_random_viewpoint_list=$VIEWPOINTS \
 -preprocessor_histogram_file=$LOG_PREFIX-i-gvs-b1-n4f.hlog 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/gvs.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/gvs.env	(revision 2682)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/gvs.env	(revision 2687)
@@ -36,5 +36,5 @@
 
 	# internal: 0 intel: 1 2: havran 3: havran dyn
-	rayCastMethod 2
+	rayCastMethod 1
 
 	exportVisibility false
Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/run_demo_vienna
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/run_demo_vienna	(revision 2686)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/run_demo_vienna	(revision 2687)
@@ -8,6 +8,6 @@
 
 
-#SCENE=../data/vienna/vienna_cropped.obj
-SCENE=../data/vienna/city_full.obj
+SCENE=../data/vienna/vienna_cropped.obj
+#SCENE=../data/vienna/city_full.obj
 
 VIEWCELLS=../data/vienna/vienna_cropped-gradient-viewcells.xml.gz
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2677)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2687)
@@ -13,4 +13,5 @@
 class TriangleIntersectable;
 class KdIntersectable;
+
 
 /** View space partition statistics.
@@ -243,4 +244,6 @@
 	void CountObject(Intersectable *triObj);
 
+	void CompileViewCellsFromPointList();
+
 
 	//////////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2686)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2687)
@@ -1923,4 +1923,9 @@
 				   "false");
 	
+	RegisterOption("Preprocessor.useVbos",
+				   optBool,
+				   "preprocessor_use_vbos",
+				   "true");
+
 	RegisterOption("Preprocessor.quitOnFinish",
 				   optBool,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2685)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2687)
@@ -1192,6 +1192,7 @@
 	else
 	{
-		cout << "setting ray pool size to samples per pass" << endl;       
-		reserveRays = mSamplesPerPass * 2;
+		const int n = 4;
+		cout << "hack: setting ray pool size to multiple " << n << " of samples per pass" << endl;       
+		reserveRays = mSamplesPerPass * n;
 	}
 
@@ -1256,15 +1257,12 @@
 #if SHOW_RAYCAST_TIMING
 		if (castDoubleRays)
-			cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
+			cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M double rays/s" << endl;
 		else
-			cout << rays.size() / (1e3f * TimeDiff(t1, t2)) << "M rays/s" << endl;
+			cout << rays.size() / (1e3f * TimeDiff(t1, t2)) << "M single rays/s" << endl;
 #endif
 
 	}
-	//cout << "numDummy: " << numTransformed << endl;	
-	//cerr<<"Determining PVS objects"<<endl;
+
 	DeterminePvsObjects(vssRays);
-	//	cerr<<"done."<<endl;
-	
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2686)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2687)
@@ -104,7 +104,4 @@
 GlRendererBuffer(sceneGraph, viewcells, tree)
 {
-	mUseVbos = true;
-	//mUseVbos = false;
-	
 	//makeCurrent();
 	MakeLive();
@@ -267,5 +264,5 @@
 	pErrorPixels = (float)pixelCount / (GetWidth() * GetHeight());
 
-	const int pixelThres = 100000;
+	const int pixelThres = 0;
 
 	// some error happened
@@ -285,5 +282,5 @@
 			char filename[256];
 			//sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels);
-			sprintf(filename, "error-frame-%04d-%08d.png", mFrame, pixelCount);
+			sprintf(filename, "error-frame-%04d-%04d-%08d.png", mFrame, viewcell->GetId(), pixelCount);
 			QImage im = toImage();
 			string str = mSnapPrefix + filename;
@@ -292,10 +289,9 @@
 			im.save(qstr, "PNG");
 
-
+#if 0
 			///////////
 			//-- output computed pvs
 
-			mUseFalseColors = false;
-
+			//mUseFalseColors = true;
 			glPushAttrib(GL_CURRENT_BIT);
 			glColor3f(0, 1, 0);
@@ -312,6 +308,5 @@
 			RenderPvs(pvs);
 
-			mUseForcedColors = false;
-
+			//mUseForcedColors = false;
 			im = toImage();
 			sprintf(filename, "error-frame-%04d-%04d-%08d-pvs.png", mFrame, viewcell->GetId(), pixelCount);
@@ -321,4 +316,5 @@
 
 			glPopAttrib();
+#endif
 		}
 	}
@@ -575,5 +571,4 @@
 	if (viewcell) 
 	{
-#if 1
 		// copy the pvs so that it can be filtered ...
 		if (PvsChanged(viewcell)) 
@@ -607,11 +602,4 @@
 			mCurrentPvsCost = mPvsCache.mPvs.EvalPvsCost();
 		}
-#else
-	
-		// update the indices for rendering
-		PreparePvs(viewcell->GetPvs());
-		emit PvsUpdated();
-
-#endif
 
 		// Render PVS
@@ -643,12 +631,5 @@
 	else 
 	{
-		//OcclusionQuery *query = mOcclusionQueries[0];
-		//query->BeginQuery();
-		
 		RenderScene();
-
-		//query->EndQuery();
-		//int pixels = query->GetQueryResult();
-		//cout << " pixels: " << pixels;
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2686)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2687)
@@ -83,6 +83,4 @@
 mData(NULL),
 mIndices(NULL),
-//mUseVbos(true),
-mUseVbos(false),
 mComputeGVS(false),
 mCurrentFrame(100)
@@ -121,4 +119,7 @@
 		mViewPoint = Vector3(1038.7f, 192.4f, -471.0f);
 		mViewDirection = Vector3(-0.8f, 0.0f, -0.6f);
+
+		mViewPoint = Vector3(440.295, 196.959, -781.302);
+		mViewDirection = Vector3(-0.0566328, 0, -0.998395);
 	}
 
@@ -136,4 +137,7 @@
 	//mUseGlLists = true;
 	mUseGlLists = false;
+
+	bool mUseVbos;
+	Environment::GetSingleton()->GetBoolValue("Preprocessor.useVbos", mUseVbos);
 
 	if (mViewCellsManager->GetViewCellPointsList()->size())
@@ -1590,9 +1594,6 @@
 	for (sit = viewPoints.begin(); sit != sit_end; ++ sit, ++ i) 
 	{
-		if ((i % 100) == 99)
-			cout << "processed " << i << " view points " << endl;
-
-		//cout << "\n\nvp: " << (*sit) << endl;
 		SimpleRay sray = *sit;
+		
 		int pvsSize = -1;
 
@@ -1623,28 +1624,33 @@
 		// drop the first frame (for some reason, the first frame yields wrong pixel error)
 		
-		if (//(mFrame > 0) && 
-			(err >=  -1e-6f) && (err < (1.0f - 1e-6f)) && (pvsSize != 0)) 
-		{
-			if (err > mPvsStat.maxError)
+		if (pvsSize != 0)
+		{
+			// hack: test if error is suspiciously large
+			if ((err >=  -1e-6f) && (err < (1.0f - 1e-6f)))
 			{
-				mPvsStat.maxError = err;
-				cout << "new max error: " << mPvsStat.maxError * GetWidth() * GetHeight()  << endl;
+				if (err > mPvsStat.maxError)
+				{
+					mPvsStat.maxError = err;
+					cout << "new max error: " << mPvsStat.maxError * GetWidth() * GetHeight()  << endl;
+				}
+
+				mPvsStat.sumError += err;
+				mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
+
+				if (err == 0.0f)
+					++ mPvsStat.errorFreeFrames;
+
+				// $$ JB
+				// moved it back here not to count frames with negative err (backfacing triangle)
+				++ mPvsStat.frames;
+
+				if ((mPvsStat.frames % 100) == 0)
+					cout << "processed " << mPvsStat.frames << " valid view points " << endl;
 			}
-
-			mPvsStat.sumError += err;
-			mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
-
-			if (err == 0.0f)
-				++ mPvsStat.errorFreeFrames;
-
-			// $$ JB
-			// moved it back here not to count frames with negative err (backfacing triangle)
-			++ mPvsStat.frames;
-		}
-		else
-		{
-			cerr << "warning: negative error " << err << " at pvs size " << pvsSize << endl;
-		}
-
+			else
+			{
+				cerr << "warning: strange error (" << err << "), pvs size " << pvsSize << endl;
+			}
+		}
 	}
 
@@ -1703,8 +1709,8 @@
 
 
+	// backfacing polygon found -> not a valid viewspace sample
 	if (pixelCount > 0)
-	{
-		return false; // backfacing polygon found -> not a valid viewspace sample
-	}
+		return false; 
+	
 	return true;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2686)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2687)
@@ -174,5 +174,6 @@
 void GvsPreprocessor::CountObject(Intersectable *triObj)
 {
-	if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) && (triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
+	if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) && 
+		(triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
 	{
 		++ triObj->mCounter;
@@ -486,5 +487,5 @@
 	//CLEAR_CONTAINER(invalidSamples);
 	
-	//cout << "cast rays: " << castRays << endl;
+	//cout << "here3 cast rays: " << castRays << endl;
 	return castRays;
 }
@@ -658,9 +659,10 @@
 int GvsPreprocessor::ProcessQueue()
 {
+	++ mGvsStats.mGvsPass;
+
 	int castSamples = 0;
-	++ mGvsStats.mGvsPass;
 
 	while (!mRayQueue.empty())//&& (mGvsStats.mTotalSamples + castSamples < mTotalSamples) )
-	{
+	{//cout<<"r";
 		//cout << "queue size: " << mRayQueue.size() << endl;
 		// handle next ray
@@ -671,5 +673,5 @@
 
 		castSamples += newSamples;
-		//delete ray;
+		// note: don't have to delete because handled by ray pool
 	}
 
@@ -814,6 +816,10 @@
 
 		if (!mOnlyRandomSampling)
-			newSamples += ProcessQueue();
-		
+		{
+			int samplesPerRun = ProcessQueue();
+			newSamples += samplesPerRun;
+			//if (samplesPerRun > 0) cout << "spr: " << samplesPerRun << " ";
+		}
+
 		passSamples += newSamples;
         mGvsStats.mPerViewCellSamples += newSamples;
@@ -847,34 +853,67 @@
 
 
-void GvsPreprocessor::CompileViewCellsList()
-{
+void GvsPreprocessor::CompileViewCellsFromPointList()
+{
+	ViewCell::NewMail();
+
 	// Receive list of view cells from view cells manager
 	ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
-	
-	if (!vcPoints->empty())
-	{
-		cout << "processing view cell list" << endl;
-		
-		vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
-
-		for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
+
+	vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
+
+	for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
+	{
+		ViewCellPoints *vp = *vit;
+
+		if (vp->first) // view cell  specified
 		{
-			mViewCells.push_back((*vit)->first);
-
-			if (mViewCells.size() >= mMaxViewCells)
-				break;
+			ViewCell *viewCell = vp->first;
+
+			if (!viewCell->Mailed())
+			{
+				viewCell->Mail();
+				mViewCells.push_back(viewCell);
+
+				if (mViewCells.size() >= mMaxViewCells)
+					break;
+			}
 		}
+		else // no view cell specified => compute view cells using view point
+		{
+			SimpleRayContainer::const_iterator rit, rit_end = vp->second.end();
+
+			for (rit = vp->second.begin(); rit != rit_end; ++ rit)
+			{
+				SimpleRay ray = *rit;
+
+				ViewCell *viewCell = mViewCellsManager->GetViewCell(ray.mOrigin);
+
+				if (viewCell && !viewCell->Mailed())
+				{
+					viewCell->Mail();
+					mViewCells.push_back(viewCell);
+
+					if (mViewCells.size() >= mMaxViewCells)
+						break;
+				}
+			}
+		}
+	}
+}
+
+
+void GvsPreprocessor::CompileViewCellsList()
+{
+	if (!mViewCellsManager->GetViewCellPointsList()->empty())
+	{
+		cout << "processing view point list" << endl;
+		CompileViewCellsFromPointList();
 	}
 	else
 	{
+		ViewCell::NewMail();
+
 		while ((int)mViewCells.size() < mMaxViewCells)
 		{
-			if (0)
-			{
-				mViewCells.push_back(mViewCellsManager->GetViewCell((int)mViewCells.size()));
-				continue;
-			}
-
-			// HACK
 			const int tries = 10000;
 			int i = 0;
@@ -906,5 +945,5 @@
 }
 
-
+#if 0
 void GvsPreprocessor::IntersectWithViewCell()
 {
@@ -955,6 +994,79 @@
 		}
 	}
-}
-
+	cout << "added " << mTrianglePvs.size() << " triangles (" << mCurrentViewCell->GetPvs().GetSize() << " objects) by intersection" << endl;
+
+}
+
+#else
+
+void GvsPreprocessor::IntersectWithViewCell()
+{
+	//Intersectable::NewMail();
+	mCurrentViewCell->GetMesh()->ComputeBoundingBox();
+	AxisAlignedBox3 box = mCurrentViewCell->GetMesh()->mBox;
+
+	// compute pvs kd nodes that intersect view cell
+	ObjectContainer kdobjects;
+	mKdTree->CollectKdObjects(box, kdobjects);
+
+	ObjectContainer pvsKdObjects;
+
+	ObjectContainer::const_iterator oit, oit_end = kdobjects.end();
+
+	for (oit = kdobjects.begin(); oit != oit_end; ++ oit)
+	{
+       	KdIntersectable *kdInt = static_cast<KdIntersectable *>(*oit);
+	
+		myobjects.clear();
+		mKdTree->CollectObjects(kdInt->GetItem(), myobjects);
+
+		// account for kd object pvs
+		bool addkdobj = false;
+
+		ObjectContainer::const_iterator oit, oit_end = myobjects.end();
+
+		for (oit = myobjects.begin(); oit != oit_end; ++ oit)
+		{
+			TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
+             
+			// the triangle itself intersects => add object
+			if (box.Intersects(triObj->GetItem()))
+			{
+				if ((triObj->mCounter < ACCOUNTED_OBJECT))
+				{
+					triObj->mCounter += ACCOUNTED_OBJECT;
+
+					mTrianglePvs.push_back(triObj);
+					mGenericStats = (int)mTrianglePvs.size();
+
+					addkdobj = true;
+				}
+			}
+		}
+		
+		// add node to object pvs
+		if (addkdobj)
+			pvsKdObjects.push_back(kdInt);
+	}
+
+	// new mail because objects were mailed for kd tree search
+	KdNode::NewMail();
+
+	// now actually ADD new objects to object pvs
+	oit_end = pvsKdObjects.end();
+
+	for (oit = pvsKdObjects.begin(); oit != oit_end; ++ oit)
+	{
+       	KdIntersectable *kdInt = static_cast<KdIntersectable *>(*oit);
+	
+		kdInt->Mail();
+
+		mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
+		if (SHOW_QT_VISUALIZATION) UpdateStatsForVisualization(kdInt);	
+	}
+
+	cout << "added " << mTrianglePvs.size() << " triangles (" << (int)pvsKdObjects.size() << " objects) by intersection" << endl;
+}
+#endif
 
 void GvsPreprocessor::PerViewCellComputation()
@@ -1147,5 +1259,5 @@
 	}
 
-	cout << "cast " << 2 * mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl;
+	cout << "cast " << mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M single rays/s" << endl;
 
 	if (GVS_DEBUG)
@@ -1216,7 +1328,7 @@
 	app << "#Id\n" << mViewCellId << endl;
 	app << "#Time\n" << mTimePerViewCell << endl;;
-	app << "#TrianglePvs\n" << mTrianglePvs << endl;
+	app << "#TriaePvs\n" << mTrianglePvs << endl;
 	app << "#ObjectPvs\n" << mPerViewCellPvs << endl;
-	app << "#PvsCost\n" << (int)mPvsCost << endl;
+	app << "#UpdatedPvs\n" << (int)mPvsCost << endl;
 
 	app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl;
