Index: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 238)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 242)
@@ -59,4 +59,7 @@
 	/// vertices are connected in counterclockwise order.
 	VertexContainer mVertices;
+
+	/// we can also store materials with polygons
+	Material *mMaterial;
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 241)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 242)
@@ -4,7 +4,4 @@
 #include "Mesh.h"
 #include "Containers.h"
-
-
-#include <queue>
 #include <stack>
 
@@ -12,9 +9,11 @@
 class Plane3;
 //class Mesh;
-
-//namespace GtpVisibilityPreprocessor {
 class BspTree;  
 class BspInterior;
 class Polygon3;
+class AxisAlignedBox3;
+
+
+//namespace GtpVisibilityPreprocessor {
 
 /** Container storing a soup of polygons used during BSP tree construction
@@ -245,7 +244,15 @@
 	void Construct(const ObjectContainer &objects);
 
-	int BspTree::CollectLeafPvs();
+	int CollectLeafPvs();
 
 	void CollectLeaves(vector<BspLeaf *> &leaves);
+
+	/** Returns box which bounds the whole tree.
+	*/
+	AxisAlignedBox3 GetBoundingBox()const;
+
+	/** Returns root of BSP tree.
+	*/
+	BspNode *GetRoot() const;
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 191)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 242)
@@ -9,4 +9,5 @@
 
 class KdTree;
+class BspTree;
 class SceneGraphNode;
 class Ray;
@@ -43,4 +44,7 @@
 
   virtual bool
+  ExportBspTree(const BspTree &tree) = 0;
+
+  virtual bool
   ExportRays(const vector<Ray> &rays,
 	     const float length=1000,
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 241)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 242)
@@ -7,5 +7,5 @@
 #include "Polygon3.h"
 #include "Ray.h"
-
+#include "AxisAlignedBox3.h"
 #include <stack>
 #include <time.h>
@@ -110,5 +110,5 @@
 		{
 			case Polygon3::COINCIDENT:
-				break; // do nothing
+				break; // TODO: compare normals
 			case Polygon3::FRONT_SIDE:
 				frontPolys->push_back(poly);
@@ -676,4 +676,14 @@
 }
 
+AxisAlignedBox3 BspTree::GetBoundingBox() const
+{
+	return mBox;
+}
+
+BspNode *BspTree::GetRoot() const
+{
+	return mRoot;
+}
+
 void BspTree::EvaluateLeafStats(const BspTraversalData &data)
 {
@@ -723,5 +733,5 @@
 	
 	float position;
-
+	
 	while (1) // endless loop
 	{
@@ -742,47 +752,33 @@
 			if (entSide < 0)
 			{
-				if(extSide > 0) // plane splits ray
-				{
-					node = in->GetBack();
-					farChild = in->GetFront();
-				} else
-				{
-					node = in->GetBack();
+				node = in->GetBack();
+
+				if(extSide <= 0) 
 					continue;
-				} 
-			}
+					
+				farChild = in->GetFront(); // plane splits ray
+
 			} else if (entSide > 0)
 			{
-				if (extSide < 0) // plane splits ray
-				{
-					node = in->GetFront();
-					farChild = in->GetBack(); 
-				} else
-				{
-					node = in->GetFront();
+				node = in->GetFront();
+
+				if (extSide >= 0)
 					continue;
-				}
+
+				farChild = in->GetBack();	 // plane splits ray			
 			}
-			else // ray and plane are coincident // DOTO: WHAT IN THIS CASE ?
+			else // ray and plane are coincident // WHAT TO DO IN THIS CASE ?
 			{
-				//node = in->GetFront();
+				node = in->GetFront();
 				continue;
 			}
 
-		    // case N4 or P4
-			float tdist = (position - ray.GetLoc(axis)) / ray.GetDir(axis);
-      tStack.push(RayTraversalData(farChild, extp, maxt));
-      extp = ray.GetLoc() + ray.GetDir()*tdist;
-      maxt = tdist;
-			
-			float tDist = 0;
-			extp = splitPlane->FindIntersection(entp, extp, &t);
-
+			//-- split
+
+			// push data for far child
 			tStack.push(BspRayTraversalData(farChild, extp, maxt));
 
-			
-
-				maxt = tDist;
-			}
+			// find intersection with plane between ray origin and exit point
+			extp = splitPlane->FindIntersection(ray.GetLoc(), extp, &maxt);
 		} else // compute intersections with objects in leaf
 		{
@@ -813,4 +809,5 @@
 			mint = maxt;
 			BspRayTraversalData &s  = tStack.top();
+
 			node = s.mNode;
 			extp = s.mExitPoint;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 238)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 242)
@@ -121,5 +121,6 @@
 Preprocessor::Export( const string filename,
 		      const bool scene,
-		      const bool kdtree
+		      const bool kdtree,
+			  const bool bsptree
 		      )
 {
@@ -135,4 +136,9 @@
     }
 
+	if (bsptree) {
+		exporter->SetWireframe();
+		exporter->ExportBspTree(*mBspTree);
+	}
+
     delete exporter;
     return true;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 191)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 242)
@@ -14,4 +14,7 @@
 class Intersectable;
 class MeshInstance;
+class Polygon3;
+class ViewCell;
+class BspTree;
 
 class X3dExporter : public Exporter
@@ -34,4 +37,6 @@
   ExportKdTree(const KdTree &tree);
   
+  bool ExportBspTree(const BspTree &tree);
+
   bool
   ExportScene(SceneGraphNode *root) 
@@ -41,4 +46,7 @@
   }
   
+  virtual void 
+	ExportPolygon(Polygon3 *poly);
+
   virtual bool
   ExportBox(const AxisAlignedBox3 &box);
@@ -53,4 +61,5 @@
   ExportMesh(Mesh *mesh);
 
+  virtual void ExportViewCell(ViewCell *viewCell);
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 237)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 242)
@@ -38,6 +38,6 @@
   //  p->mSceneGraph->Export("soda.x3d");
   if (0) {
-    p->Export(filename + "-out.x3d", true, false);
-    p->Export(filename + "-kdtree.x3d", false, true);
+    p->Export(filename + "-out.x3d", true, false, false);
+    p->Export(filename + "-kdtree.x3d", false, true, false);
   }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 209)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 242)
@@ -5,4 +5,7 @@
 #include "Mesh.h"
 #include "KdTree.h"
+#include "ViewCellBsp.h"
+#include "ViewCell.h"
+#include "Polygon3.h";
 
 
@@ -98,4 +101,7 @@
   case Intersectable::TRANSFORMED_MESH_INSTANCE:
     ExportMeshInstance((MeshInstance *)object);
+	break;
+  case Intersectable::VIEWCELL:
+	ExportViewCell((ViewCell *)object);
     break;
   default:
@@ -112,4 +118,11 @@
   // and use a reference to the that mesh instead
   ExportMesh(object->GetMesh());
+}
+
+void
+X3dExporter::ExportViewCell(ViewCell *object)
+{
+	if (object->GetMesh())
+		ExportMesh(object->GetMesh());
 }
 
@@ -183,4 +196,65 @@
 }
 
+
+void X3dExporter::ExportPolygon(Polygon3 *poly)
+{
+	stream << "<Shape>" << endl;
+	stream << "<Appearance>" << endl;
+  
+	// $$ tmp -> random material
+  
+	float r, g, b;
+
+	if (mUseForcedMaterial) 
+	{
+		r = mForcedMaterial.mDiffuseColor.r;
+		g = mForcedMaterial.mDiffuseColor.g;
+		b = mForcedMaterial.mDiffuseColor.b;
+	} 
+	else if (poly->mMaterial) 
+	{
+		r = poly->mMaterial->mDiffuseColor.r;
+		g = poly->mMaterial->mDiffuseColor.g;
+		b = poly->mMaterial->mDiffuseColor.b;
+	} else 
+	{
+		r = RandomValue(0.5, 1.0);
+		g = RandomValue(0.5, 1.0);
+		b = RandomValue(0.5, 1.0);
+	}
+
+	stream << "<Material diffuseColor=\"" << r << " " << g << " " << b 
+		   << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
+
+    stream << "</Appearance>" << endl;
+
+
+	if (mWireframe)
+		stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
+	else
+		stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
+
+	VertexContainer::const_iterator vi = poly->mVertices.begin();
+
+	stream << "<Coordinate  point=\"" << endl;
+  
+  	for (; vi != poly->mVertices.end(); ++vi) 
+	{
+		stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
+		stream << "," << endl;
+	}
+  
+	stream<<"\" >"<<endl;
+	stream<<"</Coordinate>"<<endl;
+
+	if (mWireframe)
+		stream << "</IndexedLineSet>" << endl;
+	else
+		stream << "</IndexedFaceSet>" << endl;
+  
+	stream << "</Shape>" << endl;
+}
+
+
 bool
 X3dExporter::ExportBox(const AxisAlignedBox3 &box)
@@ -209,7 +283,50 @@
 
 bool
-X3dExporter::ExportKdTree(const KdTree &tree)
-{
-  if (mExportRayDensity) {
+X3dExporter::ExportBspTree(const BspTree &tree)
+{
+	if (mExportRayDensity) 
+	{
+		return false;//ExportKdTreeRayDensity(tree);
+	}
+  
+	stack<BspNode *> tStack;
+
+	tStack.push(tree.GetRoot());
+
+	Mesh *mesh = new Mesh;
+
+	AxisAlignedBox3 box = tree.GetBoundingBox();
+	ExportBox(box);
+
+  	while (!tStack.empty()) 
+	{
+		BspNode *node = tStack.top();
+    
+		tStack.pop();
+	
+		if (!node->IsLeaf()) 
+		{
+			BspInterior *interior = dynamic_cast<BspInterior *>(node);
+      
+			tStack.push(interior->GetFront());
+			tStack.push(interior->GetBack());
+		}
+		else
+		{
+			BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
+			// NOTE: could also export polygons to see if splits work
+			if (leaf->GetViewCell())
+				ExportViewCell(leaf->GetViewCell());
+		}
+  }
+  
+  ExportMesh(mesh);
+  delete mesh;
+  return true;
+}
+
+bool X3dExporter::ExportKdTree(const KdTree &tree)
+{
+	 if (mExportRayDensity) {
     return ExportKdTreeRayDensity(tree);
   }
@@ -250,4 +367,6 @@
   delete mesh;
   return true;
+	// TODO
+	return true;
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 235)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 242)
@@ -76,5 +76,6 @@
   Export( const string filename,
 	  const bool scene,
-	  const bool kdtree
+	  const bool kdtree,
+	  const bool bsptree
 	  );
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 239)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 242)
@@ -2,10 +2,10 @@
 #include "Mesh.h"
 
-#define SIDE_TOLERANCE 0.002
+#define SIDE_TOLERANCE 0.002 // TODO: Test different values
 
-Polygon3::Polygon3()
+Polygon3::Polygon3(): mMaterial(NULL)
 {}
 
-Polygon3::Polygon3(const VertexContainer &vertices): mVertices(vertices) 
+Polygon3::Polygon3(const VertexContainer &vertices): mVertices(vertices), mMaterial(NULL)
 {}
 
@@ -17,4 +17,6 @@
 	{
 		mVertices.push_back(parent->mVertices[*it]);
+		mMaterial = parent->mMaterial;
+		
 		Debug << parent->mVertices[*it] << endl;
 	}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 223)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 242)
@@ -238,4 +238,6 @@
     exporter->SetExportRayDensity(true);
     exporter->ExportKdTree(*mKdTree);
+	exporter->ExportBspTree(*mBspTree);
+
     delete exporter;
   }
@@ -247,4 +249,5 @@
     exporter->SetWireframe();
     exporter->ExportKdTree(*mKdTree);
+	exporter->ExportBspTree(*mBspTree);
 
     for (i=0; i < pvsOut; i++) 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp	(revision 191)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp	(revision 242)
@@ -97,4 +97,5 @@
     exporter->SetWireframe();
     exporter->ExportKdTree(*tree);
+//	exporter->ExportBspTree(*tree);
     exporter->ExportRays(rays, 10000);
     exporter->SetFilled();
