improve bounds tracking and output in cut modes. improve dark mode export dialog

This commit is contained in:
Stewart Allen 2024-06-12 10:31:40 -07:00
commit a15e73495f
4 changed files with 91 additions and 51 deletions

View file

@ -328,10 +328,10 @@ async function prepare(widgets, settings, update) {
} }
// compute tile width / height // compute tile width / height
for (let layerout of output) { for (let layer of output) {
let min = {w:Infinity, h:Infinity}, max = {w:-Infinity, h:-Infinity}, p; let min = {w:Infinity, h:Infinity}, max = {w:-Infinity, h:-Infinity}, p;
// compute bounding box for each layer // compute bounding box for each layer
for (let out of layerout) { for (let out of layer) {
p = out.point; p = out.point;
out.point = p.clone(); // b/c first/last point are often shared out.point = p.clone(); // b/c first/last point are often shared
min.w = Math.min(min.w, p.x); min.w = Math.min(min.w, p.x);
@ -339,10 +339,10 @@ async function prepare(widgets, settings, update) {
min.h = Math.min(min.h, p.y); min.h = Math.min(min.h, p.y);
max.h = Math.max(max.h, p.y); max.h = Math.max(max.h, p.y);
} }
layerout.w = max.w - min.w; layer.w = max.w - min.w;
layerout.h = max.h - min.h; layer.h = max.h - min.h;
// shift objects to top/left of w/h box bounds // shift objects to top/left of w/h box bounds
for (let out of layerout) { for (let out of layer) {
p = out.point; p = out.point;
p.x -= min.w; p.x -= min.w;
p.y -= min.h; p.y -= min.h;
@ -356,13 +356,13 @@ async function prepare(widgets, settings, update) {
spacing = process.ctOutTileSpacing, spacing = process.ctOutTileSpacing,
// sort objects by size when not using laser layer ordering // sort objects by size when not using laser layer ordering
c = sort ? output.sort() : output, c = sort ? output.sort() : output,
p = new kiri.Pack(dw, dh, spacing, isWire).fit(c, !sort); p = new kiri.Pack(dw, dh, spacing, {invy:isWire}).fit(c, !sort);
// test different ratios until packed // test different ratios until packed
while (!p.packed) { while (!p.packed) {
dw *= 1.1; dw *= 1.1;
dh *= 1.1; dh *= 1.1;
p = new kiri.Pack(dw, dh, spacing, isWire).fit(c ,!sort); p = new kiri.Pack(dw, dh, spacing, {invy:isWire}).fit(c ,!sort);
} }
// update packed tile with new location // update packed tile with new location
@ -392,62 +392,91 @@ function exportLaser(print, online, ondone) {
* *
*/ */
function exportElements(settings, output, onpre, onpoly, onpost, onpoint, onlayer) { function exportElements(settings, output, onpre, onpoly, onpost, onpoint, onlayer) {
let process = settings.process, let { process, device } = settings,
zcolor = process.ctOutZColor, { bedWidth, bedDepth } = device,
{ ctOriginCenter, ctOriginBounds } = process,
{ outputInvertX, outputInvertY } = process,
{ ctOriginOffX, ctOriginOffY } = process,
last, last,
point, point,
poly = [], poly = [],
off = {x:ctOriginOffX||0, y:ctOriginOffY||0},
min = {x:0, y:0}, min = {x:0, y:0},
max = {x:0, y:0}; max = {x:0, y:0},
size = {w:0, h:0};
output.forEach(function(layer) { // compute bounds for entire output
layer.forEach(function(out) { output.forEach(layer => {
layer.forEach(out => {
point = out.point; point = out.point;
if (process.outputInvertX) point.x = -point.x; if (outputInvertX) point.x = -point.x;
if (process.outputInvertY) point.y = -point.y; if (outputInvertY) point.y = -point.y;
min.x = Math.min(min.x, point.x); min.x = Math.min(min.x, point.x);
max.x = Math.max(max.x, point.x); max.x = Math.max(max.x, point.x);
min.y = Math.min(min.y, point.y); min.y = Math.min(min.y, point.y);
max.y = Math.max(max.y, point.y); max.y = Math.max(max.y, point.y);
}); });
}); });
size.w = max.x - min.x;
size.h = max.y - min.y;
console.log('export bounds', min, max, size);
if (!process.ctOriginCenter) { let bounds = base.newBounds();
// normalize against origin lower left if (ctOriginCenter) {
output.forEach(function(layer) { // place origin at geometric center of all points
layer.forEach(function(out) { // regardless of workspace size
for (let layer of output) {
for (let out of layer) {
point = out.point; point = out.point;
point.x -= min.x; point.x = point.x - min.x - size.w / 2 + off.x;
point.y -= min.y; point.y = point.y - min.y - size.h / 2 + off.y;
}); bounds.update(point);
}); }
max.x = max.x - min.x; }
max.y = max.y - min.y; max.x = max.x - min.x + off.x;
max.y = max.y - min.y + off.y;
min.x = 0;
min.y = 0;
} else if (ctOriginBounds) {
// place origin at min x,y of all points
for (let layer of output) {
for (let out of layer) {
point = out.point;
point.x = point.x - min.x + ctOriginOffX;
point.y = point.y - min.y + ctOriginOffY;
bounds.update(point);
}
}
max.x = max.x - min.x + off.x;
max.y = max.y - min.y + off.y;
min.x = 0; min.x = 0;
min.y = 0; min.y = 0;
} else { } else {
// normalize against center of build area // place origin at min x,y taking into account
let w = settings.device.bedWidth; // each layer's relative position
let h = settings.device.bedDepth; let w = bedWidth;
output.forEach(function(layer) { let h = bedDepth;
layer.forEach(function(out) { for (let layer of output) {
for (let out of layer) {
point = out.point; point = out.point;
point.x += w/2; point.x += w / 2 + off.x;
point.y += h/2; point.y += h / 2 + off.y;
}); bounds.update(point);
}); }
max.x = w; }
max.y = h; max.x = w + off.x;
max.y = h + off.y;
min.x = 0; min.x = 0;
min.y = 0; min.y = 0;
} }
onpre(min, max, process.ctOutPower, process.ctOutSpeed); onpre(min, max, process.ctOutPower, process.ctOutSpeed);
output.forEach(function(layer, index) { // output each layer with moves between them
output.forEach((layer, index) => {
let thick = 0; let thick = 0;
let color = 0; let color = 0;
layer.forEach(function(out, li) { layer.forEach((out, li) => {
thick = out.thick; thick = out.thick;
point = out.point; point = out.point;
if (onlayer) { if (onlayer) {

View file

@ -12,37 +12,47 @@ gapp.register("kiri.pack", [], (root, exports) => {
const { kiri } = root; const { kiri } = root;
class Packer { class Packer {
constructor(w, h, spacing, inv) { constructor(w, h, spacing, opt = {}) {
this.root = { x: 0, y: 0, w: w, h: h }; this.root = { x: 0, y: 0, w: w, h: h };
this.max = { w: 0, h: 0 }; this.max = { w: 0, h: 0 };
this.packed = false; this.packed = false;
this.spacing = typeof(spacing) === 'number' ? spacing : 1; this.spacing = typeof(spacing) === 'number' ? spacing : 1;
this.inv = inv; this.opt = opt;
this.pad = this.spacing / 2; this.pad = this.spacing / 2;
} }
// array of blocks/tiles with {w,h} properties
simple(blocks) { simple(blocks) {
let n = 0, block, x = 0, y = 0, mh = 0; let n = 0, block, x = 0, y = 0, mh = 0, maxx = 0, maxy = 0;
let spacing = blocks.length > 1 ? this.spacing : 0; let spacing = blocks.length > 1 ? this.spacing : 0;
while (n < blocks.length) { while (n < blocks.length) {
block = blocks[n++]; block = blocks[n++];
block.fit = { x, y }; block.fit = { x, y };
this.max.w = Math.max(this.max.w, x + block.w); this.max.w = Math.max(this.max.w, x + block.w);
this.max.h = Math.max(this.max.h, y + block.h); this.max.h = Math.max(this.max.h, y + block.h);
maxx = Math.max(maxx, x);
maxy = Math.max(maxy, y);
mh = Math.max(mh, block.h); mh = Math.max(mh, block.h);
x += block.w + spacing; x += block.w + spacing;
if (x > this.root.w) { if (x > this.root.w) {
x = 0; x = 0;
y += ((mh + spacing) * (this.inv ? -1 : 1)); y = y + mh + spacing;
mh = 0; mh = 0;
} }
if (this.inv && (y < -this.root.h)) {
return this;
}
if (y > this.root.h) { if (y > this.root.h) {
return this; return this;
} }
} }
if (this.opt.invx) {
for (let block of blocks) {
block.fit.x = maxx - block.fit.x;
}
}
if (this.opt.invy) {
for (let block of blocks) {
block.fit.y = maxy - block.fit.y;
}
}
this.packed = true; this.packed = true;
return this; return this;
} }

View file

@ -93,16 +93,15 @@ function platformUpdateOrigin(update_bounds = true) {
origin.x += process.camOriginOffX; origin.x += process.camOriginOffX;
origin.y += process.camOriginOffY; origin.y += process.camOriginOffY;
origin.z += process.camOriginOffZ; origin.z += process.camOriginOffZ;
if (isIndexed) {
origin.y = 0;
}
} }
// WireEDM origin offsets // WireEDM origin offsets
if (MODE === MODES.WEDM) { if (MODE === MODES.WEDM) {
origin.x -= process.ctOriginOffX; origin.x -= process.ctOriginOffX;
origin.y -= process.ctOriginOffY; origin.y += process.ctOriginOffY;
}
if (isIndexed) {
origin.y = 0;
} }
space.platform.setRulers(ruler, ruler, 1 / api.view.unit_scale(), 'X', isBelt ? 'Z' : 'Y'); space.platform.setRulers(ruler, ruler, 1 / api.view.unit_scale(), 'X', isBelt ? 'Z' : 'Y');

View file

@ -275,7 +275,9 @@ th, tr, td, label {
.dark #slider-max, .dark #slider-max,
.dark .mdialog, .dark .mdialog,
.dark .t-body, .dark .t-body,
.dark .cam-pop-op .dark .cam-pop-op,
.dark #mod-print,
.dark #mod-print .header label
{ {
background-color: #222; background-color: #222;
color: white; color: white;