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.PlaneAngleRadians;
    22  import org.junit.Assert;
    23  import org.junit.Test;
    24
    25  public 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(PlaneAngleRadians.PI_OVER_TWO);
    32
    33      private static final Point1S PI = Point1S.of(PlaneAngleRadians.PI);
    34
    35      private static final Point1S MINUS_HALF_PI = Point1S.of(-PlaneAngleRadians.PI_OVER_TWO);
    36
    37      @Test
    38      public void testIdentity() {
    39          // act
    40          final Transform1S t = Transform1S.identity();
    41
    42          // assert
    43          Assert.assertTrue(t.preservesOrientation());
    44          Assert.assertFalse(t.isNegation());
    45          Assert.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      public void testRotate_positive() {
    57          // arrange
    58          final Transform1S t = Transform1S.createRotation(PlaneAngleRadians.PI_OVER_TWO);
    59
    60          // act/assert
    61          Assert.assertTrue(t.preservesOrientation());
    62          Assert.assertFalse(t.isNegation());
    63          Assert.assertEquals(PlaneAngleRadians.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 * PlaneAngleRadians.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      public void testRotate_negative() {
    75          // arrange
    76          final Transform1S t = Transform1S.createRotation(-PlaneAngleRadians.PI_OVER_TWO);
    77
    78          // act/assert
    79          Assert.assertTrue(t.preservesOrientation());
    80          Assert.assertFalse(t.isNegation());
    81          Assert.assertEquals(-PlaneAngleRadians.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(-PlaneAngleRadians.PI), t.apply(MINUS_HALF_PI), TEST_EPS);
    87
    88          checkInverse(t);
    89      }
    90
    91      @Test
    92      public void testNegate() {
    93          // arrange
    94          final Transform1S t = Transform1S.createNegation();
    95
    96          // act/assert
    97          Assert.assertFalse(t.preservesOrientation());
    98          Assert.assertTrue(t.isNegation());
    99          Assert.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(-PlaneAngleRadians.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     public void testNegateThenRotate() {
    111         // arrange
    112         final Transform1S t = Transform1S.createNegation().rotate(PlaneAngleRadians.PI_OVER_TWO);
    113
    114         // act/assert
    115         Assert.assertFalse(t.preservesOrientation());
    116         Assert.assertTrue(t.isNegation());
    117         Assert.assertEquals(PlaneAngleRadians.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     public void testRotateThenNegate() {
    129         // arrange
    130         final Transform1S t = Transform1S.createRotation(PlaneAngleRadians.PI_OVER_TWO).negate();
    131
    132         // act/assert
    133         Assert.assertFalse(t.preservesOrientation());
    134         Assert.assertTrue(t.isNegation());
    135         Assert.assertEquals(-PlaneAngleRadians.PI_OVER_TWO, t.getRotation(), TEST_EPS);
    136
    137         SphericalTestUtils.assertPointsEqual(MINUS_HALF_PI, t.apply(ZERO), TEST_EPS);
    138         SphericalTestUtils.assertPointsEqual(Point1S.of(-PlaneAngleRadians.PI), t.apply(HALF_PI), TEST_EPS);
    139         SphericalTestUtils.assertPointsEqual(Point1S.of(-1.5 * PlaneAngleRadians.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     public void testMultiply() {
    147         // arrange
    148         final Transform1S neg = Transform1S.identity().negate();
    149         final Transform1S rot = Transform1S.identity().rotate(PlaneAngleRadians.PI_OVER_TWO);
    150
    151         // act
    152         final Transform1S t = rot.multiply(neg);
    153
    154         // assert
    155         Assert.assertFalse(t.preservesOrientation());
    156         Assert.assertTrue(t.isNegation());
    157         Assert.assertEquals(PlaneAngleRadians.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     public void testPreultiply() {
    169         // arrange
    170         final Transform1S neg = Transform1S.identity().negate();
    171         final Transform1S rot = Transform1S.identity().rotate(PlaneAngleRadians.PI_OVER_TWO);
    172
    173         // act
    174         final Transform1S t = neg.premultiply(rot);
    175
    176         // assert
    177         Assert.assertFalse(t.preservesOrientation());
    178         Assert.assertTrue(t.isNegation());
    179         Assert.assertEquals(PlaneAngleRadians.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     public void testHashCode() {
    191         // arrange
    192         final Transform1S a = Transform1S.identity().negate().rotate(PlaneAngleRadians.PI_OVER_TWO);
    193         final Transform1S b = Transform1S.identity().rotate(PlaneAngleRadians.PI_OVER_TWO);
    194         final Transform1S c = Transform1S.identity().negate().rotate(-PlaneAngleRadians.PI_OVER_TWO);
    195         final Transform1S d = Transform1S.identity().negate().rotate(PlaneAngleRadians.PI_OVER_TWO);
    196
    197         // act/assert
    198         Assert.assertEquals(a, a);
    199
    200         Assert.assertFalse(a.equals(null));
    201         Assert.assertFalse(a.equals(new Object()));
    202
    203         Assert.assertNotEquals(a, b);
    204         Assert.assertNotEquals(a, c);
    205
    206         Assert.assertEquals(a, d);
    207         Assert.assertEquals(d, a);
    208     }
    209
    210     @Test
    211     public void testEquals() {
    212         // arrange
    213         final Transform1S a = Transform1S.identity().negate().rotate(PlaneAngleRadians.PI_OVER_TWO);
    214         final Transform1S b = Transform1S.identity().rotate(PlaneAngleRadians.PI_OVER_TWO);
    215         final Transform1S c = Transform1S.identity().negate().rotate(-PlaneAngleRadians.PI_OVER_TWO);
    216         final Transform1S d = Transform1S.identity().negate().rotate(PlaneAngleRadians.PI_OVER_TWO);
    217
    218         // act
    219         final int hash = a.hashCode();
    220
    221         // assert
    222         Assert.assertEquals(hash, a.hashCode());
    223
    224         Assert.assertNotEquals(hash, b.hashCode());
    225         Assert.assertNotEquals(hash, c.hashCode());
    226
    227         Assert.assertEquals(hash, d.hashCode());
    228     }
    229
    230     @Test
    231     public void testToString() {
    232         // arrange
    233         final Transform1S t = Transform1S.identity().negate().rotate(1);
    234
    235         // act
    236         final String str = t.toString();
    237
    238         // assert
    239         GeometryTestUtils.assertContains("Transform1S", str);
    240         GeometryTestUtils.assertContains("negate= true", str);
    241         GeometryTestUtils.assertContains("rotate= 1", str);
    242     }
    243
    244     private static void checkInverse(final Transform1S t) {
    245         final Transform1S inv = t.inverse();
    246
    247         for (double x = -PlaneAngleRadians.TWO_PI; x <= 2 * PlaneAngleRadians.TWO_PI; x += 0.2) {
    248             final Point1S pt = Point1S.of(x);
    249
    250             SphericalTestUtils.assertPointsEqual(pt, inv.apply(t.apply(pt)), TEST_EPS);
    251             SphericalTestUtils.assertPointsEqual(pt, t.apply(inv.apply(pt)), TEST_EPS);
    252         }
    253     }
    254 }