fix cam 2d animation

This commit is contained in:
Stewart Allen 2025-07-05 13:30:06 -04:00
commit ee78dd41e7
5 changed files with 142 additions and 131 deletions

View file

@ -2,82 +2,79 @@
// WORKER BACK END ANIMATION CODE for 2D
import { Tool } from './tool.js';
const asPoints = false;
const asLines = false;
let stock, center, grid, gridX, gridY, rez;
let path, pathIndex, tool, tools, last, toolID = 1;
kiri.worker.animate_setup = function(data, send) {
const { settings } = data;
const { process } = settings;
const print = worker.print;
const density = parseInt(settings.controller.animesh) * 1000;
export function init(worker) {
const { dispatch } = worker;
pathIndex = 0;
path = print.output.flat();
tools = settings.tools;
stock = settings.stock;
rez = 1/Math.sqrt(density/(stock.x * stock.y));
dispatch.animate_setup = function(data, send) {
const { settings } = data;
const { process } = settings;
const print = worker.current.print;
const density = parseInt(settings.controller.animesh) * 1000;
pathIndex = 0;
path = print.output.flat();
tools = settings.tools;
stock = settings.stock;
rez = 1/Math.sqrt(density/(stock.x * stock.y));
//destructure arcs into path points
path = path.map(o=>
o.arcPoints
? [
...o.arcPoints.map(point=>
({
...o,
point
})
),
o
]
: o
)
.flat();
// destructure arcs into path points
path = path.map(o =>
o.arcPoints ? [ ...o.arcPoints.map(point => ({ ...o, point })), o ] : o
).flat();
const step = rez;
const stepsX = Math.floor(stock.x / step);
const stepsY = Math.floor(stock.y / step);
const { pos, ind, sab } = createGrid(stepsX, stepsY, stock, step, true);
const offset = {
x: process.camOriginCenter ? 0 : stock.x / 2,
y: process.camOriginCenter ? 0 : stock.y / 2,
z: process.camOriginTop ? -stock.z : 0
}
const step = rez;
const stepsX = Math.floor(stock.x / step);
const stepsY = Math.floor(stock.y / step);
const { pos, ind, sab } = createGrid(stepsX, stepsY, stock, step, true);
const offset = {
x: process.camOriginCenter ? 0 : stock.x / 2,
y: process.camOriginCenter ? 0 : stock.y / 2,
z: process.camOriginTop ? -stock.z : 0
}
grid = pos;
gridX = stepsX;
gridY = stepsY;
grid = pos;
gridX = stepsX;
gridY = stepsY;
tool = null;
last = null;
animating = false;
animateClear = false;
tool = null;
last = null;
animating = false;
animateClear = false;
center = Object.assign({}, stock.center);
center.z -= stock.z / 2;
center = Object.assign({}, stock.center);
center.z -= stock.z / 2;
send.data({ mesh_add: { id: 0, ind, offset, sab } }, [ ]); // sab not transferrable
send.data({ mesh_move: { id: 0, pos: center } });
send.done();
};
send.data({ mesh_add: { id: 0, ind, offset, sab } }, [ ]); // sab not transferrable
send.data({ mesh_move: { id: 0, pos: center } });
send.done();
};
kiri.worker.animate = function(data, send) {
renderPause = data.pause || renderPause;
renderSpeed = data.speed || 0;
if (animating) {
return send.done();
}
renderSteps = data.steps || 1;
renderDone = false;
animating = renderSpeed > 0;
renderPath(send);
};
dispatch.animate = function(data, send) {
renderPause = data.pause || renderPause;
renderSpeed = data.speed || 0;
if (animating) {
return send.done();
}
renderSteps = data.steps || 1;
renderDone = false;
animating = renderSpeed > 0;
renderPath(send);
};
kiri.worker.animate_cleanup = function(data, send) {
if (animating) {
animateClear = true;
}
};
dispatch.animate_cleanup = function(data, send) {
if (animating) {
animateClear = true;
}
};
}
function createGrid(stepsX, stepsY, size, step, stock) {
const gridPoints = stepsX * stepsY;
@ -290,7 +287,7 @@ function updateTool(toolobj, send) {
if (tool) {
send.data({ mesh_del: toolID });
}
tool = new CAM.Tool({ tools }, toolobj.getID());
tool = new Tool({ tools }, toolobj.getID());
tool.generateProfile(rez);
const flen = tool.fluteLength() || 15;
const slen = tool.shaftLength() || 15;

View file

@ -30,63 +30,67 @@ let renderSpeed = 0;
let indexCount = 0;
let updates = 0;
kiri.worker.animate_setup2 = function (data, send) {
const { settings } = data;
const { process } = settings;
const print = worker.print;
const density = parseInt(settings.controller.animesh) * 1000;
const isIndexed = process.camStockIndexed;
export function init(worker) {
const { dispatch } = worker;
pathIndex = 0;
path = print.output.flat();
tools = settings.tools;
stock = settings.stock;
rez = 1 / Math.sqrt(density / (stock.x * stock.y));
dispatch.animate_setup2 = function (data, send) {
const { settings } = data;
const { process } = settings;
const print = worker.current.print;
const density = parseInt(settings.controller.animesh) * 1000;
const isIndexed = process.camStockIndexed;
tool = null;
last = null;
animating = false;
animateClear = false;
stockIndex = 0;
indexCount = 0;
startTime = 0;
updates = 0;
stockZ = isIndexed ? 0 : stock.z;
pathIndex = 0;
path = print.output.flat();
tools = settings.tools;
stock = settings.stock;
rez = 1 / Math.sqrt(density / (stock.x * stock.y));
stockSlices = [];
const { x, y, z } = stock;
const sliceCount = parseInt(settings.controller.animesh || 2000) / 100;
const sliceWidth = stock.x / sliceCount;
for (let i = 0; i < sliceCount; i++) {
let xmin = -(x / 2) + (i * sliceWidth) + sliceWidth / 2;
let slice = new Stock(sliceWidth, y, z).translate(xmin, 0, 0);
stockSlices.push(slice);
slice.updateMesh([]);
slice.send(send);
// send({ mesh_move: { id: slice.id, pos: { x:0, y:0, z: stock.z/2} } });
}
tool = null;
last = null;
animating = false;
animateClear = false;
stockIndex = 0;
indexCount = 0;
startTime = 0;
updates = 0;
stockZ = isIndexed ? 0 : stock.z;
send.done();
};
stockSlices = [];
const { x, y, z } = stock;
const sliceCount = parseInt(settings.controller.animesh || 2000) / 100;
const sliceWidth = stock.x / sliceCount;
for (let i = 0; i < sliceCount; i++) {
let xmin = -(x / 2) + (i * sliceWidth) + sliceWidth / 2;
let slice = new Stock(sliceWidth, y, z).translate(xmin, 0, 0);
stockSlices.push(slice);
slice.updateMesh([]);
slice.send(send);
// send({ mesh_move: { id: slice.id, pos: { x:0, y:0, z: stock.z/2} } });
}
kiri.worker.animate2 = function (data, send) {
renderPause = data.pause || renderPause;
renderSpeed = data.speed || 0;
if (animating) {
return send.done();
}
renderSteps = data.steps || 1;
renderDone = false;
animating = renderSpeed > 0;
startTime = startTime || Date.now();
renderPath(send);
};
send.done();
};
kiri.worker.animate_cleanup2 = function (data, send) {
if (animating) {
animateClear = true;
}
};
dispatch.animate2 = function (data, send) {
renderPause = data.pause || renderPause;
renderSpeed = data.speed || 0;
if (animating) {
return send.done();
}
renderSteps = data.steps || 1;
renderDone = false;
animating = renderSpeed > 0;
startTime = startTime || Date.now();
renderPath(send);
};
dispatch.animate_cleanup2 = function (data, send) {
if (animating) {
animateClear = true;
}
};
}
function renderPath(send) {
if (renderDone) {

View file

@ -6,6 +6,9 @@ import { cam_prepare } from './prepare.js';
import { cam_export } from './export.js';
import { tool as mesh_tool } from '../../mesh/tool.js';
import { init as init_2d } from './anim-2d-be.js';
import { init as init_3d } from './anim-3d-be.js';
function surface_prep(widget, index) {
if (!widget.tool) {
let tool = widget.tool = new mesh_tool();
@ -20,7 +23,13 @@ function surface_find(widget, faces, radians) {
};
function init(worker) {
worker.cam_surfaces = function(data, send) {
const { dispatch } = worker;
init_2d(worker);
init_3d(worker);
dispatch.cam_surfaces = function(data, send) {
const { settings, index } = data;
const widgets = Object.values(worker.cache);
for (let widget of widgets) {
@ -36,14 +45,14 @@ function init(worker) {
send.done({});
};
worker.cam_surface_find = function(data, send) {
dispatch.cam_surface_find = function(data, send) {
const { id, face, radians } = data;
const widget = worker.cache[id];
const faces = surface_find(widget, [face], radians);
send.done(faces);
}
worker.cam_traces = async function(data, send) {
dispatch.cam_traces = async function(data, send) {
const { settings, single } = data;
const widgets = Object.values(worker.cache);
const fresh = [];
@ -59,7 +68,7 @@ function init(worker) {
} } )));
};
worker.cam_traces_clear = function(data, send) {
dispatch.cam_traces_clear = function(data, send) {
for (let widget of Object.values(worker.cache)) {
delete widget.traces;
delete widget.topo;
@ -67,7 +76,7 @@ function init(worker) {
send.done({});
};
worker.cam_holes = async function(data, send) {
dispatch.cam_holes = async function(data, send) {
const { settings, indiv, rec } = data;
const widgets = Object.values(worker.cache);
const fresh = [];

View file

@ -22,7 +22,7 @@ function extrudeMM(dist, perMM, factor) {
// defer loading until client and worker exist
function init(worker) {
worker.fdm_support_generate = function(data, send) {
worker.dispatch.fdm_support_generate = function(data, send) {
const { settings } = data;
const widgets = Object.values(worker.cache);
const fresh = widgets.filter(widget => FDM.supports(settings, widget));

View file

@ -744,16 +744,17 @@ dispatch.onmessage = self.onmessage = async function(e) {
}
};
self.kiri_worker = {
const worker = self.kiri_worker = {
cache: wcache,
current,
dispatch,
drivers,
version,
minions: minwork
};
// initilize driver mode handlers
CAM.init(dispatch);
FDM.init(dispatch);
CAM.init(worker);
FDM.init(worker);
broker.publish("worker.started", { dispatch, minions: minwork });