/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import"../../../../core/has.js";import{generateUID as t}from"../../../../core/uid.js";import{getMetersPerUnitForSR as i}from"../../../../core/units.js";import{subtract as r,distance as s}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as o}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{fromValues as e}from"../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{projectBuffer as n}from"../../../../geometry/projection/projectBuffer.js";import{Attribute as a}from"./Attribute.js";import{Geometry as h}from"./Geometry.js";import{fromValues as c}from"./LocalOriginFactory.js";import{Object3D as m}from"./Object3D.js";import{gridLocalOriginFactory as g}from"./testUtils.js";import{WebGLLayer as _}from"./WebGLLayer.js";import{RibbonLineMaterial as l}from"../materials/RibbonLineMaterial.js";class d{constructor(r){this._originSR=r,this._rootOriginId="root/"+t(),this._origins=new Map,this._objects=new Map,this._gridSize=5e5,this._originSR?.isGeographic&&(this._gridSize/=i(this._originSR)),this._baselineDistance=.5*this._gridSize;const s=this._baselineDistance*b;this._baselineObjectSize=s/p}getOrigin(t){const i=this._origins.get(this._rootOriginId);if(null==i){const i=g.rootOrigin;if(null!=i)return this._origins.set(this._rootOriginId,c(i[0],i[1],i[2],this._rootOriginId)),this.getOrigin(t);const r=c(t[0]+Math.random()-.5,t[1]+Math.random()-.5,t[2]+Math.random()-.5,this._rootOriginId);return this._origins.set(this._rootOriginId,r),r}const s=this._gridSize,o=Math.round(t[0]/s),e=Math.round(t[1]/s),n=Math.round(t[2]/s),a=`${o}/${e}/${n}`;let h=this._origins.get(a);const m=.5*s;if(r(f,t,i.vec3),f[0]=Math.abs(f[0]),f[1]=Math.abs(f[1]),f[2]=Math.abs(f[2]),f[0]<m&&f[1]<m&&f[2]<m){if(h){const i=Math.max(...f);r(f,t,h.vec3),f[0]=Math.abs(f[0]),f[1]=Math.abs(f[1]),f[2]=Math.abs(f[2]);if(Math.max(...f)<i)return h}return i}return h||(h=c(o*s,e*s,n*s,a),this._origins.set(a,h)),h}needsOriginUpdate(t,i,r){const o=s(t.vec3,i),e=Math.max(1,r/this._baselineObjectSize);return o>this._baselineDistance*e}_drawOriginBox(t,i=e(1,1,0,1)){const r=window.view,s=r.stage,o=i.toString();if(!this._objects.has(o)){this._material=new l({width:2,color:i},!1);const t=new _(s,{pickable:!1}),r=new m({castShadow:!1});t.add(r),this._objects.set(o,r)}const c=this._objects.get(o),g=[0,1,5,4,0,2,1,7,6,2,0,1,3,7,5,4,6,2,0],d=g.length,f=new Array(3*d),b=new Array,p=.5*this._gridSize;for(let e=0;e<d;e++)f[3*e]=t[0]+(1&g[e]?p:-p),f[3*e+1]=t[1]+(2&g[e]?p:-p),f[3*e+2]=t[2]+(4&g[e]?p:-p),e>0&&b.push(e-1,e);n(f,this._originSR,0,f,r.renderSpatialReference,0,d);const j=new h(this._material,[["position",new a(f,b,3,!0)]],null,2);c.addGeometry(j)}get test(){}}const f=o(),b=2**-23,p=.05;export{d as GridLocalOriginFactory};
