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;
    18
    19  import java.util.Arrays;
    20  import java.util.List;
    21
    22  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
    23  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
    24  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    25  import org.apache.commons.geometry.euclidean.twod.ConvexArea;
    26  import org.apache.commons.geometry.euclidean.twod.Vector2D;
    27  import org.apache.commons.geometry.hull.euclidean.twod.ConvexHull2D;
    28  import org.apache.commons.geometry.hull.euclidean.twod.MonotoneChain;
    29  import org.junit.Assert;
    30  import org.junit.Test;
    31
    32  /** This class contains code listed as examples in the user guide and other documentation.
    33   * If any portion of this code changes, the corresponding examples in the documentation <em>must</em> be updated.
    34   */
    35  public class DocumentationExamplesTest {
    36
    37      private static final double TEST_EPS = 1e-15;
    38
    39      @Test
    40      public void testMonotoneChainExample() {
    41          final DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-10);
    42
    43          // create a list of input points for the algorithm
    44          final List<Vector2D> pts = Arrays.asList(
    45                      Vector2D.ZERO,
    46                      Vector2D.of(0.5, 0.5),
    47                      Vector2D.of(0, 0.5),
    48                      Vector2D.of(0, 1),
    49                      Vector2D.of(0.25, 0.1),
    50                      Vector2D.of(1, 0),
    51                      Vector2D.of(1, 1),
    52                      Vector2D.of(0.75, 0.9)
    53                  );
    54
    55          // create an instance of the monotone chain convex hull generator
    56          final MonotoneChain mc = new MonotoneChain(precision);
    57
    58          // compute the convex hull
    59          final ConvexHull2D hull = mc.generate(pts);
    60
    61          // list the vertices from the input that were used in the hull
    62          final List<Vector2D> vertices = hull.getVertices(); // [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
    63
    64          // get the hull as a region
    65          final ConvexArea region = hull.getRegion();
    66          final boolean containsAll = pts.stream().allMatch(region::contains); // true - region contains all input points
    67
    68          // ---
    69          Assert.assertEquals(4, vertices.size());
    70          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, vertices.get(0), TEST_EPS);
    71          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(1, 0), vertices.get(1), TEST_EPS);
    72          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(1, 1), vertices.get(2), TEST_EPS);
    73          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, 1), vertices.get(3), TEST_EPS);
    74
    75          Assert.assertTrue(containsAll);
    76      }
    77  }