Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1278)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1284)
@@ -671,10 +671,4 @@
 	bool mShowVisualization;
 
-	// HACK in order to detect empty view cells
-	void CollectEmptyViewCells();
-	void TestEmptyViewCells(const ObjectContainer &obj);
-
-	ViewCellContainer mEmptyViewCells;
-
 	int mRenderCostEvaluationType;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1278)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1284)
@@ -264,79 +264,8 @@
 }
 
-void ViewCellsManager::CollectEmptyViewCells()
-{
-	mEmptyViewCells.clear();
-	ViewCellContainer leaves;
-	mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
-
-	ViewCellContainer::const_iterator it, it_end = leaves.end();
-
-	cout << "collecting empty view cells" << endl;
-	
-	for (it = leaves.begin(); it != it_end; ++ it)
-	{
-		if ((*it)->GetPvs().Empty())
-		{
-			mEmptyViewCells.push_back(*it);
-		}
-	}
-	Debug << "empty view cells found: " << (int)mEmptyViewCells.size() << endl;
-}
-
 
 bool ViewCellsManager::EqualToSpatialNode(ViewCell *viewCell) const 
 { 
 	return false;
-}
-
-
-void ViewCellsManager::TestEmptyViewCells(const ObjectContainer &obj)
-{
-	ViewCellContainer::const_iterator it, it_end = mEmptyViewCells.end();
-
-	char buf[50];
-	int i = 0;
-	for (it = mEmptyViewCells.begin(); it != it_end; ++ it)
-	{
-		if (!(*it)->GetPvs().Empty())
-		{
-			sprintf(buf, "empty-viewcells-%09d.x3d", i/*(*it)->GetId()*/);
-			Exporter *exporter = Exporter::GetExporter(buf);
-			
-			if (exporter && i < 20)
-			{
-				Ray *pray = (*it)->mPiercingRays[0];
-				Debug << "view cell " << (*it)->GetId() << " not empty, pvs: " 
-					  << (*it)->GetPvs().CountObjectsInPvs() << " " << (int)pray->intersections.size() << endl;
-
-				exporter->ExportRays((*it)->mPiercingRays);
-							
-				exporter->SetFilled();
-				exporter->SetForcedMaterial(RgbColor(0,0,1));
-
-				for (int j = 0; j < (int)pray->intersections.size(); ++ j)
-				{
-					if (pray->intersections[j].mObject)
-						exporter->ExportIntersectable(pray->intersections[j].mObject);
-				}
-
-				//exporter->SetWireframe();
-				exporter->SetForcedMaterial(RgbColor(0,1,0));
-				exporter->ExportGeometry(obj);
-
-				exporter->SetFilled();
-
-				exporter->SetForcedMaterial(RgbColor(1,0,0));
-				ExportViewCellGeometry(exporter, *it);
-
-				delete exporter;		
-			}
-
-		
-			++ i;
-		}
-	}
-	Debug << "\nSampled " << i << " new view cells (" 
-		  << " of " << (int)mEmptyViewCells.size() << ")" << endl << endl;
 }
 
@@ -1582,15 +1511,4 @@
 	vc->GetPvs().Merge(right->GetPvs());
 
-	//-- merge ray sets
-	if (0)
-	{
-		stable_sort(left->mPiercingRays.begin(), left->mPiercingRays.end());
-		stable_sort(right->mPiercingRays.begin(), right->mPiercingRays.end());
-
-		std::merge(left->mPiercingRays.begin(), left->mPiercingRays.end(),
-				   right->mPiercingRays.begin(), right->mPiercingRays.end(),
-				   vc->mPiercingRays.begin());
-	}
-
 	// set only links to child (not from child to parent, maybe not wished!!)
 	vc->mChildren.push_back(left);
@@ -2003,8 +1921,14 @@
 {
 	if (ViewCellsTreeConstructed())
+	{
 		mViewCellsTree->CreateUniqueViewCellsIds();
-	else
+	}
+	else // no view cells tree, handle view cells "myself"
+	{
 		for (int i = 0; i < (int)mViewCells.size(); ++ i)
+		{
 			mViewCells[i]->SetId(i);
+		}
+	}
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1264)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1284)
@@ -104,5 +104,4 @@
 ViewCell::ViewCell(): 
 MeshInstance(NULL), 
-mPiercingRays(0),
 mArea(-1),
 mVolume(-1),
@@ -118,5 +117,4 @@
 ViewCell::ViewCell(Mesh *mesh): 
 MeshInstance(mesh), 
-mPiercingRays(0),
 mArea(-1),
 mVolume(-1),
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1264)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1284)
@@ -191,8 +191,6 @@
 	bool AddPvsSample(Intersectable *sample, const float pdf, float &contribution);
   
-
 	/// Rays piercing this view cell.
-	RayContainer mPiercingRays;
-
+	//RayContainer mPiercingRays;
 
 	/** if this is a view cell correspending to a leaf in a hierarchy.
@@ -294,4 +292,5 @@
 {
 	friend class ViewCellsManager;
+
 public:
 	ViewCellInterior();
@@ -300,5 +299,4 @@
 	ViewCellInterior(Mesh *mesh);
 	
-
 	/** Sets pointer from parent to child and vice versa.
 	*/
@@ -307,13 +305,14 @@
 	bool IsLeaf() const;
 
-	ViewCellContainer mChildren;
-
-  void SetCost(const float c) {
-	mCost = c;
-  }
-  float GetCost() const {
-	return mCost;
-  }
+	void SetCost(const float c) {
+		mCost = c;
+	}
+	
+	float GetCost() const {
+		return mCost;
+	}
   
+  ViewCellContainer mChildren;
+
 protected:
   /** overall cost resulting from the merge */
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1272)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1284)
@@ -134,5 +134,4 @@
 #endif
 
-  cerr<<"HERE!\n";
 	int returnCode = 0;
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 1278)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 1284)
@@ -118,5 +118,5 @@
   void EndVspInterior();
 
-  void StartViewCell(ViewCell *viewCell, AttributeList&  attributes);
+  void ParseViewCellPvs(ViewCell *viewCell, AttributeList&  attributes);
   void EndViewCells();
   void EndBoundingBoxes();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1233)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1284)
@@ -95,6 +95,5 @@
 			return 0;
 		}
-	}
-				 
+	}			 
 
 	// compute leaf pvs
@@ -120,5 +119,5 @@
                        
 				// add #objects exclusivly in this node
-				pvs += leaf->mObjects.size();
+				pvs += (int)leaf->mObjects.size();
 			}
 			else // traverse tree
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1279)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1284)
@@ -574,7 +574,7 @@
 	
 			const int colorCode = 0;
-			const int maxPvs = 200;//mOspTree.GetStatistics().maxPvs;
-
-			exporter->ExportOspTree(*mOspTree, colorCode == 0 ? 0 : maxPvs);
+			const int maxPvs = 0;//mOspTree.GetStatistics().maxPvs;
+
+			exporter->ExportOspTree(*mOspTree, 0);
 
 			delete exporter;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1278)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1284)
@@ -311,8 +311,5 @@
 															  AttributeList& attributes)
 {
-	//if (!mViewCellsManager)return;
-
 	//-- use cell type according to the chosen method
-
 	switch (mObjectSpaceHierarchyType)
 	{
@@ -336,5 +333,5 @@
 	if (element == "Interior") 
 	{
-		Debug << "[";
+		cout << "[";
 		StartViewCellInterior(attributes);
 	}
@@ -342,5 +339,5 @@
 	if (element == "Leaf") 
 	{
-		Debug << "l";
+		cout << "l";
 		StartViewCellLeaf(attributes);
 	}
@@ -390,5 +387,5 @@
 	if (element == "BoundingBox")
 	{
-		Debug << "b";
+		cout << "b";
 		StartBoundingBox(attributes);
 	}
@@ -415,6 +412,6 @@
 
 
-void ViewCellsParseHandlers::StartViewCell(ViewCell *viewCell, 
-										   AttributeList&  attributes)
+void ViewCellsParseHandlers::ParseViewCellPvs(ViewCell *viewCell, 
+											  AttributeList&  attributes)
 {
 	int len = attributes.getLength();
@@ -468,5 +465,5 @@
 				else
 				{
-					Debug << "error: object with id " << objId << " does not exist" << endl;
+					cout << "error: object with id " << objId << " does not exist" << endl;
 				}
 			}
@@ -524,10 +521,10 @@
 			if (strcmp(ptr, "bsp") == 0)
 			{
-				cout << "view space hierarchy: Bsp" << endl;
+				cout << "\nview space hierarchy: Bsp" << endl;
 				mViewSpaceHierarchyType = BSP;
 			}
 			else if (strcmp(ptr, "vsp") == 0)
 			{
-				Debug << "view space hierarchy: Vsp" << endl;
+				cout << "\nview space hierarchy: Vsp" << endl;
 				mViewSpaceHierarchyType = VSP;
 			}
@@ -557,5 +554,5 @@
 
 	Vector3 bmin, bmax;
-
+cout << "here3" << endl;
 	for (int i = 0; i < len; ++ i) 
 	{
@@ -569,11 +566,13 @@
 			if (strcmp(ptr, "osp") == 0)
 			{
-				Debug << "object space hierarchy: Osp" << endl;
-				mHierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV);
+				cout << "\nobject space hierarchy: Osp" << endl;
+				mHierarchyManager = 
+					new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV);
 			}
 			else if (strcmp(ptr, "bvh") == 0)
 			{
-				Debug << "object space hierarchy: Bvh" << endl;
-				mHierarchyManager = new HierarchyManager(mVspTree, HierarchyManager::BV_BASED_OBJ_SUBDIV);
+				cout << "\nobject space hierarchy: Bvh" << endl;
+				mHierarchyManager = 
+					new HierarchyManager(mVspTree, HierarchyManager::BV_BASED_OBJ_SUBDIV);
 			}
 		}
@@ -650,5 +649,4 @@
 		}
 	}
-
 	
 	if (viewCellId >= 0) // valid view cell found
@@ -665,4 +663,5 @@
 		if (viewCell->GetId() == viewCellId)
 		{
+			// create new view cell for bsp nodes
 			leaf->SetViewCell(viewCell);
 			viewCell->mLeaf = leaf;
@@ -670,5 +669,5 @@
 		else
 		{
-			Debug << "error: view cell does not exist" << endl;
+			cout << "error: view cell does not exist" << endl;
 		}
 	}
@@ -724,9 +723,10 @@
 void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes)
 {
-	BspViewCell *viewCell = new BspViewCell();
+	ViewCellLeaf *viewCell = new ViewCellLeaf();
 
 	if (mCurrentViewCell) // replace front or (if not NULL) back child
 	{
-		ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
+		ViewCellInterior *interior = 
+			dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
 		interior->SetupChildLink(viewCell);
 	}
@@ -736,5 +736,5 @@
 	}
 
-	StartViewCell(viewCell, attributes);
+	ParseViewCellPvs(viewCell, attributes);
 
 	// collect leaves
@@ -746,5 +746,5 @@
 {
 	ViewCellInterior* interior = new ViewCellInterior();
-	
+	cout << "here7"<<endl;
 	if (mCurrentViewCell) // replace NULL child of parent with current node
 	{
@@ -760,5 +760,5 @@
 	mCurrentViewCell = interior;
 
-	StartViewCell(interior, attributes);
+	ParseViewCellPvs(interior, attributes);
 }
 
@@ -766,4 +766,11 @@
 void ViewCellsParseHandlers::CreateViewSpaceHierarchy()
 {
+	ViewCellContainer::iterator it, it_end = mViewCells.end();
+	Debug << endl;
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+	{
+		Debug << (*it)->GetId() << " ";
+	}
+Debug << endl;
 	if (mViewSpaceHierarchyType == BSP)
 	{
@@ -774,4 +781,30 @@
 		// set view space box
 		mVspBspTree->mBox = mViewSpaceBox;
+
+		ViewCellContainer::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;
+
+			BspViewCell *bspVc = new BspViewCell();
+			bspVc->SetPvs(vc->GetPvs());
+			bspVc->SetId(vc->GetId());
+
+			if (vc->IsRoot())
+			{
+				mViewCellsTree->mRoot = bspVc;
+			}
+			else
+			{
+                vc->GetParent()->SetupChildLink(bspVc);
+				vc->GetParent()->RemoveChildLink(vc);
+			}
+
+			DEL_PTR(vc);
+			(*vit) = bspVc;
+		}
+
 	}
 	else if (mViewSpaceHierarchyType == VSP) 
@@ -784,5 +817,30 @@
 		mVspTree->mBoundingBox = mViewSpaceBox;
 
-		// object space hierarchy already constructed
+		ViewCellContainer::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;
+
+			VspViewCell *vspVc = new VspViewCell();
+			vspVc->SetPvs(vc->GetPvs());
+			vspVc->SetId(vc->GetId());
+
+			if (vc->IsRoot())
+			{
+				mViewCellsTree->mRoot = vspVc;
+			}
+			else
+			{
+                vc->GetParent()->SetupChildLink(vspVc);
+				vc->GetParent()->RemoveChildLink(vc);
+			}
+			
+			DEL_PTR(vc);
+			(*vit) = vspVc;
+		}
+
+		// if object space hierarchy already constructed
 		if (mHierarchyManager)
 		{
@@ -791,5 +849,11 @@
 		}
 	}
-	
+	Debug << "************************" << endl;
+	it_end = mViewCells.end();
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+	{
+		Debug << (*it)->GetId() << " ";
+	}
+	Debug << endl;
 	cout << "\nview space box: " << mViewSpaceBox << endl;
 }
@@ -879,7 +943,12 @@
 		dummyVc.SetId(viewCellId);
 
+		cout << "\nsearching view cell with id " << viewCellId << endl;
+
 		ViewCellContainer::iterator vit =
 			lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
 			
+		if (vit == mViewCells.end())
+			cout << "error: view cell " << viewCellId << " not found" << endl;
+
 		VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit);
 	
@@ -891,5 +960,5 @@
 		else
 		{
-			Debug << "error: view cell does not exist" << endl;
+			cout << "error: view cell does not exist" << endl;
 		}
 	}
@@ -917,6 +986,5 @@
 		if (attrName == "plane") 
 		{
-			sscanf(ptr, "%d %f",
-				   &plane.mAxis, &plane.mPosition);
+			sscanf(ptr, "%d %f", &plane.mAxis, &plane.mPosition);
 		}
 	}
