forked from hschendel/stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writebinary.go
66 lines (57 loc) · 1.6 KB
/
writebinary.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
package stl
// This file defines functions to write a Solid into the STL binary format.
import (
"encoding/binary"
"io"
"math"
)
// Write solid in binary STL into an io.Writer.
// Does not check whether len(solid.Triangles) fits into uint32.
func writeSolidBinary(w io.Writer, solid *Solid) error {
headerBuf := make([]byte, 84)
if solid.BinaryHeader == nil {
// use name if no binary header set
copy(headerBuf, []byte(solid.Name))
} else {
copy(headerBuf, solid.BinaryHeader)
}
// Write triangle count
binary.LittleEndian.PutUint32(headerBuf[80:84], uint32(len(solid.Triangles)))
_, errHeader := w.Write(headerBuf)
if errHeader != nil {
return errHeader
}
// Write each triangle
for _, t := range solid.Triangles {
tErr := writeTriangleBinary(w, &t)
if tErr != nil {
return tErr
}
}
return nil
}
func writeTriangleBinary(w io.Writer, t *Triangle) error {
buf := make([]byte, 50)
offset := 0
encodePoint(buf, &offset, &t.Normal)
encodePoint(buf, &offset, &t.Vertices[0])
encodePoint(buf, &offset, &t.Vertices[1])
encodePoint(buf, &offset, &t.Vertices[2])
encodeUint16(buf, &offset, t.Attributes)
_, err := w.Write(buf)
return err
}
func encodePoint(buf []byte, offset *int, pt *Vec3) {
encodeFloat32(buf, offset, pt[0])
encodeFloat32(buf, offset, pt[1])
encodeFloat32(buf, offset, pt[2])
}
func encodeFloat32(buf []byte, offset *int, f float32) {
u32 := math.Float32bits(f)
binary.LittleEndian.PutUint32(buf[*offset:(*offset)+4], u32)
(*offset) += 4
}
func encodeUint16(buf []byte, offset *int, u uint16) {
binary.LittleEndian.PutUint16(buf[*offset:(*offset)+2], u)
(*offset) += 2
}