/** Checks the given OctreeNode, and determines if it needs to be moved
        * to a different octant.
        */
        public void UpdateOctreeNode(OctreeNode node) {
            AxisAlignedBox box = node.WorldAABB;
        
            if(box.IsNull) {
                return;
            }

            if(node.Octant == null) {
                //if outside the octree, force into the root node.
                if(!node.IsInBox(octree.Box)) {
                    octree.AddNode(node);
                }
                else {
                    AddOctreeNode(node, octree);
                    return;
                }
            }

            if(!node.IsInBox(node.Octant.Box)) {
                RemoveOctreeNode(node);

                //if outside the octree, force into the root node.
                if(!node.IsInBox(octree.Box)) {
                    octree.AddNode(node);
                }
                else {
                    AddOctreeNode(node,octree);
                }
            }
        }
        public void AddOctreeNode(OctreeNode node, Octree octant, int depth) {
            AxisAlignedBox box = node.WorldAABB;

            //if the octree is twice as big as the scene node,
            //we will add it to a child.
            if((depth < maxDepth) && octant.IsTwiceSize(box)) {
                int x,y,z;

                octant.GetChildIndexes(box, out x, out y, out z);

                if(octant.Children[x,y,z] == null) {
                    octant.Children[x,y,z] = new Octree(octant);

                    Vector3[] corners = octant.Box.Corners;

                    Vector3 min, max;

                    if(x == 0) {
                        min.x = corners[0].x;
                        max.x = (corners[0].x + corners[4].x) / 2;
                    }
                    else {
                        min.x = (corners[0].x + corners[4].x) / 2;
                        max.x = corners[4].x;
                    }

                    if ( y == 0 ) {
                        min.y = corners[ 0 ].y;
                        max.y = ( corners[ 0 ].y + corners[ 4 ].y ) / 2;
                    }

                    else {
                        min.y = ( corners[ 0 ].y + corners[ 4 ].y ) / 2;
                        max.y = corners[ 4 ].y;
                    }

                    if ( z == 0 ) {
                        min.z = corners[ 0 ].z;
                        max.z = ( corners[ 0 ].z + corners[ 4 ].z ) / 2;
                    }

                    else {
                        min.z = ( corners[ 0 ].z + corners[ 4 ].z ) / 2;
                        max.z = corners[ 4 ].z;
                    }

                    octant.Children[x,y,z].Box.SetExtents(min,max);
                    octant.Children[x,y,z].HalfSize = (max - min) / 2;

                }

                AddOctreeNode(node, octant.Children[x,y,z], ++depth);
            }
            else {
                octant.AddNode(node);
            }
        }
		public void AddOctreeNode( OctreeNode node, Octree octant, int depth )
		{
			AxisAlignedBox box = node.WorldAABB;

			//if the octree is twice as big as the scene node,
			//we will add it to a child.
			if ( ( depth < maxDepth ) && octant.IsTwiceSize( box ) )
			{
				int x, y, z;

				octant.GetChildIndexes( box, out x, out y, out z );

				if ( octant.Children[ x, y, z ] == null )
				{
					octant.Children[ x, y, z ] = new Octree( octant );

					Vector3[] corners = octant.Box.Corners;

					Vector3 min, max;

					if ( x == 0 )
					{
						min.x = corners[ 0 ].x;
						max.x = ( corners[ 0 ].x + corners[ 4 ].x ) / 2;
					}
					else
					{
						min.x = ( corners[ 0 ].x + corners[ 4 ].x ) / 2;
						max.x = corners[ 4 ].x;
					}

					if ( y == 0 )
					{
						min.y = corners[ 0 ].y;
						max.y = ( corners[ 0 ].y + corners[ 4 ].y ) / 2;
					}

					else
					{
						min.y = ( corners[ 0 ].y + corners[ 4 ].y ) / 2;
						max.y = corners[ 4 ].y;
					}

					if ( z == 0 )
					{
						min.z = corners[ 0 ].z;
						max.z = ( corners[ 0 ].z + corners[ 4 ].z ) / 2;
					}

					else
					{
						min.z = ( corners[ 0 ].z + corners[ 4 ].z ) / 2;
						max.z = corners[ 4 ].z;
					}

					octant.Children[ x, y, z ].Box.SetExtents( min, max );
					octant.Children[ x, y, z ].HalfSize = ( max - min ) / 2;

				}

				AddOctreeNode( node, octant.Children[ x, y, z ], ++depth );
			}
			else
			{
				octant.AddNode( node );
			}
		}