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;
    18
    19  import java.nio.file.Path;
    20  import java.nio.file.Paths;
    21  import java.util.stream.Stream;
    22
    23  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
    24  import org.apache.commons.geometry.euclidean.threed.AffineTransformMatrix3D;
    25  import org.apache.commons.geometry.euclidean.threed.RegionBSPTree3D;
    26  import org.apache.commons.geometry.euclidean.threed.Triangle3D;
    27  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    28  import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh;
    29  import org.apache.commons.geometry.euclidean.threed.shape.Parallelepiped;
    30  import org.apache.commons.geometry.euclidean.threed.shape.Sphere;
    31  import org.apache.commons.geometry.io.core.input.FileGeometryInput;
    32  import org.apache.commons.geometry.io.core.input.GeometryInput;
    33  import org.apache.commons.geometry.io.core.output.FileGeometryOutput;
    34  import org.apache.commons.geometry.io.core.output.GeometryOutput;
    35  import org.apache.commons.geometry.io.euclidean.threed.IO3D;
    36  import org.apache.commons.numbers.core.Precision;
    37  import org.junit.jupiter.api.Assertions;
    38  import org.junit.jupiter.api.Test;
    39  import org.junit.jupiter.api.io.TempDir;
    40
    41  class DocumentationExamplesTest {
    42
    43      private static final double TEST_EPS = 1e-12;
    44
    45      @TempDir
    46      Path tempDir;
    47
    48      @Test
    49      void testIndexPageExample() {
    50          // construct a precision instance to handle floating-point comparisons
    51          final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
    52
    53          // create a BSP tree representing the unit cube
    54          final RegionBSPTree3D tree = Parallelepiped.unitCube(precision).toTree();
    55
    56          // create a sphere centered on the origin
    57          final Sphere sphere = Sphere.from(Vector3D.ZERO, 0.65, precision);
    58
    59          // subtract a BSP tree approximation of the sphere containing 512 facets
    60          // from the cube, modifying the cube tree in place
    61          tree.difference(sphere.toTree(3));
    62
    63          // compute some properties of the resulting region
    64          final double size = tree.getSize(); // 0.11509505362599505
    65          final Vector3D centroid = tree.getCentroid(); // (0, 0, 0)
    66
    67          // convert to a triangle mesh
    68          final TriangleMesh mesh = tree.toTriangleMesh(precision);
    69
    70          // save as an OBJ file
    71          IO3D.write(mesh, Paths.get("target/cube-minus-sphere.obj"));
    72
    73          // -----------
    74          Assertions.assertEquals(0.11509505362599505, size, TEST_EPS);
    75          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.ZERO, centroid, TEST_EPS);
    76      }
    77
    78      @Test
    79      void testIO3DExample() throws Exception {
    80
    81          final Path inputPath = Paths.get(EuclideanIOTestUtils.resource("/models/cube.obj").toURI());
    82          final Path outputPath = tempDir.resolve("scaled.csv");
    83
    84          final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
    85
    86          final GeometryInput input = new FileGeometryInput(inputPath);
    87          final GeometryOutput scaledOutput = new FileGeometryOutput(outputPath);
    88          final AffineTransformMatrix3D transform = AffineTransformMatrix3D.createScale(2);
    89
    90          // Use the input triangle stream in a try-with-resources statement to ensure
    91          // all resources are properly released.
    92          try (Stream<Triangle3D> stream = IO3D.triangles(input, null, precision)) {
    93              IO3D.write(stream.map(t -> t.transform(transform)), scaledOutput, null);
    94          }
    95
    96          // -------------
    97          final RegionBSPTree3D result = IO3D.read(outputPath, precision).toTree();
    98
    99          Assertions.assertEquals(8.0, result.getSize(), TEST_EPS);
    100
    101     }
    102 }