Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtGlViewer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtGlViewer.cpp	(revision 1931)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtGlViewer.cpp	(revision 1931)
@@ -0,0 +1,187 @@
+// ===================================================================
+// $Id$
+
+// Qt headers
+#include <QtGui>
+#include <QtOpenGL>
+
+// C++ standard headers
+#include <cmath>
+
+// local headers
+#include "common.h"
+#include "QtGlViewer.h"
+#include "QtGlRenderer.h"
+#include "Trackball.h"
+#include "RssPreprocessor.h"
+#include "SceneGraph.h"
+
+
+namespace GtpVisibilityPreprocessor {
+
+QtGlRendererWidget *renderer;
+
+QtGlViewer::QtGlViewer(QWidget *parent,
+					   QtGlRendererWidget *renderer):
+  QGLWidget(parent), mRenderer(renderer)
+{
+	scale = 1.0f;
+	mWireframe = false;
+	trackball(manipulatorLastQuat, 0.0f, 0.0f, 0.0f, 0.0f);
+	connect(renderer, SIGNAL(PvsUpdated()), this, SLOT(updateGL()));
+}
+  
+QSize
+QtGlViewer::minimumSizeHint() const
+{
+  return QSize(100, 100);
+}
+
+QSize
+QtGlViewer::sizeHint() const {
+  return QSize(500, 500);
+}
+  
+// This method has to be rendefined by all renderer implementations
+void
+QtGlViewer::RenderScene()
+{
+	mRenderer->mFrame++;
+	
+	// render into the right side of the window buffer
+	if (mRenderer->mRenderViewCells) 
+	  mRenderer->RenderViewCells();
+	
+	mRenderer->RenderPvs();
+	
+// 	if (mRenderer->mShowRays) {
+// 	  RssPreprocessor *p = (RssPreprocessor *)
+// 			mRenderer->mViewCellsManager->GetPreprocessor();
+// 	  if (p->mRssTree) {
+// 			VssRayContainer rays;
+// 			p->mRssTree->CollectRays(rays, 10000);
+// 			mRenderer->RenderRays(rays);
+// 	  }
+// 	}
+	
+	if (mRenderer->mShowRenderCost) 
+	  mRenderer->RenderRenderCost();
+
+	glColor3f(1.0f, 1.0f, 1.0f);
+	mRenderer->RenderInfo();
+}
+
+
+void
+QtGlViewer::initializeGL()
+{  
+  const char *vendorString, *rendererString, *versionString, *extString;
+
+  // get GL info
+  vendorString = (const char *) glGetString(GL_VENDOR);
+  rendererString = (const char *) glGetString(GL_RENDERER);
+  versionString = (const char *) glGetString(GL_VERSION);
+  extString = (const char *) glGetString(GL_EXTENSIONS);
+  
+  Debug << "Info: GL Vendor = "<<vendorString<<endl
+        << "Info: GL Renderer = "<<rendererString<<endl
+        << "Info: GL Version = "<<versionString<<endl
+        << "Info: GL Extensions = "<<extString<<endl<<flush;
+
+
+  return;
+}
+
+void
+QtGlViewer::paintGL()
+{
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+  glLoadIdentity();
+
+  float m[4][4];
+
+  glLoadIdentity();
+  gluLookAt(0.0, 0.0, 1.0,  /* eye is at (0,0,30) */
+						0.0, 0.0, 0.0,      /* center is at (0,0,0) */
+						0.0, 1.0, 0.);      /* up is in positive Y direction */
+  
+  build_rotmatrix(m, manipulatorLastQuat);
+  glMultMatrixf(&m[0][0]);
+
+  float s = scale*20.0f/Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
+  glScalef(s, s, s);
+  
+  Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
+  glTranslatef(t.x, t.y, t.z);
+
+	RenderScene();
+}
+
+void
+QtGlViewer::resizeGL(int width, int height)
+{
+  glViewport(0, 0, width, height);
+  
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  gluPerspective(70.0, 1.0, 0.1, 100.0);
+  //  glOrtho(-0.5*width/height, +0.5*width/height, 0.5, -0.5, 4.0, 15.0);
+  glMatrixMode(GL_MODELVIEW);
+}
+
+
+void
+QtGlViewer::mousePressEvent(QMouseEvent *event)
+{
+  lastPos = event->pos();
+}
+
+void
+QtGlViewer::keyPressEvent ( QKeyEvent * e )
+{
+  switch (e->key()) {
+  case Qt::Key_W:
+		mWireframe  = !mWireframe;
+		updateGL();
+  default:
+		e->ignore();
+		break;
+  }
+  updateGL();
+}
+
+void
+QtGlViewer::mouseMoveEvent(QMouseEvent *event)
+{
+  int dx = event->x() - lastPos.x();
+  int dy = event->y() - lastPos.y();  
+  
+  if (event->modifiers() & Qt::CTRL)
+		{
+			scale = scale*(1.0f - dy/(float)height());
+			if (scale < 0.01)
+				scale = 0.01;
+			updateGL();
+		}
+  else {
+		float W = width();
+		float H = height();
+		int x = event->x();
+		int lastX = lastPos.x();
+		int y = event->y();
+		int lastY = lastPos.y();
+	
+		float quat[4];
+		trackball(quat,
+							(2.0 * lastX - W) / W,
+							(H - 2.0 * lastY) / H,
+							(2.0 * x - W) / W,
+							(H - 2.0 * y) / H
+							);
+		add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
+  }
+  
+  lastPos = event->pos();
+updateGL();
+}
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp	(revision 1877)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp	(revision 1931)
@@ -102,4 +102,8 @@
   OcclusionQuery query;
 
+  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+  if (viewcell == NULL)
+	return 0.0f;
+
   if (mDetectEmptyViewSpace) {
 	// now check whether any backfacing polygon would pass the depth test
@@ -108,5 +112,6 @@
 	glEnable( GL_CULL_FACE );
 	glCullFace(GL_BACK);
-	
+
+	cout<<"RS ";
 	RenderScene();
 	
@@ -117,5 +122,7 @@
 	query.BeginQuery();
 	
+	cout<<"RS ";
 	RenderScene();
+	cout<<"RS3 ";
 	
 	query.EndQuery();
@@ -128,4 +135,5 @@
 	// reenable other state
 	pixelCount = query.GetQueryResult();
+	cout<<"RS4 ";
 	
 	if (pixelCount > 0)
@@ -145,52 +153,49 @@
   
   ObjectPvs pvs;
-  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
-	
-  if (viewcell) {
+  if (1) {
+	pvs = viewcell->GetPvs();
+  } else {
 	mViewCellsManager->ApplyFilter2(viewcell,
 									false,
 									1.0f,
 									pvs);
-	
-	pvsSize = 0;
-	SetupCamera();
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
-		
-	  
-		// 	// Render PVS
-		ObjectPvsIterator it = pvs.GetIterator();
-		
-		pvsSize = pvs.GetSize();
-		Intersectable::NewMail();
-	for (; it.HasMoreEntries(); ) {
-	  ObjectPvsEntry entry = it.Next();
-	  Intersectable *object = entry.mObject;
-	  RenderIntersectable(object);
-	}
-	
-	//	glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
-	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-	
-	
-	mUseFalseColors = true;
-	
-	query.BeginQuery();
-	
-	SetupCamera();
-	
-	RenderScene();
-	
-	query.EndQuery();
-	
-	
-	unsigned int pixelCount;
-	// reenable other state
-	pixelCount = query.GetQueryResult();
-	
-	
-	pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
-  } else
-	pErrorPixels = 0.0f;
+  }
+  
+  SetupCamera();
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+  glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
+  
+  
+  // 	// Render PVS
+  ObjectPvsIterator it = pvs.GetIterator();
+  
+  pvsSize = pvs.GetSize();
+  Intersectable::NewMail();
+  for (; it.HasMoreEntries(); ) {
+	ObjectPvsEntry entry = it.Next();
+	Intersectable *object = entry.mObject;
+	RenderIntersectable(object);
+  }
+  
+  //	glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
+  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+  
+  mUseFalseColors = true;
+  
+  query.BeginQuery();
+  
+  SetupCamera();
+  
+  RenderScene();
+  
+  query.EndQuery();
+  
+  
+  // reenable other state
+  pixelCount = query.GetQueryResult();
+  
+  
+  pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
+
   
   if (mSnapErrorFrames && pErrorPixels > 0.001f) {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1926)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1931)
@@ -1270,3 +1270,25 @@
 }
 
-}
+void
+Preprocessor::ComputeRenderError()
+{
+  // compute rendering error
+	
+  if (renderer && renderer->mPvsStatFrames) {
+	//	emit EvalPvsStat();
+	//	QMutex mutex;
+	//	mutex.lock();
+	//	renderer->mRenderingFinished.wait(&mutex);
+	//	mutex.unlock();
+	
+	renderer->EvalPvsStat();
+
+	mStats <<
+	  "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
+	  "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
+	  "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
+	  "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
+  }
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1926)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1931)
@@ -303,25 +303,4 @@
 
 
-void
-RssPreprocessor::ComputeRenderError()
-{
-  // compute rendering error
-	
-  if (renderer && renderer->mPvsStatFrames) {
-	//	emit EvalPvsStat();
-	//	QMutex mutex;
-	//	mutex.lock();
-	//	renderer->mRenderingFinished.wait(&mutex);
-	//	mutex.unlock();
-	
-	renderer->EvalPvsStat();
-
-	mStats <<
-	  "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
-	  "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
-	  "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
-	  "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
-  }
-}
 
 void
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1900)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1931)
@@ -103,5 +103,5 @@
 	samplesPerPass 1000000
 	initialSamples 2000000
-	vssSamples 20000000
+	vssSamples 150000000
 	vssSamplesPerPass 5000000
 	useImportanceSampling true
@@ -195,4 +195,5 @@
 
 ViewCells {
+	useKdPvs true
 	# samples used for view cell construction
 	Construction {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1926)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1931)
@@ -146,4 +146,8 @@
 		const bool pruneInvalidRays = true);
 
+  /** Compute pixel error of the current PVS solution by sampling given number of viewpoints */
+  virtual void
+  ComputeRenderError();
+
 	/** Returns a view cells manager of the given name.
 	*/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 1900)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 1931)
@@ -115,6 +115,4 @@
 
 
-  void
-  ComputeRenderError();
 
   /** Redefininition of the get sample rays method from the preprocessor */
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1928)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1931)
@@ -30,5 +30,8 @@
 
 #define USE_RAY_LENGTH_AS_CONTRIBUTION 0
-#define DIST_WEIGHTED_CONTRIBUTION 1
+#define DIST_WEIGHTED_CONTRIBUTION 0
+#define SUM_RAY_CONTRIBUTIONS 0
+#define AVG_RAY_CONTRIBUTIONS 1
+#define CONTRIBUTION_RELATIVE_TO_PVS_SIZE 1
 
 namespace GtpVisibilityPreprocessor {
@@ -1693,5 +1696,5 @@
 	  }
 	}
-  
+
   return sum;
 }
@@ -2193,5 +2196,5 @@
 	Intersectable *terminationObj = GetIntersectable(ray, true);
 
-
+	
 	for (; it != viewcells.end(); ++ it) 
 	{
@@ -2201,44 +2204,60 @@
 		{	
 			// if ray not outside of view space
-			float contribution;
-
+			float relContribution = 0.0f;
+			float absContribution = 0.0f;
+			
 			if (terminationObj) 
 			{
 				// todo: maybe not correct for kd node pvs
-				bool absContribution = 
-					viewcell->GetPvs().GetSampleContribution(
-															terminationObj,
-															ray.mPdf,
-															contribution);
-#if DIST_WEIGHTED_CONTRIBUTION
-				// clear the relative contribution
-				contribution = 0.0f;
+			  if (viewcell->GetPvs().GetSampleContribution(
+														   terminationObj,
+														   ray.mPdf,
+														   relContribution))
+				absContribution = 1.0f;
+			  
+			  // $$ clear the relative contribution as it is currently not correct anyway
+			  relContribution = 0.0f;
+
+			  if (absContribution == 1.0f)
+				{
+				  ++ ray.mPvsContribution;
+				  relContribution = 1.0f;
+				  if (addRays) {
+#if PVS_ADD_DIRTY
+					viewcell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf);
+					if (viewcell->GetPvs().RequiresResort()) {
+					  viewcell->GetPvs().SimpleSort();
+					}
+#else
+					viewcell->GetPvs().AddSample(terminationObj, ray.mPdf);
 #endif
-				if (absContribution)
-				{
-					++ ray.mPvsContribution;
-					if (addRays) {
-#if PVS_ADD_DIRTY
-						viewcell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf);
-						if (viewcell->GetPvs().RequiresResort()) {
-							viewcell->GetPvs().SimpleSort();
-						}
-#else
-						viewcell->GetPvs().AddSample(terminationObj, ray.mPdf);
+				  }
+
+#if CONTRIBUTION_RELATIVE_TO_PVS_SIZE
+				  relContribution /= viewcell->GetPvs().GetSize();
 #endif
-					}
+				  
 #if DIST_WEIGHTED_CONTRIBUTION 
-					// recalculate the contribution - weight the 1.0f contribution by the sqr distance to the
-					// object-> a new contribution in the proximity of the viewcell has a larger weight!
-					contribution = 1.0f/SqrDistance(GetViewCellBox(viewcell).Center(),
-						ray.mTermination);
+				  // recalculate the contribution - weight the 1.0f contribution by the sqr distance to the
+				  // object-> a new contribution in the proximity of the viewcell has a larger weight!
+				  relContribution /= SqrDistance(GetViewCellBox(viewcell).Center(),
+												 ray.mTermination);
+				  
 #endif
 				}
-
-				ray.mRelativePvsContribution += contribution;
+#if SUM_RAY_CONTRIBUTIONS || AVG_RAY_CONTRIBUTIONS
+			  ray.mRelativePvsContribution += relContribution;
+#else
+			  // recalculate relative contribution - use max of AbsContribution
+			  if (ray.mRelativePvsContribution < absContribution)
+				ray.mRelativePvsContribution = absContribution;
+#endif
 			}
 		}
 	}
 
+#if AVG_RAY_CONTRIBUTIONS
+	ray.mRelativePvsContribution /= viewcells.size();
+#endif
 #if USE_RAY_LENGTH_AS_CONTRIBUTION
   float c = 0.0f;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 1925)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 1931)
@@ -84,6 +84,11 @@
   mUseForcedColors = false;
   mRenderBoxes = false;
-  //  mUseGlLists = true;
-  mUseGlLists = false;
+  mUseGlLists = true;
+  //mUseGlLists = false;
+
+  Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames);
+  mPvsErrorBuffer.resize(mPvsStatFrames);
+  ClearErrorBuffer();
+  
 }
 
@@ -466,7 +471,4 @@
 GlRenderer(sceneGraph, viewcells, tree)  
 {
-  Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames);
-  mPvsErrorBuffer.resize(mPvsStatFrames);
-  ClearErrorBuffer();
   
   mPixelBuffer = NULL;
@@ -572,5 +574,5 @@
 
 void
-GlRendererBuffer::RandomViewPoint()
+GlRenderer::RandomViewPoint()
 { 
   // do not use this function since it could return different viewpoints for
@@ -858,5 +860,5 @@
 
 void
-GlRendererBuffer::ClearErrorBuffer()
+GlRenderer::ClearErrorBuffer()
 {
   for (int i=0; i < mPvsStatFrames; i++) {
@@ -869,64 +871,11 @@
 GlRendererBuffer::EvalPvsStat()
 {
-  mPvsStat.Reset();
-  halton.Reset();
 
   MakeCurrent();
 
-  SetupProjection(GetWidth(), GetHeight());
-
-  cout<<"mPvsStatFrames="<<mPvsStatFrames<<endl;
-  
-  for (int i=0; i < mPvsStatFrames; i++) {
-	float err;
-	// set frame id for saving the error buffer
-	mFrame = i;
-	RandomViewPoint();
-
-	// atlanta problematic frames: 325 525 691 1543
-#if 0
-	if (mFrame != 325 &&
-		mFrame != 525 &&
-		mFrame != 691 &&
-		mFrame != 1543)
-	  mPvsErrorBuffer[i] = -1;
-	else {
-	  Debug<<"frame ="<<mFrame<<" vp="<<mViewPoint<<" vd="<<mViewDirection<<endl;
-	}
-#endif
-	
-	if (mPvsErrorBuffer[i].mError > 0.0f) {
-	  int pvsSize;
-
-
-	  float error = GetPixelError(pvsSize);
-	  mPvsErrorBuffer[i].mError = error;
-	  mPvsErrorBuffer[i].mPvsSize = pvsSize;
-
-	  //	  emit UpdatePvsErrorItem(i,
-	  //							  mPvsErrorBuffer[i]);
-
-	  cout<<"("<<i<<","<<mPvsErrorBuffer[i].mError<<")";
-	  //	  swapBuffers();
-	}
-	
-	err = mPvsErrorBuffer[i].mError;
-	
-	if (err >= 0.0f) {
-	  if (err > mPvsStat.maxError)
-		mPvsStat.maxError = err;
-	  mPvsStat.sumError += err;
-	  mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
-	  
-	  if (err == 0.0f)
-		mPvsStat.errorFreeFrames++;
-	  mPvsStat.frames++;
-	}
-  }
-
-  glFinish();
+  GlRenderer::EvalPvsStat();
+  
   DoneCurrent();
-
-  cout<<endl<<flush;
+  
   //  mRenderingFinished.wakeAll();
 }
@@ -1363,13 +1312,246 @@
 
 
-
-
-
-
-
-
-
-
-
-
-}
+void
+GlRenderer::EvalPvsStat()
+{
+
+  mPvsStat.Reset();
+  halton.Reset();
+
+
+  SetupProjection(GetWidth(), GetHeight());
+
+  cout<<"mPvsStatFrames="<<mPvsStatFrames<<endl;
+  
+  for (int i=0; i < mPvsStatFrames; i++) {
+	float err;
+	// set frame id for saving the error buffer
+	mFrame = i;
+	RandomViewPoint();
+
+	// atlanta problematic frames: 325 525 691 1543
+#if 0
+	if (mFrame != 325 &&
+		mFrame != 525 &&
+		mFrame != 691 &&
+		mFrame != 1543)
+	  mPvsErrorBuffer[i] = -1;
+	else {
+	  Debug<<"frame ="<<mFrame<<" vp="<<mViewPoint<<" vd="<<mViewDirection<<endl;
+	}
+#endif
+	
+	if (mPvsErrorBuffer[i].mError > 0.0f) {
+	  int pvsSize;
+
+
+	  float error = GetPixelError(pvsSize);
+	  mPvsErrorBuffer[i].mError = error;
+	  mPvsErrorBuffer[i].mPvsSize = pvsSize;
+
+	  //	  emit UpdatePvsErrorItem(i,
+	  //							  mPvsErrorBuffer[i]);
+
+	  cout<<"("<<i<<","<<mPvsErrorBuffer[i].mError<<")";
+	  //	  swapBuffers();
+	}
+	
+	err = mPvsErrorBuffer[i].mError;
+	
+	if (err >= 0.0f) {
+	  if (err > mPvsStat.maxError)
+		mPvsStat.maxError = err;
+	  mPvsStat.sumError += err;
+	  mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
+	  
+	  if (err == 0.0f)
+		mPvsStat.errorFreeFrames++;
+	  mPvsStat.frames++;
+	}
+  }
+
+  glFinish();
+  cout<<endl<<flush;
+
+}
+
+
+float
+GlRenderer::GetPixelError(int &pvsSize)
+{
+  float pErrorPixels = -1.0f;
+  
+  glReadBuffer(GL_BACK);
+  
+  //  mUseFalseColors = true;
+  
+  mUseFalseColors = false;
+  unsigned int pixelCount;
+
+  //static int query = -1;
+  //if (query == -1)
+  //	  glGenOcclusionQueriesNV(1, (unsigned int *)&query);
+
+  OcclusionQuery query;
+
+  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
+  if (viewcell == NULL)
+	return 0.0f;
+
+  if (mDetectEmptyViewSpace) {
+	// now check whether any backfacing polygon would pass the depth test
+	SetupCamera();
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	glEnable( GL_CULL_FACE );
+	glCullFace(GL_BACK);
+
+	cout<<"RS ";
+	RenderScene();
+	
+	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+	glDepthMask(GL_FALSE);
+	glDisable( GL_CULL_FACE );
+
+	query.BeginQuery();
+	
+	cout<<"RS ";
+	RenderScene();
+	cout<<"RS3 ";
+	
+	query.EndQuery();
+	
+	// at this point, if possible, go and do some other computation
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	glDepthMask(GL_TRUE);
+	glEnable( GL_CULL_FACE );
+	
+	// reenable other state
+	pixelCount = query.GetQueryResult();
+	cout<<"RS4 ";
+	
+	if (pixelCount > 0)
+	  return -1.0f; // backfacing polygon found -> not a valid viewspace sample
+
+  } else
+	glDisable( GL_CULL_FACE );
+	
+
+  //  ViewCell *viewcell = NULL;
+  
+  //  PrVs prvs;
+  
+  //  mViewCellsManager->SetMaxFilterSize(0);
+  //  mViewCellsManager->GetPrVS(mViewPoint, prvs, mViewCellsManager->GetFilterWidth());
+  //  viewcell = prvs.mViewCell;
+  
+  ObjectPvs pvs;
+  if (1) {
+	pvs = viewcell->GetPvs();
+  } else {
+	mViewCellsManager->ApplyFilter2(viewcell,
+									false,
+									1.0f,
+									pvs);
+  }
+  
+  SetupCamera();
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+  glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
+  
+  
+  // 	// Render PVS
+  ObjectPvsIterator it = pvs.GetIterator();
+  
+  pvsSize = pvs.GetSize();
+  Intersectable::NewMail();
+  for (; it.HasMoreEntries(); ) {
+	ObjectPvsEntry entry = it.Next();
+	Intersectable *object = entry.mObject;
+	RenderIntersectable(object);
+  }
+  
+  //	glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
+  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+  
+  mUseFalseColors = true;
+  
+  query.BeginQuery();
+  
+  SetupCamera();
+  
+  RenderScene();
+  
+  query.EndQuery();
+  
+  
+  // reenable other state
+  pixelCount = query.GetQueryResult();
+  
+  
+  pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
+
+#if 0  
+  if (mSnapErrorFrames && pErrorPixels > 0.001f) {
+	
+	char filename[256];
+	sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels);
+	QImage im = toImage();
+	string str = mSnapPrefix + filename;
+	QString qstr(str.c_str());
+	
+	im.save(qstr, "PNG");
+	if (1) { //0 && mFrame == 1543) {
+	  int x,y;
+	  int lastIndex = -1;
+	  for (y=0; y < im.height(); y++)
+		for (x=0; x < im.width(); x++) {
+		  QRgb p = im.pixel(x,y);
+		  int index = qRed(p) + (qGreen(p)<<8) + (qBlue(p)<<16);
+		  if (qGreen(p) != 255 && index!=0) {
+			if (index != lastIndex) {
+			  //				Debug<<"ei="<<index<<" ";
+			  lastIndex = index;
+			}
+		  }
+		}
+	}
+	
+	
+	mUseFalseColors = false;
+	glPushAttrib(GL_CURRENT_BIT);
+	glColor3f(0,1,0);
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	SetupCamera();
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	
+	// Render PVS
+	Intersectable::NewMail();
+
+	ObjectPvsIterator it = pvs.GetIterator();
+	for (; it.HasMoreEntries(); ) {
+	  ObjectPvsEntry entry = it.Next();
+	  Intersectable *object = entry.mObject;
+	  RenderIntersectable(object);
+	}
+	
+	im = toImage();
+	sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels);
+	str = mSnapPrefix + filename;
+	qstr = str.c_str();
+	im.save(qstr, "PNG");
+	glPopAttrib();
+  }
+#endif
+  
+  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+  
+  return pErrorPixels;
+}
+
+
+
+
+
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtGlViewer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtGlViewer.h	(revision 1931)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtGlViewer.h	(revision 1931)
@@ -0,0 +1,54 @@
+#ifndef __QTGLVIEWER_H
+#define __QTGLVIEWER_H
+
+#include <QtOpenGL>
+
+
+namespace GtpVisibilityPreprocessor {
+
+class QtGlRendererWidget;
+
+class QtGlViewer : public QGLWidget
+{
+	
+	Q_OBJECT
+
+	friend class QtGlRendererWidget;
+	friend class GlRenderer;
+
+public:
+	QtGlRendererWidget *mRenderer;
+	
+	QtGlViewer(QWidget *parent,
+						 QtGlRendererWidget *renderer);
+	
+  virtual ~QtGlViewer(){};
+  
+  QSize minimumSizeHint() const;
+  QSize sizeHint() const;
+  
+  // This method has to be rendefined by all renderer implementations
+  virtual void RenderScene();
+  
+protected:
+  void initializeGL();
+  void paintGL();
+  void resizeGL(int width, int height);
+  void mousePressEvent(QMouseEvent *event);
+  void mouseMoveEvent(QMouseEvent *event);
+
+	void keyPressEvent ( QKeyEvent * e );
+	
+  bool mWireframe;
+
+private:
+
+	float manipulatorLastQuat[4];
+  float scale;
+  QPoint lastPos;
+
+};
+
+}
+
+#endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1900)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1931)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: ?t 14. XII 10:54:14 2006
+# Generated by qmake (2.00a) (Qt 4.1.2) on: út 2. I 10:32:48 2007
 # Project:  preprocessor.pro
 # Template: app
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1906)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1931)
@@ -2629,81 +2629,13 @@
 {
 
-  if (1) {
-	float param[5];
-	
-	for (int i=0; i < numberOfRays; i++) {
-	  mHalton.GetNext(5, param);
-	  GenerateRay(param, rays);
-	}
-
-	return rays.size();
-  }
-  
-  vector<RssTreeLeaf *> leaves;
-
-  Debug<<"Collecting leaves..."<<endl<<flush;
-  CollectLeaves(leaves);
-  Debug<<"done."<<endl<<flush;
-  
-  
-  if (numberOfLeaves != leaves.size()) {
-	Debug<<"sorting leaves..."<<endl<<flush;
-	sort(leaves.begin(),
-		 leaves.end(),
-		 GreaterContribution);
-	Debug<<"done."<<endl<<flush;
-  }
-			 
-  
-  Debug<<"Evaluating leaf importance..."<<endl<<flush;
-
-  float sumContrib = 0.0;
-  int i;
-  int k = 0;
-  for (i=0; i < leaves.size() && k < numberOfLeaves; i++)
-	if (ValidLeaf(leaves[i])) {
-	  float c = leaves[i]->GetImportance();
-	  sumContrib += c;
-	  //		cout<<"ray contrib "<<i<<" : "<<c<<endl;
-	  k++;
-	}
-
-  Debug<<"done."<<endl<<flush;
-  
-  float avgContrib = sumContrib/numberOfLeaves;
-  
-  // always generate at leat n ray per leaf
-  int fixedPerLeaf = 1;
-  // int fixedPerLeaf = 0;
-  int fixed = fixedPerLeaf*(int)leaves.size();
-
-  // check if the number of fixed is not too big
-
-  if (fixed >= numberOfRays/2) {
-	fixedPerLeaf = 0;
-	fixed = 0;
-  }
-  
-  int iGenerated = numberOfRays - fixed;
-  float ratioPerLeaf = iGenerated /(avgContrib*numberOfLeaves);
-  
-  k = 0;
-
-  Debug<<"generating leaf rays..."<<endl<<flush;
-
-  for (i=0; i < leaves.size() && k < numberOfLeaves; i++)
-	if (ValidLeaf(leaves[i])) {
-	  RssTreeLeaf *leaf = leaves[i];
-	  float c = leaf->GetImportance();
-	  
-	  int num = fixedPerLeaf + (int)(c*ratioPerLeaf + 0.5f);
-	  GenerateLeafRaysSimple(leaf, num, rays);
-
-	  k++;
-	}
-
-  Debug<<"done."<<endl<<flush;
-
-  return (int)rays.size();
+  float param[5];
+  
+  for (int i=0; i < numberOfRays; i++) {
+	mHalton.GetNext(5, param);
+	GenerateRay(param, rays);
+  }
+  
+  return rays.size();
+  
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 1900)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 1931)
@@ -108,4 +108,9 @@
 	if (!mMixtureDistribution->RequiresRays())
 	  CLEAR_CONTAINER(vssRays);
+
+	if (renderer) {
+	  ComputeRenderError();
+	}
+	
   }
   
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 1832)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 1931)
@@ -104,4 +104,6 @@
 
 
+  virtual void RandomViewPoint();
+
   void SetupFalseColor(const int id);
   void RenderIntersectable(Intersectable *);
@@ -139,5 +141,9 @@
 
   
-  
+  virtual float
+  GetPixelError(int &pvsSize);
+  
+  virtual void
+  EvalPvsStat();
 
   void InitGL();
@@ -154,11 +160,28 @@
   inline void SetSnapPrefix(const string &pref) { mSnapPrefix = pref; }
 
+  virtual void ClearErrorBuffer();
+  
+
 public:
   int mFrame;
   bool mWireFrame;
-	
+
+  PvsRenderStatistics mPvsStat;
+  
+  int mPvsStatFrames;
+  struct PvsErrorEntry {
+	  PvsErrorEntry() {}
+	  float mError;
+	  int mPvsSize;
+	  Vector3 mPosition;
+	  Vector3 mDirection;
+  };
+
 protected:
 
-	PvsCache mPvsCache;
+  
+  vector<PvsErrorEntry> mPvsErrorBuffer;
+
+  PvsCache mPvsCache;
 
   vector<OcclusionQuery *> mOcclusionQueries;
@@ -231,6 +254,4 @@
   virtual void EvalPvsStat();
 
-  virtual void ClearErrorBuffer();
-  
 
   virtual int GetWidth() const = 0;
@@ -241,5 +262,4 @@
   
 
-  virtual void RandomViewPoint();
   virtual void SampleBeamContributions(
 							   Intersectable *sourceObject,
@@ -266,21 +286,8 @@
   virtual int ComputePvs() const = 0;
 
-  virtual float
-  GetPixelError(int &pvsSize) = 0;
   
   virtual int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const = 0;
-  
-  PvsRenderStatistics mPvsStat;
-   
-  int mPvsStatFrames;
-  struct PvsErrorEntry {
-	  PvsErrorEntry() {}
-	  float mError;
-	  int mPvsSize;
-	  Vector3 mPosition;
-	  Vector3 mDirection;
-  };
-  
-  vector<PvsErrorEntry> mPvsErrorBuffer;
+
+
 
   
Index: GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 1891)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/preprocessor.pro	(revision 1931)
@@ -109,8 +109,7 @@
 InternalRayCaster.cpp IntelRayCaster.cpp \
 RayCaster.cpp PreprocessorFactory.cpp GvsPreprocessor.cpp \
-Trackball.cpp ObjExporter.cpp SubdivisionCandidate.cpp QtGlViewer.cpp \
+Trackball.cpp ObjExporter.cpp SubdivisionCandidate.cpp \
 Mailable.cpp \
-CombinedPreprocessor.cpp
-
+CombinedPreprocessor.cpp QtGlViewer.cpp 
 
 SOURCES += BoostPreprocessorThread.cpp 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1901)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1931)
@@ -528,4 +528,5 @@
 	float contribution =
 	  mPreprocessor.mViewCellsManager->ComputeSampleContribution(*ray, true, false);
+
 	mDistributions[i]->mContribution += contribution;
 	mDistributions[i]->mRays ++;
@@ -579,6 +580,9 @@
   
   cout<<"ratios: ";
-  for (i=0; i < mDistributions.size(); i++)
-	cout<<mDistributions[i]->mRatio<<" ";
+  float last = 0.0f;
+  for (i=0; i < mDistributions.size(); i++) {
+	cout<<mDistributions[i]->mRatio-last<<" ";
+	last = mDistributions[i]->mRatio;
+  }
   cout<<endl;
 }
