Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 551)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 556)
@@ -560,5 +560,5 @@
 	void AddToPvs(BspLeaf *leaf,
 				  const RayInfoContainer &rays, 
-				  int &sampleContributions,
+				  float &sampleContributions,
 				  int &contributingSamples);
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 542)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 556)
@@ -2180,8 +2180,8 @@
 
 		if (ray->mTerminationObject)
-		  vc->GetPvs().AddSample(ray->mTerminationObject);
+		  vc->GetPvs().AddSample(ray->mTerminationObject, ray->mPdf);
 
 		if (ray->mOriginObject)
-		  vc->GetPvs().AddSample(ray->mOriginObject);
+		  vc->GetPvs().AddSample(ray->mOriginObject, ray->mPdf);
 	}
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 492)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 556)
@@ -25,7 +25,14 @@
 template<typename T> 
 struct PvsData {
-  int mVisibleSamples;
+  //  int mVisibleSamples;
+  // sum of probability density of visible sample rays
+  float mSumPdf;
   PvsData<T>() {}
-  PvsData<T>(const int samples): mVisibleSamples(samples) {}
+  PvsData<T>(const float sumPdf):
+	mSumPdf(sumPdf) {}
+
+  // $$JB in order to return meaningfull values
+  // it assumes that the sum pdf has been normalized somehow!!!
+  float GetVisibility() { return mSumPdf; }
 };
 
@@ -46,4 +53,9 @@
   int GetSize() const {return (int)mEntries.size();}
   bool Empty() const {return mEntries.empty();}
+
+  /** Normalize the visibility of entries in order to get comparable
+	  results */
+  
+  void NormalizeMaximum();
   
   /** Merges pvs of a and pvs of b into this pvs.
@@ -61,5 +73,5 @@
   PvsData<T> *Find(T sample);
 
-  bool GetSampleContribution(T sample, float &contribution);
+  bool GetSampleContribution(T sample, const float pdf, float &contribution);
   
   /** Adds sample to PVS. 
@@ -67,10 +79,10 @@
 	  @returns true if sample was not already in PVS.
   */
-  bool AddSample(T sample, float &contribution);
+  bool AddSample(T sample, const float pdf, float &contribution);
 
   /** Adds sample to PVS.
 	  @returns contribution of sample (0 or 1)
   */
-  int AddSample(T sample);
+  float AddSample(T sample, const float pdf);
   
   /** Adds one pvs to another one.
@@ -94,5 +106,5 @@
 	  @param visibleSampels number of references to be removed
   */
-  bool RemoveSample(T sample, const int visibleSamples);
+  bool RemoveSample(T sample, const float pdf);
 
   /// Map of PVS entries
@@ -130,5 +142,5 @@
 		
 		if (data)
-			data->mVisibleSamples += (*it).second.mVisibleSamples;
+			data->mSumPdf += (*it).second.mSumPdf;
 		else
 			mEntries.insert(*it);
@@ -159,15 +171,16 @@
 
 template <typename T>
-int
-Pvs<T>::AddSample(T sample)
+float
+Pvs<T>::AddSample(T sample, const float pdf)
 {
   PvsData<T> *data = Find(sample);
   
   if (data)  {
-	return ++data->mVisibleSamples;
+	data->mSumPdf+=pdf;
+	return data->mSumPdf;
   }
   else {
-	mEntries[sample] = PvsData<T>(1);
-	return 1;
+	mEntries[sample] = PvsData<T>(pdf);
+	return pdf;
   }
 }
@@ -175,15 +188,17 @@
 template <typename T>
 bool
-Pvs<T>::AddSample(T sample, float &contribution)
+Pvs<T>::AddSample(T sample,
+				  const float pdf,
+				  float &contribution)
 {
   PvsData<T> *data = Find(sample);
   
   if (data)  {
-	data->mVisibleSamples++;
-	contribution = 1.0f/data->mVisibleSamples;
+	data->mSumPdf+=pdf;
+	contribution = pdf/data->mSumPdf;
 	return false;
   }
   else {
-	mEntries[sample] = PvsData<T>(1);
+	mEntries[sample] = PvsData<T>(pdf);
 	contribution = 1.0f;
 	return true;
@@ -193,10 +208,12 @@
 template <typename T>
 bool
-Pvs<T>::GetSampleContribution(T sample, float &contribution) 
+Pvs<T>::GetSampleContribution(T sample,
+							  const float pdf,
+							  float &contribution) 
 {
   PvsData<T> *data = Find(sample);
   
   if (data)  {
-	contribution = 1.0f/(data->mVisibleSamples + 1);
+	contribution = pdf/(data->mSumPdf + pdf);
 	return false;
   }
@@ -208,19 +225,20 @@
 
 template <typename T>
-bool Pvs<T>::RemoveSample(T sample, const int visibleSamples)
-{
-	std::map<T, PvsData<T>, LtSample<T> >::
-		iterator it = mEntries.find(sample);
-
-	if (it == mEntries.end())
-		return false;
-
-	PvsData<T> *data = &(*it).second;
-  
-	data->mVisibleSamples -= visibleSamples;
-	if (data->mVisibleSamples <= 0)
-		mEntries.erase(it);
-
-	return true;
+bool Pvs<T>::RemoveSample(T sample,
+						  const float pdf)
+{
+  std::map<T, PvsData<T>, LtSample<T> >::
+	iterator it = mEntries.find(sample);
+  
+  if (it == mEntries.end())
+	return false;
+  
+  PvsData<T> *data = &(*it).second;
+  
+  data->mSumPdf -= pdf;
+  if (data->mSumPdf <= 0.0f)
+	mEntries.erase(it);
+  
+  return true;
 }
 
@@ -228,13 +246,13 @@
 int Pvs<T>::AddPvs(const Pvs<T> &pvs)
 {
-	std::map<T, PvsData<T>, LtSample<T> >::
-		const_iterator it, it_end = pvs.mEntries.end();
-
-	float contri;
-	// output PVS of view cell
-	for (it = pvs.mEntries.begin(); it != it_end; ++ it) 
-		AddSample((*it).first, contri);
-
-	return GetSize();
+  std::map<T, PvsData<T>, LtSample<T> >::
+	const_iterator it, it_end = pvs.mEntries.end();
+  
+  float contri;
+  // output PVS of view cell
+  for (it = pvs.mEntries.begin(); it != it_end; ++ it) 
+	AddSample((*it).first, (*it).second.mSumPdf, contri);
+  
+  return GetSize();
 }
  
@@ -242,12 +260,12 @@
 int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
 {
-	std::map<T, PvsData<T>, LtSample<T> >::
-		const_iterator it, it_end = pvs.mEntries.end();
-
-	// output PVS of view cell
-	for (it = pvs.mEntries.begin(); it != it_end; ++ it) 
-		RemoveSample((*it).first, (*it).second.mVisibleSamples);
-
-	return GetSize();
+  std::map<T, PvsData<T>, LtSample<T> >::
+	const_iterator it, it_end = pvs.mEntries.end();
+  
+  // output PVS of view cell
+  for (it = pvs.mEntries.begin(); it != it_end; ++ it) 
+	RemoveSample((*it).first, (*it).second.mSumPdf);
+  
+  return GetSize();
 }
 
@@ -262,4 +280,28 @@
 		entries.push_back((*it)->first);
 }
+
+template <typename T>
+void Pvs<T>::NormalizeMaximum()
+{
+  std::map<T, PvsData<T>, LtSample<T> >::
+	const_iterator it, it_end = mEntries.end();
+
+  float maxPdfSum = -1.0f;
+
+  // output PVS of view cell
+  for (it = mEntries.begin(); it != it_end; ++ it) {
+	float sum = (*it)->second.sumPdf;
+	if (sum > maxSum)
+	  maxSum = sum;
+  }
+
+  maxSum = 1.0f/maxSum;
+
+  for (it = mEntries.begin(); it != it_end; ++ it) {
+	(*it)->second.sumPdf *= maxSum;
+  }
+  
+}
+
 
 /** Class instantiating the Pvs template for kd tree nodes.
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/PreprocessorThread.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/PreprocessorThread.h	(revision 492)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/PreprocessorThread.h	(revision 556)
@@ -13,5 +13,5 @@
 public:
   PreprocessorThread(Preprocessor *p, QObject *parent = 0);
-  ~PreprocessorThread() {}
+  ~PreprocessorThread();
 
   void Main();
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 549)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 556)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.0) on: po 16. I 20:32:45 2006
+# Generated by qmake (2.00a) (Qt 4.1.0) on: ?t 19. I 11:57:31 2006
 # Project:  preprocessor.pro
 # Template: app
@@ -7,7 +7,7 @@
 #############################################################################
 
-first: release
-install: release-install
-uninstall: release-uninstall
+first: debug
+install: debug-install
+uninstall: debug-uninstall
 MAKEFILE      = Makefile
 QMAKE         = qmake
@@ -27,7 +27,23 @@
 MKDIR         = mkdir
 SUBTARGETS    =  \
-		release \
-		debug
+		debug \
+		release
 
+debug: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug
+debug-make_default: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug 
+debug-make_first: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug first
+debug-all: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug all
+debug-clean: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug clean
+debug-distclean: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug distclean
+debug-install: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug install
+debug-uninstall: $(MAKEFILE).Debug FORCE
+	$(MAKE) -f $(MAKEFILE).Debug uninstall
 release: $(MAKEFILE).Release FORCE
 	$(MAKE) -f $(MAKEFILE).Release
@@ -46,20 +62,4 @@
 release-uninstall: $(MAKEFILE).Release FORCE
 	$(MAKE) -f $(MAKEFILE).Release uninstall
-debug: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug
-debug-make_default: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug 
-debug-make_first: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug first
-debug-all: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug all
-debug-clean: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug clean
-debug-distclean: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug distclean
-debug-install: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug install
-debug-uninstall: $(MAKEFILE).Debug FORCE
-	$(MAKE) -f $(MAKEFILE).Debug 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\release.prf \
+		D:\Qt\4.1.0\mkspecs\features\debug.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\release.prf:
+D:\Qt\4.1.0\mkspecs\features\debug.prf:
 D:\Qt\4.1.0\mkspecs\features\debug_and_release.prf:
 D:\Qt\4.1.0\mkspecs\features\default_post.prf:
@@ -109,25 +109,29 @@
 qmake_all: 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
+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
 	-$(DEL_FILE) preprocessor.exp
-distclean: release-distclean debug-distclean FORCE
+	-$(DEL_FILE) preprocessor.pdb
+	-$(DEL_FILE) preprocessor.ilk
+	-$(DEL_FILE) vc*.pdb
+	-$(DEL_FILE) vc*.idb
+distclean: debug-distclean release-distclean FORCE
 	-$(DEL_FILE) Makefile
 
+debug-mocclean: $(MAKEFILE).Debug
+	$(MAKE) -f $(MAKEFILE).Debug mocclean
 release-mocclean: $(MAKEFILE).Release
 	$(MAKE) -f $(MAKEFILE).Release mocclean
-debug-mocclean: $(MAKEFILE).Debug
-	$(MAKE) -f $(MAKEFILE).Debug mocclean
-mocclean: release-mocclean debug-mocclean
+mocclean: debug-mocclean release-mocclean
 
+debug-mocables: $(MAKEFILE).Debug
+	$(MAKE) -f $(MAKEFILE).Debug mocables
 release-mocables: $(MAKEFILE).Release
 	$(MAKE) -f $(MAKEFILE).Release mocables
-debug-mocables: $(MAKEFILE).Debug
-	$(MAKE) -f $(MAKEFILE).Debug mocables
-mocables: release-mocables debug-mocables
+mocables: debug-mocables release-mocables
 FORCE:
 
+$(MAKEFILE).Debug: Makefile
 $(MAKEFILE).Release: Makefile
-$(MAKEFILE).Debug: Makefile
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 555)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 556)
@@ -300,5 +300,8 @@
 	virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0;
 
-	
+
+  virtual float GetViewSpaceVolume() {
+	return mViewSpaceBox.GetVolume()*(2.0f*sqr(M_PI));
+  }
 	/** Creates meshes from the view cells.
 	*/
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 555)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 556)
@@ -493,6 +493,7 @@
 	
 		//-- update pvs
-		int conSamp = 0, sampCon = 0;
-		AddToPvs(leaf, *tData.mRays, conSamp, sampCon);
+		int conSamp = 0;
+		float sampCon = 0.0f;
+		AddToPvs(leaf, *tData.mRays, sampCon, conSamp);
 
 		mStat.contributingSamples += conSamp;
@@ -660,32 +661,38 @@
 void VspBspTree::AddToPvs(BspLeaf *leaf,
 						  const RayInfoContainer &rays,
-						  int &sampleContributions,
+						  float &sampleContributions,
 						  int &contributingSamples)
 {
-	sampleContributions = 0;
-	contributingSamples = 0;
-
-    RayInfoContainer::const_iterator it, it_end = rays.end();
-
-	ViewCell *vc = leaf->GetViewCell();
-
-	// add contributions from samples to the PVS
-	for (it = rays.begin(); it != it_end; ++ it)
-	{
-		int contribution = 0;
-		VssRay *ray = (*it).mRay;
-
-		if (ray->mTerminationObject)
-			contribution += vc->GetPvs().AddSample(ray->mTerminationObject);
-
-		if (ray->mOriginObject)
-			contribution += vc->GetPvs().AddSample(ray->mOriginObject);
-
-		if (contribution)
-		{
-			sampleContributions += contribution;
-			++ contributingSamples;
-		}
-		//leaf->mVssRays.push_back(ray);
+  sampleContributions = 0;
+  contributingSamples = 0;
+  
+  RayInfoContainer::const_iterator it, it_end = rays.end();
+  
+  ViewCell *vc = leaf->GetViewCell();
+  
+  // add contributions from samples to the PVS
+  for (it = rays.begin(); it != it_end; ++ it)
+	{
+	  float sc = 0.0f;
+	  VssRay *ray = (*it).mRay;
+	  bool madeContrib = false;
+	  float contribution;
+	  if (ray->mTerminationObject) {
+		if (vc->GetPvs().AddSample(ray->mTerminationObject, ray->mPdf, contribution))
+		  madeContrib = true;
+		sc += contribution;
+	  }
+	  
+	  if (ray->mOriginObject) {
+		if (vc->GetPvs().AddSample(ray->mOriginObject, ray->mPdf, contribution))
+		  madeContrib = true;
+		sc += contribution;
+	  }
+	  
+	  sampleContributions += sc;
+	  if (madeContrib)
+		  ++ contributingSamples;
+		
+	  //leaf->mVssRays.push_back(ray);
 	}
 }
@@ -1499,5 +1506,5 @@
 		  << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), "
 		  << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
-		  << "Area: " << data.mArea << " (min: " << mTermMinArea << "), "
+	  //		  << "Area: " << data.mArea << " (min: " << mTermMinArea << "), "
 		  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
 		  << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, "
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 537)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 556)
@@ -304,9 +304,9 @@
   Vector3 mOrigin;
   Vector3 mDirection;
-  float mProbability;
+  float mPdf;
 
   SimpleRay() {}
   SimpleRay(const Vector3 &o, const Vector3 &d, const float p=1.0f):
-	mOrigin(o), mDirection(d), mProbability(p) {}
+	mOrigin(o), mDirection(d), mPdf(p) {}
 };
 
@@ -315,13 +315,13 @@
 public:
   
-  float mSumProbabilities;
-  
-  SimpleRayContainer():vector<SimpleRay>(), mSumProbabilities(0.0f) {}
-  
-  void NormalizeProbabilities() {
+  float mSumPdf;
+  
+  SimpleRayContainer():vector<SimpleRay>(), mSumPdf(0.0f) {}
+  
+  void NormalizePdf() {
 	iterator it = begin();
-	float c = 1.0f/mSumProbabilities;
+	float c = 1.0f/mSumPdf;
 	for (; it != end(); it++)
-	  (*it).mProbability*=c;
+	  (*it).mPdf*=c;
   }
   
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 535)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 556)
@@ -922,5 +922,5 @@
 	    // kd tree PVS
 		float contribution;
-		if (leaf->mKdPvs.AddSample(node, contribution))
+		if (leaf->mKdPvs.AddSample(node, 1.0f, contribution))
 	      totalPvsSize++;
 	  }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 551)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 556)
@@ -2470,9 +2470,13 @@
 		float relContribution;
 		if (!ray->intersections.empty())
-		  contribution += vc->GetPvs().AddSample(ray->intersections[0].mObject, relContribution);
+		  contribution += vc->GetPvs().AddSample(ray->intersections[0].mObject,
+												 1.0f,
+												 relContribution);
 		
 		if (ray->sourceObject.mObject)
-			contribution += vc->GetPvs().AddSample(ray->sourceObject.mObject, relContribution);
-
+			contribution += vc->GetPvs().AddSample(ray->sourceObject.mObject,
+												   1.0f,
+												   relContribution);
+		
 		if (contribution)
 		{
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 540)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 556)
@@ -33,9 +33,19 @@
 Preprocessor::~Preprocessor()
 {
+  cerr<<"Deleting view cells manager...\n";
   DEL_PTR(mViewCellsManager);
+  cerr<<"done.\n";
+  cerr<<"Deleting bsp tree...\n";
   DEL_PTR(mBspTree);
+  cerr<<"done.\n";
+  cerr<<"Deleting kd tree...\n";
   DEL_PTR(mKdTree);
+  cerr<<"done.\n";
+  cerr<<"Deleting vspkd tree...\n";
   DEL_PTR(mVspKdTree);
+  cerr<<"done.\n";
+  cerr<<"Deleting vspbsp tree...\n";
   DEL_PTR(mVspBspTree);
+  cerr<<"done.\n";
 }
 
@@ -258,4 +268,5 @@
 		mViewCellsFilename = buf;
 	}
+
 	if (mUseGlRenderer || mUseGlDebugger)
 	{
@@ -263,5 +274,5 @@
 		// renderer must be initialised
 		renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree);
-		renderer->makeCurrent();
+		//		renderer->makeCurrent();
 	}
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 551)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 556)
@@ -1030,8 +1030,8 @@
   //  if ( (int)(leaf->rays.size()) < termMinCost)
   //    stat.minCostNodes++;
-  if ( leaf->GetPvsSize() < termMinPvs)
+  if ( leaf->GetPvsSize() <= termMinPvs)
 	stat.minPvsNodes++;
 
-  if ( leaf->GetPvsSize() < termMinRays)
+  if ( leaf->GetPvsSize() <= termMinRays)
 	stat.minRaysNodes++;
 
@@ -1051,6 +1051,6 @@
 RssTree::TerminationCriteriaSatisfied(RssTreeLeaf *leaf)
 {
-  return ( (leaf->GetPvsSize() < termMinPvs) ||
-		   (leaf->rays.size() < termMinRays) ||
+  return ( (leaf->GetPvsSize() <= termMinPvs) ||
+		   (leaf->rays.size() <= termMinRays) ||
 		   //			 (leaf->GetAvgRayContribution() > termMaxRayContribution ) ||
 		   (leaf->depth >= termMaxDepth) ||
@@ -2081,14 +2081,21 @@
 	  ri++) {
 	Intersectable *object = (*ri).GetObject();
-	if (object && !object->Mailed()) {
-	  object->Mail();
-	  pvs.push_back(object);
-	  if (exporter)
-		exporter->ExportIntersectable(object);
-	}
-  }
-
+	if (object) {
+	  if (!object->Mailed()) {
+		object->Mail();
+		object->mCounter = 1;
+		pvs.push_back(object);
+		if (exporter)
+		  exporter->ExportIntersectable(object);
+	  } else {
+		object->mCounter++;
+	  }
+	}
+  }
+  // sort objects based on their mCounter
+  sort(pvs.begin(), pvs.end(), Intersectable::GreaterCounter);
+  
   float extendedConvexCombinationProb = 0.0f; //0.7f
-  float silhouetteCheckPercentage = 0.0f; //0.5f
+  float silhouetteCheckPercentage = 0.5f; //0.5f
   for (int i=0; generated < numberOfRays && i < numberOfTries; i++) {
 	bool useExtendedConvexCombination = ((nrays >= 2) && (Random(1.0f) <
@@ -2179,5 +2186,4 @@
 		  oi++) {
 		Intersectable *object = *oi;
-		
 		if ( object->CastRay(traversalRay) ) {
 		  intersects = true;
@@ -2197,9 +2203,9 @@
   
   //  cout<<"desired="<<numberOfRays<<" tries="<<i<<endl;
-  // assign probabilitites to the generated rays
-  float p = (box.GetVolume()*dirBox.GetVolume())/generated;
+  // assign pdfs to the generated rays
+  float p = (box.GetVolume()*dirBox.GetVolume())/i;
   for (i=startIndex; i < rays.size(); i++) {
-	rays[i].mProbability = p;
-	rays.mSumProbabilities += p;
+	rays[i].mPdf = p;
+	rays.mSumPdf += p;
   }
   
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 537)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 556)
@@ -65,5 +65,5 @@
 
   // probability of this ray
-  float mProbability;
+  float mPdf;
   
   //////////////////////////////
@@ -74,5 +74,5 @@
 		 Intersectable *terminationObject,
 		 const int pass = 0,
-		 const float probability = 1.0f
+		 const float pdf = 1.0f
 		 ):
 	mMailbox(-1),
@@ -86,5 +86,5 @@
 	mViewCells(0),
 	mWeightedPvsContribution(0),
-	mProbability(probability)
+	mPdf(pdf)
   {
     Precompute();
@@ -98,5 +98,5 @@
 	mPass(0),
 	mViewCells(0),
-	mProbability(1.0f)
+	mPdf(1.0f)
   {
 	if (ray.sourceObject.mObject)
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 549)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 556)
@@ -85,4 +85,7 @@
   if (!mDetectEmptyViewSpace)
 	ray.mFlags &= ~Ray::CULL_BACKFACES;
+  else
+	ray.mFlags |= Ray::CULL_BACKFACES;
+
 
   // cast ray to KD tree to find intersection with other objects
@@ -113,4 +116,6 @@
   if (!mDetectEmptyViewSpace)
 	ray.mFlags &= ~Ray::CULL_BACKFACES;
+  else
+	ray.mFlags |= Ray::CULL_BACKFACES;
 
   if (mKdTree->CastRay(ray)) {
@@ -130,4 +135,5 @@
 	// cast again to ensure that there is no objectA
 	SetupRay(ray, pointB, direction);
+	ray.mFlags |= Ray::CULL_BACKFACES;
 	if (mKdTree->CastRay(ray)) {
 	  objectA = ray.intersections[0].mObject;
@@ -315,6 +321,6 @@
   //	exporter->ExportKdTree(*mKdTree);
   exporter->SetFilled();
-  // temporarily do not export the scene
-  if (0)
+  // $$JB temporarily do not export the scene
+  if (1)
 	exporter->ExportScene(mSceneGraph->mRoot);
   exporter->SetWireframe();
@@ -459,4 +465,5 @@
 	//	renderer->mRenderingFinished.wait(&mutex);
 	//	mutex.unlock();
+
 	renderer->EvalPvsStat();
 	mStats <<
@@ -470,4 +477,7 @@
 RssPreprocessor::ComputeVisibility()
 {
+
+  if (renderer)
+	renderer->InitGL();
   
   //  connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) );
@@ -698,5 +708,5 @@
 		Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
 		rays.push_back(SimpleRay(viewpoint, direction, 1.0f));
-		rays.mSumProbabilities+=1.0f;
+		rays.mSumPdf += 1.0f;
 	  }
 	} else {
@@ -704,8 +714,8 @@
 	}
 
-	rays.NormalizeProbabilities();
+	rays.NormalizePdf();
 	
 	for (int i=0; i < rays.size(); i++)
-	  CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mProbability, vssRays);
+	  CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mPdf, vssRays);
 
 	
@@ -802,19 +812,22 @@
   if (mUseViewcells) {
 
-
-  //-- render simulation after merge
-  cout << "\nevaluating bsp view cells render time after merge ... ";
-  
-  mRenderSimulator->RenderScene();
-  SimulationStatistics ss;
-  mRenderSimulator->GetStatistics(ss);
-
-  cout << " finished" << endl;
-  cout << ss << endl;
-  Debug << ss << endl;
-
-  }
-
+	
+	//-- render simulation after merge
+	cout << "\nevaluating bsp view cells render time after merge ... ";
+	
+	mRenderSimulator->RenderScene();
+	SimulationStatistics ss;
+	mRenderSimulator->GetStatistics(ss);
+	
+	cout << " finished" << endl;
+	cout << ss << endl;
+	Debug << ss << endl;
+	
+  }
+
+  cerr<<"Deleting RSS tree...\n";
   delete rssTree;
+  cerr<<"Done.\n";
+
   
   return true;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 554)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 556)
@@ -79,7 +79,9 @@
 
 
-  if (!mDetectEmptyViewSpace)
-	ray.mFlags &= ~Ray::CULL_BACKFACES;
-  
+	if (!mDetectEmptyViewSpace)
+	  ray.mFlags &= ~Ray::CULL_BACKFACES;
+	else
+	  ray.mFlags |= Ray::CULL_BACKFACES;
+
   if (mKdTree->CastRay(ray)) {
 	objectA = ray.intersections[0].mObject;
@@ -103,4 +105,6 @@
   if (!mDetectEmptyViewSpace)
 	ray.mFlags &= ~Ray::CULL_BACKFACES;
+							   else
+								 ray.mFlags |= Ray::CULL_BACKFACES;
 
   if (mKdTree->CastRay(ray)) {
@@ -120,4 +124,5 @@
 	// cast again to ensure that there is no objectA
 	SetupRay(ray, pointB, direction);
+	  ray.mFlags |= Ray::CULL_BACKFACES;
 	if (mKdTree->CastRay(ray)) {
 	  objectA = ray.intersections[0].mObject;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 540)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 556)
@@ -84,5 +84,5 @@
 
   // create a preprocessor thread
-  PreprocessorThread *pt = new PreprocessorThread(p);
+  PreprocessorThread *pt = new PreprocessorThread(p, app);
 
   //  p->mSceneGraph->Export("soda.x3d");
@@ -94,12 +94,10 @@
   if (p->mUseGlRenderer) {
 	
-	  rendererWidget = new GlRendererWidget(p->mSceneGraph, p->mViewCellsManager, p->mKdTree);
+	rendererWidget = new GlRendererWidget(p->mSceneGraph, p->mViewCellsManager, p->mKdTree);
 	//  renderer->resize(640, 480);
 	rendererWidget->resize(640, 480);
 	rendererWidget->show();
-
 	pt->start(QThread::LowPriority);
-  } else
-  {
+  } else{
 	// just call the mail method -> will be executed in the main thread
 	pt->Main();
@@ -111,5 +109,5 @@
   if (app)
 	return app->exec();
-
+  
   return 0;
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Intersectable.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Intersectable.h	(revision 497)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Intersectable.h	(revision 556)
@@ -75,5 +75,9 @@
 									 VssRayContainer &rays
 									 ) {return 0;}
-  
+
+  static bool GreaterCounter(const Intersectable *a,
+							 const Intersectable *b) {
+	return a->mCounter > b->mCounter;
+  }
 };
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 549)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 556)
@@ -13,7 +13,7 @@
 #;../data/vienna/vienna-plane.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
 }
 
@@ -25,5 +25,5 @@
 #	type vss
 	type rss
-	detectEmptyViewSpace false
+	detectEmptyViewSpace true
 }
 
@@ -70,7 +70,7 @@
 RssPreprocessor {
 	samplesPerPass  100000
-	initialSamples 1000000
-	vssSamples 20000000
-	vssSamplesPerPass 1000000
+	initialSamples 100000
+	vssSamples 10000
+	vssSamplesPerPass 100000
 	useImportanceSampling true
 
@@ -93,11 +93,10 @@
 
 RssTree {
-
 	epsilon		1e-6
-	perObjectTree true
+	perObjectTree false
 
 	maxDepth 	40
-	minPvs		3
-	minRays		30
+	minPvs		1
+	minRays		100
 	minSize		0.001
  	maxCostRatio 1.0
@@ -107,8 +106,8 @@
 	maxStaticMemory	100
 
-#	splitType regular
+	splitType regular
 #	splitType heuristic
-	splitType hybrid
-	splitUseOnlyDrivingAxis false
+#	splitType hybrid
+	splitUseOnlyDrivingAxis true
 	importanceBasedCost false
 
@@ -162,5 +161,5 @@
 
 
-Sampling {
+SamplingPreprocessor {
 	totalSamples 10000000
 	samplesPerPass  3
@@ -200,6 +199,6 @@
 	}
 	
-#	filename ../data/soda/viewcells_soda5.xml
-	filename ../data/atlanta/viewcells_atlanta2.xml
+	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
@@ -284,22 +283,22 @@
 	}
 	
-	Termination {
-		# parameters used for autopartition
-		minRays 		100
-		minPolygons 		-1
-		maxDepth		30
-		minPvs 			2
-		#minProbability 	0.0001
-		minProbabilty 		0.000
-		maxRayContribution	0.005
-		maxCostRatio		0.9
-		missTolerance		4
-		#maxAccRayLength	100
+#	Termination {
+#		# parameters used for autopartition
+#		minRays 		100
+#		minPolygons 		-1
+#		maxDepth		30
+#		minPvs 			2
+#		#minProbability 	0.0001
+#		minProbabilty 		0.000
+#		maxRayContribution	0.005
+#		maxCostRatio		0.9
+#		missTolerance		4
+#		#maxAccRayLength	100
 		
-		maxViewCells		20000
+#		maxViewCells		20000
 		
-		# used for pvs criterium
-		ct_div_ci 0.0
-	}
+#		# used for pvs criterium
+#		ct_div_ci 0.0
+#	}
 	
 	splitUseOnlyDrivingAxis false
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 540)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 556)
@@ -122,11 +122,11 @@
 	QGLPixelBuffer(QSize(w, h)), GlRenderer(sceneGraph, viewcells, tree) {
 
-	mPvsStatFrames = 10000;
+	mPvsStatFrames = 5000;
 	mPvsErrorBuffer.resize(mPvsStatFrames);
 	ClearErrorBuffer();
 
-	makeCurrent();
-	InitGL();
-	doneCurrent();
+// 	makeCurrent();
+// 	InitGL();
+// 	doneCurrent();
 
 	}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro	(revision 549)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro	(revision 556)
@@ -17,5 +17,5 @@
 
 # debuc config
-CONFIG		+= console warn_off thread release qt
+CONFIG		+= console warn_off thread debug qt
 
 # RELEASE CONFIG
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp	(revision 508)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp	(revision 556)
@@ -197,11 +197,14 @@
 
 				ObjectContainer::iterator oit =
-					lower_bound(mObjects->begin(), mObjects->end(), &dummyInst, ilt);
-			
+				  lower_bound(mObjects->begin(), mObjects->end(), &dummyInst, ilt);
+				
 				Intersectable *obj = *oit;
 				
 				if (obj->GetId() == objId)
 				{
-					viewCell->GetPvs().AddSample(obj);
+				  // $$JB we should store a float a per object which corresponds
+				  // to sumof pdfs, i.e. its relative visibility
+				  // temporarily set to 1.0f
+					viewCell->GetPvs().AddSample(obj, 1.0f);
 				}
 				else
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/PreprocessorThread.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/PreprocessorThread.cpp	(revision 492)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/PreprocessorThread.cpp	(revision 556)
@@ -13,4 +13,5 @@
 {
   mPreprocessor = p;
+  connect(this, SIGNAL(finished()), qApp, SLOT(closeAllWindows(void)));
 }
 
@@ -21,8 +22,9 @@
   if (1) {
     mPreprocessor->ComputeVisibility();
-    mPreprocessor->ExportPreprocessedData("scene.vis");
+	//	mPreprocessor->ExportPreprocessedData("scene.vis");
   }
   
   Camera camera;
+
   if (0) {
     camera.LookAtBox(mPreprocessor->mKdTree->GetBox());
@@ -43,5 +45,11 @@
   }
 
-  QApplication::quit();
+
+  cerr<<"Preprocessor main finished...\n";
   
 }
+
+PreprocessorThread::~PreprocessorThread()
+{
+  cerr<<"Preprocessor thread destructor...\n";
+}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 555)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 556)
@@ -458,6 +458,7 @@
 			bool added =
 			viewcell->GetPvs().AddSample(ray.mTerminationObject,
-											contribution
-											);
+										 ray.mPdf,
+										 contribution
+										 );
 			if (added)
 			ray.mPvsContribution++;
@@ -473,14 +474,15 @@
 			float contribution;
 			if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
-														contribution
-														))
-			ray.mPvsContribution++;
+														 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);
+		  ViewCell *viewcell = *it;
+		  // if ray not outside of view space
+		  viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
 		}
 	}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 544)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 556)
@@ -49,6 +49,10 @@
 GlRenderer::~GlRenderer()
 {
+  cerr<<"gl renderer destructor..\n";
+  if (sCgFragmentProgram)
 	cgDestroyProgram(sCgFragmentProgram);
+  if (sCgContext)
 	cgDestroyContext(sCgContext);
+  cerr<<"done."<<endl;
 }
 
@@ -283,5 +287,5 @@
 							 &pixelCount);
 	
-	pErrorPixels = (100.f*pixelCount)/(GetWidth()*GetHeight());
+	pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
   }
   
@@ -343,5 +347,5 @@
 		  errorPixels++;
 		}
-	pErrorPixels = (100.f*errorPixels)/(im1.height()*im1.width());
+	pErrorPixels = ((float)errorPixels)/(im1.height()*im1.width());
   }
 
@@ -466,9 +470,10 @@
 	}
   }
-  
+
+  glFinish();
   doneCurrent();
 
   cout<<endl<<flush;
-  mRenderingFinished.wakeAll();
+  //  mRenderingFinished.wakeAll();
 }
 
@@ -940,5 +945,7 @@
 void GlRendererBuffer::InitGL()
 {
-	GlRenderer::InitGL();
+ makeCurrent(); 
+ GlRenderer::InitGL();
+#if 0
 	// initialise dual depth buffer textures
 	glGenTextures(1, &frontDepthMap);
@@ -980,5 +987,6 @@
   }
 
-  sCgFragmentProgram = cgCreateProgramFromFile(sCgContext, 		
+
+ sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,
 											   CG_SOURCE, "../src/dual_depth.cg",
 											   sCgFragmentProfile,
@@ -991,7 +999,10 @@
   cgGLLoadProgram(sCgFragmentProgram);
   cgGLBindProgram(sCgFragmentProgram);
- 
-  Debug << "---- PROGRAM BEGIN ----\n" << 
+
+  Debug << "---- PROGRAM BEGIN ----\n" <<
 	  cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n";
+
+#endif
+  doneCurrent();
 }
 
@@ -1071,8 +1082,8 @@
 GlDebuggerWidget::~GlDebuggerWidget()
 {
-	mRenderBuffer->releaseFromDynamicTexture();
-	glDeleteTextures(1, &dynamicTexture);
-	
-	DEL_PTR(mRenderBuffer);
+ mRenderBuffer->releaseFromDynamicTexture();
+   glDeleteTextures(1, &dynamicTexture);
+	 
+	 DEL_PTR(mRenderBuffer);
 }
 
