Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 299)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 301)
@@ -457,6 +457,7 @@
 
 		if (!poly->Valid())
-			Debug << "Input polygon not valid: " << *poly << endl;
-
+			Debug << "Input polygon not valid: " << *poly 
+				  << endl << "Area: " << poly->GetArea() << endl;
+		
 		++ polysNum;
 	}
@@ -580,7 +581,7 @@
 	if ((tData.mPolygons->size() <= sTermMaxPolygons) || (tData.mDepth >= sTermMaxDepth))
 	{
-//#ifdef _DEBUG
+#ifdef _DEBUG
 		Debug << "subdivision terminated at depth " << tData.mDepth << ", #polys: " << (int)tData.mPolygons->size() << endl;
-//#endif
+#endif
 
 		EvaluateLeafStats(tData);
@@ -735,10 +736,35 @@
 		Polygon3::IncludeInBox(polys, box);
         
-		int axis = box.Size().DrivingAxis();
+		int objectsBack = 0, objectsFront = 0;
+		int axis = 0;
+		float costRatio = MAX_FLOAT;
+		Vector3 position;
+
+		for (int i=0; i < 3; i++) 
+		{
+			float p = 0;
+			float r = 0;/*BestCostRatio(leaf,
+									box,
+									i,
+									p,
+									objectsBack,
+									objectsFront);*/
+			if (r < costRatio)
+			{
+				costRatio = r;
+				axis = i;
+				position = p;
+			}
+		}
+		
+		//if (costRatio > mMaxCostRatio) axis = -1;
+		Vector3 norm(0,0,0); norm[axis] = 1.0f;
+		return Plane3(norm, position);
+
+		/*int axis = box.Size().DrivingAxis();
 		Vector3 norm(0,0,0); norm[axis] = 1;
-
-		Vector3 pt = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
-
-		return Plane3(norm, pt);
+		Vector3 pt = (box.Min()[axis] + box.Max()[axis]) * 0.5f;*/
+		//Debug << "splitting axis aligned" << endl;
+		//return Plane3(norm, pt);
 	}
 
@@ -786,5 +812,5 @@
 		}
 	}
-	Debug << "Plane lowest cost: " << lowestCost << endl;
+	//Debug << "Plane lowest cost: " << lowestCost << endl;
 	
 	return polys[bestPlaneIdx]->GetSupportingPlane();
@@ -798,9 +824,5 @@
 float BspTree::EvalSplitPlane(const PolygonContainer &polys, const Plane3 &candidatePlane) const
 {
-	float sumBalancedPolys = 0;
-	float sumBalancedViewCells = 0;
-	float sumSplits = 0;
-	float sumPolyArea = 0;
-	float verticalAxis = 0;
+	float val = 0;
 	
 	if (sSplitPlaneStrategy & VERTICAL_AXIS)
@@ -809,13 +831,16 @@
 		// want to put a penalty on the dot product between the "tiny" vertical axis
 		// and the split plane axis
-		verticalAxis = fabs(DotProd(candidatePlane.mNormal, tinyAxis)) * (float)polys.size();
+		val += sVerticalSplitsFactor * 
+			   fabs(DotProd(candidatePlane.mNormal, tinyAxis)) * (float)polys.size();
 	}
 	//-- strategies where the effect of the split plane on the polygons is tested
-	if ((sSplitPlaneStrategy & BALANCED_POLYS)      || 
-		(sSplitPlaneStrategy & LEAST_SPLITS)        ||
+	if ((sSplitPlaneStrategy & BALANCED_POLYS)     || 
+		(sSplitPlaneStrategy & LEAST_SPLITS)       ||
 		(sSplitPlaneStrategy & LARGEST_POLY_AREA))
 	{
 		PolygonContainer::const_iterator it, it_end = polys.end();
-		
+		float sumBalancedPolys = 0;
+		float sumSplits = 0;
+
 		for (it = polys.begin(); it != it_end; ++ it)
 		{
@@ -830,8 +855,14 @@
 			if (sSplitPlaneStrategy & LARGEST_POLY_AREA)
 			{
-				Debug << "polygon area: " << (*it)->GetArea() << " ";
-				sumPolyArea += (*it)->GetArea();
+				if (classification == Polygon3::COINCIDENT)
+				{	
+					float area = (*it)->GetArea();
+					//Debug << "polygon area: " << area << " ";
+					if (area) val += 100.0f/area;
+				}
 			}
-		}Debug << "\n";
+		}
+		val += sBalancedPolysFactor * fabs(sumBalancedPolys) + 
+			   sLeastSplitsFactor * sumSplits;
 	}
 	
@@ -841,4 +872,6 @@
 		ObjectContainer frontViewCells;
 		ObjectContainer backViewCells;
+
+		float sumBalancedViewCells = 0;
 
 		PolygonContainer::const_iterator it, it_end = polys.end();
@@ -868,5 +901,5 @@
 			{
 				intersect = *frontIt;
-				sumBalancedViewCells += 1;
+				sumBalancedViewCells += 1.0f;
 			}
 		}
@@ -883,12 +916,10 @@
 			}
 		}
+
+		 val += sBalancedViewCellsFactor * fabs(sumBalancedViewCells);
 	}
 
 	// return linear combination of the sums
-	return sBalancedPolysFactor * fabs(sumBalancedPolys) + 
-		   sBalancedViewCellsFactor * fabs(sumBalancedViewCells) +
-		   sLeastSplitsFactor * sumSplits + 
-		   sVerticalSplitsFactor * verticalAxis +
-		   sumPolyArea;
+	return val;
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 300)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 301)
@@ -122,15 +122,16 @@
 	for (it = mVertices.begin(); it != it_end; ++it)
 		vtx.push_back(rot * (*it));
-	
-	Vector3 cur = vtx.back();
+
+	//-- compute area
+	Vector3 last = vtx.back();
 
 	float area = 0.0f;
 	for (it = vtx.begin(); it != vtx.end(); ++it)
 	{
-		area += cur.y * (*it).x - cur.x * (*it).y;
-
-		cur = *it;
-	}
-
+		area += last.y * (*it).x - last.x * (*it).y;
+
+		last = *it;
+	}
+	//Debug << "area: " << area * 0.5 << endl;
 	return area * 0.5f;
 }
@@ -219,5 +220,8 @@
 	// check if area exceeds certain size
 	if (AREA_LIMIT > GetArea())
+	{
+		//Debug << "area too small: " << GetArea() << endl;
 		return false;
+	}
 #else
 	Vector3 vtx = mVertices.back();
