From 9493206d29dd5b26a3036d027b45c050325a140e Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sun, 8 Nov 2020 13:04:54 -0500 Subject: [PATCH] prep cam driver for new prepare render --- src/kiri/client.js | 15 ++- src/kiri/main.js | 19 ++-- src/kiri/print.js | 34 ------- src/kiri/worker.js | 33 +++---- src/mode/cam/driver.js | 2 +- src/mode/cam/prepare.js | 34 ++++--- src/mode/fdm/client.js | 214 ++++++++++++++++++++-------------------- src/mode/fdm/prepare.js | 47 +++++---- 8 files changed, 194 insertions(+), 204 deletions(-) diff --git a/src/kiri/client.js b/src/kiri/client.js index e6ed28a4..724bffa1 100644 --- a/src/kiri/client.js +++ b/src/kiri/client.js @@ -153,14 +153,21 @@ KIRI.work = { movez: movez } }, function(reply) { - if (reply.done || reply.error) delete slicing[widget.id]; + if (reply.done || reply.error) { + delete slicing[widget.id]; + } callback(reply); }, [vertices]); }, - printSetup : function(settings, callback) { - send("printSetup", {settings:settings}, function(reply) { - callback(reply); + prepare : function(settings, update, done) { + send("prepare", {settings:settings}, function(reply) { + if (reply.progress) { + update(reply.progress, reply.message); + } + if (reply.done) { + done(reply.output); + } }); }, diff --git a/src/kiri/main.js b/src/kiri/main.js index 5f8d618e..99923130 100644 --- a/src/kiri/main.js +++ b/src/kiri/main.js @@ -705,20 +705,19 @@ setOpacity(color.preview_opacity); } - currentPrint = kiri.newPrint(settings, WIDGETS); - currentPrint.setup(true, function(update, status) { - API.show.progress(update, status); - }, function() { - if (!currentPrint) { - return setViewMode(VIEWS.ARRANGE); - } - + KIRI.client.prepare(settings, function(progress, message) { + API.show.progress(progress, message); + }, function (output) { API.show.progress(0); if (!isCam) setOpacity(0); - if (feature.preview) { + output = KIRI.codec.decode(output); + if (feature.preview && output) { STACKS.clear(); - currentPrint.render(STACKS.create('print', SPACE.platform.world)); + const stack = STACKS.create('print', SPACE.platform.world) + output.forEach(layer => { + stack.add(layer); + }); } API.event.emit('print', pMode); diff --git a/src/kiri/print.js b/src/kiri/print.js index 057d1675..f84746e7 100644 --- a/src/kiri/print.js +++ b/src/kiri/print.js @@ -212,34 +212,6 @@ scope.bytes = gcode.length; }; - PRO.setup = function(remote, onupdate, ondone) { - let print = this, - settings = print.settings, - mode = settings.mode; - - lastPoint = null; - lastEmit = null; - - if (KIRI.client) { - // executed main kiri.js - KIRI.client.printSetup(settings, function(reply) { - if (reply.done) { - print.output = KIRI.codec.decode(reply.output); - ondone(); - } else { - onupdate(reply.progress, reply.message) - } - }); - } else { - // executed from kiri-worker.js - // uses encodeOutput in this class to produce print.output above - // this encoding could be made vastly more efficient - let driver = KIRI.driver[mode]; - if (driver) driver.printSetup(print, onupdate); - else console.log({missing_print_driver: mode}); - ondone(); - } - }; // more generic. only used by SLA at the moment PRO.export = function(remote, online, ondone) { @@ -339,12 +311,6 @@ return newout; }; - PRO.render = function(stack) { - this.output.forEach(layer => { - stack.add(layer); - }); - }; - function pref(a,b) { return a !== undefined ? a : b; } diff --git a/src/kiri/worker.js b/src/kiri/worker.js index a008d6f3..82912865 100644 --- a/src/kiri/worker.js +++ b/src/kiri/worker.js @@ -106,30 +106,27 @@ KIRI.worker = { } }, - printSetup: function(data, send) { + prepare: function(data, send) { // create widget array from id:widget cache - const widgets = []; - for (let key in cache) { - if (cache.hasOwnProperty(key)) { - widgets.push(cache[key]); - } - } + const widgets = Object.values(cache); // let client know we've started send.data({update:0.05, updateStatus:"preview"}); - current.print = KIRI.newPrint(data.settings, widgets, data.id); - current.print.setup(false, function(progress, message) { - send.data({ - progress, - message - }); - }, function() { - send.done({ - done: true, - output: KIRI.codec.encode(current.print.output) - }); + 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 }); }); + + send.done({ done: true, output: KIRI.codec.encode(layers) }); }, printExport: function(data, send) { diff --git a/src/mode/cam/driver.js b/src/mode/cam/driver.js index 2bc09c84..ead7311a 100644 --- a/src/mode/cam/driver.js +++ b/src/mode/cam/driver.js @@ -4,7 +4,7 @@ (function() { - let KIRI = self.kiri, + const KIRI = self.kiri, CAM = KIRI.driver.CAM = { // init, // src/mode/cam/client.js // sliceRender, // src/mode/cam/client.js diff --git a/src/mode/cam/prepare.js b/src/mode/cam/prepare.js index c5448ce5..753e96ad 100644 --- a/src/mode/cam/prepare.js +++ b/src/mode/cam/prepare.js @@ -20,16 +20,28 @@ * @param {Number} [index] into widget array * @param {Object} [firstPoint] starting point */ - CAM.printSetup = function(print, update, index, firstPoint) { - let widgetIndex = index || 0, - widgetArray = print.widgets, - widgetCount = widgetArray.length, - widget = widgetArray[widgetIndex]; + CAM.prepare = function(widgets, settings, update) { + const count = widgets.length; + const weight = 1/count; + const print = KIRI.newPrint(settings, widgets); + print.output = []; - if (widgetIndex >= widgetCount || !widget) return; + let point; + widgets.forEach((widget, index) => { + point = prepEach(widget, settings, print, point, progress => { + update((index * weight + progress * weight) * 0.5); + }); + }); - let settings = print.settings, - device = settings.device, + print.output = KIRI.driver.FDM.prepareRender(print.output, progress => { + update(0.5 + progress * 0.5); + }, { thin: true }); + + return print.output; + }; + + function prepEach(widget, settings, print, firstPoint, update) { + let device = settings.device, process = settings.process, stock = settings.stock, outer = settings.bounds, @@ -58,7 +70,7 @@ drillDown = process.camDrillDown, drillLift = process.camDrillLift, drillDwell = process.camDrillDwell, - newOutput = widgetIndex === 0 ? [] : print.output, + newOutput = print.output || [], layerOut = [], printPoint, isNewMode, @@ -574,9 +586,7 @@ // replace output single flattened layer with all points print.output = newOutput; - if (widgetIndex + 1 < widgetCount) { - printSetup(print, update, widgetIndex + 1, printPoint); - } + return printPoint; }; /** diff --git a/src/mode/fdm/client.js b/src/mode/fdm/client.js index b2ed1352..4c4ffae8 100644 --- a/src/mode/fdm/client.js +++ b/src/mode/fdm/client.js @@ -22,112 +22,112 @@ }); } - FDM.printRender = function(print, options) { - let debug = KIRI.api.const.LOCAL; - let scope = print, emits, last, - moves, moving = true, - opt = options || {}, - tools = opt.tools || [], - showmoves = !opt.nomoves, - maxspeed = 0; - // find max speed - scope.output.forEach(function(layerout) { - layerout.forEach(function(out, index) { - if (out.emit && out.speed) { - maxspeed = Math.max(maxspeed, out.speed); - } - }); - }); - if (maxspeed === 0) { - maxspeed = 4000; - } - maxspeed *= 1.001; - // render layered output - scope.lines = 0; - scope.output.forEach(function(layerout) { - let move = [], print = [], cprint; - layerout.forEach(function(out, index) { - let point = toPoint(out.point); - if (last) { - // drop short segments - // if (point.emit === last.emit && UTIL.distSq(last, point) < 0.001 && point.z === last.z) { - // return; - // } - if (out.emit > 0) { - if (moving || !cprint) { - cprint = base.newPolygon().setOpen(true).append(last); - print.push({ - poly: cprint, - speed: out.speed || maxspeed || 4000, - tool:tools[out.tool] - }); - } - try { - cprint.append(point); - } catch (e) { - console.log(e, {cprint}); - } - moving = false; - } else { - cprint = null; - move.push(last); - move.push(point); - moving = true; - } - // move direction arrow heads - if (debug && last.z == point.z) { - let rs = BASE.newSlope( - {x: point.x, y: point.y}, - {x: last.x, y: last.y} - ); - let ao1 = BASE.newSlopeFromAngle(rs.angle + 25); - let ao2 = BASE.newSlopeFromAngle(rs.angle - 25); - let sp = BASE.newPoint(point.x, point.y, point.z); - move.push(sp); - move.push(sp.projectOnSlope(ao1, BASE.config.debug_arrow)); - move.push(sp); - move.push(sp.projectOnSlope(ao2, BASE.config.debug_arrow)); - } - } - last = point; - }); - emits = KIRI.newLayer(scope.group); - if (showmoves) { - moves = KIRI.newLayer(scope.group); - moves.lines(move, opt.move_color || 0x888888); - } - emits.setTransparent(false); - // emit printing shapes - print.forEach(segment => { - let {poly, speed, tool} = segment; - let off = tool ? (tool.extNozzle || 0.4) / 2 : 0.2; - let sint = Math.min(maxspeed, parseInt(speed)); - let rgb = scope.hsv2rgb({h:sint/maxspeed, s:1, v:1}); - let color = ((rgb.r * 0xff) << 16) | - ((rgb.g * 0xff) << 8) | - ((rgb.b * 0xff) << 0); - if (opt.flat) { - poly = poly.clone().setZ(0); - } - if (opt.aslines) { - emits.poly(poly, opt.color || color, false, true); - } else { - // first point may be from the layer below, so use second point - emits.noodle_open(poly, off - 0.02, color, 0x0, poly.getZ(1)); - } - }); - emits.renderAll(); - if (showmoves) { - moves.render(); - scope.movesView.push(moves); - } - scope.printView.push(emits); - scope.lines += print.length; - }); - } - - function toPoint(obj) { - return base.newPoint(obj.x, obj.y, obj.z); - } + // FDM.printRender = function(print, options) { + // let debug = KIRI.api.const.LOCAL; + // let scope = print, emits, last, + // moves, moving = true, + // opt = options || {}, + // tools = opt.tools || [], + // showmoves = !opt.nomoves, + // maxspeed = 0; + // // find max speed + // scope.output.forEach(function(layerout) { + // layerout.forEach(function(out, index) { + // if (out.emit && out.speed) { + // maxspeed = Math.max(maxspeed, out.speed); + // } + // }); + // }); + // if (maxspeed === 0) { + // maxspeed = 4000; + // } + // maxspeed *= 1.001; + // // render layered output + // scope.lines = 0; + // scope.output.forEach(function(layerout) { + // let move = [], print = [], cprint; + // layerout.forEach(function(out, index) { + // let point = toPoint(out.point); + // if (last) { + // // drop short segments + // // if (point.emit === last.emit && UTIL.distSq(last, point) < 0.001 && point.z === last.z) { + // // return; + // // } + // if (out.emit > 0) { + // if (moving || !cprint) { + // cprint = base.newPolygon().setOpen(true).append(last); + // print.push({ + // poly: cprint, + // speed: out.speed || maxspeed || 4000, + // tool:tools[out.tool] + // }); + // } + // try { + // cprint.append(point); + // } catch (e) { + // console.log(e, {cprint}); + // } + // moving = false; + // } else { + // cprint = null; + // move.push(last); + // move.push(point); + // moving = true; + // } + // // move direction arrow heads + // if (debug && last.z == point.z) { + // let rs = BASE.newSlope( + // {x: point.x, y: point.y}, + // {x: last.x, y: last.y} + // ); + // let ao1 = BASE.newSlopeFromAngle(rs.angle + 25); + // let ao2 = BASE.newSlopeFromAngle(rs.angle - 25); + // let sp = BASE.newPoint(point.x, point.y, point.z); + // move.push(sp); + // move.push(sp.projectOnSlope(ao1, BASE.config.debug_arrow)); + // move.push(sp); + // move.push(sp.projectOnSlope(ao2, BASE.config.debug_arrow)); + // } + // } + // last = point; + // }); + // emits = KIRI.newLayer(scope.group); + // if (showmoves) { + // moves = KIRI.newLayer(scope.group); + // moves.lines(move, opt.move_color || 0x888888); + // } + // emits.setTransparent(false); + // // emit printing shapes + // print.forEach(segment => { + // let {poly, speed, tool} = segment; + // let off = tool ? (tool.extNozzle || 0.4) / 2 : 0.2; + // let sint = Math.min(maxspeed, parseInt(speed)); + // let rgb = scope.hsv2rgb({h:sint/maxspeed, s:1, v:1}); + // let color = ((rgb.r * 0xff) << 16) | + // ((rgb.g * 0xff) << 8) | + // ((rgb.b * 0xff) << 0); + // if (opt.flat) { + // poly = poly.clone().setZ(0); + // } + // if (opt.aslines) { + // emits.poly(poly, opt.color || color, false, true); + // } else { + // // first point may be from the layer below, so use second point + // emits.noodle_open(poly, off - 0.02, color, 0x0, poly.getZ(1)); + // } + // }); + // emits.renderAll(); + // if (showmoves) { + // moves.render(); + // scope.movesView.push(moves); + // } + // scope.printView.push(emits); + // scope.lines += print.length; + // }); + // } + // + // function toPoint(obj) { + // return base.newPoint(obj.x, obj.y, obj.z); + // } })(); diff --git a/src/mode/fdm/prepare.js b/src/mode/fdm/prepare.js index 9f6f4397..a67d8c9a 100644 --- a/src/mode/fdm/prepare.js +++ b/src/mode/fdm/prepare.js @@ -15,13 +15,13 @@ /** * DRIVER PRINT CONTRACT * - * @param {Object} print state object - * @param {Function} update incremental callback + * @param {Function} update progress callback + * @returns {Object[]} returns array of render objects */ - FDM.printSetup = function(print, update) { - let widgets = print.widgets.slice(), - settings = FDM.fixExtruders(print.settings), - device = settings.device, + FDM.prepare = function(widgets, settings, update) { + settings = FDM.fixExtruders(settings); + + let device = settings.device, nozzle = device.extruders[0].extNozzle, process = settings.process, mode = settings.mode, @@ -40,7 +40,9 @@ find, layerout = [], slices = [], - sliceEntry; + sliceEntry, + print = KIRI.newPrint(settings, widgets), + isThin = settings.controller.thinRender; // TODO pick a widget with a slice on the first layer and use that nozzle // create brim, skirt, raft if specificed in FDM mode (code shared by laser) @@ -371,19 +373,26 @@ lastOut = undefined; } - print.output = render(output, progress => { + print.output = FDM.prepareRender(output, progress => { update(0.5 + progress * 0.5); - }, device.extruders); + }, { tools: device.extruders, thin: isThin }); + + return print.output; }; - function render(output, update, tools) { + FDM.prepareRender = function(levels, update, options) { + const opts = options || {}; + const tools = opts.tools; + const thin = opts.thin || false; const layers = []; - output.forEach((level, index) => { + levels.forEach((level, index) => { const prints = {}; const moves = []; - const pushPrint = (tool, poly) => { - let array = prints[tool] = prints[tool] || []; - array.width = tools[tool].extNozzle / 2; + const pushPrint = (toolid, poly) => { + toolid = toolid || 0; + const array = prints[toolid] = prints[toolid] || []; + const tool = tools[toolid]; + array.width = (tool.extNozzle || 1) / 2; array.push(poly); }; let lastOut = null; @@ -418,11 +427,13 @@ } lastOut = out; }); - const out = new KIRI.Render(); - layers.push(out); - out.setLayer('move', 0xaaaaaa).addPolys(moves); + const output = new KIRI.Render(); + layers.push(output); + output.setLayer('move', 0xaaaaaa).addPolys(moves); Object.values(prints).forEach(array => { - out.setLayer('print', 0x888800).addPolys(array, { offset: array.width, height }) + output + .setLayer('print', 0x888800) + .addPolys(array, thin ? null : { offset: array.width, height }) }) update(index / output.length); });