/
stl.go
246 lines (213 loc) · 6.17 KB
/
stl.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
//-----------------------------------------------------------------------------
/*
STL Load/Save
*/
//-----------------------------------------------------------------------------
package render
import (
"bufio"
"encoding/binary"
"fmt"
"os"
"strconv"
"strings"
"sync"
"github.com/deadsy/sdfx/sdf"
v3 "github.com/deadsy/sdfx/vec/v3"
)
//-----------------------------------------------------------------------------
// STLHeader defines the STL file header.
type STLHeader struct {
_ [80]uint8 // Header
Count uint32 // Number of triangles
}
// STLTriangle defines the triangle data within an STL file.
type STLTriangle struct {
Normal, Vertex1, Vertex2, Vertex3 [3]float32
_ uint16 // Attribute byte count
}
//-----------------------------------------------------------------------------
// parseFloats converts float value strings to []float64.
func parseFloats(in []string) ([]float64, error) {
out := make([]float64, len(in))
for i := range in {
val, err := strconv.ParseFloat(in[i], 64)
if err != nil {
return nil, err
}
out[i] = val
}
return out, nil
}
// loadSTLAscii loads an STL file created in ASCII format.
func loadSTLAscii(file *os.File) ([]*sdf.Triangle3, error) {
var v []v3.Vec
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) == 4 && fields[0] == "vertex" {
f, err := parseFloats(fields[1:])
if err != nil {
return nil, err
}
v = append(v, v3.Vec{f[0], f[1], f[2]})
}
}
// make triangles out of every 3 vertices
var mesh []*sdf.Triangle3
for i := 0; i < len(v); i += 3 {
mesh = append(mesh, &sdf.Triangle3{v[i+0], v[i+1], v[i+2]})
}
return mesh, scanner.Err()
}
// loadSTLBinary loads an STL file created in binary format.
func loadSTLBinary(file *os.File) ([]*sdf.Triangle3, error) {
r := bufio.NewReader(file)
header := STLHeader{}
if err := binary.Read(r, binary.LittleEndian, &header); err != nil {
return nil, err
}
mesh := make([]*sdf.Triangle3, int(header.Count))
for i := range mesh {
d := STLTriangle{}
if err := binary.Read(r, binary.LittleEndian, &d); err != nil {
return nil, err
}
v1 := v3.Vec{float64(d.Vertex1[0]), float64(d.Vertex1[1]), float64(d.Vertex1[2])}
v2 := v3.Vec{float64(d.Vertex2[0]), float64(d.Vertex2[1]), float64(d.Vertex2[2])}
v3 := v3.Vec{float64(d.Vertex3[0]), float64(d.Vertex3[1]), float64(d.Vertex3[2])}
mesh[i] = &sdf.Triangle3{v1, v2, v3}
}
return mesh, nil
}
// LoadSTL loads an STL file (ascii or binary) and returns the triangle mesh.
func LoadSTL(path string) ([]*sdf.Triangle3, error) {
// open file
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
// get file size
info, err := file.Stat()
if err != nil {
return nil, err
}
size := info.Size()
// read header, get expected binary size
header := STLHeader{}
if err := binary.Read(file, binary.LittleEndian, &header); err != nil {
return nil, err
}
expectedSize := int64(header.Count)*50 + 84
// rewind to start of file
_, err = file.Seek(0, 0)
if err != nil {
return nil, err
}
// parse ascii or binary stl
if size == expectedSize {
return loadSTLBinary(file)
}
return loadSTLAscii(file)
}
//-----------------------------------------------------------------------------
// SaveSTL writes a triangle mesh to an STL file.
func SaveSTL(path string, mesh []*sdf.Triangle3) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
buf := bufio.NewWriter(file)
header := STLHeader{}
header.Count = uint32(len(mesh))
if err := binary.Write(buf, binary.LittleEndian, &header); err != nil {
return err
}
var d STLTriangle
for _, triangle := range mesh {
n := triangle.Normal()
d.Normal[0] = float32(n.X)
d.Normal[1] = float32(n.Y)
d.Normal[2] = float32(n.Z)
d.Vertex1[0] = float32(triangle[0].X)
d.Vertex1[1] = float32(triangle[0].Y)
d.Vertex1[2] = float32(triangle[0].Z)
d.Vertex2[0] = float32(triangle[1].X)
d.Vertex2[1] = float32(triangle[1].Y)
d.Vertex2[2] = float32(triangle[1].Z)
d.Vertex3[0] = float32(triangle[2].X)
d.Vertex3[1] = float32(triangle[2].Y)
d.Vertex3[2] = float32(triangle[2].Z)
if err := binary.Write(buf, binary.LittleEndian, &d); err != nil {
return err
}
}
return buf.Flush()
}
//-----------------------------------------------------------------------------
// writeSTL writes a stream of triangles to an STL file.
func writeSTL(wg *sync.WaitGroup, path string) (chan<- []*sdf.Triangle3, error) {
f, err := os.Create(path)
if err != nil {
return nil, err
}
// Use buffered IO for optimal IO writes.
// The default buffer size doesn't appear to limit performance.
buf := bufio.NewWriter(f)
// write an empty header
hdr := STLHeader{}
if err := binary.Write(buf, binary.LittleEndian, &hdr); err != nil {
return nil, err
}
// External code writes triangles to this channel.
// This goroutine reads the channel and writes triangles to the file.
c := make(chan []*sdf.Triangle3)
wg.Add(1)
go func() {
defer wg.Done()
defer f.Close()
var count uint32
var d STLTriangle
// read triangles from the channel and write them to the file
for ts := range c {
for _, t := range ts {
n := t.Normal()
d.Normal[0] = float32(n.X)
d.Normal[1] = float32(n.Y)
d.Normal[2] = float32(n.Z)
d.Vertex1[0] = float32(t[0].X)
d.Vertex1[1] = float32(t[0].Y)
d.Vertex1[2] = float32(t[0].Z)
d.Vertex2[0] = float32(t[1].X)
d.Vertex2[1] = float32(t[1].Y)
d.Vertex2[2] = float32(t[1].Z)
d.Vertex3[0] = float32(t[2].X)
d.Vertex3[1] = float32(t[2].Y)
d.Vertex3[2] = float32(t[2].Z)
if err := binary.Write(buf, binary.LittleEndian, &d); err != nil {
fmt.Printf("%s\n", err)
return
}
count++
}
}
// flush the triangles
buf.Flush()
// back to the start of the file
if _, err := f.Seek(0, 0); err != nil {
fmt.Printf("%s\n", err)
return
}
// rewrite the header with the correct mesh count
hdr.Count = count
if err := binary.Write(f, binary.LittleEndian, &hdr); err != nil {
fmt.Printf("%s\n", err)
return
}
}()
return c, nil
}
//-----------------------------------------------------------------------------