Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1193)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1201)
@@ -4002,9 +4002,6 @@
 }
 
-#if ZIPPED_VIEWCELLS
-bool VspBspTree::Export(ogzstream &stream)
-#else
-bool VspBspTree::Export(ofstream &stream)
-#endif
+
+bool VspBspTree::Export(OUT_STREAM &stream)
 {
 	ExportNode(mRoot, stream);
@@ -4013,9 +4010,6 @@
 }
 
-#if ZIPPED_VIEWCELLS
-void VspBspTree::ExportNode(BspNode *node, ogzstream &stream)
-#else
-void VspBspTree::ExportNode(BspNode *node, ofstream &stream)
-#endif
+
+void VspBspTree::ExportNode(BspNode *node, OUT_STREAM &stream)
 {
 	if (node->IsLeaf())
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1184)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1201)
@@ -1011,4 +1011,10 @@
 											const bool storeViewCells);
 
+	ViewCellsManager *LoadViewCells(const string &filename, 
+		ObjectContainer *objects,
+		const bool finalizeViewCells,
+		BoundingBoxConverter *bconverter);
+
+
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1199)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1201)
@@ -323,5 +323,9 @@
   mSceneGraph->CollectObjects(&root->mObjects);
   
+  long startTime = GetTime();
+
+  cout << "building kd tree ... " << endl;
   mKdTree->Construct();
+  cout << "construction finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs " << endl;
   return true;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1196)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1201)
@@ -2702,9 +2702,6 @@
 }
 
-#if ZIPPED_VIEWCELLS
-bool VspTree::Export(ogzstream &stream)
-#else
-bool VspTree::Export(ofstream &stream)
-#endif
+
+bool VspTree::Export(OUT_STREAM &stream)
 {
 	ExportNode(mRoot, stream);
@@ -2714,9 +2711,5 @@
 
 
-#if ZIPPED_VIEWCELLS
-void VspTree::ExportNode(VspNode *node, ogzstream &stream)
-#else
-void VspTree::ExportNode(VspNode *node, ofstream &stream)
-#endif
+void VspTree::ExportNode(VspNode *node, OUT_STREAM &stream)
 {
 	if (node->IsLeaf())
@@ -5416,4 +5409,107 @@
 
 
+bool OspTree::Export(OUT_STREAM &stream)
+{
+	ExportNode(mRoot, stream);
+
+	return true;
+}
+
+
+void OspTree::ExportNode(KdNode *node, OUT_STREAM &stream)
+{
+	if (node->IsLeaf())
+	{
+		KdLeaf *leaf = dynamic_cast<KdLeaf *>(node);
+
+		stream << "<Leaf ";
+		stream << "objects=\"";
+		
+		//-- export objects in kd leaves
+		//if (mExportObjects) ExportObjects(leaf, stream);
+		
+		stream << "\" />" << endl;
+		//stream << " </Leaf>" << endl;
+	}
+	else
+	{	
+		KdInterior *interior = dynamic_cast<KdInterior *>(node);
+	
+		stream << "<Interior plane=\"" << interior->mPosition << " " 
+			   << interior->mAxis << "\">" << endl;
+
+		ExportNode(interior->mBack, stream);
+		ExportNode(interior->mFront, stream);
+
+		stream << "</Interior>" << endl;
+	}
+}
+
+
+struct KdObjectsTraversalData
+{
+	KdNode *node;
+	ObjectContainer *objects;
+};
+
+
+void OspTree::InsertObjects(KdNode *node, const ObjectContainer &objects)
+{
+	stack<KdObjectsTraversalData> tStack;
+
+	while (!tStack.empty())
+	{
+		KdObjectsTraversalData tData = tStack.top();
+        tStack.pop();
+
+		KdNode *node = tData.node;
+		
+		if (node->IsLeaf())
+		{
+			KdLeaf *leaf = dynamic_cast<KdLeaf *>(node);
+
+			ObjectContainer::const_iterator oit, oit_end = tData.objects->end();
+
+			for (oit = tData.objects->begin(); oit != oit_end; ++ oit)
+			{
+				leaf->mObjects.push_back(*oit);
+			}
+		}
+		else // interior
+		{
+			KdObjectsTraversalData frontData, backData;
+			KdInterior *interior = dynamic_cast<KdInterior *>(node);
+
+			frontData.objects = new ObjectContainer();
+			backData.objects = new ObjectContainer();
+
+			ObjectContainer::const_iterator oit, oit_end = tData.objects->end();
+			
+		    for (oit = tData.objects->begin(); oit != oit_end; ++ oit) 
+			{
+				Intersectable *object = *oit;
+		
+				// determine the side of this ray with respect to the plane
+				const AxisAlignedBox3 box = object->GetBox();
+
+				if (box.Max(interior->mAxis) >= interior->mPosition)
+				{
+					frontData.objects->push_back(object);
+				}
+
+				if (box.Min(interior->mAxis) < interior->mPosition)
+				{
+					backData.objects->push_back(object);
+				}
+			}
+
+			tStack.push(backData);
+			tStack.push(frontData);
+		}
+
+		DEL_PTR(tData.objects);
+	}
+}
+
 
 /*******************************************************************/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1199)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1201)
@@ -716,9 +716,5 @@
 	cout << "exporting view cells to xml ... ";
 	
-#if ZIPPED_VIEWCELLS
-	ogzstream stream(filename.c_str());
-#else
-	std::ofstream stream(filename.c_str());
-#endif
+	OUT_STREAM stream(filename.c_str());
 
 	// for output we need unique ids for each view cell
@@ -4897,4 +4893,7 @@
 	Debug << mOspTree->GetStatistics() << endl;
 
+	OUT_STREAM stream("osptree.xml.zip");
+	mOspTree->Export(stream);
+
 	// print view cell statistics
 	ResetViewCells();
@@ -5456,9 +5455,5 @@
 	cout << "exporting view cells to xml ... ";
 	
-#if ZIPPED_VIEWCELLS
-	ogzstream stream(filename.c_str());
-#else
-	std::ofstream stream(filename.c_str());
-#endif
+	OUT_STREAM stream(filename.c_str());
 
 	// for output we need unique ids for each view cell
@@ -5468,5 +5463,6 @@
 	stream << "<VisibilitySolution>" << endl;
 
-	//-- the view space bounding box
+	//-- export view space bounding box
+	
 	stream << "<ViewSpaceBox" 
 		   << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
@@ -5475,4 +5471,5 @@
 	
 	//-- export bounding boxes
+
 	stream << "<BoundingBoxes>" << endl;
 
@@ -5494,26 +5491,32 @@
 
 	//-- export the view cells and the pvs
-	stream << "<HierarchyType name=\"vspTree\" />" << endl;
-
+
+	stream << "<Hierarchy name=\"vspTree\" />" << endl;
 	const int numViewCells = mCurrentViewCellsStats.viewCells;
 
 	stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
-	
 	mViewCellsTree->Export(stream, exportPvs);
-
 	stream << "</ViewCells>" << endl;
 
 
-	//-- export the spatial hierarchy
+	//-- export the view space hierarchy
+	
+	// the type of the view space partition
+	stream << "<ViewSpacePartition>" << endl;
+	mVspTree->Export(stream);
+	stream << endl << "</ViewSpacePartitioin>" << endl;
+
+	
+	//-- export the object space partition
 	
 	// the type of the view cells hierarchy
-	stream << "<Hierarchy>" << endl;
-	mVspTree->Export(stream);
-	stream << endl << "</Hierarchy>" << endl;
+	stream << "<ObjectSpacePartition>" << endl;
+	mOspTree->Export(stream);
+	stream << endl << "</ObjectSpacePartition>" << endl;
+
 
 	stream << "</VisibilitySolution>" << endl;
-
-
 	stream.close();
+	
 	cout << "finished" << endl;
 
@@ -5728,4 +5731,20 @@
 }
 
+
+ViewCellsManager *VspOspViewCellsManager::LoadViewCells(const string &filename, 
+														ObjectContainer *objects,
+														const bool finalizeViewCells,
+														BoundingBoxConverter *bconverter)
+												 
+{
+	ViewCellsManager *vm = 
+		ViewCellsManager::LoadViewCells(filename, objects, finalizeViewCells, bconverter);
+
+	mOspTree->InsertObjects(mOspTree->GetRoot(), *objects);
+
+	return vm;
+}
+
+
 #if TEST_EVALUATION
 void VspOspViewCellsManager::EvalViewCellPartition()
Index: GTP/trunk/Lib/Vis/Preprocessing/src/common.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 1161)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 1201)
@@ -75,4 +75,15 @@
 #define strncasecmp strnicmp
 #endif // __WATCOMC__
+
+// matt
+
+#define USE_GZLIB 1
+
+#if USE_GZLIB
+
+#define OUT_BIN_MODE ios::out
+#define IN_BIN_MODE ios::in
+
+#else
 
 #ifdef sgi
@@ -89,4 +100,5 @@
 #endif // sgi
 
+#endif
 
 //  #ifndef HAS_BOOL
@@ -483,17 +495,23 @@
 }
 
-// if the view cells should be exported and inported as gzstream
-#define ZIPPED_VIEWCELLS 1
-
-#endif
-
-
-
-
-
-
-
-
-
-
-
+
+#if USE_GZLIB
+	// type of out put and input streams
+	#define OUT_STREAM ogzstream
+	#define IN_STREAM igzstream
+#else
+	#define OUT_STREAM ofstream
+	#define IN_STREAM ifstream
+#endif
+
+#endif
+
+
+
+
+
+
+
+
+
+
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1145)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1201)
@@ -321,9 +321,5 @@
 	/** Writes tree to output stream
 	*/
-#if ZIPPED_VIEWCELLS
-	bool Export(ogzstream &stream);
-#else
-	bool Export(ofstream &stream);
-#endif
+	bool Export(OUT_STREAM &stream);
 
 	/** Casts beam, i.e. a 5D frustum of rays, into tree.
@@ -697,9 +693,5 @@
 		@note: should be implemented as visitor.
 	*/
-#if ZIPPED_VIEWCELLS
-	void ExportNode(BspNode *node, ogzstream &stream);
-#else
-	void ExportNode(BspNode *node, ofstream &stream);
-#endif
+	void ExportNode(BspNode *node, OUT_STREAM &stream);
 
 	/** Returns estimated memory usage of tree.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1199)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1201)
@@ -2182,9 +2182,5 @@
 
 
-#if ZIPPED_VIEWCELLS
-bool ViewCellsTree::Export(ogzstream &stream, const bool exportPvs)
-#else
-bool ViewCellsTree::Export(ofstream &stream, const bool exportPvs)
-#endif
+bool ViewCellsTree::Export(OUT_STREAM &stream, const bool exportPvs)
 {
 	// export recursivly all view cells from the root
@@ -2223,9 +2219,6 @@
 }
 
-#if ZIPPED_VIEWCELLS
-void ViewCellsTree::ExportPvs(ViewCell *viewCell, ogzstream &stream)
-#else
-void ViewCellsTree::ExportPvs(ViewCell *viewCell, ofstream &stream)
-#endif
+
+void ViewCellsTree::ExportPvs(ViewCell *viewCell, OUT_STREAM &stream)
 {
 	ObjectPvsMap::iterator it, it_end = viewCell->GetPvs().mEntries.end();
@@ -2237,9 +2230,8 @@
 }
 
-#if ZIPPED_VIEWCELLS
-void ViewCellsTree::ExportViewCell(ViewCell *viewCell, ogzstream &stream, const bool exportPvs)
-#else
-void ViewCellsTree::ExportViewCell(ViewCell *viewCell, ofstream &stream, const bool exportPvs)
-#endif
+
+void ViewCellsTree::ExportViewCell(ViewCell *viewCell, 
+								   OUT_STREAM &stream, 
+								   const bool exportPvs)
 {
 	if (viewCell->IsLeaf())
@@ -2258,5 +2250,4 @@
 
 		stream << "\" />" << endl;
-		//stream << " </Leaf>" << endl;
 	}
 	else
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1189)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1201)
@@ -733,9 +733,5 @@
 	/** Writes tree to output stream
 	*/
-#if ZIPPED_VIEWCELLS
-	bool Export(ogzstream &stream);
-#else
-	bool Export(ofstream &stream);
-#endif
+	bool Export(OUT_STREAM &stream);
 
 	/** Casts beam, i.e. a 5D frustum of rays, into tree.
@@ -1033,9 +1029,5 @@
 		@note: should be implemented as visitor.
 	*/
-#if ZIPPED_VIEWCELLS
-	void ExportNode(VspNode *node, ogzstream &stream);
-#else
-	void ExportNode(VspNode *node, ofstream &stream);
-#endif
+	void ExportNode(VspNode *node, OUT_STREAM &stream);
 
 	/** Returns estimated memory usage of tree.
@@ -1335,5 +1327,4 @@
 	void CollectLeaves(vector<KdLeaf *> &leaves) const;
 
-
 	/** Returns bounding box of the whole tree (= bbox of root node)
 	*/
@@ -1390,9 +1381,5 @@
 	/** Writes tree to output stream
 	*/
-#if ZIPPED_VIEWCELLS
-	bool Export(ogzstream &stream);
-#else
-	bool Export(ofstream &stream);
-#endif
+	bool Export(OUT_STREAM &stream);
 
 	/** Returns or creates a new intersectable for use in a kd based pvs.
@@ -1423,4 +1410,9 @@
 	float EvalRenderCost(const VssRayContainer &myrays);
 	float EvalLeafCost(const OspTraversalData &tData);
+
+	/** Adds this objects to the kd leaf objects.
+		@warning: Can corrupt the tree
+	*/
+	void InsertObjects(KdNode *node, const ObjectContainer &objects);
 
 protected:
@@ -1621,9 +1613,5 @@
 		@note: should be implemented as visitor.
 	*/
-#if ZIPPED_VIEWCELLS
-	void ExportNode(KdNode *node, ogzstream &stream);
-#else
-	void ExportNode(KdNode *node, ofstream &stream);
-#endif
+	void ExportNode(KdNode *node, OUT_STREAM &stream);
 
 	/** Returns estimated memory usage of tree.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/gzstream.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/gzstream.h	(revision 372)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/gzstream.h	(revision 1201)
@@ -99,4 +99,5 @@
   void close();
   gzstreambuf* rdbuf() { return &buf; }
+  int is_open() { return buf.is_open(); }
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1197)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1201)
@@ -1135,6 +1135,5 @@
 
 
-
-void KdTree::ExportBinLeaf(ofstream &stream, KdLeaf *leaf)
+void KdTree::ExportBinLeaf(OUT_STREAM &stream, KdLeaf *leaf)
 {
 	ObjectContainer::const_iterator it, it_end = leaf->mObjects.end();
@@ -1157,5 +1156,5 @@
 
 
-KdLeaf *KdTree::ImportBinLeaf(ifstream &stream, 
+KdLeaf *KdTree::ImportBinLeaf(IN_STREAM &stream, 
 							  KdInterior *parent,
 							  const ObjectContainer &objects)
@@ -1194,5 +1193,5 @@
 
 
-void KdTree::ExportBinInterior(ofstream &stream, KdInterior *interior)
+void KdTree::ExportBinInterior(OUT_STREAM &stream, KdInterior *interior)
 {
 	int interiorid = TYPE_INTERIOR;
@@ -1207,5 +1206,5 @@
 
 
-KdInterior *KdTree::ImportBinInterior(ifstream &stream, KdInterior *parent)
+KdInterior *KdTree::ImportBinInterior(IN_STREAM  &stream, KdInterior *parent)
 {
 	KdInterior *interior = new KdInterior(parent);
@@ -1226,8 +1225,7 @@
 bool KdTree::ExportBinTree(const string &filename)
 {
-	ofstream stream(filename.c_str(), ios::binary);
+	OUT_STREAM stream(filename.c_str(), OUT_BIN_MODE);
 	
-	if (!stream.is_open())
-		return false;
+	//if (!stream.is_open()) return false;
 
 	// export binary version of mesh
@@ -1261,5 +1259,5 @@
 
 
-KdNode *KdTree::LoadNextNode(ifstream &stream, 
+KdNode *KdTree::LoadNextNode(IN_STREAM &stream, 
 							 KdInterior *parent,
 							 const ObjectContainer &objects)
@@ -1288,8 +1286,7 @@
 	// export binary version of mesh
 	queue<TraversalData> tStack;
-	ifstream stream(filename.c_str(), ios::binary);
-
-	if (!stream.is_open())
-		return false;
+	IN_STREAM stream(filename.c_str(), IN_BIN_MODE);
+
+	//if (!stream.is_open()) return false;
 
 	std::stable_sort(objects.begin(), objects.end(), ilt);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1168)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1201)
@@ -487,9 +487,5 @@
 	/** Exports view cells to file.
 	*/
-#if ZIPPED_VIEWCELLS
-	bool Export(ogzstream &stream, const bool exportPvs = false);
-#else
-	bool Export(ofstream &stream, const bool exportPvs = false);
-#endif
+	bool Export(OUT_STREAM &stream, const bool exportPvs = false);
 
 	/** Export statistics of this view cell tree.
@@ -603,17 +599,9 @@
 		NOTE: should be in exporter!!
 	*/
-#if ZIPPED_VIEWCELLS
-	void ExportViewCell(ViewCell *viewCell, ogzstream &stream, const bool exportPvs);
-#else
-	void ExportViewCell(ViewCell *viewCell, ofstream &stream, const bool exportPvs);	
-#endif
+	void ExportViewCell(ViewCell *viewCell, OUT_STREAM &stream, const bool exportPvs);	
 
 	/** Exports pvs of a view cell.
 	*/
-#if ZIPPED_VIEWCELLS
-	void ExportPvs(ViewCell *viewCell, ogzstream &stream);
-#else
-	void ExportPvs(ViewCell *viewCell, ofstream &stream);
-#endif
+	void ExportPvs(ViewCell *viewCell, OUT_STREAM &stream);
 
 	int GetEntriesInPvsForCompressedStorage(ViewCell *vc) const;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1197)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1201)
@@ -557,9 +557,9 @@
 	void ProcessMultipleRefs(KdLeaf *leaf) const;
 
-	void ExportBinLeaf(ofstream &stream, KdLeaf *leaf);
-	void ExportBinInterior(ofstream &stream, KdInterior *interior);
-	KdLeaf *ImportBinLeaf(ifstream &stream, KdInterior *parent, const ObjectContainer &objects);
-	KdInterior *ImportBinInterior(ifstream &stream, KdInterior *parent);
-	KdNode *LoadNextNode(ifstream &stream, KdInterior *parent, const ObjectContainer &objects);
+	void ExportBinLeaf(OUT_STREAM  &stream, KdLeaf *leaf);
+	void ExportBinInterior(OUT_STREAM &stream, KdInterior *interior);
+	KdLeaf *ImportBinLeaf(IN_STREAM &stream, KdInterior *parent, const ObjectContainer &objects);
+	KdInterior *ImportBinInterior(IN_STREAM  &stream, KdInterior *parent);
+	KdNode *LoadNextNode(IN_STREAM  &stream, KdInterior *parent, const ObjectContainer &objects);
 
   int mTermMaxNodes;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1199)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1201)
@@ -164,12 +164,17 @@
 	preprocessor->BuildKdTree();
 	preprocessor->KdTreeStatistics(cout);
-	preprocessor->mKdTree->ExportBinTree("kd.bin");
+	preprocessor->mKdTree->ExportBinTree("kd.bin.gz");
 	
 	MeshManager::GetSingleton()->ExportEntries("meshes.bin");
 
 	KdTree *kdTree2 = new KdTree;
-	kdTree2->LoadBinTree("kd.bin", preprocessor->mObjects);
-  	Exporter *exporter = Exporter::GetExporter("testkd.x3d");
-	
+
+	cout << "loading kd tree ... ";
+	long startTime = GetTime();
+	kdTree2->LoadBinTree("kd.bin.gz", preprocessor->mObjects);
+  		
+	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
+
+	Exporter *exporter = Exporter::GetExporter("testkd.x3d");
 	MeshManager::GetSingleton()->ExportEntries("meshes.bin");
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 1022)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 1201)
@@ -8,5 +8,5 @@
 #include "Mesh.h"
 #include "BoundingBoxConverter.h"
-//#include "gzstream.h"
+
 
 namespace GtpVisibilityPreprocessor {
@@ -23,5 +23,5 @@
 class VspTree;
 class OspTree;
-
+class VspNode;
 
 
@@ -80,5 +80,7 @@
 
   BspNode *mCurrentBspNode;
+  VspNode *mCurrentVspNode;
   ViewCell *mCurrentViewCell;
+  KdNode *mCurrentKdNode;
 
   ViewCellContainer mViewCells;
@@ -86,10 +88,11 @@
   ObjectContainer *mObjects;
   BoundingBoxConverter *mBoundingBoxConverter;
-
   AxisAlignedBox3 mViewSpaceBox;
-
   IndexedBoundingBoxContainer mIBoundingBoxes;
  
-  bool mParseViewCells;
+  /// current task for the parser
+  int mCurrentTask;
+
+  enum {PARSE_OPTIONS, PARSE_VIEWCELLS, PARSE_VSP, PARSE_OSP};
 
   // Handlers for X3D
@@ -97,5 +100,9 @@
   void StartBspInterior(AttributeList& attributes);
   void EndBspInterior();
- 
+
+  void StartVspLeaf(AttributeList& attributes);
+  void StartVspInterior(AttributeList& attributes);
+  void EndVspInterior();
+
   void StartViewCell(ViewCell *viewCell, AttributeList&  attributes);
   void EndViewCells();
@@ -104,5 +111,7 @@
   void StartHierarchy(AttributeList& attributes);
 
-  void startBspElement(string element, AttributeList& attributes);
+  void StartBspElement(string element, AttributeList& attributes);
+  void StartVspElement(string element, AttributeList& attributes);
+
   void StartViewSpaceBox(AttributeList&  attributes);
 
@@ -111,4 +120,13 @@
   void StartViewCellInterior(AttributeList& attributes);
   void EndViewCellInterior();
+
+  void StartViewSpacePartitionElement(const std::string &element, AttributeList& attributes);
+  void StartObjectSpacePartitionElement(const std::string &element, AttributeList& attributes);
+  void StartViewCellHierarchyElement(const std::string &element, AttributeList& attributes);
+
+  void StartOspElement(string element, AttributeList& attributes);
+
+  void StartOspLeaf(AttributeList& attributes);
+  void StartOspInterior(AttributeList& attributes);
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1197)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1201)
@@ -31,4 +31,6 @@
 #include "GzFileInputSource.h"
 #include "VspOspTree.h"
+#include "KdTree.h"
+
 
 
@@ -81,7 +83,9 @@
   , mBspTree(NULL)
   , mViewCellsTree(NULL)
-  , mParseViewCells(true)
+  , mCurrentTask(PARSE_OPTIONS)
   , mCurrentViewCell(NULL)
   , mCurrentBspNode(NULL)
+  , mCurrentVspNode(NULL)
+  , mCurrentKdNode(NULL)
   , mObjects(objects)
   , mBoundingBoxConverter(bconverter)
@@ -113,5 +117,5 @@
 
   // inside the view cell description
-  if (mParseViewCells)
+  if (mCurrentTask == PARSE_VIEWCELLS)
   {
 	  if (element == "Interior")
@@ -136,4 +140,13 @@
 
 
+void ViewCellsParseHandlers::EndVspInterior()
+{
+	// go one up in the tree
+	if (mCurrentVspNode->GetParent())
+	{	Debug << "]";
+		mCurrentVspNode = mCurrentVspNode->GetParent();
+	}
+}
+
 void ViewCellsParseHandlers::EndViewCellInterior()
 {
@@ -156,5 +169,7 @@
 	// sort view cells to help associating view cells according to their id
 	stable_sort(mViewCells.begin(), mViewCells.end(), vlt);
-	mParseViewCells = false;
+
+	// not parsing view cells anymore
+	mCurrentTask = PARSE_OPTIONS;
 }
 
@@ -195,5 +210,5 @@
 
 
-void ViewCellsParseHandlers::startBspElement(string element,
+void ViewCellsParseHandlers::StartBspElement(string element,
 											 AttributeList& attributes)
 {
@@ -212,4 +227,97 @@
 
 
+void ViewCellsParseHandlers::StartVspElement(string element,
+											 AttributeList& attributes)
+{
+	if (element == "Interior") 
+	{
+		Debug << "[";
+		StartVspInterior(attributes);
+	}
+
+	if (element == "Leaf") 
+	{
+		Debug << "l";
+		StartVspLeaf(attributes);
+	}
+}
+
+
+void ViewCellsParseHandlers::StartOspElement(string element,
+											 AttributeList& attributes)
+{
+	if (element == "Interior") 
+	{
+		Debug << "[";
+		StartOspInterior(attributes);
+	}
+
+	if (element == "Leaf") 
+	{
+		Debug << "l";
+		StartOspLeaf(attributes);
+	}
+}
+
+void ViewCellsParseHandlers::StartViewSpacePartitionElement(const std::string &element,
+															AttributeList& attributes)
+{
+	if (!mViewCellsManager)
+		return;
+
+	//-- use cell type according to the chosen view cell manager
+	switch (mViewCellsManager->GetType())
+	{
+		case ViewCellsManager::BSP:
+		case ViewCellsManager::VSP_BSP:
+			StartBspElement(element, attributes);
+			break;
+		case ViewCellsManager::VSP_OSP:
+			StartVspElement(element, attributes);
+			break;
+		default:
+			Debug << "not implemented" << endl;
+			break;
+	}
+}
+
+
+void ViewCellsParseHandlers::StartObjectSpacePartitionElement(const std::string &element,
+															  AttributeList& attributes)
+{
+	if (!mViewCellsManager)
+		return;
+
+	//-- use cell type according to the chosen view cell manager
+	switch (mViewCellsManager->GetType())
+	{
+		case ViewCellsManager::VSP_OSP:
+			StartOspElement(element, attributes);
+			break;
+		default:
+			Debug << "not implemented" << endl;
+			break;
+	}
+}
+
+
+void ViewCellsParseHandlers::StartViewCellHierarchyElement(const std::string &element,
+														   AttributeList& attributes)
+{
+	// interiors + leaves interpreted view cells else
+	if (element == "Interior") 
+	{
+		Debug << "[";
+		StartViewCellInterior(attributes);
+	}
+
+	if (element == "Leaf") 
+	{
+		Debug << "l";
+		StartViewCellLeaf(attributes);
+	}
+}
+
+
 void ViewCellsParseHandlers::startElement(const XMLCh* const name,
 										  AttributeList& attributes)
@@ -218,18 +326,31 @@
 	string element(lname.LocalForm());
 
-
 	if (element == "ViewCells")
 	{
-		Debug << "parsing view cells" << endl;
-		mParseViewCells = true;
+		cout << "parsing view cells" << endl;
+		mCurrentTask = PARSE_VIEWCELLS;
+	}
+
+	// decides about the view cells manager type
+	if (element == "Hierarchy")
+	{
+		cout << "parsing view cells manager type" << endl;
+		StartHierarchy(attributes);
 	}
 
 	// decides about the view cell hierarchy
-	if (element == "HierarchyType")
-	//if (element == "Hierarchy")
-	{
-		//Debug << "parsing spatial hierarchy" << endl;
-		//mParseViewCells = false;
-		StartHierarchy(attributes);
+	if (element == "ViewSpacePartition")
+	{
+		//StartViewSpacePartition(attributes);
+		cout << "parsing view space partition" << endl;
+		mCurrentTask = PARSE_VSP;
+	}
+
+	// decides about the view cell hierarchy
+	if (element == "ObjectSpacePartition")
+	{
+		//StartObjectSpacePartition(attributes);
+		cout << "parsing object space partition" << endl;
+		mCurrentTask = PARSE_OSP;
 	}
 	
@@ -248,39 +369,20 @@
 	}
 
-	
-	if (!mParseViewCells)
-	{
-		//-- 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;
-	
-			default:
-				Debug << "not implemented" << endl;
-				break;
-			}
-		}
-	}
-	else
-	{
-		// interiors + leaves interpreted view cells else
-		if (element == "Interior") 
-		{
-			Debug << "[";
-			StartViewCellInterior(attributes);
-		}
-
-		if (element == "Leaf") 
-		{
-			Debug << "l";
-			StartViewCellLeaf(attributes);
-		}
-	}
-
+	// parse view space partition
+	switch (mCurrentTask)
+	{
+	case PARSE_VSP:
+		StartViewSpacePartitionElement(element, attributes);
+		break;
+	case PARSE_OSP:
+		StartObjectSpacePartitionElement(element, attributes);
+		break;
+	case PARSE_VIEWCELLS:
+		StartViewCellHierarchyElement(element, attributes);
+		break;
+	default:
+		break;
+	}
+	
 	++ mElementCount;
 	mAttrCount += attributes.getLength();
@@ -673,7 +775,165 @@
 
 
+void ViewCellsParseHandlers::StartVspLeaf(AttributeList& attributes)
+{
+	VspLeaf * leaf = 
+		new VspLeaf(dynamic_cast<VspInterior *>(mCurrentVspNode), NULL);
+
+	if (mCurrentVspNode) // replace front or (if not NULL) back child
+	{
+		dynamic_cast<VspInterior *>(mCurrentVspNode)->ReplaceChildLink(NULL, leaf);
+	}
+	else
+	{
+		mVspTree->mRoot = leaf;
+	}
+
+	//-- find associated view cell
+	int viewCellId;
+	
+	int len = attributes.getLength();
+	  
+	for (int i = 0; i < len; ++ i) 
+	{
+		string attrName(StrX(attributes.getName(i)).LocalForm());
+		StrX attrValue(attributes.getValue(i));
+
+		const char *ptr = attrValue.LocalForm();
+		char *endptr = NULL;
+
+		if (attrName == "viewCellId") 
+		{
+			viewCellId = strtol(ptr, &endptr, 10);
+		}
+	}
+	
+	if (viewCellId >= 0) // valid view cell
+	{
+		// TODO: get view cell with specified id
+		ViewCellInterior dummyVc;
+		dummyVc.SetId(viewCellId);
+
+		ViewCellContainer::iterator vit =
+			lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
+			
+		VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit);
+	
+		if (viewCell->GetId() == viewCellId)
+		{
+			leaf->SetViewCell(viewCell);
+			viewCell->mLeaf = leaf;
+		}
+		else
+		{
+			Debug << "error: view cell does not exist" << endl;
+		}
+	}
+	else
+	{
+		// add to invalid view space
+		leaf->SetViewCell(mVspTree->GetOrCreateOutOfBoundsCell());
+		leaf->SetTreeValid(false);
+		mVspTree->PropagateUpValidity(leaf);
+	}
+}
+
+
+void ViewCellsParseHandlers::StartVspInterior(AttributeList& attributes)
+{
+	AxisAlignedPlane plane;
+	int len = attributes.getLength();
+
+	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 == "plane") 
+		{
+			sscanf(ptr, "%d %f",
+				   &plane.mAxis, &plane.mPosition);
+		}
+	}
+
+	VspInterior* interior = new VspInterior(plane);
+	
+	if (mCurrentVspNode) // replace NULL child of parent with current node
+	{
+		VspInterior *current = dynamic_cast<VspInterior *>(mCurrentVspNode);
+
+		current->ReplaceChildLink(NULL, interior);
+		interior->SetParent(current);
+	}
+	else
+	{
+		mVspTree->mRoot = interior;
+	}
+
+	mCurrentVspNode = interior;
+}
+
+
+void ViewCellsParseHandlers::StartOspInterior(AttributeList& attributes)
+{
+	AxisAlignedPlane plane;
+	int len = attributes.getLength();
+
+	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 == "plane") 
+		{
+			sscanf(ptr, "%d %f",
+				   &plane.mAxis, &plane.mPosition);
+		}
+	}
+
+	KdInterior* interior = new KdInterior(NULL);
+	
+	interior->mAxis = plane.mAxis;
+	interior->mPosition = plane.mPosition;
+
+	if (mCurrentKdNode) // replace NULL child of parent with current node
+	{
+		KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode);
+		parent->ReplaceChildLink(NULL, interior);
+		interior->mParent = parent;
+	}
+	else
+	{
+		mOspTree->mRoot = interior;
+	}
+
+	mCurrentKdNode = interior;
+}
+
+
+void ViewCellsParseHandlers::StartOspLeaf(AttributeList& attributes)
+{
+	KdLeaf * leaf = 
+		new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL);
+
+	if (mCurrentKdNode) // replace front or (if not NULL) back child
+	{
+		dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf);
+	}
+	else
+	{
+		mOspTree->mRoot = leaf;
+	}
+}
+
+
+
+
 // ---------------------------------------------------------------------------
 //  StdInParseHandlers: Overrides of the SAX ErrorHandler interface
 // ---------------------------------------------------------------------------
+
+
 void
 ViewCellsParseHandlers::error(const SAXParseException& e)
@@ -755,5 +1015,5 @@
       {
 	const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
-#if ZIPPED_VIEWCELLS
+#if USE_GZLIB
 	XMLCh *myFilePath = XMLString::transcode(filename.c_str());
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1166)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1201)
@@ -24,5 +24,5 @@
 	Vector3 normal;
 	  
-	const int i = RandomValue(0, (int)mPreprocessor.mObjects.size() - 1);
+	const int i = RandomValue(0, float((int)mPreprocessor.mObjects.size() - 1));
 
 	Intersectable *object = mPreprocessor.mObjects[i];
