Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 320)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 321)
@@ -9,7 +9,7 @@
 #	filename vienna.x3d
 #	filename ../data/vienna/vienna-simple.x3d
-	filename ../data/vienna/vienna-buildings.x3d
+#	filename ../data/vienna/vienna-buildings.x3d
 #	filename ../data/vienna/viewcells-25-sel.x3d
-#	filename ../data/atlanta/atlanta2.x3d
+	filename ../data/atlanta/atlanta2.x3d
 #	filename ../data/soda/soda.dat
 #	filename ../data/soda/soda5.dat
@@ -56,17 +56,17 @@
 
 Sampling {
-	totalSamples	500000
-	samplesPerPass  10
+	totalSamples	100
+	samplesPerPass  20
 }
 
 ViewCells {
 	hierarchyType bspTree
-	height 7.0
+	height 5.0
 	maxViewCells 0
 	#hierarchyType kdTree
 	#hierarchyType sceneDependent
 #	filename ../data/atlanta/atlanta_viewcells_large.x3d
-	filename ../data/vienna/viewcells-25-sel.x3d
-#	filename ../data/vienna/viewcells-25.x3d
+#	filename ../data/vienna/viewcells-25-sel.x3d
+	filename ../data/vienna/viewcells-25.x3d
 #	filename ../data/vienna/viewcells-large-sel.x3d
 }
@@ -74,8 +74,8 @@
 BspTree {
 #       constructionMethod fromRays
-	constructionMethod fromViewCells
-#	constructionMethod fromSceneGeometry
+#	constructionMethod fromViewCells
+	constructionMethod fromSceneGeometry
 
-	# next polygon         = 1
+	# random polygon       = 1
 	# axis aligned         = 2
 	# least splits         = 4
@@ -101,5 +101,5 @@
 	#splitPlaneStrategy 72
 	
-	splitPlaneStrategy 66
+	splitPlaneStrategy 72
 	
 	maxCandidates 50
@@ -107,6 +107,10 @@
 	Termination {
 		maxPolysForAxisAligned 100
-		maxPolygons 0
-		maxDepth 200
+		maxPolygons 30
+		maxDepth 100
 	}
+	# if split polys are stored for visualization
+	storeSplitPolys false
+	# x3d visualization of the split planes
+	exportSplits true
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 319)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 321)
@@ -77,5 +77,5 @@
 	/** Returns the surface normal.
 	*/
-	Vector3 GetNormal() const;
+	inline Vector3 GetNormal() const;
 
 	/** Includes polygons to axis aligned box.
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 319)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 321)
@@ -524,5 +524,5 @@
 	/// Strategies for choosing next split plane.
 	enum {NO_STRATEGY = 0,
-		  NEXT_POLYGON = 1, 
+		  RANDOM_POLYGON = 1, 
 		  AXIS_ALIGNED = 2,
 		  LEAST_SPLITS = 4, 
@@ -536,7 +536,4 @@
 	/// box around the whole view domain
 	AxisAlignedBox3 mBox;
-
-	/// if polygons should be stored in the tree
-	bool mStoreSplitPolys;
 
 	/// view cell corresponding to unbounded space
@@ -571,4 +568,7 @@
 	static float sLargestPolyAreaFactor;
 	static float sBlockedRaysFactor;
+	
+	/// if polygons should be stored in the tree
+	static bool sStoreSplitPolys;
 
 private:
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 313)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 321)
@@ -1185,4 +1185,9 @@
 		 "100");
 
+  RegisterOption("BspTree.storeSplitPolys",
+		 optBool,
+		 "-bsp_store_split_polys=",
+		 "false");
+
   RegisterOption("BspTree.splitPlaneStrategy",
 		optString,
@@ -1199,4 +1204,9 @@
 	  "-view_cells_max_viewcells=",
 	  "0");
+  
+  RegisterOption("BspTree.exportSplits",
+	  optBool,
+	  "-bsp_exportSplits",
+	  "false");
 }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 319)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 321)
@@ -10,5 +10,4 @@
 mKdTree(NULL),
 mBspTree(NULL),
-mRootViewCell(NULL),
 mViewCellsType(BSP_VIEW_CELLS)
 {
@@ -21,5 +20,4 @@
 	DEL_PTR(mBspTree);
 	DEL_PTR(mKdTree);
-	DEL_PTR(mRootViewCell);
 }
 
@@ -150,7 +148,6 @@
 {
 	DEL_PTR(mBspTree);
-	DEL_PTR(mRootViewCell);
-	mRootViewCell = new ViewCell(NULL);
-	mBspTree = new BspTree(mRootViewCell);
+	
+	mBspTree = new BspTree(&mUnbounded);
 
 	ObjectContainer objects;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 319)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 321)
@@ -10,4 +10,5 @@
 #include "X3dExporter.h" // delete later
 #include "ViewCell.h"
+#include "SceneGraph.h"
 
 #define USE_EXE_PATH false
@@ -59,23 +60,42 @@
 	  p->BspTreeStatistics(Debug);
 	  p->Export("vc_bsptree2.x3d", false, false, true);
+	 
+	  bool exportSplits = false;
+	  environment->GetBoolValue("BspTree.exportSplits", exportSplits);
+
 	  // export the bsp splits
-	  Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
+	  if (exportSplits)
+	  {
+		  Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
 
-	  if (exporter) 
-	  {	
-          Material m; 
-		  m.mDiffuseColor = RgbColor(1, 0, 0);
-		  exporter->SetForcedMaterial(m);
-		  exporter->SetWireframe();//exporter->SetFilled();
-		  exporter->ExportBspSplits(*p->mBspTree);
+		  if (exporter) 
+		  {	
+			  Material m; 
+			  m.mDiffuseColor = RgbColor(1, 0, 0);
+			  exporter->SetForcedMaterial(m);
+			  exporter->SetWireframe();
+			  exporter->ExportBspSplits(*p->mBspTree);
 
-		  //m.mDiffuseColor = RgbColor(0, 1, 0);
-		  //exporter->SetForcedMaterial(m);
-		  exporter->SetFilled();
+			  // take forced material because does not load for big scenes otherwise
+			  m.mDiffuseColor = RgbColor(0, 1, 0);
+			  exporter->SetForcedMaterial(m);
+			  exporter->SetFilled();
 
-		  exporter->ResetForcedMaterial();
-		  exporter->ExportViewCells(p->mViewCells);
+			  exporter->ResetForcedMaterial();
 
-		  delete exporter;
+			  if (1)
+			  {
+				  ObjectContainer objects;
+				  p->mSceneGraph->CollectObjects(&objects);
+                  Material m;//= RandomMaterial();
+				  m.mDiffuseColor = RgbColor(0, 0, 1);
+				  exporter->SetForcedMaterial(m);
+                 
+				  for (int j = 0; j < objects.size(); ++ j)
+					   exporter->ExportIntersectable(objects[j]);
+			
+				  delete exporter;
+			  }
+		  }
 	  }
 #if 0
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 310)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 321)
@@ -8,4 +8,5 @@
 #include "KdTree.h"
 #include "ViewCellBsp.h"
+#include "ViewCell.h"
 //#include "SceneGraph.h"
 
@@ -112,6 +113,6 @@
   BspTree *mBspTree;
 
-  /// the root view cell of the bsp tree
-  ViewCell *mRootViewCell;
+  /// the view cell corresponding to unbounded space
+  ViewCell mUnbounded;
 
   /// view cell hierarchy types
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 319)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 321)
@@ -35,9 +35,4 @@
 Plane3 Polygon3::GetSupportingPlane() const
 {
-	Vector3 v1 = mVertices[0] - mVertices[1];
-	Vector3 v2 = mVertices[2] - mVertices[1];
-#ifdef _DEBUG
-	Debug << "plane spanned by " <<  v1 << ", " << v2  << endl;
-#endif
 	return Plane3(mVertices[0], mVertices[1], mVertices[2]);
 }
@@ -45,6 +40,6 @@
 Vector3 Polygon3::GetNormal() const 
 {
-    return Normalize(CrossProd(mVertices[0] - mVertices[1],
-							   mVertices[2] - mVertices[1]));
+    return Normalize(CrossProd(mVertices[2] - mVertices[1],
+							   mVertices[0] - mVertices[1]));
 }
 
@@ -130,5 +125,4 @@
     
     //Debug << "area2: " << 0.5f * fabs(DotProd(GetNormal(), v)) << endl;  
-
 	return 0.5f * fabs(DotProd(GetNormal(), v));
 }
@@ -170,6 +164,9 @@
 		}
 		// 3 vertices enough to decide coincident
-		else if (((++ count) >= 3) && !onFrontSide && !onBackSide) 
+		else if (((++ count) >= 3) && !onFrontSide && !onBackSide)
+		//	&& (DotProd(GetSupportingPlane().mNormal, plane.mNormal)  > 0))
+		{   
 			return COINCIDENT; 
+		}
 	}
 
@@ -182,5 +179,5 @@
 		return FRONT_SIDE;
 	}
-
+	//if (DotProd(GetNormal(), plane.mNormal)  < 0) return FRONT_SIDE;
 	return COINCIDENT; // plane and polygon are coincident
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 320)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 321)
@@ -66,16 +66,17 @@
 	int j;
  
-	// add object to view cell
+	// object can be seen from the view cell => add to view cell pvs
 	for (j=0; j < ray.viewCells.size(); ++ j) 
-	{ 
-		ViewCell *vc = ray.viewCells[j];
-
-		contributingSamples += vc->GetPvs().AddSample(obj);
+	{ 	// if ray not in unbounded space
+		if (ray.viewCells[j] != &mUnbounded)
+			contributingSamples += ray.viewCells[j]->GetPvs().AddSample(obj);
 	}
  
+	// rays passing through this viewcell
 	if (mPass > 1)
 		for (j=1; j < ((int)ray.viewCells.size() - 1); ++ j) 
 		{
-            ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
+			if (ray.viewCells[j] != &mUnbounded)
+				ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
 		}
  
@@ -103,9 +104,9 @@
 	int sampleContributions = 0;
 
+	// cast ray to KD tree to find intersection with other objects
+	mKdTree->CastRay(ray);
+
 	if (mViewCellsType == BSP_VIEW_CELLS)
 	{
-		// cast ray to KD tree to find intersection with other objects
-		mKdTree->CastRay(ray);
-		
 		// cast ray to BSP tree to get intersection with view cells
 		mBspTree->CastRay(ray);
@@ -121,6 +122,4 @@
 	else
 	{
-		mKdTree->CastRay(ray);
-	
 		if (ray.leaves.size()) {
 			sampleContributions += AddNodeSamples(object, ray);
@@ -233,4 +232,14 @@
   int vcPvsOut = Min((int)mViewCells.size(), 5);
 
+  ViewCellContainer pvsViewCells;
+
+  // some random view cells for output
+  for (int j = 0; j < vcPvsOut; ++ j)
+  {
+	  int idx = Random((int)mViewCells.size());
+	  Debug << "output view cell no. " << idx << endl;
+	  pvsViewCells.push_back(mViewCells[Random((int)mViewCells.size())]);
+  }
+
   vector<Ray> rays[10];
   vector<Ray> vcRays[5];
@@ -245,4 +254,6 @@
 		int index = 0;
 			
+		Debug << "starting new loop " << totalSamples << endl;
+
 		// construct Bsp tree if not
 		if ((mViewCellsType == Preprocessor::BSP_VIEW_CELLS) &&
@@ -251,5 +262,5 @@
 			BuildBspTree();
 		}
-
+Debug << "here" << totalSamples << endl;
 		for (i = 0; i < objects.size(); i++) {
 			KdNode *nodeToSample = NULL;
@@ -351,6 +362,6 @@
 						// check whether we can add this to the rays
 						for (int k = 0; k < ray.viewCells.size(); ++ k)
-							for (int j = 0; j < vcPvsOut; ++ j) 
-								if (mViewCells[j] == ray.viewCells[k]) 
+							for (int j = 0; j < (int)pvsViewCells.size(); ++ j) 
+								if (pvsViewCells[j] == ray.viewCells[k]) 
 								{
 									vcRays[j].push_back(ray);
@@ -465,7 +476,7 @@
       if (mViewCellsType == BSP_VIEW_CELLS)
 	  {
-		   for (int j = 0; j < vcPvsOut; ++ j)
+		   for (int j = 0; j < pvsViewCells.size(); ++ j)
 		   {
-			   ViewCell *vc = mViewCells[j];
+			   ViewCell *vc = pvsViewCells[j];
 
 			   Intersectable::NewMail();
@@ -491,5 +502,10 @@
 			   exporter->SetForcedMaterial(m);
 
-			   exporter->ExportBspTree(*mBspTree);
+			   if (1)
+			   {
+				   exporter->ExportViewCells(mViewCells);
+			   }
+
+			   // export rays piercing this view cell
 			   exporter->ExportRays(vcRays[j], 1000, RgbColor(0, 1, 0));
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 318)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 321)
@@ -1657,5 +1657,5 @@
 	bool operator<(const VertexData &b) const 
 	{
-		return mAngle < b.mAngle;
+		return mAngle > b.mAngle;
 	}
 };
