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.examples.io.threed;
    18
    19  import java.io.File;
    20  import java.io.IOException;
    21  import java.io.InputStream;
    22  import java.io.OutputStream;
    23  import java.nio.file.Files;
    24  import java.util.List;
    25  import java.util.stream.Collectors;
    26
    27  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
    28  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
    29  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    30  import org.apache.commons.geometry.euclidean.threed.BoundarySource3D;
    31  import org.apache.commons.geometry.euclidean.threed.Planes;
    32  import org.apache.commons.geometry.euclidean.threed.Triangle3D;
    33  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    34  import org.junit.Assert;
    35  import org.junit.Rule;
    36  import org.junit.Test;
    37  import org.junit.rules.TemporaryFolder;
    38
    39  public class ModelIOTest {
    40
    41      private static final double TEST_EPS = 1e-10;
    42
    43      private static final DoublePrecisionContext TEST_PRECISION =
    44              new EpsilonDoublePrecisionContext(TEST_EPS);
    45
    46      @Rule
    47      public TemporaryFolder tempFolder = new TemporaryFolder();
    48
    49      @Test
    50      public void testGetHandler() {
    51          // act
    52          ModelIOHandlerRegistry registry = ModelIO.getModelIOHandlerRegistry();
    53
    54          // assert
    55          Assert.assertTrue(registry instanceof DefaultModelIOHandlerRegistry);
    56          Assert.assertSame(registry, ModelIO.getModelIOHandlerRegistry());
    57      }
    58
    59      @Test
    60      public void testWriteRead_typeFromFileExtension() throws IOException {
    61          // act/assert
    62          checkWriteRead(model -> {
    63              File file = new File(tempFolder.getRoot(), "model.obj");
    64
    65              ModelIO.write(model, file);
    66              return ModelIO.read(file, TEST_PRECISION);
    67          });
    68      }
    69
    70      @Test
    71      public void testWriteRead_typeAndFile() throws IOException {
    72          // act/assert
    73          checkWriteRead(model -> {
    74              File file = new File(tempFolder.getRoot(), "objmodel");
    75
    76              ModelIO.write(model, "OBJ", file);
    77              return ModelIO.read("obj", file, TEST_PRECISION);
    78          });
    79      }
    80
    81      @Test
    82      public void testWriteRead_typeAndStream() throws IOException {
    83          // act/assert
    84          checkWriteRead(model -> {
    85              File file = new File(tempFolder.getRoot(), "objmodel");
    86
    87              try (OutputStream out = Files.newOutputStream(file.toPath())) {
    88                  ModelIO.write(model, "OBJ", out);
    89              }
    90
    91              try (InputStream in = Files.newInputStream(file.toPath())) {
    92                  return ModelIO.read("OBJ", in, TEST_PRECISION);
    93              }
    94          });
    95      }
    96
    97      @FunctionalInterface
    98      private interface ModelIOFunction {
    99          BoundarySource3D apply(BoundarySource3D model) throws IOException;
    100     }
    101
    102     private void checkWriteRead(ModelIOFunction fn) throws IOException {
    103         // arrange
    104         BoundarySource3D model = BoundarySource3D.from(
    105                 Planes.triangleFromVertices(Vector3D.ZERO, Vector3D.of(1, 0, 0), Vector3D.of(0, 1, 0), TEST_PRECISION)
    106             );
    107
    108         // act
    109         BoundarySource3D result = fn.apply(model);
    110
    111         // assert
    112         List<Triangle3D> tris = result.triangleStream().collect(Collectors.toList());
    113         Assert.assertEquals(1, tris.size());
    114
    115         Triangle3D tri = tris.get(0);
    116         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.ZERO, tri.getPoint1(), TEST_EPS);
    117         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.of(1, 0, 0), tri.getPoint2(), TEST_EPS);
    118         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.of(0, 1, 0), tri.getPoint3(), TEST_EPS);
    119     }
    120 }