1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.io.euclidean.threed.stl;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.nio.charset.StandardCharsets;
23 import java.util.stream.Collectors;
24
25 import org.apache.commons.geometry.core.GeometryTestUtils;
26 import org.apache.commons.geometry.euclidean.threed.BoundaryList3D;
27 import org.apache.commons.geometry.io.euclidean.EuclideanIOTestUtils;
28 import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
29 import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitions;
30 import org.apache.commons.numbers.core.Precision;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.Test;
33
34 class StlFacetDefinitionReadersTest {
35
36 private static final double TEST_EPS = 1e-10;
37
38 private static final Precision.DoubleEquivalence TEST_PRECISION =
39 Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
40
41 @Test
42 void testCreate_cubeBinaryFile() throws IOException {
43
44 final URL url = EuclideanIOTestUtils.resource("/models/cube-binary.stl");
45
46
47 try (FacetDefinitionReader reader = StlFacetDefinitionReaders.create(url.openStream(), null)) {
48
49
50 Assertions.assertEquals(BinaryStlFacetDefinitionReader.class, reader.getClass());
51
52 BoundaryList3D boundaries = new BoundaryList3D(EuclideanIOTestUtils.readAll(reader).stream()
53 .map(f -> FacetDefinitions.toPolygon(f, TEST_PRECISION))
54 .collect(Collectors.toList()));
55 EuclideanIOTestUtils.assertCube(boundaries, TEST_EPS);
56 }
57 }
58
59 @Test
60 void testCreate_cubeAsciiFile() throws IOException {
61
62 final URL url = EuclideanIOTestUtils.resource("/models/cube-ascii.stl");
63
64
65 try (FacetDefinitionReader reader = StlFacetDefinitionReaders.create(url.openStream(), null)) {
66
67
68 Assertions.assertEquals(TextStlFacetDefinitionReader.class, reader.getClass());
69
70 BoundaryList3D boundaries = new BoundaryList3D(EuclideanIOTestUtils.readAll(reader).stream()
71 .map(f -> FacetDefinitions.toPolygon(f, TEST_PRECISION))
72 .collect(Collectors.toList()));
73 EuclideanIOTestUtils.assertCube(boundaries, TEST_EPS);
74 }
75 }
76
77 @Test
78 void testCreate_nonStandardCharset_charsetGiven() {
79
80 final String content = "solid test\n" +
81 "facet normal 1 2 3 " +
82 "outer loop " +
83 "vertex 4 5 6 " +
84 "vertex 7 8 9 " +
85 "vertex 10 11 12 " +
86 "endloop " +
87 "endfacet " +
88 "endsolid test";
89
90 final ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_16));
91
92
93 try (FacetDefinitionReader reader = StlFacetDefinitionReaders.create(in, StandardCharsets.UTF_16)) {
94
95
96 Assertions.assertEquals(TextStlFacetDefinitionReader.class, reader.getClass());
97
98 Assertions.assertNotNull(reader.readFacet());
99 Assertions.assertNull(reader.readFacet());
100 }
101 }
102
103 @Test
104 void testCreate_nonStandardCharset_noCharsetGiven() {
105
106 final String content = "solid test\n" +
107 "facet normal 1 2 3 " +
108 "outer loop " +
109 "vertex 4 5 6 " +
110 "vertex 7 8 9 " +
111 "vertex 10 11 12 " +
112 "endloop " +
113 "endfacet " +
114 "endsolid test";
115
116 final ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_16));
117
118
119 try (FacetDefinitionReader reader = StlFacetDefinitionReaders.create(in, null)) {
120
121
122 Assertions.assertEquals(BinaryStlFacetDefinitionReader.class, reader.getClass());
123
124 Assertions.assertNotNull(reader.readFacet());
125 Assertions.assertNotNull(reader.readFacet());
126 Assertions.assertThrows(IllegalStateException.class, () -> reader.readFacet());
127 }
128 }
129
130 @Test
131 void testCreate_notEnoughBytes() {
132
133 final byte[] bytes = new byte[1];
134 final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
135
136
137 GeometryTestUtils.assertThrowsWithMessage(
138 () -> StlFacetDefinitionReaders.create(in, null),
139 IllegalStateException.class,
140 "Cannot determine STL format: attempted to read 5 bytes but found only 1 available");
141 }
142 }