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.List;
    21  import java.util.stream.Collectors;
    22
    23  import org.apache.commons.numbers.core.Precision;
    24  import org.junit.jupiter.api.Assertions;
    25  import org.junit.jupiter.api.Test;
    26
    27  class BoundarySource2DTest {
    28
    29      private static final double TEST_EPS = 1e-10;
    30
    31      private static final Precision.DoubleEquivalence TEST_PRECISION =
    32              Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
    33
    34      @Test
    35      void testToList() {
    36          // act
    37          final BoundarySource2D src = BoundarySource2D.of(
    38              Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.of(1, 0), TEST_PRECISION),
    39              Lines.segmentFromPoints(Vector2D.of(1, 0), Vector2D.of(1, 1), TEST_PRECISION)
    40          );
    41
    42          // act
    43          final BoundaryList2D list = src.toList();
    44
    45          // assert
    46          Assertions.assertEquals(2, list.count());
    47      }
    48
    49      @Test
    50      void testToList_noBoundaries() {
    51          // act
    52          final BoundarySource2D src = BoundarySource2D.of();
    53
    54          // act
    55          final BoundaryList2D list = src.toList();
    56
    57          // assert
    58          Assertions.assertEquals(0, list.count());
    59      }
    60
    61      @Test
    62      void testToTree() {
    63          // act
    64          final BoundarySource2D src = BoundarySource2D.of(
    65              Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.of(1, 0), TEST_PRECISION),
    66              Lines.segmentFromPoints(Vector2D.of(1, 0), Vector2D.of(1, 1), TEST_PRECISION)
    67          );
    68
    69          // act
    70          final RegionBSPTree2D tree = src.toTree();
    71
    72          // assert
    73          Assertions.assertEquals(5, tree.count());
    74          Assertions.assertFalse(tree.isFull());
    75          Assertions.assertFalse(tree.isEmpty());
    76      }
    77
    78      @Test
    79      void testToTree_noBoundaries() {
    80          // act
    81          final BoundarySource2D src = BoundarySource2D.of();
    82
    83          // act
    84          final RegionBSPTree2D tree = src.toTree();
    85
    86          // assert
    87          Assertions.assertEquals(1, tree.count());
    88          Assertions.assertFalse(tree.isFull());
    89          Assertions.assertTrue(tree.isEmpty());
    90      }
    91
    92      @Test
    93      void testOf_varargs_empty() {
    94          // act
    95          final BoundarySource2D src = BoundarySource2D.of();
    96
    97          // assert
    98          final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
    99          Assertions.assertEquals(0, segments.size());
    100     }
    101
    102     @Test
    103     void testOf_varargs() {
    104         // act
    105         final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
    106         final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
    107
    108         final BoundarySource2D src = BoundarySource2D.of(a, b);
    109
    110         // assert
    111         final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
    112         Assertions.assertEquals(2, segments.size());
    113
    114         Assertions.assertSame(a, segments.get(0));
    115         Assertions.assertSame(b, segments.get(1));
    116     }
    117
    118     @Test
    119     void testOf_list_empty() {
    120         // arrange
    121         final List<LineConvexSubset> input = new ArrayList<>();
    122
    123         // act
    124         final BoundarySource2D src = BoundarySource2D.of(input);
    125
    126         // assert
    127         final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
    128         Assertions.assertEquals(0, segments.size());
    129     }
    130
    131     @Test
    132     void testOf_list() {
    133         // act
    134         final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
    135         final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
    136
    137         final List<LineConvexSubset> input = new ArrayList<>();
    138         input.add(a);
    139         input.add(b);
    140
    141         final BoundarySource2D src = BoundarySource2D.of(input);
    142
    143         // assert
    144         final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
    145         Assertions.assertEquals(2, segments.size());
    146
    147         Assertions.assertSame(a, segments.get(0));
    148         Assertions.assertSame(b, segments.get(1));
    149     }
    150 }