Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 837)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 840)
@@ -93,5 +93,5 @@
 	environment->GetIntValue("ViewCells.renderCostEvaluationType", mRenderCostEvaluationType);
 
-	environment->GetIntValue("ViewCells.pvsExportMode", mPvsExportMode);
+	environment->GetBoolValue("ViewCells.exportBboxesForPvs", mExportBboxesForPvs);
 
 	char buf[100];
@@ -1677,15 +1677,4 @@
 		if (viewcell->GetValid())
 		{
-			// HACK
-#if TEST_EMPTY_VIEW_CELLS
-				for (int i = 0; i < mEmptyViewCells.size(); ++i)
-				{
-					if (viewcell == mEmptyViewCells[i])
-					{
-						viewcell->mPiercingRays.push_back(new Ray(ray));
-						Debug << "empty view cell ray found: " << ray.mOriginObject << ", " << ray.mTerminationObject << endl;
-					}
-				}
-#endif
 			// if ray not outside of view space
 			float contribution;
@@ -1699,5 +1688,5 @@
 			}
 
-			//for directional sampling it is important to count only contributions
+			// for directional sampling it is important to count only contributions
 			// made in one direction!!!
 			// the other contributions of this sample will be counted for the oposite ray!
@@ -1858,4 +1847,161 @@
 }
 
+
+int ViewCellsManager::GetMaxFilterSize() const
+{
+	return mMaxFilterSize;  
+}
+
+static const bool USE_ASCII = true;
+
+bool ViewCellsManager::ExportBoundingBoxes(const string filename, 
+										   const ObjectContainer &objects) const
+{
+	ObjectContainer::const_iterator it, it_end = objects.end();
+	
+	if (USE_ASCII)
+	{
+		ofstream boxesOut(filename.c_str());
+		if (!boxesOut.is_open())
+			return false;
+
+		for (it = objects.begin(); it != it_end; ++ it)
+		{
+			MeshInstance *mi = dynamic_cast<MeshInstance *>(*it);
+			const AxisAlignedBox3 box = mi->GetBox();
+
+			boxesOut << mi->GetId() << " " 
+					 << box.Min().x << " " 
+					 << box.Min().y << " " 
+					 << box.Min().z << " " 
+					 << box.Max().x << " " 
+					 << box.Max().y << " " 
+                     << box.Max().z << endl;	
+		}
+
+		boxesOut.close();
+	}
+	else
+	{
+		ofstream boxesOut(filename.c_str(), ios::binary);
+
+		if (!boxesOut.is_open())
+			return false;
+
+		for (it = objects.begin(); it != it_end; ++ it)
+		{	
+			MeshInstance *mi = dynamic_cast<MeshInstance *>(*it);
+			const AxisAlignedBox3 box = mi->GetBox();
+			Vector3 bmin = box.Min();
+			Vector3 bmax = box.Max();
+
+			boxesOut.write(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
+			boxesOut.write(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
+		}
+		
+		boxesOut.close();
+	}
+
+
+	return true;
+}
+
+
+// use ascii format to store rays
+#define USE_ASCII 0
+
+
+inline bool ilt(Intersectable *obj1, Intersectable *obj2)
+{
+	return obj1->mId < obj2->mId;
+}
+
+/*
+bool Preprocessor::LoadBoundingBoxes(const string filename, 
+									 const ObjectContainer &objects) const
+{
+	std::stable_sort(objects.begin(), objects.end(), ilt);
+	char fileName[100];
+	environment->GetStringValue("Preprocessor.samplesFilename", fileName);
+	
+    Vector3 origin, termination;
+	// HACK: needed only for lower_bound algorithm to find the 
+	// intersected objects
+	MeshInstance sObj(NULL);
+	MeshInstance tObj(NULL);
+
+#if USE_ASCII
+	ifstream samplesIn(fileName);
+	if (!samplesIn.is_open())
+		return false;
+
+	string buf;
+	while (!(getline(samplesIn, buf)).eof())
+	{
+		sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d", 
+			   &origin.x, &origin.y, &origin.z,
+			   &termination.x, &termination.y, &termination.z, 
+			   &(sObj.mId), &(tObj.mId));
+		
+		Intersectable *sourceObj = NULL;
+		Intersectable *termObj = NULL;
+		
+		if (sObj.mId >= 0)
+		{
+			ObjectContainer::iterator oit =
+				lower_bound(objects.begin(), objects.end(), &sObj, ilt);
+			sourceObj = *oit;
+		}
+		
+		if (tObj.mId >= 0)
+		{
+			ObjectContainer::iterator oit =
+				lower_bound(objects.begin(), objects.end(), &tObj, ilt);
+			termObj = *oit;
+		}
+
+		samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
+	}
+#else
+	ifstream samplesIn(fileName, ios::binary);
+	if (!samplesIn.is_open())
+		return false;
+
+	while (1)
+	{
+		 samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
+		 samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
+		 samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
+		 samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
+		
+		 if (samplesIn.eof())
+			break;
+
+		Intersectable *sourceObj = NULL;
+		Intersectable *termObj = NULL;
+		
+		if (sObj.mId >= 0)
+		{
+			ObjectContainer::iterator oit =
+				lower_bound(objects.begin(), objects.end(), &sObj, ilt);
+			sourceObj = *oit;
+		}
+		
+		if (tObj.mId >= 0)
+		{
+			ObjectContainer::iterator oit =
+				lower_bound(objects.begin(), objects.end(), &tObj, ilt);
+			termObj = *oit;
+		}
+
+		samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
+	}
+
+#endif
+	samplesIn.close();
+
+	return true;
+}*/
+
 /**********************************************************************/
 /*                   BspViewCellsManager implementation               */
@@ -2117,10 +2263,17 @@
 	if (mExportViewCells)
 	{
-		char buff[100];
-		environment->GetStringValue("ViewCells.filename", buff);
-		string vcFilename(buff);
-
-		ExportViewCells(buff);
-	}
+		char filename[100];
+		environment->GetStringValue("ViewCells.filename", filename);
+		ExportViewCells(filename);
+	}
+	
+	// export bounding boxes
+	if (mExportBboxesForPvs)
+	{
+		char filename[100];
+		environment->GetStringValue("ViewCells.boxesFilename", filename);
+		ExportBoundingBoxes(filename, objects);
+	}
+
 
 	return 0;
@@ -2484,6 +2637,7 @@
 	Mesh *mesh = new Mesh();
 
-	geom.AddToMesh(*mesh);
+	IncludeNodeGeomInMesh(geom, *mesh);
 	vc->SetMesh(mesh);
+
 	// put mesh into mesh container so we can savely delete it
 	mMeshContainer.push_back(mesh);
@@ -2578,9 +2732,11 @@
 	//-- the type of the view cells hierarchy
 	//stream << "<Hierarchy name=\"bspTree\" />" << endl;
-	stream << "<Hierarchy name=\"vspBspTree\" />" << endl; // write vsp bsp here because can use same tree and is bug free
+	// NOTE: load in vsp bsp here because bsp and vsp bsp can use same tree and vsp bsp is bug free
+	stream << "<Hierarchy name=\"vspBspTree\" />" << endl; 
+	
 	//-- load the view cells itself, i.e., the ids and the pvs
 	stream << "<ViewCells>" << endl;
 
-	mViewCellsTree->Export(stream, mPvsExportMode);
+	mViewCellsTree->Export(stream);
 
 	stream << "</ViewCells>" << endl;
@@ -2597,24 +2753,4 @@
 
 	return true;
-}
-
-
-void BspViewCellsManager::AddCurrentViewCellsToHierarchy()
-{
-	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
-	for (it = mViewCells.begin(); it != it_end; ++ it)
-	{
-		ViewCell *vc = *it;
-		ViewCellContainer leaves;
-		mViewCellsTree->CollectLeaves(vc, leaves);
-		
-		ViewCellContainer::const_iterator lit, lit_end = leaves.end();
-
-		for (lit = leaves.begin(); lit != lit_end; ++ lit)
-		{
-			BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*lit);
-			bspVc->mLeaf->SetViewCell(vc);
-		}
-	}
 }
 
@@ -3723,5 +3859,5 @@
 int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects,
 										const VssRayContainer &rays)
-{Debug << "here8773" << endl;
+{
 	if (!ViewCellsConstructed())
 	{
@@ -3812,6 +3948,5 @@
 
 	// collapse sibling leaves that share the same view cell
-	if (0)
-		mVspBspTree->CollapseTree();
+	if (0) mVspBspTree->CollapseTree();
 
 	// recompute view cell list and statistics
@@ -3821,15 +3956,19 @@
 	if (1) FinalizeViewCells(true);
 
-
-	Debug << "here3" << endl;
 	// write view cells to disc
 	if (mExportViewCells)
 	{
-	Debug << "here4" << endl;
-		char buff[100];
-		environment->GetStringValue("ViewCells.filename", buff);
-		string vcFilename(buff);
-
-		ExportViewCells(buff);
+		char filename[100];
+		environment->GetStringValue("ViewCells.filename", filename);
+		ExportViewCells(filename);
+	}
+
+	
+	// export bounding boxes
+	if (mExportBboxesForPvs)
+	{
+		char filename[100];
+		environment->GetStringValue("ViewCells.boxesFilename", filename);
+		ExportBoundingBoxes(filename, objects);
 	}
 
@@ -4506,5 +4645,6 @@
 	
 	Mesh *mesh = new Mesh();
-	geom.AddToMesh(*mesh);
+	IncludeNodeGeomInMesh(geom, *mesh);
+
 	vc->SetMesh(mesh);
 	// put mesh into mesh container so we can savely delete it
@@ -4543,9 +4683,4 @@
 }
 
-
-inline bool ilt(Intersectable *obj1, Intersectable *obj2)
-{
-	return obj1->mId < obj2->mId;
-}
 
 
@@ -4567,13 +4702,16 @@
 		   << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\" />" << endl;
 
+
+
 	//-- the type of the view cells hierarchy
 	stream << "<Hierarchy name=\"vspBspTree\" />" << endl;
 
-	//-- export the view cells itself, i.e., the ids and the pvs
+	//-- export the view cells and the pvs
 	stream << "<ViewCells>" << endl;
-	Debug << "**************************** mode: " << mPvsExportMode << endl;
-	mViewCellsTree->Export(stream, mPvsExportMode);
+	
+	mViewCellsTree->Export(stream);
 
 	stream << "</ViewCells>" << endl;
+
 
 	//-- export the hierarchy
@@ -4581,4 +4719,6 @@
 	mVspBspTree->Export(stream);
 	stream << endl << "</Hierarchy>" << endl;
+
+
 
 	stream << "</Visibility_Solution>" << endl;
@@ -4734,12 +4874,4 @@
 
 
-void VspBspViewCellsManager::AddCurrentViewCellsToHierarchy()
-{
-	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
-	for (it = mViewCells.begin(); it != it_end; ++ it)
-	{
-	}
-}
-
 //////////////////////////////////
 ViewCellsManager *ViewCellsManagerFactory::Create(const string mName)
