Ejemplo n.º 1
0
 public QuadTreeNode(float2 origin, float halfSize, Vertex **data, QuadTreeNode *parent) : this()
 {
     Origin   = origin;
     HalfSize = halfSize;
     Data     = data;
     Parent   = parent;
 }
Ejemplo n.º 2
0
        void Dispose(QuadTreeNode *n)
        {
            if (n->IsLeaf)
            {
                UnsafeUtility.Free(n->Data, _allocator);
                return;
            }

            Dispose(n->BL);
            Dispose(n->TL);
            Dispose(n->BR);
            Dispose(n->TR);
        }
Ejemplo n.º 3
0
        public QuadTree(float size, int initialCapacity, int bucketSize, Allocator allocator) : this()
        {
            _bucketSize = bucketSize;
            _allocator  = allocator;
            var buckets = (int)math.ceil((float)initialCapacity / bucketSize);

            _chunks = new Stack <IntPtr>(buckets, allocator);
            for (int i = 0; i < buckets; i++)
            {
                _chunks.Push((IntPtr)Malloc());
            }
            _nodes = new PersistentStore <QuadTreeNode>((int)(1.3334f * buckets), allocator);
            _root  = _nodes.Set(new QuadTreeNode(0, size / 2, GetChunk(), null));
        }
Ejemplo n.º 4
0
        void Clear(QuadTreeNode *n)
        {
            if (n->IsLeaf)
            {
                _chunks.Push((IntPtr)n->Data);
                return;
            }

            Clear(n->BL);
            Clear(n->TL);
            Clear(n->BR);
            Clear(n->TR);
            _nodes.Recycle(n->BL);
            _nodes.Recycle(n->TL);
            _nodes.Recycle(n->BR);
            _nodes.Recycle(n->TR);
        }
Ejemplo n.º 5
0
        public QuadTree(float size, int initialCapacity, int bucketSize, Allocator allocator) : this()
        {
            _bucketSize = bucketSize;
            _allocator  = allocator;
            var buckets = (int)math.ceil((float)initialCapacity / bucketSize);

            _chunks = new Stack <IntPtr>(buckets, allocator);
            for (int i = 0; i < buckets; i++)
            {
                _chunks.Push((IntPtr)Malloc());
            }

            const int blockSize     = 128;
            var       capacity      = 1.3334f * buckets;
            var       initialBlocks = (int)math.ceil(capacity / blockSize);

            _nodes = new BlockPool <QuadTreeNode>(blockSize, initialBlocks, allocator);
            _root  = _nodes.GetElementPointer(new QuadTreeNode(0, size / 2, GetChunk(), null));
        }
Ejemplo n.º 6
0
        static void FindClosest(QuadTreeNode *n, float2 p, float2 min, float2 max, ref float dist, ref Vertex *closest)
        {
            if (n->IsLeaf)
            {
                for (int i = 0; i < n->Count; i++)
                {
                    var v = n->Data[i];
                    var d = math.lengthsq(v->Point - p);
                    if (d < dist)
                    {
                        closest = v;
                        dist    = d;
                    }
                }

                return;
            }

            if (Math.RectsOverlap(min, max, n->Origin - n->HalfSize, n->Origin))
            {
                FindClosest(n->BL, p, min, max, ref dist, ref closest);
            }

            if (Math.RectsOverlap(min, max, n->Origin - new float2(n->HalfSize, 0), n->Origin + new float2(0, n->HalfSize)))
            {
                FindClosest(n->TL, p, min, max, ref dist, ref closest);
            }

            if (Math.RectsOverlap(min, max, n->Origin - new float2(0, n->HalfSize), n->Origin + new float2(n->HalfSize, 0)))
            {
                FindClosest(n->BR, p, min, max, ref dist, ref closest);
            }

            if (Math.RectsOverlap(min, max, n->Origin, n->Origin + n->HalfSize))
            {
                FindClosest(n->TR, p, min, max, ref dist, ref closest);
            }
        }