示例#1
0
        private int AllocateNode()
        {
            // Expand the node pool as needed.
            if (_freeList == NullNode)
            {
                Debug.Assert(_nodeCount == _nodeCapacity);

                // The free list is empty. Rebuild a bigger pool.
                DynamicTreeNode[] oldNodes = _nodes;
                _nodeCapacity *= 2;
                _nodes         = new DynamicTreeNode[_nodeCapacity];
                Array.Copy(oldNodes, _nodes, _nodeCount);

                // Build a linked list for the free list. The parent
                // pointer becomes the "next" pointer.
                for (int i = _nodeCount; i < _nodeCapacity - 1; ++i)
                {
                    _nodes[i].parentOrNext = i + 1;
                }
                _nodes[_nodeCapacity - 1].parentOrNext = NullNode;
                _freeList = _nodeCount;
            }

            // Peel a node off the free list.
            int nodeId = _freeList;

            _freeList = _nodes[nodeId].parentOrNext;
            _nodes[nodeId].parentOrNext = NullNode;
            _nodes[nodeId].child1       = NullNode;
            _nodes[nodeId].child2       = NullNode;
            ++_nodeCount;
            return(nodeId);
        }
示例#2
0
        /// Query an AABB for overlapping proxies. The callback class
        /// is called for each proxy that overlaps the supplied AABB.
        public void Query(Action <int> callback, ref AABB aabb)
        {
            int count = 0;

            stack[count++] = _root;

            while (count > 0)
            {
                int nodeId = stack[--count];
                if (nodeId == NullNode)
                {
                    continue;
                }

                DynamicTreeNode node = _nodes[nodeId];

                if (AABB.TestOverlap(ref node.aabb, ref aabb))
                {
                    if (node.IsLeaf())
                    {
                        callback(node.userData);
                    }
                    else
                    {
                        Debug.Assert(count + 1 < k_stackSize);
                        stack[count++] = node.child1;
                        stack[count++] = node.child2;
                    }
                }
            }
        }
示例#3
0
        private int ComputeHeight(int nodeId)
        {
            if (nodeId == NullNode)
            {
                return(0);
            }

            Debug.Assert(0 <= nodeId && nodeId < _nodeCapacity);
            DynamicTreeNode node    = _nodes[nodeId];
            int             height1 = ComputeHeight(node.child1);
            int             height2 = ComputeHeight(node.child2);

            return(1 + Math.Max(height1, height2));
        }
示例#4
0
        /// ructing the tree initializes the node pool.
        public DynamicTree()
        {
            _root = NullNode;

            _nodeCapacity = 16;
            _nodeCount    = 0;
            _nodes        = new DynamicTreeNode[_nodeCapacity];

            // Build a linked list for the free list.
            for (int i = 0; i < _nodeCapacity - 1; ++i)
            {
                _nodes[i].parentOrNext = i + 1;
            }
            _nodes[_nodeCapacity - 1].parentOrNext = NullNode;
            _freeList = 0;

            _path = 0;
        }
示例#5
0
        /// Ray-cast against the proxies in the tree. This relies on the callback
        /// to perform a exact ray-cast in the case were the proxy contains a shape.
        /// The callback also performs the any collision filtering. This has performance
        /// roughly equal to k * log(n), where k is the number of collisions and n is the
        /// number of proxies in the tree.
        /// @param input the ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
        /// @param callback a callback class that is called for each proxy that is hit by the ray.
        public void RayCast(RayCastCallback callback, ref RayCastInput input)
        {
            Vector2 p1 = input.p1;
            Vector2 p2 = input.p2;
            Vector2 r  = p2 - p1;

            Debug.Assert(r.LengthSquared() > 0.0f);
            r.Normalize();

            // v is perpendicular to the segment.
            Vector2 v     = MathUtils.Cross(1.0f, r);
            Vector2 abs_v = MathUtils.Abs(v);

            // Separating axis for segment (Gino, p80).
            // |dot(v, p1 - c)| > dot(|v|, h)

            float maxFraction = input.maxFraction;

            // Build a bounding box for the segment.
            AABB segmentAABB = new AABB();
            {
                Vector2 t = p1 + maxFraction * (p2 - p1);
                segmentAABB.lowerBound = Vector2.Min(p1, t);
                segmentAABB.upperBound = Vector2.Max(p1, t);
            }

            int count = 0;

            stack[count++] = _root;

            while (count > 0)
            {
                int nodeId = stack[--count];
                if (nodeId == NullNode)
                {
                    continue;
                }

                DynamicTreeNode node = _nodes[nodeId];

                if (AABB.TestOverlap(ref node.aabb, ref segmentAABB) == false)
                {
                    continue;
                }

                // Separating axis for segment (Gino, p80).
                // |dot(v, p1 - c)| > dot(|v|, h)
                Vector2 c          = node.aabb.GetCenter();
                Vector2 h          = node.aabb.GetExtents();
                float   separation = Math.Abs(Vector2.Dot(v, p1 - c)) - Vector2.Dot(abs_v, h);
                if (separation > 0.0f)
                {
                    continue;
                }

                if (node.IsLeaf())
                {
                    RayCastInput subInput;
                    subInput.p1          = input.p1;
                    subInput.p2          = input.p2;
                    subInput.maxFraction = maxFraction;

                    RayCastOutput output;

                    callback(out output, ref subInput, node.userData);

                    if (output.hit)
                    {
                        // Early exit.
                        if (output.fraction == 0.0f)
                        {
                            return;
                        }

                        maxFraction = output.fraction;

                        // Update segment bounding box.
                        {
                            Vector2 t = p1 + maxFraction * (p2 - p1);
                            segmentAABB.lowerBound = Vector2.Min(p1, t);
                            segmentAABB.upperBound = Vector2.Max(p1, t);
                        }
                    }
                }
                else
                {
                    Debug.Assert(count + 1 < k_stackSize);
                    stack[count++] = node.child1;
                    stack[count++] = node.child2;
                }
            }
        }