Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 384)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 385)
@@ -126,8 +126,8 @@
 	#splitPlaneStrategy 130
 	
-	splitPlaneStrategy 1032
+	splitPlaneStrategy 1024
 	
 	maxPolyCandidates 50
-	maxRayCandidates 0
+	maxRayCandidates 50
 	
 	Termination {
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 369)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 385)
@@ -36,6 +36,6 @@
 
 	int Compress() {return 0;}
-	int GetSize() {return (int)mEntries.size();}
-	bool Empty() {return mEntries.size() == 0;}
+	int GetSize() const {return (int)mEntries.size();}
+	bool Empty() const {return mEntries.size() == 0;}
 	/** Merges pvs of a and pvs of b into this pvs.
 	*/
@@ -90,5 +90,4 @@
 	}
 }
-
 
 template <typename T>
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 384)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 385)
@@ -1110,5 +1110,5 @@
 	vector<Plane3 *> planes;
 	vector<bool> sides;
-	float pvsSize = 0;
+	int pvsSize = 0;
 
 	if (sSplitPlaneStrategy & PVS)
@@ -1680,5 +1680,6 @@
 		  << "Depth: " << data.mDepth << " (max: " << sTermMaxDepth << "), "
 		  << "#polygons: " << (int)data.mPolygons->size() << " (max: " << sTermMaxPolygons << "), "
-		  << "#rays: " << (int)data.mRays->size() << " (max: " << sTermMaxRays << ")" << endl;
+		  << "#rays: " << (int)data.mRays->size() << " (max: " << sTermMaxRays << "), " 
+		  << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << endl;
 //#endif
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 384)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 385)
@@ -546,5 +546,5 @@
 		BspViewCellsStatistics stat;
 		
-		Debug << "overall scene size: " << objects.size() << endl;
+		Debug << "overall scene size: " << (int)objects.size() << endl;
 		mBspTree->EvaluateViewCellsStats(stat);
 		
@@ -603,5 +603,4 @@
 		bool exportSplits = false;
 		environment->GetBoolValue("BspTree.exportSplits", exportSplits);
-
 		
 		cout << "exporting splits ... ";
@@ -707,5 +706,5 @@
 		{
 			// construct BSP tree using the samples
-			cout << "building bsp tree from " << mSampleRays.size() << " samples " << endl;
+			cout << "building bsp tree from " << (int)mSampleRays.size() << " samples " << endl;
 			BuildBspTree();
 		
@@ -820,112 +819,171 @@
 }
 
+inline bool vc_gt(ViewCell *a, ViewCell *b) 
+{ 
+	return a->GetPvs().GetSize() > b->GetPvs().GetSize();
+}
+
 void SamplingPreprocessor::ExportBspPvs(const ObjectContainer &objects)
 {
-	//-- some random view cells and rays for output
 	const int leafOut = 10;
-
-	vector<Ray> vcRays[leafOut];
-	vector<BspLeaf *> bspLeaves;
+	
+	ViewCell::NewMail();
+
+	if (0){
+		//-- some random view cells and rays for output
+		const int raysOut = min((int)mSampleRays.size(), 20000);
+		vector<Ray> vcRays[leafOut];
+		vector<BspLeaf *> bspLeaves;
+		for (int i = 0; i < leafOut; ++ i)
+			bspLeaves.push_back(mBspTree->GetRandomLeaf());	
+		
+		for (int i = 0; i < bspLeaves.size(); ++ i)
+		{
+			cout << "creating output for view cell " << i << " ... ";
+			// check whether we can add the current ray to the output rays
+			for (int k = 0; k < raysOut; ++ k) 
+			{
+				Ray *ray = mSampleRays[k];
+
+				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+				{
+					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
+
+					if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell()) 
+					{
+						vcRays[i].push_back(*ray);
+					}
+				}
+			}
+
+			Intersectable::NewMail();
+
+			BspViewCell *vc = dynamic_cast<BspViewCell *>(bspLeaves[i]->GetViewCell());
+
+			//bspLeaves[j]->Mail();
+			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
+
+			Exporter *exporter = Exporter::GetExporter(s);
+			exporter->SetFilled();
+
+			ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
+
+			exporter->SetWireframe();
+
+			Material m;//= RandomMaterial();
+			m.mDiffuseColor = RgbColor(0, 1, 0);
+			exporter->SetForcedMaterial(m);
+
+			if (vc->GetMesh())
+				exporter->ExportViewCell(vc);
+			else
+			{
+				PolygonContainer cell;
+				// export view cell
+				mBspTree->ConstructGeometry(vc, cell);
+				exporter->ExportPolygons(cell);
+				CLEAR_CONTAINER(cell);
+			}
+
+			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
+					<< ", piercing rays=" << (int)vcRays[i].size() << endl;
+
+			// export rays piercing this view cell
+			exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
+
+			m.mDiffuseColor = RgbColor(1, 0, 0);
+			exporter->SetForcedMaterial(m);
+
+			// output PVS of view cell
+			for (; it != vc->GetPvs().mEntries.end(); ++ it) 
+			{
+				Intersectable *intersect = (*it).first;
+				if (!intersect->Mailed())
+				{
+					exporter->ExportIntersectable(intersect);
+					intersect->Mail();
+				}			
+			}
+				
+			// output rest of the objects
+			if (0)
+			{
+				Material m;//= RandomMaterial();
+				m.mDiffuseColor = RgbColor(0, 0, 1);
+				exporter->SetForcedMaterial(m);
+
+				for (int j = 0; j < objects.size(); ++ j)
+					if (!objects[j]->Mailed())
+					{
+						exporter->SetForcedMaterial(m);
+						exporter->ExportIntersectable(objects[j]);
+						objects[j]->Mail();
+					}
+			}
+			DEL_PTR(exporter);
+			cout << "finished" << endl;
+		}
+	}
+	else
+	{
+		ViewCellContainer viewCells;
+
+		mBspTree->CollectViewCells(viewCells);
+		stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
+
+		int limit = min(leafOut, (int)viewCells.size()); 
+		
+		for (int i = 0; i < limit; ++ i)
+		{
+			cout << "creating output for view cell " << i << " ... ";
 			
-	for (int i = 0; i < leafOut; ++ i)
-		bspLeaves.push_back(mBspTree->GetRandomLeaf());
-	
-	const int raysOut = min((int)mSampleRays.size(), 20000);
-
-	ViewCell::NewMail();
-
-	for (int i = 0; i < bspLeaves.size(); ++ i)
-	{
-		cout << "creating output for view cell " << i << " ... ";
-		// check whether we can add the current ray to the output rays
-		for (int k = 0; k < raysOut; ++ k) 
-		{
-			Ray *ray = mSampleRays[k];
-
-			for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+			Intersectable::NewMail();
+
+			BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
+
+			//bspLeaves[j]->Mail();
+			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
+
+			Exporter *exporter = Exporter::GetExporter(s);
+			exporter->SetFilled();
+
+			ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
+
+			exporter->SetWireframe();
+
+			Material m;//= RandomMaterial();
+			m.mDiffuseColor = RgbColor(0, 1, 0);
+			exporter->SetForcedMaterial(m);
+
+			if (vc->GetMesh())
+				exporter->ExportViewCell(vc);
+			else
 			{
-				BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
-
-				if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell()) 
+				PolygonContainer cell;
+				// export view cell
+				mBspTree->ConstructGeometry(vc, cell);
+				exporter->ExportPolygons(cell);
+				CLEAR_CONTAINER(cell);
+			}
+
+			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() << endl;
+
+			m.mDiffuseColor = RgbColor(1, 0, 0);
+			exporter->SetForcedMaterial(m);
+
+			// output PVS of view cell
+			for (; it != vc->GetPvs().mEntries.end(); ++ it) 
+			{
+				Intersectable *intersect = (*it).first;
+				if (!intersect->Mailed())
 				{
-					vcRays[i].push_back(*ray);
-				}
-			}
-		}
-
-		Intersectable::NewMail();
-
-		ViewCell *vc = bspLeaves[i]->GetViewCell();
-
-		//bspLeaves[j]->Mail();
-		char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
-
-		Exporter *exporter = Exporter::GetExporter(s);
-		exporter->SetFilled();
-
-		ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
-
-		exporter->SetWireframe();
-
-		Material m;//= RandomMaterial();
-		m.mDiffuseColor = RgbColor(0, 1, 0);
-		exporter->SetForcedMaterial(m);
-
-		if (vc->GetMesh())
-			exporter->ExportViewCell(vc);
-		else
-		{
-            PolygonContainer cell;
-			// export view cell
-			mBspTree->ConstructGeometry(bspLeaves[i]->GetViewCell(), cell);
-			exporter->ExportPolygons(cell);
-			CLEAR_CONTAINER(cell);
-		}
-
-		Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
-				<< ", piercing rays=" << (int)vcRays[i].size() << endl;
-
-		// export view cells
-		if (0)
-		{
-			m.mDiffuseColor = RgbColor(1, 0, 1);
-			exporter->SetForcedMaterial(m);
-			exporter->ExportViewCells(mViewCells);
-		}
-
-		// export rays piercing this view cell
-		exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
-
-		m.mDiffuseColor = RgbColor(1, 0, 0);
-		exporter->SetForcedMaterial(m);
-
-		// output PVS of view cell
-		for (; it != vc->GetPvs().mEntries.end(); ++ it) 
-		{
-			Intersectable *intersect = (*it).first;
-			if (!intersect->Mailed())
-			{
-				exporter->ExportIntersectable(intersect);
-				intersect->Mail();
-			}			
-		}
-			
-		// output rest of the objects
-		if (0)
-		{
-			Material m;//= RandomMaterial();
-			m.mDiffuseColor = RgbColor(0, 0, 1);
-			exporter->SetForcedMaterial(m);
-
-			for (int j = 0; j < objects.size(); ++ j)
-				if (!objects[j]->Mailed())
-				{
-					exporter->SetForcedMaterial(m);
-					exporter->ExportIntersectable(objects[j]);
-					objects[j]->Mail();
-				}
-		}
-		DEL_PTR(exporter);
-		cout << "finished" << endl;
+					exporter->ExportIntersectable(intersect);
+					intersect->Mail();
+				}			
+			}
+				
+			DEL_PTR(exporter);
+			cout << "finished" << endl;
+		}
 	}
 }
