UNPKG

awv3

Version:
138 lines (122 loc) 4.58 kB
import * as THREE from 'three'; import Ccbaseref from './ccbaseref'; import * as ConstraintType from './constraint/type'; export default class Ccref extends Ccbaseref { constructor(sketcher, id) { super(); this.sketcher = sketcher; this.id = id; } get graphics() { return this.sketcher.graphics.get(this.id); } get state() { return this.sketcher.tree[this.id]; } get tree() { return this.sketcher.connection.resolveTree(this.id)[0]; } get pos() { return this.getMember('pos', 'Vector3'); } get class() { return this.state.class; } get entities() { return this.state.members.entities.members.map(e => new Ccref(this.sketcher, e.value)); } get parent() { let parentId = this.state.parent; return parentId ? new Ccref(this.sketcher, parentId) : undefined; } get children() { return (this.state.children || []).map(child => new Ccref(this.sketcher, child)); } // local-to-world transformation as a THREE matrix get matrixWorld() { const [orig, vx, vy, vz] = this.state.coordinateSystem.map(row => new THREE.Vector3().fromArray(row)); return new THREE.Matrix4().makeBasis(vx, vy, vz).setPosition(orig); } updateGraphics() { if (this.graphics) { this.graphics.updateFromGeomParams(this.geomParams); this.graphics.updateFromSketcherAndId(this.sketcher, this.id); this.sketcher.refresh(); } } updateGraphicsRecursive() { for (let obj of this.descendants) obj.updateGraphics(); } getMember(name, wrap = undefined) { const raw = this.state.members[name]; switch (wrap) { case 'Ccref': return new Ccref(this.sketcher, raw.value || raw.id); case 'Vector3': return new THREE.Vector3().fromArray(raw.value); default: return raw; } } getNamedChild(childName) { return this.children.find(child => child.state.name === childName); } get geomParams() { const geomParams = { coordinateSystem: this.matrixWorld, scale: this.sketcher.graphicScale, }; if (this.sketcher.transientGeomParams.has(this.id)) return { ...this.sketcher.transientGeomParams.get(this.id), ...geomParams }; if (this.isConstraint()) { const textureName = Object.entries(ConstraintType).find( ([name, typeObject]) => typeObject.type === this.class, )[0]; return { ...geomParams, coordinateSystem: this.parent.matrixWorld, // constraints have incorrect server-side csys class: this.class, entities: this.entities, texture: this.sketcher.textures[textureName], value: { type: (this.state.members.type || {}).value, userValue: (this.state.members.userValue || {}).value, value: (this.state.members.value || {}).value, }, }; } switch (this.class) { case 'CC_Point': return { ...geomParams, start: this.pos }; case 'CC_Line': return { ...geomParams, start: this.startPoint.pos, end: this.endPoint.pos }; case 'CC_Arc': return { ...geomParams, start: this.startPoint.pos, end: this.endPoint.pos, center: this.center.pos, clockwise: this.state.members.bulge.value < 0, radius: this.state.members.radius.value, // ignored by most methods }; case 'CC_Circle': return { ...geomParams, center: this.center.pos, radius: this.state.members.radius.value, }; default: return geomParams; } } set geomParams(geomParams) { if (geomParams !== undefined) this.sketcher.transientGeomParams.set(this.id, geomParams); else this.sketcher.transientGeomParams.delete(this.id); for (let child of this.children) child.geomParams = geomParams !== undefined ? { start: geomParams[child.pointType] } : undefined; } get pointType() { return this.state.name.replace('Point', ''); } }