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