Exemple #1
0
        public void TestContainedBy()
        {
            BoundingBox first = new BoundingBox(180, -180, 180, -180);
            BoundingBox second = new BoundingBox(1, -1, 1, -1);
            Assert.True(second.ContainedBy(first));

            second = new BoundingBox(1000, -1, 1, -1);
            Assert.False(second.ContainedBy(first));
        }
 /// <summary>
 /// Expands the BoundingBox to include the specified coordinates.
 /// </summary>
 /// <param name="bounds">A collection of coordinates to expand the box with.</param>
 /// <param name="box">The BoundingBox to expand.</param>
 internal static void ExpandBox(IBoundsInformation bounds, BoundingBox box)
 {
     if (bounds != null)
     {
         foreach (var coord in bounds.Coordinates)
         {
             box.Expand(coord.Latitude, coord.Longitude);
         }
     }
 }        
        /// <summary>
        /// Calculates the coordinates of the bounds of the <see cref="Feature"/>.
        /// </summary>
        /// <param name="feature">The feature instance.</param>
        /// <returns>
        /// A <c>BoundingBox</c> containing the coordinates of the bounds of the
        /// feature.
        /// </returns>
        /// <remarks>
        /// If the feature is a <see cref="Container"/> then the returned value
        /// will be the bounds of all the features within that container.
        /// </remarks>
        /// <exception cref="ArgumentNullException">feature is null.</exception>
        public static BoundingBox CalculateBounds(this Feature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException("feature");
            }

            BoundingBox box = new BoundingBox();
            ExpandBox(feature, box);

            return box.IsEmpty ? null : box;
        }
        /// <summary>
        /// Calculates the coordinates of the bounds of the <see cref="Geometry"/>.
        /// </summary>
        /// <param name="geometry">The geometry instance.</param>
        /// <returns>
        /// A <c>BoundingBox</c> containing the coordinates of the bounds of the
        /// geometry or null if the bounds could not be calculated.
        /// </returns>
        /// <exception cref="ArgumentNullException">geometry is null.</exception>
        public static BoundingBox CalculateBounds(this Geometry geometry)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry");
            }

            BoundingBox box = new BoundingBox();
            ExpandBox(geometry, box);

            return box.IsEmpty ? null : box;
        }
Exemple #5
0
        public void TestAlign()
        {
            BoundingBox box = new BoundingBox();
            box.North = 37.786807;  // Lincoln Park 3
            box.South = 37.781563;  // Lincoln Park 7
            box.East = -122.494135;  // Lincoln Park 18
            box.West = -122.504031;  // Lincoln Park 5

            BoundingBox qt = new BoundingBox(180, -180, 180, -180);
            box.Align(qt, 24);

            Assert.That(qt.North, Is.EqualTo(37.79296875).Within(0.000001));
            Assert.That(qt.South, Is.EqualTo(37.7490234375).Within(0.000001));
            Assert.That(qt.East, Is.EqualTo(-122.4755859375).Within(0.000001));
            Assert.That(qt.West, Is.EqualTo(-122.51953125).Within(0.000001));
        }
 /// <summary>
 /// Expands the BoundingBox to include the specified Geometry.
 /// </summary>
 /// <param name="geometry">The Geometry to expand the box to include.</param>
 /// <param name="box">The BoundingBox to expand.</param>
 internal static void ExpandBox(Geometry geometry, BoundingBox box)
 {
     MultipleGeometry multiple = geometry as MultipleGeometry;
     if (multiple != null)
     {
         foreach (var geo in multiple.Geometry)
         {
             ExpandBox(geo, box);
         }
     }
     else
     {
         // Try it as an IBoundsInformation, doesn't matter if the conversion fails
         ExpandBox(geometry as IBoundsInformation, box);
     }
 }
        private static LookAt ComputeLookAt(BoundingBox box)
        {
            Vector center = box.Center;
            double north = MathHelpers.Distance(box.Center, new Vector(box.North, center.Longitude));
            double west = MathHelpers.Distance(box.Center, new Vector(center.Latitude, box.West));

            // Calculate the corner and range
            double northWest = Math.Sqrt(Math.Pow(north, 2) + Math.Pow(west, 2));
            double range = northWest * Math.Tan(FieldOfView) * Margin;

            LookAt output = new LookAt();
            output.AltitudeMode = AltitudeMode.RelativeToGround;
            output.Latitude = center.Latitude;
            output.Longitude = center.Longitude;
            output.Range = Math.Max(range, MinimumRange); // Clamp value
            return output;
        }
Exemple #8
0
        public void BasicTest()
        {
            const double North = 45.45;
            const double South = -12.12;
            const double East = 123.123;
            const double West = -89.89;

            BoundingBox box = new BoundingBox();
            Assert.That(box.IsEmpty, Is.True);

            box.Expand(North, East);
            Assert.That(box.IsEmpty, Is.False);
            box.Expand(South, West);

            VerifyBounds(box, North, South, East, West);
            Assert.That(box.Center.Latitude, Is.EqualTo(16.665).Within(0.000001));
            Assert.That(box.Center.Longitude, Is.EqualTo(16.6165).Within(0.000001));
        }
        private static void RunTestCase(KmlFile file, string id, BoundingBox box)
        {
            Feature feature = file.FindObject(id) as Feature;
            Assert.That(feature, Is.Not.Null); // Verify the test data

            var bounds = feature.CalculateBounds();
            Assert.That(bounds, Is.Not.Null);
            Assert.That(bounds.East, Is.EqualTo(box.East));
            Assert.That(bounds.North, Is.EqualTo(box.North));
            Assert.That(bounds.South, Is.EqualTo(box.South));
            Assert.That(bounds.West, Is.EqualTo(box.West));
        }
        private static void ExpandBox(Feature feature, BoundingBox box)
        {
            Placemark placemark = feature as Placemark;
            if (placemark != null)
            {
                GeometryExtensions.ExpandBox(placemark.Geometry, box); // Can pass in nulls
                return;
            }

            Container container = feature as Container;
            if (container != null)
            {
                foreach (var f in container.Features)
                {
                    ExpandBox(f, box);
                }

                return;
            }

            // It's not a placemark or container, try it as a IBoundsInformation
            // and allow the conversion to fail
            GeometryExtensions.ExpandBox(feature as IBoundsInformation, box);
        }
        /// <summary>
        /// Expands the bounding box to conatin the specified bounds.
        /// </summary>
        /// <param name="box">The bounds to contain by this instance.</param>
        /// <exception cref="ArgumentNullException">box is null.</exception>
        public void Expand(BoundingBox box)
        {
            if (box == null)
            {
                throw new ArgumentNullException("box");
            }

            this.ExpandLatitude(box.North);
            this.ExpandLatitude(box.South);
            this.ExpandLongitude(box.East);
            this.ExpandLongitude(box.West);
        }
        /// <summary>
        /// Determines if this instance is inside the specified bounds.
        /// </summary>
        /// <param name="box">The BoundingBox to compare.</param>
        /// <returns>
        /// true if the specified value parameter contains this instance;
        /// otherwise, false.
        /// </returns>
        /// <exception cref="ArgumentNullException">box is null.</exception>
        public bool ContainedBy(BoundingBox box)
        {
            if (box == null)
            {
                throw new ArgumentNullException("box");
            }

            return (box.North >= this.North) &&
                   (box.South <= this.South) &&
                   (box.East >= this.East) &&
                   (box.West <= this.West);
        }
        /// <summary>
        /// Aligns this instance within the specified quadrant tree down to the
        /// maximum level specified.
        /// </summary>
        /// <param name="quadTree">The quadrant tree to align to.</param>
        /// <param name="maxDepth">The maximum depth to iterate for.</param>
        /// <exception cref="ArgumentNullException">quadTree is null.</exception>
        public void Align(BoundingBox quadTree, int maxDepth)
        {
            if (quadTree == null)
            {
                throw new ArgumentNullException("quadTree");
            }

            Vector center = quadTree.Center;
            if (this.ContainedBy(new BoundingBox(quadTree.North, center.Latitude, quadTree.East, center.Longitude)))
            {
                quadTree.South = center.Latitude;
                quadTree.West = center.Longitude;
            }
            else if (this.ContainedBy(new BoundingBox(quadTree.North, center.Latitude, center.Longitude, quadTree.West)))
            {
                quadTree.South = center.Latitude;
                quadTree.East = center.Longitude;
            }
            else if (this.ContainedBy(new BoundingBox(center.Latitude, quadTree.South, quadTree.East, center.Longitude)))
            {
                quadTree.North = center.Latitude;
                quadTree.West = center.Longitude;
            }
            else if (this.ContainedBy(new BoundingBox(center.Latitude, quadTree.South, center.Longitude, quadTree.West)))
            {
                quadTree.North = center.Latitude;
                quadTree.East = center.Longitude;
            }
            else
            {
                return;  // Target not contained by any child quadrant of quadTree.
            }

            // Fall through from above and recurse.
            if (maxDepth > 0)
            {
                this.Align(quadTree, maxDepth - 1);
            }
        }
Exemple #14
0
        public void TestContains()
        {
            double[][] points =
            {
                new double[] { 46.3941, 10.1168 },
                new double[] { 46.6356, 8.84678 },
                new double[] { 46.69, 8.95711 },
                new double[] { 46.158, 8.97531 },
                new double[] { 46.1719, 8.79744 },
                new double[] { 46.1217, 8.35152 },
                new double[] { 46.62, 8.5706 },
                new double[] { 46.7067, 8.953 },
                new double[] { 46.6087, 8.82036 },
                new double[] { 46.1546, 8.9633 },
                new double[] { 46.2368, 10.1363 },
                new double[] { 46.7079, 9.19907 },
                new double[] { 45.9296, 8.92094 },
                new double[] { 46.1738, 8.84359 },
                new double[] { 46.5616, 8.34504 },
                new double[] { 46.7389, 8.97314 },
                new double[] { 46.7493, 8.23686 },
                new double[] { 46.7233, 8.92272 },
                new double[] { 45.9528, 8.95471 }
            };

            BoundingBox box = new BoundingBox();

            foreach (var point in points)
            {
                box.Expand(point[0], point[1]);
            }

            VerifyBounds(box, 46.7493, 45.9296, 10.1363, 8.23686);
            Assert.That(box.Center.Latitude, Is.EqualTo(46.33945).Within(0.000001));
            Assert.That(box.Center.Longitude, Is.EqualTo(9.18658).Within(0.000001));

            foreach (var point in points)
            {
                Assert.True(box.Contains(point[0], point[1]));
                box.Expand(point[0], point[1]);
            }
        }
 public TestCase(string id, BoundingBox box)
 {
     this.Box = box;
     this.Id = id;
 }
Exemple #16
0
 private static void VerifyBounds(BoundingBox box, double north, double south, double east, double west)
 {
     Assert.That(box.East, Is.EqualTo(east));
     Assert.That(box.North, Is.EqualTo(north));
     Assert.That(box.South, Is.EqualTo(south));
     Assert.That(box.West, Is.EqualTo(west));
 }
Exemple #17
0
        public void TestExpand()
        {
            const double North = 89.123;
            const double South = -2.222;
            const double East = -88.888;
            const double West = -154.6789;

            BoundingBox box = new BoundingBox(North, South, East, West);
            BoundingBox other = new BoundingBox();

            other.Expand(box);
            VerifyBounds(box, North, South, East, West);
        }