/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{roundDownToNearest as t}from"../../../../core/mathUtils.js";import{create as s}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{set as i}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{create as o}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{set as n,dot as e,subtract as a,scale as r,add as _,copy as h,scaleAndAdd as u,normalize as m,cross as l}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as f}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{newDoubleArray as c}from"../../../../geometry/support/DoubleArray.js";import{create as B,fromArray as p,getNormal as d}from"../../../../geometry/support/plane.js";class V{constructor(){this._mean=f(),this._plane=B(),this._u=f(),this._v=f(),this._w=f(),this._minBound=o(),this._maxBound=o(),this._bound1=f(),this._bound2=f(),this._bound3=f(),this._mapBasis=s(),this._east=f(),this._north=f(),this._up=f(),this._flatPositionBuffer=null}_initializeUVBasis(){n(this._u,1,0,0),n(this._v,0,1,0),n(this._w,0,0,1)}_resetUVBounds(){i(this._minBound,Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY),i(this._maxBound,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY)}_forEachVertexOffset(t,s,i){if(null!=t)for(const o of t)i(3*o);else for(let o=0;o<3*s;o+=3)i(o)}_updateUVBounds(t,s=null){const i=this._getVertexCount(t,s);this._forEachVertexOffset(s,i,s=>this._updateUVBoundsForPoint(t,s))}_updateUVBoundsForPoint(t,s){n(I,t[s],t[s+1],t[s+2]);const i=e(this._u,I),o=e(this._v,I);this._minBound[0]=Math.min(this._minBound[0],i),this._minBound[1]=Math.min(this._minBound[1],o),this._maxBound[0]=Math.max(this._maxBound[0],i),this._maxBound[1]=Math.max(this._maxBound[1],o)}prepareUVFrame(t,s,o,e=null){if(null!=e&&0===e.length)return this._initializeUVBasis(),n(this._mean,0,0,0),i(this._minBound,0,0),i(this._maxBound,0,0),[0,0];this._initializeUVBasis(),this._computeMeanVertexPosition(t,e);const a=null==e?t:this._toFlatPositionBuffer(t,e);return p(this._plane,a)&&this._calculatePlanarUVcoordinateSystem(s),this._resetUVBounds(),this._updateUVBounds(t,e),b(this._minBound[0],this._minBound[1],o)}_toFlatPositionBuffer(t,s){const i=3*s.length;null!=this._flatPositionBuffer&&this._flatPositionBuffer.length===i||(this._flatPositionBuffer=c(i));const{_flatPositionBuffer:o}=this;let n=0;for(const e of s){const s=3*e;o[n++]=t[s],o[n++]=t[s+1],o[n++]=t[s+2]}return o}_writeMapSpaceUVForPoint(t,s,i,o,a,r,_,h){n(I,i,o,a),t[s]=(e(this._u,I)-r)/h,t[s+1]=(e(this._v,I)-_)/h,t[s+2]=r/h,t[s+3]=_/h}writeMapSpaceUVs(t,s,i,o,n,e=null){const a=this._getVertexCount(s,e);this._forEachVertexOffset(e,a,e=>{const a=e/3*4;this._writeMapSpaceUVForPoint(t,a,s[e],s[e+1],s[e+2],i,o,n)})}writeBoundingRect(t,s){const{_bound1:i,_bound2:o,_bound3:n,_maxBound:h,_minBound:u,_u:m,_v:l,_w:f,_mean:c}=this,B=e(f,c);x(i,u[0],u[1],B,m,l,f),x(o,h[0],u[1],B,m,l,f),x(n,u[0],h[1],B,m,l,f),a(o,o,i),r(o,o,.5),a(n,n,i),r(n,n,.5),_(i,i,o),_(i,i,n);for(let e=0;e<3;++e)t[s+e]=i[e],t[s+e+3]=o[e],t[s+e+6]=n[e]}_calculatePlanarUVcoordinateSystem(t){const s=this._u,i=this._v,o=this._w,a=this._east,_=this._north,f=this._up;null!=t?(t.basisMatrixAtPosition(this._mean,this._mapBasis),n(a,this._mapBasis[0],this._mapBasis[1],this._mapBasis[2]),n(_,this._mapBasis[4],this._mapBasis[5],this._mapBasis[6]),n(f,this._mapBasis[8],this._mapBasis[9],this._mapBasis[10])):(n(a,1,0,0),n(_,0,1,0),n(f,0,0,1));const c=d(this._plane);h(P,c),e(P,f)<0&&r(P,P,-1),h(o,P);const B=e(P,_),p=e(P,a);Math.abs(B)>Math.abs(p)?(u(s,a,P,-p),m(s,s),l(i,s,P),m(i,i),r(i,i,-1)):(u(i,_,P,-B),m(i,i),l(s,i,P),m(s,s))}_computeMeanVertexPosition(t,s=null){const i=this._getVertexCount(t,s);if(0===i)return void n(this._mean,0,0,0);const o=g;n(o,0,0,0),this._forEachVertexOffset(s,i,s=>{o[0]+=t[s],o[1]+=t[s+1],o[2]+=t[s+2]}),r(this._mean,o,1/i)}_getVertexCount(t,s){return null==s?t.length/3:s.length}}function x(t,s,i,o,e,a,r){n(t,s*e[0]+i*a[0]+o*r[0],s*e[1]+i*a[1]+o*r[1],s*e[2]+i*a[2]+o*r[2])}function b(s,i,o){return[t(s,o),t(i,o)]}const I=f(),P=f(),g=f();export{V as UVWorkspace,b as computeUVCellStart};
