From b85607598b93fb0ee46ea792aac23e81cba4d76a Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Tue, 24 Nov 2020 11:01:29 -0500 Subject: [PATCH] checkpoint cam animation work --- src/mode/cam/animate.js | 169 ++++++++++++++++++++++++++++++++-------- src/mode/cam/client.js | 8 ++ src/mode/cam/tool.js | 16 +++- web/kiri/index.css | 11 +++ web/kiri/index.html | 1 + 5 files changed, 172 insertions(+), 33 deletions(-) diff --git a/src/mode/cam/animate.js b/src/mode/cam/animate.js index 45c90cbc..29865a28 100644 --- a/src/mode/cam/animate.js +++ b/src/mode/cam/animate.js @@ -5,59 +5,132 @@ kiri.loader.push(function() { let KIRI = self.kiri, - BASE = self.base, CAM = KIRI.driver.CAM, - PRO = CAM.process, - newPolygon = BASE.newPolygon, - newPoint = BASE.newPoint; + API, WORLD, + meshes = {}; if (KIRI.client) - CAM.animate = function(API) { - KIRI.client.animate(API.conf.get(), data => { - const { ind, pos } = data; + CAM.animate_clear = function(api) { + WORLD = (API = api).const.SPACE.platform.world; + $('layer-animate').innerHTML = ''; + Object.keys(meshes).forEach(id => deleteMesh(id)); + } - const geo = new THREE.BufferGeometry(); - geo.setAttribute('position', new THREE.BufferAttribute(pos, 3)); - geo.setIndex(new THREE.BufferAttribute(new Uint16Array(ind), 1)); - const mat = new THREE.LineBasicMaterial({ - transparent: true, - opacity: 0.75, - color: 0 - }); - const seg = new THREE.LineSegments(geo, mat); - - API.const.SPACE.platform.world.add(seg); + if (KIRI.client) + CAM.animate = function(api) { + WORLD = (API = api).const.SPACE.platform.world; + KIRI.client.animate_setup(API.conf.get(), data => { + checkMeshCommands(data); + const UC = API.uc; + const layer = $('layer-animate'); + layer.innerHTML = ''; + UC.setGroup(layer); + UC.newRow([ + UC.newButton(null,play,{icon:''}), + UC.newButton(null,pause,{icon:''}) + ]); }); }; - if (KIRI.client) - KIRI.client.animate = function(settings, ondone) { - send("animate", {settings}, reply => { - ondone(reply); + function checkMeshCommands(data) { + if (!data) { + return; + } + if (data.mesh_add) { + const { id, ind, pos } = data.mesh_add; + addMesh(id, ind, pos); + } + if (data.mesh_del) { + deleteMesh(data.mesh_del); + } + if (data.mesh_move) { + const { id, pos } = data.mesh_move; + const mesh = meshes[id]; + mesh.position.x = pos.x; + mesh.position.y = pos.y; + mesh.position.z = pos.z; + } + } + + function addMesh(id, ind, pos) { + const geo = new THREE.BufferGeometry(); + geo.setAttribute('position', new THREE.BufferAttribute(pos, 3)); + geo.setIndex(new THREE.BufferAttribute(new Uint16Array(ind), 1)); + const mat = new THREE.LineBasicMaterial({ + transparent: true, + opacity: 0.75, + color: 0 }); + const mesh = new THREE.LineSegments(geo, mat); + WORLD.add(mesh); + meshes[id] = mesh; + } + + function updateMesh(id) { + + } + + function deleteMesh(id) { + WORLD.remove(meshes[id]); + delete meshes[id]; + } + + function play() { + KIRI.client.animate({speed: 1}, handleGridUpdate); + } + + function pause() { + KIRI.client.animate({speed: 0}, handleGridUpdate); + } + + function handleGridUpdate(data) { + checkMeshCommands(data); + } + + if (KIRI.client) + KIRI.client.animate_setup = function(settings, ondone) { + send("animate_setup", {settings}, ondone); }; + if (KIRI.client) + KIRI.client.animate = function(data, ondone) { + send("animate", data, ondone); + }; + + let path, pathIndex, grid, tool, tools, rez; + if (KIRI.worker) - KIRI.worker.animate = function(data, send) { - console.log({current}); + KIRI.worker.animate_setup = function(data, send) { const { settings } = data; + + rez = 0.5; + tools = settings.tools; + path = current.print.output.flat(); + pathIndex = 0; + const stock = settings.stock; - const center = stock.center; - const step = 1; + // const center = stock.center; + const step = rez; const stepsX = Math.floor(stock.x / step) + 1; const stepsY = Math.floor(stock.y / step) + 1; + const { pos, ind } = createGrid(stepsX, stepsY, stock, step); + + send.done({ mesh_add: { id: 0, pos, ind } }); + }; + + function createGrid(stepsX, stepsY, size, step) { const gridPoints = stepsX * stepsY; - const pos = new Float32Array(gridPoints * 3); + const pos = grid = new Float32Array(gridPoints * 3); const ind = []; - const ox = stock.x / 2; - const oy = stock.y / 2; + const ox = size.x / 2; + const oy = size.y / 2; // initialize grid points for (let x=0, ai=0; x 0) ind.appendAll([ (stepsY * x) + (y - 1), (stepsY * x) + (y ) @@ -69,7 +142,41 @@ kiri.loader.push(function() { } } - send.done({ pos, ind }); + return { pos, ind }; + } + + if (KIRI.worker) + KIRI.worker.animate = function(data, send) { + if (data.speed) { + let next = path[pathIndex++]; + if (next.tool && (!tool || tool.getNumber() !== next.tool)) { + updateTool(next.tool, send); + } + send.data({ mesh_move: { id: tool.getID(), pos: next.point }}); + } + send.done(); }; + function updateTool(toolnum, send) { + if (tool) { + send.data({ mesh_del: tool.getID() }); + } + tool = new CAM.Tool({ tools }, undefined, toolnum); + tool.generateProfile(rez); + const flen = tool.fluteLength() || 30; + // const frad = tool.fluteDiameter() / 2; + const prof = tool.profile; + const { size, pix } = tool.profileDim; + const { pos, ind } = createGrid(pix, pix, {x:size, y:size, z:flen}, rez); + const mid = Math.floor(pix/2); + // deform mesh to fit tool profile + for (let i=0, il=prof.length; i < il; ) { + const dx = mid + prof[i++]; + const dy = mid + prof[i++]; + const dz = prof[i++]; + pos[(dx * pix + dy) * 3 + 2] = -dz; + } + send.data({ mesh_add: { id:tool.getID(), pos, ind }}); + } + }); diff --git a/src/mode/cam/client.js b/src/mode/cam/client.js index b4689361..d4d25e50 100644 --- a/src/mode/cam/client.js +++ b/src/mode/cam/client.js @@ -10,6 +10,7 @@ PRO = CAM.process, newPolygon = BASE.newPolygon, newPoint = BASE.newPoint, + isAnimate, isArrange, isCamMode, camStock, @@ -34,6 +35,8 @@ api.event.on("view.set", (mode) => { isArrange = (mode === VIEWS.ARRANGE); + isAnimate = false; + CAM.animate_clear(api); }); api.event.on("settings.saved", (settings) => { @@ -72,6 +75,7 @@ }; function animate() { + isAnimate = true; API.widgets.opacity(0.75); API.hide.slider(); STACKS.clear(); @@ -79,6 +83,10 @@ } function updateStock(args, event) { + if (isAnimate) { + return; + } + STACKS.remove('bounds'); if (isCamMode && isArrange) { STACKS.clear(); diff --git a/src/mode/cam/tool.js b/src/mode/cam/tool.js index 4065a0dc..65c03b62 100644 --- a/src/mode/cam/tool.js +++ b/src/mode/cam/tool.js @@ -11,8 +11,16 @@ CAM = KIRI.driver.CAM; class Tool { - constructor(settings, id) { - this.tool = settings.tools.filter(tool => tool.id === id)[0]; + constructor(settings, id, number) { + if (number >= 0) { + this.tool = settings.tools.filter(tool => tool.number === number)[0]; + } else { + this.tool = settings.tools.filter(tool => tool.id === id)[0]; + } + } + + getID() { + return this.tool.id; } getType() { @@ -95,6 +103,10 @@ } this.profile = toolOffset; + this.profileDim = { + size: shaft_pix_float, + pix: profile_pix_iter + }; return this; } } diff --git a/web/kiri/index.css b/web/kiri/index.css index 73361707..bb69350d 100644 --- a/web/kiri/index.css +++ b/web/kiri/index.css @@ -872,6 +872,17 @@ th, tr, td, label { pointer-events: all; } +#layer-animate { + position: absolute; + left: 15px; + right: 15px; + bottom: 3px; +} +#layer-animate svg { + color: #333; + font-size: 30px; +} + #slider { display: none; height: 30px; diff --git a/web/kiri/index.html b/web/kiri/index.html index ab1c980f..6fa569ce 100644 --- a/web/kiri/index.html +++ b/web/kiri/index.html @@ -382,6 +382,7 @@
alerts
alerts
alerts
+

this site uses cookies to preserve application preferences