Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 341)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 342)
@@ -354,5 +354,5 @@
 		@returns the number of merged view cells
 	*/
-	//int MergeViewCells();
+	int MergeViewCells();
 
 	/** Set to true if new view cells shall be generated in each leaf.
@@ -368,5 +368,5 @@
 
 protected:
-	
+
 	// --------------------------------------------------------------
 	// For sorting objects
@@ -389,4 +389,7 @@
 	};
 
+	BspNode *MergeNodes(BspInterior *node);
+
+	bool MergeNecessary(BspLeaf *front, BspLeaf *back) const;
 
 	/** Evaluates tree stats in the BSP tree leafs.
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 341)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 342)
@@ -1579,16 +1579,12 @@
 	}
 }
-/*
+
 int BspTree::MergeViewCells()
 {
-	int prunedCells = 0;
-
 	stack<BspNode *> nodeStack;
 	stack<BspLeaf *> leafStack;
 
 	nodeStack.push(mRoot);
-
-	ViewCell::NewMail();
-
+	
 	// collect leaves
 	while (!nodeStack.empty()) 
@@ -1600,59 +1596,102 @@
 		if (node->IsLeaf())
 		{
-			//if (!leafStack.empty() && leafStack.top()->mParent() == node->mParent)
-				leafStack.push(dynamic_cast<BspLeaf *>(node));
-
+			BspNode *leaf = dynamic_cast<BspLeaf *>(node);
+
+			// get other leaf we can merge with
+			while (!leafStack.empty() && leafStack.top()->mParent == node->mParent)
+			{
+				leafStack.pop();
+				BspNode *mergedNode = MergeNodes(node->mParent);
+
+				if (mergedNode->IsLeaf())
+					leafStack.push(dynamic_cast<BspLeaf *>(mergedNode));
+				else
+					leafStack.push(dynamic_cast<BspLeaf *>(node));
+			}
 		}
 		else
 		{
-			nodeStack.push_back(node->mFront);
-			nodeStack.push_back(node->mFront);
-		}
-	
-		while (!leafStack.empty())
-		{
-			BspLeaf *leaf = leafStack.top();
-			leafStack.pop();
-		}
-
+			BspInterior *interior = dynamic_cast<BspInterior *>(node);
+			nodeStack.push(interior->mFront);
+			nodeStack.push(interior->mBack);
+		}
+	}
 
 	return 0;
+}
+
+bool BspTree::MergeNecessary(BspLeaf *front, BspLeaf *back) const
+{
+	return true;
+}
+
+BspNode *BspTree::MergeNodes(BspInterior *node)
+{
+	BspLeaf *front = dynamic_cast<BspLeaf *>(node->mFront);
+	BspLeaf *back = dynamic_cast<BspLeaf *>(node->mBack);
+
+	if (!MergeNecessary(front, back))
+		return node;
+
+	DEL_PTR(front->mViewCell);
+	DEL_PTR(front);
+		
+	DEL_PTR(back->mViewCell);
+	DEL_PTR(back);
+
+	BspLeaf *leaf = new BspLeaf(new ViewCell());
+	BspInterior *parent = node->GetParent();
+
+	// replace a link from node's parent
+	if (!node->IsRoot())
+	{
+		parent->ReplaceChildLink(node, leaf);
+		node->SetParent(parent);
+	}
+	else // new root
+	{
+		mRoot = leaf;
+	}
+		
+	return leaf;
+}
+
+/*BspNode *BspTree::MergeNodes(BspNode *node)
+{
+	BspNode *front = MergeNodes(dynamic_cast<BspInterior *>(node)->mFront);
+	BspNode *back = MergeNodes(dynamic_cast<BspInterior *>(node)->mBack);
+
+	if (!front->IsLeaf() && !back->IsLeaf())
+
+	{
+		if (!MergeNecessary(front, back))
+			return node;
+
+		DEL_PTR(front->mViewCell);
+		DEL_PTR(front);
+		
+		DEL_PTR(back->mViewCell);
+		DEL_PTR(back);
+
+		BspLeaf *leaf = new BspLeaf(new ViewCell());
+		BspInterior *parent = node->GetParent();
+
+		// replace a link from node's parent
+		if (!node->IsRoot())
+		{
+			parent->ReplaceChildLink(node, leaf);
+			node->SetParent(parent);
+		}
+		else // new root
+		{
+			mRoot = leaf;
+		}
+		
+		return leaf;
+	}
+
+	return node;
 }*/
-/*
-BspLeaf *BspTree::MergeNodes(BspInterior *interior)
-{
-	BspNode *frontLeaf = MergeNodes(interior->mFront);
-	BspNode *backLeaf = MergeNodes(interior->mBack);
-
-	if (frontLeaf->IsLeaf() && backLeaf->IsLeaf())
-	{
-		if (!MergeNecessary(frontLeaf, backLeaf))
-			return interior;
-
-		DEL_PTR(frontLeaf->mViewCell);
-		DEL_PTR(interior->mFront);
-		
-		DEL_PTR(backLeaf->mViewCell);
-		DEL_PTR(interior->mBack);
-
-		BspLeaf *leaf = new BspLeaf(viewCell);
-					
-		// replace a link from node's parent
-		if (!interior->IsRoot())
-		{
-			parent->ReplaceChildLink(interior, leaf);
-			interior->SetParent(parent);
-		}
-		else // new root
-		{
-			mRoot = leaf;
-		}
-		
-		return leaf;
-	}
-
-	return interior;
-}
-*/
+
 void BspTree::SetGenerateViewCells(int generateViewCells)
 {
