Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreVisibilityOctreeSceneManager.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreVisibilityOctreeSceneManager.h	(revision 870)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreVisibilityOctreeSceneManager.h	(revision 880)
@@ -196,14 +196,15 @@
 	GtpVisibilityPreprocessor::ViewCellsManager *mViewCellsManager;
 
-	/** Used to assign Ogre meshes to view cell entries.
-	*/
+	/// Used to assign Ogre meshes to view cell entries.
 	GtpVisibilityPreprocessor::ObjectContainer mObjects;
 
-	GtpVisibilityPreprocessor::ViewCell *mOldViewCell;
+	GtpVisibilityPreprocessor::ViewCell *mElementaryViewCell;
 	GtpVisibilityPreprocessor::ViewCell *mCurrentViewCell;
 
-	/** If view cells are used.
-	*/
+	/// If view cells are used.
 	bool mUseViewCells;
+
+	/// if the view cells are filtered
+	bool mUseVisibilityFilter;
 };
 
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOctreeHierarchyInterface.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOctreeHierarchyInterface.cpp	(revision 868)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOctreeHierarchyInterface.cpp	(revision 880)
@@ -144,5 +144,5 @@
 	{
 		mSavedNode = node;
-	    //dynamic_cast<Octree *>(node)->_getCullBounds(&mBox);
+	    //static_cast<Octree *>(node)->_getCullBounds(&mBox);
 		mBox = static_cast<Octree *>(node)->_getWorldAABB();
 	}
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreVisibilityOctreeSceneManager.cpp	(revision 870)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreVisibilityOctreeSceneManager.cpp	(revision 880)
@@ -45,7 +45,8 @@
 mIsHierarchicalCulling(false),
 mViewCellsLoaded(false),
+mUseViewCells(false),
+mUseVisibilityFilter(false),
 mCurrentViewCell(NULL),
-mOldViewCell(NULL),
-mUseViewCells(false)
+mElementaryViewCell(NULL)
 {
 	mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
@@ -91,4 +92,5 @@
 	OGRE_DELETE(mHierarchyInterface);
 	CLEAR_CONTAINER(mObjects);
+	OGRE_DELETE(mCurrentViewCell);
 }
 //-----------------------------------------------------------------------
@@ -514,21 +516,24 @@
 	if (key == "UseViewCells")
 	{ 
-		if (mViewCellsLoaded)
-		{
-			mUseViewCells = *static_cast<const bool *>(val);
-			
-			if (mUseViewCells)
-			{
-				SetObjectsVisible(false);
-				mCurrentViewCell = mOldViewCell = NULL;
-			}
-			else
-			{
-				SetObjectsVisible(true);
-			}
-		}
-
-		return true;
-	}
+		mUseViewCells = *static_cast<const bool *>(val);
+
+		// reset view cell
+		OGRE_DELETE(mCurrentViewCell);
+		if (mUseViewCells)
+			mCurrentViewCell =  mViewCellsManager->GenerateViewCell();
+		mElementaryViewCell = NULL;
+		// if using view cells, all objects are set to false initially
+		SetObjectsVisible(!mUseViewCells);
+		
+		return true;
+	}
+	if (key == "UseVisibilityFilter")
+	{
+		mUseVisibilityFilter = *static_cast<const bool *>(val);
+		// set null =>recomputation of the pvs
+        mElementaryViewCell = NULL;
+		return true;
+	}
+
 	return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
 		setOption(key, val) || OctreeSceneManager::setOption(key, val);
@@ -864,11 +869,12 @@
 			// Also render any objects which have receive shadows disabled
 			OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPassesNoShadow(), true);
-		
-			/*std::stringstream d; 
+#if 0		
+			std::stringstream d; 
 			d << " solid size: " << (int)pPriorityGrp->_getSolidPasses().size()
 				<< " solid no shadow size: " << (int)pPriorityGrp->_getSolidPassesNoShadow().size()
 				<< "difspec size: " << (int)pPriorityGrp->_getSolidPassesDiffuseSpecular().size()
 				<< " decal size: " << (int)pPriorityGrp->_getSolidPassesDecal().size();
-			LogManager::getSingleton().logMessage(d.str());*/
+			LogManager::getSingleton().logMessage(d.str());
+#endif
 		}
 	}
@@ -1027,7 +1033,12 @@
 void VisibilityOctreeSceneManager::applyViewCellPvs(GtpVisibilityPreprocessor::ViewCell *vc, 
 													const bool load)
-{	GtpVisibilityPreprocessor::Debug << "here9 " << endl;
-	// NOTE: should not happen, rather apply view cell representing unbounded space then
-	if (!vc) return;
+{	// NOTE: should not happen, rather apply view cell representing unbounded space then
+	if (!vc) 
+	{	
+		// set everything visible for savety
+		SetObjectsVisible(true);
+
+		return;
+	}
 
 	GtpVisibilityPreprocessor::ObjectPvsMap::const_iterator oit,
@@ -1041,5 +1052,5 @@
 		OgreMeshInstance *omi = dynamic_cast<OgreMeshInstance*>((*oit).first);
 		omi->GetMesh()->setVisible(load);
-		GtpVisibilityPreprocessor::Debug << "here45 " << omi->GetId() << endl;
+		//GtpVisibilityPreprocessor::Debug << "here45 " << omi->GetId() << endl;
 	}
 }
@@ -1048,7 +1059,7 @@
 {
 	// for OGRE 1.2
-#ifdef OGRE12
-	MovableObjectIterator mit = getMovableObjectIterator("Entity");
-#else
+//#ifdef OGRE12
+	//MovableObjectIterator mit = getMovableObjectIterator("Entity");
+#if 0
 	EntityIterator eit = getEntityIterator();
 
@@ -1060,4 +1071,13 @@
 	}
 
+#else
+	GtpVisibilityPreprocessor::ObjectContainer::iterator it, it_end = mObjects.end();
+
+	for (it = mObjects.begin(); it != it_end; ++ it)
+	{
+		OgreMeshInstance *omi = static_cast<OgreMeshInstance *>(*it);
+		Entity *ent = omi->GetMesh();
+		ent->setVisible(visible);
+	}
 #endif
 }
@@ -1089,34 +1109,48 @@
 void VisibilityOctreeSceneManager::updatePvs(Camera *cam)
 {
-	Ogre::LogManager::getSingleton().logMessage("here196");
 	if (mViewCellsLoaded && mUseViewCells)
 	{
-		GtpVisibilityPreprocessor::ViewCell *vc = 
-			mViewCellsManager->GetViewCell(OgreTypeConverter::ConvertFromOgre(cam->getDerivedPosition()));
-
-		std::stringstream d; d << "pos " << cam->getDerivedPosition() << "vc: " << vc;
-		Ogre::LogManager::getSingleton().logMessage(d.str());
-
-		// view cell changed => unload old objects and load new objects
-		if (vc != mCurrentViewCell)
-		{
-			//-- unload old pvs
-			if (mOldViewCell)
-			{
-				const bool load = false;
-
-				applyViewCellPvs(mOldViewCell, load);
-				Ogre::LogManager::getSingleton().logMessage("here16");
-			}
-
-			mOldViewCell = mCurrentViewCell;
-
-			//-- load new pvs
-			mCurrentViewCell = vc;
-			const bool load = true;
-
-			applyViewCellPvs(mCurrentViewCell, load);
-			Ogre::LogManager::getSingleton().logMessage("here106");
-		}
+		const GtpVisibilityPreprocessor::Vector3 viewPoint = 
+			OgreTypeConverter::ConvertFromOgre(cam->getDerivedPosition());
+
+		GtpVisibilityPreprocessor::ViewCell *newElementary = 
+			mViewCellsManager->GetViewCell(viewPoint);
+
+		// elementary view cell did not change => apply same pvs
+		if (mElementaryViewCell == newElementary)
+			return;
+
+		mElementaryViewCell = newElementary;
+
+		//-- unload old pvs
+		applyViewCellPvs(mCurrentViewCell, false);
+
+
+		//-- the new view cell
+		
+		GtpVisibilityPreprocessor::ViewCell *viewCell;
+
+		
+		if (mUseVisibilityFilter)
+		{
+			//-- compute new filtered cell
+			GtpVisibilityPreprocessor::PrVs prvs;
+			mViewCellsManager->GetPrVS(viewPoint, prvs);
+			viewCell = prvs.mViewCell;
+		}
+		else
+		{
+			viewCell = newElementary;
+		}
+			
+		//-- load new pvs
+		applyViewCellPvs(viewCell, true);
+
+		// store pvs
+		mCurrentViewCell->SetPvs(viewCell->GetPvs());
+
+		// delete merge tree of filtered view cell
+		if (mUseVisibilityFilter)
+			mViewCellsManager->DeleteLocalMergeTree(viewCell);
 	}
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 870)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 880)
@@ -174,4 +174,10 @@
 {
 	return mPvs;
+}
+
+
+void ViewCell::SetPvs(const ObjectPvs &pvs)
+{
+	mPvs = pvs;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 879)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 880)
@@ -43,5 +43,5 @@
   ViewCell *mViewCell;
 
-  // input parameter is the render budget for the PrVS
+  // input parameter is the render budget for the PrVs
   float mRenderBudget;
 
@@ -467,4 +467,6 @@
 										ViewCellContainer &viewCells) const;
 
+	/** Tests the visibility filter functionality.
+	*/
 	virtual void TestFilter(const ObjectContainer &objects) {};
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 860)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 880)
@@ -117,4 +117,6 @@
 
 	ObjectPvs &GetPvs();
+
+	void SetPvs(const ObjectPvs &pvs);
 
 	/** Type of view cells.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 870)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 880)
@@ -300,16 +300,15 @@
 				  lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);
 				
-				Intersectable *obj = *oit;
-				
-				if (obj->GetId() == objId)
+							
+				if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
 				{
-				  // $$JB we should store a float a per object which corresponds
-				  // to sumof pdfs, i.e. its relative visibility
-				  // temporarily set to 1.0f
-					viewCell->GetPvs().AddSample(obj, 1.0f);				
+					// $$JB we should store a float a per object which corresponds
+					// to sumof pdfs, i.e. its relative visibility
+					// temporarily set to 1.0f
+					viewCell->GetPvs().AddSample(*oit, 1.0f);				
 				}
 				else
 				{
-					Debug << "error: object does not exist" << endl;
+					Debug << "error: object with id " << objId << " does not exist" << endl;
 				}
 			}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 879)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 880)
@@ -4959,5 +4959,5 @@
 	Vector3 bsize = mViewSpaceBox.Size();
 	const Vector3 viewPoint(mViewSpaceBox.Center());
-	float w = Magnitude(mViewSpaceBox.Size())*mFilterWidth;
+	float w = Magnitude(mViewSpaceBox.Size()) * mFilterWidth;
 	const Vector3 width = Vector3(w);
 	
@@ -4980,5 +4980,5 @@
 
 		exporter->SetForcedMaterial(RgbColor(0,1,0));
-		ExportViewCellGeometry(exporter,  GetViewCell(viewPoint));
+		ExportViewCellGeometry(exporter, GetViewCell(viewPoint));
 
 		//exporter->ResetForcedMaterial();
