-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
176 lines (164 loc) · 4.79 KB
/
read.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
package binvox
import (
"bufio"
"fmt"
"io"
"log"
"os"
"regexp"
"strconv"
)
var (
dimRE = regexp.MustCompile(`^dim (\d+) (\d+) (\d+)\s*$`)
translateRE = regexp.MustCompile(`^translate (\S+) (\S+) (\S+)\s*$`)
scaleRE = regexp.MustCompile(`^scale (\S+)\s*$`)
)
// Read reads a binvox file and returns a BinVOX using WhiteVoxels.
// sx, sy, sz are the starting indices for reading a model.
// nx, ny, nz are the number of voxels to read in each direction (0=all).
func Read(filename string, sx, sy, sz, nx, ny, nz int) (*BinVOX, error) {
log.Printf("Loading file %q...", filename)
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("unable to open file %q: %v", filename, err)
}
defer f.Close()
binVOX, err := read(f, sx, sy, sz, nx, ny, nz)
if err != nil {
return nil, fmt.Errorf("Read(%q): %v", filename, err)
}
log.Printf("Done loading %v voxels from file %q.", len(binVOX.WhiteVoxels), filename)
return binVOX, nil
}
func read(r io.Reader, sx, sy, sz, cx, cy, cz int) (*BinVOX, error) {
if sx < 0 || sy < 0 || sz < 0 || cx < 0 || cy < 0 || cz < 0 {
return nil, fmt.Errorf("invalid parameters: start=(%v,%v,%v) count=(%v,%v,%v)", sx, sy, sz, cx, cy, cz)
}
b := bufio.NewReader(r)
header, err := b.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("could not read header: %v", err)
}
if header != "#binvox 1\n" {
return nil, fmt.Errorf("not a binvox file: %v", header)
}
dim, err := b.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("could not read dimensions: %v", err)
}
parts := dimRE.FindStringSubmatch(dim)
if len(parts) != 4 {
return nil, fmt.Errorf("unable to parse dimensions: %v", dim)
}
nx, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("unable to parse dimensions: %v", dim)
}
ny, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("unable to parse dimensions: %v", dim)
}
nz, err := strconv.Atoi(parts[3])
if err != nil {
return nil, fmt.Errorf("unable to parse dimensions: %v", dim)
}
if sx > nx || sy > ny || sz > nz {
return nil, fmt.Errorf("invalid parameters: start=(%v,%v,%v) model dim=(%v,%v,%v)", sx, sy, sz, nx, ny, nz)
}
if cx == 0 || cx > nx {
cx = nx
}
if cy == 0 || cy > ny {
cy = ny
}
if cz == 0 || cz > nz {
cz = nz
}
translate, err := b.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("could not read translate: %v", err)
}
parts = translateRE.FindStringSubmatch(translate)
if len(parts) != 4 {
return nil, fmt.Errorf("unable to parse translation: %v", translate)
}
tx, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return nil, fmt.Errorf("unable to parse translation: %v", translate)
}
ty, err := strconv.ParseFloat(parts[2], 64)
if err != nil {
return nil, fmt.Errorf("unable to parse translation: %v", translate)
}
tz, err := strconv.ParseFloat(parts[3], 64)
if err != nil {
return nil, fmt.Errorf("unable to parse translation: %v", translate)
}
scaleLine, err := b.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("could not read scale: %v", err)
}
parts = scaleRE.FindStringSubmatch(scaleLine)
if len(parts) != 2 {
return nil, fmt.Errorf("unable to parse scale: %v", scaleLine)
}
scale, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return nil, fmt.Errorf("unable to parse scale: %v", scaleLine)
}
data, err := b.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("could not read data: %v", err)
}
if data != "data\n" {
return nil, fmt.Errorf("could not find data section: %v", data)
}
log.Printf("binvox dim=(%v,%v,%v) translate=(%v,%v,%v), uniform scale=%v", nx, ny, nz, tx, ty, tz, scale)
// Read run-length encoded data.
var xi, yi, zi int
voxels := WhiteVoxelMap{}
for {
value, err := b.ReadByte()
if err != nil {
if err != io.EOF {
return nil, fmt.Errorf("error reading value: %v", err)
}
break
}
if value != 0 && value != 1 {
return nil, fmt.Errorf("invalid value: %v", value)
}
count, err := b.ReadByte()
if err != nil { // Should not EOF when getting count, so return error
return nil, fmt.Errorf("error reading count: %v", err)
}
if count == 0 {
return nil, fmt.Errorf("invalid count: %v", count)
}
// The y-coordinate runs fastest, then the z-coordinate, then the x-coordinate.
// Note that we are only saving the white pixels.
for i := 0; i < int(count); i++ {
if xi >= nx {
return nil, fmt.Errorf("run-length encoding overrun: x index=%v, x dim=%v", xi, nx)
}
if value == 1 && xi >= sx && xi <= sx+cx && yi >= sy && yi <= sy+cy && zi >= sz && zi <= sz+cz {
voxels[Key{X: xi, Y: yi, Z: zi}] = struct{}{}
}
yi++
if yi >= ny {
yi = 0
zi++
if zi >= nz {
zi = 0
xi++
}
}
}
}
return &BinVOX{
NX: nx, NY: ny, NZ: nz,
TX: tx, TY: ty, TZ: tz,
Scale: scale,
WhiteVoxels: voxels,
}, nil
}