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.obj;
    18
    19  import java.io.StringReader;
    20  import java.util.Arrays;
    21  import java.util.List;
    22  import java.util.regex.Pattern;
    23
    24  import org.apache.commons.geometry.core.GeometryTestUtils;
    25  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    26  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    27  import org.apache.commons.geometry.io.core.test.CloseCountReader;
    28  import org.apache.commons.geometry.io.euclidean.EuclideanIOTestUtils;
    29  import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
    30  import org.junit.jupiter.api.Assertions;
    31  import org.junit.jupiter.api.Test;
    32
    33  class ObjFacetDefinitionReaderTest {
    34
    35      private static final double TEST_EPS = 1e-10;
    36
    37      @Test
    38      void testDefaults() {
    39          // arrange
    40          final ObjFacetDefinitionReader reader = reader("");
    41
    42          // act/assert
    43          Assertions.assertFalse(reader.isFailOnNonPolygonKeywords());
    44      }
    45
    46      @Test
    47      void testClose() {
    48          // arrange
    49          final CloseCountReader closeReader = new CloseCountReader(new StringReader(""));
    50
    51          // act/assert
    52          try (ObjFacetDefinitionReader reader = new ObjFacetDefinitionReader(closeReader)) {
    53              Assertions.assertEquals(0, closeReader.getCloseCount());
    54          }
    55
    56          Assertions.assertEquals(1, closeReader.getCloseCount());
    57      }
    58
    59      @Test
    60      void testReadFacet_withNormal() {
    61          // arrange
    62          final ObjFacetDefinitionReader reader = reader(
    63                  "o test\n\n" +
    64                  "v 0 0 0\r\n" +
    65                  "v 1 0 0\n" +
    66                  "v 1 1 0\r" +
    67                  "v 0 1 0\n" +
    68                  "vn 0 0 -1\n" +
    69                  "f 1//1 2//1 3//1 4//1\n" +
    70                  "curv non-polygon data\n");
    71
    72          // act
    73          final List<FacetDefinition> facets = EuclideanIOTestUtils.readAll(reader);
    74
    75          // assert
    76          Assertions.assertEquals(1, facets.size());
    77          EuclideanIOTestUtils.assertFacetVertices(facets.get(0), Arrays.asList(
    78                      Vector3D.ZERO, Vector3D.of(1, 0, 0), Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0)
    79                  ), TEST_EPS);
    80          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_Z, facets.get(0).getNormal(), TEST_EPS);
    81      }
    82
    83      @Test
    84      void testReadFacet_withoutNormal() {
    85          // arrange
    86          final ObjFacetDefinitionReader reader = reader(
    87                  "o test\n\n" +
    88                  "v 0 0 0\r\n" +
    89                  "v 1 0 0\n" +
    90                  "v 1 1 0\r" +
    91                  "f 1 2 3\n");
    92
    93          // act
    94          final List<FacetDefinition> facets = EuclideanIOTestUtils.readAll(reader);
    95
    96          // assert
    97          Assertions.assertEquals(1, facets.size());
    98          EuclideanIOTestUtils.assertFacetVertices(facets.get(0), Arrays.asList(
    99                      Vector3D.ZERO, Vector3D.of(1, 0, 0), Vector3D.of(1, 1, 0)
    100                 ), TEST_EPS);
    101         Assertions.assertNull(facets.get(0).getNormal());
    102     }
    103
    104     @Test
    105     void testReadFacet_failOnNonPolygon() {
    106         // arrange
    107         final ObjFacetDefinitionReader reader = reader(
    108                 "o test\n\n" +
    109                 "v 0 0 0\r\n" +
    110                 "v 1 0 0\n" +
    111                 "v 1 1 0\r" +
    112                 "v 0 1 0\n" +
    113                 "vn 0 0 1\n" +
    114                 "f 1//1 2//1 3//1\n" +
    115                 "curv non-polygon data\n");
    116
    117         reader.setFailOnNonPolygonKeywords(true);
    118
    119         // act/assert
    120         GeometryTestUtils.assertThrowsWithMessage(
    121                 () -> EuclideanIOTestUtils.readAll(reader),
    122                 IllegalStateException.class, Pattern.compile("^Parsing failed.*"));
    123     }
    124
    125     private static ObjFacetDefinitionReader reader(final String str) {
    126         return new ObjFacetDefinitionReader(new StringReader(str));
    127     }
    128 }