three.cad/src/sketcher/Sketch.js

499 lines
13 KiB
JavaScript
Raw Normal View History

2021-03-25 16:04:13 +08:00
2021-03-27 04:40:40 +08:00
import * as THREE from '../../node_modules/three/src/Three';
2021-03-25 16:04:13 +08:00
2021-03-26 12:30:35 +08:00
import { drawOnClick1, drawOnClick2, drawPreClick2, drawClear } from './drawEvents'
2021-04-05 11:52:17 +08:00
import { onHover, onDrag, onDragDim, onPick, onRelease } from '../utils/mouseEvents'
2021-03-26 12:30:35 +08:00
import { addDimension, setCoincident } from './constraintEvents'
2021-04-01 08:11:42 +08:00
import { get3PtArc } from './drawArc'
2021-04-03 03:33:09 +08:00
import { _vec2, _vec3, raycaster, awaitPts } from '../utils/shared'
2021-04-01 06:03:35 +08:00
import { replacer, reviver } from '../utils/mapJSONReplacer'
2021-04-03 03:33:09 +08:00
import { AxesHelper } from '../utils/axes'
2021-04-05 16:05:53 +08:00
import { drawDimension, _onMoveDimension, setDimLines } from './drawDimension';
2021-03-25 16:04:13 +08:00
2021-03-26 12:30:35 +08:00
2021-04-01 13:35:08 +08:00
class Sketch {
2021-03-29 18:27:34 +08:00
2021-04-01 13:35:08 +08:00
constructor(camera, canvas, store, preload) {
// [0]:x, [1]:y, [2]:z
this.ptsBuf = new Float32Array(this.max_pts * 3).fill(NaN)
// [0]:type, [1]:pt1, [2]:pt2, [3]:pt3, [4]:pt4
this.linksBuf = new Float32Array(this.max_links * 5).fill(NaN)
// [0]:type, [1]:val, [2]:pt1, [3]:pt2, [4]:lk1, [5]:lk2
this.constraintsBuf = new Float32Array(this.max_constraints * 6).fill(NaN)
this.plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
2021-04-05 11:52:17 +08:00
this.labelContainer = document.getElementById('labels')
2021-04-01 13:35:08 +08:00
if (preload === undefined) {
this.obj3d = new THREE.Group()
this.obj3d.name = "s" + nid++
2021-04-05 11:52:17 +08:00
this.obj3d.userData.type = "sketch"
2021-04-01 13:35:08 +08:00
this.obj3d.matrixAutoUpdate = false;
this.objIdx = new Map()
this.linkedObjs = new Map()
this.l_id = 0;
this.constraints = new Map()
this.c_id = 0;
2021-04-04 12:54:27 +08:00
this.obj3d.add(new THREE.Group().add(new AxesHelper(2)));
this.obj3d.add(new THREE.Group());
this.obj3d.add(new THREE.Group());
2021-04-05 11:52:17 +08:00
this.labels = []
2021-04-01 08:04:14 +08:00
} else {
2021-04-01 13:35:08 +08:00
this.obj3d = preload.obj3d
this.obj3d.inverse = preload.obj3d.matrix.clone().invert()
this.objIdx = JSON.parse(preload.objIdx, reviver)
this.linkedObjs = JSON.parse(preload.linkedObjs, reviver)
this.l_id = preload.l_id;
this.constraints = JSON.parse(preload.constraints, reviver)
this.c_id = preload.c_id;
this.updatePointsBuffer()
this.updateOtherBuffers()
this.plane.applyMatrix4(this.obj3d.matrix)
2021-04-01 08:04:14 +08:00
}
2021-03-25 16:04:13 +08:00
this.camera = camera;
2021-03-31 07:20:24 +08:00
this.canvas = canvas;
2021-03-28 20:00:31 +08:00
this.store = store;
2021-03-25 16:04:13 +08:00
2021-03-28 20:00:31 +08:00
2021-04-01 06:03:35 +08:00
2021-03-28 20:00:31 +08:00
2021-03-31 07:20:24 +08:00
2021-03-29 18:27:34 +08:00
2021-03-30 13:13:13 +08:00
this.bindHandlers()
2021-03-31 07:20:24 +08:00
this.selected = []
2021-03-30 13:13:13 +08:00
this.hovered = []
this.mode = ""
this.subsequent = false;
}
2021-04-01 13:35:08 +08:00
toJSON() {
return {
obj3d: this.obj3d,
objIdx: JSON.stringify(this.objIdx, replacer),
linkedObjs: JSON.stringify(this.linkedObjs, replacer),
l_id: this.l_id,
constraints: JSON.stringify(this.constraints, replacer),
c_id: this.c_id,
}
}
2021-03-29 18:27:34 +08:00
2021-03-30 13:13:13 +08:00
bindHandlers() {
2021-03-26 12:30:35 +08:00
this.drawOnClick1 = drawOnClick1.bind(this);
this.drawPreClick2 = drawPreClick2.bind(this);
this.drawOnClick2 = drawOnClick2.bind(this);
2021-04-05 16:05:53 +08:00
2021-04-04 08:59:14 +08:00
this.drawDimension = drawDimension.bind(this)
2021-04-04 12:54:27 +08:00
this._onMoveDimension = _onMoveDimension.bind(this)
2021-04-05 16:05:53 +08:00
this.setDimLines = setDimLines.bind(this)
2021-03-25 16:04:13 +08:00
2021-04-03 03:33:09 +08:00
this.awaitPts = awaitPts.bind(this);
2021-03-25 16:04:13 +08:00
this.onHover = onHover.bind(this);
this.onPick = onPick.bind(this);
this.onDrag = onDrag.bind(this);
this.onRelease = onRelease.bind(this);
this.onKeyPress = this.onKeyPress.bind(this);
2021-04-04 12:54:27 +08:00
2021-03-25 16:04:13 +08:00
}
2021-03-29 18:27:34 +08:00
activate() {
window.addEventListener('keydown', this.onKeyPress)
2021-03-31 07:20:24 +08:00
this.canvas.addEventListener('pointerdown', this.onPick)
this.canvas.addEventListener('pointermove', this.onHover)
2021-04-01 13:35:08 +08:00
this.store.dispatch({ type: 'set-active-sketch', sketch: this.obj3d.name })
2021-04-05 11:52:17 +08:00
2021-04-05 16:05:53 +08:00
this.setDimLines()
2021-04-01 13:35:08 +08:00
window.sketcher = this
2021-03-30 13:13:13 +08:00
}
deactivate() {
window.removeEventListener('keydown', this.onKeyPress)
2021-03-31 07:20:24 +08:00
this.canvas.removeEventListener('pointerdown', this.onPick)
this.canvas.removeEventListener('pointermove', this.onHover)
2021-03-30 13:13:13 +08:00
this.store.dispatch({ type: 'exit-sketch' })
2021-04-05 16:05:53 +08:00
this.labelContainer.innerHTML = ""
2021-03-29 18:27:34 +08:00
}
2021-03-28 20:00:31 +08:00
align(origin, x_dir, y_dir) {
2021-03-31 07:20:24 +08:00
const up = _vec3.subVectors(y_dir, origin).normalize();
2021-03-28 20:00:31 +08:00
const te = _m1.elements;
2021-03-31 07:20:24 +08:00
_x.subVectors(x_dir, origin).normalize();
_z.crossVectors(_x, up).normalize();
_y.crossVectors(_z, _x);
2021-03-28 20:00:31 +08:00
te[0] = _x.x; te[4] = _y.x; te[8] = _z.x;
te[1] = _x.y; te[5] = _y.y; te[9] = _z.y;
te[2] = _x.z; te[6] = _y.z; te[10] = _z.z;
2021-04-01 13:35:08 +08:00
this.obj3d.quaternion.setFromRotationMatrix(_m1);
const parent = this.obj3d.parent;
2021-03-28 20:00:31 +08:00
_m1.extractRotation(parent.matrixWorld);
2021-03-31 07:20:24 +08:00
_q1.setFromRotationMatrix(_m1);
2021-04-01 13:35:08 +08:00
this.obj3d.quaternion.premultiply(_q1.invert());
this.obj3d.updateMatrix();
this.obj3d.matrix.setPosition(origin)
2021-03-31 07:20:24 +08:00
2021-04-01 13:35:08 +08:00
this.plane.applyMatrix4(this.obj3d.matrix)
this.obj3d.inverse = this.obj3d.matrix.clone().invert()
2021-03-25 16:04:13 +08:00
}
2021-03-28 20:00:31 +08:00
2021-03-25 16:04:13 +08:00
onKeyPress(e) {
switch (e.key) {
case 'Escape':
2021-03-26 12:30:35 +08:00
drawClear.bind(this)()
2021-03-25 16:04:13 +08:00
this.mode = ""
2021-04-05 16:05:53 +08:00
document.activeElement.blur()
2021-03-25 16:04:13 +08:00
break;
case 'l':
if (this.mode == 'line') {
2021-03-26 12:30:35 +08:00
drawClear.bind(this)()
2021-03-25 16:04:13 +08:00
}
2021-03-31 07:20:24 +08:00
this.canvas.addEventListener('pointerdown', this.drawOnClick1)
2021-03-25 16:04:13 +08:00
this.mode = "line"
break;
case 'a':
2021-03-31 07:20:24 +08:00
this.canvas.addEventListener('pointerdown', this.drawOnClick1)
2021-03-25 16:04:13 +08:00
this.mode = "arc"
break;
2021-04-03 03:33:09 +08:00
case 'd':
2021-04-04 08:59:14 +08:00
this.drawDimension()
this.mode = ""
2021-04-03 03:33:09 +08:00
break;
2021-03-26 12:30:35 +08:00
case 'x':
2021-03-25 16:04:13 +08:00
this.deleteSelected()
break;
2021-03-26 12:30:35 +08:00
case 'c':
2021-03-29 05:34:55 +08:00
2021-03-26 12:30:35 +08:00
setCoincident.call(this)
2021-03-29 05:34:55 +08:00
2021-03-26 12:30:35 +08:00
this.mode = ""
break;
2021-03-31 07:20:24 +08:00
2021-03-29 05:34:55 +08:00
case 'z':
var string = JSON.stringify(this.toJSON());
window.string = string;
alert("Size of sample is: " + string.length);
2021-03-29 10:08:49 +08:00
window.compressed = LZString.compress(string);
2021-03-29 05:34:55 +08:00
alert("Size of compressed sample is: " + compressed.length);
string = LZString.decompress(compressed);
alert("Sample is: " + string);
2021-03-26 12:30:35 +08:00
break;
2021-03-25 16:04:13 +08:00
}
}
deleteSelected() {
2021-04-05 11:52:17 +08:00
this.selected
.filter(e => e.userData.type == 'dimension')
.forEach(e => this.constraints.has(e.name) && this.deleteConstraints(e.name))
2021-03-30 09:46:55 +08:00
2021-04-05 11:52:17 +08:00
const toDelete = this.selected
.filter(e => e.userData.type == 'line')
2021-03-30 06:12:46 +08:00
.sort((a, b) => b.id - a.id)
2021-03-30 09:46:55 +08:00
.map(obj => {
return this.delete(obj)
2021-03-30 06:12:46 +08:00
})
2021-04-05 11:52:17 +08:00
if (toDelete.length) {
this.updatePointsBuffer(toDelete[toDelete.length - 1])
}
2021-03-25 16:04:13 +08:00
this.updateOtherBuffers()
2021-03-31 07:20:24 +08:00
this.selected = []
2021-04-01 13:35:08 +08:00
this.obj3d.dispatchEvent({ type: 'change' })
2021-03-25 16:04:13 +08:00
}
2021-03-30 09:46:55 +08:00
delete(obj) {
2021-04-05 11:52:17 +08:00
if (obj.userData.type == 'dimension') {
this.deleteConstraints(obj.name)
return
}
2021-03-31 16:07:34 +08:00
let link = this.linkedObjs.get(obj.userData.l_id)
2021-03-30 06:12:46 +08:00
if (!link) return;
link = link[1]
2021-03-30 09:46:55 +08:00
let i = this.objIdx.get(link[0]) || this.updatePoint // hacky, see drawEvent.js for updatePoint def
2021-03-30 06:12:46 +08:00
for (let j = 0; j < link.length; j++) {
2021-04-01 13:35:08 +08:00
const obj = this.obj3d.children[i + j]
2021-03-30 06:12:46 +08:00
obj.geometry.dispose()
obj.material.dispose()
2021-03-31 16:07:34 +08:00
for (let c_id of obj.userData.constraints) {
2021-03-30 06:12:46 +08:00
this.deleteConstraints(c_id)
}
}
2021-04-01 13:35:08 +08:00
this.obj3d.children.splice(i, link.length)
2021-03-30 06:12:46 +08:00
2021-03-31 16:07:34 +08:00
this.linkedObjs.delete(obj.userData.l_id)
2021-03-30 06:12:46 +08:00
2021-03-30 09:46:55 +08:00
return i
2021-03-30 06:12:46 +08:00
}
2021-03-25 16:04:13 +08:00
deleteConstraints(c_id) {
2021-04-05 11:52:17 +08:00
for (let idx of this.constraints.get(c_id)[2]) { // clean on contraint references
2021-03-30 06:12:46 +08:00
if (idx == -1) continue
2021-04-01 13:35:08 +08:00
const ob = this.obj3d.children[this.objIdx.get(idx)]
2021-03-30 06:12:46 +08:00
if (ob) {
2021-03-31 16:07:34 +08:00
ob.userData.constraints.splice(ob.userData.constraints.indexOf(c_id), 1)
2021-03-30 06:12:46 +08:00
}
2021-03-25 16:04:13 +08:00
}
this.constraints.delete(c_id)
2021-04-05 11:52:17 +08:00
for (let i = 0; i < this.obj3d.children[1].children.length; i++) {
if (this.obj3d.children[1].children[i].name == c_id) {
this.obj3d.children[1].children.splice(i, i + 2).forEach(
e => {
e.geometry.dispose()
e.material.dispose()
}
)
break
}
}
2021-03-25 16:04:13 +08:00
}
updateOtherBuffers() {
let i = 0
for (let [key, obj] of this.constraints) {
this.constraintsBuf.set(
[
2021-03-29 18:27:34 +08:00
this.constraintNum[obj[0]], obj[1],
2021-03-30 06:12:46 +08:00
...obj[2].map(ele => this.objIdx.get(ele) ?? -1),
2021-03-25 16:04:13 +08:00
],
(i) * 6
)
i++
}
i = 0;
for (let [key, obj] of this.linkedObjs) {
this.linksBuf.set(
[
this.linkNum[obj[0]],
2021-03-30 06:12:46 +08:00
...obj[1].map(ele => this.objIdx.get(ele) ?? -1),
2021-03-25 16:04:13 +08:00
],
(i) * 5
)
i++
}
}
updatePointsBuffer(startingIdx = 0) {
2021-04-01 13:35:08 +08:00
for (let i = startingIdx; i < this.obj3d.children.length; i++) {
const obj = this.obj3d.children[i]
2021-04-01 06:03:35 +08:00
this.objIdx.set(obj.name, i)
2021-03-25 16:04:13 +08:00
if (obj.type == "Points") {
this.ptsBuf.set(obj.geometry.attributes.position.array, 3 * i)
}
}
}
2021-04-05 16:05:53 +08:00
updateBoundingSpheres() {
for (let x = 3; x < this.obj3d.children.length; x++) {
const obj = this.obj3d.children[x]
obj.geometry.computeBoundingSphere()
}
for (let x = 0; x < this.obj3d.children[1].children.length; x++) {
const obj = this.obj3d.children[1].children[x]
obj.geometry.computeBoundingSphere()
}
}
2021-03-25 16:04:13 +08:00
getLocation(e) {
2021-03-29 18:27:34 +08:00
raycaster.setFromCamera(
_vec2.set(
2021-03-25 16:04:13 +08:00
(e.clientX / window.innerWidth) * 2 - 1,
- (e.clientY / window.innerHeight) * 2 + 1
),
this.camera
);
2021-04-01 13:35:08 +08:00
raycaster.ray.intersectPlane(this.plane, _vec3).applyMatrix4(this.obj3d.inverse)
2021-03-25 16:04:13 +08:00
2021-04-04 08:59:14 +08:00
return _vec3
2021-03-25 16:04:13 +08:00
}
2021-04-05 11:52:17 +08:00
getScreenXY(arr) {
return _vec3.set(...arr).applyMatrix4(this.obj3d.matrix).project(this.camera)
}
2021-03-25 16:04:13 +08:00
solve() {
const pts_buffer =
Module._malloc(this.ptsBuf.length * this.ptsBuf.BYTES_PER_ELEMENT)
Module.HEAPF32.set(this.ptsBuf, pts_buffer >> 2)
const constraints_buffer =
Module._malloc(this.constraintsBuf.length * this.constraintsBuf.BYTES_PER_ELEMENT)
Module.HEAPF32.set(this.constraintsBuf, constraints_buffer >> 2)
const links_buffer =
Module._malloc(this.linksBuf.length * this.linksBuf.BYTES_PER_ELEMENT)
Module.HEAPF32.set(this.linksBuf, links_buffer >> 2)
Module["_solver"](
2021-04-01 13:35:08 +08:00
this.obj3d.children.length, pts_buffer,
2021-03-25 16:04:13 +08:00
this.constraints.size, constraints_buffer,
this.linkedObjs.size, links_buffer)
2021-03-28 20:00:31 +08:00
/*
- loop to update all the children that are points
2021-04-04 12:54:27 +08:00
- why +6? we skip first two triplets because it refers to a non-geometry children
2021-03-28 20:00:31 +08:00
- we also sneak in updating lines children as well, by checking when ptsBuf[ptr] is NaN
*/
2021-03-25 16:04:13 +08:00
2021-04-04 12:54:27 +08:00
for (let i = 3, ptr = (pts_buffer >> 2) + 9; i < this.obj3d.children.length; i += 1, ptr += 3) {
2021-04-03 03:33:09 +08:00
// for (let i = 0, ptr = (pts_buffer >> 2) + 3; i < this.obj3d.children.length; i += 1, ptr += 3) {
2021-03-25 16:04:13 +08:00
2021-04-01 13:35:08 +08:00
const pos = this.obj3d.children[i].geometry.attributes.position;
2021-03-25 16:04:13 +08:00
if (isNaN(Module.HEAPF32[ptr])) {
pos.array[0] = Module.HEAPF32[ptr - 6]
pos.array[1] = Module.HEAPF32[ptr - 5]
pos.array[3] = Module.HEAPF32[ptr - 3]
pos.array[4] = Module.HEAPF32[ptr - 2]
} else {
pos.array[0] = Module.HEAPF32[ptr]
pos.array[1] = Module.HEAPF32[ptr + 1]
}
2021-03-28 20:00:31 +08:00
2021-03-25 16:04:13 +08:00
pos.needsUpdate = true;
}
Module._free(pts_buffer)
Module._free(links_buffer)
Module._free(constraints_buffer)
2021-03-26 12:30:35 +08:00
2021-03-28 20:00:31 +08:00
/*
arcs were not updated in above loop, we go through all arcs linkedObjs
and updated based on the control pts (which were updated in loop above)
*/
2021-03-26 12:30:35 +08:00
for (let [k, obj] of this.linkedObjs) {
2021-03-28 20:00:31 +08:00
if (obj[0] != 'arc') continue;
2021-04-01 13:35:08 +08:00
const [p1, p2, c, arc] = obj[1].map(e => this.obj3d.children[this.objIdx.get(e)])
2021-03-26 12:30:35 +08:00
const points = get3PtArc(
p1.geometry.attributes.position.array,
p2.geometry.attributes.position.array,
c.geometry.attributes.position.array
);
arc.geometry.attributes.position.set(points)
arc.needsUpdate = true;
}
2021-04-05 16:05:53 +08:00
this.setDimLines()
2021-04-05 11:52:17 +08:00
2021-04-01 13:35:08 +08:00
this.obj3d.dispatchEvent({ type: 'change' })
2021-03-25 16:04:13 +08:00
}
2021-04-01 13:35:08 +08:00
2021-04-01 06:03:35 +08:00
2021-03-25 16:04:13 +08:00
}
2021-03-31 07:20:24 +08:00
const _m1 = new THREE.Matrix4()
const _q1 = new THREE.Quaternion()
const _x = new THREE.Vector3();
const _y = new THREE.Vector3();
const _z = new THREE.Vector3();
2021-03-25 16:04:13 +08:00
2021-04-01 13:35:08 +08:00
Object.assign(Sketch.prototype,
2021-03-29 18:27:34 +08:00
{
linkNum: {
'line': 0,
'arc': 1
},
constraintNum: {
2021-04-03 03:33:09 +08:00
points_coincident: 0,
pt_pt_distance: 1,
pt_plane_distance: 2,
pt_line_distance: 3,
pt_face_distance: 4,
pt_in_plane: 5,
pt_on_line: 6,
pt_on_face: 7,
equal_length_lines: 8,
length_ratio: 9,
eq_len_pt_line_d: 10,
eq_pt_ln_distances: 11,
equal_angle: 12,
equal_line_arc_len: 13,
symmetric: 14,
symmetric_horiz: 15,
symmetric_vert: 16,
symmetric_line: 17,
at_midpoint: 18,
horizontal: 19,
vertical: 20,
diameter: 21,
pt_on_circle: 22,
same_orientation: 23,
angle: 24,
parallel: 25,
perpendicular: 26,
arc_line_tangent: 27,
cubic_line_tangent: 28,
equal_radius: 29,
proj_pt_distance: 30,
where_dragged: 31,
curve_curve_tangent: 32,
length_difference: 33,
2021-03-29 18:27:34 +08:00
},
2021-03-30 09:46:55 +08:00
max_pts: 1000,
max_links: 1000,
max_constraints: 1000,
2021-03-29 18:27:34 +08:00
}
)
2021-04-03 03:33:09 +08:00
export { Sketch }