saxi
Version:
Drive the AxiDraw pen plotter
3 lines (2 loc) • 21.4 kB
JavaScript
(()=>{var _e=Object.create;var le=Object.defineProperty;var ke=Object.getOwnPropertyDescriptor;var Te=Object.getOwnPropertyNames;var Ae=Object.getPrototypeOf,Ie=Object.prototype.hasOwnProperty;var Q=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var Fe=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Te(e))!Ie.call(t,s)&&s!==n&&le(t,s,{get:()=>e[s],enumerable:!(r=ke(e,s))||r.enumerable});return t};var Ue=(t,e,n)=>(n=t!=null?_e(Ae(t)):{},Fe(e||!t||!t.__esModule?le(n,"default",{value:t,enumerable:!0}):n,t));var he=Q(U=>{"use strict";Object.defineProperty(U,"__esModule",{value:!0});U.vsub=U.vlen=U.vlen2=void 0;function pe(t){return t.x*t.x+t.y*t.y}U.vlen2=pe;function Ee(t){return Math.sqrt(pe(t))}U.vlen=Ee;function qe(t,e){return{x:t.x-e.x,y:t.y-e.y}}U.vsub=qe});var me=Q((re,ie)=>{(function(t,e){typeof re=="object"&&typeof ie<"u"?ie.exports=e():typeof define=="function"&&define.amd?define(e):(t=t||self,t.TinyQueue=e())})(re,function(){"use strict";var t=function(r,s){if(r===void 0&&(r=[]),s===void 0&&(s=e),this.data=r,this.length=this.data.length,this.compare=s,this.length>0)for(var c=(this.length>>1)-1;c>=0;c--)this._down(c)};t.prototype.push=function(r){this.data.push(r),this.length++,this._up(this.length-1)},t.prototype.pop=function(){if(this.length!==0){var r=this.data[0],s=this.data.pop();return this.length--,this.length>0&&(this.data[0]=s,this._down(0)),r}},t.prototype.peek=function(){return this.data[0]},t.prototype._up=function(r){for(var s=this,c=s.data,u=s.compare,l=c[r];r>0;){var d=r-1>>1,b=c[d];if(u(l,b)>=0)break;c[r]=b,r=d}c[r]=l},t.prototype._down=function(r){for(var s=this,c=s.data,u=s.compare,l=this.length>>1,d=c[r];r<l;){var b=(r<<1)+1,x=c[b],y=b+1;if(y<this.length&&u(c[y],x)<0&&(b=y,x=c[y]),u(x,d)>=0)break;c[r]=x,r=b}c[r]=d};function e(n,r){return n<r?-1:n>r?1:0}return t})});var de=Q(J=>{"use strict";var Le=J&&J.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(J,"__esModule",{value:!0});J.knn=void 0;var je=Le(me());function Re(t,e,n,r,s,c){let u=t.data,l=[],d=t.toBBox,b,x,y,w,g=new je.default(void 0,Je);for(;u;){for(b=0;b<u.children.length;b++)x=u.children[b],y=He(e,n,u.leaf?d(x):x),(!c||y<=c*c)&&g.push({node:x,isItem:u.leaf,dist:y});for(;g.length&&g.peek().isItem;)if(w=g.pop().node,(!s||s(w))&&l.push(w),r&&l.length===r)return l;u=g.pop(),u&&(u=u.node)}return l}J.knn=Re;function Je(t,e){return t.dist-e.dist}function He(t,e,n){var r=fe(t,n.minX,n.maxX),s=fe(e,n.minY,n.maxY);return r*r+s*s}function fe(t,e,n){return t<e?e-t:t<=n?0:t-n}});var xe=Q((oe,se)=>{(function(t,e){typeof oe=="object"&&typeof se<"u"?se.exports=e():typeof define=="function"&&define.amd?define(e):(t=t||self).RBush=e()})(oe,function(){"use strict";function t(i,o,a,p,h){(function m(f,v,M,P,B){for(;P>M;){if(P-M>600){var V=P-M+1,S=v-M+1,W=Math.log(V),L=.5*Math.exp(2*W/3),j=.5*Math.sqrt(W*L*(V-L)/V)*(S-V/2<0?-1:1),R=Math.max(M,Math.floor(v-S*L/V+j)),Ye=Math.min(P,Math.floor(v+(V-S)*L/V+j));m(f,v,R,Ye,B)}var O=f[v],G=M,Y=P;for(e(f,M,v),B(f[P],O)>0&&e(f,M,P);G<Y;){for(e(f,G,Y),G++,Y--;B(f[G],O)<0;)G++;for(;B(f[Y],O)>0;)Y--}B(f[M],O)===0?e(f,M,Y):e(f,++Y,P),Y<=v&&(M=Y+1),v<=Y&&(P=Y-1)}})(i,o,a||0,p||i.length-1,h||n)}function e(i,o,a){var p=i[o];i[o]=i[a],i[a]=p}function n(i,o){return i<o?-1:i>o?1:0}var r=function(i){i===void 0&&(i=9),this._maxEntries=Math.max(4,i),this._minEntries=Math.max(2,Math.ceil(.4*this._maxEntries)),this.clear()};function s(i,o,a){if(!a)return o.indexOf(i);for(var p=0;p<o.length;p++)if(a(i,o[p]))return p;return-1}function c(i,o){u(i,0,i.children.length,o,i)}function u(i,o,a,p,h){h||(h=z(null)),h.minX=1/0,h.minY=1/0,h.maxX=-1/0,h.maxY=-1/0;for(var m=o;m<a;m++){var f=i.children[m];l(h,i.leaf?p(f):f)}return h}function l(i,o){return i.minX=Math.min(i.minX,o.minX),i.minY=Math.min(i.minY,o.minY),i.maxX=Math.max(i.maxX,o.maxX),i.maxY=Math.max(i.maxY,o.maxY),i}function d(i,o){return i.minX-o.minX}function b(i,o){return i.minY-o.minY}function x(i){return(i.maxX-i.minX)*(i.maxY-i.minY)}function y(i){return i.maxX-i.minX+(i.maxY-i.minY)}function w(i,o){return i.minX<=o.minX&&i.minY<=o.minY&&o.maxX<=i.maxX&&o.maxY<=i.maxY}function g(i,o){return o.minX<=i.maxX&&o.minY<=i.maxY&&o.maxX>=i.minX&&o.maxY>=i.minY}function z(i){return{children:i,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function A(i,o,a,p,h){for(var m=[o,a];m.length;)if(!((a=m.pop())-(o=m.pop())<=p)){var f=o+Math.ceil((a-o)/p/2)*p;t(i,f,o,a,h),m.push(o,f,f,a)}}return r.prototype.all=function(){return this._all(this.data,[])},r.prototype.search=function(i){var o=this.data,a=[];if(!g(i,o))return a;for(var p=this.toBBox,h=[];o;){for(var m=0;m<o.children.length;m++){var f=o.children[m],v=o.leaf?p(f):f;g(i,v)&&(o.leaf?a.push(f):w(i,v)?this._all(f,a):h.push(f))}o=h.pop()}return a},r.prototype.collides=function(i){var o=this.data;if(!g(i,o))return!1;for(var a=[];o;){for(var p=0;p<o.children.length;p++){var h=o.children[p],m=o.leaf?this.toBBox(h):h;if(g(i,m)){if(o.leaf||w(i,m))return!0;a.push(h)}}o=a.pop()}return!1},r.prototype.load=function(i){if(!i||!i.length)return this;if(i.length<this._minEntries){for(var o=0;o<i.length;o++)this.insert(i[o]);return this}var a=this._build(i.slice(),0,i.length-1,0);if(this.data.children.length)if(this.data.height===a.height)this._splitRoot(this.data,a);else{if(this.data.height<a.height){var p=this.data;this.data=a,a=p}this._insert(a,this.data.height-a.height-1,!0)}else this.data=a;return this},r.prototype.insert=function(i){return i&&this._insert(i,this.data.height-1),this},r.prototype.clear=function(){return this.data=z([]),this},r.prototype.remove=function(i,o){if(!i)return this;for(var a,p,h,m=this.data,f=this.toBBox(i),v=[],M=[];m||v.length;){if(m||(m=v.pop(),p=v[v.length-1],a=M.pop(),h=!0),m.leaf){var P=s(i,m.children,o);if(P!==-1)return m.children.splice(P,1),v.push(m),this._condense(v),this}h||m.leaf||!w(m,f)?p?(a++,m=p.children[a],h=!1):m=null:(v.push(m),M.push(a),a=0,p=m,m=m.children[0])}return this},r.prototype.toBBox=function(i){return i},r.prototype.compareMinX=function(i,o){return i.minX-o.minX},r.prototype.compareMinY=function(i,o){return i.minY-o.minY},r.prototype.toJSON=function(){return this.data},r.prototype.fromJSON=function(i){return this.data=i,this},r.prototype._all=function(i,o){for(var a=[];i;)i.leaf?o.push.apply(o,i.children):a.push.apply(a,i.children),i=a.pop();return o},r.prototype._build=function(i,o,a,p){var h,m=a-o+1,f=this._maxEntries;if(m<=f)return c(h=z(i.slice(o,a+1)),this.toBBox),h;p||(p=Math.ceil(Math.log(m)/Math.log(f)),f=Math.ceil(m/Math.pow(f,p-1))),(h=z([])).leaf=!1,h.height=p;var v=Math.ceil(m/f),M=v*Math.ceil(Math.sqrt(f));A(i,o,a,M,this.compareMinX);for(var P=o;P<=a;P+=M){var B=Math.min(P+M-1,a);A(i,P,B,v,this.compareMinY);for(var V=P;V<=B;V+=v){var S=Math.min(V+v-1,B);h.children.push(this._build(i,V,S,p-1))}}return c(h,this.toBBox),h},r.prototype._chooseSubtree=function(i,o,a,p){for(;p.push(o),!o.leaf&&p.length-1!==a;){for(var h=1/0,m=1/0,f=void 0,v=0;v<o.children.length;v++){var M=o.children[v],P=x(M),B=(V=i,S=M,(Math.max(S.maxX,V.maxX)-Math.min(S.minX,V.minX))*(Math.max(S.maxY,V.maxY)-Math.min(S.minY,V.minY))-P);B<m?(m=B,h=P<h?P:h,f=M):B===m&&P<h&&(h=P,f=M)}o=f||o.children[0]}var V,S;return o},r.prototype._insert=function(i,o,a){var p=a?i:this.toBBox(i),h=[],m=this._chooseSubtree(p,this.data,o,h);for(m.children.push(i),l(m,p);o>=0&&h[o].children.length>this._maxEntries;)this._split(h,o),o--;this._adjustParentBBoxes(p,h,o)},r.prototype._split=function(i,o){var a=i[o],p=a.children.length,h=this._minEntries;this._chooseSplitAxis(a,h,p);var m=this._chooseSplitIndex(a,h,p),f=z(a.children.splice(m,a.children.length-m));f.height=a.height,f.leaf=a.leaf,c(a,this.toBBox),c(f,this.toBBox),o?i[o-1].children.push(f):this._splitRoot(a,f)},r.prototype._splitRoot=function(i,o){this.data=z([i,o]),this.data.height=i.height+1,this.data.leaf=!1,c(this.data,this.toBBox)},r.prototype._chooseSplitIndex=function(i,o,a){for(var p,h,m,f,v,M,P,B=1/0,V=1/0,S=o;S<=a-o;S++){var W=u(i,0,S,this.toBBox),L=u(i,S,a,this.toBBox),j=(h=W,m=L,f=void 0,v=void 0,M=void 0,P=void 0,f=Math.max(h.minX,m.minX),v=Math.max(h.minY,m.minY),M=Math.min(h.maxX,m.maxX),P=Math.min(h.maxY,m.maxY),Math.max(0,M-f)*Math.max(0,P-v)),R=x(W)+x(L);j<B?(B=j,p=S,V=R<V?R:V):j===B&&R<V&&(V=R,p=S)}return p||a-o},r.prototype._chooseSplitAxis=function(i,o,a){var p=i.leaf?this.compareMinX:d,h=i.leaf?this.compareMinY:b;this._allDistMargin(i,o,a,p)<this._allDistMargin(i,o,a,h)&&i.children.sort(p)},r.prototype._allDistMargin=function(i,o,a,p){i.children.sort(p);for(var h=this.toBBox,m=u(i,0,o,h),f=u(i,a-o,a,h),v=y(m)+y(f),M=o;M<a-o;M++){var P=i.children[M];l(m,i.leaf?h(P):P),v+=y(m)}for(var B=a-o-1;B>=o;B--){var V=i.children[B];l(f,i.leaf?h(V):V),v+=y(f)}return v},r.prototype._adjustParentBBoxes=function(i,o,a){for(var p=a;p>=0;p--)l(o[p],i)},r.prototype._condense=function(i){for(var o=i.length-1,a=void 0;o>=0;o--)i[o].children.length===0?o>0?(a=i[o-1].children).splice(a.indexOf(i[o]),1):this.clear():c(i[o],this.toBBox)},r})});var ye=Q(_=>{"use strict";var $e=_&&_.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(_,"__esModule",{value:!0});_.reorder=_.elideShorterThan=_.merge=void 0;var be=he(),Ne=de(),Ce=$e(xe());function We(t,e){return t.slice(t.findIndex(n=>!e(n)))}function Ge(t,e=.5){if(t.length===0)return[];let n=e*e,r=t.slice(),s=[r.shift()];for(;r.length;){let c=s[s.length-1],u=r.shift(),l=c[c.length-1],d=u[0],b=l.x-d.x,x=l.y-d.y;b*b+x*x<=n?s[s.length-1]=s[s.length-1].concat(We(u,w=>{let g=l.x-w.x,z=l.y-w.y;return g*g+z*z<=n})):s.push(u)}return s}_.merge=Ge;function Qe(t){if(t.length<=1)return 0;let e=0,n=t[0];for(let r=1;r<t.length;r++)e+=be.vlen(be.vsub(n,t[r])),n=t[r];return e}function Ke(t,e){return t.filter(n=>Qe(n)>=e)}_.elideShorterThan=Ke;function*Ze(t,e){for(let n=t;n<e;n++)yield n}function Oe(t){if(t.length===0)return t;let e=l=>l%2===0?t[l/2|0][0]:t[l/2|0][t[l/2|0].length-1];class n extends Ce.default{toBBox(d){let{x:b,y:x}=e(d);return{minX:b,minY:x,maxX:b,maxY:x}}compareMinX(d,b){return e(d).x-e(b).x}compareMinY(d,b){return e(d).y-e(b).y}}let r=new n;r.load([...Ze(2,t.length*2)]);let s=[];s.push(t[0]);let c=e(1),u=t.length*2-2;for(;u;){let[l]=Ne.knn(r,c.x,c.y,1),d=l/2|0;r.remove(d*2),r.remove(d*2+1),s.push(l%2===0?t[d]:t[d].slice().reverse()),c=e(l%2===0?l+1:l-1),u-=2}return s}_.reorder=Oe});var C=Ue(ye(),1);function ce(t){return t.x*t.x+t.y*t.y}function K(t){return Math.sqrt(ce(t))}function X(t,e){return{x:t.x-e.x,y:t.y-e.y}}function D(t,e){return{x:t.x*e,y:t.y*e}}function Z(t){return D(t,1/K(t))}function k(t,e){return{x:t.x+e.x,y:t.y+e.y}}function ve(t,e){return t.x*e.x+t.y*e.y}function Me(t,e,n){if(n===0)return t;let r=Math.PI/180*n,s=Math.cos(r),c=Math.sin(r),u=s*(t.x-e.x)-c*(t.y-e.y)+e.x,l=s*(t.y-e.y)+c*(t.x-e.x)+e.y;return{x:u,y:l}}function H(t,e=2){return{x:Number(t.x.toFixed(e)),y:Number(t.y.toFixed(e))}}var ee=class t{get landscape(){return new t({x:Math.max(this.size.x,this.size.y),y:Math.min(this.size.x,this.size.y)})}get portrait(){return new t({x:Math.min(this.size.x,this.size.y),y:Math.max(this.size.x,this.size.y)})}get isLandscape(){return this.size.x===Math.max(this.size.x,this.size.y)}static standard={USLetter:new t(H(D({x:8.5,y:11},25.4))),USLegal:new t(H(D({x:8.5,y:14},25.4))),ArchA:new t(H(D({x:9,y:12},25.4))),A3:new t({x:297,y:420}),A4:new t({x:210,y:297}),A5:new t({x:148,y:210}),A6:new t({x:105,y:148}),"6x8":new t(H(D({x:6,y:8},25.4))),"5x7":new t(H(D({x:5,y:7},25.4))),"11x14":new t(H(D({x:11,y:14},25.4)))};size;constructor(e){this.size=e}};var $=1e-9,Mt={penUpHeight:50,penDownHeight:60,pointJoinRadius:0,pathJoinRadius:.5,paperSize:ee.standard.ArchA.landscape,marginMm:20,selectedGroupLayers:new Set,selectedStrokeLayers:new Set,layerMode:"stroke",penDownAcceleration:200,penDownMaxVelocity:50,penDownCorneringFactor:.127,penUpAcceleration:400,penUpMaxVelocity:200,penDropDuration:.12,penLiftDuration:.12,sortPaths:!0,rotateDrawing:0,fitPage:!0,cropToMargins:!0,minimumPathLength:0,hardware:"v3",penHome:{x:0,y:0}},ge=(t="v3")=>t==="brushless"?E:t==="nextdraw-2234"?q:I,I={stepsPerMm:5,penServoMin:7500,penServoMax:28e3,penPctToPos(t){let e=t/100;return Math.round(this.penServoMin*e+this.penServoMax*(1-e))}},E={stepsPerMm:5,penServoMin:5400,penServoMax:12600,penPctToPos(t){let e=t/100;return Math.round(this.penServoMin*e+this.penServoMax*(1-e))}},q={stepsPerMm:5,penServoMin:19600,penServoMax:28e3,penPctToPos(t){let e=t/100;return Math.round(this.penServoMin*e+this.penServoMax*(1-e))}},Pt={penDownProfile:{acceleration:200*I.stepsPerMm,maximumVelocity:50*I.stepsPerMm,corneringFactor:.127*I.stepsPerMm},penUpProfile:{acceleration:400*I.stepsPerMm,maximumVelocity:200*I.stepsPerMm,corneringFactor:0},penUpPos:I.penPctToPos(50),penDownPos:I.penPctToPos(60),penDropDuration:.12,penLiftDuration:.12},gt={penDownProfile:{acceleration:200*E.stepsPerMm,maximumVelocity:50*E.stepsPerMm,corneringFactor:.127*E.stepsPerMm},penUpProfile:{acceleration:400*E.stepsPerMm,maximumVelocity:200*E.stepsPerMm,corneringFactor:0},penUpPos:E.penPctToPos(50),penDownPos:E.penPctToPos(60),penDropDuration:.08,penLiftDuration:.08},Vt={penDownProfile:{acceleration:200*q.stepsPerMm,maximumVelocity:50*q.stepsPerMm,corneringFactor:.127*q.stepsPerMm},penUpProfile:{acceleration:400*q.stepsPerMm,maximumVelocity:200*q.stepsPerMm,corneringFactor:0},penUpPos:q.penPctToPos(50),penDownPos:q.penPctToPos(60),penDropDuration:.08,penLiftDuration:.08},T=class t{constructor(e,n,r,s,c){this.accel=e;this.duration=n;this.vInitial=r;this.p1=s;this.p2=c;if(!(r>=0))throw new Error(`vInitial must be >= 0, but was ${r}`);if(!(r+e*n>=-$))throw new Error(`vFinal must be >= 0, but vInitial=${r}, duration=${n}, accel=${e}`);this.accel=e,this.duration=n,this.vInitial=r,this.p1=s,this.p2=c,this.distance=K(X(s,c))}accel;duration;vInitial;p1;p2;static deserialize(e){return new t(e.accel,e.duration,e.vInitial,e.p1,e.p2)}distance;get vFinal(){return Math.max(0,this.vInitial+this.accel*this.duration)}instant(e,n=0,r=0){let s=Math.max(0,Math.min(this.duration,e)),c=this.accel,u=this.vInitial+this.accel*s,l=Math.max(0,Math.min(this.distance,this.vInitial*s+c*s*s/2)),d=k(this.p1,D(Z(X(this.p2,this.p1)),l));return{t:s+n,p:d,s:l+r,v:u,a:c}}serialize(){return{accel:this.accel,duration:this.duration,vInitial:this.vInitial,p1:this.p1,p2:this.p2}}},F=class t{constructor(e,n,r){this.initialPos=e;this.finalPos=n;this.pDuration=r}initialPos;finalPos;pDuration;static deserialize(e){return new t(e.initialPos,e.finalPos,e.duration)}duration(){return this.pDuration}serialize(){return{initialPos:this.initialPos,finalPos:this.finalPos,duration:this.pDuration}}};function Pe(t,e,n){let r=[],s=e;r.push(s);for(let c of t)s=n(s,c),r.push(s);return r}function et(t,e){let n=0,r=t.length;for(;n<r;){let s=Math.floor((n+r)/2);t[s]<e?n=s+1:r=s}return n}var N=class t{constructor(e){this.blocks=e;this.ts=Pe(e.map(n=>n.duration),0,(n,r)=>n+r).slice(0,-1),this.ss=Pe(e.map(n=>n.distance),0,(n,r)=>n+r).slice(0,-1)}blocks;static deserialize(e){return new t(e.blocks.map(T.deserialize))}ts;ss;get p1(){return this.blocks[0].p1}get p2(){return this.blocks[this.blocks.length-1].p2}duration(){return this.blocks.map(e=>e.duration).reduce((e,n)=>e+n,0)}instant(e){let n=et(this.ts,e),r=this.ts[n]===e?n:n-1;return this.blocks[r].instant(e-this.ts[r],this.ts[r],this.ss[r])}serialize(){return{blocks:this.blocks.map(e=>e.serialize())}}},ue=class t{constructor(e){this.motions=e}motions;static deserialize(e){return new t(e.map(n=>{if("blocks"in n)return N.deserialize(n);if("initialPos"in n)return F.deserialize(n);throw new Error(`Wrong parameter: ${n}`)}))}duration(e=0){return this.motions.slice(e).map(n=>n.duration()).reduce((n,r)=>n+r,0)}motion(e){return this.motions[e]}withPenHeights(e,n){let r=0;return new t(this.motions.map((s,c)=>{if(s instanceof N)return s;if(s instanceof F)return c===this.motions.length-1?new F(n,e,s.duration()):r++%2===0?new F(e,n,s.duration()):new F(n,e,s.duration());throw new Error(`Wrong motion ${s}`)}))}serialize(){return this.motions.map(e=>e.serialize())}},te=class{constructor(e,n,r=[]){this.p1=e;this.p2=n;this.blocks=r}p1;p2;blocks;maxEntryVelocity=0;entryVelocity=0;length(){return K(X(this.p2,this.p1))}direction(){return Z(X(this.p2,this.p1))}};function tt(t,e,n,r,s){let c=-ve(t.direction(),e.direction());if(Math.abs(c-1)<$)return 0;let u=Math.sqrt((1-c)/2);if(Math.abs(u-1)<$)return n;let l=Math.sqrt(r*s*u/(1-u));return Math.min(l,n)}function nt(t,e,n,r,s,c){let u=(2*r*t+n*n-e*e)/(4*r),l=t-u,d=Math.sqrt(e*e+2*r*u),b=(d-e)/r,x=(n-d)/-r,y=k(s,D(Z(X(c,s)),u));return{s1:u,s2:l,t1:b,t2:x,vMax:d,p1:s,p2:y,p3:c}}function rt(t,e,n,r,s,c,u){let l=(n-e)/s,d=(n+e)/2*l,b=(r-n)/-s,x=(r+n)/2*b,y=t-d-x,w=y/n,g=Z(X(u,c)),z=k(c,D(g,d)),A=k(c,D(g,t-x));return{s1:d,s2:y,s3:x,t1:l,t2:w,t3:b,p1:c,p2:z,p3:A,p4:u}}function it(t,e){if(e===0)return t;let n=[];n.push(t[0]);for(let r of t.slice(1))K(X(r,n[n.length-1]))>e&&n.push(r);return n}function ae(t,e){let n=it(t,$);if(n.length===1)return new N([new T(0,0,0,n[0],n[0])]);let r=n.slice(1).map((x,y)=>new te(n[y],x)),s=e.acceleration,c=e.maximumVelocity,u=e.corneringFactor;r.slice(1).forEach((x,y)=>{let w=r[y];x.maxEntryVelocity=tt(w,x,c,s,u)});let l=n[n.length-1];r.push(new te(l,l));let d=0;for(;d<r.length-1;){let x=r[d],y=r[d+1],w=x.length(),g=x.entryVelocity,z=y.maxEntryVelocity,A=x.p1,i=x.p2,o=nt(w,g,z,s,A,i);if(o.s1<-$)x.maxEntryVelocity=Math.sqrt(z*z+2*s*w),d-=1;else if(o.s2<=0){let a=Math.sqrt(g*g+2*s*w),p=(a-g)/s;x.blocks=[new T(s,p,g,A,i)],y.entryVelocity=a,d+=1}else if(o.vMax>c){let a=rt(w,g,c,z,s,A,i);x.blocks=[new T(s,a.t1,g,a.p1,a.p2),new T(0,a.t2,c,a.p2,a.p3),new T(-s,a.t3,c,a.p3,a.p4)],y.entryVelocity=z,d+=1}else x.blocks=[new T(s,o.t1,g,o.p1,o.p2),new T(-s,o.t2,o.vMax,o.p2,o.p3)],y.entryVelocity=z,d+=1}let b=[];for(let x of r)for(let y of x.blocks)y.duration>$&&b.push(y);return new N(b)}function Ve(t,e,n={x:0,y:0}){let r=[],s=n,c={up:new F(e.penDownPos,e.penUpPos,e.penLiftDuration),down:new F(e.penUpPos,e.penDownPos,e.penDropDuration)};for(let u of t){let l=ae(u,e.penDownProfile),d=ae([s,l.p1],e.penUpProfile);r.push(d,c.down,l,c.up),s=l.p2}return r.push(ae([s,n],e.penUpProfile)),new ue(r)}function ot(t){let e=-1/0,n=-1/0,r=1/0,s=1/0;for(let c of t)for(let u of c)u.x>e&&(e=u.x),u.y>n&&(n=u.y),u.x<r&&(r=u.x),u.y<s&&(s=u.y);return[{x:r,y:s},{x:e,y:n}]}function st(t,e,n){let[r,s]=ot(t),c=n.x-e.x,u=n.y-e.y,l=c/(s.x-r.x),d=u/(s.y-r.y),b=Math.min(l,d),x=k(e,D(X(n,e),.5)),y=X(x,D(X(s,r),b*.5));return t.map(w=>w.map(g=>k(D(X(g,r),b),y)))}function De(t,e,n){return st(t,{x:n,y:n},X(e.size,{x:n,y:n}))}function ne(t,e){let[n,r]=t,[s,c]=e,u=X(c,s),l=[-u.x,u.x,-u.y,u.y],d=[s.x-n.x,r.x-s.x,s.y-n.y,r.y-s.y],b=-1/0,x=1/0;for(let y=0;y<4;y++)if(l[y]===0){if(d[y]<0)return null}else{let w=d[y]/l[y];l[y]<0&&b<w?b=w:l[y]>0&&x>w&&(x=w)}return b>x||b>1||b<0?null:k(s,D(u,b))}function we(t,e){let[n,r]=t;return e.x>=n.x&&e.x<=r.x&&e.y>=n.y&&e.y<=r.y}function ct(t,e){let[n,r]=e,s=we(t,n),c=we(t,r);if(s&&c)return e;if(s&&!c)return[e[0],ne(t,[e[1],e[0]])];if(!s&&c)return[ne(t,e),e[1]];let u=ne(t,e),l=ne(t,[e[1],e[0]]);return u&&l?[u,l]:null}function at(t,e){let n=[],r=null;for(let s=1;s<t.length;s++){let[c,u]=[t[s-1],t[s]],l=ct(e,[c,u]);l?(r||(r=[l[0]],n.push(r)),r.push(l[1]),l[1]!==u&&(r=null)):r=null}return n}function Be(t,e,n){let r=[{x:0,y:0},e.size],s={x:n,y:n},c=[k(r[0],s),X(r[1],s)],u=[];for(let l of t)for(let d of at(l,c))u.push(d);return u}function ze(t,e){if(e===0)return t;let n=[t[0]],r=e*e;for(let s of t.slice(1))ce(X(s,n[n.length-1]))>r&&n.push(s);return n}var ut=96,lt=25.4,Se=lt/ut;function Xe(t,e){let n=t.map(c=>c.points),r=ge(e.hardware);e.rotateDrawing!==0&&(console.time("rotating paths"),n=n.map(c=>c.map(u=>Me(u,D({x:e.paperSize.size.x/2,y:e.paperSize.size.y/2},1/Se),e.rotateDrawing))),console.timeEnd("rotating paths")),e.fitPage?n=De(n,e.paperSize,e.marginMm):(n=n.map(c=>c.map(u=>D(u,Se))),e.cropToMargins&&(n=Be(n,e.paperSize,e.marginMm))),e.layerMode==="group"?n=n.filter((c,u)=>e.selectedGroupLayers.has(t[u].groupId)):e.layerMode==="stroke"&&(n=n.filter((c,u)=>e.selectedStrokeLayers.has(t[u].stroke))),e.pointJoinRadius>0&&(n=n.map(c=>ze(c,e.pointJoinRadius))),e.sortPaths&&(console.time("sorting paths"),n=(0,C.reorder)(n),console.timeEnd("sorting paths")),e.minimumPathLength>0&&(console.time("eliding short paths"),n=(0,C.elideShorterThan)(n,e.minimumPathLength),console.timeEnd("eliding short paths")),e.pathJoinRadius>0&&(console.time("joining nearby paths"),n=(0,C.merge)(n,e.pathJoinRadius),console.timeEnd("joining nearby paths")),n=n.map(c=>c.map(u=>D(u,r.stepsPerMm))),console.time("planning pen motions");let s=Ve(n,{penUpPos:r.penPctToPos(e.penUpHeight),penDownPos:r.penPctToPos(e.penDownHeight),penDownProfile:{acceleration:e.penDownAcceleration*r.stepsPerMm,maximumVelocity:e.penDownMaxVelocity*r.stepsPerMm,corneringFactor:e.penDownCorneringFactor*r.stepsPerMm},penUpProfile:{acceleration:e.penUpAcceleration*r.stepsPerMm,maximumVelocity:e.penUpMaxVelocity*r.stepsPerMm,corneringFactor:0},penDropDuration:e.penDropDuration,penLiftDuration:e.penLiftDuration},D(e.penHome,r.stepsPerMm));return console.timeEnd("planning pen motions"),s}self.addEventListener("message",t=>{let{paths:e,planOptions:n}=t.data,r=Xe(e,n);console.time("serializing");let s=r.serialize();console.timeEnd("serializing"),self.postMessage(s)});})();
//# sourceMappingURL=background-planner.js.map