Index: trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 563)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.0) on: ?t 19. I 11:57:31 2006
+# Generated by qmake (2.00a) (Qt 4.1.0) on: ?t 19. I 16:46:41 2006
 # Project:  preprocessor.pro
 # Template: app
@@ -7,7 +7,7 @@
 #############################################################################
 
-first: debug
-install: debug-install
-uninstall: debug-uninstall
+first: release
+install: release-install
+uninstall: release-uninstall
 MAKEFILE      = Makefile
 QMAKE         = qmake
@@ -27,7 +27,23 @@
 MKDIR         = mkdir
 SUBTARGETS    =  \
-		debug \
-		release
+		release \
+		debug
 
+release: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release
+release-make_default: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release 
+release-make_first: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release first
+release-all: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release all
+release-clean: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release clean
+release-distclean: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release distclean
+release-install: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release install
+release-uninstall: $(MAKEFILE).Release FORCE
+	$(MAKE) -f $(MAKEFILE).Release uninstall
 debug: $(MAKEFILE).Debug FORCE
 	$(MAKE) -f $(MAKEFILE).Debug
@@ -46,20 +62,4 @@
 debug-uninstall: $(MAKEFILE).Debug FORCE
 	$(MAKE) -f $(MAKEFILE).Debug uninstall
-release: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release
-release-make_default: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release 
-release-make_first: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release first
-release-all: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release all
-release-clean: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release clean
-release-distclean: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release distclean
-release-install: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release install
-release-uninstall: $(MAKEFILE).Release FORCE
-	$(MAKE) -f $(MAKEFILE).Release uninstall
 
 Makefile: preprocessor.pro  D:/Qt/4.1.0/mkspecs/win32-msvc.net\qmake.conf D:/Qt/4.1.0/mkspecs/qconfig.pri \
@@ -68,5 +68,5 @@
 		D:\Qt\4.1.0\mkspecs\features\default_pre.prf \
 		D:\Qt\4.1.0\mkspecs\features\win32\default_pre.prf \
-		D:\Qt\4.1.0\mkspecs\features\debug.prf \
+		D:\Qt\4.1.0\mkspecs\features\release.prf \
 		D:\Qt\4.1.0\mkspecs\features\debug_and_release.prf \
 		D:\Qt\4.1.0\mkspecs\features\default_post.prf \
@@ -89,5 +89,5 @@
 D:\Qt\4.1.0\mkspecs\features\default_pre.prf:
 D:\Qt\4.1.0\mkspecs\features\win32\default_pre.prf:
-D:\Qt\4.1.0\mkspecs\features\debug.prf:
+D:\Qt\4.1.0\mkspecs\features\release.prf:
 D:\Qt\4.1.0\mkspecs\features\debug_and_release.prf:
 D:\Qt\4.1.0\mkspecs\features\default_post.prf:
@@ -109,29 +109,25 @@
 qmake_all: FORCE
 
-make_default: debug-make_default release-make_default FORCE
-make_first: debug-make_first release-make_first FORCE
-all: debug-all release-all FORCE
-clean: debug-clean release-clean FORCE
+make_default: release-make_default debug-make_default FORCE
+make_first: release-make_first debug-make_first FORCE
+all: release-all debug-all FORCE
+clean: release-clean debug-clean FORCE
 	-$(DEL_FILE) preprocessor.exp
-	-$(DEL_FILE) preprocessor.pdb
-	-$(DEL_FILE) preprocessor.ilk
-	-$(DEL_FILE) vc*.pdb
-	-$(DEL_FILE) vc*.idb
-distclean: debug-distclean release-distclean FORCE
+distclean: release-distclean debug-distclean FORCE
 	-$(DEL_FILE) Makefile
 
+release-mocclean: $(MAKEFILE).Release
+	$(MAKE) -f $(MAKEFILE).Release mocclean
 debug-mocclean: $(MAKEFILE).Debug
 	$(MAKE) -f $(MAKEFILE).Debug mocclean
-release-mocclean: $(MAKEFILE).Release
-	$(MAKE) -f $(MAKEFILE).Release mocclean
-mocclean: debug-mocclean release-mocclean
+mocclean: release-mocclean debug-mocclean
 
+release-mocables: $(MAKEFILE).Release
+	$(MAKE) -f $(MAKEFILE).Release mocables
 debug-mocables: $(MAKEFILE).Debug
 	$(MAKE) -f $(MAKEFILE).Debug mocables
-release-mocables: $(MAKEFILE).Release
-	$(MAKE) -f $(MAKEFILE).Release mocables
-mocables: debug-mocables release-mocables
+mocables: release-mocables debug-mocables
 FORCE:
 
+$(MAKEFILE).Release: Makefile
 $(MAKEFILE).Debug: Makefile
-$(MAKEFILE).Release: Makefile
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 562)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 563)
@@ -68,11 +68,12 @@
 		@param rays bundle of rays used to find intersections with view cells and 
 		adding the contribution
-		@param castRays true if ray should be cast to gain the information, false if rays
-		already hold the view cells intersection information and need not be recast.
-		@param sampleContributions returns the number of sample contributions
-		@param contributingSamples returns the number of contributingSamples
-	*/
-	void  ComputeSampleContributions(const VssRayContainer &rays);
-
+		@param addRays true if rays should be added to the PVSs of the viewcells they
+		intersect */
+  float ComputeSampleContributions(const VssRayContainer &rays,
+								   const bool addContributions = true);
+
+  /** Add sample contributions to the viewcells they intersect */
+  void AddSampleContributions(const VssRayContainer &rays);
+  
 
 	/** Computes sample contribution of a simgle ray to the view cells PVS.
@@ -83,6 +84,8 @@
 		@returns number of sample contributions
 	*/
-	virtual void ComputeSampleContributions(VssRay &ray);
-  
+	virtual float ComputeSampleContributions(VssRay &ray, const bool addRays = true);
+
+  virtual void AddSampleContributions(VssRay &ray);
+
 	/** Prints out statistics of the view cells.
 	*/
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 563)
@@ -325,5 +325,9 @@
 	  (*it).mPdf*=c;
   }
-  
+
+  void AddRay(const SimpleRay &ray) {
+	push_back(ray);
+	mSumPdf += ray.mPdf;
+  }
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 547)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 563)
@@ -1434,4 +1434,9 @@
 					"false");
 
+	RegisterOption("Preprocessor.pvsRenderErrorSamples",
+				   optInt,
+				   "pvsRenderErrorSamples=",
+				   "10000");
+
 	RegisterOption("Preprocessor.useGlDebugger",
 					optBool,
@@ -1675,5 +1680,8 @@
 	RegisterOption("RssPreprocessor.Export.numRays", optInt, "rss_export_num_rays=", "5000");
 	RegisterOption("RssPreprocessor.useViewcells", optBool, "rss_use_viewcells", "false");
-	RegisterOption("RssPreprocessor.updateSubdivision", optBool, "rss_update_subdivision", "false");
+	RegisterOption("RssPreprocessor.updateSubdivision",
+				   optBool,
+				   "rss_update_subdivision",
+				   "false");
 
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 563)
@@ -33,19 +33,19 @@
 Preprocessor::~Preprocessor()
 {
-  cerr<<"Deleting view cells manager...\n";
+  Debug<<"Deleting view cells manager...\n";
   DEL_PTR(mViewCellsManager);
-  cerr<<"done.\n";
-  cerr<<"Deleting bsp tree...\n";
+  Debug<<"done.\n";
+  Debug<<"Deleting bsp tree...\n";
   DEL_PTR(mBspTree);
-  cerr<<"done.\n";
-  cerr<<"Deleting kd tree...\n";
+  Debug<<"done.\n";
+  Debug<<"Deleting kd tree...\n";
   DEL_PTR(mKdTree);
-  cerr<<"done.\n";
-  cerr<<"Deleting vspkd tree...\n";
+  Debug<<"done.\n";
+  Debug<<"Deleting vspkd tree...\n";
   DEL_PTR(mVspKdTree);
-  cerr<<"done.\n";
-  cerr<<"Deleting vspbsp tree...\n";
+  Debug<<"done.\n";
+  Debug<<"Deleting vspbsp tree...\n";
   DEL_PTR(mVspBspTree);
-  cerr<<"done.\n";
+  Debug<<"done.\n";
 }
 
@@ -429,2 +429,55 @@
 	return true;
 }
+
+
+
+bool
+Preprocessor::GenerateRays(
+						   const int number,
+						   const int sampleType,
+						   SimpleRayContainer &rays
+						   )
+{
+  Vector3 origin, direction; 
+
+  for (int i=0; rays.size() < number; i++) {
+	mViewCellsManager->GetViewPoint(origin);
+	
+	
+	// now get the direction
+	switch (sampleType) {
+	case OBJECT_BASED_DISTRIBUTION: {
+	  Vector3 point;
+	  Vector3 normal;
+	  int i = RandomValue(0, mObjects.size() - 1);
+	  Intersectable *object = mObjects[i];
+	  object->GetRandomSurfacePoint(point, normal);
+	  direction = point - origin;
+	}
+	  break;
+	case DIRECTION_BASED_DISTRIBUTION:
+	  direction = UniformRandomVector();
+	  break;
+	case DIRECTION_BOX_BASED_DISTRIBUTION: {
+	  float alpha = RandomValue(0.0f, 2*M_PI);
+	  float beta = RandomValue(-M_PI/2, M_PI/2);
+	  direction = VssRay::GetDirection(alpha, beta);
+	  break;
+	}
+	case SPATIAL_BOX_BASED_DISTRIBUTION:
+	  direction = mKdTree->GetBox().GetRandomPoint() - origin;
+	  break;
+	default:
+	  // unsuported distribution type
+	  return false;
+	}
+	// $$ jb the pdf is yet not correct for all sampling methods!
+	float pdf = 1.0f;
+	float c = Magnitude(direction);
+	if (c > Limits::Small) {
+	  direction*=1.0f/c;
+	  rays.AddRay(SimpleRay(origin, direction, pdf));
+	}
+  }
+  return true;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 563)
@@ -2097,5 +2097,5 @@
   
   float extendedConvexCombinationProb = 0.0f; //0.7f
-  float silhouetteCheckPercentage = 0.5f; //0.5f
+  float silhouetteCheckPercentage = 0.0f; //0.5f
   for (int i=0; generated < numberOfRays && i < numberOfTries; i++) {
 	bool useExtendedConvexCombination = ((nrays >= 2) && (Random(1.0f) <
@@ -2204,5 +2204,6 @@
   //  cout<<"desired="<<numberOfRays<<" tries="<<i<<endl;
   // assign pdfs to the generated rays
-  float p = (box.GetVolume()*dirBox.GetVolume())/i;
+  float p = 1.0f; //(box.GetVolume()*dirBox.GetVolume())/i;
+  rays.mSumPdf = 0.0f;
   for (i=startIndex; i < rays.size(); i++) {
 	rays[i].mPdf = p;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 563)
@@ -42,8 +42,46 @@
   environment->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
   environment->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
+  environment->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
   
   mStats.open("stats.log");
-
-}
+  mRssTree = NULL;
+}
+
+
+bool
+RssPreprocessor::GenerateRays(
+							  const int number,
+							  const int sampleType,
+							  SimpleRayContainer &rays
+							  )
+{
+  bool result = false;
+
+  switch (sampleType) {
+  case RSS_BASED_DISTRIBUTION:
+  case RSS_SILHOUETTE_BASED_DISTRIBUTION:
+	if (mRssTree) {
+	  GenerateImportanceRays(mRssTree, number, rays);
+	  result = true;
+	  rays.NormalizePdf();
+	}
+	break;
+  default:
+	result = Preprocessor::GenerateRays(number, sampleType, rays);
+  }
+
+  return result;
+}
+
+void
+RssPreprocessor::CastRays(
+						  SimpleRayContainer &rays,
+						  VssRayContainer &vssRays
+						  )
+{
+  for (int i=0; i < rays.size(); i++)
+	CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mPdf, vssRays);
+}
+
 
 RssPreprocessor::~RssPreprocessor()
@@ -190,89 +228,4 @@
 }
 
-Vector3
-RssPreprocessor::GetDirection(const Vector3 &viewpoint,
-							  AxisAlignedBox3 *viewSpaceBox
-							  )
-{
-  Vector3 point;
-  if (!use2dSampling) {
-	if (mObjectBasedSampling) {
-	  Vector3 normal;
-	  int i = RandomValue(0, mObjects.size()-1);
-	  Intersectable *object = mObjects[i];
-	  object->GetRandomSurfacePoint(point, normal);
-	} else {
-	  // select
-	  if (mDirectionalSampling) {
-		point = viewpoint + UniformRandomVector();
-	  } else {
-		// select the other point uniformly distruted in the whole viewspace
-		AxisAlignedBox3 box;
-		
-		if (viewSpaceBox)
-		  box =*viewSpaceBox;
-		else 
-		  box = mKdTree->GetBox();
-		
-		point = box.GetRandomPoint();
-	  }
-	}
-  } else {
-	AxisAlignedBox3 box;
-		
-	if (viewSpaceBox)
-	  box =*viewSpaceBox;
-	else 
-	  box = mKdTree->GetBox();
-	
-	point = box.GetRandomPoint();
-	point.y = viewpoint.y;
-  }
-	
-  return point - viewpoint;
-}
-
-Vector3
-RssPreprocessor::InitialGetDirection(const Vector3 &viewpoint,
-									 AxisAlignedBox3 *viewSpaceBox
-									 )
-{
-  Vector3 point;
-  if (!use2dSampling) {
-	if (1 || mObjectBasedSampling) {
-	  Vector3 normal;
-	  int i = RandomValue(0, mObjects.size()-1);
-	  Intersectable *object = mObjects[i];
-	  object->GetRandomSurfacePoint(point, normal);
-	} else {
-	  // select
-	  if (1 || mDirectionalSampling) {
-		point = viewpoint + UniformRandomVector();
-	  } else {
-		// select the other point uniformly distruted in the whole viewspace
-		AxisAlignedBox3 box;
-		
-		if (viewSpaceBox)
-		  box =*viewSpaceBox;
-		else 
-		  box = mKdTree->GetBox();
-		
-		point = box.GetRandomPoint();
-	  }
-	}
-  } else {
-	AxisAlignedBox3 box;
-	
-	if (viewSpaceBox)
-	  box =*viewSpaceBox;
-	else 
-	  box = mKdTree->GetBox();
-	
-	point = box.GetRandomPoint();
-	point.y = viewpoint.y;
-  }
-  
-  return point - viewpoint;
-}
 
 int
@@ -302,5 +255,4 @@
   }
 	
-  cout<<"Generated "<<num<<" rays."<<endl;
 	
   return num;
@@ -322,5 +274,5 @@
   exporter->SetFilled();
   // $$JB temporarily do not export the scene
-  if (1)
+  if (0)
 	exporter->ExportScene(mSceneGraph->mRoot);
   exporter->SetWireframe();
@@ -474,11 +426,10 @@
 }
 
+
+
 bool
 RssPreprocessor::ComputeVisibility()
 {
 
-  if (renderer)
-	renderer->InitGL();
-  
   //  connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) );
   
@@ -493,58 +444,17 @@
   int totalSamples = 0;
 
-  AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
-
-  if (fromBoxVisibility) {
-	float m = box->Min(1);
-	float bsize = box->Size(1);
-
-	float size = 0.02f;
-	float s = 0.5f - size;
-	float olds = Magnitude(box->Size());
-	box->Enlarge(box->Size()*Vector3(-s));
-	//	Vector3 translation = Vector3(-olds*0.2f, 0, 0);
-	Vector3 translation = Vector3(-0.05f*olds, 0, 0);
-	box->SetMin(box->Min() + translation);
-	box->SetMax(box->Max() + translation);
-
-	box->SetMin(1,  m + bsize*0.1f);
-	box->SetMax(1,  m + bsize*0.6f);
-
-	
-  } else {
-		
-	// sample city like heights
-	float m = box->Min(1);
-	float bsize = box->Size(1);
-	box->SetMin(1,  m + bsize*0.2f);
-	box->SetMax(1,  m + bsize*0.3f);
-  }
-
-  if (use2dSampling)
-	box->SetMax(1, box->Min(1));
-	
-  if (useViewSpaceBox)
-  {
-	mViewSpaceBox = box;
-	mViewCellsManager->SetViewSpaceBox(*box);
-  }
-  else
-  {
-	mViewSpaceBox = NULL;
-	mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
-  }
+  mViewSpaceBox = NULL;
+  mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
+  
   
   //-- load view cells from file if requested
-  if (mLoadViewCells)
-  {	
-	  // load now because otherwise bounding box not correct
-	  mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects);
-  }
-
-  RssTree *rssTree = NULL;
+  if (mLoadViewCells) {	
+	// load now because otherwise bounding box not correct
+	mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects);
+  }
+  
   int rssPass = 0;
   int rssSamples = 0;
-
-#if 0
+  
   if (mLoadInitialSamples) {
 	cout << "Loading samples from file ... ";
@@ -552,175 +462,38 @@
 	cout << "finished\n" << endl;
   } else {
-#endif
-	while (totalSamples < mInitialSamples) {
-	  int passContributingSamples = 0;
-	  int passSampleContributions = 0;
-	  int passSamples = 0;
-	  int index = 0;
-	  
-	  int sampleContributions;
-	  
-	  //int s = Min(mSamplesPerPass, mInitialSamples);
-	  int s = mInitialSamples;
-
-	  float probability = 1.0f/mInitialSamples;
-	  for (int k=0; k < s; k++) {
-		
-		
-		//Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
-		Vector3 viewpoint; 
-		mViewCellsManager->GetViewPoint(viewpoint);
-		//		viewpoint = GetViewpoint(mViewSpaceBox);
-		Vector3 direction = InitialGetDirection(viewpoint, mViewSpaceBox);
-		
-		sampleContributions = CastRay(viewpoint, direction, probability, mVssRays);
-		
-		
-		//-- CORR matt: put block inside loop
-		if (sampleContributions) {
-		  passContributingSamples ++;
-		  passSampleContributions += sampleContributions;
-		}
-		passSamples++;
-		totalSamples++;
-	  }
-	  
-	  
-	  float avgRayContrib = (passContributingSamples > 0) ? 
-		passSampleContributions/(float)passContributingSamples : 0;
-	  
-	  cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
-	  cout << "#TotalSamples=" << totalSamples/1000 
-		   << "k   #SampleContributions=" << passSampleContributions << " (" 
-		   << 100*passContributingSamples/(float)passSamples<<"%)" 
-		   << "avgcontrib=" << avgRayContrib << endl;
-	  
-	  mPass++;
-	}
-	  
-	cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
-	cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
-	
-	rssSamples += (int)mVssRays.size();
-	
-	
-	if (mExportRays) {
-	  char filename[64];
-	  sprintf(filename, "rss-rays-initial.x3d");
-	  ExportRays(filename, mVssRays, mExportNumRays);
-	}
-	
-	if (mStoreInitialSamples)
-	  {
-		cout << "Writing samples to file ... ";
-		ExportSamples(mVssRays);
-		cout << "finished\n" << endl;
-	  }
-	
-	if (mUseViewcells) {
-	  // construct view cells
-	  if (!mLoadViewCells)
-		mViewCellsManager->Construct(mObjects, mVssRays);
-	  // evaluate contributions of the intitial rays
-	  mViewCellsManager->ComputeSampleContributions(mVssRays);
-	  
-	  
-	  mStats <<
-		"#Pass\n" <<mPass<<endl<<
-		"#RssPass\n" <<rssPass<<endl<<
-		"#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
-		"#TotalSamples\n" <<totalSamples<<endl<<
-		"#RssSamples\n" <<rssSamples<<endl;
-	  
-
-	  mVssRays.PrintStatistics(mStats);
-	  mViewCellsManager->PrintPvsStatistics(mStats);
-	  
-	  VssRayContainer selectedRays;
-	  int desired = Max(
-						mViewCellsManager->GetPostProcessSamples(),
-						mViewCellsManager->GetVisualizationSamples());
-	  
-	  mVssRays.SelectRays(desired, selectedRays);
-	  
-	  //-- post process view cells
-	  mViewCellsManager->PostProcess(mObjects, selectedRays);
-	  
-	  //-- several visualizations and statistics
-	  Debug << "view cells after post processing: " << endl;
-	  mViewCellsManager->PrintStatistics(Debug);
-	  
-	  if (1) 
-		mViewCellsManager->Visualize(mObjects, selectedRays);
-
-	  ComputeRenderError();
-	  
-	}
-
-
-  
-  rssPass++;
-  
-  rssTree = new RssTree;
-  rssTree->SetPass(mPass);
-  
-  /// compute view cell contribution of rays if view cells manager already constructed
-  mViewCellsManager->ComputeSampleContributions(mVssRays);
-
-  if (mUseImportanceSampling) {
-	
-	//	if (fromBoxVisibility)
-	//	  rssTree->Construct(mObjects, mVssRays, mViewSpaceBox);
-	//	else
-	rssTree->Construct(mObjects, mVssRays);
-
-	rssTree->stat.Print(mStats);
-	cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
-	
-	if (mExportRssTree) {
-	  ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
-	  ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
-	  ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));
-	  ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));
-	  ExportRssTreeLeaves(rssTree, 10);
-	}
-	
-	if (mExportPvs) {
-	  ExportPvs("rss-pvs-initial.x3d", rssTree);
-	}
-  }
-  
-  // viewcells->UpdatePVS(newVssRays);
-  
-
-  while (1) {
-	int num = mRssSamplesPerPass;
 	SimpleRayContainer rays;
-	VssRayContainer vssRays;
-
-
-	if (!mUseImportanceSampling) {
-	  for (int j=0; j < num; j++) {
-	    //changed by matt
-		//Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 
-		Vector3 viewpoint; 
-		mViewCellsManager->GetViewPoint(viewpoint);
-		Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
-		rays.push_back(SimpleRay(viewpoint, direction, 1.0f));
-		rays.mSumPdf += 1.0f;
-	  }
-	} else {
-	  num = GenerateImportanceRays(rssTree, num, rays);
-	}
-
-	rays.NormalizePdf();
-	
-	for (int i=0; i < rays.size(); i++)
-	  CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mPdf, vssRays);
-
-	
-	totalSamples += (int)rays.size();
-	rssSamples += (int)vssRays.size();
-
+
+	GenerateRays(mInitialSamples, SPATIAL_BOX_BASED_DISTRIBUTION, rays);
+	
+	CastRays(rays, mVssRays);
+  }
+  
+  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
+  
+  rssSamples += (int)mVssRays.size();
+  totalSamples += mInitialSamples;
+  mPass++;
+  
+  if (mExportRays) {
+	char filename[64];
+	sprintf(filename, "rss-rays-initial.x3d");
+	ExportRays(filename, mVssRays, mExportNumRays);
+  }
+  
+  if (mStoreInitialSamples) {
+	cout << "Writing samples to file ... ";
+	ExportSamples(mVssRays);
+	cout << "finished\n" << endl;
+  }
+	
+  if (mUseViewcells) {
+	// construct view cells
+	if (!mLoadViewCells)
+	  mViewCellsManager->Construct(mObjects, mVssRays);
+
+	
+
+	// evaluate contributions of the intitial rays
+	mViewCellsManager->ComputeSampleContributions(mVssRays);
 	
 	
@@ -731,11 +504,138 @@
 	  "#TotalSamples\n" <<totalSamples<<endl<<
 	  "#RssSamples\n" <<rssSamples<<endl;
+	
+
+	mVssRays.PrintStatistics(mStats);
+	mViewCellsManager->PrintPvsStatistics(mStats);
+	  
+	VssRayContainer selectedRays;
+	int desired = Max(
+					  mViewCellsManager->GetPostProcessSamples(),
+					  mViewCellsManager->GetVisualizationSamples());
+	
+	mVssRays.SelectRays(desired, selectedRays);
+	  
+	//-- post process view cells
+	mViewCellsManager->PostProcess(mObjects, selectedRays);
+	  
+	//-- several visualizations and statistics
+	Debug << "view cells after post processing: " << endl;
+	mViewCellsManager->PrintStatistics(Debug);
+	
+	if (1) 
+	  mViewCellsManager->Visualize(mObjects, selectedRays);
+	
+	ComputeRenderError();
+  }
+  
+  rssPass++;
+  
+  mRssTree = new RssTree;
+  mRssTree->SetPass(mPass);
+  
+  /// compute view cell contribution of rays if view cells manager already constructed
+  mViewCellsManager->ComputeSampleContributions(mVssRays);
+
+  if (mUseImportanceSampling) {
+	
+	mRssTree->Construct(mObjects, mVssRays);
+	
+	mRssTree->stat.Print(mStats);
+	cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
+	
+	if (mExportRssTree) {
+	  ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
+	  ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
+	  ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
+	  ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
+	  ExportRssTreeLeaves(mRssTree, 10);
+	}
+	
+	if (mExportPvs) {
+	  ExportPvs("rss-pvs-initial.x3d", mRssTree);
+	}
+  }
+  
+  // viewcells->UpdatePVS(newVssRays);
+  
+
+  while (1) {
+	SimpleRayContainer rays;
+	VssRayContainer vssRays;
+	int castRays = 0;
+	if (mUseImportanceSampling) {
+	  VssRayContainer tmpVssRays;
+	  
+	  GenerateRays(mRssSamplesPerPass/2, RSS_BASED_DISTRIBUTION, rays);
+	  CastRays(rays, tmpVssRays);
+	  castRays += rays.size();
+	  float c1 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false);
+	  mStats<<"#RssRelContrib"<<endl<<c1/rays.size()<<endl;
+
+	  vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
+	  rays.clear();
+	  tmpVssRays.clear();
+	  
+	  GenerateRays(mRssSamplesPerPass/4, SPATIAL_BOX_BASED_DISTRIBUTION, rays);
+	  CastRays(rays, tmpVssRays);
+	  castRays += rays.size();
+	  float c2 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false);
+	  mStats<<"#SpatialRelContrib"<<endl<<c2/rays.size()<<endl;
+
+	  vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
+	  
+	  rays.clear();
+	  tmpVssRays.clear();
+	  
+	  
+	  GenerateRays(mRssSamplesPerPass/4, DIRECTION_BASED_DISTRIBUTION, rays);
+	  CastRays(rays, tmpVssRays);
+	  castRays += rays.size();
+	  float c3 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false);
+	  mStats<<"#DirectionalRelContrib"<<endl<<c3/rays.size()<<endl;
+	  
+	  vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
+	  
+	  rays.clear();
+	  tmpVssRays.clear();
+	  
+	  // add contributions of all rays at once...
+	  mViewCellsManager->AddSampleContributions(vssRays);
+	  
+	}
+	else {
+	  int rayType = SPATIAL_BOX_BASED_DISTRIBUTION;
+	  if (mObjectBasedSampling)
+		rayType = OBJECT_BASED_DISTRIBUTION;
+	  else
+		if (mDirectionalSampling)
+		  rayType = DIRECTION_BASED_DISTRIBUTION;
+	  
+	  GenerateRays(mRssSamplesPerPass, rayType, rays);
+	  CastRays(rays, vssRays);
+	  castRays += rays.size();
+	  if (mUseViewcells) {
+		/// compute view cell contribution of rays
+		mViewCellsManager->ComputeSampleContributions(vssRays);
+	  }
+	  	
+	  
+	}
+	totalSamples += castRays;
+	rssSamples += (int)vssRays.size();
+
+	cout<<"Generated "<<castRays<<" rays, progress "<<totalSamples/((float) mRssSamples +
+																	mInitialSamples)<<"%\n";
+	
+	
+	mStats <<
+	  "#Pass\n" <<mPass<<endl<<
+	  "#RssPass\n" <<rssPass<<endl<<
+	  "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
+	  "#TotalSamples\n" <<totalSamples<<endl<<
+	  "#RssSamples\n" <<rssSamples<<endl;
 
 
 	if (mUseViewcells) {
-	  
-	  /// compute view cell contribution of rays
-	  mViewCellsManager->ComputeSampleContributions(vssRays);
-	  
 	  vssRays.PrintStatistics(mStats);
 	  mViewCellsManager->PrintPvsStatistics(mStats);
@@ -770,34 +670,26 @@
 	
 	if (mUseImportanceSampling) {
-	  rssTree->AddRays(vssRays);
-	  
-	  if (0) {
-		cout<<"############# Rss PVS STAT ##################\n";
-		cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl;
-		cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl;
-	  }
-	  
+	  mRssTree->AddRays(vssRays);
 	  
 	  if (mUpdateSubdivision) {
 		int updatePasses = 1;
 		if (mPass % updatePasses == 0) {
-		  int subdivided = rssTree->UpdateSubdivision();
+		  int subdivided = mRssTree->UpdateSubdivision();
 		  cout<<"subdivided leafs = "<<subdivided<<endl;
-		  cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
+		  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
 		}
 	  }
 	}
-	
-
 	
 	if (mExportPvs) {
 	  char filename[64];
 	  sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
-	  ExportPvs(filename, rssTree);
-	}
-
+	  ExportPvs(filename, mRssTree);
+	}
+	
 	
 	if (!mUseImportanceSampling) 
 	  CLEAR_CONTAINER(vssRays);
+	// otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
 
 	if (totalSamples >= mRssSamples + mInitialSamples)
@@ -807,5 +699,5 @@
 	rssPass++;
 	mPass++;
-	rssTree->SetPass(mPass);
+	mRssTree->SetPass(mPass);
   }
   
@@ -825,8 +717,8 @@
 	
   }
-
-  cerr<<"Deleting RSS tree...\n";
-  delete rssTree;
-  cerr<<"Done.\n";
+  
+  Debug<<"Deleting RSS tree...\n";
+  delete mRssTree;
+  Debug<<"Done.\n";
 
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 563)
@@ -25,6 +25,9 @@
 #	type vss
 	type rss
-	detectEmptyViewSpace true
-}
+	detectEmptyViewSpace false
+	pvsRenderErrorSamples 10000
+
+}
+
 
 VssPreprocessor {
@@ -53,7 +56,7 @@
 	maxStaticMemory	100
 
-	splitType regular
+#	splitType regular
 #	splitType heuristic
-#	splitType hybrid
+	splitType hybrid
 	splitUseOnlyDrivingAxis true
 
@@ -69,8 +72,8 @@
 
 RssPreprocessor {
-	samplesPerPass  100000
-	initialSamples 100000
-	vssSamples 10000
-	vssSamplesPerPass 100000
+	samplesPerPass 500000
+	initialSamples 1000000
+	vssSamples 15000000
+	vssSamplesPerPass 1000000
 	useImportanceSampling true
 
@@ -87,5 +90,5 @@
 	useViewcells true
 	updateSubdivision true
-	loadInitialSamples true
+	loadInitialSamples false
 	storeInitialSamples false
 }
@@ -102,12 +105,12 @@
  	maxCostRatio 1.0
 	maxRayContribution 0.5
-	maxRays		1000000
+	maxRays		5000000
 	maxTotalMemory	200
 	maxStaticMemory	100
 
-	splitType regular
+#	splitType regular
 #	splitType heuristic
-#	splitType hybrid
-	splitUseOnlyDrivingAxis true
+	splitType hybrid
+	splitUseOnlyDrivingAxis false
 	importanceBasedCost false
 
@@ -166,9 +169,11 @@
 }
 
+
 ViewCells {
 	loadFromFile true
+	exportToFile false
 	#type kdTree
 	#type vspKdTree
-	# type bspTree
+	#type bspTree
 	type vspBspTree
 	
@@ -176,17 +181,17 @@
 	
 	height 5.0
-	maxViewCells 500
-	maxPvs 200
-	
-	
-PostProcess {
-		minPvsDif 100
-		minPvs 10
-		maxPvs 150
+	maxViewCells 100000
+	#percentage of total visible objects where pvs is considered invalid
+	maxPvsRatio 0.05
+		
+	delayedConstruction true
+		
+		
+	PostProcess {
 		# how much samples are used for post processing
-		samples 100000
-	}
-
-		Visualization {
+		samples 200000
+	}
+
+	Visualization {
 		# how much samples we use for visualization
 		samples 5000
@@ -196,13 +201,16 @@
 		colorCode Random
 		exportRays false
-		exportGeometry true
-	}
-	
-	filename ../data/soda/viewcells_soda5-2.xml
-#	filename ../data/atlanta/viewcells_atlanta2.xml
+		exportGeometry false
+	}
+	
+#	filename ../data/soda/viewcells_soda5-2.xml
+	filename ../data/atlanta/viewcells_atlanta2.xml
 #	filename ../data/atlanta/atlanta_viewcells_large.x3d
 #	filename ../data/vienna/viewcells-25-sel.x3d
 #	filename ../data/vienna/viewcells-25.x3d
 #	filename ../data/vienna/viewcells-large-sel.x3d
+#	filename ../scripts/viewcells_vienna.xml
+
+
 }
 
@@ -252,4 +260,5 @@
 	}
 }
+
 
 VspBspTree {
@@ -309,9 +318,3 @@
 	}
 	
-	PostProcess {
-		maxCostRatio 0.1
-		minViewCells 500
-		maxPvsSize   1000
-		useRaysForMerge true
-	}
-}
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 538)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 563)
@@ -98,4 +98,24 @@
   bool ExportSamples(const VssRayContainer &samples) const;
 
+  /** Get Sample rays of particular type, returns false if this
+	  type of rays is not supported by the preprocessor
+  */
+  enum {
+	OBJECT_BASED_DISTRIBUTION,
+	DIRECTION_BASED_DISTRIBUTION,
+	DIRECTION_BOX_BASED_DISTRIBUTION,
+	SPATIAL_BOX_BASED_DISTRIBUTION,
+	RSS_BASED_DISTRIBUTION,
+	RSS_SILHOUETTE_BASED_DISTRIBUTION,
+	VSS_BASED_DISTRIBUTION
+  };
+  
+  virtual bool
+  GenerateRays(
+			   const int number,
+			   const int raysType,
+			   SimpleRayContainer &rays
+			   );
+  
   /// scene graph loaded from file
   SceneGraph *mSceneGraph;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 563)
@@ -90,5 +90,4 @@
   void SetupMaterial(Material *m);
   virtual void SetupCamera();
-  void RandomViewPoint();
   
   bool
@@ -119,16 +118,6 @@
 					 SceneGraph *sceneGraph,
 					 ViewCellsManager *viewcells,
-					 KdTree *tree):
-	QGLPixelBuffer(QSize(w, h)), GlRenderer(sceneGraph, viewcells, tree) {
-
-	mPvsStatFrames = 5000;
-	mPvsErrorBuffer.resize(mPvsStatFrames);
-	ClearErrorBuffer();
-
-// 	makeCurrent();
-// 	InitGL();
-// 	doneCurrent();
-
-	}
+					 KdTree *tree);
+
   
   void
@@ -143,4 +132,5 @@
 
 
+  void RandomViewPoint();
   void SampleBeamContributions(
 							   Intersectable *sourceObject,
Index: trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro	(revision 556)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro	(revision 563)
@@ -17,5 +17,5 @@
 
 # debuc config
-CONFIG		+= console warn_off thread debug qt
+CONFIG		+= console warn_off thread release qt
 
 # RELEASE CONFIG
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 563)
@@ -67,5 +67,5 @@
   }
   
-  mViewCellsManager->ComputeSampleContributions(vssRays);
+  mViewCellsManager->ComputeSampleContributions(vssRays, true);
   CLEAR_CONTAINER(vssRays);
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.h	(revision 537)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.h	(revision 563)
@@ -38,5 +38,5 @@
 
   ofstream mStats;
-
+  RssTree *mRssTree;
 
   // rays cast during the processing
@@ -56,13 +56,4 @@
   GetViewpoint(AxisAlignedBox3 *viewSpaceBox);
 
-  Vector3
-  GetDirection(const Vector3 &viewpoint,
-			   AxisAlignedBox3 *viewSpaceBox
-			   );
-
-  Vector3
-  InitialGetDirection(const Vector3 &viewpoint,
-					  AxisAlignedBox3 *viewSpaceBox
-					  );
 
   void
@@ -86,5 +77,10 @@
 	
   virtual bool BuildBspTree() { return false; }
-
+  
+  void
+  CastRays(
+		   SimpleRayContainer &rays,
+		   VssRayContainer &vssRays
+		   );
 
   bool
@@ -132,4 +128,12 @@
   ComputeRenderError();
 
+  /** Redefininition of the get sample rays method from the preprocessor */
+  bool
+  GenerateRays(
+			   const int number,
+			   const int sampleType,
+			   SimpleRayContainer &rays
+			   );
+
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 562)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 563)
@@ -171,16 +171,20 @@
 
 
-void ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays)
-{
-	// view cells not yet constructed
-	if (!ViewCellsConstructed())
-		return;
-
-	VssRayContainer::const_iterator it, it_end = rays.end();
-
-	for (it = rays.begin(); it != it_end; ++ it)
-	{
-		ComputeSampleContributions(*(*it));
-	}
+float
+ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
+											 const bool addRays
+											 )
+{
+  // view cells not yet constructed
+  if (!ViewCellsConstructed())
+	return 0.0f;
+  
+  VssRayContainer::const_iterator it, it_end = rays.end();
+
+  float sum = 0.0f;
+  for (it = rays.begin(); it != it_end; ++ it) {
+	sum += ComputeSampleContributions(*(*it), addRays);
+  }
+  return sum;
 }
 
@@ -440,4 +444,16 @@
 }
 
+void
+ViewCellsManager::AddSampleContributions(const VssRayContainer &rays)
+{
+  if (!ViewCellsConstructed())
+	return;
+  
+  VssRayContainer::const_iterator it, it_end = rays.end();
+  
+  for (it = rays.begin(); it != it_end; ++ it) {
+	AddSampleContributions(*(*it));
+  }
+}
 
 int ViewCellsManager::GetMinPvsSize() const
@@ -454,76 +470,76 @@
 
 
-void ViewCellsManager::ComputeSampleContributions(VssRay &ray)
-{
-	ViewCellContainer viewcells;
-
-	ray.mPvsContribution = 0;
-	ray.mRelativePvsContribution = 0.0f;
-
-	// matt TODO: remove this!!
-	Ray hray(ray);
-	//static Ray hray;
-	//hray.Init(ray.GetOrigin(), ray.GetDir(), Ray::LINE_SEGMENT);
-	
-	float tmin = 0, tmax = 1.0;
-
-	if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
-		return;
-
-	Vector3 origin = hray.Extrap(tmin);
-	Vector3 termination = hray.Extrap(tmax);
-
-	CastLineSegment(origin, termination, viewcells);
-	//Debug << "constribution: " << (int)viewcells.size() << endl;
-	// copy viewcells memory efficiently
-	const bool storeViewcells = false;
-	if (storeViewcells)
-	{
-		ray.mViewCells.reserve(viewcells.size());
-		ray.mViewCells = viewcells;
-	}
-
-	ViewCellContainer::const_iterator it = viewcells.begin();
-
-	bool addInPlace = false;
-
-	if (addInPlace) 
-	{
-		for (; it != viewcells.end(); ++it) {
-			ViewCell *viewcell = *it;
-
-			// if ray not outside of view space
-			float contribution;
-			bool added =
-			viewcell->GetPvs().AddSample(ray.mTerminationObject,
-										 ray.mPdf,
-										 contribution
-										 );
-			if (added)
-			ray.mPvsContribution++;
-
-			ray.mRelativePvsContribution += contribution;
-		}
-	} 
-	else 
-	{
-		for (; it != viewcells.end(); ++it) {
-			ViewCell *viewcell = *it;
-			// if ray not outside of view space
-			float contribution;
-			if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
-														 ray.mPdf,
-														 contribution
-														 ))
-			  ray.mPvsContribution++;
-			ray.mRelativePvsContribution += contribution;
-		}
-		
-		for (it = viewcells.begin(); it != viewcells.end(); ++it) {
-		  ViewCell *viewcell = *it;
-		  // if ray not outside of view space
-		  viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
-		}
-	}
+void
+ViewCellsManager::AddSampleContributions(VssRay &ray)
+{
+  // assumes viewcells have been stored...
+  ViewCellContainer *viewcells = &ray.mViewCells;
+  ViewCellContainer::const_iterator it;
+  for (it = viewcells->begin(); it != viewcells->end(); ++it) {
+	ViewCell *viewcell = *it;
+	// if ray not outside of view space
+	viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
+  }
+}
+
+float
+ViewCellsManager::ComputeSampleContributions(VssRay &ray,
+											 const bool addRays
+											 )
+{
+  ViewCellContainer viewcells;
+  
+  ray.mPvsContribution = 0;
+  ray.mRelativePvsContribution = 0.0f;
+  
+  // matt TODO: remove this!!
+  Ray hray(ray);
+  float tmin = 0, tmax = 1.0;
+  
+  //hray.Init(ray.GetOrigin(), ray.GetDir(), Ray::LINE_SEGMENT);
+  if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
+	return 0.0f;
+  
+  Vector3 origin = hray.Extrap(tmin);
+  Vector3 termination = hray.Extrap(tmax);
+
+  CastLineSegment(origin,
+				  termination,
+				  viewcells);
+  
+  //Debug << "constribution: " << (int)viewcells.size() << endl;
+  
+  // copy viewcells memory efficiently
+  const bool storeViewcells = !addRays;
+  
+  if (storeViewcells)
+	{
+	  ray.mViewCells.reserve(viewcells.size());
+	  ray.mViewCells = viewcells;
+	}
+  
+  ViewCellContainer::const_iterator it = viewcells.begin();
+  
+  
+  for (; it != viewcells.end(); ++it) {
+	ViewCell *viewcell = *it;
+	// if ray not outside of view space
+	float contribution;
+	if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
+												 ray.mPdf,
+												 contribution
+												 ))
+	  ray.mPvsContribution++;
+	ray.mRelativePvsContribution += contribution;
+  }
+  
+  if (addRays)
+	for (it = viewcells.begin(); it != viewcells.end(); ++it) {
+	  ViewCell *viewcell = *it;
+	  // if ray not outside of view space
+	  viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
+	}
+
+  return ray.mRelativePvsContribution;
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 561)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 563)
@@ -7,4 +7,5 @@
 #include "Beam.h"
 #include "KdTree.h"
+#include "Environment.h"
 
 #include <GL/glext.h>
@@ -191,5 +192,5 @@
 
 void
-GlRenderer::RandomViewPoint()
+GlRendererBuffer::RandomViewPoint()
 {
   Vector3 pVector = Vector3(halton.GetNumber(1),
@@ -201,5 +202,7 @@
 							0.0f);
   
-  mViewPoint = mSceneGraph->GetBox().GetPoint(pVector);
+  //  viewcells->GetViewPoint(mViewPoint);
+
+  mViewPoint =  mSceneGraph->GetBox().GetPoint(pVector);
   
   mViewDirection = Normalize(Vector3(sin(dVector.x),
@@ -211,4 +214,19 @@
 }
 
+
+GlRendererBuffer::GlRendererBuffer(const int w,
+								   const int h,
+								   SceneGraph *sceneGraph,
+								   ViewCellsManager *viewcells,
+								   KdTree *tree):
+QGLPixelBuffer(QSize(w, h)), GlRenderer(sceneGraph, viewcells, tree) {
+ 
+ environment->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames);
+   mPvsErrorBuffer.resize(mPvsStatFrames);
+	 ClearErrorBuffer();
+	   
+	   InitGL();
+		 
+		 }
 
 float
@@ -947,5 +965,5 @@
  makeCurrent(); 
  GlRenderer::InitGL();
-#if 0
+#if 1
 	// initialise dual depth buffer textures
 	glGenTextures(1, &frontDepthMap);
