Ejemplo n.º 1
0
        /// <summary>
        /// Find the closest object to the specified point within the specified distance
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="distanceSquared"></param>
        /// <returns></returns>
        public T NearestTo(Vector pt, ref double distanceSquared, T ignore)
        {
            T result = default(T);

            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)
                {
                    result = node.NearestTo(pt, ref distanceSquared, ignore);
                }
                //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)
                            {
                                T result2 = default(T);
                                result2 = node.NearestTo(pt, ref distanceSquared, ignore);
                                if (result2 != null)
                                {
                                    result = result2;
                                }
                            }
                        }
                    }

                    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)
                            {
                                T result2 = default(T);
                                result2 = node.NearestTo(pt, ref distanceSquared, ignore);
                                if (result2 != null)
                                {
                                    result = result2;
                                }
                            }
                        }
                    }
                    offsetIndex++;
                }
            }
            else
            {
                foreach (T item in _Children)
                {
                    if (_Tree.CanReturn(item) && !object.ReferenceEquals(item, ignore))
                    {
                        double distSquaredTo = _Tree.DistanceSquaredBetween(pt, item);
                        if (distSquaredTo < distanceSquared)
                        {
                            distanceSquared = distSquaredTo;
                            result          = item;
                        }
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Find the closest item in the tree to the specified point within the specified maximum distance
        /// </summary>
        /// <param name="pt">The point to search from</param>
        /// <param name="maxDistance">The maximum distance</param>
        /// <param name="ignore">Optional.  A value in the tree which is to be ignored.</param>
        /// <returns></returns>
        public T NearestTo(Vector pt, double maxDistance, T ignore = default(T))
        {
            double distanceSquared = maxDistance * maxDistance;

            return(_RootNode.NearestTo(pt, ref distanceSquared, ignore));
        }