Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 467)
@@ -29,4 +29,10 @@
 
 public:
+  struct PvsStatistics {
+	int minPvs;
+	int maxPvs;
+	float avgPvs;
+	int viewcells;
+  };
   
   /// view cell container types
@@ -165,4 +171,8 @@
 							  ) = 0;
 
+  virtual void GetPvsStatistics(PvsStatistics &stat);
+
+  virtual void
+  PrintPvsStatistics(ostream &s);
 
 protected:
@@ -183,5 +193,5 @@
 
 	/// Loaded view cells
-	ViewCellContainer mViewCells;
+  ViewCellContainer mViewCells;
 
 	/// maximum number of samples taken for construction of the view cells
@@ -234,4 +244,5 @@
 					  );
 
+  
 protected:
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 464)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 467)
@@ -269,5 +269,8 @@
 };
 
-typedef  vector<VssRay *> VssRayContainer;
+struct VssRayContainer : public vector<VssRay *>
+{
+  void PrintStatistics(ostream &s);
+};
 
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 467)
@@ -12,5 +12,5 @@
 #include "RenderSimulator.h"
 
-static bool useViewSpaceBox = true;
+static bool useViewSpaceBox = false;
 static bool use2dSampling = false;
 static bool fromBoxVisibility = false;
@@ -219,10 +219,10 @@
   
   cout<<
-	"#AVG_PVS_SIZE\n"<<avgPvsSize<<endl<<
-	"#AVG_RAYS\n"<<avgRays<<endl<<
-	"#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl<<
-	"#AVG_PVS_ENTROPY\n"<<avgPvsEntropy<<endl<<
-	"#AVG_RAY_LENGTH_ENTROPY\n"<<avgRayLengthEntropy<<endl<<
-	"#AVG_IMPORTANCE\n"<<avgImportance<<endl;
+	"#RSS_AVG_PVS_SIZE\n"<<avgPvsSize<<endl<<
+	"#RSS_AVG_RAYS\n"<<avgRays<<endl<<
+	"#RSS_AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl<<
+	"#RSS_AVG_PVS_ENTROPY\n"<<avgPvsEntropy<<endl<<
+	"#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<avgRayLengthEntropy<<endl<<
+	"#RSS_AVG_IMPORTANCE\n"<<avgImportance<<endl;
   
   if (0) {
@@ -466,7 +466,5 @@
 	}
     
-	mPass++;
-		
-	int pvsSize = 0;
+		
 	float avgRayContrib = (passContributingSamples > 0) ? 
 	  passSampleContributions/(float)passContributingSamples : 0;
@@ -475,7 +473,6 @@
 	cout << "#TotalSamples=" << totalSamples/1000 
 		 << "k   #SampleContributions=" << passSampleContributions << " (" 
-		 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
-		 << pvsSize/(float)mObjects.size() << endl 
-		 << "avg ray contrib=" << avgRayContrib << endl;
+		 << 100*passContributingSamples/(float)passSamples<<"%)" 
+		 << "avgcontrib=" << avgRayContrib << endl;
 		
 	mStats <<
@@ -485,6 +482,8 @@
 	  "#SampleContributions\n" << passSampleContributions << endl << 
 	  "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
-	  "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
 	  "#AvgRayContrib\n" << avgRayContrib << endl;
+	
+	mPass++;
+
   }
   
@@ -499,33 +498,58 @@
   }
   
-  if (mUseViewcells)
+  if (mUseViewcells) {
 	// construct view cells
 	mViewCellsManager->Construct(mObjects, mVssRays, mViewSpaceBox);
+	
+	VssRayContainer selectedRays;
+	int desired = Max(
+					  mViewCellsManager->GetPostProcessSamples(),
+					  mViewCellsManager->GetVisualizationSamples());
+	float p = desired/(float)mVssRays.size();
+	//	rssTree->CollectRays(storedRays, desired);
+	for (int i=0; i < mVssRays.size(); i++) {
+	  if (Random(1.0f) < p)
+		selectedRays.push_back(mVssRays[i]);
+	}
+	//-- post process view cells
+	mViewCellsManager->PostProcess(mObjects, selectedRays);
+	
+	//-- several visualizations and statistics
+	mViewCellsManager->PrintStatistics(Debug);
+	
+	if (1) 
+	  mViewCellsManager->Visualize(mObjects, selectedRays);
+
+	CLEAR_CONTAINER(storedRays);
+  }
+
 
   rssTree = new RssTree;
 	
-  if (fromBoxVisibility)
-	rssTree->Construct(mVssRays, mViewSpaceBox);
-  else
-	rssTree->Construct(mVssRays, NULL);
-
-  cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
-
-  if (mExportRssTree) {
-	ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
-	ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
-	ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));
-	ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));
-	ExportRssTreeLeaves(rssTree, 10);
-  }
-
-  if (mExportPvs) {
-	ExportPvs("rss-pvs-initial.x3d", rssTree);
-  }
-
+  if (mUseImportanceSampling) {
+	
+	if (fromBoxVisibility)
+	  rssTree->Construct(mVssRays, mViewSpaceBox);
+	else
+	  rssTree->Construct(mVssRays, NULL);
+	cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
+	
+	if (mExportRssTree) {
+	  ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
+	  ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
+	  ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));
+	  ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));
+	  ExportRssTreeLeaves(rssTree, 10);
+	}
+	
+	if (mExportPvs) {
+	  ExportPvs("rss-pvs-initial.x3d", rssTree);
+	}
+  }
+  
   // viewcells->UpdatePVS(newVssRays);
   
-  int samples = 0;
-  int pass = 0;
+  int rssPass = 0;
+  int rssSamples = 0;
   while (1) {
 	int num = mRssSamplesPerPass;
@@ -546,77 +570,72 @@
 	for (int i=0; i < rays.size(); i++)
 	  CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
-	
+
+	
+	totalSamples+=num;
+	rssSamples+=num;
+
+	mStats <<
+	  "#Pass\n" <<mPass<<endl<<
+	  "#RssPass\n" <<rssPass<<endl<<
+	  "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
+	  "#TotalSamples\n" <<totalSamples<<endl<<
+	  "#RssSamples\n" <<rssSamples<<endl;
+
 	if (mUseViewcells) {
 	  
+	  /// compute view cell contribution of rays
+	  mViewCellsManager->ComputeSampleContributions(vssRays);
 	  
-	  /// compute view cell contribution of rays
-	  mViewCellsManager->ComputeSampleContributions(vssRays
-													);
+	  vssRays.PrintStatistics(mStats);
+	  mViewCellsManager->PrintPvsStatistics(mStats);
+	}
+
+	// add rays to the tree after the viewcells have been cast to have their contributions
+	// already when adding into the tree
+	// do not add those rays which have too low or no contribution....
+	
+	if (mUseImportanceSampling) {
+	  rssTree->AddRays(vssRays);
 	  
+	  if (0) {
+		cout<<"############# Rss PVS STAT ##################\n";
+		cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl;
+		cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl;
+	  }
 	  
-	  float pvs = rssTree->GetAvgPvsSize();
-	  cout<<"*****************************\n";
-	  cout<<samples<<" avgPVS ="<<pvs<<endl;
-	  cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
-	  cout<<"*****************************\n";
-	}
-
-
-
-	rssTree->AddRays(vssRays);
-	
-	if (mUpdateSubdivision) {
-	  int subdivided = rssTree->UpdateSubdivision();
-	  cout<<"subdivided leafs = "<<subdivided<<endl;
-	  cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
-	}
-
-	if (mUseViewcells) {
-	  //	  float avgPvs = GetAvgPvsSize(rssTree, kdViewcells);
-	  //	  cout<<"Average PVS size = "<<avgPvs<<endl;
-	}
-
+	  if (mUpdateSubdivision) {
+		int subdivided = rssTree->UpdateSubdivision();
+		cout<<"subdivided leafs = "<<subdivided<<endl;
+		cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
+	  }
+	}
+	
 	if (mExportRays) {
 	  char filename[64];
 	  if (mUseImportanceSampling)
-		sprintf(filename, "rss-rays-i%04d.x3d", pass);
+		sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
 	  else
-		sprintf(filename, "rss-rays-%04d.x3d", pass);
+		sprintf(filename, "rss-rays-%04d.x3d", rssPass);
 	  
 	  ExportRays(filename, vssRays, mExportNumRays);
 	}
 
-	samples+=num;
-
 	
 	if (mExportPvs) {
 	  char filename[64];
-	  sprintf(filename, "rss-pvs-%04d.x3d", pass);
+	  sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
 	  ExportPvs(filename, rssTree);
 	}
 
-	if (samples >= mRssSamples)
+	if (totalSamples >= mRssSamples + mInitialSamples)
 	  break;
 
 	
-	pass++;
+	rssPass++;
+	mPass++;
   }
   
   if (mUseViewcells) {
 
-	VssRayContainer storedRays;
-	rssTree->CollectRays(storedRays, Max(
-										 mViewCellsManager->GetPostProcessSamples(),
-										 mViewCellsManager->GetVisualizationSamples()));
-	
-	//-- post process view cells
-	mViewCellsManager->PostProcess(mObjects, storedRays);
-	
-	//-- several visualizations and statistics
-	mViewCellsManager->PrintStatistics(Debug);
-
-	mViewCellsManager->Visualize(mObjects, storedRays);
-
-	CLEAR_CONTAINER(storedRays);
 
   //-- render simulation after merge
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.cpp	(revision 464)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.cpp	(revision 467)
@@ -194,2 +194,25 @@
   }
 }
+
+
+void
+VssRayContainer::PrintStatistics(ostream &s)
+{
+
+  VssRayContainer::const_iterator it = begin(), it_end = end();
+  int sumContributions = 0;
+  float sumRelContributions = 0.0f;
+  
+  for (; it != it_end; ++it) {
+	VssRay *ray = *it;
+	sumContributions += ray->mPvsContribution;
+	sumRelContributions += ray->mRelativePvsContribution;
+  }
+  
+  s<<"##### VSS RAY STAT ##########\n";
+  s<<"#RAYS\n"<<size()<<endl;
+  s<<"#AVG_RAY_PVS_CONTRIBUTION\n"<<sumContributions/(float)size()<<endl;
+  s<<"#AVG_RAY_RELATIVE_PVS_CONTRIBUTION\n"<<sumRelContributions/
+	(float)size()<<endl;
+  
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 464)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 467)
@@ -66,5 +66,5 @@
 	initialSamples 500000
 	vssSamples 10000000
-	vssSamplesPerPass 500000
+	vssSamplesPerPass 100000
 	useImportanceSampling true
 
@@ -76,5 +76,5 @@
 	}
 
-	useViewcells false
+	useViewcells true
 	updateSubdivision false
 }
@@ -156,7 +156,7 @@
 	loadFromFile false
 	#type kdTree
-	type vspKdTree
+	#type vspKdTree
 	#type bspTree
-	#type vspBspTree
+	type vspBspTree
 	
 	#type sceneDependent
@@ -339,9 +339,9 @@
 	Termination {
 		# parameters used for autopartition
-		minRays 200
+		minRays 1000
 		minPolygons -1
 		maxDepth 40
-		minPvs 100
-		minArea 0.01
+		minPvs 20
+		minArea 200000
 		maxRayContribution 0.005
 		#maxAccRayLength 100
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 467)
@@ -208,4 +208,38 @@
 }
 
+void
+ViewCellsManager::GetPvsStatistics(PvsStatistics &stat)
+{
+  ViewCellContainer::const_iterator it = mViewCells.begin();
+  stat.viewcells = 0;
+  stat.minPvs = 100000000;
+  stat.maxPvs = 0;
+  stat.avgPvs = 0.0f;
+
+  for (; it != mViewCells.end(); ++it) {
+	ViewCell *viewcell = *it;
+	int pvsSize = viewcell->GetPvs().GetSize();
+	if ( pvsSize < stat.minPvs)
+	  stat.minPvs = pvsSize;
+	if (pvsSize > stat.maxPvs)
+	  stat.maxPvs = pvsSize;
+	stat.avgPvs += pvsSize;
+	stat.viewcells++;
+  }
+  if (stat.viewcells)
+	stat.avgPvs/=stat.viewcells;
+}
+
+void
+ViewCellsManager::PrintPvsStatistics(ostream &s)
+{
+  s<<"############# Viewcell PVS STAT ##################\n";
+  PvsStatistics pvsStat;
+  GetPvsStatistics(pvsStat);
+  s<<"#AVG_PVS\n"<<pvsStat.avgPvs<<endl;
+  s<<"#MAX_PVS\n"<<pvsStat.maxPvs<<endl;
+  s<<"#MIN_PVS\n"<<pvsStat.minPvs<<endl;
+}
+
 /**********************************************************************/
 /*                   BspViewCellsManager implementation               */
@@ -260,9 +294,11 @@
 		sampleRays.push_back(new Ray(*rays[i]));
 
-	if (mViewCells.empty()) // no view cells loaded
-		mBspTree->Construct(objects, sampleRays);
-	else
-		mBspTree->Construct(mViewCells);
-
+	if (mViewCells.empty()) { // no view cells loaded 
+	  mBspTree->Construct(objects, sampleRays);
+	  mBspTree->CollectViewCells(mViewCells);
+	} else
+	  mBspTree->Construct(mViewCells);
+	
+	
 	Debug << mBspTree->GetStatistics() << endl;
 	
@@ -280,4 +316,7 @@
 				  viewcells
 				  );
+
+  ray.mPvsContribution = 0;
+  ray.mRelativePvsContribution = 0.0f;
   
   ViewCellContainer::const_iterator it = viewcells.begin();
@@ -770,4 +809,5 @@
 }
 
+
 /**********************************************************************/
 /*                   KdViewCellsManager implementation               */
@@ -1186,4 +1226,5 @@
 
 	mVspBspTree->Construct(sampleRays);
+	mVspBspTree->CollectViewCells(mViewCells);
 	
 	Debug << mVspBspTree->GetStatistics() << endl;
