Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env	(revision 2542)
@@ -219,5 +219,5 @@
 		###################################################
 
-		# minimal steps of same type: for interleaved, this is only valid for the first few splits
+		# minimal steps of same domain (view /object): for interleaved mode
 		minStepsOfSameType 100
 		maxStepsOfSameType 900
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2542)
@@ -520,4 +520,6 @@
 										   BoundingBoxConverter *bconverter = NULL);
 
+	/** Convenience function assuming that there are no preprocessor objects.
+	*/
 	static ViewCellsManager *LoadViewCells(const string &filename, 
 										   ObjectContainer &pvsObjects,
@@ -525,6 +527,13 @@
 										   BoundingBoxConverter *bconverter = NULL);
 
+	ViewCellsManager *LoadViewCellsBin(const string &filename, 
+	                                   ObjectContainer &pvsObjects,
+									   ObjectContainer &preprocessorObjects,
+									   bool finalizeViewCells,
+									   BoundingBoxConverter *bconverter);
+
+
 	///////////////////////
-	// visiblity filter options
+	//-- visiblity filter options
 
 	// TODO: write own visibiltiy filter class
@@ -636,5 +645,6 @@
 						const vector<int> &strategies,
 						VssRayContainer &vssRays) const;
-
+	/** cast samples for the purpose of evaluating the view cells solution
+	*/
 	int CastEvaluationSamples(const int samplesPerPass, 
 							  VssRayContainer &passSamples);// const;
@@ -660,4 +670,7 @@
 	*/
 	void EvaluateViewCellsStats();
+	/** This helper function loads the bounding boxes for a binary solution.
+	*/
+	void LoadIndexedBoundingBoxes(IN_STREAM &stream, IndexedBoundingBoxContainer &iboxes);
 
 
@@ -1135,7 +1148,11 @@
 {
 	friend class ViewCellsParseHandlers;
+	friend class ViewCellsManager;
 
 public:
 
+	/** This version takes a view cells tree and a hierarchy
+		as input.
+	*/
 	VspOspViewCellsManager(ViewCellsTree *vcTree, 
 						   const string &hierarchyType);
@@ -1196,6 +1213,4 @@
 							   const string &prefix,
 							   VssRayContainer *visRays = NULL);
-	
-	;
 	/** See parent class.
 	*/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2538)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2542)
@@ -2393,5 +2393,4 @@
 				"vsp_term_max_view_cells=",
 				"10000");
-
 	
 	RegisterOption("VspTree.Termination.missTolerance",
Index: GTP/trunk/Lib/Vis/Preprocessing/src/LogManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/LogManager.h	(revision 1112)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/LogManager.h	(revision 2542)
@@ -16,6 +16,6 @@
 	there is always only one log manager.
 */
-class LogManager {
-
+class LogManager 
+{
 public:
 
@@ -34,16 +34,13 @@
 	LogManager();
 
-  ~LogManager() {}
-  
+	~LogManager() {}
+
 	void LogMessage(const std::string &str);
 
 protected:
-
 	std::ofstream mOutStream;
 
 private:
-
 	static LogManager *sLogManager;
-	
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2542)
@@ -509,5 +509,4 @@
 	if (result) 
 	{ 
-
 		mSceneGraph->AssignObjectIds();
  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 2332)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 2542)
@@ -254,5 +254,5 @@
 {
 	//debuggerWidget = new GlDebuggerWidget(renderer);
-	//  renderer->resize(640, 480);
+	//renderer->resize(640, 480);
 	//debuggerWidget->resize(640, 480);
 
@@ -265,9 +265,11 @@
 	exporter->ExportGeometry(objects);
 	exporter->SetFilled();
+	
 	//Randomize();
-// §§matt
-//	debuggerWidget = new GlDebuggerWidget(renderer);
-
-	/*debuggerWidget->mBeam = beam;
+	//§§matt
+	//debuggerWidget = new GlDebuggerWidget(renderer);
+
+	/*
+	debuggerWidget->mBeam = beam;
 	debuggerWidget->mSourceObject = sourceObj;
 	debuggerWidget->mSamples = 10000;
@@ -276,5 +278,6 @@
 	debuggerWidget->show();
 	
-	renderer->makeCurrent();*/
+	renderer->makeCurrent();
+	*/
 
 	for (int i = 0; i < 10; ++ i)
@@ -299,6 +302,8 @@
 
 		BeamSampleStatistics stats;
-// §§matt
-/*		renderer->SampleBeamContributions(sourceObj,
+
+		// §§matt
+		/*	
+		renderer->SampleBeamContributions(sourceObj,
 										  beam,
 										  200000,
@@ -310,5 +315,6 @@
 		image.save(s, "PNG");
 		Debug << "beam statistics: " << stats << endl << endl;
-*/
+		*/
+
 		if (1)
 		{
@@ -321,5 +327,6 @@
 		}
 
-		bool exportViewCells = false;
+		const bool exportViewCells = false;
+
 		if (exportViewCells)
 		{
@@ -338,16 +345,7 @@
 				exporter->ExportViewCell(*it);
 			}
-
-			/*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
-			
-			for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
-			{
-				exporter->ExportBox(mKdTree->GetBox((*it)));
-			}*/
 		}
 	}
-	/*while (1)
-	{ debuggerWidget->repaint();
-	};*/
+	
 	delete exporter;
 }
@@ -376,10 +374,7 @@
 	int totalSamples = 0;
 
-	//mSceneGraph->CollectObjects(&mObjects);
-
 	if (!mLoadViewCells)
 	{
-		//-- generate new view cells from the scratch
-		//-- for construction the manager uses it's own set of samples
+		// generate new view cells from the scratch using coarse sampling
 		ConstructViewCells();
 	}
@@ -387,8 +382,9 @@
 	else 
 	{	 
-		//-- load view cells from file
-		//-- test successful view cells loading by exporting them again
-		VssRayContainer dummies;
-		mViewCellsManager->Visualize(mObjects, dummies);
+		// load view cells from file
+		// we test success by exporting view cells again
+		VssRayContainer dummyContainer;
+		mViewCellsManager->Visualize(mObjects, dummyContainer);
+		
 		mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
 	}
@@ -401,5 +397,7 @@
 	{
 		cout << "Loading samples from file ... ";
+		
 		LoadSamples(mVssRays, mObjects);
+		
 		cout << "finished\n" << endl;
 		totalSamples = (int)mVssRays.size();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2538)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2542)
@@ -462,21 +462,11 @@
 			if (mUseSpatialFilter) 
 			{
-				// 9.11. 2006 -> experiment with new spatial filter
-#if 0
-				mViewCellsManager->ApplySpatialFilter(mKdTree,
-					mSpatialFilterSize*
-					Magnitude(mViewCellsManager->GetViewSpaceBox().Size()),
-					mPvsCache.mPvs);
-#else
-				//cout<<"updating filter" << endl;
 				// mSpatialFilter size is in range 0.001 - 0.1
 				mViewCellsManager->ApplyFilter2(viewcell,
-					mUseFilter,
-					100 * mSpatialFilterSize,
-					mPvsCache.mPvs,
-					&mPvsCache.filteredBoxes
-					);
-#endif
-			} 
+					                            mUseFilter,
+												100 * mSpatialFilterSize,
+												mPvsCache.mPvs,
+												&mPvsCache.filteredBoxes);
+			}
 			else
 			{
@@ -496,6 +486,8 @@
 		if (mUseSpatialFilter && mRenderBoxes) 
 		{
-			for (int i=0; i < mPvsCache.filteredBoxes.size(); i++)
+			for (int i=0; i < mPvsCache.filteredBoxes.size(); ++ i)
+			{
 				RenderBox(mPvsCache.filteredBoxes[i]);
+			}
 		} 
 		else 
@@ -507,8 +499,9 @@
 			else
 			{
+				mPvsSize = mPvsCache.mPvs.GetSize();
+
 				ObjectPvsIterator it = mPvsCache.mPvs.GetIterator();
 
-				mPvsSize = mPvsCache.mPvs.GetSize();
-				for (; it.HasMoreEntries(); ) 
+				while (it.HasMoreEntries())
 				{
 					Intersectable *object = it.Next(pvsData);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.h	(revision 2538)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.h	(revision 2542)
@@ -255,5 +255,4 @@
 		{
 			mDirty = false;
-			// todo: avoid computing split plane
 			sOspTree->EvalSubdivisionCandidate(*this);	
 		}
@@ -272,6 +271,5 @@
 		}
 
-		void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
-									const bool onlyUnmailed)
+		void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList, const bool onlyUnmailed)
 		{
 			sOspTree->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2542)
@@ -34,4 +34,6 @@
 #define PVS_ADD_DIRTY 1
 
+
+
 namespace GtpVisibilityPreprocessor {
 
@@ -46,4 +48,10 @@
 const static bool CLAMP_TO_BOX = true;
 
+
+
+inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
+{
+	return obj1->mId < obj2->mId;
+}
 
 
@@ -994,5 +1002,6 @@
 	EvaluateViewCellsStats();
 
-	if (1) CompressViewCells();
+	// compress the final solution
+	if (0) CompressViewCells();
 
 	// write view cells to disc
@@ -1021,10 +1030,10 @@
 												 
 {
+	if (!Debug.is_open()) Debug.open("debug.log");
+
 	// give just an empty container as parameter:
 	// this loading function is used in the engine, thus it
 	// does not need to load the entities the preprocessor works on
 	ObjectContainer preprocessorObjects;
-
-	if (!Debug.is_open()) Debug.open("debug.log");
 
 	return LoadViewCells(filename, 
@@ -1033,4 +1042,55 @@
 						 finalizeViewCells,
 						 bconverter);
+}
+
+
+void ViewCellsManager::LoadIndexedBoundingBoxes(IN_STREAM &stream, IndexedBoundingBoxContainer &iboxes)
+{
+	Vector3 bmin;
+	Vector3 bmax;
+	int id;
+
+	stream.read(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
+	stream.read(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
+	stream.read(reinterpret_cast<char *>(&id), sizeof(int));
+
+	AxisAlignedBox3 box(bmin, bmax);
+
+	iboxes.push_back(IndexedBoundingBox(id, box));
+	//Debug << "bbox: " << box << endl;
+}
+
+
+ViewCellsManager *ViewCellsManager::LoadViewCellsBin(const string &filename, 
+													 ObjectContainer &pvsObjects,
+													 ObjectContainer &preprocessorObjects,
+													 bool finalizeViewCells,
+													 BoundingBoxConverter *bconverter)
+{
+	IN_STREAM stream(filename.c_str());
+	
+	IndexedBoundingBoxContainer iboxes;
+	LoadIndexedBoundingBoxes(stream, iboxes);
+
+	if (bconverter)
+	{
+		// all bounding boxes gathered in this step => 
+		// associate object ids with bounding boxes
+		bconverter->IdentifyObjects(iboxes, pvsObjects);
+	}
+
+	// sort objects by id
+	sort(pvsObjects.begin(), pvsObjects.end(), ilt);
+
+	ViewCellsTree *vcTree = new ViewCellsTree();
+	VspTree *vspTree = new VspTree();
+
+	HierarchyManager *hm = new HierarchyManager(0);
+
+	vspTree->ImportBinary(stream, pvsObjects);
+	
+	VspOspViewCellsManager *vcm = new VspOspViewCellsManager(vcTree, hm);
+
+	return vcm;
 }
 
@@ -1061,5 +1121,5 @@
 	}
 	
-	if (0) //hack: should work with reset function, but something is wrong ..
+	if (0) //hack: reset function should work, but something is wrong ..
 	{
 		vm->ResetViewCells();
@@ -1104,8 +1164,7 @@
 											 const ObjectContainer &objects)
 {
+	// no view cells constructed yet
 	if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
-	{
 		return false;
-	}
 
 	cout << "exporting view cells to xml ... ";
@@ -2424,14 +2483,11 @@
 
 
-
-		  if (evaluateFilter) {
+		  if (evaluateFilter) 
+		  {
 			  ObjectPvs filteredPvs;
 
-			  PvsFilterStatistics fstat = ApplyFilter2(viewcell,
-				  false,
-				  mFilterWidth,
-				  filteredPvs);
-
-			  float filteredCost = filteredPvs.EvalPvsCost();
+			  PvsFilterStatistics fstat = ApplyFilter2(viewcell, false, mFilterWidth, filteredPvs);
+
+			  const float filteredCost = filteredPvs.EvalPvsCost();
 
 			  stat.avgFilteredPvs += filteredCost;
@@ -3070,6 +3126,8 @@
 			MeshInstance *mi = static_cast<MeshInstance *>(*it);
 			const AxisAlignedBox3 box = mi->GetBox();
+			
 			Vector3 bmin = box.Min();
 			Vector3 bmax = box.Max();
+			
 			int id = mi->GetId();
 
@@ -3444,5 +3502,5 @@
 
 	// Minimal number of samples so that filtering takes place
-#define MIN_SAMPLES  50
+	const float MIN_SAMPLES = 50;
 
 	if (samples > MIN_SAMPLES) 
@@ -3537,5 +3595,5 @@
 	}
 
-	Debug << " nPvs size = " << pvs.GetSize() << endl;
+	//Debug << " nPvs size = " << pvs.GetSize() << endl;
 
 	if (!mUseKdPvs)
@@ -5887,6 +5945,6 @@
 
 
-Intersectable *
-VspOspViewCellsManager::GetIntersectable(const VssRay &ray, const bool isTermination) const
+Intersectable *VspOspViewCellsManager::GetIntersectable(const VssRay &ray, 
+														const bool isTermination) const
 {
 	if (mUseKdPvs)
@@ -6387,5 +6445,4 @@
 			const bool exportBounds = false;
 
-			if (1)
 			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, 
 			                                              objects, 
@@ -6575,6 +6632,11 @@
 											 const ObjectContainer &objects)
 {
+	// no view cells were computed
 	if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
 		return false;
+
+	cout << "exporting binary" << endl;
+	string fname("test.vc");
+	return ExportBinaryViewCells(fname, exportPvs, objects);
 
 	const long starttime = GetTime();
@@ -6612,7 +6674,8 @@
 
 				stream << "<BoundingBox" << " id=\"" << id << "\""
-				   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
-				   << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
+					   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
+				       << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
 			}
+
 			stream << "</BoundingBoxes>" << endl;
 		}
@@ -6661,4 +6724,5 @@
 												   const ObjectContainer &objects)
 {
+	// no view cells constructed yet
 	if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
 		return false;
@@ -6678,6 +6742,6 @@
     ///////////////
 	//-- export bounding boxes
-	//-- we need the boxes to identify objects in the target engine
-
+
+	// we use bounding box intersection to identify pvs objects in the target engine
 	vector<KdIntersectable *>::const_iterator kit, kit_end = mPreprocessor->mKdTree->mKdIntersectables.end();
 
@@ -6689,6 +6753,7 @@
 		const AxisAlignedBox3 &box = obj->GetBox();
 
-		// set kd node id
+		// setting the kd node id important to identify the kd node as pvs entry
 		obj->SetId(id);
+
 		Vector3 bmin = box.Min();
 		Vector3 bmax = box.Max();
@@ -6703,5 +6768,4 @@
 	//-- export the view cells and the pvs
 
-
 	int numViewCells = mCurrentViewCellsStats.viewCells;
 	stream.write(reinterpret_cast<char *>(&numViewCells), sizeof(int));
@@ -6712,4 +6776,7 @@
 	stream.write(reinterpret_cast<char *>(&vmin), sizeof(Vector3));
 	stream.write(reinterpret_cast<char *>(&vmax), sizeof(Vector3));
+
+	// todo
+	mViewCellsTree->ExportBinary(stream);
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 2542)
@@ -2589,18 +2589,10 @@
 
 ///////////////////
-// Binary export
+//-- Binary export
 
 void ViewCellsTree::ExportBinInterior(OUT_STREAM &stream, ViewCellInterior *interior)
 {
-#if TODO
 	int interiorid = TYPE_INTERIOR;
 	stream.write(reinterpret_cast<char *>(&interiorid), sizeof(int));
-
-	int axis = interior->GetAxis();
-	float pos = interior->GetPosition();
-
-	stream.write(reinterpret_cast<char *>(&axis), sizeof(int));
-	stream.write(reinterpret_cast<char *>(&pos), sizeof(float));
-#endif
 }
 
@@ -2608,20 +2600,17 @@
 void ViewCellsTree::ExportBinLeaf(OUT_STREAM &stream, ViewCell *leaf)
 {
-#if TODO
 	int type = TYPE_LEAF;
 
-	int id = -1;
-	if (viewCell != mOutOfBoundsCell)
-		id = viewCell->GetId();
+	int id = leaf->GetId();
 
 	stream.write(reinterpret_cast<char *>(&type), sizeof(int));
 	stream.write(reinterpret_cast<char *>(&id), sizeof(int));
 
-	int pvsSize = viewCell->GetPvs().GetSize();
+	int pvsSize = leaf->GetPvs().GetSize();
 	stream.write(reinterpret_cast<char *>(&pvsSize), sizeof(int));
 
-	ObjectPvsIterator pit = viewCell->GetPvs().GetIterator();
-
-	// write PVS of view cell
+	ObjectPvsIterator pit = leaf->GetPvs().GetIterator();
+
+	// write pvs
 	while (pit.HasMoreEntries())
 	{
@@ -2630,5 +2619,4 @@
 		stream.write(reinterpret_cast<char *>(&id), sizeof(int));
 	}
-#endif
 }
 
@@ -2636,5 +2624,5 @@
 bool ViewCellsTree::ExportBinary(OUT_STREAM &stream)
 {
-#if TODO
+
 	// export binary version of mesh
 	queue<ViewCell *> tStack;
@@ -2652,13 +2640,19 @@
 		else
 		{
-			VspInterior *interior = static_cast<ViewCellInterior *>(node);
-
+			ViewCellInterior *interior = static_cast<ViewCellInterior *>(node);
+
+			// export current interior
 			ExportBinInterior(stream, interior);
 			
-			tStack.push(interior->GetFront());
-			tStack.push(interior->GetBack());
-		}
-	}
-#endif
+			// push children
+			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+
+			for (it = interior->mChildren.begin(); it != it_end; ++ it)
+			{
+				tStack.push(*it);
+			}
+		}
+	}
+
 	return true;
 }
@@ -2666,5 +2660,5 @@
 
 //////////////////
-// import binary
+//-- import binary
 
 ViewCellInterior *ViewCellsTree::ImportBinInterior(IN_STREAM  &stream, ViewCellInterior *parent)
@@ -2725,26 +2719,4 @@
 
 
-/** Data used for tree traversal
-*/
-struct MyTraversalData
-{  
-public:
-
-	MyTraversalData(ViewCell *node, const int depth, const AxisAlignedBox3 &box):
-	mNode(node), mDepth(depth), mBox(box)
-	{}
-
-
-	//////////////////////
-
-	/// the current node
-	ViewCell *mNode;
-	/// current depth
-	int mDepth;
-	/// the bounding box
-	AxisAlignedBox3 mBox;
-};
-
-
 bool ViewCellsTree::ImportBinary(IN_STREAM &stream, const ObjectContainer &pvsObjects)
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 2176)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 2542)
@@ -37,19 +37,4 @@
 				   Vector3 &normal) const
 {
-#if 0
-	VertexContainer vertices;
-	vertices.push_back(mVertices[0]);
-	vertices.push_back(mVertices[1]);
-	vertices.push_back(mVertices[2]);
-
-	Polygon3 poly(vertices);
-
-	int dummy = poly.CastRay(ray, t, nearestT);
-	normal = poly.GetNormal();
-	
-	//	cout << "polyversion code: " << dummy << " t: " << t << " nearestT: " << nearestT << endl;
-	return dummy;
-#endif
-	
 	//////////////
 	// specialised triangle ray casting version
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 2542)
@@ -792,8 +792,8 @@
 	
 	mPlaneTimer.Exit();
-
 	mEvalTimer.Entry();
 
 	VspLeaf *leaf = static_cast<VspLeaf *>(splitCandidate.mParentData.mNode);
+
 
 	//////////////
@@ -832,5 +832,4 @@
 
 #if COUNT_ORIGIN_OBJECTS
-
 		obj = (*ray).mOriginObject;
 
@@ -838,5 +837,4 @@
 		{
 			leaf = mBvHierarchy->GetLeaf(obj);
-
 			UpdateContributionsToPvs(leaf, cf, sData); 
 		}
@@ -3227,11 +3225,10 @@
 
 void VspTree::AddCandidateToDirtyList(const VssRay &ray, 
-								const bool isTermination,
-								vector<SubdivisionCandidate *> &dirtyList,
-								const bool onlyUnmailed) const
+									  const bool isTermination,
+									  vector<SubdivisionCandidate *> &dirtyList,
+									  const bool onlyUnmailed) const
 
 {
 #if HACK_PERFORMANCE
-
 	Intersectable *obj = isTermination ? ray.mTerminationObject : ray.mOriginObject; 
 
@@ -3251,5 +3248,4 @@
 	}
 #else
-
 	SubdivisionCandidate *candidate = NULL;
 
@@ -3379,6 +3375,6 @@
 			// simple render cost evaluation
 			if (-- leaf->mCounter == 0)
-				//pvs += (int)leaf->mObjects.size();
 				pvs += BvHierarchy::EvalAbsCost(leaf->mObjects);
+
 			break;
 		}
@@ -3466,5 +3462,4 @@
 	return pvsSize;
 #endif
-	
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.h	(revision 2176)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.h	(revision 2542)
@@ -8,4 +8,6 @@
 #include "Mesh.h"
 #include "Material.h"
+
+
 
 namespace GtpVisibilityPreprocessor {
@@ -124,5 +126,4 @@
 	ResourceManager<T>()
 	{}
-
 	/** Release resources.
 	*/
@@ -137,5 +138,4 @@
 		}
 	}
-  
 	/** Copy constructor.
 	*/
@@ -143,7 +143,6 @@
     {
     }
-
-	 /** Helper function returning unique id for resource.
-	 */
+	/** Helper function returning unique id for resource.
+	*/
 	static unsigned int CreateUniqueId()
 	{
Index: GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 2360)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 2542)
@@ -34,10 +34,8 @@
 
 
-/** This class implements a flexible heap for
-use as a priority queue.
-
-Unlike the stl priority_queue, the class allows access to all 
-elements. It implements efficient insertion routines and remove routines
-of arbitrary elements.
+/** This class implements a flexible heap for use as a priority queue.
+	Unlike the stl priority_queue, the class allows access to all 
+	elements. It implements efficient insertion routines and remove routines
+	of arbitrary elements.
 */
 template<typename T>
@@ -48,15 +46,15 @@
 	FlexibleHeap(const unsigned int n): mBuffer(n) { }
 
-	void Push(T t) { Push(t, t->GetPriority()); }
+	void Push(T t);
 	void Push(T t, const float priority);
-	bool Update(T t) { return Update(t, t->GetPriority()); }
+	bool Update(T t);
 	bool Update(T t, const float priority);
 
-	unsigned int Size() const { return (unsigned int)mBuffer.size(); }
-	bool Empty() const {return mBuffer.empty(); }
-	T Item(const unsigned int i) { return mBuffer[i]; }
-	const T Item(const unsigned int i) const { return mBuffer[i]; }
+	inline unsigned int Size() const;
+	inline bool Empty() const;
+	inline T Item(const unsigned int i);
+	inline const T Item(const unsigned int i) const;
 	T Pop();
-	T Top() const { return (mBuffer.empty() ? (T)NULL : mBuffer.front()); }
+	inline T Top() const;
 	/** Erases the element from the heap.
 	*/
@@ -79,7 +77,59 @@
 
 
-/*****************************************************************************/
-/*                         MyHeap implementation                             *
-/*****************************************************************************/
+
+
+/******************************************************************/
+/*                 FexibleHeap implementation                     */
+/******************************************************************/
+
+
+template <typename T>
+void FlexibleHeap<T>::Push(T t) 
+{ 
+	Push(t, t->GetPriority()); 
+}
+
+
+template <typename T>
+bool FlexibleHeap<T>::Update(T t) 
+{ 
+	return Update(t, t->GetPriority()); 
+}
+
+
+template <typename T>
+inline unsigned int FlexibleHeap<T>::Size() const 
+{ 
+	return (unsigned int)mBuffer.size(); 
+}
+
+
+template <typename T>
+inline bool FlexibleHeap<T>::Empty() const 
+{
+	return mBuffer.empty(); 
+}
+
+
+template <typename T>
+inline T FlexibleHeap<T>::Item(const unsigned int i) 
+{ 
+	return mBuffer[i]; 
+}
+
+
+template <typename T>
+inline const T FlexibleHeap<T>::Item(const unsigned int i) const 
+{ 
+	return mBuffer[i]; 
+}
+
+
+template <typename T>
+inline T FlexibleHeap<T>::Top() const 
+{ 
+	return (mBuffer.empty() ? (T)NULL : mBuffer.front()); 
+}
+
 
 template <typename T>
@@ -120,4 +170,5 @@
 	}
 }
+
 
 template <typename T>
@@ -200,5 +251,7 @@
 	mBuffer.pop_back();
 
-	DownHeap(0);
+	if (!mBuffer.empty())
+		DownHeap(0);
+
 	dead->NotInHeap();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 2542)
@@ -549,5 +549,5 @@
 	
 	PrepareViewSpaceSubdivision(viewSpaceQueue, sampleRays, objects);
-
+	/// q: why clear the dirty list here?
 	dirtyList.clear();
 	// view space subdivision started
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 2539)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 2542)
@@ -60,8 +60,8 @@
 //      can be set via the -v= command.
 // ---------------------------------------------------------------------------
-static bool     doNamespaces       = false;
-static bool     doSchema           = false;
-static bool     schemaFullChecking = false;
-static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
+static bool doNamespaces = false;
+static bool doSchema = false;
+static bool schemaFullChecking = false;
+static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
 
 // hack for loading bvh nodes
@@ -313,4 +313,5 @@
 	else
 	{
+		// sort objects by id
 		sort(mPvsObjects.begin(), mPvsObjects.end(), ilt);
 	}
@@ -318,4 +319,5 @@
 	Debug << "\nconverted bounding boxes to objects in "
 		  << TimeDiff(startTime, GetTime()) * 1e-6 << " secs" << endl;
+
 	mBoxTimer.Exit();
 }
@@ -693,4 +695,5 @@
 		string attrName(StrX(attributes.getName(i)).LocalForm());
 		StrX attrValue(attributes.getValue(i));
+
 		const char *ptr = attrValue.LocalForm();
 
@@ -711,5 +714,4 @@
 	AxisAlignedBox3 box(bmin, bmax);
 	mIBoundingBoxes.push_back(IndexedBoundingBox(id, box));
-	//Debug << "bbox: " << box << endl;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 2176)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 2542)
@@ -1591,5 +1591,5 @@
 	}
 
-	// andidates holding this view cells are thrown into dirty list
+	// candidates that contain this view cells are thrown into dirty list
 	ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.cpp	(revision 1001)
+++ 	(revision )
@@ -1,109 +1,0 @@
-#include "ResourceManager.h"
-#include "Mesh.h"
-
-namespace GtpVisibilityPreprocessor {
-
-ResourceManager *ResourceManager::sResourceManager = NULL;
-
-
-ResourceManager *ResourceManager::GetSingleton()
-{Debug << "here1" << endl;
-	if (!sResourceManager)
-	{
-		Debug << "here888" << endl;
-		sResourceManager = new ResourceManager();
-	}
-
-  Debug << "here999" << endl;
-	return sResourceManager;
-}
-
-
-ResourceManager::ResourceManager()
-{
-}
-
-ResourceManager::~ResourceManager()
-{
-	cout << "here839 " << mMeshes.size() << endl;
-
-	MeshMap::iterator mit, mit_end = mMeshes.end();
-	for (mit = mMeshes.begin(); mit != mMeshes.end(); ++ mit)
-	{
-		cout << "mesh: " << (*mit).first << " " << (*mit).second << endl;
-		DEL_PTR((*mit).second);
-	}
-}
-
-void ResourceManager::DelSingleton()
-{
-	cout << "here11" << endl;
-	DEL_PTR(sResourceManager);
-}
-
-/*Mesh *ResourceManager::CreateMesh(const string meshName)
-{
-	Mesh *mesh = new Mesh();
-	mMeshes[meshName] = mesh;
-	
-	return mesh;
-}*/
-
-
-Mesh *ResourceManager::GetOrCreateMesh(const string meshName)
-{Debug << "here2" << endl;
-	Mesh *mesh = NULL;
-
-	if (!(mesh = FindMesh(meshName.c_str())))
-	{
-		mesh = new Mesh();
-		mMeshes[meshName.c_str()] = mesh;
-	}
-
-	return mesh;
-}
-
-
-/*
-bool ResourceManager::InsertMesh(Mesh *mesh, const string meshName)
-{
-	Mesh *mesh = NULL;
-
-	if (FindMesh(meshName.c_str()))
-		return false;
-
-	mMeshes[meshName.c_str()] = mesh;
-	
-	return mesh;
-}
-*/
-
-Mesh *ResourceManager::FindMesh(const string &name) const
-{Debug << "here3" << endl;
-	MeshMap::const_iterator mit = mMeshes.find(name.c_str());
-
-	if (mit != mMeshes.end()) 
-	{
-		return (*mit).second;
-	}
-
-	return NULL;
-}
-
-
-bool ResourceManager::DestroyMesh(const string &meshName)
-{
-	if (!FindMesh(meshName))
-	{
-		Mesh *mesh = mMeshes[meshName.c_str()];
-
-		mMeshes.erase(meshName.c_str());
-		DEL_PTR(mesh);
-
-		return true;
-	}
-
-	return false;
-}
-
-}
