clean up codec transferrable object allocation and encoding
This commit is contained in:
parent
f5941befaf
commit
4ce39c75b5
3 changed files with 39 additions and 31 deletions
|
|
@ -475,9 +475,9 @@ kiri.worker = {
|
|||
const state = { zeros: [] };
|
||||
const emit = { progress, message };
|
||||
if (layer) {
|
||||
emit.layer = codec.encode(layer, state)
|
||||
emit.layer = codec.encode(layer, state);
|
||||
}
|
||||
send.data(emit);
|
||||
send.data(emit, state.zeros);
|
||||
});
|
||||
|
||||
const unitScale = settings.controller.units === 'in' ? (1 / 25.4) : 1;
|
||||
|
|
@ -656,7 +656,7 @@ dispatch.onmessage = self.onmessage = function(e) {
|
|||
// if (direct && direct.length) {
|
||||
// console.log({
|
||||
// zeros: direct.length,
|
||||
// sz:direct.map(z => z.byteLength).reduce((a,v) => a+v)
|
||||
// sz:direct.map(z => z.byteLength)
|
||||
// });
|
||||
// }
|
||||
dispatch.send({
|
||||
|
|
|
|||
|
|
@ -58,17 +58,28 @@ function toCodable(object) {
|
|||
return o;
|
||||
}
|
||||
|
||||
function allocFloat32Array(arg) {
|
||||
function allocFloat32Array(arg, zeros) {
|
||||
if (arg === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if (arg.length === 0) {
|
||||
return [];
|
||||
}
|
||||
let f32;
|
||||
if (arg.byteLength) {
|
||||
// already a float array
|
||||
return arg;
|
||||
}
|
||||
if (Array.isArray(arg)) {
|
||||
f32 = arg;
|
||||
} else if (Array.isArray(arg)) {
|
||||
// create float array from array
|
||||
return new Float32Array(arg);
|
||||
f32 = new Float32Array(arg);
|
||||
} else {
|
||||
// usually a number (size) for array
|
||||
f32 = new Float32Array(arg);
|
||||
}
|
||||
// usually a number (size) for array
|
||||
return new Float32Array(arg);
|
||||
if (zeroOut && zeros && f32.byteLength > 0) {
|
||||
zeros.push(f32.buffer);
|
||||
}
|
||||
return f32;
|
||||
}
|
||||
|
||||
function encode(o, state) {
|
||||
|
|
@ -187,6 +198,7 @@ registerDecoder('slice', function(v, state) {
|
|||
});
|
||||
|
||||
kiri.Layers.prototype.encode = function(state) {
|
||||
let zeros = state.zeros;
|
||||
let enc = {
|
||||
type: 'layers',
|
||||
layers: Object.keys(this.layers),
|
||||
|
|
@ -194,29 +206,23 @@ kiri.Layers.prototype.encode = function(state) {
|
|||
const e = {
|
||||
polys: encode(layer.polys, state),
|
||||
lines: encodePointArray(layer.lines, state),
|
||||
faces: codec.allocFloat32Array(layer.faces),
|
||||
norms: layer.norms ? codec.allocFloat32Array(layer.norms) : undefined,
|
||||
faces: codec.allocFloat32Array(layer.faces, zeros),
|
||||
norms: codec.allocFloat32Array(layer.norms, zeros),
|
||||
cface: layer.cface || codec.undef,
|
||||
color: layer.color,
|
||||
paths: layer.paths.map(lp => {
|
||||
const pe = {
|
||||
z: lp.z,
|
||||
index: lp.index,
|
||||
faces: codec.allocFloat32Array(lp.faces),
|
||||
norms: lp.norms ? codec.allocFloat32Array(lp.norms) : undefined
|
||||
faces: codec.allocFloat32Array(lp.faces, zeros),
|
||||
norms: codec.allocFloat32Array(lp.norms, zeros)
|
||||
};
|
||||
if (zeroOut && state.zeros && pe.faces.length) {
|
||||
state.zeros.push(pe.faces.buffer);
|
||||
}
|
||||
return pe;
|
||||
}),
|
||||
cpath: layer.cpath || codec.undef,
|
||||
off: layer.off
|
||||
};
|
||||
if (zeroOut && state.zeros && e.faces.length) {
|
||||
state.zeros.push(e.faces.buffer);
|
||||
}
|
||||
// console.log('-->',layer,e);
|
||||
// console.log('-->',layer,e,zeros.length);
|
||||
return e;
|
||||
})
|
||||
};
|
||||
|
|
@ -301,9 +307,14 @@ registerDecoder('top', function(v, state) {
|
|||
});
|
||||
|
||||
function encodePointArray(points, state, z) {
|
||||
if (!points) return null;
|
||||
if (!points) {
|
||||
return null;
|
||||
}
|
||||
if (points.length === 0) {
|
||||
return points;
|
||||
}
|
||||
|
||||
let array = codec.allocFloat32Array(points.length * 3),
|
||||
let array = codec.allocFloat32Array(points.length * 3, state.zeros),
|
||||
pos = 0;
|
||||
|
||||
points.forEach(function(point) {
|
||||
|
|
@ -326,10 +337,6 @@ function encodePointArray(points, state, z) {
|
|||
array[pos++] = z !== undefined ? z : point.z;
|
||||
});
|
||||
|
||||
if (zeroOut && state.zeros) {
|
||||
state.zeros.push(array.buffer);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -54,10 +54,10 @@ class Layers {
|
|||
off: off === true,
|
||||
lines: [],
|
||||
polys: [], // colors are an attribute on polygons
|
||||
faces: [],
|
||||
norms: undefined, // vertex normals
|
||||
cface: undefined, // face color indices
|
||||
paths: [],
|
||||
faces: [], // triangles for areas and flats
|
||||
norms: undefined, // flats vertex normals
|
||||
cface: undefined, // flats face color indices
|
||||
paths: [], // 3d extrusion / tube paths (used to be an array, now merged - FIX)
|
||||
cpath: undefined, // path colors indices
|
||||
color: colors || {
|
||||
check: 0,
|
||||
|
|
@ -128,6 +128,7 @@ class Layers {
|
|||
}
|
||||
|
||||
// z planar closed polygonal areas
|
||||
// used for FDM solids, bridges, flats debug & SLA slice visualization
|
||||
addAreas(polys, options) {
|
||||
const faces = this.current.faces;
|
||||
polys = Array.isArray(polys) ? polys : [ polys ];
|
||||
|
|
|
|||
Loading…
Reference in a new issue