@layoutit/polycss-core 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +107 -3
- package/dist/index.d.ts +107 -3
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var as=Object.defineProperty;var bf=Object.getOwnPropertyDescriptor;var xf=Object.getOwnPropertyNames;var Pf=Object.prototype.hasOwnProperty;var Mf=(e,n)=>{for(var t in n)as(e,t,{get:n[t],enumerable:!0})},Af=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of xf(n))!Pf.call(e,r)&&r!==t&&as(e,r,{get:()=>n[r],enumerable:!(o=bf(n,r))||o.enumerable});return e};var Sf=e=>Af(as({},"__esModule",{value:!0}),e);var Qh={};Mf(Qh,{ASYNC_RENDER_BUDGET_MS:()=>Il,ATLAS_CANONICAL_SIZE_AUTO_DESKTOP:()=>cr,ATLAS_CANONICAL_SIZE_EXPLICIT:()=>Ye,ATLAS_MAX_SIZE:()=>Cn,ATLAS_PADDING:()=>Ht,AUTO_ATLAS_LOW_AREA:()=>Zt,AUTO_ATLAS_MAX_BITMAP_SIDE:()=>ht,AUTO_ATLAS_MAX_DECODED_BYTES_DESKTOP:()=>Qo,AUTO_ATLAS_MAX_DECODED_BYTES_MOBILE:()=>qo,AUTO_ATLAS_MEDIUM_AREA:()=>Zo,AUTO_ATLAS_SCALE_GUARD:()=>Jo,BAKED_SHADOW_MIN_UP:()=>Al,BAKED_SHADOW_Z_SQUASH:()=>Ml,BASE_TILE:()=>ke,BASIS_EPS:()=>de,BORDER_SHAPE_BLEED:()=>lr,BORDER_SHAPE_CANONICAL_SIZE:()=>pn,BORDER_SHAPE_CENTER_PERCENT:()=>Jt,BORDER_SHAPE_POINT_EPS:()=>eo,CAMERA_BACKFACE_CULL_EPS:()=>jt,COLOR_PARSE_CACHE_MAX:()=>er,CORNER_SHAPE_DUPLICATE_EPS:()=>no,CORNER_SHAPE_POINT_EPS:()=>We,DECIMAL_SCALES:()=>sr,DEFAULT_AMBIENT_COLOR:()=>Kn,DEFAULT_AMBIENT_INTENSITY:()=>Hn,DEFAULT_ATLAS_CSS_DECIMALS:()=>Qt,DEFAULT_BORDER_SHAPE_DECIMALS:()=>rr,DEFAULT_CAMERA_STATE:()=>Xe,DEFAULT_LIGHT_COLOR:()=>Wn,DEFAULT_LIGHT_DIR:()=>Yn,DEFAULT_LIGHT_INTENSITY:()=>jn,DEFAULT_MATRIX_DECIMALS:()=>dn,DEFAULT_PROJECTION:()=>Ia,DEFAULT_SEAM_BLEED:()=>Ll,DEFAULT_SEAM_FACET_SPLIT_OPTIONS:()=>Is,DEFAULT_SEAM_OVERLAP_OPTIONS:()=>vs,DEFAULT_TILE:()=>mn,LoopOnce:()=>Bi,LoopPingPong:()=>ki,LoopRepeat:()=>xr,MAX_ATLAS_SCALE:()=>Ho,MIN_ATLAS_SCALE:()=>Ko,PROJECTIVE_QUAD_BLEED:()=>mr,PROJECTIVE_QUAD_DENOM_EPS:()=>ur,PROJECTIVE_QUAD_MAX_WEIGHT_RATIO:()=>fr,QUAT_IDENTITY:()=>za,RECEIVER_NORMAL_TOL:()=>Ni,RECEIVER_OFFSET_TOL:()=>Di,RECEIVER_OUTLINE_EXPAND:()=>wi,RECT_EPS:()=>Me,SEAM_LIGHT_EPS:()=>qt,SOLID_QUAD_CANONICAL_SIZE:()=>Zn,SOLID_TRIANGLE_BLEED:()=>or,SOLID_TRIANGLE_CANONICAL_SIZE:()=>ir,SOLID_TRIANGLE_LARGE_BORDER_CANONICAL_SIZE:()=>ar,SURFACE_DISTANCE_EPS:()=>tr,SURFACE_NORMAL_EPS:()=>nr,TEXTURE_EDGE_REPAIR_ALPHA_MIN:()=>Ol,TEXTURE_EDGE_REPAIR_RADIUS:()=>Rl,TEXTURE_EDGE_REPAIR_SOURCE_ALPHA_MIN:()=>Vl,TEXTURE_TRIANGLE_BLEED:()=>_l,VOXEL_CAMERA_CULL_AXIS_EPS:()=>ai,VOXEL_CAMERA_CULL_NORMAL_LIMIT:()=>ci,applyPackedAtlasCanonicalSize:()=>ss,arrowPolygons:()=>yi,atlasArea:()=>is,atlasBitmapMaxSide:()=>Aa,atlasCanonicalSizeForEntry:()=>hf,atlasCanonicalSizeForTextureQuality:()=>Ta,atlasDecodedBytes:()=>Sa,atlasPadding:()=>va,autoAtlasBudgetFactor:()=>Ca,autoAtlasMaxDecodedBytes:()=>Ea,autoAtlasScale:()=>Ma,autoAtlasScaleCap:()=>Pa,axesHelperPolygons:()=>pi,bakeSolidTextureSampledPolygons:()=>Tu,bakeSolidTextureSamples:()=>Tr,basisAxisKey:()=>pa,borderShapeBoundsFromPoints:()=>ta,borderShapeGeometryForPlan:()=>Et,boxPolygons:()=>hi,buildBakedShadowProjectionMatrix:()=>Sl,buildBasisHints:()=>pf,buildEdgeOwners:()=>hr,buildPolyMeshTransform:()=>Tl,buildPolySceneTransform:()=>vl,buildSceneContext:()=>Ba,buildSeamBleedPolygonEdges:()=>ea,buildSeamBleedPolygonSet:()=>ef,buildSharedEdgeMap:()=>Ul,buildTextureEdgeRepairSets:()=>Ju,cachedParsePureColor:()=>qn,cameraCullNormalGroups:()=>fi,cameraCullNormalGroupsFromPolygons:()=>hl,cameraCullNormalKey:()=>ui,cameraCullVisibleSignature:()=>bl,cameraFacingDepth:()=>li,chooseIslandXAxis:()=>ha,chooseLocalBasis:()=>ns,clampChannel:()=>In,classifyFacing:()=>yr,clipPolygonToConvex2D:()=>Sn,colorErrorScore:()=>Bl,compatibleBleedSurface:()=>ts,compatibleSurface:()=>da,computeLightVisibility:()=>pl,computePlanSeamBleedEdgeAmounts:()=>$r,computeProjectiveQuadCoefficients:()=>Qr,computeProjectiveQuadMatrix:()=>ua,computeReceiverShadowFaces:()=>Xl,computeSceneBbox:()=>ms,computeSeamBleedInsets:()=>Yr,computeShapeLighting:()=>wa,computeSolidTriangleColorPlan:()=>lf,computeSolidTriangleColorPlanFromNormal:()=>Zr,computeSolidTrianglePlan:()=>uf,computeSolidTrianglePlanFromCssPoints:()=>qr,computeSurfaceNormal:()=>wn,computeTextureAtlasPlan:()=>xa,computeTextureAtlasPlanPublic:()=>gf,computeTexturePaintMetrics:()=>Ua,computeUvAffine:()=>os,computeUvSampleRect:()=>rs,conePolygons:()=>Ti,convexHull2D:()=>$o,cornerShapeDiagonal:()=>ia,cornerShapeGeometryForPlan:()=>Wr,cornerShapePointSides:()=>ra,coverPlanarPolygons:()=>Ao,createIsometricCamera:()=>Za,createPolyAnimationMixer:()=>Yl,crossVec:()=>mf,cssBorderShapeForGeometry:()=>jr,cssBorderShapeForPlan:()=>nf,cssDistanceToWorld:()=>Oi,cssPoints:()=>vn,cssPositionToWorld:()=>Vi,cullInteriorPolygons:()=>un,cylinderPolygons:()=>Kt,dedupeOverlappingPolygons:()=>rc,dodecahedronPolygons:()=>Ei,dominantCountKey:()=>Hr,dotVec:()=>mo,ensureCcw2D:()=>fn,eulerXYZFromQuat:()=>Wa,evaluateIslandAxis:()=>ga,expandClipPoints:()=>Jn,expandConvexHullOutward:()=>Li,extractSilhouetteLoops:()=>br,filterAtlasPlans:()=>af,findOverlappingPolygonDuplicates:()=>Rt,formatAffineMatrix3dColumns:()=>Yu,formatAffineMatrix3dScalars:()=>Nr,formatAffineMatrix3dTransformScalars:()=>Dr,formatAtlasMatrix:()=>Fr,formatBorderShapeElementStyle:()=>of,formatBorderShapeEntryMatrix:()=>tf,formatBorderShapeMatrix:()=>Wu,formatColor:()=>_t,formatCornerShapeElementStyle:()=>rf,formatCssLength:()=>Xu,formatCssLengthPx:()=>Ku,formatMatrix3d:()=>ju,formatMatrix3dValues:()=>En,formatPercent:()=>Tn,formatScaledMatrixFromPlan:()=>At,formatSolidQuadEntryMatrix:()=>Hu,formatSolidQuadMatrix:()=>Qi,fullRectBounds:()=>ca,getPolygonBasisInfo:()=>ma,getSolidPaintDefaultsForPlansCore:()=>cf,groupReceiverFaceGroups:()=>jo,icosahedronPolygons:()=>Ci,incrementCount:()=>Kr,intersect2DLines:()=>kr,intersect2DLinesRaw:()=>Zu,inverseRotateVec3:()=>Ga,isAxisAlignedSurfaceNormal:()=>mi,isBakedShadowCaster:()=>Cl,isBasisOptimizable:()=>fa,isConvexPolygonPoints:()=>St,isFullRectBasis:()=>ya,isFullRectSolid:()=>Nn,isProjectiveQuadPlan:()=>Tt,isSolidTrianglePlan:()=>nt,isVoxelCameraCullableNormalGroups:()=>yl,loadMesh:()=>$u,makeLocalBasis:()=>tt,mergePolygons:()=>tn,meshScaleVec3:()=>Wo,normalFacesCamera:()=>pt,normalizeAtlasScale:()=>It,normalizeInvertMultiplier:()=>Ha,normalizePolygons:()=>ho,normalizedSeamBleed:()=>zr,octahedronPolygons:()=>Mi,offsetConvexPolygonPoints:()=>et,offsetConvexPolygonPointsByEdgeAmounts:()=>Ct,offsetStableTrianglePoints:()=>Ur,offsetTrianglePoints:()=>lo,optimizeAnimatedMeshPolygons:()=>Iu,optimizeMeshParseResult:()=>Xn,optimizeMeshPolygons:()=>jc,packTextureAtlasPlans:()=>vt,packTextureAtlasPlansWithScaleCore:()=>yf,parseAlpha:()=>oo,parseColor:()=>Dn,parseGltf:()=>Cr,parseHex:()=>gn,parseHexColor:()=>cs,parseMtl:()=>Ir,parseObj:()=>vr,parsePureColor:()=>bn,parseRgbColor:()=>ls,parseStl:()=>wr,parseVox:()=>_r,planePolygons:()=>Pi,polyCssDistanceToWorld:()=>Oi,polyCssPositionToWorld:()=>Vi,polygonContainsPoint:()=>na,polygonCssSurfaceNormal:()=>Uo,polygonFaces:()=>ka,polygonFacesCamera:()=>gl,polygonSignedArea2D:()=>Ii,prepareCasterEdgeOwners:()=>Gl,prepareCasterPolyItems:()=>zl,prepareReceiverFacePlanes:()=>$l,projectCssVertexToGround:()=>El,projectTextureTriangle:()=>ba,quantizeCssColor:()=>gr,quatFromAxisAngle:()=>Xa,quatFromEulerXYZ:()=>Ya,quatMultiply:()=>$a,repairMeshSeams:()=>Rc,resolveBleedRatio:()=>dr,resolveProjectiveQuadGuards:()=>la,resolveSeamBleed:()=>uo,rgbEqual:()=>Nl,rgbKey:()=>pr,rgbToCss:()=>Fl,rgbToHex:()=>ro,ringPolygons:()=>bi,ringQuadPolygons:()=>xi,rotateVec3:()=>yo,rotateVec3InWrapperCssFrame:()=>_n,roundDecimal:()=>we,safariCssProjectiveUnsupported:()=>sf,safePlanSeamBleedAmount:()=>fo,screenToWorldOnSphere:()=>Qa,screenToWorldRay:()=>ps,seamBleedAmountArray:()=>Xr,seamFacetSplitPolygons:()=>ws,seamFacetSplitReport:()=>Vc,seamLightBrightness:()=>Jr,seamOverlapDiagnostics:()=>Lc,seamOverlapPolygons:()=>Ns,seamOverlapReport:()=>Nc,shadeColor:()=>La,shadePolygon:()=>Vn,sharedCornerShapeSide:()=>sa,signedArea2D:()=>Br,simplifyCornerShapePoints:()=>oa,simplifyTriangleMeshPolygons:()=>Lo,spherePolygons:()=>Ai,stableBasisFromPlan:()=>Ji,stableTriangleMatrixDecimals:()=>Gr,stepRgbToward:()=>Dl,tetrahedronPolygons:()=>Si,textureTintFactors:()=>so,tintToCss:()=>wl,torusPolygons:()=>vi,worldCssForMesh:()=>gt,worldDirectionToCss:()=>Yo,worldDirectionToPolyCss:()=>Yo,worldDirectionalLightToCss:()=>Ri,worldDirectionalLightToPolyCss:()=>Ri,worldDistanceToCss:()=>_i,worldDistanceToPolyCss:()=>_i,worldPositionToCss:()=>Qe,worldPositionToPolyCss:()=>Qe});module.exports=Sf(Qh);var Ia="cubic";function cs(e){let n=e.replace("#","");if(n.length===3){let t=parseInt(n[0]+n[0],16),o=parseInt(n[1]+n[1],16),r=parseInt(n[2]+n[2],16);return Number.isNaN(t)||Number.isNaN(o)||Number.isNaN(r)?null:{rgb:[t,o,r],alpha:1}}if(n.length===6){let t=parseInt(n.slice(0,2),16),o=parseInt(n.slice(2,4),16),r=parseInt(n.slice(4,6),16);return Number.isNaN(t)||Number.isNaN(o)||Number.isNaN(r)?null:{rgb:[t,o,r],alpha:1}}return null}function ls(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 bn(e){if(!e)return null;let n=e.trim(),t=cs(n);return t||ls(n)}function In(e){return Math.max(0,Math.min(255,Math.round(e)))}function _t(e){let[n,t,o]=e.rgb.map(In);return e.alpha<1?`rgba(${n}, ${t}, ${o}, ${e.alpha})`:`rgb(${n}, ${t}, ${o})`}var fs={rgb:[204,204,204],alpha:1},_a=new Map;function Dn(e){if(!e)return null;let n=e.trim(),t=_a.get(n);if(t)return t;let o=bn(n);return o?(_a.set(n,o),o):null}function La(e,n){let t=Dn(e)??fs,o=[In(t.rgb[0]+n),In(t.rgb[1]+n),In(t.rgb[2]+n)];return _t({rgb:o,alpha:t.alpha})}var us={direction:[0,0,-1],color:"#ffffff",intensity:1},Oa={color:"#ffffff",intensity:.4};function Va(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 Ra(e,n,t){let o=Dn(n)??fs;return e*(o.rgb[t]/255)}function wa(e,n,t,o){let r=Dn(n)??fs,s=Va(t?.direction??us.direction),i=t?.color??us.color,c=Math.max(0,t?.intensity??us.intensity),a=o?.color??Oa.color,l=Math.max(0,o?.intensity??Oa.intensity),u=Va(e),f=Math.max(0,-(u[0]*s[0]+u[1]*s[1]+u[2]*s[2])),d=c*f,m=[0,0,0];for(let p=0;p<3;p=p+1){let h=r.rgb[p],g=Ra(h,a,p)*l,y=Ra(h,i,p)*d;m[p]=In(g+y)}return _t({rgb:m,alpha:r.alpha})}var Na="#cccccc",po=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Da=(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]],Fa=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],go=e=>Math.hypot(e[0],e[1],e[2]);function Cf(e){let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0;for(let u of e)u[0]<n&&(n=u[0]),u[0]>r&&(r=u[0]),u[1]<t&&(t=u[1]),u[1]>s&&(s=u[1]),u[2]<o&&(o=u[2]),u[2]>i&&(i=u[2]);let c=r-n,a=s-t,l=i-o;return Math.hypot(c,a,l)}function Ef(){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 ho(e){let n=[],t=[];if(!e||e.length===0)return{polygons:n,warnings:t};for(let o=0;o<e.length;o++){let r=e[o];if(!r||!Array.isArray(r.vertices)){t.push(`Polygon ${o}: missing vertices, dropped`);continue}let s=r.vertices;if(s.length<3){t.push(`Polygon ${o}: ${s.length} vertices (need >= 3), dropped`);continue}let i=po(s[1],s[0]),c=po(s[2],s[0]),a=Da(i,c),l=go(a);if(s.length===3){if(l<1e-12){go(i)<1e-12||go(c)<1e-12?t.push(`Polygon ${o}: zero-area triangle (coincident vertices), dropped`):t.push(`Polygon ${o}: vertices collinear, dropped`);continue}}else if(l<1e-12){t.push(`Polygon ${o}: first 3 vertices collinear, dropped`);continue}let u=[{...r,vertices:s.slice()}];if(s.length>=4){let f=[a[0]/l,a[1]/l,a[2]/l],d=Fa(f,s[0]),m=Cf(s),p=Math.max(1e-6,m*.001),h=0;for(let g=3;g<s.length;g++){let y=Math.abs(Fa(f,s[g])-d);y>h&&(h=y)}if(h>p){let g=[],y=r.uvs&&r.uvs.length===s.length?r.uvs:void 0;for(let b=1;b<s.length-1;b++){let x=[s[0].slice(),s[b].slice(),s[b+1].slice()],P=Da(po(x[1],x[0]),po(x[2],x[0]));if(go(P)<1e-12)continue;let S={...r,vertices:x};y&&(S.uvs=[y[0].slice(),y[b].slice(),y[b+1].slice()]),g.push(S)}if(g.length===0){t.push(`Polygon ${o}: ${s.length} non-coplanar vertices, fan-triangulation produced no valid triangles, dropped`);continue}t.push(`Polygon ${o}: ${s.length} non-coplanar vertices, fan-triangulated to ${g.length} triangles`),u=g}}for(let f of u){let d=Tf(f,o,t);d&&n.push(d)}}return{polygons:n,warnings:t}}function Tf(e,n,t){let o={vertices:e.vertices},r=e.texture;if(typeof r=="string"&&r===""&&(r=void 0),e.color!==void 0&&r!==void 0&&Ef()&&t.push(`Polygon ${n}: color and texture both set; texture wins`),e.color!==void 0&&(Dn(e.color)?o.color=e.color:(t.push(`Polygon ${n}: invalid color "${e.color}", replaced with ${Na}`),o.color=Na)),r!==void 0&&(o.texture=r,e.textureWrap&&(o.textureWrap={...e.textureWrap}),e.textureAlphaMode&&(o.textureAlphaMode=e.textureAlphaMode)),e.doubleSided===!0&&(o.doubleSided=!0),e.uvs!==void 0&&(!Array.isArray(e.uvs)||e.uvs.length!==e.vertices.length?t.push(`Polygon ${n}: uvs length ${Array.isArray(e.uvs)?e.uvs.length:"?"} != vertices length ${e.vertices.length}, uvs stripped`):o.uvs=e.uvs),e.data!==void 0&&e.data!==null&&typeof e.data=="object"){let s={},i=!1;for(let c of Object.keys(e.data)){let a=e.data[c];typeof a=="string"||typeof a=="number"||typeof a=="boolean"?s[c]=a:(i=!0,t.push(`Polygon ${n}: data["${c}"] has non-primitive value, key dropped`))}Object.keys(s).length>0&&(o.data=s)}return o}function ms(e){if(!e||e.length===0)return{min:[0,0,0],max:[0,0,0]};let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0,c=!1;for(let a of e)if(a?.vertices)for(let l of a.vertices)l&&(c=!0,l[0]<n&&(n=l[0]),l[0]>r&&(r=l[0]),l[1]<t&&(t=l[1]),l[1]>s&&(s=l[1]),l[2]<o&&(o=l[2]),l[2]>i&&(i=l[2]));return c?{min:[n,t,o],max:[r,s,i]}:{min:[0,0,0],max:[0,0,0]}}function Ba(e){let n=e.polygons??[],t,o;if(e.skipNormalize)t=n,o=[];else{let c=ho(n);t=c.polygons,o=c.warnings}let r=ms(t),s=[r.max[0]-r.min[0],r.max[1]-r.min[1],r.max[2]-r.min[2]];return{context:{polygons:t,sceneBbox:r,warnings:o},dimensions:{sceneBbox:r,size:s},warnings:o}}function ka(e){return!e.vertices||e.vertices.length<3?[]:[{v:e.vertices.map(n=>[n[0],n[1],n[2]]),color:e.color}]}function vf(e){let n=e[0],t=e[1],o=e[2],r=[t[0]-n[0],t[1]-n[1],t[2]-n[2]],s=[o[0]-n[0],o[1]-n[1],o[2]-n[2]],i=r[1]*s[2]-r[2]*s[1],c=r[2]*s[0]-r[0]*s[2],a=r[0]*s[1]-r[1]*s[0],l=Math.hypot(i,c,a);return l<=1e-9?null:[i/l,c/l,a/l]}function If(e){let n=0;for(let t=0;t<e.length;t++){let[o,r]=e[t],[s,i]=e[(t+1)%e.length];n+=o*i-s*r}return Math.abs(n)/2}function _f(e,n,t){if(!e||e.length<3)return null;let o=e.map(c=>[c[1]*n,c[0]*n,c[2]*t]),r=vf(o);if(!r)return null;let s=1/0,i=0;for(let c=0;c<o.length;c++){let a=o[c],l=o[(c+1)%o.length],u=[l[0]-a[0],l[1]-a[1],l[2]-a[2]],f=u[0]*r[0]+u[1]*r[1]+u[2]*r[2],d=[u[0]-f*r[0],u[1]-f*r[1],u[2]-f*r[2]],m=Math.hypot(d[0],d[1],d[2]);if(m<=1e-9)continue;let p=[d[0]/m,d[1]/m,d[2]/m],h=[r[1]*p[2]-r[2]*p[1],r[2]*p[0]-r[0]*p[2],r[0]*p[1]-r[1]*p[0]],g=Math.hypot(h[0],h[1],h[2]);if(g<=1e-9)continue;let y=[h[0]/g,h[1]/g,h[2]/g],b=o.map(v=>{let I=v[0]-a[0],_=v[1]-a[1],O=v[2]-a[2];return[I*p[0]+_*p[1]+O*p[2],I*y[0]+_*y[1]+O*y[2]]}),x=1/0,P=1/0,S=-1/0,C=-1/0;for(let[v,I]of b)x=Math.min(x,v),P=Math.min(P,I),S=Math.max(S,v),C=Math.max(C,I);let T=S-x,A=C-P,M=Math.max(1,Math.ceil(T))*Math.max(1,Math.ceil(A)),E=If(b);E<=1e-9||M<s&&(s=M,i=E)}return!Number.isFinite(s)||i<=1e-9?null:{elementArea:s,polygonArea:i}}function Ua(e,n={}){let t=n.tileSize??100,o=n.layerElevation??t,r=n.texturedOnly??!0,s=0,i=0,c=0,a=0,l=0;for(let f of e){let d=!!f.texture;if(r&&!d)continue;let m=_f(f.vertices,t,o);m&&(s+=1,d&&(i+=1),c+=m.elementArea,a+=m.polygonArea,l=Math.max(l,Math.max(0,m.elementArea-m.polygonArea)/m.elementArea))}let u=Math.max(0,c-a);return{totalPolygons:e.length,measuredPolygons:s,texturedPolygons:i,elementArea:c,polygonArea:a,transparentArea:u,transparentRatio:c>0?u/c:0,overdrawRatio:a>0?c/a:0,worstTransparentRatio:l}}function yo(e,n,t,o){let r=n*Math.PI/180,s=t*Math.PI/180,i=o*Math.PI/180,[c,a,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[c,a]=[c*u-a*f,c*f+a*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[c,l]=[c*u+l*f,-c*f+l*u]}if(r!==0){let u=Math.cos(r),f=Math.sin(r);[a,l]=[a*u-l*f,a*f+l*u]}return[c,a,l]}function Ga(e,n){let t=-n[0]*Math.PI/180,o=-n[1]*Math.PI/180,r=-n[2]*Math.PI/180,[s,i,c]=e;if(t!==0){let a=Math.cos(t),l=Math.sin(t);[i,c]=[i*a-c*l,i*l+c*a]}if(o!==0){let a=Math.cos(o),l=Math.sin(o);[s,c]=[s*a+c*l,-s*l+c*a]}if(r!==0){let a=Math.cos(r),l=Math.sin(r);[s,i]=[s*a-i*l,s*l+i*a]}return[s,i,c]}function _n(e,n){let[t,o,r]=e,s=-(n[2]??0)*Math.PI/180;if(s!==0){let a=Math.cos(s),l=Math.sin(s);[t,o]=[t*a-o*l,t*l+o*a]}let i=-(n[1]??0)*Math.PI/180;if(i!==0){let a=Math.cos(i),l=Math.sin(i);[o,r]=[o*a-r*l,o*l+r*a]}let c=-(n[0]??0)*Math.PI/180;if(c!==0){let a=Math.cos(c),l=Math.sin(c);[t,r]=[t*a+r*l,-t*l+r*a]}return[t,o,r]}var ds=Math.PI/180,Ot=180/Math.PI,za=[1,0,0,0];function $a(e,n){let[t,o,r,s]=e,[i,c,a,l]=n;return[t*i-o*c-r*a-s*l,t*c+o*i+r*l-s*a,t*a-o*l+r*i+s*c,t*l+o*a-r*c+s*i]}function Xa(e,n){let t=n*.5,o=Math.sin(t);return[Math.cos(t),e[0]*o,e[1]*o,e[2]*o]}function Ya(e){let n=e[0]*ds,t=e[1]*ds,o=e[2]*ds,r=Math.cos(n*.5),s=Math.sin(n*.5),i=Math.cos(t*.5),c=Math.sin(t*.5),a=Math.cos(o*.5),l=Math.sin(o*.5);return[r*i*a-s*c*l,s*i*a+r*c*l,r*c*a-s*i*l,r*i*l+s*c*a]}function Wa(e){let[n,t,o,r]=e,s=2*(t*r+n*o),i=2*(o*r-n*t),c=1-2*(t*t+o*o),a=2*(t*o-n*r),l=1-2*(o*o+r*r),u=Math.max(-1,Math.min(1,s)),f=Math.asin(u);return Math.abs(u)<.99999?[Math.atan2(-i,c)*Ot,f*Ot,Math.atan2(-a,l)*Ot]:[Math.atan2(2*(o*r+n*t),1-2*(t*t+r*r))*Ot,f*Ot,0]}var ke=50;function Ha(e){if(typeof e=="number")return e===0?void 0:e<0?-1:1;if(typeof e=="boolean")return e?-1:1}var Xe={target:[0,0,0],rotX:65,rotY:45,zoom:.65,distance:0},ja=100,Ka=1e4,at=e=>Math.round(e*ja)/ja,Of=e=>Math.round(e*Ka)/Ka;function Za(e={}){let n={target:e.target??[...Xe.target],rotX:e.rotX??Xe.rotX,rotY:e.rotY??Xe.rotY,zoom:e.zoom??Xe.zoom,distance:e.distance??Xe.distance};function t(r){r.target!==void 0&&(n.target=[at(r.target[0]),at(r.target[1]),at(r.target[2])]),r.rotX!==void 0&&(n.rotX=at(r.rotX)),r.rotY!==void 0&&(n.rotY=at(r.rotY)),r.zoom!==void 0&&(n.zoom=Of(r.zoom)),r.distance!==void 0&&(n.distance=at(r.distance))}function o(r={}){let i=(r.cols??0)*50,c=(r.rows??0)*50,[a,l,u]=n.target,f=l*50,d=a*50,m=u*50;return{transform:`${n.distance!==0?` translateZ(${-n.distance}px)`:""}scale(${n.zoom}) rotateX(${n.rotX}deg) rotate(${n.rotY}deg) translate3d(${-f}px, ${-d}px, ${-m}px)`,width:`${i}px`,height:`${c}px`}}return{state:n,update:t,getStyle:o}}var qa=Math.PI/180;function ps(e){let{camera:n,tileSize:t,viewportCenterX:o,viewportCenterY:r,mouseX:s,mouseY:i}=e,c=n.rotX*qa,a=n.rotY*qa,l=n.zoom/t,u=n.target,f=s-o,d=i-r,m=f/l,p=d/l,h=Math.cos(c),g=Math.sin(c),y=m,b=p*h,x=-p*g,P=0,S=g,C=h,T=Math.cos(a),A=Math.sin(a),M=y*T+b*A,E=-y*A+b*T,v=x,I=P*T+S*A,_=-P*A+S*T,O=C,V=u[1]*t,w=u[0]*t,F=u[2]*t,D=M+V,H=E+w,K=v+F,Q=[H/t,D/t,K/t],ne=[_/t,I/t,O/t];return{origin:Q,direction:ne}}function Qa(e){let{origin:n,direction:t}=ps(e),{sphereCenter:o,sphereRadius:r}=e,s=n[0]-o[0],i=n[1]-o[1],c=n[2]-o[2],a=t[0],l=t[1],u=t[2],f=a*a+l*l+u*u;if(f===0)return null;let d=2*(s*a+i*l+c*u),m=s*s+i*i+c*c-r*r,p=d*d-4*f*m;if(p>=0){let T=Math.sqrt(p),A=(-d-T)/(2*f),M=(-d+T)/(2*f),E=t[2]>=0?Math.max(A,M):Math.min(A,M);return[n[0]+E*t[0],n[1]+E*t[1],n[2]+E*t[2]]}let h=-(s*a+i*l+c*u)/f,g=n[0]+h*a,y=n[1]+h*l,b=n[2]+h*u,x=g-o[0],P=y-o[1],S=b-o[2],C=Math.hypot(x,P,S);return C===0?null:[o[0]+x/C*r,o[1]+P/C*r,o[2]+S/C*r]}var On=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],bo=(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]],Vt=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],gs=e=>Math.hypot(e[0],e[1],e[2]),ct=(e,n)=>e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2];function Vf(e){return`${e[0]},${e[1]},${e[2]}`}function ec(e){if(e.length<3)return null;let n=On(e[1],e[0]),t=On(e[2],e[0]),o=bo(n,t),r=gs(o);if(r<1e-12)return null;let s=[o[0]/r,o[1]/r,o[2]/r],i=Vt(s,e[0]);return{normal:s,d:i}}function Rf(e,n){return Vt(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.05}function Lf(e,n){return Vt(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.001}function wf(e,n,t,o){let r=e.vertices.findIndex(A=>ct(A,t)),s=e.vertices.findIndex(A=>ct(A,o)),i=n.vertices.findIndex(A=>ct(A,t)),c=n.vertices.findIndex(A=>ct(A,o));if(r<0||s<0||i<0||c<0)return null;let a=e.vertices.length,l=n.vertices.length,u=(r+1)%a===s,f=(i+1)%l===c;if(u===f)return null;let d=u?s:r,m=u?r:s,p=f?c:i,h=f?i:c,g=!!(e.uvs&&n.uvs),y=[],b=g?[]:void 0,x=d;for(;y.push(e.vertices[x]),b&&b.push(e.uvs[x]),x!==m;)x=(x+1)%a;for(x=p;y.push(n.vertices[x]),b&&b.push(n.uvs[x]),x!==h;)x=(x+1)%l;let P=[],S=b?[]:void 0;for(let A=0;A<y.length;A++)(P.length===0||!ct(y[A],P[P.length-1]))&&(P.push(y[A]),S&&b&&S.push(b[A]));if(P.length>1&&ct(P[0],P[P.length-1])&&(P.pop(),S?.pop()),g)return Ja(P,S);let C=[],T=S?[]:void 0;for(let A=0;A<P.length;A++){let M=P[(A-1+P.length)%P.length],E=P[A],v=P[(A+1)%P.length],I=bo(On(E,M),On(v,M));gs(I)>1e-9&&(C.push(E),T&&S&&T.push(S[A]))}return C.length<3?null:Ja(C,T)}function Nf(e,n){let t=e.length,o=0;for(let r=0;r<t;r++){let s=e[r],i=e[(r+1)%t],c=e[(r+2)%t],a=On(i,s),l=On(c,i),u=Vt(bo(a,l),n);if(Math.abs(u)<1e-9)continue;let f=u>0?1:-1;if(o===0)o=f;else if(f!==o)return!1}return!0}function Df(e,n){if(e.length<3)return!1;let t=ec(e);if(!t)return!1;for(let o of e)if(Math.abs(Vt(t.normal,o)-t.d)>n)return!1;return!0}function Ff(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function Bf(e,n){return(e?.s??"")===(n?.s??"")&&(e?.t??"")===(n?.t??"")}function kf(e,n){return(e??"")===(n??"")}function Uf(e,n){let t=[];for(let o=1;o<e.length-1;o++)t.push({vertices:[[...e[0]],[...e[o]],[...e[o+1]]],uvs:[[...n[0]],[...n[o]],[...n[o+1]]]});return t}function Ja(e,n){for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length];if(!(gs(bo(On(r,o),On(s,o)))<=1e-9))return t===0?{vertices:e,uvs:n}:{vertices:[...e.slice(t),...e.slice(0,t)],uvs:n?[...n.slice(t),...n.slice(0,t)]:void 0}}return{vertices:e,uvs:n}}function tn(e){let n=[],t=[],o=new WeakMap,r=m=>{let p=o.get(m);if(p)return p;let h=Vf(m);return o.set(m,h),h},s=(m,p)=>{let h=r(m),g=r(p);return h<g?`${h}|${g}`:`${g}|${h}`},i=(m,p)=>r(m)<r(p)?1:-1,c=m=>{let p=new Map;for(let h=0;h<m.length;h++){let g=m[h],y=m[(h+1)%m.length];p.set(s(g,y),i(g,y))}return p},a=0,l=!1,u=m=>(a+=m,a<=8e6?!0:(l=!0,!1));for(let m of e??[]){if(!m||!m.vertices||m.vertices.length<3){m&&n.push(m);continue}let p=m.vertices,h=ec(p);if(!h){n.push(m);continue}let g=m.texture&&m.uvs&&m.uvs.length===p.length?m.uvs.map(b=>[b[0],b[1]]):void 0,y=m.texture&&g?m.textureTriangles?.length?Ff(m.textureTriangles):Uf(p,g):void 0;t.push({vertices:p,uvs:g,color:m.color??"#cccccc",texture:m.texture,textureWrap:m.texture?m.textureWrap:void 0,textureAlphaMode:m.texture?m.textureAlphaMode:void 0,textureTriangles:y,normal:h.normal,d:h.d,edgeDirections:c(p),alive:!0,doubleSided:m.doubleSided===!0,data:m.data})}let f=()=>{let m=new Map;for(let g=0;g<t.length;g++){let y=t[g];if(!y.alive)continue;let b=y.vertices.length;if(!u(b))return!1;for(let x=0;x<b;x++){let P=y.vertices[x],S=y.vertices[(x+1)%b],C=s(P,S),T=m.get(C);T?T.second<0&&(T.second=g):(T={a:P,b:S,first:g,second:-1},m.set(C,T))}}let p=!1,h=(g,y,b)=>{let x=g.edgeDirections.get(s(y,b));return x?x===i(y,b)?1:-1:0};for(let g of m.values()){if(g.second<0)continue;let y=g.first,b=g.second;if(y===b)continue;let x=t[y],P=t[b];if(!x.alive||!P.alive||x.color!==P.color||x.texture!==P.texture||!Bf(x.textureWrap,P.textureWrap)||!kf(x.textureAlphaMode,P.textureAlphaMode)||x.doubleSided!==P.doubleSided)continue;let S=!!(x.texture||P.texture);if(S&&(!x.textureTriangles||!P.textureTriangles)||!!x.uvs!=!!P.uvs||(S?!Lf(x,P):!Rf(x,P)))continue;if(!u(x.vertices.length+P.vertices.length))return!1;let C=h(x,g.a,g.b);if(C===0)continue;let T=h(P,g.a,g.b);if(T===0||C===T)continue;let[A,M]=C===1?[g.a,g.b]:[g.b,g.a],E=wf(x,P,A,M);if(E){if(!u(E.vertices.length))return!1;E.vertices.length>96||Df(E.vertices,.001)&&Nf(E.vertices,x.normal)&&(x.vertices=E.vertices,x.uvs=E.uvs,x.edgeDirections=c(E.vertices),x.textureTriangles=S?[...x.textureTriangles??[],...P.textureTriangles??[]]:void 0,P.alive=!1,p=!0)}}return p},d=0;for(;!l&&d<16&&f();)d++;for(let m of t){if(!m.alive)continue;let p={vertices:m.vertices.map(h=>[h[0],h[1],h[2]]),color:m.color};m.texture&&(p.texture=m.texture),m.texture&&m.textureWrap&&(p.textureWrap={...m.textureWrap}),m.texture&&m.textureAlphaMode&&(p.textureAlphaMode=m.textureAlphaMode),m.uvs&&(p.uvs=m.uvs.map(h=>[h[0],h[1]])),m.textureTriangles?.length&&(p.textureTriangles=m.textureTriangles),m.doubleSided&&(p.doubleSided=!0),m.data&&(p.data=m.data),n.push(p)}return n}var Gf=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],zf=(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]],on=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2];function $f(e,n){let t=e.vertices;if(!t||t.length<3)return null;let o=0,r=0,s=0;for(let h=0;h<t.length;h++){let g=t[h],y=t[(h+1)%t.length];o+=(g[1]-y[1])*(g[2]+y[2]),r+=(g[2]-y[2])*(g[0]+y[0]),s+=(g[0]-y[0])*(g[1]+y[1])}let i=Math.hypot(o,r,s);if(i<1e-12)return null;let c=[o/i,r/i,s/i],a=0,l=0,u=0;for(let h of t)a+=h[0],l+=h[1],u+=h[2];let f=1/t.length,d=[a*f,l*f,u*f],m=on(c,d),p=i*.5;return{index:n,polygon:e,normal:c,d:m,centroid:d,area:p,local2D:null,bbox2D:null,basis:null}}function Xf(e,n){return e.polygon.doubleSided===!0&&n.polygon.doubleSided===!0&&on(e.normal,n.normal)<0}function Yf(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=n[0]-e[0]*on(n,e),o=n[1]-e[1]*on(n,e),r=n[2]-e[2]*on(n,e),s=Math.hypot(t,o,r);t/=s,o/=s,r/=s;let i=[t,o,r],c=zf(e,i);return{u:i,v:c}}function Wf(e){if(e.local2D)return;let n=Yf(e.normal),t=[],o=1/0,r=1/0,s=-1/0,i=-1/0;for(let c of e.polygon.vertices){let a=on(c,n.u),l=on(c,n.v);t.push([a,l]),a<o&&(o=a),a>s&&(s=a),l<r&&(r=l),l>i&&(i=l)}e.local2D=t,e.bbox2D={min:[o,r],max:[s,i]},e.basis=n}function jf(e,n){let t=[],o=1/0,r=1/0,s=-1/0,i=-1/0;for(let c of e.vertices){let a=on(c,n.u),l=on(c,n.v);t.push([a,l]),a<o&&(o=a),a>s&&(s=a),l<r&&(r=l),l>i&&(i=l)}return{local2D:t,bbox2D:{min:[o,r],max:[s,i]}}}function Kf(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 nc(e,n){let t=!1;for(let o=0,r=n.length-1;o<n.length;r=o++){let s=n[o],i=n[r];s[1]>e[1]!=i[1]>e[1]&&e[0]<(i[0]-s[0])*(e[1]-s[1])/(i[1]-s[1]+1e-30)+s[0]&&(t=!t)}return t}function tc(e){let n=0,t=0;for(let o of e)n+=o[0],t+=o[1];return[n/e.length,t/e.length]}function Hf(e,n){let t=tc(e),o=tc(n),r=1e-4,s=0;for(let l of e){let u=[l[0]+(t[0]-l[0])*r,l[1]+(t[1]-l[1])*r];nc(u,n)&&s++}let i=0;for(let l of n){let u=[l[0]+(o[0]-l[0])*r,l[1]+(o[1]-l[1])*r];nc(u,e)&&i++}let c=s/e.length,a=i/n.length;return Math.max(c,a)}function Zf(e,n){let t=e.normal[0],o=e.normal[1],r=e.normal[2],s=Math.abs(t),i=Math.abs(o),c=Math.abs(r),a=t;i>s&&i>c?a=o:c>s&&c>i&&(a=r),a<0&&(t=-t,o=-o,r=-r);let l=Math.round(t/.05),u=Math.round(o/.05),f=Math.round(r/.05),d=e.d*(e.normal[0]===t&&e.normal[1]===o&&e.normal[2]===r?1:-1),m=Math.round(d/(n*2));return`${l},${u},${f}|${m}`}function qf(e,n,t,o){let r=on(e.normal,n.normal);if(Math.abs(r)<1-t)return!1;let s=r>0?1:-1;return Math.abs(e.d-s*n.d)<o}function oc(e,n){let t=Gf(n,e.centroid);return on(e.normal,t)>0}function Rt(e,n){if(!e||e.length<2)return new Set;let t=n?.normalTolerance??.001,o=n?.distanceTolerance??.05,r=n?.overlapFraction??.7,s=n?.preserveDoubleSidedBackfaces??!0,i=[];for(let p=0;p<e.length;p++){let h=$f(e[p],p);h&&i.push(h)}if(i.length<2)return new Set;let c=0,a=0,l=0,u=0;for(let p of i)c+=p.centroid[0]*p.area,a+=p.centroid[1]*p.area,l+=p.centroid[2]*p.area,u+=p.area;let f=u>0?[c/u,a/u,l/u]:[0,0,0],d=new Map;for(let p of i){let h=Zf(p,o),g=d.get(h);g||(g=[],d.set(h,g)),g.push(p)}let m=new Set;for(let p of d.values())if(!(p.length<2))for(let h=0;h<p.length;h++){let g=p[h];if(!m.has(g.index))for(let y=h+1;y<p.length;y++){let b=p[y];if(m.has(b.index)||!qf(g,b,t,o)||s&&Xf(g,b))continue;Wf(g);let x=jf(b.polygon,g.basis);if(!Kf(g.bbox2D,x.bbox2D)||Hf(g.local2D,x.local2D)<r)continue;let S=oc(g,f),C=oc(b,f),T;if(S&&!C?T=g:C&&!S?T=b:T=g.area<b.area?g:b,m.add(T.index),T===g)break}}return m}function rc(e,n){if(!e||e.length<2)return e??[];let t=Rt(e,n);if(t.size===0)return e;let o=[];for(let r=0;r<e.length;r++)t.has(r)||o.push(e[r]);return o}var Qf=4,Jf=8,em=.001,nm=1e-9,tm=1e6,om=1e6,rm=8,Fn=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],bs=(e,n)=>[e[0]+n[0],e[1]+n[1],e[2]+n[2]],Nt=(e,n)=>[e[0]*n,e[1]*n,e[2]*n],Bn=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],cc=(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]],lc=e=>Math.hypot(e[0],e[1],e[2]);function Dt(e){let n=lc(e);return n<=nm?null:[e[0]/n,e[1]/n,e[2]/n]}function wt(e,n){return Math.round(e*n)/n}function rn(e){return wt(e,tm)}function hs(e){return wt(e,om)}function ys(e,n){return Math.abs(e[0]-n[0])<=1e-7&&Math.abs(e[1]-n[1])<=1e-7}function xn(e){return`${rn(e[0])},${rn(e[1])}`}function sm(e){let n=xn(e.a),t=xn(e.b);return n<t?`${n}|${t}`:`${t}|${n}`}function lt(e){return`${e[0]},${e[1]},${e[2]}`}function uc(e,n){let t=lt(e),o=lt(n);return t<o?`${t}|${o}`:`${o}|${t}`}function xs(e){return e?Object.keys(e).sort().map(n=>`${n}:${String(e[n])}`).join("|"):""}function sc(e){return[e.color??"#cccccc",e.texture??"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided",xs(e.data)].join("|")}function fc(e){let n=e.vertices;if(!n||n.length<3)return null;let t=n[0],o=[0,0,0];for(let s=1;s<n.length-1;s++)o=bs(o,cc(Fn(n[s],t),Fn(n[s+1],t)));let r=Dt(o);return r?{normal:r,d:Bn(r,t)}:null}function im(e,n,t){return Bn(e.normal,n.normal)>1-t&&Math.abs(e.d-n.d)<=t}function am(e){return!e.texture&&!e.uvs&&!e.textureTriangles}function cm(e,n){let t=n[e[0]]?.data,o=xs(t);for(let r of e)if(xs(n[r].data)!==o)return;return t?{...t}:void 0}function lm(e,n){return e.every(t=>n[t].doubleSided===!0)}function um(e,n){let t=e.map(a=>am(a)?fc(a):null),o=t.map(Boolean),r=new Map;for(let a=0;a<e.length;a++){if(!t[a])continue;let l=e[a].vertices;for(let u=0;u<l.length;u++){let f=uc(l[u],l[(u+1)%l.length]),d=r.get(f),m={polygon:a,edge:u};d?d.push(m):r.set(f,[m])}}let s=e.map(()=>new Set);for(let a of r.values())if(!(a.length<2))for(let l=0;l<a.length;l++)for(let u=l+1;u<a.length;u++){let f=a[l].polygon,d=a[u].polygon,m=t[f],p=t[d];!m||!p||sc(e[f])===sc(e[d])&&im(m,p,n)&&(s[f].add(d),s[d].add(f))}let i=new Set,c=[];for(let a=0;a<e.length;a++){if(!o[a]||i.has(a))continue;let l=[],u=[a];for(i.add(a);u.length>0;){let f=u.shift();l.push(f);for(let d of s[f])i.has(d)||(i.add(d),u.push(d))}c.push(l)}return{groups:c}}function fm(e,n){let t=new Set(e),o=new Map;for(let s of e){let i=n[s].vertices;for(let c=0;c<i.length;c++){let a=uc(i[c],i[(c+1)%i.length]),l=o.get(a),u={polygon:s,edge:c};l?l.push(u):o.set(a,[u])}}let r=[];for(let s of o.values()){let i=s.filter(c=>t.has(c.polygon));if(i.length===1){let c=i[0],a=n[c.polygon].vertices;r.push({a:a[c.edge],b:a[(c.edge+1)%a.length]})}else if(i.length!==2)return null}return r}function mm(e){let n=new Map;for(let t of e)n.set(lt(t.a),(n.get(lt(t.a))??0)+1),n.set(lt(t.b),(n.get(lt(t.b))??0)+1);for(let t of n.values())if(t%2!==0)return!1;return!0}function mc(e){let n=[e[0],e[1],e[2]],t=Math.abs(n[0])>=Math.abs(n[1])&&Math.abs(n[0])>=Math.abs(n[2])?0:Math.abs(n[1])>=Math.abs(n[2])?1:2;return n[t]<0?[-n[0],-n[1],-n[2]]:n}function dm(e){let n=mc(e);return`${wt(n[0],1e3)},${wt(n[1],1e3)},${wt(n[2],1e3)}`}function pm(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=Fn(n,Nt(e,Bn(n,e)));return Dt(t)??[1,0,0]}function gm(e,n,t){let o=new Map;for(let s of e){let i=Fn(s.b,s.a),c=Fn(i,Nt(n,Bn(i,n))),a=Dt(c);if(!a)continue;let l=mc(a),u=dm(l),f=lc(c),d=o.get(u);d?d.weight+=f:o.set(u,{axis:l,weight:f})}let r=[...o.values()].sort((s,i)=>i.weight-s.weight).slice(0,t).map(s=>s.axis);return r.length===0&&r.push(pm(n)),r}function ic(e,n,t,o){let r=Fn(e,n);return[rn(Bn(r,t)),rn(Bn(r,o))]}function hm(e){let n=[...e].sort((o,r)=>o-r),t=[];for(let o of n)(t.length===0||Math.abs(o-t[t.length-1])>1e-7)&&t.push(o);return t}function Lt(e,n){let[t,o]=e.a,[r,s]=e.b,i=(n-t)/(r-t);return rn(o+(s-o)*i)}function Ps(e){let n=[];for(let o of e)(n.length===0||Math.abs(o[0]-n[n.length-1][0])>1e-7||Math.abs(o[1]-n[n.length-1][1])>1e-7)&&n.push(o);n.length>1&&Math.abs(n[0][0]-n[n.length-1][0])<=1e-7&&Math.abs(n[0][1]-n[n.length-1][1])<=1e-7&&n.pop();let t=[];for(let o=0;o<n.length;o++){let r=n[(o-1+n.length)%n.length],s=n[o],i=n[(o+1)%n.length],c=(s[0]-r[0])*(i[1]-s[1])-(s[1]-r[1])*(i[0]-s[0]);Math.abs(c)>1e-8&&t.push(s)}return t}function Po(e){let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-r[0]*o[1]}return n/2}function Mo(e){let n=1/0,t=1/0,o=-1/0,r=-1/0;for(let[s,i]of e)n=Math.min(n,s),t=Math.min(t,i),o=Math.max(o,s),r=Math.max(r,i);return{minX:n,minY:t,maxX:o,maxY:r}}function dc(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 xo(e){return Math.abs(Po(e))}function ym(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=1e-8)continue;let c=i>0?1:-1;if(n===0)n=c;else if(n!==c)return!1}return!0}function bm(e,n,t){let o=t[0]-n[0],r=t[1]-n[1],s=e[0]-n[0],i=e[1]-n[1],c=o*i-r*s,a=Math.hypot(o,r);if(a<=1e-9||Math.abs(c)>Math.max(1e-8,a*1e-8))return!1;let l=s*o+i*r;return l>=-1e-8&&l<=o*o+r*r+1e-8}function xm(e,n,t){let o=t[0]-n[0],r=t[1]-n[1],s=o*o+r*r;return s<=1e-12?0:((e[0]-n[0])*o+(e[1]-n[1])*r)/s}function Pm(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],c=o[1]-t[1],a=Math.hypot(r,s),l=Math.hypot(i,c);if(a<=1e-9||l<=1e-9)return!1;let u=r*(t[1]-e[1])-s*(t[0]-e[0]),f=r*(o[1]-e[1])-s*(o[0]-e[0]),d=r*c-s*i,m=Math.max(1e-8,Math.max(a,l)*1e-8);if(Math.abs(u)>m||Math.abs(f)>m||Math.abs(d)>m)return!1;let p=Math.abs(r)>=Math.abs(s),h=Math.min(p?e[0]:e[1],p?n[0]:n[1]),g=Math.max(p?e[0]:e[1],p?n[0]:n[1]),y=Math.min(p?t[0]:t[1],p?o[0]:o[1]),b=Math.max(p?t[0]:t[1],p?o[0]:o[1]);return Math.min(g,b)-Math.max(h,y)>1e-8}function Mm(e,n){for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];for(let s=0;s<n.length;s++)if(Pm(o,r,n[s],n[(s+1)%n.length]))return!0}return!1}function ac(e,n){let t=[];for(let o=0;o<e.length;o++){let r=e[o],s=e[(o+1)%e.length],i=[{t:0,point:r},{t:1,point:s}];for(let a of n)ys(a,r)||ys(a,s)||bm(a,r,s)&&i.push({t:xm(a,r,s),point:a});i.sort((a,l)=>a.t-l.t);let c=[];for(let a of i)c.some(l=>Math.abs(l.t-a.t)<=1e-8||ys(l.point,a.point))||c.push(a);for(let a=0;a<c.length-1;a++){let l=c[a].point,u=c[a+1].point;Math.hypot(u[0]-l[0],u[1]-l[1])<=1e-8||t.push({a:[rn(l[0]),rn(l[1])],b:[rn(u[0]),rn(u[1])]})}}return t}function Am(e,n,t=Mo(e),o=Mo(n)){if(!dc(t,o)||!Mm(e,n))return null;let r=[...ac(e,n),...ac(n,e)],s=new Map;for(let y of r){let b=sm(y),x=s.get(b);x?x.push(y):s.set(b,[y])}let i=!1,c=[];for(let y of s.values()){if(y.length===1){c.push(y[0]);continue}i=!0;let b=y.filter(P=>xn(P.a)<xn(P.b)).length,x=y.length-b;if(b!==x)return null}if(!i||c.length<3)return null;let a=new Map;for(let y of c){let b=xn(y.a);if(a.has(b))return null;a.set(b,y)}let l=c[0],u=xn(l.a),f=[],d=new Set,m=u;for(let y=0;y<=c.length;y++){let b=a.get(m);if(!b)return null;let x=`${xn(b.a)}>${xn(b.b)}`;if(d.has(x))return null;if(d.add(x),f.push(b.a),m=xn(b.b),m===u)break}if(m!==u||d.size!==c.length)return null;let p=Ps(f);if(p.length<3||!ym(p))return null;let h=xo(p),g=xo(e)+xo(n);return Math.abs(h-g)>Math.max(1e-5,g*1e-5)?null:Po(p)>=0?p:[...p].reverse()}function Sm(e){let n=e.map(Ps).filter(r=>r.length>=3&&xo(r)>1e-8),t=n.map(Mo),o=!0;for(;o;){o=!1;for(let r=0;r<n.length;r++){for(let s=r+1;s<n.length;s++){if(!dc(t[r],t[s]))continue;let i=Am(n[r],n[s],t[r],t[s]);if(i){n[r]=i,t[r]=Mo(i),n.splice(s,1),t.splice(s,1),o=!0;break}}if(o)break}}return n}function Cm(e,n,t){let o=new Map;return r=>{let s=rn(r[0]),i=rn(r[1]),c=`${s},${i}`,a=o.get(c);if(a)return[a[0],a[1],a[2]];let l=bs(e,bs(Nt(n,s),Nt(t,i))),u=[hs(l[0]),hs(l[1]),hs(l[2])];return o.set(c,u),[u[0],u[1],u[2]]}}function Em(e,n,t,o,r){let s=t[0]?.a;if(!s)return null;let i=Dt(Fn(r,Nt(o,Bn(r,o))));if(!i)return null;let c=Dt(cc(o,i));if(!c)return null;let a=[],l=[];for(let y of t){let b=ic(y.a,s,i,c),x=ic(y.b,s,i,c);Math.hypot(b[0]-x[0],b[1]-x[1])<=1e-7||(a.push({a:b,b:x}),l.push(b[0],x[0]))}let u=hm(l);if(a.length<3||u.length<2)return null;let f=n[e[0]].color,d=cm(e,n),m=lm(e,n),p=[];for(let y=0;y<u.length-1;y++){let b=u[y],x=u[y+1];if(x-b<=1e-7)continue;let P=(b+x)/2,S=a.filter(C=>{let T=Math.min(C.a[0],C.b[0]),A=Math.max(C.a[0],C.b[0]);return T<P&&P<A&&Math.abs(C.a[0]-C.b[0])>1e-7}).map(C=>({segment:C,yMid:Lt(C,P)})).sort((C,T)=>C.yMid-T.yMid);if(S.length!==0){if(S.length%2!==0)return null;for(let C=0;C<S.length;C+=2){let T=S[C].segment,A=S[C+1].segment,M=Lt(T,b),E=Lt(T,x),v=Lt(A,b),I=Lt(A,x),_=Ps([[b,M],[x,E],[x,I],[b,v]]);if(_.length<3||Math.abs(Po(_))<=1e-8)continue;let O=Po(_)>0?_:[..._].reverse();p.push(O)}}}if(p.length===0||e.length>2&&p.length>e.length*rm)return null;let h=Cm(s,i,c),g=Sm(p).map(y=>({vertices:y.map(h),...f?{color:f}:{},...m?{doubleSided:!0}:{},...d?{data:d}:{}}));return tn(g)}function Tm(e,n,t){let o=fm(e,n);if(!o||o.length<3||!mm(o))return null;let r=fc(n[e[0]]);if(!r)return null;let s=null;for(let i of gm(o,r.normal,t)){let c=Em(e,n,o,r.normal,i);c&&(!s||c.length<s.length)&&(s=c)}return!s||s.length>=e.length?null:s}function Ao(e,n={}){let t=n.minGroupPolygons??Qf,o=n.maxCandidateAxes??Jf,r=n.planeEpsilon??em,s=e??[];if(s.length<t)return s;let{groups:i}=um(s,r);if(i.length===0)return s;let c=new Map,a=new Set;for(let u of i){if(u.length<t)continue;let f=Tm(u,s,o);if(f){c.set(u[0],f);for(let d of u)a.add(d)}}if(c.size===0)return s;let l=[];for(let u=0;u<s.length;u++){let f=c.get(u);if(f){l.push(...f);continue}a.has(u)||l.push(s[u])}return l}function vm(e,n){let t=`${e[0]},${e[1]},${e[2]}`,o=`${n[0]},${n[1]},${n[2]}`;return t<o?`${t}|${o}`:`${o}|${t}`}function Im(e){if(e.length<100)return!1;let n=new Map;for(let o of e){let r=o.vertices;for(let s=0;s<r.length;s++){let i=vm(r[s],r[(s+1)%r.length]);n.set(i,(n.get(i)??0)+1)}}let t=0;for(let o of n.values())o===1&&(t+=1);return t>=128&&t/Math.max(1,n.size)>.2}function _m(e,n,t){let o=Math.abs(e)>.9?0:1,r=Math.abs(e)>.9?1:0,s=r*t,i=-o*t,c=o*n-r*e,a=Math.hypot(s,i,c);return s/=a,i/=a,c/=a,{ux:s,uy:i,uz:c,vx:n*c-t*i,vy:t*s-e*c,vz:e*i-n*s}}function Om(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0;for(let[H,K,Q]of n)t+=H,o+=K,r+=Q;let s=1/n.length;t*=s,o*=s,r*=s;let i=n[0],c=n[1],a=n[2],l=c[0]-i[0],u=c[1]-i[1],f=c[2]-i[2],d=a[0]-i[0],m=a[1]-i[1],p=a[2]-i[2],h=u*p-f*m,g=f*d-l*p,y=l*m-u*d,b=Math.hypot(h,g,y);if(b<1e-9)return null;h/=b,g/=b,y/=b;let{ux:x,uy:P,uz:S,vx:C,vy:T,vz:A}=_m(h,g,y),M=n.length-2,E=new Float64Array(M*9),v=0;for(let H=1;H<n.length-1;H++){let K=n[0],Q=n[H],ne=n[H+1];E[v++]=K[0],E[v++]=K[1],E[v++]=K[2],E[v++]=Q[0],E[v++]=Q[1],E[v++]=Q[2],E[v++]=ne[0],E[v++]=ne[1],E[v++]=ne[2]}let I=0,_=1/0,O=1/0,V=1/0,w=-1/0,F=-1/0,D=-1/0;for(let[H,K,Q]of n){let ne=H-t,ee=K-o,ue=Q-r,U=ne*ne+ee*ee+ue*ue;U>I&&(I=U),H<_&&(_=H),H>w&&(w=H),K<O&&(O=K),K>F&&(F=K),Q<V&&(V=Q),Q>D&&(D=Q)}return{centroid:[t,o,r],normal:[h,g,y],vertices:n,triFlat:E,bcx:t,bcy:o,bcz:r,br2:I,ux:x,uy:P,uz:S,vx:C,vy:T,vz:A,minX:_,minY:O,minZ:V,maxX:w,maxY:F,maxZ:D}}function Vm(e,n,t,o,r,s,i,c){let a=i[c],l=i[c+1],u=i[c+2],f=i[c+3]-a,d=i[c+4]-l,m=i[c+5]-u,p=i[c+6]-a,h=i[c+7]-l,g=i[c+8]-u,y=r*g-s*h,b=s*p-o*g,x=o*h-r*p,P=f*y+d*b+m*x;if(P>-1e-9&&P<1e-9)return!1;let S=1/P,C=e-a,T=n-l,A=t-u,M=S*(C*y+T*b+A*x);if(M<0||M>1)return!1;let E=T*m-A*d,v=A*f-C*m,I=C*d-T*f,_=S*(o*E+r*v+s*I);return _<0||M+_>1?!1:S*(p*E+h*v+g*I)>.001}function Rm(e,n,t,o,r,s,i){let c=i.bcx-e,a=i.bcy-n,l=i.bcz-t,u=c*o+a*r+l*s,f=c-u*o,d=a-u*r,m=l-u*s;if(f*f+d*d+m*m>i.br2)return!1;let p=i.triFlat,h=p.length;for(let g=0;g<h;g+=9)if(Vm(e,n,t,o,r,s,p,g))return!0;return!1}var Ft=9,Lm=6,Ue=8;function Ms(e,n,t,o,r,s){let i=o-e,c=r-n,a=s-t;return i*c+c*a+a*i}function wm(e){let n=[];for(let C=0;C<e.length;C++)e[C]&&n.push(C);let t=n.length,o=new Int32Array(t);for(let C=0;C<t;C++)o[C]=n[C];let r=new Float64Array(t),s=new Float64Array(t),i=new Float64Array(t);for(let C=0;C<t;C++){let T=e[o[C]];r[C]=(T.minX+T.maxX)*.5,s[C]=(T.minY+T.maxY)*.5,i[C]=(T.minZ+T.maxZ)*.5}let c=2*Math.max(1,t)+1,a=new Float64Array(c*Ft),l=0,u=new Float64Array(Ue),f=new Float64Array(Ue),d=new Float64Array(Ue),m=new Float64Array(Ue),p=new Float64Array(Ue),h=new Float64Array(Ue),g=new Int32Array(Ue),y=new Float64Array(Ue-1),b=new Int32Array(Ue-1),x=new Float64Array(Ue-1),P=new Int32Array(Ue-1);function S(C,T){let A=l++,M=A*Ft,E=T-C,v=1/0,I=1/0,_=1/0,O=-1/0,V=-1/0,w=-1/0;for(let G=C;G<T;G++){let te=e[o[G]];te.minX<v&&(v=te.minX),te.maxX>O&&(O=te.maxX),te.minY<I&&(I=te.minY),te.maxY>V&&(V=te.maxY),te.minZ<_&&(_=te.minZ),te.maxZ>w&&(w=te.maxZ)}if(a[M]=v,a[M+1]=I,a[M+2]=_,a[M+3]=O,a[M+4]=V,a[M+5]=w,E<=Lm)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let F=1/0,D=1/0,H=1/0,K=-1/0,Q=-1/0,ne=-1/0;for(let G=C;G<T;G++)r[G]<F&&(F=r[G]),r[G]>K&&(K=r[G]),s[G]<D&&(D=s[G]),s[G]>Q&&(Q=s[G]),i[G]<H&&(H=i[G]),i[G]>ne&&(ne=i[G]);let ee=K-F,ue=Q-D,U=ne-H;if(ee===0&&ue===0&&U===0)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let $=Ms(v,I,_,O,V,w),k=$>0?1/$:0,L=E+1,W=0,Y=0;for(let G=0;G<3;G++){let te=G===0?F:G===1?D:H,ce=G===0?ee:G===1?ue:U;if(ce===0)continue;let ye=G===0?r:G===1?s:i,he=Ue/ce;u.fill(1/0),f.fill(1/0),d.fill(1/0),m.fill(-1/0),p.fill(-1/0),h.fill(-1/0),g.fill(0);for(let N=C;N<T;N++){let X=(ye[N]-te)*he|0;X>=Ue&&(X=Ue-1);let oe=e[o[N]];oe.minX<u[X]&&(u[X]=oe.minX),oe.maxX>m[X]&&(m[X]=oe.maxX),oe.minY<f[X]&&(f[X]=oe.minY),oe.maxY>p[X]&&(p[X]=oe.maxY),oe.minZ<d[X]&&(d[X]=oe.minZ),oe.maxZ>h[X]&&(h[X]=oe.maxZ),g[X]++}let be=1/0,ge=1/0,Se=1/0,De=-1/0,R=-1/0,z=-1/0,j=0;for(let N=0;N<Ue-1;N++)u[N]<be&&(be=u[N]),m[N]>De&&(De=m[N]),f[N]<ge&&(ge=f[N]),p[N]>R&&(R=p[N]),d[N]<Se&&(Se=d[N]),h[N]>z&&(z=h[N]),j+=g[N],y[N]=Ms(be,ge,Se,De,R,z),b[N]=j;let J=1/0,le=1/0,re=1/0,pe=-1/0,xe=-1/0,ie=-1/0,ae=0;for(let N=Ue-2;N>=0;N--){let X=N+1;u[X]<J&&(J=u[X]),m[X]>pe&&(pe=m[X]),f[X]<le&&(le=f[X]),p[X]>xe&&(xe=p[X]),d[X]<re&&(re=d[X]),h[X]>ie&&(ie=h[X]),ae+=g[X],x[N]=Ms(J,le,re,pe,xe,ie),P[N]=ae}for(let N=0;N<Ue-1;N++){if(b[N]===0||P[N]===0)continue;let X=.125+(y[N]*b[N]+x[N]*P[N])*k;X<L&&(L=X,W=G,Y=te+(N+1)/he)}}let se=W===0?r:W===1?s:i,Z=C,B=T-1;for(;Z<=B;)if(se[Z]<Y)Z++;else{let G=o[Z];o[Z]=o[B],o[B]=G;let te=r[Z];r[Z]=r[B],r[B]=te;let ce=s[Z];s[Z]=s[B],s[B]=ce;let ye=i[Z];i[Z]=i[B],i[B]=ye,B--}let q=Z;(q===C||q===T)&&(q=C+T>>1),a[M+6]=0;let me=S(C,q),fe=S(q,T);return a[A*Ft+7]=me,a[A*Ft+8]=fe,A}return t>0&&S(0,t),{data:a,nodeCount:l,polyIndices:o,meta:e}}function As(e,n,t,o,r,s,i,c,a){if(c.nodeCount===0)return!1;let{data:l,polyIndices:u,meta:f}=c,d=o!==0?1/o:o>=0?1/0:-1/0,m=r!==0?1/r:r>=0?1/0:-1/0,p=s!==0?1/s:s>=0?1/0:-1/0,h=0;for(a[h++]=0;h>0;){let y=a[--h]*Ft,b=(l[y]-e)*d,x=(l[y+3]-e)*d,P=b<x?b:x,S=b<x?x:b,C=(l[y+1]-n)*m,T=(l[y+4]-n)*m,A=C<T?C:T,M=C<T?T:C;if(P>M||A>S)continue;A>P&&(P=A),M<S&&(S=M);let E=(l[y+2]-t)*p,v=(l[y+5]-t)*p,I=E<v?E:v,_=E<v?v:E;if(!(P>_||I>S)&&(_<S&&(S=_),!(S<.001)))if(l[y+6]===1){let O=l[y+7]|0,V=l[y+8]|0;for(let w=O;w<V;w++){let F=u[w];if(F===i)continue;let D=f[F];if(D&&Rm(e,n,t,o,r,s,D))return!0}}else a[h++]=l[y+7]|0,a[h++]=l[y+8]|0}return!1}function Nm(e){let n=pc.get(e);if(n)return n;let t=(1+Math.sqrt(5))/2,o=new Float64Array(e*3);for(let r=0;r<e;r++){let s=(r+.5)/e,i=Math.sqrt(Math.max(0,1-s*s)),c=2*Math.PI*(r/t);o[r*3]=i*Math.cos(c),o[r*3+1]=i*Math.sin(c),o[r*3+2]=s}return pc.set(e,o),o}var pc=new Map;function un(e,n){let t=n?.samples??8;if(e.length<4||t<1||!n?.force&&Im(e))return e;let o=Math.max(1,Math.ceil(t*(n?.minEscapeRatio??1/t))),r=e.map(Om),s=Nm(t),i=[],c=wm(r),a=new Int32Array(Math.max(64,c.nodeCount)),l=192,u=new Float64Array(l);for(let f=0;f<e.length;f++){let d=r[f];if(!d){i.push(e[f]);continue}let m=d.normal[0],p=d.normal[1],h=d.normal[2],g=.001*m,y=.001*p,b=.001*h;{let V=d.centroid[0]+g,w=d.centroid[1]+y,F=d.centroid[2]+b;if(!As(V,w,F,m,p,h,f,c,a)){i.push(e[f]);continue}}let{ux:x,uy:P,uz:S,vx:C,vy:T,vz:A}=d,M=d.centroid[0],E=d.centroid[1],v=d.centroid[2],I=d.vertices,_=I.length,O=0;u[O++]=M+g,u[O++]=E+y,u[O++]=v+b;for(let V=0;V<_;V++){let w=I[V];u[O++]=w[0]+(M-w[0])*.08+g,u[O++]=w[1]+(E-w[1])*.08+y,u[O++]=w[2]+(v-w[2])*.08+b}for(let V=0;V<_;V++){let w=I[V],F=I[(V+1)%_],D=(w[0]+F[0])*.5,H=(w[1]+F[1])*.5,K=(w[2]+F[2])*.5;u[O++]=D+(M-D)*.08+g,u[O++]=H+(E-H)*.08+y,u[O++]=K+(v-K)*.08+b}if(o<=1){let V=!1;e:for(let w=0;w<s.length;w+=3){let F=s[w],D=s[w+1],H=s[w+2],K=F*x+D*C+H*m,Q=F*P+D*T+H*p,ne=F*S+D*A+H*h;for(let ee=0;ee<O;ee+=3){let ue=u[ee],U=u[ee+1],$=u[ee+2];if(!As(ue,U,$,K,Q,ne,f,c,a)){V=!0;break e}}}V&&i.push(e[f])}else{let V=0;for(let w=0;w<s.length;w+=3){let F=s[w],D=s[w+1],H=s[w+2],K=F*x+D*C+H*m,Q=F*P+D*T+H*p,ne=F*S+D*A+H*h;for(let ee=0;ee<O;ee+=3){let ue=u[ee],U=u[ee+1],$=u[ee+2];if(!As(ue,U,$,K,Q,ne,f,c,a)){V++;break}}}V>=o&&i.push(e[f])}}return i}var vs={overlapPx:1.25,maxGapPx:14,capacityScale:1},Is={budget:40};function kn(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function ut(e){return typeof e=="number"?{overlapPx:Math.max(0,e)*.175,maxGapPx:14,capacityScale:1}:{overlapPx:Math.max(0,kn(e?.overlapPx,1.25)),maxGapPx:Math.max(0,kn(e?.maxGapPx,14)),capacityScale:Math.max(0,kn(e?.capacityScale,1))}}function ft(e,n){return!(typeof n=="number"&&n<=0)&&e.maxGapPx>1e-6&&e.capacityScale>1e-6}function Mc(e){return{rotX:kn(e?.rotX,65),rotY:kn(e?.rotY,45),viewAware:e?.viewAware!==!1,passes:Math.max(1,Math.min(3,Math.round(kn(e?.passes,2)))),budget:Math.max(0,Math.min(256,Math.round(kn(e?.budget,40))))}}function Pn(e){return e.map(n=>[n[1]*50,n[0]*50,n[2]*50])}function Ac(e){return[e[1]/50,e[0]/50,e[2]/50]}function Io(e){return`${e[0]},${e[1]},${e[2]}`}function Dm(e,n){let t=Io(e),o=Io(n);return t<o?`${t}|${o}`:`${o}|${t}`}function Mn(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function mt(e){return Math.hypot(e[0],e[1],e[2])}function Gt(e){let n=mt(e);return n>1e-6?[e[0]/n,e[1]/n,e[2]/n]:null}function Ut(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function So(e,n,t,o){let r=n*Math.PI/180,s=t*Math.PI/180,i=o*Math.PI/180,[c,a,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[c,a]=[c*u-a*f,c*f+a*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[c,l]=[c*u+l*f,-c*f+l*u]}if(r!==0){let u=Math.cos(r),f=Math.sin(r);[a,l]=[a*u-l*f,a*f+l*u]}return[c,a,l]}function _s(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 Fm(e){if(e.length<3)return null;let n=e[0],t=[0,0,0];for(let o=1;o+1<e.length;o+=1){let r=Mn(e[o],n),s=Mn(e[o+1],n),i=_s(r,s);t[0]-=i[0],t[1]-=i[1],t[2]-=i[2]}return Gt(t)}function Os(e){let n=e[0],t=Fm(e);if(!t)return null;let o=null;for(let u=0;u<e.length;u+=1){let f=Mn(e[(u+1)%e.length],e[u]);if(mt(f)>1e-6){o=f;break}}if(!o)return null;let r=Ut(o,t),s=[o[0]-r*t[0],o[1]-r*t[1],o[2]-r*t[2]],i=Gt(s);if(!i)return null;let c=Gt(_s(t,i));if(!c)return null;let a=e.map(u=>{let f=Mn(u,n);return[Ut(f,i),Ut(f,c)]}),l=_o(a);return Math.abs(l)>1e-6?{origin:n,normal:t,xAxis:i,yAxis:c,local:a,area:l}:null}function _o(e){let n=0;for(let t=0;t<e.length;t+=1){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-o[1]*r[0]}return n/2}function Vs(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t+=1){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=1e-6)continue;let c=Math.sign(i);if(n===0)n=c;else if(c!==n)return!1}return!0}function Sc(e,n,t){let o=e[t],r=e[(t+1)%e.length],s=r[0]-o[0],i=r[1]-o[1],c=Math.hypot(s,i);if(c<=1e-6)return null;let a=n>0?1:-1;return[a*(i/c),a*(-s/c)]}function Bm(e,n){let t=Sc(e.local,e.area,n);return t?Gt([e.xAxis[0]*t[0]+e.yAxis[0]*t[1],e.xAxis[1]*t[0]+e.yAxis[1]*t[1],e.xAxis[2]*t[0]+e.yAxis[2]*t[1]]):null}function Cc(e,n){let t=e.length,o=e[n],r=e[(n+1)%t],s=r[0]-o[0],i=r[1]-o[1],c=Math.hypot(s,i);if(c<=1e-6)return 0;let a=[c*.24],l=1/0;for(let f=0;f<t;f+=1){if(f===n||f===(n+1)%t)continue;let d=e[f],m=Math.abs((d[0]-o[0])*i-(d[1]-o[1])*s)/c;m>1e-6&&(l=Math.min(l,m))}Number.isFinite(l)&&a.push(l*.32);let u=[{origin:o,point:e[(n+t-1)%t]},{origin:r,point:e[(n+2)%t]}];for(let f of u){let d=f.point[0]-f.origin[0],m=f.point[1]-f.origin[1],p=Math.hypot(d,m);if(p<=1e-6)continue;let h=Math.abs(s*m-i*d)/(c*p);h>1e-6&&a.push(p*h*.32)}return Math.min(...a)}function Ec(e){return e.map(n=>{let t=Pn(n.vertices),o=Os(t),r=!Rs(n)&&!!o&&Vs(o.local),s=r?o.local.map((i,c)=>Cc(o.local,c)):[];return{basis:o,cssPoints:t,capacities:s,patchable:r}})}function Rs(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function km(e){return e.material?.key??e.color??""}function Tc(e,n,t){let o=[];for(let r=0;r<n.length;r+=1){let s=n[r];if(!(!s.patchable||!s.basis))for(let i=0;i<s.cssPoints.length;i+=1){let c=s.cssPoints[i],a=s.cssPoints[(i+1)%s.cssPoints.length],l=Mn(a,c),u=mt(l),f=Gt(l),d=Bm(s.basis,i),m=(s.capacities[i]??0)*t;!f||!d||m<=1e-6||u<=1e-6||o.push({index:o.length,polygon:r,edge:i,key:Dm(c,a),a:c,b:a,minX:Math.min(c[0],a[0]),minY:Math.min(c[1],a[1]),minZ:Math.min(c[2],a[2]),maxX:Math.max(c[0],a[0]),maxY:Math.max(c[1],a[1]),maxZ:Math.max(c[2],a[2]),length:u,dir:f,normal:s.basis.normal,outward:d,capacity:m,materialKey:km(e[r]),color:e[r].color})}}return o}function Ls(e,n){return e.materialKey===n.materialKey&&e.color===n.color}function Co(e,n){return mt(Mn(e,n))<=1e-4}function Um(e,n){return Co(e.a,n.a)||Co(e.a,n.b)||Co(e.b,n.a)||Co(e.b,n.b)}function Gm(e,n){return Ls(e,n)?Um(e,n)?"connected-facet":"true-gap":"material-boundary"}function Ss(e,n,t,o,r,s){return{kind:e,aPolygon:n.polygon,aEdge:n.edge,bPolygon:t.polygon,bEdge:t.edge,aColor:n.color,bColor:t.color,aMaterialKey:n.materialKey,bMaterialKey:t.materialKey,gapPx:o.gap,spanPx:o.aEnd-o.aStart,aStartPx:o.aStart,aEndPx:o.aEnd,bStartPx:o.bStart,bEndPx:o.bEnd,targetClosurePx:r,appliedClosurePx:s,residualGapPx:Math.max(0,o.gap-s),residualTargetPx:Math.max(0,r-s)}}function gc(e,n,t,o,r){let s=Math.max(0,Math.min(n.length,Math.min(t,o))),i=Math.max(0,Math.min(n.length,Math.max(t,o)));if(r<=1e-6||i-s<=1e-6)return;let c=e[n.polygon].get(n.edge),a={start:s,end:i,amount:r};c?c.push(a):e[n.polygon].set(n.edge,[a])}function Oo(){return{exactPairs:0,nearPairs:0,patchedPolygons:0,patchedEdges:0,maxMeasuredGapPx:0,maxAppliedAmountPx:0,unclosedPairs:0,maxResidualGapPx:0}}function zm(e,n){let t=0,o=0,r=0;for(let s of e){s.size>0&&(r+=1),o+=s.size;for(let i of s.values())for(let c of i)t=Math.max(t,c.amount)}return{...n,patchedPolygons:r,patchedEdges:o,maxAppliedAmountPx:t}}function Vo(e,n,t=!1,o=!1){let r=Ec(e),s=Tc(e,r,n.capacityScale),i=o?[]:e.map(()=>new Map),c=Oo(),a=t?[]:void 0;if(s.length===0)return{edgeRepairs:i,diagnostics:c,candidates:a};let l=new Map;for(let u of s){let f=l.get(u.key);f?f.push(u):l.set(u.key,[u])}for(let u of l.values())u.length<2||(c.exactPairs+=u.length===2?1:u.length);return $m(s,l,i,c,n,a),{edgeRepairs:i,diagnostics:o?c:zm(i,c),candidates:a}}function $m(e,n,t,o,r,s){let i=r.maxGapPx,c=new Set;for(let[m,p]of n)p.length>1&&c.add(m);let a=(m,p)=>{let h=m.index<p.index?m:p,g=m.index<p.index?p:m;if(g.polygon===h.polygon||h.key===g.key&&c.has(h.key)||!s&&!Ls(h,g)||!Xm(h,g,i)||Math.abs(Ut(h.dir,g.dir))<.985)return;let y=Km(h,g,i);if(!y)return;let b=Gm(h,g),x=y.gap+r.overlapPx;if(b==="material-boundary"){s?.push(Ss(b,h,g,y,x,0));return}let P=x;if(P<=.25){s?.push(Ss(b,h,g,y,x,0));return}let S=h.capacity*y.facingA,C=g.capacity*y.facingB,T=Math.min(S,P/2),A=Math.min(C,P/2);if(P-=T+A,P>1e-6){let M=Math.min(S-T,P);T+=M,P-=M}if(P>1e-6){let M=Math.min(C-A,P);A+=M,P-=M}s?.push(Ss(b,h,g,y,x,T+A)),t.length>0&&T>1e-6&&gc(t,h,y.aStart,y.aEnd,T/y.facingA),t.length>0&&A>1e-6&&gc(t,g,y.bStart,y.bEnd,A/y.facingB),o.nearPairs+=1,o.maxMeasuredGapPx=Math.max(o.maxMeasuredGapPx,y.gap),P>.25&&(o.unclosedPairs+=1,o.maxResidualGapPx=Math.max(o.maxResidualGapPx,P))},l=Math.max(28,i*2);if(e.length<=1e4){let m=[...e].sort((g,y)=>g.minX-y.minX),p=new Int32Array(m.length),h=0;for(let g=0;g+1<m.length;g+=1){let b=m[g].maxX+i,x=g+1;for(;x<m.length&&m[x].minX<=b;)x+=1;if(p[g]=x,h+=x-g-1,h>25e6)break}if(h<=2e6||h<=25e6&&jm(e,l,i)){for(let g=0;g+1<m.length;g+=1)for(let y=g+1;y<p[g];y+=1)a(m[g],m[y]);return}}let u=new Map;for(let m of e)Ym(u,m,l,i);let f=new Set,d=e.length;for(let m of u.values())for(let p=0;p+1<m.length;p+=1)for(let h=p+1;h<m.length;h+=1){let g=m[p],y=m[h],b=g.index<y.index?g:y,x=g.index<y.index?y:g,P=b.index*d+x.index;f.has(P)||(f.add(P),a(b,x))}}function Xm(e,n,t){return e.minX<=n.maxX+t&&n.minX<=e.maxX+t&&e.minY<=n.maxY+t&&n.minY<=e.maxY+t&&e.minZ<=n.maxZ+t&&n.minZ<=e.maxZ+t}function hc(e,n){return[Math.floor(e[0]/n),Math.floor(e[1]/n),Math.floor(e[2]/n)]}function Ym(e,n,t,o){let[r,s,i,c,a,l]=vc(n,t,o);for(let u=r;u<=c;u+=1)for(let f=s;f<=a;f+=1)for(let d=i;d<=l;d+=1){let m=`${u},${f},${d}`,p=e.get(m);p?p.push(n):e.set(m,[n])}}function vc(e,n,t){let[o,r,s]=hc([e.minX-t,e.minY-t,e.minZ-t],n),[i,c,a]=hc([e.maxX+t,e.maxY+t,e.maxZ+t],n);return[o,r,s,i,c,a]}function Wm(e,n,t){let[o,r,s,i,c,a]=vc(e,n,t);return(i-o+1)*(c-r+1)*(a-s+1)}function jm(e,n,t){let o=0,r=0;for(let s of e){let i=Wm(s,n,t);if(o+=i,i>r&&(r=i),r>8192)return!0}return o/Math.max(1,e.length)>256}function yc(e,n,t){return(e[0]-n[0])*t[0]+(e[1]-n[1])*t[1]+(e[2]-n[2])*t[2]}function bc(e,n,t){return(e.a[0]+e.dir[0]*n-t.a[0])*t.dir[0]+(e.a[1]+e.dir[1]*n-t.a[1])*t.dir[1]+(e.a[2]+e.dir[2]*n-t.a[2])*t.dir[2]}function Km(e,n,t){let o=yc(n.a,e.a,e.dir),r=yc(n.b,e.a,e.dir),s=Math.max(0,Math.min(o,r)),i=Math.min(e.length,Math.max(o,r)),c=i-s,a=Math.min(e.length,n.length);if(c<Math.max(.75,a*.12))return null;let l=(s+i)/2,u=e.a[0]+e.dir[0]*l,f=e.a[1]+e.dir[1]*l,d=e.a[2]+e.dir[2]*l,m=Math.max(0,Math.min(n.length,(u-n.a[0])*n.dir[0]+(f-n.a[1])*n.dir[1]+(d-n.a[2])*n.dir[2])),p=n.a[0]+n.dir[0]*m-u,h=n.a[1]+n.dir[1]*m-f,g=n.a[2]+n.dir[2]*m-d,y=Math.hypot(p,h,g);if(y<=1e-6||y>t||y>Math.min(t,Math.max(4,a*.28)))return null;let b=1/y,x=p*b,P=h*b,S=g*b,C=e.outward[0]*x+e.outward[1]*P+e.outward[2]*S,T=-(n.outward[0]*x+n.outward[1]*P+n.outward[2]*S);if(C<.5||T<.5)return null;let A=Math.max(0,Math.min(n.length,bc(e,s,n))),M=Math.max(0,Math.min(n.length,bc(e,i,n)));return{gap:y,facingA:C,facingB:T,aStart:s,aEnd:i,bStart:Math.min(A,M),bEnd:Math.max(A,M)}}function Cs(e,n,t,o){let r=t<=1e-6?0:Math.max(0,Math.min(1,o/t));return[e[0]+(n[0]-e[0])*r,e[1]+(n[1]-e[1])*r]}function Bt(e,n){let t=e[e.length-1];t&&Math.hypot(t[0]-n[0],t[1]-n[1])<=1e-6||e.push(n)}function Hm(e,n,t){if(!e?.length||n<=1e-6||t<=1e-6)return[];let o=new Set([0,n]);for(let i of e){let c=Math.max(0,Math.min(n,i.start)),a=Math.max(0,Math.min(n,i.end));a-c<=1e-6||i.amount<=1e-6||(o.add(c),o.add(a))}let r=Array.from(o).sort((i,c)=>i-c),s=[];for(let i=0;i+1<r.length;i+=1){let c=r[i],a=r[i+1];if(a-c<=1e-6)continue;let l=(c+a)/2,u=0;for(let d of e)l+1e-6<d.start||l-1e-6>d.end||(u=Math.max(u,d.amount));let f=Math.min(t,u);f>1e-6&&s.push({start:c,end:a,amount:f})}return s}function Zm(e,n){if(n.length<3||!Vs(n))return!1;let t=_o(e),o=_o(n);return Math.sign(o)===Math.sign(t)&&Math.abs(o)>=Math.abs(t)-1e-6}function qm(e,n,t){if(n.size===0||e.vertices.length<3||Rs(e))return e;let o=Pn(e.vertices),r=Os(o);if(!r||!Vs(r.local))return e;let s=[];for(let l=0;l<r.local.length;l+=1){let u=r.local[l],f=r.local[(l+1)%r.local.length],d=Math.hypot(f[0]-u[0],f[1]-u[1]),m=Sc(r.local,r.area,l);if(!m||d<=1e-6)return e;let p=Hm(n.get(l),d,Cc(r.local,l)*t);Bt(s,u);let h=0;for(let g of p){g.start>h+1e-6&&Bt(s,Cs(u,f,d,g.start));let y=Cs(u,f,d,g.start),b=Cs(u,f,d,g.end);Bt(s,[y[0]+m[0]*g.amount,y[1]+m[1]*g.amount]),Bt(s,[b[0]+m[0]*g.amount,b[1]+m[1]*g.amount]),g.end<d-1e-6&&Bt(s,b),h=g.end}}let i=s[0],c=s[s.length-1];if(i&&c&&Math.hypot(i[0]-c[0],i[1]-c[1])<=1e-6&&s.pop(),!Zm(r.local,s))return e;let a=s.map(([l,u])=>Ac([r.origin[0]+l*r.xAxis[0]+u*r.yAxis[0],r.origin[1]+l*r.xAxis[1]+u*r.yAxis[1],r.origin[2]+l*r.xAxis[2]+u*r.yAxis[2]]));return{...e,vertices:a}}function Ic(e){return e.vertices.length===4&&!Rs(e)}function Qm(e){let n=e.basis?.local;if(!n||n.length!==4)return 1;let t=1/0,o=1/0,r=-1/0,s=-1/0;for(let[a,l]of n)t=Math.min(t,a),o=Math.min(o,l),r=Math.max(r,a),s=Math.max(s,l);let i=Math.max(1e-6,(r-t)*(s-o));return .75+(1-Math.max(0,Math.min(1,Math.abs(e.basis.area)/i)))*2.5}function Es(e,n){return mt(Mn(Pn([e])[0],Pn([n])[0]))}function Eo(e,n,t){let o=Es(e,n),r=Es(n,t),s=Es(t,e),i=Math.max(o,r,s);return i<=1e-6?0:mt(_s(Mn(Pn([n])[0],Pn([e])[0]),Mn(Pn([t])[0],Pn([e])[0])))*.5/(i*i)}function Jm(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],c=o[1]-t[1],a=r*c-s*i;if(Math.abs(a)<=1e-6)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*c-u*i)/a;return[e[0]+f*r,e[1]+f*s]}function ed(e,n){if(e.length<3||e.length!==n.length)return null;let t=Math.max(0,...n);if(t<=1e-6)return e;let o=_o(e);if(Math.abs(o)<=1e-6)return null;let r=o>0?1:-1,s=[];for(let a=0;a<e.length;a+=1){let l=e[a],u=e[(a+1)%e.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=1e-6)return null;let p=Math.max(0,n[a]),h=r*(d/m)*p,g=r*(-f/m)*p;s.push({a:[l[0]+h,l[1]+g],b:[u[0]+h,u[1]+g]})}let i=[],c=Math.max(2,t*4);for(let a=0;a<e.length;a+=1){let l=s[(a+e.length-1)%e.length],u=s[a],f=Jm(l.a,l.b,u.a,u.b);if(!f)return null;let d=e[a],m=f[0]-d[0],p=f[1]-d[1],h=Math.hypot(m,p);i.push(h>c?[d[0]+m/h*c,d[1]+p/h*c]:f)}return i}function nd(e,n){return[e.origin[0]+e.xAxis[0]*n[0]+e.yAxis[0]*n[1],e.origin[1]+e.xAxis[1]*n[0]+e.yAxis[1]*n[1],e.origin[2]+e.xAxis[2]*n[0]+e.yAxis[2]*n[1]]}function To(e,n){let t=Os(Pn(e));if(!t)return e;let o=ed(t.local,n);return o?o.map(r=>Ac(nd(t,r))):e}function td(e,n){let t={...e,uvs:void 0,textureTriangles:void 0},[o,r,s,i]=e.vertices,c=Math.min(Eo(o,r,s),Eo(o,s,i)),a=Math.min(Eo(o,r,i),Eo(r,s,i)),l=u=>n.has(u)?.75:0;return c>=a?[{...t,vertices:To([o,r,s],[l(0),l(1),1.25])},{...t,vertices:To([o,s,i],[1.25,l(2),l(3)])}]:[{...t,vertices:To([o,r,i],[l(0),1.25,l(3)])},{...t,vertices:To([r,s,i],[l(1),l(2),1.25])}]}function xc(e){if(e.length===0)return{q1:1/0,median:1/0,q3:1/0,p70:1/0,p80:1/0,p88:1/0,p95:1/0,p97:1/0,max:1/0};let n=[...e].sort((o,r)=>o-r),t=o=>n[Math.min(n.length-1,Math.floor((n.length-1)*o))]??0;return{q1:t(.25),median:t(.5),q3:t(.75),p70:t(.7),p80:t(.8),p88:t(.88),p95:t(.95),p97:t(.97),max:n[n.length-1]??0}}function vo(e,n){let t=e[n];if(t===n)return n;let o=vo(e,t);return e[n]=o,o}function od(e){let n=new Set,t=new Set;for(let o of e)o.sel&&t.add(o.comp);for(let o of e)if(t.has(o.comp))for(let r of o.e)n.add(r);return{endpoints:n}}function rd(e,n){return n?e.e.some(t=>n.endpoints.has(t)):!0}function sd(e,n,t,o){let r=[];for(let[a,l]of t){if(l.length!==2)continue;let[u,f]=l;if(!Ls(u,f))continue;let d=l.filter(C=>Ic(e[C.polygon]));if(d.length===0)continue;let m=d[0],p=1-Math.min(1,Math.abs(Ut(u.normal,f.normal))),h=Math.max(u.length,f.length);if(p<.01&&h<64)continue;let g=Math.max(...d.map(C=>Qm(n[C.polygon]))),y=d.length===2?1.18:1,b=Math.max(.08,p),x=o.viewAware?id(u,f,o.rotX,o.rotY):{proj:h,vr:1},P=o.viewAware?Math.max(x.proj,h*.12):h,S=o.viewAware?.18+x.vr*1.82:1;r.push({key:a,e:[Io(m.a),Io(m.b)],o:l,r:d,len:h,rank:P,proj:x.proj,s:P*(.58+b*2.8)*g*y*S,nr:p,sr:g,vr:x.vr,comp:-1,sel:!1,why:"below-threshold",cost:d.length})}let s=Array.from({length:r.length},(a,l)=>l),i=new Map;for(let a=0;a<r.length;a+=1)for(let l of r[a].e){let u=i.get(l);if(u===void 0)i.set(l,a);else{let f=vo(s,u),d=vo(s,a);f!==d&&(s[d]=f)}}let c=new Map;for(let a=0;a<r.length;a+=1){let l=vo(s,a),u=c.get(l);u===void 0&&(u=c.size,c.set(l,u)),r[a].comp=u}return r}function id(e,n,t,o){let r=So(e.a,t,0,o),s=So(e.b,t,0,o),i=Math.hypot(s[0]-r[0],s[1]-r[1]),c=So(e.normal,t,0,o)[2],a=So(n.normal,t,0,o)[2],l=Math.max(0,c,a),u=Math.min(1,Math.abs(c-a));return{proj:i,vr:Math.max(.04,Math.min(1,l*.85+u*.45))}}function ad(e){let n=new Map;for(let t of e){let o=n.get(t.comp);(!o||t.s>o.s||t.s===o.s&&t.len>o.len)&&n.set(t.comp,t)}return new Set(n.values())}function kt(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+n*n*n*3.65,o=e.r.length===1?1.08:1;return e.rank*t*o/Math.sqrt(Math.max(1,e.r.length))}function Pc(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+Math.min(1,e.vr)*1.65,o=.7+Math.min(2.6,e.sr)*.3,r=e.r.length===1?1.1:1,s=e.rank*(.6+n*n*1.4)*t*o*r/Math.sqrt(Math.max(1,e.r.length));return Math.max(s,kt(e)*.75)}function Ts(e){let n=new Set;for(let t of e)for(let o of t.r)n.add(o.polygon);return n.size}function _c(e,n){return e.nr<=.25&&e.rank>=n.median*1.08&&e.proj>=24}function cd(e,n,t){return e.s>=n||e.rank>=t}function ld(e,n,t,o,r,s,i){let c=Math.max(0,Math.floor(t));if(c<=0||e.length===0||n==="follow-up")return{budgetLimit:c,componentBudgets:null};let a=new Map;for(let m of e){let p=a.get(m.comp);p?p.push(m):a.set(m.comp,[m])}let l=new Map,u=Math.max(1e-6,o.q3-o.q1),f=Math.min(o.max,Math.max(o.p95,o.q3+u)),d=0;for(let[m,p]of a){let h=p.filter(S=>cd(S,s,i)||S.s>=f);if(h.length===0)continue;let g=p.filter(S=>_c(S,r)),y=Ts(h),b=Ts(g),x=Math.ceil(Math.sqrt(Math.max(0,b-y))*3),P=Math.min(Ts(p),y+x);P<=0||(l.set(m,P),d+=P)}return l.size===0?{budgetLimit:0,componentBudgets:l}:{budgetLimit:Math.min(c,d),componentBudgets:l}}function ud(e,n,t){let o=new Map;if(e.length===0||t<=0)return{selected:o,budgetLimit:0};let r=xc(e.map(M=>M.s)),s=xc(e.map(M=>M.rank)),i=Math.max(1e-6,r.q3-r.q1),c=Math.min(r.max,Math.max(r.p95,r.q3+i*.85)),a=Math.min(r.max,Math.max(r.p80,r.q3+i*.15)),l=Math.min(r.max,Math.max(r.p70,r.q3)),u=Math.max(s.p97,s.median*1.8),f=Math.max(s.p88,s.median*1.35),d=ld(e,n,t,r,s,c,u),m=d.budgetLimit;if(m<=0)return{selected:o,budgetLimit:0};let p=ad(e),h=new Set,g=new Set,y=new Set,b=new Map,x=n==="primary"?Math.min(Math.max(0,d.budgetLimit-1),Math.max(2,Math.floor(d.budgetLimit*.28))):0,P=[...e].sort((M,E)=>kt(E)-kt(M)||E.s/Math.max(1,E.r.length)-M.s/Math.max(1,M.r.length)||E.len-M.len),S=[...e].sort((M,E)=>Pc(E)-Pc(M)||kt(E)-kt(M)||E.len-M.len),C=(M,E)=>{M.sel=!0,M.why=E,y.add(M.comp);for(let v of M.e)g.add(v);for(let v of M.r){h.add(v.polygon);let I=o.get(v.polygon);I?I.add(v.edge):o.set(v.polygon,new Set([v.edge]))}},T=(M,E)=>{let v=d.componentBudgets;if(E<=0||!v)return!0;let I=v.get(M.comp)??0;return(b.get(M.comp)??0)+E<=I},A=(M,E)=>{E<=0||!d.componentBudgets||b.set(M.comp,(b.get(M.comp)??0)+E)};for(let M of S){let E=M.r.reduce((I,_)=>I+(h.has(_.polygon)?0:1),0);if(M.cost=E,!(E>m||!T(M,E)||E>0&&m-E<x||!_c(M,s))&&(C(M,"global-outlier"),A(M,E),m-=E,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}for(let M of P){if(M.sel)continue;let E=M.r.reduce((V,w)=>V+(h.has(w.polygon)?0:1),0);if(M.cost=E,E>m||!T(M,E)||E>0&&m-E<x)continue;let v=M.r.length===2,I=p.has(M)&&(n==="primary"||n==="follow-up"&&M.s>=a*1.08&&M.rank>=f)&&(v?M.s>=a||M.rank>=f:M.s>=a*1.18||M.rank>=f*1.12),_=v?M.s>=c||M.rank>=u:M.s>=c*1.35||M.rank>=u*1.2,O=E===0&&(v?M.s>=l||M.rank>=f:M.s>=l*1.25||M.rank>=f*1.12);if(!(!I&&!_&&!O)&&(C(M,I?"component-anchor":_?"global-outlier":"shared-polygon"),A(M,E),m-=E,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}for(let M of P){if(M.sel)continue;let E=M.r.reduce((w,F)=>w+(h.has(F.polygon)?0:1),0);if(M.cost=E,E>1||E>m||!T(M,E))continue;let v=M.r.some(w=>h.has(w.polygon)),I=M.e.some(w=>g.has(w));if(n==="primary"&&M.r.length===1&&E===1&&(y.has(M.comp)||I||v)&&M.rank>=f*1.05&&M.nr>=.16&&M.vr<=.32){if(C(M,"local-follow-up"),A(M,E),m-=E,m<=0)return{selected:o,budgetLimit:d.budgetLimit};continue}if(M.r.length!==2)continue;let O=v&&E===1&&M.proj>=f*1.45&&M.vr<=.18;if(!I&&!O)continue;if((M.s>=l*.42||M.rank>=f*.62)&&M.s<=l*1.1&&M.proj>=24&&(C(M,"local-follow-up"),A(M,E),m-=E,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}return{selected:o,budgetLimit:d.budgetLimit}}function Oc(e,n,t,o,r){let s=Ec(e),i=Tc(e,s,n.capacityScale),c=new Map;for(let u of i){let f=c.get(u.key);f?f.push(u):c.set(u.key,[u])}let a=sd(e,s,c,t).filter(u=>rd(u,r)),l=ud(a,r?"follow-up":"primary",o);return{selected:l.selected,candidates:a,budgetLimit:l.budgetLimit}}function fd(e,n){let t=[];for(let o=0;o<e.length;o+=1){let r=e[o],s=n.get(o);s&&Ic(r)?t.push(...td(r,s)):t.push(r)}return t}function md(e){let[n,t]=e.o,o=n??t,r=t??n;return{key:e.key,aPolygon:o?.polygon??-1,aEdge:o?.edge??-1,bPolygon:r?.polygon??-1,bEdge:r?.edge??-1,color:o?.color??r?.color,materialKey:o?.materialKey??r?.materialKey??"",lengthPx:e.len,projectedLengthPx:e.proj,score:e.s,normalRisk:e.nr,shapeRisk:e.sr,viewRisk:e.vr,component:e.comp,marginalCost:e.cost,selected:e.sel,reason:e.why}}function ws(e,n,t){let o=ut(n),r=Mc(t);if(!ft(o,n)||e.length===0)return e;let s=e,i,c=r.budget;for(let a=0;a<r.passes;a+=1){let l=Oc(s,o,r,c,i);if(a===0&&(c=Math.min(c,l.budgetLimit)),l.selected.size===0)break;let u=fd(s,l.selected);if(u.length===s.length)break;if(c-=l.selected.size,c<=0){s=u;break}i=od(l.candidates),s=u}return s}function Vc(e,n,t){let o=ut(n),r=Mc(t);if(!ft(o,n)||e.length===0)return{candidates:[],selectedPolygons:0,selectedEdges:0,addedPolygons:0};let s=Oc(e,o,r,r.budget),i=0;for(let c of s.selected.values())i+=c.size;return{candidates:s.candidates.map(md),selectedPolygons:s.selected.size,selectedEdges:i,addedPolygons:s.selected.size}}function Ns(e,n){let t=ut(n);if(!ft(t,n)||e.length===0)return e;let{edgeRepairs:o}=Vo(e,t);return o.some(r=>r.size>0)?e.map((r,s)=>qm(r,o[s],t.capacityScale)):e}function Rc(e,n=vs,t=Is){if(e.length===0)return e;let o=ws(e,n,t);return Ns(o,n)}function Lc(e,n){let t=ut(n);return!ft(t,n)||e.length===0?Oo():Vo(e,t).diagnostics}function wc(e,n){let t=ut(n);return!ft(t,n)||e.length===0?Oo():Vo(e,t,!1,!0).diagnostics}function Nc(e,n){let t=ut(n);if(!ft(t,n)||e.length===0)return{diagnostics:Oo(),candidates:[]};let o=Vo(e,t,!0);return{diagnostics:o.diagnostics,candidates:o.candidates??[]}}var Un={ratio:.7,maxError:.18,maxNormalDot:Math.cos(65*Math.PI/180),preserveVertices:!0,vertexKeyMode:"relaxed",lockBoundary:!0,minGroupTriangles:80,maxPasses:12};function dd(e={}){let n=Number.isFinite(e.ratio)?Math.max(.05,Math.min(.98,e.ratio)):Un.ratio,t=Number.isFinite(e.maxError)&&e.maxError>0?e.maxError:Un.maxError,o=Number.isFinite(e.maxNormalAngleDeg)?Math.max(0,Math.min(89,e.maxNormalAngleDeg)):65;return{ratio:n,maxError:t,maxNormalDot:Math.cos(o*Math.PI/180),preserveVertices:e.preserveVertices??Un.preserveVertices,vertexKeyMode:e.vertexKeyMode??Un.vertexKeyMode,lockBoundary:e.lockBoundary??Un.lockBoundary,minGroupTriangles:Math.max(3,Math.floor(e.minGroupTriangles??Un.minGroupTriangles)),maxPasses:Math.max(1,Math.floor(e.maxPasses??Un.maxPasses))}}function pd(e){return!!(e.texture||e.material?.texture||e.uvs?.length||e.textureTriangles?.length)}function gd(e){return e.vertices.length===3&&!pd(e)}function hd(e){return[e.color??"",e.doubleSided===!0?"2":"1",e.material?.key??""].join("\0")}function Dc(e){return`${Math.round(e[0]*1e5)},${Math.round(e[1]*1e5)},${Math.round(e[2]*1e5)}`}function yd(e,n,t,o){let r=o.vertexKeyMode==="source"?e.simplifySourceVertexKeys?.[t]??e.simplifyVertexKeys?.[t]:e.simplifyVertexKeys?.[t];return r?`${Dc(n)}|${r}`:Dc(n)}function bd(e,n){return[e[0]+n[0],e[1]+n[1],e[2]+n[2]]}function dt(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function xd(e,n){return[e[0]*n,e[1]*n,e[2]*n]}function Ds(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Fc(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 ks(e){return Math.hypot(e[0],e[1],e[2])}function Fs(e,n){return ks(dt(e,n))}function Pd(e){let n=ks(e);return n>1e-12?[e[0]/n,e[1]/n,e[2]/n]:[0,0,1]}function Us(e,n,t,o){return Pd(Fc(dt(e[t],e[n]),dt(e[o],e[n])))}function Bc(e,n,t,o){return ks(Fc(dt(e[t],e[n]),dt(e[o],e[n])))/2}function Md(){return[0,0,0,0,0,0,0,0,0,0]}function Ro(e,n){for(let t=0;t<e.length;t+=1)e[t]+=n[t]}function Ad(e,n,t){let o=e[0],r=e[1],s=e[2],i=-Ds(e,n);return[o*o*t,o*r*t,o*s*t,o*i*t,r*r*t,r*s*t,r*i*t,s*s*t,s*i*t,i*i*t]}function Bs(e,n){let t=n[0],o=n[1],r=n[2];return e[0]*t*t+2*e[1]*t*o+2*e[2]*t*r+2*e[3]*t+e[4]*o*o+2*e[5]*o*r+2*e[6]*o+e[7]*r*r+2*e[8]*r+e[9]}function Sd(e,n,t,o,r,s,i,c,a,l,u,f){let d=e*(r*a-s*c)-n*(o*a-s*i)+t*(o*c-r*i);if(Math.abs(d)<1e-10)return null;let m=1/d,p=l*(r*a-s*c)-n*(u*a-s*f)+t*(u*c-r*f),h=e*(u*a-s*f)-l*(o*a-s*i)+t*(o*f-u*i),g=e*(r*f-u*c)-n*(o*f-u*i)+l*(o*c-r*i),y=[p*m,h*m,g*m];return y.every(Number.isFinite)?y:null}function Cd(e,n,t,o){let r=Sd(e[0],e[1],e[2],e[1],e[4],e[5],e[2],e[5],e[7],-e[3],-e[6],-e[8]),s=xd(bd(n,t),.5),i=Fs(n,t)*1.35,c=o?[n,t]:[n,t,s,...r&&Fs(r,s)<=i?[r]:[]],a=c[0],l=Bs(e,a);for(let u=1;u<c.length;u+=1){let f=c[u],d=Bs(e,f);d<l&&(a=f,l=d)}return a}function Ed(e,n){return e<n?`${e}|${n}`:`${n}|${e}`}function Td(e,n,t){return`${e}|${n}|${t}`}function vd(e,n){let t=new Map,o=[],r=[];for(let s of e){let i=[];for(let u=0;u<s.vertices.length;u+=1){let f=s.vertices[u],d=yd(s,f,u,n),m=t.get(d);m===void 0&&(m=o.length,t.set(d,m),o.push([...f])),i.push(m)}let[c,a,l]=i;c===void 0||a===void 0||l===void 0||c===a||c===l||a===l||r.push({a:c,b:a,c:l,polygon:s,normal:Us(o,c,a,l)})}return{vertices:o,triangles:r}}function Id(e,n){let t=vd(e,n),o=Array.from({length:t.vertices.length},()=>new Set),r=Array.from({length:t.triangles.length},()=>!0);for(let s=0;s<t.triangles.length;s+=1){let i=t.triangles[s];o[i.a].add(s),o[i.b].add(s),o[i.c].add(s)}return{vertices:t.vertices,triangles:t.triangles,alive:r,vertexTriangles:o,activeCount:t.triangles.length}}function _d(e){let n=new Map,t=(o,r,s)=>{let i=Ed(o,r),c=n.get(i);c?c.triangles.push(s):n.set(i,{a:Math.min(o,r),b:Math.max(o,r),triangles:[s]})};for(let o=0;o<e.triangles.length;o+=1){if(!e.alive[o])continue;let r=e.triangles[o];t(r.a,r.b,o),t(r.b,r.c,o),t(r.c,r.a,o)}return n}function Od(e){let n=Array.from({length:e.vertices.length},()=>Md());for(let t=0;t<e.triangles.length;t+=1){if(!e.alive[t])continue;let o=e.triangles[t],r=Math.max(1e-6,Bc(e.vertices,o.a,o.b,o.c)),s=Ad(o.normal,e.vertices[o.a],Math.sqrt(r));Ro(n[o.a],s),Ro(n[o.b],s),Ro(n[o.c],s)}return n}function Vd(e,n,t){let o=new Set,r=s=>{let i=e.vertexTriangles[s];if(i)for(let c of i)e.alive[c]&&o.add(c)};return r(n),r(t),Array.from(o).sort((s,i)=>s-i)}function Rd(e,n,t){let o=e.vertexTriangles[n],r=e.vertexTriangles[t];if(!o||!r)return 0;let[s,i]=o.size<=r.size?[o,r]:[r,o],c=0;for(let a of s)e.alive[a]&&i.has(a)&&(c+=1);return c}function kc(e,n,t){return[e.a===t?n:e.a,e.b===t?n:e.b,e.c===t?n:e.c]}function Ld(e,n,t){if(Rd(e,n.a,n.b)!==2)return null;let o=Vd(e,n.a,n.b);if(o.length===0)return null;let r=e.vertices[n.a];e.vertices[n.a]=n.target;let s=0,i=!0;for(let c of o){let a=e.triangles[c],[l,u,f]=kc(a,n.a,n.b);if(l===u||l===f||u===f)continue;if(Bc(e.vertices,l,u,f)<=1e-8){i=!1;break}let m=Us(e.vertices,l,u,f);if(Ds(a.normal,m)<t.maxNormalDot){i=!1;break}let p=a.a===n.a?r:e.vertices[a.a];if(Math.abs(Ds(a.normal,dt(n.target,p)))>t.maxError){i=!1;break}s+=1}return e.vertices[n.a]=r,i&&s>0?o:null}function wd(e,n,t){e.vertexTriangles[t.a]?.delete(n),e.vertexTriangles[t.b]?.delete(n),e.vertexTriangles[t.c]?.delete(n)}function Nd(e,n,t){e.vertexTriangles[t.a].add(n),e.vertexTriangles[t.b].add(n),e.vertexTriangles[t.c].add(n)}function Dd(e,n,t){e.vertices[n.a]=n.target;let o=new Set;for(let r of t){if(!e.alive[r])continue;let s=e.triangles[r],[i,c,a]=kc(s,n.a,n.b);if(wd(e,r,s),i===c||i===a||c===a){e.alive[r]=!1,e.activeCount-=1;continue}let l=Td(i,c,a);if(o.has(l)){e.alive[r]=!1,e.activeCount-=1;continue}o.add(l);let u={...s,a:i,b:c,c:a,normal:Us(e.vertices,i,c,a)};e.triangles[r]=u,Nd(e,r,u)}}function Fd(e,n){let t=_d(e),o=new Set;for(let c of t.values())c.triangles.length>2&&(o.add(c.a),o.add(c.b));if(n.lockBoundary)for(let c of t.values())c.triangles.length===1&&(o.add(c.a),o.add(c.b));let r=Od(e),s=n.maxError*n.maxError,i=[];for(let c of t.values()){if(c.triangles.length!==2||o.has(c.a)||o.has(c.b))continue;let a=[...r[c.a]];Ro(a,r[c.b]);let l=Cd(a,e.vertices[c.a],e.vertices[c.b],n.preserveVertices),u=Math.max(0,Bs(a,l));if(u>s*Math.max(1,c.triangles.length))continue;let f=Fs(e.vertices[c.a],e.vertices[c.b]);i.push({a:c.a,b:c.b,target:l,error:u,length:f})}return i.sort((c,a)=>c.error-a.error||c.length-a.length),i}function Bd(e){let n=[];for(let t=0;t<e.triangles.length;t+=1)e.alive[t]&&n.push(e.triangles[t]);return n}function kd(e,n){if(e.length<n.minGroupTriangles)return e;let t=Id(e,n),o=t.activeCount;if(o<n.minGroupTriangles)return e;let r=Math.max(1,Math.floor(o*n.ratio));if(r>=o)return e;let s=0;for(let i=0;i<n.maxPasses&&t.activeCount>r;i+=1){let c=Fd(t,n);if(c.length===0)break;let a=0;for(let l of c){if(t.activeCount<=r)break;let u=Ld(t,l,n);if(!u)continue;let f=t.activeCount;Dd(t,l,u),!(t.activeCount>=f)&&(s+=1,a+=1)}if(a===0)break}return s===0||t.activeCount>=o?e:Bd(t).map(i=>({...i.polygon,vertices:[[...t.vertices[i.a]],[...t.vertices[i.b]],[...t.vertices[i.c]]]}))}function Ud(e){let n=new Map;for(let t=0;t<e.length;t+=1){let o=e[t];if(!gd(o))continue;let r=hd(o),s=n.get(r);s?(s.indices.push(t),s.polygons.push(o)):n.set(r,{indices:[t],polygons:[o]})}return n}function Lo(e,n){let t=dd(n),o=Ud(e);if(o.size===0)return e;let r=new Map,s=!1;for(let c of o.values()){let a=kd(c.polygons,t);if(!(a===c.polygons||a.length>=c.polygons.length)){r.set(c.indices[0],a);for(let l=1;l<c.indices.length;l+=1)r.set(c.indices[l],[]);s=!0}}if(!s)return e;let i=[];for(let c=0;c<e.length;c+=1){let a=r.get(c);a?i.push(...a):a===void 0&&i.push(e[c])}return i}var Gd=3,zd=.03,$d=.02,zt={maxAngleDeg:Gd,maxPlaneDisplacement:zd,maxBoundaryDisplacement:$d,isolatedPairs:!1},wo={maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.04,isolatedPairs:!0},Gs=[{...wo,maxAngleDeg:30},{...wo,maxAngleDeg:45},{...wo,maxAngleDeg:60,maxBoundaryDisplacement:.06}],Xd=1800,Yd=.65,Uc=500,Gc=2200,Wd=1e3,jd=2200,Kd=350,Hd=700,Zd=60,qd=.06,Qd=4,Jd=.003,ep=4e3,np=3e3,tp=4500,op=2400,rp=4200,sp=1800,ip=5e3,ap=.28,cp=.15,lp=.23,up=2e3,fp=16,mp=4e3,dp=900,pp=16,gp=1.5,$s=.045,hp=.999,yp=1e-5,bp=24,xp=50,Pp=1e3,Mp=1e3,zn={minGroupPolygons:2,maxCandidateAxes:24},Ap={...zn,maxCandidateAxes:1},zc={...zn,maxCandidateAxes:2},Sp={...zn,maxCandidateAxes:2},Cp={steps:[{kind:"baselineCandidates"},{kind:"initialLossyCandidates"},{kind:"finalLossyCandidates"}]};function jc(e,n={}){return Ep(e,n).polygons}function Kc(e,n={}){let t={meshResolution:n.meshResolution,stopAtPolygonCount:n.stopAtPolygonCount,rectCover:n.rectCover},o=new No,r={captureVisiblePolygons:!0,skipInteriorCull:n.skipInteriorCull===!0};return o.workspaceFor(e,r).createRun(t,r).optimizeParse({staticSimplification:n.staticSimplification,useCandidateFirst:n.useCandidateFirst===!0}).polygons}function Ep(e,n={},t={}){return new No().workspaceFor(e,t).createRun(n,t).optimize()}var No=class{constructor(){this.workspaces=new Map}workspaceFor(n,t){let o=Tp(t),r=this.workspaces.get(o);r||(r=new WeakMap,this.workspaces.set(o,r));let s=r.get(n);return s||(s=new Xs(n,t,this),r.set(n,s)),s}};function Tp(e){return[e.skipInteriorCull===!0?"skip-cull":"cull",e.simplifiedCandidate===!0?"simplified":"source"].join(":")}var Xs=class{constructor(n,t,o){this.source=n,this.graph=o,this.preprocessCache={skipInteriorCull:t.skipInteriorCull===!0}}polygons(){return this.source}preprocess(n){return Zp(this.source,n,this.preprocessCache)}configureSnappedCullReuse(n,t){let o=n.length/Math.max(1,this.source.length);this.preprocessCache.reuseSnappedInteriorCull=t.simplifiedCandidate===!0?this.source.length<=rp:n.length<=sp||n.length<=ip&&o>=ap}visiblePolygons(){return this.preprocessCache.interior??this.preprocessCache.deduped??this.source}createRun(n,t){return new Ys(this,n,t)}workspaceFor(n,t){return this.graph.workspaceFor(n,t)}},Ys=class e{constructor(n,t,o,r={}){this.baselineCandidatesComplete=!1;this.initialLossyCandidatesComplete=!1;this.finalLossyCandidatesComplete=!1;this.workspace=n,this.source=n.polygons(),this.options=t,this.runOptions=o,this.costCandidateMode=r.costCandidateMode??"cost",this.seamCandidateMode=r.seamCandidateMode??"seamSafe",this.offerBaseline=r.offerBaseline===!0,this.meshResolution=t.meshResolution??"lossy",this.stopAtPolygonCount=Number.isFinite(t.stopAtPolygonCount)?Math.max(0,Math.floor(t.stopAtPolygonCount)):void 0,this.requiredMaxPolygonCount=Number.isFinite(o.requiredMaxPolygonCount)?Math.max(0,Math.floor(o.requiredMaxPolygonCount)):void 0,this.baseline=this.preprocess(!1),this.workspace.configureSnappedCullReuse(this.baseline,o),this.visiblePolygons=o.captureVisiblePolygons?this.workspace.visiblePolygons():void 0,this.acceptor=r.acceptor??new Ws(this.baseline,this.requiredMaxPolygonCount)}optimize(){return this.runPlan(Cp)}optimizeParse(n){let t=n.staticSimplification===!1?null:n.staticSimplification??{},o=[{kind:"baselineCandidates"},{kind:"initialLossyCandidates"}];return n.useCandidateFirst||o.push({kind:"finalLossyCandidates"}),t&&o.push({kind:"staticSimplificationCandidates",options:t,stopOnAccept:n.useCandidateFirst}),n.useCandidateFirst&&o.push({kind:"finalLossyCandidates"}),this.runPlan({steps:o})}get best(){return this.acceptor.polygons}get bestCost(){return this.acceptor.cost}runStaticSimplificationCandidates(n={}){if(this.meshResolution!=="lossy"||!Gp(this.source,this.best))return!1;let t=Zc(this.source,n);if(!t)return!1;for(let o of this.staticSimplificationPlans(t)){let r=zp(o,n);if(r&&this.generateStaticSimplificationCandidate(r,n,o.skipInteriorCull))return!0}return!1}staticSimplificationPlans(n){let t=this.source.length-n.length,o=[],r=this.visiblePolygons&&this.visiblePolygons!==this.source?this.source.length-this.visiblePolygons.length:0;return this.visiblePolygons&&this.visiblePolygons!==this.source&&this.source.length>=mp&&r>=dp&&(t<=pp||r>=t*gp)&&o.push({source:this.visiblePolygons,skipInteriorCull:!0}),this.best.length>=up&&t<=fp&&Up(this.source)&&o.push({source:this.source,vertexKeyMode:"source"}),o.push({source:this.source,precomputed:n}),o}generateStaticSimplificationCandidate(n,t,o=!1){let r={requiredMaxPolygonCount:this.best.length-1,skipExactRectCover:!0,skipInteriorCull:o,simplifiedCandidate:!0},s=this.workspace.workspaceFor(n,r),i=this.best;return new e(s,{meshResolution:"lossy",stopAtPolygonCount:kp(this.best.length,t)},r,{acceptor:this.acceptor,costCandidateMode:"dom",seamCandidateMode:"dom",offerBaseline:!0}).optimize(),this.best!==i}runPlan(n){for(let t of n.steps){let o=!1;if(t.kind==="baselineCandidates")this.runBaselineCandidates();else if(t.kind==="staticSimplificationCandidates")o=this.runStaticSimplificationCandidates(t.options)&&t.stopOnAccept;else{if(!this.shouldRunLossyPipeline())break;t.kind==="initialLossyCandidates"?this.runInitialLossyPipeline():this.runFinalLossyPipeline()}if(o||this.shouldStop())break}return this.result()}runBaselineCandidates(){if(this.baselineCandidatesComplete||(this.baselineCandidatesComplete=!0,this.offerBaseline&&(this.acceptCandidate(this.baseline),this.shouldStop())))return;let n=this.runOptions.skipExactRectCover===!0?!1:this.meshResolution==="lossy"&&this.options.rectCover===void 0?Xp(this.baseline):this.options.rectCover;if(this.shouldStop())return;let t=Gn(this.baseline,n);if(t!==this.baseline&&this.acceptCandidate(t),!this.shouldStop()&&this.meshResolution==="lossy"&&this.options.rectCover===void 0&&this.runOptions.skipExactRectCover!==!0){let o=Gn(this.baseline,void 0);o!==this.baseline&&this.acceptCandidate(o)}}shouldRunLossyPipeline(){return this.meshResolution==="lossy"&&!this.shouldStop()&&this.best.length>1&&this.bestCost>1+1e-9}runInitialLossyPipeline(){if(this.initialLossyCandidatesComplete)return;this.initialLossyCandidatesComplete=!0;let n=this.preprocess(wo);this.acceptCandidate(n),!this.shouldStop()&&(this.options.rectCover===void 0&&this.source.length>=Uc&&this.source.length<=Gc&&(this.acceptCandidate(Gn(n,zc)),this.shouldStop())||this.options.rectCover!==void 0&&this.options.rectCover!==!1&&(this.acceptCandidate(Gn(n,this.options.rectCover)),this.shouldStop()))}runFinalLossyPipeline(){this.finalLossyCandidatesComplete||(this.finalLossyCandidatesComplete=!0,!(this.runOptions.simplifiedCandidate===!0&&this.source.length>tp)&&(this.runAggressiveLossyVariants(),!this.shouldStop()&&this.runLargeRectCoverCandidate()))}runAggressiveLossyVariants(){let n=this.stopAtPolygonCount===void 0&&this.source.length>=ep&&this.best.length<=np,t=!1;for(let o=n?Gs.length:0;o<Gs.length;o+=1){if(o===1&&!t||o===1&&this.runOptions.simplifiedCandidate===!0&&this.source.length<op||o===2&&!Wp(this.source))continue;let r=this.preprocess(Gs[o]),s=r;this.options.rectCover===void 0&&this.source.length>=Uc&&this.source.length<=Gc&&(s=Gn(r,zc)),this.options.rectCover!==void 0&&this.options.rectCover!==!1&&(s=Gn(r,this.options.rectCover));let i=this.acceptSeamSafeCandidate(s,this.source.length);if(o===0&&i&&(t=!0),this.shouldStop())return}}runLargeRectCoverCandidate(){if(this.options.rectCover!==void 0)return;let n=Gn(this.best,Yp(this.best));n!==this.best&&this.acceptSeamSafeCandidate(n,this.best.length)}preprocess(n){return this.workspace.preprocess(n)}result(){return{polygons:this.best,visiblePolygons:this.visiblePolygons}}shouldStop(){return this.stopAtPolygonCount!==void 0&&this.best.length<=this.stopAtPolygonCount}acceptCandidate(n,t=$n(n)){return this.emitCandidate({mode:this.costCandidateMode,polygons:n,cost:t})}acceptDomCandidate(n){return this.emitCandidate({mode:"dom",polygons:n})}acceptSeamSafeCandidate(n,t,o){return this.emitCandidate({mode:this.seamCandidateMode,polygons:n,sourcePolygonCount:t,cost:o})}emitCandidate(n){return this.acceptor.accept({...n,maxPolygonCount:n.maxPolygonCount??this.requiredMaxPolygonCount})}},Ws=class{constructor(n,t){this.best=n,this.bestCost=$n(n),this.bestDiagnostics={polygons:n},this.requiredMaxPolygonCount=t}get polygons(){return this.best}get cost(){return this.bestCost}accept(n){return n.mode==="cost"?this.acceptCostCandidate(n.polygons,n.cost):n.mode==="dom"?this.acceptDomCandidate(n.polygons,n.cost,n.maxPolygonCount):this.acceptSeamSafeCandidate(n.polygons,n.sourcePolygonCount??n.polygons.length,n.cost,n.maxPolygonCount)}acceptCostCandidate(n,t=$n(n)){return t>=this.bestCost?!1:(this.commit(n,t),!0)}acceptDomCandidate(n,t=$n(n),o){return n.length>=this.best.length||!this.withinMaxPolygonCount(n,o)?!1:(this.commit(n,t),!0)}acceptSeamSafeCandidate(n,t,o,r){if(n.length>=this.best.length||!this.withinMaxPolygonCount(n,r))return!1;let s=Bp(t);if(this.bestCost-n.length<s)return!1;let i=o??$n(n),c=this.bestCost-i;if(c<=0||c<s)return!1;let a=$c(n);if(!a)return!1;let l=this.bestSeamDiagnostics();return!l||vp(a,l)||Ip(this.bestTopologyEdges(),this.bestTopologySelfDiagnostics(),n)?!1:(this.commit(n,i,a),!0)}withinMaxPolygonCount(n,t){let o=t??this.requiredMaxPolygonCount;return o===void 0||n.length<=o}commit(n,t,o){this.best=n,this.bestCost=t,this.bestDiagnostics={polygons:n,seam:o}}resetBestDiagnostics(n){this.bestDiagnostics={polygons:this.best,seam:n}}bestSeamDiagnostics(){if(this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),!this.bestDiagnostics.seam){let n=$c(this.best);if(!n)return null;this.bestDiagnostics.seam=n}return this.bestDiagnostics.seam}bestTopologyEdges(){return this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),this.bestDiagnostics.topologyEdges||(this.bestDiagnostics.topologyEdges=Hc(this.best)),this.bestDiagnostics.topologyEdges}bestTopologySelfDiagnostics(){return this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),this.bestDiagnostics.topologySelf||(this.bestDiagnostics.topologySelf=Rp(this.bestTopologyEdges(),$s)),this.bestDiagnostics.topologySelf}};function $n(e){let n=0;for(let t of e){let o=t.vertices.length,r=o<=4?0:Math.min(4,o-4)*.12,s=t.texture||t.material?.texture||t.textureTriangles?.length?.15:0;n+=1+r+s}return n}function $c(e){try{return wc(e)}catch(n){if(n instanceof RangeError&&n.message==="Set maximum size exceeded")return null;throw n}}function vp(e,n){return e.nearPairs>n.nearPairs||e.unclosedPairs>n.unclosedPairs||e.maxMeasuredGapPx>n.maxMeasuredGapPx+1e-9||e.maxResidualGapPx>n.maxResidualGapPx+1e-9}function Ip(e,n,t){let o=Hc(t);return _p(e,o,$s)?!0:Vp(o.boundarySegments,$s,n)}function _p(e,n,t){if(e.internalSegments.length===0)return!1;let o=Op(e,t);for(let r of n.boundarySegments)if(!e.boundaryKeys.has(r.key)&&(e.internalKeys.has(r.key)||Dp(r,o,t)))return!0;return!1}function Op(e,n){return(!e.internalIndex||e.internalIndexTolerance!==n)&&(e.internalIndex=Hs(e.internalSegments,n),e.internalIndexTolerance=n),e.internalIndex}function Vp(e,n,t){let o=Hs(e,n),r=new Set,s=0,i=0,c=0;for(let a of e)s=Math.max(s,a.index+1);for(let a of e)for(let l of Zs(a,o,n)){if(l===a||l.polygon===a.polygon||l.key===a.key)continue;let u=a.index<l.index?a.index*s+l.index:l.index*s+a.index;if(r.has(u))continue;r.add(u);let f=qs(a,l,n);if(f&&(i+=1,c+=f.overlapLength,i>t.tJunctionPairs||c>t.tJunctionLength+1e-9))return!0}return!1}function Rp(e,n){let t={tJunctionPairs:0,tJunctionLength:0};return Lp(t,e.boundarySegments,n),t}function Lp(e,n,t){let o=Hs(n,t),r=new Set,s=0;for(let i of n)s=Math.max(s,i.index+1);for(let i of n)for(let c of Zs(i,o,t)){if(c===i||c.polygon===i.polygon||c.key===i.key)continue;let a=i.index<c.index?i.index*s+c.index:c.index*s+i.index;if(r.has(a))continue;r.add(a);let l=qs(i,c,t);l&&(e.tJunctionPairs+=1,e.tJunctionLength+=l.overlapLength)}}function Hc(e){let n=new Map;for(let a=0;a<e.length;a+=1){let l=e[a].vertices;for(let u=0;u<l.length;u+=1){let f=l[u],d=l[(u+1)%l.length],m=wp(Yt(f,d),a,u,f,d);if(!m)continue;let p=n.get(m.key);p?p.count+=1:n.set(m.key,{count:1,segment:m})}}let t=new Set,o=new Set,r=[],s=[],i=0,c=0;for(let a of n.values()){let l={...a.segment,index:c};c+=1,a.count===1?(t.add(l.key),r.push(l),i+=l.length):(o.add(l.key),s.push(l))}return{boundaryKeys:t,internalKeys:o,boundarySegments:r,internalSegments:s,boundaryLength:i}}function wp(e,n,t,o,r){let s=An(r,o),i=Math.hypot(s[0],s[1],s[2]);if(i<=1e-10)return null;let c=[s[0]/i,s[1]/i,s[2]/i];return{index:-1,key:e,polygon:n,edge:t,a:o,b:r,dir:c,length:i,minX:Math.min(o[0],r[0]),minY:Math.min(o[1],r[1]),minZ:Math.min(o[2],r[2]),maxX:Math.max(o[0],r[0]),maxY:Math.max(o[1],r[1]),maxZ:Math.max(o[2],r[2])}}function Hs(e,n){let t=Math.max(n*8,.5),o=new Map;for(let r of e)Np(o,r,t,n);return{cellSize:t,cells:o}}function Np(e,n,t,o){let[r,s,i]=Do([n.minX-o,n.minY-o,n.minZ-o],t),[c,a,l]=Do([n.maxX+o,n.maxY+o,n.maxZ+o],t);for(let u=r;u<=c;u+=1)for(let f=s;f<=a;f+=1)for(let d=i;d<=l;d+=1){let m=`${u},${f},${d}`,p=e.get(m);p?p.push(n):e.set(m,[n])}}function Do(e,n){return[Math.floor(e[0]/n),Math.floor(e[1]/n),Math.floor(e[2]/n)]}function Zs(e,n,t){let o=[],r=new Set,[s,i,c]=Do([e.minX-t,e.minY-t,e.minZ-t],n.cellSize),[a,l,u]=Do([e.maxX+t,e.maxY+t,e.maxZ+t],n.cellSize);for(let f=s;f<=a;f+=1)for(let d=i;d<=l;d+=1)for(let m=c;m<=u;m+=1){let p=n.cells.get(`${f},${d},${m}`);if(p)for(let h of p)r.has(h)||(r.add(h),o.push(h))}return o}function Dp(e,n,t){for(let o of Zs(e,n,t))if(qs(e,o,t))return!0;return!1}function qs(e,n,t){if(!Fp(e,n,t)||Math.abs(qe(e.dir,n.dir))<hp)return null;let o=qe(An(n.a,e.a),e.dir),r=qe(An(n.b,e.a),e.dir),s=Math.max(0,Math.min(o,r)),i=Math.min(e.length,Math.max(o,r)),c=i-s;if(c<=Math.max(yp,Math.min(e.length,n.length)*1e-4))return null;let a=[e.a[0]+e.dir[0]*((s+i)/2),e.a[1]+e.dir[1]*((s+i)/2),e.a[2]+e.dir[2]*((s+i)/2)],l=Math.max(0,Math.min(n.length,qe(An(a,n.a),n.dir))),u=[n.a[0]+n.dir[0]*l,n.a[1]+n.dir[1]*l,n.a[2]+n.dir[2]*l],f=ul(a,u);return f<=t?{overlapLength:c,offset:f}:null}function Fp(e,n,t){return e.minX<=n.maxX+t&&n.minX<=e.maxX+t&&e.minY<=n.maxY+t&&n.minY<=e.maxY+t&&e.minZ<=n.maxZ+t&&n.minZ<=e.maxZ+t}function Bp(e){return Math.max(Qd,e*Jd)}function kp(e,n){let t=Number.isFinite(n.earlyStopDropRatio)?Math.max(0,n.earlyStopDropRatio):cp;return Math.max(0,e-Math.max(1,Math.ceil(e*t)))}function Up(e){return e.some(n=>n.simplifySourceVertexKeys?.length===n.vertices.length)}function Gp(e,n){return e.length===0||n.length/e.length>lp}function Zc(e,n,t){let o=Lo(e,{...n.simplifyTriangleMeshOptions,...t?{vertexKeyMode:t}:{}});return o===e||o.length>=e.length?null:o}function zp(e,n){return e.precomputed!==void 0?e.precomputed:Zc(e.source,n,e.vertexKeyMode)}function Gn(e,n){if(n===!1)return e;let t=$p(e,n);if(!t)return e;let o=Ao(e,t);return o.length<e.length?o:e}function $p(e,n){if(n&&n!==!0)return n;let t=n===!0,o=e.length;return o>2200||!t&&o>=bp&&o<xp||!t&&o>Pp?null:!t&&o<=300?{...zn,maxCandidateAxes:1}:o>300?!t&&(qc(e)>12||Qc(e)>Mp)?null:{...zn,maxCandidateAxes:Math.min(zn.maxCandidateAxes??24,2)}:zn}function Xp(e){return e.length>Xd||e.length===0||jp(e)/e.length<Yd?!1:Ap}function Yp(e){return e.length<Wd||e.length>jd||qc(e)<=12||Qc(e)>Kd?!1:Sp}function Wp(e){return e.length<=Hd}function jp(e){let n=0;for(let t of e)t.vertices.length===3&&(n+=1);return n}function qc(e){let n=0;for(let t of e)n=Math.max(n,t.vertices.length);return n}function Qc(e){let n=new Map;for(let o of e)for(let r=0;r<o.vertices.length;r++){let s=o.vertices[r],i=o.vertices[(r+1)%o.vertices.length],c=Yt(s,i);n.set(c,(n.get(c)??0)+1)}let t=0;for(let o of n.values())o===1&&(t+=1);return t}function Fo(e,n){return n==null?e:n.map(t=>e[t]).filter(t=>!!t)}function js(e,n){if(n===e||n.length===e.length&&n.every((r,s)=>r===e[s]))return null;let t=new Set(n),o=[];for(let r=0;r<e.length;r++)t.has(e[r])&&o.push(r);return o.length===e.length?null:o}function Kp(e,n){if(n?.deduped)return n.deduped;let t=n?.dedupedIndices;if(t===void 0){let r=Rt(e);if(r.size===0)t=null;else{t=[];for(let s=0;s<e.length;s++)r.has(s)||t.push(s)}n&&(n.dedupedIndices=t)}let o=Fo(e,t);return n&&(n.deduped=o),o}function Hp(e,n){if(n?.skipInteriorCull)return e;if(n?.interior)return n.interior;let t=n?.interiorIndices;if(t===void 0){let r=un(e);t=js(e,r),n&&(n.interiorIndices=t)}let o=Fo(e,t);return n&&(n.interior=o),o}function Zp(e,n,t){let o=Kp(e,t),r=Hp(o,t),s=t?.baseline??tn(r);if(t&&!t.baseline&&(t.baseline=s),!n)return s;let i=n===!0?zt:Qp(n);if(i.isolatedPairs){let u=Yc(Xc(o,t),i,t),f=tn(u);if(t?.snappedInteriorUsesBaselineFilter&&qp(s,f)){let d=Yc(Xc(o,t,!0),i,t),m=tn(d);m.length<f.length&&(f=m)}return f.length<s.length?f:s}let c=fg(o,i,t),a=un(c),l=tn(a);return l.length<s.length?l:s}function Jc(e,n){return n?(n.snapped||(n.snapped=Bo(e)),n.snapped):Bo(e)}function Xc(e,n,t=!1){if(!n)return un(Bo(e));if(t&&n.snappedInteriorExact)return n.snappedInteriorExact;if(!t&&n.snappedInterior)return n.snappedInterior;let o=Jc(e,n);if(t){if(o===e&&n.deduped===e&&n.interior)return n.snappedInteriorExactIndices=n.interiorIndices,n.snappedInteriorExact=n.interior,n.snappedInteriorExact;let r=un(o);return n.snappedInteriorExactIndices=js(o,r),n.snappedInteriorExact=r,n.snappedInteriorExact}if(!n.snappedInterior){if(o===e&&n.deduped===e&&n.interior)return n.snappedInteriorIndices=n.interiorIndices,n.snappedInterior=n.interior,n.snappedInteriorUsesBaselineFilter=!1,n.snappedInterior;if(n.reuseSnappedInteriorCull!==!1&&n.interiorIndices!==void 0)return n.snappedInteriorIndices=n.interiorIndices,n.snappedInterior=Fo(o,n.snappedInteriorIndices),n.snappedInteriorUsesBaselineFilter=!0,n.snappedInterior;if(n.snappedInteriorIndices===void 0){let r=un(o);n.snappedInteriorIndices=js(o,r),n.snappedInterior=r,n.snappedInteriorExactIndices=n.snappedInteriorIndices,n.snappedInteriorExact=r,n.snappedInteriorUsesBaselineFilter=!1}else n.snappedInterior=Fo(o,n.snappedInteriorIndices)}return n.snappedInterior}function qp(e,n){return n.length>=e.length}function Qp(e){return{maxAngleDeg:e.maxAngleDeg??zt.maxAngleDeg,maxPlaneDisplacement:e.maxPlaneDisplacement??zt.maxPlaneDisplacement,maxBoundaryDisplacement:e.maxBoundaryDisplacement??zt.maxBoundaryDisplacement,isolatedPairs:e.isolatedPairs??zt.isolatedPairs}}function Yc(e,n,t){let o=Jp(e,t),r=[];for(let i of eg(e,o))ng(i,n)&&r.push(i.candidate);let s=og(r);return s.length===0?e:tg(e,s)}function Jp(e,n){if(n?.trianglePairSource?.polygons===e)return n.trianglePairSource;let t=e.map(i=>{let c=Js(i);return c?{polygon:i,normal:c.normal,area:c.area,materialKey:sl(i)}:null}),o=new Map;for(let i=0;i<e.length;i++){let c=e[i];if(!(c.vertices.length!==3||!t[i]))for(let a=0;a<c.vertices.length;a++){let l=Yt(c.vertices[a],c.vertices[(a+1)%c.vertices.length]),u=o.get(l);u?u.push(i):o.set(l,[i])}}let r=[];for(let i of o.values())i.length===2&&r.push([i[0],i[1]]);let s={polygons:e,metas:t,edgeOwnerPairs:r};return n&&(n.trianglePairSource=s),s}function eg(e,n){if(n.preparedCandidates)return n.preparedCandidates;let t=[];for(let[o,r]of n.edgeOwnerPairs){let s=cg(o,r,e,n.metas);s&&t.push(s)}return n.preparedCandidates=t,t}function ng(e,n){let t=Math.cos(n.maxAngleDeg*Math.PI/180);return e.normalDot>=t&&e.maxDistance<=Math.min(n.maxPlaneDisplacement,n.maxBoundaryDisplacement)}function tg(e,n){let t=new Map,o=new Set,r=el(n.flatMap(i=>i.vertexMoves));for(let i of n){let c=Math.min(i.a,i.b);t.set(c,i.polygon),o.add(Math.max(i.a,i.b))}let s=[];for(let i=0;i<e.length;i++){let c=t.get(i);if(c){s.push(c);continue}o.has(i)||s.push(e[i])}return r.size>0?nl(s,r):s}function og(e){return e.length>3e3?rg(e):sg(e)}function rg(e){let n=new Map;for(let s of e)n.set(s.a,(n.get(s.a)??0)+1),n.set(s.b,(n.get(s.b)??0)+1);let t=[...e].sort((s,i)=>{let c=(n.get(s.a)??0)+(n.get(s.b)??0),a=(n.get(i.a)??0)+(n.get(i.b)??0);return c-a||s.score-i.score}),o=new Set,r=[];for(let s of t)o.has(s.a)||o.has(s.b)||(o.add(s.a),o.add(s.b),r.push(s));return r}function sg(e){let n=new Map;for(let l=0;l<e.length;l++){let u=e[l],f=n.get(u.a);f?f.push(l):n.set(u.a,[l]);let d=n.get(u.b);d?d.push(l):n.set(u.b,[l])}let t=[],o=new Array(e.length).fill(!0),r=new Map,s=new Ks;for(let[l,u]of n)r.set(l,u.length);let i=l=>(r.get(l.a)??0)+(r.get(l.b)??0),c=l=>{let u=e[l];s.push({degree:i(u),score:u.score,index:l})},a=(l,u)=>{if(!o[l])return;o[l]=!1;let f=e[l];for(let d of[f.a,f.b])r.set(d,(r.get(d)??0)-1),u.add(d)};for(let l=0;l<e.length;l++)c(l);for(;s.size()>0;){let l=s.pop();if(!o[l.index])continue;let u=e[l.index];if(i(u)!==l.degree){c(l.index);continue}t.push(u);let d=new Set;for(let m of[u.a,u.b])for(let p of n.get(m)??[])a(p,d);for(let m of d)for(let p of n.get(m)??[])o[p]&&c(p)}return t}var Ks=class{constructor(){this.items=[]}size(){return this.items.length}push(n){this.items.push(n);let t=this.items.length-1;for(;t>0;){let o=t-1>>1;if(zs(this.items[o],this.items[t])<=0)break;[this.items[o],this.items[t]]=[this.items[t],this.items[o]],t=o}}pop(){if(this.items.length===0)return null;let n=this.items[0],t=this.items.pop();if(this.items.length>0){this.items[0]=t;let o=0;for(;;){let r=o*2+1,s=r+1,i=o;if(r<this.items.length&&zs(this.items[r],this.items[i])<0&&(i=r),s<this.items.length&&zs(this.items[s],this.items[i])<0&&(i=s),i===o)break;[this.items[o],this.items[i]]=[this.items[i],this.items[o]],o=i}}return n}};function zs(e,n){return e.degree-n.degree||e.score-n.score||e.index-n.index}function el(e){let n=new Map;for(let o of e){let r=n.get(o.key);r?(r.x+=o.target[0],r.y+=o.target[1],r.z+=o.target[2],r.count+=1):n.set(o.key,{x:o.target[0],y:o.target[1],z:o.target[2],count:1})}let t=new Map;for(let[o,r]of n)t.set(o,[r.x/r.count,r.y/r.count,r.z/r.count]);return t}function ig(e,n){let t=[];for(let o=0;o<e.length;o++){let r=e[o].vertices,s=n[o]?.vertices;if(!s||s.length!==r.length)continue;for(let a=0;a<r.length;a++)t.push({key:sn(r[a]),target:s[a]});let i=e[o].textureTriangles??[],c=n[o]?.textureTriangles??[];for(let a=0;a<i.length;a++){let l=c[a];if(l)for(let u=0;u<i[a].vertices.length;u++)t.push({key:sn(i[a].vertices[u]),target:l.vertices[u]})}}return t}function ag(e,n){let t=[];for(let o of e)for(let r of o.textureTriangles??[])for(let s of r.vertices)t.push({key:sn(s),target:oi(s,n)});return t}function nl(e,n){return e.map(t=>{let o=!1,r=c=>{let a=n.get(sn(c));return a?(o=!0,a):c},s=t.vertices.map(r),i=Qs(t.textureTriangles,r);return o?{...t,vertices:s,...i?{textureTriangles:i}:{}}:t})}function cg(e,n,t,o){let r=t[e],s=t[n],i=o[e],c=o[n];if(!i||!c||r.vertices.length!==3||s.vertices.length!==3||!Sg(r,s,i,c))return null;let a=ni(r,s);if(!a)return null;let[l,u,f,d]=a;if((f+1)%s.vertices.length===d)return null;let p=Math.abs(qe(i.normal,c.normal)),h=Math.cos(Zd*Math.PI/180);if(p<h)return null;let g=(u+1)%r.vertices.length,y=3-f-d,b=[r.vertices[u],r.vertices[g],r.vertices[l],s.vertices[y]],x=lg(b);if(!x)return null;let P=0,S=0;for(let M of b){let E=Math.abs(ti(M,x));P=Math.max(P,E),S+=E*E}if(P>qd)return null;let C=b.map(M=>oi(M,x));if(!ug(C,x.normal))return null;let T=Js({vertices:C});if(!T||qe(T.normal,i.normal)<.2||qe(T.normal,c.normal)<.2)return null;let A={vertices:b,color:r.color,...r.doubleSided?{doubleSided:!0}:{},...r.data?{data:{...r.data}}:{}};if(ei(r,s)&&r.uvs&&s.uvs&&r.texture){A.texture=r.texture,r.textureWrap&&(A.textureWrap={...r.textureWrap}),r.textureAlphaMode&&(A.textureAlphaMode=r.textureAlphaMode),A.uvs=[[...r.uvs[u]],[...r.uvs[g]],[...r.uvs[l]],[...s.uvs[y]]];let M=mg([r,s]);M?.length&&(A.textureTriangles=M)}return{normalDot:p,maxDistance:P,candidate:{a:e,b:n,polygon:A,vertexMoves:[...b.map((M,E)=>({key:sn(M),target:C[E]})),...ag([r,s],x)],score:S/b.length+P*.25+(1-p)*.1}}}function lg(e){if(e.length<3)return null;let n=0,t=0,o=0,r=0,s=0,i=0;for(let a=0;a<e.length;a++){let l=e[a],u=e[(a+1)%e.length];n+=(l[1]-u[1])*(l[2]+u[2]),t+=(l[2]-u[2])*(l[0]+u[0]),o+=(l[0]-u[0])*(l[1]+u[1]),r+=l[0],s+=l[1],i+=l[2]}let c=fl([n,t,o]);return c?{normal:c,point:[r/e.length,s/e.length,i/e.length]}:null}function ug(e,n){let t=0;for(let o=0;o<e.length;o++){let r=e[o],s=e[(o+1)%e.length],i=e[(o+2)%e.length],c=qe(ll(An(s,r),An(i,s)),n);if(Math.abs(c)<=1e-9)continue;let a=c>0?1:-1;if(t===0)t=a;else if(t!==a)return!1}return!0}function fg(e,n,t){let o=Jc(e,t),r=bg(o,n);if(r<=0)return o;let s=o.map(m=>{let p=Js(m);return p?{polygon:m,normal:p.normal,area:p.area,materialKey:sl(m)}:null}),i=Mg(o,s),c=new Set,a=Array(o.length),l=[],u=(m,p)=>{a[m]=p};for(let m=0;m<o.length;m++){let p=s[m];if(c.has(m))continue;if(!p){u(m,o[m]);continue}let h=Cg(m,s,i,c,r,n);for(let y of h)c.add(y);if(h.length<2){u(m,o[m]);continue}let g=gg(h,o,s,i,r,n);l.push(...g.vertexMoves);for(let y of h)u(y,g.polygons.get(y)??o[y])}let f=a.flatMap(m=>m?[m]:[]),d=l.length>0?nl(f,el(l)):f;return Bo(d)}function Bo(e){let n=rl(e),t=1e-4;if(n<=0)return e;let o=xg(n),r=Pg(t),s=!1,i=e.map(c=>{let a=!1,l=p=>{let h=o.snap(p);return $t(h,p)||(a=!0),h},u=c.vertices.map(l),f=c.uvs&&c.uvs.length===c.vertices.length?c.uvs.map(p=>{let h=r.snap(p);return Xt(h,p)||(a=!0),h}):void 0,d=Qs(c.textureTriangles,l);if(!a&&!c.texture)return c;let m={...c,vertices:u,...f?{uvs:f}:{},...d?{textureTriangles:d}:{}};return s=!0,{...m,...m.texture?{textureTriangles:tl(m)}:{}}});return s?i:e}function tl(e){if(e.texture){if(e.textureTriangles?.length)return pg(e.textureTriangles);if(e.uvs&&e.uvs.length===e.vertices.length)return dg(e.vertices,e.uvs)}}function mg(e){let n=e.flatMap(t=>tl(t)??[]);return n.length>0?n:void 0}function dg(e,n){let t=[];for(let o=1;o<e.length-1;o++)t.push({vertices:[[...e[0]],[...e[o]],[...e[o+1]]],uvs:[[...n[0]],[...n[o]],[...n[o+1]]]});return t}function pg(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function Qs(e,n){if(e?.length)return e.map(t=>({vertices:t.vertices.map(n),uvs:t.uvs.map(o=>[...o])}))}function gg(e,n,t,o,r,s){let i=ol(e,n,t,r,s);return i?yg(i):hg(e,n,t,o,r,s)}function hg(e,n,t,o,r,s){let i=new Set(e),c=[];for(let f of e)for(let d of o.get(f)??[]){if(f>=d||!i.has(d))continue;let m=ol([f,d],n,t,r,s);m&&c.push(m)}c.sort((f,d)=>d.score-f.score);let a=new Set,l=new Map,u=[];for(let f of c)if(!f.indices.some(d=>a.has(d))){u.push(...f.vertexMoves);for(let d=0;d<f.indices.length;d++){let m=f.indices[d];a.add(m),l.set(m,n[m])}}return{polygons:l,vertexMoves:u}}function yg(e){let n=new Map;for(let t=0;t<e.indices.length;t++)n.set(e.indices[t],e.source[t]);return{polygons:n,vertexMoves:e.vertexMoves}}function ol(e,n,t,o,r){let s=il(e,t);if(!s||!al(e,t,s,o,r))return null;let i=e.map(u=>n[u]),c=i.map(u=>vg(u,s)),a=$n(tn(i)),l=$n(tn(c));return l>=a?null:{indices:e,source:i,projected:c,vertexMoves:ig(i,c),score:a-l}}function bg(e,n){return rl(e)<=0?0:n.maxPlaneDisplacement}function rl(e){let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0;for(let a of e)for(let[l,u,f]of a.vertices)n=Math.min(n,l),t=Math.min(t,u),o=Math.min(o,f),r=Math.max(r,l),s=Math.max(s,u),i=Math.max(i,f);if(!Number.isFinite(n))return 0;let c=Math.hypot(r-n,s-t,i-o);return c<=0?0:Math.min(.025,Math.max(1e-4,c*25e-5))}function xg(e){let n=new Map,t=r=>Math.floor(r/e),o=(r,s,i)=>`${r},${s},${i}`;return{snap(r){let s=t(r[0]),i=t(r[1]),c=t(r[2]);for(let f=-1;f<=1;f++)for(let d=-1;d<=1;d++)for(let m=-1;m<=1;m++){let p=n.get(o(s+f,i+d,c+m));if(p){for(let h of p)if(ul(r,h)<=e)return[h[0],h[1],h[2]]}}let a=[r[0],r[1],r[2]],l=o(s,i,c),u=n.get(l);return u?u.push(a):n.set(l,[a]),a}}}function Pg(e){let n=new Map,t=r=>Math.floor(r/e),o=(r,s)=>`${r},${s}`;return{snap(r){let s=t(r[0]),i=t(r[1]);for(let u=-1;u<=1;u++)for(let f=-1;f<=1;f++){let d=n.get(o(s+u,i+f));if(d){for(let m of d)if(Math.hypot(r[0]-m[0],r[1]-m[1])<=e)return[m[0],m[1]]}}let c=[r[0],r[1]],a=o(s,i),l=n.get(a);return l?l.push(c):n.set(a,[c]),c}}}function sl(e){return[e.color??"#cccccc",e.texture??"",e.texture&&e.textureWrap?`${e.textureWrap.s}/${e.textureWrap.t}`:"",e.texture&&e.textureAlphaMode?e.textureAlphaMode:"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided"].join("|")}function Js(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0,s=n[0];for(let c=1;c<n.length-1;c++){let a=An(n[c],s),l=An(n[c+1],s),u=ll(a,l);t+=u[0],o+=u[1],r+=u[2]}let i=Math.hypot(t,o,r);return i<=1e-10?null:{normal:[t/i,o/i,r/i],area:i/2}}function Mg(e,n){let t=new Map,o=new Map;for(let r=0;r<e.length;r++){let s=e[r];if(!(!n[r]||s.vertices.length<3))for(let i=0;i<s.vertices.length;i++){let c=Yt(s.vertices[i],s.vertices[(i+1)%s.vertices.length]),a=t.get(c);a?a.push(r):t.set(c,[r])}}for(let r of t.values())for(let s=0;s<r.length;s++)for(let i=s+1;i<r.length;i++){let c=r[s],a=r[i];Ag(e[c],e[a],n[c],n[a])&&(Wc(o,c,a),Wc(o,a,c))}return o}function Ag(e,n,t,o){if(!t||!o||t.materialKey!==o.materialKey||!!e.uvs!=!!n.uvs)return!1;if(ko(e)||ko(n))return ei(e,n);if(!e.uvs||!n.uvs)return!0;let r=ni(e,n);if(!r)return!1;let[s,i,c,a]=r;return Xt(e.uvs[s],n.uvs[c])&&Xt(e.uvs[i],n.uvs[a])}function Sg(e,n,t,o){return t.materialKey!==o.materialKey?!1:ko(e)||ko(n)?ei(e,n):!e.uvs&&!n.uvs&&!e.textureTriangles?.length&&!n.textureTriangles?.length}function ko(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function ei(e,n){if(!e.texture||!n.texture||e.texture!==n.texture||(e.textureWrap?.s??"")!==(n.textureWrap?.s??"")||(e.textureWrap?.t??"")!==(n.textureWrap?.t??"")||(e.textureAlphaMode??"")!==(n.textureAlphaMode??"")||e.material?.texture||n.material?.texture||!e.uvs||!n.uvs||e.uvs.length!==e.vertices.length||n.uvs.length!==n.vertices.length)return!1;let t=ni(e,n);if(!t)return!1;let[o,r,s,i]=t;return Xt(e.uvs[o],n.uvs[s])&&Xt(e.uvs[r],n.uvs[i])}function Wc(e,n,t){let o=e.get(n);o?o.add(t):e.set(n,new Set([t]))}function Cg(e,n,t,o,r,s){let i=[e],c=new Set([e]),a=[e];for(;a.length>0;){let l=a.shift();for(let u of t.get(l)??[]){if(o.has(u)||c.has(u))continue;let f=n[u],d=n[e];!f||!d||f.materialKey===d.materialKey&&Eg([...i,u],n,r,s)&&(i.push(u),c.add(u),a.push(u))}}return i}function Eg(e,n,t,o){let r=il(e,n);return!!r&&al(e,n,r,t,o)}function il(e,n){let t=n[e[0]];if(!t)return null;let o=0,r=0,s=0,i=0,c=0,a=0,l=0;for(let m of e){let p=n[m];if(!p)return null;let h=qe(t.normal,p.normal)<0?-1:1,g=Math.max(p.area,1e-6);o+=p.normal[0]*h*g,r+=p.normal[1]*h*g,s+=p.normal[2]*h*g;for(let y of p.polygon.vertices)i+=y[0],c+=y[1],a+=y[2],l+=1}let u=fl([o,r,s]);if(!u||l===0)return null;let f=cl(e,n),d=Tg(e,n,u,f);if(d){let m=(d.min+d.max)/2;return{normal:u,point:[u[0]*m,u[1]*m,u[2]*m],boundaryVertexKeys:f}}return{normal:u,point:[i/l,c/l,a/l],boundaryVertexKeys:f}}function Tg(e,n,t,o){let r=1/0,s=-1/0;for(let i of e){let c=n[i];if(c)for(let a of c.polygon.vertices){if(!o.has(sn(a)))continue;let l=qe(a,t);r=Math.min(r,l),s=Math.max(s,l)}}return Number.isFinite(r)&&Number.isFinite(s)?{min:r,max:s}:null}function al(e,n,t,o,r){let s=Math.cos(r.maxAngleDeg*Math.PI/180),i=t.boundaryVertexKeys??cl(e,n);for(let c of e){let a=n[c];if(!a||Math.abs(qe(a.normal,t.normal))<s)return!1;for(let l of a.polygon.vertices){let u=i.has(sn(l))?r.maxBoundaryDisplacement:o;if(Math.abs(ti(l,t))>u)return!1}}return!0}function cl(e,n){let t=new Map;for(let r of e){let s=n[r];if(!s)continue;let i=s.polygon.vertices;for(let c=0;c<i.length;c++){let a=i[c],l=i[(c+1)%i.length],u=Yt(a,l),f=t.get(u);f?f.count+=1:t.set(u,{count:1,a,b:l})}}let o=new Set;for(let r of t.values())r.count===1&&(o.add(sn(r.a)),o.add(sn(r.b)));return o}function vg(e,n){let t=r=>oi(r,n),o=Qs(e.textureTriangles,t);return{...e,vertices:e.vertices.map(t),...o?{textureTriangles:o}:{}}}function ni(e,n){for(let t=0;t<e.vertices.length;t++){let o=(t+1)%e.vertices.length;for(let r=0;r<n.vertices.length;r++){let s=(r+1)%n.vertices.length;if($t(e.vertices[t],n.vertices[r])&&$t(e.vertices[o],n.vertices[s]))return[t,o,r,s];if($t(e.vertices[t],n.vertices[s])&&$t(e.vertices[o],n.vertices[r]))return[t,o,s,r]}}return null}function Yt(e,n){let t=sn(e),o=sn(n);return t<o?`${t}|${o}`:`${o}|${t}`}function sn(e){return`${e[0]},${e[1]},${e[2]}`}function $t(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function Xt(e,n){return Math.abs(e[0]-n[0])<=1e-4&&Math.abs(e[1]-n[1])<=1e-4}function An(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function ll(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 qe(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function ul(e,n){return Math.hypot(e[0]-n[0],e[1]-n[1],e[2]-n[2])}function fl(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 ti(e,n){return qe(An(e,n.point),n.normal)}function oi(e,n){let t=ti(e,n);return[e[0]-n.normal[0]*t,e[1]-n.normal[1]*t,e[2]-n.normal[2]*t]}var Ig=36,_g=.15,Og=.2,Vg=650,Rg=15e3,Lg=.25;function ri(e){return!!(e.texture||e.material?.texture||e.uvs?.length||e.textureTriangles?.length)}function ml(e){let n=bn(e);return!n||n.alpha<1?e.trim().toLowerCase():`#${n.rgb.map(t=>Math.max(0,Math.min(255,Math.round(t))).toString(16).padStart(2,"0")).join("")}`}function wg(e){let n=bn(e);return n?{rgb:[Math.max(0,Math.min(255,Math.round(n.rgb[0]))),Math.max(0,Math.min(255,Math.round(n.rgb[1]))),Math.max(0,Math.min(255,Math.round(n.rgb[2])))],alpha:n.alpha}:null}function Ng(e,n){return Math.hypot(e.rgb[0]-n.rgb[0],e.rgb[1]-n.rgb[1],e.rgb[2]-n.rgb[2])}function dl(e){let n=e.rgb[0]/255,t=e.rgb[1]/255,o=e.rgb[2]/255,r=Math.max(n,t,o),s=Math.min(n,t,o),i=r-s,c=0;return i!==0&&(r===n?c=60*((t-o)/i%6):r===t?c=60*((o-n)/i+2):c=60*((n-t)/i+4)),c<0&&(c+=360),{hue:c,saturation:r===0?0:i/r,value:r}}function Dg(e,n){let t=Math.abs(e-n)%360;return t>180?360-t:t}function Fg(e,n){if(e.alpha<1||n.alpha<1)return!1;let t=dl(e),o=dl(n),r=t.saturation<.08,s=o.saturation<.08;if(r||s)return r===s;let i=Math.min(t.value,o.value)<.18?32:18;return Dg(t.hue,o.hue)<=i}function Bg(e,n){let t=new Map;for(let i of e.keys()){let c=wg(i);c&&t.set(i,c)}let o=[],r=new Map,s=Array.from(e.entries()).sort((i,c)=>c[1]-i[1]||i[0].localeCompare(c[0]));for(let[i]of s){let c=t.get(i);if(!c){r.set(i,i);continue}let a=null;for(let l of o){if(!Fg(c,l.parsed))continue;let u=Ng(c,l.parsed);u>n||(!a||u<a.distance)&&(a={color:l.color,distance:u})}a?r.set(i,a.color):(o.push({color:i,parsed:c}),r.set(i,i))}return r}function kg(e,n,t){if(t!=="lossy"||e.animation)return e;let o=n.source;if(!o||o.polygons===e.polygons)return e;let r=n.bakedTextureColorMergeDistance??Ig;if(!Number.isFinite(r)||r<=0)return e;let s=new Map,i=[];for(let u=0;u<e.polygons.length;u+=1){let f=o.polygons[u],d=e.polygons[u];if(!f||!d||f===d||!d.color||!ri(f)||ri(d))continue;let m=ml(d.color);i.push(u),s.set(m,(s.get(m)??0)+1)}if(s.size<2)return e;let c=Bg(s,r),a=!1,l=e.polygons.slice();for(let u of i){let f=l[u],d=ml(f.color),m=c.get(d)??d;m!==d&&(l[u]={...f,color:m},a=!0)}return a?{...e,polygons:l}:e}function Ug(e){let n=0;for(let t of e)ri(t)&&(n+=1);return e.length===0||n/e.length<=Lg}function Gg(e){return!!e.metadata?.stlTopology}function zg(e,n){return n?Math.max(e.simplifyEarlyStopDropRatio??_g,Og):e.simplifyEarlyStopDropRatio}function Xn(e,n={}){return new si(e,n).optimize()}var si=class{constructor(n,t){this.result=n,this.options=t,this.meshResolution=t.meshResolution??"lossy"}optimize(){if(this.result.voxelSource||this.result.animation)return this.result;let n=kg(this.result,this.options,this.meshResolution),t=Gg(n),o=t?"lossless":this.meshResolution,r=this.canSimplify(n.polygons,o),s=r&&n.polygons.length>=Vg&&n.polygons.length<=Rg,i=Kc(n.polygons,{meshResolution:o,useCandidateFirst:s,staticSimplification:r?{simplifyTriangleMeshOptions:this.options.simplifyTriangleMeshOptions,earlyStopDropRatio:zg(this.options,s)}:!1,skipInteriorCull:t});return i===n.polygons?n:{...n,polygons:i}}canSimplify(n,t){return t==="lossy"&&this.options.simplifyTriangleMeshes!==!1&&Ug(n)}};function $g(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0;for(let[_,O,V]of n)t+=_,o+=O,r+=V;let s=1/n.length;t*=s,o*=s,r*=s;let i=n[0],c=n[1],a=n[2],l=c[0]-i[0],u=c[1]-i[1],f=c[2]-i[2],d=a[0]-i[0],m=a[1]-i[1],p=a[2]-i[2],h=u*p-f*m,g=f*d-l*p,y=l*m-u*d,b=Math.hypot(h,g,y);if(b<1e-9)return null;h/=b,g/=b,y/=b;let x=n.length-2,P=new Float64Array(x*9),S=0;for(let _=1;_<n.length-1;_++){let O=n[0],V=n[_],w=n[_+1];P[S++]=O[0],P[S++]=O[1],P[S++]=O[2],P[S++]=V[0],P[S++]=V[1],P[S++]=V[2],P[S++]=w[0],P[S++]=w[1],P[S++]=w[2]}let C=0,T=1/0,A=1/0,M=1/0,E=-1/0,v=-1/0,I=-1/0;for(let[_,O,V]of n){let w=_-t,F=O-o,D=V-r,H=w*w+F*F+D*D;H>C&&(C=H),_<T&&(T=_),_>E&&(E=_),O<A&&(A=O),O>v&&(v=O),V<M&&(M=V),V>I&&(I=V)}return{centroid:[t,o,r],normal:[h,g,y],triFlat:P,bcx:t,bcy:o,bcz:r,br2:C,minX:T,minY:A,minZ:M,maxX:E,maxY:v,maxZ:I}}function Xg(e,n,t,o,r,s,i,c){let a=i[c],l=i[c+1],u=i[c+2],f=i[c+3]-a,d=i[c+4]-l,m=i[c+5]-u,p=i[c+6]-a,h=i[c+7]-l,g=i[c+8]-u,y=r*g-s*h,b=s*p-o*g,x=o*h-r*p,P=f*y+d*b+m*x;if(P>-1e-9&&P<1e-9)return!1;let S=1/P,C=e-a,T=n-l,A=t-u,M=S*(C*y+T*b+A*x);if(M<0||M>1)return!1;let E=T*m-A*d,v=A*f-C*m,I=C*d-T*f,_=S*(o*E+r*v+s*I);return _<0||M+_>1?!1:S*(p*E+h*v+g*I)>.001}function Yg(e,n,t,o,r,s,i){let c=i.bcx-e,a=i.bcy-n,l=i.bcz-t,u=c*o+a*r+l*s,f=c-u*o,d=a-u*r,m=l-u*s;if(f*f+d*d+m*m>i.br2)return!1;let p=i.triFlat,h=p.length;for(let g=0;g<h;g+=9)if(Xg(e,n,t,o,r,s,p,g))return!0;return!1}var Wt=9,Wg=6,Ge=12;function ii(e,n,t,o,r,s){let i=o-e,c=r-n,a=s-t;return i*c+c*a+a*i}function jg(e){let n=[];for(let C=0;C<e.length;C++)e[C]&&n.push(C);let t=n.length,o=new Int32Array(t);for(let C=0;C<t;C++)o[C]=n[C];let r=new Float64Array(t),s=new Float64Array(t),i=new Float64Array(t);for(let C=0;C<t;C++){let T=e[o[C]];r[C]=(T.minX+T.maxX)*.5,s[C]=(T.minY+T.maxY)*.5,i[C]=(T.minZ+T.maxZ)*.5}let c=2*Math.max(1,t)+1,a=new Float64Array(c*Wt),l=0,u=new Float64Array(Ge),f=new Float64Array(Ge),d=new Float64Array(Ge),m=new Float64Array(Ge),p=new Float64Array(Ge),h=new Float64Array(Ge),g=new Int32Array(Ge),y=new Float64Array(Ge-1),b=new Int32Array(Ge-1),x=new Float64Array(Ge-1),P=new Int32Array(Ge-1);function S(C,T){let A=l++,M=A*Wt,E=T-C,v=1/0,I=1/0,_=1/0,O=-1/0,V=-1/0,w=-1/0;for(let G=C;G<T;G++){let te=e[o[G]];te.minX<v&&(v=te.minX),te.maxX>O&&(O=te.maxX),te.minY<I&&(I=te.minY),te.maxY>V&&(V=te.maxY),te.minZ<_&&(_=te.minZ),te.maxZ>w&&(w=te.maxZ)}if(a[M]=v,a[M+1]=I,a[M+2]=_,a[M+3]=O,a[M+4]=V,a[M+5]=w,E<=Wg)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let F=1/0,D=1/0,H=1/0,K=-1/0,Q=-1/0,ne=-1/0;for(let G=C;G<T;G++)r[G]<F&&(F=r[G]),r[G]>K&&(K=r[G]),s[G]<D&&(D=s[G]),s[G]>Q&&(Q=s[G]),i[G]<H&&(H=i[G]),i[G]>ne&&(ne=i[G]);let ee=K-F,ue=Q-D,U=ne-H;if(ee===0&&ue===0&&U===0)return a[M+6]=1,a[M+7]=C,a[M+8]=T,A;let $=ii(v,I,_,O,V,w),k=$>0?1/$:0,L=E+1,W=0,Y=0;for(let G=0;G<3;G++){let te=G===0?F:G===1?D:H,ce=G===0?ee:G===1?ue:U;if(ce===0)continue;let ye=G===0?r:G===1?s:i,he=Ge/ce;u.fill(1/0),f.fill(1/0),d.fill(1/0),m.fill(-1/0),p.fill(-1/0),h.fill(-1/0),g.fill(0);for(let N=C;N<T;N++){let X=(ye[N]-te)*he|0;X>=Ge&&(X=Ge-1);let oe=e[o[N]];oe.minX<u[X]&&(u[X]=oe.minX),oe.maxX>m[X]&&(m[X]=oe.maxX),oe.minY<f[X]&&(f[X]=oe.minY),oe.maxY>p[X]&&(p[X]=oe.maxY),oe.minZ<d[X]&&(d[X]=oe.minZ),oe.maxZ>h[X]&&(h[X]=oe.maxZ),g[X]++}let be=1/0,ge=1/0,Se=1/0,De=-1/0,R=-1/0,z=-1/0,j=0;for(let N=0;N<Ge-1;N++)u[N]<be&&(be=u[N]),m[N]>De&&(De=m[N]),f[N]<ge&&(ge=f[N]),p[N]>R&&(R=p[N]),d[N]<Se&&(Se=d[N]),h[N]>z&&(z=h[N]),j+=g[N],y[N]=ii(be,ge,Se,De,R,z),b[N]=j;let J=1/0,le=1/0,re=1/0,pe=-1/0,xe=-1/0,ie=-1/0,ae=0;for(let N=Ge-2;N>=0;N--){let X=N+1;u[X]<J&&(J=u[X]),m[X]>pe&&(pe=m[X]),f[X]<le&&(le=f[X]),p[X]>xe&&(xe=p[X]),d[X]<re&&(re=d[X]),h[X]>ie&&(ie=h[X]),ae+=g[X],x[N]=ii(J,le,re,pe,xe,ie),P[N]=ae}for(let N=0;N<Ge-1;N++){if(b[N]===0||P[N]===0)continue;let X=.125+(y[N]*b[N]+x[N]*P[N])*k;X<L&&(L=X,W=G,Y=te+(N+1)/he)}}let se=W===0?r:W===1?s:i,Z=C,B=T-1;for(;Z<=B;)if(se[Z]<Y)Z++;else{let G=o[Z];o[Z]=o[B],o[B]=G;let te=r[Z];r[Z]=r[B],r[B]=te;let ce=s[Z];s[Z]=s[B],s[B]=ce;let ye=i[Z];i[Z]=i[B],i[B]=ye,B--}let q=Z;(q===C||q===T)&&(q=C+T>>1),a[M+6]=0;let me=S(C,q),fe=S(q,T);return a[A*Wt+7]=me,a[A*Wt+8]=fe,A}return t>0&&S(0,t),{data:a,nodeCount:l,polyIndices:o,meta:e}}function Kg(e,n,t,o,r,s,i,c,a){if(c.nodeCount===0)return!1;let{data:l,polyIndices:u,meta:f}=c,d=o!==0?1/o:o>=0?1/0:-1/0,m=r!==0?1/r:r>=0?1/0:-1/0,p=s!==0?1/s:s>=0?1/0:-1/0,h=0;for(a[h++]=0;h>0;){let y=a[--h]*Wt,b=(l[y]-e)*d,x=(l[y+3]-e)*d,P=b<x?b:x,S=b<x?x:b,C=(l[y+1]-n)*m,T=(l[y+4]-n)*m,A=C<T?C:T,M=C<T?T:C;if(P>M||A>S)continue;A>P&&(P=A),M<S&&(S=M);let E=(l[y+2]-t)*p,v=(l[y+5]-t)*p,I=E<v?E:v,_=E<v?v:E;if(!(P>_||I>S)&&(_<S&&(S=_),!(S<.001)))if(l[y+6]===1){let O=l[y+7]|0,V=l[y+8]|0;for(let w=O;w<V;w++){let F=u[w];if(F===i)continue;let D=f[F];if(D&&Yg(e,n,t,o,r,s,D))return!0}}else a[h++]=l[y+7]|0,a[h++]=l[y+8]|0}return!1}function pl(e,n,t){let o=new Set;if(e.length<2)return o;let r=Math.hypot(n[0],n[1],n[2]);if(r<1e-9)return o;let s=n[0]/r,i=n[1]/r,c=n[2]/r,a=e.map($g);if(t&&t.size>0)for(let f of t)a[f]=null;let l=jg(a),u=new Int32Array(Math.max(64,l.nodeCount));for(let f=0;f<e.length;f++){let d=a[f];if(!d)continue;let m=d.centroid[0]+d.normal[0]*.001,p=d.centroid[1]+d.normal[1]*.001,h=d.centroid[2]+d.normal[2]*.001;d.normal[0]*s+d.normal[1]*i+d.normal[2]*c<=0||Kg(m,p,h,s,i,c,f,l,u)&&o.add(f)}return o}var jt=1e-5,ai=.001,ci=6;function Uo(e){let n=e.vertices;if(n.length<3)return null;let t=n[0],o=0,r=0,s=0;for(let c=1;c+1<n.length;c++){let a=n[c],l=n[c+1],u=a[1]-t[1],f=a[0]-t[0],d=a[2]-t[2],m=l[1]-t[1],p=l[0]-t[0],h=l[2]-t[2];o-=f*h-d*p,r-=d*m-u*h,s-=u*p-f*m}let i=Math.hypot(o,r,s);return i<1e-9?null:[o/i,r/i,s/i]}function li(e,n){let t=n.meshRotation,o=t?_n(e,t):e;return yo(o,n.rotX,0,n.rotY)[2]}function pt(e,n,t=jt){return li(e,n)>t}function gl(e,n,t=jt){let o=Uo(e);return o===null||pt(o,n,t)}function ui(e){return`${e[0].toFixed(4)},${e[1].toFixed(4)},${e[2].toFixed(4)}`}function fi(e){let n=new Map;for(let t of e){if(!t)continue;let o=ui(t);n.has(o)||n.set(o,t)}return Array.from(n,([t,o])=>({key:t,normal:o}))}function hl(e){return fi(e.map(Uo))}function mi(e,n=ai){let t=Math.abs(e[0]),o=Math.abs(e[1]),r=Math.abs(e[2]),s=Math.max(t,o,r);return s>1-n&&t+o+r-s<n}function yl(e){return e.length<=ci&&e.every(({normal:n})=>mi(n))}function bl(e,n,t=jt){let o=[];for(let{key:r,normal:s}of e)pt(s,n,t)&&o.push(r);return o.sort(),o.join("|")}function di(e,n,t,o,r){let s=(p,h,g)=>{let y=[0,0,0];return y[e]=p,y[(e+1)%3]=h,y[(e+2)%3]=g,y},i=s(n,-o,-o),c=s(n,o,-o),a=s(n,o,o),l=s(n,-o,o),u=s(t,-o,-o),f=s(t,o,-o),d=s(t,o,o),m=s(t,-o,o);return[{vertices:[i,c,a,l],color:r},{vertices:[u,f,d,m],color:r},{vertices:[i,c,f,u],color:r},{vertices:[c,a,d,f],color:r},{vertices:[a,l,m,d],color:r},{vertices:[l,i,u,m],color:r}]}function pi(e={}){let n=e.size??5,t=e.thickness??.025,o=e.negative??!1,r=e.xColor??"#ff3a3a",s=e.yColor??"#3aff3a",i=e.zColor??"#3a8aff",c=n*t/2,a=o?-n:0;return[...di(0,a,n,c,r),...di(1,a,n,c,s),...di(2,a,n,c,i)]}var Hg=["right","left","front","back","top","bottom"];function gi(e){return[e[0],e[1],e[2]]}function xl(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 Zg(e){if(e.min&&e.max)return xl(e.min,e.max);let n=e.center?gi(e.center):[0,0,0],t;typeof e.size=="number"?t=[e.size,e.size,e.size]:e.size?t=gi(e.size):t=[1,1,1];let o=[t[0]/2,t[1]/2,t[2]/2];return xl([n[0]-o[0],n[1]-o[1],n[2]-o[2]],[n[0]+o[0],n[1]+o[1],n[2]+o[2]])}function qg(e,n){let[t,o,r]=e,[s,i,c]=n;return[{name:"right",vertices:[[s,o,r],[s,i,r],[s,i,c],[s,o,c]]},{name:"left",vertices:[[t,i,r],[t,o,r],[t,o,c],[t,i,c]]},{name:"front",vertices:[[s,i,r],[t,i,r],[t,i,c],[s,i,c]]},{name:"back",vertices:[[t,o,r],[s,o,r],[s,o,c],[t,o,c]]},{name:"top",vertices:[[t,o,c],[s,o,c],[s,i,c],[t,i,c]]},{name:"bottom",vertices:[[t,i,r],[s,i,r],[s,o,r],[t,o,r]]}]}function Qg(e,n){let t={...e,...n};return(e.data!==void 0||n?.data!==void 0)&&(t.data={...e.data??{},...n?.data??{}}),t}function Jg(e,n){let t={vertices:e.vertices.map(gi)};return n.color!==void 0&&(t.color=n.color),n.texture!==void 0&&(t.texture=n.texture),n.material!==void 0&&(t.material=n.material),n.uvs!==void 0&&(t.uvs=n.uvs.map(o=>[o[0],o[1]])),n.data!==void 0&&(t.data={...n.data}),t}function hi(e={}){let{min:n,max:t}=Zg(e);if(n[0]===t[0]||n[1]===t[1]||n[2]===t[2])return[];let o={color:e.color??"#ffffff",texture:e.texture,material:e.material,uvs:e.uvs,data:e.data},r=qg(n,t),s=new Map(r.map(c=>[c.name,c])),i=[];for(let c of Hg){let a=e.faces?.[c];if(a===!1)continue;let l=s.get(c);l&&i.push(Jg(l,Qg(o,a)))}return i}function Pl(e,n,t,o){let r=[0,0,0];return r[e]=n,r[(e+1)%3]=t,r[(e+2)%3]=o,r}function e0(e,n,t,o,r){let s=(p,h,g)=>Pl(e,p,h,g),i=s(n,-o,-o),c=s(n,o,-o),a=s(n,o,o),l=s(n,-o,o),u=s(t,-o,-o),f=s(t,o,-o),d=s(t,o,o),m=s(t,-o,o);return[{vertices:[i,c,a,l],color:r},{vertices:[u,f,d,m],color:r},{vertices:[i,c,f,u],color:r},{vertices:[c,a,d,f],color:r},{vertices:[a,l,m,d],color:r},{vertices:[l,i,u,m],color:r}]}function n0(e,n,t,o,r){let s=(f,d,m)=>Pl(e,f,d,m),i=s(n,-o,-o),c=s(n,o,-o),a=s(n,o,o),l=s(n,-o,o),u=s(t,0,0);return[{vertices:[i,c,a,l],color:r},{vertices:[i,c,u],color:r},{vertices:[c,a,u],color:r},{vertices:[a,l,u],color:r},{vertices:[l,i,u],color:r}]}function t0(e){return e.map(n=>({...n,vertices:[...n.vertices].reverse()}))}function yi(e){let n=e.axis,t=e.sign??1,o=e.shaftLength??4,r=e.shaftHalfThickness??.05,s=e.headLength??.8,i=e.headHalfThickness??.2,c=e.color??"#ffffff",a=e.shaft??!0,l=o*t,u=Math.min(0,l),f=Math.max(0,l),d=l,m=(o+s)*t,p=n0(n,d,m,i,c),h=t===-1?t0(p):p;return a?[...e0(n,u,f,r,c),...h]:h}function Go(e,n,t){let o=[0,0,0],r=(e+1)%3,s=(e+2)%3;return o[r]=Math.cos(t)*n,o[s]=Math.sin(t)*n,o}function bi(e){let n=e.axis,t=e.radius,o=e.halfThickness??Math.max(.05,t*.04),r=e.segments??32,s=e.color??"#ffffff",i=t-o,c=t+o,a=[];for(let l=0;l<r;l++){let u=l/r*Math.PI*2,f=(l+1)/r*Math.PI*2,d=Go(n,i,u),m=Go(n,i,f),p=Go(n,c,u),h=Go(n,c,f);a.push({vertices:[d,p,h,m],color:s})}return a}function xi(e){let n=e.axis,t=e.outerRadius,o=e.color??"#ffffff",r=(n+1)%3,s=(n+2)%3,i=(c,a)=>{let l=[0,0,0];return l[n]=0,l[r]=c,l[s]=a,l};return[{vertices:[i(-t,-t),i(t,-t),i(t,t),i(-t,t)],color:o}]}function Pi(e){let n=e.axis,t=e.size??.4,o=e.offset??t*2,r=typeof o=="number"?o:o[0],s=typeof o=="number"?o:o[1],i=e.along??0,c=e.color??"#ffffff",a=(n+1)%3,l=(n+2)%3,u=(f,d)=>{let m=[0,0,0];return m[n]=i,m[a]=r+f,m[l]=s+d,m};return[{vertices:[u(-t,-t),u(t,-t),u(t,t),u(-t,t)],color:c}]}function Mi(e){let{center:n,size:t,color:o="#ffffff"}=e,[r,s,i]=n,c=[[r+t,s,i],[r-t,s,i],[r,s+t,i],[r,s-t,i],[r,s,i+t],[r,s,i-t]];return[[0,2,4],[2,1,4],[1,3,4],[3,0,4],[2,0,5],[1,2,5],[3,1,5],[0,3,5]].map(l=>({vertices:[c[l[0]],c[l[1]],c[l[2]]],color:o}))}function Ai(e={}){let{radius:n=50,color:t="#cccccc"}=e,o=Math.min(Math.max(0,Math.floor(e.subdivisions??1)),3),r=(1+Math.sqrt(5))/2,s=[[-1,r,0],[1,r,0],[-1,-r,0],[1,-r,0],[0,-1,r],[0,1,r],[0,-1,-r],[0,1,-r],[r,0,-1],[r,0,1],[-r,0,-1],[-r,0,1]].map(([a,l,u])=>{let f=Math.sqrt(a*a+l*l+u*u);return[a/f,l/f,u/f]}),i=[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]];for(let a=0;a<o;a++){let l=[...s],u=[],f=new Map,d=(m,p)=>{let h=m<p?`${m}-${p}`:`${p}-${m}`,g=f.get(h);if(g!==void 0)return g;let y=s[m],b=s[p],x=(y[0]+b[0])/2,P=(y[1]+b[1])/2,S=(y[2]+b[2])/2,C=Math.sqrt(x*x+P*P+S*S);x/=C,P/=C,S/=C;let T=l.length;return l.push([x,P,S]),f.set(h,T),T};for(let[m,p,h]of i){let g=d(m,p),y=d(p,h),b=d(h,m);u.push([m,g,b]),u.push([p,y,g]),u.push([h,b,y]),u.push([g,y,b])}s=l,i=u}let c=s.map(([a,l,u])=>[a*n,l*n,u*n]);return i.map(([a,l,u])=>({vertices:[c[a],c[l],c[u]],color:t}))}function Si(e={}){let{size:n=100,color:t="#cccccc"}=e,o=n/Math.sqrt(3),r=[[o,o,o],[o,-o,-o],[-o,o,-o],[-o,-o,o]];return[[0,1,2],[0,3,1],[0,2,3],[1,3,2]].map(([i,c,a])=>({vertices:[r[i],r[c],r[a]],color:t}))}function Ci(e={}){let{size:n=100,color:t="#cccccc"}=e,o=(1+Math.sqrt(5))/2,r=n/Math.sqrt(1+o*o),s=[[-1,o,0],[1,o,0],[-1,-o,0],[1,-o,0],[0,-1,o],[0,1,o],[0,-1,-o],[0,1,-o],[o,0,-1],[o,0,1],[-o,0,-1],[-o,0,1]].map(([c,a,l])=>[c*r,a*r,l*r]);return[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]].map(([c,a,l])=>({vertices:[s[c],s[a],s[l]],color:t}))}function Ei(e={}){let{size:n=100,color:t="#cccccc"}=e,o=(1+Math.sqrt(5))/2,r=1/o,s=n/Math.sqrt(3),c=[[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1],[0,r,o],[0,-r,o],[0,r,-o],[0,-r,-o],[r,o,0],[-r,o,0],[r,-o,0],[-r,-o,0],[o,0,r],[o,0,-r],[-o,0,r],[-o,0,-r]].map(([l,u,f])=>[l*s,u*s,f*s]);return[[12,13,4,8,0],[0,8,9,2,16],[16,17,1,12,0],[1,10,5,13,12],[17,3,11,10,1],[2,9,6,15,14],[2,14,3,17,16],[14,15,7,11,3],[18,6,9,8,4],[4,13,5,19,18],[5,10,11,7,19],[18,19,7,15,6]].map(l=>({vertices:l.map(u=>c[u]),color:t}))}function Kt(e={}){let{radius:n=50,height:t=100,radialSegments:o=12,color:r="#cccccc"}=e,s=e.radiusTop??n,i=t/2,c=-i,a=i,l=Math.max(3,o),u=[],f=Array.from({length:l+1},(p,h)=>h/l*Math.PI*2),d=n<=1e-6,m=s<=1e-6;for(let p=0;p<l&&!(d&&m);p++){let h=f[p],g=f[p+1],y=Math.cos(h)*n,b=Math.sin(h)*n,x=Math.cos(g)*n,P=Math.sin(g)*n,S=Math.cos(h)*s,C=Math.sin(h)*s,T=Math.cos(g)*s,A=Math.sin(g)*s,M=[y,b,c],E=[x,P,c],v=[T,A,a],I=[S,C,a];if(m){let _=[0,0,a];u.push({vertices:[M,E,_],color:r})}else if(d){let _=[0,0,c];u.push({vertices:[_,v,I],color:r})}else u.push({vertices:[M,E,v,I],color:r})}if(n>1e-6){let p=[0,0,c];for(let h=0;h<l;h++){let g=f[h],y=f[h+1],b=[Math.cos(g)*n,Math.sin(g)*n,c],x=[Math.cos(y)*n,Math.sin(y)*n,c];u.push({vertices:[[...p],[...x],[...b]],color:r})}}if(s>1e-6){let p=[0,0,a];for(let h=0;h<l;h++){let g=f[h],y=f[h+1],b=[Math.cos(g)*s,Math.sin(g)*s,a],x=[Math.cos(y)*s,Math.sin(y)*s,a];u.push({vertices:[[...p],[...b],[...x]],color:r})}}return u}function Ti(e={}){return Kt({...e,radiusTop:0})}function zo(e,n,t,o){let r=Math.sin(e),s=Math.cos(e),i=Math.sin(n),c=Math.cos(n);return[(t+o*c)*s,(t+o*c)*r,o*i]}function vi(e={}){let{radius:n=50,tube:t=15,radialSegments:o=12,tubularSegments:r=16,color:s="#cccccc"}=e,i=Math.max(3,o),c=Math.max(3,r),a=[];for(let l=0;l<i;l++){let u=l/i*Math.PI*2,f=(l+1)/i*Math.PI*2;for(let d=0;d<c;d++){let m=d/c*Math.PI*2,p=(d+1)/c*Math.PI*2,h=zo(u,m,n,t),g=zo(f,m,n,t),y=zo(f,p,n,t),b=zo(u,p,n,t);a.push({vertices:[h,g,y,b],color:s})}}return a}var Ml=.01,Al=.01;function Sl(e,n){let t=Math.hypot(e[0],e[1],e[2])||1,o=e[0]/t,r=e[1]/t,s=e[2]/t,i=Math.sign(s||1)*Math.max(Math.abs(s),.01),c=n,a=.01;return[1,0,0,0,0,1,0,0,-o/i,-r/i,a,0,c*o/i,c*r/i,c*(1-a),1]}function Cl(e,n){let t=Math.hypot(n[0],n[1],n[2])||1,o=n[0]/t,r=n[1]/t,s=n[2]/t;return e[0]*o+e[1]*r+e[2]*s>0}function $o(e){if(e.length<=1)return e.map(i=>[i[0],i[1]]);let t=e.map(i=>[i[0],i[1]]);t.sort((i,c)=>i[0]-c[0]||i[1]-c[1]);let o=(i,c,a)=>(c[0]-i[0])*(a[1]-i[1])-(c[1]-i[1])*(a[0]-i[0]),r=[];for(let i of t){for(;r.length>=2&&o(r[r.length-2],r[r.length-1],i)<=0;)r.pop();r.push(i)}let s=[];for(let i=t.length-1;i>=0;i--){let c=t[i];for(;s.length>=2&&o(s[s.length-2],s[s.length-1],c)<=0;)s.pop();s.push(c)}return r.pop(),s.pop(),r.concat(s)}function Ii(e){let n=0,t=e.length;for(let o=0;o<t;o++){let r=e[o],s=e[(o+1)%t];n+=r[0]*s[1]-s[0]*r[1]}return n/2}function fn(e){let n=e.map(t=>[t[0],t[1]]);return Ii(n)<0&&n.reverse(),n}function El(e,n,t){let o=Math.hypot(n[0],n[1],n[2])||1,r=n[0]/o,s=n[1]/o,i=n[2]/o,c=Math.sign(i||1)*Math.max(Math.abs(i),.01),a=e[2]-t;return[e[0]-r/c*a,e[1]-s/c*a]}function Sn(e,n){if(e.length===0||n.length<3)return[];let t=e.map(r=>[r[0],r[1]]),o=n.length;for(let r=0;r<o;r++){if(t.length===0)return[];let s=t;t=[];let i=n[r],c=n[(r+1)%o],a=f=>(c[0]-i[0])*(f[1]-i[1])-(c[1]-i[1])*(f[0]-i[0])>=0,l=(f,d)=>{let m=i[0],p=i[1],h=c[0],g=c[1],y=f[0],b=f[1],x=d[0],P=d[1],S=(m-h)*(b-P)-(p-g)*(y-x);if(Math.abs(S)<1e-12)return[f[0],f[1]];let C=((m-y)*(b-P)-(p-b)*(y-x))/S;return[m+C*(h-m),p+C*(g-p)]},u=s.length;for(let f=0;f<u;f++){let d=s[f],m=s[(f+u-1)%u],p=a(d),h=a(m);p?(h||t.push(l(m,d)),t.push([d[0],d[1]])):h&&t.push(l(m,d))}}return t}function Xo(e){if(!Number.isFinite(e)||e<=0)throw new Error("PolyCSS world unit size must be a positive finite number.");return e}function _i(e,n=50){return e*Xo(n)}function Oi(e,n=50){return e/Xo(n)}function Qe(e,n=50){let t=Xo(n);return[e[1]*t,e[0]*t,e[2]*t]}function Vi(e,n=50){let t=Xo(n);return[e[1]/t,e[0]/t,e[2]/t]}function Yo(e){return[e[1],e[0],e[2]]}function Ri(e){return e?.direction?{...e,direction:Yo(e.direction)}:e}function Wo(e){return e==null?[1,1,1]:typeof e=="number"?[e,e,e]:[e[0]??1,e[1]??1,e[2]??1]}function gt(e){let n=Wo(e),t=n[0]===1&&n[1]===1&&n[2]===1;return(o,r)=>{let s=Qe(r),i=o[1]*50,c=o[0]*50,a=o[2]*50,l=t?i:i*n[0],u=t?c:c*n[1],f=t?a:a*n[2];return[l+s[0],u+s[1],f+s[2]]}}function Li(e,n){if(e.length<3||n===0)return e;let t=[],o=e.length;for(let r=0;r<o;r++){let s=e[(r-1+o)%o],i=e[r],c=e[(r+1)%o],a=i[0]-s[0],l=i[1]-s[1],u=c[0]-i[0],f=c[1]-i[1],d=Math.hypot(a,l),m=Math.hypot(u,f);if(d<1e-9||m<1e-9){t.push(i);continue}let p=l/d,h=-a/d,g=f/m,y=-u/m,b=p+g,x=h+y,P=Math.hypot(b,x);if(P<1e-9){t.push(i);continue}let S=b/P,C=x/P,T=S*p+C*h;if(Math.abs(T)<1e-9){t.push(i);continue}let A=n/T;t.push([i[0]+S*A,i[1]+C*A])}return t}var wi=.5,Ni=.001,Di=.5;function jo(e,n,t,o){let r=[];for(let u=0;u<e.length;u++){if(o.has(u))continue;let f=e[u];if(f.vertices.length<3)continue;let d=f.vertices.map(A=>t(A,n)),m=d[0],p=0,h=0,g=0;for(let A=1;A+1<d.length;A++){let M=d[A],E=d[A+1],v=M[0]-m[0],I=M[1]-m[1],_=M[2]-m[2],O=E[0]-m[0],V=E[1]-m[1],w=E[2]-m[2];p+=V*_-w*I,h+=w*v-O*_,g+=O*I-V*v}let y=Math.hypot(p,h,g);if(y<1e-9)continue;let b=[p/y,h/y,g/y],x=null;for(let A=1;A<d.length;A++){let M=d[A],E=M[0]-m[0],v=M[1]-m[1],I=M[2]-m[2],_=Math.hypot(E,v,I);if(_>1e-9){x=[E/_,v/_,I/_];break}}if(!x)continue;let P=x[0]*b[0]+x[1]*b[1]+x[2]*b[2];x=[x[0]-P*b[0],x[1]-P*b[1],x[2]-P*b[2]];let S=Math.hypot(x[0],x[1],x[2]);if(S<1e-9)continue;x=[x[0]/S,x[1]/S,x[2]/S];let C=[b[1]*x[2]-b[2]*x[1],b[2]*x[0]-b[0]*x[2],b[0]*x[1]-b[1]*x[0]],T=b[0]*m[0]+b[1]*m[1]+b[2]*m[2];r.push({face:f,O:m,n:b,u:x,v:C,offset:T,polyIndex:u})}let s=[];for(let u of r){let f=!1;for(let d of s){let m=d.rep;if(!(1-(u.n[0]*m.n[0]+u.n[1]*m.n[1]+u.n[2]*m.n[2])>Ni)&&!(Math.abs(u.offset-m.offset)>Di)){d.faces.push(u),f=!0;break}}f||s.push({rep:u,faces:[u]})}let i=[],c=3,a=u=>`${u[0].toFixed(c)},${u[1].toFixed(c)},${u[2].toFixed(c)}`;for(let u of s){if(u.faces.length===1){l(u.faces);continue}let f=u.faces.map(g=>{let y=new Set;for(let b of g.face.vertices)y.add(a(t(b,n)));return y}),d=u.faces.map((g,y)=>y),m=g=>{for(;d[g]!==g;)d[g]=d[d[g]],g=d[g];return g},p=(g,y)=>{let b=m(g),x=m(y);b!==x&&(d[b]=x)};for(let g=0;g<u.faces.length;g++)for(let y=g+1;y<u.faces.length;y++){let b=f[g],x=f[y],P=0;for(let S of b)if(x.has(S)&&(P++,P>=2))break;P>=2&&p(g,y)}let h=new Map;for(let g=0;g<u.faces.length;g++){let y=m(g),b=h.get(y);b||(b=[],h.set(y,b)),b.push(u.faces[g])}for(let g of h.values())l(g)}function l(u){let f=u[0],{O:d,n:m,u:p,v:h}=f,g=[],y=[],b=[];for(let S of u){let C=[];for(let T of S.face.vertices){let A=t(T,n),M=A[0]-d[0],E=A[1]-d[1],v=A[2]-d[2],I=[M*p[0]+E*p[1]+v*p[2],M*h[0]+E*h[1]+v*h[2]];g.push(I),C.push(I)}C.length>=3&&(y.push(fn(C)),b.push(S.polyIndex))}if(g.length<3)return;let x=$o(g);if(x.length<3)return;let P=Li(fn(x),wi);i.push({O:d,n:m,u:p,v:h,outlineUv:P,memberPolysUv:y,memberPolyIndices:b})}return i}function Tl(e){let n=typeof e.scale=="number"?e.scale:e.scale?.[0]??1,t=typeof e.scale=="number"?e.scale:e.scale?.[1]??1,o=typeof e.scale=="number"?e.scale:e.scale?.[2]??1,r=n!==1||t!==1||o!==1,s=!!e.rotation&&(!!e.rotation[0]||!!e.rotation[1]||!!e.rotation[2]),i=e.position?Qe(e.position):[0,0,0],c=[];return(i[0]!==0||i[1]!==0||i[2]!==0)&&c.push(`translate3d(${i[0]}px, ${i[1]}px, ${i[2]}px)`),s&&(e.rotation[0]&&c.push(`rotateY(${-e.rotation[0]}deg)`),e.rotation[1]&&c.push(`rotateX(${-e.rotation[1]}deg)`),e.rotation[2]&&c.push(`rotateZ(${-e.rotation[2]}deg)`)),r&&c.push(`scale3d(${n}, ${t}, ${o})`),c.length>0?c.join(" "):void 0}function vl(e={}){let n=e.target??Xe.target,t=e.autoCenterOffset??[0,0,0],o=e.layoutScale??1,r=e.worldUnitPx??50,s=(e.zoom??Xe.zoom)/r*o,i=(e.distance??Xe.distance)*o,c=Qe([n[0]+t[0],n[1]+t[1],n[2]+t[2]],r);return`${i!==0?`translateZ(${-i}px) `:""}scale(${s}) rotateX(${e.rotX??Xe.rotX}deg) rotate(${e.rotY??Xe.rotY}deg) translate3d(${-c[0]}px, ${-c[1]}px, ${-c[2]}px)`}var mn=50,Yn=[.4,-.7,.59],Wn="#ffffff",jn=1,Kn="#ffffff",Hn=.4,Cn=4096,Ht=1,Ko=.1,Ho=1,Zt=Cn*Cn,Zo=Zt*3,ht=2048,qo=4*1024*1024,Qo=16*1024*1024,Jo=.995,er=512,Il=12,Me=.001,de=1e-9,nr=1e-4,tr=.1,qt=.01,_l=.75,Ol=1,Vl=250,Rl=1.5,or=.75,dn=3,rr=2,Qt=4,sr=[1,10,100,1e3,1e4,1e5,1e6],Zn=64,ir=32,ar=96,Ye=64,cr=128,Jt=50,eo=1e-7,pn=16,lr=.9,We=.75,no=.2,ur=.05,fr=256,mr=.6,Ll=1.5;function dr(e){return e===void 0||e==="auto"||!Number.isFinite(e)?1:Math.max(0,Math.min(1,e))}var to=new Map;function qn(e){if(to.has(e)){let t=to.get(e);return t===void 0?null:t}let n=bn(e);return to.size>=er&&to.clear(),to.set(e,n),n}function gn(e){let n=qn(e);return n?{r:n.rgb[0],g:n.rgb[1],b:n.rgb[2]}:{r:255,g:255,b:255}}function pr({r:e,g:n,b:t}){return`${e},${n},${t}`}function oo(e){return qn(e)?.alpha??1}function ro({r:e,g:n,b:t}){let o=r=>Math.round(Math.max(0,Math.min(255,r))).toString(16).padStart(2,"0");return`#${o(e)}${o(n)}${o(t)}`}function so(e,n,t,o){let r=gn(n),s=gn(t),i=ze(r.r/255),c=ze(r.g/255),a=ze(r.b/255),l=ze(s.r/255),u=ze(s.g/255),f=ze(s.b/255);return{r:(i*e+l*o)*yt,g:(c*e+u*o)*yt,b:(a*e+f*o)*yt}}function wl({r:e,g:n,b:t}){let o=r=>Math.round(Math.max(0,Math.min(1,r))*255);return`rgb(${o(e)} ${o(n)} ${o(t)})`}function ze(e){return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)}function o0(e){return e<=.0031308?e*12.92:1.055*Math.pow(e,1/2.4)-.055}var yt=1/Math.PI;function Vn(e,n,t,o,r){let s=gn(e),i=gn(t),c=gn(o),a=ze(s.r/255),l=ze(s.g/255),u=ze(s.b/255),f=ze(i.r/255),d=ze(i.g/255),m=ze(i.b/255),p=ze(c.r/255),h=ze(c.g/255),g=ze(c.b/255),y=a*(f*n+p*r)*yt,b=l*(d*n+h*r)*yt,x=u*(m*n+g*r)*yt,P=M=>Math.max(0,Math.min(255,Math.round(o0(Math.max(0,Math.min(1,M)))*255))),S=P(y),C=P(b),T=P(x),A=oo(e);return A<1?`rgba(${S}, ${C}, ${T}, ${A})`:ro({r:S,g:C,b:T})}function gr(e,n){if(!Number.isFinite(n)||n<=1)return e;let t=qn(e);if(!t)return e;let o=255/Math.max(1,Math.round(n)-1),r=i=>Math.max(0,Math.min(255,Math.round(Math.round(i/o)*o))),s={r:r(t.rgb[0]),g:r(t.rgb[1]),b:r(t.rgb[2])};return t.alpha<1?`rgba(${s.r}, ${s.g}, ${s.b}, ${t.alpha})`:ro(s)}function Nl(e,n){return!!e&&!!n&&e.r===n.r&&e.g===n.g&&e.b===n.b}function Dl(e,n,t){let o=(r,s)=>{let i=s-r;return Math.abs(i)<=t?s:r+Math.sign(i)*t};return{r:o(e.r,n.r),g:o(e.g,n.g),b:o(e.b,n.b)}}function Fl(e,n=1){return n<1?`rgba(${e.r}, ${e.g}, ${e.b}, ${n})`:ro(e)}function Bl(e,n){if(e===void 0)return Number.POSITIVE_INFINITY;if(e===n)return 0;let t=qn(e),o=qn(n);if(!t||!o)return 1;let r=t.rgb[0]-o.rgb[0],s=t.rgb[1]-o.rgb[1],i=t.rgb[2]-o.rgb[2],c=(t.alpha-o.alpha)*255;return Math.sqrt(r*r+s*s+i*i+c*c)/510}function hr(e){let t=c=>Math.round(c/1e-4).toString(36),o=c=>`${t(c[0])},${t(c[1])},${t(c[2])}`,r=(c,a)=>{let l=o(c),u=o(a);return l<u?`${l}|${u}`:`${u}|${l}`},s=new Map;for(let c=0;c<e.length;c++){let a=e[c];if(!a||a.vertices.length<2)continue;let l=a.vertices;for(let u=0;u<l.length;u++){let f=l[u],d=l[(u+1)%l.length],m=r(f,d),p=s.get(m);p?(p.degree+=1,p.degree===2?p.polyB=c:p.polyB=null):s.set(m,{polyA:c,polyB:null,vertA:f,vertB:d,degree:1})}}let i=new Map;for(let[c,a]of s)i.set(c,{polyA:a.polyA,polyB:a.polyB,vertA:a.vertA,vertB:a.vertB});return i}function yr(e,n){let t=Math.hypot(n[0],n[1],n[2])||1,o=n[0]/t,r=n[1]/t,s=n[2]/t;return e.map(i=>i?i[0]*o+i[1]*r+i[2]*s<-1e-6:!0)}function br(e,n){let t=[];for(let l of e.values()){let u=n[l.polyA]??!1;if(l.polyB===null)u&&t.push([l.vertA,l.vertB]);else{let f=n[l.polyB]??!1;u!==f&&t.push([l.vertA,l.vertB])}}if(t.length===0)return[];let o=1e-4,r=l=>Math.round(l/o).toString(36),s=l=>`${r(l[0])},${r(l[1])},${r(l[2])}`,i=t.map(([l,u])=>({a:l,b:u,aKey:s(l),bKey:s(u),used:!1})),c=new Map;for(let l of i){let u=c.get(l.aKey);u||(u=[],c.set(l.aKey,u)),u.push(l);let f=c.get(l.bKey);f||(f=[],c.set(l.bKey,f)),f.push(l)}let a=[];for(let l of i){if(l.used)continue;let u=[l.a],f=l,d=l.aKey,m=l.bKey;f.used=!0;for(let p=0;p<i.length+1;p++){u.push(f.bKey===m?f.b:f.a);let h=c.get(m)??[],g=null;for(let y of h)if(!(y===f||y.used)){g=y;break}if(!g||(g.used=!0,d=m,m=g.aKey===d?g.bKey:g.aKey,f=g,m===l.aKey))break}u.length>=3&&a.push(u)}return a}var kl=40;function Ul(e){let t=c=>Math.round(c/1e-4).toString(36),o=c=>`${t(c[0])},${t(c[1])},${t(c[2])}`,r=(c,a)=>{let l=o(c),u=o(a);return l<u?`${l}|${u}`:`${u}|${l}`},s=new Map;for(let c=0;c<e.length;c++){let a=e[c];if(!a||a.vertices.length<2)continue;let l=a.vertices;for(let u=0;u<l.length;u++){let f=l[u],d=l[(u+1)%l.length],m=r(f,d),p=s.get(m);p||(p=[],s.set(m,p)),p.push(c)}}let i=new Map;for(let c of s.values())if(!(c.length<2))for(let a of c)for(let l of c){if(a===l)continue;let u=i.get(a);u||(u=new Set,i.set(a,u)),u.add(l)}return i}function Gl(e,n,t,o){let r=gt(t),s=!!o&&(o[0]!==0||o[1]!==0||o[2]!==0),i=s?Qe(n):null,c=e.map(a=>{if(!a)return{vertices:[],color:""};let l=a.vertices.map(u=>r(u,n));return s&&i&&o&&(l=l.map(u=>{let f=[u[0]-i[0],u[1]-i[1],u[2]-i[2]],d=_n(f,o);return[d[0]+i[0],d[1]+i[1],d[2]+i[2]]})),{vertices:l,color:""}});return hr(c)}function zl(e,n,t,o,r){let s=[],i=gt(t),c=!!r&&(r[0]!==0||r[1]!==0||r[2]!==0),a=c?Qe(n):null;for(let l=0;l<e.length;l++){if(!o(l))continue;let u=e[l];if(!u)continue;let f=u.vertices.map(S=>i(S,n));c&&a&&r&&(f=f.map(S=>{let C=[S[0]-a[0],S[1]-a[1],S[2]-a[2]],T=_n(C,r);return[T[0]+a[0],T[1]+a[1],T[2]+a[2]]}));let d=1/0,m=1/0,p=1/0,h=-1/0,g=-1/0,y=-1/0;for(let S of f)S[0]<d&&(d=S[0]),S[0]>h&&(h=S[0]),S[1]<m&&(m=S[1]),S[1]>g&&(g=S[1]),S[2]<p&&(p=S[2]),S[2]>y&&(y=S[2]);let b=[[d,m,p],[h,m,p],[d,g,p],[h,g,p],[d,m,y],[h,m,y],[d,g,y],[h,g,y]],x=null,P=0;if(f.length>=3){let S=f[0],C=f[1],T=f[2],A=[C[0]-S[0],C[1]-S[1],C[2]-S[2]],M=[T[0]-S[0],T[1]-S[1],T[2]-S[2]],E=M[1]*A[2]-M[2]*A[1],v=M[2]*A[0]-M[0]*A[2],I=M[0]*A[1]-M[1]*A[0],_=Math.hypot(E,v,I);_>1e-9&&(x=[E/_,v/_,I/_],P=x[0]*S[0]+x[1]*S[1]+x[2]*S[2])}s.push({wv:f,bboxCorners:b,planeN:x,planeOffset:P,polygonIndex:l})}return s}function $l(e,n,t,o,r,s){let i=gt(t),c=!!s&&(s[0]!==0||s[1]!==0||s[2]!==0),a=c?Qe(n):null,f=jo(e,n,c&&a&&s?(g,y)=>{let b=i(g,y),x=[b[0]-a[0],b[1]-a[1],b[2]-a[2]],P=_n(x,s);return[P[0]+a[0],P[1]+a[1],P[2]+a[2]]}:i,o).map((g,y)=>{let{O:b,n:x,u:P,v:S,outlineUv:C,memberPolysUv:T,memberPolyIndices:A}=g,M=1/0,E=1/0,v=-1/0,I=-1/0;for(let Q of C)Q[0]<M&&(M=Q[0]),Q[1]<E&&(E=Q[1]),Q[0]>v&&(v=Q[0]),Q[1]>I&&(I=Q[1]);let _=v-M,O=I-E,V=r*50,w=b[0]+M*P[0]+E*S[0]+V*x[0],F=b[1]+M*P[1]+E*S[1]+V*x[1],D=b[2]+M*P[2]+E*S[2]+V*x[2],K=`matrix3d(${[P[0],P[1],P[2],0,S[0],S[1],S[2],0,x[0],x[1],x[2],0,w,F,D,1].map(Q=>Q.toFixed(4)).join(",")})`;return{O:b,n:x,u:P,v:S,outlineUv:C,memberPolysUv:T,memberPolyIndices:A,minU:M,minV:E,width:_,height:O,matrixCss:K,faceIndex:y,lift:V}}),d=.015,m=.5,p=20,h=new Set;for(let g=0;g<f.length;g++){if(h.has(g))continue;let y=f[g],b=y.n[0]*y.O[0]+y.n[1]*y.O[1]+y.n[2]*y.O[2],x=y.minU,P=y.minV,S=y.minU+y.width,C=y.minV+y.height;for(let T=0;T<f.length;T++){if(T===g||h.has(T))continue;let A=f[T];if(y.n[0]*A.n[0]+y.n[1]*A.n[1]+y.n[2]*A.n[2]<1-d)continue;let E=y.n[0]*A.O[0]+y.n[1]*A.O[1]+y.n[2]*A.O[2];if(E<=b+m||E-b>p)continue;let v=1/0,I=1/0,_=-1/0,O=-1/0;for(let V of A.outlineUv){let w=A.O[0]+V[0]*A.u[0]+V[1]*A.v[0],F=A.O[1]+V[0]*A.u[1]+V[1]*A.v[1],D=A.O[2]+V[0]*A.u[2]+V[1]*A.v[2],H=w-y.O[0],K=F-y.O[1],Q=D-y.O[2],ne=H*y.u[0]+K*y.u[1]+Q*y.u[2],ee=H*y.v[0]+K*y.v[1]+Q*y.v[2];ne<v&&(v=ne),ne>_&&(_=ne),ee<I&&(I=ee),ee>O&&(O=ee)}if(v<=x&&_>=S&&I<=P&&O>=C){let V=Math.max(0,S-x)*Math.max(0,C-P);if(Math.max(0,_-v)*Math.max(0,O-I)>V*2)continue;h.add(g);break}}}return f=f.filter((g,y)=>!h.has(y)),f}function Xl(e){let{receiverPlanes:n,receiverPolygons:t,receiverHasTexture:o,casters:r,lightDir:s,cameraRot:i,ambientLight:c,directionalLight:a,shadow:l}=e,u=Math.hypot(s[0],s[1],s[2])||1,f=s[0]/u,d=s[1]/u,m=s[2]/u,p=c?.color??"#ffffff",h=c?.intensity??.4,g=a?.intensity??1,y=l?.color??"#000000",b=l?.opacity??.25,x=l?.maxExtend??2e3,P=[],S=r.map(A=>{let M=A.edgeOwners;if(!M||A.selfShadowEdgeMap)return null;let E=A.casterPolygonCount??0;if(E<kl||A.items.length<kl)return null;let v=0;for(let O of A.items)O.planeN&&v++;if(v*2<A.items.length)return null;let I=new Array(E).fill(null);for(let O of A.items)O.planeN&&O.polygonIndex<E&&(I[O.polygonIndex]=O.planeN);let _=yr(I,s);return br(M,_)}),C=(A,M,E,v)=>{if(A.length<3)return[];let I=[],_=V=>(V[0]-M[0])*E[0]+(V[1]-M[1])*E[1]+(V[2]-M[2])*E[2],O=(V,w,F,D)=>{let H=(F-v)/(F-D);return[V[0]+H*(w[0]-V[0]),V[1]+H*(w[1]-V[1]),V[2]+H*(w[2]-V[2])]};for(let V=0;V<A.length;V++){let w=A[V],F=A[(V+A.length-1)%A.length],D=_(w),H=_(F),K=D>v,Q=H>v;K?(Q||I.push(O(F,w,H,D)),I.push(w)):Q&&I.push(O(F,w,H,D))}return I},T={rotX:i.rotX,rotY:i.rotY};for(let A of n){let{O:M,n:E,u:v,v:I,outlineUv:_,minU:O,minV:V,width:w,height:F}=A,D=f*E[0]+d*E[1]+m*E[2];if(D<=1e-6||!pt(E,T))continue;let H=ie=>(ie[0]-M[0])*E[0]+(ie[1]-M[1])*E[1]+(ie[2]-M[2])*E[2],K=(ie,ae,N,X)=>{let oe=N/(N-X);return[ie[0]+oe*(ae[0]-ie[0]),ie[1]+oe*(ae[1]-ie[1]),ie[2]+oe*(ae[2]-ie[2])]},Q=ie=>{let ae=ie[0]-M[0],N=ie[1]-M[1],X=ie[2]-M[2],oe=(ae*E[0]+N*E[1]+X*E[2])/D,Te=ie[0]-oe*f,Ce=ie[1]-oe*d,Pe=ie[2]-oe*m,_e=Te-M[0],Oe=Ce-M[1],Ve=Pe-M[2];return[_e*v[0]+Oe*v[1]+Ve*v[2],_e*I[0]+Oe*I[1]+Ve*I[2]]},ne=new Map,ee=0,ue=O,U=V,$=A.minU+w,k=A.minV+F,L=1/0,W=1/0,Y=-1/0,se=-1/0;for(let ie of r)for(let ae of ie.items)for(let N of ae.wv){let X=N[0]-M[0],oe=N[1]-M[1],Te=N[2]-M[2],Ce=X*v[0]+oe*v[1]+Te*v[2],Pe=X*I[0]+oe*I[1]+Te*I[2];Ce<L&&(L=Ce),Ce>Y&&(Y=Ce),Pe<W&&(W=Pe),Pe>se&&(se=Pe)}let Z=Number.isFinite(L)?[[L-x,W-x],[Y+x,W-x],[Y+x,se+x],[L-x,se+x]]:[],B=.05,q=E[0]*M[0]+E[1]*M[1]+E[2]*M[2],me=.0025,fe=5;for(let ie=0;ie<r.length;ie++){let ae=r[ie],N=ae.selfShadowEdgeMap,X=S[ie];if(X!==null){if(X.length===0)continue;for(let oe of X){if(oe.length<3)continue;let Te=C(oe,M,E,B);if(Te.length<3)continue;let Ce=Te.map(Q),Pe=fn(Ce),_e=Z.length===4?Sn(Pe,Z):Pe;if(_e.length<3)continue;let Oe=Sn(_e,_);if(Oe.length<3)continue;let Ve;for(let $e of A.memberPolysUv){let Ee=Sn(Oe,fn($e));Ee.length<3||(Ve||(Ve=ne.get(ae.id),Ve||(Ve={id:ae.id,verts:[],subPolygonIndices:[]},ne.set(ae.id,Ve))),Ve.verts.push(Ee),Ve.subPolygonIndices.push(-1),ee++)}}continue}for(let oe of ae.items){if(N){let Ee=N.get(oe.polygonIndex);if(Ee){let Fe=!1;for(let Ne of A.memberPolyIndices)if(Ee.has(Ne)){Fe=!0;break}if(Fe)continue}}if(oe.planeN&&oe.planeN[0]*f+oe.planeN[1]*d+oe.planeN[2]*m>-1e-6)continue;if(oe.planeN){let Ee=oe.planeN,Fe=Ee[0]*E[0]+Ee[1]*E[1]+Ee[2]*E[2];if(Math.abs(Fe)>1-me){let Ne=Fe>0?1:-1;if(Math.abs(oe.planeOffset-Ne*q)<fe)continue}}let Te=oe.bboxCorners,Ce=!1,Pe=1/0,_e=1/0,Oe=-1/0,Ve=-1/0;for(let Ee=0;Ee<8;Ee++){let Fe=Te[Ee];H(Fe)>B&&(Ce=!0);let Ne=Q(Fe);Ne[0]<Pe&&(Pe=Ne[0]),Ne[0]>Oe&&(Oe=Ne[0]),Ne[1]<_e&&(_e=Ne[1]),Ne[1]>Ve&&(Ve=Ne[1])}if(!Ce||Oe<ue||Pe>$||Ve<U||_e>k)continue;let $e=oe.wv;for(let Ee=1;Ee<$e.length-1;Ee++){let Fe=$e[0],Ne=$e[Ee],ot=$e[Ee+1],rt=H(Fe),hn=H(Ne),Je=H(ot),yn=[],Be=[[Fe,rt],[Ne,hn],[ot,Je]];for(let Ke=0;Ke<3;Ke++){let[ve,en]=Be[Ke],[an,nn]=Be[(Ke+1)%3],He=en>B,cn=nn>B;He&&yn.push(ve),He!==cn&&yn.push(K(ve,an,en,nn))}if(yn.length<3)continue;let je=yn.map(Q),Ae=fn(je),Re=Z.length===4?Sn(Ae,Z):Ae;if(Re.length<3)continue;let Le=Sn(Re,_);if(Le.length<3)continue;let Ie;for(let Ke of A.memberPolysUv){let ve=Sn(Le,fn(Ke));if(!(ve.length<3)){if(N){let en=0,an=1/0,nn=-1/0,He=1/0,cn=-1/0;for(let ln=0;ln<ve.length;ln++){let Ze=ve[ln],it=ve[(ln+1)%ve.length];en+=Ze[0]*it[1]-it[0]*Ze[1],Ze[0]<an&&(an=Ze[0]),Ze[0]>nn&&(nn=Ze[0]),Ze[1]<He&&(He=Ze[1]),Ze[1]>cn&&(cn=Ze[1])}if(Math.abs(en)*.5<5)continue}Ie||(Ie=ne.get(ae.id),Ie||(Ie={id:ae.id,verts:[],subPolygonIndices:[]},ne.set(ae.id,Ie))),Ie.verts.push(ve),Ie.subPolygonIndices.push(oe.polygonIndex),ee++}}}}}if(ee===0||!(w>0)||!(F>0))continue;let G=b;if(o){let ae=g*Math.max(0,D)+Math.max(0,h);if(ae>0){let N=Math.max(0,h)/ae,X=Math.pow(N,1/2.4);G=b*(1-X)}else G=0}let te=A.memberPolyIndices[0]??0,ce=t[te]?.color??"#cccccc",ye=o?y:Vn(ce,0,"#000000",p,h),he=1/0,be=1/0,ge=-1/0,Se=-1/0;for(let ie of ne.values())for(let ae of ie.verts)for(let N of ae)N[0]<he&&(he=N[0]),N[0]>ge&&(ge=N[0]),N[1]<be&&(be=N[1]),N[1]>Se&&(Se=N[1]);he=Math.floor(he-1),be=Math.floor(be-1),ge=Math.ceil(ge+1),Se=Math.ceil(Se+1),he<O&&(he=O),be<V&&(be=V),ge>O+w&&(ge=O+w),Se>V+F&&(Se=V+F);let De=ge-he,R=Se-be;if(!(De>0)||!(R>0))continue;let z=A.lift,j=M[0]+he*v[0]+be*I[0]+z*E[0],J=M[1]+he*v[1]+be*I[1]+z*E[1],le=M[2]+he*v[2]+be*I[2]+z*E[2],pe=`matrix3d(${[v[0],v[1],v[2],0,I[0],I[1],I[2],0,E[0],E[1],E[2],0,j,J,le,1].map(ie=>ie.toFixed(4)).join(",")})`,xe=[];for(let ie of ne.values()){let ae="";for(let N of ie.verts){ae+=`M${(N[0][0]-he).toFixed(1)},${(N[0][1]-be).toFixed(1)}`;for(let X=1;X<N.length;X++)ae+=`L${(N[X][0]-he).toFixed(1)},${(N[X][1]-be).toFixed(1)}`;ae+="Z"}xe.push({casterId:ie.id,d:ae,casterPolygonIndices:ie.subPolygonIndices})}P.push({faceIndex:A.faceIndex,memberPolyIndices:A.memberPolyIndices,matrixCss:pe,width:De,height:R,fill:ye,opacity:G,paths:xe})}return P}var r0=Symbol.for("polycss.animation.triangleFrameSource"),s0=Symbol.for("polycss.animation.triangleFrameTarget"),Bi=2200,xr=2201,ki=2202;function i0(e,n){let t=0,o=1,r=1,s=xr,i=1/0,c=0,a=!1,l=null,u=!0,f=!1,d={clampWhenFinished:!1,get timeScale(){return r},set timeScale(m){r=m},get weight(){return o},set weight(m){o=m},get time(){return t},set time(m){t=m},get isRunning(){return a},get enabled(){return u},set enabled(m){u=m},get paused(){return f},set paused(m){f=m},play(){return a=!0,d},stop(){return a=!1,t=0,c=0,l=null,d},reset(){return t=0,c=0,d},fadeIn(m){return l={from:0,to:1,elapsed:0,duration:m},o=0,d},fadeOut(m){return l={from:o,to:0,elapsed:0,duration:m},d},crossFadeTo(m,p){return d.fadeOut(p),m.fadeIn(p),d},crossFadeFrom(m,p){return m.fadeOut(p),d.fadeIn(p),d},setLoop(m,p){return s=m,i=p,d},setEffectiveTimeScale(m){return r=m,d},setEffectiveWeight(m){return o=m,d}};return d._internal={get clip(){return e},get time(){return t},get weight(){return o},get enabled(){return u},get paused(){return f},get running(){return a},get loopMode(){return s},get repetitions(){return i},get completedReps(){return c},set completedReps(m){c=m},get clampWhenFinished(){return d.clampWhenFinished},get timeScale(){return r},get fade(){return l},advance(m){if(!a)return;if(l){l.elapsed+=m;let h=l.duration>0?Math.min(l.elapsed/l.duration,1):1;if(o=l.from+(l.to-l.from)*h,h>=1&&(o=l.to,l=null,o<=0)){a=!1;return}}if(f)return;let p=e.duration;if(!(p<=0)){if(t+=m*r,s===Bi)t>=p&&(t=d.clampWhenFinished?p:0,a=!1);else if(s===xr){if(t>=p){let h=t%p,g=Math.floor(t/p);c+=g,t=h,i!==1/0&&c>=i&&(t=d.clampWhenFinished?p:0,a=!1)}}else if(p>0){let h=p*2,g=t%h;if(t>=h){let y=Math.floor(t/h);c+=y,i!==1/0&&c>=i?(t=d.clampWhenFinished?p:0,a=!1):t=g}}}},sampleTime(){if(s===ki){let m=e.duration,p=m*2,h=t%p;return h<=m?h:p-h}return t}},d}function a0(e){return e._internal}function Fi(e,n){return typeof n=="number"?e[n]:e.find(t=>t.name===n)}function Yl(e,n){let t=new Map,o=n,r=e;function s(f){let d=Fi(n.clips,f);if(!d)throw new Error(`PolyAnimationMixer: no clip found for key "${f}". Available: ${n.clips.map(p=>p.name).join(", ")}`);let m=t.get(d.index);return m||(m=i0(d,n),t.set(d.index,m)),m}function i(f){let d=Fi(n.clips,f);return d?t.get(d.index)??null:null}function c(f){let d=[];for(let y of t.values()){let b=a0(y);b.advance(f),b.running&&b.enabled&&d.push({internal:b,clip:b.clip})}if(d.length===0)return;if(d.length===1){let{internal:y,clip:b}=d[0],x=y.sampleTime(),P=o[r0]?.(b.name,x);if(P&&r[s0]?.(P))return;let S=n.sample(b.name,x);e.setPolygons(S);return}let m=0;for(let{internal:y}of d)m+=y.weight;if(m<=0)return;let p=d.map(({internal:y,clip:b})=>({polygons:n.sample(b.name,y.sampleTime()),weight:y.weight/m})),h=p[0].polygons;if(h.length===0)return;let g=h.map((y,b)=>{let x=y.vertices.map((P,S)=>{let C=0,T=0,A=0;for(let{polygons:M,weight:E}of p){let v=M[b];if(!v)continue;let I=v.vertices[S];I&&(C+=I[0]*E,T+=I[1]*E,A+=I[2]*E)}return[C,T,A]});return{...y,vertices:x}});e.setPolygons(g)}function a(){for(let f of t.values())f.stop()}function l(f){let d=Fi(n.clips,f);d&&t.delete(d.index)}function u(){t.clear()}return{clipAction:s,existingAction:i,update:c,stopAllAction:a,uncacheClip:l,uncacheRoot:u}}var Gi={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},Zl={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16},c0={0:"POINTS",1:"LINES",2:"LINE_LOOP",3:"LINE_STRIP",4:"TRIANGLES",5:"TRIANGLE_STRIP",6:"TRIANGLE_FAN"},zi=["EXT_meshopt_compression","KHR_meshopt_compression"];function l0(e){return e===5120||e===5121||e===5122||e===5123}function ql(e){let n=globalThis.TextDecoder;return new n().decode(e)}function u0(e){let n=e.indexOf(",");if(n<0)throw new Error("parseGltf: malformed data: URI");let t=e.slice(5,n),o=e.slice(n+1);if(!t.includes(";base64")){let i=decodeURIComponent(o),c=new Uint8Array(i.length);for(let a=0;a<i.length;a++)c[a]=i.charCodeAt(a)&255;return c}let r=globalThis.atob(o),s=new Uint8Array(r.length);for(let i=0;i<r.length;i++)s[i]=r.charCodeAt(i);return s}function f0(e){let n=new DataView(e);if(n.getUint32(0,!0)!==1179937895)throw new Error("parseGltf: not a GLB (bad magic)");let t=n.getUint32(4,!0);if(t!==2)throw new Error(`parseGltf: only glTF v2 supported (got v${t})`);let o=12,r=null,s=null;for(;o<e.byteLength;){let i=n.getUint32(o,!0),c=n.getUint32(o+4,!0),a=o+8;if(c===1313821514){let l=new Uint8Array(e,a,i);r=JSON.parse(ql(l))}else c===5130562&&(s=new Uint8Array(e,a,i));o=a+i}if(!r)throw new Error("parseGltf: no JSON chunk in GLB");return{doc:r,bin:s}}function Wl(e){if(!e)return null;let n=/^(\d+)\.(\d+)(?:\D.*)?$/.exec(e);return n?[Number(n[1]),Number(n[2])]:null}function m0(e){let n=Wl(e.asset?.version);if(!n||n[0]!==2)throw new Error(`parseGltf: only glTF asset v2 supported (got ${e.asset?.version??"missing"})`);let t=Wl(e.asset?.minVersion);if(e.asset?.minVersion&&(!t||t[0]>2||t[0]===2&&t[1]>0))throw new Error(`parseGltf: glTF asset requires minVersion ${e.asset.minVersion}`)}function d0(e,n,t){let o=e.buffers??[],r=new Set(e.extensionsRequired??[]),s=zi.some(i=>r.has(i));return o.map((i,c)=>{let a=i.uri;if(a){if(a.startsWith("data:"))return u0(a);if(t){let l=t(a);if(l instanceof Uint8Array)return l;throw new Error("parseGltf: resolveBuffer returned a Promise; use parseGltf via async if your buffers are external")}throw new Error(`parseGltf: external buffer URI "${a}" \u2014 provide options.resolveBuffer`)}if(c===0&&n)return n;if(s&&zi.some(l=>i.extensions?.[l]))return new Uint8Array(0);throw new Error(`parseGltf: buffer[${c}] has no uri and no GLB BIN chunk`)})}function ao(e,n,t){let o=e.bufferViews?.[t],r=o?n[o.buffer]:void 0;if(!o||!r)throw new Error(`parseGltf: bad bufferView ${t}`);let s=o.byteOffset??0;if(s<0||o.byteLength<0||s+o.byteLength>r.byteLength)throw new Error(`parseGltf: bufferView ${t} outside buffer ${o.buffer}`);return{buffer:r,view:o}}function Ql(e,n,t,o){if(e.count<=0)return;let r=e.byteOffset??0,s=r+t*(e.count-1)+o;if(r<0||s>n.byteLength)throw new Error("parseGltf: accessor does not fit bufferView")}function Jl(e,n){return e.byteStride!==void 0&&e.byteStride>0?e.byteStride:n}function p0(e,n,t){if(t||e===5126)return new Float32Array(n);switch(e){case 5120:return new Int8Array(n);case 5121:return new Uint8Array(n);case 5122:return new Int16Array(n);case 5123:return new Uint16Array(n);case 5125:return new Uint32Array(n);default:throw new Error(`parseGltf: unhandled componentType ${e}`)}}function Pr(e,n,t){let o=e.accessors?.[t];if(!o)throw new Error(`parseGltf: bad accessor ${t}`);let r=Gi[o.componentType],s=Zl[o.type];if(!r||!s)throw new Error(`parseGltf: unsupported accessor type ${o.type}/${o.componentType}`);if(o.sparse||o.normalized||o.bufferView===void 0){let{values:g}=co(e,n,t);return{array:p0(o.componentType,g,o.normalized),count:o.count,componentCount:s}}let{buffer:i,view:c}=ao(e,n,o.bufferView),a=(c.byteOffset??0)+(o.byteOffset??0),l=o.count*s,u=r*s,f=Jl(c,u);if(Ql(o,c,f,u),f===u){let g=i.buffer.slice(i.byteOffset+a,i.byteOffset+a+l*r),y;switch(o.componentType){case 5120:y=new Int8Array(g);break;case 5121:y=new Uint8Array(g);break;case 5122:y=new Int16Array(g);break;case 5123:y=new Uint16Array(g);break;case 5125:y=new Uint32Array(g);break;case 5126:y=new Float32Array(g);break;default:throw new Error(`parseGltf: unhandled componentType ${o.componentType}`)}return{array:y,count:o.count,componentCount:s}}let d;switch(o.componentType){case 5120:d=new Int8Array(l);break;case 5121:d=new Uint8Array(l);break;case 5122:d=new Int16Array(l);break;case 5123:d=new Uint16Array(l);break;case 5125:d=new Uint32Array(l);break;case 5126:d=new Float32Array(l);break;default:throw new Error(`parseGltf: unhandled componentType ${o.componentType}`)}let m=new DataView(i.buffer),p=i.byteOffset+a,h=0;for(let g=0;g<o.count;g++){let y=p+g*f;for(let b=0;b<s;b++)d[h++]=Sr(m,y+b*r,o.componentType)}return{array:d,count:o.count,componentCount:s}}function Sr(e,n,t){switch(t){case 5120:return e.getInt8(n);case 5121:return e.getUint8(n);case 5122:return e.getInt16(n,!0);case 5123:return e.getUint16(n,!0);case 5125:return e.getUint32(n,!0);case 5126:return e.getFloat32(n,!0);default:throw new Error(`parseGltf: unhandled componentType ${t}`)}}function jl(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 co(e,n,t){let o=e.accessors?.[t];if(!o)throw new Error(`parseGltf: bad accessor ${t}`);let r=Gi[o.componentType],s=Zl[o.type];if(!r||!s)throw new Error(`parseGltf: unsupported accessor type ${o.type}/${o.componentType}`);let i=r*s,c=new Array(o.count*s).fill(0);if(o.bufferView!==void 0){let{buffer:a,view:l}=ao(e,n,o.bufferView),u=a.byteOffset+(l.byteOffset??0)+(o.byteOffset??0),f=Jl(l,i);Ql(o,l,f,i);let d=new DataView(a.buffer),m=0;for(let p=0;p<o.count;p++){let h=u+p*f;for(let g=0;g<s;g++){let y=Sr(d,h+g*r,o.componentType);c[m++]=o.normalized?jl(y,o.componentType):y}}}if(o.sparse){let a=o.sparse,l=a.indices,u=Gi[l.componentType];if(l.componentType!==5121&&l.componentType!==5123&&l.componentType!==5125)throw new Error(`parseGltf: unhandled sparse index componentType ${l.componentType}`);let{buffer:f,view:d}=ao(e,n,l.bufferView),m=f.byteOffset+(d.byteOffset??0)+(l.byteOffset??0),p=new DataView(f.buffer),{buffer:h,view:g}=ao(e,n,a.values.bufferView),y=h.byteOffset+(g.byteOffset??0)+(a.values.byteOffset??0),b=new DataView(h.buffer);for(let x=0;x<a.count;x++){let P=Sr(p,m+x*u,l.componentType);for(let S=0;S<s;S++){let C=Sr(b,y+(x*s+S)*r,o.componentType);c[P*s+S]=o.normalized?jl(C,o.componentType):C}}}return{values:c,count:o.count,componentCount:s}}function g0(e,n,t){let o=[],r=[],s=globalThis;for(let i of e.images??[]){if(i.uri){if(t&&!i.uri.startsWith("data:"))try{o.push(new s.URL(i.uri,t).href)}catch{o.push(i.uri)}else o.push(i.uri);continue}if(i.bufferView!==void 0){let c;try{let{buffer:f,view:d}=ao(e,n,i.bufferView),m=d.byteOffset??0;c=f.subarray(m,m+d.byteLength)}catch{o.push("");continue}let a=i.mimeType??"image/png",l=new s.Blob([c],{type:a}),u=s.URL.createObjectURL(l);o.push(u),r.push(u)}else o.push("")}return{urls:o,objectUrls:r}}function Kl(e){switch(e??10497){case 33071:return"clamp-to-edge";case 33648:return"mirrored-repeat";case 10497:return"repeat";default:return"repeat"}}function h0(e,n){let t=n?.sampler!==void 0?e.samplers?.[n.sampler]:void 0;return{s:Kl(t?.wrapS),t:Kl(t?.wrapT)}}function eu(e){switch(e){case"BLEND":return"blend";case"MASK":return"mask";default:return"opaque"}}function y0(e,n){let t=new Map,o=e.materials??[];for(let r=0;r<o.length;r++){let s=o[r].pbrMetallicRoughness?.baseColorTexture?.index;if(s===void 0)continue;let i=e.textures?.[s],c=i?.source;if(c===void 0)continue;let a=n[c];a&&t.set(r,{url:a,wrap:h0(e,i),alphaMode:eu(o[r].alphaMode),texCoord:o[r].pbrMetallicRoughness?.baseColorTexture?.texCoord??0})}return t}function b0(e,n,t){let o=e?.pbrMetallicRoughness?.baseColorFactor;if(!o||o.length<3)return n;let r=a=>Math.max(0,Math.min(1,a)),s=a=>Math.round(r(a)*255).toString(16).padStart(2,"0"),i=a=>Math.round(r(a)*255),c=t==="opaque"?1:r(o[3]??1);return c<1?`rgba(${i(o[0])}, ${i(o[1])}, ${i(o[2])}, ${Math.round(c*1e3)/1e3})`:`#${s(o[0])}${s(o[1])}${s(o[2])}`}var bt=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function nu(e,n){let t=new Array(16);return tu(t,e,n)}function tu(e,n,t){for(let o=0;o<4;o++)for(let r=0;r<4;r++)e[r*4+o]=n[0+o]*t[r*4+0]+n[4+o]*t[r*4+1]+n[8+o]*t[r*4+2]+n[12+o]*t[r*4+3];return e}function Mr(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 ou(e,n,t){let o=e?.[0]??0,r=e?.[1]??0,s=e?.[2]??0,i=n?.[0]??0,c=n?.[1]??0,a=n?.[2]??0,l=n?.[3]??1,u=t?.[0]??1,f=t?.[1]??1,d=t?.[2]??1,m=i+i,p=c+c,h=a+a,g=i*m,y=i*p,b=i*h,x=c*p,P=c*h,S=a*h,C=l*m,T=l*p,A=l*h;return[(1-(x+S))*u,(y+A)*u,(b-T)*u,0,(y-A)*f,(1-(g+S))*f,(P+C)*f,0,(b+T)*d,(P-C)*d,(1-(g+x))*d,0,o,r,s,1]}function x0(e){return e.matrix&&e.matrix.length===16?e.matrix.slice():ou(e.translation,e.rotation,e.scale)}function ru(e,n,t){let o=new Array(Math.min(e.length,n.length));for(let r=0;r<o.length;r++)o[r]=e[r]+(n[r]-e[r])*t;return o}function io(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 P0(e,n,t){let o=io(e),r=io(n),s=o[0]*r[0]+o[1]*r[1]+o[2]*r[2]+o[3]*r[3];if(s<0&&(s=-s,r=[-r[0],-r[1],-r[2],-r[3]]),s>.9995)return io(ru(o,r,t));let i=Math.acos(Math.max(-1,Math.min(1,s))),c=Math.sin(i),a=Math.sin((1-t)*i)/c,l=Math.sin(t*i)/c;return io([o[0]*a+r[0]*l,o[1]*a+r[1]*l,o[2]*a+r[2]*l,o[3]*a+r[3]*l])}function su(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 iu(e){return e.matrix?e.matrix.slice():ou(e.translation,e.rotation,e.scale)}function M0(e){let n=e.scene??0,t=e.scenes?.[n]?.nodes;return t&&t.length>0?t:[]}function au(e,n){let t=e.nodes??[],o=new Array(t.length),r=new Set,s=new Set,i=(a,l)=>{if(a<0||a>=t.length||s.has(a))return;let u=nu(l,n[a]??bt);o[a]=u,r.add(a),s.add(a);for(let f of t[a].children??[])i(f,u);s.delete(a)},c=M0(e);if(c.length>0)for(let a of c)i(a,bt);for(let a=0;a<t.length;a++)r.has(a)||i(a,bt);return o}var cu=Symbol("polycss.gltfAnimationRuntimeInfo"),A0=Symbol.for("polycss.animation.triangleFrameSource");function lu(e,n){return e?e[cu]?.withPolygonFilter(n):void 0}function Ui(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function S0(e,n,t){return Ui(e,n)||Ui(e,t)||Ui(n,t)}function Hl(e,n,t,o,r){if(t===void 0)return;let{values:s,count:i,componentCount:c}=co(e,n,t);if(i!==r||c<1)return;let a=[];for(let l=0;l<i;l++){let u=[];for(let f=0;f<o;f++)u.push(s[l*c+f]??0);a.push(u)}return a}function C0(e,n,t,o){if(t===void 0)return Array.from({length:o},()=>bt.slice());let{values:r,componentCount:s,count:i}=co(e,n,t);if(s!==16)throw new Error(`parseGltf: inverseBindMatrices accessor ${t} is not MAT4`);let c=[];for(let a=0;a<o;a++){let l=Math.min(a,i-1);c.push(r.slice(l*16,l*16+16))}return c}function Ar(e,n){let t=e.componentCount,o=e.interpolation==="CUBICSPLINE"?(n*3+1)*t:n*t;return e.output.slice(o,o+t)}function E0(e,n,t){let o=e.input;if(o.length===0)return[];if(o.length===1||n<=o[0])return Ar(e,0);let r=o.length-1;if(n>=o[r])return Ar(e,r);let s=0,i=r;for(;s+1<i;){let d=s+i>>1;o[d]<=n?s=d:i=d}let c=o[s],a=o[s+1],l=a>c?(n-c)/(a-c):0,u=Ar(e,s),f=Ar(e,s+1);return e.interpolation==="STEP"?u:t==="rotation"?P0(u,f,l):ru(u,f,l)}function uu(e,n){let t=[];for(let o=0;o<(e.animations?.length??0);o++){let r=e.animations[o],s=(r.samplers??[]).map(a=>{let l=co(e,n,a.input),u=co(e,n,a.output);return{input:l.values,output:u.values,componentCount:u.componentCount,interpolation:a.interpolation??"LINEAR"}}),i=[];for(let a of r.channels??[]){let l=a.target.node,u=a.target.path,f=s[a.sampler];l===void 0||!u||!f||u!=="translation"&&u!=="rotation"&&u!=="scale"||i.push({sampler:f,targetNode:l,path:u})}let c=i.reduce((a,l)=>{let u=l.sampler.input;return Math.max(a,u[u.length-1]??0)},0);t.push({info:{index:o,name:r.name??`animation_${o}`,duration:c,channelCount:i.length},channels:i})}return t}function fu(e,n,t,o){let r=t.info.duration,s=r>0?(o%r+r)%r:Math.max(0,o),i=n.map(c=>({translation:c.translation.slice(),rotation:c.rotation.slice(),scale:c.scale.slice(),matrix:c.matrix?c.matrix.slice():void 0}));for(let c of t.channels){let a=i[c.targetNode];if(!a)continue;let l=E0(c.sampler,s,c.path);a.matrix=void 0,c.path==="translation"?a.translation=l.slice(0,3):c.path==="rotation"?a.rotation=io(l.slice(0,4)):c.path==="scale"&&(a.scale=l.slice(0,3))}return au(e,i.map(iu))}function T0(e,n,t,o,r,s){if((e.animations??[]).length===0||t.length===0)return;let c=(e.nodes??[]).map(g=>su(g)),a=c.map(iu),l=au(e,a),u=(e.skins??[]).map(g=>({joints:g.joints??[],inverseBindMatrices:C0(e,n,g.inverseBindMatrices,g.joints?.length??0)})),f=uu(e,n),d=f.map(g=>g.info);if(d.length===0)return;let m=g=>{let y=t.map(b=>new Array(b.triangleMask.length).fill(!1));for(let b of g){let x=o[b];if(!x)return;let P=y[x.sourceIndex];if(!P||x.triangleIndex<0||x.triangleIndex>=P.length)return;P[x.triangleIndex]=!0}return y.map((b,x)=>{let P=t[x],S=new Set,C=0;for(let T=0;T+2<P.indices.length;T+=3,C++)b[C]&&(S.add(P.indices[T]),S.add(P.indices[T+1]),S.add(P.indices[T+2]));return{triangleMask:b,activeVertices:Array.from(S).sort((T,A)=>T-A)}})},p=(g,y,b,x,P,S,C,T,A)=>{let M=r(g),E=r(y),v=r(b),I={vertices:[M,E,v],color:x};return P&&(I.texture=P),P&&S&&(I.textureWrap=S),P&&C&&(I.textureAlphaMode=C),T&&(I.doubleSided=!0),A&&(I.uvs=A),I},h=g=>{let y=t.reduce((_,O,V)=>{let w=g?.[V]?.triangleMask??O.triangleMask,F=0;for(let D=0;D<w.length;D++)w[D]&&F++;return _+F},0),b=new Float64Array(Math.max(0,y*9)),x=new Array(y),P=new Array(y).fill(!1),S={polygonCount:0,vertices:b,colors:x,textureFlags:P,solidTriangles:!0},C=t.map(_=>new Array(_.positions.length)),T=u.map(_=>_.joints.map(()=>new Array(16))),A=(_,O)=>{let V=typeof _=="number"?f[_]:f.find(w=>w.info.name===_);return V?fu(e,c,V,O):null},M=(_,O,V,w)=>{let F=V?.activeVertices,D=C[_],H=(K,Q,ne,ee)=>{let ue=D[K];ue?(ue[0]=Q,ue[1]=ne,ue[2]=ee):D[K]=[Q,ne,ee]};if(O.skinIndex!==void 0&&O.joints&&O.weights&&u[O.skinIndex]){let K=u[O.skinIndex],Q=O.joints,ne=O.weights,ee=T[O.skinIndex]??[];for(let U=0;U<K.joints.length;U++){let $=K.joints[U],k=w[$],L=K.inverseBindMatrices[U],W=ee[U]??(ee[U]=new Array(16));ee[U]=k&&L?tu(W,k,L):void 0}let ue=U=>{let $=O.positions[U],k=0,L=0,W=0,Y=0,se=Q[U]??[],Z=ne[U]??[];for(let B=0;B<4;B++){let q=Z[B]??0;if(q<=0)continue;let me=Math.round(se[B]??0),fe=ee[me];if(!fe)continue;let G=fe[0]*$[0]+fe[4]*$[1]+fe[8]*$[2]+fe[12],te=fe[1]*$[0]+fe[5]*$[1]+fe[9]*$[2]+fe[13],ce=fe[2]*$[0]+fe[6]*$[1]+fe[10]*$[2]+fe[14];k+=G*q,L+=te*q,W+=ce*q,Y+=q}if(Y>0){let B=1/Y;H(U,k*B,L*B,W*B)}else H(U,O.meshBindWorld[0]*$[0]+O.meshBindWorld[4]*$[1]+O.meshBindWorld[8]*$[2]+O.meshBindWorld[12],O.meshBindWorld[1]*$[0]+O.meshBindWorld[5]*$[1]+O.meshBindWorld[9]*$[2]+O.meshBindWorld[13],O.meshBindWorld[2]*$[0]+O.meshBindWorld[6]*$[1]+O.meshBindWorld[10]*$[2]+O.meshBindWorld[14])};if(F)for(let U of F)ue(U);else for(let U=0;U<O.positions.length;U++)ue(U)}else{let K=O.meshNode!==null?w[O.meshNode]??O.meshBindWorld:O.meshBindWorld,Q=ne=>{let ee=O.positions[ne];H(ne,K[0]*ee[0]+K[4]*ee[1]+K[8]*ee[2]+K[12],K[1]*ee[0]+K[5]*ee[1]+K[9]*ee[2]+K[13],K[2]*ee[0]+K[6]*ee[1]+K[10]*ee[2]+K[14])};if(F)for(let ne of F)Q(ne);else for(let ne=0;ne<O.positions.length;ne++)Q(ne)}return D},E=(_,O)=>{let V=A(_,O);if(!V)return[];let w=[];for(let F=0;F<t.length;F++){let D=t[F],H=g?.[F],K=H?.triangleMask??D.triangleMask,Q=M(F,D,H,V),ne=0;for(let ee=0;ee+2<D.indices.length;ee+=3,ne++){if(!K[ne])continue;let ue=D.indices[ee],U=D.indices[ee+1],$=D.indices[ee+2],k=Q[ue],L=Q[U],W=Q[$];if(!k||!L||!W)continue;let Y;if(D.uvs&&D.texture){let Z=D.uvs[ue],B=D.uvs[U],q=D.uvs[$];Z&&B&&q&&(Y=[Z,B,q])}let se=p(k,L,W,D.color,D.texture,D.textureWrap,D.textureAlphaMode,D.doubleSided,Y);se&&w.push(se)}}return w},v=(_,O)=>{let V=A(_,O);if(!V)return null;b.length<y*9&&(b=new Float64Array(y*9),S.vertices=b),x.length<y&&(x=new Array(y),S.colors=x),P.length<y&&(P=new Array(y).fill(!1),S.textureFlags=P);let w=0,F=0,D=!1;for(let H=0;H<t.length;H++){let K=t[H],Q=g?.[H],ne=Q?.triangleMask??K.triangleMask,ee=M(H,K,Q,V),ue=0;for(let U=0;U+2<K.indices.length;U+=3,ue++){if(!ne[ue])continue;let $=K.indices[U],k=K.indices[U+1],L=K.indices[U+2],W=ee[$],Y=ee[k],se=ee[L];if(!W||!Y||!se)continue;s(W,b,F),s(Y,b,F+3),s(se,b,F+6),x[w]=K.color;let Z=!!K.texture;P[w]=Z,Z&&(D=!0),w++,F+=9}}return S.polygonCount=w,S.solidTriangles=!D,S},I={clips:d,sample:E};return I[A0]=v,I[cu]={withPolygonFilter(_){let O=m(_);return O?h(O):void 0}},I};return h()}function Cr(e,n){let t=n?.targetSize??60,o=n?.defaultColor??"#888888",r=n?.materialColors??{},s=n?.materialTextures??{},i=e instanceof Uint8Array?e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength):e,c=i.byteLength,a,l=null;if(i.byteLength>=4&&new DataView(i).getUint32(0,!0)===1179937895){let R=f0(i);a=R.doc,l=R.bin}else a=JSON.parse(ql(new Uint8Array(i)));m0(a);let u=d0(a,l,n?.resolveBuffer),{urls:f,objectUrls:d}=g0(a,u,n?.baseUrl),m=y0(a,f),p=[],h=new Set,g=new Set(a.extensionsRequired??[]),y=g.has("KHR_mesh_quantization")||(a.extensionsUsed??[]).includes("KHR_mesh_quantization");function b(R,z){h.has(R)||(h.add(R),p.push(z))}function x(R,z){if(R===void 0)return!1;let j=a.accessors?.[R];if(!j)return!1;let J=le=>a.bufferViews?.[le]?.extensions?.[z]!==void 0;return!!(j.bufferView!==void 0&&J(j.bufferView)||j.sparse&&(J(j.sparse.indices.bufferView)||J(j.sparse.values.bufferView)))}function P(R){for(let z of zi)if(g.has(z)&&(R.extensions?.[z]||x(R.indices,z)||Object.values(R.attributes??{}).some(j=>x(j,z))))return z}function S(R,z){if(z instanceof Float32Array)return!0;let j=a.accessors?.[R];return y&&j?.type==="VEC3"&&l0(j.componentType)}function C(R,z){return(z!==null?a.nodes?.[z]?.weights:void 0)??R.weights??[]}function T(R,z,j,J,le){let re=j.targets;if(!re||re.length===0)return;let pe=C(R,J);if(!pe.some(ae=>Number.isFinite(ae)&&ae!==0))return;let xe=Array.from({length:le},()=>[0,0,0]),ie=!1;for(let ae=0;ae<re.length;ae++){let N=pe[ae]??0;if(!Number.isFinite(N)||N===0)continue;let X=re[ae]?.POSITION;if(X===void 0)continue;let{array:oe,count:Te,componentCount:Ce}=Pr(a,u,X);if(Te!==le||Ce<3){b(`morph-position-count:${z}:${ae}`,`Mesh ${R.name??z}: skipped morph target ${ae} POSITION with mismatched count`);continue}if(!S(X,oe)){b(`morph-position-type:${z}:${ae}`,`Mesh ${R.name??z}: skipped morph target ${ae} POSITION with unsupported accessor type`);continue}ie=!0;for(let Pe=0;Pe<le;Pe++){let _e=Pe*Ce,Oe=xe[Pe];Oe[0]+=Number(oe[_e])*N,Oe[1]+=Number(oe[_e+1])*N,Oe[2]+=Number(oe[_e+2])*N}}return ie?xe:void 0}let A=[],M=[],E=[],v=(a.meshes??[]).map((R,z)=>R.name??`mesh_${z}`),I=(a.materials??[]).map((R,z)=>R.name??`material_${z}`);function _(R,z){if(z===4)return R;let j=[];if(z===5)for(let J=0;J+2<R.length;J++)j.push(R[J],R[J+1+J%2],R[J+2-J%2]);else if(z===6)for(let J=0;J+2<R.length;J++)j.push(R[J+1],R[J+2],R[0]);return j}function O(R){let z=[];for(let j=0;j+2<R.length;j+=3){let J=R[j],le=R[j+1],re=R[j+2];z.push(J,le,re,J,re,le)}return z}function V(R,z,j){let J=a.meshes?.[R];if(J){if(!Array.isArray(J.primitives)){b(`malformed-primitives:${R}`,`Mesh ${J.name??R}: skipped mesh with non-array primitives`);return}for(let le=0;le<J.primitives.length;le+=1){let re=J.primitives[le];if(!re)continue;let pe=re.mode??4;if(pe!==4&&pe!==5&&pe!==6){let Ae=c0[pe]??`mode ${pe}`;b(`unsupported-mode:${pe}`,`Skipped primitives with unsupported mode ${pe} (${Ae})`);continue}if(re.extensions?.KHR_draco_mesh_compression&&g.has("KHR_draco_mesh_compression")){b("KHR_draco_mesh_compression","Skipped primitives with unsupported required extension KHR_draco_mesh_compression");continue}let xe=P(re);if(xe){b(xe,`Skipped primitives with unsupported required extension ${xe}`);continue}if(re.attributes.POSITION===void 0){b(`missing-position:${R}`,`Mesh ${J.name??R}: skipped primitive without POSITION attribute`);continue}let ie=re.material!==void 0?a.materials?.[re.material]:void 0,ae=eu(ie?.alphaMode),N=ie?.name,oe=(N?r[N]:void 0)??b0(ie,o,ae),Te=ie?.doubleSided===!0,Ce=re.material!==void 0?m.get(re.material):void 0,Pe=N&&s[N]?s[N]:re.material!==void 0?Ce?.url:void 0,_e=Pe&&re.material!==void 0?Ce?.wrap:void 0,Oe=Pe?Ce?.alphaMode??ae:void 0,Ve=Pe?Ce?.texCoord??0:0,$e=re.attributes.POSITION,{array:Ee,count:Fe,componentCount:Ne}=Pr(a,u,$e);if(!S($e,Ee))continue;let ot=T(J,R,re,j,Fe),rt=[],hn=[];for(let Ae=0;Ae<Fe;Ae++){let Re=Ae*Ne,Le=ot?.[Ae],Ie=[Number(Ee[Re])+(Le?.[0]??0),Number(Ee[Re+1])+(Le?.[1]??0),Number(Ee[Re+2])+(Le?.[2]??0)];rt.push(Ie),hn.push(Mr(z,Ie))}let Je=null,yn=re.attributes[`TEXCOORD_${Ve}`];if(Pe&&yn!==void 0){let{array:Ae,count:Re}=Pr(a,u,yn);Je=[];let Le=1;Ae instanceof Uint8Array?Le=1/255:Ae instanceof Uint16Array&&(Le=1/65535);for(let Ie=0;Ie<Re;Ie++){let Ke=Ae[Ie*2]*Le,ve=Ae[Ie*2+1]*Le;Je.push([Ke,1-ve])}}let Be;if(re.indices!==void 0){let{array:Ae,count:Re}=Pr(a,u,re.indices);Be=[];for(let Le=0;Le<Re;Le++)Be.push(Number(Ae[Le]))}else Be=hn.map((Ae,Re)=>Re);Be=_(Be,pe),Te&&(Be=O(Be));let je;if((a.animations?.length??0)>0){let Ae=Hl(a,u,re.attributes.JOINTS_0,4,Fe),Re=Hl(a,u,re.attributes.WEIGHTS_0,4,Fe);je={sourceIndex:M.length,meshNode:j,meshBindWorld:z,skinIndex:j!==null?a.nodes?.[j]?.skin:void 0,positions:rt,indices:Be,triangleMask:[],color:oe,texture:Pe,textureWrap:_e,textureAlphaMode:Oe,doubleSided:Te,uvs:Je??void 0,joints:Ae,weights:Re},M.push(je)}for(let Ae=0;Ae+2<Be.length;Ae+=3){let Re=Be[Ae],Le=Be[Ae+1],Ie=Be[Ae+2],Ke=je?je.triangleMask.length:void 0;je&&je.triangleMask.push(!1);let ve=hn[Re],en=hn[Le],an=hn[Ie];if(!ve||!en||!an)continue;let nn;if(Je&&Pe){let cn=Je[Re],st=Je[Le],ln=Je[Ie];cn&&st&&ln&&(nn=[cn,st,ln])}let He=`${R}:${le}:${j??"root"}`;A.push({v0:ve,v1:en,v2:an,color:oe,texture:Pe,textureWrap:_e,textureAlphaMode:Oe,doubleSided:Te,uvs:nn,simplifySourceVertexKeys:[`${He}:${Re}`,`${He}:${Le}`,`${He}:${Ie}`],source:je,sourceIndex:je?.sourceIndex,sourceTriangleIndex:Ke})}}}}function w(R,z,j){if(j.has(R)){b(`recursive-node:${R}`,`Skipped recursive node reference ${R} in glTF scene graph`);return}let J=a.nodes?.[R];if(!J)return;let le=nu(z,x0(J));typeof J.mesh=="number"&&V(J.mesh,le,R),j.add(R);for(let re of J.children??[])w(re,le,j);j.delete(R)}function F(R){let z=new Set,j=R.info.duration;for(let J of R.channels)for(let le of J.sampler.input)!Number.isFinite(le)||le<=0||j>0&&le>=j||z.add(le);return j>0&&(z.add(j*.25),z.add(j*.5),z.add(j*.75)),Array.from(z).sort((J,le)=>J-le).slice(0,128)}function D(R,z){let j=R.source;if(!j||R.sourceTriangleIndex===void 0||j.skinIndex!==void 0)return;let J=R.sourceTriangleIndex*3,le=j.indices[J],re=j.indices[J+1],pe=j.indices[J+2],xe=le!==void 0?j.positions[le]:void 0,ie=re!==void 0?j.positions[re]:void 0,ae=pe!==void 0?j.positions[pe]:void 0;if(!xe||!ie||!ae)return;let N=j.meshNode!==null?z[j.meshNode]??j.meshBindWorld:j.meshBindWorld;return{v0:Mr(N,xe),v1:Mr(N,ie),v2:Mr(N,ae)}}function H(R,z){return R[0]===z[0]&&R[1]===z[1]&&R[2]===z[2]}function K(R){return H(R.v0,R.v1)||H(R.v0,R.v2)||H(R.v1,R.v2)}function Q(){return A.some(R=>!K(R))}function ne(){if(A.length===0||(a.animations?.length??0)===0||Q())return;let R=uu(a,u);if(R.length===0)return;let z=(a.nodes??[]).map(J=>su(J)),j=new Array(A.length);for(let J of R)for(let le of F(J)){let re=fu(a,z,J,le);for(let pe=0;pe<A.length;pe++){if(j[pe])continue;let xe=D(A[pe],re);xe&&!K(xe)&&(j[pe]=xe)}}if(j.some(Boolean))for(let J=0;J<A.length;J++){let le=j[J];le&&(A[J].v0=le.v0,A[J].v1=le.v1,A[J].v2=le.v2)}}let ee=a.scene??0,ue=a.scenes?.[ee]?.nodes;if(ue&&ue.length>0)for(let R of ue)w(R,bt,new Set);else{(a.meshes?.length??0)===0&&b("no-gltf-meshes","No glTF meshes found");for(let R=0;R<(a.meshes?.length??0);R++)V(R,bt,null)}ne();let U=v0(d);if(A.length===0)return{polygons:[],objectUrls:d,dispose:U,warnings:p,metadata:{triangleCount:0,meshes:v,materials:I,sourceBytes:c}};let $=1/0,k=1/0,L=1/0,W=-1/0,Y=-1/0,se=-1/0;for(let R of A)for(let z of[R.v0,R.v1,R.v2])z[0]<$&&($=z[0]),z[0]>W&&(W=z[0]),z[1]<k&&(k=z[1]),z[1]>Y&&(Y=z[1]),z[2]<L&&(L=z[2]),z[2]>se&&(se=z[2]);let Z=Math.max(W-$,Y-k,se-L),B=Z>0?t/Z:1,q=n?.center,me=q===!0||q==="center",fe=me?($+W)*.5:$,G=me?(k+Y)*.5:k,te=me?(L+se)*.5:L,ce=R=>Math.round(R*1e3)/1e3,ye=n?.upAxis??"y",he=ye==="z"?([R,z,j])=>[ce((R-fe)*B),ce((z-G)*B),ce((j-te)*B)]:([R,z,j])=>[ce((j-te)*B),ce((R-fe)*B),ce((z-G)*B)],be=ye==="z"?(R,z,j)=>{z[j]=ce((R[0]-fe)*B),z[j+1]=ce((R[1]-G)*B),z[j+2]=ce((R[2]-te)*B)}:(R,z,j)=>{z[j]=ce((R[2]-te)*B),z[j+1]=ce((R[0]-fe)*B),z[j+2]=ce((R[1]-G)*B)},ge=[],Se=0;for(let R of A){let z=he(R.v0),j=he(R.v1),J=he(R.v2),le=S0(z,j,J);if(R.source&&R.sourceTriangleIndex!==void 0&&(R.source.triangleMask[R.sourceTriangleIndex]=!le),le){Se++;continue}let re={vertices:[z,j,J],color:R.color};R.texture&&(re.texture=R.texture),R.texture&&R.textureWrap&&(re.textureWrap=R.textureWrap),R.texture&&R.textureAlphaMode&&(re.textureAlphaMode=R.textureAlphaMode),R.doubleSided&&(re.doubleSided=!0),R.uvs&&(re.uvs=R.uvs),R.simplifySourceVertexKeys&&(re.simplifySourceVertexKeys=R.simplifySourceVertexKeys),ge.push(re),E.push(R.sourceIndex!==void 0&&R.sourceTriangleIndex!==void 0?{sourceIndex:R.sourceIndex,triangleIndex:R.sourceTriangleIndex}:void 0)}ge.length===0&&Se>0&&b("all-projected-triangles-degenerate","No non-degenerate glTF triangles remained after normalization");let De=T0(a,u,M,E,he,be);return{polygons:ge,animation:De,objectUrls:d,dispose:U,warnings:p,metadata:{triangleCount:ge.length,meshes:v,materials:I,animations:De?.clips,sourceBytes:c}}}function v0(e){let n=!1;return()=>{if(n)return;n=!0;let t=globalThis.URL;if(t?.revokeObjectURL)for(let o of e)try{t.revokeObjectURL(o)}catch{}}}var xu=Symbol("polycss.solidTextureBakedAnimationInfo"),mu=Symbol.for("polycss.animation.triangleFrameSource"),I0=16*1024*1024,_0=2,O0=32,V0=2,R0=.08,L0=8,w0=128,du=32,N0=.045,D0=10,xt=6,F0=16;function $i(e){return e.material?.texture??e.texture}function B0(){let e=globalThis;return typeof e.Image!="function"||typeof e.document?.createElement!="function"?null:{Image:e.Image,createCanvas(){return e.document.createElement("canvas")}}}function k0(e,n){return new Promise((t,o)=>{let r=new n,s=!1,i=c=>{s||(s=!0,c())};r.decoding="async",r.onload=()=>i(()=>t(r)),r.onerror=()=>i(()=>o(new Error(`texture load failed: ${e}`))),r.src=e})}async function U0(e,n,t){try{let o=await k0(e,n.Image),r=Math.max(0,Math.floor(o.naturalWidth||o.width||0)),s=Math.max(0,Math.floor(o.naturalHeight||o.height||0));if(r<=0||s<=0||r*s>t)return null;let i=n.createCanvas();i.width=r,i.height=s;let c=i.getContext("2d",{willReadFrequently:!0});if(!c)return null;c.drawImage(o,0,0,r,s);let a=c.getImageData(0,0,r,s).data;return{width:r,height:s,data:a,lowDetail:G0(r,s,a)}}catch{return null}}function pu(e,n,t,o,r,s){let i=(o*n+t)*4,c=(s*n+r)*4;return Math.max(Math.abs((e[i]??0)-(e[c]??0)),Math.abs((e[i+1]??0)-(e[c+1]??0)),Math.abs((e[i+2]??0)-(e[c+2]??0)))}function G0(e,n,t){let o=Math.max(1,Math.floor(Math.max(e,n)/w0)),r=0,s=0,i=0;for(let c=0;c<n;c+=o)for(let a=0;a<e;a+=o){if(a+o<e){let l=pu(t,e,a,c,a+o,c);i+=l,r++,l>du&&s++}if(c+o<n){let l=pu(t,e,a,c,a,c+o);i+=l,r++,l>du&&s++}}return r>0&&s/r<=N0&&i/r<=D0}function gu(e,n,t){return Math.max(n,Math.min(t,e))}function hu(e,n){if(!Number.isFinite(e))return null;switch(n){case"repeat":return e-Math.floor(e);case"mirrored-repeat":{let t=Math.floor(e),o=e-t;return Math.abs(t%2)===1?1-o:o}default:return Math.max(0,Math.min(1,e))}}function z0(e,n,t){let o=n[0],r=1-n[1],s=hu(o,t?.s),i=hu(r,t?.t);if(s===null||i===null)return null;let c=gu(Math.floor(s*e.width),0,e.width-1),l=(gu(Math.floor(i*e.height),0,e.height-1)*e.width+c)*4;return{r:e.data[l]??0,g:e.data[l+1]??0,b:e.data[l+2]??0,a:e.data[l+3]??255}}function Rn(e,n,t,o,r,s){return[e[0]*o+n[0]*r+t[0]*s,e[1]*o+n[1]*r+t[1]*s]}function $0(e){let[n,t,o]=e;return[Rn(n,t,o,1/3,1/3,1/3),Rn(n,t,o,.8,.1,.1),Rn(n,t,o,.1,.8,.1),Rn(n,t,o,.1,.1,.8),Rn(n,t,o,.45,.45,.1),Rn(n,t,o,.45,.1,.45),Rn(n,t,o,.1,.45,.45)]}function X0(e){let[n,t,o]=e,r=$0(e);for(let s=1;s<xt;s++)for(let i=1;i<xt-s;i++){let c=xt-s-i;c<=0||r.push(Rn(n,t,o,s/xt,i/xt,c/xt))}return r}function Pu(e){if(e.textureTriangles?.length)return e.textureTriangles;let n=e.uvs;if(!n||n.length!==e.vertices.length||n.length<3)return[];let t=[];for(let o=1;o+1<n.length;o++)t.push({uvs:[n[0],n[o],n[o+1]]});return t}function Xi(e,n,t){return Math.abs(e.r-n.r)<=t&&Math.abs(e.g-n.g)<=t&&Math.abs(e.b-n.b)<=t&&Math.abs(e.a-n.a)<=t}function Er(e){let n=o=>Math.round(Math.max(0,Math.min(255,o))).toString(16).padStart(2,"0");if(e.a>=255)return`#${n(e.r)}${n(e.g)}${n(e.b)}`;let t=Math.round(Math.max(0,Math.min(255,e.a))/255*1e3)/1e3;return`rgba(${Math.round(e.r)}, ${Math.round(e.g)}, ${Math.round(e.b)}, ${t})`}function Mu(e){if(!e)return null;let n=e.trim().toLowerCase(),t=n.match(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);if(t){let s=t[1],i=c=>{let a=s.length<=4?s[c]+s[c]:s.slice(c*2,c*2+2);return parseInt(a,16)};return{r:i(0),g:i(1),b:i(2),a:s.length===4||s.length===8?i(3):255}}let o=n.match(/^rgba?\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)(?:\s*,\s*([0-9.]+)\s*)?\)$/);if(!o)return null;let r=o[4]===void 0?255:Number(o[4])<=1?Number(o[4])*255:Number(o[4]);return{r:Number(o[1]),g:Number(o[2]),b:Number(o[3]),a:r}}function Y0(){return{min:{r:255,g:255,b:255,a:255},max:{r:0,g:0,b:0,a:0},sum:{r:0,g:0,b:0,a:0},colors:new Map,count:0}}function Au(e){return`${e.r},${e.g},${e.b},${e.a}`}function yu(e,n){e.min.r=Math.min(e.min.r,n.r),e.min.g=Math.min(e.min.g,n.g),e.min.b=Math.min(e.min.b,n.b),e.min.a=Math.min(e.min.a,n.a),e.max.r=Math.max(e.max.r,n.r),e.max.g=Math.max(e.max.g,n.g),e.max.b=Math.max(e.max.b,n.b),e.max.a=Math.max(e.max.a,n.a),e.sum.r+=n.r,e.sum.g+=n.g,e.sum.b+=n.b,e.sum.a+=n.a;let t=Au(n),o=e.colors.get(t);e.colors.set(t,{color:n,count:(o?.count??0)+1}),e.count++}function bu(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 W0(e){if(e.count<L0||e.colors.size<=1||e.colors.size>V0)return null;let n=null;for(let r of e.colors.values())(!n||r.count>n.count)&&(n=r);if(!n)return null;let t=e.count-n.count,o=Math.max(1,Math.floor(e.count*R0));return t<=o?n.color:null}function j0(e,n,t,o){let r=Pu(e);if(r.length===0)return null;let s=Y0(),i=e.textureAlphaMode==="opaque"?Mu(e.color):null;for(let c of r)for(let a of X0(c.uvs)){let l=z0(n,a,e.textureWrap);if(!l)return null;if(e.textureAlphaMode==="opaque"&&l.a===0&&e.color){i&&yu(s,{...i,a:255});continue}e.textureAlphaMode==="opaque"&&(l.a=255),yu(s,l)}if(s.count===0)return e.textureAlphaMode==="opaque"&&e.color?e.color:null;if(e.textureAlphaMode==="opaque"&&s.max.a===0&&e.color)return e.color;if(Xi(s.min,s.max,t))return Er(bu(s));if(!o){let c=W0(s);if(c)return Er(c)}return o||!n.lowDetail||!Xi(s.min,s.max,O0)?null:Er(bu(s))}function Su(e,n){let{texture:t,textureWrap:o,textureAlphaMode:r,material:s,uvs:i,textureTriangles:c,...a}=e,l=e.uvs?.length===e.vertices.length?e.uvs.map(d=>`uv:${Math.round(d[0]*1e5)},${Math.round(d[1]*1e5)}`):void 0,u=l??e.simplifyVertexKeys,f=e.simplifySourceVertexKeys?.length===e.vertices.length?e.simplifySourceVertexKeys.map((d,m)=>l?`${d}|${l[m]}`:d):e.simplifySourceVertexKeys;return{...a,color:n,...u?{simplifyVertexKeys:u}:{},...f?{simplifySourceVertexKeys:f}:{}}}function K0(e,n){if(n.length<2)return e;let t=[],o=new Map;for(let l of n){let u=Mu(e[l]?.color);if(!u||u.a<255)continue;let f=Au(u);t.push({index:l,key:f});let d=o.get(f);d?d.count+=1:o.set(f,{color:u,count:1})}if(o.size<2)return e;let r=[],s=new Map,i=[...o.entries()].sort((l,u)=>u[1].count-l[1].count||l[0].localeCompare(u[0]));for(let[l,u]of i){let f=null;for(let d of r)if(Xi(u.color,d,F0)){f=d;break}f||(f=u.color,r.push(f)),s.set(l,Er(f))}let c=!1,a=e.slice();for(let l of t){let u=s.get(l.key),f=e[l.index];!u||!f||f.color===u||(a[l.index]={...f,color:u},c=!0)}return c?a:e}function H0(e,n){let t=e.slice();for(let o=0;o<n.length;o++){let r=n[o],s=e[r.index];s&&(t[r.index]=Su(s,r.color))}return t}function Cu(e){return e?e[xu]:void 0}async function Eu(e,n={}){if(n.enabled===!1)return null;let t=B0();if(!t)return null;let o=e.filter(a=>$i(a)&&Pu(a).length>0);if(o.length===0)return null;let r=n.maxTexturePixels??I0,s=new Map;await Promise.all(Array.from(new Set(o.map(a=>$i(a)))).map(async a=>{s.set(a,await U0(a,t,r))}));let i=n.colorTolerance??_0,c=n.colorTolerance!==void 0;return{bake(a){let l=!1,u=[],f=a.map((d,m)=>{let p=$i(d);if(!p)return d;let h=s.get(p);if(!h)return d;let g=j0(d,h,i,c);return g?(l=!0,u.push(m),Su(d,g)):d});return l?{polygons:K0(f,u),changed:!0,bakedIndices:u}:{polygons:a,changed:!1,bakedIndices:u}}}}async function Tu(e,n={}){let t=await Eu(e,n);return t?t.bake(e).polygons:e}async function Tr(e,n={}){let t=await Eu(e.polygons,n);if(!t)return e;let o=t.bake(e.polygons);if(!o.changed)return e;let r=[];for(let c of o.bakedIndices){let l=o.polygons[c].color;l&&r.push({index:c,color:l})}let s=e.animation,i=s&&{...s,sample(c,a){return H0(s.sample(c,a),r)}};if(i&&s){let c=s[mu];if(c){let a=new Map(r.map(f=>[f.index,f.color])),l=[],u={polygonCount:0,vertices:new Float64Array(0),colors:l,solidTriangles:!1};i[mu]=(f,d)=>{let m=c(f,d);if(!m)return m;l.length<m.polygonCount&&(l=new Array(m.polygonCount),u.colors=l);let p=m.textureFlags,h=m.solidTriangles===!0||!!p;for(let g=0;g<m.polygonCount;g++){let y=a.get(g);l[g]=y??m.colors?.[g],p?.[g]&&!y&&(h=!1)}return u.polygonCount=m.polygonCount,u.vertices=m.vertices,u.textureFlags=p,u.solidTriangles=h,u}}i[xu]={source:s,bakedColorEntries:r}}return{...e,polygons:o.polygons,animation:i}}var vu=Symbol.for("polycss.animation.triangleFrameSource");function Z0(e,n){let t=e[n];if(!t)return null;let o=[];for(let r=0;r<t.vertices.length;r++)o.push(n,r);return{rest:t,originalIndex:n,sourceRefs:o}}function q0(e){let n=un(e);if(n.length>=e.length)return[];let t=new Map;e.forEach((r,s)=>t.set(r,s));let o=[];for(let r of n){let s=t.get(r);if(s===void 0)return[];let i=Z0(e,s);if(!i)return[];o.push(i)}return o}function Yi(e,n,t=!1){let o=new Array(n.length);for(let r=0;r<n.length;r++){let s=n[r],i=s.sourceRefs,c=new Array(i.length/2);for(let l=0,u=0;l<i.length;l+=2,u++){let d=e[i[l]]?.vertices[i[l+1]];if(!d)return e;let m=d[0],p=d[1],h=d[2];if(!Number.isFinite(m)||!Number.isFinite(p)||!Number.isFinite(h))return e;c[u]=[m,p,h]}let a=t?s.rest.color:e[i[0]??-1]?.color??s.rest.color;o[r]={...s.rest,vertices:c,color:a}}return o}function Q0(e,n){let t=new Map;return n.forEach((o,r)=>{t.set(o,r)}),e.map((o,r)=>({...o,sourceRefs:o.sourceRefs.map((s,i)=>i%2===0?t.get(s)??-1:s)}))}function J0(e,n){if(e.length<n.length)return null;let t=new Array(n.length);for(let o=0;o<n.length;o++){let r=e[o],s=n[o];if(!r||r.vertices.length!==s.rest.vertices.length)return null;t[o]={...s.rest,vertices:r.vertices,color:s.rest.color}}return t}function eh(e){return!!(e.texture||e.material?.texture)}function Iu(e,n={}){if(!e.animation||e.polygons.length===0||n.meshResolution==="lossless")return e;let t=q0(e.polygons);if(t.length===0||t.length>=e.polygons.length)return e;let o=Cu(e.animation),r=o?.source??e.animation,s=t.map(b=>b.originalIndex),i=lu(r,s),c=i??r,a=i?Q0(t,s):t,l=t.map(b=>b.rest.color),u=t.every(b=>b.rest.vertices.length===3&&!eh(b.rest)),f=i&&!o&&t.length===s.length&&t.every((b,x)=>b.rest.vertices.length===3&&b.sourceRefs.length===6&&b.sourceRefs[0]===s[x]&&b.sourceRefs[2]===s[x]&&b.sourceRefs[4]===s[x]),d=c[vu],m=new Float64Array(t.length*9),p=l.slice(),h=new Array(t.length).fill(!1),g={polygonCount:t.length,vertices:m,colors:p,textureFlags:h,solidTriangles:u},y={...e.animation,sample(b,x){let P=c.sample(b,x);return f?P:i&&o?u&&t.length===s.length?J0(P,t)??Yi(P,a,!0):Yi(P,a,!0):Yi(P,a,!!o)}};return d&&u&&(y[vu]=(b,x)=>{let P=d(b,x);if(!P)return P;if(i)return P.polygonCount<t.length||P.vertices.length<t.length*9?null:(g.polygonCount=t.length,g.vertices=P.vertices,g.colors=o?l:P.colors,g.textureFlags=o?h:P.textureFlags,g.solidTriangles=o?u:P.solidTriangles===!0,g);m.length<t.length*9&&(m=new Float64Array(t.length*9),g.vertices=m),p.length<t.length&&(p=new Array(t.length),g.colors=p),h.length<t.length&&(h=new Array(t.length).fill(!1),g.textureFlags=h);let S=u&&P.solidTriangles!==!1;for(let C=0;C<a.length;C++){let T=a[C],A=T.sourceRefs;for(let v=0,I=0;v<A.length;v+=2,I++){let _=A[v],O=A[v+1];if(_<0||_>=P.polygonCount)return null;let V=_*9+O*3,w=C*9+I*3;if(V+2>=P.vertices.length)return null;m[w]=P.vertices[V],m[w+1]=P.vertices[V+1],m[w+2]=P.vertices[V+2]}let M=A[0],E=!!P.textureFlags?.[M];h[C]=E,p[C]=o?T.rest.color:P.colors?.[M]??T.rest.color,!o&&E&&(S=!1)}return g.polygonCount=t.length,g.vertices=m,g.colors=p,g.textureFlags=h,g.solidTriangles=o?u:S,g}),{...e,polygons:t.map(b=>b.rest),animation:y,metadata:{...e.metadata,triangleCount:t.length}}}var nh=/^[0-9A-Fa-f]{6}$/,th=["#3b82f6","#ef4444","#22c55e","#eab308","#a855f7","#06b6d4","#f97316","#ec4899"];function oh(e){let n=[],t="";for(let o of e.split(`
|
|
2
|
-
`)){let r=o.endsWith("\r")?o.slice(0,-1):o;if(r.endsWith("\\")){t+=`${r.slice(0,-1).trimEnd()} `;continue}n.push(t+r),t=""}return t&&n.push(t.trimEnd()),n}function
|
|
3
|
-
`)){let s=r.trim();if(!(s.length===0||s.charCodeAt(0)===35)){if(s.startsWith("newmtl ")){o=s.slice(7).trim();continue}if(o){if(s.startsWith("Kd ")){let i=s.split(/\s+/),c=parseFloat(i[1]),a=parseFloat(i[2]),l=parseFloat(i[3]);Number.isFinite(c)&&Number.isFinite(a)&&Number.isFinite(l)&&(n[o]=`#${Wi(c)}${Wi(a)}${Wi(l)}`)}else if(s.startsWith("map_Kd ")){let i=s.split(/\s+/),c=i[i.length-1]?.replace(/\\+/g,"/");c&&c!=="map_Kd"&&(t[o]=c)}}}}return{colors:n,textures:t}}var sh=[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 ih(e){let n=e>>0&255,t=e>>8&255,o=e>>16&255;return[n,t,o]}function ji(e){return(e&255).toString(16).padStart(2,"0")}function Vu(e,n,t){return`#${ji(e)}${ji(n)}${ji(t)}`}function ah(e,n,t,o){if(o>=255)return Vu(e,n,t);let r=Math.round(Math.max(0,Math.min(255,o))/255*1e3)/1e3;return`rgba(${e}, ${n}, ${t}, ${r})`}function Ru(e){let n=e.match(/^#([0-9a-f]{6})$/i);if(n){let o=n[1];return{rgb:[parseInt(o.slice(0,2),16),parseInt(o.slice(2,4),16),parseInt(o.slice(4,6),16)],alpha:1}}let t=e.match(/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/i);return t?{rgb:[Number(t[1]),Number(t[2]),Number(t[3])],alpha:Number(t[4])}:null}function Lu(e,n){return Math.hypot(e.rgb[0]-n.rgb[0],e.rgb[1]-n.rgb[1],e.rgb[2]-n.rgb[2])}function _u(e){let n=e.rgb[0]/255,t=e.rgb[1]/255,o=e.rgb[2]/255,r=Math.max(n,t,o),s=Math.min(n,t,o),i=r-s,c=0;return i!==0&&(r===n?c=60*((t-o)/i%6):r===t?c=60*((o-n)/i+2):c=60*((n-t)/i+4)),c<0&&(c+=360),{hue:c,saturation:r===0?0:i/r,value:r}}function ch(e,n){let t=Math.abs(e-n)%360;return t>180?360-t:t}function wu(e,n){if(e.alpha<1||n.alpha<1)return!1;let t=_u(e),o=_u(n),r=t.saturation<.08,s=o.saturation<.08;if(r||s)return r===s;let i=Math.min(t.value,o.value)<.16?35:24;return ch(t.hue,o.hue)<=i}function lh(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return null;let o=new Map;for(let c of e.keys()){let a=Ru(c);a&&o.set(c,a)}let r=[],s=new Map,i=Array.from(e.entries()).sort((c,a)=>a[1]-c[1]||c[0].localeCompare(a[0]));for(let[c]of i){let a=o.get(c);if(!a){s.set(c,c);continue}let l=null;for(let u of r){if(!wu(a,u.parsed))continue;let f=Lu(a,u.parsed);f>t||(!l||f<l.distance)&&(l={color:u.color,distance:f})}l?s.set(c,l.color):(r.push({color:c,parsed:a}),s.set(c,c))}return s}var uh=[[1,0],[-1,0],[0,1],[0,-1]];function Nu(e,n){return`${e},${n}`}function fh(e){let n=e.indexOf(",");return[Number(e.slice(0,n)),Number(e.slice(n+1))]}function mh(e,n,t){return e<=4||(n===1||t===1)&&e<=12?!0:Math.min(n,t)<=2&&Math.max(n,t)>=4&&e<=18}function dh(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return;let o=new Map,r=s=>(o.has(s)||o.set(s,Ru(s)),o.get(s)??null);for(let s of e.values()){if(s.size<3)continue;let i=new Map;for(let l of s.values())i.set(l,(i.get(l)??0)+1);let c=new Set,a=[];for(let[l,u]of s){if(c.has(l))continue;let f=[l],d=[],m=new Map,p=1/0,h=-1/0,g=1/0,y=-1/0;for(c.add(l);f.length>0;){let C=f.pop();d.push(C);let[T,A]=fh(C);p=Math.min(p,T),h=Math.max(h,T),g=Math.min(g,A),y=Math.max(y,A);for(let[M,E]of uh){let v=Nu(T+M,A+E),I=s.get(v);if(I){if(I===u){c.has(v)||(c.add(v),f.push(v));continue}m.set(I,(m.get(I)??0)+1)}}}let b=h-p+1,x=y-g+1;if(!mh(d.length,b,x)||m.size===0)continue;let P=r(u);if(!P)continue;let S=null;for(let[C,T]of m){let A=i.get(C)??0;if(A<=d.length)continue;let M=r(C);if(!M||!wu(P,M))continue;let E=Lu(P,M);if(E>t)continue;let v={color:C,contacts:T,distance:E,count:A};(!S||v.contacts>S.contacts||v.contacts===S.contacts&&v.count>S.count||v.contacts===S.contacts&&v.count===S.count&&v.distance<S.distance||v.contacts===S.contacts&&v.count===S.count&&v.distance===S.distance&&v.color.localeCompare(S.color)<0)&&(S=v)}if(S)for(let C of d)a.push([C,S.color])}for(let[l,u]of a)s.set(l,u)}}var ph=542658390,gh=new Set(["nTRN","nGRP","nSHP"]),hh="Skipped MagicaVoxel scene graph transforms; models were flattened into one grid";function _r(e,n){let t=n?.targetSize??60,o=e.byteLength;if(e.byteLength<8)return Pt(o,["parseVox: buffer too small to be a valid .vox file"]);let r=new DataView(e);if(r.getUint32(0,!0)!==ph)return Pt(o,["parseVox: not a .vox file (bad magic)"]);if(e.byteLength<20)return Pt(o,["parseVox: buffer too small for MAIN chunk"]);if(Ou(r,8)!=="MAIN")return Pt(o,["parseVox: expected MAIN chunk at offset 8"]);let a=20+r.getUint32(12,!0),l=a+r.getUint32(16,!0),u=[],f=[],d=[],m=null,p=k=>{d.includes(k)||d.push(k)};for(;a<l&&a+12<=e.byteLength;){let k=Ou(r,a),L=r.getUint32(a+4,!0),W=r.getUint32(a+8,!0),Y=a+12,se=Y+L+W;if(k==="SIZE"){if(L>=12&&Y+12<=e.byteLength){let Z=r.getUint32(Y,!0),B=r.getUint32(Y+4,!0),q=r.getUint32(Y+8,!0);u.push({sx:Z,sy:B,sz:q})}}else if(k==="XYZI"){if(L>=4&&Y+4<=e.byteLength){let Z=r.getUint32(Y,!0),B=[],q=Math.min(Z,Math.floor((L-4)/4));for(let me=0;me<q;me++){let fe=Y+4+me*4;B.push({x:r.getUint8(fe),y:r.getUint8(fe+1),z:r.getUint8(fe+2),colorIndex:r.getUint8(fe+3)})}f.push(B)}}else if(k==="RGBA"&&L>=1024&&Y+1024<=e.byteLength){m=[];for(let Z=0;Z<256;Z++){let B=Y+Z*4,q=r.getUint8(B),me=r.getUint8(B+1),fe=r.getUint8(B+2),G=r.getUint8(B+3);m.push(ah(q,me,fe,G))}}gh.has(k)&&p(hh),a=se}let h=[],g=new Set;for(let k of f)for(let L of k){if(L.colorIndex===0)continue;let W=`${L.x},${L.y},${L.z}`;g.has(W)||(g.add(W),h.push(L))}if(h.length===0)return Pt(o,d);let y=new Map,b=k=>{let L=y.get(k);if(L)return L;let W=k-1,Y;if(m!==null)Y=m[W]??"#888888";else{let se=sh[k]??0,[Z,B,q]=ih(se);Y=Vu(Z,B,q)}return y.set(k,Y),Y},x=new Map;for(let k of h){let L=b(k.colorIndex);x.set(L,(x.get(L)??0)+1)}let P=lh(x,n?.paletteMergeDistance),S=k=>{let L=b(k);return P?.get(L)??L},C=(k,L,W)=>g.has(`${k},${L},${W}`),T=new Map,A=(k,L,W,Y,se)=>{let Z=`${k}:${L}`,B=T.get(Z);B||(B=new Map,T.set(Z,B)),B.set(Nu(W,Y),se)};for(let k of h){let{x:L,y:W,z:Y}=k,se=S(k.colorIndex);C(L+1,W,Y)||A(0,L+1,W,Y,se),C(L-1,W,Y)||A(1,L,W,Y,se),C(L,W+1,Y)||A(2,W+1,L,Y,se),C(L,W-1,Y)||A(3,W,L,Y,se),C(L,W,Y+1)||A(4,Y+1,L,W,se),C(L,W,Y-1)||A(5,Y,L,W,se)}dh(T,n?.colorRegionMergeDistance);let M=new Map;for(let[k,L]of T)for(let[W,Y]of L){let se=`${k}:${Y}`,Z=M.get(se);Z||(Z=new Set,M.set(se,Z)),Z.add(W)}function E(k){let L=new Set,W=[],Y=1/0,se=-1/0,Z=1/0,B=-1/0;for(let q of k){let[me,fe]=q.split(","),G=+me,te=+fe;G<Y&&(Y=G),G>se&&(se=G),te<Z&&(Z=te),te>B&&(B=te)}for(let q=Z;q<=B;q++)for(let me=Y;me<=se;me++){let fe=`${me},${q}`;if(!k.has(fe)||L.has(fe))continue;let G=1;for(;me+G<=se;){let ce=`${me+G},${q}`;if(!k.has(ce)||L.has(ce))break;G++}let te=1;e:for(;q+te<=B;){for(let ce=0;ce<G;ce++){let ye=`${me+ce},${q+te}`;if(!k.has(ye)||L.has(ye))break e}te++}for(let ce=0;ce<te;ce++)for(let ye=0;ye<G;ye++)L.add(`${me+ye},${q+ce}`);W.push({u:me,v:q,w:G,h:te})}return W}let v=(k,L,W,Y,se,Z)=>{let B=W+se,q=Y+Z;switch(k){case 0:return[[L,W,Y],[L,B,Y],[L,B,q],[L,W,q]];case 1:return[[L,B,Y],[L,W,Y],[L,W,q],[L,B,q]];case 2:return[[W,L,Y],[W,L,q],[B,L,q],[B,L,Y]];case 3:return[[B,L,Y],[B,L,q],[W,L,q],[W,L,Y]];case 4:return[[W,Y,L],[B,Y,L],[B,q,L],[W,q,L]];default:return[[W,q,L],[B,q,L],[B,Y,L],[W,Y,L]]}},I=[];for(let[k,L]of M){let W=k.indexOf(":"),Y=k.indexOf(":",W+1),se=+k.slice(0,W),Z=+k.slice(W+1,Y),B=k.slice(Y+1);for(let{u:q,v:me,w:fe,h:G}of E(L))I.push({vertices:v(se,Z,q,me,fe,G),color:B})}if(I.length===0)return Pt(o,d);let _=1/0,O=1/0,V=1/0,w=-1/0,F=-1/0,D=-1/0;for(let k of I)for(let L of k.vertices)L[0]<_&&(_=L[0]),L[0]>w&&(w=L[0]),L[1]<O&&(O=L[1]),L[1]>F&&(F=L[1]),L[2]<V&&(V=L[2]),L[2]>D&&(D=L[2]);let H=Math.max(w-_,F-O,D-V),K=H>0?t/H:1,Q=K*50,ne=Number.isFinite(Q)&&Q>0?Math.max(1,Math.round(Q))/50:K,ee={kind:"magica-vox",cells:h.map(k=>({x:F-(k.y+1),y:k.x-_,z:k.z-V,color:S(k.colorIndex)})),rows:Math.max(0,F-O),cols:Math.max(0,w-_),depth:Math.max(0,D-V),scale:ne,sourceBytes:o},ue=k=>Math.round(k*1e3)/1e3,U=k=>[ue((k[0]-_)*ne),ue((k[1]-O)*ne),ue((k[2]-V)*ne)],$=I.map(({vertices:k,color:L})=>({vertices:k.map(U),color:L}));return{polygons:$,voxelSource:ee,objectUrls:[],dispose:()=>{},warnings:d,metadata:{triangleCount:$.length,sourceBytes:o,voxelCount:h.length}}}function Ou(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 Pt(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:n,metadata:{triangleCount:0,sourceBytes:e}}}var yh="#888888";var bh=/^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:[eE][+-]?\d+)?$/,Qn="COLOR=";function Bu(e,n,t){let o=n??t;if(!Number.isFinite(o))throw new Error(`parseStl: ${e} must be finite`);return o}function xh(e,n,t){let o=Bu(e,n,t);if(o<=0)throw new Error(`parseStl: ${e} must be greater than 0`);return o}function Ph(e){if(e===void 0||e==="z"||e==="y")return e??"z";throw new Error('parseStl: upAxis must be "z" or "y"')}function Mh(e){let n=e??yh;if(typeof n!="string"||n.trim().length===0)throw new Error("parseStl: defaultColor must be a non-empty string");return n}function Ah(e){if(typeof e=="string")return null;if(e instanceof Uint8Array)return new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength));if(e instanceof ArrayBuffer)return new Uint8Array(e);throw new Error("parseStl: source must be an ArrayBuffer, Uint8Array, or ASCII string")}function ku(e){let n=globalThis.TextDecoder;return new n().decode(e)}function Or(e,n){return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0)]}function Vr(e){return Number.isFinite(e[0])&&Number.isFinite(e[1])&&Number.isFinite(e[2])}function Du(e){let n=e.trim().split(/\s+/);if(n.length!==3||!n.every(o=>bh.test(o)))return null;let t=[Number(n[0]),Number(n[1]),Number(n[2])];return Vr(t)?t:null}function Sh(e){let n="";for(let o of e)o!==0&&(n+=o>=32&&o<=126?String.fromCharCode(o):" ");return n.replace(/\s+/g," ").trim()||void 0}function Ki(e){return Math.max(0,Math.min(255,e)).toString(16).padStart(2,"0")}function Uu(e,n,t,o=255){if(o>=255)return`#${Ki(e)}${Ki(n)}${Ki(t)}`;let r=Math.round(o/255*1e3)/1e3;return`rgba(${e}, ${n}, ${t}, ${r})`}function Hi(e){return Math.round(e/31*255)}function Ch(e){for(let n=0;n<80-Qn.length-4;n+=1){let t=!0;for(let c=0;c<Qn.length;c+=1)if(e.getUint8(n+c)!==Qn.charCodeAt(c)){t=!1;break}if(!t)continue;let o=e.getUint8(n+Qn.length),r=e.getUint8(n+Qn.length+1),s=e.getUint8(n+Qn.length+2),i=e.getUint8(n+Qn.length+3);return{defaultColor:Uu(o,r,s,i),alpha:Math.round(i/255*1e3)/1e3,alphaByte:i}}return null}function Eh(e,n){if((e&32768)!==0)return{color:n.defaultColor,usesDefaultColor:!0};let t=Hi(e&31),o=Hi(e>>5&31),r=Hi(e>>10&31);return{color:Uu(t,o,r,n.alphaByte),usesDefaultColor:!1}}function Th(e){let n=[],t=/^\s*solid(?:\s+([^\r\n]*?))?\s*$/gim,o=/^\s*endsolid\b[^\r\n]*$/gim,r;for(;r=t.exec(e);)n.push({index:r.index,kind:"start",name:(r[1]??"").trim()});for(;r=o.exec(e);)n.push({index:r.index,kind:"end"});return n.sort((s,i)=>s.index-i.index||(s.kind==="end"?-1:1))}function vh(e){let n=Math.min(e.byteLength,1024);for(let t=84;t<n;t+=1){let o=e[t];if(o===0||o<9||o>13&&o<32||o>126)return!0}return!1}function Ih(e){let n=ku(e.subarray(0,Math.min(e.byteLength,512))).trimStart().toLowerCase();return!n.startsWith("solid")&&!n.startsWith("facet")?!1:!vh(e)}function _h(e){if(e.byteLength<84)return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(80,!0),o=84+t*50;if(o===e.byteLength)return{triangleCount:t,declaredTriangleCount:t,trailingBytes:0};if(Ih(e))return null;if(o<e.byteLength)return{triangleCount:t,declaredTriangleCount:t,trailingBytes:e.byteLength-o};let r=e.byteLength-84;return r>0&&r%50===0?{triangleCount:r/50,declaredTriangleCount:t,trailingBytes:0}:null}function Oh(e,n,t){let o=new DataView(e.buffer,e.byteOffset,e.byteLength),{triangleCount:r,declaredTriangleCount:s,trailingBytes:i}=n;s!==r&&t.push(`parseStl: binary STL declared ${s} triangles but contains ${r} complete triangle record${r===1?"":"s"}`),i>0&&t.push(`parseStl: ignored ${i} trailing binary byte${i===1?"":"s"}`);let c=Sh(e.subarray(0,80)),a=Ch(o),l=0,u=0,f=[];for(let d=0,m=84;d<r;d+=1,m+=50){let p=Or(o,m),h=[Or(o,m+12),Or(o,m+24),Or(o,m+36)];if(!h.every(Vr))throw new Error(`parseStl: non-finite coordinate in binary triangle ${d}`);let g=o.getUint16(m+48,!0),y=a?Eh(g,a):null;y&&(y.usesDefaultColor?u+=1:l+=1),f.push({normal:Vr(p)?p:null,hasIgnoredNormal:!Vr(p),color:y?.color,vertices:h,hasIgnoredAttribute:!a&&g!==0})}return{format:"binary",facets:f,binaryHeader:c,color:a?{format:"magics",defaultColor:a.defaultColor,alpha:a.alpha,coloredTriangleCount:l,defaultColorTriangleCount:u}:void 0}}function Fu(e,n){let t=[],o=/^\s*facet\s+normal\s+([^\r\n]+)([\s\S]*?)^\s*endfacet\b[^\r\n]*$/gim,r=/^\s*vertex\s+([^\r\n]+)/gim,s=/^\s*facet\s+normal\b/gim,i=Th(e),c=0,a,l=0;for(;s.exec(e);)l+=1;let u=0,f=0,d=0,m;for(;m=o.exec(e);){for(u+=1;c<i.length&&i[c].index<m.index;){let b=i[c];a=b.kind==="start"?b.name:void 0,c+=1}let p=Du(m[1]);p||(d+=1);let h=[],g=!1;r.lastIndex=0;let y;for(;y=r.exec(m[2]);){let b=Du(y[1]);if(!b){g=!0;break}h.push(b)}if(g||h.length!==3){f+=1;continue}t.push({normal:p,hasIgnoredNormal:!p,solidName:a,vertices:[h[0],h[1],h[2]]})}return f+=Math.max(0,l-u),f>0&&n.push(`parseStl: skipped ${f} malformed ASCII facet${f===1?"":"s"}`),d>0&&n.push(`parseStl: ignored ${d} malformed ASCII facet normal${d===1?"":"s"}`),{format:"ascii",facets:t}}function Mt(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Lr(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 Zi(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Gu(e){return Math.hypot(e[0],e[1],e[2])}function Vh(e){let n=Lr(Mt(e[1],e[0]),Mt(e[2],e[0]));return Gu(n)<=1e-8}function Rr(e){let n=Gu(e);return n<=1e-8?null:[e[0]/n,e[1]/n,e[2]/n]}function Rh(e){let n=t=>Object.is(t,-0)?0:t;return`${n(e[0])},${n(e[1])},${n(e[2])}`}function Lh(e,n){return e<n?`${e}|${n}`:`${n}|${e}`}function qi(e,n){return n?[e.vertices[0],e.vertices[2],e.vertices[1]]:e.vertices}function wh(e,n,t){let o=0;for(let r of n.facets){let[s,i,c]=qi(e[r],t[r]);o+=Zi(s,Lr(i,c))/6}return o}function Nh(e,n,t){if(n.facets.length<8)return null;let o=0,r=0;for(let i of n.facets){let c=e[i],a=c.normal?Rr(c.normal):null;if(!a)continue;let l=qi(c,t[i]),u=Rr(Lr(Mt(l[1],l[0]),Mt(l[2],l[0])));if(!u)continue;let f=Zi(u,a);f>=.95?o+=1:f<=-.95&&(r+=1)}let s=o+r;return s/n.facets.length<.75?null:r/s>=.9?"opposed":o/s>=.9?"aligned":null}function Dh(e){let n=new Map,t=e.map(()=>[]);for(let u=0;u<e.length;u+=1){let f=e[u].vertices.map(Rh);for(let d=0;d<3;d+=1){let m=f[d],p=f[(d+1)%3],h=Lh(m,p),g=n.get(h),y={facetIndex:u,from:m,to:p};g?g.push(y):n.set(h,[y])}}let o=0;for(let u of n.values()){if(u.length>2&&(o+=1),u.length<2)continue;let[f,...d]=u;for(let m of d){let p=f.from===m.from&&f.to===m.to;t[f.facetIndex].push({to:m.facetIndex,flipDiffers:p}),t[m.facetIndex].push({to:f.facetIndex,flipDiffers:p})}}let r=new Array(e.length).fill(!1),s=new Array(e.length).fill(-1),i=[],c=0;for(let u=0;u<e.length;u+=1){if(s[u]>=0)continue;let f=i.length,d={facets:[],conflicts:0,boundaryEdges:0,nonManifoldEdges:0,flippedOutward:!1,flippedToSuppliedNormals:!1};i.push(d),s[u]=f;let m=[u];for(let p=0;p<m.length;p+=1){let h=m[p];d.facets.push(h);for(let g of t[h]){let y=r[h]!==g.flipDiffers;s[g.to]<0?(s[g.to]=f,r[g.to]=y,m.push(g.to)):r[g.to]!==y&&h<g.to&&(d.conflicts+=1,c+=1)}}}for(let u of n.values()){let f=i[s[u[0].facetIndex]];u.length===1?f.boundaryEdges+=1:u.length>2&&(f.nonManifoldEdges+=1)}for(let u of i){if(u.nonManifoldEdges>0||u.conflicts>0){for(let d of u.facets)r[d]=!1;continue}if(u.boundaryEdges>0){if(Nh(e,u,r)==="opposed"){for(let d of u.facets)r[d]=!r[d];u.flippedToSuppliedNormals=!0}continue}if(wh(e,u,r)<-1e-10){for(let d of u.facets)r[d]=!r[d];u.flippedOutward=!0}}let a=0;return{facets:e.map((u,f)=>r[f]?(a+=1,{...u,vertices:qi(u,!0)}):u),repairedCount:a,componentCount:i.length,outwardComponentCount:i.filter(u=>u.flippedOutward).length,suppliedNormalComponentCount:i.filter(u=>u.flippedToSuppliedNormals).length,conflictCount:c,nonManifoldEdgeCount:o}}function Fh(e,n,t,o,r){let s=i=>Math.round(i*1e3)/1e3;return r==="y"?[s((e[2]-n[2])*t+o),s((e[0]-n[0])*t+o),s((e[1]-n[1])*t+o)]:[s((e[0]-n[0])*t+o),s((e[1]-n[1])*t+o),s((e[2]-n[2])*t+o)]}function Bh(e){if(e.every(r=>r.solidName===void 0))return;let n=[],t=e[0]?.solidName??"",o=0;for(let r=1;r<=e.length;r+=1){let s=e[r]?.solidName??"";r<e.length&&s===t||(n.push({name:t,start:o,count:r-o}),t=s,o=r)}return n}function kh(e){if(!e)return;let n=[],t=new Set;for(let o of e)!o.name||t.has(o.name)||(t.add(o.name),n.push(o.name));return n.length>0?n:void 0}function wr(e,n){let t=xh("targetSize",n?.targetSize,60),o=Bu("gridShift",n?.gridShift,1),r=Mh(n?.defaultColor),s=Ph(n?.upAxis),i=[],c=Ah(e),a=typeof e=="string"?e.length:c.byteLength;if(a===0||typeof e=="string"&&e.trim().length===0)throw new Error("parseStl: empty input");let l=c?_h(c):null,u=typeof e=="string"?Fu(e,i):l?Oh(c,l,i):Fu(ku(c),i),f=0,d=0,m=0,p=[];for(let A of u.facets){if(A.hasIgnoredAttribute&&(f+=1),A.hasIgnoredNormal&&u.format==="binary"&&(d+=1),Vh(A.vertices)){m+=1;continue}p.push(A)}let h=Dh(p),g=0;for(let A of h.facets){let M=Rr(Lr(Mt(A.vertices[1],A.vertices[0]),Mt(A.vertices[2],A.vertices[0]))),E=A.normal?Rr(A.normal):null;M&&E&&Zi(M,E)<.95&&(g+=1)}if(f>0&&i.push(`parseStl: ignored non-zero binary attribute byte count on ${f} triangle${f===1?"":"s"}`),d>0&&i.push(`parseStl: ignored ${d} non-finite binary normal${d===1?"":"s"}`),m>0&&i.push(`parseStl: skipped ${m} degenerate triangle${m===1?"":"s"}`),h.repairedCount>0&&i.push(`parseStl: repaired winding on ${h.repairedCount} triangle${h.repairedCount===1?"":"s"}`),h.outwardComponentCount>0&&i.push(`parseStl: oriented ${h.outwardComponentCount} closed component${h.outwardComponentCount===1?"":"s"} outward`),h.suppliedNormalComponentCount>0&&i.push(`parseStl: oriented ${h.suppliedNormalComponentCount} open component${h.suppliedNormalComponentCount===1?"":"s"} from supplied normals`),h.conflictCount>0&&i.push(`parseStl: found ${h.conflictCount} inconsistent shared-edge winding constraint${h.conflictCount===1?"":"s"}`),h.nonManifoldEdgeCount>0&&i.push(`parseStl: found ${h.nonManifoldEdgeCount} non-manifold shared edge${h.nonManifoldEdgeCount===1?"":"s"}`),g>0&&i.push(`parseStl: ${g} supplied normal${g===1?" disagrees":"s disagree"} with triangle winding`),p.length===0)throw u.facets.length===0?new Error(`parseStl: no valid ${u.format} facets`):new Error("parseStl: no valid facets after filtering");let y=[1/0,1/0,1/0],b=[-1/0,-1/0,-1/0];for(let A of h.facets)for(let M of A.vertices)for(let E=0;E<3;E+=1)M[E]<y[E]&&(y[E]=M[E]),M[E]>b[E]&&(b[E]=M[E]);let x=Math.max(b[0]-y[0],b[1]-y[1],b[2]-y[2]),P=x>0?t/x:1,S=Bh(h.facets),C=kh(S),T=h.facets.map(A=>({vertices:A.vertices.map(M=>Fh(M,y,P,o,s)),color:A.color??r}));return{polygons:T,objectUrls:[],dispose:()=>{},warnings:i,metadata:{triangleCount:T.length,meshes:C,materials:[],sourceBytes:a,stlHeader:u.binaryHeader,stlColor:u.color,stlSolids:S,stlTopology:{componentCount:h.componentCount,repairedTriangleCount:h.repairedCount,outwardComponentCount:h.outwardComponentCount,suppliedNormalComponentCount:h.suppliedNormalComponentCount,inconsistentSharedEdgeCount:h.conflictCount,nonManifoldSharedEdgeCount:h.nonManifoldEdgeCount}}}}var Ln="loadMesh";async function zu(e,n){let t=n?.solidTextureSamples;return t===!1?e:Tr(e,typeof t=="object"?t:void 0)}function Uh(e){let n=e.split("?")[0].split("#")[0],t=n.lastIndexOf(".");return t<0?"":n.slice(t+1).toLowerCase()}async function $u(e,n){let t=Uh(e);if(t==="mtl")throw new Error(`${Ln}: .mtl is a material file, not a mesh \u2014 use parseMtl directly`);let o=globalThis.fetch;if(!o)throw new Error(`${Ln}: no fetch() in this environment`);let r=n?.baseUrl??e;if(t==="obj"){let s=await o(e);if(!s.ok)throw new Error(`${Ln}: ${e} \u2192 ${s.status}`);let i=await s.text(),c=n?.objOptions;if(n?.mtlUrl){let u=await o(n.mtlUrl);if(!u.ok)throw new Error(`${Ln}: ${n.mtlUrl} \u2192 ${u.status}`);let f=await u.text(),{colors:d,textures:m}=Ir(f),p={},h=globalThis.URL,g=globalThis.document?.baseURI,y=n.mtlUrl;if(h&&g)try{y=new h(n.mtlUrl,g).toString()}catch{}for(let[b,x]of Object.entries(m)){if(h)try{p[b]=new h(x,y).toString();continue}catch{}let P=n.mtlUrl.lastIndexOf("/"),S=P>=0?n.mtlUrl.slice(0,P+1):"";p[b]=x.startsWith("/")||/^https?:\/\//.test(x)?x:S+x}c={...c??{},materialColors:{...d,...c?.materialColors??{}},materialTextures:{...p,...c?.materialTextures??{}}}}let a=vr(i,c),l=await zu(a,n);return Xn(l,{meshResolution:n?.meshResolution,source:a})}if(t==="glb"||t==="gltf"){let s=await o(e);if(!s.ok)throw new Error(`${Ln}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer(),c=Cr(i,{baseUrl:r,...n?.gltfOptions??{}}),a=await zu(c,n);return Xn(a,{meshResolution:n?.meshResolution,source:c})}if(t==="vox"){let s=await o(e);if(!s.ok)throw new Error(`${Ln}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return Xn(_r(i,n?.voxOptions),{meshResolution:n?.meshResolution})}if(t==="stl"){let s=await o(e);if(!s.ok)throw new Error(`${Ln}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return Xn(wr(i,n?.stlOptions),{meshResolution:n?.meshResolution})}throw new Error(`${Ln}: unsupported extension ".${t}" (supported: obj, glb, gltf, vox, stl)`)}function we(e,n){if(Object.is(e,0)||Object.is(e,-0))return"0";if(e===1)return"1";if(e===-1)return"-1";let t=sr[n]??10**n,o=Math.round(e*t)/t;return Object.is(o,0)||Object.is(o,-0)?"0":String(o)}function Xu(e,n=Qt){let t=we(e,n);return t==="0"?"0":`${t}px`}function En(e,n=dn){if(e.length===0)return"";let t=we(e[0],n);for(let o=1;o<e.length;o++)t+=`,${we(e[o],n)}`;return t}function Yu(e,n,t,o,r=dn){return Nr(e[0],e[1],e[2],n[0],n[1],n[2],t[0],t[1],t[2],o[0],o[1],o[2],r)}function Nr(e,n,t,o,r,s,i,c,a,l,u,f,d=dn){if(d===3){let m=Math.round(e*1e3)/1e3||0,p=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,g=Math.round(o*1e3)/1e3||0,y=Math.round(r*1e3)/1e3||0,b=Math.round(s*1e3)/1e3||0,x=Math.round(i*1e3)/1e3||0,P=Math.round(c*1e3)/1e3||0,S=Math.round(a*1e3)/1e3||0,C=Math.round(l*1e3)/1e3||0,T=Math.round(u*1e3)/1e3||0,A=Math.round(f*1e3)/1e3||0;return`${m},${p},${h},0,${g},${y},${b},0,${x},${P},${S},0,${C},${T},${A},1`}return`${we(e,d)},${we(n,d)},${we(t,d)},0,${we(o,d)},${we(r,d)},${we(s,d)},0,${we(i,d)},${we(c,d)},${we(a,d)},0,${we(l,d)},${we(u,d)},${we(f,d)},1`}function Dr(e,n,t,o,r,s,i,c,a,l,u,f,d=dn){if(d!==3)return`matrix3d(${Nr(e,n,t,o,r,s,i,c,a,l,u,f,d)})`;let m=Math.round(e*1e3)/1e3||0,p=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,g=Math.round(o*1e3)/1e3||0,y=Math.round(r*1e3)/1e3||0,b=Math.round(s*1e3)/1e3||0,x=Math.round(i*1e3)/1e3||0,P=Math.round(c*1e3)/1e3||0,S=Math.round(a*1e3)/1e3||0,C=Math.round(l*1e3)/1e3||0,T=Math.round(u*1e3)/1e3||0,A=Math.round(f*1e3)/1e3||0;return`matrix3d(${m},${p},${h},0,${g},${y},${b},0,${x},${P},${S},0,${C},${T},${A},1)`}function At(e,n,t,o=0,r=0){let s=e.matrix.split(",").map(d=>Number(d));if(s.length!==16||s.some(d=>!Number.isFinite(d)))return e.matrix;let i=s[0],c=s[1],a=s[2],l=s[4],u=s[5],f=s[6];return s[0]*=n,s[1]*=n,s[2]*=n,s[4]*=t,s[5]*=t,s[6]*=t,s[12]+=o*i+r*l,s[13]+=o*c+r*u,s[14]+=o*a+r*f,En(s)}function Wu(e,n){return At(e,n.width/pn,n.height/pn,n.minX,n.minY)}function Qi(e){let n=e.seamBleedInsets??{left:0,right:0,top:0,bottom:0},t=e.canvasW||1,o=e.canvasH||1;return At(e,(t+n.left+n.right)/Zn,(o+n.top+n.bottom)/Zn,-n.left,-n.top)}function Fr(e,n){let t=e.matrix.split(",").map(o=>Number(o));return t.length!==16||t.some(o=>!Number.isFinite(o))?e.canonicalMatrix:(t[0]*=e.canvasW/n,t[1]*=e.canvasW/n,t[2]*=e.canvasW/n,t[4]*=e.canvasH/n,t[5]*=e.canvasH/n,t[6]*=e.canvasH/n,En(t))}function Tn(e,n=rr){let t=we(e,n);return Number(t)===0?"0":`${t}%`}function ju(e,n=dn){return`matrix3d(${e.split(",").map(t=>{let o=Number(t.trim());return Number.isFinite(o)?we(o,n):t.trim()}).join(",")})`}function Ku(e,n=Qt){let t=we(e,n);return Number(t)===0||Object.is(Number(t),-0)?"0":`${t}px`}function Hu(e){return`matrix3d(${Qi(e)})`}function vn(e,n,t){return e.map(o=>[o[1]*n,o[0]*n,o[2]*t])}function wn(e){if(e.length<3)return null;let n=e[0],t=0,o=0,r=0;for(let i=1;i+1<e.length;i++){let c=e[i],a=e[i+1],l=[c[0]-n[0],c[1]-n[1],c[2]-n[2]],u=[a[0]-n[0],a[1]-n[1],a[2]-n[2]];t-=l[1]*u[2]-l[2]*u[1],o-=l[2]*u[0]-l[0]*u[2],r-=l[0]*u[1]-l[1]*u[0]}let s=Math.hypot(t,o,r);return s<=de?null:(t/=s,o/=s,r/=s,[t,o,r])}function St(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=de)return!1;let c=Math.sign(i);if(n===0)n=c;else if(c!==n)return!1}return!0}function Br(e){let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-o[1]*r[0]}return n/2}function kr(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],c=o[1]-t[1],a=r*c-s*i;if(Math.abs(a)<=de)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*c-u*i)/a;return[e[0]+f*r,e[1]+f*s]}function Zu(e,n,t,o,r,s,i,c){let a=t-e,l=o-n,u=i-r,f=c-s,d=a*f-l*u;if(Math.abs(d)<=de)return null;let m=r-e,p=s-n,h=(m*f-p*u)/d;return[e+h*a,n+h*l]}function Jn(e,n){if(e.length<6||n<=0)return e;let t=0,o=0,r=e.length/2;for(let i=0;i<e.length;i+=2)t+=e[i],o+=e[i+1];t/=r,o/=r;let s=e.slice();for(let i=0;i<s.length;i+=2){let c=s[i]-t,a=s[i+1]-o,l=Math.hypot(c,a);l<=Me||(s[i]+=c/l*n,s[i+1]+=a/l*n)}return s}function et(e,n){if(e.length<6||e.length%2!==0||n<=0)return e;let t=[];for(let a=0;a<e.length;a+=2)t.push([e[a],e[a+1]]);if(!St(t))return Jn(e,n);let o=Br(t);if(Math.abs(o)<=de)return Jn(e,n);let r=o>0?1:-1,s=[];for(let a=0;a<t.length;a++){let l=t[a],u=t[(a+1)%t.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=de)return Jn(e,n);let p=r*(d/m)*n,h=r*(-f/m)*n;s.push({a:[l[0]+p,l[1]+h],b:[u[0]+p,u[1]+h]})}let i=[],c=Math.max(2,n*4);for(let a=0;a<t.length;a++){let l=s[(a+t.length-1)%t.length],u=s[a],f=kr(l.a,l.b,u.a,u.b);if(!f)return Jn(e,n);let d=t[a],m=f[0]-d[0],p=f[1]-d[1],h=Math.hypot(m,p);h>c?i.push(d[0]+m/h*c,d[1]+p/h*c):i.push(f[0],f[1])}return i}function Ct(e,n){if(e.length<6||e.length%2!==0)return e;let t=e.length/2;if(n.length!==t)return e;let o=n.map(f=>typeof f=="number"&&Number.isFinite(f)?Math.max(0,f):0),r=Math.max(0,...o);if(r<=0)return e;if(o.every(f=>Math.abs(f-o[0])<=de))return et(e,o[0]);let s=[];for(let f=0;f<e.length;f+=2)s.push([e[f],e[f+1]]);if(!St(s))return e;let i=Br(s);if(Math.abs(i)<=de)return e;let c=i>0?1:-1,a=[];for(let f=0;f<s.length;f++){let d=s[f],m=s[(f+1)%s.length],p=m[0]-d[0],h=m[1]-d[1],g=Math.hypot(p,h);if(g<=de)return e;let y=o[f],b=c*(h/g)*y,x=c*(-p/g)*y;a.push({a:[d[0]+b,d[1]+x],b:[m[0]+b,m[1]+x]})}let l=[],u=Math.max(2,r*4);for(let f=0;f<s.length;f++){let d=a[(f+s.length-1)%s.length],m=a[f],p=kr(d.a,d.b,m.a,m.b);if(!p)return e;let h=s[f],g=p[0]-h[0],y=p[1]-h[1],b=Math.hypot(g,y);b>u?l.push(h[0]+g/b*u,h[1]+y/b*u):l.push(p[0],p[1])}return l}function lo(e,n,t,o,r,s,i){if(i<=0)return[e,n,t,o,r,s];let c=(e*o-n*t+t*s-o*r+r*n-s*e)/2,a=()=>Jn([e,n,t,o,r,s],i);if(Math.abs(c)<=de)return a();let l=c>0?1:-1,u=t-e,f=o-n,d=Math.sqrt(u*u+f*f),m=r-t,p=s-o,h=Math.sqrt(m*m+p*p),g=e-r,y=n-s,b=Math.sqrt(g*g+y*y);if(d<=de||h<=de||b<=de)return a();let x=l*(f/d)*i,P=l*(-u/d)*i,S=e+x,C=n+P,T=t+x,A=o+P,M=l*(p/h)*i,E=l*(-m/h)*i,v=t+M,I=o+E,_=r+M,O=s+E,V=l*(y/b)*i,w=l*(-g/b)*i,F=r+V,D=s+w,H=e+V,K=n+w,Q=H-F,ne=K-D,ee=T-S,ue=A-C,U=Q*ue-ne*ee;if(Math.abs(U)<=de)return a();let $=S-F,k=C-D,L=($*ue-k*ee)/U,W=F+L*Q,Y=D+L*ne,se=T-S,Z=A-C,B=_-v,q=O-I,me=se*q-Z*B;if(Math.abs(me)<=de)return a();let fe=v-S,G=I-C,te=(fe*q-G*B)/me,ce=S+te*se,ye=C+te*Z,he=_-v,be=O-I,ge=H-F,Se=K-D,De=he*Se-be*ge;if(Math.abs(De)<=de)return a();let R=F-v,z=D-I,j=(R*Se-z*ge)/De,J=v+j*he,le=I+j*be,re=Math.max(2,i*4),pe=W-e,xe=Y-n,ie=Math.sqrt(pe*pe+xe*xe);ie>re&&(W=e+pe/ie*re,Y=n+xe/ie*re);let ae=ce-t,N=ye-o,X=Math.sqrt(ae*ae+N*N);X>re&&(ce=t+ae/X*re,ye=o+N/X*re);let oe=J-r,Te=le-s,Ce=Math.sqrt(oe*oe+Te*Te);return Ce>re&&(J=r+oe/Ce*re,le=s+Te/Ce*re),[W,Y,ce,ye,J,le]}function Ur(e,n,t,o){let r=e+n;if(o<=0||t<=de||r<=de||!Number.isFinite(e+n+t+o))return lo(e,0,0,t,r,t,o);let s=Math.sqrt(e*e+t*t),i=Math.sqrt(n*n+t*t);if(s<=de||i<=de)return lo(e,0,0,t,r,t,o);let c=-o*t/s,a=-o*e/s,l=o*t/i,u=-o*n/i,f=e+c,d=a,m=r+l,p=t+u,h=-t*r;if(Math.abs(h)<=de)return lo(e,0,0,t,r,t,o);let g=f-m,y=d-p,b=(g*t+y*e)/h,x=m-b*n,P=p-b*t,S=-o*(e+s)/t,C=t+o,T=r+o*(n+i)/t,A=C,M=Math.max(2,o*4),E=x-e,v=P,I=Math.sqrt(E*E+v*v);I>M&&(x=e+E/I*M,P=v/I*M);let _=Math.sqrt(S*S+o*o);_>M&&(S=S/_*M,C=t+o/_*M);let O=T-r,V=Math.sqrt(O*O+o*o);return V>M&&(T=r+O/V*M,A=t+o/V*M),[x,P,S,C,T,A]}function Ji(e,n){if(e.screenPts.length<6||n.vertices.length<3)return null;let t=e.tileSize,o=e.layerElevation,r=vn(n.vertices,t,o),s=wn(r);if(!s)return null;let i=e.screenPts[0],c=e.screenPts[1],a=e.screenPts[2],l=e.screenPts[3],u=e.screenPts[4],f=e.screenPts[5],d=a-i,m=l-c,p=u-i,h=f-c,g=d*h-m*p;if(Math.abs(g)<=de)return null;let y=r[0],b=r[1],x=r[2],P=[b[0]-y[0],b[1]-y[1],b[2]-y[2]],S=[x[0]-y[0],x[1]-y[1],x[2]-y[2]],C=[(P[0]*h-m*S[0])/g,(P[1]*h-m*S[1])/g,(P[2]*h-m*S[2])/g],T=[(d*S[0]-P[0]*p)/g,(d*S[1]-P[1]*p)/g,(d*S[2]-P[2]*p)/g],A=y[0]-C[0]*i-T[0]*c,M=y[1]-C[1]*i-T[1]*c,E=y[2]-C[2]*i-T[2]*c;return{normal:s,xAxis:C,yAxis:T,tx:A,ty:M,tz:E}}function Gr(e){return Math.max(0,Math.min(6,Math.floor(e??dn)))}function qu(e){return`${e[0]},${e[1]},${e[2]}`}function Qu(e,n){let t=qu(e),o=qu(n);return t<o?`${t}|${o}`:`${o}|${t}`}function Ju(e){let n=new Map;for(let o=0;o<e.length;o++){let r=e[o].vertices;if(!(!r||r.length<3||!e[o].texture))for(let s=0;s<r.length;s++){let i=Qu(r[s],r[(s+1)%r.length]),c=n.get(i),a={polygon:o,edge:s};c?c.push(a):n.set(i,[a])}}let t=e.map(()=>new Set);for(let o of n.values())if(!(o.length<2))for(let r=0;r<o.length;r++)for(let s=r+1;s<o.length;s++)t[o[r].polygon].add(o[r].edge),t[o[s].polygon].add(o[s].edge);return t.map(o=>o.size>0?o:void 0)}function uo(e,n){return typeof e=="number"&&Number.isFinite(e)?Math.max(0,e):n}function zr(e){return typeof e=="number"&&Number.isFinite(e)&&e>0?e:void 0}function fo(e,n,t){if(t<=0||e.length<6||e.length%2!==0)return 0;let o=e.length/2;if(n<0||n>=o)return 0;let r=[];for(let A=0;A<e.length;A+=2)r.push([e[A],e[A+1]]);let[s,i]=r[n],[c,a]=r[(n+1)%o],l=c-s,u=a-i,f=Math.hypot(l,u);if(f<=Me)return 0;let d=[],m=Number.POSITIVE_INFINITY;for(let A=0;A<o;A+=1){if(A===n||A===(n+1)%o)continue;let[M,E]=r[A],v=Math.abs((M-s)*u-(E-i)*l)/f;v>Me&&(m=Math.min(m,v))}Number.isFinite(m)&&d.push(m*.5);let p=r[(n+o-1)%o],h=r[(n+2)%o],g=p[0]-s,y=p[1]-i,b=h[0]-c,x=h[1]-a,P=Math.hypot(g,y),S=Math.hypot(b,x);if(P>Me){let A=Math.abs(l*y-u*g)/(f*P);A>Me&&d.push(P*A)}if(S>Me){let A=Math.abs(-l*x- -u*b)/(f*S);A>Me&&d.push(S*A)}let C=d.filter(A=>Number.isFinite(A)&&A>0);if(C.length===0)return Number.isFinite(t)?Math.max(0,t):0;let T=Math.min(...C);return Math.max(0,Math.min(Number.isFinite(t)?t:T,T))}function $r(e,n,t){if(!n?.size||t===void 0)return;let o=new Map;for(let r of n){let s=fo(e,r,t);s>0&&o.set(r,s)}return o.size>0?o:void 0}function Xr(e,n){return e<3||!n?.size?null:Array.from({length:e},(t,o)=>Math.max(0,n.get(o)??0))}function Yr(e,n){if(!n?.size||e.length<6||e.length%2!==0)return;let t=e.length/2,o=Number.POSITIVE_INFINITY,r=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];o=Math.min(o,u),r=Math.min(r,f),s=Math.max(s,u),i=Math.max(i,f)}if(!Number.isFinite(o+r+s+i))return;let c=Math.max(Me*8,.25),a={left:0,right:0,top:0,bottom:0};for(let[l,u]of n){let f=Math.max(0,u);if(f<=0||l<0||l>=t)continue;let d=l*2,m=(l+1)%t*2,p=e[d],h=e[d+1],g=e[m],y=e[m+1];Math.abs(p-g)<=c?Math.abs(p-o)<=c&&Math.abs(g-o)<=c?a.left=Math.max(a.left,f):Math.abs(p-s)<=c&&Math.abs(g-s)<=c&&(a.right=Math.max(a.right,f)):Math.abs(h-y)<=c&&(Math.abs(h-r)<=c&&Math.abs(y-r)<=c?a.top=Math.max(a.top,f):Math.abs(h-i)<=c&&Math.abs(y-i)<=c&&(a.bottom=Math.max(a.bottom,f)))}return a.left||a.right||a.top||a.bottom?a:void 0}function Gh(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function zh(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let o=vn(e.vertices,n,t),r=wn(o);return r?{normal:r,planeD:Gh(r,o[0])}:null}function $h(e,n){return(e.material?.key??e.color??"")===(n.material?.key??n.color??"")&&e.color===n.color}function ef(e,n={}){return new Set(ea(e,n).keys())}function ea(e,n={}){let t=n.tileSize??50,o=n.layerElevation??t,r=e.map(a=>zh(a,t,o)),s=new Map;for(let a=0;a<e.length;a++){let l=e[a].vertices;if(!(!l||l.length<3))for(let u=0;u<l.length;u++){let f=Qu(l[u],l[(u+1)%l.length]),d=s.get(f),m={polygon:a,edge:u};d?d.push(m):s.set(f,[m])}}let i=new Map,c=(a,l)=>{let u=i.get(a);u?u.add(l):i.set(a,new Set([l]))};for(let a of s.values())if(!(a.length<2))for(let l=0;l<a.length;l++)for(let u=l+1;u<a.length;u++){let f=a[l],d=a[u],m=f.polygon,p=d.polygon;!r[m]||!r[p]||$h(e[m],e[p])&&(c(m,f.edge),c(p,d.edge))}return i}function Xh(e,n,t,o,r,s){let i=(e-t)*(s-o)-(n-o)*(r-t);return Math.abs(i)>eo?!1:(e-t)*(e-r)+(n-o)*(n-s)<=eo}function na(e,n=Jt,t=Jt){let o=!1;for(let r=0,s=e.length-1;r<e.length;s=r++){let[i,c]=e[r],[a,l]=e[s];if(Xh(n,t,i,c,a,l))return!0;c>t!=l>t&&n<(a-i)*(t-c)/(l-c)+i&&(o=!o)}return o}function ta(e,n,t){let o=Number.POSITIVE_INFINITY,r=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];!Number.isFinite(u)||!Number.isFinite(f)||(o=Math.min(o,u),r=Math.min(r,f),s=Math.max(s,u),i=Math.max(i,f))}let c=s-o,a=i-r;return!Number.isFinite(o)||!Number.isFinite(r)||!Number.isFinite(c)||!Number.isFinite(a)||c<=de||a<=de?{minX:0,minY:0,width:n,height:t}:{minX:o,minY:r,width:c,height:a}}function Et(e){let n=e.bleedRatio??1,t=lr*n,o=e.canvasW||1,r=e.canvasH||1,s=t>0?et(e.screenPts,t):e.screenPts,i=t>0?ta(s,o,r):{minX:0,minY:0,width:o,height:r},c=[];for(let a=0;a<s.length;a+=2){let l=Math.max(0,Math.min(100,(s[a]-i.minX)/i.width*100)),u=Math.max(0,Math.min(100,(s[a+1]-i.minY)/i.height*100));c.push([l,u])}return{bounds:i,points:c}}function oa(e){let n=[];for(let t of e){let o=n[n.length-1];o&&Math.hypot(o[0]-t[0],o[1]-t[1])<=no||n.push(t)}if(n.length>1){let t=n[0],o=n[n.length-1];Math.hypot(t[0]-o[0],t[1]-o[1])<=no&&n.pop()}return n}function ra([e,n]){let t=new Set;return Math.abs(e)<=We&&t.add("left"),Math.abs(e-100)<=We&&t.add("right"),Math.abs(n)<=We&&t.add("top"),Math.abs(n-100)<=We&&t.add("bottom"),t.size>0?t:null}function sa(e,n){for(let t of e)if(n.has(t))return!0;return!1}function ia(e,n,t,o){let r=(s,i,c)=>{let a=n.has(i)?e:o.has(i)?t:null,l=n.has(c)?e:o.has(c)?t:null;if(!a||!l)return null;let u=(()=>{switch(s){case"topLeft":return{x:a[0],y:l[1]};case"topRight":return{x:100-a[0],y:l[1]};case"bottomRight":return{x:100-a[0],y:100-l[1]};case"bottomLeft":return{x:a[0],y:100-l[1]}}})();return u.x>We&&u.y>We&&u.x<100-We&&u.y<100-We?[s,u]:null};return(n.has("top")||o.has("top"))&&(n.has("left")||o.has("left"))?r("topLeft","top","left"):(n.has("top")||o.has("top"))&&(n.has("right")||o.has("right"))?r("topRight","top","right"):(n.has("bottom")||o.has("bottom"))&&(n.has("right")||o.has("right"))?r("bottomRight","bottom","right"):(n.has("bottom")||o.has("bottom"))&&(n.has("left")||o.has("left"))?r("bottomLeft","bottom","left"):null}function Wr(e){if(e.texture||nt(e)||Nn(e))return null;let n=Et(e),t=oa(n.points);if(t.length<4)return null;let o=t.map(ra);if(o.some(i=>!i))return null;let r={},s=0;for(let i=0;i<t.length;i+=1){let c=o[i],a=o[(i+1)%t.length];if(sa(c,a))continue;let l=ia(t[i],c,t[(i+1)%t.length],a);if(!l)return null;let[u,f]=l,d=r[u];if(d&&(Math.abs(d.x-f.x)>We||Math.abs(d.y-f.y)>We))return null;r[u]=f,s+=1}return s>0?{bounds:n.bounds,radii:r}:null}function Yh([e,n]){return`${Tn(e)} ${Tn(n)}`}function Wh(e){return`polygon(${e.map(Yh).join(",")})`}function jh(e){if(na(e))return"circle(0)";let n=0,t=0,o=Math.max(1,e.length);for(let[i,c]of e)n+=i,t+=c;let r=Tn(Math.max(0,Math.min(100,n/o))),s=Tn(Math.max(0,Math.min(100,t/o)));return`circle(0 at ${r} ${s})`}function jr(e){return`${Wh(e)} ${jh(e)}`}function nf(e){return jr(Et(e).points)}function aa(e,n){return At(e,n.width/pn,n.height/pn,n.minX,n.minY)}function tf(e){let n=Et(e);return`matrix3d(${aa(e,n.bounds)})`}function of(e){let n=Et(e);return[`transform:matrix3d(${aa(e,n.bounds)})`,`border-shape:${jr(n.points)}`].join(";")}function rf(e,n){let t=[`transform:matrix3d(${aa(e,n.bounds)})`,`width:${pn}px`,`height:${pn}px`,"border:0","box-sizing:border-box","background:currentColor"];for(let[o,r]of Object.entries(n.radii)){let s=o.replace(/[A-Z]/g,i=>`-${i.toLowerCase()}`);t.push(`border-${s}-radius:${Tn(r.x)} ${Tn(r.y)}`),t.push(`corner-${s}-shape:bevel`)}return t.join(";")}function ca(e){if(e.screenPts.length!==8)return null;let n=[],t=[],o=.001,r=(s,i)=>{for(let c of s)if(Math.abs(c-i)<=o)return;s.push(i)};for(let s=0;s<e.screenPts.length;s+=2)r(n,e.screenPts[s]),r(t,e.screenPts[s+1]);if(n.length!==2||t.length!==2||(n.sort((s,i)=>s-i),t.sort((s,i)=>s-i),Math.abs(n[0])>o||Math.abs(t[0])>o||n[1]-n[0]<=o||t[1]-t[0]<=o))return null;for(let s=0;s<e.screenPts.length;s+=2){let i=e.screenPts[s],c=e.screenPts[s+1],a=Math.abs(i-n[0])<=o||Math.abs(i-n[1])<=o,l=Math.abs(c-t[0])<=o||Math.abs(c-t[1])<=o;if(!a||!l)return null}return{left:n[0],top:t[0],width:n[1]-n[0],height:t[1]-t[0]}}function Nn(e){return!!ca(e)}function nt(e){return!e.texture&&e.polygon.vertices.length===3}function Tt(e){return!e.texture&&!!e.projectiveMatrix&&!Nn(e)}function sf(e){let n=/\b(?:Chrome|HeadlessChrome|Chromium|Edg|OPR)\//.test(e);return/\bVersion\/[\d.]+.*\bSafari\//.test(e)&&!n}function Kr(e,n){e.set(n,(e.get(n)??0)+1)}function Hr(e){let n,t=1;for(let[o,r]of e)r>t&&(n=o,t=r);return n}function af(e,n,t,o){let r=!t.has("b"),s=r&&o.projectiveQuadSupported,i=!t.has("u")&&o.solidTriangleSupported,c=!t.has("i")&&!!o.cornerShapeSupported,a=!t.has("i")&&o.borderShapeSupported,l=t.has("b");return e.map(u=>{if(!u||u.texture)return u;if(i&&nt(u))return null;let f=Nn(u);return c&&!f&&!(s&&Tt(u))&&Wr(u)!==null||r&&f||s&&Tt(u)||a&&(!f||l)?null:u})}function cf(e,n,t,o,r,s,i){let c=new Map,a=new Map,l=new Map,u=!t.has("b"),f=u&&o.projectiveQuadSupported,d=!t.has("u")&&o.solidTriangleSupported,m=!t.has("i")&&o.cornerShapeSupported,p=!t.has("i")&&o.borderShapeSupported;for(let y of e){if(!y||y.texture)continue;let b=m&&!!i?.(y);if(n==="dynamic"){if(!(d&&nt(y))&&!(u&&Nn(y))&&!(f&&Tt(y))&&!b&&!p)continue;let x=r(y.polygon.color??"#cccccc"),P=s(x);Kr(a,P),l.has(P)||l.set(P,x);continue}!(d&&nt(y))&&!(u&&Nn(y))&&!(f&&Tt(y))&&!b&&!p||Kr(c,y.shadedColor)}let h=Hr(c),g=Hr(a);return{paintColor:h,dynamicColorKey:g,dynamicColor:g?l.get(g):void 0}}function Kh(e,n){if((e+1)%3===n)return e;if((n+1)%3===e)return n}function Hh(e,n,t,o,r,s,i){if(!e?.size)return null;let c=uo(n,t);return[[s,o],[o,r],[r,s]].map(([l,u],f)=>{let d=Kh(l,u),m=d!==void 0&&e.has(d)?c:0;return fo(i,f,m)})}function Zr(e,n,t,o,r,s,i,c){let a=s,l="",u,f,d="";if(i){let m=c??e.color??"#cccccc",p=s.directionalLight,h=s.ambientLight,g=p?.direction??Yn,y=p?.color??Wn,b=Math.max(0,p?.intensity??jn),x=h?.color??Kn,P=Math.max(0,h?.intensity??Hn),S=Math.hypot(g[0],g[1],g[2])||1,C=g[0]/S,T=g[1]/S,A=g[2]/S,E=s.lightOccludedPolyIndices?.has(n)??!1?0:b*Math.max(0,t*C+o*T+r*A),v=Vn(m,E,y,x,P),I=s.textureLighting??"baked",_=I==="baked"&&a.stableTriangleColorSteps?gr(v,a.stableTriangleColorSteps):v,O=gn(m),V=_===s.solidPaintDefaults?.paintColor,w=I==="dynamic"&&pr(O)===s.solidPaintDefaults?.dynamicColorKey;l=I==="dynamic"||V?"":_,u=l?gn(l):void 0,f=l?oo(l):void 0,d=I==="dynamic"?`--pnx:${t.toFixed(4)};--pny:${o.toFixed(4)};--pnz:${r.toFixed(4)};`+(w?"":`--psr:${(O.r/255).toFixed(4)};--psg:${(O.g/255).toFixed(4)};--psb:${(O.b/255).toFixed(4)};`):""}return{index:n,polygon:e,colorComputed:i,bakedColor:l||void 0,bakedRgb:u,bakedAlpha:f,dynamicVars:d}}function lf(e,n,t){if(e.texture||e.vertices.length!==3)return null;let o=t.tileSize??50,r=t.layerElevation??o,s=e.vertices[0],i=e.vertices[1],c=e.vertices[2],a=[s[1]*o,s[0]*o,s[2]*r],l=[i[1]*o,i[0]*o,i[2]*r],u=[c[1]*o,c[0]*o,c[2]*r],f=l[0]-a[0],d=l[1]-a[1],m=l[2]-a[2],p=u[0]-a[0],h=u[1]-a[1],g=u[2]-a[2],y=-(d*g-m*h),b=-(m*p-f*g),x=-(f*h-d*p),P=Math.sqrt(y*y+b*b+x*x);return P<=de?null:(y/=P,b/=P,x/=P,Zr(e,n,y,b,x,t,!0))}function uf(e,n,t,o={}){if(e.texture||e.vertices.length!==3)return null;let r=t.tileSize??50,s=t.layerElevation??r,i=e.vertices[0],c=e.vertices[1],a=e.vertices[2],l=i[1]*r,u=i[0]*r,f=i[2]*s,d=c[1]*r,m=c[0]*r,p=c[2]*s,h=a[1]*r,g=a[0]*r,y=a[2]*s;return qr(e,n,t,o,l,u,f,d,m,p,h,g,y)}function qr(e,n,t,o,r,s,i,c,a,l,u,f,d){let m=t,p=c-r,h=a-s,g=l-i,y=u-r,b=f-s,x=d-i,P=-(h*x-g*b),S=-(g*y-p*x),C=-(p*b-h*y),T=Math.sqrt(P*P+S*S+C*C);if(T<=de)return null;P/=T,S/=T,C/=T;let A=o.basis,M=A?.a??0,E=A?.b??1,v=A?.c??2;M<0||M>2||E<0||E>2||v<0||v>2||M===E||M===v||E===v?(A=void 0,M=0,E=1,v=2):M===0&&E===1&&v===2||M===1&&E===2&&v===0||M===2&&E===0&&v===1||(A=void 0,M=0,E=1,v=2);let I=()=>A?qr(e,n,t,{...o,basis:void 0},r,s,i,c,a,l,u,f,d):null;if(!A){let ve=p*p+h*h+g*g,en=u-c,an=f-a,nn=d-l,He=r-u,cn=s-f,st=i-d,ln=en*en+an*an+nn*nn,Ze=He*He+cn*cn+st*st,it=ve;ln>it&&(M=1,E=2,v=0,it=ln),Ze>it&&(M=2,E=0,v=1)}let _,O,V,w,F,D,H=v===0?r:v===1?c:u,K=v===0?s:v===1?a:f,Q=v===0?i:v===1?l:d;M===0?(_=r,O=s,V=i):M===1?(_=c,O=a,V=l):(_=u,O=f,V=d),E===0?(w=r,F=s,D=i):E===1?(w=c,F=a,D=l):(w=u,F=f,D=d);let ne=w-_,ee=F-O,ue=D-V,U=Math.sqrt(ne*ne+ee*ee+ue*ue);if(U<=de)return I();let $=ne/U,k=ee/U,L=ue/U,W=(H-_)*$+(K-O)*k+(Q-V)*L,Y=S*L-C*k,se=C*$-P*L,Z=P*k-S*$,B=T/U;if(B<=de)return I();let q=Math.max(0,Math.min(U,W)),me=Math.max(0,U-q),fe=[q,0,0,B,q+me,B],G=or*(m.bleedRatio??1),te=Hh(m.seamEdges,m.seamBleed,G,M,E,v,fe),ce=te?Ct(fe,te):Ur(q,me,B,uo(m.seamBleed,G)),ye=ce[0],he=ce[1],be=ce[2],ge=ce[3],Se=ce[4],De=ce[5],R=(ge+De)/2,z=ye-be,j=Se-ye,J=R-he;if(z<=de||j<=de||J<=de||!Number.isFinite(z+j+J))return I();let le=o.includeColor??!0,re=!1,pe,xe,ie,ae="";if(le){let ve=Zr(e,n,P,S,C,t,!0,o.color);re=ve.colorComputed,pe=ve.bakedColor,xe=ve.bakedRgb,ie=ve.bakedAlpha,ae=ve.dynamicVars??""}let N=pe?`color:${pe};`:"",X=o.primitive??o.resolvedPrimitive??"border",Te=1/(X==="border-large"?ar:ir),Ce=z+j,Pe=Ce*Te,_e=(j-z)*.5*Te,Oe=J*Te,Ve=ye-q-Ce*.5,$e=he,Ee=$*Pe,Fe=k*Pe,Ne=L*Pe,ot=$*_e+Y*Oe,rt=k*_e+se*Oe,hn=L*_e+Z*Oe,Je=H+$*Ve+Y*$e,yn=K+k*Ve+se*$e,Be=Q+L*Ve+Z*$e,je=o.matrixDecimals??Gr(m.stableTriangleMatrixDecimals),Ae=Dr(Ee,Fe,Ne,ot,rt,hn,P,S,C,Je,yn,Be,je),Re=t.textureLighting??"baked",Ie=m.optimizeStableTriangleStyle===!0&&Re==="baked"?"":`transform:${Ae};`+N+ae,Ke=A&&A.a===M&&A.b===E&&A.c===v?A:{a:M,b:E,c:v};return{index:n,polygon:e,styleText:Ie,transformText:Ae,basis:Ke,primitive:X,colorComputed:re,bakedColor:pe,bakedRgb:xe,bakedAlpha:ie,dynamicVars:ae}}function ff(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function la(e){let n=e?.maxWeightRatio,t=Math.max(0,ff(e?.denomEps,ur)),o=typeof n=="number"&&Number.isFinite(n)&&n>0?Math.max(1,n):fr,r=Math.max(0,ff(e?.bleed,mr));return{denomEps:t,maxWeightRatio:o,bleed:r,disableGuards:e?.disableGuards===!0}}function Qr(e,n){if(e.length!==4||!St(e))return null;let[t,o,r,s]=e,i=t[0]-o[0]+r[0]-s[0],c=t[1]-o[1]+r[1]-s[1],a=o[0]-r[0],l=s[0]-r[0],u=o[1]-r[1],f=s[1]-r[1],d=a*f-u*l;if(Math.abs(d)<=de)return null;let m=(i*f-c*l)/d,p=(a*c-u*i)/d,h=[1,1+m,1+m+p,1+p];if(h.some(b=>!Number.isFinite(b)))return null;let g=Math.min(...h),y=Math.max(...h);return!n.disableGuards&&(g<=n.denomEps||y/g>n.maxWeightRatio)?null:{g:m,h:p,w1:1+m,w3:1+p}}function ua(e,n,t,o,r,s,i,c,a){if(e.length!==8)return null;let l=[[e[0],e[1]],[e[2],e[3]],[e[4],e[5]],[e[6],e[7]]];if(!Qr(l,c))return null;let u=Xr(4,a),f=u?Ct(e,u):et(e,c.bleed),d=[[f[0],f[1]],[f[2],f[3]],[f[4],f[5]],[f[6],f[7]]],m=Qr(d,c);if(!m)return null;let{g:p,h,w1:g,w3:y}=m,[b,x,,P]=d,S=[r+b[0]*n[0]+b[1]*t[0],s+b[0]*n[1]+b[1]*t[1],i+b[0]*n[2]+b[1]*t[2]],C=([A,M],E)=>[(E-1)*r+(E*A-b[0])*n[0]+(E*M-b[1])*t[0],(E-1)*s+(E*A-b[0])*n[1]+(E*M-b[1])*t[1],(E-1)*i+(E*A-b[0])*n[2]+(E*M-b[1])*t[2]],T=[...C(x,g),p,...C(P,y),h,o[0],o[1],o[2],0,S[0],S[1],S[2],1];for(let A=0;A<8;A+=1)T[A]/=Zn;return En(T,6)}function mo(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function mf(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 fa(e){return!e.texture}function ma(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let o=vn(e.vertices,n,t),r=wn(o);return r?{pts:o,normal:r,planeD:mo(r,o[0]),optimizable:fa(e)}:null}function da(e,n){return!e||!n||!e.optimizable||!n.optimizable?!1:ts(e,n)}function ts(e,n){return!e||!n||mo(e.normal,n.normal)<1-nr?!1:Math.abs(e.planeD-n.planeD)<=tr}function Jr(e,n){if(!e)return null;let t=n.directionalLight,o=n.ambientLight,r=t?.direction??Yn,s=t?.color??Wn,i=Math.max(0,t?.intensity??jn),c=o?.color??Kn,a=Math.max(0,o?.intensity??Hn),l=Math.hypot(r[0],r[1],r[2])||1,u=r[0]/l,f=r[1]/l,d=r[2]/l,m=i*Math.max(0,e.normal[0]*u+e.normal[1]*f+e.normal[2]*d),p=so(m,s,c,a);return p.r*.2126+p.g*.7152+p.b*.0722}function pa(e){let n=[...e],t=Math.abs(n[0])>de?0:Math.abs(n[1])>de?1:2;return n[t]<0&&(n[0]*=-1,n[1]*=-1,n[2]*=-1),`${n[0].toFixed(6)},${n[1].toFixed(6)},${n[2].toFixed(6)}`}function tt(e,n,t,o,r={}){let s=mo(o,t),i=[o[0]-s*t[0],o[1]-s*t[1],o[2]-s*t[2]],c=Math.hypot(i[0],i[1],i[2]);if(c<=de)return null;let a=[i[0]/c,i[1]/c,i[2]/c],l=[t[1]*a[2]-t[2]*a[1],t[2]*a[0]-t[0]*a[2],t[0]*a[1]-t[1]*a[0]],u=Math.hypot(l[0],l[1],l[2]);if(u<=de)return null;let f=[l[0]/u,l[1]/u,l[2]/u],d=e.map(V=>{let w=V[0]-n[0],F=V[1]-n[1],D=V[2]-n[2];return[w*a[0]+F*a[1]+D*a[2],w*f[0]+F*f[1]+D*f[2]]}),m=r.boundsOrigin??n,p=n[0]-m[0],h=n[1]-m[1],g=n[2]-m[2],y=p*a[0]+h*a[1]+g*a[2],b=p*f[0]+h*f[1]+g*f[2],x=1/0,P=1/0,S=-1/0,C=-1/0;for(let[V,w]of d){let F=V+y,D=w+b;F<x&&(x=F),F>S&&(S=F),D<P&&(P=D),D>C&&(C=D)}let T=S-x,A=C-P;if(!Number.isFinite(T)||!Number.isFinite(A))return null;let M=r.snapBounds?Math.floor(x+Me):x,E=r.snapBounds?Math.floor(P+Me):P,v=r.snapBounds?Math.ceil(S-Me):S,I=r.snapBounds?Math.ceil(C-Me):C,_=Math.max(1,r.snapBounds?v-M:Math.ceil(T)),O=Math.max(1,r.snapBounds?I-E:Math.ceil(A));return{xAxis:a,yAxis:f,local2D:d,shiftX:y-M,shiftY:b-E,canvasW:_,canvasH:O,pixelArea:_*O,rawArea:T*A}}function es(e){return`${e[0]},${e[1]},${e[2]}`}function df(e,n){return es(e)<es(n)?[n[0]-e[0],n[1]-e[1],n[2]-e[2]]:[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Zh(e,n){let t=es(e),o=es(n);return t<o?`${t}|${o}`:`${o}|${t}`}function ga(e,n,t,o){let r=0,s=0;for(let i of e){let c=n[i];if(!c)return null;let a=tt(c.pts,c.pts[0],c.normal,t,{boundsOrigin:o,snapBounds:!0});if(!a)return null;r+=a.pixelArea,s+=a.rawArea}return{pixelArea:r,rawArea:s}}function ha(e,n){let t=n[e[0]]?.pts[0];if(!t)return null;let o={pixelArea:0,rawArea:0},r=null,s=new Set;for(let i of e){let c=n[i];if(!c)continue;let a=[c.pts[1][0]-c.pts[0][0],c.pts[1][1]-c.pts[0][1],c.pts[1][2]-c.pts[0][2]],l=tt(c.pts,c.pts[0],c.normal,a);o&&l?(o.pixelArea+=l.pixelArea,o.rawArea+=l.rawArea):o=null;for(let u=0;u<c.pts.length;u++){let f=df(c.pts[u],c.pts[(u+1)%c.pts.length]),d=tt(c.pts,c.pts[0],c.normal,f);if(!d)continue;let m=pa(d.xAxis);if(s.has(m))continue;s.add(m);let p=ga(e,n,d.xAxis,t);p&&(!r||p.pixelArea<r.pixelArea||p.pixelArea===r.pixelArea&&p.rawArea<r.rawArea-Me)&&(r={xAxis:d.xAxis,...p})}}return r&&o&&(r.pixelArea<o.pixelArea||r.pixelArea===o.pixelArea&&r.rawArea<=o.rawArea+Me)?{xAxis:r.xAxis,boundsOrigin:t,seamEdges:new Set}:null}function pf(e,n){let t=n.tileSize??50,o=n.layerElevation??t,r=e.map(f=>ma(f,t,o)),s=new Map,i=e.map(()=>new Set),c=e.map(()=>new Set);for(let f=0;f<e.length;f++){let d=e[f].vertices;if(!(!d||d.length<3))for(let m=0;m<d.length;m++){let p=Zh(d[m],d[(m+1)%d.length]),h=s.get(p),g={polygon:f,edge:m};h?h.push(g):s.set(p,[g])}}let a=e.map(()=>new Set);for(let f of s.values())if(!(f.length<2))for(let d=0;d<f.length;d++)for(let m=d+1;m<f.length;m++){let p=f[d],h=f[m],g=p.polygon,y=h.polygon;if(e[g].texture&&e[y].texture&&(c[p.polygon].add(p.edge),c[h.polygon].add(h.edge)),ts(r[g],r[y]))i[p.polygon].add(p.edge),i[h.polygon].add(h.edge);else{let b=Jr(r[g],n),x=Jr(r[y],n);b!==null&&x!==null&&(b<=x+qt&&i[p.polygon].add(p.edge),x<=b+qt&&i[h.polygon].add(h.edge))}da(r[g],r[y])&&(a[g].add(y),a[y].add(g))}let l=Array(e.length).fill(void 0),u=new Set;for(let f=0;f<e.length;f++){if(u.has(f)||!r[f]?.optimizable)continue;let d=[],m=[f];for(u.add(f);m.length>0;){let h=m.pop();d.push(h);for(let g of a[h])u.has(g)||(u.add(g),m.push(g))}if(d.length<2)continue;let p=ha(d,r);if(p)for(let h of d)l[h]={xAxis:p.xAxis,boundsOrigin:p.boundsOrigin,seamEdges:i[h],textureEdgeRepairEdges:c[h]}}for(let f=0;f<e.length;f++)!l[f]&&(i[f].size>0||c[f].size>0)&&(l[f]={seamEdges:i[f],textureEdgeRepairEdges:c[f]});return l}function ns(e,n,t,o){if(o.optimize&&o.fixedXAxis)return tt(e,n,t,o.fixedXAxis,{boundsOrigin:o.boundsOrigin,snapBounds:o.snapBounds});let r=null,s=o.optimize&&o.seamEdges&&o.seamEdges.size>0?Array.from(o.seamEdges):null,i=s??(o.optimize?e.map((c,a)=>a):[0]);for(let c of i){let a=(c+1)%e.length,l=s?df(e[c],e[a]):[e[a][0]-e[c][0],e[a][1]-e[c][1],e[a][2]-e[c][2]],u=tt(e,n,t,l,{boundsOrigin:o.boundsOrigin,snapBounds:o.snapBounds});u&&(!r||u.pixelArea<r.pixelArea||u.pixelArea===r.pixelArea&&u.rawArea<r.rawArea-Me)&&(r=u)}return r}function ya(e){if(e.local2D.length!==4)return!1;let n=[],t=[],o=(r,s)=>{for(let i of r)if(Math.abs(i-s)<=Me)return;r.push(s)};for(let[r,s]of e.local2D)o(n,r+e.shiftX),o(t,s+e.shiftY);if(n.length!==2||t.length!==2||(n.sort((r,s)=>r-s),t.sort((r,s)=>r-s),Math.abs(n[0])>Me||Math.abs(t[0])>Me||n[1]-n[0]<=Me||t[1]-t[0]<=Me))return!1;for(let[r,s]of e.local2D){let i=r+e.shiftX,c=s+e.shiftY,a=Math.abs(i-n[0])<=Me||Math.abs(i-n[1])<=Me,l=Math.abs(c-t[0])<=Me||Math.abs(c-t[1])<=Me;if(!a||!l)return!1}return!0}function os(e,n){if(e.length<3||n.length<3)return null;let[t,o,r]=e,[s,i,c]=n,a=t[0],l=t[1],u=o[0],f=o[1],d=r[0],m=r[1],p=s[0],h=1-s[1],g=i[0],y=1-i[1],b=c[0],x=1-c[1],P=g-p,S=y-h,C=b-p,T=x-h,A=P*T-C*S;if(Math.abs(A)<=1e-9)return null;let M=u-a,E=d-a,v=f-l,I=m-l,_={a:(M*T-E*S)/A,b:(P*E-C*M)/A,c:(v*T-I*S)/A,d:(P*I-C*v)/A,e:0,f:0};return _.e=a-_.a*p-_.b*h,_.f=l-_.c*p-_.d*h,_}function rs(e){if(e.length===0)return null;let n=1/0,t=1/0,o=-1/0,r=-1/0;for(let s of e){let i=s[0],c=1-s[1];if(!Number.isFinite(i)||!Number.isFinite(c))return null;n=Math.min(n,i),o=Math.max(o,i),t=Math.min(t,c),r=Math.max(r,c)}return{minU:n,minV:t,maxU:o,maxV:r}}function ba(e,n,t,o,r,s,i,c){let l=vn(e.vertices,n,t).map(d=>{let m=d[0]-o[0],p=d[1]-o[1],h=d[2]-o[2];return[m*r[0]+p*r[1]+h*r[2]+i,m*s[0]+p*s[1]+h*s[2]+c]}),u=os(l,e.uvs),f=rs(e.uvs);return!u&&!f?null:{screenPts:l.flatMap(([d,m])=>[d,m]),uvAffine:u,uvSampleRect:f}}function xa(e,n,t,o,r){let s=t,{vertices:i,texture:c,uvs:a}=e;if(!i||i.length<3)return null;let l=t.tileSize??50,u=t.layerElevation??l,f=vn(i,l,u),d=f[0],m=f[1],p=[m[0]-d[0],m[1]-d[1],m[2]-d[2]];if(Math.hypot(p[0],p[1],p[2])===0)return null;let g=wn(f);if(!g)return null;let y=ns(f,d,g,{optimize:!1}),b=c||y&&ya(y)?y:ns(f,d,g,{optimize:!0,fixedXAxis:r?.xAxis,boundsOrigin:r?.boundsOrigin,snapBounds:!!r,seamEdges:r?.seamEdges});if(!b)return null;let{xAxis:x,yAxis:P,local2D:S}=b,C=c&&r?.textureEdgeRepairEdges?.size?r.textureEdgeRepairEdges:null,T=!!(c&&C),A=b.shiftX,M=b.shiftY,E=b.canvasW,v=b.canvasH,I=[];for(let[ge,Se]of S)I.push(ge+A,Se+M);let _=d[0]-A*x[0]-M*P[0],O=d[1]-A*x[1]-M*P[1],V=d[2]-A*x[2]-M*P[2],w=En([x[0],x[1],x[2],0,P[0],P[1],P[2],0,g[0],g[1],g[2],0,_,O,V,1]),F=En([x[0]*E,x[1]*E,x[2]*E,0,P[0]*v,P[1]*v,P[2]*v,0,g[0],g[1],g[2],0,_,O,V,1]),D=En([x[0]*E/Ye,x[1]*E/Ye,x[2]*E/Ye,0,P[0]*v/Ye,P[1]*v/Ye,P[2]*v/Ye,0,g[0],g[1],g[2],0,_,O,V,1]),H=zr(s.seamBleed),K=$r(I,s.seamEdges??r?.seamEdges,H),Q=K?new Set(K.keys()):void 0,ne=K?Math.max(...K.values()):void 0,ee=Yr(I,K),ue=!c&&i.length===4?ua(I,x,P,g,_,O,V,o,K):null,U=t.directionalLight,$=t.ambientLight,k=U?.direction??Yn,L=U?.color??Wn,W=Math.max(0,U?.intensity??jn),Y=$?.color??Kn,se=Math.max(0,$?.intensity??Hn),Z=Math.hypot(k[0],k[1],k[2])||1,B=k[0]/Z,q=k[1]/Z,me=k[2]/Z,G=t.lightOccludedPolyIndices?.has(n)??!1?0:W*Math.max(0,g[0]*B+g[1]*q+g[2]*me),te=so(G,L,Y,se),ce=Vn(e.color??"#cccccc",G,L,Y,se),ye=null,he=null;c&&a&&a.length>=3&&a.length===i.length&&(he=rs(a),ye=os(S.map(([ge,Se])=>[ge+A,Se+M]),a));let be=c&&e.textureTriangles?.length?e.textureTriangles.map(ge=>ba(ge,l,u,d,x,P,A,M)).filter(ge=>!!ge):null;return{index:n,polygon:e,texture:c,tileSize:l,layerElevation:u,matrix:w,canonicalMatrix:F,atlasMatrix:D,projectiveMatrix:ue,canvasW:E,canvasH:v,screenPts:I,uvAffine:ye,uvSampleRect:he,textureTriangles:be,textureEdgeRepairEdges:C,textureEdgeRepair:T,seamBleed:ne,seamBleedEdges:Q,seamBleedEdgeAmounts:K,seamBleedInsets:ee,bleedRatio:dr(s.seamBleed),normal:g,textureTint:te,shadedColor:ce}}function gf(e,n,t={},o,r){let s=la(o),i=t,c=r??(t.textureEdgeRepairEdges?.size?{seamEdges:new Set,textureEdgeRepairEdges:t.textureEdgeRepairEdges}:void 0);return xa(e,n,i,s,c)}function It(e){let n=typeof e=="string"?Number(e):e;return n===void 0||!Number.isFinite(n)?1:Math.min(Ho,Math.max(Ko,n))}function is(e){return e.reduce((n,t)=>n+t.width*t.height,0)}function Pa(e,n){let t=is(e);if(t<=0)return 1;let o=Math.max(1,...e.map(i=>Math.max(i.width,i.height))),r=ht/o,s=Math.sqrt(n/(t*4));return It(Math.min(r,s))}function Ma(e,n){let t=is(e),o=.5;return t<=Zt?o=1:t<=Zo&&(o=.75),It(Math.min(o,Pa(e,n)))}function Aa(e,n){return e.reduce((t,o)=>Math.max(t,Math.ceil(o.width*n),Math.ceil(o.height*n)),0)}function Sa(e,n){return e.reduce((t,o)=>t+Math.ceil(o.width*n)*Math.ceil(o.height*n)*4,0)}function Ca(e,n,t){let o=Aa(e,n),r=Sa(e,n),s=o>ht?ht/o:1,i=r>t?Math.sqrt(t/r):1;return Math.min(s,i)}function Ea(e){return e?qo:Qo}function Ta(e,n){return e!==void 0&&e!=="auto"?Ye:n?Ye:cr}function ss(e,n){for(let t of e.entries)t&&(t.atlasCanonicalSize=n,t.atlasMatrix=Fr(t,n));return e}function hf(e){return e.atlasCanonicalSize??Ye}function va(e){return Math.max(Ht,Math.ceil(Ht/e))}function vt(e,n=1){let t=Array(e.length).fill(null),o=[],r=va(n),s=e.filter(a=>!!a).sort((a,l)=>l.canvasH-a.canvasH||l.canvasW-a.canvasW||a.index-l.index),i=()=>({width:r,height:r,entries:[],shelves:[]}),c=(a,l,u)=>{if(a.sealed)return null;for(let m of a.shelves)if(l.canvasH<=m.height&&m.x+l.canvasW+r<=Cn){let p={...l,pageIndex:u,x:m.x,y:m.y};return m.x+=l.canvasW+r*2,a.entries.push(p),a.width=Math.max(a.width,p.x+l.canvasW+r),p}let f=a.shelves.length===0?r:a.height+r;if(f+l.canvasH+r>Cn)return null;let d={...l,pageIndex:u,x:r,y:f};return a.shelves.push({x:r+l.canvasW+r*2,y:f,height:l.canvasH}),a.entries.push(d),a.width=Math.max(a.width,d.x+l.canvasW+r),a.height=Math.max(a.height,f+l.canvasH+r),d};for(let a of s){if(a.canvasW+r*2>Cn||a.canvasH+r*2>Cn){let f=o.length,d={...a,pageIndex:f,x:r,y:r};t[a.index]=d,o.push({width:a.canvasW+r*2,height:a.canvasH+r*2,entries:[d],shelves:[],sealed:!0});continue}let u=null;for(let f=0;f<o.length&&(u=c(o[f],a,f),!u);f++);if(!u){let f=i(),d=o.length;o.push(f),u=c(f,a,d)}u&&(t[a.index]=u)}return{entries:t,pages:o.map(({width:a,height:l,entries:u})=>({width:a,height:l,entries:u}))}}function qh(e,n,t){let o=Ma(n.pages,t),r=o===1?n:vt(e,o);for(let s=0;s<4;s++){let i=Ca(r.pages,o,t);if(i>=1)break;let c=It(o*i*Jo);if(c>=o)break;o=c,r=vt(e,o)}return{packed:r,atlasScale:o}}function yf(e,n,t){let o=Ta(n,t);if(n!==void 0&&n!=="auto"){let i=It(n);return{packed:ss(vt(e,i),o),atlasScale:i,atlasCanonicalSize:o}}let r=vt(e,1),s=qh(e,r,Ea(t));return{packed:ss(s.packed,o),atlasScale:s.atlasScale,atlasCanonicalSize:o}}0&&(module.exports={ASYNC_RENDER_BUDGET_MS,ATLAS_CANONICAL_SIZE_AUTO_DESKTOP,ATLAS_CANONICAL_SIZE_EXPLICIT,ATLAS_MAX_SIZE,ATLAS_PADDING,AUTO_ATLAS_LOW_AREA,AUTO_ATLAS_MAX_BITMAP_SIDE,AUTO_ATLAS_MAX_DECODED_BYTES_DESKTOP,AUTO_ATLAS_MAX_DECODED_BYTES_MOBILE,AUTO_ATLAS_MEDIUM_AREA,AUTO_ATLAS_SCALE_GUARD,BAKED_SHADOW_MIN_UP,BAKED_SHADOW_Z_SQUASH,BASE_TILE,BASIS_EPS,BORDER_SHAPE_BLEED,BORDER_SHAPE_CANONICAL_SIZE,BORDER_SHAPE_CENTER_PERCENT,BORDER_SHAPE_POINT_EPS,CAMERA_BACKFACE_CULL_EPS,COLOR_PARSE_CACHE_MAX,CORNER_SHAPE_DUPLICATE_EPS,CORNER_SHAPE_POINT_EPS,DECIMAL_SCALES,DEFAULT_AMBIENT_COLOR,DEFAULT_AMBIENT_INTENSITY,DEFAULT_ATLAS_CSS_DECIMALS,DEFAULT_BORDER_SHAPE_DECIMALS,DEFAULT_CAMERA_STATE,DEFAULT_LIGHT_COLOR,DEFAULT_LIGHT_DIR,DEFAULT_LIGHT_INTENSITY,DEFAULT_MATRIX_DECIMALS,DEFAULT_PROJECTION,DEFAULT_SEAM_BLEED,DEFAULT_SEAM_FACET_SPLIT_OPTIONS,DEFAULT_SEAM_OVERLAP_OPTIONS,DEFAULT_TILE,LoopOnce,LoopPingPong,LoopRepeat,MAX_ATLAS_SCALE,MIN_ATLAS_SCALE,PROJECTIVE_QUAD_BLEED,PROJECTIVE_QUAD_DENOM_EPS,PROJECTIVE_QUAD_MAX_WEIGHT_RATIO,QUAT_IDENTITY,RECEIVER_NORMAL_TOL,RECEIVER_OFFSET_TOL,RECEIVER_OUTLINE_EXPAND,RECT_EPS,SEAM_LIGHT_EPS,SOLID_QUAD_CANONICAL_SIZE,SOLID_TRIANGLE_BLEED,SOLID_TRIANGLE_CANONICAL_SIZE,SOLID_TRIANGLE_LARGE_BORDER_CANONICAL_SIZE,SURFACE_DISTANCE_EPS,SURFACE_NORMAL_EPS,TEXTURE_EDGE_REPAIR_ALPHA_MIN,TEXTURE_EDGE_REPAIR_RADIUS,TEXTURE_EDGE_REPAIR_SOURCE_ALPHA_MIN,TEXTURE_TRIANGLE_BLEED,VOXEL_CAMERA_CULL_AXIS_EPS,VOXEL_CAMERA_CULL_NORMAL_LIMIT,applyPackedAtlasCanonicalSize,arrowPolygons,atlasArea,atlasBitmapMaxSide,atlasCanonicalSizeForEntry,atlasCanonicalSizeForTextureQuality,atlasDecodedBytes,atlasPadding,autoAtlasBudgetFactor,autoAtlasMaxDecodedBytes,autoAtlasScale,autoAtlasScaleCap,axesHelperPolygons,bakeSolidTextureSampledPolygons,bakeSolidTextureSamples,basisAxisKey,borderShapeBoundsFromPoints,borderShapeGeometryForPlan,boxPolygons,buildBakedShadowProjectionMatrix,buildBasisHints,buildEdgeOwners,buildPolyMeshTransform,buildPolySceneTransform,buildSceneContext,buildSeamBleedPolygonEdges,buildSeamBleedPolygonSet,buildSharedEdgeMap,buildTextureEdgeRepairSets,cachedParsePureColor,cameraCullNormalGroups,cameraCullNormalGroupsFromPolygons,cameraCullNormalKey,cameraCullVisibleSignature,cameraFacingDepth,chooseIslandXAxis,chooseLocalBasis,clampChannel,classifyFacing,clipPolygonToConvex2D,colorErrorScore,compatibleBleedSurface,compatibleSurface,computeLightVisibility,computePlanSeamBleedEdgeAmounts,computeProjectiveQuadCoefficients,computeProjectiveQuadMatrix,computeReceiverShadowFaces,computeSceneBbox,computeSeamBleedInsets,computeShapeLighting,computeSolidTriangleColorPlan,computeSolidTriangleColorPlanFromNormal,computeSolidTrianglePlan,computeSolidTrianglePlanFromCssPoints,computeSurfaceNormal,computeTextureAtlasPlan,computeTextureAtlasPlanPublic,computeTexturePaintMetrics,computeUvAffine,computeUvSampleRect,conePolygons,convexHull2D,cornerShapeDiagonal,cornerShapeGeometryForPlan,cornerShapePointSides,coverPlanarPolygons,createIsometricCamera,createPolyAnimationMixer,crossVec,cssBorderShapeForGeometry,cssBorderShapeForPlan,cssDistanceToWorld,cssPoints,cssPositionToWorld,cullInteriorPolygons,cylinderPolygons,dedupeOverlappingPolygons,dodecahedronPolygons,dominantCountKey,dotVec,ensureCcw2D,eulerXYZFromQuat,evaluateIslandAxis,expandClipPoints,expandConvexHullOutward,extractSilhouetteLoops,filterAtlasPlans,findOverlappingPolygonDuplicates,formatAffineMatrix3dColumns,formatAffineMatrix3dScalars,formatAffineMatrix3dTransformScalars,formatAtlasMatrix,formatBorderShapeElementStyle,formatBorderShapeEntryMatrix,formatBorderShapeMatrix,formatColor,formatCornerShapeElementStyle,formatCssLength,formatCssLengthPx,formatMatrix3d,formatMatrix3dValues,formatPercent,formatScaledMatrixFromPlan,formatSolidQuadEntryMatrix,formatSolidQuadMatrix,fullRectBounds,getPolygonBasisInfo,getSolidPaintDefaultsForPlansCore,groupReceiverFaceGroups,icosahedronPolygons,incrementCount,intersect2DLines,intersect2DLinesRaw,inverseRotateVec3,isAxisAlignedSurfaceNormal,isBakedShadowCaster,isBasisOptimizable,isConvexPolygonPoints,isFullRectBasis,isFullRectSolid,isProjectiveQuadPlan,isSolidTrianglePlan,isVoxelCameraCullableNormalGroups,loadMesh,makeLocalBasis,mergePolygons,meshScaleVec3,normalFacesCamera,normalizeAtlasScale,normalizeInvertMultiplier,normalizePolygons,normalizedSeamBleed,octahedronPolygons,offsetConvexPolygonPoints,offsetConvexPolygonPointsByEdgeAmounts,offsetStableTrianglePoints,offsetTrianglePoints,optimizeAnimatedMeshPolygons,optimizeMeshParseResult,optimizeMeshPolygons,packTextureAtlasPlans,packTextureAtlasPlansWithScaleCore,parseAlpha,parseColor,parseGltf,parseHex,parseHexColor,parseMtl,parseObj,parsePureColor,parseRgbColor,parseStl,parseVox,planePolygons,polyCssDistanceToWorld,polyCssPositionToWorld,polygonContainsPoint,polygonCssSurfaceNormal,polygonFaces,polygonFacesCamera,polygonSignedArea2D,prepareCasterEdgeOwners,prepareCasterPolyItems,prepareReceiverFacePlanes,projectCssVertexToGround,projectTextureTriangle,quantizeCssColor,quatFromAxisAngle,quatFromEulerXYZ,quatMultiply,repairMeshSeams,resolveBleedRatio,resolveProjectiveQuadGuards,resolveSeamBleed,rgbEqual,rgbKey,rgbToCss,rgbToHex,ringPolygons,ringQuadPolygons,rotateVec3,rotateVec3InWrapperCssFrame,roundDecimal,safariCssProjectiveUnsupported,safePlanSeamBleedAmount,screenToWorldOnSphere,screenToWorldRay,seamBleedAmountArray,seamFacetSplitPolygons,seamFacetSplitReport,seamLightBrightness,seamOverlapDiagnostics,seamOverlapPolygons,seamOverlapReport,shadeColor,shadePolygon,sharedCornerShapeSide,signedArea2D,simplifyCornerShapePoints,simplifyTriangleMeshPolygons,spherePolygons,stableBasisFromPlan,stableTriangleMatrixDecimals,stepRgbToward,tetrahedronPolygons,textureTintFactors,tintToCss,torusPolygons,worldCssForMesh,worldDirectionToCss,worldDirectionToPolyCss,worldDirectionalLightToCss,worldDirectionalLightToPolyCss,worldDistanceToCss,worldDistanceToPolyCss,worldPositionToCss,worldPositionToPolyCss});
|
|
1
|
+
"use strict";var gs=Object.defineProperty;var zf=Object.getOwnPropertyDescriptor;var Gf=Object.getOwnPropertyNames;var $f=Object.prototype.hasOwnProperty;var Xf=(e,n)=>{for(var t in n)gs(e,t,{get:n[t],enumerable:!0})},Yf=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of Gf(n))!$f.call(e,r)&&r!==t&&gs(e,r,{get:()=>n[r],enumerable:!(o=zf(n,r))||o.enumerable});return e};var Wf=e=>Yf(gs({},"__esModule",{value:!0}),e);var Ty={};Xf(Ty,{ASYNC_RENDER_BUDGET_MS:()=>Zl,ATLAS_CANONICAL_SIZE_AUTO_DESKTOP:()=>gr,ATLAS_CANONICAL_SIZE_EXPLICIT:()=>Ye,ATLAS_MAX_SIZE:()=>En,ATLAS_PADDING:()=>Zt,AUTO_ATLAS_LOW_AREA:()=>qt,AUTO_ATLAS_MAX_BITMAP_SIDE:()=>yt,AUTO_ATLAS_MAX_DECODED_BYTES_DESKTOP:()=>sr,AUTO_ATLAS_MAX_DECODED_BYTES_MOBILE:()=>rr,AUTO_ATLAS_MEDIUM_AREA:()=>or,AUTO_ATLAS_SCALE_GUARD:()=>ir,BAKED_SHADOW_MIN_UP:()=>Bl,BAKED_SHADOW_Z_SQUASH:()=>Fl,BASE_TILE:()=>Ue,BASIS_EPS:()=>de,BORDER_SHAPE_BLEED:()=>hr,BORDER_SHAPE_CANONICAL_SIZE:()=>hn,BORDER_SHAPE_CENTER_PERCENT:()=>eo,BORDER_SHAPE_POINT_EPS:()=>no,CAMERA_BACKFACE_CULL_EPS:()=>Ht,COLOR_PARSE_CACHE_MAX:()=>ar,CORNER_SHAPE_DUPLICATE_EPS:()=>to,CORNER_SHAPE_POINT_EPS:()=>We,DECIMAL_SCALES:()=>mr,DEFAULT_AMBIENT_COLOR:()=>Kn,DEFAULT_AMBIENT_INTENSITY:()=>Zn,DEFAULT_ATLAS_CSS_DECIMALS:()=>Jt,DEFAULT_BORDER_SHAPE_DECIMALS:()=>fr,DEFAULT_CAMERA_STATE:()=>Ee,DEFAULT_LIGHT_COLOR:()=>jn,DEFAULT_LIGHT_DIR:()=>Wn,DEFAULT_LIGHT_INTENSITY:()=>Hn,DEFAULT_MATRIX_DECIMALS:()=>pn,DEFAULT_PROJECTION:()=>ka,DEFAULT_SEAM_BLEED:()=>nu,DEFAULT_SEAM_FACET_SPLIT_OPTIONS:()=>Us,DEFAULT_SEAM_OVERLAP_OPTIONS:()=>ks,DEFAULT_TILE:()=>dn,LoopOnce:()=>Zi,LoopPingPong:()=>qi,LoopRepeat:()=>Er,MAX_ATLAS_SCALE:()=>tr,MIN_ATLAS_SCALE:()=>nr,PROJECTIVE_QUAD_BLEED:()=>br,PROJECTIVE_QUAD_DENOM_EPS:()=>yr,PROJECTIVE_QUAD_MAX_WEIGHT_RATIO:()=>xr,QUAT_IDENTITY:()=>nc,RECEIVER_NORMAL_TOL:()=>ji,RECEIVER_OFFSET_TOL:()=>Hi,RECEIVER_OUTLINE_EXPAND:()=>Wi,RECT_EPS:()=>Me,SEAM_LIGHT_EPS:()=>Qt,SOLID_QUAD_CANONICAL_SIZE:()=>gn,SOLID_TRIANGLE_BLEED:()=>ur,SOLID_TRIANGLE_CANONICAL_SIZE:()=>dr,SOLID_TRIANGLE_LARGE_BORDER_CANONICAL_SIZE:()=>pr,SURFACE_DISTANCE_EPS:()=>lr,SURFACE_NORMAL_EPS:()=>cr,TEXTURE_EDGE_REPAIR_ALPHA_MIN:()=>Ql,TEXTURE_EDGE_REPAIR_RADIUS:()=>eu,TEXTURE_EDGE_REPAIR_SOURCE_ALPHA_MIN:()=>Jl,TEXTURE_TRIANGLE_BLEED:()=>ql,VOXEL_CAMERA_CULL_AXIS_EPS:()=>bi,VOXEL_CAMERA_CULL_NORMAL_LIMIT:()=>Pi,applyPackedAtlasCanonicalSize:()=>kf,applyPackedAtlasLeafSizing:()=>Po,arrowPolygons:()=>_i,atlasArea:()=>ps,atlasBitmapMaxSide:()=>Ra,atlasCanonicalSizeForEntry:()=>Fa,atlasCanonicalSizeForTextureQuality:()=>Na,atlasDecodedBytes:()=>Va,atlasPadding:()=>Ba,autoAtlasBudgetFactor:()=>La,autoAtlasMaxDecodedBytes:()=>wa,autoAtlasScale:()=>Oa,autoAtlasScaleCap:()=>_a,axesHelperPolygons:()=>Ei,bakeSolidTextureSampledPolygons:()=>Hu,bakeSolidTextureSamples:()=>Lr,basisAxisKey:()=>Sa,borderShapeBoundsFromPoints:()=>da,borderShapeGeometryForPlan:()=>vt,boxPolygons:()=>Ii,buildBakedShadowProjectionMatrix:()=>kl,buildBasisHints:()=>Vf,buildEdgeOwners:()=>Ar,buildPolyCameraSceneTransform:()=>lc,buildPolyMeshTransform:()=>Hl,buildPolySceneTransform:()=>Kl,buildSceneContext:()=>qa,buildSeamBleedPolygonEdges:()=>fa,buildSeamBleedPolygonSet:()=>Af,buildSharedEdgeMap:()=>cu,buildTextureEdgeRepairSets:()=>Sf,cachedParsePureColor:()=>qn,cameraCullNormalGroups:()=>Ai,cameraCullNormalGroupsFromPolygons:()=>Vl,cameraCullNormalKey:()=>Si,cameraCullVisibleSignature:()=>wl,cameraFacingDepth:()=>Mi,capturePolyCameraSnapshot:()=>uc,chooseIslandXAxis:()=>Ca,chooseLocalBasis:()=>rs,clampChannel:()=>_n,classifyFacing:()=>Cr,clipPolygonToConvex2D:()=>Tn,colorErrorScore:()=>iu,compatibleBleedSurface:()=>ss,compatibleSurface:()=>Ma,computeLightVisibility:()=>Ol,computePlanSeamBleedEdgeAmounts:()=>Kr,computeProjectiveQuadCoefficients:()=>ns,computeProjectiveQuadMatrix:()=>xo,computeReceiverShadowFaces:()=>mu,computeSceneBbox:()=>Ps,computeSeamBleedInsets:()=>qr,computeShapeLighting:()=>Ya,computeSolidTriangleColorPlan:()=>Ff,computeSolidTriangleColorPlanFromNormal:()=>ms,computeSolidTrianglePlan:()=>Bf,computeSolidTrianglePlanFromCssPoints:()=>ds,computeSurfaceNormal:()=>Nn,computeTextureAtlasPlan:()=>va,computeTextureAtlasPlanPublic:()=>Lf,computeTexturePaintMetrics:()=>Ja,computeUvAffine:()=>is,computeUvSampleRect:()=>as,conePolygons:()=>Bi,convexHull2D:()=>Zo,cornerShapeDiagonal:()=>ya,cornerShapeGeometryForPlan:()=>Qr,cornerShapePointSides:()=>ga,coverPlanarPolygons:()=>_o,createIsometricCamera:()=>fc,createPolyAnimationMixer:()=>du,crossVec:()=>Of,cssBorderShapeForGeometry:()=>Jr,cssBorderShapeForPlan:()=>Cf,cssDistanceToWorld:()=>Gi,cssPoints:()=>In,cssPositionToWorld:()=>$i,cullInteriorPolygons:()=>fn,cylinderPolygons:()=>Kt,dedupeOverlappingPolygons:()=>Pc,dodecahedronPolygons:()=>Fi,dominantCountKey:()=>fs,dotVec:()=>bo,ensureCcw2D:()=>mn,eulerXYZFromQuat:()=>sc,evaluateIslandAxis:()=>Aa,expandClipPoints:()=>Jn,expandConvexHullOutward:()=>Yi,extractSilhouetteLoops:()=>Tr,filterAtlasPlans:()=>Nf,findOverlappingPolygonDuplicates:()=>Lt,formatAffineMatrix3dColumns:()=>pf,formatAffineMatrix3dScalars:()=>Gr,formatAffineMatrix3dTransformScalars:()=>$r,formatAtlasMatrix:()=>Ct,formatBorderShapeElementStyle:()=>Ef,formatBorderShapeEntryMatrix:()=>Tf,formatBorderShapeMatrix:()=>gf,formatColor:()=>Ot,formatCornerShapeElementStyle:()=>vf,formatCssLength:()=>df,formatCssLengthPx:()=>yf,formatMatrix3d:()=>hf,formatMatrix3dValues:()=>an,formatPercent:()=>vn,formatScaledMatrixFromPlan:()=>At,formatSolidQuadEntryMatrix:()=>xf,formatSolidQuadMatrix:()=>ua,fullRectBounds:()=>Ia,getPolygonBasisInfo:()=>Pa,getSolidPaintDefaultsForPlansCore:()=>Df,groupReceiverFaceGroups:()=>er,icosahedronPolygons:()=>Di,incrementCount:()=>us,intersect2DLines:()=>Yr,intersect2DLinesRaw:()=>bf,inverseRotateVec3:()=>ec,isAxisAlignedSurfaceNormal:()=>Ci,isBakedShadowCaster:()=>Ul,isBasisOptimizable:()=>ba,isConvexPolygonPoints:()=>Tt,isFullRectBasis:()=>Ta,isFullRectSolid:()=>Dn,isProjectiveQuadPlan:()=>It,isSolidTrianglePlan:()=>nt,isVoxelCameraCullableNormalGroups:()=>Ll,loadMesh:()=>mf,makeLocalBasis:()=>tt,mergePolygons:()=>tn,meshScaleVec3:()=>Jo,normalFacesCamera:()=>gt,normalizeAtlasScale:()=>ot,normalizeInvertMultiplier:()=>cc,normalizePolygons:()=>Ao,normalizedSeamBleed:()=>Hr,octahedronPolygons:()=>Li,offsetConvexPolygonPoints:()=>et,offsetConvexPolygonPointsByEdgeAmounts:()=>Et,offsetStableTrianglePoints:()=>Wr,offsetTrianglePoints:()=>uo,optimizeAnimatedMeshPolygons:()=>Zu,optimizeMeshParseResult:()=>Yn,optimizeMeshPolygons:()=>ll,packTextureAtlasPlans:()=>_t,packTextureAtlasPlansWithScaleCore:()=>Uf,parseAlpha:()=>ro,parseColor:()=>Fn,parseGltf:()=>Rr,parseHex:()=>yn,parseHexColor:()=>hs,parseMtl:()=>Nr,parseObj:()=>wr,parsePureColor:()=>Pn,parseRgbColor:()=>ys,parseStl:()=>zr,parseVox:()=>Dr,planePolygons:()=>Vi,polyCameraTargetToCss:()=>Ss,polyCssDistanceToWorld:()=>$l,polyCssPositionToWorld:()=>Yl,polygonContainsPoint:()=>ma,polygonCssSurfaceNormal:()=>jo,polygonFaces:()=>Qa,polygonFacesCamera:()=>Rl,polygonSignedArea2D:()=>Ui,prepareCasterEdgeOwners:()=>lu,prepareCasterPolyItems:()=>uu,prepareReceiverFacePlanes:()=>fu,projectCssVertexToGround:()=>zl,projectTextureTriangle:()=>Ea,quantizeCssColor:()=>Sr,quatFromAxisAngle:()=>oc,quatFromEulerXYZ:()=>rc,quatMultiply:()=>tc,repairMeshSeams:()=>Hc,resolveAtlasLeafBox:()=>Da,resolveBleedRatio:()=>Pr,resolvePolyCameraAppliedPerspectiveStyle:()=>As,resolvePolyTextureImageRendering:()=>If,resolvePolyTextureImageSource:()=>go,resolvePolyTextureLeafGeometry:()=>ls,resolvePolyTexturePresentation:()=>ho,resolvePolyTextureUrl:()=>es,resolveProjectiveQuadGuards:()=>yo,resolveSeamBleed:()=>mo,rgbEqual:()=>ou,rgbKey:()=>Mr,rgbToCss:()=>su,rgbToHex:()=>so,ringPolygons:()=>Oi,ringQuadPolygons:()=>Ri,rotateVec3:()=>Co,rotateVec3InWrapperCssFrame:()=>On,roundDecimal:()=>Ne,safariCssProjectiveUnsupported:()=>wf,safePlanSeamBleedAmount:()=>po,screenToWorldOnSphere:()=>dc,screenToWorldRay:()=>Cs,seamBleedAmountArray:()=>Zr,seamFacetSplitPolygons:()=>Ws,seamFacetSplitReport:()=>jc,seamLightBrightness:()=>ts,seamOverlapDiagnostics:()=>Kc,seamOverlapPolygons:()=>js,seamOverlapReport:()=>qc,shadeColor:()=>Xa,shadePolygon:()=>Vn,sharedCornerShapeSide:()=>ha,signedArea2D:()=>Xr,simplifyCornerShapePoints:()=>pa,simplifyTriangleMeshPolygons:()=>Uo,spherePolygons:()=>wi,stableBasisFromPlan:()=>fo,stableTriangleMatrixDecimals:()=>jr,stepRgbToward:()=>ru,tetrahedronPolygons:()=>Ni,textureTintFactors:()=>io,tintToCss:()=>tu,torusPolygons:()=>ki,worldCssForMesh:()=>ht,worldDirectionToCss:()=>Qo,worldDirectionToPolyCss:()=>Wl,worldDirectionalLightToCss:()=>Xi,worldDirectionalLightToPolyCss:()=>jl,worldDistanceToCss:()=>zi,worldDistanceToPolyCss:()=>Gl,worldPositionToCss:()=>Qe,worldPositionToPolyCss:()=>Xl});module.exports=Wf(Ty);var ka="cubic";function hs(e){let n=e.replace("#","");if(n.length===3){let t=parseInt(n[0]+n[0],16),o=parseInt(n[1]+n[1],16),r=parseInt(n[2]+n[2],16);return Number.isNaN(t)||Number.isNaN(o)||Number.isNaN(r)?null:{rgb:[t,o,r],alpha:1}}if(n.length===6){let t=parseInt(n.slice(0,2),16),o=parseInt(n.slice(2,4),16),r=parseInt(n.slice(4,6),16);return Number.isNaN(t)||Number.isNaN(o)||Number.isNaN(r)?null:{rgb:[t,o,r],alpha:1}}return null}function ys(e){let n=e.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)/i);return n?{rgb:[Number(n[1]),Number(n[2]),Number(n[3])],alpha:n[4]?Number(n[4]):1}:null}function Pn(e){if(!e)return null;let n=e.trim(),t=hs(n);return t||ys(n)}function _n(e){return Math.max(0,Math.min(255,Math.round(e)))}function Ot(e){let[n,t,o]=e.rgb.map(_n);return e.alpha<1?`rgba(${n}, ${t}, ${o}, ${e.alpha})`:`rgb(${n}, ${t}, ${o})`}var bs={rgb:[204,204,204],alpha:1},Ua=new Map;function Fn(e){if(!e)return null;let n=e.trim(),t=Ua.get(n);if(t)return t;let o=Pn(n);return o?(Ua.set(n,o),o):null}function Xa(e,n){let t=Fn(e)??bs,o=[_n(t.rgb[0]+n),_n(t.rgb[1]+n),_n(t.rgb[2]+n)];return Ot({rgb:o,alpha:t.alpha})}var xs={direction:[0,0,-1],color:"#ffffff",intensity:1},za={color:"#ffffff",intensity:.4};function Ga(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 $a(e,n,t){let o=Fn(n)??bs;return e*(o.rgb[t]/255)}function Ya(e,n,t,o){let r=Fn(n)??bs,s=Ga(t?.direction??xs.direction),i=t?.color??xs.color,a=Math.max(0,t?.intensity??xs.intensity),c=o?.color??za.color,l=Math.max(0,o?.intensity??za.intensity),u=Ga(e),f=Math.max(0,-(u[0]*s[0]+u[1]*s[1]+u[2]*s[2])),d=a*f,m=[0,0,0];for(let p=0;p<3;p=p+1){let h=r.rgb[p],g=$a(h,c,p)*l,y=$a(h,i,p)*d;m[p]=_n(g+y)}return Ot({rgb:m,alpha:r.alpha})}var Wa="#cccccc",Mo=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],ja=(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]],Ha=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],So=e=>Math.hypot(e[0],e[1],e[2]);function jf(e){let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0;for(let u of e)u[0]<n&&(n=u[0]),u[0]>r&&(r=u[0]),u[1]<t&&(t=u[1]),u[1]>s&&(s=u[1]),u[2]<o&&(o=u[2]),u[2]>i&&(i=u[2]);let a=r-n,c=s-t,l=i-o;return Math.hypot(a,c,l)}function Hf(){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 Ka(e){if(e)return{...e,sourceRect:e.sourceRect?{...e.sourceRect}:void 0}}function Za(e){return e?{...e}:void 0}function Kf(e){if(e)return{...e,imageSource:Ka(e.imageSource),presentation:Za(e.presentation)}}function Ao(e){let n=[],t=[];if(!e||e.length===0)return{polygons:n,warnings:t};for(let o=0;o<e.length;o++){let r=e[o];if(!r||!Array.isArray(r.vertices)){t.push(`Polygon ${o}: missing vertices, dropped`);continue}let s=r.vertices;if(s.length<3){t.push(`Polygon ${o}: ${s.length} vertices (need >= 3), dropped`);continue}let i=Mo(s[1],s[0]),a=Mo(s[2],s[0]),c=ja(i,a),l=So(c);if(s.length===3){if(l<1e-12){So(i)<1e-12||So(a)<1e-12?t.push(`Polygon ${o}: zero-area triangle (coincident vertices), dropped`):t.push(`Polygon ${o}: vertices collinear, dropped`);continue}}else if(l<1e-12){t.push(`Polygon ${o}: first 3 vertices collinear, dropped`);continue}let u=[{...r,vertices:s.slice()}];if(s.length>=4){let f=[c[0]/l,c[1]/l,c[2]/l],d=Ha(f,s[0]),m=jf(s),p=Math.max(1e-6,m*.001),h=0;for(let g=3;g<s.length;g++){let y=Math.abs(Ha(f,s[g])-d);y>h&&(h=y)}if(h>p){let g=[],y=r.uvs&&r.uvs.length===s.length?r.uvs:void 0;for(let x=1;x<s.length-1;x++){let b=[s[0].slice(),s[x].slice(),s[x+1].slice()],P=ja(Mo(b[1],b[0]),Mo(b[2],b[0]));if(So(P)<1e-12)continue;let A={...r,vertices:b};y&&(A.uvs=[y[0].slice(),y[x].slice(),y[x+1].slice()]),g.push(A)}if(g.length===0){t.push(`Polygon ${o}: ${s.length} non-coplanar vertices, fan-triangulation produced no valid triangles, dropped`);continue}t.push(`Polygon ${o}: ${s.length} non-coplanar vertices, fan-triangulated to ${g.length} triangles`),u=g}}for(let f of u){let d=Zf(f,o,t);d&&n.push(d)}}return{polygons:n,warnings:t}}function Zf(e,n,t){let o={vertices:e.vertices},r=Kf(e.material),s=Ka(e.textureImageSource),i=Za(e.texturePresentation),a=r?.texture??e.texture??s?.url??r?.imageSource?.url;if(typeof a=="string"&&a===""&&(a=void 0),e.color!==void 0&&a!==void 0&&Hf()&&t.push(`Polygon ${n}: color and texture both set; texture wins`),e.color!==void 0&&(Fn(e.color)?o.color=e.color:(t.push(`Polygon ${n}: invalid color "${e.color}", replaced with ${Wa}`),o.color=Wa)),a!==void 0&&(o.texture=a,e.textureWrap&&(o.textureWrap={...e.textureWrap}),e.textureAlphaMode&&(o.textureAlphaMode=e.textureAlphaMode)),r&&(o.material=r),s&&(o.textureImageSource=s),i&&(o.texturePresentation=i),Array.isArray(e.textureTriangles)&&e.textureTriangles.length>0&&(o.textureTriangles=e.textureTriangles.map(c=>({vertices:c.vertices.map(l=>l.slice()),uvs:c.uvs.map(l=>l.slice())}))),e.doubleSided===!0&&(o.doubleSided=!0),e.uvs!==void 0&&(!Array.isArray(e.uvs)||e.uvs.length!==e.vertices.length?t.push(`Polygon ${n}: uvs length ${Array.isArray(e.uvs)?e.uvs.length:"?"} != vertices length ${e.vertices.length}, uvs stripped`):o.uvs=e.uvs),e.data!==void 0&&e.data!==null&&typeof e.data=="object"){let c={},l=!1;for(let u of Object.keys(e.data)){let f=e.data[u];typeof f=="string"||typeof f=="number"||typeof f=="boolean"?c[u]=f:(l=!0,t.push(`Polygon ${n}: data["${u}"] has non-primitive value, key dropped`))}Object.keys(c).length>0&&(o.data=c)}return o}function Ps(e){if(!e||e.length===0)return{min:[0,0,0],max:[0,0,0]};let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0,a=!1;for(let c of e)if(c?.vertices)for(let l of c.vertices)l&&(a=!0,l[0]<n&&(n=l[0]),l[0]>r&&(r=l[0]),l[1]<t&&(t=l[1]),l[1]>s&&(s=l[1]),l[2]<o&&(o=l[2]),l[2]>i&&(i=l[2]));return a?{min:[n,t,o],max:[r,s,i]}:{min:[0,0,0],max:[0,0,0]}}function qa(e){let n=e.polygons??[],t,o;if(e.skipNormalize)t=n,o=[];else{let a=Ao(n);t=a.polygons,o=a.warnings}let r=Ps(t),s=[r.max[0]-r.min[0],r.max[1]-r.min[1],r.max[2]-r.min[2]];return{context:{polygons:t,sceneBbox:r,warnings:o},dimensions:{sceneBbox:r,size:s},warnings:o}}function Qa(e){return!e.vertices||e.vertices.length<3?[]:[{v:e.vertices.map(n=>[n[0],n[1],n[2]]),color:e.color}]}function qf(e){let n=e[0],t=e[1],o=e[2],r=[t[0]-n[0],t[1]-n[1],t[2]-n[2]],s=[o[0]-n[0],o[1]-n[1],o[2]-n[2]],i=r[1]*s[2]-r[2]*s[1],a=r[2]*s[0]-r[0]*s[2],c=r[0]*s[1]-r[1]*s[0],l=Math.hypot(i,a,c);return l<=1e-9?null:[i/l,a/l,c/l]}function Qf(e){let n=0;for(let t=0;t<e.length;t++){let[o,r]=e[t],[s,i]=e[(t+1)%e.length];n+=o*i-s*r}return Math.abs(n)/2}function Jf(e,n,t){if(!e||e.length<3)return null;let o=e.map(a=>[a[1]*n,a[0]*n,a[2]*t]),r=qf(o);if(!r)return null;let s=1/0,i=0;for(let a=0;a<o.length;a++){let c=o[a],l=o[(a+1)%o.length],u=[l[0]-c[0],l[1]-c[1],l[2]-c[2]],f=u[0]*r[0]+u[1]*r[1]+u[2]*r[2],d=[u[0]-f*r[0],u[1]-f*r[1],u[2]-f*r[2]],m=Math.hypot(d[0],d[1],d[2]);if(m<=1e-9)continue;let p=[d[0]/m,d[1]/m,d[2]/m],h=[r[1]*p[2]-r[2]*p[1],r[2]*p[0]-r[0]*p[2],r[0]*p[1]-r[1]*p[0]],g=Math.hypot(h[0],h[1],h[2]);if(g<=1e-9)continue;let y=[h[0]/g,h[1]/g,h[2]/g],x=o.map(v=>{let I=v[0]-c[0],_=v[1]-c[1],O=v[2]-c[2];return[I*p[0]+_*p[1]+O*p[2],I*y[0]+_*y[1]+O*y[2]]}),b=1/0,P=1/0,A=-1/0,C=-1/0;for(let[v,I]of x)b=Math.min(b,v),P=Math.min(P,I),A=Math.max(A,v),C=Math.max(C,I);let E=A-b,S=C-P,M=Math.max(1,Math.ceil(E))*Math.max(1,Math.ceil(S)),T=Qf(x);T<=1e-9||M<s&&(s=M,i=T)}return!Number.isFinite(s)||i<=1e-9?null:{elementArea:s,polygonArea:i}}function Ja(e,n={}){let t=n.tileSize??100,o=n.layerElevation??t,r=n.texturedOnly??!0,s=0,i=0,a=0,c=0,l=0;for(let f of e){let d=!!f.texture;if(r&&!d)continue;let m=Jf(f.vertices,t,o);m&&(s+=1,d&&(i+=1),a+=m.elementArea,c+=m.polygonArea,l=Math.max(l,Math.max(0,m.elementArea-m.polygonArea)/m.elementArea))}let u=Math.max(0,a-c);return{totalPolygons:e.length,measuredPolygons:s,texturedPolygons:i,elementArea:a,polygonArea:c,transparentArea:u,transparentRatio:a>0?u/a:0,overdrawRatio:c>0?a/c:0,worstTransparentRatio:l}}function Co(e,n,t,o){let r=n*Math.PI/180,s=t*Math.PI/180,i=o*Math.PI/180,[a,c,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[a,c]=[a*u-c*f,a*f+c*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[a,l]=[a*u+l*f,-a*f+l*u]}if(r!==0){let u=Math.cos(r),f=Math.sin(r);[c,l]=[c*u-l*f,c*f+l*u]}return[a,c,l]}function ec(e,n){let t=-n[0]*Math.PI/180,o=-n[1]*Math.PI/180,r=-n[2]*Math.PI/180,[s,i,a]=e;if(t!==0){let c=Math.cos(t),l=Math.sin(t);[i,a]=[i*c-a*l,i*l+a*c]}if(o!==0){let c=Math.cos(o),l=Math.sin(o);[s,a]=[s*c+a*l,-s*l+a*c]}if(r!==0){let c=Math.cos(r),l=Math.sin(r);[s,i]=[s*c-i*l,s*l+i*c]}return[s,i,a]}function On(e,n){let[t,o,r]=e,s=-(n[2]??0)*Math.PI/180;if(s!==0){let c=Math.cos(s),l=Math.sin(s);[t,o]=[t*c-o*l,t*l+o*c]}let i=-(n[1]??0)*Math.PI/180;if(i!==0){let c=Math.cos(i),l=Math.sin(i);[o,r]=[o*c-r*l,o*l+r*c]}let a=-(n[0]??0)*Math.PI/180;if(a!==0){let c=Math.cos(a),l=Math.sin(a);[t,r]=[t*c+r*l,-t*l+r*c]}return[t,o,r]}var Ms=Math.PI/180,Rt=180/Math.PI,nc=[1,0,0,0];function tc(e,n){let[t,o,r,s]=e,[i,a,c,l]=n;return[t*i-o*a-r*c-s*l,t*a+o*i+r*l-s*c,t*c-o*l+r*i+s*a,t*l+o*c-r*a+s*i]}function oc(e,n){let t=n*.5,o=Math.sin(t);return[Math.cos(t),e[0]*o,e[1]*o,e[2]*o]}function rc(e){let n=e[0]*Ms,t=e[1]*Ms,o=e[2]*Ms,r=Math.cos(n*.5),s=Math.sin(n*.5),i=Math.cos(t*.5),a=Math.sin(t*.5),c=Math.cos(o*.5),l=Math.sin(o*.5);return[r*i*c-s*a*l,s*i*c+r*a*l,r*a*c-s*i*l,r*i*l+s*a*c]}function sc(e){let[n,t,o,r]=e,s=2*(t*r+n*o),i=2*(o*r-n*t),a=1-2*(t*t+o*o),c=2*(t*o-n*r),l=1-2*(o*o+r*r),u=Math.max(-1,Math.min(1,s)),f=Math.asin(u);return Math.abs(u)<.99999?[Math.atan2(-i,a)*Rt,f*Rt,Math.atan2(-c,l)*Rt]:[Math.atan2(2*(o*r+n*t),1-2*(t*t+r*r))*Rt,f*Rt,0]}var Ue=50;function cc(e){if(typeof e=="number")return e===0?void 0:e<0?-1:1;if(typeof e=="boolean")return e?-1:1}var Ee={target:[0,0,0],rotX:65,rotY:45,zoom:.65,distance:0};function Ss(e=Ee.target,n=[0,0,0],t=50){let o=e[0]+n[0],r=e[1]+n[1],s=e[2]+n[2];return[r*t,o*t,s*t]}function lc(e=Ee,n={}){let t=n.tileSize??50,o=n.layoutScale??1,r=e.target??Ee.target,[s,i,a]=Ss(r,n.autoCenterOffset,t),c=(e.zoom??Ee.zoom)/t*o,l=(e.distance??Ee.distance)*o,u=e.rotX??Ee.rotX,f=e.rotY??Ee.rotY;return`${l!==0?`translateZ(${-l}px) `:""}scale(${c}) rotateX(${u}deg) rotate(${f}deg) translate3d(${-s}px, ${-i}px, ${-a}px)`}function As(e="none"){if(e==="none")return"1000000px";let n=Number.parseFloat(e);return Number.isFinite(n)?`${n}px`:e}function uc(e,n={}){let t=n.perspectiveStyle??e.perspectiveStyle??"none",o=n.projection??e.type??(t==="none"?"orthographic":"perspective"),r=e.state??Ee;return{projection:o,perspectiveStyle:t,appliedPerspectiveStyle:As(t),state:{target:[...r.target??Ee.target],rotX:r.rotX??Ee.rotX,rotY:r.rotY??Ee.rotY,zoom:r.zoom??Ee.zoom,distance:r.distance??Ee.distance}}}var ic=100,ac=1e4,ct=e=>Math.round(e*ic)/ic,em=e=>Math.round(e*ac)/ac;function fc(e={}){let n={target:e.target??[...Ee.target],rotX:e.rotX??Ee.rotX,rotY:e.rotY??Ee.rotY,zoom:e.zoom??Ee.zoom,distance:e.distance??Ee.distance};function t(r){r.target!==void 0&&(n.target=[ct(r.target[0]),ct(r.target[1]),ct(r.target[2])]),r.rotX!==void 0&&(n.rotX=ct(r.rotX)),r.rotY!==void 0&&(n.rotY=ct(r.rotY)),r.zoom!==void 0&&(n.zoom=em(r.zoom)),r.distance!==void 0&&(n.distance=ct(r.distance))}function o(r={}){let i=(r.cols??0)*50,a=(r.rows??0)*50,[c,l,u]=n.target,f=l*50,d=c*50,m=u*50;return{transform:`${n.distance!==0?` translateZ(${-n.distance}px)`:""}scale(${n.zoom}) rotateX(${n.rotX}deg) rotate(${n.rotY}deg) translate3d(${-f}px, ${-d}px, ${-m}px)`,width:`${i}px`,height:`${a}px`}}return{state:n,update:t,getStyle:o}}var mc=Math.PI/180;function Cs(e){let{camera:n,tileSize:t,viewportCenterX:o,viewportCenterY:r,mouseX:s,mouseY:i}=e,a=n.rotX*mc,c=n.rotY*mc,l=n.zoom/t,u=n.target,f=s-o,d=i-r,m=f/l,p=d/l,h=Math.cos(a),g=Math.sin(a),y=m,x=p*h,b=-p*g,P=0,A=g,C=h,E=Math.cos(c),S=Math.sin(c),M=y*E+x*S,T=-y*S+x*E,v=b,I=P*E+A*S,_=-P*S+A*E,O=C,R=u[1]*t,w=u[0]*t,F=u[2]*t,D=M+R,K=T+w,H=v+F,Q=[K/t,D/t,H/t],ne=[_/t,I/t,O/t];return{origin:Q,direction:ne}}function dc(e){let{origin:n,direction:t}=Cs(e),{sphereCenter:o,sphereRadius:r}=e,s=n[0]-o[0],i=n[1]-o[1],a=n[2]-o[2],c=t[0],l=t[1],u=t[2],f=c*c+l*l+u*u;if(f===0)return null;let d=2*(s*c+i*l+a*u),m=s*s+i*i+a*a-r*r,p=d*d-4*f*m;if(p>=0){let E=Math.sqrt(p),S=(-d-E)/(2*f),M=(-d+E)/(2*f),T=t[2]>=0?Math.max(S,M):Math.min(S,M);return[n[0]+T*t[0],n[1]+T*t[1],n[2]+T*t[2]]}let h=-(s*c+i*l+a*u)/f,g=n[0]+h*c,y=n[1]+h*l,x=n[2]+h*u,b=g-o[0],P=y-o[1],A=x-o[2],C=Math.hypot(b,P,A);return C===0?null:[o[0]+b/C*r,o[1]+P/C*r,o[2]+A/C*r]}var Rn=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],To=(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]],Vt=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],vs=e=>Math.hypot(e[0],e[1],e[2]),lt=(e,n)=>e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2];function nm(e){return`${e[0]},${e[1]},${e[2]}`}function hc(e){if(e.length<3)return null;let n=Rn(e[1],e[0]),t=Rn(e[2],e[0]),o=To(n,t),r=vs(o);if(r<1e-12)return null;let s=[o[0]/r,o[1]/r,o[2]/r],i=Vt(s,e[0]);return{normal:s,d:i}}function tm(e,n){return Vt(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.05}function om(e,n){return Vt(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.001}function rm(e,n,t,o){let r=e.vertices.findIndex(S=>lt(S,t)),s=e.vertices.findIndex(S=>lt(S,o)),i=n.vertices.findIndex(S=>lt(S,t)),a=n.vertices.findIndex(S=>lt(S,o));if(r<0||s<0||i<0||a<0)return null;let c=e.vertices.length,l=n.vertices.length,u=(r+1)%c===s,f=(i+1)%l===a;if(u===f)return null;let d=u?s:r,m=u?r:s,p=f?a:i,h=f?i:a,g=!!(e.uvs&&n.uvs),y=[],x=g?[]:void 0,b=d;for(;y.push(e.vertices[b]),x&&x.push(e.uvs[b]),b!==m;)b=(b+1)%c;for(b=p;y.push(n.vertices[b]),x&&x.push(n.uvs[b]),b!==h;)b=(b+1)%l;let P=[],A=x?[]:void 0;for(let S=0;S<y.length;S++)(P.length===0||!lt(y[S],P[P.length-1]))&&(P.push(y[S]),A&&x&&A.push(x[S]));if(P.length>1&<(P[0],P[P.length-1])&&(P.pop(),A?.pop()),g)return gc(P,A);let C=[],E=A?[]:void 0;for(let S=0;S<P.length;S++){let M=P[(S-1+P.length)%P.length],T=P[S],v=P[(S+1)%P.length],I=To(Rn(T,M),Rn(v,M));vs(I)>1e-9&&(C.push(T),E&&A&&E.push(A[S]))}return C.length<3?null:gc(C,E)}function sm(e,n){let t=e.length,o=0;for(let r=0;r<t;r++){let s=e[r],i=e[(r+1)%t],a=e[(r+2)%t],c=Rn(i,s),l=Rn(a,i),u=Vt(To(c,l),n);if(Math.abs(u)<1e-9)continue;let f=u>0?1:-1;if(o===0)o=f;else if(f!==o)return!1}return!0}function im(e,n){if(e.length<3)return!1;let t=hc(e);if(!t)return!1;for(let o of e)if(Math.abs(Vt(t.normal,o)-t.d)>n)return!1;return!0}function am(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function Ts(e){if(e)return{...e,sourceRect:e.sourceRect?{...e.sourceRect}:void 0}}function Es(e){return e?{...e}:void 0}function pc(e){if(e)return{...e,imageSource:Ts(e.imageSource),presentation:Es(e.presentation)}}function cm(e,n){return(e?.s??"")===(n?.s??"")&&(e?.t??"")===(n?.t??"")}function lm(e,n){return(e??"")===(n??"")}function um(e,n){let t=[];for(let o=1;o<e.length-1;o++)t.push({vertices:[[...e[0]],[...e[o]],[...e[o+1]]],uvs:[[...n[0]],[...n[o]],[...n[o+1]]]});return t}function gc(e,n){for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length];if(!(vs(To(Rn(r,o),Rn(s,o)))<=1e-9))return t===0?{vertices:e,uvs:n}:{vertices:[...e.slice(t),...e.slice(0,t)],uvs:n?[...n.slice(t),...n.slice(0,t)]:void 0}}return{vertices:e,uvs:n}}function tn(e){let n=[],t=[],o=new WeakMap,r=m=>{let p=o.get(m);if(p)return p;let h=nm(m);return o.set(m,h),h},s=(m,p)=>{let h=r(m),g=r(p);return h<g?`${h}|${g}`:`${g}|${h}`},i=(m,p)=>r(m)<r(p)?1:-1,a=m=>{let p=new Map;for(let h=0;h<m.length;h++){let g=m[h],y=m[(h+1)%m.length];p.set(s(g,y),i(g,y))}return p},c=0,l=!1,u=m=>(c+=m,c<=8e6?!0:(l=!0,!1));for(let m of e??[]){if(!m||!m.vertices||m.vertices.length<3){m&&n.push(m);continue}let p=m.vertices,h=hc(p);if(!h){n.push(m);continue}let g=m.texture&&m.uvs&&m.uvs.length===p.length?m.uvs.map(x=>[x[0],x[1]]):void 0,y=m.texture&&g?m.textureTriangles?.length?am(m.textureTriangles):um(p,g):void 0;t.push({vertices:p,uvs:g,color:m.color??"#cccccc",texture:m.texture,textureWrap:m.texture?m.textureWrap:void 0,textureAlphaMode:m.texture?m.textureAlphaMode:void 0,textureImageSource:Ts(m.textureImageSource),texturePresentation:Es(m.texturePresentation),material:pc(m.material),textureTriangles:y,normal:h.normal,d:h.d,edgeDirections:a(p),alive:!0,doubleSided:m.doubleSided===!0,data:m.data})}let f=()=>{let m=new Map;for(let g=0;g<t.length;g++){let y=t[g];if(!y.alive)continue;let x=y.vertices.length;if(!u(x))return!1;for(let b=0;b<x;b++){let P=y.vertices[b],A=y.vertices[(b+1)%x],C=s(P,A),E=m.get(C);E?E.second<0&&(E.second=g):(E={a:P,b:A,first:g,second:-1},m.set(C,E))}}let p=!1,h=(g,y,x)=>{let b=g.edgeDirections.get(s(y,x));return b?b===i(y,x)?1:-1:0};for(let g of m.values()){if(g.second<0)continue;let y=g.first,x=g.second;if(y===x)continue;let b=t[y],P=t[x];if(!b.alive||!P.alive||b.color!==P.color||b.texture!==P.texture||!cm(b.textureWrap,P.textureWrap)||!lm(b.textureAlphaMode,P.textureAlphaMode)||b.doubleSided!==P.doubleSided)continue;let A=!!(b.texture||P.texture);if(A&&(!b.textureTriangles||!P.textureTriangles)||!!b.uvs!=!!P.uvs||(A?!om(b,P):!tm(b,P)))continue;if(!u(b.vertices.length+P.vertices.length))return!1;let C=h(b,g.a,g.b);if(C===0)continue;let E=h(P,g.a,g.b);if(E===0||C===E)continue;let[S,M]=C===1?[g.a,g.b]:[g.b,g.a],T=rm(b,P,S,M);if(T){if(!u(T.vertices.length))return!1;T.vertices.length>96||im(T.vertices,.001)&&sm(T.vertices,b.normal)&&(b.vertices=T.vertices,b.uvs=T.uvs,b.edgeDirections=a(T.vertices),b.textureTriangles=A?[...b.textureTriangles??[],...P.textureTriangles??[]]:void 0,P.alive=!1,p=!0)}}return p},d=0;for(;!l&&d<16&&f();)d++;for(let m of t){if(!m.alive)continue;let p={vertices:m.vertices.map(h=>[h[0],h[1],h[2]]),color:m.color};m.texture&&(p.texture=m.texture),m.texture&&m.textureWrap&&(p.textureWrap={...m.textureWrap}),m.texture&&m.textureAlphaMode&&(p.textureAlphaMode=m.textureAlphaMode),m.textureImageSource&&(p.textureImageSource=Ts(m.textureImageSource)),m.texturePresentation&&(p.texturePresentation=Es(m.texturePresentation)),m.material&&(p.material=pc(m.material)),m.uvs&&(p.uvs=m.uvs.map(h=>[h[0],h[1]])),m.textureTriangles?.length&&(p.textureTriangles=m.textureTriangles),m.doubleSided&&(p.doubleSided=!0),m.data&&(p.data=m.data),n.push(p)}return n}var fm=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],mm=(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]],on=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2];function dm(e,n){let t=e.vertices;if(!t||t.length<3)return null;let o=0,r=0,s=0;for(let h=0;h<t.length;h++){let g=t[h],y=t[(h+1)%t.length];o+=(g[1]-y[1])*(g[2]+y[2]),r+=(g[2]-y[2])*(g[0]+y[0]),s+=(g[0]-y[0])*(g[1]+y[1])}let i=Math.hypot(o,r,s);if(i<1e-12)return null;let a=[o/i,r/i,s/i],c=0,l=0,u=0;for(let h of t)c+=h[0],l+=h[1],u+=h[2];let f=1/t.length,d=[c*f,l*f,u*f],m=on(a,d),p=i*.5;return{index:n,polygon:e,normal:a,d:m,centroid:d,area:p,local2D:null,bbox2D:null,basis:null}}function pm(e,n){return e.polygon.doubleSided===!0&&n.polygon.doubleSided===!0&&on(e.normal,n.normal)<0}function gm(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=n[0]-e[0]*on(n,e),o=n[1]-e[1]*on(n,e),r=n[2]-e[2]*on(n,e),s=Math.hypot(t,o,r);t/=s,o/=s,r/=s;let i=[t,o,r],a=mm(e,i);return{u:i,v:a}}function hm(e){if(e.local2D)return;let n=gm(e.normal),t=[],o=1/0,r=1/0,s=-1/0,i=-1/0;for(let a of e.polygon.vertices){let c=on(a,n.u),l=on(a,n.v);t.push([c,l]),c<o&&(o=c),c>s&&(s=c),l<r&&(r=l),l>i&&(i=l)}e.local2D=t,e.bbox2D={min:[o,r],max:[s,i]},e.basis=n}function ym(e,n){let t=[],o=1/0,r=1/0,s=-1/0,i=-1/0;for(let a of e.vertices){let c=on(a,n.u),l=on(a,n.v);t.push([c,l]),c<o&&(o=c),c>s&&(s=c),l<r&&(r=l),l>i&&(i=l)}return{local2D:t,bbox2D:{min:[o,r],max:[s,i]}}}function xm(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 yc(e,n){let t=!1;for(let o=0,r=n.length-1;o<n.length;r=o++){let s=n[o],i=n[r];s[1]>e[1]!=i[1]>e[1]&&e[0]<(i[0]-s[0])*(e[1]-s[1])/(i[1]-s[1]+1e-30)+s[0]&&(t=!t)}return t}function xc(e){let n=0,t=0;for(let o of e)n+=o[0],t+=o[1];return[n/e.length,t/e.length]}function bm(e,n){let t=xc(e),o=xc(n),r=1e-4,s=0;for(let l of e){let u=[l[0]+(t[0]-l[0])*r,l[1]+(t[1]-l[1])*r];yc(u,n)&&s++}let i=0;for(let l of n){let u=[l[0]+(o[0]-l[0])*r,l[1]+(o[1]-l[1])*r];yc(u,e)&&i++}let a=s/e.length,c=i/n.length;return Math.max(a,c)}function Pm(e,n){let t=e.normal[0],o=e.normal[1],r=e.normal[2],s=Math.abs(t),i=Math.abs(o),a=Math.abs(r),c=t;i>s&&i>a?c=o:a>s&&a>i&&(c=r),c<0&&(t=-t,o=-o,r=-r);let l=Math.round(t/.05),u=Math.round(o/.05),f=Math.round(r/.05),d=e.d*(e.normal[0]===t&&e.normal[1]===o&&e.normal[2]===r?1:-1),m=Math.round(d/(n*2));return`${l},${u},${f}|${m}`}function Mm(e,n,t,o){let r=on(e.normal,n.normal);if(Math.abs(r)<1-t)return!1;let s=r>0?1:-1;return Math.abs(e.d-s*n.d)<o}function bc(e,n){let t=fm(n,e.centroid);return on(e.normal,t)>0}function Lt(e,n){if(!e||e.length<2)return new Set;let t=n?.normalTolerance??.001,o=n?.distanceTolerance??.05,r=n?.overlapFraction??.7,s=n?.preserveDoubleSidedBackfaces??!0,i=[];for(let p=0;p<e.length;p++){let h=dm(e[p],p);h&&i.push(h)}if(i.length<2)return new Set;let a=0,c=0,l=0,u=0;for(let p of i)a+=p.centroid[0]*p.area,c+=p.centroid[1]*p.area,l+=p.centroid[2]*p.area,u+=p.area;let f=u>0?[a/u,c/u,l/u]:[0,0,0],d=new Map;for(let p of i){let h=Pm(p,o),g=d.get(h);g||(g=[],d.set(h,g)),g.push(p)}let m=new Set;for(let p of d.values())if(!(p.length<2))for(let h=0;h<p.length;h++){let g=p[h];if(!m.has(g.index))for(let y=h+1;y<p.length;y++){let x=p[y];if(m.has(x.index)||!Mm(g,x,t,o)||s&&pm(g,x))continue;hm(g);let b=ym(x.polygon,g.basis);if(!xm(g.bbox2D,b.bbox2D)||bm(g.local2D,b.local2D)<r)continue;let A=bc(g,f),C=bc(x,f),E;if(A&&!C?E=g:C&&!A?E=x:E=g.area<x.area?g:x,m.add(E.index),E===g)break}}return m}function Pc(e,n){if(!e||e.length<2)return e??[];let t=Lt(e,n);if(t.size===0)return e;let o=[];for(let r=0;r<e.length;r++)t.has(r)||o.push(e[r]);return o}var Sm=4,Am=8,Cm=.001,Tm=1e-9,Em=1e6,vm=1e6,Im=8,Bn=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Os=(e,n)=>[e[0]+n[0],e[1]+n[1],e[2]+n[2]],Dt=(e,n)=>[e[0]*n,e[1]*n,e[2]*n],kn=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],Cc=(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]],Tc=e=>Math.hypot(e[0],e[1],e[2]);function Ft(e){let n=Tc(e);return n<=Tm?null:[e[0]/n,e[1]/n,e[2]/n]}function Nt(e,n){return Math.round(e*n)/n}function rn(e){return Nt(e,Em)}function Is(e){return Nt(e,vm)}function _s(e,n){return Math.abs(e[0]-n[0])<=1e-7&&Math.abs(e[1]-n[1])<=1e-7}function Mn(e){return`${rn(e[0])},${rn(e[1])}`}function _m(e){let n=Mn(e.a),t=Mn(e.b);return n<t?`${n}|${t}`:`${t}|${n}`}function ut(e){return`${e[0]},${e[1]},${e[2]}`}function Ec(e,n){let t=ut(e),o=ut(n);return t<o?`${t}|${o}`:`${o}|${t}`}function Rs(e){return e?Object.keys(e).sort().map(n=>`${n}:${String(e[n])}`).join("|"):""}function Mc(e){return[e.color??"#cccccc",e.texture??"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided",Rs(e.data)].join("|")}function vc(e){let n=e.vertices;if(!n||n.length<3)return null;let t=n[0],o=[0,0,0];for(let s=1;s<n.length-1;s++)o=Os(o,Cc(Bn(n[s],t),Bn(n[s+1],t)));let r=Ft(o);return r?{normal:r,d:kn(r,t)}:null}function Om(e,n,t){return kn(e.normal,n.normal)>1-t&&Math.abs(e.d-n.d)<=t}function Rm(e){return!e.texture&&!e.uvs&&!e.textureTriangles}function Vm(e,n){let t=n[e[0]]?.data,o=Rs(t);for(let r of e)if(Rs(n[r].data)!==o)return;return t?{...t}:void 0}function Lm(e,n){return e.every(t=>n[t].doubleSided===!0)}function wm(e,n){let t=e.map(c=>Rm(c)?vc(c):null),o=t.map(Boolean),r=new Map;for(let c=0;c<e.length;c++){if(!t[c])continue;let l=e[c].vertices;for(let u=0;u<l.length;u++){let f=Ec(l[u],l[(u+1)%l.length]),d=r.get(f),m={polygon:c,edge:u};d?d.push(m):r.set(f,[m])}}let s=e.map(()=>new Set);for(let c of r.values())if(!(c.length<2))for(let l=0;l<c.length;l++)for(let u=l+1;u<c.length;u++){let f=c[l].polygon,d=c[u].polygon,m=t[f],p=t[d];!m||!p||Mc(e[f])===Mc(e[d])&&Om(m,p,n)&&(s[f].add(d),s[d].add(f))}let i=new Set,a=[];for(let c=0;c<e.length;c++){if(!o[c]||i.has(c))continue;let l=[],u=[c];for(i.add(c);u.length>0;){let f=u.shift();l.push(f);for(let d of s[f])i.has(d)||(i.add(d),u.push(d))}a.push(l)}return{groups:a}}function Nm(e,n){let t=new Set(e),o=new Map;for(let s of e){let i=n[s].vertices;for(let a=0;a<i.length;a++){let c=Ec(i[a],i[(a+1)%i.length]),l=o.get(c),u={polygon:s,edge:a};l?l.push(u):o.set(c,[u])}}let r=[];for(let s of o.values()){let i=s.filter(a=>t.has(a.polygon));if(i.length===1){let a=i[0],c=n[a.polygon].vertices;r.push({a:c[a.edge],b:c[(a.edge+1)%c.length]})}else if(i.length!==2)return null}return r}function Dm(e){let n=new Map;for(let t of e)n.set(ut(t.a),(n.get(ut(t.a))??0)+1),n.set(ut(t.b),(n.get(ut(t.b))??0)+1);for(let t of n.values())if(t%2!==0)return!1;return!0}function Ic(e){let n=[e[0],e[1],e[2]],t=Math.abs(n[0])>=Math.abs(n[1])&&Math.abs(n[0])>=Math.abs(n[2])?0:Math.abs(n[1])>=Math.abs(n[2])?1:2;return n[t]<0?[-n[0],-n[1],-n[2]]:n}function Fm(e){let n=Ic(e);return`${Nt(n[0],1e3)},${Nt(n[1],1e3)},${Nt(n[2],1e3)}`}function Bm(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],t=Bn(n,Dt(e,kn(n,e)));return Ft(t)??[1,0,0]}function km(e,n,t){let o=new Map;for(let s of e){let i=Bn(s.b,s.a),a=Bn(i,Dt(n,kn(i,n))),c=Ft(a);if(!c)continue;let l=Ic(c),u=Fm(l),f=Tc(a),d=o.get(u);d?d.weight+=f:o.set(u,{axis:l,weight:f})}let r=[...o.values()].sort((s,i)=>i.weight-s.weight).slice(0,t).map(s=>s.axis);return r.length===0&&r.push(Bm(n)),r}function Sc(e,n,t,o){let r=Bn(e,n);return[rn(kn(r,t)),rn(kn(r,o))]}function Um(e){let n=[...e].sort((o,r)=>o-r),t=[];for(let o of n)(t.length===0||Math.abs(o-t[t.length-1])>1e-7)&&t.push(o);return t}function wt(e,n){let[t,o]=e.a,[r,s]=e.b,i=(n-t)/(r-t);return rn(o+(s-o)*i)}function Vs(e){let n=[];for(let o of e)(n.length===0||Math.abs(o[0]-n[n.length-1][0])>1e-7||Math.abs(o[1]-n[n.length-1][1])>1e-7)&&n.push(o);n.length>1&&Math.abs(n[0][0]-n[n.length-1][0])<=1e-7&&Math.abs(n[0][1]-n[n.length-1][1])<=1e-7&&n.pop();let t=[];for(let o=0;o<n.length;o++){let r=n[(o-1+n.length)%n.length],s=n[o],i=n[(o+1)%n.length],a=(s[0]-r[0])*(i[1]-s[1])-(s[1]-r[1])*(i[0]-s[0]);Math.abs(a)>1e-8&&t.push(s)}return t}function vo(e){let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-r[0]*o[1]}return n/2}function Io(e){let n=1/0,t=1/0,o=-1/0,r=-1/0;for(let[s,i]of e)n=Math.min(n,s),t=Math.min(t,i),o=Math.max(o,s),r=Math.max(r,i);return{minX:n,minY:t,maxX:o,maxY:r}}function _c(e,n){return!(e.maxX<n.minX-1e-7||n.maxX<e.minX-1e-7||e.maxY<n.minY-1e-7||n.maxY<e.minY-1e-7)}function Eo(e){return Math.abs(vo(e))}function zm(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=1e-8)continue;let a=i>0?1:-1;if(n===0)n=a;else if(n!==a)return!1}return!0}function Gm(e,n,t){let o=t[0]-n[0],r=t[1]-n[1],s=e[0]-n[0],i=e[1]-n[1],a=o*i-r*s,c=Math.hypot(o,r);if(c<=1e-9||Math.abs(a)>Math.max(1e-8,c*1e-8))return!1;let l=s*o+i*r;return l>=-1e-8&&l<=o*o+r*r+1e-8}function $m(e,n,t){let o=t[0]-n[0],r=t[1]-n[1],s=o*o+r*r;return s<=1e-12?0:((e[0]-n[0])*o+(e[1]-n[1])*r)/s}function Xm(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],a=o[1]-t[1],c=Math.hypot(r,s),l=Math.hypot(i,a);if(c<=1e-9||l<=1e-9)return!1;let u=r*(t[1]-e[1])-s*(t[0]-e[0]),f=r*(o[1]-e[1])-s*(o[0]-e[0]),d=r*a-s*i,m=Math.max(1e-8,Math.max(c,l)*1e-8);if(Math.abs(u)>m||Math.abs(f)>m||Math.abs(d)>m)return!1;let p=Math.abs(r)>=Math.abs(s),h=Math.min(p?e[0]:e[1],p?n[0]:n[1]),g=Math.max(p?e[0]:e[1],p?n[0]:n[1]),y=Math.min(p?t[0]:t[1],p?o[0]:o[1]),x=Math.max(p?t[0]:t[1],p?o[0]:o[1]);return Math.min(g,x)-Math.max(h,y)>1e-8}function Ym(e,n){for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];for(let s=0;s<n.length;s++)if(Xm(o,r,n[s],n[(s+1)%n.length]))return!0}return!1}function Ac(e,n){let t=[];for(let o=0;o<e.length;o++){let r=e[o],s=e[(o+1)%e.length],i=[{t:0,point:r},{t:1,point:s}];for(let c of n)_s(c,r)||_s(c,s)||Gm(c,r,s)&&i.push({t:$m(c,r,s),point:c});i.sort((c,l)=>c.t-l.t);let a=[];for(let c of i)a.some(l=>Math.abs(l.t-c.t)<=1e-8||_s(l.point,c.point))||a.push(c);for(let c=0;c<a.length-1;c++){let l=a[c].point,u=a[c+1].point;Math.hypot(u[0]-l[0],u[1]-l[1])<=1e-8||t.push({a:[rn(l[0]),rn(l[1])],b:[rn(u[0]),rn(u[1])]})}}return t}function Wm(e,n,t=Io(e),o=Io(n)){if(!_c(t,o)||!Ym(e,n))return null;let r=[...Ac(e,n),...Ac(n,e)],s=new Map;for(let y of r){let x=_m(y),b=s.get(x);b?b.push(y):s.set(x,[y])}let i=!1,a=[];for(let y of s.values()){if(y.length===1){a.push(y[0]);continue}i=!0;let x=y.filter(P=>Mn(P.a)<Mn(P.b)).length,b=y.length-x;if(x!==b)return null}if(!i||a.length<3)return null;let c=new Map;for(let y of a){let x=Mn(y.a);if(c.has(x))return null;c.set(x,y)}let l=a[0],u=Mn(l.a),f=[],d=new Set,m=u;for(let y=0;y<=a.length;y++){let x=c.get(m);if(!x)return null;let b=`${Mn(x.a)}>${Mn(x.b)}`;if(d.has(b))return null;if(d.add(b),f.push(x.a),m=Mn(x.b),m===u)break}if(m!==u||d.size!==a.length)return null;let p=Vs(f);if(p.length<3||!zm(p))return null;let h=Eo(p),g=Eo(e)+Eo(n);return Math.abs(h-g)>Math.max(1e-5,g*1e-5)?null:vo(p)>=0?p:[...p].reverse()}function jm(e){let n=e.map(Vs).filter(r=>r.length>=3&&Eo(r)>1e-8),t=n.map(Io),o=!0;for(;o;){o=!1;for(let r=0;r<n.length;r++){for(let s=r+1;s<n.length;s++){if(!_c(t[r],t[s]))continue;let i=Wm(n[r],n[s],t[r],t[s]);if(i){n[r]=i,t[r]=Io(i),n.splice(s,1),t.splice(s,1),o=!0;break}}if(o)break}}return n}function Hm(e,n,t){let o=new Map;return r=>{let s=rn(r[0]),i=rn(r[1]),a=`${s},${i}`,c=o.get(a);if(c)return[c[0],c[1],c[2]];let l=Os(e,Os(Dt(n,s),Dt(t,i))),u=[Is(l[0]),Is(l[1]),Is(l[2])];return o.set(a,u),[u[0],u[1],u[2]]}}function Km(e,n,t,o,r){let s=t[0]?.a;if(!s)return null;let i=Ft(Bn(r,Dt(o,kn(r,o))));if(!i)return null;let a=Ft(Cc(o,i));if(!a)return null;let c=[],l=[];for(let y of t){let x=Sc(y.a,s,i,a),b=Sc(y.b,s,i,a);Math.hypot(x[0]-b[0],x[1]-b[1])<=1e-7||(c.push({a:x,b}),l.push(x[0],b[0]))}let u=Um(l);if(c.length<3||u.length<2)return null;let f=n[e[0]].color,d=Vm(e,n),m=Lm(e,n),p=[];for(let y=0;y<u.length-1;y++){let x=u[y],b=u[y+1];if(b-x<=1e-7)continue;let P=(x+b)/2,A=c.filter(C=>{let E=Math.min(C.a[0],C.b[0]),S=Math.max(C.a[0],C.b[0]);return E<P&&P<S&&Math.abs(C.a[0]-C.b[0])>1e-7}).map(C=>({segment:C,yMid:wt(C,P)})).sort((C,E)=>C.yMid-E.yMid);if(A.length!==0){if(A.length%2!==0)return null;for(let C=0;C<A.length;C+=2){let E=A[C].segment,S=A[C+1].segment,M=wt(E,x),T=wt(E,b),v=wt(S,x),I=wt(S,b),_=Vs([[x,M],[b,T],[b,I],[x,v]]);if(_.length<3||Math.abs(vo(_))<=1e-8)continue;let O=vo(_)>0?_:[..._].reverse();p.push(O)}}}if(p.length===0||e.length>2&&p.length>e.length*Im)return null;let h=Hm(s,i,a),g=jm(p).map(y=>({vertices:y.map(h),...f?{color:f}:{},...m?{doubleSided:!0}:{},...d?{data:d}:{}}));return tn(g)}function Zm(e,n,t){let o=Nm(e,n);if(!o||o.length<3||!Dm(o))return null;let r=vc(n[e[0]]);if(!r)return null;let s=null;for(let i of km(o,r.normal,t)){let a=Km(e,n,o,r.normal,i);a&&(!s||a.length<s.length)&&(s=a)}return!s||s.length>=e.length?null:s}function _o(e,n={}){let t=n.minGroupPolygons??Sm,o=n.maxCandidateAxes??Am,r=n.planeEpsilon??Cm,s=e??[];if(s.length<t)return s;let{groups:i}=wm(s,r);if(i.length===0)return s;let a=new Map,c=new Set;for(let u of i){if(u.length<t)continue;let f=Zm(u,s,o);if(f){a.set(u[0],f);for(let d of u)c.add(d)}}if(a.size===0)return s;let l=[];for(let u=0;u<s.length;u++){let f=a.get(u);if(f){l.push(...f);continue}c.has(u)||l.push(s[u])}return l}function qm(e,n){let t=`${e[0]},${e[1]},${e[2]}`,o=`${n[0]},${n[1]},${n[2]}`;return t<o?`${t}|${o}`:`${o}|${t}`}function Qm(e){if(e.length<100)return!1;let n=new Map;for(let o of e){let r=o.vertices;for(let s=0;s<r.length;s++){let i=qm(r[s],r[(s+1)%r.length]);n.set(i,(n.get(i)??0)+1)}}let t=0;for(let o of n.values())o===1&&(t+=1);return t>=128&&t/Math.max(1,n.size)>.2}function Jm(e,n,t){let o=Math.abs(e)>.9?0:1,r=Math.abs(e)>.9?1:0,s=r*t,i=-o*t,a=o*n-r*e,c=Math.hypot(s,i,a);return s/=c,i/=c,a/=c,{ux:s,uy:i,uz:a,vx:n*a-t*i,vy:t*s-e*a,vz:e*i-n*s}}function ed(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0;for(let[K,H,Q]of n)t+=K,o+=H,r+=Q;let s=1/n.length;t*=s,o*=s,r*=s;let i=n[0],a=n[1],c=n[2],l=a[0]-i[0],u=a[1]-i[1],f=a[2]-i[2],d=c[0]-i[0],m=c[1]-i[1],p=c[2]-i[2],h=u*p-f*m,g=f*d-l*p,y=l*m-u*d,x=Math.hypot(h,g,y);if(x<1e-9)return null;h/=x,g/=x,y/=x;let{ux:b,uy:P,uz:A,vx:C,vy:E,vz:S}=Jm(h,g,y),M=n.length-2,T=new Float64Array(M*9),v=0;for(let K=1;K<n.length-1;K++){let H=n[0],Q=n[K],ne=n[K+1];T[v++]=H[0],T[v++]=H[1],T[v++]=H[2],T[v++]=Q[0],T[v++]=Q[1],T[v++]=Q[2],T[v++]=ne[0],T[v++]=ne[1],T[v++]=ne[2]}let I=0,_=1/0,O=1/0,R=1/0,w=-1/0,F=-1/0,D=-1/0;for(let[K,H,Q]of n){let ne=K-t,ee=H-o,ue=Q-r,U=ne*ne+ee*ee+ue*ue;U>I&&(I=U),K<_&&(_=K),K>w&&(w=K),H<O&&(O=H),H>F&&(F=H),Q<R&&(R=Q),Q>D&&(D=Q)}return{centroid:[t,o,r],normal:[h,g,y],vertices:n,triFlat:T,bcx:t,bcy:o,bcz:r,br2:I,ux:b,uy:P,uz:A,vx:C,vy:E,vz:S,minX:_,minY:O,minZ:R,maxX:w,maxY:F,maxZ:D}}function nd(e,n,t,o,r,s,i,a){let c=i[a],l=i[a+1],u=i[a+2],f=i[a+3]-c,d=i[a+4]-l,m=i[a+5]-u,p=i[a+6]-c,h=i[a+7]-l,g=i[a+8]-u,y=r*g-s*h,x=s*p-o*g,b=o*h-r*p,P=f*y+d*x+m*b;if(P>-1e-9&&P<1e-9)return!1;let A=1/P,C=e-c,E=n-l,S=t-u,M=A*(C*y+E*x+S*b);if(M<0||M>1)return!1;let T=E*m-S*d,v=S*f-C*m,I=C*d-E*f,_=A*(o*T+r*v+s*I);return _<0||M+_>1?!1:A*(p*T+h*v+g*I)>.001}function td(e,n,t,o,r,s,i){let a=i.bcx-e,c=i.bcy-n,l=i.bcz-t,u=a*o+c*r+l*s,f=a-u*o,d=c-u*r,m=l-u*s;if(f*f+d*d+m*m>i.br2)return!1;let p=i.triFlat,h=p.length;for(let g=0;g<h;g+=9)if(nd(e,n,t,o,r,s,p,g))return!0;return!1}var Bt=9,od=6,ze=8;function Ls(e,n,t,o,r,s){let i=o-e,a=r-n,c=s-t;return i*a+a*c+c*i}function rd(e){let n=[];for(let C=0;C<e.length;C++)e[C]&&n.push(C);let t=n.length,o=new Int32Array(t);for(let C=0;C<t;C++)o[C]=n[C];let r=new Float64Array(t),s=new Float64Array(t),i=new Float64Array(t);for(let C=0;C<t;C++){let E=e[o[C]];r[C]=(E.minX+E.maxX)*.5,s[C]=(E.minY+E.maxY)*.5,i[C]=(E.minZ+E.maxZ)*.5}let a=2*Math.max(1,t)+1,c=new Float64Array(a*Bt),l=0,u=new Float64Array(ze),f=new Float64Array(ze),d=new Float64Array(ze),m=new Float64Array(ze),p=new Float64Array(ze),h=new Float64Array(ze),g=new Int32Array(ze),y=new Float64Array(ze-1),x=new Int32Array(ze-1),b=new Float64Array(ze-1),P=new Int32Array(ze-1);function A(C,E){let S=l++,M=S*Bt,T=E-C,v=1/0,I=1/0,_=1/0,O=-1/0,R=-1/0,w=-1/0;for(let z=C;z<E;z++){let te=e[o[z]];te.minX<v&&(v=te.minX),te.maxX>O&&(O=te.maxX),te.minY<I&&(I=te.minY),te.maxY>R&&(R=te.maxY),te.minZ<_&&(_=te.minZ),te.maxZ>w&&(w=te.maxZ)}if(c[M]=v,c[M+1]=I,c[M+2]=_,c[M+3]=O,c[M+4]=R,c[M+5]=w,T<=od)return c[M+6]=1,c[M+7]=C,c[M+8]=E,S;let F=1/0,D=1/0,K=1/0,H=-1/0,Q=-1/0,ne=-1/0;for(let z=C;z<E;z++)r[z]<F&&(F=r[z]),r[z]>H&&(H=r[z]),s[z]<D&&(D=s[z]),s[z]>Q&&(Q=s[z]),i[z]<K&&(K=i[z]),i[z]>ne&&(ne=i[z]);let ee=H-F,ue=Q-D,U=ne-K;if(ee===0&&ue===0&&U===0)return c[M+6]=1,c[M+7]=C,c[M+8]=E,S;let $=Ls(v,I,_,O,R,w),k=$>0?1/$:0,L=T+1,W=0,Y=0;for(let z=0;z<3;z++){let te=z===0?F:z===1?D:K,ce=z===0?ee:z===1?ue:U;if(ce===0)continue;let ye=z===0?r:z===1?s:i,he=ze/ce;u.fill(1/0),f.fill(1/0),d.fill(1/0),m.fill(-1/0),p.fill(-1/0),h.fill(-1/0),g.fill(0);for(let N=C;N<E;N++){let X=(ye[N]-te)*he|0;X>=ze&&(X=ze-1);let oe=e[o[N]];oe.minX<u[X]&&(u[X]=oe.minX),oe.maxX>m[X]&&(m[X]=oe.maxX),oe.minY<f[X]&&(f[X]=oe.minY),oe.maxY>p[X]&&(p[X]=oe.maxY),oe.minZ<d[X]&&(d[X]=oe.minZ),oe.maxZ>h[X]&&(h[X]=oe.maxZ),g[X]++}let xe=1/0,ge=1/0,Ae=1/0,Fe=-1/0,V=-1/0,G=-1/0,j=0;for(let N=0;N<ze-1;N++)u[N]<xe&&(xe=u[N]),m[N]>Fe&&(Fe=m[N]),f[N]<ge&&(ge=f[N]),p[N]>V&&(V=p[N]),d[N]<Ae&&(Ae=d[N]),h[N]>G&&(G=h[N]),j+=g[N],y[N]=Ls(xe,ge,Ae,Fe,V,G),x[N]=j;let J=1/0,le=1/0,re=1/0,pe=-1/0,be=-1/0,ie=-1/0,ae=0;for(let N=ze-2;N>=0;N--){let X=N+1;u[X]<J&&(J=u[X]),m[X]>pe&&(pe=m[X]),f[X]<le&&(le=f[X]),p[X]>be&&(be=p[X]),d[X]<re&&(re=d[X]),h[X]>ie&&(ie=h[X]),ae+=g[X],b[N]=Ls(J,le,re,pe,be,ie),P[N]=ae}for(let N=0;N<ze-1;N++){if(x[N]===0||P[N]===0)continue;let X=.125+(y[N]*x[N]+b[N]*P[N])*k;X<L&&(L=X,W=z,Y=te+(N+1)/he)}}let se=W===0?r:W===1?s:i,Z=C,B=E-1;for(;Z<=B;)if(se[Z]<Y)Z++;else{let z=o[Z];o[Z]=o[B],o[B]=z;let te=r[Z];r[Z]=r[B],r[B]=te;let ce=s[Z];s[Z]=s[B],s[B]=ce;let ye=i[Z];i[Z]=i[B],i[B]=ye,B--}let q=Z;(q===C||q===E)&&(q=C+E>>1),c[M+6]=0;let me=A(C,q),fe=A(q,E);return c[S*Bt+7]=me,c[S*Bt+8]=fe,S}return t>0&&A(0,t),{data:c,nodeCount:l,polyIndices:o,meta:e}}function ws(e,n,t,o,r,s,i,a,c){if(a.nodeCount===0)return!1;let{data:l,polyIndices:u,meta:f}=a,d=o!==0?1/o:o>=0?1/0:-1/0,m=r!==0?1/r:r>=0?1/0:-1/0,p=s!==0?1/s:s>=0?1/0:-1/0,h=0;for(c[h++]=0;h>0;){let y=c[--h]*Bt,x=(l[y]-e)*d,b=(l[y+3]-e)*d,P=x<b?x:b,A=x<b?b:x,C=(l[y+1]-n)*m,E=(l[y+4]-n)*m,S=C<E?C:E,M=C<E?E:C;if(P>M||S>A)continue;S>P&&(P=S),M<A&&(A=M);let T=(l[y+2]-t)*p,v=(l[y+5]-t)*p,I=T<v?T:v,_=T<v?v:T;if(!(P>_||I>A)&&(_<A&&(A=_),!(A<.001)))if(l[y+6]===1){let O=l[y+7]|0,R=l[y+8]|0;for(let w=O;w<R;w++){let F=u[w];if(F===i)continue;let D=f[F];if(D&&td(e,n,t,o,r,s,D))return!0}}else c[h++]=l[y+7]|0,c[h++]=l[y+8]|0}return!1}function sd(e){let n=Oc.get(e);if(n)return n;let t=(1+Math.sqrt(5))/2,o=new Float64Array(e*3);for(let r=0;r<e;r++){let s=(r+.5)/e,i=Math.sqrt(Math.max(0,1-s*s)),a=2*Math.PI*(r/t);o[r*3]=i*Math.cos(a),o[r*3+1]=i*Math.sin(a),o[r*3+2]=s}return Oc.set(e,o),o}var Oc=new Map;function fn(e,n){let t=n?.samples??8;if(e.length<4||t<1||!n?.force&&Qm(e))return e;let o=Math.max(1,Math.ceil(t*(n?.minEscapeRatio??1/t))),r=e.map(ed),s=sd(t),i=[],a=rd(r),c=new Int32Array(Math.max(64,a.nodeCount)),l=192,u=new Float64Array(l);for(let f=0;f<e.length;f++){let d=r[f];if(!d){i.push(e[f]);continue}let m=d.normal[0],p=d.normal[1],h=d.normal[2],g=.001*m,y=.001*p,x=.001*h;{let R=d.centroid[0]+g,w=d.centroid[1]+y,F=d.centroid[2]+x;if(!ws(R,w,F,m,p,h,f,a,c)){i.push(e[f]);continue}}let{ux:b,uy:P,uz:A,vx:C,vy:E,vz:S}=d,M=d.centroid[0],T=d.centroid[1],v=d.centroid[2],I=d.vertices,_=I.length,O=0;u[O++]=M+g,u[O++]=T+y,u[O++]=v+x;for(let R=0;R<_;R++){let w=I[R];u[O++]=w[0]+(M-w[0])*.08+g,u[O++]=w[1]+(T-w[1])*.08+y,u[O++]=w[2]+(v-w[2])*.08+x}for(let R=0;R<_;R++){let w=I[R],F=I[(R+1)%_],D=(w[0]+F[0])*.5,K=(w[1]+F[1])*.5,H=(w[2]+F[2])*.5;u[O++]=D+(M-D)*.08+g,u[O++]=K+(T-K)*.08+y,u[O++]=H+(v-H)*.08+x}if(o<=1){let R=!1;e:for(let w=0;w<s.length;w+=3){let F=s[w],D=s[w+1],K=s[w+2],H=F*b+D*C+K*m,Q=F*P+D*E+K*p,ne=F*A+D*S+K*h;for(let ee=0;ee<O;ee+=3){let ue=u[ee],U=u[ee+1],$=u[ee+2];if(!ws(ue,U,$,H,Q,ne,f,a,c)){R=!0;break e}}}R&&i.push(e[f])}else{let R=0;for(let w=0;w<s.length;w+=3){let F=s[w],D=s[w+1],K=s[w+2],H=F*b+D*C+K*m,Q=F*P+D*E+K*p,ne=F*A+D*S+K*h;for(let ee=0;ee<O;ee+=3){let ue=u[ee],U=u[ee+1],$=u[ee+2];if(!ws(ue,U,$,H,Q,ne,f,a,c)){R++;break}}}R>=o&&i.push(e[f])}}return i}var ks={overlapPx:1.25,maxGapPx:14,capacityScale:1},Us={budget:40};function Un(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function ft(e){return typeof e=="number"?{overlapPx:Math.max(0,e)*.175,maxGapPx:14,capacityScale:1}:{overlapPx:Math.max(0,Un(e?.overlapPx,1.25)),maxGapPx:Math.max(0,Un(e?.maxGapPx,14)),capacityScale:Math.max(0,Un(e?.capacityScale,1))}}function mt(e,n){return!(typeof n=="number"&&n<=0)&&e.maxGapPx>1e-6&&e.capacityScale>1e-6}function Fc(e){return{rotX:Un(e?.rotX,65),rotY:Un(e?.rotY,45),viewAware:e?.viewAware!==!1,passes:Math.max(1,Math.min(3,Math.round(Un(e?.passes,2)))),budget:Math.max(0,Math.min(256,Math.round(Un(e?.budget,40))))}}function Sn(e){return e.map(n=>[n[1]*50,n[0]*50,n[2]*50])}function Bc(e){return[e[1]/50,e[0]/50,e[2]/50]}function No(e){return`${e[0]},${e[1]},${e[2]}`}function id(e,n){let t=No(e),o=No(n);return t<o?`${t}|${o}`:`${o}|${t}`}function An(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function dt(e){return Math.hypot(e[0],e[1],e[2])}function Gt(e){let n=dt(e);return n>1e-6?[e[0]/n,e[1]/n,e[2]/n]:null}function zt(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Oo(e,n,t,o){let r=n*Math.PI/180,s=t*Math.PI/180,i=o*Math.PI/180,[a,c,l]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[a,c]=[a*u-c*f,a*f+c*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[a,l]=[a*u+l*f,-a*f+l*u]}if(r!==0){let u=Math.cos(r),f=Math.sin(r);[c,l]=[c*u-l*f,c*f+l*u]}return[a,c,l]}function zs(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 ad(e){if(e.length<3)return null;let n=e[0],t=[0,0,0];for(let o=1;o+1<e.length;o+=1){let r=An(e[o],n),s=An(e[o+1],n),i=zs(r,s);t[0]-=i[0],t[1]-=i[1],t[2]-=i[2]}return Gt(t)}function Gs(e){let n=e[0],t=ad(e);if(!t)return null;let o=null;for(let u=0;u<e.length;u+=1){let f=An(e[(u+1)%e.length],e[u]);if(dt(f)>1e-6){o=f;break}}if(!o)return null;let r=zt(o,t),s=[o[0]-r*t[0],o[1]-r*t[1],o[2]-r*t[2]],i=Gt(s);if(!i)return null;let a=Gt(zs(t,i));if(!a)return null;let c=e.map(u=>{let f=An(u,n);return[zt(f,i),zt(f,a)]}),l=Do(c);return Math.abs(l)>1e-6?{origin:n,normal:t,xAxis:i,yAxis:a,local:c,area:l}:null}function Do(e){let n=0;for(let t=0;t<e.length;t+=1){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-o[1]*r[0]}return n/2}function $s(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t+=1){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=1e-6)continue;let a=Math.sign(i);if(n===0)n=a;else if(a!==n)return!1}return!0}function kc(e,n,t){let o=e[t],r=e[(t+1)%e.length],s=r[0]-o[0],i=r[1]-o[1],a=Math.hypot(s,i);if(a<=1e-6)return null;let c=n>0?1:-1;return[c*(i/a),c*(-s/a)]}function cd(e,n){let t=kc(e.local,e.area,n);return t?Gt([e.xAxis[0]*t[0]+e.yAxis[0]*t[1],e.xAxis[1]*t[0]+e.yAxis[1]*t[1],e.xAxis[2]*t[0]+e.yAxis[2]*t[1]]):null}function Uc(e,n){let t=e.length,o=e[n],r=e[(n+1)%t],s=r[0]-o[0],i=r[1]-o[1],a=Math.hypot(s,i);if(a<=1e-6)return 0;let c=[a*.24],l=1/0;for(let f=0;f<t;f+=1){if(f===n||f===(n+1)%t)continue;let d=e[f],m=Math.abs((d[0]-o[0])*i-(d[1]-o[1])*s)/a;m>1e-6&&(l=Math.min(l,m))}Number.isFinite(l)&&c.push(l*.32);let u=[{origin:o,point:e[(n+t-1)%t]},{origin:r,point:e[(n+2)%t]}];for(let f of u){let d=f.point[0]-f.origin[0],m=f.point[1]-f.origin[1],p=Math.hypot(d,m);if(p<=1e-6)continue;let h=Math.abs(s*m-i*d)/(a*p);h>1e-6&&c.push(p*h*.32)}return Math.min(...c)}function zc(e){return e.map(n=>{let t=Sn(n.vertices),o=Gs(t),r=!Xs(n)&&!!o&&$s(o.local),s=r?o.local.map((i,a)=>Uc(o.local,a)):[];return{basis:o,cssPoints:t,capacities:s,patchable:r}})}function Xs(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function ld(e){return e.material?.key??e.color??""}function Gc(e,n,t){let o=[];for(let r=0;r<n.length;r+=1){let s=n[r];if(!(!s.patchable||!s.basis))for(let i=0;i<s.cssPoints.length;i+=1){let a=s.cssPoints[i],c=s.cssPoints[(i+1)%s.cssPoints.length],l=An(c,a),u=dt(l),f=Gt(l),d=cd(s.basis,i),m=(s.capacities[i]??0)*t;!f||!d||m<=1e-6||u<=1e-6||o.push({index:o.length,polygon:r,edge:i,key:id(a,c),a,b:c,minX:Math.min(a[0],c[0]),minY:Math.min(a[1],c[1]),minZ:Math.min(a[2],c[2]),maxX:Math.max(a[0],c[0]),maxY:Math.max(a[1],c[1]),maxZ:Math.max(a[2],c[2]),length:u,dir:f,normal:s.basis.normal,outward:d,capacity:m,materialKey:ld(e[r]),color:e[r].color})}}return o}function Ys(e,n){return e.materialKey===n.materialKey&&e.color===n.color}function Ro(e,n){return dt(An(e,n))<=1e-4}function ud(e,n){return Ro(e.a,n.a)||Ro(e.a,n.b)||Ro(e.b,n.a)||Ro(e.b,n.b)}function fd(e,n){return Ys(e,n)?ud(e,n)?"connected-facet":"true-gap":"material-boundary"}function Ns(e,n,t,o,r,s){return{kind:e,aPolygon:n.polygon,aEdge:n.edge,bPolygon:t.polygon,bEdge:t.edge,aColor:n.color,bColor:t.color,aMaterialKey:n.materialKey,bMaterialKey:t.materialKey,gapPx:o.gap,spanPx:o.aEnd-o.aStart,aStartPx:o.aStart,aEndPx:o.aEnd,bStartPx:o.bStart,bEndPx:o.bEnd,targetClosurePx:r,appliedClosurePx:s,residualGapPx:Math.max(0,o.gap-s),residualTargetPx:Math.max(0,r-s)}}function Rc(e,n,t,o,r){let s=Math.max(0,Math.min(n.length,Math.min(t,o))),i=Math.max(0,Math.min(n.length,Math.max(t,o)));if(r<=1e-6||i-s<=1e-6)return;let a=e[n.polygon].get(n.edge),c={start:s,end:i,amount:r};a?a.push(c):e[n.polygon].set(n.edge,[c])}function Fo(){return{exactPairs:0,nearPairs:0,patchedPolygons:0,patchedEdges:0,maxMeasuredGapPx:0,maxAppliedAmountPx:0,unclosedPairs:0,maxResidualGapPx:0}}function md(e,n){let t=0,o=0,r=0;for(let s of e){s.size>0&&(r+=1),o+=s.size;for(let i of s.values())for(let a of i)t=Math.max(t,a.amount)}return{...n,patchedPolygons:r,patchedEdges:o,maxAppliedAmountPx:t}}function Bo(e,n,t=!1,o=!1){let r=zc(e),s=Gc(e,r,n.capacityScale),i=o?[]:e.map(()=>new Map),a=Fo(),c=t?[]:void 0;if(s.length===0)return{edgeRepairs:i,diagnostics:a,candidates:c};let l=new Map;for(let u of s){let f=l.get(u.key);f?f.push(u):l.set(u.key,[u])}for(let u of l.values())u.length<2||(a.exactPairs+=u.length===2?1:u.length);return dd(s,l,i,a,n,c),{edgeRepairs:i,diagnostics:o?a:md(i,a),candidates:c}}function dd(e,n,t,o,r,s){let i=r.maxGapPx,a=new Set;for(let[m,p]of n)p.length>1&&a.add(m);let c=(m,p)=>{let h=m.index<p.index?m:p,g=m.index<p.index?p:m;if(g.polygon===h.polygon||h.key===g.key&&a.has(h.key)||!s&&!Ys(h,g)||!pd(h,g,i)||Math.abs(zt(h.dir,g.dir))<.985)return;let y=xd(h,g,i);if(!y)return;let x=fd(h,g),b=y.gap+r.overlapPx;if(x==="material-boundary"){s?.push(Ns(x,h,g,y,b,0));return}let P=b;if(P<=.25){s?.push(Ns(x,h,g,y,b,0));return}let A=h.capacity*y.facingA,C=g.capacity*y.facingB,E=Math.min(A,P/2),S=Math.min(C,P/2);if(P-=E+S,P>1e-6){let M=Math.min(A-E,P);E+=M,P-=M}if(P>1e-6){let M=Math.min(C-S,P);S+=M,P-=M}s?.push(Ns(x,h,g,y,b,E+S)),t.length>0&&E>1e-6&&Rc(t,h,y.aStart,y.aEnd,E/y.facingA),t.length>0&&S>1e-6&&Rc(t,g,y.bStart,y.bEnd,S/y.facingB),o.nearPairs+=1,o.maxMeasuredGapPx=Math.max(o.maxMeasuredGapPx,y.gap),P>.25&&(o.unclosedPairs+=1,o.maxResidualGapPx=Math.max(o.maxResidualGapPx,P))},l=Math.max(28,i*2);if(e.length<=1e4){let m=[...e].sort((g,y)=>g.minX-y.minX),p=new Int32Array(m.length),h=0;for(let g=0;g+1<m.length;g+=1){let x=m[g].maxX+i,b=g+1;for(;b<m.length&&m[b].minX<=x;)b+=1;if(p[g]=b,h+=b-g-1,h>25e6)break}if(h<=2e6||h<=25e6&&yd(e,l,i)){for(let g=0;g+1<m.length;g+=1)for(let y=g+1;y<p[g];y+=1)c(m[g],m[y]);return}}let u=new Map;for(let m of e)gd(u,m,l,i);let f=new Set,d=e.length;for(let m of u.values())for(let p=0;p+1<m.length;p+=1)for(let h=p+1;h<m.length;h+=1){let g=m[p],y=m[h],x=g.index<y.index?g:y,b=g.index<y.index?y:g,P=x.index*d+b.index;f.has(P)||(f.add(P),c(x,b))}}function pd(e,n,t){return e.minX<=n.maxX+t&&n.minX<=e.maxX+t&&e.minY<=n.maxY+t&&n.minY<=e.maxY+t&&e.minZ<=n.maxZ+t&&n.minZ<=e.maxZ+t}function Vc(e,n){return[Math.floor(e[0]/n),Math.floor(e[1]/n),Math.floor(e[2]/n)]}function gd(e,n,t,o){let[r,s,i,a,c,l]=$c(n,t,o);for(let u=r;u<=a;u+=1)for(let f=s;f<=c;f+=1)for(let d=i;d<=l;d+=1){let m=`${u},${f},${d}`,p=e.get(m);p?p.push(n):e.set(m,[n])}}function $c(e,n,t){let[o,r,s]=Vc([e.minX-t,e.minY-t,e.minZ-t],n),[i,a,c]=Vc([e.maxX+t,e.maxY+t,e.maxZ+t],n);return[o,r,s,i,a,c]}function hd(e,n,t){let[o,r,s,i,a,c]=$c(e,n,t);return(i-o+1)*(a-r+1)*(c-s+1)}function yd(e,n,t){let o=0,r=0;for(let s of e){let i=hd(s,n,t);if(o+=i,i>r&&(r=i),r>8192)return!0}return o/Math.max(1,e.length)>256}function Lc(e,n,t){return(e[0]-n[0])*t[0]+(e[1]-n[1])*t[1]+(e[2]-n[2])*t[2]}function wc(e,n,t){return(e.a[0]+e.dir[0]*n-t.a[0])*t.dir[0]+(e.a[1]+e.dir[1]*n-t.a[1])*t.dir[1]+(e.a[2]+e.dir[2]*n-t.a[2])*t.dir[2]}function xd(e,n,t){let o=Lc(n.a,e.a,e.dir),r=Lc(n.b,e.a,e.dir),s=Math.max(0,Math.min(o,r)),i=Math.min(e.length,Math.max(o,r)),a=i-s,c=Math.min(e.length,n.length);if(a<Math.max(.75,c*.12))return null;let l=(s+i)/2,u=e.a[0]+e.dir[0]*l,f=e.a[1]+e.dir[1]*l,d=e.a[2]+e.dir[2]*l,m=Math.max(0,Math.min(n.length,(u-n.a[0])*n.dir[0]+(f-n.a[1])*n.dir[1]+(d-n.a[2])*n.dir[2])),p=n.a[0]+n.dir[0]*m-u,h=n.a[1]+n.dir[1]*m-f,g=n.a[2]+n.dir[2]*m-d,y=Math.hypot(p,h,g);if(y<=1e-6||y>t||y>Math.min(t,Math.max(4,c*.28)))return null;let x=1/y,b=p*x,P=h*x,A=g*x,C=e.outward[0]*b+e.outward[1]*P+e.outward[2]*A,E=-(n.outward[0]*b+n.outward[1]*P+n.outward[2]*A);if(C<.5||E<.5)return null;let S=Math.max(0,Math.min(n.length,wc(e,s,n))),M=Math.max(0,Math.min(n.length,wc(e,i,n)));return{gap:y,facingA:C,facingB:E,aStart:s,aEnd:i,bStart:Math.min(S,M),bEnd:Math.max(S,M)}}function Ds(e,n,t,o){let r=t<=1e-6?0:Math.max(0,Math.min(1,o/t));return[e[0]+(n[0]-e[0])*r,e[1]+(n[1]-e[1])*r]}function kt(e,n){let t=e[e.length-1];t&&Math.hypot(t[0]-n[0],t[1]-n[1])<=1e-6||e.push(n)}function bd(e,n,t){if(!e?.length||n<=1e-6||t<=1e-6)return[];let o=new Set([0,n]);for(let i of e){let a=Math.max(0,Math.min(n,i.start)),c=Math.max(0,Math.min(n,i.end));c-a<=1e-6||i.amount<=1e-6||(o.add(a),o.add(c))}let r=Array.from(o).sort((i,a)=>i-a),s=[];for(let i=0;i+1<r.length;i+=1){let a=r[i],c=r[i+1];if(c-a<=1e-6)continue;let l=(a+c)/2,u=0;for(let d of e)l+1e-6<d.start||l-1e-6>d.end||(u=Math.max(u,d.amount));let f=Math.min(t,u);f>1e-6&&s.push({start:a,end:c,amount:f})}return s}function Pd(e,n){if(n.length<3||!$s(n))return!1;let t=Do(e),o=Do(n);return Math.sign(o)===Math.sign(t)&&Math.abs(o)>=Math.abs(t)-1e-6}function Md(e,n,t){if(n.size===0||e.vertices.length<3||Xs(e))return e;let o=Sn(e.vertices),r=Gs(o);if(!r||!$s(r.local))return e;let s=[];for(let l=0;l<r.local.length;l+=1){let u=r.local[l],f=r.local[(l+1)%r.local.length],d=Math.hypot(f[0]-u[0],f[1]-u[1]),m=kc(r.local,r.area,l);if(!m||d<=1e-6)return e;let p=bd(n.get(l),d,Uc(r.local,l)*t);kt(s,u);let h=0;for(let g of p){g.start>h+1e-6&&kt(s,Ds(u,f,d,g.start));let y=Ds(u,f,d,g.start),x=Ds(u,f,d,g.end);kt(s,[y[0]+m[0]*g.amount,y[1]+m[1]*g.amount]),kt(s,[x[0]+m[0]*g.amount,x[1]+m[1]*g.amount]),g.end<d-1e-6&&kt(s,x),h=g.end}}let i=s[0],a=s[s.length-1];if(i&&a&&Math.hypot(i[0]-a[0],i[1]-a[1])<=1e-6&&s.pop(),!Pd(r.local,s))return e;let c=s.map(([l,u])=>Bc([r.origin[0]+l*r.xAxis[0]+u*r.yAxis[0],r.origin[1]+l*r.xAxis[1]+u*r.yAxis[1],r.origin[2]+l*r.xAxis[2]+u*r.yAxis[2]]));return{...e,vertices:c}}function Xc(e){return e.vertices.length===4&&!Xs(e)}function Sd(e){let n=e.basis?.local;if(!n||n.length!==4)return 1;let t=1/0,o=1/0,r=-1/0,s=-1/0;for(let[c,l]of n)t=Math.min(t,c),o=Math.min(o,l),r=Math.max(r,c),s=Math.max(s,l);let i=Math.max(1e-6,(r-t)*(s-o));return .75+(1-Math.max(0,Math.min(1,Math.abs(e.basis.area)/i)))*2.5}function Fs(e,n){return dt(An(Sn([e])[0],Sn([n])[0]))}function Vo(e,n,t){let o=Fs(e,n),r=Fs(n,t),s=Fs(t,e),i=Math.max(o,r,s);return i<=1e-6?0:dt(zs(An(Sn([n])[0],Sn([e])[0]),An(Sn([t])[0],Sn([e])[0])))*.5/(i*i)}function Ad(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],a=o[1]-t[1],c=r*a-s*i;if(Math.abs(c)<=1e-6)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*a-u*i)/c;return[e[0]+f*r,e[1]+f*s]}function Cd(e,n){if(e.length<3||e.length!==n.length)return null;let t=Math.max(0,...n);if(t<=1e-6)return e;let o=Do(e);if(Math.abs(o)<=1e-6)return null;let r=o>0?1:-1,s=[];for(let c=0;c<e.length;c+=1){let l=e[c],u=e[(c+1)%e.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=1e-6)return null;let p=Math.max(0,n[c]),h=r*(d/m)*p,g=r*(-f/m)*p;s.push({a:[l[0]+h,l[1]+g],b:[u[0]+h,u[1]+g]})}let i=[],a=Math.max(2,t*4);for(let c=0;c<e.length;c+=1){let l=s[(c+e.length-1)%e.length],u=s[c],f=Ad(l.a,l.b,u.a,u.b);if(!f)return null;let d=e[c],m=f[0]-d[0],p=f[1]-d[1],h=Math.hypot(m,p);i.push(h>a?[d[0]+m/h*a,d[1]+p/h*a]:f)}return i}function Td(e,n){return[e.origin[0]+e.xAxis[0]*n[0]+e.yAxis[0]*n[1],e.origin[1]+e.xAxis[1]*n[0]+e.yAxis[1]*n[1],e.origin[2]+e.xAxis[2]*n[0]+e.yAxis[2]*n[1]]}function Lo(e,n){let t=Gs(Sn(e));if(!t)return e;let o=Cd(t.local,n);return o?o.map(r=>Bc(Td(t,r))):e}function Ed(e,n){let t={...e,uvs:void 0,textureTriangles:void 0},[o,r,s,i]=e.vertices,a=Math.min(Vo(o,r,s),Vo(o,s,i)),c=Math.min(Vo(o,r,i),Vo(r,s,i)),l=u=>n.has(u)?.75:0;return a>=c?[{...t,vertices:Lo([o,r,s],[l(0),l(1),1.25])},{...t,vertices:Lo([o,s,i],[1.25,l(2),l(3)])}]:[{...t,vertices:Lo([o,r,i],[l(0),1.25,l(3)])},{...t,vertices:Lo([r,s,i],[l(1),l(2),1.25])}]}function Nc(e){if(e.length===0)return{q1:1/0,median:1/0,q3:1/0,p70:1/0,p80:1/0,p88:1/0,p95:1/0,p97:1/0,max:1/0};let n=[...e].sort((o,r)=>o-r),t=o=>n[Math.min(n.length-1,Math.floor((n.length-1)*o))]??0;return{q1:t(.25),median:t(.5),q3:t(.75),p70:t(.7),p80:t(.8),p88:t(.88),p95:t(.95),p97:t(.97),max:n[n.length-1]??0}}function wo(e,n){let t=e[n];if(t===n)return n;let o=wo(e,t);return e[n]=o,o}function vd(e){let n=new Set,t=new Set;for(let o of e)o.sel&&t.add(o.comp);for(let o of e)if(t.has(o.comp))for(let r of o.e)n.add(r);return{endpoints:n}}function Id(e,n){return n?e.e.some(t=>n.endpoints.has(t)):!0}function _d(e,n,t,o){let r=[];for(let[c,l]of t){if(l.length!==2)continue;let[u,f]=l;if(!Ys(u,f))continue;let d=l.filter(C=>Xc(e[C.polygon]));if(d.length===0)continue;let m=d[0],p=1-Math.min(1,Math.abs(zt(u.normal,f.normal))),h=Math.max(u.length,f.length);if(p<.01&&h<64)continue;let g=Math.max(...d.map(C=>Sd(n[C.polygon]))),y=d.length===2?1.18:1,x=Math.max(.08,p),b=o.viewAware?Od(u,f,o.rotX,o.rotY):{proj:h,vr:1},P=o.viewAware?Math.max(b.proj,h*.12):h,A=o.viewAware?.18+b.vr*1.82:1;r.push({key:c,e:[No(m.a),No(m.b)],o:l,r:d,len:h,rank:P,proj:b.proj,s:P*(.58+x*2.8)*g*y*A,nr:p,sr:g,vr:b.vr,comp:-1,sel:!1,why:"below-threshold",cost:d.length})}let s=Array.from({length:r.length},(c,l)=>l),i=new Map;for(let c=0;c<r.length;c+=1)for(let l of r[c].e){let u=i.get(l);if(u===void 0)i.set(l,c);else{let f=wo(s,u),d=wo(s,c);f!==d&&(s[d]=f)}}let a=new Map;for(let c=0;c<r.length;c+=1){let l=wo(s,c),u=a.get(l);u===void 0&&(u=a.size,a.set(l,u)),r[c].comp=u}return r}function Od(e,n,t,o){let r=Oo(e.a,t,0,o),s=Oo(e.b,t,0,o),i=Math.hypot(s[0]-r[0],s[1]-r[1]),a=Oo(e.normal,t,0,o)[2],c=Oo(n.normal,t,0,o)[2],l=Math.max(0,a,c),u=Math.min(1,Math.abs(a-c));return{proj:i,vr:Math.max(.04,Math.min(1,l*.85+u*.45))}}function Rd(e){let n=new Map;for(let t of e){let o=n.get(t.comp);(!o||t.s>o.s||t.s===o.s&&t.len>o.len)&&n.set(t.comp,t)}return new Set(n.values())}function Ut(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+n*n*n*3.65,o=e.r.length===1?1.08:1;return e.rank*t*o/Math.sqrt(Math.max(1,e.r.length))}function Dc(e){let n=Math.max(0,1-Math.min(1,e.nr)),t=.35+Math.min(1,e.vr)*1.65,o=.7+Math.min(2.6,e.sr)*.3,r=e.r.length===1?1.1:1,s=e.rank*(.6+n*n*1.4)*t*o*r/Math.sqrt(Math.max(1,e.r.length));return Math.max(s,Ut(e)*.75)}function Bs(e){let n=new Set;for(let t of e)for(let o of t.r)n.add(o.polygon);return n.size}function Yc(e,n){return e.nr<=.25&&e.rank>=n.median*1.08&&e.proj>=24}function Vd(e,n,t){return e.s>=n||e.rank>=t}function Ld(e,n,t,o,r,s,i){let a=Math.max(0,Math.floor(t));if(a<=0||e.length===0||n==="follow-up")return{budgetLimit:a,componentBudgets:null};let c=new Map;for(let m of e){let p=c.get(m.comp);p?p.push(m):c.set(m.comp,[m])}let l=new Map,u=Math.max(1e-6,o.q3-o.q1),f=Math.min(o.max,Math.max(o.p95,o.q3+u)),d=0;for(let[m,p]of c){let h=p.filter(A=>Vd(A,s,i)||A.s>=f);if(h.length===0)continue;let g=p.filter(A=>Yc(A,r)),y=Bs(h),x=Bs(g),b=Math.ceil(Math.sqrt(Math.max(0,x-y))*3),P=Math.min(Bs(p),y+b);P<=0||(l.set(m,P),d+=P)}return l.size===0?{budgetLimit:0,componentBudgets:l}:{budgetLimit:Math.min(a,d),componentBudgets:l}}function wd(e,n,t){let o=new Map;if(e.length===0||t<=0)return{selected:o,budgetLimit:0};let r=Nc(e.map(M=>M.s)),s=Nc(e.map(M=>M.rank)),i=Math.max(1e-6,r.q3-r.q1),a=Math.min(r.max,Math.max(r.p95,r.q3+i*.85)),c=Math.min(r.max,Math.max(r.p80,r.q3+i*.15)),l=Math.min(r.max,Math.max(r.p70,r.q3)),u=Math.max(s.p97,s.median*1.8),f=Math.max(s.p88,s.median*1.35),d=Ld(e,n,t,r,s,a,u),m=d.budgetLimit;if(m<=0)return{selected:o,budgetLimit:0};let p=Rd(e),h=new Set,g=new Set,y=new Set,x=new Map,b=n==="primary"?Math.min(Math.max(0,d.budgetLimit-1),Math.max(2,Math.floor(d.budgetLimit*.28))):0,P=[...e].sort((M,T)=>Ut(T)-Ut(M)||T.s/Math.max(1,T.r.length)-M.s/Math.max(1,M.r.length)||T.len-M.len),A=[...e].sort((M,T)=>Dc(T)-Dc(M)||Ut(T)-Ut(M)||T.len-M.len),C=(M,T)=>{M.sel=!0,M.why=T,y.add(M.comp);for(let v of M.e)g.add(v);for(let v of M.r){h.add(v.polygon);let I=o.get(v.polygon);I?I.add(v.edge):o.set(v.polygon,new Set([v.edge]))}},E=(M,T)=>{let v=d.componentBudgets;if(T<=0||!v)return!0;let I=v.get(M.comp)??0;return(x.get(M.comp)??0)+T<=I},S=(M,T)=>{T<=0||!d.componentBudgets||x.set(M.comp,(x.get(M.comp)??0)+T)};for(let M of A){let T=M.r.reduce((I,_)=>I+(h.has(_.polygon)?0:1),0);if(M.cost=T,!(T>m||!E(M,T)||T>0&&m-T<b||!Yc(M,s))&&(C(M,"global-outlier"),S(M,T),m-=T,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}for(let M of P){if(M.sel)continue;let T=M.r.reduce((R,w)=>R+(h.has(w.polygon)?0:1),0);if(M.cost=T,T>m||!E(M,T)||T>0&&m-T<b)continue;let v=M.r.length===2,I=p.has(M)&&(n==="primary"||n==="follow-up"&&M.s>=c*1.08&&M.rank>=f)&&(v?M.s>=c||M.rank>=f:M.s>=c*1.18||M.rank>=f*1.12),_=v?M.s>=a||M.rank>=u:M.s>=a*1.35||M.rank>=u*1.2,O=T===0&&(v?M.s>=l||M.rank>=f:M.s>=l*1.25||M.rank>=f*1.12);if(!(!I&&!_&&!O)&&(C(M,I?"component-anchor":_?"global-outlier":"shared-polygon"),S(M,T),m-=T,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}for(let M of P){if(M.sel)continue;let T=M.r.reduce((w,F)=>w+(h.has(F.polygon)?0:1),0);if(M.cost=T,T>1||T>m||!E(M,T))continue;let v=M.r.some(w=>h.has(w.polygon)),I=M.e.some(w=>g.has(w));if(n==="primary"&&M.r.length===1&&T===1&&(y.has(M.comp)||I||v)&&M.rank>=f*1.05&&M.nr>=.16&&M.vr<=.32){if(C(M,"local-follow-up"),S(M,T),m-=T,m<=0)return{selected:o,budgetLimit:d.budgetLimit};continue}if(M.r.length!==2)continue;let O=v&&T===1&&M.proj>=f*1.45&&M.vr<=.18;if(!I&&!O)continue;if((M.s>=l*.42||M.rank>=f*.62)&&M.s<=l*1.1&&M.proj>=24&&(C(M,"local-follow-up"),S(M,T),m-=T,m<=0))return{selected:o,budgetLimit:d.budgetLimit}}return{selected:o,budgetLimit:d.budgetLimit}}function Wc(e,n,t,o,r){let s=zc(e),i=Gc(e,s,n.capacityScale),a=new Map;for(let u of i){let f=a.get(u.key);f?f.push(u):a.set(u.key,[u])}let c=_d(e,s,a,t).filter(u=>Id(u,r)),l=wd(c,r?"follow-up":"primary",o);return{selected:l.selected,candidates:c,budgetLimit:l.budgetLimit}}function Nd(e,n){let t=[];for(let o=0;o<e.length;o+=1){let r=e[o],s=n.get(o);s&&Xc(r)?t.push(...Ed(r,s)):t.push(r)}return t}function Dd(e){let[n,t]=e.o,o=n??t,r=t??n;return{key:e.key,aPolygon:o?.polygon??-1,aEdge:o?.edge??-1,bPolygon:r?.polygon??-1,bEdge:r?.edge??-1,color:o?.color??r?.color,materialKey:o?.materialKey??r?.materialKey??"",lengthPx:e.len,projectedLengthPx:e.proj,score:e.s,normalRisk:e.nr,shapeRisk:e.sr,viewRisk:e.vr,component:e.comp,marginalCost:e.cost,selected:e.sel,reason:e.why}}function Ws(e,n,t){let o=ft(n),r=Fc(t);if(!mt(o,n)||e.length===0)return e;let s=e,i,a=r.budget;for(let c=0;c<r.passes;c+=1){let l=Wc(s,o,r,a,i);if(c===0&&(a=Math.min(a,l.budgetLimit)),l.selected.size===0)break;let u=Nd(s,l.selected);if(u.length===s.length)break;if(a-=l.selected.size,a<=0){s=u;break}i=vd(l.candidates),s=u}return s}function jc(e,n,t){let o=ft(n),r=Fc(t);if(!mt(o,n)||e.length===0)return{candidates:[],selectedPolygons:0,selectedEdges:0,addedPolygons:0};let s=Wc(e,o,r,r.budget),i=0;for(let a of s.selected.values())i+=a.size;return{candidates:s.candidates.map(Dd),selectedPolygons:s.selected.size,selectedEdges:i,addedPolygons:s.selected.size}}function js(e,n){let t=ft(n);if(!mt(t,n)||e.length===0)return e;let{edgeRepairs:o}=Bo(e,t);return o.some(r=>r.size>0)?e.map((r,s)=>Md(r,o[s],t.capacityScale)):e}function Hc(e,n=ks,t=Us){if(e.length===0)return e;let o=Ws(e,n,t);return js(o,n)}function Kc(e,n){let t=ft(n);return!mt(t,n)||e.length===0?Fo():Bo(e,t).diagnostics}function Zc(e,n){let t=ft(n);return!mt(t,n)||e.length===0?Fo():Bo(e,t,!1,!0).diagnostics}function qc(e,n){let t=ft(n);if(!mt(t,n)||e.length===0)return{diagnostics:Fo(),candidates:[]};let o=Bo(e,t,!0);return{diagnostics:o.diagnostics,candidates:o.candidates??[]}}var zn={ratio:.7,maxError:.18,maxNormalDot:Math.cos(65*Math.PI/180),preserveVertices:!0,vertexKeyMode:"relaxed",lockBoundary:!0,minGroupTriangles:80,maxPasses:12};function Fd(e={}){let n=Number.isFinite(e.ratio)?Math.max(.05,Math.min(.98,e.ratio)):zn.ratio,t=Number.isFinite(e.maxError)&&e.maxError>0?e.maxError:zn.maxError,o=Number.isFinite(e.maxNormalAngleDeg)?Math.max(0,Math.min(89,e.maxNormalAngleDeg)):65;return{ratio:n,maxError:t,maxNormalDot:Math.cos(o*Math.PI/180),preserveVertices:e.preserveVertices??zn.preserveVertices,vertexKeyMode:e.vertexKeyMode??zn.vertexKeyMode,lockBoundary:e.lockBoundary??zn.lockBoundary,minGroupTriangles:Math.max(3,Math.floor(e.minGroupTriangles??zn.minGroupTriangles)),maxPasses:Math.max(1,Math.floor(e.maxPasses??zn.maxPasses))}}function Bd(e){return!!(e.texture||e.material?.texture||e.uvs?.length||e.textureTriangles?.length)}function kd(e){return e.vertices.length===3&&!Bd(e)}function Ud(e){return[e.color??"",e.doubleSided===!0?"2":"1",e.material?.key??""].join("\0")}function Qc(e){return`${Math.round(e[0]*1e5)},${Math.round(e[1]*1e5)},${Math.round(e[2]*1e5)}`}function zd(e,n,t,o){let r=o.vertexKeyMode==="source"?e.simplifySourceVertexKeys?.[t]??e.simplifyVertexKeys?.[t]:e.simplifyVertexKeys?.[t];return r?`${Qc(n)}|${r}`:Qc(n)}function Gd(e,n){return[e[0]+n[0],e[1]+n[1],e[2]+n[2]]}function pt(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function $d(e,n){return[e[0]*n,e[1]*n,e[2]*n]}function Hs(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Jc(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 qs(e){return Math.hypot(e[0],e[1],e[2])}function Ks(e,n){return qs(pt(e,n))}function Xd(e){let n=qs(e);return n>1e-12?[e[0]/n,e[1]/n,e[2]/n]:[0,0,1]}function Qs(e,n,t,o){return Xd(Jc(pt(e[t],e[n]),pt(e[o],e[n])))}function el(e,n,t,o){return qs(Jc(pt(e[t],e[n]),pt(e[o],e[n])))/2}function Yd(){return[0,0,0,0,0,0,0,0,0,0]}function ko(e,n){for(let t=0;t<e.length;t+=1)e[t]+=n[t]}function Wd(e,n,t){let o=e[0],r=e[1],s=e[2],i=-Hs(e,n);return[o*o*t,o*r*t,o*s*t,o*i*t,r*r*t,r*s*t,r*i*t,s*s*t,s*i*t,i*i*t]}function Zs(e,n){let t=n[0],o=n[1],r=n[2];return e[0]*t*t+2*e[1]*t*o+2*e[2]*t*r+2*e[3]*t+e[4]*o*o+2*e[5]*o*r+2*e[6]*o+e[7]*r*r+2*e[8]*r+e[9]}function jd(e,n,t,o,r,s,i,a,c,l,u,f){let d=e*(r*c-s*a)-n*(o*c-s*i)+t*(o*a-r*i);if(Math.abs(d)<1e-10)return null;let m=1/d,p=l*(r*c-s*a)-n*(u*c-s*f)+t*(u*a-r*f),h=e*(u*c-s*f)-l*(o*c-s*i)+t*(o*f-u*i),g=e*(r*f-u*a)-n*(o*f-u*i)+l*(o*a-r*i),y=[p*m,h*m,g*m];return y.every(Number.isFinite)?y:null}function Hd(e,n,t,o){let r=jd(e[0],e[1],e[2],e[1],e[4],e[5],e[2],e[5],e[7],-e[3],-e[6],-e[8]),s=$d(Gd(n,t),.5),i=Ks(n,t)*1.35,a=o?[n,t]:[n,t,s,...r&&Ks(r,s)<=i?[r]:[]],c=a[0],l=Zs(e,c);for(let u=1;u<a.length;u+=1){let f=a[u],d=Zs(e,f);d<l&&(c=f,l=d)}return c}function Kd(e,n){return e<n?`${e}|${n}`:`${n}|${e}`}function Zd(e,n,t){return`${e}|${n}|${t}`}function qd(e,n){let t=new Map,o=[],r=[];for(let s of e){let i=[];for(let u=0;u<s.vertices.length;u+=1){let f=s.vertices[u],d=zd(s,f,u,n),m=t.get(d);m===void 0&&(m=o.length,t.set(d,m),o.push([...f])),i.push(m)}let[a,c,l]=i;a===void 0||c===void 0||l===void 0||a===c||a===l||c===l||r.push({a,b:c,c:l,polygon:s,normal:Qs(o,a,c,l)})}return{vertices:o,triangles:r}}function Qd(e,n){let t=qd(e,n),o=Array.from({length:t.vertices.length},()=>new Set),r=Array.from({length:t.triangles.length},()=>!0);for(let s=0;s<t.triangles.length;s+=1){let i=t.triangles[s];o[i.a].add(s),o[i.b].add(s),o[i.c].add(s)}return{vertices:t.vertices,triangles:t.triangles,alive:r,vertexTriangles:o,activeCount:t.triangles.length}}function Jd(e){let n=new Map,t=(o,r,s)=>{let i=Kd(o,r),a=n.get(i);a?a.triangles.push(s):n.set(i,{a:Math.min(o,r),b:Math.max(o,r),triangles:[s]})};for(let o=0;o<e.triangles.length;o+=1){if(!e.alive[o])continue;let r=e.triangles[o];t(r.a,r.b,o),t(r.b,r.c,o),t(r.c,r.a,o)}return n}function ep(e){let n=Array.from({length:e.vertices.length},()=>Yd());for(let t=0;t<e.triangles.length;t+=1){if(!e.alive[t])continue;let o=e.triangles[t],r=Math.max(1e-6,el(e.vertices,o.a,o.b,o.c)),s=Wd(o.normal,e.vertices[o.a],Math.sqrt(r));ko(n[o.a],s),ko(n[o.b],s),ko(n[o.c],s)}return n}function np(e,n,t){let o=new Set,r=s=>{let i=e.vertexTriangles[s];if(i)for(let a of i)e.alive[a]&&o.add(a)};return r(n),r(t),Array.from(o).sort((s,i)=>s-i)}function tp(e,n,t){let o=e.vertexTriangles[n],r=e.vertexTriangles[t];if(!o||!r)return 0;let[s,i]=o.size<=r.size?[o,r]:[r,o],a=0;for(let c of s)e.alive[c]&&i.has(c)&&(a+=1);return a}function nl(e,n,t){return[e.a===t?n:e.a,e.b===t?n:e.b,e.c===t?n:e.c]}function op(e,n,t){if(tp(e,n.a,n.b)!==2)return null;let o=np(e,n.a,n.b);if(o.length===0)return null;let r=e.vertices[n.a];e.vertices[n.a]=n.target;let s=0,i=!0;for(let a of o){let c=e.triangles[a],[l,u,f]=nl(c,n.a,n.b);if(l===u||l===f||u===f)continue;if(el(e.vertices,l,u,f)<=1e-8){i=!1;break}let m=Qs(e.vertices,l,u,f);if(Hs(c.normal,m)<t.maxNormalDot){i=!1;break}let p=c.a===n.a?r:e.vertices[c.a];if(Math.abs(Hs(c.normal,pt(n.target,p)))>t.maxError){i=!1;break}s+=1}return e.vertices[n.a]=r,i&&s>0?o:null}function rp(e,n,t){e.vertexTriangles[t.a]?.delete(n),e.vertexTriangles[t.b]?.delete(n),e.vertexTriangles[t.c]?.delete(n)}function sp(e,n,t){e.vertexTriangles[t.a].add(n),e.vertexTriangles[t.b].add(n),e.vertexTriangles[t.c].add(n)}function ip(e,n,t){e.vertices[n.a]=n.target;let o=new Set;for(let r of t){if(!e.alive[r])continue;let s=e.triangles[r],[i,a,c]=nl(s,n.a,n.b);if(rp(e,r,s),i===a||i===c||a===c){e.alive[r]=!1,e.activeCount-=1;continue}let l=Zd(i,a,c);if(o.has(l)){e.alive[r]=!1,e.activeCount-=1;continue}o.add(l);let u={...s,a:i,b:a,c,normal:Qs(e.vertices,i,a,c)};e.triangles[r]=u,sp(e,r,u)}}function ap(e,n){let t=Jd(e),o=new Set;for(let a of t.values())a.triangles.length>2&&(o.add(a.a),o.add(a.b));if(n.lockBoundary)for(let a of t.values())a.triangles.length===1&&(o.add(a.a),o.add(a.b));let r=ep(e),s=n.maxError*n.maxError,i=[];for(let a of t.values()){if(a.triangles.length!==2||o.has(a.a)||o.has(a.b))continue;let c=[...r[a.a]];ko(c,r[a.b]);let l=Hd(c,e.vertices[a.a],e.vertices[a.b],n.preserveVertices),u=Math.max(0,Zs(c,l));if(u>s*Math.max(1,a.triangles.length))continue;let f=Ks(e.vertices[a.a],e.vertices[a.b]);i.push({a:a.a,b:a.b,target:l,error:u,length:f})}return i.sort((a,c)=>a.error-c.error||a.length-c.length),i}function cp(e){let n=[];for(let t=0;t<e.triangles.length;t+=1)e.alive[t]&&n.push(e.triangles[t]);return n}function lp(e,n){if(e.length<n.minGroupTriangles)return e;let t=Qd(e,n),o=t.activeCount;if(o<n.minGroupTriangles)return e;let r=Math.max(1,Math.floor(o*n.ratio));if(r>=o)return e;let s=0;for(let i=0;i<n.maxPasses&&t.activeCount>r;i+=1){let a=ap(t,n);if(a.length===0)break;let c=0;for(let l of a){if(t.activeCount<=r)break;let u=op(t,l,n);if(!u)continue;let f=t.activeCount;ip(t,l,u),!(t.activeCount>=f)&&(s+=1,c+=1)}if(c===0)break}return s===0||t.activeCount>=o?e:cp(t).map(i=>({...i.polygon,vertices:[[...t.vertices[i.a]],[...t.vertices[i.b]],[...t.vertices[i.c]]]}))}function up(e){let n=new Map;for(let t=0;t<e.length;t+=1){let o=e[t];if(!kd(o))continue;let r=Ud(o),s=n.get(r);s?(s.indices.push(t),s.polygons.push(o)):n.set(r,{indices:[t],polygons:[o]})}return n}function Uo(e,n){let t=Fd(n),o=up(e);if(o.size===0)return e;let r=new Map,s=!1;for(let a of o.values()){let c=lp(a.polygons,t);if(!(c===a.polygons||c.length>=a.polygons.length)){r.set(a.indices[0],c);for(let l=1;l<a.indices.length;l+=1)r.set(a.indices[l],[]);s=!0}}if(!s)return e;let i=[];for(let a=0;a<e.length;a+=1){let c=r.get(a);c?i.push(...c):c===void 0&&i.push(e[a])}return i}var fp=3,mp=.03,dp=.02,$t={maxAngleDeg:fp,maxPlaneDisplacement:mp,maxBoundaryDisplacement:dp,isolatedPairs:!1},zo={maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.04,isolatedPairs:!0},Js=[{...zo,maxAngleDeg:30},{...zo,maxAngleDeg:45},{...zo,maxAngleDeg:60,maxBoundaryDisplacement:.06}],pp=1800,gp=.65,tl=500,ol=2200,hp=1e3,yp=2200,xp=350,bp=700,Pp=60,Mp=.06,Sp=4,Ap=.003,Cp=4e3,Tp=3e3,Ep=4500,vp=2400,Ip=4200,_p=1800,Op=5e3,Rp=.28,Vp=.15,Lp=.23,wp=2e3,Np=16,Dp=4e3,Fp=900,Bp=16,kp=1.5,ni=.045,Up=.999,zp=1e-5,Gp=24,$p=50,Xp=1e3,Yp=1e3,$n={minGroupPolygons:2,maxCandidateAxes:24},Wp={...$n,maxCandidateAxes:1},rl={...$n,maxCandidateAxes:2},jp={...$n,maxCandidateAxes:2},Hp={steps:[{kind:"baselineCandidates"},{kind:"initialLossyCandidates"},{kind:"finalLossyCandidates"}]};function ll(e,n={}){return Kp(e,n).polygons}function ul(e,n={}){let t={meshResolution:n.meshResolution,stopAtPolygonCount:n.stopAtPolygonCount,rectCover:n.rectCover},o=new Go,r={captureVisiblePolygons:!0,skipInteriorCull:n.skipInteriorCull===!0};return o.workspaceFor(e,r).createRun(t,r).optimizeParse({staticSimplification:n.staticSimplification,useCandidateFirst:n.useCandidateFirst===!0}).polygons}function Kp(e,n={},t={}){return new Go().workspaceFor(e,t).createRun(n,t).optimize()}var Go=class{constructor(){this.workspaces=new Map}workspaceFor(n,t){let o=Zp(t),r=this.workspaces.get(o);r||(r=new WeakMap,this.workspaces.set(o,r));let s=r.get(n);return s||(s=new ti(n,t,this),r.set(n,s)),s}};function Zp(e){return[e.skipInteriorCull===!0?"skip-cull":"cull",e.simplifiedCandidate===!0?"simplified":"source"].join(":")}var ti=class{constructor(n,t,o){this.source=n,this.graph=o,this.preprocessCache={skipInteriorCull:t.skipInteriorCull===!0}}polygons(){return this.source}preprocess(n){return Pg(this.source,n,this.preprocessCache)}configureSnappedCullReuse(n,t){let o=n.length/Math.max(1,this.source.length);this.preprocessCache.reuseSnappedInteriorCull=t.simplifiedCandidate===!0?this.source.length<=Ip:n.length<=_p||n.length<=Op&&o>=Rp}visiblePolygons(){return this.preprocessCache.interior??this.preprocessCache.deduped??this.source}createRun(n,t){return new oi(this,n,t)}workspaceFor(n,t){return this.graph.workspaceFor(n,t)}},oi=class e{constructor(n,t,o,r={}){this.baselineCandidatesComplete=!1;this.initialLossyCandidatesComplete=!1;this.finalLossyCandidatesComplete=!1;this.workspace=n,this.source=n.polygons(),this.options=t,this.runOptions=o,this.costCandidateMode=r.costCandidateMode??"cost",this.seamCandidateMode=r.seamCandidateMode??"seamSafe",this.offerBaseline=r.offerBaseline===!0,this.meshResolution=t.meshResolution??"lossy",this.stopAtPolygonCount=Number.isFinite(t.stopAtPolygonCount)?Math.max(0,Math.floor(t.stopAtPolygonCount)):void 0,this.requiredMaxPolygonCount=Number.isFinite(o.requiredMaxPolygonCount)?Math.max(0,Math.floor(o.requiredMaxPolygonCount)):void 0,this.baseline=this.preprocess(!1),this.workspace.configureSnappedCullReuse(this.baseline,o),this.visiblePolygons=o.captureVisiblePolygons?this.workspace.visiblePolygons():void 0,this.acceptor=r.acceptor??new ri(this.baseline,this.requiredMaxPolygonCount)}optimize(){return this.runPlan(Hp)}optimizeParse(n){let t=n.staticSimplification===!1?null:n.staticSimplification??{},o=[{kind:"baselineCandidates"},{kind:"initialLossyCandidates"}];return n.useCandidateFirst||o.push({kind:"finalLossyCandidates"}),t&&o.push({kind:"staticSimplificationCandidates",options:t,stopOnAccept:n.useCandidateFirst}),n.useCandidateFirst&&o.push({kind:"finalLossyCandidates"}),this.runPlan({steps:o})}get best(){return this.acceptor.polygons}get bestCost(){return this.acceptor.cost}runStaticSimplificationCandidates(n={}){if(this.meshResolution!=="lossy"||!fg(this.source,this.best))return!1;let t=ml(this.source,n);if(!t)return!1;for(let o of this.staticSimplificationPlans(t)){let r=mg(o,n);if(r&&this.generateStaticSimplificationCandidate(r,n,o.skipInteriorCull))return!0}return!1}staticSimplificationPlans(n){let t=this.source.length-n.length,o=[],r=this.visiblePolygons&&this.visiblePolygons!==this.source?this.source.length-this.visiblePolygons.length:0;return this.visiblePolygons&&this.visiblePolygons!==this.source&&this.source.length>=Dp&&r>=Fp&&(t<=Bp||r>=t*kp)&&o.push({source:this.visiblePolygons,skipInteriorCull:!0}),this.best.length>=wp&&t<=Np&&ug(this.source)&&o.push({source:this.source,vertexKeyMode:"source"}),o.push({source:this.source,precomputed:n}),o}generateStaticSimplificationCandidate(n,t,o=!1){let r={requiredMaxPolygonCount:this.best.length-1,skipExactRectCover:!0,skipInteriorCull:o,simplifiedCandidate:!0},s=this.workspace.workspaceFor(n,r),i=this.best;return new e(s,{meshResolution:"lossy",stopAtPolygonCount:lg(this.best.length,t)},r,{acceptor:this.acceptor,costCandidateMode:"dom",seamCandidateMode:"dom",offerBaseline:!0}).optimize(),this.best!==i}runPlan(n){for(let t of n.steps){let o=!1;if(t.kind==="baselineCandidates")this.runBaselineCandidates();else if(t.kind==="staticSimplificationCandidates")o=this.runStaticSimplificationCandidates(t.options)&&t.stopOnAccept;else{if(!this.shouldRunLossyPipeline())break;t.kind==="initialLossyCandidates"?this.runInitialLossyPipeline():this.runFinalLossyPipeline()}if(o||this.shouldStop())break}return this.result()}runBaselineCandidates(){if(this.baselineCandidatesComplete||(this.baselineCandidatesComplete=!0,this.offerBaseline&&(this.acceptCandidate(this.baseline),this.shouldStop())))return;let n=this.runOptions.skipExactRectCover===!0?!1:this.meshResolution==="lossy"&&this.options.rectCover===void 0?pg(this.baseline):this.options.rectCover;if(this.shouldStop())return;let t=Gn(this.baseline,n);if(t!==this.baseline&&this.acceptCandidate(t),!this.shouldStop()&&this.meshResolution==="lossy"&&this.options.rectCover===void 0&&this.runOptions.skipExactRectCover!==!0){let o=Gn(this.baseline,void 0);o!==this.baseline&&this.acceptCandidate(o)}}shouldRunLossyPipeline(){return this.meshResolution==="lossy"&&!this.shouldStop()&&this.best.length>1&&this.bestCost>1+1e-9}runInitialLossyPipeline(){if(this.initialLossyCandidatesComplete)return;this.initialLossyCandidatesComplete=!0;let n=this.preprocess(zo);this.acceptCandidate(n),!this.shouldStop()&&(this.options.rectCover===void 0&&this.source.length>=tl&&this.source.length<=ol&&(this.acceptCandidate(Gn(n,rl)),this.shouldStop())||this.options.rectCover!==void 0&&this.options.rectCover!==!1&&(this.acceptCandidate(Gn(n,this.options.rectCover)),this.shouldStop()))}runFinalLossyPipeline(){this.finalLossyCandidatesComplete||(this.finalLossyCandidatesComplete=!0,!(this.runOptions.simplifiedCandidate===!0&&this.source.length>Ep)&&(this.runAggressiveLossyVariants(),!this.shouldStop()&&this.runLargeRectCoverCandidate()))}runAggressiveLossyVariants(){let n=this.stopAtPolygonCount===void 0&&this.source.length>=Cp&&this.best.length<=Tp,t=!1;for(let o=n?Js.length:0;o<Js.length;o+=1){if(o===1&&!t||o===1&&this.runOptions.simplifiedCandidate===!0&&this.source.length<vp||o===2&&!hg(this.source))continue;let r=this.preprocess(Js[o]),s=r;this.options.rectCover===void 0&&this.source.length>=tl&&this.source.length<=ol&&(s=Gn(r,rl)),this.options.rectCover!==void 0&&this.options.rectCover!==!1&&(s=Gn(r,this.options.rectCover));let i=this.acceptSeamSafeCandidate(s,this.source.length);if(o===0&&i&&(t=!0),this.shouldStop())return}}runLargeRectCoverCandidate(){if(this.options.rectCover!==void 0)return;let n=Gn(this.best,gg(this.best));n!==this.best&&this.acceptSeamSafeCandidate(n,this.best.length)}preprocess(n){return this.workspace.preprocess(n)}result(){return{polygons:this.best,visiblePolygons:this.visiblePolygons}}shouldStop(){return this.stopAtPolygonCount!==void 0&&this.best.length<=this.stopAtPolygonCount}acceptCandidate(n,t=Xn(n)){return this.emitCandidate({mode:this.costCandidateMode,polygons:n,cost:t})}acceptDomCandidate(n){return this.emitCandidate({mode:"dom",polygons:n})}acceptSeamSafeCandidate(n,t,o){return this.emitCandidate({mode:this.seamCandidateMode,polygons:n,sourcePolygonCount:t,cost:o})}emitCandidate(n){return this.acceptor.accept({...n,maxPolygonCount:n.maxPolygonCount??this.requiredMaxPolygonCount})}},ri=class{constructor(n,t){this.best=n,this.bestCost=Xn(n),this.bestDiagnostics={polygons:n},this.requiredMaxPolygonCount=t}get polygons(){return this.best}get cost(){return this.bestCost}accept(n){return n.mode==="cost"?this.acceptCostCandidate(n.polygons,n.cost):n.mode==="dom"?this.acceptDomCandidate(n.polygons,n.cost,n.maxPolygonCount):this.acceptSeamSafeCandidate(n.polygons,n.sourcePolygonCount??n.polygons.length,n.cost,n.maxPolygonCount)}acceptCostCandidate(n,t=Xn(n)){return t>=this.bestCost?!1:(this.commit(n,t),!0)}acceptDomCandidate(n,t=Xn(n),o){return n.length>=this.best.length||!this.withinMaxPolygonCount(n,o)?!1:(this.commit(n,t),!0)}acceptSeamSafeCandidate(n,t,o,r){if(n.length>=this.best.length||!this.withinMaxPolygonCount(n,r))return!1;let s=cg(t);if(this.bestCost-n.length<s)return!1;let i=o??Xn(n),a=this.bestCost-i;if(a<=0||a<s)return!1;let c=sl(n);if(!c)return!1;let l=this.bestSeamDiagnostics();return!l||qp(c,l)||Qp(this.bestTopologyEdges(),this.bestTopologySelfDiagnostics(),n)?!1:(this.commit(n,i,c),!0)}withinMaxPolygonCount(n,t){let o=t??this.requiredMaxPolygonCount;return o===void 0||n.length<=o}commit(n,t,o){this.best=n,this.bestCost=t,this.bestDiagnostics={polygons:n,seam:o}}resetBestDiagnostics(n){this.bestDiagnostics={polygons:this.best,seam:n}}bestSeamDiagnostics(){if(this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),!this.bestDiagnostics.seam){let n=sl(this.best);if(!n)return null;this.bestDiagnostics.seam=n}return this.bestDiagnostics.seam}bestTopologyEdges(){return this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),this.bestDiagnostics.topologyEdges||(this.bestDiagnostics.topologyEdges=fl(this.best)),this.bestDiagnostics.topologyEdges}bestTopologySelfDiagnostics(){return this.bestDiagnostics.polygons!==this.best&&this.resetBestDiagnostics(),this.bestDiagnostics.topologySelf||(this.bestDiagnostics.topologySelf=tg(this.bestTopologyEdges(),ni)),this.bestDiagnostics.topologySelf}};function Xn(e){let n=0;for(let t of e){let o=t.vertices.length,r=o<=4?0:Math.min(4,o-4)*.12,s=t.texture||t.material?.texture||t.textureTriangles?.length?.15:0;n+=1+r+s}return n}function sl(e){try{return Zc(e)}catch(n){if(n instanceof RangeError&&n.message==="Set maximum size exceeded")return null;throw n}}function qp(e,n){return e.nearPairs>n.nearPairs||e.unclosedPairs>n.unclosedPairs||e.maxMeasuredGapPx>n.maxMeasuredGapPx+1e-9||e.maxResidualGapPx>n.maxResidualGapPx+1e-9}function Qp(e,n,t){let o=fl(t);return Jp(e,o,ni)?!0:ng(o.boundarySegments,ni,n)}function Jp(e,n,t){if(e.internalSegments.length===0)return!1;let o=eg(e,t);for(let r of n.boundarySegments)if(!e.boundaryKeys.has(r.key)&&(e.internalKeys.has(r.key)||ig(r,o,t)))return!0;return!1}function eg(e,n){return(!e.internalIndex||e.internalIndexTolerance!==n)&&(e.internalIndex=ai(e.internalSegments,n),e.internalIndexTolerance=n),e.internalIndex}function ng(e,n,t){let o=ai(e,n),r=new Set,s=0,i=0,a=0;for(let c of e)s=Math.max(s,c.index+1);for(let c of e)for(let l of ci(c,o,n)){if(l===c||l.polygon===c.polygon||l.key===c.key)continue;let u=c.index<l.index?c.index*s+l.index:l.index*s+c.index;if(r.has(u))continue;r.add(u);let f=li(c,l,n);if(f&&(i+=1,a+=f.overlapLength,i>t.tJunctionPairs||a>t.tJunctionLength+1e-9))return!0}return!1}function tg(e,n){let t={tJunctionPairs:0,tJunctionLength:0};return og(t,e.boundarySegments,n),t}function og(e,n,t){let o=ai(n,t),r=new Set,s=0;for(let i of n)s=Math.max(s,i.index+1);for(let i of n)for(let a of ci(i,o,t)){if(a===i||a.polygon===i.polygon||a.key===i.key)continue;let c=i.index<a.index?i.index*s+a.index:a.index*s+i.index;if(r.has(c))continue;r.add(c);let l=li(i,a,t);l&&(e.tJunctionPairs+=1,e.tJunctionLength+=l.overlapLength)}}function fl(e){let n=new Map;for(let c=0;c<e.length;c+=1){let l=e[c].vertices;for(let u=0;u<l.length;u+=1){let f=l[u],d=l[(u+1)%l.length],m=rg(Wt(f,d),c,u,f,d);if(!m)continue;let p=n.get(m.key);p?p.count+=1:n.set(m.key,{count:1,segment:m})}}let t=new Set,o=new Set,r=[],s=[],i=0,a=0;for(let c of n.values()){let l={...c.segment,index:a};a+=1,c.count===1?(t.add(l.key),r.push(l),i+=l.length):(o.add(l.key),s.push(l))}return{boundaryKeys:t,internalKeys:o,boundarySegments:r,internalSegments:s,boundaryLength:i}}function rg(e,n,t,o,r){let s=Cn(r,o),i=Math.hypot(s[0],s[1],s[2]);if(i<=1e-10)return null;let a=[s[0]/i,s[1]/i,s[2]/i];return{index:-1,key:e,polygon:n,edge:t,a:o,b:r,dir:a,length:i,minX:Math.min(o[0],r[0]),minY:Math.min(o[1],r[1]),minZ:Math.min(o[2],r[2]),maxX:Math.max(o[0],r[0]),maxY:Math.max(o[1],r[1]),maxZ:Math.max(o[2],r[2])}}function ai(e,n){let t=Math.max(n*8,.5),o=new Map;for(let r of e)sg(o,r,t,n);return{cellSize:t,cells:o}}function sg(e,n,t,o){let[r,s,i]=$o([n.minX-o,n.minY-o,n.minZ-o],t),[a,c,l]=$o([n.maxX+o,n.maxY+o,n.maxZ+o],t);for(let u=r;u<=a;u+=1)for(let f=s;f<=c;f+=1)for(let d=i;d<=l;d+=1){let m=`${u},${f},${d}`,p=e.get(m);p?p.push(n):e.set(m,[n])}}function $o(e,n){return[Math.floor(e[0]/n),Math.floor(e[1]/n),Math.floor(e[2]/n)]}function ci(e,n,t){let o=[],r=new Set,[s,i,a]=$o([e.minX-t,e.minY-t,e.minZ-t],n.cellSize),[c,l,u]=$o([e.maxX+t,e.maxY+t,e.maxZ+t],n.cellSize);for(let f=s;f<=c;f+=1)for(let d=i;d<=l;d+=1)for(let m=a;m<=u;m+=1){let p=n.cells.get(`${f},${d},${m}`);if(p)for(let h of p)r.has(h)||(r.add(h),o.push(h))}return o}function ig(e,n,t){for(let o of ci(e,n,t))if(li(e,o,t))return!0;return!1}function li(e,n,t){if(!ag(e,n,t)||Math.abs(qe(e.dir,n.dir))<Up)return null;let o=qe(Cn(n.a,e.a),e.dir),r=qe(Cn(n.b,e.a),e.dir),s=Math.max(0,Math.min(o,r)),i=Math.min(e.length,Math.max(o,r)),a=i-s;if(a<=Math.max(zp,Math.min(e.length,n.length)*1e-4))return null;let c=[e.a[0]+e.dir[0]*((s+i)/2),e.a[1]+e.dir[1]*((s+i)/2),e.a[2]+e.dir[2]*((s+i)/2)],l=Math.max(0,Math.min(n.length,qe(Cn(c,n.a),n.dir))),u=[n.a[0]+n.dir[0]*l,n.a[1]+n.dir[1]*l,n.a[2]+n.dir[2]*l],f=El(c,u);return f<=t?{overlapLength:a,offset:f}:null}function ag(e,n,t){return e.minX<=n.maxX+t&&n.minX<=e.maxX+t&&e.minY<=n.maxY+t&&n.minY<=e.maxY+t&&e.minZ<=n.maxZ+t&&n.minZ<=e.maxZ+t}function cg(e){return Math.max(Sp,e*Ap)}function lg(e,n){let t=Number.isFinite(n.earlyStopDropRatio)?Math.max(0,n.earlyStopDropRatio):Vp;return Math.max(0,e-Math.max(1,Math.ceil(e*t)))}function ug(e){return e.some(n=>n.simplifySourceVertexKeys?.length===n.vertices.length)}function fg(e,n){return e.length===0||n.length/e.length>Lp}function ml(e,n,t){let o=Uo(e,{...n.simplifyTriangleMeshOptions,...t?{vertexKeyMode:t}:{}});return o===e||o.length>=e.length?null:o}function mg(e,n){return e.precomputed!==void 0?e.precomputed:ml(e.source,n,e.vertexKeyMode)}function Gn(e,n){if(n===!1)return e;let t=dg(e,n);if(!t)return e;let o=_o(e,t);return o.length<e.length?o:e}function dg(e,n){if(n&&n!==!0)return n;let t=n===!0,o=e.length;return o>2200||!t&&o>=Gp&&o<$p||!t&&o>Xp?null:!t&&o<=300?{...$n,maxCandidateAxes:1}:o>300?!t&&(dl(e)>12||pl(e)>Yp)?null:{...$n,maxCandidateAxes:Math.min($n.maxCandidateAxes??24,2)}:$n}function pg(e){return e.length>pp||e.length===0||yg(e)/e.length<gp?!1:Wp}function gg(e){return e.length<hp||e.length>yp||dl(e)<=12||pl(e)>xp?!1:jp}function hg(e){return e.length<=bp}function yg(e){let n=0;for(let t of e)t.vertices.length===3&&(n+=1);return n}function dl(e){let n=0;for(let t of e)n=Math.max(n,t.vertices.length);return n}function pl(e){let n=new Map;for(let o of e)for(let r=0;r<o.vertices.length;r++){let s=o.vertices[r],i=o.vertices[(r+1)%o.vertices.length],a=Wt(s,i);n.set(a,(n.get(a)??0)+1)}let t=0;for(let o of n.values())o===1&&(t+=1);return t}function Xo(e,n){return n==null?e:n.map(t=>e[t]).filter(t=>!!t)}function si(e,n){if(n===e||n.length===e.length&&n.every((r,s)=>r===e[s]))return null;let t=new Set(n),o=[];for(let r=0;r<e.length;r++)t.has(e[r])&&o.push(r);return o.length===e.length?null:o}function xg(e,n){if(n?.deduped)return n.deduped;let t=n?.dedupedIndices;if(t===void 0){let r=Lt(e);if(r.size===0)t=null;else{t=[];for(let s=0;s<e.length;s++)r.has(s)||t.push(s)}n&&(n.dedupedIndices=t)}let o=Xo(e,t);return n&&(n.deduped=o),o}function bg(e,n){if(n?.skipInteriorCull)return e;if(n?.interior)return n.interior;let t=n?.interiorIndices;if(t===void 0){let r=fn(e);t=si(e,r),n&&(n.interiorIndices=t)}let o=Xo(e,t);return n&&(n.interior=o),o}function Pg(e,n,t){let o=xg(e,t),r=bg(o,t),s=t?.baseline??tn(r);if(t&&!t.baseline&&(t.baseline=s),!n)return s;let i=n===!0?$t:Sg(n);if(i.isolatedPairs){let u=al(il(o,t),i,t),f=tn(u);if(t?.snappedInteriorUsesBaselineFilter&&Mg(s,f)){let d=al(il(o,t,!0),i,t),m=tn(d);m.length<f.length&&(f=m)}return f.length<s.length?f:s}let a=Ng(o,i,t),c=fn(a),l=tn(c);return l.length<s.length?l:s}function gl(e,n){return n?(n.snapped||(n.snapped=Yo(e)),n.snapped):Yo(e)}function il(e,n,t=!1){if(!n)return fn(Yo(e));if(t&&n.snappedInteriorExact)return n.snappedInteriorExact;if(!t&&n.snappedInterior)return n.snappedInterior;let o=gl(e,n);if(t){if(o===e&&n.deduped===e&&n.interior)return n.snappedInteriorExactIndices=n.interiorIndices,n.snappedInteriorExact=n.interior,n.snappedInteriorExact;let r=fn(o);return n.snappedInteriorExactIndices=si(o,r),n.snappedInteriorExact=r,n.snappedInteriorExact}if(!n.snappedInterior){if(o===e&&n.deduped===e&&n.interior)return n.snappedInteriorIndices=n.interiorIndices,n.snappedInterior=n.interior,n.snappedInteriorUsesBaselineFilter=!1,n.snappedInterior;if(n.reuseSnappedInteriorCull!==!1&&n.interiorIndices!==void 0)return n.snappedInteriorIndices=n.interiorIndices,n.snappedInterior=Xo(o,n.snappedInteriorIndices),n.snappedInteriorUsesBaselineFilter=!0,n.snappedInterior;if(n.snappedInteriorIndices===void 0){let r=fn(o);n.snappedInteriorIndices=si(o,r),n.snappedInterior=r,n.snappedInteriorExactIndices=n.snappedInteriorIndices,n.snappedInteriorExact=r,n.snappedInteriorUsesBaselineFilter=!1}else n.snappedInterior=Xo(o,n.snappedInteriorIndices)}return n.snappedInterior}function Mg(e,n){return n.length>=e.length}function Sg(e){return{maxAngleDeg:e.maxAngleDeg??$t.maxAngleDeg,maxPlaneDisplacement:e.maxPlaneDisplacement??$t.maxPlaneDisplacement,maxBoundaryDisplacement:e.maxBoundaryDisplacement??$t.maxBoundaryDisplacement,isolatedPairs:e.isolatedPairs??$t.isolatedPairs}}function al(e,n,t){let o=Ag(e,t),r=[];for(let i of Cg(e,o))Tg(i,n)&&r.push(i.candidate);let s=vg(r);return s.length===0?e:Eg(e,s)}function Ag(e,n){if(n?.trianglePairSource?.polygons===e)return n.trianglePairSource;let t=e.map(i=>{let a=fi(i);return a?{polygon:i,normal:a.normal,area:a.area,materialKey:Ml(i)}:null}),o=new Map;for(let i=0;i<e.length;i++){let a=e[i];if(!(a.vertices.length!==3||!t[i]))for(let c=0;c<a.vertices.length;c++){let l=Wt(a.vertices[c],a.vertices[(c+1)%a.vertices.length]),u=o.get(l);u?u.push(i):o.set(l,[i])}}let r=[];for(let i of o.values())i.length===2&&r.push([i[0],i[1]]);let s={polygons:e,metas:t,edgeOwnerPairs:r};return n&&(n.trianglePairSource=s),s}function Cg(e,n){if(n.preparedCandidates)return n.preparedCandidates;let t=[];for(let[o,r]of n.edgeOwnerPairs){let s=Vg(o,r,e,n.metas);s&&t.push(s)}return n.preparedCandidates=t,t}function Tg(e,n){let t=Math.cos(n.maxAngleDeg*Math.PI/180);return e.normalDot>=t&&e.maxDistance<=Math.min(n.maxPlaneDisplacement,n.maxBoundaryDisplacement)}function Eg(e,n){let t=new Map,o=new Set,r=hl(n.flatMap(i=>i.vertexMoves));for(let i of n){let a=Math.min(i.a,i.b);t.set(a,i.polygon),o.add(Math.max(i.a,i.b))}let s=[];for(let i=0;i<e.length;i++){let a=t.get(i);if(a){s.push(a);continue}o.has(i)||s.push(e[i])}return r.size>0?yl(s,r):s}function vg(e){return e.length>3e3?Ig(e):_g(e)}function Ig(e){let n=new Map;for(let s of e)n.set(s.a,(n.get(s.a)??0)+1),n.set(s.b,(n.get(s.b)??0)+1);let t=[...e].sort((s,i)=>{let a=(n.get(s.a)??0)+(n.get(s.b)??0),c=(n.get(i.a)??0)+(n.get(i.b)??0);return a-c||s.score-i.score}),o=new Set,r=[];for(let s of t)o.has(s.a)||o.has(s.b)||(o.add(s.a),o.add(s.b),r.push(s));return r}function _g(e){let n=new Map;for(let l=0;l<e.length;l++){let u=e[l],f=n.get(u.a);f?f.push(l):n.set(u.a,[l]);let d=n.get(u.b);d?d.push(l):n.set(u.b,[l])}let t=[],o=new Array(e.length).fill(!0),r=new Map,s=new ii;for(let[l,u]of n)r.set(l,u.length);let i=l=>(r.get(l.a)??0)+(r.get(l.b)??0),a=l=>{let u=e[l];s.push({degree:i(u),score:u.score,index:l})},c=(l,u)=>{if(!o[l])return;o[l]=!1;let f=e[l];for(let d of[f.a,f.b])r.set(d,(r.get(d)??0)-1),u.add(d)};for(let l=0;l<e.length;l++)a(l);for(;s.size()>0;){let l=s.pop();if(!o[l.index])continue;let u=e[l.index];if(i(u)!==l.degree){a(l.index);continue}t.push(u);let d=new Set;for(let m of[u.a,u.b])for(let p of n.get(m)??[])c(p,d);for(let m of d)for(let p of n.get(m)??[])o[p]&&a(p)}return t}var ii=class{constructor(){this.items=[]}size(){return this.items.length}push(n){this.items.push(n);let t=this.items.length-1;for(;t>0;){let o=t-1>>1;if(ei(this.items[o],this.items[t])<=0)break;[this.items[o],this.items[t]]=[this.items[t],this.items[o]],t=o}}pop(){if(this.items.length===0)return null;let n=this.items[0],t=this.items.pop();if(this.items.length>0){this.items[0]=t;let o=0;for(;;){let r=o*2+1,s=r+1,i=o;if(r<this.items.length&&ei(this.items[r],this.items[i])<0&&(i=r),s<this.items.length&&ei(this.items[s],this.items[i])<0&&(i=s),i===o)break;[this.items[o],this.items[i]]=[this.items[i],this.items[o]],o=i}}return n}};function ei(e,n){return e.degree-n.degree||e.score-n.score||e.index-n.index}function hl(e){let n=new Map;for(let o of e){let r=n.get(o.key);r?(r.x+=o.target[0],r.y+=o.target[1],r.z+=o.target[2],r.count+=1):n.set(o.key,{x:o.target[0],y:o.target[1],z:o.target[2],count:1})}let t=new Map;for(let[o,r]of n)t.set(o,[r.x/r.count,r.y/r.count,r.z/r.count]);return t}function Og(e,n){let t=[];for(let o=0;o<e.length;o++){let r=e[o].vertices,s=n[o]?.vertices;if(!s||s.length!==r.length)continue;for(let c=0;c<r.length;c++)t.push({key:sn(r[c]),target:s[c]});let i=e[o].textureTriangles??[],a=n[o]?.textureTriangles??[];for(let c=0;c<i.length;c++){let l=a[c];if(l)for(let u=0;u<i[c].vertices.length;u++)t.push({key:sn(i[c].vertices[u]),target:l.vertices[u]})}}return t}function Rg(e,n){let t=[];for(let o of e)for(let r of o.textureTriangles??[])for(let s of r.vertices)t.push({key:sn(s),target:gi(s,n)});return t}function yl(e,n){return e.map(t=>{let o=!1,r=a=>{let c=n.get(sn(a));return c?(o=!0,c):a},s=t.vertices.map(r),i=ui(t.textureTriangles,r);return o?{...t,vertices:s,...i?{textureTriangles:i}:{}}:t})}function Vg(e,n,t,o){let r=t[e],s=t[n],i=o[e],a=o[n];if(!i||!a||r.vertices.length!==3||s.vertices.length!==3||!jg(r,s,i,a))return null;let c=di(r,s);if(!c)return null;let[l,u,f,d]=c;if((f+1)%s.vertices.length===d)return null;let p=Math.abs(qe(i.normal,a.normal)),h=Math.cos(Pp*Math.PI/180);if(p<h)return null;let g=(u+1)%r.vertices.length,y=3-f-d,x=[r.vertices[u],r.vertices[g],r.vertices[l],s.vertices[y]],b=Lg(x);if(!b)return null;let P=0,A=0;for(let M of x){let T=Math.abs(pi(M,b));P=Math.max(P,T),A+=T*T}if(P>Mp)return null;let C=x.map(M=>gi(M,b));if(!wg(C,b.normal))return null;let E=fi({vertices:C});if(!E||qe(E.normal,i.normal)<.2||qe(E.normal,a.normal)<.2)return null;let S={vertices:x,color:r.color,...r.doubleSided?{doubleSided:!0}:{},...r.data?{data:{...r.data}}:{}};if(mi(r,s)&&r.uvs&&s.uvs&&r.texture){S.texture=r.texture,r.textureWrap&&(S.textureWrap={...r.textureWrap}),r.textureAlphaMode&&(S.textureAlphaMode=r.textureAlphaMode),S.uvs=[[...r.uvs[u]],[...r.uvs[g]],[...r.uvs[l]],[...s.uvs[y]]];let M=Dg([r,s]);M?.length&&(S.textureTriangles=M)}return{normalDot:p,maxDistance:P,candidate:{a:e,b:n,polygon:S,vertexMoves:[...x.map((M,T)=>({key:sn(M),target:C[T]})),...Rg([r,s],b)],score:A/x.length+P*.25+(1-p)*.1}}}function Lg(e){if(e.length<3)return null;let n=0,t=0,o=0,r=0,s=0,i=0;for(let c=0;c<e.length;c++){let l=e[c],u=e[(c+1)%e.length];n+=(l[1]-u[1])*(l[2]+u[2]),t+=(l[2]-u[2])*(l[0]+u[0]),o+=(l[0]-u[0])*(l[1]+u[1]),r+=l[0],s+=l[1],i+=l[2]}let a=vl([n,t,o]);return a?{normal:a,point:[r/e.length,s/e.length,i/e.length]}:null}function wg(e,n){let t=0;for(let o=0;o<e.length;o++){let r=e[o],s=e[(o+1)%e.length],i=e[(o+2)%e.length],a=qe(Tl(Cn(s,r),Cn(i,s)),n);if(Math.abs(a)<=1e-9)continue;let c=a>0?1:-1;if(t===0)t=c;else if(t!==c)return!1}return!0}function Ng(e,n,t){let o=gl(e,t),r=Gg(o,n);if(r<=0)return o;let s=o.map(m=>{let p=fi(m);return p?{polygon:m,normal:p.normal,area:p.area,materialKey:Ml(m)}:null}),i=Yg(o,s),a=new Set,c=Array(o.length),l=[],u=(m,p)=>{c[m]=p};for(let m=0;m<o.length;m++){let p=s[m];if(a.has(m))continue;if(!p){u(m,o[m]);continue}let h=Hg(m,s,i,a,r,n);for(let y of h)a.add(y);if(h.length<2){u(m,o[m]);continue}let g=kg(h,o,s,i,r,n);l.push(...g.vertexMoves);for(let y of h)u(y,g.polygons.get(y)??o[y])}let f=c.flatMap(m=>m?[m]:[]),d=l.length>0?yl(f,hl(l)):f;return Yo(d)}function Yo(e){let n=Pl(e),t=1e-4;if(n<=0)return e;let o=$g(n),r=Xg(t),s=!1,i=e.map(a=>{let c=!1,l=p=>{let h=o.snap(p);return Xt(h,p)||(c=!0),h},u=a.vertices.map(l),f=a.uvs&&a.uvs.length===a.vertices.length?a.uvs.map(p=>{let h=r.snap(p);return Yt(h,p)||(c=!0),h}):void 0,d=ui(a.textureTriangles,l);if(!c&&!a.texture)return a;let m={...a,vertices:u,...f?{uvs:f}:{},...d?{textureTriangles:d}:{}};return s=!0,{...m,...m.texture?{textureTriangles:xl(m)}:{}}});return s?i:e}function xl(e){if(e.texture){if(e.textureTriangles?.length)return Bg(e.textureTriangles);if(e.uvs&&e.uvs.length===e.vertices.length)return Fg(e.vertices,e.uvs)}}function Dg(e){let n=e.flatMap(t=>xl(t)??[]);return n.length>0?n:void 0}function Fg(e,n){let t=[];for(let o=1;o<e.length-1;o++)t.push({vertices:[[...e[0]],[...e[o]],[...e[o+1]]],uvs:[[...n[0]],[...n[o]],[...n[o+1]]]});return t}function Bg(e){return e.map(n=>({vertices:n.vertices.map(t=>[...t]),uvs:n.uvs.map(t=>[...t])}))}function ui(e,n){if(e?.length)return e.map(t=>({vertices:t.vertices.map(n),uvs:t.uvs.map(o=>[...o])}))}function kg(e,n,t,o,r,s){let i=bl(e,n,t,r,s);return i?zg(i):Ug(e,n,t,o,r,s)}function Ug(e,n,t,o,r,s){let i=new Set(e),a=[];for(let f of e)for(let d of o.get(f)??[]){if(f>=d||!i.has(d))continue;let m=bl([f,d],n,t,r,s);m&&a.push(m)}a.sort((f,d)=>d.score-f.score);let c=new Set,l=new Map,u=[];for(let f of a)if(!f.indices.some(d=>c.has(d))){u.push(...f.vertexMoves);for(let d=0;d<f.indices.length;d++){let m=f.indices[d];c.add(m),l.set(m,n[m])}}return{polygons:l,vertexMoves:u}}function zg(e){let n=new Map;for(let t=0;t<e.indices.length;t++)n.set(e.indices[t],e.source[t]);return{polygons:n,vertexMoves:e.vertexMoves}}function bl(e,n,t,o,r){let s=Sl(e,t);if(!s||!Al(e,t,s,o,r))return null;let i=e.map(u=>n[u]),a=i.map(u=>qg(u,s)),c=Xn(tn(i)),l=Xn(tn(a));return l>=c?null:{indices:e,source:i,projected:a,vertexMoves:Og(i,a),score:c-l}}function Gg(e,n){return Pl(e)<=0?0:n.maxPlaneDisplacement}function Pl(e){let n=1/0,t=1/0,o=1/0,r=-1/0,s=-1/0,i=-1/0;for(let c of e)for(let[l,u,f]of c.vertices)n=Math.min(n,l),t=Math.min(t,u),o=Math.min(o,f),r=Math.max(r,l),s=Math.max(s,u),i=Math.max(i,f);if(!Number.isFinite(n))return 0;let a=Math.hypot(r-n,s-t,i-o);return a<=0?0:Math.min(.025,Math.max(1e-4,a*25e-5))}function $g(e){let n=new Map,t=r=>Math.floor(r/e),o=(r,s,i)=>`${r},${s},${i}`;return{snap(r){let s=t(r[0]),i=t(r[1]),a=t(r[2]);for(let f=-1;f<=1;f++)for(let d=-1;d<=1;d++)for(let m=-1;m<=1;m++){let p=n.get(o(s+f,i+d,a+m));if(p){for(let h of p)if(El(r,h)<=e)return[h[0],h[1],h[2]]}}let c=[r[0],r[1],r[2]],l=o(s,i,a),u=n.get(l);return u?u.push(c):n.set(l,[c]),c}}}function Xg(e){let n=new Map,t=r=>Math.floor(r/e),o=(r,s)=>`${r},${s}`;return{snap(r){let s=t(r[0]),i=t(r[1]);for(let u=-1;u<=1;u++)for(let f=-1;f<=1;f++){let d=n.get(o(s+u,i+f));if(d){for(let m of d)if(Math.hypot(r[0]-m[0],r[1]-m[1])<=e)return[m[0],m[1]]}}let a=[r[0],r[1]],c=o(s,i),l=n.get(c);return l?l.push(a):n.set(c,[a]),a}}}function Ml(e){return[e.color??"#cccccc",e.texture??"",e.texture&&e.textureWrap?`${e.textureWrap.s}/${e.textureWrap.t}`:"",e.texture&&e.textureAlphaMode?e.textureAlphaMode:"",e.uvs?"uv":"plain",e.doubleSided===!0?"double-sided":"single-sided"].join("|")}function fi(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0,s=n[0];for(let a=1;a<n.length-1;a++){let c=Cn(n[a],s),l=Cn(n[a+1],s),u=Tl(c,l);t+=u[0],o+=u[1],r+=u[2]}let i=Math.hypot(t,o,r);return i<=1e-10?null:{normal:[t/i,o/i,r/i],area:i/2}}function Yg(e,n){let t=new Map,o=new Map;for(let r=0;r<e.length;r++){let s=e[r];if(!(!n[r]||s.vertices.length<3))for(let i=0;i<s.vertices.length;i++){let a=Wt(s.vertices[i],s.vertices[(i+1)%s.vertices.length]),c=t.get(a);c?c.push(r):t.set(a,[r])}}for(let r of t.values())for(let s=0;s<r.length;s++)for(let i=s+1;i<r.length;i++){let a=r[s],c=r[i];Wg(e[a],e[c],n[a],n[c])&&(cl(o,a,c),cl(o,c,a))}return o}function Wg(e,n,t,o){if(!t||!o||t.materialKey!==o.materialKey||!!e.uvs!=!!n.uvs)return!1;if(Wo(e)||Wo(n))return mi(e,n);if(!e.uvs||!n.uvs)return!0;let r=di(e,n);if(!r)return!1;let[s,i,a,c]=r;return Yt(e.uvs[s],n.uvs[a])&&Yt(e.uvs[i],n.uvs[c])}function jg(e,n,t,o){return t.materialKey!==o.materialKey?!1:Wo(e)||Wo(n)?mi(e,n):!e.uvs&&!n.uvs&&!e.textureTriangles?.length&&!n.textureTriangles?.length}function Wo(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function mi(e,n){if(!e.texture||!n.texture||e.texture!==n.texture||(e.textureWrap?.s??"")!==(n.textureWrap?.s??"")||(e.textureWrap?.t??"")!==(n.textureWrap?.t??"")||(e.textureAlphaMode??"")!==(n.textureAlphaMode??"")||e.material?.texture||n.material?.texture||!e.uvs||!n.uvs||e.uvs.length!==e.vertices.length||n.uvs.length!==n.vertices.length)return!1;let t=di(e,n);if(!t)return!1;let[o,r,s,i]=t;return Yt(e.uvs[o],n.uvs[s])&&Yt(e.uvs[r],n.uvs[i])}function cl(e,n,t){let o=e.get(n);o?o.add(t):e.set(n,new Set([t]))}function Hg(e,n,t,o,r,s){let i=[e],a=new Set([e]),c=[e];for(;c.length>0;){let l=c.shift();for(let u of t.get(l)??[]){if(o.has(u)||a.has(u))continue;let f=n[u],d=n[e];!f||!d||f.materialKey===d.materialKey&&Kg([...i,u],n,r,s)&&(i.push(u),a.add(u),c.push(u))}}return i}function Kg(e,n,t,o){let r=Sl(e,n);return!!r&&Al(e,n,r,t,o)}function Sl(e,n){let t=n[e[0]];if(!t)return null;let o=0,r=0,s=0,i=0,a=0,c=0,l=0;for(let m of e){let p=n[m];if(!p)return null;let h=qe(t.normal,p.normal)<0?-1:1,g=Math.max(p.area,1e-6);o+=p.normal[0]*h*g,r+=p.normal[1]*h*g,s+=p.normal[2]*h*g;for(let y of p.polygon.vertices)i+=y[0],a+=y[1],c+=y[2],l+=1}let u=vl([o,r,s]);if(!u||l===0)return null;let f=Cl(e,n),d=Zg(e,n,u,f);if(d){let m=(d.min+d.max)/2;return{normal:u,point:[u[0]*m,u[1]*m,u[2]*m],boundaryVertexKeys:f}}return{normal:u,point:[i/l,a/l,c/l],boundaryVertexKeys:f}}function Zg(e,n,t,o){let r=1/0,s=-1/0;for(let i of e){let a=n[i];if(a)for(let c of a.polygon.vertices){if(!o.has(sn(c)))continue;let l=qe(c,t);r=Math.min(r,l),s=Math.max(s,l)}}return Number.isFinite(r)&&Number.isFinite(s)?{min:r,max:s}:null}function Al(e,n,t,o,r){let s=Math.cos(r.maxAngleDeg*Math.PI/180),i=t.boundaryVertexKeys??Cl(e,n);for(let a of e){let c=n[a];if(!c||Math.abs(qe(c.normal,t.normal))<s)return!1;for(let l of c.polygon.vertices){let u=i.has(sn(l))?r.maxBoundaryDisplacement:o;if(Math.abs(pi(l,t))>u)return!1}}return!0}function Cl(e,n){let t=new Map;for(let r of e){let s=n[r];if(!s)continue;let i=s.polygon.vertices;for(let a=0;a<i.length;a++){let c=i[a],l=i[(a+1)%i.length],u=Wt(c,l),f=t.get(u);f?f.count+=1:t.set(u,{count:1,a:c,b:l})}}let o=new Set;for(let r of t.values())r.count===1&&(o.add(sn(r.a)),o.add(sn(r.b)));return o}function qg(e,n){let t=r=>gi(r,n),o=ui(e.textureTriangles,t);return{...e,vertices:e.vertices.map(t),...o?{textureTriangles:o}:{}}}function di(e,n){for(let t=0;t<e.vertices.length;t++){let o=(t+1)%e.vertices.length;for(let r=0;r<n.vertices.length;r++){let s=(r+1)%n.vertices.length;if(Xt(e.vertices[t],n.vertices[r])&&Xt(e.vertices[o],n.vertices[s]))return[t,o,r,s];if(Xt(e.vertices[t],n.vertices[s])&&Xt(e.vertices[o],n.vertices[r]))return[t,o,s,r]}}return null}function Wt(e,n){let t=sn(e),o=sn(n);return t<o?`${t}|${o}`:`${o}|${t}`}function sn(e){return`${e[0]},${e[1]},${e[2]}`}function Xt(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function Yt(e,n){return Math.abs(e[0]-n[0])<=1e-4&&Math.abs(e[1]-n[1])<=1e-4}function Cn(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Tl(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 qe(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function El(e,n){return Math.hypot(e[0]-n[0],e[1]-n[1],e[2]-n[2])}function vl(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 pi(e,n){return qe(Cn(e,n.point),n.normal)}function gi(e,n){let t=pi(e,n);return[e[0]-n.normal[0]*t,e[1]-n.normal[1]*t,e[2]-n.normal[2]*t]}var Qg=36,Jg=.15,e0=.2,n0=650,t0=15e3,o0=.25;function hi(e){return!!(e.texture||e.material?.texture||e.uvs?.length||e.textureTriangles?.length)}function Il(e){let n=Pn(e);return!n||n.alpha<1?e.trim().toLowerCase():`#${n.rgb.map(t=>Math.max(0,Math.min(255,Math.round(t))).toString(16).padStart(2,"0")).join("")}`}function r0(e){let n=Pn(e);return n?{rgb:[Math.max(0,Math.min(255,Math.round(n.rgb[0]))),Math.max(0,Math.min(255,Math.round(n.rgb[1]))),Math.max(0,Math.min(255,Math.round(n.rgb[2])))],alpha:n.alpha}:null}function s0(e,n){return Math.hypot(e.rgb[0]-n.rgb[0],e.rgb[1]-n.rgb[1],e.rgb[2]-n.rgb[2])}function _l(e){let n=e.rgb[0]/255,t=e.rgb[1]/255,o=e.rgb[2]/255,r=Math.max(n,t,o),s=Math.min(n,t,o),i=r-s,a=0;return i!==0&&(r===n?a=60*((t-o)/i%6):r===t?a=60*((o-n)/i+2):a=60*((n-t)/i+4)),a<0&&(a+=360),{hue:a,saturation:r===0?0:i/r,value:r}}function i0(e,n){let t=Math.abs(e-n)%360;return t>180?360-t:t}function a0(e,n){if(e.alpha<1||n.alpha<1)return!1;let t=_l(e),o=_l(n),r=t.saturation<.08,s=o.saturation<.08;if(r||s)return r===s;let i=Math.min(t.value,o.value)<.18?32:18;return i0(t.hue,o.hue)<=i}function c0(e,n){let t=new Map;for(let i of e.keys()){let a=r0(i);a&&t.set(i,a)}let o=[],r=new Map,s=Array.from(e.entries()).sort((i,a)=>a[1]-i[1]||i[0].localeCompare(a[0]));for(let[i]of s){let a=t.get(i);if(!a){r.set(i,i);continue}let c=null;for(let l of o){if(!a0(a,l.parsed))continue;let u=s0(a,l.parsed);u>n||(!c||u<c.distance)&&(c={color:l.color,distance:u})}c?r.set(i,c.color):(o.push({color:i,parsed:a}),r.set(i,i))}return r}function l0(e,n,t){if(t!=="lossy"||e.animation)return e;let o=n.source;if(!o||o.polygons===e.polygons)return e;let r=n.bakedTextureColorMergeDistance??Qg;if(!Number.isFinite(r)||r<=0)return e;let s=new Map,i=[];for(let u=0;u<e.polygons.length;u+=1){let f=o.polygons[u],d=e.polygons[u];if(!f||!d||f===d||!d.color||!hi(f)||hi(d))continue;let m=Il(d.color);i.push(u),s.set(m,(s.get(m)??0)+1)}if(s.size<2)return e;let a=c0(s,r),c=!1,l=e.polygons.slice();for(let u of i){let f=l[u],d=Il(f.color),m=a.get(d)??d;m!==d&&(l[u]={...f,color:m},c=!0)}return c?{...e,polygons:l}:e}function u0(e){let n=0;for(let t of e)hi(t)&&(n+=1);return e.length===0||n/e.length<=o0}function f0(e){return!!e.metadata?.stlTopology}function m0(e,n){return n?Math.max(e.simplifyEarlyStopDropRatio??Jg,e0):e.simplifyEarlyStopDropRatio}function Yn(e,n={}){return new yi(e,n).optimize()}var yi=class{constructor(n,t){this.result=n,this.options=t,this.meshResolution=t.meshResolution??"lossy"}optimize(){if(this.result.voxelSource||this.result.animation)return this.result;let n=l0(this.result,this.options,this.meshResolution),t=f0(n),o=t?"lossless":this.meshResolution,r=this.canSimplify(n.polygons,o),s=r&&n.polygons.length>=n0&&n.polygons.length<=t0,i=ul(n.polygons,{meshResolution:o,useCandidateFirst:s,staticSimplification:r?{simplifyTriangleMeshOptions:this.options.simplifyTriangleMeshOptions,earlyStopDropRatio:m0(this.options,s)}:!1,skipInteriorCull:t});return i===n.polygons?n:{...n,polygons:i}}canSimplify(n,t){return t==="lossy"&&this.options.simplifyTriangleMeshes!==!1&&u0(n)}};function d0(e){let n=e.vertices;if(!n||n.length<3)return null;let t=0,o=0,r=0;for(let[_,O,R]of n)t+=_,o+=O,r+=R;let s=1/n.length;t*=s,o*=s,r*=s;let i=n[0],a=n[1],c=n[2],l=a[0]-i[0],u=a[1]-i[1],f=a[2]-i[2],d=c[0]-i[0],m=c[1]-i[1],p=c[2]-i[2],h=u*p-f*m,g=f*d-l*p,y=l*m-u*d,x=Math.hypot(h,g,y);if(x<1e-9)return null;h/=x,g/=x,y/=x;let b=n.length-2,P=new Float64Array(b*9),A=0;for(let _=1;_<n.length-1;_++){let O=n[0],R=n[_],w=n[_+1];P[A++]=O[0],P[A++]=O[1],P[A++]=O[2],P[A++]=R[0],P[A++]=R[1],P[A++]=R[2],P[A++]=w[0],P[A++]=w[1],P[A++]=w[2]}let C=0,E=1/0,S=1/0,M=1/0,T=-1/0,v=-1/0,I=-1/0;for(let[_,O,R]of n){let w=_-t,F=O-o,D=R-r,K=w*w+F*F+D*D;K>C&&(C=K),_<E&&(E=_),_>T&&(T=_),O<S&&(S=O),O>v&&(v=O),R<M&&(M=R),R>I&&(I=R)}return{centroid:[t,o,r],normal:[h,g,y],triFlat:P,bcx:t,bcy:o,bcz:r,br2:C,minX:E,minY:S,minZ:M,maxX:T,maxY:v,maxZ:I}}function p0(e,n,t,o,r,s,i,a){let c=i[a],l=i[a+1],u=i[a+2],f=i[a+3]-c,d=i[a+4]-l,m=i[a+5]-u,p=i[a+6]-c,h=i[a+7]-l,g=i[a+8]-u,y=r*g-s*h,x=s*p-o*g,b=o*h-r*p,P=f*y+d*x+m*b;if(P>-1e-9&&P<1e-9)return!1;let A=1/P,C=e-c,E=n-l,S=t-u,M=A*(C*y+E*x+S*b);if(M<0||M>1)return!1;let T=E*m-S*d,v=S*f-C*m,I=C*d-E*f,_=A*(o*T+r*v+s*I);return _<0||M+_>1?!1:A*(p*T+h*v+g*I)>.001}function g0(e,n,t,o,r,s,i){let a=i.bcx-e,c=i.bcy-n,l=i.bcz-t,u=a*o+c*r+l*s,f=a-u*o,d=c-u*r,m=l-u*s;if(f*f+d*d+m*m>i.br2)return!1;let p=i.triFlat,h=p.length;for(let g=0;g<h;g+=9)if(p0(e,n,t,o,r,s,p,g))return!0;return!1}var jt=9,h0=6,Ge=12;function xi(e,n,t,o,r,s){let i=o-e,a=r-n,c=s-t;return i*a+a*c+c*i}function y0(e){let n=[];for(let C=0;C<e.length;C++)e[C]&&n.push(C);let t=n.length,o=new Int32Array(t);for(let C=0;C<t;C++)o[C]=n[C];let r=new Float64Array(t),s=new Float64Array(t),i=new Float64Array(t);for(let C=0;C<t;C++){let E=e[o[C]];r[C]=(E.minX+E.maxX)*.5,s[C]=(E.minY+E.maxY)*.5,i[C]=(E.minZ+E.maxZ)*.5}let a=2*Math.max(1,t)+1,c=new Float64Array(a*jt),l=0,u=new Float64Array(Ge),f=new Float64Array(Ge),d=new Float64Array(Ge),m=new Float64Array(Ge),p=new Float64Array(Ge),h=new Float64Array(Ge),g=new Int32Array(Ge),y=new Float64Array(Ge-1),x=new Int32Array(Ge-1),b=new Float64Array(Ge-1),P=new Int32Array(Ge-1);function A(C,E){let S=l++,M=S*jt,T=E-C,v=1/0,I=1/0,_=1/0,O=-1/0,R=-1/0,w=-1/0;for(let z=C;z<E;z++){let te=e[o[z]];te.minX<v&&(v=te.minX),te.maxX>O&&(O=te.maxX),te.minY<I&&(I=te.minY),te.maxY>R&&(R=te.maxY),te.minZ<_&&(_=te.minZ),te.maxZ>w&&(w=te.maxZ)}if(c[M]=v,c[M+1]=I,c[M+2]=_,c[M+3]=O,c[M+4]=R,c[M+5]=w,T<=h0)return c[M+6]=1,c[M+7]=C,c[M+8]=E,S;let F=1/0,D=1/0,K=1/0,H=-1/0,Q=-1/0,ne=-1/0;for(let z=C;z<E;z++)r[z]<F&&(F=r[z]),r[z]>H&&(H=r[z]),s[z]<D&&(D=s[z]),s[z]>Q&&(Q=s[z]),i[z]<K&&(K=i[z]),i[z]>ne&&(ne=i[z]);let ee=H-F,ue=Q-D,U=ne-K;if(ee===0&&ue===0&&U===0)return c[M+6]=1,c[M+7]=C,c[M+8]=E,S;let $=xi(v,I,_,O,R,w),k=$>0?1/$:0,L=T+1,W=0,Y=0;for(let z=0;z<3;z++){let te=z===0?F:z===1?D:K,ce=z===0?ee:z===1?ue:U;if(ce===0)continue;let ye=z===0?r:z===1?s:i,he=Ge/ce;u.fill(1/0),f.fill(1/0),d.fill(1/0),m.fill(-1/0),p.fill(-1/0),h.fill(-1/0),g.fill(0);for(let N=C;N<E;N++){let X=(ye[N]-te)*he|0;X>=Ge&&(X=Ge-1);let oe=e[o[N]];oe.minX<u[X]&&(u[X]=oe.minX),oe.maxX>m[X]&&(m[X]=oe.maxX),oe.minY<f[X]&&(f[X]=oe.minY),oe.maxY>p[X]&&(p[X]=oe.maxY),oe.minZ<d[X]&&(d[X]=oe.minZ),oe.maxZ>h[X]&&(h[X]=oe.maxZ),g[X]++}let xe=1/0,ge=1/0,Ae=1/0,Fe=-1/0,V=-1/0,G=-1/0,j=0;for(let N=0;N<Ge-1;N++)u[N]<xe&&(xe=u[N]),m[N]>Fe&&(Fe=m[N]),f[N]<ge&&(ge=f[N]),p[N]>V&&(V=p[N]),d[N]<Ae&&(Ae=d[N]),h[N]>G&&(G=h[N]),j+=g[N],y[N]=xi(xe,ge,Ae,Fe,V,G),x[N]=j;let J=1/0,le=1/0,re=1/0,pe=-1/0,be=-1/0,ie=-1/0,ae=0;for(let N=Ge-2;N>=0;N--){let X=N+1;u[X]<J&&(J=u[X]),m[X]>pe&&(pe=m[X]),f[X]<le&&(le=f[X]),p[X]>be&&(be=p[X]),d[X]<re&&(re=d[X]),h[X]>ie&&(ie=h[X]),ae+=g[X],b[N]=xi(J,le,re,pe,be,ie),P[N]=ae}for(let N=0;N<Ge-1;N++){if(x[N]===0||P[N]===0)continue;let X=.125+(y[N]*x[N]+b[N]*P[N])*k;X<L&&(L=X,W=z,Y=te+(N+1)/he)}}let se=W===0?r:W===1?s:i,Z=C,B=E-1;for(;Z<=B;)if(se[Z]<Y)Z++;else{let z=o[Z];o[Z]=o[B],o[B]=z;let te=r[Z];r[Z]=r[B],r[B]=te;let ce=s[Z];s[Z]=s[B],s[B]=ce;let ye=i[Z];i[Z]=i[B],i[B]=ye,B--}let q=Z;(q===C||q===E)&&(q=C+E>>1),c[M+6]=0;let me=A(C,q),fe=A(q,E);return c[S*jt+7]=me,c[S*jt+8]=fe,S}return t>0&&A(0,t),{data:c,nodeCount:l,polyIndices:o,meta:e}}function x0(e,n,t,o,r,s,i,a,c){if(a.nodeCount===0)return!1;let{data:l,polyIndices:u,meta:f}=a,d=o!==0?1/o:o>=0?1/0:-1/0,m=r!==0?1/r:r>=0?1/0:-1/0,p=s!==0?1/s:s>=0?1/0:-1/0,h=0;for(c[h++]=0;h>0;){let y=c[--h]*jt,x=(l[y]-e)*d,b=(l[y+3]-e)*d,P=x<b?x:b,A=x<b?b:x,C=(l[y+1]-n)*m,E=(l[y+4]-n)*m,S=C<E?C:E,M=C<E?E:C;if(P>M||S>A)continue;S>P&&(P=S),M<A&&(A=M);let T=(l[y+2]-t)*p,v=(l[y+5]-t)*p,I=T<v?T:v,_=T<v?v:T;if(!(P>_||I>A)&&(_<A&&(A=_),!(A<.001)))if(l[y+6]===1){let O=l[y+7]|0,R=l[y+8]|0;for(let w=O;w<R;w++){let F=u[w];if(F===i)continue;let D=f[F];if(D&&g0(e,n,t,o,r,s,D))return!0}}else c[h++]=l[y+7]|0,c[h++]=l[y+8]|0}return!1}function Ol(e,n,t){let o=new Set;if(e.length<2)return o;let r=Math.hypot(n[0],n[1],n[2]);if(r<1e-9)return o;let s=n[0]/r,i=n[1]/r,a=n[2]/r,c=e.map(d0);if(t&&t.size>0)for(let f of t)c[f]=null;let l=y0(c),u=new Int32Array(Math.max(64,l.nodeCount));for(let f=0;f<e.length;f++){let d=c[f];if(!d)continue;let m=d.centroid[0]+d.normal[0]*.001,p=d.centroid[1]+d.normal[1]*.001,h=d.centroid[2]+d.normal[2]*.001;d.normal[0]*s+d.normal[1]*i+d.normal[2]*a<=0||x0(m,p,h,s,i,a,f,l,u)&&o.add(f)}return o}var Ht=1e-5,bi=.001,Pi=6;function jo(e){let n=e.vertices;if(n.length<3)return null;let t=n[0],o=0,r=0,s=0;for(let a=1;a+1<n.length;a++){let c=n[a],l=n[a+1],u=c[1]-t[1],f=c[0]-t[0],d=c[2]-t[2],m=l[1]-t[1],p=l[0]-t[0],h=l[2]-t[2];o-=f*h-d*p,r-=d*m-u*h,s-=u*p-f*m}let i=Math.hypot(o,r,s);return i<1e-9?null:[o/i,r/i,s/i]}function Mi(e,n){let t=n.meshRotation,o=t?On(e,t):e;return Co(o,n.rotX,0,n.rotY)[2]}function gt(e,n,t=Ht){return Mi(e,n)>t}function Rl(e,n,t=Ht){let o=jo(e);return o===null||gt(o,n,t)}function Si(e){return`${e[0].toFixed(4)},${e[1].toFixed(4)},${e[2].toFixed(4)}`}function Ai(e){let n=new Map;for(let t of e){if(!t)continue;let o=Si(t);n.has(o)||n.set(o,t)}return Array.from(n,([t,o])=>({key:t,normal:o}))}function Vl(e){return Ai(e.map(jo))}function Ci(e,n=bi){let t=Math.abs(e[0]),o=Math.abs(e[1]),r=Math.abs(e[2]),s=Math.max(t,o,r);return s>1-n&&t+o+r-s<n}function Ll(e){return e.length<=Pi&&e.every(({normal:n})=>Ci(n))}function wl(e,n,t=Ht){let o=[];for(let{key:r,normal:s}of e)gt(s,n,t)&&o.push(r);return o.sort(),o.join("|")}function Ti(e,n,t,o,r){let s=(p,h,g)=>{let y=[0,0,0];return y[e]=p,y[(e+1)%3]=h,y[(e+2)%3]=g,y},i=s(n,-o,-o),a=s(n,o,-o),c=s(n,o,o),l=s(n,-o,o),u=s(t,-o,-o),f=s(t,o,-o),d=s(t,o,o),m=s(t,-o,o);return[{vertices:[i,a,c,l],color:r},{vertices:[u,f,d,m],color:r},{vertices:[i,a,f,u],color:r},{vertices:[a,c,d,f],color:r},{vertices:[c,l,m,d],color:r},{vertices:[l,i,u,m],color:r}]}function Ei(e={}){let n=e.size??5,t=e.thickness??.025,o=e.negative??!1,r=e.xColor??"#ff3a3a",s=e.yColor??"#3aff3a",i=e.zColor??"#3a8aff",a=n*t/2,c=o?-n:0;return[...Ti(0,c,n,a,r),...Ti(1,c,n,a,s),...Ti(2,c,n,a,i)]}var b0=["right","left","front","back","top","bottom"];function vi(e){return[e[0],e[1],e[2]]}function Nl(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 P0(e){if(e.min&&e.max)return Nl(e.min,e.max);let n=e.center?vi(e.center):[0,0,0],t;typeof e.size=="number"?t=[e.size,e.size,e.size]:e.size?t=vi(e.size):t=[1,1,1];let o=[t[0]/2,t[1]/2,t[2]/2];return Nl([n[0]-o[0],n[1]-o[1],n[2]-o[2]],[n[0]+o[0],n[1]+o[1],n[2]+o[2]])}function M0(e,n){let[t,o,r]=e,[s,i,a]=n;return[{name:"right",vertices:[[s,o,r],[s,i,r],[s,i,a],[s,o,a]]},{name:"left",vertices:[[t,i,r],[t,o,r],[t,o,a],[t,i,a]]},{name:"front",vertices:[[s,i,r],[t,i,r],[t,i,a],[s,i,a]]},{name:"back",vertices:[[t,o,r],[s,o,r],[s,o,a],[t,o,a]]},{name:"top",vertices:[[t,o,a],[s,o,a],[s,i,a],[t,i,a]]},{name:"bottom",vertices:[[t,i,r],[s,i,r],[s,o,r],[t,o,r]]}]}function S0(e,n){let t={...e,...n};return(e.data!==void 0||n?.data!==void 0)&&(t.data={...e.data??{},...n?.data??{}}),t}function A0(e,n){let t={vertices:e.vertices.map(vi)};return n.color!==void 0&&(t.color=n.color),n.texture!==void 0&&(t.texture=n.texture),n.material!==void 0&&(t.material=n.material),n.uvs!==void 0&&(t.uvs=n.uvs.map(o=>[o[0],o[1]])),n.data!==void 0&&(t.data={...n.data}),t}function Ii(e={}){let{min:n,max:t}=P0(e);if(n[0]===t[0]||n[1]===t[1]||n[2]===t[2])return[];let o={color:e.color??"#ffffff",texture:e.texture,material:e.material,uvs:e.uvs,data:e.data},r=M0(n,t),s=new Map(r.map(a=>[a.name,a])),i=[];for(let a of b0){let c=e.faces?.[a];if(c===!1)continue;let l=s.get(a);l&&i.push(A0(l,S0(o,c)))}return i}function Dl(e,n,t,o){let r=[0,0,0];return r[e]=n,r[(e+1)%3]=t,r[(e+2)%3]=o,r}function C0(e,n,t,o,r){let s=(p,h,g)=>Dl(e,p,h,g),i=s(n,-o,-o),a=s(n,o,-o),c=s(n,o,o),l=s(n,-o,o),u=s(t,-o,-o),f=s(t,o,-o),d=s(t,o,o),m=s(t,-o,o);return[{vertices:[i,a,c,l],color:r},{vertices:[u,f,d,m],color:r},{vertices:[i,a,f,u],color:r},{vertices:[a,c,d,f],color:r},{vertices:[c,l,m,d],color:r},{vertices:[l,i,u,m],color:r}]}function T0(e,n,t,o,r){let s=(f,d,m)=>Dl(e,f,d,m),i=s(n,-o,-o),a=s(n,o,-o),c=s(n,o,o),l=s(n,-o,o),u=s(t,0,0);return[{vertices:[i,a,c,l],color:r},{vertices:[i,a,u],color:r},{vertices:[a,c,u],color:r},{vertices:[c,l,u],color:r},{vertices:[l,i,u],color:r}]}function E0(e){return e.map(n=>({...n,vertices:[...n.vertices].reverse()}))}function _i(e){let n=e.axis,t=e.sign??1,o=e.shaftLength??4,r=e.shaftHalfThickness??.05,s=e.headLength??.8,i=e.headHalfThickness??.2,a=e.color??"#ffffff",c=e.shaft??!0,l=o*t,u=Math.min(0,l),f=Math.max(0,l),d=l,m=(o+s)*t,p=T0(n,d,m,i,a),h=t===-1?E0(p):p;return c?[...C0(n,u,f,r,a),...h]:h}function Ho(e,n,t){let o=[0,0,0],r=(e+1)%3,s=(e+2)%3;return o[r]=Math.cos(t)*n,o[s]=Math.sin(t)*n,o}function Oi(e){let n=e.axis,t=e.radius,o=e.halfThickness??Math.max(.05,t*.04),r=e.segments??32,s=e.color??"#ffffff",i=t-o,a=t+o,c=[];for(let l=0;l<r;l++){let u=l/r*Math.PI*2,f=(l+1)/r*Math.PI*2,d=Ho(n,i,u),m=Ho(n,i,f),p=Ho(n,a,u),h=Ho(n,a,f);c.push({vertices:[d,p,h,m],color:s})}return c}function Ri(e){let n=e.axis,t=e.outerRadius,o=e.color??"#ffffff",r=(n+1)%3,s=(n+2)%3,i=(a,c)=>{let l=[0,0,0];return l[n]=0,l[r]=a,l[s]=c,l};return[{vertices:[i(-t,-t),i(t,-t),i(t,t),i(-t,t)],color:o}]}function Vi(e){let n=e.axis,t=e.size??.4,o=e.offset??t*2,r=typeof o=="number"?o:o[0],s=typeof o=="number"?o:o[1],i=e.along??0,a=e.color??"#ffffff",c=(n+1)%3,l=(n+2)%3,u=(f,d)=>{let m=[0,0,0];return m[n]=i,m[c]=r+f,m[l]=s+d,m};return[{vertices:[u(-t,-t),u(t,-t),u(t,t),u(-t,t)],color:a}]}function Li(e){let{center:n,size:t,color:o="#ffffff"}=e,[r,s,i]=n,a=[[r+t,s,i],[r-t,s,i],[r,s+t,i],[r,s-t,i],[r,s,i+t],[r,s,i-t]];return[[0,2,4],[2,1,4],[1,3,4],[3,0,4],[2,0,5],[1,2,5],[3,1,5],[0,3,5]].map(l=>({vertices:[a[l[0]],a[l[1]],a[l[2]]],color:o}))}function wi(e={}){let{radius:n=50,color:t="#cccccc"}=e,o=Math.min(Math.max(0,Math.floor(e.subdivisions??1)),3),r=(1+Math.sqrt(5))/2,s=[[-1,r,0],[1,r,0],[-1,-r,0],[1,-r,0],[0,-1,r],[0,1,r],[0,-1,-r],[0,1,-r],[r,0,-1],[r,0,1],[-r,0,-1],[-r,0,1]].map(([c,l,u])=>{let f=Math.sqrt(c*c+l*l+u*u);return[c/f,l/f,u/f]}),i=[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]];for(let c=0;c<o;c++){let l=[...s],u=[],f=new Map,d=(m,p)=>{let h=m<p?`${m}-${p}`:`${p}-${m}`,g=f.get(h);if(g!==void 0)return g;let y=s[m],x=s[p],b=(y[0]+x[0])/2,P=(y[1]+x[1])/2,A=(y[2]+x[2])/2,C=Math.sqrt(b*b+P*P+A*A);b/=C,P/=C,A/=C;let E=l.length;return l.push([b,P,A]),f.set(h,E),E};for(let[m,p,h]of i){let g=d(m,p),y=d(p,h),x=d(h,m);u.push([m,g,x]),u.push([p,y,g]),u.push([h,x,y]),u.push([g,y,x])}s=l,i=u}let a=s.map(([c,l,u])=>[c*n,l*n,u*n]);return i.map(([c,l,u])=>({vertices:[a[c],a[l],a[u]],color:t}))}function Ni(e={}){let{size:n=100,color:t="#cccccc"}=e,o=n/Math.sqrt(3),r=[[o,o,o],[o,-o,-o],[-o,o,-o],[-o,-o,o]];return[[0,1,2],[0,3,1],[0,2,3],[1,3,2]].map(([i,a,c])=>({vertices:[r[i],r[a],r[c]],color:t}))}function Di(e={}){let{size:n=100,color:t="#cccccc"}=e,o=(1+Math.sqrt(5))/2,r=n/Math.sqrt(1+o*o),s=[[-1,o,0],[1,o,0],[-1,-o,0],[1,-o,0],[0,-1,o],[0,1,o],[0,-1,-o],[0,1,-o],[o,0,-1],[o,0,1],[-o,0,-1],[-o,0,1]].map(([a,c,l])=>[a*r,c*r,l*r]);return[[0,11,5],[0,5,1],[0,1,7],[0,7,10],[0,10,11],[1,5,9],[5,11,4],[11,10,2],[10,7,6],[7,1,8],[3,9,4],[3,4,2],[3,2,6],[3,6,8],[3,8,9],[4,9,5],[2,4,11],[6,2,10],[8,6,7],[9,8,1]].map(([a,c,l])=>({vertices:[s[a],s[c],s[l]],color:t}))}function Fi(e={}){let{size:n=100,color:t="#cccccc"}=e,o=(1+Math.sqrt(5))/2,r=1/o,s=n/Math.sqrt(3),a=[[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1],[0,r,o],[0,-r,o],[0,r,-o],[0,-r,-o],[r,o,0],[-r,o,0],[r,-o,0],[-r,-o,0],[o,0,r],[o,0,-r],[-o,0,r],[-o,0,-r]].map(([l,u,f])=>[l*s,u*s,f*s]);return[[12,13,4,8,0],[0,8,9,2,16],[16,17,1,12,0],[1,10,5,13,12],[17,3,11,10,1],[2,9,6,15,14],[2,14,3,17,16],[14,15,7,11,3],[18,6,9,8,4],[4,13,5,19,18],[5,10,11,7,19],[18,19,7,15,6]].map(l=>({vertices:l.map(u=>a[u]),color:t}))}function Kt(e={}){let{radius:n=50,height:t=100,radialSegments:o=12,color:r="#cccccc"}=e,s=e.radiusTop??n,i=t/2,a=-i,c=i,l=Math.max(3,o),u=[],f=Array.from({length:l+1},(p,h)=>h/l*Math.PI*2),d=n<=1e-6,m=s<=1e-6;for(let p=0;p<l&&!(d&&m);p++){let h=f[p],g=f[p+1],y=Math.cos(h)*n,x=Math.sin(h)*n,b=Math.cos(g)*n,P=Math.sin(g)*n,A=Math.cos(h)*s,C=Math.sin(h)*s,E=Math.cos(g)*s,S=Math.sin(g)*s,M=[y,x,a],T=[b,P,a],v=[E,S,c],I=[A,C,c];if(m){let _=[0,0,c];u.push({vertices:[M,T,_],color:r})}else if(d){let _=[0,0,a];u.push({vertices:[_,v,I],color:r})}else u.push({vertices:[M,T,v,I],color:r})}if(n>1e-6){let p=[0,0,a];for(let h=0;h<l;h++){let g=f[h],y=f[h+1],x=[Math.cos(g)*n,Math.sin(g)*n,a],b=[Math.cos(y)*n,Math.sin(y)*n,a];u.push({vertices:[[...p],[...b],[...x]],color:r})}}if(s>1e-6){let p=[0,0,c];for(let h=0;h<l;h++){let g=f[h],y=f[h+1],x=[Math.cos(g)*s,Math.sin(g)*s,c],b=[Math.cos(y)*s,Math.sin(y)*s,c];u.push({vertices:[[...p],[...x],[...b]],color:r})}}return u}function Bi(e={}){return Kt({...e,radiusTop:0})}function Ko(e,n,t,o){let r=Math.sin(e),s=Math.cos(e),i=Math.sin(n),a=Math.cos(n);return[(t+o*a)*s,(t+o*a)*r,o*i]}function ki(e={}){let{radius:n=50,tube:t=15,radialSegments:o=12,tubularSegments:r=16,color:s="#cccccc"}=e,i=Math.max(3,o),a=Math.max(3,r),c=[];for(let l=0;l<i;l++){let u=l/i*Math.PI*2,f=(l+1)/i*Math.PI*2;for(let d=0;d<a;d++){let m=d/a*Math.PI*2,p=(d+1)/a*Math.PI*2,h=Ko(u,m,n,t),g=Ko(f,m,n,t),y=Ko(f,p,n,t),x=Ko(u,p,n,t);c.push({vertices:[h,g,y,x],color:s})}}return c}var Fl=.01,Bl=.01;function kl(e,n){let t=Math.hypot(e[0],e[1],e[2])||1,o=e[0]/t,r=e[1]/t,s=e[2]/t,i=Math.sign(s||1)*Math.max(Math.abs(s),.01),a=n,c=.01;return[1,0,0,0,0,1,0,0,-o/i,-r/i,c,0,a*o/i,a*r/i,a*(1-c),1]}function Ul(e,n){let t=Math.hypot(n[0],n[1],n[2])||1,o=n[0]/t,r=n[1]/t,s=n[2]/t;return e[0]*o+e[1]*r+e[2]*s>0}function Zo(e){if(e.length<=1)return e.map(i=>[i[0],i[1]]);let t=e.map(i=>[i[0],i[1]]);t.sort((i,a)=>i[0]-a[0]||i[1]-a[1]);let o=(i,a,c)=>(a[0]-i[0])*(c[1]-i[1])-(a[1]-i[1])*(c[0]-i[0]),r=[];for(let i of t){for(;r.length>=2&&o(r[r.length-2],r[r.length-1],i)<=0;)r.pop();r.push(i)}let s=[];for(let i=t.length-1;i>=0;i--){let a=t[i];for(;s.length>=2&&o(s[s.length-2],s[s.length-1],a)<=0;)s.pop();s.push(a)}return r.pop(),s.pop(),r.concat(s)}function Ui(e){let n=0,t=e.length;for(let o=0;o<t;o++){let r=e[o],s=e[(o+1)%t];n+=r[0]*s[1]-s[0]*r[1]}return n/2}function mn(e){let n=e.map(t=>[t[0],t[1]]);return Ui(n)<0&&n.reverse(),n}function zl(e,n,t){let o=Math.hypot(n[0],n[1],n[2])||1,r=n[0]/o,s=n[1]/o,i=n[2]/o,a=Math.sign(i||1)*Math.max(Math.abs(i),.01),c=e[2]-t;return[e[0]-r/a*c,e[1]-s/a*c]}function Tn(e,n){if(e.length===0||n.length<3)return[];let t=e.map(r=>[r[0],r[1]]),o=n.length;for(let r=0;r<o;r++){if(t.length===0)return[];let s=t;t=[];let i=n[r],a=n[(r+1)%o],c=f=>(a[0]-i[0])*(f[1]-i[1])-(a[1]-i[1])*(f[0]-i[0])>=0,l=(f,d)=>{let m=i[0],p=i[1],h=a[0],g=a[1],y=f[0],x=f[1],b=d[0],P=d[1],A=(m-h)*(x-P)-(p-g)*(y-b);if(Math.abs(A)<1e-12)return[f[0],f[1]];let C=((m-y)*(x-P)-(p-x)*(y-b))/A;return[m+C*(h-m),p+C*(g-p)]},u=s.length;for(let f=0;f<u;f++){let d=s[f],m=s[(f+u-1)%u],p=c(d),h=c(m);p?(h||t.push(l(m,d)),t.push([d[0],d[1]])):h&&t.push(l(m,d))}}return t}function qo(e){if(!Number.isFinite(e)||e<=0)throw new Error("PolyCSS world unit size must be a positive finite number.");return e}function zi(e,n=50){return e*qo(n)}function Gi(e,n=50){return e/qo(n)}function Qe(e,n=50){let t=qo(n);return[e[1]*t,e[0]*t,e[2]*t]}function $i(e,n=50){let t=qo(n);return[e[1]/t,e[0]/t,e[2]/t]}function Qo(e){return[e[1],e[0],e[2]]}function Xi(e){return e?.direction?{...e,direction:Qo(e.direction)}:e}var Gl=zi,$l=Gi,Xl=Qe,Yl=$i,Wl=Qo,jl=Xi;function Jo(e){return e==null?[1,1,1]:typeof e=="number"?[e,e,e]:[e[0]??1,e[1]??1,e[2]??1]}function ht(e){let n=Jo(e),t=n[0]===1&&n[1]===1&&n[2]===1;return(o,r)=>{let s=Qe(r),i=o[1]*50,a=o[0]*50,c=o[2]*50,l=t?i:i*n[0],u=t?a:a*n[1],f=t?c:c*n[2];return[l+s[0],u+s[1],f+s[2]]}}function Yi(e,n){if(e.length<3||n===0)return e;let t=[],o=e.length;for(let r=0;r<o;r++){let s=e[(r-1+o)%o],i=e[r],a=e[(r+1)%o],c=i[0]-s[0],l=i[1]-s[1],u=a[0]-i[0],f=a[1]-i[1],d=Math.hypot(c,l),m=Math.hypot(u,f);if(d<1e-9||m<1e-9){t.push(i);continue}let p=l/d,h=-c/d,g=f/m,y=-u/m,x=p+g,b=h+y,P=Math.hypot(x,b);if(P<1e-9){t.push(i);continue}let A=x/P,C=b/P,E=A*p+C*h;if(Math.abs(E)<1e-9){t.push(i);continue}let S=n/E;t.push([i[0]+A*S,i[1]+C*S])}return t}var Wi=.5,ji=.001,Hi=.5;function er(e,n,t,o){let r=[];for(let u=0;u<e.length;u++){if(o.has(u))continue;let f=e[u];if(f.vertices.length<3)continue;let d=f.vertices.map(S=>t(S,n)),m=d[0],p=0,h=0,g=0;for(let S=1;S+1<d.length;S++){let M=d[S],T=d[S+1],v=M[0]-m[0],I=M[1]-m[1],_=M[2]-m[2],O=T[0]-m[0],R=T[1]-m[1],w=T[2]-m[2];p+=R*_-w*I,h+=w*v-O*_,g+=O*I-R*v}let y=Math.hypot(p,h,g);if(y<1e-9)continue;let x=[p/y,h/y,g/y],b=null;for(let S=1;S<d.length;S++){let M=d[S],T=M[0]-m[0],v=M[1]-m[1],I=M[2]-m[2],_=Math.hypot(T,v,I);if(_>1e-9){b=[T/_,v/_,I/_];break}}if(!b)continue;let P=b[0]*x[0]+b[1]*x[1]+b[2]*x[2];b=[b[0]-P*x[0],b[1]-P*x[1],b[2]-P*x[2]];let A=Math.hypot(b[0],b[1],b[2]);if(A<1e-9)continue;b=[b[0]/A,b[1]/A,b[2]/A];let C=[x[1]*b[2]-x[2]*b[1],x[2]*b[0]-x[0]*b[2],x[0]*b[1]-x[1]*b[0]],E=x[0]*m[0]+x[1]*m[1]+x[2]*m[2];r.push({face:f,O:m,n:x,u:b,v:C,offset:E,polyIndex:u})}let s=[];for(let u of r){let f=!1;for(let d of s){let m=d.rep;if(!(1-(u.n[0]*m.n[0]+u.n[1]*m.n[1]+u.n[2]*m.n[2])>ji)&&!(Math.abs(u.offset-m.offset)>Hi)){d.faces.push(u),f=!0;break}}f||s.push({rep:u,faces:[u]})}let i=[],a=3,c=u=>`${u[0].toFixed(a)},${u[1].toFixed(a)},${u[2].toFixed(a)}`;for(let u of s){if(u.faces.length===1){l(u.faces);continue}let f=u.faces.map(g=>{let y=new Set;for(let x of g.face.vertices)y.add(c(t(x,n)));return y}),d=u.faces.map((g,y)=>y),m=g=>{for(;d[g]!==g;)d[g]=d[d[g]],g=d[g];return g},p=(g,y)=>{let x=m(g),b=m(y);x!==b&&(d[x]=b)};for(let g=0;g<u.faces.length;g++)for(let y=g+1;y<u.faces.length;y++){let x=f[g],b=f[y],P=0;for(let A of x)if(b.has(A)&&(P++,P>=2))break;P>=2&&p(g,y)}let h=new Map;for(let g=0;g<u.faces.length;g++){let y=m(g),x=h.get(y);x||(x=[],h.set(y,x)),x.push(u.faces[g])}for(let g of h.values())l(g)}function l(u){let f=u[0],{O:d,n:m,u:p,v:h}=f,g=[],y=[],x=[];for(let A of u){let C=[];for(let E of A.face.vertices){let S=t(E,n),M=S[0]-d[0],T=S[1]-d[1],v=S[2]-d[2],I=[M*p[0]+T*p[1]+v*p[2],M*h[0]+T*h[1]+v*h[2]];g.push(I),C.push(I)}C.length>=3&&(y.push(mn(C)),x.push(A.polyIndex))}if(g.length<3)return;let b=Zo(g);if(b.length<3)return;let P=Yi(mn(b),Wi);i.push({O:d,n:m,u:p,v:h,outlineUv:P,memberPolysUv:y,memberPolyIndices:x})}return i}function Hl(e){let n=typeof e.scale=="number"?e.scale:e.scale?.[0]??1,t=typeof e.scale=="number"?e.scale:e.scale?.[1]??1,o=typeof e.scale=="number"?e.scale:e.scale?.[2]??1,r=n!==1||t!==1||o!==1,s=!!e.rotation&&(!!e.rotation[0]||!!e.rotation[1]||!!e.rotation[2]),i=e.position?Qe(e.position):[0,0,0],a=[];return(i[0]!==0||i[1]!==0||i[2]!==0)&&a.push(`translate3d(${i[0]}px, ${i[1]}px, ${i[2]}px)`),s&&(e.rotation[0]&&a.push(`rotateY(${-e.rotation[0]}deg)`),e.rotation[1]&&a.push(`rotateX(${-e.rotation[1]}deg)`),e.rotation[2]&&a.push(`rotateZ(${-e.rotation[2]}deg)`)),r&&a.push(`scale3d(${n}, ${t}, ${o})`),a.length>0?a.join(" "):void 0}function Kl(e={}){let n=e.target??Ee.target,t=e.autoCenterOffset??[0,0,0],o=e.layoutScale??1,r=e.worldUnitPx??50,s=(e.zoom??Ee.zoom)/r*o,i=(e.distance??Ee.distance)*o,a=Qe([n[0]+t[0],n[1]+t[1],n[2]+t[2]],r);return`${i!==0?`translateZ(${-i}px) `:""}scale(${s}) rotateX(${e.rotX??Ee.rotX}deg) rotate(${e.rotY??Ee.rotY}deg) translate3d(${-a[0]}px, ${-a[1]}px, ${-a[2]}px)`}var dn=50,Wn=[.4,-.7,.59],jn="#ffffff",Hn=1,Kn="#ffffff",Zn=.4,En=4096,Zt=1,nr=.1,tr=1,qt=En*En,or=qt*3,yt=2048,rr=4*1024*1024,sr=16*1024*1024,ir=.995,ar=512,Zl=12,Me=.001,de=1e-9,cr=1e-4,lr=.1,Qt=.01,ql=.75,Ql=1,Jl=250,eu=1.5,ur=.75,pn=3,fr=2,Jt=4,mr=[1,10,100,1e3,1e4,1e5,1e6],gn=64,dr=32,pr=96,Ye=64,gr=128,eo=50,no=1e-7,hn=16,hr=.9,We=.75,to=.2,yr=.05,xr=256,br=.6,nu=1.5;function Pr(e){return e===void 0||e==="auto"||!Number.isFinite(e)?1:Math.max(0,Math.min(1,e))}var oo=new Map;function qn(e){if(oo.has(e)){let t=oo.get(e);return t===void 0?null:t}let n=Pn(e);return oo.size>=ar&&oo.clear(),oo.set(e,n),n}function yn(e){let n=qn(e);return n?{r:n.rgb[0],g:n.rgb[1],b:n.rgb[2]}:{r:255,g:255,b:255}}function Mr({r:e,g:n,b:t}){return`${e},${n},${t}`}function ro(e){return qn(e)?.alpha??1}function so({r:e,g:n,b:t}){let o=r=>Math.round(Math.max(0,Math.min(255,r))).toString(16).padStart(2,"0");return`#${o(e)}${o(n)}${o(t)}`}function io(e,n,t,o){let r=yn(n),s=yn(t),i=$e(r.r/255),a=$e(r.g/255),c=$e(r.b/255),l=$e(s.r/255),u=$e(s.g/255),f=$e(s.b/255);return{r:(i*e+l*o)*xt,g:(a*e+u*o)*xt,b:(c*e+f*o)*xt}}function tu({r:e,g:n,b:t}){let o=r=>Math.round(Math.max(0,Math.min(1,r))*255);return`rgb(${o(e)} ${o(n)} ${o(t)})`}function $e(e){return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)}function v0(e){return e<=.0031308?e*12.92:1.055*Math.pow(e,1/2.4)-.055}var xt=1/Math.PI;function Vn(e,n,t,o,r){let s=yn(e),i=yn(t),a=yn(o),c=$e(s.r/255),l=$e(s.g/255),u=$e(s.b/255),f=$e(i.r/255),d=$e(i.g/255),m=$e(i.b/255),p=$e(a.r/255),h=$e(a.g/255),g=$e(a.b/255),y=c*(f*n+p*r)*xt,x=l*(d*n+h*r)*xt,b=u*(m*n+g*r)*xt,P=M=>Math.max(0,Math.min(255,Math.round(v0(Math.max(0,Math.min(1,M)))*255))),A=P(y),C=P(x),E=P(b),S=ro(e);return S<1?`rgba(${A}, ${C}, ${E}, ${S})`:so({r:A,g:C,b:E})}function Sr(e,n){if(!Number.isFinite(n)||n<=1)return e;let t=qn(e);if(!t)return e;let o=255/Math.max(1,Math.round(n)-1),r=i=>Math.max(0,Math.min(255,Math.round(Math.round(i/o)*o))),s={r:r(t.rgb[0]),g:r(t.rgb[1]),b:r(t.rgb[2])};return t.alpha<1?`rgba(${s.r}, ${s.g}, ${s.b}, ${t.alpha})`:so(s)}function ou(e,n){return!!e&&!!n&&e.r===n.r&&e.g===n.g&&e.b===n.b}function ru(e,n,t){let o=(r,s)=>{let i=s-r;return Math.abs(i)<=t?s:r+Math.sign(i)*t};return{r:o(e.r,n.r),g:o(e.g,n.g),b:o(e.b,n.b)}}function su(e,n=1){return n<1?`rgba(${e.r}, ${e.g}, ${e.b}, ${n})`:so(e)}function iu(e,n){if(e===void 0)return Number.POSITIVE_INFINITY;if(e===n)return 0;let t=qn(e),o=qn(n);if(!t||!o)return 1;let r=t.rgb[0]-o.rgb[0],s=t.rgb[1]-o.rgb[1],i=t.rgb[2]-o.rgb[2],a=(t.alpha-o.alpha)*255;return Math.sqrt(r*r+s*s+i*i+a*a)/510}function Ar(e){let t=a=>Math.round(a/1e-4).toString(36),o=a=>`${t(a[0])},${t(a[1])},${t(a[2])}`,r=(a,c)=>{let l=o(a),u=o(c);return l<u?`${l}|${u}`:`${u}|${l}`},s=new Map;for(let a=0;a<e.length;a++){let c=e[a];if(!c||c.vertices.length<2)continue;let l=c.vertices;for(let u=0;u<l.length;u++){let f=l[u],d=l[(u+1)%l.length],m=r(f,d),p=s.get(m);p?(p.degree+=1,p.degree===2?p.polyB=a:p.polyB=null):s.set(m,{polyA:a,polyB:null,vertA:f,vertB:d,degree:1})}}let i=new Map;for(let[a,c]of s)i.set(a,{polyA:c.polyA,polyB:c.polyB,vertA:c.vertA,vertB:c.vertB});return i}function Cr(e,n){let t=Math.hypot(n[0],n[1],n[2])||1,o=n[0]/t,r=n[1]/t,s=n[2]/t;return e.map(i=>i?i[0]*o+i[1]*r+i[2]*s<-1e-6:!0)}function Tr(e,n){let t=[];for(let l of e.values()){let u=n[l.polyA]??!1;if(l.polyB===null)u&&t.push([l.vertA,l.vertB]);else{let f=n[l.polyB]??!1;u!==f&&t.push([l.vertA,l.vertB])}}if(t.length===0)return[];let o=1e-4,r=l=>Math.round(l/o).toString(36),s=l=>`${r(l[0])},${r(l[1])},${r(l[2])}`,i=t.map(([l,u])=>({a:l,b:u,aKey:s(l),bKey:s(u),used:!1})),a=new Map;for(let l of i){let u=a.get(l.aKey);u||(u=[],a.set(l.aKey,u)),u.push(l);let f=a.get(l.bKey);f||(f=[],a.set(l.bKey,f)),f.push(l)}let c=[];for(let l of i){if(l.used)continue;let u=[l.a],f=l,d=l.aKey,m=l.bKey;f.used=!0;for(let p=0;p<i.length+1;p++){u.push(f.bKey===m?f.b:f.a);let h=a.get(m)??[],g=null;for(let y of h)if(!(y===f||y.used)){g=y;break}if(!g||(g.used=!0,d=m,m=g.aKey===d?g.bKey:g.aKey,f=g,m===l.aKey))break}u.length>=3&&c.push(u)}return c}var au=40;function cu(e){let t=a=>Math.round(a/1e-4).toString(36),o=a=>`${t(a[0])},${t(a[1])},${t(a[2])}`,r=(a,c)=>{let l=o(a),u=o(c);return l<u?`${l}|${u}`:`${u}|${l}`},s=new Map;for(let a=0;a<e.length;a++){let c=e[a];if(!c||c.vertices.length<2)continue;let l=c.vertices;for(let u=0;u<l.length;u++){let f=l[u],d=l[(u+1)%l.length],m=r(f,d),p=s.get(m);p||(p=[],s.set(m,p)),p.push(a)}}let i=new Map;for(let a of s.values())if(!(a.length<2))for(let c of a)for(let l of a){if(c===l)continue;let u=i.get(c);u||(u=new Set,i.set(c,u)),u.add(l)}return i}function lu(e,n,t,o){let r=ht(t),s=!!o&&(o[0]!==0||o[1]!==0||o[2]!==0),i=s?Qe(n):null,a=e.map(c=>{if(!c)return{vertices:[],color:""};let l=c.vertices.map(u=>r(u,n));return s&&i&&o&&(l=l.map(u=>{let f=[u[0]-i[0],u[1]-i[1],u[2]-i[2]],d=On(f,o);return[d[0]+i[0],d[1]+i[1],d[2]+i[2]]})),{vertices:l,color:""}});return Ar(a)}function uu(e,n,t,o,r){let s=[],i=ht(t),a=!!r&&(r[0]!==0||r[1]!==0||r[2]!==0),c=a?Qe(n):null;for(let l=0;l<e.length;l++){if(!o(l))continue;let u=e[l];if(!u)continue;let f=u.vertices.map(A=>i(A,n));a&&c&&r&&(f=f.map(A=>{let C=[A[0]-c[0],A[1]-c[1],A[2]-c[2]],E=On(C,r);return[E[0]+c[0],E[1]+c[1],E[2]+c[2]]}));let d=1/0,m=1/0,p=1/0,h=-1/0,g=-1/0,y=-1/0;for(let A of f)A[0]<d&&(d=A[0]),A[0]>h&&(h=A[0]),A[1]<m&&(m=A[1]),A[1]>g&&(g=A[1]),A[2]<p&&(p=A[2]),A[2]>y&&(y=A[2]);let x=[[d,m,p],[h,m,p],[d,g,p],[h,g,p],[d,m,y],[h,m,y],[d,g,y],[h,g,y]],b=null,P=0;if(f.length>=3){let A=f[0],C=f[1],E=f[2],S=[C[0]-A[0],C[1]-A[1],C[2]-A[2]],M=[E[0]-A[0],E[1]-A[1],E[2]-A[2]],T=M[1]*S[2]-M[2]*S[1],v=M[2]*S[0]-M[0]*S[2],I=M[0]*S[1]-M[1]*S[0],_=Math.hypot(T,v,I);_>1e-9&&(b=[T/_,v/_,I/_],P=b[0]*A[0]+b[1]*A[1]+b[2]*A[2])}s.push({wv:f,bboxCorners:x,planeN:b,planeOffset:P,polygonIndex:l})}return s}function fu(e,n,t,o,r,s){let i=ht(t),a=!!s&&(s[0]!==0||s[1]!==0||s[2]!==0),c=a?Qe(n):null,f=er(e,n,a&&c&&s?(g,y)=>{let x=i(g,y),b=[x[0]-c[0],x[1]-c[1],x[2]-c[2]],P=On(b,s);return[P[0]+c[0],P[1]+c[1],P[2]+c[2]]}:i,o).map((g,y)=>{let{O:x,n:b,u:P,v:A,outlineUv:C,memberPolysUv:E,memberPolyIndices:S}=g,M=1/0,T=1/0,v=-1/0,I=-1/0;for(let Q of C)Q[0]<M&&(M=Q[0]),Q[1]<T&&(T=Q[1]),Q[0]>v&&(v=Q[0]),Q[1]>I&&(I=Q[1]);let _=v-M,O=I-T,R=r*50,w=x[0]+M*P[0]+T*A[0]+R*b[0],F=x[1]+M*P[1]+T*A[1]+R*b[1],D=x[2]+M*P[2]+T*A[2]+R*b[2],H=`matrix3d(${[P[0],P[1],P[2],0,A[0],A[1],A[2],0,b[0],b[1],b[2],0,w,F,D,1].map(Q=>Q.toFixed(4)).join(",")})`;return{O:x,n:b,u:P,v:A,outlineUv:C,memberPolysUv:E,memberPolyIndices:S,minU:M,minV:T,width:_,height:O,matrixCss:H,faceIndex:y,lift:R}}),d=.015,m=.5,p=20,h=new Set;for(let g=0;g<f.length;g++){if(h.has(g))continue;let y=f[g],x=y.n[0]*y.O[0]+y.n[1]*y.O[1]+y.n[2]*y.O[2],b=y.minU,P=y.minV,A=y.minU+y.width,C=y.minV+y.height;for(let E=0;E<f.length;E++){if(E===g||h.has(E))continue;let S=f[E];if(y.n[0]*S.n[0]+y.n[1]*S.n[1]+y.n[2]*S.n[2]<1-d)continue;let T=y.n[0]*S.O[0]+y.n[1]*S.O[1]+y.n[2]*S.O[2];if(T<=x+m||T-x>p)continue;let v=1/0,I=1/0,_=-1/0,O=-1/0;for(let R of S.outlineUv){let w=S.O[0]+R[0]*S.u[0]+R[1]*S.v[0],F=S.O[1]+R[0]*S.u[1]+R[1]*S.v[1],D=S.O[2]+R[0]*S.u[2]+R[1]*S.v[2],K=w-y.O[0],H=F-y.O[1],Q=D-y.O[2],ne=K*y.u[0]+H*y.u[1]+Q*y.u[2],ee=K*y.v[0]+H*y.v[1]+Q*y.v[2];ne<v&&(v=ne),ne>_&&(_=ne),ee<I&&(I=ee),ee>O&&(O=ee)}if(v<=b&&_>=A&&I<=P&&O>=C){let R=Math.max(0,A-b)*Math.max(0,C-P);if(Math.max(0,_-v)*Math.max(0,O-I)>R*2)continue;h.add(g);break}}}return f=f.filter((g,y)=>!h.has(y)),f}function mu(e){let{receiverPlanes:n,receiverPolygons:t,receiverHasTexture:o,casters:r,lightDir:s,cameraRot:i,ambientLight:a,directionalLight:c,shadow:l}=e,u=Math.hypot(s[0],s[1],s[2])||1,f=s[0]/u,d=s[1]/u,m=s[2]/u,p=a?.color??"#ffffff",h=a?.intensity??.4,g=c?.intensity??1,y=l?.color??"#000000",x=l?.opacity??.25,b=l?.maxExtend??2e3,P=[],A=r.map(S=>{let M=S.edgeOwners;if(!M||S.selfShadowEdgeMap)return null;let T=S.casterPolygonCount??0;if(T<au||S.items.length<au)return null;let v=0;for(let O of S.items)O.planeN&&v++;if(v*2<S.items.length)return null;let I=new Array(T).fill(null);for(let O of S.items)O.planeN&&O.polygonIndex<T&&(I[O.polygonIndex]=O.planeN);let _=Cr(I,s);return Tr(M,_)}),C=(S,M,T,v)=>{if(S.length<3)return[];let I=[],_=R=>(R[0]-M[0])*T[0]+(R[1]-M[1])*T[1]+(R[2]-M[2])*T[2],O=(R,w,F,D)=>{let K=(F-v)/(F-D);return[R[0]+K*(w[0]-R[0]),R[1]+K*(w[1]-R[1]),R[2]+K*(w[2]-R[2])]};for(let R=0;R<S.length;R++){let w=S[R],F=S[(R+S.length-1)%S.length],D=_(w),K=_(F),H=D>v,Q=K>v;H?(Q||I.push(O(F,w,K,D)),I.push(w)):Q&&I.push(O(F,w,K,D))}return I},E={rotX:i.rotX,rotY:i.rotY};for(let S of n){let{O:M,n:T,u:v,v:I,outlineUv:_,minU:O,minV:R,width:w,height:F}=S,D=f*T[0]+d*T[1]+m*T[2];if(D<=1e-6||!gt(T,E))continue;let K=ie=>(ie[0]-M[0])*T[0]+(ie[1]-M[1])*T[1]+(ie[2]-M[2])*T[2],H=(ie,ae,N,X)=>{let oe=N/(N-X);return[ie[0]+oe*(ae[0]-ie[0]),ie[1]+oe*(ae[1]-ie[1]),ie[2]+oe*(ae[2]-ie[2])]},Q=ie=>{let ae=ie[0]-M[0],N=ie[1]-M[1],X=ie[2]-M[2],oe=(ae*T[0]+N*T[1]+X*T[2])/D,ve=ie[0]-oe*f,Ce=ie[1]-oe*d,Pe=ie[2]-oe*m,Oe=ve-M[0],Re=Ce-M[1],Ve=Pe-M[2];return[Oe*v[0]+Re*v[1]+Ve*v[2],Oe*I[0]+Re*I[1]+Ve*I[2]]},ne=new Map,ee=0,ue=O,U=R,$=S.minU+w,k=S.minV+F,L=1/0,W=1/0,Y=-1/0,se=-1/0;for(let ie of r)for(let ae of ie.items)for(let N of ae.wv){let X=N[0]-M[0],oe=N[1]-M[1],ve=N[2]-M[2],Ce=X*v[0]+oe*v[1]+ve*v[2],Pe=X*I[0]+oe*I[1]+ve*I[2];Ce<L&&(L=Ce),Ce>Y&&(Y=Ce),Pe<W&&(W=Pe),Pe>se&&(se=Pe)}let Z=Number.isFinite(L)?[[L-b,W-b],[Y+b,W-b],[Y+b,se+b],[L-b,se+b]]:[],B=.05,q=T[0]*M[0]+T[1]*M[1]+T[2]*M[2],me=.0025,fe=5;for(let ie=0;ie<r.length;ie++){let ae=r[ie],N=ae.selfShadowEdgeMap,X=A[ie];if(X!==null){if(X.length===0)continue;for(let oe of X){if(oe.length<3)continue;let ve=C(oe,M,T,B);if(ve.length<3)continue;let Ce=ve.map(Q),Pe=mn(Ce),Oe=Z.length===4?Tn(Pe,Z):Pe;if(Oe.length<3)continue;let Re=Tn(Oe,_);if(Re.length<3)continue;let Ve;for(let Xe of S.memberPolysUv){let Te=Tn(Re,mn(Xe));Te.length<3||(Ve||(Ve=ne.get(ae.id),Ve||(Ve={id:ae.id,verts:[],subPolygonIndices:[]},ne.set(ae.id,Ve))),Ve.verts.push(Te),Ve.subPolygonIndices.push(-1),ee++)}}continue}for(let oe of ae.items){if(N){let Te=N.get(oe.polygonIndex);if(Te){let Be=!1;for(let De of S.memberPolyIndices)if(Te.has(De)){Be=!0;break}if(Be)continue}}if(oe.planeN&&oe.planeN[0]*f+oe.planeN[1]*d+oe.planeN[2]*m>-1e-6)continue;if(oe.planeN){let Te=oe.planeN,Be=Te[0]*T[0]+Te[1]*T[1]+Te[2]*T[2];if(Math.abs(Be)>1-me){let De=Be>0?1:-1;if(Math.abs(oe.planeOffset-De*q)<fe)continue}}let ve=oe.bboxCorners,Ce=!1,Pe=1/0,Oe=1/0,Re=-1/0,Ve=-1/0;for(let Te=0;Te<8;Te++){let Be=ve[Te];K(Be)>B&&(Ce=!0);let De=Q(Be);De[0]<Pe&&(Pe=De[0]),De[0]>Re&&(Re=De[0]),De[1]<Oe&&(Oe=De[1]),De[1]>Ve&&(Ve=De[1])}if(!Ce||Re<ue||Pe>$||Ve<U||Oe>k)continue;let Xe=oe.wv;for(let Te=1;Te<Xe.length-1;Te++){let Be=Xe[0],De=Xe[Te],rt=Xe[Te+1],st=K(Be),xn=K(De),Je=K(rt),bn=[],ke=[[Be,st],[De,xn],[rt,Je]];for(let He=0;He<3;He++){let[Ie,en]=ke[He],[cn,nn]=ke[(He+1)%3],Ke=en>B,ln=nn>B;Ke&&bn.push(Ie),Ke!==ln&&bn.push(H(Ie,cn,en,nn))}if(bn.length<3)continue;let je=bn.map(Q),Se=mn(je),Le=Z.length===4?Tn(Se,Z):Se;if(Le.length<3)continue;let we=Tn(Le,_);if(we.length<3)continue;let _e;for(let He of S.memberPolysUv){let Ie=Tn(we,mn(He));if(!(Ie.length<3)){if(N){let en=0,cn=1/0,nn=-1/0,Ke=1/0,ln=-1/0;for(let un=0;un<Ie.length;un++){let Ze=Ie[un],at=Ie[(un+1)%Ie.length];en+=Ze[0]*at[1]-at[0]*Ze[1],Ze[0]<cn&&(cn=Ze[0]),Ze[0]>nn&&(nn=Ze[0]),Ze[1]<Ke&&(Ke=Ze[1]),Ze[1]>ln&&(ln=Ze[1])}if(Math.abs(en)*.5<5)continue}_e||(_e=ne.get(ae.id),_e||(_e={id:ae.id,verts:[],subPolygonIndices:[]},ne.set(ae.id,_e))),_e.verts.push(Ie),_e.subPolygonIndices.push(oe.polygonIndex),ee++}}}}}if(ee===0||!(w>0)||!(F>0))continue;let z=x;if(o){let ae=g*Math.max(0,D)+Math.max(0,h);if(ae>0){let N=Math.max(0,h)/ae,X=Math.pow(N,1/2.4);z=x*(1-X)}else z=0}let te=S.memberPolyIndices[0]??0,ce=t[te]?.color??"#cccccc",ye=o?y:Vn(ce,0,"#000000",p,h),he=1/0,xe=1/0,ge=-1/0,Ae=-1/0;for(let ie of ne.values())for(let ae of ie.verts)for(let N of ae)N[0]<he&&(he=N[0]),N[0]>ge&&(ge=N[0]),N[1]<xe&&(xe=N[1]),N[1]>Ae&&(Ae=N[1]);he=Math.floor(he-1),xe=Math.floor(xe-1),ge=Math.ceil(ge+1),Ae=Math.ceil(Ae+1),he<O&&(he=O),xe<R&&(xe=R),ge>O+w&&(ge=O+w),Ae>R+F&&(Ae=R+F);let Fe=ge-he,V=Ae-xe;if(!(Fe>0)||!(V>0))continue;let G=S.lift,j=M[0]+he*v[0]+xe*I[0]+G*T[0],J=M[1]+he*v[1]+xe*I[1]+G*T[1],le=M[2]+he*v[2]+xe*I[2]+G*T[2],pe=`matrix3d(${[v[0],v[1],v[2],0,I[0],I[1],I[2],0,T[0],T[1],T[2],0,j,J,le,1].map(ie=>ie.toFixed(4)).join(",")})`,be=[];for(let ie of ne.values()){let ae="";for(let N of ie.verts){ae+=`M${(N[0][0]-he).toFixed(1)},${(N[0][1]-xe).toFixed(1)}`;for(let X=1;X<N.length;X++)ae+=`L${(N[X][0]-he).toFixed(1)},${(N[X][1]-xe).toFixed(1)}`;ae+="Z"}be.push({casterId:ie.id,d:ae,casterPolygonIndices:ie.subPolygonIndices})}P.push({faceIndex:S.faceIndex,memberPolyIndices:S.memberPolyIndices,matrixCss:pe,width:Fe,height:V,fill:ye,opacity:z,paths:be})}return P}var I0=Symbol.for("polycss.animation.triangleFrameSource"),_0=Symbol.for("polycss.animation.triangleFrameTarget"),Zi=2200,Er=2201,qi=2202;function O0(e,n){let t=0,o=1,r=1,s=Er,i=1/0,a=0,c=!1,l=null,u=!0,f=!1,d={clampWhenFinished:!1,get timeScale(){return r},set timeScale(m){r=m},get weight(){return o},set weight(m){o=m},get time(){return t},set time(m){t=m},get isRunning(){return c},get enabled(){return u},set enabled(m){u=m},get paused(){return f},set paused(m){f=m},play(){return c=!0,d},stop(){return c=!1,t=0,a=0,l=null,d},reset(){return t=0,a=0,d},fadeIn(m){return l={from:0,to:1,elapsed:0,duration:m},o=0,d},fadeOut(m){return l={from:o,to:0,elapsed:0,duration:m},d},crossFadeTo(m,p){return d.fadeOut(p),m.fadeIn(p),d},crossFadeFrom(m,p){return m.fadeOut(p),d.fadeIn(p),d},setLoop(m,p){return s=m,i=p,d},setEffectiveTimeScale(m){return r=m,d},setEffectiveWeight(m){return o=m,d}};return d._internal={get clip(){return e},get time(){return t},get weight(){return o},get enabled(){return u},get paused(){return f},get running(){return c},get loopMode(){return s},get repetitions(){return i},get completedReps(){return a},set completedReps(m){a=m},get clampWhenFinished(){return d.clampWhenFinished},get timeScale(){return r},get fade(){return l},advance(m){if(!c)return;if(l){l.elapsed+=m;let h=l.duration>0?Math.min(l.elapsed/l.duration,1):1;if(o=l.from+(l.to-l.from)*h,h>=1&&(o=l.to,l=null,o<=0)){c=!1;return}}if(f)return;let p=e.duration;if(!(p<=0)){if(t+=m*r,s===Zi)t>=p&&(t=d.clampWhenFinished?p:0,c=!1);else if(s===Er){if(t>=p){let h=t%p,g=Math.floor(t/p);a+=g,t=h,i!==1/0&&a>=i&&(t=d.clampWhenFinished?p:0,c=!1)}}else if(p>0){let h=p*2,g=t%h;if(t>=h){let y=Math.floor(t/h);a+=y,i!==1/0&&a>=i?(t=d.clampWhenFinished?p:0,c=!1):t=g}}}},sampleTime(){if(s===qi){let m=e.duration,p=m*2,h=t%p;return h<=m?h:p-h}return t}},d}function R0(e){return e._internal}function Ki(e,n){return typeof n=="number"?e[n]:e.find(t=>t.name===n)}function du(e,n){let t=new Map,o=n,r=e;function s(f){let d=Ki(n.clips,f);if(!d)throw new Error(`PolyAnimationMixer: no clip found for key "${f}". Available: ${n.clips.map(p=>p.name).join(", ")}`);let m=t.get(d.index);return m||(m=O0(d,n),t.set(d.index,m)),m}function i(f){let d=Ki(n.clips,f);return d?t.get(d.index)??null:null}function a(f){let d=[];for(let y of t.values()){let x=R0(y);x.advance(f),x.running&&x.enabled&&d.push({internal:x,clip:x.clip})}if(d.length===0)return;if(d.length===1){let{internal:y,clip:x}=d[0],b=y.sampleTime(),P=o[I0]?.(x.name,b);if(P&&r[_0]?.(P))return;let A=n.sample(x.name,b);e.setPolygons(A);return}let m=0;for(let{internal:y}of d)m+=y.weight;if(m<=0)return;let p=d.map(({internal:y,clip:x})=>({polygons:n.sample(x.name,y.sampleTime()),weight:y.weight/m})),h=p[0].polygons;if(h.length===0)return;let g=h.map((y,x)=>{let b=y.vertices.map((P,A)=>{let C=0,E=0,S=0;for(let{polygons:M,weight:T}of p){let v=M[x];if(!v)continue;let I=v.vertices[A];I&&(C+=I[0]*T,E+=I[1]*T,S+=I[2]*T)}return[C,E,S]});return{...y,vertices:b}});e.setPolygons(g)}function c(){for(let f of t.values())f.stop()}function l(f){let d=Ki(n.clips,f);d&&t.delete(d.index)}function u(){t.clear()}return{clipAction:s,existingAction:i,update:a,stopAllAction:c,uncacheClip:l,uncacheRoot:u}}var Ji={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},xu={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16},V0={0:"POINTS",1:"LINES",2:"LINE_LOOP",3:"LINE_STRIP",4:"TRIANGLES",5:"TRIANGLE_STRIP",6:"TRIANGLE_FAN"},ea=["EXT_meshopt_compression","KHR_meshopt_compression"];function L0(e){return e===5120||e===5121||e===5122||e===5123}function bu(e){let n=globalThis.TextDecoder;return new n().decode(e)}function w0(e){let n=e.indexOf(",");if(n<0)throw new Error("parseGltf: malformed data: URI");let t=e.slice(5,n),o=e.slice(n+1);if(!t.includes(";base64")){let i=decodeURIComponent(o),a=new Uint8Array(i.length);for(let c=0;c<i.length;c++)a[c]=i.charCodeAt(c)&255;return a}let r=globalThis.atob(o),s=new Uint8Array(r.length);for(let i=0;i<r.length;i++)s[i]=r.charCodeAt(i);return s}function N0(e){let n=new DataView(e);if(n.getUint32(0,!0)!==1179937895)throw new Error("parseGltf: not a GLB (bad magic)");let t=n.getUint32(4,!0);if(t!==2)throw new Error(`parseGltf: only glTF v2 supported (got v${t})`);let o=12,r=null,s=null;for(;o<e.byteLength;){let i=n.getUint32(o,!0),a=n.getUint32(o+4,!0),c=o+8;if(a===1313821514){let l=new Uint8Array(e,c,i);r=JSON.parse(bu(l))}else a===5130562&&(s=new Uint8Array(e,c,i));o=c+i}if(!r)throw new Error("parseGltf: no JSON chunk in GLB");return{doc:r,bin:s}}function pu(e){if(!e)return null;let n=/^(\d+)\.(\d+)(?:\D.*)?$/.exec(e);return n?[Number(n[1]),Number(n[2])]:null}function D0(e){let n=pu(e.asset?.version);if(!n||n[0]!==2)throw new Error(`parseGltf: only glTF asset v2 supported (got ${e.asset?.version??"missing"})`);let t=pu(e.asset?.minVersion);if(e.asset?.minVersion&&(!t||t[0]>2||t[0]===2&&t[1]>0))throw new Error(`parseGltf: glTF asset requires minVersion ${e.asset.minVersion}`)}function F0(e,n,t){let o=e.buffers??[],r=new Set(e.extensionsRequired??[]),s=ea.some(i=>r.has(i));return o.map((i,a)=>{let c=i.uri;if(c){if(c.startsWith("data:"))return w0(c);if(t){let l=t(c);if(l instanceof Uint8Array)return l;throw new Error("parseGltf: resolveBuffer returned a Promise; use parseGltf via async if your buffers are external")}throw new Error(`parseGltf: external buffer URI "${c}" \u2014 provide options.resolveBuffer`)}if(a===0&&n)return n;if(s&&ea.some(l=>i.extensions?.[l]))return new Uint8Array(0);throw new Error(`parseGltf: buffer[${a}] has no uri and no GLB BIN chunk`)})}function co(e,n,t){let o=e.bufferViews?.[t],r=o?n[o.buffer]:void 0;if(!o||!r)throw new Error(`parseGltf: bad bufferView ${t}`);let s=o.byteOffset??0;if(s<0||o.byteLength<0||s+o.byteLength>r.byteLength)throw new Error(`parseGltf: bufferView ${t} outside buffer ${o.buffer}`);return{buffer:r,view:o}}function Pu(e,n,t,o){if(e.count<=0)return;let r=e.byteOffset??0,s=r+t*(e.count-1)+o;if(r<0||s>n.byteLength)throw new Error("parseGltf: accessor does not fit bufferView")}function Mu(e,n){return e.byteStride!==void 0&&e.byteStride>0?e.byteStride:n}function B0(e,n,t){if(t||e===5126)return new Float32Array(n);switch(e){case 5120:return new Int8Array(n);case 5121:return new Uint8Array(n);case 5122:return new Int16Array(n);case 5123:return new Uint16Array(n);case 5125:return new Uint32Array(n);default:throw new Error(`parseGltf: unhandled componentType ${e}`)}}function vr(e,n,t){let o=e.accessors?.[t];if(!o)throw new Error(`parseGltf: bad accessor ${t}`);let r=Ji[o.componentType],s=xu[o.type];if(!r||!s)throw new Error(`parseGltf: unsupported accessor type ${o.type}/${o.componentType}`);if(o.sparse||o.normalized||o.bufferView===void 0){let{values:g}=lo(e,n,t);return{array:B0(o.componentType,g,o.normalized),count:o.count,componentCount:s}}let{buffer:i,view:a}=co(e,n,o.bufferView),c=(a.byteOffset??0)+(o.byteOffset??0),l=o.count*s,u=r*s,f=Mu(a,u);if(Pu(o,a,f,u),f===u){let g=i.buffer.slice(i.byteOffset+c,i.byteOffset+c+l*r),y;switch(o.componentType){case 5120:y=new Int8Array(g);break;case 5121:y=new Uint8Array(g);break;case 5122:y=new Int16Array(g);break;case 5123:y=new Uint16Array(g);break;case 5125:y=new Uint32Array(g);break;case 5126:y=new Float32Array(g);break;default:throw new Error(`parseGltf: unhandled componentType ${o.componentType}`)}return{array:y,count:o.count,componentCount:s}}let d;switch(o.componentType){case 5120:d=new Int8Array(l);break;case 5121:d=new Uint8Array(l);break;case 5122:d=new Int16Array(l);break;case 5123:d=new Uint16Array(l);break;case 5125:d=new Uint32Array(l);break;case 5126:d=new Float32Array(l);break;default:throw new Error(`parseGltf: unhandled componentType ${o.componentType}`)}let m=new DataView(i.buffer),p=i.byteOffset+c,h=0;for(let g=0;g<o.count;g++){let y=p+g*f;for(let x=0;x<s;x++)d[h++]=Or(m,y+x*r,o.componentType)}return{array:d,count:o.count,componentCount:s}}function Or(e,n,t){switch(t){case 5120:return e.getInt8(n);case 5121:return e.getUint8(n);case 5122:return e.getInt16(n,!0);case 5123:return e.getUint16(n,!0);case 5125:return e.getUint32(n,!0);case 5126:return e.getFloat32(n,!0);default:throw new Error(`parseGltf: unhandled componentType ${t}`)}}function gu(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 lo(e,n,t){let o=e.accessors?.[t];if(!o)throw new Error(`parseGltf: bad accessor ${t}`);let r=Ji[o.componentType],s=xu[o.type];if(!r||!s)throw new Error(`parseGltf: unsupported accessor type ${o.type}/${o.componentType}`);let i=r*s,a=new Array(o.count*s).fill(0);if(o.bufferView!==void 0){let{buffer:c,view:l}=co(e,n,o.bufferView),u=c.byteOffset+(l.byteOffset??0)+(o.byteOffset??0),f=Mu(l,i);Pu(o,l,f,i);let d=new DataView(c.buffer),m=0;for(let p=0;p<o.count;p++){let h=u+p*f;for(let g=0;g<s;g++){let y=Or(d,h+g*r,o.componentType);a[m++]=o.normalized?gu(y,o.componentType):y}}}if(o.sparse){let c=o.sparse,l=c.indices,u=Ji[l.componentType];if(l.componentType!==5121&&l.componentType!==5123&&l.componentType!==5125)throw new Error(`parseGltf: unhandled sparse index componentType ${l.componentType}`);let{buffer:f,view:d}=co(e,n,l.bufferView),m=f.byteOffset+(d.byteOffset??0)+(l.byteOffset??0),p=new DataView(f.buffer),{buffer:h,view:g}=co(e,n,c.values.bufferView),y=h.byteOffset+(g.byteOffset??0)+(c.values.byteOffset??0),x=new DataView(h.buffer);for(let b=0;b<c.count;b++){let P=Or(p,m+b*u,l.componentType);for(let A=0;A<s;A++){let C=Or(x,y+(b*s+A)*r,o.componentType);a[P*s+A]=o.normalized?gu(C,o.componentType):C}}}return{values:a,count:o.count,componentCount:s}}function k0(e,n,t){let o=[],r=[],s=globalThis;for(let i of e.images??[]){if(i.uri){if(t&&!i.uri.startsWith("data:"))try{o.push(new s.URL(i.uri,t).href)}catch{o.push(i.uri)}else o.push(i.uri);continue}if(i.bufferView!==void 0){let a;try{let{buffer:f,view:d}=co(e,n,i.bufferView),m=d.byteOffset??0;a=f.subarray(m,m+d.byteLength)}catch{o.push("");continue}let c=i.mimeType??"image/png",l=new s.Blob([a],{type:c}),u=s.URL.createObjectURL(l);o.push(u),r.push(u)}else o.push("")}return{urls:o,objectUrls:r}}function hu(e){switch(e??10497){case 33071:return"clamp-to-edge";case 33648:return"mirrored-repeat";case 10497:return"repeat";default:return"repeat"}}function U0(e,n){let t=n?.sampler!==void 0?e.samplers?.[n.sampler]:void 0;return{s:hu(t?.wrapS),t:hu(t?.wrapT)}}function Su(e){switch(e){case"BLEND":return"blend";case"MASK":return"mask";default:return"opaque"}}function z0(e,n){let t=new Map,o=e.materials??[];for(let r=0;r<o.length;r++){let s=o[r].pbrMetallicRoughness?.baseColorTexture?.index;if(s===void 0)continue;let i=e.textures?.[s],a=i?.source;if(a===void 0)continue;let c=n[a];c&&t.set(r,{url:c,wrap:U0(e,i),alphaMode:Su(o[r].alphaMode),texCoord:o[r].pbrMetallicRoughness?.baseColorTexture?.texCoord??0})}return t}function G0(e,n,t){let o=e?.pbrMetallicRoughness?.baseColorFactor;if(!o||o.length<3)return n;let r=c=>Math.max(0,Math.min(1,c)),s=c=>Math.round(r(c)*255).toString(16).padStart(2,"0"),i=c=>Math.round(r(c)*255),a=t==="opaque"?1:r(o[3]??1);return a<1?`rgba(${i(o[0])}, ${i(o[1])}, ${i(o[2])}, ${Math.round(a*1e3)/1e3})`:`#${s(o[0])}${s(o[1])}${s(o[2])}`}var bt=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function Au(e,n){let t=new Array(16);return Cu(t,e,n)}function Cu(e,n,t){for(let o=0;o<4;o++)for(let r=0;r<4;r++)e[r*4+o]=n[0+o]*t[r*4+0]+n[4+o]*t[r*4+1]+n[8+o]*t[r*4+2]+n[12+o]*t[r*4+3];return e}function Ir(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 Tu(e,n,t){let o=e?.[0]??0,r=e?.[1]??0,s=e?.[2]??0,i=n?.[0]??0,a=n?.[1]??0,c=n?.[2]??0,l=n?.[3]??1,u=t?.[0]??1,f=t?.[1]??1,d=t?.[2]??1,m=i+i,p=a+a,h=c+c,g=i*m,y=i*p,x=i*h,b=a*p,P=a*h,A=c*h,C=l*m,E=l*p,S=l*h;return[(1-(b+A))*u,(y+S)*u,(x-E)*u,0,(y-S)*f,(1-(g+A))*f,(P+C)*f,0,(x+E)*d,(P-C)*d,(1-(g+b))*d,0,o,r,s,1]}function $0(e){return e.matrix&&e.matrix.length===16?e.matrix.slice():Tu(e.translation,e.rotation,e.scale)}function Eu(e,n,t){let o=new Array(Math.min(e.length,n.length));for(let r=0;r<o.length;r++)o[r]=e[r]+(n[r]-e[r])*t;return o}function ao(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 X0(e,n,t){let o=ao(e),r=ao(n),s=o[0]*r[0]+o[1]*r[1]+o[2]*r[2]+o[3]*r[3];if(s<0&&(s=-s,r=[-r[0],-r[1],-r[2],-r[3]]),s>.9995)return ao(Eu(o,r,t));let i=Math.acos(Math.max(-1,Math.min(1,s))),a=Math.sin(i),c=Math.sin((1-t)*i)/a,l=Math.sin(t*i)/a;return ao([o[0]*c+r[0]*l,o[1]*c+r[1]*l,o[2]*c+r[2]*l,o[3]*c+r[3]*l])}function vu(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 Iu(e){return e.matrix?e.matrix.slice():Tu(e.translation,e.rotation,e.scale)}function Y0(e){let n=e.scene??0,t=e.scenes?.[n]?.nodes;return t&&t.length>0?t:[]}function _u(e,n){let t=e.nodes??[],o=new Array(t.length),r=new Set,s=new Set,i=(c,l)=>{if(c<0||c>=t.length||s.has(c))return;let u=Au(l,n[c]??bt);o[c]=u,r.add(c),s.add(c);for(let f of t[c].children??[])i(f,u);s.delete(c)},a=Y0(e);if(a.length>0)for(let c of a)i(c,bt);for(let c=0;c<t.length;c++)r.has(c)||i(c,bt);return o}var Ou=Symbol("polycss.gltfAnimationRuntimeInfo"),W0=Symbol.for("polycss.animation.triangleFrameSource");function Ru(e,n){return e?e[Ou]?.withPolygonFilter(n):void 0}function Qi(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function j0(e,n,t){return Qi(e,n)||Qi(e,t)||Qi(n,t)}function yu(e,n,t,o,r){if(t===void 0)return;let{values:s,count:i,componentCount:a}=lo(e,n,t);if(i!==r||a<1)return;let c=[];for(let l=0;l<i;l++){let u=[];for(let f=0;f<o;f++)u.push(s[l*a+f]??0);c.push(u)}return c}function H0(e,n,t,o){if(t===void 0)return Array.from({length:o},()=>bt.slice());let{values:r,componentCount:s,count:i}=lo(e,n,t);if(s!==16)throw new Error(`parseGltf: inverseBindMatrices accessor ${t} is not MAT4`);let a=[];for(let c=0;c<o;c++){let l=Math.min(c,i-1);a.push(r.slice(l*16,l*16+16))}return a}function _r(e,n){let t=e.componentCount,o=e.interpolation==="CUBICSPLINE"?(n*3+1)*t:n*t;return e.output.slice(o,o+t)}function K0(e,n,t){let o=e.input;if(o.length===0)return[];if(o.length===1||n<=o[0])return _r(e,0);let r=o.length-1;if(n>=o[r])return _r(e,r);let s=0,i=r;for(;s+1<i;){let d=s+i>>1;o[d]<=n?s=d:i=d}let a=o[s],c=o[s+1],l=c>a?(n-a)/(c-a):0,u=_r(e,s),f=_r(e,s+1);return e.interpolation==="STEP"?u:t==="rotation"?X0(u,f,l):Eu(u,f,l)}function Vu(e,n){let t=[];for(let o=0;o<(e.animations?.length??0);o++){let r=e.animations[o],s=(r.samplers??[]).map(c=>{let l=lo(e,n,c.input),u=lo(e,n,c.output);return{input:l.values,output:u.values,componentCount:u.componentCount,interpolation:c.interpolation??"LINEAR"}}),i=[];for(let c of r.channels??[]){let l=c.target.node,u=c.target.path,f=s[c.sampler];l===void 0||!u||!f||u!=="translation"&&u!=="rotation"&&u!=="scale"||i.push({sampler:f,targetNode:l,path:u})}let a=i.reduce((c,l)=>{let u=l.sampler.input;return Math.max(c,u[u.length-1]??0)},0);t.push({info:{index:o,name:r.name??`animation_${o}`,duration:a,channelCount:i.length},channels:i})}return t}function Lu(e,n,t,o){let r=t.info.duration,s=r>0?(o%r+r)%r:Math.max(0,o),i=n.map(a=>({translation:a.translation.slice(),rotation:a.rotation.slice(),scale:a.scale.slice(),matrix:a.matrix?a.matrix.slice():void 0}));for(let a of t.channels){let c=i[a.targetNode];if(!c)continue;let l=K0(a.sampler,s,a.path);c.matrix=void 0,a.path==="translation"?c.translation=l.slice(0,3):a.path==="rotation"?c.rotation=ao(l.slice(0,4)):a.path==="scale"&&(c.scale=l.slice(0,3))}return _u(e,i.map(Iu))}function Z0(e,n,t,o,r,s){if((e.animations??[]).length===0||t.length===0)return;let a=(e.nodes??[]).map(g=>vu(g)),c=a.map(Iu),l=_u(e,c),u=(e.skins??[]).map(g=>({joints:g.joints??[],inverseBindMatrices:H0(e,n,g.inverseBindMatrices,g.joints?.length??0)})),f=Vu(e,n),d=f.map(g=>g.info);if(d.length===0)return;let m=g=>{let y=t.map(x=>new Array(x.triangleMask.length).fill(!1));for(let x of g){let b=o[x];if(!b)return;let P=y[b.sourceIndex];if(!P||b.triangleIndex<0||b.triangleIndex>=P.length)return;P[b.triangleIndex]=!0}return y.map((x,b)=>{let P=t[b],A=new Set,C=0;for(let E=0;E+2<P.indices.length;E+=3,C++)x[C]&&(A.add(P.indices[E]),A.add(P.indices[E+1]),A.add(P.indices[E+2]));return{triangleMask:x,activeVertices:Array.from(A).sort((E,S)=>E-S)}})},p=(g,y,x,b,P,A,C,E,S)=>{let M=r(g),T=r(y),v=r(x),I={vertices:[M,T,v],color:b};return P&&(I.texture=P),P&&A&&(I.textureWrap=A),P&&C&&(I.textureAlphaMode=C),E&&(I.doubleSided=!0),S&&(I.uvs=S),I},h=g=>{let y=t.reduce((_,O,R)=>{let w=g?.[R]?.triangleMask??O.triangleMask,F=0;for(let D=0;D<w.length;D++)w[D]&&F++;return _+F},0),x=new Float64Array(Math.max(0,y*9)),b=new Array(y),P=new Array(y).fill(!1),A={polygonCount:0,vertices:x,colors:b,textureFlags:P,solidTriangles:!0},C=t.map(_=>new Array(_.positions.length)),E=u.map(_=>_.joints.map(()=>new Array(16))),S=(_,O)=>{let R=typeof _=="number"?f[_]:f.find(w=>w.info.name===_);return R?Lu(e,a,R,O):null},M=(_,O,R,w)=>{let F=R?.activeVertices,D=C[_],K=(H,Q,ne,ee)=>{let ue=D[H];ue?(ue[0]=Q,ue[1]=ne,ue[2]=ee):D[H]=[Q,ne,ee]};if(O.skinIndex!==void 0&&O.joints&&O.weights&&u[O.skinIndex]){let H=u[O.skinIndex],Q=O.joints,ne=O.weights,ee=E[O.skinIndex]??[];for(let U=0;U<H.joints.length;U++){let $=H.joints[U],k=w[$],L=H.inverseBindMatrices[U],W=ee[U]??(ee[U]=new Array(16));ee[U]=k&&L?Cu(W,k,L):void 0}let ue=U=>{let $=O.positions[U],k=0,L=0,W=0,Y=0,se=Q[U]??[],Z=ne[U]??[];for(let B=0;B<4;B++){let q=Z[B]??0;if(q<=0)continue;let me=Math.round(se[B]??0),fe=ee[me];if(!fe)continue;let z=fe[0]*$[0]+fe[4]*$[1]+fe[8]*$[2]+fe[12],te=fe[1]*$[0]+fe[5]*$[1]+fe[9]*$[2]+fe[13],ce=fe[2]*$[0]+fe[6]*$[1]+fe[10]*$[2]+fe[14];k+=z*q,L+=te*q,W+=ce*q,Y+=q}if(Y>0){let B=1/Y;K(U,k*B,L*B,W*B)}else K(U,O.meshBindWorld[0]*$[0]+O.meshBindWorld[4]*$[1]+O.meshBindWorld[8]*$[2]+O.meshBindWorld[12],O.meshBindWorld[1]*$[0]+O.meshBindWorld[5]*$[1]+O.meshBindWorld[9]*$[2]+O.meshBindWorld[13],O.meshBindWorld[2]*$[0]+O.meshBindWorld[6]*$[1]+O.meshBindWorld[10]*$[2]+O.meshBindWorld[14])};if(F)for(let U of F)ue(U);else for(let U=0;U<O.positions.length;U++)ue(U)}else{let H=O.meshNode!==null?w[O.meshNode]??O.meshBindWorld:O.meshBindWorld,Q=ne=>{let ee=O.positions[ne];K(ne,H[0]*ee[0]+H[4]*ee[1]+H[8]*ee[2]+H[12],H[1]*ee[0]+H[5]*ee[1]+H[9]*ee[2]+H[13],H[2]*ee[0]+H[6]*ee[1]+H[10]*ee[2]+H[14])};if(F)for(let ne of F)Q(ne);else for(let ne=0;ne<O.positions.length;ne++)Q(ne)}return D},T=(_,O)=>{let R=S(_,O);if(!R)return[];let w=[];for(let F=0;F<t.length;F++){let D=t[F],K=g?.[F],H=K?.triangleMask??D.triangleMask,Q=M(F,D,K,R),ne=0;for(let ee=0;ee+2<D.indices.length;ee+=3,ne++){if(!H[ne])continue;let ue=D.indices[ee],U=D.indices[ee+1],$=D.indices[ee+2],k=Q[ue],L=Q[U],W=Q[$];if(!k||!L||!W)continue;let Y;if(D.uvs&&D.texture){let Z=D.uvs[ue],B=D.uvs[U],q=D.uvs[$];Z&&B&&q&&(Y=[Z,B,q])}let se=p(k,L,W,D.color,D.texture,D.textureWrap,D.textureAlphaMode,D.doubleSided,Y);se&&w.push(se)}}return w},v=(_,O)=>{let R=S(_,O);if(!R)return null;x.length<y*9&&(x=new Float64Array(y*9),A.vertices=x),b.length<y&&(b=new Array(y),A.colors=b),P.length<y&&(P=new Array(y).fill(!1),A.textureFlags=P);let w=0,F=0,D=!1;for(let K=0;K<t.length;K++){let H=t[K],Q=g?.[K],ne=Q?.triangleMask??H.triangleMask,ee=M(K,H,Q,R),ue=0;for(let U=0;U+2<H.indices.length;U+=3,ue++){if(!ne[ue])continue;let $=H.indices[U],k=H.indices[U+1],L=H.indices[U+2],W=ee[$],Y=ee[k],se=ee[L];if(!W||!Y||!se)continue;s(W,x,F),s(Y,x,F+3),s(se,x,F+6),b[w]=H.color;let Z=!!H.texture;P[w]=Z,Z&&(D=!0),w++,F+=9}}return A.polygonCount=w,A.solidTriangles=!D,A},I={clips:d,sample:T};return I[W0]=v,I[Ou]={withPolygonFilter(_){let O=m(_);return O?h(O):void 0}},I};return h()}function Rr(e,n){let t=n?.targetSize??60,o=n?.defaultColor??"#888888",r=n?.materialColors??{},s=n?.materialTextures??{},i=e instanceof Uint8Array?e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength):e,a=i.byteLength,c,l=null;if(i.byteLength>=4&&new DataView(i).getUint32(0,!0)===1179937895){let V=N0(i);c=V.doc,l=V.bin}else c=JSON.parse(bu(new Uint8Array(i)));D0(c);let u=F0(c,l,n?.resolveBuffer),{urls:f,objectUrls:d}=k0(c,u,n?.baseUrl),m=z0(c,f),p=[],h=new Set,g=new Set(c.extensionsRequired??[]),y=g.has("KHR_mesh_quantization")||(c.extensionsUsed??[]).includes("KHR_mesh_quantization");function x(V,G){h.has(V)||(h.add(V),p.push(G))}function b(V,G){if(V===void 0)return!1;let j=c.accessors?.[V];if(!j)return!1;let J=le=>c.bufferViews?.[le]?.extensions?.[G]!==void 0;return!!(j.bufferView!==void 0&&J(j.bufferView)||j.sparse&&(J(j.sparse.indices.bufferView)||J(j.sparse.values.bufferView)))}function P(V){for(let G of ea)if(g.has(G)&&(V.extensions?.[G]||b(V.indices,G)||Object.values(V.attributes??{}).some(j=>b(j,G))))return G}function A(V,G){if(G instanceof Float32Array)return!0;let j=c.accessors?.[V];return y&&j?.type==="VEC3"&&L0(j.componentType)}function C(V,G){return(G!==null?c.nodes?.[G]?.weights:void 0)??V.weights??[]}function E(V,G,j,J,le){let re=j.targets;if(!re||re.length===0)return;let pe=C(V,J);if(!pe.some(ae=>Number.isFinite(ae)&&ae!==0))return;let be=Array.from({length:le},()=>[0,0,0]),ie=!1;for(let ae=0;ae<re.length;ae++){let N=pe[ae]??0;if(!Number.isFinite(N)||N===0)continue;let X=re[ae]?.POSITION;if(X===void 0)continue;let{array:oe,count:ve,componentCount:Ce}=vr(c,u,X);if(ve!==le||Ce<3){x(`morph-position-count:${G}:${ae}`,`Mesh ${V.name??G}: skipped morph target ${ae} POSITION with mismatched count`);continue}if(!A(X,oe)){x(`morph-position-type:${G}:${ae}`,`Mesh ${V.name??G}: skipped morph target ${ae} POSITION with unsupported accessor type`);continue}ie=!0;for(let Pe=0;Pe<le;Pe++){let Oe=Pe*Ce,Re=be[Pe];Re[0]+=Number(oe[Oe])*N,Re[1]+=Number(oe[Oe+1])*N,Re[2]+=Number(oe[Oe+2])*N}}return ie?be:void 0}let S=[],M=[],T=[],v=(c.meshes??[]).map((V,G)=>V.name??`mesh_${G}`),I=(c.materials??[]).map((V,G)=>V.name??`material_${G}`);function _(V,G){if(G===4)return V;let j=[];if(G===5)for(let J=0;J+2<V.length;J++)j.push(V[J],V[J+1+J%2],V[J+2-J%2]);else if(G===6)for(let J=0;J+2<V.length;J++)j.push(V[J+1],V[J+2],V[0]);return j}function O(V){let G=[];for(let j=0;j+2<V.length;j+=3){let J=V[j],le=V[j+1],re=V[j+2];G.push(J,le,re,J,re,le)}return G}function R(V,G,j){let J=c.meshes?.[V];if(J){if(!Array.isArray(J.primitives)){x(`malformed-primitives:${V}`,`Mesh ${J.name??V}: skipped mesh with non-array primitives`);return}for(let le=0;le<J.primitives.length;le+=1){let re=J.primitives[le];if(!re)continue;let pe=re.mode??4;if(pe!==4&&pe!==5&&pe!==6){let Se=V0[pe]??`mode ${pe}`;x(`unsupported-mode:${pe}`,`Skipped primitives with unsupported mode ${pe} (${Se})`);continue}if(re.extensions?.KHR_draco_mesh_compression&&g.has("KHR_draco_mesh_compression")){x("KHR_draco_mesh_compression","Skipped primitives with unsupported required extension KHR_draco_mesh_compression");continue}let be=P(re);if(be){x(be,`Skipped primitives with unsupported required extension ${be}`);continue}if(re.attributes.POSITION===void 0){x(`missing-position:${V}`,`Mesh ${J.name??V}: skipped primitive without POSITION attribute`);continue}let ie=re.material!==void 0?c.materials?.[re.material]:void 0,ae=Su(ie?.alphaMode),N=ie?.name,oe=(N?r[N]:void 0)??G0(ie,o,ae),ve=ie?.doubleSided===!0,Ce=re.material!==void 0?m.get(re.material):void 0,Pe=N&&s[N]?s[N]:re.material!==void 0?Ce?.url:void 0,Oe=Pe&&re.material!==void 0?Ce?.wrap:void 0,Re=Pe?Ce?.alphaMode??ae:void 0,Ve=Pe?Ce?.texCoord??0:0,Xe=re.attributes.POSITION,{array:Te,count:Be,componentCount:De}=vr(c,u,Xe);if(!A(Xe,Te))continue;let rt=E(J,V,re,j,Be),st=[],xn=[];for(let Se=0;Se<Be;Se++){let Le=Se*De,we=rt?.[Se],_e=[Number(Te[Le])+(we?.[0]??0),Number(Te[Le+1])+(we?.[1]??0),Number(Te[Le+2])+(we?.[2]??0)];st.push(_e),xn.push(Ir(G,_e))}let Je=null,bn=re.attributes[`TEXCOORD_${Ve}`];if(Pe&&bn!==void 0){let{array:Se,count:Le}=vr(c,u,bn);Je=[];let we=1;Se instanceof Uint8Array?we=1/255:Se instanceof Uint16Array&&(we=1/65535);for(let _e=0;_e<Le;_e++){let He=Se[_e*2]*we,Ie=Se[_e*2+1]*we;Je.push([He,1-Ie])}}let ke;if(re.indices!==void 0){let{array:Se,count:Le}=vr(c,u,re.indices);ke=[];for(let we=0;we<Le;we++)ke.push(Number(Se[we]))}else ke=xn.map((Se,Le)=>Le);ke=_(ke,pe),ve&&(ke=O(ke));let je;if((c.animations?.length??0)>0){let Se=yu(c,u,re.attributes.JOINTS_0,4,Be),Le=yu(c,u,re.attributes.WEIGHTS_0,4,Be);je={sourceIndex:M.length,meshNode:j,meshBindWorld:G,skinIndex:j!==null?c.nodes?.[j]?.skin:void 0,positions:st,indices:ke,triangleMask:[],color:oe,texture:Pe,textureWrap:Oe,textureAlphaMode:Re,doubleSided:ve,uvs:Je??void 0,joints:Se,weights:Le},M.push(je)}for(let Se=0;Se+2<ke.length;Se+=3){let Le=ke[Se],we=ke[Se+1],_e=ke[Se+2],He=je?je.triangleMask.length:void 0;je&&je.triangleMask.push(!1);let Ie=xn[Le],en=xn[we],cn=xn[_e];if(!Ie||!en||!cn)continue;let nn;if(Je&&Pe){let ln=Je[Le],it=Je[we],un=Je[_e];ln&&it&&un&&(nn=[ln,it,un])}let Ke=`${V}:${le}:${j??"root"}`;S.push({v0:Ie,v1:en,v2:cn,color:oe,texture:Pe,textureWrap:Oe,textureAlphaMode:Re,doubleSided:ve,uvs:nn,simplifySourceVertexKeys:[`${Ke}:${Le}`,`${Ke}:${we}`,`${Ke}:${_e}`],source:je,sourceIndex:je?.sourceIndex,sourceTriangleIndex:He})}}}}function w(V,G,j){if(j.has(V)){x(`recursive-node:${V}`,`Skipped recursive node reference ${V} in glTF scene graph`);return}let J=c.nodes?.[V];if(!J)return;let le=Au(G,$0(J));typeof J.mesh=="number"&&R(J.mesh,le,V),j.add(V);for(let re of J.children??[])w(re,le,j);j.delete(V)}function F(V){let G=new Set,j=V.info.duration;for(let J of V.channels)for(let le of J.sampler.input)!Number.isFinite(le)||le<=0||j>0&&le>=j||G.add(le);return j>0&&(G.add(j*.25),G.add(j*.5),G.add(j*.75)),Array.from(G).sort((J,le)=>J-le).slice(0,128)}function D(V,G){let j=V.source;if(!j||V.sourceTriangleIndex===void 0||j.skinIndex!==void 0)return;let J=V.sourceTriangleIndex*3,le=j.indices[J],re=j.indices[J+1],pe=j.indices[J+2],be=le!==void 0?j.positions[le]:void 0,ie=re!==void 0?j.positions[re]:void 0,ae=pe!==void 0?j.positions[pe]:void 0;if(!be||!ie||!ae)return;let N=j.meshNode!==null?G[j.meshNode]??j.meshBindWorld:j.meshBindWorld;return{v0:Ir(N,be),v1:Ir(N,ie),v2:Ir(N,ae)}}function K(V,G){return V[0]===G[0]&&V[1]===G[1]&&V[2]===G[2]}function H(V){return K(V.v0,V.v1)||K(V.v0,V.v2)||K(V.v1,V.v2)}function Q(){return S.some(V=>!H(V))}function ne(){if(S.length===0||(c.animations?.length??0)===0||Q())return;let V=Vu(c,u);if(V.length===0)return;let G=(c.nodes??[]).map(J=>vu(J)),j=new Array(S.length);for(let J of V)for(let le of F(J)){let re=Lu(c,G,J,le);for(let pe=0;pe<S.length;pe++){if(j[pe])continue;let be=D(S[pe],re);be&&!H(be)&&(j[pe]=be)}}if(j.some(Boolean))for(let J=0;J<S.length;J++){let le=j[J];le&&(S[J].v0=le.v0,S[J].v1=le.v1,S[J].v2=le.v2)}}let ee=c.scene??0,ue=c.scenes?.[ee]?.nodes;if(ue&&ue.length>0)for(let V of ue)w(V,bt,new Set);else{(c.meshes?.length??0)===0&&x("no-gltf-meshes","No glTF meshes found");for(let V=0;V<(c.meshes?.length??0);V++)R(V,bt,null)}ne();let U=q0(d);if(S.length===0)return{polygons:[],objectUrls:d,dispose:U,warnings:p,metadata:{triangleCount:0,meshes:v,materials:I,sourceBytes:a}};let $=1/0,k=1/0,L=1/0,W=-1/0,Y=-1/0,se=-1/0;for(let V of S)for(let G of[V.v0,V.v1,V.v2])G[0]<$&&($=G[0]),G[0]>W&&(W=G[0]),G[1]<k&&(k=G[1]),G[1]>Y&&(Y=G[1]),G[2]<L&&(L=G[2]),G[2]>se&&(se=G[2]);let Z=Math.max(W-$,Y-k,se-L),B=Z>0?t/Z:1,q=n?.center,me=q===!0||q==="center",fe=me?($+W)*.5:$,z=me?(k+Y)*.5:k,te=me?(L+se)*.5:L,ce=V=>Math.round(V*1e3)/1e3,ye=n?.upAxis??"y",he=ye==="z"?([V,G,j])=>[ce((V-fe)*B),ce((G-z)*B),ce((j-te)*B)]:([V,G,j])=>[ce((j-te)*B),ce((V-fe)*B),ce((G-z)*B)],xe=ye==="z"?(V,G,j)=>{G[j]=ce((V[0]-fe)*B),G[j+1]=ce((V[1]-z)*B),G[j+2]=ce((V[2]-te)*B)}:(V,G,j)=>{G[j]=ce((V[2]-te)*B),G[j+1]=ce((V[0]-fe)*B),G[j+2]=ce((V[1]-z)*B)},ge=[],Ae=0;for(let V of S){let G=he(V.v0),j=he(V.v1),J=he(V.v2),le=j0(G,j,J);if(V.source&&V.sourceTriangleIndex!==void 0&&(V.source.triangleMask[V.sourceTriangleIndex]=!le),le){Ae++;continue}let re={vertices:[G,j,J],color:V.color};V.texture&&(re.texture=V.texture),V.texture&&V.textureWrap&&(re.textureWrap=V.textureWrap),V.texture&&V.textureAlphaMode&&(re.textureAlphaMode=V.textureAlphaMode),V.doubleSided&&(re.doubleSided=!0),V.uvs&&(re.uvs=V.uvs),V.simplifySourceVertexKeys&&(re.simplifySourceVertexKeys=V.simplifySourceVertexKeys),ge.push(re),T.push(V.sourceIndex!==void 0&&V.sourceTriangleIndex!==void 0?{sourceIndex:V.sourceIndex,triangleIndex:V.sourceTriangleIndex}:void 0)}ge.length===0&&Ae>0&&x("all-projected-triangles-degenerate","No non-degenerate glTF triangles remained after normalization");let Fe=Z0(c,u,M,T,he,xe);return{polygons:ge,animation:Fe,objectUrls:d,dispose:U,warnings:p,metadata:{triangleCount:ge.length,meshes:v,materials:I,animations:Fe?.clips,sourceBytes:a}}}function q0(e){let n=!1;return()=>{if(n)return;n=!0;let t=globalThis.URL;if(t?.revokeObjectURL)for(let o of e)try{t.revokeObjectURL(o)}catch{}}}var zu=Symbol("polycss.solidTextureBakedAnimationInfo"),wu=Symbol.for("polycss.animation.triangleFrameSource"),Q0=16*1024*1024,J0=2,eh=32,nh=2,th=.08,oh=8,rh=128,Nu=32,sh=.045,ih=10,Pt=6,ah=16;function na(e){return e.material?.texture??e.texture}function ch(){let e=globalThis;return typeof e.Image!="function"||typeof e.document?.createElement!="function"?null:{Image:e.Image,createCanvas(){return e.document.createElement("canvas")}}}function lh(e,n){return new Promise((t,o)=>{let r=new n,s=!1,i=a=>{s||(s=!0,a())};r.decoding="async",r.onload=()=>i(()=>t(r)),r.onerror=()=>i(()=>o(new Error(`texture load failed: ${e}`))),r.src=e})}async function uh(e,n,t){try{let o=await lh(e,n.Image),r=Math.max(0,Math.floor(o.naturalWidth||o.width||0)),s=Math.max(0,Math.floor(o.naturalHeight||o.height||0));if(r<=0||s<=0||r*s>t)return null;let i=n.createCanvas();i.width=r,i.height=s;let a=i.getContext("2d",{willReadFrequently:!0});if(!a)return null;a.drawImage(o,0,0,r,s);let c=a.getImageData(0,0,r,s).data;return{width:r,height:s,data:c,lowDetail:fh(r,s,c)}}catch{return null}}function Du(e,n,t,o,r,s){let i=(o*n+t)*4,a=(s*n+r)*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 fh(e,n,t){let o=Math.max(1,Math.floor(Math.max(e,n)/rh)),r=0,s=0,i=0;for(let a=0;a<n;a+=o)for(let c=0;c<e;c+=o){if(c+o<e){let l=Du(t,e,c,a,c+o,a);i+=l,r++,l>Nu&&s++}if(a+o<n){let l=Du(t,e,c,a,c,a+o);i+=l,r++,l>Nu&&s++}}return r>0&&s/r<=sh&&i/r<=ih}function Fu(e,n,t){return Math.max(n,Math.min(t,e))}function Bu(e,n){if(!Number.isFinite(e))return null;switch(n){case"repeat":return e-Math.floor(e);case"mirrored-repeat":{let t=Math.floor(e),o=e-t;return Math.abs(t%2)===1?1-o:o}default:return Math.max(0,Math.min(1,e))}}function mh(e,n,t){let o=n[0],r=1-n[1],s=Bu(o,t?.s),i=Bu(r,t?.t);if(s===null||i===null)return null;let a=Fu(Math.floor(s*e.width),0,e.width-1),l=(Fu(Math.floor(i*e.height),0,e.height-1)*e.width+a)*4;return{r:e.data[l]??0,g:e.data[l+1]??0,b:e.data[l+2]??0,a:e.data[l+3]??255}}function Ln(e,n,t,o,r,s){return[e[0]*o+n[0]*r+t[0]*s,e[1]*o+n[1]*r+t[1]*s]}function dh(e){let[n,t,o]=e;return[Ln(n,t,o,1/3,1/3,1/3),Ln(n,t,o,.8,.1,.1),Ln(n,t,o,.1,.8,.1),Ln(n,t,o,.1,.1,.8),Ln(n,t,o,.45,.45,.1),Ln(n,t,o,.45,.1,.45),Ln(n,t,o,.1,.45,.45)]}function ph(e){let[n,t,o]=e,r=dh(e);for(let s=1;s<Pt;s++)for(let i=1;i<Pt-s;i++){let a=Pt-s-i;a<=0||r.push(Ln(n,t,o,s/Pt,i/Pt,a/Pt))}return r}function Gu(e){if(e.textureTriangles?.length)return e.textureTriangles;let n=e.uvs;if(!n||n.length!==e.vertices.length||n.length<3)return[];let t=[];for(let o=1;o+1<n.length;o++)t.push({uvs:[n[0],n[o],n[o+1]]});return t}function ta(e,n,t){return Math.abs(e.r-n.r)<=t&&Math.abs(e.g-n.g)<=t&&Math.abs(e.b-n.b)<=t&&Math.abs(e.a-n.a)<=t}function Vr(e){let n=o=>Math.round(Math.max(0,Math.min(255,o))).toString(16).padStart(2,"0");if(e.a>=255)return`#${n(e.r)}${n(e.g)}${n(e.b)}`;let t=Math.round(Math.max(0,Math.min(255,e.a))/255*1e3)/1e3;return`rgba(${Math.round(e.r)}, ${Math.round(e.g)}, ${Math.round(e.b)}, ${t})`}function $u(e){if(!e)return null;let n=e.trim().toLowerCase(),t=n.match(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);if(t){let s=t[1],i=a=>{let c=s.length<=4?s[a]+s[a]:s.slice(a*2,a*2+2);return parseInt(c,16)};return{r:i(0),g:i(1),b:i(2),a:s.length===4||s.length===8?i(3):255}}let o=n.match(/^rgba?\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)(?:\s*,\s*([0-9.]+)\s*)?\)$/);if(!o)return null;let r=o[4]===void 0?255:Number(o[4])<=1?Number(o[4])*255:Number(o[4]);return{r:Number(o[1]),g:Number(o[2]),b:Number(o[3]),a:r}}function gh(){return{min:{r:255,g:255,b:255,a:255},max:{r:0,g:0,b:0,a:0},sum:{r:0,g:0,b:0,a:0},colors:new Map,count:0}}function Xu(e){return`${e.r},${e.g},${e.b},${e.a}`}function ku(e,n){e.min.r=Math.min(e.min.r,n.r),e.min.g=Math.min(e.min.g,n.g),e.min.b=Math.min(e.min.b,n.b),e.min.a=Math.min(e.min.a,n.a),e.max.r=Math.max(e.max.r,n.r),e.max.g=Math.max(e.max.g,n.g),e.max.b=Math.max(e.max.b,n.b),e.max.a=Math.max(e.max.a,n.a),e.sum.r+=n.r,e.sum.g+=n.g,e.sum.b+=n.b,e.sum.a+=n.a;let t=Xu(n),o=e.colors.get(t);e.colors.set(t,{color:n,count:(o?.count??0)+1}),e.count++}function Uu(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 hh(e){if(e.count<oh||e.colors.size<=1||e.colors.size>nh)return null;let n=null;for(let r of e.colors.values())(!n||r.count>n.count)&&(n=r);if(!n)return null;let t=e.count-n.count,o=Math.max(1,Math.floor(e.count*th));return t<=o?n.color:null}function yh(e,n,t,o){let r=Gu(e);if(r.length===0)return null;let s=gh(),i=e.textureAlphaMode==="opaque"?$u(e.color):null;for(let a of r)for(let c of ph(a.uvs)){let l=mh(n,c,e.textureWrap);if(!l)return null;if(e.textureAlphaMode==="opaque"&&l.a===0&&e.color){i&&ku(s,{...i,a:255});continue}e.textureAlphaMode==="opaque"&&(l.a=255),ku(s,l)}if(s.count===0)return e.textureAlphaMode==="opaque"&&e.color?e.color:null;if(e.textureAlphaMode==="opaque"&&s.max.a===0&&e.color)return e.color;if(ta(s.min,s.max,t))return Vr(Uu(s));if(!o){let a=hh(s);if(a)return Vr(a)}return o||!n.lowDetail||!ta(s.min,s.max,eh)?null:Vr(Uu(s))}function Yu(e,n){let{texture:t,textureWrap:o,textureAlphaMode:r,material:s,uvs:i,textureTriangles:a,...c}=e,l=e.uvs?.length===e.vertices.length?e.uvs.map(d=>`uv:${Math.round(d[0]*1e5)},${Math.round(d[1]*1e5)}`):void 0,u=l??e.simplifyVertexKeys,f=e.simplifySourceVertexKeys?.length===e.vertices.length?e.simplifySourceVertexKeys.map((d,m)=>l?`${d}|${l[m]}`:d):e.simplifySourceVertexKeys;return{...c,color:n,...u?{simplifyVertexKeys:u}:{},...f?{simplifySourceVertexKeys:f}:{}}}function xh(e,n){if(n.length<2)return e;let t=[],o=new Map;for(let l of n){let u=$u(e[l]?.color);if(!u||u.a<255)continue;let f=Xu(u);t.push({index:l,key:f});let d=o.get(f);d?d.count+=1:o.set(f,{color:u,count:1})}if(o.size<2)return e;let r=[],s=new Map,i=[...o.entries()].sort((l,u)=>u[1].count-l[1].count||l[0].localeCompare(u[0]));for(let[l,u]of i){let f=null;for(let d of r)if(ta(u.color,d,ah)){f=d;break}f||(f=u.color,r.push(f)),s.set(l,Vr(f))}let a=!1,c=e.slice();for(let l of t){let u=s.get(l.key),f=e[l.index];!u||!f||f.color===u||(c[l.index]={...f,color:u},a=!0)}return a?c:e}function bh(e,n){let t=e.slice();for(let o=0;o<n.length;o++){let r=n[o],s=e[r.index];s&&(t[r.index]=Yu(s,r.color))}return t}function Wu(e){return e?e[zu]:void 0}async function ju(e,n={}){if(n.enabled===!1)return null;let t=ch();if(!t)return null;let o=e.filter(c=>na(c)&&Gu(c).length>0);if(o.length===0)return null;let r=n.maxTexturePixels??Q0,s=new Map;await Promise.all(Array.from(new Set(o.map(c=>na(c)))).map(async c=>{s.set(c,await uh(c,t,r))}));let i=n.colorTolerance??J0,a=n.colorTolerance!==void 0;return{bake(c){let l=!1,u=[],f=c.map((d,m)=>{let p=na(d);if(!p)return d;let h=s.get(p);if(!h)return d;let g=yh(d,h,i,a);return g?(l=!0,u.push(m),Yu(d,g)):d});return l?{polygons:xh(f,u),changed:!0,bakedIndices:u}:{polygons:c,changed:!1,bakedIndices:u}}}}async function Hu(e,n={}){let t=await ju(e,n);return t?t.bake(e).polygons:e}async function Lr(e,n={}){let t=await ju(e.polygons,n);if(!t)return e;let o=t.bake(e.polygons);if(!o.changed)return e;let r=[];for(let a of o.bakedIndices){let l=o.polygons[a].color;l&&r.push({index:a,color:l})}let s=e.animation,i=s&&{...s,sample(a,c){return bh(s.sample(a,c),r)}};if(i&&s){let a=s[wu];if(a){let c=new Map(r.map(f=>[f.index,f.color])),l=[],u={polygonCount:0,vertices:new Float64Array(0),colors:l,solidTriangles:!1};i[wu]=(f,d)=>{let m=a(f,d);if(!m)return m;l.length<m.polygonCount&&(l=new Array(m.polygonCount),u.colors=l);let p=m.textureFlags,h=m.solidTriangles===!0||!!p;for(let g=0;g<m.polygonCount;g++){let y=c.get(g);l[g]=y??m.colors?.[g],p?.[g]&&!y&&(h=!1)}return u.polygonCount=m.polygonCount,u.vertices=m.vertices,u.textureFlags=p,u.solidTriangles=h,u}}i[zu]={source:s,bakedColorEntries:r}}return{...e,polygons:o.polygons,animation:i}}var Ku=Symbol.for("polycss.animation.triangleFrameSource");function Ph(e,n){let t=e[n];if(!t)return null;let o=[];for(let r=0;r<t.vertices.length;r++)o.push(n,r);return{rest:t,originalIndex:n,sourceRefs:o}}function Mh(e){let n=fn(e);if(n.length>=e.length)return[];let t=new Map;e.forEach((r,s)=>t.set(r,s));let o=[];for(let r of n){let s=t.get(r);if(s===void 0)return[];let i=Ph(e,s);if(!i)return[];o.push(i)}return o}function oa(e,n,t=!1){let o=new Array(n.length);for(let r=0;r<n.length;r++){let s=n[r],i=s.sourceRefs,a=new Array(i.length/2);for(let l=0,u=0;l<i.length;l+=2,u++){let d=e[i[l]]?.vertices[i[l+1]];if(!d)return e;let m=d[0],p=d[1],h=d[2];if(!Number.isFinite(m)||!Number.isFinite(p)||!Number.isFinite(h))return e;a[u]=[m,p,h]}let c=t?s.rest.color:e[i[0]??-1]?.color??s.rest.color;o[r]={...s.rest,vertices:a,color:c}}return o}function Sh(e,n){let t=new Map;return n.forEach((o,r)=>{t.set(o,r)}),e.map((o,r)=>({...o,sourceRefs:o.sourceRefs.map((s,i)=>i%2===0?t.get(s)??-1:s)}))}function Ah(e,n){if(e.length<n.length)return null;let t=new Array(n.length);for(let o=0;o<n.length;o++){let r=e[o],s=n[o];if(!r||r.vertices.length!==s.rest.vertices.length)return null;t[o]={...s.rest,vertices:r.vertices,color:s.rest.color}}return t}function Ch(e){return!!(e.texture||e.material?.texture)}function Zu(e,n={}){if(!e.animation||e.polygons.length===0||n.meshResolution==="lossless")return e;let t=Mh(e.polygons);if(t.length===0||t.length>=e.polygons.length)return e;let o=Wu(e.animation),r=o?.source??e.animation,s=t.map(x=>x.originalIndex),i=Ru(r,s),a=i??r,c=i?Sh(t,s):t,l=t.map(x=>x.rest.color),u=t.every(x=>x.rest.vertices.length===3&&!Ch(x.rest)),f=i&&!o&&t.length===s.length&&t.every((x,b)=>x.rest.vertices.length===3&&x.sourceRefs.length===6&&x.sourceRefs[0]===s[b]&&x.sourceRefs[2]===s[b]&&x.sourceRefs[4]===s[b]),d=a[Ku],m=new Float64Array(t.length*9),p=l.slice(),h=new Array(t.length).fill(!1),g={polygonCount:t.length,vertices:m,colors:p,textureFlags:h,solidTriangles:u},y={...e.animation,sample(x,b){let P=a.sample(x,b);return f?P:i&&o?u&&t.length===s.length?Ah(P,t)??oa(P,c,!0):oa(P,c,!0):oa(P,c,!!o)}};return d&&u&&(y[Ku]=(x,b)=>{let P=d(x,b);if(!P)return P;if(i)return P.polygonCount<t.length||P.vertices.length<t.length*9?null:(g.polygonCount=t.length,g.vertices=P.vertices,g.colors=o?l:P.colors,g.textureFlags=o?h:P.textureFlags,g.solidTriangles=o?u:P.solidTriangles===!0,g);m.length<t.length*9&&(m=new Float64Array(t.length*9),g.vertices=m),p.length<t.length&&(p=new Array(t.length),g.colors=p),h.length<t.length&&(h=new Array(t.length).fill(!1),g.textureFlags=h);let A=u&&P.solidTriangles!==!1;for(let C=0;C<c.length;C++){let E=c[C],S=E.sourceRefs;for(let v=0,I=0;v<S.length;v+=2,I++){let _=S[v],O=S[v+1];if(_<0||_>=P.polygonCount)return null;let R=_*9+O*3,w=C*9+I*3;if(R+2>=P.vertices.length)return null;m[w]=P.vertices[R],m[w+1]=P.vertices[R+1],m[w+2]=P.vertices[R+2]}let M=S[0],T=!!P.textureFlags?.[M];h[C]=T,p[C]=o?E.rest.color:P.colors?.[M]??E.rest.color,!o&&T&&(A=!1)}return g.polygonCount=t.length,g.vertices=m,g.colors=p,g.textureFlags=h,g.solidTriangles=o?u:A,g}),{...e,polygons:t.map(x=>x.rest),animation:y,metadata:{...e.metadata,triangleCount:t.length}}}var Th=/^[0-9A-Fa-f]{6}$/,Eh=["#3b82f6","#ef4444","#22c55e","#eab308","#a855f7","#06b6d4","#f97316","#ec4899"];function vh(e){let n=[],t="";for(let o of e.split(`
|
|
2
|
+
`)){let r=o.endsWith("\r")?o.slice(0,-1):o;if(r.endsWith("\\")){t+=`${r.slice(0,-1).trimEnd()} `;continue}n.push(t+r),t=""}return t&&n.push(t.trimEnd()),n}function wr(e,n){let t=n?.targetSize??60,o=n?.defaultColor??"#888888",r=n?.palette??Eh,s=n?.materialColors??{},i=n?.materialTextures??{},a=[],c=[],l=[],u=[],f=new Map,d=[],m=new Set,p=o,h,g=n?.includeObjects?new Set(n.includeObjects):null,y=n?.excludeObjects?new Set(n.excludeObjects):null,x=null,b=()=>x===null?g===null:!(g&&!g.has(x)||y&&y.has(x)),P=U=>U in s?s[U]:Th.test(U)?`#${U}`:(f.has(U)||(f.set(U,r[u.length%r.length]),u.push(U)),f.get(U)),A=(U,$)=>{m.has(U)||(m.add(U),d.push($))},C=(U,$)=>{let k=parseInt(U,10);return Number.isFinite(k)?k<0?$+k:k-1:NaN},E=vh(e);for(let U of E)if(!(U.length===0||U.charCodeAt(0)===35)){if(U.startsWith("v ")){let $=U.trim().split(/\s+/);a.push([parseFloat($[1]),parseFloat($[2]),parseFloat($[3])])}else if(U.startsWith("vt ")){let $=U.trim().split(/\s+/);c.push([parseFloat($[1]),parseFloat($[2])])}else if(U.startsWith("o "))x=U.trim().slice(2).trim();else if(U.startsWith("usemtl ")){let $=U.trim().split(/\s+/)[1];p=P($),h=i[$]}else if(U.startsWith("p "))b()&&A("unsupported-point-elements","Skipped OBJ point elements; PolyCSS only renders face polygons");else if(U.startsWith("l "))b()&&A("unsupported-line-elements","Skipped OBJ line elements; PolyCSS only renders face polygons");else if(U.startsWith("f ")){if(!b())continue;let $=U.trim().split(/\s+/).slice(1),k=[],L=[];for(let W of $){let Y=W.split("/");k.push(C(Y[0],a.length));let se=Y[1];if(se&&se.length>0){let Z=C(se,c.length);L.push(Number.isFinite(Z)?Z:null)}else L.push(null)}l.push({idx:k,uvIdx:L,color:p,texture:h})}}if(a.length===0||l.length===0)return Ih(u,e.length,d);let S=new Set;for(let U of l)for(let $ of U.idx)S.add($);let M=1/0,T=1/0,v=1/0,I=-1/0,_=-1/0,O=-1/0;for(let U of S){let $=a[U];$&&($[0]<M&&(M=$[0]),$[0]>I&&(I=$[0]),$[1]<T&&(T=$[1]),$[1]>_&&(_=$[1]),$[2]<v&&(v=$[2]),$[2]>O&&(O=$[2]))}let R=Math.max(I-M,_-T,O-v),w=R>0?t/R:1,F=n?.center,D=F===!0||F==="center",K=D?(M+I)*.5:M,H=D?(T+_)*.5:T,Q=D?(v+O)*.5:v,ne=U=>Math.round(U*1e3)/1e3,ee=a.map(([U,$,k])=>[ne((k-Q)*w),ne((U-K)*w),ne(($-H)*w)]),ue=[];for(let{idx:U,uvIdx:$,color:k,texture:L}of l)for(let W=1;W<U.length-1;W++){let Y=U[0],se=U[W],Z=U[W+1],B=ee[Y],q=ee[se],me=ee[Z];if(!B||!q||!me||B[0]===q[0]&&B[1]===q[1]&&B[2]===q[2]||B[0]===me[0]&&B[1]===me[1]&&B[2]===me[2]||q[0]===me[0]&&q[1]===me[1]&&q[2]===me[2])continue;let fe;if(L){let te=$[0],ce=$[W],ye=$[W+1];if(te!=null&&ce!=null&&ye!=null){let he=c[te],xe=c[ce],ge=c[ye];he&&xe&&ge&&(fe=[he,xe,ge])}}let z={vertices:[B,q,me],color:k};L&&(z.texture=L),fe&&(z.uvs=fe),ue.push(z)}return{polygons:ue,objectUrls:[],dispose:()=>{},warnings:d,metadata:{triangleCount:ue.length,materials:u,sourceBytes:e.length}}}function Ih(e,n,t=[]){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:t,metadata:{triangleCount:0,materials:e,sourceBytes:n}}}var ra=e=>Math.round(Math.max(0,Math.min(1,e))*255).toString(16).padStart(2,"0");function Nr(e){let n={},t={},o=null;for(let r of e.split(`
|
|
3
|
+
`)){let s=r.trim();if(!(s.length===0||s.charCodeAt(0)===35)){if(s.startsWith("newmtl ")){o=s.slice(7).trim();continue}if(o){if(s.startsWith("Kd ")){let i=s.split(/\s+/),a=parseFloat(i[1]),c=parseFloat(i[2]),l=parseFloat(i[3]);Number.isFinite(a)&&Number.isFinite(c)&&Number.isFinite(l)&&(n[o]=`#${ra(a)}${ra(c)}${ra(l)}`)}else if(s.startsWith("map_Kd ")){let i=s.split(/\s+/),a=i[i.length-1]?.replace(/\\+/g,"/");a&&a!=="map_Kd"&&(t[o]=a)}}}}return{colors:n,textures:t}}var _h=[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 Oh(e){let n=e>>0&255,t=e>>8&255,o=e>>16&255;return[n,t,o]}function sa(e){return(e&255).toString(16).padStart(2,"0")}function Ju(e,n,t){return`#${sa(e)}${sa(n)}${sa(t)}`}function Rh(e,n,t,o){if(o>=255)return Ju(e,n,t);let r=Math.round(Math.max(0,Math.min(255,o))/255*1e3)/1e3;return`rgba(${e}, ${n}, ${t}, ${r})`}function ef(e){let n=e.match(/^#([0-9a-f]{6})$/i);if(n){let o=n[1];return{rgb:[parseInt(o.slice(0,2),16),parseInt(o.slice(2,4),16),parseInt(o.slice(4,6),16)],alpha:1}}let t=e.match(/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/i);return t?{rgb:[Number(t[1]),Number(t[2]),Number(t[3])],alpha:Number(t[4])}:null}function nf(e,n){return Math.hypot(e.rgb[0]-n.rgb[0],e.rgb[1]-n.rgb[1],e.rgb[2]-n.rgb[2])}function qu(e){let n=e.rgb[0]/255,t=e.rgb[1]/255,o=e.rgb[2]/255,r=Math.max(n,t,o),s=Math.min(n,t,o),i=r-s,a=0;return i!==0&&(r===n?a=60*((t-o)/i%6):r===t?a=60*((o-n)/i+2):a=60*((n-t)/i+4)),a<0&&(a+=360),{hue:a,saturation:r===0?0:i/r,value:r}}function Vh(e,n){let t=Math.abs(e-n)%360;return t>180?360-t:t}function tf(e,n){if(e.alpha<1||n.alpha<1)return!1;let t=qu(e),o=qu(n),r=t.saturation<.08,s=o.saturation<.08;if(r||s)return r===s;let i=Math.min(t.value,o.value)<.16?35:24;return Vh(t.hue,o.hue)<=i}function Lh(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return null;let o=new Map;for(let a of e.keys()){let c=ef(a);c&&o.set(a,c)}let r=[],s=new Map,i=Array.from(e.entries()).sort((a,c)=>c[1]-a[1]||a[0].localeCompare(c[0]));for(let[a]of i){let c=o.get(a);if(!c){s.set(a,a);continue}let l=null;for(let u of r){if(!tf(c,u.parsed))continue;let f=nf(c,u.parsed);f>t||(!l||f<l.distance)&&(l={color:u.color,distance:f})}l?s.set(a,l.color):(r.push({color:a,parsed:c}),s.set(a,a))}return s}var wh=[[1,0],[-1,0],[0,1],[0,-1]];function of(e,n){return`${e},${n}`}function Nh(e){let n=e.indexOf(",");return[Number(e.slice(0,n)),Number(e.slice(n+1))]}function Dh(e,n,t){return e<=4||(n===1||t===1)&&e<=12?!0:Math.min(n,t)<=2&&Math.max(n,t)>=4&&e<=18}function Fh(e,n){let t=n??0;if(!Number.isFinite(t)||t<=0)return;let o=new Map,r=s=>(o.has(s)||o.set(s,ef(s)),o.get(s)??null);for(let s of e.values()){if(s.size<3)continue;let i=new Map;for(let l of s.values())i.set(l,(i.get(l)??0)+1);let a=new Set,c=[];for(let[l,u]of s){if(a.has(l))continue;let f=[l],d=[],m=new Map,p=1/0,h=-1/0,g=1/0,y=-1/0;for(a.add(l);f.length>0;){let C=f.pop();d.push(C);let[E,S]=Nh(C);p=Math.min(p,E),h=Math.max(h,E),g=Math.min(g,S),y=Math.max(y,S);for(let[M,T]of wh){let v=of(E+M,S+T),I=s.get(v);if(I){if(I===u){a.has(v)||(a.add(v),f.push(v));continue}m.set(I,(m.get(I)??0)+1)}}}let x=h-p+1,b=y-g+1;if(!Dh(d.length,x,b)||m.size===0)continue;let P=r(u);if(!P)continue;let A=null;for(let[C,E]of m){let S=i.get(C)??0;if(S<=d.length)continue;let M=r(C);if(!M||!tf(P,M))continue;let T=nf(P,M);if(T>t)continue;let v={color:C,contacts:E,distance:T,count:S};(!A||v.contacts>A.contacts||v.contacts===A.contacts&&v.count>A.count||v.contacts===A.contacts&&v.count===A.count&&v.distance<A.distance||v.contacts===A.contacts&&v.count===A.count&&v.distance===A.distance&&v.color.localeCompare(A.color)<0)&&(A=v)}if(A)for(let C of d)c.push([C,A.color])}for(let[l,u]of c)s.set(l,u)}}var Bh=542658390,kh=new Set(["nTRN","nGRP","nSHP"]),Uh="Skipped MagicaVoxel scene graph transforms; models were flattened into one grid";function Dr(e,n){let t=n?.targetSize??60,o=e.byteLength;if(e.byteLength<8)return Mt(o,["parseVox: buffer too small to be a valid .vox file"]);let r=new DataView(e);if(r.getUint32(0,!0)!==Bh)return Mt(o,["parseVox: not a .vox file (bad magic)"]);if(e.byteLength<20)return Mt(o,["parseVox: buffer too small for MAIN chunk"]);if(Qu(r,8)!=="MAIN")return Mt(o,["parseVox: expected MAIN chunk at offset 8"]);let c=20+r.getUint32(12,!0),l=c+r.getUint32(16,!0),u=[],f=[],d=[],m=null,p=k=>{d.includes(k)||d.push(k)};for(;c<l&&c+12<=e.byteLength;){let k=Qu(r,c),L=r.getUint32(c+4,!0),W=r.getUint32(c+8,!0),Y=c+12,se=Y+L+W;if(k==="SIZE"){if(L>=12&&Y+12<=e.byteLength){let Z=r.getUint32(Y,!0),B=r.getUint32(Y+4,!0),q=r.getUint32(Y+8,!0);u.push({sx:Z,sy:B,sz:q})}}else if(k==="XYZI"){if(L>=4&&Y+4<=e.byteLength){let Z=r.getUint32(Y,!0),B=[],q=Math.min(Z,Math.floor((L-4)/4));for(let me=0;me<q;me++){let fe=Y+4+me*4;B.push({x:r.getUint8(fe),y:r.getUint8(fe+1),z:r.getUint8(fe+2),colorIndex:r.getUint8(fe+3)})}f.push(B)}}else if(k==="RGBA"&&L>=1024&&Y+1024<=e.byteLength){m=[];for(let Z=0;Z<256;Z++){let B=Y+Z*4,q=r.getUint8(B),me=r.getUint8(B+1),fe=r.getUint8(B+2),z=r.getUint8(B+3);m.push(Rh(q,me,fe,z))}}kh.has(k)&&p(Uh),c=se}let h=[],g=new Set;for(let k of f)for(let L of k){if(L.colorIndex===0)continue;let W=`${L.x},${L.y},${L.z}`;g.has(W)||(g.add(W),h.push(L))}if(h.length===0)return Mt(o,d);let y=new Map,x=k=>{let L=y.get(k);if(L)return L;let W=k-1,Y;if(m!==null)Y=m[W]??"#888888";else{let se=_h[k]??0,[Z,B,q]=Oh(se);Y=Ju(Z,B,q)}return y.set(k,Y),Y},b=new Map;for(let k of h){let L=x(k.colorIndex);b.set(L,(b.get(L)??0)+1)}let P=Lh(b,n?.paletteMergeDistance),A=k=>{let L=x(k);return P?.get(L)??L},C=(k,L,W)=>g.has(`${k},${L},${W}`),E=new Map,S=(k,L,W,Y,se)=>{let Z=`${k}:${L}`,B=E.get(Z);B||(B=new Map,E.set(Z,B)),B.set(of(W,Y),se)};for(let k of h){let{x:L,y:W,z:Y}=k,se=A(k.colorIndex);C(L+1,W,Y)||S(0,L+1,W,Y,se),C(L-1,W,Y)||S(1,L,W,Y,se),C(L,W+1,Y)||S(2,W+1,L,Y,se),C(L,W-1,Y)||S(3,W,L,Y,se),C(L,W,Y+1)||S(4,Y+1,L,W,se),C(L,W,Y-1)||S(5,Y,L,W,se)}Fh(E,n?.colorRegionMergeDistance);let M=new Map;for(let[k,L]of E)for(let[W,Y]of L){let se=`${k}:${Y}`,Z=M.get(se);Z||(Z=new Set,M.set(se,Z)),Z.add(W)}function T(k){let L=new Set,W=[],Y=1/0,se=-1/0,Z=1/0,B=-1/0;for(let q of k){let[me,fe]=q.split(","),z=+me,te=+fe;z<Y&&(Y=z),z>se&&(se=z),te<Z&&(Z=te),te>B&&(B=te)}for(let q=Z;q<=B;q++)for(let me=Y;me<=se;me++){let fe=`${me},${q}`;if(!k.has(fe)||L.has(fe))continue;let z=1;for(;me+z<=se;){let ce=`${me+z},${q}`;if(!k.has(ce)||L.has(ce))break;z++}let te=1;e:for(;q+te<=B;){for(let ce=0;ce<z;ce++){let ye=`${me+ce},${q+te}`;if(!k.has(ye)||L.has(ye))break e}te++}for(let ce=0;ce<te;ce++)for(let ye=0;ye<z;ye++)L.add(`${me+ye},${q+ce}`);W.push({u:me,v:q,w:z,h:te})}return W}let v=(k,L,W,Y,se,Z)=>{let B=W+se,q=Y+Z;switch(k){case 0:return[[L,W,Y],[L,B,Y],[L,B,q],[L,W,q]];case 1:return[[L,B,Y],[L,W,Y],[L,W,q],[L,B,q]];case 2:return[[W,L,Y],[W,L,q],[B,L,q],[B,L,Y]];case 3:return[[B,L,Y],[B,L,q],[W,L,q],[W,L,Y]];case 4:return[[W,Y,L],[B,Y,L],[B,q,L],[W,q,L]];default:return[[W,q,L],[B,q,L],[B,Y,L],[W,Y,L]]}},I=[];for(let[k,L]of M){let W=k.indexOf(":"),Y=k.indexOf(":",W+1),se=+k.slice(0,W),Z=+k.slice(W+1,Y),B=k.slice(Y+1);for(let{u:q,v:me,w:fe,h:z}of T(L))I.push({vertices:v(se,Z,q,me,fe,z),color:B})}if(I.length===0)return Mt(o,d);let _=1/0,O=1/0,R=1/0,w=-1/0,F=-1/0,D=-1/0;for(let k of I)for(let L of k.vertices)L[0]<_&&(_=L[0]),L[0]>w&&(w=L[0]),L[1]<O&&(O=L[1]),L[1]>F&&(F=L[1]),L[2]<R&&(R=L[2]),L[2]>D&&(D=L[2]);let K=Math.max(w-_,F-O,D-R),H=K>0?t/K:1,Q=H*50,ne=Number.isFinite(Q)&&Q>0?Math.max(1,Math.round(Q))/50:H,ee={kind:"magica-vox",cells:h.map(k=>({x:F-(k.y+1),y:k.x-_,z:k.z-R,color:A(k.colorIndex)})),rows:Math.max(0,F-O),cols:Math.max(0,w-_),depth:Math.max(0,D-R),scale:ne,sourceBytes:o},ue=k=>Math.round(k*1e3)/1e3,U=k=>[ue((k[0]-_)*ne),ue((k[1]-O)*ne),ue((k[2]-R)*ne)],$=I.map(({vertices:k,color:L})=>({vertices:k.map(U),color:L}));return{polygons:$,voxelSource:ee,objectUrls:[],dispose:()=>{},warnings:d,metadata:{triangleCount:$.length,sourceBytes:o,voxelCount:h.length}}}function Qu(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 Mt(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:n,metadata:{triangleCount:0,sourceBytes:e}}}var zh="#888888";var Gh=/^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:[eE][+-]?\d+)?$/,Qn="COLOR=";function af(e,n,t){let o=n??t;if(!Number.isFinite(o))throw new Error(`parseStl: ${e} must be finite`);return o}function $h(e,n,t){let o=af(e,n,t);if(o<=0)throw new Error(`parseStl: ${e} must be greater than 0`);return o}function Xh(e){if(e===void 0||e==="z"||e==="y")return e??"z";throw new Error('parseStl: upAxis must be "z" or "y"')}function Yh(e){let n=e??zh;if(typeof n!="string"||n.trim().length===0)throw new Error("parseStl: defaultColor must be a non-empty string");return n}function Wh(e){if(typeof e=="string")return null;if(e instanceof Uint8Array)return new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength));if(e instanceof ArrayBuffer)return new Uint8Array(e);throw new Error("parseStl: source must be an ArrayBuffer, Uint8Array, or ASCII string")}function cf(e){let n=globalThis.TextDecoder;return new n().decode(e)}function Fr(e,n){return[e.getFloat32(n,!0),e.getFloat32(n+4,!0),e.getFloat32(n+8,!0)]}function Br(e){return Number.isFinite(e[0])&&Number.isFinite(e[1])&&Number.isFinite(e[2])}function rf(e){let n=e.trim().split(/\s+/);if(n.length!==3||!n.every(o=>Gh.test(o)))return null;let t=[Number(n[0]),Number(n[1]),Number(n[2])];return Br(t)?t:null}function jh(e){let n="";for(let o of e)o!==0&&(n+=o>=32&&o<=126?String.fromCharCode(o):" ");return n.replace(/\s+/g," ").trim()||void 0}function ia(e){return Math.max(0,Math.min(255,e)).toString(16).padStart(2,"0")}function lf(e,n,t,o=255){if(o>=255)return`#${ia(e)}${ia(n)}${ia(t)}`;let r=Math.round(o/255*1e3)/1e3;return`rgba(${e}, ${n}, ${t}, ${r})`}function aa(e){return Math.round(e/31*255)}function Hh(e){for(let n=0;n<80-Qn.length-4;n+=1){let t=!0;for(let a=0;a<Qn.length;a+=1)if(e.getUint8(n+a)!==Qn.charCodeAt(a)){t=!1;break}if(!t)continue;let o=e.getUint8(n+Qn.length),r=e.getUint8(n+Qn.length+1),s=e.getUint8(n+Qn.length+2),i=e.getUint8(n+Qn.length+3);return{defaultColor:lf(o,r,s,i),alpha:Math.round(i/255*1e3)/1e3,alphaByte:i}}return null}function Kh(e,n){if((e&32768)!==0)return{color:n.defaultColor,usesDefaultColor:!0};let t=aa(e&31),o=aa(e>>5&31),r=aa(e>>10&31);return{color:lf(t,o,r,n.alphaByte),usesDefaultColor:!1}}function Zh(e){let n=[],t=/^\s*solid(?:\s+([^\r\n]*?))?\s*$/gim,o=/^\s*endsolid\b[^\r\n]*$/gim,r;for(;r=t.exec(e);)n.push({index:r.index,kind:"start",name:(r[1]??"").trim()});for(;r=o.exec(e);)n.push({index:r.index,kind:"end"});return n.sort((s,i)=>s.index-i.index||(s.kind==="end"?-1:1))}function qh(e){let n=Math.min(e.byteLength,1024);for(let t=84;t<n;t+=1){let o=e[t];if(o===0||o<9||o>13&&o<32||o>126)return!0}return!1}function Qh(e){let n=cf(e.subarray(0,Math.min(e.byteLength,512))).trimStart().toLowerCase();return!n.startsWith("solid")&&!n.startsWith("facet")?!1:!qh(e)}function Jh(e){if(e.byteLength<84)return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(80,!0),o=84+t*50;if(o===e.byteLength)return{triangleCount:t,declaredTriangleCount:t,trailingBytes:0};if(Qh(e))return null;if(o<e.byteLength)return{triangleCount:t,declaredTriangleCount:t,trailingBytes:e.byteLength-o};let r=e.byteLength-84;return r>0&&r%50===0?{triangleCount:r/50,declaredTriangleCount:t,trailingBytes:0}:null}function ey(e,n,t){let o=new DataView(e.buffer,e.byteOffset,e.byteLength),{triangleCount:r,declaredTriangleCount:s,trailingBytes:i}=n;s!==r&&t.push(`parseStl: binary STL declared ${s} triangles but contains ${r} complete triangle record${r===1?"":"s"}`),i>0&&t.push(`parseStl: ignored ${i} trailing binary byte${i===1?"":"s"}`);let a=jh(e.subarray(0,80)),c=Hh(o),l=0,u=0,f=[];for(let d=0,m=84;d<r;d+=1,m+=50){let p=Fr(o,m),h=[Fr(o,m+12),Fr(o,m+24),Fr(o,m+36)];if(!h.every(Br))throw new Error(`parseStl: non-finite coordinate in binary triangle ${d}`);let g=o.getUint16(m+48,!0),y=c?Kh(g,c):null;y&&(y.usesDefaultColor?u+=1:l+=1),f.push({normal:Br(p)?p:null,hasIgnoredNormal:!Br(p),color:y?.color,vertices:h,hasIgnoredAttribute:!c&&g!==0})}return{format:"binary",facets:f,binaryHeader:a,color:c?{format:"magics",defaultColor:c.defaultColor,alpha:c.alpha,coloredTriangleCount:l,defaultColorTriangleCount:u}:void 0}}function sf(e,n){let t=[],o=/^\s*facet\s+normal\s+([^\r\n]+)([\s\S]*?)^\s*endfacet\b[^\r\n]*$/gim,r=/^\s*vertex\s+([^\r\n]+)/gim,s=/^\s*facet\s+normal\b/gim,i=Zh(e),a=0,c,l=0;for(;s.exec(e);)l+=1;let u=0,f=0,d=0,m;for(;m=o.exec(e);){for(u+=1;a<i.length&&i[a].index<m.index;){let x=i[a];c=x.kind==="start"?x.name:void 0,a+=1}let p=rf(m[1]);p||(d+=1);let h=[],g=!1;r.lastIndex=0;let y;for(;y=r.exec(m[2]);){let x=rf(y[1]);if(!x){g=!0;break}h.push(x)}if(g||h.length!==3){f+=1;continue}t.push({normal:p,hasIgnoredNormal:!p,solidName:c,vertices:[h[0],h[1],h[2]]})}return f+=Math.max(0,l-u),f>0&&n.push(`parseStl: skipped ${f} malformed ASCII facet${f===1?"":"s"}`),d>0&&n.push(`parseStl: ignored ${d} malformed ASCII facet normal${d===1?"":"s"}`),{format:"ascii",facets:t}}function St(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Ur(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 ca(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function uf(e){return Math.hypot(e[0],e[1],e[2])}function ny(e){let n=Ur(St(e[1],e[0]),St(e[2],e[0]));return uf(n)<=1e-8}function kr(e){let n=uf(e);return n<=1e-8?null:[e[0]/n,e[1]/n,e[2]/n]}function ty(e){let n=t=>Object.is(t,-0)?0:t;return`${n(e[0])},${n(e[1])},${n(e[2])}`}function oy(e,n){return e<n?`${e}|${n}`:`${n}|${e}`}function la(e,n){return n?[e.vertices[0],e.vertices[2],e.vertices[1]]:e.vertices}function ry(e,n,t){let o=0;for(let r of n.facets){let[s,i,a]=la(e[r],t[r]);o+=ca(s,Ur(i,a))/6}return o}function sy(e,n,t){if(n.facets.length<8)return null;let o=0,r=0;for(let i of n.facets){let a=e[i],c=a.normal?kr(a.normal):null;if(!c)continue;let l=la(a,t[i]),u=kr(Ur(St(l[1],l[0]),St(l[2],l[0])));if(!u)continue;let f=ca(u,c);f>=.95?o+=1:f<=-.95&&(r+=1)}let s=o+r;return s/n.facets.length<.75?null:r/s>=.9?"opposed":o/s>=.9?"aligned":null}function iy(e){let n=new Map,t=e.map(()=>[]);for(let u=0;u<e.length;u+=1){let f=e[u].vertices.map(ty);for(let d=0;d<3;d+=1){let m=f[d],p=f[(d+1)%3],h=oy(m,p),g=n.get(h),y={facetIndex:u,from:m,to:p};g?g.push(y):n.set(h,[y])}}let o=0;for(let u of n.values()){if(u.length>2&&(o+=1),u.length<2)continue;let[f,...d]=u;for(let m of d){let p=f.from===m.from&&f.to===m.to;t[f.facetIndex].push({to:m.facetIndex,flipDiffers:p}),t[m.facetIndex].push({to:f.facetIndex,flipDiffers:p})}}let r=new Array(e.length).fill(!1),s=new Array(e.length).fill(-1),i=[],a=0;for(let u=0;u<e.length;u+=1){if(s[u]>=0)continue;let f=i.length,d={facets:[],conflicts:0,boundaryEdges:0,nonManifoldEdges:0,flippedOutward:!1,flippedToSuppliedNormals:!1};i.push(d),s[u]=f;let m=[u];for(let p=0;p<m.length;p+=1){let h=m[p];d.facets.push(h);for(let g of t[h]){let y=r[h]!==g.flipDiffers;s[g.to]<0?(s[g.to]=f,r[g.to]=y,m.push(g.to)):r[g.to]!==y&&h<g.to&&(d.conflicts+=1,a+=1)}}}for(let u of n.values()){let f=i[s[u[0].facetIndex]];u.length===1?f.boundaryEdges+=1:u.length>2&&(f.nonManifoldEdges+=1)}for(let u of i){if(u.nonManifoldEdges>0||u.conflicts>0){for(let d of u.facets)r[d]=!1;continue}if(u.boundaryEdges>0){if(sy(e,u,r)==="opposed"){for(let d of u.facets)r[d]=!r[d];u.flippedToSuppliedNormals=!0}continue}if(ry(e,u,r)<-1e-10){for(let d of u.facets)r[d]=!r[d];u.flippedOutward=!0}}let c=0;return{facets:e.map((u,f)=>r[f]?(c+=1,{...u,vertices:la(u,!0)}):u),repairedCount:c,componentCount:i.length,outwardComponentCount:i.filter(u=>u.flippedOutward).length,suppliedNormalComponentCount:i.filter(u=>u.flippedToSuppliedNormals).length,conflictCount:a,nonManifoldEdgeCount:o}}function ay(e,n,t,o,r){let s=i=>Math.round(i*1e3)/1e3;return r==="y"?[s((e[2]-n[2])*t+o),s((e[0]-n[0])*t+o),s((e[1]-n[1])*t+o)]:[s((e[0]-n[0])*t+o),s((e[1]-n[1])*t+o),s((e[2]-n[2])*t+o)]}function cy(e){if(e.every(r=>r.solidName===void 0))return;let n=[],t=e[0]?.solidName??"",o=0;for(let r=1;r<=e.length;r+=1){let s=e[r]?.solidName??"";r<e.length&&s===t||(n.push({name:t,start:o,count:r-o}),t=s,o=r)}return n}function ly(e){if(!e)return;let n=[],t=new Set;for(let o of e)!o.name||t.has(o.name)||(t.add(o.name),n.push(o.name));return n.length>0?n:void 0}function zr(e,n){let t=$h("targetSize",n?.targetSize,60),o=af("gridShift",n?.gridShift,1),r=Yh(n?.defaultColor),s=Xh(n?.upAxis),i=[],a=Wh(e),c=typeof e=="string"?e.length:a.byteLength;if(c===0||typeof e=="string"&&e.trim().length===0)throw new Error("parseStl: empty input");let l=a?Jh(a):null,u=typeof e=="string"?sf(e,i):l?ey(a,l,i):sf(cf(a),i),f=0,d=0,m=0,p=[];for(let S of u.facets){if(S.hasIgnoredAttribute&&(f+=1),S.hasIgnoredNormal&&u.format==="binary"&&(d+=1),ny(S.vertices)){m+=1;continue}p.push(S)}let h=iy(p),g=0;for(let S of h.facets){let M=kr(Ur(St(S.vertices[1],S.vertices[0]),St(S.vertices[2],S.vertices[0]))),T=S.normal?kr(S.normal):null;M&&T&&ca(M,T)<.95&&(g+=1)}if(f>0&&i.push(`parseStl: ignored non-zero binary attribute byte count on ${f} triangle${f===1?"":"s"}`),d>0&&i.push(`parseStl: ignored ${d} non-finite binary normal${d===1?"":"s"}`),m>0&&i.push(`parseStl: skipped ${m} degenerate triangle${m===1?"":"s"}`),h.repairedCount>0&&i.push(`parseStl: repaired winding on ${h.repairedCount} triangle${h.repairedCount===1?"":"s"}`),h.outwardComponentCount>0&&i.push(`parseStl: oriented ${h.outwardComponentCount} closed component${h.outwardComponentCount===1?"":"s"} outward`),h.suppliedNormalComponentCount>0&&i.push(`parseStl: oriented ${h.suppliedNormalComponentCount} open component${h.suppliedNormalComponentCount===1?"":"s"} from supplied normals`),h.conflictCount>0&&i.push(`parseStl: found ${h.conflictCount} inconsistent shared-edge winding constraint${h.conflictCount===1?"":"s"}`),h.nonManifoldEdgeCount>0&&i.push(`parseStl: found ${h.nonManifoldEdgeCount} non-manifold shared edge${h.nonManifoldEdgeCount===1?"":"s"}`),g>0&&i.push(`parseStl: ${g} supplied normal${g===1?" disagrees":"s disagree"} with triangle winding`),p.length===0)throw u.facets.length===0?new Error(`parseStl: no valid ${u.format} facets`):new Error("parseStl: no valid facets after filtering");let y=[1/0,1/0,1/0],x=[-1/0,-1/0,-1/0];for(let S of h.facets)for(let M of S.vertices)for(let T=0;T<3;T+=1)M[T]<y[T]&&(y[T]=M[T]),M[T]>x[T]&&(x[T]=M[T]);let b=Math.max(x[0]-y[0],x[1]-y[1],x[2]-y[2]),P=b>0?t/b:1,A=cy(h.facets),C=ly(A),E=h.facets.map(S=>({vertices:S.vertices.map(M=>ay(M,y,P,o,s)),color:S.color??r}));return{polygons:E,objectUrls:[],dispose:()=>{},warnings:i,metadata:{triangleCount:E.length,meshes:C,materials:[],sourceBytes:c,stlHeader:u.binaryHeader,stlColor:u.color,stlSolids:A,stlTopology:{componentCount:h.componentCount,repairedTriangleCount:h.repairedCount,outwardComponentCount:h.outwardComponentCount,suppliedNormalComponentCount:h.suppliedNormalComponentCount,inconsistentSharedEdgeCount:h.conflictCount,nonManifoldSharedEdgeCount:h.nonManifoldEdgeCount}}}}var wn="loadMesh";async function ff(e,n){let t=n?.solidTextureSamples;return t===!1?e:Lr(e,typeof t=="object"?t:void 0)}function uy(e){let n=e.split("?")[0].split("#")[0],t=n.lastIndexOf(".");return t<0?"":n.slice(t+1).toLowerCase()}async function mf(e,n){let t=uy(e);if(t==="mtl")throw new Error(`${wn}: .mtl is a material file, not a mesh \u2014 use parseMtl directly`);let o=globalThis.fetch;if(!o)throw new Error(`${wn}: no fetch() in this environment`);let r=n?.baseUrl??e;if(t==="obj"){let s=await o(e);if(!s.ok)throw new Error(`${wn}: ${e} \u2192 ${s.status}`);let i=await s.text(),a=n?.objOptions;if(n?.mtlUrl){let u=await o(n.mtlUrl);if(!u.ok)throw new Error(`${wn}: ${n.mtlUrl} \u2192 ${u.status}`);let f=await u.text(),{colors:d,textures:m}=Nr(f),p={},h=globalThis.URL,g=globalThis.document?.baseURI,y=n.mtlUrl;if(h&&g)try{y=new h(n.mtlUrl,g).toString()}catch{}for(let[x,b]of Object.entries(m)){if(h)try{p[x]=new h(b,y).toString();continue}catch{}let P=n.mtlUrl.lastIndexOf("/"),A=P>=0?n.mtlUrl.slice(0,P+1):"";p[x]=b.startsWith("/")||/^https?:\/\//.test(b)?b:A+b}a={...a??{},materialColors:{...d,...a?.materialColors??{}},materialTextures:{...p,...a?.materialTextures??{}}}}let c=wr(i,a),l=await ff(c,n);return Yn(l,{meshResolution:n?.meshResolution,source:c})}if(t==="glb"||t==="gltf"){let s=await o(e);if(!s.ok)throw new Error(`${wn}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer(),a=Rr(i,{baseUrl:r,...n?.gltfOptions??{}}),c=await ff(a,n);return Yn(c,{meshResolution:n?.meshResolution,source:a})}if(t==="vox"){let s=await o(e);if(!s.ok)throw new Error(`${wn}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return Yn(Dr(i,n?.voxOptions),{meshResolution:n?.meshResolution})}if(t==="stl"){let s=await o(e);if(!s.ok)throw new Error(`${wn}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return Yn(zr(i,n?.stlOptions),{meshResolution:n?.meshResolution})}throw new Error(`${wn}: unsupported extension ".${t}" (supported: obj, glb, gltf, vox, stl)`)}function Ne(e,n){if(Object.is(e,0)||Object.is(e,-0))return"0";if(e===1)return"1";if(e===-1)return"-1";let t=mr[n]??10**n,o=Math.round(e*t)/t;return Object.is(o,0)||Object.is(o,-0)?"0":String(o)}function df(e,n=Jt){let t=Ne(e,n);return t==="0"?"0":`${t}px`}function an(e,n=pn){if(e.length===0)return"";let t=Ne(e[0],n);for(let o=1;o<e.length;o++)t+=`,${Ne(e[o],n)}`;return t}function pf(e,n,t,o,r=pn){return Gr(e[0],e[1],e[2],n[0],n[1],n[2],t[0],t[1],t[2],o[0],o[1],o[2],r)}function Gr(e,n,t,o,r,s,i,a,c,l,u,f,d=pn){if(d===3){let m=Math.round(e*1e3)/1e3||0,p=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,g=Math.round(o*1e3)/1e3||0,y=Math.round(r*1e3)/1e3||0,x=Math.round(s*1e3)/1e3||0,b=Math.round(i*1e3)/1e3||0,P=Math.round(a*1e3)/1e3||0,A=Math.round(c*1e3)/1e3||0,C=Math.round(l*1e3)/1e3||0,E=Math.round(u*1e3)/1e3||0,S=Math.round(f*1e3)/1e3||0;return`${m},${p},${h},0,${g},${y},${x},0,${b},${P},${A},0,${C},${E},${S},1`}return`${Ne(e,d)},${Ne(n,d)},${Ne(t,d)},0,${Ne(o,d)},${Ne(r,d)},${Ne(s,d)},0,${Ne(i,d)},${Ne(a,d)},${Ne(c,d)},0,${Ne(l,d)},${Ne(u,d)},${Ne(f,d)},1`}function $r(e,n,t,o,r,s,i,a,c,l,u,f,d=pn){if(d!==3)return`matrix3d(${Gr(e,n,t,o,r,s,i,a,c,l,u,f,d)})`;let m=Math.round(e*1e3)/1e3||0,p=Math.round(n*1e3)/1e3||0,h=Math.round(t*1e3)/1e3||0,g=Math.round(o*1e3)/1e3||0,y=Math.round(r*1e3)/1e3||0,x=Math.round(s*1e3)/1e3||0,b=Math.round(i*1e3)/1e3||0,P=Math.round(a*1e3)/1e3||0,A=Math.round(c*1e3)/1e3||0,C=Math.round(l*1e3)/1e3||0,E=Math.round(u*1e3)/1e3||0,S=Math.round(f*1e3)/1e3||0;return`matrix3d(${m},${p},${h},0,${g},${y},${x},0,${b},${P},${A},0,${C},${E},${S},1)`}function At(e,n,t,o=0,r=0){let s=e.matrix.split(",").map(d=>Number(d));if(s.length!==16||s.some(d=>!Number.isFinite(d)))return e.matrix;let i=s[0],a=s[1],c=s[2],l=s[4],u=s[5],f=s[6];return s[0]*=n,s[1]*=n,s[2]*=n,s[4]*=t,s[5]*=t,s[6]*=t,s[12]+=o*i+r*l,s[13]+=o*a+r*u,s[14]+=o*c+r*f,an(s)}function gf(e,n){return At(e,n.width/hn,n.height/hn,n.minX,n.minY)}function ua(e){let n=e.seamBleedInsets??{left:0,right:0,top:0,bottom:0},t=e.canvasW||1,o=e.canvasH||1;return At(e,(t+n.left+n.right)/gn,(o+n.top+n.bottom)/gn,-n.left,-n.top)}function Ct(e,n,t=n){let o=e.matrix.split(",").map(r=>Number(r));return o.length!==16||o.some(r=>!Number.isFinite(r))?e.canonicalMatrix:(o[0]*=e.canvasW/n,o[1]*=e.canvasW/n,o[2]*=e.canvasW/n,o[4]*=e.canvasH/t,o[5]*=e.canvasH/t,o[6]*=e.canvasH/t,an(o))}function vn(e,n=fr){let t=Ne(e,n);return Number(t)===0?"0":`${t}%`}function hf(e,n=pn){return`matrix3d(${e.split(",").map(t=>{let o=Number(t.trim());return Number.isFinite(o)?Ne(o,n):t.trim()}).join(",")})`}function yf(e,n=Jt){let t=Ne(e,n);return Number(t)===0||Object.is(Number(t),-0)?"0":`${t}px`}function xf(e){return`matrix3d(${ua(e)})`}function In(e,n,t){return e.map(o=>[o[1]*n,o[0]*n,o[2]*t])}function Nn(e){if(e.length<3)return null;let n=e[0],t=0,o=0,r=0;for(let i=1;i+1<e.length;i++){let a=e[i],c=e[i+1],l=[a[0]-n[0],a[1]-n[1],a[2]-n[2]],u=[c[0]-n[0],c[1]-n[1],c[2]-n[2]];t-=l[1]*u[2]-l[2]*u[1],o-=l[2]*u[0]-l[0]*u[2],r-=l[0]*u[1]-l[1]*u[0]}let s=Math.hypot(t,o,r);return s<=de?null:(t/=s,o/=s,r/=s,[t,o,r])}function Tt(e){if(e.length<3)return!1;let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length],s=e[(t+2)%e.length],i=(r[0]-o[0])*(s[1]-r[1])-(r[1]-o[1])*(s[0]-r[0]);if(Math.abs(i)<=de)return!1;let a=Math.sign(i);if(n===0)n=a;else if(a!==n)return!1}return!0}function Xr(e){let n=0;for(let t=0;t<e.length;t++){let o=e[t],r=e[(t+1)%e.length];n+=o[0]*r[1]-o[1]*r[0]}return n/2}function Yr(e,n,t,o){let r=n[0]-e[0],s=n[1]-e[1],i=o[0]-t[0],a=o[1]-t[1],c=r*a-s*i;if(Math.abs(c)<=de)return null;let l=t[0]-e[0],u=t[1]-e[1],f=(l*a-u*i)/c;return[e[0]+f*r,e[1]+f*s]}function bf(e,n,t,o,r,s,i,a){let c=t-e,l=o-n,u=i-r,f=a-s,d=c*f-l*u;if(Math.abs(d)<=de)return null;let m=r-e,p=s-n,h=(m*f-p*u)/d;return[e+h*c,n+h*l]}function Jn(e,n){if(e.length<6||n<=0)return e;let t=0,o=0,r=e.length/2;for(let i=0;i<e.length;i+=2)t+=e[i],o+=e[i+1];t/=r,o/=r;let s=e.slice();for(let i=0;i<s.length;i+=2){let a=s[i]-t,c=s[i+1]-o,l=Math.hypot(a,c);l<=Me||(s[i]+=a/l*n,s[i+1]+=c/l*n)}return s}function et(e,n){if(e.length<6||e.length%2!==0||n<=0)return e;let t=[];for(let c=0;c<e.length;c+=2)t.push([e[c],e[c+1]]);if(!Tt(t))return Jn(e,n);let o=Xr(t);if(Math.abs(o)<=de)return Jn(e,n);let r=o>0?1:-1,s=[];for(let c=0;c<t.length;c++){let l=t[c],u=t[(c+1)%t.length],f=u[0]-l[0],d=u[1]-l[1],m=Math.hypot(f,d);if(m<=de)return Jn(e,n);let p=r*(d/m)*n,h=r*(-f/m)*n;s.push({a:[l[0]+p,l[1]+h],b:[u[0]+p,u[1]+h]})}let i=[],a=Math.max(2,n*4);for(let c=0;c<t.length;c++){let l=s[(c+t.length-1)%t.length],u=s[c],f=Yr(l.a,l.b,u.a,u.b);if(!f)return Jn(e,n);let d=t[c],m=f[0]-d[0],p=f[1]-d[1],h=Math.hypot(m,p);h>a?i.push(d[0]+m/h*a,d[1]+p/h*a):i.push(f[0],f[1])}return i}function Et(e,n){if(e.length<6||e.length%2!==0)return e;let t=e.length/2;if(n.length!==t)return e;let o=n.map(f=>typeof f=="number"&&Number.isFinite(f)?Math.max(0,f):0),r=Math.max(0,...o);if(r<=0)return e;if(o.every(f=>Math.abs(f-o[0])<=de))return et(e,o[0]);let s=[];for(let f=0;f<e.length;f+=2)s.push([e[f],e[f+1]]);if(!Tt(s))return e;let i=Xr(s);if(Math.abs(i)<=de)return e;let a=i>0?1:-1,c=[];for(let f=0;f<s.length;f++){let d=s[f],m=s[(f+1)%s.length],p=m[0]-d[0],h=m[1]-d[1],g=Math.hypot(p,h);if(g<=de)return e;let y=o[f],x=a*(h/g)*y,b=a*(-p/g)*y;c.push({a:[d[0]+x,d[1]+b],b:[m[0]+x,m[1]+b]})}let l=[],u=Math.max(2,r*4);for(let f=0;f<s.length;f++){let d=c[(f+s.length-1)%s.length],m=c[f],p=Yr(d.a,d.b,m.a,m.b);if(!p)return e;let h=s[f],g=p[0]-h[0],y=p[1]-h[1],x=Math.hypot(g,y);x>u?l.push(h[0]+g/x*u,h[1]+y/x*u):l.push(p[0],p[1])}return l}function uo(e,n,t,o,r,s,i){if(i<=0)return[e,n,t,o,r,s];let a=(e*o-n*t+t*s-o*r+r*n-s*e)/2,c=()=>Jn([e,n,t,o,r,s],i);if(Math.abs(a)<=de)return c();let l=a>0?1:-1,u=t-e,f=o-n,d=Math.sqrt(u*u+f*f),m=r-t,p=s-o,h=Math.sqrt(m*m+p*p),g=e-r,y=n-s,x=Math.sqrt(g*g+y*y);if(d<=de||h<=de||x<=de)return c();let b=l*(f/d)*i,P=l*(-u/d)*i,A=e+b,C=n+P,E=t+b,S=o+P,M=l*(p/h)*i,T=l*(-m/h)*i,v=t+M,I=o+T,_=r+M,O=s+T,R=l*(y/x)*i,w=l*(-g/x)*i,F=r+R,D=s+w,K=e+R,H=n+w,Q=K-F,ne=H-D,ee=E-A,ue=S-C,U=Q*ue-ne*ee;if(Math.abs(U)<=de)return c();let $=A-F,k=C-D,L=($*ue-k*ee)/U,W=F+L*Q,Y=D+L*ne,se=E-A,Z=S-C,B=_-v,q=O-I,me=se*q-Z*B;if(Math.abs(me)<=de)return c();let fe=v-A,z=I-C,te=(fe*q-z*B)/me,ce=A+te*se,ye=C+te*Z,he=_-v,xe=O-I,ge=K-F,Ae=H-D,Fe=he*Ae-xe*ge;if(Math.abs(Fe)<=de)return c();let V=F-v,G=D-I,j=(V*Ae-G*ge)/Fe,J=v+j*he,le=I+j*xe,re=Math.max(2,i*4),pe=W-e,be=Y-n,ie=Math.sqrt(pe*pe+be*be);ie>re&&(W=e+pe/ie*re,Y=n+be/ie*re);let ae=ce-t,N=ye-o,X=Math.sqrt(ae*ae+N*N);X>re&&(ce=t+ae/X*re,ye=o+N/X*re);let oe=J-r,ve=le-s,Ce=Math.sqrt(oe*oe+ve*ve);return Ce>re&&(J=r+oe/Ce*re,le=s+ve/Ce*re),[W,Y,ce,ye,J,le]}function Wr(e,n,t,o){let r=e+n;if(o<=0||t<=de||r<=de||!Number.isFinite(e+n+t+o))return uo(e,0,0,t,r,t,o);let s=Math.sqrt(e*e+t*t),i=Math.sqrt(n*n+t*t);if(s<=de||i<=de)return uo(e,0,0,t,r,t,o);let a=-o*t/s,c=-o*e/s,l=o*t/i,u=-o*n/i,f=e+a,d=c,m=r+l,p=t+u,h=-t*r;if(Math.abs(h)<=de)return uo(e,0,0,t,r,t,o);let g=f-m,y=d-p,x=(g*t+y*e)/h,b=m-x*n,P=p-x*t,A=-o*(e+s)/t,C=t+o,E=r+o*(n+i)/t,S=C,M=Math.max(2,o*4),T=b-e,v=P,I=Math.sqrt(T*T+v*v);I>M&&(b=e+T/I*M,P=v/I*M);let _=Math.sqrt(A*A+o*o);_>M&&(A=A/_*M,C=t+o/_*M);let O=E-r,R=Math.sqrt(O*O+o*o);return R>M&&(E=r+O/R*M,S=t+o/R*M),[b,P,A,C,E,S]}function fo(e,n){if(e.screenPts.length<6||n.vertices.length<3)return null;let t=e.tileSize,o=e.layerElevation,r=In(n.vertices,t,o),s=Nn(r);if(!s)return null;let i=e.screenPts[0],a=e.screenPts[1],c=e.screenPts[2],l=e.screenPts[3],u=e.screenPts[4],f=e.screenPts[5],d=c-i,m=l-a,p=u-i,h=f-a,g=d*h-m*p;if(Math.abs(g)<=de)return null;let y=r[0],x=r[1],b=r[2],P=[x[0]-y[0],x[1]-y[1],x[2]-y[2]],A=[b[0]-y[0],b[1]-y[1],b[2]-y[2]],C=[(P[0]*h-m*A[0])/g,(P[1]*h-m*A[1])/g,(P[2]*h-m*A[2])/g],E=[(d*A[0]-P[0]*p)/g,(d*A[1]-P[1]*p)/g,(d*A[2]-P[2]*p)/g],S=y[0]-C[0]*i-E[0]*a,M=y[1]-C[1]*i-E[1]*a,T=y[2]-C[2]*i-E[2]*a;return{normal:s,xAxis:C,yAxis:E,tx:S,ty:M,tz:T}}function jr(e){return Math.max(0,Math.min(6,Math.floor(e??pn)))}function Pf(e){return`${e[0]},${e[1]},${e[2]}`}function Mf(e,n){let t=Pf(e),o=Pf(n);return t<o?`${t}|${o}`:`${o}|${t}`}function Sf(e){let n=new Map;for(let o=0;o<e.length;o++){let r=e[o].vertices;if(!(!r||r.length<3||!e[o].texture))for(let s=0;s<r.length;s++){let i=Mf(r[s],r[(s+1)%r.length]),a=n.get(i),c={polygon:o,edge:s};a?a.push(c):n.set(i,[c])}}let t=e.map(()=>new Set);for(let o of n.values())if(!(o.length<2))for(let r=0;r<o.length;r++)for(let s=r+1;s<o.length;s++)t[o[r].polygon].add(o[r].edge),t[o[s].polygon].add(o[s].edge);return t.map(o=>o.size>0?o:void 0)}function mo(e,n){return typeof e=="number"&&Number.isFinite(e)?Math.max(0,e):n}function Hr(e){return typeof e=="number"&&Number.isFinite(e)&&e>0?e:void 0}function po(e,n,t){if(t<=0||e.length<6||e.length%2!==0)return 0;let o=e.length/2;if(n<0||n>=o)return 0;let r=[];for(let S=0;S<e.length;S+=2)r.push([e[S],e[S+1]]);let[s,i]=r[n],[a,c]=r[(n+1)%o],l=a-s,u=c-i,f=Math.hypot(l,u);if(f<=Me)return 0;let d=[],m=Number.POSITIVE_INFINITY;for(let S=0;S<o;S+=1){if(S===n||S===(n+1)%o)continue;let[M,T]=r[S],v=Math.abs((M-s)*u-(T-i)*l)/f;v>Me&&(m=Math.min(m,v))}Number.isFinite(m)&&d.push(m*.5);let p=r[(n+o-1)%o],h=r[(n+2)%o],g=p[0]-s,y=p[1]-i,x=h[0]-a,b=h[1]-c,P=Math.hypot(g,y),A=Math.hypot(x,b);if(P>Me){let S=Math.abs(l*y-u*g)/(f*P);S>Me&&d.push(P*S)}if(A>Me){let S=Math.abs(-l*b- -u*x)/(f*A);S>Me&&d.push(A*S)}let C=d.filter(S=>Number.isFinite(S)&&S>0);if(C.length===0)return Number.isFinite(t)?Math.max(0,t):0;let E=Math.min(...C);return Math.max(0,Math.min(Number.isFinite(t)?t:E,E))}function Kr(e,n,t){if(!n?.size||t===void 0)return;let o=new Map;for(let r of n){let s=po(e,r,t);s>0&&o.set(r,s)}return o.size>0?o:void 0}function Zr(e,n){return e<3||!n?.size?null:Array.from({length:e},(t,o)=>Math.max(0,n.get(o)??0))}function qr(e,n){if(!n?.size||e.length<6||e.length%2!==0)return;let t=e.length/2,o=Number.POSITIVE_INFINITY,r=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];o=Math.min(o,u),r=Math.min(r,f),s=Math.max(s,u),i=Math.max(i,f)}if(!Number.isFinite(o+r+s+i))return;let a=Math.max(Me*8,.25),c={left:0,right:0,top:0,bottom:0};for(let[l,u]of n){let f=Math.max(0,u);if(f<=0||l<0||l>=t)continue;let d=l*2,m=(l+1)%t*2,p=e[d],h=e[d+1],g=e[m],y=e[m+1];Math.abs(p-g)<=a?Math.abs(p-o)<=a&&Math.abs(g-o)<=a?c.left=Math.max(c.left,f):Math.abs(p-s)<=a&&Math.abs(g-s)<=a&&(c.right=Math.max(c.right,f)):Math.abs(h-y)<=a&&(Math.abs(h-r)<=a&&Math.abs(y-r)<=a?c.top=Math.max(c.top,f):Math.abs(h-i)<=a&&Math.abs(y-i)<=a&&(c.bottom=Math.max(c.bottom,f)))}return c.left||c.right||c.top||c.bottom?c:void 0}function fy(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function my(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let o=In(e.vertices,n,t),r=Nn(o);return r?{normal:r,planeD:fy(r,o[0])}:null}function dy(e,n){return(e.material?.key??e.color??"")===(n.material?.key??n.color??"")&&e.color===n.color}function Af(e,n={}){return new Set(fa(e,n).keys())}function fa(e,n={}){let t=n.tileSize??50,o=n.layerElevation??t,r=e.map(c=>my(c,t,o)),s=new Map;for(let c=0;c<e.length;c++){let l=e[c].vertices;if(!(!l||l.length<3))for(let u=0;u<l.length;u++){let f=Mf(l[u],l[(u+1)%l.length]),d=s.get(f),m={polygon:c,edge:u};d?d.push(m):s.set(f,[m])}}let i=new Map,a=(c,l)=>{let u=i.get(c);u?u.add(l):i.set(c,new Set([l]))};for(let c of s.values())if(!(c.length<2))for(let l=0;l<c.length;l++)for(let u=l+1;u<c.length;u++){let f=c[l],d=c[u],m=f.polygon,p=d.polygon;!r[m]||!r[p]||dy(e[m],e[p])&&(a(m,f.edge),a(p,d.edge))}return i}function py(e,n,t,o,r,s){let i=(e-t)*(s-o)-(n-o)*(r-t);return Math.abs(i)>no?!1:(e-t)*(e-r)+(n-o)*(n-s)<=no}function ma(e,n=eo,t=eo){let o=!1;for(let r=0,s=e.length-1;r<e.length;s=r++){let[i,a]=e[r],[c,l]=e[s];if(py(n,t,i,a,c,l))return!0;a>t!=l>t&&n<(c-i)*(t-a)/(l-a)+i&&(o=!o)}return o}function da(e,n,t){let o=Number.POSITIVE_INFINITY,r=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY;for(let l=0;l<e.length;l+=2){let u=e[l],f=e[l+1];!Number.isFinite(u)||!Number.isFinite(f)||(o=Math.min(o,u),r=Math.min(r,f),s=Math.max(s,u),i=Math.max(i,f))}let a=s-o,c=i-r;return!Number.isFinite(o)||!Number.isFinite(r)||!Number.isFinite(a)||!Number.isFinite(c)||a<=de||c<=de?{minX:0,minY:0,width:n,height:t}:{minX:o,minY:r,width:a,height:c}}function vt(e){let n=e.bleedRatio??1,t=hr*n,o=e.canvasW||1,r=e.canvasH||1,s=t>0?et(e.screenPts,t):e.screenPts,i=t>0?da(s,o,r):{minX:0,minY:0,width:o,height:r},a=[];for(let c=0;c<s.length;c+=2){let l=Math.max(0,Math.min(100,(s[c]-i.minX)/i.width*100)),u=Math.max(0,Math.min(100,(s[c+1]-i.minY)/i.height*100));a.push([l,u])}return{bounds:i,points:a}}function pa(e){let n=[];for(let t of e){let o=n[n.length-1];o&&Math.hypot(o[0]-t[0],o[1]-t[1])<=to||n.push(t)}if(n.length>1){let t=n[0],o=n[n.length-1];Math.hypot(t[0]-o[0],t[1]-o[1])<=to&&n.pop()}return n}function ga([e,n]){let t=new Set;return Math.abs(e)<=We&&t.add("left"),Math.abs(e-100)<=We&&t.add("right"),Math.abs(n)<=We&&t.add("top"),Math.abs(n-100)<=We&&t.add("bottom"),t.size>0?t:null}function ha(e,n){for(let t of e)if(n.has(t))return!0;return!1}function ya(e,n,t,o){let r=(s,i,a)=>{let c=n.has(i)?e:o.has(i)?t:null,l=n.has(a)?e:o.has(a)?t:null;if(!c||!l)return null;let u=(()=>{switch(s){case"topLeft":return{x:c[0],y:l[1]};case"topRight":return{x:100-c[0],y:l[1]};case"bottomRight":return{x:100-c[0],y:100-l[1]};case"bottomLeft":return{x:c[0],y:100-l[1]}}})();return u.x>We&&u.y>We&&u.x<100-We&&u.y<100-We?[s,u]:null};return(n.has("top")||o.has("top"))&&(n.has("left")||o.has("left"))?r("topLeft","top","left"):(n.has("top")||o.has("top"))&&(n.has("right")||o.has("right"))?r("topRight","top","right"):(n.has("bottom")||o.has("bottom"))&&(n.has("right")||o.has("right"))?r("bottomRight","bottom","right"):(n.has("bottom")||o.has("bottom"))&&(n.has("left")||o.has("left"))?r("bottomLeft","bottom","left"):null}function Qr(e){if(e.texture||nt(e)||Dn(e))return null;let n=vt(e),t=pa(n.points);if(t.length<4)return null;let o=t.map(ga);if(o.some(i=>!i))return null;let r={},s=0;for(let i=0;i<t.length;i+=1){let a=o[i],c=o[(i+1)%t.length];if(ha(a,c))continue;let l=ya(t[i],a,t[(i+1)%t.length],c);if(!l)return null;let[u,f]=l,d=r[u];if(d&&(Math.abs(d.x-f.x)>We||Math.abs(d.y-f.y)>We))return null;r[u]=f,s+=1}return s>0?{bounds:n.bounds,radii:r}:null}function gy([e,n]){return`${vn(e)} ${vn(n)}`}function hy(e){return`polygon(${e.map(gy).join(",")})`}function yy(e){if(ma(e))return"circle(0)";let n=0,t=0,o=Math.max(1,e.length);for(let[i,a]of e)n+=i,t+=a;let r=vn(Math.max(0,Math.min(100,n/o))),s=vn(Math.max(0,Math.min(100,t/o)));return`circle(0 at ${r} ${s})`}function Jr(e){return`${hy(e)} ${yy(e)}`}function Cf(e){return Jr(vt(e).points)}function xa(e,n){return At(e,n.width/hn,n.height/hn,n.minX,n.minY)}function Tf(e){let n=vt(e);return`matrix3d(${xa(e,n.bounds)})`}function Ef(e){let n=vt(e);return[`transform:matrix3d(${xa(e,n.bounds)})`,`border-shape:${Jr(n.points)}`].join(";")}function vf(e,n){let t=[`transform:matrix3d(${xa(e,n.bounds)})`,`width:${hn}px`,`height:${hn}px`,"border:0","box-sizing:border-box","background:currentColor"];for(let[o,r]of Object.entries(n.radii)){let s=o.replace(/[A-Z]/g,i=>`-${i.toLowerCase()}`);t.push(`border-${s}-radius:${vn(r.x)} ${vn(r.y)}`),t.push(`corner-${s}-shape:bevel`)}return t.join(";")}function go(e){return e.textureImageSource??e.material?.imageSource}function es(e){return e.material?.texture??e.texture??e.textureImageSource?.url??e.material?.imageSource?.url}function ho(e,n={}){let t=go(e);return{...n,...e.material?.presentation,...t?.imageRendering?{imageRendering:t.imageRendering}:null,...e.texturePresentation}}function If(e,n){return ho(e,{imageRendering:n}).imageRendering??"auto"}function _f(e,n){return typeof e=="number"&&Number.isFinite(e)?e:n}function yo(e){let n=e?.maxWeightRatio,t=Math.max(0,_f(e?.denomEps,yr)),o=typeof n=="number"&&Number.isFinite(n)&&n>0?Math.max(1,n):xr,r=Math.max(0,_f(e?.bleed,br));return{denomEps:t,maxWeightRatio:o,bleed:r,disableGuards:e?.disableGuards===!0}}function ns(e,n){if(e.length!==4||!Tt(e))return null;let[t,o,r,s]=e,i=t[0]-o[0]+r[0]-s[0],a=t[1]-o[1]+r[1]-s[1],c=o[0]-r[0],l=s[0]-r[0],u=o[1]-r[1],f=s[1]-r[1],d=c*f-u*l;if(Math.abs(d)<=de)return null;let m=(i*f-a*l)/d,p=(c*a-u*i)/d,h=[1,1+m,1+m+p,1+p];if(h.some(x=>!Number.isFinite(x)))return null;let g=Math.min(...h),y=Math.max(...h);return!n.disableGuards&&(g<=n.denomEps||y/g>n.maxWeightRatio)?null:{g:m,h:p,w1:1+m,w3:1+p}}function xo(e,n,t,o,r,s,i,a,c){if(e.length!==8)return null;let l=[[e[0],e[1]],[e[2],e[3]],[e[4],e[5]],[e[6],e[7]]];if(!ns(l,a))return null;let u=Zr(4,c),f=u?Et(e,u):et(e,a.bleed),d=[[f[0],f[1]],[f[2],f[3]],[f[4],f[5]],[f[6],f[7]]],m=ns(d,a);if(!m)return null;let{g:p,h,w1:g,w3:y}=m,[x,b,,P]=d,A=[r+x[0]*n[0]+x[1]*t[0],s+x[0]*n[1]+x[1]*t[1],i+x[0]*n[2]+x[1]*t[2]],C=([S,M],T)=>[(T-1)*r+(T*S-x[0])*n[0]+(T*M-x[1])*t[0],(T-1)*s+(T*S-x[0])*n[1]+(T*M-x[1])*t[1],(T-1)*i+(T*S-x[0])*n[2]+(T*M-x[1])*t[2]],E=[...C(b,g),p,...C(P,y),h,o[0],o[1],o[2],0,A[0],A[1],A[2],1];for(let S=0;S<8;S+=1)E[S]/=gn;return an(E,6)}function bo(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function Of(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 ba(e){return!e.texture}function Pa(e,n,t){if(!e.vertices||e.vertices.length<3)return null;let o=In(e.vertices,n,t),r=Nn(o);return r?{pts:o,normal:r,planeD:bo(r,o[0]),optimizable:ba(e)}:null}function Ma(e,n){return!e||!n||!e.optimizable||!n.optimizable?!1:ss(e,n)}function ss(e,n){return!e||!n||bo(e.normal,n.normal)<1-cr?!1:Math.abs(e.planeD-n.planeD)<=lr}function ts(e,n){if(!e)return null;let t=n.directionalLight,o=n.ambientLight,r=t?.direction??Wn,s=t?.color??jn,i=Math.max(0,t?.intensity??Hn),a=o?.color??Kn,c=Math.max(0,o?.intensity??Zn),l=Math.hypot(r[0],r[1],r[2])||1,u=r[0]/l,f=r[1]/l,d=r[2]/l,m=i*Math.max(0,e.normal[0]*u+e.normal[1]*f+e.normal[2]*d),p=io(m,s,a,c);return p.r*.2126+p.g*.7152+p.b*.0722}function Sa(e){let n=[...e],t=Math.abs(n[0])>de?0:Math.abs(n[1])>de?1:2;return n[t]<0&&(n[0]*=-1,n[1]*=-1,n[2]*=-1),`${n[0].toFixed(6)},${n[1].toFixed(6)},${n[2].toFixed(6)}`}function tt(e,n,t,o,r={}){let s=bo(o,t),i=[o[0]-s*t[0],o[1]-s*t[1],o[2]-s*t[2]],a=Math.hypot(i[0],i[1],i[2]);if(a<=de)return null;let c=[i[0]/a,i[1]/a,i[2]/a],l=[t[1]*c[2]-t[2]*c[1],t[2]*c[0]-t[0]*c[2],t[0]*c[1]-t[1]*c[0]],u=Math.hypot(l[0],l[1],l[2]);if(u<=de)return null;let f=[l[0]/u,l[1]/u,l[2]/u],d=e.map(R=>{let w=R[0]-n[0],F=R[1]-n[1],D=R[2]-n[2];return[w*c[0]+F*c[1]+D*c[2],w*f[0]+F*f[1]+D*f[2]]}),m=r.boundsOrigin??n,p=n[0]-m[0],h=n[1]-m[1],g=n[2]-m[2],y=p*c[0]+h*c[1]+g*c[2],x=p*f[0]+h*f[1]+g*f[2],b=1/0,P=1/0,A=-1/0,C=-1/0;for(let[R,w]of d){let F=R+y,D=w+x;F<b&&(b=F),F>A&&(A=F),D<P&&(P=D),D>C&&(C=D)}let E=A-b,S=C-P;if(!Number.isFinite(E)||!Number.isFinite(S))return null;let M=r.snapBounds?Math.floor(b+Me):b,T=r.snapBounds?Math.floor(P+Me):P,v=r.snapBounds?Math.ceil(A-Me):A,I=r.snapBounds?Math.ceil(C-Me):C,_=Math.max(1,r.snapBounds?v-M:Math.ceil(E)),O=Math.max(1,r.snapBounds?I-T:Math.ceil(S));return{xAxis:c,yAxis:f,local2D:d,shiftX:y-M,shiftY:x-T,canvasW:_,canvasH:O,pixelArea:_*O,rawArea:E*S}}function os(e){return`${e[0]},${e[1]},${e[2]}`}function Rf(e,n){return os(e)<os(n)?[n[0]-e[0],n[1]-e[1],n[2]-e[2]]:[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function xy(e,n){let t=os(e),o=os(n);return t<o?`${t}|${o}`:`${o}|${t}`}function Aa(e,n,t,o){let r=0,s=0;for(let i of e){let a=n[i];if(!a)return null;let c=tt(a.pts,a.pts[0],a.normal,t,{boundsOrigin:o,snapBounds:!0});if(!c)return null;r+=c.pixelArea,s+=c.rawArea}return{pixelArea:r,rawArea:s}}function Ca(e,n){let t=n[e[0]]?.pts[0];if(!t)return null;let o={pixelArea:0,rawArea:0},r=null,s=new Set;for(let i of e){let a=n[i];if(!a)continue;let c=[a.pts[1][0]-a.pts[0][0],a.pts[1][1]-a.pts[0][1],a.pts[1][2]-a.pts[0][2]],l=tt(a.pts,a.pts[0],a.normal,c);o&&l?(o.pixelArea+=l.pixelArea,o.rawArea+=l.rawArea):o=null;for(let u=0;u<a.pts.length;u++){let f=Rf(a.pts[u],a.pts[(u+1)%a.pts.length]),d=tt(a.pts,a.pts[0],a.normal,f);if(!d)continue;let m=Sa(d.xAxis);if(s.has(m))continue;s.add(m);let p=Aa(e,n,d.xAxis,t);p&&(!r||p.pixelArea<r.pixelArea||p.pixelArea===r.pixelArea&&p.rawArea<r.rawArea-Me)&&(r={xAxis:d.xAxis,...p})}}return r&&o&&(r.pixelArea<o.pixelArea||r.pixelArea===o.pixelArea&&r.rawArea<=o.rawArea+Me)?{xAxis:r.xAxis,boundsOrigin:t,seamEdges:new Set}:null}function Vf(e,n){let t=n.tileSize??50,o=n.layerElevation??t,r=e.map(f=>Pa(f,t,o)),s=new Map,i=e.map(()=>new Set),a=e.map(()=>new Set);for(let f=0;f<e.length;f++){let d=e[f].vertices;if(!(!d||d.length<3))for(let m=0;m<d.length;m++){let p=xy(d[m],d[(m+1)%d.length]),h=s.get(p),g={polygon:f,edge:m};h?h.push(g):s.set(p,[g])}}let c=e.map(()=>new Set);for(let f of s.values())if(!(f.length<2))for(let d=0;d<f.length;d++)for(let m=d+1;m<f.length;m++){let p=f[d],h=f[m],g=p.polygon,y=h.polygon;if(e[g].texture&&e[y].texture&&(a[p.polygon].add(p.edge),a[h.polygon].add(h.edge)),ss(r[g],r[y]))i[p.polygon].add(p.edge),i[h.polygon].add(h.edge);else{let x=ts(r[g],n),b=ts(r[y],n);x!==null&&b!==null&&(x<=b+Qt&&i[p.polygon].add(p.edge),b<=x+Qt&&i[h.polygon].add(h.edge))}Ma(r[g],r[y])&&(c[g].add(y),c[y].add(g))}let l=Array(e.length).fill(void 0),u=new Set;for(let f=0;f<e.length;f++){if(u.has(f)||!r[f]?.optimizable)continue;let d=[],m=[f];for(u.add(f);m.length>0;){let h=m.pop();d.push(h);for(let g of c[h])u.has(g)||(u.add(g),m.push(g))}if(d.length<2)continue;let p=Ca(d,r);if(p)for(let h of d)l[h]={xAxis:p.xAxis,boundsOrigin:p.boundsOrigin,seamEdges:i[h],textureEdgeRepairEdges:a[h]}}for(let f=0;f<e.length;f++)!l[f]&&(i[f].size>0||a[f].size>0)&&(l[f]={seamEdges:i[f],textureEdgeRepairEdges:a[f]});return l}function rs(e,n,t,o){if(o.optimize&&o.fixedXAxis)return tt(e,n,t,o.fixedXAxis,{boundsOrigin:o.boundsOrigin,snapBounds:o.snapBounds});let r=null,s=o.optimize&&o.seamEdges&&o.seamEdges.size>0?Array.from(o.seamEdges):null,i=s??(o.optimize?e.map((a,c)=>c):[0]);for(let a of i){let c=(a+1)%e.length,l=s?Rf(e[a],e[c]):[e[c][0]-e[a][0],e[c][1]-e[a][1],e[c][2]-e[a][2]],u=tt(e,n,t,l,{boundsOrigin:o.boundsOrigin,snapBounds:o.snapBounds});u&&(!r||u.pixelArea<r.pixelArea||u.pixelArea===r.pixelArea&&u.rawArea<r.rawArea-Me)&&(r=u)}return r}function Ta(e){if(e.local2D.length!==4)return!1;let n=[],t=[],o=(r,s)=>{for(let i of r)if(Math.abs(i-s)<=Me)return;r.push(s)};for(let[r,s]of e.local2D)o(n,r+e.shiftX),o(t,s+e.shiftY);if(n.length!==2||t.length!==2||(n.sort((r,s)=>r-s),t.sort((r,s)=>r-s),Math.abs(n[0])>Me||Math.abs(t[0])>Me||n[1]-n[0]<=Me||t[1]-t[0]<=Me))return!1;for(let[r,s]of e.local2D){let i=r+e.shiftX,a=s+e.shiftY,c=Math.abs(i-n[0])<=Me||Math.abs(i-n[1])<=Me,l=Math.abs(a-t[0])<=Me||Math.abs(a-t[1])<=Me;if(!c||!l)return!1}return!0}function is(e,n){if(e.length<3||n.length<3)return null;let[t,o,r]=e,[s,i,a]=n,c=t[0],l=t[1],u=o[0],f=o[1],d=r[0],m=r[1],p=s[0],h=1-s[1],g=i[0],y=1-i[1],x=a[0],b=1-a[1],P=g-p,A=y-h,C=x-p,E=b-h,S=P*E-C*A;if(Math.abs(S)<=1e-9)return null;let M=u-c,T=d-c,v=f-l,I=m-l,_={a:(M*E-T*A)/S,b:(P*T-C*M)/S,c:(v*E-I*A)/S,d:(P*I-C*v)/S,e:0,f:0};return _.e=c-_.a*p-_.b*h,_.f=l-_.c*p-_.d*h,_}function as(e){if(e.length===0)return null;let n=1/0,t=1/0,o=-1/0,r=-1/0;for(let s of e){let i=s[0],a=1-s[1];if(!Number.isFinite(i)||!Number.isFinite(a))return null;n=Math.min(n,i),o=Math.max(o,i),t=Math.min(t,a),r=Math.max(r,a)}return{minU:n,minV:t,maxU:o,maxV:r}}function Ea(e,n,t,o,r,s,i,a){let l=In(e.vertices,n,t).map(d=>{let m=d[0]-o[0],p=d[1]-o[1],h=d[2]-o[2];return[m*r[0]+p*r[1]+h*r[2]+i,m*s[0]+p*s[1]+h*s[2]+a]}),u=is(l,e.uvs),f=as(e.uvs);return!u&&!f?null:{screenPts:l.flatMap(([d,m])=>[d,m]),uvAffine:u,uvSampleRect:f}}function va(e,n,t,o,r){let s=t,{vertices:i,uvs:a}=e,c=es(e);if(!i||i.length<3)return null;let l=t.tileSize??50,u=t.layerElevation??l,f=In(i,l,u),d=f[0],m=f[1],p=[m[0]-d[0],m[1]-d[1],m[2]-d[2]];if(Math.hypot(p[0],p[1],p[2])===0)return null;let g=Nn(f);if(!g)return null;let y=rs(f,d,g,{optimize:!1}),x=c||y&&Ta(y)?y:rs(f,d,g,{optimize:!0,fixedXAxis:r?.xAxis,boundsOrigin:r?.boundsOrigin,snapBounds:!!r,seamEdges:r?.seamEdges});if(!x)return null;let{xAxis:b,yAxis:P,local2D:A}=x,C=c&&r?.textureEdgeRepairEdges?.size?r.textureEdgeRepairEdges:null,E=!!(c&&C),S=x.shiftX,M=x.shiftY,T=x.canvasW,v=x.canvasH,I=[];for(let[ge,Ae]of A)I.push(ge+S,Ae+M);let _=d[0]-S*b[0]-M*P[0],O=d[1]-S*b[1]-M*P[1],R=d[2]-S*b[2]-M*P[2],w=an([b[0],b[1],b[2],0,P[0],P[1],P[2],0,g[0],g[1],g[2],0,_,O,R,1]),F=an([b[0]*T,b[1]*T,b[2]*T,0,P[0]*v,P[1]*v,P[2]*v,0,g[0],g[1],g[2],0,_,O,R,1]),D=an([b[0]*T/Ye,b[1]*T/Ye,b[2]*T/Ye,0,P[0]*v/Ye,P[1]*v/Ye,P[2]*v/Ye,0,g[0],g[1],g[2],0,_,O,R,1]),K=Hr(s.seamBleed),H=Kr(I,s.seamEdges??r?.seamEdges,K),Q=H?new Set(H.keys()):void 0,ne=H?Math.max(...H.values()):void 0,ee=qr(I,H),ue=!c&&i.length===4?xo(I,b,P,g,_,O,R,o,H):null,U=t.directionalLight,$=t.ambientLight,k=U?.direction??Wn,L=U?.color??jn,W=Math.max(0,U?.intensity??Hn),Y=$?.color??Kn,se=Math.max(0,$?.intensity??Zn),Z=Math.hypot(k[0],k[1],k[2])||1,B=k[0]/Z,q=k[1]/Z,me=k[2]/Z,z=t.lightOccludedPolyIndices?.has(n)??!1?0:W*Math.max(0,g[0]*B+g[1]*q+g[2]*me),te=io(z,L,Y,se),ce=Vn(e.color??"#cccccc",z,L,Y,se),ye=null,he=null;c&&a&&a.length>=3&&a.length===i.length&&(he=as(a),ye=is(A.map(([ge,Ae])=>[ge+S,Ae+M]),a));let xe=c&&e.textureTriangles?.length?e.textureTriangles.map(ge=>Ea(ge,l,u,d,b,P,S,M)).filter(ge=>!!ge):null;return{index:n,polygon:e,texture:c,tileSize:l,layerElevation:u,matrix:w,canonicalMatrix:F,atlasMatrix:D,projectiveMatrix:ue,canvasW:T,canvasH:v,screenPts:I,uvAffine:ye,uvSampleRect:he,textureTriangles:xe,textureEdgeRepairEdges:C,textureEdgeRepair:E,seamBleed:ne,seamBleedEdges:Q,seamBleedEdgeAmounts:H,seamBleedInsets:ee,bleedRatio:Pr(s.seamBleed),normal:g,textureTint:te,shadedColor:ce}}function Lf(e,n,t={},o,r){let s=yo(o),i=t,a=r??(t.textureEdgeRepairEdges?.size?{seamEdges:new Set,textureEdgeRepairEdges:t.textureEdgeRepairEdges}:void 0);return va(e,n,i,s,a)}function cs(e){return typeof e=="number"&&Number.isFinite(e)&&e>0}function by(e){return!!e&&typeof e.url=="string"&&e.url.length>0&&cs(e.width)&&cs(e.height)}function Py(e){let n=e.sourceRect;return n?!Number.isFinite(n.x)||!Number.isFinite(n.y)||!cs(n.width)||!cs(n.height)?null:{x:n.x,y:n.y,width:n.width,height:n.height}:{x:0,y:0,width:e.width,height:e.height}}function My(e,n,t){let o=e.split(",").map(Number);if(o.length!==16||o.some(i=>!Number.isFinite(i)))return null;let r=gn/n,s=gn/t;for(let i=0;i<4;i+=1)o[i]*=r;for(let i=4;i<8;i+=1)o[i]*=s;return an(o,6)}function ls(e,n={}){let t=go(e.polygon);if(!by(t))return null;let o=ho(e.polygon,{imageRendering:n.imageRendering,backend:n.backend,lighting:n.lighting,projection:n.projection});if(o.backend!=="image")return null;let r=o.lighting??"source";if(r!=="source")return null;let s=Py(t);if(!s)return null;let i=Math.max(1,s.width),a=Math.max(1,s.height),c=o.projection??"affine",l=c,u=null;if(c==="projective"){if(n.allowProjective===!1||e.screenPts.length!==8||e.polygon.vertices.length!==4)return null;let f=n.projectiveQuadGuards??yo(void 0),d=fo(e,e.polygon);if(!d)return null;let m=xo(e.screenPts,d.xAxis,d.yAxis,d.normal,d.tx,d.ty,d.tz,f,e.seamBleedEdgeAmounts);if(u=m?My(m,i,a):null,!u)return null}else l="affine",u=Ct(e,i,a);return{source:t,url:t.url,sourceRect:s,leafWidth:i,leafHeight:a,matrix:u,backgroundPosition:[-s.x,-s.y],backgroundSize:[t.width,t.height],imageRendering:o.imageRendering??"auto",lighting:r,projection:l}}function Ia(e){if(e.screenPts.length!==8)return null;let n=[],t=[],o=.001,r=(s,i)=>{for(let a of s)if(Math.abs(a-i)<=o)return;s.push(i)};for(let s=0;s<e.screenPts.length;s+=2)r(n,e.screenPts[s]),r(t,e.screenPts[s+1]);if(n.length!==2||t.length!==2||(n.sort((s,i)=>s-i),t.sort((s,i)=>s-i),Math.abs(n[0])>o||Math.abs(t[0])>o||n[1]-n[0]<=o||t[1]-t[0]<=o))return null;for(let s=0;s<e.screenPts.length;s+=2){let i=e.screenPts[s],a=e.screenPts[s+1],c=Math.abs(i-n[0])<=o||Math.abs(i-n[1])<=o,l=Math.abs(a-t[0])<=o||Math.abs(a-t[1])<=o;if(!c||!l)return null}return{left:n[0],top:t[0],width:n[1]-n[0],height:t[1]-t[0]}}function Dn(e){return!!Ia(e)}function nt(e){return!e.texture&&e.polygon.vertices.length===3}function It(e){return!e.texture&&!!e.projectiveMatrix&&!Dn(e)}function wf(e){let n=/\b(?:Chrome|HeadlessChrome|Chromium|Edg|OPR)\//.test(e);return/\bVersion\/[\d.]+.*\bSafari\//.test(e)&&!n}function us(e,n){e.set(n,(e.get(n)??0)+1)}function fs(e){let n,t=1;for(let[o,r]of e)r>t&&(n=o,t=r);return n}function Nf(e,n,t,o){let r=!t.has("b"),s=r&&o.projectiveQuadSupported,i=!t.has("u")&&o.solidTriangleSupported,a=!t.has("i")&&!!o.cornerShapeSupported,c=!t.has("i")&&o.borderShapeSupported,l=t.has("b");return e.map(u=>{if(u?.texture&&ls(u,{allowProjective:o.projectiveQuadSupported,backend:o.textureBackend,imageRendering:o.textureImageRendering,lighting:o.textureImageLighting,projection:o.textureProjection}))return null;if(!u||u.texture)return u;if(i&&nt(u))return null;let f=Dn(u);return a&&!f&&!(s&&It(u))&&Qr(u)!==null||r&&f||s&&It(u)||c&&(!f||l)?null:u})}function Df(e,n,t,o,r,s,i){let a=new Map,c=new Map,l=new Map,u=!t.has("b"),f=u&&o.projectiveQuadSupported,d=!t.has("u")&&o.solidTriangleSupported,m=!t.has("i")&&o.cornerShapeSupported,p=!t.has("i")&&o.borderShapeSupported;for(let y of e){if(!y||y.texture)continue;let x=m&&!!i?.(y);if(n==="dynamic"){if(!(d&&nt(y))&&!(u&&Dn(y))&&!(f&&It(y))&&!x&&!p)continue;let b=r(y.polygon.color??"#cccccc"),P=s(b);us(c,P),l.has(P)||l.set(P,b);continue}!(d&&nt(y))&&!(u&&Dn(y))&&!(f&&It(y))&&!x&&!p||us(a,y.shadedColor)}let h=fs(a),g=fs(c);return{paintColor:h,dynamicColorKey:g,dynamicColor:g?l.get(g):void 0}}function Sy(e,n){if((e+1)%3===n)return e;if((n+1)%3===e)return n}function Ay(e,n,t,o,r,s,i){if(!e?.size)return null;let a=mo(n,t);return[[s,o],[o,r],[r,s]].map(([l,u],f)=>{let d=Sy(l,u),m=d!==void 0&&e.has(d)?a:0;return po(i,f,m)})}function ms(e,n,t,o,r,s,i,a){let c=s,l="",u,f,d="";if(i){let m=a??e.color??"#cccccc",p=s.directionalLight,h=s.ambientLight,g=p?.direction??Wn,y=p?.color??jn,x=Math.max(0,p?.intensity??Hn),b=h?.color??Kn,P=Math.max(0,h?.intensity??Zn),A=Math.hypot(g[0],g[1],g[2])||1,C=g[0]/A,E=g[1]/A,S=g[2]/A,T=s.lightOccludedPolyIndices?.has(n)??!1?0:x*Math.max(0,t*C+o*E+r*S),v=Vn(m,T,y,b,P),I=s.textureLighting??"baked",_=I==="baked"&&c.stableTriangleColorSteps?Sr(v,c.stableTriangleColorSteps):v,O=yn(m),R=_===s.solidPaintDefaults?.paintColor,w=I==="dynamic"&&Mr(O)===s.solidPaintDefaults?.dynamicColorKey;l=I==="dynamic"||R?"":_,u=l?yn(l):void 0,f=l?ro(l):void 0,d=I==="dynamic"?`--pnx:${t.toFixed(4)};--pny:${o.toFixed(4)};--pnz:${r.toFixed(4)};`+(w?"":`--psr:${(O.r/255).toFixed(4)};--psg:${(O.g/255).toFixed(4)};--psb:${(O.b/255).toFixed(4)};`):""}return{index:n,polygon:e,colorComputed:i,bakedColor:l||void 0,bakedRgb:u,bakedAlpha:f,dynamicVars:d}}function Ff(e,n,t){if(e.texture||e.vertices.length!==3)return null;let o=t.tileSize??50,r=t.layerElevation??o,s=e.vertices[0],i=e.vertices[1],a=e.vertices[2],c=[s[1]*o,s[0]*o,s[2]*r],l=[i[1]*o,i[0]*o,i[2]*r],u=[a[1]*o,a[0]*o,a[2]*r],f=l[0]-c[0],d=l[1]-c[1],m=l[2]-c[2],p=u[0]-c[0],h=u[1]-c[1],g=u[2]-c[2],y=-(d*g-m*h),x=-(m*p-f*g),b=-(f*h-d*p),P=Math.sqrt(y*y+x*x+b*b);return P<=de?null:(y/=P,x/=P,b/=P,ms(e,n,y,x,b,t,!0))}function Bf(e,n,t,o={}){if(e.texture||e.vertices.length!==3)return null;let r=t.tileSize??50,s=t.layerElevation??r,i=e.vertices[0],a=e.vertices[1],c=e.vertices[2],l=i[1]*r,u=i[0]*r,f=i[2]*s,d=a[1]*r,m=a[0]*r,p=a[2]*s,h=c[1]*r,g=c[0]*r,y=c[2]*s;return ds(e,n,t,o,l,u,f,d,m,p,h,g,y)}function ds(e,n,t,o,r,s,i,a,c,l,u,f,d){let m=t,p=a-r,h=c-s,g=l-i,y=u-r,x=f-s,b=d-i,P=-(h*b-g*x),A=-(g*y-p*b),C=-(p*x-h*y),E=Math.sqrt(P*P+A*A+C*C);if(E<=de)return null;P/=E,A/=E,C/=E;let S=o.basis,M=S?.a??0,T=S?.b??1,v=S?.c??2;M<0||M>2||T<0||T>2||v<0||v>2||M===T||M===v||T===v?(S=void 0,M=0,T=1,v=2):M===0&&T===1&&v===2||M===1&&T===2&&v===0||M===2&&T===0&&v===1||(S=void 0,M=0,T=1,v=2);let I=()=>S?ds(e,n,t,{...o,basis:void 0},r,s,i,a,c,l,u,f,d):null;if(!S){let Ie=p*p+h*h+g*g,en=u-a,cn=f-c,nn=d-l,Ke=r-u,ln=s-f,it=i-d,un=en*en+cn*cn+nn*nn,Ze=Ke*Ke+ln*ln+it*it,at=Ie;un>at&&(M=1,T=2,v=0,at=un),Ze>at&&(M=2,T=0,v=1)}let _,O,R,w,F,D,K=v===0?r:v===1?a:u,H=v===0?s:v===1?c:f,Q=v===0?i:v===1?l:d;M===0?(_=r,O=s,R=i):M===1?(_=a,O=c,R=l):(_=u,O=f,R=d),T===0?(w=r,F=s,D=i):T===1?(w=a,F=c,D=l):(w=u,F=f,D=d);let ne=w-_,ee=F-O,ue=D-R,U=Math.sqrt(ne*ne+ee*ee+ue*ue);if(U<=de)return I();let $=ne/U,k=ee/U,L=ue/U,W=(K-_)*$+(H-O)*k+(Q-R)*L,Y=A*L-C*k,se=C*$-P*L,Z=P*k-A*$,B=E/U;if(B<=de)return I();let q=Math.max(0,Math.min(U,W)),me=Math.max(0,U-q),fe=[q,0,0,B,q+me,B],z=ur*(m.bleedRatio??1),te=Ay(m.seamEdges,m.seamBleed,z,M,T,v,fe),ce=te?Et(fe,te):Wr(q,me,B,mo(m.seamBleed,z)),ye=ce[0],he=ce[1],xe=ce[2],ge=ce[3],Ae=ce[4],Fe=ce[5],V=(ge+Fe)/2,G=ye-xe,j=Ae-ye,J=V-he;if(G<=de||j<=de||J<=de||!Number.isFinite(G+j+J))return I();let le=o.includeColor??!0,re=!1,pe,be,ie,ae="";if(le){let Ie=ms(e,n,P,A,C,t,!0,o.color);re=Ie.colorComputed,pe=Ie.bakedColor,be=Ie.bakedRgb,ie=Ie.bakedAlpha,ae=Ie.dynamicVars??""}let N=pe?`color:${pe};`:"",X=o.primitive??o.resolvedPrimitive??"border",ve=1/(X==="border-large"?pr:dr),Ce=G+j,Pe=Ce*ve,Oe=(j-G)*.5*ve,Re=J*ve,Ve=ye-q-Ce*.5,Xe=he,Te=$*Pe,Be=k*Pe,De=L*Pe,rt=$*Oe+Y*Re,st=k*Oe+se*Re,xn=L*Oe+Z*Re,Je=K+$*Ve+Y*Xe,bn=H+k*Ve+se*Xe,ke=Q+L*Ve+Z*Xe,je=o.matrixDecimals??jr(m.stableTriangleMatrixDecimals),Se=$r(Te,Be,De,rt,st,xn,P,A,C,Je,bn,ke,je),Le=t.textureLighting??"baked",_e=m.optimizeStableTriangleStyle===!0&&Le==="baked"?"":`transform:${Se};`+N+ae,He=S&&S.a===M&&S.b===T&&S.c===v?S:{a:M,b:T,c:v};return{index:n,polygon:e,styleText:_e,transformText:Se,basis:He,primitive:X,colorComputed:re,bakedColor:pe,bakedRgb:be,bakedAlpha:ie,dynamicVars:ae}}function ot(e){let n=typeof e=="string"?Number(e):e;return n===void 0||!Number.isFinite(n)?1:Math.min(tr,Math.max(nr,n))}function ps(e){return e.reduce((n,t)=>n+t.width*t.height,0)}function _a(e,n){let t=ps(e);if(t<=0)return 1;let o=Math.max(1,...e.map(i=>Math.max(i.width,i.height))),r=yt/o,s=Math.sqrt(n/(t*4));return ot(Math.min(r,s))}function Oa(e,n){let t=ps(e),o=.5;return t<=qt?o=1:t<=or&&(o=.75),ot(Math.min(o,_a(e,n)))}function Ra(e,n){return e.reduce((t,o)=>Math.max(t,Math.ceil(o.width*n),Math.ceil(o.height*n)),0)}function Va(e,n){return e.reduce((t,o)=>t+Math.ceil(o.width*n)*Math.ceil(o.height*n)*4,0)}function La(e,n,t){let o=Ra(e,n),r=Va(e,n),s=o>yt?yt/o:1,i=r>t?Math.sqrt(t/r):1;return Math.min(s,i)}function wa(e){return e?rr:sr}function Na(e,n){return e!==void 0&&e!=="auto"?Ye:n?Ye:gr}function kf(e,n){return Po(e,n,1,"canonical")}function Da(e,n,t,o=Fa(e)){let r=t??"canonical";if(r==="local")return{width:Math.max(1,e.canvasW||1),height:Math.max(1,e.canvasH||1),sizing:r};if(r==="raster"){let s=ot(n);return{width:Math.max(1,(e.canvasW||1)*s),height:Math.max(1,(e.canvasH||1)*s),sizing:r}}return{width:o,height:o,sizing:"canonical"}}function Po(e,n,t,o="canonical"){for(let r of e.entries){if(!r)continue;let s=Da(r,t,o,n);r.atlasCanonicalSize=n,r.atlasLeafSizing=s.sizing,r.atlasLeafWidth=s.width,r.atlasLeafHeight=s.height,r.atlasMatrix=Ct(r,s.width,s.height)}return e}function Fa(e){return e.atlasCanonicalSize??Ye}function Ba(e){return Math.max(Zt,Math.ceil(Zt/e))}function _t(e,n=1){let t=Array(e.length).fill(null),o=[],r=Ba(n),s=e.filter(c=>!!c).sort((c,l)=>l.canvasH-c.canvasH||l.canvasW-c.canvasW||c.index-l.index),i=()=>({width:r,height:r,entries:[],shelves:[]}),a=(c,l,u)=>{if(c.sealed)return null;for(let m of c.shelves)if(l.canvasH<=m.height&&m.x+l.canvasW+r<=En){let p={...l,pageIndex:u,x:m.x,y:m.y};return m.x+=l.canvasW+r*2,c.entries.push(p),c.width=Math.max(c.width,p.x+l.canvasW+r),p}let f=c.shelves.length===0?r:c.height+r;if(f+l.canvasH+r>En)return null;let d={...l,pageIndex:u,x:r,y:f};return c.shelves.push({x:r+l.canvasW+r*2,y:f,height:l.canvasH}),c.entries.push(d),c.width=Math.max(c.width,d.x+l.canvasW+r),c.height=Math.max(c.height,f+l.canvasH+r),d};for(let c of s){if(c.canvasW+r*2>En||c.canvasH+r*2>En){let f=o.length,d={...c,pageIndex:f,x:r,y:r};t[c.index]=d,o.push({width:c.canvasW+r*2,height:c.canvasH+r*2,entries:[d],shelves:[],sealed:!0});continue}let u=null;for(let f=0;f<o.length&&(u=a(o[f],c,f),!u);f++);if(!u){let f=i(),d=o.length;o.push(f),u=a(f,c,d)}u&&(t[c.index]=u)}return{entries:t,pages:o.map(({width:c,height:l,entries:u})=>({width:c,height:l,entries:u}))}}function Cy(e,n,t){let o=Oa(n.pages,t),r=o===1?n:_t(e,o);for(let s=0;s<4;s++){let i=La(r.pages,o,t);if(i>=1)break;let a=ot(o*i*ir);if(a>=o)break;o=a,r=_t(e,o)}return{packed:r,atlasScale:o}}function Uf(e,n,t,o="canonical"){let r=Na(n,t);if(n!==void 0&&n!=="auto"){let a=ot(n);return{packed:Po(_t(e,a),r,a,o),atlasScale:a,atlasCanonicalSize:r}}let s=_t(e,1),i=Cy(e,s,wa(t));return{packed:Po(i.packed,r,i.atlasScale,o),atlasScale:i.atlasScale,atlasCanonicalSize:r}}0&&(module.exports={ASYNC_RENDER_BUDGET_MS,ATLAS_CANONICAL_SIZE_AUTO_DESKTOP,ATLAS_CANONICAL_SIZE_EXPLICIT,ATLAS_MAX_SIZE,ATLAS_PADDING,AUTO_ATLAS_LOW_AREA,AUTO_ATLAS_MAX_BITMAP_SIDE,AUTO_ATLAS_MAX_DECODED_BYTES_DESKTOP,AUTO_ATLAS_MAX_DECODED_BYTES_MOBILE,AUTO_ATLAS_MEDIUM_AREA,AUTO_ATLAS_SCALE_GUARD,BAKED_SHADOW_MIN_UP,BAKED_SHADOW_Z_SQUASH,BASE_TILE,BASIS_EPS,BORDER_SHAPE_BLEED,BORDER_SHAPE_CANONICAL_SIZE,BORDER_SHAPE_CENTER_PERCENT,BORDER_SHAPE_POINT_EPS,CAMERA_BACKFACE_CULL_EPS,COLOR_PARSE_CACHE_MAX,CORNER_SHAPE_DUPLICATE_EPS,CORNER_SHAPE_POINT_EPS,DECIMAL_SCALES,DEFAULT_AMBIENT_COLOR,DEFAULT_AMBIENT_INTENSITY,DEFAULT_ATLAS_CSS_DECIMALS,DEFAULT_BORDER_SHAPE_DECIMALS,DEFAULT_CAMERA_STATE,DEFAULT_LIGHT_COLOR,DEFAULT_LIGHT_DIR,DEFAULT_LIGHT_INTENSITY,DEFAULT_MATRIX_DECIMALS,DEFAULT_PROJECTION,DEFAULT_SEAM_BLEED,DEFAULT_SEAM_FACET_SPLIT_OPTIONS,DEFAULT_SEAM_OVERLAP_OPTIONS,DEFAULT_TILE,LoopOnce,LoopPingPong,LoopRepeat,MAX_ATLAS_SCALE,MIN_ATLAS_SCALE,PROJECTIVE_QUAD_BLEED,PROJECTIVE_QUAD_DENOM_EPS,PROJECTIVE_QUAD_MAX_WEIGHT_RATIO,QUAT_IDENTITY,RECEIVER_NORMAL_TOL,RECEIVER_OFFSET_TOL,RECEIVER_OUTLINE_EXPAND,RECT_EPS,SEAM_LIGHT_EPS,SOLID_QUAD_CANONICAL_SIZE,SOLID_TRIANGLE_BLEED,SOLID_TRIANGLE_CANONICAL_SIZE,SOLID_TRIANGLE_LARGE_BORDER_CANONICAL_SIZE,SURFACE_DISTANCE_EPS,SURFACE_NORMAL_EPS,TEXTURE_EDGE_REPAIR_ALPHA_MIN,TEXTURE_EDGE_REPAIR_RADIUS,TEXTURE_EDGE_REPAIR_SOURCE_ALPHA_MIN,TEXTURE_TRIANGLE_BLEED,VOXEL_CAMERA_CULL_AXIS_EPS,VOXEL_CAMERA_CULL_NORMAL_LIMIT,applyPackedAtlasCanonicalSize,applyPackedAtlasLeafSizing,arrowPolygons,atlasArea,atlasBitmapMaxSide,atlasCanonicalSizeForEntry,atlasCanonicalSizeForTextureQuality,atlasDecodedBytes,atlasPadding,autoAtlasBudgetFactor,autoAtlasMaxDecodedBytes,autoAtlasScale,autoAtlasScaleCap,axesHelperPolygons,bakeSolidTextureSampledPolygons,bakeSolidTextureSamples,basisAxisKey,borderShapeBoundsFromPoints,borderShapeGeometryForPlan,boxPolygons,buildBakedShadowProjectionMatrix,buildBasisHints,buildEdgeOwners,buildPolyCameraSceneTransform,buildPolyMeshTransform,buildPolySceneTransform,buildSceneContext,buildSeamBleedPolygonEdges,buildSeamBleedPolygonSet,buildSharedEdgeMap,buildTextureEdgeRepairSets,cachedParsePureColor,cameraCullNormalGroups,cameraCullNormalGroupsFromPolygons,cameraCullNormalKey,cameraCullVisibleSignature,cameraFacingDepth,capturePolyCameraSnapshot,chooseIslandXAxis,chooseLocalBasis,clampChannel,classifyFacing,clipPolygonToConvex2D,colorErrorScore,compatibleBleedSurface,compatibleSurface,computeLightVisibility,computePlanSeamBleedEdgeAmounts,computeProjectiveQuadCoefficients,computeProjectiveQuadMatrix,computeReceiverShadowFaces,computeSceneBbox,computeSeamBleedInsets,computeShapeLighting,computeSolidTriangleColorPlan,computeSolidTriangleColorPlanFromNormal,computeSolidTrianglePlan,computeSolidTrianglePlanFromCssPoints,computeSurfaceNormal,computeTextureAtlasPlan,computeTextureAtlasPlanPublic,computeTexturePaintMetrics,computeUvAffine,computeUvSampleRect,conePolygons,convexHull2D,cornerShapeDiagonal,cornerShapeGeometryForPlan,cornerShapePointSides,coverPlanarPolygons,createIsometricCamera,createPolyAnimationMixer,crossVec,cssBorderShapeForGeometry,cssBorderShapeForPlan,cssDistanceToWorld,cssPoints,cssPositionToWorld,cullInteriorPolygons,cylinderPolygons,dedupeOverlappingPolygons,dodecahedronPolygons,dominantCountKey,dotVec,ensureCcw2D,eulerXYZFromQuat,evaluateIslandAxis,expandClipPoints,expandConvexHullOutward,extractSilhouetteLoops,filterAtlasPlans,findOverlappingPolygonDuplicates,formatAffineMatrix3dColumns,formatAffineMatrix3dScalars,formatAffineMatrix3dTransformScalars,formatAtlasMatrix,formatBorderShapeElementStyle,formatBorderShapeEntryMatrix,formatBorderShapeMatrix,formatColor,formatCornerShapeElementStyle,formatCssLength,formatCssLengthPx,formatMatrix3d,formatMatrix3dValues,formatPercent,formatScaledMatrixFromPlan,formatSolidQuadEntryMatrix,formatSolidQuadMatrix,fullRectBounds,getPolygonBasisInfo,getSolidPaintDefaultsForPlansCore,groupReceiverFaceGroups,icosahedronPolygons,incrementCount,intersect2DLines,intersect2DLinesRaw,inverseRotateVec3,isAxisAlignedSurfaceNormal,isBakedShadowCaster,isBasisOptimizable,isConvexPolygonPoints,isFullRectBasis,isFullRectSolid,isProjectiveQuadPlan,isSolidTrianglePlan,isVoxelCameraCullableNormalGroups,loadMesh,makeLocalBasis,mergePolygons,meshScaleVec3,normalFacesCamera,normalizeAtlasScale,normalizeInvertMultiplier,normalizePolygons,normalizedSeamBleed,octahedronPolygons,offsetConvexPolygonPoints,offsetConvexPolygonPointsByEdgeAmounts,offsetStableTrianglePoints,offsetTrianglePoints,optimizeAnimatedMeshPolygons,optimizeMeshParseResult,optimizeMeshPolygons,packTextureAtlasPlans,packTextureAtlasPlansWithScaleCore,parseAlpha,parseColor,parseGltf,parseHex,parseHexColor,parseMtl,parseObj,parsePureColor,parseRgbColor,parseStl,parseVox,planePolygons,polyCameraTargetToCss,polyCssDistanceToWorld,polyCssPositionToWorld,polygonContainsPoint,polygonCssSurfaceNormal,polygonFaces,polygonFacesCamera,polygonSignedArea2D,prepareCasterEdgeOwners,prepareCasterPolyItems,prepareReceiverFacePlanes,projectCssVertexToGround,projectTextureTriangle,quantizeCssColor,quatFromAxisAngle,quatFromEulerXYZ,quatMultiply,repairMeshSeams,resolveAtlasLeafBox,resolveBleedRatio,resolvePolyCameraAppliedPerspectiveStyle,resolvePolyTextureImageRendering,resolvePolyTextureImageSource,resolvePolyTextureLeafGeometry,resolvePolyTexturePresentation,resolvePolyTextureUrl,resolveProjectiveQuadGuards,resolveSeamBleed,rgbEqual,rgbKey,rgbToCss,rgbToHex,ringPolygons,ringQuadPolygons,rotateVec3,rotateVec3InWrapperCssFrame,roundDecimal,safariCssProjectiveUnsupported,safePlanSeamBleedAmount,screenToWorldOnSphere,screenToWorldRay,seamBleedAmountArray,seamFacetSplitPolygons,seamFacetSplitReport,seamLightBrightness,seamOverlapDiagnostics,seamOverlapPolygons,seamOverlapReport,shadeColor,shadePolygon,sharedCornerShapeSide,signedArea2D,simplifyCornerShapePoints,simplifyTriangleMeshPolygons,spherePolygons,stableBasisFromPlan,stableTriangleMatrixDecimals,stepRgbToward,tetrahedronPolygons,textureTintFactors,tintToCss,torusPolygons,worldCssForMesh,worldDirectionToCss,worldDirectionToPolyCss,worldDirectionalLightToCss,worldDirectionalLightToPolyCss,worldDistanceToCss,worldDistanceToPolyCss,worldPositionToCss,worldPositionToPolyCss});
|