@soonspacejs/plugin-measuring
Version:
Measuring plugin for SoonSpace.js
286 lines (285 loc) • 16.3 kB
JavaScript
import w from "soonspacejs";
import { LineBasicMaterial as x, DoubleSide as L, MeshBasicMaterial as C, SpriteMaterial as I, Sprite as B, BufferGeometry as m, Line as M, Mesh as D, Vector3 as p, QuadraticBezierCurve3 as H } from "three";
function F() {
const a = document.createElement("div");
return a.setAttribute("style", "height: 1in; visibility: hidden; position: absolute; margin: 0; padding: 0;"), document.body.appendChild(a), 0.0254 / a.clientHeight;
}
const E = {
m: 1,
mm: 1e-3,
cm: 0.01,
ft: 0.3048,
in: 0.0254,
pt: F()
}, P = (a) => a === 2 ? "²" : a === 3 ? "³" : "", y = (a, t = 1) => a + P(t), k = (a, t, e, s = 1) => (e == null && (e = t), e === t ? {
value: a,
unit: y(e)
} : {
value: a * Math.pow(E[t] / E[e], s),
unit: y(e) + P(s)
}), u = (a, t) => a.toFixed(t), N = "data:image/webp;base64,UklGRkQKAABXRUJQVlA4TDcKAAAvf8AfEPXYu/7fldLqffq1m957773PvGmmnNn7tr+2a7MtsCLfFPA64lEOkVxiOQ79un+LMKYHxiu6FhmcEJyAXUgcQq4jcXAlSbJp7X9NN59t27bfud8MI0mKqigsj0cFBaGVAwYAASBWtm3btm3b5m3btm3btm3brk2A+ScuCAShIBJEB7FB3NqgL36nHRABkoL8oA7oCUaDhWAzOABOgvPgUvvNr37/bPXcpQGQ/8KcHkAMkBO0ABPBHnAHfAR/gLQ7tn/75+7o+7ziQGJQDYwFx8Eb8J8kItb4rYmIIidEgrW2znmKi6LICcm+o+DYvlu9yhoElcBUcA38JOkitzbBhvS2pfC5qzbd3349MuiemDZPL27PrS7Pby8vbC87Fn5aLDwy6Hr2HpZVFzmSK20f2UEVgQygHzgBvpOIiNy65LalsNzXej0xaV7cnV6/Ve+8s/d/SeIfEnFIxiFFW6l+tufSzLDMuggR5Oh88U/FgCCQdzd4SHJN9JMNme3u2jDzyrF655V98A/JcAi9TYSQtvWhHzt1cKa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4auXhawPmuCC+GAlKd+9cS+8KvyNvUIugfgmyHGddLcXP6+cT1YN9uchR9ZBfBPswdZluk3Z5bn1pdvmnu0+1OK43Dpogt74cZRy4tFR9+4n8bnm7R8nhKM/NkzwQQjoAF6S3DmViIPNJWundpL8BHS8bHISlAPnSdfy1Pz23ifxueNrqy2OBBd2mlw9Wwd/KJ1316dXjlXF54avHLynNnQKO+pnTe6CKKAreEy69ent4f7jRmyDZ+Mbp7sc+fPiAZPTIA9YC35R3C9zbz6K2AfLxxfnfnHC6YV5k+sHQCtwiRTZnFseHXVvXIvuZByCE3af+qMuQrbPHjAKBMnBIPCIFNmYXR7ovl46nN77JN4Hwfvwi8u/i5C3Puw2ijy5cpyUaF1yu6PymXztXNRsMg42Gza0taXJidlIyOMrJ41CT4Lh4HYf6aTrzsr02Kh7aX+qfvALPvQ2PetDv1Cdait3iSP7Ll04aRR7DnQB+8FngHRubZL6PY2h7fn1ZalaG4t92KSPx2rVpW/bLtfPOEfy649Bl3NGwTOgJBh9E3wDSInWRD8kWJ/a6hP3NoeHeo+2J2fN08vm2dXt0KHxyQNtc+WJemm+EAnJ2Zvnjs0YVYO4oCwYDPaB5x0kKS6KIickKS6KIick2THVf+2LmlE7iAIygXrbwSZw9eg0054+Wvhm+/X+A0aLIBTEB9lBZdAeDADTwUqwHRzY9tVX234Pl06eGq62xt8Z3YIgEAYiZ/L5mWLPYfOv3AA=";
var T = /* @__PURE__ */ ((a) => (a.Distance = "Distance", a.Area = "Area", a.Angle = "Angle", a))(T || {});
const { utils: { randomString: b } } = w;
class d {
// save the last click time, in order to detect double click event
constructor(t) {
this.ssp = t;
const { viewport: { scene: e, camera: s } } = t;
this.scene = e, this.camera = s;
}
// lineWidth is ignored for Chrome on Windows, which is a known issue:
// https://github.com/mrdoob/three.js/issues/269
static LINE_MATERIAL = new x({ color: 16773120, linewidth: 2, opacity: 0.8, transparent: !0, side: L, depthWrite: !1, depthTest: !1 });
static MESH_MATERIAL = new C({ color: 8900346, transparent: !0, opacity: 0.8, side: L, depthWrite: !1, depthTest: !1 });
static MAX_DISTANCE = 500;
// when intersected object's distance is too far away, then ignore it
static OBJ_NAME = "object_for_measure" + b();
static LABEL_NAME = "label_for_measure" + b();
mode = null;
options = { unit: "m", precision: 2 };
scene;
camera;
spriteMaterial;
// 缓存之前的绘制对象
objectsStore = /* @__PURE__ */ new Set();
onCancel = () => {
};
onDone = () => {
};
pointMarkers = [];
// 边缘点标记
polyline;
// distance/area/angle 使用的绘制线段
faces;
// area
curve;
// angle 角度曲线
labels = [];
// poinode 标签
pointArray = [];
tempPointMarker;
// used to store temporary Points
tempLine;
// used to store temporary line, which is useful for drawing line/area/angle as mouse moves
tempLabel;
// used to store temporary label as mouse moves
lastClickTime;
get domElement() {
return this.ssp.domElement;
}
/**
* Starts the measurement
*/
start(t = "Distance", e = {}) {
const { unit: s = "m", precision: i = 2 } = e;
this.mode = t, this.options.unit = s, this.options.precision = i, this.ssp.signals.mouseMove.add(this.mousemove), this.ssp.signals.click.add(this.click), this.ssp.signals.dblClick.add(this.dblclick), this.ssp.signals.keyDown.add(this.keydown), this.pointArray = [], this.polyline = this.createLine(), this.scene.add(this.polyline), this.mode === "Area" && (this.faces = this.createFaces(), this.scene.add(this.faces)), this.domElement.style.cursor = "crosshair";
}
/**
* Ends the measurement
*/
close() {
this.mode !== null && (this.ssp.signals.mouseMove.remove(this.mousemove), this.ssp.signals.click.remove(this.click), this.ssp.signals.dblClick.remove(this.dblclick), this.ssp.signals.keyDown.remove(this.keydown), this.mode = null, this.domElement.style.cursor = "", this.pointArray = [], this.tempPointMarker && this.scene.remove(this.tempPointMarker), this.tempLine && this.scene.remove(this.tempLine), this.tempLabel && this.ssp.removeObject(this.tempLabel), this.pointMarkers.forEach((t) => this.scene.remove(t)), this.polyline && this.scene.remove(this.polyline), this.faces && this.scene.remove(this.faces), this.curve && this.scene.remove(this.curve), this.labels.forEach((t) => this.ssp.removeObject(t)), this.tempPointMarker = void 0, this.tempLine = void 0, this.tempLabel = void 0, this.pointMarkers = [], this.polyline = void 0, this.faces = void 0, this.curve = void 0, this.labels = [], this.ssp.render());
}
cancel() {
this.close(), this.onCancel();
}
clear() {
this.objectsStore.forEach((t) => this.ssp.removeObject(t)), this.objectsStore.clear();
}
/**
* Initializes point marker material
*/
initPointMarkerMaterial() {
const t = w.utils.textureLoader.load(N);
this.spriteMaterial = new I({
map: t,
depthTest: !1,
depthWrite: !1,
sizeAttenuation: !1,
opacity: 0.8
});
}
/**
* Creates point marker
*/
createPointMarker(t) {
this.spriteMaterial || this.initPointMarkerMaterial();
const e = 0.012, s = new B(this.spriteMaterial);
return s.scale.set(e, e, e), t && s.position.copy(t), s.name = d.OBJ_NAME, s;
}
/**
* Creates Line
*/
createLine(t = d.LINE_MATERIAL) {
const e = new m(), s = new M(e, t);
return s.frustumCulled = !1, s.name = d.OBJ_NAME, s;
}
/**
* Creates Mesh
*/
createFaces() {
const t = new m(), e = new D(t, d.MESH_MATERIAL);
return e.userData.vertices = [], e.frustumCulled = !1, e.name = d.OBJ_NAME, e;
}
/**
* Draw completed
*/
done() {
if (this.mode === null) return;
let t = !1;
const e = this.pointArray.length;
if (this.mode === "Area" && this.polyline)
if (e > 2) {
const s = this.pointArray[0];
this.polyline.geometry.dispose(), this.polyline.geometry = new m().setFromPoints([...this.pointArray, s]);
const i = this.calculateArea(this.pointArray), n = `${u(k(i, "m", this.options.unit, 2).value, this.options.precision)} ${this.getUnitString()}`, r = this.getBarycenter(this.pointArray), o = this.createLabel(n);
o.position.copy(r), o.element && (o.element.innerHTML = n), this.labels.push(o);
} else
t = !0;
if (this.mode === "Distance" && e < 2 && (t = !0), this.mode === "Angle" && this.polyline)
if (e >= 3) {
const s = this.pointArray[0], i = this.pointArray[1], n = this.pointArray[2], r = new p(s.x - i.x, s.y - i.y, s.z - i.z).normalize(), o = this.getAngleBisector(s, i, n), c = new p(n.x - i.x, n.y - i.y, n.z - i.z).normalize(), l = this.calculateAngle(s, i, n), v = `${u(l, this.options.precision)} ${this.getUnitString()}`, g = Math.min(s.distanceTo(i), n.distanceTo(i));
let h = g * 0.3, f = i.clone().add(new p(o.x * h, o.y * h, o.z * h));
const A = this.createLabel(v);
A.position.set(f.x, f.y, f.z), A.element.innerHTML = v, this.labels.push(A), h = g * 0.2, f = i.clone().add(new p(o.x * h, o.y * h, o.z * h));
const S = i.clone().add(new p(r.x * h, r.y * h, r.z * h)), z = i.clone().add(new p(c.x * h, c.y * h, c.z * h));
this.curve = this.createCurve(S, f, z), this.scene.add(this.curve);
} else
t = !0;
t || (this.pointMarkers.length > 0 && this.pointMarkers.forEach((s) => this.objectsStore.add(s)), this.polyline && this.objectsStore.add(this.polyline), this.faces && this.objectsStore.add(this.faces), this.curve && this.objectsStore.add(this.curve), this.labels.length > 0 && this.labels.forEach((s) => this.objectsStore.add(s)), this.pointMarkers = [], this.polyline = void 0, this.faces = void 0, this.curve = void 0, this.labels = []), this.close(), this.ssp.render(), this.onDone();
}
mousemove = (t) => {
if (this.mode === null) return;
const e = this.getClosestIntersection(t);
if (e) {
if (this.tempPointMarker ? this.tempPointMarker.position.set(e.x, e.y, e.z) : (this.tempPointMarker = this.createPointMarker(e), this.scene.add(this.tempPointMarker)), this.pointArray.length > 0) {
const s = this.pointArray[this.pointArray.length - 1], i = this.tempLine || this.createLine(), n = this.pointArray[0], r = this.pointArray[this.pointArray.length - 1];
if (this.mode === "Area" ? (i.geometry.dispose(), i.geometry = new m().setFromPoints([r, e, n])) : (i.geometry.dispose(), i.geometry = new m().setFromPoints([r, e])), this.mode === "Distance") {
const o = s.distanceTo(e), c = `${u(k(o, "m", this.options.unit).value, this.options.precision)} ${this.getUnitString()}`, l = new p((e.x + s.x) / 2, (e.y + s.y) / 2, (e.z + s.z) / 2);
this.addOrUpdateTempLabel(c, l);
}
this.tempLine || (this.scene.add(i), this.tempLine = i);
}
this.ssp.render();
}
};
dblclick = (t) => {
this.click(t), this.done();
};
click = (t) => {
if (this.mode === null) return;
const e = this.getClosestIntersection(t);
if (!e) return;
const s = Date.now();
if (this.lastClickTime && s - this.lastClickTime < 300)
return;
this.lastClickTime = s, this.pointArray.push(e);
const i = this.pointArray.length, n = this.createPointMarker(e);
if (this.pointMarkers.push(n), this.scene.add(n), this.polyline && (this.polyline.geometry.dispose(), this.polyline.geometry = new m().setFromPoints(this.pointArray), this.tempLabel && i > 1)) {
const r = this.pointArray[i - 2];
this.tempLabel.position.set((r.x + e.x) / 2, (r.y + e.y) / 2, (r.z + e.z) / 2), this.scene.add(this.tempLabel), this.labels.push(this.tempLabel), this.tempLabel = void 0;
}
if (this.mode === "Area" && this.faces) {
const r = this.faces.userData.vertices;
r.push(e), this.faces.geometry.dispose(), this.faces.geometry = new m().setFromPoints(r);
const o = r.length;
if (o > 2) {
const c = [];
for (let l = 1; l < o - 1; ++l)
c.push(0, l, l + 1);
this.faces.geometry.setIndex(c);
}
}
this.mode === "Angle" && this.pointArray.length >= 3 && this.done(), this.ssp.render();
};
keydown = (t) => {
t.key === "Enter" ? this.done() : t.key === "Escape" && this.cancel();
};
/**
* The the closest intersection
* @param e
*/
getClosestIntersection = (t) => {
if (this.mode === null) return;
let e = this.ssp.viewport.getIntersects(t);
return e && e.length > 0 && (e = e.filter((s) => s.object.name !== d.OBJ_NAME && s.object.visible), e.length > 0 && e[0].distance < d.MAX_DISTANCE) ? e[0].point : null;
};
/**
* Adds or update temp label and position
*/
addOrUpdateTempLabel(t, e) {
this.tempLabel || (this.tempLabel = this.createLabel(t)), this.tempLabel.position.set(e.x, e.y, e.z), this.tempLabel.element.innerHTML = t;
}
/**
* Creates label
*/
createLabel(t) {
const e = document.createElement("div");
e.innerHTML = t, e.style.padding = "3px 6px", e.style.color = "#fff", e.style.fontSize = "12px", e.style.position = "absolute", e.style.backgroundColor = "rgba(25, 25, 25, 0.3)", e.style.borderRadius = "12px", e.style.top = "0px", e.style.left = "0px";
const s = this.ssp.createPoiNode({ id: "label_id" + b(), element: e, type: "2D" });
return s.name = d.LABEL_NAME, s;
}
/**
* Creates the arc curve to indicate the angle in degree
*/
createCurve(t, e, s) {
const n = new H(t, e, s).getPoints(4), r = new m().setFromPoints(n), o = new M(r, d.LINE_MATERIAL);
return o.name = d.OBJ_NAME, o;
}
/**
* Calculates area
* TODO: for concave polygon, the value doesn't right, need to fix it
* @param points
*/
calculateArea(t) {
let e = 0;
for (let s = 0, i = 1, n = 2; n < t.length; i++, n++) {
const r = t[s].distanceTo(t[i]), o = t[i].distanceTo(t[n]), c = t[n].distanceTo(t[s]), l = (r + o + c) / 2;
e += Math.sqrt(l * (l - r) * (l - o) * (l - c));
}
return e;
}
/**
* Gets included angle of two lines in degree
*/
calculateAngle(t, e, s) {
const i = t, n = e, r = s, o = new p(i.x - n.x, i.y - n.y, i.z - n.z), c = new p(r.x - n.x, r.y - n.y, r.z - n.z);
return o.angleTo(c) * 180 / Math.PI;
}
/**
* Gets angle bisector of two lines
*/
getAngleBisector(t, e, s) {
const i = t, n = e, r = s, o = new p(i.x - n.x, i.y - n.y, i.z - n.z).normalize(), c = new p(r.x - n.x, r.y - n.y, r.z - n.z).normalize();
return new p(o.x + c.x, o.y + c.y, o.z + c.z).normalize();
}
/**
* Get the barycenter of points
*/
getBarycenter(t) {
const e = t.length;
let s = 0, i = 0, n = 0;
return t.forEach((r) => {
s += r.x, i += r.y, n += r.z;
}), new p(s / e, i / e, n / e);
}
/**
* Gets unit string for distance, area or angle
*/
getUnitString() {
return this.mode === "Distance" ? y(this.options.unit) : this.mode === "Area" ? `${y(this.options.unit, 2)}` : this.mode === "Angle" ? "°" : "";
}
/**
* Converts a number to a string with proper fraction digits
*/
numberToString(t) {
if (t < 1e-4)
return t.toString();
let e = 2;
return t < 0.01 ? e = 4 : t < 0.1 && (e = 3), t.toFixed(e);
}
}
export {
T as MeasuringMode,
d as default
};