Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 605)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 607)
@@ -176,10 +176,10 @@
 	# samples used for view cell construction
 	Construction {
-		samples 2500000
+		samples 2000000
 		samplesPerPass 500000
 	}
 
 	#number of active view cells
-	active 256
+	active 2048
 	maxStaticMemory	40
 
@@ -188,5 +188,5 @@
 
 	#type kdTree
-	type vspKdTree
+	#type vspKdTree
 	#type bspTree
 	type vspBspTree
@@ -214,5 +214,5 @@
 		refine false
 		compress false
-		merge false
+		merge true
 	}
 
@@ -304,9 +304,9 @@
 	
 	# maximal candidates for split planes
-	maxPolyCandidates 0
+	maxPolyCandidates 200
 
 	
 	# maximal tested rays for split cost heuristics
-	maxTests 20000
+	maxTests 10000
 	
 	maxTotalMemory	50
@@ -320,16 +320,18 @@
 	}
 	
+	usePolygonSplitIfAvailable 0
+
 	Termination {
 		# parameters used for autopartition
-		minRays 		-1
+		minRays 		500
 		minPolygons 		-1
-		maxDepth		40
+		maxDepth		25
 		minPvs 			-1
 		minProbability		0.0000001
-		maxRayContribution	100000000.2
-		maxCostRatio		9.85
-		missTolerance		2
-		
-		maxViewCells		2048
+		maxRayContribution	0.3
+		maxCostRatio		0.9
+		missTolerance		3
+		
+		maxViewCells		30000
 		
 		# used for pvs criterium
@@ -337,10 +339,10 @@
 		
 		AxisAligned {
-			minRays 		1
+			minRays 		900000
 			maxRayContribution	9.9
 		}
 	}
 	
-	splitUseOnlyDrivingAxis true
+	splitUseOnlyDrivingAxis false
 	
 	Visualization {
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 605)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 607)
@@ -706,4 +706,6 @@
 	
 	float mTotalCost;
+	int mTotalPvsSize;
+
 	int mSplits;
 
@@ -711,4 +713,7 @@
 
 	bool mUseRandomAxis;
+
+	bool mUsePolygonSplitIfAvailable;
+
 private:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 605)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 607)
@@ -497,8 +497,7 @@
 	cout << "actual merge starts now ... " << endl;
 	
-
 	mStats << "#Pass\n" << pass << endl
 		   << "#Merged\n" << mergeStats.merged << endl 
-		   << "#Viewcells\n" << realNumActiveViewCells << endl 
+		   << "#ViewCells\n" << realNumActiveViewCells << endl 
 		   << "#RenderCostIncrease\n" << 0 << endl
 		   << "#TotalRenderCost\n" << totalRenderCost << endl
@@ -508,6 +507,7 @@
 		   << "#Deviation\n" << mDeviation << endl
 		   << "#TotalPvs\n" << totalPvs << endl
-		   << "#PvsSizeDecrease\n" << 0 << endl
-		   << "#Volume\n" << endl;
+		   << "#PvsSizeDecrease\n0" << endl
+		   << "#Volume\n" << endl
+		   << "#Siblings\n" << mergeStats.siblings << endl;
 
 	//-- use priority queue to merge leaf pairs
@@ -604,8 +604,7 @@
 			mergedVc->SetMergeCost(totalRenderCost);
 
-#if VC_HISTORY
-			if (mc.mLeftViewCell->IsSibling(mc.mRightViewCell))
+			if (mViewCellsManager->EqualToSpatialNode(mergedVc))
 				++ mergeStats.siblings;
-#endif
+
 			if (((mergeStats.merged % statsOut) == 0) || 
 			(realNumActiveViewCells == mMergeMinViewCells))
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 605)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 607)
@@ -352,15 +352,18 @@
 										vector<MergeCandidate> &candidates) = 0;
 
-	
-  
-  void CollectViewCells(const int n) {
-	mNumActiveViewCells = n;
-	mViewCells.clear();
-	CollectViewCells();
-  }
+	 
+	void CollectViewCells(const int n) 
+	{
+		mNumActiveViewCells = n;
+		mViewCells.clear();
+		CollectViewCells();
+	}
+
+	bool EqualToSpatialNode(ViewCell *viewCell) { return false;} // implemented in subclasses
+
 
 
 protected:
-
+	
 
 	/**
@@ -753,5 +756,9 @@
 	void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
 
+	bool EqualToSpatialNode(ViewCell *viewCell);
+
 protected:
+
+	bool EqualToSpatialNode(ViewCell *viewCell, BspNode **node);
 
 	/** HACK
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 605)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 607)
@@ -116,4 +116,5 @@
 	environment->GetFloatValue("VspBspTree.Construction.renderCostWeight", mRenderCostWeight);
 
+	environment->GetBoolValue("VspBspTree.usePolygonSplitIfAvailable", mUsePolygonSplitIfAvailable);
 
 	mSubdivisionStats.open("subdivisionStats.log");
@@ -431,9 +432,11 @@
 
 	mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume();
-
+	mTotalPvsSize = tData.mPvs;
+	
 	mSubdivisionStats 
 			<< "#ViewCells\n1\n" <<  endl
 			<< "#RenderCostDecrease\n0\n" << endl 
-			<< "#TotalRenderCost\n" << mTotalCost << endl;
+			<< "#TotalRenderCost\n" << mTotalCost << endl
+			<< "#AvgRenderCost\n" << mTotalPvsSize << endl;
 
 	Debug << "total cost: " << mTotalCost << endl;
@@ -533,12 +536,19 @@
 				float cData = tData.mPvs * tData.mProbability;;
 
-				float costDecr = 
+                float costDecr = 
 					(cFront + cBack - cData) / mBox.GetVolume();
+
+				int pvsFront = tFrontData.mPvs * tFrontData.mProbability;
+				int pvsBack = tBackData.mPvs * tBackData.mProbability;
+				int pvsData = tData.mPvs * tData.mProbability;;
+
 				mTotalCost += costDecr;
+				mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
 
 				mSubdivisionStats 
 						<< "#ViewCells\n" << mBspStats.Leaves() << endl
 						<< "#RenderCostDecrease\n" << -costDecr << endl 
-						<< "#TotalRenderCost\n" << mTotalCost << endl;
+						<< "#TotalRenderCost\n" << mTotalCost << endl
+						<< "#AvgRenderCost\n" << mTotalPvsSize / mBspStats.Leaves() << endl;
 			}
 
@@ -1111,4 +1121,14 @@
 	}
 
+	// cost ratio miss
+	if (!mUsePolygonSplitIfAvailable || data.mPolygons->empty())
+	{
+		frontData.mIsKdNode = backData.mIsKdNode = false;
+		if (lowestCost > mTermMaxCostRatio)
+			return false;
+
+		return true;
+	}
+
 	//-- evaluate axis aligned splits
 	int axis;
@@ -1361,37 +1381,35 @@
 
 		// only render cost heuristics or combined with standard deviation
+		const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit);
+        const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
+		const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
+			
+		const float oldRenderCost = pOverall * (float)penaltyOld + Limits::Small;
+		const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
+
+		float oldCost, newCost;
+
+		// only render cost
 		if (1)
 		{
-			const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit);
-         	const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
-			const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
-			
-			const float oldRenderCost = pOverall * (float)penaltyOld + Limits::Small;
-			const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
-
-			float oldCost, newCost;
-
-			// only render cost
-			if (1)
-			{
-				oldCost = oldRenderCost;
-				newCost = newRenderCost;
-			}
-			else // also considering standard deviation
-			{
-				// standard deviation is difference of back and front pvs
-				const float expectedCost = 0.5f * (penaltyFront + penaltyBack);
-
-				const float newDeviation = 0.5f * 
-					fabs(penaltyFront - expectedCost) + fabs(penaltyBack - expectedCost);
-
-                const float oldDeviation = penaltyOld + Limits::Small;
-
-				newCost = mRenderCostWeight * newRenderCost + (1.0f - mRenderCostWeight) * newDeviation;
-				oldCost = mRenderCostWeight * oldRenderCost + (1.0f - mRenderCostWeight) * oldDeviation;
-			}
-
-			cost += mPvsFactor * newCost / oldCost;
-		}
+			oldCost = oldRenderCost;
+			newCost = newRenderCost;
+		}
+		else // also considering standard deviation
+		{
+			// standard deviation is difference of back and front pvs
+			const float expectedCost = 0.5f * (penaltyFront + penaltyBack);
+
+			const float newDeviation = 0.5f * 
+				fabs(penaltyFront - expectedCost) + fabs(penaltyBack - expectedCost);
+
+			const float oldDeviation = penaltyOld + Limits::Small;
+
+			newCost = mRenderCostWeight * newRenderCost + (1.0f - mRenderCostWeight) * newDeviation;
+			oldCost = mRenderCostWeight * oldRenderCost + (1.0f - mRenderCostWeight) * oldDeviation;
+		}
+
+		cost += mPvsFactor * newCost / oldCost;
+		
 	}
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 599)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 607)
@@ -1909,4 +1909,9 @@
 					"false");
 
+	RegisterOption("VspBspTree.usePolygonSplitIfAvailable",
+					optBool,
+                    "vsp_bsp_usePolygonSplitIfAvailable=",
+					"true");
+
 	RegisterOption("VspBspTree.Termination.AxisAligned.minRays",
 			optInt,
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 605)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 607)
@@ -113,4 +113,5 @@
 
 
+
 /// helper function which destroys rays or copies them into the output ray container
 inline void addOutRays(VssRayContainer &rays, VssRayContainer *outRays)
@@ -2612,4 +2613,47 @@
 
 
+bool VspBspViewCellsManager::EqualToSpatialNode(ViewCell *viewCell)
+{
+	BspNode *node;
+	return EqualToSpatialNode(viewCell, &node);
+}
+
+
+bool VspBspViewCellsManager::EqualToSpatialNode(ViewCell *viewCell, BspNode **node)
+{
+	if (!viewCell->IsLeaf())
+	{
+		BspViewCell *bspVc = dynamic_cast<BspViewCell *>(viewCell);
+
+		*node = bspVc->mLeaf;
+        return true;
+	}
+	else
+	{
+		ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(viewCell);
+
+		// cannot be node of binary tree
+		if (interior->mChildren.size() != 2)
+			return false;
+
+		ViewCell *left = interior->mChildren[0];
+		ViewCell *right = interior->mChildren[1];
+
+		BspNode *leftNode;
+		BspNode *rightNode;
+
+		if (EqualToSpatialNode(left, &leftNode) && EqualToSpatialNode(right, &rightNode))
+		{
+			*node = leftNode->GetParent();
+			return leftNode->IsSibling(rightNode);	
+		}
+
+		*node = NULL;
+
+		return false;
+	}
+}
+
+
 void VspBspViewCellsManager::RefineViewCells(const VssRayContainer &rays,
 											 const ObjectContainer &objects)
