Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1695)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1696)
@@ -772,5 +772,5 @@
 		Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);	
 
-		HierarchyManager *mHierarchyManager = CreateHierarchyManager(buf);
+		mHierarchyManager = CreateHierarchyManager(buf);
 		mViewCellsManager = new VspOspViewCellsManager(vcTree, mHierarchyManager);
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1633)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1696)
@@ -428,4 +428,10 @@
 			<File
 				RelativePath="..\src\MutualVisibility.h">
+			</File>
+			<File
+				RelativePath=".\ObjExporter.cpp">
+			</File>
+			<File
+				RelativePath=".\ObjExporter.h">
 			</File>
 			<File
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1695)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1696)
@@ -334,5 +334,5 @@
 	
 	/// leaf pvs
-	ObjectPvs *mPvs;
+	//ObjectPvs *mPvs;
 
 	/// Probability that the view point lies in this leaf
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1693)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1696)
@@ -214,5 +214,5 @@
 BvHierarchy::~BvHierarchy()
 {
-	// delete kd intersectables
+	// delete bvh intersectables
 	BvhIntersectableMap::iterator it, it_end = mBvhIntersectables.end();
 
@@ -222,11 +222,15 @@
 	}
 
+	// delete the local subdivision candidates
 	DEL_PTR(mSubdivisionCandidates);
 
+	// delete the presorted objects
 	for (int i = 0; i < 3; ++ i)
 	{
 		DEL_PTR(mSortedObjects[i]);
 	}
-	mSubdivisionStats.close();
+	
+	// delete the tree
+	DEL_PTR(mRoot);
 }
 
@@ -329,5 +333,5 @@
 	for (oit = objects.begin(); oit != oit_end; ++ oit)
 	{
-		nRays += (int)(*oit)->mVssRays.size();
+		nRays += (int)(*oit)->GetOrCreateRays()->size();
 	}
 
@@ -1336,5 +1340,5 @@
 		if (ray->mTerminationObject)
 		{
-			ray->mTerminationObject->mVssRays.push_back(ray);
+			ray->mTerminationObject->GetOrCreateRays()->push_back(ray);
 			if (!ray->Mailed())
 			{
@@ -1346,5 +1350,5 @@
 		if (ray->mOriginObject)
 		{
-			ray->mOriginObject->mVssRays.push_back(ray);
+			ray->mOriginObject->GetOrCreateRays()->push_back(ray);
 
 			if (!ray->Mailed())
@@ -1385,7 +1389,7 @@
 	{
 		Intersectable *obj = *oit;
-		VssRayContainer::const_iterator rit, rit_end = obj->mVssRays.end();
-
-		for (rit = obj->mVssRays.begin(); rit < rit_end; ++ rit)
+		VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end();
+
+		for (rit = obj->GetOrCreateRays()->begin(); rit < rit_end; ++ rit)
 		{
 			VssRay *ray = (*rit);
@@ -1514,7 +1518,7 @@
 								   const bool setCounter) const
 {
-	VssRayContainer::const_iterator rit, rit_end = obj->mVssRays.end();
-
-	for (rit = obj->mVssRays.begin(); rit < rit_end; ++ rit)
+	VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end();
+
+	for (rit = obj->GetOrCreateRays()->begin(); rit < rit_end; ++ rit)
 	{
 		VssRay *ray = (*rit);
@@ -1557,7 +1561,7 @@
 	int result = 0;
 	
-	VssRayContainer::const_iterator rit, rit_end = obj->mVssRays.end();
-
-	for (rit = obj->mVssRays.begin(); rit < rit_end; ++ rit)
+	VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end();
+
+	for (rit = obj->GetOrCreateRays()->begin(); rit < rit_end; ++ rit)
 	{
 		VssRay *ray = (*rit);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1695)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1696)
@@ -244,4 +244,8 @@
 	{
 		CLEAR_CONTAINER(mViewCells);
+	}
+	else
+	{
+		DEL_PTR(mViewCellsTree);
 	}
 }
@@ -3041,6 +3045,6 @@
 			{	
 				// prepare some rays for output
-				VssRayContainer::const_iterator rit, rit_end = (*vit)->mVssRays.end();
-				for (rit = (*vit)->mVssRays.begin(); rit != rit_end; ++ rit)
+				VssRayContainer::const_iterator rit, rit_end = (*vit)->GetOrCreateRays()->end();
+				for (rit = (*vit)->GetOrCreateRays()->begin(); rit != rit_end; ++ rit)
 				{
 					collectRays.push_back(*rit);
@@ -4968,15 +4972,11 @@
 
 
-	// should maybe be done here to allow merge working with area or volume
-	// and to correct the rendering statistics
-	if (0) FinalizeViewCells(false);
-		
 	// compute tree by merging the nodes of the spatial hierarchy
 	ViewCell *root = ConstructSpatialMergeTree(mHierarchyManager->GetVspTree()->GetRoot());
 	mViewCellsTree->SetRoot(root);
 
-
 	//////////////////////////
-	//-- update pvs in the whole hierarchy
+	//-- update pvs up to the root of the hierarchy
+
 	ObjectPvs pvs;
 	UpdatePvsForEvaluation(root, pvs);
@@ -5010,8 +5010,9 @@
 	//-- some tasks still to do on the view cells:
 	//-- Compute meshes from view cell geometry, evaluate volume and / or area
+
 	if (1) FinalizeViewCells(true);
 
 	// write out view cells (this moved to preprocessor)
-	if (1 && mExportViewCells)
+	if (mExportViewCells)
 	{
 		char filename[100];
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1692)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1696)
@@ -345,5 +345,6 @@
 
 VspLeaf::VspLeaf(): 
-mViewCell(NULL), mPvs(NULL), mSubdivisionCandidate(NULL)
+mViewCell(NULL), mSubdivisionCandidate(NULL)
+//, mPvs(NULL)
 {
 }
@@ -352,6 +353,4 @@
 VspLeaf::~VspLeaf()
 {
-	DEL_PTR(mPvs);
-
 	VssRayContainer::const_iterator vit, vit_end = mVssRays.end();
 	for (vit = mVssRays.begin(); vit != vit_end; ++ vit)
@@ -380,10 +379,12 @@
 
 VspLeaf::VspLeaf(VspInterior *parent): 
-VspNode(parent), mViewCell(NULL), mPvs(NULL)
+VspNode(parent), mViewCell(NULL)
+//, mPvs(NULL)
 {}
 
 
 VspLeaf::VspLeaf(VspInterior *parent, ViewCellLeaf *viewCell): 
-VspNode(parent), mViewCell(viewCell), mPvs(NULL)
+VspNode(parent), mViewCell(viewCell)
+//, mPvs(NULL)
 {
 }
@@ -963,5 +964,5 @@
 	backData.mNode = backLeaf;
 
-	// explicitely create front and back view cell
+	// create front and back view cell for the new leaves
 	CreateViewCell(frontData, false);
 	CreateViewCell(backData, false);
@@ -2846,7 +2847,7 @@
 	// set view cell values
 	viewCell->mLeaves.push_back(leaf);
-
 	viewCell->SetVolume(mBoundingBox.GetVolume());
-    leaf->mProbability = mBoundingBox.GetVolume();
+
+	leaf->mProbability = mBoundingBox.GetVolume();
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1695)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1696)
@@ -160,5 +160,5 @@
 	//be ignored. Use it as you will!
 	//note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
-#if 0
+#if 1
   _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
   _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
@@ -325,10 +325,10 @@
 	  preprocessor->mUseGlDebugger = false;
 	}
-	
+
 	if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
-	  // just call the mail method -> will be executed in the main thread
-	  pt->Main();
-	}
-	
+		// just call the mail method -> will be executed in the main thread
+		pt->Main();
+	}	
+
 	// release memory
 	Cleanup();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1687)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1696)
@@ -48,12 +48,10 @@
   BvhLeaf *mBvhLeaf;
 //BvhNode *mBvhLeaf;
-  /// some rays piercing this intersectable
-  VssRayContainer mVssRays;
-
+  
   /// # of references to this instance
   int mReferences;
 
   //////////////////
-  // note matt: delete these, they are only taking memory+
+  // note matt: delete these, they are only taking memory
 
 
@@ -77,5 +75,5 @@
 		};
   
-  Intersectable(): mMailbox(0), mReferences(0), mBvhLeaf(0) {}
+  Intersectable(): mMailbox(0), mReferences(0), mBvhLeaf(0), mVssRays(NULL) {}
 
   virtual Intersectable::~Intersectable() {};
@@ -114,7 +112,7 @@
 
   virtual int GetRandomVisibleSurfacePoint(Vector3 &point,
-	  Vector3 &normal,
-	  const Vector3 &viewpoint,
-	  const int maxTries) = 0;
+										   Vector3 &normal,
+										   const Vector3 &viewpoint,
+                                           const int maxTries) = 0;
 
   virtual ostream &Describe(ostream &s) = 0;
@@ -160,8 +158,25 @@
   }
 
-  /** returns normal from the face with the specified index.
-  PROBLEM: Does not fit to all intersectable types (e.g., spheres)
+  /** Returns normal from the face with the specified index.
+	  PROBLEM: Does not fit to all intersectable types (e.g., spheres)
   */
   virtual Vector3 GetNormal(const int idx) const { return Vector3(0, 0, 0); }
+
+  VssRayContainer *GetOrCreateRays() 
+  { 
+	  if (!mVssRays)
+		mVssRays = new VssRayContainer();
+	  return mVssRays;
+  }
+
+  void DelRayRefs()
+  {
+	  DEL_PTR(mVssRays);
+  }
+
+protected:
+
+  /// some rays piercing this intersectable
+  VssRayContainer *mVssRays;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1595)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1696)
@@ -96,5 +96,5 @@
 			{
 				VssRay *nray = new VssRay(*vssRay);
-				(*vit)->mVssRays.push_back(nray);				
+				(*vit)->GetOrCreateRays()->push_back(nray);				
 			}
 		}
@@ -480,8 +480,8 @@
 		for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
 		{
-			CLEAR_CONTAINER((*vit)->mVssRays);
+			(*vit)->DelRayRefs();
 		}
 
-		CLEAR_CONTAINER(mVssRays);
+		//CLEAR_CONTAINER(mVssRays);
 		// ComputeRenderError();
 		++ mPass;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1692)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1696)
@@ -389,5 +389,5 @@
 	SplitQueue viewSpaceQueue;
 
-	int vspSteps, ospSteps;
+	int vspSteps = 0, ospSteps = 0;
 
 	// use sah for evaluating osp tree construction 
@@ -451,4 +451,5 @@
 	// then optimizate view space partition for the current osp
 	// and vice versa until iteration depth is reached.
+
 	bool lastSplitWasOsp = true;
 
@@ -948,16 +949,9 @@
 		SubdivisionCandidate *sc = NextSubdivisionCandidate(splitQueue); 
 		
-		float rc = (float)sc->GetRenderCostDecrease() / (mInitialRenderCost - mHierarchyStats.mTotalCost + 1.0f);
-		float mc = (float)sc->GetPvsEntriesIncr() / (float)mHierarchyStats.mPvsEntries;	
-
+		const float priority = sc->GetPriority();
+		
+		//float rc = (float)sc->GetRenderCostDecrease() / (mInitialRenderCost - mHierarchyStats.mTotalCost + 1.0f);
+		//float mc = (float)sc->GetPvsEntriesIncr() / (float)mHierarchyStats.mPvsEntries;	
 		//cout << "dRc: " << sc->GetRenderCostDecrease() << " dMem: " << sc->GetPvsEntriesIncr() << " ratio1 " << rc << " ratio2 " << mc << " const: " << 1.0f - GetMemoryConst() << endl;
-
-		// minimum slope reached
-		if ((steps > maxSteps) || 
-			((sc->GetPriority() < threshold) && !(steps < minSteps)))
-		{
-			cout << "\n**************** breaking on " << sc->GetPriority() << " smaller than " << threshold << endl;
-			break;
-		}
 
 		////////
@@ -969,7 +963,18 @@
 		if (success)
 		{
-			sc->CollectDirtyCandidates(dirtyCandidates, true);
+			//sc->CollectDirtyCandidates(dirtyCandidates, true);
 			++ steps;
 		}
+
+		DEL_PTR(sc);
+
+		// minimum slope reached
+		if ((steps >= maxSteps) || 
+			((priority < threshold) && !(steps < minSteps)))
+		{
+			cout << "\n**************** breaking on " << priority << " smaller than " << threshold << endl;
+			break;
+		}
+
 	}
 
@@ -1156,7 +1161,4 @@
 										  AxisAlignedBox3 *forcedViewSpace)
 {
-	// assume object space subdivision constructed
-	//mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
-
 	const long startTime = GetTime();
 	const int limit = mNumMultiLevels;
@@ -1538,5 +1540,5 @@
 	for (oit = objects.begin(); oit != oit_end; ++ oit)
 	{
-		(*oit)->mVssRays.clear();
+		(*oit)->DelRayRefs();
 	}
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/data/graz/walls.x3d
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/data/graz/walls.x3d	(revision 257)
+++ GTP/trunk/Lib/Vis/Preprocessing/data/graz/walls.x3d	(revision 1696)
@@ -1,4 +1,3 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN"   "http://www.web3d.org/specifications/x3d-3.0.dtd">
 <X3D profile="Full">
   <head>
Index: GTP/trunk/Lib/Vis/Preprocessing/data/graz/terrain.x3d
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/data/graz/terrain.x3d	(revision 257)
+++ GTP/trunk/Lib/Vis/Preprocessing/data/graz/terrain.x3d	(revision 1696)
@@ -1,4 +1,3 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN"   "http://www.web3d.org/specifications/x3d-3.0.dtd">
 <X3D profile="Full">
   <head>
Index: GTP/trunk/Lib/Vis/Preprocessing/data/graz/terrain_walls.x3d
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/data/graz/terrain_walls.x3d	(revision 257)
+++ GTP/trunk/Lib/Vis/Preprocessing/data/graz/terrain_walls.x3d	(revision 1696)
@@ -1,4 +1,3 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN"   "http://www.web3d.org/specifications/x3d-3.0.dtd">
 <X3D profile="Full">
   <head>
