View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.geometry.spherical.twod;
    18
    19  import java.util.ArrayList;
    20  import java.util.Arrays;
    21  import java.util.Collections;
    22  import java.util.List;
    23  import java.util.function.Consumer;
    24
    25  import org.apache.commons.geometry.spherical.SphericalTestUtils;
    26  import org.apache.commons.geometry.spherical.twod.InteriorAngleGreatArcConnector.Maximize;
    27  import org.apache.commons.geometry.spherical.twod.InteriorAngleGreatArcConnector.Minimize;
    28  import org.apache.commons.numbers.core.Precision;
    29  import org.junit.jupiter.api.Assertions;
    30  import org.junit.jupiter.api.Test;
    31
    32  class InteriorAngleGreatArcConnectorTest {
    33
    34      private static final double TEST_EPS = 1e-10;
    35
    36      private static final Precision.DoubleEquivalence TEST_PRECISION =
    37              Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
    38
    39      @Test
    40      void testConnectAll_empty() {
    41          runWithMaxAndMin(connector -> {
    42              // arrange
    43              final List<GreatArc> arcs = new ArrayList<>();
    44              connector.add(arcs);
    45
    46              // act
    47              final List<GreatArcPath> paths = connector.connectAll();
    48
    49              // assert
    50              Assertions.assertEquals(0, paths.size());
    51          });
    52      }
    53
    54      @Test
    55      void testConnectAll_singlePath() {
    56          runWithMaxAndMin(connector -> {
    57              // arrange
    58              final List<GreatArc> arcs = Collections.singletonList(
    59                      GreatCircles.arcFromPoints(Point2S.PLUS_I, Point2S.PLUS_J, TEST_PRECISION)
    60              );
    61              connector.add(arcs);
    62
    63              // act
    64              final List<GreatArcPath> paths = connector.connectAll();
    65
    66              // assert
    67              Assertions.assertEquals(1, paths.size());
    68
    69              final GreatArcPath a = paths.get(0);
    70              Assertions.assertEquals(1, a.getArcs().size());
    71              assertPathPoints(a, Point2S.PLUS_I, Point2S.PLUS_J);
    72          });
    73      }
    74
    75      @Test
    76      void testConnectAll_maximize_instance() {
    77          // arrange
    78          final GreatArc a1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.PLUS_I, TEST_PRECISION);
    79          final GreatArc a2 = GreatCircles.arcFromPoints(Point2S.PLUS_I, Point2S.PLUS_J, TEST_PRECISION);
    80          final GreatArc a3 = GreatCircles.arcFromPoints(Point2S.PLUS_J, Point2S.PLUS_K, TEST_PRECISION);
    81
    82          final GreatArc b1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.MINUS_I, TEST_PRECISION);
    83          final GreatArc b2 = GreatCircles.arcFromPoints(Point2S.MINUS_I, Point2S.MINUS_J, TEST_PRECISION);
    84          final GreatArc b3 = GreatCircles.arcFromPoints(Point2S.MINUS_J, Point2S.PLUS_K, TEST_PRECISION);
    85
    86          final InteriorAngleGreatArcConnector connector = new InteriorAngleGreatArcConnector.Maximize();
    87
    88          // act
    89          final List<GreatArcPath> paths = connector.connectAll(Arrays.asList(b3, b1, a1, a3, b2, a2));
    90
    91          // assert
    92          Assertions.assertEquals(1, paths.size());
    93
    94          assertPathPoints(paths.get(0),
    95              Point2S.PLUS_K,
    96              Point2S.MINUS_I,
    97              Point2S.MINUS_J,
    98              Point2S.PLUS_K,
    99              Point2S.PLUS_I,
    100             Point2S.PLUS_J,
    101             Point2S.PLUS_K
    102         );
    103     }
    104
    105     @Test
    106     void testConnectAll_maximize_method() {
    107         // arrange
    108         final GreatArc a1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.PLUS_I, TEST_PRECISION);
    109         final GreatArc a2 = GreatCircles.arcFromPoints(Point2S.PLUS_I, Point2S.PLUS_J, TEST_PRECISION);
    110         final GreatArc a3 = GreatCircles.arcFromPoints(Point2S.PLUS_J, Point2S.PLUS_K, TEST_PRECISION);
    111
    112         final GreatArc b1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.MINUS_I, TEST_PRECISION);
    113         final GreatArc b2 = GreatCircles.arcFromPoints(Point2S.MINUS_I, Point2S.MINUS_J, TEST_PRECISION);
    114         final GreatArc b3 = GreatCircles.arcFromPoints(Point2S.MINUS_J, Point2S.PLUS_K, TEST_PRECISION);
    115
    116         // act
    117         final List<GreatArcPath> paths = InteriorAngleGreatArcConnector.connectMaximized(
    118                 Arrays.asList(b3, b1, a1, a3, b2, a2));
    119
    120         // assert
    121         Assertions.assertEquals(1, paths.size());
    122
    123         assertPathPoints(paths.get(0),
    124             Point2S.PLUS_K,
    125             Point2S.MINUS_I,
    126             Point2S.MINUS_J,
    127             Point2S.PLUS_K,
    128             Point2S.PLUS_I,
    129             Point2S.PLUS_J,
    130             Point2S.PLUS_K
    131         );
    132     }
    133
    134     @Test
    135     void testConnectAll_minimize_instance() {
    136         // arrange
    137         final GreatArc a1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.PLUS_I, TEST_PRECISION);
    138         final GreatArc a2 = GreatCircles.arcFromPoints(Point2S.PLUS_I, Point2S.PLUS_J, TEST_PRECISION);
    139         final GreatArc a3 = GreatCircles.arcFromPoints(Point2S.PLUS_J, Point2S.PLUS_K, TEST_PRECISION);
    140
    141         final GreatArc b1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.MINUS_I, TEST_PRECISION);
    142         final GreatArc b2 = GreatCircles.arcFromPoints(Point2S.MINUS_I, Point2S.MINUS_J, TEST_PRECISION);
    143         final GreatArc b3 = GreatCircles.arcFromPoints(Point2S.MINUS_J, Point2S.PLUS_K, TEST_PRECISION);
    144
    145         final InteriorAngleGreatArcConnector connector = new InteriorAngleGreatArcConnector.Minimize();
    146
    147         // act
    148         final List<GreatArcPath> paths = connector.connectAll(Arrays.asList(b3, b1, a1, a3, b2, a2));
    149
    150         // assert
    151         Assertions.assertEquals(2, paths.size());
    152
    153         assertPathPoints(paths.get(0),
    154             Point2S.PLUS_K,
    155             Point2S.MINUS_I,
    156             Point2S.MINUS_J,
    157             Point2S.PLUS_K
    158         );
    159
    160         assertPathPoints(paths.get(1),
    161             Point2S.PLUS_K,
    162             Point2S.PLUS_I,
    163             Point2S.PLUS_J,
    164             Point2S.PLUS_K
    165         );
    166     }
    167
    168     @Test
    169     void testConnectAll_minimize_method() {
    170         // arrange
    171         final GreatArc a1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.PLUS_I, TEST_PRECISION);
    172         final GreatArc a2 = GreatCircles.arcFromPoints(Point2S.PLUS_I, Point2S.PLUS_J, TEST_PRECISION);
    173         final GreatArc a3 = GreatCircles.arcFromPoints(Point2S.PLUS_J, Point2S.PLUS_K, TEST_PRECISION);
    174
    175         final GreatArc b1 = GreatCircles.arcFromPoints(Point2S.PLUS_K, Point2S.MINUS_I, TEST_PRECISION);
    176         final GreatArc b2 = GreatCircles.arcFromPoints(Point2S.MINUS_I, Point2S.MINUS_J, TEST_PRECISION);
    177         final GreatArc b3 = GreatCircles.arcFromPoints(Point2S.MINUS_J, Point2S.PLUS_K, TEST_PRECISION);
    178
    179         // act
    180         final List<GreatArcPath> paths = InteriorAngleGreatArcConnector.connectMinimized(
    181                 Arrays.asList(b3, b1, a1, a3, b2, a2));
    182
    183         // assert
    184         Assertions.assertEquals(2, paths.size());
    185
    186         assertPathPoints(paths.get(0),
    187             Point2S.PLUS_K,
    188             Point2S.MINUS_I,
    189             Point2S.MINUS_J,
    190             Point2S.PLUS_K
    191         );
    192
    193         assertPathPoints(paths.get(1),
    194             Point2S.PLUS_K,
    195             Point2S.PLUS_I,
    196             Point2S.PLUS_J,
    197             Point2S.PLUS_K
    198         );
    199     }
    200
    201     /**
    202      * Run the given consumer function twice, once with a Maximize instance and once with
    203      * a Minimize instance.
    204      */
    205     private static void runWithMaxAndMin(final Consumer<? super InteriorAngleGreatArcConnector> body) {
    206         body.accept(new Maximize());
    207         body.accept(new Minimize());
    208     }
    209
    210     private static void assertPathPoints(final GreatArcPath path, final Point2S... points) {
    211         final List<Point2S> expectedPoints = Arrays.asList(points);
    212         final List<Point2S> actualPoints = path.getVertices();
    213
    214         final String msg = "Expected path points to equal " + expectedPoints + " but was " + actualPoints;
    215         Assertions.assertEquals(expectedPoints.size(), actualPoints.size(), msg);
    216
    217         for (int i = 0; i < expectedPoints.size(); ++i) {
    218             SphericalTestUtils.assertPointsEq(expectedPoints.get(i), actualPoints.get(i), TEST_EPS);
    219         }
    220     }
    221 }