Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1421)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1444)
@@ -348,14 +348,17 @@
 		  << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
-
-	///////////////////////////////////////////
-	//-- Initial hierarchy construction finished
+	// collect view cells and compute statistics
+	ResetViewCells();
+
+
+	///////////////////
+	//-- Initial hierarchy construction finished.
 	//-- We can do some stats and visualization
 	
-	ResetViewCells();
-  		
-	if (0) //-- optionally export initial view cell partition
-	{
-        Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
+	if (0)
+	{
+		//-- export initial view cell partition
+		Debug << "\nView cells after initial sampling:\n" 
+			  << mCurrentViewCellsStats << endl;
 
 		const string filename("viewcells.wrl");
@@ -400,7 +403,5 @@
 
 		const int samplingType = mSamplingType;
-			/*dirSamples ? 
-						Preprocessor::DIRECTION_BASED_DISTRIBUTION :
-				 		Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;*/
+			//dirSamples ? Preprocessor::DIRECTION_BASED_DISTRIBUTION :	Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
 
 		if (0) dirSamples = !dirSamples; // toggle sampling method
@@ -445,8 +446,7 @@
 	//-- post processing of the initial construction
 	//-- We can bottom-up merge the view cells in this step
-
-
-	// we can additionally cast some post processing sample rays.
-	// These rays can be used to store the view cells with the rays
+	//-- We can additionally cast some post processing sample rays.
+	//-- These rays can be used to store the view cells with the rays
+
 	VssRayContainer postProcessSamples;
 	cout << "casting " << mPostProcessSamples << " post processing samples ... ";
@@ -457,5 +457,5 @@
 	cout << "starting post processing and visualization" << endl;
 
-	// store view cells for post processing?
+	// store view cells with rays for post processing?
 	const bool storeViewCells = true;
 
@@ -480,5 +480,4 @@
 		EvalViewCellPartition();
 	}
-
 	
 	/////////////////
@@ -497,9 +496,12 @@
 			ComputeSampleContributions(visualizationSamples, true, storeViewCells);
 
-		// different visualizations
+		// various visualizations
 		Visualize(preprocessor->mObjects, visualizationSamples);
 
 		disposeRays(visualizationSamples, outRays);
 	}
+
+	// recalculate view cells
+	EvaluateViewCellsStats();
 
 	return numSamples;
@@ -614,5 +616,4 @@
 		long startTime = GetTime();
 
-		//vm->PrepareLoadedViewCells();
 		vm->ResetViewCells();
 
@@ -620,7 +621,7 @@
 		vm->mMaxPvsSize = (int)objects->size();
 
-		// create the meshes and compute volumes
 		if (finalizeViewCells)
 		{
+			// create the meshes and compute volumes
 			vm->FinalizeViewCells(true);
 			vm->mViewCellsTree->AssignRandomColors();
@@ -901,7 +902,6 @@
 		startTime = GetTime();
 
-		cout << "compute new statistics ... ";
-		Debug << "compute new statistics ... ";
-
+		cout << "compute new statistics ... " << endl;
+	
 		//-- propagate pvs or pvs size information
 		ObjectPvs pvs;
@@ -916,5 +916,5 @@
 		timeDiff = TimeDiff(startTime, GetTime());
 		cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
-		Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
+		Debug << "statistis compted in " << timeDiff * 1e-3 << " secs" << endl;
 	
 		disposeRays(evaluationSamples, NULL);
@@ -922,4 +922,5 @@
 	
 
+	////////////7
 	//-- histogram
 
@@ -1286,41 +1287,41 @@
 bool ViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
 {
-  if (!ViewCellsConstructed())
-	  return mViewSpaceBox.IsInside(viewPoint);
-  else 
-  {
-	  if (!mViewSpaceBox.IsInside(viewPoint))
-		  return false;
-	  
-	  ViewCell *viewcell = GetViewCell(viewPoint);
-	  
-	  if (!viewcell || !viewcell->GetValid())
-		  return false;
-  }
-
-  return true;
-}
-
-
-float
-ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
-											 const bool addRays,									
-											 const bool storeViewCells
-											 )
-{
-  // view cells not yet constructed
-  if (!ViewCellsConstructed())
-	return 0.0f;
-
-  VssRayContainer::const_iterator it, it_end = rays.end();
-
-  float sum = 0.0f;
-
-  for (it = rays.begin(); it != it_end; ++ it) 
-  {
-	  sum += ComputeSampleContribution(*(*it), addRays, storeViewCells);
-  }
-
-  return sum;
+	if (!ViewCellsConstructed())
+	{
+		return mViewSpaceBox.IsInside(viewPoint);
+	}
+	else 
+	{
+		if (!mViewSpaceBox.IsInside(viewPoint))
+			return false;
+
+		ViewCell *viewcell = GetViewCell(viewPoint);
+
+		if (!viewcell || !viewcell->GetValid())
+			return false;
+	}
+
+	return true;
+}
+
+
+float ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
+												   const bool addRays,									
+												   const bool storeViewCells)
+{
+	// view cells not yet constructed
+	if (!ViewCellsConstructed())
+		return 0.0f;
+	
+	float sum = 0.0f;
+
+	VssRayContainer::const_iterator it, it_end = rays.end();
+	
+	for (it = rays.begin(); it != it_end; ++ it) 
+	{
+		sum += ComputeSampleContribution(*(*it), addRays, storeViewCells);
+	}
+
+	return sum;
 }
 
@@ -1345,9 +1346,11 @@
 												float &avgRenderCost)
 {
-	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
-
+	////////////
 	//-- compute expected value
+
 	totalRenderCost = 0;
 	totalPvs = 0;
+
+	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
 
 	for (it = mViewCells.begin(); it != it_end; ++ it)
@@ -1364,5 +1367,7 @@
 
 
+	///////////
 	//-- compute standard defiation
+
 	variance = 0;
 	deviation = 0;
@@ -1727,7 +1732,5 @@
 	CollectViewCells();
 	
-	
 	// stats are computed once more
-	mCurrentViewCellsStats.Reset();
 	EvaluateViewCellsStats();
 
@@ -2554,6 +2557,4 @@
 
 	Debug << mBspTree->GetStatistics() << endl;
-
-	//EvaluateViewCellsStats();
 	Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
 
@@ -2630,7 +2631,7 @@
     int vcSize = 0;
 	int pvsSize = 0;
-
-	
-	///////////////////////
+	
+
+	//////////////////
 	//-- merge leaves of the view cell hierarchy	
 	
@@ -2668,7 +2669,6 @@
 	
 	////////////////////////
-	//-- visualization and statistics
-
-	// export statistics after merge
+	//-- visualization and statistics after merge
+
 	if (1)
 	{
@@ -2686,8 +2686,7 @@
 	if (1) // export merged view cells
 	{
-		mColorCode = 0; // hack color code
+		mColorCode = 0; // use random colors
 		Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
 		
-
 		cout << "exporting view cells after merge ... ";
 
@@ -3241,5 +3240,4 @@
 	// create the view cells
 	mKdTree->CreateAndCollectViewCells(mViewCells);
-
 	// cast rays
 	ComputeSampleContributions(rays, true, false);
@@ -3623,4 +3621,5 @@
     cout << "finished" << endl;
 
+	/////////////////
 	//-- stats after construction
 
@@ -3631,9 +3630,10 @@
 
 
+	//////////////////////
+	//-- recast rest of rays
 	startTime = GetTime();
 
 	cout << "Computing remaining ray contributions ... ";
 
-	// recast rest of rays
 	if (SAMPLE_AFTER_SUBDIVISION)
 		ComputeSampleContributions(savedRays, true, false);
@@ -3646,5 +3646,4 @@
 	cout << "construction finished" << endl;
 
-	
 	if (0)
 	{
@@ -3696,6 +3695,4 @@
 	}
 
-	//-- stats and visualizations
-	cout << "finished merging" << endl;
 	cout << "merged view cells in "
 		 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
@@ -3704,4 +3701,7 @@
 		  << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
 	
+
+	//////////////////
+	//-- stats and visualizations
 
 	int savedColorCode = mColorCode;
@@ -3710,6 +3710,5 @@
 	ResetViewCells();
 	Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
-
-	//BspLeaf::NewMail();
+	
 	if (1) // export merged view cells
 	{
@@ -3892,13 +3891,13 @@
 	if (0) FinalizeViewCells(false);
 		
-	//////////////////
+	//////////
 	//-- merge the individual view cells
 	MergeViewCells(postProcessRays, objects);
 	
-	/////////////////////////////////
-	//-- refines the merged view cells
+	// refines the merged view cells
 	if (0) RefineViewCells(postProcessRays, objects);
 
-	//////////////////
+
+	///////////
 	//-- render simulation after merge + refine
 
@@ -3934,8 +3933,8 @@
 	// compute final meshes and volume / area
 	if (1) FinalizeViewCells(true);
-cout << "here09*******************" << endl;
+
 	// write view cells to disc
 	if (0 && mExportViewCells)
-	{cout << "here77*******************" << endl;
+	{
 		char filename[100];
 		Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
@@ -4793,7 +4792,11 @@
 	}
 
-	// print subdivision statistics
+	////////////
+	//-- print satistics for subdivision and view cells
+
 	Debug << endl << endl << *mHierarchyManager << endl;
-	//mHierarchyManager->PrintHierarchyStatistics(Debug);
+
+	ResetViewCells();
+	Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
 
 	if (0)
@@ -4803,13 +4806,11 @@
 	}	
 
-	// print view cell statistics
-	ResetViewCells();
-	Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
-
-
+
+	//////////
+	//-- recast rest of rays
+	
 	const long startTime = GetTime();
 	cout << "Computing remaining ray contributions ... ";
 
-	// recast rest of rays
 	if (SAMPLE_AFTER_SUBDIVISION)
 		ComputeSampleContributions(savedRays, true, false);
@@ -5487,6 +5488,6 @@
 
 			// for directional sampling it is important to count only contributions
-			// made in one direction!!!
-			// the other contributions of this sample will be counted for the oposite ray!
+			// made in one direction!
+			// the other contributions of this sample will be counted for the opposite ray!
 #if SAMPLE_ORIGIN_OBJECTS
 			if (ray.mOriginObject && 
@@ -5590,6 +5591,7 @@
 		mViewCellsTree->ResetPvs();
 	}
-	else // start from current sampless
-	{
+	else 
+	{
+		// start from current sampless
 		// statistics before casting more samples
 		cout << "compute new statistics ... ";
