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.io.euclidean.threed;
    18
    19  import java.util.Arrays;
    20  import java.util.List;
    21
    22  import org.apache.commons.geometry.core.GeometryTestUtils;
    23  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    24  import org.apache.commons.geometry.euclidean.threed.ConvexPolygon3D;
    25  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    26  import org.apache.commons.numbers.core.Precision;
    27  import org.junit.jupiter.api.Assertions;
    28  import org.junit.jupiter.api.Test;
    29
    30  class FacetDefinitionsTest {
    31
    32      private static final double TEST_EPS = 1e-10;
    33
    34      private static final Precision.DoubleEquivalence TEST_PRECISION = Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
    35
    36      private static final List<Vector3D> FACET_PTS = Arrays.asList(
    37              Vector3D.ZERO, Vector3D.of(1, 0, 0), Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0));
    38
    39      @Test
    40      void testToPolygon_noNormal() {
    41          // arrange
    42          final SimpleFacetDefinition f = new SimpleFacetDefinition(FACET_PTS);
    43
    44          // act
    45          final ConvexPolygon3D p = FacetDefinitions.toPolygon(f, TEST_PRECISION);
    46
    47          // assert
    48          Assertions.assertSame(TEST_PRECISION, p.getPlane().getPrecision());
    49
    50          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_Z, p.getPlane().getNormal(), TEST_EPS);
    51          Assertions.assertEquals(4, p.getVertices().size());
    52          Assertions.assertEquals(1.0, p.getSize(), TEST_EPS);
    53      }
    54
    55      @Test
    56      void testToPolygon_withNormal_similarDirection() {
    57          // arrange
    58          final Vector3D normal = Vector3D.of(0.1, 0.2, 0.3);
    59          final SimpleFacetDefinition f = new SimpleFacetDefinition(FACET_PTS, normal);
    60
    61          // act
    62          final ConvexPolygon3D p = FacetDefinitions.toPolygon(f, TEST_PRECISION);
    63
    64          // assert
    65          Assertions.assertSame(TEST_PRECISION, p.getPlane().getPrecision());
    66
    67          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_Z, p.getPlane().getNormal(), TEST_EPS);
    68          Assertions.assertEquals(4, p.getVertices().size());
    69          Assertions.assertEquals(1.0, p.getSize(), TEST_EPS);
    70      }
    71
    72      @Test
    73      void testToPolygon_withNormal_differentDirection() {
    74          // arrange
    75          final Vector3D normal = Vector3D.of(0.1, 0.2, -0.3);
    76          final SimpleFacetDefinition f = new SimpleFacetDefinition(FACET_PTS, normal);
    77
    78          // act
    79          final ConvexPolygon3D p = FacetDefinitions.toPolygon(f, TEST_PRECISION);
    80
    81          // assert
    82          Assertions.assertSame(TEST_PRECISION, p.getPlane().getPrecision());
    83
    84          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_Z, p.getPlane().getNormal(), TEST_EPS);
    85          Assertions.assertEquals(4, p.getVertices().size());
    86          Assertions.assertEquals(1.0, p.getSize(), TEST_EPS);
    87      }
    88
    89      @Test
    90      void testToPolygon_failure() {
    91          // arrange
    92          final SimpleFacetDefinition f = new SimpleFacetDefinition(Arrays.asList(
    93                  Vector3D.ZERO, Vector3D.ZERO, Vector3D.ZERO));
    94
    95          // act/assert
    96          Assertions.assertThrows(IllegalArgumentException.class, () -> FacetDefinitions.toPolygon(f, TEST_PRECISION));
    97      }
    98
    99      @Test
    100     void testToPolygon_invalidArgs() {
    101         // arrange
    102         final SimpleFacetDefinition f = new SimpleFacetDefinition(Arrays.asList(
    103                 Vector3D.ZERO, Vector3D.ZERO, Vector3D.ZERO));
    104
    105         // act/assert
    106         GeometryTestUtils.assertThrowsWithMessage(
    107                 () -> FacetDefinitions.toPolygon(null, TEST_PRECISION),
    108                 NullPointerException.class, "Facet cannot be null");
    109
    110         GeometryTestUtils.assertThrowsWithMessage(
    111                 () -> FacetDefinitions.toPolygon(f, null),
    112                 NullPointerException.class, "Precision context cannot be null");
    113     }
    114 }