Index: GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.h	(revision 1996)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.h	(revision 2003)
@@ -30,5 +30,5 @@
 	/** Default constructor initialising e.g., KD tree
 	*/
-	InternalRayCaster(const Preprocessor &preprocessor, KdTree *kdTree);
+	InternalRayCaster(const Preprocessor &preprocessor);
 	virtual ~InternalRayCaster();
 
@@ -60,5 +60,5 @@
 protected:
 
-	KdTree *mKdTree;
+	//KdTree *mKdTree;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2003)
@@ -41,4 +41,7 @@
 		mTrianglePvs = 0;
 		mViewCellId = 0;
+		mTotalTime = 0;
+		mTimePerViewCell = 0;
+		mTotalTrianglePvs = 0;
 	}
 
@@ -53,5 +56,5 @@
 	int mBorderSamples;
 	int mGvsPass;
-	
+
 	int mTotalPvs;
 	int mViewCells;
@@ -59,7 +62,11 @@
 	int mPerViewCellPvs;
 	int mTrianglePvs;
+	int mTotalTrianglePvs;
 	int mViewCellId;
-	
-	float RaysPerSec() const { if (!Time()) return 0; return (float)mTotalSamples / Time() * 1e-6f; }
+
+	float mTimePerViewCell;
+	float mTotalTime;
+
+	float RaysPerSec() const { if (!mTotalTime) return 0; return (float)mTotalSamples / mTotalTime * 1e-6f; }
 
 	void Print(ostream &app) const;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2002)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2003)
@@ -265,11 +265,9 @@
 
 	cout << "binary obj dump available, loading " << filename.c_str() << endl;
-	// table associating indices with vectors
-	map<int, Vector3> hashTable;
+	
 
 	// table for vertices
 	VertexContainer vertices; 
-	FaceContainer faces;
-
+	
 	if (parents)
 		cout << "using face parents" << endl;
@@ -277,4 +275,10 @@
 		cout << "not using face parents" << endl;
 
+	int dummy = 0;
+	vector<Triangle3> triangles;
+	//triangles.reserve(sizeof(Triangle3) * 13000000);
+	//triangles.reserve(13000000);
+	//root->mGeometry.reserve(13000000);
+
 	while (1)
 	{
@@ -291,14 +295,11 @@
 		TriangleIntersectable *obj = new TriangleIntersectable(tri);
 		root->mGeometry.push_back(obj);
-
-		// matt: we don't really need to keep an additional data structure
-		// if working with triangles => remove this
-		if (parents) 
-		{
-			FaceParentInfo info(obj, 0);
-			parents->push_back(info);
-		}	
-	}
-	
+		//triangles.push_back(tri);
+		//dummy ++;
+		//if (dummy % 100000 == 9999)
+		//cout << "idx: " << dummy << endl;
+	}
+	
+	//cout << "here411" << endl;
 	return true;
 }
@@ -405,6 +406,9 @@
 Intersectable *Preprocessor::GetParentObject(const int index) const
 {
-	if (index == -1)
-	  return NULL;
+	if (index < 0)
+	{
+		cerr << "Warning: triangle index smaller zero! " << index << endl;
+		return NULL;
+	}
 	
 	if (!mFaceParents.empty())
@@ -421,14 +425,15 @@
 	}
 	else
-	  {
-	  if (index >= (int)mObjects.size()) {
-		cerr<<"Warning: triangle  index out of range! "<<index<<endl;
-		return NULL;
-	  }
-	  else
-		{
-		  return mObjects[index];
-		}
-	  }
+	{
+		  if (index >= (int)mObjects.size()) 
+		  {
+			  cerr<<"Warning: triangle  index out of range! " << index << endl;
+			  return NULL;
+		  }
+		  else
+		  {
+			  return mObjects[index];
+		  }
+	}
 }
 
@@ -1078,6 +1083,5 @@
 
 
-bool Preprocessor::InitRayCast(const string externKdTree, 
-							   const string internKdTree)
+bool Preprocessor::LoadInternKdTree( const string internKdTree)
 {
 	// always try to load the kd tree
@@ -1111,4 +1115,41 @@
 	cout << mKdTree->GetBox() << endl;
 
+	return true;
+}
+
+
+bool Preprocessor::InitRayCast(const string externKdTree, 
+							   const string internKdTree)
+{
+	// always try to load the kd tree
+/*	cout << "loading kd tree file " << internKdTree << " ... " << endl;
+
+	if (!LoadKdTree(internKdTree))
+	{
+		cout << "error loading kd tree with filename " 
+			 << internKdTree << ", rebuilding it instead ... " << endl;
+		// build new kd tree from scene geometry
+		BuildKdTree();
+
+		// export kd tree?
+		const long startTime = GetTime();
+		cout << "exporting kd tree ... ";
+
+		if (!ExportKdTree(internKdTree))
+		{
+			cout << " error exporting kd tree with filename " 
+				 << internKdTree << endl;
+		}
+		else
+		{
+			cout << "finished in " 
+				 << TimeDiff(startTime, GetTime()) * 1e-3 
+				 << " secs" << endl;
+		}
+	}
+	
+	KdTreeStatistics(cout);
+	cout << mKdTree->GetBox() << endl;
+*/
 	int rayCastMethod;
 	Environment::GetSingleton()->
@@ -1118,5 +1159,5 @@
 	{
 		cout << "ray cast method: internal" << endl;
-		mRayCaster = new InternalRayCaster(*this, mKdTree);
+		mRayCaster = new InternalRayCaster(*this);
 	}
 	else
@@ -1140,6 +1181,4 @@
 					   )
 {
-
-
   const long t1 = GetTime();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1920)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 2003)
@@ -226,13 +226,17 @@
 	*/
 	int GetObjectSpaceSubdivisionType() const;	
+	
 	/** The type of view space space subdivison
 	*/
 	int GetViewSpaceSubdivisionType() const;
+	
 	/** Sets a pointer to the view cells manager.
 	*/		
 	void SetViewCellsManager(ViewCellsManager *vcm);
+	
 	/** Sets a pointer to the view cells tree.
 	*/
 	void SetViewCellsTree(ViewCellsTree *vcTree);
+
 	/** Exports the object hierarchy to disc.
 	*/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2002)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2003)
@@ -164,4 +164,6 @@
 	bool InitRayCast(const string externKdTree, const string internKdTree);
 
+	bool LoadInternKdTree(const string internKdTree);
+
 	bool ExportObj(const string filename, const ObjectContainer &objects);
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1977)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 2003)
@@ -514,5 +514,6 @@
 		float GetPriority() const
 		{
-			return mPriority;
+			return (float)-mParentData.mDepth;
+			//return mPriority;
 		}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 2003)
@@ -26,7 +26,8 @@
 #define USE_FIXEDPOINT_T 0
 #define USE_VOLUMES_FOR_HEURISTICS 1
-
-  //int BvhNode::sMailId = 10000; //2147483647;
-  //int BvhNode::sReservedMailboxes = 1;
+#define TEST_POWERPLANT 0
+  
+//int BvhNode::sMailId = 10000;
+//int BvhNode::sReservedMailboxes = 1;
 
 BvHierarchy *BvHierarchy::BvhSubdivisionCandidate::sBvHierarchy = NULL;
@@ -382,5 +383,5 @@
 										BvhTraversalData &frontData,
 										BvhTraversalData &backData)
-{
+{ 
 	const BvhTraversalData &tData = sc.mParentData;
 	BvhLeaf *leaf = tData.mNode;
@@ -427,5 +428,5 @@
 	++ mBvhStats.splits;
 
-
+ 
 	////////////////////////////////////////
 	//-- fill front and back traversal data with the new values
@@ -446,6 +447,8 @@
 	AssociateObjectsWithLeaf(back);
 	AssociateObjectsWithLeaf(front);
-   
+  
+	////////////
 	//-- compute pvs correction to cope with undersampling
+
 	frontData.mPvs = (float)CountViewCells(front->mObjects);
 	backData.mPvs = (float)CountViewCells(back->mObjects);
@@ -453,4 +456,5 @@
 	frontData.mCorrectedPvs = sc.mCorrectedFrontPvs;
 	backData.mCorrectedPvs = sc.mCorrectedBackPvs;
+
 
 	// compute probability of this node being visible, 
@@ -462,4 +466,5 @@
 	backData.mCorrectedVolume = sc.mCorrectedBackVolume;
 
+
     // how often was max cost ratio missed in this branch?
 	frontData.mMaxCostMisses = sc.GetMaxCostMisses();
@@ -468,7 +473,7 @@
 	// set the time stamp so the order of traversal can be reconstructed
 	node->SetTimeStamp(mHierarchyManager->mTimeStamp ++);
-		
+	 
 	// assign the objects in sorted order
-	if (mUseGlobalSorting)
+	if (!TEST_POWERPLANT && mUseGlobalSorting)
 	{
 		AssignSortedObjects(sc, frontData, backData);
@@ -497,5 +502,5 @@
 		BvhTraversalData tFrontData;
 		BvhTraversalData tBackData;
-			
+		
 		// create new interior node and two leaf node
 		currentNode = SubdivideNode(*sc, tFrontData, tBackData);
@@ -628,4 +633,5 @@
 	}
 
+
 	const BvhTraversalData &tData = splitCandidate.mParentData;
 	BvhLeaf *leaf = tData.mNode;
@@ -671,9 +677,11 @@
 
 	if (0)
-	cout << "bvh volume cost"
+	{
+		cout << "bvh volume cost"
 		 << " avg ray contri: " << avgRayContri << " ratio: " << oldRatio 
 		 << " parent: " << parentVol << " old vol: " << oldVolume 
 		 << " frontvol: " << frontVol << " corr. " << splitCandidate.mCorrectedFrontVolume 
 		 << " backvol: " << backVol << " corr. " << splitCandidate.mCorrectedBackVolume << endl;
+	}
 
 #ifdef GTP_DEBUG
@@ -849,5 +857,5 @@
 
 
-#if 0
+#if 1
 
 /// compute object boundaries using spatial mid split
@@ -857,6 +865,8 @@
 											ObjectContainer &objectsBack)
 {
-	const float maxBox = tData.mBoundingBox.Max(axis);
-	const float minBox = tData.mBoundingBox.Min(axis);
+	AxisAlignedBox3 parentBox = tData.mNode->GetBoundingBox();
+
+	const float maxBox = parentBox.Max(axis);
+	const float minBox = parentBox.Min(axis);
 
 	float midPoint = (maxBox + minBox) * 0.5f;
@@ -882,6 +892,9 @@
 	}
 
-	const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects);
-	const float newRenderCost = EvalRenderCost(objectsFront) * EvalRenderCost(objectsBack);
+	AxisAlignedBox3 fbox = EvalBoundingBox(objectsFront, &parentBox);
+	AxisAlignedBox3 bbox = EvalBoundingBox(objectsBack, &parentBox);
+
+	const float oldRenderCost = (float)tData.mNode->mObjects.size() * parentBox.SurfaceArea();
+	const float newRenderCost = (float)objectsFront.size() * fbox.SurfaceArea() +  (float)objectsBack.size() * bbox.SurfaceArea();
 
 	const float ratio = newRenderCost / oldRenderCost;
@@ -920,8 +933,9 @@
 
 #if 1
+	// hack: always take driving axis
 	const float cost = (tData.mNode->GetBoundingBox().Size().DrivingAxis() == axis) ? -1.0f : 0.0f;
 #else
 	const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects);
-	const float newRenderCost = EvalRenderCost(objectsFront) * EvalRenderCost(objectsBack);
+	const float newRenderCost = EvalRenderCost(objectsFront) + EvalRenderCost(objectsBack);
 
 	const float cost = newRenderCost / oldRenderCost;
@@ -986,5 +1000,5 @@
 	}
 
-	// temporary surface areas
+	// record surface areas during the sweep
 	float al = 0;
 	float ar = boxArea;
@@ -1394,5 +1408,5 @@
 void BvHierarchy::CreateLocalSubdivisionCandidates(const ObjectContainer &objects, 
 												  SortableEntryContainer **subdivisionCandidates, 
-												  const bool sort,
+												  const bool sortEntries,
 												  const int axis)
 {
@@ -1400,5 +1414,5 @@
 
 	// compute requested size and look if subdivision candidate has to be recomputed
-	const int requestedSize = (int)objects.size() * 2;
+	const int requestedSize = (int)objects.size();
 	
 	// creates a sorted split candidates array
@@ -1423,7 +1437,8 @@
 	}
 
-	if (sort)
+	if (sortEntries)
 	{	// no presorted candidate list
-		stable_sort((*subdivisionCandidates)->begin(), (*subdivisionCandidates)->end());
+		//stable_sort((*subdivisionCandidates)->begin(), (*subdivisionCandidates)->end());
+		sort((*subdivisionCandidates)->begin(), (*subdivisionCandidates)->end());
 	}
 }
@@ -2201,9 +2216,9 @@
 	// root and bounding box was already constructed
 	BvhLeaf *bvhLeaf = dynamic_cast<BvhLeaf *>(mRoot);
-
+	
 	// only rays intersecting objects in node are interesting
 	const int nRays = AssociateObjectsWithRays(sampleRays);
 	//cout << "using " << nRays << " of " << (int)sampleRays.size() << " rays" << endl;
-
+	
 	// probability that volume is "seen" from the view cells
 	const float prop = EvalViewCellsVolume(objects) / GetViewSpaceVolume();
@@ -2213,5 +2228,5 @@
 	
 	// create sorted object lists for the first data
-	if (mUseGlobalSorting)
+	if (!TEST_POWERPLANT && mUseGlobalSorting)
 	{
 		AssignInitialSortedObjectList(oData, objects);
@@ -2257,7 +2272,9 @@
 			tQueue.Push(sCandidate);
 		}
+
+		cout << "size of initial bv subdivision: " << GetStatistics().Leaves() << endl;
 	}
 	else
-	{
+	{	
 		// evaluate priority
 		EvalSubdivisionCandidate(*oSubdivisionCandidate);
@@ -2265,7 +2282,6 @@
 
 		tQueue.Push(oSubdivisionCandidate);
-	}
-		
-	cout << "size of initial bv subdivision: " << GetStatistics().Leaves() << endl;
+		cout << "size of initial bv subdivision: " << GetStatistics().Leaves() << endl;
+	}
 }
 
@@ -2304,5 +2320,6 @@
 
 	*(tData.mSortedObjects[3]) = objects;
-	stable_sort(tData.mSortedObjects[3]->begin(), tData.mSortedObjects[3]->end(), smallerSize);
+	//stable_sort(tData.mSortedObjects[3]->begin(), tData.mSortedObjects[3]->end(), smallerSize);
+	sort(tData.mSortedObjects[3]->begin(), tData.mSortedObjects[3]->end(), smallerSize);
 }
 
@@ -2375,5 +2392,6 @@
 	BvhTraversalData oData(bvhLeaf, 0, prop, nRays);
 
-	AssignInitialSortedObjectList(oData, objects);
+	if (!TEST_POWERPLANT && mUseGlobalSorting)
+		AssignInitialSortedObjectList(oData, objects);
 	
 
@@ -2657,6 +2675,7 @@
 			delete bsc;
 		}
-		else // initial preprocessing  finished for this candidate
-		{
+		else 
+		{
+			// initial preprocessing  finished for this candidate
 			// add to candidate container
 			candidateContainer.push_back(bsc);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2003)
@@ -311,9 +311,9 @@
 	// question: rather create view cells resource manager?
 	if (!ViewCellsTreeConstructed())
-	{cout << "here6" << endl;
+	{
 		CLEAR_CONTAINER(mViewCells);
 	}
 	else
-	{cout << "here7" << endl;
+	{
 		DEL_PTR(mViewCellsTree);
 	}
@@ -5962,5 +5962,5 @@
 		}
 
-		// hack color code (show pvs size)
+		// hack color code
 		const int savedColorCode = mColorCode;
 
@@ -6005,7 +6005,8 @@
 
 		mColorCode = 1; // 0 = random, 1 = export pvs
+		// don't visualize render cost
 		const float maxRenderCost = -1;
 
-		if (0)
+		if (1)
 		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, 
 													  CLAMP_TO_BOX ? &bbox : NULL, maxRenderCost, false);
@@ -6013,5 +6014,6 @@
 
 		//ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
-		ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
+		if (0)
+			ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
 
 		delete exporter;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1920)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 2003)
@@ -149,7 +149,9 @@
 
 		SetFilled();
-		// hack
-		if (1 || (leaf->mObjects.size() < 50000))
-		ExportGeometry(leaf->mObjects, true, box);
+		
+		if (0)
+			ExportGeometry(leaf->mObjects, true, box);
+		else
+			ExportBox(leaf->GetBoundingBox());
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 2003)
@@ -517,5 +517,5 @@
 
 ViewCellsTree::~ViewCellsTree()
-{cout<<"here1002";
+{
 	DEL_PTR(mRoot);
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1941)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 2003)
@@ -495,5 +495,6 @@
 		float GetPriority() const
 		{
-			return mPriority;
+			return (float)-mParentData.mDepth;
+			//return mPriority;
 		}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 2003)
@@ -17,5 +17,6 @@
 RayCaster(preprocessor)
 {
-	InitRayCast(externKdTree);
+	if (!InitRayCast(externKdTree))
+		cout << "warning: intel ray tracer could not be initialized!" << endl;
 }
 
@@ -28,6 +29,7 @@
 bool IntelRayCaster::InitRayCast(const string externKdTree)
 {
-  cout<<"Ray Cast file: " << externKdTree << endl;
-  return mlrtaLoadAS(externKdTree.c_str());
+	cout<<"Intel ray cast file: " << externKdTree << endl;
+	
+	return mlrtaLoadAS(externKdTree.c_str());
 }
 
@@ -41,5 +43,5 @@
 							)
 {
-	//cout << "intel ray" << endl;
+	cout << "intel ray" << endl;
 	VssRay *vssRay  = NULL;
 	int hits = 0;
@@ -73,6 +75,6 @@
 	if (castDoubleRay)
 	{
-	  //cerr<<"HERE"<<endl;
-	  Vector3 dir = -simpleRay.mDirection;
+		//cerr<<"HERE"<<endl;
+		Vector3 dir = -simpleRay.mDirection;
 		hittriangle = mlrtaIntersectAS(
 										&simpleRay.mOrigin.x,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2003)
@@ -94,5 +94,5 @@
 
   if (!splitCandidates)
-    splitCandidates = new vector<SortableEntry>;
+    splitCandidates = new vector<SortableEntry *>;
 
   // first construct a leaf that will get subdivide
@@ -114,4 +114,5 @@
 
   // remove the allocated array
+  CLEAR_CONTAINER(*splitCandidates);
   delete splitCandidates;
 
@@ -460,5 +461,6 @@
 			    )
 {
-  splitCandidates->clear();
+	CLEAR_CONTAINER(*splitCandidates);
+  //splitCandidates->clear();
   
   int requestedSize = 2*(int)node->mObjects.size();
@@ -467,5 +469,5 @@
       requestedSize < (int)(splitCandidates->capacity()/10) ) {
     delete splitCandidates;
-    splitCandidates = new vector<SortableEntry>;
+    splitCandidates = new vector<SortableEntry *>;
   }
   
@@ -478,5 +480,5 @@
     AxisAlignedBox3 box = (*mi)->GetBox();
 
-    splitCandidates->push_back(SortableEntry(SortableEntry::BOX_MIN,
+    splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MIN,
 											 box.Min(axis),
 											 *mi)
@@ -484,5 +486,5 @@
     
     
-    splitCandidates->push_back(SortableEntry(SortableEntry::BOX_MAX,
+    splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
 											 box.Max(axis),
 											 *mi)
@@ -532,11 +534,11 @@
 
   float totalIntersections = 0.0f;
-  vector<SortableEntry>::const_iterator ci;
+  vector<SortableEntry *>::const_iterator ci;
 
   for(ci = splitCandidates->begin();
       ci < splitCandidates->end();
       ci++) 
-    if ((*ci).type == SortableEntry::BOX_MIN) {
-      totalIntersections += (*ci).intersectable->IntersectionComplexity();
+    if ((*ci)->type == SortableEntry::BOX_MIN) {
+      totalIntersections += (*ci)->intersectable->IntersectionComplexity();
     }
 	
@@ -558,20 +560,20 @@
       ci < splitCandidates->end();
       ci++) {
-    switch ((*ci).type) {
+    switch ((*ci)->type) {
     case SortableEntry::BOX_MIN:
       objectsLeft++;
-      intersectionsLeft += (*ci).intersectable->IntersectionComplexity();
+      intersectionsLeft += (*ci)->intersectable->IntersectionComplexity();
       break;
     case SortableEntry::BOX_MAX:
       objectsRight--;
-      intersectionsRight -= (*ci).intersectable->IntersectionComplexity();
+      intersectionsRight -= (*ci)->intersectable->IntersectionComplexity();
       break;
     }
 
-    if ((*ci).value > minBand && (*ci).value < maxBand) {
+    if ((*ci)->value > minBand && (*ci)->value < maxBand) {
       AxisAlignedBox3 lbox = box;
       AxisAlignedBox3 rbox = box;
-      lbox.SetMax(axis, (*ci).value);
-      rbox.SetMin(axis, (*ci).value);
+      lbox.SetMax(axis, (*ci)->value);
+      rbox.SetMin(axis, (*ci)->value);
 
       float sum;
@@ -589,5 +591,5 @@
 	float newCost = mCt_div_ci + sum/boxArea;
 	float ratio = newCost/oldCost;
-	costStream<<(*ci).value<<" "<<ratio<<endl;
+	costStream<<(*ci)->value<<" "<<ratio<<endl;
   }
 #endif
@@ -595,5 +597,5 @@
       if (sum < minSum) {
 		minSum = sum;
-		position = (*ci).value;
+		position = (*ci)->value;
 		
 		objectsBack = objectsLeft;
@@ -1264,5 +1266,5 @@
 		if ((oit != objects.end()) && ((*oit)->GetId() == objId))
 		{
-			leaf->mObjects.push_back(*oit);
+			if (1) leaf->mObjects.push_back(*oit);
 		}
 		else
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1990)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 2003)
@@ -1135,5 +1135,6 @@
 	}
 
-	stable_sort(mLocalSubdivisionCandidates->begin(), mLocalSubdivisionCandidates->end());
+	//stable_sort(mLocalSubdivisionCandidates->begin(), mLocalSubdivisionCandidates->end());
+	sort(mLocalSubdivisionCandidates->begin(), mLocalSubdivisionCandidates->end());
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Statistics.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Statistics.h	(revision 860)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Statistics.h	(revision 2003)
@@ -17,8 +17,8 @@
   static inline double TimeDiff(long time1, long time2) // in s
   {
-    const double clk=1.0E-6; // ticks per second
-    long t=time2-time1;
+    const double clk = 1.0E-6; // ticks per second
+    long t = time2 - time1;
 		
-    return ((t<0)?-t:t)*clk;
+    return ((t < 0) ? -t : t) * clk;
   }
 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.h	(revision 2003)
@@ -548,5 +548,5 @@
   
   // reusable array of split candidates
-  vector<SortableEntry> *splitCandidates;
+  vector<SortableEntry *> *splitCandidates;
 
   float
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2003)
@@ -192,11 +192,25 @@
 	}
 
+	Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
+	string filename(buff);
+	
+	const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
+	const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
+
+	if (preprocessor->InitRayCast(externKdTree, internKdTree))
+	{
+		cout << "ray casting initialized!" << endl;
+	}
+	else
+	{
+		cout << "ray casting initialization failed!" << endl;
+		Cleanup();
+		exit(1);
+	}
+
+
 	/////////////
 	//-- load scene 
 
-	Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
-	string filename(buff);
-	
-
 	if (!preprocessor->LoadScene(filename))
 	{
@@ -206,17 +220,15 @@
 	}
 	
-	const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
-	const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
 
 	////////////
 	//-- initialize external ray caster
 
-	if (preprocessor->InitRayCast(externKdTree, internKdTree))
-	{
-		cout << "ray casting initialized!" << endl;
+	if (preprocessor->LoadInternKdTree(internKdTree))
+	{
+		cout << "intern kd tree loaded!" << endl;
 	}
 	else
 	{
-		cout << "ray casting initialization failed" << endl;
+		cout << "loading intern kd tree failed!" << endl;
 		Cleanup();
 		exit(1);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1996)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 2003)
@@ -10,6 +10,6 @@
 
 
-InternalRayCaster::InternalRayCaster(const Preprocessor &preprocessor, KdTree *kdTree):
-RayCaster(preprocessor), mKdTree(kdTree)
+InternalRayCaster::InternalRayCaster(const Preprocessor &preprocessor):
+RayCaster(preprocessor)
 {
 }
@@ -50,5 +50,5 @@
 
   
-  if (mKdTree->CastRay(ray)) {
+  if (mPreprocessor.mKdTree->CastRay(ray)) {
 	hitA.mObject = ray.intersections[0].mObject;
 	hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
@@ -60,5 +60,5 @@
   ray.mFlags &= ~Ray::CULL_BACKFACES;
   
-  if (castDoubleRay && mKdTree->CastRay(ray)) {
+  if (castDoubleRay && mPreprocessor.mKdTree->CastRay(ray)) {
 	hitB.mObject = ray.intersections[0].mObject;
 	hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
@@ -102,5 +102,5 @@
   VssRay *vssRay;
   
-  if (mKdTree->CastRay(ray)) {
+  if (mPreprocessor.mKdTree->CastRay(ray)) {
 	// sort intersections
 	ray.SortIntersections();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2000)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2003)
@@ -32,5 +32,4 @@
 Preprocessor(), 
 mSamplingType(SamplingStrategy::VIEWCELL_BASED_DISTRIBUTION),
-//mSamplingType(SamplingStrategy::DIRECTION_BASED_DISTRIBUTION),
 mProcessedViewCells(0),
 mCurrentViewCell(NULL)
@@ -203,5 +202,5 @@
 		return false;
 
-	if (0 && GVS_DEBUG)
+	if (1 && GVS_DEBUG)
 		mVssRays.push_back(new VssRay(*vssRay));
 
@@ -653,5 +652,5 @@
 	//exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
 	//exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
-	//exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
+	exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
 }
 
@@ -773,5 +772,5 @@
 		passSamples += newSamples;
 		mGvsStats.mPerViewCellSamples += newSamples;
-		//cout << "here2 " << passSamples % (mSamplesPerPass + 1) << endl;
+		
 
 		if (passSamples >= mSamplesPerPass)
@@ -884,4 +883,5 @@
 		(*oit)->mCounter = 0;
 	}
+	long lastTime = GetTime();
 
 	while (NextViewCell())
@@ -896,4 +896,5 @@
 		//mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize();
 		mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
+		mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
 
 		ObjectContainer objectPvs;
@@ -910,13 +911,21 @@
 		mGvsStats.mViewCells = mProcessedViewCells;//mPass;
 		//mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
-		mGvsStats.mPerViewCellPvs = (int)mTrianglePvs.size();
+		mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
 
 		mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
 		mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
 
+		// timing
+		const long currentTime = GetTime();
+        
+		mGvsStats.mTimePerViewCell = TimeDiff(lastTime, currentTime) * 1e-3f;
+		mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
+
+		lastTime = currentTime;
+
 		mGvsStats.Stop();
 		mGvsStats.Print(mGvsStatsStream);
 
-		mTrianglePvs.clear();
+        mTrianglePvs.clear();
 
 		if (GVS_DEBUG)
@@ -1037,5 +1046,4 @@
 
 	//Randomize(0);
-	
 	mGvsStats.Reset();
 	mGvsStats.Start();
@@ -1119,21 +1127,31 @@
 void GvsStatistics::Print(ostream &app) const
 {
-	app << "#Pass\n" << mPass << endl;
+	app << "#ViewCells\n" << mViewCells << endl;
 	app << "#ViewCellId\n" << mViewCellId << endl;
-	app << "#Time\n" << Time() << endl;
+	app << "#TotalTime\n" << mTotalTime << endl;
+	app << "#TimePerViewCell\n" << mTimePerViewCell << endl;;
+
+	app << "#RaysPerSec\n" << RaysPerSec() << endl;
+
+	app << "#TrianglePvs\n" << mTrianglePvs << endl;
+	app << "#TotalTrianglePvs\n" << mTotalTrianglePvs << endl;
+
+	app << "#PerViewCellPvs\n" << mPerViewCellPvs << endl;
+	app << "#TotalPvs\n" << mTotalPvs << endl;
+	
+	app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl;
 	app << "#TotalSamples\n" << mTotalSamples << endl;
-	app << "#ScDiff\n" << mPassContribution << endl;
 	app	<< "#SamplesContri\n" << mTotalContribution << endl;
-	app << "#ReverseSamples\n" << mReverseSamples << endl;
-	app << "#BorderSamples\n" << mBorderSamples << endl;		
-	app << "#GvsRuns\n" << mGvsPass << endl;
-	app << "#ViewCells\n" << mViewCells << endl;
-	app << "#TotalPvs\n" << mTotalPvs << endl;
-	app << "#PerViewCellPvs\n" << mPerViewCellPvs << endl;
-	app << "#TrianglePvs\n" << mTrianglePvs << endl;
-	app << "#RaysPerSec\n" << RaysPerSec() << endl;
-	app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl << endl;
-}
-
-
-}
+		
+	//app << "#ReverseSamples\n" << mReverseSamples << endl;
+	//app << "#BorderSamples\n" << mBorderSamples << endl;
+
+	//app << "#Pass\n" << mPass << endl;
+	//app << "#ScDiff\n" << mPassContribution << endl;
+	//app << "#GvsRuns\n" << mGvsPass << endl;	
+
+	app << endl;
+}
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2001)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2003)
@@ -748,6 +748,7 @@
 
 	Vector3 point;
-	Vector3 normal, normal2;
-	
+	Vector3 normal;
+		
+	//Vector normalObj;
 	// float r[1];
 	// sHalton.GetNext(1, r);
@@ -769,5 +770,5 @@
 
 	// get point on view cell surface
-	mViewCell->GetRandomSurfacePoint(origin, normal2);
+	mViewCell->GetRandomSurfacePoint(origin, normal);
 
 	//direction = point - origin;
@@ -775,5 +776,5 @@
 	// move a little bit back to avoid piercing through walls
 	// that bound the view cell
-	origin -= 0.01f * normal2;
+	origin -= 0.01f * normal;
 
 	// $$ jb the pdf is yet not correct for all sampling methods!
