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.Reader;
20 import java.util.Arrays;
21
22 import org.apache.commons.geometry.euclidean.threed.Vector3D;
23 import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
24 import org.apache.commons.geometry.io.core.internal.SimpleTextParser;
25 import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
26 import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
27 import org.apache.commons.geometry.io.euclidean.threed.SimpleFacetDefinition;
28
29
30
31
32 public class TextStlFacetDefinitionReader implements FacetDefinitionReader {
33
34
35 private Reader reader;
36
37
38 private SimpleTextParser parser;
39
40
41 private boolean foundSolidStart;
42
43
44 private boolean foundSolidEnd;
45
46
47 private String solidName;
48
49
50
51
52 public TextStlFacetDefinitionReader(final Reader reader) {
53 this.reader = reader;
54 this.parser = new SimpleTextParser(reader);
55 }
56
57
58
59
60
61
62 public String getSolidName() {
63 ensureSolidStarted();
64
65 return solidName;
66 }
67
68
69 @Override
70 public FacetDefinition readFacet() {
71 if (!foundSolidEnd && parser.hasMoreCharacters()) {
72 ensureSolidStarted();
73
74 nextWord();
75
76 int choice = parser.chooseIgnoreCase(
77 StlConstants.FACET_START_KEYWORD,
78 StlConstants.SOLID_END_KEYWORD);
79
80 if (choice == 0) {
81 return readFacetInternal();
82 } else {
83 foundSolidEnd = true;
84 }
85 }
86
87 return null;
88 }
89
90
91 @Override
92 public void close() {
93 GeometryIOUtils.closeUnchecked(reader);
94 }
95
96
97
98
99
100
101 private FacetDefinition readFacetInternal() {
102 matchKeyword(StlConstants.NORMAL_KEYWORD);
103 final Vector3D normal = readVector();
104
105 matchKeyword(StlConstants.OUTER_KEYWORD);
106 matchKeyword(StlConstants.LOOP_START_KEYWORD);
107
108 matchKeyword(StlConstants.VERTEX_KEYWORD);
109 final Vector3D p1 = readVector();
110
111 matchKeyword(StlConstants.VERTEX_KEYWORD);
112 final Vector3D p2 = readVector();
113
114 matchKeyword(StlConstants.VERTEX_KEYWORD);
115 final Vector3D p3 = readVector();
116
117 matchKeyword(StlConstants.LOOP_END_KEYWORD);
118 matchKeyword(StlConstants.FACET_END_KEYWORD);
119
120 return new SimpleFacetDefinition(Arrays.asList(p1, p2, p3), normal);
121 }
122
123
124
125
126
127
128 private void ensureSolidStarted() {
129 if (!foundSolidStart) {
130 beginSolid();
131
132 foundSolidStart = true;
133 }
134 }
135
136
137
138
139
140
141 private void beginSolid() {
142 matchKeyword(StlConstants.SOLID_START_KEYWORD);
143
144 solidName = trimmedOrNull(parser.nextLine()
145 .getCurrentToken());
146 }
147
148
149
150
151
152 private void nextWord() {
153 parser.discardWhitespace()
154 .nextAlphanumeric();
155 }
156
157
158
159
160
161
162 private void matchKeyword(final String keyword) {
163 nextWord();
164 parser.matchIgnoreCase(keyword);
165 }
166
167
168
169
170
171
172 private Vector3D readVector() {
173 final double x = readDouble();
174 final double y = readDouble();
175 final double z = readDouble();
176
177 return Vector3D.of(x, y, z);
178 }
179
180
181
182
183
184
185 private double readDouble() {
186 return parser
187 .discardWhitespace()
188 .next(SimpleTextParser::isDecimalPart)
189 .getCurrentTokenAsDouble();
190 }
191
192
193
194
195
196
197
198 private static String trimmedOrNull(final String str) {
199 if (str != null) {
200 final String trimmed = str.trim();
201 if (!trimmed.isEmpty()) {
202 return trimmed;
203 }
204 }
205
206 return null;
207 }
208 }