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
    22  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    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 BoundarySourceBoundsBuilder3DTest {
    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 testGetBounds_noBoundaries() {
    36          // arrange
    37          final BoundarySource3D src = BoundarySource3D.of(new ArrayList<>());
    38          final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
    39
    40          // act
    41          final Bounds3D b = builder.getBounds(src);
    42
    43          // assert
    44          Assertions.assertNull(b);
    45      }
    46
    47      @Test
    48      void testGetBounds_singleFiniteBoundary() {
    49          // arrange
    50          final ConvexPolygon3D poly = Planes.convexPolygonFromVertices(Arrays.asList(
    51                  Vector3D.of(1, 1, 1),
    52                  Vector3D.of(1, 0, 2),
    53                  Vector3D.of(3, 4, 5)), TEST_PRECISION);
    54
    55          final BoundarySource3D src = BoundarySource3D.of(poly);
    56          final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
    57
    58          // act
    59          final Bounds3D b = builder.getBounds(src);
    60
    61          // assert
    62          checkBounds(b, Vector3D.of(1, 0, 1), Vector3D.of(3, 4, 5));
    63          for (final Vector3D pt : poly.getVertices()) {
    64              Assertions.assertTrue(b.contains(pt));
    65          }
    66      }
    67
    68      @Test
    69      void testGetBounds_multipleFiniteBoundaries() {
    70          // arrange
    71          final ConvexPolygon3D poly1 = Planes.convexPolygonFromVertices(Arrays.asList(
    72                  Vector3D.of(1, 1, 1),
    73                  Vector3D.of(1, 0, 2),
    74                  Vector3D.of(3, 4, 5)), TEST_PRECISION);
    75
    76          final ConvexPolygon3D poly2 = Planes.convexPolygonFromVertices(Arrays.asList(
    77                  Vector3D.of(-1, 1, 1),
    78                  Vector3D.of(1, 4, 4),
    79                  Vector3D.of(7, 4, 5)), TEST_PRECISION);
    80
    81          final ConvexPolygon3D poly3 = Planes.convexPolygonFromVertices(Arrays.asList(
    82                  Vector3D.of(-2, 1, 1),
    83                  Vector3D.of(1, 7, 2),
    84                  Vector3D.of(5, 4, 10)), TEST_PRECISION);
    85
    86          final BoundarySource3D src = BoundarySource3D.of(poly1, poly2, poly3);
    87          final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
    88
    89          // act
    90          final Bounds3D b = builder.getBounds(src);
    91
    92          // assert
    93          checkBounds(b, Vector3D.of(-2, 0, 1), Vector3D.of(7, 7, 10));
    94
    95          src.boundaryStream().forEach(boundary -> {
    96              for (final Vector3D pt : boundary.getVertices()) {
    97                  Assertions.assertTrue(b.contains(pt));
    98              }
    99          });
    100     }
    101
    102     @Test
    103     void testGetBounds_singleInfiniteBoundary() {
    104         // arrange
    105         final PlaneConvexSubset boundary = Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Z, TEST_PRECISION)
    106                 .span();
    107         final BoundarySource3D src = BoundarySource3D.of(boundary);
    108         final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
    109
    110         // act
    111         final Bounds3D b = builder.getBounds(src);
    112
    113         // assert
    114         Assertions.assertNull(b);
    115     }
    116
    117     @Test
    118     void testGetBounds_mixedFiniteAndInfiniteBoundaries() {
    119         // arrange
    120         final PlaneConvexSubset inf = Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Z, TEST_PRECISION)
    121                 .span()
    122                 .split(Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Y, TEST_PRECISION))
    123                 .getMinus();
    124
    125         final ConvexPolygon3D poly1 = Planes.convexPolygonFromVertices(Arrays.asList(
    126                 Vector3D.of(1, 1, 1),
    127                 Vector3D.of(1, 0, 2),
    128                 Vector3D.of(3, 4, 5)), TEST_PRECISION);
    129
    130         final ConvexPolygon3D poly2 = Planes.convexPolygonFromVertices(Arrays.asList(
    131                 Vector3D.of(-1, 1, 1),
    132                 Vector3D.of(1, 4, 4),
    133                 Vector3D.of(7, 4, 5)), TEST_PRECISION);
    134
    135         final ConvexPolygon3D poly3 = Planes.convexPolygonFromVertices(Arrays.asList(
    136                 Vector3D.of(-2, 1, 1),
    137                 Vector3D.of(1, 7, 2),
    138                 Vector3D.of(5, 4, 10)), TEST_PRECISION);
    139
    140         final BoundarySource3D src = BoundarySource3D.of(poly1, poly2, inf, poly3);
    141         final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
    142
    143         // act
    144         final Bounds3D b = builder.getBounds(src);
    145
    146         // assert
    147         Assertions.assertNull(b);
    148     }
    149
    150     private static void checkBounds(final Bounds3D b, final Vector3D min, final Vector3D max) {
    151         EuclideanTestUtils.assertCoordinatesEqual(min, b.getMin(), TEST_EPS);
    152         EuclideanTestUtils.assertCoordinatesEqual(max, b.getMax(), TEST_EPS);
    153     }
    154 }