Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 850)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 859)
@@ -25,4 +25,5 @@
   // Constructors.
   AxisAlignedBox3() { }
+  
   AxisAlignedBox3(const Vector3 &nMin, const Vector3 &nMax)
   {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 811)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 859)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: pá 14. IV 14:54:41 2006
+# Generated by qmake (2.00a) (Qt 4.1.2) on: út 25. IV 12:50:38 2006
 # Project:  preprocessor.pro
 # Template: app
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 850)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 859)
@@ -422,4 +422,13 @@
 	float EvalRenderCost(Intersectable *obj) const;
 
+    void
+	ApplyFilter(KdTree *kdTree,
+				const float viewSpaceFilterSize,
+				const float spatialFilterSize
+				);
+
+  AxisAlignedBox3
+  GetViewCellBox(ViewCell *vc);
+
 	/** Exports bounding boxes of objects to file.
 	*/
@@ -431,5 +440,13 @@
 
 protected:
-
+    void
+	ApplyFilter(ViewCell *viewCell,
+				KdTree *kdTree,
+				const float viewSpaceFilterSize,
+				const float spatialFilterSize,
+				ObjectPvs &pvs
+				);
+
+  
 	/** Returns the bounding box of filter width.
 	*/
@@ -667,4 +684,5 @@
 	void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
 
+
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.h	(revision 752)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.h	(revision 859)
@@ -100,5 +100,6 @@
     mFaces.push_back(face);
   }
-  
+
+  void ComputeBoundingBox();
   void Preprocess();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 850)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 859)
@@ -1036,4 +1036,8 @@
 }
 
+  /**
+	 Input scene filename. Currently simplified X3D (.x3d), Unigraphics (.dat),
+	 and UNC (.ply) formats are supported.
+  */
 
 Environment::Environment()
@@ -1062,5 +1066,4 @@
   RegisterOption("Limits.infinity", optFloat, NULL, "1e6");
 
-
   RegisterOption("Scene.filename",
 				 optString,
@@ -1068,4 +1071,5 @@
 				 "atlanta2.x3d");
 
+  
   RegisterOption("Unigraphics.meshGrouping",
 				 optInt,
@@ -1078,10 +1082,10 @@
 				 "kd_term_min_cost=",
 				 "10");
-
+  
   RegisterOption("KdTree.Termination.maxNodes",
 				 optInt,
 				 "kd_term_max_nodes=",
 				 "200000");
-
+  
   RegisterOption("KdTree.Termination.maxDepth",
 				 optInt,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 837)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 859)
@@ -714,4 +714,36 @@
 
 void
+KdTree::CollectObjects(const AxisAlignedBox3 &box,
+					   ObjectContainer &objects)
+{
+  stack<KdNode *> nodeStack;
+
+  nodeStack.push(mRoot);
+
+  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];
+		if (!object->Mailed()) {
+		  object->Mail();
+		  objects.push_back(object);
+		}
+      }
+    } else {
+      KdInterior *interior = (KdInterior *)node;
+
+	  if ( box.Max()[interior->mAxis] > interior->mPosition )
+		nodeStack.push(interior->mFront);
+ 
+	  if (box.Min()[interior->mAxis] < interior->mPosition)
+		nodeStack.push(interior->mBack);
+    }
+  }
+}
+
+void
 KdTree::CollectObjects(KdNode *n, ObjectContainer &objects)
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 811)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 859)
@@ -798,4 +798,10 @@
   Debug<<"Done.\n";
 
+
+  cout<<"Applying visibility filter...";
+  mViewCellsManager->ApplyFilter(mKdTree,
+								 0.05f,
+								 0.05f);
+  cout<<"done.";
   
   return true;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 752)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 859)
@@ -307,5 +307,9 @@
   void
   CollectObjects(KdNode *n, ObjectContainer &objects);
-	
+
+  void
+  CollectObjects(const AxisAlignedBox3 &box,
+				 ObjectContainer &objects);
+
   void
   CollectLeaves(vector<KdLeaf *> &leaves);
@@ -342,12 +346,12 @@
   KdTree::GetRandomLeaf(const Plane3 &halfspace);
 
-	KdNode *
-	GetRandomLeaf(const bool onlyUnmailed = false);
-
+  KdNode *
+  GetRandomLeaf(const bool onlyUnmailed = false);
+  
   int
   FindNeighbors(KdNode *n,
-		vector<KdNode *> &neighbors,
-		bool onlyUnmailed
-		);
+				vector<KdNode *> &neighbors,
+				bool onlyUnmailed
+				);
 
   int
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 811)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 859)
@@ -12,7 +12,7 @@
 #filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-plane.x3d;../data/vienna/vienna-roads.x3d
 # filename ../data/vienna/viewcells-25-sel.x3d
-#filename ../data/atlanta/atlanta2.x3d
+filename ../data/atlanta/atlanta2.x3d
 #filename ../data/soda/soda.dat
-filename ../data/soda/soda5.dat
+#filename ../data/soda/soda5.dat
 #filename ../data/PowerPlant/ppsection1/part_a/g0.ply
 #filename ../data/PowerPlant/ppsection1/part_b/g0.ply
@@ -28,10 +28,10 @@
 	# stored sample rays
 	samplesFilename rays.out
-	useGlRenderer false
-	useGlDebugger true
+	useGlRenderer true
+	useGlDebugger false
 #	type sampling
 #	type vss
-#	type rss
-	type render
+	type rss
+#	type render
 	detectEmptyViewSpace false
 	pvsRenderErrorSamples 0
@@ -51,5 +51,5 @@
 	vssSamples 1000000
 	vssSamplesPerPass 200000
-	useImportanceSampling true
+	useImportanceSampling false
 	loadInitialSamples  false
 	storeInitialSamples false
@@ -89,9 +89,9 @@
 
 RssPreprocessor {
-	samplesPerPass 100000
-	initialSamples 500000
-	vssSamples 1000000000
-	vssSamplesPerPass 100000
-	useImportanceSampling true
+	samplesPerPass 1000000
+	initialSamples 1000000
+	vssSamples 1000000
+	vssSamplesPerPass 1000000
+	useImportanceSampling false
 
 	directionalSampling false
@@ -101,5 +101,5 @@
 		pvs false
 		rssTree false
-		rays true
+		rays false
 		numRays 20000
 	}
@@ -170,5 +170,5 @@
 MeshKdTree {
 	Termination {
-		minCost 8
+		minCost 40
 		maxDepth 18
 		maxCostRatio 0.9
@@ -201,5 +201,5 @@
 
 	#number of active view cells
-	active 15000
+	active 5000
 	maxStaticMemory	40
 
@@ -214,5 +214,5 @@
 	
 	height 5.0
-	maxViewCells 4000
+	maxViewCells 5000
 
 	#percentage of total visible objects where pvs is considered invalid
@@ -248,5 +248,5 @@
 		#colorCode MergedTreeDiff
 		colorCode Random
-		exportRays true
+		exportRays false
 		exportGeometry false
 		exportMergedViewCells false
@@ -273,6 +273,6 @@
 #	filename ../data/soda/soda5-viewcells.xml
 #	filename ../scripts/viewcells_atlanta.xml
-	filename ../data/soda/soda5-viewcells2.xml
-#	filename ../data/atlanta/viewcells_atlanta3.xml
+#	filename ../data/soda/soda5-viewcells2.xml
+	filename ../data/atlanta/viewcells_atlanta3.xml
 #	filename ../data/vienna/viewcells_vienna.xml
 #	filename ../data/PowerPlant/power_plant_viewcells_all3.xml
@@ -376,5 +376,5 @@
 		minGlobalCostRatio      0.0000001
 #		minGlobalCostRatio      0.0001
-		maxViewCells		3000
+		maxViewCells		50000
 	
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 850)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 859)
@@ -207,4 +207,29 @@
 }
 
+AxisAlignedBox3
+ViewCellsManager::GetViewCellBox(ViewCell *vc)
+{
+  Mesh *m = vc->GetMesh();
+  
+  if (m) {
+	m->ComputeBoundingBox();
+	return m->mBox;
+  }
+
+  AxisAlignedBox3 box;
+  
+  box.Initialize();
+  
+  if (!vc->IsLeaf()) {
+	ViewCellInterior *vci = (ViewCellInterior *) vc;
+	
+	ViewCellContainer::iterator it = vci->mChildren.begin();
+	for (; it != vci->mChildren.end(); ++it) {
+	  box.Include(GetViewCellBox(*it));
+	}
+  }
+  
+  return box;
+}
 
 void ViewCellsManager::CollectEmptyViewCells()
@@ -4807,4 +4832,121 @@
 
 
+void
+ViewCellsManager::ApplyFilter(ViewCell *viewCell,
+							  KdTree *kdTree,
+							  const float viewSpaceFilterSize,
+							  const float spatialFilterSize,
+							  ObjectPvs &pvs
+							  )
+{
+  // extend the pvs of the viewcell by pvs of its neighbors
+  // and apply spatial filter by including all neighbors of the objects
+  // in the pvs
+
+  // get all viewcells intersecting the viewSpaceFilterBox
+  // and compute the pvs union
+  
+  //Vector3 center = viewCell->GetBox().Center();
+  //  Vector3 center = m->mBox.Center();
+  
+  //  AxisAlignedBox3 box(center - Vector3(viewSpaceFilterSize/2),
+  //					  center + Vector3(viewSpaceFilterSize/2));
+  
+  AxisAlignedBox3 box = GetViewCellBox(viewCell);
+  box.Enlarge(Vector3(viewSpaceFilterSize/2));
+  
+  ViewCellContainer viewCells;
+  ComputeBoxIntersections(box, viewCells);
+
+  //  cout<<"box="<<box<<endl;
+  ViewCellContainer::const_iterator it=viewCells.begin(), it_end = viewCells.end();
+  
+  int i;
+  for (i=0; it != it_end; ++it, i++) {
+	//	cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
+	pvs.Merge((*it)->GetPvs());
+  }
+  
+  // now compute a new Pvs by including also objects intersecting the extended boxes of
+  // visible objects
+
+  Intersectable::NewMail();
+  
+  std::map<Intersectable *,
+	PvsData<Intersectable *>,
+	LtSample<Intersectable *> >::const_iterator oi;
+  
+  for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++oi) {
+	Intersectable *object = (*oi).first;
+	object->Mail();
+  }
+
+  ObjectPvs nPvs;
+  // now go through the pvs again
+  for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++oi) {
+	Intersectable *object = (*oi).first;
+
+	//	Vector3 center = object->GetBox().Center();
+	//	AxisAlignedBox3 box(center - Vector3(spatialFilterSize/2),
+	//						center + Vector3(spatialFilterSize/2));
+
+	AxisAlignedBox3 box = object->GetBox();
+	box.Enlarge(Vector3(spatialFilterSize/2));
+
+	ObjectContainer objects;
+
+	// $$ warning collect objects takes only unmailed ones!
+	kdTree->CollectObjects(box,
+						   objects);
+	//	cout<<"collected objects="<<objects.size()<<endl;
+	ObjectContainer::const_iterator noi = objects.begin();
+	for (; noi != objects.end(); ++noi) {
+	  Intersectable *o = *noi;
+	  if (!o->Mailed())
+		// $$ JB warning: pdfs are not correct at this point!
+		nPvs.AddSample(o, Limits::Small);
+	}
+  }
+
+  pvs.Merge(nPvs);
+
+}
+
+
+
+void
+ViewCellsManager::ApplyFilter(KdTree *kdTree,
+							  const float relViewSpaceFilterSize,
+							  const float relSpatialFilterSize
+							  )
+{
+
+  ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+
+  ObjectPvs *newPvs;
+  newPvs = new ObjectPvs[mViewCells.size()];
+
+  float viewSpaceFilterSize = Magnitude(mViewSpaceBox.Size())*relViewSpaceFilterSize;
+  float spatialFilterSize = Magnitude(kdTree->GetBox().Size())*relSpatialFilterSize;
+  
+  int i;
+  for (i=0, it = mViewCells.begin(); it != it_end; ++ it, i++) {
+	ApplyFilter(*it,
+				kdTree,
+				viewSpaceFilterSize,
+				spatialFilterSize,
+				newPvs[i]
+				);
+  }
+
+  // now replace all pvss
+  for (i= 0, it = mViewCells.begin(); it != it_end; ++ it, i++) {
+	ObjectPvs &pvs = (*it)->GetPvs();
+	pvs.Clear();
+	pvs = newPvs[i];
+  }
+  
+}
+
 void VspBspViewCellsManager::TestFilter(const ObjectContainer &objects)
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 811)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 859)
@@ -41,4 +41,6 @@
 //PFNGLGETQUERYUIVARBPROC glGetQueryuivARB;
 #endif
+
+bool useFilter = false;
 
 GlRenderer::GlRenderer(SceneGraph *sceneGraph,
@@ -838,10 +840,14 @@
   
   ViewCell *viewcell;
-  //  viewcell = mViewCellsManager->GetViewCell(mViewPoint);
 
   PrVs prvs;
-  //  mViewCellsManager->SetMaxFilterSize(1);
-  mViewCellsManager->GetPrVS(mViewPoint, prvs);
-  viewcell = prvs.mViewCell;
+  
+  if (!useFilter)
+	viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+  else {
+	//  mViewCellsManager->SetMaxFilterSize(1);
+	mViewCellsManager->GetPrVS(mViewPoint, prvs);
+	viewcell = prvs.mViewCell;
+  }
   
   if (viewcell) {
@@ -869,5 +875,7 @@
 	  mWireFrame = false;
 	}
-	mViewCellsManager->DeleteLocalMergeTree(viewcell);
+	
+	if (useFilter)
+	  mViewCellsManager->DeleteLocalMergeTree(viewcell);
   } else {
 	ObjectContainer::const_iterator oi = mObjects.begin();
@@ -910,10 +918,15 @@
 
   ViewCell *viewcell;
-  //  viewcell = mViewCellsManager->GetViewCell(mViewPoint);
 
   PrVs prvs;
   //  mViewCellsManager->SetMaxFilterSize(1);
-  mViewCellsManager->GetPrVS(mViewPoint, prvs);
-  viewcell = prvs.mViewCell;
+
+
+  if (!useFilter)
+	viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+  else {
+	mViewCellsManager->GetPrVS(mViewPoint, prvs);
+	viewcell = prvs.mViewCell;
+  }
   
   QImage im1, im2;
@@ -1016,7 +1029,7 @@
   glDisable(GL_CLIP_PLANE0);
 
-  if (viewcell)
+  if (useFilter && viewcell)
 	mViewCellsManager->DeleteLocalMergeTree(viewcell);
-
+  
   mRenderError = pErrorPixels;
   return pErrorPixels;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 811)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 859)
@@ -50,9 +50,9 @@
 };
 
-void
-Mesh::Preprocess()
-{
-  Cleanup();
-	
+
+void
+Mesh::ComputeBoundingBox()
+{
+
   mBox.Initialize();
   VertexContainer::const_iterator vi = mVertices.begin();
@@ -61,5 +61,13 @@
   }
 
-  
+}
+
+void
+Mesh::Preprocess()
+{
+ Cleanup();
+   
+   ComputeBoundingBox();
+   
   /** true if it is a watertight convex mesh */
   mIsConvex = false;
