Index: /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 2382)
+++ /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 2544)
@@ -674,16 +674,4 @@
 
     // Position it at 500 in Z direction
-    //mCamera->setPosition(Vector3(0,50,500));
-	//mCamera->setPosition(Vector3(500,256,666));
-	//mCamera->setPosition(Vector3(1280,600,1666));
-    // Look back along -Z
-    //mCamera->lookAt(Vector3(0,50,-300));
-	//mCamera->lookAt(Vector3(-20,30,10));
-
-	//mFollowCam = mSceneMgr->createCamera("FollowCam");
-	//mFollowCam->setPosition(Vector3(800,150,800));
-	//mFollowCam->setNearClipDistance(5);
-	//mFollowCam->setFOVy(Angle(15));
-
 	mTopCam = mSceneMgr->createCamera("TopCam");
 	mTopCam->setPosition(Vector3(0,mPlaneDim * 1.25,0));
Index: /GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TerrainFrameListener.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TerrainFrameListener.cpp	(revision 2512)
+++ /GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TerrainFrameListener.cpp	(revision 2544)
@@ -501,5 +501,5 @@
 		// draw octree bounding boxes because
 		// interesting for chc visualization
-		if (mNodeVizMode != NODEVIZ_RENDER_PVS) 
+		if (0 && mNodeVizMode != NODEVIZ_RENDER_PVS) 
 		{
 			mSceneMgr->setOption("ShowOctree", &mShowVisualization);
@@ -512,5 +512,5 @@
 		mVizCamera->setOrientation(Quaternion::IDENTITY);
 
-		Vector3 camPos = mCamNode->getPosition();
+		const Vector3 &camPos = mCamNode->getPosition();
 		mVizCamera->setPosition(camPos.x, mVizCameraHeight, camPos.z);
 
Index: /GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TestCullingTerrainApplication.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TestCullingTerrainApplication.cpp	(revision 2501)
+++ /GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TestCullingTerrainApplication.cpp	(revision 2544)
@@ -574,7 +574,7 @@
 								 mSunLight, 
 								 this);
-
+#ifdef GAMETOOLS_ILLUMINATION_MODULE
 	mTerrainFrameListener->setPriority(10);
-	
+#endif
 	mRoot->addFrameListener(mTerrainFrameListener); 
 }
@@ -736,6 +736,5 @@
 bool TestCullingTerrainApplication::LoadViewCells(const String &filename)
 {
-	// if not already loaded, 
-	// the scene manager will load the view cells
+	// if not already loaded, the scene manager will load the view cells
 	return mSceneMgr->setOption("UseViewCells", filename.c_str());
 }
@@ -762,5 +761,5 @@
 	mSavedAmbientLight = mSceneMgr->getAmbientLight();
 
-	// -- ambient light must be full for visualization, shadows disabled
+	//-- ambient light must be full for visualization, shadows disabled
     if (showViz)
 	{
Index: /GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionCullingSceneManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionCullingSceneManager.cpp	(revision 2543)
+++ /GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionCullingSceneManager.cpp	(revision 2544)
@@ -1686,4 +1686,5 @@
 	const bool finalizeViewCells = false;
 
+
 	/////////////
 	//-- load the view cells assigning the found objects to the pvss
@@ -1741,7 +1742,5 @@
 	{		
 		GtpVisibilityPreprocessor::Intersectable *obj = pit.Next();
-		// no associated geometry found
-		if (!obj) continue;
-		SetObjectVisible(obj, loadObjects);
+		if (obj) SetObjectVisible(obj, loadObjects);
 	}
 }
@@ -1757,4 +1756,8 @@
 	GtpVisibilityPreprocessor::ViewCell *viewCell = 
 		mViewCellsManager->GetViewCell(viewPoint);
+
+	//std::stringstream d; d << "vp: " << viewPoint << " vc: " << viewCell;
+	//LogManager::getSingleton().logMessage(d.str());
+	
 
 	// view cell did not change => don't change pvs
@@ -1774,5 +1777,4 @@
 void OcclusionCullingSceneManager::CreateViewCellsGeometry()
 {
-	//LogManager::getSingleton().logMessage("creating view cells geometry");
 	if (mViewCellsGeometryLoaded) return;
 
@@ -1835,5 +1837,4 @@
 	}
 }
-
 //-------------------------------------------------------------------------
 void OcclusionCullingSceneManager::UpdateVisibility(Entity *ent)
Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/preprocess_visibility_internal.sh
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/preprocess_visibility_internal.sh	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/preprocess_visibility_internal.sh	(revision 2544)
@@ -14,6 +14,6 @@
 
 #NUM_SAMPLE_RAYS=200000000
-#NUM_SAMPLE_RAYS=10000000
-NUM_SAMPLE_RAYS=10000
+NUM_SAMPLE_RAYS=30000000
+#NUM_SAMPLE_RAYS=10000
 
 $PREPROCESSOR -total_samples=$NUM_SAMPLE_RAYS \
Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/mypreprocess_visibility.sh
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/mypreprocess_visibility.sh	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/mypreprocess_visibility.sh	(revision 2544)
@@ -1,3 +1,3 @@
 #!/bin/sh
 
-./preprocess_visibility_internal.sh ../data/vienna/vienna_cropped.obj ../data/vienna/vienna_cropped-seq-3000-false-20-viewcells.xml.gz vienna-visibility.bn
+./preprocess_visibility_internal.sh ../data/vienna/vienna_cropped.obj ../data/vienna/vienna_cropped-seq-3000-false-20-viewcells.xml.gz vienna-visibility2.bn
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 2176)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 2544)
@@ -100,26 +100,31 @@
 SceneGraph::AssignObjectIds()
 {
-  int id = 1;
-  stack<SceneGraphNode *> nodeStack;
-  
-  nodeStack.push(mRoot);
+	// matt: rather start with id zero
+	int id = 0;
+	stack<SceneGraphNode *> nodeStack;
 
-  while (!nodeStack.empty()) {
-    SceneGraphNode *node = nodeStack.top();
-    nodeStack.pop();
-		
-    ObjectContainer::iterator mi = node->mGeometry.begin();
-    for (; mi != node->mGeometry.end(); mi++) {
-	 	(*mi)->SetId(id++);
+	nodeStack.push(mRoot);
+
+	while (!nodeStack.empty()) 
+	{
+		SceneGraphNode *node = nodeStack.top();
+		nodeStack.pop();
+
+		ObjectContainer::iterator mi = node->mGeometry.begin();
+
+		for (; mi != node->mGeometry.end(); mi ++) 
+		{
+			(*mi)->SetId(id ++);
+		}
+
+		SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
+		for (; ni != node->mChildren.end(); ni ++) 
+		{
+			nodeStack.push(*ni);
+		}
 	}
-    
-    SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
-    for (; ni != node->mChildren.end(); ni++) {
-      nodeStack.push(*ni);
-    }
-  }
-  
-  // return max id
-  return id;
+
+	// return max id
+	return id;
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2544)
@@ -33,4 +33,10 @@
 
 
+inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
+{
+	return obj1->mId < obj2->mId;
+}
+
+
 Preprocessor *preprocessor = NULL;
   
@@ -403,15 +409,15 @@
 	  ((rayCastMethod == RayCaster::INTEL_RAYCASTER) && mLoadMeshes) ?
 	  &mFaceParents : NULL;
-	
+
 	if (files == 1) 
-	  {
+	{
 		if (strstr(filename.c_str(), ".x3d"))
-		  {
+		{
 			parser = new X3dParser;
-			
+
 			result = parser->ParseFile(filename, 
-									   mSceneGraph->GetRoot(),
-									   mLoadMeshes,
-									   fi);
+				mSceneGraph->GetRoot(),
+				mLoadMeshes,
+				fi);
 			delete parser;
 		}
@@ -421,7 +427,7 @@
 
 			result = parser->ParseFile(filename, 
-									   mSceneGraph->GetRoot(),
-									   mLoadMeshes,
-									   fi);
+				mSceneGraph->GetRoot(),
+				mLoadMeshes,
+				fi);
 			delete parser;
 		}
@@ -432,5 +438,5 @@
 			// hack: load binary dump
 			const string bnFile = ReplaceSuffix(filename, ".obj", ".bn");
-			
+
 			if (!mLoadMeshes)
 			{
@@ -439,14 +445,11 @@
 
 			// parse obj
-            if (!result)
+			if (!result)
 			{
 				cout << "no binary dump available or loading full meshes, parsing file" << endl;
 				parser = new ObjParser;
-		
-				result = parser->ParseFile(filename, 
-								   mSceneGraph->GetRoot(),
-								   mLoadMeshes,
-								   fi);
-						
+
+				result = parser->ParseFile(filename, mSceneGraph->GetRoot(), mLoadMeshes, fi);
+
 				cout << "loaded " << (int)mSceneGraph->GetRoot()->mGeometry.size() << " entities" << endl;
 
@@ -457,5 +460,5 @@
 
 					ExportBinaryObj(bnFile, mSceneGraph->GetRoot());
-				
+
 					cout << "finished" << endl;
 				}
@@ -467,11 +470,8 @@
 		{
 			parser = new UnigraphicsParser;
-			result = parser->ParseFile(filename, 
-									   mSceneGraph->GetRoot(),
-									   mLoadMeshes,								  
-									   fi);
+			result = parser->ParseFile(filename, mSceneGraph->GetRoot(), mLoadMeshes, fi);
 			delete parser;
 		}
-		
+
 		cout << filename << endl;
 	} 
@@ -479,5 +479,5 @@
 	{
 		vector<string>::const_iterator fit, fit_end = filenames.end();
-		
+
 		for (fit = filenames.begin(); fit != fit_end; ++ fit) 
 		{
@@ -506,9 +506,7 @@
 		}
 	}
-	
+
 	if (result) 
-	{ 
-		mSceneGraph->AssignObjectIds();
- 
+	{  
 		int intersectables, faces;
 		mSceneGraph->GetStatistics(intersectables, faces);
@@ -520,7 +518,7 @@
 		mObjects.reserve(intersectables);
 		mSceneGraph->CollectObjects(&mObjects);
-		
-		// temp hack
-		//ExportObj("cropped_vienna.obj", mObjects);
+	
+		mSceneGraph->AssignObjectIds();
+
 		mSceneGraph->GetRoot()->UpdateBox();
 				
@@ -605,24 +603,24 @@
 bool
 Preprocessor::Export( const string &filename,
-					  const bool scene,
-					  const bool kdtree
-					  )
-{
-  Exporter *exporter = Exporter::GetExporter(filename);
-	
-  if (exporter) {
-    if (2 && scene)
-      exporter->ExportScene(mSceneGraph->GetRoot());
-
-    if (1 && kdtree) {
-      exporter->SetWireframe();
-      exporter->ExportKdTree(*mKdTree);
-    }
-
-    delete exporter;
-    return true;
-  }
-
-  return false;
+					 const bool scene,
+					 const bool kdtree
+					 )
+{
+	Exporter *exporter = Exporter::GetExporter(filename);
+
+	if (exporter) {
+		if (2 && scene)
+			exporter->ExportScene(mSceneGraph->GetRoot());
+
+		if (1 && kdtree) {
+			exporter->SetWireframe();
+			exporter->ExportKdTree(*mKdTree);
+		}
+
+		delete exporter;
+		return true;
+	}
+
+	return false;
 }
 
@@ -631,5 +629,5 @@
 {
 	// load the view cells assigning the found objects to the pvss
-#if 1
+#if 0
 	cerr << "loading binary view cells" << endl;
 	ViewCellsManager *dummyViewCellsManager = 
@@ -816,10 +814,4 @@
 // use ascii format to store rays
 #define USE_ASCII 0
-
-
-static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
-{
-	return obj1->mId < obj2->mId;
-}
 
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 2544)
@@ -258,5 +258,5 @@
 	*/
 	void SetupChildLinks(VspNode *front, VspNode *back);
-	/** Returns boundiŽng box of this node.
+	/** Returns bounding box of this node.
 	*/
 	AxisAlignedBox3 GetBoundingBox() const;
@@ -661,5 +661,5 @@
 	/** Imports tree from binary format.
 	*/
-	bool ImportBinary(IN_STREAM &stream, ObjectContainer &pvsObjects);
+	bool ImportBinary(IN_STREAM &stream);
 
 	void TestOutput(const std::string &filename);
@@ -867,9 +867,8 @@
 
 	VspInterior *ImportBinInterior(IN_STREAM  &stream, VspInterior *parent);
-	VspLeaf *ImportBinLeaf(IN_STREAM &stream, VspInterior *parent, const ObjectContainer &pvsObjects);
+	VspLeaf *ImportBinLeaf(IN_STREAM &stream, VspInterior *parent);
 
 	VspNode *ImportNextNode(IN_STREAM &stream, 
-							VspInterior *parent,
-							const ObjectContainer &pvsObjects);
+							VspInterior *parent);
 
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2544)
@@ -1021,5 +1021,4 @@
 	if (!Debug.is_open()) Debug.open("debug.log");
 
-	Debug << "here23" << endl;
 	// give just an empty container as parameter:
 	// this loading function is used in the engine, thus it
@@ -1040,17 +1039,28 @@
 	stream.read(reinterpret_cast<char *>(&numBoxes), sizeof(int));
 
+	iboxes.reserve(numBoxes);
+
+	Vector3 bmin;
+	Vector3 bmax;
+	int id;
+
+	PerfTimer boxTimer;
+
+	boxTimer.Start();
+	boxTimer.Entry();
+	
 	for (int i = 0; i < numBoxes; ++ i)
 	{
-		Vector3 bmin;
-		Vector3 bmax;
-		int id;
-
 		stream.read(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
 		stream.read(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
 		stream.read(reinterpret_cast<char *>(&id), sizeof(int));
 
-		AxisAlignedBox3 box(bmin, bmax);
-		iboxes.push_back(IndexedBoundingBox(id, box));
-	}
+		iboxes.push_back(IndexedBoundingBox(id, AxisAlignedBox3(bmin, bmax)));
+	}
+
+
+	boxTimer.Exit();
+
+	Debug << numBoxes << " boxes loaded in " << boxTimer.TotalTime() << " secs" << endl;
 }
 
@@ -6603,11 +6613,10 @@
 		return false;
 
-	/*if (strstr(filename.c_str(), ".bn"))
-	{
-		cout << "exporting view cells to binary format" << endl;
+	if (strstr(filename.c_str(), ".bn"))
+	{
 		return ExportViewCellsBinary(filename, exportPvs, objects);
-	}*/
-
-	cout << "exporting binary" << endl; string fname("test.vc"); return ExportViewCellsBinary(fname, exportPvs, objects);
+	}
+
+	//cout << "exporting binary" << endl; string fname("test.vc"); return ExportViewCellsBinary(fname, exportPvs, objects);
 
 	const long starttime = GetTime();
@@ -6767,5 +6776,5 @@
 	stream.close();
 	
-	mHierarchyManager->GetVspTree()->TestOutput("output.txt");
+	//mHierarchyManager->GetVspTree()->TestOutput("output.txt");
 
 	cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3 << " secs" << endl;
@@ -7121,20 +7130,12 @@
 	const long startTime = GetTime();
 
+	// load all the bounding boxes
 	IndexedBoundingBoxContainer iboxes;
 	ViewCellsManager::LoadIndexedBoundingBoxesBinary(stream, iboxes);
 
-	Debug << (int)iboxes.size() << " boxes loaded" << endl;
-
-	/*IndexedBoundingBoxContainer::const_iterator bit, bit_end = iboxes.end();
-	for (bit = iboxes.begin(); bit != bit_end; ++ bit)
-	{
-		IndexedBoundingBox ibox = *bit;
-		cerr << "box: " << ibox.first << " " << ibox.second << endl;
-	}*/
-
+	pvsObjects.reserve(iboxes.size());
 
 	if (bconverter)
 	{
-		// all bounding boxes gathered in this step => 
 		// associate object ids with bounding boxes
 		bconverter->IdentifyObjects(iboxes, pvsObjects);
@@ -7142,6 +7143,20 @@
 
 	// sort objects by id
-	sort(pvsObjects.begin(), pvsObjects.end(), ilt);
-	
+	//sort(pvsObjects.begin(), pvsObjects.end(), ilt);
+
+
+	ObjectContainer pvsLookup;
+	pvsLookup.resize(iboxes.size());
+
+	for (size_t i = 0; i < pvsLookup.size(); ++ i)
+	{
+		pvsLookup[i] = NULL;
+	}
+
+	for (size_t i = 0; i < pvsObjects.size(); ++ i)
+	{
+		pvsLookup[pvsObjects[i]->GetId()] = pvsObjects[i];
+	}
+
 
 	/////////////
@@ -7164,5 +7179,5 @@
     ViewCellsTree *vcTree = new ViewCellsTree();
 
-	if (!vcTree->ImportBinary(stream, pvsObjects))
+	if (!vcTree->ImportBinary(stream, pvsLookup))
 	{
 		Debug << "Error: loading view cells tree failed!" << endl;
@@ -7177,5 +7192,5 @@
 	vspTree->mBoundingBox = viewSpaceBox;
 
-	if (!vspTree->ImportBinary(stream, pvsObjects))
+	if (!vspTree->ImportBinary(stream))
 	{
 		Debug << "Error: loading vsp tree failed!" << endl;
@@ -7198,8 +7213,8 @@
 
 
-
 	//////////////
 	//-- do some more preparation
 
+	vm->mViewSpaceBox = viewSpaceBox;
 	vm->mViewCells.clear();
 
@@ -7232,4 +7247,10 @@
 		vc->mLeaves.push_back(leaf);
 	}
+
+	
+	/*for (cit = viewCells.begin(); cit != cit_end; ++ cit)
+	{
+		Debug << "pvssize: " << (*cit)->GetPvs().GetSize();
+	}*/
 
 
@@ -7247,5 +7268,5 @@
 		  << TimeDiff(startTime, GetTime()) * 1e-6f << " secs" << endl;
 
-	vspTree->TestOutput("input.txt");
+	//vspTree->TestOutput("input.txt");
 
 	return vm;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2544)
@@ -371,16 +371,15 @@
 GlRenderer::SetupCamera()
 {
-  Vector3 target = mViewPoint + mViewDirection;
-  //cout << "vp: " << mViewPoint << " dr: " << mViewDirection << endl;
-  //cout<<"box: " << mKdTree->GetBox()<<endl;
-  Vector3 up(0,1,0);
-  
-  if (abs(DotProd(mViewDirection, up)) > 0.99f)
-	up = Vector3(1, 0, 0);
-  
-  glLoadIdentity();
-  gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
-			target.x, target.y, target.z,
-			up.x, up.y, up.z);
+	Vector3 target = mViewPoint + mViewDirection;
+
+	Vector3 up(0,1,0);
+
+	if (abs(DotProd(mViewDirection, up)) > 0.99f)
+		up = Vector3(1, 0, 0);
+
+	glLoadIdentity();
+	gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
+		target.x, target.y, target.z,
+		up.x, up.y, up.z);
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 2544)
@@ -14,5 +14,5 @@
 #include "Exporter.h"
 #include "BvHierarchy.h"
-
+#include "Timer/PerfTimer.h"
 
 
@@ -22,4 +22,8 @@
 #define TYPE_INTERIOR -2
 #define TYPE_LEAF -3
+
+static PerfTimer sPvsTimer;
+static PerfTimer sSearchTimer;
+
 
 
@@ -2670,36 +2674,26 @@
 										   const ObjectContainer &pvsObjects)
 {
-	int leafId = TYPE_LEAF;
-	int objId = leafId;
+	VspViewCell *leaf = new VspViewCell();
+
+	Intersectable *obj;
+
+	sPvsTimer.Entry();
+
 	int pvsSize;
-
 	stream.read(reinterpret_cast<char *>(&pvsSize), sizeof(int));
-	VspViewCell *leaf = new VspViewCell();
-
-	//cerr << "\npvs size: " << pvsSize << endl;
-
-	MeshInstance dummyInst(NULL);
-	
+	stream.read(reinterpret_cast<char *>(mPvsIds), sizeof(int) * pvsSize);
+
+	// just add pvs dirty, there won't be any duplicates
+	ObjectPvs &pvs = leaf->GetPvs();
+	pvs.Reserve(pvsSize);
+
 	// read object ids
 	for (int i = 0; i < pvsSize; ++ i)
 	{	
-		stream.read(reinterpret_cast<char *>(&objId), sizeof(int));
-
-		// load the object
-		dummyInst.SetId(objId);
-
-		ObjectContainer::const_iterator oit =
-			lower_bound(pvsObjects.begin(), pvsObjects.end(), (Intersectable *)&dummyInst, ilt);
-								
-		if ((oit != pvsObjects.end()) && ((*oit)->GetId() == objId))
-		{
-			//cerr << "o";
-			leaf->GetPvs().AddSample(*oit, 0);
-		}
-		else
-		{
-			cerr << "x " << objId << " ";//error: object with id " << objId << " does not exist" << endl;
-		}
-	}
+		obj = pvsObjects[mPvsIds[i]];
+		if (obj) pvs.AddSampleDirty(obj, 0);
+	}
+
+	sPvsTimer.Exit();
 
 	return leaf;
@@ -2736,4 +2730,8 @@
 	queue<ViewCell *> tStack;
 
+	sPvsTimer.Start();
+
+	mPvsIds = new int[pvsObjects.size()];
+
 	// hack: we make a new root
 	DEL_PTR(mRoot);
@@ -2741,5 +2739,5 @@
 	mRoot = ImportNextNode(stream, NULL, pvsObjects);
 
-	cerr << "root: " << mRoot << endl;
+	//cerr << "root: " << mRoot << endl;
 	tStack.push(mRoot);
 
@@ -2768,4 +2766,8 @@
 	}
 
+	Debug << "needed " << sPvsTimer.TotalTime() << " secs for pvs loading" << endl;
+
+	delete mPvsIds;
+
 	return true;
 }
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 2539)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 2544)
@@ -25,10 +25,7 @@
 
 
-//class ObjectPvs;
-
 
 /** Statistics for a view cell partition.
 */
-
 class ViewCellsStatistics: public StatisticsBase
 {
@@ -84,4 +81,6 @@
 
 
+/** Statistics for a view cells tree.
+*/
 class ViewCellsTreeStats
 {
@@ -142,9 +141,7 @@
 
 
-/**
-	A view cell. View cells are regions in space. The visibility informations,
-	i.e., the primitives seen by the view cell are stored in a PVs.
-	A view cell can be represented in many different ways, e.g.,
-	a mesh representation.
+/** This class represents a view cell, which is a region in view space. 
+	The objects seen by the view cell are stored in a pvs.
+	A view cell can be represented in many different ways, e.g., as a mesh.
 */
 class ViewCell: public MeshInstance
@@ -156,16 +153,14 @@
 
 	ViewCell();
-
 	/** Constructor taking a mesh representing the shape of	the viewcell.
 	*/
 	ViewCell(Mesh *mesh);
-
 	/** Default destructor.
 	*/
 	virtual ~ViewCell();
-	/** Returns Pvs.
+	/** Returns const reference to pvs.
 	*/
 	const ObjectPvs &GetPvs() const;
-	/** Returns pvs.
+	/** Returns reference to pvs.
 	*/
 	ObjectPvs &GetPvs();
@@ -222,17 +217,13 @@
 	*/
 	bool GetValid() const;
-
 	/** Returns estimated render cost of this view cell.
 	*/
 	float GetRenderCost() const;
-
 	/** set color for visiualizations.
 	*/
 	void SetColor(const RgbColor &color);
-
 	/** get color for visualuzations.
 	*/
     RgbColor GetColor() const;
-
 	/** Adds a sample to the pvs.
 		@param sample the sample to be added
@@ -241,5 +232,4 @@
 	*/
 	bool AddPvsSample(Intersectable *sample, const float pdf, float &contribution);
-
 	/** if this is a view cell correspending to a leaf in a hierarchy.
 	*/
@@ -257,5 +247,4 @@
 	*/
 	void SetMergeCost(const float mergeCost);
-
 	/** Returns merge cost needed to merge this leaf from other cells.
 		@hack The function is available for leaves also to have a common interface,
@@ -391,7 +380,14 @@
 {
 public:
-	ViewCellLeaf()  {  mActiveViewCell = this; }
-	ViewCellLeaf(Mesh *mesh):
-	ViewCell(mesh) { mActiveViewCell = this; }
+	
+	ViewCellLeaf()  
+	{  
+		mActiveViewCell = this; 
+	}
+	
+	ViewCellLeaf(Mesh *mesh): ViewCell(mesh) 
+	{ 
+		mActiveViewCell = this; 
+	}
 
 	bool IsLeaf() const
@@ -454,23 +450,22 @@
 public:
 
+	/** Default constructor.
+	*/
 	ViewCellsTree();
-
 	/** View cells tree constructor taking a view cell mnanager as parameter
 	*/
 	ViewCellsTree(ViewCellsManager *vcm);
+	/** Destructor.
+	*/
 	~ViewCellsTree();
-
 	/** Returns number of leaves this view cell consists of.
 	*/
 	int GetNumInitialViewCells(ViewCell *vc) const;
-
 	/** Collects leaves corresponding to a view cell.
 	*/
 	void CollectLeaves(ViewCell *vc, ViewCellContainer &leaves) const;
-
 	/** Merges view cells according to some cost heuristics.
 	*/
 	int ConstructMergeTree(const VssRayContainer &rays, const ObjectContainer &objects);
-	
 	/** Refines view cells using shuffling, i.e., border leaves 
 		of two view cells are exchanged if the resulting view cells
@@ -479,30 +474,23 @@
 	*/
 	int RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
-	
 	/** Assign colors to the viewcells so that they can be renderered interactively without
 	    color flickering.  
 	*/
 	void AssignRandomColors();
-
 	/** Updates view cell stats for this particular view cell.
 	*/
 	void UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat);
-
 	/** Get costs resulting from each merge step. 
 	*/
 	void GetCostFunction(vector<float> &costFunction);
-	
 	/** Returns storage cost resulting from each merge step.
 	*/
 	void GetStorageFunction(vector<int> &storageCost);
-
 	/** Returns optimal set of view cells for a given number of view cells.
 	*/
 	void CollectBestViewCellSet(ViewCellContainer &viewCells, const int numViewCells);
-
 	/** Root of view cells tree.
 	*/
 	ViewCell *GetRoot() const;
-
 	/** Returns pvs of view cell.
 		@note pvs is returned per reference if tree is not compressed,
@@ -510,9 +498,7 @@
 	*/
 	void GetPvs(ViewCell *vc, ObjectPvs &pvs) const;
-
 	/** Returns pvs size (i.e. the render cost of the stored objects)
 	*/
 	float GetTrianglesInPvs(ViewCell *vc) const;
-  
 	/** Returns number of entries associated with this view cell. 
 
@@ -522,26 +508,20 @@
 	*/
 	int GetPvsEntries(ViewCell *vc) const;
-
 	/** Returns the number of physically stored entries in the view cells sub tree.
 		This can vary based on the current storage method
 	*/
 	int CountStoredPvsEntries(ViewCell *root) const;
-
 	/** Returns memory cost of this view cell.
 	*/
 	float GetMemoryCost(ViewCell *vc) const;
-
 	/** Sets method of storage for view cells.
 	*/
 	void SetViewCellsStorage(int type);
-
 	/** pvs storage methods 
 	*/
 	enum {PVS_IN_INTERIORS, COMPRESSED, PVS_IN_LEAVES};
-
 	/** If view cells in this tree have compressed pvs.
 	*/
 	int ViewCellsStorage() const;
-
 	/** Returns active view cell that is in the path of this view cell.
 	*/
@@ -736,4 +716,7 @@
 	int mMaxMergesPerPass;
 	float mAvgCostMaxDeviation;
+
+	int *mPvsIds;
+	ObjectContainer mTempObjects;
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/TraversalTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/TraversalTree.cpp	(revision 2176)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/TraversalTree.cpp	(revision 2544)
@@ -480,9 +480,5 @@
 		AxisAlignedBox3 box = (*mi)->GetBox();
 
-		//cout << "box: " << box << endl;
-		splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
-			box.Max(axis),
-			*mi)
-			);
+		splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX, box.Max(axis),	*mi));
 	}
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 2543)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 2544)
@@ -46,4 +46,11 @@
 
 
+
+inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
+{
+	return obj1->mId < obj2->mId;
+}
+
+
 // pvs penalty can be different from pvs size
 inline static float EvalPvsPenalty(const float pvs, 
@@ -2823,8 +2830,9 @@
 		if (node->IsLeaf()) 
 		{
-			/*const AxisAlignedBox3 box = GetBoundingBox(static_cast<VspLeaf *>(node));
+			const AxisAlignedBox3 box = GetBoundingBox(static_cast<VspLeaf *>(node));
+
 			if (!box.IsInside(point))
-				cerr << "error, point " << point << " should be in view cell " << box << endl;
-			*/	
+				Debug << "error, point " << point << " should be in view cell " << box << endl;
+			
 			viewcell = static_cast<VspLeaf *>(node)->GetViewCell();
 			break;
@@ -3919,6 +3927,5 @@
 
 VspLeaf *VspTree::ImportBinLeaf(IN_STREAM &stream, 
-								VspInterior *parent,
-								const ObjectContainer &pvsObjects)
+								VspInterior *parent)
 {
 	int leafId = TYPE_LEAF;
@@ -3933,6 +3940,5 @@
 
 VspNode *VspTree::ImportNextNode(IN_STREAM &stream, 
-								 VspInterior *parent,
-								 const ObjectContainer &objects)
+								 VspInterior *parent)
 {
 	int nodeType;
@@ -3942,5 +3948,5 @@
 	{
 		//cerr << "l";
-		return ImportBinLeaf(stream, static_cast<VspInterior *>(parent), objects);
+		return ImportBinLeaf(stream, static_cast<VspInterior *>(parent));
 	}
 
@@ -3978,5 +3984,5 @@
 
 
-bool VspTree::ImportBinary(IN_STREAM &stream, ObjectContainer &pvsObjects)
+bool VspTree::ImportBinary(IN_STREAM &stream)
 {
 	// export binary version of mesh
@@ -3988,5 +3994,5 @@
 	// hack: we make a new root
 	DEL_PTR(mRoot);
-	mRoot = ImportNextNode(stream, NULL, pvsObjects);
+	mRoot = ImportNextNode(stream, NULL);
 
 	tStack.push(MyTraversalData(mRoot, 0, mBoundingBox));
@@ -4008,8 +4014,8 @@
 			interior->SetBoundingBox(tData.mBox);
 
-            VspNode *front = ImportNextNode(stream, interior, pvsObjects);
-			VspNode *back = ImportNextNode(stream, interior, pvsObjects);
-	
-			interior->SetupChildLinks(back, front);
+            VspNode *front = ImportNextNode(stream, interior);
+			VspNode *back = ImportNextNode(stream, interior);
+	
+			interior->SetupChildLinks(front, back);
 
 			++ mVspStats.splits[interior->GetAxis()];
@@ -4044,4 +4050,6 @@
 	CollectLeaves(vspLeaves);
 
+	str << "root: " << mBoundingBox << endl;
+
 	vector<VspLeaf *>::const_iterator vit, vit_end = vspLeaves.end();
 
@@ -4053,4 +4061,6 @@
 		str << "\nleaf: " << GetBoundingBox(leaf) << endl;
 
+		ObjectContainer pvsObjects;
+
 		ObjectPvsIterator pit = vc->GetPvs().GetIterator();
 
@@ -4058,9 +4068,15 @@
 		{
 			Intersectable *obj = pit.Next();
-			str << obj->GetId() << " ";
-		}
-	}
-}
-
-
-}
+			pvsObjects.push_back(obj);
+		}
+
+		sort(pvsObjects.begin(), pvsObjects.end(), ilt);
+		str << "pvs: " << pvsObjects.size() << endl;
+		
+		for (int i = 0; i < pvsObjects.size(); ++ i)
+			str << pvsObjects[i]->GetId() << " ";
+	}
+}
+
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2187)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2544)
@@ -857,7 +857,6 @@
 	mCurrentViewCell->GetMesh()->ComputeBoundingBox();
 	AxisAlignedBox3 box = mCurrentViewCell->GetMesh()->mBox;
-	//cout << "box: " << box << endl;
+	
 	vector<KdLeaf *> leaves;
-
 	mKdTree->GetBoxIntersections(box, leaves);
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectionBoundingBoxConverter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectionBoundingBoxConverter.cpp	(revision 2116)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectionBoundingBoxConverter.cpp	(revision 2544)
@@ -40,6 +40,6 @@
 
 bool IntersectionBoundingBoxConverter::IdentifyObjects(
-			const IndexedBoundingBoxContainer &iboxes,
-			ObjectContainer &objects) const
+	                                        const IndexedBoundingBoxContainer &iboxes,
+											ObjectContainer &objects) const
 {
 	GtpVisibilityPreprocessor::IndexedBoundingBoxContainer::
