Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1284)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1286)
@@ -89,5 +89,6 @@
   , mCurrentBspNode(NULL)
   , mCurrentVspNode(NULL)
-  , mCurrentKdNode(NULL)
+  , mCurrentOspNode(NULL)
+  , mCurrentBvhNode(NULL)
   , mObjects(objects)
   , mBoundingBoxConverter(bconverter)
@@ -114,24 +115,85 @@
 
 	if (element == "BoundingBoxes")
+	{
 		EndBoundingBoxes();
+	}
 
 	if (element == "ViewCells")
+	{	
 		EndViewCells();
-
-	// inside the view cell description
-	if (mCurrentState == PARSE_VIEWCELLS)
-	{
-		if (element == "Interior")
-			EndViewCellInterior();
-	}
-	else
-	{
-		if (element == "Interior")
-			EndBspInterior();
+	}
+
+	if (element == "ObjectSpaceHierarchy")
+	{
+		EndObjectSpaceHierarchy();
 	}
 
 	// finished, create view cells manager
 	if (element == "VisibilitySolution")
+	{
 		CreateViewCellsManager();
+	}
+
+	if (element == "Interior")
+	{
+		switch (mCurrentState)
+		{
+		case PARSE_VIEWCELLS:
+			EndViewCellInterior();
+			break;
+		case PARSE_OBJECTSPACE_HIERARCHY:
+			EndObjectSpaceHierarchyInterior();
+			break;
+		case PARSE_VIEWSPACE_HIERARCHY:	
+			EndViewSpaceHierarchyInterior();
+			break;
+		default:
+			break;
+		}
+	}
+}
+
+
+void ViewCellsParseHandlers::EndObjectSpaceHierarchy()
+{
+	if (mObjectSpaceHierarchyType == OSP)
+	{
+		mHierarchyManager->mOspTree->InsertObjects(
+			mHierarchyManager->mOspTree->mRoot, *mObjects);
+	}
+}
+
+
+void ViewCellsParseHandlers::EndViewSpaceHierarchyInterior()
+{
+	switch (mViewSpaceHierarchyType)
+	{
+	case BSP:
+		EndBspInterior();
+		break;
+	case VSP:
+		EndVspInterior();
+		break;	
+	default:
+		Debug << "not implemented view space hierarchy type " << mViewSpaceHierarchyType << endl;
+		break;
+	}
+}
+
+
+void ViewCellsParseHandlers::EndObjectSpaceHierarchyInterior()
+{
+	switch (mObjectSpaceHierarchyType)
+	{
+	case OSP:
+		EndOspInterior();
+		break;
+	case BVH:
+		EndBvhInterior();
+		break;	
+	default:
+		Debug << "not implemented object space hierarchy type " << mViewSpaceHierarchyType << endl;
+		break;
+	}
 }
 
@@ -143,4 +205,24 @@
 	{	cout << "]";
 		mCurrentBspNode = mCurrentBspNode->GetParent();
+	}
+}
+
+
+void ViewCellsParseHandlers::EndBvhInterior()
+{
+	// go one up in the tree
+	if (mCurrentBvhNode->GetParent())
+	{	cout << "]";
+		mCurrentBvhNode = mCurrentBvhNode->GetParent();
+	}
+}
+
+
+void ViewCellsParseHandlers::EndOspInterior()
+{
+	// go one up in the tree
+	if (mCurrentOspNode->mParent)
+	{	cout << "]";
+		mCurrentOspNode = mCurrentOspNode->mParent;
 	}
 }
@@ -243,5 +325,4 @@
 		StartVspInterior(attributes);
 	}
-
 	if (element == "Leaf") 
 	{
@@ -289,19 +370,16 @@
 															AttributeList& attributes)
 {
-	//if (!mViewCellsManager)return;
-
 	//-- use cell type according to the chosen method
-
 	switch (mViewSpaceHierarchyType)
 	{
-		case BSP:
-			StartBspElement(element, attributes);
-			break;
-		case VSP:
-			StartVspElement(element, attributes);
-			break;
-		default:
-			Debug << "not implemented" << endl;
-			break;
+	case BSP:
+		StartBspElement(element, attributes);
+		break;
+	case VSP:
+		StartVspElement(element, attributes);
+		break;
+	default:
+		Debug << "not implemented" << endl;
+		break;
 	}
 }
@@ -412,5 +490,5 @@
 
 
-void ViewCellsParseHandlers::ParseViewCellPvs(ViewCell *viewCell, 
+void ViewCellsParseHandlers::StartViewCellPvs(ViewCell *viewCell, 
 											  AttributeList&  attributes)
 {
@@ -433,8 +511,7 @@
 			while (1)
 			{
-				int index = strtol(ptr, &endptr, 10);
-
-				if (ptr == endptr)
-					break;
+				const int index = strtol(ptr, &endptr, 10);
+
+				if (ptr == endptr) break;
 
 				objIndices.push_back(index);
@@ -454,5 +531,7 @@
 
 				ObjectContainer::iterator oit =
-					lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);	
+					lower_bound(mObjects->begin(), 
+								mObjects->end(), 
+								(Intersectable *)&dummyInst, ilt);	
 							
 				if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
@@ -551,8 +630,7 @@
 void ViewCellsParseHandlers::StartObjectSpaceHierarchy(AttributeList& attributes)
 {
-	int len = attributes.getLength();
-
+	const int len = attributes.getLength();
 	Vector3 bmin, bmax;
-cout << "here3" << endl;
+
 	for (int i = 0; i < len; ++ i) 
 	{
@@ -567,10 +645,26 @@
 			{
 				cout << "\nobject space hierarchy: Osp" << endl;
+
 				mHierarchyManager = 
 					new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV);
+
+				mObjectSpaceHierarchyType = OSP;
+
+				//std::stable_sort(objects.begin(), objects.end(), ilt);
+				mHierarchyManager->mOspTree->mBoundingBox.Initialize();
+				ObjectContainer::const_iterator oit, oit_end = mObjects->end();
+				
+				//-- compute bounding box
+				for (oit = mObjects->begin(); oit != oit_end; ++ oit)
+				{
+					Intersectable *obj = *oit;
+					// compute bounding box of view space
+					mHierarchyManager->mOspTree->mBoundingBox.Include(obj->GetBox());
+				}
 			}
 			else if (strcmp(ptr, "bvh") == 0)
 			{
 				cout << "\nobject space hierarchy: Bvh" << endl;
+				mObjectSpaceHierarchyType = BVH;
 				mHierarchyManager = 
 					new HierarchyManager(mVspTree, HierarchyManager::BV_BASED_OBJ_SUBDIV);
@@ -579,5 +673,4 @@
 	}
 }
-
 
 
@@ -675,10 +768,7 @@
 	{
 		// add to invalid view space
-		if (mViewSpaceHierarchyType== ViewCellsManager::VSP_BSP)
-		{
-			leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell());
-			leaf->SetTreeValid(false);
-			mVspBspTree->PropagateUpValidity(leaf);
-		}
+		leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell());
+		leaf->SetTreeValid(false);
+		mVspBspTree->PropagateUpValidity(leaf);
 	}
 }
@@ -736,5 +826,5 @@
 	}
 
-	ParseViewCellPvs(viewCell, attributes);
+	StartViewCellPvs(viewCell, attributes);
 
 	// collect leaves
@@ -746,5 +836,5 @@
 {
 	ViewCellInterior* interior = new ViewCellInterior();
-	cout << "here7"<<endl;
+	
 	if (mCurrentViewCell) // replace NULL child of parent with current node
 	{
@@ -760,5 +850,5 @@
 	mCurrentViewCell = interior;
 
-	ParseViewCellPvs(interior, attributes);
+	StartViewCellPvs(interior, attributes);
 }
 
@@ -767,10 +857,10 @@
 {
 	ViewCellContainer::iterator it, it_end = mViewCells.end();
-	Debug << endl;
+	cout << "\n====================" << endl << endl;
 	for (it = mViewCells.begin(); it != it_end; ++ it)
 	{
-		Debug << (*it)->GetId() << " ";
-	}
-Debug << endl;
+		cout << (*it)->GetId() << " ";
+	}
+cout << endl;
 	if (mViewSpaceHierarchyType == BSP)
 	{
@@ -799,6 +889,5 @@
 			else
 			{
-                vc->GetParent()->SetupChildLink(bspVc);
-				vc->GetParent()->RemoveChildLink(vc);
+                vc->GetParent()->ReplaceChildLink(vc, bspVc);
 			}
 
@@ -806,5 +895,4 @@
 			(*vit) = bspVc;
 		}
-
 	}
 	else if (mViewSpaceHierarchyType == VSP) 
@@ -834,6 +922,5 @@
 			else
 			{
-                vc->GetParent()->SetupChildLink(vspVc);
-				vc->GetParent()->RemoveChildLink(vc);
+				vc->GetParent()->ReplaceChildLink(vc, vspVc);
 			}
 			
@@ -849,11 +936,11 @@
 		}
 	}
-	Debug << "************************" << endl;
+	cout << "************************" << endl;
 	it_end = mViewCells.end();
 	for (it = mViewCells.begin(); it != it_end; ++ it)
 	{
-		Debug << (*it)->GetId() << " ";
-	}
-	Debug << endl;
+		cout << (*it)->GetId() << " ";
+	}
+	cout << endl;
 	cout << "\nview space box: " << mViewSpaceBox << endl;
 }
@@ -862,9 +949,8 @@
 void ViewCellsParseHandlers::CreateViewCellsManager(/*const char *name*/)
 {
-	if (mViewSpaceHierarchyType == BSP) // 
+	if (mViewSpaceHierarchyType == BSP)
 	{
 		Debug << "creating view cells manager: VspBsp" << endl;
 
-		//mVspBspTree = new VspBspTree();
 		mViewCellsManager = new VspBspViewCellsManager(mViewCellsTree, mVspBspTree);
 	}
@@ -950,7 +1036,7 @@
 		if (vit == mViewCells.end())
 			cout << "error: view cell " << viewCellId << " not found" << endl;
-
+	
 		VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit);
-	
+		
 		if (viewCell->GetId() == viewCellId)
 		{
@@ -964,5 +1050,5 @@
 	}
 	else
-	{
+	{	
 		// add to invalid view space
 		leaf->SetViewCell(mVspTree->GetOrCreateOutOfBoundsCell());
@@ -986,5 +1072,5 @@
 		if (attrName == "plane") 
 		{
-			sscanf(ptr, "%d %f", &plane.mAxis, &plane.mPosition);
+			sscanf(ptr, "%f %d", &plane.mPosition, &plane.mAxis);
 		}
 	}
@@ -994,12 +1080,21 @@
 	if (mCurrentVspNode) // replace NULL child of parent with current node
 	{
-		VspInterior *current = dynamic_cast<VspInterior *>(mCurrentVspNode);
-
-		current->ReplaceChildLink(NULL, interior);
-		interior->SetParent(current);
+		VspInterior *parent = dynamic_cast<VspInterior *>(mCurrentVspNode);
+
+		parent->ReplaceChildLink(NULL, interior);
+		interior->SetParent(parent);
+		
+		AxisAlignedBox3 frontBox, backBox;
+
+		parent->GetBoundingBox().Split(parent->GetPlane().mAxis, parent->GetPlane().mPosition, frontBox, backBox);
+		if (parent->GetFront() == interior)
+			interior->SetBoundingBox(frontBox);
+		else
+			interior->SetBoundingBox(backBox);
 	}
 	else
 	{
 		mVspTree->mRoot = interior;
+		interior->SetBoundingBox(mVspTree->GetBoundingBox());
 	}
 
@@ -1021,6 +1116,5 @@
 		if (attrName == "plane") 
 		{
-			sscanf(ptr, "%d %f",
-				   &plane.mAxis, &plane.mPosition);
+			sscanf(ptr, "%f %d", &plane.mPosition, &plane.mAxis);
 		}
 	}
@@ -1031,16 +1125,25 @@
 	interior->mPosition = plane.mPosition;
 
-	if (mCurrentKdNode) // replace NULL child of parent with current node
-	{
-		KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode);
+	if (mCurrentOspNode) // replace NULL child of parent with current node
+	{
+		KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentOspNode);
 		parent->ReplaceChildLink(NULL, interior);
 		interior->mParent = parent;
+
+		AxisAlignedBox3 frontBox, backBox;
+
+		parent->mBox.Split(parent->mAxis, parent->mPosition, frontBox, backBox);
+		if (parent->mFront == interior)
+			interior->mBox = frontBox;
+		else
+			interior->mBox = backBox;
 	}
 	else
 	{
 		mHierarchyManager->mOspTree->mRoot = interior;
-	}
-
-	mCurrentKdNode = interior;
+		interior->mBox = mHierarchyManager->mOspTree->mBoundingBox;
+	}
+
+	mCurrentOspNode = interior;
 }
 
@@ -1049,9 +1152,9 @@
 {
 	KdLeaf * leaf = 
-		new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL);
-
-	if (mCurrentKdNode) // replace front or (if not NULL) back child
-	{
-		dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf);
+		new KdLeaf(dynamic_cast<KdInterior *>(mCurrentOspNode), NULL);
+
+	if (mCurrentOspNode) // replace front or (if not NULL) back child
+	{
+		dynamic_cast<KdInterior *>(mCurrentOspNode)->ReplaceChildLink(NULL, leaf);
 	}
 	else
@@ -1063,23 +1166,9 @@
 
 void ViewCellsParseHandlers::StartBvhLeaf(AttributeList& attributes)
-{/*
-	KdLeaf * leaf = 
-		new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL);
-
-	if (mCurrentKdNode) // replace front or (if not NULL) back child
-	{
-		dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf);
-	}
-	else
-	{
-		mHierarchyManager->mBvHierarchy->mRoot = leaf;
-	}*/
-}
-
-
-void ViewCellsParseHandlers::StartBvhInterior(AttributeList& attributes)
-{/*
-	AxisAlignedPlane plane;
-	int len = attributes.getLength();
+{
+	const int len = attributes.getLength();
+	Vector3 minBox, maxBox;
+
+	ObjectContainer objects;
 
 	for (int i = 0; i < len; ++ i) 
@@ -1089,30 +1178,115 @@
 		const char *ptr = attrValue.LocalForm();
 
-		if (attrName == "plane") 
-		{
-			sscanf(ptr, "%d %f",
-				   &plane.mAxis, &plane.mPosition);
-		}
-	}
-
-	KdInterior* interior = new KdInterior(NULL);
-	
-	interior->mAxis = plane.mAxis;
-	interior->mPosition = plane.mPosition;
-
-	if (mCurrentKdNode) // replace NULL child of parent with current node
-	{
-		KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode);
+		if (attrName == "min") 
+		{
+			sscanf(ptr, "%f %f %f", &minBox);
+		}
+		if (attrName == "max") 
+		{
+			sscanf(ptr, "%f %f %f", &maxBox);
+		}
+		if (attrName == "objects")
+		{
+			StartBvhLeafObjects(objects, ptr);
+		}
+	}
+
+	BvhLeaf * leaf = 
+		new BvhLeaf(AxisAlignedBox3(minBox, maxBox), 
+					dynamic_cast<BvhInterior *>(mCurrentBvhNode), (int)objects.size());
+
+	leaf->mObjects = objects;
+
+	if (mCurrentBvhNode) // replace front or (if not NULL) back child
+	{
+		dynamic_cast<BvhInterior *>(mCurrentBvhNode)->ReplaceChildLink(NULL, leaf);
+	}
+	else
+	{
+		mHierarchyManager->mBvHierarchy->mRoot = leaf;
+	}
+}
+
+
+void ViewCellsParseHandlers::StartBvhLeafObjects(ObjectContainer &objects, 
+												 const char *ptr)
+{
+	vector<int> objIndices;
+
+	char *endptr;
+			
+	while (1)
+	{
+		const int index = strtol(ptr, &endptr, 10);
+
+		if (ptr == endptr) break;
+
+		objIndices.push_back(index);
+
+		ptr = endptr;
+	}
+
+	//TODO: find objects and add them to pvs
+	// TODO: get view cell with specified id
+	MeshInstance dummyInst(NULL);
+
+	vector<int>::const_iterator it, it_end = objIndices.end();
+
+	for (it = objIndices.begin(); it != it_end; ++ it)
+	{
+		const int objId = *it;	
+		dummyInst.SetId(objId);
+
+		ObjectContainer::iterator oit =
+			lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);	
+							
+		if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
+		{
+			objects.push_back(*oit);	
+		}
+		else
+		{
+			cout << "error: object with id " << objId << " does not exist" << endl;
+		}
+	}
+}
+
+
+void ViewCellsParseHandlers::StartBvhInterior(AttributeList& attributes)
+{
+	const int len = attributes.getLength();
+	Vector3 minBox, maxBox;
+
+	for (int i = 0; i < len; ++ i) 
+	{
+		string attrName(StrX(attributes.getName(i)).LocalForm());
+		StrX attrValue(attributes.getValue(i));
+		const char *ptr = attrValue.LocalForm();
+
+		if (attrName == "min") 
+		{
+			sscanf(ptr, "%f %f %f", &minBox);
+		}
+		if (attrName == "max") 
+		{
+			sscanf(ptr, "%f %f %f", &maxBox);
+		}
+	}
+
+	BvhInterior* interior = new BvhInterior(AxisAlignedBox3(minBox, maxBox));
+
+	if (mCurrentBvhNode) // replace NULL child of parent with current node
+	{
+		BvhInterior *parent = dynamic_cast<BvhInterior *>(mCurrentBvhNode);
 		parent->ReplaceChildLink(NULL, interior);
-		interior->mParent = parent;
+		interior->SetParent(parent);
 	}
 	else
 	{
-		mOspTree->mRoot = interior;
-	}
-
-	mCurrentKdNode = interior;*/
-}
-
+		mBvHierarchy->mRoot = interior;
+	}
+
+	mCurrentBvhNode = interior;
+}
 
 
