Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreBiHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreBiHierarchy.cpp	(revision 2383)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreBiHierarchy.cpp	(revision 2390)
@@ -364,7 +364,7 @@
 	Vector3 bmin = parent.getMinimum();
 	Vector3 bmax = parent.getMaximum();
-	Real mu=lookupPenalty(
+	Real mu=/*lookupPenalty(
 		(mPosition[mDimension] - bmin[mDimension]) / 
-		(bmax[mDimension] - bmin[mDimension]));
+		(bmax[mDimension] - bmin[mDimension]));*/ 1;
 	Real sav = surfaceArea(parent);
 	Real pl = surfaceArea(split.bleft) / sav;
@@ -1115,6 +1115,7 @@
 BiHierarchy::Node * BiHierarchy::recBuild(BihPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BiHierarchy::Branch * parent)
 {
-#define OLD_METHOD	
-//	859
+#undef OLD_METHOD	
+#undef DEBUG_BUILD_TREE
+
 	// determine the depth we are on
 	int level = 0;
@@ -1131,5 +1132,5 @@
 	// initialize
 	const int dim = 3;
-	int pStart, pOn, pEnd;
+//	int pStart, pOn, pEnd;
 	int nLeft[dim], nPlane[dim], nRight[dim];
 	int nObjectCount[dim];
@@ -1150,30 +1151,26 @@
 	BihPlaneEventList::iterator it_a = begin; // iterator for adjusting the bounding box
     
+	// hold the minimum left and right bounding box
 	AxisAlignedBox minLefts[dim];
 	AxisAlignedBox minRights[dim];
 
-	Vector3 MaxBoundingLeft=aabb.getMaximum();
-	Vector3 MinBoundingRight=aabb.getMinimum();
-
-	Vector3 LastPosition=MinBoundingRight;
-
-
-	
-
-
-	bool LeftMod[dim];
-	bool RightMod[dim];
-
+
+
+	Vector3 LastPosition=aabb.getMinimum(); // needed to remember the last split plane position 
+
+	// needed for the final sort before splitting
 	AxisAlignedBox minLeft;
 	AxisAlignedBox minRight;
+
+	// the border which seperates finished and unfinished events
 	BihPlaneEventList::iterator LeftBorder[dim];
 	
     for (int i=0; i<dim; i++)
 	{
-	  minLefts[i].setNull();			
-	  minRights[i]=aabb;
-	  nRight[i]=nObjects;
-	  nLeft[i]=0;
-	  LeftBorder[i]=it;
+	  minLefts[i].setNull(); // left aabb starts with null
+	  minRights[i]=aabb; // right aabb contains all objects
+	  nRight[i]=nObjects; // all objects are on the right side
+	  nLeft[i]=0; 
+	  LeftBorder[i]=it; // set the left border to the first event
 	}
 	// try all planes to find the best one for the given set of objects
@@ -1184,4 +1181,5 @@
 	{
 
+// slow implementation. for every event the left and right bounding boxes are calculated.
 #ifdef OLD_METHOD
 		it = begin;
@@ -1276,7 +1274,9 @@
 		while (it != end)
 		{
-			it->getRenderable()->setDecided(0,false);
-			it->getRenderable()->setDecided(1,false);
-			it->getRenderable()->setDecided(2,false);
+			for (int i=0; i<dim; i++)
+			{
+			  it->getRenderable()->setDecided(i,false);
+			  it->getRenderable()->setActualSide(i,BihPlaneEvent::PES_RIGHT);
+			}
 			it->ClearGrowBB();
 			it++;
@@ -1284,13 +1284,15 @@
 		it = end;
 		AxisAlignedBox tempBB[3];
-		for (int i=0; i<3; i++) tempBB[i].setNull();
+		for (int i=0; i<dim; i++) tempBB[i].setNull();
+		
 		while (it != begin)
 		{
             it--;
-			if ((it->getRenderable()) && (it->equalsType(*it, BihPlaneEvent::PET_START)))
+			if ((it->getRenderable())) //&& (it->equalsType(*it, BihPlaneEvent::PET_START)))
 			{
 			  tempBB[it->getDimension()].merge(it->getRenderable()->getBoundingBox());
 			  it->GrowBB(tempBB[it->getDimension()]);
 			}
+			
 			
 			
@@ -1334,5 +1336,7 @@
 							
 							minRights[d]=it_a->GetGrowBB();
-							//mBuildLog->logMessage("Found Right side: d" + StringConverter::toString(d)+ " " + StringConverter::toString(pos[d]) );
+#ifdef DEBUG_BUILD_TREE
+							mBuildLog->logMessage("Found Right side: d" + StringConverter::toString(d)+ " " + StringConverter::toString(pos[d]) );
+#endif							
 							break;
 						}
@@ -1346,18 +1350,6 @@
 						if (it_a->getRenderable())
 						{
-							/*
-							if (it_a->getDimension()==0 && d==0)
-							{
-								mBuildLog->logMessage("--------Enter Loop: d"  );
-								if (it_a->GetType() == BihPlaneEvent::PET_START)
-									mBuildLog->logMessage("--------START"  );
-								if (it_a->GetType() == BihPlaneEvent::PET_END)
-									mBuildLog->logMessage("--------END"  );
-								if (it_a->GetType() == BihPlaneEvent::PET_ON)
-									mBuildLog->logMessage("--------ON"  );
-
-							}
-							*/
-							if ((it_a->getDimension()==d) /*&& (it_a->GetType() == BihPlaneEvent::PET_START) */&& (!it_a->getRenderable()->isDecided(d)))
+							
+							if ((it_a->getDimension()==d)  && (!it_a->getRenderable()->isDecided(d)))
 							{
 								it_a->classify(*it, split.side,true);
@@ -1366,9 +1358,8 @@
 									// right side
 									
-									
-									minRights[d].merge(it_a->GetGrowBB());
-									//if (d==0)
-									//mBuildLog->logMessage("Move to Right side: d" + StringConverter::toString(d)+ " " + StringConverter::toString(pos[d]) );
-									
+									minRights[d].merge(it_a->getRenderable()->getBoundingBox());
+#ifdef DEBUG_BUILD_TREE
+									mBuildLog->logMessage("Move to Right side: d" + StringConverter::toString(d)+ " " + StringConverter::toString(pos[d]) );
+#endif									
 
 								} else
@@ -1376,18 +1367,23 @@
 									minLefts[d].merge(it_a->getRenderable()->getBoundingBox());
 									it_a->getRenderable()->setDecided(d,true);
+									it_a->getRenderable()->setActualSide(d,BihPlaneEvent::PES_LEFT);
+
+
 									Vector3 pos=minLefts[d].getMaximum();
 									nLeft[d]++; nRight[d]--;
-									//if (d==0)
-                                    //mBuildLog->logMessage("Move to Left side: d" + StringConverter::toString(d)+ " " + StringConverter::toString(pos[d]) );
-
+#ifdef DEBUG_BUILD_TREE
+                                    mBuildLog->logMessage("Move to Left side: d" + StringConverter::toString(d)+ " " + StringConverter::toString(pos[d]) );
+#endif
 									if ((it_a->getPosition()[d]>LeftBorder[d]->getPosition()[d]) && (!foundLeft[d]))
 									{
 										LeftBorder[d]=it_a;
-										//if (d==0)
-										//mBuildLog->logMessage("Adjust left to  d" + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+#ifdef DEBUG_BUILD_TREE
+										mBuildLog->logMessage("Adjust left to  d" + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+#endif									
 									} else
 									{
-									    //if (d==0)
-										//mBuildLog->logMessage("Found left   d" + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+#ifdef DEBUG_BUILD_TREE
+										mBuildLog->logMessage("Found left   d" + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+#endif										
 										foundLeft[d]=true;
 									}
@@ -1400,19 +1396,11 @@
 						else
 						{
-							//if (d==0)
-							//mBuildLog->logMessage("------No renderable " + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+#ifdef DEBUG_BUILD_TREE
+							mBuildLog->logMessage("------No renderable " + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+#endif						
 						}
 						it_a++;
 					}
-
-
-								
-
-
-
-
-					
-
-			   
+		   
 #endif
 			    if (it!=end) it++;
@@ -1421,9 +1409,8 @@
 				if (split.cost < best.cost)
 				{
+					if ((nLeft[d]!=0) && (nRight[d]!=0))
 					best = split;
 				}
-#endif
-				/*
-				if (d==0)
+#ifdef DEBUG_BUILD_TREE
 				mBuildLog->logMessage("SAH " + StringConverter::toString(best.cost)+ " " + StringConverter::toString(split.cost) + 
 					"dim: " + StringConverter::toString(d)+ "Elements: " + StringConverter::toString(nLeft[d]) +  
@@ -1431,5 +1418,13 @@
 					" " + StringConverter::toString(it->getPosition()[d]) +" " + StringConverter::toString(minRights[d].getMinimum()[d])
 					+" " + StringConverter::toString(LeftBorder[d]->getPosition()[d]));
-				*/
+
+				    mBuildLog->logMessage("   BOXES  " + StringConverter::toString(minLefts[d].getMaximum()[d]-minLefts[d].getMinimum()[d]) +
+					" " + StringConverter::toString(minRights[d].getMaximum()[d]-minRights[d].getMinimum()[d]));
+#endif // DEBUG_BUILD_TREE
+				
+#endif // OLD_METHOD
+				
+				
+				
 #ifndef OLD_METHOD
 				if (it!=end)
@@ -1441,7 +1436,38 @@
 					if (split.cost < best.cost)
 					{
-						best = split;
+						if ((nLeft[d]!=0) && (nRight[d]!=0))
+						{
+							best = split;
+
+							it_a=begin;
+							while (it_a!=end)
+							{
+								if (it_a->getRenderable()) it_a->getRenderable()->FinalizeSide(d);
+								it_a++;
+							}
+#ifdef DEBUG_BUILD_TREE
+							mBuildLog->logMessage("SAH " + StringConverter::toString(best.cost)+ " " + StringConverter::toString(split.cost) + 
+							"dim: " + StringConverter::toString(d)+ "Elements: " + StringConverter::toString(nLeft[d]) +  
+							" " + StringConverter::toString(nRight[d])+ "Planes: "+ StringConverter::toString(minLefts[d].getMaximum()[d]) +
+							" " + StringConverter::toString(it->getPosition()[d]) +" " + StringConverter::toString(minRights[d].getMinimum()[d])
+							+" " + StringConverter::toString(LeftBorder[d]->getPosition()[d])+ " " + StringConverter::toString(it->getPosition()[it->getDimension()]));
+						    
+							mBuildLog->logMessage("BOXES Left " + StringConverter::toString(minLefts[d].getMaximum()[d]-minLefts[d].getMinimum()[d]) +
+							" " + StringConverter::toString(minRights[d].getMaximum()[d]-minRights[d].getMinimum()[d]));
+#endif
+						}
+
 					}
+					
+					
+					
+					
+				} else
+				{
+#ifdef DEBUG_BUILD_TREE
+					mBuildLog->logMessage("Cancel " + StringConverter::toString(it->getPosition()[it->getDimension()]));
+#endif
 				}
+
 				} else
 				{
@@ -1449,6 +1475,22 @@
 					if (split.cost < best.cost)
 					{
-						best = split;
+						if ((nLeft[d]!=0) && (nRight[d]!=0))
+						{
+							best = split;
+							it_a=begin;
+							while (it_a!=end)
+							{
+								if (it_a->getRenderable()) it_a->getRenderable()->FinalizeSide(d);
+								it_a++;
+							}
+						}
 					}
+#ifdef DEBUG_BUILD_TREE
+					mBuildLog->logMessage("SAH " + StringConverter::toString(best.cost)+ " " + StringConverter::toString(split.cost) + 
+					"dim: " + StringConverter::toString(d)+ "Elements: " + StringConverter::toString(nLeft[d]) +  
+					" " + StringConverter::toString(nRight[d])+ "Planes: "+ StringConverter::toString(minLefts[d].getMaximum()[d]) +
+					" " + StringConverter::toString(it->getPosition()[d]) +" " + StringConverter::toString(minRights[d].getMinimum()[d])
+					+" " + StringConverter::toString(LeftBorder[d]->getPosition()[d])+ " " + StringConverter::toString(it->getPosition()[it->getDimension()]));
+#endif
 				}
 
@@ -1515,10 +1557,12 @@
 	/************************************************/
 	// check terminating condition
-	if (best.cost > BihPlaneEvent::KI*nObjects || level >= mMaxDepth  || nObjects<4 )
-	//if (best.cost > 2*nObjects || level >= mMaxDepth )//|| nObjects<4 )
+	
+	if (best.cost > BihPlaneEvent::KI*nObjects || level >= mMaxDepth  || nObjects<2 )
 	{
 		// Terminating condition reached, create leaf and add renderables to list
+#ifdef DEBUG_BUILD_TREE
 		mBuildLog->logMessage("Terminate " + StringConverter::toString(best.cost)+ " o:" + StringConverter::toString(nObjects) + 
 				" ki: " + StringConverter::toString(BihPlaneEvent::KI*nObjects)+ " dim: " + StringConverter::toString(best.event.getDimension()));
+#endif
 		BiHierarchy::Leaf * leaf = new BiHierarchy::Leaf(this, level, aabb, parent);
 		if (parent->GetSide()==1) leaf->SetToLeft(); else leaf->SetToRight();
@@ -1579,71 +1623,40 @@
 		{
 			// right-only nodes go in right list
-			if (it->getRenderable()->getSide() == BihPlaneEvent::PES_RIGHT)
-			{
-				if (!it->getRenderable()->isClassified())
+			if (it->getRenderable())
+			{
+				if (it->getRenderable()->GetFinalSide(best.event.getDimension()) == BihPlaneEvent::PES_RIGHT)
 				{
-					it->getRenderable()->setClassified(true);
-					nRightS++;
+					if (!it->getRenderable()->isClassified())
+					{
+						it->getRenderable()->setClassified(true);
+						nRightS++;
+					}
+					minRight.merge(it->getRenderable()->getBoundingBox());
+					eventsRight.push_back(*it);
 				}
-				minRight.merge(it->getRenderable()->getBoundingBox());
-				//mBuildLog->logMessage("merge right " + StringConverter::toString(minRight.volume()));
-				eventsRight.push_back(*it);
-			}
-			// left-only nodes go in left list
-			else if (it->getRenderable()->getSide() == BihPlaneEvent::PES_LEFT)
-			{
-				if (!it->getRenderable()->isClassified())
+				// left-only nodes go in left list
+				else if (it->getRenderable()->GetFinalSide(best.event.getDimension()) == BihPlaneEvent::PES_LEFT)
 				{
-					it->getRenderable()->setClassified(true);
-					nLeftS++;
+					if (!it->getRenderable()->isClassified())
+					{
+						it->getRenderable()->setClassified(true);
+						nLeftS++;
+					}
+					minLeft.merge(it->getRenderable()->getBoundingBox());
+					eventsLeft.push_back(*it);
 				}
-				minLeft.merge(it->getRenderable()->getBoundingBox());
-				//mBuildLog->logMessage("merge left " + StringConverter::toString(minLeft.volume()));
-				eventsLeft.push_back(*it);
-			}
-			// remaining nodes go in both lists, bust must be clipped to prevent
-			// events from lying outside the new nodes aabb
-			else
-			{
-				if (!it->getRenderable()->isClassified())
-				{
-					it->getRenderable()->setClassified(true);
-					nBothS++;
-				}
-				
-				// calculate the intersecting box for the right side
-                
-                 
-				  minRight.merge(best.bright.intersection(it->getRenderable()->getBoundingBox()));
-				
-
-				//mBuildLog->logMessage("merge Right Overlap " + StringConverter::toString(minRight.volume()));
-				eventsRight.push_back(it->clip(best.bright, best.event.getDimension()));
-				
-				  minLeft.merge(best.bleft.intersection(it->getRenderable()->getBoundingBox()));
-				
-				//mBuildLog->logMessage("merge Left Overlap " + StringConverter::toString(minLeft.volume()));
-				eventsLeft.push_back(it->clip(best.bleft, best.event.getDimension()));
-			}
+			}
+			
 			it++;
 		}
-
-		mBuildLog->logMessage("Split R:" + StringConverter::toString(nRightS)+ " L:" + StringConverter::toString(nLeftS) + 
-				" O: " + StringConverter::toString(nObjects));
-
+#ifdef DEBUG_BUILD_TREE
+		mBuildLog->logMessage("Split " + StringConverter::toString(best.cost)+ " o:" + StringConverter::toString(nObjects) + 
+				" ki: " + StringConverter::toString(BihPlaneEvent::KI*nObjects)+ " dim: " + StringConverter::toString(best.event.getDimension()));
+#endif
+	
 		
 
 
-		// calculate minimum of the left bounding box
-		/*
-		AxisAlignedBox minimum;
-		minimum.setNull();
-		it=eventsLeft.begin();
-		while (it!=eventsLeft.end())
-		{
-			minimum.merge(it->getRenderable()->getBoundingBox());
-			it++;
-		}
-		*/
+		
 
  
@@ -1654,34 +1667,11 @@
 
 		// now create the child nodes and continue recursion
-	
-		//if (best.bleft.volume()>minLeft.volume())
-		{
-			mBuildLog->logMessage("Volume Smaller Left " + StringConverter::toString(minLeft.volume()) + " " + StringConverter::toString(best.bleft.volume(),3,3));
-		    if (best.bleft.volume()!=minLeft.volume())
-			{
-				mBuildLog->logMessage("Error Volume Left "   + StringConverter::toString(best.bleft.getMinimum())+ " " + StringConverter::toString(best.bleft.getMaximum()));
-				mBuildLog->logMessage("Error Volume Left " + StringConverter::toString(minLeft.getMinimum()) + " " + StringConverter::toString(minLeft.getMaximum()) );
-			}
-
-		}
-
-		//if (best.bright.volume()>minRight.volume())
-		{
-			mBuildLog->logMessage("Volume Smaller Right " + StringConverter::toString(minRight.volume()) + " " + StringConverter::toString(best.bright.volume(),3,3));
-			if (best.bright.volume()!=minRight.volume())
-			{
-				mBuildLog->logMessage("Error Volume Right " + StringConverter::toString(best.bright.getMinimum())+ " " + StringConverter::toString(best.bright.getMaximum()));
-				mBuildLog->logMessage("Error Volume Right " + StringConverter::toString(minRight.getMinimum()) + " " + StringConverter::toString(minRight.getMaximum()) );
-			}
-		}
-		
 
 		if (eventsLeft.size() > 0)
 		{
 			
-			branch->mLeft = recBuild(eventsLeft, nLeftS + nBothS, /*best.bleft*/minLeft, branch);
+			branch->mLeft = recBuild(eventsLeft, nLeftS + nBothS, minLeft, branch);
 			branch->mLeft->SetToLeft();
-			//mBuildLog->logMessage("SetToLeft");
-			//branch->mLeft = recBuild(eventsLeft, nLeftS + nBothS, best.bleft, branch);
+			
 		}
 		if (eventsRight.size() > 0)
@@ -1690,8 +1680,5 @@
 			branch->mRight = recBuild(eventsRight, nBothS + nRightS, /*best.bright*/ minRight, branch);
 			branch->mRight->SetToRight();
-			//mBuildLog->logMessage("SetToRight");
-
-
-			//branch->mRight = recBuild(eventsRight, nBothS + nRightS, best.bright, branch);
+			
 		}
 
@@ -2277,8 +2264,8 @@
 		queue->addRenderable(getWireBoundingBox());
 		*/
-		/*
+		
 		if (mLevel == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
 			queue->addRenderable(getWireBoundingBox());
-		*/
+		
 	}
 
