-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstl.go
140 lines (130 loc) · 2.7 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
package meshview
import (
"bufio"
"encoding/binary"
"io"
"math"
"os"
"runtime"
"strconv"
"strings"
"sync"
)
func LoadSTL(path string) (*MeshData, 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
}
// read header, get expected binary size
type STLHeader struct {
_ [80]uint8
Count uint32
}
header := STLHeader{}
if err := binary.Read(file, binary.LittleEndian, &header); err != nil {
return nil, err
}
expectedSize := int64(header.Count)*50 + 84
// parse ascii or binary stl
if info.Size() == expectedSize {
return loadSTLB(file, int(header.Count))
} else {
// rewind to start of file
_, err = file.Seek(0, 0)
if err != nil {
return nil, err
}
return loadSTLA(file)
}
}
func loadSTLA(file *os.File) (*MeshData, error) {
var data []float32
var x1, y1, z1, x2, y2, z2, x3, y3, z3 float32
i := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) < 12 || line[0] != 'v' {
continue
}
fields := strings.Fields(line[7:])
if len(fields) != 3 {
continue
}
x, _ := strconv.ParseFloat(fields[0], 32)
y, _ := strconv.ParseFloat(fields[1], 32)
z, _ := strconv.ParseFloat(fields[2], 32)
switch i % 3 {
case 0:
x1 = float32(x)
y1 = float32(y)
z1 = float32(z)
case 1:
x2 = float32(x)
y2 = float32(y)
z2 = float32(z)
case 2:
x3 = float32(x)
y3 = float32(y)
z3 = float32(z)
data = append(data, []float32{x1, y1, z1, x2, y2, z2, x3, y3, z3}...)
}
i++
}
box := boxForData(data)
return &MeshData{data, box}, scanner.Err()
}
func makeFloat(b []byte) float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(b))
}
func loadSTLB(file *os.File, count int) (*MeshData, error) {
buf := make([]byte, count*50)
_, err := io.ReadFull(file, buf)
if err != nil {
return nil, err
}
data := make([]float32, count*9)
wn := runtime.NumCPU() - 1
if wn < 1 {
wn = 1
}
var wg sync.WaitGroup
for wi := 0; wi < wn; wi++ {
wg.Add(1)
go func(wi int) {
n := count / wn
if count%wn > 0 {
n++
}
i0 := n * wi
i1 := i0 + n
if i1 > count {
i1 = count
}
for i := i0; i < i1; i++ {
j := i * 9
b := buf[i*50+12:]
data[j+0] = makeFloat(b[0:])
data[j+1] = makeFloat(b[4:])
data[j+2] = makeFloat(b[8:])
data[j+3] = makeFloat(b[12:])
data[j+4] = makeFloat(b[16:])
data[j+5] = makeFloat(b[20:])
data[j+6] = makeFloat(b[24:])
data[j+7] = makeFloat(b[28:])
data[j+8] = makeFloat(b[32:])
}
wg.Done()
}(wi)
}
wg.Wait()
box := boxForData(data)
return &MeshData{data, box}, nil
}