Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1557)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1563)
@@ -233,11 +233,11 @@
   Mesh *m = vc->GetMesh();
   
-  if (m) {
-	m->ComputeBoundingBox();
-	return m->mBox;
+  if (m) 
+  {
+	  m->ComputeBoundingBox();
+	  return m->mBox;
   }
 
   AxisAlignedBox3 box;
-  
   box.Initialize();
   
@@ -289,5 +289,4 @@
 	{
 		VssRayContainer::const_iterator it, it_end = rays.end();
-
 		for (it = rays.begin(); it != it_end; ++ it)
 		{
@@ -298,8 +297,6 @@
 	{
 		VssRayContainer::const_iterator it, it_end = rays.end();
-
 		for (it = rays.begin(); it != it_end; ++ it)
 		{
-			//(*it)->Unref();
 			if (!(*it)->IsActive())
 				delete (*it);
@@ -322,5 +319,5 @@
 	mPreprocessor = preprocessor;
 	
-	
+
 	///////////////////////////////////////////////////////
 	//-- Initial sampling for the construction of the view cell hierarchy. 
@@ -361,6 +358,5 @@
 	{
 		//-- export initial view cell partition
-		Debug << "\nView cells after initial sampling:\n" 
-			  << mCurrentViewCellsStats << endl;
+		Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
 
 		const string filename("viewcells.wrl");
@@ -428,20 +424,20 @@
 
 	
-#if 0
-	///////////////
-	//-- Get stats after the additional sampling step
-	//-- and before the bottom-up merge step
-
-	EvaluateViewCellsStats();
-	Debug << "\noriginal view cell partition before post process:\n" 
-		  << mCurrentViewCellsStats << endl;
-
-	mRenderer->RenderScene();
-	SimulationStatistics ss;
-	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
-
-    Debug << ss << endl;
-#endif
-
+	if (0)
+	{
+		///////////////
+		//-- Get stats after the additional sampling step
+		//-- and before the bottom-up merge step
+
+		EvaluateViewCellsStats();
+		Debug << "\noriginal view cell partition before post process:\n" 
+			  << mCurrentViewCellsStats << endl;
+	
+		mRenderer->RenderScene();
+		SimulationStatistics ss;
+		dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
+
+		Debug << ss << endl;
+	}
 
 	////////////////////
@@ -612,10 +608,9 @@
 {
 	ViewCellsParser parser;
-
 	ViewCellsManager *vm = NULL;
 
 	if (parser.ParseViewCellsFile(filename, &vm, objects, bconverter))
 	{
-		long startTime = GetTime();
+		const long startTime = GetTime();
 
 		vm->ResetViewCells();
@@ -671,10 +666,9 @@
 		for (oit = objects.begin(); oit != oit_end; ++ oit)
 		{
-			MeshInstance *mi = dynamic_cast<MeshInstance *>(*oit);
-			const AxisAlignedBox3 box = mi->GetBox();
+			const AxisAlignedBox3 box = (*oit)->GetBox();
 
 			////////////
 			//-- the bounding boxes
-			stream << "<BoundingBox" << " id=\"" << mi->GetId() << "\""
+			stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
 				   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
 				   << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
@@ -1262,6 +1256,4 @@
 											 const bool extrudeBaseTriangles)
 {
-	bool success;
-
 	/// we use predefined view cells from now on
 	mUsePredefinedViewCells = true;
@@ -1271,5 +1263,8 @@
 	{
 		Environment::GetSingleton()->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
-		success = parser.ParseFile(filename, *this);
+		const bool success = parser.ParseFile(filename, *this);
+
+		if (!success)
+			return false;
 	}
 	else
@@ -1277,46 +1272,52 @@
 		// hack: use standard mesh loading
 		// create temporary scene graph for loading the view cells geometry
+		// note: delete the meshes as they are created two times for transformed mesh instances.
 		SceneGraphNode *root = new SceneGraphNode();
-		success = parser.ParseFile(filename, root, true);
-				
-		if (success)
-		{
-			ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
-			for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
+		const bool success = parser.ParseFile(filename, root, true);
+		
+		if (!success)
+		{
+			DEL_PTR(root);
+			return false;
+		}
+
+		ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
+		
+		for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
+		{
+			Mesh *mesh;
+			if ((*oit)->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
 			{
-				if ((*oit)->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
-				{
-					TransformedMeshInstance *mit = dynamic_cast<TransformedMeshInstance *>(*oit);
-					Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
-					mit->GetTransformedMesh(*mesh);
-					mesh->ComputeBoundingBox();
-					mViewCells.push_back(GenerateViewCell(mesh));
-				}
-				else if ((*oit)->Type() == Intersectable::MESH_INSTANCE)
-				{
-					MeshInstance *mit = dynamic_cast<MeshInstance *>(*oit);
-					mViewCells.push_back(GenerateViewCell(mit->GetMesh()));
-				}			
+				TransformedMeshInstance *mit = dynamic_cast<TransformedMeshInstance *>(*oit);
+				mesh = MeshManager::GetSingleton()->CreateResource();
+				mit->GetTransformedMesh(*mesh);
 			}
-		}
+			else if ((*oit)->Type() == Intersectable::MESH_INSTANCE)
+			{
+				MeshInstance *mit = dynamic_cast<MeshInstance *>(*oit);
+				mesh = mit->GetMesh();
+			}
+			mesh->ComputeBoundingBox();
+			mViewCells.push_back(GenerateViewCell(mesh));
+		}
+
 		DEL_PTR(root);
 	}
 
-	if (success)
-	{
-		// set view space box to bounding box of the view cells
-		AxisAlignedBox3 bbox;
-		bbox.Initialize();
-		ViewCellContainer::iterator it = mViewCells.begin(), it_end = mViewCells.end();
-		for (; it != it_end; ++ it) 
-		{
-			bbox.Include(GetViewCellBox(*it));
-		}
-		SetViewSpaceBox(bbox);
-	
-		cout << "generated " << (int)mViewCells.size() << " view cells using the geometry from file " << filename << endl;
-	}
-
-	return success;
+	// set view space box to bounding box of the view cells
+	AxisAlignedBox3 bbox;
+	bbox.Initialize();
+	ViewCellContainer::iterator it = mViewCells.begin(), it_end = mViewCells.end();
+
+	for (; it != it_end; ++ it) 
+	{
+		bbox.Include((*it)->GetMesh()->mBox);
+	}
+
+	SetViewSpaceBox(bbox);
+	cout << "view space box: " << bbox << endl;
+	cout << "generated " << (int)mViewCells.size() << " view cells using the geometry " << filename << endl;
+
+	return true;
 }
 
@@ -2539,10 +2540,9 @@
 	VssRayContainer savedRays;
 
+	// choose a a number of rays based on the ratio of cast rays / requested rays
 	const int limit = min(mInitialSamples, (int)rays.size());
-
 	VssRayContainer::const_iterator it, it_end = rays.end();
 
 	const float prop = (float)limit / ((float)rays.size() + Limits::Small);
-
 	for (it = rays.begin(); it != it_end; ++ it)
 	{
@@ -2785,5 +2785,5 @@
 		return;
 	
-	int savedColorCode = mColorCode;
+	const int savedColorCode = mColorCode;
 	
 	if (1) // export final view cells
@@ -2989,17 +2989,6 @@
 												 const AxisAlignedBox3 *sceneBox,
 												 const AxisAlignedPlane *clipPlane
-												 ) const
-{
-	// out of bounds cell
-	//if (vc->GetId() == OUT_OF_BOUNDS_ID) return;
-
-	// export mesh if available
-	if (vc->GetMesh())
-	{
-		exporter->ExportMesh(vc->GetMesh());
-		return;
-	}
-
-	// otherwise construct from leaves
+													) const
+{
 	if (clipPlane)
 	{
@@ -3018,5 +3007,5 @@
 			mBspTree->ConstructGeometry(*it, geom);
 
-			const float eps = 0.00000001f;
+			const float eps = 0.0001f;
 			const int cf = geom.Side(plane, eps);
 
@@ -3032,7 +3021,7 @@
 								   mViewSpaceBox, 
 								   eps);
-	
+
 				if (back.Valid())
-				{	
+				{
 					exporter->ExportPolygons(back.GetPolys());
 				}			
@@ -3042,7 +3031,16 @@
 	else
 	{
-		BspNodeGeometry geom;
-		mBspTree->ConstructGeometry(vc, geom);
-		exporter->ExportPolygons(geom.GetPolys());
+		// export mesh if available
+		// TODO: some bug here?
+		if (1 && vc->GetMesh())
+		{
+			exporter->ExportMesh(vc->GetMesh());
+		}
+		else
+		{
+			BspNodeGeometry geom;
+			mBspTree->ConstructGeometry(vc, geom);
+			exporter->ExportPolygons(geom.GetPolys());
+		}
 	}
 }
@@ -3158,8 +3156,7 @@
 		for (oit = objects.begin(); oit != oit_end; ++ oit)
 		{
-			MeshInstance *mi = dynamic_cast<MeshInstance *>(*oit);
-			const AxisAlignedBox3 box = mi->GetBox();
+			const AxisAlignedBox3 box = (*oit)->GetBox();
 			
-			stream << "<BoundingBox" << " id=\"" << mi->GetId() << "\""
+			stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
 				   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
 				   << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
@@ -4123,8 +4120,6 @@
 		{
 			cout << "exporting view cells after post process ... ";
-
 			if (0)
-			{
-				// export view space box
+			{	// export view space box
 				exporter->SetWireframe();
 				exporter->ExportBox(mViewSpaceBox);
@@ -4133,5 +4128,4 @@
 
 			Material m; 
-
 			m.mDiffuseColor.r = 0.0f;
 			m.mDiffuseColor.g = 0.5f;
@@ -4140,15 +4134,13 @@
             exporter->SetForcedMaterial(m);
 
-			if (0 && mExportGeometry)
+			if (1 && mExportGeometry)
 			{
 				exporter->ExportGeometry(objects);
 			}
 
-			// export rays
 			if (0 && mExportRays)
 			{
 				exporter->ExportRays(visRays, RgbColor(1, 0, 0));
 			}
-
 			ExportViewCellsForViz(exporter, NULL, GetClipPlane());
 
@@ -4500,6 +4492,4 @@
 													) const
 {
-	// out of bounds cell
-	//if (vc->GetId() == OUT_OF_BOUNDS_ID)	return;
 	if (clipPlane)
 	{
@@ -4513,5 +4503,4 @@
 		{
 			BspNodeGeometry geom;
-
 			BspNodeGeometry front;
 			BspNodeGeometry back;
@@ -4545,5 +4534,5 @@
 		// export mesh if available
 		// TODO: some bug here?
-		if (0 && vc->GetMesh())
+		if (1 && vc->GetMesh())
 		{
 			exporter->ExportMesh(vc->GetMesh());
@@ -5070,5 +5059,5 @@
 
 			// matt: hack for clamping scene
-			AxisAlignedBox3 bbox = mHierarchyManager->GetViewSpaceBox();
+			AxisAlignedBox3 bbox = mViewSpaceBox;
 			bbox.Scale(Vector3(0.5, 1, 0.5));
 			if (CLAMP_TO_BOX)
