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.oned;
    18
    19  import org.apache.commons.geometry.core.GeometryTestUtils;
    20  import org.apache.commons.geometry.spherical.SphericalTestUtils;
    21  import org.apache.commons.numbers.angle.Angle;
    22  import org.junit.jupiter.api.Assertions;
    23  import org.junit.jupiter.api.Test;
    24
    25  class Transform1STest {
    26
    27      private static final double TEST_EPS = 1e-10;
    28
    29      private static final Point1S ZERO = Point1S.ZERO;
    30
    31      private static final Point1S HALF_PI = Point1S.of(Angle.PI_OVER_TWO);
    32
    33      private static final Point1S PI = Point1S.of(Math.PI);
    34
    35      private static final Point1S MINUS_HALF_PI = Point1S.of(-Angle.PI_OVER_TWO);
    36
    37      @Test
    38      void testIdentity() {
    39          // act
    40          final Transform1S t = Transform1S.identity();
    41
    42          // assert
    43          Assertions.assertTrue(t.preservesOrientation());
    44          Assertions.assertFalse(t.isNegation());
    45          Assertions.assertEquals(0, t.getRotation(), TEST_EPS);
    46
    47          SphericalTestUtils.assertPointsEqual(ZERO, t.apply(ZERO), TEST_EPS);
    48          SphericalTestUtils.assertPointsEqual(HALF_PI, t.apply(HALF_PI), TEST_EPS);
    49          SphericalTestUtils.assertPointsEqual(PI, t.apply(PI), TEST_EPS);
    50          SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(MINUS_HALF_PI), TEST_EPS);
    51
    52          checkInverse(t);
    53      }
    54
    55      @Test
    56      void testRotate_positive() {
    57          // arrange
    58          final Transform1S t = Transform1S.createRotation(Angle.PI_OVER_TWO);
    59
    60          // act/assert
    61          Assertions.assertTrue(t.preservesOrientation());
    62          Assertions.assertFalse(t.isNegation());
    63          Assertions.assertEquals(Angle.PI_OVER_TWO, t.getRotation(), TEST_EPS);
    64
    65          SphericalTestUtils.assertPointsEqual(HALF_PI, t.apply(ZERO), TEST_EPS);
    66          SphericalTestUtils.assertPointsEqual(PI, t.apply(HALF_PI), TEST_EPS);
    67          SphericalTestUtils.assertPointsEqual(Point1S.of(1.5 * Math.PI), t.apply(PI), TEST_EPS);
    68          SphericalTestUtils.assertPointsEqual(Point1S.ZERO, t.apply(MINUS_HALF_PI), TEST_EPS);
    69
    70          checkInverse(t);
    71      }
    72
    73      @Test
    74      void testRotate_negative() {
    75          // arrange
    76          final Transform1S t = Transform1S.createRotation(-Angle.PI_OVER_TWO);
    77
    78          // act/assert
    79          Assertions.assertTrue(t.preservesOrientation());
    80          Assertions.assertFalse(t.isNegation());
    81          Assertions.assertEquals(-Angle.PI_OVER_TWO, t.getRotation(), TEST_EPS);
    82
    83          SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(ZERO), TEST_EPS);
    84          SphericalTestUtils.assertPointsEqual(ZERO, t.apply(HALF_PI), TEST_EPS);
    85          SphericalTestUtils.assertPointsEqual(HALF_PI, t.apply(PI), TEST_EPS);
    86          SphericalTestUtils.assertPointsEqual(Point1S.of(-Math.PI), t.apply(MINUS_HALF_PI), TEST_EPS);
    87
    88          checkInverse(t);
    89      }
    90
    91      @Test
    92      void testNegate() {
    93          // arrange
    94          final Transform1S t = Transform1S.createNegation();
    95
    96          // act/assert
    97          Assertions.assertFalse(t.preservesOrientation());
    98          Assertions.assertTrue(t.isNegation());
    99          Assertions.assertEquals(0, t.getRotation(), TEST_EPS);
    100
    101         SphericalTestUtils.assertPointsEqual(ZERO, t.apply(ZERO), TEST_EPS);
    102         SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(HALF_PI), TEST_EPS);
    103         SphericalTestUtils.assertPointsEqual(Point1S.of(-Math.PI), t.apply(PI), TEST_EPS);
    104         SphericalTestUtils.assertPointsEqual(HALF_PI, t.apply(MINUS_HALF_PI), TEST_EPS);
    105
    106         checkInverse(t);
    107     }
    108
    109     @Test
    110     void testNegateThenRotate() {
    111         // arrange
    112         final Transform1S t = Transform1S.createNegation().rotate(Angle.PI_OVER_TWO);
    113
    114         // act/assert
    115         Assertions.assertFalse(t.preservesOrientation());
    116         Assertions.assertTrue(t.isNegation());
    117         Assertions.assertEquals(Angle.PI_OVER_TWO, t.getRotation(), TEST_EPS);
    118
    119         SphericalTestUtils.assertPointsEqual(HALF_PI, t.apply(ZERO), TEST_EPS);
    120         SphericalTestUtils.assertPointsEqual(ZERO, t.apply(HALF_PI), TEST_EPS);
    121         SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(PI), TEST_EPS);
    122         SphericalTestUtils.assertPointsEqual(PI, t.apply(MINUS_HALF_PI), TEST_EPS);
    123
    124         checkInverse(t);
    125     }
    126
    127     @Test
    128     void testRotateThenNegate() {
    129         // arrange
    130         final Transform1S t = Transform1S.createRotation(Angle.PI_OVER_TWO).negate();
    131
    132         // act/assert
    133         Assertions.assertFalse(t.preservesOrientation());
    134         Assertions.assertTrue(t.isNegation());
    135         Assertions.assertEquals(-Angle.PI_OVER_TWO, t.getRotation(), TEST_EPS);
    136
    137         SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(ZERO), TEST_EPS);
    138         SphericalTestUtils.assertPointsEqual(Point1S.of(-Math.PI), t.apply(HALF_PI), TEST_EPS);
    139         SphericalTestUtils.assertPointsEqual(Point1S.of(-1.5 * Math.PI), t.apply(PI), TEST_EPS);
    140         SphericalTestUtils.assertPointsEqual(ZERO, t.apply(MINUS_HALF_PI), TEST_EPS);
    141
    142         checkInverse(t);
    143     }
    144
    145     @Test
    146     void testMultiply() {
    147         // arrange
    148         final Transform1S neg = Transform1S.identity().negate();
    149         final Transform1S rot = Transform1S.identity().rotate(Angle.PI_OVER_TWO);
    150
    151         // act
    152         final Transform1S t = rot.multiply(neg);
    153
    154         // assert
    155         Assertions.assertFalse(t.preservesOrientation());
    156         Assertions.assertTrue(t.isNegation());
    157         Assertions.assertEquals(Angle.PI_OVER_TWO, t.getRotation(), TEST_EPS);
    158
    159         SphericalTestUtils.assertPointsEqual(HALF_PI, t.apply(ZERO), TEST_EPS);
    160         SphericalTestUtils.assertPointsEqual(ZERO, t.apply(HALF_PI), TEST_EPS);
    161         SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(PI), TEST_EPS);
    162         SphericalTestUtils.assertPointsEqual(PI, t.apply(MINUS_HALF_PI), TEST_EPS);
    163
    164         checkInverse(t);
    165     }
    166
    167     @Test
    168     void testPreultiply() {
    169         // arrange
    170         final Transform1S neg = Transform1S.identity().negate();
    171         final Transform1S rot = Transform1S.identity().rotate(Angle.PI_OVER_TWO);
    172
    173         // act
    174         final Transform1S t = neg.premultiply(rot);
    175
    176         // assert
    177         Assertions.assertFalse(t.preservesOrientation());
    178         Assertions.assertTrue(t.isNegation());
    179         Assertions.assertEquals(Angle.PI_OVER_TWO, t.getRotation(), TEST_EPS);
    180
    181         SphericalTestUtils.assertPointsEqual(HALF_PI, t.apply(ZERO), TEST_EPS);
    182         SphericalTestUtils.assertPointsEqual(ZERO, t.apply(HALF_PI), TEST_EPS);
    183         SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(PI), TEST_EPS);
    184         SphericalTestUtils.assertPointsEqual(PI, t.apply(MINUS_HALF_PI), TEST_EPS);
    185
    186         checkInverse(t);
    187     }
    188
    189     @Test
    190     void testHashCode() {
    191         // arrange
    192         final Transform1S a = Transform1S.identity().negate().rotate(Angle.PI_OVER_TWO);
    193         final Transform1S b = Transform1S.identity().rotate(Angle.PI_OVER_TWO);
    194         final Transform1S c = Transform1S.identity().negate().rotate(-Angle.PI_OVER_TWO);
    195         final Transform1S d = Transform1S.identity().negate().rotate(Angle.PI_OVER_TWO);
    196
    197         // act
    198         final int hash = a.hashCode();
    199
    200         // assert
    201         Assertions.assertEquals(hash, a.hashCode());
    202
    203         Assertions.assertNotEquals(hash, b.hashCode());
    204         Assertions.assertNotEquals(hash, c.hashCode());
    205
    206         Assertions.assertEquals(hash, d.hashCode());
    207     }
    208
    209     @Test
    210     void testEquals() {
    211         // arrange
    212         final Transform1S a = Transform1S.identity().negate().rotate(Angle.PI_OVER_TWO);
    213         final Transform1S b = Transform1S.identity().rotate(Angle.PI_OVER_TWO);
    214         final Transform1S c = Transform1S.identity().negate().rotate(-Angle.PI_OVER_TWO);
    215         final Transform1S d = Transform1S.identity().negate().rotate(Angle.PI_OVER_TWO);
    216
    217         // act/assert
    218         GeometryTestUtils.assertSimpleEqualsCases(a);
    219
    220         Assertions.assertNotEquals(a, b);
    221         Assertions.assertNotEquals(a, c);
    222
    223         Assertions.assertEquals(a, d);
    224         Assertions.assertEquals(d, a);
    225     }
    226
    227     @Test
    228     void testToString() {
    229         // arrange
    230         final Transform1S t = Transform1S.identity().negate().rotate(1);
    231
    232         // act
    233         final String str = t.toString();
    234
    235         // assert
    236         GeometryTestUtils.assertContains("Transform1S", str);
    237         GeometryTestUtils.assertContains("negate= true", str);
    238         GeometryTestUtils.assertContains("rotate= 1", str);
    239     }
    240
    241     private static void checkInverse(final Transform1S t) {
    242         final Transform1S inv = t.inverse();
    243
    244         for (double x = -Angle.TWO_PI; x <= 2 * Angle.TWO_PI; x += 0.2) {
    245             final Point1S pt = Point1S.of(x);
    246
    247             SphericalTestUtils.assertPointsEqual(pt, inv.apply(t.apply(pt)), TEST_EPS);
    248             SphericalTestUtils.assertPointsEqual(pt, t.apply(inv.apply(pt)), TEST_EPS);
    249         }
    250     }
    251 }