Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 694)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 703)
@@ -282,4 +282,6 @@
 }
 
+
+
 /************************************************************************/
 /*                class ViewCellsStatistics implementation              */
@@ -451,4 +453,5 @@
 
 	mViewCellsManager->CollectMergeCandidates(rays, candidates);
+
 	while(!candidates.empty())
 	{
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 694)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 703)
@@ -54,5 +54,5 @@
 static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
 
-#define ROTATE_SCENE 1
+#define ROTATE_SCENE 0
 
 
@@ -125,5 +125,5 @@
 			
 			mesh->mFaces.push_back(new Face(vc));
-			//if (ROTATE_SCENE)
+			if (ROTATE_SCENE)
 				RotateMesh(mesh);
 			mesh->Preprocess();
@@ -143,5 +143,5 @@
 		{
 			// HACK
-			//if (ROTATE_SCENE)
+			if (ROTATE_SCENE)
 				RotateMesh(mCurrentMesh);
 			mCurrentMesh->Preprocess();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 697)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 703)
@@ -64,4 +64,40 @@
   };
   
+  /**
+	Candidate for leaf merging based on priority.
+	*/
+	class LocalMergeCandidate
+	{  
+	public:
+		LocalMergeCandidate(ViewCell *l, ViewCell *r);
+
+		/** If this merge pair is still valid.
+		*/
+		//bool IsValid() const;
+	
+		friend bool operator<(const LocalMergeCandidate &leafa, const LocalMergeCandidate &leafb)
+		{
+			return leafb.GetMergeCost() < leafa.GetMergeCost();
+		}
+
+		void SetLeftViewCell(ViewCell *l) {mLeftViewCell = l;}
+		void SetRightViewCell(ViewCell *l) {mRightViewCell = l;}
+
+		ViewCell *GetLeftViewCell() const {return mLeftViewCell;}
+		ViewCell *GetRightViewCell() const { return mRightViewCell;}
+
+		float GetMergeCost() const {return mMergeCost;} 
+		void SetMergeCost(const float cost) {mMergeCost = cost;} 
+
+	protected:
+
+		/// render cost increase by this merge
+		float mMergeCost;
+	
+		ViewCell *mLeftViewCell;
+		ViewCell *mRightViewCell;
+	};
+
+
 
   /// view cell container types
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 664)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 703)
@@ -545,4 +545,6 @@
 	ViewCell *GetRightViewCell() const;
 
+	/** Returns leaf view cell initially associated with this merge candidate.
+	*/
 	ViewCell *GetInitialLeftViewCell() const;
 	ViewCell *GetInitialRightViewCell() const;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 697)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 703)
@@ -544,4 +544,201 @@
 }
 
+/*
+int ViewCellsManager::ConstructLocalMergeTree(vector<LocalMergeCandidate> candidates)
+											 
+{
+	while(!candidates.empty())
+	{
+		MergeCandidate mc = candidates.back();
+		candidates.pop_back();
+		EvalMergeCost(mc);
+		mergeQueue.push(mc);
+	}
+
+	ViewCell::NewMail();
+
+	
+	long startTime = GetTime();
+
+	mergeStats.candidates = (int)mMergeQueue.size();
+
+	// passes are needed for statistics, because we don't want to record
+	// every merge
+	int pass = 0;
+	int mergedPerPass = 0;
+	float realExpectedCost = mExpectedCost;
+	float realAvgRenderCost = mAvgRenderCost;
+	int realNumActiveViewCells = mNumActiveViewCells;
+	
+	 reset.
+
+	int numMergedViewCells = 0;
+
+	environment->GetIntValue("ViewCells.PostProcess.maxMergesPerPass", maxMergesPerPass);
+	environment->GetFloatValue("ViewCells.PostProcess.avgCostMaxDeviation", avgCostMaxDeviation);
+
+	cout << "actual merge starts now ... " << endl;
+
+	//-- use priority queue to merge leaf pairs
+
+	//const float maxAvgCost = 350;
+	while (!mMergeQueue.empty())//NumActiveViewCells > mMergeMinViewCells))
+	{
+		//-- reset merge queue if the ratio of current expected cost / real expected cost
+		//   too small or after a given number of merges
+		if ((mergedPerPass > maxMergesPerPass) ||
+			(avgCostMaxDeviation > mAvgRenderCost / realAvgRenderCost))
+		{
+			// adjust render cost
+			++ pass;
+
+			mergedPerPass = 0;
+			mExpectedCost = realExpectedCost;
+					
+			
+		
+			// refines the view cells
+			// then priorities are recomputed
+			// and the candidates are put back into merge queue
+			if (mRefineViewCells)
+				RefineViewCells(rays, objects);
+			else
+				ResetMergeQueue();
+
+			Debug << "Values after reset: "  
+				  << " erc: " << mExpectedCost 
+				  << " avg: " << mAvgRenderCost 
+				  << " dev: " << mDeviation << endl;
+
+			if (mExportMergedViewCells)
+			{
+				ExportMergedViewCells(activeViewCells, objects, numMergedViewCells);
+			}
+		}
+
+
+		MergeCandidate mc = mMergeQueue.top();
+		mMergeQueue.pop();
+	
+		// both view cells equal
+		// NOTE: do I really still need this? probably cannot happen!!
+		if (mc.mLeftViewCell == mc.mRightViewCell)
+			continue;
+
+		if (mc.IsValid())
+		{
+			ViewCell::NewMail();
+
+			//-- update statistical values
+			-- realNumActiveViewCells;
+			++ mergeStats.merged;
+			++ mergedPerPass;
+
+			const float renderCostIncr = mc.GetRenderCost();
+			const float mergeCostIncr = mc.GetMergeCost();
+
+			totalRenderCost += renderCostIncr;
+			mDeviation += mc.GetDeviationIncr();
+			
+			
+			// merge the view cells of leaf1 and leaf2
+			int pvsDiff;
+			ViewCellInterior *mergedVc = 
+				MergeViewCells(mc.mLeftViewCell, mc.mRightViewCell, pvsDiff);
+
+
+			// total render cost and deviation has changed
+			// real expected cost will be larger than expected cost used for the
+			// cost heuristics, but cannot recompute costs on each increase of the 
+			// expected cost
+			totalPvs += pvsDiff;
+			realExpectedCost = totalRenderCost / (float)realNumActiveViewCells;
+			realAvgRenderCost = (float)totalPvs / (float)realNumActiveViewCells;
+	
+			// set merge cost to this node
+			mergedVc->SetMergeCost(totalRenderCost);
+
+			//if (mViewCellsManager->EqualToSpatialNode(mergedVc))
+			//	++ mergeStats.siblings;
+			mergedVc->SetCost(realExpectedCost);
+
+			if ((mergeStats.merged % statsOut) == 0)
+				cout << "merged " << mergeStats.merged << " view cells" << endl;
+
+		}
+		else
+		{ 
+			// merge candidate not valid, because one of the leaves was already
+			// merged with another one => validate and reinsert into queue
+			if (ValidateMergeCandidate(mc))
+			{
+				EvalMergeCost(mc);
+				mMergeQueue.push(mc);
+			}
+		}
+		
+	}
+
+	
+	// adjust stats and reset queue one final time
+	mExpectedCost = realExpectedCost;
+	mAvgRenderCost = realAvgRenderCost;
+	mNumActiveViewCells = realNumActiveViewCells;
+
+	UpdateActiveViewCells(activeViewCells);
+
+	
+	// refine view cells and reset costs
+	if (mRefineViewCells)
+		RefineViewCells(rays, objects);
+	else
+		ResetMergeQueue();
+
+	// create a root node if the merge was not done till root level,
+	// else take the single node as new root
+	if ((int)activeViewCells.size() > 1)
+	{
+		Debug << "creating root of view cell hierarchy for " 
+			  << (int)activeViewCells.size() << " view cells" << endl;
+		
+		ViewCellInterior *root = mViewCellsManager->MergeViewCells(activeViewCells);
+		root->SetMergeCost(totalRenderCost);
+		// $$JB keep this 0 temporarilly
+		root->SetCost(0.0f);
+
+		mRoot = root;
+	}
+	else if ((int)activeViewCells.size() == 1)
+	{
+		Debug << "setting root of the merge history" << endl;
+		mRoot = activeViewCells[0];
+	}
+
+	//-- empty merge queue just in case
+	while (!mMergeQueue.empty())
+	{
+		mMergeQueue.pop();
+	}
+
+	// TODO delete because makes no sense here
+	mergeStats.expectedRenderCost = realExpectedCost;
+	mergeStats.deviation = mDeviation;
+
+	// we want to optimize this heuristics
+	mergeStats.heuristics = 
+		mDeviation * (1.0f - mRenderCostWeight) + 
+		mExpectedCost * mRenderCostWeight;
+
+	mergeStats.mergeTime = TimeDiff(startTime, GetTime());
+	mergeStats.Stop();
+	Debug << mergeStats << endl << endl;
+
+	// assign colors for the view cells so that at least one is always consistent
+	AssignRandomColors();
+	
+	//TODO: should return sample contributions?
+	return mergeStats.merged;
+}
+*/
 
 bool ViewCellsManager::CheckValidity(ViewCell *vc,
