OctTreeNode how restricts the bounding boxes of its children.
OctTreeNode how restricts the bounding boxes of its children.

--- a/AxisAlignedBox.cpp
+++ b/AxisAlignedBox.cpp
@@ -15,9 +15,19 @@
   : Object(),_minPoint(minPoint),_maxPoint(maxPoint)
 { }
 
+void AxisAlignedBox::setMinPoint(const Vector& minPoint)
+{
+  _minPoint=minPoint;
+}
+
 Vector AxisAlignedBox::minPoint() const
 {
   return _minPoint;
+}
+
+void AxisAlignedBox::setMaxPoint(const Vector& maxPoint)
+{
+  _maxPoint=maxPoint;
 }
 
 Vector AxisAlignedBox::maxPoint() const

--- a/AxisAlignedBox.h
+++ b/AxisAlignedBox.h
@@ -12,7 +12,9 @@
   void fillIntersectionInformation(IntersectionInformation *info) const;
   IntersectionInformation intersection(Ray ray) const;  
 
+  void setMinPoint(const Vector& minPoint);
   Vector minPoint() const;
+  void setMaxPoint(const Vector& maxPoint);
   Vector maxPoint() const;
 
   void insertPoint(Vector point);

file:a/Mesh.cpp -> file:b/Mesh.cpp
--- a/Mesh.cpp
+++ b/Mesh.cpp
@@ -117,7 +117,6 @@
 
   fh.close();
 
-  //_octTree=new OctTreeNode(_triangles);
   _triangles.optimize();
 }
 

file:a/Mesh.h -> file:b/Mesh.h
--- a/Mesh.h
+++ b/Mesh.h
@@ -9,7 +9,6 @@
 #include "Vertex.h"
 #include "Triangle.h"
 #include "AxisAlignedBox.h"
-#include "OctTreeNode.h"
 #include "ObjectList.h"
 
 /**
@@ -32,8 +31,6 @@
   ObjectList _triangles;
   std::vector<Vector> _textureVertices;
 
-  OctTreeNode *_octTree;
-
   AxisAlignedBox _boundingBox;
 
   void loadFile(std::string filename);

--- a/OctTreeNode.cpp
+++ b/OctTreeNode.cpp
@@ -2,9 +2,9 @@
 
 #include <iostream>
 
-OctTreeNode::OctTreeNode(std::vector<Object*> objects)
-{
-  if(objects.size()>10)
+OctTreeNode::OctTreeNode(std::vector<Object*> objects,Vector minPoint,Vector maxPoint,bool forceLeaf)
+{
+  if(objects.size()>10 && !forceLeaf)
     {
       for(std::vector<Object*>::const_iterator iter=objects.begin();
 	  iter!=objects.end();
@@ -13,8 +13,55 @@
 	  _boundingBox.insertPoint((*iter)->minPoint());
 	  _boundingBox.insertPoint((*iter)->maxPoint());
 	}
+      // Ensure that the _boundingBox.minPoint is within 
+      // the boundary defined by minPoint
+      if(!minPoint.isNull())
+	{
+	  if(_boundingBox.minPoint().x()>minPoint.x())
+	    minPoint.setX(_boundingBox.minPoint().x());
+	  if(_boundingBox.minPoint().y()>minPoint.y())
+	    minPoint.setY(_boundingBox.minPoint().y());	  
+	  if(_boundingBox.minPoint().z()>minPoint.z())
+	    minPoint.setZ(_boundingBox.minPoint().z());	  
+	  _boundingBox.setMinPoint(minPoint);
+	}
+      // Do a similar thing with maxPoint
+      if(!maxPoint.isNull())
+	{
+	  if(_boundingBox.maxPoint().x()<maxPoint.x())
+	    maxPoint.setX(_boundingBox.maxPoint().x());
+	  if(_boundingBox.maxPoint().y()<maxPoint.y())
+	    maxPoint.setY(_boundingBox.maxPoint().y());	  
+	  if(_boundingBox.maxPoint().z()<maxPoint.z())
+	    maxPoint.setZ(_boundingBox.maxPoint().z());	  
+	  _boundingBox.setMaxPoint(maxPoint);
+	  }
+
       Vector avg=(_boundingBox.minPoint()+_boundingBox.maxPoint())*0.5;
       std::vector<Object*> partitions[8];
+
+      // Define the dimensions of each partition
+      Vector minPoints[8];
+      minPoints[0]=avg;
+      minPoints[1]=Vector(avg.x(),avg.y(),_boundingBox.minPoint().z());
+      minPoints[2]=Vector(avg.x(),_boundingBox.minPoint().y(),avg.z());
+      minPoints[3]=Vector(avg.x(),_boundingBox.minPoint().y(),_boundingBox.minPoint().z());
+      minPoints[4]=Vector(_boundingBox.minPoint().x(),avg.y(),avg.z());
+      minPoints[5]=Vector(_boundingBox.minPoint().x(),avg.y(),_boundingBox.minPoint().z());
+      minPoints[6]=Vector(_boundingBox.minPoint().x(),_boundingBox.minPoint().y(),avg.z());
+      minPoints[7]=_boundingBox.minPoint();
+
+      Vector maxPoints[8];
+      maxPoints[0]=_boundingBox.maxPoint();
+      maxPoints[1]=Vector(_boundingBox.maxPoint().x(),_boundingBox.maxPoint().y(),avg.z());
+      maxPoints[2]=Vector(_boundingBox.maxPoint().x(),avg.y(),_boundingBox.maxPoint().z());
+      maxPoints[3]=Vector(_boundingBox.maxPoint().x(),avg.y(),avg.z());
+      maxPoints[4]=Vector(avg.x(),_boundingBox.maxPoint().y(),_boundingBox.maxPoint().z());
+      maxPoints[5]=Vector(avg.x(),_boundingBox.maxPoint().y(),avg.z());
+      maxPoints[6]=Vector(avg.x(),avg.y(),_boundingBox.maxPoint().z());
+      maxPoints[7]=avg;
+
+      // Determine which partitions the object is in
       for(std::vector<Object*>::const_iterator iter=objects.begin();
 	  iter!=objects.end();
 	  iter++)
@@ -55,22 +102,14 @@
 	    }
 	}
 
-      bool forceLeaf=false;
+
+      
       for(unsigned int i=0;i<8;i++)
-	if(partitions[i].size()==objects.size())
-	  forceLeaf=true;
-
-      if(!forceLeaf)
-	{
-	  for(unsigned int i=0;i<8;i++)
-	    if(partitions[i].size()>0)
-	      {
-		OctTreeNode *node=new OctTreeNode(partitions[i]);
-		_children.push_back(node);
-	      }
-	}
-      else
-	_objects=objects;
+	if(partitions[i].size()>0)
+	  {
+	    OctTreeNode *node=new OctTreeNode(partitions[i],minPoints[i],maxPoints[i],partitions[i].size()==objects.size());
+	    _children.push_back(node);
+	  }
     }
   else
     _objects=objects;

--- a/OctTreeNode.h
+++ b/OctTreeNode.h
@@ -11,7 +11,7 @@
 class OctTreeNode
 {
 public:
-  OctTreeNode(std::vector<Object*> objects);
+  OctTreeNode(std::vector<Object*> objects,Vector minPoint=Vector(),Vector maxPoint=Vector(),bool forceLeaf=false);
   ~OctTreeNode();
 
   AxisAlignedBox boundingBox() const;