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.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.util.List;
22  import java.util.stream.Collectors;
23  
24  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
25  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
26  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
27  import org.apache.commons.geometry.euclidean.threed.BoundarySource3D;
28  import org.apache.commons.geometry.euclidean.threed.Planes;
29  import org.apache.commons.geometry.euclidean.threed.Triangle3D;
30  import org.apache.commons.geometry.euclidean.threed.Vector3D;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  public class DefaultModelIOHandlerRegistryTest {
35  
36      private static final double TEST_EPS = 1e-10;
37  
38      private static final DoublePrecisionContext TEST_PRECISION =
39              new EpsilonDoublePrecisionContext(TEST_EPS);
40  
41      private DefaultModelIOHandlerRegistry registry = new DefaultModelIOHandlerRegistry();
42  
43      @Test
44      public void testDefaultHandlers() {
45          // act
46          List<ModelIOHandler> handlers = registry.getHandlers();
47  
48          // assert
49          Assert.assertEquals(1, handlers.size());
50      }
51  
52      @Test
53      public void testSupportedTypes() {
54          // act/assert
55          Assert.assertTrue(registry.handlesType("obj"));
56          Assert.assertTrue(registry.handlesType("OBJ"));
57      }
58  
59      @Test
60      public void testReadWrite_supportedTypes() {
61          // act/assert
62          checkWriteRead("obj");
63      }
64  
65      private void checkWriteRead(String type) {
66          // arrange
67          BoundarySource3D model = BoundarySource3D.from(
68                  Planes.triangleFromVertices(Vector3D.ZERO, Vector3D.of(1, 0, 0), Vector3D.of(0, 1, 0), TEST_PRECISION)
69              );
70  
71          ByteArrayOutputStream out = new ByteArrayOutputStream();
72  
73          // act
74          registry.write(model, type, out);
75          BoundarySource3D result = registry.read(type, new ByteArrayInputStream(out.toByteArray()), TEST_PRECISION);
76  
77          // assert
78          List<Triangle3D> tris = result.triangleStream().collect(Collectors.toList());
79          Assert.assertEquals(1, tris.size());
80  
81          Triangle3D tri = tris.get(0);
82          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.ZERO, tri.getPoint1(), TEST_EPS);
83          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.of(1, 0, 0), tri.getPoint2(), TEST_EPS);
84          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.of(0, 1, 0), tri.getPoint3(), TEST_EPS);
85      }
86  }