示例#1
0
        private static IEnvelop SumChildBounds(RTreeNode node, int startIndex, int endIndex)
        {
            var retval = new Envelop();

            for (var i = startIndex; i < endIndex; i++)
            {
                retval.Extend(node.Children[i].Envelope);
            }

            return(retval);
        }
示例#2
0
        private void Remove(T item, Envelop envelope)
        {
            var node         = Root;
            var itemEnvelope = envelope;

            var path    = new Stack <RTreeNode>();
            var indexes = new Stack <int>();

            var       i       = 0;
            var       goingUp = false;
            RTreeNode parent  = null;

            // depth-first iterative tree traversal
            while (node != null || path.Count > 0)
            {
                if (node == null)
                {
                    // go up
                    node   = path.TryPop();
                    parent = path.TryPeek();
                    i      = indexes.TryPop();

                    goingUp = true;
                }

                if (node != null && node.IsLeaf)
                {
                    // check current node
                    var index = node.Children.FindIndex(n => Comparer.Equals(item, n.Data));

                    if (index != -1)
                    {
                        // item found, remove the item and condense tree upwards
                        node.Children.RemoveAt(index);
                        path.Push(node);
                        CondenseNodes(path.ToArray());

                        return;
                    }
                }

                if (!goingUp && !node.IsLeaf && node.Envelope.Contains(itemEnvelope))
                {
                    // go down
                    path.Push(node);
                    indexes.Push(i);
                    i      = 0;
                    parent = node;
                    node   = node.Children[0];
                }
                else if (parent != null)
                {
                    i++;
                    if (i == parent.Children.Count)
                    {
                        // end of list; will go up
                        node = null;
                    }
                    else
                    {
                        // go right
                        node    = parent.Children[i];
                        goingUp = false;
                    }
                }
                else
                {
                    node = null;  // nothing found
                }
            }
        }