@layoutit/polycss-core 0.0.1 → 0.2.0
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 +25 -2
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +339 -3
- package/dist/index.d.ts +339 -3
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var xn="cubic";function St(e){let t=e.replace("#","");if(t.length===3){let r=parseInt(t[0]+t[0],16),n=parseInt(t[1]+t[1],16),o=parseInt(t[2]+t[2],16);return Number.isNaN(r)||Number.isNaN(n)||Number.isNaN(o)?null:{rgb:[r,n,o],alpha:1}}if(t.length===6){let r=parseInt(t.slice(0,2),16),n=parseInt(t.slice(2,4),16),o=parseInt(t.slice(4,6),16);return Number.isNaN(r)||Number.isNaN(n)||Number.isNaN(o)?null:{rgb:[r,n,o],alpha:1}}return null}function It(e){let t=e.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)/i);return t?{rgb:[Number(t[1]),Number(t[2]),Number(t[3])],alpha:t[4]?Number(t[4]):1}:null}function qe(e){if(!e)return null;let t=e.trim(),r=St(t);return r||It(t)}function me(e){return Math.max(0,Math.min(255,Math.round(e)))}function Le(e){let[t,r,n]=e.rgb.map(me);return e.alpha<1?`rgba(${t}, ${r}, ${n}, ${e.alpha})`:`rgb(${t}, ${r}, ${n})`}var tt={rgb:[204,204,204],alpha:1},Ct=new Map;function ye(e){if(!e)return null;let t=e.trim(),r=Ct.get(t);if(r)return r;let n=qe(t);return n?(Ct.set(t,n),n):null}function hn(e,t){let r=ye(e)??tt,n=[me(r.rgb[0]+t),me(r.rgb[1]+t),me(r.rgb[2]+t)];return Le({rgb:n,alpha:r.alpha})}var et={direction:[0,0,-1],color:"#ffffff",intensity:1},Tt={color:"#ffffff",intensity:.4};function Et(e){let t=Math.hypot(e[0],e[1],e[2]);return t<1e-12?[0,0,0]:[e[0]/t,e[1]/t,e[2]/t]}function Ot(e,t,r){let n=ye(t)??tt;return e*(n.rgb[r]/255)}function bn(e,t,r,n){let o=ye(t)??tt,i=Et(r?.direction??et.direction),s=r?.color??et.color,a=Math.max(0,r?.intensity??et.intensity),c=n?.color??Tt.color,f=Math.max(0,n?.intensity??Tt.intensity),l=Et(e),u=Math.max(0,-(l[0]*i[0]+l[1]*i[1]+l[2]*i[2])),g=a*u,m=[0,0,0];for(let p=0;p<3;p=p+1){let h=o.rgb[p],b=Ot(h,c,p)*f,y=Ot(h,s,p)*g;m[p]=me(b+y)}return Le({rgb:m,alpha:o.alpha})}var Rt="#cccccc",ke=(e,t)=>[e[0]-t[0],e[1]-t[1],e[2]-t[2]],Lt=(e,t)=>[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]],kt=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],Ue=e=>Math.hypot(e[0],e[1],e[2]);function yn(e){let t=1/0,r=1/0,n=1/0,o=-1/0,i=-1/0,s=-1/0;for(let l of e)l[0]<t&&(t=l[0]),l[0]>o&&(o=l[0]),l[1]<r&&(r=l[1]),l[1]>i&&(i=l[1]),l[2]<n&&(n=l[2]),l[2]>s&&(s=l[2]);let a=o-t,c=i-r,f=s-n;return Math.hypot(a,c,f)}function Pn(){let e=globalThis;if(e.__POLYCSS_DEV__===!0)return!0;if(e.__POLYCSS_DEV__===!1)return!1;let t=e.process?.env?.NODE_ENV;return typeof t=="string"?t!=="production":!1}function nt(e){let t=[],r=[];if(!e||e.length===0)return{polygons:t,warnings:r};for(let n=0;n<e.length;n++){let o=e[n];if(!o||!Array.isArray(o.vertices)){r.push(`Polygon ${n}: missing vertices, dropped`);continue}let i=o.vertices;if(i.length<3){r.push(`Polygon ${n}: ${i.length} vertices (need >= 3), dropped`);continue}let s=ke(i[1],i[0]),a=ke(i[2],i[0]),c=Lt(s,a),f=Ue(c);if(i.length===3){if(f<1e-12){Ue(s)<1e-12||Ue(a)<1e-12?r.push(`Polygon ${n}: zero-area triangle (coincident vertices), dropped`):r.push(`Polygon ${n}: vertices collinear, dropped`);continue}}else if(f<1e-12){r.push(`Polygon ${n}: first 3 vertices collinear, dropped`);continue}let l=[{...o,vertices:i.slice()}];if(i.length>=4){let u=[c[0]/f,c[1]/f,c[2]/f],g=kt(u,i[0]),m=yn(i),p=Math.max(1e-6,m*.001),h=0;for(let b=3;b<i.length;b++){let y=Math.abs(kt(u,i[b])-g);y>h&&(h=y)}if(h>p){let b=[],y=o.uvs&&o.uvs.length===i.length?o.uvs:void 0;for(let M=1;M<i.length-1;M++){let C=[i[0].slice(),i[M].slice(),i[M+1].slice()],v=Lt(ke(C[1],C[0]),ke(C[2],C[0]));if(Ue(v)<1e-12)continue;let x={...o,vertices:C};y&&(x.uvs=[y[0].slice(),y[M].slice(),y[M+1].slice()]),b.push(x)}if(b.length===0){r.push(`Polygon ${n}: ${i.length} non-coplanar vertices, fan-triangulation produced no valid triangles, dropped`);continue}r.push(`Polygon ${n}: ${i.length} non-coplanar vertices, fan-triangulated to ${b.length} triangles`),l=b}}for(let u of l){let g=An(u,n,r);g&&t.push(g)}}return{polygons:t,warnings:r}}function An(e,t,r){let n={vertices:e.vertices},o=e.texture;if(typeof o=="string"&&o===""&&(o=void 0),e.color!==void 0&&o!==void 0&&Pn()&&r.push(`Polygon ${t}: color and texture both set; texture wins`),e.color!==void 0&&(ye(e.color)?n.color=e.color:(r.push(`Polygon ${t}: invalid color "${e.color}", replaced with ${Rt}`),n.color=Rt)),o!==void 0&&(n.texture=o),e.uvs!==void 0&&(!Array.isArray(e.uvs)||e.uvs.length!==e.vertices.length?r.push(`Polygon ${t}: uvs length ${Array.isArray(e.uvs)?e.uvs.length:"?"} != vertices length ${e.vertices.length}, uvs stripped`):n.uvs=e.uvs),e.data!==void 0&&e.data!==null&&typeof e.data=="object"){let i={},s=!1;for(let a of Object.keys(e.data)){let c=e.data[a];typeof c=="string"||typeof c=="number"||typeof c=="boolean"?i[a]=c:(s=!0,r.push(`Polygon ${t}: data["${a}"] has non-primitive value, key dropped`))}Object.keys(i).length>0&&(n.data=i)}return n}function Ut(e){if(!e||e.length===0)return{min:[0,0,0],max:[0,0,0]};let t=1/0,r=1/0,n=1/0,o=-1/0,i=-1/0,s=-1/0,a=!1;for(let c of e)if(c?.vertices)for(let f of c.vertices)f&&(a=!0,f[0]<t&&(t=f[0]),f[0]>o&&(o=f[0]),f[1]<r&&(r=f[1]),f[1]>i&&(i=f[1]),f[2]<n&&(n=f[2]),f[2]>s&&(s=f[2]));return a?{min:[t,r,n],max:[o,i,s]}:{min:[0,0,0],max:[0,0,0]}}function Vn(e){let t=e.polygons??[],r,n;if(e.skipNormalize)r=t,n=[];else{let a=nt(t);r=a.polygons,n=a.warnings}let o=Ut(r),i=[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:n},dimensions:{sceneBbox:o,size:i},warnings:n}}function vn(e){return!e.vertices||e.vertices.length<3?[]:[{v:e.vertices.map(t=>[t[0],t[1],t[2]]),color:e.color}]}function Mn(e){let t=e[0],r=e[1],n=e[2],o=[r[0]-t[0],r[1]-t[1],r[2]-t[2]],i=[n[0]-t[0],n[1]-t[1],n[2]-t[2]],s=o[1]*i[2]-o[2]*i[1],a=o[2]*i[0]-o[0]*i[2],c=o[0]*i[1]-o[1]*i[0],f=Math.hypot(s,a,c);return f<=1e-9?null:[s/f,a/f,c/f]}function wn(e){let t=0;for(let r=0;r<e.length;r++){let[n,o]=e[r],[i,s]=e[(r+1)%e.length];t+=n*s-i*o}return Math.abs(t)/2}function Sn(e,t,r){if(!e||e.length<3)return null;let n=e.map(a=>[a[1]*t,a[0]*t,a[2]*r]),o=Mn(n);if(!o)return null;let i=1/0,s=0;for(let a=0;a<n.length;a++){let c=n[a],f=n[(a+1)%n.length],l=[f[0]-c[0],f[1]-c[1],f[2]-c[2]],u=l[0]*o[0]+l[1]*o[1]+l[2]*o[2],g=[l[0]-u*o[0],l[1]-u*o[1],l[2]-u*o[2]],m=Math.hypot(g[0],g[1],g[2]);if(m<=1e-9)continue;let p=[g[0]/m,g[1]/m,g[2]/m],h=[o[1]*p[2]-o[2]*p[1],o[2]*p[0]-o[0]*p[2],o[0]*p[1]-o[1]*p[0]],b=Math.hypot(h[0],h[1],h[2]);if(b<=1e-9)continue;let y=[h[0]/b,h[1]/b,h[2]/b],M=n.map(R=>{let F=R[0]-c[0],O=R[1]-c[1],z=R[2]-c[2];return[F*p[0]+O*p[1]+z*p[2],F*y[0]+O*y[1]+z*y[2]]}),C=1/0,v=1/0,x=-1/0,V=-1/0;for(let[R,F]of M)C=Math.min(C,R),v=Math.min(v,F),x=Math.max(x,R),V=Math.max(V,F);let I=x-C,S=V-v,E=Math.max(1,Math.ceil(I))*Math.max(1,Math.ceil(S)),U=wn(M);U<=1e-9||E<i&&(i=E,s=U)}return!Number.isFinite(i)||s<=1e-9?null:{elementArea:i,polygonArea:s}}function In(e,t={}){let r=t.tileSize??100,n=t.layerElevation??r,o=t.texturedOnly??!0,i=0,s=0,a=0,c=0,f=0;for(let u of e){let g=!!u.texture;if(o&&!g)continue;let m=Sn(u.vertices,r,n);m&&(i+=1,g&&(s+=1),a+=m.elementArea,c+=m.polygonArea,f=Math.max(f,Math.max(0,m.elementArea-m.polygonArea)/m.elementArea))}let l=Math.max(0,a-c);return{totalPolygons:e.length,measuredPolygons:i,texturedPolygons:s,elementArea:a,polygonArea:c,transparentArea:l,transparentRatio:a>0?l/a:0,overdrawRatio:c>0?a/c:0,worstTransparentRatio:f}}function Cn(e,t,r,n){let o=t*Math.PI/180,i=r*Math.PI/180,s=n*Math.PI/180,[a,c,f]=e;if(s!==0){let l=Math.cos(s),u=Math.sin(s);[a,c]=[a*l-c*u,a*u+c*l]}if(i!==0){let l=Math.cos(i),u=Math.sin(i);[a,f]=[a*l+f*u,-a*u+f*l]}if(o!==0){let l=Math.cos(o),u=Math.sin(o);[c,f]=[c*l-f*u,c*u+f*l]}return[a,c,f]}function Tn(e,t){let r=-t[0]*Math.PI/180,n=-t[1]*Math.PI/180,o=-t[2]*Math.PI/180,[i,s,a]=e;if(r!==0){let c=Math.cos(r),f=Math.sin(r);[s,a]=[s*c-a*f,s*f+a*c]}if(n!==0){let c=Math.cos(n),f=Math.sin(n);[i,a]=[i*c+a*f,-i*f+a*c]}if(o!==0){let c=Math.cos(o),f=Math.sin(o);[i,s]=[i*c-s*f,i*f+s*c]}return[i,s,a]}var En=50;function On(e){if(typeof e=="number")return e===0?void 0:e<0?-1:1;if(typeof e=="boolean")return e?-1:1}var Ae={target:[0,0,0],rotX:65,rotY:45,zoom:.65,distance:0},Nt=100,_t=1e4,Pe=e=>Math.round(e*Nt)/Nt,Rn=e=>Math.round(e*_t)/_t;function Ln(e={}){let t={target:e.target??[...Ae.target],rotX:e.rotX??Ae.rotX,rotY:e.rotY??Ae.rotY,zoom:e.zoom??Ae.zoom,distance:e.distance??Ae.distance};function r(o){o.target!==void 0&&(t.target=[Pe(o.target[0]),Pe(o.target[1]),Pe(o.target[2])]),o.rotX!==void 0&&(t.rotX=Pe(o.rotX)),o.rotY!==void 0&&(t.rotY=Pe(o.rotY)),o.zoom!==void 0&&(t.zoom=Rn(o.zoom)),o.distance!==void 0&&(t.distance=Pe(o.distance))}function n(o={}){let s=(o.cols??0)*50,a=(o.rows??0)*50,[c,f,l]=t.target,u=f*50,g=c*50,m=l*50;return{transform:`${t.distance!==0?` translateZ(${-t.distance}px)`:""}scale(${t.zoom}) rotateX(${t.rotX}deg) rotate(${t.rotY}deg) translate3d(${-u}px, ${-g}px, ${-m}px)`,width:`${s}px`,height:`${a}px`}}return{state:t,update:r,getStyle:n}}var le=(e,t)=>[e[0]-t[0],e[1]-t[1],e[2]-t[2]],Ne=(e,t)=>[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]],we=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],rt=e=>Math.hypot(e[0],e[1],e[2]),fe=(e,t)=>e[0]===t[0]&&e[1]===t[1]&&e[2]===t[2];function kn(e,t){let r=`${e[0]},${e[1]},${e[2]}`,n=`${t[0]},${t[1]},${t[2]}`;return r<n?`${r}|${n}`:`${n}|${r}`}function $t(e){if(e.length<3)return null;let t=le(e[1],e[0]),r=le(e[2],e[0]),n=Ne(t,r),o=rt(n);if(o<1e-12)return null;let i=[n[0]/o,n[1]/o,n[2]/o],s=we(i,e[0]);return{normal:i,d:s}}function Un(e,t){return we(e.normal,t.normal)<1-.001?!1:Math.abs(e.d-t.d)<.05}function Nn(e,t){return we(e.normal,t.normal)<1-.001?!1:Math.abs(e.d-t.d)<.001}function _n(e,t,r,n){let o=e.vertices.findIndex(S=>fe(S,r)),i=e.vertices.findIndex(S=>fe(S,n)),s=t.vertices.findIndex(S=>fe(S,r)),a=t.vertices.findIndex(S=>fe(S,n));if(o<0||i<0||s<0||a<0)return null;let c=e.vertices.length,f=t.vertices.length,l=(o+1)%c===i,u=(s+1)%f===a;if(l===u)return null;let g=l?i:o,m=l?o:i,p=u?a:s,h=u?s:a,b=!!(e.uvs&&t.uvs),y=[],M=b?[]:void 0,C=g;for(;y.push(e.vertices[C]),M&&M.push(e.uvs[C]),C!==m;)C=(C+1)%c;for(C=p;y.push(t.vertices[C]),M&&M.push(t.uvs[C]),C!==h;)C=(C+1)%f;let v=[],x=M?[]:void 0;for(let S=0;S<y.length;S++)(v.length===0||!fe(y[S],v[v.length-1]))&&(v.push(y[S]),x&&M&&x.push(M[S]));if(v.length>1&&fe(v[0],v[v.length-1])&&(v.pop(),x?.pop()),b)return Bn(v,x);let V=[],I=x?[]:void 0;for(let S=0;S<v.length;S++){let E=v[(S-1+v.length)%v.length],U=v[S],R=v[(S+1)%v.length],F=Ne(le(U,E),le(R,E));rt(F)>1e-9&&(V.push(U),I&&x&&I.push(x[S]))}return V.length<3?null:{vertices:V,uvs:I}}function $n(e,t){let r=e.length,n=0;for(let o=0;o<r;o++){let i=e[o],s=e[(o+1)%r],a=e[(o+2)%r],c=le(s,i),f=le(a,s),l=we(Ne(c,f),t);if(Math.abs(l)<1e-9)continue;let u=l>0?1:-1;if(n===0)n=u;else if(u!==n)return!1}return!0}function zn(e){if(e.length<3)return!1;let t=$t(e);if(!t)return!1;for(let r of e)if(Math.abs(we(t.normal,r)-t.d)>.001)return!1;return!0}function Fn(e){return e.map(t=>({vertices:t.vertices.map(r=>[...r]),uvs:t.uvs.map(r=>[...r])}))}function Gn(e,t){let r=[];for(let n=1;n<e.length-1;n++)r.push({vertices:[[...e[0]],[...e[n]],[...e[n+1]]],uvs:[[...t[0]],[...t[n]],[...t[n+1]]]});return r}function Bn(e,t){for(let r=0;r<e.length;r++){let n=e[r],o=e[(r+1)%e.length],i=e[(r+2)%e.length];if(!(rt(Ne(le(o,n),le(i,n)))<=1e-9))return r===0?{vertices:e,uvs:t}:{vertices:[...e.slice(r),...e.slice(0,r)],uvs:t?[...t.slice(r),...t.slice(0,r)]:void 0}}return{vertices:e,uvs:t}}function Se(e){let t=[],r=[];for(let o of e??[]){if(!o||!o.vertices||o.vertices.length<3){o&&t.push(o);continue}let i=o.vertices.map(f=>[f[0],f[1],f[2]]),s=$t(i);if(!s){t.push(o);continue}let a=o.texture&&o.uvs&&o.uvs.length===i.length?o.uvs.map(f=>[f[0],f[1]]):void 0,c=o.texture&&a?o.textureTriangles?.length?Fn(o.textureTriangles):Gn(i,a):void 0;r.push({vertices:i,uvs:a,color:o.color??"#cccccc",texture:o.texture,textureTriangles:c,normal:s.normal,d:s.d,alive:!0,data:o.data})}let n=()=>{let o=new Map;for(let a=0;a<r.length;a++){let c=r[a];if(!c.alive)continue;let f=c.vertices.length;for(let l=0;l<f;l++){let u=c.vertices[l],g=c.vertices[(l+1)%f],m=kn(u,g),p=o.get(m);p||(p=[],o.set(m,p)),p.push(a)}}let i=!1,s=(a,c)=>{for(let f=0;f<a.vertices.length;f++){let l=a.vertices[f],u=a.vertices[(f+1)%a.vertices.length];for(let g=0;g<c.vertices.length;g++){let m=c.vertices[g],p=c.vertices[(g+1)%c.vertices.length];if(fe(l,p)&&fe(u,m))return[l,u]}}return null};for(let[,a]of o){if(a.length<2)continue;let[c,f]=a;if(c===f)continue;let l=r[c],u=r[f];if(!l.alive||!u.alive||l.color!==u.color||l.texture!==u.texture)continue;let g=!!(l.texture||u.texture);if(g&&(!l.textureTriangles||!u.textureTriangles)||!!l.uvs!=!!u.uvs||(g?!Nn(l,u):!Un(l,u)))continue;let m=s(l,u);if(!m)continue;let[p,h]=m,b=_n(l,u,p,h);b&&(g&&!zn(b.vertices)||$n(b.vertices,l.normal)&&(l.vertices=b.vertices,l.uvs=b.uvs,l.textureTriangles=g?[...l.textureTriangles??[],...u.textureTriangles??[]]:void 0,u.alive=!1,i=!0))}return i};for(;n(););for(let o of r){if(!o.alive)continue;let i={vertices:o.vertices,color:o.color};o.texture&&(i.texture=o.texture),o.uvs&&(i.uvs=o.uvs),o.textureTriangles?.length&&(i.textureTriangles=o.textureTriangles),o.data&&(i.data=o.data),t.push(i)}return t}var Dn=4,jn=8,Xn=.001,Yn=1e-9,Hn=1e6,Wn=1e6,ge=(e,t)=>[e[0]-t[0],e[1]-t[1],e[2]-t[2]],st=(e,t)=>[e[0]+t[0],e[1]+t[1],e[2]+t[2]],Te=(e,t)=>[e[0]*t,e[1]*t,e[2]*t],de=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],Dt=(e,t)=>[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]],jt=e=>Math.hypot(e[0],e[1],e[2]);function Ee(e){let t=jt(e);return t<=Yn?null:[e[0]/t,e[1]/t,e[2]/t]}function Ce(e,t){return Math.round(e*t)/t}function se(e){return Ce(e,Hn)}function ot(e){return Ce(e,Wn)}function it(e,t){return Math.abs(e[0]-t[0])<=1e-7&&Math.abs(e[1]-t[1])<=1e-7}function ce(e){return`${se(e[0])},${se(e[1])}`}function Zn(e){let t=ce(e.a),r=ce(e.b);return t<r?`${t}|${r}`:`${r}|${t}`}function Ve(e){return`${e[0]},${e[1]},${e[2]}`}function Xt(e,t){let r=Ve(e),n=Ve(t);return r<n?`${r}|${n}`:`${n}|${r}`}function ct(e){return e?Object.keys(e).sort().map(t=>`${t}:${String(e[t])}`).join("|"):""}function zt(e){return[e.color??"#cccccc",e.texture??"",e.uvs?"uv":"plain",ct(e.data)].join("|")}function Yt(e){let t=e.vertices;if(!t||t.length<3)return null;let r=t[0],n=[0,0,0];for(let i=1;i<t.length-1;i++)n=st(n,Dt(ge(t[i],r),ge(t[i+1],r)));let o=Ee(n);return o?{normal:o,d:de(o,r)}:null}function Kn(e,t,r){return de(e.normal,t.normal)>1-r&&Math.abs(e.d-t.d)<=r}function Jn(e){return!e.texture&&!e.uvs&&!e.textureTriangles}function Qn(e,t){let r=t[e[0]]?.data,n=ct(r);for(let o of e)if(ct(t[o].data)!==n)return;return r?{...r}:void 0}function qn(e,t){let r=e.map(c=>Jn(c)?Yt(c):null),n=r.map(Boolean),o=new Map;for(let c=0;c<e.length;c++){if(!r[c])continue;let f=e[c].vertices;for(let l=0;l<f.length;l++){let u=Xt(f[l],f[(l+1)%f.length]),g=o.get(u),m={polygon:c,edge:l};g?g.push(m):o.set(u,[m])}}let i=e.map(()=>new Set);for(let c of o.values())if(!(c.length<2))for(let f=0;f<c.length;f++)for(let l=f+1;l<c.length;l++){let u=c[f].polygon,g=c[l].polygon,m=r[u],p=r[g];!m||!p||zt(e[u])===zt(e[g])&&Kn(m,p,t)&&(i[u].add(g),i[g].add(u))}let s=new Set,a=[];for(let c=0;c<e.length;c++){if(!n[c]||s.has(c))continue;let f=[],l=[c];for(s.add(c);l.length>0;){let u=l.shift();f.push(u);for(let g of i[u])s.has(g)||(s.add(g),l.push(g))}a.push(f)}return{groups:a}}function er(e,t){let r=new Set(e),n=new Map;for(let i of e){let s=t[i].vertices;for(let a=0;a<s.length;a++){let c=Xt(s[a],s[(a+1)%s.length]),f=n.get(c),l={polygon:i,edge:a};f?f.push(l):n.set(c,[l])}}let o=[];for(let i of n.values()){let s=i.filter(a=>r.has(a.polygon));if(s.length===1){let a=s[0],c=t[a.polygon].vertices;o.push({a:c[a.edge],b:c[(a.edge+1)%c.length]})}else if(s.length!==2)return null}return o}function tr(e){let t=new Map;for(let r of e)t.set(Ve(r.a),(t.get(Ve(r.a))??0)+1),t.set(Ve(r.b),(t.get(Ve(r.b))??0)+1);for(let r of t.values())if(r%2!==0)return!1;return!0}function Ht(e){let t=[e[0],e[1],e[2]],r=Math.abs(t[0])>=Math.abs(t[1])&&Math.abs(t[0])>=Math.abs(t[2])?0:Math.abs(t[1])>=Math.abs(t[2])?1:2;return t[r]<0?[-t[0],-t[1],-t[2]]:t}function nr(e){let t=Ht(e);return`${Ce(t[0],1e3)},${Ce(t[1],1e3)},${Ce(t[2],1e3)}`}function rr(e){let t=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],r=ge(t,Te(e,de(t,e)));return Ee(r)??[1,0,0]}function or(e,t,r){let n=new Map;for(let i of e){let s=ge(i.b,i.a),a=ge(s,Te(t,de(s,t))),c=Ee(a);if(!c)continue;let f=Ht(c),l=nr(f),u=jt(a),g=n.get(l);g?g.weight+=u:n.set(l,{axis:f,weight:u})}let o=[...n.values()].sort((i,s)=>s.weight-i.weight).slice(0,r).map(i=>i.axis);return o.length===0&&o.push(rr(t)),o}function Ft(e,t,r,n){let o=ge(e,t);return[se(de(o,r)),se(de(o,n))]}function ir(e){let t=[...e].sort((n,o)=>n-o),r=[];for(let n of t)(r.length===0||Math.abs(n-r[r.length-1])>1e-7)&&r.push(n);return r}function Ie(e,t){let[r,n]=e.a,[o,i]=e.b,s=(t-r)/(o-r);return se(n+(i-n)*s)}function at(e){let t=[];for(let n of e)(t.length===0||Math.abs(n[0]-t[t.length-1][0])>1e-7||Math.abs(n[1]-t[t.length-1][1])>1e-7)&&t.push(n);t.length>1&&Math.abs(t[0][0]-t[t.length-1][0])<=1e-7&&Math.abs(t[0][1]-t[t.length-1][1])<=1e-7&&t.pop();let r=[];for(let n=0;n<t.length;n++){let o=t[(n-1+t.length)%t.length],i=t[n],s=t[(n+1)%t.length],a=(i[0]-o[0])*(s[1]-i[1])-(i[1]-o[1])*(s[0]-i[0]);Math.abs(a)>1e-8&&r.push(i)}return r}function $e(e){let t=0;for(let r=0;r<e.length;r++){let n=e[r],o=e[(r+1)%e.length];t+=n[0]*o[1]-o[0]*n[1]}return t/2}function Gt(e){let t=1/0,r=1/0,n=-1/0,o=-1/0;for(let[i,s]of e)t=Math.min(t,i),r=Math.min(r,s),n=Math.max(n,i),o=Math.max(o,s);return{minX:t,minY:r,maxX:n,maxY:o}}function sr(e,t){let r=Gt(e),n=Gt(t);return!(r.maxX<n.minX-1e-7||n.maxX<r.minX-1e-7||r.maxY<n.minY-1e-7||n.maxY<r.minY-1e-7)}function _e(e){return Math.abs($e(e))}function cr(e){if(e.length<3)return!1;let t=0;for(let r=0;r<e.length;r++){let n=e[r],o=e[(r+1)%e.length],i=e[(r+2)%e.length],s=(o[0]-n[0])*(i[1]-o[1])-(o[1]-n[1])*(i[0]-o[0]);if(Math.abs(s)<=1e-8)continue;let a=s>0?1:-1;if(t===0)t=a;else if(t!==a)return!1}return!0}function ar(e,t,r){let n=r[0]-t[0],o=r[1]-t[1],i=e[0]-t[0],s=e[1]-t[1],a=n*s-o*i,c=Math.hypot(n,o);if(c<=1e-9||Math.abs(a)>Math.max(1e-8,c*1e-8))return!1;let f=i*n+s*o;return f>=-1e-8&&f<=n*n+o*o+1e-8}function fr(e,t,r){let n=r[0]-t[0],o=r[1]-t[1],i=n*n+o*o;return i<=1e-12?0:((e[0]-t[0])*n+(e[1]-t[1])*o)/i}function Bt(e,t){let r=[];for(let n=0;n<e.length;n++){let o=e[n],i=e[(n+1)%e.length],s=[{t:0,point:o},{t:1,point:i}];for(let c of t)it(c,o)||it(c,i)||ar(c,o,i)&&s.push({t:fr(c,o,i),point:c});s.sort((c,f)=>c.t-f.t);let a=[];for(let c of s)a.some(f=>Math.abs(f.t-c.t)<=1e-8||it(f.point,c.point))||a.push(c);for(let c=0;c<a.length-1;c++){let f=a[c].point,l=a[c+1].point;Math.hypot(l[0]-f[0],l[1]-f[1])<=1e-8||r.push({a:[se(f[0]),se(f[1])],b:[se(l[0]),se(l[1])]})}}return r}function lr(e,t){if(!sr(e,t))return null;let r=[...Bt(e,t),...Bt(t,e)],n=new Map;for(let h of r){let b=Zn(h),y=n.get(b);y?y.push(h):n.set(b,[h])}let o=!1,i=[];for(let h of n.values()){if(h.length===1){i.push(h[0]);continue}o=!0;let b=h.filter(M=>ce(M.a)<ce(M.b)).length,y=h.length-b;if(b!==y)return null}if(!o||i.length<3)return null;let s=new Map;for(let h of i){let b=ce(h.a);if(s.has(b))return null;s.set(b,h)}let a=i[0],c=ce(a.a),f=[],l=new Set,u=c;for(let h=0;h<=i.length;h++){let b=s.get(u);if(!b)return null;let y=`${ce(b.a)}>${ce(b.b)}`;if(l.has(y))return null;if(l.add(y),f.push(b.a),u=ce(b.b),u===c)break}if(u!==c||l.size!==i.length)return null;let g=at(f);if(g.length<3||!cr(g))return null;let m=_e(g),p=_e(e)+_e(t);return Math.abs(m-p)>Math.max(1e-5,p*1e-5)?null:$e(g)>=0?g:[...g].reverse()}function ur(e){let t=e.map(at).filter(n=>n.length>=3&&_e(n)>1e-8),r=!0;for(;r;){r=!1;for(let n=0;n<t.length;n++){for(let o=n+1;o<t.length;o++){let i=lr(t[n],t[o]);if(i){t[n]=i,t.splice(o,1),r=!0;break}}if(r)break}}return t}function mr(e,t,r){let n=new Map;return o=>{let i=se(o[0]),s=se(o[1]),a=`${i},${s}`,c=n.get(a);if(c)return[c[0],c[1],c[2]];let f=st(e,st(Te(t,i),Te(r,s))),l=[ot(f[0]),ot(f[1]),ot(f[2])];return n.set(a,l),[l[0],l[1],l[2]]}}function gr(e,t,r,n,o){let i=r[0]?.a;if(!i)return null;let s=Ee(ge(o,Te(n,de(o,n))));if(!s)return null;let a=Ee(Dt(n,s));if(!a)return null;let c=[],f=[];for(let b of r){let y=Ft(b.a,i,s,a),M=Ft(b.b,i,s,a);Math.hypot(y[0]-M[0],y[1]-M[1])<=1e-7||(c.push({a:y,b:M}),f.push(y[0],M[0]))}let l=ir(f);if(c.length<3||l.length<2)return null;let u=t[e[0]].color,g=Qn(e,t),m=[];for(let b=0;b<l.length-1;b++){let y=l[b],M=l[b+1];if(M-y<=1e-7)continue;let C=(y+M)/2,v=c.filter(x=>{let V=Math.min(x.a[0],x.b[0]),I=Math.max(x.a[0],x.b[0]);return V<C&&C<I&&Math.abs(x.a[0]-x.b[0])>1e-7}).map(x=>({segment:x,yMid:Ie(x,C)})).sort((x,V)=>x.yMid-V.yMid);if(v.length!==0){if(v.length%2!==0)return null;for(let x=0;x<v.length;x+=2){let V=v[x].segment,I=v[x+1].segment,S=Ie(V,y),E=Ie(V,M),U=Ie(I,y),R=Ie(I,M),F=at([[y,S],[M,E],[M,R],[y,U]]);if(F.length<3||Math.abs($e(F))<=1e-8)continue;let O=$e(F)>0?F:[...F].reverse();m.push(O)}}}if(m.length===0)return null;let p=mr(i,s,a),h=ur(m).map(b=>({vertices:b.map(p),...u?{color:u}:{},...g?{data:g}:{}}));return Se(h)}function dr(e,t,r){let n=er(e,t);if(!n||n.length<3||!tr(n))return null;let o=Yt(t[e[0]]);if(!o)return null;let i=null;for(let s of or(n,o.normal,r)){let a=gr(e,t,n,o.normal,s);a&&(!i||a.length<i.length)&&(i=a)}return!i||i.length>=e.length?null:i}function pr(e,t={}){let r=t.minGroupPolygons??Dn,n=t.maxCandidateAxes??jn,o=t.planeEpsilon??Xn,i=e??[];if(i.length<r)return i;let{groups:s}=qn(i,o);if(s.length===0)return i;let a=new Map,c=new Set;for(let l of s){if(l.length<r)continue;let u=dr(l,i,n);if(u){a.set(l[0],u);for(let g of l)c.add(g)}}if(a.size===0)return i;let f=[];for(let l=0;l<i.length;l++){let u=a.get(l);if(u){f.push(...u);continue}c.has(l)||f.push(i[l])}return f}function xr(e){let t=e.vertices;if(!t||t.length<3)return null;let r=0,n=0,o=0;for(let[O,z,L]of t)r+=O,n+=z,o+=L;let i=1/t.length;r*=i,n*=i,o*=i;let s=t[0],a=t[1],c=t[2],f=a[0]-s[0],l=a[1]-s[1],u=a[2]-s[2],g=c[0]-s[0],m=c[1]-s[1],p=c[2]-s[2],h=l*p-u*m,b=u*g-f*p,y=f*m-l*g,M=Math.hypot(h,b,y);if(M<1e-9)return null;h/=M,b/=M,y/=M;let C=t.length-2,v=new Float64Array(C*9),x=0;for(let O=1;O<t.length-1;O++){let z=t[0],L=t[O],_=t[O+1];v[x++]=z[0],v[x++]=z[1],v[x++]=z[2],v[x++]=L[0],v[x++]=L[1],v[x++]=L[2],v[x++]=_[0],v[x++]=_[1],v[x++]=_[2]}let V=0,I=1/0,S=1/0,E=1/0,U=-1/0,R=-1/0,F=-1/0;for(let[O,z,L]of t){let _=O-r,T=z-n,P=L-o,d=_*_+T*T+P*P;d>V&&(V=d),O<I&&(I=O),O>U&&(U=O),z<S&&(S=z),z>R&&(R=z),L<E&&(E=L),L>F&&(F=L)}return{centroid:[r,n,o],normal:[h,b,y],vertices:t,triFlat:v,bcx:r,bcy:n,bcz:o,br2:V,minX:I,minY:S,minZ:E,maxX:U,maxY:R,maxZ:F}}function hr(e,t,r,n,o,i,s,a){let c=s[a],f=s[a+1],l=s[a+2],u=s[a+3]-c,g=s[a+4]-f,m=s[a+5]-l,p=s[a+6]-c,h=s[a+7]-f,b=s[a+8]-l,y=o*b-i*h,M=i*p-n*b,C=n*h-o*p,v=u*y+g*M+m*C;if(v>-1e-9&&v<1e-9)return!1;let x=1/v,V=e-c,I=t-f,S=r-l,E=x*(V*y+I*M+S*C);if(E<0||E>1)return!1;let U=I*m-S*g,R=S*u-V*m,F=V*g-I*u,O=x*(n*U+o*R+i*F);return O<0||E+O>1?!1:x*(p*U+h*R+b*F)>.001}function br(e,t,r,n,o,i,s){let a=s.bcx-e,c=s.bcy-t,f=s.bcz-r,l=a*n+c*o+f*i,u=a-l*n,g=c-l*o,m=f-l*i;if(u*u+g*g+m*m>s.br2)return!1;let p=s.triFlat,h=p.length;for(let b=0;b<h;b+=9)if(hr(e,t,r,n,o,i,p,b))return!0;return!1}var Oe=9,yr=6,ee=12;function ft(e,t,r,n,o,i){let s=n-e,a=o-t,c=i-r;return s*a+a*c+c*s}function Pr(e){let t=[];for(let V=0;V<e.length;V++)e[V]&&t.push(V);let r=t.length,n=new Int32Array(r);for(let V=0;V<r;V++)n[V]=t[V];let o=new Float64Array(r),i=new Float64Array(r),s=new Float64Array(r);for(let V=0;V<r;V++){let I=e[n[V]];o[V]=(I.minX+I.maxX)*.5,i[V]=(I.minY+I.maxY)*.5,s[V]=(I.minZ+I.maxZ)*.5}let a=2*Math.max(1,r)+1,c=new Float64Array(a*Oe),f=0,l=new Float64Array(ee),u=new Float64Array(ee),g=new Float64Array(ee),m=new Float64Array(ee),p=new Float64Array(ee),h=new Float64Array(ee),b=new Int32Array(ee),y=new Float64Array(ee-1),M=new Int32Array(ee-1),C=new Float64Array(ee-1),v=new Int32Array(ee-1);function x(V,I){let S=f++,E=S*Oe,U=I-V,R=1/0,F=1/0,O=1/0,z=-1/0,L=-1/0,_=-1/0;for(let B=V;B<I;B++){let Y=e[n[B]];Y.minX<R&&(R=Y.minX),Y.maxX>z&&(z=Y.maxX),Y.minY<F&&(F=Y.minY),Y.maxY>L&&(L=Y.maxY),Y.minZ<O&&(O=Y.minZ),Y.maxZ>_&&(_=Y.maxZ)}if(c[E]=R,c[E+1]=F,c[E+2]=O,c[E+3]=z,c[E+4]=L,c[E+5]=_,U<=yr)return c[E+6]=1,c[E+7]=V,c[E+8]=I,S;let T=1/0,P=1/0,d=1/0,A=-1/0,w=-1/0,N=-1/0;for(let B=V;B<I;B++)o[B]<T&&(T=o[B]),o[B]>A&&(A=o[B]),i[B]<P&&(P=i[B]),i[B]>w&&(w=i[B]),s[B]<d&&(d=s[B]),s[B]>N&&(N=s[B]);let k=A-T,G=w-P,$=N-d;if(k===0&&G===0&&$===0)return c[E+6]=1,c[E+7]=V,c[E+8]=I,S;let X=ft(R,F,O,z,L,_),H=X>0?1/X:0,Z=U+1,K=0,q=0;for(let B=0;B<3;B++){let Y=B===0?T:B===1?P:d,re=B===0?k:B===1?G:$;if(re===0)continue;let ae=B===0?o:B===1?i:s,ue=ee/re;l.fill(1/0),u.fill(1/0),g.fill(1/0),m.fill(-1/0),p.fill(-1/0),h.fill(-1/0),b.fill(0);for(let D=V;D<I;D++){let j=(ae[D]-Y)*ue|0;j>=ee&&(j=ee-1);let oe=e[n[D]];oe.minX<l[j]&&(l[j]=oe.minX),oe.maxX>m[j]&&(m[j]=oe.maxX),oe.minY<u[j]&&(u[j]=oe.minY),oe.maxY>p[j]&&(p[j]=oe.maxY),oe.minZ<g[j]&&(g[j]=oe.minZ),oe.maxZ>h[j]&&(h[j]=oe.maxZ),b[j]++}let he=1/0,be=1/0,De=1/0,je=-1/0,Xe=-1/0,Ye=-1/0,Mt=0;for(let D=0;D<ee-1;D++)l[D]<he&&(he=l[D]),m[D]>je&&(je=m[D]),u[D]<be&&(be=u[D]),p[D]>Xe&&(Xe=p[D]),g[D]<De&&(De=g[D]),h[D]>Ye&&(Ye=h[D]),Mt+=b[D],y[D]=ft(he,be,De,je,Xe,Ye),M[D]=Mt;let He=1/0,We=1/0,Ze=1/0,Ke=-1/0,Je=-1/0,Qe=-1/0,wt=0;for(let D=ee-2;D>=0;D--){let j=D+1;l[j]<He&&(He=l[j]),m[j]>Ke&&(Ke=m[j]),u[j]<We&&(We=u[j]),p[j]>Je&&(Je=p[j]),g[j]<Ze&&(Ze=g[j]),h[j]>Qe&&(Qe=h[j]),wt+=b[j],C[D]=ft(He,We,Ze,Ke,Je,Qe),v[D]=wt}for(let D=0;D<ee-1;D++){if(M[D]===0||v[D]===0)continue;let j=.125+(y[D]*M[D]+C[D]*v[D])*H;j<Z&&(Z=j,K=B,q=Y+(D+1)/ue)}}let ne=K===0?o:K===1?i:s,J=V,Q=I-1;for(;J<=Q;)if(ne[J]<q)J++;else{let B=n[J];n[J]=n[Q],n[Q]=B;let Y=o[J];o[J]=o[Q],o[Q]=Y;let re=i[J];i[J]=i[Q],i[Q]=re;let ae=s[J];s[J]=s[Q],s[Q]=ae,Q--}let ie=J;(ie===V||ie===I)&&(ie=V+I>>1),c[E+6]=0;let te=x(V,ie),W=x(ie,I);return c[S*Oe+7]=te,c[S*Oe+8]=W,S}return r>0&&x(0,r),{data:c,nodeCount:f,polyIndices:n,meta:e}}function Wt(e,t,r,n,o,i,s,a,c){if(a.nodeCount===0)return!1;let{data:f,polyIndices:l,meta:u}=a,g=n!==0?1/n:n>=0?1/0:-1/0,m=o!==0?1/o:o>=0?1/0:-1/0,p=i!==0?1/i:i>=0?1/0:-1/0,h=0;for(c[h++]=0;h>0;){let y=c[--h]*Oe,M=(f[y]-e)*g,C=(f[y+3]-e)*g,v=M<C?M:C,x=M<C?C:M,V=(f[y+1]-t)*m,I=(f[y+4]-t)*m,S=V<I?V:I,E=V<I?I:V;if(v>E||S>x)continue;S>v&&(v=S),E<x&&(x=E);let U=(f[y+2]-r)*p,R=(f[y+5]-r)*p,F=U<R?U:R,O=U<R?R:U;if(!(v>O||F>x)&&(O<x&&(x=O),!(x<.001)))if(f[y+6]===1){let z=f[y+7]|0,L=f[y+8]|0;for(let _=z;_<L;_++){let T=l[_];if(T===s)continue;let P=u[T];if(P&&br(e,t,r,n,o,i,P))return!0}}else c[h++]=f[y+7]|0,c[h++]=f[y+8]|0}return!1}function Ar(e){let t=(1+Math.sqrt(5))/2,r=new Float64Array(e*3);for(let n=0;n<e;n++){let o=(n+.5)/e,i=Math.sqrt(Math.max(0,1-o*o)),s=2*Math.PI*(n/t);r[n*3]=i*Math.cos(s),r[n*3+1]=i*Math.sin(s),r[n*3+2]=o}return r}function Vr(e){let t=Math.abs(e[0])>.9?0:1,r=Math.abs(e[0])>.9?1:0,n=r*e[2],o=-t*e[2],i=t*e[1]-r*e[0],s=Math.hypot(n,o,i);n/=s,o/=s,i/=s;let a=e[1]*i-e[2]*o,c=e[2]*n-e[0]*i,f=e[0]*o-e[1]*n;return{ux:n,uy:o,uz:i,vx:a,vy:c,vz:f}}function lt(e,t){let r=t?.samples??8;if(e.length<4||r<1)return e;let n=e.map(xr),o=Ar(r),i=[],s=Pr(n),a=new Int32Array(Math.max(64,s.nodeCount)),c=192,f=new Float64Array(c);for(let l=0;l<e.length;l++){let u=n[l];if(!u){i.push(e[l]);continue}let g=u.normal[0],m=u.normal[1],p=u.normal[2],h=.001*g,b=.001*m,y=.001*p;{let L=u.centroid[0]+h,_=u.centroid[1]+b,T=u.centroid[2]+y;if(!Wt(L,_,T,g,m,p,l,s,a)){i.push(e[l]);continue}}let{ux:M,uy:C,uz:v,vx:x,vy:V,vz:I}=Vr(u.normal),S=u.centroid[0],E=u.centroid[1],U=u.centroid[2],R=u.vertices,F=R.length,O=0;f[O++]=S+h,f[O++]=E+b,f[O++]=U+y;for(let L=0;L<F;L++){let _=R[L];f[O++]=_[0]+(S-_[0])*.08+h,f[O++]=_[1]+(E-_[1])*.08+b,f[O++]=_[2]+(U-_[2])*.08+y}for(let L=0;L<F;L++){let _=R[L],T=R[(L+1)%F],P=(_[0]+T[0])*.5,d=(_[1]+T[1])*.5,A=(_[2]+T[2])*.5;f[O++]=P+(S-P)*.08+h,f[O++]=d+(E-d)*.08+b,f[O++]=A+(U-A)*.08+y}let z=!1;e:for(let L=0;L<o.length;L+=3){let _=o[L],T=o[L+1],P=o[L+2],d=_*M+T*x+P*g,A=_*C+T*V+P*m,w=_*v+T*I+P*p;for(let N=0;N<O;N+=3){let k=f[N],G=f[N+1],$=f[N+2];if(!Wt(k,G,$,d,A,w,l,s,a)){z=!0;break e}}}z&&i.push(e[l])}return i}function ut(e,t,r,n,o){let i=(p,h,b)=>{let y=[0,0,0];return y[e]=p,y[(e+1)%3]=h,y[(e+2)%3]=b,y},s=i(t,-n,-n),a=i(t,n,-n),c=i(t,n,n),f=i(t,-n,n),l=i(r,-n,-n),u=i(r,n,-n),g=i(r,n,n),m=i(r,-n,n);return[{vertices:[s,a,c,f],color:o},{vertices:[l,u,g,m],color:o},{vertices:[s,a,u,l],color:o},{vertices:[a,c,g,u],color:o},{vertices:[c,f,m,g],color:o},{vertices:[f,s,l,m],color:o}]}function Zt(e={}){let t=e.size??5,r=e.thickness??.025,n=e.negative??!1,o=e.xColor??"#ff3a3a",i=e.yColor??"#3aff3a",s=e.zColor??"#3a8aff",a=t*r/2,c=n?-t:0;return[...ut(0,c,t,a,o),...ut(1,c,t,a,i),...ut(2,c,t,a,s)]}function Kt(e,t,r,n){let o=[0,0,0];return o[e]=t,o[(e+1)%3]=r,o[(e+2)%3]=n,o}function vr(e,t,r,n,o){let i=(p,h,b)=>Kt(e,p,h,b),s=i(t,-n,-n),a=i(t,n,-n),c=i(t,n,n),f=i(t,-n,n),l=i(r,-n,-n),u=i(r,n,-n),g=i(r,n,n),m=i(r,-n,n);return[{vertices:[s,a,c,f],color:o},{vertices:[l,u,g,m],color:o},{vertices:[s,a,u,l],color:o},{vertices:[a,c,g,u],color:o},{vertices:[c,f,m,g],color:o},{vertices:[f,s,l,m],color:o}]}function Mr(e,t,r,n,o){let i=(u,g,m)=>Kt(e,u,g,m),s=i(t,-n,-n),a=i(t,n,-n),c=i(t,n,n),f=i(t,-n,n),l=i(r,0,0);return[{vertices:[s,a,c,f],color:o},{vertices:[s,a,l],color:o},{vertices:[a,c,l],color:o},{vertices:[c,f,l],color:o},{vertices:[f,s,l],color:o}]}function wr(e){return e.map(t=>({...t,vertices:[...t.vertices].reverse()}))}function Jt(e){let t=e.axis,r=e.sign??1,n=e.shaftLength??4,o=e.shaftHalfThickness??.05,i=e.headLength??.8,s=e.headHalfThickness??.2,a=e.color??"#ffffff",c=n*r,f=Math.min(0,c),l=Math.max(0,c),u=c,g=(n+i)*r,m=Mr(t,u,g,s,a);return[...vr(t,f,l,o,a),...r===-1?wr(m):m]}function ze(e,t,r){let n=[0,0,0],o=(e+1)%3,i=(e+2)%3;return n[o]=Math.cos(r)*t,n[i]=Math.sin(r)*t,n}function Qt(e){let t=e.axis,r=e.radius,n=e.halfThickness??Math.max(.05,r*.04),o=e.segments??32,i=e.color??"#ffffff",s=r-n,a=r+n,c=[];for(let f=0;f<o;f++){let l=f/o*Math.PI*2,u=(f+1)/o*Math.PI*2,g=ze(t,s,l),m=ze(t,s,u),p=ze(t,a,l),h=ze(t,a,u);c.push({vertices:[g,p,h,m],color:i})}return c}function qt(e){let{center:t,size:r,color:n="#ffffff"}=e,[o,i,s]=t,a=[[o+r,i,s],[o-r,i,s],[o,i+r,s],[o,i-r,s],[o,i,s+r],[o,i,s-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(f=>({vertices:[a[f[0]],a[f[1]],a[f[2]]],color:n}))}var Sr=2200,Ir=2201,Cr=2202;function Tr(e,t){let r=0,n=1,o=1,i=2201,s=1/0,a=0,c=!1,f=null,l=!0,u=!1,g={clampWhenFinished:!1,get timeScale(){return o},set timeScale(m){o=m},get weight(){return n},set weight(m){n=m},get time(){return r},set time(m){r=m},get isRunning(){return c},get enabled(){return l},set enabled(m){l=m},get paused(){return u},set paused(m){u=m},play(){return c=!0,g},stop(){return c=!1,r=0,a=0,f=null,g},reset(){return r=0,a=0,g},fadeIn(m){return f={from:0,to:1,elapsed:0,duration:m},n=0,g},fadeOut(m){return f={from:n,to:0,elapsed:0,duration:m},g},crossFadeTo(m,p){return g.fadeOut(p),m.fadeIn(p),g},crossFadeFrom(m,p){return m.fadeOut(p),g.fadeIn(p),g},setLoop(m,p){return i=m,s=p,g},setEffectiveTimeScale(m){return o=m,g},setEffectiveWeight(m){return n=m,g}};return g._internal={get clip(){return e},get time(){return r},get weight(){return n},get enabled(){return l},get paused(){return u},get running(){return c},get loopMode(){return i},get repetitions(){return s},get completedReps(){return a},set completedReps(m){a=m},get clampWhenFinished(){return g.clampWhenFinished},get timeScale(){return o},get fade(){return f},advance(m){if(!c)return;if(f){f.elapsed+=m;let h=f.duration>0?Math.min(f.elapsed/f.duration,1):1;if(n=f.from+(f.to-f.from)*h,h>=1&&(n=f.to,f=null,n<=0)){c=!1;return}}if(u)return;let p=e.duration;if(!(p<=0)){if(r+=m*o,i===2200)r>=p&&(r=g.clampWhenFinished?p:0,c=!1);else if(i===2201){if(r>=p){let h=r%p,b=Math.floor(r/p);a+=b,r=h,s!==1/0&&a>=s&&(r=g.clampWhenFinished?p:0,c=!1)}}else if(p>0){let h=p*2,b=r%h;if(r>=h){let y=Math.floor(r/h);a+=y,s!==1/0&&a>=s?(r=g.clampWhenFinished?p:0,c=!1):r=b}}}},sampleTime(){if(i===2202){let m=e.duration,p=m*2,h=r%p;return h<=m?h:p-h}return r}},g}function Er(e){return e._internal}function mt(e,t){return typeof t=="number"?e[t]:e.find(r=>r.name===t)}function Or(e,t){let r=new Map;function n(f){let l=mt(t.clips,f);if(!l)throw new Error(`PolyAnimationMixer: no clip found for key "${f}". Available: ${t.clips.map(g=>g.name).join(", ")}`);let u=r.get(l.index);return u||(u=Tr(l,t),r.set(l.index,u)),u}function o(f){let l=mt(t.clips,f);return l?r.get(l.index)??null:null}function i(f){let l=[];for(let h of r.values()){let b=Er(h);b.advance(f),b.running&&b.enabled&&l.push({internal:b,clip:b.clip})}if(l.length===0)return;if(l.length===1){let{internal:h,clip:b}=l[0],y=t.sample(b.name,h.sampleTime());e.setPolygons(y);return}let u=0;for(let{internal:h}of l)u+=h.weight;if(u<=0)return;let g=l.map(({internal:h,clip:b})=>({polygons:t.sample(b.name,h.sampleTime()),weight:h.weight/u})),m=g[0].polygons;if(m.length===0)return;let p=m.map((h,b)=>{let y=h.vertices.map((M,C)=>{let v=0,x=0,V=0;for(let{polygons:I,weight:S}of g){let E=I[b];if(!E)continue;let U=E.vertices[C];U&&(v+=U[0]*S,x+=U[1]*S,V+=U[2]*S)}return[v,x,V]});return{...h,vertices:y}});e.setPolygons(p)}function s(){for(let f of r.values())f.stop()}function a(f){let l=mt(t.clips,f);l&&r.delete(l.index)}function c(){r.clear()}return{clipAction:n,existingAction:o,update:i,stopAllAction:s,uncacheClip:a,uncacheRoot:c}}var Rr=/^[0-9A-Fa-f]{6}$/,Lr=["#3b82f6","#ef4444","#22c55e","#eab308","#a855f7","#06b6d4","#f97316","#ec4899"];function gt(e,t){let r=t?.targetSize??60,n=t?.gridShift??1,o=t?.defaultColor??"#888888",i=t?.palette??Lr,s=t?.materialColors??{},a=t?.materialTextures??{},c=[],f=[],l=[],u=[],g=new Map,m=o,p,h=t?.includeObjects?new Set(t.includeObjects):null,b=t?.excludeObjects?new Set(t.excludeObjects):null,y=null,M=()=>y===null?h===null:!(h&&!h.has(y)||b&&b.has(y)),C=T=>T in s?s[T]:Rr.test(T)?`#${T}`:(g.has(T)||(g.set(T,i[u.length%i.length]),u.push(T)),g.get(T)),v=e.split(`
|
|
2
|
-
`);for(let
|
|
3
|
-
`)){let i=o.trim();if(!(i.length===0||i.charCodeAt(0)===35)){if(i.startsWith("newmtl ")){n=i.slice(7).trim();continue}if(n){if(i.startsWith("Kd ")){let s=i.split(/\s+/),a=parseFloat(s[1]),c=parseFloat(s[2]),f=parseFloat(s[3]);Number.isFinite(a)&&Number.isFinite(c)&&Number.isFinite(f)&&(t[n]=`#${dt(a)}${dt(c)}${dt(f)}`)}else if(i.startsWith("map_Kd ")){let s=i.split(/\s+/),a=s[s.length-1]?.replace(/\\+/g,"/");a&&a!=="map_Kd"&&(r[n]=a)}}}}return{colors:t,textures:r}}var on={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},sn={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16};function cn(e){let t=globalThis.TextDecoder;return new t().decode(e)}function Ur(e){let t=e.indexOf(",");if(t<0)throw new Error("parseGltf: malformed data: URI");let r=e.slice(5,t),n=e.slice(t+1);if(!r.includes(";base64")){let s=decodeURIComponent(n),a=new Uint8Array(s.length);for(let c=0;c<s.length;c++)a[c]=s.charCodeAt(c)&255;return a}let o=globalThis.atob(n),i=new Uint8Array(o.length);for(let s=0;s<o.length;s++)i[s]=o.charCodeAt(s);return i}function Nr(e,t){let r=e.buffers?.[0];if(!r)throw new Error("parseGltf: JSON doc has no buffers[0]");let n=r.uri;if(!n)throw new Error("parseGltf: JSON doc buffer has no uri (and there's no GLB BIN chunk)");if(n.startsWith("data:"))return Ur(n);if(t){let o=t(n);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 "${n}" \u2014 provide options.resolveBuffer`)}function _r(e){let t=new DataView(e);if(t.getUint32(0,!0)!==1179937895)throw new Error("parseGltf: not a GLB (bad magic)");let r=t.getUint32(4,!0);if(r!==2)throw new Error(`parseGltf: only glTF v2 supported (got v${r})`);let n=12,o=null,i=null;for(;n<e.byteLength;){let s=t.getUint32(n,!0),a=t.getUint32(n+4,!0),c=n+8;if(a===1313821514){let f=new Uint8Array(e,c,s);o=JSON.parse(cn(f))}else a===5130562&&(i=new Uint8Array(e,c,s));n=c+s}if(!o)throw new Error("parseGltf: no JSON chunk in GLB");return{doc:o,bin:i}}function xt(e,t,r){let n=e.accessors?.[r],o=e.bufferViews?.[n?.bufferView??-1];if(!n||!o)throw new Error(`parseGltf: bad accessor/bufferView ${r}`);let i=on[n.componentType],s=sn[n.type];if(!i||!s)throw new Error(`parseGltf: unsupported accessor type ${n.type}/${n.componentType}`);let a=(o.byteOffset??0)+(n.byteOffset??0),c=n.count*s,f=t.buffer.slice(t.byteOffset+a,t.byteOffset+a+c*i),l;switch(n.componentType){case 5126:l=new Float32Array(f);break;case 5123:l=new Uint16Array(f);break;case 5125:l=new Uint32Array(f);break;case 5121:l=new Uint8Array(f);break;default:throw new Error(`parseGltf: unhandled componentType ${n.componentType}`)}return{array:l,count:n.count,componentCount:s}}function $r(e,t,r){switch(r){case 5120:return e.getInt8(t);case 5121:return e.getUint8(t);case 5122:return e.getInt16(t,!0);case 5123:return e.getUint16(t,!0);case 5125:return e.getUint32(t,!0);case 5126:return e.getFloat32(t,!0);default:throw new Error(`parseGltf: unhandled componentType ${r}`)}}function zr(e,t){switch(t){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 Be(e,t,r){let n=e.accessors?.[r],o=e.bufferViews?.[n?.bufferView??-1];if(!n||!o)throw new Error(`parseGltf: bad accessor/bufferView ${r}`);let i=on[n.componentType],s=sn[n.type];if(!i||!s)throw new Error(`parseGltf: unsupported accessor type ${n.type}/${n.componentType}`);let a=t.byteOffset+(o.byteOffset??0)+(n.byteOffset??0),c=o.byteStride??i*s,f=new DataView(t.buffer),l=new Array(n.count*s),u=0;for(let g=0;g<n.count;g++){let m=a+g*c;for(let p=0;p<s;p++){let h=$r(f,m+p*i,n.componentType);l[u++]=n.normalized?zr(h,n.componentType):h}}return{values:l,count:n.count,componentCount:s}}function Fr(e,t,r){let n=[],o=[],i=globalThis;for(let s of e.images??[]){if(s.uri){if(r&&!s.uri.startsWith("data:"))try{n.push(new i.URL(s.uri,r).href)}catch{n.push(s.uri)}else n.push(s.uri);continue}if(s.bufferView!==void 0){let a=e.bufferViews?.[s.bufferView];if(!a){n.push("");continue}let c=a.byteOffset??0,f=t.subarray(c,c+a.byteLength),l=s.mimeType??"image/png",u=new i.Blob([f],{type:l}),g=i.URL.createObjectURL(u);n.push(g),o.push(g)}else n.push("")}return{urls:n,objectUrls:o}}function Gr(e,t){let r=new Map,n=e.materials??[];for(let o=0;o<n.length;o++){let i=n[o].pbrMetallicRoughness?.baseColorTexture?.index;if(i===void 0)continue;let s=e.textures?.[i]?.source;if(s===void 0)continue;let a=t[s];a&&r.set(o,a)}return r}function Br(e,t){let r=e?.pbrMetallicRoughness?.baseColorFactor;if(!r||r.length<3)return t;let n=o=>Math.round(Math.max(0,Math.min(1,o))*255).toString(16).padStart(2,"0");return`#${n(r[0])}${n(r[1])}${n(r[2])}`}var ve=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function ht(e,t){let r=new Array(16);for(let n=0;n<4;n++)for(let o=0;o<4;o++)r[o*4+n]=e[0+n]*t[o*4+0]+e[4+n]*t[o*4+1]+e[8+n]*t[o*4+2]+e[12+n]*t[o*4+3];return r}function Ge(e,t){return[e[0]*t[0]+e[4]*t[1]+e[8]*t[2]+e[12],e[1]*t[0]+e[5]*t[1]+e[9]*t[2]+e[13],e[2]*t[0]+e[6]*t[1]+e[10]*t[2]+e[14]]}function an(e,t,r){let n=e?.[0]??0,o=e?.[1]??0,i=e?.[2]??0,s=t?.[0]??0,a=t?.[1]??0,c=t?.[2]??0,f=t?.[3]??1,l=r?.[0]??1,u=r?.[1]??1,g=r?.[2]??1,m=s+s,p=a+a,h=c+c,b=s*m,y=s*p,M=s*h,C=a*p,v=a*h,x=c*h,V=f*m,I=f*p,S=f*h;return[(1-(C+x))*l,(y+S)*l,(M-I)*l,0,(y-S)*u,(1-(b+x))*u,(v+V)*u,0,(M+I)*g,(v-V)*g,(1-(b+C))*g,0,n,o,i,1]}function Dr(e){return e.matrix&&e.matrix.length===16?e.matrix.slice():an(e.translation,e.rotation,e.scale)}function jr(e,t){return[e[0]+t[0],e[1]+t[1],e[2]+t[2]]}function en(e,t){return[e[0]*t,e[1]*t,e[2]*t]}function fn(e,t,r){let n=new Array(Math.min(e.length,t.length));for(let o=0;o<n.length;o++)n[o]=e[o]+(t[o]-e[o])*r;return n}function Re(e){let t=Math.hypot(e[0]??0,e[1]??0,e[2]??0,e[3]??1)||1;return[(e[0]??0)/t,(e[1]??0)/t,(e[2]??0)/t,(e[3]??1)/t]}function Xr(e,t,r){let n=Re(e),o=Re(t),i=n[0]*o[0]+n[1]*o[1]+n[2]*o[2]+n[3]*o[3];if(i<0&&(i=-i,o=[-o[0],-o[1],-o[2],-o[3]]),i>.9995)return Re(fn(n,o,r));let s=Math.acos(Math.max(-1,Math.min(1,i))),a=Math.sin(s),c=Math.sin((1-r)*s)/a,f=Math.sin(r*s)/a;return Re([n[0]*c+o[0]*f,n[1]*c+o[1]*f,n[2]*c+o[2]*f,n[3]*c+o[3]*f])}function Yr(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 tn(e){return e.matrix?e.matrix.slice():an(e.translation,e.rotation,e.scale)}function Hr(e){let t=e.scene??0,r=e.scenes?.[t]?.nodes;return r&&r.length>0?r:[]}function nn(e,t){let r=e.nodes??[],n=new Array(r.length),o=new Set,i=(a,c)=>{if(a<0||a>=r.length)return;let f=ht(c,t[a]??ve);n[a]=f,o.add(a);for(let l of r[a].children??[])i(l,f)},s=Hr(e);if(s.length>0)for(let a of s)i(a,ve);for(let a=0;a<r.length;a++)o.has(a)||i(a,ve);return n}function rn(e,t,r,n,o){if(r===void 0)return;let{values:i,count:s,componentCount:a}=Be(e,t,r);if(s!==o||a<1)return;let c=[];for(let f=0;f<s;f++){let l=[];for(let u=0;u<n;u++)l.push(i[f*a+u]??0);c.push(l)}return c}function Wr(e,t,r,n){if(r===void 0)return Array.from({length:n},()=>ve.slice());let{values:o,componentCount:i,count:s}=Be(e,t,r);if(i!==16)throw new Error(`parseGltf: inverseBindMatrices accessor ${r} is not MAT4`);let a=[];for(let c=0;c<n;c++){let f=Math.min(c,s-1);a.push(o.slice(f*16,f*16+16))}return a}function Fe(e,t){let r=e.componentCount,n=e.interpolation==="CUBICSPLINE"?(t*3+1)*r:t*r;return e.output.slice(n,n+r)}function Zr(e,t,r){let n=e.input;if(n.length===0)return[];if(n.length===1||t<=n[0])return Fe(e,0);let o=n.length-1;if(t>=n[o])return Fe(e,o);let i=0,s=o;for(;i+1<s;){let g=i+s>>1;n[g]<=t?i=g:s=g}let a=n[i],c=n[i+1],f=c>a?(t-a)/(c-a):0,l=Fe(e,i),u=Fe(e,i+1);return e.interpolation==="STEP"?l:r==="rotation"?Xr(l,u,f):fn(l,u,f)}function Kr(e,t,r,n){let o=e.animations??[];if(o.length===0||r.length===0)return;let i=(e.nodes??[]).map(m=>Yr(m)),s=i.map(tn),a=nn(e,s),c=(e.skins??[]).map(m=>({joints:m.joints??[],inverseBindMatrices:Wr(e,t,m.inverseBindMatrices,m.joints?.length??0)})),f=[];for(let m=0;m<o.length;m++){let p=o[m],h=(p.samplers??[]).map(M=>{let C=Be(e,t,M.input),v=Be(e,t,M.output);return{input:C.values,output:v.values,componentCount:v.componentCount,interpolation:M.interpolation??"LINEAR"}}),b=[];for(let M of p.channels??[]){let C=M.target.node,v=M.target.path,x=h[M.sampler];C===void 0||!v||!x||v==="weights"||b.push({sampler:x,targetNode:C,path:v})}let y=b.reduce((M,C)=>{let v=C.sampler.input;return Math.max(M,v[v.length-1]??0)},0);f.push({info:{index:m,name:p.name??`animation_${m}`,duration:y,channelCount:b.length},channels:b})}let l=f.map(m=>m.info);if(l.length===0)return;let u=(m,p,h,b,y,M)=>{let C=n(m),v=n(p),x=n(h);if(C[0]===v[0]&&C[1]===v[1]&&C[2]===v[2]||C[0]===x[0]&&C[1]===x[1]&&C[2]===x[2]||v[0]===x[0]&&v[1]===x[1]&&v[2]===x[2])return null;let V={vertices:[C,v,x],color:b};return y&&(V.texture=y),M&&(V.uvs=M),V};return{clips:l,sample:(m,p)=>{let h=typeof m=="number"?f[m]:f.find(x=>x.info.name===m);if(!h)return[];let b=h.info.duration,y=b>0?(p%b+b)%b:Math.max(0,p),M=i.map(x=>({translation:x.translation.slice(),rotation:x.rotation.slice(),scale:x.scale.slice(),matrix:x.matrix?x.matrix.slice():void 0}));for(let x of h.channels){let V=M[x.targetNode];if(!V)continue;let I=Zr(x.sampler,y,x.path);V.matrix=void 0,x.path==="translation"?V.translation=I.slice(0,3):x.path==="rotation"?V.rotation=Re(I.slice(0,4)):x.path==="scale"&&(V.scale=I.slice(0,3))}let C=nn(e,M.map(tn)),v=[];for(let x of r){let V=[];if(x.skinIndex!==void 0&&x.joints&&x.weights&&c[x.skinIndex]){let I=c[x.skinIndex];for(let S=0;S<x.positions.length;S++){let E=x.positions[S],U=[0,0,0],R=0,F=x.joints[S]??[],O=x.weights[S]??[];for(let z=0;z<4;z++){let L=O[z]??0;if(L<=0)continue;let _=Math.round(F[z]??0),T=I.joints[_],P=C[T],d=I.inverseBindMatrices[_];if(!P||!d)continue;let A=ht(P,d);U=jr(U,en(Ge(A,E),L)),R+=L}V.push(R>0?en(U,1/R):Ge(x.meshBindWorld,E))}}else{let I=x.meshNode!==null?C[x.meshNode]??x.meshBindWorld:x.meshBindWorld;for(let S of x.positions)V.push(Ge(I,S))}for(let I=0;I+2<x.indices.length;I+=3){let S=x.indices[I],E=x.indices[I+1],U=x.indices[I+2],R=V[S],F=V[E],O=V[U];if(!R||!F||!O)continue;let z;if(x.uvs&&x.texture){let _=x.uvs[S],T=x.uvs[E],P=x.uvs[U];_&&T&&P&&(z=[_,T,P])}let L=u(R,F,O,x.color,x.texture,z);L&&v.push(L)}}return v}}}function bt(e,t){let r=t?.targetSize??60,n=t?.gridShift??1,o=t?.defaultColor??"#888888",i=t?.materialColors??{},s=e instanceof Uint8Array?e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength):e,a=s.byteLength,c,f;if(s.byteLength>=4&&new DataView(s).getUint32(0,!0)===1179937895){let d=_r(s);if(c=d.doc,!d.bin)throw new Error("parseGltf: GLB has no binary chunk");f=d.bin}else c=JSON.parse(cn(new Uint8Array(s))),f=Nr(c,t?.resolveBuffer);let{urls:l,objectUrls:u}=Fr(c,f,t?.baseUrl),g=Gr(c,l),m=[],p=[],h=(c.meshes??[]).map((d,A)=>d.name??`mesh_${A}`),b=(c.materials??[]).map((d,A)=>d.name??`material_${A}`);function y(d,A,w){let N=c.meshes?.[d];if(N)for(let k of N.primitives){if((k.mode??4)!==4)continue;let $=k.material!==void 0?c.materials?.[k.material]?.name:void 0,H=($?i[$]:void 0)??Br(k.material!==void 0?c.materials?.[k.material]:void 0,o),Z=k.material!==void 0?g.get(k.material):void 0,{array:K,count:q}=xt(c,f,k.attributes.POSITION);if(!(K instanceof Float32Array))continue;let ne=[],J=[];for(let W=0;W<q;W++){let B=[K[W*3],K[W*3+1],K[W*3+2]];ne.push(B),J.push(Ge(A,B))}let Q=null,ie=k.attributes.TEXCOORD_0;if(Z&&ie!==void 0){let{array:W,count:B}=xt(c,f,ie);Q=[];let Y=1;W instanceof Uint8Array?Y=1/255:W instanceof Uint16Array&&(Y=1/65535);for(let re=0;re<B;re++){let ae=W[re*2]*Y,ue=W[re*2+1]*Y;Q.push([ae,1-ue])}}let te;if(k.indices!==void 0){let{array:W,count:B}=xt(c,f,k.indices);te=[];for(let Y=0;Y<B;Y++)te.push(Number(W[Y]))}else te=J.map((W,B)=>B);if((c.animations?.length??0)>0){let W=rn(c,f,k.attributes.JOINTS_0,4,q),B=rn(c,f,k.attributes.WEIGHTS_0,4,q);p.push({meshNode:w,meshBindWorld:A,skinIndex:w!==null?c.nodes?.[w]?.skin:void 0,positions:ne,indices:te,color:H,texture:Z,uvs:Q??void 0,joints:W,weights:B})}for(let W=0;W+2<te.length;W+=3){let B=J[te[W]],Y=J[te[W+1]],re=J[te[W+2]];if(!B||!Y||!re)continue;let ae;if(Q&&Z){let ue=Q[te[W]],he=Q[te[W+1]],be=Q[te[W+2]];ue&&he&&be&&(ae=[ue,he,be])}m.push({v0:B,v1:Y,v2:re,color:H,texture:Z,uvs:ae})}}}function M(d,A){let w=c.nodes?.[d];if(!w)return;let N=ht(A,Dr(w));typeof w.mesh=="number"&&y(w.mesh,N,d);for(let k of w.children??[])M(k,N)}let C=c.scene??0,v=c.scenes?.[C]?.nodes;if(v&&v.length>0)for(let d of v)M(d,ve);else for(let d=0;d<(c.meshes?.length??0);d++)y(d,ve,null);let x=Jr(u);if(m.length===0)return{polygons:[],objectUrls:u,dispose:x,warnings:[],metadata:{triangleCount:0,meshes:h,materials:b,sourceBytes:a}};let V=1/0,I=1/0,S=1/0,E=-1/0,U=-1/0,R=-1/0;for(let d of m)for(let A of[d.v0,d.v1,d.v2])A[0]<V&&(V=A[0]),A[0]>E&&(E=A[0]),A[1]<I&&(I=A[1]),A[1]>U&&(U=A[1]),A[2]<S&&(S=A[2]),A[2]>R&&(R=A[2]);let F=Math.max(E-V,U-I,R-S),O=F>0?r/F:1,z=d=>Math.round(d*1e3)/1e3,_=(t?.upAxis??"y")==="z"?([d,A,w])=>[z((d-V)*O+n),z((A-I)*O+n),z((w-S)*O+n)]:([d,A,w])=>[z((w-S)*O+n),z((d-V)*O+n),z((A-I)*O+n)],T=Kr(c,f,p,_),P=[];for(let d of m){let A=_(d.v0),w=_(d.v1),N=_(d.v2);if(A[0]===w[0]&&A[1]===w[1]&&A[2]===w[2]||A[0]===N[0]&&A[1]===N[1]&&A[2]===N[2]||w[0]===N[0]&&w[1]===N[1]&&w[2]===N[2])continue;let k={vertices:[A,w,N],color:d.color};d.texture&&(k.texture=d.texture),d.uvs&&(k.uvs=d.uvs),P.push(k)}return{polygons:P,animation:T,objectUrls:u,dispose:x,warnings:[],metadata:{triangleCount:P.length,meshes:h,materials:b,animations:T?.clips,sourceBytes:a}}}function Jr(e){let t=!1;return()=>{if(t)return;t=!0;let r=globalThis.URL;if(r?.revokeObjectURL)for(let n of e)try{r.revokeObjectURL(n)}catch{}}}function yt(e){return e.material?.texture??e.texture}function Qr(){let e=globalThis;return typeof e.Image!="function"||typeof e.document?.createElement!="function"?null:{Image:e.Image,createCanvas(){return e.document.createElement("canvas")}}}function qr(e,t){return new Promise((r,n)=>{let o=new t,i=!1,s=a=>{i||(i=!0,a())};o.decoding="async",o.onload=()=>s(()=>r(o)),o.onerror=()=>s(()=>n(new Error(`texture load failed: ${e}`))),o.src=e,typeof o.decode=="function"&&o.decode().then(()=>s(()=>r(o)),()=>{})})}async function eo(e,t,r){try{let n=await qr(e,t.Image),o=Math.max(0,Math.floor(n.naturalWidth||n.width||0)),i=Math.max(0,Math.floor(n.naturalHeight||n.height||0));if(o<=0||i<=0||o*i>r)return null;let s=t.createCanvas();s.width=o,s.height=i;let a=s.getContext("2d",{willReadFrequently:!0});return a?(a.drawImage(n,0,0,o,i),{width:o,height:i,data:a.getImageData(0,0,o,i).data}):null}catch{return null}}function ln(e,t,r){return Math.max(t,Math.min(r,e))}function to(e,t){let r=t[0],n=1-t[1];if(!Number.isFinite(r)||!Number.isFinite(n))return null;let o=ln(Math.floor(r*e.width),0,e.width-1),s=(ln(Math.floor(n*e.height),0,e.height-1)*e.width+o)*4;return{r:e.data[s]??0,g:e.data[s+1]??0,b:e.data[s+2]??0,a:e.data[s+3]??255}}function pe(e,t,r,n,o,i){return[e[0]*n+t[0]*o+r[0]*i,e[1]*n+t[1]*o+r[1]*i]}function no(e){let[t,r,n]=e;return[pe(t,r,n,1/3,1/3,1/3),pe(t,r,n,.8,.1,.1),pe(t,r,n,.1,.8,.1),pe(t,r,n,.1,.1,.8),pe(t,r,n,.45,.45,.1),pe(t,r,n,.45,.1,.45),pe(t,r,n,.1,.45,.45)]}function un(e){if(e.textureTriangles?.length)return e.textureTriangles;let t=e.uvs;if(!t||t.length!==e.vertices.length||t.length<3)return[];let r=[];for(let n=1;n+1<t.length;n++)r.push({uvs:[t[0],t[n],t[n+1]]});return r}function ro(e,t,r){return Math.abs(e.r-t.r)<=r&&Math.abs(e.g-t.g)<=r&&Math.abs(e.b-t.b)<=r&&Math.abs(e.a-t.a)<=r}function oo(e){let t=n=>Math.round(Math.max(0,Math.min(255,n))).toString(16).padStart(2,"0");if(e.a>=255)return`#${t(e.r)}${t(e.g)}${t(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 io(e,t,r){let n=un(e);if(n.length===0)return null;let o=null;for(let i of n)for(let s of no(i.uvs)){let a=to(t,s);if(!a)return null;if(!o)o=a;else if(!ro(o,a,r))return null}return o?oo(o):null}function so(e,t){let{texture:r,material:n,uvs:o,textureTriangles:i,...s}=e;return{...s,color:t}}async function mn(e,t={}){if(t.enabled===!1)return null;let r=Qr();if(!r)return null;let n=e.filter(a=>yt(a)&&un(a).length>0);if(n.length===0)return null;let o=t.maxTexturePixels??16777216,i=new Map;await Promise.all(Array.from(new Set(n.map(a=>yt(a)))).map(async a=>{i.set(a,await eo(a,r,o))}));let s=t.colorTolerance??2;return{bake(a){let c=!1,f=a.map(l=>{let u=yt(l);if(!u)return l;let g=i.get(u);if(!g)return l;let m=io(l,g,s);return m?(c=!0,so(l,m)):l});return{polygons:c?f:a,changed:c}}}}async function co(e,t={}){let r=await mn(e,t);return r?r.bake(e).polygons:e}async function Pt(e,t={}){let r=await mn(e.polygons,t);if(!r)return e;let n=r.bake(e.polygons);return!n.changed&&!e.animation?e:{...e,polygons:n.polygons,animation:e.animation?{...e.animation,sample(o,i){return r.bake(e.animation.sample(o,i)).polygons}}:e.animation}}var ao=[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 fo(e){let t=e>>0&255,r=e>>8&255,n=e>>16&255;return[t,r,n]}function At(e){return(e&255).toString(16).padStart(2,"0")}function gn(e,t,r){return`#${At(e)}${At(t)}${At(r)}`}var lo=542658390;function Vt(e,t){let r=t?.targetSize??60,n=t?.gridShift??0,o=e.byteLength;if(e.byteLength<8)return Me(o,["parseVox: buffer too small to be a valid .vox file"]);let i=new DataView(e);if(i.getUint32(0,!0)!==lo)return Me(o,["parseVox: not a .vox file (bad magic)"]);if(e.byteLength<20)return Me(o,["parseVox: buffer too small for MAIN chunk"]);if(dn(i,8)!=="MAIN")return Me(o,["parseVox: expected MAIN chunk at offset 8"]);let f=20+i.getUint32(12,!0),l=f+i.getUint32(16,!0),u=[],g=[],m=null;for(;f<l&&f+12<=e.byteLength;){let P=dn(i,f),d=i.getUint32(f+4,!0),A=i.getUint32(f+8,!0),w=f+12,N=w+d+A;if(P==="SIZE"){if(d>=12&&w+12<=e.byteLength){let k=i.getUint32(w,!0),G=i.getUint32(w+4,!0),$=i.getUint32(w+8,!0);u.push({sx:k,sy:G,sz:$})}}else if(P==="XYZI"){if(d>=4&&w+4<=e.byteLength){let k=i.getUint32(w,!0),G=[],$=Math.min(k,Math.floor((d-4)/4));for(let X=0;X<$;X++){let H=w+4+X*4;G.push({x:i.getUint8(H),y:i.getUint8(H+1),z:i.getUint8(H+2),colorIndex:i.getUint8(H+3)})}g.push(G)}}else if(P==="RGBA"&&d>=1024&&w+1024<=e.byteLength){m=[];for(let k=0;k<256;k++){let G=w+k*4,$=i.getUint8(G),X=i.getUint8(G+1),H=i.getUint8(G+2);m.push(gn($,X,H))}}f=N}let p=[],h=new Set;for(let P of g)for(let d of P){if(d.colorIndex===0)continue;let A=`${d.x},${d.y},${d.z}`;h.has(A)||(h.add(A),p.push(d))}if(p.length===0)return Me(o,[]);let b=P=>{let d=P-1;if(m!==null)return m[d]??"#888888";let A=ao[P]??0,[w,N,k]=fo(A);return gn(w,N,k)},y=(P,d,A)=>h.has(`${P},${d},${A}`),M=new Map,C=(P,d,A,w,N)=>{let k=`${P}:${d}:${N}`,G=M.get(k);G||(G=new Set,M.set(k,G)),G.add(`${A},${w}`)};for(let P of p){let{x:d,y:A,z:w}=P,N=b(P.colorIndex);y(d+1,A,w)||C(0,d+1,A,w,N),y(d-1,A,w)||C(1,d,A,w,N),y(d,A+1,w)||C(2,A+1,d,w,N),y(d,A-1,w)||C(3,A,d,w,N),y(d,A,w+1)||C(4,w+1,d,A,N),y(d,A,w-1)||C(5,w,d,A,N)}function v(P){let d=new Set,A=[],w=1/0,N=-1/0,k=1/0,G=-1/0;for(let $ of P){let[X,H]=$.split(","),Z=+X,K=+H;Z<w&&(w=Z),Z>N&&(N=Z),K<k&&(k=K),K>G&&(G=K)}for(let $=k;$<=G;$++)for(let X=w;X<=N;X++){let H=`${X},${$}`;if(!P.has(H)||d.has(H))continue;let Z=1;for(;X+Z<=N;){let q=`${X+Z},${$}`;if(!P.has(q)||d.has(q))break;Z++}let K=1;e:for(;$+K<=G;){for(let q=0;q<Z;q++){let ne=`${X+q},${$+K}`;if(!P.has(ne)||d.has(ne))break e}K++}for(let q=0;q<K;q++)for(let ne=0;ne<Z;ne++)d.add(`${X+ne},${$+q}`);A.push({u:X,v:$,w:Z,h:K})}return A}let x=(P,d,A,w,N,k)=>{let G=A+N,$=w+k;switch(P){case 0:return[[d,A,w],[d,G,w],[d,G,$],[d,A,$]];case 1:return[[d,G,w],[d,A,w],[d,A,$],[d,G,$]];case 2:return[[A,d,w],[A,d,$],[G,d,$],[G,d,w]];case 3:return[[G,d,w],[G,d,$],[A,d,$],[A,d,w]];case 4:return[[A,w,d],[G,w,d],[G,$,d],[A,$,d]];default:return[[A,$,d],[G,$,d],[G,w,d],[A,w,d]]}},V=[];for(let[P,d]of M){let A=P.indexOf(":"),w=P.indexOf(":",A+1),N=+P.slice(0,A),k=+P.slice(A+1,w),G=P.slice(w+1);for(let{u:$,v:X,w:H,h:Z}of v(d))V.push({vertices:x(N,k,$,X,H,Z),color:G})}if(V.length===0)return Me(o,[]);let I=1/0,S=1/0,E=1/0,U=-1/0,R=-1/0,F=-1/0;for(let P of V)for(let d of P.vertices)d[0]<I&&(I=d[0]),d[0]>U&&(U=d[0]),d[1]<S&&(S=d[1]),d[1]>R&&(R=d[1]),d[2]<E&&(E=d[2]),d[2]>F&&(F=d[2]);let O=Math.max(U-I,R-S,F-E),z=O>0?r/O:1,L=P=>Math.round(P*1e3)/1e3,_=P=>[L((P[0]-I)*z+n),L((P[1]-S)*z+n),L((P[2]-E)*z+n)],T=V.map(({vertices:P,color:d})=>({vertices:P.map(_),color:d}));return{polygons:T,objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:T.length,sourceBytes:o,voxelCount:p.length}}}function dn(e,t){return String.fromCharCode(e.getUint8(t))+String.fromCharCode(e.getUint8(t+1))+String.fromCharCode(e.getUint8(t+2))+String.fromCharCode(e.getUint8(t+3))}function Me(e,t){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:t,metadata:{triangleCount:0,sourceBytes:e}}}var xe="loadMesh";function vt(e){let t=lt(e.polygons),r=Se(t);return r.length===e.polygons.length?e:{...e,polygons:r}}async function pn(e,t){let r=t?.solidTextureSamples;return r===!1?e:Pt(e,typeof r=="object"?r:void 0)}function uo(e){let t=e.split("?")[0].split("#")[0],r=t.lastIndexOf(".");return r<0?"":t.slice(r+1).toLowerCase()}async function mo(e,t){let r=uo(e);if(r==="mtl")throw new Error(`${xe}: .mtl is a material file, not a mesh \u2014 use parseMtl directly`);let n=globalThis.fetch;if(!n)throw new Error(`${xe}: no fetch() in this environment`);let o=t?.baseUrl??e;if(r==="obj"){let i=await n(e);if(!i.ok)throw new Error(`${xe}: ${e} \u2192 ${i.status}`);let s=await i.text(),a=t?.objOptions;if(t?.mtlUrl){let f=await n(t.mtlUrl);if(!f.ok)throw new Error(`${xe}: ${t.mtlUrl} \u2192 ${f.status}`);let l=await f.text(),{colors:u,textures:g}=pt(l),m={},p=globalThis.URL,h=globalThis.document?.baseURI,b=t.mtlUrl;if(p&&h)try{b=new p(t.mtlUrl,h).toString()}catch{}for(let[y,M]of Object.entries(g)){if(p)try{m[y]=new p(M,b).toString();continue}catch{}let C=t.mtlUrl.lastIndexOf("/"),v=C>=0?t.mtlUrl.slice(0,C+1):"";m[y]=M.startsWith("/")||/^https?:\/\//.test(M)?M:v+M}a={...a??{},materialColors:{...u,...a?.materialColors??{}},materialTextures:{...m,...a?.materialTextures??{}}}}let c=gt(s,a);return vt(await pn(c,t))}if(r==="glb"||r==="gltf"){let i=await n(e);if(!i.ok)throw new Error(`${xe}: ${e} \u2192 ${i.status}`);let s=await i.arrayBuffer(),a=bt(s,{baseUrl:o,...t?.gltfOptions??{}});return vt(await pn(a,t))}if(r==="vox"){let i=await n(e);if(!i.ok)throw new Error(`${xe}: ${e} \u2192 ${i.status}`);let s=await i.arrayBuffer();return vt(Vt(s,t?.voxOptions))}throw new Error(`${xe}: unsupported extension ".${r}" (supported: obj, glb, gltf, vox)`)}export{En as BASE_TILE,Ae as DEFAULT_CAMERA_STATE,xn as DEFAULT_PROJECTION,Sr as LoopOnce,Cr as LoopPingPong,Ir as LoopRepeat,Jt as arrowPolygons,Zt as axesHelperPolygons,co as bakeSolidTextureSampledPolygons,Pt as bakeSolidTextureSamples,Vn as buildSceneContext,me as clampChannel,Ut as computeSceneBbox,bn as computeShapeLighting,In as computeTexturePaintMetrics,pr as coverPlanarPolygons,Ln as createIsometricCamera,Or as createPolyAnimationMixer,lt as cullInteriorPolygons,Le as formatColor,Tn as inverseRotateVec3,mo as loadMesh,Se as mergePolygons,On as normalizeInvertMultiplier,nt as normalizePolygons,qt as octahedronPolygons,ye as parseColor,bt as parseGltf,St as parseHexColor,pt as parseMtl,gt as parseObj,qe as parsePureColor,It as parseRgbColor,Vt as parseVox,vn as polygonFaces,Qt as ringPolygons,Cn as rotateVec3,hn as shadeColor};
|
|
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 _ of P)if(!(_.length===0||_.charCodeAt(0)===35)){if(_.startsWith("v ")){let L=_.trim().split(/\s+/);l.push([parseFloat(L[1]),parseFloat(L[2]),parseFloat(L[3])])}else if(_.startsWith("vt ")){let L=_.trim().split(/\s+/);c.push([parseFloat(L[1]),parseFloat(L[2])])}else if(_.startsWith("o "))x=_.trim().slice(2).trim();else if(_.startsWith("usemtl ")){let L=_.trim().split(/\s+/)[1];m=M(L),d=a[L]}else if(_.startsWith("f ")){if(!y())continue;let L=_.trim().split(/\s+/).slice(1),T=[],B=[];for(let O of L){let S=O.split("/");T.push(parseInt(S[0],10)-1);let k=S[1];if(k&&k.length>0){let N=parseInt(k,10)-1;B.push(Number.isFinite(N)?N:null)}else B.push(null)}u.push({idx:T,uvIdx:B,color:m,texture:d})}}if(l.length===0||u.length===0)return Lc(f,e.length);let b=new Set;for(let _ of u)for(let L of _.idx)b.add(L);let A=1/0,w=1/0,I=1/0,C=-1/0,v=-1/0,V=-1/0;for(let _ of b){let L=l[_];L&&(L[0]<A&&(A=L[0]),L[0]>C&&(C=L[0]),L[1]<w&&(w=L[1]),L[1]>v&&(v=L[1]),L[2]<I&&(I=L[2]),L[2]>V&&(V=L[2]))}let R=Math.max(C-A,v-w,V-I),E=R>0?r/R:1,F=_=>Math.round(_*1e3)/1e3,D=l.map(([_,L,T])=>[F((T-I)*E+t),F((_-A)*E+t),F((L-w)*E+t)]),z=[];for(let{idx:_,uvIdx:L,color:T,texture:B}of u)for(let O=1;O<_.length-1;O++){let S=_[0],k=_[O],N=_[O+1],U=D[S],Y=D[k],G=D[N];if(!U||!Y||!G||U[0]===Y[0]&&U[1]===Y[1]&&U[2]===Y[2]||U[0]===G[0]&&U[1]===G[1]&&U[2]===G[2]||Y[0]===G[0]&&Y[1]===G[1]&&Y[2]===G[2])continue;let $;if(B){let q=L[0],ee=L[O],H=L[O+1];if(q!=null&&ee!=null&&H!=null){let K=c[q],te=c[ee],se=c[H];K&&te&&se&&($=[K,te,se])}}let W={vertices:[U,Y,G],color:T};B&&(W.texture=B),$&&(W.uvs=$),z.push(W)}return{polygons:z,objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:z.length,materials:f,sourceBytes:e.length}}}function Lc(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:0,materials:e,sourceBytes:n}}}var gt=e=>Math.round(Math.max(0,Math.min(1,e))*255).toString(16).padStart(2,"0");function dt(e){let n={},r={},t=null;for(let o of e.split(`
|
|
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};
|