public void Test_S2LatLngRect_GetDirectHausdorffDistancePointToRect() { // The Hausdorff distance from a point to a rect should be the same as its // distance to the rect. S2LatLngRect a1 = PointRectFromDegrees(5, 8); S2LatLngRect a2 = PointRectFromDegrees(90, 10); // north pole S2LatLngRect b = RectFromDegrees(-85, -50, -80, 10); Assert2.DoubleEqual(a1.GetDirectedHausdorffDistance(b).Radians, a1.GetDistance(b).Radians); Assert2.DoubleEqual(a2.GetDirectedHausdorffDistance(b).Radians, a2.GetDistance(b).Radians); b = RectFromDegrees(4, -10, 80, 10); Assert2.DoubleEqual(a1.GetDirectedHausdorffDistance(b).Radians, a1.GetDistance(b).Radians); Assert2.DoubleEqual(a2.GetDirectedHausdorffDistance(b).Radians, a2.GetDistance(b).Radians); b = RectFromDegrees(70, 170, 80, -170); Assert2.DoubleEqual(a1.GetDirectedHausdorffDistance(b).Radians, a1.GetDistance(b).Radians); Assert2.DoubleEqual(a2.GetDirectedHausdorffDistance(b).Radians, a2.GetDistance(b).Radians); }
// This function assumes that GetDirectedHausdorffDistance() always returns // a distance from some point in a to b. So the function mainly tests whether // the returned distance is large enough, and only does a weak test on whether // it is small enough. private static void VerifyGetDirectedHausdorffDistance(S2LatLngRect a, S2LatLngRect b) { S1Angle hausdorff_distance = a.GetDirectedHausdorffDistance(b); const double kResolution = 0.1; S1Angle max_distance = S1Angle.Zero; int sample_size_on_lat = (int)(a.Lat.GetLength() / kResolution) + 1; int sample_size_on_lng = (int)(a.Lng.GetLength() / kResolution) + 1; double delta_on_lat = a.Lat.GetLength() / sample_size_on_lat; double delta_on_lng = a.Lng.GetLength() / sample_size_on_lng; double lng = a.Lng.Lo; for (int i = 0; i <= sample_size_on_lng; ++i, lng += delta_on_lng) { double lat = a.Lat.Lo; for (int j = 0; j <= sample_size_on_lat; ++j, lat += delta_on_lat) { S2LatLng latlng = S2LatLng.FromRadians(lat, lng).Normalized(); S1Angle distance_to_b = b.GetDistance(latlng); if (distance_to_b >= max_distance) { max_distance = distance_to_b; } } } Assert.True(max_distance.Radians <= hausdorff_distance.Radians + 1e-10); Assert.True(max_distance.Radians >= hausdorff_distance.Radians - kResolution); }
// This method verifies a.GetDistance(b), where b is a S2LatLng, by comparing // its result against a.GetDistance(c), c being the point rectangle created // from b. private static void VerifyGetRectPointDistance(S2LatLngRect a, S2LatLng p) { S1Angle distance1 = BruteForceRectPointDistance(a, p.Normalized()); S1Angle distance2 = a.GetDistance(p.Normalized()); Assert2.Near(Math.Abs(distance1.Radians - distance2.Radians), 0, 1e-10); }
// This method verifies a.GetDistance(b) by comparing its result against a // brute-force implementation. The correctness of the brute-force version is // much easier to verify by inspection. private static void VerifyGetDistance(S2LatLngRect a, S2LatLngRect b) { S1Angle distance1 = BruteForceDistance(a, b); S1Angle distance2 = a.GetDistance(b); Assert2.Near(distance1.Radians - distance2.Radians, 0, 1e-10); }
/** * This method verifies a.getDistance(b) by comparing its result against a * brute-force implementation. The correctness of the brute-force version is * much easier to verify by inspection. */ private static void verifyGetDistance(S2LatLngRect a, S2LatLngRect b) { var distance1 = bruteForceDistance(a, b); var distance2 = a.GetDistance(b); assertEquals(distance1.Radians, distance2.Radians, 1e-10); }
/** * This method verifies a.getDistance(b), where b is a S2LatLng, by comparing * its result against a.getDistance(c), c being the point rectangle created * from b. */ private static void verifyGetRectPointDistance(S2LatLngRect a, S2LatLng p) { var distance1 = bruteForceRectPointDistance(a, p.Normalized); var distance2 = a.GetDistance(p.Normalized); assertEquals(distance1.Radians, distance2.Radians, 1e-10); }
public void Test_S2LatLngRect_GetDistanceOverlapping() { // Check pairs of rectangles that overlap: (should all return 0): S2LatLngRect a = RectFromDegrees(0, 0, 2, 2); S2LatLngRect b = PointRectFromDegrees(0, 0); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(a)); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(b)); Assert.Equal(S1Angle.FromRadians(0), b.GetDistance(b)); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(S2LatLng.FromDegrees(0, 0))); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(RectFromDegrees(0, 1, 2, 3))); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(RectFromDegrees(0, 2, 2, 4))); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(RectFromDegrees(1, 0, 3, 2))); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(RectFromDegrees(2, 0, 4, 2))); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(RectFromDegrees(1, 1, 3, 3))); Assert.Equal(S1Angle.FromRadians(0), a.GetDistance(RectFromDegrees(2, 2, 4, 4))); }