Exemplo n.º 1
0
        /// <summary>
        /// Find all items within the given distance from the given point
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="distanceSquared"></param>
        /// <param name="output"></param>
        public void CloseTo(Vector pt, double distanceSquared, ref IList <T> output)
        {
            if (_SplitDimension != CoordinateAxis.Undefined)
            {
                double value = _Tree.PositionInDimension(_SplitDimension, pt);
                //Check starting cell:
                double t          = (value - _Origin) / _CellSize;
                int    startIndex = (int)Math.Floor(t);
                if (startIndex < 0)
                {
                    startIndex = 0;
                }
                else if (startIndex >= _Branches.Length)
                {
                    startIndex = _Branches.Length - 1;
                }
                DDTreeNode <T> node = _Branches[startIndex];
                if (node != null)
                {
                    node.CloseTo(pt, distanceSquared, ref output);
                }
                //Check surrounding cells within max distance:
                int    offsetIndex     = 1;
                double cellSizeSquared = _CellSize * _CellSize;
                double t0 = t - startIndex;
                double t1 = -t0;
                t0 -= 1;
                bool increase = true;
                bool decrease = true;
                while (increase || decrease)
                {
                    if (increase)
                    {
                        double pInd = offsetIndex + t1;
                        int    i    = startIndex + offsetIndex;
                        if (pInd * pInd * cellSizeSquared >= distanceSquared || i >= _Branches.Length)
                        {
                            increase = false;
                        }
                        else
                        {
                            node = _Branches[i];
                            if (node != null)
                            {
                                node.CloseTo(pt, distanceSquared, ref output);
                            }
                        }
                    }

                    if (decrease)
                    {
                        double pInd = offsetIndex + t0;
                        int    i    = startIndex - offsetIndex;
                        if (pInd * pInd * cellSizeSquared >= distanceSquared || i < 0)
                        {
                            decrease = false;
                        }
                        else
                        {
                            node = _Branches[i];
                            if (node != null)
                            {
                                node.CloseTo(pt, distanceSquared, ref output);
                            }
                        }
                    }
                    offsetIndex++;
                }
            }
            else
            {
                foreach (T item in _Children)
                {
                    double distSquaredTo = _Tree.DistanceSquaredBetween(pt, item);
                    if (distSquaredTo < distanceSquared && !output.Contains(item))
                    {
                        output.Add(item);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Find all items within the specified distance from the given point
        /// </summary>
        /// <param name="pt">The point to check distance to</param>
        /// <param name="maxDistance">The maximum distance within which items will be included</param>
        /// <param name="output">A collection to be populated with all the items close to the specified point</param>
        public void CloseTo(Vector pt, double maxDistance, ref IList <T> output)
        {
            double distanceSquared = maxDistance * maxDistance;

            _RootNode.CloseTo(pt, distanceSquared, ref output);
        }