UNPKG

awv3

Version:
198 lines (163 loc) 5.2 kB
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import * as THREE from 'three'; import Object3 from '../../../three/object3'; import DimensionGraphics from '../../dimensions/graphics/'; import BaseHandler from './base'; import { intersectLines } from '../../sketcher/geomutils'; var AngleHandler = /*#__PURE__*/ function (_BaseHandler) { _inheritsLoose(AngleHandler, _BaseHandler); function AngleHandler() { var _this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _BaseHandler.call.apply(_BaseHandler, [this].concat(args)) || this; _this.preview = new DimensionGraphics('CC_AngularDimension', _this.sketcher.dimension); _this.sketcher.pool.add(_this.preview); _this.intersectionPoint = new THREE.Vector3(); _this.dir0 = new THREE.Vector3(); _this.dir1 = new THREE.Vector3(); _this.tuv = { type: 0, userValue: 0, value: 0, sector: 0, reflex: false, dimPos: new THREE.Vector3() }; _this.resolve = function () {}; return _this; } var _proto = AngleHandler.prototype; _proto.destroy = function destroy(options) { this.preview.destroy(); _BaseHandler.prototype.destroy.call(this, options); }; _proto.filterObjectsWithInteraction = function filterObjectsWithInteraction(object) { return object.id === this.sketch.id && object.graphics !== undefined; }; _proto[Object3.Events.Interaction.Clicked] = function (object, hitObject) { this.resolve(this.tuv); }; _proto.cancel = function cancel() { this.resolve(undefined); }; _proto.onMouseMove = function onMouseMove() { this.updatePreview(); }; _proto.onKeyUp = function onKeyUp() { this.updatePreview(); }; _proto.onKeyDown = function onKeyDown() { this.updatePreview(); }; _proto.setLines = function setLines(lines) { var _lines$map = lines.map(function (line) { return [line.startPoint.pos, line.endPoint.pos]; }), _lines$map$ = _lines$map[0], p0s = _lines$map$[0], p0e = _lines$map$[1], _lines$map$2 = _lines$map[1], p1s = _lines$map$2[0], p1e = _lines$map$2[1]; this.dir0.copy(p0e).sub(p0s).normalize(); this.dir1.copy(p1e).sub(p1s).normalize(); // ensure that ccw angle from dir0 to dir1 is between 0 and Math.PI if (ccwAngle(this.dir0, this.dir1) < 0) { var s = lines[0]; lines[0] = lines[1]; lines[1] = s; return this.setLines(lines); } this.intersectionPoint = intersectLines(p0s, this.dir0, p1s, this.dir1); this.updatePreview(); this.preview.onRender(); }; _proto.updatePreview = function updatePreview() { var point = this.getRecentMousePosition(); var dir = point.clone().sub(this.intersectionPoint).normalize(); var rightOf0 = ccwAngle(this.dir0, dir) < 0, leftOf1 = ccwAngle(this.dir1, dir) >= 0; this.tuv.sector = rightOf0 << 1 | rightOf0 ^ leftOf1; this.tuv.reflex = !!this.sketcher.view.input.keys.control; this.tuv.type = this.tuv.sector ^ this.tuv.reflex << 1; this.tuv.dimPos.copy(point); var start = new THREE.Vector3(), end = new THREE.Vector3(); switch (this.tuv.type) { case 0: { start.copy(this.dir0); end.copy(this.dir1); break; } case 1: { start.copy(this.dir1); end.copy(this.dir0).negate(); break; } case 2: { start.copy(this.dir0).negate(); end.copy(this.dir1).negate(); break; } case 3: { start.copy(this.dir1).negate(); end.copy(this.dir0); break; } } this.tuv.userValue = ofSign(ccwAngle(start, end), this.tuv.reflex); this.tuv.value = ofSign(ccwAngle(this.dir0, this.dir1)); function f(v) { return { value: v.toArray() }; } var state = { virtual: true, visible: true, dimension: { id: 0, class: 'CC_AngularDimension', coordinateSystem: this.sketch.state.coordinateSystem, previewValue: THREE.Math.radToDeg(this.tuv.userValue), members: { startPt: f(start.add(this.intersectionPoint)), endPt: f(end.add(this.intersectionPoint)), dimPt: f(point), cornerPt: f(this.intersectionPoint), paramName: { value: '_' }, ccw: { value: !this.tuv.reflex }, extendToCorner: { value: true } } }, instanceCsys: this.sketch.state.coordinateSystem //TODO: seems like ugly hack (copypaste from 106 line) }; this.preview.updateFromState(state); }; return AngleHandler; }(BaseHandler); export { AngleHandler as default }; function ccwAngle(a, b) { return Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y); } function ofSign(a, neg) { if (neg === void 0) { neg = false; } if (neg && a > 0.0) a -= 2.0 * Math.PI; if (!neg && a < 0.0) a += 2.0 * Math.PI; return Math.abs(a); }