2020-06-03 22:39:30 -04:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
2020-11-03 11:19:13 -05:00
|
|
|
let BASE = self.base,
|
|
|
|
|
KIRI = self.kiri,
|
|
|
|
|
UTIL = BASE.util,
|
|
|
|
|
time = UTIL.time,
|
2020-06-16 11:08:50 -04:00
|
|
|
current = self.worker = {
|
|
|
|
|
print: null,
|
|
|
|
|
snap: null
|
2020-11-03 11:19:13 -05:00
|
|
|
},
|
|
|
|
|
cache = {};
|
2020-03-18 18:22:57 -04:00
|
|
|
|
|
|
|
|
// catch clipper alerts and convert to console messages
|
|
|
|
|
self.alert = function(o) {
|
|
|
|
|
console.log(o);
|
|
|
|
|
};
|
2020-02-29 20:13:49 -05:00
|
|
|
|
2020-11-03 11:19:13 -05:00
|
|
|
console.log(`kiri | init work | ${KIRI.version}`);
|
|
|
|
|
BASE.debug.disable();
|
|
|
|
|
|
|
|
|
|
// code is running in the worker / server context
|
|
|
|
|
const dispatch =
|
|
|
|
|
KIRI.server =
|
|
|
|
|
KIRI.worker = {
|
|
|
|
|
cache: cache,
|
|
|
|
|
|
2020-11-06 10:47:49 -05:00
|
|
|
decimate: function(data, send) {
|
|
|
|
|
let { vertices, options } = data;
|
2020-02-29 20:13:49 -05:00
|
|
|
vertices = new Float32Array(vertices),
|
2020-11-06 10:47:49 -05:00
|
|
|
vertices = BASE.pointsToVertices(BASE.verticesToPoints(vertices, options));
|
2020-02-29 20:13:49 -05:00
|
|
|
send.done(vertices);
|
|
|
|
|
},
|
|
|
|
|
|
2020-04-15 21:44:37 -04:00
|
|
|
snap: function(data, send) {
|
2020-06-16 11:08:50 -04:00
|
|
|
current.snap = data;
|
2020-04-15 21:44:37 -04:00
|
|
|
send.done();
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
slice: function(data, send) {
|
|
|
|
|
let settings = data.settings,
|
|
|
|
|
vertices = new Float32Array(data.vertices),
|
|
|
|
|
position = data.position,
|
2020-04-19 21:32:06 -04:00
|
|
|
tracking = data.tracking,
|
2020-11-06 10:47:49 -05:00
|
|
|
points = BASE.verticesToPoints(vertices, { maxpass: 0 }),
|
2020-05-06 14:04:43 -04:00
|
|
|
state = data.state || {},
|
|
|
|
|
rotation = state.rotation,
|
|
|
|
|
centerz = state.centerz,
|
|
|
|
|
movez = state.movez;
|
|
|
|
|
|
|
|
|
|
if (rotation) {
|
|
|
|
|
state.rotate = new THREE.Matrix4().makeRotationY(-rotation);
|
|
|
|
|
}
|
2020-02-29 20:13:49 -05:00
|
|
|
|
|
|
|
|
send.data({update:0.05, updateStatus:"slicing"});
|
|
|
|
|
|
2020-11-03 11:19:13 -05:00
|
|
|
let widget = KIRI.newWidget(data.id).setPoints(points),
|
|
|
|
|
last = time(),
|
2020-02-29 20:13:49 -05:00
|
|
|
now;
|
|
|
|
|
|
|
|
|
|
// do it here so cancel can work
|
|
|
|
|
cache[data.id] = widget;
|
|
|
|
|
|
|
|
|
|
// fake mesh object to satisfy printing
|
2020-04-19 21:32:06 -04:00
|
|
|
widget.track = tracking;
|
2020-02-29 20:13:49 -05:00
|
|
|
widget.mesh = {
|
|
|
|
|
widget: widget,
|
|
|
|
|
position: position
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-01 09:04:10 -04:00
|
|
|
try {
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
widget.slice(settings, function(error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
delete cache[data.id];
|
|
|
|
|
send.data({error: error});
|
|
|
|
|
} else {
|
|
|
|
|
let slices = widget.slices || [];
|
|
|
|
|
send.data({send_start: time()});
|
|
|
|
|
send.data({
|
|
|
|
|
topo: settings.synth.sendTopo ? widget.topo : null,
|
|
|
|
|
stats: widget.stats,
|
|
|
|
|
slices: slices.length
|
|
|
|
|
});
|
|
|
|
|
slices.forEach(function(slice,index) {
|
2020-05-06 14:04:43 -04:00
|
|
|
send.data({index: index, slice: slice.encode(state)});
|
2020-02-29 20:13:49 -05:00
|
|
|
})
|
2020-03-18 18:22:57 -04:00
|
|
|
if (self.debug && widget.polish) {
|
2020-11-03 11:19:13 -05:00
|
|
|
send.data({polish: KIRI.codec.encode(widget.polish)});
|
2020-03-04 15:07:06 -05:00
|
|
|
}
|
2020-02-29 20:13:49 -05:00
|
|
|
send.data({send_end: time()});
|
|
|
|
|
}
|
|
|
|
|
send.done({done: true});
|
|
|
|
|
}, function(update, msg) {
|
2020-11-03 11:19:13 -05:00
|
|
|
now = time();
|
2020-02-29 20:13:49 -05:00
|
|
|
if (now - last < 10 && update < 0.99) return;
|
|
|
|
|
// on update
|
|
|
|
|
send.data({update: (0.05 + update * 0.95), updateStatus: msg});
|
|
|
|
|
last = now;
|
|
|
|
|
});
|
2020-04-01 09:04:10 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
send.data({error: error.toString()});
|
2020-04-13 19:28:17 -04:00
|
|
|
console.log(error);
|
2020-04-01 09:04:10 -04:00
|
|
|
}
|
2020-02-29 20:13:49 -05:00
|
|
|
},
|
|
|
|
|
|
2020-11-08 13:04:54 -05:00
|
|
|
prepare: function(data, send) {
|
2020-11-08 10:55:36 -05:00
|
|
|
// create widget array from id:widget cache
|
2020-11-08 13:04:54 -05:00
|
|
|
const widgets = Object.values(cache);
|
2020-02-29 20:13:49 -05:00
|
|
|
|
2020-11-08 10:55:36 -05:00
|
|
|
// let client know we've started
|
2020-02-29 20:13:49 -05:00
|
|
|
send.data({update:0.05, updateStatus:"preview"});
|
|
|
|
|
|
2020-11-08 13:04:54 -05:00
|
|
|
const drivers = KIRI.driver;
|
|
|
|
|
const settings = data.settings;
|
|
|
|
|
const mode = settings.mode;
|
|
|
|
|
const driver = drivers[mode];
|
|
|
|
|
|
|
|
|
|
if (!(driver && driver.prepare)) {
|
|
|
|
|
return console.log({invalid_print_driver: mode, driver});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const layers = driver.prepare(widgets, settings, (progress, message) => {
|
|
|
|
|
send.data({ progress, message });
|
2020-02-29 20:13:49 -05:00
|
|
|
});
|
2020-11-08 13:04:54 -05:00
|
|
|
|
|
|
|
|
send.done({ done: true, output: KIRI.codec.encode(layers) });
|
2020-02-29 20:13:49 -05:00
|
|
|
},
|
2020-04-14 09:26:59 -04:00
|
|
|
|
2020-11-08 17:35:40 -05:00
|
|
|
export: function(data, send) {
|
|
|
|
|
const mode = data.settings.mode;
|
|
|
|
|
const driver = KIRI.driver[mode];
|
|
|
|
|
|
|
|
|
|
if (!(driver && driver.export)) {
|
|
|
|
|
console.log({missing_export_driver: mode});
|
|
|
|
|
return send.done()
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 19:38:02 -05:00
|
|
|
let output;
|
|
|
|
|
driver.export(current.print, function(line, direct) {
|
|
|
|
|
send.data({line}, direct);
|
|
|
|
|
}, function(done) {
|
|
|
|
|
// SLA workaround
|
|
|
|
|
output = done;
|
2020-11-08 17:35:40 -05:00
|
|
|
});
|
2020-11-08 19:38:02 -05:00
|
|
|
const { bounds, time, lines, bytes, distance, settings } = current.print;
|
2020-11-08 17:35:40 -05:00
|
|
|
|
|
|
|
|
send.done({
|
|
|
|
|
done: true,
|
2020-11-08 19:38:02 -05:00
|
|
|
output: output ? output : { bounds, time, lines, bytes, distance, settings }
|
2020-04-14 09:26:59 -04:00
|
|
|
});
|
|
|
|
|
},
|
2020-02-29 20:13:49 -05:00
|
|
|
|
2020-11-09 13:57:18 -05:00
|
|
|
parse: function(args, send) {
|
|
|
|
|
const { settings, code, type } = args;
|
|
|
|
|
const origin = settings.origin;
|
|
|
|
|
const offset = {
|
|
|
|
|
x: origin.x,
|
|
|
|
|
y: -origin.y,
|
|
|
|
|
z: origin.z
|
|
|
|
|
};
|
|
|
|
|
const print = KIRI.newPrint(settings, Object.values(cache));
|
2020-11-09 14:39:08 -05:00
|
|
|
const tools = settings.device.extruders;
|
2020-11-09 18:21:59 -05:00
|
|
|
const mode = settings.mode;
|
|
|
|
|
const thin = settings.controller.thinRender || mode !== 'FDM';
|
2020-11-09 13:57:18 -05:00
|
|
|
|
|
|
|
|
const parsed = print.parseGCode(code, offset, progress => {
|
2020-11-09 14:39:08 -05:00
|
|
|
send.data({ progress: progress * 0.5 });
|
2020-11-09 13:57:18 -05:00
|
|
|
}, done => {
|
2020-11-09 14:39:08 -05:00
|
|
|
const layers = KIRI.driver.FDM.prepareRender(done.output, progress => {
|
|
|
|
|
send.data({ progress: 0.5 + progress * 0.5 });
|
2020-11-09 19:22:28 -05:00
|
|
|
}, { thin, tools, xspeed: mode === 'FDM' });
|
2020-11-09 13:57:18 -05:00
|
|
|
send.done({parsed: KIRI.codec.encode(layers)});
|
2020-11-09 18:21:59 -05:00
|
|
|
}, { fdm : mode === 'FDM' });
|
2020-02-29 20:13:49 -05:00
|
|
|
},
|
|
|
|
|
|
2020-11-09 23:01:41 -05:00
|
|
|
parse_svg: function(parsed, send) {
|
|
|
|
|
parsed.forEach(layer => {
|
|
|
|
|
layer.forEach(out => {
|
|
|
|
|
const { x, y, z } = out.point;
|
|
|
|
|
out.point = BASE.newPoint(x,y,z || 0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
const layers = KIRI.driver.FDM.prepareRender(parsed, progress => {
|
|
|
|
|
send.data({ progress });
|
|
|
|
|
}, { thin: true });
|
|
|
|
|
send.done({parsed: KIRI.codec.encode(layers)});
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-29 20:13:49 -05:00
|
|
|
clear: function(data, send) {
|
2020-06-16 11:08:50 -04:00
|
|
|
current.snap = null;
|
|
|
|
|
current.print = null;
|
2020-02-29 20:13:49 -05:00
|
|
|
if (!data.id) {
|
|
|
|
|
cache = {};
|
|
|
|
|
send.done({ clear: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let had = cache[data.id] !== undefined;
|
|
|
|
|
delete cache[data.id];
|
|
|
|
|
send.done({
|
|
|
|
|
id: data.id,
|
|
|
|
|
had: had,
|
|
|
|
|
has: cache[data.id] !== undefined
|
|
|
|
|
});
|
|
|
|
|
},
|
2020-09-14 09:43:34 -04:00
|
|
|
|
|
|
|
|
config: function(data, send) {
|
|
|
|
|
const update = {};
|
|
|
|
|
if (data.base) {
|
|
|
|
|
update.base = data.base;
|
2020-11-03 11:19:13 -05:00
|
|
|
Object.assign(BASE.config, data.base);
|
2020-09-14 09:43:34 -04:00
|
|
|
} else {
|
|
|
|
|
console.log({invalid:data});
|
|
|
|
|
}
|
|
|
|
|
send.done({config: update});
|
|
|
|
|
}
|
2020-02-29 20:13:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.onmessage = function(e) {
|
2020-11-03 11:19:13 -05:00
|
|
|
let time_recv = time(),
|
2020-02-29 20:13:49 -05:00
|
|
|
msg = e.data,
|
|
|
|
|
run = dispatch[msg.task],
|
|
|
|
|
send = {
|
|
|
|
|
data : function(data,direct) {
|
|
|
|
|
self.postMessage({
|
|
|
|
|
seq: msg.seq,
|
|
|
|
|
task: msg.task,
|
|
|
|
|
done: false,
|
|
|
|
|
data: data
|
|
|
|
|
},direct);
|
|
|
|
|
},
|
|
|
|
|
done : function(data,direct) {
|
|
|
|
|
self.postMessage({
|
|
|
|
|
seq: msg.seq,
|
|
|
|
|
task: msg.task,
|
|
|
|
|
done: true,
|
|
|
|
|
data: data
|
|
|
|
|
},direct);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (run) {
|
|
|
|
|
let time_xfer = (time_recv - msg.time),
|
|
|
|
|
output = run(msg.data, send),
|
2020-11-03 11:19:13 -05:00
|
|
|
time_send = time(),
|
2020-02-29 20:13:49 -05:00
|
|
|
time_proc = time_send - time_recv;
|
|
|
|
|
|
|
|
|
|
if (output) self.postMessage({
|
|
|
|
|
seq: msg.seq,
|
|
|
|
|
task: msg.task,
|
|
|
|
|
time_send: time_xfer,
|
|
|
|
|
time_proc: time_proc,
|
|
|
|
|
// replaced on reply side
|
2020-11-03 11:19:13 -05:00
|
|
|
time_recv: time(),
|
2020-02-29 20:13:49 -05:00
|
|
|
data: output
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.log({kiri_msg:e});
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-06-16 11:08:50 -04:00
|
|
|
|
2020-11-03 11:19:13 -05:00
|
|
|
// load kiri modules
|
|
|
|
|
KIRI.loader.forEach(fn => {
|
|
|
|
|
fn(dispatch);
|
|
|
|
|
});
|