forked from hschendel/stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadascii.go
264 lines (240 loc) · 5.83 KB
/
readascii.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package stl
// This file defines a parser for the STL ASCII format.
import (
"bufio"
"bytes"
"container/list"
"errors"
"fmt"
"io"
"regexp"
"strconv"
)
func readAllAscii(r io.Reader) (solid *Solid, err error) {
var sd Solid
p := newParser(r)
if p.Parse(&sd) {
solid = &sd
} else {
err = errors.New(p.ErrorText)
}
return
}
type parser struct {
line int
errors *list.List
currentWord string
currentLine []byte
eof bool
lineScanner *bufio.Scanner
wordScanner *bufio.Scanner
HeaderError bool
TrianglesSkipped bool
ErrorText string
}
func newParser(reader io.Reader) *parser {
var p parser
p.errors = list.New()
p.eof = false
p.lineScanner = bufio.NewScanner(reader)
p.nextLine()
return &p
}
func (p *parser) addError(msg string) {
p.errors.PushBack(fmt.Sprintf("%d: %s", p.line, msg))
}
const (
idNone = 0
idSolid = 1 << iota
idFacet
idNormal
idOuter
idLoop
idVertex
idEndloop
idEndfacet
idEndsolid
)
var identRegexps = map[int]*regexp.Regexp{
idSolid: regexp.MustCompile("^solid$"),
idFacet: regexp.MustCompile("^facet$"),
idNormal: regexp.MustCompile("^normal$"),
idOuter: regexp.MustCompile("^outer$"),
idLoop: regexp.MustCompile("^loop$"),
idVertex: regexp.MustCompile("^vertex$"),
idEndloop: regexp.MustCompile("^endloop$"),
idEndfacet: regexp.MustCompile("^endfacet$"),
idEndsolid: regexp.MustCompile("^endsolid$"),
(idFacet | idEndsolid): regexp.MustCompile(`^(facet|endsolid)$`),
}
var reFloat = regexp.MustCompile(`^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$`)
var idents = map[int]string{
idSolid: "solid",
idFacet: "facet",
idNormal: "normal",
idOuter: "outer",
idLoop: "loop",
idVertex: "vertex",
idEndloop: "endloop",
idEndfacet: "endfacet",
idEndsolid: "endsolit",
}
func (p *parser) Parse(solid *Solid) bool {
if p.eof {
p.HeaderError = true
p.addError("File is empty")
} else {
p.HeaderError = !p.parseAsciiHeaderLine(solid)
triangles := list.New()
TriangleLoop:
for !p.eof && !p.isCurrentTokenIdent(idEndsolid) {
if !p.isCurrentTokenIdent(idFacet) {
p.addError(`"facet" or "endsolid" expected`)
switch p.skipToToken(idFacet | idEndsolid) {
case idEndsolid, idNone:
break TriangleLoop
}
}
var t Triangle
if p.parseFacet(&t) {
triangles.PushBack(&t)
} else {
p.TrianglesSkipped = true
p.skipToToken(idFacet | idEndsolid)
}
}
solid.Triangles = make([]Triangle, triangles.Len())
for i, e := 0, triangles.Front(); e != nil; e = e.Next() {
solid.Triangles[i] = *((e.Value).(*Triangle))
i++
}
}
success := !p.HeaderError && !p.TrianglesSkipped && p.consumeToken(idEndsolid)
p.generateErrorText()
return success
}
func (p *parser) generateErrorText() {
var buf bytes.Buffer
if p.TrianglesSkipped {
buf.WriteString("Triangles had to be skipped.\n")
}
for e := p.errors.Front(); e != nil; e = e.Next() {
buf.WriteString(e.Value.(string))
buf.WriteString("\n")
}
p.ErrorText = buf.String()
}
// assumes that the first 6 chars "solid " have already bin read
func (p *parser) parseAsciiHeaderLine(solid *Solid) bool {
var success bool
if p.eof {
p.addError("Unexpected end of file")
success = false
} else {
solid.Name = extractAsciiString(p.currentLine)
success = true
}
p.nextLine()
return success
}
func (p *parser) parseFacet(t *Triangle) bool {
return p.consumeToken(idFacet) &&
p.consumeToken(idNormal) && p.parsePoint(&(t.Normal)) &&
p.consumeToken(idOuter) && p.consumeToken(idLoop) &&
p.consumeToken(idVertex) && p.parsePoint(&(t.Vertices[0])) &&
p.consumeToken(idVertex) && p.parsePoint(&(t.Vertices[1])) &&
p.consumeToken(idVertex) && p.parsePoint(&(t.Vertices[2])) &&
p.consumeToken(idEndloop) &&
p.consumeToken(idEndfacet)
}
func (p *parser) parsePoint(pt *Vec3) bool {
return p.parseFloat32(&(pt[0])) &&
p.parseFloat32(&(pt[1])) &&
p.parseFloat32(&(pt[2]))
}
func (p *parser) parseFloat32(f *float32) bool {
if p.eof {
return false
}
f64, err := strconv.ParseFloat(p.currentWord, 32)
if err != nil {
p.addError("Unable to parse float")
return false
} else {
*f = float32(f64)
p.nextWord()
return true
}
}
func (p *parser) isCurrentTokenIdent(ident int) bool {
re := identRegexps[ident]
return re.MatchString(p.currentWord)
}
func (p *parser) skipToToken(ident int) int {
re := identRegexps[ident]
for { // terminates when no more next words are there, or ident has been found
if re.MatchString(p.currentWord) {
if ident == (idFacet | idEndsolid) {
if identRegexps[idFacet].MatchString(p.currentWord) {
return idFacet
} else {
return idEndsolid
}
} else {
return ident
}
} else {
if !p.nextWord() {
return idNone
}
}
}
}
func (p *parser) consumeToken(ident int) bool {
re := identRegexps[ident]
if re.MatchString(p.currentWord) {
p.nextWord()
return true
} else {
ident := idents[ident]
p.addError("\"" + ident + "\" expected")
return false
}
}
func (p *parser) nextWord() bool {
if p.eof {
return false
}
// Try to advance word scanner
if p.wordScanner.Scan() {
p.currentWord = p.wordScanner.Text()
return true
} else {
if p.wordScanner.Err() == nil { // line has ended
return p.nextLine()
} else {
p.addError(p.wordScanner.Err().Error())
p.currentLine = nil
p.currentWord = ""
p.eof = true
return false
}
}
}
func (p *parser) nextLine() bool {
if p.lineScanner.Scan() {
p.currentLine = p.lineScanner.Bytes()
p.line++
p.wordScanner = bufio.NewScanner(bytes.NewReader(p.currentLine))
p.wordScanner.Split(bufio.ScanWords)
return p.nextWord()
} else {
if p.lineScanner.Err() != nil {
p.addError(p.lineScanner.Err().Error())
}
p.currentLine = nil
p.currentWord = ""
p.eof = true
return false
}
}