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