Index: trunk/VUT/GtpVisibility/include/HierarchyInterface.h
===================================================================
--- trunk/VUT/GtpVisibility/include/HierarchyInterface.h	(revision 154)
+++ trunk/VUT/GtpVisibility/include/HierarchyInterface.h	(revision 155)
@@ -4,4 +4,5 @@
 #include "DistanceQueue.h"
 #include "VisibilityMesh.h"
+#include <stack>
 
 namespace GtpVisibility {
@@ -13,4 +14,5 @@
 typedef std::pair<HierarchyNode *, OcclusionQuery *> QueryPair;
 typedef std::queue<QueryPair> QueryQueue;
+typedef std::stack<HierarchyNode *> TraversalStack;
 
 /**	Class which implements a hierarchy interface for a scene hierarchy.
@@ -28,8 +30,8 @@
 	*/
     virtual bool IsLeaf(HierarchyNode *node) const = 0;
-	/** Traverses the given node. 
+	/** Traverses and renders the given node. 
 		@param node the hierarchy node
 	*/
-	virtual void TraverseNode(HierarchyNode *node) = 0;
+	virtual void TraverseAndRenderNode(HierarchyNode *node) = 0;
 	/** Renders current hierarchy node.
 		@param node current hierarchy node to be rendered
@@ -49,20 +51,23 @@
 		@param root the hierarchy root
 	*/
-	void SetSceneRoot(HierarchyNode *root);
+	void SetHierarchyRoot(HierarchyNode *root);
     /** Get the root of the scene hierarchy.
 		@return the hierarchy root
 	*/
-	HierarchyNode *GetSceneRoot() const;
-	/** Sets the scene root and initialises this scene traverser for a traversal.
-		@param root current scene root
-		@remark initialises some parameters, and also the statistics.
-	*/
-	void InitFrame(HierarchyNode *root);
+	HierarchyNode *GetHierarchyRoot() const;
+	/** Sets the scene root and initialises this hierarchy interface for a traversal.
+		@param frontToBack 
+			if traversal is initialised for front-to-back rendering or
+			simple traversal of the nodes.
+		@remark also resets the statistics evaluated in the last traversal
+	*/
+	void InitTraversal(bool frontToBack = true);
 	/** Returns current frame id.
 		@returns frame id
 	*/
 	unsigned int GetFrameId() const;
-	/** Returns the current distance queue.
-		@returns current distance queue
+	/** Returns a pointer to the distance queue.
+		@returns current distance queue.
+		@remark the distance queue stores hierarchy nodes in a front-to-back order
 	*/
 	DistanceQueue *GetQueue();
@@ -112,9 +117,5 @@
 	*/
 	unsigned int GetNumRenderedNodes();
-	/** Optimization when issuing occlusion test: test is done with actual geometry 
-		rather than the bounding box
-		@param useOptimization if the optimization should be used
-	*/
-	void SetUseOptimization(bool useOptimization);
+		
 	//bool mIsShadowPass;
 
@@ -137,7 +138,10 @@
 	virtual GtpVisibility::OcclusionQuery *IssueOcclusionQuery(GtpVisibility::Mesh *mesh) = 0;
 
-	/** Returns geometry of hierarchy node. 
-	*/
-	virtual void GetGeometry(GtpVisibility::HierarchyNode *node,	
+	/** Returns the geometry of a given hierarchy node. 
+		@param node the hierarchy node containing the geometry
+		@param geometryList geometry is returned in this list
+		@param includeChildren if the geometry of the children should be taken into account
+	*/
+	virtual void GetNodeGeometryList(GtpVisibility::HierarchyNode *node,	
 					 		 GeometryList *geometryList, 
 							 bool includeChildren) = 0;
@@ -154,20 +158,43 @@
 	*/
 	virtual int GetNodeId(HierarchyNode *node) = 0;
+
+	/** This is an optimization when issuing the occlusion test. 
+		The test is done with actual geometry rather than the bounding 
+		box of leave nodes previously marked as visible
+		@param testGeometry if this optimization should be used
+		@remark this option is only useful for the coherent hierarchical culling algorithm
+	*/
+	void TestGeometryForVisibleLeaves(bool testGeometry);
+
+	/** 
+		Traverses hierarchy and returns next node.
+		@returns next node in hierarchy.
+	*/
+	virtual HierarchyNode *GetNextNode() = 0;
+
 protected:
-	
-	bool mUseOptimization;
+	/// chc optimization for testing geometry of leaves instead of bounding box
+	bool mTestGeometryForVisibleLeaves;
+
 	unsigned int mFrameId;
 
 	int mCurrentTestIdx;
 	
-	//--- statistics
+	/// number of traversed nodes
 	unsigned int mNumTraversedNodes;
-
+	/// the distance queue used for rendering of nodes in front to back order.
 	DistanceQueue *mDistanceQueue;
-	HierarchyNode *mSceneRoot;
-	HierarchyNode *mPreviousNode;
+	/// internal structure for traversal
+	TraversalStack *mTraversalStack;
+
+	/// the root of the hierarchy
+	HierarchyNode *mHierarchyRoot;
+	
+	// buffer for a node pointer
+	HierarchyNode *mSavedNode;
 
 	std::vector<HierarchyNode *> mRenderedNodes;
 };
 } // namespace GtpVisibility
+
 #endif // GtpVisisibilityHierarchyInterface_H
Index: trunk/VUT/GtpVisibility/include/CoherentHierarchicalCullingManager.h
===================================================================
--- trunk/VUT/GtpVisibility/include/CoherentHierarchicalCullingManager.h	(revision 147)
+++ trunk/VUT/GtpVisibility/include/CoherentHierarchicalCullingManager.h	(revision 155)
@@ -15,7 +15,12 @@
 public:
 	CoherentHierarchicalCullingManager();
+	/** Constructor taking the assumed visibility into account, i.e., the estimation
+		for how many frames the current visibility is considered to be valid
+	*/
+	CoherentHierarchicalCullingManager(const unsigned int assumedVisibility);
+
 	void RenderScene();
-	/** Sets assumed visibility which is a an estimation for e 
-		how many frames the visibility is considered to be valied
+	/** Sets assumed visibility (i.e., an estimation for 
+		how many frames the visibility is considered to be valid).
 		@param assumedVisibility indicates for how many frames the 
 		same visibility is be assumed.
@@ -32,4 +37,10 @@
 
 	unsigned int mAssumedVisibility;
+	
+	/** Skip query for this node.
+	*/
+	void SkipQuery(HierarchyNode *node);
+
+
 	/** Threshold for rand function to return positive result with respect to
 		mAssumedVisibility.
Index: trunk/VUT/GtpVisibility/include/VisibilityManager.h
===================================================================
--- trunk/VUT/GtpVisibility/include/VisibilityManager.h	(revision 146)
+++ trunk/VUT/GtpVisibility/include/VisibilityManager.h	(revision 155)
@@ -52,11 +52,10 @@
 	void SetVisibilityCullingThreshold(unsigned int threshold);
 
-	/** Determines if optimization for coherent hierarchical culling is used.
-	*/
-	void SetUseChcOptimization(bool useOptimization);
 	/** Indicates for how long a frame is considered to be visible.
 		@param assumedVisibility estimation for how long visibility is assumed to be valid.
+		@remark this option is only valid for the coherent hierarchical culling algorithm
 	*/
-	void SetAssumedVisibility(unsigned int assumedVisibility);
+	void SetAssumedVisibilityForChc(unsigned int assumedVisibility);
+	
 	/** Sets pointer to a query manager. 
 	*/
@@ -75,5 +74,5 @@
 
 	unsigned int mVisibilityThreshold;
-	unsigned int mAssumedVisibility;
+	unsigned int mAssumedVisibilityForChc;
 };
 } // namespace GtpVisibility
Index: trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp	(revision 147)
+++ trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp	(revision 155)
@@ -28,5 +28,5 @@
 		{		
 			mHierarchyInterface->SetNodeVisible(node, true);
-			mHierarchyInterface->TraverseNode(node);
+			mHierarchyInterface->TraverseAndRenderNode(node);
 		}
 	}
Index: trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 148)
+++ trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 155)
@@ -5,8 +5,16 @@
 
 //-----------------------------------------------------------------------
-CoherentHierarchicalCullingManager:: CoherentHierarchicalCullingManager():
-mAssumedVisibility(0), mThreshold(0)
+CoherentHierarchicalCullingManager::CoherentHierarchicalCullingManager()
 {
-	// initialise random generator because of assumed visibility
+	SetAssumedVisibility(0);
+	// initialise random generator in case we use assumed visibility
+	srand(time(NULL));
+}
+//-----------------------------------------------------------------------
+CoherentHierarchicalCullingManager::CoherentHierarchicalCullingManager(const unsigned int 
+																		assumedVisibility)
+{
+	SetAssumedVisibility(assumedVisibility);
+	// initialise random generator in case we use assumed visibility
 	srand(time(NULL));
 }
@@ -32,9 +40,9 @@
 			{
 				mHierarchyInterface->PullUpVisibility(node);
-				mHierarchyInterface->TraverseNode(node);
+				mHierarchyInterface->TraverseAndRenderNode(node);
 			}
 			else
 			{	
-				mNumQueryCulledNodes ++;
+				++ mNumQueryCulledNodes;
 				
 				if(mVisualizeCulledNodes)
@@ -62,4 +70,9 @@
 				}
 			}
+			// -- if node intersects near plane, skip query because wrong results possible
+			else if (intersects)
+			{
+				SkipQuery(node);
+			}
 			else
 			{
@@ -67,18 +80,9 @@
 				bool wasVisible = mHierarchyInterface->IsNodeVisible(node) && 
 					(mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId() - 1);
-
-				// if node is assumed to be visible in this frame => skip query
-				// if intersects near plane => also skip query because wrong results possible
-				bool assumeVisible = intersects ||
-					(wasVisible && (mAssumedVisibility > 0) && 
-					 mHierarchyInterface->HasGeometry(node) && DecideVisible(node));	
-
-				if (assumeVisible)
+					
+				// if we assume node to be visible in this frame => skip query 
+				if (wasVisible && (mAssumedVisibility > 0) &&  DecideVisible(node))// && mHierarchyInterface->HasGeometry(node))
 				{
-					// update node's visited flag
-					mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId());
-
-					mHierarchyInterface->PullUpVisibility(node);			
-					mHierarchyInterface->TraverseNode(node);
+					SkipQuery(node);
 
 					continue;
@@ -107,5 +111,5 @@
 				if (wasVisible)
 				{
-					mHierarchyInterface->TraverseNode(node);
+					mHierarchyInterface->TraverseAndRenderNode(node);
 				}
 			}
@@ -129,9 +133,18 @@
 bool CoherentHierarchicalCullingManager::DecideVisible(HierarchyNode *node) const
 {
-	/*bool result = rand() < mThreshold;
-	std::stringstream d; d << "Assumed vis: " << mAssumedVisibility << ", result: " << result;
+	/*bool result = rand() < mThreshold; std::stringstream d; d << "Assumed vis: " << mAssumedVisibility << ", result: " << result;
 	Ogre::LogManager::getSingleton().logMessage(d.str()); return result;*/
 	
 	return rand() < mThreshold;
 }
+//-----------------------------------------------------------------------
+void CoherentHierarchicalCullingManager::SkipQuery(HierarchyNode *node)
+{
+	// -- set node to be visible in this frame, then traverse it
+	mHierarchyInterface->SetLastVisited(node, mHierarchyInterface->GetFrameId());
+
+	mHierarchyInterface->PullUpVisibility(node);			
+	mHierarchyInterface->TraverseAndRenderNode(node);
+}
+
 } // namespace GtpVisibility
Index: trunk/VUT/GtpVisibility/src/HierarchyInterface.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/HierarchyInterface.cpp	(revision 130)
+++ trunk/VUT/GtpVisibility/src/HierarchyInterface.cpp	(revision 155)
@@ -7,9 +7,9 @@
 //-----------------------------------------------------------------------
 HierarchyInterface::HierarchyInterface(): 
-mFrameId(0), mNumTraversedNodes(0), mSceneRoot(0), 
-mPreviousNode(0), mCurrentTestIdx(0), mUseOptimization(false)
-//, mIsShadowPass(false)
+mFrameId(0), mNumTraversedNodes(0), mHierarchyRoot(NULL), 
+mSavedNode(NULL), mCurrentTestIdx(0), mTestGeometryForVisibleLeaves(false)
 {	
 	mDistanceQueue = new DistanceQueue(GreaterDistance<HierarchyNode *>(this));
+	mTraversalStack = new std::stack<HierarchyNode *>;
 }
 //-----------------------------------------------------------------------
@@ -17,20 +17,28 @@
 {		
 	delete mDistanceQueue;
+	delete mTraversalStack;
 }
 //-----------------------------------------------------------------------
-void HierarchyInterface::SetSceneRoot(HierarchyNode *root)
+void HierarchyInterface::SetHierarchyRoot(HierarchyNode *root)
 {
-	mSceneRoot = root;
+	mHierarchyRoot = root;
 }
 //-----------------------------------------------------------------------
-void HierarchyInterface::InitFrame(HierarchyNode *root)
+void HierarchyInterface::InitTraversal(bool frontToBack)
 {
-	++ mFrameId;
-	mCurrentTestIdx = 0;
-	mNumTraversedNodes = 0;
-	mRenderedNodes.clear();
+	// initialise for front-to-back rendering
+	if (frontToBack)
+	{
+		++ mFrameId;
+		mCurrentTestIdx = 0;
+		mNumTraversedNodes = 0;
+		mRenderedNodes.clear();
 
-	SetSceneRoot(root);
-	mDistanceQueue->push(mSceneRoot);
+		mDistanceQueue->push(mHierarchyRoot);
+	}
+ 	else
+	{	// initialise for simple node traversal
+		mTraversalStack->push(mHierarchyRoot);
+	}
 }
 //-----------------------------------------------------------------------
@@ -51,7 +59,7 @@
 }
 //-----------------------------------------------------------------------
-HierarchyNode *HierarchyInterface::GetSceneRoot() const 
+HierarchyNode *HierarchyInterface::GetHierarchyRoot() const
 {
-	return mSceneRoot;
+	return mHierarchyRoot;
 }
 //-----------------------------------------------------------------------
@@ -66,7 +74,7 @@
 }
 //-----------------------------------------------------------------------
-void HierarchyInterface::SetUseOptimization(bool useOptimization)
+void HierarchyInterface::TestGeometryForVisibleLeaves(bool testGeometry)
 {
-	mUseOptimization = useOptimization;
+	mTestGeometryForVisibleLeaves = testGeometry;
 }
 //-----------------------------------------------------------------------
@@ -75,4 +83,3 @@
 	return &mRenderedNodes;
 }
-
 } // namespace GtpVisibility
Index: trunk/VUT/GtpVisibility/src/VisibilityManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/VisibilityManager.cpp	(revision 153)
+++ trunk/VUT/GtpVisibility/src/VisibilityManager.cpp	(revision 155)
@@ -14,5 +14,5 @@
 mPreprocessingManager(new DummyPreprocessingManager(NULL)),
 mVisibilityEnvironment(visEnvironment),
-mAssumedVisibility(0)
+mAssumedVisibilityForChc(0)
 {
 	SetCullingManager(VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING);
@@ -27,6 +27,6 @@
 }
 //-----------------------------------------------------------------------
-void VisibilityManager::SetCullingManager(
-	VisibilityEnvironment::CullingManagerType ocmType)
+void VisibilityManager::SetCullingManager(VisibilityEnvironment::
+										  CullingManagerType ocmType)
 {
 	// change only if necessary
@@ -38,4 +38,5 @@
 			delete mCullingManager;
 		}
+
 		mCullingManagerType = ocmType;
 		
@@ -47,7 +48,5 @@
 
 		case VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING:
-			mCullingManager = new CoherentHierarchicalCullingManager();
-			dynamic_cast<CoherentHierarchicalCullingManager *>(mCullingManager)->
-				SetAssumedVisibility(mAssumedVisibility);
+			mCullingManager = new CoherentHierarchicalCullingManager(mAssumedVisibilityForChc);
 			break;
 
@@ -98,14 +97,18 @@
 }
 //-----------------------------------------------------------------------
-void VisibilityManager::SetAssumedVisibility(unsigned int assumedVisibility)
+void VisibilityManager::SetAssumedVisibilityForChc(unsigned int assumedVisibility) 
+									 
 {
-	mAssumedVisibility = assumedVisibility;
+	mAssumedVisibilityForChc = assumedVisibility;
 
+	// in case we currently use chc
 	if (VisibilityManager::GetCullingManagerType() == 
 		VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING)
 	{
 		dynamic_cast<CoherentHierarchicalCullingManager *>(mCullingManager)->
-				SetAssumedVisibility(mAssumedVisibility);
+				SetAssumedVisibility(assumedVisibility);
 	}
 }
+
+
 } // namespace GtpVisibility
Index: trunk/VUT/GtpVisibility/src/StopAndWaitCullingManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/StopAndWaitCullingManager.cpp	(revision 139)
+++ trunk/VUT/GtpVisibility/src/StopAndWaitCullingManager.cpp	(revision 155)
@@ -27,13 +27,12 @@
 			}
 		}
+		// if intersects near plane => skip occlusion query because wrong results possible
+		else if (intersects)
+		{
+			mHierarchyInterface->SetNodeVisible(node, true);
+			mHierarchyInterface->TraverseAndRenderNode(node);
+		}
 		else
 		{
-			// if intersects near plane => skip occlusion query because wrong results possible
-			if (intersects)
-			{
-	            mHierarchyInterface->SetNodeVisible(node, true);
-				mHierarchyInterface->TraverseNode(node);
-				continue;
-			}
 
 			++ mNumQueriesIssued;
@@ -46,5 +45,5 @@
 			if (visiblePixels > mVisibilityThreshold)
 			{
-				mHierarchyInterface->TraverseNode(node);
+				mHierarchyInterface->TraverseAndRenderNode(node);
 			}
 			else
Index: trunk/VUT/GtpVisibility/src/DummyQueryManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/DummyQueryManager.cpp	(revision 144)
+++ trunk/VUT/GtpVisibility/src/DummyQueryManager.cpp	(revision 155)
@@ -13,5 +13,5 @@
 					     bool relativeVisibility)
   {
-	  visibleNodes->push_back(NodeInfo(mHierarchyInterface->GetSceneRoot(), 0, 0));
+	  visibleNodes->push_back(NodeInfo(mHierarchyInterface->GetHierarchyRoot(), 0, 0));
     
 //      HierarchyInterface::MeshIterator *mi =
@@ -31,5 +31,5 @@
 						bool relativeVisibility)
   {
-	  visibleNodes->push_back(NodeInfo(mHierarchyInterface->GetSceneRoot(), 0, 0));
+	  visibleNodes->push_back(NodeInfo(mHierarchyInterface->GetHierarchyRoot(), 0, 0));
     
 //      HierarchyInterface::MeshIterator *mi =
