/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */ "use strict"; let gs_kiri_sla = exports; (function() { if (!self.kiri) self.kiri = { }; if (!self.kiri.driver) self.kiri.driver = { }; if (self.kiri.driver.SLA) return; let KIRI = self.kiri, BASE = self.base, DBUG = BASE.debug, UTIL = BASE.util, CONF = BASE.config, POLY = BASE.polygons, SLA = KIRI.driver.SLA = { slice, printSetup, printExport, printDownload, printRender }, SLICER = KIRI.slicer, newPoint = BASE.newPoint; /** * DRIVER SLICE CONTRACT - runs in worker * * @param {Object} settings * @param {Widget} Widget * @param {Function} onupdate (called with % complete and optional message) * @param {Function} ondone (called when complete with an array of Slice objects) */ function slice(settings, widget, onupdate, ondone) { let process = settings.process, device = settings.device; // calculate % complete and call onupdate() function doupdate(slices, index, from, to, msg) { onupdate(0.5 + (from + ((index / slices.length) * (to - from))) * 0.5, msg); } // for each slice, performe a function and call doupdate() function forSlices(slices, from, to, fn, msg) { slices.forEach(function(slice) { fn(slice); doupdate(slices, slice.index, from, to, msg) }); } SLICER.sliceWidget(widget, { height: process.slaSlice || 0.05 }, function(slices) { widget.slices = slices; // reset for solids and support projections slices.forEach(function(slice) { slice.invalidateFill(); slice.invalidateSolids(); slice.invalidateSupports(); slice.isSolidFill = false; }); forSlices(slices, 0.0, 0.2, (slice) => { slice.doShells(1, 0); // slice.tops.forEach(top => { top.solids = [] }); }, "slice"); forSlices(slices, 0.2, 0.4, (slice) => { slice.doDiff(0.00001, 0.005); }, "delta"); forSlices(slices, 0.4, 0.5, (slice) => { slice.projectFlats(process.slaSolids || 5); slice.projectBridges(process.slaSolids || 5); }, "project"); forSlices(slices, 0.5, 0.6, (slice) => { slice.doSolidsFill(undefined, undefined, 0.00001); }, "solid"); ondone(); }, function(update) { return onupdate(0.0 + update * 0.5); }); }; /** * DRIVER PRINT CONTRACT - runs in worker * @param {Object} print state object * @param {Function} update incremental callback */ function printSetup(print, update) { let widgets = print.widgets, settings = print.settings, device = settings.device, process = settings.process, output = print.images = [], layermax = 0, width = 2560, height = 1440, width2 = width/2, height2 = height/2, scaleX = width / device.bedWidth, scaleY = height / device.bedDepth; widgets.forEach(widget => { layermax = Math.max(widget.slices.length); }); function polyout(poly, ctx) { poly.forEachPoint((p,i) => { if (i === 0) { ctx.moveTo(p.y * scaleY + height2, p.x * scaleX + width2); } else { ctx.lineTo(p.y * scaleY + height2, p.x * scaleX + width2); } }, true); ctx.closePath(); } for (let index=0; index < layermax; index++) { let layer = new OffscreenCanvas(height,width); let ctx = layer.getContext('2d'); ctx.fillStyle = 'rgb(200, 0, 0)'; let count = 0; widgets.forEach(widget => { let slice = widget.slices[index]; if (slice) { slice.tops.map(top => top.poly).forEach(poly => { ctx.beginPath(); polyout(poly.setClockwise(), ctx); if (poly.inner) { poly.inner.forEach(inner => { polyout(inner.setCounterClockwise(), ctx); }); } ctx.fill(); count++; }); } }); output.push(ctx.getImageData(0,0,height,width)); update(index / layermax); if (count === 0) break; } update(1); }; /** * DRIVER PRINT CONTRACT - runs in worker * @param {Object} print state object * @param {Function} online streaming reply * @param {Function} ondone last reply */ function printExport(print, online, ondone) { let widgets = print.widgets, settings = print.settings, device = settings.device, process = settings.process, output = print.output; print.images.forEach(image => { online(image.data, [image.data.buffer]); }); ondone({width:2560,height:1440}); }; // runs in browser main function printRender(print) { let widgets = print.widgets, settings = print.settings, device = settings.device, process = settings.process; for (let index=0; ; index++) { let layer = KIRI.newLayer(print.group); print.printView.push(layer); let count = 0; widgets.forEach(widget => { let slice = widget.slices[index]; if (!slice) { return; } slice.tops.map(top => top.poly).forEach(poly => { layer.poly(poly, 0x888888, true, false); count++; }); }); layer.render(); if (count === 0) { // TODO fix with contract for exposing layer count // hack uses expected gcode output array in print object print.output = print.printView; break; } } } // runs in browser main function printDownload(print) { let { API, lines, done } = print.sla; let filename = `print-${new Date().getTime().toString(36)}`; API.ajax("/kiri/output-sla.html", html => { API.ui.print.innerHTML = html; $('print-filename').value = filename; $('print-layers').value = lines.length; $('print-close').onclick = API.modal.hide; $('print-photons').onclick = () => { download_photons(print) }; // $('print-photon').onclick = () => { download_photon(print) }; // $('print-pws').onclick = () => { download_pws(print) }; let canvas = $('print-canvas'); let ctx = canvas.getContext('2d'); let img = ctx.createImageData(done.height, done.width); let imgDV = new DataView(img.data.buffer); let range = $('print-range'); range.value = 0; range.min = 0; range.max = lines.length - 1; range.oninput = function() { let lineDV = new DataView(lines[range.value].buffer); for (let i=0; i { let count = line.length / 4; let bits = new Uint8Array(line.length / 4); let bitsDV = new DataView(bits.buffer); let lineDV = new DataView(line.buffer); // reduce RGB to R for (let i = 0; i < count; i++) { // defeat anti-aliasing for the moment bitsDV.setUint8(i, lineDV.getUint8(i * 4) > 0 ? 1 : 0); } return { subs: [{ exposureTime: process.slaLayerOn, data: bits }] }; }); let layers = encodeLayers(converted, "photons"); let filebuf = new ArrayBuffer(75366 + layers.length + 28 * layerCount); let filedat = new DataView(filebuf); let filePos = 0; filedat.setUint32 (0, 2, false); filedat.setUint32 (4, 3227560, false); filedat.setUint32 (8, 824633720, false); filedat.setUint16 (12, 10, false); filedat.setFloat64(14, process.slaSlice, false); filedat.setFloat64(22, process.slaLayerOn, false); filedat.setFloat64(30, process.slaLayerOff, false); filedat.setFloat64(38, process.slaBaseOn, false); filedat.setUint32 (46, process.slaBaseLayers, false); filedat.setFloat64(50, process.slaPeelDist, false); filedat.setFloat64(58, process.slaPeelLift, false); filedat.setFloat64(66, process.slaPeelDrop, false); filedat.setFloat64(74, 69420, false); filedat.setUint32 (82, 224, false); filedat.setUint32 (86, 42, false); filedat.setUint32 (90, 168, false); filedat.setUint32 (94, 10, false); filedat.setUint32 (75362, layerCount, false); filePos = 75366; for (let i = 0; i < layerCount; i++) { let layer = layers.layers[i], sublayer = layer.sublayers[0], numbytes = sublayer.length; filedat.setUint32 (filePos + 0, 69420, false); filedat.setFloat64(filePos + 4, 0); filedat.setUint32 (filePos + 12, height, false); filedat.setUint32 (filePos + 16, width, false); filedat.setUint32 (filePos + 20, numbytes * 8 + 32, false); filedat.setUint32 (filePos + 24, 2684702720, false); filePos += 28; for (let j = 0; j < numbytes; j++) { filedat.setUint8(filePos + j, sublayer[j]); } filePos += numbytes; } saveAs( new Blob([filebuf], { type: "application/octet-stream" }), $('print-filename').value + ".photons"); print.sla.API.modal.hide(); } function download_photon() { } function download_pws() { } function encodeLayers(input, type) { let layers = [], length = 0; for (let index = 0; index < input.length; index++) { let subs = input[index].subs, sublayers = [], sublength = 0; for (let subindex = 0; subindex < subs.length; subindex++) { let data = subs[subindex].data; let encoded = rleEncode(data, type); sublength += encoded.length; sublayers.push(encoded); if (type == "photons") break; } length += sublength; layers.push({ sublength, sublayers }); } return { length, layers }; } function rleEncode(data, type) { let maxlen = (type === 'photons') ? 128 : 125, color = data[0], runlen = 1, output = []; for (let index = 1; index < data.length; index++) { let newColor = data[index]; if (newColor !== color) { output.push(rleByte(color, runlen, type)); color = newColor; runlen = 1; } else { if (runlen === maxlen) { output.push(rleByte(color, runlen, type)); runlen = 1; } else { runlen++; } } } if (runlen > 0) { output.push(rleByte(color, runlen, type)); } return output; } function rleByte(color, length, type) { switch (type) { case 'pws': case 'photon': return length | (color * 128); case 'photons': length--; return (length & 1 ? 128 : 0) | (length & 2 ? 64 : 0) | (length & 4 ? 32 : 0) | (length & 8 ? 16 : 0) | (length & 16 ? 8 : 0) | (length & 32 ? 4 : 0) | (length & 64 ? 2 : 0) | color; } } function rleDecode(data) { let bytes = []; for (let i = 0; i < data.length; i++) { let val = data[i], col = val & 1, count = ((val & 128 ? 1 : 0) | (val & 64 ? 2 : 0) | (val & 32 ? 4 : 0) | (val & 16 ? 8 : 0) | (val & 8 ? 16 : 0) | (val & 4 ? 32 : 0) | (val & 2 ? 64 : 0)) + 1; for (let j = 0; j < count; j++) { bytes.push(col); } } return bytes; } })();