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.euclidean.internal;
    18
    19  import org.apache.commons.geometry.core.GeometryTestUtils;
    20  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    21  import org.apache.commons.geometry.euclidean.oned.Vector1D;
    22  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    23  import org.junit.Assert;
    24  import org.junit.Test;
    25
    26  public class VectorsTest {
    27
    28      private static final double EPS = Math.ulp(1d);
    29
    30      @Test
    31      public void testIsRealNonZero() {
    32          // act/assert
    33          Assert.assertTrue(Vectors.isRealNonZero(1e-20));
    34          Assert.assertTrue(Vectors.isRealNonZero(1e20));
    35          Assert.assertTrue(Vectors.isRealNonZero(-1e-20));
    36          Assert.assertTrue(Vectors.isRealNonZero(-1e20));
    37
    38          Assert.assertFalse(Vectors.isRealNonZero(0.0));
    39          Assert.assertFalse(Vectors.isRealNonZero(-0.0));
    40          Assert.assertFalse(Vectors.isRealNonZero(Double.NaN));
    41          Assert.assertFalse(Vectors.isRealNonZero(Double.POSITIVE_INFINITY));
    42          Assert.assertFalse(Vectors.isRealNonZero(Double.NEGATIVE_INFINITY));
    43      }
    44
    45      @Test
    46      public void testCheckedNorm_normArg() {
    47          // act/assert
    48          Assert.assertEquals(1.0, Vectors.checkedNorm(1.0), EPS);
    49          Assert.assertEquals(23.12, Vectors.checkedNorm(23.12), EPS);
    50          Assert.assertEquals(2e-12, Vectors.checkedNorm(2e-12), EPS);
    51
    52          Assert.assertEquals(-1.0, Vectors.checkedNorm(-1.0), EPS);
    53          Assert.assertEquals(-23.12, Vectors.checkedNorm(-23.12), EPS);
    54          Assert.assertEquals(-2e-12, Vectors.checkedNorm(-2e-12), EPS);
    55
    56          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(0.0),
    57                  IllegalArgumentException.class, "Illegal norm: 0.0");
    58          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Double.NaN),
    59                  IllegalArgumentException.class, "Illegal norm: NaN");
    60          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Double.POSITIVE_INFINITY),
    61                  IllegalArgumentException.class, "Illegal norm: Infinity");
    62          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Double.NEGATIVE_INFINITY),
    63                  IllegalArgumentException.class, "Illegal norm: -Infinity");
    64      }
    65
    66      @Test
    67      public void testCheckedNorm_vectorArg() {
    68          // act/assert
    69          Assert.assertEquals(1.0, Vectors.checkedNorm(Vector1D.of(1.0)), EPS);
    70          Assert.assertEquals(23.12, Vectors.checkedNorm(Vector1D.of(23.12)), EPS);
    71          Assert.assertEquals(2e-12, Vectors.checkedNorm(Vector1D.of(2e-12)), EPS);
    72
    73          Assert.assertEquals(1.0, Vectors.checkedNorm(Vector1D.of(-1.0)), EPS);
    74          Assert.assertEquals(23.12, Vectors.checkedNorm(Vector1D.of(-23.12)), EPS);
    75          Assert.assertEquals(2e-12, Vectors.checkedNorm(Vector1D.of(-2e-12)), EPS);
    76
    77          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.ZERO),
    78                  IllegalArgumentException.class, "Illegal norm: 0.0");
    79          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.NaN),
    80                  IllegalArgumentException.class, "Illegal norm: NaN");
    81          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.POSITIVE_INFINITY),
    82                  IllegalArgumentException.class, "Illegal norm: NaN");
    83          GeometryTestUtils.assertThrows(() -> Vectors.checkedNorm(Vector3D.NEGATIVE_INFINITY),
    84                  IllegalArgumentException.class, "Illegal norm: NaN");
    85      }
    86
    87      @Test
    88      public void testTryNormalize() {
    89          // act/assert
    90          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X,
    91                  Vectors.tryNormalize(Vector3D.of(2, 0, 0)), EPS);
    92
    93          Assert.assertNull(Vectors.tryNormalize(Vector3D.of(0, 0, 0)));
    94          Assert.assertNull(Vectors.tryNormalize(Vector3D.of(-0, 0, 0)));
    95
    96          Assert.assertNull(Vectors.tryNormalize(Vector3D.of(Double.NaN, 1, 1)));
    97          Assert.assertNull(Vectors.tryNormalize(Vector3D.of(1, Double.POSITIVE_INFINITY, 1)));
    98          Assert.assertNull(Vectors.tryNormalize(Vector3D.of(1, 1, Double.NEGATIVE_INFINITY)));
    99      }
    100
    101     @Test
    102     public void testNorm_oneD() {
    103         // act/assert
    104         Assert.assertEquals(0.0, Vectors.norm(0.0), EPS);
    105
    106         Assert.assertEquals(2.0, Vectors.norm(-2.0), EPS);
    107         Assert.assertEquals(1.0, Vectors.norm(-1.0), EPS);
    108
    109         Assert.assertEquals(1.0, Vectors.norm(1.0), EPS);
    110         Assert.assertEquals(2.0, Vectors.norm(2.0), EPS);
    111     }
    112
    113     @Test
    114     public void testNorm_twoD() {
    115         // act/assert
    116         Assert.assertEquals(0.0, Vectors.norm(0.0, 0.0), EPS);
    117
    118         Assert.assertEquals(Math.sqrt(5.0), Vectors.norm(1.0, 2.0), EPS);
    119         Assert.assertEquals(5.0, Vectors.norm(3.0, -4.0), EPS);
    120         Assert.assertEquals(Math.sqrt(61.0), Vectors.norm(-5.0, 6.0), EPS);
    121         Assert.assertEquals(Math.sqrt(130.0), Vectors.norm(-7.0, -9.0), EPS);
    122     }
    123
    124     @Test
    125     public void testNorm_threeD() {
    126         // act/assert
    127         Assert.assertEquals(0.0, Vectors.norm(0.0, 0.0, 0.0), EPS);
    128
    129         Assert.assertEquals(Math.sqrt(14.0), Vectors.norm(1.0, 2.0, 3.0), EPS);
    130         Assert.assertEquals(Math.sqrt(77.0), Vectors.norm(4.0, 5.0, -6.0), EPS);
    131         Assert.assertEquals(Math.sqrt(194.0), Vectors.norm(7.0, -8.0, 9.0), EPS);
    132         Assert.assertEquals(Math.sqrt(365.0), Vectors.norm(10.0, -11.0, -12.0), EPS);
    133         Assert.assertEquals(Math.sqrt(590.0), Vectors.norm(-13.0, 14.0, 15.0), EPS);
    134         Assert.assertEquals(Math.sqrt(869.0), Vectors.norm(-16.0, 17.0, -18.0), EPS);
    135         Assert.assertEquals(Math.sqrt(1202.0), Vectors.norm(-19.0, -20.0, 21.0), EPS);
    136         Assert.assertEquals(Math.sqrt(1589.0), Vectors.norm(-22.0, -23.0, -24.0), EPS);
    137     }
    138
    139     @Test
    140     public void testNormSq_oneD() {
    141         // act/assert
    142         Assert.assertEquals(0.0, Vectors.normSq(0.0), EPS);
    143
    144         Assert.assertEquals(9.0, Vectors.normSq(-3.0), EPS);
    145         Assert.assertEquals(1.0, Vectors.normSq(-1.0), EPS);
    146
    147         Assert.assertEquals(1.0, Vectors.normSq(1.0), EPS);
    148         Assert.assertEquals(9.0, Vectors.normSq(3.0), EPS);
    149     }
    150
    151     @Test
    152     public void testNormSq_twoD() {
    153         // act/assert
    154         Assert.assertEquals(0.0, Vectors.normSq(0.0, 0.0), EPS);
    155
    156         Assert.assertEquals(5.0, Vectors.normSq(1.0, 2.0), EPS);
    157         Assert.assertEquals(25.0, Vectors.normSq(3.0, -4.0), EPS);
    158         Assert.assertEquals(61.0, Vectors.normSq(-5.0, 6.0), EPS);
    159         Assert.assertEquals(130.0, Vectors.normSq(-7.0, -9.0), EPS);
    160     }
    161
    162     @Test
    163     public void testNormSq_threeD() {
    164         // act/assert
    165         Assert.assertEquals(0.0, Vectors.normSq(0.0, 0.0, 0.0), EPS);
    166
    167         Assert.assertEquals(14.0, Vectors.normSq(1.0, 2.0, 3.0), EPS);
    168         Assert.assertEquals(77.0, Vectors.normSq(4.0, 5.0, -6.0), EPS);
    169         Assert.assertEquals(194.0, Vectors.normSq(7.0, -8.0, 9.0), EPS);
    170         Assert.assertEquals(365.0, Vectors.normSq(10.0, -11.0, -12.0), EPS);
    171         Assert.assertEquals(590.0, Vectors.normSq(-13.0, 14.0, 15.0), EPS);
    172         Assert.assertEquals(869.0, Vectors.normSq(-16.0, 17.0, -18.0), EPS);
    173         Assert.assertEquals(1202.0, Vectors.normSq(-19.0, -20.0, 21.0), EPS);
    174         Assert.assertEquals(1589.0, Vectors.normSq(-22.0, -23.0, -24.0), EPS);
    175     }
    176 }