@layoutit/polycss-core 0.2.0 → 0.2.1
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/README.md +167 -103
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +911 -73
- package/dist/index.d.ts +911 -73
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var To="cubic";function wt(e){let n=e.replace("#","");if(n.length===3){let r=parseInt(n[0]+n[0],16),t=parseInt(n[1]+n[1],16),o=parseInt(n[2]+n[2],16);return Number.isNaN(r)||Number.isNaN(t)||Number.isNaN(o)?null:{rgb:[r,t,o],alpha:1}}if(n.length===6){let r=parseInt(n.slice(0,2),16),t=parseInt(n.slice(2,4),16),o=parseInt(n.slice(4,6),16);return Number.isNaN(r)||Number.isNaN(t)||Number.isNaN(o)?null:{rgb:[r,t,o],alpha:1}}return null}function It(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 Fn(e){if(!e)return null;let n=e.trim(),r=wt(n);return r||It(n)}function Ce(e){return Math.max(0,Math.min(255,Math.round(e)))}function Ze(e){let[n,r,t]=e.rgb.map(Ce);return e.alpha<1?`rgba(${n}, ${r}, ${t}, ${e.alpha})`:`rgb(${n}, ${r}, ${t})`}var zn={rgb:[204,204,204],alpha:1},Rt=new Map;function Ie(e){if(!e)return null;let n=e.trim(),r=Rt.get(n);if(r)return r;let t=Fn(n);return t?(Rt.set(n,t),t):null}function Eo(e,n){let r=Ie(e)??zn,t=[Ce(r.rgb[0]+n),Ce(r.rgb[1]+n),Ce(r.rgb[2]+n)];return Ze({rgb:t,alpha:r.alpha})}var Dn={direction:[0,0,-1],color:"#ffffff",intensity:1},Tt={color:"#ffffff",intensity:.4};function Et(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 Ot(e,n,r){let t=Ie(n)??zn;return e*(t.rgb[r]/255)}function Oo(e,n,r,t){let o=Ie(n)??zn,s=Et(r?.direction??Dn.direction),i=r?.color??Dn.color,a=Math.max(0,r?.intensity??Dn.intensity),l=t?.color??Tt.color,c=Math.max(0,t?.intensity??Tt.intensity),u=Et(e),f=Math.max(0,-(u[0]*s[0]+u[1]*s[1]+u[2]*s[2])),g=a*f,m=[0,0,0];for(let d=0;d<3;d=d+1){let p=o.rgb[d],h=Ot(p,l,d)*c,x=Ot(p,i,d)*g;m[d]=Ce(h+x)}return Ze({rgb:m,alpha:o.alpha})}var kt="#cccccc",Qe=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],_t=(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]],Lt=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],qe=e=>Math.hypot(e[0],e[1],e[2]);function ko(e){let n=1/0,r=1/0,t=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]<r&&(r=u[1]),u[1]>s&&(s=u[1]),u[2]<t&&(t=u[2]),u[2]>i&&(i=u[2]);let a=o-n,l=s-r,c=i-t;return Math.hypot(a,l,c)}function _o(){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 Gn(e){let n=[],r=[];if(!e||e.length===0)return{polygons:n,warnings:r};for(let t=0;t<e.length;t++){let o=e[t];if(!o||!Array.isArray(o.vertices)){r.push(`Polygon ${t}: missing vertices, dropped`);continue}let s=o.vertices;if(s.length<3){r.push(`Polygon ${t}: ${s.length} vertices (need >= 3), dropped`);continue}let i=Qe(s[1],s[0]),a=Qe(s[2],s[0]),l=_t(i,a),c=qe(l);if(s.length===3){if(c<1e-12){qe(i)<1e-12||qe(a)<1e-12?r.push(`Polygon ${t}: zero-area triangle (coincident vertices), dropped`):r.push(`Polygon ${t}: vertices collinear, dropped`);continue}}else if(c<1e-12){r.push(`Polygon ${t}: first 3 vertices collinear, dropped`);continue}let u=[{...o,vertices:s.slice()}];if(s.length>=4){let f=[l[0]/c,l[1]/c,l[2]/c],g=Lt(f,s[0]),m=ko(s),d=Math.max(1e-6,m*.001),p=0;for(let h=3;h<s.length;h++){let x=Math.abs(Lt(f,s[h])-g);x>p&&(p=x)}if(p>d){let h=[],x=o.uvs&&o.uvs.length===s.length?o.uvs:void 0;for(let y=1;y<s.length-1;y++){let M=[s[0].slice(),s[y].slice(),s[y+1].slice()],P=_t(Qe(M[1],M[0]),Qe(M[2],M[0]));if(qe(P)<1e-12)continue;let b={...o,vertices:M};x&&(b.uvs=[x[0].slice(),x[y].slice(),x[y+1].slice()]),h.push(b)}if(h.length===0){r.push(`Polygon ${t}: ${s.length} non-coplanar vertices, fan-triangulation produced no valid triangles, dropped`);continue}r.push(`Polygon ${t}: ${s.length} non-coplanar vertices, fan-triangulated to ${h.length} triangles`),u=h}}for(let f of u){let g=Lo(f,t,r);g&&n.push(g)}}return{polygons:n,warnings:r}}function Lo(e,n,r){let t={vertices:e.vertices},o=e.texture;if(typeof o=="string"&&o===""&&(o=void 0),e.color!==void 0&&o!==void 0&&_o()&&r.push(`Polygon ${n}: color and texture both set; texture wins`),e.color!==void 0&&(Ie(e.color)?t.color=e.color:(r.push(`Polygon ${n}: invalid color "${e.color}", replaced with ${kt}`),t.color=kt)),o!==void 0&&(t.texture=o),e.uvs!==void 0&&(!Array.isArray(e.uvs)||e.uvs.length!==e.vertices.length?r.push(`Polygon ${n}: uvs length ${Array.isArray(e.uvs)?e.uvs.length:"?"} != vertices length ${e.vertices.length}, uvs stripped`):t.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 l=e.data[a];typeof l=="string"||typeof l=="number"||typeof l=="boolean"?s[a]=l:(i=!0,r.push(`Polygon ${n}: data["${a}"] has non-primitive value, key dropped`))}Object.keys(s).length>0&&(t.data=s)}return t}function Bt(e){if(!e||e.length===0)return{min:[0,0,0],max:[0,0,0]};let n=1/0,r=1/0,t=1/0,o=-1/0,s=-1/0,i=-1/0,a=!1;for(let l of e)if(l?.vertices)for(let c of l.vertices)c&&(a=!0,c[0]<n&&(n=c[0]),c[0]>o&&(o=c[0]),c[1]<r&&(r=c[1]),c[1]>s&&(s=c[1]),c[2]<t&&(t=c[2]),c[2]>i&&(i=c[2]));return a?{min:[n,r,t],max:[o,s,i]}:{min:[0,0,0],max:[0,0,0]}}function Bo(e){let n=e.polygons??[],r,t;if(e.skipNormalize)r=n,t=[];else{let a=Gn(n);r=a.polygons,t=a.warnings}let o=Bt(r),s=[o.max[0]-o.min[0],o.max[1]-o.min[1],o.max[2]-o.min[2]];return{context:{polygons:r,sceneBbox:o,warnings:t},dimensions:{sceneBbox:o,size:s},warnings:t}}function No(e){return!e.vertices||e.vertices.length<3?[]:[{v:e.vertices.map(n=>[n[0],n[1],n[2]]),color:e.color}]}function Fo(e){let n=e[0],r=e[1],t=e[2],o=[r[0]-n[0],r[1]-n[1],r[2]-n[2]],s=[t[0]-n[0],t[1]-n[1],t[2]-n[2]],i=o[1]*s[2]-o[2]*s[1],a=o[2]*s[0]-o[0]*s[2],l=o[0]*s[1]-o[1]*s[0],c=Math.hypot(i,a,l);return c<=1e-9?null:[i/c,a/c,l/c]}function Do(e){let n=0;for(let r=0;r<e.length;r++){let[t,o]=e[r],[s,i]=e[(r+1)%e.length];n+=t*i-s*o}return Math.abs(n)/2}function zo(e,n,r){if(!e||e.length<3)return null;let t=e.map(a=>[a[1]*n,a[0]*n,a[2]*r]),o=Fo(t);if(!o)return null;let s=1/0,i=0;for(let a=0;a<t.length;a++){let l=t[a],c=t[(a+1)%t.length],u=[c[0]-l[0],c[1]-l[1],c[2]-l[2]],f=u[0]*o[0]+u[1]*o[1]+u[2]*o[2],g=[u[0]-f*o[0],u[1]-f*o[1],u[2]-f*o[2]],m=Math.hypot(g[0],g[1],g[2]);if(m<=1e-9)continue;let d=[g[0]/m,g[1]/m,g[2]/m],p=[o[1]*d[2]-o[2]*d[1],o[2]*d[0]-o[0]*d[2],o[0]*d[1]-o[1]*d[0]],h=Math.hypot(p[0],p[1],p[2]);if(h<=1e-9)continue;let x=[p[0]/h,p[1]/h,p[2]/h],y=t.map(V=>{let R=V[0]-l[0],E=V[1]-l[1],F=V[2]-l[2];return[R*d[0]+E*d[1]+F*d[2],R*x[0]+E*x[1]+F*x[2]]}),M=1/0,P=1/0,b=-1/0,A=-1/0;for(let[V,R]of y)M=Math.min(M,V),P=Math.min(P,R),b=Math.max(b,V),A=Math.max(A,R);let w=b-M,I=A-P,C=Math.max(1,Math.ceil(w))*Math.max(1,Math.ceil(I)),v=Do(y);v<=1e-9||C<s&&(s=C,i=v)}return!Number.isFinite(s)||i<=1e-9?null:{elementArea:s,polygonArea:i}}function Go(e,n={}){let r=n.tileSize??100,t=n.layerElevation??r,o=n.texturedOnly??!0,s=0,i=0,a=0,l=0,c=0;for(let f of e){let g=!!f.texture;if(o&&!g)continue;let m=zo(f.vertices,r,t);m&&(s+=1,g&&(i+=1),a+=m.elementArea,l+=m.polygonArea,c=Math.max(c,Math.max(0,m.elementArea-m.polygonArea)/m.elementArea))}let u=Math.max(0,a-l);return{totalPolygons:e.length,measuredPolygons:s,texturedPolygons:i,elementArea:a,polygonArea:l,transparentArea:u,transparentRatio:a>0?u/a:0,overdrawRatio:l>0?a/l:0,worstTransparentRatio:c}}function Je(e,n,r,t){let o=n*Math.PI/180,s=r*Math.PI/180,i=t*Math.PI/180,[a,l,c]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[a,l]=[a*u-l*f,a*f+l*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[a,c]=[a*u+c*f,-a*f+c*u]}if(o!==0){let u=Math.cos(o),f=Math.sin(o);[l,c]=[l*u-c*f,l*f+c*u]}return[a,l,c]}function Uo(e,n){let r=-n[0]*Math.PI/180,t=-n[1]*Math.PI/180,o=-n[2]*Math.PI/180,[s,i,a]=e;if(r!==0){let l=Math.cos(r),c=Math.sin(r);[i,a]=[i*l-a*c,i*c+a*l]}if(t!==0){let l=Math.cos(t),c=Math.sin(t);[s,a]=[s*l+a*c,-s*c+a*l]}if(o!==0){let l=Math.cos(o),c=Math.sin(o);[s,i]=[s*l-i*c,s*c+i*l]}return[s,i,a]}var Un=Math.PI/180,ze=180/Math.PI,$o=[1,0,0,0];function Xo(e,n){let[r,t,o,s]=e,[i,a,l,c]=n;return[r*i-t*a-o*l-s*c,r*a+t*i+o*c-s*l,r*l-t*c+o*i+s*a,r*c+t*l-o*a+s*i]}function jo(e,n){let r=n*.5,t=Math.sin(r);return[Math.cos(r),e[0]*t,e[1]*t,e[2]*t]}function Yo(e){let n=e[0]*Un,r=e[1]*Un,t=e[2]*Un,o=Math.cos(n*.5),s=Math.sin(n*.5),i=Math.cos(r*.5),a=Math.sin(r*.5),l=Math.cos(t*.5),c=Math.sin(t*.5);return[o*i*l-s*a*c,s*i*l+o*a*c,o*a*l-s*i*c,o*i*c+s*a*l]}function Ho(e){let[n,r,t,o]=e,s=2*(r*o+n*t),i=2*(t*o-n*r),a=1-2*(r*r+t*t),l=2*(r*t-n*o),c=1-2*(t*t+o*o),u=Math.max(-1,Math.min(1,s)),f=Math.asin(u);return Math.abs(u)<.99999?[Math.atan2(-i,a)*ze,f*ze,Math.atan2(-l,c)*ze]:[Math.atan2(2*(t*o+n*r),1-2*(r*r+o*o))*ze,f*ze,0]}var en=50;function Ko(e){if(typeof e=="number")return e===0?void 0:e<0?-1:1;if(typeof e=="boolean")return e?-1:1}var Te={target:[0,0,0],rotX:65,rotY:45,zoom:.65,distance:0},Nt=100,Ft=1e4,Re=e=>Math.round(e*Nt)/Nt,Wo=e=>Math.round(e*Ft)/Ft;function Zo(e={}){let n={target:e.target??[...Te.target],rotX:e.rotX??Te.rotX,rotY:e.rotY??Te.rotY,zoom:e.zoom??Te.zoom,distance:e.distance??Te.distance};function r(o){o.target!==void 0&&(n.target=[Re(o.target[0]),Re(o.target[1]),Re(o.target[2])]),o.rotX!==void 0&&(n.rotX=Re(o.rotX)),o.rotY!==void 0&&(n.rotY=Re(o.rotY)),o.zoom!==void 0&&(n.zoom=Wo(o.zoom)),o.distance!==void 0&&(n.distance=Re(o.distance))}function t(o={}){let i=(o.cols??0)*50,a=(o.rows??0)*50,[l,c,u]=n.target,f=c*50,g=l*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, ${-g}px, ${-m}px)`,width:`${i}px`,height:`${a}px`}}return{state:n,update:r,getStyle:t}}var xe=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],nn=(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]],Ge=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],$n=e=>Math.hypot(e[0],e[1],e[2]),he=(e,n)=>e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2];function Qo(e,n){let r=`${e[0]},${e[1]},${e[2]}`,t=`${n[0]},${n[1]},${n[2]}`;return r<t?`${r}|${t}`:`${t}|${r}`}function zt(e){if(e.length<3)return null;let n=xe(e[1],e[0]),r=xe(e[2],e[0]),t=nn(n,r),o=$n(t);if(o<1e-12)return null;let s=[t[0]/o,t[1]/o,t[2]/o],i=Ge(s,e[0]);return{normal:s,d:i}}function qo(e,n){return Ge(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.05}function Jo(e,n){return Ge(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.001}function es(e,n,r,t){let o=e.vertices.findIndex(I=>he(I,r)),s=e.vertices.findIndex(I=>he(I,t)),i=n.vertices.findIndex(I=>he(I,r)),a=n.vertices.findIndex(I=>he(I,t));if(o<0||s<0||i<0||a<0)return null;let l=e.vertices.length,c=n.vertices.length,u=(o+1)%l===s,f=(i+1)%c===a;if(u===f)return null;let g=u?s:o,m=u?o:s,d=f?a:i,p=f?i:a,h=!!(e.uvs&&n.uvs),x=[],y=h?[]:void 0,M=g;for(;x.push(e.vertices[M]),y&&y.push(e.uvs[M]),M!==m;)M=(M+1)%l;for(M=d;x.push(n.vertices[M]),y&&y.push(n.uvs[M]),M!==p;)M=(M+1)%c;let P=[],b=y?[]:void 0;for(let I=0;I<x.length;I++)(P.length===0||!he(x[I],P[P.length-1]))&&(P.push(x[I]),b&&y&&b.push(y[I]));if(P.length>1&&he(P[0],P[P.length-1])&&(P.pop(),b?.pop()),h)return Dt(P,b);let A=[],w=b?[]:void 0;for(let I=0;I<P.length;I++){let C=P[(I-1+P.length)%P.length],v=P[I],V=P[(I+1)%P.length],R=nn(xe(v,C),xe(V,C));$n(R)>1e-9&&(A.push(v),w&&b&&w.push(b[I]))}return A.length<3?null:Dt(A,w)}function ns(e,n){let r=e.length,t=0;for(let o=0;o<r;o++){let s=e[o],i=e[(o+1)%r],a=e[(o+2)%r],l=xe(i,s),c=xe(a,i),u=Ge(nn(l,c),n);if(Math.abs(u)<1e-9)continue;let f=u>0?1:-1;if(t===0)t=f;else if(f!==t)return!1}return!0}function ts(e,n){if(e.length<3)return!1;let r=zt(e);if(!r)return!1;for(let t of e)if(Math.abs(Ge(r.normal,t)-r.d)>n)return!1;return!0}function rs(e){return e.map(n=>({vertices:n.vertices.map(r=>[...r]),uvs:n.uvs.map(r=>[...r])}))}function os(e,n){let r=[];for(let t=1;t<e.length-1;t++)r.push({vertices:[[...e[0]],[...e[t]],[...e[t+1]]],uvs:[[...n[0]],[...n[t]],[...n[t+1]]]});return r}function Dt(e,n){for(let r=0;r<e.length;r++){let t=e[r],o=e[(r+1)%e.length],s=e[(r+2)%e.length];if(!($n(nn(xe(o,t),xe(s,t)))<=1e-9))return r===0?{vertices:e,uvs:n}:{vertices:[...e.slice(r),...e.slice(0,r)],uvs:n?[...n.slice(r),...n.slice(0,r)]:void 0}}return{vertices:e,uvs:n}}function le(e){let n=[],r=[];for(let o of e??[]){if(!o||!o.vertices||o.vertices.length<3){o&&n.push(o);continue}let s=o.vertices.map(c=>[c[0],c[1],c[2]]),i=zt(s);if(!i){n.push(o);continue}let a=o.texture&&o.uvs&&o.uvs.length===s.length?o.uvs.map(c=>[c[0],c[1]]):void 0,l=o.texture&&a?o.textureTriangles?.length?rs(o.textureTriangles):os(s,a):void 0;r.push({vertices:s,uvs:a,color:o.color??"#cccccc",texture:o.texture,textureTriangles:l,normal:i.normal,d:i.d,alive:!0,data:o.data})}let t=()=>{let o=new Map;for(let a=0;a<r.length;a++){let l=r[a];if(!l.alive)continue;let c=l.vertices.length;for(let u=0;u<c;u++){let f=l.vertices[u],g=l.vertices[(u+1)%c],m=Qo(f,g),d=o.get(m);d||(d=[],o.set(m,d)),d.push(a)}}let s=!1,i=(a,l)=>{for(let c=0;c<a.vertices.length;c++){let u=a.vertices[c],f=a.vertices[(c+1)%a.vertices.length];for(let g=0;g<l.vertices.length;g++){let m=l.vertices[g],d=l.vertices[(g+1)%l.vertices.length];if(he(u,d)&&he(f,m))return[u,f]}}return null};for(let[,a]of o){if(a.length<2)continue;let[l,c]=a;if(l===c)continue;let u=r[l],f=r[c];if(!u.alive||!f.alive||u.color!==f.color||u.texture!==f.texture)continue;let g=!!(u.texture||f.texture);if(g&&(!u.textureTriangles||!f.textureTriangles)||!!u.uvs!=!!f.uvs||(g?!Jo(u,f):!qo(u,f)))continue;let m=i(u,f);if(!m)continue;let[d,p]=m,h=es(u,f,d,p);h&&ts(h.vertices,.001)&&ns(h.vertices,u.normal)&&(u.vertices=h.vertices,u.uvs=h.uvs,u.textureTriangles=g?[...u.textureTriangles??[],...f.textureTriangles??[]]:void 0,f.alive=!1,s=!0)}return s};for(;t(););for(let o of r){if(!o.alive)continue;let s={vertices:o.vertices,color:o.color};o.texture&&(s.texture=o.texture),o.uvs&&(s.uvs=o.uvs),o.textureTriangles?.length&&(s.textureTriangles=o.textureTriangles),o.data&&(s.data=o.data),n.push(s)}return n}var ss=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],is=(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]],me=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2];function cs(e,n){let r=e.vertices;if(!r||r.length<3)return null;let t=0,o=0,s=0;for(let p=0;p<r.length;p++){let h=r[p],x=r[(p+1)%r.length];t+=(h[1]-x[1])*(h[2]+x[2]),o+=(h[2]-x[2])*(h[0]+x[0]),s+=(h[0]-x[0])*(h[1]+x[1])}let i=Math.hypot(t,o,s);if(i<1e-12)return null;let a=[t/i,o/i,s/i],l=0,c=0,u=0;for(let p of r)l+=p[0],c+=p[1],u+=p[2];let f=1/r.length,g=[l*f,c*f,u*f],m=me(a,g),d=i*.5;return{index:n,polygon:e,normal:a,d:m,centroid:g,area:d,local2D:null,bbox2D:null,basis:null}}function as(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],r=n[0]-e[0]*me(n,e),t=n[1]-e[1]*me(n,e),o=n[2]-e[2]*me(n,e),s=Math.hypot(r,t,o);r/=s,t/=s,o/=s;let i=[r,t,o],a=is(e,i);return{u:i,v:a}}function ls(e){if(e.local2D)return;let n=as(e.normal),r=[],t=1/0,o=1/0,s=-1/0,i=-1/0;for(let a of e.polygon.vertices){let l=me(a,n.u),c=me(a,n.v);r.push([l,c]),l<t&&(t=l),l>s&&(s=l),c<o&&(o=c),c>i&&(i=c)}e.local2D=r,e.bbox2D={min:[t,o],max:[s,i]},e.basis=n}function us(e,n){let r=[],t=1/0,o=1/0,s=-1/0,i=-1/0;for(let a of e.vertices){let l=me(a,n.u),c=me(a,n.v);r.push([l,c]),l<t&&(t=l),l>s&&(s=l),c<o&&(o=c),c>i&&(i=c)}return{local2D:r,bbox2D:{min:[t,o],max:[s,i]}}}function fs(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 Gt(e,n){let r=!1;for(let t=0,o=n.length-1;t<n.length;o=t++){let s=n[t],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]&&(r=!r)}return r}function Ut(e){let n=0,r=0;for(let t of e)n+=t[0],r+=t[1];return[n/e.length,r/e.length]}function ms(e,n){let r=Ut(e),t=Ut(n),o=1e-4,s=0;for(let c of e){let u=[c[0]+(r[0]-c[0])*o,c[1]+(r[1]-c[1])*o];Gt(u,n)&&s++}let i=0;for(let c of n){let u=[c[0]+(t[0]-c[0])*o,c[1]+(t[1]-c[1])*o];Gt(u,e)&&i++}let a=s/e.length,l=i/n.length;return Math.max(a,l)}function gs(e,n){let r=e.normal[0],t=e.normal[1],o=e.normal[2],s=Math.abs(r),i=Math.abs(t),a=Math.abs(o),l=r;i>s&&i>a?l=t:a>s&&a>i&&(l=o),l<0&&(r=-r,t=-t,o=-o);let c=Math.round(r/.05),u=Math.round(t/.05),f=Math.round(o/.05),g=e.d*(e.normal[0]===r&&e.normal[1]===t&&e.normal[2]===o?1:-1),m=Math.round(g/(n*2));return`${c},${u},${f}|${m}`}function ds(e,n,r,t){let o=me(e.normal,n.normal);if(Math.abs(o)<1-r)return!1;let s=o>0?1:-1;return Math.abs(e.d-s*n.d)<t}function $t(e,n){let r=ss(n,e.centroid);return me(e.normal,r)>0}function tn(e,n){if(!e||e.length<2)return new Set;let r=n?.normalTolerance??.001,t=n?.distanceTolerance??.05,o=n?.overlapFraction??.7,s=[];for(let m=0;m<e.length;m++){let d=cs(e[m],m);d&&s.push(d)}if(s.length<2)return new Set;let i=0,a=0,l=0,c=0;for(let m of s)i+=m.centroid[0]*m.area,a+=m.centroid[1]*m.area,l+=m.centroid[2]*m.area,c+=m.area;let u=c>0?[i/c,a/c,l/c]:[0,0,0],f=new Map;for(let m of s){let d=gs(m,t),p=f.get(d);p||(p=[],f.set(d,p)),p.push(m)}let g=new Set;for(let m of f.values())if(!(m.length<2))for(let d=0;d<m.length;d++){let p=m[d];if(!g.has(p.index))for(let h=d+1;h<m.length;h++){let x=m[h];if(g.has(x.index)||!ds(p,x,r,t))continue;ls(p);let y=us(x.polygon,p.basis);if(!fs(p.bbox2D,y.bbox2D)||ms(p.local2D,y.local2D)<o)continue;let P=$t(p,u),b=$t(x,u),A;if(P&&!b?A=p:b&&!P?A=x:A=p.area<x.area?p:x,g.add(A.index),A===p)break}}return g}function ps(e,n){if(!e||e.length<2)return e??[];let r=tn(e,n);if(r.size===0)return e;let t=[];for(let o=0;o<e.length;o++)r.has(o)||t.push(e[o]);return t}var hs=4,xs=8,ys=.001,bs=1e-9,Ps=1e6,Ms=1e6,ve=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Yn=(e,n)=>[e[0]+n[0],e[1]+n[1],e[2]+n[2]],Xe=(e,n)=>[e[0]*n,e[1]*n,e[2]*n],Ae=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],Ht=(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]],Kt=e=>Math.hypot(e[0],e[1],e[2]);function je(e){let n=Kt(e);return n<=bs?null:[e[0]/n,e[1]/n,e[2]/n]}function $e(e,n){return Math.round(e*n)/n}function ue(e){return $e(e,Ps)}function Xn(e){return $e(e,Ms)}function jn(e,n){return Math.abs(e[0]-n[0])<=1e-7&&Math.abs(e[1]-n[1])<=1e-7}function ge(e){return`${ue(e[0])},${ue(e[1])}`}function Cs(e){let n=ge(e.a),r=ge(e.b);return n<r?`${n}|${r}`:`${r}|${n}`}function Ee(e){return`${e[0]},${e[1]},${e[2]}`}function Wt(e,n){let r=Ee(e),t=Ee(n);return r<t?`${r}|${t}`:`${t}|${r}`}function Hn(e){return e?Object.keys(e).sort().map(n=>`${n}:${String(e[n])}`).join("|"):""}function Xt(e){return[e.color??"#cccccc",e.texture??"",e.uvs?"uv":"plain",Hn(e.data)].join("|")}function Zt(e){let n=e.vertices;if(!n||n.length<3)return null;let r=n[0],t=[0,0,0];for(let s=1;s<n.length-1;s++)t=Yn(t,Ht(ve(n[s],r),ve(n[s+1],r)));let o=je(t);return o?{normal:o,d:Ae(o,r)}:null}function vs(e,n,r){return Ae(e.normal,n.normal)>1-r&&Math.abs(e.d-n.d)<=r}function As(e){return!e.texture&&!e.uvs&&!e.textureTriangles}function Vs(e,n){let r=n[e[0]]?.data,t=Hn(r);for(let o of e)if(Hn(n[o].data)!==t)return;return r?{...r}:void 0}function Ss(e,n){let r=e.map(l=>As(l)?Zt(l):null),t=r.map(Boolean),o=new Map;for(let l=0;l<e.length;l++){if(!r[l])continue;let c=e[l].vertices;for(let u=0;u<c.length;u++){let f=Wt(c[u],c[(u+1)%c.length]),g=o.get(f),m={polygon:l,edge:u};g?g.push(m):o.set(f,[m])}}let s=e.map(()=>new Set);for(let l of o.values())if(!(l.length<2))for(let c=0;c<l.length;c++)for(let u=c+1;u<l.length;u++){let f=l[c].polygon,g=l[u].polygon,m=r[f],d=r[g];!m||!d||Xt(e[f])===Xt(e[g])&&vs(m,d,n)&&(s[f].add(g),s[g].add(f))}let i=new Set,a=[];for(let l=0;l<e.length;l++){if(!t[l]||i.has(l))continue;let c=[],u=[l];for(i.add(l);u.length>0;){let f=u.shift();c.push(f);for(let g of s[f])i.has(g)||(i.add(g),u.push(g))}a.push(c)}return{groups:a}}function ws(e,n){let r=new Set(e),t=new Map;for(let s of e){let i=n[s].vertices;for(let a=0;a<i.length;a++){let l=Wt(i[a],i[(a+1)%i.length]),c=t.get(l),u={polygon:s,edge:a};c?c.push(u):t.set(l,[u])}}let o=[];for(let s of t.values()){let i=s.filter(a=>r.has(a.polygon));if(i.length===1){let a=i[0],l=n[a.polygon].vertices;o.push({a:l[a.edge],b:l[(a.edge+1)%l.length]})}else if(i.length!==2)return null}return o}function Is(e){let n=new Map;for(let r of e)n.set(Ee(r.a),(n.get(Ee(r.a))??0)+1),n.set(Ee(r.b),(n.get(Ee(r.b))??0)+1);for(let r of n.values())if(r%2!==0)return!1;return!0}function Qt(e){let n=[e[0],e[1],e[2]],r=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[r]<0?[-n[0],-n[1],-n[2]]:n}function Rs(e){let n=Qt(e);return`${$e(n[0],1e3)},${$e(n[1],1e3)},${$e(n[2],1e3)}`}function Ts(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],r=ve(n,Xe(e,Ae(n,e)));return je(r)??[1,0,0]}function Es(e,n,r){let t=new Map;for(let s of e){let i=ve(s.b,s.a),a=ve(i,Xe(n,Ae(i,n))),l=je(a);if(!l)continue;let c=Qt(l),u=Rs(c),f=Kt(a),g=t.get(u);g?g.weight+=f:t.set(u,{axis:c,weight:f})}let o=[...t.values()].sort((s,i)=>i.weight-s.weight).slice(0,r).map(s=>s.axis);return o.length===0&&o.push(Ts(n)),o}function jt(e,n,r,t){let o=ve(e,n);return[ue(Ae(o,r)),ue(Ae(o,t))]}function Os(e){let n=[...e].sort((t,o)=>t-o),r=[];for(let t of n)(r.length===0||Math.abs(t-r[r.length-1])>1e-7)&&r.push(t);return r}function Ue(e,n){let[r,t]=e.a,[o,s]=e.b,i=(n-r)/(o-r);return ue(t+(s-t)*i)}function Kn(e){let n=[];for(let t of e)(n.length===0||Math.abs(t[0]-n[n.length-1][0])>1e-7||Math.abs(t[1]-n[n.length-1][1])>1e-7)&&n.push(t);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 r=[];for(let t=0;t<n.length;t++){let o=n[(t-1+n.length)%n.length],s=n[t],i=n[(t+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&&r.push(s)}return r}function on(e){let n=0;for(let r=0;r<e.length;r++){let t=e[r],o=e[(r+1)%e.length];n+=t[0]*o[1]-o[0]*t[1]}return n/2}function sn(e){let n=1/0,r=1/0,t=-1/0,o=-1/0;for(let[s,i]of e)n=Math.min(n,s),r=Math.min(r,i),t=Math.max(t,s),o=Math.max(o,i);return{minX:n,minY:r,maxX:t,maxY:o}}function ks(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 rn(e){return Math.abs(on(e))}function _s(e){if(e.length<3)return!1;let n=0;for(let r=0;r<e.length;r++){let t=e[r],o=e[(r+1)%e.length],s=e[(r+2)%e.length],i=(o[0]-t[0])*(s[1]-o[1])-(o[1]-t[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 Ls(e,n,r){let t=r[0]-n[0],o=r[1]-n[1],s=e[0]-n[0],i=e[1]-n[1],a=t*i-o*s,l=Math.hypot(t,o);if(l<=1e-9||Math.abs(a)>Math.max(1e-8,l*1e-8))return!1;let c=s*t+i*o;return c>=-1e-8&&c<=t*t+o*o+1e-8}function Bs(e,n,r){let t=r[0]-n[0],o=r[1]-n[1],s=t*t+o*o;return s<=1e-12?0:((e[0]-n[0])*t+(e[1]-n[1])*o)/s}function Ns(e,n,r,t){let o=n[0]-e[0],s=n[1]-e[1],i=t[0]-r[0],a=t[1]-r[1],l=Math.hypot(o,s),c=Math.hypot(i,a);if(l<=1e-9||c<=1e-9)return!1;let u=o*(r[1]-e[1])-s*(r[0]-e[0]),f=o*(t[1]-e[1])-s*(t[0]-e[0]),g=o*a-s*i,m=Math.max(1e-8,Math.max(l,c)*1e-8);if(Math.abs(u)>m||Math.abs(f)>m||Math.abs(g)>m)return!1;let d=Math.abs(o)>=Math.abs(s),p=Math.min(d?e[0]:e[1],d?n[0]:n[1]),h=Math.max(d?e[0]:e[1],d?n[0]:n[1]),x=Math.min(d?r[0]:r[1],d?t[0]:t[1]),y=Math.max(d?r[0]:r[1],d?t[0]:t[1]);return Math.min(h,y)-Math.max(p,x)>1e-8}function Fs(e,n){for(let r=0;r<e.length;r++){let t=e[r],o=e[(r+1)%e.length];for(let s=0;s<n.length;s++)if(Ns(t,o,n[s],n[(s+1)%n.length]))return!0}return!1}function Yt(e,n){let r=[];for(let t=0;t<e.length;t++){let o=e[t],s=e[(t+1)%e.length],i=[{t:0,point:o},{t:1,point:s}];for(let l of n)jn(l,o)||jn(l,s)||Ls(l,o,s)&&i.push({t:Bs(l,o,s),point:l});i.sort((l,c)=>l.t-c.t);let a=[];for(let l of i)a.some(c=>Math.abs(c.t-l.t)<=1e-8||jn(c.point,l.point))||a.push(l);for(let l=0;l<a.length-1;l++){let c=a[l].point,u=a[l+1].point;Math.hypot(u[0]-c[0],u[1]-c[1])<=1e-8||r.push({a:[ue(c[0]),ue(c[1])],b:[ue(u[0]),ue(u[1])]})}}return r}function Ds(e,n,r=sn(e),t=sn(n)){if(!ks(r,t)||!Fs(e,n))return null;let o=[...Yt(e,n),...Yt(n,e)],s=new Map;for(let x of o){let y=Cs(x),M=s.get(y);M?M.push(x):s.set(y,[x])}let i=!1,a=[];for(let x of s.values()){if(x.length===1){a.push(x[0]);continue}i=!0;let y=x.filter(P=>ge(P.a)<ge(P.b)).length,M=x.length-y;if(y!==M)return null}if(!i||a.length<3)return null;let l=new Map;for(let x of a){let y=ge(x.a);if(l.has(y))return null;l.set(y,x)}let c=a[0],u=ge(c.a),f=[],g=new Set,m=u;for(let x=0;x<=a.length;x++){let y=l.get(m);if(!y)return null;let M=`${ge(y.a)}>${ge(y.b)}`;if(g.has(M))return null;if(g.add(M),f.push(y.a),m=ge(y.b),m===u)break}if(m!==u||g.size!==a.length)return null;let d=Kn(f);if(d.length<3||!_s(d))return null;let p=rn(d),h=rn(e)+rn(n);return Math.abs(p-h)>Math.max(1e-5,h*1e-5)?null:on(d)>=0?d:[...d].reverse()}function zs(e){let n=e.map(Kn).filter(o=>o.length>=3&&rn(o)>1e-8),r=n.map(sn),t=!0;for(;t;){t=!1;for(let o=0;o<n.length;o++){for(let s=o+1;s<n.length;s++){let i=Ds(n[o],n[s],r[o],r[s]);if(i){n[o]=i,r[o]=sn(i),n.splice(s,1),r.splice(s,1),t=!0;break}}if(t)break}}return n}function Gs(e,n,r){let t=new Map;return o=>{let s=ue(o[0]),i=ue(o[1]),a=`${s},${i}`,l=t.get(a);if(l)return[l[0],l[1],l[2]];let c=Yn(e,Yn(Xe(n,s),Xe(r,i))),u=[Xn(c[0]),Xn(c[1]),Xn(c[2])];return t.set(a,u),[u[0],u[1],u[2]]}}function Us(e,n,r,t,o){let s=r[0]?.a;if(!s)return null;let i=je(ve(o,Xe(t,Ae(o,t))));if(!i)return null;let a=je(Ht(t,i));if(!a)return null;let l=[],c=[];for(let h of r){let x=jt(h.a,s,i,a),y=jt(h.b,s,i,a);Math.hypot(x[0]-y[0],x[1]-y[1])<=1e-7||(l.push({a:x,b:y}),c.push(x[0],y[0]))}let u=Os(c);if(l.length<3||u.length<2)return null;let f=n[e[0]].color,g=Vs(e,n),m=[];for(let h=0;h<u.length-1;h++){let x=u[h],y=u[h+1];if(y-x<=1e-7)continue;let M=(x+y)/2,P=l.filter(b=>{let A=Math.min(b.a[0],b.b[0]),w=Math.max(b.a[0],b.b[0]);return A<M&&M<w&&Math.abs(b.a[0]-b.b[0])>1e-7}).map(b=>({segment:b,yMid:Ue(b,M)})).sort((b,A)=>b.yMid-A.yMid);if(P.length!==0){if(P.length%2!==0)return null;for(let b=0;b<P.length;b+=2){let A=P[b].segment,w=P[b+1].segment,I=Ue(A,x),C=Ue(A,y),v=Ue(w,x),V=Ue(w,y),R=Kn([[x,I],[y,C],[y,V],[x,v]]);if(R.length<3||Math.abs(on(R))<=1e-8)continue;let E=on(R)>0?R:[...R].reverse();m.push(E)}}}if(m.length===0)return null;let d=Gs(s,i,a),p=zs(m).map(h=>({vertices:h.map(d),...f?{color:f}:{},...g?{data:g}:{}}));return le(p)}function $s(e,n,r){let t=ws(e,n);if(!t||t.length<3||!Is(t))return null;let o=Zt(n[e[0]]);if(!o)return null;let s=null;for(let i of Es(t,o.normal,r)){let a=Us(e,n,t,o.normal,i);a&&(!s||a.length<s.length)&&(s=a)}return!s||s.length>=e.length?null:s}function Wn(e,n={}){let r=n.minGroupPolygons??hs,t=n.maxCandidateAxes??xs,o=n.planeEpsilon??ys,s=e??[];if(s.length<r)return s;let{groups:i}=Ss(s,o);if(i.length===0)return s;let a=new Map,l=new Set;for(let u of i){if(u.length<r)continue;let f=$s(u,s,t);if(f){a.set(u[0],f);for(let g of u)l.add(g)}}if(a.size===0)return s;let c=[];for(let u=0;u<s.length;u++){let f=a.get(u);if(f){c.push(...f);continue}l.has(u)||c.push(s[u])}return c}function Xs(e){let n=e.vertices;if(!n||n.length<3)return null;let r=0,t=0,o=0;for(let[E,F,D]of n)r+=E,t+=F,o+=D;let s=1/n.length;r*=s,t*=s,o*=s;let i=n[0],a=n[1],l=n[2],c=a[0]-i[0],u=a[1]-i[1],f=a[2]-i[2],g=l[0]-i[0],m=l[1]-i[1],d=l[2]-i[2],p=u*d-f*m,h=f*g-c*d,x=c*m-u*g,y=Math.hypot(p,h,x);if(y<1e-9)return null;p/=y,h/=y,x/=y;let M=n.length-2,P=new Float64Array(M*9),b=0;for(let E=1;E<n.length-1;E++){let F=n[0],D=n[E],z=n[E+1];P[b++]=F[0],P[b++]=F[1],P[b++]=F[2],P[b++]=D[0],P[b++]=D[1],P[b++]=D[2],P[b++]=z[0],P[b++]=z[1],P[b++]=z[2]}let A=0,w=1/0,I=1/0,C=1/0,v=-1/0,V=-1/0,R=-1/0;for(let[E,F,D]of n){let z=E-r,_=F-t,L=D-o,T=z*z+_*_+L*L;T>A&&(A=T),E<w&&(w=E),E>v&&(v=E),F<I&&(I=F),F>V&&(V=F),D<C&&(C=D),D>R&&(R=D)}return{centroid:[r,t,o],normal:[p,h,x],vertices:n,triFlat:P,bcx:r,bcy:t,bcz:o,br2:A,minX:w,minY:I,minZ:C,maxX:v,maxY:V,maxZ:R}}function js(e,n,r,t,o,s,i,a){let l=i[a],c=i[a+1],u=i[a+2],f=i[a+3]-l,g=i[a+4]-c,m=i[a+5]-u,d=i[a+6]-l,p=i[a+7]-c,h=i[a+8]-u,x=o*h-s*p,y=s*d-t*h,M=t*p-o*d,P=f*x+g*y+m*M;if(P>-1e-9&&P<1e-9)return!1;let b=1/P,A=e-l,w=n-c,I=r-u,C=b*(A*x+w*y+I*M);if(C<0||C>1)return!1;let v=w*m-I*g,V=I*f-A*m,R=A*g-w*f,E=b*(t*v+o*V+s*R);return E<0||C+E>1?!1:b*(d*v+p*V+h*R)>.001}function Ys(e,n,r,t,o,s,i){let a=i.bcx-e,l=i.bcy-n,c=i.bcz-r,u=a*t+l*o+c*s,f=a-u*t,g=l-u*o,m=c-u*s;if(f*f+g*g+m*m>i.br2)return!1;let d=i.triFlat,p=d.length;for(let h=0;h<p;h+=9)if(js(e,n,r,t,o,s,d,h))return!0;return!1}var Ye=9,Hs=6,re=12;function Zn(e,n,r,t,o,s){let i=t-e,a=o-n,l=s-r;return i*a+a*l+l*i}function Ks(e){let n=[];for(let A=0;A<e.length;A++)e[A]&&n.push(A);let r=n.length,t=new Int32Array(r);for(let A=0;A<r;A++)t[A]=n[A];let o=new Float64Array(r),s=new Float64Array(r),i=new Float64Array(r);for(let A=0;A<r;A++){let w=e[t[A]];o[A]=(w.minX+w.maxX)*.5,s[A]=(w.minY+w.maxY)*.5,i[A]=(w.minZ+w.maxZ)*.5}let a=2*Math.max(1,r)+1,l=new Float64Array(a*Ye),c=0,u=new Float64Array(re),f=new Float64Array(re),g=new Float64Array(re),m=new Float64Array(re),d=new Float64Array(re),p=new Float64Array(re),h=new Int32Array(re),x=new Float64Array(re-1),y=new Int32Array(re-1),M=new Float64Array(re-1),P=new Int32Array(re-1);function b(A,w){let I=c++,C=I*Ye,v=w-A,V=1/0,R=1/0,E=1/0,F=-1/0,D=-1/0,z=-1/0;for(let X=A;X<w;X++){let Q=e[t[X]];Q.minX<V&&(V=Q.minX),Q.maxX>F&&(F=Q.maxX),Q.minY<R&&(R=Q.minY),Q.maxY>D&&(D=Q.maxY),Q.minZ<E&&(E=Q.minZ),Q.maxZ>z&&(z=Q.maxZ)}if(l[C]=V,l[C+1]=R,l[C+2]=E,l[C+3]=F,l[C+4]=D,l[C+5]=z,v<=Hs)return l[C+6]=1,l[C+7]=A,l[C+8]=w,I;let _=1/0,L=1/0,T=1/0,B=-1/0,O=-1/0,S=-1/0;for(let X=A;X<w;X++)o[X]<_&&(_=o[X]),o[X]>B&&(B=o[X]),s[X]<L&&(L=s[X]),s[X]>O&&(O=s[X]),i[X]<T&&(T=i[X]),i[X]>S&&(S=i[X]);let k=B-_,N=O-L,U=S-T;if(k===0&&N===0&&U===0)return l[C+6]=1,l[C+7]=A,l[C+8]=w,I;let Y=Zn(V,R,E,F,D,z),G=Y>0?1/Y:0,$=v+1,W=0,q=0;for(let X=0;X<3;X++){let Q=X===0?_:X===1?L:T,ce=X===0?k:X===1?N:U;if(ce===0)continue;let pe=X===0?o:X===1?s:i,Me=re/ce;u.fill(1/0),f.fill(1/0),g.fill(1/0),m.fill(-1/0),d.fill(-1/0),p.fill(-1/0),h.fill(0);for(let j=A;j<w;j++){let Z=(pe[j]-Q)*Me|0;Z>=re&&(Z=re-1);let ae=e[t[j]];ae.minX<u[Z]&&(u[Z]=ae.minX),ae.maxX>m[Z]&&(m[Z]=ae.maxX),ae.minY<f[Z]&&(f[Z]=ae.minY),ae.maxY>d[Z]&&(d[Z]=ae.maxY),ae.minZ<g[Z]&&(g[Z]=ae.minZ),ae.maxZ>p[Z]&&(p[Z]=ae.maxZ),h[Z]++}let Se=1/0,we=1/0,In=1/0,Rn=-1/0,Tn=-1/0,En=-1/0,Vt=0;for(let j=0;j<re-1;j++)u[j]<Se&&(Se=u[j]),m[j]>Rn&&(Rn=m[j]),f[j]<we&&(we=f[j]),d[j]>Tn&&(Tn=d[j]),g[j]<In&&(In=g[j]),p[j]>En&&(En=p[j]),Vt+=h[j],x[j]=Zn(Se,we,In,Rn,Tn,En),y[j]=Vt;let On=1/0,kn=1/0,_n=1/0,Ln=-1/0,Bn=-1/0,Nn=-1/0,St=0;for(let j=re-2;j>=0;j--){let Z=j+1;u[Z]<On&&(On=u[Z]),m[Z]>Ln&&(Ln=m[Z]),f[Z]<kn&&(kn=f[Z]),d[Z]>Bn&&(Bn=d[Z]),g[Z]<_n&&(_n=g[Z]),p[Z]>Nn&&(Nn=p[Z]),St+=h[Z],M[j]=Zn(On,kn,_n,Ln,Bn,Nn),P[j]=St}for(let j=0;j<re-1;j++){if(y[j]===0||P[j]===0)continue;let Z=.125+(x[j]*y[j]+M[j]*P[j])*G;Z<$&&($=Z,W=X,q=Q+(j+1)/Me)}}let ee=W===0?o:W===1?s:i,H=A,K=w-1;for(;H<=K;)if(ee[H]<q)H++;else{let X=t[H];t[H]=t[K],t[K]=X;let Q=o[H];o[H]=o[K],o[K]=Q;let ce=s[H];s[H]=s[K],s[K]=ce;let pe=i[H];i[H]=i[K],i[K]=pe,K--}let te=H;(te===A||te===w)&&(te=A+w>>1),l[C+6]=0;let se=b(A,te),J=b(te,w);return l[I*Ye+7]=se,l[I*Ye+8]=J,I}return r>0&&b(0,r),{data:l,nodeCount:c,polyIndices:t,meta:e}}function qt(e,n,r,t,o,s,i,a,l){if(a.nodeCount===0)return!1;let{data:c,polyIndices:u,meta:f}=a,g=t!==0?1/t:t>=0?1/0:-1/0,m=o!==0?1/o:o>=0?1/0:-1/0,d=s!==0?1/s:s>=0?1/0:-1/0,p=0;for(l[p++]=0;p>0;){let x=l[--p]*Ye,y=(c[x]-e)*g,M=(c[x+3]-e)*g,P=y<M?y:M,b=y<M?M:y,A=(c[x+1]-n)*m,w=(c[x+4]-n)*m,I=A<w?A:w,C=A<w?w:A;if(P>C||I>b)continue;I>P&&(P=I),C<b&&(b=C);let v=(c[x+2]-r)*d,V=(c[x+5]-r)*d,R=v<V?v:V,E=v<V?V:v;if(!(P>E||R>b)&&(E<b&&(b=E),!(b<.001)))if(c[x+6]===1){let F=c[x+7]|0,D=c[x+8]|0;for(let z=F;z<D;z++){let _=u[z];if(_===i)continue;let L=f[_];if(L&&Ys(e,n,r,t,o,s,L))return!0}}else l[p++]=c[x+7]|0,l[p++]=c[x+8]|0}return!1}function Ws(e){let n=(1+Math.sqrt(5))/2,r=new Float64Array(e*3);for(let t=0;t<e;t++){let o=(t+.5)/e,s=Math.sqrt(Math.max(0,1-o*o)),i=2*Math.PI*(t/n);r[t*3]=s*Math.cos(i),r[t*3+1]=s*Math.sin(i),r[t*3+2]=o}return r}function Zs(e){let n=Math.abs(e[0])>.9?0:1,r=Math.abs(e[0])>.9?1:0,t=r*e[2],o=-n*e[2],s=n*e[1]-r*e[0],i=Math.hypot(t,o,s);t/=i,o/=i,s/=i;let a=e[1]*s-e[2]*o,l=e[2]*t-e[0]*s,c=e[0]*o-e[1]*t;return{ux:t,uy:o,uz:s,vx:a,vy:l,vz:c}}function Oe(e,n){let r=n?.samples??8;if(e.length<4||r<1)return e;let t=e.map(Xs),o=Ws(r),s=[],i=Ks(t),a=new Int32Array(Math.max(64,i.nodeCount)),l=192,c=new Float64Array(l);for(let u=0;u<e.length;u++){let f=t[u];if(!f){s.push(e[u]);continue}let g=f.normal[0],m=f.normal[1],d=f.normal[2],p=.001*g,h=.001*m,x=.001*d;{let D=f.centroid[0]+p,z=f.centroid[1]+h,_=f.centroid[2]+x;if(!qt(D,z,_,g,m,d,u,i,a)){s.push(e[u]);continue}}let{ux:y,uy:M,uz:P,vx:b,vy:A,vz:w}=Zs(f.normal),I=f.centroid[0],C=f.centroid[1],v=f.centroid[2],V=f.vertices,R=V.length,E=0;c[E++]=I+p,c[E++]=C+h,c[E++]=v+x;for(let D=0;D<R;D++){let z=V[D];c[E++]=z[0]+(I-z[0])*.08+p,c[E++]=z[1]+(C-z[1])*.08+h,c[E++]=z[2]+(v-z[2])*.08+x}for(let D=0;D<R;D++){let z=V[D],_=V[(D+1)%R],L=(z[0]+_[0])*.5,T=(z[1]+_[1])*.5,B=(z[2]+_[2])*.5;c[E++]=L+(I-L)*.08+p,c[E++]=T+(C-T)*.08+h,c[E++]=B+(v-B)*.08+x}let F=!1;e:for(let D=0;D<o.length;D+=3){let z=o[D],_=o[D+1],L=o[D+2],T=z*y+_*b+L*g,B=z*M+_*A+L*m,O=z*P+_*w+L*d;for(let S=0;S<E;S+=3){let k=c[S],N=c[S+1],U=c[S+2];if(!qt(k,N,U,T,B,O,u,i,a)){F=!0;break e}}}F&&s.push(e[u])}return s}var Qs=3,qs=.03,Js=.02,He={maxAngleDeg:Qs,maxPlaneDisplacement:qs,maxBoundaryDisplacement:Js,isolatedPairs:!1},ke={maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.0725,isolatedPairs:!0},ln=[{maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.02},{maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.0725},{maxAngleDeg:45,maxPlaneDisplacement:1,maxBoundaryDisplacement:.0725}],ei=[4,8,12],ni=300,ti=.3,ri=300,oi=16,si=.015,Jt=2.6,ii=3,ci=96,ai=1800,li=.65,ui=900,fi=300,er=1e-5,ye={minGroupPolygons:2,maxCandidateAxes:24},mi={...ye,maxCandidateAxes:1},gi={maxGap:0,internalBoundaryLength:0,excessBoundaryLength:0};function et(e,n={}){let r=n.meshResolution??"lossy",t={},o=Qn(e,!1,t),s=o,i=oe(o),a=(u,f=oe(u))=>f>=i?!1:(s=u,i=f,!0),l=r==="lossy"&&n.rectCover===void 0?Li(o):n.rectCover,c=an(o,l);if(c!==o&&a(c),r==="lossy"&&n.approximateMerge!==!1){let u=[],f=s,g=null,m=null,d=()=>(g??(g=wi(e)),g),p=()=>(m??(m=cn(d(),f,ke.maxBoundaryDisplacement).metrics),m),h=n.approximateMerge===void 0||n.approximateMerge===!0,x=(v,V=!0)=>!Vi(d(),v.metrics,v.tolerance,V?p():null),y=(v,V,R=!0)=>{let E=d(),F=fn(E,V);return cn(E,v,V,cr(E,F,R?p():null))},M=(v,V)=>{a(v,V)},P=(v,V,R=ke.maxBoundaryDisplacement,E)=>{!h||V>i+ir(i)||u.push({polygons:v,cost:V,maxBoundaryDisplacement:R,metrics:E})},b=n.rectCover!==void 0&&n.rectCover!==!1,A=h&&Pi(o),w=A?[]:nr(n.approximateMerge,h?o:void 0),I=h?di(e):[];for(let v=0;v<w.length;v++){let V=w[v],R=Qn(e,V,t),E=oe(R),F=null,D,z=T=>(F??(F=y(R,V.maxBoundaryDisplacement,T)),F),_=!!V.allowReferenceCracks,L=!0;if(h||V.guard){let T=z(_);D=T.metrics,L=x(T,_)}if(!(!L&&E<i)&&(L&&(M(R,E),P(R,E,V.maxBoundaryDisplacement,D)),b)){let T=an(R,n.rectCover),B=oe(T),O=null,S,k=N=>(O??(O=y(T,V.maxBoundaryDisplacement,N)),O);if(T!==R&&B<i){let N=!0;if(h||V.guard){let U=k(_);S=U.metrics,N=x(U,_)}N&&(M(T,B),P(T,B,V.maxBoundaryDisplacement,S))}}}if(h&&I.length===0&&bi(o))return s;if(h)for(let v of I){let V=Bi(v,t),R=V.baseline,E=oe(R),F=!0,D=null,z,_=()=>(D??(D=y(R,ke.maxBoundaryDisplacement)),D);if(E<i){let T=_();z=T.metrics,F=x(T)}if(F&&(M(R,E),P(R,E,void 0,z)),b){let T=an(R,n.rectCover);if(T!==R){let B=oe(T),O=null,S,k=()=>(O??(O=y(T,ke.maxBoundaryDisplacement)),O),N=()=>{let U=k();return S=U.metrics,x(U)};(B>=i||N())&&(M(T,B),P(T,B,void 0,S))}}let L=A?[]:nr(n.approximateMerge,V.baseline);for(let T of L){let B=Qn(v,T,V),O=oe(B),S=null,k,N=G=>(S??(S=y(B,T.maxBoundaryDisplacement,G)),S),U=!!T.allowReferenceCracks,Y=!0;if(h||T.guard){let G=N(U);k=G.metrics,Y=x(G,U)}if(!(!Y&&O<i)&&(Y&&(M(B,O),P(B,O,T.maxBoundaryDisplacement,k)),b)){let G=an(B,n.rectCover),$=oe(G),W=null,q,ee=H=>(W??(W=y(G,T.maxBoundaryDisplacement,H)),W);if(G!==B&&$<i){let H=!0;if(h||T.guard){let K=ee(U);q=K.metrics,H=x(K,U)}H&&(M(G,$),P(G,$,T.maxBoundaryDisplacement,q))}}}}if(h&&!A)for(let v of ln){let V=pr({...v,isolatedPairs:!0}),R=$i(s,V);if(R===s)continue;let E=oe(R);if(E>=i)continue;let F=y(R,V.maxBoundaryDisplacement);x(F)&&(M(R,E),P(R,E,V.maxBoundaryDisplacement,F.metrics))}let C=Ci(u,s,i,v=>(v.metrics??(v.metrics=cn(d(),v.polygons,v.maxBoundaryDisplacement).metrics),v.metrics),()=>cn(d(),s,ke.maxBoundaryDisplacement).metrics);C&&(s=C.polygons,i=C.cost)}return s}function di(e){let n=pi(e);if(n.eligiblePolygons<24||n.colorCount<8)return[];let r=[],t=new Set;for(let o of ei){let s=hi(e,o,n.colorCount);if(!s)continue;let i=yi(s);t.has(i)||(t.add(i),r.push(s))}return r}function pi(e){let n=new Set,r=0;for(let t of e)t.texture||t.material?.texture||t.uvs||t.textureTriangles?.length||or(t.color)&&(r+=1,n.add(t.color??"#cccccc"));return{eligiblePolygons:r,colorCount:n.size}}function hi(e,n,r){let t=!1,o=new Set,s=e.map(i=>{if(i.texture||i.material?.texture||i.uvs||i.textureTriangles?.length)return i;let a=or(i.color);if(!a)return i;let l=xi([Math.round(a[0]/n)*n,Math.round(a[1]/n)*n,Math.round(a[2]/n)*n]);return o.add(l),l===i.color?i:(t=!0,{...i,color:l})});return!t||o.size>=r?null:s}function or(e){let n=e??"#cccccc",r=/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i.exec(n);if(r)return[parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16),parseInt(r[3]+r[3],16)];let t=/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(n);return t?[parseInt(t[1],16),parseInt(t[2],16),parseInt(t[3],16)]:null}function xi(e){return`#${e.map(n=>Math.max(0,Math.min(255,Math.round(n))).toString(16).padStart(2,"0")).join("")}`}function yi(e){return e.map(n=>n.color??"").join("|")}function nr(e,n){if(e&&e!==!0)return typeof e.isolatedPairs=="boolean"?[{...e,guard:e.isolatedPairs===!1}]:[{...e,isolatedPairs:!0,guard:!1},{...e,isolatedPairs:!1,guard:!0}];if(n&&sr(n))return[{...ln[0],isolatedPairs:!0,guard:!1,allowReferenceCracks:!0}];let r=[],t=new Set,o=n&&n.length>ri?[!0]:[!0,!1];for(let s=0;s<ln.length;s++){let i=ln[s];for(let a of o){let l={...i,isolatedPairs:a,guard:s>0||a===!1,allowReferenceCracks:!0},c=[l.maxAngleDeg,l.maxPlaneDisplacement,l.maxBoundaryDisplacement,l.isolatedPairs].join("|");t.has(c)||(t.add(c),r.push(l))}}return r.length===0?[{...ke,guard:!1}]:r}function sr(e){return e.length<ni?!1:nt(e)/e.length<=ti}function bi(e){return e.length>=ui&&sr(e)}function Pi(e){if(e.length<fi)return!1;for(let n of e){if(n.vertices.length!==4||n.texture||n.material?.texture||n.textureTriangles?.length)return!1;let r=_e(n);if(!r||!Mi(r.normal))return!1}return!0}function Mi(e){let n=Math.abs(e[0]),r=Math.abs(e[1]),t=Math.abs(e[2]),o=Math.max(n,r,t);return o>=1-er&&n+r+t-o<=er}function oe(e){let n=0;for(let r of e){let t=r.vertices.length,o=t<=4?0:Math.min(4,t-4)*.12,s=r.texture||r.material?.texture||r.textureTriangles?.length?.15:0;n+=1+o+s}return n}function ir(e){return Math.max(oi,e*si)}function Ci(e,n,r,t,o){if(e.length===0)return null;let s=ir(r),i=e.find(u=>u.polygons===n),a=null,l=null,c=null;for(let u of e){if(u.polygons===n||u.cost>r+s)continue;let f=t(u);a??(a=i?t(i):o()),Ai(f,a)&&(!l||!c||vi(u,f,l,c)<0)&&(l=u,c=f)}return l}function vi(e,n,r,t){return n.maxGap-t.maxGap||n.internalBoundaryLength-t.internalBoundaryLength||n.excessBoundaryLength-t.excessBoundaryLength||e.cost-r.cost}function Ai(e,n){let r=Math.max(5e-4,n.maxGap*.02);if(e.maxGap<n.maxGap-r)return!0;if(e.maxGap>n.maxGap+r)return!1;let t=Math.max(8,n.internalBoundaryLength*.01);if(e.internalBoundaryLength<n.internalBoundaryLength-t)return!0;if(e.internalBoundaryLength>n.internalBoundaryLength+t)return!1;let o=Math.max(8,n.excessBoundaryLength*.01);return e.excessBoundaryLength<n.excessBoundaryLength-o}function Vi(e,n,r,t=null){return un(n,cr(e,r,t))}function cr(e,n,r=null){if(!r)return{maxGap:1/0,internalBoundaryLength:0,excessBoundaryLength:n};let t=Math.max(n*.1,1e-6),o=r.maxGap+t,s=n<=.08?Math.max(o,Math.min(n*.75,.04)):o,i=Math.max(n*2,r.internalBoundaryLength*.15),a=Math.max(n*2,r.excessBoundaryLength*.15);return{maxGap:s,internalBoundaryLength:r.internalBoundaryLength+i,excessBoundaryLength:r.excessBoundaryLength+a}}function un(e,n){return e.maxGap>n.maxGap||e.internalBoundaryLength>n.internalBoundaryLength||e.excessBoundaryLength>n.excessBoundaryLength}function Si(e,n,r=0,t=fn(e,r),o){let s=e.edges,i=ar(n),a=fn(e,r),l=t>0?Ri(e,t):null,c={...gi,excessBoundaryLength:Math.max(0,i.boundaryLength-s.boundaryLength)};if(o&&un(c,o))return{metrics:c,tolerance:a};for(let u of i.boundarySegments){let f=Le(u.a,u.b);if(s.boundaryKeys.has(f))continue;if(s.internalKeys.has(f)){if(c.internalBoundaryLength+=de(u.a,u.b),o&&un(c,o))break;continue}let g=l?Oi(u,l,t):null;if(g!==null&&(c.maxGap=Math.max(c.maxGap,g),c.internalBoundaryLength+=de(u.a,u.b),o&&un(c,o)))break}return{metrics:c,tolerance:a}}function cn(e,n,r=0,t){return Si(e,n,r,Ii(e,r),t)}function wi(e){let n=Ti(e),r=n>0?Math.min(.08,Math.max(.001,n*.001)):0;return{edges:ar(e),baseTolerance:r,polygonCount:e.length,indexes:new Map}}function fn(e,n=0){return Math.max(e.baseTolerance,n*1.05)}function Ii(e,n=0){return Math.max(fn(e,n),e.baseTolerance*Jt,n*Jt)}function Ri(e,n){let r=n.toFixed(6),t=e.indexes.get(r);if(t)return t;let o=Ei(e.edges.internalSegments,n);return e.indexes.set(r,o),o}function ar(e){let n=new Map;for(let a of e)for(let l=0;l<a.vertices.length;l++){let c=a.vertices[l],u=a.vertices[(l+1)%a.vertices.length],f=Le(c,u),g=n.get(f);g?g.count+=1:n.set(f,{count:1,a:c,b:u})}let r=new Set,t=new Set,o=[],s=[],i=0;for(let[a,l]of n){let c={a:l.a,b:l.b};l.count===1?(r.add(a),o.push(c),i+=de(c.a,c.b)):(t.add(a),s.push(c))}return{boundaryKeys:r,internalKeys:t,boundarySegments:o,internalSegments:s,boundaryLength:i}}function Ti(e){let n=1/0,r=1/0,t=1/0,o=-1/0,s=-1/0,i=-1/0;for(let a of e)for(let[l,c,u]of a.vertices)n=Math.min(n,l),r=Math.min(r,c),t=Math.min(t,u),o=Math.max(o,l),s=Math.max(s,c),i=Math.max(i,u);return Number.isFinite(n)?Math.hypot(o-n,s-r,i-t):0}function Ei(e,n){let r=Math.max(n*2,1e-6),t=new Map;for(let o of e){let[s,i,a]=lr(o,r),l=ur(s,i,a),c=t.get(l);c?c.push(o):t.set(l,[o])}return{cellSize:r,cells:t}}function Oi(e,n,r){let[t,o,s]=lr(e,n.cellSize),i=null;for(let a=-1;a<=1;a++)for(let l=-1;l<=1;l++)for(let c=-1;c<=1;c++){let u=n.cells.get(ur(t+a,o+l,s+c));if(u)for(let f of u){let g=ki(e,f);g<=r&&(i=i===null?g:Math.min(i,g))}}return i}function lr(e,n){return[Math.floor((e.a[0]+e.b[0])/2/n),Math.floor((e.a[1]+e.b[1])/2/n),Math.floor((e.a[2]+e.b[2])/2/n)]}function ur(e,n,r){return`${e},${n},${r}`}function ki(e,n){return Math.min(Math.max(de(e.a,n.a),de(e.b,n.b)),Math.max(de(e.a,n.b),de(e.b,n.a)))}function an(e,n){if(n===!1)return e;let r=_i(e,n);if(!r)return e;let t=Wn(e,r);return t.length<e.length?t:e}function _i(e,n){if(n&&n!==!0)return n;let r=e.length;return r>2200?null:r>1200?{...ye,maxCandidateAxes:Math.min(ye.maxCandidateAxes??24,2)}:r>300&&nt(e)<=ci?{...ye,maxCandidateAxes:Math.min(ye.maxCandidateAxes??24,2)}:r>900?{...ye,maxCandidateAxes:Math.min(ye.maxCandidateAxes??24,4)}:ye}function Li(e){return e.length>ai||e.length===0||nt(e)/e.length<li?!1:mi}function nt(e){let n=0;for(let r of e)r.vertices.length===3&&(n+=1);return n}function tt(e,n){return n==null?e:n.map(r=>e[r]).filter(r=>!!r)}function fr(e,n){if(n===e||n.length===e.length&&n.every((o,s)=>o===e[s]))return null;let r=new Set(n),t=[];for(let o=0;o<e.length;o++)r.has(e[o])&&t.push(o);return t.length===e.length?null:t}function mr(e,n){if(n?.deduped)return n.deduped;let r=n?.dedupedIndices;if(r===void 0){let o=tn(e);if(o.size===0)r=null;else{r=[];for(let s=0;s<e.length;s++)o.has(s)||r.push(s)}n&&(n.dedupedIndices=r)}let t=tt(e,r);return n&&(n.deduped=t),t}function gr(e,n){if(n?.interior)return n.interior;let r=n?.interiorIndices;if(r===void 0){let o=Oe(e);r=fr(e,o),n&&(n.interiorIndices=r)}let t=tt(e,r);return n&&(n.interior=t),t}function Bi(e,n){let r={dedupedIndices:n.dedupedIndices,interiorIndices:n.interiorIndices,snappedInteriorIndices:n.snappedInteriorIndices},t=mr(e,r);n.snapped&&n.snapped.length===t.length&&(r.snapped=Ni(t,n.snapped));let o=gr(t,r);return r.baseline=le(o),r}function Ni(e,n){return e.map((r,t)=>{let o=n[t];return o?{...r,vertices:o.vertices,...o.uvs?{uvs:o.uvs}:{},...o.textureTriangles?{textureTriangles:o.textureTriangles}:{}}:r})}function Qn(e,n,r){let t=mr(e,r),o=gr(t,r),s=r?.baseline??le(o);if(r&&!r.baseline&&(r.baseline=s),!n)return s;let i=n===!0?He:pr(n);if(i.isolatedPairs){let l=Di(Fi(t,r),i),c=le(l);return c.length<s.length?c:s}let a=le(Oe(Ji(t,i,r)));return a.length<s.length?a:s}function dr(e,n){return n?(n.snapped||(n.snapped=gn(e)),n.snapped):gn(e)}function Fi(e,n){if(!n)return Oe(gn(e));if(!n.snappedInterior){let r=dr(e,n);if(n.snappedInteriorIndices===void 0){let t=Oe(r);n.snappedInteriorIndices=fr(r,t),n.snappedInterior=t}else n.snappedInterior=tt(r,n.snappedInteriorIndices)}return n.snappedInterior}function pr(e){return{maxAngleDeg:e.maxAngleDeg??He.maxAngleDeg,maxPlaneDisplacement:e.maxPlaneDisplacement??He.maxPlaneDisplacement,maxBoundaryDisplacement:e.maxBoundaryDisplacement??He.maxBoundaryDisplacement,isolatedPairs:e.isolatedPairs??He.isolatedPairs}}function Di(e,n){let r=e.map(f=>{let g=_e(f);return g?{polygon:f,normal:g.normal,area:g.area,materialKey:ot(f)}:null}),t=new Map;for(let f=0;f<e.length;f++){let g=e[f];if(!(g.vertices.length!==3||!r[f]))for(let m=0;m<g.vertices.length;m++){let d=Le(g.vertices[m],g.vertices[(m+1)%g.vertices.length]),p=t.get(d);p?p.push(f):t.set(d,[f])}}let o=[];for(let f of t.values()){if(f.length!==2)continue;let[g,m]=f,d=qi(g,m,e,r,n);d&&o.push(d)}let s=new Set,i=new Map,a=new Set,l=zi(o),c=hn(l.flatMap(f=>f.vertexMoves));for(let f of l){s.add(f.a),s.add(f.b);let g=Math.min(f.a,f.b);i.set(g,f.polygon),a.add(Math.max(f.a,f.b))}let u=[];for(let f=0;f<e.length;f++){let g=i.get(f);if(g){u.push(g);continue}a.has(f)||u.push(e[f])}return c.size>0?xn(u,c):u}function zi(e){return e.length>3e3?Gi(e):Ui(e)}function Gi(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 r=[...e].sort((s,i)=>{let a=(n.get(s.a)??0)+(n.get(s.b)??0),l=(n.get(i.a)??0)+(n.get(i.b)??0);return a-l||s.score-i.score}),t=new Set,o=[];for(let s of r)t.has(s.a)||t.has(s.b)||(t.add(s.a),t.add(s.b),o.push(s));return o}function Ui(e){let n=new Map;for(let c=0;c<e.length;c++){let u=e[c],f=n.get(u.a);f?f.push(c):n.set(u.a,[c]);let g=n.get(u.b);g?g.push(c):n.set(u.b,[c])}let r=[],t=new Array(e.length).fill(!0),o=new Map,s=new Jn;for(let[c,u]of n)o.set(c,u.length);let i=c=>(o.get(c.a)??0)+(o.get(c.b)??0),a=c=>{let u=e[c];s.push({degree:i(u),score:u.score,index:c})},l=(c,u)=>{if(!t[c])return;t[c]=!1;let f=e[c];for(let g of[f.a,f.b])o.set(g,(o.get(g)??0)-1),u.add(g)};for(let c=0;c<e.length;c++)a(c);for(;s.size()>0;){let c=s.pop();if(!t[c.index])continue;let u=e[c.index];if(i(u)!==c.degree){a(c.index);continue}r.push(u);let g=new Set;for(let m of[u.a,u.b])for(let d of n.get(m)??[])l(d,g);for(let m of g)for(let d of n.get(m)??[])t[d]&&a(d)}return r}var Jn=class{constructor(){this.items=[]}size(){return this.items.length}push(n){this.items.push(n);let r=this.items.length-1;for(;r>0;){let t=r-1>>1;if(qn(this.items[t],this.items[r])<=0)break;[this.items[t],this.items[r]]=[this.items[r],this.items[t]],r=t}}pop(){if(this.items.length===0)return null;let n=this.items[0],r=this.items.pop();if(this.items.length>0){this.items[0]=r;let t=0;for(;;){let o=t*2+1,s=o+1,i=t;if(o<this.items.length&&qn(this.items[o],this.items[i])<0&&(i=o),s<this.items.length&&qn(this.items[s],this.items[i])<0&&(i=s),i===t)break;[this.items[t],this.items[i]]=[this.items[i],this.items[t]],t=i}}return n}};function qn(e,n){return e.degree-n.degree||e.score-n.score||e.index-n.index}function $i(e,n){let r=e,t=oe(r),o=Hi(r);for(let s=0;s<ii;s++){let i=Xi(r,n,o);if(!i)break;let a=oe(i.polygons);if(a>=t)break;r=i.polygons,t=a,o=i.origins}return r===e?e:r}function Xi(e,n,r){let t=e.map(g=>{let m=_e(g);return m?{polygon:g,normal:m.normal,area:m.area,materialKey:ot(g)}:null}),o=new Map;for(let g=0;g<e.length;g++){let m=e[g];if(!(!t[g]||m.vertices.length<3))for(let d=0;d<m.vertices.length;d++){let p=Le(m.vertices[d],m.vertices[(d+1)%m.vertices.length]),h=o.get(p);h?h.push({polygon:g,edge:d}):o.set(p,[{polygon:g,edge:d}])}}let s=[];for(let g of o.values()){if(g.length!==2)continue;let[m,d]=g,p=ji(m.polygon,m.edge,d.polygon,d.edge,e,t,n);p&&s.push(p)}if(s.length===0)return null;s.sort((g,m)=>m.score-g.score);let i=new Set,a=[];for(let g of s)i.has(g.a)||i.has(g.b)||(i.add(g.a),i.add(g.b),a.push(g));if(a.length===0)return null;let l=hn(a.flatMap(g=>g.vertexMoves));if(!Yi(l,r,n.maxBoundaryDisplacement))return null;let c=xn(e,l),u=Ki(e,l,r),f=le(c);return oe(f)<oe(e)?{polygons:f,origins:Wi(f,u)}:null}function ji(e,n,r,t,o,s,i){let a=o[e],l=o[r],c=s[e],u=s[r];if(!c||!u||a.vertices.length===3&&l.vertices.length===3||!Cr(a,l,c,u))return null;let f=Math.abs(fe(c.normal,u.normal)),g=Math.cos(i.maxAngleDeg*Math.PI/180);if(f<g)return null;let m=tr(a,l,n)??tr(l,a,t);if(!m||m.length<4||m.length>10)return null;let d=xr(m);if(!d)return null;let p=0,h=0;for(let I of m){let C=Math.abs(yn(I,d));p=Math.max(p,C),h+=C*C}if(p>Math.min(i.maxPlaneDisplacement,i.maxBoundaryDisplacement))return null;let x=m.map(I=>bn(I,d));if(!yr(x,d.normal))return null;let y=_e({vertices:x});if(!y||fe(y.normal,c.normal)<.2||fe(y.normal,u.normal)<.2)return null;let M=[...m.map((I,C)=>({key:ne(I),target:x[C]})),...hr([a,l],d)],P=xn([a,l],hn(M)),b=oe([a,l]),A=oe(le(P));if(A>=b)return null;let w=b-A-(h/m.length+p*.25+(1-f)*.1);return w<=0?null:{a:e,b:r,vertexMoves:M,score:w}}function tr(e,n,r){let t=e.vertices,o=n.vertices,s=t[r],i=t[(r+1)%t.length],a=-1;for(let u=0;u<o.length;u++)if(be(o[u],i)&&be(o[(u+1)%o.length],s)){a=u;break}if(a<0)return null;let l=[],c=(r+1)%t.length;for(l.push(t[c]);c!==r;)c=(c+1)%t.length,l.push(t[c]);for(c=(a+2)%o.length;c!==a;){let u=o[c];be(u,l[l.length-1])||l.push(u),c=(c+1)%o.length}return l.length>1&&be(l[0],l[l.length-1])&&l.pop(),l}function Yi(e,n,r){for(let[t,o]of e){let s=Zi(t),i=n.get(t)??(s?[s]:[]);if(i.length===0)return!1;for(let a of i)if(de(a,o)>r+1e-6)return!1}return!0}function Hi(e){let n=new Map;for(let r of e){for(let t of r.vertices)mn(n,ne(t),t);for(let t of r.textureTriangles??[])for(let o of t.vertices)mn(n,ne(o),o)}return n}function Ki(e,n,r){let t=new Map;for(let o of e){let s=[...o.vertices,...(o.textureTriangles??[]).flatMap(i=>i.vertices)];for(let i of s){let a=ne(i),l=n.get(a)??i,c=ne(l);for(let u of r.get(a)??[i])mn(t,c,u)}}return t}function Wi(e,n){let r=new Map;for(let t of e){let o=[...t.vertices,...(t.textureTriangles??[]).flatMap(s=>s.vertices)];for(let s of o){let i=ne(s);for(let a of n.get(i)??[s])mn(r,i,a)}}return r}function mn(e,n,r){let t=e.get(n);if(!t){e.set(n,[r]);return}let o=ne(r);t.some(s=>ne(s)===o)||t.push(r)}function Zi(e){let n=e.split(",").map(Number);return n.length===3&&n.every(Number.isFinite)?[n[0],n[1],n[2]]:null}function hn(e){let n=new Map;for(let t of e){let o=n.get(t.key);o?(o.x+=t.target[0],o.y+=t.target[1],o.z+=t.target[2],o.count+=1):n.set(t.key,{x:t.target[0],y:t.target[1],z:t.target[2],count:1})}let r=new Map;for(let[t,o]of n)r.set(t,[o.x/o.count,o.y/o.count,o.z/o.count]);return r}function Qi(e,n){let r=[];for(let t=0;t<e.length;t++){let o=e[t].vertices,s=n[t]?.vertices;if(!s||s.length!==o.length)continue;for(let l=0;l<o.length;l++)r.push({key:ne(o[l]),target:s[l]});let i=e[t].textureTriangles??[],a=n[t]?.textureTriangles??[];for(let l=0;l<i.length;l++){let c=a[l];if(c)for(let u=0;u<i[l].vertices.length;u++)r.push({key:ne(i[l].vertices[u]),target:c.vertices[u]})}}return r}function hr(e,n){let r=[];for(let t of e)for(let o of t.textureTriangles??[])for(let s of o.vertices)r.push({key:ne(s),target:bn(s,n)});return r}function xn(e,n){return e.map(r=>{let t=!1,o=a=>{let l=n.get(ne(a));return l?(t=!0,l):a},s=r.vertices.map(o),i=rt(r.textureTriangles,o);return t?{...r,vertices:s,...i?{textureTriangles:i}:{}}:r})}function qi(e,n,r,t,o){let s=r[e],i=r[n],a=t[e],l=t[n];if(!a||!l||s.vertices.length!==3||i.vertices.length!==3||!Cr(s,i,a,l))return null;let c=it(s,i);if(!c)return null;let[u,f,g,m]=c;if((g+1)%i.vertices.length===m)return null;let p=Math.abs(fe(a.normal,l.normal)),h=Math.cos(o.maxAngleDeg*Math.PI/180);if(p<h)return null;let x=(f+1)%s.vertices.length,y=3-g-m,M=[s.vertices[f],s.vertices[x],s.vertices[u],i.vertices[y]],P=xr(M);if(!P)return null;let b=0,A=0;for(let v of M){let V=Math.abs(yn(v,P));b=Math.max(b,V),A+=V*V}if(b>Math.min(o.maxPlaneDisplacement,o.maxBoundaryDisplacement))return null;let w=M.map(v=>bn(v,P));if(!yr(w,P.normal))return null;let I=_e({vertices:w});if(!I||fe(I.normal,a.normal)<.2||fe(I.normal,l.normal)<.2)return null;let C={vertices:M,color:s.color,...s.data?{data:{...s.data}}:{}};if(st(s,i)&&s.uvs&&i.uvs&&s.texture){C.texture=s.texture,C.uvs=[[...s.uvs[f]],[...s.uvs[x]],[...s.uvs[u]],[...i.uvs[y]]];let v=ec([s,i]);v?.length&&(C.textureTriangles=v)}return{a:e,b:n,polygon:C,vertexMoves:[...M.map((v,V)=>({key:ne(v),target:w[V]})),...hr([s,i],P)],score:A/M.length+b*.25+(1-p)*.1}}function xr(e){if(e.length<3)return null;let n=0,r=0,t=0,o=0,s=0,i=0;for(let l=0;l<e.length;l++){let c=e[l],u=e[(l+1)%e.length];n+=(c[1]-u[1])*(c[2]+u[2]),r+=(c[2]-u[2])*(c[0]+u[0]),t+=(c[0]-u[0])*(c[1]+u[1]),o+=c[0],s+=c[1],i+=c[2]}let a=wr([n,r,t]);return a?{normal:a,point:[o/e.length,s/e.length,i/e.length]}:null}function yr(e,n){let r=0;for(let t=0;t<e.length;t++){let o=e[t],s=e[(t+1)%e.length],i=e[(t+2)%e.length],a=fe(Sr(Ke(s,o),Ke(i,s)),n);if(Math.abs(a)<=1e-9)continue;let l=a>0?1:-1;if(r===0)r=l;else if(r!==l)return!1}return!0}function Ji(e,n,r){let t=dr(e,r),o=ic(t,n);if(o<=0)return t;let s=t.map(m=>{let d=_e(m);return d?{polygon:m,normal:d.normal,area:d.area,materialKey:ot(m)}:null}),i=lc(t,s),a=new Set,l=Array(t.length),c=[],u=(m,d)=>{l[m]=d};for(let m=0;m<t.length;m++){let d=s[m];if(a.has(m))continue;if(!d){u(m,t[m]);continue}let p=fc(m,s,i,a,o,n);for(let x of p)a.add(x);if(p.length<2){u(m,t[m]);continue}let h=rc(p,t,s,i,o,n);c.push(...h.vertexMoves);for(let x of p)u(x,h.polygons.get(x)??t[x])}let f=l.flatMap(m=>m?[m]:[]),g=c.length>0?xn(f,hn(c)):f;return gn(g)}function gn(e){let n=Mr(e),r=1e-4;if(n<=0)return e;let t=cc(n),o=ac(r);return e.map(s=>{let i=f=>t.snap(f),a=s.vertices.map(i),l=s.uvs&&s.uvs.length===s.vertices.length?s.uvs.map(f=>o.snap(f)):void 0,c=rt(s.textureTriangles,i),u={...s,vertices:a,...l?{uvs:l}:{},...c?{textureTriangles:c}:{}};return{...u,...u.texture?{textureTriangles:br(u)}:{}}})}function br(e){if(e.texture){if(e.textureTriangles?.length)return tc(e.textureTriangles);if(e.uvs&&e.uvs.length===e.vertices.length)return nc(e.vertices,e.uvs)}}function ec(e){let n=e.flatMap(r=>br(r)??[]);return n.length>0?n:void 0}function nc(e,n){let r=[];for(let t=1;t<e.length-1;t++)r.push({vertices:[[...e[0]],[...e[t]],[...e[t+1]]],uvs:[[...n[0]],[...n[t]],[...n[t+1]]]});return r}function tc(e){return e.map(n=>({vertices:n.vertices.map(r=>[...r]),uvs:n.uvs.map(r=>[...r])}))}function rt(e,n){if(e?.length)return e.map(r=>({vertices:r.vertices.map(n),uvs:r.uvs.map(t=>[...t])}))}function rc(e,n,r,t,o,s){let i=Pr(e,n,r,o,s);return i?sc(i):oc(e,n,r,t,o,s)}function oc(e,n,r,t,o,s){let i=new Set(e),a=[];for(let f of e)for(let g of t.get(f)??[]){if(f>=g||!i.has(g))continue;let m=Pr([f,g],n,r,o,s);m&&a.push(m)}a.sort((f,g)=>g.score-f.score);let l=new Set,c=new Map,u=[];for(let f of a)if(!f.indices.some(g=>l.has(g))){u.push(...f.vertexMoves);for(let g=0;g<f.indices.length;g++){let m=f.indices[g];l.add(m),c.set(m,n[m])}}return{polygons:c,vertexMoves:u}}function sc(e){let n=new Map;for(let r=0;r<e.indices.length;r++)n.set(e.indices[r],e.source[r]);return{polygons:n,vertexMoves:e.vertexMoves}}function Pr(e,n,r,t,o){let s=vr(e,r);if(!s||!Ar(e,r,s,t,o))return null;let i=e.map(u=>n[u]),a=i.map(u=>dc(u,s)),l=oe(le(i)),c=oe(le(a));return c>=l?null:{indices:e,source:i,projected:a,vertexMoves:Qi(i,a),score:l-c}}function ic(e,n){return Mr(e)<=0?0:n.maxPlaneDisplacement}function Mr(e){let n=1/0,r=1/0,t=1/0,o=-1/0,s=-1/0,i=-1/0;for(let l of e)for(let[c,u,f]of l.vertices)n=Math.min(n,c),r=Math.min(r,u),t=Math.min(t,f),o=Math.max(o,c),s=Math.max(s,u),i=Math.max(i,f);if(!Number.isFinite(n))return 0;let a=Math.hypot(o-n,s-r,i-t);return a<=0?0:Math.min(.025,Math.max(1e-4,a*25e-5))}function cc(e){let n=new Map,r=o=>Math.floor(o/e),t=(o,s,i)=>`${o},${s},${i}`;return{snap(o){let s=r(o[0]),i=r(o[1]),a=r(o[2]);for(let f=-1;f<=1;f++)for(let g=-1;g<=1;g++)for(let m=-1;m<=1;m++){let d=n.get(t(s+f,i+g,a+m));if(d){for(let p of d)if(de(o,p)<=e)return[p[0],p[1],p[2]]}}let l=[o[0],o[1],o[2]],c=t(s,i,a),u=n.get(c);return u?u.push(l):n.set(c,[l]),l}}}function ac(e){let n=new Map,r=o=>Math.floor(o/e),t=(o,s)=>`${o},${s}`;return{snap(o){let s=r(o[0]),i=r(o[1]);for(let u=-1;u<=1;u++)for(let f=-1;f<=1;f++){let g=n.get(t(s+u,i+f));if(g){for(let m of g)if(Math.hypot(o[0]-m[0],o[1]-m[1])<=e)return[m[0],m[1]]}}let a=[o[0],o[1]],l=t(s,i),c=n.get(l);return c?c.push(a):n.set(l,[a]),a}}}function ot(e){return`${e.color??"#cccccc"}|${e.texture??""}|${e.uvs?"uv":"plain"}`}function _e(e){let n=e.vertices;if(!n||n.length<3)return null;let r=0,t=0,o=0,s=n[0];for(let a=1;a<n.length-1;a++){let l=Ke(n[a],s),c=Ke(n[a+1],s),u=Sr(l,c);r+=u[0],t+=u[1],o+=u[2]}let i=Math.hypot(r,t,o);return i<=1e-10?null:{normal:[r/i,t/i,o/i],area:i/2}}function lc(e,n){let r=new Map,t=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=Le(s.vertices[i],s.vertices[(i+1)%s.vertices.length]),l=r.get(a);l?l.push(o):r.set(a,[o])}}for(let o of r.values())for(let s=0;s<o.length;s++)for(let i=s+1;i<o.length;i++){let a=o[s],l=o[i];uc(e[a],e[l],n[a],n[l])&&(rr(t,a,l),rr(t,l,a))}return t}function uc(e,n,r,t){if(!r||!t||r.materialKey!==t.materialKey||!!e.uvs!=!!n.uvs)return!1;if(dn(e)||dn(n))return st(e,n);if(!e.uvs||!n.uvs)return!0;let o=it(e,n);if(!o)return!1;let[s,i,a,l]=o;return pn(e.uvs[s],n.uvs[a])&&pn(e.uvs[i],n.uvs[l])}function Cr(e,n,r,t){return r.materialKey!==t.materialKey?!1:dn(e)||dn(n)?st(e,n):!e.uvs&&!n.uvs&&!e.textureTriangles?.length&&!n.textureTriangles?.length}function dn(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function st(e,n){if(!e.texture||!n.texture||e.texture!==n.texture||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 r=it(e,n);if(!r)return!1;let[t,o,s,i]=r;return pn(e.uvs[t],n.uvs[s])&&pn(e.uvs[o],n.uvs[i])}function rr(e,n,r){let t=e.get(n);t?t.add(r):e.set(n,new Set([r]))}function fc(e,n,r,t,o,s){let i=[e],a=new Set([e]),l=[e];for(;l.length>0;){let c=l.shift();for(let u of r.get(c)??[]){if(t.has(u)||a.has(u))continue;let f=n[u],g=n[e];!f||!g||f.materialKey===g.materialKey&&mc([...i,u],n,o,s)&&(i.push(u),a.add(u),l.push(u))}}return i}function mc(e,n,r,t){let o=vr(e,n);return!!o&&Ar(e,n,o,r,t)}function vr(e,n){let r=n[e[0]];if(!r)return null;let t=0,o=0,s=0,i=0,a=0,l=0,c=0;for(let m of e){let d=n[m];if(!d)return null;let p=fe(r.normal,d.normal)<0?-1:1,h=Math.max(d.area,1e-6);t+=d.normal[0]*p*h,o+=d.normal[1]*p*h,s+=d.normal[2]*p*h;for(let x of d.polygon.vertices)i+=x[0],a+=x[1],l+=x[2],c+=1}let u=wr([t,o,s]);if(!u||c===0)return null;let f=Vr(e,n),g=gc(e,n,u,f);if(g){let m=(g.min+g.max)/2;return{normal:u,point:[u[0]*m,u[1]*m,u[2]*m]}}return{normal:u,point:[i/c,a/c,l/c]}}function gc(e,n,r,t){let o=1/0,s=-1/0;for(let i of e){let a=n[i];if(a)for(let l of a.polygon.vertices){if(!t.has(ne(l)))continue;let c=fe(l,r);o=Math.min(o,c),s=Math.max(s,c)}}return Number.isFinite(o)&&Number.isFinite(s)?{min:o,max:s}:null}function Ar(e,n,r,t,o){let s=Math.cos(o.maxAngleDeg*Math.PI/180),i=Vr(e,n);for(let a of e){let l=n[a];if(!l||Math.abs(fe(l.normal,r.normal))<s)return!1;for(let c of l.polygon.vertices){let u=i.has(ne(c))?o.maxBoundaryDisplacement:t;if(Math.abs(yn(c,r))>u)return!1}}return!0}function Vr(e,n){let r=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 l=i[a],c=i[(a+1)%i.length],u=Le(l,c),f=r.get(u);f?f.count+=1:r.set(u,{count:1,a:l,b:c})}}let t=new Set;for(let o of r.values())o.count===1&&(t.add(ne(o.a)),t.add(ne(o.b)));return t}function dc(e,n){let r=o=>bn(o,n),t=rt(e.textureTriangles,r);return{...e,vertices:e.vertices.map(r),...t?{textureTriangles:t}:{}}}function it(e,n){for(let r=0;r<e.vertices.length;r++){let t=(r+1)%e.vertices.length;for(let o=0;o<n.vertices.length;o++){let s=(o+1)%n.vertices.length;if(be(e.vertices[r],n.vertices[o])&&be(e.vertices[t],n.vertices[s]))return[r,t,o,s];if(be(e.vertices[r],n.vertices[s])&&be(e.vertices[t],n.vertices[o]))return[r,t,s,o]}}return null}function Le(e,n){let r=ne(e),t=ne(n);return r<t?`${r}|${t}`:`${t}|${r}`}function ne(e){return`${e[0]},${e[1]},${e[2]}`}function be(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function pn(e,n){return Math.abs(e[0]-n[0])<=1e-4&&Math.abs(e[1]-n[1])<=1e-4}function Ke(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Sr(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 fe(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function de(e,n){return Math.hypot(e[0]-n[0],e[1]-n[1],e[2]-n[2])}function wr(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 yn(e,n){return fe(Ke(e,n.point),n.normal)}function bn(e,n){let r=yn(e,n);return[e[0]-n.normal[0]*r,e[1]-n.normal[1]*r,e[2]-n.normal[2]*r]}var Pn=1e-5,Ir=.001,Rr=6;function ct(e){let n=e.vertices;if(n.length<3)return null;let r=n[0],t=0,o=0,s=0;for(let a=1;a+1<n.length;a++){let l=n[a],c=n[a+1],u=l[1]-r[1],f=l[0]-r[0],g=l[2]-r[2],m=c[1]-r[1],d=c[0]-r[0],p=c[2]-r[2];t-=f*p-g*d,o-=g*m-u*p,s-=u*d-f*m}let i=Math.hypot(t,o,s);return i<1e-9?null:[t/i,o/i,s/i]}function Tr(e,n){let r=n.meshRotation,t=r?Je(e,r[0]??0,r[1]??0,r[2]??0):e;return Je(t,n.rotX,0,n.rotY)[2]}function at(e,n,r=Pn){return Tr(e,n)>r}function pc(e,n,r=Pn){let t=ct(e);return t===null||at(t,n,r)}function Er(e){return`${e[0].toFixed(4)},${e[1].toFixed(4)},${e[2].toFixed(4)}`}function Or(e){let n=new Map;for(let r of e){if(!r)continue;let t=Er(r);n.has(t)||n.set(t,r)}return Array.from(n,([r,t])=>({key:r,normal:t}))}function hc(e){return Or(e.map(ct))}function kr(e,n=Ir){let r=Math.abs(e[0]),t=Math.abs(e[1]),o=Math.abs(e[2]),s=Math.max(r,t,o);return s>1-n&&r+t+o-s<n}function xc(e){return e.length<=Rr&&e.every(({normal:n})=>kr(n))}function yc(e,n,r=Pn){let t=[];for(let{key:o,normal:s}of e)at(s,n,r)&&t.push(o);return t.sort(),t.join("|")}function lt(e,n,r,t,o){let s=(d,p,h)=>{let x=[0,0,0];return x[e]=d,x[(e+1)%3]=p,x[(e+2)%3]=h,x},i=s(n,-t,-t),a=s(n,t,-t),l=s(n,t,t),c=s(n,-t,t),u=s(r,-t,-t),f=s(r,t,-t),g=s(r,t,t),m=s(r,-t,t);return[{vertices:[i,a,l,c],color:o},{vertices:[u,f,g,m],color:o},{vertices:[i,a,f,u],color:o},{vertices:[a,l,g,f],color:o},{vertices:[l,c,m,g],color:o},{vertices:[c,i,u,m],color:o}]}function _r(e={}){let n=e.size??5,r=e.thickness??.025,t=e.negative??!1,o=e.xColor??"#ff3a3a",s=e.yColor??"#3aff3a",i=e.zColor??"#3a8aff",a=n*r/2,l=t?-n:0;return[...lt(0,l,n,a,o),...lt(1,l,n,a,s),...lt(2,l,n,a,i)]}var bc=["right","left","front","back","top","bottom"];function ut(e){return[e[0],e[1],e[2]]}function Lr(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 Pc(e){if(e.min&&e.max)return Lr(e.min,e.max);let n=e.center?ut(e.center):[0,0,0],r;typeof e.size=="number"?r=[e.size,e.size,e.size]:e.size?r=ut(e.size):r=[1,1,1];let t=[r[0]/2,r[1]/2,r[2]/2];return Lr([n[0]-t[0],n[1]-t[1],n[2]-t[2]],[n[0]+t[0],n[1]+t[1],n[2]+t[2]])}function Mc(e,n){let[r,t,o]=e,[s,i,a]=n;return[{name:"right",vertices:[[s,t,o],[s,i,o],[s,i,a],[s,t,a]]},{name:"left",vertices:[[r,i,o],[r,t,o],[r,t,a],[r,i,a]]},{name:"front",vertices:[[s,i,o],[r,i,o],[r,i,a],[s,i,a]]},{name:"back",vertices:[[r,t,o],[s,t,o],[s,t,a],[r,t,a]]},{name:"top",vertices:[[r,t,a],[s,t,a],[s,i,a],[r,i,a]]},{name:"bottom",vertices:[[r,i,o],[s,i,o],[s,t,o],[r,t,o]]}]}function Cc(e,n){let r={...e,...n};return(e.data!==void 0||n?.data!==void 0)&&(r.data={...e.data??{},...n?.data??{}}),r}function vc(e,n){let r={vertices:e.vertices.map(ut)};return n.color!==void 0&&(r.color=n.color),n.texture!==void 0&&(r.texture=n.texture),n.material!==void 0&&(r.material=n.material),n.uvs!==void 0&&(r.uvs=n.uvs.map(t=>[t[0],t[1]])),n.data!==void 0&&(r.data={...n.data}),r}function Br(e={}){let{min:n,max:r}=Pc(e);if(n[0]===r[0]||n[1]===r[1]||n[2]===r[2])return[];let t={color:e.color??"#ffffff",texture:e.texture,material:e.material,uvs:e.uvs,data:e.data},o=Mc(n,r),s=new Map(o.map(a=>[a.name,a])),i=[];for(let a of bc){let l=e.faces?.[a];if(l===!1)continue;let c=s.get(a);c&&i.push(vc(c,Cc(t,l)))}return i}function Nr(e,n,r,t){let o=[0,0,0];return o[e]=n,o[(e+1)%3]=r,o[(e+2)%3]=t,o}function Ac(e,n,r,t,o){let s=(d,p,h)=>Nr(e,d,p,h),i=s(n,-t,-t),a=s(n,t,-t),l=s(n,t,t),c=s(n,-t,t),u=s(r,-t,-t),f=s(r,t,-t),g=s(r,t,t),m=s(r,-t,t);return[{vertices:[i,a,l,c],color:o},{vertices:[u,f,g,m],color:o},{vertices:[i,a,f,u],color:o},{vertices:[a,l,g,f],color:o},{vertices:[l,c,m,g],color:o},{vertices:[c,i,u,m],color:o}]}function Vc(e,n,r,t,o){let s=(f,g,m)=>Nr(e,f,g,m),i=s(n,-t,-t),a=s(n,t,-t),l=s(n,t,t),c=s(n,-t,t),u=s(r,0,0);return[{vertices:[i,a,l,c],color:o},{vertices:[i,a,u],color:o},{vertices:[a,l,u],color:o},{vertices:[l,c,u],color:o},{vertices:[c,i,u],color:o}]}function Sc(e){return e.map(n=>({...n,vertices:[...n.vertices].reverse()}))}function Fr(e){let n=e.axis,r=e.sign??1,t=e.shaftLength??4,o=e.shaftHalfThickness??.05,s=e.headLength??.8,i=e.headHalfThickness??.2,a=e.color??"#ffffff",l=e.shaft??!0,c=t*r,u=Math.min(0,c),f=Math.max(0,c),g=c,m=(t+s)*r,d=Vc(n,g,m,i,a),p=r===-1?Sc(d):d;return l?[...Ac(n,u,f,o,a),...p]:p}function Mn(e,n,r){let t=[0,0,0],o=(e+1)%3,s=(e+2)%3;return t[o]=Math.cos(r)*n,t[s]=Math.sin(r)*n,t}function Dr(e){let n=e.axis,r=e.radius,t=e.halfThickness??Math.max(.05,r*.04),o=e.segments??32,s=e.color??"#ffffff",i=r-t,a=r+t,l=[];for(let c=0;c<o;c++){let u=c/o*Math.PI*2,f=(c+1)/o*Math.PI*2,g=Mn(n,i,u),m=Mn(n,i,f),d=Mn(n,a,u),p=Mn(n,a,f);l.push({vertices:[g,d,p,m],color:s})}return l}function zr(e){let n=e.axis,r=e.outerRadius,t=e.color??"#ffffff",o=(n+1)%3,s=(n+2)%3,i=(a,l)=>{let c=[0,0,0];return c[n]=0,c[o]=a,c[s]=l,c};return[{vertices:[i(-r,-r),i(r,-r),i(r,r),i(-r,r)],color:t}]}function Gr(e){let n=e.axis,r=e.size??.4,t=e.offset??r*2,o=typeof t=="number"?t:t[0],s=typeof t=="number"?t:t[1],i=e.along??0,a=e.color??"#ffffff",l=(n+1)%3,c=(n+2)%3,u=(f,g)=>{let m=[0,0,0];return m[n]=i,m[l]=o+f,m[c]=s+g,m};return[{vertices:[u(-r,-r),u(r,-r),u(r,r),u(-r,r)],color:a}]}function Ur(e){let{center:n,size:r,color:t="#ffffff"}=e,[o,s,i]=n,a=[[o+r,s,i],[o-r,s,i],[o,s+r,i],[o,s-r,i],[o,s,i+r],[o,s,i-r]];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(c=>({vertices:[a[c[0]],a[c[1]],a[c[2]]],color:t}))}var wc=2200,Ic=2201,Rc=2202;function Tc(e,n){let r=0,t=1,o=1,s=2201,i=1/0,a=0,l=!1,c=null,u=!0,f=!1,g={clampWhenFinished:!1,get timeScale(){return o},set timeScale(m){o=m},get weight(){return t},set weight(m){t=m},get time(){return r},set time(m){r=m},get isRunning(){return l},get enabled(){return u},set enabled(m){u=m},get paused(){return f},set paused(m){f=m},play(){return l=!0,g},stop(){return l=!1,r=0,a=0,c=null,g},reset(){return r=0,a=0,g},fadeIn(m){return c={from:0,to:1,elapsed:0,duration:m},t=0,g},fadeOut(m){return c={from:t,to:0,elapsed:0,duration:m},g},crossFadeTo(m,d){return g.fadeOut(d),m.fadeIn(d),g},crossFadeFrom(m,d){return m.fadeOut(d),g.fadeIn(d),g},setLoop(m,d){return s=m,i=d,g},setEffectiveTimeScale(m){return o=m,g},setEffectiveWeight(m){return t=m,g}};return g._internal={get clip(){return e},get time(){return r},get weight(){return t},get enabled(){return u},get paused(){return f},get running(){return l},get loopMode(){return s},get repetitions(){return i},get completedReps(){return a},set completedReps(m){a=m},get clampWhenFinished(){return g.clampWhenFinished},get timeScale(){return o},get fade(){return c},advance(m){if(!l)return;if(c){c.elapsed+=m;let p=c.duration>0?Math.min(c.elapsed/c.duration,1):1;if(t=c.from+(c.to-c.from)*p,p>=1&&(t=c.to,c=null,t<=0)){l=!1;return}}if(f)return;let d=e.duration;if(!(d<=0)){if(r+=m*o,s===2200)r>=d&&(r=g.clampWhenFinished?d:0,l=!1);else if(s===2201){if(r>=d){let p=r%d,h=Math.floor(r/d);a+=h,r=p,i!==1/0&&a>=i&&(r=g.clampWhenFinished?d:0,l=!1)}}else if(d>0){let p=d*2,h=r%p;if(r>=p){let x=Math.floor(r/p);a+=x,i!==1/0&&a>=i?(r=g.clampWhenFinished?d:0,l=!1):r=h}}}},sampleTime(){if(s===2202){let m=e.duration,d=m*2,p=r%d;return p<=m?p:d-p}return r}},g}function Ec(e){return e._internal}function ft(e,n){return typeof n=="number"?e[n]:e.find(r=>r.name===n)}function Oc(e,n){let r=new Map;function t(c){let u=ft(n.clips,c);if(!u)throw new Error(`PolyAnimationMixer: no clip found for key "${c}". Available: ${n.clips.map(g=>g.name).join(", ")}`);let f=r.get(u.index);return f||(f=Tc(u,n),r.set(u.index,f)),f}function o(c){let u=ft(n.clips,c);return u?r.get(u.index)??null:null}function s(c){let u=[];for(let p of r.values()){let h=Ec(p);h.advance(c),h.running&&h.enabled&&u.push({internal:h,clip:h.clip})}if(u.length===0)return;if(u.length===1){let{internal:p,clip:h}=u[0],x=n.sample(h.name,p.sampleTime());e.setPolygons(x);return}let f=0;for(let{internal:p}of u)f+=p.weight;if(f<=0)return;let g=u.map(({internal:p,clip:h})=>({polygons:n.sample(h.name,p.sampleTime()),weight:p.weight/f})),m=g[0].polygons;if(m.length===0)return;let d=m.map((p,h)=>{let x=p.vertices.map((y,M)=>{let P=0,b=0,A=0;for(let{polygons:w,weight:I}of g){let C=w[h];if(!C)continue;let v=C.vertices[M];v&&(P+=v[0]*I,b+=v[1]*I,A+=v[2]*I)}return[P,b,A]});return{...p,vertices:x}});e.setPolygons(d)}function i(){for(let c of r.values())c.stop()}function a(c){let u=ft(n.clips,c);u&&r.delete(u.index)}function l(){r.clear()}return{clipAction:t,existingAction:o,update:s,stopAllAction:i,uncacheClip:a,uncacheRoot:l}}var kc=/^[0-9A-Fa-f]{6}$/,_c=["#3b82f6","#ef4444","#22c55e","#eab308","#a855f7","#06b6d4","#f97316","#ec4899"];function mt(e,n){let r=n?.targetSize??60,t=n?.gridShift??1,o=n?.defaultColor??"#888888",s=n?.palette??_c,i=n?.materialColors??{},a=n?.materialTextures??{},l=[],c=[],u=[],f=[],g=new Map,m=o,d,p=n?.includeObjects?new Set(n.includeObjects):null,h=n?.excludeObjects?new Set(n.excludeObjects):null,x=null,y=()=>x===null?p===null:!(p&&!p.has(x)||h&&h.has(x)),M=_=>_ in i?i[_]:kc.test(_)?`#${_}`:(g.has(_)||(g.set(_,s[f.length%s.length]),f.push(_)),g.get(_)),P=e.split(`
|
|
2
|
-
`);for(let
|
|
3
|
-
`)){let s=o.trim();if(!(s.length===0||s.charCodeAt(0)===35)){if(s.startsWith("newmtl ")){t=s.slice(7).trim();continue}if(t){if(s.startsWith("Kd ")){let i=s.split(/\s+/),a=parseFloat(i[1]),l=parseFloat(i[2]),c=parseFloat(i[3]);Number.isFinite(a)&&Number.isFinite(l)&&Number.isFinite(c)&&(n[t]=`#${gt(a)}${gt(l)}${gt(c)}`)}else if(s.startsWith("map_Kd ")){let i=s.split(/\s+/),a=i[i.length-1]?.replace(/\\+/g,"/");a&&a!=="map_Kd"&&(r[t]=a)}}}}return{colors:n,textures:r}}var Hr={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},Kr={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16};function Wr(e){let n=globalThis.TextDecoder;return new n().decode(e)}function Bc(e){let n=e.indexOf(",");if(n<0)throw new Error("parseGltf: malformed data: URI");let r=e.slice(5,n),t=e.slice(n+1);if(!r.includes(";base64")){let i=decodeURIComponent(t),a=new Uint8Array(i.length);for(let l=0;l<i.length;l++)a[l]=i.charCodeAt(l)&255;return a}let o=globalThis.atob(t),s=new Uint8Array(o.length);for(let i=0;i<o.length;i++)s[i]=o.charCodeAt(i);return s}function Nc(e,n){let r=e.buffers?.[0];if(!r)throw new Error("parseGltf: JSON doc has no buffers[0]");let t=r.uri;if(!t)throw new Error("parseGltf: JSON doc buffer has no uri (and there's no GLB BIN chunk)");if(t.startsWith("data:"))return Bc(t);if(n){let o=n(t);if(o instanceof Uint8Array)return o;throw new Error("parseGltf: resolveBuffer returned a Promise; use parseGltf via async if your buffers are external")}throw new Error(`parseGltf: external buffer URI "${t}" \u2014 provide options.resolveBuffer`)}function Fc(e){let n=new DataView(e);if(n.getUint32(0,!0)!==1179937895)throw new Error("parseGltf: not a GLB (bad magic)");let r=n.getUint32(4,!0);if(r!==2)throw new Error(`parseGltf: only glTF v2 supported (got v${r})`);let t=12,o=null,s=null;for(;t<e.byteLength;){let i=n.getUint32(t,!0),a=n.getUint32(t+4,!0),l=t+8;if(a===1313821514){let c=new Uint8Array(e,l,i);o=JSON.parse(Wr(c))}else a===5130562&&(s=new Uint8Array(e,l,i));t=l+i}if(!o)throw new Error("parseGltf: no JSON chunk in GLB");return{doc:o,bin:s}}function pt(e,n,r){let t=e.accessors?.[r],o=e.bufferViews?.[t?.bufferView??-1];if(!t||!o)throw new Error(`parseGltf: bad accessor/bufferView ${r}`);let s=Hr[t.componentType],i=Kr[t.type];if(!s||!i)throw new Error(`parseGltf: unsupported accessor type ${t.type}/${t.componentType}`);let a=(o.byteOffset??0)+(t.byteOffset??0),l=t.count*i,c=n.buffer.slice(n.byteOffset+a,n.byteOffset+a+l*s),u;switch(t.componentType){case 5126:u=new Float32Array(c);break;case 5123:u=new Uint16Array(c);break;case 5125:u=new Uint32Array(c);break;case 5121:u=new Uint8Array(c);break;default:throw new Error(`parseGltf: unhandled componentType ${t.componentType}`)}return{array:u,count:t.count,componentCount:i}}function Dc(e,n,r){switch(r){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 ${r}`)}}function zc(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 An(e,n,r){let t=e.accessors?.[r],o=e.bufferViews?.[t?.bufferView??-1];if(!t||!o)throw new Error(`parseGltf: bad accessor/bufferView ${r}`);let s=Hr[t.componentType],i=Kr[t.type];if(!s||!i)throw new Error(`parseGltf: unsupported accessor type ${t.type}/${t.componentType}`);let a=n.byteOffset+(o.byteOffset??0)+(t.byteOffset??0),l=o.byteStride??s*i,c=new DataView(n.buffer),u=new Array(t.count*i),f=0;for(let g=0;g<t.count;g++){let m=a+g*l;for(let d=0;d<i;d++){let p=Dc(c,m+d*s,t.componentType);u[f++]=t.normalized?zc(p,t.componentType):p}}return{values:u,count:t.count,componentCount:i}}function Gc(e,n,r){let t=[],o=[],s=globalThis;for(let i of e.images??[]){if(i.uri){if(r&&!i.uri.startsWith("data:"))try{t.push(new s.URL(i.uri,r).href)}catch{t.push(i.uri)}else t.push(i.uri);continue}if(i.bufferView!==void 0){let a=e.bufferViews?.[i.bufferView];if(!a){t.push("");continue}let l=a.byteOffset??0,c=n.subarray(l,l+a.byteLength),u=i.mimeType??"image/png",f=new s.Blob([c],{type:u}),g=s.URL.createObjectURL(f);t.push(g),o.push(g)}else t.push("")}return{urls:t,objectUrls:o}}function Uc(e,n){let r=new Map,t=e.materials??[];for(let o=0;o<t.length;o++){let s=t[o].pbrMetallicRoughness?.baseColorTexture?.index;if(s===void 0)continue;let i=e.textures?.[s]?.source;if(i===void 0)continue;let a=n[i];a&&r.set(o,a)}return r}function $c(e,n){let r=e?.pbrMetallicRoughness?.baseColorFactor;if(!r||r.length<3)return n;let t=a=>Math.max(0,Math.min(1,a)),o=a=>Math.round(t(a)*255).toString(16).padStart(2,"0"),s=a=>Math.round(t(a)*255),i=t(r[3]??1);return i<1?`rgba(${s(r[0])}, ${s(r[1])}, ${s(r[2])}, ${Math.round(i*1e3)/1e3})`:`#${o(r[0])}${o(r[1])}${o(r[2])}`}var Be=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function ht(e,n){let r=new Array(16);for(let t=0;t<4;t++)for(let o=0;o<4;o++)r[o*4+t]=e[0+t]*n[o*4+0]+e[4+t]*n[o*4+1]+e[8+t]*n[o*4+2]+e[12+t]*n[o*4+3];return r}function vn(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 Zr(e,n,r){let t=e?.[0]??0,o=e?.[1]??0,s=e?.[2]??0,i=n?.[0]??0,a=n?.[1]??0,l=n?.[2]??0,c=n?.[3]??1,u=r?.[0]??1,f=r?.[1]??1,g=r?.[2]??1,m=i+i,d=a+a,p=l+l,h=i*m,x=i*d,y=i*p,M=a*d,P=a*p,b=l*p,A=c*m,w=c*d,I=c*p;return[(1-(M+b))*u,(x+I)*u,(y-w)*u,0,(x-I)*f,(1-(h+b))*f,(P+A)*f,0,(y+w)*g,(P-A)*g,(1-(h+M))*g,0,t,o,s,1]}function Xc(e){return e.matrix&&e.matrix.length===16?e.matrix.slice():Zr(e.translation,e.rotation,e.scale)}function jc(e,n){return[e[0]+n[0],e[1]+n[1],e[2]+n[2]]}function $r(e,n){return[e[0]*n,e[1]*n,e[2]*n]}function Qr(e,n,r){let t=new Array(Math.min(e.length,n.length));for(let o=0;o<t.length;o++)t[o]=e[o]+(n[o]-e[o])*r;return t}function We(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 Yc(e,n,r){let t=We(e),o=We(n),s=t[0]*o[0]+t[1]*o[1]+t[2]*o[2]+t[3]*o[3];if(s<0&&(s=-s,o=[-o[0],-o[1],-o[2],-o[3]]),s>.9995)return We(Qr(t,o,r));let i=Math.acos(Math.max(-1,Math.min(1,s))),a=Math.sin(i),l=Math.sin((1-r)*i)/a,c=Math.sin(r*i)/a;return We([t[0]*l+o[0]*c,t[1]*l+o[1]*c,t[2]*l+o[2]*c,t[3]*l+o[3]*c])}function Hc(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 Xr(e){return e.matrix?e.matrix.slice():Zr(e.translation,e.rotation,e.scale)}function Kc(e){let n=e.scene??0,r=e.scenes?.[n]?.nodes;return r&&r.length>0?r:[]}function jr(e,n){let r=e.nodes??[],t=new Array(r.length),o=new Set,s=(a,l)=>{if(a<0||a>=r.length)return;let c=ht(l,n[a]??Be);t[a]=c,o.add(a);for(let u of r[a].children??[])s(u,c)},i=Kc(e);if(i.length>0)for(let a of i)s(a,Be);for(let a=0;a<r.length;a++)o.has(a)||s(a,Be);return t}function Yr(e,n,r,t,o){if(r===void 0)return;let{values:s,count:i,componentCount:a}=An(e,n,r);if(i!==o||a<1)return;let l=[];for(let c=0;c<i;c++){let u=[];for(let f=0;f<t;f++)u.push(s[c*a+f]??0);l.push(u)}return l}function Wc(e,n,r,t){if(r===void 0)return Array.from({length:t},()=>Be.slice());let{values:o,componentCount:s,count:i}=An(e,n,r);if(s!==16)throw new Error(`parseGltf: inverseBindMatrices accessor ${r} is not MAT4`);let a=[];for(let l=0;l<t;l++){let c=Math.min(l,i-1);a.push(o.slice(c*16,c*16+16))}return a}function Cn(e,n){let r=e.componentCount,t=e.interpolation==="CUBICSPLINE"?(n*3+1)*r:n*r;return e.output.slice(t,t+r)}function Zc(e,n,r){let t=e.input;if(t.length===0)return[];if(t.length===1||n<=t[0])return Cn(e,0);let o=t.length-1;if(n>=t[o])return Cn(e,o);let s=0,i=o;for(;s+1<i;){let g=s+i>>1;t[g]<=n?s=g:i=g}let a=t[s],l=t[s+1],c=l>a?(n-a)/(l-a):0,u=Cn(e,s),f=Cn(e,s+1);return e.interpolation==="STEP"?u:r==="rotation"?Yc(u,f,c):Qr(u,f,c)}function Qc(e,n,r,t){let o=e.animations??[];if(o.length===0||r.length===0)return;let s=(e.nodes??[]).map(m=>Hc(m)),i=s.map(Xr),a=jr(e,i),l=(e.skins??[]).map(m=>({joints:m.joints??[],inverseBindMatrices:Wc(e,n,m.inverseBindMatrices,m.joints?.length??0)})),c=[];for(let m=0;m<o.length;m++){let d=o[m],p=(d.samplers??[]).map(y=>{let M=An(e,n,y.input),P=An(e,n,y.output);return{input:M.values,output:P.values,componentCount:P.componentCount,interpolation:y.interpolation??"LINEAR"}}),h=[];for(let y of d.channels??[]){let M=y.target.node,P=y.target.path,b=p[y.sampler];M===void 0||!P||!b||P==="weights"||h.push({sampler:b,targetNode:M,path:P})}let x=h.reduce((y,M)=>{let P=M.sampler.input;return Math.max(y,P[P.length-1]??0)},0);c.push({info:{index:m,name:d.name??`animation_${m}`,duration:x,channelCount:h.length},channels:h})}let u=c.map(m=>m.info);if(u.length===0)return;let f=(m,d,p,h,x,y)=>{let M=t(m),P=t(d),b=t(p);if(M[0]===P[0]&&M[1]===P[1]&&M[2]===P[2]||M[0]===b[0]&&M[1]===b[1]&&M[2]===b[2]||P[0]===b[0]&&P[1]===b[1]&&P[2]===b[2])return null;let A={vertices:[M,P,b],color:h};return x&&(A.texture=x),y&&(A.uvs=y),A};return{clips:u,sample:(m,d)=>{let p=typeof m=="number"?c[m]:c.find(b=>b.info.name===m);if(!p)return[];let h=p.info.duration,x=h>0?(d%h+h)%h:Math.max(0,d),y=s.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 p.channels){let A=y[b.targetNode];if(!A)continue;let w=Zc(b.sampler,x,b.path);A.matrix=void 0,b.path==="translation"?A.translation=w.slice(0,3):b.path==="rotation"?A.rotation=We(w.slice(0,4)):b.path==="scale"&&(A.scale=w.slice(0,3))}let M=jr(e,y.map(Xr)),P=[];for(let b of r){let A=[];if(b.skinIndex!==void 0&&b.joints&&b.weights&&l[b.skinIndex]){let w=l[b.skinIndex];for(let I=0;I<b.positions.length;I++){let C=b.positions[I],v=[0,0,0],V=0,R=b.joints[I]??[],E=b.weights[I]??[];for(let F=0;F<4;F++){let D=E[F]??0;if(D<=0)continue;let z=Math.round(R[F]??0),_=w.joints[z],L=M[_],T=w.inverseBindMatrices[z];if(!L||!T)continue;let B=ht(L,T);v=jc(v,$r(vn(B,C),D)),V+=D}A.push(V>0?$r(v,1/V):vn(b.meshBindWorld,C))}}else{let w=b.meshNode!==null?M[b.meshNode]??b.meshBindWorld:b.meshBindWorld;for(let I of b.positions)A.push(vn(w,I))}for(let w=0;w+2<b.indices.length;w+=3){let I=b.indices[w],C=b.indices[w+1],v=b.indices[w+2],V=A[I],R=A[C],E=A[v];if(!V||!R||!E)continue;let F;if(b.uvs&&b.texture){let z=b.uvs[I],_=b.uvs[C],L=b.uvs[v];z&&_&&L&&(F=[z,_,L])}let D=f(V,R,E,b.color,b.texture,F);D&&P.push(D)}}return P}}}function xt(e,n){let r=n?.targetSize??60,t=n?.gridShift??1,o=n?.defaultColor??"#888888",s=n?.materialColors??{},i=e instanceof Uint8Array?e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength):e,a=i.byteLength,l,c;if(i.byteLength>=4&&new DataView(i).getUint32(0,!0)===1179937895){let T=Fc(i);if(l=T.doc,!T.bin)throw new Error("parseGltf: GLB has no binary chunk");c=T.bin}else l=JSON.parse(Wr(new Uint8Array(i))),c=Nc(l,n?.resolveBuffer);let{urls:u,objectUrls:f}=Gc(l,c,n?.baseUrl),g=Uc(l,u),m=[],d=[],p=(l.meshes??[]).map((T,B)=>T.name??`mesh_${B}`),h=(l.materials??[]).map((T,B)=>T.name??`material_${B}`);function x(T,B,O){let S=l.meshes?.[T];if(S)for(let k of S.primitives){if((k.mode??4)!==4)continue;let U=k.material!==void 0?l.materials?.[k.material]?.name:void 0,G=(U?s[U]:void 0)??$c(k.material!==void 0?l.materials?.[k.material]:void 0,o),$=k.material!==void 0?g.get(k.material):void 0,{array:W,count:q}=pt(l,c,k.attributes.POSITION);if(!(W instanceof Float32Array))continue;let ee=[],H=[];for(let J=0;J<q;J++){let X=[W[J*3],W[J*3+1],W[J*3+2]];ee.push(X),H.push(vn(B,X))}let K=null,te=k.attributes.TEXCOORD_0;if($&&te!==void 0){let{array:J,count:X}=pt(l,c,te);K=[];let Q=1;J instanceof Uint8Array?Q=1/255:J instanceof Uint16Array&&(Q=1/65535);for(let ce=0;ce<X;ce++){let pe=J[ce*2]*Q,Me=J[ce*2+1]*Q;K.push([pe,1-Me])}}let se;if(k.indices!==void 0){let{array:J,count:X}=pt(l,c,k.indices);se=[];for(let Q=0;Q<X;Q++)se.push(Number(J[Q]))}else se=H.map((J,X)=>X);if((l.animations?.length??0)>0){let J=Yr(l,c,k.attributes.JOINTS_0,4,q),X=Yr(l,c,k.attributes.WEIGHTS_0,4,q);d.push({meshNode:O,meshBindWorld:B,skinIndex:O!==null?l.nodes?.[O]?.skin:void 0,positions:ee,indices:se,color:G,texture:$,uvs:K??void 0,joints:J,weights:X})}for(let J=0;J+2<se.length;J+=3){let X=H[se[J]],Q=H[se[J+1]],ce=H[se[J+2]];if(!X||!Q||!ce)continue;let pe;if(K&&$){let Me=K[se[J]],Se=K[se[J+1]],we=K[se[J+2]];Me&&Se&&we&&(pe=[Me,Se,we])}m.push({v0:X,v1:Q,v2:ce,color:G,texture:$,uvs:pe})}}}function y(T,B){let O=l.nodes?.[T];if(!O)return;let S=ht(B,Xc(O));typeof O.mesh=="number"&&x(O.mesh,S,T);for(let k of O.children??[])y(k,S)}let M=l.scene??0,P=l.scenes?.[M]?.nodes;if(P&&P.length>0)for(let T of P)y(T,Be);else for(let T=0;T<(l.meshes?.length??0);T++)x(T,Be,null);let b=qc(f);if(m.length===0)return{polygons:[],objectUrls:f,dispose:b,warnings:[],metadata:{triangleCount:0,meshes:p,materials:h,sourceBytes:a}};let A=1/0,w=1/0,I=1/0,C=-1/0,v=-1/0,V=-1/0;for(let T of m)for(let B of[T.v0,T.v1,T.v2])B[0]<A&&(A=B[0]),B[0]>C&&(C=B[0]),B[1]<w&&(w=B[1]),B[1]>v&&(v=B[1]),B[2]<I&&(I=B[2]),B[2]>V&&(V=B[2]);let R=Math.max(C-A,v-w,V-I),E=R>0?r/R:1,F=T=>Math.round(T*1e3)/1e3,z=(n?.upAxis??"y")==="z"?([T,B,O])=>[F((T-A)*E+t),F((B-w)*E+t),F((O-I)*E+t)]:([T,B,O])=>[F((O-I)*E+t),F((T-A)*E+t),F((B-w)*E+t)],_=Qc(l,c,d,z),L=[];for(let T of m){let B=z(T.v0),O=z(T.v1),S=z(T.v2);if(B[0]===O[0]&&B[1]===O[1]&&B[2]===O[2]||B[0]===S[0]&&B[1]===S[1]&&B[2]===S[2]||O[0]===S[0]&&O[1]===S[1]&&O[2]===S[2])continue;let k={vertices:[B,O,S],color:T.color};T.texture&&(k.texture=T.texture),T.uvs&&(k.uvs=T.uvs),L.push(k)}return{polygons:L,animation:_,objectUrls:f,dispose:b,warnings:[],metadata:{triangleCount:L.length,meshes:p,materials:h,animations:_?.clips,sourceBytes:a}}}function qc(e){let n=!1;return()=>{if(n)return;n=!0;let r=globalThis.URL;if(r?.revokeObjectURL)for(let t of e)try{r.revokeObjectURL(t)}catch{}}}function yt(e){return e.material?.texture??e.texture}function Jc(){let e=globalThis;return typeof e.Image!="function"||typeof e.document?.createElement!="function"?null:{Image:e.Image,createCanvas(){return e.document.createElement("canvas")}}}function ea(e,n){return new Promise((r,t)=>{let o=new n,s=!1,i=a=>{s||(s=!0,a())};o.decoding="async",o.onload=()=>i(()=>r(o)),o.onerror=()=>i(()=>t(new Error(`texture load failed: ${e}`))),o.src=e})}async function na(e,n,r){try{let t=await ea(e,n.Image),o=Math.max(0,Math.floor(t.naturalWidth||t.width||0)),s=Math.max(0,Math.floor(t.naturalHeight||t.height||0));if(o<=0||s<=0||o*s>r)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(t,0,0,o,s);let l=a.getImageData(0,0,o,s).data;return{width:o,height:s,data:l,lowDetail:ta(o,s,l)}}catch{return null}}function qr(e,n,r,t,o,s){let i=(t*n+r)*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 ta(e,n,r){let t=Math.max(1,Math.floor(Math.max(e,n)/128)),o=0,s=0,i=0;for(let a=0;a<n;a+=t)for(let l=0;l<e;l+=t){if(l+t<e){let c=qr(r,e,l,a,l+t,a);i+=c,o++,c>32&&s++}if(a+t<n){let c=qr(r,e,l,a,l,a+t);i+=c,o++,c>32&&s++}}return o>0&&s/o<=.045&&i/o<=10}function Jr(e,n,r){return Math.max(n,Math.min(r,e))}function ra(e,n){let r=n[0],t=1-n[1];if(!Number.isFinite(r)||!Number.isFinite(t))return null;let o=Jr(Math.floor(r*e.width),0,e.width-1),i=(Jr(Math.floor(t*e.height),0,e.height-1)*e.width+o)*4;return{r:e.data[i]??0,g:e.data[i+1]??0,b:e.data[i+2]??0,a:e.data[i+3]??255}}function Pe(e,n,r,t,o,s){return[e[0]*t+n[0]*o+r[0]*s,e[1]*t+n[1]*o+r[1]*s]}function oa(e){let[n,r,t]=e;return[Pe(n,r,t,1/3,1/3,1/3),Pe(n,r,t,.8,.1,.1),Pe(n,r,t,.1,.8,.1),Pe(n,r,t,.1,.1,.8),Pe(n,r,t,.45,.45,.1),Pe(n,r,t,.45,.1,.45),Pe(n,r,t,.1,.45,.45)]}function sa(e){let[n,r,t]=e,o=oa(e);for(let s=1;s<6;s++)for(let i=1;i<6-s;i++){let a=6-s-i;a<=0||o.push(Pe(n,r,t,s/6,i/6,a/6))}return o}function ro(e){if(e.textureTriangles?.length)return e.textureTriangles;let n=e.uvs;if(!n||n.length!==e.vertices.length||n.length<3)return[];let r=[];for(let t=1;t+1<n.length;t++)r.push({uvs:[n[0],n[t],n[t+1]]});return r}function eo(e,n,r){return Math.abs(e.r-n.r)<=r&&Math.abs(e.g-n.g)<=r&&Math.abs(e.b-n.b)<=r&&Math.abs(e.a-n.a)<=r}function no(e){let n=t=>Math.round(Math.max(0,Math.min(255,t))).toString(16).padStart(2,"0");if(e.a>=255)return`#${n(e.r)}${n(e.g)}${n(e.b)}`;let r=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)}, ${r})`}function ia(){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},count:0}}function ca(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,e.count++}function to(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 aa(e,n,r,t){let o=ro(e);if(o.length===0||!t&&!n.lowDetail)return null;let s=ia();for(let i of o)for(let a of sa(i.uvs)){let l=ra(n,a);if(!l)return null;ca(s,l)}return s.count===0?null:eo(s.min,s.max,r)?no(to(s)):t||!eo(s.min,s.max,32)?null:no(to(s))}function la(e,n){let{texture:r,material:t,uvs:o,textureTriangles:s,...i}=e;return{...i,color:n}}async function oo(e,n={}){if(n.enabled===!1)return null;let r=Jc();if(!r)return null;let t=e.filter(l=>yt(l)&&ro(l).length>0);if(t.length===0)return null;let o=n.maxTexturePixels??16777216,s=new Map;await Promise.all(Array.from(new Set(t.map(l=>yt(l)))).map(async l=>{s.set(l,await na(l,r,o))}));let i=n.colorTolerance??2,a=n.colorTolerance!==void 0;return{bake(l){let c=!1,u=l.map(f=>{let g=yt(f);if(!g)return f;let m=s.get(g);if(!m)return f;let d=aa(f,m,i,a);return d?(c=!0,la(f,d)):f});return{polygons:c?u:l,changed:c}}}}async function ua(e,n={}){let r=await oo(e,n);return r?r.bake(e).polygons:e}async function bt(e,n={}){let r=await oo(e.polygons,n);if(!r)return e;let t=r.bake(e.polygons);return!t.changed&&!e.animation?e:{...e,polygons:t.polygons,animation:e.animation?{...e.animation,sample(o,s){return r.bake(e.animation.sample(o,s)).polygons}}:e.animation}}var fa=[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 ma(e){let n=e>>0&255,r=e>>8&255,t=e>>16&255;return[n,r,t]}function Pt(e){return(e&255).toString(16).padStart(2,"0")}function io(e,n,r){return`#${Pt(e)}${Pt(n)}${Pt(r)}`}function ga(e,n,r,t){if(t>=255)return io(e,n,r);let o=Math.round(Math.max(0,Math.min(255,t))/255*1e3)/1e3;return`rgba(${e}, ${n}, ${r}, ${o})`}var da=542658390;function Mt(e,n){let r=n?.targetSize??60,t=n?.gridShift??0,o=e.byteLength;if(e.byteLength<8)return Ne(o,["parseVox: buffer too small to be a valid .vox file"]);let s=new DataView(e);if(s.getUint32(0,!0)!==da)return Ne(o,["parseVox: not a .vox file (bad magic)"]);if(e.byteLength<20)return Ne(o,["parseVox: buffer too small for MAIN chunk"]);if(so(s,8)!=="MAIN")return Ne(o,["parseVox: expected MAIN chunk at offset 8"]);let c=20+s.getUint32(12,!0),u=c+s.getUint32(16,!0),f=[],g=[],m=null;for(;c<u&&c+12<=e.byteLength;){let O=so(s,c),S=s.getUint32(c+4,!0),k=s.getUint32(c+8,!0),N=c+12,U=N+S+k;if(O==="SIZE"){if(S>=12&&N+12<=e.byteLength){let Y=s.getUint32(N,!0),G=s.getUint32(N+4,!0),$=s.getUint32(N+8,!0);f.push({sx:Y,sy:G,sz:$})}}else if(O==="XYZI"){if(S>=4&&N+4<=e.byteLength){let Y=s.getUint32(N,!0),G=[],$=Math.min(Y,Math.floor((S-4)/4));for(let W=0;W<$;W++){let q=N+4+W*4;G.push({x:s.getUint8(q),y:s.getUint8(q+1),z:s.getUint8(q+2),colorIndex:s.getUint8(q+3)})}g.push(G)}}else if(O==="RGBA"&&S>=1024&&N+1024<=e.byteLength){m=[];for(let Y=0;Y<256;Y++){let G=N+Y*4,$=s.getUint8(G),W=s.getUint8(G+1),q=s.getUint8(G+2),ee=s.getUint8(G+3);m.push(ga($,W,q,ee))}}c=U}let d=[],p=new Set;for(let O of g)for(let S of O){if(S.colorIndex===0)continue;let k=`${S.x},${S.y},${S.z}`;p.has(k)||(p.add(k),d.push(S))}if(d.length===0)return Ne(o,[]);let h=O=>{let S=O-1;if(m!==null)return m[S]??"#888888";let k=fa[O]??0,[N,U,Y]=ma(k);return io(N,U,Y)},x=(O,S,k)=>p.has(`${O},${S},${k}`),y=new Map,M=(O,S,k,N,U)=>{let Y=`${O}:${S}:${U}`,G=y.get(Y);G||(G=new Set,y.set(Y,G)),G.add(`${k},${N}`)};for(let O of d){let{x:S,y:k,z:N}=O,U=h(O.colorIndex);x(S+1,k,N)||M(0,S+1,k,N,U),x(S-1,k,N)||M(1,S,k,N,U),x(S,k+1,N)||M(2,k+1,S,N,U),x(S,k-1,N)||M(3,k,S,N,U),x(S,k,N+1)||M(4,N+1,S,k,U),x(S,k,N-1)||M(5,N,S,k,U)}function P(O){let S=new Set,k=[],N=1/0,U=-1/0,Y=1/0,G=-1/0;for(let $ of O){let[W,q]=$.split(","),ee=+W,H=+q;ee<N&&(N=ee),ee>U&&(U=ee),H<Y&&(Y=H),H>G&&(G=H)}for(let $=Y;$<=G;$++)for(let W=N;W<=U;W++){let q=`${W},${$}`;if(!O.has(q)||S.has(q))continue;let ee=1;for(;W+ee<=U;){let K=`${W+ee},${$}`;if(!O.has(K)||S.has(K))break;ee++}let H=1;e:for(;$+H<=G;){for(let K=0;K<ee;K++){let te=`${W+K},${$+H}`;if(!O.has(te)||S.has(te))break e}H++}for(let K=0;K<H;K++)for(let te=0;te<ee;te++)S.add(`${W+te},${$+K}`);k.push({u:W,v:$,w:ee,h:H})}return k}let b=(O,S,k,N,U,Y)=>{let G=k+U,$=N+Y;switch(O){case 0:return[[S,k,N],[S,G,N],[S,G,$],[S,k,$]];case 1:return[[S,G,N],[S,k,N],[S,k,$],[S,G,$]];case 2:return[[k,S,N],[k,S,$],[G,S,$],[G,S,N]];case 3:return[[G,S,N],[G,S,$],[k,S,$],[k,S,N]];case 4:return[[k,N,S],[G,N,S],[G,$,S],[k,$,S]];default:return[[k,$,S],[G,$,S],[G,N,S],[k,N,S]]}},A=[];for(let[O,S]of y){let k=O.indexOf(":"),N=O.indexOf(":",k+1),U=+O.slice(0,k),Y=+O.slice(k+1,N),G=O.slice(N+1);for(let{u:$,v:W,w:q,h:ee}of P(S))A.push({vertices:b(U,Y,$,W,q,ee),color:G})}if(A.length===0)return Ne(o,[]);let w=1/0,I=1/0,C=1/0,v=-1/0,V=-1/0,R=-1/0;for(let O of A)for(let S of O.vertices)S[0]<w&&(w=S[0]),S[0]>v&&(v=S[0]),S[1]<I&&(I=S[1]),S[1]>V&&(V=S[1]),S[2]<C&&(C=S[2]),S[2]>R&&(R=S[2]);let E=Math.max(v-w,V-I,R-C),F=E>0?r/E:1,D=F*50,z=Number.isFinite(D)&&D>0?Math.max(1,Math.round(D))/50:F,_={kind:"magica-vox",cells:d.map(O=>({x:O.x-w,y:O.y-I,z:O.z-C,color:h(O.colorIndex)})),rows:Math.max(0,v-w),cols:Math.max(0,V-I),depth:Math.max(0,R-C),scale:z,gridShift:t,sourceBytes:o},L=O=>Math.round(O*1e3)/1e3,T=O=>[L((O[0]-w)*z+t),L((O[1]-I)*z+t),L((O[2]-C)*z+t)],B=A.map(({vertices:O,color:S})=>({vertices:O.map(T),color:S}));return{polygons:B,voxelSource:_,objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:B.length,sourceBytes:o,voxelCount:d.length}}}function so(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 Ne(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:n,metadata:{triangleCount:0,sourceBytes:e}}}var pa=["t","b","bl","br","fr","fl"];var co={x:0,y:1,z:2},ao=new Map(pa.map((e,n)=>[e,n])),ha={t:1,fr:1,fl:1,b:-1,bl:-1,br:-1};var xa=(e,n)=>{let r=[{mask:e.mask,filledCount:e.filledCount,allowMask:null}];if(!n)return r;let{width:t,height:o}=e,s=new Uint8Array(t*o),i=n.minRow-e.minRow,a=n.minCol-e.minCol;for(let u=0;u<n.height;u+=1){let f=u+i;if(f<0||f>=o)continue;let g=f*t,m=u*n.width;for(let d=0;d<n.width;d+=1){if(!n.mask[m+d])continue;let p=d+a;if(p<0||p>=t)continue;let h=g+p;e.mask[h]||(s[h]=1)}}let l=0;for(let u=0;u<s.length;u+=1)l+=s[u]?1:0;if(!l)return r;let c=e.mask.slice();for(let u=0;u<s.length;u+=1)s[u]&&(c[u]=1);return r.push({mask:c,filledCount:e.filledCount+l,allowMask:s}),r},Sn=(e,n,r,t)=>{let{r0:o,c0:s,r1:i,c1:a}=r;if(i<=o||a<=s)return[];let l=[];if(!t){for(let c=o;c<i;c+=1){let u=c*n,f=s;for(;f<a;){for(;f<a&&!e[u+f];)f+=1;if(f>=a)break;let g=f;for(;f<a&&e[u+f];)f+=1;l.push({r0:c,c0:g,r1:c+1,c1:f})}}return l}for(let c=s;c<a;c+=1){let u=o;for(;u<i;){for(;u<i&&!e[u*n+c];)u+=1;if(u>=i)break;let f=u;for(;u<i&&e[u*n+c];)u+=1;l.push({r0:f,c0:c,r1:u,c1:c+1})}}return l},De=e=>{if(e.length<2)return e;e.sort((t,o)=>t.r0-o.r0||t.r1-o.r1||t.c0-o.c0||t.c1-o.c1);let n=[];for(let t of e){let o=n[n.length-1];if(o&&t.r0===o.r0&&t.r1===o.r1&&t.c0===o.c1){o.c1=t.c1;continue}n.push(t)}n.sort((t,o)=>t.c0-o.c0||t.c1-o.c1||t.r0-o.r0||t.r1-o.r1);let r=[];for(let t of n){let o=r[r.length-1];if(o&&t.c0===o.c0&&t.c1===o.c1&&t.r0===o.r1){o.r1=t.r1;continue}r.push(t)}return r},wn=(e,n,r)=>{let t={r0:0,c0:0,r1:r,c1:n},o=De(Sn(e,n,t,!1)),s=De(Sn(e,n,t,!0));return!o.length||s.length&&s.length<o.length?s:o},ya=(e,n,r,t,o,s)=>{t.fill(0);let i=null;for(let a=0;a<r;a+=1){let l=a*n;for(let u=0;u<n;u+=1)t[u]=e[l+u]?t[u]+1:0;let c=0;for(let u=0;u<=n;u+=1){let f=u<n?t[u]:0,g=u;for(;c>0&&s[c-1]>f;){c-=1;let m=s[c],d=o[c],p=m*(u-d);(!i||p>i.area)&&(i={r0:a-m+1,c0:d,r1:a+1,c1:u,area:p}),g=d}f>0&&(c===0||s[c-1]<f)&&(o[c]=g,s[c]=f,c+=1)}}return i},vo=(e,n,r,t)=>{if(t<=1)return null;let o=new Int32Array(n),s=new Int32Array(n+1),i=new Int32Array(n+1),a=[];for(;a.length<t;){let l=ya(e,n,r,o,s,i);if(!l||l.area<=0)return a;if(a.push({r0:l.r0,c0:l.c0,r1:l.r1,c1:l.c1}),a.length>=t)return null;for(let c=l.r0;c<l.r1;c+=1){let u=c*n;for(let f=l.c0;f<l.c1;f+=1)e[u+f]=0}}return null},ba=(e,n,r,t)=>{if(e.length<2||t<=1)return null;let o=r,s=n,i=-1,a=-1,l=0;for(let m of e)m.r0<o&&(o=m.r0),m.c0<s&&(s=m.c0),m.r1>i&&(i=m.r1),m.c1>a&&(a=m.c1),l+=(m.r1-m.r0)*(m.c1-m.c0);let c=a-s,u=i-o;if(c<=0||u<=0)return null;if(l===c*u)return t>1?[{r0:o,c0:s,r1:i,c1:a}]:null;let f=new Uint8Array(c*u);for(let m of e)for(let d=m.r0;d<m.r1;d+=1){let p=(d-o)*c;for(let h=m.c0;h<m.c1;h+=1)f[p+h-s]=1}let g=vo(f,c,u,t);return g?g.map(m=>({r0:m.r0+o,c0:m.c0+s,r1:m.r1+o,c1:m.c1+s})):null},Pa=(e,n,r)=>{let o=Sn(e,n,{r0:0,c0:0,r1:r,c1:n},!1);if(o.length<2)return o;let s=new Int32Array(o.length);for(let p=0;p<s.length;p+=1)s[p]=p;let i=p=>{let h=p;for(;s[h]!==h;)h=s[h];for(;s[p]!==p;){let x=s[p];s[p]=h,p=x}return h},a=(p,h)=>{let x=i(p),y=i(h);x!==y&&(s[y]=x)},l=0,c=0,u=-1,f=0;for(let p=0;p<o.length;p+=1){let h=o[p];if(h){h.r0!==u&&(h.r0===u+1?(l=f,c=p):(l=p,c=p),u=h.r0,f=p);for(let x=l;x<c;x+=1){let y=o[x];if(y&&!(y.c1<=h.c0)){if(y.c0>=h.c1)break;a(p,x)}}}}let g=new Int32Array(o.length);g.fill(-1);let m=[];for(let p=0;p<o.length;p+=1){let h=i(p),x=g[h]??-1;x<0&&(x=m.length,g[h]=x,m.push([])),m[x]?.push(p)}if(m.length<2)return o;let d=[];for(let p of m){let h=r,x=n,y=-1,M=-1,P=0;for(let C of p){let v=o[C];v&&(v.r0<h&&(h=v.r0),v.c0<x&&(x=v.c0),v.r1>y&&(y=v.r1),v.c1>M&&(M=v.c1),P+=v.c1-v.c0)}let b=M-x,A=y-h;if(P===b*A){d.push({r0:h,c0:x,r1:y,c1:M});continue}let w=new Uint8Array(b*A);for(let C of p){let v=o[C];if(!v)continue;let V=(v.r0-h)*b;for(let R=v.c0;R<v.c1;R+=1)w[V+R-x]=1}let I=wn(w,b,A);for(let C of I)d.push({r0:C.r0+h,c0:C.c0+x,r1:C.r1+h,c1:C.c1+x})}return d},lo=(e,n)=>{let{width:r,ids:t,palette:o}=n,s=new Map,i=e.c1-e.c0,a=e.r1-e.r0;for(let h=e.r0;h<e.r1;h+=1){let x=h*r;for(let y=e.c0;y<e.c1;y+=1){let M=t[x+y];if(!M)continue;let P=(s.get(M)??0)+1;s.set(M,P)}}if(!s.size)return[];let l=Array.from(s.keys()).sort((h,x)=>h-x),c=new Uint8Array(i*a),u=[],f=l[0]??0,g=-1,m=-1;for(let h of l){c.fill(0);for(let M=e.r0;M<e.r1;M+=1){let P=M*r,b=(M-e.r0)*i;for(let A=e.c0;A<e.c1;A+=1)t[P+A]===h&&(c[b+(A-e.c0)]=1)}let x=wn(c,i,a);u.push({colorId:h,rects:x});let y=s.get(h)??0;(x.length>g||x.length===g&&y>m||x.length===g&&y===m&&h<f)&&(f=h,g=x.length,m=y)}let d=o[f]??"";if(!d)return[];let p=[{...e,baseColor:d}];for(let{colorId:h,rects:x}of u){if(h===f||!x.length)continue;let y=o[h]??"";if(y)for(let M of x)p.push({r0:M.r0+e.r0,c0:M.c0+e.c0,r1:M.r1+e.r0,c1:M.c1+e.c0,baseColor:y})}return p},ie=(e,n,r,t)=>{let{width:o,height:s}=n,i=new Uint32Array(o*s);for(let l of e){let c=t.get(l.baseColor);if(!c)return!1;let u=Math.max(0,l.r0),f=Math.max(0,l.c0),g=Math.min(s,l.r1),m=Math.min(o,l.c1);for(let d=u;d<g;d+=1){let p=d*o;for(let h=f;h<m;h+=1)i[p+h]=c}}let a=n.ids;for(let l=0;l<i.length;l+=1)if(i[l]!==a[l]&&!(r&&!a[l]&&r[l]))return!1;return!0},uo=e=>{if(e.length<2)return e;let n=new Map;for(let t of e){let o=n.get(t.baseColor);o?o.push(t):n.set(t.baseColor,[t])}let r=[];for(let[,t]of n)r.push(...De(t.map(o=>({...o}))));return r},Fe=e=>Math.max(0,e.r1-e.r0)*Math.max(0,e.c1-e.c0),Ma=(e,n)=>e.r0<n.r1&&e.r1>n.r0&&e.c0<n.c1&&e.c1>n.c0,fo=(e,n,r,t)=>e<t&&n>r,Ao=(e,n)=>({r0:Math.min(e.r0,n.r0),c0:Math.min(e.c0,n.c0),r1:Math.max(e.r1,n.r1),c1:Math.max(e.c1,n.c1),baseColor:e.baseColor}),Vo=(e,n,r)=>{let{width:t,height:o}=n,s=new Int32Array(t*o),i=new Int32Array(t*o),a=new Int32Array(e.length);s.fill(-1),i.fill(-1);for(let l=0;l<e.length;l+=1){let c=e[l],u=r.get(c.baseColor);if(!u)return null;a[l]=u;let f=Math.max(0,c.r0),g=Math.max(0,c.c0),m=Math.min(o,c.r1),d=Math.min(t,c.c1);for(let p=f;p<m;p+=1){let h=p*t;for(let x=g;x<d;x+=1){let y=h+x;i[y]=s[y]??-1,s[y]=l}}}return{lastPaint:s,previousPaint:i,brushColorIds:a}},mo=(e,n,r,t,o,s,i,a,l,c)=>{let u=r.get(l.baseColor);if(!u)return!1;let f=e.ids,g=c?i:a,m=c?a:i;for(let d=l.r0;d<l.r1;d+=1){let p=d*e.width;for(let h=l.c0;h<l.c1;h+=1){let x=p+h;if(f[x]===u){if(c&&t[x]===m){let M=o[x]??-1;if(M>g&&s[M]!==u)return!1}continue}if(n&&!f[x]&&n[x])continue;let y=t[x]??-1;if(!(y>g&&y!==m))return!1}}return!0},Ca=e=>{let n=new Map,r=new Map;for(let i=0;i<e.length;i+=1){let a=e[i],l=`${a.baseColor}|r|${a.r0}|${a.r1}`,c=`${a.baseColor}|c|${a.c0}|${a.c1}`,u=n.get(l);u?u.push(i):n.set(l,[i]);let f=r.get(c);f?f.push(i):r.set(c,[i])}let t=new Set,o=[],s=(i,a)=>{let l=Math.min(i,a),c=Math.max(i,a);if(l===c)return;let u=`${l}:${c}`;if(t.has(u))return;t.add(u);let f=e[l],g=e[c];if(!f||!g||f.baseColor!==g.baseColor)return;let m=Ao(f,g),d=Fe(m)-Fe(f)-Fe(g);d<0||o.push({first:l,second:c,replacement:m,extraArea:d})};for(let i of n.values())if(!(i.length<2)){i.sort((a,l)=>e[a].c0-e[l].c0||e[a].c1-e[l].c1);for(let a=0;a<i.length-1;a+=1)s(i[a],i[a+1])}for(let i of r.values())if(!(i.length<2)){i.sort((a,l)=>e[a].r0-e[l].r0||e[a].r1-e[l].r1);for(let a=0;a<i.length-1;a+=1)s(i[a],i[a+1])}return o.sort((i,a)=>i.extraArea-a.extraArea||i.first-a.first||i.second-a.second),o},va=e=>{let n=new Map;for(let t=0;t<e.length;t+=1){let o=e[t],s=n.get(o.baseColor);s?s.push(t):n.set(o.baseColor,[t])}let r=[];for(let t of n.values())if(!(t.length<2))for(let o=0;o<t.length-1;o+=1){let s=t[o],i=e[s];if(i)for(let a=o+1;a<t.length;a+=1){let l=t[a],c=e[l];if(!c||!fo(i.r0,i.r1,c.r0,c.r1)&&!fo(i.c0,i.c1,c.c0,c.c1))continue;let u=Ao(i,c),f=Fe(u)-Fe(i)-Fe(c);r.push({first:s,second:l,replacement:u,extraArea:f})}}return r.sort((t,o)=>t.extraArea-o.extraArea||t.first-o.first||t.second-o.second),r},go=(e,n,r,t,o)=>{if(!n.length)return null;let s=Vo(e,r,o);if(!s)return null;let{lastPaint:i,previousPaint:a,brushColorIds:l}=s,c=new Uint8Array(e.length),u=new Uint8Array(e.length),f=[],g=0,m=[];for(let p of n){let{first:h,second:x,replacement:y}=p;if(c[h]||c[x])continue;let M=!1;for(let A of m)if(Ma(A,y)){M=!0;break}if(M)continue;let P=e[h],b=e[x];if(!(!P||!b||P.baseColor!==b.baseColor)){if(mo(r,t,o,i,a,l,h,x,y,!0)){c[h]=1,c[x]=1,f[h]=y,g+=1,u[x]=1,m.push(y);continue}mo(r,t,o,i,a,l,h,x,y,!1)&&(c[h]=1,c[x]=1,u[h]=1,f[x]=y,g+=1,m.push(y))}}if(!g)return null;let d=[];for(let p=0;p<e.length;p+=1)u[p]||d.push(f[p]??e[p]);return d},Aa=(e,n,r,t)=>{if(e.length<2)return e;let o=e,s=!1;for(;;){let i=go(o,Ca(o),n,r,t);if(i){o=i,s=!0;continue}let a=go(o,va(o),n,r,t);if(!a)break;o=a,s=!0}return s&&o.length<e.length&&ie(o,n,r,t)?o:e},Va=(e,n,r,t,o,s)=>{let i=s[n]??0;if(!i)return!1;for(let a=e.r0;a<e.r1;a+=1){let l=a*r.width;for(let c=e.c0;c<e.c1;c+=1){let u=l+c;if(t[u]!==n)continue;let f=o[u]??-1;if(f<0||s[f]!==i)return!1}}return!0},Sa=(e,n,r,t)=>{if(e.length<2)return e;let o=e,s=!1;for(;;){let i=Vo(o,n,t);if(!i)break;let{lastPaint:a,previousPaint:l,brushColorIds:c}=i,u=-1;for(let g=0;g<o.length;g+=1){let m=o[g];if(m&&Va(m,g,n,a,l,c)){u=g;break}}if(u<0)break;let f=[];for(let g=0;g<o.length;g+=1)g!==u&&f.push(o[g]);o=f,s=!0}return s&&o.length<e.length&&ie(o,n,r,t)?o:e},vt=e=>{let n=new Set;for(let r of e.ids)r&&n.add(r);return Array.from(n).sort((r,t)=>r-t)},wa=(e,n,r,t)=>{let{width:o,height:s,ids:i}=e,a=null,l=(u,f)=>!!n[u]||i[u]===f||!!t&&!i[u]&&!!t[u],c=(u,f,g,m,d)=>{let p=0;for(let x=f;x<m;x+=1){let y=x*o;for(let M=g;M<d;M+=1){let P=y+M,b=i[P];if(!l(P,u))return;!n[P]&&b===u&&(p+=1)}}if(!p)return;let h=(m-f)*(d-g);(!a||p>a.gain||p===a.gain&&h>a.area)&&(a={colorId:u,r0:f,c0:g,r1:m,c1:d,gain:p,area:h})};for(let u of r){for(let f=0;f<s;f+=1){let g=f*o,m=0;for(;m<o;){for(;m<o&&(n[g+m]||i[g+m]!==u);)m+=1;let d=m;for(;m<o&&!n[g+m]&&i[g+m]===u;)m+=1;if(d===m)continue;let p=f;e:for(;p>0;){let x=(p-1)*o;for(let y=d;y<m;y+=1){let M=x+y;if(!l(M,u))break e}p-=1}let h=f+1;e:for(;h<s;){let x=h*o;for(let y=d;y<m;y+=1){let M=x+y;if(!l(M,u))break e}h+=1}c(u,p,d,h,m)}}for(let f=0;f<o;f+=1){let g=0;for(;g<s;){for(;g<s&&(n[g*o+f]||i[g*o+f]!==u);)g+=1;let m=g;for(;g<s&&!n[g*o+f]&&i[g*o+f]===u;)g+=1;if(m===g)continue;let d=f;e:for(;d>0;){for(let h=m;h<g;h+=1){let x=h*o+d-1;if(!l(x,u))break e}d-=1}let p=f+1;e:for(;p<o;){for(let h=m;h<g;h+=1){let x=h*o+p;if(!l(x,u))break e}p+=1}c(u,m,d,g,p)}}}return a},po=(e,n,r,t=null)=>{let o=e.palette.length-1,s=e.width*e.height,i=t?4:3;if(t){if(r<20||r>64||o<2||o>4)return null}else if(r>=45&&r<=64&&o>=4&&o<=5&&e.filledCount<=650&&s<=1200)i=5;else if(r<150||o<2||o>3)return null;let a=vt(e);if(a.length<2||a.length>i)return null;let l=new Uint8Array(e.ids.length),c=e.filledCount,u=[];for(;c>0;){if(u.length>=r-1)return null;let g=wa(e,l,a,t);if(!g)return null;let m=e.palette[g.colorId]??"";if(!m)return null;u.push({r0:g.r0,c0:g.c0,r1:g.r1,c1:g.c1,baseColor:m});for(let d=g.r0;d<g.r1;d+=1){let p=d*e.width;for(let h=g.c0;h<g.c1;h+=1){let x=p+h;!l[x]&&e.ids[x]===g.colorId&&(l[x]=1,c-=1)}}}if(u.length>=r)return null;let f=u.reverse();return ie(f,e,t,n)?f:null},Ia=(e,n,r,t)=>{if(e.palette.length!==2||t<=2||t>32)return null;let o=e.palette[1]??"";if(!o)return null;let s=vo(n.mask.slice(),e.width,e.height,t);if(!s||s.length>=t)return null;let i=s.map(a=>({...a,baseColor:o}));return ie(i,e,n.allowMask,r)?i:null},Ra=42,ho=400,Ta=5200,Ea=20,Oa=128,ka=80,_a=32,xo=160,La=1600,Ba=256,So=[],Vn=new Map,wo=e=>{let n=e,r=0;for(;n;)n&=n-1n,r+=1;return r},Na=(e,n,r,t)=>{let o=new Map,s=new Uint8Array(n),i=new Array(n).fill(0n),a=(c,u,f,g,m)=>{if(!c)return!0;let d=(g-u)*(m-f),p=o.get(c);return!((!p||d<p.area||d===p.area&&g-u>p.r1-p.r0)&&(o.set(c,{r0:u,c0:f,r1:g,c1:m,bits:c,count:wo(c),area:d}),o.size>Ta))};for(let c=0;c<r;c+=1){s.fill(1),i.fill(0n);for(let u=c;u<r;u+=1){let f=u*n;for(let m=0;m<n;m+=1){if(!s[m])continue;let d=f+m;if(!e[d]){s[m]=0,i[m]=0n;continue}i[m]|=t[d]??0n}let g=0;for(;g<n;){for(;g<n&&!s[g];)g+=1;if(g>=n)break;let m=0n,d=g;for(;d<n&&s[d];)if(m|=i[d]??0n,d+=1,!a(m,c,g,u+1,d))return null;g=d+1}}}let l=Array.from(o.values());return l.sort((c,u)=>u.count-c.count||c.area-u.area||c.r0-u.r0||c.c0-u.c0),l},Io=(e,n,r,t,o,s)=>{let i=Na(e,n,r,t);if(!i||!i.length)return null;let a=[],l=o;for(;l&&a.length<s;){let c=-1,u=0;for(let f=0;f<i.length;f+=1){let g=i[f];if(!g)continue;if(g.count<=u)break;let m=wo(g.bits&l);m>u&&(u=m,c=f)}if(c<0||!u)break;a.push(c),l&=~(i[c]?.bits??0n)}return l||a.length>=s?null:a.map(c=>{let u=i[c];return{r0:u.r0,c0:u.c0,r1:u.r1,c1:u.c1}})},yo=(e,n)=>{if(!n.length){e.push("e");return}let r=n[0]?1:0,t=1;for(let o=1;o<n.length;o+=1){let s=n[o]?1:0;if(s===r){t+=1;continue}e.push(r?"1":"0",t.toString(36),","),r=s,t=1}e.push(r?"1":"0",t.toString(36))},Fa=(e,n,r)=>{let t=[e.width.toString(36),"x",e.height.toString(36),":",r.toString(36),":"];return yo(t,e.mask),t.push(":"),yo(t,n.mask),t.join("")},bo=(e,n)=>{Vn.size>=Ba&&Vn.clear(),Vn.set(e,n??So)},Po=(e,n,r,t)=>{if(e.palette.length!==2||t<=2||t>Ra||e.filledCount>ho)return null;if(n.allowMask){if(n.filledCount-e.filledCount<Ea)return null}else if(e.width*e.height>Oa||e.filledCount>ka)return null;let o=e.palette[1]??"";if(!o)return null;let s=Fa(e,n,t),i=Vn.get(s);if(i){if(i===So)return null;let g=i.map(m=>({...m,baseColor:o}));return ie(g,e,n.allowMask,r)?g:null}let a=new Array(e.ids.length).fill(0n),l=0,c=0n;for(let g=0;g<e.ids.length;g+=1){if(e.ids[g]!==1)continue;let m=1n<<BigInt(l);a[g]=m,l+=1,c|=m}if(!c||l>ho)return bo(s,null),null;let u=Io(n.mask,e.width,e.height,a,c,t);if(bo(s,u),!u)return null;let f=u.map(g=>({...g,baseColor:o}));return ie(f,e,n.allowMask,r)?f:null},Da=(e,n,r,t)=>{if(t<=2||t>_a||e.filledCount>xo)return null;let o=e.width*e.height;if(o>La)return null;let s=Math.max(1,Math.min(e.width,e.height)),i=Math.max(e.width,e.height);if(n.allowMask){if(o>800||o>64&&(o<700||e.filledCount<140))return null}else if(o>64&&(t<20||e.filledCount<100||i<s*4))return null;let a=vt(e);if(a.length!==2)return null;let l=null,c=[a,[a[1]??0,a[0]??0]];for(let u of c){let f=[];for(let g=0;g<u.length;g+=1){let m=u[g]??0,d=u.length-g-1,p=t-f.length-d;if(p<=1)break;let h=new Set(u.slice(g)),x=n.allowMask?n.allowMask.slice():new Uint8Array(e.ids.length),y=new Array(e.ids.length).fill(0n),M=0n,P=0;for(let w=0;w<e.ids.length;w+=1){let I=e.ids[w];if(!h.has(I))continue;x[w]=1;let C=1n<<BigInt(P);y[w]=C,P+=1,M|=C}if(!M||P>xo){f.length=t;break}let b=Io(x,e.width,e.height,y,M,p);if(!b){f.length=t;break}let A=e.palette[m]??"";if(!A){f.length=t;break}for(let w of b)f.push({...w,baseColor:A});if(f.length>=t)break}f.length<t&&ie(f,e,n.allowMask,r)&&(!l||f.length<l.length)&&(l=f)}return l},Mo=(e,n)=>{let r=[];for(let t of e)t!==n&&r.push(t);return r},za=(e,n,r)=>{let t=new Set(n),o=r?r.slice():new Uint8Array(e.ids.length),s=e.ids;for(let i=0;i<s.length;i+=1)t.has(s[i])&&(o[i]=1);return o},Ga=(e,n)=>{let{width:r,height:t,ids:o}=e;for(let s=0;s<t;s+=1){let i=s*r,a=0;for(;a<r;){for(;a<r&&!n[i+a];)a+=1;let l=a;for(;a<r&&n[i+a];)a+=1;if(l===a)continue;let c=l>0?o[i+l-1]:0,u=a<r?o[i+a]:0;if(c&&c===u)return!0}}for(let s=0;s<r;s+=1){let i=0;for(;i<t;){for(;i<t&&!n[i*r+s];)i+=1;let a=i;for(;i<t&&n[i*r+s];)i+=1;if(a===i)continue;let l=a>0?o[(a-1)*r+s]:0,c=i<t?o[i*r+s]:0;if(l&&l===c)return!0}}return!1},Ct=(e,n,r,t)=>{let o=n.join(","),s=t.get(o);if(s)return s;let i=za(e,n,r),a=wn(i,e.width,e.height);return t.set(o,a),a},Ua=(e,n,r,t)=>{let o=e.palette.length-1;if(o<3||t<=1)return null;let s=n.mask.length;if(n.filledCount*(o+2)<s*o)return null;let i=wn(n.mask,e.width,e.height);if(i.length+o-1>=t)return null;let a=vt(e),l=new Map;l.set(a.join(","),i);let c=a.slice(),u=[];for(;c.length;){let f=Ct(e,c,n.allowMask,l),g=c.length-1;if(u.length+f.length+g>=t)return null;let m=c[0]??0,d=Number.MAX_SAFE_INTEGER,p=-1;for(let y of c){let M=Mo(c,y),P=M.length?Ct(e,M,n.allowMask,l).length:0,b=Ct(e,[y],n.allowMask,l).length;(P<d||P===d&&b>p||P===d&&b===p&&y<m)&&(m=y,d=P,p=b)}if(u.length+f.length+d+Math.max(0,c.length-2)>=t)return null;let h=e.palette[m]??"";if(!h)return null;for(let y of f)u.push({...y,baseColor:h});let x=Mo(c,m);c.length=0,c.push(...x)}return u.length<t&&ie(u,e,n.allowMask,r)?u:null},Co=(e,n,r,t=null)=>{let o=e.palette.length-1;if(o<(t?1:2)||o>=r)return null;let{width:s,height:i,ids:a,palette:l}=e,c=[],u=[],f=[],g=[],m=[],d=[],p=[],h=0,x=C=>{let v=C;for(;m[v]!==v;)v=m[v]??v;for(;m[C]!==C;){let V=m[C]??C;m[C]=v,C=V}return v},y=(C,v)=>{let V=x(C),R=x(v);if(V!==R){m[R]=V,h-=1;let E=g[V]??g[R]??0;p[E]=Math.max(0,(p[E]??1)-1)}},M=0,P=0,b=-1,A=0;for(let C=0;C<i;C+=1){let v=C*s,V=0;for(;V<s;){let R=a[v+V];if(!R){V+=1;continue}let E=V;for(V+=1;V<s&&(a[v+V]===R||t&&t[v+V]);)V+=1;let F={r0:C,c0:E,r1:C+1,c1:V},D=f.length;f.push(F),g.push(R),m.push(D),h+=1,p[R]=(p[R]??0)+1;let z=c[R];z?z.push(F):c[R]=[F],C!==b&&(C===b+1?(M=A,P=D):(M=D,P=D),b=C,A=D);for(let _=M;_<P;_+=1){let L=f[_];if(L&&!(L.c1<=E)){if(L.c0>=V)break;g[_]===R&&y(D,_)}}}}if(h>=r)return null;for(let C=0;C<s;C+=1){let v=0;for(;v<i;){let V=a[v*s+C];if(!V){v+=1;continue}let R=v;for(v+=1;v<i&&(a[v*s+C]===V||t&&t[v*s+C]);)v+=1;let E=u[V],F={r0:R,c0:C,r1:v,c1:C+1};E?E.push(F):u[V]=[F]}}let w=[],I=0;for(let C=1;C<l.length;C+=1){let v=l[C]??"";if(!v)return null;let V=De(c[C]??[]),R=De(u[C]??[]);if(!V.length&&!R.length)continue;let E=R.length&&R.length<V.length?R:V,F=p[C]??E.length;I+=E.length,w.push({colorId:C,fill:v,rects:E,rowRects:V,componentCount:F})}if(I<r)for(let C of w){if(C.rects.length<=C.componentCount)continue;let v=ba(C.rowRects,s,i,C.rects.length);!v||v.length>=C.rects.length||(I+=v.length-C.rects.length,C.rects=v)}if(w.sort((C,v)=>{let V=C.rects.length-C.componentCount;return v.rects.length-v.componentCount-V||v.rects.length-C.rects.length}),I>=r)return null;for(let C of w)for(let v of C.rects)d.push({...v,baseColor:C.fill});return d.length<r&&ie(d,e,t,n)?d:null},$a=(e,n,r)=>{let t={r0:0,c0:0,r1:e.height,c1:e.width},o=null,s=-1,i=!1;for(let a of[!1,!0]){let l=De(Sn(n.mask,e.width,t,a));s<0?s=l.length:l.length!==s&&(i=!0);let c=[];for(let g of l)c.push(...lo(g,e));if(!ie(c,e,n.allowMask,r))continue;let u=c,f=uo(c);f.length<u.length&&ie(f,e,n.allowMask,r)&&(u=f),(!o||u.length<o.length)&&(o=u)}if(i){let a=Pa(n.mask,e.width,e.height);if(!o||a.length<o.length){let l=[];for(let c of a)l.push(...lo(c,e));if(ie(l,e,n.allowMask,r)){let c=l,u=uo(l);u.length<c.length&&ie(u,e,n.allowMask,r)&&(c=u),(!o||c.length<o.length)&&(o=c)}}}return o},Xa=(e,n)=>{let r=e.buffer,t=new Map;for(let c=1;c<r.palette.length;c+=1)t.set(r.palette[c],c);let o=(c,u)=>{let f=c,g=Aa(f,r,u,t);g.length<f.length&&(f=g);let m=Sa(f,r,u,t);return m.length<f.length&&(f=m),f},s=null,i=null,a=xa(r,n);for(let c of a){let u=$a(r,c,t);u&&(!s||u.length<s.length)&&(s=u,i=c.allowMask);let f=Ua(r,c,t,s?.length??Number.MAX_SAFE_INTEGER);f&&(!s||f.length<s.length)&&(s=f,i=c.allowMask)}s&&(s=o(s,i));let l=!1;for(let c of a){if(!c.allowMask||!Ga(r,c.allowMask))continue;let u=Co(r,t,s?.length??Number.MAX_SAFE_INTEGER,c.allowMask);if(u&&(!s||u.length<s.length)){let f=o(u,c.allowMask);(!s||f.length<s.length)&&(s=f,i=c.allowMask,l=!0)}}if(!l){let c=Co(r,t,s?.length??Number.MAX_SAFE_INTEGER);if(c&&(!s||c.length<s.length)){let u=o(c,null);(!s||u.length<s.length)&&(s=u,i=null)}}if(s){let c=po(r,t,s.length);if(c&&c.length<s.length){let u=o(c,null);s=u.length<c.length?u:c,i=null}if(e.key.axis==="z"&&e.key.face==="t")for(let u of a){if(!u.allowMask||u.filledCount-r.filledCount<20)continue;let f=po(r,t,s.length,u.allowMask);if(f&&f.length<s.length){let g=o(f,u.allowMask);s=g.length<f.length?g:f,i=u.allowMask}}}if(s)for(let c of a){let u=Da(r,c,t,s.length);if(u&&u.length<s.length&&(s=u,i=c.allowMask),!c.allowMask){let f=Po(r,c,t,s.length);f&&f.length<s.length&&(s=f,i=c.allowMask)}if(e.key.axis==="z"){let f=Ia(r,c,t,s.length);if(f&&f.length<s.length&&(s=f,i=c.allowMask),c.allowMask){let g=Po(r,c,t,s.length);g&&g.length<s.length&&(s=g,i=c.allowMask)}}}return{key:e.key,buffer:r,brushes:s??[]}},ja=e=>{let n=Math.max(0,Math.floor(e.rows)),r=Math.max(0,Math.floor(e.cols)),t=Math.max(0,Math.floor(e.depth));if(n<=0||r<=0||t<=0||e.cells.length===0)return[];let o=n*r,s=new Int32Array(o*t),i=[],a=new Map;for(let m of e.cells){let d=Math.floor(m.x),p=Math.floor(m.y),h=Math.floor(m.z);if(d<0||d>=n||p<0||p>=r||h<0||h>=t)continue;let x=h*o+d*r+p;s[x]||(s[x]=1,i.push(x),a.set(x,{color:m.color||"#cccccc"}))}let l=new Map,c=(m,d,p,h,x,y)=>{let M=`${m}:${d}:${p}`,P=l.get(M);P||(P={key:{axis:m,plane:d,face:p},minRow:x,minCol:y,maxRow:x,maxCol:y,cells:[]},l.set(M,P)),P.cells.push({row:x,col:y,color:h}),x<P.minRow&&(P.minRow=x),y<P.minCol&&(P.minCol=y),x>P.maxRow&&(P.maxRow=x),y>P.maxCol&&(P.maxCol=y)},u=(m,d,p)=>m>=0&&m<n&&d>=0&&d<r&&p>=0&&p<t&&s[p*o+m*r+d]!==0;for(let m of i){let d=Math.floor(m/o),p=m-d*o,h=Math.floor(p/r),x=p-h*r,y=a.get(m)?.color??"#cccccc";u(h,x,d+1)||c("z",d+1,"t",y,h,x),u(h,x,d-1)||c("z",d,"b",y,h,x),u(h,x-1,d)||c("y",x,"bl",y,h,d),u(h,x+1,d)||c("y",x+1,"fr",y,h,d),u(h-1,x,d)||c("x",h,"br",y,d,x),u(h+1,x,d)||c("x",h+1,"fl",y,d,x)}let f=Array.from(l.values()).sort((m,d)=>co[m.key.axis]-co[d.key.axis]||m.key.plane-d.key.plane||(ao.get(m.key.face)??0)-(ao.get(d.key.face)??0)),g=[];for(let m of f){m.cells.length>1&&m.cells.sort((b,A)=>b.row!==A.row?b.row-A.row:b.col-A.col);let d=m.maxCol-m.minCol+1,p=m.maxRow-m.minRow+1;if(d<=0||p<=0)continue;let h=new Uint32Array(d*p),x=[""],y=new Map,M=0;for(let b of m.cells){let A=b.row-m.minRow,w=b.col-m.minCol;if(A<0||w<0||A>=p||w>=d)continue;let I=A*d+w,C=y.get(b.color);C===void 0&&(C=x.length,y.set(b.color,C),x.push(b.color)),h[I]||(M+=1),h[I]=C}if(M===0)continue;let P=new Uint8Array(h.length);for(let b=0;b<h.length;b+=1)h[b]&&(P[b]=1);g.push({key:m.key,buffer:{width:d,height:p,minRow:m.minRow,minCol:m.minCol,ids:h,mask:P,filledCount:M,palette:x}})}return g};var Ve="loadMesh";function At(e,n){if(e.voxelSource)return e;let r=et(e.polygons,{meshResolution:n?.meshResolution});return r.length===e.polygons.length?e:{...e,polygons:r}}async function Ro(e,n){let r=n?.solidTextureSamples;return r===!1?e:bt(e,typeof r=="object"?r:void 0)}function Ya(e){let n=e.split("?")[0].split("#")[0],r=n.lastIndexOf(".");return r<0?"":n.slice(r+1).toLowerCase()}async function Ha(e,n){let r=Ya(e);if(r==="mtl")throw new Error(`${Ve}: .mtl is a material file, not a mesh \u2014 use parseMtl directly`);let t=globalThis.fetch;if(!t)throw new Error(`${Ve}: no fetch() in this environment`);let o=n?.baseUrl??e;if(r==="obj"){let s=await t(e);if(!s.ok)throw new Error(`${Ve}: ${e} \u2192 ${s.status}`);let i=await s.text(),a=n?.objOptions;if(n?.mtlUrl){let c=await t(n.mtlUrl);if(!c.ok)throw new Error(`${Ve}: ${n.mtlUrl} \u2192 ${c.status}`);let u=await c.text(),{colors:f,textures:g}=dt(u),m={},d=globalThis.URL,p=globalThis.document?.baseURI,h=n.mtlUrl;if(d&&p)try{h=new d(n.mtlUrl,p).toString()}catch{}for(let[x,y]of Object.entries(g)){if(d)try{m[x]=new d(y,h).toString();continue}catch{}let M=n.mtlUrl.lastIndexOf("/"),P=M>=0?n.mtlUrl.slice(0,M+1):"";m[x]=y.startsWith("/")||/^https?:\/\//.test(y)?y:P+y}a={...a??{},materialColors:{...f,...a?.materialColors??{}},materialTextures:{...m,...a?.materialTextures??{}}}}let l=mt(i,a);return At(await Ro(l,n),n)}if(r==="glb"||r==="gltf"){let s=await t(e);if(!s.ok)throw new Error(`${Ve}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer(),a=xt(i,{baseUrl:o,...n?.gltfOptions??{}});return At(await Ro(a,n),n)}if(r==="vox"){let s=await t(e);if(!s.ok)throw new Error(`${Ve}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return At(Mt(i,n?.voxOptions),n)}throw new Error(`${Ve}: unsupported extension ".${r}" (supported: obj, glb, gltf, vox)`)}export{en as BASE_TILE,Pn as CAMERA_BACKFACE_CULL_EPS,Te as DEFAULT_CAMERA_STATE,To as DEFAULT_PROJECTION,wc as LoopOnce,Rc as LoopPingPong,Ic as LoopRepeat,ha as POLY_VOXEL_NEXT_LAYER_STEP,$o as QUAT_IDENTITY,Ir as VOXEL_CAMERA_CULL_AXIS_EPS,Rr as VOXEL_CAMERA_CULL_NORMAL_LIMIT,Fr as arrowPolygons,_r as axesHelperPolygons,ua as bakeSolidTextureSampledPolygons,bt as bakeSolidTextureSamples,Br as boxPolygons,ja as buildPolyVoxelFaceData,Xa as buildPolyVoxelSlicePlan,Bo as buildSceneContext,Or as cameraCullNormalGroups,hc as cameraCullNormalGroupsFromPolygons,Er as cameraCullNormalKey,yc as cameraCullVisibleSignature,Tr as cameraFacingDepth,Ce as clampChannel,Bt as computeSceneBbox,Oo as computeShapeLighting,Go as computeTexturePaintMetrics,Wn as coverPlanarPolygons,Zo as createIsometricCamera,Oc as createPolyAnimationMixer,Oe as cullInteriorPolygons,ps as dedupeOverlappingPolygons,Ho as eulerXYZFromQuat,tn as findOverlappingPolygonDuplicates,Ze as formatColor,Uo as inverseRotateVec3,kr as isAxisAlignedSurfaceNormal,xc as isVoxelCameraCullableNormalGroups,Ha as loadMesh,le as mergePolygons,at as normalFacesCamera,Ko as normalizeInvertMultiplier,Gn as normalizePolygons,Ur as octahedronPolygons,et as optimizeMeshPolygons,Ie as parseColor,xt as parseGltf,wt as parseHexColor,dt as parseMtl,mt as parseObj,Fn as parsePureColor,It as parseRgbColor,Mt as parseVox,Gr as planePolygons,ct as polygonCssSurfaceNormal,No as polygonFaces,pc as polygonFacesCamera,jo as quatFromAxisAngle,Yo as quatFromEulerXYZ,Xo as quatMultiply,Dr as ringPolygons,zr as ringQuadPolygons,Je as rotateVec3,Eo as shadeColor};
|
|
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};
|