From 2af361fc5ae8d794ebad93b411d0f0fc577b678f Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Tue, 1 Nov 2022 19:55:30 -0400 Subject: [PATCH] work on getting index translated widget geometry --- src/kiri-mode/cam/client.js | 4 +++- src/kiri-mode/cam/ops.js | 3 ++- src/kiri-mode/cam/slice.js | 13 +++++++++++ src/kiri-mode/cam/slicer.js | 3 +-- src/kiri/codec.js | 6 +++-- src/kiri/layers.js | 10 +++++++-- src/kiri/stack.js | 10 ++++----- src/kiri/widget.js | 45 +++++++++++++++++-------------------- 8 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/kiri-mode/cam/client.js b/src/kiri-mode/cam/client.js index 0d03d1ef..0fb7ea82 100644 --- a/src/kiri-mode/cam/client.js +++ b/src/kiri-mode/cam/client.js @@ -1761,7 +1761,9 @@ function updateStock(args, event) { const {x, y, z} = stock; if (x && y && z && !STACKS.getStack('bounds')) { - const render = new kiri.Layers().setLayer('bounds', { face: 0xaaaaaa, line: 0xaaaaaa, index: currentIndex }); + const render = new kiri.Layers() + .setRotation(currentIndex) + .setLayer('bounds', { face: 0xaaaaaa, line: 0xaaaaaa }); const stack = STACKS.setFreeMem(false).create('bounds', SPACE.world); const hx = x/2, hy = y/2; const sz = stock.z || 0; diff --git a/src/kiri-mode/cam/ops.js b/src/kiri-mode/cam/ops.js index 4fae17e0..7466b069 100644 --- a/src/kiri-mode/cam/ops.js +++ b/src/kiri-mode/cam/ops.js @@ -1036,7 +1036,6 @@ class OpPocket extends CamOp { } function newSliceOut(z) { let slice = newSlice(z); - addSlices(slice); sliceOut.push(slice); return slice; } @@ -1109,10 +1108,12 @@ class OpPocket extends CamOp { progress(zpro, "pocket"); } zpro += zinc; + addSlices(slice); } } } let surfaces = op.surfaces[widget.id] || []; + // let vert = widget.getGeoVertices(true,true).map(v => v.round(4)); let vert = widget.getVertices().array.map(v => v.round(4)); let outline = []; let faces = CAM.surface_find(widget, surfaces); diff --git a/src/kiri-mode/cam/slice.js b/src/kiri-mode/cam/slice.js index b89c86b1..347c8c3d 100644 --- a/src/kiri-mode/cam/slice.js +++ b/src/kiri-mode/cam/slice.js @@ -139,6 +139,7 @@ CAM.slice = function(settings, widget, onupdate, ondone) { function setAxisIndex(degrees) { axisIndex = degrees; + // state.axisIndex = (Math.PI / 180) * axisIndex; } function addSlices(slices) { @@ -213,6 +214,18 @@ CAM.slice = function(settings, widget, onupdate, ondone) { op.slice((progress, message) => { onupdate((opSum + (progress * weight)) / opTot, message || op.type()); }); + // setup new state when indexing the workspace + if (false && op.op.type === "index") { + let points = base.verticesToPoints(widget.getGeoVertices(true, true)); + state.slicer = new kiri.cam_slicer(points, { + zlist: true, + zline: true, + nobucket: true // b/c negative Z when rotated + }); + new CAM.OPS.shadow(state, { type: "shadow", silent: true }).slice(progress => { + console.log('reshadow', progress.round(3)); + }); + } camOps.push(op); opSum += weight; } diff --git a/src/kiri-mode/cam/slicer.js b/src/kiri-mode/cam/slicer.js index 859d325f..a1c4517b 100644 --- a/src/kiri-mode/cam/slicer.js +++ b/src/kiri-mode/cam/slicer.js @@ -131,8 +131,7 @@ class Slicer { let bucketCount = Math.max(1, Math.ceil(zMax / (zSum / points.length)) - 1); let zScale = this.zScale = 1 / (zMax / bucketCount); let buckets = this.buckets = []; - - if (bucketCount > 1) { + if (bucketCount > 1 && !this.options.nobucket) { // create empty buckets for (let i = 0; i <= bucketCount + 1; i++) { buckets.push([]); diff --git a/src/kiri/codec.js b/src/kiri/codec.js index c2a2ae75..ffe26de0 100644 --- a/src/kiri/codec.js +++ b/src/kiri/codec.js @@ -400,7 +400,8 @@ kiri.Layers.prototype.encode = function(state) { faces: codec.allocFloat32Array(paths.faces, zeros), norms: codec.allocFloat32Array(paths.norms, zeros) } : undefined, - cpath: layer.cpath || codec.undef + cpath: layer.cpath || codec.undef, + rotation: layer.rotation } }) }; @@ -430,7 +431,8 @@ registerDecoder(TYPE.LAYERS, function(v, state) { faces: codec.allocFloat32Array(paths.faces), norms: codec.allocFloat32Array(paths.norms) } : undefined, - cpath: data.cpath + cpath: data.cpath, + rotation: data.rotation }; // fixup null -> Infinity in material counts (JSON stringify sucks) if (d.cface) { diff --git a/src/kiri/layers.js b/src/kiri/layers.js index a2fd8add..6d9f762e 100644 --- a/src/kiri/layers.js +++ b/src/kiri/layers.js @@ -40,6 +40,12 @@ class Layers { return this.layers[layer]; } + // in radians + setRotation(x = 0, y = 0, z = 0) { + this.rotation = { x, y, z }; + return this; + } + setLayer(layer, colors, off) { let layers = this.layers; if (typeof(colors) === 'number') { @@ -50,6 +56,7 @@ class Layers { }; } this.current = layers[layer] = layers[layer] || { + rotation: this.rotation, off: off === true, lines: [], polys: [], // colors are an attribute on polygons @@ -59,8 +66,7 @@ class Layers { paths: undefined, // 3d extrusion / tube paths (used to be an array, now merged - FIX) cpath: undefined, // path colors indices color: colors || { - index: 0, - check: 0, + check: 0, // unused? line: 0, face: 0, opacity: 1 diff --git a/src/kiri/stack.js b/src/kiri/stack.js index 551c6162..1f919c66 100644 --- a/src/kiri/stack.js +++ b/src/kiri/stack.js @@ -106,8 +106,8 @@ class Stack { } } - const { polys, lines, faces, cface, paths, cpath, color, off, norms } = layer; - const { index, opacity } = color; + const { polys, lines, faces, cface, paths, cpath, color, off, norms, rotation } = layer; + const { opacity } = color; const meshes = []; const defstate = !off; const mats = []; @@ -155,7 +155,7 @@ class Stack { } geo.setFromPoints(vert); const segs = new THREE.LineSegments(geo, mat); - if (index) segs.rotation.x = index; + if (rotation) segs.rotation.x = rotation.x; meshes.push(segs); group.add(segs); mats.appendAll(mat); @@ -186,7 +186,7 @@ class Stack { geo.addGroup(0, Infinity, 0); } const mesh = new THREE.Mesh(geo, mat); - if (index) mesh.rotation.x = index; + if (rotation) mesh.rotation.x = rotation.x; // mesh.castShadow = true; // mesh.receiveShadow = true; meshes.push(mesh); @@ -221,7 +221,7 @@ class Stack { } const mesh = new THREE.Mesh(geo, mat); mesh.position.z = z; - if (index) mesh.rotation.x = index; + if (rotation) mesh.rotation.x = rotation.x; // mesh.castShadow = true; // mesh.receiveShadow = true; meshes.push(mesh); diff --git a/src/kiri/widget.js b/src/kiri/widget.js index 05e1512f..c096e50a 100644 --- a/src/kiri/widget.js +++ b/src/kiri/widget.js @@ -649,40 +649,37 @@ class Widget { this.points = null; } - getGeoVertices(unroll) { + getGeoVertices(unroll, index) { let geo = this.mesh.geometry; - let pos = geo.getAttribute('position').array; + let pos = geo.getAttribute('position'); + // if indexed, return points rotated about X and then offset + let track = this.track; + if (index && track.indexed) { + let delta = track.delta; + pos = pos.clone().applyMatrix4( + new THREE.Matrix4().makeRotationX(track.indexRad) + ).applyMatrix4( + new THREE.Matrix4().makeTranslation(0, -delta.y, -delta.z + track.tzoff) + ); + } + pos = pos.array; if (geo.index && unroll !== false) { let idx = geo.index.array; let len = idx.length; - let p2 = new Float32Array(len * 3); + let pp2 = new Float32Array(len * 3); let inc = 0; for (let i=0; i { - let done = index >= count; - return done ? { done } : - { value: newPoint(verts[index++], verts[index++], verts[index++]) }; - } - } } - }; + return pos; } getPoints() {