Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 533)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 535)
@@ -29,6 +29,6 @@
 VssPreprocessor {
 	samplesPerPass  100000
-	initialSamples 200000
-	vssSamples 300000
+	initialSamples 300000
+	vssSamples 800000
 	vssSamplesPerPass 100000
 	useImportanceSampling true
@@ -49,27 +49,27 @@
 	useRss    false
 	epsilon		1e-6
-
+	
 	maxDepth 	40
 	minPvs		30
-	minRays		1000
+	minRays		100
 	minSize		0.001
- 	maxCostRatio	0.9
-	maxRayContribution 0.05
-	
+ 	maxCostRatio	1.5
+	maxRayContribution 0.5
+		
 	maxTotalMemory	200
-	maxStaticMemory	20
-
+	maxStaticMemory	100
+	
 	splitType regular
-#	splitType heuristic
-#	splitType hybrid
+	#	splitType heuristic
+	#	splitType hybrid
 	splitUseOnlyDrivingAxis true
-
-	interleaveDirSplits	false
-    dirSplitDepth 40
-
+	
+	interleaveDirSplits	true
+    dirSplitDepth 0
+	
 	numberOfEndPointDomains	10000
 	ct_div_ci	0.0
 	randomize	false
-
+	
 	refDirBoxMaxSize	0.1
 }
@@ -171,5 +171,5 @@
 ViewCells {
 	loadFromFile false
-	exportToFile true
+	exportToFile false
 	#type kdTree
 	#type vspKdTree
@@ -189,5 +189,5 @@
 	PostProcess {
 		# how much samples are used for post processing
-		samples 200000
+		samples 20000
 	}
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 534)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 535)
@@ -28,13 +28,19 @@
   mPlanes.push_back(Plane3(-CrossProd(directions[3], directions[0]), center));
 
-
   // now make sure all planes contain the spatial box
   int i, j;
-  for (i=0; i < mPlanes.size(); i++) {
-	for (j=0; j < 8; j++) {
-	  float dist = mPlanes[i].Distance(box.GetVertex(j));
-	  if (dist > 0)
-		mPlanes[i].mD -= dist;
-	}
+  for (i=0; i < mPlanes.size(); i++) 
+  {
+	  float maxDist = 0;
+	  
+	  for (j=0; j < 8; j++) 
+	  {
+		float dist = mPlanes[i].Distance(box.GetVertex(j));
+	
+		if (dist > maxDist)
+			maxDist = dist;
+	  }
+
+	  mPlanes[i].mD -= maxDist;
   }
 
@@ -85,15 +91,9 @@
 }
 
+
 Vector3 Beam::GetMainDirection() const
 {
-	 Vector3 directions[4];
-	 
-	 directions[0] = VssRay::GetDirection(mDirBox.Min().x, mDirBox.Min().y);
-	 directions[1] = VssRay::GetDirection(mDirBox.Max().x, mDirBox.Min().y);
-	 directions[2] = VssRay::GetDirection(mDirBox.Max().x, mDirBox.Max().y);
-	 directions[3] = VssRay::GetDirection(mDirBox.Min().x, mDirBox.Max().y);
-
-	const Vector3 mainDir = directions[0] + directions[1] + directions[2] + directions[3];
-	return Normalize(mainDir);
+	const Vector3 dCenter = mDirBox.Center();
+	return VssRay::GetDirection(dCenter.x, dCenter.y);
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 535)
@@ -46,5 +46,5 @@
 	/** Computes bounding box of the geometry.
 	*/
-	void ComputeBoundingBox(AxisAlignedBox3 &box);
+	void IncludeInBox(AxisAlignedBox3 &box);
 
 	/** Splits the polygon and returns the part of the polygon inside of the node geometry.
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 535)
@@ -29,4 +29,5 @@
 const float VspBspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0};
 
+int BspMergeCandidate::sMaxPvsSize = 0;
 
 int VspBspTree::sFrontId = 0;
@@ -311,5 +312,6 @@
 
 	mMaxPvs = (int)(mMaxPvsRatio * (float)numObj);
-	Debug << "maximal pvs where view cell is valid: " << mMaxPvs << endl;
+	
+	Debug << "maximal pvs (i.e., view cell considered as valid: " << mMaxPvs << endl;
 	//-- store rays
 	for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
@@ -424,5 +426,5 @@
 		 (data.mArea <= mTermMinArea) ||
 		 (mStat.Leaves() >= mMaxViewCells) ||
-		 (data.GetAvgRayContribution() >= mTermMaxRayContribution) ||
+		 (data.GetAvgRayContribution() > mTermMaxRayContribution) ||
 		 (data.mDepth >= mTermMaxDepth));
 }
@@ -2159,4 +2161,5 @@
 typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
 
+
 int VspBspTree::CastBeam(Beam &beam)
 {
@@ -2176,26 +2179,43 @@
 		
 		AxisAlignedBox3 box;
-		geom->ComputeBoundingBox(box);
-
-		int side = beam.ComputeIntersection(box);
+		box.Initialize();
+		geom->IncludeInBox(box);
+
+		Debug << "box:\n " << box << endl;;
+		const int side = beam.ComputeIntersection(box);
 		
 		switch (side) 
 		{
 		case -1:
+			Debug << "here999" << endl;
 			CollectViewCells(node, beam.mViewCells, true);
 			break;
 		case 0:
+			
 			if (node->IsLeaf())
 			{
-				BspLeaf *leaf = dynamic_cast<BspLeaf *>(leaf);
+				Debug << "here2" << endl;
+				BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
+			
 				if (!leaf->GetViewCell()->Mailed() && leaf->TreeValid())
+				{
+					leaf->GetViewCell()->Mail();
 					beam.mViewCells.push_back(leaf->GetViewCell());
+				}
 			}
 			else 
 			{
+				Debug << "here77" << endl;
 				BspInterior *interior = dynamic_cast<BspInterior *>(node);
+			
 				BspNode *first = interior->GetBack();
 				BspNode *second = interior->GetFront();
-                
+            
+				BspNodeGeometry *firstGeom = new BspNodeGeometry();
+				BspNodeGeometry *secondGeom = new BspNodeGeometry();
+
+				ConstructGeometry(first, *firstGeom);
+				ConstructGeometry(second, *secondGeom);
+
 				// decide on the order of the nodes
 				if (DotProd(beam.mPlanes[0].mNormal, 
@@ -2203,19 +2223,18 @@
 				{
 					swap(first, second);
+					swap(firstGeom, secondGeom);
 				}
 
-				BspNodeGeometry *leftGeom = new BspNodeGeometry();
-				BspNodeGeometry *rightGeom = new BspNodeGeometry();
-
-				ConstructGeometry(first, *leftGeom);
-				ConstructGeometry(second, *rightGeom);
-
-				nodeStack.push(bspNodePair(first, leftGeom));
-				nodeStack.push(bspNodePair(second, rightGeom));
+				nodeStack.push(bspNodePair(first, firstGeom));
+				nodeStack.push(bspNodePair(second, secondGeom));
 			}
+			
 			break;
+			Debug << "culling!!" << endl;
 			// default: cull
 		}
+		Debug << "here4" << endl;
 		DEL_PTR(geom);
+		
 	}
 
@@ -2471,4 +2490,6 @@
 int VspBspTree::MergeViewCells(const VssRayContainer &rays)
 {
+	BspMergeCandidate::sMaxPvsSize = mMaxPvs;
+
 	MergeStatistics mergeStats;
 	mergeStats.Start();
@@ -2866,6 +2887,6 @@
 
 	mMergeCost = newCost - oldCost;
-//	if (vcPvs > sMaxPvsSize) // strong penalty if pvs size too large
-//		mMergeCost += 1.0;
+	if (newPvs > sMaxPvsSize) // strong penalty if pvs size too large
+		mMergeCost += 1.0;
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 490)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 535)
@@ -23,4 +23,5 @@
 class RssTree;
 class Mesh;
+class Beam;
 
 class Exporter
@@ -117,4 +118,6 @@
 				 const Vector3 direction) = 0;
 
+  virtual void ExportBeam(const Beam &beam, const AxisAlignedBox3 &box) = 0;
+
   void SetExportRayDensity(const bool d) { mExportRayDensity = d; }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 535)
@@ -1008,5 +1008,5 @@
   {
 	  vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
-
+	
 	  Intersectable::NewMail();
 	  for (it = beam.mKdNodes.begin(); it != it_end; ++ it)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 508)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 535)
@@ -2723,5 +2723,5 @@
 }
 
-void BspNodeGeometry::ComputeBoundingBox(AxisAlignedBox3 &box)
+void BspNodeGeometry::IncludeInBox(AxisAlignedBox3 &box)
 {
 	Polygon3::IncludeInBox(mPolys, box);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 486)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 535)
@@ -23,5 +23,5 @@
 class BspNode;
 //class VspKdViewCell;
-
+class Beam;
 
 class X3dExporter : public Exporter
@@ -121,4 +121,6 @@
   ExportBspSplits(const VspBspTree &tree, const bool exportDepth);
 
+  virtual void ExportBeam(const Beam &beam, const AxisAlignedBox3 &box);
+
 protected:
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 534)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 535)
@@ -354,12 +354,20 @@
   }
 }
-
-
-void VssPreprocessor::TestBeamCasting(VssTree *tree, ViewCellsManager *vm)
+#include "ViewCellBsp.h"
+
+void VssPreprocessor::TestBeamCasting(VssTree *tree, 
+									  ViewCellsManager *vm, 
+									  const ObjectContainer &objects)
 {
 	vector<VssTreeLeaf *> leaves;
 	tree->CollectLeaves(leaves);
 
-	for (int i = 0; i < 10; ++i)
+	Exporter *exporter = Exporter::GetExporter("shafts.x3d");
+
+	exporter->SetWireframe();
+	exporter->ExportGeometry(objects);
+	exporter->SetFilled();
+	//Randomize();
+	for (int i = 0; i < 1; ++ i)
 	{
 		const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
@@ -367,8 +375,8 @@
 
 		Beam beam;
-		AxisAlignedBox3 dirBox =tree->GetDirBBox(leaf);
+		AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
 		AxisAlignedBox3 box = tree->GetBBox(leaf);
 		
-		beam.Construct(dirBox, box);
+		beam.Construct(box, dirBox);
 
 		// collect kd leaves and view cells
@@ -387,6 +395,33 @@
 
 		Debug << "beam statistics: " << stats << endl << endl;
-	}
-
+		
+		AxisAlignedBox3 sbox = mSceneGraph->GetBox();
+		Vector3 bmin = sbox.Min() - 150.0f;
+		Vector3 bmax = sbox.Max() + 150.0f;
+		AxisAlignedBox3 vbox(bmin, bmax);
+		
+		exporter->ExportBeam(beam, vbox);
+
+		bool exportViewCells = false;
+		
+		if (exportViewCells)
+		{
+			exporter->SetWireframe();
+			
+			ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
+			
+			for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
+			{
+				BspNodeGeometry geom;
+				AxisAlignedBox3 vbox;
+				vbox.Initialize();
+				mVspBspTree->ConstructGeometry(*it, geom);
+				geom->IncludeInBox(vbox);
+				exporter->ExportBox(vbox);
+				//exporter->ExportViewCell(*it);
+			}
+		}
+	}
+	delete exporter;
 }
 
@@ -600,7 +635,5 @@
   int pass = 0;
 
-  if (mTestBeamSampling)
-	  TestBeamCasting(vssTree, mViewCellsManager);
-
+  
   // cast view cell samples
   while (1) {
@@ -670,5 +703,5 @@
   }
 
-
+  Debug << vssTree->stat << endl;
   VssRayContainer viewCellRays;
   
@@ -681,4 +714,7 @@
   //-- post process view cells
   mViewCellsManager->PostProcess(mObjects, viewCellRays);
+
+  if (mTestBeamSampling)
+	  TestBeamCasting(vssTree, mViewCellsManager, mObjects);
 
   //-- several visualizations and statistics
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 503)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 535)
@@ -13,4 +13,5 @@
 #include "VspBspTree.h"
 #include "RssTree.h"
+#include "Beam.h"
 
 
@@ -1234,4 +1235,40 @@
   stream<<">"<<endl;
   stream<<"</Viewpoint>"<<endl;
-  
-}
+
+}
+
+
+
+void X3dExporter::ExportBeam(const Beam &beam, const AxisAlignedBox3 &box)
+{
+	PolygonContainer polys;
+	//ExportBox(beam.mBox);
+
+	for (int i = 0; i < beam.mPlanes.size(); ++ i)
+	{
+		Polygon3 *poly = box.CrossSection(beam.mPlanes[i]);
+		
+		for (int j = 0; (j < beam.mPlanes.size()) && poly; ++ j)
+		{
+			if (j != i)
+			{
+				Polygon3 *front = new Polygon3();
+				Polygon3 *back = new Polygon3();
+				
+				poly->Split(beam.mPlanes[j], *front, *back, Limits::Small);
+				DEL_PTR(poly);
+				DEL_PTR(front);
+
+				if (!back->Valid(Limits::Small))
+					DEL_PTR(back);
+				poly = back;
+			}
+		}
+		if (poly)
+			polys.push_back(poly);
+	}
+
+
+	ExportPolygons(polys);
+	CLEAR_CONTAINER(polys);
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 534)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 535)
@@ -288,5 +288,5 @@
 		minPolygons 		-1
 		maxDepth		30
-		minPvs 			20
+		minPvs 			2
 		#minArea 		0.0001
 		minArea 		0.000
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 535)
@@ -81,4 +81,5 @@
   void SetupFalseColor(const int id);
   void RenderIntersectable(Intersectable *);
+  void RenderViewCell(ViewCell *vc);
   void RenderMeshInstance(MeshInstance *mi);
   void RenderMesh(Mesh *m);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 535)
@@ -105,5 +105,5 @@
   void CastRay(const BspTree &tree, const VssRay & vssRay);
 
-  void TestBeamCasting(VssTree *tre, ViewCellsManager *vm);
+  void TestBeamCasting(VssTree *tre, ViewCellsManager *vm, const ObjectContainer &objects);
 
   bool mTestBeamSampling;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 535)
@@ -2443,4 +2443,4 @@
 int VspBspViewCellsManager::CastBeam(Beam &beam)
 {
-	return 0;
-}
+	return mVspBspTree->CastBeam(beam);
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 532)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 535)
@@ -67,4 +67,7 @@
 	RenderMeshInstance((MeshInstance *)object);
 	break;
+  case Intersectable::VIEW_CELL:
+	  RenderViewCell(dynamic_cast<ViewCell *>(object));
+	  break;
   default:
 	cerr<<"Rendering this object not yet implemented\n";
@@ -73,4 +76,11 @@
 }
 
+
+void
+GlRenderer::RenderViewCell(ViewCell *vc)
+{
+	if (vc->GetMesh())
+		RenderMesh(vc->GetMesh());
+}
 
 void
