Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 588)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 590)
@@ -98,5 +98,5 @@
 	useViewcells true
 	updateSubdivision true
-	loadInitialSamples false
+	loadIfnitialSamples false
 	storeInitialSamples false
 }
@@ -212,5 +212,5 @@
 		useRaysForMerge false
 		refine false
-		compress false
+		compress true
 	}
 
@@ -287,5 +287,5 @@
 		epsilon 0.005
 		randomize false
-		renderCostWeight 0.5
+		renderCostWeight 1.0
 	}
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 587)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 590)
@@ -23,4 +23,5 @@
 class MergeCandidate;
 class Beam;
+class ViewCellsTree;
 
 struct BspRay;
@@ -298,4 +299,5 @@
 	void CollapseViewCells();
 
+	ViewCellsTree *mViewCellsTree;
 protected:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 589)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 590)
@@ -107,5 +107,6 @@
 mValid(true),
 mParent(NULL),
-mTimeStamp(0)
+mTimeStamp(0),
+mIsActive(false)
 {
 }
@@ -118,5 +119,6 @@
 mValid(true),
 mParent(NULL),
-mTimeStamp(0)
+mTimeStamp(0),
+mIsActive(false)
 {
 }
@@ -1310,4 +1312,8 @@
 		if (vc->IsLeaf() || ((viewCells.size() + tqueue.size()) >= numViewCells))
 		{
+			// todo: should be done with a function taking the active flag and some 
+			// time stamp so I don't have to reset view cells, this also means that 
+			// the leaf view cells can be set active fist
+			vc->mIsActive = true; 
 			viewCells.push_back(vc);
 		}
@@ -1502,5 +1508,8 @@
 int ViewCellsTree::GetNumPvsEntries(ViewCell *vc) const
 {
-	int pvsSize = vc->GetPvs().GetSize();
+	int pvsSize = 0;
+	// only count leaves for uncompressed method for fairness
+	if (mIsCompressed || vc->IsLeaf())
+		pvsSize = vc->GetPvs().GetSize();
 
 	if (!vc->IsLeaf())
@@ -1523,4 +1532,15 @@
 {
 	return mIsCompressed;
+}
+
+
+ViewCell *ViewCellsTree::GetActiveViewCell(ViewCell *vc) const
+{
+	while (vc->GetParent() && !vc->mIsActive)
+	{
+		vc = vc->GetParent();
+	}
+
+	return vc;
 }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 587)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 590)
@@ -20,6 +20,5 @@
 class ViewCellsStatistics;
 class ViewCellsManager;
-
-
+class ViewCellsTree;
 
 class BspNodeGeometry
@@ -322,7 +321,7 @@
 public:
 	BspLeaf();
-	BspLeaf(BspViewCell *viewCell);
+	BspLeaf(ViewCell *viewCell);
 	BspLeaf(BspInterior *parent);
-	BspLeaf(BspInterior *parent, BspViewCell *viewCell);
+	BspLeaf(BspInterior *parent, ViewCell *viewCell);
 
 	~BspLeaf();
@@ -334,9 +333,9 @@
 	/** Returns pointer of view cell.
 	*/
-	BspViewCell *GetViewCell() const;
+	ViewCell *GetViewCell() const;
 
 	/** Sets pointer to view cell.
 	*/
-	void SetViewCell(BspViewCell *viewCell);
+	void SetViewCell(ViewCell *viewCell);
 
 	/// Rays piercing this leaf.
@@ -352,5 +351,5 @@
 	
 	/// if NULL this does not correspond to feasible viewcell
-	BspViewCell *mViewCell;
+	ViewCell *mViewCell;
 };
 
@@ -500,7 +499,6 @@
 	BspNode *GetRoot() const;
 
-	/** Exports Bsp tree to file.
-	*/
-	bool Export(const string filename);
+	
+	//bool Export(const string filename);
 
 	/** Collects the leaf view cells of the tree
@@ -558,7 +556,4 @@
 	BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
 
-	/** Returns view cell corresponding to unbounded space.
-	*/
-	BspViewCell *GetRootCell() const;
 
 	/** Returns epsilon of this tree.
@@ -571,4 +566,17 @@
 	int CollectMergeCandidates(const VssRayContainer &rays,
 					           vector<MergeCandidate> &candidates);
+
+	/** Exports Bsp tree to file.
+	*/
+	bool Export(ofstream &stream);
+
+
+	/** Returns view cell corresponding to 
+		the invalid view space. If it does not exist, it is created.
+	*/
+	BspViewCell *GetOutOfBoundsCell();
+
+	ViewCellsTree *mViewCellsTree;
+	
 protected:
 
@@ -593,4 +601,6 @@
 	};
 
+	void ExportNode(BspNode *node, ofstream &stream);
+
 	/** Evaluates tree stats in the BSP tree leafs.
 	*/
@@ -850,5 +860,8 @@
 
 
-
+	/** Returns view cell corresponding to 
+		the invalid view space. If it does not exist, it is created.
+	*/
+	BspViewCell *GetOrCreateOutOfBoundsCell();
 
 	/// Pointer to the root of the tree.
@@ -877,5 +890,5 @@
 
 	/// view cell corresponding to unbounded space
-	BspViewCell *mRootCell;
+	BspViewCell *mOutOfBoundsCell;
 
 	/// if view cells should be generated or the given view cells should be used.
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 587)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 590)
@@ -514,5 +514,11 @@
 	void Finalize(ViewCell *viewCell, const bool createMesh);
 
+	bool ExportViewCells(const string filename);
+
 protected:
+
+	/** HACK
+	*/
+	void AddCurrentViewCellsToHierarchy();
 
 	void CollectViewCells();
@@ -719,4 +725,8 @@
 protected:
 
+	/** HACK
+	*/
+	void AddCurrentViewCellsToHierarchy();
+
 	/** Merges the view cells.
 	*/
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 589)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 590)
@@ -17,4 +17,5 @@
 #include "ViewCellsManager.h"
 #include "Beam.h"
+
 
 //-- static members
@@ -1741,4 +1742,6 @@
 void VspBspTree::CollapseViewCells()
 {
+// TODO
+#if VC_HISTORY
 	stack<BspNode *> nodeStack;
 
@@ -1762,5 +1765,5 @@
 	
 				ViewCellContainer leaves;
-				mViewCellsManager->GetViewCellsTree()->CollectLeaves(viewCell, leaves);
+				mViewCellsTree->CollectLeaves(viewCell, leaves);
 
 				ViewCellContainer::const_iterator it, it_end = leaves.end();
@@ -1788,4 +1791,5 @@
 
 	Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl;
+#endif
 }
 
@@ -1855,6 +1859,7 @@
 			if (!onlyValid || node->TreeValid())
 			{
-				ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
-			
+				ViewCell *viewCell = 
+					mViewCellsTree->GetActiveViewCell(dynamic_cast<BspLeaf *>(node)->GetViewCell());
+						
 				if (!onlyUnmailed || !viewCell->Mailed()) 
 				{
@@ -1872,4 +1877,5 @@
 		}
 	}
+
 }
 
@@ -2058,5 +2064,6 @@
 {
 	ViewCellContainer leaves;
-	mViewCellsManager->GetViewCellsTree()->CollectLeaves(vc, leaves);
+	
+	mViewCellsTree->CollectLeaves(vc, leaves);
 
 	ViewCellContainer::const_iterator it, it_end = leaves.end();
@@ -2065,4 +2072,5 @@
 	{
 		BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
+		
 		ConstructGeometry(l, vcGeom);
 	}
@@ -2433,8 +2441,9 @@
 			BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
 
-			if (!leaf->GetViewCell()->Mailed())
-			{
-				viewcells.push_back(leaf->GetViewCell());
-				leaf->GetViewCell()->Mail();
+			ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
+			if (!viewCell->Mailed())
+			{
+				viewcells.push_back(viewCell);
+				viewCell->Mail();
 				++ hits;
 			}
@@ -2497,4 +2506,6 @@
 BspNode *VspBspTree::CollapseTree(BspNode *node, int &collapsed)
 {
+// TODO
+#if VC_HISTORY
 	if (node->IsLeaf())
 		return node;
@@ -2530,5 +2541,5 @@
 		}
 	}
-
+#endif
 	return node;
 }
@@ -2551,4 +2562,6 @@
 void VspBspTree::RepairViewCellsLeafLists()
 {
+// TODO
+#if VC_HISTORY
 	// list not valid anymore => clear
 	stack<BspNode *> nodeStack;
@@ -2567,6 +2580,5 @@
 
 			BspViewCell *viewCell = leaf->GetViewCell();
-	// TODO
-#if VC_HISTORY
+
 			if (!viewCell->Mailed())
 			{
@@ -2576,15 +2588,16 @@
 	
 			viewCell->mLeaves.push_back(leaf);
+
+		}
+		else
+		{
+			BspInterior *interior = dynamic_cast<BspInterior *>(node);
+
+			nodeStack.push(interior->GetFront());
+			nodeStack.push(interior->GetBack());
+		}
+	}
 // TODO
 #endif
-		}
-		else
-		{
-			BspInterior *interior = dynamic_cast<BspInterior *>(node);
-
-			nodeStack.push(interior->GetFront());
-			nodeStack.push(interior->GetBack());
-		}
-	}
 }
 
@@ -2929,8 +2942,9 @@
 	{
 		BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
-			
+		ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
+
 		int id = -1;
-		if (leaf->GetViewCell() != mOutOfBoundsCell)
-			id = leaf->GetViewCell()->GetId();
+		if (viewCell != mOutOfBoundsCell)
+			id = viewCell->GetId();
 
 		stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 589)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 590)
@@ -15,4 +15,6 @@
 #include <stack>
 
+
+
 //-- static members
 
@@ -173,5 +175,5 @@
 
 
-BspLeaf::BspLeaf(BspViewCell *viewCell): 
+BspLeaf::BspLeaf(ViewCell *viewCell): 
 mViewCell(viewCell)
 {
@@ -185,15 +187,15 @@
 
 
-BspLeaf::BspLeaf(BspInterior *parent, BspViewCell *viewCell): 
+BspLeaf::BspLeaf(BspInterior *parent, ViewCell *viewCell): 
 BspNode(parent), mViewCell(viewCell), mPvs(NULL)
 {
 }
 
-BspViewCell *BspLeaf::GetViewCell() const
+ViewCell *BspLeaf::GetViewCell() const
 {
 	return mViewCell;
 }
 
-void BspLeaf::SetViewCell(BspViewCell *viewCell)
+void BspLeaf::SetViewCell(ViewCell *viewCell)
 {
 	mViewCell = viewCell;
@@ -218,6 +220,5 @@
 	Randomize(); // initialise random generator for heuristics
 
-	// the view cell corresponding to unbounded space
-	mRootCell = new BspViewCell();
+	mOutOfBoundsCell = GetOrCreateOutOfBoundsCell();
 
 	//-- termination criteria for autopartition
@@ -443,13 +444,31 @@
 	DEL_PTR(mRoot);
 
-	// HACK: view cells not generated => root cell not used
-	if (mGenerateViewCells)
-		DEL_PTR(mRootCell);
-}
-
-BspViewCell *BspTree::GetRootCell() const
-{
-	return mRootCell;
-}
+	// TODO must be deleted if used!!
+	//if (mGenerateViewCells) DEL_PTR(mOutOfBoundsCell);
+}
+
+BspViewCell *BspTree::GetOutOfBoundsCell()
+{
+	return mOutOfBoundsCell;
+}
+
+
+BspNode *BspTree::GetRoot() const
+{
+	return mRoot;
+}
+
+BspViewCell *BspTree::GetOrCreateOutOfBoundsCell()
+{
+	if (!mOutOfBoundsCell)
+	{
+		mOutOfBoundsCell = new BspViewCell();
+		mOutOfBoundsCell->SetId(-1);
+		mOutOfBoundsCell->SetValid(false);
+	}
+
+	return mOutOfBoundsCell;
+}
+
 
 void BspTree::InsertViewCell(ViewCell *viewCell)
@@ -466,4 +485,5 @@
 }
 
+
 void BspTree::InsertPolygons(PolygonContainer *polys)
 {	
@@ -477,5 +497,5 @@
 								 polys, 
 								 0, 
-								 mRootCell, 
+								 mOutOfBoundsCell, 
 								 new BoundedRayContainer(), 
 								 0, 
@@ -510,11 +530,11 @@
 				
 				// extract view cells associated with the split polygons
-				ViewCell *frontViewCell = mRootCell;
-				ViewCell *backViewCell = mRootCell;
+				ViewCell *frontViewCell = GetOrCreateOutOfBoundsCell();
+				ViewCell *backViewCell = GetOrCreateOutOfBoundsCell();
 			
 				BspTraversalData frontData(interior->GetFront(), 
 										   frontPolys, 
 										   tData.mDepth + 1,
-										   mRootCell,	
+										   mOutOfBoundsCell,	
 										   tData.mRays,
 										   tData.mPvs,
@@ -525,5 +545,5 @@
 										  backPolys,
 										  tData.mDepth + 1,
-										  mRootCell,	
+										  mOutOfBoundsCell,	
 										  tData.mRays,
 										  tData.mPvs,
@@ -640,5 +660,5 @@
 			}
 		
-			AddMeshToPolygons(mesh, polys, mRootCell);
+			AddMeshToPolygons(mesh, polys, mOutOfBoundsCell);
 		}
 	}
@@ -814,5 +834,5 @@
 						   polys, 
 						   0, 
-						   mRootCell, 
+						   GetOrCreateOutOfBoundsCell(), 
 						   rays, 
 						   ComputePvsSize(*rays), 
@@ -881,5 +901,5 @@
 		
 		//-- add pvs
-		if (viewCell != mRootCell)
+		if (viewCell != mOutOfBoundsCell)
 		{
 			int conSamp = 0, sampCon = 0;
@@ -909,7 +929,7 @@
 	PolygonContainer coincident;
 	
-	BspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, 
+	BspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mOutOfBoundsCell, 
 								new BoundedRayContainer(), 0, 0, new BspNodeGeometry());
-	BspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, 
+	BspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mOutOfBoundsCell, 
 							   new BoundedRayContainer(), 0, 0, new BspNodeGeometry());
 
@@ -1812,9 +1832,4 @@
 }
 
-
-BspNode *BspTree::GetRoot() const
-{
-	return mRoot;
-}
 
 void BspTree::EvaluateLeafStats(const BspTraversalData &data)
@@ -2055,16 +2070,4 @@
 }
 
-bool BspTree::Export(const string filename)
-{
-	Exporter *exporter = Exporter::GetExporter(filename);
-
-	if (exporter) 
-	{
-		exporter->ExportBspTree(*this);
-		return true;
-	}	
-
-	return false;
-}
 
 void BspTree::CollectViewCells(ViewCellContainer &viewCells) const
@@ -2689,5 +2692,5 @@
 		if (ray->mViewCells.size() < 2)
 			continue;
-//TODO viewcellhierarchy
+
 		iit = ray->mViewCells.begin();
 		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*(iit ++));
@@ -3023,6 +3026,44 @@
 }
 
+
 void BspNodeGeometry::IncludeInBox(AxisAlignedBox3 &box)
 {
 	Polygon3::IncludeInBox(mPolys, box);
 }
+
+
+bool BspTree::Export(ofstream &stream)
+{
+	ExportNode(mRoot, stream);
+
+	return true;
+}
+
+
+void BspTree::ExportNode(BspNode *node, ofstream &stream)
+{
+	if (node->IsLeaf())
+	{
+		BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
+			
+		int id = -1;
+		if (leaf->GetViewCell() != mOutOfBoundsCell)
+			id = leaf->GetViewCell()->GetId();
+
+		stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
+	}
+	else
+	{
+		BspInterior *interior = dynamic_cast<BspInterior *>(node);
+	
+		Plane3 plane = interior->GetPlane();
+		stream << "<Interior plane=\"" << plane.mNormal.x << " " 
+			   << plane.mNormal.y << " " << plane.mNormal.z << " " 
+			   << plane.mD << "\">" << endl;
+
+		ExportNode(interior->GetBack(), stream);
+		ExportNode(interior->GetFront(), stream);
+
+		stream << "</Interior>" << endl;
+	}
+}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 586)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 590)
@@ -190,6 +190,8 @@
 	static int sReservedMailboxes;
 	
+	bool mIsActive;
 
 protected:
+
 
 	/// the potentially visible objects
@@ -322,4 +324,6 @@
 	bool IsCompressed() const;
 
+	ViewCell *GetActiveViewCell(ViewCell *vc) const;
+
 protected:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 589)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 590)
@@ -535,4 +535,6 @@
   AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
 
+  mSceneGraph->CollectObjects(&mObjects);
+
   //-- load view cells from file if requested
   if (!mLoadViewCells)
@@ -546,9 +548,14 @@
 	  mViewCellsManager->PrintStatistics(Debug);
   }
-	
-
+  else
+  {
+	 
+	  VssRayContainer dummies;
+	  mViewCellsManager->Visualize(mObjects, dummies);
+	  mViewCellsManager->ExportViewCells("test.xml");
+  }
   VssTree *vssTree = NULL;
 
-  mSceneGraph->CollectObjects(&mObjects);
+  
 
   long initialTime = GetTime();
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParserXerces.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParserXerces.h	(revision 577)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParserXerces.h	(revision 590)
@@ -14,4 +14,5 @@
 
 class VspBspTree;
+class VspKdTree;
 class BspTree;
 class ViewCellsManager;
@@ -64,7 +65,8 @@
 
   VspBspTree *mVspBspTree;
+  VspKdTree *mVspKdTree;
   BspTree *mBspTree;
   
-  BspNode *mCurrentNode;
+  BspNode *mCurrentBspNode;
   ViewCellContainer mViewCells;
   ViewCellsManager *mViewCellsManager;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp	(revision 580)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp	(revision 590)
@@ -28,4 +28,5 @@
 #include "VspBspTree.h"
 #include "ViewCellBsp.h"
+#include "VspKdTree.h"
 #include "ViewCellsManager.h"
 
@@ -98,7 +99,7 @@
 {
 	// go one up in the tree
-	if (mCurrentNode->GetParent())
+	if (mCurrentBspNode->GetParent())
 	{	cout << "]";
-		mCurrentNode = mCurrentNode->GetParent();
+		mCurrentBspNode = mCurrentBspNode->GetParent();
 	}
 }
@@ -213,7 +214,11 @@
 	vector<int> objIndices;
   
-	ViewCell *viewCell = mViewCellsManager->GenerateViewCell();
+//hack!!
+	
+	ViewCell *viewCell = new ViewCellInterior();//mViewCellsManager->GenerateViewCell();
+	viewCell->mIsActive = true;
+
 	mViewCells.push_back(viewCell);
-
+	
 	for (int i = 0; i < len; ++ i) 
 	{
@@ -316,9 +321,9 @@
 {
 	BspLeaf * leaf = 
-		new BspLeaf(dynamic_cast<BspInterior *>(mCurrentNode), NULL);
-
-	if (mCurrentNode) // replace front or (if not NULL) back child
-	{
-		dynamic_cast<BspInterior *>(mCurrentNode)->ReplaceChildLink(NULL, leaf);
+		new BspLeaf(dynamic_cast<BspInterior *>(mCurrentBspNode), NULL);
+
+	if (mCurrentBspNode) // replace front or (if not NULL) back child
+	{
+		dynamic_cast<BspInterior *>(mCurrentBspNode)->ReplaceChildLink(NULL, leaf);
 	}
 	else
@@ -353,8 +358,9 @@
 	}
 
+	
 	if (viewCellId >= 0) // valid view cell
 	{
 		// TODO: get view cell with specified id
-		BspViewCell dummyVc;
+		ViewCellInterior dummyVc;
 		dummyVc.SetId(viewCellId);
 
@@ -362,9 +368,14 @@
 			lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
 			
-		BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit);
+		ViewCellInterior *viewCell = dynamic_cast<ViewCellInterior *>(*vit);
+
+		// giant hack!!
+		BspViewCell *l = dynamic_cast<BspViewCell *>(mViewCellsManager->GenerateViewCell());
+		viewCell->SetupChildLink(l);
 
 		if (viewCell->GetId() == viewCellId)
 		{
-			leaf->SetViewCell(viewCell);
+			leaf->SetViewCell(l);
+			l->mLeaf = leaf;
 		}
 		else
@@ -405,7 +416,7 @@
 	BspInterior* interior = new BspInterior(plane);
 	
-	if (mCurrentNode) // replace NULL child of parent with current node
-	{
-		BspInterior *current = dynamic_cast<BspInterior *>(mCurrentNode);
+	if (mCurrentBspNode) // replace NULL child of parent with current node
+	{
+		BspInterior *current = dynamic_cast<BspInterior *>(mCurrentBspNode);
 
 		current->ReplaceChildLink(NULL, interior);
@@ -424,5 +435,5 @@
 	}
 
-	mCurrentNode = interior;
+	mCurrentBspNode = interior;
 }
 
@@ -436,5 +447,6 @@
 
 		mBspTree = new BspTree();
-		mCurrentNode = mBspTree->GetRoot();
+		mBspTree->mBox = mViewSpaceBox;
+		mCurrentBspNode = mBspTree->GetRoot();
 
 		mViewCellsManager = new BspViewCellsManager(mBspTree);
@@ -445,16 +457,17 @@
 
 		mVspBspTree = new VspBspTree();
-		mCurrentNode = mVspBspTree->GetRoot();
+		mCurrentBspNode = mVspBspTree->GetRoot();
 
 		mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
 
+		mVspBspTree->mBox = mViewSpaceBox;
 		Debug << "creating vsp bsp view cells manager" << endl;
 	}
-	/*else if (strcmp(name, "vspKdTree") == 0)
-	{
-		mVspKdTree = new VspKdTree();		
-	
-		mViewCellsManager = VspKdViewCellsManager(mVspKdTree);
-	}*/
+	else if (strcmp(name, "vspKdTree") == 0)
+	{
+		// TODO
+		mVspKdTree = new VspKdTree();	
+		mViewCellsManager = new VspKdViewCellsManager(mVspKdTree);
+	}
 	else
 	{
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 587)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 590)
@@ -686,8 +686,10 @@
 {
 	mViewCells.clear();
+	
 	CollectViewCells();
-
+	
 	mViewCellsStats.Reset();
 	EvaluateViewCellsStats();
+
 	// has to be recomputed
 	mTotalAreaValid = false;
@@ -699,4 +701,5 @@
 	return mMaxPvsSize;
 }
+
 
 void
@@ -921,4 +924,5 @@
 	environment->GetIntValue("BspTree.Construction.samples", mInitialSamples);
 	mBspTree->SetViewCellsManager(this);
+	mBspTree->mViewCellsTree = mViewCellsTree;
 }
 
@@ -1024,8 +1028,10 @@
 	if (!ViewCellsTreeConstructed())
 	{
+		
 		mBspTree->CollectViewCells(mViewCells);
 	}
 	else 
 	{
+		
 		// we can use the view cells tree hierarchy to get the right set
 		mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumMergedViewCells);
@@ -1069,4 +1075,14 @@
 	}
 
+	
+	// view cells already finished before post processing step (i.e. because they were loaded)
+	if (mViewCellsFinished)
+	{
+		FinalizeViewCells(true);
+		EvaluateViewCellsStats();
+
+		return 0;
+	}
+
 	//-- post processing of bsp view cells
     int vcSize = 0;
@@ -1098,4 +1114,5 @@
 				m.mDiffuseColor = RgbColor(0, 1, 0);
 				exporter->SetForcedMaterial(m);
+			
 				exporter->SetWireframe();
 
@@ -1132,6 +1149,7 @@
 	FinalizeViewCells(true);
 	
-	// for output we need unique ids for each view cell
-	CreateUniqueViewCellIds();
+	// HACK: removes view cells in bsp leaves with active ones
+	if (0)
+		AddCurrentViewCellsToHierarchy();
 
 	// write view cells to disc
@@ -1186,4 +1204,5 @@
 			else
 				exporter->SetFilled();
+
 			ExportViewCellsForViz(exporter);
 
@@ -1486,7 +1505,70 @@
 
 
-/**********************************************************************/
-/*                   KdViewCellsManager implementation               */
-/**********************************************************************/
+
+bool BspViewCellsManager::ExportViewCells(const string filename)
+{
+	cout << "exporting view cells to xml ... ";
+	std::ofstream stream;
+
+	// 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 << "<Visibility_Solution>" << endl;
+
+	//-- the view space bounding box
+	stream << "<ViewSpaceBox" 
+		   << " 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
+	//stream << "<Hierarchy name=\"bspTree\" />" << endl;
+	stream << "<Hierarchy name=\"vspBspTree\" />" << endl; // write vsp bsp here because can use same tree and is bug free
+	//-- load the view cells itself, i.e., the ids and the pvs
+	stream << "<ViewCells>" << endl;
+	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+		ExportViewCell(*it, stream);
+
+	stream << "</ViewCells>" << endl;
+
+	//-- load the hierarchy
+	stream << "<Hierarchy>" << endl;
+	mBspTree->Export(stream);
+	stream << endl << "</Hierarchy>" << endl;
+
+	stream << "</Visibility_Solution>" << endl;
+	stream.close();
+
+	cout << "finished" << endl;
+
+	return true;
+}
+
+
+void BspViewCellsManager::AddCurrentViewCellsToHierarchy()
+{
+	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+	{
+		ViewCell *vc = *it;
+		ViewCellContainer leaves;
+		mViewCellsTree->CollectLeaves(vc, leaves);
+		
+		ViewCellContainer::const_iterator lit, lit_end = leaves.end();
+
+		for (lit = leaves.begin(); lit != lit_end; ++ lit)
+		{
+			BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*lit);
+			bspVc->mLeaf->SetViewCell(vc);
+		}
+	}
+}
+
+/************************************************************************/
+/*                   KdViewCellsManager implementation                  */
+/************************************************************************/
 
 
@@ -2177,4 +2259,5 @@
 	environment->GetIntValue("VspBspTree.Construction.samples", mInitialSamples);
 	mVspBspTree->SetViewCellsManager(this);
+	mVspBspTree->mViewCellsTree = mViewCellsTree;
 }
 
@@ -2199,5 +2282,5 @@
 	if (!ViewCellsTreeConstructed())
 	{
-		mVspBspTree->CollectViewCells(mViewCells, true);
+		mVspBspTree->CollectViewCells(mViewCells, false);
 	}
 	else 
@@ -2568,6 +2651,8 @@
 	FinalizeViewCells(true);
 
-	// for output we need unique ids for each view cell
-	CreateUniqueViewCellIds();
+	// HACK: removes view cells in bsp leaves with active ones
+	if (0)
+		AddCurrentViewCellsToHierarchy();
+
 
 	// write view cells to disc
@@ -2966,9 +3051,10 @@
 
 void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
-											  ViewCell *vc) const
+													ViewCell *vc) const
 {
 	if (vc->GetMesh())
 	{
 		exporter->ExportMesh(vc->GetMesh());
+	
 		return;
 	}
@@ -3023,8 +3109,9 @@
 		delete vc->GetMesh();
 
+	
 	BspNodeGeometry geom;
+
+	mVspBspTree->ConstructGeometry(vc, geom);
 	
-	mVspBspTree->ConstructGeometry(vc, geom);
-
 	Mesh *mesh = new Mesh();
 	geom.AddToMesh(*mesh);
@@ -3043,5 +3130,5 @@
 	if (parser.ParseFile(filename, &vm, objects))
 	{
-		vm->PrepareLoadedViewCells();
+		//vm->PrepareLoadedViewCells();
 		vm->ResetViewCells();
 
@@ -3052,6 +3139,4 @@
 
 		Debug << (int)vm->mViewCells.size() << " view cells loaded" << endl;
-
-		//vm->ExportViewCells("test.xml");
 	}
 	else
@@ -3078,5 +3163,6 @@
 
 	// for output we need unique ids for each view cell
-	//CreateUniqueViewCellIds();
+	CreateUniqueViewCellIds();
+
 
 	stream.open(filename.c_str());
@@ -3101,7 +3187,7 @@
 
 	//-- load the hierarchy
-	stream << "<BspTree>" << endl;
+	stream << "<Hierarchy>" << endl;
 	mVspBspTree->Export(stream);
-	stream << endl << "</BspTree>" << endl;
+	stream << endl << "</Hierarchy>" << endl;
 
 	stream << "</Visibility_Solution>" << endl;
@@ -3151,4 +3237,5 @@
 {
 	// TODO: do I still need this here?
+	if (0)
 	mVspBspTree->RepairViewCellsLeafLists();
 }
@@ -3176,4 +3263,11 @@
 
 
+void VspBspViewCellsManager::AddCurrentViewCellsToHierarchy()
+{
+	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+	{
+	}
+}
 
 //////////////////////////////////
