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.threed.line;
    18
    19  import java.util.ArrayList;
    20  import java.util.Arrays;
    21  import java.util.List;
    22
    23  import org.apache.commons.geometry.core.GeometryTestUtils;
    24  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    25  import org.apache.commons.numbers.core.Precision;
    26  import org.junit.jupiter.api.Assertions;
    27  import org.junit.jupiter.api.Test;
    28
    29  class LinecastPoint3DTest {
    30
    31      private static final double TEST_EPS = 1e-10;
    32
    33      private static final Precision.DoubleEquivalence TEST_PRECISION = Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
    34
    35      private static final Line3D X_AXIS = Lines3D.fromPointAndDirection(Vector3D.ZERO, Vector3D.Unit.PLUS_X,
    36              TEST_PRECISION);
    37
    38      private static final Line3D Y_AXIS = Lines3D.fromPointAndDirection(Vector3D.ZERO, Vector3D.Unit.PLUS_Y,
    39              TEST_PRECISION);
    40
    41      @Test
    42      void testProperties() {
    43          // arrange
    44          final Vector3D pt = Vector3D.of(1, 1, 1);
    45          final Vector3D normal = Vector3D.Unit.MINUS_X;
    46
    47          final LinecastPoint3D it = new LinecastPoint3D(pt, normal, X_AXIS);
    48
    49          // act
    50          Assertions.assertSame(pt, it.getPoint());
    51          Assertions.assertSame(normal, it.getNormal());
    52          Assertions.assertSame(X_AXIS, it.getLine());
    53          Assertions.assertEquals(1.0, it.getAbscissa(), TEST_EPS);
    54      }
    55
    56      @Test
    57      void testCompareTo() {
    58          // arrange
    59          final LinecastPoint3D a = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, X_AXIS);
    60
    61          final LinecastPoint3D b = new LinecastPoint3D(Vector3D.of(2, 2, 2), Vector3D.Unit.PLUS_X, X_AXIS);
    62          final LinecastPoint3D c = new LinecastPoint3D(Vector3D.of(-3, 3, 3), Vector3D.Unit.PLUS_X, X_AXIS);
    63          final LinecastPoint3D d = new LinecastPoint3D(Vector3D.of(1, 4, 4), Vector3D.Unit.PLUS_Y, X_AXIS);
    64          final LinecastPoint3D e = new LinecastPoint3D(Vector3D.of(1, 4, 4), Vector3D.Unit.PLUS_X, X_AXIS);
    65
    66          // act/assert
    67          Assertions.assertEquals(-1, LinecastPoint3D.ABSCISSA_ORDER.compare(a, b));
    68          Assertions.assertEquals(1, LinecastPoint3D.ABSCISSA_ORDER.compare(a, c));
    69          Assertions.assertEquals(1, LinecastPoint3D.ABSCISSA_ORDER.compare(a, d));
    70          Assertions.assertEquals(0, LinecastPoint3D.ABSCISSA_ORDER.compare(a, e));
    71      }
    72
    73      @Test
    74      void testHashCode() {
    75          // arrange
    76          final LinecastPoint3D a = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, X_AXIS);
    77          final LinecastPoint3D b = new LinecastPoint3D(Vector3D.of(2, 2, 2), Vector3D.Unit.PLUS_X, X_AXIS);
    78          final LinecastPoint3D c = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_Y, X_AXIS);
    79          final LinecastPoint3D d = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, Y_AXIS);
    80          final LinecastPoint3D e = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, X_AXIS);
    81
    82          // act
    83          final int hash = a.hashCode();
    84
    85          // assert
    86          Assertions.assertEquals(hash, a.hashCode());
    87
    88          Assertions.assertNotEquals(hash, b.hashCode());
    89          Assertions.assertNotEquals(hash, c.hashCode());
    90          Assertions.assertNotEquals(hash, d.hashCode());
    91
    92          Assertions.assertEquals(hash, e.hashCode());
    93      }
    94
    95      @Test
    96      void testEquals() {
    97          // arrange
    98          final LinecastPoint3D a = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, X_AXIS);
    99          final LinecastPoint3D b = new LinecastPoint3D(Vector3D.of(2, 2, 2), Vector3D.Unit.PLUS_X, X_AXIS);
    100         final LinecastPoint3D c = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_Y, X_AXIS);
    101         final LinecastPoint3D d = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, Y_AXIS);
    102         final LinecastPoint3D e = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, X_AXIS);
    103
    104         // act/assert
    105         GeometryTestUtils.assertSimpleEqualsCases(a);
    106
    107         Assertions.assertNotEquals(a, b);
    108         Assertions.assertNotEquals(a, c);
    109         Assertions.assertNotEquals(a, d);
    110
    111         Assertions.assertEquals(a, e);
    112         Assertions.assertEquals(e, a);
    113     }
    114
    115     @Test
    116     void testEq() {
    117         // arrange
    118         final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-2);
    119
    120         final Line3D line = Lines3D.fromPointAndDirection(Vector3D.ZERO, Vector3D.Unit.PLUS_X, precision);
    121         final Line3D otherLine = Lines3D.fromPointAndDirection(Vector3D.of(1e-4, 1e-4, 1e-4), Vector3D.Unit.PLUS_X,
    122                 precision);
    123
    124         final LinecastPoint3D a = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, line);
    125
    126         final LinecastPoint3D b = new LinecastPoint3D(Vector3D.of(2, 2, 2), Vector3D.Unit.PLUS_X, line);
    127         final LinecastPoint3D c = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_Y, line);
    128
    129         final LinecastPoint3D d = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, line);
    130         final LinecastPoint3D e = new LinecastPoint3D(Vector3D.of(1 + 1e-3, 1 + 1e-3, 1 + 1e-3),
    131                 Vector3D.Unit.from(1 + 1e-3, 1e-3, 1e-3), otherLine);
    132
    133         // act/assert
    134         Assertions.assertTrue(a.eq(a, precision));
    135
    136         Assertions.assertFalse(a.eq(b, precision));
    137         Assertions.assertFalse(a.eq(c, precision));
    138
    139         Assertions.assertTrue(a.eq(d, precision));
    140         Assertions.assertTrue(a.eq(e, precision));
    141     }
    142
    143     @Test
    144     void testToString() {
    145         // arrange
    146         final LinecastPoint3D it = new LinecastPoint3D(Vector3D.of(1, 1, 1), Vector3D.Unit.PLUS_X, X_AXIS);
    147
    148         // act
    149         final String str = it.toString();
    150
    151         // assert
    152         GeometryTestUtils.assertContains("LinecastPoint3D[point= (1.0, 1.0, 1.0), normal= (1.0, 0.0, 0.0)", str);
    153     }
    154
    155     @Test
    156     void testSortAndFilter_empty() {
    157         // arrange
    158         final List<LinecastPoint3D> pts = new ArrayList<>();
    159
    160         // act
    161         LinecastPoint3D.sortAndFilter(pts);
    162
    163         // assert
    164         Assertions.assertEquals(0, pts.size());
    165     }
    166
    167     @Test
    168     void testSortAndFilter() {
    169         // arrange
    170         final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-2);
    171
    172         final Line3D line = Lines3D.fromPointAndDirection(Vector3D.ZERO, Vector3D.Unit.PLUS_X, precision);
    173         final Line3D eqLine = Lines3D.fromPointAndDirection(Vector3D.of(1e-3, 1e-3, 1e-3), Vector3D.Unit.PLUS_X,
    174                 precision);
    175         final Line3D diffLine = Lines3D.fromPointAndDirection(Vector3D.ZERO, Vector3D.Unit.PLUS_Y, precision);
    176
    177         final LinecastPoint3D a = new LinecastPoint3D(Vector3D.ZERO, Vector3D.Unit.MINUS_Y, line);
    178         final LinecastPoint3D aDup1 = new LinecastPoint3D(Vector3D.of(1e-3, 0, 0), Vector3D.Unit.MINUS_Y, line);
    179         final LinecastPoint3D aDup2 = new LinecastPoint3D(Vector3D.of(1e-3, 1e-3, 1e-3), Vector3D.of(1e-3, -1, 0),
    180                 eqLine);
    181
    182         final LinecastPoint3D b = new LinecastPoint3D(Vector3D.ZERO, Vector3D.Unit.MINUS_X, diffLine);
    183         final LinecastPoint3D bDup = new LinecastPoint3D(Vector3D.of(-1e-3, 1e-4, 1e-4), Vector3D.Unit.MINUS_X,
    184                 diffLine);
    185
    186         final LinecastPoint3D c = new LinecastPoint3D(Vector3D.of(0.5, 0, 0), Vector3D.Unit.MINUS_Y, line);
    187
    188         final LinecastPoint3D d = new LinecastPoint3D(Vector3D.of(1, 0, 0), Vector3D.Unit.MINUS_Y, line);
    189
    190         final List<LinecastPoint3D> list = new ArrayList<>(Arrays.asList(d, aDup1, bDup, b, c, a, aDup2));
    191
    192         // act
    193         LinecastPoint3D.sortAndFilter(list);
    194
    195         // assert
    196         Assertions.assertEquals(4, list.size());
    197
    198         Assertions.assertSame(b, list.get(0));
    199         Assertions.assertSame(a, list.get(1));
    200         Assertions.assertSame(c, list.get(2));
    201         Assertions.assertSame(d, list.get(3));
    202     }
    203 }