Exemplo n.º 1
0
        bool ISpaceManager.Any <T>(SpacePoint <T> center, float within, IDimensionSelector dimSelector, Func <SpacePoint <T>, SpacePoint <T>, float, bool> where)
        {
            if (dimSelector == null)
            {
                dimSelector = DimensionSelector.Default;
            }

            var centerDimPoint = center.Dimensions[dimSelector.MainDimensionIndex];
            var centerLink     = centerDimPoint.HeadLink;

            if (centerDimPoint.NumberPoints > 1)
            {
                foreach (var sp in centerDimPoint.SpaPoints.Where(p => p.ID != center.ID))
                {
                    var distance = sp.DistanceTo(center, dimSelector);
                    if (distance <= within && where (sp, center, distance))
                    {
                        return(true);
                    }
                }
            }
            var centerPos = centerDimPoint.Position;
            var next      = centerLink.Next;

            while (next != null && (next.Position - centerPos).Abs() <= within)
            {
                foreach (var nextSpaPoint in next.DimPoint.SpaPoints)
                {
                    var distance = nextSpaPoint.DistanceTo(center, dimSelector);
                    if (distance <= within && where (nextSpaPoint, center, distance))
                    {
                        return(true);
                    }
                }
                next = next.Next;
            }
            var prev = centerLink.Prev;

            while (prev != null && (prev.Position - centerPos).Abs() <= within)
            {
                foreach (var prevSpaPoint in prev.DimPoint.SpaPoints)
                {
                    var distance = prevSpaPoint.DistanceTo(center, dimSelector);
                    if (distance <= within && where (prevSpaPoint, center, distance))
                    {
                        return(true);
                    }
                }
                prev = prev.Prev;
            }
            return(false);
        }
Exemplo n.º 2
0
        public float DistanceTo(float[] otherPoint, IDimensionSelector dimSelector)
        {
            var hasSelector = dimSelector != null;

            var sum = 0f;

            for (var d = 0; d < Dimensions.Length; ++d)
            {
                if (hasSelector && !dimSelector.IncludesDimension(d))
                {
                    continue;
                }

                var valOther = otherPoint[d];
                var valCurrD = Dimensions[d].Position;

                var n = valCurrD - valOther;
                n    = n * n;
                sum += n;
            }
            return((float)Math.Sqrt(sum));
        }
Exemplo n.º 3
0
        private static IEnumerable <SpacePoint <T> > EnumerateSpacePoints <T>(
            DimensionPoint <T> curr, HashSet <SpacePoint <T> > spacePoints,
            HashSet <DimensionPoint <T> > tempDimPoints, float within,
            IDimensionSelector dimSelector, params float[] dimensionPositions)
        {
            if (curr == null)
            {
                yield break;
            }
            tempDimPoints.Add(curr);

            foreach (var sp in curr.SpaPoints)
            {
                if (!spacePoints.Contains(sp))
                {
                    var dist = sp.DistanceTo(dimensionPositions, dimSelector);
                    if (dist <= within)
                    {
                        spacePoints.Add(sp);
                        yield return(sp);
                    }
                }
            }
        }
Exemplo n.º 4
0
        IEnumerable <SpacePoint <T> > ISpaceManager.FindSpacePointsWithin <T>(Space <T> space, float within, IDimensionSelector dimSelector, params float[] dimensionPositions)
        {
            MustBe.Equal(space.Dimensions.Length, dimensionPositions.Length, () => "space.Dimensions.Length AND dimensionPositions.Length");

            var spacePoints   = new HashSet <SpacePoint <T> >();
            var tempDimPoints = new HashSet <DimensionPoint <T> >();

            for (var d = 0; d < space.Dimensions.Length; ++d)
            {
                if (dimSelector != null && !dimSelector.IncludesDimension(d))
                {
                    continue;
                }

                DimensionPoint <T> left;
                DimensionPoint <T> right;
                var dCenter = dimensionPositions[d];
                _spaceManager.TryFindDimensionPoint(space.Dimensions[d], dCenter, out left, out right);
                var curr = left;
                if (curr != null && !tempDimPoints.Contains(curr))
                {
                    foreach (var sp in EnumerateSpacePoints(curr, spacePoints, tempDimPoints, within, dimSelector, dimensionPositions))
                    {
                        yield return(sp);
                    }
                    var prev = curr.HeadLink.Prev;
                    while (prev != null && !tempDimPoints.Contains(prev.DimPoint) && (prev.Position - dCenter).Abs() <= within)
                    {
                        foreach (var sp in EnumerateSpacePoints(prev.DimPoint, spacePoints, tempDimPoints, within, dimSelector, dimensionPositions))
                        {
                            yield return(sp);
                        }
                    }
                }
                curr = right;
                if (curr != null && !tempDimPoints.Contains(curr))
                {
                    foreach (var sp in EnumerateSpacePoints(curr, spacePoints, tempDimPoints, within, dimSelector, dimensionPositions))
                    {
                        yield return(sp);
                    }
                    var next = curr.HeadLink.Next;
                    while (next != null && !tempDimPoints.Contains(next.DimPoint) && (next.Position - dCenter).Abs() <= within)
                    {
                        foreach (var sp in EnumerateSpacePoints(next.DimPoint, spacePoints, tempDimPoints, within, dimSelector, dimensionPositions))
                        {
                            yield return(sp);
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        TAccumulate ISpaceManager.AggregateWithin <TSource, TAccumulate>(SpacePoint <TSource> center, TAccumulate seed, float within, IDimensionSelector dimSelector, Func <TAccumulate, SpacePoint <TSource>, SpacePoint <TSource>, float, TAccumulate> func)
        {
            if (dimSelector == null)
            {
                dimSelector = DimensionSelector.Default;
            }

            var centerDimPoint = center.Dimensions[dimSelector.MainDimensionIndex];
            var centerLink     = centerDimPoint.HeadLink;

            if (centerDimPoint.NumberPoints > 1)
            {
                foreach (var sp in centerDimPoint.SpaPoints.Where(p => p.ID != center.ID))
                {
                    var dictance = sp.DistanceTo(center, dimSelector);
                    if (dictance <= within)
                    {
                        seed = func(seed, sp, center, dictance);
                    }
                }
            }
            var centerPos = centerDimPoint.Position;
            var next      = centerLink.Next;

            while (next != null && (next.Position - centerPos).Abs() <= within)
            {
                foreach (var nextSpaPoint in next.DimPoint.SpaPoints)
                {
                    var dictance = nextSpaPoint.DistanceTo(center, dimSelector);
                    if (dictance <= within)
                    {
                        seed = func(seed, nextSpaPoint, center, dictance);
                    }
                }
                next = next.Next;
            }
            var prev = centerLink.Prev;

            while (prev != null && (prev.Position - centerPos).Abs() <= within)
            {
                foreach (var prevSpaPoint in prev.DimPoint.SpaPoints)
                {
                    var dictance = prevSpaPoint.DistanceTo(center, dimSelector);
                    if (dictance <= within)
                    {
                        seed = func(seed, prevSpaPoint, center, dictance);
                    }
                }
                prev = prev.Prev;
            }
            return(seed);
        }