Ticket #21172: 21172.2.patch

File 21172.2.patch, 9.6 KB (added by taylor.smock, 3 years ago)

Add tests, correct math

  • src/org/openstreetmap/josm/tools/Geometry.java

    diff --git a/src/org/openstreetmap/josm/tools/Geometry.java b/src/org/openstreetmap/josm/tools/Geometry.java
    index b10ce47a16..c5d9241d85 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.tools;
    33
     4import static org.openstreetmap.josm.data.projection.Ellipsoid.WGS84;
     5
    46import java.awt.geom.Area;
    57import java.awt.geom.Line2D;
    68import java.awt.geom.Path2D;
    import org.openstreetmap.josm.command.ChangeNodesCommand;  
    2527import org.openstreetmap.josm.command.Command;
    2628import org.openstreetmap.josm.data.coor.EastNorth;
    2729import org.openstreetmap.josm.data.coor.ILatLon;
     30import org.openstreetmap.josm.data.coor.LatLon;
    2831import org.openstreetmap.josm.data.osm.BBox;
    2932import org.openstreetmap.josm.data.osm.DataSet;
    3033import org.openstreetmap.josm.data.osm.INode;
    public final class Geometry {  
    15551558        return smallest != Double.MAX_VALUE ? Math.sqrt(smallest) : Double.NaN;
    15561559    }
    15571560
     1561    /**
     1562     * Create a new LatLon at a specified distance. Currently uses WGS84, but may change randomly in the future.
     1563     * This does not currently attempt to be hugely accurate. The actual location may be off
     1564     * depending upon the distance and the elevation, but should be within 0.0002 meters.
     1565     *
     1566     * @param original The originating point
     1567     * @param angle The angle (from true north) in radians
     1568     * @param offset The distance to the new point in the current projection's units
     1569     * @return The location at the specified angle and distance from the originating point
     1570     * @since xxx
     1571     */
     1572    public static ILatLon getLatLonFrom(final ILatLon original, final double angle, final double offset) {
     1573        final double meterOffset = ProjectionRegistry.getProjection().getMetersPerUnit() * offset;
     1574        final double radianLat = Math.toRadians(original.lat());
     1575        final double radianLon = Math.toRadians(original.lon());
     1576        final double angularDistance = meterOffset / WGS84.a;
     1577        final double lat = Math.asin(Math.sin(radianLat) * Math.cos(angularDistance)
     1578                + Math.cos(radianLat) * Math.sin(angularDistance) * Math.cos(angle));
     1579        final double lon = radianLon + Math.atan2(Math.sin(angle) * Math.sin(angularDistance) * Math.cos(radianLat),
     1580                Math.cos(angularDistance) - Math.sin(radianLat) * Math.sin(lat));
     1581        return new LatLon(Math.toDegrees(lat), Math.toDegrees(lon));
     1582    }
     1583
    15581584    /**
    15591585     * Calculate closest distance between a line segment s1-s2 and a point p
    15601586     * @param s1 start of segment
  • test/unit/org/openstreetmap/josm/TestUtils.java

    diff --git a/test/unit/org/openstreetmap/josm/TestUtils.java b/test/unit/org/openstreetmap/josm/TestUtils.java
    index c50b956165..c30e05388d 100644
    a b import java.time.Instant;  
    2222import java.time.ZoneOffset;
    2323import java.time.format.DateTimeFormatter;
    2424import java.time.temporal.Temporal;
     25import java.util.ArrayList;
    2526import java.util.Arrays;
    2627import java.util.Collection;
    2728import java.util.Comparator;
    import java.util.Objects;  
    3031import java.util.Set;
    3132import java.util.concurrent.ExecutionException;
    3233import java.util.concurrent.ThreadPoolExecutor;
     34import java.util.concurrent.atomic.AtomicInteger;
    3335import java.util.function.Function;
    3436import java.util.stream.Collectors;
     37import java.util.stream.IntStream;
    3538import java.util.stream.Stream;
    3639
    3740import org.junit.jupiter.api.Assertions;
    public final class TestUtils {  
    168171        Arrays.sort(array, comparator);
    169172    }
    170173
     174    /**
     175     * Create a test matrix for parameterized tests.
     176     * <br />
     177     * <b>WARNING:</b> This can quickly become <i>very</i> large (this is combinatorial,
     178     * so the returned {@link Stream} length is the size of the object collections multiplied by each other.
     179     * So if you have three lists of size 3, 4, and 5, the stream size would be {@code 3 * 4 * 5} or 60 elements.
     180     * <br />
     181     * Generally speaking, you should avoid putting expected values into the test matrix.
     182     *
     183     * @param objectCollections The collections of objects. May include/provide {@code null}.
     184     * @return The object arrays to be used as arguments. Note: The returned stream might not be thread-safe.
     185     */
     186    public static Stream<Object[]> createTestMatrix(List<?>... objectCollections) {
     187        // Create the original object arrays
     188        final AtomicInteger size = new AtomicInteger(1);
     189        Stream.of(objectCollections).mapToInt(Collection::size).forEach(i -> size.set(size.get() * i));
     190        final List<Object[]> testMatrix = new ArrayList<>(size.get());
     191        final int[] indexes = IntStream.range(0, objectCollections.length).map(i -> 0).toArray();
     192
     193        // It is important to make a new object array each time (we modify them)
     194        return IntStream.range(0, size.get()).mapToObj(index -> new Object[objectCollections.length]).peek(args -> {
     195            // Just in case someone tries to make this parallel, synchronize on indexes to avoid most issues.
     196            synchronized (indexes) {
     197                // Set the args
     198                for (int listIndex = 0; listIndex < objectCollections.length; listIndex++) {
     199                    args[listIndex] = objectCollections[listIndex].get(indexes[listIndex]);
     200                }
     201                // Increment indexes
     202                for (int listIndex = 0; listIndex < objectCollections.length; listIndex++) {
     203                    indexes[listIndex] = indexes[listIndex] + 1;
     204                    if (indexes[listIndex] >= objectCollections[listIndex].size()) {
     205                        indexes[listIndex] = 0;
     206                    } else {
     207                        break;
     208                    }
     209                }
     210            }
     211        });
     212    }
     213
    171214    private static <T> String getFailMessage(T o1, T o2, int a, int b) {
    172215        return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
    173216        .append(o2).append("\ngave: ").append(a).append("/").append(b)
  • test/unit/org/openstreetmap/josm/tools/GeometryTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/GeometryTest.java b/test/unit/org/openstreetmap/josm/tools/GeometryTest.java
    index 8a74bd0204..4d73b94d62 100644
    a b import java.nio.file.Files;  
    1111import java.nio.file.Paths;
    1212import java.util.ArrayList;
    1313import java.util.Arrays;
     14import java.util.Collections;
    1415import java.util.List;
     16import java.util.stream.Stream;
    1517
    1618import org.junit.Assert;
    17 import org.junit.jupiter.api.extension.RegisterExtension;
    1819import org.junit.jupiter.api.Test;
     20import org.junit.jupiter.api.extension.RegisterExtension;
     21import org.junit.jupiter.params.ParameterizedTest;
     22import org.junit.jupiter.params.provider.Arguments;
     23import org.junit.jupiter.params.provider.MethodSource;
    1924import org.openstreetmap.josm.TestUtils;
    2025import org.openstreetmap.josm.data.coor.EastNorth;
    2126import org.openstreetmap.josm.data.coor.LatLon;
    import org.openstreetmap.josm.data.osm.Relation;  
    2631import org.openstreetmap.josm.data.osm.RelationMember;
    2732import org.openstreetmap.josm.data.osm.Way;
    2833import org.openstreetmap.josm.data.osm.search.SearchCompiler;
     34import org.openstreetmap.josm.data.projection.Projection;
     35import org.openstreetmap.josm.data.projection.ProjectionRegistry;
     36import org.openstreetmap.josm.data.projection.Projections;
    2937import org.openstreetmap.josm.io.OsmReader;
    3038import org.openstreetmap.josm.testutils.JOSMTestRules;
    3139
    class GeometryTest {  
    469477        assertTrue(expected > Geometry.getDistanceSegmentSegment(node1, node2, node3, node4));
    470478    }
    471479
     480    static Stream<Arguments> testGetLatLonFrom() {
     481        // The projection can quickly explode the test matrix, so only test WGS84 (EPSG:3857). If other projections have
     482        // issues, add them to the first list.
     483        return TestUtils.createTestMatrix(
     484                // Check specific projections
     485                Collections.singletonList(Projections.getProjectionByCode("EPSG:3857")),
     486                // Check extreme latitudes (degrees)
     487                Arrays.asList(0, 89, -89),
     488                // Test extreme longitudes (degrees)
     489                Arrays.asList(0, -179, 179),
     490                // Test various angles (degrees)
     491                // This tests cardinal directions, and then some varying angles.
     492                // TBH, the cardinal directions should find any issues uncovered by the varying angles,
     493                // but it may not.
     494                Arrays.asList(0, 90, 180, 270, 15, 30, 45, 60, 75),
     495                // Test various distances (meters)
     496                Arrays.asList(1, 10, 100, 1_000, 10_000)
     497                ).map(Arguments::of);
     498    }
     499
     500    @ParameterizedTest(name = "[{index}] {3}° {4}m @ lat = {1} lon = {2} - {0}")
     501    @MethodSource
     502    void testGetLatLonFrom(final Projection projection, final double lat, final double lon, final double angle, final double offsetInMeters) {
     503        ProjectionRegistry.setProjection(projection);
     504        final double offset = offsetInMeters / projection.getMetersPerUnit();
     505        final LatLon original = new LatLon(lat, lon);
     506
     507        final LatLon actual = (LatLon) Geometry.getLatLonFrom(original, Math.toRadians(angle), offset);
     508        // Due to degree -> radian -> degree conversion, there is a limit to how precise it can be
     509        assertEquals(offsetInMeters, original.greatCircleDistance(actual), 0.000_000_1);
     510        // The docs indicate that this should not be highly precise.
     511        assertEquals(angle, Math.toDegrees(original.bearing(actual)), 0.000_001);
     512    }
    472513}