Index: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 384)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 396)
@@ -99,4 +99,7 @@
 					 Polygon3 &back_piece) const;
 
+	/** Returns new polygon with reverse orientation.
+	*/
+	Polygon3 *CreateReversePolygon() const;
 
 	/// vertices are connected in counterclockwise order.
@@ -111,5 +114,4 @@
 	/// Rays piercing this polygon
 	RayContainer mPiercingRays;
-
 
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 392)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 396)
@@ -20,11 +20,5 @@
 public:
 	BspNodeGeometry()
-	{};
-
-	BspNodeGeometry(const PolygonContainer &polys, 
-					const vector<Plane3> &planes, 
-					const vector<bool> &sides);
-	BspNodeGeometry(const vector<Plane3> &planes, 
-					const vector<bool> &sides);	   
+	{};  
 
 	~BspNodeGeometry();
@@ -34,12 +28,12 @@
 	/** Computes new cell based on the old cell definition and a new split plane
 		@param side indicates which side of the halfspace 
-		@returns true if plane contributes to the cell.
-	*/
-	BspNodeGeometry *ConstructChild(const BspTree &tree,
-									const Plane3 &splitPlane,
-									const bool side) const;
-
-	vector<Plane3> mPlanes;
-	vector<bool> mSides;
+	*/
+	void SplitGeometry(BspNodeGeometry &front, 
+					   BspNodeGeometry &back,
+					   const BspTree &tree,
+					   const Plane3 &splitPlane) const;
+
+	Polygon3 *SplitPolygon(Polygon3 *poly, const BspTree &tree) const;
+
 	PolygonContainer mPolys;
 };
@@ -732,5 +726,5 @@
 	/** Extracts the split planes representing the space bounded by node n.
 	*/
-	void ExtractSplitPlanes(BspNode *n, vector<Plane3> &planes, vector<bool> &sides) const;
+	void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
 
 	/** Computes the pvs of the front and back leaf with a given classification.
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 386)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 396)
@@ -88,5 +88,5 @@
 
   virtual void 
-  ExportBspLeaves(const BspTree &tree) = 0;
+  ExportBspLeaves(const BspTree &tree, const int maxPvs = 0) = 0;
 
   void SetExportRayDensity(const bool d) { mExportRayDensity = d; }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 394)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 396)
@@ -697,6 +697,6 @@
 	//-- terminate traversal  
 	if (((int)tData.mPolygons->size() <= sTermMaxPolygons) || 
-		((int)tData.mRays->size() <= sTermMaxRays)         ||
-		((int)tData.mPvs <= sTermMinPvs)            ||
+		((int)tData.mRays->size() <= sTermMaxRays) ||
+		(tData.mPvs <= sTermMinPvs) ||
 		 (tData.mDepth >= sTermMaxDepth))
 		
@@ -1343,7 +1343,9 @@
 		{	
 			// construct child geometry with regard to the candidate split plane
-			frontData.mCell = cell.ConstructChild(*this, candidatePlane, true);
-			backData.mCell = cell.ConstructChild(*this, candidatePlane, false);
-
+			frontData.mCell = new BspNodeGeometry();
+			backData.mCell = new BspNodeGeometry();
+
+			cell.SplitGeometry(*frontData.mCell, *backData.mCell, *this, candidatePlane);
+		
 			pFront = frontData.mArea = frontData.mCell->GetArea();
 			pBack = backData.mArea = backData.mCell->GetArea();
@@ -2008,7 +2010,5 @@
 }
 
-void BspTree::ExtractSplitPlanes(BspNode *n, 
-								 vector<Plane3> &planes, 
-								 vector<bool> &sides) const
+void BspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const
 {
 	BspNode *lastNode;
@@ -2024,7 +2024,10 @@
 		{
 			BspInterior *interior = dynamic_cast<BspInterior *>(n);
-
-			planes.push_back(* dynamic_cast<BspInterior *>(interior)->GetPlane());
-			sides.push_back(interior->mFront == lastNode);
+			Plane3 halfSpace = *dynamic_cast<BspInterior *>(interior)->GetPlane();
+
+            if (interior->mFront != lastNode)
+				halfSpace.ReverseOrientation();
+
+			halfSpaces.push_back(halfSpace);
 		}
 	}
@@ -2034,17 +2037,38 @@
 void BspTree::ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const
 {
-	stack<BspNode *> tStack;
-
-	vector<Plane3> planes;
-	vector<bool> sides;
-
-	ExtractSplitPlanes(n, cell.mPlanes, cell.mSides);
+	PolygonContainer polys;
+	ConstructGeometry(n, polys);
+	cell.mPolys = polys;
+}
+
+void BspTree::ConstructGeometry(BspViewCell *vc, PolygonContainer &cell) const
+{
+	vector<BspLeaf *> leaves = vc->mBspLeaves;
+
+	vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
+
+	for (it = leaves.begin(); it != it_end; ++ it)
+		ConstructGeometry(*it, cell);
+}
+
+
+void BspTree::ConstructGeometry(BspNode *n, PolygonContainer &cell) const
+{
+	vector<Plane3> halfSpaces;
+	ExtractHalfSpaces(n, halfSpaces);
 
 	PolygonContainer candidatePolys;
 
-	// bounded planes are added to the polygons
-	for (int i = 0; i < (int)cell.mPlanes.size(); ++ i)
-	{
-		candidatePolys.push_back(GetBoundingBox().CrossSection(cell.mPlanes[i]));
+	// bounded planes are added to the polygons (reverse polygons
+	// as they have to be outfacing
+	for (int i = 0; i < (int)halfSpaces.size(); ++ i)
+	{
+		Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]);
+		
+		if (p->Valid())
+		{
+			candidatePolys.push_back(p->CreateReversePolygon());
+			DEL_PTR(p);
+		}
 	}
 
@@ -2062,10 +2086,8 @@
 	for (int i = 0; i < (int)candidatePolys.size(); ++ i)
 	{
-		bool inside = true;
-
 		// polygon is split by all other planes
-		for (int j = 0; (j < cell.mPlanes.size()) && inside; ++ j)
-		{
-			if (i == j) // same plane
+		for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
+		{
+			if (i == j) // polygon and plane are coincident
 				continue;
 
@@ -2073,5 +2095,5 @@
 			Polygon3 *frontPoly, *backPoly;
 
-			const int cf = candidatePolys[i]->ClassifyPlane(cell.mPlanes[j]);
+			const int cf = candidatePolys[i]->ClassifyPlane(halfSpaces[j]);
 			
 			switch (cf)
@@ -2080,32 +2102,23 @@
 					frontPoly = new Polygon3();
 					backPoly = new Polygon3();
-				
-					candidatePolys[i]->Split(cell.mPlanes[j], *frontPoly, 
+
+					candidatePolys[i]->Split(halfSpaces[j], *frontPoly, 
 											 *backPoly, splitPts);
+
 					DEL_PTR(candidatePolys[i]);
 
-					if(sides[j] == true)
-					{
+					if (frontPoly->Valid())
 						candidatePolys[i] = frontPoly;
-						DEL_PTR(backPoly);
-					}
 					else
-					{
-						candidatePolys[i] = backPoly;
 						DEL_PTR(frontPoly);
-					}
-					
+
+					DEL_PTR(backPoly);
 					break;
-
 				case Polygon3::BACK_SIDE:
-					if (cell.mSides[j]) 
-						inside = false;
+					DEL_PTR(candidatePolys[i]);
 					break;
+				// just take polygon as it is
 				case Polygon3::FRONT_SIDE:
-					if (!cell.mSides[j])
-						inside = false;
-					break;
 				case Polygon3::COINCIDENT:
-					break;
 				default:
 					break;
@@ -2113,108 +2126,9 @@
 		}
 		
-		if (inside)
-			cell.mPolys.push_back(candidatePolys[i]);
-		else
-			DEL_PTR(candidatePolys[i]);
-	}
-}
-
-void BspTree::ConstructGeometry(BspNode *n, PolygonContainer &cell) const
-{
-	stack<BspNode *> tStack;
-
-	vector<Plane3> planes;
-	vector<bool> sides;
-
-	ExtractSplitPlanes(n, planes, sides);
-
-	PolygonContainer candidatePolys;
-
-	// bounded planes are added to the polygons
-	for (int i = 0; i < (int)planes.size(); ++ i)
-	{
-		candidatePolys.push_back(GetBoundingBox().CrossSection(planes[i]));
-	}
-
-	// add faces of bounding box (also could be faces of the cell)
-	for (int i = 0; i < 6; ++ i)
-	{
-		VertexContainer vertices;
-	
-		for (int j = 0; j < 4; ++ j)
-			vertices.push_back(mBox.GetFace(i).mVertices[j]);
-
-		candidatePolys.push_back(new Polygon3(vertices));
-	}
-
-	for (int i = 0; i < (int)candidatePolys.size(); ++ i)
-	{
-		bool inside = true;
-
-		// polygon is split by all other planes
-		for (int j = 0; (j < planes.size()) && inside; ++ j)
-		{
-			if (i == j) // same plane
-				continue;
-
-			VertexContainer splitPts;
-			Polygon3 *frontPoly, *backPoly;
-
-			const int cf = candidatePolys[i]->ClassifyPlane(planes[j]);
-			
-			switch (cf)
-			{
-				case Polygon3::SPLIT:
-					frontPoly = new Polygon3();
-					backPoly = new Polygon3();
-
-					candidatePolys[i]->Split(planes[j], *frontPoly, 
-											 *backPoly, splitPts);
-					DEL_PTR(candidatePolys[i]);
-
-					if(sides[j] == true)
-					{
-						candidatePolys[i] = frontPoly;
-						DEL_PTR(backPoly);
-					}
-					else
-					{
-						candidatePolys[i] = backPoly;
-						DEL_PTR(frontPoly);
-					}
-					
-					break;
-
-				case Polygon3::BACK_SIDE:
-					if (sides[j]) 
-						inside = false;
-					break;
-				case Polygon3::FRONT_SIDE:
-					if (!sides[j])
-						inside = false;
-					break;
-				case Polygon3::COINCIDENT:
-					break;
-				default:
-					break;
-			}
-		}
-		
-		if (inside)
+		if (candidatePolys[i])
 			cell.push_back(candidatePolys[i]);
-		else
-			DEL_PTR(candidatePolys[i]);
-	}
-}
-
-void BspTree::ConstructGeometry(BspViewCell *vc, PolygonContainer &cell) const
-{
-	vector<BspLeaf *> leaves = vc->mBspLeaves;
-
-	vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
-
-	for (it = leaves.begin(); it != it_end; ++ it)
-		ConstructGeometry(*it, cell);
-}
+	}
+}
+
 
 int BspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, 
@@ -2229,8 +2143,6 @@
 		
 	// planes needed to verify that we found neighbor leaf.
-	vector<Plane3> planes;
-	vector<bool> sides;
-
-	ExtractSplitPlanes(n, planes, sides);
+	vector<Plane3> halfSpaces;
+	ExtractHalfSpaces(n, halfSpaces);
 
 	while (!nodeStack.empty()) 
@@ -2243,17 +2155,16 @@
             if (node != n && (!onlyUnmailed || !node->Mailed()))
 			{
-				// test all planes of current node on neighbour
+				// test all planes of current node if candidate really
+				// is neighbour
 				PolygonContainer neighborCandidate;
 				ConstructGeometry(node, neighborCandidate);
 				
 				bool isAdjacent = true;
-				for (int i = 0; (i < planes.size()) && isAdjacent; ++ i)
+				for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
 				{
 					const int cf = 
-						Polygon3::ClassifyPlane(neighborCandidate, planes[i]);
-
-					if ((cf == Polygon3::BACK_SIDE) && sides[i])
-						isAdjacent = false;
-					else if ((cf == Polygon3::FRONT_SIDE) && !sides[i])
+						Polygon3::ClassifyPlane(neighborCandidate, halfSpaces[i]);
+
+					if (cf == Polygon3::BACK_SIDE)
 						isAdjacent = false;
 				}
@@ -2409,15 +2320,4 @@
  *************************************************************/
 
-BspNodeGeometry::BspNodeGeometry(const PolygonContainer &polys, 
-								 const vector<Plane3> &planes, 
-								 const vector<bool> &sides):
-mPolys(polys), mPlanes(planes), mSides(sides)
-{}					   
-
-BspNodeGeometry::BspNodeGeometry(const vector<Plane3> &planes, 
-								 const vector<bool> &sides):
-mPlanes(planes), mSides(sides)
-{}
-
 BspNodeGeometry::~BspNodeGeometry()
 {
@@ -2430,66 +2330,14 @@
 }
 
-void BspNodeGeometry::SplitGeometry(BspNodeGeometry frontChild.
-									BspNodeGeometry backChild,
+void BspNodeGeometry::SplitGeometry(BspNodeGeometry &front,
+									BspNodeGeometry &back,
 									const BspTree &tree,						 
-									const Plane3 &splitPlane,
-									const bool side) const
-{
-	//BspNodeGeometry *childCell = new BspNodeGeometry(mPlanes, mSides);
-
+									const Plane3 &splitPlane) const
+{	
 	// get cross section of new polygon
 	Polygon3 *planePoly = tree.GetBoundingBox().CrossSection(splitPlane);
 
-	Polygon3 *frontPoly = NULL;//new Polygon3(planePoly->mVertices);
-	Polygon3 *backPoly = NULL; //new Polygon3(planePoly->mVertices);
-
-	// polygon is split by all other planes
-	for (int i = 0; (i < (int)mPlanes.size()) && planePoly; ++ i)
-	{
-		const int cf = planePoly->ClassifyPlane(mPlanes[i]);
-			
-		// split new polygon with all previous planes
-		switch (cf)
-		{
-			case Polygon3::SPLIT:
-				{
-					VertexContainer splitPts;
-				
-					frontPoly = new Polygon3();
-					backPoly = new Polygon3();
-
-					planePoly->Split(mPlanes[i], *frontPoly, *backPoly, splitPts);
-					DEL_PTR(planePoly);
-
-					if(mSides[i] == true)
-					{
-						planePoly = frontPoly;
-						DEL_PTR(backPoly);
-					}
-					else
-					{
-						planePoly = backPoly;
-						DEL_PTR(frontPoly);
-					}
-					
-					if (!planePoly->Valid())
-						DEL_PTR(planePoly);
-				}
-				break;
-			case Polygon3::BACK_SIDE:
-				if (mSides[i]) 
-					DEL_PTR(planePoly);
-				break;
-			case Polygon3::FRONT_SIDE:
-				if (!mSides[i])
-					DEL_PTR(planePoly);
-				break;
-			case Polygon3::COINCIDENT:
-				break;
-			default:
-				break;
-		}
-	}
-	
+	planePoly = SplitPolygon(planePoly, tree);
+
 	//-- plane poly splits all other cell polygons
 	for (int i = 0; i < (int)mPolys.size(); ++ i)
@@ -2513,54 +2361,80 @@
 					DEL_PTR(poly);
 
-					if (side == true)
-					{
-						poly = frontPoly;
-						DEL_PTR(backPoly);
-					}
-					else
-					{
-						poly = backPoly;
-						DEL_PTR(frontPoly);
-					}
-					if (!poly->Valid())
-						DEL_PTR(poly);
-					else
-						childCell->mPolys.push_back(poly);
+					if (frontPoly->Valid())
+						front.mPolys.push_back(frontPoly);
+					if (backPoly->Valid())
+						back.mPolys.push_back(backPoly);
 				}
 				
 				break;
 			case Polygon3::BACK_SIDE:
-				if (!side) 
-					childCell->mPolys.push_back(new Polygon3(mPolys[i]->mVertices));			
+				back.mPolys.push_back(new Polygon3(mPolys[i]->mVertices));			
 				break;
 			case Polygon3::FRONT_SIDE:
-				if (side)
-					childCell->mPolys.push_back(new Polygon3(mPolys[i]->mVertices));	
+				front.mPolys.push_back(new Polygon3(mPolys[i]->mVertices));	
 				break;
 			case Polygon3::COINCIDENT:
-				childCell->mPolys.push_back(new Polygon3(mPolys[i]->mVertices));
+				//front.mPolys.push_back(CreateReversePolygon(mPolys[i]));
+				//back.mPolys.push_back(new Polygon3(mPolys[i]->mVertices));
 				break;
 			default:
+				break;
+		}
+	}
+
+	//-- finally add the new polygon to the child cells
+	if (planePoly)
+	{
+		// add polygon with normal pointing into positive half space to back cell
+		back.mPolys.push_back(planePoly);
+		// add polygon with reverse orientation to front cell
+		front.mPolys.push_back(planePoly->CreateReversePolygon());
+	}
+
+	//Debug << "returning new geometry " << mPolys.size() << " CHILD: " << childCell->mPolys.size() << endl;
+	//Debug << "old area " << this->GetArea() << " new: " << childCell->GetArea() << endl;
+}
+
+Polygon3 *BspNodeGeometry::SplitPolygon(Polygon3 *planePoly,
+										const BspTree &tree) const
+{
+	// polygon is split by all other planes
+	for (int i = 0; (i < (int)mPolys.size()) && planePoly; ++ i)
+	{
+		Plane3 plane = mPolys[i]->GetSupportingPlane();
+
+		const int cf = 
+			planePoly->ClassifyPlane(plane);
+			
+		// split new polygon with all previous planes
+		switch (cf)
+		{
+			case Polygon3::SPLIT:
+				{
+					VertexContainer splitPts;
 				
+					Polygon3 *frontPoly = new Polygon3();
+					Polygon3 *backPoly = new Polygon3();
+
+					planePoly->Split(plane, *frontPoly, *backPoly, splitPts);
+					DEL_PTR(planePoly);
+
+					if (backPoly->Valid())
+						planePoly = backPoly;
+					else
+						DEL_PTR(backPoly);
+				}
 				break;
-		}
-	}
-
-	//-- finally add the new polygon
-	if (planePoly)
-	{
-		childCell->mPolys.push_back(planePoly);
-		childCell->mPlanes.push_back(splitPlane);
-		childCell->mSides.push_back(side);
-	}
-
-	Debug << "returning new geometry " << mPolys.size() << " CHILD: " << childCell->mPolys.size() << endl;
-
-	Debug << "old area " << this->GetArea() << " new: " << childCell->GetArea() << endl;
-	return childCell;
-}
-
-/*void BspNodeGeometry::SplitPolygon(Polygon3 *poly,
-								   const BspTree &tree) const
-{
-}*/
+			case Polygon3::FRONT_SIDE:
+				DEL_PTR(planePoly);
+                break;
+			// polygon is taken as it is
+			case Polygon3::BACK_SIDE:
+			case Polygon3::COINCIDENT:
+			default:
+				break;
+		}
+	}
+
+	return planePoly;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 372)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h	(revision 396)
@@ -30,4 +30,9 @@
   }
 
+  void ReverseOrientation()
+  {
+	  mNormal *= -1;
+	  mD *= -1;
+  }
 
   float Distance(const Vector3 &v) const {
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 386)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 396)
@@ -94,5 +94,5 @@
 
   virtual void 
-  ExportBspLeaves(const BspTree &tree);
+  ExportBspLeaves(const BspTree &tree, const int maxPvs = 0);
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 390)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 396)
@@ -229,5 +229,4 @@
 	ViewCellContainer::const_iterator it, it_end = viewCells.end();
 
-
 	if (maxPvs > 0)
 		mUseForcedMaterial = true;
@@ -257,16 +256,52 @@
 
 void 
-X3dExporter::ExportBspLeaves(const BspTree &tree)
-{
-	vector<BspLeaf *> leaves;
-	tree.CollectLeaves(leaves);
-
-	vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
-
-	for (it = leaves.begin(); it != it_end; ++ it)
-	{
-		PolygonContainer cell;
-		tree.ConstructGeometry(*it, cell);
-		ExportPolygons(cell);
+X3dExporter::ExportBspLeaves(const BspTree &tree, const int maxPvs)
+{
+	stack<pair<BspNode *, BspNodeGeometry *> > tStack;
+	ViewCell::NewMail();
+
+	BspNodeGeometry *geom = new BspNodeGeometry();
+	tree.ConstructGeometry(tree.GetRoot(), *geom);
+
+	tStack.push(pair<BspNode *, BspNodeGeometry *>(tree.GetRoot(), geom));
+
+	if (maxPvs > 0)
+		mUseForcedMaterial = true;
+
+	while (!tStack.empty()) 
+	{
+		BspNode *node = tStack.top().first;
+		BspNodeGeometry *cell = tStack.top().second;
+		tStack.pop();
+
+		if (!node->IsLeaf()) 
+		{
+			BspInterior *interior = dynamic_cast<BspInterior *>(node);
+								
+			BspNodeGeometry *front = new BspNodeGeometry();
+			BspNodeGeometry *back = new BspNodeGeometry();
+
+			cell->SplitGeometry(*front, *back, tree, *interior->GetPlane());
+
+			tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetFront(), front));
+			tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetBack(), back));
+		}
+		else
+		{
+			if (maxPvs > 0)
+			{
+				BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
+
+				mForcedMaterial.mDiffuseColor.b = 1.0f;
+  				float importance = (float)leaf->GetViewCell()->GetPvs().GetSize() / (float)maxPvs;
+
+				mForcedMaterial.mDiffuseColor.r = importance;
+				mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
+			}
+
+			ExportPolygons(cell->mPolys);
+		}
+		
+		DEL_PTR(cell);
 	}
 }
@@ -532,15 +567,9 @@
 	}
   
-	stack<BspNode *> tStack;
-
-	tStack.push(tree.GetRoot());
-
-	Mesh *mesh = new Mesh;
-
-	AxisAlignedBox3 box = tree.GetBoundingBox();
 	bool savedWireframe = mWireframe;
 
 	SetWireframe();
-	ExportBox(box);
+	
+	ExportBox(tree.GetBoundingBox());
 	
 	if (!savedWireframe)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 390)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 396)
@@ -439,7 +439,20 @@
 
 	for (pit = cell.begin(); pit != cell.end(); ++ pit)
-	{
 		area += (*pit)->GetArea();
-	}
+
 	return area;
 }
+
+
+Polygon3 *Polygon3::CreateReversePolygon() const
+{
+	Polygon3 *revPoly = new Polygon3();
+
+	VertexContainer::const_reverse_iterator rit, 
+			rit_end = mVertices.rend();
+
+	for(rit = mVertices.rbegin(); rit != rit_end; ++ rit)
+		revPoly->mVertices.push_back(*rit);
+
+	return revPoly;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 392)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 396)
@@ -558,9 +558,9 @@
 		if (1) // export view cells
 		{
-			cout << "exporting view cells ... ";
+			cout << "exporting initial view cells (=leaves) ... ";
 			Exporter *exporter = Exporter::GetExporter("view_cells.x3d");
 			if (exporter)
 			{
-				exporter->ExportBspViewCellPartition(*mBspTree, stat.maxPvs);
+				exporter->ExportBspLeaves(*mBspTree, stat.maxPvs);
 				//exporter->ExportBspViewCellPartition(*mBspTree, 0);
 				delete exporter;
@@ -1065,4 +1065,5 @@
 		renderTime += area * RenderPvs(*(*it), objRt) + vcOverhead;
 		totalArea += area;
+		CLEAR_CONTAINER(cell);
 	}
 
