Index: GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1528)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1551)
@@ -128,5 +128,4 @@
 		const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
 
-	//	cout << "here45 origin " << a << " termination: " << b << endl;
 #if _DEBUG
 		bool isnan = false;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 1551)
@@ -646,5 +646,4 @@
 	*/
 	void ConstructGeometry(ViewCell* vc, BspNodeGeometry &geom) const;
-
 			
 	/** Sets pointer to view cells manager.
@@ -675,5 +674,5 @@
 	/** Exports Bsp tree to file.
 	*/
-	bool Export(ofstream &stream);
+	bool Export(OUT_STREAM &stream);
 
 	/** Pointer to the view cells tree.
@@ -718,5 +717,5 @@
 	};
 
-	void ExportNode(BspNode *node, ofstream &stream);
+	void ExportNode(BspNode *node, OUT_STREAM &stream);
 
 	/** Evaluates tree stats in the BSP tree leafs.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1551)
@@ -875,5 +875,5 @@
 		}
 		
-        viewCell->mLeaf = leaf;
+		viewCell->mLeaves.push_back(leaf);
 
 		if (mUseAreaForPvs)
@@ -973,4 +973,5 @@
 
 
+	//////////////////
 	//-- terminate traversal and create new view cell
 	if (newNode->IsLeaf())
@@ -993,4 +994,7 @@
 		mBspStats.sampleContributions +=(int) sampCon;
 
+		viewCell->mLeaves.push_back(leaf);
+
+		///////////
 		//-- store additional info
 		if (mStoreRays)
@@ -1003,8 +1007,5 @@
 			}
 		}
-
-		
-		viewCell->mLeaf = leaf;
-
+	
 		if (mUseAreaForPvs)
 			viewCell->SetArea(tData.mProbability);
@@ -2991,6 +2992,9 @@
 								   BspNodeGeometry &vcGeom) const
 {
+	// if false, cannot construct geometry for interior leaf
+	if (!mViewCellsTree)
+		return;
+
 	ViewCellContainer leaves;
-	
 	mViewCellsTree->CollectLeaves(vc, leaves);
 
@@ -2999,7 +3003,12 @@
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
-		BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-		
-		ConstructGeometry(l, vcGeom);
+		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*it);
+		vector<BspLeaf *>::const_iterator bit, bit_end = bspVc->mLeaves.end();
+
+		for (bit = bspVc->mLeaves.begin(); bit != bit_end; ++ bit)
+		{
+			BspLeaf *l = *bit;
+			ConstructGeometry(l, vcGeom);
+		}
 	}
 }
@@ -3806,8 +3815,8 @@
 		if (ray->mViewCells.size() < 2)
 			continue;
-//TODO viewcellhierarchy
+
 		iit = ray->mViewCells.begin();
 		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*(iit ++));
-		BspLeaf *leaf = bspVc->mLeaf;
+		BspLeaf *leaf = bspVc->mLeaves[0];
 		
 		// traverse intersections 
@@ -3818,5 +3827,5 @@
 			BspLeaf *prevLeaf = leaf;
 			bspVc = dynamic_cast<BspViewCell *>(*iit);
-            leaf = bspVc->mLeaf;
+            leaf = bspVc->mLeaves[0]; // exactly one leaf
 
 			// view space not valid or same view cell
@@ -3984,5 +3993,4 @@
 {
 	ExportNode(mRoot, stream);
-
 	return true;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1551)
@@ -578,4 +578,6 @@
 	AxisAlignedPlane *GetClipPlane();
 	
+	void ExportMergedViewCells(const ObjectContainer &objects);
+
 	///////////////////////
 
@@ -777,6 +779,4 @@
 	*/
 	void TestSubdivision();
-
-	void ExportMergedViewCells(const ObjectContainer &objects);
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1551)
@@ -420,8 +420,9 @@
 	if (!mOutOfBoundsCellPartOfTree)
 	{
+		cout << "here223" << endl;
 		// out of bounds cell not part of tree =>
 		// delete manually
 		DEL_PTR(mOutOfBoundsCell);
-	}
+	}else cout << "here991" << endl;
 }
 
@@ -583,12 +584,11 @@
   
 	int polysSize = 0;
-	cout << "here55 " << mBbox << endl;
-	
-cout << "here99";
+
 	for (int i = 0; i < limit; ++ i)
-	{cout << "q";
-	Mesh *mesh = viewCells[i]->GetMesh();
+	{
+		Mesh *mesh = viewCells[i]->GetMesh();
+
 		if (mesh) 
-		{cout << "w";
+		{
 			// copy the mesh into polygons and add to BSP tree aabb
 			mBbox.Include(viewCells[i]->GetBox()); 
@@ -596,5 +596,5 @@
 		}
 	}
-cout << "here155 " << mBbox << endl;
+
 	return polysSize;
 }
@@ -608,7 +608,7 @@
 	int limit = (maxObjects > 0) ? 
 		Min((int)objects.size(), maxObjects) : (int)objects.size();
-  cout << "here88";
+  
 	for (int i = 0; i < limit; ++i)
-	{cout << "e";
+	{
 		Intersectable *object = objects[i];
 		Mesh *mesh = NULL;
@@ -638,7 +638,8 @@
 		
         if (!mesh) continue;
+
 		// copy the mesh data to polygons
 		if (addToBbox)
-		{cout << "b";
+		{
 			mBbox.Include(object->GetBox()); // add to BSP tree aabb
 		}
@@ -659,5 +660,4 @@
 void BspTree::Construct(const ViewCellContainer &viewCells)
 {
-	cout << "here5500000000" << endl;
 	// construct hierarchy over the given view cells
 	mUsePredefinedViewCells = true;
@@ -1006,5 +1006,5 @@
 
 		leaf->SetViewCell(viewCell);
-		viewCell->mLeaf = leaf;
+		viewCell->mLeaves.push_back(leaf);
 
 		//float probability = max(0.0f, tData.mProbability);
@@ -2241,14 +2241,4 @@
 		}
 	}
-
-	// also add out of bounds cell
-	if (0 && !mOutOfBoundsCell->Mailed())
-	{
-		mOutOfBoundsCell->Mail();
-		viewCells.push_back(mOutOfBoundsCell);
-	}
-
-	cout << "here555 " << viewCells.size() << endl;
-
 }
 
@@ -2389,6 +2379,12 @@
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
-		BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-		ConstructGeometry(l, vcGeom);
+		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*it);
+		vector<BspLeaf *>::const_iterator bit, bit_end = bspVc->mLeaves.end();
+
+		for (bit = bspVc->mLeaves.begin(); bit != bit_end; ++ bit)
+		{
+			BspLeaf *l = *bit;
+			ConstructGeometry(l, vcGeom);
+		}
 	}
 }
@@ -2810,5 +2806,5 @@
 		iit = ray->mViewCells.begin();
 		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*(iit ++));
-		BspLeaf *leaf = bspVc->mLeaf;
+		BspLeaf *leaf = bspVc->mLeaves[0];
 		
 		// traverse intersections 
@@ -2819,5 +2815,5 @@
 			BspLeaf *prevLeaf = leaf;
 			bspVc = dynamic_cast<BspViewCell *>(*iit);
-            leaf = bspVc->mLeaf;
+            leaf = bspVc->mLeaves[0];
 
 			// view space not valid or same view cell
@@ -3338,23 +3334,19 @@
 
 
-bool BspTree::Export(ofstream &stream)
+bool BspTree::Export(OUT_STREAM &stream)
 {
 	ExportNode(mRoot, stream);
-
 	return true;
 }
 
 
-void BspTree::ExportNode(BspNode *node, ofstream &stream)
+void BspTree::ExportNode(BspNode *node, OUT_STREAM &stream)
 {
 	if (node->IsLeaf())
 	{
 		BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
-			
-		int id = -1;
-		if (leaf->GetViewCell() != mOutOfBoundsCell)
-		{
-			id = leaf->GetViewCell()->GetId();
-		}
+		ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
+
+		const int id = viewCell->GetId();
 
 		stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
@@ -3376,3 +3368,4 @@
 }
 
-}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1548)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1551)
@@ -42,23 +42,7 @@
 class Exporter;
 
-#if 0
-template <typename T> class GtPriority
-{
-public:
-	bool operator() (const T c1, const T c2) const
-	{
-		//return false;
-		return c1->GetPriority() < c2->GetPriority();
-	}
-};
-
-typedef std::priority_queue<SubdivisionCandidate *, 
-							std::vector<SubdivisionCandidate *>, 
-							GtPriority<std::vector<SubdivisionCandidate *>::value_type> > SplitQueue;
-#endif
-
-
-
-/** View space partition statistics.
+
+
+/** View space / object space hierarchy statistics.
 */
 class HierarchyStatistics: public StatisticsBase
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1548)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1551)
@@ -1445,5 +1445,5 @@
 	{
 		VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
-		VspLeaf *leaf = vc->mLeaf;
+		VspLeaf *leaf = vc->mLeaves[0];
 		SubdivisionCandidate *candidate = leaf->GetSubdivisionCandidate();
 		
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1548)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1551)
@@ -30,5 +30,4 @@
 
 
-
 namespace GtpVisibilityPreprocessor {
 
@@ -667,7 +666,5 @@
 	{
 		//-- export bounding boxes
-
 		stream << "<BoundingBoxes>" << endl;
-
 		ObjectContainer::const_iterator oit, oit_end = objects.end();
 
@@ -677,4 +674,5 @@
 			const AxisAlignedBox3 box = mi->GetBox();
 
+			////////////
 			//-- the bounding boxes
 			stream << "<BoundingBox" << " id=\"" << mi->GetId() << "\""
@@ -687,10 +685,10 @@
 
 	
+	/////////////
 	//-- export the view cells and the pvs
 
 	const int numViewCells = mCurrentViewCellsStats.viewCells;
-
 	stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
-	
+
 	mViewCellsTree->Export(stream, exportPvs);
 
@@ -698,4 +696,5 @@
 
 
+	//////////
 	//-- export the view space hierarchy
 	
@@ -753,14 +752,11 @@
 	Debug << "stepsize: " << stepSize << endl;
 	
-
 	const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
 	const float totalVol = GetViewSpaceBox().GetVolume();
 
 	int currentPvs = minVal;//(int)ceil(minRenderCost);
-
 	
 	int i = 0;
 	int j = 0;
-
 	float volSum = 0;
 	int smallerSum = 0;
@@ -1310,5 +1306,5 @@
 	{
 		// set view space box to bounding box of the view cells
-		/*AxisAlignedBox3 bbox;
+		AxisAlignedBox3 bbox;
 		bbox.Initialize();
 		ViewCellContainer::iterator it = mViewCells.begin(), it_end = mViewCells.end();
@@ -1317,5 +1313,5 @@
 			bbox.Include(GetViewCellBox(*it));
 		}
-		SetViewSpaceBox(bbox);*/
+		SetViewSpaceBox(bbox);
 	
 		cout << "generated " << (int)mViewCells.size() << " view cells using the geometry from file " << filename << endl;
@@ -1965,7 +1961,10 @@
 	else // no view cells tree, handle view cells "myself"
 	{
-		for (int i = 0; i < (int)mViewCells.size(); ++ i)
-		{
-			mViewCells[i]->SetId(i);
+		int i = 0;
+		ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
+		for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
+		{
+			if ((*vit)->GetId() != -1) 
+				mViewCells[i]->SetId(i ++);
 		}
 	}
@@ -2560,5 +2559,5 @@
 	}
 	else
-	{	cout << "here222" << endl;
+	{	
 		// use predefined view cells geometry => 
 		// contruct bsp hierarchy over them
@@ -2620,5 +2619,5 @@
 
 
-void BspViewCellsManager::ExportMergedViewCells(const ObjectContainer &objects)
+void ViewCellsManager::ExportMergedViewCells(const ObjectContainer &objects)
 {
 	// save color code
@@ -2725,5 +2724,4 @@
 		ObjectPvs pvs;
 		UpdatePvsForEvaluation(root, pvs);
-		
 	}
 
@@ -2746,11 +2744,10 @@
 	}
 
-	ResetViewCells(); // reset view cells
+	// recompute view cells and stats
+	ResetViewCells();
 	Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
 
+	//  visualization of the view cells
 	if (1) ExportMergedViewCells(objects);
-
-	// only for debugging purpose: test if the subdivision is valid
-	if (0) TestSubdivision();
 
 	// compute final meshes and volume / area
@@ -2787,5 +2784,5 @@
 	
 	int savedColorCode = mColorCode;
-		
+	
 	if (1) // export final view cells
 	{
@@ -2807,4 +2804,5 @@
 		cout << "finished" << endl;
 	}
+
 	// reset color code
 	mColorCode = savedColorCode;
@@ -2967,13 +2965,11 @@
 	{
 		BspNodeGeometry geom;
-		BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-		mBspTree->ConstructGeometry(leaf, geom);
+		mBspTree->ConstructGeometry(*it, geom);
 
 		const float lVol = geom.GetVolume();
-		
 		newVol += lVol;
 		subdivVol += (*it)->GetVolume();
 
-		float thres = 0.9f;
+		const float thres = 0.9f;
 		if ((lVol < ((*it)->GetVolume() * thres)) ||
 			(lVol * thres > ((*it)->GetVolume())))
@@ -2991,6 +2987,10 @@
 												 const AxisAlignedBox3 *sceneBox,
 												 const AxisAlignedPlane *clipPlane
-												) const
-{
+												 ) const
+{cout << "here55543 " << vc->GetId() << endl;
+	// out of bounds cell
+	if (vc->GetId() == -1)
+		return;
+
 	// export mesh if available
 	if (vc->GetMesh())
@@ -2999,4 +2999,5 @@
 		return;
 	}
+
 	// otherwise construct from leaves
 	if (clipPlane)
@@ -3011,10 +3012,8 @@
 		{
 			BspNodeGeometry geom;
-
 			BspNodeGeometry front;
 			BspNodeGeometry back;
 
-			BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-			mBspTree->ConstructGeometry(leaf, geom);
+			mBspTree->ConstructGeometry(*it, geom);
 
 			const float eps = 0.00000001f;
@@ -3033,8 +3032,6 @@
 								   eps);
 	
-				//Debug << "geo size: " << geom.Size() << endl;
-				//Debug << "size b: " << back.Size() << " f: " << front.Size() << endl;
 				if (back.Valid())
-				{
+				{	
 					exporter->ExportPolygons(back.GetPolys());
 				}			
@@ -3046,5 +3043,4 @@
 		BspNodeGeometry geom;
 		mBspTree->ConstructGeometry(vc, geom);
-			
 		exporter->ExportPolygons(geom.GetPolys());
 	}
@@ -3054,6 +3050,5 @@
 void BspViewCellsManager::CreateMesh(ViewCell *vc)
 {
-	// delete previous mesh
-	///DEL_PTR(vc->GetMesh());
+	// note: should previous mesh be deleted (via mesh manager?)
 	BspNodeGeometry geom;
 	mBspTree->ConstructGeometry(vc, geom);
@@ -3080,17 +3075,14 @@
 	{
 		BspNodeGeometry geom;
-		BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-		mBspTree->ConstructGeometry(leaf, geom);
+
+		mBspTree->ConstructGeometry(*it, geom);
 
 		const float lVol = geom.GetVolume();
 		const float lArea = geom.GetArea();
 
-		//(*it)->SetVolume(vol);
-		//(*it)->SetArea(area);
-
 		area += lArea;
 		volume += lVol;
-
-        CreateMesh(*it);
+	
+		CreateMesh(*it);
 	}
 
@@ -3106,10 +3098,8 @@
 		return NULL;
 	}
-
 	if (!mViewSpaceBox.IsInside(point))
 	{
 		return NULL;
 	}
-
 	return mBspTree->GetViewCell(point);
 }
@@ -3141,43 +3131,66 @@
 										  const ObjectContainer &objects)
 {
-#if TODO
+	if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
+	{
+		return false;
+	}
+
 	cout << "exporting view cells to xml ... ";
-	std::ofstream stream;
+
+	OUT_STREAM stream(filename.c_str());
 
 	// for output we need unique ids for each view cell
 	CreateUniqueViewCellIds();
 
-
-	stream.open(filename.c_str());
 	stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
 	stream << "<VisibilitySolution>" << endl;
 
-	//-- the view space bounding box
-	stream << "<ViewSpaceBox" 
+	if (exportPvs) 
+	{
+		//////////
+		//-- export bounding boxes: they are used to identify the objects from the pvs and
+		//-- assign them to the entities in the rendering engine
+
+		stream << "<BoundingBoxes>" << endl;
+		ObjectContainer::const_iterator oit, oit_end = objects.end();
+
+		for (oit = objects.begin(); oit != oit_end; ++ oit)
+		{
+			MeshInstance *mi = dynamic_cast<MeshInstance *>(*oit);
+			const AxisAlignedBox3 box = mi->GetBox();
+			
+			stream << "<BoundingBox" << " id=\"" << mi->GetId() << "\""
+				   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
+				   << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
+		}
+
+		stream << "</BoundingBoxes>" << endl;
+	}
+
+	///////////
+	//-- export the view cells and the pvs
+
+	const int numViewCells = mCurrentViewCellsStats.viewCells;
+	stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
+
+	mViewCellsTree->Export(stream, exportPvs);
+	
+	stream << "</ViewCells>" << endl;
+
+	/////////////
+	//-- export the view space hierarchy
+	stream << "<ViewSpaceHierarchy type=\"bsp\""
 		   << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
-		   << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\" />" << endl;
-
-	//-- the type of the view cells hierarchy
-	
-	// NOTE: load in vsp bsp here because bsp and vsp bsp can use same tree and vsp bsp is bug free
-	stream << "<Hierarchy name=\"vspBspTree\" >" << endl; 
-	
-	//-- load the view cells itself, i.e., the ids and the pvs
-	stream << "<ViewCells>" << endl;
-
-	mViewCellsTree->Export(stream, exportPvs);
-
-	stream << "</ViewCells>" << endl;
-
-	//-- load the hierarchy
-	stream << "<Hierarchy>" << endl;
+		   << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
+
 	mBspTree->Export(stream);
-	stream << endl << "</Hierarchy>" << endl;
-
+
+	// end tags
+	stream << "</ViewSpaceHierarchy>" << endl;
 	stream << "</VisibilitySolution>" << endl;
+
 	stream.close();
-
 	cout << "finished" << endl;
-#endif
+
 	return true;
 }
@@ -3516,5 +3529,5 @@
 	{
 		KdViewCell *kdVc = dynamic_cast<KdViewCell *>(*it);
-		exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaf));
+		exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaves[0]));
 	}
 }
@@ -3851,6 +3864,5 @@
 	{
 		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(viewCell);
-
-		return bspVc->mLeaf;
+		return bspVc->mLeaves[0];
  	}
 	else
@@ -4082,10 +4094,10 @@
 bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
 {
-  // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
-  // validy update in preprocessor for all managers)
-  return ViewCellsManager::ViewPointValid(viewPoint);
-
-  //	return mViewSpaceBox.IsInside(viewPoint) &&
-  //		   mVspBspTree->ViewPointValid(viewPoint);
+	// $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
+	// validy update in preprocessor for all managers)
+	return ViewCellsManager::ViewPointValid(viewPoint);
+
+	//	return mViewSpaceBox.IsInside(viewPoint) &&
+	//		   mVspBspTree->ViewPointValid(viewPoint);
 }
 
@@ -4099,7 +4111,6 @@
 	VssRayContainer visRays;
 	GetRaySets(sampleRays, mVisualizationSamples, visRays);
-
-	
-	if (0) 
+	
+	if (1) 
 	{	
 		//////////////////
@@ -4146,64 +4157,7 @@
 	}
 
-	if (0)
-	{
-		cout << "exporting depth map ... ";
-
-		Exporter *exporter = Exporter::GetExporter("depth_map.x3d");
-		if (exporter)
-		{
-			if (1)
-			{
-				exporter->SetWireframe();
-				exporter->ExportBox(mViewSpaceBox);
-				exporter->SetFilled();
-			}
-
-			if (mExportGeometry)
-			{
-				exporter->ExportGeometry(objects);
-			}
-
-			const int maxDepth = mVspBspTree->GetStatistics().maxDepth;
-
-			ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
-
-			for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
-			{
-				ViewCell *vc = *vit;
-
-				ViewCellContainer leaves;
-				mViewCellsTree->CollectLeaves(vc, leaves);
-
-				ViewCellContainer::const_iterator lit, lit_end = leaves.end();
-
-				for (lit = leaves.begin(); lit != lit_end; ++ lit)
-				{
-					BspLeaf *leaf = dynamic_cast<BspViewCell *>(*lit)->mLeaf;
-
-					Material m; 
-
-					float relDepth = (float)leaf->GetDepth() / (float)maxDepth;
-					m.mDiffuseColor.r = relDepth;
-					m.mDiffuseColor.g = 0.0f;
-					m.mDiffuseColor.b = 1.0f - relDepth;
-
-                    exporter->SetForcedMaterial(m);
-				
-
-					BspNodeGeometry geom;
-					mVspBspTree->ConstructGeometry(leaf, geom);
-					exporter->ExportPolygons(geom.GetPolys());
-				}
-			}
-
-			delete exporter;
-		}
-
-
-		cout << "finished" << endl;
-	}
-
+	////////////////
 	//-- visualization of the BSP splits
+
 	bool exportSplits = false;
 	Environment::GetSingleton()->GetBoolValue("VspBspTree.Visualization.exportSplits", exportSplits);
@@ -4216,4 +4170,5 @@
 	}
 
+	////////
 	//-- export single view cells
 	ExportBspPvs(objects, visRays);
@@ -4308,68 +4263,40 @@
 		{
 			// output rays stored with the view cells during subdivision
-			if (1)
+			VssRayContainer vcRays;
+            VssRayContainer collectRays;
+
+			raysOut = min((int)rays.size(), 100);
+
+			// collect intial view cells
+			ViewCellContainer leaves;
+			mViewCellsTree->CollectLeaves(vc, leaves);
+
+			ViewCellContainer::const_iterator vit, vit_end = leaves.end();
+       
+			for (vit = leaves.begin(); vit != vit_end; ++ vit)
 			{
-				VssRayContainer vcRays;
-                VssRayContainer collectRays;
-
-				raysOut = min((int)rays.size(), 100);
-
-				// collect intial view cells
-				ViewCellContainer leaves;
-				mViewCellsTree->CollectLeaves(vc, leaves);
-
-				ViewCellContainer::const_iterator vit, vit_end = leaves.end();
-       
-				for (vit = leaves.begin(); vit != vit_end; ++ vit)
+				BspLeaf *vcLeaf = dynamic_cast<BspViewCell *>(*vit)->mLeaves[0];
+				
+				VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
+
+				for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
 				{
-					BspLeaf *vcLeaf = dynamic_cast<BspViewCell *>(*vit)->mLeaf;
-				
-					VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
-
-					for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
-					{
-						collectRays.push_back(*rit);
-					}
+					collectRays.push_back(*rit);
 				}
-
-				VssRayContainer::const_iterator rit, rit_end = collectRays.end();
-
-				for (rit = collectRays.begin(); rit != rit_end; ++ rit)
-				{
-					float p = RandomValue(0.0f, (float)collectRays.size());
+			}
+
+			VssRayContainer::const_iterator rit, rit_end = collectRays.end();
+
+			for (rit = collectRays.begin(); rit != rit_end; ++ rit)
+			{
+				float p = RandomValue(0.0f, (float)collectRays.size());
 			
-					if (p < raysOut)
-						vcRays.push_back(*rit);
-				}
-
-				//Debug << "#rays: " << (int)vcRays.size() << endl;
-
-				//-- export rays piercing this view cell
-				exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
+				if (p < raysOut)
+					vcRays.push_back(*rit);
 			}
-		
-			// associate new rays with output view cell
-			if (0)
-			{
-				VssRayContainer vcRays;
-				raysOut = min((int)rays.size(), mVisualizationSamples);
-
-				// check whether we can add the current ray to the output rays
-				for (int k = 0; k < raysOut; ++ k)
-				{
-					VssRay *ray = rays[k];
-					for	(int j = 0; j < (int)ray->mViewCells.size(); ++ j)
-					{
-						ViewCell *rayvc = ray->mViewCells[j];
-	
-						if (rayvc == vc)
-							vcRays.push_back(ray);
-					}
-				}	
-				
-				//-- export rays piercing this view cell
-				exporter->ExportRays(vcRays, RgbColor(1, 1, 0));
-			}
-
+
+			//////////
+			//-- export rays piercing this view cell
+			exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
 		}
 		
@@ -4390,6 +4317,5 @@
 			////////
 			//-- export pvs
-			ObjectPvsMap::const_iterator oit,
-				oit_end = pvs.mEntries.end();
+			ObjectPvsMap::const_iterator oit, oit_end = pvs.mEntries.end();
 
 			Intersectable::NewMail();
@@ -4573,4 +4499,8 @@
 													) const
 {
+	// out of bounds cell
+	if (vc->GetId() == -1)
+		return;
+
 	if (clipPlane)
 	{
@@ -4588,6 +4518,5 @@
 			BspNodeGeometry back;
 
-			BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-			mVspBspTree->ConstructGeometry(leaf, geom);
+			mVspBspTree->ConstructGeometry(*it, geom);
 
 			const float eps = 0.0001f;
@@ -4605,7 +4534,4 @@
 								   mViewSpaceBox, 
 								   eps);
-	
-				//Debug << "geo size: " << geom.Size() << endl;
-				//Debug << "size b: " << back.Size() << " f: " << front.Size() << endl;
 
 				if (back.Valid())
@@ -4646,11 +4572,10 @@
 		for (int j = 0; j < (int)leaves.size(); ++ j)
 		{
-			BspLeaf *leaf = dynamic_cast<BspViewCell *>(leaves[i])->mLeaf;
+			BspLeaf *leaf = dynamic_cast<BspViewCell *>(leaves[i])->mLeaves[0];
 
 			if (i != j)
 			{
-				BspLeaf *leaf2 =dynamic_cast<BspViewCell *>(leaves[j])->mLeaf;
-				
-				int dist = mVspBspTree->TreeDistance(leaf, leaf2);
+				BspLeaf *leaf2 =dynamic_cast<BspViewCell *>(leaves[j])->mLeaves[0];
+				const int dist = mVspBspTree->TreeDistance(leaf, leaf2);
 				
 				if (dist > maxDist)
@@ -4678,7 +4603,5 @@
 void VspBspViewCellsManager::CreateMesh(ViewCell *vc)
 {
-	//if (vc->GetMesh()) delete vc->GetMesh();
 	BspNodeGeometry geom;
-
 	mVspBspTree->ConstructGeometry(vc, geom);
 	
@@ -4710,12 +4633,8 @@
 	{
 		BspNodeGeometry geom;
-		BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-		mVspBspTree->ConstructGeometry(leaf, geom);
+		mVspBspTree->ConstructGeometry(*it, geom);
 
 		const float lVol = geom.GetVolume();
 		const float lArea = geom.GetArea();
-
-		//(*it)->SetVolume(vol);
-		//(*it)->SetArea(area);
 
 		area += lArea;
@@ -4744,6 +4663,5 @@
 	{
 		BspNodeGeometry geom;
-		BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
-		mVspBspTree->ConstructGeometry(leaf, geom);
+		mVspBspTree->ConstructGeometry(*it, geom);
 
 		const float lVol = geom.GetVolume();
@@ -5086,8 +5004,8 @@
 	{
 		VspViewCell *vspVc = dynamic_cast<VspViewCell *>(*it);
-		VspLeaf *l = vspVc->mLeaf;
+		VspLeaf *l = vspVc->mLeaves[0];
 
 		const AxisAlignedBox3 box = 
-			mHierarchyManager->GetVspTree()->GetBoundingBox(vspVc->mLeaf);
+			mHierarchyManager->GetVspTree()->GetBoundingBox(vspVc->mLeaves[0]);
 		
 		if (sceneBox && !Overlap(*sceneBox, box))
@@ -5272,5 +5190,5 @@
 				for (vit = leaves.begin(); vit != vit_end; ++ vit)
 				{
-					VspLeaf *vcLeaf = dynamic_cast<VspViewCell *>(*vit)->mLeaf;
+					VspLeaf *vcLeaf = dynamic_cast<VspViewCell *>(*vit)->mLeaves[0];
 					VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
 
@@ -5441,7 +5359,6 @@
     for (it = leaves.begin(); it != it_end; ++ it)
 	{
-		VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaf;
+		VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaves[0];
 		const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
-
         IncludeBoxInMesh(box, *mesh);
 	}
@@ -5471,5 +5388,5 @@
     for (it = leaves.begin(); it != it_end; ++ it)
 	{
-		VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaf;
+		VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaves[0];
 		
 		const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
@@ -5477,7 +5394,4 @@
 		const float lVol = box.GetVolume();
 		const float lArea = box.SurfaceArea();
-
-		//(*it)->SetVolume(vol);
-		//(*it)->SetArea(area);
 
 		area += lArea;
@@ -5714,6 +5628,5 @@
 	disposeRays(evaluationSamples, NULL);
 }
+
 #endif
-
-
-}
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1551)
@@ -130,4 +130,9 @@
 
 
+ViewCell::~ViewCell()
+{
+}
+
+
 const ObjectPvs &ViewCell::GetPvs() const
 {
@@ -250,5 +255,4 @@
 {
 	const bool result = mPvs.AddSample(sample, pdf, contribution);
-	
 	mPvsSizeValid = false; // have to recompute pvs size
 
@@ -328,9 +332,8 @@
 
 
+
 /************************************************************************/
 /*                class ViewCellsStatistics implementation              */
 /************************************************************************/
-
-
 
 
@@ -2227,5 +2230,6 @@
 		tstack.pop();
 
-		vc->SetId(currentId ++);
+		if (vc->GetId() != -1) // out of bounds
+			vc->SetId(currentId ++);
 
 		if (!vc->IsLeaf())
@@ -2266,10 +2270,9 @@
 		stream << "pvs=\"";
 		
-		//-- export pvs
+		//-- export pvs, i.e., the ids of the objects in the pvs
 		if (exportPvs)
 		{
 			ExportPvs(viewCell, stream);
 		}
-
 		stream << "\" />" << endl;
 	}
@@ -2283,8 +2286,9 @@
 		stream << "pvs=\"";
 
-		//-- NOTE: do not export pvss for interior view cells because
+		// NOTE: do not export pvss for interior view cells because
 		// they can be completely reconstructed from the leaf pvss
-		if (0)
-			ExportPvs(viewCell, stream);
+		// on the other hand: we could store a tag with the compression scheme,
+        // then some scheme were pvs is in the interiors could be used
+		if (0) ExportPvs(viewCell, stream);
 		
 		stream << "\" >" << endl;
@@ -2327,4 +2331,10 @@
 		}
 	}
+}
+
+
+void ViewCellsTree::SetViewCellsManager(ViewCellsManager *vcm)
+{
+	mViewCellsManager = vcm;
 }
 
@@ -2416,6 +2426,4 @@
 
 
-
-
 /************************************************************************/
 /*                    MergeStatistics implementation                    */
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1415)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1551)
@@ -1028,5 +1028,5 @@
 	  leaf->mViewCell = viewCell;
 	  // push back pointer to this leaf
-	  viewCell->mLeaf = leaf;
+	  viewCell->mLeaves[0] = leaf;
       vc.push_back(viewCell);
     } else {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1551)
@@ -107,57 +107,41 @@
 	/** Default destructor.
 	*/
-	virtual ~ViewCell() {}
-
+	virtual ~ViewCell();
 	/** Returns Pvs.
 	*/
 	const ObjectPvs &GetPvs() const;
-
 	/** Returns pvs.
 	*/
 	ObjectPvs &GetPvs();
-
 	/** Completely substitutes the pvs.
 	*/
 	void SetPvs(const ObjectPvs &pvs);
-
 	/** Type of view cells.
 	*/
 	int Type() const;
-
-	
 	/** Adds a passing ray to the passing ray container.
 	*/
 	void AddPassingRay(const Ray &ray, const int contributions);
-
 	/** Returns volume of the view cell.
 	*/
 	float GetVolume() const;
-
 	/** Returns area of the view cell.
 	*/
 	float GetArea() const;
-
 	/** Sets the volume of the view cell.
 	*/
 	void SetVolume(float volume);
-	
 	/** Sets the area of the view cell.
 	*/
 	void SetArea(float area);
-
-
 	/** if this view cell is the root of a view cell hierarchy
 	*/
 	bool IsRoot() const;
-
 	/** Returns parent view cell.
 	*/
 	ViewCellInterior *GetParent() const;
-
 	/** Sets parent of this view cell.
 	*/
 	void SetParent(ViewCellInterior *parent);
-
-
 	/** Sets the mesh for this view cell.
 	*/
@@ -231,8 +215,6 @@
 
 
-
-	////////////////
+	//////////
 	//-- mailing stuff
-
 
 	static void NewMail(const int reserve = 1) 
@@ -346,5 +328,6 @@
 	}
 
-	/** Returns if this view cell is active.
+	/** Returns active view cell, i.e. this view cell or
+		a parent view cell which is set as active view cell.
 	*/
 	ViewCell *GetActiveViewCell() const 
@@ -355,5 +338,4 @@
 	void SetActiveViewCell(ViewCell *vc)
 	{ mActiveViewCell = vc;}
-
 	
 	/** points to the currently active view cell. This is the
@@ -375,5 +357,4 @@
 	ViewCellLeaf(mesh) {  }
 		
-
 	bool IsLeaf() const
 	{
@@ -381,7 +362,6 @@
 	}
 
-
-	/// Leaf of some hierarchy which is part of this view cell.
-	T mLeaf;
+	/// Leaves of some hierarchy which contains this view cell.
+	vector<T> mLeaves;
 };
 
@@ -507,6 +487,4 @@
 	void SetRoot(ViewCell *root);
 
-	//float ComputeVolume(ViewCell *vc);
-
 	/** Assignes unique ids to view cells.
 	*/
@@ -521,8 +499,7 @@
 	int CountKdPvs(const ViewCellLeaf *vc) const;
 
-	void SetViewCellsManager(ViewCellsManager *vcm)
-	{
-		mViewCellsManager = vcm;
-	}
+	/** Sets pointer to view cells manager.
+	*/
+	void SetViewCellsManager(ViewCellsManager *vcm);
 
 protected:
@@ -595,5 +572,5 @@
 	void ResetMergeQueue();
 
-	/** Updates the current top level of view cells.
+	/** Updates the current cut of view cells.
 		@returns number of newly merged view cells
 	*/
@@ -643,19 +620,15 @@
 	/// if the view cell tree hold compressed pvs
 	int mViewCellsStorage;
-
+	/// pointer to the view cells manager
 	ViewCellsManager *mViewCellsManager;
+	/// the root of the view cells hierarchy
 	ViewCell *mRoot;
 
 	/// if merge visualization should be shown
 	bool mExportMergedViewCells;
-
-	
 	/// intermediate container of merged view cells.
 	ViewCellContainer mMergedViewCells;
-	
-
 	/// if merged view cells are refined.
 	bool mRefineViewCells;
-
 	/// weights between variance and render cost increase (must be between zero and one)
 	float mRenderCostWeight;
@@ -666,9 +639,9 @@
     float mDeviation;
 	float mAvgRenderCost;
+	
 	/// the area is used for pvs heuristics
 	int mUseAreaForPvs;
-
+	/// number of currently active view cells (=current cut)
 	int mNumActiveViewCells;
-
 	/// minimal number of view cells
 	int mMergeMinViewCells;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1551)
@@ -614,5 +614,5 @@
 		
 	// set view cell values
-	viewCell->mLeaf = leaf;
+	viewCell->mLeaves.push_back(leaf);
 
 	viewCell->SetVolume(tData.mProbability);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1551)
@@ -103,5 +103,5 @@
 		}
 	}
-//	cout << "here81 " << hitA.mObject << endl;
+
 	const bool validA = 
 		ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
@@ -136,5 +136,5 @@
 			vssRays.push_back(vssRay);
 			++ hits;
-			//cout << "here70 vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
+			//cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
 		}
 
@@ -152,5 +152,5 @@
 			vssRays.push_back(vssRay);
 			++ hits;
-			//cout << "here71 vssray 2: " << *vssRay << endl;
+			//cout << "vssray 2: " << *vssRay << endl;
 		}
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 1287)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 1551)
@@ -75,8 +75,9 @@
 
 
+  /////////////////////////
+
   VspBspTree *mVspBspTree;
   HierarchyManager *mHierarchyManager;
-  //vHierarchy *mBvHierarchy;
-
+ 
   BspTree *mBspTree;
   ViewCellsTree *mViewCellsTree;
@@ -87,6 +88,5 @@
   KdNode *mCurrentOspNode;
   VspNode *mCurrentVspNode;
-  //KdNode *mCurrentKdNode;
-
+ 
   ViewCell *mCurrentViewCell;
     
@@ -95,5 +95,7 @@
   ViewCell *mViewCellRoot;
 
-  ViewCellContainer mViewCells;
+  typedef map<int, ViewCell *> ViewCellsMap;
+  ViewCellsMap mViewCells;
+
   ViewCellsManager *mViewCellsManager;
   ObjectContainer *mObjects;
@@ -117,5 +119,10 @@
   int mObjectSpaceHierarchyType;
 
+  ////////////////////////////////
+
+
   // Handlers for X3D
+  ViewCell *GetOrCreateViewCell(const int id, const bool isLeaf);
+
   void StartBspLeaf(AttributeList& attributes);
   void StartBspInterior(AttributeList& attributes);
@@ -126,5 +133,6 @@
   void EndVspInterior();
 
-  void StartViewCellPvs(ViewCell *viewCell, AttributeList&  attributes);
+  void StartViewCellPvs(ObjectPvs &pvs, const char *ptr);
+
   void EndViewCells();
   void EndBoundingBoxes();
@@ -137,6 +145,5 @@
 
   void StartBoundingBox(AttributeList& attributes);
-  void StartViewCellLeaf(AttributeList& attributes);
-  void StartViewCellInterior(AttributeList& attributes);
+  void StartViewCell(AttributeList& attributes, const bool isLeaf);
   void EndViewCellInterior();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1528)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1551)
@@ -41,5 +41,5 @@
 		hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
 		hitA.mNormal = ray.intersections[0].mNormal; 
-	//	cout << "here91 " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
+	//	cout << "hita: " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
 	}
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1545)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1551)
@@ -186,7 +186,6 @@
 		VssRay *newRay = mRayCaster->CastRay(sray, mViewSpaceBox, false);
 		//cout << "\np1: " << p1 << "\np : " <<  p << "\np2: " << p2 << endl;
-		if (!newRay) 
-			return 0;
-cout << "here42" << endl;
+		if (!newRay) return 0;
+
 		const bool enqueued = HandleRay(newRay);
 		
@@ -410,4 +409,12 @@
 		ConstructViewCells(mViewSpaceBox);
 		cout << "view cells loaded" << endl;
+	}
+	else if (1)
+	{	 cout << "here2255566" << endl;	
+		//-- load view cells from file
+		//-- test successful view cells loading by exporting them again
+		VssRayContainer dummies;
+		mViewCellsManager->Visualize(mObjects, dummies);
+		mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1486)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1551)
@@ -260,5 +260,5 @@
 {
 	// sort view cells to help associating view cells according to their id
-	stable_sort(mViewCells.begin(), mViewCells.end(), vlt);
+	//stable_sort(mViewCells.begin(), mViewCells.end(), vlt);
 
 	// not parsing view cells anymore
@@ -393,5 +393,5 @@
 	{
 		cout << "[";
-		StartViewCellInterior(attributes);
+		StartViewCell(attributes, false);
 	}
 
@@ -399,5 +399,5 @@
 	{
 		cout << "l";
-		StartViewCellLeaf(attributes);
+		StartViewCell(attributes, true);
 	}
 }
@@ -464,92 +464,45 @@
 
 
-void ViewCellsParseHandlers::StartViewCellPvs(ViewCell *viewCell, 
-											  AttributeList&  attributes)
-{
-	int len = attributes.getLength();
+void ViewCellsParseHandlers::StartViewCellPvs(ObjectPvs &pvs, const char *ptr)
+{
+	// handle obect indices
 	vector<int> objIndices;
-
-	for (int i = 0; i < len; ++ i) 
-	{
-		string attrName(StrX(attributes.getName(i)).LocalForm());
-		
-		if (attrName == "pvs") 
-		{
-			StrX attrValue(attributes.getValue(i));
+	char *endptr;
 			
-			// handle coordIndex
-			objIndices.clear();
-			const char *ptr = attrValue.LocalForm();
-			char *endptr;
-			
-			while (1)
-			{
-				const int index = strtol(ptr, &endptr, 10);
-
-				if (ptr == endptr) break;
-
-				objIndices.push_back(index);
-
-				ptr = endptr;
-			}
-
-			//TODO: find objects and add them to pvs
-			// TODO: get view cell with specified id
-			MeshInstance dummyInst(NULL);
-
-			vector<int>::const_iterator it, it_end = objIndices.end();
-			for (it = objIndices.begin(); it != it_end; ++ it)
-			{
-				const int objId = *it;	
-				dummyInst.SetId(objId);
-
-				ObjectContainer::iterator oit =
-					lower_bound(mObjects->begin(), 
-								mObjects->end(), 
-								(Intersectable *)&dummyInst, ilt);	
-							
-				if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
-				{
-					// $$JB we should store a float a per object which corresponds
-					// to sumof pdfs, i.e. its relative visibility
-					// temporarily set to 1.0f
-					viewCell->GetPvs().AddSample(*oit, 1.0f);				
-				}
-				else
-				{
-					cout << "error: object with id " << objId << " does not exist" << endl;
-				}
-			}
-		}
-		else if (attrName == "id") 
-		{
-			StrX attrValue(attributes.getValue(i));
-			
-			const char *ptr = attrValue.LocalForm();
-			char *endptr = NULL;
-			const int id = strtol(ptr, &endptr, 10);
-
-			viewCell->SetId(id);
-		}
-		/*else if (attrName == "active")
-		{
-			StrX attrValue(attributes.getValue(i));
-			
-			const char *ptr = attrValue.LocalForm();
-			char *endptr = NULL;
-			const bool isActive = (bool)strtol(ptr, &endptr, 10);
-
-			if (isActive)
-				viewCell->SetActive();
-		}*/
-		else if (attrName == "mergecost")
-		{
-			StrX attrValue(attributes.getValue(i));
-			
-			const char *ptr = attrValue.LocalForm();
-			char *endptr = NULL;
-			const float cost = (float)strtod(ptr, &endptr);
-
-			viewCell->SetMergeCost(cost);
+	while (1)
+	{	// read object ids
+		const int index = strtol(ptr, &endptr, 10);
+		if (ptr == endptr) 
+			break;
+		objIndices.push_back(index);
+		ptr = endptr;
+	}
+
+	// TODO: 
+	// 1) find objects and add them to pvs
+	// 2) get view cell with specified id
+	MeshInstance dummyInst(NULL);
+
+	vector<int>::const_iterator it, it_end = objIndices.end();
+	for (it = objIndices.begin(); it != it_end; ++ it)
+	{
+		const int objId = *it;	
+		dummyInst.SetId(objId);
+
+		ObjectContainer::iterator oit =
+			lower_bound(mObjects->begin(), 
+			mObjects->end(), 
+			(Intersectable *)&dummyInst, ilt);	
+
+		if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
+		{
+			// $$JB we should store a float a per object which corresponds
+			// to sumof pdfs, i.e. its relative visibility
+			// temporarily set to 1.0f
+			pvs.AddSample(*oit, 1.0f);				
+		}
+		else
+		{
+			cout << "error: object with id " << objId << " does not exist" << endl;
 		}
 	}
@@ -705,4 +658,5 @@
 	}
 
+	///////////
 	//-- find associated view cell
 
@@ -728,11 +682,6 @@
 	{
 		// TODO: get view cell with specified id
-		ViewCellInterior dummyVc;
-		dummyVc.SetId(viewCellId);
-
-		ViewCellContainer::iterator vit =
-			lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
-			
-		BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit);
+		ViewCellsMap::iterator vit = mViewCells.find(viewCellId);
+		BspViewCell *viewCell = dynamic_cast<BspViewCell *>((*vit).second);
 	
 		if (viewCell->GetId() == viewCellId)
@@ -740,5 +689,5 @@
 			// create new view cell for bsp nodes
 			leaf->SetViewCell(viewCell);
-			viewCell->mLeaf = leaf;
+			viewCell->mLeaves.push_back(leaf);
 		}
 		else
@@ -793,44 +742,102 @@
 
 
-void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes)
-{
-	ViewCellLeaf *viewCell = new ViewCellLeaf();
-
-	if (mCurrentViewCell) // replace front or (if not NULL) back child
-	{
-		ViewCellInterior *interior = 
-			dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
-		interior->SetupChildLink(viewCell);
-	}
-	else // root
-	{
-		mViewCellsTree->SetRoot(viewCell);
-	}
-
-	StartViewCellPvs(viewCell, attributes);
-
-	// collect leaves
-	mViewCells.push_back(viewCell);
-}
-
-
-void ViewCellsParseHandlers::StartViewCellInterior(AttributeList& attributes)
-{
-	ViewCellInterior* interior = new ViewCellInterior();
+ViewCell *ViewCellsParseHandlers::GetOrCreateViewCell(const int id, const bool isLeaf)
+{
+	ViewCellsMap::iterator vit = mViewCells.find(id);
+
+	if (vit != mViewCells.end())
+	{
+		return (*vit).second;
+	}
+	else
+	{
+		ViewCell *vc;
+		if (isLeaf)
+		{
+			vc = new ViewCellLeaf();
+		}
+		else
+		{
+			vc = new ViewCellInterior();
+		}
+
+		vc->SetId(id);
+		mViewCells[id] = vc;
+		return vc;
+	}
+}
+
+
+void ViewCellsParseHandlers::StartViewCell(AttributeList& attributes, const bool isLeaf)
+{
+	ViewCell *viewCell = NULL;
 	
-	if (mCurrentViewCell) // replace NULL child of parent with current node
-	{
-		ViewCellInterior *current = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
-
-		current->SetupChildLink(interior);
-	}
-	else
-	{
-		mViewCellsTree->SetRoot(interior);
-	}
-
-	mCurrentViewCell = interior;
-
-	StartViewCellPvs(interior, attributes);
+	const int len = attributes.getLength();
+	float mergeCost;
+	ObjectPvs pvs;
+
+	for (int i = 0; i < len; ++ i) 
+	{
+		const string attrName(StrX(attributes.getName(i)).LocalForm());
+	
+		if (attrName == "id") 
+		{
+			const StrX attrValue(attributes.getValue(i));
+			const char *ptr = attrValue.LocalForm();
+			char *endptr = NULL;
+			const int id = strtol(ptr, &endptr, 10);
+
+			// create new view cell, otherwise use reference.
+			viewCell = GetOrCreateViewCell(id, isLeaf);
+			
+			if (mCurrentViewCell) // replace front or (if not NULL) back child
+			{	
+				ViewCellInterior *interior = 
+					dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
+				interior->SetupChildLink(viewCell);
+			}
+			else 
+			{	// set the new root
+				mViewCellsTree->SetRoot(viewCell);
+			}
+			
+			if (!isLeaf)
+			{
+				mCurrentViewCell = viewCell;
+			}
+		}
+		if (attrName == "pvs") 
+		{
+			StrX attrValue(attributes.getValue(i));
+			const char *ptr = attrValue.LocalForm();
+
+			// note: id must come before pvs! 
+			// otherwise view cell is undefined
+			StartViewCellPvs(pvs, ptr);
+		}
+		else if (attrName == "active")
+		{
+			StrX attrValue(attributes.getValue(i));
+			const char *ptr = attrValue.LocalForm();
+			char *endptr = NULL;
+			const bool isActive = (bool)strtol(ptr, &endptr, 10);
+
+			if (isActive)
+			{
+				// TODO
+			}
+		}
+		else if (attrName == "mergecost")
+		{
+			StrX attrValue(attributes.getValue(i));
+			
+			const char *ptr = attrValue.LocalForm();
+			char *endptr = NULL;
+			mergeCost = (float)strtod(ptr, &endptr);
+		}
+	}
+
+	viewCell->SetMergeCost(mergeCost);
+	viewCell->SetPvs(pvs);
 }
 
@@ -841,5 +848,4 @@
 	{
 		cout << "hierarchy type: Bsp" << endl;
-
 		mVspBspTree = new VspBspTree();
 
@@ -847,14 +853,16 @@
 		mVspBspTree->mBox = mViewSpaceBox;
 
-		ViewCellContainer::iterator vit, vit_end = mViewCells.end();
-
-		// reset view cells using the current node type
+		ViewCellsMap::iterator vit, vit_end = mViewCells.end();
+
+		// remove view cells and exchange them with the 
+		// view cells specialized for the current hierarchy node type
 		for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
 		{
-			ViewCell *vc = *vit;
-
+			ViewCell *vc = (*vit).second;
+			if (!vc->IsLeaf()) // exchange only leaves
+				continue;
 			BspViewCell *bspVc = new BspViewCell();
+			bspVc->SetId(vc->GetId());
 			bspVc->SetPvs(vc->GetPvs());
-			bspVc->SetId(vc->GetId());
 
 			if (vc->IsRoot())
@@ -868,5 +876,5 @@
 
 			DEL_PTR(vc);
-			(*vit) = bspVc;
+			(*vit).second = bspVc;
 		}
 	}
@@ -874,5 +882,4 @@
 	{
 		cout << "hierarchy type: Vsp" << endl;
-
 		mVspTree = new VspTree();
 
@@ -880,14 +887,17 @@
 		mVspTree->mBoundingBox = mViewSpaceBox;
 
-		ViewCellContainer::iterator vit, vit_end = mViewCells.end();
+		ViewCellsMap::iterator vit, vit_end = mViewCells.end();
 
 		// reset view cells using the current node type
 		for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
 		{
-			ViewCell *vc = *vit;
+			ViewCell *vc = (*vit).second;
 
 			VspViewCell *vspVc = new VspViewCell();
 			vspVc->SetPvs(vc->GetPvs());
 			vspVc->SetId(vc->GetId());
+
+			if (!vc->IsLeaf()) // exchange only leaves
+				continue;
 
 			if (vc->IsRoot())
@@ -901,5 +911,5 @@
 			
 			DEL_PTR(vc);
-			(*vit) = vspVc;
+			(*vit).second = vspVc;
 		}
 
@@ -970,5 +980,7 @@
 	}
 
-	//-- find associated view cell
+	/////////////
+	//-- find view cell associated with the id
+
 	int viewCellId;
 	
@@ -989,23 +1001,23 @@
 	}
 	
-	if (viewCellId >= 0) // valid view cell
+	if (viewCellId >= 0) // valid view cell found
 	{
 		// TODO: get view cell with specified id
-		ViewCellInterior dummyVc;
+		/*ViewCellInterior dummyVc;
 		dummyVc.SetId(viewCellId);
 
-		//cout << "\nsearching view cell with id " << viewCellId << endl;
 		ViewCellContainer::iterator vit =
 			lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
-			
+		*/
+		ViewCellsMap::iterator vit = mViewCells.find(viewCellId);
 		if (vit == mViewCells.end())
 			cout << "error: view cell " << viewCellId << " not found" << endl;
 	
-		VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit);
+		VspViewCell *viewCell = dynamic_cast<VspViewCell *>((*vit).second);
 		
 		if (viewCell->GetId() == viewCellId)
 		{
 			leaf->SetViewCell(viewCell);
-			viewCell->mLeaf = leaf;
+			viewCell->mLeaves.push_back(leaf);
 		}
 		else
Index: GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1528)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1551)
@@ -1585,5 +1585,5 @@
 		VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
 
-		VspLeaf *leaf = vc->mLeaf;
+		VspLeaf *leaf = vc->mLeaves[0];
 		dirtyList.push_back(leaf->GetSubdivisionCandidate());
 	}
