diff --git a/dist/index.html b/dist/index.html index b62c7f4..016bf3b 100644 --- a/dist/index.html +++ b/dist/index.html @@ -39,7 +39,6 @@
- diff --git a/src/sketcher/Sketcher.js b/src/sketcher/Sketcher.js index e8e68e2..5c29631 100644 --- a/src/sketcher/Sketcher.js +++ b/src/sketcher/Sketcher.js @@ -7,7 +7,7 @@ import { onHover, onDrag, onPick, onRelease } from './pickEvents' import { addDimension, setCoincident } from './constraintEvents' import { get3PtArc } from './sketchArc' import { extrude } from './extrude' -import {_vec2, _vec3, raycaster} from '../utils/static' +import { _vec2, _vec3, raycaster } from '../utils/static' @@ -62,7 +62,7 @@ class Sketcher extends THREE.Group { this.onKeyPress = this.onKeyPress.bind(this); - this.matrixAutoUpdate=false; + this.matrixAutoUpdate = false; this.selected = new Set() this.hovered = [] @@ -151,24 +151,57 @@ class Sketcher extends THREE.Group { } deleteSelected() { - let minI = this.children.length; - for (let obj of this.selected) { - minI = Math.min(minI, this.delete(obj)) - } + const toDelete = [...this.selected] + .filter(e => e.type == 'Line') + .sort((a, b) => b.id - a.id) + .map(e => { + const i = this.objIdx.get(e.id) + this.delete(i) + return i + }) - // this.updatePointsBuffer(minI) - this.updatePointsBuffer() + + this.updatePointsBuffer(toDelete[0]) + // this.updatePointsBuffer() this.updateOtherBuffers() this.selected.clear() this.dispatchEvent({ type: 'change' }) } + delete(i) { + const obj = this.children[i] + let link = this.linkedObjs.get(obj.l_id) + if (!link) return; + link = link[1] + + console.log('delete',link.length) + for (let j = 0; j < link.length; j++) { + const obj = this.children[i + j] + obj.geometry.dispose() + obj.material.dispose() + + for (let c_id of obj.constraints) { + console.log(j,c_id) + this.deleteConstraints(c_id) + } + } + + this.children.splice(i, link.length) + + this.linkedObjs.delete(obj.l_id) + + } + + deleteConstraints(c_id) { - for (let ob of this.constraints.get(c_id)[2]) { - if (ob == -1) continue - ob.constraints.delete(c_id) + for (let idx of this.constraints.get(c_id)[2]) { ////////// + if (idx == -1) continue + const ob = this.children[this.objIdx.get(idx)] + if (ob) { + ob.constraints.delete(c_id) + } } this.constraints.delete(c_id) } @@ -179,7 +212,7 @@ class Sketcher extends THREE.Group { this.constraintsBuf.set( [ this.constraintNum[obj[0]], obj[1], - ...obj[2].map(ele => this.objIdx.get(ele.id) ?? -1), + ...obj[2].map(ele => this.objIdx.get(ele) ?? -1), ], (i) * 6 ) @@ -191,7 +224,7 @@ class Sketcher extends THREE.Group { this.linksBuf.set( [ this.linkNum[obj[0]], - ...obj[1].map(ele => this.objIdx.get(ele.id) ?? -1), + ...obj[1].map(ele => this.objIdx.get(ele) ?? -1), ], (i) * 5 ) @@ -200,29 +233,7 @@ class Sketcher extends THREE.Group { } - delete(obj) { - let link = this.linkedObjs.get(obj.l_id) - if (!link) return Infinity; - link = link[1] - let i = this.children.indexOf(link[0]) - - for (let j = 0; j < link.length; j++) { - const obj = this.children[i + j] - obj.geometry.dispose() - obj.material.dispose() - - for (let c_id of obj.constraints) { - this.deleteConstraints(c_id) - } - } - - this.children.splice(i, link.length) - - this.linkedObjs.delete(obj.l_id) - - return i - } updatePointsBuffer(startingIdx = 0) { for (let i = startingIdx; i < this.children.length; i++) { @@ -299,7 +310,7 @@ class Sketcher extends THREE.Group { */ for (let [k, obj] of this.linkedObjs) { if (obj[0] != 'arc') continue; - const [p1, p2, c, arc] = obj[1] + const [p1, p2, c, arc] = obj[1].map(e => this.children[this.objIdx.get(e)]) const points = get3PtArc( p1.geometry.attributes.position.array, diff --git a/src/sketcher/constraintEvents.js b/src/sketcher/constraintEvents.js index 676f2e4..88c221e 100644 --- a/src/sketcher/constraintEvents.js +++ b/src/sketcher/constraintEvents.js @@ -68,7 +68,7 @@ export function setCoincident() { this.constraints.set(++this.c_id, [ 'coincident', -1, - [toComb[i - 1], toComb[i], -1, -1] + [toComb[i - 1].id, toComb[i].id, -1, -1] /////// ] ) toComb[i].constraints.add(this.c_id) diff --git a/src/sketcher/drawEvents.js b/src/sketcher/drawEvents.js index 20d3e82..4fea6b4 100644 --- a/src/sketcher/drawEvents.js +++ b/src/sketcher/drawEvents.js @@ -16,7 +16,7 @@ export function drawOnClick1(e) { this.updatePoint = this.children.length this.add(...this.toPush) - this.linkedObjs.set(this.l_id, [this.mode, this.toPush]) + this.linkedObjs.set(this.l_id, [this.mode, this.toPush.map(e=>e.id)]) for (let obj of this.toPush) { obj.l_id = this.l_id } @@ -65,7 +65,7 @@ export function drawClear() { this.domElement.removeEventListener('pointermove', this.drawPreClick2); this.domElement.removeEventListener('pointerdown', this.drawOnClick2); - this.delete(this.children[this.children.length - 1]) + this.delete(this.updatePoint) this.dispatchEvent({ type: 'change' }) this.subsequent = false diff --git a/src/sketcher/extrude.js b/src/sketcher/extrude.js index d67b73b..18583b4 100644 --- a/src/sketcher/extrude.js +++ b/src/sketcher/extrude.js @@ -1,10 +1,11 @@ import * as THREE from '../../node_modules/three/src/Three'; -import {pointMaterial} from '../utils/static' +import { pointMaterial } from '../utils/static' export function extrude(sketch) { let constraints = sketch.constraints; let linkedObjs = sketch.linkedObjs; let children = sketch.children; + let objIdx = sketch.objIdx; let visited = new Set() let v2s = [] @@ -13,16 +14,18 @@ export function extrude(sketch) { let linkedObj = linkedObjs.get(node.l_id) let arr; if (linkedObj[0] == 'line') { - arr = linkedObj[1][2].geometry.attributes.position.array + // console.log(children, objIdx, linkedObj) + arr = children[objIdx.get(linkedObj[1][2])].geometry.attributes.position.array } else if (linkedObj[0] == 'arc') { - arr = linkedObj[1][3].geometry.attributes.position.array + arr = children[objIdx.get(linkedObj[1][3])].geometry.attributes.position.array } for (let i = 0; i < arr.length; i += 3) { v2s.push(new THREE.Vector2(arr[i], arr[i + 1])) } for (let i = 0; i < 2; i++) { - let d = linkedObj[1][i] + // let d = linkedObj[1][i] + let d = children[objIdx.get(linkedObj[1][i])] if (d == -1 || d == node) continue; if (d == children[1]) { console.log('pair found') @@ -36,8 +39,10 @@ export function extrude(sketch) { function findTouching(node) { for (let t of node.constraints) { if (constraints.get(t)[0] != 'coincident') continue - for (let d of constraints.get(t)[2]) { - if (d == -1 || d == node) continue; + for (let c of constraints.get(t)[2]) { + if (c == -1) continue; + const d = children[objIdx.get(c)] + if (d == node) continue; if (d == children[1]) { console.log('loop found') } else { diff --git a/src/sketcher/sketchLine.js b/src/sketcher/sketchLine.js index e6cc767..5c70fe1 100644 --- a/src/sketcher/sketchLine.js +++ b/src/sketcher/sketchLine.js @@ -41,7 +41,7 @@ export function sketchLine(mouseLoc) { this.constraints.set(++this.c_id, [ 'coincident', -1, - [this.children[this.children.length - 2], p1, -1, -1] + [this.children[this.children.length - 2].id, p1.id, -1, -1] ] )