Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1193)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1196)
@@ -4531,5 +4531,4 @@
 			// compute bounding box of view space
 			mBoundingBox.Include(obj->GetBox());
-			mBoundingBox.Include(obj->GetBox());
 		}
 	}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1194)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1196)
@@ -1125,7 +1125,7 @@
 
 
-#define INTERIOR_START_ID -2
-#define LEAF_START_ID -3
-#define LEAF_END_ID -4
+#define TYPE_INTERIOR -2
+#define TYPE_LEAF -3
+//#define LEAF_END_ID -4
 
 
@@ -1134,12 +1134,15 @@
 	ObjectContainer::const_iterator it, it_end = leaf->mObjects.end();
 	
-	int leafStartId = LEAF_START_ID;
-	stream.write(reinterpret_cast<char *>(&leafStartId), sizeof(int));
+	int type = TYPE_LEAF;
+	int size = (int)leaf->mObjects.size();
+
+	stream.write(reinterpret_cast<char *>(&type), sizeof(int));
+	stream.write(reinterpret_cast<char *>(&size), sizeof(int));
+
 	for (it = leaf->mObjects.begin(); it != it_end; ++ it)
 	{	
 		Intersectable *obj = *it;
-				
 		int id = obj->mId;		
-		
+	
 		//stream.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
 		stream.write(reinterpret_cast<char *>(&id), sizeof(int));
@@ -1147,21 +1150,27 @@
 	
 	// end leaf
-	int leafEndId = LEAF_END_ID;
-	stream.write(reinterpret_cast<char *>(&leafEndId), sizeof(int));
-}
-
-
-void KdTree::ImportBinLeaf(ifstream &stream, KdLeaf *leaf)
-{
-	ObjectContainer::const_iterator it, it_end = leaf->mObjects.end();
-	
-	int leafId = LEAF_START_ID;
+	//int leafEndId = LEAF_END_ID; stream.write(reinterpret_cast<char *>(&leafEndId), sizeof(int));
+}
+
+
+KdLeaf *KdTree::ImportBinLeaf(ifstream &stream, KdInterior *parent)
+{
+	int leafId = TYPE_LEAF;
 	int objId = leafId;
-	
-	while (objId != LEAF_END_ID)
+	int size;
+
+	stream.read(reinterpret_cast<char *>(&size), sizeof(int));
+
+	KdLeaf *leaf = new KdLeaf(parent, size);
+
+	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;
     }
+
+	return leaf;
 }
 
@@ -1169,25 +1178,29 @@
 void KdTree::ExportBinInterior(ofstream &stream, KdInterior *interior)
 {
-	int interiorid = INTERIOR_START_ID;
+	int interiorid = TYPE_INTERIOR;
 	stream.write(reinterpret_cast<char *>(&interiorid), sizeof(int));
 
 	int axis = interior->mAxis;
-	Vector3 pos = interior->mPosition;
+	float pos = interior->mPosition;
 
 	stream.write(reinterpret_cast<char *>(&axis), sizeof(int));
-	stream.write(reinterpret_cast<char *>(&pos), sizeof(Vector3));
-}
-
-
-void KdTree::ImportBinInterior(ifstream &stream, KdInterior *interior)
-{
-	int interiorid = -2;
-	stream.read(reinterpret_cast<char *>(&interiorid), sizeof(int));
+	stream.write(reinterpret_cast<char *>(&pos), sizeof(float));
+}
+
+
+KdInterior *KdTree::ImportBinInterior(ifstream &stream, KdInterior *parent)
+{
+	KdInterior *interior = new KdInterior(parent);
 
 	int axis = interior->mAxis;
-	Vector3 pos = interior->mPosition;
+	float pos = interior->mPosition;
 
 	stream.read(reinterpret_cast<char *>(&axis), sizeof(int));
-	stream.read(reinterpret_cast<char *>(&pos), sizeof(Vector3));
+	stream.read(reinterpret_cast<char *>(&pos), sizeof(float));
+
+	interior->mAxis = axis;
+	interior->mPosition = pos;
+
+	return interior;
 }
 
@@ -1202,5 +1215,4 @@
 	// export binary version of mesh
 	stack<KdNode *> tStack;
-
 	tStack.push(mRoot);
 
@@ -1208,17 +1220,42 @@
 	{
 		KdNode *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);
+		}
+
+		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);
 		}
 	}
@@ -1228,35 +1265,103 @@
 
 
-bool KdTree::LoadBinTree(const string &filename)
-{
-	ofstream stream(filename.c_str(), ios::binary);
-	
+KdNode *KdTree::LoadNextNode(ifstream &stream, KdInterior *parent)
+{
+	int nodeType;
+	stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int));
+
+	if (nodeType == TYPE_LEAF)
+	{
+		return ImportBinLeaf(stream, dynamic_cast<KdInterior *>(parent));
+	}
+
+	if (nodeType == TYPE_INTERIOR)
+	{
+		return ImportBinInterior(stream, dynamic_cast<KdInterior *>(parent));
+	}
+
+	Debug << "error! loading failed!" << endl;
+	
+	return NULL;
+}
+
+
+bool KdTree::LoadBinTree(const string &filename, const ObjectContainer &objects)
+{
+	// export binary version of mesh
+	stack<TraversalData> tStack;
+	ifstream stream(filename.c_str(), ios::binary);
+	Debug << "here94" << endl;
 	if (!stream.is_open())
 		return false;
 
-	// export binary version of mesh
-	stack<KdNode *> tStack;
-
-	tStack.push(mRoot);
+	mBox.Initialize();
+
+	ObjectContainer::const_iterator oit, oit_end = objects.end();
+
+	//-- compute bounding box
+    for (oit = objects.begin(); oit != oit_end; ++ oit)
+	{
+		Intersectable *obj = *oit;
+
+		// compute bounding box of view space
+		mBox.Include(obj->GetBox());
+	}
+
+	DEL_PTR(mRoot); // we make a new root
+    Debug << "here44" << endl;
+	KdNode *node = LoadNextNode(stream, NULL);
+	mRoot = node;
+
+	tStack.push(TraversalData(node, mBox, 0));
+	mStat.Reset();
+	mStat.nodes = 1;
 
 	while(!tStack.empty())
 	{
-		KdNode *node = tStack.top();
-
-		if (node->IsLeaf())
+		TraversalData tData = tStack.top();
+		tStack.pop();
+
+		KdNode *node = tData.mNode;
+
+		if (!node->IsLeaf())
 		{
-			ExportBinLeaf(stream, dynamic_cast<KdLeaf *>(node));
+			mStat.nodes += 2;
+
+			Debug << "i" ;
+			KdInterior *interior = dynamic_cast<KdInterior *>(node);
+			interior->mBox = tData.mBox;
+
+            KdNode *front = LoadNextNode(stream, interior);
+			KdNode *back = LoadNextNode(stream, interior);
+	
+			interior->SetupChildLinks(back, front);
+
+			++ mStat.splits[interior->mAxis];
+
+			Debug << "plane: " << interior->mAxis << " " << interior->mPosition << endl;
+			Debug << "box: " << tData.mBox << endl;
+
+			// compute new bounding box
+			AxisAlignedBox3 frontBox, backBox;
+			
+			tData.mBox.Split(interior->mAxis, 
+							 interior->mPosition, 
+							 frontBox, 
+							 backBox);
+
+			tStack.push(TraversalData(back, backBox, tData.mDepth + 1));
+			tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));			
 		}
 		else
 		{
-			KdInterior *interior = dynamic_cast<KdInterior *>(node);
-
-			ExportBinInterior(stream, interior);
-			
-			tStack.push(interior->mFront);
-			tStack.push(interior->mBack);
+			EvaluateLeafStats(tData);
+			//dynamic_cast<KdLeaf *>(node)->mBox = tData.mBox;
+			Debug << "l" << endl;
 		}
 	}
 
+	Debug << "\nhere85" << endl;
+	Debug << mStat << endl;
+
 	return true;
 }
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1194)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1196)
@@ -392,5 +392,5 @@
 
   bool ExportBinTree(const string &filename);
-  bool LoadBinTree(const string &filename);
+  bool LoadBinTree(const string &filename, const ObjectContainer &object);
 
 protected:
@@ -559,6 +559,7 @@
 	void ExportBinLeaf(ofstream &stream, KdLeaf *leaf);
 	void ExportBinInterior(ofstream &stream, KdInterior *interior);
-void ImportBinLeaf(ifstream &stream, KdLeaf *leaf);
-	void ImportBinInterior(ifstream &stream, KdInterior *interior);
+	KdLeaf *ImportBinLeaf(ifstream &stream, KdInterior *parent);
+	KdInterior *ImportBinInterior(ifstream &stream, KdInterior *parent);
+	KdNode *LoadNextNode(ifstream &stream, KdInterior *parent);
 
   int mTermMaxNodes;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1194)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1196)
@@ -158,13 +158,28 @@
 
 	//-- build kd tree from scene geometry
-
 	preprocessor->BuildKdTree();
 	preprocessor->KdTreeStatistics(cout);
 	preprocessor->mKdTree->ExportBinTree("kd.bin");
+	
+	KdTree *kdTree2 = new KdTree;
+	kdTree2->LoadBinTree("kd.bin", preprocessor->mObjects);
+  	Exporter *exporter = Exporter::GetExporter("testkd.x3d");
+	
+	if (exporter) 
+	{
+		exporter->SetWireframe();
+		exporter->ExportKdTree(*kdTree2);
+
+		delete exporter;
+	}
+
+	DEL_PTR(kdTree2);
+
 	// parse view cells related options
 	preprocessor->PrepareViewCells();
 
 	//  p->mSceneGraph->Export("soda.x3d");
-	if (0) {
+	if (0) 
+	{
 		preprocessor->Export(filename + "-out.x3d", true, false, false);
 		preprocessor->Export(filename + "-kdtree.x3d", false, true, false);	
