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.twod;
    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.numbers.core.Precision;
    25  import org.junit.jupiter.api.Assertions;
    26  import org.junit.jupiter.api.Test;
    27
    28  class LinecastPoint2DTest {
    29
    30      private static final double TEST_EPS = 1e-10;
    31
    32      private static final Precision.DoubleEquivalence TEST_PRECISION =
    33              Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
    34
    35      private static final Line X_AXIS =
    36              Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
    37
    38      private static final Line Y_AXIS =
    39              Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_Y, TEST_PRECISION);
    40
    41      @Test
    42      void testProperties() {
    43          // arrange
    44          final Vector2D pt = Vector2D.of(1, 1);
    45          final Vector2D normal = Vector2D.Unit.PLUS_X;
    46
    47          final LinecastPoint2D it = new LinecastPoint2D(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 testAbscissaOrder() {
    58          // arrange
    59          final LinecastPoint2D a = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, X_AXIS);
    60
    61          final LinecastPoint2D b = new LinecastPoint2D(Vector2D.of(2, 2), Vector2D.Unit.PLUS_X, X_AXIS);
    62          final LinecastPoint2D c = new LinecastPoint2D(Vector2D.of(-3, 3), Vector2D.Unit.PLUS_Y, X_AXIS);
    63          final LinecastPoint2D d = new LinecastPoint2D(Vector2D.of(1, 4), Vector2D.Unit.PLUS_Y, X_AXIS);
    64          final LinecastPoint2D e = new LinecastPoint2D(Vector2D.of(1, 4), Vector2D.Unit.PLUS_X, X_AXIS);
    65
    66          // act/assert
    67          Assertions.assertEquals(-1, LinecastPoint2D.ABSCISSA_ORDER.compare(a, b));
    68          Assertions.assertEquals(1, LinecastPoint2D.ABSCISSA_ORDER.compare(a, c));
    69          Assertions.assertEquals(1, LinecastPoint2D.ABSCISSA_ORDER.compare(a, d));
    70          Assertions.assertEquals(0, LinecastPoint2D.ABSCISSA_ORDER.compare(a, e));
    71      }
    72
    73      @Test
    74      void testHashCode() {
    75          // arrange
    76          final LinecastPoint2D a = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, X_AXIS);
    77          final LinecastPoint2D b = new LinecastPoint2D(Vector2D.of(2, 2), Vector2D.Unit.PLUS_X, X_AXIS);
    78          final LinecastPoint2D c = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_Y, X_AXIS);
    79          final LinecastPoint2D d = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, Y_AXIS);
    80          final LinecastPoint2D e = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.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 LinecastPoint2D a = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, X_AXIS);
    99          final LinecastPoint2D b = new LinecastPoint2D(Vector2D.of(2, 2), Vector2D.Unit.PLUS_X, X_AXIS);
    100         final LinecastPoint2D c = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_Y, X_AXIS);
    101         final LinecastPoint2D d = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, Y_AXIS);
    102         final LinecastPoint2D e = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.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 Line line = Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_X, precision);
    121         final Line otherLine = Lines.fromPointAndDirection(Vector2D.of(1e-4, 1e-4), Vector2D.Unit.PLUS_X, precision);
    122
    123         final LinecastPoint2D a = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, line);
    124
    125         final LinecastPoint2D b = new LinecastPoint2D(Vector2D.of(2, 2), Vector2D.Unit.PLUS_X, line);
    126         final LinecastPoint2D c = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_Y, line);
    127
    128         final LinecastPoint2D d = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, line);
    129         final LinecastPoint2D e = new LinecastPoint2D(
    130                 Vector2D.of(1 + 1e-3, 1 + 1e-3), Vector2D.Unit.from(1 + 1e-3, 1e-3), otherLine);
    131
    132         // act/assert
    133         Assertions.assertTrue(a.eq(a, precision));
    134
    135         Assertions.assertFalse(a.eq(b, precision));
    136         Assertions.assertFalse(a.eq(c, precision));
    137
    138         Assertions.assertTrue(a.eq(d, precision));
    139         Assertions.assertTrue(a.eq(e, precision));
    140     }
    141
    142     @Test
    143     void testToString() {
    144         // arrange
    145         final LinecastPoint2D it = new LinecastPoint2D(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, X_AXIS);
    146
    147         // act
    148         final String str = it.toString();
    149
    150         // assert
    151         GeometryTestUtils.assertContains("LinecastPoint2D[point= (1.0, 1.0), normal= (1.0, 0.0)", str);
    152     }
    153
    154     @Test
    155     void testSortAndFilter_empty() {
    156         // arrange
    157         final List<LinecastPoint2D> pts = new ArrayList<>();
    158
    159         // act
    160         LinecastPoint2D.sortAndFilter(pts);
    161
    162         // assert
    163         Assertions.assertEquals(0, pts.size());
    164     }
    165
    166     @Test
    167     void testSortAndFilter() {
    168         // arrange
    169         final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-2);
    170
    171         final Line line = Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_X, precision);
    172         final Line eqLine = Lines.fromPointAndDirection(Vector2D.of(1e-3, 1e-3), Vector2D.Unit.PLUS_X, precision);
    173         final Line diffLine = Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_Y, precision);
    174
    175         final LinecastPoint2D a = new LinecastPoint2D(Vector2D.ZERO, Vector2D.Unit.MINUS_Y, line);
    176         final LinecastPoint2D aDup1 = new LinecastPoint2D(Vector2D.of(1e-3, 0), Vector2D.Unit.MINUS_Y, line);
    177         final LinecastPoint2D aDup2 = new LinecastPoint2D(Vector2D.of(1e-3, 1e-3), Vector2D.of(1e-3, -1), eqLine);
    178
    179         final LinecastPoint2D b = new LinecastPoint2D(Vector2D.ZERO, Vector2D.Unit.MINUS_X, diffLine);
    180         final LinecastPoint2D bDup = new LinecastPoint2D(Vector2D.of(-1e-3, 1e-4), Vector2D.Unit.MINUS_X, diffLine);
    181
    182         final LinecastPoint2D c = new LinecastPoint2D(Vector2D.of(0.5, 0), Vector2D.Unit.MINUS_Y, line);
    183
    184         final LinecastPoint2D d = new LinecastPoint2D(Vector2D.of(1, 0), Vector2D.Unit.MINUS_Y, line);
    185
    186         final List<LinecastPoint2D> list = new ArrayList<>(Arrays.asList(d, aDup1, bDup, b, c, a, aDup2));
    187
    188         // act
    189         LinecastPoint2D.sortAndFilter(list);
    190
    191         // assert
    192         Assertions.assertEquals(4, list.size());
    193
    194         Assertions.assertSame(b, list.get(0));
    195         Assertions.assertSame(a, list.get(1));
    196         Assertions.assertSame(c, list.get(2));
    197         Assertions.assertSame(d, list.get(3));
    198     }
    199 }