"use strict";exports.__esModule=!0,exports.default=void 0;var _extension=require("../../core/extension"),_three=require("three"),_tool=require("../../webgl/tool"),_default=exports.default=(0,_extension.definePlugin)("tool",{setup(a,b){function c(a){let b=20037508.34*a.longitude/180,c=Math.log(Math.tan((90+a.latitude)*Math.PI/360))/(Math.PI/180);return c=20037508.34*c/180,{x:b,y:c}}function d(a){let b=180*(a.x/20037508.34),c=180*(a.y/20037508.34);return c=180/Math.PI*(2*Math.atan(Math.exp(c*Math.PI/180))-Math.PI/2),{longitude:b,latitude:c}}return{containsComponent:(a,c="all")=>{if("all"===c){let c=new _three.Box3().setFromPoints(a.map(a=>new _three.Vector3(a.x,a.y,a.z)));return b.filter(a=>c.containsBox(new _three.Box3().setFromObject(a.model)))}else{const d=a=>"x"===c?new _three.Vector2(a.y,a.z):"y"===c?new _three.Vector2(a.x,a.z):new _three.Vector2(a.z,a.y);let e=new _three.Box2().setFromPoints(a.map(a=>d(a)));return b.filter(a=>e.containsPoint(d(a.model.position)))}},buildCoordinateConvertor:a=>{if(0===a.length)throw Error("\u8BF7\u7ED9\u51FA\u4E00\u5230\u4E24\u4E2Agps\u5750\u6807\u548C\u4E09\u7EF4\u5750\u6807\u7684\u6620\u5C04\u503C");let b={x:0,y:0},e=a[0],f=c(e.gps);for(let d=1;d<a.length;d++){let g=a[d],h=c(g.gps);g.vector3.x!=e.vector3.x&&(b.x+=Math.abs((h.x-f.x)/(g.vector3.x-e.vector3.x))),g.vector3.z!=e.vector3.z&&(b.y+=Math.abs((h.y-f.y)/(g.vector3.z-e.vector3.z)))}let g=a.length,h=1===g?b.x:b.x/(g-1),i=1===g?b.y:b.y/(g-1);0==h&&(h=1),0===i&&(i=1);return{toVector3:b=>{let d=c(b),e=a.reduce((a,b)=>{let e=c(b.gps),f=d.x-e.x,g=d.y-e.y;return a.x+=b.vector3.x-f/h,a.y+=b.vector3.z+g/i,a},{x:0,y:0});return{x:e.x/g,z:e.y/g,y:0}},toGps:b=>{let e=a.reduce((a,d)=>{let e=b.x-d.vector3.x,f=b.z-d.vector3.z,g=c(d.gps);return a.x+=g.x-e*h,a.y+=g.y+f*i,a},{x:0,y:0}),f={x:e.x/g,y:e.y/g};return d(f)}}},calculateDistance:a=>a.reduce((b,c,d)=>{if(0===d)return 0;else{let e=a[d-1];return b+new _three.Vector3(c.x,c.y,c.z).distanceTo(new _three.Vector3(e.x,e.y,e.z))}},0),createMeasure:a=>b=>new _three.Vector3(a.x,a.y,a.z).distanceTo(new _three.Vector3(b.x,b.y,b.z)),threeCoordinateToScreenPoint:a=>(0,_tool.threeCoordinateToScreenPoint)(a,b.container,b.camera),screenPointToThreeCoordinate:(a,c)=>{let d=b.container,e=d.getBoundingClientRect(),f=a-e.left,g=c-e.top,h=2*(f/d.offsetWidth)-1,i=2*-(g/d.offsetHeight)+1,j=new _three.Vector3(h,i,.5);j.unproject(b.camera);let k=b.camera.position,l=j.sub(k).normalize(),m=new _three.Raycaster(k,l),n=m.intersectObjects(b.scene.children);return 0<n.length?n[0].point:null}}}});