Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1288)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1293)
@@ -2450,4 +2450,10 @@
 					"1000");
 	
+	RegisterOption("Hierarchy.Construction.type",
+					optInt,
+					"hierarchy_construction_type=",
+					"0");
+
+
 	//////////////////////////////////////////////////////////////////////////////////
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1292)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1293)
@@ -483,5 +483,5 @@
 		Debug << "hierarchy manager: osp" << endl;
 		// HACK for testing if per kd evaluation works!!
-		const bool ishack = true;
+		const bool ishack = false;
 		if (ishack)
 			hierarchyManager = new HierarchyManager(mVspTree, mKdTree);
@@ -852,5 +852,5 @@
 	VssRay *vssRay  = NULL;
 	int hits = 0;
-
+	int hittriangle;
 	Vector3 pointA, pointB;
 	Vector3 normalA, normalB;
@@ -858,13 +858,16 @@
 	float dist;
 	
-	int hittriangle;
 	double normal[3];
+#ifdef GTP_INTERNAL
 	hittriangle = mlrtaIntersectAS(&viewPoint.x,
 								   &direction.x,
 								   normal,
 								   dist);
+#else
+	hittriangle = -1;
+#endif
 	if (hittriangle !=-1 ) {
 	  if (hittriangle >= mFaceParents.size())
-		cerr<<"Warning: traingle index out of range! "<<hittriangle<<endl;
+		cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
 	  else {
 		objectA = mFaceParents[hittriangle].mObject;
@@ -879,8 +882,13 @@
 
 	Vector3 dir = -direction;
+#ifdef GTP_INTERNAL
 	hittriangle = mlrtaIntersectAS(&viewPoint.x,
 								   &dir.x,
 								   normal,
 								   dist);
+#else
+	hittriangle = -1;
+#endif
+
 	if (hittriangle !=-1 ) {
 	  if (hittriangle >= mFaceParents.size())
@@ -1325,8 +1333,15 @@
 	  int hittriangle;
 	  double n[3];
+
+#ifdef GTP_INTERNAL
 	  hittriangle = mlrtaIntersectAS(&viewPoint.x,
 									 &direction.x,
 									 n,
 									 dist);
+
+#else
+	hittriangle = -1;
+#endif
+
 	  if (hittriangle !=-1 ) {
 		if (hittriangle >= mFaceParents.size())
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1288)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1293)
@@ -150,4 +150,9 @@
 	~HierarchyManager();
 
+	void Construct(
+		const VssRayContainer &sampleRays,
+		const ObjectContainer &objects,
+		AxisAlignedBox3 *forcedViewSpace);
+
 	/** Constructs the view space and object space subdivision from a given set of rays
 		and a set of objects.
@@ -155,19 +160,15 @@
 		@param objects the set of objects
 	*/
-	void Construct(const VssRayContainer &sampleRays,
-				   const ObjectContainer &objects,
-				   AxisAlignedBox3 *forcedViewSpace);
-
-	/** Constructs first view cells, then object space partition.
-	*/
-	void Construct2(const VssRayContainer &sampleRays,
-					const ObjectContainer &objects,
-					AxisAlignedBox3 *forcedViewSpace);
-
-	/** Constructs only vsp tree.
-	*/
-	void Construct3(const VssRayContainer &sampleRays,
-					const ObjectContainer &objects,
-					AxisAlignedBox3 *forcedViewSpace);
+	void ConstructInterleaved(
+		const VssRayContainer &sampleRays,
+		const ObjectContainer &objects,
+		AxisAlignedBox3 *forcedViewSpace);
+
+	/** Constructs first view space hierarchy, then object space hierarchy.
+	*/
+	void ConstructSequential(
+		const VssRayContainer &sampleRays,
+		const ObjectContainer &objects,
+		AxisAlignedBox3 *forcedViewSpace);
 
 	enum 
@@ -266,4 +267,5 @@
 protected:
 
+	int mConstructionType;
 	int mObjectSpaceSubdivisonType;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1291)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1293)
@@ -639,5 +639,5 @@
 
 	Debug << "\n§§§§ eval local cost §§§§" << endl
-		  << "back pvs: " << (int)objectsBack.size() << " front pvs: " << objectsFront.size() << " total pvs: " << nTotalObjects << endl 
+		  << "back pvs: " << (int)objectsBack.size() << " front pvs: " << (int)objectsFront.size() << " total pvs: " << nTotalObjects << endl 
 		  << "back p: " << volBack / viewSpaceVol << " front p " << volFront / viewSpaceVol << " p: " << totalVol / viewSpaceVol << endl
 		  << "old rc: " << oldRenderCost / viewSpaceVol << " new rc: " << newRenderCost / viewSpaceVol << endl
@@ -814,5 +814,5 @@
 		{
 			if (mUseCostHeuristics)
-			{cout << "a";
+			{
 				//-- partition objects using heuristics
 				nCostRatio[axis] =
@@ -824,5 +824,5 @@
 			}
 			else
-			{cout << "b";
+			{
 				nCostRatio[axis] =
 					EvalLocalObjectPartition(
@@ -849,5 +849,5 @@
 	backObjects = nBackObjects[bestAxis];
 
-	Debug << "val: " << nCostRatio[bestAxis] << " axis: " << bestAxis << endl;
+	//Debug << "val: " << nCostRatio[bestAxis] << " axis: " << bestAxis << endl;
 	return nCostRatio[bestAxis];
 }
@@ -1107,5 +1107,8 @@
 {
 	// rather use the simple version
-	return object->mBvhLeaf;
+	if (object->mBvhLeaf)
+	{
+		return object->mBvhLeaf;
+	}
 
 	///////////////////////////////////////
@@ -1133,5 +1136,7 @@
 
 			if (IsObjectInLeaf(leaf, object))
+			{
 				return leaf;
+			}
 		} 
 		else 	
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1291)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1293)
@@ -4805,6 +4805,5 @@
 	Debug << "saved rays: " << (int)savedRays.size() << endl;
 
-	//mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox);
-	mHierarchyManager->Construct2(constructionRays, objects, &mViewSpaceBox);
+	mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox);
 
 	VssRayContainer::const_iterator vit, vit_end = constructionRays.end();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1287)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1293)
@@ -117,6 +117,5 @@
 	white.mDiffuseColor.g = 1;
 	white.mDiffuseColor.b = 1;
-	int objSize = 0;
-
+	
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
@@ -146,5 +145,4 @@
 			SetFilled();
 			ExportGeometry(leaf->mObjects);
-			objSize += leaf->mObjects.size();
 		}
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1292)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1293)
@@ -2628,4 +2628,5 @@
 	Debug <<  "rays size: " << (int)rays.size() << endl;
 
+
 	//-- prepare view space partition
 
@@ -2930,5 +2931,5 @@
 			BvhLeaf *bvhleaf = mHierarchyManager->mBvHierarchy->GetLeaf(obj);
 
-			if (!bvhleaf->Mailed())
+			if (bvhleaf && !bvhleaf->Mailed())
 			{
 				bvhleaf->Mail();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1281)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1293)
@@ -66,5 +66,5 @@
 		};
   
-  Intersectable(): mMailbox(0), mReferences(0) {}
+  Intersectable(): mMailbox(0), mReferences(0), mBvhLeaf(0) {}
 
 	void SetId(const int id) { mId = id; }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1291)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1293)
@@ -154,15 +154,13 @@
 					KdIntersectable *kdObj = dynamic_cast<KdIntersectable *>(obj);
 					pvs += CountNewObjectsInKdNode(kdObj);	
-					cout << "z";
 					break;
 				}
 			case Intersectable::BVH_INTERSECTABLE:
 				{
-					cout << "x";
 					BvhIntersectable *bvhObj = dynamic_cast<BvhIntersectable *>(obj);
 					pvs += CountNewObjectsInBvhNode(bvhObj);
 					break;
 				}
-			default:cout << "y";
+			default:
 				++ pvs;
 				break;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1291)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1293)
@@ -96,4 +96,7 @@
 		"Hierarchy.Termination.globalCostMissTolerance", mTermGlobalCostMissTolerance);
 
+	Environment::GetSingleton()->GetIntValue(
+		"Hierarchy.Construction.type", mConstructionType);
+
 	//Debug << "max depth: " << mTermMaxDepth << endl;
 	Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
@@ -144,10 +147,22 @@
 	SubdivisionCandidate *vsc = 
 		mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, viewSpaceRays);
+
 	mTQueue.Push(vsc);
 
-	SubdivisionCandidate *osc = 
-		mOspTree->PrepareConstruction(sampleRays, objects, objectSpaceRays);
-
-	mTQueue.Push(osc);
+    SubdivisionCandidate *osc;
+
+	switch (mObjectSpaceSubdivisonType)
+	{
+	case KD_BASED_OBJ_SUBDIV:
+		osc = mOspTree->PrepareConstruction(sampleRays, objects, objectSpaceRays);
+		mTQueue.Push(osc);
+		break;
+	case BV_BASED_OBJ_SUBDIV:
+		osc = mBvHierarchy->PrepareConstruction(sampleRays, objects);
+		mTQueue.Push(osc);
+		break;
+	default:
+		break;
+	}
 }
 
@@ -204,6 +219,24 @@
 
 void HierarchyManager::Construct(const VssRayContainer &sampleRays,
-								 const ObjectContainer &objects,
-								 AxisAlignedBox3 *forcedViewSpace)
+								const ObjectContainer &objects,
+								AxisAlignedBox3 *forcedViewSpace)
+{
+	switch (mConstructionType)
+	{
+	case 0: 
+		ConstructSequential(sampleRays, objects, forcedViewSpace);
+		break;
+	case 1:
+		ConstructInterleaved(sampleRays, objects, forcedViewSpace);
+		break;
+	default:
+		break;
+	}
+}
+
+
+void HierarchyManager::ConstructInterleaved(const VssRayContainer &sampleRays,
+											const ObjectContainer &objects,
+											AxisAlignedBox3 *forcedViewSpace)
 {
 	RayInfoContainer *objectSpaceRays = new RayInfoContainer();
@@ -211,9 +244,10 @@
 
 	// prepare vsp and osp trees for traversal
-	PrepareConstruction(sampleRays, objects, forcedViewSpace, *viewSpaceRays, *objectSpaceRays);
-
-	mVspTree->mVspStats.Reset();
-	mVspTree->mVspStats.Start();
-	
+	PrepareConstruction(sampleRays, 
+						objects, 
+						forcedViewSpace, 
+						*viewSpaceRays, 
+						*objectSpaceRays);
+
 	mHierarchyStats.Reset();
 	mHierarchyStats.Start();
@@ -232,6 +266,5 @@
 bool HierarchyManager::ApplySubdivisionCandidate(SubdivisionCandidate *sc)
 {
-	const bool globalTerminationCriteriaMet = 
-			GlobalTerminationCriteriaMet(sc);
+	const bool globalTerminationCriteriaMet = GlobalTerminationCriteriaMet(sc);
 
 	const bool vspSplit = (sc->Type() == SubdivisionCandidate::VIEW_SPACE);
@@ -297,5 +330,6 @@
 			if (repair) RepairQueue();
 
-			cout << "candidate: " << splitCandidate->Type() << ", priority: " << splitCandidate->GetPriority() << endl;
+			cout << "candidate: " << splitCandidate->Type() << ", priority: " 
+				 << splitCandidate->GetPriority() << endl;
 		}
 
@@ -305,32 +339,4 @@
 
 
-void HierarchyManager::Construct3(const VssRayContainer &sampleRays,
-								  const ObjectContainer &objects,
-								  AxisAlignedBox3 *forcedViewSpace)
-{
-	// construct only view space partition
-	// object kd tree is taken for osp
-
-	mVspTree->mVspStats.Reset();
-	mVspTree->mVspStats.Start();
-
-	RayInfoContainer *viewSpaceRays = new RayInfoContainer();
-	
-	SubdivisionCandidate *sc = 
-		mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, *viewSpaceRays);
-
-	mTQueue.Push(sc);
-
-	cout << "starting vsp construction ... " << endl;
-
-	long startTime = GetTime();
-
-	RunConstruction(false);
-
-	cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl;
-	mVspTree->mVspStats.Stop();
-}
-
-
 bool HierarchyManager::FinishedConstruction() const
 {
@@ -339,5 +345,6 @@
 
 
-void HierarchyManager::CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
+void HierarchyManager::CollectObjectSpaceDirtyList(
+	SubdivisionCandidateContainer &dirtyList)
 {
 	switch (mObjectSpaceSubdivisonType)
@@ -535,13 +542,11 @@
 			
 	exporter->SetWireframe();
-	const int maxPvs = 0;//mOspTree.GetStatistics().maxPvs;
-
 	exporter->ExportOspTree(*mOspTree, 0);
 }
 
 
-void HierarchyManager::Construct2(const VssRayContainer &sampleRays,
-								  const ObjectContainer &objects,
-								  AxisAlignedBox3 *forcedViewSpace)
+void HierarchyManager::ConstructSequential(const VssRayContainer &sampleRays,
+										   const ObjectContainer &objects,
+										   AxisAlignedBox3 *forcedViewSpace)
 {
 	// rays clipped in view space and in object space
