Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 586)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 587)
@@ -920,4 +920,5 @@
 {
 	environment->GetIntValue("BspTree.Construction.samples", mInitialSamples);
+	mBspTree->SetViewCellsManager(this);
 }
 
@@ -965,5 +966,5 @@
 	{
 		// no view cells loaded
-		mBspTree->Construct(objects, constructionRays);
+		mBspTree->Construct(objects, constructionRays, &mViewSpaceBox);
 		// collect final view cells
 		mBspTree->CollectViewCells(mViewCells);
@@ -986,4 +987,32 @@
 
 
+	ResetViewCells();
+    
+	Debug << "\nView cells after " << (int)savedRays.size() 
+		  << " samples:\n" << mViewCellsStats << endl;
+
+	if (1) // export initial view cells
+	{
+		cout << "exporting initial view cells (=leaves) ... ";
+		Exporter *exporter = Exporter::GetExporter("view_cells.x3d");
+
+		if (exporter)
+		{
+			//exporter->SetWireframe();
+			exporter->SetFilled();
+			ExportViewCellsForViz(exporter);
+
+			if (0 && mExportRays)
+				exporter->ExportRays(rays, RgbColor(1, 1, 1));
+
+			if (mExportGeometry)
+				exporter->ExportGeometry(objects);
+
+			delete exporter;
+		}
+		cout << "finished" << endl;
+	}
+
+
 	return sampleContributions;
 }
@@ -992,5 +1021,14 @@
 void BspViewCellsManager::CollectViewCells()
 {
-	mBspTree->CollectViewCells(mViewCells);
+	// view cells tree constructed
+	if (!ViewCellsTreeConstructed())
+	{
+		mBspTree->CollectViewCells(mViewCells);
+	}
+	else 
+	{
+		// we can use the view cells tree hierarchy to get the right set
+		mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumMergedViewCells);
+	}
 }
 
@@ -1007,5 +1045,6 @@
 
 
-float BspViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
+float BspViewCellsManager::GetRendercost(ViewCell *viewCell, 
+										 float objRendercost) const
 {
 	return viewCell->GetPvs().GetSize() * objRendercost;
@@ -1072,11 +1111,11 @@
 
 	long startTime = GetTime();
-
+	
+	VssRayContainer postProcessRays;
+	GetRaySets(rays, mPostProcessSamples, postProcessRays);
 
 	//-- merge or subdivide view cells
-	int merged = 0;
-
-	// TODO
-	
+	int merged = mViewCellsTree->ConstructMergeTree(rays, objects);
+
 	//-- stats and visualizations
 	cout << "finished" << endl;
@@ -1088,6 +1127,21 @@
 		  << "using " << (int)rays.size() << " samples" << endl << endl;
 
+
 	// reset view cells and stats
 	ResetViewCells();
+	FinalizeViewCells(true);
+	
+	// for output we need unique ids for each view cell
+	CreateUniqueViewCellIds();
+
+	// write view cells to disc
+	if (mExportViewCells)
+	{
+		char buff[100];
+		environment->GetStringValue("ViewCells.filename", buff);
+		string vcFilename(buff);
+
+		ExportViewCells(buff);
+	}
 
 	return merged;
@@ -1112,16 +1166,38 @@
 		return;
 	
-	if (1) // export view cells
-	{
+	//BspLeaf::NewMail();
+	if (1) // export merged view cells
+	{
+
+		cout << "reseting view cells ... ";
+		ResetViewCells();
+
+		cout << "finished" << endl;
+
+		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
+		Debug << "\nView cells after merge:\n" << mViewCellsStats << endl;
+
 		cout << "exporting view cells after merge ... ";
-		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
-
 
 		if (exporter)
 		{
+			if (0)
+				exporter->SetWireframe();
+			else
+				exporter->SetFilled();
 			ExportViewCellsForViz(exporter);
+
+			if (mExportGeometry)
+			{
+				Material m;
+				m.mDiffuseColor = RgbColor(0, 1, 0);
+				exporter->SetForcedMaterial(m);
+				exporter->SetFilled();
+
+				exporter->ExportGeometry(objects);
+			}
+
 			delete exporter;
 		}
-
 		cout << "finished" << endl;
 	}
@@ -1189,7 +1265,7 @@
 
 	// sort view cells to get largest view cells
-#if 0
-	stable_sort(mViewCells.begin(), mViewCells.end(), vc_gt);
-#endif
+	if (0)
+		stable_sort(mViewCells.begin(), mViewCells.end(), vc_gt);
+
 	int limit = min(leafOut, (int)mViewCells.size());
 
@@ -1208,17 +1284,19 @@
 		cout << "creating output for view cell " << i << " ... ";
 
-#if 0
-		// check whether we can add the current ray to the output rays
-		for (int k = 0; k < raysOut; ++ k)
-		{
-			BspRay *ray = mBspRays[k];
-			for	(int j = 0; j < (int)ray->intersections.size(); ++ j)
+		if(0)
+		{
+			// check whether we can add the current ray to the output rays
+			for (int k = 0; k < raysOut; ++ k)
 			{
-				BspLeaf *leaf = ray->intersections[j].mLeaf;
-				if (vc == leaf->GetViewCell())
-					vcRays.push_back(ray->vssRay);
+				BspRay *ray = mBspRays[k];
+				for	(int j = 0; j < (int)ray->intersections.size(); ++ j)
+				{
+					BspLeaf *leaf = ray->intersections[j].mLeaf;
+					if (vc == leaf->GetViewCell())
+						vcRays.push_back(ray->vssRay);
+				}
 			}
 		}
-#endif
+
 		//bspLeaves[j]->Mail();
 		char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
@@ -1232,16 +1310,6 @@
 		exporter->SetForcedMaterial(m);
 
-		if (vc->GetMesh())
-			exporter->ExportViewCell(vc);
-		else
-		{
-			BspNodeGeometry vcGeom;
-
-			//-- export view cell
-#if VC_HISTORY
-			mBspTree->ConstructGeometry(vc, vcGeom);
-			exporter->ExportPolygons(vcGeom.mPolys);
-#endif
-		}
+		ExportViewCellGeometry(exporter, vc);
+		
 
 		Debug << i << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc)
@@ -1284,32 +1352,4 @@
 
 
-
-void BspViewCellsManager::ConstructBspRays(const VssRayContainer &rays,
-										   const int numSamples)
-{
-	VssRayContainer::const_iterator it, it_end = rays.end();
-
-	for (it = rays.begin(); it != rays.end() && mBspRays.size() < numSamples; ++ it)
-	{
-		VssRay *vssRay = *it;
-		BspRay *ray = new BspRay(vssRay);
-
-		ViewCellContainer viewCells;
-
-		// cast line segment to get intersections with bsp leaves
-		CastLineSegment(vssRay->mTermination, vssRay->mOrigin, viewCells);
-#if VC_HISTORY
-		ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
-		for (vit = viewCells.begin(); vit != vit_end; ++ vit)
-		{
-			ViewCell *vc = *vit;
-			ray->intersections.push_back(BspIntersection(0, vc->mLeaf));
-		}
-#endif
-		mBspRays.push_back(ray);
-	}
-}
-
-
 void BspViewCellsManager::ExportColor(Exporter *exporter,
 									  ViewCell *vc) const
@@ -1354,41 +1394,93 @@
 }
 
+
 void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
-										 ViewCell *vc) const
+												 ViewCell *vc) const
 {
 	if (vc->GetMesh())
-		exporter->ExportViewCell(vc);
+	{
+		exporter->ExportMesh(vc->GetMesh());
+		return;
+	}
+
+	BspNodeGeometry geom;
+	mBspTree->ConstructGeometry(vc, geom);
+	exporter->ExportPolygons(geom.mPolys);
+}
+
+
+void BspViewCellsManager::CreateMesh(ViewCell *vc)
+{
+	if (vc->GetMesh())
+		delete vc->GetMesh();
+
+	BspNodeGeometry geom;
+	
+	mBspTree->ConstructGeometry(vc, geom);
+
+	Mesh *mesh = new Mesh();
+	geom.AddToMesh(*mesh);
+	vc->SetMesh(mesh);
+	mMeshContainer.push_back(mesh);
+}
+
+
+void BspViewCellsManager::Finalize(ViewCell *viewCell, 
+								   const bool createMesh)
+{
+	CreateMesh(viewCell);
+
+	float area = 0;
+	float volume = 0;
+
+	ViewCellContainer leaves;
+	mViewCellsTree->CollectLeaves(viewCell, leaves);
+
+	ViewCellContainer::const_iterator it, it_end = leaves.end();
+
+	for (it = leaves.begin(); it != it_end; ++ it)
+	{
+		BspNodeGeometry geom;
+		BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
+		mBspTree->ConstructGeometry(leaf, geom);
+
+		area += geom.GetArea();
+		volume += geom.GetVolume();
+	}
+
+	viewCell->SetVolume(volume);
+	viewCell->SetArea(area);
+}
+
+
+ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point) const
+{
+	if (!mBspTree)
+		return NULL;
+
+	if (!mViewSpaceBox.IsInside(point))
+		return NULL;
+	
+	return mBspTree->GetViewCell(point);
+}
+
+
+void BspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays, 
+												 vector<MergeCandidate> &candidates)
+{
+	cout << "collecting merge candidates ... " << endl;
+
+	if (mUseRaysForMerge)
+	{
+		mBspTree->CollectMergeCandidates(rays, candidates);
+	}
 	else
 	{
-		BspNodeGeometry geom;
-#if VC_HISTORY
-		mBspTree->ConstructGeometry(vc, geom);
-		exporter->ExportPolygons(geom.mPolys);
-#endif
-	}
-}
-
-
-void BspViewCellsManager::CreateMesh(ViewCell *vc)
-{
-	// TODO
-}
-
-
-ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point) const
-{
-  if (!mBspTree)
-	return NULL;
-
-  if (!mViewSpaceBox.IsInside(point))
-	return NULL;
-
-  return mBspTree->GetViewCell(point);
-}
-
-
-void BspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates)
-{
-	// TODO
+		vector<BspLeaf *> leaves;
+		mBspTree->CollectLeaves(leaves);
+		mBspTree->CollectMergeCandidates(leaves, candidates);
+	}
+
+	cout << "fininshed collecting candidates" << endl;
 }
 
@@ -1453,4 +1545,5 @@
 	return 0;
 }
+
 
 bool KdViewCellsManager::ViewCellsConstructed() const
@@ -2154,5 +2247,7 @@
 	VssRayContainer savedRays;
 
-	Debug << "samples used for subdivision: " << mInitialSamples << " rays: " << (int)rays.size() << endl;
+	Debug << "samples used for subdivision: " << mInitialSamples 
+		  << " rays: " << (int)rays.size() << endl;
+
 	GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
 
@@ -2170,5 +2265,7 @@
 	long startTime = GetTime();
 	int collapsedLeaves = mVspBspTree->CollapseTree();
-	Debug << "collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3 << " seconds" << endl;
+	Debug << "collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3 
+		  << " seconds" << endl;
+
     cout << "finished" << endl;
 
@@ -2257,4 +2354,5 @@
 		cout << "reseting view cells ... ";
 		ResetViewCells();
+
 		cout << "finished" << endl;
 
@@ -2685,14 +2783,14 @@
 
 	// sort view cells to visualize the largest view cells
-#if 0
-	stable_sort(mViewCells.begin(), mViewCells.end(), vc_gt);
-#endif
+	if (0)
+		stable_sort(mViewCells.begin(), mViewCells.end(), vc_gt);
+
 	int limit = min(leafOut, (int)mViewCells.size());
 
-#if 1
+	int raysOut;
+
 	//-- some rays for output
-	const int raysOut = min((int)rays.size(), mVisualizationSamples);
-
-#endif
+	if (1)
+		raysOut = min((int)rays.size(), mVisualizationSamples);
 
 	for (int i = 0; i < limit; ++ i)
@@ -2709,4 +2807,7 @@
 			vc = mViewCells[Random((int)mViewCells.size())];
 
+		vector<ViewCell *> leaves;
+		mViewCellsTree->CollectLeaves(vc, leaves);
+
         if (1)
 		{
@@ -2718,9 +2819,7 @@
 				{
 					ViewCell *rayvc = ray->mViewCells[j];
-#if VC_HISTORY
-					BspLeaf *leaf = rayvc->mLeaf;
-					if (vc == rayvc)
+
+					if (leaves[0] == rayvc)
 						vcRays.push_back(ray);
-#endif
 				}
 			}
@@ -3021,5 +3120,6 @@
 
 
-void VspBspViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
+void VspBspViewCellsManager::Finalize(ViewCell *viewCell, 
+									  const bool createMesh)
 {
 	CreateMesh(viewCell);
