Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/Plugin_VisibilitySceneManager.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/Plugin_VisibilitySceneManager.vcproj	(revision 2560)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/Plugin_VisibilitySceneManager.vcproj	(revision 2643)
@@ -179,5 +179,5 @@
 				Name="VCLinkerTool"
 				IgnoreImportLibrary="TRUE"
-				AdditionalDependencies="OgreMain.lib CEGUIBase.lib OgreGUIRenderer.lib Plugin_OctreeSceneManager.lib GtpVisibility.lib zdll.lib zziplib.lib Preprocessor.lib xerces-c_2.lib devil.lib glut32.lib OpenGL32.Lib glu32.lib glew32.lib glew32s.lib RTScene.lib RTWorld.lib IVReader.lib"
+				AdditionalDependencies="OgreMain.lib CEGUIBase.lib OgreGUIRenderer.lib Plugin_OctreeSceneManager.lib GtpVisibility.lib zdll.lib zziplib.lib Preprocessor.lib xerces-c_2.lib devil.lib glut32.lib OpenGL32.Lib glu32.lib glew32.lib glew32s.lib RTScene.lib RTWorld.lib IVReader.lib cg.lib cgGL.lib"
 				OutputFile="$(OutDir)/Plugin_VisibilitySceneManager.dll"
 				Version="0.99"
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.vcproj	(revision 2560)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.vcproj	(revision 2643)
@@ -152,5 +152,5 @@
 			<Tool
 				Name="VCLinkerTool"
-				AdditionalDependencies="OgreMain.lib zdll.lib zziplib.lib Preprocessor.lib xerces-c_2.lib RTScene.lib RTWorld.lib"
+				AdditionalDependencies="OgreMain.lib zdll.lib zziplib.lib Preprocessor.lib xerces-c_2.lib RTScene.lib RTWorld.lib devil.lib cg.lib cgGL.lib glew32.lib"
 				OutputFile="$(OutDir)/ObjReader.dll"
 				LinkIncremental="1"
Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/gvs.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/gvs.env	(revision 2625)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/gvs.env	(revision 2643)
@@ -95,4 +95,5 @@
 
 KdTree {
+	pvsArea   1e-5
 	sahUseFaces true
 
Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/demo1.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/demo1.env	(revision 2633)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/demo1.env	(revision 2643)
@@ -170,5 +170,5 @@
 
 KdTree {
-	pvsArea   3e-5
+	pvsArea   1e-5
 	sahUseFaces false
 	Termination {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 2633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 2643)
@@ -145,5 +145,5 @@
 				OptimizeForWindowsApplication="TRUE"
 				AdditionalIncludeDirectories="..\include;..\..\..\..\..\..\NonGTP\Boost;..\src;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;&quot;$(QTDIR)\include\QtOpenGl&quot;;&quot;$(QTDIR)\include\Qt&quot;;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include&quot;;QtInterface;..\src\sparsehash\src\;..\src\ootl\src\include\ootl;..\src\ootl\src\include\ootl\sandbox;$(NOINHERIT)"
-				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;GTP_INTERNAL;USE_QT;USE_CG;USE_HASH_PVS;QT_OPENGL_LIB;QT_DLL"
+				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;GTP_INTERNAL;USE_QT;USE_CG;USE_VERBOSE_PVS;QT_OPENGL_LIB;QT_DLL"
 				StringPooling="TRUE"
 				MinimalRebuild="TRUE"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 2636)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 2643)
@@ -72,5 +72,4 @@
 
 #if DYNAMIC_OBJECTS_HACK
-	void RebuildKtbTree();
 	SceneGraphLeafIntersectable *GetIntersectable() { return mIntersectable; }
 #endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp	(revision 2635)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ObjectPvs.cpp	(revision 2643)
@@ -16,4 +16,5 @@
 namespace GtpVisibilityPreprocessor {
 
+static ObjectPvs dummyPvs;
 
 /** the pvs is the number of different objects in the node leaves
@@ -33,8 +34,7 @@
 
 		// already processed node (=> objects already in pvs)?
-		if (!node->Mailed())
+		if (1)//!node->Mailed())
 		{
-			node->Mail();
-
+			//node->Mail();
 			if (node->IsLeaf())
 			{
@@ -50,10 +50,11 @@
 				for (oit = leaf->mMultipleObjects.begin(); oit != oit_end; ++ oit)
 				{
-					Intersectable *object = *oit;						
+					Intersectable *obj = *oit;						
                             
-					if (!object->Mailed())
+					if (1)//!obj->Mailed())
 					{
-						object->Mail();
-						++ pvs;
+						//obj->Mail();
+						if (dummyPvs.AddSample(obj))
+							++ pvs;
 					}
 				}
@@ -66,10 +67,11 @@
 				for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
 				  {
-					Intersectable *object = *oit;						
+					Intersectable *obj = *oit;						
 					
-					if (!object->Mailed())
+					if (!obj->Mailed2())
 					  {
-						object->Mail();
-						++ pvs;
+						  //if (dummyPvs.AddSample(obj, 1.0f))++ pvs;
+						  obj->Mail2();
+						  ++pvs;
 					  }
 				  }
@@ -78,5 +80,5 @@
 			}
 			else // traverse tree
-			  {
+			{
 				KdInterior *interior = static_cast<KdInterior *>(node);
 
@@ -87,4 +89,5 @@
 	}
 
+	///return dummyPvs.Size();
 	return pvs;
 }
@@ -152,10 +155,12 @@
 
 
+
 float ObjectPvs::EvalPvsCost() const
 {
 	float pvs = 0;
 
-	Intersectable::NewMail();
-	KdNode::NewMail();
+	Intersectable::NewMail2();
+	//KdNode::NewMail();
+	dummyPvs.Clear();
 
 	ObjectPvsIterator pit = GetIterator();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 2621)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 2643)
@@ -6,5 +6,5 @@
 #include "Intersectable.h"
 #include "IntersectableWrapper.h"
-#include "ktball.h"
+//#include "ktball.h"
 
 
@@ -279,7 +279,4 @@
 
 SceneGraphLeaf::SceneGraphLeaf(): mIsDynamic(false)
-#if DYNAMIC_OBJECTS_HACK
-, mKtbTree(NULL)
-#endif
 {
 	mTrafo = IdentityMatrix();
@@ -292,7 +289,4 @@
 
 SceneGraphLeaf::SceneGraphLeaf(bool isDynamic): mIsDynamic(isDynamic)
-#if DYNAMIC_OBJECTS_HACK
-, mKtbTree(NULL)
-#endif
 {
     mTrafo = IdentityMatrix();
@@ -324,18 +318,4 @@
 
 
-#if DYNAMIC_OBJECTS_HACK
-
-
-void SceneGraphLeaf::RebuildKtbTree()
-{
-	if (mKtbTree)
-		delete mKtbTree;
-
-	mKtbTree = new CKTB();
-	mKtbTree->BuildUp(mGeometry);
-}
-
-#endif
-
-
-}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.cpp	(revision 2582)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.cpp	(revision 2643)
@@ -17,4 +17,10 @@
 namespace GtpVisibilityPreprocessor {
 
+
+int Intersectable::sMailId = 10000;//2147483647;
+int Intersectable::sReservedMailboxes = 1;
+
+int Intersectable::sMailId2 = 10000;//2147483647;
+int Intersectable::sReservedMailboxes2 = 1;
 
 // This is the result of computing intersection
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2638)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2643)
@@ -1045,29 +1045,34 @@
 	GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
 
-  if ((rayCastMethod == 2) || (rayCastMethod == 3)) {
-    HavranRayCaster *hr = 0;
-    if (rayCastMethod == 3)
-      hr = reinterpret_cast<HavranDynRayCaster*>(mRayCaster);
-    else
-      hr = reinterpret_cast<HavranRayCaster*>(mRayCaster);
-
-    string ktbFilename = internKdTree;
-    int l = ktbFilename.length();
-    ktbFilename[l-1] = 't';
-    ktbFilename[l-2] = 'b';
-    ktbFilename[l-3] = 'k';
-		
-    cout << "Trying to load tree from file " << ktbFilename << endl;
-    if (hr->ImportBinTree(ktbFilename, this->mObjects)) {
-      cout << "Loading failed - building kd-tree" << endl;
-      hr->Build(this->mObjects);
-      cout << "Exporting kd-tree to file " << ktbFilename << endl;
-      hr->ExportBinTree(ktbFilename);
-    }
-    else
-      cout << " done." << endl;
+#ifdef USE_HAVRAN_RAYCASTER
+
+  if ((rayCastMethod == 2) || (rayCastMethod == 3)) 
+  {
+	  HavranRayCaster *hr = 0;
+
+	  if (rayCastMethod == 3)
+		  hr = reinterpret_cast<HavranDynRayCaster*>(mRayCaster);
+	  else
+		  hr = reinterpret_cast<HavranRayCaster*>(mRayCaster);
+
+	  string ktbFilename = internKdTree;
+	  int l = ktbFilename.length();
+	  ktbFilename[l-1] = 't';
+	  ktbFilename[l-2] = 'b';
+	  ktbFilename[l-3] = 'k';
+
+	  cout << "Trying to load tree from file " << ktbFilename << endl;
+	  if (hr->ImportBinTree(ktbFilename, this->mObjects)) {
+		  cout << "Loading failed - building kd-tree" << endl;
+		  hr->Build(this->mObjects);
+		  cout << "Exporting kd-tree to file " << ktbFilename << endl;
+		  hr->ExportBinTree(ktbFilename);
+	  }
+	  else
+		  cout << " done." << endl;
   }
-  
-  
+#endif
+
+
   if (!mUseKdTree) {
 	// create just a dummy KdTree
@@ -1134,11 +1139,15 @@
 	if (rayCastMethod == 2) 
 	{
+#ifdef USE_HAVRAN_RAYCASTER
 	  cout << "ray cast method: havran" << endl <<flush;
 	  mRayCaster = new GALIGN16 HavranRayCaster(*this);
+#endif
 	}
 	if (rayCastMethod == 3) 
 	{
+#ifdef USE_HAVRAN_RAYCASTER
 	  cout << "ray cast method: havran - dyn" << endl <<flush;
 	  mRayCaster = new GALIGN16 HavranDynRayCaster(*this);
+#endif
 	}
 
@@ -1759,5 +1768,5 @@
 		cout<<leaf->GetBox();
 		mDynamicObjects.push_back(leaf);
-		//	  leaf->RebuildKtbTree();
+		//leaf->RebuildKtbTree();
 		mSceneGraph->GetRoot()->mChildren.push_back(leaf);
 		
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 2633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 2643)
@@ -126,5 +126,5 @@
 				OptimizeForWindowsApplication="TRUE"
 				AdditionalIncludeDirectories="..\src;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;..\..\..\..\..\..\NonGTP\Boost;..\MultiLevelRayTracing;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;&quot;$(CG_INC_PATH)&quot;;Timer;..\src\sparsehash\src\google\sparsehash;..\src\sparsehash\src\windows;..\src\sparsehash\src\google;..\src\sparsehash\src;..\src\ootl\;..\src\ootl\src\;..\src\ootl\src\cpp;..\src\ootl\src\cpp\include\;..\src\ootl\src\include\ootl;..\src\ootl\src\include\ootl\sandbox;..\src\ootl\src\cpp\include\ootl\mswin;..\src\havran"
-				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;GTP_INTERNAL;USE_QT;USE_HASH_PVS;USE_CG"
+				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;GTP_INTERNAL;USE_QT;USE_VERBOSE_PVS;USE_CG"
 				StringPooling="TRUE"
 				MinimalRebuild="TRUE"
@@ -417,4 +417,7 @@
 			<File
 				RelativePath=".\HashPvs.h">
+			</File>
+			<File
+				RelativePath=".\HavranDynRayCaster.cpp">
 			</File>
 			<File
@@ -1144,4 +1147,10 @@
 			</File>
 			<File
+				RelativePath=".\havran\ktbs.cpp">
+			</File>
+			<File
+				RelativePath=".\havran\ktbs.h">
+			</File>
+			<File
 				RelativePath=".\havran\ktbtrav.cpp">
 			</File>
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2638)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2643)
@@ -307,4 +307,12 @@
 	GlRenderer::InitGL();
 
+	//glEnable(GL_FOG);
+	//glFogi(GL_FOG_MODE, GL_EXP);
+	glFogi(GL_FOG_MODE, GL_LINEAR);
+
+//	glFogf(GL_FOG_DENSITY, .2f);
+	glFogf(GL_FOG_START, 50.f);
+	glFogf(GL_FOG_END, 500.f);
+
 	GLfloat light_ambient[] = {0.3, 0.3, 0.3, 1.0};
     GLfloat light_diffuse[] = {0.6, 0.6, 0.6, 1.0};
@@ -440,5 +448,5 @@
 	int indexBufferSize = 0;
 	
-	KdNode::NewMail();
+	KdNode::NewMail2();
 	//Intersectable::NewMail();
 
@@ -506,5 +514,5 @@
 	if (viewcell) 
 	{
-#if 1
+#if 0
 		// copy the pvs so that it can be filtered ...
 		if (PvsChanged(viewcell)) 
@@ -518,10 +526,9 @@
 				//mMutex.lock();
 				// mSpatialFilter size is in range 0.001 - 0.1
-			  mViewCellsManager->ApplyFilter2(viewcell,
-					                            mUseFilter,
-					                            100.0f * mSpatialFilterSize,
-					                            //pvs,
-												mPvsCache.mPvs,		
-					                            &mPvsCache.filteredBoxes);
+				mViewCellsManager->ApplyFilter2(viewcell,
+					mUseFilter,
+					100.0f * mSpatialFilterSize,
+					mPvsCache.mPvs,		
+					&mPvsCache.filteredBoxes);
 				//mPvsCache.mPvs = pvs;
 				//mMutex.unlock();
@@ -535,10 +542,12 @@
 			// update the indices for rendering
 			PreparePvs2(mPvsCache.mPvs);
-			
 			emit PvsUpdated();
+			mCurrentPvsCost = mPvsCache.mPvs.EvalPvsCost();
 		}
 #else
+	
 		PreparePvs2(viewcell->GetPvs());
 		emit PvsUpdated();
+
 #endif
 
@@ -577,4 +586,5 @@
 			mWireFrame = false;
 		}
+		mCurrentPvsCost = viewcell->GetPvs().EvalPvsCost();
 	} 
 	else 
@@ -585,4 +595,6 @@
 		RenderScene();
 	}
+
+	//cout << "vp: " << mViewPoint << " vd: " << mViewDirection << endl;
 }
 
@@ -794,5 +806,5 @@
 		}
 
-		if (1 && mShowRays) 
+		if (mShowRays) 
 		{
 			RenderRays(mViewCellsManager->mVizBuffer.GetRays(), mRayVisualizationMethod, mShowDistribution, 1);
@@ -937,4 +949,5 @@
 	//mRenderVisibilityEstimates = true;
 
+	mComputeGVS = false;
 	mUseRandomColorPerPvsObject = false;
 
@@ -980,5 +993,5 @@
 	mShowRays = false;
 
-	SetSceneCut(0);
+	SetSceneCut(1000);
 	mControlWidget = new QtRendererControlWidget(NULL);
 
@@ -991,4 +1004,5 @@
 	connect(mControlWidget, SIGNAL(SetRandomViewPoint()), this,	SLOT(SetRandomViewPoint()));
 	connect(mControlWidget, SIGNAL(StoreStatistics(void)), this, SLOT(StoreStatistics(void)));
+	connect(mControlWidget, SIGNAL(ComputeGVS(void)), this, SLOT(ComputeGVS(void)));
 	connect(mControlWidget, SIGNAL(LoadObject(void)), this, SLOT(LoadObject(void)));
 
@@ -1270,11 +1284,17 @@
 #if 0
 	QFont font40; font40.setPointSize(30);
-	s.sprintf("PVS: %04d", mPvsSize);
-	renderText(20, 40, s, font40);
+	//s.sprintf("PVS: %04d", mPvsSize);
+	//renderText(20, 40, s, font40);
 #endif
 	
-/*	QFont font40; font40.setPointSize(30);
-	s.sprintf("New triangles: %04d", 100);
-	renderText(200, 40, s, font40);*/
+	//s.sprintf("RAW TRI: %07d", mViewCellsManager->GetPreprocessor()->mGenericStats);
+	//renderText(290, 40, s, font40);
+	//s.sprintf("PVS TRI: %07d", mViewCellsManager->GetPreprocessor()->mGenericStats2);
+	//renderText(290, 70, s, font40);
+
+	s.sprintf("PVS TRI: %07d", (int)mCurrentPvsCost);
+	renderText(290, 70, s, font40);
+	//renderText(290, 70, s, font40);
+
 }
 
@@ -1283,6 +1303,7 @@
 QtGlRendererWidget::SetViewCellGranularity(int number)
 {
-	if (mViewCellsManager) {
-		//  	mViewCellsManager->SetMaxFilterSize(number);
+	if (mViewCellsManager)
+	{
+		//	mViewCellsManager->SetMaxFilterSize(number);
 
 		// $$ tmp off
@@ -1301,4 +1322,5 @@
 	if (mViewCellsManager) 
 		mViewCellsManager->SetMaxFilterSize(number);
+
 	mPvsCache.Reset();
 	updateGL();
@@ -1324,5 +1346,5 @@
 		AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
 		Vector3 p = (1.0f - f) * box.Min() + f * box.Max();
-		mSceneCutPlane.mNormal = Vector3(0,-1,0);
+		mSceneCutPlane.mNormal = Vector3(0, -1, 0);
 		mSceneCutPlane.mD = -DotProd(mSceneCutPlane.mNormal, p);
 
@@ -1996,8 +2018,9 @@
 	QGroupBox *groupBox = CreateVisualizationPanel(hbox);
 	vh->addWidget(groupBox, 0, 0);
+
 #if REMOVE_TEMPORARY
 	QGroupBox *groupBox2 = CreateRenderCostPanel(hbox);
 	vh->addWidget(groupBox2, 0, 0);
-#endif
+	
 	QGroupBox *groupBox3 = CreateRayVisualizationPanel(hbox);
 	vh->addWidget(groupBox3, 0, 0);
@@ -2005,13 +2028,8 @@
 	QGroupBox *groupBox4 = CreateTrafoPanel(hbox);
 	vh->addWidget(groupBox4, 0, 0);
+#endif
 
 	//////////////////////////////////
 
-
-	/*cb = new QRadiobox("Top View", vbox);
-	vl->addWidget(cb);
-	cb->setChecked(false);
-	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
-*/
 
 	//vbox->resize(800,150);
@@ -2168,4 +2186,8 @@
 #endif
 
+	button = new QPushButton("Compute GVS", vbox);
+	vbox->layout()->addWidget(button);
+	connect(button, SIGNAL(clicked()), SIGNAL(ComputeGVS()));
+
 #if DYNAMIC_OBJECTS_HACK
 
@@ -2202,6 +2224,6 @@
 	
 	//connect(button, SIGNAL(clicked(void)), SLOT(StoreStatistics(void)));
-	//mHidingCost = 0.1f;
-//////////////////////////////////////////
+	
+	//////////////////////////////////////////
 
 	label = new QLabel("Spatial Filter size");
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2636)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2643)
@@ -99,6 +99,5 @@
 
   void FocusNextPvsErrorFrame();
-  void UpdatePvsErrorItem(int i,
-						  GlRendererBuffer::PvsErrorEntry &);
+  void UpdatePvsErrorItem(int i, GlRendererBuffer::PvsErrorEntry &);
 
   
@@ -113,4 +112,5 @@
   void SetRandomViewPoint();
   void StoreStatistics();
+  void ComputeGVS();
   void LoadObject();
   void UpdateAllPvs();
@@ -231,4 +231,6 @@
 	ViewCellInfoContainer mCompareInfo;
 
+	int mCurrentPvsCost;
+
 	vector<RgbColor> mColors;
 
@@ -316,8 +318,14 @@
 	void SetRandomViewPoint();
 	void StoreStatistics();
+	
 	void LoadObject();
 
 	void SetRenderErrors(bool b) {
 		mRenderErrors = b;
+		updateGL();
+	}
+
+	void ComputeGVS() {
+		mComputeGVS = true;
 		updateGL();
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtInterface.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtInterface.vcproj	(revision 2633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtInterface.vcproj	(revision 2643)
@@ -165,6 +165,6 @@
 			<Tool
 				Name="VCCLCompilerTool"
-				AdditionalIncludeDirectories="..\..\src;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\..\..\..\..\..\..\NonGTP\Zlib\include;&quot;$(CG_INC_PATH)&quot;;..\..\src\sparsehash\src;..\..\src\sparsehash\src\google;..\..\src\sparsehash\src\windows;..\..\src\sparsehash\src\google\sparsehash"
-				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;QTGLRENDERER_EXPORTS;USE_HASH_PVS;USE_QT;USE_CG;GTP_INTERNAL"
+				AdditionalIncludeDirectories="..\..\src;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\..\..\..\..\..\..\NonGTP\Zlib\include;&quot;$(CG_INC_PATH)&quot;;..\..\src\sparsehash\src;..\..\src\sparsehash\src\google;..\..\src\sparsehash\src\windows;..\..\src\sparsehash\src\google\sparsehash;..\src\havran"
+				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;QTGLRENDERER_EXPORTS;USE_VERBOSE_PVS;USE_QT;USE_CG;GTP_INTERNAL"
 				RuntimeLibrary="2"
 				UsePrecompiledHeader="0"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2636)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2643)
@@ -274,4 +274,5 @@
 
   int mGenericStats;
+	int mGenericStats2;
 
   void ScheduleUpdateDynamicObjects() {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GtpVisibility05.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GtpVisibility05.vcproj	(revision 2568)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GtpVisibility05.vcproj	(revision 2643)
@@ -115,5 +115,5 @@
 			<Tool
 				Name="VCCLCompilerTool"
-				AdditionalIncludeDirectories="..\include;..\src;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;..\..\..\..\..\..\NonGTP\Boost;Timer"
+				AdditionalIncludeDirectories="..\includ;..\src;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;..\..\..\..\..\..\NonGTP\Boost;Timer;..\src\havran"
 				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;NO_QT;USE_VERBOSE_PVS"
 				RuntimeLibrary="2"
@@ -232,4 +232,12 @@
 			</File>
 			<File
+				RelativePath=".\HavranDynRayCaster.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\HavranRayCaster.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\HierarchyManager.cpp"
 				>
@@ -348,4 +356,8 @@
 			</File>
 			<File
+				RelativePath=".\RandomViewCellsHandler.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\Ray.cpp"
 				>
@@ -409,4 +421,8 @@
 			<File
 				RelativePath=".\Tetrahedron3.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\timer.cpp"
 				>
 			</File>
@@ -526,4 +542,8 @@
 			</File>
 			<File
+				RelativePath=".\havran\configh.h"
+				>
+			</File>
+			<File
 				RelativePath=".\Containers.h"
 				>
@@ -546,4 +566,12 @@
 			</File>
 			<File
+				RelativePath=".\FilterBasedDistribution.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\FilterBasedDistribution.h"
+				>
+			</File>
+			<File
 				RelativePath=".\FlexibleHeap.h"
 				>
@@ -586,4 +614,8 @@
 			</File>
 			<File
+				RelativePath=".\HavranRayCaster.h"
+				>
+			</File>
+			<File
 				RelativePath=".\HierarchyManager.h"
 				>
@@ -722,4 +754,8 @@
 			</File>
 			<File
+				RelativePath=".\RandomViewCellsHandler.h"
+				>
+			</File>
+			<File
 				RelativePath=".\Ray.h"
 				>
@@ -734,4 +770,8 @@
 			</File>
 			<File
+				RelativePath=".\havran\raypack.h"
+				>
+			</File>
+			<File
 				RelativePath=".\Rectangle3.h"
 				>
@@ -799,4 +839,8 @@
 			<File
 				RelativePath=".\Tetrahedron3.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\timer.h"
 				>
 			</File>
@@ -888,4 +932,96 @@
 			>
 		</Filter>
+		<Filter
+			Name="havran"
+			>
+			<File
+				RelativePath=".\havran\allocgo2.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\allocgo2.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktb.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktb.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktb8b.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktb8b.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbai.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbai.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktball.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktball.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbconf.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbf2trv.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbftrav.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbs.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbs.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbtrav.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\ktbtrav.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\sbbox.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\sbbox.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\subdivm.h"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\testrt.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\havran\testrt.h"
+				>
+			</File>
+		</Filter>
 	</Files>
 	<Globals>
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2638)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2643)
@@ -85,4 +85,5 @@
 //mUseVbos(true),
 mUseVbos(false),
+mComputeGVS(false),
 mCurrentFrame(-1)
 {
@@ -101,6 +102,21 @@
 	  //	mViewDirection = Vector3(0.433884, 0, -0.900969);
 	
+	//mViewPoint = Vector3(1213.85, 176.988, -437.364);
+	//mViewDirection = Vector3(0.433884, 0, -0.900969);
+
+	mViewPoint = Vector3(32.8596, 9.86079, -1023.79);
+	mViewDirection = Vector3(-0.92196, 0, 0.387286);
+
 	//mViewPoint = Vector3(1099.9,183.0,-387);
 	//mViewDirection = Vector3(-0.6,0,-0.8);
+
+	// inside
+	mViewPoint = Vector3(14.1254, 10.9818, -1032.75);
+	mViewDirection = Vector3(-0.604798, 0, 0.796379);
+
+	// outside
+	mViewPoint = Vector3(35.092, 17.7078, -857.966);
+	mViewDirection = Vector3(-0.411287, 0, -0.911506);
+
 #endif
 	mFrame = 0;
@@ -1721,5 +1737,5 @@
 
 		// check if already rendered
-		if (!obj->Mailed())
+		if (!obj->Mailed2())
 			bufferSize += 3;
 		//else cout << obj->mMailbox << " " << obj->sMailId << " ";
@@ -1736,8 +1752,8 @@
 
 		// check if already rendered
-		if (obj->Mailed())
+		if (obj->Mailed2())
 			continue;
 
-		obj->Mail();
+		obj->Mail2();
 
 		Triangle3 tri = obj->GetItem();
@@ -1789,5 +1805,5 @@
 	int indexBufferSize = 0;
 	
-	KdNode::NewMail();
+	KdNode::NewMail2();
 		
 	ObjectPvsIterator it = pvs.GetIterator();
@@ -1813,8 +1829,8 @@
 void GlRenderer::_UpdatePvsIndices(KdNode *node, int &indexBufferSize)
 {
-	if (node->Mailed())
+	if (node->Mailed2())
 		return;
 
-	node->Mail();
+	node->Mail2();
 
 	// if (mObjects.size() * 3 < indexBufferSize) cerr << "problem: " << mObjects.size() * 3 << " < " << indexBufferSize << endl;
@@ -1851,6 +1867,4 @@
 		leaf->mIndexBufferSize = indexBufferSize - leaf->mIndexBufferStart;
 	}
-
-	//cout << "id: " << indexBufferSize << endl;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 2588)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 2643)
@@ -49,6 +49,4 @@
 		       myless<vector<ViewCell *>::value_type> > TraversalQueue;
 
-int Intersectable::sMailId = 10000;//2147483647;
-int Intersectable::sReservedMailboxes = 1;
 
 float MergeCandidate::sRenderCostWeight = 0;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BoundingBoxConverter.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BoundingBoxConverter.h	(revision 2111)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BoundingBoxConverter.h	(revision 2643)
@@ -1,4 +1,6 @@
 #ifndef _BoundingBoxConverter_H__
 #define _BoundingBoxConverter_H__
+
+
 #include <vector>
 #include "AxisAlignedBox3.h"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Camera.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Camera.h	(revision 2629)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Camera.h	(revision 2643)
@@ -4,5 +4,5 @@
 #include "Vector3.h"
 #include "AxisAlignedBox3.h"
-#include "ktbconf.h"
+
 
 namespace GtpVisibilityPreprocessor {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2638)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2643)
@@ -23,4 +23,6 @@
 int KdNode::sMailId = 1;
 int KdNode::sReservedMailboxes = 1;
+int KdNode::sMailId2 = 1;
+int KdNode::sReservedMailboxes2 = 1;
 
 
@@ -32,5 +34,5 @@
 
 KdNode::KdNode(KdInterior *parent):
-mParent(parent), mMailbox(0), mIntersectable(NULL)
+mParent(parent), mMailbox(0), mIntersectable(NULL), mMailbox2(0)
 {
   if (parent)
@@ -1152,4 +1154,36 @@
 }
 
+
+void KdTree::CollectObjectsWithoutMail(KdNode *n, ObjectContainer &objects)
+{
+	stack<KdNode *> nodeStack;
+
+	nodeStack.push(n);
+
+	while (!nodeStack.empty()) 
+	{
+		KdNode *node = nodeStack.top();
+		nodeStack.pop();
+
+		if (node->IsLeaf()) 
+		{
+			KdLeaf *leaf = (KdLeaf *)node;
+			for (int j=0; j < leaf->mObjects.size(); j++) 
+			{
+				Intersectable *object = leaf->mObjects[j];
+				objects.push_back(object);
+			}
+		}
+		else 
+		{
+			KdInterior *interior = (KdInterior *)node;
+
+			nodeStack.push(interior->mFront);
+			nodeStack.push(interior->mBack);
+		}
+	}
+}
+
+
 void
 KdTree::CollectObjects(KdNode *n, ObjectContainer &objects)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2618)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2643)
@@ -102,4 +102,8 @@
   int mMailbox;
 
+  static int sMailId2;
+  static int sReservedMailboxes2;
+  int mMailbox2;
+
   KdIntersectable *mIntersectable;
   
@@ -114,4 +118,17 @@
   void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
   bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
+
+
+  static void NewMail2(const int reserve = 1) {
+    sMailId2 += sReservedMailboxes;
+    sReservedMailboxes2 = reserve;
+  }
+
+  void Mail2() { mMailbox2 = sMailId2; }
+  bool Mailed2() const { return mMailbox2 == sMailId2; }
+ 
+  void Mail2(const int mailbox) { mMailbox2 = sMailId2 + mailbox; }
+  bool Mailed2(const int mailbox) const { return mMailbox2 == sMailId2 + mailbox; }
+
 
   virtual ~KdNode(){};
@@ -372,8 +389,8 @@
   void
   CollectObjects(KdNode *n, ObjectContainer &objects);
-
-  void
-  CollectObjects(const AxisAlignedBox3 &box,
-		 ObjectContainer &objects);
+void
+CollectObjectsWithoutMail(KdNode *n, ObjectContainer &objects);
+  void
+  CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
 
   void
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2634)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2643)
@@ -28,5 +28,5 @@
 #include "ViewCellsManager.h"
 
-#include "testrt.h"
+//#include "testrt.h"
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 2615)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 2643)
@@ -84,4 +84,28 @@
 	int mMailbox;
 
+	static void NewMail2(const int reserve = 1) 
+	{
+		sMailId2 += sReservedMailboxes2;
+		sReservedMailboxes2 = reserve;
+	}
+
+	////////////
+	void Mail2() { mMailbox2 = sMailId2; }
+	bool Mailed2() const { return mMailbox2 == sMailId2; }
+
+	void Mail2(const int mailbox) { mMailbox2 = sMailId2 + mailbox; }
+	bool Mailed2(const int mailbox) const { return mMailbox2 == sMailId2 + mailbox; }
+
+	int IncMail2() { return ++ mMailbox2 - sMailId2; }
+
+	// last mail id -> warning not thread safe!
+	// both mailId and mailbox should be unique for each thread!!!
+	static int sMailId2;
+	static int sReservedMailboxes2;
+
+	/// Mailbox used for traversals
+	int mMailbox2;
+
+	////////////
 
 	////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2625)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2643)
@@ -63,10 +63,10 @@
 struct PvsCache 
 {
-
 	PvsCache():mViewCell(NULL), mUnfilteredPvsSize(0) {}
 
 	void Reset()
 	{ 
-		mViewCell = NULL; mPvs.Clear(); 
+		mViewCell = NULL; 
+		mPvs.Clear(); 
 		filteredBoxes.clear(); 
 		mUnfilteredPvsSize = 0;
@@ -229,4 +229,5 @@
 
 	vector<PvsErrorEntry> mPvsErrorBuffer;
+	bool mComputeGVS;
 
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.h	(revision 2629)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HavranRayCaster.h	(revision 2643)
@@ -1,4 +1,6 @@
 #ifndef _HavranRayCaster_H__
 #define _HavranRayCaster_H__
+
+#ifdef USE_HAVRAN_RAYCASTER
 
 #include "RayCaster.h"
@@ -208,3 +210,5 @@
 } // namespace
 
+#endif
+
 #endif // _HavranRayCaster_H__
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
-}
-
-}
+}
+
+}
