Index: /GTP/trunk/Lib/Vis/Preprocessing/src/DifferenceSampling.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/DifferenceSampling.h	(revision 2546)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/DifferenceSampling.h	(revision 2548)
@@ -2,9 +2,8 @@
 #define _DifferenceSampling_H__
 
-#include <vector>
-//
+#include "common.h"
+//#include "ObjectPvs.h"
 
-#include "common.h"
-#include "Halton.h"
+class SimpleRay;
 
 namespace GtpVisibilityPreprocessor {
@@ -35,6 +34,11 @@
 private:
 
-  virtual bool GenerateSample(SimpleRay &ray);
-  
+	virtual bool GenerateSample(SimpleRay &ray);
+
+	//ObjectPvs mDifferencePvs;
+	ObjectContainer mPvsDifference;
+
+	ViewCell *mCurrentViewCell;
+	int mNumSamples;
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 2176)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 2548)
@@ -47,5 +47,6 @@
 		MUTATION_BASED_DISTRIBUTION,
 		HW_GLOBAL_LINES_DISTRIBUTION,
-		VIEWCELL_BASED_DISTRIBUTION
+		VIEWCELL_BASED_DISTRIBUTION,
+		DIFFERENCE_SAMPLING_BASED_DISTRIBUTION,
 	};
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2547)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2548)
@@ -3456,4 +3456,5 @@
 	float samples = (float)basePvs.GetSamples();
 
+
 	//////////
 	//-- now compute the filter box around the current viewCell
@@ -3599,5 +3600,4 @@
 	viewCell->SetFilteredPvsSize(pvs.GetSize());
 
-	//cout << "pvssize: " << pvs.GetSize() << endl;
 	// warning: not thread-safe!
 	if (!mUseKdPvs)
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 2530)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 2548)
@@ -591,6 +591,6 @@
 
 template <typename T, typename S>
-typename vector< PvsEntry<T, S> >::iterator VerbosePvs<T, S>::AddSample2(T sample, 
-																  const float pdf)
+typename vector< PvsEntry<T, S> >::iterator 
+VerbosePvs<T, S>::AddSample2(T sample, const float pdf)
 {
 	++ mSamples;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/DifferenceSampling.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/DifferenceSampling.cpp	(revision 2546)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/DifferenceSampling.cpp	(revision 2548)
@@ -3,4 +3,6 @@
 #include "DifferenceSampling.h"
 #include "ViewCell.h"
+#include "ViewCellsManager.h"
+#include "Preprocessor.h"
 
 
@@ -10,16 +12,116 @@
 
 
-
 namespace GtpVisibilityPreprocessor {
 
 
+/** Collects the pvs of the objects which are 
+	in the first but not in the second pvs.
+*/
+static void GetPvsDifference(const ObjectPvs &largerPvs, 
+							 const ObjectPvs &smallerPvs, 
+							 ObjectContainer &pvsDifference)
+{
+	Intersectable::NewMail();
+	Intersectable *obj;
+
+	ObjectPvsIterator bit = smallerPvs.GetIterator();
+
+	// mail smaller pvs
+	while (bit.HasMoreEntries())
+	{
+		obj = bit.Next();
+		obj->Mail();
+	}
+
+	ObjectPvsIterator ait = largerPvs.GetIterator();
+
+	// write differece pvs
+	while (ait.HasMoreEntries())
+	{
+		obj = ait.Next();
+		pvsDifference.push_back(obj);
+
+		//if (!obj->Mailed())
+		//	c.AddSampleDirty(obj, 0);
+	}
+}
+
+
 DifferenceSampling::DifferenceSampling(Preprocessor &preprocessor):
-SamplingStrategy(preprocessor)
+SamplingStrategy(preprocessor), mCurrentViewCell(0), mNumSamples(0)
 {}
 
 
-void DifferenceSampling::ComputeDifferenceSet(ViewCell *vc, ObjectContainer &differenceSet)
+void DifferenceSampling::ComputeDifferenceSet(ViewCell *vc, 
+											  ObjectContainer &differenceSet)
 {
+	const ObjectPvs &pvs = vc->GetPvs();
+	const float filterSize = 100;
+	ObjectPvs filteredPvs;
+
+    PvsFilterStatistics pvsStats = mPreprocessor.
+		mViewCellsManager->ApplyFilter2(vc, 
+		                                false, 
+										filterSize, 
+										filteredPvs);
+
+	//mDifferencePvs.Clear(false);
+	//mDifferencePvs.Reserve(filteredPvs.GetSize());
+	mPvsDifference.clear();
+	mPvsDifference.reserve(filteredPvs.GetSize());
+
+	GetPvsDifference(filteredPvs, pvs, mPvsDifference);
 }
 
+
+bool DifferenceSampling::GenerateSample(SimpleRay &ray)
+{
+	if (mNumSamples -- <= 0)
+	{
+		ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells();
+		const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
+
+		mCurrentViewCell = viewCells[vcIdx];
+
+		ComputeDifferenceSet(mCurrentViewCell, mPvsDifference);
+
+		// n samples per object
+		const int n = 50;
+		mNumSamples = mPvsDifference.size() * n;
+	}
+
+	Vector3 origin, direction; 
+	Vector3 point;
+	Vector3 normal, vcnormal;
+
+	// get a new object from the difference set
+	const int objIdx = (int)RandomValue(0, (float)mPvsDifference.size() - 0.5f);
+    Intersectable *object = mPvsDifference[objIdx];
+
+	// cast a ray from the view cells border towards the object
+	object->GetRandomSurfacePoint(point, normal);
+	mCurrentViewCell->GetRandomEdgePoint(origin, vcnormal);
+
+	direction = point - origin;
+
+	// $$ jb the pdf is yet not correct for all sampling methods!
+	const float c = Magnitude(direction);
+
+	if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
+	{
+		return false;
+	}
+
+	// $$ jb the pdf is yet not correct for all sampling methods!
+	const float pdf = 1.0f;
+	//cout << "p: " << point << " ";
+	direction *= 1.0f / c;
+	ray = SimpleRay(origin, direction, DIFFERENCE_SAMPLING_BASED_DISTRIBUTION, pdf);
+
+	//cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
+
+	return true;
 }
+
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2353)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2548)
@@ -8,4 +8,6 @@
 #include "Mutation.h"
 #include "Timer/PerfTimer.h"
+#include "DifferenceSampling.h"
+
 
 
@@ -711,4 +713,7 @@
 					 mDistributions.push_back(new MutationBasedDistribution(mPreprocessor));
 					}
+					else if (strcmp(curr, "difference")==0) {
+					 mDistributions.push_back(new DifferenceSampling(mPreprocessor));
+					}
 	
 	
