From e608091603345c99160e1ca69e112be081b3582c Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Tue, 30 Jul 2024 16:56:34 -0400 Subject: [PATCH] fix centering, poly scaling --- src/geo/point.js | 7 +++++++ src/geo/polygon.js | 1 + src/mesh/api.js | 8 +++++--- src/mesh/build.js | 13 ++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/geo/point.js b/src/geo/point.js index 0eef9184..f383d75a 100644 --- a/src/geo/point.js +++ b/src/geo/point.js @@ -81,6 +81,13 @@ class Point { return this; } + scale(x = 1,y = 1,z = 1) { + this.x *= x; + this.y *= y; + this.z *= z; + return this; + } + round(precision) { return newPoint( this.x.round(precision), diff --git a/src/geo/polygon.js b/src/geo/polygon.js index fb8e0a0d..a4334a26 100644 --- a/src/geo/polygon.js +++ b/src/geo/polygon.js @@ -615,6 +615,7 @@ class Polygon { * @returns {Polygon|Point} a new polygon centered on x=0, y=0, z=0 */ center(point) { + throw "does not work"; let ap = newPoint(0, 0, 0, null), np = newPolygon(), pa = this.points; diff --git a/src/mesh/api.js b/src/mesh/api.js index 5dc58088..a492cda0 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -494,11 +494,13 @@ let sketch = { sketch.selection.bounded_op((sketch, bounds, recs) => { let center = bounds.center(0); for (let rec of recs) { - rec.poly.move({ x: -center.x, y: -center.y, z:0 }); - Object.assign(rec.item, rec.poly.toObject()); + let { item } = rec; + let poly = newPolygon().fromObject(item); + poly.move({ x: -center.x, y: -center.y, z:0 }); + Object.assign(rec.item, poly.toObject()); } - selection.centerXY(); sketch.render(); + selection.centerXY(); }); }, diff --git a/src/mesh/build.js b/src/mesh/build.js index b5da21d3..cec5428e 100644 --- a/src/mesh/build.js +++ b/src/mesh/build.js @@ -8,7 +8,7 @@ gapp.register("mesh.build", [], (root, exports) => { const { broker } = gapp; -const { mesh, moto } = root; +const { base, mesh, moto } = root; const { api, util } = mesh; const { space } = moto; const { bind, div, label, input, hr } = h; @@ -873,10 +873,17 @@ function ui_build() { // polygon scaling, circle radius let el2 = bound[`item_val_${axis.toUpperCase()}_size`]; if (!el2) continue; - sel_item.type !== 'polygon' && el2.classList.add('editable'); + el2.classList.add('editable'); el2.onclick = field_edit(`${axis} size`, (nval, oval) => { if (sel_item.type === 'polygon') { - // todo + let scale = {x:1,y:1,z:1}; + let poly = base.newPolygon().fromObject(sel_item); + let bounds = poly.bounds; + scale[axis] = nval/oval; + poly.move(bounds.center().scale(-1,-1,-1)); + poly.scale(scale); + poly.move(bounds.center()); + Object.assign(sel_item, poly.toObject()); } else if (sel_item.type === 'circle') { sel_item.radius = nval / 2; }