Repository navigation
Expand file tree
/
Copy pathdc-manager.js
More file actions
196 lines (165 loc) · 6.23 KB
/
Copy pathdc-manager.js
File metadata and controls
196 lines (165 loc) · 6.23 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
dc wasm integration.
*/
import Module from './public/dc.module.js';
import {Allocator, ScratchStack} from './geometry-util.js';
const dc = {};
let loadPromise = null;
// let scratchStack = null;
// dc.loaded = false;
dc.waitForLoad = () => {
if (!loadPromise) {
loadPromise = (async () => {
await Module.waitForLoad();
// Module._initialize();
// const scratchStackSize = 8 * 1024 * 1024;
// scratchStack = new ScratchStack(Module, scratchStackSize);
// dc.loaded = true;
})();
}
return loadPromise;
};
const fillModes = ['floodFill', 'surfaceOnly', 'raycastFill'];
dc.fillModes = fillModes;
const dcWorker = (() => {
const w = {};
w.alloc = (constructor, count) => {
if (count > 0) {
const size = constructor.BYTES_PER_ELEMENT * count;
const ptr = Module._doMalloc(size)
return new constructor(Module.HEAP8.buffer, ptr, count);
} else {
return new constructor(Module.HEAP8.buffer, 0, 0);
}
};
w.free = (ptr) => {
Module._doFree(ptr)
};
w.createInstance = ({
chunkSize,
range,
fatness,
}) => {
return Module._createInstance(
chunkSize,
range,
fatness,
);
};
w.destroyInstance = (...args) => {
return Module._destroyInstance(...args);
};
w.createPointCloudMesh = (instance, positions) => {
const allocator = new Allocator(Module);
const positionsTypedArray = allocator.alloc(Float32Array, positions.length);
positionsTypedArray.set(positions);
const outputLengthArray = allocator.alloc(Uint32Array, 1);
const outputBufferOffset = Module._createPointCloudMesh(
instance,
positionsTypedArray.byteOffset,
positionsTypedArray.length,
outputLengthArray.byteOffset,
);
const outputLength = outputLengthArray[0];
const outputBuffer = new Uint8Array(
Module.HEAP8.buffer,
outputBufferOffset,
outputLength
).slice();
allocator.freeAll();
Module._doFree(outputBufferOffset);
return outputBuffer;
};
w.marchCubes = (instance, positions) => {
const allocator = new Allocator(Module);
const positionsTypedArray = allocator.alloc(Float32Array, positions.length);
positionsTypedArray.set(positions);
const outputLengthArray = allocator.alloc(Uint32Array, 1);
const outputBufferOffset = Module._marchCubes(
instance,
positionsTypedArray.byteOffset,
positionsTypedArray.length,
outputLengthArray.byteOffset,
);
const outputLength = outputLengthArray[0];
const outputBuffer = new Uint8Array(
Module.HEAP8.buffer,
outputBufferOffset,
outputLength
).slice();
allocator.freeAll();
Module._doFree(outputBufferOffset);
return outputBuffer;
};
// class Parameters
// {
// public:
// IUserCallback* m_callback{nullptr}; // Optional user provided callback interface for progress
// IUserLogger* m_logger{nullptr}; // Optional user provided callback interface for log messages
// IUserTaskRunner* m_taskRunner{nullptr}; // Optional user provided interface for creating tasks
// uint32_t m_maxConvexHulls{ 64 }; // The maximum number of convex hulls to produce
// uint32_t m_resolution{ 400000 }; // The voxel resolution to use
// double m_minimumVolumePercentErrorAllowed{ 1 }; // if the voxels are within 1% of the volume of the hull, we consider this a close enough approximation
// uint32_t m_maxRecursionDepth{ 10 }; // The maximum recursion depth
// bool m_shrinkWrap{true}; // Whether or not to shrinkwrap the voxel positions to the source mesh on output
// FillMode m_fillMode{ FillMode::FLOOD_FILL }; // How to fill the interior of the voxelized mesh
// uint32_t m_maxNumVerticesPerCH{ 64 }; // The maximum number of vertices allowed in any output convex hull
// bool m_asyncACD{ true }; // Whether or not to run asynchronously, taking advantage of additional cores
// uint32_t m_minEdgeLength{ 2 }; // Once a voxel patch has an edge length of less than 4 on all 3 sides, we don't keep recursing
// bool m_findBestPlane{ false }; // Whether or not to attempt to split planes along the best location. Experimental feature. False by default.
// };
w.vhacd = ({
positions,
indices,
maxConvexHulls = 64,
resolution = 400000,
minimumVolumePercentErrorAllowed = 1,
maxRecursionDepth = 10,
shrinkWrap = true,
fillMode = fillModes[0],
maxNumVerticesPerCH = 64,
minEdgeLength = 2,
findBestPlane = false,
}) => {
const allocator = new Allocator(Module);
const positionsTypedArray = allocator.alloc(Float32Array, positions.length);
positionsTypedArray.set(positions);
const indicesTypedArray = allocator.alloc(Uint32Array, indices.length);
indicesTypedArray.set(indices);
const outputLengthArray = allocator.alloc(Uint32Array, 1);
const shrinkWrapInt = +shrinkWrap;
const fillModeIndex = fillModes.indexOf(fillMode);
if (fillModeIndex === -1) {
throw new Error('invalid fill mode: ' + fillMode);
}
const findBestPlaneInt = +findBestPlane;
const outputBufferOffset = Module._vhacd(
positionsTypedArray.byteOffset,
positionsTypedArray.length,
indicesTypedArray.byteOffset,
indicesTypedArray.length,
outputLengthArray.byteOffset,
maxConvexHulls,
resolution,
minimumVolumePercentErrorAllowed,
maxRecursionDepth,
shrinkWrapInt,
fillModeIndex,
maxNumVerticesPerCH,
minEdgeLength,
findBestPlaneInt,
);
const outputLength = outputLengthArray[0];
const outputBuffer = new Uint8Array(
Module.HEAP8.buffer,
outputBufferOffset,
outputLength
).slice();
allocator.freeAll();
Module._doFree(outputBufferOffset);
return outputBuffer;
};
return w;
})();
dc.dcWorker = dcWorker;
export default dc;