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