Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreBiHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreBiHierarchy.cpp	(revision 2361)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreBiHierarchy.cpp	(revision 2383)
@@ -1115,4 +1115,6 @@
 BiHierarchy::Node * BiHierarchy::recBuild(BihPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BiHierarchy::Branch * parent)
 {
+#define OLD_METHOD	
+//	859
 	// determine the depth we are on
 	int level = 0;
@@ -1151,21 +1153,48 @@
 	AxisAlignedBox minRights[dim];
 
+	Vector3 MaxBoundingLeft=aabb.getMaximum();
+	Vector3 MinBoundingRight=aabb.getMinimum();
+
+	Vector3 LastPosition=MinBoundingRight;
+
+
+	
+
+
+	bool LeftMod[dim];
+	bool RightMod[dim];
+
 	AxisAlignedBox minLeft;
 	AxisAlignedBox minRight;
+	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;
+	}
 	// try all planes to find the best one for the given set of objects
 	
 	// Use SAH method
+	//mBuildLog->logMessage("------------NewRun:" + StringConverter::toString(nObjects));
 	if (mAlgorithm==BIHAL_SAH)
 	{
+
+#ifdef OLD_METHOD
+		it = begin;
+		
 		while (it != end)
 		{
 			BihPlaneEventList::iterator p = it;
+
+
 			pStart = pOn = pEnd = 0;
 			
 			while (it != end && p->equalsType(*it, BihPlaneEvent::PET_END))
 			{
-				it++; pEnd++;
+				it++; pEnd++; 
 			}
 			while (it != end && p->equalsType(*it, BihPlaneEvent::PET_ON))
@@ -1177,9 +1206,11 @@
 				it++; pStart++;
 			}
-			
+
 			
 			int d = p->getDimension();
+
 			nPlane[d] = pOn;
-			//nRight[d] -= pOn; nRight[d] -= pEnd;
+			nRight[d] -= pOn; nRight[d] -= pEnd;
+
 
             // find the corrected offsets for the left and right bounding box
@@ -1187,55 +1218,249 @@
 			LeftOffset[d]=0;
 			RightOffset[d]=0;
-			minLefts[d].setNull();
-	        minRights[d].setNull();
+			
+			
+			
+	
 			nRight[d]=0;
 			nLeft[d]=0;
+			minLefts[d].setNull();
+			minRights[d].setNull();
+
+            if (minLefts[d].isNull() && minRights[d].isNull())
+			{
+				while (it_a != end)
+				{
+					if ((it_a->getDimension()==d) && (it_a->GetType() == BihPlaneEvent::PET_START))
+					{
+						it_a->getRenderable()->setSide(BihPlaneEvent::PES_BOTH);
+						it_a->getRenderable()->setClassified(false);
+						it_a->classify(*p, split.side,true);
+						if (it_a->getRenderable()->getSide() == BihPlaneEvent::PES_RIGHT)
+						{
+							if (!it_a->getRenderable()->isClassified())
+							{
+								it_a->getRenderable()->setClassified(true);
+								nRight[d]++;
+							}
+							minRights[d].merge(it_a->getRenderable()->getBoundingBox());
+							
+						}
+						// left-only nodes go in left list
+						else if (it_a->getRenderable()->getSide() == BihPlaneEvent::PES_LEFT)
+						{
+							
+							if (!it_a->getRenderable()->isClassified())
+							{
+								it_a->getRenderable()->setClassified(true);
+								nLeft[d]++;
+							}
+							minLefts[d].merge(it_a->getRenderable()->getBoundingBox());
+							
+						}
+					}
+
+
+					it_a++;
+				}
+			}
+
+
+
+			   
+#endif
+
+///////////////////////////////////////////////////////////////////////////
+
+#ifndef OLD_METHOD		
+		it = begin;
+		while (it != end)
+		{
+			it->getRenderable()->setDecided(0,false);
+			it->getRenderable()->setDecided(1,false);
+			it->getRenderable()->setDecided(2,false);
+			it->ClearGrowBB();
+			it++;
+		}
+		it = end;
+		AxisAlignedBox tempBB[3];
+		for (int i=0; i<3; i++) tempBB[i].setNull();
+		while (it != begin)
+		{
+            it--;
+			if ((it->getRenderable()) && (it->equalsType(*it, BihPlaneEvent::PET_START)))
+			{
+			  tempBB[it->getDimension()].merge(it->getRenderable()->getBoundingBox());
+			  it->GrowBB(tempBB[it->getDimension()]);
+			}
 			
-
-			while (it_a != end)
-			{
-				if (it_a->getDimension()==d)
-				{
-					it_a->getRenderable()->setSide(BihPlaneEvent::PES_BOTH);
-					it_a->getRenderable()->setClassified(false);
-					it_a->classify(*p, split.side,true);
-					if (it_a->getRenderable()->getSide() == BihPlaneEvent::PES_RIGHT)
+			
+		}
+
+		it = begin;
+		
+		
+		while (it != end)
+		{
+			
+
+            BihPlaneEventList::iterator p = it;
+			
+			int d = p->getDimension();
+
+
+            // find the corrected offsets for the left and right bounding box
+			
+			LeftOffset[d]=0;
+			RightOffset[d]=0;
+			
+			
+			
+
+
+
+
+			        
+                    bool foundLeft[3];
+					foundLeft[0]=false; foundLeft[1]=false; foundLeft[2]=false;
+					
+			        
+					// set the right border
+					it_a=it;
+					while (it_a!=end)
 					{
-						if (!it_a->getRenderable()->isClassified())
+						if ((it_a->getDimension()==d) && (it_a->GetType() == BihPlaneEvent::PET_START))
 						{
-							it_a->getRenderable()->setClassified(true);
-							nRight[d]++;
+							
+							
+							minRights[d]=it_a->GetGrowBB();
+							//mBuildLog->logMessage("Found Right side: d" + StringConverter::toString(d)+ " " + StringConverter::toString(pos[d]) );
+							break;
 						}
-						minRights[d].merge(it_a->getRenderable()->getBoundingBox());
-						
+						it_a++;
 					}
-					// left-only nodes go in left list
-					else if (it_a->getRenderable()->getSide() == BihPlaneEvent::PES_LEFT)
+
+					
+					it_a=LeftBorder[d];
+					while (it_a!=it)
 					{
-						if (!it_a->getRenderable()->isClassified())
+						if (it_a->getRenderable())
 						{
-							it_a->getRenderable()->setClassified(true);
-							nLeft[d]++;
+							/*
+							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)))
+							{
+								it_a->classify(*it, split.side,true);
+								if  (it_a->getRenderable()->getSide() == BihPlaneEvent::PES_RIGHT)
+								{
+									// 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]) );
+									
+
+								} else
+								{
+									minLefts[d].merge(it_a->getRenderable()->getBoundingBox());
+									it_a->getRenderable()->setDecided(d,true);
+									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]) );
+
+									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]) );
+									} else
+									{
+									    //if (d==0)
+										//mBuildLog->logMessage("Found left   d" + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+										foundLeft[d]=true;
+									}
+
+
+								}
+							} 
+
 						}
-						minLefts[d].merge(it_a->getRenderable()->getBoundingBox());
-						
+						else
+						{
+							//if (d==0)
+							//mBuildLog->logMessage("------No renderable " + StringConverter::toString(d)+ " " + StringConverter::toString(it_a->getPosition()[d]) );
+						}
+						it_a++;
 					}
-				}
-
-
-				it_a++;
-			}
-
-			
+
+
+								
+
+
+
+
+					
+
+			   
+#endif
+			    if (it!=end) it++;
+#ifdef OLD_METHOD
 				p->SAH(aabb, nLeft[d], nPlane[d], nRight[d], split,minLefts[d],minRights[d]);
-				mBuildLog->logMessage("SAH " + StringConverter::toString(best.cost)+ " " + StringConverter::toString(split.cost) + 
-					"dim: " + StringConverter::toString(d));
 				if (split.cost < best.cost)
 				{
 					best = split;
 				}
+#endif
+				/*
+				if (d==0)
+				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]));
+				*/
+#ifndef OLD_METHOD
+				if (it!=end)
+				{
+				if ((it->getPosition()[it->getDimension()]!=LastPosition[it->getDimension()]))
+				{
+					LastPosition[it->getDimension()]=it->getPosition()[it->getDimension()];
+					p->SAH(aabb, nLeft[d], nPlane[d], nRight[d], split,minLefts[d],minRights[d]);
+					if (split.cost < best.cost)
+					{
+						best = split;
+					}
+				}
+				} else
+				{
+					p->SAH(aabb, nLeft[d], nPlane[d], nRight[d], split,minLefts[d],minRights[d]);
+					if (split.cost < best.cost)
+					{
+						best = split;
+					}
+				}
+
+#endif
+
+
+
+				
+				
 			
-
-			//nLeft[d] += pStart; nLeft[d] += pOn; 
+#ifdef OLD_METHOD
+			nLeft[d] += pStart; nLeft[d] += pOn; 
+#endif
 			
 		    nPlane[d] = 0;
@@ -1290,5 +1515,6 @@
 	/************************************************/
 	// check terminating condition
-	if (/*best.cost > BihPlaneEvent::KI*nObjects ||*/ level >= mMaxDepth || nObjects<20 )
+	if (best.cost > BihPlaneEvent::KI*nObjects || level >= mMaxDepth  || nObjects<4 )
+	//if (best.cost > 2*nObjects || level >= mMaxDepth )//|| nObjects<4 )
 	{
 		// Terminating condition reached, create leaf and add renderables to list
@@ -1406,4 +1632,6 @@
 				" O: " + StringConverter::toString(nObjects));
 
+		
+
 
 		// calculate minimum of the left bounding box
@@ -1419,5 +1647,5 @@
 		*/
 
-
+ 
 
 		// create a new branch node and continue recursion
@@ -1426,15 +1654,26 @@
 
 		// now create the child nodes and continue recursion
-		/*
-		if (best.bleft.volume()>minLeft.volume())
+	
+		//if (best.bleft.volume()>minLeft.volume())
 		{
 			mBuildLog->logMessage("Volume Smaller Left " + StringConverter::toString(minLeft.volume()) + " " + StringConverter::toString(best.bleft.volume(),3,3));
-		}
-
-		if (best.bright.volume()>minRight.volume())
+		    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)
@@ -1443,5 +1682,5 @@
 			branch->mLeft = recBuild(eventsLeft, nLeftS + nBothS, /*best.bleft*/minLeft, branch);
 			branch->mLeft->SetToLeft();
-			mBuildLog->logMessage("SetToLeft");
+			//mBuildLog->logMessage("SetToLeft");
 			//branch->mLeft = recBuild(eventsLeft, nLeftS + nBothS, best.bleft, branch);
 		}
@@ -1451,5 +1690,5 @@
 			branch->mRight = recBuild(eventsRight, nBothS + nRightS, /*best.bright*/ minRight, branch);
 			branch->mRight->SetToRight();
-			mBuildLog->logMessage("SetToRight");
+			//mBuildLog->logMessage("SetToRight");
 
 
@@ -2034,6 +2273,8 @@
 	if (showBoxes)
 	{
-        if (mLevel == mOwner->getHiLiteLevel()|| (mLevel-1) == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
+        /*
+		if (mLevel == mOwner->getHiLiteLevel()|| (mLevel-1) == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
 		queue->addRenderable(getWireBoundingBox());
+		*/
 		/*
 		if (mLevel == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
