Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2006)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2011)
@@ -26,4 +26,20 @@
 
 #define Q_SEARCH_STEPS 3
+
+#define SORT_RAY_ENTRIES 1
+
+// use avg ray contribution as importance
+// if 0 the importance is evaluated from the succ of mutations
+#define USE_AVG_CONTRIBUTION 1
+
+MutationBasedDistribution::RayEntry &
+MutationBasedDistribution::GetEntry(const int index)
+{
+#if SORT_RAY_ENTRIES
+  return mRays[index];
+#else
+  return mRays[(mBufferStart+index)%mRays.size()];
+#endif
+}
 
 void
@@ -57,5 +73,11 @@
 
   int reverseCandidates = 0;
-  
+
+#if 0
+  sort(mRays.begin(), mRays.end());
+  // reset the start of the buffer
+  mBufferStart = 0;
+#endif
+
   for (int i=0; i < vssRays.size(); i++) {
 	if (vssRays[i]->mPvsContribution) {
@@ -92,4 +114,5 @@
 		*mRays[mBufferStart].mRay = *vssRays[i];
 		mRays[mBufferStart].mMutations = 0;
+		mRays[mBufferStart].mUnsuccessfulMutations = 0;
 		mRays[mBufferStart].ResetReverseMutation();
 		//		mRays[mBufferStart] = RayEntry(newRay);
@@ -97,6 +120,4 @@
 		if (mBufferStart >= mMaxRays)
 		  mBufferStart = 0;
-		
-
 	  }
 	} else {
@@ -156,8 +177,12 @@
 	cout<<"Reverse candidates:"<<reverseCandidates<<endl;
   }
-
+  
   float pContributingRays = contributingRays/(float)vssRays.size();
+  
+  cout<<"Percentage of contributing rays:"<<pContributingRays<<endl;
+  
+#if USE_AVG_CONTRIBUTION
   float importance = 1.0f/(pContributingRays + 1e-5);
-  // float importance = 1.0f;
+// float importance = 1.0f;
   // set this values for last contributingRays
   int index = mBufferStart - 1;
@@ -168,5 +193,26 @@
 	mRays[index].mImportance = importance;
   }
-
+#else
+  // use unsucc mutation samples as feedback on importance
+  for (int i=0; i < mRays.size(); i++) {
+	const float  minImportance = 0.1f;
+	const int minImportanceSamples = 20;
+	mRays[i].mImportance = minImportance +
+	  (1-minImportance)*exp(-3.0f*mRays[i].mUnsuccessfulMutations/minImportanceSamples);
+	
+	//mRays[i].mImportance = 1.0f/(mRays[i].mUnsuccessfulMutations+3);
+	//	mRays[i].mImportance = 1.0f;
+  }
+#endif
+  
+#if SORT_RAY_ENTRIES
+  long t1 = GetTime();
+  sort(mRays.begin(), mRays.end());
+  // reset the start of the buffer
+  mBufferStart = 0;
+  mLastIndex = mRays.size();
+  cout<<"Mutation candidates sorted in "<<TimeDiff(t1, GetTime())<<" ms."<<endl;
+#endif
+  
 #if MUTATION_USE_CDF
   // compute cdf
@@ -184,5 +230,9 @@
 	GetEntry(0).mImportance<<" "<<
 	GetEntry(mRays.size()-1).mImportance<<endl;
-  
+
+  cout<<"Sampling factor = "<<
+	GetEntry(0).GetSamplingFactor()<<" "<<
+	GetEntry(mRays.size()-1).GetSamplingFactor()<<endl;
+
   cerr<<"Mutation update done."<<endl;
 }
@@ -516,4 +566,19 @@
 
 #if !MUTATION_USE_CDF
+#if SORT_RAY_ENTRIES
+  index = mLastIndex - 1;
+  if (index < 0 || index >= mRays.size()-1) {
+	index = mRays.size() - 1;
+  } else
+	if (
+		mRays[index].GetSamplingFactor() >= mRays[mLastIndex].GetSamplingFactor()) {
+	  // make another round
+
+	  //	  cout<<"R2"<<endl;
+	  //	  cout<<mLastIndex<<endl;
+	  //	  cout<<index<<endl;
+	  index = mRays.size() - 1;
+	}
+#else
   // get tail of the buffer
   index = (mLastIndex+1)%mRays.size();
@@ -534,4 +599,5 @@
 	index = (index+1)%mRays.size();
   }
+#endif
 #else
   static HaltonSequence iHalton;
@@ -558,5 +624,6 @@
 
   mLastIndex = index;
-
+  //  Debug<<index<<" "<<mRays[index].GetSamplingFactor()<<endl;
+  
   if (mRays[index].HasReverseMutation()) {
 	//cout<<"R "<<mRays[index].mutatedOrigin<<" "<<mRays[index].mutatedTermination<<endl;
@@ -768,3 +835,4 @@
 
 
-}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/run_test2
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 2008)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/run_test2	(revision 2011)
@@ -7,5 +7,6 @@
 #VIEWCELLS=../data/vienna/vienna-viewcells-5000.xml
 
-#SCENE=../data/soda/soda5.dat
+
+#SCENE=../data/soda/soda.obj
 #VIEWCELLS=../data/soda/soda5-viewcells-1000.xml
 #VIEWCELLS=../data/soda/soda5-viewcells-single.xml
@@ -49,7 +50,8 @@
 $COMMAND -preprocessor=combined -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
  -rss_distributions=mutation+object_direction+spatial -view_cells_filter_max_size=1 \
- -preprocessor_visibility_file=$PREFIX-i-mixed-b1-n4h.xml \
- -preprocessor_stats=$PREFIX-i-mixed-b1-n4h.log \
- -preprocessor_histogram_file=$PREFIX-i-mixed-b1-n4h.hlog
+ -view_cells_use_kd_pvs- -af_use_kd_pvs- \
+ -preprocessor_visibility_file=$PREFIX-i-mixed-b1-n4i.xml \
+ -preprocessor_stats=$PREFIX-i-mixed-b1-n4i.log \
+ -preprocessor_histogram_file=$PREFIX-i-mixed-b1-n4i.hlog
 
 
@@ -58,4 +60,5 @@
 # g - gaussian origin, q=2, reverse samples
 
+# g, h contain pvs error estimations...
 
 # $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2010)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2011)
@@ -1613,4 +1613,5 @@
 		"false");
 
+	
 	RegisterOption("ViewCells.useKdPvsAfterFiltering",
 				   optBool, 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2008)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2011)
@@ -1203,5 +1203,5 @@
 #if 1
   mRayCaster->SortRays(rays);
-  cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" s."<<endl;
+  cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" ms."<<endl;
 
   if (0) {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 2008)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/default.env	(revision 2011)
@@ -61,6 +61,6 @@
 #	type render
 	detectEmptyViewSpace true
-#	pvsRenderErrorSamples 0
-	pvsRenderErrorSamples 10000
+	pvsRenderErrorSamples 0
+#	pvsRenderErrorSamples 10000
 	quitOnFinish false
 	computeVisibility true
@@ -111,5 +111,4 @@
 
 	useImportanceSampling true
-
 
 	Export {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Makefile
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 2008)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Makefile	(revision 2011)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.2) on: ne 21. I 22:47:46 2007
+# Generated by qmake (2.00a) (Qt 4.1.2) on: po 22. I 08:54:12 2007
 # Project:  preprocessor.pro
 # Template: app
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.h	(revision 2001)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.h	(revision 2011)
@@ -57,4 +57,9 @@
 	
 	float GetSamplingFactor() const { return mMutations/mImportance; }
+	
+	friend bool operator<(const RayEntry &a, const RayEntry &b) {
+	  return a.GetSamplingFactor() > b.GetSamplingFactor();
+	}
+	
 	RayEntry() {}
 	RayEntry(VssRay *r):mRay(r),
@@ -118,7 +123,6 @@
 						 );
 
-  RayEntry &GetEntry(const int index) {
-	return mRays[(mBufferStart+index)%mRays.size()];
-  }
+  RayEntry &GetEntry(const int index);
+
   
   vector<RayEntry> mRays;
