Example #1
0
 public static void Free <T>(IList <T> data, IMemoryPool <T> pool)
 {
     foreach (var d in data)
     {
         pool.Free(d);
     }
     data.Clear();
 }
Example #2
0
 public static void Free <T>(IList <T> data, IMemoryPool <T> pool)
 {
     foreach (var d in data)
     {
         pool.Free(d);
     }
     if (!data.IsReadOnly)
     {
         data.Clear();
     }
 }
Example #3
0
        public static IMemoryPool <BVH <Value> > Clear(BVH <Value> t, IMemoryPool <BVH <Value> > alloc)
        {
            if (t == null)
            {
                return(alloc);
            }

            for (var i = 0; i < 2; i++)
            {
                Clear(t.ch [i], alloc);
            }
            return(alloc.Free(t.Clear()));
        }
Example #4
0
        public bool Build(IList <AABB3> objectBounds, IList <int> indices, int offset, int length, out int countFromLeft)
        {
            Clear();
            cleared = false;

            var world = aabbPool.New();

            foreach (var j in Enumerable.Range(offset, length).Select(i => indices[i]))
            {
                var ob = objectBounds[j];
                world.Encapsulate(ob.Center);
            }

            var size        = world.Size;
            var longestAxis = (size.x > size.z ? (size.x > size.y ? 0 : 1) : (size.y > size.z ? 1 : 2));
            var longest     = size[longestAxis];
            var worldMin    = world.Min[longestAxis];

            if (longest < 1e-4f)
            {
                countFromLeft = -1;
                return(false);
            }

            var invLongest = 1f / longest;

            foreach (var j in Enumerable.Range(offset, length).Select(i => indices[i]))
            {
                var ob = objectBounds[j];
                var c  = ob.Center;
                var k  = (int)(binCount * (c[longestAxis] - worldMin) * invLongest * SMALLER_THAN_ONE);
                bins[k].Add(j);
                bbs[k].Encapsulate(ob);
            }

            var leftCount  = 0;
            var leftBounds = aabbPool.New();

            for (var i = 0; i < lefts.Length; i++)
            {
                leftCount += bins[i].Count;
                leftBounds.Encapsulate(bbs[i]);
                lefts[i] = leftCount * leftBounds.SurfaceArea;
            }

            var rightCount  = 0;
            var rightBounds = aabbPool.New();

            for (var i = 0; i < rights.Length; i++)
            {
                var j = binCount - i - 1;
                rightCount += bins[j].Count;
                rightBounds.Encapsulate(bbs[j]);
                rights[j] = rightCount * rightBounds.SurfaceArea;
            }

            var bestIndex = -1;
            var bestCost  = float.MaxValue;

            for (var i = 1; i < binCount; i++)
            {
                var cost = lefts[i - 1] + rights[i];
                if (cost < bestCost)
                {
                    bestCost  = cost;
                    bestIndex = i;
                }
            }

            countFromLeft = 0;
            for (var i = 0; i < bestIndex; i++)
            {
                countFromLeft += bins[i].Count;
            }

            var offsetOfIndices = offset;

            foreach (var bin in bins)
            {
                foreach (var i in bin)
                {
                    indices[offsetOfIndices++] = i;
                }
            }

            aabbPool.Free(world);
            aabbPool.Free(leftBounds);
            aabbPool.Free(rightBounds);

            return(true);
        }