@soonspacejs/plugin-measuring 2.13.16 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import SoonSpace, { PoiNode } from 'soonspacejs';
1
+ import { default as SoonSpace, PoiNode } from 'soonspacejs';
2
2
  import { BufferGeometry, Camera, Line, LineBasicMaterial, Mesh, MeshBasicMaterial, Object3D, Scene, Sprite, SpriteMaterial, Vector3 } from 'three';
3
3
  import { Options } from './utils/ProjectSettingsDef';
4
4
  export declare enum MeasuringMode {
@@ -21,7 +21,7 @@ declare class MeasuringPlugin {
21
21
  protected scene: Scene;
22
22
  protected camera: Camera;
23
23
  protected spriteMaterial?: SpriteMaterial;
24
- objectsStore: Set<Object3D<import("three").Object3DEventMap>>;
24
+ objectsStore: Set<Object3D<import('three').Object3DEventMap>>;
25
25
  onCancel: () => void;
26
26
  onDone: () => void;
27
27
  protected pointMarkers: Sprite[];
@@ -86,7 +86,7 @@ declare class MeasuringPlugin {
86
86
  /**
87
87
  * Creates the arc curve to indicate the angle in degree
88
88
  */
89
- createCurve(p0: Vector3, p1: Vector3, p2: Vector3): Line<BufferGeometry<import("three").NormalBufferAttributes, import("three").BufferGeometryEventMap>, LineBasicMaterial, import("three").Object3DEventMap>;
89
+ createCurve(p0: Vector3, p1: Vector3, p2: Vector3): Line<BufferGeometry<import('three').NormalBufferAttributes, import('three').BufferGeometryEventMap>, LineBasicMaterial, import('three').Object3DEventMap>;
90
90
  /**
91
91
  * Calculates area
92
92
  * TODO: for concave polygon, the value doesn't right, need to fix it
package/dist/index.esm.js CHANGED
@@ -1 +1,285 @@
1
- import t from"soonspacejs";import{LineBasicMaterial as e,DoubleSide as s,MeshBasicMaterial as i,Vector3 as n,SpriteMaterial as r,Sprite as o,BufferGeometry as h,Line as a,Mesh as l,QuadraticBezierCurve3 as c}from"three";const p={m:1,mm:.001,cm:.01,ft:.3048,in:.0254,pt:function(){const t=document.createElement("div");return t.setAttribute("style","height: 1in; visibility: hidden; position: absolute; margin: 0; padding: 0;"),document.body.appendChild(t),.0254/t.clientHeight}()},d=t=>2===t?"²":3===t?"³":"",m=(t,e=1)=>t+d(e),A=(t,e,s,i=1)=>{if(null==s&&(s=e),s===e)return{value:t,unit:m(s)};return{value:t*Math.pow(p[e]/p[s],i),unit:m(s)+d(i)}},u=(t,e)=>t.toFixed(e);var y;!function(t){t.Distance="Distance",t.Area="Area",t.Angle="Angle"}(y||(y={}));const{utils:{randomString:f}}=t;class b{constructor(t){this.ssp=t,this.mode=null,this.options={unit:"m",precision:2},this.objectsStore=new Set,this.onCancel=()=>{},this.onDone=()=>{},this.pointMarkers=[],this.labels=[],this.pointArray=[],this.mousemove=t=>{if(null===this.mode)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 t=this.pointArray[this.pointArray.length-1],s=this.tempLine||this.createLine(),i=s.geometry,r=this.pointArray[0],o=this.pointArray[this.pointArray.length-1];if(this.mode===y.Area?i.setFromPoints([o,e,r]):i.setFromPoints([o,e]),this.mode===y.Distance){const s=t.distanceTo(e),i=`${u(A(s,"m",this.options.unit).value,this.options.precision)} ${this.getUnitString()}`,r=new n((e.x+t.x)/2,(e.y+t.y)/2,(e.z+t.z)/2);this.addOrUpdateTempLabel(i,r)}this.tempLine||(this.scene.add(s),this.tempLine=s)}this.ssp.render()}},this.dblclick=t=>{this.click(t),this.done()},this.click=t=>{if(null===this.mode)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.setFromPoints(this.pointArray),this.tempLabel&&i>1)){const t=this.pointArray[i-2];this.tempLabel.position.set((t.x+e.x)/2,(t.y+e.y)/2,(t.z+e.z)/2),this.scene.add(this.tempLabel),this.labels.push(this.tempLabel),this.tempLabel=void 0}if(this.mode===y.Area&&this.faces){const t=this.faces.geometry,s=this.faces.userData.vertices;s.push(e),t.setFromPoints(s);const i=s.length;if(i>2){const e=[];for(let t=1;t<i-1;++t)e.push(0,t,t+1);t.setIndex(e)}}this.mode===y.Angle&&this.pointArray.length>=3&&this.done(),this.ssp.render()},this.keydown=t=>{"Enter"===t.key?this.done():"Escape"===t.key&&this.cancel()},this.getClosestIntersection=t=>{if(null===this.mode)return;let e=this.ssp.viewport.getIntersects(t);return e&&e.length>0&&(e=e.filter(t=>t.object.name!==b.OBJ_NAME&&t.object.visible),e.length>0&&e[0].distance<b.MAX_DISTANCE)?e[0].point:null};const{viewport:{scene:e,camera:s}}=t;this.scene=e,this.camera=s}get domElement(){return this.ssp.domElement}start(t=y.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===y.Area&&(this.faces=this.createFaces(),this.scene.add(this.faces)),this.domElement.style.cursor="crosshair"}close(){null!==this.mode&&(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()}initPointMarkerMaterial(){const e=t.utils.textureLoader.load("data:image/webp;base64,UklGRkQKAABXRUJQVlA4TDcKAAAvf8AfEPXYu/7fldLqffq1m957773PvGmmnNn7tr+2a7MtsCLfFPA64lEOkVxiOQ79un+LMKYHxiu6FhmcEJyAXUgcQq4jcXAlSbJp7X9NN59t27bfud8MI0mKqigsj0cFBaGVAwYAASBWtm3btm3b5m3btm3btm3brk2A+ScuCAShIBJEB7FB3NqgL36nHRABkoL8oA7oCUaDhWAzOABOgvPgUvvNr37/bPXcpQGQ/8KcHkAMkBO0ABPBHnAHfAR/gLQ7tn/75+7o+7ziQGJQDYwFx8Eb8J8kItb4rYmIIidEgrW2znmKi6LICcm+o+DYvlu9yhoElcBUcA38JOkitzbBhvS2pfC5qzbd3349MuiemDZPL27PrS7Pby8vbC87Fn5aLDwy6Hr2HpZVFzmSK20f2UEVgQygHzgBvpOIiNy65LalsNzXej0xaV7cnV6/Ve+8s/d/SeIfEnFIxiFFW6l+tufSzLDMuggR5Oh88U/FgCCQdzd4SHJN9JMNme3u2jDzyrF655V98A/JcAi9TYSQtvWhHzt1cKaHUgDI/btPHFZHCeQBM8ArUgrrk9td1enJefP6taj60MZZtKGvDi19Vy8Ief3wfEkRIC0YA16QTjbllod73cH3v8H7OIDeNw7+9os4cmD0rAryoAu4CVC4pbDMvPko4mR8iAPbFl+cESKCPAS65HMOFAHbwF9AZHNheWLavP3MrI8D7W210CXC5S/LuQWig/4DpNuQ2R4ddm89MmvjwFu7dIAO4MB+n0MgM1gH/lFW720Or16q2MY5aWNfXhWeGf8oZ0B1cI10m3OfZ5a3RjIc4pz1jZkrjmyr5AYIA73AOwoSlP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CtwiRTZnFseHXVvXIvuZByCE3af+qMuQrbPHjAKBMnBIPCIFNmYXR7ovl46nN77JN4Hwfvwi8u/i5C3Puw2ijy5cpyUaF1yu6PymXztXNRsMg42Gza0taXJidlIyOMrJ41CT4Lh4HYf6aTrzsr02Kh7aX+qfvALPvQ2PetDv1Cdait3iSP7Ll04aRR7DnQB+8FngHRubZL6PY2h7fn1ZalaG4t92KSPx2rVpW/bLtfPOEfy649Bl3NGwTOgJBh9E3wDSInWRD8kWJ/a6hP3NoeHeo+2J2fN08vm2dXt0KHxyQNtc+WJemm+EAnJ2Zvnjs0YVYO4oCwYDPaB5x0kKS6KIickKS6KIick2THVf+2LmlE7iAIygXrbwSZw9eg0054+Wvhm+/X+A0aLIBTEB9lBZdAeDADTwUqwHRzY9tVX234Pl06eGq62xt8Z3YIgEAYiZ/L5mWLPYfOv3AA=");this.spriteMaterial=new r({map:e,depthTest:!1,depthWrite:!1,sizeAttenuation:!1,opacity:.8})}createPointMarker(t){this.spriteMaterial||this.initPointMarkerMaterial();const e=.012,s=new o(this.spriteMaterial);return s.scale.set(e,e,e),t&&s.position.copy(t),s.name=b.OBJ_NAME,s}createLine(t=b.LINE_MATERIAL){const e=new h,s=new a(e,t);return s.frustumCulled=!1,s.name=b.OBJ_NAME,s}createFaces(){const t=new h,e=new l(t,b.MESH_MATERIAL);return e.userData.vertices=[],e.frustumCulled=!1,e.name=b.OBJ_NAME,e}done(){if(null===this.mode)return;let t=!1;const e=this.pointArray.length;if(this.mode===y.Area&&this.polyline)if(e>2){const t=this.pointArray[0];this.polyline.geometry.setFromPoints([...this.pointArray,t]);const e=this.calculateArea(this.pointArray),s=`${u(A(e,"m",this.options.unit,2).value,this.options.precision)} ${this.getUnitString()}`,i=this.getBarycenter(this.pointArray),n=this.createLabel(s);n.position.copy(i),n.element&&(n.element.innerHTML=s),this.labels.push(n)}else t=!0;if(this.mode===y.Distance&&e<2&&(t=!0),this.mode===y.Angle&&this.polyline)if(e>=3){const t=this.pointArray[0],e=this.pointArray[1],s=this.pointArray[2],i=new n(t.x-e.x,t.y-e.y,t.z-e.z).normalize(),r=this.getAngleBisector(t,e,s),o=new n(s.x-e.x,s.y-e.y,s.z-e.z).normalize(),h=this.calculateAngle(t,e,s),a=`${u(h,this.options.precision)} ${this.getUnitString()}`,l=Math.min(t.distanceTo(e),s.distanceTo(e));let c=.3*l,p=e.clone().add(new n(r.x*c,r.y*c,r.z*c));const d=this.createLabel(a);d.position.set(p.x,p.y,p.z),d.element.innerHTML=a,this.labels.push(d),c=.2*l,p=e.clone().add(new n(r.x*c,r.y*c,r.z*c));const m=e.clone().add(new n(i.x*c,i.y*c,i.z*c)),A=e.clone().add(new n(o.x*c,o.y*c,o.z*c));this.curve=this.createCurve(m,p,A),this.scene.add(this.curve)}else t=!0;t||(this.pointMarkers.length>0&&this.pointMarkers.forEach(t=>this.objectsStore.add(t)),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(t=>this.objectsStore.add(t)),this.pointMarkers=[],this.polyline=void 0,this.faces=void 0,this.curve=void 0,this.labels=[]),this.close(),this.ssp.render(),this.onDone()}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}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:f(),element:e,type:"2D"});return s.name=b.LABEL_NAME,s}createCurve(t,e,s){const i=new c(t,e,s).getPoints(4),n=(new h).setFromPoints(i),r=new a(n,b.LINE_MATERIAL);return r.name=b.OBJ_NAME,r}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]),h=t[n].distanceTo(t[s]),a=(r+o+h)/2;e+=Math.sqrt(a*(a-r)*(a-o)*(a-h))}return e}calculateAngle(t,e,s){const i=t,r=e,o=s,h=new n(i.x-r.x,i.y-r.y,i.z-r.z),a=new n(o.x-r.x,o.y-r.y,o.z-r.z);return 180*h.angleTo(a)/Math.PI}getAngleBisector(t,e,s){const i=t,r=e,o=s,h=new n(i.x-r.x,i.y-r.y,i.z-r.z).normalize(),a=new n(o.x-r.x,o.y-r.y,o.z-r.z).normalize();return new n(h.x+a.x,h.y+a.y,h.z+a.z).normalize()}getBarycenter(t){const e=t.length;let s=0,i=0,r=0;return t.forEach(t=>{s+=t.x,i+=t.y,r+=t.z}),new n(s/e,i/e,r/e)}getUnitString(){return this.mode===y.Distance?m(this.options.unit):this.mode===y.Area?`${m(this.options.unit,2)}`:this.mode===y.Angle?"°":""}numberToString(t){if(t<1e-4)return t.toString();let e=2;return t<.01?e=4:t<.1&&(e=3),t.toFixed(e)}}b.LINE_MATERIAL=new e({color:16773120,linewidth:2,opacity:.8,transparent:!0,side:s,depthWrite:!1,depthTest:!1}),b.MESH_MATERIAL=new i({color:8900346,transparent:!0,opacity:.8,side:s,depthWrite:!1,depthTest:!1}),b.MAX_DISTANCE=500,b.OBJ_NAME="object_for_measure"+f(),b.LABEL_NAME="label_for_measure"+f();export{y as MeasuringMode,b as default};
1
+ import P from "soonspacejs";
2
+ import { LineBasicMaterial as x, DoubleSide as L, MeshBasicMaterial as C, SpriteMaterial as I, Sprite as B, BufferGeometry as y, Line as M, Mesh as D, Vector3 as h, QuadraticBezierCurve3 as H } from "three";
3
+ function F() {
4
+ const a = document.createElement("div");
5
+ return a.setAttribute("style", "height: 1in; visibility: hidden; position: absolute; margin: 0; padding: 0;"), document.body.appendChild(a), 0.0254 / a.clientHeight;
6
+ }
7
+ const E = {
8
+ m: 1,
9
+ mm: 1e-3,
10
+ cm: 0.01,
11
+ ft: 0.3048,
12
+ in: 0.0254,
13
+ pt: F()
14
+ }, w = (a) => a === 2 ? "²" : a === 3 ? "³" : "", A = (a, e = 1) => a + w(e), k = (a, e, t, s = 1) => (t == null && (t = e), t === e ? {
15
+ value: a,
16
+ unit: A(t)
17
+ } : {
18
+ value: a * Math.pow(E[e] / E[t], s),
19
+ unit: A(t) + w(s)
20
+ }), b = (a, e) => a.toFixed(e), N = "data:image/webp;base64,UklGRkQKAABXRUJQVlA4TDcKAAAvf8AfEPXYu/7fldLqffq1m957773PvGmmnNn7tr+2a7MtsCLfFPA64lEOkVxiOQ79un+LMKYHxiu6FhmcEJyAXUgcQq4jcXAlSbJp7X9NN59t27bfud8MI0mKqigsj0cFBaGVAwYAASBWtm3btm3b5m3btm3btm3brk2A+ScuCAShIBJEB7FB3NqgL36nHRABkoL8oA7oCUaDhWAzOABOgvPgUvvNr37/bPXcpQGQ/8KcHkAMkBO0ABPBHnAHfAR/gLQ7tn/75+7o+7ziQGJQDYwFx8Eb8J8kItb4rYmIIidEgrW2znmKi6LICcm+o+DYvlu9yhoElcBUcA38JOkitzbBhvS2pfC5qzbd3349MuiemDZPL27PrS7Pby8vbC87Fn5aLDwy6Hr2HpZVFzmSK20f2UEVgQygHzgBvpOIiNy65LalsNzXej0xaV7cnV6/Ve+8s/d/SeIfEnFIxiFFW6l+tufSzLDMuggR5Oh88U/FgCCQdzd4SHJN9JMNme3u2jDzyrF655V98A/JcAi9TYSQtvWhHzt1cKaHUgDI/btPHFZHCeQBM8ArUgrrk9td1enJefP6taj60MZZtKGvDi19Vy8Ief3wfEkRIC0YA16QTjbllod73cH3v8H7OIDeNw7+9os4cmD0rAryoAu4CVC4pbDMvPko4mR8iAPbFl+cESKCPAS65HMOFAHbwF9AZHNheWLavP3MrI8D7W210CXC5S/LuQWig/4DpNuQ2R4ddm89MmvjwFu7dIAO4MB+n0MgM1gH/lFW720Or16q2MY5aWNfXhWeGf8oZ0B1cI10m3OfZ5a3RjIc4pz1jZkrjmyr5AYIA73AOwoSlPf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twiRTZnFseHXVvXIvuZByCE3af+qMuQrbPHjAKBMnBIPCIFNmYXR7ovl46nN77JN4Hwfvwi8u/i5C3Puw2ijy5cpyUaF1yu6PymXztXNRsMg42Gza0taXJidlIyOMrJ41CT4Lh4HYf6aTrzsr02Kh7aX+qfvALPvQ2PetDv1Cdait3iSP7Ll04aRR7DnQB+8FngHRubZL6PY2h7fn1ZalaG4t92KSPx2rVpW/bLtfPOEfy649Bl3NGwTOgJBh9E3wDSInWRD8kWJ/a6hP3NoeHeo+2J2fN08vm2dXt0KHxyQNtc+WJemm+EAnJ2Zvnjs0YVYO4oCwYDPaB5x0kKS6KIickKS6KIick2THVf+2LmlE7iAIygXrbwSZw9eg0054+Wvhm+/X+A0aLIBTEB9lBZdAeDADTwUqwHRzY9tVX234Pl06eGq62xt8Z3YIgEAYiZ/L5mWLPYfOv3AA=";
21
+ var T = /* @__PURE__ */ ((a) => (a.Distance = "Distance", a.Area = "Area", a.Angle = "Angle", a))(T || {});
22
+ const { utils: { randomString: v } } = P;
23
+ class d {
24
+ // save the last click time, in order to detect double click event
25
+ constructor(e) {
26
+ this.ssp = e;
27
+ const { viewport: { scene: t, camera: s } } = e;
28
+ this.scene = t, this.camera = s;
29
+ }
30
+ // lineWidth is ignored for Chrome on Windows, which is a known issue:
31
+ // https://github.com/mrdoob/three.js/issues/269
32
+ static LINE_MATERIAL = new x({ color: 16773120, linewidth: 2, opacity: 0.8, transparent: !0, side: L, depthWrite: !1, depthTest: !1 });
33
+ static MESH_MATERIAL = new C({ color: 8900346, transparent: !0, opacity: 0.8, side: L, depthWrite: !1, depthTest: !1 });
34
+ static MAX_DISTANCE = 500;
35
+ // when intersected object's distance is too far away, then ignore it
36
+ static OBJ_NAME = "object_for_measure" + v();
37
+ static LABEL_NAME = "label_for_measure" + v();
38
+ mode = null;
39
+ options = { unit: "m", precision: 2 };
40
+ scene;
41
+ camera;
42
+ spriteMaterial;
43
+ // 缓存之前的绘制对象
44
+ objectsStore = /* @__PURE__ */ new Set();
45
+ onCancel = () => {
46
+ };
47
+ onDone = () => {
48
+ };
49
+ pointMarkers = [];
50
+ // 边缘点标记
51
+ polyline;
52
+ // distance/area/angle 使用的绘制线段
53
+ faces;
54
+ // area
55
+ curve;
56
+ // angle 角度曲线
57
+ labels = [];
58
+ // poinode 标签
59
+ pointArray = [];
60
+ tempPointMarker;
61
+ // used to store temporary Points
62
+ tempLine;
63
+ // used to store temporary line, which is useful for drawing line/area/angle as mouse moves
64
+ tempLabel;
65
+ // used to store temporary label as mouse moves
66
+ lastClickTime;
67
+ get domElement() {
68
+ return this.ssp.domElement;
69
+ }
70
+ /**
71
+ * Starts the measurement
72
+ */
73
+ start(e = "Distance", t = {}) {
74
+ const { unit: s = "m", precision: i = 2 } = t;
75
+ this.mode = e, 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";
76
+ }
77
+ /**
78
+ * Ends the measurement
79
+ */
80
+ close() {
81
+ 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((e) => this.scene.remove(e)), this.polyline && this.scene.remove(this.polyline), this.faces && this.scene.remove(this.faces), this.curve && this.scene.remove(this.curve), this.labels.forEach((e) => this.ssp.removeObject(e)), 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());
82
+ }
83
+ cancel() {
84
+ this.close(), this.onCancel();
85
+ }
86
+ clear() {
87
+ this.objectsStore.forEach((e) => this.ssp.removeObject(e)), this.objectsStore.clear();
88
+ }
89
+ /**
90
+ * Initializes point marker material
91
+ */
92
+ initPointMarkerMaterial() {
93
+ const e = P.utils.textureLoader.load(N);
94
+ this.spriteMaterial = new I({
95
+ map: e,
96
+ depthTest: !1,
97
+ depthWrite: !1,
98
+ sizeAttenuation: !1,
99
+ opacity: 0.8
100
+ });
101
+ }
102
+ /**
103
+ * Creates point marker
104
+ */
105
+ createPointMarker(e) {
106
+ this.spriteMaterial || this.initPointMarkerMaterial();
107
+ const t = 0.012, s = new B(this.spriteMaterial);
108
+ return s.scale.set(t, t, t), e && s.position.copy(e), s.name = d.OBJ_NAME, s;
109
+ }
110
+ /**
111
+ * Creates Line
112
+ */
113
+ createLine(e = d.LINE_MATERIAL) {
114
+ const t = new y(), s = new M(t, e);
115
+ return s.frustumCulled = !1, s.name = d.OBJ_NAME, s;
116
+ }
117
+ /**
118
+ * Creates Mesh
119
+ */
120
+ createFaces() {
121
+ const e = new y(), t = new D(e, d.MESH_MATERIAL);
122
+ return t.userData.vertices = [], t.frustumCulled = !1, t.name = d.OBJ_NAME, t;
123
+ }
124
+ /**
125
+ * Draw completed
126
+ */
127
+ done() {
128
+ if (this.mode === null) return;
129
+ let e = !1;
130
+ const t = this.pointArray.length;
131
+ if (this.mode === "Area" && this.polyline)
132
+ if (t > 2) {
133
+ const s = this.pointArray[0];
134
+ this.polyline.geometry.setFromPoints([...this.pointArray, s]);
135
+ const i = this.calculateArea(this.pointArray), n = `${b(k(i, "m", this.options.unit, 2).value, this.options.precision)} ${this.getUnitString()}`, r = this.getBarycenter(this.pointArray), o = this.createLabel(n);
136
+ o.position.copy(r), o.element && (o.element.innerHTML = n), this.labels.push(o);
137
+ } else
138
+ e = !0;
139
+ if (this.mode === "Distance" && t < 2 && (e = !0), this.mode === "Angle" && this.polyline)
140
+ if (t >= 3) {
141
+ const s = this.pointArray[0], i = this.pointArray[1], n = this.pointArray[2], r = new h(s.x - i.x, s.y - i.y, s.z - i.z).normalize(), o = this.getAngleBisector(s, i, n), c = new h(n.x - i.x, n.y - i.y, n.z - i.z).normalize(), p = this.calculateAngle(s, i, n), m = `${b(p, this.options.precision)} ${this.getUnitString()}`, g = Math.min(s.distanceTo(i), n.distanceTo(i));
142
+ let l = g * 0.3, f = i.clone().add(new h(o.x * l, o.y * l, o.z * l));
143
+ const u = this.createLabel(m);
144
+ u.position.set(f.x, f.y, f.z), u.element.innerHTML = m, this.labels.push(u), l = g * 0.2, f = i.clone().add(new h(o.x * l, o.y * l, o.z * l));
145
+ const S = i.clone().add(new h(r.x * l, r.y * l, r.z * l)), z = i.clone().add(new h(c.x * l, c.y * l, c.z * l));
146
+ this.curve = this.createCurve(S, f, z), this.scene.add(this.curve);
147
+ } else
148
+ e = !0;
149
+ e || (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();
150
+ }
151
+ mousemove = (e) => {
152
+ if (this.mode === null) return;
153
+ const t = this.getClosestIntersection(e);
154
+ if (t) {
155
+ if (this.tempPointMarker ? this.tempPointMarker.position.set(t.x, t.y, t.z) : (this.tempPointMarker = this.createPointMarker(t), this.scene.add(this.tempPointMarker)), this.pointArray.length > 0) {
156
+ const s = this.pointArray[this.pointArray.length - 1], i = this.tempLine || this.createLine(), n = i.geometry, r = this.pointArray[0], o = this.pointArray[this.pointArray.length - 1];
157
+ if (this.mode === "Area" ? n.setFromPoints([o, t, r]) : n.setFromPoints([o, t]), this.mode === "Distance") {
158
+ const c = s.distanceTo(t), p = `${b(k(c, "m", this.options.unit).value, this.options.precision)} ${this.getUnitString()}`, m = new h((t.x + s.x) / 2, (t.y + s.y) / 2, (t.z + s.z) / 2);
159
+ this.addOrUpdateTempLabel(p, m);
160
+ }
161
+ this.tempLine || (this.scene.add(i), this.tempLine = i);
162
+ }
163
+ this.ssp.render();
164
+ }
165
+ };
166
+ dblclick = (e) => {
167
+ this.click(e), this.done();
168
+ };
169
+ click = (e) => {
170
+ if (this.mode === null) return;
171
+ const t = this.getClosestIntersection(e);
172
+ if (!t) return;
173
+ const s = Date.now();
174
+ if (this.lastClickTime && s - this.lastClickTime < 300)
175
+ return;
176
+ this.lastClickTime = s, this.pointArray.push(t);
177
+ const i = this.pointArray.length, n = this.createPointMarker(t);
178
+ if (this.pointMarkers.push(n), this.scene.add(n), this.polyline && (this.polyline.geometry.setFromPoints(this.pointArray), this.tempLabel && i > 1)) {
179
+ const r = this.pointArray[i - 2];
180
+ this.tempLabel.position.set((r.x + t.x) / 2, (r.y + t.y) / 2, (r.z + t.z) / 2), this.scene.add(this.tempLabel), this.labels.push(this.tempLabel), this.tempLabel = void 0;
181
+ }
182
+ if (this.mode === "Area" && this.faces) {
183
+ const r = this.faces.geometry, o = this.faces.userData.vertices;
184
+ o.push(t), r.setFromPoints(o);
185
+ const c = o.length;
186
+ if (c > 2) {
187
+ const p = [];
188
+ for (let m = 1; m < c - 1; ++m)
189
+ p.push(0, m, m + 1);
190
+ r.setIndex(p);
191
+ }
192
+ }
193
+ this.mode === "Angle" && this.pointArray.length >= 3 && this.done(), this.ssp.render();
194
+ };
195
+ keydown = (e) => {
196
+ e.key === "Enter" ? this.done() : e.key === "Escape" && this.cancel();
197
+ };
198
+ /**
199
+ * The the closest intersection
200
+ * @param e
201
+ */
202
+ getClosestIntersection = (e) => {
203
+ if (this.mode === null) return;
204
+ let t = this.ssp.viewport.getIntersects(e);
205
+ return t && t.length > 0 && (t = t.filter((s) => s.object.name !== d.OBJ_NAME && s.object.visible), t.length > 0 && t[0].distance < d.MAX_DISTANCE) ? t[0].point : null;
206
+ };
207
+ /**
208
+ * Adds or update temp label and position
209
+ */
210
+ addOrUpdateTempLabel(e, t) {
211
+ this.tempLabel || (this.tempLabel = this.createLabel(e)), this.tempLabel.position.set(t.x, t.y, t.z), this.tempLabel.element.innerHTML = e;
212
+ }
213
+ /**
214
+ * Creates label
215
+ */
216
+ createLabel(e) {
217
+ const t = document.createElement("div");
218
+ t.innerHTML = e, t.style.padding = "3px 6px", t.style.color = "#fff", t.style.fontSize = "12px", t.style.position = "absolute", t.style.backgroundColor = "rgba(25, 25, 25, 0.3)", t.style.borderRadius = "12px", t.style.top = "0px", t.style.left = "0px";
219
+ const s = this.ssp.createPoiNode({ id: v(), element: t, type: "2D" });
220
+ return s.name = d.LABEL_NAME, s;
221
+ }
222
+ /**
223
+ * Creates the arc curve to indicate the angle in degree
224
+ */
225
+ createCurve(e, t, s) {
226
+ const n = new H(e, t, s).getPoints(4), r = new y().setFromPoints(n), o = new M(r, d.LINE_MATERIAL);
227
+ return o.name = d.OBJ_NAME, o;
228
+ }
229
+ /**
230
+ * Calculates area
231
+ * TODO: for concave polygon, the value doesn't right, need to fix it
232
+ * @param points
233
+ */
234
+ calculateArea(e) {
235
+ let t = 0;
236
+ for (let s = 0, i = 1, n = 2; n < e.length; i++, n++) {
237
+ const r = e[s].distanceTo(e[i]), o = e[i].distanceTo(e[n]), c = e[n].distanceTo(e[s]), p = (r + o + c) / 2;
238
+ t += Math.sqrt(p * (p - r) * (p - o) * (p - c));
239
+ }
240
+ return t;
241
+ }
242
+ /**
243
+ * Gets included angle of two lines in degree
244
+ */
245
+ calculateAngle(e, t, s) {
246
+ const i = e, n = t, r = s, o = new h(i.x - n.x, i.y - n.y, i.z - n.z), c = new h(r.x - n.x, r.y - n.y, r.z - n.z);
247
+ return o.angleTo(c) * 180 / Math.PI;
248
+ }
249
+ /**
250
+ * Gets angle bisector of two lines
251
+ */
252
+ getAngleBisector(e, t, s) {
253
+ const i = e, n = t, r = s, o = new h(i.x - n.x, i.y - n.y, i.z - n.z).normalize(), c = new h(r.x - n.x, r.y - n.y, r.z - n.z).normalize();
254
+ return new h(o.x + c.x, o.y + c.y, o.z + c.z).normalize();
255
+ }
256
+ /**
257
+ * Get the barycenter of points
258
+ */
259
+ getBarycenter(e) {
260
+ const t = e.length;
261
+ let s = 0, i = 0, n = 0;
262
+ return e.forEach((r) => {
263
+ s += r.x, i += r.y, n += r.z;
264
+ }), new h(s / t, i / t, n / t);
265
+ }
266
+ /**
267
+ * Gets unit string for distance, area or angle
268
+ */
269
+ getUnitString() {
270
+ return this.mode === "Distance" ? A(this.options.unit) : this.mode === "Area" ? `${A(this.options.unit, 2)}` : this.mode === "Angle" ? "°" : "";
271
+ }
272
+ /**
273
+ * Converts a number to a string with proper fraction digits
274
+ */
275
+ numberToString(e) {
276
+ if (e < 1e-4)
277
+ return e.toString();
278
+ let t = 2;
279
+ return e < 0.01 ? t = 4 : e < 0.1 && (t = 3), e.toFixed(t);
280
+ }
281
+ }
282
+ export {
283
+ T as MeasuringMode,
284
+ d as default
285
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@soonspacejs/plugin-measuring",
3
3
  "pluginName": "MeasuringPlugin",
4
- "version": "2.13.16",
4
+ "version": "2.14.0",
5
5
  "description": "Measuring plugin for SoonSpace.js",
6
6
  "main": "dist/index.esm.js",
7
7
  "module": "dist/index.esm.js",
@@ -13,8 +13,8 @@
13
13
  ],
14
14
  "author": "xunwei",
15
15
  "license": "UNLICENSED",
16
- "gitHead": "aaf0c46565c740277fad34bc3a3ba9fa61d48278",
16
+ "gitHead": "4c85e8b7b8ad24ccb9b42f3a1826bca377c42a6d",
17
17
  "peerDependencies": {
18
- "soonspacejs": "2.13.16"
18
+ "soonspacejs": "2.14.0"
19
19
  }
20
20
  }