Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 575)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 577)
@@ -120,6 +120,7 @@
 		@return true on success
     */
-    virtual bool LoadViewCells(const string filename, ObjectContainer *objects);
-
+    virtual bool LoadViewCellsGeometry(const string filename);
+
+	
 	/** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
 		@param the base triangle
@@ -314,4 +315,12 @@
 	virtual void FinalizeViewCells(const bool createMesh);
 
+
+	/** Loads view cells from file. The view cells manager is created with respect to the loaded
+		view cells.
+
+		@returns the view cells manager if loading was successful, false otherwise
+	*/
+	static ViewCellsManager *LoadViewCells(const string filename, ObjectContainer *objects);
+
 protected:
 
@@ -366,6 +375,9 @@
 	void ExportViewCell(ViewCell *viewCell, ofstream &stream);
 
-	/// the view cell corresponding to space ouside the valid view space
-	//ViewCell *mOutOfBoundsCell;
+
+	/** 
+		Takes different measures to prepares the view cells after loading them from disc.
+	*/
+	virtual void PrepareLoadedViewCells() {};
 
 	/// Renders the view cells.
@@ -641,5 +653,5 @@
 	void CreateMesh(ViewCell *vc);
 
-	bool LoadViewCells(const string filename, ObjectContainer *objects);
+	//bool LoadViewCellsGeometry(const string filename, ObjectContainer *objects);
 	bool ExportViewCells(const string filename);
 
@@ -671,4 +683,6 @@
 	void ExportColor(Exporter *exporter, ViewCell *vc) const;
 
+	void PrepareLoadedViewCells();
+
 	/// the view space partition BSP tree.
 	VspBspTree *mVspBspTree;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 576)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 577)
@@ -190,9 +190,20 @@
 bool Preprocessor::PrepareViewCells()
 {
-	//-- parse type of view cell container
-	char viewCellsStr[64];
-	environment->GetStringValue("ViewCells.type", viewCellsStr);
-
-	mViewCellsManager = CreateViewCellsManager(viewCellsStr);
+	//-- parse view cells construction method
+	environment->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
+	char buf[100];
+	
+	if (mLoadViewCells)
+	{
+		environment->GetStringValue("ViewCells.filename", buf);
+		mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects);
+	}
+	else
+	{
+		//-- parse type of view cell container
+		char viewCellsStr[64];
+		environment->GetStringValue("ViewCells.type", viewCellsStr);
+	    mViewCellsManager = CreateViewCellsManager(viewCellsStr);
+	}
 
 	float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
@@ -207,13 +218,4 @@
 	mViewCellsManager->SetRenderer(mRenderSimulator);
 
-	//-- parse view cells construction method
-	environment->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
-	char buf[100];
-
-	if (mLoadViewCells)
-	{
-		environment->GetStringValue("ViewCells.filename", buf);
-		mViewCellsFilename = buf;
-	}
 
 	if (mUseGlRenderer || mUseGlDebugger)
@@ -228,6 +230,4 @@
 	environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
 	
-
-
 	return true;
 }
@@ -242,16 +242,14 @@
 	else if (strcmp(name, "bspTree") == 0)
 	{
+		Debug << "view cell type: Bsp" << endl;
+
 		mBspTree = new BspTree();
-
-		Debug << "view cell type: Bsp" << endl;
-
 		mViewCellsManager = new BspViewCellsManager(mBspTree);
 	}
 	else if (strcmp(name, "vspBspTree") == 0)
 	{
+		Debug << "view cell type: VspBsp" << endl;
+
 		mVspBspTree = new VspBspTree();
-
-		Debug << "view cell type: VspBsp" << endl;
-
 		mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
 	}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 576)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 577)
@@ -467,17 +467,10 @@
 
   mViewSpaceBox = NULL;
-  mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
-  
-  
-  //-- load view cells from file if requested
-  if (mLoadViewCells) {	
-	// load now because otherwise bounding box not correct
-	mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects);
-	// $$JB tmp make all viewcelss valid
-	mViewCellsManager->SetValidity(0, 9999999999); 
-
-  }
-  else
-  {
+
+  // if not already loaded, construct view cells from file
+  if (!mLoadViewCells) {	
+	
+	  mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
+
 	  // construct view cells using it's own set of samples
 	  mViewCellsManager->Construct(this);
@@ -487,5 +480,6 @@
 	  mViewCellsManager->PrintStatistics(Debug);
   }
-  
+
+
   int rssPass = 0;
   int rssSamples = 0;
@@ -515,7 +509,9 @@
   
   if (mExportRays) {
+	
 	char filename[64];
 	sprintf(filename, "rss-rays-initial.x3d");
 	ExportRays(filename, mVssRays, mExportNumRays);
+	
   }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 574)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 577)
@@ -417,6 +417,6 @@
 		vm->CastBeam(beam);
 
-		Debug << "found " << beam.mViewCells.size() << " view cells and " 
-			  << beam.mKdNodes.size() << " kd nodes" << endl;
+		Debug << "found " << (int)beam.mViewCells.size() << " view cells and " 
+			  << (int)beam.mKdNodes.size() << " kd nodes" << endl;
 
 		BeamSampleStatistics stats;
@@ -526,20 +526,16 @@
   {
 	mViewSpaceBox = box;
-	mViewCellsManager->SetViewSpaceBox(*box);
   }
   else
   {
 	mViewSpaceBox = NULL;
-	mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
-  }
-  
+  }
+  
+  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
+
   //-- load view cells from file if requested
-  if (mLoadViewCells)
-  {	
-	  // load now because otherwise bounding box not correct
-	  mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects);
-  }
-  else
-  {
+  if (!mLoadViewCells)
+  {
+	  mViewCellsManager->SetViewSpaceBox(vbox);
 	  // construct view cells using it's own set of samples
 	  mViewCellsManager->Construct(this);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParserXerces.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParserXerces.h	(revision 575)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParserXerces.h	(revision 577)
@@ -23,6 +23,5 @@
   //  Constructors and Destructor
   // -----------------------------------------------------------------------
-  ViewCellsParseHandlers(ViewCellsManager **viewCellsManager,
-						 ObjectContainer *objects);
+  ViewCellsParseHandlers(ObjectContainer *objects);
   ~ViewCellsParseHandlers();
   
@@ -72,4 +71,6 @@
   ObjectContainer *mObjects;
 
+  AxisAlignedBox3 mViewSpaceBox;
+
   // Handlers for X3D
   void StartBspLeaf(AttributeList& attributes);
@@ -83,4 +84,5 @@
 
   void startBspElement(string element, AttributeList& attributes);
+  void StartViewSpaceBox(AttributeList&  attributes);
   // -----------------------------------------------------------------------
   //  Handlers for the SAX ErrorHandler interface
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 576)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 577)
@@ -154,6 +154,4 @@
 
   bool mLoadViewCells;
-
-  string mViewCellsFilename;
   
   bool mDetectEmptyViewSpace;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp	(revision 575)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp	(revision 577)
@@ -61,14 +61,14 @@
 //  StdInParseHandlers: Constructors and Destructor
 // ---------------------------------------------------------------------------
-ViewCellsParseHandlers::ViewCellsParseHandlers(ViewCellsManager **viewCells,
-											   ObjectContainer *objects):
+ViewCellsParseHandlers::ViewCellsParseHandlers(ObjectContainer *objects):
   mElementCount(0)
   , mAttrCount(0)
   , mCharacterCount(0)
   , mSpaceCount(0)
+  , mViewCellsManager(NULL)
+  , mVspBspTree(NULL)
+  , mBspTree(NULL)
 {
 	mObjects = objects;
-	viewCells = &mViewCellsManager;
-	//mPreprocessor = preprocessor;
 }
 
@@ -142,4 +142,37 @@
 											 AttributeList& attributes)
 {
+	if (element == "Interior") 
+	{
+		cout << "[";
+		StartBspInterior(attributes);
+	}
+
+	if (element == "Leaf") 
+	{
+		StartBspLeaf(attributes);
+	}
+}
+
+
+void ViewCellsParseHandlers::startElement(const XMLCh* const name,
+										  AttributeList& attributes)
+{
+	StrX lname(name);
+	string element(lname.LocalForm());
+
+	// decides the used view cell hierarchy
+	if (element == "Hierarchy")
+	{
+		cout << "h";
+		StartHierarchy(attributes);
+	}
+	
+	// decides the used view cell hierarchy
+	if (element == "ViewSpaceBox")
+	{
+		cout << "vsb";
+		StartViewSpaceBox(attributes);
+	}
+
 	if (element == "ViewCell")
 	{
@@ -148,42 +181,20 @@
 	}
 
-	if (element == "Interior") 
-	{
-		cout << "[";
-		StartBspInterior(attributes);
-	}
-
-	if (element == "Leaf") 
-	{
-		StartBspLeaf(attributes);
-	}
-}
-
-
-void ViewCellsParseHandlers::startElement(const XMLCh* const name,
-										  AttributeList& attributes)
-{
-	StrX lname(name);
-	string element(lname.LocalForm());
+	// 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 (element == "Hierarchy")
-	{
-		cout << "h";
-		StartHierarchy(attributes);
-	}
-
-	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;
+		}
+	}
+
 	++ mElementCount;
 	mAttrCount += attributes.getLength();
@@ -272,4 +283,34 @@
 
 
+void ViewCellsParseHandlers::StartViewSpaceBox(AttributeList& attributes)
+{
+	int len = attributes.getLength();
+
+	Vector3 bmin, bmax;
+
+	for (int i = 0; i < len; ++ i) 
+	{
+		string attrName(StrX(attributes.getName(i)).LocalForm());
+		StrX attrValue(attributes.getValue(i));
+		const char *ptr = attrValue.LocalForm();
+
+		if (attrName == "min") 
+		{
+			sscanf(ptr, "%f %f %f",
+				   &bmin.x, &bmin.y, &bmin.z);
+		}
+		else if (attrName == "max") 
+		{
+			sscanf(ptr, "%f %f %f",
+				   &bmax.x, &bmax.y, &bmax.z);
+		}
+	}
+
+	mViewSpaceBox = AxisAlignedBox3(bmin, bmax);
+
+	Debug << "view space box: " << mViewSpaceBox << endl;
+}
+
+
 void ViewCellsParseHandlers::StartBspLeaf(AttributeList& attributes)
 {
@@ -284,7 +325,11 @@
 	{
 		if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
+		{
 			mBspTree->mRoot = leaf;
-		else
+		}
+		else if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
+		{
 			mVspBspTree->mRoot = leaf;
+		}
 	}
 
@@ -369,7 +414,11 @@
 	{
 		if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
+		{
 			mBspTree->mRoot = interior;
+		}
 		else
+		{
 			mVspBspTree->mRoot = interior;
+		}
 	}
 
@@ -381,11 +430,9 @@
 void ViewCellsParseHandlers::CreateViewCellsManager(const char *name)
 {
-	
 	if (strcmp(name, "bspTree") == 0)
 	{
+		Debug << "view cell type: Bsp" << endl;
+
 		mBspTree = new BspTree();
-
-		Debug << "view cell type: Bsp" << endl;
-
 		mCurrentNode = mBspTree->GetRoot();
 
@@ -394,10 +441,12 @@
 	else if (strcmp(name, "vspBspTree") == 0)
 	{
+		Debug << "view cell type: VspBsp" << endl;
+
 		mVspBspTree = new VspBspTree();
-
-		Debug << "view cell type: VspBsp" << endl;
 		mCurrentNode = mVspBspTree->GetRoot();
 
 		mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
+
+		Debug << "creating vsp bsp view cells manager" << endl;
 	}
 	/*else if (strcmp(name, "vspKdTree") == 0)
@@ -412,4 +461,6 @@
 		exit(1);
 	}
+
+	mViewCellsManager->SetViewSpaceBox(mViewSpaceBox);
 }
 
@@ -503,5 +554,5 @@
   //  to do.
   //
-  ViewCellsParseHandlers handler(viewCells, objects);
+  ViewCellsParseHandlers handler(objects);
   parser->setDocumentHandler(&handler);
   parser->setErrorHandler(&handler);
@@ -558,4 +609,7 @@
   XMLPlatformUtils::Terminate();
   
+   // assign new view cells manager
+  *viewCells = handler.mViewCellsManager;
+
   if (errorCount > 0)
     return false;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 575)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 577)
@@ -52,4 +52,6 @@
 	environment->GetIntValue("ViewCells.Construction.samplesPerPass", mSamplesPerPass);
 
+	environment->GetBoolValue("ViewCells.exportToFile", mExportViewCells);
+
 	mMinPvsSize = emptyViewCells ? 1 : 0;
 
@@ -236,4 +238,5 @@
 }
 
+
 void
 ViewCellsManager::SetValidity(
@@ -275,5 +278,6 @@
 }
 
-bool ViewCellsManager::LoadViewCells(const string filename, ObjectContainer *objects)
+
+bool ViewCellsManager::LoadViewCellsGeometry(const string filename)
 {
 	X3dParser parser;
@@ -575,4 +579,5 @@
 {
 	mViewSpaceBox = box;
+	mTotalAreaValid = false;
 }
 
@@ -2176,4 +2181,5 @@
 		FinalizeViewCells(true);
 		EvaluateViewCellsStats();
+
 		return 0;
 	}
@@ -2279,4 +2285,7 @@
 	FinalizeViewCells(true);
 
+	// for output we need unique ids for each view cell
+	CreateUniqueViewCellIds();
+
 	// write view cells to disc
 	if (mExportViewCells)
@@ -2368,4 +2377,60 @@
 			delete exporter;
 		}
+	}
+
+	if (1)
+	{
+		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->mBspStats.maxDepth;
+
+			ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
+
+			for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
+			{
+				BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit);
+
+				vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end();
+
+				for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit)
+				{
+					BspLeaf *leaf = *lit;
+
+					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.mPolys);
+				}
+			}
+
+			delete exporter;
+		}
+
+
+		cout << "finished" << endl;
 	}
 
@@ -2661,4 +2726,7 @@
 void VspBspViewCellsManager::CreateMesh(ViewCell *vc)
 {
+	if (vc->GetMesh())
+		delete vc->GetMesh();
+
 	BspNodeGeometry geom;
 	BspViewCell *bspVc = dynamic_cast<BspViewCell *>(vc);
@@ -2672,24 +2740,33 @@
 
 
-bool VspBspViewCellsManager::LoadViewCells(const string filename, ObjectContainer *objects)
+ViewCellsManager *ViewCellsManager::LoadViewCells(const string filename, 
+												  ObjectContainer *objects)
 {
 	ViewCellsParser parser;
 
-	DEL_PTR(mVspBspTree);
-	mVspBspTree = new VspBspTree();
-
-	bool success ;//= parser.ParseFile(filename, &this, objects);
-	mVspBspTree->RepairViewCellsLeafLists();
-	mVspBspTree->mBox = GetViewSpaceBox();
-	ResetViewCells();
-
-	mViewCellsFinished = true;
-	mMaxPvsSize = (int)objects->size();
-
-	FinalizeViewCells(true);
-
-	Debug << (int)mViewCells.size() << " view cells loaded" << endl;
-
-	return success;
+	ViewCellsManager *vm = NULL;
+
+	if (parser.ParseFile(filename, &vm, objects))
+	{
+		vm->PrepareLoadedViewCells();
+		vm->ResetViewCells();
+
+		vm->mViewCellsFinished = true;
+		vm->mMaxPvsSize = (int)objects->size();
+
+		vm->FinalizeViewCells(true);
+
+		Debug << (int)vm->mViewCells.size() << " view cells loaded" << endl;
+
+		//vm->ExportViewCells("test.xml");
+	}
+	else
+	{
+		Debug << "failed loading view cells" << endl;
+		DEL_PTR(vm);
+	}
+
+
+	return vm;
 }
 
@@ -2706,10 +2783,18 @@
 	std::ofstream stream;
 
-	// for output we need unique ids
-	CreateUniqueViewCellIds();
+	// 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=\"vspBspTree\" />" << endl;
 
 	//-- load the view cells itself, i.e., the ids and the pvs
@@ -2766,5 +2851,12 @@
 
 
-
+void VspBspViewCellsManager::PrepareLoadedViewCells()
+{
+	// TODO: do I still need this here?
+	mVspBspTree->RepairViewCellsLeafLists();
+}
+
+
+//////////////////////////////////77
 ViewCellsManager *ViewCellsManagerFactory::Create(const string mName)
 {
