Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 448)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 449)
@@ -337,4 +337,5 @@
 {
 	DEL_PTR(mRoot);
+	DEL_PTR(mSplitCandidates);
 }
 
@@ -446,9 +447,9 @@
 VspKdTree::Construct(const VssRayContainer &rays,
 					 AxisAlignedBox3 *forcedBoundingBox)
-{
+{Debug << "here888" << endl;
 	mStat.Start();
-  
+  Debug << "here1000" << endl;
 	mMaxMemory = mMaxStaticMemory;
-
+	Debug << "here1" << endl;
 	DEL_PTR(mRoot);
 
@@ -460,5 +461,5 @@
 	mStat.nodes = 1;
   	mBox.Initialize();
-
+Debug << "here2" << endl;
 	//-- compute bounding box
 	if (forcedBoundingBox)
@@ -899,5 +900,5 @@
 		requestedSize < (int)(mSplitCandidates->capacity()/10) ) 
 	{
-    	delete mSplitCandidates;
+    	DEL_PTR(mSplitCandidates);
    		mSplitCandidates = new vector<SortableEntry>;
 	}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.h	(revision 448)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.h	(revision 449)
@@ -9,4 +9,5 @@
 class ViewCell;
 class KdLeaf;
+class VspKdTree;
 class VspBspTree;
 
@@ -151,3 +152,26 @@
 };
 
+class VspKdRenderSimulator: public RenderSimulator
+{
+public:
+	VspKdRenderSimulator(VspKdTree *vspKdTree);
+
+	VspKdRenderSimulator(float objRenderCost, 
+						 float vcOverhead, 
+						 float moveSpeed, 
+						 VspKdTree *vspKdTree);
+
+	SimulationStatistics SimulateRendering();
+
+protected:
+	/** Simulates rendering of the pvs of one view cell, with given rendering time for an object.
+		@param viewCell the view cell holding the Pvs
+		@param objRenderTime estimated render time for one object of the Pvs
+	*/
+	Real RenderPvs(ViewCell &viewCell, const float objRenderTime) const;
+
+private:
+	VspKdTree *mVspKdTree;
+};
+
 #endif // RenderSimulator
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 448)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 449)
@@ -4,5 +4,5 @@
 #include "ViewCell.h"
 #include "VspBspTree.h"
-
+#include "VspKdTree.h"
 
 void SimulationStatistics::Print(ostream &app) const
@@ -148,7 +148,7 @@
 }
 
-/********************************************************
- *     class KdLeafRenderSimulator implementation       *
- *******************************************************/
+/*******************************************************/
+/*     class KdenderSimulator implementation           */
+/*******************************************************/
 
 KdRenderSimulator::KdRenderSimulator(float objRenderCost, 
@@ -242,4 +242,5 @@
 /*     class BspRenderSimulator implementation          */
 /********************************************************/
+
 VspBspRenderSimulator::VspBspRenderSimulator(VspBspTree *vspBspTree):
 mVspBspTree(vspBspTree)
@@ -334,2 +335,42 @@
 	return viewCell.GetPvs().GetSize() * objRenderTime;
 }
+
+
+
+/********************************************************/
+/*     class BspRenderSimulator implementation          */
+/********************************************************/
+
+
+VspKdRenderSimulator::VspKdRenderSimulator(VspKdTree *vspKdTree):
+mVspKdTree(vspKdTree)
+{
+}
+
+VspKdRenderSimulator::VspKdRenderSimulator(float objRenderCost, 
+										   float vcOverhead, 
+										   float moveSpeed,
+										   VspKdTree *vspKdTree):
+RenderSimulator(objRenderCost, vcOverhead, moveSpeed), 
+mVspKdTree(vspKdTree)
+{
+}
+
+SimulationStatistics VspKdRenderSimulator::SimulateRendering()
+{
+	SimulationStatistics simStat;
+
+	simStat.Reset();
+	simStat.Start();
+
+	// TODO
+	simStat.Stop();
+
+	return simStat;
+}
+
+Real VspKdRenderSimulator::RenderPvs(ViewCell &viewCell, 
+									  float objRenderTime) const
+{
+	return 0; // TODO
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 446)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 449)
@@ -11,5 +11,6 @@
 #include "VspKdTree.h"
 #include "VssTree.h"
-#include "RssTree.h"
+#include "VspBspTree.h"
+
 
 X3dExporter::X3dExporter(const string filename):Exporter(filename)
@@ -285,5 +286,9 @@
 			BspNodeGeometry *back = new BspNodeGeometry();
 
-			cell->SplitGeometry(*front, *back, tree, *interior->GetPlane());
+			cell->SplitGeometry(*front, 
+								*back, 
+								interior->GetPlane(), 
+								tree.GetBoundingBox(), 
+								tree.GetEpsilon());
 
 			tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetFront(), front));
@@ -872,91 +877,4 @@
 }
 
-
-bool
-X3dExporter::ExportRssTree2(const RssTree &tree,
-							const Vector3 direction
-							)
-{
-  stack<RssTreeNode *> tStack;
-  
-  
-  mUseForcedMaterial = true;
-
-  Vector3 dirParam;
-
-  dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
-  dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
-
-  float maxImportance = 0.0f;
-  tStack.push(tree.GetRoot());
-  while (!tStack.empty()) {
-	
-	RssTreeNode *node = tStack.top();
-    tStack.pop();
-	
-    if (!node->IsLeaf()) {
-      RssTreeInterior *interior = (RssTreeInterior *)node;
-	  if (interior->axis < 3) {
-		tStack.push(interior->front);
-		tStack.push(interior->back);
-	  } else {
-		if (dirParam[interior->axis-3] < interior->position)
-		  tStack.push(interior->back);
-		else
-		  tStack.push(interior->front);
-	  }
-    } else {
-	  RssTreeLeaf *leaf = (RssTreeLeaf *)node;
-	  if (tree.ValidLeaf(leaf)) {
-		float i = leaf->GetImportance();
-		if (i > maxImportance)
-		  maxImportance = i;
-	  }
-	}
-  }
-
-  tStack.push(tree.GetRoot());
-  while (!tStack.empty()) {
-
-	RssTreeNode *node = tStack.top();
-    tStack.pop();
-	
-			
-    if (!node->IsLeaf()) {
-      RssTreeInterior *interior = (RssTreeInterior *)node;
-	  if (interior->axis < 3) {
-		tStack.push(interior->front);
-		tStack.push(interior->back);
-	  } else {
-		if (dirParam[interior->axis-3] < interior->position)
-		  tStack.push(interior->back);
-		else
-		  tStack.push(interior->front);
-	  }
-    } else {
-	  RssTreeLeaf *leaf = (RssTreeLeaf *)node;
-	  if (tree.ValidLeaf(leaf)) {
-		AxisAlignedBox3 box;
-		box = tree.GetShrankedBBox(leaf);
-		Mesh *mesh = new Mesh;
-		AddBoxToMesh(box, mesh);
-		
-		// get 4 corners of the ray directions
-		
-		mForcedMaterial.mDiffuseColor.b = 1.0f;
-		mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
-		mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
-		
-		ExportMesh(mesh);
-		delete mesh;
-	  }
-	}
-  }
-
-  mUseForcedMaterial = false;
-
-  return true;
-}
-
 bool
 X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
@@ -1070,5 +988,5 @@
 	BspNode *mNode;
 
-	vector<Plane3 *> mPlanes;
+	vector<Plane3> mPlanes;
 	vector<bool> mSides;
 	bool mIsFront;
@@ -1080,5 +998,5 @@
 
 	BspSplitData(BspNode *node, 
-				vector<Plane3 *> planes, 
+				vector<Plane3> planes, 
 				vector<bool> sides, 
 				const bool isFront,
@@ -1105,10 +1023,12 @@
 }
 
-void X3dExporter::ExportBspSplits(const BspTree &tree,
-								  const bool exportDepth)
+void X3dExporter::ExportBspNodeSplits(BspNode *root,
+									  const AxisAlignedBox3 &box, 
+									  const bool exportDepth,
+									  const bool epsilon)
 {
 	std::stack<BspSplitData> tStack;
 
-	BspSplitData tData(tree.GetRoot());
+	BspSplitData tData(root);
 	tStack.push(tData);
   
@@ -1134,20 +1054,25 @@
 
 			// add current side of split plane
-			if (tData.mNode != tree.GetRoot())
+			if (tData.mNode != root)
 				tData.mSides.push_back(tData.mIsFront);
 
 			// bounded plane is added to the polygons
 			Polygon3 *planePoly = 
-				tree.GetBoundingBox().CrossSection(*interior->GetPlane());
+				box.CrossSection(interior->GetPlane());
 		
 			// do all the splits with the previous planes
 			for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
 			{				
-				if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT)
+				if (planePoly->ClassifyPlane(tData.mPlanes[i], epsilon) 
+					== Polygon3::SPLIT)
 				{
 					Polygon3 *frontPoly = new Polygon3();
 					Polygon3 *backPoly = new Polygon3();
 
-					planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly);
+					planePoly->Split(tData.mPlanes[i], 
+									 *frontPoly, 
+									 *backPoly,
+									 epsilon);
+
 					DEL_PTR(planePoly);
 
@@ -1167,5 +1092,5 @@
 			tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
 
-			if (planePoly->Valid())
+			if (planePoly->Valid(epsilon))
 			{
 				polys.push_back(planePoly);
@@ -1206,4 +1131,22 @@
 }
 
+void X3dExporter::ExportBspSplits(const BspTree &tree,
+								  const bool exportDepth)
+{
+	ExportBspNodeSplits(tree.GetRoot(), 
+						tree.GetBoundingBox(), 
+						exportDepth, 
+						tree.GetEpsilon());
+}
+
+void X3dExporter::ExportBspSplits(const VspBspTree &tree,
+								  const bool exportDepth)
+{
+	ExportBspNodeSplits(tree.GetRoot(), 
+						tree.GetBoundingBox(), 
+						exportDepth, 
+						tree.GetEpsilon());
+}
+
 void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
 {
@@ -1225,5 +1168,5 @@
 
 			// bounded plane is added to the polygons
-			polys.push_back(tree.GetBoundingBox().CrossSection(*interior->GetPlane()));
+			polys.push_back(tree.GetBoundingBox().CrossSection(interior->GetPlane()));
 		
 			// push the children on the stack
@@ -1238,13 +1181,41 @@
 
 
-void
-X3dExporter::ExportGeometry(const ObjectContainer &objects)
-{
-  
-  ObjectContainer::const_iterator oi = objects.begin();
-  for (; oi != objects.end(); oi++) {
-    // export the transform...
-    ExportIntersectable(*oi);
-  }
-  
-}
+void X3dExporter::ExportGeometry(const ObjectContainer &objects)
+{
+	for (int j = 0; j < objects.size(); ++ j)
+		ExportIntersectable(objects[j]);
+}
+
+void X3dExporter::ExportBspViewCellPartition(const VspBspTree &tree, 
+											 const int maxPvs)
+{
+	ViewCellContainer viewCells;
+	tree.CollectViewCells(viewCells);
+
+	ViewCellContainer::const_iterator it, it_end = viewCells.end();
+
+	if (maxPvs > 0)
+		mUseForcedMaterial = true;
+
+	for (it = viewCells.begin(); it != it_end; ++ it)
+	{
+		if (maxPvs > 0)
+		{
+			mForcedMaterial.mDiffuseColor.b = 1.0f;
+  			float importance = (float)(*it)->GetPvs().GetSize() / (float)maxPvs;
+
+			mForcedMaterial.mDiffuseColor.r = importance;
+			mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
+		}
+
+		if ((*it)->GetMesh())
+			ExportViewCell(*it);
+		else
+		{
+			PolygonContainer cell;
+			tree.ConstructGeometry(dynamic_cast<BspViewCell *>(*it), cell);
+
+			ExportPolygons(cell);
+		}
+	}
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 448)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 449)
@@ -972,5 +972,5 @@
 mVspKdTree(vspKdTree)
 {
-   mRenderSimulator = NULL;//new VspKdRenderSimulator(vspKdTree);
+   mRenderSimulator = new VspKdRenderSimulator(vspKdTree);
    InitRenderSimulator();
 }
@@ -1097,5 +1097,4 @@
 
 			//-- export stored PVS
-			
 			ObjectContainer pvsObj;
 			leaf->ExtractPvs(pvsObj);
