Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 676)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 678)
@@ -129,5 +129,5 @@
 		float GetCost() const
 		{
-			//cout << "here " << mPriority << endl;
+			//cout << mPriority << endl;
 			return mPriority;
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 666)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 678)
@@ -40,5 +40,6 @@
 	*/
 	float GetArea() const;
-	
+	/** Returns volume of this node geometry.
+	*/
 	float GetVolume() const;
 
@@ -69,8 +70,7 @@
 	Vector3 CenterOfMass() const;
 
-	/** The polygons the geometry consists of.
-	*/
-	PolygonContainer mPolys;
-
+	bool Valid() const;
+
+	
 	friend ostream &operator<<(ostream &s, const BspNodeGeometry &a)
 	{
@@ -82,4 +82,18 @@
 	}
 
+	int Size() const;
+	const PolygonContainer &GetPolys();
+
+	void Add(Polygon3 *p, const Plane3 &plane);
+
+protected:
+	/** The polygons the geometry consists of.
+	*/
+	PolygonContainer mPolys;
+
+	/** The corresponding set of planes for the polygons.
+		Need this because of precision issues.
+	*/
+	vector<Plane3> mPlanes;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 676)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 678)
@@ -186,5 +186,4 @@
 	mSplitCandidates = new vector<SortableEntry>;
 
-	Debug << "here3" << endl;
 	Debug << endl;
 }
@@ -463,5 +462,5 @@
 
 	// first node is kd node, i.e. an axis aligned box
-	if (1)
+	if (0)
         tData.mIsKdNode = true;
 	else
@@ -1348,7 +1347,7 @@
 	if (1)
 	{
-		PolygonContainer::const_iterator it, it_end = tData.mGeometry->mPolys.end();
-
-		for(it = tData.mGeometry->mPolys.begin(); it < it_end; ++ it)
+		PolygonContainer::const_iterator it, it_end = tData.mGeometry->GetPolys().end();
+
+		for(it = tData.mGeometry->GetPolys().begin(); it < it_end; ++ it)
 			box.Include(*(*it));
 	}
@@ -1402,9 +1401,14 @@
 					pos = box.Max(); pos[axis] = nPosition[axis];
 					AxisAlignedBox3 bBox(box.Min(), pos);
-					bBox.ExtractPolys(nBackGeom[axis]->mPolys);
-					
+					// TODO
+#if 0
+						bBox.ExtractPolys(nBackGeom[axis]->GetPolys());
+#endif					
 					pos = box.Min(); pos[axis] = nPosition[axis];
 					AxisAlignedBox3 fBox(pos, box.Max());
-					fBox.ExtractPolys(nFrontGeom[axis]->mPolys);
+					// TODO
+#if 0
+						fBox.ExtractPolys(nFrontGeom[axis]->GetPolys());
+#endif
 				}
 				else
@@ -1569,9 +1573,12 @@
 		DEL_PTR(bGeom);
 	}
-	
-
-#ifdef _DEBUG
+    
+
+	if (lowestCost > 10)
+		Debug << "warning!! lowest cost: " << lowestCost << endl;
+	
+//#ifdef _DEBUG
 	Debug << "plane lowest cost: " << lowestCost << endl;
-#endif
+//#endif
 
 	if (lowestCost > mTermMaxCostRatio)
@@ -1716,5 +1723,5 @@
 			Debug << "vol f " << pFront << " b " << pBack << " p " << pOverall << endl;
 			Debug << "real vol f " << pFront << " b " << geomBack.GetVolume() << " p " << pOverall << endl;
-			Debug << "polys f" << (int)geomFront.mPolys.size() << " b " << (int)geomFront.mPolys.size() << " data " << (int)data.mGeometry->mPolys.size() << endl;
+			Debug << "polys f " << geomFront.Size() << " b " << geomBack.Size() << " data " << data.mGeometry->Size() << endl;
 		}
 
@@ -1807,5 +1814,5 @@
 								  candidatePlane,
 								  mBox,
-								  0.0000001f);
+								  0.000000001f);
 								  //mEpsilon);
 
@@ -1820,13 +1827,8 @@
 		if (1)
 		{
-			if (pFront <= 0)
-			{
-				//Debug << "error f: " << pFront << endl;
-				return 999;
-			}
-
-			if (pBack <= 0) 
-			{
-				//Debug << "error b: " << pBack << endl;
+			if ((pFront <= 0) || (pBack <= 0) || 
+				!geomFront.Valid() || !geomBack.Valid())
+			{
+				Debug << "error f: " << pFront << " b: " << pBack << endl;
 				return 999;
 			}
@@ -2527,7 +2529,8 @@
 
 	PolygonContainer candidatePolys;
+	vector<Plane3> candidatePlanes;
 
 	// bounded planes are added to the polygons (reverse polygons
-	// as they have to be outfacing
+	// as they have to be outfacing)
 	for (int i = 0; i < (int)halfSpaces.size(); ++ i)
 	{
@@ -2537,4 +2540,9 @@
 		{
 			candidatePolys.push_back(p->CreateReversePolygon());
+			// Why?
+			Plane3 candidatePlane = p->GetSupportingPlane(); //halfSpaces[i];
+			//candidatePlane.ReverseOrientation();
+			candidatePlanes.push_back(candidatePlane);
+
 			DEL_PTR(p);
 		}
@@ -2549,5 +2557,8 @@
 			vertices.push_back(mBox.GetFace(i).mVertices[j]);
 
-		candidatePolys.push_back(new Polygon3(vertices));
+		Polygon3 *poly = new Polygon3(vertices);
+
+		candidatePolys.push_back(poly);
+		candidatePlanes.push_back(poly->GetSupportingPlane());
 	}
 
@@ -2587,4 +2598,5 @@
 					DEL_PTR(backPoly);
 					break;
+
 				case Polygon3::BACK_SIDE:
 					DEL_PTR(candidatePolys[i]);
@@ -2599,5 +2611,8 @@
 
 		if (candidatePolys[i])
-			geom.mPolys.push_back(candidatePolys[i]);
+		{
+			geom.Add(candidatePolys[i], candidatePlanes[i]);
+			//	geom.mPolys.push_back(candidates[i]);
+		}
 	}
 }
@@ -2667,5 +2682,5 @@
 					{
 						const int cf =
-							Polygon3::ClassifyPlane(geom->mPolys,
+							Polygon3::ClassifyPlane(geom->GetPolys(),
 													halfSpaces[i],
 													mEpsilon);
@@ -2681,10 +2696,10 @@
 					// TODO: why is this wrong??
 					// test all planes of current node if still adjacent
-					for (int i = 0; (i < (int)nodeGeom.mPolys.size()) && isAdjacent; ++ i)
+					for (int i = 0; (i < nodeGeom.Size()) && isAdjacent; ++ i)
 					{
-						Polygon3 *poly = nodeGeom.mPolys[i];
+						Polygon3 *poly = nodeGeom.GetPolys()[i];
 
 						const int cf =
-							Polygon3::ClassifyPlane(geom->mPolys,
+							Polygon3::ClassifyPlane(geom->GetPolys(),
 													poly->GetSupportingPlane(),
 													mEpsilon);
@@ -2707,5 +2722,5 @@
 			BspInterior *interior = dynamic_cast<BspInterior *>(node);
 
-			const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys,
+			const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
 												   interior->GetPlane(),
 												   mEpsilon);
@@ -2799,5 +2814,5 @@
 			BspInterior *interior = dynamic_cast<BspInterior *>(node);
 
-			const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys,
+			const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
 												   interior->GetPlane(),
 												   mEpsilon);
@@ -2870,5 +2885,5 @@
 
 			const int cf = 
-				Polygon3::ClassifyPlane(geom.mPolys, halfspace, mEpsilon);
+				Polygon3::ClassifyPlane(geom.GetPolys(), halfspace, mEpsilon);
 
 			if (cf == Polygon3::BACK_SIDE)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 676)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 678)
@@ -2303,4 +2303,5 @@
 {
 	BspNode *lastNode;
+
 	do
 	{
@@ -2355,7 +2356,9 @@
 
 	PolygonContainer candidates;
+	vector<Plane3> candidatePlanes;
 
 	// bounded planes are added to the polygons (reverse polygons
 	// as they have to be outfacing
+	// comment: why is this?
 	for (int i = 0; i < (int)halfSpaces.size(); ++ i)
 	{
@@ -2365,4 +2368,9 @@
 		{
 			candidates.push_back(p->CreateReversePolygon());
+
+			Plane3 candidatePlane = p->GetSupportingPlane();//halfSpaces[i];
+			//candidatePlane.ReverseOrientation();
+			candidatePlanes.push_back(candidatePlane);
+
 			DEL_PTR(p);
 		}
@@ -2377,6 +2385,9 @@
 			vertices.push_back(mBox.GetFace(i).mVertices[j]);
 
+		Polygon3 *poly = new Polygon3(vertices);
 		candidates.push_back(new Polygon3(vertices));
-	}
+		candidatePlanes.push_back(poly->GetSupportingPlane());
+	}
+
 
 	for (int i = 0; i < (int)candidates.size(); ++ i)
@@ -2426,5 +2437,8 @@
 		
 		if (candidates[i])
-			geom.mPolys.push_back(candidates[i]);
+		{
+			geom.Add(candidates[i], candidatePlanes[i]);
+		//	geom.mPolys.push_back(candidates[i]);
+		}
 	}
 }
@@ -2476,5 +2490,5 @@
 					{
 						const int cf =
-							Polygon3::ClassifyPlane(geom->mPolys,
+							Polygon3::ClassifyPlane(geom->GetPolys(),
 													halfSpaces[i],
 													mEpsilon);
@@ -2490,10 +2504,10 @@
 					// TODO: why is this wrong??
 					// test all planes of current node if still adjacent
-					for (int i = 0; (i < (int)nodeGeom.mPolys.size()) && isAdjacent; ++ i)
+					for (int i = 0; (i < nodeGeom.Size()) && isAdjacent; ++ i)
 					{
-						Polygon3 *poly = nodeGeom.mPolys[i];
+						Polygon3 *poly = nodeGeom.GetPolys()[i];
 
 						const int cf =
-							Polygon3::ClassifyPlane(geom->mPolys,
+							Polygon3::ClassifyPlane(geom->GetPolys(),
 													poly->GetSupportingPlane(),
 													mEpsilon);
@@ -2516,5 +2530,5 @@
 			BspInterior *interior = dynamic_cast<BspInterior *>(node);
 
-			const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys,
+			const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
 												   interior->GetPlane(),
 												   mEpsilon);
@@ -2584,5 +2598,5 @@
 			ConstructGeometry(interior, geom);
 
-			const int cf = Polygon3::ClassifyPlane(geom.mPolys, 
+			const int cf = Polygon3::ClassifyPlane(geom.GetPolys(), 
 												   halfspace, 
 												   mEpsilon);
@@ -2880,8 +2894,11 @@
 	
 	PolygonContainer::const_iterator it, it_end = rhs.mPolys.end();
+
+	int i = 0;
+
 	for (it = rhs.mPolys.begin(); it != it_end; ++ it)
 	{
 		Polygon3 *poly = *it;
-		mPolys.push_back(new Polygon3(*poly));
+		Add(new Polygon3(*poly), rhs.mPlanes[i ++]);
 	}
 }
@@ -2891,4 +2908,23 @@
 {
 	CLEAR_CONTAINER(mPolys);
+}
+
+
+int BspNodeGeometry::Size() const
+{
+	return (int)mPolys.size();
+}
+
+
+const PolygonContainer &BspNodeGeometry::GetPolys()
+{
+	return mPolys;
+}
+
+
+void BspNodeGeometry::Add(Polygon3 *p, const Plane3 &plane)
+{
+    mPolys.push_back(p);
+	mPlanes.push_back(plane);
 }
 
@@ -2923,5 +2959,6 @@
 			const Vector3 v2 = poly->mVertices[i + 1] - center;
 
-			volume += f * (DotProd(v0, CrossProd(v1, v2)));
+			// more robust version using abs and the center of mass
+			volume += fabs (f * (DotProd(v0, CrossProd(v1, v2))));
 		}
 	}
@@ -2953,4 +2990,13 @@
 
 	return center / (float)n;
+}
+
+
+bool BspNodeGeometry::Valid() const
+{
+	if (mPolys.size() < 4)
+		return false;
+	
+	return true;
 }
 
@@ -3003,24 +3049,35 @@
 
 					if (frontPoly->Valid(epsilon))
-						front.mPolys.push_back(frontPoly);
+					{
+						front.Add(frontPoly, mPlanes[i]);
+					}
 					else
+					{
 						DEL_PTR(frontPoly);
-
+					}
 					if (backPoly->Valid(epsilon))
-						back.mPolys.push_back(backPoly);
+					{
+						back.Add(backPoly, mPlanes[i]);
+					}
 					else
+					{
 						DEL_PTR(backPoly);
+					}
 				}
 				
 				break;
 			case Polygon3::BACK_SIDE:
-				back.mPolys.push_back(new Polygon3(mPolys[i]->mVertices));			
+		
+				back.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
 				break;
 			case Polygon3::FRONT_SIDE:
-				front.mPolys.push_back(new Polygon3(mPolys[i]->mVertices));	
+
+				front.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
 				break;
+			// only put into back container (split should have no effect ...)
 			case Polygon3::COINCIDENT:
+
+				back.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
 				//front.mPolys.push_back(CreateReversePolygon(mPolys[i]));
-				back.mPolys.push_back(new Polygon3(mPolys[i]->mVertices));
 				break;
 			default:
@@ -3033,7 +3090,12 @@
 	{
 		// add polygon with normal pointing into positive half space to back cell
-		back.mPolys.push_back(planePoly);
+		back.Add(planePoly, splitPlane);
+		//back.mPolys.push_back(planePoly);
+
 		// add polygon with reverse orientation to front cell
-		front.mPolys.push_back(planePoly->CreateReversePolygon());
+		Plane3 reversePlane(splitPlane);
+		reversePlane.ReverseOrientation();
+		front.Add(planePoly->CreateReversePolygon(), reversePlane);
+		//front.mPolys.push_back(planePoly->CreateReversePolygon());
 	}
 }
@@ -3049,9 +3111,13 @@
 	for (int i = 0; (i < (int)mPolys.size()) && planePoly; ++ i)
 	{
+#if 0
 		Plane3 plane = mPolys[i]->GetSupportingPlane();
-
+#else
+		Plane3 plane = mPlanes[i];
+	
+#endif
 		/// don't use epsilon here to get exact split planes
 		const int cf = 
-			planePoly->ClassifyPlane(plane, Limits::Small);
+			planePoly->ClassifyPlane(plane, epsilon);
 			
 		// split new polygon with all previous planes
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 580)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 678)
@@ -277,5 +277,5 @@
 			}
 
-			ExportPolygons(cell->mPolys);
+			ExportPolygons(cell->GetPolys());
 		}
 		
@@ -971,5 +971,5 @@
 		tree.ConstructGeometry(*it, geom);
 		
-		ExportPolygons(geom.mPolys);
+		ExportPolygons(geom.GetPolys());
 	}
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 667)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 678)
@@ -1716,11 +1716,11 @@
 						   0.0001f);
 
-		if ((int)back.mPolys.size() >= 3)
-			exporter->ExportPolygons(back.mPolys);
+		if (back.Size() >= 3)
+			exporter->ExportPolygons(back.GetPolys());
 	}
 	else
 	{
 		
-		exporter->ExportPolygons(geom.mPolys);
+		exporter->ExportPolygons(geom.GetPolys());
 	}
 }
@@ -3164,5 +3164,5 @@
 					BspNodeGeometry geom;
 					mVspBspTree->ConstructGeometry(leaf, geom);
-					exporter->ExportPolygons(geom.mPolys);
+					exporter->ExportPolygons(geom.GetPolys());
 				}
 			}
@@ -3486,6 +3486,6 @@
 							   0.0001f);
 
-			if ((int)back.mPolys.size() >= 3)
-				exporter->ExportPolygons(back.mPolys);
+			if (back.Size() >= 3)
+				exporter->ExportPolygons(back.GetPolys());
 		}
 	}
@@ -3495,5 +3495,5 @@
 		mVspBspTree->ConstructGeometry(vc, geom);
 			
-		exporter->ExportPolygons(geom.mPolys);
+		exporter->ExportPolygons(geom.GetPolys());
 	}
 }
