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.hull.euclidean.twod;
    18
    19  import java.util.ArrayList;
    20  import java.util.Arrays;
    21  import java.util.Collections;
    22  import java.util.List;
    23
    24  import org.apache.commons.geometry.core.GeometryTestUtils;
    25  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
    26  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
    27  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    28  import org.apache.commons.geometry.euclidean.twod.Vector2D;
    29  import org.apache.commons.geometry.euclidean.twod.path.LinePath;
    30  import org.junit.Assert;
    31  import org.junit.Test;
    32
    33  public class ConvexHull2DTest {
    34
    35      private static final double TEST_EPS = 1e-10;
    36
    37      private static final DoublePrecisionContext TEST_PRECISION =
    38              new EpsilonDoublePrecisionContext(TEST_EPS);
    39
    40      @Test
    41      public void testProperties_noPoints() {
    42          // act
    43          final ConvexHull2D hull = new ConvexHull2D(Collections.emptyList(), TEST_PRECISION);
    44
    45          // assert
    46          Assert.assertEquals(0, hull.getVertices().size());
    47
    48          final LinePath path = hull.getPath();
    49          Assert.assertEquals(0, path.getElements().size());
    50
    51          final List<Vector2D> pathVertices = path.getVertexSequence();
    52          Assert.assertEquals(0, pathVertices.size());
    53
    54          Assert.assertNull(hull.getRegion());
    55      }
    56
    57      @Test
    58      public void testProperties_singlePoint() {
    59          // arrange
    60          final List<Vector2D> vertices = Collections.singletonList(Vector2D.Unit.PLUS_X);
    61
    62          // act
    63          final ConvexHull2D hull = new ConvexHull2D(vertices, TEST_PRECISION);
    64
    65          // assert
    66          Assert.assertEquals(vertices, hull.getVertices());
    67
    68          final LinePath path = hull.getPath();
    69          Assert.assertEquals(0, path.getElements().size());
    70
    71          final List<Vector2D> pathVertices = path.getVertexSequence();
    72          Assert.assertEquals(0, pathVertices.size());
    73
    74          Assert.assertNull(hull.getRegion());
    75      }
    76
    77      @Test
    78      public void testProperties_twoPoints() {
    79          // arrange
    80          final List<Vector2D> vertices = Arrays.asList(Vector2D.Unit.PLUS_X, Vector2D.Unit.PLUS_Y);
    81
    82          // act
    83          final ConvexHull2D hull = new ConvexHull2D(vertices, TEST_PRECISION);
    84
    85          // assert
    86          Assert.assertEquals(vertices, hull.getVertices());
    87
    88          final LinePath path = hull.getPath();
    89          Assert.assertEquals(1, path.getElements().size());
    90
    91          final List<Vector2D> pathVertices = path.getVertexSequence();
    92          Assert.assertEquals(2, pathVertices.size());
    93          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.PLUS_X, pathVertices.get(0), TEST_EPS);
    94          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.PLUS_Y, pathVertices.get(1), TEST_EPS);
    95
    96          Assert.assertNull(hull.getRegion());
    97      }
    98
    99      @Test
    100     public void testProperties_threePoints() {
    101         // arrange
    102         final List<Vector2D> vertices = Arrays.asList(Vector2D.ZERO, Vector2D.Unit.PLUS_X, Vector2D.Unit.PLUS_Y);
    103
    104         // act
    105         final ConvexHull2D hull = new ConvexHull2D(vertices, TEST_PRECISION);
    106
    107         // assert
    108         Assert.assertEquals(vertices, hull.getVertices());
    109
    110         final LinePath path = hull.getPath();
    111         Assert.assertEquals(3, path.getElements().size());
    112
    113         final List<Vector2D> pathVertices = path.getVertexSequence();
    114         Assert.assertEquals(4, pathVertices.size());
    115         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, pathVertices.get(0), TEST_EPS);
    116         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.PLUS_X, pathVertices.get(1), TEST_EPS);
    117         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.PLUS_Y, pathVertices.get(2), TEST_EPS);
    118         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, pathVertices.get(3), TEST_EPS);
    119
    120         Assert.assertEquals(0.5, hull.getRegion().getSize(), TEST_EPS);
    121     }
    122
    123     @Test
    124     public void testProperties_fourPoints() {
    125         // arrange
    126         final List<Vector2D> vertices = Arrays.asList(Vector2D.ZERO, Vector2D.Unit.PLUS_X,
    127                 Vector2D.of(1, 1), Vector2D.Unit.PLUS_Y);
    128
    129         // act
    130         final ConvexHull2D hull = new ConvexHull2D(vertices, TEST_PRECISION);
    131
    132         // assert
    133         Assert.assertEquals(vertices, hull.getVertices());
    134
    135         final LinePath path = hull.getPath();
    136         Assert.assertEquals(4, path.getElements().size());
    137
    138         final List<Vector2D> pathVertices = path.getVertexSequence();
    139         Assert.assertEquals(5, pathVertices.size());
    140         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, pathVertices.get(0), TEST_EPS);
    141         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.PLUS_X, pathVertices.get(1), TEST_EPS);
    142         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(1, 1), pathVertices.get(2), TEST_EPS);
    143         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.PLUS_Y, pathVertices.get(3), TEST_EPS);
    144         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, pathVertices.get(4), TEST_EPS);
    145
    146         Assert.assertEquals(1.0, hull.getRegion().getSize(), TEST_EPS);
    147     }
    148
    149     @Test
    150     public void testVertexListCannotBeModified() {
    151         // arrange
    152         final List<Vector2D> vertices = new ArrayList<>();
    153         vertices.add(Vector2D.Unit.PLUS_X);
    154
    155         final ConvexHull2D hull = new ConvexHull2D(vertices, TEST_PRECISION);
    156
    157         // act
    158         final List<Vector2D> hullVertices = hull.getVertices();
    159
    160         // assert
    161         Assert.assertNotSame(vertices, hullVertices);
    162         GeometryTestUtils.assertThrows(() -> {
    163             hullVertices.add(Vector2D.Unit.PLUS_Y);
    164         }, UnsupportedOperationException.class);
    165     }
    166
    167     @Test
    168     public void testToString() {
    169         // arrange
    170         final List<Vector2D> vertices = Collections.singletonList(Vector2D.Unit.PLUS_X);
    171         final ConvexHull2D hull = new ConvexHull2D(vertices, TEST_PRECISION);
    172
    173         // act
    174         final String str = hull.toString();
    175
    176         // assert
    177         GeometryTestUtils.assertContains("ConvexHull2D[vertices= [(1", str);
    178     }
    179 }