Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1168)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1197)
@@ -605,5 +605,5 @@
 
 
-bool VrmlExporter::ExportKdTree(const KdTree &tree)
+bool VrmlExporter::ExportKdTree(const KdTree &tree, const bool exportGeometry)
 {
 	 if (mExportRayDensity) 
@@ -1304,5 +1304,4 @@
 
 
-
 void VrmlExporter::ExportBeam(const Beam &beam, const AxisAlignedBox3 &box)
 {
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1139)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1197)
@@ -57,5 +57,5 @@
 
   virtual bool
-  ExportKdTree(const KdTree &tree) = 0;
+  ExportKdTree(const KdTree &tree, const bool exportGeometry = false) = 0;
 
   virtual bool
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1194)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1197)
@@ -478,4 +478,15 @@
 
 
+bool Preprocessor::LoadKdTree()
+{
+	return true;
+}
+
+bool Preprocessor::ExportKdTree()
+{
+	return true;
+}
+
+
 bool Preprocessor::LoadSamples(VssRayContainer &samples, 
 							   ObjectContainer &objects) const
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.h	(revision 1121)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.h	(revision 1197)
@@ -45,5 +45,5 @@
 
   bool
-  ExportKdTree(const KdTree &tree);
+  ExportKdTree(const KdTree &tree, const bool exportGeometry = false);
 
   bool
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1178)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1197)
@@ -594,5 +594,6 @@
 
 
-bool X3dExporter::ExportKdTree(const KdTree &tree)
+bool X3dExporter::ExportKdTree(const KdTree &tree, 
+							   const bool exportGeometry)
 {
 	 if (mExportRayDensity) {
@@ -630,4 +631,11 @@
       tStack.push(interior->mBack);
     }
+	else if (exportGeometry)
+	{
+		SetFilled();
+		SetForcedMaterial(RandomMaterial());
+		ExportGeometry(dynamic_cast<KdLeaf *>(node)->mObjects);	
+		SetWireframe();
+	}
   }
   
@@ -637,6 +645,4 @@
   return true;
 }
-
-
 
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1145)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1197)
@@ -113,4 +113,7 @@
   bool ExportSamples(const VssRayContainer &samples) const;
 
+  bool LoadKdTree();
+  bool ExportKdTree();
+
   /** Get Sample rays of particular type, returns false if this
 	  type of rays is not supported by the preprocessor
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h	(revision 1121)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h	(revision 1197)
@@ -45,5 +45,5 @@
 
   bool
-  ExportKdTree(const KdTree &tree);
+  ExportKdTree(const KdTree &tree, const bool exportGeometry = false);
 
   //bool ExportOspTree(const OspTree &tree);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1196)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1197)
@@ -14,4 +14,10 @@
 int KdNode::sMailId = 1;
 int KdNode::sReservedMailboxes = 1;
+
+inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
+{
+	return obj1->mId < obj2->mId;
+}
+
 
 KdNode::KdNode(KdInterior *parent):mParent(parent), mMailbox(0)
@@ -1127,5 +1133,5 @@
 #define TYPE_INTERIOR -2
 #define TYPE_LEAF -3
-//#define LEAF_END_ID -4
+
 
 
@@ -1148,11 +1154,10 @@
 		stream.write(reinterpret_cast<char *>(&id), sizeof(int));
     }
-	
-	// end leaf
-	//int leafEndId = LEAF_END_ID; stream.write(reinterpret_cast<char *>(&leafEndId), sizeof(int));
-}
-
-
-KdLeaf *KdTree::ImportBinLeaf(ifstream &stream, KdInterior *parent)
+}
+
+
+KdLeaf *KdTree::ImportBinLeaf(ifstream &stream, 
+							  KdInterior *parent,
+							  const ObjectContainer &objects)
 {
 	int leafId = TYPE_LEAF;
@@ -1161,14 +1166,27 @@
 
 	stream.read(reinterpret_cast<char *>(&size), sizeof(int));
-
 	KdLeaf *leaf = new KdLeaf(parent, size);
 
-	Debug << "objects size: " << size << endl;
+	MeshInstance dummyInst(NULL);
+	//Debug << "objects size: " << size << endl;
+
 	// read object ids
 	for (int i = 0; i < size; ++ i)
 	{	
 		stream.read(reinterpret_cast<char *>(&objId), sizeof(int));
-		 //if (samplesIn.eof()) break;
-    }
+		dummyInst.SetId(objId);
+
+		ObjectContainer::const_iterator oit =
+			lower_bound(objects.begin(), objects.end(), (Intersectable *)&dummyInst, ilt);
+								
+		if ((oit != objects.end()) && ((*oit)->GetId() == objId))
+		{
+			leaf->mObjects.push_back(*oit);
+		}
+		else
+		{
+			Debug << "error: object with id " << objId << " does not exist" << endl;
+		}
+	}
 
 	return leaf;
@@ -1214,48 +1232,26 @@
 
 	// export binary version of mesh
-	stack<KdNode *> tStack;
+	queue<KdNode *> tStack;
 	tStack.push(mRoot);
 
 	while(!tStack.empty())
 	{
-		KdNode *node = tStack.top();
+		KdNode *node = tStack.front();
 		tStack.pop();
 		
 		if (node->IsLeaf())
 		{
-			Debug << "l";
+			//Debug << "l";
 			ExportBinLeaf(stream, dynamic_cast<KdLeaf *>(node));
 		}
 		else
 		{
-			Debug << "i";
+			//Debug << "i";
 			KdInterior *interior = dynamic_cast<KdInterior *>(node);
 
 			ExportBinInterior(stream, interior);
 			
+			tStack.push(interior->mFront);
 			tStack.push(interior->mBack);
-			tStack.push(interior->mFront);
-		}
-
-		if (tStack.empty())
-			break;
-
-		node = tStack.top();
-		tStack.pop();
-	
-		if (node->IsLeaf())
-		{
-			Debug << "l";
-			ExportBinLeaf(stream, dynamic_cast<KdLeaf *>(node));
-		}
-		else
-		{
-			Debug << "i";
-			KdInterior *interior = dynamic_cast<KdInterior *>(node);
-
-			ExportBinInterior(stream, interior);
-			
-			tStack.push(interior->mBack);
-			tStack.push(interior->mFront);
 		}
 	}
@@ -1265,5 +1261,7 @@
 
 
-KdNode *KdTree::LoadNextNode(ifstream &stream, KdInterior *parent)
+KdNode *KdTree::LoadNextNode(ifstream &stream, 
+							 KdInterior *parent,
+							 const ObjectContainer &objects)
 {
 	int nodeType;
@@ -1272,5 +1270,5 @@
 	if (nodeType == TYPE_LEAF)
 	{
-		return ImportBinLeaf(stream, dynamic_cast<KdInterior *>(parent));
+		return ImportBinLeaf(stream, dynamic_cast<KdInterior *>(parent), objects);
 	}
 
@@ -1286,15 +1284,16 @@
 
 
-bool KdTree::LoadBinTree(const string &filename, const ObjectContainer &objects)
+bool KdTree::LoadBinTree(const string &filename, ObjectContainer &objects)
 {
 	// export binary version of mesh
-	stack<TraversalData> tStack;
+	queue<TraversalData> tStack;
 	ifstream stream(filename.c_str(), ios::binary);
-	Debug << "here94" << endl;
+
 	if (!stream.is_open())
 		return false;
 
+	std::stable_sort(objects.begin(), objects.end(), ilt);
+
 	mBox.Initialize();
-
 	ObjectContainer::const_iterator oit, oit_end = objects.end();
 
@@ -1309,6 +1308,6 @@
 
 	DEL_PTR(mRoot); // we make a new root
-    Debug << "here44" << endl;
-	KdNode *node = LoadNextNode(stream, NULL);
+  
+	KdNode *node = LoadNextNode(stream, NULL, objects);
 	mRoot = node;
 
@@ -1319,5 +1318,5 @@
 	while(!tStack.empty())
 	{
-		TraversalData tData = tStack.top();
+		TraversalData tData = tStack.front();
 		tStack.pop();
 
@@ -1328,10 +1327,10 @@
 			mStat.nodes += 2;
 
-			Debug << "i" ;
+			//Debug << "i" ;
 			KdInterior *interior = dynamic_cast<KdInterior *>(node);
 			interior->mBox = tData.mBox;
 
-            KdNode *front = LoadNextNode(stream, interior);
-			KdNode *back = LoadNextNode(stream, interior);
+            KdNode *front = LoadNextNode(stream, interior, objects);
+			KdNode *back = LoadNextNode(stream, interior, objects);
 	
 			interior->SetupChildLinks(back, front);
@@ -1339,6 +1338,6 @@
 			++ mStat.splits[interior->mAxis];
 
-			Debug << "plane: " << interior->mAxis << " " << interior->mPosition << endl;
-			Debug << "box: " << tData.mBox << endl;
+			//Debug << "plane: " << interior->mAxis << " " << interior->mPosition << endl;
+			//Debug << "box: " << tData.mBox << endl;
 
 			// compute new bounding box
@@ -1350,16 +1349,14 @@
 							 backBox);
 
+			tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));			
 			tStack.push(TraversalData(back, backBox, tData.mDepth + 1));
-			tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));			
 		}
 		else
 		{
 			EvaluateLeafStats(tData);
-			//dynamic_cast<KdLeaf *>(node)->mBox = tData.mBox;
-			Debug << "l" << endl;
-		}
-	}
-
-	Debug << "\nhere85" << endl;
+			//Debug << "l" << endl;
+		}
+	}
+
 	Debug << mStat << endl;
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1196)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1197)
@@ -392,5 +392,5 @@
 
   bool ExportBinTree(const string &filename);
-  bool LoadBinTree(const string &filename, const ObjectContainer &object);
+  bool LoadBinTree(const string &filename, ObjectContainer &object);
 
 protected:
@@ -559,7 +559,7 @@
 	void ExportBinLeaf(ofstream &stream, KdLeaf *leaf);
 	void ExportBinInterior(ofstream &stream, KdInterior *interior);
-	KdLeaf *ImportBinLeaf(ifstream &stream, KdInterior *parent);
+	KdLeaf *ImportBinLeaf(ifstream &stream, KdInterior *parent, const ObjectContainer &objects);
 	KdInterior *ImportBinInterior(ifstream &stream, KdInterior *parent);
-	KdNode *LoadNextNode(ifstream &stream, KdInterior *parent);
+	KdNode *LoadNextNode(ifstream &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 1196)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1197)
@@ -162,12 +162,16 @@
 	preprocessor->mKdTree->ExportBinTree("kd.bin");
 	
+	MeshManager::GetSingleton()->ExportEntries("meshes.bin");
+
 	KdTree *kdTree2 = new KdTree;
 	kdTree2->LoadBinTree("kd.bin", preprocessor->mObjects);
   	Exporter *exporter = Exporter::GetExporter("testkd.x3d");
 	
+	MeshManager::GetSingleton()->ExportEntries("meshes.bin");
+
 	if (exporter) 
 	{
 		exporter->SetWireframe();
-		exporter->ExportKdTree(*kdTree2);
+		exporter->ExportKdTree(*kdTree2, true);
 
 		delete exporter;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.h	(revision 1141)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ResourceManager.h	(revision 1197)
@@ -91,4 +91,21 @@
 	}
 	
+	bool ExportEntries(const string &filename)
+	{
+		ofstream stream(filename.c_str(), ios::binary);
+	
+		if (!stream.is_open())
+			return false;
+
+		std::map<unsigned int, T *>::const_iterator mit, mit_end = mEntries.end();
+
+		for (mit = mEntries.begin(); mit != mit_end; ++ mit)
+		{
+			int id = (*mit).first;
+			stream.write(reinterpret_cast<char *>(&id), sizeof(int));
+		}
+
+		return true;
+	}
 
 	/** Returns size of resource container.
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1194)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1197)
@@ -62,4 +62,8 @@
 
 
+inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
+{
+	return obj1->mId < obj2->mId;
+}
 
 
@@ -83,4 +87,5 @@
   , mBoundingBoxConverter(bconverter)
 {
+	std::stable_sort(mObjects->begin(), mObjects->end(), ilt);
 }
 
@@ -283,10 +288,4 @@
 
 
-inline bool ilt(Intersectable *obj1, Intersectable *obj2)
-{
-	return obj1->mId < obj2->mId;
-}
-
-
 void ViewCellsParseHandlers::StartViewCell(ViewCell *viewCell, 
 										   AttributeList&  attributes)
@@ -331,6 +330,5 @@
 
 				ObjectContainer::iterator oit =
-				  lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);
-				
+					lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);	
 							
 				if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
@@ -501,5 +499,4 @@
 			
 		BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit);
-
 	
 		if (viewCell->GetId() == viewCellId)
