Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1902)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1903)
@@ -39,7 +39,8 @@
 class ViewCellsTree;
 class ViewCell;
-
+class MixtureDistribution;
 struct AxisAlignedPlane;
 struct BspRay;
+
 
   struct PvsFilterStatistics {
@@ -596,4 +597,7 @@
 						VssRayContainer &vssRays) const;
 
+	int CastPassSamples2(const int samplesPerPass, 
+						 VssRayContainer &passSamples) const;
+
 	/** Parse the options from the environment file.
 	*/
@@ -756,4 +760,6 @@
 
 	bool mUseKdPvs;
+
+	MixtureDistribution *mMixtureDistribution;
 
 	//////////////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1899)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1903)
@@ -1826,9 +1826,8 @@
 
 		if (onlyMailedRays && !ray->Mailed())
-		{//if (onlyMailedRays)cout << "u";
+		{
 			continue;
-		}//else if (onlyMailedRays) cout << "z";
-
-		//ray->Mail();
+		}
+
 		++ numRays;
 
@@ -1846,5 +1845,5 @@
 			if (!useMailBoxing || !vc->Mailed())
 			{
-				if (useMailBoxing)
+				if (useMailBoxing) // => view cell not mailed
 				{
 					vc->Mail();
@@ -1854,4 +1853,5 @@
 					}
 				}
+
 				viewCells.push_back(vc);
 			}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1902)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1903)
@@ -79,4 +79,35 @@
 
 
+int ViewCellsManager::CastPassSamples2(const int samplesPerPass, VssRayContainer &passSamples) const
+{
+	long startTime = GetTime();
+
+	SimpleRayContainer simpleRays;
+	
+	simpleRays.reserve(samplesPerPass);
+	passSamples.reserve(samplesPerPass * 2); // always creates double rays
+
+	// create one third of each type
+	int castRays = 0;
+
+	mMixtureDistribution->GenerateSamples(mSamplesPerPass, simpleRays);
+
+	cout << "generated " << simpleRays.size() << " samples in " 
+		 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+	startTime = GetTime();
+
+	// shoot simple ray and add it to importance samples
+	mPreprocessor->CastRays(simpleRays, passSamples, true);
+
+    cout << "cast " <<  samplesPerPass << " samples in " 
+		 << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+	mMixtureDistribution->ComputeContributions(passSamples);
+
+	return (int)passSamples.size();
+}
+
+
 void ViewCellsManager::ParseEnvironment()
 {
@@ -123,13 +154,5 @@
 		mStrategies.push_back(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION);
 	}
-	else
-	{
-		mStrategies.push_back(SamplingStrategy::OBJECT_BASED_DISTRIBUTION);
-		//mStrategies.push_back(SamplingStrategy::VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-		mStrategies.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-		//mStrategies.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-		//mStrategies.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-	}
-
+	
 	
     Debug << "casting strategies: ";
@@ -137,4 +160,12 @@
 		Debug << mStrategies[i] << " ";
 	Debug << endl;
+
+	// now decode distribution string
+	char buff[1024];
+	Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions", buff);
+
+	mMixtureDistribution = new MixtureDistribution(*mPreprocessor);
+	mMixtureDistribution->Construct(buff);
+
 
 	// sampling type for view cells construction samples
@@ -278,5 +309,8 @@
 		DEL_PTR(mViewCellsTree);
 	}
-}
+
+	DEL_PTR(mMixtureDistribution);
+}
+
 
 Intersectable *
@@ -2028,81 +2062,83 @@
 											const bool storeViewCells)
 {
-  ray.mPvsContribution = 0;
-  ray.mRelativePvsContribution = 0.0f;
-  
-  if (ray.mTerminationObject==NULL)
-	return 0.0f;
-  
-  ViewCellContainer viewcells;
-  
-  static Ray hray;
-  hray.Init(ray);
-
-  float tmin = 0, tmax = 1.0;
-  
-  if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
-	return 0;
-  
-  Vector3 origin = hray.Extrap(tmin);
-  Vector3 termination = hray.Extrap(tmax);
-  
-  ViewCell::NewMail();
-  
-  // traverse the view space subdivision
-  CastLineSegment(origin, termination, viewcells);
-  
-  if (storeViewCells)
+	ray.mPvsContribution = 0;
+	ray.mRelativePvsContribution = 0.0f;
+
+	if (ray.mTerminationObject==NULL)
+		return 0.0f;
+
+	ViewCellContainer viewcells;
+
+	static Ray hray;
+	hray.Init(ray);
+
+	float tmin = 0, tmax = 1.0;
+
+	if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
+		return 0;
+
+	Vector3 origin = hray.Extrap(tmin);
+	Vector3 termination = hray.Extrap(tmax);
+
+	ViewCell::NewMail();
+
+	// traverse the view space subdivision
+	CastLineSegment(origin, termination, viewcells);
+
+	if (storeViewCells)
 	{	
-	  // copy viewcells memory efficiently
-	  ray.mViewCells.reserve(viewcells.size());
-	  ray.mViewCells = viewcells;
-	}
-  
-  ViewCellContainer::const_iterator it = viewcells.begin();
-  
-  Intersectable *terminationObj = GetIntersectable(ray, true);
-  
-  
-  for (; it != viewcells.end(); ++ it) 
-	{
-	  ViewCell *viewcell = *it;
-	  
-	  if (viewcell->GetValid())
-		{	// if ray not outside of view space
-		  float contribution;
-		  
-		  if (terminationObj) 
+		// copy viewcells memory efficiently
+		ray.mViewCells.reserve(viewcells.size());
+		ray.mViewCells = viewcells;
+	}
+
+	ViewCellContainer::const_iterator it = viewcells.begin();
+
+	Intersectable *terminationObj = GetIntersectable(ray, true);
+
+
+	for (; it != viewcells.end(); ++ it) 
+	{
+		ViewCell *viewcell = *it;
+
+		if (viewcell->GetValid())
+		{	
+			// if ray not outside of view space
+			float contribution;
+
+			if (terminationObj) 
 			{
-			  // todo: maybe not correct for kd node pvs
-			  bool absContribution = viewcell->GetPvs().GetSampleContribution(
-																			  terminationObj,
-																			  ray.mPdf,
-																			  contribution);
+				// todo: maybe not correct for kd node pvs
+				bool absContribution = 
+					viewcell->GetPvs().GetSampleContribution(
+															terminationObj,
+															ray.mPdf,
+															contribution);
 #if DIST_WEIGHTED_CONTRIBUTION
-			  // clear the relative contribution
-			  contribution = 0.0f;
+				// clear the relative contribution
+				contribution = 0.0f;
 #endif
-			  if (absContribution)
+				if (absContribution)
 				{
-				  ++ ray.mPvsContribution;
-				  if (addRays) {
+					++ ray.mPvsContribution;
+					if (addRays) {
 #if PVS_ADD_DIRTY
-					viewcell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf);
-					if (viewcell->GetPvs().RequiresResort()) {
-					  viewcell->GetPvs().SimpleSort();
+						viewcell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf);
+						if (viewcell->GetPvs().RequiresResort()) {
+							viewcell->GetPvs().SimpleSort();
+						}
+#else
+						viewcell->GetPvs().AddSample(terminationObj, ray.mPdf);
+#endif
 					}
-#else
-					viewcell->GetPvs().AddSample(terminationObj, ray.mPdf);
-#endif
-				  }
 #if DIST_WEIGHTED_CONTRIBUTION 
-				  // recalculate the contribution - weight the 1.0f contribution by the sqr distance to the
-				  // object-> a new contribution in the proximity of the viewcell has a larger weight!
-				  contribution = 1.0f/SqrDistance(GetViewCellBox(viewcell).Center(),
-												  ray.mTermination);
+					// recalculate the contribution - weight the 1.0f contribution by the sqr distance to the
+					// object-> a new contribution in the proximity of the viewcell has a larger weight!
+					contribution = 1.0f/SqrDistance(GetViewCellBox(viewcell).Center(),
+						ray.mTermination);
 #endif
 				}
-			  
-			  ray.mRelativePvsContribution += contribution;
+
+				ray.mRelativePvsContribution += contribution;
 			}
 		}
@@ -6016,13 +6052,5 @@
 		evalStrats.push_back(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION);
 	}
-	else
-	{
-		//evalStrats.push_back(SamplingStrategy::VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-		evalStrats.push_back(SamplingStrategy::OBJECT_BASED_DISTRIBUTION);
-		evalStrats.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-		//evalStrats.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-		//evalStrats.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
-	}
-	
+		
     Debug << "casting eval strategies: ";
 	for (int i = 0; i < (int)evalStrats.size(); ++ i)
@@ -6048,7 +6076,14 @@
 		Debug << "casting " << samplesPerPass << " samples ... ";
 	
-		CastPassSamples(samplesPerPass, evalStrats, evaluationSamples);
-		//CastPassSamples(samplesPerPass, mStrategies, evaluationSamples);
-		
+		if (1)
+		{
+			CastPassSamples(samplesPerPass, evalStrats, evaluationSamples);
+		}
+		else
+		{
+			// use mixed distributions
+			CastPassSamples2(samplesPerPass, evaluationSamples);
+		}
+
 		castSamples += samplesPerPass;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1877)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1903)
@@ -800,5 +800,5 @@
 						 ViewCellContainer &viewCells,
 						 const bool setCounter,
-						 const bool onlyUnmailedRays) const;
+						 const bool onlyMailedRays) const;
 
 	/** Counts the view cells of this object. note: only
@@ -817,5 +817,5 @@
 						 const bool useMailBoxing,
 						 const bool setCounter,
-						 const bool onlyUnmailedRays) const;
+						 const bool onlyMailedRays) const;
 
 	/** Evaluates increase in pvs size.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1899)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1903)
@@ -1451,5 +1451,5 @@
 	// get subset of rays
 	RayInfoContainer randomRays;
-	randomRays.reserve(mMaxTests);
+	randomRays.reserve(min(mMaxTests, (int)tData.mRays->size()));
 
 	RayInfoContainer *usedRays;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1892)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1903)
@@ -265,5 +265,4 @@
 	//PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
 	PreprocessorThread *pt;
-cout << "here8"<<endl;
 
 #if USE_QT
