@soonspacejs/plugin-measuring
Version:
Measuring plugin for SoonSpace.js
2 lines (1 loc) • 12.3 kB
JavaScript
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/btPHFZHCeQBM8ArUgrrk9td1enJefP6taj60MZZtKGvDi19Vy8Ief3wfEkRIC0YA16QTjbllod73cH3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nu0+1OK43Dpogt74cZRy4tFR9+4n8bnm7R8nhKM/NkzwQQjoAF6S3DmViIPNJWundpL8BHS8bHISlAPnSdfy1Pz23ifxueNrqy2OBBd2mlw9Wwd/KJ1316dXjlXF54avHLynNnQKO+pnTe6CKKAreEy69ent4f7jRmyDZ+Mbp7sc+fPiAZPTIA9YC35R3C9zbz6K2AfLxxfnfnHC6YV5k+sHQCtwiRTZnFseHXVvXIvuZByCE3af+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};