コード例 #1
0
        private SphereTreeNode CombineLeafNodes(SphereTreeNode s)
        {
            // Remember my own parent, and remove me from that parent
            if (MO.DoLog)
            {
                MO.Log(" Combined {0} with {1}", this, s);
            }
            SphereTreeNode myparent = parent;

            Debug.Assert(myparent != null, "parent was null!");
            parent.RemoveChild(this);
            // Make a new non-leaf node node to hold both
            SphereTreeNode n = new SphereTreeNode(center, radius, sphereTree);

            myparent.AddChild(n);
            n.AddChild(this);
            n.AddChild(s);
            n.RecalculateCenterAndRadius();
            if (MO.DoLog)
            {
                MO.Log(" Made combined node {0} child of {1}", n, myparent);
            }
            return(n);
        }
コード例 #2
0
 private SphereTreeNode FindChildToDemote(SphereTreeNode s)
 {
     if (MO.DoLog)
     MO.Log(" Demote this {0} s {1}", this, s);
     Debug.Assert(s.leafNode, "s isn't a leaf node");
     // All children are occupied, and s is not wholly contained in
     // any of them.  Combine s with some child, creating a
     // subordinate.  Choose the child whose center is nearest
     // s.center.  If the child has a larger radius, insert s in
     // the child.  If the child has a smaller radius, s becomes
     // the child, and the child is inserted in s.
     float closest = float.MaxValue;
     int closestChild = -1;
     for (int i=0; i<childCount; i++) {
     SphereTreeNode child = children[i];
     Debug.Assert(child != null, "Null child!");
     float d = Primitives.DistanceSquared(s.center, child.center);
     if (d < closest) {
         closest = d;
         closestChild = i;
     }
     }
     Debug.Assert (closestChild >= 0, "closestChild not found!");
     SphereTreeNode cs = children[closestChild];
     if (cs.leafNode) {
     if (MO.DoLog)
         MO.Log(" Calling CombineLeafNodes to combine {0} with {1}", cs, this);
     SphereTreeNode n = cs.CombineLeafNodes(s);
     children[closestChild] = n;
     return n;
     }
     else if (cs.ChildSlotsFree() > 0) {
     cs.AddChild(s);
     cs.RecalculateCenterAndRadius();
     return cs;
     }
     else {
     SphereTreeNode n = new SphereTreeNode(sphereTree);
     RemoveChild(cs);
     AddChild(n);
     n.AddChild(cs);
     n.AddChild(s);
     n.RecalculateCenterAndRadius();
     if (MO.DoLog)
         MO.Log(" In demote, made new node n {0}", n);
     return n;
     }
 }
コード例 #3
0
 private SphereTreeNode CombineLeafNodes(SphereTreeNode s)
 {
     // Remember my own parent, and remove me from that parent
     if (MO.DoLog)
     MO.Log(" Combined {0} with {1}", this, s);
     SphereTreeNode myparent = parent;
     Debug.Assert(myparent != null, "parent was null!");
     parent.RemoveChild(this);
     // Make a new non-leaf node node to hold both
     SphereTreeNode n = new SphereTreeNode(center, radius, sphereTree);
     myparent.AddChild(n);
     n.AddChild(this);
     n.AddChild(s);
     n.RecalculateCenterAndRadius();
     if (MO.DoLog)
     MO.Log(" Made combined node {0} child of {1}", n, myparent);
     return n;
 }
コード例 #4
0
        private SphereTreeNode FindChildToDemote(SphereTreeNode s)
        {
            if (MO.DoLog)
            {
                MO.Log(" Demote this {0} s {1}", this, s);
            }
            Debug.Assert(s.leafNode, "s isn't a leaf node");
            // All children are occupied, and s is not wholly contained in
            // any of them.  Combine s with some child, creating a
            // subordinate.  Choose the child whose center is nearest
            // s.center.  If the child has a larger radius, insert s in
            // the child.  If the child has a smaller radius, s becomes
            // the child, and the child is inserted in s.
            float closest      = float.MaxValue;
            int   closestChild = -1;

            for (int i = 0; i < childCount; i++)
            {
                SphereTreeNode child = children[i];
                Debug.Assert(child != null, "Null child!");
                float d = Primitives.DistanceSquared(s.center, child.center);
                if (d < closest)
                {
                    closest      = d;
                    closestChild = i;
                }
            }
            Debug.Assert(closestChild >= 0, "closestChild not found!");
            SphereTreeNode cs = children[closestChild];

            if (cs.leafNode)
            {
                if (MO.DoLog)
                {
                    MO.Log(" Calling CombineLeafNodes to combine {0} with {1}", cs, this);
                }
                SphereTreeNode n = cs.CombineLeafNodes(s);
                children[closestChild] = n;
                return(n);
            }
            else if (cs.ChildSlotsFree() > 0)
            {
                cs.AddChild(s);
                cs.RecalculateCenterAndRadius();
                return(cs);
            }
            else
            {
                SphereTreeNode n = new SphereTreeNode(sphereTree);
                RemoveChild(cs);
                AddChild(n);
                n.AddChild(cs);
                n.AddChild(s);
                n.RecalculateCenterAndRadius();
                if (MO.DoLog)
                {
                    MO.Log(" In demote, made new node n {0}", n);
                }
                return(n);
            }
        }