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.enclosing;
    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.precision.DoublePrecisionContext;
    25  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
    26  import org.apache.commons.geometry.euclidean.twod.Vector2D;
    27  import org.junit.Assert;
    28  import org.junit.Test;
    29
    30  public class EnclosingBallTest {
    31
    32      private static final double TEST_EPS = 1e-10;
    33
    34      @Test
    35      public void testProperties_emptySupport() {
    36          // arrange
    37          final Vector2D center = Vector2D.of(1.2, 3.4);
    38          final double radius = 10;
    39          final List<Vector2D> support = new ArrayList<>();
    40
    41          // act
    42          final EnclosingBall<Vector2D> ball = new EnclosingBall<>(center, radius, support);
    43
    44          // assert
    45          Assert.assertSame(center, ball.getCenter());
    46          Assert.assertEquals(radius, ball.getRadius(), TEST_EPS);
    47          Assert.assertEquals(0, ball.getSupportSize());
    48
    49          final List<Vector2D> resultSupport = ball.getSupport();
    50          Assert.assertEquals(0, resultSupport.size());
    51      }
    52
    53      @Test
    54      public void testProperties_nonEmptySupport() {
    55          // arrange
    56          final Vector2D center = Vector2D.of(1.2, 3.4);
    57          final double radius = 10;
    58          final List<Vector2D> support = new ArrayList<>(Arrays.asList(
    59                  Vector2D.ZERO, Vector2D.Unit.PLUS_X, Vector2D.Unit.PLUS_Y));
    60
    61          // act
    62          final EnclosingBall<Vector2D> ball = new EnclosingBall<>(center, radius, support);
    63
    64          // assert
    65          Assert.assertSame(center, ball.getCenter());
    66          Assert.assertEquals(radius, ball.getRadius(), TEST_EPS);
    67          Assert.assertEquals(3, ball.getSupportSize());
    68
    69          final List<Vector2D> resultSupport = ball.getSupport();
    70          Assert.assertNotSame(support, resultSupport);
    71          Assert.assertEquals(support, resultSupport);
    72      }
    73
    74      @Test(expected = UnsupportedOperationException.class)
    75      public void testGetSupport_listCannotBeModified() {
    76          // arrange
    77          final List<Vector2D> support = new ArrayList<>(Collections.singletonList(Vector2D.ZERO));
    78
    79          final EnclosingBall<Vector2D> ball = new EnclosingBall<>(Vector2D.of(1, 1), 4, support);
    80
    81          // act/assert
    82          ball.getSupport().add(Vector2D.Unit.PLUS_X);
    83      }
    84
    85      @Test
    86      public void testContains_strict() {
    87          // arrange
    88          final Vector2D center = Vector2D.of(1, 2);
    89          final double radius = 2;
    90          final EnclosingBall<Vector2D> ball = new EnclosingBall<>(center, radius, Collections.emptyList());
    91
    92          // act/assert
    93          Assert.assertTrue(ball.contains(center));
    94
    95          Assert.assertTrue(ball.contains(Vector2D.of(2, 3)));
    96          Assert.assertTrue(ball.contains(Vector2D.of(0, 1)));
    97
    98          Assert.assertTrue(ball.contains(Vector2D.of(0, 2)));
    99          Assert.assertTrue(ball.contains(Vector2D.of(1, 4)));
    100
    101         Assert.assertFalse(ball.contains(Vector2D.of(3.00001, 2)));
    102         Assert.assertFalse(ball.contains(Vector2D.of(1, -1e-12)));
    103
    104         Assert.assertFalse(ball.contains(Vector2D.of(1, 5)));
    105         Assert.assertFalse(ball.contains(Vector2D.of(1, -1)));
    106         Assert.assertFalse(ball.contains(Vector2D.of(-2, 2)));
    107         Assert.assertFalse(ball.contains(Vector2D.of(4, 2)));
    108     }
    109
    110     @Test
    111     public void testContains_precision() {
    112         // arrange
    113         final DoublePrecisionContext lowerPrecision = new EpsilonDoublePrecisionContext(1e-4);
    114         final DoublePrecisionContext higherPrecision = new EpsilonDoublePrecisionContext(1e-10);
    115
    116         final Vector2D center = Vector2D.of(1, 2);
    117         final double radius = 2;
    118         final EnclosingBall<Vector2D> ball = new EnclosingBall<>(center, radius, Collections.emptyList());
    119
    120         // act/assert
    121         Assert.assertTrue(ball.contains(center, higherPrecision));
    122
    123         Assert.assertTrue(ball.contains(Vector2D.of(2, 3), higherPrecision));
    124         Assert.assertTrue(ball.contains(Vector2D.of(0, 1), higherPrecision));
    125
    126         Assert.assertTrue(ball.contains(Vector2D.of(0, 2), higherPrecision));
    127         Assert.assertTrue(ball.contains(Vector2D.of(1, 4), higherPrecision));
    128
    129         Assert.assertFalse(ball.contains(Vector2D.of(3.00001, 2), higherPrecision));
    130         Assert.assertTrue(ball.contains(Vector2D.of(1, -1e-12), higherPrecision));
    131
    132         Assert.assertTrue(ball.contains(Vector2D.of(3.00001, 2), lowerPrecision));
    133         Assert.assertTrue(ball.contains(Vector2D.of(1, -1e-12), lowerPrecision));
    134
    135         Assert.assertFalse(ball.contains(Vector2D.of(1, 5), higherPrecision));
    136         Assert.assertFalse(ball.contains(Vector2D.of(1, -1), higherPrecision));
    137         Assert.assertFalse(ball.contains(Vector2D.of(-2, 2), higherPrecision));
    138         Assert.assertFalse(ball.contains(Vector2D.of(4, 2), higherPrecision));
    139     }
    140
    141     @Test
    142     public void testToString() {
    143         // arrange
    144         final EnclosingBall<Vector2D> ball = new EnclosingBall<>(Vector2D.ZERO, 1, Collections.singletonList(Vector2D.Unit.PLUS_X));
    145
    146         // act
    147         final String str = ball.toString();
    148
    149         // assert
    150         Assert.assertTrue(str.startsWith("EnclosingBall[center= (0"));
    151         Assert.assertTrue(str.contains("radius= 1"));
    152     }
    153 }