dielines 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/dist/dielines.js +4814 -0
- package/dist/dielines.js.map +1 -0
- package/dist/dielines.umd.cjs +18 -0
- package/dist/dielines.umd.cjs.map +1 -0
- package/dist/types/Dieline.d.ts +23 -0
- package/dist/types/FoldLockBump.d.ts +16 -0
- package/dist/types/drawFlaps.d.ts +26 -0
- package/dist/types/linearDistribution.d.ts +18 -0
- package/dist/types/main.d.ts +4 -0
- package/package.json +48 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
(function(q,nt){typeof exports=="object"&&typeof module<"u"?nt(exports):typeof define=="function"&&define.amd?define(["exports"],nt):(q=typeof globalThis<"u"?globalThis:q||self,nt(q.Dielines={}))})(this,function(q){"use strict";function nt(r,t,e,s){return r<=s&&t>=e}class ot{constructor(t=1/0,e=1/0,s=-1/0,n=-1/0){this.xMin=t,this.yMin=e,this.xMax=s,this.yMax=n}get width(){return this.xMax-this.xMin}get height(){return this.yMax-this.yMin}contains(t){const[e,s]=t;return nt(this.xMin,this.xMax,e,e)&&nt(this.yMin,this.yMax,s,s)}overlaps(t){return nt(this.xMin,this.xMax,t.xMin,t.xMax)&&nt(this.yMin,this.yMax,t.yMin,t.yMax)}addPoint(t){const[e,s]=t;return new ot(Math.min(this.xMin,e),Math.min(this.yMin,s),Math.max(this.xMax,e),Math.max(this.yMax,s))}merge(t){return new ot(Math.min(this.xMin,t.xMin),Math.min(this.yMin,t.yMin),Math.max(this.xMax,t.xMax),Math.max(this.yMax,t.yMax))}}function ms(r){return r.reduce((t,e)=>t.addPoint(e),new ot)}const le=(r,t=1e-9)=>{let e=r;return Math.abs(r)<t&&(e=0),e.toFixed(-Math.log10(t))};function it(r,t=1e-9){return Array.from(new Map(r.map(([e,s])=>[`[${le(e,t)},${le(s,t)}]`,[e,s]])).values())}const bt=Math.PI/180,Ut=180/Math.PI,F=r=>`[${r[0]}, ${r[1]}]`,b=([r,t],[e,s],n=1e-9)=>Math.abs(r-e)<=n&&Math.abs(t-s)<=n,$=([r,t],[e,s])=>[r+e,t+s],j=([r,t],[e,s])=>[r-e,t-s],Rt=([r,t])=>r*r+t*t,I=([r,t],e)=>[r*e,t*e],Pt=([r,t],[e,s]=[0,0])=>(r-e)**2+(t-s)**2,B=(r,t=[0,0])=>Math.sqrt(Pt(r,t));function ht([r,t],[e,s]){return r*s-t*e}function Yt([r,t],[e,s]){return r*e+t*s}function H([r,t]){const e=B([r,t]);return[r/e,t/e]}function at(r,t){const e=Math.cos(t)*r,s=Math.sin(t)*r;return[e,s]}function gs([r,t]){return Math.atan2(t,r)}function ue(r){const t=B(r),e=gs(r);return[t,e]}function Dt(r,t,e=1e-9){const s=ht(r,t),n=Rt(r),o=Rt(t);return s*s<n*o*e*e}function O(r){return[-r[1],r[0]]}function xt(r){return[r[1],-r[0]]}function ps(r,t,e){return b(e,r)||Dt(j(e,r),t)}const ft=(r,t)=>{const[e,s,n,o,i,h,l,a,c]=r,[u,m,p,g,d,M,f,w,y]=t;return[e*u+s*g+n*f,e*m+s*d+n*w,e*p+s*M+n*y,o*u+i*g+h*f,o*m+i*d+h*w,o*p+i*M+h*y,l*u+a*g+c*f,l*m+a*d+c*w,l*p+a*M+c*y]},ws=r=>{const[t,e,s,n,o,i,h,l,a]=r,c=t*(o*a-i*l)-e*(n*a-i*h)+s*(n*l-o*h);return[(o*a-i*l)/c,(s*l-e*a)/c,(e*i-s*o)/c,(i*h-n*a)/c,(t*a-s*h)/c,(s*n-t*i)/c,(n*l-o*h)/c,(e*h-t*l)/c,(t*o-e*n)/c]},ds=r=>{const[t,e,s,n,o,i,h,l,a]=r;return[t,n,h,e,o,l,s,i,a]};class z{constructor(t){this._matrix=[1,0,0,0,1,0,0,0,1],t&&(this._matrix=[...t])}clone(){return new z(this._matrix)}transpose(){return this._matrix=ds(this._matrix),this}inverse(){return this._matrix=ws(this._matrix),this}translate(t,e){return this._matrix=ft(this._matrix,[1,0,t,0,1,e,0,0,1]),this}rotate(t,e){const s=Math.cos(t),n=Math.sin(t),o=[s,-n,0,n,s,0,0,0,1];return e&&this.translate(e[0],e[1]),this._matrix=ft(this._matrix,o),e&&this.translate(-e[0],-e[1]),this}mirrorX(){return this._matrix=ft(this._matrix,[1,0,0,0,-1,0,0,0,1]),this}mirrorY(){return this._matrix=ft(this._matrix,[-1,0,0,0,1,0,0,0,1]),this}mirrorLine(t,e){const[s,n]=t,o=Math.atan2(n,s);return e&&this.translate(e[0],e[1]),this.rotate(o),this.mirrorX(),this.rotate(-o),e&&this.translate(-e[0],-e[1]),this}mirrorCenter(t){return t&&this.translate(t[0],t[1]),this._matrix=ft(this._matrix,[-1,0,0,0,-1,0,0,0,1]),t&&this.translate(-t[0],-t[1]),this}scale(t,e){return e&&this.translate(e[0],e[1]),this._matrix=ft(this._matrix,[t,0,0,0,t,0,0,0,1]),e&&this.translate(-e[0],-e[1]),this}transform(t){const[e,s]=t,[n,o,i,h,l,a]=this._matrix;return[n*e+o*s+i,h*e+l*s+a]}transformAngle(t){const[e,s]=this.transform([Math.cos(t),Math.sin(t)]),[n,o]=this.transform([0,0]);return Math.atan2(s-o,e-n)}keepsOrientation(){const[t,,,,e]=this._matrix;return t*e>0}scaleFactor(){const[t,,,e]=this._matrix;return Math.sqrt(t*t+e*e)}}let lt=class{translateX(t){const e=new z().translate(t,0);return this.transform(e)}translateY(t){const e=new z().translate(0,t);return this.transform(e)}translate(t,e){const s=new z().translate(t,e);return this.transform(s)}translateTo([t,e]){const s=new z().translate(t,e);return this.transform(s)}rotate(t,e){const s=new z().rotate(t*bt,e);return this.transform(s)}scale(t,e){const s=new z().scale(t,e);return this.transform(s)}mirrorCenter(t){const e=new z().mirrorCenter(t);return this.transform(e)}mirror(t="x",e){const s=new z;return t==="x"?s.mirrorX():t==="y"?s.mirrorY():s.mirrorLine(t,e),this.transform(s)}};class Xt extends lt{constructor(t,e){super(),this.firstPoint=t,this.lastPoint=e,this.precision=1e-9,this.firstPoint=t,this.lastPoint=e}get repr(){return`${this.segmentType} ${F(this.firstPoint)} - ${F(this.lastPoint)}`}get info(){return this.repr}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}class v extends Xt{constructor(){super(...arguments),this.segmentType="LINE",this._V=null,this._slope=null,this._yIntercept=null,this._boundingBox=null}isValidParameter(t){const e=this.length*this.precision;return t>=-e&&1-t>=-e}paramPoint(t){return $(this.firstPoint,I(this.V,t))}get length(){return B(this.firstPoint,this.lastPoint)}get squareLength(){return Pt(this.firstPoint,this.lastPoint)}get V(){return this._V===null&&(this._V=j(this.lastPoint,this.firstPoint)),this._V}get slope(){if(this._slope===null){const[t,e]=this.V;this._slope=e/t}return this._slope}get yIntercept(){return this._yIntercept===null&&(this._yIntercept=this.firstPoint[1]-this.slope*this.firstPoint[0]),this._yIntercept}get midPoint(){return $(this.firstPoint,I(this.V,.5))}isSame(t){return t instanceof v?b(this.firstPoint,t.firstPoint)&&b(this.lastPoint,t.lastPoint)||b(this.lastPoint,t.firstPoint)&&b(this.firstPoint,t.lastPoint):!1}clone(){return new v(this.firstPoint,this.lastPoint)}reverse(){return new v(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new ot(Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)),this._boundingBox}distanceFrom(t){const e=j(t,this.firstPoint),s=Yt(e,this.V)/this.squareLength;if(s<0)return B(t,this.firstPoint);if(s>1)return B(t,this.lastPoint);const n=this.paramPoint(s);return B(t,n)}isOnSegment(t){if(b(t,this.firstPoint,this.precision))return!0;const e=j(t,this.firstPoint);if(!Dt(this.V,e))return!1;const s=Yt(e,this.V)/this.squareLength;return this.isValidParameter(s)}gradientAt(t){return this.V}tangentAt(t){if(!this.isOnSegment(t))throw new Error("Point is not on segment");return H(this.V)}get normalVector(){return O(H(this.V))}get tangentAtFirstPoint(){return H(this.V)}get tangentAtLastPoint(){return H(this.V)}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t],e.forEach(l=>{if(!this.isOnSegment(l))throw new Error(`Point ${F(l)} is not on segment ${this.repr}`)});const s=[this.firstPoint,...e,this.lastPoint],n=it(s),o=this.lastPoint[0]-this.firstPoint[0];let i=Math.sign(o),h=0;return Math.abs(o)<this.precision&&(i=Math.sign(this.lastPoint[1]-this.firstPoint[1]),h=1),n.sort((l,a)=>i*(l[h]-a[h])),n.flatMap((l,a)=>a===n.length-1?[]:new v(l,n[a+1]))}transform(t){return new v(t.transform(this.firstPoint),t.transform(this.lastPoint))}}function ys(r){return Array.from(Array(r).keys())}function Z(r){const t=Math.min(...r.map(e=>e.length));return ys(t).map(e=>r.map(s=>s[e]))}function ut(r,t=1e-9){return r<0?r+2*Math.PI:r>=2*Math.PI?r%(2*Math.PI):r>2*Math.PI-t?0:r}function Tt(r,t,e,s=1e-9){let n=t-r;return e&&(n=-n),n<0&&(n+=2*Math.PI),n>2*Math.PI-s?0:n}const $t=(r,t,e)=>{const s=ht(r.V,t.V),n=Rt(r.V),o=Rt(t.V),i=e?e*e:r.precision*t.precision;if(s*s<n*o*i)return"parallel";const h=j(t.firstPoint,r.firstPoint),l=ht(h,t.V)/s,a=ht(h,r.V)/s;return{intersectionParam1:l,intersectionParam2:a}};function ce(r,t,e=!1,s){const n=$t(r,t,s);if(n==="parallel"){if(!e)return null;if(r.isSame(t))return r;const h=it([t.isOnSegment(r.firstPoint)?r.firstPoint:null,t.isOnSegment(r.lastPoint)?r.lastPoint:null,r.isOnSegment(t.firstPoint)?t.firstPoint:null,r.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(l=>l!==null)).sort((l,a)=>l[0]-a[0]);if(h.length===0||h.length===1)return null;if(h.length===2)return new v(h[0],h[1]);throw console.error(h),new Error("Unexpected number of points while intersecting parallel lines")}const{intersectionParam1:o,intersectionParam2:i}=n;return!r.isValidParameter(o)||!t.isValidParameter(i)?null:r.paramPoint(o)}const _t=(r,t)=>{const e=j(r,t);return ue(e)};class S extends Xt{constructor(t,e,s,n=!1,{ignoreChecks:o=!1}={}){if(super(t,e),this.segmentType="ARC",this._coefficients=null,this._angularLength=null,this._radius=null,this._firstAngle=null,this._lastAngle=null,this._boundingBox=null,this.center=s,this.clockwise=n,!o){if(b(t,e))throw new Error("Invalid arc, cannot be a full circle");if(Math.abs(this.radius-B(this.lastPoint,this.center))>this.precision)throw new Error(`Invalid arc, radius does not match between ${F(t)} and ${F(e)}} (center ${F(s)})`)}}get info(){return`ARC(${F(this.firstPoint)}, ${F(this.lastPoint)}, ${F(this.center)}, ${this.clockwise?"CW":"CCW"})`}get coefficients(){if(this._coefficients===null){const[t,e]=this.center,s=this.radius*this.radius;this._coefficients={x2:1/s,xy:0,y2:1/s,x:-(2*t)/s,y:-(2*e)/s,c:(t*t+e*e-s)/s}}return this._coefficients}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}angleToParam(t){return Tt(this.firstAngle,ut(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=Tt(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return $(this.center,at(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[e,s]=_t(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error(`Point ${F(t)} is not on segment ${this.repr}`);const n=this.angleToParam(s);if(!this.isValidParameter(n))throw new Error(`Point ${F(t)} is not on segment ${this.repr}`);return n}get radius(){return this._radius===null&&(this._radius=B(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,e]=j(this.firstPoint,this.center);this._firstAngle=ut(Math.atan2(e,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,e]=j(this.lastPoint,this.center);this._lastAngle=ut(Math.atan2(e,t))}return this._lastAngle}get length(){return this.radius*this.angularLength}get squareLength(){return this.length*this.length}get midPoint(){return this.paramPoint(.5)}isSame(t){return!(t instanceof S)||!b(this.center,t.center)?!1:b(this.firstPoint,t.firstPoint)&&b(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||b(this.lastPoint,t.firstPoint)&&b(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new S(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new S(this.lastPoint,this.firstPoint,this.center,!this.clockwise)}get boundingBox(){if(this._boundingBox===null){const t=this.radius+this.precision,e=s=>this.isValidParameter(this.angleToParam(s));this._boundingBox=new ot(e(Math.PI)?this.center[0]-t:Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,e(Math.PI*1.5)?this.center[1]-t:Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,e(0)?this.center[0]+t:Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,e(Math.PI/2)?this.center[1]+t:Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)}return this._boundingBox}distanceFrom(t){const[e,s]=_t(t,this.center);return this.isValidParameter(this.angleToParam(s))?Math.abs(e-this.radius):Math.sqrt(Math.min(Pt(t,this.firstPoint),Pt(t,this.lastPoint)))}isOnSegment(t){if(b(t,this.firstPoint)||b(t,this.lastPoint))return!0;const[e,s]=_t(t,this.center);if(Math.abs(e-this.radius)>this.precision)return!1;const n=this.angleToParam(s);return this.isValidParameter(n)}gradientAt(t){const e=this.firstAngle+t*this.angularLength*(this.clockwise?-1:1),s=this.radius*this.angularLength,n=-s*Math.sin(e),o=s*Math.cos(e);return this.clockwise?[-n,-o]:[n,o]}tangentAt(t){const[e,s]=_t(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error("Point is not on the arc");const n=this.angleToParam(s);if(!this.isValidParameter(n))throw new Error("Point is not on the arc");const o=at(1,s);return(this.clockwise?xt:O)(H(o))}get tangentAtFirstPoint(){const t=at(1,this.firstAngle);return(this.clockwise?xt:O)(H(t))}get tangentAtLastPoint(){const t=at(1,this.lastAngle);return(this.clockwise?xt:O)(H(t))}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const s=[0,1,...e.map(i=>this.pointToParam(i))],n=new Map(Z([s,[this.firstPoint,this.lastPoint,...e]]));s.sort((i,h)=>i-h);let o=null;return s.flatMap((i,h)=>{if(h===s.length-1)return[];const l=s[h+1];if(l-i<this.precision)return o===null&&(o=i),[];const a=o===null?i:o,c=new S(n.get(a)||this.paramPoint(a),n.get(l)||this.paramPoint(l),this.center,this.clockwise);return o=null,c})}transform(t){return new S(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}}function Ms(r,t,e){const s=new v(t,r),n=new v(t,e),o=O(s.tangentAtFirstPoint),i=O(n.tangentAtLastPoint),h=$t({firstPoint:s.midPoint,V:o,precision:1e-9},{firstPoint:n.midPoint,V:i,precision:1e-9});if(h==="parallel")throw new Error("Cannot create an arc from three colinear points");const l=ht(j(r,t),j(e,t))>0;return new S(r,e,$(s.midPoint,I(o,h.intersectionParam1)),l,{ignoreChecks:!0})}function fe(r,t,e){const s=new v(t,r),n=O(s.tangentAtFirstPoint),o=$t({firstPoint:s.midPoint,V:n,precision:1e-9},{firstPoint:r,V:O(e),precision:1e-9});if(o==="parallel")throw new Error("Cannot create an arc from three colinear points");const i=$(s.midPoint,I(n,o.intersectionParam1)),h=ht(j(i,r),j(i,$(r,e)))<0;return new S(r,t,i,h,{ignoreChecks:!0})}function bs(r,t){const e=j(t,r.firstPoint),s=Yt(e,r.V)/r.squareLength;return r.paramPoint(s)}function Ft(r,t,e){const s=e||r.precision,n=bs(r,t.center),o=B(n,t.center);if(o>t.radius+s)return[];if(Math.abs(o-t.radius)<s){const u=n;return r.isOnSegment(u)&&t.isOnSegment(u)?[u]:[]}const i=[],h=Math.sqrt(t.radius*t.radius-o*o),l=r.tangentAtFirstPoint,a=$(n,I(l,h));r.isOnSegment(a)&&t.isOnSegment(a)&&i.push(a);const c=$(n,I(l,-h));return r.isOnSegment(c)&&t.isOnSegment(c)&&i.push(c),i}const Ps=r=>{const{firstPoint:t,lastPoint:e,center:s,clockwise:n}=r;return new S(e,t,s,n,{ignoreChecks:!0})},xs=(r,t)=>{if(r.isSame(t))return[r];const e=it([t.isOnSegment(r.firstPoint)?r.firstPoint:null,t.isOnSegment(r.lastPoint)?r.lastPoint:null,r.isOnSegment(t.firstPoint)?t.firstPoint:null,r.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(s=>s!==null)).sort((s,n)=>r.pointToParam(s)-r.pointToParam(n));if(e.length===0)return[];if(e.length===1)return[];if(e.length===2)return r.isSame(Ps(t))?[]:[new S(e[0],e[1],r.center,r.clockwise)];if(e.length===3){const s=b(e[0],t.lastPoint)||b(e[0],t.firstPoint)?1:0;return[new S(e[0+s],e[1+s],r.center,r.clockwise)]}else if(e.length===4)return[new S(e[0],e[1],r.center,r.clockwise),new S(e[2],e[3],r.center,r.clockwise)];throw new Error("Bug in the arc arc overlap algorithm")};function me(r,t,e=!1,s){const n=s||r.precision,o=B(r.center,t.center),i=r.radius+t.radius;if(o>i+n)return[];const h=Math.abs(r.radius-t.radius);if(o<h-n)return[];if(o<n)return h>n?[]:e?xs(r,t):[];const l=H(j(t.center,r.center)),a=o>i-n;if(a||Math.abs(o-h)<n){const f=a||r.radius>t.radius?1:-1,w=$(r.center,I(l,f*r.radius));return r.isOnSegment(w)&&t.isOnSegment(w)?[w]:[]}const c=r.radius*r.radius/(2*o)-t.radius*t.radius/(2*o)+o/2,u=$(r.center,I(l,c)),m=Math.sqrt(r.radius*r.radius-c*c),p=O(l),g=$(u,I(p,m)),d=$(u,I(p,-m)),M=[];return r.isOnSegment(g)&&t.isOnSegment(g)&&M.push(g),r.isOnSegment(d)&&t.isOnSegment(d)&&M.push(d),M}let U=class kt extends Xt{constructor(t,e,s,n,o,i,h=!1,{ignoreChecks:l=!1,angleUnits:a="deg"}={}){super(t,e),this.segmentType="ELLIPSE_ARC",this.precision=1e-6,this._coefficients=null,this.center=s;const c=n>=o;this.majorRadius=c?n:o,this.minorRadius=c?o:n;const u=a==="deg"?i*bt:i;if(this.tiltAngle=ut(c?u:u+Math.PI/2),this.clockwise=h,!l){if(b(t,e))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${F(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(e))throw new Error(`Last point ${F(e)} not on the ellipse defined by ${this.info}`);if(Math.abs(this.majorRadius-this.minorRadius)<this.precision)throw new Error("Both radii should be different, create an arc instead")}}get coefficients(){if(this._coefficients===null){const[t,e]=this.center,s=this.majorRadius*this.majorRadius,n=this.minorRadius*this.minorRadius,o=Math.cos(this.tiltAngle),i=Math.sin(this.tiltAngle),h=s*i*i+n*o*o,l=2*(n-s)*i*o,a=s*o*o+n*i*i;this._coefficients={x2:h,xy:l,y2:a,x:-2*h*t-l*e,y:-2*a*e-l*t,c:h*t*t+l*t*e+a*e*e-s*n}}return this._coefficients}get info(){return`ELLIPSE_ARC(${F(this.firstPoint)}, ${F(this.lastPoint)}, ${F(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*Ut}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new kt(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new kt(this.firstPoint,this.lastPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}computeEllipseExtrema(){const t=Math.tan(this.tiltAngle),e=this.minorRadius/this.majorRadius,s=-Math.atan(e*t),n=Math.atan(e/t);return[s,Math.PI+s,n,Math.PI+n].map(o=>this.thetaToParam(o)).filter(o=>this.isValidParameter(o)).map(o=>this.paramPoint(o))}get boundingBox(){if(this._boundingBox===void 0){const t=this.computeEllipseExtrema(),e=[this.firstPoint,this.lastPoint,...t];this._boundingBox=ms(e)}return this._boundingBox}get linearExentricity(){return this._linearExentricity===void 0&&(this._linearExentricity=Math.sqrt(this.majorRadius*this.majorRadius-this.minorRadius*this.minorRadius)),this._linearExentricity}get exentricity(){return this._exentricity===void 0&&(this._exentricity=this.linearExentricity/this.majorRadius),this._exentricity}get focals(){if(this._focals===void 0){const t=this.majorAxis;this._focals=[$(this.center,I(t,this.linearExentricity)),$(this.center,I(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return at(1,this.tiltAngle)}paramPoint(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1);return this.reverseEllipseReferenceFrameTransform.transform([this.majorRadius*Math.cos(e),this.minorRadius*Math.sin(e)])}pointToParam(t){if(!this.isPointOnEllipse(t))throw new Error(`Point ${F(t)} not on the ellipse defined by ${this.repr}`);const e=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(e))throw new Error(`Point ${F(t)} is not on segment ${this.repr}`);return e}get midPoint(){return this.paramPoint(.5)}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}isSame(t){return b(this.center,t.center)&&Math.abs(this.majorRadius-t.majorRadius)<this.precision&&Math.abs(this.minorRadius-t.minorRadius)<this.precision&&(Math.abs(this.tiltAngle-t.tiltAngle)<this.precision||Math.abs(Math.abs(this.tiltAngle-t.tiltAngle)-Math.PI)<this.precision)&&(b(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||b(this.firstPoint,t.lastPoint)&&this.clockwise!==t.clockwise)}pointTheta(t){const e=this.ellipseReferenceFrameTransform.transform(t),s=Math.atan2(e[1]/this.minorRadius,e[0]/this.majorRadius);return ut(s)}thetaToParam(t){return Tt(this.firstAngle,ut(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[e,s]=this.focals,n=B(t,e),o=B(t,s);return Math.abs(2*this.majorRadius-n-o)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let e;if(b(t,this.center)?e=$(this.center,I(O(this.majorAxis),this.minorRadius)):e=this.reverseEllipseReferenceFrameTransform.transform(As(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return B(t,e);if(ps(t,this.majorAxis,this.center)){const s=ut(2*Math.PI-this.pointTheta(t)),n=this.thetaToParam(s);if(this.isValidParameter(n))return B(t,this.paramPoint(n))}return Math.min(B(t,this.firstPoint),B(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new z().rotate(-this.tiltAngle).translate(-this.center[0],-this.center[1])),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new z().translate(this.center[0],this.center[1]).rotate(this.tiltAngle)),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new z().rotate(this.tiltAngle)),this._rotateFromEllipseReferenceFrame}get firstAngle(){return this._firstAngle===void 0&&(this._firstAngle=this.pointTheta(this.firstPoint)),this._firstAngle}get lastAngle(){return this._lastAngle===void 0&&(this._lastAngle=this.pointTheta(this.lastPoint)),this._lastAngle}get deltaAngle(){return this._deltaAngle===void 0&&(this._deltaAngle=Tt(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const e=this.tangentAt(t);return this.clockwise?O(e):xt(e)}gradientAt(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),s=-this.majorRadius*this.deltaAngle*Math.sin(e),n=this.minorRadius*this.deltaAngle*Math.cos(e),o=this.clockwise?[-s,-n]:[s,n];return this.rotateFromEllipseReferenceFrame.transform(o)}tangentAt(t){const e=this.pointTheta(t),s=-this.majorRadius*Math.sin(e),n=this.minorRadius*Math.cos(e),o=this.clockwise?[-s,-n]:[s,n];return H(this.rotateFromEllipseReferenceFrame.transform(o))}get tangentAtFirstPoint(){const t=-this.majorRadius*Math.sin(this.firstAngle),e=this.minorRadius*Math.cos(this.firstAngle),s=this.clockwise?[-t,-e]:[t,e];return H(this.rotateFromEllipseReferenceFrame.transform(s))}get tangentAtLastPoint(){const t=-this.majorRadius*Math.sin(this.lastAngle),e=this.minorRadius*Math.cos(this.lastAngle),s=this.clockwise?[-t,-e]:[t,e];return H(this.rotateFromEllipseReferenceFrame.transform(s))}transform(t){const e=t.transformAngle(this.tiltAngle),s=t.scaleFactor();return new kt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),this.majorRadius*s,this.minorRadius*s,e,t.keepsOrientation()?this.clockwise:!this.clockwise,{angleUnits:"rad"})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const s=[0,1,...e.map(i=>this.pointToParam(i))],n=new Map(Z([s,[this.firstPoint,this.lastPoint,...e]]));s.sort((i,h)=>i-h);let o=null;return s.flatMap((i,h)=>{if(h===s.length-1)return[];const l=s[h+1];if(l-i<this.precision)return o===null&&(o=i),[];const a=o===null?i:o,c=new kt(n.get(a)||this.paramPoint(a),n.get(l)||this.paramPoint(l),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return o=null,c})}};function Es(r,t,e,s,n,o,i){const{center:h,rx:l,ry:a}=vs(r,t,e,s,n*bt,!o,i);return Math.abs(l-a)<1e-9?new S(r,t,h,i):new U(r,t,h,l,a,n,i)}function vs([r,t],[e,s],n,o,i,h,l){if(n<0&&(n=-n),o<0&&(o=-o),n==0||o==0)throw Error("rx and ry can not be 0");const a=Math.sin(i),c=Math.cos(i),u=(r-e)/2,m=(t-s)/2,p=(r+e)/2,g=(t+s)/2,d=c*u+a*m,M=c*m-a*u,f=d*d/(n*n)+M*M/(o*o);f>1&&(n=n*Math.sqrt(f),o=o*Math.sqrt(f));const w=n*o,y=n*M,x=o*d,E=y*y+x*x;if(!E)throw Error("start point can not be same as end point");let R=Math.sqrt(Math.abs((w*w-E)/E));h==l&&(R=-R);const k=R*y/o,P=-R*x/n,T=c*k-a*P+p,_=a*k+c*P+g;return{center:[T,_],rx:n,ry:o}}function As(r,t,e){const s=Math.abs(e[0]),n=Math.abs(e[1]);let o=.707,i=.707;const h=r,l=t;for(let a=0;a<3;a++){const c=h*o,u=l*i,m=(h*h-l*l)*o**3/h,p=(l*l-h*h)*i**3/l,g=c-m,d=u-p,M=s-m,f=n-p,w=Math.hypot(g,d),y=Math.hypot(M,f);o=Math.min(1,Math.max(0,(M*w/y+m)/h)),i=Math.min(1,Math.max(0,(f*w/y+p)/l));const x=Math.hypot(o,i);o/=x,i/=x}return[h*o*Math.sign(e[0]),l*i*Math.sign(e[1])]}function Wt(r,t,e=1e-9){const s=r.transform(t.ellipseReferenceFrameTransform),n=s.slope,o=s.yIntercept,i=t.majorRadius*t.majorRadius,h=t.minorRadius*t.minorRadius,l=t.majorRadius*t.minorRadius,a=s.slope*s.slope,c=s.yIntercept*s.yIntercept,u=f=>f.map(w=>t.reverseEllipseReferenceFrameTransform.transform(w)).filter(w=>r.isOnSegment(w)&&t.isOnSegment(w));if(!Number.isFinite(n)){const f=s.firstPoint[0];if(Math.abs(f)-t.majorRadius>e)return[];if(Math.abs(Math.abs(f)-t.majorRadius)<e)return u([[f,0]]);const w=t.minorRadius*Math.sqrt(1-f*f/i),y=[f,w],x=[f,-w];return u([y,x])}const m=i*a+h-c;if(m<-e)return[];const p=i*a+h;if(Math.abs(m)<e){const f=-(i*n*o)/p,w=h*o/p;return u([[f,w]])}const g=Math.sqrt(m),d=[-(i*n*o+l*g)/p,(h*o-l*n*g)/p],M=[-(i*n*o-l*g)/p,(h*o+l*n*g)/p];return u([d,M])}const Ss=Object.prototype.toString;function Y(r){const t=Ss.call(r);return t.endsWith("Array]")&&!t.includes("Big")}function ks(r){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!Y(r))throw new TypeError("input must be an array");if(r.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,s=e===void 0?0:e,n=t.toIndex,o=n===void 0?r.length:n;if(s<0||s>=r.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=s||o>r.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var i=r[s],h=s+1;h<o;h++)r[h]>i&&(i=r[h]);return i}function Rs(r){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!Y(r))throw new TypeError("input must be an array");if(r.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,s=e===void 0?0:e,n=t.toIndex,o=n===void 0?r.length:n;if(s<0||s>=r.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=s||o>r.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var i=r[s],h=s+1;h<o;h++)r[h]<i&&(i=r[h]);return i}function ge(r){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(Y(r)){if(r.length===0)throw new TypeError("input must not be empty")}else throw new TypeError("input must be an array");var e;if(t.output!==void 0){if(!Y(t.output))throw new TypeError("output option must be an array if specified");e=t.output}else e=new Array(r.length);var s=Rs(r),n=ks(r);if(s===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var o=t.min,i=o===void 0?t.autoMinMax?s:0:o,h=t.max,l=h===void 0?t.autoMinMax?n:1:h;if(i>=l)throw new RangeError("min option must be smaller than max option");for(var a=(l-i)/(n-s),c=0;c<r.length;c++)e[c]=(r[c]-s)*a+i;return e}const It=" ".repeat(2),pe=" ".repeat(4);function Ts(){return we(this)}function we(r,t={}){const{maxRows:e=15,maxColumns:s=10,maxNumSize:n=8,padMinus:o="auto"}=t;return`${r.constructor.name} {
|
|
2
|
+
${It}[
|
|
3
|
+
${pe}${$s(r,e,s,n,o)}
|
|
4
|
+
${It}]
|
|
5
|
+
${It}rows: ${r.rows}
|
|
6
|
+
${It}columns: ${r.columns}
|
|
7
|
+
}`}function $s(r,t,e,s,n){const{rows:o,columns:i}=r,h=Math.min(o,t),l=Math.min(i,e),a=[];if(n==="auto"){n=!1;t:for(let c=0;c<h;c++)for(let u=0;u<l;u++)if(r.get(c,u)<0){n=!0;break t}}for(let c=0;c<h;c++){let u=[];for(let m=0;m<l;m++)u.push(_s(r.get(c,m),s,n));a.push(`${u.join(" ")}`)}return l!==i&&(a[a.length-1]+=` ... ${i-e} more columns`),h!==o&&a.push(`... ${o-t} more rows`),a.join(`
|
|
8
|
+
${pe}`)}function _s(r,t,e){return(r>=0&&e?` ${de(r,t-1)}`:de(r,t)).padEnd(t)}function de(r,t){let e=r.toString();if(e.length<=t)return e;let s=r.toFixed(t);if(s.length>t&&(s=r.toFixed(Math.max(0,t-(s.length-t)))),s.length<=t&&!s.startsWith("0.000")&&!s.startsWith("-0.000"))return s;let n=r.toExponential(t);return n.length>t&&(n=r.toExponential(Math.max(0,t-(n.length-t)))),n.slice(0)}function Fs(r,t){r.prototype.add=function(e){return typeof e=="number"?this.addS(e):this.addM(e)},r.prototype.addS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)+e);return this},r.prototype.addM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)+e.get(s,n));return this},r.add=function(e,s){return new t(e).add(s)},r.prototype.sub=function(e){return typeof e=="number"?this.subS(e):this.subM(e)},r.prototype.subS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)-e);return this},r.prototype.subM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)-e.get(s,n));return this},r.sub=function(e,s){return new t(e).sub(s)},r.prototype.subtract=r.prototype.sub,r.prototype.subtractS=r.prototype.subS,r.prototype.subtractM=r.prototype.subM,r.subtract=r.sub,r.prototype.mul=function(e){return typeof e=="number"?this.mulS(e):this.mulM(e)},r.prototype.mulS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)*e);return this},r.prototype.mulM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)*e.get(s,n));return this},r.mul=function(e,s){return new t(e).mul(s)},r.prototype.multiply=r.prototype.mul,r.prototype.multiplyS=r.prototype.mulS,r.prototype.multiplyM=r.prototype.mulM,r.multiply=r.mul,r.prototype.div=function(e){return typeof e=="number"?this.divS(e):this.divM(e)},r.prototype.divS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)/e);return this},r.prototype.divM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)/e.get(s,n));return this},r.div=function(e,s){return new t(e).div(s)},r.prototype.divide=r.prototype.div,r.prototype.divideS=r.prototype.divS,r.prototype.divideM=r.prototype.divM,r.divide=r.div,r.prototype.mod=function(e){return typeof e=="number"?this.modS(e):this.modM(e)},r.prototype.modS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)%e);return this},r.prototype.modM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)%e.get(s,n));return this},r.mod=function(e,s){return new t(e).mod(s)},r.prototype.modulus=r.prototype.mod,r.prototype.modulusS=r.prototype.modS,r.prototype.modulusM=r.prototype.modM,r.modulus=r.mod,r.prototype.and=function(e){return typeof e=="number"?this.andS(e):this.andM(e)},r.prototype.andS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)&e);return this},r.prototype.andM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)&e.get(s,n));return this},r.and=function(e,s){return new t(e).and(s)},r.prototype.or=function(e){return typeof e=="number"?this.orS(e):this.orM(e)},r.prototype.orS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)|e);return this},r.prototype.orM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)|e.get(s,n));return this},r.or=function(e,s){return new t(e).or(s)},r.prototype.xor=function(e){return typeof e=="number"?this.xorS(e):this.xorM(e)},r.prototype.xorS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)^e);return this},r.prototype.xorM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)^e.get(s,n));return this},r.xor=function(e,s){return new t(e).xor(s)},r.prototype.leftShift=function(e){return typeof e=="number"?this.leftShiftS(e):this.leftShiftM(e)},r.prototype.leftShiftS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)<<e);return this},r.prototype.leftShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)<<e.get(s,n));return this},r.leftShift=function(e,s){return new t(e).leftShift(s)},r.prototype.signPropagatingRightShift=function(e){return typeof e=="number"?this.signPropagatingRightShiftS(e):this.signPropagatingRightShiftM(e)},r.prototype.signPropagatingRightShiftS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>e);return this},r.prototype.signPropagatingRightShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>e.get(s,n));return this},r.signPropagatingRightShift=function(e,s){return new t(e).signPropagatingRightShift(s)},r.prototype.rightShift=function(e){return typeof e=="number"?this.rightShiftS(e):this.rightShiftM(e)},r.prototype.rightShiftS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>>e);return this},r.prototype.rightShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>>e.get(s,n));return this},r.rightShift=function(e,s){return new t(e).rightShift(s)},r.prototype.zeroFillRightShift=r.prototype.rightShift,r.prototype.zeroFillRightShiftS=r.prototype.rightShiftS,r.prototype.zeroFillRightShiftM=r.prototype.rightShiftM,r.zeroFillRightShift=r.rightShift,r.prototype.not=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,~this.get(e,s));return this},r.not=function(e){return new t(e).not()},r.prototype.abs=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.abs(this.get(e,s)));return this},r.abs=function(e){return new t(e).abs()},r.prototype.acos=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.acos(this.get(e,s)));return this},r.acos=function(e){return new t(e).acos()},r.prototype.acosh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.acosh(this.get(e,s)));return this},r.acosh=function(e){return new t(e).acosh()},r.prototype.asin=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.asin(this.get(e,s)));return this},r.asin=function(e){return new t(e).asin()},r.prototype.asinh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.asinh(this.get(e,s)));return this},r.asinh=function(e){return new t(e).asinh()},r.prototype.atan=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.atan(this.get(e,s)));return this},r.atan=function(e){return new t(e).atan()},r.prototype.atanh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.atanh(this.get(e,s)));return this},r.atanh=function(e){return new t(e).atanh()},r.prototype.cbrt=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.cbrt(this.get(e,s)));return this},r.cbrt=function(e){return new t(e).cbrt()},r.prototype.ceil=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.ceil(this.get(e,s)));return this},r.ceil=function(e){return new t(e).ceil()},r.prototype.clz32=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.clz32(this.get(e,s)));return this},r.clz32=function(e){return new t(e).clz32()},r.prototype.cos=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.cos(this.get(e,s)));return this},r.cos=function(e){return new t(e).cos()},r.prototype.cosh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.cosh(this.get(e,s)));return this},r.cosh=function(e){return new t(e).cosh()},r.prototype.exp=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.exp(this.get(e,s)));return this},r.exp=function(e){return new t(e).exp()},r.prototype.expm1=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.expm1(this.get(e,s)));return this},r.expm1=function(e){return new t(e).expm1()},r.prototype.floor=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.floor(this.get(e,s)));return this},r.floor=function(e){return new t(e).floor()},r.prototype.fround=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.fround(this.get(e,s)));return this},r.fround=function(e){return new t(e).fround()},r.prototype.log=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log(this.get(e,s)));return this},r.log=function(e){return new t(e).log()},r.prototype.log1p=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log1p(this.get(e,s)));return this},r.log1p=function(e){return new t(e).log1p()},r.prototype.log10=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log10(this.get(e,s)));return this},r.log10=function(e){return new t(e).log10()},r.prototype.log2=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log2(this.get(e,s)));return this},r.log2=function(e){return new t(e).log2()},r.prototype.round=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.round(this.get(e,s)));return this},r.round=function(e){return new t(e).round()},r.prototype.sign=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sign(this.get(e,s)));return this},r.sign=function(e){return new t(e).sign()},r.prototype.sin=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sin(this.get(e,s)));return this},r.sin=function(e){return new t(e).sin()},r.prototype.sinh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sinh(this.get(e,s)));return this},r.sinh=function(e){return new t(e).sinh()},r.prototype.sqrt=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sqrt(this.get(e,s)));return this},r.sqrt=function(e){return new t(e).sqrt()},r.prototype.tan=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.tan(this.get(e,s)));return this},r.tan=function(e){return new t(e).tan()},r.prototype.tanh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.tanh(this.get(e,s)));return this},r.tanh=function(e){return new t(e).tanh()},r.prototype.trunc=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.trunc(this.get(e,s)));return this},r.trunc=function(e){return new t(e).trunc()},r.pow=function(e,s){return new t(e).pow(s)},r.prototype.pow=function(e){return typeof e=="number"?this.powS(e):this.powM(e)},r.prototype.powS=function(e){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,Math.pow(this.get(s,n),e));return this},r.prototype.powM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.set(s,n,Math.pow(this.get(s,n),e.get(s,n)));return this}}function J(r,t,e){let s=e?r.rows:r.rows-1;if(t<0||t>s)throw new RangeError("Row index out of range")}function K(r,t,e){let s=e?r.columns:r.columns-1;if(t<0||t>s)throw new RangeError("Column index out of range")}function mt(r,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==r.columns)throw new RangeError("vector size must be the same as the number of columns");return t}function gt(r,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==r.rows)throw new RangeError("vector size must be the same as the number of rows");return t}function Is(r,t){if(!Y(t))throw new TypeError("row indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=r.rows)throw new RangeError("row indices are out of range")}function Ls(r,t){if(!Y(t))throw new TypeError("column indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=r.columns)throw new RangeError("column indices are out of range")}function ye(r,t,e,s,n){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(Ct("startRow",t),Ct("endRow",e),Ct("startColumn",s),Ct("endColumn",n),t>e||s>n||t<0||t>=r.rows||e<0||e>=r.rows||s<0||s>=r.columns||n<0||n>=r.columns)throw new RangeError("Submatrix indices are out of range")}function Lt(r,t=0){let e=[];for(let s=0;s<r;s++)e.push(t);return e}function Ct(r,t){if(typeof t!="number")throw new TypeError(`${r} must be a number`)}function pt(r){if(r.isEmpty())throw new Error("Empty matrix has no elements to index")}function Cs(r){let t=Lt(r.rows);for(let e=0;e<r.rows;++e)for(let s=0;s<r.columns;++s)t[e]+=r.get(e,s);return t}function Bs(r){let t=Lt(r.columns);for(let e=0;e<r.rows;++e)for(let s=0;s<r.columns;++s)t[s]+=r.get(e,s);return t}function js(r){let t=0;for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)t+=r.get(e,s);return t}function Vs(r){let t=Lt(r.rows,1);for(let e=0;e<r.rows;++e)for(let s=0;s<r.columns;++s)t[e]*=r.get(e,s);return t}function Ns(r){let t=Lt(r.columns,1);for(let e=0;e<r.rows;++e)for(let s=0;s<r.columns;++s)t[s]*=r.get(e,s);return t}function qs(r){let t=1;for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)t*=r.get(e,s);return t}function Os(r,t,e){const s=r.rows,n=r.columns,o=[];for(let i=0;i<s;i++){let h=0,l=0,a=0;for(let c=0;c<n;c++)a=r.get(i,c)-e[i],h+=a,l+=a*a;t?o.push((l-h*h/n)/(n-1)):o.push((l-h*h/n)/n)}return o}function zs(r,t,e){const s=r.rows,n=r.columns,o=[];for(let i=0;i<n;i++){let h=0,l=0,a=0;for(let c=0;c<s;c++)a=r.get(c,i)-e[i],h+=a,l+=a*a;t?o.push((l-h*h/s)/(s-1)):o.push((l-h*h/s)/s)}return o}function Us(r,t,e){const s=r.rows,n=r.columns,o=s*n;let i=0,h=0,l=0;for(let a=0;a<s;a++)for(let c=0;c<n;c++)l=r.get(a,c)-e,i+=l,h+=l*l;return t?(h-i*i/o)/(o-1):(h-i*i/o)/o}function Ys(r,t){for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)r.set(e,s,r.get(e,s)-t[e])}function Ds(r,t){for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)r.set(e,s,r.get(e,s)-t[s])}function Xs(r,t){for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)r.set(e,s,r.get(e,s)-t)}function Ws(r){const t=[];for(let e=0;e<r.rows;e++){let s=0;for(let n=0;n<r.columns;n++)s+=Math.pow(r.get(e,n),2)/(r.columns-1);t.push(Math.sqrt(s))}return t}function Gs(r,t){for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)r.set(e,s,r.get(e,s)/t[e])}function Hs(r){const t=[];for(let e=0;e<r.columns;e++){let s=0;for(let n=0;n<r.rows;n++)s+=Math.pow(r.get(n,e),2)/(r.rows-1);t.push(Math.sqrt(s))}return t}function Js(r,t){for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)r.set(e,s,r.get(e,s)/t[s])}function Ks(r){const t=r.size-1;let e=0;for(let s=0;s<r.columns;s++)for(let n=0;n<r.rows;n++)e+=Math.pow(r.get(n,s),2)/t;return Math.sqrt(e)}function Qs(r,t){for(let e=0;e<r.rows;e++)for(let s=0;s<r.columns;s++)r.set(e,s,r.get(e,s)/t)}let L=class C{static from1DArray(t,e,s){if(t*e!==s.length)throw new RangeError("data length does not match given dimensions");let n=new A(t,e);for(let o=0;o<t;o++)for(let i=0;i<e;i++)n.set(o,i,s[o*e+i]);return n}static rowVector(t){let e=new A(1,t.length);for(let s=0;s<t.length;s++)e.set(0,s,t[s]);return e}static columnVector(t){let e=new A(t.length,1);for(let s=0;s<t.length;s++)e.set(s,0,t[s]);return e}static zeros(t,e){return new A(t,e)}static ones(t,e){return new A(t,e).fill(1)}static rand(t,e,s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{random:n=Math.random}=s;let o=new A(t,e);for(let i=0;i<t;i++)for(let h=0;h<e;h++)o.set(i,h,n());return o}static randInt(t,e,s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{min:n=0,max:o=1e3,random:i=Math.random}=s;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(n>=o)throw new RangeError("min must be smaller than max");let h=o-n,l=new A(t,e);for(let a=0;a<t;a++)for(let c=0;c<e;c++){let u=n+Math.round(i()*h);l.set(a,c,u)}return l}static eye(t,e,s){e===void 0&&(e=t),s===void 0&&(s=1);let n=Math.min(t,e),o=this.zeros(t,e);for(let i=0;i<n;i++)o.set(i,i,s);return o}static diag(t,e,s){let n=t.length;e===void 0&&(e=n),s===void 0&&(s=e);let o=Math.min(n,e,s),i=this.zeros(e,s);for(let h=0;h<o;h++)i.set(h,h,t[h]);return i}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let s=t.rows,n=t.columns,o=new A(s,n);for(let i=0;i<s;i++)for(let h=0;h<n;h++)o.set(i,h,Math.min(t.get(i,h),e.get(i,h)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let s=t.rows,n=t.columns,o=new this(s,n);for(let i=0;i<s;i++)for(let h=0;h<n;h++)o.set(i,h,Math.max(t.get(i,h),e.get(i,h)));return o}static checkMatrix(t){return C.isMatrix(t)?t:new A(t)}static isMatrix(t){return t!=null&&t.klass==="Matrix"}get size(){return this.rows*this.columns}apply(t){if(typeof t!="function")throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.call(this,e,s);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.push(this.get(e,s));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let s=0;s<this.columns;s++)t[e].push(this.get(e,s))}return t}toJSON(){return this.to2DArray()}isRowVector(){return this.rows===1}isColumnVector(){return this.columns===1}isVector(){return this.rows===1||this.columns===1}isSquare(){return this.rows===this.columns}isEmpty(){return this.rows===0||this.columns===0}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isEchelonForm(){let t=0,e=0,s=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&o===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>s?(o=!0,s=e):(n=!1,o=!0);t++}return n}isReducedEchelonForm(){let t=0,e=0,s=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&o===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>s?(o=!0,s=e):(n=!1,o=!0);for(let i=e+1;i<this.rows;i++)this.get(t,i)!==0&&(n=!1);t++}return n}echelonForm(){let t=this.clone(),e=0,s=0;for(;e<t.rows&&s<t.columns;){let n=e;for(let o=e;o<t.rows;o++)t.get(o,s)>t.get(n,s)&&(n=o);if(t.get(n,s)===0)s++;else{t.swapRows(e,n);let o=t.get(e,s);for(let i=s;i<t.columns;i++)t.set(e,i,t.get(e,i)/o);for(let i=e+1;i<t.rows;i++){let h=t.get(i,s)/t.get(e,s);t.set(i,s,0);for(let l=s+1;l<t.columns;l++)t.set(i,l,t.get(i,l)-t.get(e,l)*h)}e++,s++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,s=t.rows,n=s-1;for(;n>=0;)if(t.maxRow(n)===0)n--;else{let o=0,i=!1;for(;o<s&&i===!1;)t.get(n,o)===1?i=!0:o++;for(let h=0;h<n;h++){let l=t.get(h,o);for(let a=o;a<e;a++){let c=t.get(h,a)-l*t.get(n,a);t.set(h,a,c)}}n--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{rows:e=1,columns:s=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(s)||s<=0)throw new TypeError("columns must be a positive integer");let n=new A(this.rows*e,this.columns*s);for(let o=0;o<e;o++)for(let i=0;i<s;i++)n.setSubMatrix(this,this.rows*o,this.columns*i);return n}fill(t){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,t);return this}neg(){return this.mulS(-1)}getRow(t){J(this,t);let e=[];for(let s=0;s<this.columns;s++)e.push(this.get(t,s));return e}getRowVector(t){return A.rowVector(this.getRow(t))}setRow(t,e){J(this,t),e=mt(this,e);for(let s=0;s<this.columns;s++)this.set(t,s,e[s]);return this}swapRows(t,e){J(this,t),J(this,e);for(let s=0;s<this.columns;s++){let n=this.get(t,s);this.set(t,s,this.get(e,s)),this.set(e,s,n)}return this}getColumn(t){K(this,t);let e=[];for(let s=0;s<this.rows;s++)e.push(this.get(s,t));return e}getColumnVector(t){return A.columnVector(this.getColumn(t))}setColumn(t,e){K(this,t),e=gt(this,e);for(let s=0;s<this.rows;s++)this.set(s,t,e[s]);return this}swapColumns(t,e){K(this,t),K(this,e);for(let s=0;s<this.rows;s++){let n=this.get(s,t);this.set(s,t,this.get(s,e)),this.set(s,e,n)}return this}addRowVector(t){t=mt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)+t[s]);return this}subRowVector(t){t=mt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)-t[s]);return this}mulRowVector(t){t=mt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)*t[s]);return this}divRowVector(t){t=mt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)/t[s]);return this}addColumnVector(t){t=gt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)+t[e]);return this}subColumnVector(t){t=gt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)-t[e]);return this}mulColumnVector(t){t=gt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)*t[e]);return this}divColumnVector(t){t=gt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)/t[e]);return this}mulRow(t,e){J(this,t);for(let s=0;s<this.columns;s++)this.set(t,s,this.get(t,s)*e);return this}mulColumn(t,e){K(this,t);for(let s=0;s<this.rows;s++)this.set(s,t,this.get(s,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>e[s]&&(e[s]=this.get(s,n));return e}case"column":{const e=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>e[n]&&(e[n]=this.get(s,n));return e}case void 0:{let e=this.get(0,0);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>e&&(e=this.get(s,n));return e}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){pt(this);let t=this.get(0,0),e=[0,0];for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>t&&(t=this.get(s,n),e[0]=s,e[1]=n);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<e[s]&&(e[s]=this.get(s,n));return e}case"column":{const e=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<e[n]&&(e[n]=this.get(s,n));return e}case void 0:{let e=this.get(0,0);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<e&&(e=this.get(s,n));return e}default:throw new Error(`invalid option: ${t}`)}}minIndex(){pt(this);let t=this.get(0,0),e=[0,0];for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<t&&(t=this.get(s,n),e[0]=s,e[1]=n);return e}maxRow(t){if(J(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let s=1;s<this.columns;s++)this.get(t,s)>e&&(e=this.get(t,s));return e}maxRowIndex(t){J(this,t),pt(this);let e=this.get(t,0),s=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n),s[1]=n);return s}minRow(t){if(J(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let s=1;s<this.columns;s++)this.get(t,s)<e&&(e=this.get(t,s));return e}minRowIndex(t){J(this,t),pt(this);let e=this.get(t,0),s=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n),s[1]=n);return s}maxColumn(t){if(K(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let s=1;s<this.rows;s++)this.get(s,t)>e&&(e=this.get(s,t));return e}maxColumnIndex(t){K(this,t),pt(this);let e=this.get(0,t),s=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t),s[0]=n);return s}minColumn(t){if(K(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let s=1;s<this.rows;s++)this.get(s,t)<e&&(e=this.get(s,t));return e}minColumnIndex(t){K(this,t),pt(this);let e=this.get(0,t),s=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t),s[0]=n);return s}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let s=0;s<t;s++)e.push(this.get(s,s));return e}norm(t="frobenius"){let e=0;if(t==="max")return this.max();if(t==="frobenius"){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)e=e+this.get(s,n)*this.get(s,n);return Math.sqrt(e)}else throw new RangeError(`unknown norm type: ${t}`)}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t+=this.get(e,s),this.set(e,s,t);return this}dot(t){C.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let s=0;for(let n=0;n<e.length;n++)s+=e[n]*t[n];return s}mmul(t){t=A.checkMatrix(t);let e=this.rows,s=this.columns,n=t.columns,o=new A(e,n),i=new Float64Array(s);for(let h=0;h<n;h++){for(let l=0;l<s;l++)i[l]=t.get(l,h);for(let l=0;l<e;l++){let a=0;for(let c=0;c<s;c++)a+=this.get(l,c)*i[c];o.set(l,h,a)}}return o}strassen2x2(t){t=A.checkMatrix(t);let e=new A(2,2);const s=this.get(0,0),n=t.get(0,0),o=this.get(0,1),i=t.get(0,1),h=this.get(1,0),l=t.get(1,0),a=this.get(1,1),c=t.get(1,1),u=(s+a)*(n+c),m=(h+a)*n,p=s*(i-c),g=a*(l-n),d=(s+o)*c,M=(h-s)*(n+i),f=(o-a)*(l+c),w=u+g-d+f,y=p+d,x=m+g,E=u-m+p+M;return e.set(0,0,w),e.set(0,1,y),e.set(1,0,x),e.set(1,1,E),e}strassen3x3(t){t=A.checkMatrix(t);let e=new A(3,3);const s=this.get(0,0),n=this.get(0,1),o=this.get(0,2),i=this.get(1,0),h=this.get(1,1),l=this.get(1,2),a=this.get(2,0),c=this.get(2,1),u=this.get(2,2),m=t.get(0,0),p=t.get(0,1),g=t.get(0,2),d=t.get(1,0),M=t.get(1,1),f=t.get(1,2),w=t.get(2,0),y=t.get(2,1),x=t.get(2,2),E=(s+n+o-i-h-c-u)*M,R=(s-i)*(-p+M),k=h*(-m+p+d-M-f-w+x),P=(-s+i+h)*(m-p+M),T=(i+h)*(-m+p),_=s*m,N=(-s+a+c)*(m-g+f),Q=(-s+a)*(g-f),tt=(a+c)*(-m+g),et=(s+n+o-h-l-a-c)*f,V=c*(-m+g+d-M-f-w+y),X=(-o+c+u)*(M+w-y),Ot=(o-u)*(M-y),st=o*w,yt=(c+u)*(-w+y),W=(-o+h+l)*(f+w-x),us=(o-l)*(f-x),cs=(h+l)*(-w+x),Wn=n*d,Gn=l*y,Hn=i*g,Jn=a*p,Kn=u*x,Qn=_+st+Wn,Zn=E+P+T+_+X+st+yt,tr=_+N+tt+et+st+W+cs,er=R+k+P+_+st+W+us,sr=R+P+T+_+Gn,nr=st+W+us+cs+Hn,rr=_+N+Q+V+X+Ot+st,or=X+Ot+st+yt+Jn,ir=_+N+Q+tt+Kn;return e.set(0,0,Qn),e.set(0,1,Zn),e.set(0,2,tr),e.set(1,0,er),e.set(1,1,sr),e.set(1,2,nr),e.set(2,0,rr),e.set(2,1,or),e.set(2,2,ir),e}mmulStrassen(t){t=A.checkMatrix(t);let e=this.clone(),s=e.rows,n=e.columns,o=t.rows,i=t.columns;n!==o&&console.warn(`Multiplying ${s} x ${n} and ${o} x ${i} matrix: dimensions do not match.`);function h(u,m,p){let g=u.rows,d=u.columns;if(g===m&&d===p)return u;{let M=C.zeros(m,p);return M=M.setSubMatrix(u,0,0),M}}let l=Math.max(s,o),a=Math.max(n,i);e=h(e,l,a),t=h(t,l,a);function c(u,m,p,g){if(p<=512||g<=512)return u.mmul(m);p%2===1&&g%2===1?(u=h(u,p+1,g+1),m=h(m,p+1,g+1)):p%2===1?(u=h(u,p+1,g),m=h(m,p+1,g)):g%2===1&&(u=h(u,p,g+1),m=h(m,p,g+1));let d=parseInt(u.rows/2,10),M=parseInt(u.columns/2,10),f=u.subMatrix(0,d-1,0,M-1),w=m.subMatrix(0,d-1,0,M-1),y=u.subMatrix(0,d-1,M,u.columns-1),x=m.subMatrix(0,d-1,M,m.columns-1),E=u.subMatrix(d,u.rows-1,0,M-1),R=m.subMatrix(d,m.rows-1,0,M-1),k=u.subMatrix(d,u.rows-1,M,u.columns-1),P=m.subMatrix(d,m.rows-1,M,m.columns-1),T=c(C.add(f,k),C.add(w,P),d,M),_=c(C.add(E,k),w,d,M),N=c(f,C.sub(x,P),d,M),Q=c(k,C.sub(R,w),d,M),tt=c(C.add(f,y),P,d,M),et=c(C.sub(E,f),C.add(w,x),d,M),V=c(C.sub(y,k),C.add(R,P),d,M),X=C.add(T,Q);X.sub(tt),X.add(V);let Ot=C.add(N,tt),st=C.add(_,Q),yt=C.sub(T,_);yt.add(N),yt.add(et);let W=C.zeros(2*X.rows,2*X.columns);return W=W.setSubMatrix(X,0,0),W=W.setSubMatrix(Ot,X.rows,0),W=W.setSubMatrix(st,0,X.columns),W=W.setSubMatrix(yt,X.rows,X.columns),W.subMatrix(0,p-1,0,g-1)}return c(e,t,l,a)}scaleRows(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:s=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(s))throw new TypeError("max must be a number");if(e>=s)throw new RangeError("min must be smaller than max");let n=new A(this.rows,this.columns);for(let o=0;o<this.rows;o++){const i=this.getRow(o);i.length>0&&ge(i,{min:e,max:s,output:i}),n.setRow(o,i)}return n}scaleColumns(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:s=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(s))throw new TypeError("max must be a number");if(e>=s)throw new RangeError("min must be smaller than max");let n=new A(this.rows,this.columns);for(let o=0;o<this.columns;o++){const i=this.getColumn(o);i.length&&ge(i,{min:e,max:s,output:i}),n.setColumn(o,i)}return n}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let s=0;s<t;s++){let n=this.get(e,s),o=this.get(e,this.columns-1-s);this.set(e,s,o),this.set(e,this.columns-1-s,n)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let s=0;s<t;s++){let n=this.get(s,e),o=this.get(this.rows-1-s,e);this.set(s,e,o),this.set(this.rows-1-s,e,n)}return this}kroneckerProduct(t){t=A.checkMatrix(t);let e=this.rows,s=this.columns,n=t.rows,o=t.columns,i=new A(e*n,s*o);for(let h=0;h<e;h++)for(let l=0;l<s;l++)for(let a=0;a<n;a++)for(let c=0;c<o;c++)i.set(n*h+a,o*l+c,this.get(h,l)*t.get(a,c));return i}kroneckerSum(t){if(t=A.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,s=t.rows,n=this.kroneckerProduct(A.eye(s,s)),o=A.eye(e,e).kroneckerProduct(t);return n.add(o)}transpose(){let t=new A(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.set(s,e,this.get(e,s));return t}sortRows(t=Me){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=Me){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,s,n){ye(this,t,e,s,n);let o=new A(e-t+1,n-s+1);for(let i=t;i<=e;i++)for(let h=s;h<=n;h++)o.set(i-t,h-s,this.get(i,h));return o}subMatrixRow(t,e,s){if(e===void 0&&(e=0),s===void 0&&(s=this.columns-1),e>s||e<0||e>=this.columns||s<0||s>=this.columns)throw new RangeError("Argument out of range");let n=new A(t.length,s-e+1);for(let o=0;o<t.length;o++)for(let i=e;i<=s;i++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);n.set(o,i-e,this.get(t[o],i))}return n}subMatrixColumn(t,e,s){if(e===void 0&&(e=0),s===void 0&&(s=this.rows-1),e>s||e<0||e>=this.rows||s<0||s>=this.rows)throw new RangeError("Argument out of range");let n=new A(s-e+1,t.length);for(let o=0;o<t.length;o++)for(let i=e;i<=s;i++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);n.set(i-e,o,this.get(i,t[o]))}return n}setSubMatrix(t,e,s){if(t=A.checkMatrix(t),t.isEmpty())return this;let n=e+t.rows-1,o=s+t.columns-1;ye(this,e,n,s,o);for(let i=0;i<t.rows;i++)for(let h=0;h<t.columns;h++)this.set(e+i,s+h,t.get(i,h));return this}selection(t,e){Is(this,t),Ls(this,e);let s=new A(t.length,e.length);for(let n=0;n<t.length;n++){let o=t[n];for(let i=0;i<e.length;i++){let h=e[i];s.set(n,i,this.get(o,h))}}return s}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let s=0;s<t;s++)e+=this.get(s,s);return e}clone(){let t=new A(this.rows,this.columns);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.set(e,s,this.get(e,s));return t}sum(t){switch(t){case"row":return Cs(this);case"column":return Bs(this);case void 0:return js(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return Vs(this);case"column":return Ns(this);case void 0:return qs(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":{for(let s=0;s<this.rows;s++)e[s]/=this.columns;return e}case"column":{for(let s=0;s<this.columns;s++)e[s]/=this.rows;return e}case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{unbiased:s=!0,mean:n=this.mean(t)}=e;if(typeof s!="boolean")throw new TypeError("unbiased must be a boolean");switch(t){case"row":{if(!Y(n))throw new TypeError("mean must be an array");return Os(this,s,n)}case"column":{if(!Y(n))throw new TypeError("mean must be an array");return zs(this,s,n)}case void 0:{if(typeof n!="number")throw new TypeError("mean must be a number");return Us(this,s,n)}default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){typeof t=="object"&&(e=t,t=void 0);const s=this.variance(t,e);if(t===void 0)return Math.sqrt(s);for(let n=0;n<s.length;n++)s[n]=Math.sqrt(s[n]);return s}center(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{center:s=this.mean(t)}=e;switch(t){case"row":{if(!Y(s))throw new TypeError("center must be an array");return Ys(this,s),this}case"column":{if(!Y(s))throw new TypeError("center must be an array");return Ds(this,s),this}case void 0:{if(typeof s!="number")throw new TypeError("center must be a number");return Xs(this,s),this}default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");let s=e.scale;switch(t){case"row":{if(s===void 0)s=Ws(this);else if(!Y(s))throw new TypeError("scale must be an array");return Gs(this,s),this}case"column":{if(s===void 0)s=Hs(this);else if(!Y(s))throw new TypeError("scale must be an array");return Js(this,s),this}case void 0:{if(s===void 0)s=Ks(this);else if(typeof s!="number")throw new TypeError("scale must be a number");return Qs(this,s),this}default:throw new Error(`invalid option: ${t}`)}}toString(t){return we(this,t)}};L.prototype.klass="Matrix",typeof Symbol<"u"&&(L.prototype[Symbol.for("nodejs.util.inspect.custom")]=Ts);function Me(r,t){return r-t}function Zs(r){return r.every(t=>typeof t=="number")}L.random=L.rand,L.randomInt=L.randInt,L.diagonal=L.diag,L.prototype.diagonal=L.prototype.diag,L.identity=L.eye,L.prototype.negate=L.prototype.neg,L.prototype.tensorProduct=L.prototype.kroneckerProduct;let A=class fs extends L{constructor(t,e){if(super(),fs.isMatrix(t))return t.clone();if(Number.isInteger(t)&&t>=0)if(this.data=[],Number.isInteger(e)&&e>=0)for(let s=0;s<t;s++)this.data.push(new Float64Array(e));else throw new TypeError("nColumns must be a positive integer");else if(Y(t)){const s=t;if(t=s.length,e=t?s[0].length:0,typeof e!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<t;n++){if(s[n].length!==e)throw new RangeError("Inconsistent array dimensions");if(!Zs(s[n]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(s[n]))}}else throw new TypeError("First argument must be a positive number or an array");this.rows=t,this.columns=e}set(t,e,s){return this.data[t][e]=s,this}get(t,e){return this.data[t][e]}removeRow(t){return J(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return e===void 0&&(e=t,t=this.rows),J(this,t,!0),e=Float64Array.from(mt(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){K(this,t);for(let e=0;e<this.rows;e++){const s=new Float64Array(this.columns-1);for(let n=0;n<t;n++)s[n]=this.data[e][n];for(let n=t+1;n<this.columns;n++)s[n-1]=this.data[e][n];this.data[e]=s}return this.columns-=1,this}addColumn(t,e){typeof e>"u"&&(e=t,t=this.columns),K(this,t,!0),e=gt(this,e);for(let s=0;s<this.rows;s++){const n=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)n[o]=this.data[s][o];for(n[o++]=e[s];o<this.columns+1;o++)n[o]=this.data[s][o-1];this.data[s]=n}return this.columns+=1,this}};Fs(L,A);class tn extends L{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,s){return this.data[t][e]=s,this}get(t,e){return this.data[t][e]}}function be(r,t){let e=0;return Math.abs(r)>Math.abs(t)?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):t!==0?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0}class en{constructor(t,e={}){const{assumeSymmetric:s=!1}=e;if(t=tn.checkMatrix(t),!t.isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let n=t.columns,o=new A(n,n),i=new Float64Array(n),h=new Float64Array(n),l=t,a,c,u=!1;if(s?u=!0:u=t.isSymmetric(),u){for(a=0;a<n;a++)for(c=0;c<n;c++)o.set(a,c,l.get(a,c));sn(n,h,i,o),nn(n,h,i,o)}else{let m=new A(n,n),p=new Float64Array(n);for(c=0;c<n;c++)for(a=0;a<n;a++)m.set(a,c,l.get(a,c));rn(n,m,p,o),on(n,h,i,o,m)}this.n=n,this.e=h,this.d=i,this.V=o}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t=this.n,e=this.e,s=this.d,n=new A(t,t),o,i;for(o=0;o<t;o++){for(i=0;i<t;i++)n.set(o,i,0);n.set(o,o,s[o]),e[o]>0?n.set(o,o+1,e[o]):e[o]<0&&n.set(o,o-1,e[o])}return n}}function sn(r,t,e,s){let n,o,i,h,l,a,c,u;for(l=0;l<r;l++)e[l]=s.get(r-1,l);for(h=r-1;h>0;h--){for(u=0,i=0,a=0;a<h;a++)u=u+Math.abs(e[a]);if(u===0)for(t[h]=e[h-1],l=0;l<h;l++)e[l]=s.get(h-1,l),s.set(h,l,0),s.set(l,h,0);else{for(a=0;a<h;a++)e[a]/=u,i+=e[a]*e[a];for(n=e[h-1],o=Math.sqrt(i),n>0&&(o=-o),t[h]=u*o,i=i-n*o,e[h-1]=n-o,l=0;l<h;l++)t[l]=0;for(l=0;l<h;l++){for(n=e[l],s.set(l,h,n),o=t[l]+s.get(l,l)*n,a=l+1;a<=h-1;a++)o+=s.get(a,l)*e[a],t[a]+=s.get(a,l)*n;t[l]=o}for(n=0,l=0;l<h;l++)t[l]/=i,n+=t[l]*e[l];for(c=n/(i+i),l=0;l<h;l++)t[l]-=c*e[l];for(l=0;l<h;l++){for(n=e[l],o=t[l],a=l;a<=h-1;a++)s.set(a,l,s.get(a,l)-(n*t[a]+o*e[a]));e[l]=s.get(h-1,l),s.set(h,l,0)}}e[h]=i}for(h=0;h<r-1;h++){if(s.set(r-1,h,s.get(h,h)),s.set(h,h,1),i=e[h+1],i!==0){for(a=0;a<=h;a++)e[a]=s.get(a,h+1)/i;for(l=0;l<=h;l++){for(o=0,a=0;a<=h;a++)o+=s.get(a,h+1)*s.get(a,l);for(a=0;a<=h;a++)s.set(a,l,s.get(a,l)-o*e[a])}}for(a=0;a<=h;a++)s.set(a,h+1,0)}for(l=0;l<r;l++)e[l]=s.get(r-1,l),s.set(r-1,l,0);s.set(r-1,r-1,1),t[0]=0}function nn(r,t,e,s){let n,o,i,h,l,a,c,u,m,p,g,d,M,f,w,y;for(i=1;i<r;i++)t[i-1]=t[i];t[r-1]=0;let x=0,E=0,R=Number.EPSILON;for(a=0;a<r;a++){for(E=Math.max(E,Math.abs(e[a])+Math.abs(t[a])),c=a;c<r&&!(Math.abs(t[c])<=R*E);)c++;if(c>a)do{for(n=e[a],u=(e[a+1]-n)/(2*t[a]),m=be(u,1),u<0&&(m=-m),e[a]=t[a]/(u+m),e[a+1]=t[a]*(u+m),p=e[a+1],o=n-e[a],i=a+2;i<r;i++)e[i]-=o;for(x=x+o,u=e[c],g=1,d=g,M=g,f=t[a+1],w=0,y=0,i=c-1;i>=a;i--)for(M=d,d=g,y=w,n=g*t[i],o=g*u,m=be(u,t[i]),t[i+1]=w*m,w=t[i]/m,g=u/m,u=g*e[i]-w*n,e[i+1]=o+w*(g*n+w*e[i]),l=0;l<r;l++)o=s.get(l,i+1),s.set(l,i+1,w*s.get(l,i)+g*o),s.set(l,i,g*s.get(l,i)-w*o);u=-w*y*M*f*t[a]/p,t[a]=w*u,e[a]=g*u}while(Math.abs(t[a])>R*E);e[a]=e[a]+x,t[a]=0}for(i=0;i<r-1;i++){for(l=i,u=e[i],h=i+1;h<r;h++)e[h]<u&&(l=h,u=e[h]);if(l!==i)for(e[l]=e[i],e[i]=u,h=0;h<r;h++)u=s.get(h,i),s.set(h,i,s.get(h,l)),s.set(h,l,u)}}function rn(r,t,e,s){let n=0,o=r-1,i,h,l,a,c,u,m;for(u=n+1;u<=o-1;u++){for(m=0,a=u;a<=o;a++)m=m+Math.abs(t.get(a,u-1));if(m!==0){for(l=0,a=o;a>=u;a--)e[a]=t.get(a,u-1)/m,l+=e[a]*e[a];for(h=Math.sqrt(l),e[u]>0&&(h=-h),l=l-e[u]*h,e[u]=e[u]-h,c=u;c<r;c++){for(i=0,a=o;a>=u;a--)i+=e[a]*t.get(a,c);for(i=i/l,a=u;a<=o;a++)t.set(a,c,t.get(a,c)-i*e[a])}for(a=0;a<=o;a++){for(i=0,c=o;c>=u;c--)i+=e[c]*t.get(a,c);for(i=i/l,c=u;c<=o;c++)t.set(a,c,t.get(a,c)-i*e[c])}e[u]=m*e[u],t.set(u,u-1,m*h)}}for(a=0;a<r;a++)for(c=0;c<r;c++)s.set(a,c,a===c?1:0);for(u=o-1;u>=n+1;u--)if(t.get(u,u-1)!==0){for(a=u+1;a<=o;a++)e[a]=t.get(a,u-1);for(c=u;c<=o;c++){for(h=0,a=u;a<=o;a++)h+=e[a]*s.get(a,c);for(h=h/e[u]/t.get(u,u-1),a=u;a<=o;a++)s.set(a,c,s.get(a,c)+h*e[a])}}}function on(r,t,e,s,n){let o=r-1,i=0,h=r-1,l=Number.EPSILON,a=0,c=0,u=0,m=0,p=0,g=0,d=0,M=0,f,w,y,x,E,R,k,P,T,_,N,Q,tt,et,V;for(f=0;f<r;f++)for((f<i||f>h)&&(e[f]=n.get(f,f),t[f]=0),w=Math.max(f-1,0);w<r;w++)c=c+Math.abs(n.get(f,w));for(;o>=i;){for(x=o;x>i&&(g=Math.abs(n.get(x-1,x-1))+Math.abs(n.get(x,x)),g===0&&(g=c),!(Math.abs(n.get(x,x-1))<l*g));)x--;if(x===o)n.set(o,o,n.get(o,o)+a),e[o]=n.get(o,o),t[o]=0,o--,M=0;else if(x===o-1){if(k=n.get(o,o-1)*n.get(o-1,o),u=(n.get(o-1,o-1)-n.get(o,o))/2,m=u*u+k,d=Math.sqrt(Math.abs(m)),n.set(o,o,n.get(o,o)+a),n.set(o-1,o-1,n.get(o-1,o-1)+a),P=n.get(o,o),m>=0){for(d=u>=0?u+d:u-d,e[o-1]=P+d,e[o]=e[o-1],d!==0&&(e[o]=P-k/d),t[o-1]=0,t[o]=0,P=n.get(o,o-1),g=Math.abs(P)+Math.abs(d),u=P/g,m=d/g,p=Math.sqrt(u*u+m*m),u=u/p,m=m/p,w=o-1;w<r;w++)d=n.get(o-1,w),n.set(o-1,w,m*d+u*n.get(o,w)),n.set(o,w,m*n.get(o,w)-u*d);for(f=0;f<=o;f++)d=n.get(f,o-1),n.set(f,o-1,m*d+u*n.get(f,o)),n.set(f,o,m*n.get(f,o)-u*d);for(f=i;f<=h;f++)d=s.get(f,o-1),s.set(f,o-1,m*d+u*s.get(f,o)),s.set(f,o,m*s.get(f,o)-u*d)}else e[o-1]=P+u,e[o]=P+u,t[o-1]=d,t[o]=-d;o=o-2,M=0}else{if(P=n.get(o,o),T=0,k=0,x<o&&(T=n.get(o-1,o-1),k=n.get(o,o-1)*n.get(o-1,o)),M===10){for(a+=P,f=i;f<=o;f++)n.set(f,f,n.get(f,f)-P);g=Math.abs(n.get(o,o-1))+Math.abs(n.get(o-1,o-2)),P=T=.75*g,k=-.4375*g*g}if(M===30&&(g=(T-P)/2,g=g*g+k,g>0)){for(g=Math.sqrt(g),T<P&&(g=-g),g=P-k/((T-P)/2+g),f=i;f<=o;f++)n.set(f,f,n.get(f,f)-g);a+=g,P=T=k=.964}for(M=M+1,E=o-2;E>=x&&(d=n.get(E,E),p=P-d,g=T-d,u=(p*g-k)/n.get(E+1,E)+n.get(E,E+1),m=n.get(E+1,E+1)-d-p-g,p=n.get(E+2,E+1),g=Math.abs(u)+Math.abs(m)+Math.abs(p),u=u/g,m=m/g,p=p/g,!(E===x||Math.abs(n.get(E,E-1))*(Math.abs(m)+Math.abs(p))<l*(Math.abs(u)*(Math.abs(n.get(E-1,E-1))+Math.abs(d)+Math.abs(n.get(E+1,E+1))))));)E--;for(f=E+2;f<=o;f++)n.set(f,f-2,0),f>E+2&&n.set(f,f-3,0);for(y=E;y<=o-1&&(et=y!==o-1,y!==E&&(u=n.get(y,y-1),m=n.get(y+1,y-1),p=et?n.get(y+2,y-1):0,P=Math.abs(u)+Math.abs(m)+Math.abs(p),P!==0&&(u=u/P,m=m/P,p=p/P)),P!==0);y++)if(g=Math.sqrt(u*u+m*m+p*p),u<0&&(g=-g),g!==0){for(y!==E?n.set(y,y-1,-g*P):x!==E&&n.set(y,y-1,-n.get(y,y-1)),u=u+g,P=u/g,T=m/g,d=p/g,m=m/u,p=p/u,w=y;w<r;w++)u=n.get(y,w)+m*n.get(y+1,w),et&&(u=u+p*n.get(y+2,w),n.set(y+2,w,n.get(y+2,w)-u*d)),n.set(y,w,n.get(y,w)-u*P),n.set(y+1,w,n.get(y+1,w)-u*T);for(f=0;f<=Math.min(o,y+3);f++)u=P*n.get(f,y)+T*n.get(f,y+1),et&&(u=u+d*n.get(f,y+2),n.set(f,y+2,n.get(f,y+2)-u*p)),n.set(f,y,n.get(f,y)-u),n.set(f,y+1,n.get(f,y+1)-u*m);for(f=i;f<=h;f++)u=P*s.get(f,y)+T*s.get(f,y+1),et&&(u=u+d*s.get(f,y+2),s.set(f,y+2,s.get(f,y+2)-u*p)),s.set(f,y,s.get(f,y)-u),s.set(f,y+1,s.get(f,y+1)-u*m)}}}if(c!==0){for(o=r-1;o>=0;o--)if(u=e[o],m=t[o],m===0)for(x=o,n.set(o,o,1),f=o-1;f>=0;f--){for(k=n.get(f,f)-u,p=0,w=x;w<=o;w++)p=p+n.get(f,w)*n.get(w,o);if(t[f]<0)d=k,g=p;else if(x=f,t[f]===0?n.set(f,o,k!==0?-p/k:-p/(l*c)):(P=n.get(f,f+1),T=n.get(f+1,f),m=(e[f]-u)*(e[f]-u)+t[f]*t[f],R=(P*g-d*p)/m,n.set(f,o,R),n.set(f+1,o,Math.abs(P)>Math.abs(d)?(-p-k*R)/P:(-g-T*R)/d)),R=Math.abs(n.get(f,o)),l*R*R>1)for(w=f;w<=o;w++)n.set(w,o,n.get(w,o)/R)}else if(m<0)for(x=o-1,Math.abs(n.get(o,o-1))>Math.abs(n.get(o-1,o))?(n.set(o-1,o-1,m/n.get(o,o-1)),n.set(o-1,o,-(n.get(o,o)-u)/n.get(o,o-1))):(V=Bt(0,-n.get(o-1,o),n.get(o-1,o-1)-u,m),n.set(o-1,o-1,V[0]),n.set(o-1,o,V[1])),n.set(o,o-1,0),n.set(o,o,1),f=o-2;f>=0;f--){for(_=0,N=0,w=x;w<=o;w++)_=_+n.get(f,w)*n.get(w,o-1),N=N+n.get(f,w)*n.get(w,o);if(k=n.get(f,f)-u,t[f]<0)d=k,p=_,g=N;else if(x=f,t[f]===0?(V=Bt(-_,-N,k,m),n.set(f,o-1,V[0]),n.set(f,o,V[1])):(P=n.get(f,f+1),T=n.get(f+1,f),Q=(e[f]-u)*(e[f]-u)+t[f]*t[f]-m*m,tt=(e[f]-u)*2*m,Q===0&&tt===0&&(Q=l*c*(Math.abs(k)+Math.abs(m)+Math.abs(P)+Math.abs(T)+Math.abs(d))),V=Bt(P*p-d*_+m*N,P*g-d*N-m*_,Q,tt),n.set(f,o-1,V[0]),n.set(f,o,V[1]),Math.abs(P)>Math.abs(d)+Math.abs(m)?(n.set(f+1,o-1,(-_-k*n.get(f,o-1)+m*n.get(f,o))/P),n.set(f+1,o,(-N-k*n.get(f,o)-m*n.get(f,o-1))/P)):(V=Bt(-p-T*n.get(f,o-1),-g-T*n.get(f,o),d,m),n.set(f+1,o-1,V[0]),n.set(f+1,o,V[1]))),R=Math.max(Math.abs(n.get(f,o-1)),Math.abs(n.get(f,o))),l*R*R>1)for(w=f;w<=o;w++)n.set(w,o-1,n.get(w,o-1)/R),n.set(w,o,n.get(w,o)/R)}for(f=0;f<r;f++)if(f<i||f>h)for(w=f;w<r;w++)s.set(f,w,n.get(f,w));for(w=r-1;w>=i;w--)for(f=i;f<=h;f++){for(d=0,y=i;y<=Math.min(w,h);y++)d=d+s.get(f,y)*n.get(y,w);s.set(f,w,d)}}}function Bt(r,t,e,s){let n,o;return Math.abs(e)>Math.abs(s)?(n=s/e,o=e+n*s,[(r+n*t)/o,(t-n*r)/o]):(n=e/s,o=s+n*e,[(n*r+t)/o,(n*t-r)/o])}function Pe(r,t=1e-9){if(r[r.length-1]==0)return Pe(r.slice(0,-1),t);const e=new A([r.slice(0,-1).reverse().map(n=>-n/r[r.length-1]),...Array.from({length:r.length-2},(n,o)=>{const i=new Array(r.length-1).fill(0);return i[o]=1,i})]),s=new en(e);return s.realEigenvalues.filter((n,o)=>Math.abs(s.imaginaryEigenvalues[o])<t)}function xe(r,t){const e=Math.max(r.precision,t.precision),s=r.coefficients,n=s.x2,o=s.xy,i=s.y2,h=s.x,l=s.y,a=s.c,c=t.coefficients,u=c.x2,m=c.xy,p=c.y2,g=c.x,d=c.y,M=c.c,f={z0:a*n*g*g+n*n*M*M-h*n*g*M+u*u*a*a-2*n*M*u*a-h*g*u*a+u*h*h*M,z1:d*h*h*u-M*g*n*o-2*n*M*u*l-a*u*m*h+2*g*m*n*a+2*d*M*n*n+g*g*n*l-d*g*n*h-2*n*d*u*a-a*u*g*o+2*a*l*u*u-M*m*n*h-l*u*g*h+2*M*o*u*h,z2:d*d*n*n+2*p*M*n*n-l*u*g*o+M*u*o*o-l*u*m*h-M*m*n*o-2*n*d*u*l+2*g*m*n*l-p*g*n*h-2*n*p*u*a+m*m*n*a+2*d*o*u*h+l*l*u*u-i*u*g*h-d*m*n*h+2*a*i*u*u-a*u*m*o+p*h*h*u+g*g*n*i-d*g*n*o-2*n*M*u*i,z3:-2*n*u*i*d+d*u*o*o+2*p*o*u*h-i*u*m*h+m*m*n*l-d*m*n*o-2*n*p*u*l-l*u*m*o-p*m*n*h+2*d*p*n*n+2*l*i*u*u-i*u*g*o+2*g*m*n*i-p*g*n*o,z4:n*n*p*p-2*n*p*u*i+u*u*i*i-o*n*m*p-o*m*u*i+o*o*u*p+i*n*m*m},w=Pe([f.z0,f.z1,f.z2,f.z3,f.z4],e).flatMap(y=>{const x=n*m*y+n*g-u*o*y-u*h;if(x)return[[-(n*M+n*p*y*y-u*i*y*y+n*d*y-u*l*y-u*a)/x,y]];const E=o*y+h,R=-E/(2*n),k=i*y*y+l*y+a,P=E*E/(4*n*n)-k/n;if(Math.abs(P)<e)return[[R,y]];if(P>0){const T=Math.sqrt(P);return[[R+T,y],[R-T,y]]}return[]});return it(w,e)}function Ee(r,t){return xe(r,t).filter(e=>r.isOnSegment(e)&&t.isOnSegment(e))}const hn=r=>{const{firstPoint:t,lastPoint:e,center:s,majorRadius:n,minorRadius:o,tiltAngle:i,clockwise:h}=r;return new U(e,t,s,n,o,i,h,{ignoreChecks:!0,angleUnits:"rad"})},an=(r,t)=>{if(r.isSame(t))return[r];const e=(n,o)=>new U(n,o,r.center,r.majorRadius,r.minorRadius,r.tiltAngle,r.clockwise,{ignoreChecks:!0,angleUnits:"rad"}),s=it([t.isOnSegment(r.firstPoint)?r.firstPoint:null,t.isOnSegment(r.lastPoint)?r.lastPoint:null,r.isOnSegment(t.firstPoint)?t.firstPoint:null,r.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(n=>n!==null)).sort((n,o)=>r.pointToParam(n)-r.pointToParam(o));if(s.length===0)return[];if(s.length===1)return[];if(s.length===2)return r.isSame(hn(t))?[]:[e(s[0],s[1])];if(s.length===3){const n=b(s[0],t.lastPoint)||b(s[0],t.firstPoint)?1:0;return[e(s[0+n],s[1+n])]}else if(s.length===4)return[e(s[0],s[1]),e(s[2],s[3])];throw new Error("Bug in the ellipse arc ellipse arc overlap algorithm")};function ln(r,t,e=!1){const s=Math.max(r.precision,t.precision);return b(r.center,t.center)&&Math.abs(r.majorRadius-t.majorRadius)<s&&Math.abs(r.minorRadius-t.minorRadius)<s&&(Math.abs(r.tiltAngle-t.tiltAngle)<s||Math.abs(Math.abs(r.tiltAngle-t.tiltAngle)-Math.PI)<s)?e?an(r,t):[]:xe(r,t).filter(n=>r.isOnSegment(n)&&t.isOnSegment(n))}function un(r,t,e){if(r instanceof v&&t instanceof v){const s=ce(r,t,!1,e);return s===null?[]:[s]}if(r instanceof v&&t instanceof S)return Ft(r,t,e);if(r instanceof S&&t instanceof v)return Ft(t,r,e);if(r instanceof S&&t instanceof S)return me(r,t,!1,e);throw new Error("Not implemented")}function Et(r,t,e){if(r instanceof v&&t instanceof v){const s=ce(r,t,!0,e);return s===null?{intersections:[],overlaps:[],count:0}:s instanceof v?{intersections:[],overlaps:[s],count:1}:{intersections:[s],overlaps:[],count:1}}if(!r.boundingBox.overlaps(t.boundingBox))return{intersections:[],overlaps:[],count:0};if(r instanceof v&&t instanceof S){const s=Ft(r,t,e);return{intersections:s,overlaps:[],count:s.length}}if(r instanceof S&&t instanceof v){const s=Ft(t,r,e);return{intersections:s,overlaps:[],count:s.length}}if(r instanceof S&&t instanceof S){const s=me(r,t,!0,e);return s.length?s[0]instanceof S?{intersections:[],overlaps:s,count:s.length}:{intersections:s,overlaps:[],count:s.length}:{intersections:[],overlaps:[],count:0}}if(r instanceof v&&t instanceof U){const s=Wt(r,t,e);return{intersections:s,overlaps:[],count:s.length}}if(t instanceof v&&r instanceof U){const s=Wt(t,r,e);return{intersections:s,overlaps:[],count:s.length}}if(r instanceof S&&t instanceof U){const s=Ee(r,t);return{intersections:s,overlaps:[],count:s.length}}if(t instanceof S&&r instanceof U){const s=Ee(t,r);return{intersections:s,overlaps:[],count:s.length}}if(r instanceof U&&t instanceof U){const s=ln(r,t,!0);return s.length?s[0]instanceof U?{intersections:[],overlaps:s,count:s.length}:{intersections:s,overlaps:[],count:s.length}:{intersections:[],overlaps:[],count:0}}throw new Error("Not implemented")}function ve(r){const t=[];for(let e=0;e<r;e++)for(let s=0;s<=e;s++)t.push([e,s]);return t}function*Gt(r){for(const[t,e]of ve(r.length))t!==e&&(yield[r[t],r[e]])}class Ae extends lt{constructor(t,{ignoreChecks:e=!1}={}){super(),this._boundingBox=null,e||Se(t),this.segments=t}get repr(){return this.segments.map(t=>t.repr).join(`
|
|
9
|
+
`)+`
|
|
10
|
+
`}get info(){return this.repr}get firstPoint(){return this.segments[0].firstPoint}get lastPoint(){return this.segments[this.segments.length-1].lastPoint}get segmentsCount(){return this.segments.length}onStroke(t){return this.segments.some(e=>e.isOnSegment(t))}intersects(t){return this.boundingBox.overlaps(t.boundingBox)?this.segments.some(e=>t.segments.some(s=>Et(e,s).count>0)):!1}overlappingSegments(t){return this.segments.flatMap(e=>t.segments.flatMap(s=>e.boundingBox.overlaps(s.boundingBox)?Et(e,s).overlaps:[]))}get boundingBox(){if(this._boundingBox===null){let t=this.segments[0].boundingBox;this.segments.slice(1).forEach(e=>{t=t.merge(e.boundingBox)}),this._boundingBox=t}return this._boundingBox}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}function cn(r,t="Stroke"){ve(r.length).forEach(([e,s])=>{if(e===s)return;const n=r[e],o=r[s],i=Et(n,o),h=Math.max(n.precision,o.precision);if(i.count!==0){if(i.count===1&&!i.overlaps.length){const l=e-s,a=i.intersections[0];if(l===1&&b(n.firstPoint,a,h)||l===-1&&b(n.lastPoint,a,h)||l===r.length-1&&b(n.lastPoint,a,h)&&b(o.firstPoint,a,h)||-l===r.length-1&&b(n.firstPoint,a,h)&&b(o.lastPoint,a,h))return}if(!(i.count===2&&r.length===2&&(b(n.firstPoint,i.intersections[0],h)&&b(n.lastPoint,i.intersections[1],h)||b(n.firstPoint,i.intersections[1],h)&&b(n.lastPoint,i.intersections[0],h))))throw new Error(`${t} segments must not intersect, but segments ${n.info} and ${o.info} do at ${JSON.stringify(i.intersections)}`)}})}function Se(r,t="Stroke"){if(r.length===0)throw new Error(`${t} must have at least one segment`);Z([r.slice(0,-1),r.slice(1)]).forEach(([e,s])=>{if(!b(e.lastPoint,s.firstPoint))throw new Error(`${t} segments must be connected, but ${e.info} and ${s.info} are not`)}),cn(r,t)}function ke(r,t){return!!(r instanceof v&&t instanceof v&&Dt(r.V,t.V)||r instanceof S&&t instanceof S&&b(r.center,t.center)&&r.radius-t.radius<r.precision)}function Re(r,t){if(r instanceof v&&t instanceof v)return new v(r.firstPoint,t.lastPoint);if(r instanceof S&&t instanceof S)return new S(r.firstPoint,t.lastPoint,r.center,r.clockwise);throw new Error("Not implemented")}function Te(r){let t=!1;const e=[];for(const s of r.segments){if(e.length===0){e.push(s);continue}const n=e[e.length-1];ke(n,s)?(t=!0,e.pop(),e.push(Re(n,s))):e.push(s)}if(b(r.firstPoint,r.lastPoint)&&ke(e[0],e[e.length-1])){t=!0;const s=e.pop();e[0]=Re(s,e[0])}return t?e:null}let rt=class Mt extends Ae{constructor(){super(...arguments),this.strokeType="STRAND"}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new Mt(t,{ignoreChecks:!0})}clone(){return new Mt(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}extend(t){if(!b(this.lastPoint,t.firstPoint))throw console.error(this.repr,t.repr),new Error("Cannot extend strand: connection point is not the same");return new Mt([...this.segments,...t.segments])}simplify(){const t=Te(this);return t?new Mt(t,{ignoreChecks:!0}):this}transform(t){return new Mt(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}};const fn=(r,t)=>{const e=$t(t,{V:[1,0],firstPoint:r,precision:t.precision});if(e==="parallel")return 0;const{intersectionParam1:s,intersectionParam2:n}=e;if(!t.isValidParameter(s)||n<=-t.precision)return 0;if(Math.abs(s)<t.precision||Math.abs(s-1)<t.precision){const[,o]=t.midPoint;return r[1]-o<0?1:0}return 1};class $e{constructor(t){this._count=0,this.segment=t}update(t,e=!1){!e&&!this.segment.isOnSegment(t)||(b(t,this.segment.firstPoint)?this._count+=this.segment.tangentAtFirstPoint[1]>0?1:0:b(t,this.segment.lastPoint)?this._count+=this.segment.tangentAtLastPoint[1]>0?0:1:this._count+=1)}get count(){return this._count}}const mn=(r,t)=>{const e=t.precision,s=Math.abs(r[1]-t.center[1]);if(s>t.radius+e)return 0;const n=Pt(r,t.center),o=t.radius*t.radius,i=e*e;if(Math.abs(n-o)<i&&t.isOnSegment(r))return 0;const h=n-o>i;if(h&&t.center[0]<r[0])return 0;const l=Math.sqrt(t.radius*t.radius-s*s),a=new $e(t);return a.update([t.center[0]+l,r[1]]),h&&a.update([t.center[0]-l,r[1]]),a.count},gn=(r,t)=>{const e=t.boundingBox.xMax+t.boundingBox.width/2,s=new v(r,[e,r[1]]),n=new $e(t);return Wt(s,t).forEach(o=>{n.update(o,!0)}),n.count};function pn(r,t){if(t instanceof v)return fn(r,t);if(t instanceof S)return mn(r,t);if(t instanceof U)return gn(r,t);throw new Error("Not implemented")}class G extends Ae{constructor(t,{ignoreChecks:e=!1}={}){super(t,{ignoreChecks:!0}),this.strokeType="LOOP",this._clockwise=null,e||wn(t)}get clockwise(){if(this._clockwise===null){const t=this.segments.flatMap(s=>s instanceof v?[s.firstPoint]:[s.firstPoint,s.paramPoint(.5)]),e=t.map((s,n)=>{const o=t[(n+1)%t.length];return(o[0]-s[0])*(o[1]+s[1])}).reduce((s,n)=>s+n,0);this._clockwise=e>0}return this._clockwise}clone(){return new G(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new G(t,{ignoreChecks:!0})}transform(t){return new G(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}contains(t){return this.onStroke(t)||!this.boundingBox.contains(t)?!1:this.segments.reduce((e,s)=>e+pn(t,s),0)%2===1}simplify(){const t=Te(this);return t?new G(t,{ignoreChecks:!0}):this}}function wn(r){if(Se(r,"Loop"),!b(r[0].firstPoint,r[r.length-1].lastPoint))throw new Error("Loop segment must be closed")}function _e(r){if(r instanceof v)return{type:r.segmentType,firstPoint:r.firstPoint,lastPoint:r.lastPoint};if(r instanceof S)return{type:r.segmentType,firstPoint:r.firstPoint,lastPoint:r.lastPoint,center:r.center,clockwise:r.clockwise};if(r instanceof U)return{type:r.segmentType,firstPoint:r.firstPoint,lastPoint:r.lastPoint,center:r.center,clockwise:r.clockwise,majorRadius:r.majorRadius,minorRadius:r.minorRadius,tiltAngle:r.tiltAngle};throw new Error("Unknown segment type")}function Ht(r){return{type:"LOOP",segments:r.segments.map(_e)}}function Fe(r){return{type:"FIGURE",contour:Ht(r.contour),holes:r.holes.map(Ht)}}function dn(r){return{type:"DIAGRAM",figures:r.figures.map(Fe)}}function Ie(r){if(r instanceof St)return dn(r);if(r instanceof D)return Fe(r);if(r instanceof G)return Ht(r);if(r instanceof S||r instanceof v)return _e(r);throw new Error("Unknown shape type")}class yn{constructor(){this.ids=[],this.values=[],this.length=0}clear(){this.length=0}push(t,e){let s=this.length++;for(;s>0;){const n=s-1>>1,o=this.values[n];if(e>=o)break;this.ids[s]=this.ids[n],this.values[s]=o,s=n}this.ids[s]=t,this.values[s]=e}pop(){if(this.length===0)return;const t=this.ids[0];if(this.length--,this.length>0){const e=this.ids[0]=this.ids[this.length],s=this.values[0]=this.values[this.length],n=this.length>>1;let o=0;for(;o<n;){let i=(o<<1)+1;const h=i+1;let l=this.ids[i],a=this.values[i];const c=this.values[h];if(h<this.length&&c<a&&(i=h,l=this.ids[h],a=c),a>=s)break;this.ids[o]=l,this.values[o]=a,o=i}this.ids[o]=e,this.values[o]=s}return t}peek(){if(this.length!==0)return this.ids[0]}peekValue(){if(this.length!==0)return this.values[0]}shrink(){this.ids.length=this.values.length=this.length}}const Le=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],Jt=3;class Kt{static from(t){if(!t||t.byteLength===void 0||t.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");const[e,s]=new Uint8Array(t,0,2);if(e!==251)throw new Error("Data does not appear to be in a Flatbush format.");if(s>>4!==Jt)throw new Error(`Got v${s>>4} data when expected v${Jt}.`);const[n]=new Uint16Array(t,2,1),[o]=new Uint32Array(t,4,1);return new Kt(o,n,Le[s&15],void 0,t)}constructor(t,e=16,s=Float64Array,n=ArrayBuffer,o){if(t===void 0)throw new Error("Missing required argument: numItems.");if(isNaN(t)||t<=0)throw new Error(`Unexpected numItems value: ${t}.`);this.numItems=+t,this.nodeSize=Math.min(Math.max(+e,2),65535);let i=t,h=i;this._levelBounds=[i*4];do i=Math.ceil(i/this.nodeSize),h+=i,this._levelBounds.push(h*4);while(i!==1);this.ArrayType=s||Float64Array,this.IndexArrayType=h<16384?Uint16Array:Uint32Array;const l=Le.indexOf(this.ArrayType),a=h*4*this.ArrayType.BYTES_PER_ELEMENT;if(l<0)throw new Error(`Unexpected typed array class: ${s}.`);o&&o.byteLength!==void 0&&!o.buffer?(this.data=o,this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+a,h),this._pos=h*4,this.minX=this._boxes[this._pos-4],this.minY=this._boxes[this._pos-3],this.maxX=this._boxes[this._pos-2],this.maxY=this._boxes[this._pos-1]):(this.data=new n(8+a+h*this.IndexArrayType.BYTES_PER_ELEMENT),this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+a,h),this._pos=0,this.minX=1/0,this.minY=1/0,this.maxX=-1/0,this.maxY=-1/0,new Uint8Array(this.data,0,2).set([251,(Jt<<4)+l]),new Uint16Array(this.data,2,1)[0]=e,new Uint32Array(this.data,4,1)[0]=t),this._queue=new yn}add(t,e,s,n){const o=this._pos>>2,i=this._boxes;return this._indices[o]=o,i[this._pos++]=t,i[this._pos++]=e,i[this._pos++]=s,i[this._pos++]=n,t<this.minX&&(this.minX=t),e<this.minY&&(this.minY=e),s>this.maxX&&(this.maxX=s),n>this.maxY&&(this.maxY=n),o}finish(){if(this._pos>>2!==this.numItems)throw new Error(`Added ${this._pos>>2} items when expected ${this.numItems}.`);const t=this._boxes;if(this.numItems<=this.nodeSize){t[this._pos++]=this.minX,t[this._pos++]=this.minY,t[this._pos++]=this.maxX,t[this._pos++]=this.maxY;return}const e=this.maxX-this.minX||1,s=this.maxY-this.minY||1,n=new Uint32Array(this.numItems),o=(1<<16)-1;for(let i=0,h=0;i<this.numItems;i++){const l=t[h++],a=t[h++],c=t[h++],u=t[h++],m=Math.floor(o*((l+c)/2-this.minX)/e),p=Math.floor(o*((a+u)/2-this.minY)/s);n[i]=bn(m,p)}Qt(n,t,this._indices,0,this.numItems-1,this.nodeSize);for(let i=0,h=0;i<this._levelBounds.length-1;i++){const l=this._levelBounds[i];for(;h<l;){const a=h;let c=t[h++],u=t[h++],m=t[h++],p=t[h++];for(let g=1;g<this.nodeSize&&h<l;g++)c=Math.min(c,t[h++]),u=Math.min(u,t[h++]),m=Math.max(m,t[h++]),p=Math.max(p,t[h++]);this._indices[this._pos>>2]=a,t[this._pos++]=c,t[this._pos++]=u,t[this._pos++]=m,t[this._pos++]=p}}}search(t,e,s,n,o){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let i=this._boxes.length-4;const h=[],l=[];for(;i!==void 0;){const a=Math.min(i+this.nodeSize*4,Be(i,this._levelBounds));for(let c=i;c<a;c+=4){if(s<this._boxes[c]||n<this._boxes[c+1]||t>this._boxes[c+2]||e>this._boxes[c+3])continue;const u=this._indices[c>>2]|0;i>=this.numItems*4?h.push(u):(o===void 0||o(u))&&l.push(u)}i=h.pop()}return l}neighbors(t,e,s=1/0,n=1/0,o){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let i=this._boxes.length-4;const h=this._queue,l=[],a=n*n;for(;i!==void 0;){const c=Math.min(i+this.nodeSize*4,Be(i,this._levelBounds));for(let u=i;u<c;u+=4){const m=this._indices[u>>2]|0,p=Ce(t,this._boxes[u],this._boxes[u+2]),g=Ce(e,this._boxes[u+1],this._boxes[u+3]),d=p*p+g*g;i>=this.numItems*4?h.push(m<<1,d):(o===void 0||o(m))&&h.push((m<<1)+1,d)}for(;h.length&&h.peek()&1;)if(h.peekValue()>a||(l.push(h.pop()>>1),l.length===s))return h.clear(),l;i=h.pop()>>1}return h.clear(),l}}function Ce(r,t,e){return r<t?t-r:r<=e?0:r-e}function Be(r,t){let e=0,s=t.length-1;for(;e<s;){const n=e+s>>1;t[n]>r?s=n:e=n+1}return t[e]}function Qt(r,t,e,s,n,o){if(Math.floor(s/o)>=Math.floor(n/o))return;const i=r[s+n>>1];let h=s-1,l=n+1;for(;;){do h++;while(r[h]<i);do l--;while(r[l]>i);if(h>=l)break;Mn(r,t,e,h,l)}Qt(r,t,e,s,l,o),Qt(r,t,e,l+1,n,o)}function Mn(r,t,e,s,n){const o=r[s];r[s]=r[n],r[n]=o;const i=4*s,h=4*n,l=t[i],a=t[i+1],c=t[i+2],u=t[i+3];t[i]=t[h],t[i+1]=t[h+1],t[i+2]=t[h+2],t[i+3]=t[h+3],t[h]=l,t[h+1]=a,t[h+2]=c,t[h+3]=u;const m=e[s];e[s]=e[n],e[n]=m}function bn(r,t){let e=r^t,s=65535^e,n=65535^(r|t),o=r&(t^65535),i=e|s>>1,h=e>>1^e,l=n>>1^s&o>>1^n,a=e&n>>1^o>>1^o;e=i,s=h,n=l,o=a,i=e&e>>2^s&s>>2,h=e&s>>2^s&(e^s)>>2,l^=e&n>>2^s&o>>2,a^=s&n>>2^(e^s)&o>>2,e=i,s=h,n=l,o=a,i=e&e>>4^s&s>>4,h=e&s>>4^s&(e^s)>>4,l^=e&n>>4^s&o>>4,a^=s&n>>4^(e^s)&o>>4,e=i,s=h,n=l,o=a,l^=e&n>>8^s&o>>8,a^=s&n>>8^(e^s)&o>>8,e=l^l>>1,s=a^a>>1;let c=r^t,u=s|65535^(c|e);return c=(c|c<<8)&16711935,c=(c|c<<4)&252645135,c=(c|c<<2)&858993459,c=(c|c<<1)&1431655765,u=(u|u<<8)&16711935,u=(u|u<<4)&252645135,u=(u|u<<2)&858993459,u=(u|u<<1)&1431655765,(u<<1|c)>>>0}function je(r,t=1e-7){if(r.length===0)return[];if(r.length===1)return[r];const e=new Kt(r.length);r.forEach(o=>{const[i,h]=o.firstPoint;e.add(i-t,h-t,i+t,h+t)}),e.finish();const s=[],n=new Set;return r.forEach((o,i)=>{if(n.has(i))return;const h=[o];let l=i;n.add(i);let a=r.length;for(;;){if(a--<0)throw new Error("Infinite loop detected");const c=h[h.length-1].lastPoint,[u,m]=c,p=e.search(u-t,m-t,u+t,m+t),g=w=>Math.abs((l-w)%r.length),d=p.filter(w=>!n.has(w)).map(w=>[r[w],w,g(w)]).sort(([,,w],[,,y])=>g(w)-g(y));if(d.length===0){s.push(h);break}const[M,f]=d[0];h.push(M),n.add(f),l=f}}),s}let D=class zt extends lt{constructor(t,e=[],{ignoreChecks:s=!1}={}){super(),s||Pn(t,e),this.contour=t,this.holes=e}get boundingBox(){return this.contour.boundingBox}get isFull(){return this.holes.length===0}get allLoops(){return[this.contour,...this.holes]}clone(){return new zt(this.contour.clone(),this.holes.map(t=>t.clone()))}transform(t){return new zt(this.contour.transform(t),this.holes.map(e=>e.transform(t)))}contains(t){return this.contour.contains(t)&&!this.holes.some(e=>e.contains(t))}intersects(t){return this.allLoops.some(e=>t.allLoops.some(s=>e.intersects(s)))}overlappingStrands(t){const e=t instanceof zt?t.allLoops:[t],s=this.allLoops.flatMap(n=>e.flatMap(o=>n.overlappingSegments(o)));return je(s).map(n=>new rt(n))}};function Pn(r,t=[]){if(!r)throw new Error("Figure must have a contour");for(const[e,s]of Gt([r,...t]))if(e.intersects(s))throw new Error("Loops in a figure must not intersect");if(t.some(e=>!r.contains(e.firstPoint)&&!r.onStroke(e.firstPoint)))throw new Error("Holes must be inside the contour");for(const[e,s]of Gt(t))if(e.contains(s.firstPoint))throw console.error(Ie(e),Ie(s)),new Error("Holes must not be inside other holes")}const xn=r=>{const t=r.map((n,o)=>r.slice(o+1).map((i,h)=>[h+o+1,i]).filter(([,i])=>n.boundingBox.overlaps(i.boundingBox)).map(([i])=>i)),e=[],s=Array(t.length);return t.forEach((n,o)=>{let i=s[o];i||(i=[],e.push(i)),i.push(r[o]),n.length&&n.forEach(h=>{s[h]=i})}),e},Ve=r=>r.map((t,e)=>{const s=t.segments[0].midPoint,n=r.filter((o,i)=>e===i?!1:o.contains(s));return{loop:t,isIn:n}}),En=(r,t)=>r.flatMap(({loop:e})=>Zt(t.filter(({loop:s,isIn:n})=>s===e||n.indexOf(e)!==-1))),vn=(r,t)=>{const e=t.filter(({isIn:n})=>n.length<=1),s=Zt(Ve(r.map(({loop:n})=>n)));return[e,...s]},Zt=r=>{if(!r.length)return[];const t=r.filter(({isIn:s})=>!s.length),e=r.filter(({isIn:s})=>s.length>1);return t.length===1&&e.length===0?[r]:t.length>1?En(t,r):vn(e,r)};function vt(r){return xn(r).map(Ve).flatMap(Zt).map(t=>{if(t.length===1)return new D(t[0].loop);t.sort((n,o)=>n.isIn.length-o.isIn.length);const[e,...s]=t.map(({loop:n})=>n);return new D(e,s)})}function An(r,t){const e=[];for(const s of r)for(const n of t)e.push([s,n]);return e}function*te(r,t,e){const s=i=>t.some(h=>b(h,i.lastPoint)),n=i=>e.some(h=>i.isSame(h));let o=[];for(const i of r)s(i)?(o.push(i),yield new rt(o,{ignoreChecks:!0}),o=[]):n(i)?(o.length&&(yield new rt(o,{ignoreChecks:!0}),o=[]),yield new rt([i],{ignoreChecks:!0})):o.push(i);o.length&&(yield new rt(o,{ignoreChecks:!0}))}const Ne=(r,t)=>{const e=r.findIndex(n=>b(t,n.firstPoint)),s=r.slice(0,e);return r.slice(e).concat(s)},qe=(r,t)=>{let e=r;const s=i=>b(i.firstPoint,t.firstPoint)&&b(i.lastPoint,t.lastPoint);let n=r.findIndex(s);if(n===-1){const i=r.map(h=>h.reverse());if(i.reverse(),n=i.findIndex(s),n===-1)throw console.error(i.map(h=>h.repr),t.repr),new Error("Failed to rotate to segment start");e=i}const o=e.slice(0,n);return e.slice(n).concat(o)};function Sn(r,t,e){return r.filter(s=>{const n=t.filter(i=>b(i.firstPoint,s)||b(i.lastPoint,s));if(n.length%2)throw new Error("Bug in the intersection algo on non crossing point");const o=n.map(i=>e.contains(i.midPoint));return!(o.every(i=>i)||!o.some(i=>i))})}function kn(r,t,e){let s=[];const n=[],o=new Array(r.segments.length).fill(0).map(()=>[]),i=new Array(t.segments.length).fill(0).map(()=>[]);if(r.segments.forEach((m,p)=>{t.segments.forEach((g,d)=>{const{intersections:M,overlaps:f}=Et(m,g,e);s.push(...M),o[p].push(...M),i[d].push(...M),n.push(...f);const w=f.flatMap(y=>[y.firstPoint,y.lastPoint]);s.push(...w),o[p].push(...w),i[d].push(...w)})}),s=it(s,e),!s.length||s.length===1)return null;const h=([m,p])=>p.length?m.splitAt(p):[m];let l=Z([r.segments,o]).flatMap(h),a=Z([t.segments,i]).flatMap(h);if(s=Sn(s,l,t),!s.length&&!n.length)return null;if(n.length){const m=n[0];l=qe(l,m),a=qe(a,m)}else{const m=s[0];l=Ne(l,m),a=Ne(a,m)}let c=Array.from(te(l,s,n)),u=Array.from(te(a,s,n));return(!b(u[0].lastPoint,c[0].lastPoint)||n.length>0&&u[0].segmentsCount!==1)&&(u=u.map(m=>m.reverse()).reverse(),b(u[0].lastPoint,c[0].lastPoint)||(c=c.map(m=>m.reverse()).reverse())),Z([c,u]).map(([m,p])=>m.segmentsCount===1&&n.some(g=>m.segments[0].isSame(g))?[m,"same"]:[m,p])}function Oe(r){let t=r[0];for(const e of r.slice(1))t=t.extend(e);if(!b(t.firstPoint,t.lastPoint))throw console.error(F(t.firstPoint),F(t.lastPoint)),new Error("Bug in the intersection algo on non closing strand");return new G(t.segments)}function Rn(r,t){const e=Z([t.slice(0,-1),t.slice(1)]).map(([n,o])=>Oe(r.slice(n,o)));let s=r.slice(t[t.length-1]);return t[0]!==0&&(s=s.concat(r.slice(0,t[0]))),e.push(Oe(s)),e}function Tn(r){if(!r.length)return[];const t=r.map(n=>n.firstPoint);let e=r.map(n=>n.lastPoint);e=e.slice(-1).concat(e.slice(0,-1));const s=Z([t,e]).flatMap(([n,o],i)=>b(n,o)?[]:i);try{return Rn(r,s)}catch{return je(r.flatMap(n=>n.segments)).filter(n=>n.length>1).filter(n=>b(n[0].firstPoint,n.at(-1).lastPoint)).map(n=>new G(n))}}const ze=(r,t)=>{if(r.length===0)return[t];const e=r.at(-1);return b(e.lastPoint,t.firstPoint)?r.slice(0,-1).concat([e.extend(t)]):b(e.lastPoint,t.lastPoint)?r.slice(0,-1).concat([e.extend(t.reverse())]):r.concat([t])},$n=(r,t)=>r.length===0?[t]:b(r[0].firstPoint,t.lastPoint)?[t.extend(r[0])].concat(r.slice(1)):[t].concat(r);function ee(r,t,{firstInside:e,secondInside:s}){const n=kn(r,t);if(!n){const l=r.segments[0].midPoint,a=t.contains(l),c=t.segments[0].midPoint,u=r.contains(c);return{identical:!1,firstCurveInSecond:a,secondCurveInFirst:u}}if(n.every(([,l])=>l==="same"))return{identical:!0};let o=null,i=null;const h=n.flatMap(([l,a])=>{let c=[],u=0;if(a==="same")return i===1?(i=1,l):i===2||i===0?(i=null,[]):i===null?(o?o=o.extend(l):o=l,[]):(console.error("weird situation"),[]);const m=l.segments[0].midPoint,p=t.contains(m);(e==="keep"&&p||e==="remove"&&!p)&&(u+=1,c=ze(c,l));const g=a.segments[0].midPoint,d=r.contains(g);if(s==="keep"&&d||s==="remove"&&!d){const M=a;u+=1,u===2&&c.length?(c=ze(c,M),o=null):c=[M]}return i===null&&u===1&&o&&(c=$n(c,o)),u===1&&(i=u,o=null),c.length?c:(o=null,[])});return Tn(h)}const _n=(r,t)=>{const e=ee(r,t,{firstInside:"remove",secondInside:"remove"});return Array.isArray(e)?e:e.identical?[r]:e.firstCurveInSecond?[t]:e.secondCurveInFirst?[r]:[r,t]},jt=(r,t)=>{const e=ee(r,t,{firstInside:"remove",secondInside:"keep"});return Array.isArray(e)?e:e.identical?[]:e.firstCurveInSecond?[]:e.secondCurveInFirst?[r,t]:[r]},se=(r,t)=>{const e=ee(r,t,{firstInside:"keep",secondInside:"keep"});return Array.isArray(e)?e:e.identical?[r]:e.firstCurveInSecond?[r]:e.secondCurveInFirst?[t]:[]};function Fn(r){const t=new Map,e=[];return r.forEach((s,n)=>{let o;t.has(n)?o=t.get(n):(o={current:[s],fusedWith:new Set([n])},e.push(o)),r.slice(n+1).forEach((i,h)=>{const l=o.current,a=n+h+1;if(o.fusedWith.has(a))return;let c=[i],u=!1;if(t.has(a)&&(c=t.get(a).current,u=!0),!l.some(p=>c.some(g=>p.intersects(g))))return;let m;l.length>1||c.length>1?m=At(l,c):m=Ue(l[0],c[0]),o.fusedWith.add(a),o.current=m,u||t.set(a,o)})}),e.flatMap(({current:s})=>s)}function Ue(r,t){const e=_n(r.contour,t.contour),s=t.holes.flatMap(i=>jt(i,r.contour)),n=r.holes.flatMap(i=>jt(i,t.contour)),o=An(r.holes,t.holes).flatMap(([i,h])=>se(i,h));return vt([...e,...s,...n,...o])}function Vt(r,t){if(r.isFull&&t.isFull)return vt(jt(r.contour,t.contour));if(r.isFull){const s=jt(r.contour,t.contour),n=t.holes.flatMap(o=>se(o,r.contour));return vt([...s,...n])}else if(t.isFull&&!r.contour.intersects(t.contour))if(r.contour.contains(t.contour.firstPoint)){const s=At(r.holes.map(n=>new D(n)),[t]);return vt([r.contour,...s.flatMap(n=>n.allLoops)])}else return[r];let e=Vt(new D(r.contour),t);return r.holes.forEach(s=>{e=e.flatMap(n=>Vt(n,new D(s)))}),e}function In(r,t){const e=se(r.contour,t.contour);if(!e.length)return[];let s=vt(e);return s=wt(s,r.holes.map(n=>new D(n))),wt(s,t.holes.map(n=>new D(n)))}function At(r,t){if(!r.length)return t;if(!t.length)return r;if(r.length===1&&t.length>1||t.length===1&&r.length>1)return Fn([...r,...t]);if(r.length>1&&t.length>1){let e=At([r[0]],t);return r.slice(1).forEach(s=>{e=At([s],e)}),e}return r.length===1&&t.length===1?Ue(r[0],t[0]):[]}function wt(r,t){if(!r.length)return[];if(!t.length)return r;if(r.length===1&&t.length===1)return Vt(r[0],t[0]);if(r.length>1)return r.flatMap(s=>wt([s],t));let e=Vt(r[0],t[0]);return t.slice(1).forEach(s=>{e=wt(e,[s])}),e}function ne(r,t){return!r.length||!t.length?[]:r.length===1&&t.length===1?In(r[0],t[0]):r.length>1?r.flatMap(e=>ne([e],t)):t.flatMap(e=>ne(r,[e]))}let St=class ct extends lt{constructor(t=[],{ignoreChecks:e=!1}={}){super(),this._boundingBox=null,e||Ln(t),this.figures=t}get isEmpty(){return this.figures.length===0}get boundingBox(){if(this.isEmpty)return new ot;if(this._boundingBox===null){let t=this.figures[0].boundingBox;for(const e of this.figures.slice(1))t=t.merge(e.boundingBox);this._boundingBox=t}return this._boundingBox}clone(){return new ct(this.figures.map(t=>t.clone()))}transform(t){return new ct(this.figures.map(e=>e.transform(t)))}contains(t){return this.figures.some(e=>e.contains(t))}intersects(t){return this.figures.some(e=>t.figures.some(s=>e.intersects(s)))}overlappingStrands(t){return this.figures.flatMap(e=>t instanceof ct?t.figures.flatMap(s=>e.overlappingStrands(s)):e.overlappingStrands(t))}fuse(t){return new ct(At(this.figures,t.figures))}cut(t){return new ct(wt(this.figures,t.figures))}intersect(t){return new ct(ne(this.figures,t.figures))}};function Ln(r){for(const[t,e]of Gt(r))if(t.intersects(e))throw new Error("Diagram figures must not intersect")}class re{constructor(t,e){this.firstPoint=t,this.lastPoint=e}}function Ye(r,t){if(r instanceof v)return Cn(r,t);if(r instanceof S)return Bn(r,t);throw new Error("Not implemented")}function Cn(r,t){const{firstPoint:e,lastPoint:s}=r,n=r.normalVector;return new v($(e,I(n,t)),$(s,I(n,t)))}function Bn(r,t){const e=$(r.firstPoint,I(O(r.tangentAtFirstPoint),t)),s=$(r.lastPoint,I(O(r.tangentAtLastPoint),t)),n=t*(r.clockwise?1:-1);return r.radius+n<r.precision?new re(e,s):new S(e,s,r.center,r.clockwise)}function De(r,t,e){const s=ht(r.tangentAtLastPoint,t.tangentAtFirstPoint);if(Math.abs(s)<1e-10)return null;const n=s>0?1:-1,o=Math.abs(e)*n,i=Ye(r,o),h=Ye(t,o);if(i instanceof re||h instanceof re)return null;let l;try{l=un(i,h,1e-9).at(-1)}catch{return null}if(!l)return null;const a=l,c=(p,g)=>{const d=g.tangentAt(a),M=xt(d),f=$(a,I(M,o));return p.splitAt(f)},[u]=c(r,i),[,m]=c(t,h);return{first:u,second:m,center:a}}function Xe(r,t,e){const s=De(r,t,e);if(!s)return console.warn("Cannot fillet between segments",r.repr,t.repr),[r,t];const{first:n,second:o}=s;return[n,fe(n.lastPoint,o.firstPoint,n.tangentAtLastPoint),o]}function We(r,t,e){const s=De(r,t,e);if(!s)return console.warn("Cannot chamfer between segments",r.repr,t.repr),[r,t];const{first:n,second:o}=s;return[n,new v(n.lastPoint,o.firstPoint),o]}function Ge(r,{ignoreChecks:t=!1}={}){return new St([new D(new G([...r],{ignoreChecks:t}))])}let jn=class{constructor(t=[0,0]){this.pointer=t,this.firstPoint=t,this.pendingSegments=[],this._nextCorner=null}movePointerTo(t){if(this.pendingSegments.length)throw new Error("You can only move the pointer if there is no segment defined");return this.pointer=t,this.firstPoint=t,this}saveSegment(t){if(b(t.firstPoint,t.lastPoint))throw new Error(`Segment has no length, ${t.repr}`);if(!this._nextCorner)return this.pendingSegments.push(t),this;const e=this.pendingSegments.pop();if(!e)throw new Error("bug in the custom corner algorithm");const s=this._nextCorner.mode==="chamfer"?We:Xe;return this.pendingSegments.push(...s(e,t,this._nextCorner.radius)),this._nextCorner=null,this}lineTo(t){const e=new v(this.pointer,t);return this.pointer=t,this.saveSegment(e)}line(t,e){return this.lineTo([this.pointer[0]+t,this.pointer[1]+e])}vLine(t){return this.line(0,t)}hLine(t){return this.line(t,0)}vLineTo(t){return this.lineTo([this.pointer[0],t])}hLineTo(t){return this.lineTo([t,this.pointer[1]])}polarLineTo([t,e]){const s=e*bt,n=at(t,s);return this.lineTo(n)}polarLine(t,e){const s=e*bt,[n,o]=at(t,s);return this.line(n,o)}tangentLine(t){const e=this.pendingSegments.at(-1);if(!e)throw new Error("You need a previous segment to sketch a tangent line");const[s,n]=e.tangentAtLastPoint;return this.line(s*t,n*t)}threePointsArcTo(t,e){return this.saveSegment(Ms(this.pointer,e,t)),this.pointer=t,this}threePointsArc(t,e,s,n){const[o,i]=this.pointer;return this.threePointsArcTo([o+t,i+e],[o+s,i+n])}sagittaArcTo(t,e){if(!e)return this.lineTo(t);const s=new v(this.pointer,t),n=O(s.tangentAtFirstPoint),o=$(s.midPoint,I(n,e));return this.threePointsArcTo(t,o)}sagittaArc(t,e,s){return this.sagittaArcTo([t+this.pointer[0],e+this.pointer[1]],s)}vSagittaArc(t,e){return this.sagittaArc(0,t,e)}hSagittaArc(t,e){return this.sagittaArc(t,0,e)}bulgeArcTo(t,e){if(!e)return this.lineTo(t);const s=B(this.pointer,t)/2,n=-e*s;return this.sagittaArcTo(t,n)}bulgeArc(t,e,s){return this.bulgeArcTo([t+this.pointer[0],e+this.pointer[1]],s)}vBulgeArc(t,e){return this.bulgeArc(0,t,e)}hBulgeArc(t,e){return this.bulgeArc(t,0,e)}tangentArcTo(t,e){const s=this.pendingSegments.at(-1);if(!s)throw new Error("You need a previous curve to sketch a tangent arc");return this.saveSegment(fe(this.pointer,t,e??s.tangentAtLastPoint)),this.pointer=t,this}tangentArc(t,e,s){const[n,o]=this.pointer;return this.tangentArcTo([t+n,e+o],s)}ellipseTo(t,e,s,n,o,i){return this.saveSegment(Es(this.pointer,t,e,s,n,o,i)),this.pointer=t,this}ellipse(t,e,s,n,o,i,h){return this.ellipseTo([t+this.pointer[0],e+this.pointer[1]],s,n,o,i,h)}halfEllipseTo(t,e){const[s,n]=ue(j(t,this.pointer));return this.ellipseTo(t,s/2,Math.abs(e),n*Ut,!0,e>0)}halfEllipse(t,e,s){return this.halfEllipseTo([t+this.pointer[0],e+this.pointer[1]],s)}customCorner(t,e="fillet"){if(!this.pendingSegments.length)throw new Error("You need a segment defined to fillet the angle");return t?(this._nextCorner={mode:e,radius:t},this):this}_customCornerLastWithFirst(t,e="fillet"){if(!t)return;const s=this.pendingSegments.pop(),n=this.pendingSegments.shift();if(!s||!n)throw new Error("Not enough curves to close and fillet");const o=e==="chamfer"?We:Xe;this.pendingSegments.push(...o(s,n,t))}close(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],s=this.pendingSegments.at(-1);return b(e.firstPoint,s.lastPoint)||this.lineTo(e.firstPoint),this._nextCorner!==null&&(this._customCornerLastWithFirst(this._nextCorner.radius,this._nextCorner.mode),this._nextCorner=null),Ge(this.pendingSegments,{ignoreChecks:t})}closeWithMirror(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],s=this.pendingSegments.at(-1),n=j(s.lastPoint,e.firstPoint),o=new z().mirrorLine(n,e.firstPoint),i=this.pendingSegments.map(h=>h.transform(o).reverse());return i.reverse(),Ge([...this.pendingSegments,...i],{ignoreChecks:t})}asStrand(){return new rt([...this.pendingSegments])}};function He(r=[0,0]){return new jn(r)}function Je(r){if(r instanceof D)return[r];if(r instanceof G)return[new D(r)];if(r instanceof St)return r.figures;throw new Error("Unknown shape")}function Ke(r,t,e=1e-9){let s=[];const n=[],o=new Array(t.segments.length).fill(0).map(()=>[]);t.segments.forEach((h,l)=>{r.segments.forEach(a=>{const{intersections:c,overlaps:u}=Et(h,a,e);s.push(...c),o[l].push(...c),n.push(...u);const m=u.flatMap(p=>[p.firstPoint,p.lastPoint]);s.push(...m),o[l].push(...m)})}),s=it(s,e);const i=Z([t.segments,o]).flatMap(([h,l])=>l.length?h.splitAt(l):[h]);return Array.from(te(i,s,n))}function Qe(r,t,e=!1){return Ke(t,r).filter(s=>{const n=s.segments[0].midPoint;return t.onStroke(n)?!e:!t.contains(n)})}function Vn(r,t,e=!1){return Ke(t,r).filter(s=>{const n=s.segments[0].midPoint;return t.onStroke(n)?!e:t.contains(n)})}function Ze(r,t,e=!1){const s=Qe(r,t.contour,e),n=t.holes.flatMap(o=>Vn(r,o,e));return[...s,...n]}function Nn(r,t){return new St(wt(Je(r),Je(t)))}function oe(r,t,e=!0){if(t instanceof G)return Qe(r,t,e);if(t instanceof D)return Ze(r,t,e);let s=[new rt([...r.segments])];return t.figures.forEach(n=>{s=s.flatMap(o=>Ze(o,n,e))}),s}function ie(r){if(r instanceof v)return`L ${r.lastPoint.join(" ")}`;if(r instanceof S)return`A ${r.radius} ${r.radius} 0 ${r.angularLength>Math.PI?"1":"0"} ${r.clockwise?"0":"1"} ${r.lastPoint.join(" ")}`;if(r instanceof U)return`A ${r.majorRadius} ${r.minorRadius} ${r.tiltAngle*Ut} ${r.deltaAngle>Math.PI?"1":"0"} ${r.clockwise?"0":"1"} ${r.lastPoint.join(" ")}`;throw new Error("Unknown segment type")}function ts(r){const t=`M ${r.firstPoint.join(" ")}`,e=r.segments.map(ie).join(" ");return`${t} ${e} Z`}function es(r){return`<path d="${r.allLoops.map(ts).join(" ")}" />`}function qn(r){return`<g>
|
|
11
|
+
${r.figures.map(es).join(`
|
|
12
|
+
`)}
|
|
13
|
+
</g>`}function On(r){const t=`M ${r.firstPoint.join(" ")}`,e=r.segments.map(ie).join(" ");return`${t} ${e}`}function zn(r,t=1){const e=r.xMin-t,s=r.yMin-t;return`${e} ${s} ${r.width+2*t} ${r.height+2*t}`}function ss(r,t,e=1,s){const n=zn(t,e),o=s?`width="${t.width+2*e}${s}" height="${t.height+2*e}${s}"`:"";return`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
14
|
+
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="${n}" fill="none" stroke="black" stroke-width="0.2%" vector-effect="non-scaling-stroke" ${o}>
|
|
15
|
+
${r}
|
|
16
|
+
</svg>`}function ns(r){if(r instanceof St)return qn(r);if(r instanceof D)return es(r);if(r instanceof G)return`<path d="${ts(r)}" />`;if(r instanceof rt)return`<path d="${On(r)}" />`;if(r instanceof S||r instanceof v||r instanceof U)return`<path d="${`M ${r.firstPoint.join(" ")}`} ${ie(r)}" />`;throw new Error("Unknown shape type")}const rs=r=>"shape"in r?r.shape:r,os=(r,t)=>{if(!("shape"in r))return t;const{color:e}=r;return e?`<g stroke="${e}">${t}</g>`:t};function Un(r,{margin:t=1,unit:e=null}={}){if(Array.isArray(r)){const n=r.map(h=>rs(h).mirror()),o=n.map((h,l)=>os(r[l],ns(h))).join(`
|
|
17
|
+
`),i=n.slice(1).reduce((h,l)=>h.merge(l.boundingBox),n[0].boundingBox);return ss(o,i,t,e)}const s=rs(r).mirror();return ss(os(r,ns(s)),s.boundingBox,t,e)}class dt extends lt{constructor(t,{cutLines:e=[],foldLines:s=[],foldLinesForwards:n=[],foldLinesBackwards:o=[]}={}){super(),this.body=t,this.cutLines=[...e],this.foldLinesBackwards=[...s,...o],this.foldLinesForwards=[...n]}get foldLines(){return[...this.foldLinesForwards,...this.foldLinesBackwards]}clone(){return new dt(this.body,{cutLines:[...this.cutLines],foldLinesForwards:[...this.foldLinesForwards],foldLinesBackwards:[...this.foldLinesBackwards]})}addCutLine(t){return this.cutLines.push(t),this}addFoldLine(t,e="backwards"){e==="forwards"?this.foldLinesForwards.push(t):this.foldLinesBackwards.push(t)}fuseFold(t,e="backwards"){const s=t instanceof dt?t.body:t,n=this.body.overlappingStrands(s);return e==="forwards"?this.foldLinesForwards.push(...n):this.foldLinesBackwards.push(...n),this.fuseBody(t),this}fuseBody(t){let e;return t instanceof dt?(e=t.body,this.cutLines.push(...t.cutLines),this.foldLinesForwards.push(...t.foldLinesForwards),this.foldLinesBackwards.push(...t.foldLinesBackwards)):e=t,this.body=this.body.fuse(e),this}cutShape(t){return this.body=Nn(this.body,t),this.cutLines=this.cutLines.flatMap(e=>oe(e,t,!0)),this.eraseFolds(t),this}eraseFolds(t){this.foldLinesBackwards=this.foldLinesBackwards.flatMap(e=>oe(e,t,!0)),this.foldLinesForwards=this.foldLinesForwards.flatMap(e=>oe(e,t,!0))}transform(t){return new dt(this.body.transform(t),{cutLines:this.cutLines.map(s=>s.transform(t)),foldLinesForwards:this.foldLinesForwards.map(s=>s.transform(t)),foldLinesBackwards:this.foldLinesBackwards.map(s=>s.transform(t))})}asSVG(){const t=[];return this.body&&t.push({shape:this.body,color:"red"}),this.cutLines.length&&this.cutLines.map(e=>t.push({shape:e,color:"red"})),this.foldLinesBackwards.length&&this.foldLinesBackwards.map(e=>t.push({shape:e,color:"green"})),this.foldLinesForwards.length&&this.foldLinesForwards.map(e=>t.push({shape:e,color:"blue"})),Un(t,{unit:"mm"})}}function he(r,t,{fillet:e=1,contraction:s=1,contractionMode:n="rounded",contractionLeft:o,contractionRight:i,leftContractionMode:h,rightContractionMode:l}={}){const a=o??s,c=i??s,u=He([-r/2,0]).hLine(r);return c?(l??n)==="rounded"?(u.tangentArc(-c,c,[-1,0]),u.vLine(t-c)):u.line(-c,t):u.vLine(t),u.customCorner(e),u.hLine(-r+a+c),u.customCorner(e),a?(h??n)==="rounded"?(u.vLine(-t+a),u.tangentArcTo([-r/2,0])):u.lineTo([-r/2,0]):u.vLine(-t),u.close()}function is(r,t,e={}){return he(r,t,e)}function hs(r,t,e={}){return he(r,t,e).mirror("x")}function ae(r,t,{contractionTop:e,contractionBottom:s,topContractionMode:n,bottomContractionMode:o,...i}={}){return he(r,t,{contractionLeft:e,contractionRight:s,leftContractionMode:n,rightContractionMode:o,...i}).rotate(90)}function as(r,t,e={}){return ae(r,t,e).mirror("y")}const Yn={right:as,left:ae,top:is,bottom:hs},ls=(r,t)=>He([-r/2,0]).line(t,-t).hLine(r-2*t).line(t,t);class Nt extends lt{constructor(t,e=.2){if(super(),typeof t!="number"){this.cut=t.cut,this.unfold=t.unfold,this.bump=t.bump;return}const s=ls(t,e*2.5);this.cut=s.asStrand(),this.unfold=s.close(),this.bump=ls(t-5*e,5*e).close().mirror("x")}clone(){return new Nt(this)}transform(t){return new Nt({cut:this.cut.transform(t),unfold:this.unfold.transform(t),bump:this.bump.transform(t)})}makeCut(t){t.eraseFolds(this.unfold),t.addCutLine(this.cut)}fuseBump(t){t.fuseBody(this.bump)}}class qt extends lt{constructor(t){super(),this.points=t,this.points=t}transform(t){return new qt(this.points.map(t.transform))}distribute(t){return this.points.map(e=>t.translateTo(e))}cloneDistribute(t){return this.points.map(e=>t.clone().translateTo(e))}}function Dn(r,t,e=3,s=0){if(r<=t*2)throw new Error("totalWidth must be greater than twice the objectWidth");const n=Math.max(Math.floor((r-2*s)/(t*(e+1))),2),i=(r-2*s-n*t)/(n-1),h=[];for(let l=0;l<n;l++)h.push(s+l*(t+i)-r/2+t/2);return new qt(h.map(l=>[l,0]))}function Xn(r,t,e="x"){const s=r*(t-1),n=[];for(let o=0;o<t;o++)n.push(o*r-s/2);return new qt(n.map(o=>e.toLowerCase()==="x"?[o,0]:[0,o]))}q.Dieline=dt,q.FoldLockBump=Nt,q.bottomFlap=hs,q.drawFlaps=Yn,q.leftFlap=ae,q.linearDistribution=Dn,q.linearSpread=Xn,q.rightFlap=as,q.topFlap=is,Object.defineProperty(q,Symbol.toStringTag,{value:"Module"})});
|
|
18
|
+
//# sourceMappingURL=dielines.umd.cjs.map
|