This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinvox2vola.py
129 lines (108 loc) · 4.23 KB
/
binvox2vola.py
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
#!/usr/bin/env python3
"""
binvox2vola: Converts binvox files into VOLA format.
Binvox is a very popular volumetric representation that uses run
length encoding to achieve significant compression. It is included
as there are many datasets that are stored in binvox format.
There is no information other than voxels so the occupancy information
is only available for this format.
@author Jonathan Byrne
@copyright 2018 Intel Ltd (see LICENSE file).
"""
from __future__ import print_function
import glob
import os
import numpy as np
import binutils as bu
from volatree import VolaTree
def main():
"""Read the file, build the tree. Write a Binary."""
start_time = bu.timer()
parser = bu.parser_args("*.binvox")
args = parser.parse_args()
# Parse directories or filenames, whichever you want!
if os.path.isdir(args.input):
filenames = glob.glob(os.path.join(args.input, '*.binvox'))
else:
filenames = glob.glob(args.input)
print("processing: ", ' '.join(filenames))
for filename in filenames:
if args.dense:
outfilename = bu.sub(filename, "dvol")
else:
outfilename = bu.sub(filename, "vol")
if os.path.isfile(outfilename):
print("File already exists!")
continue
print("converting", filename, "to", outfilename)
bbox, points, pointsdata = parse_binvox(filename)
print("binvox only has occupancy data," +
" no additional data is being added")
nbits = 0
if len(points) > 0:
volatree = VolaTree(args.depth, bbox, args.crs,
args.dense, nbits)
volatree.cubify(points, pointsdata)
volatree.writebin(outfilename)
bu.print_ratio(filename, outfilename)
else:
print("The points file is empty!")
bu.timer(start_time)
def parse_binvox(filename):
"""Read xyz format point data and return header, points and points data."""
header = {}
with open(filename, 'rb') as infile:
# read header info
line = infile.readline().strip()
if line.startswith(b'#binvox'):
header['dims'] = [int(x) for x in
infile.readline().strip().split(b' ')[1:]]
header['translate'] = [float(x) for x in
infile.readline().strip().split(b' ')[1:]]
header['scale'] = float(infile.readline().strip().split(b' ')[1])
infile.readline() # to remove the data line
else:
print("Not a binvox file")
exit()
bytevals = np.frombuffer(infile.read(), dtype=np.uint8)
points = runlength_to_xyz(bytevals, header)
# show everything
# np.set_printoptions(threshold=np.inf)
# supress scientific notation
np.set_printoptions(suppress=True)
minvals = points.min(axis=0).tolist()
maxvals = points.max(axis=0).tolist()
if header['dims'][0] == 32:
maxvals = [64, 64, 64]
elif header['dims'][0] == 128:
maxvals = [256, 256, 256]
elif header['dims'][0] == 512:
maxvals = [1024, 1024, 1024]
else:
maxvals = header['dims']
bbox = [minvals, maxvals]
return bbox, points, None
def runlength_to_xyz(bytevals, header):
"""Binvox uses a binary runlength encoding (valuebyte, countbyte)."""
# odds and evens, the value and then the count is specified
values, counts = bytevals[::2], bytevals[1::2]
# Make a list of start/end indexes for each run.
start, end = 0, 0
end_indexes = np.cumsum(counts)
indexes = np.concatenate(([0], end_indexes[:-1])).astype(np.int)
# use the values as booleans to remove empty points from the array
values = values.astype(np.bool)
indexes = indexes[values]
end_indexes = end_indexes[values]
occupied_voxels = []
for start, end in zip(indexes, end_indexes):
occupied_voxels.extend(range(start, end))
occupied_voxels = np.array(occupied_voxels)
x = occupied_voxels / (header['dims'][0] * header['dims'][1])
zwpy = occupied_voxels % (header['dims'][0] * header['dims'][1]) # z*w + y
z = zwpy / header['dims'][0]
y = zwpy % header['dims'][0]
points = np.vstack((x, y, z)).T
return points
if __name__ == '__main__':
main()