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