1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
46 List<ModelIOHandler> handlers = registry.getHandlers();
47
48
49 Assert.assertEquals(1, handlers.size());
50 }
51
52 @Test
53 public void testSupportedTypes() {
54
55 Assert.assertTrue(registry.handlesType("obj"));
56 Assert.assertTrue(registry.handlesType("OBJ"));
57 }
58
59 @Test
60 public void testReadWrite_supportedTypes() {
61
62 checkWriteRead("obj");
63 }
64
65 private void checkWriteRead(String type) {
66
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
74 registry.write(model, type, out);
75 BoundarySource3D result = registry.read(type, new ByteArrayInputStream(out.toByteArray()), TEST_PRECISION);
76
77
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 }