예제 #1
0
        public void InfiniteShouldReturnTheSameResultInBothVerison()
        {
            var minPoint = new Coordinate {
                Latitude = 6544, Longitude = 5577
            };
            var maxPoint = new Coordinate {
                Latitude = 9687, Longitude = 1254
            };

            var minPointA = new[] { minPoint.Latitude, minPoint.Longitude };
            var maxPointA = new[] { maxPoint.Latitude, maxPoint.Longitude };

            var hyperRect = new HyperRect <double>();

            hyperRect.MinPoint = minPointA;
            hyperRect.MaxPoint = maxPointA;

            var hyperRectCoordinate = new HyperRectCoordinate <Coordinate>();

            hyperRectCoordinate.MinPoint = minPoint;
            hyperRectCoordinate.MaxPoint = maxPoint;

            var rect           = hyperRect.Clone();
            var rectCoordinate = hyperRectCoordinate.Clone();

            var res1 = HyperRect <double> .Infinite(2, double.MaxValue, double.MinValue);

            var res2 = HyperRectCoordinate <Coordinate> .Infinite(2, double.MaxValue, double.MinValue);

            res1.MaxPoint[0].Should().Be(res2.MaxPoint.Latitude);
            res1.MaxPoint[1].Should().Be(res2.MaxPoint.Longitude);
            res1.MinPoint[0].Should().Be(res2.MinPoint.Latitude);
            res1.MinPoint[1].Should().Be(res2.MinPoint.Longitude);
        }
예제 #2
0
        public void CloneGenerateTheSameResultInBothVersion()
        {
            var minPoint = new Coordinate {
                Latitude = 6544, Longitude = 5577
            };
            var maxPoint = new Coordinate {
                Latitude = 9687, Longitude = 1254
            };

            var minPointA = new[] { minPoint.Latitude, minPoint.Longitude };
            var maxPointA = new[] { maxPoint.Latitude, maxPoint.Longitude };

            var hyperRect = new HyperRect <double>();

            hyperRect.MinPoint = minPointA;
            hyperRect.MaxPoint = maxPointA;

            var hyperRectCoordinate = new HyperRectCoordinate <Coordinate>();

            hyperRectCoordinate.MinPoint = minPoint;
            hyperRectCoordinate.MaxPoint = maxPoint;

            var rect           = hyperRect.Clone();
            var rectCoordinate = hyperRectCoordinate.Clone();

            rect.MinPoint[0].Should().Be(rectCoordinate.MinPoint.Latitude);
            rect.MinPoint[1].Should().Be(rectCoordinate.MinPoint.Longitude);

            rect.MaxPoint[0].Should().Be(rectCoordinate.MaxPoint.Latitude);
            rect.MaxPoint[1].Should().Be(rectCoordinate.MaxPoint.Longitude);
        }
예제 #3
0
        /// <summary>
        /// A top-down recursive method to find the nearest neighbors of a given point.
        /// </summary>
        /// <param name="node">The index of the node for the current recursion branch.</param>
        /// <param name="target">The point whose neighbors we are trying to find.</param>
        /// <param name="rect">The <see cref="HyperRect{T}"/> containing the possible nearest neighbors.</param>
        /// <param name="dimension">The current splitting dimension for this recursion branch.</param>
        /// <param name="nearestNeighbors">The <see cref="BoundedPriorityList{TElement,TPriority}"/> containing the nearest neighbors already discovered.</param>
        /// <param name="maxSearchRadius">The radius of the current largest distance to search from the <paramref name="target"/></param>
        private void SearchForNearestNeighbors(
            KDNode node,
            double[] target,
            HyperRect <double> rect,
            int dimension,
            BoundablePriorityList <int, double> nearestNeighbors,
            double maxSearchRadius)
        {
            if (node == null)
            {
                return;
            }

            // Work out the current dimension
            var dim = dimension % this.Dimensions;

            // Get the coordinate of the current node.
            var coordinate = this.PointSelector(this.InternalList[node.ElementIndex]);

            // Split our hyper-rectangle into 2 sub rectangles along the current
            // node's point on the current dimension
            var leftRect = rect.Clone();

            leftRect.MaxPoint[dim] = coordinate[dim];

            var rightRect = rect.Clone();

            rightRect.MinPoint[dim] = coordinate[dim];

            // Determine which side the target resides in
            var comparison = target[dim] <= coordinate[dim];

            var nearerRect  = comparison ? leftRect : rightRect;
            var furtherRect = comparison ? rightRect : leftRect;

            var nearerNode  = comparison ? node.Left : node.Right;
            var furtherNode = comparison ? node.Right : node.Left;

            // Move down into the nearer branch
            this.SearchForNearestNeighbors(
                nearerNode,
                target,
                nearerRect,
                dimension + 1,
                nearestNeighbors,
                maxSearchRadius);

            // Walk down into the further branch but only if our capacity hasn't been reached
            // OR if there's a region in the further rectangle that's closer to the target than our
            // current furtherest nearest neighbor
            var closestPointInFurtherRect = furtherRect.GetClosestPoint(target);
            var distanceToTarget          = this.Metric(closestPointInFurtherRect, target);

            if (distanceToTarget.CompareTo(maxSearchRadius) <= 0)
            {
                if (nearestNeighbors.IsFull)
                {
                    if (distanceToTarget.CompareTo(nearestNeighbors.MaxPriority) < 0)
                    {
                        this.SearchForNearestNeighbors(
                            furtherNode,
                            target,
                            furtherRect,
                            dimension + 1,
                            nearestNeighbors,
                            maxSearchRadius);
                    }
                }
                else
                {
                    this.SearchForNearestNeighbors(
                        furtherNode,
                        target,
                        furtherRect,
                        dimension + 1,
                        nearestNeighbors,
                        maxSearchRadius);
                }
            }

            // Try to add the current node to our nearest neighbors list
            distanceToTarget = this.Metric(coordinate, target);
            if (distanceToTarget.CompareTo(maxSearchRadius) <= 0)
            {
                nearestNeighbors.Add(node.ElementIndex, distanceToTarget);
            }
        }