Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/default.env
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/default.env	(revision 650)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/default.env	(revision 651)
@@ -36,5 +36,5 @@
 	loadInitialSamples  false
 	storeInitialSamples false
-	useViewSpaceBox false
+	useViewSpaceBox true
 #	testBeamSampling true
 }
@@ -185,10 +185,10 @@
 
 	exportToFile false
-	loadFromFile false
+	loadFromFile true
 
 	#type kdTree
 	#type vspKdTree
-	type bspTree
-	#type vspBspTree
+	#type bspTree
+	type vspBspTree
 	
 	#type sceneDependent
@@ -288,5 +288,5 @@
 VspBspTree {
 	Construction {
-		samples 500000
+		samples 50000
 		epsilon 0.00005
 		randomize false
@@ -420,5 +420,5 @@
 		minProbability 0.00001
 		maxRayContribution 9999
-		maxViewCells	100
+		maxViewCells	1000
 
 		# used for pvs criterium
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 650)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 651)
@@ -1262,5 +1262,5 @@
 
 	int totalPvs;
-	float totalRenderCost, float avgRenderCost, float expectedCost;
+	float totalRenderCost, avgRenderCost, expectedCost;
 
 	float deviation = 0;
@@ -1270,6 +1270,4 @@
 	ofstream stats;
 	stats.open("mergeStats.log");
-
-	Debug << "here343" << endl;
 
 	stats 
@@ -1347,5 +1345,6 @@
 
 
-/*float ViewCellsTree::ComputeVolume(ViewCell *vc)
+#if 0
+float ViewCellsTree::ComputeVolume(ViewCell *vc)
 {
 	if (vc->IsLeaf())
@@ -1369,6 +1368,6 @@
 		return volume;
 	}
-}*/
-
+}
+#endif
 
 void ViewCellsTree::SetRoot(ViewCell *root)
@@ -1621,5 +1620,5 @@
 
 
-void ViewCellsTree::PropagateUpPvs(ViewCell *vc)
+void ViewCellsTree::PropagatePvs(ViewCell *vc)
 {
 	while (vc->GetParent())
@@ -1627,4 +1626,33 @@
 		vc->GetParent()->GetPvs().Merge(vc->GetPvs());
 		vc = vc->GetParent();
+	}
+
+	if (vc->IsLeaf())
+		return;
+
+	stack<ViewCell *> tstack;
+
+	tstack.push(vc);
+
+	while (!tstack.empty())
+	{
+		ViewCell *viewCell = tstack.top();
+		tstack.pop();
+
+		if (viewCell != vc)
+		{
+			viewCell->GetPvs().Merge(vc->GetPvs());
+		}
+
+		if (!viewCell->IsLeaf())
+		{
+			ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(viewCell);
+			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+
+			for (it = interior->mChildren.begin(); it != it_end; ++ it)
+			{
+				tstack.push(*it);
+			}
+		}
 	}
 }
@@ -1727,15 +1755,35 @@
 
 
+void ViewCellsTree::CreateUniqueViewCellsIds()
+{
+	stack<ViewCell *> tstack;
+
+	int currentId = 0;
+
+	tstack.push(mRoot);
+
+	while (!tstack.empty())
+	{
+		ViewCell *vc = tstack.top();
+		tstack.pop();
+
+		vc->SetId(currentId ++);
+
+		if (!vc->IsLeaf())
+		{
+			ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
+			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+			for (it = interior->mChildren.begin(); it != it_end; ++ it)
+			{
+				tstack.push(*it);
+			}
+		}
+	}
+}
+
+
 void ViewCellsTree::ExportViewCell(ViewCell *viewCell, ofstream &stream)
 {
 	ObjectPvsMap::iterator it, it_end = viewCell->GetPvs().mEntries.end();
-
-	if (0) // test with empty pvs
-	for (it = viewCell->GetPvs().mEntries.begin(); it != it_end; ++ it)
-	{
-		stream << (*it).first->GetId() << " ";
-	}
-
-	stream << "\" />" << endl;
 
 	if (viewCell->IsLeaf())
@@ -1746,5 +1794,13 @@
 		stream << "mergecost=\"" << viewCell->GetMergeCost() << "\" ";
 		stream << "pvs=\"";
-		stream << "</Leaf>" << endl;
+		if (0)// test with empty pvs
+			for (it = viewCell->GetPvs().mEntries.begin(); it != it_end; ++ it)
+			{
+				stream << (*it).first->GetId() << " ";
+			}
+
+	
+		stream << "\" />" << endl;
+		//stream << " </Leaf>" << endl;
 	}
 	else
@@ -1757,4 +1813,12 @@
 		stream << "mergecost=\"" << viewCell->GetMergeCost() << "\" ";
 		stream << "pvs=\"";
+
+		if (0)// test with empty pvs
+			for (it = viewCell->GetPvs().mEntries.begin(); it != it_end; ++ it)
+			{
+				stream << (*it).first->GetId() << " ";
+			}
+
+		stream << "\" >" << endl;
 
 		ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 650)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 651)
@@ -531,22 +531,4 @@
 protected:
 
-	struct TraversalData
-	{  
-		ViewCellInterior *mParentViewCell;
-		BspNode *mNode;
-		
-
-		TraversalData() {}
-		TraversalData(BspNode *node, ViewCellInterior *vc):
-			 mNode(node), mParentViewCell(vc)
-		{}
-   
-		bool operator<(const TraversalData &b) const 
-		{
-			return mNode->mTimeStamp < b.mNode->mTimeStamp;
-		}
-	};
-
-	typedef priority_queue<TraversalData> TraversalQueue;
 
 	/** HACK
@@ -765,23 +747,5 @@
 protected:
 
-	struct TraversalData
-	{  
-		ViewCellInterior *mParentViewCell;
-		BspNode *mNode;
-		
-
-		TraversalData() {}
-		TraversalData(BspNode *node, ViewCellInterior *vc):
-			 mNode(node), mParentViewCell(vc)
-		{}
-   
-		bool operator<(const TraversalData &b) const 
-		{
-			return mNode->mTimeStamp < b.mNode->mTimeStamp;
-		}
-	};
-
-	typedef priority_queue<TraversalData> TraversalQueue;
-
+	
 	/** Returns node of the spatial hierarchy corresponding to the view cell
 		if such a node exists.
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 650)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 651)
@@ -301,9 +301,10 @@
 	*/
 	int RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
-
-  /** Assign colors to the viewcells so that they can be renderered interactively without
+	
+	/** Assign colors to the viewcells so that they can be renderered interactively without
 	  color flickering.
-  */
-  void AssignRandomColors();
+	  */
+	void AssignRandomColors();
+
 	/** Updates view cell stats for this particular view cell
 	*/
@@ -311,6 +312,6 @@
 	
 
-  /** Get costs resulting from each merge step. */
-  void GetCostFunction(vector<float> &costFunction);
+	/** Get costs resulting from each merge step. */
+	void GetCostFunction(vector<float> &costFunction);
   
 
@@ -351,7 +352,7 @@
 	ViewCell *GetActiveViewCell(ViewCell *vc) const;
 
-	/** Propagates pvs up the tree to the root.
-	*/
-	void PropagateUpPvs(ViewCell *vc);
+	/** Propagates pvs up the tree to the root and downwards the tree.
+	*/
+	void PropagatePvs(ViewCell *vc);
 
 	bool Export(ofstream &stream);
@@ -366,4 +367,9 @@
 
 	//float ComputeVolume(ViewCell *vc);
+
+	/** Assignes unique ids to view cells.
+	*/
+	void CreateUniqueViewCellsIds();
+
 
 protected:
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 639)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 651)
@@ -568,12 +568,11 @@
   }
   else
-  {
-	 
+  {	 
 	  VssRayContainer dummies;
 	  mViewCellsManager->Visualize(mObjects, dummies);
 	  mViewCellsManager->ExportViewCells("test.xml");
   }
+
   VssTree *vssTree = NULL;
-
   
 
@@ -624,5 +623,5 @@
 		cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
 		cout << "#TotalSamples=" << totalSamples/1000
-			<< "k   #SampleContributions=" << passSampleContributions << " ("
+			<< "#SampleContributions=" << passSampleContributions << " ("
 			<< 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
 			<< pvsSize/(float)mObjects.size() << endl
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 590)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 651)
@@ -17,4 +17,5 @@
 class BspTree;
 class ViewCellsManager;
+class ViewCellsTree;
 
 class ViewCellsParseHandlers: public HandlerBase
@@ -67,6 +68,9 @@
   VspKdTree *mVspKdTree;
   BspTree *mBspTree;
-  
+  ViewCellsTree *mViewCellsTree;
+
   BspNode *mCurrentBspNode;
+  ViewCell *mCurrentViewCell;
+
   ViewCellContainer mViewCells;
   ViewCellsManager *mViewCellsManager;
@@ -75,4 +79,6 @@
   AxisAlignedBox3 mViewSpaceBox;
 
+  bool mParseViewCells;
+
   // Handlers for X3D
   void StartBspLeaf(AttributeList& attributes);
@@ -80,14 +86,21 @@
   void EndBspInterior();
  
-  void StartViewCell(AttributeList&  attributes);
+  void StartViewCell(ViewCell *viewCell, AttributeList&  attributes);
   void EndViewCells();
-
+	
   void StartHierarchy(AttributeList& attributes);
 
   void startBspElement(string element, AttributeList& attributes);
   void StartViewSpaceBox(AttributeList&  attributes);
+
+  void StartViewCellLeaf(AttributeList& attributes);
+  void StartViewCellInterior(AttributeList& attributes);
+  void EndViewCellInterior();
+
+
   // -----------------------------------------------------------------------
   //  Handlers for the SAX ErrorHandler interface
   // -----------------------------------------------------------------------
+
   void warning(const SAXParseException& exc);
   void error(const SAXParseException& exc);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 590)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 651)
@@ -70,4 +70,8 @@
   , mVspBspTree(NULL)
   , mBspTree(NULL)
+  , mViewCellsTree(NULL)
+  , mParseViewCells(true)
+  , mCurrentViewCell(NULL)
+  , mCurrentBspNode(NULL)
 {
 	mObjects = objects;
@@ -89,8 +93,18 @@
   StrX lname(name);
   string element(lname.LocalForm());
-  if (element == "Interior")
-	  EndBspInterior();
+
   if (element == "ViewCells")
 	  EndViewCells();
+
+  if (mParseViewCells)
+  {
+	  if (element == "Interior")
+		  EndViewCellInterior();
+  }
+  else
+  {
+	if (element == "Interior")
+		  EndBspInterior();
+  }
 }
 
@@ -105,4 +119,13 @@
 }
 
+
+void ViewCellsParseHandlers::EndViewCellInterior()
+{
+	// go one up in the tree
+	if (mCurrentViewCell->GetParent())
+	{	cout << "]";
+		mCurrentViewCell = mCurrentViewCell->GetParent();
+	}
+}
 
 inline bool vlt(ViewCell *v1, ViewCell *v2)
@@ -151,4 +174,6 @@
 	if (element == "Leaf") 
 	{
+		cout << "l";
+		Debug << "leaf" << endl;
 		StartBspLeaf(attributes);
 	}
@@ -163,7 +188,14 @@
 
 	// decides the used view cell hierarchy
+	if (element == "ViewCells")
+	{
+		cout << "parsing view cells" << endl;
+		mParseViewCells = true;
+	}
+
 	if (element == "Hierarchy")
 	{
-		cout << "h";
+		cout << "parsing spatial hierarchy" << endl;
+		mParseViewCells = false;
 		StartHierarchy(attributes);
 	}
@@ -172,27 +204,39 @@
 	if (element == "ViewSpaceBox")
 	{
-		cout << "vsb";
+		cout << "b";
 		StartViewSpaceBox(attributes);
 	}
 
-	if (element == "ViewCell")
-	{
-		cout << "v";
-		StartViewCell(attributes);
-	}
 
 	// use different methods for the given view cell hierarchy types
-	if (mViewCellsManager)
-	{
-		switch (mViewCellsManager->GetType())
-		{
-		case ViewCellsManager::BSP:
-		case ViewCellsManager::VSP_BSP:
-			startBspElement(element, attributes);
-			break;
+	if (!mParseViewCells)
+	{
+		if (mViewCellsManager)
+		{
+			switch (mViewCellsManager->GetType())
+			{
+			case ViewCellsManager::BSP:
+			case ViewCellsManager::VSP_BSP:
+				startBspElement(element, attributes);
+				break;
 	
-		default:
-			Debug << "not implemented" << endl;
-			break;
+			default:
+				Debug << "not implemented" << endl;
+				break;
+			}
+		}
+	}
+	else
+	{
+		if (element == "Interior") 
+		{
+			cout << "[";
+			StartViewCellInterior(attributes);
+		}
+
+		if (element == "Leaf") 
+		{
+			cout << "l";
+			StartViewCellLeaf(attributes);
 		}
 	}
@@ -209,16 +253,9 @@
 
 
-void ViewCellsParseHandlers::StartViewCell(AttributeList&  attributes)
+void ViewCellsParseHandlers::StartViewCell(ViewCell *viewCell, AttributeList&  attributes)
 {
 	int len = attributes.getLength();
 	vector<int> objIndices;
-  
-//hack!!
-	
-	ViewCell *viewCell = new ViewCellInterior();//mViewCellsManager->GenerateViewCell();
-	viewCell->mIsActive = true;
-
-	mViewCells.push_back(viewCell);
-	
+
 	for (int i = 0; i < len; ++ i) 
 	{
@@ -284,4 +321,24 @@
 			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);
+
+			viewCell->mIsActive = isActive;
+		}
+		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);
+		}
 	}
 }
@@ -357,5 +414,5 @@
 		}
 	}
-
+Debug << "here22" << endl;
 	
 	if (viewCellId >= 0) // valid view cell
@@ -368,14 +425,12 @@
 			lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
 			
-		ViewCellInterior *viewCell = dynamic_cast<ViewCellInterior *>(*vit);
-
-		// giant hack!!
-		BspViewCell *l = dynamic_cast<BspViewCell *>(mViewCellsManager->GenerateViewCell());
-		viewCell->SetupChildLink(l);
+		BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit);
+
+		Debug << "here44" << endl;
 
 		if (viewCell->GetId() == viewCellId)
 		{
-			leaf->SetViewCell(l);
-			l->mLeaf = leaf;
+			leaf->SetViewCell(viewCell);
+			viewCell->mLeaf = leaf;
 		}
 		else
@@ -392,5 +447,5 @@
 			mVspBspTree->PropagateUpValidity(leaf);
 		}
-	}
+	}Debug << "here33" << endl;
 }
 
@@ -439,4 +494,48 @@
 
 
+void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes)
+{
+	Debug << "here111" << endl;
+	BspViewCell *viewCell = new BspViewCell();
+
+	if (mCurrentViewCell) // replace front or (if not NULL) back child
+	{
+		ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
+		interior->SetupChildLink(viewCell);
+	}
+	else // root
+	{
+		mViewCellsTree->SetRoot(viewCell);
+	}
+
+	StartViewCell(viewCell, attributes);
+
+	// collect leaves
+	mViewCells.push_back(viewCell);
+}
+
+
+void ViewCellsParseHandlers::StartViewCellInterior(AttributeList& attributes)
+{
+	Debug << "here222" << endl;
+	ViewCellInterior* interior = new ViewCellInterior();
+	
+	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;
+
+	StartViewCell(interior, attributes);
+}
+
+
 
 void ViewCellsParseHandlers::CreateViewCellsManager(const char *name)
@@ -448,5 +547,5 @@
 		mBspTree = new BspTree();
 		mBspTree->mBox = mViewSpaceBox;
-		mCurrentBspNode = mBspTree->GetRoot();
+		//mCurrentBspNode = mBspTree->GetRoot();
 
 		mViewCellsManager = new BspViewCellsManager(mBspTree);
@@ -457,5 +556,5 @@
 
 		mVspBspTree = new VspBspTree();
-		mCurrentBspNode = mVspBspTree->GetRoot();
+		//mCurrentBspNode = mVspBspTree->GetRoot();
 
 		mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
@@ -476,4 +575,5 @@
 	}
 
+	mViewCellsTree = mViewCellsManager->GetViewCellsTree();
 	mViewCellsManager->SetViewSpaceBox(mViewSpaceBox);
 }
@@ -625,5 +725,5 @@
    // assign new view cells manager
   *viewCells = handler.mViewCellsManager;
-
+  
   if (errorCount > 0)
     return false;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 650)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 651)
@@ -908,6 +908,9 @@
 void ViewCellsManager::CreateUniqueViewCellIds()
 {
-	for (int i = 0; i < (int)mViewCells.size(); ++ i)
-		mViewCells[i]->SetId(i);
+	if (ViewCellsTreeConstructed())
+		mViewCellsTree->CreateUniqueViewCellsIds();
+	else
+		for (int i = 0; i < (int)mViewCells.size(); ++ i)
+			mViewCells[i]->SetId(i);
 }
 
@@ -2948,8 +2951,8 @@
 
 	
-	if (0) // export view cells
+	if (1) // export view cells
 	{	// hack pvs
 		int savedColorCode = mColorCode;
-		mColorCode = 1;
+		mColorCode = 0;
 		Exporter *exporter = Exporter::GetExporter("final_view_cells.x3d");
 		
@@ -3169,6 +3172,5 @@
 				exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
 			}
-
-			
+		
 
 			if (1)
@@ -3195,6 +3197,4 @@
 		}
 		
-		
-
 
 		exporter->SetWireframe();
