Index: GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 1274)
+++ GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 1285)
@@ -6,4 +6,5 @@
 #include "TestKdTree.h"
 #include "WinCmdLineParser.h"
+#include <cstring>
 
 #ifdef __cplusplus
@@ -37,5 +38,6 @@
 				.addOpt("s","scenemgr", ARGUMENT_REQUIRED)
 				.addOpt("","comment", ARGUMENT_REQUIRED)
-				.addOpt("e","enhancevis", ARGUMENT_NONE);
+				.addOpt("e","enhancevis", ARGUMENT_NONE)
+				.addOpt("","savesceneto", ARGUMENT_REQUIRED);
 
 
@@ -46,4 +48,5 @@
 			options.mDemoMode = cmdparser.getOpt("d");
 			options.mEnhancedVisibility = cmdparser.getOpt("e");
+			cmdparser.getOpt("savesceneto", options.mSceneOutfileName);
 
 			std::string tmp;
@@ -302,6 +305,6 @@
 		mCamNode->setPosition(707,2500,528);
 		mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
-		//mCamNode->setPosition(0,0,0);
-		//mCamNode->setOrientation(Quaternion(Radian(Math::PI),Vector3::UNIT_Y));
+		//mCamNode->setPosition(1056.59, 467.412, 783.502);
+		//mCamNode->setOrientation(Quaternion(-0.281354, 0.0204027, 0.956747, 0.0695345));
 	}
 	// load scene from file
@@ -310,4 +313,5 @@
 		// don't load the other stuff
 		loadScene(mOptions.mSceneFiles);
+
 		// top view
 		mTopCam->setPosition(Vector3(1232, 3990, -1477));
@@ -531,4 +535,7 @@
 void KdTreeApp::createFrameListener(void)
 {
+	// set pointer to self in options
+	mOptions.myApp = this;
+
 	mFrameListener = new KdTreeAppListener(mWindow, mSceneMgr, mOptions);
 	//mFrameListener->showDebugOverlay( true );
@@ -760,5 +767,7 @@
 	LogManager::getSingleton().logMessage(d.str());
 
-	bool result = false;
+	bool loadres = false;
+	bool result = filenames.empty() ? false : true;
+
 	std::vector<std::string>::const_iterator fit, fit_end = filenames.end();
 	int i = 0;
@@ -770,27 +779,25 @@
 		if (strstr(fn.c_str(), ".iv") || strstr(fn.c_str(), ".wrl"))
 		{
-			// hack: set postion manually for vienna
-			//mCamNode->setPosition(Vector3(830, 300, -540));
-			//mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
-
 			// load iv files
-			loadSceneIV(fn, mSceneMgr->getRootSceneNode(), i);			
-		}
-
-		result = true;
-	}
-
-
-	/*
-	if (result) 
-	{
-	int intersectables, faces;
-
-	std::stringstream d;
-	d << filename << " parsed successfully.\n"
-	<< "#NUM_OBJECTS (Total numner of objects)\n" << intersectables << "\n"
-	<< "#NUM_FACES (Total numner of faces)\n" << faces << "\n";
-	}
-	*/
+			loadres = loadSceneIV(fn, mSceneMgr->getRootSceneNode(), i);			
+		}
+		else if (strstr(fn.c_str(), ".cfg"))
+		{
+			// load terrain from cfg
+			loadres = loadSceneCfg(fn, mSceneMgr->getRootSceneNode(), i);
+		}
+		else if (strstr(fn.c_str(), ".txt"))
+		{
+			// load entities form text file
+			loadres = loadSceneASCII(fn, mSceneMgr->getRootSceneNode(), i);
+		}
+		else
+		{
+			loadres = false;
+		}
+
+		// result is true only if all files have been succesfully loaded
+		result = result ? loadres : false;
+	}
 
 	return result;
@@ -802,5 +809,5 @@
 	IVReader * mIVReader = new IVReader();
 
-	if (1)
+	if (0)
 	{
 		String logFilename = "IVLog" + Ogre::StringConverter().toString(index) + ".log";
@@ -823,7 +830,189 @@
 		return true;
 	}
-
-	return false;
-}
+	else
+	{
+		OGRE_DELETE(mIVReader);
+
+		return false;
+	}
+}
+
+//-----------------------------------------------------------------------
+bool KdTreeApp::loadSceneCfg(const String &filename, SceneNode *root, const int index)
+{
+	// Set ambient light
+	mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
+	// Create a light
+	Light* l = mSceneMgr->createLight("MainLight");
+	l->setPosition(20,80,50);
+	
+	// try to load world geometry defined in the cfg file
+	mSceneMgr -> setWorldGeometry( filename );
+
+	// return true since we have no feedback from setWorldGeometry
+	return true;
+}
+
+#define MAX_BUF_SIZE 1024
+
+//-----------------------------------------------------------------------
+bool KdTreeApp::loadSceneASCII(const String &filename, SceneNode *root, const int index)
+{
+	typedef std::map<std::string, std::string> EntMeshMap;
+	typedef std::vector<std::string> Line;
+	
+	std::ifstream read;
+	read.open(filename.c_str());
+	if (read.is_open())
+	{
+		char buf[MAX_BUF_SIZE];
+		Line line;
+		std::stringstream s;
+		std::string node, ent, mesh;
+		Vector3 pos;
+		Quaternion or;
+		Vector3 scale;
+		int count;
+		EntMeshMap emm;
+
+		// the node which is parent to all entities
+		SceneNode *anchor = mSceneMgr->getRootSceneNode()->createChildSceneNode("AnchorNode");
+
+		while (!read.eof())
+		{
+			line.clear();
+			memset(buf, 0, MAX_BUF_SIZE);
+			read.getline(buf, MAX_BUF_SIZE);
+			splitFilenames(std::string(buf), line);
+			if (line.size() < 5)
+				continue;
+			// see if node not empty
+			s << line[4];
+			s >> count;
+			s.clear();
+			if (count > 0)
+			{
+				// read info
+				node = line[0];
+				s << line[1];
+				s >> pos.x >> pos.y >> pos.z;
+				s.clear();
+				s << line[2];
+				s >> or.x >> or.y >> or.z >> or.w;
+				s.clear();
+				s << line[3];
+				s >> scale.x >> scale.y >> scale.z;
+				s.clear();
+				for (int i = 5; i < 5 + count; i ++)
+				{
+					s << line[i];
+					s >> ent >> mesh;
+					s.clear();
+					emm[ent] = mesh;
+				}
+				// build node
+				SceneNode *newnode = anchor->createChildSceneNode(node, pos, or);
+				newnode->setScale(scale);
+				for (EntMeshMap::iterator emmit = emm.begin(); emmit != emm.end(); emmit ++)
+				{
+					Entity * entity = mSceneMgr->createEntity(emmit->first, emmit->second);
+					newnode->attachObject(entity);
+				}
+				emm.clear();
+			}
+
+
+		}
+
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+//-----------------------------------------------------------------------
+bool KdTreeApp::saveSceneASCII(const String& filename, SceneNode *entityroot)
+{
+	// find all entities under node entityroot and store their name, position
+	//and orientation to a test file
+	std::ofstream write;
+	write.open(filename.c_str());
+	if (write.is_open())
+	{
+		NodeList list;
+		addNodesToList(entityroot, list);
+
+		std::string fs = ";", rs = "\n";
+		std::string entnames;
+		int entcount;
+
+		for (NodeList::iterator it = list.begin(); it != list.end(); it ++)
+		{
+			// fist dump info about node (name, position, orientation, scale)
+			SceneNode * node = *it;
+			write << node->getName() << fs << 
+				StringConverter::toString(node->getPosition()) << fs <<
+				StringConverter::toString(node->getOrientation()) << fs <<
+				StringConverter::toString(node->getScale()) << fs;
+
+			entcount = 0;
+			entnames = "";
+
+			SceneNode::ObjectIterator objIt = node->getAttachedObjectIterator();
+
+			while (objIt.hasMoreElements())
+			{
+				MovableObject *movable = objIt.getNext();
+				if (movable->getMovableType() == "Entity")
+				{
+					Entity *ent = static_cast<Entity *>(movable);
+					entcount++;
+					entnames += fs + ent->getName() + " " + ent->getMesh()->getName();
+
+				}
+			}
+
+			// now dump entity count and names
+			write << entcount << entnames << rs;
+		}
+
+		write.close();
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+
+}
+
+void KdTreeApp::addNodesToList(SceneNode* node, NodeList& list)
+{
+	//// check if node has entities
+	//bool entfound = false;
+
+	//SceneNode::ObjectIterator objIt = node->getAttachedObjectIterator();
+	//while (objIt.hasMoreElements())
+	//{
+	//	MovableObject *movable = objIt.getNext();
+	//	if (movable->getMovableType() == "Entity")
+	//		entfound = true;
+	//}
+
+	//// if yes, add to list
+	//if (entfound)
+	list.push_back(node);
+
+	// check if node has children and add them to list
+	SceneNode::ChildNodeIterator childIt = node->getChildIterator();
+	while (childIt.hasMoreElements())
+	{
+		SceneNode *child = static_cast<SceneNode *>(childIt.getNext());
+		addNodesToList(child, list);
+	}
+}
+
 
 /**********************************************************************/
Index: GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp	(revision 1274)
+++ GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp	(revision 1285)
@@ -4,4 +4,5 @@
 #include <OgrePanelOverlayElement.h>
 #include "TestKdTreeAppListener.h"
+#include "TestKdTree.h"
 #include <windows.h>
 
@@ -543,4 +544,13 @@
 	}
 
+	if (mInputDevice->isKeyDown(KC_F7) && mTimeUntilNextToggle <= 0)
+	{
+		SceneNode *entnode = mSceneMgr->getSceneNode("AnchorNode");
+		if (!mOptions.myApp->saveSceneASCII(mOptions.mSceneOutfileName, entnode))
+			LogManager::getSingleton().logMessage(
+			"##Error##: Failed to save scene to " + mOptions.mSceneOutfileName);
+		mTimeUntilNextToggle = 0.5;
+	}
+
 	if (mInputDevice->isKeyDown(KC_F9) && mTimeUntilNextToggle <= 0)
 	{
