OctreeNode.Split C# (CSharp) Method

Split() protected method

protected Split ( ) : void
return void
    protected internal void Split()
    {
        // Make sure we're bigger than the minimum, if we care,
        if (MinSize != NoMinSize)
            if (Math.Abs(Bounds.Top - Bounds.Bottom) < MinSize &&
                Math.Abs(Bounds.Right - Bounds.Left) < MinSize &&
                Math.Abs(Bounds.Front - Bounds.Back) < MinSize)
                return;

        var nsHalf = (float)(Bounds.Top - (Bounds.Top - Bounds.Bottom) * 0.5);
        var ewHalf = (float)(Bounds.Right - (Bounds.Right - Bounds.Left) * 0.5);
        var fbHalf = (float)(Bounds.Front - (Bounds.Front - Bounds.Back) * 0.5);

        Branch = new OctreeNode[8];

        Branch[0] = new OctreeNode(ewHalf, Bounds.Left, Bounds.Front, fbHalf, Bounds.Top, nsHalf, MaxItems); //left-front-top
        Branch[1] = new OctreeNode(Bounds.Right, ewHalf, Bounds.Front, fbHalf, Bounds.Top, nsHalf, MaxItems);
        Branch[2] = new OctreeNode(ewHalf, Bounds.Left, Bounds.Front, fbHalf, nsHalf, Bounds.Bottom, MaxItems);
        Branch[3] = new OctreeNode(Bounds.Right, ewHalf, Bounds.Front, fbHalf, nsHalf, Bounds.Bottom, MaxItems);

        Branch[4] = new OctreeNode(ewHalf, Bounds.Left, fbHalf, Bounds.Back, Bounds.Top, nsHalf, MaxItems); //left-back-top
        Branch[5] = new OctreeNode(Bounds.Right, ewHalf, fbHalf, Bounds.Back, Bounds.Top, nsHalf, MaxItems);
        Branch[6] = new OctreeNode(ewHalf, Bounds.Left, fbHalf, Bounds.Back, nsHalf, Bounds.Bottom, MaxItems);
        Branch[7] = new OctreeNode(Bounds.Right, ewHalf, fbHalf, Bounds.Back, nsHalf, Bounds.Bottom, MaxItems);

        var temp = (ArrayList)Items.Clone();
        Items.Clear();
        var things = temp.GetEnumerator();
        while (things.MoveNext())
        {
            AddNode((OctreeLeaf)things.Current);
        }
    }

Usage Example

Beispiel #1
0
            /// <summary>
            /// Try to add an object, starting at this node
            /// </summary>
            public bool TryAddObj(OctreeObject <T> newObj, bool updateCount)
            {
                OctreeNode childNode = this;//Start here
                int        index;

                while (true)
                {
                    if (!childNode.ContainsBounds(ref newObj.boundsMin, ref newObj.boundsMax))
                    { //Doesn't fit in this node. Put it in the parent node.
                        if (childNode.childIndex == -1)
                        {
                            return(false);
                        }

                        childNode.parent.PutObjectInNode(newObj, updateCount);
                        return(true);
                    }

                    index = childNode.BestFitChild(ref newObj.boundsCenter);
                    if (childNode.isLeaf && childNode.localItemCount >= numObjectsAllowed && childNode.ContainsBounds(ref newObj.doubleBoundsMin, ref newObj.doubleBoundsMax))
                    {
                        childNode.Split();//We hit the limit and we can split. We only split if the object will fit in the new bounds
                    }
                    if (!childNode.isLeaf)
                    { //Drop down another level if we have children
                        childNode = childNode.children[index];
                    }
                    else
                    { //Place it here. We have no children and we're under the limit
                        childNode.PutObjectInNode(newObj, updateCount);
                        return(true);
                    }
                }
            }
All Usage Examples Of OctreeNode::Split