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