Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 565)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 568)
@@ -251,5 +251,5 @@
 		maxCostRatio 0.005
 		minViewCells 1000
-		maxPvsSize   50000
+		maxPvsSize   5000
 	}
 	
@@ -305,5 +305,5 @@
 		#maxAccRayLength	100
 		
-		maxViewCells		50000
+		maxViewCells		5000
 		
 		# used for pvs criterium
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 566)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 568)
@@ -1980,5 +1980,5 @@
 				bool isAdjacent = true;
 
-				if (1)
+			/*	if (1)
 				{
 					// test all planes of current node if still adjacent
@@ -2014,5 +2014,5 @@
 						}
 					}
-				}
+				}*/
 				// neighbor was found
 				if (isAdjacent)
@@ -3092,7 +3092,6 @@
 int VspBspTree::RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects)
 {
-	int shuffled = 0;
-
 	Debug << "refining " << (int)mMergeQueue.size() << " candidates " << endl;
+	
 	BspLeaf::NewMail();
 
@@ -3116,4 +3115,5 @@
 	queue<BspMergeCandidate> *backQueue = &queue2;
 
+#if 1
     Exporter *exporter = Exporter::GetExporter("neighors.x3d");
 
@@ -3125,15 +3125,11 @@
 
 	// HACK for visualization
-	/*ViewCellContainer viewCells;
-	ViewCell::NewMail();
-	CollectViewCells(mRoot, true, viewCells, true);
-	for (int i = 0; i < viewCells.size(); ++i)
-		viewCells[i]->SetId((int)RandomValue(0, Real(256*256*256)));
-	*/
-	Material m;
-	m.mDiffuseColor.r = 0;
-	m.mDiffuseColor.g = 0;
-	m.mDiffuseColor.b = 0;
-
+	//ViewCellContainer viewCells;
+	//ViewCell::NewMail();
+	//CollectViewCells(mRoot, true, viewCells, true);
+	//for (int i = 0; i < viewCells.size(); ++i)
+	//	viewCells[i]->SetId((int)RandomValue(0, Real(256*256*256)));
+	
+	
 	while (!mMergeQueue.empty())
 	{
@@ -3142,25 +3138,32 @@
 		mMergeQueue.pop();
 
-
 		// visualize neighbors
-		m = RandomMaterial();
+		Material m = RandomMaterial();
 		exporter->SetForcedMaterial(m);
-		
-		BspNodeGeometry geom1, geom2;
-
-		ConstructGeometry(mc.GetLeaf1(), geom1);
-		ConstructGeometry(mc.GetLeaf1(), geom2);
-
-		//m.mDiffuseColor.r = (mc.GetLeaf1()->GetViewCell()->GetId() & 256)/ 255.0f;
-		//m.mDiffuseColor.g = ((mc.GetLeaf1()->GetViewCell()->GetId()>>8) & 256)/ 255.0f;
-		//m.mDiffuseColor.b = ((mc.GetLeaf1()->GetViewCell()->GetId()>>16) & 256)/ 255.0f;
-		exporter->SetForcedMaterial(m);
-		exporter->ExportPolygons(geom1.mPolys);
-		
-		//m.mDiffuseColor.r = (mc.GetLeaf2()->GetViewCell()->GetId() & 256)/ 255.0f;
-		//m.mDiffuseColor.g = ((mc.GetLeaf2()->GetViewCell()->GetId()>>8) & 256)/ 255.0f;
-		//m.mDiffuseColor.b = ((mc.GetLeaf2()->GetViewCell()->GetId()>>16) & 256)/ 255.0f;
-		//exporter->SetForcedMaterial(m);
-		exporter->ExportPolygons(geom2.mPolys);
+	
+
+		if (!mc.GetLeaf1()->Mailed())
+		{
+			BspNodeGeometry geom1;
+			ConstructGeometry(mc.GetLeaf1(), geom1);
+			//m.mDiffuseColor.r = (mc.GetLeaf1()->GetViewCell()->GetId() & 256)/ 255.0f;
+			//m.mDiffuseColor.g = ((mc.GetLeaf1()->GetViewCell()->GetId()>>8) & 256)/ 255.0f;
+			//m.mDiffuseColor.b = ((mc.GetLeaf1()->GetViewCell()->GetId()>>16) & 256)/ 255.0f;
+			//exporter->SetForcedMaterial(m);
+			exporter->ExportPolygons(geom1.mPolys);
+			mc.GetLeaf1()->Mail();
+		}
+
+		if (!mc.GetLeaf2()->Mailed())
+		{
+			BspNodeGeometry geom2;
+			ConstructGeometry(mc.GetLeaf2(), geom2);
+			//m.mDiffuseColor.r = (mc.GetLeaf2()->GetViewCell()->GetId() & 256)/ 255.0f;
+			//m.mDiffuseColor.g = ((mc.GetLeaf2()->GetViewCell()->GetId()>>8) & 256)/ 255.0f;
+			//m.mDiffuseColor.b = ((mc.GetLeaf2()->GetViewCell()->GetId()>>16) & 256)/ 255.0f;
+			//exporter->SetForcedMaterial(m);
+			exporter->ExportPolygons(geom2.mPolys);
+			mc.GetLeaf2()->Mail();
+		}		
 	}
 
@@ -3170,9 +3173,18 @@
 		delete exporter;
 	}
-
+#else
+	while (!mMergeQueue.empty())
+	{
+		BspMergeCandidate mc = mMergeQueue.top();
+		shuffleQueue->push(mc);
+		mMergeQueue.pop();
+	}
+#endif
 	const int numPasses = 5;
 	int pass = 0;
 	int passShuffled = 0;
-
+	int shuffled = 0;
+
+	BspLeaf::NewMail();
 
 	do
@@ -3220,5 +3232,8 @@
 }
 
-/*inline int SubtractedPvsSize(BspViewCell *vc, BspLeaf *l, const ObjectPvs &pvs2)
+
+// recomputes pvs size minus pvs of leaf l
+#if 0
+inline int SubtractedPvsSize(BspViewCell *vc, BspLeaf *l, const ObjectPvs &pvs2)
 {
 	ObjectPvs pvs;
@@ -3228,6 +3243,8 @@
 			pvs.AddPvs(*(*it)->mPvs);
 	return pvs.GetSize();
-}*/
-
+}
+#endif
+
+// computes pvs1 minus pvs2
 inline int SubtractedPvsSize(ObjectPvs pvs1, const ObjectPvs &pvs2)
 {
@@ -3240,7 +3257,8 @@
 	//const int pvs1 = SubtractedPvsSize(vc1, leaf, *leaf->mPvs);
 	const int pvs1 = SubtractedPvsSize(vc1->GetPvs(), *leaf->mPvs);
+	
 	const int pvs2 = AddedPvsSize(vc2->GetPvs(), *leaf->mPvs);
 
-	// dont' shuffle leaves with pvs > max
+	// don't shuffle leaves with pvs > max
 	if (pvs1 + pvs2 > mViewCellsManager->GetMaxPvsSize())
 		return 1e15f;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 567)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 568)
@@ -2097,74 +2097,11 @@
 	//-- refines the merged view cells
 	RefineViewCells(postProcessRays, objects);
-	// collapse sibling leaves that share the same view cell
-	mVspBspTree->CollapseTree();
-
-	ResetViewCells();
-
-	// real meshes are only contructed only at this stage
-	FinalizeViewCells(true);
-
-	// write view cells to disc
-	if (mExportViewCells)
-	{
-		char buff[100];
-		environment->GetStringValue("ViewCells.filename", buff);
-		string vcFilename(buff);
-
-		ExportViewCells(buff);
-	}
-
-	return 0;
-}
-
-
-int VspBspViewCellsManager::GetType() const
-{
-	return VSP_BSP;
-}
-
-
-bool VspBspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
-{
-	if (!ViewCellsConstructed())
-		return ViewCellsManager::GetViewPoint(viewPoint);
-
-	// TODO: set reasonable limit
-	const int limit = 20;
-
-	for (int i = 0; i < limit; ++ i)
-	{
-		viewPoint = mViewSpaceBox.GetRandomPoint();
-		if (mVspBspTree->ViewPointValid(viewPoint))
-		{
-			return true;
-		}
-	}
-	Debug << "failed to find valid view point, taking " << viewPoint << endl;
-	return false;
-}
-
-
-bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
-{
-	return mViewSpaceBox.IsInside(viewPoint) &&
-		   mVspBspTree->ViewPointValid(viewPoint);
-}
-
-
-void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
-									   const VssRayContainer &sampleRays)
-{
-	if (!ViewCellsConstructed())
-		return;
-
-	VssRayContainer visRays;
-	GetRaySets(sampleRays, mVisualizationSamples, visRays);
-
-	if (1) // export view cells
-	{
-		cout << "exporting view cells after post process ... ";
-		Exporter *exporter = Exporter::GetExporter("final_view_cells.x3d");
-
+
+	//-- export shuffled view cells
+	if (1)
+	{
+		cout << "exporting shuffled view cells ... ";
+
+		Exporter *exporter = Exporter::GetExporter("shuffled_view_cells.x3d");
 		if (exporter)
 		{
@@ -2181,21 +2118,114 @@
 			}
 
-			// export rays
-			if (mExportRays)
+			ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
+
+			Material vm, lm;
+
+			for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
 			{
-				exporter->ExportRays(visRays, RgbColor(0, 1, 0));
+				BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit);
+
+				vm = RandomMaterial();
+
+				lm = vm;
+
+				vm.mDiffuseColor.r -= 0.45f;
+				vm.mDiffuseColor.g -= 0.45f;
+				vm.mDiffuseColor.b -= 0.45f;
+
+				vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end();
+
+				for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit)
+				{
+					BspLeaf *leaf = *lit;
+
+					if (leaf->Mailed())
+						exporter->SetForcedMaterial(lm);
+					else
+						exporter->SetForcedMaterial(vm);
+
+					BspNodeGeometry geom;
+					mVspBspTree->ConstructGeometry(leaf, geom);
+					exporter->ExportPolygons(geom.mPolys);
+				}
 			}
 
-			ExportViewCellsForViz(exporter);
 			delete exporter;
 		}
-	}
-
-	// export shuffled view cells
-	if (1)
-	{
-		cout << "exporting shuffled view cells ...";
-
-		Exporter *exporter = Exporter::GetExporter("shuffled_view_cells.x3d");
+
+
+		cout << "finished" << endl;
+	}
+
+	// collapse sibling leaves that share the same view cell
+	mVspBspTree->CollapseTree();
+
+	ResetViewCells();
+
+	// real meshes are only contructed only at this stage
+	FinalizeViewCells(true);
+
+	// write view cells to disc
+	if (mExportViewCells)
+	{
+		char buff[100];
+		environment->GetStringValue("ViewCells.filename", buff);
+		string vcFilename(buff);
+
+		ExportViewCells(buff);
+	}
+
+	return 0;
+}
+
+
+int VspBspViewCellsManager::GetType() const
+{
+	return VSP_BSP;
+}
+
+
+bool VspBspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
+{
+	if (!ViewCellsConstructed())
+		return ViewCellsManager::GetViewPoint(viewPoint);
+
+	// TODO: set reasonable limit
+	const int limit = 20;
+
+	for (int i = 0; i < limit; ++ i)
+	{
+		viewPoint = mViewSpaceBox.GetRandomPoint();
+		if (mVspBspTree->ViewPointValid(viewPoint))
+		{
+			return true;
+		}
+	}
+	Debug << "failed to find valid view point, taking " << viewPoint << endl;
+	return false;
+}
+
+
+bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
+{
+	return mViewSpaceBox.IsInside(viewPoint) &&
+		   mVspBspTree->ViewPointValid(viewPoint);
+}
+
+
+void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
+									   const VssRayContainer &sampleRays)
+{
+	if (!ViewCellsConstructed())
+		return;
+
+	VssRayContainer visRays;
+	GetRaySets(sampleRays, mVisualizationSamples, visRays);
+
+	if (1) // export view cells
+	{
+		cout << "exporting view cells after post process ... ";
+		Exporter *exporter = Exporter::GetExporter("final_view_cells.x3d");
+
 		if (exporter)
 		{
@@ -2212,42 +2242,13 @@
 			}
 
-			ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
-
-			Material vm, lm;
-
-			for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
+			// export rays
+			if (mExportRays)
 			{
-				BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit);
-
-				vm = RandomMaterial();
-
-				lm = vm;
-
-				vm.mDiffuseColor.r -= 0.45f;
-				vm.mDiffuseColor.g -= 0.45f;
-				vm.mDiffuseColor.b -= 0.45f;
-
-				vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end();
-
-				for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit)
-				{
-					BspLeaf *leaf = *lit;
-
-					if (leaf->Mailed())
-						exporter->SetForcedMaterial(lm);
-					else
-						exporter->SetForcedMaterial(vm);
-
-					BspNodeGeometry geom;
-					mVspBspTree->ConstructGeometry(leaf, geom);
-					exporter->ExportPolygons(geom.mPolys);
-				}
+				exporter->ExportRays(visRays, RgbColor(0, 1, 0));
 			}
 
+			ExportViewCellsForViz(exporter);
 			delete exporter;
 		}
-
-
-		cout << "finished" << endl;
 	}
 
