Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1576)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1632)
@@ -22,4 +22,5 @@
 	virtual void EvalPriority() = 0;
 	virtual int Type() const = 0;
+	//virtual bool Apply() = 0;
 	virtual bool GlobalTerminationCriteriaMet() const = 0;
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1603)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1632)
@@ -241,9 +241,7 @@
 
 int VspBspTree::AddMeshToPolygons(Mesh *mesh,
-								  PolygonContainer &polys,
-								  MeshInstance *parent)
-{
-	if (!mesh)
-		return 0;
+								  PolygonContainer &polys) const
+{
+	if (!mesh) return 0;
 
 	FaceContainer::const_iterator fi;
@@ -256,5 +254,4 @@
 		if (poly->Valid(mEpsilon))
 		{
-			poly->mParent = parent; // set parent intersectable
 			polys.push_back(poly);
 		}
@@ -269,26 +266,52 @@
 
 
-int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,
-								 PolygonContainer &polys,
-								 int maxObjects)
-{
-	const int limit = (maxObjects > 0) ?
-		Min((int)viewCells.size(), maxObjects) : (int)viewCells.size();
-
-	int polysSize = 0;
-	
-	for (int i = 0; i < limit; ++ i)
-	{
-		Mesh *mesh = viewCells[i]->GetMesh();
-		if (mesh) 
-		{	// copy the mesh into polygons and add to BSP tree aabb
-			mBoundingBox.Include(viewCells[i]->GetBox()); 
-			polysSize += AddMeshToPolygons(mesh,
-										   polys,
-										   viewCells[i]);
-		}
-	}
-	
-	return polysSize;
+void VspBspTree::ExtractPolygons(Intersectable *object, PolygonContainer &polys) const
+{
+	// extract the polygons from the intersectables
+	switch (object->Type())
+	{
+		case Intersectable::MESH_INSTANCE:
+			{
+				Mesh *mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
+				AddMeshToPolygons(mesh, polys);
+			}
+			break;
+		case Intersectable::VIEW_CELL:
+			{
+				Mesh *mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
+				AddMeshToPolygons(mesh, polys);
+				break;
+			}
+		case Intersectable::TRANSFORMED_MESH_INSTANCE:
+			{
+				TransformedMeshInstance *mi = 
+					dynamic_cast<TransformedMeshInstance *>(object);
+
+				Mesh mesh;
+				mi->GetTransformedMesh(mesh);
+				AddMeshToPolygons(&mesh, polys);
+			}
+			break;
+		case Intersectable::TRIANGLE_INTERSECTABLE:
+			{
+				TriangleIntersectable *intersect = 
+					dynamic_cast<TriangleIntersectable *>(object);
+
+				Polygon3 *poly = new Polygon3(intersect->GetItem());
+
+				if (poly->Valid(mEpsilon))	
+				{
+					polys.push_back(poly);
+				}
+				else
+				{
+					delete poly;
+				}
+			}
+			break;
+		default:
+			Debug << "intersectable type not supported" << endl;
+			break;
+	}
 }
 
@@ -304,42 +327,5 @@
 	{
 		Intersectable *object = objects[i];//*it;
-		Mesh *mesh = NULL;
-
-		switch (object->Type()) // extract the meshes
-		{
-		case Intersectable::MESH_INSTANCE:
-			mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
-			AddMeshToPolygons(mesh, polys, NULL);
-			break;
-		case Intersectable::VIEW_CELL:
-			mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
-			AddMeshToPolygons(mesh, polys, NULL);
-			break;
-		case Intersectable::TRANSFORMED_MESH_INSTANCE:
-			{
-				TransformedMeshInstance *mi = 
-					dynamic_cast<TransformedMeshInstance *>(object);
-
-				Mesh mesh;
-				mi->GetTransformedMesh(mesh);
-				AddMeshToPolygons(&mesh, polys, NULL);
-                break;
-			}
-		case Intersectable::TRIANGLE_INTERSECTABLE:
-			{
-				TriangleIntersectable *intersect = 
-					dynamic_cast<TriangleIntersectable *>(object);
-
-				Polygon3 *poly = new Polygon3(intersect->GetItem());
-
-				if (poly->Valid(mEpsilon))	
-					polys.push_back(poly);
-				else
-					delete poly;
-			}
-		default:
-				Debug << "intersectable type not supported" << endl;
-			break;
-		}
+		ExtractPolygons(object, polys);
 
 		mBoundingBox.Include(object->GetBox()); // add to BSP tree aabb
@@ -404,59 +390,12 @@
 			Intersectable *obj = ray->mTerminationObject;
 
-			if ((mBoundingBox.IsInside(ray->mTermination) || !forcedBoundingBox) &&
-				obj && !obj->Mailed())
-			{
-				obj->Mail();
-
-				// handle intersectable
-				switch (obj->Type())
-				{
-					case Intersectable::TRANSFORMED_MESH_INSTANCE:
-						{
-							Mesh mesh;
-				
-							TransformedMeshInstance *tmobj = 
-								dynamic_cast<TransformedMeshInstance *>(obj);
-				
-							tmobj->GetTransformedMesh(mesh);
-							AddMeshToPolygons(&mesh, polys, tmobj);
-						}
-						break;
-
-					case Intersectable::MESH_INSTANCE:
-						{
-							MeshInstance *mobj = dynamic_cast<MeshInstance *>(obj);
-							AddMeshToPolygons(mobj->GetMesh(), polys, mobj);
-						}
-						break;
-
-					case Intersectable::TRIANGLE_INTERSECTABLE:
-						{
-							TriangleIntersectable *intersect = 
-									dynamic_cast<TriangleIntersectable *>(obj);
-
-							Polygon3 *poly = new Polygon3(intersect->GetItem());
-
-							if (poly->Valid(mEpsilon))	
-								polys.push_back(poly);
-							else
-								delete poly;
-						}
-						break;
-
-					default:
-						cout << "not implemented yet" << endl;
-						break;
-				}
-
-				++ numObj;
-
-				/////////
-				//-- compute bounding box
-
-				if (!forcedBoundingBox)
-				{
-					mBoundingBox.Include(ray->mTermination);
-				}
+			++ numObj;
+
+			/////////
+			//-- compute bounding box
+
+			if (!forcedBoundingBox)
+			{
+				mBoundingBox.Include(ray->mTermination);
 			}
 
@@ -466,7 +405,6 @@
 			{		
 				ray->mOriginObject->Mail();
-				MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mOriginObject);
-				AddMeshToPolygons(obj->GetMesh(), polys, obj);
-				
+				ExtractPolygons(ray->mOriginObject, polys);
+								
 				++ numObj;
 			}
@@ -475,5 +413,4 @@
 		// throw out unnecessary polygons
 		PreprocessPolygons(polys);
-
 		cout << "finished" << endl;
 
@@ -1021,4 +958,5 @@
 	//////////////////
 	//-- terminate traversal and create new view cell
+
 	if (newNode->IsLeaf())
 	{
@@ -1035,8 +973,10 @@
 		// update scalar pvs size value
 		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
+		mViewCellsManager->UpdateScalarPvsSize(viewCell, 
+											   pvs.CountObjectsInPvs(), 
+											   pvs.GetSize());
 
 		mBspStats.contributingSamples += conSamp;
-		mBspStats.sampleContributions +=(int) sampCon;
+		mBspStats.sampleContributions += (int)sampCon;
 
 		viewCell->mLeaves.push_back(leaf);
@@ -1044,4 +984,5 @@
 		///////////
 		//-- store additional info
+
 		if (mStoreRays)
 		{
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1564)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 1632)
@@ -724,6 +724,6 @@
 
 	// extract planes of box
-	// TODO: can be done more elegantly than first extracting polygons
-	// and take their planes
+	// TODO: can be done more elegantly than here 
+	// where we first extract polygons, then compute their planes
 	for (pit = boxPolys.begin(); pit != pit_end; ++ pit)
 	{
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1627)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1632)
@@ -38,5 +38,5 @@
 // HACK
 const static bool SAMPLE_AFTER_SUBDIVISION = true;
-const static bool CLAMP_TO_BOX = true;
+const static bool CLAMP_TO_BOX = false;
 
 template <typename T> class myless
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1563)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 1632)
@@ -538,19 +538,11 @@
 						 int maxObjects = 0);
 
-	/** Extracts the meshes of the view cells and and adds them to polygons.
-		Adds view cell aabb to the aabb of the tree.
-		@param maxPolys the maximal number of objects to be stored as polygons
-		@returns the number of polygons
-	*/
-	int AddToPolygonSoup(const ViewCellContainer &viewCells, 
-						 PolygonContainer &polys, 
-						 int maxObjects = 0);
-
-	/** Extract polygons of this mesh and add to polygon container.
+	void ExtractPolygons(Intersectable *obj, PolygonContainer &polys) const;
+
+	/** Extract polygons of this mesh and adds them to container.
 		@param mesh the mesh that drives the polygon construction
-		@param parent the parent intersectable this polygon is constructed from
 		@returns number of polygons
 	*/
-	int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
+	int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys) const;
 
 	/** Selects an axis aligned for the next split.
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1614)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1632)
@@ -408,4 +408,11 @@
 		}
 
+		/*bool Apply(SplitQueue *queue, bool terminationCriteriaMet)
+		{
+			BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
+			// local or global termination criteria failed
+			return !n->IsLeaf();		
+		}*/
+
 		bool GlobalTerminationCriteriaMet() const
 		{
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1581)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1632)
@@ -2178,6 +2178,6 @@
   
   
-  int nrays = leaf->rays.size();
-  int startIndex = rays.size();
+  int nrays = (int)leaf->rays.size();
+  int startIndex = (int)rays.size();
   
   AxisAlignedBox3 box = GetBBox(leaf);
@@ -2451,5 +2451,5 @@
 	if (leaf->rays[i].mRay->RefCount() != 0) {
 	  cerr<<"Error: refcount!=0, but"<<leaf->rays[i].mRay->RefCount()<<endl;
-	  cerr<<"desired="<<desired<<"i="<<i<<" size="<<leaf->rays.size()<<endl;
+	  cerr<<"desired="<<desired<<"i="<<i<<" size="<<(int)leaf->rays.size()<<endl;
 	  exit(1);
 	}
@@ -2577,5 +2577,5 @@
   int fixedPerLeaf = 0;
   // int fixedPerLeaf = 0;
-  int fixed = fixedPerLeaf*leaves.size();
+  int fixed = fixedPerLeaf*(int)leaves.size();
   int iGenerated = numberOfRays;
   float ratioPerLeaf = iGenerated /(avgContrib*numberOfLeaves);
@@ -2592,5 +2592,5 @@
 	}
 
-  return rays.size();
+  return (int)rays.size();
 }
 
@@ -2933,5 +2933,5 @@
 	int id = object->GetId()-1;
 	if (id < 0 || id >= mRoots.size()) {
-	  Debug<<"Error: object Id out of range, Id="<<id<<" roots.size()="<<mRoots.size()<<
+	  Debug<<"Error: object Id out of range, Id="<<id<<" roots.size()="<<(int)mRoots.size()<<
 		endl<<flush;
 	  id = (int)mRoots.size()-1; // $$ last tree is used by all unsigned objects
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1627)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1632)
@@ -119,8 +119,27 @@
 	Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
 	Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
-	Debug << "construction type: " << mConstructionType << endl;
 	Debug << "min depth for object space subdivision: " << mMinDepthForObjectSpaceSubdivion << endl;
 	Debug << "repair queue: " << mRepairQueue << endl;
 	Debug << "number of multilevels: " << mNumMultiLevels << endl;
+
+	switch (mConstructionType)
+	{
+	case 0:
+		Debug << "construction type: sequential" << endl;
+		break;
+	case 1:
+		Debug << "construction type: interleaved" << endl;
+		break;
+	case 2:
+		Debug << "construction type: gradient" << endl;
+		break;
+	case 3:
+		Debug << "construction type: multilevel" << endl;
+		break;
+	default:
+		Debug << "construction type " << mConstructionType << " unknown" << endl;
+		break;
+	}
+
 	//Debug << "min render cost " << mMinRenderCostDecrease << endl;
 	Debug << endl;
@@ -308,5 +327,6 @@
 	SplitQueue viewSpaceQueue;
 
-	int maxSteps = 200; // number of initial splits;
+	// number of initial splits
+	int maxSteps = 200;
 	float renderCostDecr = 0;
 
@@ -322,13 +342,14 @@
 		// for first round, use sah splits. Once view space partition
 		// has started, use render cost heuristics instead
-		SubdivisionCandidate *osc = ResetObjectSpaceSubdivision(sampleRays, objects);
+		SubdivisionCandidate *osc = 
+			ResetObjectSpaceSubdivision(sampleRays, objects);
 		objectSpaceQueue.Push(osc);
 
-		// process object space candidates using 
-		// render queue for objects until slope of previous subdivision 
-		// is reached
+		// process object space candidates using render queue for 
+		// objects until slope of previous subdivision is reached
 		SubdivisionCandidateContainer ospContainer;
 
-		const int ospSteps = RunConstruction(objectSpaceQueue, ospContainer, renderCostDecr, maxSteps);
+		const int ospSteps = 
+			RunConstruction(objectSpaceQueue, ospContainer, renderCostDecr, maxSteps);
 	    cout << ospSteps << " object space partition steps taken" << endl;
 
@@ -345,5 +366,5 @@
 
 		CollectDirtyCandidates(ospContainer, dirtyList);
-		RepairQueue(dirtyList, viewSpaceQueue);
+		//RepairQueue(dirtyList, viewSpaceQueue);
 
 		cout << "finished repairing queue ... " << endl;
@@ -354,12 +375,14 @@
 		// subdivide view space with respect to the objects
 
-		SubdivisionCandidate *vsc = ResetViewSpaceSubdivision(sampleRays, objects);
+		SubdivisionCandidate *vsc = 
+			ResetViewSpaceSubdivision(sampleRays, objects);
 		viewSpaceQueue.Push(vsc);
 
 		SubdivisionCandidateContainer vspContainer;
 		// process view space candidates
-		const int vspSteps = RunConstruction(viewSpaceQueue, vspContainer, renderCostDecr, maxSteps);
-
-		cout << vspSteps << " object space partition steps taken" << endl;
+		const int vspSteps = 
+			RunConstruction(viewSpaceQueue, vspContainer, renderCostDecr, maxSteps);
+
+		cout << vspSteps << " view space partition steps taken" << endl;
 
 		// view space subdivision constructed
@@ -373,5 +396,5 @@
 		
 		CollectDirtyCandidates(vspContainer, dirtyList);
-		RepairQueue(dirtyList, objectSpaceQueue);
+		//RepairQueue(dirtyList, objectSpaceQueue);
 
 		cout << "finished repairing queue ... " << endl;
@@ -417,10 +440,12 @@
 	mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
 
-	// start with view space subdivison immediately?
+	// start view space subdivison immediately?
 	if (StartViewSpaceSubdivision())
 	{
 		// prepare vsp tree for traversal
 		mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
-		PrepareViewSpaceSubdivision(sampleRays, objects);
+		SubdivisionCandidate *vspSc = 
+			PrepareViewSpaceSubdivision(sampleRays, objects);
+		mTQueue.Push(vspSc);
 	}
 	
@@ -429,7 +454,9 @@
 	{
 		mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
-		PrepareObjectSpaceSubdivision(sampleRays, objects);
-	}
-cout << "****************555555555555555555555555" << endl;
+		SubdivisionCandidate *ospSc =
+			PrepareObjectSpaceSubdivision(sampleRays, objects);
+		mTQueue.Push(ospSc);
+	}
+
 	// begin subdivision
 	RunConstruction(mRepairQueue, sampleRays, objects, forcedViewSpace);
@@ -449,5 +476,5 @@
 																	const ObjectContainer &objects)
 {
-	cout << "\nstarting view space hierarchy construction ... " << endl;
+	cout << "\npreparing view space hierarchy construction ... " << endl;
 
 	// hack: reset global cost misses
@@ -541,5 +568,6 @@
 		{
 			VspNode *n = mVspTree->Subdivide(splitQueue, sc, terminationCriteriaMet);
-			// check if termination criteria failed
+
+			// check if terminated
 			success = !n->IsLeaf();
 		}
@@ -564,4 +592,5 @@
 		break;
 	default:
+		cerr << "unknown subdivision type" << endl;
 		break;
 	}
@@ -682,5 +711,5 @@
 	{
 		SubdivisionCandidate *sc = NextSubdivisionCandidate(mTQueue);    
-			
+	
 		///////////////////
 		//-- subdivide leaf node
@@ -698,5 +727,6 @@
 				 << mMinDepthForObjectSpaceSubdivion << ") " << endl;
 
-			PrepareObjectSpaceSubdivision(sampleRays, objects);
+			SubdivisionCandidate *ospSc = PrepareObjectSpaceSubdivision(sampleRays, objects);
+			mTQueue.Push(ospSc);
 
 			cout << "reseting queue ... ";
@@ -713,5 +743,6 @@
 				 << mMinDepthForViewSpaceSubdivion << ") " << endl;
 
-			PrepareViewSpaceSubdivision(sampleRays, objects);
+			SubdivisionCandidate *vspSc = PrepareViewSpaceSubdivision(sampleRays, objects);
+			mTQueue.Push(vspSc);
 
 			cout << "reseting queue ... ";
@@ -763,7 +794,10 @@
 
 		const bool repairQueue = false;
-		ApplySubdivisionCandidate(sc, splitQueue, repairQueue);
-
-		chosenCandidates.push_back(sc);
+		bool success = ApplySubdivisionCandidate(sc, splitQueue, repairQueue);
+
+		if (success)
+		{
+			chosenCandidates.push_back(sc);
+		}
 	}
 
@@ -781,5 +815,5 @@
 	}
 
-	SubdivisionCandidate *firstCandidate = NULL;
+	SubdivisionCandidate *firstCandidate;
 
 	// object space partition constructed => reconstruct
@@ -791,7 +825,6 @@
 			Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
 	
-			SubdivisionCandidate *firstCandidate = 
-				mBvHierarchy->Reset(sampleRays, objects);
-
+			firstCandidate = mBvHierarchy->Reset(sampleRays, objects);
+			cout << "here4" << endl;
 			mHierarchyStats.mTotalCost = mBvHierarchy->mTotalCost;
 
@@ -802,4 +835,5 @@
 			// evaluate stats before first subdivision
 			EvalSubdivisionStats();
+			cout << "here5" << endl;
 		}
 		break;
@@ -807,9 +841,9 @@
 	case KD_BASED_OBJ_SUBDIV:
 		// TODO
-		break;
 	default:
-		break;
-	}
-
+		firstCandidate = NULL;
+		break;
+	}
+cout << "here6" << endl;
 	return firstCandidate;
 }
