Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1414)
@@ -614,7 +614,5 @@
 	/// the corresponding view cell tree holding the logical description of view cells
 	ViewCellsTree *mViewCellsTree;
-	/// if empty view cells should be pruned (i.e., invalidated) from this point on
-	bool mPruneEmptyViewCells;
-
+	
 	/// if the values in the view cell leaves and the interiors are up to date
 	/// this is meant for lazy storing of the pvs, where only a scalar indicating
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1379)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1414)
@@ -1143,5 +1143,4 @@
 		 "true");
 
-
   RegisterOption("MeshKdTree.Termination.minCost",
 		 optInt,
@@ -1426,9 +1425,4 @@
 					"false");
 
-	RegisterOption("ViewCells.pruneEmptyViewCells",
-					optBool,
-					"view_cells_prune_empty=",
-					"false");
-
 	RegisterOption("ViewCells.processOnlyValidViewCells",
 					optBool,
@@ -1436,5 +1430,4 @@
 					"false");
 
-	
 	RegisterOption("ViewCells.PostProcess.maxCostRatio",
 			optFloat,
@@ -1745,4 +1738,15 @@
 					"20");
 
+	RegisterOption("Preprocessor.exportKdTree", 
+					optBool,
+					"preprocessor_export_kd_tree=",
+					"false");
+
+	RegisterOption("Preprocessor.loadKdTree", 
+					optBool,
+					"preprocessor_load_kd_tree=",
+					"false");
+
+
 
 	/************************************************************************************/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1405)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1414)
@@ -580,12 +580,14 @@
 
 
-bool Preprocessor::LoadKdTree()
-{
-	return true;
-}
-
-bool Preprocessor::ExportKdTree()
-{
-	return true;
+bool Preprocessor::LoadKdTree(const string filename)
+{
+	mKdTree = new KdTree();
+	return mKdTree->LoadBinTree(filename.c_str(), mObjects);
+}
+
+
+bool Preprocessor::ExportKdTree(const string filename)
+{
+	return mKdTree->ExportBinTree(filename.c_str());
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1404)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1414)
@@ -593,5 +593,5 @@
   cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
 
-  ExportRays("kdtree.x3d", mVssRays, 10);
+  if (0) ExportRays("kdtree.x3d", mVssRays, 10);
 
   if (0)
@@ -692,21 +692,19 @@
   if (0)
   {
-	VssRayContainer viewCellRays;
- 	// compute rays used for view cells construction
-	const int numRays = mViewCellsManager->GetVisualizationSamples();
-	vssTree->CollectRays(viewCellRays, numRays);
-  }
-
-  //-- render simulation after merge
-  cout << "\nevaluating bsp view cells render time after sampling ... ";
-  Debug << "\nStatistics after sampling: " << endl;
+	  VssRayContainer viewCellRays;
+	  // compute rays used for view cells construction
+	  const int numRays = mViewCellsManager->GetVisualizationSamples();
+	  vssTree->CollectRays(viewCellRays, numRays);
+  }
+
+
+  //////////////////////////////////
+  //-- render simulation after 
 
   mRenderSimulator->RenderScene();
   SimulationStatistics ss;
   mRenderSimulator->GetStatistics(ss);
-  
-  cout << " finished" << endl;
-  cout << ss << endl;
-  Debug << ss << endl;
+  Debug << "\nFinal view cells partition render time\n" << ss << endl;
+  cout << "\nFinal view cells partition render time\n" << ss << endl;
 
   delete vssTree;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1381)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1414)
@@ -120,6 +120,6 @@
   bool ExportSamples(const VssRayContainer &samples) const;
 
-  bool LoadKdTree();
-  bool ExportKdTree();
+  bool LoadKdTree(const string filename);
+  bool ExportKdTree(const string filename);
 
   /** Get Sample rays of particular type, returns false if this
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1408)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1414)
@@ -808,5 +808,6 @@
   
 #ifdef _DEBUG
-	cout << "\n\nobjects=(" << objectsBack.size() << "," << objectsFront.size() << " of " << tData.mNode->mObjects.size() << ")\t area=(" 
+	cout << "\n\nobjects=(" << (int)objectsBack.size() << "," << (int)objectsFront.size() << " of " 
+		 << (int)tData.mNode->mObjects.size() << ")\t area=(" 
 		 << areaLeft << "," << areaRight << ")" << endl;
 	cout << "cost= " << minSum << endl;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1404)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1414)
@@ -77,12 +77,4 @@
 	Environment::GetSingleton()->GetFloatValue("ViewCells.maxPvsRatio", mMaxPvsRatio);
 	
-	Environment::GetSingleton()->GetBoolValue("ViewCells.pruneEmptyViewCells", mPruneEmptyViewCells);
-
-	// HACK
-	if (0)
-		mMinPvsSize = mPruneEmptyViewCells ? 1 : 0;
-	else
-		mMinPvsSize = 0;
-
 	Environment::GetSingleton()->GetBoolValue("ViewCells.processOnlyValidViewCells", mOnlyValidViewCells);
 
@@ -196,6 +188,4 @@
 	Debug << "export geometry: " << mExportGeometry << endl;
 	Debug << "max pvs ratio: " << mMaxPvsRatio << endl;
-	
-	Debug << "prune empty view cells: " << mPruneEmptyViewCells << endl;
 	
 	Debug << "process only valid view cells: " << mOnlyValidViewCells << endl;
@@ -338,6 +328,6 @@
 	
 	long startTime = GetTime();
-
 	cout << "view cell construction: casting " << mInitialSamples << " initial samples ... ";
+
 	// cast initial samples
 	CastPassSamples(mInitialSamples, mSamplingType,	initialSamples);
@@ -366,16 +356,16 @@
 	
 	ResetViewCells();
-  	Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
-
-
-	//-- optionally export initial view cells
+  	if (0) Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
+
+	//-- optionally export initial view cell partition
 	if (0) 
 	{
-		const char filename[] = "view_cells.wrl";
-		Exporter *exporter = Exporter::GetExporter(filename);
+		const string filename("viewcells.wrl");
+		Exporter *exporter = Exporter::GetExporter(filename.c_str());
 	
 		if (exporter)
 		{
-			cout << "exporting initial view cells (=leaves) to " << filename << " ... ";
+			cout << "exporting initial view cells (=leaves) to " << filename.c_str() << " ... ";
+
 			if (mExportGeometry)
 			{
@@ -399,7 +389,6 @@
 	//-- note: guided rays could be used for this task
 
-	// time spent after initial construction
+	// time spent after construction of the initial partition
 	startTime = GetTime();
-
 	const int n = mConstructionSamples; //+initialSamples;
 	// should we use directional samples?
@@ -439,4 +428,5 @@
 
 	
+#if 0
 	////////////////////////////////////////////////
 	//-- get stats after the additional sampling step
@@ -452,5 +442,5 @@
 
     Debug << ss << endl;
-
+#endif
 
 
@@ -479,14 +469,12 @@
 	PostProcess(preprocessor->mObjects, postProcessSamples);
 
-	cout << "time needed for post processing (merge) step: "
-		 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
-
-	Debug << "time needed for post processing (merge) step: "
-		 << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
-
+	const float secs = TimeDiff(startTime, GetTime()) * 1e-3f;
+	cout << "post processing (=merge) finished in " << secs << " secs" << endl;
+
+	Debug << "post processing time: " << secs << endl;
 	disposeRays(postProcessSamples, outRays);
 
 	
-	/////////////////////////////////////////////////////////////////////
+	//////////////////////////////////////////////////////////////
 	//-- Evaluation of the resulting view cell partition. 
 	//-- We cast a number of new samples and measure the render cost
@@ -2616,5 +2604,6 @@
 	}
 	
-	// view cells already finished before post processing step (i.e. because they were loaded)
+	// view cells already finished before post processing step,
+	// i.e., because they were loaded from disc
 	if (mViewCellsFinished)
 	{
@@ -2625,12 +2614,11 @@
 	}
 
-	///////////////////////////////////////
-	//-- post processing of bsp view cells
-
     int vcSize = 0;
 	int pvsSize = 0;
 
-	//-- merge view cells
-
+	
+	///////////////////////
+	//-- merge leaves of the view cell hierarchy	
+	
 	cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
 	long startTime = GetTime();
@@ -2748,13 +2736,4 @@
 	}
 	
-	// export bounding boxes
-	if (0 && mExportBboxesForPvs)
-	{
-		char filename[100];
-		Environment::GetSingleton()->GetStringValue("ViewCells.boxesFilename", filename);
-		ExportBoundingBoxes(filename, objects);
-	}
-
-
 	return 0;
 }
@@ -3833,12 +3812,11 @@
 											 const ObjectContainer &objects)
 {
-	Debug << "render time before refine:" << endl;
 	mRenderer->RenderScene();
 	SimulationStatistics ss;
 	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
-    Debug << ss << endl;
-
+    Debug << "render time before refine\n" << ss << endl;
+
+	const long startTime = GetTime();
 	cout << "Refining the merged view cells ... ";
-	long startTime = GetTime();
 
 	// refining the merged view cells
@@ -3885,5 +3863,6 @@
 	else
 	{
-		minPvs = mPruneEmptyViewCells ? 1 : 0;
+		// problem matt: why did I start here from zero?
+		minPvs = 0;
 		maxPvs = mMaxPvsSize;
 	}
@@ -3904,33 +3883,30 @@
 	Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
 
-
-	// should maybe be done here to allow merge working with area or volume
-	// and to correct the rendering statistics
+	// should maybe be done here to allow merge working 
+	// with area or volume and to correct the rendering statistics
 	if (0) FinalizeViewCells(false);
 		
+	//////////////////////////////////////
 	//-- merge the individual view cells
 	MergeViewCells(postProcessRays, objects);
 	
-
-	// only for debugging purpose: test if the subdivision is valid
-	if (0) TestSubdivision();
-
+	/////////////////////////////////
 	//-- refines the merged view cells
 	if (0) RefineViewCells(postProcessRays, objects);
 
-	
+	///////////////////////////////////////////
 	//-- render simulation after merge + refine
-	cout << "\nevaluating bsp view cells render time before compress ... ";
+
+	cout << "\nview cells partition render time before compress";
 	dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
 	SimulationStatistics ss;
 	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
-  
-
-	cout << " finished" << endl;
+ 
 	cout << ss << endl;
-	Debug << ss << endl;
-
-
+	
+
+	///////////////////
 	//-- compression
+
 	if (ViewCellsTreeConstructed() && mCompressViewCells)
 	{
@@ -3960,14 +3936,4 @@
 		Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
 		ExportViewCells(filename, mExportPvs, objects);
-	}
-
-	
-	// export bounding boxes
-	if (mExportBboxesForPvs)
-	{
-		char filename[100];
-		Environment::GetSingleton()->GetStringValue("ViewCells.boxesFilename", filename);
-	
-		ExportBoundingBoxes(filename, objects);
 	}
 
@@ -4906,5 +4872,6 @@
 	else
 	{
-		minPvs = mPruneEmptyViewCells ? 1 : 0;
+		// problem matt: why did I start here from zero?
+		minPvs = 0;
 		maxPvs = mMaxPvsSize;
 	}
@@ -4916,5 +4883,5 @@
 
 	
-	// area has to be recomputed
+	// area is not up to date, has to be recomputed
 	mTotalAreaValid = false;
 	VssRayContainer postProcessRays;
@@ -4932,21 +4899,20 @@
 	mViewCellsTree->SetRoot(root);
 
-	// compute pvs
+	////////////////////////////////////
+	//-- update pvs in the whole hierarchy
 	ObjectPvs pvs;
 	UpdatePvsForEvaluation(root, pvs);
 
-	
+
+	///////////////////////////////////////////
 	//-- render simulation after merge + refine
-	cout << "\nevaluating bsp view cells render time before compress ... ";
+
+	cout << "\nview cells partition render time before compress";
 	dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
 	SimulationStatistics ss;
 	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
-  
-
-	cout << " finished" << endl;
 	cout << ss << endl;
-	Debug << ss << endl;
-
-
+	
+	///////////////////////
 	//-- compression
 
@@ -4962,8 +4928,10 @@
 	}
 
-	// compute final meshes and volume / area
+	/////////////////////////////////////
+	//-- compute final meshes and volume / area
 	if (1) FinalizeViewCells(true);
 
-	// write view cells to disc
+
+	// write out view cells
 	if (mExportViewCells)
 	{
@@ -4972,5 +4940,4 @@
 		ExportViewCells(filename, mExportPvs, objects);
 	}
-
 
 	return 0;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 1112)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 1414)
@@ -32,7 +32,4 @@
     mMin = nMin; mMax = nMax;
   }
-
-  //  AxisAlignedBox3(const Vector3 &center, const float radius):min(center - Vector3(radius)),
-  //						      max(center + Vector3(radius)) {}
 
   /** initialization to the non existing bounding box
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1387)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1414)
@@ -1319,4 +1319,5 @@
 	if (!stream.is_open()) return false;
 
+	// sort objects by their id
 	std::stable_sort(objects.begin(), objects.end(), ilt);
 
@@ -1324,13 +1325,15 @@
 	ObjectContainer::const_iterator oit, oit_end = objects.end();
 
-	//-- compute bounding box
+
+	///////////////////////////
+	//-- compute bounding box of object space
     for (oit = objects.begin(); oit != oit_end; ++ oit)
 	{
-		Intersectable *obj = *oit;
-		// compute bounding box of view space
-		mBox.Include(obj->GetBox());
-	}
-
-	DEL_PTR(mRoot); // we make a new root
+		const AxisAlignedBox3 box = (*oit)->GetBox();
+		mBox.Include(box);
+	}
+
+	// hack: we make a new root
+	DEL_PTR(mRoot);
   
 	KdNode *node = LoadNextNode(stream, NULL, objects);
@@ -1377,5 +1380,5 @@
 		{
 			EvaluateLeafStats(tData);
-			//Debug << "l" << endl;
+			cout << "l";
 		}
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1408)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1414)
@@ -180,7 +180,9 @@
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.loadKdTree", loadKdTree);
 
+	const string kdtreename("kd.bin.gz");
+
 	if (!loadKdTree)
 	{
-		//-- build kd tree from scene geometry
+		//-- build new kd tree from scene geometry
 		preprocessor->BuildKdTree();
 		preprocessor->KdTreeStatistics(cout);
@@ -188,18 +190,41 @@
 	else
 	{
+		const long startTime = GetTime();
 		cout << "loading kd tree ... ";
+
+		if (!preprocessor->LoadKdTree(kdtreename))
+		{
+			cout << "error loading kd tree with filename " << kdtreename << endl;
+			Cleanup();
+			exit(1);
+		}
+		cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+		if (0)
+		{
+			Exporter *exporter = Exporter::GetExporter("dummykd.x3d");
+			
+			if (exporter)
+			{
+				exporter->ExportKdTree(*preprocessor->mKdTree, true);
+				delete exporter;
+			}
+		}
+	}
+
+	Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
+
+	if (exportKdTree)
+	{
 		const long startTime = GetTime();
-		preprocessor->mKdTree->LoadBinTree("kd.bin.gz", preprocessor->mObjects);
-  			
-		cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-	}
-
-	Environment::GetSingleton()->GetBoolValue("Preprocessor.exportKdTree", exportKdTree);
-
-	if (exportKdTree)
-	{
 		cout << "exporting kd tree ... ";
-		preprocessor->mKdTree->ExportBinTree("kd.bin.gz");
-		cout << "finished" << endl;
+		if (!preprocessor->ExportKdTree(kdtreename))
+		{
+			cout << " error exporting kd tree with filename " << kdtreename << endl;
+		}
+		else
+		{
+			cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+		}
 	}
 
