Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 705)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 713)
@@ -119,9 +119,9 @@
 	  pvsReduction,
 	  pvsEnlargement;
-
+	
 	ComputeContinuousPvsDifference(pvs,
 								   pvsReduction,
 								   pvsEnlargement);
-
+	
 	return pvsReduction + pvsEnlargement;
   }
@@ -161,4 +161,6 @@
 	  data->mSumPdf = -aSumPdf;
 	}
+
+#if 0
 	float diff = bSumPdf - aSumPdf;
 	
@@ -168,4 +170,8 @@
 	  pvsReduction += -diff;
 	}
+#else
+	if (!data)
+	  pvsEnlargement += 1.0f;
+#endif
   }
   
@@ -182,4 +188,5 @@
 		bSumPdf = data->mSumPdf;
 	  }
+#if 0
 	  float diff = bSumPdf - aSumPdf;
 	  
@@ -189,4 +196,9 @@
 		pvsReduction += -diff;
 	  }
+
+#else
+	  if (!data) 
+		pvsReduction += 1.0f;
+#endif
 	}
   }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 695)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 713)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.0) on: st 15. III 09:41:58 2006
+# Generated by qmake (2.00a) (Qt 4.1.0) on: po 20. III 17:50:07 2006
 # Project:  preprocessor.pro
 # Template: app
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 710)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 713)
@@ -423,5 +423,13 @@
 	void EvalViewCellPartition(Preprocessor *preprocessor);
 
-
+  void SetMaxFilterSize(const int size) {
+	mMaxFilterSize = size;
+  }
+
+  /** deletes interior nodes from the tree which have negative merge cost set (local merge) */
+  void
+  DeleteLocalMergeTree(ViewCell *vc
+					   ) const;
+  
 protected:
 
@@ -495,4 +503,11 @@
 	ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell, 
 									  const ViewCellContainer &viewCells);
+
+	/** Constructs local view cell merge hierarchy based solely on similarity with the
+		current viewcell
+	*/
+  	ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell, 
+									   const ViewCellContainer &viewCells);
+  
 
 	/** Creates clip plane for visualization.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 677)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 713)
@@ -446,5 +446,6 @@
 	  "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
 	  "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
-	  "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl;
+	  "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
+	  "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
   }
 }
@@ -558,5 +559,5 @@
 	Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
 	// cull viewcells with PVS > median (0.5f)
-	mViewCellsManager->SetValidityPercentage(0, 0.5f); 
+	//	mViewCellsManager->SetValidityPercentage(0, 0.5f); 
 	Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 685)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 713)
@@ -26,7 +26,8 @@
 	type rss
 	detectEmptyViewSpace true
-	pvsRenderErrorSamples 10000
-	quitOnFinish false
-	computeVisibility false
+	pvsRenderErrorSamples 5000
+#	pvsRenderErrorSamples 1000
+	quitOnFinish true
+	computeVisibility true
 }
 
@@ -75,8 +76,8 @@
 
 RssPreprocessor {
-	samplesPerPass 100000
+	samplesPerPass 500000
 	initialSamples 500000
-	vssSamples 5000000
-	vssSamplesPerPass 500000
+	vssSamples 20000000
+	vssSamplesPerPass 1000000
 	useImportanceSampling true
 
@@ -176,6 +177,11 @@
 	}
 
+	Filter {
+		maxSize 20
+		width  200.0
+	}
+
 	#number of active view cells
-	active 1024
+	active 3000
 	maxStaticMemory	40
 
@@ -185,6 +191,6 @@
 	#type kdTree
 	#type vspKdTree
-	type bspTree
-	#type vspBspTree
+	#type bspTree
+	type vspBspTree
 	#type sceneDependent
 	
@@ -235,6 +241,6 @@
 	
 	Evaluation {
-		samplesPerPass 80000
-		samples 1000000
+		samplesPerPass 500
+		samples 1000
 		statsPrefix ../scripts/viewCells
 	}
@@ -248,5 +254,6 @@
 #	filename ../scripts/viewcells_soda5-2.xml
 #	filename ../data/soda/soda5-viewcells.xml
-	filename ../scripts/viewcells_atlanta.xml
+#	filename ../scripts/viewcells_atlanta.xml
+	filename ../data/soda/soda5-viewcells2.xml
 }
 
@@ -348,5 +355,5 @@
 		minGlobalCostRatio      0.0000001
 #		minGlobalCostRatio      0.0001
-		maxViewCells		20000
+		maxViewCells		3000
 	
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 608)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 713)
@@ -30,4 +30,5 @@
   float maxError;
   float sumError;
+  int sumPvsSize;
   int frames;
   int errorFreeFrames;
@@ -40,4 +41,5 @@
 	frames = 0;
 	errorFreeFrames = 0;
+	sumPvsSize = 0;
   }
 
@@ -45,4 +47,5 @@
   float GetAvgError() { return sumError/frames; }
   float GetErrorFreeFrames() { return errorFreeFrames/(float)frames; }
+  float GetAvgPvs() { return sumPvsSize/(float)frames; }
   
 };
@@ -161,5 +164,5 @@
 
   float
-  GetPixelError();
+  GetPixelError(int &pvsSize);
 
   int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const;
@@ -168,5 +171,12 @@
    
   int mPvsStatFrames;
-  vector<float> mPvsErrorBuffer;
+  struct PvsErrorEntry {
+	PvsErrorEntry() {}
+	float mError;
+	int mPvsSize;
+  };
+  
+  vector<PvsErrorEntry> mPvsErrorBuffer;
+
 private:
 	
@@ -190,4 +200,5 @@
   SetSceneCut(int);
   SetTopDistance(int);
+  SetVisibilityFilterSize(int);
 
   SetShowViewCells(bool);
@@ -195,4 +206,5 @@
   SetCutViewCells(bool);
   SetCutScene(bool);
+
 
 };
@@ -256,10 +268,13 @@
  SetViewCellGranularity(int number);
 
- void
- SetSceneCut(int cut);
-
- void
- SetTopDistance(int dist);
-
+  void
+  SetVisibilityFilterSize(int number);
+
+  void
+  SetSceneCut(int cut);
+  
+  void
+  SetTopDistance(int dist);
+  
   void SetShowViewCells(bool b) {
 	mRenderViewCells = b;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 712)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 713)
@@ -615,5 +615,5 @@
 		neighborhood.pop_back();
 	
-		//cout << "vc idx: " << bestViewCellIdx << endl;
+		//		cout << "vc idx: " << bestViewCellIdx << endl;
 		if (!bestViewCell || !root)
 			cout << "warning!!" << endl;
@@ -627,4 +627,70 @@
 
 
+struct SortableViewCellEntry {
+
+  SortableViewCellEntry() {}
+  SortableViewCellEntry(const float v, ViewCell *cell):mValue(v), mViewCell(cell) {}
+  
+  float mValue;
+  ViewCell *mViewCell;
+
+  friend bool operator<(const SortableViewCellEntry &a, const SortableViewCellEntry &b) {
+	return a.mValue < b.mValue;
+  }
+};
+
+ViewCell *
+ViewCellsManager::ConstructLocalMergeTree2(ViewCell *currentViewCell, 
+										   const ViewCellContainer &viewCells)
+{
+  
+  vector<SortableViewCellEntry> neighborhood(viewCells.size());
+  int i, j;
+  for (i = 0, j = 0; i < viewCells.size(); i++) {
+	if (viewCells[i] != currentViewCell) 
+	  neighborhood[j++] = SortableViewCellEntry(
+												EvalMergeCost(currentViewCell, viewCells[i]),
+												viewCells[i]);
+  }
+  neighborhood.resize(j);
+  
+  sort(neighborhood.begin(), neighborhood.end());
+  
+  ViewCell *root = currentViewCell;
+  
+  vector<SortableViewCellEntry>::const_iterator it, it_end = neighborhood.end();
+  
+  const int n = min(mMaxFilterSize, (int)neighborhood.size());
+  //-- use priority queue to merge leaf pairs
+  
+  //cout << "neighborhood: " << neighborhood.size() << endl;
+  for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
+	{
+	  ViewCell *bestViewCell = neighborhood[nMergedViewCells].mViewCell;
+	  //cout <<nMergedViewCells<<":"<<"homogenity=" <<neighborhood[nMergedViewCells].mValue<<endl;
+	  // create new root of the hierarchy
+	  root = MergeViewCells(root, bestViewCell);
+	  // set negative cost so that this view cell gets deleted
+	  root->SetMergeCost(-1.0f);
+	}
+  
+  return root;	
+}
+
+void
+ViewCellsManager::DeleteLocalMergeTree(ViewCell *vc
+									   ) const
+{
+  if (!vc->IsLeaf() && vc->GetMergeCost() < 0.0f) {
+	ViewCellInterior *vci = (ViewCellInterior *) vc;
+	ViewCellContainer::const_iterator it, it_end = vci->mChildren.end();
+	
+	for (it = vci->mChildren.begin(); it != it_end; ++ it)
+	  DeleteLocalMergeTree(*it);
+	vci->mChildren.clear();
+	delete vci;
+  }
+}
+
 bool ViewCellsManager::CheckValidity(ViewCell *vc,
 									 int minPvsSize,
@@ -672,5 +738,5 @@
 	ComputeBoxIntersections(box, viewCells);
 
-	ViewCell *root = ConstructLocalMergeTree(currentViewCell, viewCells);
+	ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells);
 
 	prvs.mViewCell = root;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 685)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 713)
@@ -301,8 +301,8 @@
 
 float
-GlRendererBuffer::GetPixelError()
+GlRendererBuffer::GetPixelError(int &pvsSize)
 {
   float pErrorPixels = -1.0f;
-
+  
   glReadBuffer(GL_BACK);
   
@@ -351,6 +351,13 @@
 	
 
-  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
-	
+  ViewCell *viewcell = NULL;
+  
+  PrVs prvs;
+  //  mViewCellsManager->SetMaxFilterSize(1);
+  mViewCellsManager->GetPrVS(mViewPoint, prvs);
+  viewcell = prvs.mViewCell;
+  
+  //  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+  pvsSize = 0;
   if (viewcell) {
 	SetupCamera();
@@ -363,4 +370,6 @@
 	  PvsData<Intersectable *>,
 	  LtSample<Intersectable *> >::const_iterator it = viewcell->GetPvs().mEntries.begin();
+
+	pvsSize = viewcell->GetPvs().mEntries.size();
 	
 	for (; it != viewcell->GetPvs().mEntries.end(); ++ it) {
@@ -436,4 +445,7 @@
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
   }
+
+  if (viewcell)
+	mViewCellsManager->DeleteLocalMergeTree(viewcell);
   
   return pErrorPixels;
@@ -487,5 +499,11 @@
 	RenderIntersectable(*oi);
 
-  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+  ViewCell *viewcell;
+  //  viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+
+  PrVs prvs;
+  //  mViewCellsManager->SetMaxFilterSize(1);
+  mViewCellsManager->GetPrVS(mViewPoint, prvs);
+  viewcell = prvs.mViewCell;
   
   QImage im1, im2;
@@ -587,4 +605,7 @@
   glDisable(GL_CLIP_PLANE0);
 
+  if (viewcell)
+	mViewCellsManager->DeleteLocalMergeTree(viewcell);
+
   return pErrorPixels;
 }
@@ -615,5 +636,5 @@
 {
   for (int i=0; i < mPvsStatFrames; i++) {
-	mPvsErrorBuffer[i] = 1.0f;
+	mPvsErrorBuffer[i].mError = 1.0f;
   }
 }
@@ -646,11 +667,15 @@
 	}
 #endif
-	if (mPvsErrorBuffer[i] > 0.0f) {
-	  mPvsErrorBuffer[i] = GetPixelError();
-	  cout<<"("<<i<<","<<mPvsErrorBuffer[i]<<")";
+	if (mPvsErrorBuffer[i].mError > 0.0f) {
+	  int pvsSize;
+	  float error = GetPixelError(pvsSize);
+	  mPvsErrorBuffer[i].mError = error;
+	  mPvsErrorBuffer[i].mPvsSize = pvsSize;
+
+	  cout<<"("<<i<<","<<mPvsErrorBuffer[i].mError<<")";
 	  //	  swapBuffers();
 	}
 	
-	err = mPvsErrorBuffer[i];
+	err = mPvsErrorBuffer[i].mError;
 	
 	if (err >= 0.0f) {
@@ -658,4 +683,6 @@
 		mPvsStat.maxError = err;
 	  mPvsStat.sumError += err;
+	  mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
+	  
 	  if (err == 0.0f)
 		mPvsStat.errorFreeFrames++;
@@ -820,13 +847,28 @@
   vl = new QVBoxLayout;
   vbox->setLayout(vl);
-  
+
+  QLabel *label = new QLabel("Granularity");
+  vbox->layout()->addWidget(label);
+
   QSlider *slider = new QSlider(Qt::Horizontal, vbox);
   vl->addWidget(slider);
   slider->show();
-  slider->setRange(1, 5000);
+  slider->setRange(1, 200);
   slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
-  slider->setValue(500);
+  slider->setValue(200);
 
   connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
+
+  label = new QLabel("Filter size");
+  vbox->layout()->addWidget(label);
+  
+  slider = new QSlider(Qt::Horizontal, vbox);
+  vbox->layout()->addWidget(slider);
+  slider->show();
+  slider->setRange(1, 100);
+  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
+  slider->setValue(3);
+  
+  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
 
 
@@ -881,4 +923,7 @@
   connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
 
+
+
+  
   cb = new QCheckBox("Top View", vbox);
   vbox->layout()->addWidget(cb);
@@ -886,5 +931,5 @@
   connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
   
-
+  setWindowTitle("Preprocessor Control Widget");
   adjustSize();
 }
@@ -910,4 +955,9 @@
   connect(cw, SIGNAL(SetSceneCut(int)), this, SLOT(SetSceneCut(int)));
   connect(cw, SIGNAL(SetTopDistance(int)), this, SLOT(SetTopDistance(int)));
+
+  //connect(cw, SIGNAL(SetViewCellGranularity(int)), this, SLOT(SetMaxFilterSize(int)));
+  //  connect(cw, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetVisibilityFilterSize(int)));
+  //connect(cw, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetViewCellGranularity(int)));
+
   connect(cw, SIGNAL(SetShowViewCells(bool)), this, SLOT(SetShowViewCells(bool)));
   connect(cw, SIGNAL(SetTopView(bool)), this, SLOT(SetTopView(bool)));
@@ -921,5 +971,15 @@
 GlRendererWidget::SetViewCellGranularity(int number)
 {
-  mViewCellsManager->CollectViewCells(number);
+  if (mViewCellsManager) 
+  	mViewCellsManager->SetMaxFilterSize(number);
+  //  mViewCellsManager->CollectViewCells(number);
+  updateGL();
+}
+
+void
+GlRendererWidget::SetVisibilityFilterSize(int number)
+{
+  if (mViewCellsManager) 
+  	mViewCellsManager->SetMaxFilterSize(number);
   updateGL();
 }
