Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 682)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 683)
@@ -2511,5 +2511,5 @@
 			Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane();
 
-            if (interior->GetFront() != lastNode)
+            if (interior->GetBack() != lastNode)
 				halfSpace.ReverseOrientation();
 
@@ -2530,6 +2530,5 @@
 	vector<Plane3> candidatePlanes;
 
-	// bounded planes are added to the polygons (reverse polygons
-	// as they have to be outfacing)
+	// bounded planes are added to the polygons
 	for (int i = 0; i < (int)halfSpaces.size(); ++ i)
 	{
@@ -2538,11 +2537,6 @@
 		if (p->Valid(mEpsilon))
 		{
-			candidatePolys.push_back(p->CreateReversePolygon());
-			// Why?
-			Plane3 candidatePlane = p->GetSupportingPlane(); //halfSpaces[i];
-			//candidatePlane.ReverseOrientation();
-			candidatePlanes.push_back(candidatePlane);
-
-			DEL_PTR(p);
+			candidatePolys.push_back(p);
+			candidatePlanes.push_back(halfSpaces[i]);
 		}
 	}
@@ -2590,17 +2584,18 @@
 					DEL_PTR(candidatePolys[i]);
 
-					if (frontPoly->Valid(mEpsilon))
-						candidatePolys[i] = frontPoly;
+					if (backPoly->Valid(mEpsilon))
+						candidatePolys[i] = backPoly;
 					else
-						DEL_PTR(frontPoly);
-
-					DEL_PTR(backPoly);
+						DEL_PTR(backPoly);
+
+					// outside, don't need this
+					DEL_PTR(frontPoly);
 					break;
-
-				case Polygon3::BACK_SIDE:
+				// polygon outside of halfspace
+				case Polygon3::FRONT_SIDE:
 					DEL_PTR(candidatePolys[i]);
 					break;
 				// just take polygon as it is
-				case Polygon3::FRONT_SIDE:
+				case Polygon3::BACK_SIDE:
 				case Polygon3::COINCIDENT:
 				default:
@@ -2685,5 +2680,5 @@
 													mEpsilon);
 
-						if (cf == Polygon3::BACK_SIDE)
+						if (cf == Polygon3::FRONT_SIDE)
 						{
 							isAdjacent = false;
@@ -2704,5 +2699,5 @@
 													mEpsilon);
 
-						if (cf == Polygon3::BACK_SIDE)
+						if (cf == Polygon3::FRONT_SIDE)
 						{
 							isAdjacent = false;
@@ -2738,15 +2733,15 @@
 								mEpsilon);
 		
-			if (cf == Polygon3::FRONT_SIDE)
-			{
-				nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
-				DEL_PTR(bGeom);
+			if (cf == Polygon3::BACK_SIDE)
+			{
+				nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
+				DEL_PTR(fGeom);
 			}
 			else
 			{
-				if (cf == Polygon3::BACK_SIDE)
+				if (cf == Polygon3::FRONT_SIDE)
 				{
-					nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
-					DEL_PTR(fGeom);
+					nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
+					DEL_PTR(bGeom);
 				}
 				else
@@ -2830,15 +2825,15 @@
 								mEpsilon);
 		
-			if (cf == Polygon3::FRONT_SIDE)
-			{
-				nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
-				DEL_PTR(bGeom);
+			if (cf == Polygon3::BACK_SIDE)
+			{
+				nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
+				DEL_PTR(fGeom);
 			}
 			else
 			{
-				if (cf == Polygon3::BACK_SIDE)
+				if (cf == Polygon3::FRONT_SIDE)
 				{
-					nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
-					DEL_PTR(fGeom);
+					nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
+					DEL_PTR(bGeom);
 				}
 				else
@@ -3044,5 +3039,6 @@
 
 	float t;
-	const float thresh = 1 ? 1e-6f : 0.0f;
+	//const float thresh = 1 ? 1e-6f : 0.0f;
+	const float thresh = 0.01f;
 
 	while (1)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 682)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 683)
@@ -2317,5 +2317,5 @@
 			Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane();
 
-            if (interior->GetFront() != lastNode)
+            if (interior->GetBack() != lastNode)
 				halfSpace.ReverseOrientation();
 
@@ -2344,34 +2344,22 @@
 
 
-void BspTree::SetViewCellsManager(ViewCellsManager *vcm)
-{
-	mViewCellsManager = vcm;
-}
-
-
-void BspTree::ConstructGeometry(BspNode *n, BspNodeGeometry &geom) const
+void BspTree::ConstructGeometry(BspNode *n,
+								BspNodeGeometry &geom) const
 {
 	vector<Plane3> halfSpaces;
 	ExtractHalfSpaces(n, halfSpaces);
 
-	PolygonContainer candidates;
+	PolygonContainer candidatePolys;
 	vector<Plane3> candidatePlanes;
 
-	// bounded planes are added to the polygons (reverse polygons
-	// as they have to be outfacing
-	// comment: why is this?
+	// bounded planes are added to the polygons
 	for (int i = 0; i < (int)halfSpaces.size(); ++ i)
 	{
 		Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]);
-		
+
 		if (p->Valid(mEpsilon))
 		{
-			candidates.push_back(p->CreateReversePolygon());
-
-			Plane3 candidatePlane = p->GetSupportingPlane();//halfSpaces[i];
-			//candidatePlane.ReverseOrientation();
-			candidatePlanes.push_back(candidatePlane);
-
-			DEL_PTR(p);
+			candidatePolys.push_back(p);
+			candidatePlanes.push_back(halfSpaces[i]);
 		}
 	}
@@ -2381,18 +2369,18 @@
 	{
 		VertexContainer vertices;
-	
+
 		for (int j = 0; j < 4; ++ j)
 			vertices.push_back(mBox.GetFace(i).mVertices[j]);
 
 		Polygon3 *poly = new Polygon3(vertices);
-		candidates.push_back(new Polygon3(vertices));
+
+		candidatePolys.push_back(poly);
 		candidatePlanes.push_back(poly->GetSupportingPlane());
 	}
 
-
-	for (int i = 0; i < (int)candidates.size(); ++ i)
+	for (int i = 0; i < (int)candidatePolys.size(); ++ i)
 	{
 		// polygon is split by all other planes
-		for (int j = 0; (j < (int)halfSpaces.size()) && candidates[i]; ++ j)
+		for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
 		{
 			if (i == j) // polygon and plane are coincident
@@ -2402,7 +2390,8 @@
 			Polygon3 *frontPoly, *backPoly;
 
-			const int cf = candidates[i]->
-				ClassifyPlane(halfSpaces[j], mEpsilon);
-			
+			const int cf =
+				candidatePolys[i]->ClassifyPlane(halfSpaces[j],
+												 mEpsilon);
+
 			switch (cf)
 			{
@@ -2411,13 +2400,13 @@
 					backPoly = new Polygon3();
 
-					candidates[i]->Split(halfSpaces[j], 
-										 *frontPoly, 
-										 *backPoly,
-										 mEpsilon);
-
-					DEL_PTR(candidates[i]);
+					candidatePolys[i]->Split(halfSpaces[j],
+											 *frontPoly,
+											 *backPoly,
+											 mEpsilon);
+
+					DEL_PTR(candidatePolys[i]);
 
 					if (frontPoly->Valid(mEpsilon))
-						candidates[i] = frontPoly;
+						candidatePolys[i] = frontPoly;
 					else
 						DEL_PTR(frontPoly);
@@ -2425,9 +2414,10 @@
 					DEL_PTR(backPoly);
 					break;
-				case Polygon3::BACK_SIDE:
-					DEL_PTR(candidates[i]);
+				// polygon outside of halfspace
+				case Polygon3::FRONT_SIDE:
+					DEL_PTR(candidatePolys[i]);
 					break;
 				// just take polygon as it is
-				case Polygon3::FRONT_SIDE:
+				case Polygon3::BACK_SIDE:
 				case Polygon3::COINCIDENT:
 				default:
@@ -2435,11 +2425,17 @@
 			}
 		}
-		
-		if (candidates[i])
-		{
-			geom.Add(candidates[i], candidatePlanes[i]);
-		//	geom.mPolys.push_back(candidates[i]);
-		}
-	}
+
+		if (candidatePolys[i])
+		{
+			geom.Add(candidatePolys[i], candidatePlanes[i]);
+			//	geom.mPolys.push_back(candidates[i]);
+		}
+	}
+}
+
+
+void BspTree::SetViewCellsManager(ViewCellsManager *vcm)
+{
+	mViewCellsManager = vcm;
 }
 
@@ -2448,5 +2444,6 @@
 
 
-int BspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
+int BspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> 
+						   &neighbors,
 						   const bool onlyUnmailed) const
 {
@@ -2494,5 +2491,5 @@
 													mEpsilon);
 
-						if (cf == Polygon3::BACK_SIDE)
+						if (cf == Polygon3::FRONT_SIDE)
 						{
 							isAdjacent = false;
@@ -2513,5 +2510,5 @@
 													mEpsilon);
 
-						if (cf == Polygon3::BACK_SIDE)
+						if (cf == Polygon3::FRONT_SIDE)
 						{
 							isAdjacent = false;
@@ -2544,17 +2541,18 @@
 								interior->GetPlane(),
 								mBox,
+								//0.0000001f);
 								mEpsilon);
 		
-			if (cf == Polygon3::FRONT_SIDE)
+			if (cf == Polygon3::BACK_SIDE)
 			{
-				nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
-				DEL_PTR(bGeom);
+				nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
+				DEL_PTR(fGeom);
 			}
 			else
 			{
-				if (cf == Polygon3::BACK_SIDE)
+				if (cf == Polygon3::FRONT_SIDE)
 				{
-					nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
-					DEL_PTR(fGeom);
+					nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
+					DEL_PTR(bGeom);
 				}
 				else
@@ -2570,56 +2568,4 @@
 
 	return (int)neighbors.size();
-}
-
-
-BspLeaf *BspTree::GetRandomLeaf(const Plane3 &halfspace)
-{
-    stack<BspNode *> nodeStack;
-  	nodeStack.push(mRoot);
-	
-	int mask = rand();
-  
-	while (!nodeStack.empty()) 
-	{
-		BspNode *node = nodeStack.top();
-		nodeStack.pop();
-	  
-		if (node->IsLeaf()) 
-		{
-			return dynamic_cast<BspLeaf *>(node);
-		} 
-		else 
-		{
-			BspInterior *interior = dynamic_cast<BspInterior *>(node);
-			BspNode *next;
-	
-			BspNodeGeometry geom;
-			// todo: not very efficient: constructs full cell everytime
-			ConstructGeometry(interior, geom);
-
-			const int cf = Polygon3::ClassifyPlane(geom.GetPolys(), 
-												   halfspace, 
-												   mEpsilon);
-
-			if (cf == Polygon3::BACK_SIDE)
-				next = interior->GetFront();
-			else
-				if (cf == Polygon3::FRONT_SIDE)
-					next = interior->GetFront();
-			else 
-			{
-				// random decision
-				if (mask & 1)
-					next = interior->GetBack();
-				else
-					next = interior->GetFront();
-				mask = mask >> 1;
-			}
-
-			nodeStack.push(next);
-		}
-	}
-	
-	return NULL;
 }
 
@@ -3085,5 +3031,5 @@
 				splitsGeom = false;
 
-				if (DotProd(mPlanes[i].mNormal,  splitPlane.mNormal > 0))
+				if (DotProd(mPlanes[i].mNormal, splitPlane.mNormal > 0))
 					back.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
 				else
@@ -3098,17 +3044,17 @@
 	if (planePoly)
 	{
-		// add polygon with reverse orientation to front cell
+        // add polygon with reverse orientation to front cell
 		Plane3 reversePlane(splitPlane);
 		reversePlane.ReverseOrientation();
 
-		//Debug << "poly normal : " << ndummy << " split plane normal " << splitPlane.mNormal << endl;
 		// add polygon with normal pointing into positive half space to back cell
 		back.Add(planePoly, splitPlane);
 		//back.mPolys.push_back(planePoly);
+
 		Polygon3 *reversePoly = planePoly->CreateReversePolygon();
 		front.Add(reversePoly, reversePlane);
 		Debug << "poly normal : " << reversePoly->GetSupportingPlane().mNormal << " split plane normal " << reversePlane.mNormal << endl;
 	}
-
+	else Debug << "err" << endl;
 
 	return splitsGeom;
@@ -3120,15 +3066,18 @@
 {
 	if (!planePoly->Valid(epsilon))
+	{
+		Debug << "not valid!!" << endl;
 		DEL_PTR(planePoly);
-
+	}
 	// polygon is split by all other planes
 	for (int i = 0; (i < (int)mPolys.size()) && planePoly; ++ i)
 	{
-#if 0
-		Plane3 plane = mPolys[i]->GetSupportingPlane();
-#else
+		//Plane3 plane = mPolys[i]->GetSupportingPlane();
 		Plane3 plane = mPlanes[i];
-	
-#endif
+
+		Debug << "*******************" << endl;
+		Debug << "support: " << mPolys[i]->GetSupportingPlane() << endl;
+		Debug << "plane: " << mPlanes[i] << endl;
+
 		/// don't use epsilon here to get exact split planes
 		const int cf = 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 648)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 683)
@@ -167,8 +167,12 @@
 		
 		if (side > 0)
+		{
 			onFrontSide = true;
+		}
 		else if (side < 0)
+		{
 			onBackSide = true;
-		
+		}
+
 		//TODO: check if split goes through vertex
 		if (onFrontSide && onBackSide) // split 
@@ -413,9 +417,8 @@
 							const float epsilon)
 {
-	PolygonContainer::const_iterator it;
-
 	bool onFrontSide = false;
 	bool onBackSide = false;
-	
+	PolygonContainer::const_iterator it;
+
 	// find intersections
 	for (it = polys.begin(); it != polys.end(); ++ it)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 682)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 683)
@@ -218,5 +218,5 @@
   	Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
 
-	if (0) // export initial view cells
+	if (1) // export initial view cells
 	{
 		cout << "exporting initial view cells (=leaves) ... ";
@@ -3486,21 +3486,28 @@
 			mVspBspTree->ConstructGeometry(leaf, geom);
 
-			const int cf = Polygon3::ClassifyPlane(geom.GetPolys(), *clipPlane, 0.0005);
-
-			if (cf == Polygon3::BACK_SIDE)
+			const int cf = Polygon3::ClassifyPlane(geom.GetPolys(), *clipPlane, 0.00000001f);
+
+			/*if (cf == Polygon3::BACK_SIDE)
 			{
 				exporter->ExportPolygons(geom.GetPolys());
 			}
 			else if (cf == Polygon3::SPLIT)
-			{
+			{*/
 				geom.SplitGeometry(front,
 								   back,
 								   *clipPlane,
 								   mViewSpaceBox, 
-								   0.0001f);
-	
-				if (back.Size() >= 3)
+								   0.00000001f);
+	
+				Debug << "here geo size: " << geom.Size() << endl;
+				Debug << "here size b: " << back.Size() << " f: " << front.Size() << endl;
+				if (back.Valid())
+				{
+					Debug << "here3 size: " << back.Size() << endl;
 					exporter->ExportPolygons(back.GetPolys());
-			}
+				}
+				else
+					Debug << "here2 size: " << back.Size() << endl;
+			//}
 		}
 	}
