forked from fogleman/fauxgl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stl.go
189 lines (177 loc) · 4.14 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
package fauxgl
import (
"bufio"
"encoding/binary"
"io"
"math"
"os"
"runtime"
"strings"
)
type STLHeader struct {
_ [80]uint8
Count uint32
}
type STLTriangle struct {
N, V1, V2, V3 [3]float32
_ uint16
}
func LoadSTL(path string) (*Mesh, 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 loadSTLB(file)
} else {
return loadSTLA(file)
}
}
func loadSTLA(file *os.File) (*Mesh, error) {
var vertexes []Vector
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) == 4 && fields[0] == "vertex" {
f := ParseFloats(fields[1:])
vertexes = append(vertexes, Vector{f[0], f[1], f[2]})
}
}
var triangles []*Triangle
for i := 0; i < len(vertexes); i += 3 {
t := Triangle{}
t.V1.Position = vertexes[i+0]
t.V2.Position = vertexes[i+1]
t.V3.Position = vertexes[i+2]
t.FixNormals()
triangles = append(triangles, &t)
}
return NewTriangleMesh(triangles), scanner.Err()
}
func makeFloat(b []byte) float64 {
return float64(math.Float32frombits(binary.LittleEndian.Uint32(b)))
}
func loadSTLB(file *os.File) (*Mesh, error) {
r := bufio.NewReader(file)
header := STLHeader{}
if err := binary.Read(r, binary.LittleEndian, &header); err != nil {
return nil, err
}
count := int(header.Count)
triangles := make([]*Triangle, count)
_triangles := make([]Triangle, count)
b := make([]byte, count*50)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
wn := runtime.NumCPU()
ch := make(chan Box, wn)
for wi := 0; wi < wn; wi++ {
go func(wi, wn int) {
var min, max Vector
for i := wi; i < count; i += wn {
j := i * 50
v1 := Vector{makeFloat(b[j+12 : j+16]), makeFloat(b[j+16 : j+20]), makeFloat(b[j+20 : j+24])}
v2 := Vector{makeFloat(b[j+24 : j+28]), makeFloat(b[j+28 : j+32]), makeFloat(b[j+32 : j+36])}
v3 := Vector{makeFloat(b[j+36 : j+40]), makeFloat(b[j+40 : j+44]), makeFloat(b[j+44 : j+48])}
t := &_triangles[i]
t.V1.Position = v1
t.V2.Position = v2
t.V3.Position = v3
n := t.Normal()
t.V1.Normal = n
t.V2.Normal = n
t.V3.Normal = n
if i == wi {
min = v1
max = v1
}
for _, v := range [3]Vector{v1, v2, v3} {
if v.X < min.X {
min.X = v.X
}
if v.Y < min.Y {
min.Y = v.Y
}
if v.Z < min.Z {
min.Z = v.Z
}
if v.X > max.X {
max.X = v.X
}
if v.Y > max.Y {
max.Y = v.Y
}
if v.Z > max.Z {
max.Z = v.Z
}
}
triangles[i] = t
}
ch <- Box{min, max}
}(wi, wn)
}
box := EmptyBox
for wi := 0; wi < wn; wi++ {
box = box.Extend(<-ch)
}
mesh := NewTriangleMesh(triangles)
mesh.box = &box
return mesh, nil
}
func SaveSTL(path string, mesh *Mesh) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
header := STLHeader{}
header.Count = uint32(len(mesh.Triangles))
if err := binary.Write(w, binary.LittleEndian, &header); err != nil {
return err
}
for _, triangle := range mesh.Triangles {
n := triangle.Normal()
d := STLTriangle{}
d.N[0] = float32(n.X)
d.N[1] = float32(n.Y)
d.N[2] = float32(n.Z)
d.V1[0] = float32(triangle.V1.Position.X)
d.V1[1] = float32(triangle.V1.Position.Y)
d.V1[2] = float32(triangle.V1.Position.Z)
d.V2[0] = float32(triangle.V2.Position.X)
d.V2[1] = float32(triangle.V2.Position.Y)
d.V2[2] = float32(triangle.V2.Position.Z)
d.V3[0] = float32(triangle.V3.Position.X)
d.V3[1] = float32(triangle.V3.Position.Y)
d.V3[2] = float32(triangle.V3.Position.Z)
if err := binary.Write(w, binary.LittleEndian, &d); err != nil {
return err
}
}
w.Flush()
return nil
}