Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1883)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1884)
@@ -1017,5 +1017,5 @@
 
 
-SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const
+SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId)
 {
 	switch (strategyId)
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1883)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1884)
@@ -537,4 +537,9 @@
 	curr = e+1;
   }
+
+  mMixtureDistribution = new MixtureDistribution(*this);
+  mMixtureDistribution->mDistributions = mDistributions;
+  mMixtureDistribution->Init();
+  bool oldGenerator = false;
   
   if (mLoadInitialSamples) {
@@ -545,27 +550,42 @@
 	SimpleRayContainer rays;
 	
-	cout<<"Generating initial rays..."<<endl<<flush;
-	
-	int count = 0;
-	int i;
-	
-	// Generate initial samples
-	for (i=0; i < mDistributions.size(); i++)
-	  if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
-		count++;
-	
-	for (i=0; i < mDistributions.size(); i++)
-	  if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
-		GenerateRays(mInitialSamples/count,
-					 *mDistributions[i],
-					 rays);
-	
-	
-	
-	
-	cout<<"Casting initial rays..."<<endl<<flush;
-	CastRays(rays, mVssRays, true, pruneInvalidRays);
-	
-	ExportObjectRays(mVssRays, 1546);
+	if (oldGenerator) {
+	  
+	  cout<<"Generating initial rays..."<<endl<<flush;
+	  
+	  int count = 0;
+	  int i;
+	  
+	  // Generate initial samples
+	  for (i=0; i < mDistributions.size(); i++)
+		if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
+		  count++;
+	  
+	  for (i=0; i < mDistributions.size(); i++)
+		if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
+		  GenerateRays(mInitialSamples/count,
+					   *mDistributions[i],
+					   rays);
+
+	  cout<<"Casting initial rays..."<<endl<<flush;
+	  CastRays(rays, mVssRays, true, pruneInvalidRays);
+	  
+	  ExportObjectRays(mVssRays, 1546);
+
+	  cout<<"Computing sample contributions..."<<endl<<flush;
+	  // evaluate contributions of the intitial rays
+	  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
+	  cout<<"done.\n"<<flush;
+	  
+	} else {
+	  const int batch = 1000;
+	  for (int i=0; i < mInitialSamples; i+=batch) {
+		rays.clear();
+		mVssRays.clear();
+		mMixtureDistribution->GenerateSamples(batch, rays);
+		CastRays(rays, mVssRays, true, pruneInvalidRays);
+		mMixtureDistribution->ComputeContributions(mVssRays);
+	  }
+	}
   }
   
@@ -593,8 +613,4 @@
   }
 	
-  cout<<"Computing sample contributions..."<<endl<<flush;
-  // evaluate contributions of the intitial rays
-  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
-  cout<<"done.\n"<<flush;
   
   long time = GetTime();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1883)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 1884)
@@ -90,5 +90,6 @@
 RssPreprocessor {
 
-	distributions rss+spatial+object_direction
+#	distributions rss+spatial+object_direction
+	distributions object_direction+spatial
 #	distributions object_direction
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1867)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1884)
@@ -96,5 +96,5 @@
 	/** Returns the specified sample strategy, NULL if no valid strategy.
 	*/
-	SamplingStrategy *GenerateSamplingStrategy(const int strategyId) const;
+	SamplingStrategy *GenerateSamplingStrategy(const int strategyId);
 
 	/** Export preprocessor data.
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 1883)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h	(revision 1884)
@@ -45,5 +45,5 @@
 	/** Default constructor
 	*/
-	SamplingStrategy(const Preprocessor &preprocessor);
+	SamplingStrategy(Preprocessor &preprocessor);
 
 	virtual ~SamplingStrategy();
@@ -55,7 +55,16 @@
 	virtual int GenerateSamples(const int number, SimpleRayContainer &rays);
 
-private:
 
 	virtual bool GenerateSample(SimpleRay &ray) = 0;
+  
+
+
+  virtual void Update(VssRayContainer &vssRays) {}
+
+
+
+  friend bool LowerRatio(const SamplingStrategy *a, const SamplingStrategy *b) {
+	return a->mRatio < b->mRatio;
+  }
 
 public:
@@ -71,5 +80,5 @@
 protected:
 
-	const Preprocessor &mPreprocessor;
+  Preprocessor &mPreprocessor;
 
 };
@@ -81,5 +90,5 @@
   HaltonSequence mHalton;
   
-  ObjectBasedDistribution(const Preprocessor &preprocessor):
+  ObjectBasedDistribution(Preprocessor &preprocessor):
 	SamplingStrategy(preprocessor) {
 	mType = OBJECT_BASED_DISTRIBUTION;
@@ -95,5 +104,5 @@
  public:
 	
-	 ReverseObjectBasedDistribution(const Preprocessor &preprocessor):
+	 ReverseObjectBasedDistribution(Preprocessor &preprocessor):
 	   SamplingStrategy(preprocessor) {
 	   mType = REVERSE_OBJECT_BASED_DISTRIBUTION;
@@ -110,5 +119,5 @@
   
 public:
-	 ObjectDirectionBasedDistribution(const Preprocessor &preprocessor):
+	 ObjectDirectionBasedDistribution(Preprocessor &preprocessor):
 	 SamplingStrategy(preprocessor) {
 	   mType = OBJECT_DIRECTION_BASED_DISTRIBUTION;
@@ -122,5 +131,5 @@
 {
  public:
-  DirectionBasedDistribution(const Preprocessor &preprocessor):
+  DirectionBasedDistribution(Preprocessor &preprocessor):
 	SamplingStrategy(preprocessor){
 	mType = DIRECTION_BASED_DISTRIBUTION;
@@ -134,5 +143,5 @@
 {
  public:
-	 DirectionBoxBasedDistribution(const Preprocessor &preprocessor):
+	 DirectionBoxBasedDistribution(Preprocessor &preprocessor):
 	   SamplingStrategy(preprocessor){
 	   mType = DIRECTION_BOX_BASED_DISTRIBUTION;
@@ -148,5 +157,5 @@
  public:
   HaltonSequence mHalton;
-  SpatialBoxBasedDistribution(const Preprocessor &preprocessor):
+  SpatialBoxBasedDistribution(Preprocessor &preprocessor):
 	SamplingStrategy(preprocessor){
 	mType = DIRECTION_BOX_BASED_DISTRIBUTION;
@@ -161,5 +170,5 @@
 {
  public:
-	 ViewSpaceBorderBasedDistribution(const Preprocessor &preprocessor):
+	 ViewSpaceBorderBasedDistribution(Preprocessor &preprocessor):
 	   SamplingStrategy(preprocessor){
 	   mType = SPATIAL_BOX_BASED_DISTRIBUTION;
@@ -174,5 +183,5 @@
 {
  public:
-	 ReverseViewSpaceBorderBasedDistribution(const Preprocessor &preprocessor):
+	 ReverseViewSpaceBorderBasedDistribution(Preprocessor &preprocessor):
 	   SamplingStrategy(preprocessor){
 	   mType = SPATIAL_BOX_BASED_DISTRIBUTION;
@@ -184,22 +193,4 @@
 
 
-class RssBasedDistribution: public SamplingStrategy
-{
- public:
-  RssBasedDistribution(const Preprocessor &preprocessor):
-	SamplingStrategy(preprocessor){
-	mType = RSS_BASED_DISTRIBUTION;
-  }
-
-
-  virtual int GenerateSamples(const int number, SimpleRayContainer &ray) {
-	// TBD!!!
-	return 0;
-  }
-
-private:
-  virtual bool GenerateSample(SimpleRay &ray);
-  
-};
 
 
@@ -207,5 +198,5 @@
 {
  public:
-  ViewCellBorderBasedDistribution(const Preprocessor &preprocessor):
+  ViewCellBorderBasedDistribution(Preprocessor &preprocessor):
 	SamplingStrategy(preprocessor) {}
   
@@ -219,5 +210,5 @@
   HaltonSequence mHalton;
 
-  GlobalLinesDistribution(const Preprocessor &preprocessor):
+  GlobalLinesDistribution(Preprocessor &preprocessor):
 	SamplingStrategy(preprocessor) {
 	mType = GLOBAL_LINES_DISTRIBUTION;
@@ -234,5 +225,5 @@
 {
  public:
-	 ObjectsInteriorDistribution(const Preprocessor &preprocessor):
+	 ObjectsInteriorDistribution(Preprocessor &preprocessor):
 	 SamplingStrategy(preprocessor) {}
 	 
@@ -249,5 +240,5 @@
   vector<SamplingStrategy *> mDistributions;
 
-  MixtureDistribution(const Preprocessor &preprocessor):
+  MixtureDistribution(Preprocessor &preprocessor):
 	SamplingStrategy(preprocessor)
   {
@@ -256,4 +247,8 @@
   // has to called before first usage
   void Init();
+
+  // equalize distribution contributions
+  void
+  Reset();
   
   // Generate a new sample according to a mixture distribution
@@ -261,10 +256,9 @@
 
   // add contributions of the sample to the strategies
-  void UpdateContributions(VssRayContainer &vssRays);
-  
-protected:
-  // distributions sorted according to their contribution
-  // used for new sample generation accordint to the pdf
-  vector<SamplingStrategy *> mSortedDistributions;
+  void ComputeContributions(VssRayContainer &vssRays);
+
+  void
+  UpdateRatios();
+
   
 };
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 1867)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h	(revision 1884)
@@ -7,4 +7,5 @@
 #include "Preprocessor.h"
 #include "VssRay.h"
+#include "SamplingStrategy.h"
 
 namespace GtpVisibilityPreprocessor {
@@ -22,4 +23,5 @@
 public:
   vector<SamplingStrategy *> mDistributions;
+  MixtureDistribution *mMixtureDistribution;
   int mSamplesPerPass;
   int mRssSamplesPerPass;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1883)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 1884)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: po 11. XII 10:42:23 2006
+# Generated by qmake (2.00a) (Qt 4.1.2) on: st 13. XII 00:50:24 2006
 # Project:  preprocessor.pro
 # Template: app
@@ -63,5 +63,5 @@
 	$(MAKE) -f $(MAKEFILE).Debug uninstall
 
-Makefile: preprocessor.pro  C:/Qt/4.1.2/mkspecs/win32-msvc2005\qmake.conf C:/Qt/4.1.2/mkspecs/qconfig.pri \
+Makefile: preprocessor.pro  C:/Qt/4.1.2/mkspecs/win32-msvc.net\qmake.conf C:/Qt/4.1.2/mkspecs/qconfig.pri \
 		C:\Qt\4.1.2\mkspecs\features\qt_config.prf \
 		C:\Qt\4.1.2\mkspecs\features\exclusive_builds.prf \
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.h	(revision 1877)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.h	(revision 1884)
@@ -27,4 +27,5 @@
 
 #include "Containers.h"
+#include "SamplingStrategy.h"
 
 namespace GtpVisibilityPreprocessor {
@@ -1115,6 +1116,31 @@
 };
 
+
+class RssBasedDistribution: public SamplingStrategy
+{
+ public:
+  HaltonSequence mHalton;
+  RssTree *mRssTree;
+  // id of sampling pass (a pass is defined by two consecutive updates)
+  int mPass;
+
+  RssBasedDistribution(Preprocessor &preprocessor):
+	SamplingStrategy(preprocessor){
+	mType = RSS_BASED_DISTRIBUTION;
+	mRssTree = NULL;
+	mPass = 0;
+  }
+  
+  virtual void Update(VssRayContainer &vssRays);
+  
+private:
+
+  virtual bool GenerateSample(SimpleRay &ray);
+  
 };
 
+
+};
+
 #endif // __RSSTREE_H__
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1883)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1884)
@@ -1922,6 +1922,4 @@
   
   GenerateLeafRay(params, rays, node->bbox, node->dirBBox);
-
-
 }
 
@@ -3117,5 +3115,50 @@
 
 
-
-
-}
+bool
+RssBasedDistribution::GenerateSample(SimpleRay &ray)
+{
+  float r[5];
+
+  if (mRssTree == NULL)
+	return false;
+  
+  mHalton.GetNext(5, r);
+
+  SimpleRayContainer rays;
+  mRssTree->GenerateRay(r, rays);
+  ray = rays[0];
+  
+  return false;
+}
+
+void
+RssBasedDistribution::Update(VssRayContainer &vssRays)
+{
+  if (mRssTree == NULL) {
+	mRssTree = new RssTree;
+	mRssTree->SetPass(mPass);
+	
+	/// compute view cell contribution of rays if view cells manager already constructed
+	//  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
+	
+	mRssTree->Construct(mPreprocessor.mObjects, vssRays);
+	mRssTree->stat.Print(mPreprocessor.mStats);
+	cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
+	
+  } else {
+	Debug<<"Adding rays...\n"<<flush;
+	mRssTree->AddRays(vssRays);
+	Debug<<"done.\n"<<flush;
+	if (1) {
+	  //	if (mUpdateSubdivision) {
+	  Debug<<"Updating rss tree...\n"<<flush;
+	  int subdivided = mRssTree->UpdateSubdivision();
+	  Debug<<"done.\n"<<flush;
+	  cout<<"subdivided leafs = "<<subdivided<<endl;
+	  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
+	}
+  }
+  mPass++;
+}
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1883)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1884)
@@ -10,5 +10,5 @@
 
 
-SamplingStrategy::SamplingStrategy(const Preprocessor &preprocessor): 
+SamplingStrategy::SamplingStrategy(Preprocessor &preprocessor): 
   mPreprocessor(preprocessor), mRatio(1.0f),  mTotalRays(0), mTotalContribution(0.0f)
 {
@@ -401,9 +401,4 @@
 
 
-bool
-RssBasedDistribution::GenerateSample(SimpleRay &ray)
-{
-  return false;
-}
 
 
@@ -463,23 +458,95 @@
 MixtureDistribution::Init()
 {
-  
-}
-  
-  // Generate a new sample according to a mixture distribution
+  for (int i=0; i < mDistributions.size(); i++) {
+	// small non-zero value
+	mDistributions[i]->mRays = 1;
+	// unit contribution per ray
+	if (1 || mDistributions[i]->mType != RSS_BASED_DISTRIBUTION)
+	  mDistributions[i]->mContribution = 1.0f;
+	else
+	  mDistributions[i]->mContribution = 0.0f;
+  }
+  UpdateRatios();
+}
+
+void
+MixtureDistribution::Reset()
+{
+  for (int i=0; i < mDistributions.size(); i++) {
+	// small non-zero value
+	mDistributions[i]->mRays = 1;
+	// unit contribution per ray
+	mDistributions[i]->mContribution = 1.0f;
+  }
+  UpdateRatios();
+}
+
+// Generate a new sample according to a mixture distribution
 bool
 MixtureDistribution::GenerateSample(SimpleRay &ray)
 {
-
-  return false;
+  float r;
+  mHalton.GetNext(1, &r);
+
+  // pickup a distribution
+  for (int i=0; i < mDistributions.size(); i++)
+	if (r < mDistributions[i]->mRatio)
+	  break;
+  
+  return mDistributions[i]->GenerateSample(ray);
 }
 
   // add contributions of the sample to the strategies
 void
-MixtureDistribution::UpdateContributions(VssRayContainer &vssRays)
-{
-
-}
-
-}
-
-
+MixtureDistribution::ComputeContributions(VssRayContainer &vssRays)
+{
+  int i;
+  
+  VssRayContainer::iterator it = vssRays.begin();
+
+  for(; it != vssRays.end(); ++it) {
+	VssRay *ray = *it;
+	for (i=0; i < mDistributions.size()-1; i++)
+	  if (mDistributions[i]->mType == ray->mDistribution)
+		break;
+	float contribution =
+	  mPreprocessor.mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
+	mDistributions[i]->mContribution + contribution;
+	mDistributions[i]->mRays ++;
+  }
+
+  // now update the distributions with all the rays
+  for (i=0; i < mDistributions.size(); i++)
+	mDistributions[i]->Update(vssRays);
+  
+  
+  UpdateRatios();
+}
+
+void
+MixtureDistribution::UpdateRatios()
+{
+  // now compute importance (ratio) of all distributions
+  float sum = 0.0f;
+  int i;
+  for (i=0; i < mDistributions.size(); i++) {
+	float importance = mDistributions[i]->mContribution/mDistributions[i]->mRays;
+	mDistributions[i]->mRatio = importance;
+	sum += importance;
+  }
+  
+  
+  mDistributions[0]->mRatio /= sum;
+  for (i=1; i < mDistributions.size(); i++) {
+	float r = mDistributions[i]->mRatio / sum;
+	mDistributions[i]->mRatio = mDistributions[i-1]->mRatio + r;
+  }
+  
+  cout<<"ratios: ";
+  for (i=0; i < mDistributions.size(); i++)
+	cout<<mDistributions[i]->mRatio<<" ";
+  cout<<endl;
+}
+}
+
+
