Introduction
A .binvox file holds a cube of voxels: for each cell of a regular grid,
whether it is filled. It is the main output of Patrick Min’s binvox
program, which turns a 3D mesh into voxels, and is read by the companion tools
viewvox and thinvox. The format was put together by Michael
Kazhdan.
A file begins with the text #binvox, a space and the version number,
1 or 2.
Structure
A short ASCII header of lines is followed by binary data: the voxel grid, run-length encoded. There are no multi-byte binary numbers, so byte order does not arise.
0Header text lines, from#binvox 1todatavaries…Runs pairs of value byte and count byte, until the grid is full2 bytes each
Header
| Line | Required | Meaning |
|---|---|---|
#binvox version | Yes, first | Format version, 1 or 2 |
dim depth height width | Yes | Size of the grid in voxels. The three numbers should be equal |
translate tx ty tz | No | Translation that leads back to mesh coordinates |
scale s | No | Scale factor that leads back to mesh coordinates |
data | Yes, last | Ends the header; the binary data starts after the line’s end |
The sample reader reads the header as words separated by white space. It skips the
rest of a line that starts with a word it does not know, and fails if there is no
dim before data. After data it consumes exactly
one byte, the line feed, so the data begins at the very next byte.
#binvox 1
dim 128 128 128
translate -0.120158 -0.481158 -0.863158
scale 7.24632
data
Voxel data
| Offset in run | Size | Name | Meaning |
|---|---|---|---|
| 0 | 1 | value | 0 is an empty voxel. In version 1 a filled voxel is 1. In version 2 a filled voxel can be any value from 1 to 255 |
| 1 | 1 | count | How many voxels in a row have this value, 1 to 255 |
Runs follow one another until depth × height × width voxels have been produced. The sample reader treats a run that would pass the end of the grid as an error. A longer stretch of one value is written as several runs.
Version 2 files are written by the binvox_merge tool. The only
difference from version 1 is that filled voxels can have different values; the format
gives the values no further meaning.
Voxel order
In the decoded array the y coordinate runs fastest, then z, then x. With a grid of side d, the voxel at (x, y, z), each counted from 0, is at index:
index = x * d * d + z * d + y
Mesh coordinates
Before voxelising, binvox moves and uniformly scales the mesh to fit a
unit cube with its corner at the origin. The centre of voxel
(i, j, k) in that cube is:
(xn, yn, zn) = ((i + 0.5) / d, (j + 0.5) / d, (k + 0.5) / d)
To return to the mesh’s own coordinates, multiply by the scale factor,
then add the translate vector:
x = xn * s + tx
y = yn * s + ty
z = zn * s + tz
If the two lines are absent, the same result needs the normalisation steps that
binvox printed when it made the file, applied in reverse.
Worked example
A 2 × 2 × 2 grid with translate -2 -2 -2 and scale 4, in
which two voxels are filled. The data after the header is six bytes.
| Bytes | Meaning | Indices |
|---|---|---|
00 03 | Three empty voxels | 0 to 2 |
01 02 | Two filled voxels | 3 and 4 |
00 03 | Three empty voxels | 5 to 7 |
Index 3 is voxel (0, 1, 1) and index 4 is voxel (1, 0, 0). The centre of the second is (0.75, 0.25, 0.25) in the unit cube and (1, −1, −1) in mesh coordinates.
Divergence from the specification
The text of the specification names the three dim numbers depth, width
and height. The sample program read_binvox.cc by the same author reads
them as depth, height and width. Since the three should be equal, files read the same
either way.
In the explorer
A dropped .binvox file shows its header lines and every run, in the byte
tree and in a table, with the number of voxels of each value. The Slices tab lists the
slices along x; each opens as a picture, so every voxel can be seen and traced
to its run. The surface of the filled voxels is drawn in 3D, in mesh coordinates when
the file has translate and scale. Nothing is thinned out: a
surface of more than a million rectangles is divided into parts of whole slices, each
opened on its own; a part takes roughly 200 MB of memory per million rectangles.
Files up to 256 MB are read. Slices and the 3D view are made for grids whose sides are at most 8,192 voxels; a complete cube that fits in 256 MB has a side below 3,200.
Links
Software
- binvox – command line mesh voxeliser (Windows, Linux, macOS), free with a paid version for larger grids
- viewvox – viewer for binvox files
- binvox-rw-py – reader and writer in Python
Reference
- BINVOX voxel file format specification – the header, ordering and encoding
- read_binvox.cc – the author’s sample reader in C++