-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathVolumetricData.js
More file actions
288 lines (267 loc) · 11.6 KB
/
VolumetricData.js
File metadata and controls
288 lines (267 loc) · 11.6 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const fs = require('fs');
const utils = require('./utils');
const Vector3 = require('../../dependencies/Vector3');
/**
* Buffered data that can be loaded all at once.
*/
class VolumetricData {
/**
* @constructor
* @param {Vector3} dim - dimensions of data
* @param {String} type - data type
* @param {Integer} [numElementsPerVox = 1] - number of elements stored in each voxel
* (eg three elements may be stored for a 3D vector field)
* @param {Boolean} useNull - flag if we should be checking for nullVal
*/
constructor(dim, type, useNull, numElementsPerVox = 1) {
if (dim === undefined || type === undefined || useNull === undefined) {
throw new Error('Missing initialization param for BufferedVolumetricData.');
}
if (dim.x <= 0 || dim.y <= 0 || dim.z <= 0 || !utils.isInteger(dim.x) ||
!utils.isInteger(dim.y) || !utils.isInteger(dim.z)) throw new Error('Dimensions for BufferedVolumetricData must be positive integers.');
if (numElementsPerVox <= 0 || !utils.isInteger(numElementsPerVox)) throw new Error('numElementsPerVox for BufferedVolumetricData must be a positive integer.');
this._dim = dim;
this._numElementsPerVox = numElementsPerVox;
this._type = type.toLowerCase();
this._arraydimZ = Math.floor((Math.pow(2, 31) - 1) / (this._numElementsPerVox * utils.dataSizeForType(this._type) * this._dim.x * this._dim.y));
this._numArrays = Math.ceil(this._dim.z / this._arraydimZ);// we need to split data into multiple arrays to avoid hitting js limit on array size
if (this._arraydimZ * this._dim.x * this._dim.y * this._numElementsPerVox
* utils.dataSizeForType(this._type) > (Math.pow(2, 31) - 1)) {
throw new Error('Something went wrong in this._numArrays calc.');
}
this._adjustedDim = new Vector3(dim.x, dim.y, this._arraydimZ);
this._dataOffset = 0;
this._dataSize = utils.dataSizeForType(type);
this._useNull = useNull;
if (this._useNull) this._nullVal = utils.nullValForType(type);
this._initData();
}
_initData() {
this._data = [];
const size = this._adjustedDim.x * this._adjustedDim.y * this._adjustedDim.z * this._numElementsPerVox;
switch (this._type) {
case 'uint8':
for (let i = 0; i < this._numArrays; i++) {
this._data.push(new Uint8Array(size));
}
break;
case 'float32':
for (let i = 0; i < this._numArrays; i++) {
this._data.push(new Float32Array(size));
}
break;
case 'int32':
for (let i = 0; i < this._numArrays; i++) {
this._data.push(new Int32Array(size));
}
break;
default:
throw new Error(`Unknown type ${this._type}.`);
}
if (this._useNull) {
// Typed arrays are filled with zeros by default, fill with nulls if necessary.
this.clear();
}
}
_getArrayIndicesForZ(x, y, z) {
const arrayNum = Math.floor(z / this._arraydimZ);
if (arrayNum > this._numArrays - 1) {
throw new Error(`Z index ${z} beyond bounds of array.`);
}
const adjustedZ = z - arrayNum * this._arraydimZ;
const index = this._numElementsPerVox * utils.index3Dto1D(x, y, adjustedZ, this._adjustedDim);
// Math.floor((Math.pow(2, 31) - 1) / utils.dataSizeForType(this._type))
if (index > 2147483647 / utils.dataSizeForType(this._type)) {
throw new Error(`Array index ${index} beyond bounds of max Typed Array size.`);
}
return [arrayNum, index];
}
/**
* set data at voxel
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @param {Number | null} val - data
*/
set(x, y, z, val) {
if (this._numElementsPerVox !== 1) throw new Error('Must be exactly 1 element per voxel to call set().');
const [arrayNum, index] = this._getArrayIndicesForZ(x, y, z);
if (val === null) {
if (this._useNull) this._data[arrayNum][index] = this._nullVal;
else throw new Error('Null value not valid for this VolumetricData.');
} else {
this._data[arrayNum][index] = val;
}
}
/**
* set array of elements corresponding to voxel
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @param {Array} val - array of values to set
*/
setValues(x, y, z, val) {
if (this._numElementsPerVox === 1) throw new Error('Must be more than 1 element per voxel to call setValues().');
const [arrayNum, index] = this._getArrayIndicesForZ(x, y, z);
if (val === null) {
if (this._useNull) {
for (let i = 0; i < this._numElementsPerVox; i++) {
this._data[arrayNum][index + i] = this._nullVal;
}
} else throw new Error('Null value not valid for this VolumetricData.');
} else {
if (this._numElementsPerVox !== val.length) throw new Error('setValues() val param must be same length as numElementsPerVox.');
for (let i = 0; i < this._numElementsPerVox; i++) {
this._data[arrayNum][index + i] = val[i];
}
}
}
/**
* set vector 3 at voxel
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @param {Vector3 | null} vec - data
*/
setVector3(x, y, z, vec) {
if (this._numElementsPerVox !== 3) throw new Error('Must be exactly three elements per voxel to call setVector3().');
const [arrayNum, index] = this._getArrayIndicesForZ(x, y, z);
if (vec === null) {
if (this._useNull) {
this._data[arrayNum][index] = this._nullVal;
this._data[arrayNum][index + 1] = this._nullVal;
this._data[arrayNum][index + 2] = this._nullVal;
} else throw new Error('Null value not valid for this BufferedVolumetricDataRW.');
} else {
this._data[arrayNum][index] = vec.x;
this._data[arrayNum][index + 1] = vec.y;
this._data[arrayNum][index + 2] = vec.z;
}
}
/**
* set 8 bit vector 3 at voxel
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @param {Vector3 | null} vec - data
*/
setFloat8Vector3(x, y, z, vec) {
if (vec) {
vec = vec.clone().multiplyScalar(127).addScalar(127).round();
}
this.setVector3(x, y, z, vec);
}
/**
* get single value corresponding to voxel
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @returns {Number|null} value at voxel or null
*/
get(x, y, z) {
if (this._numElementsPerVox !== 1) throw new Error('Must be exactly 1 element per voxel to call get().');
const [arrayNum, index] = this._getArrayIndicesForZ(x, y, z);
const val = this._data[arrayNum][index];
if (this._useNull && val === this._nullVal) return null;
return val;
}
/**
* get array of elements corresponding to voxel
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @returns {Array|null} values at voxel or null
*/
getValues(x, y, z) {
if (this._numElementsPerVox === 1) throw new Error('Must be more than 1 element per voxel to call getValues().');
const [arrayNum, index] = this._getArrayIndicesForZ(x, y, z);
const vals = [this._data[arrayNum][index]];
if (this._useNull && vals[0] === this._nullVal) return null;
for (let i = 1; i < this._numElementsPerVox; i++) {
const nextVal = this._data[arrayNum][index + i];
if (this._useNull && nextVal === this._nullVal) return vals;
vals.push(nextVal);
}
return vals;
}
/**
* get Vector3 corresponding to voxel
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @returns {Vector3|null} Vector3 at voxel or null
*/
getVector3(x, y, z) {
if (this._numElementsPerVox !== 3) throw new Error('Must be exactly three elements per voxel to call getVector3().');
const [arrayNum, index] = this._getArrayIndicesForZ(x, y, z);
const xVal = this._data[arrayNum][index];
if (this._useNull && xVal === this._nullVal) return null;
return new Vector3(xVal, this._data[arrayNum][index + 1], this._data[arrayNum][index + 2]);
}
/**
* get Vector3 corresponding to voxel (this is used for normals, that's why there's a normalize)
* @param {Integer} x - x position
* @param {Integer} y - y position
* @param {Integer} z - z position
* @returns {Vector3|null} Vector3 at voxel or null
*/
getFloat8Vector3(x, y, z) {
const vec = this.getVector3(x, y, z);
vec.subScalar(127).multiplyScalar(1 / 127);
return vec.normalize();
}
getLayer(z) {
const [arrayNum, index] = this._getArrayIndicesForZ(0, 0, z);
z -= arrayNum * this._arraydimZ;
return this._data[arrayNum].slice(z * this._dim.x * this._dim.y * this._numElementsPerVox,
(z + 1) * this._dim.x * this._dim.y * this._numElementsPerVox);
}
clear() {
const clearVal = this._useNull ? this._nullVal : 0;
const size = this._adjustedDim.x * this._adjustedDim.y * this._adjustedDim.z * this._numElementsPerVox;
for (let j = 0; j < this._numArrays; j++) {
for (let i = 0; i < size; i++) {
this._data[j][i] = clearVal;
}
}
}
// saveAsVol(path, filename) {
// let fd = fs.openSync(`${path}${filename}.vol`, 'w');
// const layerSize = this._dim.y * this._dim.x * this._numElementsPerVox;
// let buffer = Buffer.alloc(layerSize * this._dataSize);
// for (let z = 0; z < this._dim.z; z++) {
// const offset = z * layerSize;
// for (let y = 0; y < this._dim.y; y++) {
// for (let x = 0; x < this._dim.x; x++) {
// for (let i = 0; i < this._numElementsPerVox; i++) {
// const index = this._numElementsPerVox * (this._dim.x * y + x) + i;
// switch (this._type) {
// case 'float32':
// buffer.writeFloatLE(this._data[offset + index], 4 * index);
// break;
// case 'int32':
// buffer.writeInt32LE(this._data[offset + index], 4 * index);
// break;
// case 'uint8':
// buffer[index] = this._data[offset + index];
// break;
// default:
// throw new Error(`Unknown type ${this._type}.`);
// }
// }
// }
// }
// fs.writeSync(fd, buffer, 0, buffer.length, this._dataSize);
// }
// fs.closeSync(fd);
// fd = null;
// buffer = null;
// }
/**
* close file, clear all memory for garbage collection
*/
destroy() {
this._data = null;
}
}
module.exports = VolumetricData;