Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 639)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 648)
@@ -601,5 +601,8 @@
 	int CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
 	
-	
+	/** Preprocesses polygons and throws out all polygons which are coincident to
+		the view space box faces (they can be problematic).
+	*/
+	void PreprocessPolygons(PolygonContainer &polys);
 	
 	/** Propagates valid flag up the tree.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 644)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 648)
@@ -512,5 +512,4 @@
 
 	//-- use priority queue to merge leaf pairs
-// HACK
 
 	//const float maxAvgCost = 350;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 647)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h	(revision 648)
@@ -876,5 +876,7 @@
 				  int &contributingSamples);
 
-
+	/** Preprocesses polygons and throws out all polygons which are coincident to
+		the view space box faces (they can be problematic).
+	*/
 	void PreprocessPolygons(PolygonContainer &polys);
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 643)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 648)
@@ -332,4 +332,5 @@
 			AddMeshToPolygons(obj->GetMesh(), polys, obj);
 			++ numObj;
+
 			//-- compute bounding box
 			if (!forcedBoundingBox)
@@ -381,4 +382,7 @@
 	else 
 		mTermMinProbability *= mBox.GetVolume();
+
+	// throw out unnecessary polygons
+	PreprocessPolygons(polys);
 
 	mBspStats.polys = (int)polys.size();
@@ -1994,4 +1998,51 @@
 
 
+void VspBspTree::PreprocessPolygons(PolygonContainer &polys)
+{
+	// preprocess: throw out polygons coincident to the view space box (not needed)
+	PolygonContainer boxPolys;
+	mBox.ExtractPolys(boxPolys);
+	vector<Plane3> boxPlanes;
+
+	PolygonContainer::iterator pit, pit_end = boxPolys.end();
+
+	// extract planes of box
+	// TODO: can be done more elegantly than first extracting polygons
+	// and take their planes
+	for (pit = boxPolys.begin(); pit != pit_end; ++ pit)
+	{
+		boxPlanes.push_back((*pit)->GetSupportingPlane());
+	}
+
+	pit_end = polys.end();
+
+	for (pit = polys.begin(); pit != pit_end; ++ pit)
+	{
+		vector<Plane3>::const_iterator bit, bit_end = boxPlanes.end();
+		
+		for (bit = boxPlanes.begin(); (bit != bit_end) && (*pit); ++ bit)
+		{
+			const int cf = (*pit)->ClassifyPlane(*bit, mEpsilon);
+
+			if (cf == Polygon3::COINCIDENT)
+			{
+				DEL_PTR(*pit);
+				//Debug << "coincident!!" << endl;
+			}
+		}
+	}
+
+	// remove deleted entries
+	for (int i = 0; i < (int)polys.size(); ++ i)
+	{
+		while (!polys[i] && (i < (int)polys.size()))
+		{
+			swap(polys[i], polys.back());
+			polys.pop_back();
+		}
+	}
+}
+
+
 float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 647)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 648)
@@ -318,4 +318,5 @@
 	Debug << "splitting polygons of node " << this << " with plane " << mPlane << endl;
 #endif
+
 	PolygonContainer::const_iterator it, it_end = polys.end();
 
@@ -331,12 +332,9 @@
 			case Polygon3::COINCIDENT:
 				coincident.push_back(poly);
-				Debug << "!!!Coincident: " << poly->GetSupportingPlane() << endl;
 				break;			
 			case Polygon3::FRONT_SIDE:	
 				frontPolys.push_back(poly);
-				Debug << "!!!front: " << poly->GetSupportingPlane() << endl;
 				break;
 			case Polygon3::BACK_SIDE:
-				Debug << "!!!back: " << poly->GetSupportingPlane() << endl;
 				backPolys.push_back(poly);
 				break;
@@ -710,13 +708,47 @@
 	// preprocess: throw out polygons coincident to the view space box (not needed)
 	PolygonContainer boxPolys;
-	mBox.ExtractPolygons(boxPolys);
+	mBox.ExtractPolys(boxPolys);
 	vector<Plane3> boxPlanes;
-	PolygonContainer::const_iterator pit, pit_end = polys->end();
-
-	for (pit = polys->begin(); pit != pit_end; ++ pit)
-	{
-		
-	}
-}
+
+	PolygonContainer::iterator pit, pit_end = boxPolys.end();
+
+	// extract planes of box
+	// TODO: can be done more elegantly than first extracting polygons
+	// and take their planes
+	for (pit = boxPolys.begin(); pit != pit_end; ++ pit)
+	{
+		boxPlanes.push_back((*pit)->GetSupportingPlane());
+	}
+
+	pit_end = polys.end();
+
+	for (pit = polys.begin(); pit != pit_end; ++ pit)
+	{
+		vector<Plane3>::const_iterator bit, bit_end = boxPlanes.end();
+		
+		for (bit = boxPlanes.begin(); (bit != bit_end) && (*pit); ++ bit)
+		{
+			const int cf = (*pit)->ClassifyPlane(*bit, mEpsilon);
+
+			if (cf == Polygon3::COINCIDENT)
+			{
+				DEL_PTR(*pit);
+				//Debug << "coincident!!" << endl;
+			}
+		}
+	}
+
+	// remove deleted entries
+	for (int i = 0; i < (int)polys.size(); ++ i)
+	{
+		while (!polys[i] && (i < (int)polys.size()))
+		{
+			swap(polys[i], polys.back());
+			polys.pop_back();
+		}
+	}
+}
+
+
 
 void BspTree::Construct(const RayContainer &sampleRays,
@@ -794,5 +826,6 @@
 	}
 
-	PreprocessPolygons();
+	// throw out bad polygons
+	PreprocessPolygons(*polys);
 
 	mStat.polys = (int)polys->size();
@@ -838,4 +871,5 @@
 	}
 
+	PreprocessPolygons(*polys);
 	Debug << "tree has " << (int)polys->size() << " polys, " << (int)sampleRays.size() << " rays" << endl;
 	Construct(polys, rays);
@@ -991,33 +1025,10 @@
 	{
 		int pvsData = tData.mPvs;
+
 		float cData = (float)pvsData * tData.mProbability;
-
 		float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
 		float cBack = (float)tBackData.mPvs * tBackData.mProbability;
-		
-		Debug << "front pvs: " << tFrontData.mPvs << " p: " << tFrontData.mProbability << endl;
-		Debug << "back pvs: " << tBackData.mPvs << " p: " << tBackData.mProbability << endl;
-		Debug << "pvs: " << pvsData << " p: " << tData.mProbability << endl;
-
-		Debug << "data: " << cData << " front: " << cFront << " back: " << cBack << endl;
-		Debug << "cost ratio: " << (cFront + cBack) / cData << endl;
-
-		if (((cFront + cBack) / cData > 1) || (tFrontData.mProbability == 0) || (tBackData.mProbability == 0))
-		{
-			Debug << "ERROR!!" << endl;
-
-			vector<Plane3> halfSpaces;
-			ExtractHalfSpaces(interior, halfSpaces);
-	
-			for (int i = 0; i < (int)halfSpaces.size(); ++ i)
-				Debug << halfSpaces[i] << endl;
-
-			Debug << "geom:\n" << *tData.mGeometry << endl;
-			Debug << "fgeom:\n" << *tFrontData.mGeometry << endl;
-			Debug << "bgeom:\n" << *tBackData.mGeometry << endl;
-		}
 
 		float costDecr = (cFront + cBack - cData) / mBox.GetVolume();
-
 		
 		mTotalCost += costDecr;
@@ -1104,8 +1115,7 @@
 	
 	
-//#ifdef _DEBUG
+#ifdef _DEBUG
 	Debug << interior << endl;
-	Debug << interior->GetPlane() << endl;
-//#endif
+#endif
 	
 
@@ -1148,11 +1158,4 @@
 			frontData.mProbability = frontData.mGeometry->GetVolume();
 			backData.mProbability = tData.mProbability - frontData.mProbability;
-
-			Debug << "************" << endl;
-			Debug << "plane: " << interior->mPlane << endl;
-			Debug << "box: " << mBox << endl;
-			Debug << "all: " << tData.mProbability << " f: " << frontData.mProbability << " b: " << backData.mProbability << endl;
-			Debug << "real back: " << backData.mGeometry->GetVolume() << endl;
-			Debug << "data geom: " << tData.mGeometry->mPolys.size() << " f: " << frontData.mGeometry->mPolys.size() << " b: " << backData.mGeometry->mPolys.size() << endl;
 		}
 	}
@@ -1497,6 +1500,4 @@
 			SplitPlaneCost(poly->GetSupportingPlane(), data);
 
-		Debug << "supporting plane: " << poly->GetSupportingPlane() << endl;
-
 		if (candidateCost < lowestCost)
 		{
@@ -1522,6 +1523,5 @@
 	Debug << "plane lowest cost: " << lowestCost << endl;
 #endif
-	Debug << "choosen plane: " << bestPlane << endl;
-
+	
 	return bestPlane;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 645)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 648)
@@ -224,5 +224,5 @@
 
 	//TODO: remove for performance
-#if 1
+#if 0
 	if (1)
 	{
