Skip to content

Commit 613a64b

Browse files
committed
Add support for BINVOX files
1 parent 1b5bfde commit 613a64b

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dependencies": {
3838
"@sh-dave/format-vox": "^0.1.5",
3939
"auto-bind": "^4.0.0",
40+
"binvox": "^1.0.2",
4041
"iterator-result": "^1.0.0",
4142
"math-ds": ">= 1.0.0 < 2.0.0",
4243
"sparse-octree": "^6.0.2"

src/loaders/BINVOXLoader.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @author André Storhaug <[email protected]>
3+
*/
4+
5+
import autoBind from 'auto-bind';
6+
import { FileLoader, Loader, Vector3, LoadingManager } from 'three';
7+
import { PointOctree } from "sparse-octree";
8+
import { Parser } from 'binvox';
9+
import { levelOfDetail } from '../mixins/levelOfDetail';
10+
11+
/**
12+
* Class for loading voxel data stored in BINVOX files.
13+
* @extends Loader
14+
*/
15+
class BINVOXLoader extends Loader {
16+
/**
17+
* Create a BINVOXLoader.
18+
* @param {LoadingManager} manager
19+
* @mixes levelOfDetail
20+
*/
21+
constructor(manager) {
22+
super(manager);
23+
autoBind(this);
24+
Object.assign(this, levelOfDetail);
25+
}
26+
27+
/**
28+
* Loads and parses a BINVOX file from a URL.
29+
*
30+
* @param {String} url - URL to the BINVOX file.
31+
* @param {Function} [onLoad] - Callback invoked with the loaded object.
32+
* @param {Function} [onProgress] - Callback for download progress.
33+
* @param {Function} [onError] - Callback for download errors.
34+
*/
35+
load(url, onLoad, onProgress, onError) {
36+
var scope = this;
37+
38+
var loader = new FileLoader(this.manager);
39+
loader.setPath(this.path);
40+
loader.setResponseType('arraybuffer')
41+
loader.load(url, function (buffer) {
42+
43+
scope.parse(buffer)
44+
.then(octree => onLoad(octree))
45+
.catch(err => console.error(err))
46+
47+
}, onProgress, onError);
48+
}
49+
50+
/**
51+
* Parse BINVOX file data.
52+
* @param {Buffer} buffer Content of BINVOX file.
53+
* @return {Promise<PointOctree>} Promise with an octree filled with voxel data.
54+
*/
55+
parse(buffer) {
56+
57+
return new Promise((resolve, reject) => {
58+
const parser = new Parser();
59+
let data = parser.parse(buffer);
60+
console.log(data)
61+
const depth = data.dimension.depth;
62+
const width = data.dimension.width;
63+
const height = data.dimension.height;
64+
65+
const min = new Vector3(-depth / 2, -height / 2, -width / 2);
66+
const max = new Vector3(depth / 2, height / 2, width / 2);
67+
68+
const octree = new PointOctree(min, max, 0, this.LOD.maxPoints, this.LOD.maxDepth);
69+
let voxelData = {};
70+
71+
data.voxels.forEach(voxel => {
72+
let x, y, z;
73+
74+
x = voxel.x - (depth / 2);
75+
y = voxel.y - (width / 2);
76+
z = voxel.z - (height / 2);
77+
78+
let point = new Vector3(x, z, y);
79+
octree.insert(point, voxelData);
80+
});
81+
82+
resolve(octree);
83+
});
84+
}
85+
86+
}
87+
88+
export { BINVOXLoader };

src/loaders/LoaderFactory.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ArrayLoader } from "./ArrayLoader";
66
import { OctreeLoader } from "./OctreeLoader";
77
import { VOXLoader } from "./VOXLoader";
88
import { XMLLoader } from "./XMLLoader";
9+
import { BINVOXLoader } from "./BINVOXLoader";
910

1011
/**
1112
* Factory class for creating various loaders.
@@ -30,6 +31,9 @@ class LoaderFactory {
3031
case 'xml':
3132
return new XMLLoader(this.manager);
3233
break;
34+
case 'binvox':
35+
return new BINVOXLoader(this.manager);
36+
break;
3337
case 'array':
3438
return new ArrayLoader(this.manager);
3539
break;

0 commit comments

Comments
 (0)