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); }
public void Test_S2LatLngRect_GetDirectedHausdorffDistanceContained() { // Caller rect is contained in callee rect. Should return 0. S2LatLngRect a = RectFromDegrees(-10, 20, -5, 90); Assert.Equal(S1Angle.FromRadians(0), a.GetDirectedHausdorffDistance(RectFromDegrees(-10, 20, -5, 90))); Assert.Equal(S1Angle.FromRadians(0), a.GetDirectedHausdorffDistance(RectFromDegrees(-10, 19, -5, 91))); Assert.Equal(S1Angle.FromRadians(0), a.GetDirectedHausdorffDistance(RectFromDegrees(-11, 20, -4, 90))); Assert.Equal(S1Angle.FromRadians(0), a.GetDirectedHausdorffDistance(RectFromDegrees(-11, 19, -4, 91))); }
// 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); }