@layoutit/polycss-core 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- var cc="cubic";function Zo(e){let n=e.replace("#","");if(n.length===3){let t=parseInt(n[0]+n[0],16),r=parseInt(n[1]+n[1],16),o=parseInt(n[2]+n[2],16);return Number.isNaN(t)||Number.isNaN(r)||Number.isNaN(o)?null:{rgb:[t,r,o],alpha:1}}if(n.length===6){let t=parseInt(n.slice(0,2),16),r=parseInt(n.slice(2,4),16),o=parseInt(n.slice(4,6),16);return Number.isNaN(t)||Number.isNaN(r)||Number.isNaN(o)?null:{rgb:[t,r,o],alpha:1}}return null}function Qo(e){let n=e.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)/i);return n?{rgb:[Number(n[1]),Number(n[2]),Number(n[3])],alpha:n[4]?Number(n[4]):1}:null}function _n(e){if(!e)return null;let n=e.trim(),t=Zo(n);return t||Qo(n)}function qe(e){return Math.max(0,Math.min(255,Math.round(e)))}function rt(e){let[n,t,r]=e.rgb.map(qe);return e.alpha<1?`rgba(${n}, ${t}, ${r}, ${e.alpha})`:`rgb(${n}, ${t}, ${r})`}var Qt={rgb:[204,204,204],alpha:1},Jo=new Map;function an(e){if(!e)return null;let n=e.trim(),t=Jo.get(n);if(t)return t;let r=_n(n);return r?(Jo.set(n,r),r):null}function lc(e,n){let t=an(e)??Qt,r=[qe(t.rgb[0]+n),qe(t.rgb[1]+n),qe(t.rgb[2]+n)];return rt({rgb:r,alpha:t.alpha})}var Zt={direction:[0,0,-1],color:"#ffffff",intensity:1},es={color:"#ffffff",intensity:.4};function ns(e){let n=Math.hypot(e[0],e[1],e[2]);return n<1e-12?[0,0,0]:[e[0]/n,e[1]/n,e[2]/n]}function ts(e,n,t){let r=an(n)??Qt;return e*(r.rgb[t]/255)}function uc(e,n,t,r){let o=an(n)??Qt,s=ns(t?.direction??Zt.direction),i=t?.color??Zt.color,a=Math.max(0,t?.intensity??Zt.intensity),c=r?.color??es.color,l=Math.max(0,r?.intensity??es.intensity),u=ns(e),f=Math.max(0,-(u[0]*s[0]+u[1]*s[1]+u[2]*s[2])),d=a*f,m=[0,0,0];for(let g=0;g<3;g=g+1){let h=o.rgb[g],p=ts(h,c,g)*l,x=ts(h,i,g)*d;m[g]=qe(p+x)}return rt({rgb:m,alpha:o.alpha})}var rs="#cccccc",ot=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],os=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],ss=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],st=e=>Math.hypot(e[0],e[1],e[2]);function fc(e){let n=1/0,t=1/0,r=1/0,o=-1/0,s=-1/0,i=-1/0;for(let u of e)u[0]<n&&(n=u[0]),u[0]>o&&(o=u[0]),u[1]<t&&(t=u[1]),u[1]>s&&(s=u[1]),u[2]<r&&(r=u[2]),u[2]>i&&(i=u[2]);let a=o-n,c=s-t,l=i-r;return Math.hypot(a,c,l)}function mc(){let e=globalThis;if(e.__POLYCSS_DEV__===!0)return!0;if(e.__POLYCSS_DEV__===!1)return!1;let n=e.process?.env?.NODE_ENV;return typeof n=="string"?n!=="production":!1}function Jt(e){let n=[],t=[];if(!e||e.length===0)return{polygons:n,warnings:t};for(let r=0;r<e.length;r++){let o=e[r];if(!o||!Array.isArray(o.vertices)){t.push(`Polygon ${r}: missing vertices, dropped`);continue}let s=o.vertices;if(s.length<3){t.push(`Polygon ${r}: ${s.length} vertices (need >= 3), dropped`);continue}let i=ot(s[1],s[0]),a=ot(s[2],s[0]),c=os(i,a),l=st(c);if(s.length===3){if(l<1e-12){st(i)<1e-12||st(a)<1e-12?t.push(`Polygon ${r}: zero-area triangle (coincident vertices), dropped`):t.push(`Polygon ${r}: vertices collinear, dropped`);continue}}else if(l<1e-12){t.push(`Polygon ${r}: first 3 vertices collinear, dropped`);continue}let u=[{...o,vertices:s.slice()}];if(s.length>=4){let f=[c[0]/l,c[1]/l,c[2]/l],d=ss(f,s[0]),m=fc(s),g=Math.max(1e-6,m*.001),h=0;for(let p=3;p<s.length;p++){let x=Math.abs(ss(f,s[p])-d);x>h&&(h=x)}if(h>g){let p=[],x=o.uvs&&o.uvs.length===s.length?o.uvs:void 0;for(let b=1;b<s.length-1;b++){let P=[s[0].slice(),s[b].slice(),s[b+1].slice()],y=os(ot(P[1],P[0]),ot(P[2],P[0]));if(st(y)<1e-12)continue;let M={...o,vertices:P};x&&(M.uvs=[x[0].slice(),x[b].slice(),x[b+1].slice()]),p.push(M)}if(p.length===0){t.push(`Polygon ${r}: ${s.length} non-coplanar vertices, fan-triangulation produced no valid triangles, dropped`);continue}t.push(`Polygon ${r}: ${s.length} non-coplanar vertices, fan-triangulated to ${p.length} triangles`),u=p}}for(let f of u){let d=dc(f,r,t);d&&n.push(d)}}return{polygons:n,warnings:t}}function dc(e,n,t){let r={vertices:e.vertices},o=e.texture;if(typeof o=="string"&&o===""&&(o=void 0),e.color!==void 0&&o!==void 0&&mc()&&t.push(`Polygon ${n}: color and texture both set; texture wins`),e.color!==void 0&&(an(e.color)?r.color=e.color:(t.push(`Polygon ${n}: invalid color "${e.color}", replaced with ${rs}`),r.color=rs)),o!==void 0&&(r.texture=o,e.textureWrap&&(r.textureWrap={...e.textureWrap}),e.textureAlphaMode&&(r.textureAlphaMode=e.textureAlphaMode)),e.doubleSided===!0&&(r.doubleSided=!0),e.uvs!==void 0&&(!Array.isArray(e.uvs)||e.uvs.length!==e.vertices.length?t.push(`Polygon ${n}: uvs length ${Array.isArray(e.uvs)?e.uvs.length:"?"} != vertices length ${e.vertices.length}, uvs stripped`):r.uvs=e.uvs),e.data!==void 0&&e.data!==null&&typeof e.data=="object"){let s={},i=!1;for(let a of Object.keys(e.data)){let c=e.data[a];typeof c=="string"||typeof c=="number"||typeof c=="boolean"?s[a]=c:(i=!0,t.push(`Polygon ${n}: data["${a}"] has non-primitive value, key dropped`))}Object.keys(s).length>0&&(r.data=s)}return r}function is(e){if(!e||e.length===0)return{min:[0,0,0],max:[0,0,0]};let n=1/0,t=1/0,r=1/0,o=-1/0,s=-1/0,i=-1/0,a=!1;for(let c of e)if(c?.vertices)for(let l of c.vertices)l&&(a=!0,l[0]<n&&(n=l[0]),l[0]>o&&(o=l[0]),l[1]<t&&(t=l[1]),l[1]>s&&(s=l[1]),l[2]<r&&(r=l[2]),l[2]>i&&(i=l[2]));return a?{min:[n,t,r],max:[o,s,i]}:{min:[0,0,0],max:[0,0,0]}}function gc(e){let n=e.polygons??[],t,r;if(e.skipNormalize)t=n,r=[];else{let a=Jt(n);t=a.polygons,r=a.warnings}let o=is(t),s=[o.max[0]-o.min[0],o.max[1]-o.min[1],o.max[2]-o.min[2]];return{context:{polygons:t,sceneBbox:o,warnings:r},dimensions:{sceneBbox:o,size:s},warnings:r}}function pc(e){return!e.vertices||e.vertices.length<3?[]:[{v:e.vertices.map(n=>[n[0],n[1],n[2]]),color:e.color}]}function hc(e){let n=e[0],t=e[1],r=e[2],o=[t[0]-n[0],t[1]-n[1],t[2]-n[2]],s=[r[0]-n[0],r[1]-n[1],r[2]-n[2]],i=o[1]*s[2]-o[2]*s[1],a=o[2]*s[0]-o[0]*s[2],c=o[0]*s[1]-o[1]*s[0],l=Math.hypot(i,a,c);return l<=1e-9?null:[i/l,a/l,c/l]}function xc(e){let n=0;for(let t=0;t<e.length;t++){let[r,o]=e[t],[s,i]=e[(t+1)%e.length];n+=r*i-s*o}return Math.abs(n)/2}function bc(e,n,t){if(!e||e.length<3)return null;let r=e.map(a=>[a[1]*n,a[0]*n,a[2]*t]),o=hc(r);if(!o)return null;let s=1/0,i=0;for(let a=0;a<r.length;a++){let c=r[a],l=r[(a+1)%r.length],u=[l[0]-c[0],l[1]-c[1],l[2]-c[2]],f=u[0]*o[0]+u[1]*o[1]+u[2]*o[2],d=[u[0]-f*o[0],u[1]-f*o[1],u[2]-f*o[2]],m=Math.hypot(d[0],d[1],d[2]);if(m<=1e-9)continue;let g=[d[0]/m,d[1]/m,d[2]/m],h=[o[1]*g[2]-o[2]*g[1],o[2]*g[0]-o[0]*g[2],o[0]*g[1]-o[1]*g[0]],p=Math.hypot(h[0],h[1],h[2]);if(p<=1e-9)continue;let x=[h[0]/p,h[1]/p,h[2]/p],b=r.map(_=>{let w=_[0]-c[0],V=_[1]-c[1],N=_[2]-c[2];return[w*g[0]+V*g[1]+N*g[2],w*x[0]+V*x[1]+N*x[2]]}),P=1/0,y=1/0,M=-1/0,S=-1/0;for(let[_,w]of b)P=Math.min(P,_),y=Math.min(y,w),M=Math.max(M,_),S=Math.max(S,w);let T=M-P,E=S-y,A=Math.max(1,Math.ceil(T))*Math.max(1,Math.ceil(E)),C=xc(b);C<=1e-9||A<s&&(s=A,i=C)}return!Number.isFinite(s)||i<=1e-9?null:{elementArea:s,polygonArea:i}}function yc(e,n={}){let t=n.tileSize??100,r=n.layerElevation??t,o=n.texturedOnly??!0,s=0,i=0,a=0,c=0,l=0;for(let f of e){let d=!!f.texture;if(o&&!d)continue;let m=bc(f.vertices,t,r);m&&(s+=1,d&&(i+=1),a+=m.elementArea,c+=m.polygonArea,l=Math.max(l,Math.max(0,m.elementArea-m.polygonArea)/m.elementArea))}let u=Math.max(0,a-c);return{totalPolygons:e.length,measuredPolygons:s,texturedPolygons:i,elementArea:a,polygonArea:c,transparentArea:u,transparentRatio:a>0?u/a:0,overdrawRatio:c>0?a/c:0,worstTransparentRatio:l}}function it(e,n,t,r){let o=n*Math.PI/180,s=t*Math.PI/180,i=r*Math.PI/180,[a,c,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[a,c]=[a*u-c*f,a*f+c*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[a,l]=[a*u+l*f,-a*f+l*u]}if(o!==0){let u=Math.cos(o),f=Math.sin(o);[c,l]=[c*u-l*f,c*f+l*u]}return[a,c,l]}function Pc(e,n){let t=-n[0]*Math.PI/180,r=-n[1]*Math.PI/180,o=-n[2]*Math.PI/180,[s,i,a]=e;if(t!==0){let c=Math.cos(t),l=Math.sin(t);[i,a]=[i*c-a*l,i*l+a*c]}if(r!==0){let c=Math.cos(r),l=Math.sin(r);[s,a]=[s*c+a*l,-s*l+a*c]}if(o!==0){let c=Math.cos(o),l=Math.sin(o);[s,i]=[s*c-i*l,s*l+i*c]}return[s,i,a]}var er=Math.PI/180,vn=180/Math.PI,Ac=[1,0,0,0];function Mc(e,n){let[t,r,o,s]=e,[i,a,c,l]=n;return[t*i-r*a-o*c-s*l,t*a+r*i+o*l-s*c,t*c-r*l+o*i+s*a,t*l+r*c-o*a+s*i]}function Sc(e,n){let t=n*.5,r=Math.sin(t);return[Math.cos(t),e[0]*r,e[1]*r,e[2]*r]}function Ec(e){let n=e[0]*er,t=e[1]*er,r=e[2]*er,o=Math.cos(n*.5),s=Math.sin(n*.5),i=Math.cos(t*.5),a=Math.sin(t*.5),c=Math.cos(r*.5),l=Math.sin(r*.5);return[o*i*c-s*a*l,s*i*c+o*a*l,o*a*c-s*i*l,o*i*l+s*a*c]}function Tc(e){let[n,t,r,o]=e,s=2*(t*o+n*r),i=2*(r*o-n*t),a=1-2*(t*t+r*r),c=2*(t*r-n*o),l=1-2*(r*r+o*o),u=Math.max(-1,Math.min(1,s)),f=Math.asin(u);return Math.abs(u)<.99999?[Math.atan2(-i,a)*vn,f*vn,Math.atan2(-c,l)*vn]:[Math.atan2(2*(r*o+n*t),1-2*(t*t+o*o))*vn,f*vn,0]}var at=50;function Cc(e){if(typeof e=="number")return e===0?void 0:e<0?-1:1;if(typeof e=="boolean")return e?-1:1}var ln={target:[0,0,0],rotX:65,rotY:45,zoom:.65,distance:0},as=100,cs=1e4,cn=e=>Math.round(e*as)/as,_c=e=>Math.round(e*cs)/cs;function vc(e={}){let n={target:e.target??[...ln.target],rotX:e.rotX??ln.rotX,rotY:e.rotY??ln.rotY,zoom:e.zoom??ln.zoom,distance:e.distance??ln.distance};function t(o){o.target!==void 0&&(n.target=[cn(o.target[0]),cn(o.target[1]),cn(o.target[2])]),o.rotX!==void 0&&(n.rotX=cn(o.rotX)),o.rotY!==void 0&&(n.rotY=cn(o.rotY)),o.zoom!==void 0&&(n.zoom=_c(o.zoom)),o.distance!==void 0&&(n.distance=cn(o.distance))}function r(o={}){let i=(o.cols??0)*50,a=(o.rows??0)*50,[c,l,u]=n.target,f=l*50,d=c*50,m=u*50;return{transform:`${n.distance!==0?` translateZ(${-n.distance}px)`:""}scale(${n.zoom}) rotateX(${n.rotX}deg) rotate(${n.rotY}deg) translate3d(${-f}px, ${-d}px, ${-m}px)`,width:`${i}px`,height:`${a}px`}}return{state:n,update:t,getStyle:r}}var Ge=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],ct=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],In=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],nr=e=>Math.hypot(e[0],e[1],e[2]),un=(e,n)=>e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2];function Ic(e){return`${e[0]},${e[1]},${e[2]}`}function us(e){if(e.length<3)return null;let n=Ge(e[1],e[0]),t=Ge(e[2],e[0]),r=ct(n,t),o=nr(r);if(o<1e-12)return null;let s=[r[0]/o,r[1]/o,r[2]/o],i=In(s,e[0]);return{normal:s,d:i}}function Vc(e,n){return In(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.05}function Oc(e,n){return In(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.001}function Rc(e,n,t,r){let o=e.vertices.findIndex(E=>un(E,t)),s=e.vertices.findIndex(E=>un(E,r)),i=n.vertices.findIndex(E=>un(E,t)),a=n.vertices.findIndex(E=>un(E,r));if(o<0||s<0||i<0||a<0)return null;let c=e.vertices.length,l=n.vertices.length,u=(o+1)%c===s,f=(i+1)%l===a;if(u===f)return null;let d=u?s:o,m=u?o:s,g=f?a:i,h=f?i:a,p=!!(e.uvs&&n.uvs),x=[],b=p?[]:void 0,P=d;for(;x.push(e.vertices[P]),b&&b.push(e.uvs[P]),P!==m;)P=(P+1)%c;for(P=g;x.push(n.vertices[P]),b&&b.push(n.uvs[P]),P!==h;)P=(P+1)%l;let y=[],M=b?[]:void 0;for(let E=0;E<x.length;E++)(y.length===0||!un(x[E],y[y.length-1]))&&(y.push(x[E]),M&&b&&M.push(b[E]));if(y.length>1&&un(y[0],y[y.length-1])&&(y.pop(),M?.pop()),p)return ls(y,M);let S=[],T=M?[]:void 0;for(let E=0;E<y.length;E++){let A=y[(E-1+y.length)%y.length],C=y[E],_=y[(E+1)%y.length],w=ct(Ge(C,A),Ge(_,A));nr(w)>1e-9&&(S.push(C),T&&M&&T.push(M[E]))}return S.length<3?null:ls(S,T)}function Lc(e,n){let t=e.length,r=0;for(let o=0;o<t;o++){let s=e[o],i=e[(o+1)%t],a=e[(o+2)%t],c=Ge(i,s),l=Ge(a,i),u=In(ct(c,l),n);if(Math.abs(u)<1e-9)continue;let f=u>0?1:-1;if(r===0)r=f;else if(f!==r)return!1}return!0}function wc(e,n){if(e.length<3)return!1;let t=us(e);if(!t)return!1;for(let r of e)if(Math.abs(In(t.normal,r)-t.d)>n)return!1;return!0}function Nc(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function Dc(e,n){return(e?.s??"")===(n?.s??"")&&(e?.t??"")===(n?.t??"")}function Fc(e,n){return(e??"")===(n??"")}function Bc(e,n){let t=[];for(let r=1;r<e.length-1;r++)t.push({vertices:[[...e[0]],[...e[r]],[...e[r+1]]],uvs:[[...n[0]],[...n[r]],[...n[r+1]]]});return t}function ls(e,n){for(let t=0;t<e.length;t++){let r=e[t],o=e[(t+1)%e.length],s=e[(t+2)%e.length];if(!(nr(ct(Ge(o,r),Ge(s,r)))<=1e-9))return t===0?{vertices:e,uvs:n}:{vertices:[...e.slice(t),...e.slice(0,t)],uvs:n?[...n.slice(t),...n.slice(0,t)]:void 0}}return{vertices:e,uvs:n}}function Re(e){let n=[],t=[],r=new WeakMap,o=m=>{let g=r.get(m);if(g)return g;let h=Ic(m);return r.set(m,h),h},s=(m,g)=>{let h=o(m),p=o(g);return h<p?`${h}|${p}`:`${p}|${h}`},i=(m,g)=>`${o(m)}>${o(g)}`,a=m=>{let g=new Set;for(let h=0;h<m.length;h++)g.add(i(m[h],m[(h+1)%m.length]));return g},c=0,l=!1,u=m=>(c+=m,c<=8e6?!0:(l=!0,!1));for(let m of e??[]){if(!m||!m.vertices||m.vertices.length<3){m&&n.push(m);continue}let g=m.vertices,h=us(g);if(!h){n.push(m);continue}let p=m.texture&&m.uvs&&m.uvs.length===g.length?m.uvs.map(b=>[b[0],b[1]]):void 0,x=m.texture&&p?m.textureTriangles?.length?Nc(m.textureTriangles):Bc(g,p):void 0;t.push({vertices:g,uvs:p,color:m.color??"#cccccc",texture:m.texture,textureWrap:m.texture?m.textureWrap:void 0,textureAlphaMode:m.texture?m.textureAlphaMode:void 0,textureTriangles:x,normal:h.normal,d:h.d,directedEdges:a(g),alive:!0,doubleSided:m.doubleSided===!0,data:m.data})}let f=()=>{let m=new Map;for(let p=0;p<t.length;p++){let x=t[p];if(!x.alive)continue;let b=x.vertices.length;if(!u(b))return!1;for(let P=0;P<b;P++){let y=x.vertices[P],M=x.vertices[(P+1)%b],S=s(y,M),T=m.get(S);T?T.second<0&&(T.second=p):(T={a:y,b:M,first:p,second:-1},m.set(S,T))}}let g=!1,h=(p,x,b)=>p.directedEdges.has(i(x,b))?1:p.directedEdges.has(i(b,x))?-1:0;for(let p of m.values()){if(p.second<0)continue;let x=p.first,b=p.second;if(x===b)continue;let P=t[x],y=t[b];if(!P.alive||!y.alive||P.color!==y.color||P.texture!==y.texture||!Dc(P.textureWrap,y.textureWrap)||!Fc(P.textureAlphaMode,y.textureAlphaMode)||P.doubleSided!==y.doubleSided)continue;let M=!!(P.texture||y.texture);if(M&&(!P.textureTriangles||!y.textureTriangles)||!!P.uvs!=!!y.uvs||(M?!Oc(P,y):!Vc(P,y)))continue;if(!u(P.vertices.length+y.vertices.length))return!1;let S=h(P,p.a,p.b);if(S===0)continue;let T=h(y,p.a,p.b);if(T===0||S===T)continue;let[E,A]=S===1?[p.a,p.b]:[p.b,p.a],C=Rc(P,y,E,A);if(C){if(!u(C.vertices.length))return!1;C.vertices.length>96||wc(C.vertices,.001)&&Lc(C.vertices,P.normal)&&(P.vertices=C.vertices,P.uvs=C.uvs,P.directedEdges=a(C.vertices),P.textureTriangles=M?[...P.textureTriangles??[],...y.textureTriangles??[]]:void 0,y.alive=!1,g=!0)}}return g},d=0;for(;!l&&d<16&&f();)d++;for(let m of t){if(!m.alive)continue;let g={vertices:m.vertices.map(h=>[h[0],h[1],h[2]]),color:m.color};m.texture&&(g.texture=m.texture),m.texture&&m.textureWrap&&(g.textureWrap={...m.textureWrap}),m.texture&&m.textureAlphaMode&&(g.textureAlphaMode=m.textureAlphaMode),m.uvs&&(g.uvs=m.uvs.map(h=>[h[0],h[1]])),m.textureTriangles?.length&&(g.textureTriangles=m.textureTriangles),m.doubleSided&&(g.doubleSided=!0),m.data&&(g.data=m.data),n.push(g)}return n}var kc=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Uc=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],Ie=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2];function Gc(e,n){let t=e.vertices;if(!t||t.length<3)return null;let r=0,o=0,s=0;for(let h=0;h<t.length;h++){let p=t[h],x=t[(h+1)%t.length];r+=(p[1]-x[1])*(p[2]+x[2]),o+=(p[2]-x[2])*(p[0]+x[0]),s+=(p[0]-x[0])*(p[1]+x[1])}let i=Math.hypot(r,o,s);if(i<1e-12)return null;let a=[r/i,o/i,s/i],c=0,l=0,u=0;for(let h of t)c+=h[0],l+=h[1],u+=h[2];let f=1/t.length,d=[c*f,l*f,u*f],m=Ie(a,d),g=i*.5;return{index:n,polygon:e,normal:a,d:m,centroid:d,area:g,local2D:null,bbox2D:null,basis:null}}function $c(e,n){return e.polygon.doubleSided===!0&&n.polygon.doubleSided===!0&&Ie(e.normal,n.normal)<0}function zc(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=n[0]-e[0]*Ie(n,e),r=n[1]-e[1]*Ie(n,e),o=n[2]-e[2]*Ie(n,e),s=Math.hypot(t,r,o);t/=s,r/=s,o/=s;let i=[t,r,o],a=Uc(e,i);return{u:i,v:a}}function Xc(e){if(e.local2D)return;let n=zc(e.normal),t=[],r=1/0,o=1/0,s=-1/0,i=-1/0;for(let a of e.polygon.vertices){let c=Ie(a,n.u),l=Ie(a,n.v);t.push([c,l]),c<r&&(r=c),c>s&&(s=c),l<o&&(o=l),l>i&&(i=l)}e.local2D=t,e.bbox2D={min:[r,o],max:[s,i]},e.basis=n}function Wc(e,n){let t=[],r=1/0,o=1/0,s=-1/0,i=-1/0;for(let a of e.vertices){let c=Ie(a,n.u),l=Ie(a,n.v);t.push([c,l]),c<r&&(r=c),c>s&&(s=c),l<o&&(o=l),l>i&&(i=l)}return{local2D:t,bbox2D:{min:[r,o],max:[s,i]}}}function jc(e,n){return e.max[0]>=n.min[0]&&e.min[0]<=n.max[0]&&e.max[1]>=n.min[1]&&e.min[1]<=n.max[1]}function fs(e,n){let t=!1;for(let r=0,o=n.length-1;r<n.length;o=r++){let s=n[r],i=n[o];s[1]>e[1]!=i[1]>e[1]&&e[0]<(i[0]-s[0])*(e[1]-s[1])/(i[1]-s[1]+1e-30)+s[0]&&(t=!t)}return t}function ms(e){let n=0,t=0;for(let r of e)n+=r[0],t+=r[1];return[n/e.length,t/e.length]}function Yc(e,n){let t=ms(e),r=ms(n),o=1e-4,s=0;for(let l of e){let u=[l[0]+(t[0]-l[0])*o,l[1]+(t[1]-l[1])*o];fs(u,n)&&s++}let i=0;for(let l of n){let u=[l[0]+(r[0]-l[0])*o,l[1]+(r[1]-l[1])*o];fs(u,e)&&i++}let a=s/e.length,c=i/n.length;return Math.max(a,c)}function Hc(e,n){let t=e.normal[0],r=e.normal[1],o=e.normal[2],s=Math.abs(t),i=Math.abs(r),a=Math.abs(o),c=t;i>s&&i>a?c=r:a>s&&a>i&&(c=o),c<0&&(t=-t,r=-r,o=-o);let l=Math.round(t/.05),u=Math.round(r/.05),f=Math.round(o/.05),d=e.d*(e.normal[0]===t&&e.normal[1]===r&&e.normal[2]===o?1:-1),m=Math.round(d/(n*2));return`${l},${u},${f}|${m}`}function Kc(e,n,t,r){let o=Ie(e.normal,n.normal);if(Math.abs(o)<1-t)return!1;let s=o>0?1:-1;return Math.abs(e.d-s*n.d)<r}function ds(e,n){let t=kc(n,e.centroid);return Ie(e.normal,t)>0}function lt(e,n){if(!e||e.length<2)return new Set;let t=n?.normalTolerance??.001,r=n?.distanceTolerance??.05,o=n?.overlapFraction??.7,s=n?.preserveDoubleSidedBackfaces??!0,i=[];for(let g=0;g<e.length;g++){let h=Gc(e[g],g);h&&i.push(h)}if(i.length<2)return new Set;let a=0,c=0,l=0,u=0;for(let g of i)a+=g.centroid[0]*g.area,c+=g.centroid[1]*g.area,l+=g.centroid[2]*g.area,u+=g.area;let f=u>0?[a/u,c/u,l/u]:[0,0,0],d=new Map;for(let g of i){let h=Hc(g,r),p=d.get(h);p||(p=[],d.set(h,p)),p.push(g)}let m=new Set;for(let g of d.values())if(!(g.length<2))for(let h=0;h<g.length;h++){let p=g[h];if(!m.has(p.index))for(let x=h+1;x<g.length;x++){let b=g[x];if(m.has(b.index)||!Kc(p,b,t,r)||s&&$c(p,b))continue;Xc(p);let P=Wc(b.polygon,p.basis);if(!jc(p.bbox2D,P.bbox2D)||Yc(p.local2D,P.local2D)<o)continue;let M=ds(p,f),S=ds(b,f),T;if(M&&!S?T=p:S&&!M?T=b:T=p.area<b.area?p:b,m.add(T.index),T===p)break}}return m}function qc(e,n){if(!e||e.length<2)return e??[];let t=lt(e,n);if(t.size===0)return e;let r=[];for(let o=0;o<e.length;o++)t.has(o)||r.push(e[o]);return r}var Zc=4,Qc=8,Jc=.001,el=1e-9,nl=1e6,tl=1e6,rl=8,Ze=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],or=(e,n)=>[e[0]+n[0],e[1]+n[1],e[2]+n[2]],Rn=(e,n)=>[e[0]*n,e[1]*n,e[2]*n],Qe=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],xs=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],bs=e=>Math.hypot(e[0],e[1],e[2]);function Ln(e){let n=bs(e);return n<=el?null:[e[0]/n,e[1]/n,e[2]/n]}function On(e,n){return Math.round(e*n)/n}function Ve(e){return On(e,nl)}function tr(e){return On(e,tl)}function rr(e,n){return Math.abs(e[0]-n[0])<=1e-7&&Math.abs(e[1]-n[1])<=1e-7}function Le(e){return`${Ve(e[0])},${Ve(e[1])}`}function ol(e){let n=Le(e.a),t=Le(e.b);return n<t?`${n}|${t}`:`${t}|${n}`}function fn(e){return`${e[0]},${e[1]},${e[2]}`}function ys(e,n){let t=fn(e),r=fn(n);return t<r?`${t}|${r}`:`${r}|${t}`}function sr(e){return e?Object.keys(e).sort().map(n=>`${n}:${String(e[n])}`).join("|"):""}function gs(e){return[e.color??"#cccccc",e.texture??"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided",sr(e.data)].join("|")}function Ps(e){let n=e.vertices;if(!n||n.length<3)return null;let t=n[0],r=[0,0,0];for(let s=1;s<n.length-1;s++)r=or(r,xs(Ze(n[s],t),Ze(n[s+1],t)));let o=Ln(r);return o?{normal:o,d:Qe(o,t)}:null}function sl(e,n,t){return Qe(e.normal,n.normal)>1-t&&Math.abs(e.d-n.d)<=t}function il(e){return!e.texture&&!e.uvs&&!e.textureTriangles}function al(e,n){let t=n[e[0]]?.data,r=sr(t);for(let o of e)if(sr(n[o].data)!==r)return;return t?{...t}:void 0}function cl(e,n){return e.every(t=>n[t].doubleSided===!0)}function ll(e,n){let t=e.map(c=>il(c)?Ps(c):null),r=t.map(Boolean),o=new Map;for(let c=0;c<e.length;c++){if(!t[c])continue;let l=e[c].vertices;for(let u=0;u<l.length;u++){let f=ys(l[u],l[(u+1)%l.length]),d=o.get(f),m={polygon:c,edge:u};d?d.push(m):o.set(f,[m])}}let s=e.map(()=>new Set);for(let c of o.values())if(!(c.length<2))for(let l=0;l<c.length;l++)for(let u=l+1;u<c.length;u++){let f=c[l].polygon,d=c[u].polygon,m=t[f],g=t[d];!m||!g||gs(e[f])===gs(e[d])&&sl(m,g,n)&&(s[f].add(d),s[d].add(f))}let i=new Set,a=[];for(let c=0;c<e.length;c++){if(!r[c]||i.has(c))continue;let l=[],u=[c];for(i.add(c);u.length>0;){let f=u.shift();l.push(f);for(let d of s[f])i.has(d)||(i.add(d),u.push(d))}a.push(l)}return{groups:a}}function ul(e,n){let t=new Set(e),r=new Map;for(let s of e){let i=n[s].vertices;for(let a=0;a<i.length;a++){let c=ys(i[a],i[(a+1)%i.length]),l=r.get(c),u={polygon:s,edge:a};l?l.push(u):r.set(c,[u])}}let o=[];for(let s of r.values()){let i=s.filter(a=>t.has(a.polygon));if(i.length===1){let a=i[0],c=n[a.polygon].vertices;o.push({a:c[a.edge],b:c[(a.edge+1)%c.length]})}else if(i.length!==2)return null}return o}function fl(e){let n=new Map;for(let t of e)n.set(fn(t.a),(n.get(fn(t.a))??0)+1),n.set(fn(t.b),(n.get(fn(t.b))??0)+1);for(let t of n.values())if(t%2!==0)return!1;return!0}function As(e){let n=[e[0],e[1],e[2]],t=Math.abs(n[0])>=Math.abs(n[1])&&Math.abs(n[0])>=Math.abs(n[2])?0:Math.abs(n[1])>=Math.abs(n[2])?1:2;return n[t]<0?[-n[0],-n[1],-n[2]]:n}function ml(e){let n=As(e);return`${On(n[0],1e3)},${On(n[1],1e3)},${On(n[2],1e3)}`}function dl(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=Ze(n,Rn(e,Qe(n,e)));return Ln(t)??[1,0,0]}function gl(e,n,t){let r=new Map;for(let s of e){let i=Ze(s.b,s.a),a=Ze(i,Rn(n,Qe(i,n))),c=Ln(a);if(!c)continue;let l=As(c),u=ml(l),f=bs(a),d=r.get(u);d?d.weight+=f:r.set(u,{axis:l,weight:f})}let o=[...r.values()].sort((s,i)=>i.weight-s.weight).slice(0,t).map(s=>s.axis);return o.length===0&&o.push(dl(n)),o}function ps(e,n,t,r){let o=Ze(e,n);return[Ve(Qe(o,t)),Ve(Qe(o,r))]}function pl(e){let n=[...e].sort((r,o)=>r-o),t=[];for(let r of n)(t.length===0||Math.abs(r-t[t.length-1])>1e-7)&&t.push(r);return t}function Vn(e,n){let[t,r]=e.a,[o,s]=e.b,i=(n-t)/(o-t);return Ve(r+(s-r)*i)}function ir(e){let n=[];for(let r of e)(n.length===0||Math.abs(r[0]-n[n.length-1][0])>1e-7||Math.abs(r[1]-n[n.length-1][1])>1e-7)&&n.push(r);n.length>1&&Math.abs(n[0][0]-n[n.length-1][0])<=1e-7&&Math.abs(n[0][1]-n[n.length-1][1])<=1e-7&&n.pop();let t=[];for(let r=0;r<n.length;r++){let o=n[(r-1+n.length)%n.length],s=n[r],i=n[(r+1)%n.length],a=(s[0]-o[0])*(i[1]-s[1])-(s[1]-o[1])*(i[0]-s[0]);Math.abs(a)>1e-8&&t.push(s)}return t}function ft(e){let n=0;for(let t=0;t<e.length;t++){let r=e[t],o=e[(t+1)%e.length];n+=r[0]*o[1]-o[0]*r[1]}return n/2}function mt(e){let n=1/0,t=1/0,r=-1/0,o=-1/0;for(let[s,i]of e)n=Math.min(n,s),t=Math.min(t,i),r=Math.max(r,s),o=Math.max(o,i);return{minX:n,minY:t,maxX:r,maxY:o}}function Ms(e,n){return!(e.maxX<n.minX-1e-7||n.maxX<e.minX-1e-7||e.maxY<n.minY-1e-7||n.maxY<e.minY-1e-7)}function ut(e){return Math.abs(ft(e))}function hl(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let r=e[t],o=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(o[0]-r[0])*(s[1]-o[1])-(o[1]-r[1])*(s[0]-o[0]);if(Math.abs(i)<=1e-8)continue;let a=i>0?1:-1;if(n===0)n=a;else if(n!==a)return!1}return!0}function xl(e,n,t){let r=t[0]-n[0],o=t[1]-n[1],s=e[0]-n[0],i=e[1]-n[1],a=r*i-o*s,c=Math.hypot(r,o);if(c<=1e-9||Math.abs(a)>Math.max(1e-8,c*1e-8))return!1;let l=s*r+i*o;return l>=-1e-8&&l<=r*r+o*o+1e-8}function bl(e,n,t){let r=t[0]-n[0],o=t[1]-n[1],s=r*r+o*o;return s<=1e-12?0:((e[0]-n[0])*r+(e[1]-n[1])*o)/s}function yl(e,n,t,r){let o=n[0]-e[0],s=n[1]-e[1],i=r[0]-t[0],a=r[1]-t[1],c=Math.hypot(o,s),l=Math.hypot(i,a);if(c<=1e-9||l<=1e-9)return!1;let u=o*(t[1]-e[1])-s*(t[0]-e[0]),f=o*(r[1]-e[1])-s*(r[0]-e[0]),d=o*a-s*i,m=Math.max(1e-8,Math.max(c,l)*1e-8);if(Math.abs(u)>m||Math.abs(f)>m||Math.abs(d)>m)return!1;let g=Math.abs(o)>=Math.abs(s),h=Math.min(g?e[0]:e[1],g?n[0]:n[1]),p=Math.max(g?e[0]:e[1],g?n[0]:n[1]),x=Math.min(g?t[0]:t[1],g?r[0]:r[1]),b=Math.max(g?t[0]:t[1],g?r[0]:r[1]);return Math.min(p,b)-Math.max(h,x)>1e-8}function Pl(e,n){for(let t=0;t<e.length;t++){let r=e[t],o=e[(t+1)%e.length];for(let s=0;s<n.length;s++)if(yl(r,o,n[s],n[(s+1)%n.length]))return!0}return!1}function hs(e,n){let t=[];for(let r=0;r<e.length;r++){let o=e[r],s=e[(r+1)%e.length],i=[{t:0,point:o},{t:1,point:s}];for(let c of n)rr(c,o)||rr(c,s)||xl(c,o,s)&&i.push({t:bl(c,o,s),point:c});i.sort((c,l)=>c.t-l.t);let a=[];for(let c of i)a.some(l=>Math.abs(l.t-c.t)<=1e-8||rr(l.point,c.point))||a.push(c);for(let c=0;c<a.length-1;c++){let l=a[c].point,u=a[c+1].point;Math.hypot(u[0]-l[0],u[1]-l[1])<=1e-8||t.push({a:[Ve(l[0]),Ve(l[1])],b:[Ve(u[0]),Ve(u[1])]})}}return t}function Al(e,n,t=mt(e),r=mt(n)){if(!Ms(t,r)||!Pl(e,n))return null;let o=[...hs(e,n),...hs(n,e)],s=new Map;for(let x of o){let b=ol(x),P=s.get(b);P?P.push(x):s.set(b,[x])}let i=!1,a=[];for(let x of s.values()){if(x.length===1){a.push(x[0]);continue}i=!0;let b=x.filter(y=>Le(y.a)<Le(y.b)).length,P=x.length-b;if(b!==P)return null}if(!i||a.length<3)return null;let c=new Map;for(let x of a){let b=Le(x.a);if(c.has(b))return null;c.set(b,x)}let l=a[0],u=Le(l.a),f=[],d=new Set,m=u;for(let x=0;x<=a.length;x++){let b=c.get(m);if(!b)return null;let P=`${Le(b.a)}>${Le(b.b)}`;if(d.has(P))return null;if(d.add(P),f.push(b.a),m=Le(b.b),m===u)break}if(m!==u||d.size!==a.length)return null;let g=ir(f);if(g.length<3||!hl(g))return null;let h=ut(g),p=ut(e)+ut(n);return Math.abs(h-p)>Math.max(1e-5,p*1e-5)?null:ft(g)>=0?g:[...g].reverse()}function Ml(e){let n=e.map(ir).filter(o=>o.length>=3&&ut(o)>1e-8),t=n.map(mt),r=!0;for(;r;){r=!1;for(let o=0;o<n.length;o++){for(let s=o+1;s<n.length;s++){if(!Ms(t[o],t[s]))continue;let i=Al(n[o],n[s],t[o],t[s]);if(i){n[o]=i,t[o]=mt(i),n.splice(s,1),t.splice(s,1),r=!0;break}}if(r)break}}return n}function Sl(e,n,t){let r=new Map;return o=>{let s=Ve(o[0]),i=Ve(o[1]),a=`${s},${i}`,c=r.get(a);if(c)return[c[0],c[1],c[2]];let l=or(e,or(Rn(n,s),Rn(t,i))),u=[tr(l[0]),tr(l[1]),tr(l[2])];return r.set(a,u),[u[0],u[1],u[2]]}}function El(e,n,t,r,o){let s=t[0]?.a;if(!s)return null;let i=Ln(Ze(o,Rn(r,Qe(o,r))));if(!i)return null;let a=Ln(xs(r,i));if(!a)return null;let c=[],l=[];for(let x of t){let b=ps(x.a,s,i,a),P=ps(x.b,s,i,a);Math.hypot(b[0]-P[0],b[1]-P[1])<=1e-7||(c.push({a:b,b:P}),l.push(b[0],P[0]))}let u=pl(l);if(c.length<3||u.length<2)return null;let f=n[e[0]].color,d=al(e,n),m=cl(e,n),g=[];for(let x=0;x<u.length-1;x++){let b=u[x],P=u[x+1];if(P-b<=1e-7)continue;let y=(b+P)/2,M=c.filter(S=>{let T=Math.min(S.a[0],S.b[0]),E=Math.max(S.a[0],S.b[0]);return T<y&&y<E&&Math.abs(S.a[0]-S.b[0])>1e-7}).map(S=>({segment:S,yMid:Vn(S,y)})).sort((S,T)=>S.yMid-T.yMid);if(M.length!==0){if(M.length%2!==0)return null;for(let S=0;S<M.length;S+=2){let T=M[S].segment,E=M[S+1].segment,A=Vn(T,b),C=Vn(T,P),_=Vn(E,b),w=Vn(E,P),V=ir([[b,A],[P,C],[P,w],[b,_]]);if(V.length<3||Math.abs(ft(V))<=1e-8)continue;let N=ft(V)>0?V:[...V].reverse();g.push(N)}}}if(g.length===0||e.length>2&&g.length>e.length*rl)return null;let h=Sl(s,i,a),p=Ml(g).map(x=>({vertices:x.map(h),...f?{color:f}:{},...m?{doubleSided:!0}:{},...d?{data:d}:{}}));return Re(p)}function Tl(e,n,t){let r=ul(e,n);if(!r||r.length<3||!fl(r))return null;let o=Ps(n[e[0]]);if(!o)return null;let s=null;for(let i of gl(r,o.normal,t)){let a=El(e,n,r,o.normal,i);a&&(!s||a.length<s.length)&&(s=a)}return!s||s.length>=e.length?null:s}function ar(e,n={}){let t=n.minGroupPolygons??Zc,r=n.maxCandidateAxes??Qc,o=n.planeEpsilon??Jc,s=e??[];if(s.length<t)return s;let{groups:i}=ll(s,o);if(i.length===0)return s;let a=new Map,c=new Set;for(let u of i){if(u.length<t)continue;let f=Tl(u,s,r);if(f){a.set(u[0],f);for(let d of u)c.add(d)}}if(a.size===0)return s;let l=[];for(let u=0;u<s.length;u++){let f=a.get(u);if(f){l.push(...f);continue}c.has(u)||l.push(s[u])}return l}function Cl(e,n){let t=`${e[0]},${e[1]},${e[2]}`,r=`${n[0]},${n[1]},${n[2]}`;return t<r?`${t}|${r}`:`${r}|${t}`}function _l(e){if(e.length<100)return!1;let n=new Map;for(let r of e){let o=r.vertices;for(let s=0;s<o.length;s++){let i=Cl(o[s],o[(s+1)%o.length]);n.set(i,(n.get(i)??0)+1)}}let t=0;for(let r of n.values())r===1&&(t+=1);return t>=128&&t/Math.max(1,n.size)>.2}function vl(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,r=0,o=0;for(let[V,N,F]of n)t+=V,r+=N,o+=F;let s=1/n.length;t*=s,r*=s,o*=s;let i=n[0],a=n[1],c=n[2],l=a[0]-i[0],u=a[1]-i[1],f=a[2]-i[2],d=c[0]-i[0],m=c[1]-i[1],g=c[2]-i[2],h=u*g-f*m,p=f*d-l*g,x=l*m-u*d,b=Math.hypot(h,p,x);if(b<1e-9)return null;h/=b,p/=b,x/=b;let P=n.length-2,y=new Float64Array(P*9),M=0;for(let V=1;V<n.length-1;V++){let N=n[0],F=n[V],k=n[V+1];y[M++]=N[0],y[M++]=N[1],y[M++]=N[2],y[M++]=F[0],y[M++]=F[1],y[M++]=F[2],y[M++]=k[0],y[M++]=k[1],y[M++]=k[2]}let S=0,T=1/0,E=1/0,A=1/0,C=-1/0,_=-1/0,w=-1/0;for(let[V,N,F]of n){let k=V-t,$=N-r,R=F-o,B=k*k+$*$+R*R;B>S&&(S=B),V<T&&(T=V),V>C&&(C=V),N<E&&(E=N),N>_&&(_=N),F<A&&(A=F),F>w&&(w=F)}return{centroid:[t,r,o],normal:[h,p,x],vertices:n,triFlat:y,bcx:t,bcy:r,bcz:o,br2:S,minX:T,minY:E,minZ:A,maxX:C,maxY:_,maxZ:w}}function Il(e,n,t,r,o,s,i,a){let c=i[a],l=i[a+1],u=i[a+2],f=i[a+3]-c,d=i[a+4]-l,m=i[a+5]-u,g=i[a+6]-c,h=i[a+7]-l,p=i[a+8]-u,x=o*p-s*h,b=s*g-r*p,P=r*h-o*g,y=f*x+d*b+m*P;if(y>-1e-9&&y<1e-9)return!1;let M=1/y,S=e-c,T=n-l,E=t-u,A=M*(S*x+T*b+E*P);if(A<0||A>1)return!1;let C=T*m-E*d,_=E*f-S*m,w=S*d-T*f,V=M*(r*C+o*_+s*w);return V<0||A+V>1?!1:M*(g*C+h*_+p*w)>.001}function Vl(e,n,t,r,o,s,i){let a=i.bcx-e,c=i.bcy-n,l=i.bcz-t,u=a*r+c*o+l*s,f=a-u*r,d=c-u*o,m=l-u*s;if(f*f+d*d+m*m>i.br2)return!1;let g=i.triFlat,h=g.length;for(let p=0;p<h;p+=9)if(Il(e,n,t,r,o,s,g,p))return!0;return!1}var wn=9,Ol=6,ge=12;function cr(e,n,t,r,o,s){let i=r-e,a=o-n,c=s-t;return i*a+a*c+c*i}function Rl(e){let n=[];for(let S=0;S<e.length;S++)e[S]&&n.push(S);let t=n.length,r=new Int32Array(t);for(let S=0;S<t;S++)r[S]=n[S];let o=new Float64Array(t),s=new Float64Array(t),i=new Float64Array(t);for(let S=0;S<t;S++){let T=e[r[S]];o[S]=(T.minX+T.maxX)*.5,s[S]=(T.minY+T.maxY)*.5,i[S]=(T.minZ+T.maxZ)*.5}let a=2*Math.max(1,t)+1,c=new Float64Array(a*wn),l=0,u=new Float64Array(ge),f=new Float64Array(ge),d=new Float64Array(ge),m=new Float64Array(ge),g=new Float64Array(ge),h=new Float64Array(ge),p=new Int32Array(ge),x=new Float64Array(ge-1),b=new Int32Array(ge-1),P=new Float64Array(ge-1),y=new Int32Array(ge-1);function M(S,T){let E=l++,A=E*wn,C=T-S,_=1/0,w=1/0,V=1/0,N=-1/0,F=-1/0,k=-1/0;for(let K=S;K<T;K++){let te=e[r[K]];te.minX<_&&(_=te.minX),te.maxX>N&&(N=te.maxX),te.minY<w&&(w=te.minY),te.maxY>F&&(F=te.maxY),te.minZ<V&&(V=te.minZ),te.maxZ>k&&(k=te.maxZ)}if(c[A]=_,c[A+1]=w,c[A+2]=V,c[A+3]=N,c[A+4]=F,c[A+5]=k,C<=Ol)return c[A+6]=1,c[A+7]=S,c[A+8]=T,E;let $=1/0,R=1/0,B=1/0,z=-1/0,ee=-1/0,Q=-1/0;for(let K=S;K<T;K++)o[K]<$&&($=o[K]),o[K]>z&&(z=o[K]),s[K]<R&&(R=s[K]),s[K]>ee&&(ee=s[K]),i[K]<B&&(B=i[K]),i[K]>Q&&(Q=i[K]);let Z=z-$,ne=ee-R,j=Q-B;if(Z===0&&ne===0&&j===0)return c[A+6]=1,c[A+7]=S,c[A+8]=T,E;let D=cr(_,w,V,N,F,k),I=D>0?1/D:0,U=C+1,G=0,q=0;for(let K=0;K<3;K++){let te=K===0?$:K===1?R:B,se=K===0?Z:K===1?ne:j;if(se===0)continue;let he=K===0?o:K===1?s:i,Se=ge/se;u.fill(1/0),f.fill(1/0),d.fill(1/0),m.fill(-1/0),g.fill(-1/0),h.fill(-1/0),p.fill(0);for(let W=S;W<T;W++){let J=(he[W]-te)*Se|0;J>=ge&&(J=ge-1);let oe=e[r[W]];oe.minX<u[J]&&(u[J]=oe.minX),oe.maxX>m[J]&&(m[J]=oe.maxX),oe.minY<f[J]&&(f[J]=oe.minY),oe.maxY>g[J]&&(g[J]=oe.maxY),oe.minZ<d[J]&&(d[J]=oe.minZ),oe.maxZ>h[J]&&(h[J]=oe.maxZ),p[J]++}let le=1/0,me=1/0,pe=1/0,ve=-1/0,ye=-1/0,Ee=-1/0,xe=0;for(let W=0;W<ge-1;W++)u[W]<le&&(le=u[W]),m[W]>ve&&(ve=m[W]),f[W]<me&&(me=f[W]),g[W]>ye&&(ye=g[W]),d[W]<pe&&(pe=d[W]),h[W]>Ee&&(Ee=h[W]),xe+=p[W],x[W]=cr(le,me,pe,ve,ye,Ee),b[W]=xe;let Pe=1/0,Ae=1/0,ce=1/0,fe=-1/0,Me=-1/0,ae=-1/0,de=0;for(let W=ge-2;W>=0;W--){let J=W+1;u[J]<Pe&&(Pe=u[J]),m[J]>fe&&(fe=m[J]),f[J]<Ae&&(Ae=f[J]),g[J]>Me&&(Me=g[J]),d[J]<ce&&(ce=d[J]),h[J]>ae&&(ae=h[J]),de+=p[J],P[W]=cr(Pe,Ae,ce,fe,Me,ae),y[W]=de}for(let W=0;W<ge-1;W++){if(b[W]===0||y[W]===0)continue;let J=.125+(x[W]*b[W]+P[W]*y[W])*I;J<U&&(U=J,G=K,q=te+(W+1)/Se)}}let v=G===0?o:G===1?s:i,O=S,L=T-1;for(;O<=L;)if(v[O]<q)O++;else{let K=r[O];r[O]=r[L],r[L]=K;let te=o[O];o[O]=o[L],o[L]=te;let se=s[O];s[O]=s[L],s[L]=se;let he=i[O];i[O]=i[L],i[L]=he,L--}let X=O;(X===S||X===T)&&(X=S+T>>1),c[A+6]=0;let H=M(S,X),Y=M(X,T);return c[E*wn+7]=H,c[E*wn+8]=Y,E}return t>0&&M(0,t),{data:c,nodeCount:l,polyIndices:r,meta:e}}function Ss(e,n,t,r,o,s,i,a,c){if(a.nodeCount===0)return!1;let{data:l,polyIndices:u,meta:f}=a,d=r!==0?1/r:r>=0?1/0:-1/0,m=o!==0?1/o:o>=0?1/0:-1/0,g=s!==0?1/s:s>=0?1/0:-1/0,h=0;for(c[h++]=0;h>0;){let x=c[--h]*wn,b=(l[x]-e)*d,P=(l[x+3]-e)*d,y=b<P?b:P,M=b<P?P:b,S=(l[x+1]-n)*m,T=(l[x+4]-n)*m,E=S<T?S:T,A=S<T?T:S;if(y>A||E>M)continue;E>y&&(y=E),A<M&&(M=A);let C=(l[x+2]-t)*g,_=(l[x+5]-t)*g,w=C<_?C:_,V=C<_?_:C;if(!(y>V||w>M)&&(V<M&&(M=V),!(M<.001)))if(l[x+6]===1){let N=l[x+7]|0,F=l[x+8]|0;for(let k=N;k<F;k++){let $=u[k];if($===i)continue;let R=f[$];if(R&&Vl(e,n,t,r,o,s,R))return!0}}else c[h++]=l[x+7]|0,c[h++]=l[x+8]|0}return!1}function Ll(e){let n=(1+Math.sqrt(5))/2,t=new Float64Array(e*3);for(let r=0;r<e;r++){let o=(r+.5)/e,s=Math.sqrt(Math.max(0,1-o*o)),i=2*Math.PI*(r/n);t[r*3]=s*Math.cos(i),t[r*3+1]=s*Math.sin(i),t[r*3+2]=o}return t}function wl(e){let n=Math.abs(e[0])>.9?0:1,t=Math.abs(e[0])>.9?1:0,r=t*e[2],o=-n*e[2],s=n*e[1]-t*e[0],i=Math.hypot(r,o,s);r/=i,o/=i,s/=i;let a=e[1]*s-e[2]*o,c=e[2]*r-e[0]*s,l=e[0]*o-e[1]*r;return{ux:r,uy:o,uz:s,vx:a,vy:c,vz:l}}function $e(e,n){let t=n?.samples??8;if(e.length<4||t<1||_l(e))return e;let r=e.map(vl),o=Ll(t),s=[],i=Rl(r),a=new Int32Array(Math.max(64,i.nodeCount)),c=192,l=new Float64Array(c);for(let u=0;u<e.length;u++){let f=r[u];if(!f){s.push(e[u]);continue}let d=f.normal[0],m=f.normal[1],g=f.normal[2],h=.001*d,p=.001*m,x=.001*g;{let F=f.centroid[0]+h,k=f.centroid[1]+p,$=f.centroid[2]+x;if(!Ss(F,k,$,d,m,g,u,i,a)){s.push(e[u]);continue}}let{ux:b,uy:P,uz:y,vx:M,vy:S,vz:T}=wl(f.normal),E=f.centroid[0],A=f.centroid[1],C=f.centroid[2],_=f.vertices,w=_.length,V=0;l[V++]=E+h,l[V++]=A+p,l[V++]=C+x;for(let F=0;F<w;F++){let k=_[F];l[V++]=k[0]+(E-k[0])*.08+h,l[V++]=k[1]+(A-k[1])*.08+p,l[V++]=k[2]+(C-k[2])*.08+x}for(let F=0;F<w;F++){let k=_[F],$=_[(F+1)%w],R=(k[0]+$[0])*.5,B=(k[1]+$[1])*.5,z=(k[2]+$[2])*.5;l[V++]=R+(E-R)*.08+h,l[V++]=B+(A-B)*.08+p,l[V++]=z+(C-z)*.08+x}let N=!1;e:for(let F=0;F<o.length;F+=3){let k=o[F],$=o[F+1],R=o[F+2],B=k*b+$*M+R*d,z=k*P+$*S+R*m,ee=k*y+$*T+R*g;for(let Q=0;Q<V;Q+=3){let Z=l[Q],ne=l[Q+1],j=l[Q+2];if(!Ss(Z,ne,j,B,z,ee,u,i,a)){N=!0;break e}}}N&&s.push(e[u])}return s}var Vs={overlapPx:1.25,maxGapPx:14,capacityScale:1},Os={budget:40};function Je(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function kn(e){return typeof e=="number"?{overlapPx:Math.max(0,e)*.175,maxGapPx:14,capacityScale:1}:{overlapPx:Math.max(0,Je(e?.overlapPx,1.25)),maxGapPx:Math.max(0,Je(e?.maxGapPx,14)),capacityScale:Math.max(0,Je(e?.capacityScale,1))}}function Un(e,n){return!(typeof n=="number"&&n<=0)&&e.maxGapPx>1e-6&&e.capacityScale>1e-6}function Rs(e){return{rotX:Je(e?.rotX,65),rotY:Je(e?.rotY,45),viewAware:e?.viewAware!==!1,passes:Math.max(1,Math.min(3,Math.round(Je(e?.passes,2)))),budget:Math.max(0,Math.min(256,Math.round(Je(e?.budget,40))))}}function we(e){return e.map(n=>[n[1]*50,n[0]*50,n[2]*50])}function Ls(e){return[e[1]/50,e[0]/50,e[2]/50]}function bt(e){return`${e[0]},${e[1]},${e[2]}`}function Nl(e,n){let t=bt(e),r=bt(n);return t<r?`${t}|${r}`:`${r}|${t}`}function Ne(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function mn(e){return Math.hypot(e[0],e[1],e[2])}function Bn(e){let n=mn(e);return n>1e-6?[e[0]/n,e[1]/n,e[2]/n]:null}function Fn(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function dt(e,n,t,r){let o=n*Math.PI/180,s=t*Math.PI/180,i=r*Math.PI/180,[a,c,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[a,c]=[a*u-c*f,a*f+c*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[a,l]=[a*u+l*f,-a*f+l*u]}if(o!==0){let u=Math.cos(o),f=Math.sin(o);[c,l]=[c*u-l*f,c*f+l*u]}return[a,c,l]}function dr(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function Dl(e){if(e.length<3)return null;let n=e[0],t=[0,0,0];for(let r=1;r+1<e.length;r+=1){let o=Ne(e[r],n),s=Ne(e[r+1],n),i=dr(o,s);t[0]-=i[0],t[1]-=i[1],t[2]-=i[2]}return Bn(t)}function gr(e){let n=e[0],t=Dl(e);if(!t)return null;let r=null;for(let u=0;u<e.length;u+=1){let f=Ne(e[(u+1)%e.length],e[u]);if(mn(f)>1e-6){r=f;break}}if(!r)return null;let o=Fn(r,t),s=[r[0]-o*t[0],r[1]-o*t[1],r[2]-o*t[2]],i=Bn(s);if(!i)return null;let a=Bn(dr(t,i));if(!a)return null;let c=e.map(u=>{let f=Ne(u,n);return[Fn(f,i),Fn(f,a)]}),l=yt(c);return Math.abs(l)>1e-6?{origin:n,normal:t,xAxis:i,yAxis:a,local:c,area:l}:null}function yt(e){let n=0;for(let t=0;t<e.length;t+=1){let r=e[t],o=e[(t+1)%e.length];n+=r[0]*o[1]-r[1]*o[0]}return n/2}function pr(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t+=1){let r=e[t],o=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(o[0]-r[0])*(s[1]-o[1])-(o[1]-r[1])*(s[0]-o[0]);if(Math.abs(i)<=1e-6)continue;let a=Math.sign(i);if(n===0)n=a;else if(a!==n)return!1}return!0}function ws(e,n,t){let r=e[t],o=e[(t+1)%e.length],s=o[0]-r[0],i=o[1]-r[1],a=Math.hypot(s,i);if(a<=1e-6)return null;let c=n>0?1:-1;return[c*(i/a),c*(-s/a)]}function Fl(e,n){let t=ws(e.local,e.area,n);return t?Bn([e.xAxis[0]*t[0]+e.yAxis[0]*t[1],e.xAxis[1]*t[0]+e.yAxis[1]*t[1],e.xAxis[2]*t[0]+e.yAxis[2]*t[1]]):null}function Ns(e,n){let t=e.length,r=e[n],o=e[(n+1)%t],s=o[0]-r[0],i=o[1]-r[1],a=Math.hypot(s,i);if(a<=1e-6)return 0;let c=[a*.24],l=1/0;for(let f=0;f<t;f+=1){if(f===n||f===(n+1)%t)continue;let d=e[f],m=Math.abs((d[0]-r[0])*i-(d[1]-r[1])*s)/a;m>1e-6&&(l=Math.min(l,m))}Number.isFinite(l)&&c.push(l*.32);let u=[{origin:r,point:e[(n+t-1)%t]},{origin:o,point:e[(n+2)%t]}];for(let f of u){let d=f.point[0]-f.origin[0],m=f.point[1]-f.origin[1],g=Math.hypot(d,m);if(g<=1e-6)continue;let h=Math.abs(s*m-i*d)/(a*g);h>1e-6&&c.push(g*h*.32)}return Math.min(...c)}function Ds(e){return e.map(n=>{let t=we(n.vertices),r=gr(t),o=!hr(n)&&!!r&&pr(r.local),s=o?r.local.map((i,a)=>Ns(r.local,a)):[];return{basis:r,cssPoints:t,capacities:s,patchable:o}})}function hr(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function Bl(e){return e.material?.key??e.color??""}function Fs(e,n,t){let r=[];for(let o=0;o<n.length;o+=1){let s=n[o];if(!(!s.patchable||!s.basis))for(let i=0;i<s.cssPoints.length;i+=1){let a=s.cssPoints[i],c=s.cssPoints[(i+1)%s.cssPoints.length],l=Ne(c,a),u=mn(l),f=Bn(l),d=Fl(s.basis,i),m=(s.capacities[i]??0)*t;!f||!d||m<=1e-6||u<=1e-6||r.push({index:r.length,polygon:o,edge:i,key:Nl(a,c),a,b:c,minX:Math.min(a[0],c[0]),minY:Math.min(a[1],c[1]),minZ:Math.min(a[2],c[2]),maxX:Math.max(a[0],c[0]),maxY:Math.max(a[1],c[1]),maxZ:Math.max(a[2],c[2]),length:u,dir:f,normal:s.basis.normal,outward:d,capacity:m,materialKey:Bl(e[o]),color:e[o].color})}}return r}function xr(e,n){return e.materialKey===n.materialKey&&e.color===n.color}function gt(e,n){return mn(Ne(e,n))<=1e-4}function kl(e,n){return gt(e.a,n.a)||gt(e.a,n.b)||gt(e.b,n.a)||gt(e.b,n.b)}function Ul(e,n){return xr(e,n)?kl(e,n)?"connected-facet":"true-gap":"material-boundary"}function lr(e,n,t,r,o,s){return{kind:e,aPolygon:n.polygon,aEdge:n.edge,bPolygon:t.polygon,bEdge:t.edge,aColor:n.color,bColor:t.color,aMaterialKey:n.materialKey,bMaterialKey:t.materialKey,gapPx:r.gap,spanPx:r.aEnd-r.aStart,aStartPx:r.aStart,aEndPx:r.aEnd,bStartPx:r.bStart,bEndPx:r.bEnd,targetClosurePx:o,appliedClosurePx:s,residualGapPx:Math.max(0,r.gap-s),residualTargetPx:Math.max(0,o-s)}}function Es(e,n,t,r,o){let s=Math.max(0,Math.min(n.length,Math.min(t,r))),i=Math.max(0,Math.min(n.length,Math.max(t,r)));if(o<=1e-6||i-s<=1e-6)return;let a=e[n.polygon].get(n.edge),c={start:s,end:i,amount:o};a?a.push(c):e[n.polygon].set(n.edge,[c])}function br(){return{exactPairs:0,nearPairs:0,patchedPolygons:0,patchedEdges:0,maxMeasuredGapPx:0,maxAppliedAmountPx:0,unclosedPairs:0,maxResidualGapPx:0}}function Gl(e,n){let t=0,r=0,o=0;for(let s of e){s.size>0&&(o+=1),r+=s.size;for(let i of s.values())for(let a of i)t=Math.max(t,a.amount)}return{...n,patchedPolygons:o,patchedEdges:r,maxAppliedAmountPx:t}}function yr(e,n,t=!1){let r=Ds(e),o=Fs(e,r,n.capacityScale),s=e.map(()=>new Map),i=br(),a=t?[]:void 0;if(o.length===0)return{edgeRepairs:s,diagnostics:i,candidates:a};let c=new Map;for(let l of o){let u=c.get(l.key);u?u.push(l):c.set(l.key,[l])}for(let l of c.values())l.length<2||(i.exactPairs+=l.length===2?1:l.length);return $l(o,c,s,i,n,a),{edgeRepairs:s,diagnostics:Gl(s,i),candidates:a}}function $l(e,n,t,r,o,s){let i=o.maxGapPx,a=new Set;for(let[m,g]of n)g.length>1&&a.add(m);let c=(m,g)=>{let h=m.index<g.index?m:g,p=m.index<g.index?g:m;if(p.polygon===h.polygon||h.key===p.key&&a.has(h.key)||!s&&!xr(h,p)||!zl(h,p,i)||Math.abs(Fn(h.dir,p.dir))<.985)return;let x=Yl(h,p,i);if(!x)return;let b=Ul(h,p),P=x.gap+o.overlapPx;if(b==="material-boundary"){s?.push(lr(b,h,p,x,P,0));return}let y=P;if(y<=.25){s?.push(lr(b,h,p,x,P,0));return}let M=h.capacity*x.facingA,S=p.capacity*x.facingB,T=Math.min(M,y/2),E=Math.min(S,y/2);if(y-=T+E,y>1e-6){let A=Math.min(M-T,y);T+=A,y-=A}if(y>1e-6){let A=Math.min(S-E,y);E+=A,y-=A}s?.push(lr(b,h,p,x,P,T+E)),T>1e-6&&Es(t,h,x.aStart,x.aEnd,T/x.facingA),E>1e-6&&Es(t,p,x.bStart,x.bEnd,E/x.facingB),r.nearPairs+=1,r.maxMeasuredGapPx=Math.max(r.maxMeasuredGapPx,x.gap),y>.25&&(r.unclosedPairs+=1,r.maxResidualGapPx=Math.max(r.maxResidualGapPx,y))},l=Math.max(28,i*2);if(e.length<=1e4){let m=[...e].sort((p,x)=>p.minX-x.minX),g=new Int32Array(m.length),h=0;for(let p=0;p+1<m.length;p+=1){let b=m[p].maxX+i,P=p+1;for(;P<m.length&&m[P].minX<=b;)P+=1;if(g[p]=P,h+=P-p-1,h>25e6)break}if(h<=2e6||h<=25e6&&jl(e,l,i)){for(let p=0;p+1<m.length;p+=1)for(let x=p+1;x<g[p];x+=1)c(m[p],m[x]);return}}let u=new Map;for(let m of e)Xl(u,m,l,i);let f=new Set,d=e.length;for(let m of u.values())for(let g=0;g+1<m.length;g+=1)for(let h=g+1;h<m.length;h+=1){let p=m[g],x=m[h],b=p.index<x.index?p:x,P=p.index<x.index?x:p,y=b.index*d+P.index;f.has(y)||(f.add(y),c(b,P))}}function zl(e,n,t){return e.minX<=n.maxX+t&&n.minX<=e.maxX+t&&e.minY<=n.maxY+t&&n.minY<=e.maxY+t&&e.minZ<=n.maxZ+t&&n.minZ<=e.maxZ+t}function Ts(e,n){return[Math.floor(e[0]/n),Math.floor(e[1]/n),Math.floor(e[2]/n)]}function Xl(e,n,t,r){let[o,s,i,a,c,l]=Bs(n,t,r);for(let u=o;u<=a;u+=1)for(let f=s;f<=c;f+=1)for(let d=i;d<=l;d+=1){let m=`${u},${f},${d}`,g=e.get(m);g?g.push(n):e.set(m,[n])}}function Bs(e,n,t){let[r,o,s]=Ts([e.minX-t,e.minY-t,e.minZ-t],n),[i,a,c]=Ts([e.maxX+t,e.maxY+t,e.maxZ+t],n);return[r,o,s,i,a,c]}function Wl(e,n,t){let[r,o,s,i,a,c]=Bs(e,n,t);return(i-r+1)*(a-o+1)*(c-s+1)}function jl(e,n,t){let r=0,o=0;for(let s of e){let i=Wl(s,n,t);if(r+=i,i>o&&(o=i),o>8192)return!0}return r/Math.max(1,e.length)>256}function Cs(e,n,t){return(e[0]-n[0])*t[0]+(e[1]-n[1])*t[1]+(e[2]-n[2])*t[2]}function _s(e,n,t){return(e.a[0]+e.dir[0]*n-t.a[0])*t.dir[0]+(e.a[1]+e.dir[1]*n-t.a[1])*t.dir[1]+(e.a[2]+e.dir[2]*n-t.a[2])*t.dir[2]}function Yl(e,n,t){let r=Cs(n.a,e.a,e.dir),o=Cs(n.b,e.a,e.dir),s=Math.max(0,Math.min(r,o)),i=Math.min(e.length,Math.max(r,o)),a=i-s,c=Math.min(e.length,n.length);if(a<Math.max(.75,c*.12))return null;let l=(s+i)/2,u=e.a[0]+e.dir[0]*l,f=e.a[1]+e.dir[1]*l,d=e.a[2]+e.dir[2]*l,m=Math.max(0,Math.min(n.length,(u-n.a[0])*n.dir[0]+(f-n.a[1])*n.dir[1]+(d-n.a[2])*n.dir[2])),g=n.a[0]+n.dir[0]*m-u,h=n.a[1]+n.dir[1]*m-f,p=n.a[2]+n.dir[2]*m-d,x=Math.hypot(g,h,p);if(x<=1e-6||x>t||x>Math.min(t,Math.max(4,c*.28)))return null;let b=1/x,P=g*b,y=h*b,M=p*b,S=e.outward[0]*P+e.outward[1]*y+e.outward[2]*M,T=-(n.outward[0]*P+n.outward[1]*y+n.outward[2]*M);if(S<.5||T<.5)return null;let E=Math.max(0,Math.min(n.length,_s(e,s,n))),A=Math.max(0,Math.min(n.length,_s(e,i,n)));return{gap:x,facingA:S,facingB:T,aStart:s,aEnd:i,bStart:Math.min(E,A),bEnd:Math.max(E,A)}}function ur(e,n,t,r){let o=t<=1e-6?0:Math.max(0,Math.min(1,r/t));return[e[0]+(n[0]-e[0])*o,e[1]+(n[1]-e[1])*o]}function Nn(e,n){let t=e[e.length-1];t&&Math.hypot(t[0]-n[0],t[1]-n[1])<=1e-6||e.push(n)}function Hl(e,n,t){if(!e?.length||n<=1e-6||t<=1e-6)return[];let r=new Set([0,n]);for(let i of e){let a=Math.max(0,Math.min(n,i.start)),c=Math.max(0,Math.min(n,i.end));c-a<=1e-6||i.amount<=1e-6||(r.add(a),r.add(c))}let o=Array.from(r).sort((i,a)=>i-a),s=[];for(let i=0;i+1<o.length;i+=1){let a=o[i],c=o[i+1];if(c-a<=1e-6)continue;let l=(a+c)/2,u=0;for(let d of e)l+1e-6<d.start||l-1e-6>d.end||(u=Math.max(u,d.amount));let f=Math.min(t,u);f>1e-6&&s.push({start:a,end:c,amount:f})}return s}function Kl(e,n){if(n.length<3||!pr(n))return!1;let t=yt(e),r=yt(n);return Math.sign(r)===Math.sign(t)&&Math.abs(r)>=Math.abs(t)-1e-6}function ql(e,n,t){if(n.size===0||e.vertices.length<3||hr(e))return e;let r=we(e.vertices),o=gr(r);if(!o||!pr(o.local))return e;let s=[];for(let l=0;l<o.local.length;l+=1){let u=o.local[l],f=o.local[(l+1)%o.local.length],d=Math.hypot(f[0]-u[0],f[1]-u[1]),m=ws(o.local,o.area,l);if(!m||d<=1e-6)return e;let g=Hl(n.get(l),d,Ns(o.local,l)*t);Nn(s,u);let h=0;for(let p of g){p.start>h+1e-6&&Nn(s,ur(u,f,d,p.start));let x=ur(u,f,d,p.start),b=ur(u,f,d,p.end);Nn(s,[x[0]+m[0]*p.amount,x[1]+m[1]*p.amount]),Nn(s,[b[0]+m[0]*p.amount,b[1]+m[1]*p.amount]),p.end<d-1e-6&&Nn(s,b),h=p.end}}let i=s[0],a=s[s.length-1];if(i&&a&&Math.hypot(i[0]-a[0],i[1]-a[1])<=1e-6&&s.pop(),!Kl(o.local,s))return e;let c=s.map(([l,u])=>Ls([o.origin[0]+l*o.xAxis[0]+u*o.yAxis[0],o.origin[1]+l*o.xAxis[1]+u*o.yAxis[1],o.origin[2]+l*o.xAxis[2]+u*o.yAxis[2]]));return{...e,vertices:c}}function ks(e){return e.vertices.length===4&&!hr(e)}function Zl(e){let n=e.basis?.local;if(!n||n.length!==4)return 1;let t=1/0,r=1/0,o=-1/0,s=-1/0;for(let[c,l]of n)t=Math.min(t,c),r=Math.min(r,l),o=Math.max(o,c),s=Math.max(s,l);let i=Math.max(1e-6,(o-t)*(s-r));return .75+(1-Math.max(0,Math.min(1,Math.abs(e.basis.area)/i)))*2.5}function fr(e,n){return mn(Ne(we([e])[0],we([n])[0]))}function pt(e,n,t){let r=fr(e,n),o=fr(n,t),s=fr(t,e),i=Math.max(r,o,s);return i<=1e-6?0:mn(dr(Ne(we([n])[0],we([e])[0]),Ne(we([t])[0],we([e])[0])))*.5/(i*i)}function Ql(e,n,t,r){let o=n[0]-e[0],s=n[1]-e[1],i=r[0]-t[0],a=r[1]-t[1],c=o*a-s*i;if(Math.abs(c)<=1e-6)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*a-u*i)/c;return[e[0]+f*o,e[1]+f*s]}function Jl(e,n){if(e.length<3||e.length!==n.length)return null;let t=Math.max(0,...n);if(t<=1e-6)return e;let r=yt(e);if(Math.abs(r)<=1e-6)return null;let o=r>0?1:-1,s=[];for(let c=0;c<e.length;c+=1){let l=e[c],u=e[(c+1)%e.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=1e-6)return null;let g=Math.max(0,n[c]),h=o*(d/m)*g,p=o*(-f/m)*g;s.push({a:[l[0]+h,l[1]+p],b:[u[0]+h,u[1]+p]})}let i=[],a=Math.max(2,t*4);for(let c=0;c<e.length;c+=1){let l=s[(c+e.length-1)%e.length],u=s[c],f=Ql(l.a,l.b,u.a,u.b);if(!f)return null;let d=e[c],m=f[0]-d[0],g=f[1]-d[1],h=Math.hypot(m,g);i.push(h>a?[d[0]+m/h*a,d[1]+g/h*a]:f)}return i}function eu(e,n){return[e.origin[0]+e.xAxis[0]*n[0]+e.yAxis[0]*n[1],e.origin[1]+e.xAxis[1]*n[0]+e.yAxis[1]*n[1],e.origin[2]+e.xAxis[2]*n[0]+e.yAxis[2]*n[1]]}function ht(e,n){let t=gr(we(e));if(!t)return e;let r=Jl(t.local,n);return r?r.map(o=>Ls(eu(t,o))):e}function nu(e,n){let t={...e,uvs:void 0,textureTriangles:void 0},[r,o,s,i]=e.vertices,a=Math.min(pt(r,o,s),pt(r,s,i)),c=Math.min(pt(r,o,i),pt(o,s,i)),l=u=>n.has(u)?.75:0;return a>=c?[{...t,vertices:ht([r,o,s],[l(0),l(1),1.25])},{...t,vertices:ht([r,s,i],[1.25,l(2),l(3)])}]:[{...t,vertices:ht([r,o,i],[l(0),1.25,l(3)])},{...t,vertices:ht([o,s,i],[l(1),l(2),1.25])}]}function vs(e){if(e.length===0)return{q1:1/0,median:1/0,q3:1/0,p70:1/0,p80:1/0,p88:1/0,p95:1/0,p97:1/0,max:1/0};let n=[...e].sort((r,o)=>r-o),t=r=>n[Math.min(n.length-1,Math.floor((n.length-1)*r))]??0;return{q1:t(.25),median:t(.5),q3:t(.75),p70:t(.7),p80:t(.8),p88:t(.88),p95:t(.95),p97:t(.97),max:n[n.length-1]??0}}function xt(e,n){let t=e[n];if(t===n)return n;let r=xt(e,t);return e[n]=r,r}function tu(e){let n=new Set,t=new Set;for(let r of e)r.sel&&t.add(r.comp);for(let r of e)if(t.has(r.comp))for(let o of r.e)n.add(o);return{endpoints:n}}function ru(e,n){return n?e.e.some(t=>n.endpoints.has(t)):!0}function ou(e,n,t,r){let o=[];for(let[c,l]of t){if(l.length!==2)continue;let[u,f]=l;if(!xr(u,f))continue;let d=l.filter(S=>ks(e[S.polygon]));if(d.length===0)continue;let m=d[0],g=1-Math.min(1,Math.abs(Fn(u.normal,f.normal))),h=Math.max(u.length,f.length);if(g<.01&&h<64)continue;let p=Math.max(...d.map(S=>Zl(n[S.polygon]))),x=d.length===2?1.18:1,b=Math.max(.08,g),P=r.viewAware?su(u,f,r.rotX,r.rotY):{proj:h,vr:1},y=r.viewAware?Math.max(P.proj,h*.12):h,M=r.viewAware?.18+P.vr*1.82:1;o.push({key:c,e:[bt(m.a),bt(m.b)],o:l,r:d,len:h,rank:y,proj:P.proj,s:y*(.58+b*2.8)*p*x*M,nr:g,sr:p,vr:P.vr,comp:-1,sel:!1,why:"below-threshold",cost:d.length})}let s=Array.from({length:o.length},(c,l)=>l),i=new Map;for(let c=0;c<o.length;c+=1)for(let l of o[c].e){let u=i.get(l);if(u===void 0)i.set(l,c);else{let f=xt(s,u),d=xt(s,c);f!==d&&(s[d]=f)}}let a=new Map;for(let c=0;c<o.length;c+=1){let l=xt(s,c),u=a.get(l);u===void 0&&(u=a.size,a.set(l,u)),o[c].comp=u}return o}function su(e,n,t,r){let o=dt(e.a,t,0,r),s=dt(e.b,t,0,r),i=Math.hypot(s[0]-o[0],s[1]-o[1]),a=dt(e.normal,t,0,r)[2],c=dt(n.normal,t,0,r)[2],l=Math.max(0,a,c),u=Math.min(1,Math.abs(a-c));return{proj:i,vr:Math.max(.04,Math.min(1,l*.85+u*.45))}}function iu(e){let n=new Map;for(let t of e){let r=n.get(t.comp);(!r||t.s>r.s||t.s===r.s&&t.len>r.len)&&n.set(t.comp,t)}return new Set(n.values())}function Dn(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+n*n*n*3.65,r=e.r.length===1?1.08:1;return e.rank*t*r/Math.sqrt(Math.max(1,e.r.length))}function Is(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+Math.min(1,e.vr)*1.65,r=.7+Math.min(2.6,e.sr)*.3,o=e.r.length===1?1.1:1,s=e.rank*(.6+n*n*1.4)*t*r*o/Math.sqrt(Math.max(1,e.r.length));return Math.max(s,Dn(e)*.75)}function mr(e){let n=new Set;for(let t of e)for(let r of t.r)n.add(r.polygon);return n.size}function Us(e,n){return e.nr<=.25&&e.rank>=n.median*1.08&&e.proj>=24}function au(e,n,t){return e.s>=n||e.rank>=t}function cu(e,n,t,r,o,s,i){let a=Math.max(0,Math.floor(t));if(a<=0||e.length===0||n==="follow-up")return{budgetLimit:a,componentBudgets:null};let c=new Map;for(let m of e){let g=c.get(m.comp);g?g.push(m):c.set(m.comp,[m])}let l=new Map,u=Math.max(1e-6,r.q3-r.q1),f=Math.min(r.max,Math.max(r.p95,r.q3+u)),d=0;for(let[m,g]of c){let h=g.filter(M=>au(M,s,i)||M.s>=f);if(h.length===0)continue;let p=g.filter(M=>Us(M,o)),x=mr(h),b=mr(p),P=Math.ceil(Math.sqrt(Math.max(0,b-x))*3),y=Math.min(mr(g),x+P);y<=0||(l.set(m,y),d+=y)}return l.size===0?{budgetLimit:0,componentBudgets:l}:{budgetLimit:Math.min(a,d),componentBudgets:l}}function lu(e,n,t){let r=new Map;if(e.length===0||t<=0)return{selected:r,budgetLimit:0};let o=vs(e.map(A=>A.s)),s=vs(e.map(A=>A.rank)),i=Math.max(1e-6,o.q3-o.q1),a=Math.min(o.max,Math.max(o.p95,o.q3+i*.85)),c=Math.min(o.max,Math.max(o.p80,o.q3+i*.15)),l=Math.min(o.max,Math.max(o.p70,o.q3)),u=Math.max(s.p97,s.median*1.8),f=Math.max(s.p88,s.median*1.35),d=cu(e,n,t,o,s,a,u),m=d.budgetLimit;if(m<=0)return{selected:r,budgetLimit:0};let g=iu(e),h=new Set,p=new Set,x=new Set,b=new Map,P=n==="primary"?Math.min(Math.max(0,d.budgetLimit-1),Math.max(2,Math.floor(d.budgetLimit*.28))):0,y=[...e].sort((A,C)=>Dn(C)-Dn(A)||C.s/Math.max(1,C.r.length)-A.s/Math.max(1,A.r.length)||C.len-A.len),M=[...e].sort((A,C)=>Is(C)-Is(A)||Dn(C)-Dn(A)||C.len-A.len),S=(A,C)=>{A.sel=!0,A.why=C,x.add(A.comp);for(let _ of A.e)p.add(_);for(let _ of A.r){h.add(_.polygon);let w=r.get(_.polygon);w?w.add(_.edge):r.set(_.polygon,new Set([_.edge]))}},T=(A,C)=>{let _=d.componentBudgets;if(C<=0||!_)return!0;let w=_.get(A.comp)??0;return(b.get(A.comp)??0)+C<=w},E=(A,C)=>{C<=0||!d.componentBudgets||b.set(A.comp,(b.get(A.comp)??0)+C)};for(let A of M){let C=A.r.reduce((w,V)=>w+(h.has(V.polygon)?0:1),0);if(A.cost=C,!(C>m||!T(A,C)||C>0&&m-C<P||!Us(A,s))&&(S(A,"global-outlier"),E(A,C),m-=C,m<=0))return{selected:r,budgetLimit:d.budgetLimit}}for(let A of y){if(A.sel)continue;let C=A.r.reduce((F,k)=>F+(h.has(k.polygon)?0:1),0);if(A.cost=C,C>m||!T(A,C)||C>0&&m-C<P)continue;let _=A.r.length===2,w=g.has(A)&&(n==="primary"||n==="follow-up"&&A.s>=c*1.08&&A.rank>=f)&&(_?A.s>=c||A.rank>=f:A.s>=c*1.18||A.rank>=f*1.12),V=_?A.s>=a||A.rank>=u:A.s>=a*1.35||A.rank>=u*1.2,N=C===0&&(_?A.s>=l||A.rank>=f:A.s>=l*1.25||A.rank>=f*1.12);if(!(!w&&!V&&!N)&&(S(A,w?"component-anchor":V?"global-outlier":"shared-polygon"),E(A,C),m-=C,m<=0))return{selected:r,budgetLimit:d.budgetLimit}}for(let A of y){if(A.sel)continue;let C=A.r.reduce((k,$)=>k+(h.has($.polygon)?0:1),0);if(A.cost=C,C>1||C>m||!T(A,C))continue;let _=A.r.some(k=>h.has(k.polygon)),w=A.e.some(k=>p.has(k));if(n==="primary"&&A.r.length===1&&C===1&&(x.has(A.comp)||w||_)&&A.rank>=f*1.05&&A.nr>=.16&&A.vr<=.32){if(S(A,"local-follow-up"),E(A,C),m-=C,m<=0)return{selected:r,budgetLimit:d.budgetLimit};continue}if(A.r.length!==2)continue;let N=_&&C===1&&A.proj>=f*1.45&&A.vr<=.18;if(!w&&!N)continue;if((A.s>=l*.42||A.rank>=f*.62)&&A.s<=l*1.1&&A.proj>=24&&(S(A,"local-follow-up"),E(A,C),m-=C,m<=0))return{selected:r,budgetLimit:d.budgetLimit}}return{selected:r,budgetLimit:d.budgetLimit}}function Gs(e,n,t,r,o){let s=Ds(e),i=Fs(e,s,n.capacityScale),a=new Map;for(let u of i){let f=a.get(u.key);f?f.push(u):a.set(u.key,[u])}let c=ou(e,s,a,t).filter(u=>ru(u,o)),l=lu(c,o?"follow-up":"primary",r);return{selected:l.selected,candidates:c,budgetLimit:l.budgetLimit}}function uu(e,n){let t=[];for(let r=0;r<e.length;r+=1){let o=e[r],s=n.get(r);s&&ks(o)?t.push(...nu(o,s)):t.push(o)}return t}function fu(e){let[n,t]=e.o,r=n??t,o=t??n;return{key:e.key,aPolygon:r?.polygon??-1,aEdge:r?.edge??-1,bPolygon:o?.polygon??-1,bEdge:o?.edge??-1,color:r?.color??o?.color,materialKey:r?.materialKey??o?.materialKey??"",lengthPx:e.len,projectedLengthPx:e.proj,score:e.s,normalRisk:e.nr,shapeRisk:e.sr,viewRisk:e.vr,component:e.comp,marginalCost:e.cost,selected:e.sel,reason:e.why}}function $s(e,n,t){let r=kn(n),o=Rs(t);if(!Un(r,n)||e.length===0)return e;let s=e,i,a=o.budget;for(let c=0;c<o.passes;c+=1){let l=Gs(s,r,o,a,i);if(c===0&&(a=Math.min(a,l.budgetLimit)),l.selected.size===0)break;let u=uu(s,l.selected);if(u.length===s.length)break;if(a-=l.selected.size,a<=0){s=u;break}i=tu(l.candidates),s=u}return s}function mu(e,n,t){let r=kn(n),o=Rs(t);if(!Un(r,n)||e.length===0)return{candidates:[],selectedPolygons:0,selectedEdges:0,addedPolygons:0};let s=Gs(e,r,o,o.budget),i=0;for(let a of s.selected.values())i+=a.size;return{candidates:s.candidates.map(fu),selectedPolygons:s.selected.size,selectedEdges:i,addedPolygons:s.selected.size}}function zs(e,n){let t=kn(n);if(!Un(t,n)||e.length===0)return e;let{edgeRepairs:r}=yr(e,t);return r.some(o=>o.size>0)?e.map((o,s)=>ql(o,r[s],t.capacityScale)):e}function Pr(e,n=Vs,t=Os){if(e.length===0)return e;let r=$s(e,n,t);return zs(r,n)}function du(e,n){let t=kn(n);return!Un(t,n)||e.length===0?br():yr(e,t).diagnostics}function gu(e,n){let t=kn(n);if(!Un(t,n)||e.length===0)return{diagnostics:br(),candidates:[]};let r=yr(e,t,!0);return{diagnostics:r.diagnostics,candidates:r.candidates??[]}}var pu=3,hu=.03,xu=.02,Gn={maxAngleDeg:pu,maxPlaneDisplacement:hu,maxBoundaryDisplacement:xu,isolatedPairs:!1},bu={maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.04,isolatedPairs:!0},yu=1800,Pu=.65,Au=24,Mu=50,Su=1e3,Eu=1e3,$n={minGroupPolygons:2,maxCandidateAxes:24},Tu={...$n,maxCandidateAxes:1};function Er(e,n={}){let t=n.meshResolution??"lossy",r=f=>t==="lossy"?Pr(f):f,o={},s=Xs(e,!1,o),i=s,a=At(s),c=(f,d=At(f))=>d>=a?!1:(i=f,a=d,!0),l=t==="lossy"&&n.rectCover===void 0?_u(s):n.rectCover,u=Ar(s,l);if(u!==s&&c(u),t==="lossy"&&n.rectCover===void 0){let f=Ar(s,void 0);f!==s&&c(f)}if(t==="lossy"&&(i.length<=1||a<=1+1e-9))return r(i);if(t==="lossy"){let f=Xs(e,bu,o);c(f),n.rectCover!==void 0&&n.rectCover!==!1&&c(Ar(f,n.rectCover))}return r(i)}function At(e){let n=0;for(let t of e){let r=t.vertices.length,o=r<=4?0:Math.min(4,r-4)*.12,s=t.texture||t.material?.texture||t.textureTriangles?.length?.15:0;n+=1+o+s}return n}function Ar(e,n){if(n===!1)return e;let t=Cu(e,n);if(!t)return e;let r=ar(e,t);return r.length<e.length?r:e}function Cu(e,n){if(n&&n!==!0)return n;let t=n===!0,r=e.length;return r>2200||!t&&r>=Au&&r<Mu||!t&&r>Su?null:!t&&r<=300?{...$n,maxCandidateAxes:1}:r>300?!t&&(Iu(e)>12||Vu(e)>Eu)?null:{...$n,maxCandidateAxes:Math.min($n.maxCandidateAxes??24,2)}:$n}function _u(e){return e.length>yu||e.length===0||vu(e)/e.length<Pu?!1:Tu}function vu(e){let n=0;for(let t of e)t.vertices.length===3&&(n+=1);return n}function Iu(e){let n=0;for(let t of e)n=Math.max(n,t.vertices.length);return n}function Vu(e){let n=new Map;for(let r of e)for(let o=0;o<r.vertices.length;o++){let s=r.vertices[o],i=r.vertices[(o+1)%r.vertices.length],a=Tt(s,i);n.set(a,(n.get(a)??0)+1)}let t=0;for(let r of n.values())r===1&&(t+=1);return t}function Tr(e,n){return n==null?e:n.map(t=>e[t]).filter(t=>!!t)}function js(e,n){if(n===e||n.length===e.length&&n.every((o,s)=>o===e[s]))return null;let t=new Set(n),r=[];for(let o=0;o<e.length;o++)t.has(e[o])&&r.push(o);return r.length===e.length?null:r}function Ou(e,n){if(n?.deduped)return n.deduped;let t=n?.dedupedIndices;if(t===void 0){let o=lt(e);if(o.size===0)t=null;else{t=[];for(let s=0;s<e.length;s++)o.has(s)||t.push(s)}n&&(n.dedupedIndices=t)}let r=Tr(e,t);return n&&(n.deduped=r),r}function Ru(e,n){if(n?.interior)return n.interior;let t=n?.interiorIndices;if(t===void 0){let o=$e(e);t=js(e,o),n&&(n.interiorIndices=t)}let r=Tr(e,t);return n&&(n.interior=r),r}function Xs(e,n,t){let r=Ou(e,t),o=Ru(r,t),s=t?.baseline??Re(o);if(t&&!t.baseline&&(t.baseline=s),!n)return s;let i=n===!0?Gn:wu(n);if(i.isolatedPairs){let c=Nu(Lu(r,t),i),l=Re(c);return l.length<s.length?l:s}let a=Re($e(Wu(r,i,t)));return a.length<s.length?a:s}function Ys(e,n){return n?(n.snapped||(n.snapped=Mt(e)),n.snapped):Mt(e)}function Lu(e,n){if(!n)return $e(Mt(e));if(!n.snappedInterior){let t=Ys(e,n);if(n.snappedInteriorIndices===void 0){let r=$e(t);n.snappedInteriorIndices=js(t,r),n.snappedInterior=r}else n.snappedInterior=Tr(t,n.snappedInteriorIndices)}return n.snappedInterior}function wu(e){return{maxAngleDeg:e.maxAngleDeg??Gn.maxAngleDeg,maxPlaneDisplacement:e.maxPlaneDisplacement??Gn.maxPlaneDisplacement,maxBoundaryDisplacement:e.maxBoundaryDisplacement??Gn.maxBoundaryDisplacement,isolatedPairs:e.isolatedPairs??Gn.isolatedPairs}}function Nu(e,n){let t=e.map(i=>{let a=_r(i);return a?{polygon:i,normal:a.normal,area:a.area,materialKey:Js(i)}:null}),r=new Map;for(let i=0;i<e.length;i++){let a=e[i];if(!(a.vertices.length!==3||!t[i]))for(let c=0;c<a.vertices.length;c++){let l=Tt(a.vertices[c],a.vertices[(c+1)%a.vertices.length]),u=r.get(l);u?u.push(i):r.set(l,[i])}}let o=[];for(let i of r.values()){if(i.length!==2)continue;let[a,c]=i,l=$u(a,c,e,t,n);l&&o.push(l)}let s=Fu(o);return s.length===0?e:Du(e,s)}function Du(e,n){let t=new Map,r=new Set,o=Hs(n.flatMap(i=>i.vertexMoves));for(let i of n){let a=Math.min(i.a,i.b);t.set(a,i.polygon),r.add(Math.max(i.a,i.b))}let s=[];for(let i=0;i<e.length;i++){let a=t.get(i);if(a){s.push(a);continue}r.has(i)||s.push(e[i])}return o.size>0?Ks(s,o):s}function Fu(e){return e.length>3e3?Bu(e):ku(e)}function Bu(e){let n=new Map;for(let s of e)n.set(s.a,(n.get(s.a)??0)+1),n.set(s.b,(n.get(s.b)??0)+1);let t=[...e].sort((s,i)=>{let a=(n.get(s.a)??0)+(n.get(s.b)??0),c=(n.get(i.a)??0)+(n.get(i.b)??0);return a-c||s.score-i.score}),r=new Set,o=[];for(let s of t)r.has(s.a)||r.has(s.b)||(r.add(s.a),r.add(s.b),o.push(s));return o}function ku(e){let n=new Map;for(let l=0;l<e.length;l++){let u=e[l],f=n.get(u.a);f?f.push(l):n.set(u.a,[l]);let d=n.get(u.b);d?d.push(l):n.set(u.b,[l])}let t=[],r=new Array(e.length).fill(!0),o=new Map,s=new Sr;for(let[l,u]of n)o.set(l,u.length);let i=l=>(o.get(l.a)??0)+(o.get(l.b)??0),a=l=>{let u=e[l];s.push({degree:i(u),score:u.score,index:l})},c=(l,u)=>{if(!r[l])return;r[l]=!1;let f=e[l];for(let d of[f.a,f.b])o.set(d,(o.get(d)??0)-1),u.add(d)};for(let l=0;l<e.length;l++)a(l);for(;s.size()>0;){let l=s.pop();if(!r[l.index])continue;let u=e[l.index];if(i(u)!==l.degree){a(l.index);continue}t.push(u);let d=new Set;for(let m of[u.a,u.b])for(let g of n.get(m)??[])c(g,d);for(let m of d)for(let g of n.get(m)??[])r[g]&&a(g)}return t}var Sr=class{constructor(){this.items=[]}size(){return this.items.length}push(n){this.items.push(n);let t=this.items.length-1;for(;t>0;){let r=t-1>>1;if(Mr(this.items[r],this.items[t])<=0)break;[this.items[r],this.items[t]]=[this.items[t],this.items[r]],t=r}}pop(){if(this.items.length===0)return null;let n=this.items[0],t=this.items.pop();if(this.items.length>0){this.items[0]=t;let r=0;for(;;){let o=r*2+1,s=o+1,i=r;if(o<this.items.length&&Mr(this.items[o],this.items[i])<0&&(i=o),s<this.items.length&&Mr(this.items[s],this.items[i])<0&&(i=s),i===r)break;[this.items[r],this.items[i]]=[this.items[i],this.items[r]],r=i}}return n}};function Mr(e,n){return e.degree-n.degree||e.score-n.score||e.index-n.index}function Hs(e){let n=new Map;for(let r of e){let o=n.get(r.key);o?(o.x+=r.target[0],o.y+=r.target[1],o.z+=r.target[2],o.count+=1):n.set(r.key,{x:r.target[0],y:r.target[1],z:r.target[2],count:1})}let t=new Map;for(let[r,o]of n)t.set(r,[o.x/o.count,o.y/o.count,o.z/o.count]);return t}function Uu(e,n){let t=[];for(let r=0;r<e.length;r++){let o=e[r].vertices,s=n[r]?.vertices;if(!s||s.length!==o.length)continue;for(let c=0;c<o.length;c++)t.push({key:Oe(o[c]),target:s[c]});let i=e[r].textureTriangles??[],a=n[r]?.textureTriangles??[];for(let c=0;c<i.length;c++){let l=a[c];if(l)for(let u=0;u<i[c].vertices.length;u++)t.push({key:Oe(i[c].vertices[u]),target:l.vertices[u]})}}return t}function Gu(e,n){let t=[];for(let r of e)for(let o of r.textureTriangles??[])for(let s of o.vertices)t.push({key:Oe(s),target:Or(s,n)});return t}function Ks(e,n){return e.map(t=>{let r=!1,o=a=>{let c=n.get(Oe(a));return c?(r=!0,c):a},s=t.vertices.map(o),i=Cr(t.textureTriangles,o);return r?{...t,vertices:s,...i?{textureTriangles:i}:{}}:t})}function $u(e,n,t,r,o){let s=t[e],i=t[n],a=r[e],c=r[n];if(!a||!c||s.vertices.length!==3||i.vertices.length!==3||!rf(s,i,a,c))return null;let l=Ir(s,i);if(!l)return null;let[u,f,d,m]=l;if((d+1)%i.vertices.length===m)return null;let h=Math.abs(ze(a.normal,c.normal)),p=Math.cos(o.maxAngleDeg*Math.PI/180);if(h<p)return null;let x=(f+1)%s.vertices.length,b=3-d-m,P=[s.vertices[f],s.vertices[x],s.vertices[u],i.vertices[b]],y=zu(P);if(!y)return null;let M=0,S=0;for(let C of P){let _=Math.abs(Vr(C,y));M=Math.max(M,_),S+=_*_}if(M>Math.min(o.maxPlaneDisplacement,o.maxBoundaryDisplacement))return null;let T=P.map(C=>Or(C,y));if(!Xu(T,y.normal))return null;let E=_r({vertices:T});if(!E||ze(E.normal,a.normal)<.2||ze(E.normal,c.normal)<.2)return null;let A={vertices:P,color:s.color,...s.doubleSided?{doubleSided:!0}:{},...s.data?{data:{...s.data}}:{}};if(vr(s,i)&&s.uvs&&i.uvs&&s.texture){A.texture=s.texture,s.textureWrap&&(A.textureWrap={...s.textureWrap}),s.textureAlphaMode&&(A.textureAlphaMode=s.textureAlphaMode),A.uvs=[[...s.uvs[f]],[...s.uvs[x]],[...s.uvs[u]],[...i.uvs[b]]];let C=ju([s,i]);C?.length&&(A.textureTriangles=C)}return{a:e,b:n,polygon:A,vertexMoves:[...P.map((C,_)=>({key:Oe(C),target:T[_]})),...Gu([s,i],y)],score:S/P.length+M*.25+(1-h)*.1}}function zu(e){if(e.length<3)return null;let n=0,t=0,r=0,o=0,s=0,i=0;for(let c=0;c<e.length;c++){let l=e[c],u=e[(c+1)%e.length];n+=(l[1]-u[1])*(l[2]+u[2]),t+=(l[2]-u[2])*(l[0]+u[0]),r+=(l[0]-u[0])*(l[1]+u[1]),o+=l[0],s+=l[1],i+=l[2]}let a=oi([n,t,r]);return a?{normal:a,point:[o/e.length,s/e.length,i/e.length]}:null}function Xu(e,n){let t=0;for(let r=0;r<e.length;r++){let o=e[r],s=e[(r+1)%e.length],i=e[(r+2)%e.length],a=ze(ri(zn(s,o),zn(i,s)),n);if(Math.abs(a)<=1e-9)continue;let c=a>0?1:-1;if(t===0)t=c;else if(t!==c)return!1}return!0}function Wu(e,n,t){let r=Ys(e,t),o=Qu(r,n);if(o<=0)return r;let s=r.map(m=>{let g=_r(m);return g?{polygon:m,normal:g.normal,area:g.area,materialKey:Js(m)}:null}),i=nf(r,s),a=new Set,c=Array(r.length),l=[],u=(m,g)=>{c[m]=g};for(let m=0;m<r.length;m++){let g=s[m];if(a.has(m))continue;if(!g){u(m,r[m]);continue}let h=of(m,s,i,a,o,n);for(let x of h)a.add(x);if(h.length<2){u(m,r[m]);continue}let p=Ku(h,r,s,i,o,n);l.push(...p.vertexMoves);for(let x of h)u(x,p.polygons.get(x)??r[x])}let f=c.flatMap(m=>m?[m]:[]),d=l.length>0?Ks(f,Hs(l)):f;return Mt(d)}function Mt(e){let n=Qs(e),t=1e-4;if(n<=0)return e;let r=Ju(n),o=ef(t);return e.map(s=>{let i=f=>r.snap(f),a=s.vertices.map(i),c=s.uvs&&s.uvs.length===s.vertices.length?s.uvs.map(f=>o.snap(f)):void 0,l=Cr(s.textureTriangles,i),u={...s,vertices:a,...c?{uvs:c}:{},...l?{textureTriangles:l}:{}};return{...u,...u.texture?{textureTriangles:qs(u)}:{}}})}function qs(e){if(e.texture){if(e.textureTriangles?.length)return Hu(e.textureTriangles);if(e.uvs&&e.uvs.length===e.vertices.length)return Yu(e.vertices,e.uvs)}}function ju(e){let n=e.flatMap(t=>qs(t)??[]);return n.length>0?n:void 0}function Yu(e,n){let t=[];for(let r=1;r<e.length-1;r++)t.push({vertices:[[...e[0]],[...e[r]],[...e[r+1]]],uvs:[[...n[0]],[...n[r]],[...n[r+1]]]});return t}function Hu(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function Cr(e,n){if(e?.length)return e.map(t=>({vertices:t.vertices.map(n),uvs:t.uvs.map(r=>[...r])}))}function Ku(e,n,t,r,o,s){let i=Zs(e,n,t,o,s);return i?Zu(i):qu(e,n,t,r,o,s)}function qu(e,n,t,r,o,s){let i=new Set(e),a=[];for(let f of e)for(let d of r.get(f)??[]){if(f>=d||!i.has(d))continue;let m=Zs([f,d],n,t,o,s);m&&a.push(m)}a.sort((f,d)=>d.score-f.score);let c=new Set,l=new Map,u=[];for(let f of a)if(!f.indices.some(d=>c.has(d))){u.push(...f.vertexMoves);for(let d=0;d<f.indices.length;d++){let m=f.indices[d];c.add(m),l.set(m,n[m])}}return{polygons:l,vertexMoves:u}}function Zu(e){let n=new Map;for(let t=0;t<e.indices.length;t++)n.set(e.indices[t],e.source[t]);return{polygons:n,vertexMoves:e.vertexMoves}}function Zs(e,n,t,r,o){let s=ei(e,t);if(!s||!ni(e,t,s,r,o))return null;let i=e.map(u=>n[u]),a=i.map(u=>cf(u,s)),c=At(Re(i)),l=At(Re(a));return l>=c?null:{indices:e,source:i,projected:a,vertexMoves:Uu(i,a),score:c-l}}function Qu(e,n){return Qs(e)<=0?0:n.maxPlaneDisplacement}function Qs(e){let n=1/0,t=1/0,r=1/0,o=-1/0,s=-1/0,i=-1/0;for(let c of e)for(let[l,u,f]of c.vertices)n=Math.min(n,l),t=Math.min(t,u),r=Math.min(r,f),o=Math.max(o,l),s=Math.max(s,u),i=Math.max(i,f);if(!Number.isFinite(n))return 0;let a=Math.hypot(o-n,s-t,i-r);return a<=0?0:Math.min(.025,Math.max(1e-4,a*25e-5))}function Ju(e){let n=new Map,t=o=>Math.floor(o/e),r=(o,s,i)=>`${o},${s},${i}`;return{snap(o){let s=t(o[0]),i=t(o[1]),a=t(o[2]);for(let f=-1;f<=1;f++)for(let d=-1;d<=1;d++)for(let m=-1;m<=1;m++){let g=n.get(r(s+f,i+d,a+m));if(g){for(let h of g)if(lf(o,h)<=e)return[h[0],h[1],h[2]]}}let c=[o[0],o[1],o[2]],l=r(s,i,a),u=n.get(l);return u?u.push(c):n.set(l,[c]),c}}}function ef(e){let n=new Map,t=o=>Math.floor(o/e),r=(o,s)=>`${o},${s}`;return{snap(o){let s=t(o[0]),i=t(o[1]);for(let u=-1;u<=1;u++)for(let f=-1;f<=1;f++){let d=n.get(r(s+u,i+f));if(d){for(let m of d)if(Math.hypot(o[0]-m[0],o[1]-m[1])<=e)return[m[0],m[1]]}}let a=[o[0],o[1]],c=r(s,i),l=n.get(c);return l?l.push(a):n.set(c,[a]),a}}}function Js(e){return[e.color??"#cccccc",e.texture??"",e.texture&&e.textureWrap?`${e.textureWrap.s}/${e.textureWrap.t}`:"",e.texture&&e.textureAlphaMode?e.textureAlphaMode:"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided"].join("|")}function _r(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,r=0,o=0,s=n[0];for(let a=1;a<n.length-1;a++){let c=zn(n[a],s),l=zn(n[a+1],s),u=ri(c,l);t+=u[0],r+=u[1],o+=u[2]}let i=Math.hypot(t,r,o);return i<=1e-10?null:{normal:[t/i,r/i,o/i],area:i/2}}function nf(e,n){let t=new Map,r=new Map;for(let o=0;o<e.length;o++){let s=e[o];if(!(!n[o]||s.vertices.length<3))for(let i=0;i<s.vertices.length;i++){let a=Tt(s.vertices[i],s.vertices[(i+1)%s.vertices.length]),c=t.get(a);c?c.push(o):t.set(a,[o])}}for(let o of t.values())for(let s=0;s<o.length;s++)for(let i=s+1;i<o.length;i++){let a=o[s],c=o[i];tf(e[a],e[c],n[a],n[c])&&(Ws(r,a,c),Ws(r,c,a))}return r}function tf(e,n,t,r){if(!t||!r||t.materialKey!==r.materialKey||!!e.uvs!=!!n.uvs)return!1;if(St(e)||St(n))return vr(e,n);if(!e.uvs||!n.uvs)return!0;let o=Ir(e,n);if(!o)return!1;let[s,i,a,c]=o;return Et(e.uvs[s],n.uvs[a])&&Et(e.uvs[i],n.uvs[c])}function rf(e,n,t,r){return t.materialKey!==r.materialKey?!1:St(e)||St(n)?vr(e,n):!e.uvs&&!n.uvs&&!e.textureTriangles?.length&&!n.textureTriangles?.length}function St(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function vr(e,n){if(!e.texture||!n.texture||e.texture!==n.texture||(e.textureWrap?.s??"")!==(n.textureWrap?.s??"")||(e.textureWrap?.t??"")!==(n.textureWrap?.t??"")||(e.textureAlphaMode??"")!==(n.textureAlphaMode??"")||e.material?.texture||n.material?.texture||!e.uvs||!n.uvs||e.uvs.length!==e.vertices.length||n.uvs.length!==n.vertices.length)return!1;let t=Ir(e,n);if(!t)return!1;let[r,o,s,i]=t;return Et(e.uvs[r],n.uvs[s])&&Et(e.uvs[o],n.uvs[i])}function Ws(e,n,t){let r=e.get(n);r?r.add(t):e.set(n,new Set([t]))}function of(e,n,t,r,o,s){let i=[e],a=new Set([e]),c=[e];for(;c.length>0;){let l=c.shift();for(let u of t.get(l)??[]){if(r.has(u)||a.has(u))continue;let f=n[u],d=n[e];!f||!d||f.materialKey===d.materialKey&&sf([...i,u],n,o,s)&&(i.push(u),a.add(u),c.push(u))}}return i}function sf(e,n,t,r){let o=ei(e,n);return!!o&&ni(e,n,o,t,r)}function ei(e,n){let t=n[e[0]];if(!t)return null;let r=0,o=0,s=0,i=0,a=0,c=0,l=0;for(let m of e){let g=n[m];if(!g)return null;let h=ze(t.normal,g.normal)<0?-1:1,p=Math.max(g.area,1e-6);r+=g.normal[0]*h*p,o+=g.normal[1]*h*p,s+=g.normal[2]*h*p;for(let x of g.polygon.vertices)i+=x[0],a+=x[1],c+=x[2],l+=1}let u=oi([r,o,s]);if(!u||l===0)return null;let f=ti(e,n),d=af(e,n,u,f);if(d){let m=(d.min+d.max)/2;return{normal:u,point:[u[0]*m,u[1]*m,u[2]*m],boundaryVertexKeys:f}}return{normal:u,point:[i/l,a/l,c/l],boundaryVertexKeys:f}}function af(e,n,t,r){let o=1/0,s=-1/0;for(let i of e){let a=n[i];if(a)for(let c of a.polygon.vertices){if(!r.has(Oe(c)))continue;let l=ze(c,t);o=Math.min(o,l),s=Math.max(s,l)}}return Number.isFinite(o)&&Number.isFinite(s)?{min:o,max:s}:null}function ni(e,n,t,r,o){let s=Math.cos(o.maxAngleDeg*Math.PI/180),i=t.boundaryVertexKeys??ti(e,n);for(let a of e){let c=n[a];if(!c||Math.abs(ze(c.normal,t.normal))<s)return!1;for(let l of c.polygon.vertices){let u=i.has(Oe(l))?o.maxBoundaryDisplacement:r;if(Math.abs(Vr(l,t))>u)return!1}}return!0}function ti(e,n){let t=new Map;for(let o of e){let s=n[o];if(!s)continue;let i=s.polygon.vertices;for(let a=0;a<i.length;a++){let c=i[a],l=i[(a+1)%i.length],u=Tt(c,l),f=t.get(u);f?f.count+=1:t.set(u,{count:1,a:c,b:l})}}let r=new Set;for(let o of t.values())o.count===1&&(r.add(Oe(o.a)),r.add(Oe(o.b)));return r}function cf(e,n){let t=o=>Or(o,n),r=Cr(e.textureTriangles,t);return{...e,vertices:e.vertices.map(t),...r?{textureTriangles:r}:{}}}function Ir(e,n){for(let t=0;t<e.vertices.length;t++){let r=(t+1)%e.vertices.length;for(let o=0;o<n.vertices.length;o++){let s=(o+1)%n.vertices.length;if(Pt(e.vertices[t],n.vertices[o])&&Pt(e.vertices[r],n.vertices[s]))return[t,r,o,s];if(Pt(e.vertices[t],n.vertices[s])&&Pt(e.vertices[r],n.vertices[o]))return[t,r,s,o]}}return null}function Tt(e,n){let t=Oe(e),r=Oe(n);return t<r?`${t}|${r}`:`${r}|${t}`}function Oe(e){return`${e[0]},${e[1]},${e[2]}`}function Pt(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function Et(e,n){return Math.abs(e[0]-n[0])<=1e-4&&Math.abs(e[1]-n[1])<=1e-4}function zn(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function ri(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function ze(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function lf(e,n){return Math.hypot(e[0]-n[0],e[1]-n[1],e[2]-n[2])}function oi(e){let n=Math.hypot(e[0],e[1],e[2]);return n<=1e-10?null:[e[0]/n,e[1]/n,e[2]/n]}function Vr(e,n){return ze(zn(e,n.point),n.normal)}function Or(e,n){let t=Vr(e,n);return[e[0]-n.normal[0]*t,e[1]-n.normal[1]*t,e[2]-n.normal[2]*t]}var Ct=1e-5,si=.001,ii=6;function Rr(e){let n=e.vertices;if(n.length<3)return null;let t=n[0],r=0,o=0,s=0;for(let a=1;a+1<n.length;a++){let c=n[a],l=n[a+1],u=c[1]-t[1],f=c[0]-t[0],d=c[2]-t[2],m=l[1]-t[1],g=l[0]-t[0],h=l[2]-t[2];r-=f*h-d*g,o-=d*m-u*h,s-=u*g-f*m}let i=Math.hypot(r,o,s);return i<1e-9?null:[r/i,o/i,s/i]}function ai(e,n){let t=n.meshRotation,r=t?it(e,t[0]??0,t[1]??0,t[2]??0):e;return it(r,n.rotX,0,n.rotY)[2]}function Lr(e,n,t=Ct){return ai(e,n)>t}function uf(e,n,t=Ct){let r=Rr(e);return r===null||Lr(r,n,t)}function ci(e){return`${e[0].toFixed(4)},${e[1].toFixed(4)},${e[2].toFixed(4)}`}function li(e){let n=new Map;for(let t of e){if(!t)continue;let r=ci(t);n.has(r)||n.set(r,t)}return Array.from(n,([t,r])=>({key:t,normal:r}))}function ff(e){return li(e.map(Rr))}function ui(e,n=si){let t=Math.abs(e[0]),r=Math.abs(e[1]),o=Math.abs(e[2]),s=Math.max(t,r,o);return s>1-n&&t+r+o-s<n}function mf(e){return e.length<=ii&&e.every(({normal:n})=>ui(n))}function df(e,n,t=Ct){let r=[];for(let{key:o,normal:s}of e)Lr(s,n,t)&&r.push(o);return r.sort(),r.join("|")}function wr(e,n,t,r,o){let s=(g,h,p)=>{let x=[0,0,0];return x[e]=g,x[(e+1)%3]=h,x[(e+2)%3]=p,x},i=s(n,-r,-r),a=s(n,r,-r),c=s(n,r,r),l=s(n,-r,r),u=s(t,-r,-r),f=s(t,r,-r),d=s(t,r,r),m=s(t,-r,r);return[{vertices:[i,a,c,l],color:o},{vertices:[u,f,d,m],color:o},{vertices:[i,a,f,u],color:o},{vertices:[a,c,d,f],color:o},{vertices:[c,l,m,d],color:o},{vertices:[l,i,u,m],color:o}]}function fi(e={}){let n=e.size??5,t=e.thickness??.025,r=e.negative??!1,o=e.xColor??"#ff3a3a",s=e.yColor??"#3aff3a",i=e.zColor??"#3a8aff",a=n*t/2,c=r?-n:0;return[...wr(0,c,n,a,o),...wr(1,c,n,a,s),...wr(2,c,n,a,i)]}var gf=["right","left","front","back","top","bottom"];function Nr(e){return[e[0],e[1],e[2]]}function mi(e,n){return{min:[Math.min(e[0],n[0]),Math.min(e[1],n[1]),Math.min(e[2],n[2])],max:[Math.max(e[0],n[0]),Math.max(e[1],n[1]),Math.max(e[2],n[2])]}}function pf(e){if(e.min&&e.max)return mi(e.min,e.max);let n=e.center?Nr(e.center):[0,0,0],t;typeof e.size=="number"?t=[e.size,e.size,e.size]:e.size?t=Nr(e.size):t=[1,1,1];let r=[t[0]/2,t[1]/2,t[2]/2];return mi([n[0]-r[0],n[1]-r[1],n[2]-r[2]],[n[0]+r[0],n[1]+r[1],n[2]+r[2]])}function hf(e,n){let[t,r,o]=e,[s,i,a]=n;return[{name:"right",vertices:[[s,r,o],[s,i,o],[s,i,a],[s,r,a]]},{name:"left",vertices:[[t,i,o],[t,r,o],[t,r,a],[t,i,a]]},{name:"front",vertices:[[s,i,o],[t,i,o],[t,i,a],[s,i,a]]},{name:"back",vertices:[[t,r,o],[s,r,o],[s,r,a],[t,r,a]]},{name:"top",vertices:[[t,r,a],[s,r,a],[s,i,a],[t,i,a]]},{name:"bottom",vertices:[[t,i,o],[s,i,o],[s,r,o],[t,r,o]]}]}function xf(e,n){let t={...e,...n};return(e.data!==void 0||n?.data!==void 0)&&(t.data={...e.data??{},...n?.data??{}}),t}function bf(e,n){let t={vertices:e.vertices.map(Nr)};return n.color!==void 0&&(t.color=n.color),n.texture!==void 0&&(t.texture=n.texture),n.material!==void 0&&(t.material=n.material),n.uvs!==void 0&&(t.uvs=n.uvs.map(r=>[r[0],r[1]])),n.data!==void 0&&(t.data={...n.data}),t}function di(e={}){let{min:n,max:t}=pf(e);if(n[0]===t[0]||n[1]===t[1]||n[2]===t[2])return[];let r={color:e.color??"#ffffff",texture:e.texture,material:e.material,uvs:e.uvs,data:e.data},o=hf(n,t),s=new Map(o.map(a=>[a.name,a])),i=[];for(let a of gf){let c=e.faces?.[a];if(c===!1)continue;let l=s.get(a);l&&i.push(bf(l,xf(r,c)))}return i}function gi(e,n,t,r){let o=[0,0,0];return o[e]=n,o[(e+1)%3]=t,o[(e+2)%3]=r,o}function yf(e,n,t,r,o){let s=(g,h,p)=>gi(e,g,h,p),i=s(n,-r,-r),a=s(n,r,-r),c=s(n,r,r),l=s(n,-r,r),u=s(t,-r,-r),f=s(t,r,-r),d=s(t,r,r),m=s(t,-r,r);return[{vertices:[i,a,c,l],color:o},{vertices:[u,f,d,m],color:o},{vertices:[i,a,f,u],color:o},{vertices:[a,c,d,f],color:o},{vertices:[c,l,m,d],color:o},{vertices:[l,i,u,m],color:o}]}function Pf(e,n,t,r,o){let s=(f,d,m)=>gi(e,f,d,m),i=s(n,-r,-r),a=s(n,r,-r),c=s(n,r,r),l=s(n,-r,r),u=s(t,0,0);return[{vertices:[i,a,c,l],color:o},{vertices:[i,a,u],color:o},{vertices:[a,c,u],color:o},{vertices:[c,l,u],color:o},{vertices:[l,i,u],color:o}]}function Af(e){return e.map(n=>({...n,vertices:[...n.vertices].reverse()}))}function pi(e){let n=e.axis,t=e.sign??1,r=e.shaftLength??4,o=e.shaftHalfThickness??.05,s=e.headLength??.8,i=e.headHalfThickness??.2,a=e.color??"#ffffff",c=e.shaft??!0,l=r*t,u=Math.min(0,l),f=Math.max(0,l),d=l,m=(r+s)*t,g=Pf(n,d,m,i,a),h=t===-1?Af(g):g;return c?[...yf(n,u,f,o,a),...h]:h}function _t(e,n,t){let r=[0,0,0],o=(e+1)%3,s=(e+2)%3;return r[o]=Math.cos(t)*n,r[s]=Math.sin(t)*n,r}function hi(e){let n=e.axis,t=e.radius,r=e.halfThickness??Math.max(.05,t*.04),o=e.segments??32,s=e.color??"#ffffff",i=t-r,a=t+r,c=[];for(let l=0;l<o;l++){let u=l/o*Math.PI*2,f=(l+1)/o*Math.PI*2,d=_t(n,i,u),m=_t(n,i,f),g=_t(n,a,u),h=_t(n,a,f);c.push({vertices:[d,g,h,m],color:s})}return c}function xi(e){let n=e.axis,t=e.outerRadius,r=e.color??"#ffffff",o=(n+1)%3,s=(n+2)%3,i=(a,c)=>{let l=[0,0,0];return l[n]=0,l[o]=a,l[s]=c,l};return[{vertices:[i(-t,-t),i(t,-t),i(t,t),i(-t,t)],color:r}]}function bi(e){let n=e.axis,t=e.size??.4,r=e.offset??t*2,o=typeof r=="number"?r:r[0],s=typeof r=="number"?r:r[1],i=e.along??0,a=e.color??"#ffffff",c=(n+1)%3,l=(n+2)%3,u=(f,d)=>{let m=[0,0,0];return m[n]=i,m[c]=o+f,m[l]=s+d,m};return[{vertices:[u(-t,-t),u(t,-t),u(t,t),u(-t,t)],color:a}]}function yi(e){let{center:n,size:t,color:r="#ffffff"}=e,[o,s,i]=n,a=[[o+t,s,i],[o-t,s,i],[o,s+t,i],[o,s-t,i],[o,s,i+t],[o,s,i-t]];return[[0,2,4],[2,1,4],[1,3,4],[3,0,4],[2,0,5],[1,2,5],[3,1,5],[0,3,5]].map(l=>({vertices:[a[l[0]],a[l[1]],a[l[2]]],color:r}))}function Pi(e={}){let{radius:n=50,color:t="#cccccc"}=e,r=Math.min(Math.max(0,Math.floor(e.subdivisions??1)),3),o=(1+Math.sqrt(5))/2,s=[[-1,o,0],[1,o,0],[-1,-o,0],[1,-o,0],[0,-1,o],[0,1,o],[0,-1,-o],[0,1,-o],[o,0,-1],[o,0,1],[-o,0,-1],[-o,0,1]].map(([c,l,u])=>{let f=Math.sqrt(c*c+l*l+u*u);return[c/f,l/f,u/f]}),i=[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]];for(let c=0;c<r;c++){let l=[...s],u=[],f=new Map,d=(m,g)=>{let h=m<g?`${m}-${g}`:`${g}-${m}`,p=f.get(h);if(p!==void 0)return p;let x=s[m],b=s[g],P=(x[0]+b[0])/2,y=(x[1]+b[1])/2,M=(x[2]+b[2])/2,S=Math.sqrt(P*P+y*y+M*M);P/=S,y/=S,M/=S;let T=l.length;return l.push([P,y,M]),f.set(h,T),T};for(let[m,g,h]of i){let p=d(m,g),x=d(g,h),b=d(h,m);u.push([m,p,b]),u.push([g,x,p]),u.push([h,b,x]),u.push([p,x,b])}s=l,i=u}let a=s.map(([c,l,u])=>[c*n,l*n,u*n]);return i.map(([c,l,u])=>({vertices:[a[c],a[l],a[u]],color:t}))}function Ai(e={}){let{size:n=100,color:t="#cccccc"}=e,r=n/Math.sqrt(3),o=[[r,r,r],[r,-r,-r],[-r,r,-r],[-r,-r,r]];return[[0,1,2],[0,3,1],[0,2,3],[1,3,2]].map(([i,a,c])=>({vertices:[o[i],o[a],o[c]],color:t}))}function Mi(e={}){let{size:n=100,color:t="#cccccc"}=e,r=(1+Math.sqrt(5))/2,o=n/Math.sqrt(1+r*r),s=[[-1,r,0],[1,r,0],[-1,-r,0],[1,-r,0],[0,-1,r],[0,1,r],[0,-1,-r],[0,1,-r],[r,0,-1],[r,0,1],[-r,0,-1],[-r,0,1]].map(([a,c,l])=>[a*o,c*o,l*o]);return[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]].map(([a,c,l])=>({vertices:[s[a],s[c],s[l]],color:t}))}function Si(e={}){let{size:n=100,color:t="#cccccc"}=e,r=(1+Math.sqrt(5))/2,o=1/r,s=n/Math.sqrt(3),a=[[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1],[0,o,r],[0,-o,r],[0,o,-r],[0,-o,-r],[o,r,0],[-o,r,0],[o,-r,0],[-o,-r,0],[r,0,o],[r,0,-o],[-r,0,o],[-r,0,-o]].map(([l,u,f])=>[l*s,u*s,f*s]);return[[12,13,4,8,0],[0,8,9,2,16],[16,17,1,12,0],[1,10,5,13,12],[17,3,11,10,1],[2,9,6,15,14],[2,14,3,17,16],[14,15,7,11,3],[18,6,9,8,4],[4,13,5,19,18],[5,10,11,7,19],[18,19,7,15,6]].map(l=>({vertices:l.map(u=>a[u]),color:t}))}function vt(e={}){let{radius:n=50,height:t=100,radialSegments:r=12,color:o="#cccccc"}=e,s=e.radiusTop??n,i=t/2,a=-i,c=i,l=Math.max(3,r),u=[],f=Array.from({length:l+1},(g,h)=>h/l*Math.PI*2),d=n<=1e-6,m=s<=1e-6;for(let g=0;g<l&&!(d&&m);g++){let h=f[g],p=f[g+1],x=Math.cos(h)*n,b=Math.sin(h)*n,P=Math.cos(p)*n,y=Math.sin(p)*n,M=Math.cos(h)*s,S=Math.sin(h)*s,T=Math.cos(p)*s,E=Math.sin(p)*s,A=[x,b,a],C=[P,y,a],_=[T,E,c],w=[M,S,c];if(m){let V=[0,0,c];u.push({vertices:[A,C,V],color:o})}else if(d){let V=[0,0,a];u.push({vertices:[V,_,w],color:o})}else u.push({vertices:[A,C,_,w],color:o})}if(n>1e-6){let g=[0,0,a];for(let h=0;h<l;h++){let p=f[h],x=f[h+1],b=[Math.cos(p)*n,Math.sin(p)*n,a],P=[Math.cos(x)*n,Math.sin(x)*n,a];u.push({vertices:[[...g],[...P],[...b]],color:o})}}if(s>1e-6){let g=[0,0,c];for(let h=0;h<l;h++){let p=f[h],x=f[h+1],b=[Math.cos(p)*s,Math.sin(p)*s,c],P=[Math.cos(x)*s,Math.sin(x)*s,c];u.push({vertices:[[...g],[...b],[...P]],color:o})}}return u}function Ei(e={}){return vt({...e,radiusTop:0})}function It(e,n,t,r){let o=Math.sin(e),s=Math.cos(e),i=Math.sin(n),a=Math.cos(n);return[(t+r*a)*s,(t+r*a)*o,r*i]}function Ti(e={}){let{radius:n=50,tube:t=15,radialSegments:r=12,tubularSegments:o=16,color:s="#cccccc"}=e,i=Math.max(3,r),a=Math.max(3,o),c=[];for(let l=0;l<i;l++){let u=l/i*Math.PI*2,f=(l+1)/i*Math.PI*2;for(let d=0;d<a;d++){let m=d/a*Math.PI*2,g=(d+1)/a*Math.PI*2,h=It(u,m,n,t),p=It(f,m,n,t),x=It(f,g,n,t),b=It(u,g,n,t);c.push({vertices:[h,p,x,b],color:s})}}return c}var Mf=.01,Sf=.01;function Ef(e,n){let t=Math.hypot(e[0],e[1],e[2])||1,r=e[0]/t,o=e[1]/t,s=e[2]/t,i=Math.sign(s||1)*Math.max(Math.abs(s),.01),a=n,c=.01;return[1,0,0,0,0,1,0,0,-r/i,-o/i,c,0,a*r/i,a*o/i,a*(1-c),1]}function Tf(e,n){let t=Math.hypot(n[0],n[1],n[2])||1,r=n[0]/t,o=n[1]/t,s=n[2]/t;return e[0]*r+e[1]*o+e[2]*s>0}function Cf(e){if(e.length<=1)return e.map(i=>[i[0],i[1]]);let t=e.map(i=>[i[0],i[1]]);t.sort((i,a)=>i[0]-a[0]||i[1]-a[1]);let r=(i,a,c)=>(a[0]-i[0])*(c[1]-i[1])-(a[1]-i[1])*(c[0]-i[0]),o=[];for(let i of t){for(;o.length>=2&&r(o[o.length-2],o[o.length-1],i)<=0;)o.pop();o.push(i)}let s=[];for(let i=t.length-1;i>=0;i--){let a=t[i];for(;s.length>=2&&r(s[s.length-2],s[s.length-1],a)<=0;)s.pop();s.push(a)}return o.pop(),s.pop(),o.concat(s)}function Ci(e){let n=0,t=e.length;for(let r=0;r<t;r++){let o=e[r],s=e[(r+1)%t];n+=o[0]*s[1]-s[0]*o[1]}return n/2}function _f(e){let n=e.map(t=>[t[0],t[1]]);return Ci(n)<0&&n.reverse(),n}function vf(e,n,t){let r=Math.hypot(n[0],n[1],n[2])||1,o=n[0]/r,s=n[1]/r,i=n[2]/r,a=Math.sign(i||1)*Math.max(Math.abs(i),.01),c=e[2]-t;return[e[0]-o/a*c,e[1]-s/a*c]}function If(e,n){if(e.length===0||n.length<3)return[];let t=e.map(o=>[o[0],o[1]]),r=n.length;for(let o=0;o<r;o++){if(t.length===0)return[];let s=t;t=[];let i=n[o],a=n[(o+1)%r],c=f=>(a[0]-i[0])*(f[1]-i[1])-(a[1]-i[1])*(f[0]-i[0])>=0,l=(f,d)=>{let m=i[0],g=i[1],h=a[0],p=a[1],x=f[0],b=f[1],P=d[0],y=d[1],M=(m-h)*(b-y)-(g-p)*(x-P);if(Math.abs(M)<1e-12)return[f[0],f[1]];let S=((m-x)*(b-y)-(g-b)*(x-P))/M;return[m+S*(h-m),g+S*(p-g)]},u=s.length;for(let f=0;f<u;f++){let d=s[f],m=s[(f+u-1)%u],g=c(d),h=c(m);g?(h||t.push(l(m,d)),t.push([d[0],d[1]])):h&&t.push(l(m,d))}}return t}var Vf=Symbol.for("polycss.animation.triangleFrameSource"),Of=Symbol.for("polycss.animation.triangleFrameTarget"),_i=2200,Fr=2201,vi=2202;function Rf(e,n){let t=0,r=1,o=1,s=Fr,i=1/0,a=0,c=!1,l=null,u=!0,f=!1,d={clampWhenFinished:!1,get timeScale(){return o},set timeScale(m){o=m},get weight(){return r},set weight(m){r=m},get time(){return t},set time(m){t=m},get isRunning(){return c},get enabled(){return u},set enabled(m){u=m},get paused(){return f},set paused(m){f=m},play(){return c=!0,d},stop(){return c=!1,t=0,a=0,l=null,d},reset(){return t=0,a=0,d},fadeIn(m){return l={from:0,to:1,elapsed:0,duration:m},r=0,d},fadeOut(m){return l={from:r,to:0,elapsed:0,duration:m},d},crossFadeTo(m,g){return d.fadeOut(g),m.fadeIn(g),d},crossFadeFrom(m,g){return m.fadeOut(g),d.fadeIn(g),d},setLoop(m,g){return s=m,i=g,d},setEffectiveTimeScale(m){return o=m,d},setEffectiveWeight(m){return r=m,d}};return d._internal={get clip(){return e},get time(){return t},get weight(){return r},get enabled(){return u},get paused(){return f},get running(){return c},get loopMode(){return s},get repetitions(){return i},get completedReps(){return a},set completedReps(m){a=m},get clampWhenFinished(){return d.clampWhenFinished},get timeScale(){return o},get fade(){return l},advance(m){if(!c)return;if(l){l.elapsed+=m;let h=l.duration>0?Math.min(l.elapsed/l.duration,1):1;if(r=l.from+(l.to-l.from)*h,h>=1&&(r=l.to,l=null,r<=0)){c=!1;return}}if(f)return;let g=e.duration;if(!(g<=0)){if(t+=m*o,s===_i)t>=g&&(t=d.clampWhenFinished?g:0,c=!1);else if(s===Fr){if(t>=g){let h=t%g,p=Math.floor(t/g);a+=p,t=h,i!==1/0&&a>=i&&(t=d.clampWhenFinished?g:0,c=!1)}}else if(g>0){let h=g*2,p=t%h;if(t>=h){let x=Math.floor(t/h);a+=x,i!==1/0&&a>=i?(t=d.clampWhenFinished?g:0,c=!1):t=p}}}},sampleTime(){if(s===vi){let m=e.duration,g=m*2,h=t%g;return h<=m?h:g-h}return t}},d}function Lf(e){return e._internal}function Dr(e,n){return typeof n=="number"?e[n]:e.find(t=>t.name===n)}function wf(e,n){let t=new Map,r=n,o=e;function s(f){let d=Dr(n.clips,f);if(!d)throw new Error(`PolyAnimationMixer: no clip found for key "${f}". Available: ${n.clips.map(g=>g.name).join(", ")}`);let m=t.get(d.index);return m||(m=Rf(d,n),t.set(d.index,m)),m}function i(f){let d=Dr(n.clips,f);return d?t.get(d.index)??null:null}function a(f){let d=[];for(let x of t.values()){let b=Lf(x);b.advance(f),b.running&&b.enabled&&d.push({internal:b,clip:b.clip})}if(d.length===0)return;if(d.length===1){let{internal:x,clip:b}=d[0],P=x.sampleTime(),y=r[Vf]?.(b.name,P);if(y&&o[Of]?.(y))return;let M=n.sample(b.name,P);e.setPolygons(M);return}let m=0;for(let{internal:x}of d)m+=x.weight;if(m<=0)return;let g=d.map(({internal:x,clip:b})=>({polygons:n.sample(b.name,x.sampleTime()),weight:x.weight/m})),h=g[0].polygons;if(h.length===0)return;let p=h.map((x,b)=>{let P=x.vertices.map((y,M)=>{let S=0,T=0,E=0;for(let{polygons:A,weight:C}of g){let _=A[b];if(!_)continue;let w=_.vertices[M];w&&(S+=w[0]*C,T+=w[1]*C,E+=w[2]*C)}return[S,T,E]});return{...x,vertices:P}});e.setPolygons(p)}function c(){for(let f of t.values())f.stop()}function l(f){let d=Dr(n.clips,f);d&&t.delete(d.index)}function u(){t.clear()}return{clipAction:s,existingAction:i,update:a,stopAllAction:c,uncacheClip:l,uncacheRoot:u}}var Ur={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},Ni={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16},Nf={0:"POINTS",1:"LINES",2:"LINE_LOOP",3:"LINE_STRIP",4:"TRIANGLES",5:"TRIANGLE_STRIP",6:"TRIANGLE_FAN"};function Di(e){let n=globalThis.TextDecoder;return new n().decode(e)}function Df(e){let n=e.indexOf(",");if(n<0)throw new Error("parseGltf: malformed data: URI");let t=e.slice(5,n),r=e.slice(n+1);if(!t.includes(";base64")){let i=decodeURIComponent(r),a=new Uint8Array(i.length);for(let c=0;c<i.length;c++)a[c]=i.charCodeAt(c)&255;return a}let o=globalThis.atob(r),s=new Uint8Array(o.length);for(let i=0;i<o.length;i++)s[i]=o.charCodeAt(i);return s}function Ff(e){let n=new DataView(e);if(n.getUint32(0,!0)!==1179937895)throw new Error("parseGltf: not a GLB (bad magic)");let t=n.getUint32(4,!0);if(t!==2)throw new Error(`parseGltf: only glTF v2 supported (got v${t})`);let r=12,o=null,s=null;for(;r<e.byteLength;){let i=n.getUint32(r,!0),a=n.getUint32(r+4,!0),c=r+8;if(a===1313821514){let l=new Uint8Array(e,c,i);o=JSON.parse(Di(l))}else a===5130562&&(s=new Uint8Array(e,c,i));r=c+i}if(!o)throw new Error("parseGltf: no JSON chunk in GLB");return{doc:o,bin:s}}function Ii(e){if(!e)return null;let n=/^(\d+)\.(\d+)(?:\D.*)?$/.exec(e);return n?[Number(n[1]),Number(n[2])]:null}function Bf(e){let n=Ii(e.asset?.version);if(!n||n[0]!==2)throw new Error(`parseGltf: only glTF asset v2 supported (got ${e.asset?.version??"missing"})`);let t=Ii(e.asset?.minVersion);if(e.asset?.minVersion&&(!t||t[0]>2||t[0]===2&&t[1]>0))throw new Error(`parseGltf: glTF asset requires minVersion ${e.asset.minVersion}`)}function kf(e,n,t){let r=e.buffers??[],o=(e.extensionsRequired??[]).includes("EXT_meshopt_compression");return r.map((s,i)=>{let a=s.uri;if(a){if(a.startsWith("data:"))return Df(a);if(t){let c=t(a);if(c instanceof Uint8Array)return c;throw new Error("parseGltf: resolveBuffer returned a Promise; use parseGltf via async if your buffers are external")}throw new Error(`parseGltf: external buffer URI "${a}" \u2014 provide options.resolveBuffer`)}if(i===0&&n)return n;if(o&&s.extensions?.EXT_meshopt_compression)return new Uint8Array(0);throw new Error(`parseGltf: buffer[${i}] has no uri and no GLB BIN chunk`)})}function Wn(e,n,t){let r=e.bufferViews?.[t],o=r?n[r.buffer]:void 0;if(!r||!o)throw new Error(`parseGltf: bad bufferView ${t}`);let s=r.byteOffset??0;if(s<0||r.byteLength<0||s+r.byteLength>o.byteLength)throw new Error(`parseGltf: bufferView ${t} outside buffer ${r.buffer}`);return{buffer:o,view:r}}function Fi(e,n,t,r){if(e.count<=0)return;let o=e.byteOffset??0,s=o+t*(e.count-1)+r;if(o<0||s>n.byteLength)throw new Error("parseGltf: accessor does not fit bufferView")}function Bi(e,n){return e.byteStride!==void 0&&e.byteStride>0?e.byteStride:n}function Uf(e,n,t){if(t||e===5126)return new Float32Array(n);switch(e){case 5120:return new Int8Array(n);case 5121:return new Uint8Array(n);case 5122:return new Int16Array(n);case 5123:return new Uint16Array(n);case 5125:return new Uint32Array(n);default:throw new Error(`parseGltf: unhandled componentType ${e}`)}}function Br(e,n,t){let r=e.accessors?.[t];if(!r)throw new Error(`parseGltf: bad accessor ${t}`);let o=Ur[r.componentType],s=Ni[r.type];if(!o||!s)throw new Error(`parseGltf: unsupported accessor type ${r.type}/${r.componentType}`);if(r.sparse||r.normalized||r.bufferView===void 0){let{values:p}=jn(e,n,t);return{array:Uf(r.componentType,p,r.normalized),count:r.count,componentCount:s}}let{buffer:i,view:a}=Wn(e,n,r.bufferView),c=(a.byteOffset??0)+(r.byteOffset??0),l=r.count*s,u=o*s,f=Bi(a,u);if(Fi(r,a,f,u),f===u){let p=i.buffer.slice(i.byteOffset+c,i.byteOffset+c+l*o),x;switch(r.componentType){case 5120:x=new Int8Array(p);break;case 5121:x=new Uint8Array(p);break;case 5122:x=new Int16Array(p);break;case 5123:x=new Uint16Array(p);break;case 5125:x=new Uint32Array(p);break;case 5126:x=new Float32Array(p);break;default:throw new Error(`parseGltf: unhandled componentType ${r.componentType}`)}return{array:x,count:r.count,componentCount:s}}let d;switch(r.componentType){case 5120:d=new Int8Array(l);break;case 5121:d=new Uint8Array(l);break;case 5122:d=new Int16Array(l);break;case 5123:d=new Uint16Array(l);break;case 5125:d=new Uint32Array(l);break;case 5126:d=new Float32Array(l);break;default:throw new Error(`parseGltf: unhandled componentType ${r.componentType}`)}let m=new DataView(i.buffer),g=i.byteOffset+c,h=0;for(let p=0;p<r.count;p++){let x=g+p*f;for(let b=0;b<s;b++)d[h++]=Ot(m,x+b*o,r.componentType)}return{array:d,count:r.count,componentCount:s}}function Ot(e,n,t){switch(t){case 5120:return e.getInt8(n);case 5121:return e.getUint8(n);case 5122:return e.getInt16(n,!0);case 5123:return e.getUint16(n,!0);case 5125:return e.getUint32(n,!0);case 5126:return e.getFloat32(n,!0);default:throw new Error(`parseGltf: unhandled componentType ${t}`)}}function Vi(e,n){switch(n){case 5120:return Math.max(e/127,-1);case 5121:return e/255;case 5122:return Math.max(e/32767,-1);case 5123:return e/65535;default:return e}}function jn(e,n,t){let r=e.accessors?.[t];if(!r)throw new Error(`parseGltf: bad accessor ${t}`);let o=Ur[r.componentType],s=Ni[r.type];if(!o||!s)throw new Error(`parseGltf: unsupported accessor type ${r.type}/${r.componentType}`);let i=o*s,a=new Array(r.count*s).fill(0);if(r.bufferView!==void 0){let{buffer:c,view:l}=Wn(e,n,r.bufferView),u=c.byteOffset+(l.byteOffset??0)+(r.byteOffset??0),f=Bi(l,i);Fi(r,l,f,i);let d=new DataView(c.buffer),m=0;for(let g=0;g<r.count;g++){let h=u+g*f;for(let p=0;p<s;p++){let x=Ot(d,h+p*o,r.componentType);a[m++]=r.normalized?Vi(x,r.componentType):x}}}if(r.sparse){let c=r.sparse,l=c.indices,u=Ur[l.componentType];if(l.componentType!==5121&&l.componentType!==5123&&l.componentType!==5125)throw new Error(`parseGltf: unhandled sparse index componentType ${l.componentType}`);let{buffer:f,view:d}=Wn(e,n,l.bufferView),m=f.byteOffset+(d.byteOffset??0)+(l.byteOffset??0),g=new DataView(f.buffer),{buffer:h,view:p}=Wn(e,n,c.values.bufferView),x=h.byteOffset+(p.byteOffset??0)+(c.values.byteOffset??0),b=new DataView(h.buffer);for(let P=0;P<c.count;P++){let y=Ot(g,m+P*u,l.componentType);for(let M=0;M<s;M++){let S=Ot(b,x+(P*s+M)*o,r.componentType);a[y*s+M]=r.normalized?Vi(S,r.componentType):S}}}return{values:a,count:r.count,componentCount:s}}function Gf(e,n,t){let r=[],o=[],s=globalThis;for(let i of e.images??[]){if(i.uri){if(t&&!i.uri.startsWith("data:"))try{r.push(new s.URL(i.uri,t).href)}catch{r.push(i.uri)}else r.push(i.uri);continue}if(i.bufferView!==void 0){let a;try{let{buffer:f,view:d}=Wn(e,n,i.bufferView),m=d.byteOffset??0;a=f.subarray(m,m+d.byteLength)}catch{r.push("");continue}let c=i.mimeType??"image/png",l=new s.Blob([a],{type:c}),u=s.URL.createObjectURL(l);r.push(u),o.push(u)}else r.push("")}return{urls:r,objectUrls:o}}function Oi(e){switch(e??10497){case 33071:return"clamp-to-edge";case 33648:return"mirrored-repeat";case 10497:return"repeat";default:return"repeat"}}function $f(e,n){let t=n?.sampler!==void 0?e.samplers?.[n.sampler]:void 0;return{s:Oi(t?.wrapS),t:Oi(t?.wrapT)}}function ki(e){switch(e){case"BLEND":return"blend";case"MASK":return"mask";default:return"opaque"}}function zf(e,n){let t=new Map,r=e.materials??[];for(let o=0;o<r.length;o++){let s=r[o].pbrMetallicRoughness?.baseColorTexture?.index;if(s===void 0)continue;let i=e.textures?.[s],a=i?.source;if(a===void 0)continue;let c=n[a];c&&t.set(o,{url:c,wrap:$f(e,i),alphaMode:ki(r[o].alphaMode),texCoord:r[o].pbrMetallicRoughness?.baseColorTexture?.texCoord??0})}return t}function Xf(e,n,t){let r=e?.pbrMetallicRoughness?.baseColorFactor;if(!r||r.length<3)return n;let o=c=>Math.max(0,Math.min(1,c)),s=c=>Math.round(o(c)*255).toString(16).padStart(2,"0"),i=c=>Math.round(o(c)*255),a=t==="opaque"?1:o(r[3]??1);return a<1?`rgba(${i(r[0])}, ${i(r[1])}, ${i(r[2])}, ${Math.round(a*1e3)/1e3})`:`#${s(r[0])}${s(r[1])}${s(r[2])}`}var dn=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function Ui(e,n){let t=new Array(16);return Gi(t,e,n)}function Gi(e,n,t){for(let r=0;r<4;r++)for(let o=0;o<4;o++)e[o*4+r]=n[0+r]*t[o*4+0]+n[4+r]*t[o*4+1]+n[8+r]*t[o*4+2]+n[12+r]*t[o*4+3];return e}function Wf(e,n){return[e[0]*n[0]+e[4]*n[1]+e[8]*n[2]+e[12],e[1]*n[0]+e[5]*n[1]+e[9]*n[2]+e[13],e[2]*n[0]+e[6]*n[1]+e[10]*n[2]+e[14]]}function $i(e,n,t){let r=e?.[0]??0,o=e?.[1]??0,s=e?.[2]??0,i=n?.[0]??0,a=n?.[1]??0,c=n?.[2]??0,l=n?.[3]??1,u=t?.[0]??1,f=t?.[1]??1,d=t?.[2]??1,m=i+i,g=a+a,h=c+c,p=i*m,x=i*g,b=i*h,P=a*g,y=a*h,M=c*h,S=l*m,T=l*g,E=l*h;return[(1-(P+M))*u,(x+E)*u,(b-T)*u,0,(x-E)*f,(1-(p+M))*f,(y+S)*f,0,(b+T)*d,(y-S)*d,(1-(p+P))*d,0,r,o,s,1]}function jf(e){return e.matrix&&e.matrix.length===16?e.matrix.slice():$i(e.translation,e.rotation,e.scale)}function zi(e,n,t){let r=new Array(Math.min(e.length,n.length));for(let o=0;o<r.length;o++)r[o]=e[o]+(n[o]-e[o])*t;return r}function Xn(e){let n=Math.hypot(e[0]??0,e[1]??0,e[2]??0,e[3]??1)||1;return[(e[0]??0)/n,(e[1]??0)/n,(e[2]??0)/n,(e[3]??1)/n]}function Yf(e,n,t){let r=Xn(e),o=Xn(n),s=r[0]*o[0]+r[1]*o[1]+r[2]*o[2]+r[3]*o[3];if(s<0&&(s=-s,o=[-o[0],-o[1],-o[2],-o[3]]),s>.9995)return Xn(zi(r,o,t));let i=Math.acos(Math.max(-1,Math.min(1,s))),a=Math.sin(i),c=Math.sin((1-t)*i)/a,l=Math.sin(t*i)/a;return Xn([r[0]*c+o[0]*l,r[1]*c+o[1]*l,r[2]*c+o[2]*l,r[3]*c+o[3]*l])}function Hf(e){return{translation:e?.translation?.slice()??[0,0,0],rotation:e?.rotation?.slice()??[0,0,0,1],scale:e?.scale?.slice()??[1,1,1],matrix:e?.matrix&&e.matrix.length===16?e.matrix.slice():void 0}}function Ri(e){return e.matrix?e.matrix.slice():$i(e.translation,e.rotation,e.scale)}function Kf(e){let n=e.scene??0,t=e.scenes?.[n]?.nodes;return t&&t.length>0?t:[]}function Li(e,n){let t=e.nodes??[],r=new Array(t.length),o=new Set,s=(a,c)=>{if(a<0||a>=t.length)return;let l=Ui(c,n[a]??dn);r[a]=l,o.add(a);for(let u of t[a].children??[])s(u,l)},i=Kf(e);if(i.length>0)for(let a of i)s(a,dn);for(let a=0;a<t.length;a++)o.has(a)||s(a,dn);return r}var Xi=Symbol("polycss.gltfAnimationRuntimeInfo"),qf=Symbol.for("polycss.animation.triangleFrameSource");function Wi(e,n){return e?e[Xi]?.withPolygonFilter(n):void 0}function kr(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function Zf(e,n,t){return kr(e,n)||kr(e,t)||kr(n,t)}function wi(e,n,t,r,o){if(t===void 0)return;let{values:s,count:i,componentCount:a}=jn(e,n,t);if(i!==o||a<1)return;let c=[];for(let l=0;l<i;l++){let u=[];for(let f=0;f<r;f++)u.push(s[l*a+f]??0);c.push(u)}return c}function Qf(e,n,t,r){if(t===void 0)return Array.from({length:r},()=>dn.slice());let{values:o,componentCount:s,count:i}=jn(e,n,t);if(s!==16)throw new Error(`parseGltf: inverseBindMatrices accessor ${t} is not MAT4`);let a=[];for(let c=0;c<r;c++){let l=Math.min(c,i-1);a.push(o.slice(l*16,l*16+16))}return a}function Vt(e,n){let t=e.componentCount,r=e.interpolation==="CUBICSPLINE"?(n*3+1)*t:n*t;return e.output.slice(r,r+t)}function Jf(e,n,t){let r=e.input;if(r.length===0)return[];if(r.length===1||n<=r[0])return Vt(e,0);let o=r.length-1;if(n>=r[o])return Vt(e,o);let s=0,i=o;for(;s+1<i;){let d=s+i>>1;r[d]<=n?s=d:i=d}let a=r[s],c=r[s+1],l=c>a?(n-a)/(c-a):0,u=Vt(e,s),f=Vt(e,s+1);return e.interpolation==="STEP"?u:t==="rotation"?Yf(u,f,l):zi(u,f,l)}function em(e,n,t,r,o,s){let i=e.animations??[];if(i.length===0||t.length===0)return;let a=(e.nodes??[]).map(p=>Hf(p)),c=a.map(Ri),l=Li(e,c),u=(e.skins??[]).map(p=>({joints:p.joints??[],inverseBindMatrices:Qf(e,n,p.inverseBindMatrices,p.joints?.length??0)})),f=[];for(let p=0;p<i.length;p++){let x=i[p],b=(x.samplers??[]).map(M=>{let S=jn(e,n,M.input),T=jn(e,n,M.output);return{input:S.values,output:T.values,componentCount:T.componentCount,interpolation:M.interpolation??"LINEAR"}}),P=[];for(let M of x.channels??[]){let S=M.target.node,T=M.target.path,E=b[M.sampler];S===void 0||!T||!E||T==="weights"||P.push({sampler:E,targetNode:S,path:T})}let y=P.reduce((M,S)=>{let T=S.sampler.input;return Math.max(M,T[T.length-1]??0)},0);f.push({info:{index:p,name:x.name??`animation_${p}`,duration:y,channelCount:P.length},channels:P})}let d=f.map(p=>p.info);if(d.length===0)return;let m=p=>{let x=t.map(b=>new Array(b.triangleMask.length).fill(!1));for(let b of p){let P=r[b];if(!P)return;let y=x[P.sourceIndex];if(!y||P.triangleIndex<0||P.triangleIndex>=y.length)return;y[P.triangleIndex]=!0}return x.map((b,P)=>{let y=t[P],M=new Set,S=0;for(let T=0;T+2<y.indices.length;T+=3,S++)b[S]&&(M.add(y.indices[T]),M.add(y.indices[T+1]),M.add(y.indices[T+2]));return{triangleMask:b,activeVertices:Array.from(M).sort((T,E)=>T-E)}})},g=(p,x,b,P,y,M,S,T,E)=>{let A=o(p),C=o(x),_=o(b),w={vertices:[A,C,_],color:P};return y&&(w.texture=y),y&&M&&(w.textureWrap=M),y&&S&&(w.textureAlphaMode=S),T&&(w.doubleSided=!0),E&&(w.uvs=E),w},h=p=>{let x=t.reduce((V,N,F)=>{let k=p?.[F]?.triangleMask??N.triangleMask,$=0;for(let R=0;R<k.length;R++)k[R]&&$++;return V+$},0),b=new Float64Array(Math.max(0,x*9)),P=new Array(x),y=new Array(x).fill(!1),M={polygonCount:0,vertices:b,colors:P,textureFlags:y,solidTriangles:!0},S=t.map(V=>new Array(V.positions.length)),T=u.map(V=>V.joints.map(()=>new Array(16))),E=(V,N)=>{let F=typeof V=="number"?f[V]:f.find(B=>B.info.name===V);if(!F)return null;let k=F.info.duration,$=k>0?(N%k+k)%k:Math.max(0,N),R=a.map(B=>({translation:B.translation.slice(),rotation:B.rotation.slice(),scale:B.scale.slice(),matrix:B.matrix?B.matrix.slice():void 0}));for(let B of F.channels){let z=R[B.targetNode];if(!z)continue;let ee=Jf(B.sampler,$,B.path);z.matrix=void 0,B.path==="translation"?z.translation=ee.slice(0,3):B.path==="rotation"?z.rotation=Xn(ee.slice(0,4)):B.path==="scale"&&(z.scale=ee.slice(0,3))}return Li(e,R.map(Ri))},A=(V,N,F,k)=>{let $=F?.activeVertices,R=S[V],B=(z,ee,Q,Z)=>{let ne=R[z];ne?(ne[0]=ee,ne[1]=Q,ne[2]=Z):R[z]=[ee,Q,Z]};if(N.skinIndex!==void 0&&N.joints&&N.weights&&u[N.skinIndex]){let z=u[N.skinIndex],ee=N.joints,Q=N.weights,Z=T[N.skinIndex]??[];for(let j=0;j<z.joints.length;j++){let D=z.joints[j],I=k[D],U=z.inverseBindMatrices[j],G=Z[j]??(Z[j]=new Array(16));Z[j]=I&&U?Gi(G,I,U):void 0}let ne=j=>{let D=N.positions[j],I=0,U=0,G=0,q=0,v=ee[j]??[],O=Q[j]??[];for(let L=0;L<4;L++){let X=O[L]??0;if(X<=0)continue;let H=Math.round(v[L]??0),Y=Z[H];if(!Y)continue;let K=Y[0]*D[0]+Y[4]*D[1]+Y[8]*D[2]+Y[12],te=Y[1]*D[0]+Y[5]*D[1]+Y[9]*D[2]+Y[13],se=Y[2]*D[0]+Y[6]*D[1]+Y[10]*D[2]+Y[14];I+=K*X,U+=te*X,G+=se*X,q+=X}if(q>0){let L=1/q;B(j,I*L,U*L,G*L)}else B(j,N.meshBindWorld[0]*D[0]+N.meshBindWorld[4]*D[1]+N.meshBindWorld[8]*D[2]+N.meshBindWorld[12],N.meshBindWorld[1]*D[0]+N.meshBindWorld[5]*D[1]+N.meshBindWorld[9]*D[2]+N.meshBindWorld[13],N.meshBindWorld[2]*D[0]+N.meshBindWorld[6]*D[1]+N.meshBindWorld[10]*D[2]+N.meshBindWorld[14])};if($)for(let j of $)ne(j);else for(let j=0;j<N.positions.length;j++)ne(j)}else{let z=N.meshNode!==null?k[N.meshNode]??N.meshBindWorld:N.meshBindWorld,ee=Q=>{let Z=N.positions[Q];B(Q,z[0]*Z[0]+z[4]*Z[1]+z[8]*Z[2]+z[12],z[1]*Z[0]+z[5]*Z[1]+z[9]*Z[2]+z[13],z[2]*Z[0]+z[6]*Z[1]+z[10]*Z[2]+z[14])};if($)for(let Q of $)ee(Q);else for(let Q=0;Q<N.positions.length;Q++)ee(Q)}return R},C=(V,N)=>{let F=E(V,N);if(!F)return[];let k=[];for(let $=0;$<t.length;$++){let R=t[$],B=p?.[$],z=B?.triangleMask??R.triangleMask,ee=A($,R,B,F),Q=0;for(let Z=0;Z+2<R.indices.length;Z+=3,Q++){if(!z[Q])continue;let ne=R.indices[Z],j=R.indices[Z+1],D=R.indices[Z+2],I=ee[ne],U=ee[j],G=ee[D];if(!I||!U||!G)continue;let q;if(R.uvs&&R.texture){let O=R.uvs[ne],L=R.uvs[j],X=R.uvs[D];O&&L&&X&&(q=[O,L,X])}let v=g(I,U,G,R.color,R.texture,R.textureWrap,R.textureAlphaMode,R.doubleSided,q);v&&k.push(v)}}return k},_=(V,N)=>{let F=E(V,N);if(!F)return null;b.length<x*9&&(b=new Float64Array(x*9),M.vertices=b),P.length<x&&(P=new Array(x),M.colors=P),y.length<x&&(y=new Array(x).fill(!1),M.textureFlags=y);let k=0,$=0,R=!1;for(let B=0;B<t.length;B++){let z=t[B],ee=p?.[B],Q=ee?.triangleMask??z.triangleMask,Z=A(B,z,ee,F),ne=0;for(let j=0;j+2<z.indices.length;j+=3,ne++){if(!Q[ne])continue;let D=z.indices[j],I=z.indices[j+1],U=z.indices[j+2],G=Z[D],q=Z[I],v=Z[U];if(!G||!q||!v)continue;s(G,b,$),s(q,b,$+3),s(v,b,$+6),P[k]=z.color;let O=!!z.texture;y[k]=O,O&&(R=!0),k++,$+=9}}return M.polygonCount=k,M.solidTriangles=!R,M},w={clips:d,sample:C};return w[qf]=_,w[Xi]={withPolygonFilter(V){let N=m(V);return N?h(N):void 0}},w};return h()}function Gr(e,n){let t=n?.targetSize??60,r=n?.gridShift??1,o=n?.defaultColor??"#888888",s=n?.materialColors??{},i=n?.materialTextures??{},a=e instanceof Uint8Array?e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength):e,c=a.byteLength,l,u=null;if(a.byteLength>=4&&new DataView(a).getUint32(0,!0)===1179937895){let v=Ff(a);l=v.doc,u=v.bin}else l=JSON.parse(Di(new Uint8Array(a)));Bf(l);let f=kf(l,u,n?.resolveBuffer),{urls:d,objectUrls:m}=Gf(l,f,n?.baseUrl),g=zf(l,d),h=[],p=new Set,x=new Set(l.extensionsRequired??[]);function b(v,O){p.has(v)||(p.add(v),h.push(O))}function P(v,O){if(v===void 0)return!1;let L=l.accessors?.[v];if(!L)return!1;let X=H=>l.bufferViews?.[H]?.extensions?.[O]!==void 0;return!!(L.bufferView!==void 0&&X(L.bufferView)||L.sparse&&(X(L.sparse.indices.bufferView)||X(L.sparse.values.bufferView)))}function y(v){return x.has("EXT_meshopt_compression")?v.extensions?.EXT_meshopt_compression||P(v.indices,"EXT_meshopt_compression")?!0:Object.values(v.attributes??{}).some(O=>P(O,"EXT_meshopt_compression")):!1}let M=[],S=[],T=[],E=(l.meshes??[]).map((v,O)=>v.name??`mesh_${O}`),A=(l.materials??[]).map((v,O)=>v.name??`material_${O}`);function C(v,O){if(O===4)return v;let L=[];if(O===5)for(let X=0;X+2<v.length;X++)L.push(v[X],v[X+1+X%2],v[X+2-X%2]);else if(O===6)for(let X=0;X+2<v.length;X++)L.push(v[X+1],v[X+2],v[0]);return L}function _(v){let O=[];for(let L=0;L+2<v.length;L+=3){let X=v[L],H=v[L+1],Y=v[L+2];O.push(X,H,Y,X,Y,H)}return O}function w(v,O,L){let X=l.meshes?.[v];if(X)for(let H of X.primitives){let Y=H.mode??4;if(Y!==4&&Y!==5&&Y!==6){let W=Nf[Y]??`mode ${Y}`;b(`unsupported-mode:${Y}`,`Skipped primitives with unsupported mode ${Y} (${W})`);continue}if(H.extensions?.KHR_draco_mesh_compression&&x.has("KHR_draco_mesh_compression")){b("KHR_draco_mesh_compression","Skipped primitives with unsupported required extension KHR_draco_mesh_compression");continue}if(y(H)){b("EXT_meshopt_compression","Skipped primitives with unsupported required extension EXT_meshopt_compression");continue}if(H.attributes.POSITION===void 0){b(`missing-position:${v}`,`Mesh ${X.name??v}: skipped primitive without POSITION attribute`);continue}let K=H.material!==void 0?l.materials?.[H.material]:void 0,te=ki(K?.alphaMode),se=K?.name,Se=(se?s[se]:void 0)??Xf(K,o,te),le=K?.doubleSided===!0,me=H.material!==void 0?g.get(H.material):void 0,pe=se&&i[se]?i[se]:H.material!==void 0?me?.url:void 0,ve=pe&&H.material!==void 0?me?.wrap:void 0,ye=pe?me?.alphaMode??te:void 0,Ee=pe?me?.texCoord??0:0,{array:xe,count:Pe}=Br(l,f,H.attributes.POSITION);if(!(xe instanceof Float32Array))continue;let Ae=[],ce=[];for(let W=0;W<Pe;W++){let J=[xe[W*3],xe[W*3+1],xe[W*3+2]];Ae.push(J),ce.push(Wf(O,J))}let fe=null,Me=H.attributes[`TEXCOORD_${Ee}`];if(pe&&Me!==void 0){let{array:W,count:J}=Br(l,f,Me);fe=[];let oe=1;W instanceof Uint8Array?oe=1/255:W instanceof Uint16Array&&(oe=1/65535);for(let be=0;be<J;be++){let Te=W[be*2]*oe,Ue=W[be*2+1]*oe;fe.push([Te,1-Ue])}}let ae;if(H.indices!==void 0){let{array:W,count:J}=Br(l,f,H.indices);ae=[];for(let oe=0;oe<J;oe++)ae.push(Number(W[oe]))}else ae=ce.map((W,J)=>J);ae=C(ae,Y),le&&(ae=_(ae));let de;if((l.animations?.length??0)>0){let W=wi(l,f,H.attributes.JOINTS_0,4,Pe),J=wi(l,f,H.attributes.WEIGHTS_0,4,Pe);de={sourceIndex:S.length,meshNode:L,meshBindWorld:O,skinIndex:L!==null?l.nodes?.[L]?.skin:void 0,positions:Ae,indices:ae,triangleMask:[],color:Se,texture:pe,textureWrap:ve,textureAlphaMode:ye,doubleSided:le,uvs:fe??void 0,joints:W,weights:J},S.push(de)}for(let W=0;W+2<ae.length;W+=3){let J=de?de.triangleMask.length:void 0;de&&de.triangleMask.push(!1);let oe=ce[ae[W]],be=ce[ae[W+1]],Te=ce[ae[W+2]];if(!oe||!be||!Te)continue;let Ue;if(fe&&pe){let rn=fe[ae[W]],on=fe[ae[W+1]],sn=fe[ae[W+2]];rn&&on&&sn&&(Ue=[rn,on,sn])}M.push({v0:oe,v1:be,v2:Te,color:Se,texture:pe,textureWrap:ve,textureAlphaMode:ye,doubleSided:le,uvs:Ue,source:de,sourceIndex:de?.sourceIndex,sourceTriangleIndex:J})}}}function V(v,O){let L=l.nodes?.[v];if(!L)return;let X=Ui(O,jf(L));typeof L.mesh=="number"&&w(L.mesh,X,v);for(let H of L.children??[])V(H,X)}let N=l.scene??0,F=l.scenes?.[N]?.nodes;if(F&&F.length>0)for(let v of F)V(v,dn);else for(let v=0;v<(l.meshes?.length??0);v++)w(v,dn,null);let k=nm(m);if(M.length===0)return{polygons:[],objectUrls:m,dispose:k,warnings:h,metadata:{triangleCount:0,meshes:E,materials:A,sourceBytes:c}};let $=1/0,R=1/0,B=1/0,z=-1/0,ee=-1/0,Q=-1/0;for(let v of M)for(let O of[v.v0,v.v1,v.v2])O[0]<$&&($=O[0]),O[0]>z&&(z=O[0]),O[1]<R&&(R=O[1]),O[1]>ee&&(ee=O[1]),O[2]<B&&(B=O[2]),O[2]>Q&&(Q=O[2]);let Z=Math.max(z-$,ee-R,Q-B),ne=Z>0?t/Z:1,j=v=>Math.round(v*1e3)/1e3,D=n?.upAxis??"y",I=D==="z"?([v,O,L])=>[j((v-$)*ne+r),j((O-R)*ne+r),j((L-B)*ne+r)]:([v,O,L])=>[j((L-B)*ne+r),j((v-$)*ne+r),j((O-R)*ne+r)],U=D==="z"?(v,O,L)=>{O[L]=j((v[0]-$)*ne+r),O[L+1]=j((v[1]-R)*ne+r),O[L+2]=j((v[2]-B)*ne+r)}:(v,O,L)=>{O[L]=j((v[2]-B)*ne+r),O[L+1]=j((v[0]-$)*ne+r),O[L+2]=j((v[1]-R)*ne+r)},G=[];for(let v of M){let O=I(v.v0),L=I(v.v1),X=I(v.v2),H=Zf(O,L,X);if(v.source&&v.sourceTriangleIndex!==void 0&&(v.source.triangleMask[v.sourceTriangleIndex]=!H),H)continue;let Y={vertices:[O,L,X],color:v.color};v.texture&&(Y.texture=v.texture),v.texture&&v.textureWrap&&(Y.textureWrap=v.textureWrap),v.texture&&v.textureAlphaMode&&(Y.textureAlphaMode=v.textureAlphaMode),v.doubleSided&&(Y.doubleSided=!0),v.uvs&&(Y.uvs=v.uvs),G.push(Y),T.push(v.sourceIndex!==void 0&&v.sourceTriangleIndex!==void 0?{sourceIndex:v.sourceIndex,triangleIndex:v.sourceTriangleIndex}:void 0)}let q=em(l,f,S,T,I,U);return{polygons:G,animation:q,objectUrls:m,dispose:k,warnings:h,metadata:{triangleCount:G.length,meshes:E,materials:A,animations:q?.clips,sourceBytes:c}}}function nm(e){let n=!1;return()=>{if(n)return;n=!0;let t=globalThis.URL;if(t?.revokeObjectURL)for(let r of e)try{t.revokeObjectURL(r)}catch{}}}var Ji=Symbol("polycss.solidTextureBakedAnimationInfo"),ji=Symbol.for("polycss.animation.triangleFrameSource"),tm=16*1024*1024,rm=2,om=32,sm=2,im=.08,am=8,cm=128,Yi=32,lm=.045,um=10,gn=6,fm=16;function $r(e){return e.material?.texture??e.texture}function mm(){let e=globalThis;return typeof e.Image!="function"||typeof e.document?.createElement!="function"?null:{Image:e.Image,createCanvas(){return e.document.createElement("canvas")}}}function dm(e,n){return new Promise((t,r)=>{let o=new n,s=!1,i=a=>{s||(s=!0,a())};o.decoding="async",o.onload=()=>i(()=>t(o)),o.onerror=()=>i(()=>r(new Error(`texture load failed: ${e}`))),o.src=e})}async function gm(e,n,t){try{let r=await dm(e,n.Image),o=Math.max(0,Math.floor(r.naturalWidth||r.width||0)),s=Math.max(0,Math.floor(r.naturalHeight||r.height||0));if(o<=0||s<=0||o*s>t)return null;let i=n.createCanvas();i.width=o,i.height=s;let a=i.getContext("2d",{willReadFrequently:!0});if(!a)return null;a.drawImage(r,0,0,o,s);let c=a.getImageData(0,0,o,s).data;return{width:o,height:s,data:c,lowDetail:pm(o,s,c)}}catch{return null}}function Hi(e,n,t,r,o,s){let i=(r*n+t)*4,a=(s*n+o)*4;return Math.max(Math.abs((e[i]??0)-(e[a]??0)),Math.abs((e[i+1]??0)-(e[a+1]??0)),Math.abs((e[i+2]??0)-(e[a+2]??0)))}function pm(e,n,t){let r=Math.max(1,Math.floor(Math.max(e,n)/cm)),o=0,s=0,i=0;for(let a=0;a<n;a+=r)for(let c=0;c<e;c+=r){if(c+r<e){let l=Hi(t,e,c,a,c+r,a);i+=l,o++,l>Yi&&s++}if(a+r<n){let l=Hi(t,e,c,a,c,a+r);i+=l,o++,l>Yi&&s++}}return o>0&&s/o<=lm&&i/o<=um}function Ki(e,n,t){return Math.max(n,Math.min(t,e))}function qi(e,n){if(!Number.isFinite(e))return null;switch(n){case"repeat":return e-Math.floor(e);case"mirrored-repeat":{let t=Math.floor(e),r=e-t;return Math.abs(t%2)===1?1-r:r}default:return Math.max(0,Math.min(1,e))}}function hm(e,n,t){let r=n[0],o=1-n[1],s=qi(r,t?.s),i=qi(o,t?.t);if(s===null||i===null)return null;let a=Ki(Math.floor(s*e.width),0,e.width-1),l=(Ki(Math.floor(i*e.height),0,e.height-1)*e.width+a)*4;return{r:e.data[l]??0,g:e.data[l+1]??0,b:e.data[l+2]??0,a:e.data[l+3]??255}}function Xe(e,n,t,r,o,s){return[e[0]*r+n[0]*o+t[0]*s,e[1]*r+n[1]*o+t[1]*s]}function xm(e){let[n,t,r]=e;return[Xe(n,t,r,1/3,1/3,1/3),Xe(n,t,r,.8,.1,.1),Xe(n,t,r,.1,.8,.1),Xe(n,t,r,.1,.1,.8),Xe(n,t,r,.45,.45,.1),Xe(n,t,r,.45,.1,.45),Xe(n,t,r,.1,.45,.45)]}function bm(e){let[n,t,r]=e,o=xm(e);for(let s=1;s<gn;s++)for(let i=1;i<gn-s;i++){let a=gn-s-i;a<=0||o.push(Xe(n,t,r,s/gn,i/gn,a/gn))}return o}function ea(e){if(e.textureTriangles?.length)return e.textureTriangles;let n=e.uvs;if(!n||n.length!==e.vertices.length||n.length<3)return[];let t=[];for(let r=1;r+1<n.length;r++)t.push({uvs:[n[0],n[r],n[r+1]]});return t}function zr(e,n,t){return Math.abs(e.r-n.r)<=t&&Math.abs(e.g-n.g)<=t&&Math.abs(e.b-n.b)<=t&&Math.abs(e.a-n.a)<=t}function Rt(e){let n=r=>Math.round(Math.max(0,Math.min(255,r))).toString(16).padStart(2,"0");if(e.a>=255)return`#${n(e.r)}${n(e.g)}${n(e.b)}`;let t=Math.round(Math.max(0,Math.min(255,e.a))/255*1e3)/1e3;return`rgba(${Math.round(e.r)}, ${Math.round(e.g)}, ${Math.round(e.b)}, ${t})`}function na(e){if(!e)return null;let n=e.trim().toLowerCase(),t=n.match(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);if(t){let s=t[1],i=a=>{let c=s.length<=4?s[a]+s[a]:s.slice(a*2,a*2+2);return parseInt(c,16)};return{r:i(0),g:i(1),b:i(2),a:s.length===4||s.length===8?i(3):255}}let r=n.match(/^rgba?\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)(?:\s*,\s*([0-9.]+)\s*)?\)$/);if(!r)return null;let o=r[4]===void 0?255:Number(r[4])<=1?Number(r[4])*255:Number(r[4]);return{r:Number(r[1]),g:Number(r[2]),b:Number(r[3]),a:o}}function ym(){return{min:{r:255,g:255,b:255,a:255},max:{r:0,g:0,b:0,a:0},sum:{r:0,g:0,b:0,a:0},colors:new Map,count:0}}function ta(e){return`${e.r},${e.g},${e.b},${e.a}`}function Zi(e,n){e.min.r=Math.min(e.min.r,n.r),e.min.g=Math.min(e.min.g,n.g),e.min.b=Math.min(e.min.b,n.b),e.min.a=Math.min(e.min.a,n.a),e.max.r=Math.max(e.max.r,n.r),e.max.g=Math.max(e.max.g,n.g),e.max.b=Math.max(e.max.b,n.b),e.max.a=Math.max(e.max.a,n.a),e.sum.r+=n.r,e.sum.g+=n.g,e.sum.b+=n.b,e.sum.a+=n.a;let t=ta(n),r=e.colors.get(t);e.colors.set(t,{color:n,count:(r?.count??0)+1}),e.count++}function Qi(e){return{r:e.sum.r/e.count,g:e.sum.g/e.count,b:e.sum.b/e.count,a:e.sum.a/e.count}}function Pm(e){if(e.count<am||e.colors.size<=1||e.colors.size>sm)return null;let n=null;for(let o of e.colors.values())(!n||o.count>n.count)&&(n=o);if(!n)return null;let t=e.count-n.count,r=Math.max(1,Math.floor(e.count*im));return t<=r?n.color:null}function Am(e,n,t,r){let o=ea(e);if(o.length===0)return null;let s=ym(),i=e.textureAlphaMode==="opaque"?na(e.color):null;for(let a of o)for(let c of bm(a.uvs)){let l=hm(n,c,e.textureWrap);if(!l)return null;if(e.textureAlphaMode==="opaque"&&l.a===0&&e.color){i&&Zi(s,{...i,a:255});continue}e.textureAlphaMode==="opaque"&&(l.a=255),Zi(s,l)}if(s.count===0)return e.textureAlphaMode==="opaque"&&e.color?e.color:null;if(e.textureAlphaMode==="opaque"&&s.max.a===0&&e.color)return e.color;if(zr(s.min,s.max,t))return Rt(Qi(s));if(!r){let a=Pm(s);if(a)return Rt(a)}return r||!n.lowDetail||!zr(s.min,s.max,om)?null:Rt(Qi(s))}function ra(e,n){let{texture:t,textureWrap:r,textureAlphaMode:o,material:s,uvs:i,textureTriangles:a,...c}=e;return{...c,color:n}}function Mm(e,n){if(n.length<2)return e;let t=[],r=new Map;for(let l of n){let u=na(e[l]?.color);if(!u||u.a<255)continue;let f=ta(u);t.push({index:l,key:f});let d=r.get(f);d?d.count+=1:r.set(f,{color:u,count:1})}if(r.size<2)return e;let o=[],s=new Map,i=[...r.entries()].sort((l,u)=>u[1].count-l[1].count||l[0].localeCompare(u[0]));for(let[l,u]of i){let f=null;for(let d of o)if(zr(u.color,d,fm)){f=d;break}f||(f=u.color,o.push(f)),s.set(l,Rt(f))}let a=!1,c=e.slice();for(let l of t){let u=s.get(l.key),f=e[l.index];!u||!f||f.color===u||(c[l.index]={...f,color:u},a=!0)}return a?c:e}function Sm(e,n){let t=e.slice();for(let r=0;r<n.length;r++){let o=n[r],s=e[o.index];s&&(t[o.index]=ra(s,o.color))}return t}function oa(e){return e?e[Ji]:void 0}async function sa(e,n={}){if(n.enabled===!1)return null;let t=mm();if(!t)return null;let r=e.filter(c=>$r(c)&&ea(c).length>0);if(r.length===0)return null;let o=n.maxTexturePixels??tm,s=new Map;await Promise.all(Array.from(new Set(r.map(c=>$r(c)))).map(async c=>{s.set(c,await gm(c,t,o))}));let i=n.colorTolerance??rm,a=n.colorTolerance!==void 0;return{bake(c){let l=!1,u=[],f=c.map((d,m)=>{let g=$r(d);if(!g)return d;let h=s.get(g);if(!h)return d;let p=Am(d,h,i,a);return p?(l=!0,u.push(m),ra(d,p)):d});return l?{polygons:Mm(f,u),changed:!0,bakedIndices:u}:{polygons:c,changed:!1,bakedIndices:u}}}}async function Em(e,n={}){let t=await sa(e,n);return t?t.bake(e).polygons:e}async function Xr(e,n={}){let t=await sa(e.polygons,n);if(!t)return e;let r=t.bake(e.polygons);if(!r.changed)return e;let o=[];for(let a of r.bakedIndices){let l=r.polygons[a].color;l&&o.push({index:a,color:l})}let s=e.animation,i=s&&{...s,sample(a,c){return Sm(s.sample(a,c),o)}};if(i&&s){let a=s[ji];if(a){let c=new Map(o.map(f=>[f.index,f.color])),l=[],u={polygonCount:0,vertices:new Float64Array(0),colors:l,solidTriangles:!1};i[ji]=(f,d)=>{let m=a(f,d);if(!m)return m;l.length<m.polygonCount&&(l=new Array(m.polygonCount),u.colors=l);let g=m.textureFlags,h=m.solidTriangles===!0||!!g;for(let p=0;p<m.polygonCount;p++){let x=c.get(p);l[p]=x??m.colors?.[p],g?.[p]&&!x&&(h=!1)}return u.polygonCount=m.polygonCount,u.vertices=m.vertices,u.textureFlags=g,u.solidTriangles=h,u}}i[Ji]={source:s,bakedColorEntries:o}}return{...e,polygons:r.polygons,animation:i}}var ia=Symbol.for("polycss.animation.triangleFrameSource");function Tm(e,n){let t=e[n];if(!t)return null;let r=[];for(let o=0;o<t.vertices.length;o++)r.push(n,o);return{rest:t,originalIndex:n,sourceRefs:r}}function Cm(e){let n=$e(e);if(n.length>=e.length)return[];let t=new Map;e.forEach((o,s)=>t.set(o,s));let r=[];for(let o of n){let s=t.get(o);if(s===void 0)return[];let i=Tm(e,s);if(!i)return[];r.push(i)}return r}function Wr(e,n,t=!1){let r=new Array(n.length);for(let o=0;o<n.length;o++){let s=n[o],i=s.sourceRefs,a=new Array(i.length/2);for(let l=0,u=0;l<i.length;l+=2,u++){let d=e[i[l]]?.vertices[i[l+1]];if(!d)return e;let m=d[0],g=d[1],h=d[2];if(!Number.isFinite(m)||!Number.isFinite(g)||!Number.isFinite(h))return e;a[u]=[m,g,h]}let c=t?s.rest.color:e[i[0]??-1]?.color??s.rest.color;r[o]={...s.rest,vertices:a,color:c}}return r}function _m(e,n){let t=new Map;return n.forEach((r,o)=>{t.set(r,o)}),e.map((r,o)=>({...r,sourceRefs:r.sourceRefs.map((s,i)=>i%2===0?t.get(s)??-1:s)}))}function vm(e,n){if(e.length<n.length)return null;let t=new Array(n.length);for(let r=0;r<n.length;r++){let o=e[r],s=n[r];if(!o||o.vertices.length!==s.rest.vertices.length)return null;t[r]={...s.rest,vertices:o.vertices,color:s.rest.color}}return t}function Im(e){return!!(e.texture||e.material?.texture)}function Vm(e,n={}){if(!e.animation||e.polygons.length===0||n.meshResolution==="lossless")return e;let t=Cm(e.polygons);if(t.length===0||t.length>=e.polygons.length)return e;let r=oa(e.animation),o=r?.source??e.animation,s=t.map(b=>b.originalIndex),i=Wi(o,s),a=i??o,c=i?_m(t,s):t,l=t.map(b=>b.rest.color),u=t.every(b=>b.rest.vertices.length===3&&!Im(b.rest)),f=i&&!r&&t.length===s.length&&t.every((b,P)=>b.rest.vertices.length===3&&b.sourceRefs.length===6&&b.sourceRefs[0]===s[P]&&b.sourceRefs[2]===s[P]&&b.sourceRefs[4]===s[P]),d=a[ia],m=new Float64Array(t.length*9),g=l.slice(),h=new Array(t.length).fill(!1),p={polygonCount:t.length,vertices:m,colors:g,textureFlags:h,solidTriangles:u},x={...e.animation,sample(b,P){let y=a.sample(b,P);return f?y:i&&r?u&&t.length===s.length?vm(y,t)??Wr(y,c,!0):Wr(y,c,!0):Wr(y,c,!!r)}};return d&&u&&(x[ia]=(b,P)=>{let y=d(b,P);if(!y)return y;if(i)return y.polygonCount<t.length||y.vertices.length<t.length*9?null:(p.polygonCount=t.length,p.vertices=y.vertices,p.colors=r?l:y.colors,p.textureFlags=r?h:y.textureFlags,p.solidTriangles=r?u:y.solidTriangles===!0,p);m.length<t.length*9&&(m=new Float64Array(t.length*9),p.vertices=m),g.length<t.length&&(g=new Array(t.length),p.colors=g),h.length<t.length&&(h=new Array(t.length).fill(!1),p.textureFlags=h);let M=u&&y.solidTriangles!==!1;for(let S=0;S<c.length;S++){let T=c[S],E=T.sourceRefs;for(let _=0,w=0;_<E.length;_+=2,w++){let V=E[_],N=E[_+1];if(V<0||V>=y.polygonCount)return null;let F=V*9+N*3,k=S*9+w*3;if(F+2>=y.vertices.length)return null;m[k]=y.vertices[F],m[k+1]=y.vertices[F+1],m[k+2]=y.vertices[F+2]}let A=E[0],C=!!y.textureFlags?.[A];h[S]=C,g[S]=r?T.rest.color:y.colors?.[A]??T.rest.color,!r&&C&&(M=!1)}return p.polygonCount=t.length,p.vertices=m,p.colors=g,p.textureFlags=h,p.solidTriangles=r?u:M,p}),{...e,polygons:t.map(b=>b.rest),animation:x,metadata:{...e.metadata,triangleCount:t.length}}}var Om=/^[0-9A-Fa-f]{6}$/,Rm=["#3b82f6","#ef4444","#22c55e","#eab308","#a855f7","#06b6d4","#f97316","#ec4899"];function jr(e,n){let t=n?.targetSize??60,r=n?.gridShift??1,o=n?.defaultColor??"#888888",s=n?.palette??Rm,i=n?.materialColors??{},a=n?.materialTextures??{},c=[],l=[],u=[],f=[],d=new Map,m=o,g,h=n?.includeObjects?new Set(n.includeObjects):null,p=n?.excludeObjects?new Set(n.excludeObjects):null,x=null,b=()=>x===null?h===null:!(h&&!h.has(x)||p&&p.has(x)),P=R=>R in i?i[R]:Om.test(R)?`#${R}`:(d.has(R)||(d.set(R,s[f.length%s.length]),f.push(R)),d.get(R)),y=(R,B)=>{let z=parseInt(R,10);return Number.isFinite(z)?z<0?B+z:z-1:NaN},M=e.split(`
2
- `);for(let R of M)if(!(R.length===0||R.charCodeAt(0)===35)){if(R.startsWith("v ")){let B=R.trim().split(/\s+/);c.push([parseFloat(B[1]),parseFloat(B[2]),parseFloat(B[3])])}else if(R.startsWith("vt ")){let B=R.trim().split(/\s+/);l.push([parseFloat(B[1]),parseFloat(B[2])])}else if(R.startsWith("o "))x=R.trim().slice(2).trim();else if(R.startsWith("usemtl ")){let B=R.trim().split(/\s+/)[1];m=P(B),g=a[B]}else if(R.startsWith("f ")){if(!b())continue;let B=R.trim().split(/\s+/).slice(1),z=[],ee=[];for(let Q of B){let Z=Q.split("/");z.push(y(Z[0],c.length));let ne=Z[1];if(ne&&ne.length>0){let j=y(ne,l.length);ee.push(Number.isFinite(j)?j:null)}else ee.push(null)}u.push({idx:z,uvIdx:ee,color:m,texture:g})}}if(c.length===0||u.length===0)return Lm(f,e.length);let S=new Set;for(let R of u)for(let B of R.idx)S.add(B);let T=1/0,E=1/0,A=1/0,C=-1/0,_=-1/0,w=-1/0;for(let R of S){let B=c[R];B&&(B[0]<T&&(T=B[0]),B[0]>C&&(C=B[0]),B[1]<E&&(E=B[1]),B[1]>_&&(_=B[1]),B[2]<A&&(A=B[2]),B[2]>w&&(w=B[2]))}let V=Math.max(C-T,_-E,w-A),N=V>0?t/V:1,F=R=>Math.round(R*1e3)/1e3,k=c.map(([R,B,z])=>[F((z-A)*N+r),F((R-T)*N+r),F((B-E)*N+r)]),$=[];for(let{idx:R,uvIdx:B,color:z,texture:ee}of u)for(let Q=1;Q<R.length-1;Q++){let Z=R[0],ne=R[Q],j=R[Q+1],D=k[Z],I=k[ne],U=k[j];if(!D||!I||!U||D[0]===I[0]&&D[1]===I[1]&&D[2]===I[2]||D[0]===U[0]&&D[1]===U[1]&&D[2]===U[2]||I[0]===U[0]&&I[1]===U[1]&&I[2]===U[2])continue;let G;if(ee){let v=B[0],O=B[Q],L=B[Q+1];if(v!=null&&O!=null&&L!=null){let X=l[v],H=l[O],Y=l[L];X&&H&&Y&&(G=[X,H,Y])}}let q={vertices:[D,I,U],color:z};ee&&(q.texture=ee),G&&(q.uvs=G),$.push(q)}return{polygons:$,objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:$.length,materials:f,sourceBytes:e.length}}}function Lm(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:0,materials:e,sourceBytes:n}}}var Yr=e=>Math.round(Math.max(0,Math.min(1,e))*255).toString(16).padStart(2,"0");function Hr(e){let n={},t={},r=null;for(let o of e.split(`
3
- `)){let s=o.trim();if(!(s.length===0||s.charCodeAt(0)===35)){if(s.startsWith("newmtl ")){r=s.slice(7).trim();continue}if(r){if(s.startsWith("Kd ")){let i=s.split(/\s+/),a=parseFloat(i[1]),c=parseFloat(i[2]),l=parseFloat(i[3]);Number.isFinite(a)&&Number.isFinite(c)&&Number.isFinite(l)&&(n[r]=`#${Yr(a)}${Yr(c)}${Yr(l)}`)}else if(s.startsWith("map_Kd ")){let i=s.split(/\s+/),a=i[i.length-1]?.replace(/\\+/g,"/");a&&a!=="map_Kd"&&(t[r]=a)}}}}return{colors:n,textures:t}}var wm=[0,4294967295,4291624959,4288282623,4284940287,4281597951,4278255615,4294954239,4291611903,4288269567,4284927231,4281584895,4278242559,4294941183,4291598847,4288256511,4284914175,4281571839,4278229503,4294928127,4291585791,4288243455,4284901119,4281558783,4278216447,4294915071,4291572735,4288230399,4284888063,4281545727,4278203391,4294902015,4291559679,4288217343,4284875007,4281532671,4278190335,4294967244,4291624908,4288282572,4284940236,4281597900,4278255564,4294954188,4291611852,4288269516,4284927180,4281584844,4278242508,4294941132,4291598796,4288256460,4284914124,4281571788,4278229452,4294928076,4291585740,4288243404,4284901068,4281558732,4278216396,4294915020,4291572684,4288230348,4284888012,4281545676,4278203340,4294901964,4291559628,4288217292,4284874956,4281532620,4278190284,4294967193,4291624857,4288282521,4284940185,4281597849,4278255513,4294954137,4291611801,4288269465,4284927129,4281584793,4278242457,4294941081,4291598745,4288256409,4284914073,4281571737,4278229401,4294928025,4291585689,4288243353,4284901017,4281558681,4278216345,4294914969,4291572633,4288230297,4284887961,4281545625,4278203289,4294901913,4291559577,4288217241,4284874905,4281532569,4278190233,4294967142,4291624806,4288282470,4284940134,4281597798,4278255462,4294954086,4291611750,4288269414,4284927078,4281584742,4278242406,4294941030,4291598694,4288256358,4284914022,4281571686,4278229350,4294927974,4291585638,4288243302,4284900966,4281558630,4278216294,4294914918,4291572582,4288230246,4284887910,4281545574,4278203238,4294901862,4291559526,4288217190,4284874854,4281532518,4278190182,4294967091,4291624755,4288282419,4284940083,4281597747,4278255411,4294954035,4291611699,4288269363,4284927027,4281584691,4278242355,4294940979,4291598643,4288256307,4284913971,4281571635,4278229299,4294927923,4291585587,4288243251,4284900915,4281558579,4278216243,4294914867,4291572531,4288230195,4284887859,4281545523,4278203187,4294901811,4291559475,4288217139,4284874803,4281532467,4278190131,4294967040,4291624704,4288282368,4284940032,4281597696,4278255360,4294953984,4291611648,4288269312,4284926976,4281584640,4278242304,4294940928,4291598592,4288256256,4284913920,4281571584,4278229248,4294927872,4291585536,4288243200,4284900864,4281558528,4278216192,4294914816,4291572480,4288230144,4284887808,4281545472,4278203136,4294901760,4291559424,4288217088,4284874752,4281532416,4278190318,4278190301,4278190267,4278190250,4278190216,4278190199,4278190165,4278190148,4278190114,4278190097,4278251008,4278246656,4278237952,4278233600,4278224896,4278220544,4278211840,4278207488,4278198784,4278194432,4293787648,4292673536,4290445312,4289331200,4287102976,4285988864,4283760640,4282646528,4280418304,4279304192,4293848814,4292730333,4290493371,4289374890,4287137928,4286019447,4283782485,4282664004,4280427042,4279308561];function Nm(e){let n=e>>0&255,t=e>>8&255,r=e>>16&255;return[n,t,r]}function Kr(e){return(e&255).toString(16).padStart(2,"0")}function la(e,n,t){return`#${Kr(e)}${Kr(n)}${Kr(t)}`}function Dm(e,n,t,r){if(r>=255)return la(e,n,t);let o=Math.round(Math.max(0,Math.min(255,r))/255*1e3)/1e3;return`rgba(${e}, ${n}, ${t}, ${o})`}function ua(e){let n=e.match(/^#([0-9a-f]{6})$/i);if(n){let r=n[1];return{rgb:[parseInt(r.slice(0,2),16),parseInt(r.slice(2,4),16),parseInt(r.slice(4,6),16)],alpha:1}}let t=e.match(/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/i);return t?{rgb:[Number(t[1]),Number(t[2]),Number(t[3])],alpha:Number(t[4])}:null}function fa(e,n){return Math.hypot(e.rgb[0]-n.rgb[0],e.rgb[1]-n.rgb[1],e.rgb[2]-n.rgb[2])}function aa(e){let n=e.rgb[0]/255,t=e.rgb[1]/255,r=e.rgb[2]/255,o=Math.max(n,t,r),s=Math.min(n,t,r),i=o-s,a=0;return i!==0&&(o===n?a=60*((t-r)/i%6):o===t?a=60*((r-n)/i+2):a=60*((n-t)/i+4)),a<0&&(a+=360),{hue:a,saturation:o===0?0:i/o,value:o}}function Fm(e,n){let t=Math.abs(e-n)%360;return t>180?360-t:t}function ma(e,n){if(e.alpha<1||n.alpha<1)return!1;let t=aa(e),r=aa(n),o=t.saturation<.08,s=r.saturation<.08;if(o||s)return o===s;let i=Math.min(t.value,r.value)<.16?35:24;return Fm(t.hue,r.hue)<=i}function Bm(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return null;let r=new Map;for(let a of e.keys()){let c=ua(a);c&&r.set(a,c)}let o=[],s=new Map,i=Array.from(e.entries()).sort((a,c)=>c[1]-a[1]||a[0].localeCompare(c[0]));for(let[a]of i){let c=r.get(a);if(!c){s.set(a,a);continue}let l=null;for(let u of o){if(!ma(c,u.parsed))continue;let f=fa(c,u.parsed);f>t||(!l||f<l.distance)&&(l={color:u.color,distance:f})}l?s.set(a,l.color):(o.push({color:a,parsed:c}),s.set(a,a))}return s}var km=[[1,0],[-1,0],[0,1],[0,-1]];function da(e,n){return`${e},${n}`}function Um(e){let n=e.indexOf(",");return[Number(e.slice(0,n)),Number(e.slice(n+1))]}function Gm(e,n,t){return e<=4||(n===1||t===1)&&e<=12?!0:Math.min(n,t)<=2&&Math.max(n,t)>=4&&e<=18}function $m(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return;let r=new Map,o=s=>(r.has(s)||r.set(s,ua(s)),r.get(s)??null);for(let s of e.values()){if(s.size<3)continue;let i=new Map;for(let l of s.values())i.set(l,(i.get(l)??0)+1);let a=new Set,c=[];for(let[l,u]of s){if(a.has(l))continue;let f=[l],d=[],m=new Map,g=1/0,h=-1/0,p=1/0,x=-1/0;for(a.add(l);f.length>0;){let S=f.pop();d.push(S);let[T,E]=Um(S);g=Math.min(g,T),h=Math.max(h,T),p=Math.min(p,E),x=Math.max(x,E);for(let[A,C]of km){let _=da(T+A,E+C),w=s.get(_);if(w){if(w===u){a.has(_)||(a.add(_),f.push(_));continue}m.set(w,(m.get(w)??0)+1)}}}let b=h-g+1,P=x-p+1;if(!Gm(d.length,b,P)||m.size===0)continue;let y=o(u);if(!y)continue;let M=null;for(let[S,T]of m){let E=i.get(S)??0;if(E<=d.length)continue;let A=o(S);if(!A||!ma(y,A))continue;let C=fa(y,A);if(C>t)continue;let _={color:S,contacts:T,distance:C,count:E};(!M||_.contacts>M.contacts||_.contacts===M.contacts&&_.count>M.count||_.contacts===M.contacts&&_.count===M.count&&_.distance<M.distance||_.contacts===M.contacts&&_.count===M.count&&_.distance===M.distance&&_.color.localeCompare(M.color)<0)&&(M=_)}if(M)for(let S of d)c.push([S,M.color])}for(let[l,u]of c)s.set(l,u)}}var zm=542658390;function qr(e,n){let t=n?.targetSize??60,r=n?.gridShift??0,o=e.byteLength;if(e.byteLength<8)return pn(o,["parseVox: buffer too small to be a valid .vox file"]);let s=new DataView(e);if(s.getUint32(0,!0)!==zm)return pn(o,["parseVox: not a .vox file (bad magic)"]);if(e.byteLength<20)return pn(o,["parseVox: buffer too small for MAIN chunk"]);if(ca(s,8)!=="MAIN")return pn(o,["parseVox: expected MAIN chunk at offset 8"]);let l=20+s.getUint32(12,!0),u=l+s.getUint32(16,!0),f=[],d=[],m=null;for(;l<u&&l+12<=e.byteLength;){let D=ca(s,l),I=s.getUint32(l+4,!0),U=s.getUint32(l+8,!0),G=l+12,q=G+I+U;if(D==="SIZE"){if(I>=12&&G+12<=e.byteLength){let v=s.getUint32(G,!0),O=s.getUint32(G+4,!0),L=s.getUint32(G+8,!0);f.push({sx:v,sy:O,sz:L})}}else if(D==="XYZI"){if(I>=4&&G+4<=e.byteLength){let v=s.getUint32(G,!0),O=[],L=Math.min(v,Math.floor((I-4)/4));for(let X=0;X<L;X++){let H=G+4+X*4;O.push({x:s.getUint8(H),y:s.getUint8(H+1),z:s.getUint8(H+2),colorIndex:s.getUint8(H+3)})}d.push(O)}}else if(D==="RGBA"&&I>=1024&&G+1024<=e.byteLength){m=[];for(let v=0;v<256;v++){let O=G+v*4,L=s.getUint8(O),X=s.getUint8(O+1),H=s.getUint8(O+2),Y=s.getUint8(O+3);m.push(Dm(L,X,H,Y))}}l=q}let g=[],h=new Set;for(let D of d)for(let I of D){if(I.colorIndex===0)continue;let U=`${I.x},${I.y},${I.z}`;h.has(U)||(h.add(U),g.push(I))}if(g.length===0)return pn(o,[]);let p=new Map,x=D=>{let I=p.get(D);if(I)return I;let U=D-1,G;if(m!==null)G=m[U]??"#888888";else{let q=wm[D]??0,[v,O,L]=Nm(q);G=la(v,O,L)}return p.set(D,G),G},b=new Map;for(let D of g){let I=x(D.colorIndex);b.set(I,(b.get(I)??0)+1)}let P=Bm(b,n?.paletteMergeDistance),y=D=>{let I=x(D);return P?.get(I)??I},M=(D,I,U)=>h.has(`${D},${I},${U}`),S=new Map,T=(D,I,U,G,q)=>{let v=`${D}:${I}`,O=S.get(v);O||(O=new Map,S.set(v,O)),O.set(da(U,G),q)};for(let D of g){let{x:I,y:U,z:G}=D,q=y(D.colorIndex);M(I+1,U,G)||T(0,I+1,U,G,q),M(I-1,U,G)||T(1,I,U,G,q),M(I,U+1,G)||T(2,U+1,I,G,q),M(I,U-1,G)||T(3,U,I,G,q),M(I,U,G+1)||T(4,G+1,I,U,q),M(I,U,G-1)||T(5,G,I,U,q)}$m(S,n?.colorRegionMergeDistance);let E=new Map;for(let[D,I]of S)for(let[U,G]of I){let q=`${D}:${G}`,v=E.get(q);v||(v=new Set,E.set(q,v)),v.add(U)}function A(D){let I=new Set,U=[],G=1/0,q=-1/0,v=1/0,O=-1/0;for(let L of D){let[X,H]=L.split(","),Y=+X,K=+H;Y<G&&(G=Y),Y>q&&(q=Y),K<v&&(v=K),K>O&&(O=K)}for(let L=v;L<=O;L++)for(let X=G;X<=q;X++){let H=`${X},${L}`;if(!D.has(H)||I.has(H))continue;let Y=1;for(;X+Y<=q;){let te=`${X+Y},${L}`;if(!D.has(te)||I.has(te))break;Y++}let K=1;e:for(;L+K<=O;){for(let te=0;te<Y;te++){let se=`${X+te},${L+K}`;if(!D.has(se)||I.has(se))break e}K++}for(let te=0;te<K;te++)for(let se=0;se<Y;se++)I.add(`${X+se},${L+te}`);U.push({u:X,v:L,w:Y,h:K})}return U}let C=(D,I,U,G,q,v)=>{let O=U+q,L=G+v;switch(D){case 0:return[[I,U,G],[I,O,G],[I,O,L],[I,U,L]];case 1:return[[I,O,G],[I,U,G],[I,U,L],[I,O,L]];case 2:return[[U,I,G],[U,I,L],[O,I,L],[O,I,G]];case 3:return[[O,I,G],[O,I,L],[U,I,L],[U,I,G]];case 4:return[[U,G,I],[O,G,I],[O,L,I],[U,L,I]];default:return[[U,L,I],[O,L,I],[O,G,I],[U,G,I]]}},_=[];for(let[D,I]of E){let U=D.indexOf(":"),G=D.indexOf(":",U+1),q=+D.slice(0,U),v=+D.slice(U+1,G),O=D.slice(G+1);for(let{u:L,v:X,w:H,h:Y}of A(I))_.push({vertices:C(q,v,L,X,H,Y),color:O})}if(_.length===0)return pn(o,[]);let w=1/0,V=1/0,N=1/0,F=-1/0,k=-1/0,$=-1/0;for(let D of _)for(let I of D.vertices)I[0]<w&&(w=I[0]),I[0]>F&&(F=I[0]),I[1]<V&&(V=I[1]),I[1]>k&&(k=I[1]),I[2]<N&&(N=I[2]),I[2]>$&&($=I[2]);let R=Math.max(F-w,k-V,$-N),B=R>0?t/R:1,z=B*50,ee=Number.isFinite(z)&&z>0?Math.max(1,Math.round(z))/50:B,Q={kind:"magica-vox",cells:g.map(D=>({x:D.x-w,y:D.y-V,z:D.z-N,color:y(D.colorIndex)})),rows:Math.max(0,F-w),cols:Math.max(0,k-V),depth:Math.max(0,$-N),scale:ee,gridShift:r,sourceBytes:o},Z=D=>Math.round(D*1e3)/1e3,ne=D=>[Z((D[0]-w)*ee+r),Z((D[1]-V)*ee+r),Z((D[2]-N)*ee+r)],j=_.map(({vertices:D,color:I})=>({vertices:D.map(ne),color:I}));return{polygons:j,voxelSource:Q,objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:j.length,sourceBytes:o,voxelCount:g.length}}}function ca(e,n){return String.fromCharCode(e.getUint8(n))+String.fromCharCode(e.getUint8(n+1))+String.fromCharCode(e.getUint8(n+2))+String.fromCharCode(e.getUint8(n+3))}function pn(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:n,metadata:{triangleCount:0,sourceBytes:e}}}var en="loadMesh";function Zr(e,n){if(e.voxelSource)return e;let t=Er(e.polygons,{meshResolution:n?.meshResolution});return t===e.polygons?e:{...e,polygons:t}}async function ga(e,n){let t=n?.solidTextureSamples;return t===!1?e:Xr(e,typeof t=="object"?t:void 0)}function Xm(e){let n=e.split("?")[0].split("#")[0],t=n.lastIndexOf(".");return t<0?"":n.slice(t+1).toLowerCase()}async function Wm(e,n){let t=Xm(e);if(t==="mtl")throw new Error(`${en}: .mtl is a material file, not a mesh \u2014 use parseMtl directly`);let r=globalThis.fetch;if(!r)throw new Error(`${en}: no fetch() in this environment`);let o=n?.baseUrl??e;if(t==="obj"){let s=await r(e);if(!s.ok)throw new Error(`${en}: ${e} \u2192 ${s.status}`);let i=await s.text(),a=n?.objOptions;if(n?.mtlUrl){let l=await r(n.mtlUrl);if(!l.ok)throw new Error(`${en}: ${n.mtlUrl} \u2192 ${l.status}`);let u=await l.text(),{colors:f,textures:d}=Hr(u),m={},g=globalThis.URL,h=globalThis.document?.baseURI,p=n.mtlUrl;if(g&&h)try{p=new g(n.mtlUrl,h).toString()}catch{}for(let[x,b]of Object.entries(d)){if(g)try{m[x]=new g(b,p).toString();continue}catch{}let P=n.mtlUrl.lastIndexOf("/"),y=P>=0?n.mtlUrl.slice(0,P+1):"";m[x]=b.startsWith("/")||/^https?:\/\//.test(b)?b:y+b}a={...a??{},materialColors:{...f,...a?.materialColors??{}},materialTextures:{...m,...a?.materialTextures??{}}}}let c=jr(i,a);return Zr(await ga(c,n),n)}if(t==="glb"||t==="gltf"){let s=await r(e);if(!s.ok)throw new Error(`${en}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer(),a=Gr(i,{baseUrl:o,...n?.gltfOptions??{}});return Zr(await ga(a,n),n)}if(t==="vox"){let s=await r(e);if(!s.ok)throw new Error(`${en}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return Zr(qr(i,n?.voxOptions),n)}throw new Error(`${en}: unsupported extension ".${t}" (supported: obj, glb, gltf, vox)`)}var De=50,hn=[.4,-.7,.59],xn="#ffffff",bn=1,yn="#ffffff",Pn=.4,We=4096,Lt=1,Qr=.1,Jr=1,wt=We*We,eo=wt*3,Yn=2048,no=4*1024*1024,to=16*1024*1024,ro=.995,oo=512,jm=12,ie=.001,re=1e-9,so=1e-4,io=.1,Nt=.01,Ym=.75,Hm=1,Km=250,qm=1.5,Dt=.75,Fe=3,ao=2,Ft=4,co=[1,10,100,1e3,1e4,1e5,1e6],An=64,lo=32,uo=96,Zm="polycss-corner-triangle",Qm="polycss-large-border-triangle",Ce=64,fo=128,Bt=50,kt=1e-7,Be=16,Hn=.9,_e=.75,Ut=.2,mo=.05,go=256,po=.6,Jm=1.5;function ue(e,n){if(Object.is(e,0)||Object.is(e,-0))return"0";if(e===1)return"1";if(e===-1)return"-1";let t=co[n]??10**n,r=Math.round(e*t)/t;return Object.is(r,0)||Object.is(r,-0)?"0":String(r)}function ed(e,n=Ft){let t=ue(e,n);return t==="0"?"0":`${t}px`}function je(e,n=Fe){if(e.length===0)return"";let t=ue(e[0],n);for(let r=1;r<e.length;r++)t+=`,${ue(e[r],n)}`;return t}function nd(e,n,t,r,o=Fe){return ho(e[0],e[1],e[2],n[0],n[1],n[2],t[0],t[1],t[2],r[0],r[1],r[2],o)}function ho(e,n,t,r,o,s,i,a,c,l,u,f,d=Fe){if(d===3){let m=Math.round(e*1e3)/1e3||0,g=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,p=Math.round(r*1e3)/1e3||0,x=Math.round(o*1e3)/1e3||0,b=Math.round(s*1e3)/1e3||0,P=Math.round(i*1e3)/1e3||0,y=Math.round(a*1e3)/1e3||0,M=Math.round(c*1e3)/1e3||0,S=Math.round(l*1e3)/1e3||0,T=Math.round(u*1e3)/1e3||0,E=Math.round(f*1e3)/1e3||0;return`${m},${g},${h},0,${p},${x},${b},0,${P},${y},${M},0,${S},${T},${E},1`}return`${ue(e,d)},${ue(n,d)},${ue(t,d)},0,${ue(r,d)},${ue(o,d)},${ue(s,d)},0,${ue(i,d)},${ue(a,d)},${ue(c,d)},0,${ue(l,d)},${ue(u,d)},${ue(f,d)},1`}function xo(e,n,t,r,o,s,i,a,c,l,u,f,d=Fe){if(d!==3)return`matrix3d(${ho(e,n,t,r,o,s,i,a,c,l,u,f,d)})`;let m=Math.round(e*1e3)/1e3||0,g=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,p=Math.round(r*1e3)/1e3||0,x=Math.round(o*1e3)/1e3||0,b=Math.round(s*1e3)/1e3||0,P=Math.round(i*1e3)/1e3||0,y=Math.round(a*1e3)/1e3||0,M=Math.round(c*1e3)/1e3||0,S=Math.round(l*1e3)/1e3||0,T=Math.round(u*1e3)/1e3||0,E=Math.round(f*1e3)/1e3||0;return`matrix3d(${m},${g},${h},0,${p},${x},${b},0,${P},${y},${M},0,${S},${T},${E},1)`}function Kn(e,n,t,r=0,o=0){let s=e.matrix.split(",").map(d=>Number(d));if(s.length!==16||s.some(d=>!Number.isFinite(d)))return e.matrix;let i=s[0],a=s[1],c=s[2],l=s[4],u=s[5],f=s[6];return s[0]*=n,s[1]*=n,s[2]*=n,s[4]*=t,s[5]*=t,s[6]*=t,s[12]+=r*i+o*l,s[13]+=r*a+o*u,s[14]+=r*c+o*f,je(s)}function td(e,n){return Kn(e,n.width/Be,n.height/Be,n.minX,n.minY)}function pa(e){let n=e.seamBleedInsets??{left:0,right:0,top:0,bottom:0},t=e.canvasW||1,r=e.canvasH||1;return Kn(e,(t+n.left+n.right)/An,(r+n.top+n.bottom)/An,-n.left,-n.top)}function bo(e,n){let t=e.matrix.split(",").map(r=>Number(r));return t.length!==16||t.some(r=>!Number.isFinite(r))?e.canonicalMatrix:(t[0]*=e.canvasW/n,t[1]*=e.canvasW/n,t[2]*=e.canvasW/n,t[4]*=e.canvasH/n,t[5]*=e.canvasH/n,t[6]*=e.canvasH/n,je(t))}function Ye(e,n=ao){let t=ue(e,n);return Number(t)===0?"0":`${t}%`}function rd(e,n=Fe){return`matrix3d(${e.split(",").map(t=>{let r=Number(t.trim());return Number.isFinite(r)?ue(r,n):t.trim()}).join(",")})`}function od(e,n=Ft){let t=ue(e,n);return Number(t)===0||Object.is(Number(t),-0)?"0":`${t}px`}function sd(e){return`matrix3d(${pa(e)})`}function He(e,n,t){return e.map(r=>[r[1]*n,r[0]*n,r[2]*t])}function nn(e){if(e.length<3)return null;let n=e[0],t=0,r=0,o=0;for(let i=1;i+1<e.length;i++){let a=e[i],c=e[i+1],l=[a[0]-n[0],a[1]-n[1],a[2]-n[2]],u=[c[0]-n[0],c[1]-n[1],c[2]-n[2]];t-=l[1]*u[2]-l[2]*u[1],r-=l[2]*u[0]-l[0]*u[2],o-=l[0]*u[1]-l[1]*u[0]}let s=Math.hypot(t,r,o);return s<=re?null:(t/=s,r/=s,o/=s,[t,r,o])}function qn(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let r=e[t],o=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(o[0]-r[0])*(s[1]-o[1])-(o[1]-r[1])*(s[0]-o[0]);if(Math.abs(i)<=re)return!1;let a=Math.sign(i);if(n===0)n=a;else if(a!==n)return!1}return!0}function yo(e){let n=0;for(let t=0;t<e.length;t++){let r=e[t],o=e[(t+1)%e.length];n+=r[0]*o[1]-r[1]*o[0]}return n/2}function Po(e,n,t,r){let o=n[0]-e[0],s=n[1]-e[1],i=r[0]-t[0],a=r[1]-t[1],c=o*a-s*i;if(Math.abs(c)<=re)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*a-u*i)/c;return[e[0]+f*o,e[1]+f*s]}function id(e,n,t,r,o,s,i,a){let c=t-e,l=r-n,u=i-o,f=a-s,d=c*f-l*u;if(Math.abs(d)<=re)return null;let m=o-e,g=s-n,h=(m*f-g*u)/d;return[e+h*c,n+h*l]}function Mn(e,n){if(e.length<6||n<=0)return e;let t=0,r=0,o=e.length/2;for(let i=0;i<e.length;i+=2)t+=e[i],r+=e[i+1];t/=o,r/=o;let s=e.slice();for(let i=0;i<s.length;i+=2){let a=s[i]-t,c=s[i+1]-r,l=Math.hypot(a,c);l<=ie||(s[i]+=a/l*n,s[i+1]+=c/l*n)}return s}function Sn(e,n){if(e.length<6||e.length%2!==0||n<=0)return e;let t=[];for(let c=0;c<e.length;c+=2)t.push([e[c],e[c+1]]);if(!qn(t))return Mn(e,n);let r=yo(t);if(Math.abs(r)<=re)return Mn(e,n);let o=r>0?1:-1,s=[];for(let c=0;c<t.length;c++){let l=t[c],u=t[(c+1)%t.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=re)return Mn(e,n);let g=o*(d/m)*n,h=o*(-f/m)*n;s.push({a:[l[0]+g,l[1]+h],b:[u[0]+g,u[1]+h]})}let i=[],a=Math.max(2,n*4);for(let c=0;c<t.length;c++){let l=s[(c+t.length-1)%t.length],u=s[c],f=Po(l.a,l.b,u.a,u.b);if(!f)return Mn(e,n);let d=t[c],m=f[0]-d[0],g=f[1]-d[1],h=Math.hypot(m,g);h>a?i.push(d[0]+m/h*a,d[1]+g/h*a):i.push(f[0],f[1])}return i}function Zn(e,n){if(e.length<6||e.length%2!==0)return e;let t=e.length/2;if(n.length!==t)return e;let r=n.map(f=>typeof f=="number"&&Number.isFinite(f)?Math.max(0,f):0),o=Math.max(0,...r);if(o<=0)return e;if(r.every(f=>Math.abs(f-r[0])<=re))return Sn(e,r[0]);let s=[];for(let f=0;f<e.length;f+=2)s.push([e[f],e[f+1]]);if(!qn(s))return e;let i=yo(s);if(Math.abs(i)<=re)return e;let a=i>0?1:-1,c=[];for(let f=0;f<s.length;f++){let d=s[f],m=s[(f+1)%s.length],g=m[0]-d[0],h=m[1]-d[1],p=Math.hypot(g,h);if(p<=re)return e;let x=r[f],b=a*(h/p)*x,P=a*(-g/p)*x;c.push({a:[d[0]+b,d[1]+P],b:[m[0]+b,m[1]+P]})}let l=[],u=Math.max(2,o*4);for(let f=0;f<s.length;f++){let d=c[(f+s.length-1)%s.length],m=c[f],g=Po(d.a,d.b,m.a,m.b);if(!g)return e;let h=s[f],p=g[0]-h[0],x=g[1]-h[1],b=Math.hypot(p,x);b>u?l.push(h[0]+p/b*u,h[1]+x/b*u):l.push(g[0],g[1])}return l}function Gt(e,n,t,r,o,s,i){if(i<=0)return[e,n,t,r,o,s];let a=(e*r-n*t+t*s-r*o+o*n-s*e)/2,c=()=>Mn([e,n,t,r,o,s],i);if(Math.abs(a)<=re)return c();let l=a>0?1:-1,u=t-e,f=r-n,d=Math.sqrt(u*u+f*f),m=o-t,g=s-r,h=Math.sqrt(m*m+g*g),p=e-o,x=n-s,b=Math.sqrt(p*p+x*x);if(d<=re||h<=re||b<=re)return c();let P=l*(f/d)*i,y=l*(-u/d)*i,M=e+P,S=n+y,T=t+P,E=r+y,A=l*(g/h)*i,C=l*(-m/h)*i,_=t+A,w=r+C,V=o+A,N=s+C,F=l*(x/b)*i,k=l*(-p/b)*i,$=o+F,R=s+k,B=e+F,z=n+k,ee=B-$,Q=z-R,Z=T-M,ne=E-S,j=ee*ne-Q*Z;if(Math.abs(j)<=re)return c();let D=M-$,I=S-R,U=(D*ne-I*Z)/j,G=$+U*ee,q=R+U*Q,v=T-M,O=E-S,L=V-_,X=N-w,H=v*X-O*L;if(Math.abs(H)<=re)return c();let Y=_-M,K=w-S,te=(Y*X-K*L)/H,se=M+te*v,he=S+te*O,Se=V-_,le=N-w,me=B-$,pe=z-R,ve=Se*pe-le*me;if(Math.abs(ve)<=re)return c();let ye=$-_,Ee=R-w,xe=(ye*pe-Ee*me)/ve,Pe=_+xe*Se,Ae=w+xe*le,ce=Math.max(2,i*4),fe=G-e,Me=q-n,ae=Math.sqrt(fe*fe+Me*Me);ae>ce&&(G=e+fe/ae*ce,q=n+Me/ae*ce);let de=se-t,W=he-r,J=Math.sqrt(de*de+W*W);J>ce&&(se=t+de/J*ce,he=r+W/J*ce);let oe=Pe-o,be=Ae-s,Te=Math.sqrt(oe*oe+be*be);return Te>ce&&(Pe=o+oe/Te*ce,Ae=s+be/Te*ce),[G,q,se,he,Pe,Ae]}function Ao(e,n,t,r){let o=e+n;if(r<=0||t<=re||o<=re||!Number.isFinite(e+n+t+r))return Gt(e,0,0,t,o,t,r);let s=Math.sqrt(e*e+t*t),i=Math.sqrt(n*n+t*t);if(s<=re||i<=re)return Gt(e,0,0,t,o,t,r);let a=-r*t/s,c=-r*e/s,l=r*t/i,u=-r*n/i,f=e+a,d=c,m=o+l,g=t+u,h=-t*o;if(Math.abs(h)<=re)return Gt(e,0,0,t,o,t,r);let p=f-m,x=d-g,b=(p*t+x*e)/h,P=m-b*n,y=g-b*t,M=-r*(e+s)/t,S=t+r,T=o+r*(n+i)/t,E=S,A=Math.max(2,r*4),C=P-e,_=y,w=Math.sqrt(C*C+_*_);w>A&&(P=e+C/w*A,y=_/w*A);let V=Math.sqrt(M*M+r*r);V>A&&(M=M/V*A,S=t+r/V*A);let N=T-o,F=Math.sqrt(N*N+r*r);return F>A&&(T=o+N/F*A,E=t+r/F*A),[P,y,M,S,T,E]}function ha(e,n){if(e.screenPts.length<6||n.vertices.length<3)return null;let t=e.tileSize,r=e.layerElevation,o=He(n.vertices,t,r),s=nn(o);if(!s)return null;let i=e.screenPts[0],a=e.screenPts[1],c=e.screenPts[2],l=e.screenPts[3],u=e.screenPts[4],f=e.screenPts[5],d=c-i,m=l-a,g=u-i,h=f-a,p=d*h-m*g;if(Math.abs(p)<=re)return null;let x=o[0],b=o[1],P=o[2],y=[b[0]-x[0],b[1]-x[1],b[2]-x[2]],M=[P[0]-x[0],P[1]-x[1],P[2]-x[2]],S=[(y[0]*h-m*M[0])/p,(y[1]*h-m*M[1])/p,(y[2]*h-m*M[2])/p],T=[(d*M[0]-y[0]*g)/p,(d*M[1]-y[1]*g)/p,(d*M[2]-y[2]*g)/p],E=x[0]-S[0]*i-T[0]*a,A=x[1]-S[1]*i-T[1]*a,C=x[2]-S[2]*i-T[2]*a;return{normal:s,xAxis:S,yAxis:T,tx:E,ty:A,tz:C}}function Mo(e){return Math.max(0,Math.min(6,Math.floor(e??Fe)))}function xa(e){return`${e[0]},${e[1]},${e[2]}`}function ba(e,n){let t=xa(e),r=xa(n);return t<r?`${t}|${r}`:`${r}|${t}`}function ad(e){let n=new Map;for(let r=0;r<e.length;r++){let o=e[r].vertices;if(!(!o||o.length<3||!e[r].texture))for(let s=0;s<o.length;s++){let i=ba(o[s],o[(s+1)%o.length]),a=n.get(i),c={polygon:r,edge:s};a?a.push(c):n.set(i,[c])}}let t=e.map(()=>new Set);for(let r of n.values())if(!(r.length<2))for(let o=0;o<r.length;o++)for(let s=o+1;s<r.length;s++)t[r[o].polygon].add(r[o].edge),t[r[s].polygon].add(r[s].edge);return t.map(r=>r.size>0?r:void 0)}function $t(e,n){return typeof e=="number"&&Number.isFinite(e)?Math.max(0,e):n}function So(e){return typeof e=="number"&&Number.isFinite(e)&&e>0?e:void 0}function zt(e,n,t){if(t<=0||e.length<6||e.length%2!==0)return 0;let r=e.length/2;if(n<0||n>=r)return 0;let o=[];for(let E=0;E<e.length;E+=2)o.push([e[E],e[E+1]]);let[s,i]=o[n],[a,c]=o[(n+1)%r],l=a-s,u=c-i,f=Math.hypot(l,u);if(f<=ie)return 0;let d=[],m=Number.POSITIVE_INFINITY;for(let E=0;E<r;E+=1){if(E===n||E===(n+1)%r)continue;let[A,C]=o[E],_=Math.abs((A-s)*u-(C-i)*l)/f;_>ie&&(m=Math.min(m,_))}Number.isFinite(m)&&d.push(m*.5);let g=o[(n+r-1)%r],h=o[(n+2)%r],p=g[0]-s,x=g[1]-i,b=h[0]-a,P=h[1]-c,y=Math.hypot(p,x),M=Math.hypot(b,P);if(y>ie){let E=Math.abs(l*x-u*p)/(f*y);E>ie&&d.push(y*E)}if(M>ie){let E=Math.abs(-l*P- -u*b)/(f*M);E>ie&&d.push(M*E)}let S=d.filter(E=>Number.isFinite(E)&&E>0);if(S.length===0)return Number.isFinite(t)?Math.max(0,t):0;let T=Math.min(...S);return Math.max(0,Math.min(Number.isFinite(t)?t:T,T))}function Eo(e,n,t){if(!n?.size||t===void 0)return;let r=new Map;for(let o of n){let s=zt(e,o,t);s>0&&r.set(o,s)}return r.size>0?r:void 0}function To(e,n){return e<3||!n?.size?null:Array.from({length:e},(t,r)=>Math.max(0,n.get(r)??0))}function Co(e,n){if(!n?.size||e.length<6||e.length%2!==0)return;let t=e.length/2,r=Number.POSITIVE_INFINITY,o=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];r=Math.min(r,u),o=Math.min(o,f),s=Math.max(s,u),i=Math.max(i,f)}if(!Number.isFinite(r+o+s+i))return;let a=Math.max(ie*8,.25),c={left:0,right:0,top:0,bottom:0};for(let[l,u]of n){let f=Math.max(0,u);if(f<=0||l<0||l>=t)continue;let d=l*2,m=(l+1)%t*2,g=e[d],h=e[d+1],p=e[m],x=e[m+1];Math.abs(g-p)<=a?Math.abs(g-r)<=a&&Math.abs(p-r)<=a?c.left=Math.max(c.left,f):Math.abs(g-s)<=a&&Math.abs(p-s)<=a&&(c.right=Math.max(c.right,f)):Math.abs(h-x)<=a&&(Math.abs(h-o)<=a&&Math.abs(x-o)<=a?c.top=Math.max(c.top,f):Math.abs(h-i)<=a&&Math.abs(x-i)<=a&&(c.bottom=Math.max(c.bottom,f)))}return c.left||c.right||c.top||c.bottom?c:void 0}function cd(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function ld(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let r=He(e.vertices,n,t),o=nn(r);return o?{normal:o,planeD:cd(o,r[0])}:null}function ud(e,n){return(e.material?.key??e.color??"")===(n.material?.key??n.color??"")&&e.color===n.color}function fd(e,n={}){return new Set(ya(e,n).keys())}function ya(e,n={}){let t=n.tileSize??50,r=n.layerElevation??t,o=e.map(c=>ld(c,t,r)),s=new Map;for(let c=0;c<e.length;c++){let l=e[c].vertices;if(!(!l||l.length<3))for(let u=0;u<l.length;u++){let f=ba(l[u],l[(u+1)%l.length]),d=s.get(f),m={polygon:c,edge:u};d?d.push(m):s.set(f,[m])}}let i=new Map,a=(c,l)=>{let u=i.get(c);u?u.add(l):i.set(c,new Set([l]))};for(let c of s.values())if(!(c.length<2))for(let l=0;l<c.length;l++)for(let u=l+1;u<c.length;u++){let f=c[l],d=c[u],m=f.polygon,g=d.polygon;!o[m]||!o[g]||ud(e[m],e[g])&&(a(m,f.edge),a(g,d.edge))}return i}var Qn=new Map;function En(e){if(Qn.has(e)){let t=Qn.get(e);return t===void 0?null:t}let n=_n(e);return Qn.size>=oo&&Qn.clear(),Qn.set(e,n),n}function ke(e){let n=En(e);return n?{r:n.rgb[0],g:n.rgb[1],b:n.rgb[2]}:{r:255,g:255,b:255}}function _o({r:e,g:n,b:t}){return`${e},${n},${t}`}function Xt(e){return En(e)?.alpha??1}function Wt({r:e,g:n,b:t}){let r=o=>Math.round(Math.max(0,Math.min(255,o))).toString(16).padStart(2,"0");return`#${r(e)}${r(n)}${r(t)}`}function jt(e,n,t,r){let o=ke(n),s=ke(t);return{r:s.r/255*r+o.r/255*e,g:s.g/255*r+o.g/255*e,b:s.b/255*r+o.b/255*e}}function md({r:e,g:n,b:t}){let r=o=>Math.round(Math.max(0,Math.min(1,o))*255);return`rgb(${r(e)} ${r(n)} ${r(t)})`}function Jn(e,n,t,r,o){let s=ke(e),i=ke(t),a=ke(r),c=a.r/255*o+i.r/255*n,l=a.g/255*o+i.g/255*n,u=a.b/255*o+i.b/255*n,f=Math.max(0,Math.min(255,Math.round(s.r*c))),d=Math.max(0,Math.min(255,Math.round(s.g*l))),m=Math.max(0,Math.min(255,Math.round(s.b*u))),g=Xt(e);return g<1?`rgba(${f}, ${d}, ${m}, ${g})`:Wt({r:f,g:d,b:m})}function vo(e,n){if(!Number.isFinite(n)||n<=1)return e;let t=En(e);if(!t)return e;let r=255/Math.max(1,Math.round(n)-1),o=i=>Math.max(0,Math.min(255,Math.round(Math.round(i/r)*r))),s={r:o(t.rgb[0]),g:o(t.rgb[1]),b:o(t.rgb[2])};return t.alpha<1?`rgba(${s.r}, ${s.g}, ${s.b}, ${t.alpha})`:Wt(s)}function dd(e,n){return!!e&&!!n&&e.r===n.r&&e.g===n.g&&e.b===n.b}function gd(e,n,t){let r=(o,s)=>{let i=s-o;return Math.abs(i)<=t?s:o+Math.sign(i)*t};return{r:r(e.r,n.r),g:r(e.g,n.g),b:r(e.b,n.b)}}function pd(e,n=1){return n<1?`rgba(${e.r}, ${e.g}, ${e.b}, ${n})`:Wt(e)}function hd(e,n){if(e===void 0)return Number.POSITIVE_INFINITY;if(e===n)return 0;let t=En(e),r=En(n);if(!t||!r)return 1;let o=t.rgb[0]-r.rgb[0],s=t.rgb[1]-r.rgb[1],i=t.rgb[2]-r.rgb[2],a=(t.alpha-r.alpha)*255;return Math.sqrt(o*o+s*s+i*i+a*a)/510}function Pa(e){if(e.screenPts.length!==8)return null;let n=[],t=[],r=.001,o=(s,i)=>{for(let a of s)if(Math.abs(a-i)<=r)return;s.push(i)};for(let s=0;s<e.screenPts.length;s+=2)o(n,e.screenPts[s]),o(t,e.screenPts[s+1]);if(n.length!==2||t.length!==2||(n.sort((s,i)=>s-i),t.sort((s,i)=>s-i),Math.abs(n[0])>r||Math.abs(t[0])>r||n[1]-n[0]<=r||t[1]-t[0]<=r))return null;for(let s=0;s<e.screenPts.length;s+=2){let i=e.screenPts[s],a=e.screenPts[s+1],c=Math.abs(i-n[0])<=r||Math.abs(i-n[1])<=r,l=Math.abs(a-t[0])<=r||Math.abs(a-t[1])<=r;if(!c||!l)return null}return{left:n[0],top:t[0],width:n[1]-n[0],height:t[1]-t[0]}}function tn(e){return!!Pa(e)}function Tn(e){return!e.texture&&e.polygon.vertices.length===3}function Yt(e){return!e.texture&&!!e.projectiveMatrix&&!tn(e)}function xd(e){let n=/\b(?:Chrome|HeadlessChrome|Chromium|Edg|OPR)\//.test(e);return/\bVersion\/[\d.]+.*\bSafari\//.test(e)&&!n}function Io(e,n){e.set(n,(e.get(n)??0)+1)}function Vo(e){let n,t=1;for(let[r,o]of e)o>t&&(n=r,t=o);return n}function bd(e,n,t,r){let o=!t.has("b"),s=o,i=!t.has("u")&&r.solidTriangleSupported,a=!t.has("i")&&n!=="dynamic"&&r.borderShapeSupported,c=t.has("b");return e.map(l=>{if(!l||l.texture)return l;if(i&&Tn(l))return null;let u=tn(l);return o&&u||s&&Yt(l)||n!=="dynamic"&&a&&(!u||c)?null:l})}function yd(e,n,t,r,o,s,i){let a=new Map,c=new Map,l=new Map,u=!t.has("b"),f=u&&r.projectiveQuadSupported,d=!t.has("u")&&r.solidTriangleSupported,m=!t.has("i")&&r.cornerShapeSupported,g=!t.has("i")&&r.borderShapeSupported;for(let x of e){if(!x||x.texture)continue;let b=m&&!!i?.(x);if(n==="dynamic"){if(!(d&&Tn(x))&&!(u&&tn(x))&&!(f&&Yt(x))&&!b&&!g)continue;let P=o(x.polygon.color??"#cccccc"),y=s(P);Io(c,y),l.has(y)||l.set(y,P);continue}!(d&&Tn(x))&&!(u&&tn(x))&&!(f&&Yt(x))&&!b&&!g||Io(a,x.shadedColor)}let h=Vo(a),p=Vo(c);return{paintColor:h,dynamicColorKey:p,dynamicColor:p?l.get(p):void 0}}function Pd(e,n,t,r,o,s){let i=(e-t)*(s-r)-(n-r)*(o-t);return Math.abs(i)>kt?!1:(e-t)*(e-o)+(n-r)*(n-s)<=kt}function Aa(e,n=Bt,t=Bt){let r=!1;for(let o=0,s=e.length-1;o<e.length;s=o++){let[i,a]=e[o],[c,l]=e[s];if(Pd(n,t,i,a,c,l))return!0;a>t!=l>t&&n<(c-i)*(t-a)/(l-a)+i&&(r=!r)}return r}function Ma(e,n,t){let r=Number.POSITIVE_INFINITY,o=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];!Number.isFinite(u)||!Number.isFinite(f)||(r=Math.min(r,u),o=Math.min(o,f),s=Math.max(s,u),i=Math.max(i,f))}let a=s-r,c=i-o;return!Number.isFinite(r)||!Number.isFinite(o)||!Number.isFinite(a)||!Number.isFinite(c)||a<=re||c<=re?{minX:0,minY:0,width:n,height:t}:{minX:r,minY:o,width:a,height:c}}function et(e){let n=e.canvasW||1,t=e.canvasH||1,r=Hn>0?Sn(e.screenPts,Hn):e.screenPts,o=Hn>0?Ma(r,n,t):{minX:0,minY:0,width:n,height:t},s=[];for(let i=0;i<r.length;i+=2){let a=Math.max(0,Math.min(100,(r[i]-o.minX)/o.width*100)),c=Math.max(0,Math.min(100,(r[i+1]-o.minY)/o.height*100));s.push([a,c])}return{bounds:o,points:s}}function Sa(e){let n=[];for(let t of e){let r=n[n.length-1];r&&Math.hypot(r[0]-t[0],r[1]-t[1])<=Ut||n.push(t)}if(n.length>1){let t=n[0],r=n[n.length-1];Math.hypot(t[0]-r[0],t[1]-r[1])<=Ut&&n.pop()}return n}function Ea([e,n]){let t=new Set;return Math.abs(e)<=_e&&t.add("left"),Math.abs(e-100)<=_e&&t.add("right"),Math.abs(n)<=_e&&t.add("top"),Math.abs(n-100)<=_e&&t.add("bottom"),t.size>0?t:null}function Ta(e,n){for(let t of e)if(n.has(t))return!0;return!1}function Ca(e,n,t,r){let o=(s,i,a)=>{let c=n.has(i)?e:r.has(i)?t:null,l=n.has(a)?e:r.has(a)?t:null;if(!c||!l)return null;let u=(()=>{switch(s){case"topLeft":return{x:c[0],y:l[1]};case"topRight":return{x:100-c[0],y:l[1]};case"bottomRight":return{x:100-c[0],y:100-l[1]};case"bottomLeft":return{x:c[0],y:100-l[1]}}})();return u.x>_e&&u.y>_e&&u.x<100-_e&&u.y<100-_e?[s,u]:null};return(n.has("top")||r.has("top"))&&(n.has("left")||r.has("left"))?o("topLeft","top","left"):(n.has("top")||r.has("top"))&&(n.has("right")||r.has("right"))?o("topRight","top","right"):(n.has("bottom")||r.has("bottom"))&&(n.has("right")||r.has("right"))?o("bottomRight","bottom","right"):(n.has("bottom")||r.has("bottom"))&&(n.has("left")||r.has("left"))?o("bottomLeft","bottom","left"):null}function Ad(e){if(e.texture||Tn(e)||tn(e))return null;let n=et(e),t=Sa(n.points);if(t.length<4)return null;let r=t.map(Ea);if(r.some(i=>!i))return null;let o={},s=0;for(let i=0;i<t.length;i+=1){let a=r[i],c=r[(i+1)%t.length];if(Ta(a,c))continue;let l=Ca(t[i],a,t[(i+1)%t.length],c);if(!l)return null;let[u,f]=l,d=o[u];if(d&&(Math.abs(d.x-f.x)>_e||Math.abs(d.y-f.y)>_e))return null;o[u]=f,s+=1}return s>0?{bounds:n.bounds,radii:o}:null}function Md([e,n]){return`${Ye(e)} ${Ye(n)}`}function Sd(e){return`polygon(${e.map(Md).join(",")})`}function Ed(e){if(Aa(e))return"circle(0)";let n=0,t=0,r=Math.max(1,e.length);for(let[i,a]of e)n+=i,t+=a;let o=Ye(Math.max(0,Math.min(100,n/r))),s=Ye(Math.max(0,Math.min(100,t/r)));return`circle(0 at ${o} ${s})`}function Oo(e){return`${Sd(e)} ${Ed(e)}`}function Td(e){return Oo(et(e).points)}function Ro(e,n){return Kn(e,n.width/Be,n.height/Be,n.minX,n.minY)}function Cd(e){let n=et(e);return`matrix3d(${Ro(e,n.bounds)})`}function _d(e){let n=et(e);return[`transform:matrix3d(${Ro(e,n.bounds)})`,`border-shape:${Oo(n.points)}`].join(";")}function vd(e,n){let t=[`transform:matrix3d(${Ro(e,n.bounds)})`,`width:${Be}px`,`height:${Be}px`,"border:0","box-sizing:border-box","background:currentColor"];for(let[r,o]of Object.entries(n.radii)){let s=r.replace(/[A-Z]/g,i=>`-${i.toLowerCase()}`);t.push(`border-${s}-radius:${Ye(o.x)} ${Ye(o.y)}`),t.push(`corner-${s}-shape:bevel`)}return t.join(";")}function Id(e,n){if((e+1)%3===n)return e;if((n+1)%3===e)return n}function Vd(e,n,t,r,o,s,i){if(!e?.size)return null;let a=$t(n,t);return[[s,r],[r,o],[o,s]].map(([l,u],f)=>{let d=Id(l,u),m=d!==void 0&&e.has(d)?a:0;return zt(i,f,m)})}function Lo(e,n,t,r,o,s,i,a){let c=s,l="",u,f,d="";if(i){let m=a??e.color??"#cccccc",g=s.directionalLight,h=s.ambientLight,p=g?.direction??hn,x=g?.color??xn,b=Math.max(0,g?.intensity??bn),P=h?.color??yn,y=Math.max(0,h?.intensity??Pn),M=Math.hypot(p[0],p[1],p[2])||1,S=p[0]/M,T=p[1]/M,E=p[2]/M,A=b*Math.max(0,t*S+r*T+o*E),C=Jn(m,A,x,P,y),_=s.textureLighting??"baked",w=_==="baked"&&c.stableTriangleColorSteps?vo(C,c.stableTriangleColorSteps):C,V=ke(m),N=w===s.solidPaintDefaults?.paintColor,F=_==="dynamic"&&_o(V)===s.solidPaintDefaults?.dynamicColorKey;l=_==="dynamic"||N?"":w,u=l?ke(l):void 0,f=l?Xt(l):void 0,d=_==="dynamic"?`--pnx:${t.toFixed(4)};--pny:${r.toFixed(4)};--pnz:${o.toFixed(4)};`+(F?"":`--psr:${(V.r/255).toFixed(4)};--psg:${(V.g/255).toFixed(4)};--psb:${(V.b/255).toFixed(4)};`):""}return{index:n,polygon:e,colorComputed:i,bakedColor:l||void 0,bakedRgb:u,bakedAlpha:f,dynamicVars:d}}function Od(e,n,t){if(e.texture||e.vertices.length!==3)return null;let r=t.tileSize??50,o=t.layerElevation??r,s=e.vertices[0],i=e.vertices[1],a=e.vertices[2],c=[s[1]*r,s[0]*r,s[2]*o],l=[i[1]*r,i[0]*r,i[2]*o],u=[a[1]*r,a[0]*r,a[2]*o],f=l[0]-c[0],d=l[1]-c[1],m=l[2]-c[2],g=u[0]-c[0],h=u[1]-c[1],p=u[2]-c[2],x=-(d*p-m*h),b=-(m*g-f*p),P=-(f*h-d*g),y=Math.sqrt(x*x+b*b+P*P);return y<=re?null:(x/=y,b/=y,P/=y,Lo(e,n,x,b,P,t,!0))}function Rd(e,n,t,r={}){if(e.texture||e.vertices.length!==3)return null;let o=t.tileSize??50,s=t.layerElevation??o,i=e.vertices[0],a=e.vertices[1],c=e.vertices[2],l=i[1]*o,u=i[0]*o,f=i[2]*s,d=a[1]*o,m=a[0]*o,g=a[2]*s,h=c[1]*o,p=c[0]*o,x=c[2]*s;return wo(e,n,t,r,l,u,f,d,m,g,h,p,x)}function wo(e,n,t,r,o,s,i,a,c,l,u,f,d){let m=t,g=a-o,h=c-s,p=l-i,x=u-o,b=f-s,P=d-i,y=-(h*P-p*b),M=-(p*x-g*P),S=-(g*b-h*x),T=Math.sqrt(y*y+M*M+S*S);if(T<=re)return null;y/=T,M/=T,S/=T;let E=r.basis,A=E?.a??0,C=E?.b??1,_=E?.c??2;A<0||A>2||C<0||C>2||_<0||_>2||A===C||A===_||C===_?(E=void 0,A=0,C=1,_=2):A===0&&C===1&&_===2||A===1&&C===2&&_===0||A===2&&C===0&&_===1||(E=void 0,A=0,C=1,_=2);let w=()=>E?wo(e,n,t,{...r,basis:void 0},o,s,i,a,c,l,u,f,d):null;if(!E){let Ke=g*g+h*h+p*p,Xo=u-a,Wo=f-c,jo=d-l,Yo=o-u,Ho=s-f,Ko=i-d,qo=Xo*Xo+Wo*Wo+jo*jo,ac=Yo*Yo+Ho*Ho+Ko*Ko,qt=Ke;qo>qt&&(A=1,C=2,_=0,qt=qo),ac>qt&&(A=2,C=0,_=1)}let V,N,F,k,$,R,B=_===0?o:_===1?a:u,z=_===0?s:_===1?c:f,ee=_===0?i:_===1?l:d;A===0?(V=o,N=s,F=i):A===1?(V=a,N=c,F=l):(V=u,N=f,F=d),C===0?(k=o,$=s,R=i):C===1?(k=a,$=c,R=l):(k=u,$=f,R=d);let Q=k-V,Z=$-N,ne=R-F,j=Math.sqrt(Q*Q+Z*Z+ne*ne);if(j<=re)return w();let D=Q/j,I=Z/j,U=ne/j,G=(B-V)*D+(z-N)*I+(ee-F)*U,q=M*U-S*I,v=S*D-y*U,O=y*I-M*D,L=T/j;if(L<=re)return w();let X=Math.max(0,Math.min(j,G)),H=Math.max(0,j-X),Y=[X,0,0,L,X+H,L],K=Vd(m.seamEdges,m.seamBleed,Dt,A,C,_,Y),te=K?Zn(Y,K):Ao(X,H,L,$t(m.seamBleed,Dt)),se=te[0],he=te[1],Se=te[2],le=te[3],me=te[4],pe=te[5],ve=(le+pe)/2,ye=se-Se,Ee=me-se,xe=ve-he;if(ye<=re||Ee<=re||xe<=re||!Number.isFinite(ye+Ee+xe))return w();let Pe=r.includeColor??!0,Ae=!1,ce,fe,Me,ae="";if(Pe){let Ke=Lo(e,n,y,M,S,t,!0,r.color);Ae=Ke.colorComputed,ce=Ke.bakedColor,fe=Ke.bakedRgb,Me=Ke.bakedAlpha,ae=Ke.dynamicVars??""}let de=ce?`color:${ce};`:"",W=r.primitive??r.resolvedPrimitive??"border",oe=1/(W==="border-large"?uo:lo),be=ye+Ee,Te=be*oe,Ue=(Ee-ye)*.5*oe,rn=xe*oe,on=se-X-be*.5,sn=he,Ha=D*Te,Ka=I*Te,qa=U*Te,Za=D*Ue+q*rn,Qa=I*Ue+v*rn,Ja=U*Ue+O*rn,ec=B+D*on+q*sn,nc=z+I*on+v*sn,tc=ee+U*on+O*sn,rc=r.matrixDecimals??Mo(m.stableTriangleMatrixDecimals),zo=xo(Ha,Ka,qa,Za,Qa,Ja,y,M,S,ec,nc,tc,rc),oc=t.textureLighting??"baked",sc=m.optimizeStableTriangleStyle===!0&&oc==="baked"?"":`transform:${zo};`+de+ae,ic=E&&E.a===A&&E.b===C&&E.c===_?E:{a:A,b:C,c:_};return{index:n,polygon:e,styleText:sc,transformText:zo,basis:ic,primitive:W,colorComputed:Ae,bakedColor:ce,bakedRgb:fe,bakedAlpha:Me,dynamicVars:ae}}function _a(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function va(e){let n=e?.maxWeightRatio,t=Math.max(0,_a(e?.denomEps,mo)),r=typeof n=="number"&&Number.isFinite(n)&&n>0?Math.max(1,n):go,o=Math.max(0,_a(e?.bleed,po));return{denomEps:t,maxWeightRatio:r,bleed:o,disableGuards:e?.disableGuards===!0}}function No(e,n){if(e.length!==4||!qn(e))return null;let[t,r,o,s]=e,i=t[0]-r[0]+o[0]-s[0],a=t[1]-r[1]+o[1]-s[1],c=r[0]-o[0],l=s[0]-o[0],u=r[1]-o[1],f=s[1]-o[1],d=c*f-u*l;if(Math.abs(d)<=re)return null;let m=(i*f-a*l)/d,g=(c*a-u*i)/d,h=[1,1+m,1+m+g,1+g];if(h.some(b=>!Number.isFinite(b)))return null;let p=Math.min(...h),x=Math.max(...h);return!n.disableGuards&&(p<=n.denomEps||x/p>n.maxWeightRatio)?null:{g:m,h:g,w1:1+m,w3:1+g}}function Ia(e,n,t,r,o,s,i,a,c){if(e.length!==8)return null;let l=[[e[0],e[1]],[e[2],e[3]],[e[4],e[5]],[e[6],e[7]]];if(!No(l,a))return null;let u=To(4,c),f=u?Zn(e,u):Sn(e,a.bleed),d=[[f[0],f[1]],[f[2],f[3]],[f[4],f[5]],[f[6],f[7]]],m=No(d,a);if(!m)return null;let{g,h,w1:p,w3:x}=m,[b,P,,y]=d,M=[o+b[0]*n[0]+b[1]*t[0],s+b[0]*n[1]+b[1]*t[1],i+b[0]*n[2]+b[1]*t[2]],S=([E,A],C)=>[(C-1)*o+(C*E-b[0])*n[0]+(C*A-b[1])*t[0],(C-1)*s+(C*E-b[0])*n[1]+(C*A-b[1])*t[1],(C-1)*i+(C*E-b[0])*n[2]+(C*A-b[1])*t[2]],T=[...S(P,p),g,...S(y,x),h,r[0],r[1],r[2],0,M[0],M[1],M[2],1];for(let E=0;E<8;E+=1)T[E]/=An;return je(T,6)}function Kt(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Ld(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function Va(e){return!e.texture}function Oa(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let r=He(e.vertices,n,t),o=nn(r);return o?{pts:r,normal:o,planeD:Kt(o,r[0]),optimizable:Va(e)}:null}function Ra(e,n){return!e||!n||!e.optimizable||!n.optimizable?!1:Bo(e,n)}function Bo(e,n){return!e||!n||Kt(e.normal,n.normal)<1-so?!1:Math.abs(e.planeD-n.planeD)<=io}function Do(e,n){if(!e)return null;let t=n.directionalLight,r=n.ambientLight,o=t?.direction??hn,s=t?.color??xn,i=Math.max(0,t?.intensity??bn),a=r?.color??yn,c=Math.max(0,r?.intensity??Pn),l=Math.hypot(o[0],o[1],o[2])||1,u=o[0]/l,f=o[1]/l,d=o[2]/l,m=i*Math.max(0,e.normal[0]*u+e.normal[1]*f+e.normal[2]*d),g=jt(m,s,a,c);return g.r*.2126+g.g*.7152+g.b*.0722}function La(e){let n=[...e],t=Math.abs(n[0])>re?0:Math.abs(n[1])>re?1:2;return n[t]<0&&(n[0]*=-1,n[1]*=-1,n[2]*=-1),`${n[0].toFixed(6)},${n[1].toFixed(6)},${n[2].toFixed(6)}`}function Cn(e,n,t,r,o={}){let s=Kt(r,t),i=[r[0]-s*t[0],r[1]-s*t[1],r[2]-s*t[2]],a=Math.hypot(i[0],i[1],i[2]);if(a<=re)return null;let c=[i[0]/a,i[1]/a,i[2]/a],l=[t[1]*c[2]-t[2]*c[1],t[2]*c[0]-t[0]*c[2],t[0]*c[1]-t[1]*c[0]],u=Math.hypot(l[0],l[1],l[2]);if(u<=re)return null;let f=[l[0]/u,l[1]/u,l[2]/u],d=e.map(F=>{let k=F[0]-n[0],$=F[1]-n[1],R=F[2]-n[2];return[k*c[0]+$*c[1]+R*c[2],k*f[0]+$*f[1]+R*f[2]]}),m=o.boundsOrigin??n,g=n[0]-m[0],h=n[1]-m[1],p=n[2]-m[2],x=g*c[0]+h*c[1]+p*c[2],b=g*f[0]+h*f[1]+p*f[2],P=1/0,y=1/0,M=-1/0,S=-1/0;for(let[F,k]of d){let $=F+x,R=k+b;$<P&&(P=$),$>M&&(M=$),R<y&&(y=R),R>S&&(S=R)}let T=M-P,E=S-y;if(!Number.isFinite(T)||!Number.isFinite(E))return null;let A=o.snapBounds?Math.floor(P+ie):P,C=o.snapBounds?Math.floor(y+ie):y,_=o.snapBounds?Math.ceil(M-ie):M,w=o.snapBounds?Math.ceil(S-ie):S,V=Math.max(1,o.snapBounds?_-A:Math.ceil(T)),N=Math.max(1,o.snapBounds?w-C:Math.ceil(E));return{xAxis:c,yAxis:f,local2D:d,shiftX:x-A,shiftY:b-C,canvasW:V,canvasH:N,pixelArea:V*N,rawArea:T*E}}function Ht(e){return`${e[0]},${e[1]},${e[2]}`}function wa(e,n){return Ht(e)<Ht(n)?[n[0]-e[0],n[1]-e[1],n[2]-e[2]]:[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function wd(e,n){let t=Ht(e),r=Ht(n);return t<r?`${t}|${r}`:`${r}|${t}`}function Na(e,n,t,r){let o=0,s=0;for(let i of e){let a=n[i];if(!a)return null;let c=Cn(a.pts,a.pts[0],a.normal,t,{boundsOrigin:r,snapBounds:!0});if(!c)return null;o+=c.pixelArea,s+=c.rawArea}return{pixelArea:o,rawArea:s}}function Da(e,n){let t=n[e[0]]?.pts[0];if(!t)return null;let r={pixelArea:0,rawArea:0},o=null,s=new Set;for(let i of e){let a=n[i];if(!a)continue;let c=[a.pts[1][0]-a.pts[0][0],a.pts[1][1]-a.pts[0][1],a.pts[1][2]-a.pts[0][2]],l=Cn(a.pts,a.pts[0],a.normal,c);r&&l?(r.pixelArea+=l.pixelArea,r.rawArea+=l.rawArea):r=null;for(let u=0;u<a.pts.length;u++){let f=wa(a.pts[u],a.pts[(u+1)%a.pts.length]),d=Cn(a.pts,a.pts[0],a.normal,f);if(!d)continue;let m=La(d.xAxis);if(s.has(m))continue;s.add(m);let g=Na(e,n,d.xAxis,t);g&&(!o||g.pixelArea<o.pixelArea||g.pixelArea===o.pixelArea&&g.rawArea<o.rawArea-ie)&&(o={xAxis:d.xAxis,...g})}}return o&&r&&(o.pixelArea<r.pixelArea||o.pixelArea===r.pixelArea&&o.rawArea<=r.rawArea+ie)?{xAxis:o.xAxis,boundsOrigin:t,seamEdges:new Set}:null}function Nd(e,n){let t=n.tileSize??50,r=n.layerElevation??t,o=e.map(f=>Oa(f,t,r)),s=new Map,i=e.map(()=>new Set),a=e.map(()=>new Set);for(let f=0;f<e.length;f++){let d=e[f].vertices;if(!(!d||d.length<3))for(let m=0;m<d.length;m++){let g=wd(d[m],d[(m+1)%d.length]),h=s.get(g),p={polygon:f,edge:m};h?h.push(p):s.set(g,[p])}}let c=e.map(()=>new Set);for(let f of s.values())if(!(f.length<2))for(let d=0;d<f.length;d++)for(let m=d+1;m<f.length;m++){let g=f[d],h=f[m],p=g.polygon,x=h.polygon;if(e[p].texture&&e[x].texture&&(a[g.polygon].add(g.edge),a[h.polygon].add(h.edge)),Bo(o[p],o[x]))i[g.polygon].add(g.edge),i[h.polygon].add(h.edge);else{let b=Do(o[p],n),P=Do(o[x],n);b!==null&&P!==null&&(b<=P+Nt&&i[g.polygon].add(g.edge),P<=b+Nt&&i[h.polygon].add(h.edge))}Ra(o[p],o[x])&&(c[p].add(x),c[x].add(p))}let l=Array(e.length).fill(void 0),u=new Set;for(let f=0;f<e.length;f++){if(u.has(f)||!o[f]?.optimizable)continue;let d=[],m=[f];for(u.add(f);m.length>0;){let h=m.pop();d.push(h);for(let p of c[h])u.has(p)||(u.add(p),m.push(p))}if(d.length<2)continue;let g=Da(d,o);if(g)for(let h of d)l[h]={xAxis:g.xAxis,boundsOrigin:g.boundsOrigin,seamEdges:i[h],textureEdgeRepairEdges:a[h]}}for(let f=0;f<e.length;f++)!l[f]&&(i[f].size>0||a[f].size>0)&&(l[f]={seamEdges:i[f],textureEdgeRepairEdges:a[f]});return l}function Fo(e,n,t,r){if(r.optimize&&r.fixedXAxis)return Cn(e,n,t,r.fixedXAxis,{boundsOrigin:r.boundsOrigin,snapBounds:r.snapBounds});let o=null,s=r.optimize&&r.seamEdges&&r.seamEdges.size>0?Array.from(r.seamEdges):null,i=s??(r.optimize?e.map((a,c)=>c):[0]);for(let a of i){let c=(a+1)%e.length,l=s?wa(e[a],e[c]):[e[c][0]-e[a][0],e[c][1]-e[a][1],e[c][2]-e[a][2]],u=Cn(e,n,t,l,{boundsOrigin:r.boundsOrigin,snapBounds:r.snapBounds});u&&(!o||u.pixelArea<o.pixelArea||u.pixelArea===o.pixelArea&&u.rawArea<o.rawArea-ie)&&(o=u)}return o}function Fa(e){if(e.local2D.length!==4)return!1;let n=[],t=[],r=(o,s)=>{for(let i of o)if(Math.abs(i-s)<=ie)return;o.push(s)};for(let[o,s]of e.local2D)r(n,o+e.shiftX),r(t,s+e.shiftY);if(n.length!==2||t.length!==2||(n.sort((o,s)=>o-s),t.sort((o,s)=>o-s),Math.abs(n[0])>ie||Math.abs(t[0])>ie||n[1]-n[0]<=ie||t[1]-t[0]<=ie))return!1;for(let[o,s]of e.local2D){let i=o+e.shiftX,a=s+e.shiftY,c=Math.abs(i-n[0])<=ie||Math.abs(i-n[1])<=ie,l=Math.abs(a-t[0])<=ie||Math.abs(a-t[1])<=ie;if(!c||!l)return!1}return!0}function ko(e,n){if(e.length<3||n.length<3)return null;let[t,r,o]=e,[s,i,a]=n,c=t[0],l=t[1],u=r[0],f=r[1],d=o[0],m=o[1],g=s[0],h=1-s[1],p=i[0],x=1-i[1],b=a[0],P=1-a[1],y=p-g,M=x-h,S=b-g,T=P-h,E=y*T-S*M;if(Math.abs(E)<=1e-9)return null;let A=u-c,C=d-c,_=f-l,w=m-l,V={a:(A*T-C*M)/E,b:(y*C-S*A)/E,c:(_*T-w*M)/E,d:(y*w-S*_)/E,e:0,f:0};return V.e=c-V.a*g-V.b*h,V.f=l-V.c*g-V.d*h,V}function Uo(e){if(e.length===0)return null;let n=1/0,t=1/0,r=-1/0,o=-1/0;for(let s of e){let i=s[0],a=1-s[1];if(!Number.isFinite(i)||!Number.isFinite(a))return null;n=Math.min(n,i),r=Math.max(r,i),t=Math.min(t,a),o=Math.max(o,a)}return{minU:n,minV:t,maxU:r,maxV:o}}function Ba(e,n,t,r,o,s,i,a){let l=He(e.vertices,n,t).map(d=>{let m=d[0]-r[0],g=d[1]-r[1],h=d[2]-r[2];return[m*o[0]+g*o[1]+h*o[2]+i,m*s[0]+g*s[1]+h*s[2]+a]}),u=ko(l,e.uvs),f=Uo(e.uvs);return!u&&!f?null:{screenPts:l.flatMap(([d,m])=>[d,m]),uvAffine:u,uvSampleRect:f}}function ka(e,n,t,r,o){let s=t,{vertices:i,texture:a,uvs:c}=e;if(!i||i.length<3)return null;let l=t.tileSize??50,u=t.layerElevation??l,f=He(i,l,u),d=f[0],m=f[1],g=[m[0]-d[0],m[1]-d[1],m[2]-d[2]];if(Math.hypot(g[0],g[1],g[2])===0)return null;let p=nn(f);if(!p)return null;let x=Fo(f,d,p,{optimize:!1}),b=a||x&&Fa(x)?x:Fo(f,d,p,{optimize:!0,fixedXAxis:o?.xAxis,boundsOrigin:o?.boundsOrigin,snapBounds:!!o,seamEdges:o?.seamEdges});if(!b)return null;let{xAxis:P,yAxis:y,local2D:M}=b,S=a&&o?.textureEdgeRepairEdges?.size?o.textureEdgeRepairEdges:null,T=!!(a&&S),E=b.shiftX,A=b.shiftY,C=b.canvasW,_=b.canvasH,w=[];for(let[le,me]of M)w.push(le+E,me+A);let V=d[0]-E*P[0]-A*y[0],N=d[1]-E*P[1]-A*y[1],F=d[2]-E*P[2]-A*y[2],k=je([P[0],P[1],P[2],0,y[0],y[1],y[2],0,p[0],p[1],p[2],0,V,N,F,1]),$=je([P[0]*C,P[1]*C,P[2]*C,0,y[0]*_,y[1]*_,y[2]*_,0,p[0],p[1],p[2],0,V,N,F,1]),R=je([P[0]*C/Ce,P[1]*C/Ce,P[2]*C/Ce,0,y[0]*_/Ce,y[1]*_/Ce,y[2]*_/Ce,0,p[0],p[1],p[2],0,V,N,F,1]),B=So(s.seamBleed),z=Eo(w,s.seamEdges??o?.seamEdges,B),ee=z?new Set(z.keys()):void 0,Q=z?Math.max(...z.values()):void 0,Z=Co(w,z),ne=!a&&i.length===4?Ia(w,P,y,p,V,N,F,r,z):null,j=t.directionalLight,D=t.ambientLight,I=j?.direction??hn,U=j?.color??xn,G=Math.max(0,j?.intensity??bn),q=D?.color??yn,v=Math.max(0,D?.intensity??Pn),O=Math.hypot(I[0],I[1],I[2])||1,L=I[0]/O,X=I[1]/O,H=I[2]/O,Y=G*Math.max(0,p[0]*L+p[1]*X+p[2]*H),K=jt(Y,U,q,v),te=Jn(e.color??"#cccccc",Y,U,q,v),se=null,he=null;a&&c&&c.length>=3&&c.length===i.length&&(he=Uo(c),se=ko(M.map(([le,me])=>[le+E,me+A]),c));let Se=a&&e.textureTriangles?.length?e.textureTriangles.map(le=>Ba(le,l,u,d,P,y,E,A)).filter(le=>!!le):null;return{index:n,polygon:e,texture:a,tileSize:l,layerElevation:u,matrix:k,canonicalMatrix:$,atlasMatrix:R,projectiveMatrix:ne,canvasW:C,canvasH:_,screenPts:w,uvAffine:se,uvSampleRect:he,textureTriangles:Se,textureEdgeRepairEdges:S,textureEdgeRepair:T,seamBleed:Q,seamBleedEdges:ee,seamBleedEdgeAmounts:z,seamBleedInsets:Z,normal:p,textureTint:K,shadedColor:te}}function Dd(e,n,t={},r){let o=va(r),s=t,i=t.textureEdgeRepairEdges?.size||s.seamEdges?.size?{seamEdges:s.seamEdges??new Set,textureEdgeRepairEdges:t.textureEdgeRepairEdges}:void 0;return ka(e,n,s,o,i)}function tt(e){let n=typeof e=="string"?Number(e):e;return n===void 0||!Number.isFinite(n)?1:Math.min(Jr,Math.max(Qr,n))}function $o(e){return e.reduce((n,t)=>n+t.width*t.height,0)}function Ua(e,n){let t=$o(e);if(t<=0)return 1;let r=Math.max(1,...e.map(i=>Math.max(i.width,i.height))),o=Yn/r,s=Math.sqrt(n/(t*4));return tt(Math.min(o,s))}function Ga(e,n){let t=$o(e),r=.5;return t<=wt?r=1:t<=eo&&(r=.75),tt(Math.min(r,Ua(e,n)))}function $a(e,n){return e.reduce((t,r)=>Math.max(t,Math.ceil(r.width*n),Math.ceil(r.height*n)),0)}function za(e,n){return e.reduce((t,r)=>t+Math.ceil(r.width*n)*Math.ceil(r.height*n)*4,0)}function Xa(e,n,t){let r=$a(e,n),o=za(e,n),s=r>Yn?Yn/r:1,i=o>t?Math.sqrt(t/o):1;return Math.min(s,i)}function Wa(e){return e?no:to}function ja(e,n){return e!==void 0&&e!=="auto"?Ce:n?Ce:fo}function Go(e,n){for(let t of e.entries)t&&(t.atlasCanonicalSize=n,t.atlasMatrix=bo(t,n));return e}function Fd(e){return e.atlasCanonicalSize??Ce}function Ya(e){return Math.max(Lt,Math.ceil(Lt/e))}function nt(e,n=1){let t=Array(e.length).fill(null),r=[],o=Ya(n),s=e.filter(c=>!!c).sort((c,l)=>l.canvasH-c.canvasH||l.canvasW-c.canvasW||c.index-l.index),i=()=>({width:o,height:o,entries:[],shelves:[]}),a=(c,l,u)=>{if(c.sealed)return null;for(let m of c.shelves)if(l.canvasH<=m.height&&m.x+l.canvasW+o<=We){let g={...l,pageIndex:u,x:m.x,y:m.y};return m.x+=l.canvasW+o*2,c.entries.push(g),c.width=Math.max(c.width,g.x+l.canvasW+o),g}let f=c.shelves.length===0?o:c.height+o;if(f+l.canvasH+o>We)return null;let d={...l,pageIndex:u,x:o,y:f};return c.shelves.push({x:o+l.canvasW+o*2,y:f,height:l.canvasH}),c.entries.push(d),c.width=Math.max(c.width,d.x+l.canvasW+o),c.height=Math.max(c.height,f+l.canvasH+o),d};for(let c of s){if(c.canvasW+o*2>We||c.canvasH+o*2>We){let f=r.length,d={...c,pageIndex:f,x:o,y:o};t[c.index]=d,r.push({width:c.canvasW+o*2,height:c.canvasH+o*2,entries:[d],shelves:[],sealed:!0});continue}let u=null;for(let f=0;f<r.length&&(u=a(r[f],c,f),!u);f++);if(!u){let f=i(),d=r.length;r.push(f),u=a(f,c,d)}u&&(t[c.index]=u)}return{entries:t,pages:r.map(({width:c,height:l,entries:u})=>({width:c,height:l,entries:u}))}}function Bd(e,n,t){let r=Ga(n.pages,t),o=r===1?n:nt(e,r);for(let s=0;s<4;s++){let i=Xa(o.pages,r,t);if(i>=1)break;let a=tt(r*i*ro);if(a>=r)break;r=a,o=nt(e,r)}return{packed:o,atlasScale:r}}function kd(e,n,t){let r=ja(n,t);if(n!==void 0&&n!=="auto"){let i=tt(n);return{packed:Go(nt(e,i),r),atlasScale:i,atlasCanonicalSize:r}}let o=nt(e,1),s=Bd(e,o,Wa(t));return{packed:Go(s.packed,r),atlasScale:s.atlasScale,atlasCanonicalSize:r}}export{jm as ASYNC_RENDER_BUDGET_MS,fo as ATLAS_CANONICAL_SIZE_AUTO_DESKTOP,Ce as ATLAS_CANONICAL_SIZE_EXPLICIT,We as ATLAS_MAX_SIZE,Lt as ATLAS_PADDING,wt as AUTO_ATLAS_LOW_AREA,Yn as AUTO_ATLAS_MAX_BITMAP_SIDE,to as AUTO_ATLAS_MAX_DECODED_BYTES_DESKTOP,no as AUTO_ATLAS_MAX_DECODED_BYTES_MOBILE,eo as AUTO_ATLAS_MEDIUM_AREA,ro as AUTO_ATLAS_SCALE_GUARD,Sf as BAKED_SHADOW_MIN_UP,Mf as BAKED_SHADOW_Z_SQUASH,at as BASE_TILE,re as BASIS_EPS,Hn as BORDER_SHAPE_BLEED,Be as BORDER_SHAPE_CANONICAL_SIZE,Bt as BORDER_SHAPE_CENTER_PERCENT,kt as BORDER_SHAPE_POINT_EPS,Ct as CAMERA_BACKFACE_CULL_EPS,oo as COLOR_PARSE_CACHE_MAX,Ut as CORNER_SHAPE_DUPLICATE_EPS,_e as CORNER_SHAPE_POINT_EPS,co as DECIMAL_SCALES,yn as DEFAULT_AMBIENT_COLOR,Pn as DEFAULT_AMBIENT_INTENSITY,Ft as DEFAULT_ATLAS_CSS_DECIMALS,ao as DEFAULT_BORDER_SHAPE_DECIMALS,ln as DEFAULT_CAMERA_STATE,xn as DEFAULT_LIGHT_COLOR,hn as DEFAULT_LIGHT_DIR,bn as DEFAULT_LIGHT_INTENSITY,Fe as DEFAULT_MATRIX_DECIMALS,cc as DEFAULT_PROJECTION,Jm as DEFAULT_SEAM_BLEED,Os as DEFAULT_SEAM_FACET_SPLIT_OPTIONS,Vs as DEFAULT_SEAM_OVERLAP_OPTIONS,De as DEFAULT_TILE,_i as LoopOnce,vi as LoopPingPong,Fr as LoopRepeat,Jr as MAX_ATLAS_SCALE,Qr as MIN_ATLAS_SCALE,po as PROJECTIVE_QUAD_BLEED,mo as PROJECTIVE_QUAD_DENOM_EPS,go as PROJECTIVE_QUAD_MAX_WEIGHT_RATIO,Ac as QUAT_IDENTITY,ie as RECT_EPS,Nt as SEAM_LIGHT_EPS,An as SOLID_QUAD_CANONICAL_SIZE,Dt as SOLID_TRIANGLE_BLEED,lo as SOLID_TRIANGLE_CANONICAL_SIZE,Zm as SOLID_TRIANGLE_CORNER_CLASS,uo as SOLID_TRIANGLE_LARGE_BORDER_CANONICAL_SIZE,Qm as SOLID_TRIANGLE_LARGE_BORDER_CLASS,io as SURFACE_DISTANCE_EPS,so as SURFACE_NORMAL_EPS,Hm as TEXTURE_EDGE_REPAIR_ALPHA_MIN,qm as TEXTURE_EDGE_REPAIR_RADIUS,Km as TEXTURE_EDGE_REPAIR_SOURCE_ALPHA_MIN,Ym as TEXTURE_TRIANGLE_BLEED,si as VOXEL_CAMERA_CULL_AXIS_EPS,ii as VOXEL_CAMERA_CULL_NORMAL_LIMIT,Go as applyPackedAtlasCanonicalSize,pi as arrowPolygons,$o as atlasArea,$a as atlasBitmapMaxSide,Fd as atlasCanonicalSizeForEntry,ja as atlasCanonicalSizeForTextureQuality,za as atlasDecodedBytes,Ya as atlasPadding,Xa as autoAtlasBudgetFactor,Wa as autoAtlasMaxDecodedBytes,Ga as autoAtlasScale,Ua as autoAtlasScaleCap,fi as axesHelperPolygons,Em as bakeSolidTextureSampledPolygons,Xr as bakeSolidTextureSamples,La as basisAxisKey,Ma as borderShapeBoundsFromPoints,et as borderShapeGeometryForPlan,di as boxPolygons,Ef as buildBakedShadowProjectionMatrix,Nd as buildBasisHints,gc as buildSceneContext,ya as buildSeamBleedPolygonEdges,fd as buildSeamBleedPolygonSet,ad as buildTextureEdgeRepairSets,En as cachedParsePureColor,li as cameraCullNormalGroups,ff as cameraCullNormalGroupsFromPolygons,ci as cameraCullNormalKey,df as cameraCullVisibleSignature,ai as cameraFacingDepth,Da as chooseIslandXAxis,Fo as chooseLocalBasis,qe as clampChannel,If as clipPolygonToConvex2D,hd as colorErrorScore,Bo as compatibleBleedSurface,Ra as compatibleSurface,Eo as computePlanSeamBleedEdgeAmounts,No as computeProjectiveQuadCoefficients,Ia as computeProjectiveQuadMatrix,is as computeSceneBbox,Co as computeSeamBleedInsets,uc as computeShapeLighting,Od as computeSolidTriangleColorPlan,Lo as computeSolidTriangleColorPlanFromNormal,Rd as computeSolidTrianglePlan,wo as computeSolidTrianglePlanFromCssPoints,nn as computeSurfaceNormal,ka as computeTextureAtlasPlan,Dd as computeTextureAtlasPlanPublic,yc as computeTexturePaintMetrics,ko as computeUvAffine,Uo as computeUvSampleRect,Ei as conePolygons,Cf as convexHull2D,Ca as cornerShapeDiagonal,Ad as cornerShapeGeometryForPlan,Ea as cornerShapePointSides,ar as coverPlanarPolygons,vc as createIsometricCamera,wf as createPolyAnimationMixer,Ld as crossVec,Oo as cssBorderShapeForGeometry,Td as cssBorderShapeForPlan,He as cssPoints,$e as cullInteriorPolygons,vt as cylinderPolygons,qc as dedupeOverlappingPolygons,Si as dodecahedronPolygons,Vo as dominantCountKey,Kt as dotVec,_f as ensureCcw2D,Tc as eulerXYZFromQuat,Na as evaluateIslandAxis,Mn as expandClipPoints,bd as filterAtlasPlans,lt as findOverlappingPolygonDuplicates,nd as formatAffineMatrix3dColumns,ho as formatAffineMatrix3dScalars,xo as formatAffineMatrix3dTransformScalars,bo as formatAtlasMatrix,_d as formatBorderShapeElementStyle,Cd as formatBorderShapeEntryMatrix,td as formatBorderShapeMatrix,rt as formatColor,vd as formatCornerShapeElementStyle,ed as formatCssLength,od as formatCssLengthPx,rd as formatMatrix3d,je as formatMatrix3dValues,Ye as formatPercent,Kn as formatScaledMatrixFromPlan,sd as formatSolidQuadEntryMatrix,pa as formatSolidQuadMatrix,Pa as fullRectBounds,Oa as getPolygonBasisInfo,yd as getSolidPaintDefaultsForPlansCore,Mi as icosahedronPolygons,Io as incrementCount,Po as intersect2DLines,id as intersect2DLinesRaw,Pc as inverseRotateVec3,ui as isAxisAlignedSurfaceNormal,Tf as isBakedShadowCaster,Va as isBasisOptimizable,qn as isConvexPolygonPoints,Fa as isFullRectBasis,tn as isFullRectSolid,Yt as isProjectiveQuadPlan,Tn as isSolidTrianglePlan,mf as isVoxelCameraCullableNormalGroups,Wm as loadMesh,Cn as makeLocalBasis,Re as mergePolygons,Lr as normalFacesCamera,tt as normalizeAtlasScale,Cc as normalizeInvertMultiplier,Jt as normalizePolygons,So as normalizedSeamBleed,yi as octahedronPolygons,Sn as offsetConvexPolygonPoints,Zn as offsetConvexPolygonPointsByEdgeAmounts,Ao as offsetStableTrianglePoints,Gt as offsetTrianglePoints,Vm as optimizeAnimatedMeshPolygons,Er as optimizeMeshPolygons,nt as packTextureAtlasPlans,kd as packTextureAtlasPlansWithScaleCore,Xt as parseAlpha,an as parseColor,Gr as parseGltf,ke as parseHex,Zo as parseHexColor,Hr as parseMtl,jr as parseObj,_n as parsePureColor,Qo as parseRgbColor,qr as parseVox,bi as planePolygons,Aa as polygonContainsPoint,Rr as polygonCssSurfaceNormal,pc as polygonFaces,uf as polygonFacesCamera,Ci as polygonSignedArea2D,vf as projectCssVertexToGround,Ba as projectTextureTriangle,vo as quantizeCssColor,Sc as quatFromAxisAngle,Ec as quatFromEulerXYZ,Mc as quatMultiply,Pr as repairMeshSeams,va as resolveProjectiveQuadGuards,$t as resolveSeamBleed,dd as rgbEqual,_o as rgbKey,pd as rgbToCss,Wt as rgbToHex,hi as ringPolygons,xi as ringQuadPolygons,it as rotateVec3,ue as roundDecimal,xd as safariCssProjectiveUnsupported,zt as safePlanSeamBleedAmount,To as seamBleedAmountArray,$s as seamFacetSplitPolygons,mu as seamFacetSplitReport,Do as seamLightBrightness,du as seamOverlapDiagnostics,zs as seamOverlapPolygons,gu as seamOverlapReport,lc as shadeColor,Jn as shadePolygon,Ta as sharedCornerShapeSide,yo as signedArea2D,Sa as simplifyCornerShapePoints,Pi as spherePolygons,ha as stableBasisFromPlan,Mo as stableTriangleMatrixDecimals,gd as stepRgbToward,Ai as tetrahedronPolygons,jt as textureTintFactors,md as tintToCss,Ti as torusPolygons};
1
+ var $l="cubic";function mi(e){let n=e.replace("#","");if(n.length===3){let t=parseInt(n[0]+n[0],16),o=parseInt(n[1]+n[1],16),r=parseInt(n[2]+n[2],16);return Number.isNaN(t)||Number.isNaN(o)||Number.isNaN(r)?null:{rgb:[t,o,r],alpha:1}}if(n.length===6){let t=parseInt(n.slice(0,2),16),o=parseInt(n.slice(2,4),16),r=parseInt(n.slice(4,6),16);return Number.isNaN(t)||Number.isNaN(o)||Number.isNaN(r)?null:{rgb:[t,o,r],alpha:1}}return null}function di(e){let n=e.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)/i);return n?{rgb:[Number(n[1]),Number(n[2]),Number(n[3])],alpha:n[4]?Number(n[4]):1}:null}function Pn(e){if(!e)return null;let n=e.trim(),t=mi(n);return t||di(n)}function _n(e){return Math.max(0,Math.min(255,Math.round(e)))}function Ht(e){let[n,t,o]=e.rgb.map(_n);return e.alpha<1?`rgba(${n}, ${t}, ${o}, ${e.alpha})`:`rgb(${n}, ${t}, ${o})`}var Ho={rgb:[204,204,204],alpha:1},pi=new Map;function Wn(e){if(!e)return null;let n=e.trim(),t=pi.get(n);if(t)return t;let o=Pn(n);return o?(pi.set(n,o),o):null}function Xl(e,n){let t=Wn(e)??Ho,o=[_n(t.rgb[0]+n),_n(t.rgb[1]+n),_n(t.rgb[2]+n)];return Ht({rgb:o,alpha:t.alpha})}var Ko={direction:[0,0,-1],color:"#ffffff",intensity:1},gi={color:"#ffffff",intensity:.4};function hi(e){let n=Math.hypot(e[0],e[1],e[2]);return n<1e-12?[0,0,0]:[e[0]/n,e[1]/n,e[2]/n]}function bi(e,n,t){let o=Wn(n)??Ho;return e*(o.rgb[t]/255)}function Yl(e,n,t,o){let r=Wn(n)??Ho,s=hi(t?.direction??Ko.direction),i=t?.color??Ko.color,c=Math.max(0,t?.intensity??Ko.intensity),a=o?.color??gi.color,l=Math.max(0,o?.intensity??gi.intensity),u=hi(e),f=Math.max(0,-(u[0]*s[0]+u[1]*s[1]+u[2]*s[2])),d=c*f,m=[0,0,0];for(let p=0;p<3;p=p+1){let h=r.rgb[p],g=bi(h,a,p)*l,b=bi(h,i,p)*d;m[p]=_n(g+b)}return Ht({rgb:m,alpha:r.alpha})}var yi="#cccccc",Zt=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],xi=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],Pi=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],qt=e=>Math.hypot(e[0],e[1],e[2]);function Wl(e){let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0;for(let u of e)u[0]<n&&(n=u[0]),u[0]>r&&(r=u[0]),u[1]<t&&(t=u[1]),u[1]>s&&(s=u[1]),u[2]<o&&(o=u[2]),u[2]>i&&(i=u[2]);let c=r-n,a=s-t,l=i-o;return Math.hypot(c,a,l)}function jl(){let e=globalThis;if(e.__POLYCSS_DEV__===!0)return!0;if(e.__POLYCSS_DEV__===!1)return!1;let n=e.process?.env?.NODE_ENV;return typeof n=="string"?n!=="production":!1}function Zo(e){let n=[],t=[];if(!e||e.length===0)return{polygons:n,warnings:t};for(let o=0;o<e.length;o++){let r=e[o];if(!r||!Array.isArray(r.vertices)){t.push(`Polygon ${o}: missing vertices, dropped`);continue}let s=r.vertices;if(s.length<3){t.push(`Polygon ${o}: ${s.length} vertices (need >= 3), dropped`);continue}let i=Zt(s[1],s[0]),c=Zt(s[2],s[0]),a=xi(i,c),l=qt(a);if(s.length===3){if(l<1e-12){qt(i)<1e-12||qt(c)<1e-12?t.push(`Polygon ${o}: zero-area triangle (coincident vertices), dropped`):t.push(`Polygon ${o}: vertices collinear, dropped`);continue}}else if(l<1e-12){t.push(`Polygon ${o}: first 3 vertices collinear, dropped`);continue}let u=[{...r,vertices:s.slice()}];if(s.length>=4){let f=[a[0]/l,a[1]/l,a[2]/l],d=Pi(f,s[0]),m=Wl(s),p=Math.max(1e-6,m*.001),h=0;for(let g=3;g<s.length;g++){let b=Math.abs(Pi(f,s[g])-d);b>h&&(h=b)}if(h>p){let g=[],b=r.uvs&&r.uvs.length===s.length?r.uvs:void 0;for(let y=1;y<s.length-1;y++){let x=[s[0].slice(),s[y].slice(),s[y+1].slice()],P=xi(Zt(x[1],x[0]),Zt(x[2],x[0]));if(qt(P)<1e-12)continue;let S={...r,vertices:x};b&&(S.uvs=[b[0].slice(),b[y].slice(),b[y+1].slice()]),g.push(S)}if(g.length===0){t.push(`Polygon ${o}: ${s.length} non-coplanar vertices, fan-triangulation produced no valid triangles, dropped`);continue}t.push(`Polygon ${o}: ${s.length} non-coplanar vertices, fan-triangulated to ${g.length} triangles`),u=g}}for(let f of u){let d=Kl(f,o,t);d&&n.push(d)}}return{polygons:n,warnings:t}}function Kl(e,n,t){let o={vertices:e.vertices},r=e.texture;if(typeof r=="string"&&r===""&&(r=void 0),e.color!==void 0&&r!==void 0&&jl()&&t.push(`Polygon ${n}: color and texture both set; texture wins`),e.color!==void 0&&(Wn(e.color)?o.color=e.color:(t.push(`Polygon ${n}: invalid color "${e.color}", replaced with ${yi}`),o.color=yi)),r!==void 0&&(o.texture=r,e.textureWrap&&(o.textureWrap={...e.textureWrap}),e.textureAlphaMode&&(o.textureAlphaMode=e.textureAlphaMode)),e.doubleSided===!0&&(o.doubleSided=!0),e.uvs!==void 0&&(!Array.isArray(e.uvs)||e.uvs.length!==e.vertices.length?t.push(`Polygon ${n}: uvs length ${Array.isArray(e.uvs)?e.uvs.length:"?"} != vertices length ${e.vertices.length}, uvs stripped`):o.uvs=e.uvs),e.data!==void 0&&e.data!==null&&typeof e.data=="object"){let s={},i=!1;for(let c of Object.keys(e.data)){let a=e.data[c];typeof a=="string"||typeof a=="number"||typeof a=="boolean"?s[c]=a:(i=!0,t.push(`Polygon ${n}: data["${c}"] has non-primitive value, key dropped`))}Object.keys(s).length>0&&(o.data=s)}return o}function Mi(e){if(!e||e.length===0)return{min:[0,0,0],max:[0,0,0]};let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0,c=!1;for(let a of e)if(a?.vertices)for(let l of a.vertices)l&&(c=!0,l[0]<n&&(n=l[0]),l[0]>r&&(r=l[0]),l[1]<t&&(t=l[1]),l[1]>s&&(s=l[1]),l[2]<o&&(o=l[2]),l[2]>i&&(i=l[2]));return c?{min:[n,t,o],max:[r,s,i]}:{min:[0,0,0],max:[0,0,0]}}function Hl(e){let n=e.polygons??[],t,o;if(e.skipNormalize)t=n,o=[];else{let c=Zo(n);t=c.polygons,o=c.warnings}let r=Mi(t),s=[r.max[0]-r.min[0],r.max[1]-r.min[1],r.max[2]-r.min[2]];return{context:{polygons:t,sceneBbox:r,warnings:o},dimensions:{sceneBbox:r,size:s},warnings:o}}function Zl(e){return!e.vertices||e.vertices.length<3?[]:[{v:e.vertices.map(n=>[n[0],n[1],n[2]]),color:e.color}]}function ql(e){let n=e[0],t=e[1],o=e[2],r=[t[0]-n[0],t[1]-n[1],t[2]-n[2]],s=[o[0]-n[0],o[1]-n[1],o[2]-n[2]],i=r[1]*s[2]-r[2]*s[1],c=r[2]*s[0]-r[0]*s[2],a=r[0]*s[1]-r[1]*s[0],l=Math.hypot(i,c,a);return l<=1e-9?null:[i/l,c/l,a/l]}function Ql(e){let n=0;for(let t=0;t<e.length;t++){let[o,r]=e[t],[s,i]=e[(t+1)%e.length];n+=o*i-s*r}return Math.abs(n)/2}function Jl(e,n,t){if(!e||e.length<3)return null;let o=e.map(c=>[c[1]*n,c[0]*n,c[2]*t]),r=ql(o);if(!r)return null;let s=1/0,i=0;for(let c=0;c<o.length;c++){let a=o[c],l=o[(c+1)%o.length],u=[l[0]-a[0],l[1]-a[1],l[2]-a[2]],f=u[0]*r[0]+u[1]*r[1]+u[2]*r[2],d=[u[0]-f*r[0],u[1]-f*r[1],u[2]-f*r[2]],m=Math.hypot(d[0],d[1],d[2]);if(m<=1e-9)continue;let p=[d[0]/m,d[1]/m,d[2]/m],h=[r[1]*p[2]-r[2]*p[1],r[2]*p[0]-r[0]*p[2],r[0]*p[1]-r[1]*p[0]],g=Math.hypot(h[0],h[1],h[2]);if(g<=1e-9)continue;let b=[h[0]/g,h[1]/g,h[2]/g],y=o.map(v=>{let I=v[0]-a[0],_=v[1]-a[1],O=v[2]-a[2];return[I*p[0]+_*p[1]+O*p[2],I*b[0]+_*b[1]+O*b[2]]}),x=1/0,P=1/0,S=-1/0,C=-1/0;for(let[v,I]of y)x=Math.min(x,v),P=Math.min(P,I),S=Math.max(S,v),C=Math.max(C,I);let T=S-x,A=C-P,M=Math.max(1,Math.ceil(T))*Math.max(1,Math.ceil(A)),E=Ql(y);E<=1e-9||M<s&&(s=M,i=E)}return!Number.isFinite(s)||i<=1e-9?null:{elementArea:s,polygonArea:i}}function eu(e,n={}){let t=n.tileSize??100,o=n.layerElevation??t,r=n.texturedOnly??!0,s=0,i=0,c=0,a=0,l=0;for(let f of e){let d=!!f.texture;if(r&&!d)continue;let m=Jl(f.vertices,t,o);m&&(s+=1,d&&(i+=1),c+=m.elementArea,a+=m.polygonArea,l=Math.max(l,Math.max(0,m.elementArea-m.polygonArea)/m.elementArea))}let u=Math.max(0,c-a);return{totalPolygons:e.length,measuredPolygons:s,texturedPolygons:i,elementArea:c,polygonArea:a,transparentArea:u,transparentRatio:c>0?u/c:0,overdrawRatio:a>0?c/a:0,worstTransparentRatio:l}}function qo(e,n,t,o){let r=n*Math.PI/180,s=t*Math.PI/180,i=o*Math.PI/180,[c,a,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[c,a]=[c*u-a*f,c*f+a*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[c,l]=[c*u+l*f,-c*f+l*u]}if(r!==0){let u=Math.cos(r),f=Math.sin(r);[a,l]=[a*u-l*f,a*f+l*u]}return[c,a,l]}function nu(e,n){let t=-n[0]*Math.PI/180,o=-n[1]*Math.PI/180,r=-n[2]*Math.PI/180,[s,i,c]=e;if(t!==0){let a=Math.cos(t),l=Math.sin(t);[i,c]=[i*a-c*l,i*l+c*a]}if(o!==0){let a=Math.cos(o),l=Math.sin(o);[s,c]=[s*a+c*l,-s*l+c*a]}if(r!==0){let a=Math.cos(r),l=Math.sin(r);[s,i]=[s*a-i*l,s*l+i*a]}return[s,i,c]}function On(e,n){let[t,o,r]=e,s=-(n[2]??0)*Math.PI/180;if(s!==0){let a=Math.cos(s),l=Math.sin(s);[t,o]=[t*a-o*l,t*l+o*a]}let i=-(n[1]??0)*Math.PI/180;if(i!==0){let a=Math.cos(i),l=Math.sin(i);[o,r]=[o*a-r*l,o*l+r*a]}let c=-(n[0]??0)*Math.PI/180;if(c!==0){let a=Math.cos(c),l=Math.sin(c);[t,r]=[t*a+r*l,-t*l+r*a]}return[t,o,r]}var Qo=Math.PI/180,xt=180/Math.PI,tu=[1,0,0,0];function ou(e,n){let[t,o,r,s]=e,[i,c,a,l]=n;return[t*i-o*c-r*a-s*l,t*c+o*i+r*l-s*a,t*a-o*l+r*i+s*c,t*l+o*a-r*c+s*i]}function ru(e,n){let t=n*.5,o=Math.sin(t);return[Math.cos(t),e[0]*o,e[1]*o,e[2]*o]}function su(e){let n=e[0]*Qo,t=e[1]*Qo,o=e[2]*Qo,r=Math.cos(n*.5),s=Math.sin(n*.5),i=Math.cos(t*.5),c=Math.sin(t*.5),a=Math.cos(o*.5),l=Math.sin(o*.5);return[r*i*a-s*c*l,s*i*a+r*c*l,r*c*a-s*i*l,r*i*l+s*c*a]}function iu(e){let[n,t,o,r]=e,s=2*(t*r+n*o),i=2*(o*r-n*t),c=1-2*(t*t+o*o),a=2*(t*o-n*r),l=1-2*(o*o+r*r),u=Math.max(-1,Math.min(1,s)),f=Math.asin(u);return Math.abs(u)<.99999?[Math.atan2(-i,c)*xt,f*xt,Math.atan2(-a,l)*xt]:[Math.atan2(2*(o*r+n*t),1-2*(t*t+r*r))*xt,f*xt,0]}var $e=50;function au(e){if(typeof e=="number")return e===0?void 0:e<0?-1:1;if(typeof e=="boolean")return e?-1:1}var Kn={target:[0,0,0],rotX:65,rotY:45,zoom:.65,distance:0},Ai=100,Si=1e4,jn=e=>Math.round(e*Ai)/Ai,cu=e=>Math.round(e*Si)/Si;function lu(e={}){let n={target:e.target??[...Kn.target],rotX:e.rotX??Kn.rotX,rotY:e.rotY??Kn.rotY,zoom:e.zoom??Kn.zoom,distance:e.distance??Kn.distance};function t(r){r.target!==void 0&&(n.target=[jn(r.target[0]),jn(r.target[1]),jn(r.target[2])]),r.rotX!==void 0&&(n.rotX=jn(r.rotX)),r.rotY!==void 0&&(n.rotY=jn(r.rotY)),r.zoom!==void 0&&(n.zoom=cu(r.zoom)),r.distance!==void 0&&(n.distance=jn(r.distance))}function o(r={}){let i=(r.cols??0)*50,c=(r.rows??0)*50,[a,l,u]=n.target,f=l*50,d=a*50,m=u*50;return{transform:`${n.distance!==0?` translateZ(${-n.distance}px)`:""}scale(${n.zoom}) rotateX(${n.rotX}deg) rotate(${n.rotY}deg) translate3d(${-f}px, ${-d}px, ${-m}px)`,width:`${i}px`,height:`${c}px`}}return{state:n,update:t,getStyle:o}}var Ci=Math.PI/180;function Ei(e){let{camera:n,tileSize:t,viewportCenterX:o,viewportCenterY:r,mouseX:s,mouseY:i}=e,c=n.rotX*Ci,a=n.rotY*Ci,l=n.zoom/t,u=n.target,f=s-o,d=i-r,m=f/l,p=d/l,h=Math.cos(c),g=Math.sin(c),b=m,y=p*h,x=-p*g,P=0,S=g,C=h,T=Math.cos(a),A=Math.sin(a),M=b*T+y*A,E=-b*A+y*T,v=x,I=P*T+S*A,_=-P*A+S*T,O=C,V=u[1]*t,w=u[0]*t,F=u[2]*t,D=M+V,H=E+w,K=v+F,Q=[H/t,D/t,K/t],ne=[_/t,I/t,O/t];return{origin:Q,direction:ne}}function uu(e){let{origin:n,direction:t}=Ei(e),{sphereCenter:o,sphereRadius:r}=e,s=n[0]-o[0],i=n[1]-o[1],c=n[2]-o[2],a=t[0],l=t[1],u=t[2],f=a*a+l*l+u*u;if(f===0)return null;let d=2*(s*a+i*l+c*u),m=s*s+i*i+c*c-r*r,p=d*d-4*f*m;if(p>=0){let T=Math.sqrt(p),A=(-d-T)/(2*f),M=(-d+T)/(2*f),E=t[2]>=0?Math.max(A,M):Math.min(A,M);return[n[0]+E*t[0],n[1]+E*t[1],n[2]+E*t[2]]}let h=-(s*a+i*l+c*u)/f,g=n[0]+h*a,b=n[1]+h*l,y=n[2]+h*u,x=g-o[0],P=b-o[1],S=y-o[2],C=Math.hypot(x,P,S);return C===0?null:[o[0]+x/C*r,o[1]+P/C*r,o[2]+S/C*r]}var Mn=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Qt=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],Pt=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],Jo=e=>Math.hypot(e[0],e[1],e[2]),Hn=(e,n)=>e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2];function fu(e){return`${e[0]},${e[1]},${e[2]}`}function vi(e){if(e.length<3)return null;let n=Mn(e[1],e[0]),t=Mn(e[2],e[0]),o=Qt(n,t),r=Jo(o);if(r<1e-12)return null;let s=[o[0]/r,o[1]/r,o[2]/r],i=Pt(s,e[0]);return{normal:s,d:i}}function mu(e,n){return Pt(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.05}function du(e,n){return Pt(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.001}function pu(e,n,t,o){let r=e.vertices.findIndex(A=>Hn(A,t)),s=e.vertices.findIndex(A=>Hn(A,o)),i=n.vertices.findIndex(A=>Hn(A,t)),c=n.vertices.findIndex(A=>Hn(A,o));if(r<0||s<0||i<0||c<0)return null;let a=e.vertices.length,l=n.vertices.length,u=(r+1)%a===s,f=(i+1)%l===c;if(u===f)return null;let d=u?s:r,m=u?r:s,p=f?c:i,h=f?i:c,g=!!(e.uvs&&n.uvs),b=[],y=g?[]:void 0,x=d;for(;b.push(e.vertices[x]),y&&y.push(e.uvs[x]),x!==m;)x=(x+1)%a;for(x=p;b.push(n.vertices[x]),y&&y.push(n.uvs[x]),x!==h;)x=(x+1)%l;let P=[],S=y?[]:void 0;for(let A=0;A<b.length;A++)(P.length===0||!Hn(b[A],P[P.length-1]))&&(P.push(b[A]),S&&y&&S.push(y[A]));if(P.length>1&&Hn(P[0],P[P.length-1])&&(P.pop(),S?.pop()),g)return Ti(P,S);let C=[],T=S?[]:void 0;for(let A=0;A<P.length;A++){let M=P[(A-1+P.length)%P.length],E=P[A],v=P[(A+1)%P.length],I=Qt(Mn(E,M),Mn(v,M));Jo(I)>1e-9&&(C.push(E),T&&S&&T.push(S[A]))}return C.length<3?null:Ti(C,T)}function gu(e,n){let t=e.length,o=0;for(let r=0;r<t;r++){let s=e[r],i=e[(r+1)%t],c=e[(r+2)%t],a=Mn(i,s),l=Mn(c,i),u=Pt(Qt(a,l),n);if(Math.abs(u)<1e-9)continue;let f=u>0?1:-1;if(o===0)o=f;else if(f!==o)return!1}return!0}function hu(e,n){if(e.length<3)return!1;let t=vi(e);if(!t)return!1;for(let o of e)if(Math.abs(Pt(t.normal,o)-t.d)>n)return!1;return!0}function bu(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function yu(e,n){return(e?.s??"")===(n?.s??"")&&(e?.t??"")===(n?.t??"")}function xu(e,n){return(e??"")===(n??"")}function Pu(e,n){let t=[];for(let o=1;o<e.length-1;o++)t.push({vertices:[[...e[0]],[...e[o]],[...e[o+1]]],uvs:[[...n[0]],[...n[o]],[...n[o+1]]]});return t}function Ti(e,n){for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length];if(!(Jo(Qt(Mn(r,o),Mn(s,o)))<=1e-9))return t===0?{vertices:e,uvs:n}:{vertices:[...e.slice(t),...e.slice(0,t)],uvs:n?[...n.slice(t),...n.slice(0,t)]:void 0}}return{vertices:e,uvs:n}}function an(e){let n=[],t=[],o=new WeakMap,r=m=>{let p=o.get(m);if(p)return p;let h=fu(m);return o.set(m,h),h},s=(m,p)=>{let h=r(m),g=r(p);return h<g?`${h}|${g}`:`${g}|${h}`},i=(m,p)=>r(m)<r(p)?1:-1,c=m=>{let p=new Map;for(let h=0;h<m.length;h++){let g=m[h],b=m[(h+1)%m.length];p.set(s(g,b),i(g,b))}return p},a=0,l=!1,u=m=>(a+=m,a<=8e6?!0:(l=!0,!1));for(let m of e??[]){if(!m||!m.vertices||m.vertices.length<3){m&&n.push(m);continue}let p=m.vertices,h=vi(p);if(!h){n.push(m);continue}let g=m.texture&&m.uvs&&m.uvs.length===p.length?m.uvs.map(y=>[y[0],y[1]]):void 0,b=m.texture&&g?m.textureTriangles?.length?bu(m.textureTriangles):Pu(p,g):void 0;t.push({vertices:p,uvs:g,color:m.color??"#cccccc",texture:m.texture,textureWrap:m.texture?m.textureWrap:void 0,textureAlphaMode:m.texture?m.textureAlphaMode:void 0,textureTriangles:b,normal:h.normal,d:h.d,edgeDirections:c(p),alive:!0,doubleSided:m.doubleSided===!0,data:m.data})}let f=()=>{let m=new Map;for(let g=0;g<t.length;g++){let b=t[g];if(!b.alive)continue;let y=b.vertices.length;if(!u(y))return!1;for(let x=0;x<y;x++){let P=b.vertices[x],S=b.vertices[(x+1)%y],C=s(P,S),T=m.get(C);T?T.second<0&&(T.second=g):(T={a:P,b:S,first:g,second:-1},m.set(C,T))}}let p=!1,h=(g,b,y)=>{let x=g.edgeDirections.get(s(b,y));return x?x===i(b,y)?1:-1:0};for(let g of m.values()){if(g.second<0)continue;let b=g.first,y=g.second;if(b===y)continue;let x=t[b],P=t[y];if(!x.alive||!P.alive||x.color!==P.color||x.texture!==P.texture||!yu(x.textureWrap,P.textureWrap)||!xu(x.textureAlphaMode,P.textureAlphaMode)||x.doubleSided!==P.doubleSided)continue;let S=!!(x.texture||P.texture);if(S&&(!x.textureTriangles||!P.textureTriangles)||!!x.uvs!=!!P.uvs||(S?!du(x,P):!mu(x,P)))continue;if(!u(x.vertices.length+P.vertices.length))return!1;let C=h(x,g.a,g.b);if(C===0)continue;let T=h(P,g.a,g.b);if(T===0||C===T)continue;let[A,M]=C===1?[g.a,g.b]:[g.b,g.a],E=pu(x,P,A,M);if(E){if(!u(E.vertices.length))return!1;E.vertices.length>96||hu(E.vertices,.001)&&gu(E.vertices,x.normal)&&(x.vertices=E.vertices,x.uvs=E.uvs,x.edgeDirections=c(E.vertices),x.textureTriangles=S?[...x.textureTriangles??[],...P.textureTriangles??[]]:void 0,P.alive=!1,p=!0)}}return p},d=0;for(;!l&&d<16&&f();)d++;for(let m of t){if(!m.alive)continue;let p={vertices:m.vertices.map(h=>[h[0],h[1],h[2]]),color:m.color};m.texture&&(p.texture=m.texture),m.texture&&m.textureWrap&&(p.textureWrap={...m.textureWrap}),m.texture&&m.textureAlphaMode&&(p.textureAlphaMode=m.textureAlphaMode),m.uvs&&(p.uvs=m.uvs.map(h=>[h[0],h[1]])),m.textureTriangles?.length&&(p.textureTriangles=m.textureTriangles),m.doubleSided&&(p.doubleSided=!0),m.data&&(p.data=m.data),n.push(p)}return n}var Mu=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Au=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],en=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2];function Su(e,n){let t=e.vertices;if(!t||t.length<3)return null;let o=0,r=0,s=0;for(let h=0;h<t.length;h++){let g=t[h],b=t[(h+1)%t.length];o+=(g[1]-b[1])*(g[2]+b[2]),r+=(g[2]-b[2])*(g[0]+b[0]),s+=(g[0]-b[0])*(g[1]+b[1])}let i=Math.hypot(o,r,s);if(i<1e-12)return null;let c=[o/i,r/i,s/i],a=0,l=0,u=0;for(let h of t)a+=h[0],l+=h[1],u+=h[2];let f=1/t.length,d=[a*f,l*f,u*f],m=en(c,d),p=i*.5;return{index:n,polygon:e,normal:c,d:m,centroid:d,area:p,local2D:null,bbox2D:null,basis:null}}function Cu(e,n){return e.polygon.doubleSided===!0&&n.polygon.doubleSided===!0&&en(e.normal,n.normal)<0}function Eu(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=n[0]-e[0]*en(n,e),o=n[1]-e[1]*en(n,e),r=n[2]-e[2]*en(n,e),s=Math.hypot(t,o,r);t/=s,o/=s,r/=s;let i=[t,o,r],c=Au(e,i);return{u:i,v:c}}function Tu(e){if(e.local2D)return;let n=Eu(e.normal),t=[],o=1/0,r=1/0,s=-1/0,i=-1/0;for(let c of e.polygon.vertices){let a=en(c,n.u),l=en(c,n.v);t.push([a,l]),a<o&&(o=a),a>s&&(s=a),l<r&&(r=l),l>i&&(i=l)}e.local2D=t,e.bbox2D={min:[o,r],max:[s,i]},e.basis=n}function vu(e,n){let t=[],o=1/0,r=1/0,s=-1/0,i=-1/0;for(let c of e.vertices){let a=en(c,n.u),l=en(c,n.v);t.push([a,l]),a<o&&(o=a),a>s&&(s=a),l<r&&(r=l),l>i&&(i=l)}return{local2D:t,bbox2D:{min:[o,r],max:[s,i]}}}function Iu(e,n){return e.max[0]>=n.min[0]&&e.min[0]<=n.max[0]&&e.max[1]>=n.min[1]&&e.min[1]<=n.max[1]}function Ii(e,n){let t=!1;for(let o=0,r=n.length-1;o<n.length;r=o++){let s=n[o],i=n[r];s[1]>e[1]!=i[1]>e[1]&&e[0]<(i[0]-s[0])*(e[1]-s[1])/(i[1]-s[1]+1e-30)+s[0]&&(t=!t)}return t}function _i(e){let n=0,t=0;for(let o of e)n+=o[0],t+=o[1];return[n/e.length,t/e.length]}function _u(e,n){let t=_i(e),o=_i(n),r=1e-4,s=0;for(let l of e){let u=[l[0]+(t[0]-l[0])*r,l[1]+(t[1]-l[1])*r];Ii(u,n)&&s++}let i=0;for(let l of n){let u=[l[0]+(o[0]-l[0])*r,l[1]+(o[1]-l[1])*r];Ii(u,e)&&i++}let c=s/e.length,a=i/n.length;return Math.max(c,a)}function Ou(e,n){let t=e.normal[0],o=e.normal[1],r=e.normal[2],s=Math.abs(t),i=Math.abs(o),c=Math.abs(r),a=t;i>s&&i>c?a=o:c>s&&c>i&&(a=r),a<0&&(t=-t,o=-o,r=-r);let l=Math.round(t/.05),u=Math.round(o/.05),f=Math.round(r/.05),d=e.d*(e.normal[0]===t&&e.normal[1]===o&&e.normal[2]===r?1:-1),m=Math.round(d/(n*2));return`${l},${u},${f}|${m}`}function Vu(e,n,t,o){let r=en(e.normal,n.normal);if(Math.abs(r)<1-t)return!1;let s=r>0?1:-1;return Math.abs(e.d-s*n.d)<o}function Oi(e,n){let t=Mu(n,e.centroid);return en(e.normal,t)>0}function Jt(e,n){if(!e||e.length<2)return new Set;let t=n?.normalTolerance??.001,o=n?.distanceTolerance??.05,r=n?.overlapFraction??.7,s=n?.preserveDoubleSidedBackfaces??!0,i=[];for(let p=0;p<e.length;p++){let h=Su(e[p],p);h&&i.push(h)}if(i.length<2)return new Set;let c=0,a=0,l=0,u=0;for(let p of i)c+=p.centroid[0]*p.area,a+=p.centroid[1]*p.area,l+=p.centroid[2]*p.area,u+=p.area;let f=u>0?[c/u,a/u,l/u]:[0,0,0],d=new Map;for(let p of i){let h=Ou(p,o),g=d.get(h);g||(g=[],d.set(h,g)),g.push(p)}let m=new Set;for(let p of d.values())if(!(p.length<2))for(let h=0;h<p.length;h++){let g=p[h];if(!m.has(g.index))for(let b=h+1;b<p.length;b++){let y=p[b];if(m.has(y.index)||!Vu(g,y,t,o)||s&&Cu(g,y))continue;Tu(g);let x=vu(y.polygon,g.basis);if(!Iu(g.bbox2D,x.bbox2D)||_u(g.local2D,x.local2D)<r)continue;let S=Oi(g,f),C=Oi(y,f),T;if(S&&!C?T=g:C&&!S?T=y:T=g.area<y.area?g:y,m.add(T.index),T===g)break}}return m}function Ru(e,n){if(!e||e.length<2)return e??[];let t=Jt(e,n);if(t.size===0)return e;let o=[];for(let r=0;r<e.length;r++)t.has(r)||o.push(e[r]);return o}var Lu=4,wu=8,Nu=.001,Du=1e-9,Fu=1e6,Bu=1e6,ku=8,Vn=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],tr=(e,n)=>[e[0]+n[0],e[1]+n[1],e[2]+n[2]],St=(e,n)=>[e[0]*n,e[1]*n,e[2]*n],Rn=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],wi=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],Ni=e=>Math.hypot(e[0],e[1],e[2]);function Ct(e){let n=Ni(e);return n<=Du?null:[e[0]/n,e[1]/n,e[2]/n]}function At(e,n){return Math.round(e*n)/n}function nn(e){return At(e,Fu)}function er(e){return At(e,Bu)}function nr(e,n){return Math.abs(e[0]-n[0])<=1e-7&&Math.abs(e[1]-n[1])<=1e-7}function un(e){return`${nn(e[0])},${nn(e[1])}`}function Uu(e){let n=un(e.a),t=un(e.b);return n<t?`${n}|${t}`:`${t}|${n}`}function Zn(e){return`${e[0]},${e[1]},${e[2]}`}function Di(e,n){let t=Zn(e),o=Zn(n);return t<o?`${t}|${o}`:`${o}|${t}`}function or(e){return e?Object.keys(e).sort().map(n=>`${n}:${String(e[n])}`).join("|"):""}function Vi(e){return[e.color??"#cccccc",e.texture??"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided",or(e.data)].join("|")}function Fi(e){let n=e.vertices;if(!n||n.length<3)return null;let t=n[0],o=[0,0,0];for(let s=1;s<n.length-1;s++)o=tr(o,wi(Vn(n[s],t),Vn(n[s+1],t)));let r=Ct(o);return r?{normal:r,d:Rn(r,t)}:null}function Gu(e,n,t){return Rn(e.normal,n.normal)>1-t&&Math.abs(e.d-n.d)<=t}function zu(e){return!e.texture&&!e.uvs&&!e.textureTriangles}function $u(e,n){let t=n[e[0]]?.data,o=or(t);for(let r of e)if(or(n[r].data)!==o)return;return t?{...t}:void 0}function Xu(e,n){return e.every(t=>n[t].doubleSided===!0)}function Yu(e,n){let t=e.map(a=>zu(a)?Fi(a):null),o=t.map(Boolean),r=new Map;for(let a=0;a<e.length;a++){if(!t[a])continue;let l=e[a].vertices;for(let u=0;u<l.length;u++){let f=Di(l[u],l[(u+1)%l.length]),d=r.get(f),m={polygon:a,edge:u};d?d.push(m):r.set(f,[m])}}let s=e.map(()=>new Set);for(let a of r.values())if(!(a.length<2))for(let l=0;l<a.length;l++)for(let u=l+1;u<a.length;u++){let f=a[l].polygon,d=a[u].polygon,m=t[f],p=t[d];!m||!p||Vi(e[f])===Vi(e[d])&&Gu(m,p,n)&&(s[f].add(d),s[d].add(f))}let i=new Set,c=[];for(let a=0;a<e.length;a++){if(!o[a]||i.has(a))continue;let l=[],u=[a];for(i.add(a);u.length>0;){let f=u.shift();l.push(f);for(let d of s[f])i.has(d)||(i.add(d),u.push(d))}c.push(l)}return{groups:c}}function Wu(e,n){let t=new Set(e),o=new Map;for(let s of e){let i=n[s].vertices;for(let c=0;c<i.length;c++){let a=Di(i[c],i[(c+1)%i.length]),l=o.get(a),u={polygon:s,edge:c};l?l.push(u):o.set(a,[u])}}let r=[];for(let s of o.values()){let i=s.filter(c=>t.has(c.polygon));if(i.length===1){let c=i[0],a=n[c.polygon].vertices;r.push({a:a[c.edge],b:a[(c.edge+1)%a.length]})}else if(i.length!==2)return null}return r}function ju(e){let n=new Map;for(let t of e)n.set(Zn(t.a),(n.get(Zn(t.a))??0)+1),n.set(Zn(t.b),(n.get(Zn(t.b))??0)+1);for(let t of n.values())if(t%2!==0)return!1;return!0}function Bi(e){let n=[e[0],e[1],e[2]],t=Math.abs(n[0])>=Math.abs(n[1])&&Math.abs(n[0])>=Math.abs(n[2])?0:Math.abs(n[1])>=Math.abs(n[2])?1:2;return n[t]<0?[-n[0],-n[1],-n[2]]:n}function Ku(e){let n=Bi(e);return`${At(n[0],1e3)},${At(n[1],1e3)},${At(n[2],1e3)}`}function Hu(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=Vn(n,St(e,Rn(n,e)));return Ct(t)??[1,0,0]}function Zu(e,n,t){let o=new Map;for(let s of e){let i=Vn(s.b,s.a),c=Vn(i,St(n,Rn(i,n))),a=Ct(c);if(!a)continue;let l=Bi(a),u=Ku(l),f=Ni(c),d=o.get(u);d?d.weight+=f:o.set(u,{axis:l,weight:f})}let r=[...o.values()].sort((s,i)=>i.weight-s.weight).slice(0,t).map(s=>s.axis);return r.length===0&&r.push(Hu(n)),r}function Ri(e,n,t,o){let r=Vn(e,n);return[nn(Rn(r,t)),nn(Rn(r,o))]}function qu(e){let n=[...e].sort((o,r)=>o-r),t=[];for(let o of n)(t.length===0||Math.abs(o-t[t.length-1])>1e-7)&&t.push(o);return t}function Mt(e,n){let[t,o]=e.a,[r,s]=e.b,i=(n-t)/(r-t);return nn(o+(s-o)*i)}function rr(e){let n=[];for(let o of e)(n.length===0||Math.abs(o[0]-n[n.length-1][0])>1e-7||Math.abs(o[1]-n[n.length-1][1])>1e-7)&&n.push(o);n.length>1&&Math.abs(n[0][0]-n[n.length-1][0])<=1e-7&&Math.abs(n[0][1]-n[n.length-1][1])<=1e-7&&n.pop();let t=[];for(let o=0;o<n.length;o++){let r=n[(o-1+n.length)%n.length],s=n[o],i=n[(o+1)%n.length],c=(s[0]-r[0])*(i[1]-s[1])-(s[1]-r[1])*(i[0]-s[0]);Math.abs(c)>1e-8&&t.push(s)}return t}function no(e){let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-r[0]*o[1]}return n/2}function to(e){let n=1/0,t=1/0,o=-1/0,r=-1/0;for(let[s,i]of e)n=Math.min(n,s),t=Math.min(t,i),o=Math.max(o,s),r=Math.max(r,i);return{minX:n,minY:t,maxX:o,maxY:r}}function ki(e,n){return!(e.maxX<n.minX-1e-7||n.maxX<e.minX-1e-7||e.maxY<n.minY-1e-7||n.maxY<e.minY-1e-7)}function eo(e){return Math.abs(no(e))}function Qu(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=1e-8)continue;let c=i>0?1:-1;if(n===0)n=c;else if(n!==c)return!1}return!0}function Ju(e,n,t){let o=t[0]-n[0],r=t[1]-n[1],s=e[0]-n[0],i=e[1]-n[1],c=o*i-r*s,a=Math.hypot(o,r);if(a<=1e-9||Math.abs(c)>Math.max(1e-8,a*1e-8))return!1;let l=s*o+i*r;return l>=-1e-8&&l<=o*o+r*r+1e-8}function ef(e,n,t){let o=t[0]-n[0],r=t[1]-n[1],s=o*o+r*r;return s<=1e-12?0:((e[0]-n[0])*o+(e[1]-n[1])*r)/s}function nf(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],c=o[1]-t[1],a=Math.hypot(r,s),l=Math.hypot(i,c);if(a<=1e-9||l<=1e-9)return!1;let u=r*(t[1]-e[1])-s*(t[0]-e[0]),f=r*(o[1]-e[1])-s*(o[0]-e[0]),d=r*c-s*i,m=Math.max(1e-8,Math.max(a,l)*1e-8);if(Math.abs(u)>m||Math.abs(f)>m||Math.abs(d)>m)return!1;let p=Math.abs(r)>=Math.abs(s),h=Math.min(p?e[0]:e[1],p?n[0]:n[1]),g=Math.max(p?e[0]:e[1],p?n[0]:n[1]),b=Math.min(p?t[0]:t[1],p?o[0]:o[1]),y=Math.max(p?t[0]:t[1],p?o[0]:o[1]);return Math.min(g,y)-Math.max(h,b)>1e-8}function tf(e,n){for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];for(let s=0;s<n.length;s++)if(nf(o,r,n[s],n[(s+1)%n.length]))return!0}return!1}function Li(e,n){let t=[];for(let o=0;o<e.length;o++){let r=e[o],s=e[(o+1)%e.length],i=[{t:0,point:r},{t:1,point:s}];for(let a of n)nr(a,r)||nr(a,s)||Ju(a,r,s)&&i.push({t:ef(a,r,s),point:a});i.sort((a,l)=>a.t-l.t);let c=[];for(let a of i)c.some(l=>Math.abs(l.t-a.t)<=1e-8||nr(l.point,a.point))||c.push(a);for(let a=0;a<c.length-1;a++){let l=c[a].point,u=c[a+1].point;Math.hypot(u[0]-l[0],u[1]-l[1])<=1e-8||t.push({a:[nn(l[0]),nn(l[1])],b:[nn(u[0]),nn(u[1])]})}}return t}function of(e,n,t=to(e),o=to(n)){if(!ki(t,o)||!tf(e,n))return null;let r=[...Li(e,n),...Li(n,e)],s=new Map;for(let b of r){let y=Uu(b),x=s.get(y);x?x.push(b):s.set(y,[b])}let i=!1,c=[];for(let b of s.values()){if(b.length===1){c.push(b[0]);continue}i=!0;let y=b.filter(P=>un(P.a)<un(P.b)).length,x=b.length-y;if(y!==x)return null}if(!i||c.length<3)return null;let a=new Map;for(let b of c){let y=un(b.a);if(a.has(y))return null;a.set(y,b)}let l=c[0],u=un(l.a),f=[],d=new Set,m=u;for(let b=0;b<=c.length;b++){let y=a.get(m);if(!y)return null;let x=`${un(y.a)}>${un(y.b)}`;if(d.has(x))return null;if(d.add(x),f.push(y.a),m=un(y.b),m===u)break}if(m!==u||d.size!==c.length)return null;let p=rr(f);if(p.length<3||!Qu(p))return null;let h=eo(p),g=eo(e)+eo(n);return Math.abs(h-g)>Math.max(1e-5,g*1e-5)?null:no(p)>=0?p:[...p].reverse()}function rf(e){let n=e.map(rr).filter(r=>r.length>=3&&eo(r)>1e-8),t=n.map(to),o=!0;for(;o;){o=!1;for(let r=0;r<n.length;r++){for(let s=r+1;s<n.length;s++){if(!ki(t[r],t[s]))continue;let i=of(n[r],n[s],t[r],t[s]);if(i){n[r]=i,t[r]=to(i),n.splice(s,1),t.splice(s,1),o=!0;break}}if(o)break}}return n}function sf(e,n,t){let o=new Map;return r=>{let s=nn(r[0]),i=nn(r[1]),c=`${s},${i}`,a=o.get(c);if(a)return[a[0],a[1],a[2]];let l=tr(e,tr(St(n,s),St(t,i))),u=[er(l[0]),er(l[1]),er(l[2])];return o.set(c,u),[u[0],u[1],u[2]]}}function af(e,n,t,o,r){let s=t[0]?.a;if(!s)return null;let i=Ct(Vn(r,St(o,Rn(r,o))));if(!i)return null;let c=Ct(wi(o,i));if(!c)return null;let a=[],l=[];for(let b of t){let y=Ri(b.a,s,i,c),x=Ri(b.b,s,i,c);Math.hypot(y[0]-x[0],y[1]-x[1])<=1e-7||(a.push({a:y,b:x}),l.push(y[0],x[0]))}let u=qu(l);if(a.length<3||u.length<2)return null;let f=n[e[0]].color,d=$u(e,n),m=Xu(e,n),p=[];for(let b=0;b<u.length-1;b++){let y=u[b],x=u[b+1];if(x-y<=1e-7)continue;let P=(y+x)/2,S=a.filter(C=>{let T=Math.min(C.a[0],C.b[0]),A=Math.max(C.a[0],C.b[0]);return T<P&&P<A&&Math.abs(C.a[0]-C.b[0])>1e-7}).map(C=>({segment:C,yMid:Mt(C,P)})).sort((C,T)=>C.yMid-T.yMid);if(S.length!==0){if(S.length%2!==0)return null;for(let C=0;C<S.length;C+=2){let T=S[C].segment,A=S[C+1].segment,M=Mt(T,y),E=Mt(T,x),v=Mt(A,y),I=Mt(A,x),_=rr([[y,M],[x,E],[x,I],[y,v]]);if(_.length<3||Math.abs(no(_))<=1e-8)continue;let O=no(_)>0?_:[..._].reverse();p.push(O)}}}if(p.length===0||e.length>2&&p.length>e.length*ku)return null;let h=sf(s,i,c),g=rf(p).map(b=>({vertices:b.map(h),...f?{color:f}:{},...m?{doubleSided:!0}:{},...d?{data:d}:{}}));return an(g)}function cf(e,n,t){let o=Wu(e,n);if(!o||o.length<3||!ju(o))return null;let r=Fi(n[e[0]]);if(!r)return null;let s=null;for(let i of Zu(o,r.normal,t)){let c=af(e,n,o,r.normal,i);c&&(!s||c.length<s.length)&&(s=c)}return!s||s.length>=e.length?null:s}function sr(e,n={}){let t=n.minGroupPolygons??Lu,o=n.maxCandidateAxes??wu,r=n.planeEpsilon??Nu,s=e??[];if(s.length<t)return s;let{groups:i}=Yu(s,r);if(i.length===0)return s;let c=new Map,a=new Set;for(let u of i){if(u.length<t)continue;let f=cf(u,s,o);if(f){c.set(u[0],f);for(let d of u)a.add(d)}}if(c.size===0)return s;let l=[];for(let u=0;u<s.length;u++){let f=c.get(u);if(f){l.push(...f);continue}a.has(u)||l.push(s[u])}return l}function lf(e,n){let t=`${e[0]},${e[1]},${e[2]}`,o=`${n[0]},${n[1]},${n[2]}`;return t<o?`${t}|${o}`:`${o}|${t}`}function uf(e){if(e.length<100)return!1;let n=new Map;for(let o of e){let r=o.vertices;for(let s=0;s<r.length;s++){let i=lf(r[s],r[(s+1)%r.length]);n.set(i,(n.get(i)??0)+1)}}let t=0;for(let o of n.values())o===1&&(t+=1);return t>=128&&t/Math.max(1,n.size)>.2}function ff(e,n,t){let o=Math.abs(e)>.9?0:1,r=Math.abs(e)>.9?1:0,s=r*t,i=-o*t,c=o*n-r*e,a=Math.hypot(s,i,c);return s/=a,i/=a,c/=a,{ux:s,uy:i,uz:c,vx:n*c-t*i,vy:t*s-e*c,vz:e*i-n*s}}function mf(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0;for(let[H,K,Q]of n)t+=H,o+=K,r+=Q;let s=1/n.length;t*=s,o*=s,r*=s;let i=n[0],c=n[1],a=n[2],l=c[0]-i[0],u=c[1]-i[1],f=c[2]-i[2],d=a[0]-i[0],m=a[1]-i[1],p=a[2]-i[2],h=u*p-f*m,g=f*d-l*p,b=l*m-u*d,y=Math.hypot(h,g,b);if(y<1e-9)return null;h/=y,g/=y,b/=y;let{ux:x,uy:P,uz:S,vx:C,vy:T,vz:A}=ff(h,g,b),M=n.length-2,E=new Float64Array(M*9),v=0;for(let H=1;H<n.length-1;H++){let K=n[0],Q=n[H],ne=n[H+1];E[v++]=K[0],E[v++]=K[1],E[v++]=K[2],E[v++]=Q[0],E[v++]=Q[1],E[v++]=Q[2],E[v++]=ne[0],E[v++]=ne[1],E[v++]=ne[2]}let I=0,_=1/0,O=1/0,V=1/0,w=-1/0,F=-1/0,D=-1/0;for(let[H,K,Q]of n){let ne=H-t,ee=K-o,ue=Q-r,U=ne*ne+ee*ee+ue*ue;U>I&&(I=U),H<_&&(_=H),H>w&&(w=H),K<O&&(O=K),K>F&&(F=K),Q<V&&(V=Q),Q>D&&(D=Q)}return{centroid:[t,o,r],normal:[h,g,b],vertices:n,triFlat:E,bcx:t,bcy:o,bcz:r,br2:I,ux:x,uy:P,uz:S,vx:C,vy:T,vz:A,minX:_,minY:O,minZ:V,maxX:w,maxY:F,maxZ:D}}function df(e,n,t,o,r,s,i,c){let a=i[c],l=i[c+1],u=i[c+2],f=i[c+3]-a,d=i[c+4]-l,m=i[c+5]-u,p=i[c+6]-a,h=i[c+7]-l,g=i[c+8]-u,b=r*g-s*h,y=s*p-o*g,x=o*h-r*p,P=f*b+d*y+m*x;if(P>-1e-9&&P<1e-9)return!1;let S=1/P,C=e-a,T=n-l,A=t-u,M=S*(C*b+T*y+A*x);if(M<0||M>1)return!1;let E=T*m-A*d,v=A*f-C*m,I=C*d-T*f,_=S*(o*E+r*v+s*I);return _<0||M+_>1?!1:S*(p*E+h*v+g*I)>.001}function pf(e,n,t,o,r,s,i){let c=i.bcx-e,a=i.bcy-n,l=i.bcz-t,u=c*o+a*r+l*s,f=c-u*o,d=a-u*r,m=l-u*s;if(f*f+d*d+m*m>i.br2)return!1;let p=i.triFlat,h=p.length;for(let g=0;g<h;g+=9)if(df(e,n,t,o,r,s,p,g))return!0;return!1}var Et=9,gf=6,ke=8;function ir(e,n,t,o,r,s){let i=o-e,c=r-n,a=s-t;return i*c+c*a+a*i}function hf(e){let n=[];for(let C=0;C<e.length;C++)e[C]&&n.push(C);let t=n.length,o=new Int32Array(t);for(let C=0;C<t;C++)o[C]=n[C];let r=new Float64Array(t),s=new Float64Array(t),i=new Float64Array(t);for(let C=0;C<t;C++){let T=e[o[C]];r[C]=(T.minX+T.maxX)*.5,s[C]=(T.minY+T.maxY)*.5,i[C]=(T.minZ+T.maxZ)*.5}let c=2*Math.max(1,t)+1,a=new Float64Array(c*Et),l=0,u=new Float64Array(ke),f=new Float64Array(ke),d=new Float64Array(ke),m=new Float64Array(ke),p=new Float64Array(ke),h=new Float64Array(ke),g=new Int32Array(ke),b=new Float64Array(ke-1),y=new Int32Array(ke-1),x=new Float64Array(ke-1),P=new Int32Array(ke-1);function S(C,T){let A=l++,M=A*Et,E=T-C,v=1/0,I=1/0,_=1/0,O=-1/0,V=-1/0,w=-1/0;for(let G=C;G<T;G++){let te=e[o[G]];te.minX<v&&(v=te.minX),te.maxX>O&&(O=te.maxX),te.minY<I&&(I=te.minY),te.maxY>V&&(V=te.maxY),te.minZ<_&&(_=te.minZ),te.maxZ>w&&(w=te.maxZ)}if(a[M]=v,a[M+1]=I,a[M+2]=_,a[M+3]=O,a[M+4]=V,a[M+5]=w,E<=gf)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let F=1/0,D=1/0,H=1/0,K=-1/0,Q=-1/0,ne=-1/0;for(let G=C;G<T;G++)r[G]<F&&(F=r[G]),r[G]>K&&(K=r[G]),s[G]<D&&(D=s[G]),s[G]>Q&&(Q=s[G]),i[G]<H&&(H=i[G]),i[G]>ne&&(ne=i[G]);let ee=K-F,ue=Q-D,U=ne-H;if(ee===0&&ue===0&&U===0)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let $=ir(v,I,_,O,V,w),k=$>0?1/$:0,L=E+1,W=0,Y=0;for(let G=0;G<3;G++){let te=G===0?F:G===1?D:H,ce=G===0?ee:G===1?ue:U;if(ce===0)continue;let be=G===0?r:G===1?s:i,he=ke/ce;u.fill(1/0),f.fill(1/0),d.fill(1/0),m.fill(-1/0),p.fill(-1/0),h.fill(-1/0),g.fill(0);for(let N=C;N<T;N++){let X=(be[N]-te)*he|0;X>=ke&&(X=ke-1);let oe=e[o[N]];oe.minX<u[X]&&(u[X]=oe.minX),oe.maxX>m[X]&&(m[X]=oe.maxX),oe.minY<f[X]&&(f[X]=oe.minY),oe.maxY>p[X]&&(p[X]=oe.maxY),oe.minZ<d[X]&&(d[X]=oe.minZ),oe.maxZ>h[X]&&(h[X]=oe.maxZ),g[X]++}let ye=1/0,ge=1/0,Se=1/0,De=-1/0,R=-1/0,z=-1/0,j=0;for(let N=0;N<ke-1;N++)u[N]<ye&&(ye=u[N]),m[N]>De&&(De=m[N]),f[N]<ge&&(ge=f[N]),p[N]>R&&(R=p[N]),d[N]<Se&&(Se=d[N]),h[N]>z&&(z=h[N]),j+=g[N],b[N]=ir(ye,ge,Se,De,R,z),y[N]=j;let J=1/0,le=1/0,re=1/0,pe=-1/0,xe=-1/0,ie=-1/0,ae=0;for(let N=ke-2;N>=0;N--){let X=N+1;u[X]<J&&(J=u[X]),m[X]>pe&&(pe=m[X]),f[X]<le&&(le=f[X]),p[X]>xe&&(xe=p[X]),d[X]<re&&(re=d[X]),h[X]>ie&&(ie=h[X]),ae+=g[X],x[N]=ir(J,le,re,pe,xe,ie),P[N]=ae}for(let N=0;N<ke-1;N++){if(y[N]===0||P[N]===0)continue;let X=.125+(b[N]*y[N]+x[N]*P[N])*k;X<L&&(L=X,W=G,Y=te+(N+1)/he)}}let se=W===0?r:W===1?s:i,Z=C,B=T-1;for(;Z<=B;)if(se[Z]<Y)Z++;else{let G=o[Z];o[Z]=o[B],o[B]=G;let te=r[Z];r[Z]=r[B],r[B]=te;let ce=s[Z];s[Z]=s[B],s[B]=ce;let be=i[Z];i[Z]=i[B],i[B]=be,B--}let q=Z;(q===C||q===T)&&(q=C+T>>1),a[M+6]=0;let me=S(C,q),fe=S(q,T);return a[A*Et+7]=me,a[A*Et+8]=fe,A}return t>0&&S(0,t),{data:a,nodeCount:l,polyIndices:o,meta:e}}function ar(e,n,t,o,r,s,i,c,a){if(c.nodeCount===0)return!1;let{data:l,polyIndices:u,meta:f}=c,d=o!==0?1/o:o>=0?1/0:-1/0,m=r!==0?1/r:r>=0?1/0:-1/0,p=s!==0?1/s:s>=0?1/0:-1/0,h=0;for(a[h++]=0;h>0;){let b=a[--h]*Et,y=(l[b]-e)*d,x=(l[b+3]-e)*d,P=y<x?y:x,S=y<x?x:y,C=(l[b+1]-n)*m,T=(l[b+4]-n)*m,A=C<T?C:T,M=C<T?T:C;if(P>M||A>S)continue;A>P&&(P=A),M<S&&(S=M);let E=(l[b+2]-t)*p,v=(l[b+5]-t)*p,I=E<v?E:v,_=E<v?v:E;if(!(P>_||I>S)&&(_<S&&(S=_),!(S<.001)))if(l[b+6]===1){let O=l[b+7]|0,V=l[b+8]|0;for(let w=O;w<V;w++){let F=u[w];if(F===i)continue;let D=f[F];if(D&&pf(e,n,t,o,r,s,D))return!0}}else a[h++]=l[b+7]|0,a[h++]=l[b+8]|0}return!1}function bf(e){let n=Ui.get(e);if(n)return n;let t=(1+Math.sqrt(5))/2,o=new Float64Array(e*3);for(let r=0;r<e;r++){let s=(r+.5)/e,i=Math.sqrt(Math.max(0,1-s*s)),c=2*Math.PI*(r/t);o[r*3]=i*Math.cos(c),o[r*3+1]=i*Math.sin(c),o[r*3+2]=s}return Ui.set(e,o),o}var Ui=new Map;function fn(e,n){let t=n?.samples??8;if(e.length<4||t<1||!n?.force&&uf(e))return e;let o=Math.max(1,Math.ceil(t*(n?.minEscapeRatio??1/t))),r=e.map(mf),s=bf(t),i=[],c=hf(r),a=new Int32Array(Math.max(64,c.nodeCount)),l=192,u=new Float64Array(l);for(let f=0;f<e.length;f++){let d=r[f];if(!d){i.push(e[f]);continue}let m=d.normal[0],p=d.normal[1],h=d.normal[2],g=.001*m,b=.001*p,y=.001*h;{let V=d.centroid[0]+g,w=d.centroid[1]+b,F=d.centroid[2]+y;if(!ar(V,w,F,m,p,h,f,c,a)){i.push(e[f]);continue}}let{ux:x,uy:P,uz:S,vx:C,vy:T,vz:A}=d,M=d.centroid[0],E=d.centroid[1],v=d.centroid[2],I=d.vertices,_=I.length,O=0;u[O++]=M+g,u[O++]=E+b,u[O++]=v+y;for(let V=0;V<_;V++){let w=I[V];u[O++]=w[0]+(M-w[0])*.08+g,u[O++]=w[1]+(E-w[1])*.08+b,u[O++]=w[2]+(v-w[2])*.08+y}for(let V=0;V<_;V++){let w=I[V],F=I[(V+1)%_],D=(w[0]+F[0])*.5,H=(w[1]+F[1])*.5,K=(w[2]+F[2])*.5;u[O++]=D+(M-D)*.08+g,u[O++]=H+(E-H)*.08+b,u[O++]=K+(v-K)*.08+y}if(o<=1){let V=!1;e:for(let w=0;w<s.length;w+=3){let F=s[w],D=s[w+1],H=s[w+2],K=F*x+D*C+H*m,Q=F*P+D*T+H*p,ne=F*S+D*A+H*h;for(let ee=0;ee<O;ee+=3){let ue=u[ee],U=u[ee+1],$=u[ee+2];if(!ar(ue,U,$,K,Q,ne,f,c,a)){V=!0;break e}}}V&&i.push(e[f])}else{let V=0;for(let w=0;w<s.length;w+=3){let F=s[w],D=s[w+1],H=s[w+2],K=F*x+D*C+H*m,Q=F*P+D*T+H*p,ne=F*S+D*A+H*h;for(let ee=0;ee<O;ee+=3){let ue=u[ee],U=u[ee+1],$=u[ee+2];if(!ar(ue,U,$,K,Q,ne,f,c,a)){V++;break}}}V>=o&&i.push(e[f])}}return i}var ji={overlapPx:1.25,maxGapPx:14,capacityScale:1},Ki={budget:40};function Ln(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function qn(e){return typeof e=="number"?{overlapPx:Math.max(0,e)*.175,maxGapPx:14,capacityScale:1}:{overlapPx:Math.max(0,Ln(e?.overlapPx,1.25)),maxGapPx:Math.max(0,Ln(e?.maxGapPx,14)),capacityScale:Math.max(0,Ln(e?.capacityScale,1))}}function Qn(e,n){return!(typeof n=="number"&&n<=0)&&e.maxGapPx>1e-6&&e.capacityScale>1e-6}function Hi(e){return{rotX:Ln(e?.rotX,65),rotY:Ln(e?.rotY,45),viewAware:e?.viewAware!==!1,passes:Math.max(1,Math.min(3,Math.round(Ln(e?.passes,2)))),budget:Math.max(0,Math.min(256,Math.round(Ln(e?.budget,40))))}}function mn(e){return e.map(n=>[n[1]*50,n[0]*50,n[2]*50])}function Zi(e){return[e[1]/50,e[0]/50,e[2]/50]}function co(e){return`${e[0]},${e[1]},${e[2]}`}function yf(e,n){let t=co(e),o=co(n);return t<o?`${t}|${o}`:`${o}|${t}`}function dn(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Jn(e){return Math.hypot(e[0],e[1],e[2])}function _t(e){let n=Jn(e);return n>1e-6?[e[0]/n,e[1]/n,e[2]/n]:null}function It(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function oo(e,n,t,o){let r=n*Math.PI/180,s=t*Math.PI/180,i=o*Math.PI/180,[c,a,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[c,a]=[c*u-a*f,c*f+a*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[c,l]=[c*u+l*f,-c*f+l*u]}if(r!==0){let u=Math.cos(r),f=Math.sin(r);[a,l]=[a*u-l*f,a*f+l*u]}return[c,a,l]}function mr(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function xf(e){if(e.length<3)return null;let n=e[0],t=[0,0,0];for(let o=1;o+1<e.length;o+=1){let r=dn(e[o],n),s=dn(e[o+1],n),i=mr(r,s);t[0]-=i[0],t[1]-=i[1],t[2]-=i[2]}return _t(t)}function dr(e){let n=e[0],t=xf(e);if(!t)return null;let o=null;for(let u=0;u<e.length;u+=1){let f=dn(e[(u+1)%e.length],e[u]);if(Jn(f)>1e-6){o=f;break}}if(!o)return null;let r=It(o,t),s=[o[0]-r*t[0],o[1]-r*t[1],o[2]-r*t[2]],i=_t(s);if(!i)return null;let c=_t(mr(t,i));if(!c)return null;let a=e.map(u=>{let f=dn(u,n);return[It(f,i),It(f,c)]}),l=lo(a);return Math.abs(l)>1e-6?{origin:n,normal:t,xAxis:i,yAxis:c,local:a,area:l}:null}function lo(e){let n=0;for(let t=0;t<e.length;t+=1){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-o[1]*r[0]}return n/2}function pr(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t+=1){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=1e-6)continue;let c=Math.sign(i);if(n===0)n=c;else if(c!==n)return!1}return!0}function qi(e,n,t){let o=e[t],r=e[(t+1)%e.length],s=r[0]-o[0],i=r[1]-o[1],c=Math.hypot(s,i);if(c<=1e-6)return null;let a=n>0?1:-1;return[a*(i/c),a*(-s/c)]}function Pf(e,n){let t=qi(e.local,e.area,n);return t?_t([e.xAxis[0]*t[0]+e.yAxis[0]*t[1],e.xAxis[1]*t[0]+e.yAxis[1]*t[1],e.xAxis[2]*t[0]+e.yAxis[2]*t[1]]):null}function Qi(e,n){let t=e.length,o=e[n],r=e[(n+1)%t],s=r[0]-o[0],i=r[1]-o[1],c=Math.hypot(s,i);if(c<=1e-6)return 0;let a=[c*.24],l=1/0;for(let f=0;f<t;f+=1){if(f===n||f===(n+1)%t)continue;let d=e[f],m=Math.abs((d[0]-o[0])*i-(d[1]-o[1])*s)/c;m>1e-6&&(l=Math.min(l,m))}Number.isFinite(l)&&a.push(l*.32);let u=[{origin:o,point:e[(n+t-1)%t]},{origin:r,point:e[(n+2)%t]}];for(let f of u){let d=f.point[0]-f.origin[0],m=f.point[1]-f.origin[1],p=Math.hypot(d,m);if(p<=1e-6)continue;let h=Math.abs(s*m-i*d)/(c*p);h>1e-6&&a.push(p*h*.32)}return Math.min(...a)}function Ji(e){return e.map(n=>{let t=mn(n.vertices),o=dr(t),r=!gr(n)&&!!o&&pr(o.local),s=r?o.local.map((i,c)=>Qi(o.local,c)):[];return{basis:o,cssPoints:t,capacities:s,patchable:r}})}function gr(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function Mf(e){return e.material?.key??e.color??""}function ea(e,n,t){let o=[];for(let r=0;r<n.length;r+=1){let s=n[r];if(!(!s.patchable||!s.basis))for(let i=0;i<s.cssPoints.length;i+=1){let c=s.cssPoints[i],a=s.cssPoints[(i+1)%s.cssPoints.length],l=dn(a,c),u=Jn(l),f=_t(l),d=Pf(s.basis,i),m=(s.capacities[i]??0)*t;!f||!d||m<=1e-6||u<=1e-6||o.push({index:o.length,polygon:r,edge:i,key:yf(c,a),a:c,b:a,minX:Math.min(c[0],a[0]),minY:Math.min(c[1],a[1]),minZ:Math.min(c[2],a[2]),maxX:Math.max(c[0],a[0]),maxY:Math.max(c[1],a[1]),maxZ:Math.max(c[2],a[2]),length:u,dir:f,normal:s.basis.normal,outward:d,capacity:m,materialKey:Mf(e[r]),color:e[r].color})}}return o}function hr(e,n){return e.materialKey===n.materialKey&&e.color===n.color}function ro(e,n){return Jn(dn(e,n))<=1e-4}function Af(e,n){return ro(e.a,n.a)||ro(e.a,n.b)||ro(e.b,n.a)||ro(e.b,n.b)}function Sf(e,n){return hr(e,n)?Af(e,n)?"connected-facet":"true-gap":"material-boundary"}function cr(e,n,t,o,r,s){return{kind:e,aPolygon:n.polygon,aEdge:n.edge,bPolygon:t.polygon,bEdge:t.edge,aColor:n.color,bColor:t.color,aMaterialKey:n.materialKey,bMaterialKey:t.materialKey,gapPx:o.gap,spanPx:o.aEnd-o.aStart,aStartPx:o.aStart,aEndPx:o.aEnd,bStartPx:o.bStart,bEndPx:o.bEnd,targetClosurePx:r,appliedClosurePx:s,residualGapPx:Math.max(0,o.gap-s),residualTargetPx:Math.max(0,r-s)}}function Gi(e,n,t,o,r){let s=Math.max(0,Math.min(n.length,Math.min(t,o))),i=Math.max(0,Math.min(n.length,Math.max(t,o)));if(r<=1e-6||i-s<=1e-6)return;let c=e[n.polygon].get(n.edge),a={start:s,end:i,amount:r};c?c.push(a):e[n.polygon].set(n.edge,[a])}function uo(){return{exactPairs:0,nearPairs:0,patchedPolygons:0,patchedEdges:0,maxMeasuredGapPx:0,maxAppliedAmountPx:0,unclosedPairs:0,maxResidualGapPx:0}}function Cf(e,n){let t=0,o=0,r=0;for(let s of e){s.size>0&&(r+=1),o+=s.size;for(let i of s.values())for(let c of i)t=Math.max(t,c.amount)}return{...n,patchedPolygons:r,patchedEdges:o,maxAppliedAmountPx:t}}function fo(e,n,t=!1,o=!1){let r=Ji(e),s=ea(e,r,n.capacityScale),i=o?[]:e.map(()=>new Map),c=uo(),a=t?[]:void 0;if(s.length===0)return{edgeRepairs:i,diagnostics:c,candidates:a};let l=new Map;for(let u of s){let f=l.get(u.key);f?f.push(u):l.set(u.key,[u])}for(let u of l.values())u.length<2||(c.exactPairs+=u.length===2?1:u.length);return Ef(s,l,i,c,n,a),{edgeRepairs:i,diagnostics:o?c:Cf(i,c),candidates:a}}function Ef(e,n,t,o,r,s){let i=r.maxGapPx,c=new Set;for(let[m,p]of n)p.length>1&&c.add(m);let a=(m,p)=>{let h=m.index<p.index?m:p,g=m.index<p.index?p:m;if(g.polygon===h.polygon||h.key===g.key&&c.has(h.key)||!s&&!hr(h,g)||!Tf(h,g,i)||Math.abs(It(h.dir,g.dir))<.985)return;let b=Of(h,g,i);if(!b)return;let y=Sf(h,g),x=b.gap+r.overlapPx;if(y==="material-boundary"){s?.push(cr(y,h,g,b,x,0));return}let P=x;if(P<=.25){s?.push(cr(y,h,g,b,x,0));return}let S=h.capacity*b.facingA,C=g.capacity*b.facingB,T=Math.min(S,P/2),A=Math.min(C,P/2);if(P-=T+A,P>1e-6){let M=Math.min(S-T,P);T+=M,P-=M}if(P>1e-6){let M=Math.min(C-A,P);A+=M,P-=M}s?.push(cr(y,h,g,b,x,T+A)),t.length>0&&T>1e-6&&Gi(t,h,b.aStart,b.aEnd,T/b.facingA),t.length>0&&A>1e-6&&Gi(t,g,b.bStart,b.bEnd,A/b.facingB),o.nearPairs+=1,o.maxMeasuredGapPx=Math.max(o.maxMeasuredGapPx,b.gap),P>.25&&(o.unclosedPairs+=1,o.maxResidualGapPx=Math.max(o.maxResidualGapPx,P))},l=Math.max(28,i*2);if(e.length<=1e4){let m=[...e].sort((g,b)=>g.minX-b.minX),p=new Int32Array(m.length),h=0;for(let g=0;g+1<m.length;g+=1){let y=m[g].maxX+i,x=g+1;for(;x<m.length&&m[x].minX<=y;)x+=1;if(p[g]=x,h+=x-g-1,h>25e6)break}if(h<=2e6||h<=25e6&&_f(e,l,i)){for(let g=0;g+1<m.length;g+=1)for(let b=g+1;b<p[g];b+=1)a(m[g],m[b]);return}}let u=new Map;for(let m of e)vf(u,m,l,i);let f=new Set,d=e.length;for(let m of u.values())for(let p=0;p+1<m.length;p+=1)for(let h=p+1;h<m.length;h+=1){let g=m[p],b=m[h],y=g.index<b.index?g:b,x=g.index<b.index?b:g,P=y.index*d+x.index;f.has(P)||(f.add(P),a(y,x))}}function Tf(e,n,t){return e.minX<=n.maxX+t&&n.minX<=e.maxX+t&&e.minY<=n.maxY+t&&n.minY<=e.maxY+t&&e.minZ<=n.maxZ+t&&n.minZ<=e.maxZ+t}function zi(e,n){return[Math.floor(e[0]/n),Math.floor(e[1]/n),Math.floor(e[2]/n)]}function vf(e,n,t,o){let[r,s,i,c,a,l]=na(n,t,o);for(let u=r;u<=c;u+=1)for(let f=s;f<=a;f+=1)for(let d=i;d<=l;d+=1){let m=`${u},${f},${d}`,p=e.get(m);p?p.push(n):e.set(m,[n])}}function na(e,n,t){let[o,r,s]=zi([e.minX-t,e.minY-t,e.minZ-t],n),[i,c,a]=zi([e.maxX+t,e.maxY+t,e.maxZ+t],n);return[o,r,s,i,c,a]}function If(e,n,t){let[o,r,s,i,c,a]=na(e,n,t);return(i-o+1)*(c-r+1)*(a-s+1)}function _f(e,n,t){let o=0,r=0;for(let s of e){let i=If(s,n,t);if(o+=i,i>r&&(r=i),r>8192)return!0}return o/Math.max(1,e.length)>256}function $i(e,n,t){return(e[0]-n[0])*t[0]+(e[1]-n[1])*t[1]+(e[2]-n[2])*t[2]}function Xi(e,n,t){return(e.a[0]+e.dir[0]*n-t.a[0])*t.dir[0]+(e.a[1]+e.dir[1]*n-t.a[1])*t.dir[1]+(e.a[2]+e.dir[2]*n-t.a[2])*t.dir[2]}function Of(e,n,t){let o=$i(n.a,e.a,e.dir),r=$i(n.b,e.a,e.dir),s=Math.max(0,Math.min(o,r)),i=Math.min(e.length,Math.max(o,r)),c=i-s,a=Math.min(e.length,n.length);if(c<Math.max(.75,a*.12))return null;let l=(s+i)/2,u=e.a[0]+e.dir[0]*l,f=e.a[1]+e.dir[1]*l,d=e.a[2]+e.dir[2]*l,m=Math.max(0,Math.min(n.length,(u-n.a[0])*n.dir[0]+(f-n.a[1])*n.dir[1]+(d-n.a[2])*n.dir[2])),p=n.a[0]+n.dir[0]*m-u,h=n.a[1]+n.dir[1]*m-f,g=n.a[2]+n.dir[2]*m-d,b=Math.hypot(p,h,g);if(b<=1e-6||b>t||b>Math.min(t,Math.max(4,a*.28)))return null;let y=1/b,x=p*y,P=h*y,S=g*y,C=e.outward[0]*x+e.outward[1]*P+e.outward[2]*S,T=-(n.outward[0]*x+n.outward[1]*P+n.outward[2]*S);if(C<.5||T<.5)return null;let A=Math.max(0,Math.min(n.length,Xi(e,s,n))),M=Math.max(0,Math.min(n.length,Xi(e,i,n)));return{gap:b,facingA:C,facingB:T,aStart:s,aEnd:i,bStart:Math.min(A,M),bEnd:Math.max(A,M)}}function lr(e,n,t,o){let r=t<=1e-6?0:Math.max(0,Math.min(1,o/t));return[e[0]+(n[0]-e[0])*r,e[1]+(n[1]-e[1])*r]}function Tt(e,n){let t=e[e.length-1];t&&Math.hypot(t[0]-n[0],t[1]-n[1])<=1e-6||e.push(n)}function Vf(e,n,t){if(!e?.length||n<=1e-6||t<=1e-6)return[];let o=new Set([0,n]);for(let i of e){let c=Math.max(0,Math.min(n,i.start)),a=Math.max(0,Math.min(n,i.end));a-c<=1e-6||i.amount<=1e-6||(o.add(c),o.add(a))}let r=Array.from(o).sort((i,c)=>i-c),s=[];for(let i=0;i+1<r.length;i+=1){let c=r[i],a=r[i+1];if(a-c<=1e-6)continue;let l=(c+a)/2,u=0;for(let d of e)l+1e-6<d.start||l-1e-6>d.end||(u=Math.max(u,d.amount));let f=Math.min(t,u);f>1e-6&&s.push({start:c,end:a,amount:f})}return s}function Rf(e,n){if(n.length<3||!pr(n))return!1;let t=lo(e),o=lo(n);return Math.sign(o)===Math.sign(t)&&Math.abs(o)>=Math.abs(t)-1e-6}function Lf(e,n,t){if(n.size===0||e.vertices.length<3||gr(e))return e;let o=mn(e.vertices),r=dr(o);if(!r||!pr(r.local))return e;let s=[];for(let l=0;l<r.local.length;l+=1){let u=r.local[l],f=r.local[(l+1)%r.local.length],d=Math.hypot(f[0]-u[0],f[1]-u[1]),m=qi(r.local,r.area,l);if(!m||d<=1e-6)return e;let p=Vf(n.get(l),d,Qi(r.local,l)*t);Tt(s,u);let h=0;for(let g of p){g.start>h+1e-6&&Tt(s,lr(u,f,d,g.start));let b=lr(u,f,d,g.start),y=lr(u,f,d,g.end);Tt(s,[b[0]+m[0]*g.amount,b[1]+m[1]*g.amount]),Tt(s,[y[0]+m[0]*g.amount,y[1]+m[1]*g.amount]),g.end<d-1e-6&&Tt(s,y),h=g.end}}let i=s[0],c=s[s.length-1];if(i&&c&&Math.hypot(i[0]-c[0],i[1]-c[1])<=1e-6&&s.pop(),!Rf(r.local,s))return e;let a=s.map(([l,u])=>Zi([r.origin[0]+l*r.xAxis[0]+u*r.yAxis[0],r.origin[1]+l*r.xAxis[1]+u*r.yAxis[1],r.origin[2]+l*r.xAxis[2]+u*r.yAxis[2]]));return{...e,vertices:a}}function ta(e){return e.vertices.length===4&&!gr(e)}function wf(e){let n=e.basis?.local;if(!n||n.length!==4)return 1;let t=1/0,o=1/0,r=-1/0,s=-1/0;for(let[a,l]of n)t=Math.min(t,a),o=Math.min(o,l),r=Math.max(r,a),s=Math.max(s,l);let i=Math.max(1e-6,(r-t)*(s-o));return .75+(1-Math.max(0,Math.min(1,Math.abs(e.basis.area)/i)))*2.5}function ur(e,n){return Jn(dn(mn([e])[0],mn([n])[0]))}function so(e,n,t){let o=ur(e,n),r=ur(n,t),s=ur(t,e),i=Math.max(o,r,s);return i<=1e-6?0:Jn(mr(dn(mn([n])[0],mn([e])[0]),dn(mn([t])[0],mn([e])[0])))*.5/(i*i)}function Nf(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],c=o[1]-t[1],a=r*c-s*i;if(Math.abs(a)<=1e-6)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*c-u*i)/a;return[e[0]+f*r,e[1]+f*s]}function Df(e,n){if(e.length<3||e.length!==n.length)return null;let t=Math.max(0,...n);if(t<=1e-6)return e;let o=lo(e);if(Math.abs(o)<=1e-6)return null;let r=o>0?1:-1,s=[];for(let a=0;a<e.length;a+=1){let l=e[a],u=e[(a+1)%e.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=1e-6)return null;let p=Math.max(0,n[a]),h=r*(d/m)*p,g=r*(-f/m)*p;s.push({a:[l[0]+h,l[1]+g],b:[u[0]+h,u[1]+g]})}let i=[],c=Math.max(2,t*4);for(let a=0;a<e.length;a+=1){let l=s[(a+e.length-1)%e.length],u=s[a],f=Nf(l.a,l.b,u.a,u.b);if(!f)return null;let d=e[a],m=f[0]-d[0],p=f[1]-d[1],h=Math.hypot(m,p);i.push(h>c?[d[0]+m/h*c,d[1]+p/h*c]:f)}return i}function Ff(e,n){return[e.origin[0]+e.xAxis[0]*n[0]+e.yAxis[0]*n[1],e.origin[1]+e.xAxis[1]*n[0]+e.yAxis[1]*n[1],e.origin[2]+e.xAxis[2]*n[0]+e.yAxis[2]*n[1]]}function io(e,n){let t=dr(mn(e));if(!t)return e;let o=Df(t.local,n);return o?o.map(r=>Zi(Ff(t,r))):e}function Bf(e,n){let t={...e,uvs:void 0,textureTriangles:void 0},[o,r,s,i]=e.vertices,c=Math.min(so(o,r,s),so(o,s,i)),a=Math.min(so(o,r,i),so(r,s,i)),l=u=>n.has(u)?.75:0;return c>=a?[{...t,vertices:io([o,r,s],[l(0),l(1),1.25])},{...t,vertices:io([o,s,i],[1.25,l(2),l(3)])}]:[{...t,vertices:io([o,r,i],[l(0),1.25,l(3)])},{...t,vertices:io([r,s,i],[l(1),l(2),1.25])}]}function Yi(e){if(e.length===0)return{q1:1/0,median:1/0,q3:1/0,p70:1/0,p80:1/0,p88:1/0,p95:1/0,p97:1/0,max:1/0};let n=[...e].sort((o,r)=>o-r),t=o=>n[Math.min(n.length-1,Math.floor((n.length-1)*o))]??0;return{q1:t(.25),median:t(.5),q3:t(.75),p70:t(.7),p80:t(.8),p88:t(.88),p95:t(.95),p97:t(.97),max:n[n.length-1]??0}}function ao(e,n){let t=e[n];if(t===n)return n;let o=ao(e,t);return e[n]=o,o}function kf(e){let n=new Set,t=new Set;for(let o of e)o.sel&&t.add(o.comp);for(let o of e)if(t.has(o.comp))for(let r of o.e)n.add(r);return{endpoints:n}}function Uf(e,n){return n?e.e.some(t=>n.endpoints.has(t)):!0}function Gf(e,n,t,o){let r=[];for(let[a,l]of t){if(l.length!==2)continue;let[u,f]=l;if(!hr(u,f))continue;let d=l.filter(C=>ta(e[C.polygon]));if(d.length===0)continue;let m=d[0],p=1-Math.min(1,Math.abs(It(u.normal,f.normal))),h=Math.max(u.length,f.length);if(p<.01&&h<64)continue;let g=Math.max(...d.map(C=>wf(n[C.polygon]))),b=d.length===2?1.18:1,y=Math.max(.08,p),x=o.viewAware?zf(u,f,o.rotX,o.rotY):{proj:h,vr:1},P=o.viewAware?Math.max(x.proj,h*.12):h,S=o.viewAware?.18+x.vr*1.82:1;r.push({key:a,e:[co(m.a),co(m.b)],o:l,r:d,len:h,rank:P,proj:x.proj,s:P*(.58+y*2.8)*g*b*S,nr:p,sr:g,vr:x.vr,comp:-1,sel:!1,why:"below-threshold",cost:d.length})}let s=Array.from({length:r.length},(a,l)=>l),i=new Map;for(let a=0;a<r.length;a+=1)for(let l of r[a].e){let u=i.get(l);if(u===void 0)i.set(l,a);else{let f=ao(s,u),d=ao(s,a);f!==d&&(s[d]=f)}}let c=new Map;for(let a=0;a<r.length;a+=1){let l=ao(s,a),u=c.get(l);u===void 0&&(u=c.size,c.set(l,u)),r[a].comp=u}return r}function zf(e,n,t,o){let r=oo(e.a,t,0,o),s=oo(e.b,t,0,o),i=Math.hypot(s[0]-r[0],s[1]-r[1]),c=oo(e.normal,t,0,o)[2],a=oo(n.normal,t,0,o)[2],l=Math.max(0,c,a),u=Math.min(1,Math.abs(c-a));return{proj:i,vr:Math.max(.04,Math.min(1,l*.85+u*.45))}}function $f(e){let n=new Map;for(let t of e){let o=n.get(t.comp);(!o||t.s>o.s||t.s===o.s&&t.len>o.len)&&n.set(t.comp,t)}return new Set(n.values())}function vt(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+n*n*n*3.65,o=e.r.length===1?1.08:1;return e.rank*t*o/Math.sqrt(Math.max(1,e.r.length))}function Wi(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+Math.min(1,e.vr)*1.65,o=.7+Math.min(2.6,e.sr)*.3,r=e.r.length===1?1.1:1,s=e.rank*(.6+n*n*1.4)*t*o*r/Math.sqrt(Math.max(1,e.r.length));return Math.max(s,vt(e)*.75)}function fr(e){let n=new Set;for(let t of e)for(let o of t.r)n.add(o.polygon);return n.size}function oa(e,n){return e.nr<=.25&&e.rank>=n.median*1.08&&e.proj>=24}function Xf(e,n,t){return e.s>=n||e.rank>=t}function Yf(e,n,t,o,r,s,i){let c=Math.max(0,Math.floor(t));if(c<=0||e.length===0||n==="follow-up")return{budgetLimit:c,componentBudgets:null};let a=new Map;for(let m of e){let p=a.get(m.comp);p?p.push(m):a.set(m.comp,[m])}let l=new Map,u=Math.max(1e-6,o.q3-o.q1),f=Math.min(o.max,Math.max(o.p95,o.q3+u)),d=0;for(let[m,p]of a){let h=p.filter(S=>Xf(S,s,i)||S.s>=f);if(h.length===0)continue;let g=p.filter(S=>oa(S,r)),b=fr(h),y=fr(g),x=Math.ceil(Math.sqrt(Math.max(0,y-b))*3),P=Math.min(fr(p),b+x);P<=0||(l.set(m,P),d+=P)}return l.size===0?{budgetLimit:0,componentBudgets:l}:{budgetLimit:Math.min(c,d),componentBudgets:l}}function Wf(e,n,t){let o=new Map;if(e.length===0||t<=0)return{selected:o,budgetLimit:0};let r=Yi(e.map(M=>M.s)),s=Yi(e.map(M=>M.rank)),i=Math.max(1e-6,r.q3-r.q1),c=Math.min(r.max,Math.max(r.p95,r.q3+i*.85)),a=Math.min(r.max,Math.max(r.p80,r.q3+i*.15)),l=Math.min(r.max,Math.max(r.p70,r.q3)),u=Math.max(s.p97,s.median*1.8),f=Math.max(s.p88,s.median*1.35),d=Yf(e,n,t,r,s,c,u),m=d.budgetLimit;if(m<=0)return{selected:o,budgetLimit:0};let p=$f(e),h=new Set,g=new Set,b=new Set,y=new Map,x=n==="primary"?Math.min(Math.max(0,d.budgetLimit-1),Math.max(2,Math.floor(d.budgetLimit*.28))):0,P=[...e].sort((M,E)=>vt(E)-vt(M)||E.s/Math.max(1,E.r.length)-M.s/Math.max(1,M.r.length)||E.len-M.len),S=[...e].sort((M,E)=>Wi(E)-Wi(M)||vt(E)-vt(M)||E.len-M.len),C=(M,E)=>{M.sel=!0,M.why=E,b.add(M.comp);for(let v of M.e)g.add(v);for(let v of M.r){h.add(v.polygon);let I=o.get(v.polygon);I?I.add(v.edge):o.set(v.polygon,new Set([v.edge]))}},T=(M,E)=>{let v=d.componentBudgets;if(E<=0||!v)return!0;let I=v.get(M.comp)??0;return(y.get(M.comp)??0)+E<=I},A=(M,E)=>{E<=0||!d.componentBudgets||y.set(M.comp,(y.get(M.comp)??0)+E)};for(let M of S){let E=M.r.reduce((I,_)=>I+(h.has(_.polygon)?0:1),0);if(M.cost=E,!(E>m||!T(M,E)||E>0&&m-E<x||!oa(M,s))&&(C(M,"global-outlier"),A(M,E),m-=E,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}for(let M of P){if(M.sel)continue;let E=M.r.reduce((V,w)=>V+(h.has(w.polygon)?0:1),0);if(M.cost=E,E>m||!T(M,E)||E>0&&m-E<x)continue;let v=M.r.length===2,I=p.has(M)&&(n==="primary"||n==="follow-up"&&M.s>=a*1.08&&M.rank>=f)&&(v?M.s>=a||M.rank>=f:M.s>=a*1.18||M.rank>=f*1.12),_=v?M.s>=c||M.rank>=u:M.s>=c*1.35||M.rank>=u*1.2,O=E===0&&(v?M.s>=l||M.rank>=f:M.s>=l*1.25||M.rank>=f*1.12);if(!(!I&&!_&&!O)&&(C(M,I?"component-anchor":_?"global-outlier":"shared-polygon"),A(M,E),m-=E,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}for(let M of P){if(M.sel)continue;let E=M.r.reduce((w,F)=>w+(h.has(F.polygon)?0:1),0);if(M.cost=E,E>1||E>m||!T(M,E))continue;let v=M.r.some(w=>h.has(w.polygon)),I=M.e.some(w=>g.has(w));if(n==="primary"&&M.r.length===1&&E===1&&(b.has(M.comp)||I||v)&&M.rank>=f*1.05&&M.nr>=.16&&M.vr<=.32){if(C(M,"local-follow-up"),A(M,E),m-=E,m<=0)return{selected:o,budgetLimit:d.budgetLimit};continue}if(M.r.length!==2)continue;let O=v&&E===1&&M.proj>=f*1.45&&M.vr<=.18;if(!I&&!O)continue;if((M.s>=l*.42||M.rank>=f*.62)&&M.s<=l*1.1&&M.proj>=24&&(C(M,"local-follow-up"),A(M,E),m-=E,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}return{selected:o,budgetLimit:d.budgetLimit}}function ra(e,n,t,o,r){let s=Ji(e),i=ea(e,s,n.capacityScale),c=new Map;for(let u of i){let f=c.get(u.key);f?f.push(u):c.set(u.key,[u])}let a=Gf(e,s,c,t).filter(u=>Uf(u,r)),l=Wf(a,r?"follow-up":"primary",o);return{selected:l.selected,candidates:a,budgetLimit:l.budgetLimit}}function jf(e,n){let t=[];for(let o=0;o<e.length;o+=1){let r=e[o],s=n.get(o);s&&ta(r)?t.push(...Bf(r,s)):t.push(r)}return t}function Kf(e){let[n,t]=e.o,o=n??t,r=t??n;return{key:e.key,aPolygon:o?.polygon??-1,aEdge:o?.edge??-1,bPolygon:r?.polygon??-1,bEdge:r?.edge??-1,color:o?.color??r?.color,materialKey:o?.materialKey??r?.materialKey??"",lengthPx:e.len,projectedLengthPx:e.proj,score:e.s,normalRisk:e.nr,shapeRisk:e.sr,viewRisk:e.vr,component:e.comp,marginalCost:e.cost,selected:e.sel,reason:e.why}}function sa(e,n,t){let o=qn(n),r=Hi(t);if(!Qn(o,n)||e.length===0)return e;let s=e,i,c=r.budget;for(let a=0;a<r.passes;a+=1){let l=ra(s,o,r,c,i);if(a===0&&(c=Math.min(c,l.budgetLimit)),l.selected.size===0)break;let u=jf(s,l.selected);if(u.length===s.length)break;if(c-=l.selected.size,c<=0){s=u;break}i=kf(l.candidates),s=u}return s}function Hf(e,n,t){let o=qn(n),r=Hi(t);if(!Qn(o,n)||e.length===0)return{candidates:[],selectedPolygons:0,selectedEdges:0,addedPolygons:0};let s=ra(e,o,r,r.budget),i=0;for(let c of s.selected.values())i+=c.size;return{candidates:s.candidates.map(Kf),selectedPolygons:s.selected.size,selectedEdges:i,addedPolygons:s.selected.size}}function ia(e,n){let t=qn(n);if(!Qn(t,n)||e.length===0)return e;let{edgeRepairs:o}=fo(e,t);return o.some(r=>r.size>0)?e.map((r,s)=>Lf(r,o[s],t.capacityScale)):e}function Zf(e,n=ji,t=Ki){if(e.length===0)return e;let o=sa(e,n,t);return ia(o,n)}function qf(e,n){let t=qn(n);return!Qn(t,n)||e.length===0?uo():fo(e,t).diagnostics}function aa(e,n){let t=qn(n);return!Qn(t,n)||e.length===0?uo():fo(e,t,!1,!0).diagnostics}function Qf(e,n){let t=qn(n);if(!Qn(t,n)||e.length===0)return{diagnostics:uo(),candidates:[]};let o=fo(e,t,!0);return{diagnostics:o.diagnostics,candidates:o.candidates??[]}}var wn={ratio:.7,maxError:.18,maxNormalDot:Math.cos(65*Math.PI/180),preserveVertices:!0,vertexKeyMode:"relaxed",lockBoundary:!0,minGroupTriangles:80,maxPasses:12};function Jf(e={}){let n=Number.isFinite(e.ratio)?Math.max(.05,Math.min(.98,e.ratio)):wn.ratio,t=Number.isFinite(e.maxError)&&e.maxError>0?e.maxError:wn.maxError,o=Number.isFinite(e.maxNormalAngleDeg)?Math.max(0,Math.min(89,e.maxNormalAngleDeg)):65;return{ratio:n,maxError:t,maxNormalDot:Math.cos(o*Math.PI/180),preserveVertices:e.preserveVertices??wn.preserveVertices,vertexKeyMode:e.vertexKeyMode??wn.vertexKeyMode,lockBoundary:e.lockBoundary??wn.lockBoundary,minGroupTriangles:Math.max(3,Math.floor(e.minGroupTriangles??wn.minGroupTriangles)),maxPasses:Math.max(1,Math.floor(e.maxPasses??wn.maxPasses))}}function em(e){return!!(e.texture||e.material?.texture||e.uvs?.length||e.textureTriangles?.length)}function nm(e){return e.vertices.length===3&&!em(e)}function tm(e){return[e.color??"",e.doubleSided===!0?"2":"1",e.material?.key??""].join("\0")}function ca(e){return`${Math.round(e[0]*1e5)},${Math.round(e[1]*1e5)},${Math.round(e[2]*1e5)}`}function om(e,n,t,o){let r=o.vertexKeyMode==="source"?e.simplifySourceVertexKeys?.[t]??e.simplifyVertexKeys?.[t]:e.simplifyVertexKeys?.[t];return r?`${ca(n)}|${r}`:ca(n)}function rm(e,n){return[e[0]+n[0],e[1]+n[1],e[2]+n[2]]}function et(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function sm(e,n){return[e[0]*n,e[1]*n,e[2]*n]}function br(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function la(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function Pr(e){return Math.hypot(e[0],e[1],e[2])}function yr(e,n){return Pr(et(e,n))}function im(e){let n=Pr(e);return n>1e-12?[e[0]/n,e[1]/n,e[2]/n]:[0,0,1]}function Mr(e,n,t,o){return im(la(et(e[t],e[n]),et(e[o],e[n])))}function ua(e,n,t,o){return Pr(la(et(e[t],e[n]),et(e[o],e[n])))/2}function am(){return[0,0,0,0,0,0,0,0,0,0]}function mo(e,n){for(let t=0;t<e.length;t+=1)e[t]+=n[t]}function cm(e,n,t){let o=e[0],r=e[1],s=e[2],i=-br(e,n);return[o*o*t,o*r*t,o*s*t,o*i*t,r*r*t,r*s*t,r*i*t,s*s*t,s*i*t,i*i*t]}function xr(e,n){let t=n[0],o=n[1],r=n[2];return e[0]*t*t+2*e[1]*t*o+2*e[2]*t*r+2*e[3]*t+e[4]*o*o+2*e[5]*o*r+2*e[6]*o+e[7]*r*r+2*e[8]*r+e[9]}function lm(e,n,t,o,r,s,i,c,a,l,u,f){let d=e*(r*a-s*c)-n*(o*a-s*i)+t*(o*c-r*i);if(Math.abs(d)<1e-10)return null;let m=1/d,p=l*(r*a-s*c)-n*(u*a-s*f)+t*(u*c-r*f),h=e*(u*a-s*f)-l*(o*a-s*i)+t*(o*f-u*i),g=e*(r*f-u*c)-n*(o*f-u*i)+l*(o*c-r*i),b=[p*m,h*m,g*m];return b.every(Number.isFinite)?b:null}function um(e,n,t,o){let r=lm(e[0],e[1],e[2],e[1],e[4],e[5],e[2],e[5],e[7],-e[3],-e[6],-e[8]),s=sm(rm(n,t),.5),i=yr(n,t)*1.35,c=o?[n,t]:[n,t,s,...r&&yr(r,s)<=i?[r]:[]],a=c[0],l=xr(e,a);for(let u=1;u<c.length;u+=1){let f=c[u],d=xr(e,f);d<l&&(a=f,l=d)}return a}function fm(e,n){return e<n?`${e}|${n}`:`${n}|${e}`}function mm(e,n,t){return`${e}|${n}|${t}`}function dm(e,n){let t=new Map,o=[],r=[];for(let s of e){let i=[];for(let u=0;u<s.vertices.length;u+=1){let f=s.vertices[u],d=om(s,f,u,n),m=t.get(d);m===void 0&&(m=o.length,t.set(d,m),o.push([...f])),i.push(m)}let[c,a,l]=i;c===void 0||a===void 0||l===void 0||c===a||c===l||a===l||r.push({a:c,b:a,c:l,polygon:s,normal:Mr(o,c,a,l)})}return{vertices:o,triangles:r}}function pm(e,n){let t=dm(e,n),o=Array.from({length:t.vertices.length},()=>new Set),r=Array.from({length:t.triangles.length},()=>!0);for(let s=0;s<t.triangles.length;s+=1){let i=t.triangles[s];o[i.a].add(s),o[i.b].add(s),o[i.c].add(s)}return{vertices:t.vertices,triangles:t.triangles,alive:r,vertexTriangles:o,activeCount:t.triangles.length}}function gm(e){let n=new Map,t=(o,r,s)=>{let i=fm(o,r),c=n.get(i);c?c.triangles.push(s):n.set(i,{a:Math.min(o,r),b:Math.max(o,r),triangles:[s]})};for(let o=0;o<e.triangles.length;o+=1){if(!e.alive[o])continue;let r=e.triangles[o];t(r.a,r.b,o),t(r.b,r.c,o),t(r.c,r.a,o)}return n}function hm(e){let n=Array.from({length:e.vertices.length},()=>am());for(let t=0;t<e.triangles.length;t+=1){if(!e.alive[t])continue;let o=e.triangles[t],r=Math.max(1e-6,ua(e.vertices,o.a,o.b,o.c)),s=cm(o.normal,e.vertices[o.a],Math.sqrt(r));mo(n[o.a],s),mo(n[o.b],s),mo(n[o.c],s)}return n}function bm(e,n,t){let o=new Set,r=s=>{let i=e.vertexTriangles[s];if(i)for(let c of i)e.alive[c]&&o.add(c)};return r(n),r(t),Array.from(o).sort((s,i)=>s-i)}function ym(e,n,t){let o=e.vertexTriangles[n],r=e.vertexTriangles[t];if(!o||!r)return 0;let[s,i]=o.size<=r.size?[o,r]:[r,o],c=0;for(let a of s)e.alive[a]&&i.has(a)&&(c+=1);return c}function fa(e,n,t){return[e.a===t?n:e.a,e.b===t?n:e.b,e.c===t?n:e.c]}function xm(e,n,t){if(ym(e,n.a,n.b)!==2)return null;let o=bm(e,n.a,n.b);if(o.length===0)return null;let r=e.vertices[n.a];e.vertices[n.a]=n.target;let s=0,i=!0;for(let c of o){let a=e.triangles[c],[l,u,f]=fa(a,n.a,n.b);if(l===u||l===f||u===f)continue;if(ua(e.vertices,l,u,f)<=1e-8){i=!1;break}let m=Mr(e.vertices,l,u,f);if(br(a.normal,m)<t.maxNormalDot){i=!1;break}let p=a.a===n.a?r:e.vertices[a.a];if(Math.abs(br(a.normal,et(n.target,p)))>t.maxError){i=!1;break}s+=1}return e.vertices[n.a]=r,i&&s>0?o:null}function Pm(e,n,t){e.vertexTriangles[t.a]?.delete(n),e.vertexTriangles[t.b]?.delete(n),e.vertexTriangles[t.c]?.delete(n)}function Mm(e,n,t){e.vertexTriangles[t.a].add(n),e.vertexTriangles[t.b].add(n),e.vertexTriangles[t.c].add(n)}function Am(e,n,t){e.vertices[n.a]=n.target;let o=new Set;for(let r of t){if(!e.alive[r])continue;let s=e.triangles[r],[i,c,a]=fa(s,n.a,n.b);if(Pm(e,r,s),i===c||i===a||c===a){e.alive[r]=!1,e.activeCount-=1;continue}let l=mm(i,c,a);if(o.has(l)){e.alive[r]=!1,e.activeCount-=1;continue}o.add(l);let u={...s,a:i,b:c,c:a,normal:Mr(e.vertices,i,c,a)};e.triangles[r]=u,Mm(e,r,u)}}function Sm(e,n){let t=gm(e),o=new Set;for(let c of t.values())c.triangles.length>2&&(o.add(c.a),o.add(c.b));if(n.lockBoundary)for(let c of t.values())c.triangles.length===1&&(o.add(c.a),o.add(c.b));let r=hm(e),s=n.maxError*n.maxError,i=[];for(let c of t.values()){if(c.triangles.length!==2||o.has(c.a)||o.has(c.b))continue;let a=[...r[c.a]];mo(a,r[c.b]);let l=um(a,e.vertices[c.a],e.vertices[c.b],n.preserveVertices),u=Math.max(0,xr(a,l));if(u>s*Math.max(1,c.triangles.length))continue;let f=yr(e.vertices[c.a],e.vertices[c.b]);i.push({a:c.a,b:c.b,target:l,error:u,length:f})}return i.sort((c,a)=>c.error-a.error||c.length-a.length),i}function Cm(e){let n=[];for(let t=0;t<e.triangles.length;t+=1)e.alive[t]&&n.push(e.triangles[t]);return n}function Em(e,n){if(e.length<n.minGroupTriangles)return e;let t=pm(e,n),o=t.activeCount;if(o<n.minGroupTriangles)return e;let r=Math.max(1,Math.floor(o*n.ratio));if(r>=o)return e;let s=0;for(let i=0;i<n.maxPasses&&t.activeCount>r;i+=1){let c=Sm(t,n);if(c.length===0)break;let a=0;for(let l of c){if(t.activeCount<=r)break;let u=xm(t,l,n);if(!u)continue;let f=t.activeCount;Am(t,l,u),!(t.activeCount>=f)&&(s+=1,a+=1)}if(a===0)break}return s===0||t.activeCount>=o?e:Cm(t).map(i=>({...i.polygon,vertices:[[...t.vertices[i.a]],[...t.vertices[i.b]],[...t.vertices[i.c]]]}))}function Tm(e){let n=new Map;for(let t=0;t<e.length;t+=1){let o=e[t];if(!nm(o))continue;let r=tm(o),s=n.get(r);s?(s.indices.push(t),s.polygons.push(o)):n.set(r,{indices:[t],polygons:[o]})}return n}function Ar(e,n){let t=Jf(n),o=Tm(e);if(o.size===0)return e;let r=new Map,s=!1;for(let c of o.values()){let a=Em(c.polygons,t);if(!(a===c.polygons||a.length>=c.polygons.length)){r.set(c.indices[0],a);for(let l=1;l<c.indices.length;l+=1)r.set(c.indices[l],[]);s=!0}}if(!s)return e;let i=[];for(let c=0;c<e.length;c+=1){let a=r.get(c);a?i.push(...a):a===void 0&&i.push(e[c])}return i}var vm=3,Im=.03,_m=.02,Ot={maxAngleDeg:vm,maxPlaneDisplacement:Im,maxBoundaryDisplacement:_m,isolatedPairs:!1},po={maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.04,isolatedPairs:!0},Sr=[{...po,maxAngleDeg:30},{...po,maxAngleDeg:45},{...po,maxAngleDeg:60,maxBoundaryDisplacement:.06}],Om=1800,Vm=.65,ma=500,da=2200,Rm=1e3,Lm=2200,wm=350,Nm=700,Dm=60,Fm=.06,Bm=4,km=.003,Um=4e3,Gm=3e3,zm=4500,$m=2400,Xm=4200,Ym=1800,Wm=5e3,jm=.28,Km=.15,Hm=.23,Zm=2e3,qm=16,Qm=4e3,Jm=900,ed=16,nd=1.5,Er=.045,td=.999,od=1e-5,rd=24,sd=50,id=1e3,ad=1e3,Dn={minGroupPolygons:2,maxCandidateAxes:24},cd={...Dn,maxCandidateAxes:1},pa={...Dn,maxCandidateAxes:2},ld={...Dn,maxCandidateAxes:2},ud={steps:[{kind:"baselineCandidates"},{kind:"initialLossyCandidates"},{kind:"finalLossyCandidates"}]};function fd(e,n={}){return md(e,n).polygons}function xa(e,n={}){let t={meshResolution:n.meshResolution,stopAtPolygonCount:n.stopAtPolygonCount,rectCover:n.rectCover},o=new go,r={captureVisiblePolygons:!0,skipInteriorCull:n.skipInteriorCull===!0};return o.workspaceFor(e,r).createRun(t,r).optimizeParse({staticSimplification:n.staticSimplification,useCandidateFirst:n.useCandidateFirst===!0}).polygons}function md(e,n={},t={}){return new go().workspaceFor(e,t).createRun(n,t).optimize()}var go=class{constructor(){this.workspaces=new Map}workspaceFor(n,t){let o=dd(t),r=this.workspaces.get(o);r||(r=new WeakMap,this.workspaces.set(o,r));let s=r.get(n);return s||(s=new Tr(n,t,this),r.set(n,s)),s}};function dd(e){return[e.skipInteriorCull===!0?"skip-cull":"cull",e.simplifiedCandidate===!0?"simplified":"source"].join(":")}var Tr=class{constructor(n,t,o){this.source=n,this.graph=o,this.preprocessCache={skipInteriorCull:t.skipInteriorCull===!0}}polygons(){return this.source}preprocess(n){return Fd(this.source,n,this.preprocessCache)}configureSnappedCullReuse(n,t){let o=n.length/Math.max(1,this.source.length);this.preprocessCache.reuseSnappedInteriorCull=t.simplifiedCandidate===!0?this.source.length<=Xm:n.length<=Ym||n.length<=Wm&&o>=jm}visiblePolygons(){return this.preprocessCache.interior??this.preprocessCache.deduped??this.source}createRun(n,t){return new vr(this,n,t)}workspaceFor(n,t){return this.graph.workspaceFor(n,t)}},vr=class e{constructor(n,t,o,r={}){this.baselineCandidatesComplete=!1;this.initialLossyCandidatesComplete=!1;this.finalLossyCandidatesComplete=!1;this.workspace=n,this.source=n.polygons(),this.options=t,this.runOptions=o,this.costCandidateMode=r.costCandidateMode??"cost",this.seamCandidateMode=r.seamCandidateMode??"seamSafe",this.offerBaseline=r.offerBaseline===!0,this.meshResolution=t.meshResolution??"lossy",this.stopAtPolygonCount=Number.isFinite(t.stopAtPolygonCount)?Math.max(0,Math.floor(t.stopAtPolygonCount)):void 0,this.requiredMaxPolygonCount=Number.isFinite(o.requiredMaxPolygonCount)?Math.max(0,Math.floor(o.requiredMaxPolygonCount)):void 0,this.baseline=this.preprocess(!1),this.workspace.configureSnappedCullReuse(this.baseline,o),this.visiblePolygons=o.captureVisiblePolygons?this.workspace.visiblePolygons():void 0,this.acceptor=r.acceptor??new Ir(this.baseline,this.requiredMaxPolygonCount)}optimize(){return this.runPlan(ud)}optimizeParse(n){let t=n.staticSimplification===!1?null:n.staticSimplification??{},o=[{kind:"baselineCandidates"},{kind:"initialLossyCandidates"}];return n.useCandidateFirst||o.push({kind:"finalLossyCandidates"}),t&&o.push({kind:"staticSimplificationCandidates",options:t,stopOnAccept:n.useCandidateFirst}),n.useCandidateFirst&&o.push({kind:"finalLossyCandidates"}),this.runPlan({steps:o})}get best(){return this.acceptor.polygons}get bestCost(){return this.acceptor.cost}runStaticSimplificationCandidates(n={}){if(this.meshResolution!=="lossy"||!Id(this.source,this.best))return!1;let t=Ma(this.source,n);if(!t)return!1;for(let o of this.staticSimplificationPlans(t)){let r=_d(o,n);if(r&&this.generateStaticSimplificationCandidate(r,n,o.skipInteriorCull))return!0}return!1}staticSimplificationPlans(n){let t=this.source.length-n.length,o=[],r=this.visiblePolygons&&this.visiblePolygons!==this.source?this.source.length-this.visiblePolygons.length:0;return this.visiblePolygons&&this.visiblePolygons!==this.source&&this.source.length>=Qm&&r>=Jm&&(t<=ed||r>=t*nd)&&o.push({source:this.visiblePolygons,skipInteriorCull:!0}),this.best.length>=Zm&&t<=qm&&vd(this.source)&&o.push({source:this.source,vertexKeyMode:"source"}),o.push({source:this.source,precomputed:n}),o}generateStaticSimplificationCandidate(n,t,o=!1){let r={requiredMaxPolygonCount:this.best.length-1,skipExactRectCover:!0,skipInteriorCull:o,simplifiedCandidate:!0},s=this.workspace.workspaceFor(n,r),i=this.best;return new e(s,{meshResolution:"lossy",stopAtPolygonCount:Td(this.best.length,t)},r,{acceptor:this.acceptor,costCandidateMode:"dom",seamCandidateMode:"dom",offerBaseline:!0}).optimize(),this.best!==i}runPlan(n){for(let t of n.steps){let o=!1;if(t.kind==="baselineCandidates")this.runBaselineCandidates();else if(t.kind==="staticSimplificationCandidates")o=this.runStaticSimplificationCandidates(t.options)&&t.stopOnAccept;else{if(!this.shouldRunLossyPipeline())break;t.kind==="initialLossyCandidates"?this.runInitialLossyPipeline():this.runFinalLossyPipeline()}if(o||this.shouldStop())break}return this.result()}runBaselineCandidates(){if(this.baselineCandidatesComplete||(this.baselineCandidatesComplete=!0,this.offerBaseline&&(this.acceptCandidate(this.baseline),this.shouldStop())))return;let n=this.runOptions.skipExactRectCover===!0?!1:this.meshResolution==="lossy"&&this.options.rectCover===void 0?Vd(this.baseline):this.options.rectCover;if(this.shouldStop())return;let t=Nn(this.baseline,n);if(t!==this.baseline&&this.acceptCandidate(t),!this.shouldStop()&&this.meshResolution==="lossy"&&this.options.rectCover===void 0&&this.runOptions.skipExactRectCover!==!0){let o=Nn(this.baseline,void 0);o!==this.baseline&&this.acceptCandidate(o)}}shouldRunLossyPipeline(){return this.meshResolution==="lossy"&&!this.shouldStop()&&this.best.length>1&&this.bestCost>1+1e-9}runInitialLossyPipeline(){if(this.initialLossyCandidatesComplete)return;this.initialLossyCandidatesComplete=!0;let n=this.preprocess(po);this.acceptCandidate(n),!this.shouldStop()&&(this.options.rectCover===void 0&&this.source.length>=ma&&this.source.length<=da&&(this.acceptCandidate(Nn(n,pa)),this.shouldStop())||this.options.rectCover!==void 0&&this.options.rectCover!==!1&&(this.acceptCandidate(Nn(n,this.options.rectCover)),this.shouldStop()))}runFinalLossyPipeline(){this.finalLossyCandidatesComplete||(this.finalLossyCandidatesComplete=!0,!(this.runOptions.simplifiedCandidate===!0&&this.source.length>zm)&&(this.runAggressiveLossyVariants(),!this.shouldStop()&&this.runLargeRectCoverCandidate()))}runAggressiveLossyVariants(){let n=this.stopAtPolygonCount===void 0&&this.source.length>=Um&&this.best.length<=Gm,t=!1;for(let o=n?Sr.length:0;o<Sr.length;o+=1){if(o===1&&!t||o===1&&this.runOptions.simplifiedCandidate===!0&&this.source.length<$m||o===2&&!Ld(this.source))continue;let r=this.preprocess(Sr[o]),s=r;this.options.rectCover===void 0&&this.source.length>=ma&&this.source.length<=da&&(s=Nn(r,pa)),this.options.rectCover!==void 0&&this.options.rectCover!==!1&&(s=Nn(r,this.options.rectCover));let i=this.acceptSeamSafeCandidate(s,this.source.length);if(o===0&&i&&(t=!0),this.shouldStop())return}}runLargeRectCoverCandidate(){if(this.options.rectCover!==void 0)return;let n=Nn(this.best,Rd(this.best));n!==this.best&&this.acceptSeamSafeCandidate(n,this.best.length)}preprocess(n){return this.workspace.preprocess(n)}result(){return{polygons:this.best,visiblePolygons:this.visiblePolygons}}shouldStop(){return this.stopAtPolygonCount!==void 0&&this.best.length<=this.stopAtPolygonCount}acceptCandidate(n,t=Fn(n)){return this.emitCandidate({mode:this.costCandidateMode,polygons:n,cost:t})}acceptDomCandidate(n){return this.emitCandidate({mode:"dom",polygons:n})}acceptSeamSafeCandidate(n,t,o){return this.emitCandidate({mode:this.seamCandidateMode,polygons:n,sourcePolygonCount:t,cost:o})}emitCandidate(n){return this.acceptor.accept({...n,maxPolygonCount:n.maxPolygonCount??this.requiredMaxPolygonCount})}},Ir=class{constructor(n,t){this.best=n,this.bestCost=Fn(n),this.bestDiagnostics={polygons:n},this.requiredMaxPolygonCount=t}get polygons(){return this.best}get cost(){return this.bestCost}accept(n){return n.mode==="cost"?this.acceptCostCandidate(n.polygons,n.cost):n.mode==="dom"?this.acceptDomCandidate(n.polygons,n.cost,n.maxPolygonCount):this.acceptSeamSafeCandidate(n.polygons,n.sourcePolygonCount??n.polygons.length,n.cost,n.maxPolygonCount)}acceptCostCandidate(n,t=Fn(n)){return t>=this.bestCost?!1:(this.commit(n,t),!0)}acceptDomCandidate(n,t=Fn(n),o){return n.length>=this.best.length||!this.withinMaxPolygonCount(n,o)?!1:(this.commit(n,t),!0)}acceptSeamSafeCandidate(n,t,o,r){if(n.length>=this.best.length||!this.withinMaxPolygonCount(n,r))return!1;let s=Ed(t);if(this.bestCost-n.length<s)return!1;let i=o??Fn(n),c=this.bestCost-i;if(c<=0||c<s)return!1;let a=ga(n);if(!a)return!1;let l=this.bestSeamDiagnostics();return!l||pd(a,l)||gd(this.bestTopologyEdges(),this.bestTopologySelfDiagnostics(),n)?!1:(this.commit(n,i,a),!0)}withinMaxPolygonCount(n,t){let o=t??this.requiredMaxPolygonCount;return o===void 0||n.length<=o}commit(n,t,o){this.best=n,this.bestCost=t,this.bestDiagnostics={polygons:n,seam:o}}resetBestDiagnostics(n){this.bestDiagnostics={polygons:this.best,seam:n}}bestSeamDiagnostics(){if(this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),!this.bestDiagnostics.seam){let n=ga(this.best);if(!n)return null;this.bestDiagnostics.seam=n}return this.bestDiagnostics.seam}bestTopologyEdges(){return this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),this.bestDiagnostics.topologyEdges||(this.bestDiagnostics.topologyEdges=Pa(this.best)),this.bestDiagnostics.topologyEdges}bestTopologySelfDiagnostics(){return this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),this.bestDiagnostics.topologySelf||(this.bestDiagnostics.topologySelf=xd(this.bestTopologyEdges(),Er)),this.bestDiagnostics.topologySelf}};function Fn(e){let n=0;for(let t of e){let o=t.vertices.length,r=o<=4?0:Math.min(4,o-4)*.12,s=t.texture||t.material?.texture||t.textureTriangles?.length?.15:0;n+=1+r+s}return n}function ga(e){try{return aa(e)}catch(n){if(n instanceof RangeError&&n.message==="Set maximum size exceeded")return null;throw n}}function pd(e,n){return e.nearPairs>n.nearPairs||e.unclosedPairs>n.unclosedPairs||e.maxMeasuredGapPx>n.maxMeasuredGapPx+1e-9||e.maxResidualGapPx>n.maxResidualGapPx+1e-9}function gd(e,n,t){let o=Pa(t);return hd(e,o,Er)?!0:yd(o.boundarySegments,Er,n)}function hd(e,n,t){if(e.internalSegments.length===0)return!1;let o=bd(e,t);for(let r of n.boundarySegments)if(!e.boundaryKeys.has(r.key)&&(e.internalKeys.has(r.key)||Sd(r,o,t)))return!0;return!1}function bd(e,n){return(!e.internalIndex||e.internalIndexTolerance!==n)&&(e.internalIndex=Vr(e.internalSegments,n),e.internalIndexTolerance=n),e.internalIndex}function yd(e,n,t){let o=Vr(e,n),r=new Set,s=0,i=0,c=0;for(let a of e)s=Math.max(s,a.index+1);for(let a of e)for(let l of Rr(a,o,n)){if(l===a||l.polygon===a.polygon||l.key===a.key)continue;let u=a.index<l.index?a.index*s+l.index:l.index*s+a.index;if(r.has(u))continue;r.add(u);let f=Lr(a,l,n);if(f&&(i+=1,c+=f.overlapLength,i>t.tJunctionPairs||c>t.tJunctionLength+1e-9))return!0}return!1}function xd(e,n){let t={tJunctionPairs:0,tJunctionLength:0};return Pd(t,e.boundarySegments,n),t}function Pd(e,n,t){let o=Vr(n,t),r=new Set,s=0;for(let i of n)s=Math.max(s,i.index+1);for(let i of n)for(let c of Rr(i,o,t)){if(c===i||c.polygon===i.polygon||c.key===i.key)continue;let a=i.index<c.index?i.index*s+c.index:c.index*s+i.index;if(r.has(a))continue;r.add(a);let l=Lr(i,c,t);l&&(e.tJunctionPairs+=1,e.tJunctionLength+=l.overlapLength)}}function Pa(e){let n=new Map;for(let a=0;a<e.length;a+=1){let l=e[a].vertices;for(let u=0;u<l.length;u+=1){let f=l[u],d=l[(u+1)%l.length],m=Md(Lt(f,d),a,u,f,d);if(!m)continue;let p=n.get(m.key);p?p.count+=1:n.set(m.key,{count:1,segment:m})}}let t=new Set,o=new Set,r=[],s=[],i=0,c=0;for(let a of n.values()){let l={...a.segment,index:c};c+=1,a.count===1?(t.add(l.key),r.push(l),i+=l.length):(o.add(l.key),s.push(l))}return{boundaryKeys:t,internalKeys:o,boundarySegments:r,internalSegments:s,boundaryLength:i}}function Md(e,n,t,o,r){let s=pn(r,o),i=Math.hypot(s[0],s[1],s[2]);if(i<=1e-10)return null;let c=[s[0]/i,s[1]/i,s[2]/i];return{index:-1,key:e,polygon:n,edge:t,a:o,b:r,dir:c,length:i,minX:Math.min(o[0],r[0]),minY:Math.min(o[1],r[1]),minZ:Math.min(o[2],r[2]),maxX:Math.max(o[0],r[0]),maxY:Math.max(o[1],r[1]),maxZ:Math.max(o[2],r[2])}}function Vr(e,n){let t=Math.max(n*8,.5),o=new Map;for(let r of e)Ad(o,r,t,n);return{cellSize:t,cells:o}}function Ad(e,n,t,o){let[r,s,i]=ho([n.minX-o,n.minY-o,n.minZ-o],t),[c,a,l]=ho([n.maxX+o,n.maxY+o,n.maxZ+o],t);for(let u=r;u<=c;u+=1)for(let f=s;f<=a;f+=1)for(let d=i;d<=l;d+=1){let m=`${u},${f},${d}`,p=e.get(m);p?p.push(n):e.set(m,[n])}}function ho(e,n){return[Math.floor(e[0]/n),Math.floor(e[1]/n),Math.floor(e[2]/n)]}function Rr(e,n,t){let o=[],r=new Set,[s,i,c]=ho([e.minX-t,e.minY-t,e.minZ-t],n.cellSize),[a,l,u]=ho([e.maxX+t,e.maxY+t,e.maxZ+t],n.cellSize);for(let f=s;f<=a;f+=1)for(let d=i;d<=l;d+=1)for(let m=c;m<=u;m+=1){let p=n.cells.get(`${f},${d},${m}`);if(p)for(let h of p)r.has(h)||(r.add(h),o.push(h))}return o}function Sd(e,n,t){for(let o of Rr(e,n,t))if(Lr(e,o,t))return!0;return!1}function Lr(e,n,t){if(!Cd(e,n,t)||Math.abs(Ke(e.dir,n.dir))<td)return null;let o=Ke(pn(n.a,e.a),e.dir),r=Ke(pn(n.b,e.a),e.dir),s=Math.max(0,Math.min(o,r)),i=Math.min(e.length,Math.max(o,r)),c=i-s;if(c<=Math.max(od,Math.min(e.length,n.length)*1e-4))return null;let a=[e.a[0]+e.dir[0]*((s+i)/2),e.a[1]+e.dir[1]*((s+i)/2),e.a[2]+e.dir[2]*((s+i)/2)],l=Math.max(0,Math.min(n.length,Ke(pn(a,n.a),n.dir))),u=[n.a[0]+n.dir[0]*l,n.a[1]+n.dir[1]*l,n.a[2]+n.dir[2]*l],f=Na(a,u);return f<=t?{overlapLength:c,offset:f}:null}function Cd(e,n,t){return e.minX<=n.maxX+t&&n.minX<=e.maxX+t&&e.minY<=n.maxY+t&&n.minY<=e.maxY+t&&e.minZ<=n.maxZ+t&&n.minZ<=e.maxZ+t}function Ed(e){return Math.max(Bm,e*km)}function Td(e,n){let t=Number.isFinite(n.earlyStopDropRatio)?Math.max(0,n.earlyStopDropRatio):Km;return Math.max(0,e-Math.max(1,Math.ceil(e*t)))}function vd(e){return e.some(n=>n.simplifySourceVertexKeys?.length===n.vertices.length)}function Id(e,n){return e.length===0||n.length/e.length>Hm}function Ma(e,n,t){let o=Ar(e,{...n.simplifyTriangleMeshOptions,...t?{vertexKeyMode:t}:{}});return o===e||o.length>=e.length?null:o}function _d(e,n){return e.precomputed!==void 0?e.precomputed:Ma(e.source,n,e.vertexKeyMode)}function Nn(e,n){if(n===!1)return e;let t=Od(e,n);if(!t)return e;let o=sr(e,t);return o.length<e.length?o:e}function Od(e,n){if(n&&n!==!0)return n;let t=n===!0,o=e.length;return o>2200||!t&&o>=rd&&o<sd||!t&&o>id?null:!t&&o<=300?{...Dn,maxCandidateAxes:1}:o>300?!t&&(Aa(e)>12||Sa(e)>ad)?null:{...Dn,maxCandidateAxes:Math.min(Dn.maxCandidateAxes??24,2)}:Dn}function Vd(e){return e.length>Om||e.length===0||wd(e)/e.length<Vm?!1:cd}function Rd(e){return e.length<Rm||e.length>Lm||Aa(e)<=12||Sa(e)>wm?!1:ld}function Ld(e){return e.length<=Nm}function wd(e){let n=0;for(let t of e)t.vertices.length===3&&(n+=1);return n}function Aa(e){let n=0;for(let t of e)n=Math.max(n,t.vertices.length);return n}function Sa(e){let n=new Map;for(let o of e)for(let r=0;r<o.vertices.length;r++){let s=o.vertices[r],i=o.vertices[(r+1)%o.vertices.length],c=Lt(s,i);n.set(c,(n.get(c)??0)+1)}let t=0;for(let o of n.values())o===1&&(t+=1);return t}function bo(e,n){return n==null?e:n.map(t=>e[t]).filter(t=>!!t)}function _r(e,n){if(n===e||n.length===e.length&&n.every((r,s)=>r===e[s]))return null;let t=new Set(n),o=[];for(let r=0;r<e.length;r++)t.has(e[r])&&o.push(r);return o.length===e.length?null:o}function Nd(e,n){if(n?.deduped)return n.deduped;let t=n?.dedupedIndices;if(t===void 0){let r=Jt(e);if(r.size===0)t=null;else{t=[];for(let s=0;s<e.length;s++)r.has(s)||t.push(s)}n&&(n.dedupedIndices=t)}let o=bo(e,t);return n&&(n.deduped=o),o}function Dd(e,n){if(n?.skipInteriorCull)return e;if(n?.interior)return n.interior;let t=n?.interiorIndices;if(t===void 0){let r=fn(e);t=_r(e,r),n&&(n.interiorIndices=t)}let o=bo(e,t);return n&&(n.interior=o),o}function Fd(e,n,t){let o=Nd(e,t),r=Dd(o,t),s=t?.baseline??an(r);if(t&&!t.baseline&&(t.baseline=s),!n)return s;let i=n===!0?Ot:kd(n);if(i.isolatedPairs){let u=ba(ha(o,t),i,t),f=an(u);if(t?.snappedInteriorUsesBaselineFilter&&Bd(s,f)){let d=ba(ha(o,t,!0),i,t),m=an(d);m.length<f.length&&(f=m)}return f.length<s.length?f:s}let c=Qd(o,i,t),a=fn(c),l=an(a);return l.length<s.length?l:s}function Ca(e,n){return n?(n.snapped||(n.snapped=yo(e)),n.snapped):yo(e)}function ha(e,n,t=!1){if(!n)return fn(yo(e));if(t&&n.snappedInteriorExact)return n.snappedInteriorExact;if(!t&&n.snappedInterior)return n.snappedInterior;let o=Ca(e,n);if(t){if(o===e&&n.deduped===e&&n.interior)return n.snappedInteriorExactIndices=n.interiorIndices,n.snappedInteriorExact=n.interior,n.snappedInteriorExact;let r=fn(o);return n.snappedInteriorExactIndices=_r(o,r),n.snappedInteriorExact=r,n.snappedInteriorExact}if(!n.snappedInterior){if(o===e&&n.deduped===e&&n.interior)return n.snappedInteriorIndices=n.interiorIndices,n.snappedInterior=n.interior,n.snappedInteriorUsesBaselineFilter=!1,n.snappedInterior;if(n.reuseSnappedInteriorCull!==!1&&n.interiorIndices!==void 0)return n.snappedInteriorIndices=n.interiorIndices,n.snappedInterior=bo(o,n.snappedInteriorIndices),n.snappedInteriorUsesBaselineFilter=!0,n.snappedInterior;if(n.snappedInteriorIndices===void 0){let r=fn(o);n.snappedInteriorIndices=_r(o,r),n.snappedInterior=r,n.snappedInteriorExactIndices=n.snappedInteriorIndices,n.snappedInteriorExact=r,n.snappedInteriorUsesBaselineFilter=!1}else n.snappedInterior=bo(o,n.snappedInteriorIndices)}return n.snappedInterior}function Bd(e,n){return n.length>=e.length}function kd(e){return{maxAngleDeg:e.maxAngleDeg??Ot.maxAngleDeg,maxPlaneDisplacement:e.maxPlaneDisplacement??Ot.maxPlaneDisplacement,maxBoundaryDisplacement:e.maxBoundaryDisplacement??Ot.maxBoundaryDisplacement,isolatedPairs:e.isolatedPairs??Ot.isolatedPairs}}function ba(e,n,t){let o=Ud(e,t),r=[];for(let i of Gd(e,o))zd(i,n)&&r.push(i.candidate);let s=Xd(r);return s.length===0?e:$d(e,s)}function Ud(e,n){if(n?.trianglePairSource?.polygons===e)return n.trianglePairSource;let t=e.map(i=>{let c=Nr(i);return c?{polygon:i,normal:c.normal,area:c.area,materialKey:Oa(i)}:null}),o=new Map;for(let i=0;i<e.length;i++){let c=e[i];if(!(c.vertices.length!==3||!t[i]))for(let a=0;a<c.vertices.length;a++){let l=Lt(c.vertices[a],c.vertices[(a+1)%c.vertices.length]),u=o.get(l);u?u.push(i):o.set(l,[i])}}let r=[];for(let i of o.values())i.length===2&&r.push([i[0],i[1]]);let s={polygons:e,metas:t,edgeOwnerPairs:r};return n&&(n.trianglePairSource=s),s}function Gd(e,n){if(n.preparedCandidates)return n.preparedCandidates;let t=[];for(let[o,r]of n.edgeOwnerPairs){let s=Hd(o,r,e,n.metas);s&&t.push(s)}return n.preparedCandidates=t,t}function zd(e,n){let t=Math.cos(n.maxAngleDeg*Math.PI/180);return e.normalDot>=t&&e.maxDistance<=Math.min(n.maxPlaneDisplacement,n.maxBoundaryDisplacement)}function $d(e,n){let t=new Map,o=new Set,r=Ea(n.flatMap(i=>i.vertexMoves));for(let i of n){let c=Math.min(i.a,i.b);t.set(c,i.polygon),o.add(Math.max(i.a,i.b))}let s=[];for(let i=0;i<e.length;i++){let c=t.get(i);if(c){s.push(c);continue}o.has(i)||s.push(e[i])}return r.size>0?Ta(s,r):s}function Xd(e){return e.length>3e3?Yd(e):Wd(e)}function Yd(e){let n=new Map;for(let s of e)n.set(s.a,(n.get(s.a)??0)+1),n.set(s.b,(n.get(s.b)??0)+1);let t=[...e].sort((s,i)=>{let c=(n.get(s.a)??0)+(n.get(s.b)??0),a=(n.get(i.a)??0)+(n.get(i.b)??0);return c-a||s.score-i.score}),o=new Set,r=[];for(let s of t)o.has(s.a)||o.has(s.b)||(o.add(s.a),o.add(s.b),r.push(s));return r}function Wd(e){let n=new Map;for(let l=0;l<e.length;l++){let u=e[l],f=n.get(u.a);f?f.push(l):n.set(u.a,[l]);let d=n.get(u.b);d?d.push(l):n.set(u.b,[l])}let t=[],o=new Array(e.length).fill(!0),r=new Map,s=new Or;for(let[l,u]of n)r.set(l,u.length);let i=l=>(r.get(l.a)??0)+(r.get(l.b)??0),c=l=>{let u=e[l];s.push({degree:i(u),score:u.score,index:l})},a=(l,u)=>{if(!o[l])return;o[l]=!1;let f=e[l];for(let d of[f.a,f.b])r.set(d,(r.get(d)??0)-1),u.add(d)};for(let l=0;l<e.length;l++)c(l);for(;s.size()>0;){let l=s.pop();if(!o[l.index])continue;let u=e[l.index];if(i(u)!==l.degree){c(l.index);continue}t.push(u);let d=new Set;for(let m of[u.a,u.b])for(let p of n.get(m)??[])a(p,d);for(let m of d)for(let p of n.get(m)??[])o[p]&&c(p)}return t}var Or=class{constructor(){this.items=[]}size(){return this.items.length}push(n){this.items.push(n);let t=this.items.length-1;for(;t>0;){let o=t-1>>1;if(Cr(this.items[o],this.items[t])<=0)break;[this.items[o],this.items[t]]=[this.items[t],this.items[o]],t=o}}pop(){if(this.items.length===0)return null;let n=this.items[0],t=this.items.pop();if(this.items.length>0){this.items[0]=t;let o=0;for(;;){let r=o*2+1,s=r+1,i=o;if(r<this.items.length&&Cr(this.items[r],this.items[i])<0&&(i=r),s<this.items.length&&Cr(this.items[s],this.items[i])<0&&(i=s),i===o)break;[this.items[o],this.items[i]]=[this.items[i],this.items[o]],o=i}}return n}};function Cr(e,n){return e.degree-n.degree||e.score-n.score||e.index-n.index}function Ea(e){let n=new Map;for(let o of e){let r=n.get(o.key);r?(r.x+=o.target[0],r.y+=o.target[1],r.z+=o.target[2],r.count+=1):n.set(o.key,{x:o.target[0],y:o.target[1],z:o.target[2],count:1})}let t=new Map;for(let[o,r]of n)t.set(o,[r.x/r.count,r.y/r.count,r.z/r.count]);return t}function jd(e,n){let t=[];for(let o=0;o<e.length;o++){let r=e[o].vertices,s=n[o]?.vertices;if(!s||s.length!==r.length)continue;for(let a=0;a<r.length;a++)t.push({key:tn(r[a]),target:s[a]});let i=e[o].textureTriangles??[],c=n[o]?.textureTriangles??[];for(let a=0;a<i.length;a++){let l=c[a];if(l)for(let u=0;u<i[a].vertices.length;u++)t.push({key:tn(i[a].vertices[u]),target:l.vertices[u]})}}return t}function Kd(e,n){let t=[];for(let o of e)for(let r of o.textureTriangles??[])for(let s of r.vertices)t.push({key:tn(s),target:kr(s,n)});return t}function Ta(e,n){return e.map(t=>{let o=!1,r=c=>{let a=n.get(tn(c));return a?(o=!0,a):c},s=t.vertices.map(r),i=wr(t.textureTriangles,r);return o?{...t,vertices:s,...i?{textureTriangles:i}:{}}:t})}function Hd(e,n,t,o){let r=t[e],s=t[n],i=o[e],c=o[n];if(!i||!c||r.vertices.length!==3||s.vertices.length!==3||!up(r,s,i,c))return null;let a=Fr(r,s);if(!a)return null;let[l,u,f,d]=a;if((f+1)%s.vertices.length===d)return null;let p=Math.abs(Ke(i.normal,c.normal)),h=Math.cos(Dm*Math.PI/180);if(p<h)return null;let g=(u+1)%r.vertices.length,b=3-f-d,y=[r.vertices[u],r.vertices[g],r.vertices[l],s.vertices[b]],x=Zd(y);if(!x)return null;let P=0,S=0;for(let M of y){let E=Math.abs(Br(M,x));P=Math.max(P,E),S+=E*E}if(P>Fm)return null;let C=y.map(M=>kr(M,x));if(!qd(C,x.normal))return null;let T=Nr({vertices:C});if(!T||Ke(T.normal,i.normal)<.2||Ke(T.normal,c.normal)<.2)return null;let A={vertices:y,color:r.color,...r.doubleSided?{doubleSided:!0}:{},...r.data?{data:{...r.data}}:{}};if(Dr(r,s)&&r.uvs&&s.uvs&&r.texture){A.texture=r.texture,r.textureWrap&&(A.textureWrap={...r.textureWrap}),r.textureAlphaMode&&(A.textureAlphaMode=r.textureAlphaMode),A.uvs=[[...r.uvs[u]],[...r.uvs[g]],[...r.uvs[l]],[...s.uvs[b]]];let M=Jd([r,s]);M?.length&&(A.textureTriangles=M)}return{normalDot:p,maxDistance:P,candidate:{a:e,b:n,polygon:A,vertexMoves:[...y.map((M,E)=>({key:tn(M),target:C[E]})),...Kd([r,s],x)],score:S/y.length+P*.25+(1-p)*.1}}}function Zd(e){if(e.length<3)return null;let n=0,t=0,o=0,r=0,s=0,i=0;for(let a=0;a<e.length;a++){let l=e[a],u=e[(a+1)%e.length];n+=(l[1]-u[1])*(l[2]+u[2]),t+=(l[2]-u[2])*(l[0]+u[0]),o+=(l[0]-u[0])*(l[1]+u[1]),r+=l[0],s+=l[1],i+=l[2]}let c=Da([n,t,o]);return c?{normal:c,point:[r/e.length,s/e.length,i/e.length]}:null}function qd(e,n){let t=0;for(let o=0;o<e.length;o++){let r=e[o],s=e[(o+1)%e.length],i=e[(o+2)%e.length],c=Ke(wa(pn(s,r),pn(i,s)),n);if(Math.abs(c)<=1e-9)continue;let a=c>0?1:-1;if(t===0)t=a;else if(t!==a)return!1}return!0}function Qd(e,n,t){let o=Ca(e,t),r=sp(o,n);if(r<=0)return o;let s=o.map(m=>{let p=Nr(m);return p?{polygon:m,normal:p.normal,area:p.area,materialKey:Oa(m)}:null}),i=cp(o,s),c=new Set,a=Array(o.length),l=[],u=(m,p)=>{a[m]=p};for(let m=0;m<o.length;m++){let p=s[m];if(c.has(m))continue;if(!p){u(m,o[m]);continue}let h=fp(m,s,i,c,r,n);for(let b of h)c.add(b);if(h.length<2){u(m,o[m]);continue}let g=tp(h,o,s,i,r,n);l.push(...g.vertexMoves);for(let b of h)u(b,g.polygons.get(b)??o[b])}let f=a.flatMap(m=>m?[m]:[]),d=l.length>0?Ta(f,Ea(l)):f;return yo(d)}function yo(e){let n=_a(e),t=1e-4;if(n<=0)return e;let o=ip(n),r=ap(t),s=!1,i=e.map(c=>{let a=!1,l=p=>{let h=o.snap(p);return Vt(h,p)||(a=!0),h},u=c.vertices.map(l),f=c.uvs&&c.uvs.length===c.vertices.length?c.uvs.map(p=>{let h=r.snap(p);return Rt(h,p)||(a=!0),h}):void 0,d=wr(c.textureTriangles,l);if(!a&&!c.texture)return c;let m={...c,vertices:u,...f?{uvs:f}:{},...d?{textureTriangles:d}:{}};return s=!0,{...m,...m.texture?{textureTriangles:va(m)}:{}}});return s?i:e}function va(e){if(e.texture){if(e.textureTriangles?.length)return np(e.textureTriangles);if(e.uvs&&e.uvs.length===e.vertices.length)return ep(e.vertices,e.uvs)}}function Jd(e){let n=e.flatMap(t=>va(t)??[]);return n.length>0?n:void 0}function ep(e,n){let t=[];for(let o=1;o<e.length-1;o++)t.push({vertices:[[...e[0]],[...e[o]],[...e[o+1]]],uvs:[[...n[0]],[...n[o]],[...n[o+1]]]});return t}function np(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function wr(e,n){if(e?.length)return e.map(t=>({vertices:t.vertices.map(n),uvs:t.uvs.map(o=>[...o])}))}function tp(e,n,t,o,r,s){let i=Ia(e,n,t,r,s);return i?rp(i):op(e,n,t,o,r,s)}function op(e,n,t,o,r,s){let i=new Set(e),c=[];for(let f of e)for(let d of o.get(f)??[]){if(f>=d||!i.has(d))continue;let m=Ia([f,d],n,t,r,s);m&&c.push(m)}c.sort((f,d)=>d.score-f.score);let a=new Set,l=new Map,u=[];for(let f of c)if(!f.indices.some(d=>a.has(d))){u.push(...f.vertexMoves);for(let d=0;d<f.indices.length;d++){let m=f.indices[d];a.add(m),l.set(m,n[m])}}return{polygons:l,vertexMoves:u}}function rp(e){let n=new Map;for(let t=0;t<e.indices.length;t++)n.set(e.indices[t],e.source[t]);return{polygons:n,vertexMoves:e.vertexMoves}}function Ia(e,n,t,o,r){let s=Va(e,t);if(!s||!Ra(e,t,s,o,r))return null;let i=e.map(u=>n[u]),c=i.map(u=>pp(u,s)),a=Fn(an(i)),l=Fn(an(c));return l>=a?null:{indices:e,source:i,projected:c,vertexMoves:jd(i,c),score:a-l}}function sp(e,n){return _a(e)<=0?0:n.maxPlaneDisplacement}function _a(e){let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0;for(let a of e)for(let[l,u,f]of a.vertices)n=Math.min(n,l),t=Math.min(t,u),o=Math.min(o,f),r=Math.max(r,l),s=Math.max(s,u),i=Math.max(i,f);if(!Number.isFinite(n))return 0;let c=Math.hypot(r-n,s-t,i-o);return c<=0?0:Math.min(.025,Math.max(1e-4,c*25e-5))}function ip(e){let n=new Map,t=r=>Math.floor(r/e),o=(r,s,i)=>`${r},${s},${i}`;return{snap(r){let s=t(r[0]),i=t(r[1]),c=t(r[2]);for(let f=-1;f<=1;f++)for(let d=-1;d<=1;d++)for(let m=-1;m<=1;m++){let p=n.get(o(s+f,i+d,c+m));if(p){for(let h of p)if(Na(r,h)<=e)return[h[0],h[1],h[2]]}}let a=[r[0],r[1],r[2]],l=o(s,i,c),u=n.get(l);return u?u.push(a):n.set(l,[a]),a}}}function ap(e){let n=new Map,t=r=>Math.floor(r/e),o=(r,s)=>`${r},${s}`;return{snap(r){let s=t(r[0]),i=t(r[1]);for(let u=-1;u<=1;u++)for(let f=-1;f<=1;f++){let d=n.get(o(s+u,i+f));if(d){for(let m of d)if(Math.hypot(r[0]-m[0],r[1]-m[1])<=e)return[m[0],m[1]]}}let c=[r[0],r[1]],a=o(s,i),l=n.get(a);return l?l.push(c):n.set(a,[c]),c}}}function Oa(e){return[e.color??"#cccccc",e.texture??"",e.texture&&e.textureWrap?`${e.textureWrap.s}/${e.textureWrap.t}`:"",e.texture&&e.textureAlphaMode?e.textureAlphaMode:"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided"].join("|")}function Nr(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0,s=n[0];for(let c=1;c<n.length-1;c++){let a=pn(n[c],s),l=pn(n[c+1],s),u=wa(a,l);t+=u[0],o+=u[1],r+=u[2]}let i=Math.hypot(t,o,r);return i<=1e-10?null:{normal:[t/i,o/i,r/i],area:i/2}}function cp(e,n){let t=new Map,o=new Map;for(let r=0;r<e.length;r++){let s=e[r];if(!(!n[r]||s.vertices.length<3))for(let i=0;i<s.vertices.length;i++){let c=Lt(s.vertices[i],s.vertices[(i+1)%s.vertices.length]),a=t.get(c);a?a.push(r):t.set(c,[r])}}for(let r of t.values())for(let s=0;s<r.length;s++)for(let i=s+1;i<r.length;i++){let c=r[s],a=r[i];lp(e[c],e[a],n[c],n[a])&&(ya(o,c,a),ya(o,a,c))}return o}function lp(e,n,t,o){if(!t||!o||t.materialKey!==o.materialKey||!!e.uvs!=!!n.uvs)return!1;if(xo(e)||xo(n))return Dr(e,n);if(!e.uvs||!n.uvs)return!0;let r=Fr(e,n);if(!r)return!1;let[s,i,c,a]=r;return Rt(e.uvs[s],n.uvs[c])&&Rt(e.uvs[i],n.uvs[a])}function up(e,n,t,o){return t.materialKey!==o.materialKey?!1:xo(e)||xo(n)?Dr(e,n):!e.uvs&&!n.uvs&&!e.textureTriangles?.length&&!n.textureTriangles?.length}function xo(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function Dr(e,n){if(!e.texture||!n.texture||e.texture!==n.texture||(e.textureWrap?.s??"")!==(n.textureWrap?.s??"")||(e.textureWrap?.t??"")!==(n.textureWrap?.t??"")||(e.textureAlphaMode??"")!==(n.textureAlphaMode??"")||e.material?.texture||n.material?.texture||!e.uvs||!n.uvs||e.uvs.length!==e.vertices.length||n.uvs.length!==n.vertices.length)return!1;let t=Fr(e,n);if(!t)return!1;let[o,r,s,i]=t;return Rt(e.uvs[o],n.uvs[s])&&Rt(e.uvs[r],n.uvs[i])}function ya(e,n,t){let o=e.get(n);o?o.add(t):e.set(n,new Set([t]))}function fp(e,n,t,o,r,s){let i=[e],c=new Set([e]),a=[e];for(;a.length>0;){let l=a.shift();for(let u of t.get(l)??[]){if(o.has(u)||c.has(u))continue;let f=n[u],d=n[e];!f||!d||f.materialKey===d.materialKey&&mp([...i,u],n,r,s)&&(i.push(u),c.add(u),a.push(u))}}return i}function mp(e,n,t,o){let r=Va(e,n);return!!r&&Ra(e,n,r,t,o)}function Va(e,n){let t=n[e[0]];if(!t)return null;let o=0,r=0,s=0,i=0,c=0,a=0,l=0;for(let m of e){let p=n[m];if(!p)return null;let h=Ke(t.normal,p.normal)<0?-1:1,g=Math.max(p.area,1e-6);o+=p.normal[0]*h*g,r+=p.normal[1]*h*g,s+=p.normal[2]*h*g;for(let b of p.polygon.vertices)i+=b[0],c+=b[1],a+=b[2],l+=1}let u=Da([o,r,s]);if(!u||l===0)return null;let f=La(e,n),d=dp(e,n,u,f);if(d){let m=(d.min+d.max)/2;return{normal:u,point:[u[0]*m,u[1]*m,u[2]*m],boundaryVertexKeys:f}}return{normal:u,point:[i/l,c/l,a/l],boundaryVertexKeys:f}}function dp(e,n,t,o){let r=1/0,s=-1/0;for(let i of e){let c=n[i];if(c)for(let a of c.polygon.vertices){if(!o.has(tn(a)))continue;let l=Ke(a,t);r=Math.min(r,l),s=Math.max(s,l)}}return Number.isFinite(r)&&Number.isFinite(s)?{min:r,max:s}:null}function Ra(e,n,t,o,r){let s=Math.cos(r.maxAngleDeg*Math.PI/180),i=t.boundaryVertexKeys??La(e,n);for(let c of e){let a=n[c];if(!a||Math.abs(Ke(a.normal,t.normal))<s)return!1;for(let l of a.polygon.vertices){let u=i.has(tn(l))?r.maxBoundaryDisplacement:o;if(Math.abs(Br(l,t))>u)return!1}}return!0}function La(e,n){let t=new Map;for(let r of e){let s=n[r];if(!s)continue;let i=s.polygon.vertices;for(let c=0;c<i.length;c++){let a=i[c],l=i[(c+1)%i.length],u=Lt(a,l),f=t.get(u);f?f.count+=1:t.set(u,{count:1,a,b:l})}}let o=new Set;for(let r of t.values())r.count===1&&(o.add(tn(r.a)),o.add(tn(r.b)));return o}function pp(e,n){let t=r=>kr(r,n),o=wr(e.textureTriangles,t);return{...e,vertices:e.vertices.map(t),...o?{textureTriangles:o}:{}}}function Fr(e,n){for(let t=0;t<e.vertices.length;t++){let o=(t+1)%e.vertices.length;for(let r=0;r<n.vertices.length;r++){let s=(r+1)%n.vertices.length;if(Vt(e.vertices[t],n.vertices[r])&&Vt(e.vertices[o],n.vertices[s]))return[t,o,r,s];if(Vt(e.vertices[t],n.vertices[s])&&Vt(e.vertices[o],n.vertices[r]))return[t,o,s,r]}}return null}function Lt(e,n){let t=tn(e),o=tn(n);return t<o?`${t}|${o}`:`${o}|${t}`}function tn(e){return`${e[0]},${e[1]},${e[2]}`}function Vt(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function Rt(e,n){return Math.abs(e[0]-n[0])<=1e-4&&Math.abs(e[1]-n[1])<=1e-4}function pn(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function wa(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function Ke(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Na(e,n){return Math.hypot(e[0]-n[0],e[1]-n[1],e[2]-n[2])}function Da(e){let n=Math.hypot(e[0],e[1],e[2]);return n<=1e-10?null:[e[0]/n,e[1]/n,e[2]/n]}function Br(e,n){return Ke(pn(e,n.point),n.normal)}function kr(e,n){let t=Br(e,n);return[e[0]-n.normal[0]*t,e[1]-n.normal[1]*t,e[2]-n.normal[2]*t]}var gp=36,hp=.15,bp=.2,yp=650,xp=15e3,Pp=.25;function Ur(e){return!!(e.texture||e.material?.texture||e.uvs?.length||e.textureTriangles?.length)}function Fa(e){let n=Pn(e);return!n||n.alpha<1?e.trim().toLowerCase():`#${n.rgb.map(t=>Math.max(0,Math.min(255,Math.round(t))).toString(16).padStart(2,"0")).join("")}`}function Mp(e){let n=Pn(e);return n?{rgb:[Math.max(0,Math.min(255,Math.round(n.rgb[0]))),Math.max(0,Math.min(255,Math.round(n.rgb[1]))),Math.max(0,Math.min(255,Math.round(n.rgb[2])))],alpha:n.alpha}:null}function Ap(e,n){return Math.hypot(e.rgb[0]-n.rgb[0],e.rgb[1]-n.rgb[1],e.rgb[2]-n.rgb[2])}function Ba(e){let n=e.rgb[0]/255,t=e.rgb[1]/255,o=e.rgb[2]/255,r=Math.max(n,t,o),s=Math.min(n,t,o),i=r-s,c=0;return i!==0&&(r===n?c=60*((t-o)/i%6):r===t?c=60*((o-n)/i+2):c=60*((n-t)/i+4)),c<0&&(c+=360),{hue:c,saturation:r===0?0:i/r,value:r}}function Sp(e,n){let t=Math.abs(e-n)%360;return t>180?360-t:t}function Cp(e,n){if(e.alpha<1||n.alpha<1)return!1;let t=Ba(e),o=Ba(n),r=t.saturation<.08,s=o.saturation<.08;if(r||s)return r===s;let i=Math.min(t.value,o.value)<.18?32:18;return Sp(t.hue,o.hue)<=i}function Ep(e,n){let t=new Map;for(let i of e.keys()){let c=Mp(i);c&&t.set(i,c)}let o=[],r=new Map,s=Array.from(e.entries()).sort((i,c)=>c[1]-i[1]||i[0].localeCompare(c[0]));for(let[i]of s){let c=t.get(i);if(!c){r.set(i,i);continue}let a=null;for(let l of o){if(!Cp(c,l.parsed))continue;let u=Ap(c,l.parsed);u>n||(!a||u<a.distance)&&(a={color:l.color,distance:u})}a?r.set(i,a.color):(o.push({color:i,parsed:c}),r.set(i,i))}return r}function Tp(e,n,t){if(t!=="lossy"||e.animation)return e;let o=n.source;if(!o||o.polygons===e.polygons)return e;let r=n.bakedTextureColorMergeDistance??gp;if(!Number.isFinite(r)||r<=0)return e;let s=new Map,i=[];for(let u=0;u<e.polygons.length;u+=1){let f=o.polygons[u],d=e.polygons[u];if(!f||!d||f===d||!d.color||!Ur(f)||Ur(d))continue;let m=Fa(d.color);i.push(u),s.set(m,(s.get(m)??0)+1)}if(s.size<2)return e;let c=Ep(s,r),a=!1,l=e.polygons.slice();for(let u of i){let f=l[u],d=Fa(f.color),m=c.get(d)??d;m!==d&&(l[u]={...f,color:m},a=!0)}return a?{...e,polygons:l}:e}function vp(e){let n=0;for(let t of e)Ur(t)&&(n+=1);return e.length===0||n/e.length<=Pp}function Ip(e){return!!e.metadata?.stlTopology}function _p(e,n){return n?Math.max(e.simplifyEarlyStopDropRatio??hp,bp):e.simplifyEarlyStopDropRatio}function nt(e,n={}){return new Gr(e,n).optimize()}var Gr=class{constructor(n,t){this.result=n,this.options=t,this.meshResolution=t.meshResolution??"lossy"}optimize(){if(this.result.voxelSource||this.result.animation)return this.result;let n=Tp(this.result,this.options,this.meshResolution),t=Ip(n),o=t?"lossless":this.meshResolution,r=this.canSimplify(n.polygons,o),s=r&&n.polygons.length>=yp&&n.polygons.length<=xp,i=xa(n.polygons,{meshResolution:o,useCandidateFirst:s,staticSimplification:r?{simplifyTriangleMeshOptions:this.options.simplifyTriangleMeshOptions,earlyStopDropRatio:_p(this.options,s)}:!1,skipInteriorCull:t});return i===n.polygons?n:{...n,polygons:i}}canSimplify(n,t){return t==="lossy"&&this.options.simplifyTriangleMeshes!==!1&&vp(n)}};function Op(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0;for(let[_,O,V]of n)t+=_,o+=O,r+=V;let s=1/n.length;t*=s,o*=s,r*=s;let i=n[0],c=n[1],a=n[2],l=c[0]-i[0],u=c[1]-i[1],f=c[2]-i[2],d=a[0]-i[0],m=a[1]-i[1],p=a[2]-i[2],h=u*p-f*m,g=f*d-l*p,b=l*m-u*d,y=Math.hypot(h,g,b);if(y<1e-9)return null;h/=y,g/=y,b/=y;let x=n.length-2,P=new Float64Array(x*9),S=0;for(let _=1;_<n.length-1;_++){let O=n[0],V=n[_],w=n[_+1];P[S++]=O[0],P[S++]=O[1],P[S++]=O[2],P[S++]=V[0],P[S++]=V[1],P[S++]=V[2],P[S++]=w[0],P[S++]=w[1],P[S++]=w[2]}let C=0,T=1/0,A=1/0,M=1/0,E=-1/0,v=-1/0,I=-1/0;for(let[_,O,V]of n){let w=_-t,F=O-o,D=V-r,H=w*w+F*F+D*D;H>C&&(C=H),_<T&&(T=_),_>E&&(E=_),O<A&&(A=O),O>v&&(v=O),V<M&&(M=V),V>I&&(I=V)}return{centroid:[t,o,r],normal:[h,g,b],triFlat:P,bcx:t,bcy:o,bcz:r,br2:C,minX:T,minY:A,minZ:M,maxX:E,maxY:v,maxZ:I}}function Vp(e,n,t,o,r,s,i,c){let a=i[c],l=i[c+1],u=i[c+2],f=i[c+3]-a,d=i[c+4]-l,m=i[c+5]-u,p=i[c+6]-a,h=i[c+7]-l,g=i[c+8]-u,b=r*g-s*h,y=s*p-o*g,x=o*h-r*p,P=f*b+d*y+m*x;if(P>-1e-9&&P<1e-9)return!1;let S=1/P,C=e-a,T=n-l,A=t-u,M=S*(C*b+T*y+A*x);if(M<0||M>1)return!1;let E=T*m-A*d,v=A*f-C*m,I=C*d-T*f,_=S*(o*E+r*v+s*I);return _<0||M+_>1?!1:S*(p*E+h*v+g*I)>.001}function Rp(e,n,t,o,r,s,i){let c=i.bcx-e,a=i.bcy-n,l=i.bcz-t,u=c*o+a*r+l*s,f=c-u*o,d=a-u*r,m=l-u*s;if(f*f+d*d+m*m>i.br2)return!1;let p=i.triFlat,h=p.length;for(let g=0;g<h;g+=9)if(Vp(e,n,t,o,r,s,p,g))return!0;return!1}var wt=9,Lp=6,Ue=12;function zr(e,n,t,o,r,s){let i=o-e,c=r-n,a=s-t;return i*c+c*a+a*i}function wp(e){let n=[];for(let C=0;C<e.length;C++)e[C]&&n.push(C);let t=n.length,o=new Int32Array(t);for(let C=0;C<t;C++)o[C]=n[C];let r=new Float64Array(t),s=new Float64Array(t),i=new Float64Array(t);for(let C=0;C<t;C++){let T=e[o[C]];r[C]=(T.minX+T.maxX)*.5,s[C]=(T.minY+T.maxY)*.5,i[C]=(T.minZ+T.maxZ)*.5}let c=2*Math.max(1,t)+1,a=new Float64Array(c*wt),l=0,u=new Float64Array(Ue),f=new Float64Array(Ue),d=new Float64Array(Ue),m=new Float64Array(Ue),p=new Float64Array(Ue),h=new Float64Array(Ue),g=new Int32Array(Ue),b=new Float64Array(Ue-1),y=new Int32Array(Ue-1),x=new Float64Array(Ue-1),P=new Int32Array(Ue-1);function S(C,T){let A=l++,M=A*wt,E=T-C,v=1/0,I=1/0,_=1/0,O=-1/0,V=-1/0,w=-1/0;for(let G=C;G<T;G++){let te=e[o[G]];te.minX<v&&(v=te.minX),te.maxX>O&&(O=te.maxX),te.minY<I&&(I=te.minY),te.maxY>V&&(V=te.maxY),te.minZ<_&&(_=te.minZ),te.maxZ>w&&(w=te.maxZ)}if(a[M]=v,a[M+1]=I,a[M+2]=_,a[M+3]=O,a[M+4]=V,a[M+5]=w,E<=Lp)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let F=1/0,D=1/0,H=1/0,K=-1/0,Q=-1/0,ne=-1/0;for(let G=C;G<T;G++)r[G]<F&&(F=r[G]),r[G]>K&&(K=r[G]),s[G]<D&&(D=s[G]),s[G]>Q&&(Q=s[G]),i[G]<H&&(H=i[G]),i[G]>ne&&(ne=i[G]);let ee=K-F,ue=Q-D,U=ne-H;if(ee===0&&ue===0&&U===0)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let $=zr(v,I,_,O,V,w),k=$>0?1/$:0,L=E+1,W=0,Y=0;for(let G=0;G<3;G++){let te=G===0?F:G===1?D:H,ce=G===0?ee:G===1?ue:U;if(ce===0)continue;let be=G===0?r:G===1?s:i,he=Ue/ce;u.fill(1/0),f.fill(1/0),d.fill(1/0),m.fill(-1/0),p.fill(-1/0),h.fill(-1/0),g.fill(0);for(let N=C;N<T;N++){let X=(be[N]-te)*he|0;X>=Ue&&(X=Ue-1);let oe=e[o[N]];oe.minX<u[X]&&(u[X]=oe.minX),oe.maxX>m[X]&&(m[X]=oe.maxX),oe.minY<f[X]&&(f[X]=oe.minY),oe.maxY>p[X]&&(p[X]=oe.maxY),oe.minZ<d[X]&&(d[X]=oe.minZ),oe.maxZ>h[X]&&(h[X]=oe.maxZ),g[X]++}let ye=1/0,ge=1/0,Se=1/0,De=-1/0,R=-1/0,z=-1/0,j=0;for(let N=0;N<Ue-1;N++)u[N]<ye&&(ye=u[N]),m[N]>De&&(De=m[N]),f[N]<ge&&(ge=f[N]),p[N]>R&&(R=p[N]),d[N]<Se&&(Se=d[N]),h[N]>z&&(z=h[N]),j+=g[N],b[N]=zr(ye,ge,Se,De,R,z),y[N]=j;let J=1/0,le=1/0,re=1/0,pe=-1/0,xe=-1/0,ie=-1/0,ae=0;for(let N=Ue-2;N>=0;N--){let X=N+1;u[X]<J&&(J=u[X]),m[X]>pe&&(pe=m[X]),f[X]<le&&(le=f[X]),p[X]>xe&&(xe=p[X]),d[X]<re&&(re=d[X]),h[X]>ie&&(ie=h[X]),ae+=g[X],x[N]=zr(J,le,re,pe,xe,ie),P[N]=ae}for(let N=0;N<Ue-1;N++){if(y[N]===0||P[N]===0)continue;let X=.125+(b[N]*y[N]+x[N]*P[N])*k;X<L&&(L=X,W=G,Y=te+(N+1)/he)}}let se=W===0?r:W===1?s:i,Z=C,B=T-1;for(;Z<=B;)if(se[Z]<Y)Z++;else{let G=o[Z];o[Z]=o[B],o[B]=G;let te=r[Z];r[Z]=r[B],r[B]=te;let ce=s[Z];s[Z]=s[B],s[B]=ce;let be=i[Z];i[Z]=i[B],i[B]=be,B--}let q=Z;(q===C||q===T)&&(q=C+T>>1),a[M+6]=0;let me=S(C,q),fe=S(q,T);return a[A*wt+7]=me,a[A*wt+8]=fe,A}return t>0&&S(0,t),{data:a,nodeCount:l,polyIndices:o,meta:e}}function Np(e,n,t,o,r,s,i,c,a){if(c.nodeCount===0)return!1;let{data:l,polyIndices:u,meta:f}=c,d=o!==0?1/o:o>=0?1/0:-1/0,m=r!==0?1/r:r>=0?1/0:-1/0,p=s!==0?1/s:s>=0?1/0:-1/0,h=0;for(a[h++]=0;h>0;){let b=a[--h]*wt,y=(l[b]-e)*d,x=(l[b+3]-e)*d,P=y<x?y:x,S=y<x?x:y,C=(l[b+1]-n)*m,T=(l[b+4]-n)*m,A=C<T?C:T,M=C<T?T:C;if(P>M||A>S)continue;A>P&&(P=A),M<S&&(S=M);let E=(l[b+2]-t)*p,v=(l[b+5]-t)*p,I=E<v?E:v,_=E<v?v:E;if(!(P>_||I>S)&&(_<S&&(S=_),!(S<.001)))if(l[b+6]===1){let O=l[b+7]|0,V=l[b+8]|0;for(let w=O;w<V;w++){let F=u[w];if(F===i)continue;let D=f[F];if(D&&Rp(e,n,t,o,r,s,D))return!0}}else a[h++]=l[b+7]|0,a[h++]=l[b+8]|0}return!1}function Dp(e,n,t){let o=new Set;if(e.length<2)return o;let r=Math.hypot(n[0],n[1],n[2]);if(r<1e-9)return o;let s=n[0]/r,i=n[1]/r,c=n[2]/r,a=e.map(Op);if(t&&t.size>0)for(let f of t)a[f]=null;let l=wp(a),u=new Int32Array(Math.max(64,l.nodeCount));for(let f=0;f<e.length;f++){let d=a[f];if(!d)continue;let m=d.centroid[0]+d.normal[0]*.001,p=d.centroid[1]+d.normal[1]*.001,h=d.centroid[2]+d.normal[2]*.001;d.normal[0]*s+d.normal[1]*i+d.normal[2]*c<=0||Np(m,p,h,s,i,c,f,l,u)&&o.add(f)}return o}var Po=1e-5,ka=.001,Ua=6;function $r(e){let n=e.vertices;if(n.length<3)return null;let t=n[0],o=0,r=0,s=0;for(let c=1;c+1<n.length;c++){let a=n[c],l=n[c+1],u=a[1]-t[1],f=a[0]-t[0],d=a[2]-t[2],m=l[1]-t[1],p=l[0]-t[0],h=l[2]-t[2];o-=f*h-d*p,r-=d*m-u*h,s-=u*p-f*m}let i=Math.hypot(o,r,s);return i<1e-9?null:[o/i,r/i,s/i]}function Ga(e,n){let t=n.meshRotation,o=t?On(e,t):e;return qo(o,n.rotX,0,n.rotY)[2]}function Nt(e,n,t=Po){return Ga(e,n)>t}function Fp(e,n,t=Po){let o=$r(e);return o===null||Nt(o,n,t)}function za(e){return`${e[0].toFixed(4)},${e[1].toFixed(4)},${e[2].toFixed(4)}`}function $a(e){let n=new Map;for(let t of e){if(!t)continue;let o=za(t);n.has(o)||n.set(o,t)}return Array.from(n,([t,o])=>({key:t,normal:o}))}function Bp(e){return $a(e.map($r))}function Xa(e,n=ka){let t=Math.abs(e[0]),o=Math.abs(e[1]),r=Math.abs(e[2]),s=Math.max(t,o,r);return s>1-n&&t+o+r-s<n}function kp(e){return e.length<=Ua&&e.every(({normal:n})=>Xa(n))}function Up(e,n,t=Po){let o=[];for(let{key:r,normal:s}of e)Nt(s,n,t)&&o.push(r);return o.sort(),o.join("|")}function Xr(e,n,t,o,r){let s=(p,h,g)=>{let b=[0,0,0];return b[e]=p,b[(e+1)%3]=h,b[(e+2)%3]=g,b},i=s(n,-o,-o),c=s(n,o,-o),a=s(n,o,o),l=s(n,-o,o),u=s(t,-o,-o),f=s(t,o,-o),d=s(t,o,o),m=s(t,-o,o);return[{vertices:[i,c,a,l],color:r},{vertices:[u,f,d,m],color:r},{vertices:[i,c,f,u],color:r},{vertices:[c,a,d,f],color:r},{vertices:[a,l,m,d],color:r},{vertices:[l,i,u,m],color:r}]}function Ya(e={}){let n=e.size??5,t=e.thickness??.025,o=e.negative??!1,r=e.xColor??"#ff3a3a",s=e.yColor??"#3aff3a",i=e.zColor??"#3a8aff",c=n*t/2,a=o?-n:0;return[...Xr(0,a,n,c,r),...Xr(1,a,n,c,s),...Xr(2,a,n,c,i)]}var Gp=["right","left","front","back","top","bottom"];function Yr(e){return[e[0],e[1],e[2]]}function Wa(e,n){return{min:[Math.min(e[0],n[0]),Math.min(e[1],n[1]),Math.min(e[2],n[2])],max:[Math.max(e[0],n[0]),Math.max(e[1],n[1]),Math.max(e[2],n[2])]}}function zp(e){if(e.min&&e.max)return Wa(e.min,e.max);let n=e.center?Yr(e.center):[0,0,0],t;typeof e.size=="number"?t=[e.size,e.size,e.size]:e.size?t=Yr(e.size):t=[1,1,1];let o=[t[0]/2,t[1]/2,t[2]/2];return Wa([n[0]-o[0],n[1]-o[1],n[2]-o[2]],[n[0]+o[0],n[1]+o[1],n[2]+o[2]])}function $p(e,n){let[t,o,r]=e,[s,i,c]=n;return[{name:"right",vertices:[[s,o,r],[s,i,r],[s,i,c],[s,o,c]]},{name:"left",vertices:[[t,i,r],[t,o,r],[t,o,c],[t,i,c]]},{name:"front",vertices:[[s,i,r],[t,i,r],[t,i,c],[s,i,c]]},{name:"back",vertices:[[t,o,r],[s,o,r],[s,o,c],[t,o,c]]},{name:"top",vertices:[[t,o,c],[s,o,c],[s,i,c],[t,i,c]]},{name:"bottom",vertices:[[t,i,r],[s,i,r],[s,o,r],[t,o,r]]}]}function Xp(e,n){let t={...e,...n};return(e.data!==void 0||n?.data!==void 0)&&(t.data={...e.data??{},...n?.data??{}}),t}function Yp(e,n){let t={vertices:e.vertices.map(Yr)};return n.color!==void 0&&(t.color=n.color),n.texture!==void 0&&(t.texture=n.texture),n.material!==void 0&&(t.material=n.material),n.uvs!==void 0&&(t.uvs=n.uvs.map(o=>[o[0],o[1]])),n.data!==void 0&&(t.data={...n.data}),t}function ja(e={}){let{min:n,max:t}=zp(e);if(n[0]===t[0]||n[1]===t[1]||n[2]===t[2])return[];let o={color:e.color??"#ffffff",texture:e.texture,material:e.material,uvs:e.uvs,data:e.data},r=$p(n,t),s=new Map(r.map(c=>[c.name,c])),i=[];for(let c of Gp){let a=e.faces?.[c];if(a===!1)continue;let l=s.get(c);l&&i.push(Yp(l,Xp(o,a)))}return i}function Ka(e,n,t,o){let r=[0,0,0];return r[e]=n,r[(e+1)%3]=t,r[(e+2)%3]=o,r}function Wp(e,n,t,o,r){let s=(p,h,g)=>Ka(e,p,h,g),i=s(n,-o,-o),c=s(n,o,-o),a=s(n,o,o),l=s(n,-o,o),u=s(t,-o,-o),f=s(t,o,-o),d=s(t,o,o),m=s(t,-o,o);return[{vertices:[i,c,a,l],color:r},{vertices:[u,f,d,m],color:r},{vertices:[i,c,f,u],color:r},{vertices:[c,a,d,f],color:r},{vertices:[a,l,m,d],color:r},{vertices:[l,i,u,m],color:r}]}function jp(e,n,t,o,r){let s=(f,d,m)=>Ka(e,f,d,m),i=s(n,-o,-o),c=s(n,o,-o),a=s(n,o,o),l=s(n,-o,o),u=s(t,0,0);return[{vertices:[i,c,a,l],color:r},{vertices:[i,c,u],color:r},{vertices:[c,a,u],color:r},{vertices:[a,l,u],color:r},{vertices:[l,i,u],color:r}]}function Kp(e){return e.map(n=>({...n,vertices:[...n.vertices].reverse()}))}function Ha(e){let n=e.axis,t=e.sign??1,o=e.shaftLength??4,r=e.shaftHalfThickness??.05,s=e.headLength??.8,i=e.headHalfThickness??.2,c=e.color??"#ffffff",a=e.shaft??!0,l=o*t,u=Math.min(0,l),f=Math.max(0,l),d=l,m=(o+s)*t,p=jp(n,d,m,i,c),h=t===-1?Kp(p):p;return a?[...Wp(n,u,f,r,c),...h]:h}function Mo(e,n,t){let o=[0,0,0],r=(e+1)%3,s=(e+2)%3;return o[r]=Math.cos(t)*n,o[s]=Math.sin(t)*n,o}function Za(e){let n=e.axis,t=e.radius,o=e.halfThickness??Math.max(.05,t*.04),r=e.segments??32,s=e.color??"#ffffff",i=t-o,c=t+o,a=[];for(let l=0;l<r;l++){let u=l/r*Math.PI*2,f=(l+1)/r*Math.PI*2,d=Mo(n,i,u),m=Mo(n,i,f),p=Mo(n,c,u),h=Mo(n,c,f);a.push({vertices:[d,p,h,m],color:s})}return a}function qa(e){let n=e.axis,t=e.outerRadius,o=e.color??"#ffffff",r=(n+1)%3,s=(n+2)%3,i=(c,a)=>{let l=[0,0,0];return l[n]=0,l[r]=c,l[s]=a,l};return[{vertices:[i(-t,-t),i(t,-t),i(t,t),i(-t,t)],color:o}]}function Qa(e){let n=e.axis,t=e.size??.4,o=e.offset??t*2,r=typeof o=="number"?o:o[0],s=typeof o=="number"?o:o[1],i=e.along??0,c=e.color??"#ffffff",a=(n+1)%3,l=(n+2)%3,u=(f,d)=>{let m=[0,0,0];return m[n]=i,m[a]=r+f,m[l]=s+d,m};return[{vertices:[u(-t,-t),u(t,-t),u(t,t),u(-t,t)],color:c}]}function Ja(e){let{center:n,size:t,color:o="#ffffff"}=e,[r,s,i]=n,c=[[r+t,s,i],[r-t,s,i],[r,s+t,i],[r,s-t,i],[r,s,i+t],[r,s,i-t]];return[[0,2,4],[2,1,4],[1,3,4],[3,0,4],[2,0,5],[1,2,5],[3,1,5],[0,3,5]].map(l=>({vertices:[c[l[0]],c[l[1]],c[l[2]]],color:o}))}function ec(e={}){let{radius:n=50,color:t="#cccccc"}=e,o=Math.min(Math.max(0,Math.floor(e.subdivisions??1)),3),r=(1+Math.sqrt(5))/2,s=[[-1,r,0],[1,r,0],[-1,-r,0],[1,-r,0],[0,-1,r],[0,1,r],[0,-1,-r],[0,1,-r],[r,0,-1],[r,0,1],[-r,0,-1],[-r,0,1]].map(([a,l,u])=>{let f=Math.sqrt(a*a+l*l+u*u);return[a/f,l/f,u/f]}),i=[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]];for(let a=0;a<o;a++){let l=[...s],u=[],f=new Map,d=(m,p)=>{let h=m<p?`${m}-${p}`:`${p}-${m}`,g=f.get(h);if(g!==void 0)return g;let b=s[m],y=s[p],x=(b[0]+y[0])/2,P=(b[1]+y[1])/2,S=(b[2]+y[2])/2,C=Math.sqrt(x*x+P*P+S*S);x/=C,P/=C,S/=C;let T=l.length;return l.push([x,P,S]),f.set(h,T),T};for(let[m,p,h]of i){let g=d(m,p),b=d(p,h),y=d(h,m);u.push([m,g,y]),u.push([p,b,g]),u.push([h,y,b]),u.push([g,b,y])}s=l,i=u}let c=s.map(([a,l,u])=>[a*n,l*n,u*n]);return i.map(([a,l,u])=>({vertices:[c[a],c[l],c[u]],color:t}))}function nc(e={}){let{size:n=100,color:t="#cccccc"}=e,o=n/Math.sqrt(3),r=[[o,o,o],[o,-o,-o],[-o,o,-o],[-o,-o,o]];return[[0,1,2],[0,3,1],[0,2,3],[1,3,2]].map(([i,c,a])=>({vertices:[r[i],r[c],r[a]],color:t}))}function tc(e={}){let{size:n=100,color:t="#cccccc"}=e,o=(1+Math.sqrt(5))/2,r=n/Math.sqrt(1+o*o),s=[[-1,o,0],[1,o,0],[-1,-o,0],[1,-o,0],[0,-1,o],[0,1,o],[0,-1,-o],[0,1,-o],[o,0,-1],[o,0,1],[-o,0,-1],[-o,0,1]].map(([c,a,l])=>[c*r,a*r,l*r]);return[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]].map(([c,a,l])=>({vertices:[s[c],s[a],s[l]],color:t}))}function oc(e={}){let{size:n=100,color:t="#cccccc"}=e,o=(1+Math.sqrt(5))/2,r=1/o,s=n/Math.sqrt(3),c=[[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1],[0,r,o],[0,-r,o],[0,r,-o],[0,-r,-o],[r,o,0],[-r,o,0],[r,-o,0],[-r,-o,0],[o,0,r],[o,0,-r],[-o,0,r],[-o,0,-r]].map(([l,u,f])=>[l*s,u*s,f*s]);return[[12,13,4,8,0],[0,8,9,2,16],[16,17,1,12,0],[1,10,5,13,12],[17,3,11,10,1],[2,9,6,15,14],[2,14,3,17,16],[14,15,7,11,3],[18,6,9,8,4],[4,13,5,19,18],[5,10,11,7,19],[18,19,7,15,6]].map(l=>({vertices:l.map(u=>c[u]),color:t}))}function Ao(e={}){let{radius:n=50,height:t=100,radialSegments:o=12,color:r="#cccccc"}=e,s=e.radiusTop??n,i=t/2,c=-i,a=i,l=Math.max(3,o),u=[],f=Array.from({length:l+1},(p,h)=>h/l*Math.PI*2),d=n<=1e-6,m=s<=1e-6;for(let p=0;p<l&&!(d&&m);p++){let h=f[p],g=f[p+1],b=Math.cos(h)*n,y=Math.sin(h)*n,x=Math.cos(g)*n,P=Math.sin(g)*n,S=Math.cos(h)*s,C=Math.sin(h)*s,T=Math.cos(g)*s,A=Math.sin(g)*s,M=[b,y,c],E=[x,P,c],v=[T,A,a],I=[S,C,a];if(m){let _=[0,0,a];u.push({vertices:[M,E,_],color:r})}else if(d){let _=[0,0,c];u.push({vertices:[_,v,I],color:r})}else u.push({vertices:[M,E,v,I],color:r})}if(n>1e-6){let p=[0,0,c];for(let h=0;h<l;h++){let g=f[h],b=f[h+1],y=[Math.cos(g)*n,Math.sin(g)*n,c],x=[Math.cos(b)*n,Math.sin(b)*n,c];u.push({vertices:[[...p],[...x],[...y]],color:r})}}if(s>1e-6){let p=[0,0,a];for(let h=0;h<l;h++){let g=f[h],b=f[h+1],y=[Math.cos(g)*s,Math.sin(g)*s,a],x=[Math.cos(b)*s,Math.sin(b)*s,a];u.push({vertices:[[...p],[...y],[...x]],color:r})}}return u}function rc(e={}){return Ao({...e,radiusTop:0})}function So(e,n,t,o){let r=Math.sin(e),s=Math.cos(e),i=Math.sin(n),c=Math.cos(n);return[(t+o*c)*s,(t+o*c)*r,o*i]}function sc(e={}){let{radius:n=50,tube:t=15,radialSegments:o=12,tubularSegments:r=16,color:s="#cccccc"}=e,i=Math.max(3,o),c=Math.max(3,r),a=[];for(let l=0;l<i;l++){let u=l/i*Math.PI*2,f=(l+1)/i*Math.PI*2;for(let d=0;d<c;d++){let m=d/c*Math.PI*2,p=(d+1)/c*Math.PI*2,h=So(u,m,n,t),g=So(f,m,n,t),b=So(f,p,n,t),y=So(u,p,n,t);a.push({vertices:[h,g,b,y],color:s})}}return a}var Hp=.01,Zp=.01;function qp(e,n){let t=Math.hypot(e[0],e[1],e[2])||1,o=e[0]/t,r=e[1]/t,s=e[2]/t,i=Math.sign(s||1)*Math.max(Math.abs(s),.01),c=n,a=.01;return[1,0,0,0,0,1,0,0,-o/i,-r/i,a,0,c*o/i,c*r/i,c*(1-a),1]}function Qp(e,n){let t=Math.hypot(n[0],n[1],n[2])||1,o=n[0]/t,r=n[1]/t,s=n[2]/t;return e[0]*o+e[1]*r+e[2]*s>0}function Wr(e){if(e.length<=1)return e.map(i=>[i[0],i[1]]);let t=e.map(i=>[i[0],i[1]]);t.sort((i,c)=>i[0]-c[0]||i[1]-c[1]);let o=(i,c,a)=>(c[0]-i[0])*(a[1]-i[1])-(c[1]-i[1])*(a[0]-i[0]),r=[];for(let i of t){for(;r.length>=2&&o(r[r.length-2],r[r.length-1],i)<=0;)r.pop();r.push(i)}let s=[];for(let i=t.length-1;i>=0;i--){let c=t[i];for(;s.length>=2&&o(s[s.length-2],s[s.length-1],c)<=0;)s.pop();s.push(c)}return r.pop(),s.pop(),r.concat(s)}function ic(e){let n=0,t=e.length;for(let o=0;o<t;o++){let r=e[o],s=e[(o+1)%t];n+=r[0]*s[1]-s[0]*r[1]}return n/2}function gn(e){let n=e.map(t=>[t[0],t[1]]);return ic(n)<0&&n.reverse(),n}function Jp(e,n,t){let o=Math.hypot(n[0],n[1],n[2])||1,r=n[0]/o,s=n[1]/o,i=n[2]/o,c=Math.sign(i||1)*Math.max(Math.abs(i),.01),a=e[2]-t;return[e[0]-r/c*a,e[1]-s/c*a]}function An(e,n){if(e.length===0||n.length<3)return[];let t=e.map(r=>[r[0],r[1]]),o=n.length;for(let r=0;r<o;r++){if(t.length===0)return[];let s=t;t=[];let i=n[r],c=n[(r+1)%o],a=f=>(c[0]-i[0])*(f[1]-i[1])-(c[1]-i[1])*(f[0]-i[0])>=0,l=(f,d)=>{let m=i[0],p=i[1],h=c[0],g=c[1],b=f[0],y=f[1],x=d[0],P=d[1],S=(m-h)*(y-P)-(p-g)*(b-x);if(Math.abs(S)<1e-12)return[f[0],f[1]];let C=((m-b)*(y-P)-(p-y)*(b-x))/S;return[m+C*(h-m),p+C*(g-p)]},u=s.length;for(let f=0;f<u;f++){let d=s[f],m=s[(f+u-1)%u],p=a(d),h=a(m);p?(h||t.push(l(m,d)),t.push([d[0],d[1]])):h&&t.push(l(m,d))}}return t}function tt(e){return[e[1]*50,e[0]*50,e[2]*50]}function ac(e){return[e[1],e[0],e[2]]}function eg(e){return e?.direction?{...e,direction:ac(e.direction)}:e}function jr(e){return e==null?[1,1,1]:typeof e=="number"?[e,e,e]:[e[0]??1,e[1]??1,e[2]??1]}function Dt(e){let n=jr(e),t=n[0]===1&&n[1]===1&&n[2]===1;return(o,r)=>{let s=tt(r),i=o[1]*50,c=o[0]*50,a=o[2]*50,l=t?i:i*n[0],u=t?c:c*n[1],f=t?a:a*n[2];return[l+s[0],u+s[1],f+s[2]]}}function cc(e,n){if(e.length<3||n===0)return e;let t=[],o=e.length;for(let r=0;r<o;r++){let s=e[(r-1+o)%o],i=e[r],c=e[(r+1)%o],a=i[0]-s[0],l=i[1]-s[1],u=c[0]-i[0],f=c[1]-i[1],d=Math.hypot(a,l),m=Math.hypot(u,f);if(d<1e-9||m<1e-9){t.push(i);continue}let p=l/d,h=-a/d,g=f/m,b=-u/m,y=p+g,x=h+b,P=Math.hypot(y,x);if(P<1e-9){t.push(i);continue}let S=y/P,C=x/P,T=S*p+C*h;if(Math.abs(T)<1e-9){t.push(i);continue}let A=n/T;t.push([i[0]+S*A,i[1]+C*A])}return t}var lc=.5,uc=.001,fc=.5;function Kr(e,n,t,o){let r=[];for(let u=0;u<e.length;u++){if(o.has(u))continue;let f=e[u];if(f.vertices.length<3)continue;let d=f.vertices.map(A=>t(A,n)),m=d[0],p=0,h=0,g=0;for(let A=1;A+1<d.length;A++){let M=d[A],E=d[A+1],v=M[0]-m[0],I=M[1]-m[1],_=M[2]-m[2],O=E[0]-m[0],V=E[1]-m[1],w=E[2]-m[2];p+=V*_-w*I,h+=w*v-O*_,g+=O*I-V*v}let b=Math.hypot(p,h,g);if(b<1e-9)continue;let y=[p/b,h/b,g/b],x=null;for(let A=1;A<d.length;A++){let M=d[A],E=M[0]-m[0],v=M[1]-m[1],I=M[2]-m[2],_=Math.hypot(E,v,I);if(_>1e-9){x=[E/_,v/_,I/_];break}}if(!x)continue;let P=x[0]*y[0]+x[1]*y[1]+x[2]*y[2];x=[x[0]-P*y[0],x[1]-P*y[1],x[2]-P*y[2]];let S=Math.hypot(x[0],x[1],x[2]);if(S<1e-9)continue;x=[x[0]/S,x[1]/S,x[2]/S];let C=[y[1]*x[2]-y[2]*x[1],y[2]*x[0]-y[0]*x[2],y[0]*x[1]-y[1]*x[0]],T=y[0]*m[0]+y[1]*m[1]+y[2]*m[2];r.push({face:f,O:m,n:y,u:x,v:C,offset:T,polyIndex:u})}let s=[];for(let u of r){let f=!1;for(let d of s){let m=d.rep;if(!(1-(u.n[0]*m.n[0]+u.n[1]*m.n[1]+u.n[2]*m.n[2])>uc)&&!(Math.abs(u.offset-m.offset)>fc)){d.faces.push(u),f=!0;break}}f||s.push({rep:u,faces:[u]})}let i=[],c=3,a=u=>`${u[0].toFixed(c)},${u[1].toFixed(c)},${u[2].toFixed(c)}`;for(let u of s){if(u.faces.length===1){l(u.faces);continue}let f=u.faces.map(g=>{let b=new Set;for(let y of g.face.vertices)b.add(a(t(y,n)));return b}),d=u.faces.map((g,b)=>b),m=g=>{for(;d[g]!==g;)d[g]=d[d[g]],g=d[g];return g},p=(g,b)=>{let y=m(g),x=m(b);y!==x&&(d[y]=x)};for(let g=0;g<u.faces.length;g++)for(let b=g+1;b<u.faces.length;b++){let y=f[g],x=f[b],P=0;for(let S of y)if(x.has(S)&&(P++,P>=2))break;P>=2&&p(g,b)}let h=new Map;for(let g=0;g<u.faces.length;g++){let b=m(g),y=h.get(b);y||(y=[],h.set(b,y)),y.push(u.faces[g])}for(let g of h.values())l(g)}function l(u){let f=u[0],{O:d,n:m,u:p,v:h}=f,g=[],b=[],y=[];for(let S of u){let C=[];for(let T of S.face.vertices){let A=t(T,n),M=A[0]-d[0],E=A[1]-d[1],v=A[2]-d[2],I=[M*p[0]+E*p[1]+v*p[2],M*h[0]+E*h[1]+v*h[2]];g.push(I),C.push(I)}C.length>=3&&(b.push(gn(C)),y.push(S.polyIndex))}if(g.length<3)return;let x=Wr(g);if(x.length<3)return;let P=cc(gn(x),lc);i.push({O:d,n:m,u:p,v:h,outlineUv:P,memberPolysUv:b,memberPolyIndices:y})}return i}var hn=50,ot=[.4,-.7,.59],rt="#ffffff",st=1,it="#ffffff",at=.4,Sn=4096,Co=1,Hr=.1,Zr=1,Eo=Sn*Sn,qr=Eo*3,Ft=2048,Qr=4*1024*1024,Jr=16*1024*1024,es=.995,ns=512,ng=12,Ae=.001,de=1e-9,ts=1e-4,os=.1,To=.01,tg=.75,og=1,rg=250,sg=1.5,rs=.75,bn=3,ss=2,vo=4,is=[1,10,100,1e3,1e4,1e5,1e6],ct=64,as=32,cs=96,He=64,ls=128,Io=50,_o=1e-7,yn=16,us=.9,Ze=.75,Oo=.2,fs=.05,ms=256,ds=.6,ig=1.5;function ps(e){return e===void 0||e==="auto"||!Number.isFinite(e)?1:Math.max(0,Math.min(1,e))}var Bt=new Map;function ut(e){if(Bt.has(e)){let t=Bt.get(e);return t===void 0?null:t}let n=Pn(e);return Bt.size>=ns&&Bt.clear(),Bt.set(e,n),n}function xn(e){let n=ut(e);return n?{r:n.rgb[0],g:n.rgb[1],b:n.rgb[2]}:{r:255,g:255,b:255}}function gs({r:e,g:n,b:t}){return`${e},${n},${t}`}function Vo(e){return ut(e)?.alpha??1}function Ro({r:e,g:n,b:t}){let o=r=>Math.round(Math.max(0,Math.min(255,r))).toString(16).padStart(2,"0");return`#${o(e)}${o(n)}${o(t)}`}function Lo(e,n,t,o){let r=xn(n),s=xn(t),i=Ge(r.r/255),c=Ge(r.g/255),a=Ge(r.b/255),l=Ge(s.r/255),u=Ge(s.g/255),f=Ge(s.b/255);return{r:(i*e+l*o)*lt,g:(c*e+u*o)*lt,b:(a*e+f*o)*lt}}function ag({r:e,g:n,b:t}){let o=r=>Math.round(Math.max(0,Math.min(1,r))*255);return`rgb(${o(e)} ${o(n)} ${o(t)})`}function Ge(e){return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)}function cg(e){return e<=.0031308?e*12.92:1.055*Math.pow(e,1/2.4)-.055}var lt=1/Math.PI;function Bn(e,n,t,o,r){let s=xn(e),i=xn(t),c=xn(o),a=Ge(s.r/255),l=Ge(s.g/255),u=Ge(s.b/255),f=Ge(i.r/255),d=Ge(i.g/255),m=Ge(i.b/255),p=Ge(c.r/255),h=Ge(c.g/255),g=Ge(c.b/255),b=a*(f*n+p*r)*lt,y=l*(d*n+h*r)*lt,x=u*(m*n+g*r)*lt,P=M=>Math.max(0,Math.min(255,Math.round(cg(Math.max(0,Math.min(1,M)))*255))),S=P(b),C=P(y),T=P(x),A=Vo(e);return A<1?`rgba(${S}, ${C}, ${T}, ${A})`:Ro({r:S,g:C,b:T})}function hs(e,n){if(!Number.isFinite(n)||n<=1)return e;let t=ut(e);if(!t)return e;let o=255/Math.max(1,Math.round(n)-1),r=i=>Math.max(0,Math.min(255,Math.round(Math.round(i/o)*o))),s={r:r(t.rgb[0]),g:r(t.rgb[1]),b:r(t.rgb[2])};return t.alpha<1?`rgba(${s.r}, ${s.g}, ${s.b}, ${t.alpha})`:Ro(s)}function lg(e,n){return!!e&&!!n&&e.r===n.r&&e.g===n.g&&e.b===n.b}function ug(e,n,t){let o=(r,s)=>{let i=s-r;return Math.abs(i)<=t?s:r+Math.sign(i)*t};return{r:o(e.r,n.r),g:o(e.g,n.g),b:o(e.b,n.b)}}function fg(e,n=1){return n<1?`rgba(${e.r}, ${e.g}, ${e.b}, ${n})`:Ro(e)}function mg(e,n){if(e===void 0)return Number.POSITIVE_INFINITY;if(e===n)return 0;let t=ut(e),o=ut(n);if(!t||!o)return 1;let r=t.rgb[0]-o.rgb[0],s=t.rgb[1]-o.rgb[1],i=t.rgb[2]-o.rgb[2],c=(t.alpha-o.alpha)*255;return Math.sqrt(r*r+s*s+i*i+c*c)/510}function bs(e){let t=c=>Math.round(c/1e-4).toString(36),o=c=>`${t(c[0])},${t(c[1])},${t(c[2])}`,r=(c,a)=>{let l=o(c),u=o(a);return l<u?`${l}|${u}`:`${u}|${l}`},s=new Map;for(let c=0;c<e.length;c++){let a=e[c];if(!a||a.vertices.length<2)continue;let l=a.vertices;for(let u=0;u<l.length;u++){let f=l[u],d=l[(u+1)%l.length],m=r(f,d),p=s.get(m);p?(p.degree+=1,p.degree===2?p.polyB=c:p.polyB=null):s.set(m,{polyA:c,polyB:null,vertA:f,vertB:d,degree:1})}}let i=new Map;for(let[c,a]of s)i.set(c,{polyA:a.polyA,polyB:a.polyB,vertA:a.vertA,vertB:a.vertB});return i}function ys(e,n){let t=Math.hypot(n[0],n[1],n[2])||1,o=n[0]/t,r=n[1]/t,s=n[2]/t;return e.map(i=>i?i[0]*o+i[1]*r+i[2]*s<-1e-6:!0)}function xs(e,n){let t=[];for(let l of e.values()){let u=n[l.polyA]??!1;if(l.polyB===null)u&&t.push([l.vertA,l.vertB]);else{let f=n[l.polyB]??!1;u!==f&&t.push([l.vertA,l.vertB])}}if(t.length===0)return[];let o=1e-4,r=l=>Math.round(l/o).toString(36),s=l=>`${r(l[0])},${r(l[1])},${r(l[2])}`,i=t.map(([l,u])=>({a:l,b:u,aKey:s(l),bKey:s(u),used:!1})),c=new Map;for(let l of i){let u=c.get(l.aKey);u||(u=[],c.set(l.aKey,u)),u.push(l);let f=c.get(l.bKey);f||(f=[],c.set(l.bKey,f)),f.push(l)}let a=[];for(let l of i){if(l.used)continue;let u=[l.a],f=l,d=l.aKey,m=l.bKey;f.used=!0;for(let p=0;p<i.length+1;p++){u.push(f.bKey===m?f.b:f.a);let h=c.get(m)??[],g=null;for(let b of h)if(!(b===f||b.used)){g=b;break}if(!g||(g.used=!0,d=m,m=g.aKey===d?g.bKey:g.aKey,f=g,m===l.aKey))break}u.length>=3&&a.push(u)}return a}var mc=40;function dg(e){let t=c=>Math.round(c/1e-4).toString(36),o=c=>`${t(c[0])},${t(c[1])},${t(c[2])}`,r=(c,a)=>{let l=o(c),u=o(a);return l<u?`${l}|${u}`:`${u}|${l}`},s=new Map;for(let c=0;c<e.length;c++){let a=e[c];if(!a||a.vertices.length<2)continue;let l=a.vertices;for(let u=0;u<l.length;u++){let f=l[u],d=l[(u+1)%l.length],m=r(f,d),p=s.get(m);p||(p=[],s.set(m,p)),p.push(c)}}let i=new Map;for(let c of s.values())if(!(c.length<2))for(let a of c)for(let l of c){if(a===l)continue;let u=i.get(a);u||(u=new Set,i.set(a,u)),u.add(l)}return i}function pg(e,n,t,o){let r=Dt(t),s=!!o&&(o[0]!==0||o[1]!==0||o[2]!==0),i=s?tt(n):null,c=e.map(a=>{if(!a)return{vertices:[],color:""};let l=a.vertices.map(u=>r(u,n));return s&&i&&o&&(l=l.map(u=>{let f=[u[0]-i[0],u[1]-i[1],u[2]-i[2]],d=On(f,o);return[d[0]+i[0],d[1]+i[1],d[2]+i[2]]})),{vertices:l,color:""}});return bs(c)}function gg(e,n,t,o,r){let s=[],i=Dt(t),c=!!r&&(r[0]!==0||r[1]!==0||r[2]!==0),a=c?tt(n):null;for(let l=0;l<e.length;l++){if(!o(l))continue;let u=e[l];if(!u)continue;let f=u.vertices.map(S=>i(S,n));c&&a&&r&&(f=f.map(S=>{let C=[S[0]-a[0],S[1]-a[1],S[2]-a[2]],T=On(C,r);return[T[0]+a[0],T[1]+a[1],T[2]+a[2]]}));let d=1/0,m=1/0,p=1/0,h=-1/0,g=-1/0,b=-1/0;for(let S of f)S[0]<d&&(d=S[0]),S[0]>h&&(h=S[0]),S[1]<m&&(m=S[1]),S[1]>g&&(g=S[1]),S[2]<p&&(p=S[2]),S[2]>b&&(b=S[2]);let y=[[d,m,p],[h,m,p],[d,g,p],[h,g,p],[d,m,b],[h,m,b],[d,g,b],[h,g,b]],x=null,P=0;if(f.length>=3){let S=f[0],C=f[1],T=f[2],A=[C[0]-S[0],C[1]-S[1],C[2]-S[2]],M=[T[0]-S[0],T[1]-S[1],T[2]-S[2]],E=M[1]*A[2]-M[2]*A[1],v=M[2]*A[0]-M[0]*A[2],I=M[0]*A[1]-M[1]*A[0],_=Math.hypot(E,v,I);_>1e-9&&(x=[E/_,v/_,I/_],P=x[0]*S[0]+x[1]*S[1]+x[2]*S[2])}s.push({wv:f,bboxCorners:y,planeN:x,planeOffset:P,polygonIndex:l})}return s}function hg(e,n,t,o,r,s){let i=Dt(t),c=!!s&&(s[0]!==0||s[1]!==0||s[2]!==0),a=c?tt(n):null,f=Kr(e,n,c&&a&&s?(g,b)=>{let y=i(g,b),x=[y[0]-a[0],y[1]-a[1],y[2]-a[2]],P=On(x,s);return[P[0]+a[0],P[1]+a[1],P[2]+a[2]]}:i,o).map((g,b)=>{let{O:y,n:x,u:P,v:S,outlineUv:C,memberPolysUv:T,memberPolyIndices:A}=g,M=1/0,E=1/0,v=-1/0,I=-1/0;for(let Q of C)Q[0]<M&&(M=Q[0]),Q[1]<E&&(E=Q[1]),Q[0]>v&&(v=Q[0]),Q[1]>I&&(I=Q[1]);let _=v-M,O=I-E,V=r*50,w=y[0]+M*P[0]+E*S[0]+V*x[0],F=y[1]+M*P[1]+E*S[1]+V*x[1],D=y[2]+M*P[2]+E*S[2]+V*x[2],K=`matrix3d(${[P[0],P[1],P[2],0,S[0],S[1],S[2],0,x[0],x[1],x[2],0,w,F,D,1].map(Q=>Q.toFixed(4)).join(",")})`;return{O:y,n:x,u:P,v:S,outlineUv:C,memberPolysUv:T,memberPolyIndices:A,minU:M,minV:E,width:_,height:O,matrixCss:K,faceIndex:b,lift:V}}),d=.015,m=.5,p=20,h=new Set;for(let g=0;g<f.length;g++){if(h.has(g))continue;let b=f[g],y=b.n[0]*b.O[0]+b.n[1]*b.O[1]+b.n[2]*b.O[2],x=b.minU,P=b.minV,S=b.minU+b.width,C=b.minV+b.height;for(let T=0;T<f.length;T++){if(T===g||h.has(T))continue;let A=f[T];if(b.n[0]*A.n[0]+b.n[1]*A.n[1]+b.n[2]*A.n[2]<1-d)continue;let E=b.n[0]*A.O[0]+b.n[1]*A.O[1]+b.n[2]*A.O[2];if(E<=y+m||E-y>p)continue;let v=1/0,I=1/0,_=-1/0,O=-1/0;for(let V of A.outlineUv){let w=A.O[0]+V[0]*A.u[0]+V[1]*A.v[0],F=A.O[1]+V[0]*A.u[1]+V[1]*A.v[1],D=A.O[2]+V[0]*A.u[2]+V[1]*A.v[2],H=w-b.O[0],K=F-b.O[1],Q=D-b.O[2],ne=H*b.u[0]+K*b.u[1]+Q*b.u[2],ee=H*b.v[0]+K*b.v[1]+Q*b.v[2];ne<v&&(v=ne),ne>_&&(_=ne),ee<I&&(I=ee),ee>O&&(O=ee)}if(v<=x&&_>=S&&I<=P&&O>=C){let V=Math.max(0,S-x)*Math.max(0,C-P);if(Math.max(0,_-v)*Math.max(0,O-I)>V*2)continue;h.add(g);break}}}return f=f.filter((g,b)=>!h.has(b)),f}function bg(e){let{receiverPlanes:n,receiverPolygons:t,receiverHasTexture:o,casters:r,lightDir:s,cameraRot:i,ambientLight:c,directionalLight:a,shadow:l}=e,u=Math.hypot(s[0],s[1],s[2])||1,f=s[0]/u,d=s[1]/u,m=s[2]/u,p=c?.color??"#ffffff",h=c?.intensity??.4,g=a?.intensity??1,b=l?.color??"#000000",y=l?.opacity??.25,x=l?.maxExtend??2e3,P=[],S=r.map(A=>{let M=A.edgeOwners;if(!M||A.selfShadowEdgeMap)return null;let E=A.casterPolygonCount??0;if(E<mc||A.items.length<mc)return null;let v=0;for(let O of A.items)O.planeN&&v++;if(v*2<A.items.length)return null;let I=new Array(E).fill(null);for(let O of A.items)O.planeN&&O.polygonIndex<E&&(I[O.polygonIndex]=O.planeN);let _=ys(I,s);return xs(M,_)}),C=(A,M,E,v)=>{if(A.length<3)return[];let I=[],_=V=>(V[0]-M[0])*E[0]+(V[1]-M[1])*E[1]+(V[2]-M[2])*E[2],O=(V,w,F,D)=>{let H=(F-v)/(F-D);return[V[0]+H*(w[0]-V[0]),V[1]+H*(w[1]-V[1]),V[2]+H*(w[2]-V[2])]};for(let V=0;V<A.length;V++){let w=A[V],F=A[(V+A.length-1)%A.length],D=_(w),H=_(F),K=D>v,Q=H>v;K?(Q||I.push(O(F,w,H,D)),I.push(w)):Q&&I.push(O(F,w,H,D))}return I},T={rotX:i.rotX,rotY:i.rotY};for(let A of n){let{O:M,n:E,u:v,v:I,outlineUv:_,minU:O,minV:V,width:w,height:F}=A,D=f*E[0]+d*E[1]+m*E[2];if(D<=1e-6||!Nt(E,T))continue;let H=ie=>(ie[0]-M[0])*E[0]+(ie[1]-M[1])*E[1]+(ie[2]-M[2])*E[2],K=(ie,ae,N,X)=>{let oe=N/(N-X);return[ie[0]+oe*(ae[0]-ie[0]),ie[1]+oe*(ae[1]-ie[1]),ie[2]+oe*(ae[2]-ie[2])]},Q=ie=>{let ae=ie[0]-M[0],N=ie[1]-M[1],X=ie[2]-M[2],oe=(ae*E[0]+N*E[1]+X*E[2])/D,Te=ie[0]-oe*f,Ce=ie[1]-oe*d,Pe=ie[2]-oe*m,_e=Te-M[0],Oe=Ce-M[1],Ve=Pe-M[2];return[_e*v[0]+Oe*v[1]+Ve*v[2],_e*I[0]+Oe*I[1]+Ve*I[2]]},ne=new Map,ee=0,ue=O,U=V,$=A.minU+w,k=A.minV+F,L=1/0,W=1/0,Y=-1/0,se=-1/0;for(let ie of r)for(let ae of ie.items)for(let N of ae.wv){let X=N[0]-M[0],oe=N[1]-M[1],Te=N[2]-M[2],Ce=X*v[0]+oe*v[1]+Te*v[2],Pe=X*I[0]+oe*I[1]+Te*I[2];Ce<L&&(L=Ce),Ce>Y&&(Y=Ce),Pe<W&&(W=Pe),Pe>se&&(se=Pe)}let Z=Number.isFinite(L)?[[L-x,W-x],[Y+x,W-x],[Y+x,se+x],[L-x,se+x]]:[],B=.05,q=E[0]*M[0]+E[1]*M[1]+E[2]*M[2],me=.0025,fe=5;for(let ie=0;ie<r.length;ie++){let ae=r[ie],N=ae.selfShadowEdgeMap,X=S[ie];if(X!==null){if(X.length===0)continue;for(let oe of X){if(oe.length<3)continue;let Te=C(oe,M,E,B);if(Te.length<3)continue;let Ce=Te.map(Q),Pe=gn(Ce),_e=Z.length===4?An(Pe,Z):Pe;if(_e.length<3)continue;let Oe=An(_e,_);if(Oe.length<3)continue;let Ve;for(let ze of A.memberPolysUv){let Ee=An(Oe,gn(ze));Ee.length<3||(Ve||(Ve=ne.get(ae.id),Ve||(Ve={id:ae.id,verts:[],subPolygonIndices:[]},ne.set(ae.id,Ve))),Ve.verts.push(Ee),Ve.subPolygonIndices.push(-1),ee++)}}continue}for(let oe of ae.items){if(N){let Ee=N.get(oe.polygonIndex);if(Ee){let Fe=!1;for(let we of A.memberPolyIndices)if(Ee.has(we)){Fe=!0;break}if(Fe)continue}}if(oe.planeN&&oe.planeN[0]*f+oe.planeN[1]*d+oe.planeN[2]*m>-1e-6)continue;if(oe.planeN){let Ee=oe.planeN,Fe=Ee[0]*E[0]+Ee[1]*E[1]+Ee[2]*E[2];if(Math.abs(Fe)>1-me){let we=Fe>0?1:-1;if(Math.abs(oe.planeOffset-we*q)<fe)continue}}let Te=oe.bboxCorners,Ce=!1,Pe=1/0,_e=1/0,Oe=-1/0,Ve=-1/0;for(let Ee=0;Ee<8;Ee++){let Fe=Te[Ee];H(Fe)>B&&(Ce=!0);let we=Q(Fe);we[0]<Pe&&(Pe=we[0]),we[0]>Oe&&(Oe=we[0]),we[1]<_e&&(_e=we[1]),we[1]>Ve&&(Ve=we[1])}if(!Ce||Oe<ue||Pe>$||Ve<U||_e>k)continue;let ze=oe.wv;for(let Ee=1;Ee<ze.length-1;Ee++){let Fe=ze[0],we=ze[Ee],zn=ze[Ee+1],$n=H(Fe),cn=H(we),qe=H(zn),ln=[],Be=[[Fe,$n],[we,cn],[zn,qe]];for(let Ye=0;Ye<3;Ye++){let[ve,Qe]=Be[Ye],[on,Je]=Be[(Ye+1)%3],We=Qe>B,rn=Je>B;We&&ln.push(ve),We!==rn&&ln.push(K(ve,on,Qe,Je))}if(ln.length<3)continue;let Xe=ln.map(Q),Me=gn(Xe),Re=Z.length===4?An(Me,Z):Me;if(Re.length<3)continue;let Le=An(Re,_);if(Le.length<3)continue;let Ie;for(let Ye of A.memberPolysUv){let ve=An(Le,gn(Ye));if(!(ve.length<3)){if(N){let Qe=0,on=1/0,Je=-1/0,We=1/0,rn=-1/0;for(let sn=0;sn<ve.length;sn++){let je=ve[sn],Yn=ve[(sn+1)%ve.length];Qe+=je[0]*Yn[1]-Yn[0]*je[1],je[0]<on&&(on=je[0]),je[0]>Je&&(Je=je[0]),je[1]<We&&(We=je[1]),je[1]>rn&&(rn=je[1])}if(Math.abs(Qe)*.5<5)continue}Ie||(Ie=ne.get(ae.id),Ie||(Ie={id:ae.id,verts:[],subPolygonIndices:[]},ne.set(ae.id,Ie))),Ie.verts.push(ve),Ie.subPolygonIndices.push(oe.polygonIndex),ee++}}}}}if(ee===0||!(w>0)||!(F>0))continue;let G=y;if(o){let ae=g*Math.max(0,D)+Math.max(0,h);if(ae>0){let N=Math.max(0,h)/ae,X=Math.pow(N,1/2.4);G=y*(1-X)}else G=0}let te=A.memberPolyIndices[0]??0,ce=t[te]?.color??"#cccccc",be=o?b:Bn(ce,0,"#000000",p,h),he=1/0,ye=1/0,ge=-1/0,Se=-1/0;for(let ie of ne.values())for(let ae of ie.verts)for(let N of ae)N[0]<he&&(he=N[0]),N[0]>ge&&(ge=N[0]),N[1]<ye&&(ye=N[1]),N[1]>Se&&(Se=N[1]);he=Math.floor(he-1),ye=Math.floor(ye-1),ge=Math.ceil(ge+1),Se=Math.ceil(Se+1),he<O&&(he=O),ye<V&&(ye=V),ge>O+w&&(ge=O+w),Se>V+F&&(Se=V+F);let De=ge-he,R=Se-ye;if(!(De>0)||!(R>0))continue;let z=A.lift,j=M[0]+he*v[0]+ye*I[0]+z*E[0],J=M[1]+he*v[1]+ye*I[1]+z*E[1],le=M[2]+he*v[2]+ye*I[2]+z*E[2],pe=`matrix3d(${[v[0],v[1],v[2],0,I[0],I[1],I[2],0,E[0],E[1],E[2],0,j,J,le,1].map(ie=>ie.toFixed(4)).join(",")})`,xe=[];for(let ie of ne.values()){let ae="";for(let N of ie.verts){ae+=`M${(N[0][0]-he).toFixed(1)},${(N[0][1]-ye).toFixed(1)}`;for(let X=1;X<N.length;X++)ae+=`L${(N[X][0]-he).toFixed(1)},${(N[X][1]-ye).toFixed(1)}`;ae+="Z"}xe.push({casterId:ie.id,d:ae,casterPolygonIndices:ie.subPolygonIndices})}P.push({faceIndex:A.faceIndex,memberPolyIndices:A.memberPolyIndices,matrixCss:pe,width:De,height:R,fill:be,opacity:G,paths:xe})}return P}var yg=Symbol.for("polycss.animation.triangleFrameSource"),xg=Symbol.for("polycss.animation.triangleFrameTarget"),dc=2200,Ms=2201,pc=2202;function Pg(e,n){let t=0,o=1,r=1,s=Ms,i=1/0,c=0,a=!1,l=null,u=!0,f=!1,d={clampWhenFinished:!1,get timeScale(){return r},set timeScale(m){r=m},get weight(){return o},set weight(m){o=m},get time(){return t},set time(m){t=m},get isRunning(){return a},get enabled(){return u},set enabled(m){u=m},get paused(){return f},set paused(m){f=m},play(){return a=!0,d},stop(){return a=!1,t=0,c=0,l=null,d},reset(){return t=0,c=0,d},fadeIn(m){return l={from:0,to:1,elapsed:0,duration:m},o=0,d},fadeOut(m){return l={from:o,to:0,elapsed:0,duration:m},d},crossFadeTo(m,p){return d.fadeOut(p),m.fadeIn(p),d},crossFadeFrom(m,p){return m.fadeOut(p),d.fadeIn(p),d},setLoop(m,p){return s=m,i=p,d},setEffectiveTimeScale(m){return r=m,d},setEffectiveWeight(m){return o=m,d}};return d._internal={get clip(){return e},get time(){return t},get weight(){return o},get enabled(){return u},get paused(){return f},get running(){return a},get loopMode(){return s},get repetitions(){return i},get completedReps(){return c},set completedReps(m){c=m},get clampWhenFinished(){return d.clampWhenFinished},get timeScale(){return r},get fade(){return l},advance(m){if(!a)return;if(l){l.elapsed+=m;let h=l.duration>0?Math.min(l.elapsed/l.duration,1):1;if(o=l.from+(l.to-l.from)*h,h>=1&&(o=l.to,l=null,o<=0)){a=!1;return}}if(f)return;let p=e.duration;if(!(p<=0)){if(t+=m*r,s===dc)t>=p&&(t=d.clampWhenFinished?p:0,a=!1);else if(s===Ms){if(t>=p){let h=t%p,g=Math.floor(t/p);c+=g,t=h,i!==1/0&&c>=i&&(t=d.clampWhenFinished?p:0,a=!1)}}else if(p>0){let h=p*2,g=t%h;if(t>=h){let b=Math.floor(t/h);c+=b,i!==1/0&&c>=i?(t=d.clampWhenFinished?p:0,a=!1):t=g}}}},sampleTime(){if(s===pc){let m=e.duration,p=m*2,h=t%p;return h<=m?h:p-h}return t}},d}function Mg(e){return e._internal}function Ps(e,n){return typeof n=="number"?e[n]:e.find(t=>t.name===n)}function Ag(e,n){let t=new Map,o=n,r=e;function s(f){let d=Ps(n.clips,f);if(!d)throw new Error(`PolyAnimationMixer: no clip found for key "${f}". Available: ${n.clips.map(p=>p.name).join(", ")}`);let m=t.get(d.index);return m||(m=Pg(d,n),t.set(d.index,m)),m}function i(f){let d=Ps(n.clips,f);return d?t.get(d.index)??null:null}function c(f){let d=[];for(let b of t.values()){let y=Mg(b);y.advance(f),y.running&&y.enabled&&d.push({internal:y,clip:y.clip})}if(d.length===0)return;if(d.length===1){let{internal:b,clip:y}=d[0],x=b.sampleTime(),P=o[yg]?.(y.name,x);if(P&&r[xg]?.(P))return;let S=n.sample(y.name,x);e.setPolygons(S);return}let m=0;for(let{internal:b}of d)m+=b.weight;if(m<=0)return;let p=d.map(({internal:b,clip:y})=>({polygons:n.sample(y.name,b.sampleTime()),weight:b.weight/m})),h=p[0].polygons;if(h.length===0)return;let g=h.map((b,y)=>{let x=b.vertices.map((P,S)=>{let C=0,T=0,A=0;for(let{polygons:M,weight:E}of p){let v=M[y];if(!v)continue;let I=v.vertices[S];I&&(C+=I[0]*E,T+=I[1]*E,A+=I[2]*E)}return[C,T,A]});return{...b,vertices:x}});e.setPolygons(g)}function a(){for(let f of t.values())f.stop()}function l(f){let d=Ps(n.clips,f);d&&t.delete(d.index)}function u(){t.clear()}return{clipAction:s,existingAction:i,update:c,stopAllAction:a,uncacheClip:l,uncacheRoot:u}}var Ss={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},xc={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16},Sg={0:"POINTS",1:"LINES",2:"LINE_LOOP",3:"LINE_STRIP",4:"TRIANGLES",5:"TRIANGLE_STRIP",6:"TRIANGLE_FAN"},Cs=["EXT_meshopt_compression","KHR_meshopt_compression"];function Cg(e){return e===5120||e===5121||e===5122||e===5123}function Pc(e){let n=globalThis.TextDecoder;return new n().decode(e)}function Eg(e){let n=e.indexOf(",");if(n<0)throw new Error("parseGltf: malformed data: URI");let t=e.slice(5,n),o=e.slice(n+1);if(!t.includes(";base64")){let i=decodeURIComponent(o),c=new Uint8Array(i.length);for(let a=0;a<i.length;a++)c[a]=i.charCodeAt(a)&255;return c}let r=globalThis.atob(o),s=new Uint8Array(r.length);for(let i=0;i<r.length;i++)s[i]=r.charCodeAt(i);return s}function Tg(e){let n=new DataView(e);if(n.getUint32(0,!0)!==1179937895)throw new Error("parseGltf: not a GLB (bad magic)");let t=n.getUint32(4,!0);if(t!==2)throw new Error(`parseGltf: only glTF v2 supported (got v${t})`);let o=12,r=null,s=null;for(;o<e.byteLength;){let i=n.getUint32(o,!0),c=n.getUint32(o+4,!0),a=o+8;if(c===1313821514){let l=new Uint8Array(e,a,i);r=JSON.parse(Pc(l))}else c===5130562&&(s=new Uint8Array(e,a,i));o=a+i}if(!r)throw new Error("parseGltf: no JSON chunk in GLB");return{doc:r,bin:s}}function gc(e){if(!e)return null;let n=/^(\d+)\.(\d+)(?:\D.*)?$/.exec(e);return n?[Number(n[1]),Number(n[2])]:null}function vg(e){let n=gc(e.asset?.version);if(!n||n[0]!==2)throw new Error(`parseGltf: only glTF asset v2 supported (got ${e.asset?.version??"missing"})`);let t=gc(e.asset?.minVersion);if(e.asset?.minVersion&&(!t||t[0]>2||t[0]===2&&t[1]>0))throw new Error(`parseGltf: glTF asset requires minVersion ${e.asset.minVersion}`)}function Ig(e,n,t){let o=e.buffers??[],r=new Set(e.extensionsRequired??[]),s=Cs.some(i=>r.has(i));return o.map((i,c)=>{let a=i.uri;if(a){if(a.startsWith("data:"))return Eg(a);if(t){let l=t(a);if(l instanceof Uint8Array)return l;throw new Error("parseGltf: resolveBuffer returned a Promise; use parseGltf via async if your buffers are external")}throw new Error(`parseGltf: external buffer URI "${a}" \u2014 provide options.resolveBuffer`)}if(c===0&&n)return n;if(s&&Cs.some(l=>i.extensions?.[l]))return new Uint8Array(0);throw new Error(`parseGltf: buffer[${c}] has no uri and no GLB BIN chunk`)})}function Ut(e,n,t){let o=e.bufferViews?.[t],r=o?n[o.buffer]:void 0;if(!o||!r)throw new Error(`parseGltf: bad bufferView ${t}`);let s=o.byteOffset??0;if(s<0||o.byteLength<0||s+o.byteLength>r.byteLength)throw new Error(`parseGltf: bufferView ${t} outside buffer ${o.buffer}`);return{buffer:r,view:o}}function Mc(e,n,t,o){if(e.count<=0)return;let r=e.byteOffset??0,s=r+t*(e.count-1)+o;if(r<0||s>n.byteLength)throw new Error("parseGltf: accessor does not fit bufferView")}function Ac(e,n){return e.byteStride!==void 0&&e.byteStride>0?e.byteStride:n}function _g(e,n,t){if(t||e===5126)return new Float32Array(n);switch(e){case 5120:return new Int8Array(n);case 5121:return new Uint8Array(n);case 5122:return new Int16Array(n);case 5123:return new Uint16Array(n);case 5125:return new Uint32Array(n);default:throw new Error(`parseGltf: unhandled componentType ${e}`)}}function wo(e,n,t){let o=e.accessors?.[t];if(!o)throw new Error(`parseGltf: bad accessor ${t}`);let r=Ss[o.componentType],s=xc[o.type];if(!r||!s)throw new Error(`parseGltf: unsupported accessor type ${o.type}/${o.componentType}`);if(o.sparse||o.normalized||o.bufferView===void 0){let{values:g}=Gt(e,n,t);return{array:_g(o.componentType,g,o.normalized),count:o.count,componentCount:s}}let{buffer:i,view:c}=Ut(e,n,o.bufferView),a=(c.byteOffset??0)+(o.byteOffset??0),l=o.count*s,u=r*s,f=Ac(c,u);if(Mc(o,c,f,u),f===u){let g=i.buffer.slice(i.byteOffset+a,i.byteOffset+a+l*r),b;switch(o.componentType){case 5120:b=new Int8Array(g);break;case 5121:b=new Uint8Array(g);break;case 5122:b=new Int16Array(g);break;case 5123:b=new Uint16Array(g);break;case 5125:b=new Uint32Array(g);break;case 5126:b=new Float32Array(g);break;default:throw new Error(`parseGltf: unhandled componentType ${o.componentType}`)}return{array:b,count:o.count,componentCount:s}}let d;switch(o.componentType){case 5120:d=new Int8Array(l);break;case 5121:d=new Uint8Array(l);break;case 5122:d=new Int16Array(l);break;case 5123:d=new Uint16Array(l);break;case 5125:d=new Uint32Array(l);break;case 5126:d=new Float32Array(l);break;default:throw new Error(`parseGltf: unhandled componentType ${o.componentType}`)}let m=new DataView(i.buffer),p=i.byteOffset+a,h=0;for(let g=0;g<o.count;g++){let b=p+g*f;for(let y=0;y<s;y++)d[h++]=Fo(m,b+y*r,o.componentType)}return{array:d,count:o.count,componentCount:s}}function Fo(e,n,t){switch(t){case 5120:return e.getInt8(n);case 5121:return e.getUint8(n);case 5122:return e.getInt16(n,!0);case 5123:return e.getUint16(n,!0);case 5125:return e.getUint32(n,!0);case 5126:return e.getFloat32(n,!0);default:throw new Error(`parseGltf: unhandled componentType ${t}`)}}function hc(e,n){switch(n){case 5120:return Math.max(e/127,-1);case 5121:return e/255;case 5122:return Math.max(e/32767,-1);case 5123:return e/65535;default:return e}}function Gt(e,n,t){let o=e.accessors?.[t];if(!o)throw new Error(`parseGltf: bad accessor ${t}`);let r=Ss[o.componentType],s=xc[o.type];if(!r||!s)throw new Error(`parseGltf: unsupported accessor type ${o.type}/${o.componentType}`);let i=r*s,c=new Array(o.count*s).fill(0);if(o.bufferView!==void 0){let{buffer:a,view:l}=Ut(e,n,o.bufferView),u=a.byteOffset+(l.byteOffset??0)+(o.byteOffset??0),f=Ac(l,i);Mc(o,l,f,i);let d=new DataView(a.buffer),m=0;for(let p=0;p<o.count;p++){let h=u+p*f;for(let g=0;g<s;g++){let b=Fo(d,h+g*r,o.componentType);c[m++]=o.normalized?hc(b,o.componentType):b}}}if(o.sparse){let a=o.sparse,l=a.indices,u=Ss[l.componentType];if(l.componentType!==5121&&l.componentType!==5123&&l.componentType!==5125)throw new Error(`parseGltf: unhandled sparse index componentType ${l.componentType}`);let{buffer:f,view:d}=Ut(e,n,l.bufferView),m=f.byteOffset+(d.byteOffset??0)+(l.byteOffset??0),p=new DataView(f.buffer),{buffer:h,view:g}=Ut(e,n,a.values.bufferView),b=h.byteOffset+(g.byteOffset??0)+(a.values.byteOffset??0),y=new DataView(h.buffer);for(let x=0;x<a.count;x++){let P=Fo(p,m+x*u,l.componentType);for(let S=0;S<s;S++){let C=Fo(y,b+(x*s+S)*r,o.componentType);c[P*s+S]=o.normalized?hc(C,o.componentType):C}}}return{values:c,count:o.count,componentCount:s}}function Og(e,n,t){let o=[],r=[],s=globalThis;for(let i of e.images??[]){if(i.uri){if(t&&!i.uri.startsWith("data:"))try{o.push(new s.URL(i.uri,t).href)}catch{o.push(i.uri)}else o.push(i.uri);continue}if(i.bufferView!==void 0){let c;try{let{buffer:f,view:d}=Ut(e,n,i.bufferView),m=d.byteOffset??0;c=f.subarray(m,m+d.byteLength)}catch{o.push("");continue}let a=i.mimeType??"image/png",l=new s.Blob([c],{type:a}),u=s.URL.createObjectURL(l);o.push(u),r.push(u)}else o.push("")}return{urls:o,objectUrls:r}}function bc(e){switch(e??10497){case 33071:return"clamp-to-edge";case 33648:return"mirrored-repeat";case 10497:return"repeat";default:return"repeat"}}function Vg(e,n){let t=n?.sampler!==void 0?e.samplers?.[n.sampler]:void 0;return{s:bc(t?.wrapS),t:bc(t?.wrapT)}}function Sc(e){switch(e){case"BLEND":return"blend";case"MASK":return"mask";default:return"opaque"}}function Rg(e,n){let t=new Map,o=e.materials??[];for(let r=0;r<o.length;r++){let s=o[r].pbrMetallicRoughness?.baseColorTexture?.index;if(s===void 0)continue;let i=e.textures?.[s],c=i?.source;if(c===void 0)continue;let a=n[c];a&&t.set(r,{url:a,wrap:Vg(e,i),alphaMode:Sc(o[r].alphaMode),texCoord:o[r].pbrMetallicRoughness?.baseColorTexture?.texCoord??0})}return t}function Lg(e,n,t){let o=e?.pbrMetallicRoughness?.baseColorFactor;if(!o||o.length<3)return n;let r=a=>Math.max(0,Math.min(1,a)),s=a=>Math.round(r(a)*255).toString(16).padStart(2,"0"),i=a=>Math.round(r(a)*255),c=t==="opaque"?1:r(o[3]??1);return c<1?`rgba(${i(o[0])}, ${i(o[1])}, ${i(o[2])}, ${Math.round(c*1e3)/1e3})`:`#${s(o[0])}${s(o[1])}${s(o[2])}`}var ft=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function Cc(e,n){let t=new Array(16);return Ec(t,e,n)}function Ec(e,n,t){for(let o=0;o<4;o++)for(let r=0;r<4;r++)e[r*4+o]=n[0+o]*t[r*4+0]+n[4+o]*t[r*4+1]+n[8+o]*t[r*4+2]+n[12+o]*t[r*4+3];return e}function No(e,n){return[e[0]*n[0]+e[4]*n[1]+e[8]*n[2]+e[12],e[1]*n[0]+e[5]*n[1]+e[9]*n[2]+e[13],e[2]*n[0]+e[6]*n[1]+e[10]*n[2]+e[14]]}function Tc(e,n,t){let o=e?.[0]??0,r=e?.[1]??0,s=e?.[2]??0,i=n?.[0]??0,c=n?.[1]??0,a=n?.[2]??0,l=n?.[3]??1,u=t?.[0]??1,f=t?.[1]??1,d=t?.[2]??1,m=i+i,p=c+c,h=a+a,g=i*m,b=i*p,y=i*h,x=c*p,P=c*h,S=a*h,C=l*m,T=l*p,A=l*h;return[(1-(x+S))*u,(b+A)*u,(y-T)*u,0,(b-A)*f,(1-(g+S))*f,(P+C)*f,0,(y+T)*d,(P-C)*d,(1-(g+x))*d,0,o,r,s,1]}function wg(e){return e.matrix&&e.matrix.length===16?e.matrix.slice():Tc(e.translation,e.rotation,e.scale)}function vc(e,n,t){let o=new Array(Math.min(e.length,n.length));for(let r=0;r<o.length;r++)o[r]=e[r]+(n[r]-e[r])*t;return o}function kt(e){let n=Math.hypot(e[0]??0,e[1]??0,e[2]??0,e[3]??1)||1;return[(e[0]??0)/n,(e[1]??0)/n,(e[2]??0)/n,(e[3]??1)/n]}function Ng(e,n,t){let o=kt(e),r=kt(n),s=o[0]*r[0]+o[1]*r[1]+o[2]*r[2]+o[3]*r[3];if(s<0&&(s=-s,r=[-r[0],-r[1],-r[2],-r[3]]),s>.9995)return kt(vc(o,r,t));let i=Math.acos(Math.max(-1,Math.min(1,s))),c=Math.sin(i),a=Math.sin((1-t)*i)/c,l=Math.sin(t*i)/c;return kt([o[0]*a+r[0]*l,o[1]*a+r[1]*l,o[2]*a+r[2]*l,o[3]*a+r[3]*l])}function Ic(e){return{translation:e?.translation?.slice()??[0,0,0],rotation:e?.rotation?.slice()??[0,0,0,1],scale:e?.scale?.slice()??[1,1,1],matrix:e?.matrix&&e.matrix.length===16?e.matrix.slice():void 0}}function _c(e){return e.matrix?e.matrix.slice():Tc(e.translation,e.rotation,e.scale)}function Dg(e){let n=e.scene??0,t=e.scenes?.[n]?.nodes;return t&&t.length>0?t:[]}function Oc(e,n){let t=e.nodes??[],o=new Array(t.length),r=new Set,s=new Set,i=(a,l)=>{if(a<0||a>=t.length||s.has(a))return;let u=Cc(l,n[a]??ft);o[a]=u,r.add(a),s.add(a);for(let f of t[a].children??[])i(f,u);s.delete(a)},c=Dg(e);if(c.length>0)for(let a of c)i(a,ft);for(let a=0;a<t.length;a++)r.has(a)||i(a,ft);return o}var Vc=Symbol("polycss.gltfAnimationRuntimeInfo"),Fg=Symbol.for("polycss.animation.triangleFrameSource");function Rc(e,n){return e?e[Vc]?.withPolygonFilter(n):void 0}function As(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function Bg(e,n,t){return As(e,n)||As(e,t)||As(n,t)}function yc(e,n,t,o,r){if(t===void 0)return;let{values:s,count:i,componentCount:c}=Gt(e,n,t);if(i!==r||c<1)return;let a=[];for(let l=0;l<i;l++){let u=[];for(let f=0;f<o;f++)u.push(s[l*c+f]??0);a.push(u)}return a}function kg(e,n,t,o){if(t===void 0)return Array.from({length:o},()=>ft.slice());let{values:r,componentCount:s,count:i}=Gt(e,n,t);if(s!==16)throw new Error(`parseGltf: inverseBindMatrices accessor ${t} is not MAT4`);let c=[];for(let a=0;a<o;a++){let l=Math.min(a,i-1);c.push(r.slice(l*16,l*16+16))}return c}function Do(e,n){let t=e.componentCount,o=e.interpolation==="CUBICSPLINE"?(n*3+1)*t:n*t;return e.output.slice(o,o+t)}function Ug(e,n,t){let o=e.input;if(o.length===0)return[];if(o.length===1||n<=o[0])return Do(e,0);let r=o.length-1;if(n>=o[r])return Do(e,r);let s=0,i=r;for(;s+1<i;){let d=s+i>>1;o[d]<=n?s=d:i=d}let c=o[s],a=o[s+1],l=a>c?(n-c)/(a-c):0,u=Do(e,s),f=Do(e,s+1);return e.interpolation==="STEP"?u:t==="rotation"?Ng(u,f,l):vc(u,f,l)}function Lc(e,n){let t=[];for(let o=0;o<(e.animations?.length??0);o++){let r=e.animations[o],s=(r.samplers??[]).map(a=>{let l=Gt(e,n,a.input),u=Gt(e,n,a.output);return{input:l.values,output:u.values,componentCount:u.componentCount,interpolation:a.interpolation??"LINEAR"}}),i=[];for(let a of r.channels??[]){let l=a.target.node,u=a.target.path,f=s[a.sampler];l===void 0||!u||!f||u!=="translation"&&u!=="rotation"&&u!=="scale"||i.push({sampler:f,targetNode:l,path:u})}let c=i.reduce((a,l)=>{let u=l.sampler.input;return Math.max(a,u[u.length-1]??0)},0);t.push({info:{index:o,name:r.name??`animation_${o}`,duration:c,channelCount:i.length},channels:i})}return t}function wc(e,n,t,o){let r=t.info.duration,s=r>0?(o%r+r)%r:Math.max(0,o),i=n.map(c=>({translation:c.translation.slice(),rotation:c.rotation.slice(),scale:c.scale.slice(),matrix:c.matrix?c.matrix.slice():void 0}));for(let c of t.channels){let a=i[c.targetNode];if(!a)continue;let l=Ug(c.sampler,s,c.path);a.matrix=void 0,c.path==="translation"?a.translation=l.slice(0,3):c.path==="rotation"?a.rotation=kt(l.slice(0,4)):c.path==="scale"&&(a.scale=l.slice(0,3))}return Oc(e,i.map(_c))}function Gg(e,n,t,o,r,s){if((e.animations??[]).length===0||t.length===0)return;let c=(e.nodes??[]).map(g=>Ic(g)),a=c.map(_c),l=Oc(e,a),u=(e.skins??[]).map(g=>({joints:g.joints??[],inverseBindMatrices:kg(e,n,g.inverseBindMatrices,g.joints?.length??0)})),f=Lc(e,n),d=f.map(g=>g.info);if(d.length===0)return;let m=g=>{let b=t.map(y=>new Array(y.triangleMask.length).fill(!1));for(let y of g){let x=o[y];if(!x)return;let P=b[x.sourceIndex];if(!P||x.triangleIndex<0||x.triangleIndex>=P.length)return;P[x.triangleIndex]=!0}return b.map((y,x)=>{let P=t[x],S=new Set,C=0;for(let T=0;T+2<P.indices.length;T+=3,C++)y[C]&&(S.add(P.indices[T]),S.add(P.indices[T+1]),S.add(P.indices[T+2]));return{triangleMask:y,activeVertices:Array.from(S).sort((T,A)=>T-A)}})},p=(g,b,y,x,P,S,C,T,A)=>{let M=r(g),E=r(b),v=r(y),I={vertices:[M,E,v],color:x};return P&&(I.texture=P),P&&S&&(I.textureWrap=S),P&&C&&(I.textureAlphaMode=C),T&&(I.doubleSided=!0),A&&(I.uvs=A),I},h=g=>{let b=t.reduce((_,O,V)=>{let w=g?.[V]?.triangleMask??O.triangleMask,F=0;for(let D=0;D<w.length;D++)w[D]&&F++;return _+F},0),y=new Float64Array(Math.max(0,b*9)),x=new Array(b),P=new Array(b).fill(!1),S={polygonCount:0,vertices:y,colors:x,textureFlags:P,solidTriangles:!0},C=t.map(_=>new Array(_.positions.length)),T=u.map(_=>_.joints.map(()=>new Array(16))),A=(_,O)=>{let V=typeof _=="number"?f[_]:f.find(w=>w.info.name===_);return V?wc(e,c,V,O):null},M=(_,O,V,w)=>{let F=V?.activeVertices,D=C[_],H=(K,Q,ne,ee)=>{let ue=D[K];ue?(ue[0]=Q,ue[1]=ne,ue[2]=ee):D[K]=[Q,ne,ee]};if(O.skinIndex!==void 0&&O.joints&&O.weights&&u[O.skinIndex]){let K=u[O.skinIndex],Q=O.joints,ne=O.weights,ee=T[O.skinIndex]??[];for(let U=0;U<K.joints.length;U++){let $=K.joints[U],k=w[$],L=K.inverseBindMatrices[U],W=ee[U]??(ee[U]=new Array(16));ee[U]=k&&L?Ec(W,k,L):void 0}let ue=U=>{let $=O.positions[U],k=0,L=0,W=0,Y=0,se=Q[U]??[],Z=ne[U]??[];for(let B=0;B<4;B++){let q=Z[B]??0;if(q<=0)continue;let me=Math.round(se[B]??0),fe=ee[me];if(!fe)continue;let G=fe[0]*$[0]+fe[4]*$[1]+fe[8]*$[2]+fe[12],te=fe[1]*$[0]+fe[5]*$[1]+fe[9]*$[2]+fe[13],ce=fe[2]*$[0]+fe[6]*$[1]+fe[10]*$[2]+fe[14];k+=G*q,L+=te*q,W+=ce*q,Y+=q}if(Y>0){let B=1/Y;H(U,k*B,L*B,W*B)}else H(U,O.meshBindWorld[0]*$[0]+O.meshBindWorld[4]*$[1]+O.meshBindWorld[8]*$[2]+O.meshBindWorld[12],O.meshBindWorld[1]*$[0]+O.meshBindWorld[5]*$[1]+O.meshBindWorld[9]*$[2]+O.meshBindWorld[13],O.meshBindWorld[2]*$[0]+O.meshBindWorld[6]*$[1]+O.meshBindWorld[10]*$[2]+O.meshBindWorld[14])};if(F)for(let U of F)ue(U);else for(let U=0;U<O.positions.length;U++)ue(U)}else{let K=O.meshNode!==null?w[O.meshNode]??O.meshBindWorld:O.meshBindWorld,Q=ne=>{let ee=O.positions[ne];H(ne,K[0]*ee[0]+K[4]*ee[1]+K[8]*ee[2]+K[12],K[1]*ee[0]+K[5]*ee[1]+K[9]*ee[2]+K[13],K[2]*ee[0]+K[6]*ee[1]+K[10]*ee[2]+K[14])};if(F)for(let ne of F)Q(ne);else for(let ne=0;ne<O.positions.length;ne++)Q(ne)}return D},E=(_,O)=>{let V=A(_,O);if(!V)return[];let w=[];for(let F=0;F<t.length;F++){let D=t[F],H=g?.[F],K=H?.triangleMask??D.triangleMask,Q=M(F,D,H,V),ne=0;for(let ee=0;ee+2<D.indices.length;ee+=3,ne++){if(!K[ne])continue;let ue=D.indices[ee],U=D.indices[ee+1],$=D.indices[ee+2],k=Q[ue],L=Q[U],W=Q[$];if(!k||!L||!W)continue;let Y;if(D.uvs&&D.texture){let Z=D.uvs[ue],B=D.uvs[U],q=D.uvs[$];Z&&B&&q&&(Y=[Z,B,q])}let se=p(k,L,W,D.color,D.texture,D.textureWrap,D.textureAlphaMode,D.doubleSided,Y);se&&w.push(se)}}return w},v=(_,O)=>{let V=A(_,O);if(!V)return null;y.length<b*9&&(y=new Float64Array(b*9),S.vertices=y),x.length<b&&(x=new Array(b),S.colors=x),P.length<b&&(P=new Array(b).fill(!1),S.textureFlags=P);let w=0,F=0,D=!1;for(let H=0;H<t.length;H++){let K=t[H],Q=g?.[H],ne=Q?.triangleMask??K.triangleMask,ee=M(H,K,Q,V),ue=0;for(let U=0;U+2<K.indices.length;U+=3,ue++){if(!ne[ue])continue;let $=K.indices[U],k=K.indices[U+1],L=K.indices[U+2],W=ee[$],Y=ee[k],se=ee[L];if(!W||!Y||!se)continue;s(W,y,F),s(Y,y,F+3),s(se,y,F+6),x[w]=K.color;let Z=!!K.texture;P[w]=Z,Z&&(D=!0),w++,F+=9}}return S.polygonCount=w,S.solidTriangles=!D,S},I={clips:d,sample:E};return I[Fg]=v,I[Vc]={withPolygonFilter(_){let O=m(_);return O?h(O):void 0}},I};return h()}function Es(e,n){let t=n?.targetSize??60,o=n?.defaultColor??"#888888",r=n?.materialColors??{},s=n?.materialTextures??{},i=e instanceof Uint8Array?e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength):e,c=i.byteLength,a,l=null;if(i.byteLength>=4&&new DataView(i).getUint32(0,!0)===1179937895){let R=Tg(i);a=R.doc,l=R.bin}else a=JSON.parse(Pc(new Uint8Array(i)));vg(a);let u=Ig(a,l,n?.resolveBuffer),{urls:f,objectUrls:d}=Og(a,u,n?.baseUrl),m=Rg(a,f),p=[],h=new Set,g=new Set(a.extensionsRequired??[]),b=g.has("KHR_mesh_quantization")||(a.extensionsUsed??[]).includes("KHR_mesh_quantization");function y(R,z){h.has(R)||(h.add(R),p.push(z))}function x(R,z){if(R===void 0)return!1;let j=a.accessors?.[R];if(!j)return!1;let J=le=>a.bufferViews?.[le]?.extensions?.[z]!==void 0;return!!(j.bufferView!==void 0&&J(j.bufferView)||j.sparse&&(J(j.sparse.indices.bufferView)||J(j.sparse.values.bufferView)))}function P(R){for(let z of Cs)if(g.has(z)&&(R.extensions?.[z]||x(R.indices,z)||Object.values(R.attributes??{}).some(j=>x(j,z))))return z}function S(R,z){if(z instanceof Float32Array)return!0;let j=a.accessors?.[R];return b&&j?.type==="VEC3"&&Cg(j.componentType)}function C(R,z){return(z!==null?a.nodes?.[z]?.weights:void 0)??R.weights??[]}function T(R,z,j,J,le){let re=j.targets;if(!re||re.length===0)return;let pe=C(R,J);if(!pe.some(ae=>Number.isFinite(ae)&&ae!==0))return;let xe=Array.from({length:le},()=>[0,0,0]),ie=!1;for(let ae=0;ae<re.length;ae++){let N=pe[ae]??0;if(!Number.isFinite(N)||N===0)continue;let X=re[ae]?.POSITION;if(X===void 0)continue;let{array:oe,count:Te,componentCount:Ce}=wo(a,u,X);if(Te!==le||Ce<3){y(`morph-position-count:${z}:${ae}`,`Mesh ${R.name??z}: skipped morph target ${ae} POSITION with mismatched count`);continue}if(!S(X,oe)){y(`morph-position-type:${z}:${ae}`,`Mesh ${R.name??z}: skipped morph target ${ae} POSITION with unsupported accessor type`);continue}ie=!0;for(let Pe=0;Pe<le;Pe++){let _e=Pe*Ce,Oe=xe[Pe];Oe[0]+=Number(oe[_e])*N,Oe[1]+=Number(oe[_e+1])*N,Oe[2]+=Number(oe[_e+2])*N}}return ie?xe:void 0}let A=[],M=[],E=[],v=(a.meshes??[]).map((R,z)=>R.name??`mesh_${z}`),I=(a.materials??[]).map((R,z)=>R.name??`material_${z}`);function _(R,z){if(z===4)return R;let j=[];if(z===5)for(let J=0;J+2<R.length;J++)j.push(R[J],R[J+1+J%2],R[J+2-J%2]);else if(z===6)for(let J=0;J+2<R.length;J++)j.push(R[J+1],R[J+2],R[0]);return j}function O(R){let z=[];for(let j=0;j+2<R.length;j+=3){let J=R[j],le=R[j+1],re=R[j+2];z.push(J,le,re,J,re,le)}return z}function V(R,z,j){let J=a.meshes?.[R];if(J){if(!Array.isArray(J.primitives)){y(`malformed-primitives:${R}`,`Mesh ${J.name??R}: skipped mesh with non-array primitives`);return}for(let le=0;le<J.primitives.length;le+=1){let re=J.primitives[le];if(!re)continue;let pe=re.mode??4;if(pe!==4&&pe!==5&&pe!==6){let Me=Sg[pe]??`mode ${pe}`;y(`unsupported-mode:${pe}`,`Skipped primitives with unsupported mode ${pe} (${Me})`);continue}if(re.extensions?.KHR_draco_mesh_compression&&g.has("KHR_draco_mesh_compression")){y("KHR_draco_mesh_compression","Skipped primitives with unsupported required extension KHR_draco_mesh_compression");continue}let xe=P(re);if(xe){y(xe,`Skipped primitives with unsupported required extension ${xe}`);continue}if(re.attributes.POSITION===void 0){y(`missing-position:${R}`,`Mesh ${J.name??R}: skipped primitive without POSITION attribute`);continue}let ie=re.material!==void 0?a.materials?.[re.material]:void 0,ae=Sc(ie?.alphaMode),N=ie?.name,oe=(N?r[N]:void 0)??Lg(ie,o,ae),Te=ie?.doubleSided===!0,Ce=re.material!==void 0?m.get(re.material):void 0,Pe=N&&s[N]?s[N]:re.material!==void 0?Ce?.url:void 0,_e=Pe&&re.material!==void 0?Ce?.wrap:void 0,Oe=Pe?Ce?.alphaMode??ae:void 0,Ve=Pe?Ce?.texCoord??0:0,ze=re.attributes.POSITION,{array:Ee,count:Fe,componentCount:we}=wo(a,u,ze);if(!S(ze,Ee))continue;let zn=T(J,R,re,j,Fe),$n=[],cn=[];for(let Me=0;Me<Fe;Me++){let Re=Me*we,Le=zn?.[Me],Ie=[Number(Ee[Re])+(Le?.[0]??0),Number(Ee[Re+1])+(Le?.[1]??0),Number(Ee[Re+2])+(Le?.[2]??0)];$n.push(Ie),cn.push(No(z,Ie))}let qe=null,ln=re.attributes[`TEXCOORD_${Ve}`];if(Pe&&ln!==void 0){let{array:Me,count:Re}=wo(a,u,ln);qe=[];let Le=1;Me instanceof Uint8Array?Le=1/255:Me instanceof Uint16Array&&(Le=1/65535);for(let Ie=0;Ie<Re;Ie++){let Ye=Me[Ie*2]*Le,ve=Me[Ie*2+1]*Le;qe.push([Ye,1-ve])}}let Be;if(re.indices!==void 0){let{array:Me,count:Re}=wo(a,u,re.indices);Be=[];for(let Le=0;Le<Re;Le++)Be.push(Number(Me[Le]))}else Be=cn.map((Me,Re)=>Re);Be=_(Be,pe),Te&&(Be=O(Be));let Xe;if((a.animations?.length??0)>0){let Me=yc(a,u,re.attributes.JOINTS_0,4,Fe),Re=yc(a,u,re.attributes.WEIGHTS_0,4,Fe);Xe={sourceIndex:M.length,meshNode:j,meshBindWorld:z,skinIndex:j!==null?a.nodes?.[j]?.skin:void 0,positions:$n,indices:Be,triangleMask:[],color:oe,texture:Pe,textureWrap:_e,textureAlphaMode:Oe,doubleSided:Te,uvs:qe??void 0,joints:Me,weights:Re},M.push(Xe)}for(let Me=0;Me+2<Be.length;Me+=3){let Re=Be[Me],Le=Be[Me+1],Ie=Be[Me+2],Ye=Xe?Xe.triangleMask.length:void 0;Xe&&Xe.triangleMask.push(!1);let ve=cn[Re],Qe=cn[Le],on=cn[Ie];if(!ve||!Qe||!on)continue;let Je;if(qe&&Pe){let rn=qe[Re],Xn=qe[Le],sn=qe[Ie];rn&&Xn&&sn&&(Je=[rn,Xn,sn])}let We=`${R}:${le}:${j??"root"}`;A.push({v0:ve,v1:Qe,v2:on,color:oe,texture:Pe,textureWrap:_e,textureAlphaMode:Oe,doubleSided:Te,uvs:Je,simplifySourceVertexKeys:[`${We}:${Re}`,`${We}:${Le}`,`${We}:${Ie}`],source:Xe,sourceIndex:Xe?.sourceIndex,sourceTriangleIndex:Ye})}}}}function w(R,z,j){if(j.has(R)){y(`recursive-node:${R}`,`Skipped recursive node reference ${R} in glTF scene graph`);return}let J=a.nodes?.[R];if(!J)return;let le=Cc(z,wg(J));typeof J.mesh=="number"&&V(J.mesh,le,R),j.add(R);for(let re of J.children??[])w(re,le,j);j.delete(R)}function F(R){let z=new Set,j=R.info.duration;for(let J of R.channels)for(let le of J.sampler.input)!Number.isFinite(le)||le<=0||j>0&&le>=j||z.add(le);return j>0&&(z.add(j*.25),z.add(j*.5),z.add(j*.75)),Array.from(z).sort((J,le)=>J-le).slice(0,128)}function D(R,z){let j=R.source;if(!j||R.sourceTriangleIndex===void 0||j.skinIndex!==void 0)return;let J=R.sourceTriangleIndex*3,le=j.indices[J],re=j.indices[J+1],pe=j.indices[J+2],xe=le!==void 0?j.positions[le]:void 0,ie=re!==void 0?j.positions[re]:void 0,ae=pe!==void 0?j.positions[pe]:void 0;if(!xe||!ie||!ae)return;let N=j.meshNode!==null?z[j.meshNode]??j.meshBindWorld:j.meshBindWorld;return{v0:No(N,xe),v1:No(N,ie),v2:No(N,ae)}}function H(R,z){return R[0]===z[0]&&R[1]===z[1]&&R[2]===z[2]}function K(R){return H(R.v0,R.v1)||H(R.v0,R.v2)||H(R.v1,R.v2)}function Q(){return A.some(R=>!K(R))}function ne(){if(A.length===0||(a.animations?.length??0)===0||Q())return;let R=Lc(a,u);if(R.length===0)return;let z=(a.nodes??[]).map(J=>Ic(J)),j=new Array(A.length);for(let J of R)for(let le of F(J)){let re=wc(a,z,J,le);for(let pe=0;pe<A.length;pe++){if(j[pe])continue;let xe=D(A[pe],re);xe&&!K(xe)&&(j[pe]=xe)}}if(j.some(Boolean))for(let J=0;J<A.length;J++){let le=j[J];le&&(A[J].v0=le.v0,A[J].v1=le.v1,A[J].v2=le.v2)}}let ee=a.scene??0,ue=a.scenes?.[ee]?.nodes;if(ue&&ue.length>0)for(let R of ue)w(R,ft,new Set);else{(a.meshes?.length??0)===0&&y("no-gltf-meshes","No glTF meshes found");for(let R=0;R<(a.meshes?.length??0);R++)V(R,ft,null)}ne();let U=zg(d);if(A.length===0)return{polygons:[],objectUrls:d,dispose:U,warnings:p,metadata:{triangleCount:0,meshes:v,materials:I,sourceBytes:c}};let $=1/0,k=1/0,L=1/0,W=-1/0,Y=-1/0,se=-1/0;for(let R of A)for(let z of[R.v0,R.v1,R.v2])z[0]<$&&($=z[0]),z[0]>W&&(W=z[0]),z[1]<k&&(k=z[1]),z[1]>Y&&(Y=z[1]),z[2]<L&&(L=z[2]),z[2]>se&&(se=z[2]);let Z=Math.max(W-$,Y-k,se-L),B=Z>0?t/Z:1,q=n?.center,me=q===!0||q==="center",fe=me?($+W)*.5:$,G=me?(k+Y)*.5:k,te=me?(L+se)*.5:L,ce=R=>Math.round(R*1e3)/1e3,be=n?.upAxis??"y",he=be==="z"?([R,z,j])=>[ce((R-fe)*B),ce((z-G)*B),ce((j-te)*B)]:([R,z,j])=>[ce((j-te)*B),ce((R-fe)*B),ce((z-G)*B)],ye=be==="z"?(R,z,j)=>{z[j]=ce((R[0]-fe)*B),z[j+1]=ce((R[1]-G)*B),z[j+2]=ce((R[2]-te)*B)}:(R,z,j)=>{z[j]=ce((R[2]-te)*B),z[j+1]=ce((R[0]-fe)*B),z[j+2]=ce((R[1]-G)*B)},ge=[],Se=0;for(let R of A){let z=he(R.v0),j=he(R.v1),J=he(R.v2),le=Bg(z,j,J);if(R.source&&R.sourceTriangleIndex!==void 0&&(R.source.triangleMask[R.sourceTriangleIndex]=!le),le){Se++;continue}let re={vertices:[z,j,J],color:R.color};R.texture&&(re.texture=R.texture),R.texture&&R.textureWrap&&(re.textureWrap=R.textureWrap),R.texture&&R.textureAlphaMode&&(re.textureAlphaMode=R.textureAlphaMode),R.doubleSided&&(re.doubleSided=!0),R.uvs&&(re.uvs=R.uvs),R.simplifySourceVertexKeys&&(re.simplifySourceVertexKeys=R.simplifySourceVertexKeys),ge.push(re),E.push(R.sourceIndex!==void 0&&R.sourceTriangleIndex!==void 0?{sourceIndex:R.sourceIndex,triangleIndex:R.sourceTriangleIndex}:void 0)}ge.length===0&&Se>0&&y("all-projected-triangles-degenerate","No non-degenerate glTF triangles remained after normalization");let De=Gg(a,u,M,E,he,ye);return{polygons:ge,animation:De,objectUrls:d,dispose:U,warnings:p,metadata:{triangleCount:ge.length,meshes:v,materials:I,animations:De?.clips,sourceBytes:c}}}function zg(e){let n=!1;return()=>{if(n)return;n=!0;let t=globalThis.URL;if(t?.revokeObjectURL)for(let o of e)try{t.revokeObjectURL(o)}catch{}}}var zc=Symbol("polycss.solidTextureBakedAnimationInfo"),Nc=Symbol.for("polycss.animation.triangleFrameSource"),$g=16*1024*1024,Xg=2,Yg=32,Wg=2,jg=.08,Kg=8,Hg=128,Dc=32,Zg=.045,qg=10,mt=6,Qg=16;function Ts(e){return e.material?.texture??e.texture}function Jg(){let e=globalThis;return typeof e.Image!="function"||typeof e.document?.createElement!="function"?null:{Image:e.Image,createCanvas(){return e.document.createElement("canvas")}}}function e0(e,n){return new Promise((t,o)=>{let r=new n,s=!1,i=c=>{s||(s=!0,c())};r.decoding="async",r.onload=()=>i(()=>t(r)),r.onerror=()=>i(()=>o(new Error(`texture load failed: ${e}`))),r.src=e})}async function n0(e,n,t){try{let o=await e0(e,n.Image),r=Math.max(0,Math.floor(o.naturalWidth||o.width||0)),s=Math.max(0,Math.floor(o.naturalHeight||o.height||0));if(r<=0||s<=0||r*s>t)return null;let i=n.createCanvas();i.width=r,i.height=s;let c=i.getContext("2d",{willReadFrequently:!0});if(!c)return null;c.drawImage(o,0,0,r,s);let a=c.getImageData(0,0,r,s).data;return{width:r,height:s,data:a,lowDetail:t0(r,s,a)}}catch{return null}}function Fc(e,n,t,o,r,s){let i=(o*n+t)*4,c=(s*n+r)*4;return Math.max(Math.abs((e[i]??0)-(e[c]??0)),Math.abs((e[i+1]??0)-(e[c+1]??0)),Math.abs((e[i+2]??0)-(e[c+2]??0)))}function t0(e,n,t){let o=Math.max(1,Math.floor(Math.max(e,n)/Hg)),r=0,s=0,i=0;for(let c=0;c<n;c+=o)for(let a=0;a<e;a+=o){if(a+o<e){let l=Fc(t,e,a,c,a+o,c);i+=l,r++,l>Dc&&s++}if(c+o<n){let l=Fc(t,e,a,c,a,c+o);i+=l,r++,l>Dc&&s++}}return r>0&&s/r<=Zg&&i/r<=qg}function Bc(e,n,t){return Math.max(n,Math.min(t,e))}function kc(e,n){if(!Number.isFinite(e))return null;switch(n){case"repeat":return e-Math.floor(e);case"mirrored-repeat":{let t=Math.floor(e),o=e-t;return Math.abs(t%2)===1?1-o:o}default:return Math.max(0,Math.min(1,e))}}function o0(e,n,t){let o=n[0],r=1-n[1],s=kc(o,t?.s),i=kc(r,t?.t);if(s===null||i===null)return null;let c=Bc(Math.floor(s*e.width),0,e.width-1),l=(Bc(Math.floor(i*e.height),0,e.height-1)*e.width+c)*4;return{r:e.data[l]??0,g:e.data[l+1]??0,b:e.data[l+2]??0,a:e.data[l+3]??255}}function Cn(e,n,t,o,r,s){return[e[0]*o+n[0]*r+t[0]*s,e[1]*o+n[1]*r+t[1]*s]}function r0(e){let[n,t,o]=e;return[Cn(n,t,o,1/3,1/3,1/3),Cn(n,t,o,.8,.1,.1),Cn(n,t,o,.1,.8,.1),Cn(n,t,o,.1,.1,.8),Cn(n,t,o,.45,.45,.1),Cn(n,t,o,.45,.1,.45),Cn(n,t,o,.1,.45,.45)]}function s0(e){let[n,t,o]=e,r=r0(e);for(let s=1;s<mt;s++)for(let i=1;i<mt-s;i++){let c=mt-s-i;c<=0||r.push(Cn(n,t,o,s/mt,i/mt,c/mt))}return r}function $c(e){if(e.textureTriangles?.length)return e.textureTriangles;let n=e.uvs;if(!n||n.length!==e.vertices.length||n.length<3)return[];let t=[];for(let o=1;o+1<n.length;o++)t.push({uvs:[n[0],n[o],n[o+1]]});return t}function vs(e,n,t){return Math.abs(e.r-n.r)<=t&&Math.abs(e.g-n.g)<=t&&Math.abs(e.b-n.b)<=t&&Math.abs(e.a-n.a)<=t}function Bo(e){let n=o=>Math.round(Math.max(0,Math.min(255,o))).toString(16).padStart(2,"0");if(e.a>=255)return`#${n(e.r)}${n(e.g)}${n(e.b)}`;let t=Math.round(Math.max(0,Math.min(255,e.a))/255*1e3)/1e3;return`rgba(${Math.round(e.r)}, ${Math.round(e.g)}, ${Math.round(e.b)}, ${t})`}function Xc(e){if(!e)return null;let n=e.trim().toLowerCase(),t=n.match(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);if(t){let s=t[1],i=c=>{let a=s.length<=4?s[c]+s[c]:s.slice(c*2,c*2+2);return parseInt(a,16)};return{r:i(0),g:i(1),b:i(2),a:s.length===4||s.length===8?i(3):255}}let o=n.match(/^rgba?\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)(?:\s*,\s*([0-9.]+)\s*)?\)$/);if(!o)return null;let r=o[4]===void 0?255:Number(o[4])<=1?Number(o[4])*255:Number(o[4]);return{r:Number(o[1]),g:Number(o[2]),b:Number(o[3]),a:r}}function i0(){return{min:{r:255,g:255,b:255,a:255},max:{r:0,g:0,b:0,a:0},sum:{r:0,g:0,b:0,a:0},colors:new Map,count:0}}function Yc(e){return`${e.r},${e.g},${e.b},${e.a}`}function Uc(e,n){e.min.r=Math.min(e.min.r,n.r),e.min.g=Math.min(e.min.g,n.g),e.min.b=Math.min(e.min.b,n.b),e.min.a=Math.min(e.min.a,n.a),e.max.r=Math.max(e.max.r,n.r),e.max.g=Math.max(e.max.g,n.g),e.max.b=Math.max(e.max.b,n.b),e.max.a=Math.max(e.max.a,n.a),e.sum.r+=n.r,e.sum.g+=n.g,e.sum.b+=n.b,e.sum.a+=n.a;let t=Yc(n),o=e.colors.get(t);e.colors.set(t,{color:n,count:(o?.count??0)+1}),e.count++}function Gc(e){return{r:e.sum.r/e.count,g:e.sum.g/e.count,b:e.sum.b/e.count,a:e.sum.a/e.count}}function a0(e){if(e.count<Kg||e.colors.size<=1||e.colors.size>Wg)return null;let n=null;for(let r of e.colors.values())(!n||r.count>n.count)&&(n=r);if(!n)return null;let t=e.count-n.count,o=Math.max(1,Math.floor(e.count*jg));return t<=o?n.color:null}function c0(e,n,t,o){let r=$c(e);if(r.length===0)return null;let s=i0(),i=e.textureAlphaMode==="opaque"?Xc(e.color):null;for(let c of r)for(let a of s0(c.uvs)){let l=o0(n,a,e.textureWrap);if(!l)return null;if(e.textureAlphaMode==="opaque"&&l.a===0&&e.color){i&&Uc(s,{...i,a:255});continue}e.textureAlphaMode==="opaque"&&(l.a=255),Uc(s,l)}if(s.count===0)return e.textureAlphaMode==="opaque"&&e.color?e.color:null;if(e.textureAlphaMode==="opaque"&&s.max.a===0&&e.color)return e.color;if(vs(s.min,s.max,t))return Bo(Gc(s));if(!o){let c=a0(s);if(c)return Bo(c)}return o||!n.lowDetail||!vs(s.min,s.max,Yg)?null:Bo(Gc(s))}function Wc(e,n){let{texture:t,textureWrap:o,textureAlphaMode:r,material:s,uvs:i,textureTriangles:c,...a}=e,l=e.uvs?.length===e.vertices.length?e.uvs.map(d=>`uv:${Math.round(d[0]*1e5)},${Math.round(d[1]*1e5)}`):void 0,u=l??e.simplifyVertexKeys,f=e.simplifySourceVertexKeys?.length===e.vertices.length?e.simplifySourceVertexKeys.map((d,m)=>l?`${d}|${l[m]}`:d):e.simplifySourceVertexKeys;return{...a,color:n,...u?{simplifyVertexKeys:u}:{},...f?{simplifySourceVertexKeys:f}:{}}}function l0(e,n){if(n.length<2)return e;let t=[],o=new Map;for(let l of n){let u=Xc(e[l]?.color);if(!u||u.a<255)continue;let f=Yc(u);t.push({index:l,key:f});let d=o.get(f);d?d.count+=1:o.set(f,{color:u,count:1})}if(o.size<2)return e;let r=[],s=new Map,i=[...o.entries()].sort((l,u)=>u[1].count-l[1].count||l[0].localeCompare(u[0]));for(let[l,u]of i){let f=null;for(let d of r)if(vs(u.color,d,Qg)){f=d;break}f||(f=u.color,r.push(f)),s.set(l,Bo(f))}let c=!1,a=e.slice();for(let l of t){let u=s.get(l.key),f=e[l.index];!u||!f||f.color===u||(a[l.index]={...f,color:u},c=!0)}return c?a:e}function u0(e,n){let t=e.slice();for(let o=0;o<n.length;o++){let r=n[o],s=e[r.index];s&&(t[r.index]=Wc(s,r.color))}return t}function jc(e){return e?e[zc]:void 0}async function Kc(e,n={}){if(n.enabled===!1)return null;let t=Jg();if(!t)return null;let o=e.filter(a=>Ts(a)&&$c(a).length>0);if(o.length===0)return null;let r=n.maxTexturePixels??$g,s=new Map;await Promise.all(Array.from(new Set(o.map(a=>Ts(a)))).map(async a=>{s.set(a,await n0(a,t,r))}));let i=n.colorTolerance??Xg,c=n.colorTolerance!==void 0;return{bake(a){let l=!1,u=[],f=a.map((d,m)=>{let p=Ts(d);if(!p)return d;let h=s.get(p);if(!h)return d;let g=c0(d,h,i,c);return g?(l=!0,u.push(m),Wc(d,g)):d});return l?{polygons:l0(f,u),changed:!0,bakedIndices:u}:{polygons:a,changed:!1,bakedIndices:u}}}}async function f0(e,n={}){let t=await Kc(e,n);return t?t.bake(e).polygons:e}async function Is(e,n={}){let t=await Kc(e.polygons,n);if(!t)return e;let o=t.bake(e.polygons);if(!o.changed)return e;let r=[];for(let c of o.bakedIndices){let l=o.polygons[c].color;l&&r.push({index:c,color:l})}let s=e.animation,i=s&&{...s,sample(c,a){return u0(s.sample(c,a),r)}};if(i&&s){let c=s[Nc];if(c){let a=new Map(r.map(f=>[f.index,f.color])),l=[],u={polygonCount:0,vertices:new Float64Array(0),colors:l,solidTriangles:!1};i[Nc]=(f,d)=>{let m=c(f,d);if(!m)return m;l.length<m.polygonCount&&(l=new Array(m.polygonCount),u.colors=l);let p=m.textureFlags,h=m.solidTriangles===!0||!!p;for(let g=0;g<m.polygonCount;g++){let b=a.get(g);l[g]=b??m.colors?.[g],p?.[g]&&!b&&(h=!1)}return u.polygonCount=m.polygonCount,u.vertices=m.vertices,u.textureFlags=p,u.solidTriangles=h,u}}i[zc]={source:s,bakedColorEntries:r}}return{...e,polygons:o.polygons,animation:i}}var Hc=Symbol.for("polycss.animation.triangleFrameSource");function m0(e,n){let t=e[n];if(!t)return null;let o=[];for(let r=0;r<t.vertices.length;r++)o.push(n,r);return{rest:t,originalIndex:n,sourceRefs:o}}function d0(e){let n=fn(e);if(n.length>=e.length)return[];let t=new Map;e.forEach((r,s)=>t.set(r,s));let o=[];for(let r of n){let s=t.get(r);if(s===void 0)return[];let i=m0(e,s);if(!i)return[];o.push(i)}return o}function _s(e,n,t=!1){let o=new Array(n.length);for(let r=0;r<n.length;r++){let s=n[r],i=s.sourceRefs,c=new Array(i.length/2);for(let l=0,u=0;l<i.length;l+=2,u++){let d=e[i[l]]?.vertices[i[l+1]];if(!d)return e;let m=d[0],p=d[1],h=d[2];if(!Number.isFinite(m)||!Number.isFinite(p)||!Number.isFinite(h))return e;c[u]=[m,p,h]}let a=t?s.rest.color:e[i[0]??-1]?.color??s.rest.color;o[r]={...s.rest,vertices:c,color:a}}return o}function p0(e,n){let t=new Map;return n.forEach((o,r)=>{t.set(o,r)}),e.map((o,r)=>({...o,sourceRefs:o.sourceRefs.map((s,i)=>i%2===0?t.get(s)??-1:s)}))}function g0(e,n){if(e.length<n.length)return null;let t=new Array(n.length);for(let o=0;o<n.length;o++){let r=e[o],s=n[o];if(!r||r.vertices.length!==s.rest.vertices.length)return null;t[o]={...s.rest,vertices:r.vertices,color:s.rest.color}}return t}function h0(e){return!!(e.texture||e.material?.texture)}function b0(e,n={}){if(!e.animation||e.polygons.length===0||n.meshResolution==="lossless")return e;let t=d0(e.polygons);if(t.length===0||t.length>=e.polygons.length)return e;let o=jc(e.animation),r=o?.source??e.animation,s=t.map(y=>y.originalIndex),i=Rc(r,s),c=i??r,a=i?p0(t,s):t,l=t.map(y=>y.rest.color),u=t.every(y=>y.rest.vertices.length===3&&!h0(y.rest)),f=i&&!o&&t.length===s.length&&t.every((y,x)=>y.rest.vertices.length===3&&y.sourceRefs.length===6&&y.sourceRefs[0]===s[x]&&y.sourceRefs[2]===s[x]&&y.sourceRefs[4]===s[x]),d=c[Hc],m=new Float64Array(t.length*9),p=l.slice(),h=new Array(t.length).fill(!1),g={polygonCount:t.length,vertices:m,colors:p,textureFlags:h,solidTriangles:u},b={...e.animation,sample(y,x){let P=c.sample(y,x);return f?P:i&&o?u&&t.length===s.length?g0(P,t)??_s(P,a,!0):_s(P,a,!0):_s(P,a,!!o)}};return d&&u&&(b[Hc]=(y,x)=>{let P=d(y,x);if(!P)return P;if(i)return P.polygonCount<t.length||P.vertices.length<t.length*9?null:(g.polygonCount=t.length,g.vertices=P.vertices,g.colors=o?l:P.colors,g.textureFlags=o?h:P.textureFlags,g.solidTriangles=o?u:P.solidTriangles===!0,g);m.length<t.length*9&&(m=new Float64Array(t.length*9),g.vertices=m),p.length<t.length&&(p=new Array(t.length),g.colors=p),h.length<t.length&&(h=new Array(t.length).fill(!1),g.textureFlags=h);let S=u&&P.solidTriangles!==!1;for(let C=0;C<a.length;C++){let T=a[C],A=T.sourceRefs;for(let v=0,I=0;v<A.length;v+=2,I++){let _=A[v],O=A[v+1];if(_<0||_>=P.polygonCount)return null;let V=_*9+O*3,w=C*9+I*3;if(V+2>=P.vertices.length)return null;m[w]=P.vertices[V],m[w+1]=P.vertices[V+1],m[w+2]=P.vertices[V+2]}let M=A[0],E=!!P.textureFlags?.[M];h[C]=E,p[C]=o?T.rest.color:P.colors?.[M]??T.rest.color,!o&&E&&(S=!1)}return g.polygonCount=t.length,g.vertices=m,g.colors=p,g.textureFlags=h,g.solidTriangles=o?u:S,g}),{...e,polygons:t.map(y=>y.rest),animation:b,metadata:{...e.metadata,triangleCount:t.length}}}var y0=/^[0-9A-Fa-f]{6}$/,x0=["#3b82f6","#ef4444","#22c55e","#eab308","#a855f7","#06b6d4","#f97316","#ec4899"];function P0(e){let n=[],t="";for(let o of e.split(`
2
+ `)){let r=o.endsWith("\r")?o.slice(0,-1):o;if(r.endsWith("\\")){t+=`${r.slice(0,-1).trimEnd()} `;continue}n.push(t+r),t=""}return t&&n.push(t.trimEnd()),n}function Os(e,n){let t=n?.targetSize??60,o=n?.defaultColor??"#888888",r=n?.palette??x0,s=n?.materialColors??{},i=n?.materialTextures??{},c=[],a=[],l=[],u=[],f=new Map,d=[],m=new Set,p=o,h,g=n?.includeObjects?new Set(n.includeObjects):null,b=n?.excludeObjects?new Set(n.excludeObjects):null,y=null,x=()=>y===null?g===null:!(g&&!g.has(y)||b&&b.has(y)),P=U=>U in s?s[U]:y0.test(U)?`#${U}`:(f.has(U)||(f.set(U,r[u.length%r.length]),u.push(U)),f.get(U)),S=(U,$)=>{m.has(U)||(m.add(U),d.push($))},C=(U,$)=>{let k=parseInt(U,10);return Number.isFinite(k)?k<0?$+k:k-1:NaN},T=P0(e);for(let U of T)if(!(U.length===0||U.charCodeAt(0)===35)){if(U.startsWith("v ")){let $=U.trim().split(/\s+/);c.push([parseFloat($[1]),parseFloat($[2]),parseFloat($[3])])}else if(U.startsWith("vt ")){let $=U.trim().split(/\s+/);a.push([parseFloat($[1]),parseFloat($[2])])}else if(U.startsWith("o "))y=U.trim().slice(2).trim();else if(U.startsWith("usemtl ")){let $=U.trim().split(/\s+/)[1];p=P($),h=i[$]}else if(U.startsWith("p "))x()&&S("unsupported-point-elements","Skipped OBJ point elements; PolyCSS only renders face polygons");else if(U.startsWith("l "))x()&&S("unsupported-line-elements","Skipped OBJ line elements; PolyCSS only renders face polygons");else if(U.startsWith("f ")){if(!x())continue;let $=U.trim().split(/\s+/).slice(1),k=[],L=[];for(let W of $){let Y=W.split("/");k.push(C(Y[0],c.length));let se=Y[1];if(se&&se.length>0){let Z=C(se,a.length);L.push(Number.isFinite(Z)?Z:null)}else L.push(null)}l.push({idx:k,uvIdx:L,color:p,texture:h})}}if(c.length===0||l.length===0)return M0(u,e.length,d);let A=new Set;for(let U of l)for(let $ of U.idx)A.add($);let M=1/0,E=1/0,v=1/0,I=-1/0,_=-1/0,O=-1/0;for(let U of A){let $=c[U];$&&($[0]<M&&(M=$[0]),$[0]>I&&(I=$[0]),$[1]<E&&(E=$[1]),$[1]>_&&(_=$[1]),$[2]<v&&(v=$[2]),$[2]>O&&(O=$[2]))}let V=Math.max(I-M,_-E,O-v),w=V>0?t/V:1,F=n?.center,D=F===!0||F==="center",H=D?(M+I)*.5:M,K=D?(E+_)*.5:E,Q=D?(v+O)*.5:v,ne=U=>Math.round(U*1e3)/1e3,ee=c.map(([U,$,k])=>[ne((k-Q)*w),ne((U-H)*w),ne(($-K)*w)]),ue=[];for(let{idx:U,uvIdx:$,color:k,texture:L}of l)for(let W=1;W<U.length-1;W++){let Y=U[0],se=U[W],Z=U[W+1],B=ee[Y],q=ee[se],me=ee[Z];if(!B||!q||!me||B[0]===q[0]&&B[1]===q[1]&&B[2]===q[2]||B[0]===me[0]&&B[1]===me[1]&&B[2]===me[2]||q[0]===me[0]&&q[1]===me[1]&&q[2]===me[2])continue;let fe;if(L){let te=$[0],ce=$[W],be=$[W+1];if(te!=null&&ce!=null&&be!=null){let he=a[te],ye=a[ce],ge=a[be];he&&ye&&ge&&(fe=[he,ye,ge])}}let G={vertices:[B,q,me],color:k};L&&(G.texture=L),fe&&(G.uvs=fe),ue.push(G)}return{polygons:ue,objectUrls:[],dispose:()=>{},warnings:d,metadata:{triangleCount:ue.length,materials:u,sourceBytes:e.length}}}function M0(e,n,t=[]){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:t,metadata:{triangleCount:0,materials:e,sourceBytes:n}}}var Vs=e=>Math.round(Math.max(0,Math.min(1,e))*255).toString(16).padStart(2,"0");function Rs(e){let n={},t={},o=null;for(let r of e.split(`
3
+ `)){let s=r.trim();if(!(s.length===0||s.charCodeAt(0)===35)){if(s.startsWith("newmtl ")){o=s.slice(7).trim();continue}if(o){if(s.startsWith("Kd ")){let i=s.split(/\s+/),c=parseFloat(i[1]),a=parseFloat(i[2]),l=parseFloat(i[3]);Number.isFinite(c)&&Number.isFinite(a)&&Number.isFinite(l)&&(n[o]=`#${Vs(c)}${Vs(a)}${Vs(l)}`)}else if(s.startsWith("map_Kd ")){let i=s.split(/\s+/),c=i[i.length-1]?.replace(/\\+/g,"/");c&&c!=="map_Kd"&&(t[o]=c)}}}}return{colors:n,textures:t}}var A0=[0,4294967295,4291624959,4288282623,4284940287,4281597951,4278255615,4294954239,4291611903,4288269567,4284927231,4281584895,4278242559,4294941183,4291598847,4288256511,4284914175,4281571839,4278229503,4294928127,4291585791,4288243455,4284901119,4281558783,4278216447,4294915071,4291572735,4288230399,4284888063,4281545727,4278203391,4294902015,4291559679,4288217343,4284875007,4281532671,4278190335,4294967244,4291624908,4288282572,4284940236,4281597900,4278255564,4294954188,4291611852,4288269516,4284927180,4281584844,4278242508,4294941132,4291598796,4288256460,4284914124,4281571788,4278229452,4294928076,4291585740,4288243404,4284901068,4281558732,4278216396,4294915020,4291572684,4288230348,4284888012,4281545676,4278203340,4294901964,4291559628,4288217292,4284874956,4281532620,4278190284,4294967193,4291624857,4288282521,4284940185,4281597849,4278255513,4294954137,4291611801,4288269465,4284927129,4281584793,4278242457,4294941081,4291598745,4288256409,4284914073,4281571737,4278229401,4294928025,4291585689,4288243353,4284901017,4281558681,4278216345,4294914969,4291572633,4288230297,4284887961,4281545625,4278203289,4294901913,4291559577,4288217241,4284874905,4281532569,4278190233,4294967142,4291624806,4288282470,4284940134,4281597798,4278255462,4294954086,4291611750,4288269414,4284927078,4281584742,4278242406,4294941030,4291598694,4288256358,4284914022,4281571686,4278229350,4294927974,4291585638,4288243302,4284900966,4281558630,4278216294,4294914918,4291572582,4288230246,4284887910,4281545574,4278203238,4294901862,4291559526,4288217190,4284874854,4281532518,4278190182,4294967091,4291624755,4288282419,4284940083,4281597747,4278255411,4294954035,4291611699,4288269363,4284927027,4281584691,4278242355,4294940979,4291598643,4288256307,4284913971,4281571635,4278229299,4294927923,4291585587,4288243251,4284900915,4281558579,4278216243,4294914867,4291572531,4288230195,4284887859,4281545523,4278203187,4294901811,4291559475,4288217139,4284874803,4281532467,4278190131,4294967040,4291624704,4288282368,4284940032,4281597696,4278255360,4294953984,4291611648,4288269312,4284926976,4281584640,4278242304,4294940928,4291598592,4288256256,4284913920,4281571584,4278229248,4294927872,4291585536,4288243200,4284900864,4281558528,4278216192,4294914816,4291572480,4288230144,4284887808,4281545472,4278203136,4294901760,4291559424,4288217088,4284874752,4281532416,4278190318,4278190301,4278190267,4278190250,4278190216,4278190199,4278190165,4278190148,4278190114,4278190097,4278251008,4278246656,4278237952,4278233600,4278224896,4278220544,4278211840,4278207488,4278198784,4278194432,4293787648,4292673536,4290445312,4289331200,4287102976,4285988864,4283760640,4282646528,4280418304,4279304192,4293848814,4292730333,4290493371,4289374890,4287137928,4286019447,4283782485,4282664004,4280427042,4279308561];function S0(e){let n=e>>0&255,t=e>>8&255,o=e>>16&255;return[n,t,o]}function Ls(e){return(e&255).toString(16).padStart(2,"0")}function Qc(e,n,t){return`#${Ls(e)}${Ls(n)}${Ls(t)}`}function C0(e,n,t,o){if(o>=255)return Qc(e,n,t);let r=Math.round(Math.max(0,Math.min(255,o))/255*1e3)/1e3;return`rgba(${e}, ${n}, ${t}, ${r})`}function Jc(e){let n=e.match(/^#([0-9a-f]{6})$/i);if(n){let o=n[1];return{rgb:[parseInt(o.slice(0,2),16),parseInt(o.slice(2,4),16),parseInt(o.slice(4,6),16)],alpha:1}}let t=e.match(/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/i);return t?{rgb:[Number(t[1]),Number(t[2]),Number(t[3])],alpha:Number(t[4])}:null}function el(e,n){return Math.hypot(e.rgb[0]-n.rgb[0],e.rgb[1]-n.rgb[1],e.rgb[2]-n.rgb[2])}function Zc(e){let n=e.rgb[0]/255,t=e.rgb[1]/255,o=e.rgb[2]/255,r=Math.max(n,t,o),s=Math.min(n,t,o),i=r-s,c=0;return i!==0&&(r===n?c=60*((t-o)/i%6):r===t?c=60*((o-n)/i+2):c=60*((n-t)/i+4)),c<0&&(c+=360),{hue:c,saturation:r===0?0:i/r,value:r}}function E0(e,n){let t=Math.abs(e-n)%360;return t>180?360-t:t}function nl(e,n){if(e.alpha<1||n.alpha<1)return!1;let t=Zc(e),o=Zc(n),r=t.saturation<.08,s=o.saturation<.08;if(r||s)return r===s;let i=Math.min(t.value,o.value)<.16?35:24;return E0(t.hue,o.hue)<=i}function T0(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return null;let o=new Map;for(let c of e.keys()){let a=Jc(c);a&&o.set(c,a)}let r=[],s=new Map,i=Array.from(e.entries()).sort((c,a)=>a[1]-c[1]||c[0].localeCompare(a[0]));for(let[c]of i){let a=o.get(c);if(!a){s.set(c,c);continue}let l=null;for(let u of r){if(!nl(a,u.parsed))continue;let f=el(a,u.parsed);f>t||(!l||f<l.distance)&&(l={color:u.color,distance:f})}l?s.set(c,l.color):(r.push({color:c,parsed:a}),s.set(c,c))}return s}var v0=[[1,0],[-1,0],[0,1],[0,-1]];function tl(e,n){return`${e},${n}`}function I0(e){let n=e.indexOf(",");return[Number(e.slice(0,n)),Number(e.slice(n+1))]}function _0(e,n,t){return e<=4||(n===1||t===1)&&e<=12?!0:Math.min(n,t)<=2&&Math.max(n,t)>=4&&e<=18}function O0(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return;let o=new Map,r=s=>(o.has(s)||o.set(s,Jc(s)),o.get(s)??null);for(let s of e.values()){if(s.size<3)continue;let i=new Map;for(let l of s.values())i.set(l,(i.get(l)??0)+1);let c=new Set,a=[];for(let[l,u]of s){if(c.has(l))continue;let f=[l],d=[],m=new Map,p=1/0,h=-1/0,g=1/0,b=-1/0;for(c.add(l);f.length>0;){let C=f.pop();d.push(C);let[T,A]=I0(C);p=Math.min(p,T),h=Math.max(h,T),g=Math.min(g,A),b=Math.max(b,A);for(let[M,E]of v0){let v=tl(T+M,A+E),I=s.get(v);if(I){if(I===u){c.has(v)||(c.add(v),f.push(v));continue}m.set(I,(m.get(I)??0)+1)}}}let y=h-p+1,x=b-g+1;if(!_0(d.length,y,x)||m.size===0)continue;let P=r(u);if(!P)continue;let S=null;for(let[C,T]of m){let A=i.get(C)??0;if(A<=d.length)continue;let M=r(C);if(!M||!nl(P,M))continue;let E=el(P,M);if(E>t)continue;let v={color:C,contacts:T,distance:E,count:A};(!S||v.contacts>S.contacts||v.contacts===S.contacts&&v.count>S.count||v.contacts===S.contacts&&v.count===S.count&&v.distance<S.distance||v.contacts===S.contacts&&v.count===S.count&&v.distance===S.distance&&v.color.localeCompare(S.color)<0)&&(S=v)}if(S)for(let C of d)a.push([C,S.color])}for(let[l,u]of a)s.set(l,u)}}var V0=542658390,R0=new Set(["nTRN","nGRP","nSHP"]),L0="Skipped MagicaVoxel scene graph transforms; models were flattened into one grid";function ws(e,n){let t=n?.targetSize??60,o=e.byteLength;if(e.byteLength<8)return dt(o,["parseVox: buffer too small to be a valid .vox file"]);let r=new DataView(e);if(r.getUint32(0,!0)!==V0)return dt(o,["parseVox: not a .vox file (bad magic)"]);if(e.byteLength<20)return dt(o,["parseVox: buffer too small for MAIN chunk"]);if(qc(r,8)!=="MAIN")return dt(o,["parseVox: expected MAIN chunk at offset 8"]);let a=20+r.getUint32(12,!0),l=a+r.getUint32(16,!0),u=[],f=[],d=[],m=null,p=k=>{d.includes(k)||d.push(k)};for(;a<l&&a+12<=e.byteLength;){let k=qc(r,a),L=r.getUint32(a+4,!0),W=r.getUint32(a+8,!0),Y=a+12,se=Y+L+W;if(k==="SIZE"){if(L>=12&&Y+12<=e.byteLength){let Z=r.getUint32(Y,!0),B=r.getUint32(Y+4,!0),q=r.getUint32(Y+8,!0);u.push({sx:Z,sy:B,sz:q})}}else if(k==="XYZI"){if(L>=4&&Y+4<=e.byteLength){let Z=r.getUint32(Y,!0),B=[],q=Math.min(Z,Math.floor((L-4)/4));for(let me=0;me<q;me++){let fe=Y+4+me*4;B.push({x:r.getUint8(fe),y:r.getUint8(fe+1),z:r.getUint8(fe+2),colorIndex:r.getUint8(fe+3)})}f.push(B)}}else if(k==="RGBA"&&L>=1024&&Y+1024<=e.byteLength){m=[];for(let Z=0;Z<256;Z++){let B=Y+Z*4,q=r.getUint8(B),me=r.getUint8(B+1),fe=r.getUint8(B+2),G=r.getUint8(B+3);m.push(C0(q,me,fe,G))}}R0.has(k)&&p(L0),a=se}let h=[],g=new Set;for(let k of f)for(let L of k){if(L.colorIndex===0)continue;let W=`${L.x},${L.y},${L.z}`;g.has(W)||(g.add(W),h.push(L))}if(h.length===0)return dt(o,d);let b=new Map,y=k=>{let L=b.get(k);if(L)return L;let W=k-1,Y;if(m!==null)Y=m[W]??"#888888";else{let se=A0[k]??0,[Z,B,q]=S0(se);Y=Qc(Z,B,q)}return b.set(k,Y),Y},x=new Map;for(let k of h){let L=y(k.colorIndex);x.set(L,(x.get(L)??0)+1)}let P=T0(x,n?.paletteMergeDistance),S=k=>{let L=y(k);return P?.get(L)??L},C=(k,L,W)=>g.has(`${k},${L},${W}`),T=new Map,A=(k,L,W,Y,se)=>{let Z=`${k}:${L}`,B=T.get(Z);B||(B=new Map,T.set(Z,B)),B.set(tl(W,Y),se)};for(let k of h){let{x:L,y:W,z:Y}=k,se=S(k.colorIndex);C(L+1,W,Y)||A(0,L+1,W,Y,se),C(L-1,W,Y)||A(1,L,W,Y,se),C(L,W+1,Y)||A(2,W+1,L,Y,se),C(L,W-1,Y)||A(3,W,L,Y,se),C(L,W,Y+1)||A(4,Y+1,L,W,se),C(L,W,Y-1)||A(5,Y,L,W,se)}O0(T,n?.colorRegionMergeDistance);let M=new Map;for(let[k,L]of T)for(let[W,Y]of L){let se=`${k}:${Y}`,Z=M.get(se);Z||(Z=new Set,M.set(se,Z)),Z.add(W)}function E(k){let L=new Set,W=[],Y=1/0,se=-1/0,Z=1/0,B=-1/0;for(let q of k){let[me,fe]=q.split(","),G=+me,te=+fe;G<Y&&(Y=G),G>se&&(se=G),te<Z&&(Z=te),te>B&&(B=te)}for(let q=Z;q<=B;q++)for(let me=Y;me<=se;me++){let fe=`${me},${q}`;if(!k.has(fe)||L.has(fe))continue;let G=1;for(;me+G<=se;){let ce=`${me+G},${q}`;if(!k.has(ce)||L.has(ce))break;G++}let te=1;e:for(;q+te<=B;){for(let ce=0;ce<G;ce++){let be=`${me+ce},${q+te}`;if(!k.has(be)||L.has(be))break e}te++}for(let ce=0;ce<te;ce++)for(let be=0;be<G;be++)L.add(`${me+be},${q+ce}`);W.push({u:me,v:q,w:G,h:te})}return W}let v=(k,L,W,Y,se,Z)=>{let B=W+se,q=Y+Z;switch(k){case 0:return[[L,W,Y],[L,B,Y],[L,B,q],[L,W,q]];case 1:return[[L,B,Y],[L,W,Y],[L,W,q],[L,B,q]];case 2:return[[W,L,Y],[W,L,q],[B,L,q],[B,L,Y]];case 3:return[[B,L,Y],[B,L,q],[W,L,q],[W,L,Y]];case 4:return[[W,Y,L],[B,Y,L],[B,q,L],[W,q,L]];default:return[[W,q,L],[B,q,L],[B,Y,L],[W,Y,L]]}},I=[];for(let[k,L]of M){let W=k.indexOf(":"),Y=k.indexOf(":",W+1),se=+k.slice(0,W),Z=+k.slice(W+1,Y),B=k.slice(Y+1);for(let{u:q,v:me,w:fe,h:G}of E(L))I.push({vertices:v(se,Z,q,me,fe,G),color:B})}if(I.length===0)return dt(o,d);let _=1/0,O=1/0,V=1/0,w=-1/0,F=-1/0,D=-1/0;for(let k of I)for(let L of k.vertices)L[0]<_&&(_=L[0]),L[0]>w&&(w=L[0]),L[1]<O&&(O=L[1]),L[1]>F&&(F=L[1]),L[2]<V&&(V=L[2]),L[2]>D&&(D=L[2]);let H=Math.max(w-_,F-O,D-V),K=H>0?t/H:1,Q=K*50,ne=Number.isFinite(Q)&&Q>0?Math.max(1,Math.round(Q))/50:K,ee={kind:"magica-vox",cells:h.map(k=>({x:F-(k.y+1),y:k.x-_,z:k.z-V,color:S(k.colorIndex)})),rows:Math.max(0,F-O),cols:Math.max(0,w-_),depth:Math.max(0,D-V),scale:ne,sourceBytes:o},ue=k=>Math.round(k*1e3)/1e3,U=k=>[ue((k[0]-_)*ne),ue((k[1]-O)*ne),ue((k[2]-V)*ne)],$=I.map(({vertices:k,color:L})=>({vertices:k.map(U),color:L}));return{polygons:$,voxelSource:ee,objectUrls:[],dispose:()=>{},warnings:d,metadata:{triangleCount:$.length,sourceBytes:o,voxelCount:h.length}}}function qc(e,n){return String.fromCharCode(e.getUint8(n))+String.fromCharCode(e.getUint8(n+1))+String.fromCharCode(e.getUint8(n+2))+String.fromCharCode(e.getUint8(n+3))}function dt(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:n,metadata:{triangleCount:0,sourceBytes:e}}}var w0="#888888";var N0=/^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:[eE][+-]?\d+)?$/,kn="COLOR=";function sl(e,n,t){let o=n??t;if(!Number.isFinite(o))throw new Error(`parseStl: ${e} must be finite`);return o}function D0(e,n,t){let o=sl(e,n,t);if(o<=0)throw new Error(`parseStl: ${e} must be greater than 0`);return o}function F0(e){if(e===void 0||e==="z"||e==="y")return e??"z";throw new Error('parseStl: upAxis must be "z" or "y"')}function B0(e){let n=e??w0;if(typeof n!="string"||n.trim().length===0)throw new Error("parseStl: defaultColor must be a non-empty string");return n}function k0(e){if(typeof e=="string")return null;if(e instanceof Uint8Array)return new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength));if(e instanceof ArrayBuffer)return new Uint8Array(e);throw new Error("parseStl: source must be an ArrayBuffer, Uint8Array, or ASCII string")}function il(e){let n=globalThis.TextDecoder;return new n().decode(e)}function ko(e,n){return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0)]}function Uo(e){return Number.isFinite(e[0])&&Number.isFinite(e[1])&&Number.isFinite(e[2])}function ol(e){let n=e.trim().split(/\s+/);if(n.length!==3||!n.every(o=>N0.test(o)))return null;let t=[Number(n[0]),Number(n[1]),Number(n[2])];return Uo(t)?t:null}function U0(e){let n="";for(let o of e)o!==0&&(n+=o>=32&&o<=126?String.fromCharCode(o):" ");return n.replace(/\s+/g," ").trim()||void 0}function Ns(e){return Math.max(0,Math.min(255,e)).toString(16).padStart(2,"0")}function al(e,n,t,o=255){if(o>=255)return`#${Ns(e)}${Ns(n)}${Ns(t)}`;let r=Math.round(o/255*1e3)/1e3;return`rgba(${e}, ${n}, ${t}, ${r})`}function Ds(e){return Math.round(e/31*255)}function G0(e){for(let n=0;n<80-kn.length-4;n+=1){let t=!0;for(let c=0;c<kn.length;c+=1)if(e.getUint8(n+c)!==kn.charCodeAt(c)){t=!1;break}if(!t)continue;let o=e.getUint8(n+kn.length),r=e.getUint8(n+kn.length+1),s=e.getUint8(n+kn.length+2),i=e.getUint8(n+kn.length+3);return{defaultColor:al(o,r,s,i),alpha:Math.round(i/255*1e3)/1e3,alphaByte:i}}return null}function z0(e,n){if((e&32768)!==0)return{color:n.defaultColor,usesDefaultColor:!0};let t=Ds(e&31),o=Ds(e>>5&31),r=Ds(e>>10&31);return{color:al(t,o,r,n.alphaByte),usesDefaultColor:!1}}function $0(e){let n=[],t=/^\s*solid(?:\s+([^\r\n]*?))?\s*$/gim,o=/^\s*endsolid\b[^\r\n]*$/gim,r;for(;r=t.exec(e);)n.push({index:r.index,kind:"start",name:(r[1]??"").trim()});for(;r=o.exec(e);)n.push({index:r.index,kind:"end"});return n.sort((s,i)=>s.index-i.index||(s.kind==="end"?-1:1))}function X0(e){let n=Math.min(e.byteLength,1024);for(let t=84;t<n;t+=1){let o=e[t];if(o===0||o<9||o>13&&o<32||o>126)return!0}return!1}function Y0(e){let n=il(e.subarray(0,Math.min(e.byteLength,512))).trimStart().toLowerCase();return!n.startsWith("solid")&&!n.startsWith("facet")?!1:!X0(e)}function W0(e){if(e.byteLength<84)return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(80,!0),o=84+t*50;if(o===e.byteLength)return{triangleCount:t,declaredTriangleCount:t,trailingBytes:0};if(Y0(e))return null;if(o<e.byteLength)return{triangleCount:t,declaredTriangleCount:t,trailingBytes:e.byteLength-o};let r=e.byteLength-84;return r>0&&r%50===0?{triangleCount:r/50,declaredTriangleCount:t,trailingBytes:0}:null}function j0(e,n,t){let o=new DataView(e.buffer,e.byteOffset,e.byteLength),{triangleCount:r,declaredTriangleCount:s,trailingBytes:i}=n;s!==r&&t.push(`parseStl: binary STL declared ${s} triangles but contains ${r} complete triangle record${r===1?"":"s"}`),i>0&&t.push(`parseStl: ignored ${i} trailing binary byte${i===1?"":"s"}`);let c=U0(e.subarray(0,80)),a=G0(o),l=0,u=0,f=[];for(let d=0,m=84;d<r;d+=1,m+=50){let p=ko(o,m),h=[ko(o,m+12),ko(o,m+24),ko(o,m+36)];if(!h.every(Uo))throw new Error(`parseStl: non-finite coordinate in binary triangle ${d}`);let g=o.getUint16(m+48,!0),b=a?z0(g,a):null;b&&(b.usesDefaultColor?u+=1:l+=1),f.push({normal:Uo(p)?p:null,hasIgnoredNormal:!Uo(p),color:b?.color,vertices:h,hasIgnoredAttribute:!a&&g!==0})}return{format:"binary",facets:f,binaryHeader:c,color:a?{format:"magics",defaultColor:a.defaultColor,alpha:a.alpha,coloredTriangleCount:l,defaultColorTriangleCount:u}:void 0}}function rl(e,n){let t=[],o=/^\s*facet\s+normal\s+([^\r\n]+)([\s\S]*?)^\s*endfacet\b[^\r\n]*$/gim,r=/^\s*vertex\s+([^\r\n]+)/gim,s=/^\s*facet\s+normal\b/gim,i=$0(e),c=0,a,l=0;for(;s.exec(e);)l+=1;let u=0,f=0,d=0,m;for(;m=o.exec(e);){for(u+=1;c<i.length&&i[c].index<m.index;){let y=i[c];a=y.kind==="start"?y.name:void 0,c+=1}let p=ol(m[1]);p||(d+=1);let h=[],g=!1;r.lastIndex=0;let b;for(;b=r.exec(m[2]);){let y=ol(b[1]);if(!y){g=!0;break}h.push(y)}if(g||h.length!==3){f+=1;continue}t.push({normal:p,hasIgnoredNormal:!p,solidName:a,vertices:[h[0],h[1],h[2]]})}return f+=Math.max(0,l-u),f>0&&n.push(`parseStl: skipped ${f} malformed ASCII facet${f===1?"":"s"}`),d>0&&n.push(`parseStl: ignored ${d} malformed ASCII facet normal${d===1?"":"s"}`),{format:"ascii",facets:t}}function pt(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function zo(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function Fs(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function cl(e){return Math.hypot(e[0],e[1],e[2])}function K0(e){let n=zo(pt(e[1],e[0]),pt(e[2],e[0]));return cl(n)<=1e-8}function Go(e){let n=cl(e);return n<=1e-8?null:[e[0]/n,e[1]/n,e[2]/n]}function H0(e){let n=t=>Object.is(t,-0)?0:t;return`${n(e[0])},${n(e[1])},${n(e[2])}`}function Z0(e,n){return e<n?`${e}|${n}`:`${n}|${e}`}function Bs(e,n){return n?[e.vertices[0],e.vertices[2],e.vertices[1]]:e.vertices}function q0(e,n,t){let o=0;for(let r of n.facets){let[s,i,c]=Bs(e[r],t[r]);o+=Fs(s,zo(i,c))/6}return o}function Q0(e,n,t){if(n.facets.length<8)return null;let o=0,r=0;for(let i of n.facets){let c=e[i],a=c.normal?Go(c.normal):null;if(!a)continue;let l=Bs(c,t[i]),u=Go(zo(pt(l[1],l[0]),pt(l[2],l[0])));if(!u)continue;let f=Fs(u,a);f>=.95?o+=1:f<=-.95&&(r+=1)}let s=o+r;return s/n.facets.length<.75?null:r/s>=.9?"opposed":o/s>=.9?"aligned":null}function J0(e){let n=new Map,t=e.map(()=>[]);for(let u=0;u<e.length;u+=1){let f=e[u].vertices.map(H0);for(let d=0;d<3;d+=1){let m=f[d],p=f[(d+1)%3],h=Z0(m,p),g=n.get(h),b={facetIndex:u,from:m,to:p};g?g.push(b):n.set(h,[b])}}let o=0;for(let u of n.values()){if(u.length>2&&(o+=1),u.length<2)continue;let[f,...d]=u;for(let m of d){let p=f.from===m.from&&f.to===m.to;t[f.facetIndex].push({to:m.facetIndex,flipDiffers:p}),t[m.facetIndex].push({to:f.facetIndex,flipDiffers:p})}}let r=new Array(e.length).fill(!1),s=new Array(e.length).fill(-1),i=[],c=0;for(let u=0;u<e.length;u+=1){if(s[u]>=0)continue;let f=i.length,d={facets:[],conflicts:0,boundaryEdges:0,nonManifoldEdges:0,flippedOutward:!1,flippedToSuppliedNormals:!1};i.push(d),s[u]=f;let m=[u];for(let p=0;p<m.length;p+=1){let h=m[p];d.facets.push(h);for(let g of t[h]){let b=r[h]!==g.flipDiffers;s[g.to]<0?(s[g.to]=f,r[g.to]=b,m.push(g.to)):r[g.to]!==b&&h<g.to&&(d.conflicts+=1,c+=1)}}}for(let u of n.values()){let f=i[s[u[0].facetIndex]];u.length===1?f.boundaryEdges+=1:u.length>2&&(f.nonManifoldEdges+=1)}for(let u of i){if(u.nonManifoldEdges>0||u.conflicts>0){for(let d of u.facets)r[d]=!1;continue}if(u.boundaryEdges>0){if(Q0(e,u,r)==="opposed"){for(let d of u.facets)r[d]=!r[d];u.flippedToSuppliedNormals=!0}continue}if(q0(e,u,r)<-1e-10){for(let d of u.facets)r[d]=!r[d];u.flippedOutward=!0}}let a=0;return{facets:e.map((u,f)=>r[f]?(a+=1,{...u,vertices:Bs(u,!0)}):u),repairedCount:a,componentCount:i.length,outwardComponentCount:i.filter(u=>u.flippedOutward).length,suppliedNormalComponentCount:i.filter(u=>u.flippedToSuppliedNormals).length,conflictCount:c,nonManifoldEdgeCount:o}}function eh(e,n,t,o,r){let s=i=>Math.round(i*1e3)/1e3;return r==="y"?[s((e[2]-n[2])*t+o),s((e[0]-n[0])*t+o),s((e[1]-n[1])*t+o)]:[s((e[0]-n[0])*t+o),s((e[1]-n[1])*t+o),s((e[2]-n[2])*t+o)]}function nh(e){if(e.every(r=>r.solidName===void 0))return;let n=[],t=e[0]?.solidName??"",o=0;for(let r=1;r<=e.length;r+=1){let s=e[r]?.solidName??"";r<e.length&&s===t||(n.push({name:t,start:o,count:r-o}),t=s,o=r)}return n}function th(e){if(!e)return;let n=[],t=new Set;for(let o of e)!o.name||t.has(o.name)||(t.add(o.name),n.push(o.name));return n.length>0?n:void 0}function ks(e,n){let t=D0("targetSize",n?.targetSize,60),o=sl("gridShift",n?.gridShift,1),r=B0(n?.defaultColor),s=F0(n?.upAxis),i=[],c=k0(e),a=typeof e=="string"?e.length:c.byteLength;if(a===0||typeof e=="string"&&e.trim().length===0)throw new Error("parseStl: empty input");let l=c?W0(c):null,u=typeof e=="string"?rl(e,i):l?j0(c,l,i):rl(il(c),i),f=0,d=0,m=0,p=[];for(let A of u.facets){if(A.hasIgnoredAttribute&&(f+=1),A.hasIgnoredNormal&&u.format==="binary"&&(d+=1),K0(A.vertices)){m+=1;continue}p.push(A)}let h=J0(p),g=0;for(let A of h.facets){let M=Go(zo(pt(A.vertices[1],A.vertices[0]),pt(A.vertices[2],A.vertices[0]))),E=A.normal?Go(A.normal):null;M&&E&&Fs(M,E)<.95&&(g+=1)}if(f>0&&i.push(`parseStl: ignored non-zero binary attribute byte count on ${f} triangle${f===1?"":"s"}`),d>0&&i.push(`parseStl: ignored ${d} non-finite binary normal${d===1?"":"s"}`),m>0&&i.push(`parseStl: skipped ${m} degenerate triangle${m===1?"":"s"}`),h.repairedCount>0&&i.push(`parseStl: repaired winding on ${h.repairedCount} triangle${h.repairedCount===1?"":"s"}`),h.outwardComponentCount>0&&i.push(`parseStl: oriented ${h.outwardComponentCount} closed component${h.outwardComponentCount===1?"":"s"} outward`),h.suppliedNormalComponentCount>0&&i.push(`parseStl: oriented ${h.suppliedNormalComponentCount} open component${h.suppliedNormalComponentCount===1?"":"s"} from supplied normals`),h.conflictCount>0&&i.push(`parseStl: found ${h.conflictCount} inconsistent shared-edge winding constraint${h.conflictCount===1?"":"s"}`),h.nonManifoldEdgeCount>0&&i.push(`parseStl: found ${h.nonManifoldEdgeCount} non-manifold shared edge${h.nonManifoldEdgeCount===1?"":"s"}`),g>0&&i.push(`parseStl: ${g} supplied normal${g===1?" disagrees":"s disagree"} with triangle winding`),p.length===0)throw u.facets.length===0?new Error(`parseStl: no valid ${u.format} facets`):new Error("parseStl: no valid facets after filtering");let b=[1/0,1/0,1/0],y=[-1/0,-1/0,-1/0];for(let A of h.facets)for(let M of A.vertices)for(let E=0;E<3;E+=1)M[E]<b[E]&&(b[E]=M[E]),M[E]>y[E]&&(y[E]=M[E]);let x=Math.max(y[0]-b[0],y[1]-b[1],y[2]-b[2]),P=x>0?t/x:1,S=nh(h.facets),C=th(S),T=h.facets.map(A=>({vertices:A.vertices.map(M=>eh(M,b,P,o,s)),color:A.color??r}));return{polygons:T,objectUrls:[],dispose:()=>{},warnings:i,metadata:{triangleCount:T.length,meshes:C,materials:[],sourceBytes:a,stlHeader:u.binaryHeader,stlColor:u.color,stlSolids:S,stlTopology:{componentCount:h.componentCount,repairedTriangleCount:h.repairedCount,outwardComponentCount:h.outwardComponentCount,suppliedNormalComponentCount:h.suppliedNormalComponentCount,inconsistentSharedEdgeCount:h.conflictCount,nonManifoldSharedEdgeCount:h.nonManifoldEdgeCount}}}}var En="loadMesh";async function ll(e,n){let t=n?.solidTextureSamples;return t===!1?e:Is(e,typeof t=="object"?t:void 0)}function oh(e){let n=e.split("?")[0].split("#")[0],t=n.lastIndexOf(".");return t<0?"":n.slice(t+1).toLowerCase()}async function rh(e,n){let t=oh(e);if(t==="mtl")throw new Error(`${En}: .mtl is a material file, not a mesh \u2014 use parseMtl directly`);let o=globalThis.fetch;if(!o)throw new Error(`${En}: no fetch() in this environment`);let r=n?.baseUrl??e;if(t==="obj"){let s=await o(e);if(!s.ok)throw new Error(`${En}: ${e} \u2192 ${s.status}`);let i=await s.text(),c=n?.objOptions;if(n?.mtlUrl){let u=await o(n.mtlUrl);if(!u.ok)throw new Error(`${En}: ${n.mtlUrl} \u2192 ${u.status}`);let f=await u.text(),{colors:d,textures:m}=Rs(f),p={},h=globalThis.URL,g=globalThis.document?.baseURI,b=n.mtlUrl;if(h&&g)try{b=new h(n.mtlUrl,g).toString()}catch{}for(let[y,x]of Object.entries(m)){if(h)try{p[y]=new h(x,b).toString();continue}catch{}let P=n.mtlUrl.lastIndexOf("/"),S=P>=0?n.mtlUrl.slice(0,P+1):"";p[y]=x.startsWith("/")||/^https?:\/\//.test(x)?x:S+x}c={...c??{},materialColors:{...d,...c?.materialColors??{}},materialTextures:{...p,...c?.materialTextures??{}}}}let a=Os(i,c),l=await ll(a,n);return nt(l,{meshResolution:n?.meshResolution,source:a})}if(t==="glb"||t==="gltf"){let s=await o(e);if(!s.ok)throw new Error(`${En}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer(),c=Es(i,{baseUrl:r,...n?.gltfOptions??{}}),a=await ll(c,n);return nt(a,{meshResolution:n?.meshResolution,source:c})}if(t==="vox"){let s=await o(e);if(!s.ok)throw new Error(`${En}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return nt(ws(i,n?.voxOptions),{meshResolution:n?.meshResolution})}if(t==="stl"){let s=await o(e);if(!s.ok)throw new Error(`${En}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return nt(ks(i,n?.stlOptions),{meshResolution:n?.meshResolution})}throw new Error(`${En}: unsupported extension ".${t}" (supported: obj, glb, gltf, vox, stl)`)}function Ne(e,n){if(Object.is(e,0)||Object.is(e,-0))return"0";if(e===1)return"1";if(e===-1)return"-1";let t=is[n]??10**n,o=Math.round(e*t)/t;return Object.is(o,0)||Object.is(o,-0)?"0":String(o)}function sh(e,n=vo){let t=Ne(e,n);return t==="0"?"0":`${t}px`}function Tn(e,n=bn){if(e.length===0)return"";let t=Ne(e[0],n);for(let o=1;o<e.length;o++)t+=`,${Ne(e[o],n)}`;return t}function ih(e,n,t,o,r=bn){return Us(e[0],e[1],e[2],n[0],n[1],n[2],t[0],t[1],t[2],o[0],o[1],o[2],r)}function Us(e,n,t,o,r,s,i,c,a,l,u,f,d=bn){if(d===3){let m=Math.round(e*1e3)/1e3||0,p=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,g=Math.round(o*1e3)/1e3||0,b=Math.round(r*1e3)/1e3||0,y=Math.round(s*1e3)/1e3||0,x=Math.round(i*1e3)/1e3||0,P=Math.round(c*1e3)/1e3||0,S=Math.round(a*1e3)/1e3||0,C=Math.round(l*1e3)/1e3||0,T=Math.round(u*1e3)/1e3||0,A=Math.round(f*1e3)/1e3||0;return`${m},${p},${h},0,${g},${b},${y},0,${x},${P},${S},0,${C},${T},${A},1`}return`${Ne(e,d)},${Ne(n,d)},${Ne(t,d)},0,${Ne(o,d)},${Ne(r,d)},${Ne(s,d)},0,${Ne(i,d)},${Ne(c,d)},${Ne(a,d)},0,${Ne(l,d)},${Ne(u,d)},${Ne(f,d)},1`}function Gs(e,n,t,o,r,s,i,c,a,l,u,f,d=bn){if(d!==3)return`matrix3d(${Us(e,n,t,o,r,s,i,c,a,l,u,f,d)})`;let m=Math.round(e*1e3)/1e3||0,p=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,g=Math.round(o*1e3)/1e3||0,b=Math.round(r*1e3)/1e3||0,y=Math.round(s*1e3)/1e3||0,x=Math.round(i*1e3)/1e3||0,P=Math.round(c*1e3)/1e3||0,S=Math.round(a*1e3)/1e3||0,C=Math.round(l*1e3)/1e3||0,T=Math.round(u*1e3)/1e3||0,A=Math.round(f*1e3)/1e3||0;return`matrix3d(${m},${p},${h},0,${g},${b},${y},0,${x},${P},${S},0,${C},${T},${A},1)`}function zt(e,n,t,o=0,r=0){let s=e.matrix.split(",").map(d=>Number(d));if(s.length!==16||s.some(d=>!Number.isFinite(d)))return e.matrix;let i=s[0],c=s[1],a=s[2],l=s[4],u=s[5],f=s[6];return s[0]*=n,s[1]*=n,s[2]*=n,s[4]*=t,s[5]*=t,s[6]*=t,s[12]+=o*i+r*l,s[13]+=o*c+r*u,s[14]+=o*a+r*f,Tn(s)}function ah(e,n){return zt(e,n.width/yn,n.height/yn,n.minX,n.minY)}function ul(e){let n=e.seamBleedInsets??{left:0,right:0,top:0,bottom:0},t=e.canvasW||1,o=e.canvasH||1;return zt(e,(t+n.left+n.right)/ct,(o+n.top+n.bottom)/ct,-n.left,-n.top)}function zs(e,n){let t=e.matrix.split(",").map(o=>Number(o));return t.length!==16||t.some(o=>!Number.isFinite(o))?e.canonicalMatrix:(t[0]*=e.canvasW/n,t[1]*=e.canvasW/n,t[2]*=e.canvasW/n,t[4]*=e.canvasH/n,t[5]*=e.canvasH/n,t[6]*=e.canvasH/n,Tn(t))}function vn(e,n=ss){let t=Ne(e,n);return Number(t)===0?"0":`${t}%`}function ch(e,n=bn){return`matrix3d(${e.split(",").map(t=>{let o=Number(t.trim());return Number.isFinite(o)?Ne(o,n):t.trim()}).join(",")})`}function lh(e,n=vo){let t=Ne(e,n);return Number(t)===0||Object.is(Number(t),-0)?"0":`${t}px`}function uh(e){return`matrix3d(${ul(e)})`}function In(e,n,t){return e.map(o=>[o[1]*n,o[0]*n,o[2]*t])}function Un(e){if(e.length<3)return null;let n=e[0],t=0,o=0,r=0;for(let i=1;i+1<e.length;i++){let c=e[i],a=e[i+1],l=[c[0]-n[0],c[1]-n[1],c[2]-n[2]],u=[a[0]-n[0],a[1]-n[1],a[2]-n[2]];t-=l[1]*u[2]-l[2]*u[1],o-=l[2]*u[0]-l[0]*u[2],r-=l[0]*u[1]-l[1]*u[0]}let s=Math.hypot(t,o,r);return s<=de?null:(t/=s,o/=s,r/=s,[t,o,r])}function $t(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=de)return!1;let c=Math.sign(i);if(n===0)n=c;else if(c!==n)return!1}return!0}function $s(e){let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-o[1]*r[0]}return n/2}function Xs(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],c=o[1]-t[1],a=r*c-s*i;if(Math.abs(a)<=de)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*c-u*i)/a;return[e[0]+f*r,e[1]+f*s]}function fh(e,n,t,o,r,s,i,c){let a=t-e,l=o-n,u=i-r,f=c-s,d=a*f-l*u;if(Math.abs(d)<=de)return null;let m=r-e,p=s-n,h=(m*f-p*u)/d;return[e+h*a,n+h*l]}function gt(e,n){if(e.length<6||n<=0)return e;let t=0,o=0,r=e.length/2;for(let i=0;i<e.length;i+=2)t+=e[i],o+=e[i+1];t/=r,o/=r;let s=e.slice();for(let i=0;i<s.length;i+=2){let c=s[i]-t,a=s[i+1]-o,l=Math.hypot(c,a);l<=Ae||(s[i]+=c/l*n,s[i+1]+=a/l*n)}return s}function ht(e,n){if(e.length<6||e.length%2!==0||n<=0)return e;let t=[];for(let a=0;a<e.length;a+=2)t.push([e[a],e[a+1]]);if(!$t(t))return gt(e,n);let o=$s(t);if(Math.abs(o)<=de)return gt(e,n);let r=o>0?1:-1,s=[];for(let a=0;a<t.length;a++){let l=t[a],u=t[(a+1)%t.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=de)return gt(e,n);let p=r*(d/m)*n,h=r*(-f/m)*n;s.push({a:[l[0]+p,l[1]+h],b:[u[0]+p,u[1]+h]})}let i=[],c=Math.max(2,n*4);for(let a=0;a<t.length;a++){let l=s[(a+t.length-1)%t.length],u=s[a],f=Xs(l.a,l.b,u.a,u.b);if(!f)return gt(e,n);let d=t[a],m=f[0]-d[0],p=f[1]-d[1],h=Math.hypot(m,p);h>c?i.push(d[0]+m/h*c,d[1]+p/h*c):i.push(f[0],f[1])}return i}function Xt(e,n){if(e.length<6||e.length%2!==0)return e;let t=e.length/2;if(n.length!==t)return e;let o=n.map(f=>typeof f=="number"&&Number.isFinite(f)?Math.max(0,f):0),r=Math.max(0,...o);if(r<=0)return e;if(o.every(f=>Math.abs(f-o[0])<=de))return ht(e,o[0]);let s=[];for(let f=0;f<e.length;f+=2)s.push([e[f],e[f+1]]);if(!$t(s))return e;let i=$s(s);if(Math.abs(i)<=de)return e;let c=i>0?1:-1,a=[];for(let f=0;f<s.length;f++){let d=s[f],m=s[(f+1)%s.length],p=m[0]-d[0],h=m[1]-d[1],g=Math.hypot(p,h);if(g<=de)return e;let b=o[f],y=c*(h/g)*b,x=c*(-p/g)*b;a.push({a:[d[0]+y,d[1]+x],b:[m[0]+y,m[1]+x]})}let l=[],u=Math.max(2,r*4);for(let f=0;f<s.length;f++){let d=a[(f+s.length-1)%s.length],m=a[f],p=Xs(d.a,d.b,m.a,m.b);if(!p)return e;let h=s[f],g=p[0]-h[0],b=p[1]-h[1],y=Math.hypot(g,b);y>u?l.push(h[0]+g/y*u,h[1]+b/y*u):l.push(p[0],p[1])}return l}function $o(e,n,t,o,r,s,i){if(i<=0)return[e,n,t,o,r,s];let c=(e*o-n*t+t*s-o*r+r*n-s*e)/2,a=()=>gt([e,n,t,o,r,s],i);if(Math.abs(c)<=de)return a();let l=c>0?1:-1,u=t-e,f=o-n,d=Math.sqrt(u*u+f*f),m=r-t,p=s-o,h=Math.sqrt(m*m+p*p),g=e-r,b=n-s,y=Math.sqrt(g*g+b*b);if(d<=de||h<=de||y<=de)return a();let x=l*(f/d)*i,P=l*(-u/d)*i,S=e+x,C=n+P,T=t+x,A=o+P,M=l*(p/h)*i,E=l*(-m/h)*i,v=t+M,I=o+E,_=r+M,O=s+E,V=l*(b/y)*i,w=l*(-g/y)*i,F=r+V,D=s+w,H=e+V,K=n+w,Q=H-F,ne=K-D,ee=T-S,ue=A-C,U=Q*ue-ne*ee;if(Math.abs(U)<=de)return a();let $=S-F,k=C-D,L=($*ue-k*ee)/U,W=F+L*Q,Y=D+L*ne,se=T-S,Z=A-C,B=_-v,q=O-I,me=se*q-Z*B;if(Math.abs(me)<=de)return a();let fe=v-S,G=I-C,te=(fe*q-G*B)/me,ce=S+te*se,be=C+te*Z,he=_-v,ye=O-I,ge=H-F,Se=K-D,De=he*Se-ye*ge;if(Math.abs(De)<=de)return a();let R=F-v,z=D-I,j=(R*Se-z*ge)/De,J=v+j*he,le=I+j*ye,re=Math.max(2,i*4),pe=W-e,xe=Y-n,ie=Math.sqrt(pe*pe+xe*xe);ie>re&&(W=e+pe/ie*re,Y=n+xe/ie*re);let ae=ce-t,N=be-o,X=Math.sqrt(ae*ae+N*N);X>re&&(ce=t+ae/X*re,be=o+N/X*re);let oe=J-r,Te=le-s,Ce=Math.sqrt(oe*oe+Te*Te);return Ce>re&&(J=r+oe/Ce*re,le=s+Te/Ce*re),[W,Y,ce,be,J,le]}function Ys(e,n,t,o){let r=e+n;if(o<=0||t<=de||r<=de||!Number.isFinite(e+n+t+o))return $o(e,0,0,t,r,t,o);let s=Math.sqrt(e*e+t*t),i=Math.sqrt(n*n+t*t);if(s<=de||i<=de)return $o(e,0,0,t,r,t,o);let c=-o*t/s,a=-o*e/s,l=o*t/i,u=-o*n/i,f=e+c,d=a,m=r+l,p=t+u,h=-t*r;if(Math.abs(h)<=de)return $o(e,0,0,t,r,t,o);let g=f-m,b=d-p,y=(g*t+b*e)/h,x=m-y*n,P=p-y*t,S=-o*(e+s)/t,C=t+o,T=r+o*(n+i)/t,A=C,M=Math.max(2,o*4),E=x-e,v=P,I=Math.sqrt(E*E+v*v);I>M&&(x=e+E/I*M,P=v/I*M);let _=Math.sqrt(S*S+o*o);_>M&&(S=S/_*M,C=t+o/_*M);let O=T-r,V=Math.sqrt(O*O+o*o);return V>M&&(T=r+O/V*M,A=t+o/V*M),[x,P,S,C,T,A]}function fl(e,n){if(e.screenPts.length<6||n.vertices.length<3)return null;let t=e.tileSize,o=e.layerElevation,r=In(n.vertices,t,o),s=Un(r);if(!s)return null;let i=e.screenPts[0],c=e.screenPts[1],a=e.screenPts[2],l=e.screenPts[3],u=e.screenPts[4],f=e.screenPts[5],d=a-i,m=l-c,p=u-i,h=f-c,g=d*h-m*p;if(Math.abs(g)<=de)return null;let b=r[0],y=r[1],x=r[2],P=[y[0]-b[0],y[1]-b[1],y[2]-b[2]],S=[x[0]-b[0],x[1]-b[1],x[2]-b[2]],C=[(P[0]*h-m*S[0])/g,(P[1]*h-m*S[1])/g,(P[2]*h-m*S[2])/g],T=[(d*S[0]-P[0]*p)/g,(d*S[1]-P[1]*p)/g,(d*S[2]-P[2]*p)/g],A=b[0]-C[0]*i-T[0]*c,M=b[1]-C[1]*i-T[1]*c,E=b[2]-C[2]*i-T[2]*c;return{normal:s,xAxis:C,yAxis:T,tx:A,ty:M,tz:E}}function Ws(e){return Math.max(0,Math.min(6,Math.floor(e??bn)))}function ml(e){return`${e[0]},${e[1]},${e[2]}`}function dl(e,n){let t=ml(e),o=ml(n);return t<o?`${t}|${o}`:`${o}|${t}`}function mh(e){let n=new Map;for(let o=0;o<e.length;o++){let r=e[o].vertices;if(!(!r||r.length<3||!e[o].texture))for(let s=0;s<r.length;s++){let i=dl(r[s],r[(s+1)%r.length]),c=n.get(i),a={polygon:o,edge:s};c?c.push(a):n.set(i,[a])}}let t=e.map(()=>new Set);for(let o of n.values())if(!(o.length<2))for(let r=0;r<o.length;r++)for(let s=r+1;s<o.length;s++)t[o[r].polygon].add(o[r].edge),t[o[s].polygon].add(o[s].edge);return t.map(o=>o.size>0?o:void 0)}function Xo(e,n){return typeof e=="number"&&Number.isFinite(e)?Math.max(0,e):n}function js(e){return typeof e=="number"&&Number.isFinite(e)&&e>0?e:void 0}function Yo(e,n,t){if(t<=0||e.length<6||e.length%2!==0)return 0;let o=e.length/2;if(n<0||n>=o)return 0;let r=[];for(let A=0;A<e.length;A+=2)r.push([e[A],e[A+1]]);let[s,i]=r[n],[c,a]=r[(n+1)%o],l=c-s,u=a-i,f=Math.hypot(l,u);if(f<=Ae)return 0;let d=[],m=Number.POSITIVE_INFINITY;for(let A=0;A<o;A+=1){if(A===n||A===(n+1)%o)continue;let[M,E]=r[A],v=Math.abs((M-s)*u-(E-i)*l)/f;v>Ae&&(m=Math.min(m,v))}Number.isFinite(m)&&d.push(m*.5);let p=r[(n+o-1)%o],h=r[(n+2)%o],g=p[0]-s,b=p[1]-i,y=h[0]-c,x=h[1]-a,P=Math.hypot(g,b),S=Math.hypot(y,x);if(P>Ae){let A=Math.abs(l*b-u*g)/(f*P);A>Ae&&d.push(P*A)}if(S>Ae){let A=Math.abs(-l*x- -u*y)/(f*S);A>Ae&&d.push(S*A)}let C=d.filter(A=>Number.isFinite(A)&&A>0);if(C.length===0)return Number.isFinite(t)?Math.max(0,t):0;let T=Math.min(...C);return Math.max(0,Math.min(Number.isFinite(t)?t:T,T))}function Ks(e,n,t){if(!n?.size||t===void 0)return;let o=new Map;for(let r of n){let s=Yo(e,r,t);s>0&&o.set(r,s)}return o.size>0?o:void 0}function Hs(e,n){return e<3||!n?.size?null:Array.from({length:e},(t,o)=>Math.max(0,n.get(o)??0))}function Zs(e,n){if(!n?.size||e.length<6||e.length%2!==0)return;let t=e.length/2,o=Number.POSITIVE_INFINITY,r=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];o=Math.min(o,u),r=Math.min(r,f),s=Math.max(s,u),i=Math.max(i,f)}if(!Number.isFinite(o+r+s+i))return;let c=Math.max(Ae*8,.25),a={left:0,right:0,top:0,bottom:0};for(let[l,u]of n){let f=Math.max(0,u);if(f<=0||l<0||l>=t)continue;let d=l*2,m=(l+1)%t*2,p=e[d],h=e[d+1],g=e[m],b=e[m+1];Math.abs(p-g)<=c?Math.abs(p-o)<=c&&Math.abs(g-o)<=c?a.left=Math.max(a.left,f):Math.abs(p-s)<=c&&Math.abs(g-s)<=c&&(a.right=Math.max(a.right,f)):Math.abs(h-b)<=c&&(Math.abs(h-r)<=c&&Math.abs(b-r)<=c?a.top=Math.max(a.top,f):Math.abs(h-i)<=c&&Math.abs(b-i)<=c&&(a.bottom=Math.max(a.bottom,f)))}return a.left||a.right||a.top||a.bottom?a:void 0}function dh(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function ph(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let o=In(e.vertices,n,t),r=Un(o);return r?{normal:r,planeD:dh(r,o[0])}:null}function gh(e,n){return(e.material?.key??e.color??"")===(n.material?.key??n.color??"")&&e.color===n.color}function hh(e,n={}){return new Set(pl(e,n).keys())}function pl(e,n={}){let t=n.tileSize??50,o=n.layerElevation??t,r=e.map(a=>ph(a,t,o)),s=new Map;for(let a=0;a<e.length;a++){let l=e[a].vertices;if(!(!l||l.length<3))for(let u=0;u<l.length;u++){let f=dl(l[u],l[(u+1)%l.length]),d=s.get(f),m={polygon:a,edge:u};d?d.push(m):s.set(f,[m])}}let i=new Map,c=(a,l)=>{let u=i.get(a);u?u.add(l):i.set(a,new Set([l]))};for(let a of s.values())if(!(a.length<2))for(let l=0;l<a.length;l++)for(let u=l+1;u<a.length;u++){let f=a[l],d=a[u],m=f.polygon,p=d.polygon;!r[m]||!r[p]||gh(e[m],e[p])&&(c(m,f.edge),c(p,d.edge))}return i}function bh(e,n,t,o,r,s){let i=(e-t)*(s-o)-(n-o)*(r-t);return Math.abs(i)>_o?!1:(e-t)*(e-r)+(n-o)*(n-s)<=_o}function gl(e,n=Io,t=Io){let o=!1;for(let r=0,s=e.length-1;r<e.length;s=r++){let[i,c]=e[r],[a,l]=e[s];if(bh(n,t,i,c,a,l))return!0;c>t!=l>t&&n<(a-i)*(t-c)/(l-c)+i&&(o=!o)}return o}function hl(e,n,t){let o=Number.POSITIVE_INFINITY,r=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];!Number.isFinite(u)||!Number.isFinite(f)||(o=Math.min(o,u),r=Math.min(r,f),s=Math.max(s,u),i=Math.max(i,f))}let c=s-o,a=i-r;return!Number.isFinite(o)||!Number.isFinite(r)||!Number.isFinite(c)||!Number.isFinite(a)||c<=de||a<=de?{minX:0,minY:0,width:n,height:t}:{minX:o,minY:r,width:c,height:a}}function Yt(e){let n=e.bleedRatio??1,t=us*n,o=e.canvasW||1,r=e.canvasH||1,s=t>0?ht(e.screenPts,t):e.screenPts,i=t>0?hl(s,o,r):{minX:0,minY:0,width:o,height:r},c=[];for(let a=0;a<s.length;a+=2){let l=Math.max(0,Math.min(100,(s[a]-i.minX)/i.width*100)),u=Math.max(0,Math.min(100,(s[a+1]-i.minY)/i.height*100));c.push([l,u])}return{bounds:i,points:c}}function bl(e){let n=[];for(let t of e){let o=n[n.length-1];o&&Math.hypot(o[0]-t[0],o[1]-t[1])<=Oo||n.push(t)}if(n.length>1){let t=n[0],o=n[n.length-1];Math.hypot(t[0]-o[0],t[1]-o[1])<=Oo&&n.pop()}return n}function yl([e,n]){let t=new Set;return Math.abs(e)<=Ze&&t.add("left"),Math.abs(e-100)<=Ze&&t.add("right"),Math.abs(n)<=Ze&&t.add("top"),Math.abs(n-100)<=Ze&&t.add("bottom"),t.size>0?t:null}function xl(e,n){for(let t of e)if(n.has(t))return!0;return!1}function Pl(e,n,t,o){let r=(s,i,c)=>{let a=n.has(i)?e:o.has(i)?t:null,l=n.has(c)?e:o.has(c)?t:null;if(!a||!l)return null;let u=(()=>{switch(s){case"topLeft":return{x:a[0],y:l[1]};case"topRight":return{x:100-a[0],y:l[1]};case"bottomRight":return{x:100-a[0],y:100-l[1]};case"bottomLeft":return{x:a[0],y:100-l[1]}}})();return u.x>Ze&&u.y>Ze&&u.x<100-Ze&&u.y<100-Ze?[s,u]:null};return(n.has("top")||o.has("top"))&&(n.has("left")||o.has("left"))?r("topLeft","top","left"):(n.has("top")||o.has("top"))&&(n.has("right")||o.has("right"))?r("topRight","top","right"):(n.has("bottom")||o.has("bottom"))&&(n.has("right")||o.has("right"))?r("bottomRight","bottom","right"):(n.has("bottom")||o.has("bottom"))&&(n.has("left")||o.has("left"))?r("bottomLeft","bottom","left"):null}function qs(e){if(e.texture||bt(e)||Gn(e))return null;let n=Yt(e),t=bl(n.points);if(t.length<4)return null;let o=t.map(yl);if(o.some(i=>!i))return null;let r={},s=0;for(let i=0;i<t.length;i+=1){let c=o[i],a=o[(i+1)%t.length];if(xl(c,a))continue;let l=Pl(t[i],c,t[(i+1)%t.length],a);if(!l)return null;let[u,f]=l,d=r[u];if(d&&(Math.abs(d.x-f.x)>Ze||Math.abs(d.y-f.y)>Ze))return null;r[u]=f,s+=1}return s>0?{bounds:n.bounds,radii:r}:null}function yh([e,n]){return`${vn(e)} ${vn(n)}`}function xh(e){return`polygon(${e.map(yh).join(",")})`}function Ph(e){if(gl(e))return"circle(0)";let n=0,t=0,o=Math.max(1,e.length);for(let[i,c]of e)n+=i,t+=c;let r=vn(Math.max(0,Math.min(100,n/o))),s=vn(Math.max(0,Math.min(100,t/o)));return`circle(0 at ${r} ${s})`}function Qs(e){return`${xh(e)} ${Ph(e)}`}function Mh(e){return Qs(Yt(e).points)}function Js(e,n){return zt(e,n.width/yn,n.height/yn,n.minX,n.minY)}function Ah(e){let n=Yt(e);return`matrix3d(${Js(e,n.bounds)})`}function Sh(e){let n=Yt(e);return[`transform:matrix3d(${Js(e,n.bounds)})`,`border-shape:${Qs(n.points)}`].join(";")}function Ch(e,n){let t=[`transform:matrix3d(${Js(e,n.bounds)})`,`width:${yn}px`,`height:${yn}px`,"border:0","box-sizing:border-box","background:currentColor"];for(let[o,r]of Object.entries(n.radii)){let s=o.replace(/[A-Z]/g,i=>`-${i.toLowerCase()}`);t.push(`border-${s}-radius:${vn(r.x)} ${vn(r.y)}`),t.push(`corner-${s}-shape:bevel`)}return t.join(";")}function Ml(e){if(e.screenPts.length!==8)return null;let n=[],t=[],o=.001,r=(s,i)=>{for(let c of s)if(Math.abs(c-i)<=o)return;s.push(i)};for(let s=0;s<e.screenPts.length;s+=2)r(n,e.screenPts[s]),r(t,e.screenPts[s+1]);if(n.length!==2||t.length!==2||(n.sort((s,i)=>s-i),t.sort((s,i)=>s-i),Math.abs(n[0])>o||Math.abs(t[0])>o||n[1]-n[0]<=o||t[1]-t[0]<=o))return null;for(let s=0;s<e.screenPts.length;s+=2){let i=e.screenPts[s],c=e.screenPts[s+1],a=Math.abs(i-n[0])<=o||Math.abs(i-n[1])<=o,l=Math.abs(c-t[0])<=o||Math.abs(c-t[1])<=o;if(!a||!l)return null}return{left:n[0],top:t[0],width:n[1]-n[0],height:t[1]-t[0]}}function Gn(e){return!!Ml(e)}function bt(e){return!e.texture&&e.polygon.vertices.length===3}function Wt(e){return!e.texture&&!!e.projectiveMatrix&&!Gn(e)}function Eh(e){let n=/\b(?:Chrome|HeadlessChrome|Chromium|Edg|OPR)\//.test(e);return/\bVersion\/[\d.]+.*\bSafari\//.test(e)&&!n}function ei(e,n){e.set(n,(e.get(n)??0)+1)}function ni(e){let n,t=1;for(let[o,r]of e)r>t&&(n=o,t=r);return n}function Th(e,n,t,o){let r=!t.has("b"),s=r&&o.projectiveQuadSupported,i=!t.has("u")&&o.solidTriangleSupported,c=!t.has("i")&&!!o.cornerShapeSupported,a=!t.has("i")&&o.borderShapeSupported,l=t.has("b");return e.map(u=>{if(!u||u.texture)return u;if(i&&bt(u))return null;let f=Gn(u);return c&&!f&&!(s&&Wt(u))&&qs(u)!==null||r&&f||s&&Wt(u)||a&&(!f||l)?null:u})}function vh(e,n,t,o,r,s,i){let c=new Map,a=new Map,l=new Map,u=!t.has("b"),f=u&&o.projectiveQuadSupported,d=!t.has("u")&&o.solidTriangleSupported,m=!t.has("i")&&o.cornerShapeSupported,p=!t.has("i")&&o.borderShapeSupported;for(let b of e){if(!b||b.texture)continue;let y=m&&!!i?.(b);if(n==="dynamic"){if(!(d&&bt(b))&&!(u&&Gn(b))&&!(f&&Wt(b))&&!y&&!p)continue;let x=r(b.polygon.color??"#cccccc"),P=s(x);ei(a,P),l.has(P)||l.set(P,x);continue}!(d&&bt(b))&&!(u&&Gn(b))&&!(f&&Wt(b))&&!y&&!p||ei(c,b.shadedColor)}let h=ni(c),g=ni(a);return{paintColor:h,dynamicColorKey:g,dynamicColor:g?l.get(g):void 0}}function Ih(e,n){if((e+1)%3===n)return e;if((n+1)%3===e)return n}function _h(e,n,t,o,r,s,i){if(!e?.size)return null;let c=Xo(n,t);return[[s,o],[o,r],[r,s]].map(([l,u],f)=>{let d=Ih(l,u),m=d!==void 0&&e.has(d)?c:0;return Yo(i,f,m)})}function ti(e,n,t,o,r,s,i,c){let a=s,l="",u,f,d="";if(i){let m=c??e.color??"#cccccc",p=s.directionalLight,h=s.ambientLight,g=p?.direction??ot,b=p?.color??rt,y=Math.max(0,p?.intensity??st),x=h?.color??it,P=Math.max(0,h?.intensity??at),S=Math.hypot(g[0],g[1],g[2])||1,C=g[0]/S,T=g[1]/S,A=g[2]/S,E=s.lightOccludedPolyIndices?.has(n)??!1?0:y*Math.max(0,t*C+o*T+r*A),v=Bn(m,E,b,x,P),I=s.textureLighting??"baked",_=I==="baked"&&a.stableTriangleColorSteps?hs(v,a.stableTriangleColorSteps):v,O=xn(m),V=_===s.solidPaintDefaults?.paintColor,w=I==="dynamic"&&gs(O)===s.solidPaintDefaults?.dynamicColorKey;l=I==="dynamic"||V?"":_,u=l?xn(l):void 0,f=l?Vo(l):void 0,d=I==="dynamic"?`--pnx:${t.toFixed(4)};--pny:${o.toFixed(4)};--pnz:${r.toFixed(4)};`+(w?"":`--psr:${(O.r/255).toFixed(4)};--psg:${(O.g/255).toFixed(4)};--psb:${(O.b/255).toFixed(4)};`):""}return{index:n,polygon:e,colorComputed:i,bakedColor:l||void 0,bakedRgb:u,bakedAlpha:f,dynamicVars:d}}function Oh(e,n,t){if(e.texture||e.vertices.length!==3)return null;let o=t.tileSize??50,r=t.layerElevation??o,s=e.vertices[0],i=e.vertices[1],c=e.vertices[2],a=[s[1]*o,s[0]*o,s[2]*r],l=[i[1]*o,i[0]*o,i[2]*r],u=[c[1]*o,c[0]*o,c[2]*r],f=l[0]-a[0],d=l[1]-a[1],m=l[2]-a[2],p=u[0]-a[0],h=u[1]-a[1],g=u[2]-a[2],b=-(d*g-m*h),y=-(m*p-f*g),x=-(f*h-d*p),P=Math.sqrt(b*b+y*y+x*x);return P<=de?null:(b/=P,y/=P,x/=P,ti(e,n,b,y,x,t,!0))}function Vh(e,n,t,o={}){if(e.texture||e.vertices.length!==3)return null;let r=t.tileSize??50,s=t.layerElevation??r,i=e.vertices[0],c=e.vertices[1],a=e.vertices[2],l=i[1]*r,u=i[0]*r,f=i[2]*s,d=c[1]*r,m=c[0]*r,p=c[2]*s,h=a[1]*r,g=a[0]*r,b=a[2]*s;return oi(e,n,t,o,l,u,f,d,m,p,h,g,b)}function oi(e,n,t,o,r,s,i,c,a,l,u,f,d){let m=t,p=c-r,h=a-s,g=l-i,b=u-r,y=f-s,x=d-i,P=-(h*x-g*y),S=-(g*b-p*x),C=-(p*y-h*b),T=Math.sqrt(P*P+S*S+C*C);if(T<=de)return null;P/=T,S/=T,C/=T;let A=o.basis,M=A?.a??0,E=A?.b??1,v=A?.c??2;M<0||M>2||E<0||E>2||v<0||v>2||M===E||M===v||E===v?(A=void 0,M=0,E=1,v=2):M===0&&E===1&&v===2||M===1&&E===2&&v===0||M===2&&E===0&&v===1||(A=void 0,M=0,E=1,v=2);let I=()=>A?oi(e,n,t,{...o,basis:void 0},r,s,i,c,a,l,u,f,d):null;if(!A){let ve=p*p+h*h+g*g,Qe=u-c,on=f-a,Je=d-l,We=r-u,rn=s-f,Xn=i-d,sn=Qe*Qe+on*on+Je*Je,je=We*We+rn*rn+Xn*Xn,Yn=ve;sn>Yn&&(M=1,E=2,v=0,Yn=sn),je>Yn&&(M=2,E=0,v=1)}let _,O,V,w,F,D,H=v===0?r:v===1?c:u,K=v===0?s:v===1?a:f,Q=v===0?i:v===1?l:d;M===0?(_=r,O=s,V=i):M===1?(_=c,O=a,V=l):(_=u,O=f,V=d),E===0?(w=r,F=s,D=i):E===1?(w=c,F=a,D=l):(w=u,F=f,D=d);let ne=w-_,ee=F-O,ue=D-V,U=Math.sqrt(ne*ne+ee*ee+ue*ue);if(U<=de)return I();let $=ne/U,k=ee/U,L=ue/U,W=(H-_)*$+(K-O)*k+(Q-V)*L,Y=S*L-C*k,se=C*$-P*L,Z=P*k-S*$,B=T/U;if(B<=de)return I();let q=Math.max(0,Math.min(U,W)),me=Math.max(0,U-q),fe=[q,0,0,B,q+me,B],G=rs*(m.bleedRatio??1),te=_h(m.seamEdges,m.seamBleed,G,M,E,v,fe),ce=te?Xt(fe,te):Ys(q,me,B,Xo(m.seamBleed,G)),be=ce[0],he=ce[1],ye=ce[2],ge=ce[3],Se=ce[4],De=ce[5],R=(ge+De)/2,z=be-ye,j=Se-be,J=R-he;if(z<=de||j<=de||J<=de||!Number.isFinite(z+j+J))return I();let le=o.includeColor??!0,re=!1,pe,xe,ie,ae="";if(le){let ve=ti(e,n,P,S,C,t,!0,o.color);re=ve.colorComputed,pe=ve.bakedColor,xe=ve.bakedRgb,ie=ve.bakedAlpha,ae=ve.dynamicVars??""}let N=pe?`color:${pe};`:"",X=o.primitive??o.resolvedPrimitive??"border",Te=1/(X==="border-large"?cs:as),Ce=z+j,Pe=Ce*Te,_e=(j-z)*.5*Te,Oe=J*Te,Ve=be-q-Ce*.5,ze=he,Ee=$*Pe,Fe=k*Pe,we=L*Pe,zn=$*_e+Y*Oe,$n=k*_e+se*Oe,cn=L*_e+Z*Oe,qe=H+$*Ve+Y*ze,ln=K+k*Ve+se*ze,Be=Q+L*Ve+Z*ze,Xe=o.matrixDecimals??Ws(m.stableTriangleMatrixDecimals),Me=Gs(Ee,Fe,we,zn,$n,cn,P,S,C,qe,ln,Be,Xe),Re=t.textureLighting??"baked",Ie=m.optimizeStableTriangleStyle===!0&&Re==="baked"?"":`transform:${Me};`+N+ae,Ye=A&&A.a===M&&A.b===E&&A.c===v?A:{a:M,b:E,c:v};return{index:n,polygon:e,styleText:Ie,transformText:Me,basis:Ye,primitive:X,colorComputed:re,bakedColor:pe,bakedRgb:xe,bakedAlpha:ie,dynamicVars:ae}}function Al(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function Sl(e){let n=e?.maxWeightRatio,t=Math.max(0,Al(e?.denomEps,fs)),o=typeof n=="number"&&Number.isFinite(n)&&n>0?Math.max(1,n):ms,r=Math.max(0,Al(e?.bleed,ds));return{denomEps:t,maxWeightRatio:o,bleed:r,disableGuards:e?.disableGuards===!0}}function ri(e,n){if(e.length!==4||!$t(e))return null;let[t,o,r,s]=e,i=t[0]-o[0]+r[0]-s[0],c=t[1]-o[1]+r[1]-s[1],a=o[0]-r[0],l=s[0]-r[0],u=o[1]-r[1],f=s[1]-r[1],d=a*f-u*l;if(Math.abs(d)<=de)return null;let m=(i*f-c*l)/d,p=(a*c-u*i)/d,h=[1,1+m,1+m+p,1+p];if(h.some(y=>!Number.isFinite(y)))return null;let g=Math.min(...h),b=Math.max(...h);return!n.disableGuards&&(g<=n.denomEps||b/g>n.maxWeightRatio)?null:{g:m,h:p,w1:1+m,w3:1+p}}function Cl(e,n,t,o,r,s,i,c,a){if(e.length!==8)return null;let l=[[e[0],e[1]],[e[2],e[3]],[e[4],e[5]],[e[6],e[7]]];if(!ri(l,c))return null;let u=Hs(4,a),f=u?Xt(e,u):ht(e,c.bleed),d=[[f[0],f[1]],[f[2],f[3]],[f[4],f[5]],[f[6],f[7]]],m=ri(d,c);if(!m)return null;let{g:p,h,w1:g,w3:b}=m,[y,x,,P]=d,S=[r+y[0]*n[0]+y[1]*t[0],s+y[0]*n[1]+y[1]*t[1],i+y[0]*n[2]+y[1]*t[2]],C=([A,M],E)=>[(E-1)*r+(E*A-y[0])*n[0]+(E*M-y[1])*t[0],(E-1)*s+(E*A-y[0])*n[1]+(E*M-y[1])*t[1],(E-1)*i+(E*A-y[0])*n[2]+(E*M-y[1])*t[2]],T=[...C(x,g),p,...C(P,b),h,o[0],o[1],o[2],0,S[0],S[1],S[2],1];for(let A=0;A<8;A+=1)T[A]/=ct;return Tn(T,6)}function jo(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Rh(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function El(e){return!e.texture}function Tl(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let o=In(e.vertices,n,t),r=Un(o);return r?{pts:o,normal:r,planeD:jo(r,o[0]),optimizable:El(e)}:null}function vl(e,n){return!e||!n||!e.optimizable||!n.optimizable?!1:ai(e,n)}function ai(e,n){return!e||!n||jo(e.normal,n.normal)<1-ts?!1:Math.abs(e.planeD-n.planeD)<=os}function si(e,n){if(!e)return null;let t=n.directionalLight,o=n.ambientLight,r=t?.direction??ot,s=t?.color??rt,i=Math.max(0,t?.intensity??st),c=o?.color??it,a=Math.max(0,o?.intensity??at),l=Math.hypot(r[0],r[1],r[2])||1,u=r[0]/l,f=r[1]/l,d=r[2]/l,m=i*Math.max(0,e.normal[0]*u+e.normal[1]*f+e.normal[2]*d),p=Lo(m,s,c,a);return p.r*.2126+p.g*.7152+p.b*.0722}function Il(e){let n=[...e],t=Math.abs(n[0])>de?0:Math.abs(n[1])>de?1:2;return n[t]<0&&(n[0]*=-1,n[1]*=-1,n[2]*=-1),`${n[0].toFixed(6)},${n[1].toFixed(6)},${n[2].toFixed(6)}`}function yt(e,n,t,o,r={}){let s=jo(o,t),i=[o[0]-s*t[0],o[1]-s*t[1],o[2]-s*t[2]],c=Math.hypot(i[0],i[1],i[2]);if(c<=de)return null;let a=[i[0]/c,i[1]/c,i[2]/c],l=[t[1]*a[2]-t[2]*a[1],t[2]*a[0]-t[0]*a[2],t[0]*a[1]-t[1]*a[0]],u=Math.hypot(l[0],l[1],l[2]);if(u<=de)return null;let f=[l[0]/u,l[1]/u,l[2]/u],d=e.map(V=>{let w=V[0]-n[0],F=V[1]-n[1],D=V[2]-n[2];return[w*a[0]+F*a[1]+D*a[2],w*f[0]+F*f[1]+D*f[2]]}),m=r.boundsOrigin??n,p=n[0]-m[0],h=n[1]-m[1],g=n[2]-m[2],b=p*a[0]+h*a[1]+g*a[2],y=p*f[0]+h*f[1]+g*f[2],x=1/0,P=1/0,S=-1/0,C=-1/0;for(let[V,w]of d){let F=V+b,D=w+y;F<x&&(x=F),F>S&&(S=F),D<P&&(P=D),D>C&&(C=D)}let T=S-x,A=C-P;if(!Number.isFinite(T)||!Number.isFinite(A))return null;let M=r.snapBounds?Math.floor(x+Ae):x,E=r.snapBounds?Math.floor(P+Ae):P,v=r.snapBounds?Math.ceil(S-Ae):S,I=r.snapBounds?Math.ceil(C-Ae):C,_=Math.max(1,r.snapBounds?v-M:Math.ceil(T)),O=Math.max(1,r.snapBounds?I-E:Math.ceil(A));return{xAxis:a,yAxis:f,local2D:d,shiftX:b-M,shiftY:y-E,canvasW:_,canvasH:O,pixelArea:_*O,rawArea:T*A}}function Wo(e){return`${e[0]},${e[1]},${e[2]}`}function _l(e,n){return Wo(e)<Wo(n)?[n[0]-e[0],n[1]-e[1],n[2]-e[2]]:[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Lh(e,n){let t=Wo(e),o=Wo(n);return t<o?`${t}|${o}`:`${o}|${t}`}function Ol(e,n,t,o){let r=0,s=0;for(let i of e){let c=n[i];if(!c)return null;let a=yt(c.pts,c.pts[0],c.normal,t,{boundsOrigin:o,snapBounds:!0});if(!a)return null;r+=a.pixelArea,s+=a.rawArea}return{pixelArea:r,rawArea:s}}function Vl(e,n){let t=n[e[0]]?.pts[0];if(!t)return null;let o={pixelArea:0,rawArea:0},r=null,s=new Set;for(let i of e){let c=n[i];if(!c)continue;let a=[c.pts[1][0]-c.pts[0][0],c.pts[1][1]-c.pts[0][1],c.pts[1][2]-c.pts[0][2]],l=yt(c.pts,c.pts[0],c.normal,a);o&&l?(o.pixelArea+=l.pixelArea,o.rawArea+=l.rawArea):o=null;for(let u=0;u<c.pts.length;u++){let f=_l(c.pts[u],c.pts[(u+1)%c.pts.length]),d=yt(c.pts,c.pts[0],c.normal,f);if(!d)continue;let m=Il(d.xAxis);if(s.has(m))continue;s.add(m);let p=Ol(e,n,d.xAxis,t);p&&(!r||p.pixelArea<r.pixelArea||p.pixelArea===r.pixelArea&&p.rawArea<r.rawArea-Ae)&&(r={xAxis:d.xAxis,...p})}}return r&&o&&(r.pixelArea<o.pixelArea||r.pixelArea===o.pixelArea&&r.rawArea<=o.rawArea+Ae)?{xAxis:r.xAxis,boundsOrigin:t,seamEdges:new Set}:null}function wh(e,n){let t=n.tileSize??50,o=n.layerElevation??t,r=e.map(f=>Tl(f,t,o)),s=new Map,i=e.map(()=>new Set),c=e.map(()=>new Set);for(let f=0;f<e.length;f++){let d=e[f].vertices;if(!(!d||d.length<3))for(let m=0;m<d.length;m++){let p=Lh(d[m],d[(m+1)%d.length]),h=s.get(p),g={polygon:f,edge:m};h?h.push(g):s.set(p,[g])}}let a=e.map(()=>new Set);for(let f of s.values())if(!(f.length<2))for(let d=0;d<f.length;d++)for(let m=d+1;m<f.length;m++){let p=f[d],h=f[m],g=p.polygon,b=h.polygon;if(e[g].texture&&e[b].texture&&(c[p.polygon].add(p.edge),c[h.polygon].add(h.edge)),ai(r[g],r[b]))i[p.polygon].add(p.edge),i[h.polygon].add(h.edge);else{let y=si(r[g],n),x=si(r[b],n);y!==null&&x!==null&&(y<=x+To&&i[p.polygon].add(p.edge),x<=y+To&&i[h.polygon].add(h.edge))}vl(r[g],r[b])&&(a[g].add(b),a[b].add(g))}let l=Array(e.length).fill(void 0),u=new Set;for(let f=0;f<e.length;f++){if(u.has(f)||!r[f]?.optimizable)continue;let d=[],m=[f];for(u.add(f);m.length>0;){let h=m.pop();d.push(h);for(let g of a[h])u.has(g)||(u.add(g),m.push(g))}if(d.length<2)continue;let p=Vl(d,r);if(p)for(let h of d)l[h]={xAxis:p.xAxis,boundsOrigin:p.boundsOrigin,seamEdges:i[h],textureEdgeRepairEdges:c[h]}}for(let f=0;f<e.length;f++)!l[f]&&(i[f].size>0||c[f].size>0)&&(l[f]={seamEdges:i[f],textureEdgeRepairEdges:c[f]});return l}function ii(e,n,t,o){if(o.optimize&&o.fixedXAxis)return yt(e,n,t,o.fixedXAxis,{boundsOrigin:o.boundsOrigin,snapBounds:o.snapBounds});let r=null,s=o.optimize&&o.seamEdges&&o.seamEdges.size>0?Array.from(o.seamEdges):null,i=s??(o.optimize?e.map((c,a)=>a):[0]);for(let c of i){let a=(c+1)%e.length,l=s?_l(e[c],e[a]):[e[a][0]-e[c][0],e[a][1]-e[c][1],e[a][2]-e[c][2]],u=yt(e,n,t,l,{boundsOrigin:o.boundsOrigin,snapBounds:o.snapBounds});u&&(!r||u.pixelArea<r.pixelArea||u.pixelArea===r.pixelArea&&u.rawArea<r.rawArea-Ae)&&(r=u)}return r}function Rl(e){if(e.local2D.length!==4)return!1;let n=[],t=[],o=(r,s)=>{for(let i of r)if(Math.abs(i-s)<=Ae)return;r.push(s)};for(let[r,s]of e.local2D)o(n,r+e.shiftX),o(t,s+e.shiftY);if(n.length!==2||t.length!==2||(n.sort((r,s)=>r-s),t.sort((r,s)=>r-s),Math.abs(n[0])>Ae||Math.abs(t[0])>Ae||n[1]-n[0]<=Ae||t[1]-t[0]<=Ae))return!1;for(let[r,s]of e.local2D){let i=r+e.shiftX,c=s+e.shiftY,a=Math.abs(i-n[0])<=Ae||Math.abs(i-n[1])<=Ae,l=Math.abs(c-t[0])<=Ae||Math.abs(c-t[1])<=Ae;if(!a||!l)return!1}return!0}function ci(e,n){if(e.length<3||n.length<3)return null;let[t,o,r]=e,[s,i,c]=n,a=t[0],l=t[1],u=o[0],f=o[1],d=r[0],m=r[1],p=s[0],h=1-s[1],g=i[0],b=1-i[1],y=c[0],x=1-c[1],P=g-p,S=b-h,C=y-p,T=x-h,A=P*T-C*S;if(Math.abs(A)<=1e-9)return null;let M=u-a,E=d-a,v=f-l,I=m-l,_={a:(M*T-E*S)/A,b:(P*E-C*M)/A,c:(v*T-I*S)/A,d:(P*I-C*v)/A,e:0,f:0};return _.e=a-_.a*p-_.b*h,_.f=l-_.c*p-_.d*h,_}function li(e){if(e.length===0)return null;let n=1/0,t=1/0,o=-1/0,r=-1/0;for(let s of e){let i=s[0],c=1-s[1];if(!Number.isFinite(i)||!Number.isFinite(c))return null;n=Math.min(n,i),o=Math.max(o,i),t=Math.min(t,c),r=Math.max(r,c)}return{minU:n,minV:t,maxU:o,maxV:r}}function Ll(e,n,t,o,r,s,i,c){let l=In(e.vertices,n,t).map(d=>{let m=d[0]-o[0],p=d[1]-o[1],h=d[2]-o[2];return[m*r[0]+p*r[1]+h*r[2]+i,m*s[0]+p*s[1]+h*s[2]+c]}),u=ci(l,e.uvs),f=li(e.uvs);return!u&&!f?null:{screenPts:l.flatMap(([d,m])=>[d,m]),uvAffine:u,uvSampleRect:f}}function wl(e,n,t,o,r){let s=t,{vertices:i,texture:c,uvs:a}=e;if(!i||i.length<3)return null;let l=t.tileSize??50,u=t.layerElevation??l,f=In(i,l,u),d=f[0],m=f[1],p=[m[0]-d[0],m[1]-d[1],m[2]-d[2]];if(Math.hypot(p[0],p[1],p[2])===0)return null;let g=Un(f);if(!g)return null;let b=ii(f,d,g,{optimize:!1}),y=c||b&&Rl(b)?b:ii(f,d,g,{optimize:!0,fixedXAxis:r?.xAxis,boundsOrigin:r?.boundsOrigin,snapBounds:!!r,seamEdges:r?.seamEdges});if(!y)return null;let{xAxis:x,yAxis:P,local2D:S}=y,C=c&&r?.textureEdgeRepairEdges?.size?r.textureEdgeRepairEdges:null,T=!!(c&&C),A=y.shiftX,M=y.shiftY,E=y.canvasW,v=y.canvasH,I=[];for(let[ge,Se]of S)I.push(ge+A,Se+M);let _=d[0]-A*x[0]-M*P[0],O=d[1]-A*x[1]-M*P[1],V=d[2]-A*x[2]-M*P[2],w=Tn([x[0],x[1],x[2],0,P[0],P[1],P[2],0,g[0],g[1],g[2],0,_,O,V,1]),F=Tn([x[0]*E,x[1]*E,x[2]*E,0,P[0]*v,P[1]*v,P[2]*v,0,g[0],g[1],g[2],0,_,O,V,1]),D=Tn([x[0]*E/He,x[1]*E/He,x[2]*E/He,0,P[0]*v/He,P[1]*v/He,P[2]*v/He,0,g[0],g[1],g[2],0,_,O,V,1]),H=js(s.seamBleed),K=Ks(I,s.seamEdges??r?.seamEdges,H),Q=K?new Set(K.keys()):void 0,ne=K?Math.max(...K.values()):void 0,ee=Zs(I,K),ue=!c&&i.length===4?Cl(I,x,P,g,_,O,V,o,K):null,U=t.directionalLight,$=t.ambientLight,k=U?.direction??ot,L=U?.color??rt,W=Math.max(0,U?.intensity??st),Y=$?.color??it,se=Math.max(0,$?.intensity??at),Z=Math.hypot(k[0],k[1],k[2])||1,B=k[0]/Z,q=k[1]/Z,me=k[2]/Z,G=t.lightOccludedPolyIndices?.has(n)??!1?0:W*Math.max(0,g[0]*B+g[1]*q+g[2]*me),te=Lo(G,L,Y,se),ce=Bn(e.color??"#cccccc",G,L,Y,se),be=null,he=null;c&&a&&a.length>=3&&a.length===i.length&&(he=li(a),be=ci(S.map(([ge,Se])=>[ge+A,Se+M]),a));let ye=c&&e.textureTriangles?.length?e.textureTriangles.map(ge=>Ll(ge,l,u,d,x,P,A,M)).filter(ge=>!!ge):null;return{index:n,polygon:e,texture:c,tileSize:l,layerElevation:u,matrix:w,canonicalMatrix:F,atlasMatrix:D,projectiveMatrix:ue,canvasW:E,canvasH:v,screenPts:I,uvAffine:be,uvSampleRect:he,textureTriangles:ye,textureEdgeRepairEdges:C,textureEdgeRepair:T,seamBleed:ne,seamBleedEdges:Q,seamBleedEdgeAmounts:K,seamBleedInsets:ee,bleedRatio:ps(s.seamBleed),normal:g,textureTint:te,shadedColor:ce}}function Nh(e,n,t={},o,r){let s=Sl(o),i=t,c=r??(t.textureEdgeRepairEdges?.size?{seamEdges:new Set,textureEdgeRepairEdges:t.textureEdgeRepairEdges}:void 0);return wl(e,n,i,s,c)}function Kt(e){let n=typeof e=="string"?Number(e):e;return n===void 0||!Number.isFinite(n)?1:Math.min(Zr,Math.max(Hr,n))}function fi(e){return e.reduce((n,t)=>n+t.width*t.height,0)}function Nl(e,n){let t=fi(e);if(t<=0)return 1;let o=Math.max(1,...e.map(i=>Math.max(i.width,i.height))),r=Ft/o,s=Math.sqrt(n/(t*4));return Kt(Math.min(r,s))}function Dl(e,n){let t=fi(e),o=.5;return t<=Eo?o=1:t<=qr&&(o=.75),Kt(Math.min(o,Nl(e,n)))}function Fl(e,n){return e.reduce((t,o)=>Math.max(t,Math.ceil(o.width*n),Math.ceil(o.height*n)),0)}function Bl(e,n){return e.reduce((t,o)=>t+Math.ceil(o.width*n)*Math.ceil(o.height*n)*4,0)}function kl(e,n,t){let o=Fl(e,n),r=Bl(e,n),s=o>Ft?Ft/o:1,i=r>t?Math.sqrt(t/r):1;return Math.min(s,i)}function Ul(e){return e?Qr:Jr}function Gl(e,n){return e!==void 0&&e!=="auto"?He:n?He:ls}function ui(e,n){for(let t of e.entries)t&&(t.atlasCanonicalSize=n,t.atlasMatrix=zs(t,n));return e}function Dh(e){return e.atlasCanonicalSize??He}function zl(e){return Math.max(Co,Math.ceil(Co/e))}function jt(e,n=1){let t=Array(e.length).fill(null),o=[],r=zl(n),s=e.filter(a=>!!a).sort((a,l)=>l.canvasH-a.canvasH||l.canvasW-a.canvasW||a.index-l.index),i=()=>({width:r,height:r,entries:[],shelves:[]}),c=(a,l,u)=>{if(a.sealed)return null;for(let m of a.shelves)if(l.canvasH<=m.height&&m.x+l.canvasW+r<=Sn){let p={...l,pageIndex:u,x:m.x,y:m.y};return m.x+=l.canvasW+r*2,a.entries.push(p),a.width=Math.max(a.width,p.x+l.canvasW+r),p}let f=a.shelves.length===0?r:a.height+r;if(f+l.canvasH+r>Sn)return null;let d={...l,pageIndex:u,x:r,y:f};return a.shelves.push({x:r+l.canvasW+r*2,y:f,height:l.canvasH}),a.entries.push(d),a.width=Math.max(a.width,d.x+l.canvasW+r),a.height=Math.max(a.height,f+l.canvasH+r),d};for(let a of s){if(a.canvasW+r*2>Sn||a.canvasH+r*2>Sn){let f=o.length,d={...a,pageIndex:f,x:r,y:r};t[a.index]=d,o.push({width:a.canvasW+r*2,height:a.canvasH+r*2,entries:[d],shelves:[],sealed:!0});continue}let u=null;for(let f=0;f<o.length&&(u=c(o[f],a,f),!u);f++);if(!u){let f=i(),d=o.length;o.push(f),u=c(f,a,d)}u&&(t[a.index]=u)}return{entries:t,pages:o.map(({width:a,height:l,entries:u})=>({width:a,height:l,entries:u}))}}function Fh(e,n,t){let o=Dl(n.pages,t),r=o===1?n:jt(e,o);for(let s=0;s<4;s++){let i=kl(r.pages,o,t);if(i>=1)break;let c=Kt(o*i*es);if(c>=o)break;o=c,r=jt(e,o)}return{packed:r,atlasScale:o}}function Bh(e,n,t){let o=Gl(n,t);if(n!==void 0&&n!=="auto"){let i=Kt(n);return{packed:ui(jt(e,i),o),atlasScale:i,atlasCanonicalSize:o}}let r=jt(e,1),s=Fh(e,r,Ul(t));return{packed:ui(s.packed,o),atlasScale:s.atlasScale,atlasCanonicalSize:o}}export{ng as ASYNC_RENDER_BUDGET_MS,ls as ATLAS_CANONICAL_SIZE_AUTO_DESKTOP,He as ATLAS_CANONICAL_SIZE_EXPLICIT,Sn as ATLAS_MAX_SIZE,Co as ATLAS_PADDING,Eo as AUTO_ATLAS_LOW_AREA,Ft as AUTO_ATLAS_MAX_BITMAP_SIDE,Jr as AUTO_ATLAS_MAX_DECODED_BYTES_DESKTOP,Qr as AUTO_ATLAS_MAX_DECODED_BYTES_MOBILE,qr as AUTO_ATLAS_MEDIUM_AREA,es as AUTO_ATLAS_SCALE_GUARD,Zp as BAKED_SHADOW_MIN_UP,Hp as BAKED_SHADOW_Z_SQUASH,$e as BASE_TILE,de as BASIS_EPS,us as BORDER_SHAPE_BLEED,yn as BORDER_SHAPE_CANONICAL_SIZE,Io as BORDER_SHAPE_CENTER_PERCENT,_o as BORDER_SHAPE_POINT_EPS,Po as CAMERA_BACKFACE_CULL_EPS,ns as COLOR_PARSE_CACHE_MAX,Oo as CORNER_SHAPE_DUPLICATE_EPS,Ze as CORNER_SHAPE_POINT_EPS,is as DECIMAL_SCALES,it as DEFAULT_AMBIENT_COLOR,at as DEFAULT_AMBIENT_INTENSITY,vo as DEFAULT_ATLAS_CSS_DECIMALS,ss as DEFAULT_BORDER_SHAPE_DECIMALS,Kn as DEFAULT_CAMERA_STATE,rt as DEFAULT_LIGHT_COLOR,ot as DEFAULT_LIGHT_DIR,st as DEFAULT_LIGHT_INTENSITY,bn as DEFAULT_MATRIX_DECIMALS,$l as DEFAULT_PROJECTION,ig as DEFAULT_SEAM_BLEED,Ki as DEFAULT_SEAM_FACET_SPLIT_OPTIONS,ji as DEFAULT_SEAM_OVERLAP_OPTIONS,hn as DEFAULT_TILE,dc as LoopOnce,pc as LoopPingPong,Ms as LoopRepeat,Zr as MAX_ATLAS_SCALE,Hr as MIN_ATLAS_SCALE,ds as PROJECTIVE_QUAD_BLEED,fs as PROJECTIVE_QUAD_DENOM_EPS,ms as PROJECTIVE_QUAD_MAX_WEIGHT_RATIO,tu as QUAT_IDENTITY,uc as RECEIVER_NORMAL_TOL,fc as RECEIVER_OFFSET_TOL,lc as RECEIVER_OUTLINE_EXPAND,Ae as RECT_EPS,To as SEAM_LIGHT_EPS,ct as SOLID_QUAD_CANONICAL_SIZE,rs as SOLID_TRIANGLE_BLEED,as as SOLID_TRIANGLE_CANONICAL_SIZE,cs as SOLID_TRIANGLE_LARGE_BORDER_CANONICAL_SIZE,os as SURFACE_DISTANCE_EPS,ts as SURFACE_NORMAL_EPS,og as TEXTURE_EDGE_REPAIR_ALPHA_MIN,sg as TEXTURE_EDGE_REPAIR_RADIUS,rg as TEXTURE_EDGE_REPAIR_SOURCE_ALPHA_MIN,tg as TEXTURE_TRIANGLE_BLEED,ka as VOXEL_CAMERA_CULL_AXIS_EPS,Ua as VOXEL_CAMERA_CULL_NORMAL_LIMIT,ui as applyPackedAtlasCanonicalSize,Ha as arrowPolygons,fi as atlasArea,Fl as atlasBitmapMaxSide,Dh as atlasCanonicalSizeForEntry,Gl as atlasCanonicalSizeForTextureQuality,Bl as atlasDecodedBytes,zl as atlasPadding,kl as autoAtlasBudgetFactor,Ul as autoAtlasMaxDecodedBytes,Dl as autoAtlasScale,Nl as autoAtlasScaleCap,Ya as axesHelperPolygons,f0 as bakeSolidTextureSampledPolygons,Is as bakeSolidTextureSamples,Il as basisAxisKey,hl as borderShapeBoundsFromPoints,Yt as borderShapeGeometryForPlan,ja as boxPolygons,qp as buildBakedShadowProjectionMatrix,wh as buildBasisHints,bs as buildEdgeOwners,Hl as buildSceneContext,pl as buildSeamBleedPolygonEdges,hh as buildSeamBleedPolygonSet,dg as buildSharedEdgeMap,mh as buildTextureEdgeRepairSets,ut as cachedParsePureColor,$a as cameraCullNormalGroups,Bp as cameraCullNormalGroupsFromPolygons,za as cameraCullNormalKey,Up as cameraCullVisibleSignature,Ga as cameraFacingDepth,Vl as chooseIslandXAxis,ii as chooseLocalBasis,_n as clampChannel,ys as classifyFacing,An as clipPolygonToConvex2D,mg as colorErrorScore,ai as compatibleBleedSurface,vl as compatibleSurface,Dp as computeLightVisibility,Ks as computePlanSeamBleedEdgeAmounts,ri as computeProjectiveQuadCoefficients,Cl as computeProjectiveQuadMatrix,bg as computeReceiverShadowFaces,Mi as computeSceneBbox,Zs as computeSeamBleedInsets,Yl as computeShapeLighting,Oh as computeSolidTriangleColorPlan,ti as computeSolidTriangleColorPlanFromNormal,Vh as computeSolidTrianglePlan,oi as computeSolidTrianglePlanFromCssPoints,Un as computeSurfaceNormal,wl as computeTextureAtlasPlan,Nh as computeTextureAtlasPlanPublic,eu as computeTexturePaintMetrics,ci as computeUvAffine,li as computeUvSampleRect,rc as conePolygons,Wr as convexHull2D,Pl as cornerShapeDiagonal,qs as cornerShapeGeometryForPlan,yl as cornerShapePointSides,sr as coverPlanarPolygons,lu as createIsometricCamera,Ag as createPolyAnimationMixer,Rh as crossVec,Qs as cssBorderShapeForGeometry,Mh as cssBorderShapeForPlan,In as cssPoints,fn as cullInteriorPolygons,Ao as cylinderPolygons,Ru as dedupeOverlappingPolygons,oc as dodecahedronPolygons,ni as dominantCountKey,jo as dotVec,gn as ensureCcw2D,iu as eulerXYZFromQuat,Ol as evaluateIslandAxis,gt as expandClipPoints,cc as expandConvexHullOutward,xs as extractSilhouetteLoops,Th as filterAtlasPlans,Jt as findOverlappingPolygonDuplicates,ih as formatAffineMatrix3dColumns,Us as formatAffineMatrix3dScalars,Gs as formatAffineMatrix3dTransformScalars,zs as formatAtlasMatrix,Sh as formatBorderShapeElementStyle,Ah as formatBorderShapeEntryMatrix,ah as formatBorderShapeMatrix,Ht as formatColor,Ch as formatCornerShapeElementStyle,sh as formatCssLength,lh as formatCssLengthPx,ch as formatMatrix3d,Tn as formatMatrix3dValues,vn as formatPercent,zt as formatScaledMatrixFromPlan,uh as formatSolidQuadEntryMatrix,ul as formatSolidQuadMatrix,Ml as fullRectBounds,Tl as getPolygonBasisInfo,vh as getSolidPaintDefaultsForPlansCore,Kr as groupReceiverFaceGroups,tc as icosahedronPolygons,ei as incrementCount,Xs as intersect2DLines,fh as intersect2DLinesRaw,nu as inverseRotateVec3,Xa as isAxisAlignedSurfaceNormal,Qp as isBakedShadowCaster,El as isBasisOptimizable,$t as isConvexPolygonPoints,Rl as isFullRectBasis,Gn as isFullRectSolid,Wt as isProjectiveQuadPlan,bt as isSolidTrianglePlan,kp as isVoxelCameraCullableNormalGroups,rh as loadMesh,yt as makeLocalBasis,an as mergePolygons,jr as meshScaleVec3,Nt as normalFacesCamera,Kt as normalizeAtlasScale,au as normalizeInvertMultiplier,Zo as normalizePolygons,js as normalizedSeamBleed,Ja as octahedronPolygons,ht as offsetConvexPolygonPoints,Xt as offsetConvexPolygonPointsByEdgeAmounts,Ys as offsetStableTrianglePoints,$o as offsetTrianglePoints,b0 as optimizeAnimatedMeshPolygons,nt as optimizeMeshParseResult,fd as optimizeMeshPolygons,jt as packTextureAtlasPlans,Bh as packTextureAtlasPlansWithScaleCore,Vo as parseAlpha,Wn as parseColor,Es as parseGltf,xn as parseHex,mi as parseHexColor,Rs as parseMtl,Os as parseObj,Pn as parsePureColor,di as parseRgbColor,ks as parseStl,ws as parseVox,Qa as planePolygons,gl as polygonContainsPoint,$r as polygonCssSurfaceNormal,Zl as polygonFaces,Fp as polygonFacesCamera,ic as polygonSignedArea2D,pg as prepareCasterEdgeOwners,gg as prepareCasterPolyItems,hg as prepareReceiverFacePlanes,Jp as projectCssVertexToGround,Ll as projectTextureTriangle,hs as quantizeCssColor,ru as quatFromAxisAngle,su as quatFromEulerXYZ,ou as quatMultiply,Zf as repairMeshSeams,ps as resolveBleedRatio,Sl as resolveProjectiveQuadGuards,Xo as resolveSeamBleed,lg as rgbEqual,gs as rgbKey,fg as rgbToCss,Ro as rgbToHex,Za as ringPolygons,qa as ringQuadPolygons,qo as rotateVec3,On as rotateVec3InWrapperCssFrame,Ne as roundDecimal,Eh as safariCssProjectiveUnsupported,Yo as safePlanSeamBleedAmount,uu as screenToWorldOnSphere,Ei as screenToWorldRay,Hs as seamBleedAmountArray,sa as seamFacetSplitPolygons,Hf as seamFacetSplitReport,si as seamLightBrightness,qf as seamOverlapDiagnostics,ia as seamOverlapPolygons,Qf as seamOverlapReport,Xl as shadeColor,Bn as shadePolygon,xl as sharedCornerShapeSide,$s as signedArea2D,bl as simplifyCornerShapePoints,Ar as simplifyTriangleMeshPolygons,ec as spherePolygons,fl as stableBasisFromPlan,Ws as stableTriangleMatrixDecimals,ug as stepRgbToward,nc as tetrahedronPolygons,Lo as textureTintFactors,ag as tintToCss,sc as torusPolygons,Dt as worldCssForMesh,ac as worldDirectionToCss,eg as worldDirectionalLightToCss,tt as worldPositionToCss};