rectpackbin 0.0.5 → 0.0.7

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/build.js ADDED
@@ -0,0 +1,11 @@
1
+ // build.js
2
+ const esbuild = require("esbuild");
3
+
4
+ esbuild.build({
5
+ entryPoints: ["index.js"], // entry point of your app
6
+ bundle: true,
7
+ minify: true,
8
+ outfile: "dist/rectpackbin.min.js",
9
+ platform: "browser", // or "browser" for front-end projects
10
+ target: "es2017", // adjust based on your target environment
11
+ }).catch(() => process.exit(1));
@@ -0,0 +1 @@
1
+ var Lt=Object.create;var R=Object.defineProperty;var Nt=Object.getOwnPropertyDescriptor;var It=Object.getOwnPropertyNames;var qt=Object.getPrototypeOf,jt=Object.prototype.hasOwnProperty;var b=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports),zt=(n,t)=>{for(var s in t)R(n,s,{get:t[s],enumerable:!0})},St=(n,t,s,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of It(t))!jt.call(n,i)&&i!==s&&R(n,i,{get:()=>t[i],enumerable:!(e=Nt(t,i))||e.enumerable});return n};var Ot=(n,t,s)=>(s=n!=null?Lt(qt(n)):{},St(t||!n||!n.__esModule?R(s,"default",{value:n,enumerable:!0}):s,n)),Dt=n=>St(R({},"__esModule",{value:!0}),n);var K=b((Gs,kt)=>{var u=class{constructor(t,s){this.x=t,this.y=s}equals(t){return this.x===t.x&&this.y===t.y}toString(){return`P(${this.x}, ${this.y})`}distance(t){return Math.sqrt((this.x-t.x)**2+(this.y-t.y)**2)}distanceSquared(t){return(this.x-t.x)**2+(this.y-t.y)**2}},w=class n{constructor(t,s){if(!(t instanceof u)||!(s instanceof u))throw new Error("Start and end must be Point instances");this.start=t,this.end=s}equals(t){return t instanceof n?this.start.equals(t.start)&&this.end.equals(t.end):!1}toString(){return`S(${this.start}, ${this.end})`}get lengthSquared(){return this.start.distanceSquared(this.end)}get length(){return this.start.distance(this.end)}get top(){return Math.max(this.start.y,this.end.y)}get bottom(){return Math.min(this.start.y,this.end.y)}get right(){return Math.max(this.start.x,this.end.x)}get left(){return Math.min(this.start.x,this.end.x)}},$=class extends w{constructor(t,s){if(!(t instanceof u)||typeof s!="number")throw new Error("Start must be a Point and length must be a number");super(t,new u(t.x+s,t.y))}get length(){return this.end.x-this.start.x}},U=class extends w{constructor(t,s){if(!(t instanceof u)||typeof s!="number")throw new Error("Start must be a Point and length must be a number");super(t,new u(t.x,t.y+s))}get length(){return this.end.y-this.start.y}},W=class n{constructor(t,s,e,i,r=null){if(i<0||e<0)throw new Error("Height and width must be non-negative");this.width=e,this.height=i,this.x=t,this.y=s,this.rid=r}get bottom(){return this.y}get top(){return this.y+this.height}get left(){return this.x}get right(){return this.x+this.width}get cornerTopL(){return new u(this.left,this.top)}get cornerTopR(){return new u(this.right,this.top)}get cornerBotR(){return new u(this.right,this.bottom)}get cornerBotL(){return new u(this.left,this.bottom)}area(){return this.width*this.height}move(t,s){this.x=t,this.y=s}contains(t){return t.y>=this.y&&t.x>=this.x&&t.y+t.height<=this.y+this.height&&t.x+t.width<=this.x+this.width}intersects(t,s=!1){if(s){if(this.bottom>t.top||this.top<t.bottom||this.left>t.right||this.right<t.left)return!1}else if(this.bottom>=t.top||this.top<=t.bottom||this.left>=t.right||this.right<=t.left)return!1;return!0}intersection(t,s=!1){if(!this.intersects(t,s))return null;let e=Math.max(this.bottom,t.bottom),i=Math.max(this.left,t.left),r=Math.min(this.top,t.top),l=Math.min(this.right,t.right);return new n(i,e,l-i,r-e)}join(t){if(this.contains(t))return!0;if(t.contains(this))return this.x=t.x,this.y=t.y,this.width=t.width,this.height=t.height,!0;if(!this.intersects(t,!0))return!1;if(this.left===t.left&&this.width===t.width){let s=Math.min(this.bottom,t.bottom),e=Math.max(this.top,t.top);return this.y=s,this.height=e-s,!0}if(this.bottom===t.bottom&&this.height===t.height){let s=Math.min(this.left,t.left),e=Math.max(this.right,t.right);return this.x=s,this.width=e-s,!0}return!1}};kt.exports={Point:u,Segment:w,HSegment:$,VSegment:U,Rectangle:W}});var vt=b((Ts,Ft)=>{var{Rectangle:Et}=K(),H=class{constructor(t,s,e=!0,i=null){this.width=t,this.height=s,this.rot=e,this.rectangles=[],this.bid=i,this._surface=new Et(0,0,t,s),this.reset()}get length(){return this.rectangles.length}[Symbol.iterator](){return this.rectangles[Symbol.iterator]()}_fitsSurface(t,s){if(!(t>0&&s>0))throw new Error("Width and height must be positive");if(this.rot&&(t>this.width||s>this.height)){let e=t;t=s,s=e}return!(t>this.width||s>this.height)}getItem(t){return this.rectangles[t]}usedArea(){return this.rectangles.reduce((t,s)=>t+s.area(),0)}fitness(t,s,e=!1){throw new Error("Not implemented")}addRect(t,s,e=null){throw new Error("Not implemented")}rectList(){return this.rectangles.map(t=>[t.x,t.y,t.width,t.height,t.rid])}validatePacking(){let t=new Et(0,0,this.width,this.height);for(let s of this.rectangles)if(!t.contains(s))throw new Error("Rectangle placed outside surface");if(!(this.rectangles.length<=1)){for(let s=0;s<=this.rectangles.length-2;s++)for(let e=s+1;e<=this.rectangles.length-1;e++)if(this.rectangles[s].intersects(this.rectangles[e]))throw new Error("Rectangle collision detected")}}isEmpty(){return this.rectangles.length===0}reset(){this.rectangles=[]}};Ft.exports=H});var yt=b((As,Pt)=>{var{Rectangle:B}=K(),Ct=vt(),$t=n=>class extends n{constructor(t,s,e=!0,i=!0,...r){super(t,s,e,...r),this._merge=i,this._sections=[],this._add_section(new B(0,0,t,s,null))}_add_section(t){t.rid=0;let s=0;for(;this._merge&&this._sections&&s!==this._sections.length;)s=this._sections.length,this._sections=this._sections.filter(e=>!t.join(e));this._sections.push(t)}_split_horizontal(t,s,e){e<t.height&&this._add_section(new B(t.x,t.y+e,t.width,t.height-e)),s<t.width&&this._add_section(new B(t.x+s,t.y,t.width-s,e))}_split_vertical(t,s,e){e<t.height&&this._add_section(new B(t.x,t.y+e,s,t.height-e)),s<t.width&&this._add_section(new B(t.x+s,t.y,t.width-s,t.height))}_split(t,s,e){throw new Error("Not implemented")}_section_fitness(t,s,e){throw new Error("Not implemented")}_select_fittest_section(t,s){let e=this._sections.map(f=>{let p=this._section_fitness(f,t,s);return p!==null?[p,f,!1]:null}).filter(f=>f!==null),i=[];this.rot.rot===!0&&(i=this._sections.map(f=>{let p=this._section_fitness(f,s,t);return p!==null?[p,f,!0]:null}).filter(f=>f!==null));let r=[...e,...i];if(r.length===0)return[];let[l,h,M]=r.reduce((f,p)=>p[0]<f[0]?p:f);return[h,M]}addRect(t,s,e=null){if(!(t>0&&s>0))throw new Error("Width and height must be positive");let[i,r]=this._select_fittest_section(t,s);if(!i)return null;if(r){let h=t;t=s,s=h}this._sections=this._sections.filter(h=>h!==i),this._split(i,t,s);let l=new B(i.x,i.y,t,s,e);return this.rectangles.push(l),l}fitness(t,s){if(!(t>0&&s>0))throw new Error("Width and height must be positive");let[e,i]=this._select_fittest_section(t,s);return e?i?this._section_fitness(e,s,t):this._section_fitness(e,t,s):null}reset(){super.reset(),this._sections=[],this._add_section(new B(0,0,this.width,this.height))}},Ut=$t(Ct),a=class extends Ut{constructor(...t){super(...t)}},J=class extends a{_section_fitness(t,s,e){return s>t.width||e>t.height?null:t.area()-s*e}},x=n=>class extends n{constructor(...t){super(...t)}_section_fitness(t,s,e){return s>t.width||e>t.height?null:t.area()-s*e}},Q=class extends a{_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.max(t.width-s,t.height-e)}},g=n=>class extends n{constructor(...t){super(...t)}_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.max(t.width-s,t.height-e)}},X=class extends a{_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.min(t.width-s,t.height-e)}},m=n=>class extends n{constructor(...t){super(...t)}_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.min(t.width-s,t.height-e)}},S=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width<t.height?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Wt=S(a),Y=class extends Wt{constructor(...t){super(...t)}},O=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width>=t.height?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Kt=O(a),Z=class extends Kt{constructor(...t){super(...t)}},k=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width-s<t.height-e?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Ht=k(a),V=class extends Ht{constructor(...t){super(...t)}},E=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width-s<t.height-e?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Jt=E(a),tt=class extends Jt{constructor(...t){super(...t)}},F=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width-s<t.height-e?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Qt=F(a),st=class extends Qt{constructor(...t){super(...t)}},v=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return s*(t.height-e)>=e*(t.width-s)?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Xt=v(a),et=class extends Xt{constructor(...t){super(...t)}},Yt=S(m(a)),nt=class extends Yt{constructor(...t){super(...t)}},Zt=O(m(a)),it=class extends Zt{constructor(...t){super(...t)}},Vt=k(m(a)),rt=class extends Vt{constructor(...t){super(...t)}},ts=E(m(a)),ot=class extends ts{constructor(...t){super(...t)}},ss=F(m(a)),ct=class extends ss{constructor(...t){super(...t)}},es=v(m(a)),at=class extends es{constructor(...t){super(...t)}},ns=S(g(a)),lt=class extends ns{constructor(...t){super(...t)}},is=O(g(a)),ht=class extends is{constructor(...t){super(...t)}},rs=k(g(a)),ut=class extends rs{constructor(...t){super(...t)}},os=E(g(a)),ft=class extends os{constructor(...t){super(...t)}},cs=F(g(a)),_t=class extends cs{constructor(...t){super(...t)}},as=v(g(a)),dt=class extends as{constructor(...t){super(...t)}},ls=S(x(a)),pt=class extends ls{constructor(...t){super(...t)}},hs=O(x(a)),Bt=class extends hs{constructor(...t){super(...t)}},us=k(x(a)),xt=class extends us{constructor(...t){super(...t)}},fs=E(x(a)),gt=class extends fs{constructor(...t){super(...t)}},_s=F(x(a)),mt=class extends _s{constructor(...t){super(...t)}},ds=v(x(a)),wt=class extends ds{constructor(...t){super(...t)}};Pt.exports={Guillotine:a,GuillotineBaf:J,GuillotineBlsf:Q,GuillotineBssf:X,GuillotineSas:Y,GuillotineLas:Z,GuillotineSlas:V,GuillotineLlas:tt,GuillotineMaxas:st,GuillotineMinas:et,GuillotineBssfSas:nt,GuillotineBssfLas:it,GuillotineBssfSlas:rt,GuillotineBssfLlas:ot,GuillotineBssfMaxas:ct,GuillotineBssfMinas:at,GuillotineBlsfSas:lt,GuillotineBlsfLas:ht,GuillotineBlsfSlas:ut,GuillotineBlsfLlas:ft,GuillotineBlsfMaxas:_t,GuillotineBlsfMinas:dt,GuillotineBafSas:pt,GuillotineBafLas:Bt,GuillotineBafSlas:xt,GuillotineBafLlas:gt,GuillotineBafMaxas:mt,GuillotineBafMinas:wt}});var At=b((Ws,Tt)=>{var{GuillotineBssfSas:D}=yt();function ps(n,t){return Number(Math.ceil(n*Math.pow(10,t))/Math.pow(10,t))}var Gt=n=>[...n].sort((t,s)=>s[0]*s[1]-t[0]*t[1]),Bs=n=>[...n].sort((t,s)=>s[0]+s[1]-(t[0]+t[1])),xs=n=>[...n].sort((t,s)=>Math.abs(s[0]-s[1])-Math.abs(t[0]-t[1])),gs=n=>[...n].sort((t,s)=>{let[e,i]=[Math.min(t[0],t[1]),Math.max(t[0],t[1])],[r,l]=[Math.min(s[0],s[1]),Math.max(s[0],s[1])];return r-e||l-i}),ms=n=>[...n].sort((t,s)=>{let[e,i]=[Math.max(t[0],t[1]),Math.min(t[0],t[1])],[r,l]=[Math.max(s[0],s[1]),Math.min(s[0],s[1])];return r-e||l-i}),ws=n=>[...n].sort((t,s)=>s[0]/s[1]-t[0]/t[1]),Mt=n=>[...n],P={Online:0,Offline:1,properties:{0:{name:"Online"},1:{name:"Offline"}}},d={BNF:0,BFF:1,BBF:2,Global:3,properties:{0:{name:"BNF"},1:{name:"BFF"},2:{name:"BBF"},3:{name:"Global"}}},G=class{constructor(t,s,e,i,...r){this._width=t,this._height=s,this._count=e,this._packAlgo=i,this._algoArgs=r,this._refBin=null}_createBin(){return new this._packAlgo(this._width,this._height,...this._algoArgs)}isEmpty(){return this._count<1}fitness(t,s){return this._refBin||(this._refBin=this._createBin()),this._refBin.fitness(t,s)}fitsInside(t,s){return this._refBin||(this._refBin=this._createBin()),this._refBin._fitsSurface(t,s)}newBin(){return this._count>0?(this._count--,this._createBin()):null}},_=class{constructor({packAlgo:t=D,rotation:s=!0}){this._rotation=s,this._packAlgo=t,this.reset()}*[Symbol.iterator](){yield*[...this._closedBins,...this._openBins]}get length(){return this._closedBins.length+this._openBins.length}_newOpenBin(t=null,s=null,e=null){let i=new Set,r=null;for(let[l,h]of Object.entries(this._emptyBins))if(h.fitsInside(t,s)&&(r=h.newBin(),!!r)){this._openBins.push(r),h.isEmpty()&&i.add(l);break}return i.forEach(l=>delete this._emptyBins[l]),r}addBin(t,s,e=1,i={}){i.rot=this._rotation;let r=new G(t,s,e,this._packAlgo,i);this._emptyBins[this._binCount++]=r}rectList(){let t=[],s=0;for(let e of this){for(let i of e)t.push([s,i.x,i.y,i.width,i.height,i.rid]);s++}return t}binList(){return[...this].map(t=>[t.width,t.height])}validatePacking(){for(let t of this)t.validatePacking()}reset(){this._closedBins=[],this._openBins=[],this._emptyBins=new Map,this._binCount=0}},C=n=>class extends n{constructor({packAlgo:t=D,sortAlgo:s=Mt,rotation:e=!0}={}){super({packAlgo:t,rotation:e}),this._sortAlgo=s,this._availBins=[],this._availRect=[],this._sortedRect=[]}addBin(t,s,e=1,i={}){this._availBins.push([t,s,e,i])}addRect(t,s,e=null){this._availRect.push([t,s,e])}addRectOffline(t,s,e=null){this._availRect.push([t,s,e])}_isEverythingReady(){return this._availRect.length>0&&this._availBins.length>0}pack(){if(this.reset(),!!this._isEverythingReady()){for(let t=0;t<this._availBins.length;t++){let[s,e,i,r]=this._availBins[t];super.addBin(s,e,i,r)}this._sortedRect=this._sortAlgo(this._availRect);for(let t of this._sortedRect)super.addRect(...t)}}},ys=C(_),T=class extends ys{constructor(...t){super(...t)}},y=n=>class extends n{constructor(...t){super(...t)}addRect(t,s,e=null){for(;;){if(this._openBins.length===0&&!this._newOpenBin(t,s,e))return null;let i=this._openBins[0].addRect(t,s,e);if(i)return i;let r=this._openBins.shift();this._closedBins.push(r)}}},Rt=n=>class extends n{constructor(...t){super(...t)}addRect(t,s,e=null){for(let i of this._openBins){let r=i.addRect(t,s,e);if(r)return r}for(;;){let i=this._newOpenBin(t,s,e);if(!i)return null;let r=i.addRect(t,s,e);if(r)return r}}},bt=n=>class extends n{constructor(...t){super(...t)}addRect(t,s,e=null){let i=this._openBins.map(r=>[r.fitness(t,s),r]).filter(([r])=>r!==null);if(i.length>0){let[r,l]=i.reduce((h,M)=>M[0]<h[0]?M:h);return l.addRect(t,s,e),!0}for(;;){let r=this._newOpenBin(t,s,e);if(!r)return!1;if(r.addRect(t,s,e))return!0}}},Ms=C(y(_)),A=class extends Ms{constructor(...t){super(...t)}},Rs=C(Rt(_)),L=class extends Rs{constructor(...t){super(...t)}},bs=C(bt(_)),N=class extends bs{constructor(...t){super(...t)}},Ss=y(_),I=class extends Ss{constructor(...t){super(...t)}},Os=Rt(_),q=class extends Os{constructor(...t){super(...t)}},ks=bt(_),j=class extends ks{constructor(...t){super(...t)}},Es=y(T),z=class extends Es{constructor({packAlgo:t=D,rotation:s=!0}={}){super({packAlgo:t,sortAlgo:Mt,rotation:s})}_findBestFit(t){let s=Object.entries(this._sortedRect).map(([r,l])=>[t.fitness(l[0],l[1]),r]).filter(([r])=>r!==null);if(s.length===0)return null;let[e,i]=s.reduce((r,l)=>l[0]<r[0]?l:r);return i}_newOpenBin(t){let s=new Set,e=null;for(let[i,r]of Object.entries(this._emptyBins)){let l=!1;for(let h of Object.values(t))if(r.fitsInside(h[0],h[1])){l=!0;break}if(!l){s.add(i);continue}if(e=r.newBin(),!!e){this._openBins.push(e),r.isEmpty()&&s.add(i);break}}for(let i of s)delete this._emptyBins[i];return e}pack(){if(this.reset(),!!this._isEverythingReady())for(this._sortedRect=Object.fromEntries(this._sortAlgo(this._availRect).map((t,s)=>[s,t]));Object.keys(this._sortedRect).length>0;){let t=this._newOpenBin(this._sortedRect);if(!t)break;for(;;){let s=this._findBestFit(t);if(!s){let i=this._openBins.shift();this._closedBins.push(i);break}let e=this._sortedRect[s];delete this._sortedRect[s],y.prototype.addRect.call(this,...e)}}}};function Fs({mode:n=P.Offline,binAlgo:t=d.BBF,packAlgo:s=D,sortAlgo:e=Gt,rotation:i=!0}={}){let r=null;if(n===P.Online)switch(e=null,t){case d.BNF:r=I;break;case d.BFF:r=q;break;case d.BBF:r=j;break;default:throw new Error("Unsupported bin selection heuristic")}else if(n===P.Offline)switch(t){case d.BNF:r=A;break;case d.BFF:r=L;break;case d.BBF:r=N;break;case d.Global:r=z,e=null;break;default:throw new Error("Unsupported bin selection heuristic")}else throw new Error("Unknown packing mode");return e?new r({packAlgo:s,sortAlgo:e,rotation:i}):new r({packAlgo:s,rotation:i})}Tt.exports={float2dec:ps,SORT_AREA:Gt,SORT_PERI:Bs,SORT_DIFF:xs,SORT_SSIDE:gs,SORT_LSIDE:ms,SORT_RATIO:ws,SORT_NONE:Mt,BinFactory:G,PackerBNFMixin:y,PackerBFFMixin:Rt,PackerBBFMixin:bt,PackerOnline:_,Packer:T,PackerBNF:A,PackerBFF:L,PackerBBF:N,PackerOnlineBNF:I,PackerOnlineBFF:q,PackerOnlineBBF:j,PackerGlobal:z,PackingMode:P,PackingBin:d,newPacker:Fs}});var vs={};zt(vs,{BinFactory:()=>c.BinFactory,GuillotineBafLas:()=>o.GuillotineBafLas,GuillotineBafLlas:()=>o.GuillotineBafLlas,GuillotineBafMaxas:()=>o.GuillotineBafMaxas,GuillotineBafMinas:()=>o.GuillotineBafMinas,GuillotineBafSas:()=>o.GuillotineBafSas,GuillotineBafSlas:()=>o.GuillotineBafSlas,GuillotineBlsfLas:()=>o.GuillotineBlsfLas,GuillotineBlsfLlas:()=>o.GuillotineBlsfLlas,GuillotineBlsfMaxas:()=>o.GuillotineBlsfMaxas,GuillotineBlsfMinas:()=>o.GuillotineBlsfMinas,GuillotineBlsfSas:()=>o.GuillotineBlsfSas,GuillotineBlsfSlas:()=>o.GuillotineBlsfSlas,GuillotineBssfLas:()=>o.GuillotineBssfLas,GuillotineBssfLlas:()=>o.GuillotineBssfLlas,GuillotineBssfMaxas:()=>o.GuillotineBssfMaxas,GuillotineBssfMinas:()=>o.GuillotineBssfMinas,GuillotineBssfSas:()=>o.GuillotineBssfSas,GuillotineBssfSlas:()=>o.GuillotineBssfSlas,PackerBBFMixin:()=>c.PackerBBFMixin,PackerBFFMixin:()=>c.PackerBFFMixin,PackerBNFMixin:()=>c.PackerBNFMixin,PackerOnline:()=>c.PackerOnline,PackingBin:()=>c.PackingBin,PackingMode:()=>c.PackingMode,SORT_AREA:()=>c.SORT_AREA,SORT_DIFF:()=>c.SORT_DIFF,SORT_LSIDE:()=>c.SORT_LSIDE,SORT_NONE:()=>c.SORT_NONE,SORT_PERI:()=>c.SORT_PERI,SORT_RATIO:()=>c.SORT_RATIO,SORT_SSIDE:()=>c.SORT_SSIDE,float2dec:()=>c.float2dec,newPacker:()=>c.newPacker});module.exports=Dt(vs);var o=Ot(yt()),c=Ot(At());0&&(module.exports={BinFactory,GuillotineBafLas,GuillotineBafLlas,GuillotineBafMaxas,GuillotineBafMinas,GuillotineBafSas,GuillotineBafSlas,GuillotineBlsfLas,GuillotineBlsfLlas,GuillotineBlsfMaxas,GuillotineBlsfMinas,GuillotineBlsfSas,GuillotineBlsfSlas,GuillotineBssfLas,GuillotineBssfLlas,GuillotineBssfMaxas,GuillotineBssfMinas,GuillotineBssfSas,GuillotineBssfSlas,PackerBBFMixin,PackerBFFMixin,PackerBNFMixin,PackerOnline,PackingBin,PackingMode,SORT_AREA,SORT_DIFF,SORT_LSIDE,SORT_NONE,SORT_PERI,SORT_RATIO,SORT_SSIDE,float2dec,newPacker});
@@ -0,0 +1 @@
1
+ (()=>{var At=Object.create;var bt=Object.defineProperty;var Lt=Object.getOwnPropertyDescriptor;var Nt=Object.getOwnPropertyNames;var It=Object.getPrototypeOf,qt=Object.prototype.hasOwnProperty;var R=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var jt=(n,t,s,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Nt(t))!qt.call(n,i)&&i!==s&&bt(n,i,{get:()=>t[i],enumerable:!(e=Lt(t,i))||e.enumerable});return n};var St=(n,t,s)=>(s=n!=null?At(It(n)):{},jt(t||!n||!n.__esModule?bt(s,"default",{value:n,enumerable:!0}):s,n));var W=R((Fs,kt)=>{var u=class{constructor(t,s){this.x=t,this.y=s}equals(t){return this.x===t.x&&this.y===t.y}toString(){return`P(${this.x}, ${this.y})`}distance(t){return Math.sqrt((this.x-t.x)**2+(this.y-t.y)**2)}distanceSquared(t){return(this.x-t.x)**2+(this.y-t.y)**2}},w=class n{constructor(t,s){if(!(t instanceof u)||!(s instanceof u))throw new Error("Start and end must be Point instances");this.start=t,this.end=s}equals(t){return t instanceof n?this.start.equals(t.start)&&this.end.equals(t.end):!1}toString(){return`S(${this.start}, ${this.end})`}get lengthSquared(){return this.start.distanceSquared(this.end)}get length(){return this.start.distance(this.end)}get top(){return Math.max(this.start.y,this.end.y)}get bottom(){return Math.min(this.start.y,this.end.y)}get right(){return Math.max(this.start.x,this.end.x)}get left(){return Math.min(this.start.x,this.end.x)}},C=class extends w{constructor(t,s){if(!(t instanceof u)||typeof s!="number")throw new Error("Start must be a Point and length must be a number");super(t,new u(t.x+s,t.y))}get length(){return this.end.x-this.start.x}},$=class extends w{constructor(t,s){if(!(t instanceof u)||typeof s!="number")throw new Error("Start must be a Point and length must be a number");super(t,new u(t.x,t.y+s))}get length(){return this.end.y-this.start.y}},U=class n{constructor(t,s,e,i,r=null){if(i<0||e<0)throw new Error("Height and width must be non-negative");this.width=e,this.height=i,this.x=t,this.y=s,this.rid=r}get bottom(){return this.y}get top(){return this.y+this.height}get left(){return this.x}get right(){return this.x+this.width}get cornerTopL(){return new u(this.left,this.top)}get cornerTopR(){return new u(this.right,this.top)}get cornerBotR(){return new u(this.right,this.bottom)}get cornerBotL(){return new u(this.left,this.bottom)}area(){return this.width*this.height}move(t,s){this.x=t,this.y=s}contains(t){return t.y>=this.y&&t.x>=this.x&&t.y+t.height<=this.y+this.height&&t.x+t.width<=this.x+this.width}intersects(t,s=!1){if(s){if(this.bottom>t.top||this.top<t.bottom||this.left>t.right||this.right<t.left)return!1}else if(this.bottom>=t.top||this.top<=t.bottom||this.left>=t.right||this.right<=t.left)return!1;return!0}intersection(t,s=!1){if(!this.intersects(t,s))return null;let e=Math.max(this.bottom,t.bottom),i=Math.max(this.left,t.left),r=Math.min(this.top,t.top),c=Math.min(this.right,t.right);return new n(i,e,c-i,r-e)}join(t){if(this.contains(t))return!0;if(t.contains(this))return this.x=t.x,this.y=t.y,this.width=t.width,this.height=t.height,!0;if(!this.intersects(t,!0))return!1;if(this.left===t.left&&this.width===t.width){let s=Math.min(this.bottom,t.bottom),e=Math.max(this.top,t.top);return this.y=s,this.height=e-s,!0}if(this.bottom===t.bottom&&this.height===t.height){let s=Math.min(this.left,t.left),e=Math.max(this.right,t.right);return this.x=s,this.width=e-s,!0}return!1}};kt.exports={Point:u,Segment:w,HSegment:C,VSegment:$,Rectangle:U}});var Ft=R((vs,Et)=>{var{Rectangle:Ot}=W(),K=class{constructor(t,s,e=!0,i=null){this.width=t,this.height=s,this.rot=e,this.rectangles=[],this.bid=i,this._surface=new Ot(0,0,t,s),this.reset()}get length(){return this.rectangles.length}[Symbol.iterator](){return this.rectangles[Symbol.iterator]()}_fitsSurface(t,s){if(!(t>0&&s>0))throw new Error("Width and height must be positive");if(this.rot&&(t>this.width||s>this.height)){let e=t;t=s,s=e}return!(t>this.width||s>this.height)}getItem(t){return this.rectangles[t]}usedArea(){return this.rectangles.reduce((t,s)=>t+s.area(),0)}fitness(t,s,e=!1){throw new Error("Not implemented")}addRect(t,s,e=null){throw new Error("Not implemented")}rectList(){return this.rectangles.map(t=>[t.x,t.y,t.width,t.height,t.rid])}validatePacking(){let t=new Ot(0,0,this.width,this.height);for(let s of this.rectangles)if(!t.contains(s))throw new Error("Rectangle placed outside surface");if(!(this.rectangles.length<=1)){for(let s=0;s<=this.rectangles.length-2;s++)for(let e=s+1;e<=this.rectangles.length-1;e++)if(this.rectangles[s].intersects(this.rectangles[e]))throw new Error("Rectangle collision detected")}}isEmpty(){return this.rectangles.length===0}reset(){this.rectangles=[]}};Et.exports=K});var wt=R((Ps,vt)=>{var{Rectangle:B}=W(),zt=Ft(),Dt=n=>class extends n{constructor(t,s,e=!0,i=!0,...r){super(t,s,e,...r),this._merge=i,this._sections=[],this._add_section(new B(0,0,t,s,null))}_add_section(t){t.rid=0;let s=0;for(;this._merge&&this._sections&&s!==this._sections.length;)s=this._sections.length,this._sections=this._sections.filter(e=>!t.join(e));this._sections.push(t)}_split_horizontal(t,s,e){e<t.height&&this._add_section(new B(t.x,t.y+e,t.width,t.height-e)),s<t.width&&this._add_section(new B(t.x+s,t.y,t.width-s,e))}_split_vertical(t,s,e){e<t.height&&this._add_section(new B(t.x,t.y+e,s,t.height-e)),s<t.width&&this._add_section(new B(t.x+s,t.y,t.width-s,t.height))}_split(t,s,e){throw new Error("Not implemented")}_section_fitness(t,s,e){throw new Error("Not implemented")}_select_fittest_section(t,s){let e=this._sections.map(f=>{let p=this._section_fitness(f,t,s);return p!==null?[p,f,!1]:null}).filter(f=>f!==null),i=[];this.rot.rot===!0&&(i=this._sections.map(f=>{let p=this._section_fitness(f,s,t);return p!==null?[p,f,!0]:null}).filter(f=>f!==null));let r=[...e,...i];if(r.length===0)return[];let[c,h,M]=r.reduce((f,p)=>p[0]<f[0]?p:f);return[h,M]}addRect(t,s,e=null){if(!(t>0&&s>0))throw new Error("Width and height must be positive");let[i,r]=this._select_fittest_section(t,s);if(!i)return null;if(r){let h=t;t=s,s=h}this._sections=this._sections.filter(h=>h!==i),this._split(i,t,s);let c=new B(i.x,i.y,t,s,e);return this.rectangles.push(c),c}fitness(t,s){if(!(t>0&&s>0))throw new Error("Width and height must be positive");let[e,i]=this._select_fittest_section(t,s);return e?i?this._section_fitness(e,s,t):this._section_fitness(e,t,s):null}reset(){super.reset(),this._sections=[],this._add_section(new B(0,0,this.width,this.height))}},Ct=Dt(zt),o=class extends Ct{constructor(...t){super(...t)}},H=class extends o{_section_fitness(t,s,e){return s>t.width||e>t.height?null:t.area()-s*e}},x=n=>class extends n{constructor(...t){super(...t)}_section_fitness(t,s,e){return s>t.width||e>t.height?null:t.area()-s*e}},J=class extends o{_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.max(t.width-s,t.height-e)}},g=n=>class extends n{constructor(...t){super(...t)}_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.max(t.width-s,t.height-e)}},Q=class extends o{_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.min(t.width-s,t.height-e)}},m=n=>class extends n{constructor(...t){super(...t)}_section_fitness(t,s,e){return s>t.width||e>t.height?null:Math.min(t.width-s,t.height-e)}},b=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width<t.height?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},$t=b(o),X=class extends $t{constructor(...t){super(...t)}},S=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width>=t.height?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Ut=S(o),Y=class extends Ut{constructor(...t){super(...t)}},k=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width-s<t.height-e?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Wt=k(o),Z=class extends Wt{constructor(...t){super(...t)}},O=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width-s<t.height-e?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Kt=O(o),V=class extends Kt{constructor(...t){super(...t)}},E=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return t.width-s<t.height-e?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Ht=E(o),tt=class extends Ht{constructor(...t){super(...t)}},F=n=>class extends n{constructor(...t){super(...t)}_split(t,s,e){return s*(t.height-e)>=e*(t.width-s)?this._split_horizontal(t,s,e):this._split_vertical(t,s,e)}},Jt=F(o),st=class extends Jt{constructor(...t){super(...t)}},Qt=b(m(o)),et=class extends Qt{constructor(...t){super(...t)}},Xt=S(m(o)),nt=class extends Xt{constructor(...t){super(...t)}},Yt=k(m(o)),it=class extends Yt{constructor(...t){super(...t)}},Zt=O(m(o)),rt=class extends Zt{constructor(...t){super(...t)}},Vt=E(m(o)),ot=class extends Vt{constructor(...t){super(...t)}},ts=F(m(o)),ct=class extends ts{constructor(...t){super(...t)}},ss=b(g(o)),at=class extends ss{constructor(...t){super(...t)}},es=S(g(o)),lt=class extends es{constructor(...t){super(...t)}},ns=k(g(o)),ht=class extends ns{constructor(...t){super(...t)}},is=O(g(o)),ut=class extends is{constructor(...t){super(...t)}},rs=E(g(o)),ft=class extends rs{constructor(...t){super(...t)}},os=F(g(o)),dt=class extends os{constructor(...t){super(...t)}},cs=b(x(o)),_t=class extends cs{constructor(...t){super(...t)}},as=S(x(o)),pt=class extends as{constructor(...t){super(...t)}},ls=k(x(o)),Bt=class extends ls{constructor(...t){super(...t)}},hs=O(x(o)),xt=class extends hs{constructor(...t){super(...t)}},us=E(x(o)),gt=class extends us{constructor(...t){super(...t)}},fs=F(x(o)),mt=class extends fs{constructor(...t){super(...t)}};vt.exports={Guillotine:o,GuillotineBaf:H,GuillotineBlsf:J,GuillotineBssf:Q,GuillotineSas:X,GuillotineLas:Y,GuillotineSlas:Z,GuillotineLlas:V,GuillotineMaxas:tt,GuillotineMinas:st,GuillotineBssfSas:et,GuillotineBssfLas:nt,GuillotineBssfSlas:it,GuillotineBssfLlas:rt,GuillotineBssfMaxas:ot,GuillotineBssfMinas:ct,GuillotineBlsfSas:at,GuillotineBlsfLas:lt,GuillotineBlsfSlas:ht,GuillotineBlsfLlas:ut,GuillotineBlsfMaxas:ft,GuillotineBlsfMinas:dt,GuillotineBafSas:_t,GuillotineBafLas:pt,GuillotineBafSlas:Bt,GuillotineBafLlas:xt,GuillotineBafMaxas:gt,GuillotineBafMinas:mt}});var Tt=R((Cs,Gt)=>{var{GuillotineBssfSas:z}=wt();function ds(n,t){return Number(Math.ceil(n*Math.pow(10,t))/Math.pow(10,t))}var Pt=n=>[...n].sort((t,s)=>s[0]*s[1]-t[0]*t[1]),_s=n=>[...n].sort((t,s)=>s[0]+s[1]-(t[0]+t[1])),ps=n=>[...n].sort((t,s)=>Math.abs(s[0]-s[1])-Math.abs(t[0]-t[1])),Bs=n=>[...n].sort((t,s)=>{let[e,i]=[Math.min(t[0],t[1]),Math.max(t[0],t[1])],[r,c]=[Math.min(s[0],s[1]),Math.max(s[0],s[1])];return r-e||c-i}),xs=n=>[...n].sort((t,s)=>{let[e,i]=[Math.max(t[0],t[1]),Math.min(t[0],t[1])],[r,c]=[Math.max(s[0],s[1]),Math.min(s[0],s[1])];return r-e||c-i}),gs=n=>[...n].sort((t,s)=>s[0]/s[1]-t[0]/t[1]),yt=n=>[...n],v={Online:0,Offline:1,properties:{0:{name:"Online"},1:{name:"Offline"}}},_={BNF:0,BFF:1,BBF:2,Global:3,properties:{0:{name:"BNF"},1:{name:"BFF"},2:{name:"BBF"},3:{name:"Global"}}},P=class{constructor(t,s,e,i,...r){this._width=t,this._height=s,this._count=e,this._packAlgo=i,this._algoArgs=r,this._refBin=null}_createBin(){return new this._packAlgo(this._width,this._height,...this._algoArgs)}isEmpty(){return this._count<1}fitness(t,s){return this._refBin||(this._refBin=this._createBin()),this._refBin.fitness(t,s)}fitsInside(t,s){return this._refBin||(this._refBin=this._createBin()),this._refBin._fitsSurface(t,s)}newBin(){return this._count>0?(this._count--,this._createBin()):null}},d=class{constructor({packAlgo:t=z,rotation:s=!0}){this._rotation=s,this._packAlgo=t,this.reset()}*[Symbol.iterator](){yield*[...this._closedBins,...this._openBins]}get length(){return this._closedBins.length+this._openBins.length}_newOpenBin(t=null,s=null,e=null){let i=new Set,r=null;for(let[c,h]of Object.entries(this._emptyBins))if(h.fitsInside(t,s)&&(r=h.newBin(),!!r)){this._openBins.push(r),h.isEmpty()&&i.add(c);break}return i.forEach(c=>delete this._emptyBins[c]),r}addBin(t,s,e=1,i={}){i.rot=this._rotation;let r=new P(t,s,e,this._packAlgo,i);this._emptyBins[this._binCount++]=r}rectList(){let t=[],s=0;for(let e of this){for(let i of e)t.push([s,i.x,i.y,i.width,i.height,i.rid]);s++}return t}binList(){return[...this].map(t=>[t.width,t.height])}validatePacking(){for(let t of this)t.validatePacking()}reset(){this._closedBins=[],this._openBins=[],this._emptyBins=new Map,this._binCount=0}},D=n=>class extends n{constructor({packAlgo:t=z,sortAlgo:s=yt,rotation:e=!0}={}){super({packAlgo:t,rotation:e}),this._sortAlgo=s,this._availBins=[],this._availRect=[],this._sortedRect=[]}addBin(t,s,e=1,i={}){this._availBins.push([t,s,e,i])}addRect(t,s,e=null){this._availRect.push([t,s,e])}addRectOffline(t,s,e=null){this._availRect.push([t,s,e])}_isEverythingReady(){return this._availRect.length>0&&this._availBins.length>0}pack(){if(this.reset(),!!this._isEverythingReady()){for(let t=0;t<this._availBins.length;t++){let[s,e,i,r]=this._availBins[t];super.addBin(s,e,i,r)}this._sortedRect=this._sortAlgo(this._availRect);for(let t of this._sortedRect)super.addRect(...t)}}},ms=D(d),G=class extends ms{constructor(...t){super(...t)}},y=n=>class extends n{constructor(...t){super(...t)}addRect(t,s,e=null){for(;;){if(this._openBins.length===0&&!this._newOpenBin(t,s,e))return null;let i=this._openBins[0].addRect(t,s,e);if(i)return i;let r=this._openBins.shift();this._closedBins.push(r)}}},Mt=n=>class extends n{constructor(...t){super(...t)}addRect(t,s,e=null){for(let i of this._openBins){let r=i.addRect(t,s,e);if(r)return r}for(;;){let i=this._newOpenBin(t,s,e);if(!i)return null;let r=i.addRect(t,s,e);if(r)return r}}},Rt=n=>class extends n{constructor(...t){super(...t)}addRect(t,s,e=null){let i=this._openBins.map(r=>[r.fitness(t,s),r]).filter(([r])=>r!==null);if(i.length>0){let[r,c]=i.reduce((h,M)=>M[0]<h[0]?M:h);return c.addRect(t,s,e),!0}for(;;){let r=this._newOpenBin(t,s,e);if(!r)return!1;if(r.addRect(t,s,e))return!0}}},ws=D(y(d)),T=class extends ws{constructor(...t){super(...t)}},ys=D(Mt(d)),A=class extends ys{constructor(...t){super(...t)}},Ms=D(Rt(d)),L=class extends Ms{constructor(...t){super(...t)}},Rs=y(d),N=class extends Rs{constructor(...t){super(...t)}},bs=Mt(d),I=class extends bs{constructor(...t){super(...t)}},Ss=Rt(d),q=class extends Ss{constructor(...t){super(...t)}},ks=y(G),j=class extends ks{constructor({packAlgo:t=z,rotation:s=!0}={}){super({packAlgo:t,sortAlgo:yt,rotation:s})}_findBestFit(t){let s=Object.entries(this._sortedRect).map(([r,c])=>[t.fitness(c[0],c[1]),r]).filter(([r])=>r!==null);if(s.length===0)return null;let[e,i]=s.reduce((r,c)=>c[0]<r[0]?c:r);return i}_newOpenBin(t){let s=new Set,e=null;for(let[i,r]of Object.entries(this._emptyBins)){let c=!1;for(let h of Object.values(t))if(r.fitsInside(h[0],h[1])){c=!0;break}if(!c){s.add(i);continue}if(e=r.newBin(),!!e){this._openBins.push(e),r.isEmpty()&&s.add(i);break}}for(let i of s)delete this._emptyBins[i];return e}pack(){if(this.reset(),!!this._isEverythingReady())for(this._sortedRect=Object.fromEntries(this._sortAlgo(this._availRect).map((t,s)=>[s,t]));Object.keys(this._sortedRect).length>0;){let t=this._newOpenBin(this._sortedRect);if(!t)break;for(;;){let s=this._findBestFit(t);if(!s){let i=this._openBins.shift();this._closedBins.push(i);break}let e=this._sortedRect[s];delete this._sortedRect[s],y.prototype.addRect.call(this,...e)}}}};function Os({mode:n=v.Offline,binAlgo:t=_.BBF,packAlgo:s=z,sortAlgo:e=Pt,rotation:i=!0}={}){let r=null;if(n===v.Online)switch(e=null,t){case _.BNF:r=N;break;case _.BFF:r=I;break;case _.BBF:r=q;break;default:throw new Error("Unsupported bin selection heuristic")}else if(n===v.Offline)switch(t){case _.BNF:r=T;break;case _.BFF:r=A;break;case _.BBF:r=L;break;case _.Global:r=j,e=null;break;default:throw new Error("Unsupported bin selection heuristic")}else throw new Error("Unknown packing mode");return e?new r({packAlgo:s,sortAlgo:e,rotation:i}):new r({packAlgo:s,rotation:i})}Gt.exports={float2dec:ds,SORT_AREA:Pt,SORT_PERI:_s,SORT_DIFF:ps,SORT_SSIDE:Bs,SORT_LSIDE:xs,SORT_RATIO:gs,SORT_NONE:yt,BinFactory:P,PackerBNFMixin:y,PackerBFFMixin:Mt,PackerBBFMixin:Rt,PackerOnline:d,Packer:G,PackerBNF:T,PackerBFF:A,PackerBBF:L,PackerOnlineBNF:N,PackerOnlineBFF:I,PackerOnlineBBF:q,PackerGlobal:j,PackingMode:v,PackingBin:_,newPacker:Os}});var l=St(wt()),a=St(Tt());window.newPacker=a.newPacker;window.PackingMode=a.PackingMode;window.PackingBin=a.PackingBin;window.SORT_AREA=a.SORT_AREA;window.GuillotineBssfSas=l.GuillotineBssfSas;})();
package/index.js CHANGED
@@ -13,15 +13,11 @@ import {
13
13
 
14
14
 
15
15
 
16
- // packer.js - constants
17
- import {
18
- SORT_AREA, SORT_PERI, SORT_DIFF, SORT_SSIDE,
19
- SORT_LSIDE, SORT_RATIO, SORT_NONE
20
- } from './src/rectpack/packer.js';
16
+
21
17
 
22
18
  // packer.js - classes and functions
23
19
  import {
24
- float2dec,
20
+ float2dec,
25
21
  SORT_AREA,
26
22
  SORT_PERI,
27
23
  SORT_DIFF,
@@ -55,6 +51,12 @@ export {
55
51
 
56
52
 
57
53
 
54
+ import {
55
+ MaxRectsBl
56
+ } from './src/rectpack/maxrects.js'
57
+
58
+
59
+ export {MaxRectsBl}
58
60
 
59
61
 
60
62
  // packer.js - classes and functions
@@ -77,4 +79,12 @@ export {
77
79
  newPacker
78
80
  }
79
81
 
80
-
82
+ window.newPacker = newPacker;
83
+
84
+
85
+ window.PackingMode = PackingMode;
86
+ window.PackingBin = PackingBin;
87
+ window.SORT_AREA = SORT_AREA;
88
+ window.GuillotineBssfSas = GuillotineBssfSas;
89
+
90
+
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "rectpackbin",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "A helpful description of your package",
5
5
  "main": "src/index.js",
6
6
  "module": "commonjs",
7
7
  "scripts": {
8
8
  "test:single": "jest -t default_options test/packer/packer.test.js",
9
- "test": "jest"
9
+ "test": "jest test/waste/waste.test.js",
10
+ "build": "npx esbuild index.js --bundle --minify --platform=browser --outfile=dist/rectpackbin.min.js"
11
+
10
12
  },
11
13
  "repository": {
12
14
  "type": "git",
@@ -42,6 +42,12 @@ import {
42
42
  PackerGlobal,
43
43
  } from './packer.js';
44
44
 
45
+ import {
46
+ MaxRectsBl
47
+ } from './maxrects.js'
48
+
49
+ export {MaxRectsBl}
50
+
45
51
  // guillotine.js
46
52
  export {
47
53
  GuillotineBssfSas, GuillotineBssfLas,
@@ -0,0 +1,288 @@
1
+ const { Rectangle } = require('./geometry');
2
+ const PackingAlgorithm = require('./packAlgo');
3
+
4
+ class MaxRects extends PackingAlgorithm {
5
+ /**
6
+ * Implementation of MaxRects packing algorithm
7
+ * For a more detailed explanation of the algorithm used, see:
8
+ * Jukka Jylanki - A Thousand Ways to Pack the Bin (February 27, 2010)
9
+ * @param {number} width - Surface width
10
+ * @param {number} height - Surface height
11
+ * @param {boolean} rot - Enable rotation
12
+ */
13
+ constructor(width, height, rot = true, ...args) {
14
+ super(width, height, rot, ...args);
15
+ this._max_rects = [new Rectangle(0, 0, width, height)];
16
+ }
17
+
18
+ /**
19
+ * Calculate fitness for rectangle placement in a maximal rectangle
20
+ * @param {Rectangle} max_rect - Destination maximal rectangle
21
+ * @param {number} width - Rectangle width
22
+ * @param {number} height - Rectangle height
23
+ * @returns {number|null} Fitness value or null if placement impossible
24
+ */
25
+ _rect_fitness(max_rect, width, height) {
26
+ if (width <= max_rect.width && height <= max_rect.height) {
27
+ return 0;
28
+ }
29
+ return null;
30
+ }
31
+
32
+ /**
33
+ * Find maximal rectangle with best fitness for placing a rectangle
34
+ * @param {number} w - Rectangle width
35
+ * @param {number} h - Rectangle height
36
+ * @returns {[Rectangle, Rectangle]} Placed rectangle and maximal rectangle where it was placed
37
+ */
38
+ _select_position(w, h) {
39
+ if (!this._max_rects.length) {
40
+ return [null, null];
41
+ }
42
+
43
+ // Normal rectangle
44
+ let fitn = this._max_rects
45
+ .map(m => {
46
+ const fitness = this._rect_fitness(m, w, h);
47
+ return fitness !== null ? [fitness, w, h, m] : null;
48
+ })
49
+ .filter(x => x !== null);
50
+
51
+ // Rotated rectangle
52
+ let fitr = [];
53
+ if (this.rot) {
54
+ fitr = this._max_rects
55
+ .map(m => {
56
+ const fitness = this._rect_fitness(m, h, w);
57
+ return fitness !== null ? [fitness, h, w, m] : null;
58
+ })
59
+ .filter(x => x !== null);
60
+ }
61
+
62
+ const fit = [...fitn, ...fitr];
63
+ if (fit.length === 0) {
64
+ return [null, null];
65
+ }
66
+
67
+ const [_, width, height, m] = fit.reduce((min, curr) =>
68
+ curr[0] < min[0] ? curr : min
69
+ );
70
+
71
+ return [new Rectangle(m.x, m.y, width, height), m];
72
+ }
73
+
74
+ /**
75
+ * Generate new maximal rectangles after placing a rectangle
76
+ * @param {Rectangle} m - Maximal rectangle
77
+ * @param {Rectangle} r - Placed rectangle
78
+ * @returns {Rectangle[]} New maximal rectangles
79
+ */
80
+ _generate_splits(m, r) {
81
+ const new_rects = [];
82
+
83
+ if (r.x > m.x) {
84
+ new_rects.push(new Rectangle(m.x, m.y, r.x - m.x, m.height));
85
+ }
86
+ if (r.x + r.width < m.x + m.width) {
87
+ new_rects.push(new Rectangle(r.x + r.width, m.y, (m.x + m.width) - (r.x + r.width), m.height));
88
+ }
89
+ if (r.y + r.height < m.y + m.height) {
90
+ new_rects.push(new Rectangle(m.x, r.y + r.height, m.width, (m.y + m.height) - (r.y + r.height)));
91
+ }
92
+ if (r.y > m.y) {
93
+ new_rects.push(new Rectangle(m.x, m.y, m.width, r.y - m.y));
94
+ }
95
+
96
+ return new_rects;
97
+ }
98
+
99
+ /**
100
+ * Split all maximal rectangles intersecting with the placed rectangle
101
+ * @param {Rectangle} rect - Placed rectangle
102
+ */
103
+ _split(rect) {
104
+ const max_rects = [];
105
+
106
+ for (const r of this._max_rects) {
107
+ if (r.intersects(rect)) {
108
+ max_rects.push(...this._generate_splits(r, rect));
109
+ } else {
110
+ max_rects.push(r);
111
+ }
112
+ }
113
+
114
+ this._max_rects = max_rects;
115
+ }
116
+
117
+ /**
118
+ * Remove maximal rectangles contained by others
119
+ */
120
+ _remove_duplicates() {
121
+ const contained = new Set();
122
+
123
+ for (let i = 0; i < this._max_rects.length; i++) {
124
+ for (let j = i + 1; j < this._max_rects.length; j++) {
125
+ const m1 = this._max_rects[i];
126
+ const m2 = this._max_rects[j];
127
+
128
+ if (m1.contains(m2)) {
129
+ contained.add(m2);
130
+ } else if (m2.contains(m1)) {
131
+ contained.add(m1);
132
+ }
133
+ }
134
+ }
135
+
136
+ this._max_rects = this._max_rects.filter(m => !contained.has(m));
137
+ }
138
+
139
+ /**
140
+ * Calculate fitness for given dimensions
141
+ * @param {number} width - Rectangle width
142
+ * @param {number} height - Rectangle height
143
+ * @returns {number|null} Fitness value or null if placement impossible
144
+ */
145
+ fitness(width, height) {
146
+ if (!(width > 0 && height > 0)) {
147
+ throw new Error("Width and height must be positive");
148
+ }
149
+
150
+ const [rect, max_rect] = this._select_position(width, height);
151
+ if (!rect) {
152
+ return null;
153
+ }
154
+
155
+ return this._rect_fitness(max_rect, rect.width, rect.height);
156
+ }
157
+
158
+ /**
159
+ * Add rectangle of width x height dimensions
160
+ * @param {number} width - Rectangle width
161
+ * @param {number} height - Rectangle height
162
+ * @param {string|number} rid - Optional rectangle ID
163
+ * @returns {Rectangle|null} Placed rectangle or null if placement failed
164
+ */
165
+ addRect(width, height, rid = null) {
166
+ if (!(width > 0 && height > 0)) {
167
+ throw new Error("Width and height must be positive");
168
+ }
169
+
170
+ const [rect, _] = this._select_position(width, height);
171
+ if (!rect) {
172
+ return null;
173
+ }
174
+
175
+ this._split(rect);
176
+ this._remove_duplicates();
177
+
178
+ rect.rid = rid;
179
+ this.rectangles.push(rect);
180
+ return rect;
181
+ }
182
+
183
+ /**
184
+ * Reset the algorithm state
185
+ */
186
+ reset() {
187
+ super.reset();
188
+ this._max_rects = [new Rectangle(0, 0, this.width, this.height)];
189
+ }
190
+ }
191
+
192
+ class MaxRectsBl extends MaxRects {
193
+ /**
194
+ * Select position where the y coordinate of the top of the rectangle is lower
195
+ * @param {number} w - Rectangle width
196
+ * @param {number} h - Rectangle height
197
+ * @returns {[Rectangle, Rectangle]} Placed rectangle and maximal rectangle
198
+ */
199
+ _select_position(w, h) {
200
+ if (!this._max_rects.length) {
201
+ return [null, null];
202
+ }
203
+
204
+ let fitn = this._max_rects
205
+ .map(m => {
206
+ const fitness = this._rect_fitness(m, w, h);
207
+ return fitness !== null ? [m.y + h, m.x, w, h, m] : null;
208
+ })
209
+ .filter(x => x !== null);
210
+
211
+ let fitr = [];
212
+ if (this.rot) {
213
+ fitr = this._max_rects
214
+ .map(m => {
215
+ const fitness = this._rect_fitness(m, h, w);
216
+ return fitness !== null ? [m.y + w, m.x, h, w, m] : null;
217
+ })
218
+ .filter(x => x !== null);
219
+ }
220
+
221
+ const fit = [...fitn, ...fitr];
222
+ if (fit.length === 0) {
223
+ return [null, null];
224
+ }
225
+
226
+ const [_, __, width, height, m] = fit.reduce((min, curr) =>
227
+ curr[0] < min[0] ? curr : min
228
+ );
229
+
230
+ return [new Rectangle(m.x, m.y, width, height), m];
231
+ }
232
+ }
233
+
234
+ class MaxRectsBssf extends MaxRects {
235
+ /**
236
+ * Best Short Side Fit - minimize short leftover side
237
+ * @param {Rectangle} max_rect - Maximal rectangle
238
+ * @param {number} width - Rectangle width
239
+ * @param {number} height - Rectangle height
240
+ * @returns {number|null} Fitness value or null if placement impossible
241
+ */
242
+ _rect_fitness(max_rect, width, height) {
243
+ if (width > max_rect.width || height > max_rect.height) {
244
+ return null;
245
+ }
246
+ return Math.min(max_rect.width - width, max_rect.height - height);
247
+ }
248
+ }
249
+
250
+ class MaxRectsBaf extends MaxRects {
251
+ /**
252
+ * Best Area Fit - pick maximal rectangle with smallest area
253
+ * @param {Rectangle} max_rect - Maximal rectangle
254
+ * @param {number} width - Rectangle width
255
+ * @param {number} height - Rectangle height
256
+ * @returns {number|null} Fitness value or null if placement impossible
257
+ */
258
+ _rect_fitness(max_rect, width, height) {
259
+ if (width > max_rect.width || height > max_rect.height) {
260
+ return null;
261
+ }
262
+ return (max_rect.width * max_rect.height) - (width * height);
263
+ }
264
+ }
265
+
266
+ class MaxRectsBlsf extends MaxRects {
267
+ /**
268
+ * Best Long Side Fit - minimize long leftover side
269
+ * @param {Rectangle} max_rect - Maximal rectangle
270
+ * @param {number} width - Rectangle width
271
+ * @param {number} height - Rectangle height
272
+ * @returns {number|null} Fitness value or null if placement impossible
273
+ */
274
+ _rect_fitness(max_rect, width, height) {
275
+ if (width > max_rect.width || height > max_rect.height) {
276
+ return null;
277
+ }
278
+ return Math.max(max_rect.width - width, max_rect.height - height);
279
+ }
280
+ }
281
+
282
+ module.exports = {
283
+ MaxRects,
284
+ MaxRectsBl,
285
+ MaxRectsBssf,
286
+ MaxRectsBaf,
287
+ MaxRectsBlsf
288
+ };
@@ -0,0 +1,364 @@
1
+ // const { Rectangle } = require('./geometry');
2
+ // const { Point, HSegment } = require('./geometry');
3
+ // const PackingAlgorithm = require('./packAlgo');
4
+ // const WasteManager = require('./waste');
5
+
6
+ // // Base Skyline mixin
7
+ // const SkylineMixin = Base => class extends Base {
8
+ // /**
9
+ // * Initialize Skyline algorithm
10
+ // * @param {number} width - Surface width
11
+ // * @param {number} height - Surface height
12
+ // * @param {boolean} rot - Rectangle rotation enabled
13
+ // */
14
+ // constructor(width, height, rot = true, ...args) {
15
+ // super(width, height, rot, false, ...args);
16
+ // this._waste_management = false;
17
+ // this._waste = new WasteManager(rot);
18
+ // this._skyline = [new HSegment(new Point(0, 0), width)];
19
+ // }
20
+
21
+ // _placement_points_generator(skyline, width) {
22
+ // const skyline_r = skyline[skyline.length - 1].right;
23
+ // const skyline_l = skyline[0].left;
24
+
25
+ // // Placements using skyline segment left point
26
+ // const ppointsl = skyline
27
+ // .filter(s => s.left + width <= skyline_r)
28
+ // .map(s => s.left);
29
+
30
+ // // Placements using skyline segment right point
31
+ // const ppointsr = skyline
32
+ // .filter(s => s.right - width >= skyline_l)
33
+ // .map(s => s.right - width);
34
+
35
+ // // Merge and sort positions
36
+ // return [...ppointsl, ...ppointsr].sort((a, b) => a - b);
37
+ // }
38
+
39
+ // _generate_placements(width, height) {
40
+ // const skyline = this._skyline;
41
+ // const points = [];
42
+
43
+ // let left_index = 0;
44
+ // let right_index = 0;
45
+ // let support_height = skyline[0].top;
46
+ // let support_index = 0;
47
+
48
+ // const placements = this._placement_points_generator(skyline, width);
49
+
50
+ // for (const p of placements) {
51
+ // // If Rectangle's right side changed segment, find new support
52
+ // if (p + width > skyline[right_index].right) {
53
+ // for (right_index = right_index + 1; right_index < skyline.length; right_index++) {
54
+ // if (skyline[right_index].top >= support_height) {
55
+ // support_index = right_index;
56
+ // support_height = skyline[right_index].top;
57
+ // }
58
+ // if (p + width <= skyline[right_index].right) {
59
+ // break;
60
+ // }
61
+ // }
62
+ // }
63
+
64
+ // // If left side changed segment
65
+ // if (p >= skyline[left_index].right) {
66
+ // left_index++;
67
+ // }
68
+
69
+ // // Find new support if the previous one was shifted out
70
+ // if (support_index < left_index) {
71
+ // support_index = left_index;
72
+ // support_height = skyline[left_index].top;
73
+ // for (let i = left_index; i <= right_index; i++) {
74
+ // if (skyline[i].top >= support_height) {
75
+ // support_index = i;
76
+ // support_height = skyline[i].top;
77
+ // }
78
+ // }
79
+ // }
80
+
81
+ // // Add point if there is enough room at the top
82
+ // if (support_height + height <= this.height) {
83
+ // points.push([
84
+ // new Rectangle(p, support_height, width, height),
85
+ // left_index,
86
+ // right_index
87
+ // ]);
88
+ // }
89
+ // }
90
+
91
+ // return points;
92
+ // }
93
+
94
+ // _merge_skyline(skylineq, segment) {
95
+ // if (skylineq.length === 0) {
96
+ // skylineq.push(segment);
97
+ // return;
98
+ // }
99
+
100
+ // if (skylineq[skylineq.length - 1].top === segment.top) {
101
+ // const s = skylineq[skylineq.length - 1];
102
+ // skylineq[skylineq.length - 1] = new HSegment(s.start, s.length + segment.length);
103
+ // } else {
104
+ // skylineq.push(segment);
105
+ // }
106
+ // }
107
+
108
+ // _add_skyline(rect) {
109
+ // const skylineq = []; // Skyline after adding new one
110
+
111
+ // for (const sky of this._skyline) {
112
+ // if (sky.right <= rect.left || sky.left >= rect.right) {
113
+ // this._merge_skyline(skylineq, sky);
114
+ // continue;
115
+ // }
116
+
117
+ // if (sky.left < rect.left && sky.right > rect.left) {
118
+ // // Skyline section partially under segment left
119
+ // this._merge_skyline(skylineq,
120
+ // new HSegment(sky.start, rect.left - sky.left));
121
+ // sky = new HSegment(new Point(rect.left, sky.top), sky.right - rect.left);
122
+ // }
123
+
124
+ // if (sky.left < rect.right) {
125
+ // if (sky.left === rect.left) {
126
+ // this._merge_skyline(skylineq,
127
+ // new HSegment(new Point(rect.left, rect.top), rect.width));
128
+ // }
129
+ // // Skyline section partially under segment right
130
+ // if (sky.right > rect.right) {
131
+ // this._merge_skyline(skylineq,
132
+ // new HSegment(new Point(rect.right, sky.top), sky.right - rect.right));
133
+ // sky = new HSegment(sky.start, rect.right - sky.left);
134
+ // }
135
+ // }
136
+
137
+ // if (sky.left >= rect.left && sky.right <= rect.right) {
138
+ // // Skyline section fully under segment, account for wasted space
139
+ // if (this._waste_management && sky.top < rect.bottom) {
140
+ // this._waste.addWaste(sky.left, sky.top,
141
+ // sky.length, rect.bottom - sky.top);
142
+ // }
143
+ // } else {
144
+ // // Segment
145
+ // this._merge_skyline(skylineq, sky);
146
+ // }
147
+ // }
148
+
149
+ // this._skyline = skylineq;
150
+ // }
151
+
152
+ // _rect_fitness(rect, left_index, right_index) {
153
+ // return rect.top;
154
+ // }
155
+
156
+ // _select_position(width, height) {
157
+ // const positions = this._generate_placements(width, height);
158
+ // if (this.rot && width !== height) {
159
+ // positions.push(...this._generate_placements(height, width));
160
+ // }
161
+ // if (positions.length === 0) {
162
+ // return [null, null];
163
+ // }
164
+
165
+ // return positions.reduce((min, curr) => {
166
+ // const fitness = this._rect_fitness(curr[0], curr[1], curr[2]);
167
+ // return (!min[1] || fitness < min[1]) ? [curr[0], fitness] : min;
168
+ // }, [null, null]);
169
+ // }
170
+
171
+ // fitness(width, height) {
172
+ // if (!(width > 0 && height > 0)) {
173
+ // throw new Error("Width and height must be positive");
174
+ // }
175
+
176
+ // if (width > Math.max(this.width, this.height) ||
177
+ // height > Math.max(this.height, this.width)) {
178
+ // return null;
179
+ // }
180
+
181
+ // // If there is room in wasted space, FREE PACKING!!
182
+ // if (this._waste_management) {
183
+ // if (this._waste.fitness(width, height) !== null) {
184
+ // return 0;
185
+ // }
186
+ // }
187
+
188
+ // // Get best fitness segment
189
+ // const [_, fitness] = this._select_position(width, height);
190
+ // return fitness;
191
+ // }
192
+
193
+ // addRect(width, height, rid = null) {
194
+ // if (!(width > 0 && height > 0)) {
195
+ // throw new Error("Width and height must be positive");
196
+ // }
197
+
198
+ // if (width > Math.max(this.width, this.height) ||
199
+ // height > Math.max(this.height, this.width)) {
200
+ // return null;
201
+ // }
202
+
203
+ // let rect = null;
204
+ // // If Waste management is enabled, first try to place the rectangle there
205
+ // if (this._waste_management) {
206
+ // rect = this._waste.addRect(width, height, rid);
207
+ // }
208
+
209
+ // // Get best possible rectangle position
210
+ // if (!rect) {
211
+ // const [selectedRect] = this._select_position(width, height);
212
+ // if (selectedRect) {
213
+ // rect = selectedRect;
214
+ // this._add_skyline(rect);
215
+ // }
216
+ // }
217
+
218
+ // if (rect === null) {
219
+ // return null;
220
+ // }
221
+
222
+ // // Store rectangle
223
+ // rect.rid = rid;
224
+ // this.rectangles.push(rect);
225
+ // return rect;
226
+ // }
227
+
228
+ // reset() {
229
+ // super.reset();
230
+ // this._skyline = [new HSegment(new Point(0, 0), this.width)];
231
+ // this._waste.reset();
232
+ // }
233
+ // };
234
+
235
+ // // Waste Management Mixin
236
+ // const WasteMixin = Base => class extends Base {
237
+ // constructor(width, height, ...args) {
238
+ // super(width, height, ...args);
239
+ // this._waste_management = true;
240
+ // }
241
+ // };
242
+
243
+ // // Min Waste Fit Mixin
244
+ // const MwfMixin = Base => class extends Base {
245
+ // _rect_fitness(rect, left_index, right_index) {
246
+ // let waste = 0;
247
+ // for (let i = left_index; i <= right_index; i++) {
248
+ // const seg = this._skyline[i];
249
+ // waste += (Math.min(rect.right, seg.right) - Math.max(rect.left, seg.left)) *
250
+ // (rect.bottom - seg.top);
251
+ // }
252
+ // return waste;
253
+ // }
254
+ // };
255
+
256
+ // // Min Waste Fit Low Profile Mixin
257
+ // const MwflMixin = Base => class extends Base {
258
+ // _rect_fitness(rect, left_index, right_index) {
259
+ // let waste = 0;
260
+ // for (let i = left_index; i <= right_index; i++) {
261
+ // const seg = this._skyline[i];
262
+ // waste += (Math.min(rect.right, seg.right) - Math.max(rect.left, seg.left)) *
263
+ // (rect.bottom - seg.top);
264
+ // }
265
+ // return waste * this.width * this.height + rect.top;
266
+ // }
267
+ // };
268
+
269
+ // // Bottom Left Mixin
270
+ // const BlMixin = Base => class extends Base {
271
+ // _rect_fitness(rect, left_index, right_index) {
272
+ // return rect.top;
273
+ // }
274
+ // };
275
+
276
+ // // Create base Skyline class
277
+ // const SkylineBase = SkylineMixin(PackingAlgorithm);
278
+ // class Skyline extends SkylineBase {
279
+ // constructor(...args) {
280
+ // super(...args);
281
+ // }
282
+ // }
283
+
284
+ // // Create individual algorithm classes
285
+ // class SkylineBl extends SkylineBase {
286
+ // constructor(...args) {
287
+ // super(...args);
288
+ // }
289
+
290
+ // _rect_fitness(rect, left_index, right_index) {
291
+ // return rect.top;
292
+ // }
293
+ // }
294
+
295
+ // class SkylineMwf extends SkylineBase {
296
+ // constructor(...args) {
297
+ // super(...args);
298
+ // }
299
+
300
+ // _rect_fitness(rect, left_index, right_index) {
301
+ // let waste = 0;
302
+ // for (let i = left_index; i <= right_index; i++) {
303
+ // const seg = this._skyline[i];
304
+ // waste += (Math.min(rect.right, seg.right) - Math.max(rect.left, seg.left)) *
305
+ // (rect.bottom - seg.top);
306
+ // }
307
+ // return waste;
308
+ // }
309
+
310
+ // _rect_fitness2(rect, left_index, right_index) {
311
+ // return this._skyline
312
+ // .slice(left_index, right_index + 1)
313
+ // .reduce((waste, seg) =>
314
+ // waste + (Math.min(rect.right, seg.right) - Math.max(rect.left, seg.left)), 0);
315
+ // }
316
+ // }
317
+
318
+ // class SkylineMwfl extends SkylineBase {
319
+ // constructor(...args) {
320
+ // super(...args);
321
+ // }
322
+
323
+ // _rect_fitness(rect, left_index, right_index) {
324
+ // let waste = 0;
325
+ // for (let i = left_index; i <= right_index; i++) {
326
+ // const seg = this._skyline[i];
327
+ // waste += (Math.min(rect.right, seg.right) - Math.max(rect.left, seg.left)) *
328
+ // (rect.bottom - seg.top);
329
+ // }
330
+ // return waste * this.width * this.height + rect.top;
331
+ // }
332
+ // }
333
+
334
+ // // Create combined classes with waste management
335
+ // const SkylineBlWmBase = WasteMixin(SkylineBl);
336
+ // class SkylineBlWm extends SkylineBlWmBase {
337
+ // constructor(...args) {
338
+ // super(...args);
339
+ // }
340
+ // }
341
+
342
+ // const SkylineMwfWmBase = WasteMixin(SkylineMwf);
343
+ // class SkylineMwfWm extends SkylineMwfWmBase {
344
+ // constructor(...args) {
345
+ // super(...args);
346
+ // }
347
+ // }
348
+
349
+ // const SkylineMwflWmBase = WasteMixin(SkylineMwfl);
350
+ // class SkylineMwflWm extends SkylineMwflWmBase {
351
+ // constructor(...args) {
352
+ // super(...args);
353
+ // }
354
+ // }
355
+
356
+ // module.exports = {
357
+ // Skyline,
358
+ // SkylineBl,
359
+ // SkylineBlWm,
360
+ // SkylineMwf,
361
+ // SkylineMwfl,
362
+ // SkylineMwfWm,
363
+ // SkylineMwflWm
364
+ // };
@@ -0,0 +1,36 @@
1
+ // const { GuillotineBafMinas } = require('./guillotine');
2
+ // const { Rectangle } = require('./geometry');
3
+ // // Base class for composition
4
+ // class Base {
5
+ // constructor() {
6
+ // // Base class constructor
7
+ // }
8
+ // }
9
+
10
+ // // Create the WasteManager class using the mixin
11
+ // class WasteManager extends GuillotineBafMinas {
12
+ // constructor(rot = true, merge = true, ...args) {
13
+ // super(1, 1, rot, merge, ...args);
14
+ // this._sections = [];
15
+ // }
16
+
17
+ // addWaste(x, y, width, height) {
18
+ // // Add new waste section
19
+ // this._add_section(new Rectangle(x, y, width, height));
20
+ // }
21
+
22
+ // _fitsSurface(width, height) {
23
+ // throw new Error('Not implemented');
24
+ // }
25
+
26
+ // validatePacking() {
27
+ // throw new Error('Not implemented');
28
+ // }
29
+
30
+ // reset() {
31
+ // super.reset();
32
+ // this._sections = [];
33
+ // }
34
+ // }
35
+
36
+ // export default WasteManager;
@@ -0,0 +1,131 @@
1
+ // const { Rectangle } = require('../../src/rectpack/geometry');
2
+ // const {
3
+ // SkylineBl,
4
+ // SkylineBlWm,
5
+ // SkylineMwf,
6
+ // SkylineMwfl,
7
+ // SkylineMwflWm
8
+ // } = require('../../src/rectpack/skyline');
9
+
10
+ // describe('TestSkyline', () => {
11
+ // test('test_init', () => {
12
+ // const s = new SkylineBl(100, 100, false);
13
+ // const rect1 = s.addRect(30, 30);
14
+ // const rect2 = s.addRect(100, 70);
15
+ // expect(rect1).toEqual(new Rectangle(0, 0, 30, 30));
16
+ // expect(rect2).toEqual(new Rectangle(0, 30, 100, 70));
17
+ // });
18
+
19
+ // test('test_rotation', () => {
20
+ // // Test rotation is enabled by default
21
+ // const s1 = new SkylineBl(100, 10);
22
+ // const rect1 = s1.addRect(10, 100);
23
+ // expect(rect1).toEqual(new Rectangle(0, 0, 100, 10));
24
+
25
+ // // Test rotation can be disabled
26
+ // const s2 = new SkylineBl(100, 10, false);
27
+ // const rect2 = s2.addRect(10, 100);
28
+ // expect(rect2).toBeNull();
29
+ // });
30
+
31
+ // test('test_waste_management', () => {
32
+ // // Generate one wasted section
33
+ // const s = new SkylineBlWm(100, 100, false);
34
+ // const rect1 = s.addRect(30, 30);
35
+ // const rect2 = s.addRect(100, 70);
36
+ // expect(rect1).toEqual(new Rectangle(0, 0, 30, 30));
37
+ // expect(rect2).toEqual(new Rectangle(0, 30, 100, 70));
38
+ // expect(s.rectangles.length).toBe(2);
39
+
40
+ // // Add rectangle that only fits into wasted section
41
+ // expect(s.addRect(71, 30)).toBeNull();
42
+ // expect(s.addRect(70, 31)).toBeNull();
43
+ // const rect3 = s.addRect(70, 30);
44
+ // expect(rect3).toEqual(new Rectangle(30, 0, 70, 30));
45
+ // expect(s.rectangles.length).toBe(3);
46
+
47
+ // const rect4 = s.addRect(70, 30);
48
+ // expect(rect4).toBeNull();
49
+
50
+ // // Test the same without waste management
51
+ // const s2 = new SkylineBl(100, 100);
52
+ // const rect5 = s2.addRect(30, 30);
53
+ // const rect6 = s2.addRect(100, 70);
54
+ // expect(rect5).toEqual(new Rectangle(0, 0, 30, 30));
55
+ // expect(rect6).toEqual(new Rectangle(0, 30, 100, 70));
56
+
57
+ // expect(s2.addRect(70, 30)).toBeNull();
58
+ // expect(s2.rectangles.length).toBe(2);
59
+ // });
60
+
61
+ // test('test_iter', () => {
62
+ // const s = new SkylineBlWm(100, 100);
63
+ // expect(s.addRect(50, 50)).toBeTruthy();
64
+ // expect(s.addRect(100, 50)).toBeTruthy();
65
+ // expect(s.rectangles.length).toBe(2);
66
+ // expect(s.addRect(40, 40)).toBeTruthy();
67
+ // expect(s.rectangles.length).toBe(3);
68
+ // });
69
+
70
+ // test('test_len', () => {
71
+ // const s = new SkylineBlWm(100, 100);
72
+ // expect(s.addRect(50, 50)).toBeTruthy();
73
+ // expect(s.addRect(100, 50)).toBeTruthy();
74
+ // expect(s.rectangles.length).toBe(2);
75
+ // expect(s.addRect(50, 50)).toBeTruthy();
76
+ // expect(s.rectangles.length).toBe(3);
77
+ // });
78
+ // });
79
+
80
+ // describe('TestSkylineMwf', () => {
81
+ // test('test_init', () => {
82
+ // const p = new SkylineMwf(100, 100);
83
+ // expect(p._waste_management).toBe(false);
84
+ // });
85
+
86
+ // test('test_fitness', () => {
87
+ // const p = new SkylineMwf(100, 100, false);
88
+ // p.addRect(20, 20);
89
+ // expect(p.fitness(90, 10)).toBeLessThan(p.fitness(100, 10));
90
+ // });
91
+ // });
92
+
93
+ // describe('TestSkylineMwFwm', () => {
94
+ // test('test_init', () => {
95
+ // const p = new SkylineMwfWm(100, 100);
96
+ // expect(p._waste_management).toBe(true);
97
+ // });
98
+ // });
99
+
100
+ // describe('TestSkylineMwfl', () => {
101
+ // test('test_init', () => {
102
+ // const p = new SkylineMwfl(100, 100);
103
+ // expect(p._waste_management).toBe(false);
104
+ // });
105
+
106
+ // test('test_fitness', () => {
107
+ // const p = new SkylineMwfl(100, 100, false);
108
+ // p.addRect(20, 20);
109
+ // expect(p.fitness(90, 10)).toBeLessThan(p.fitness(90, 20));
110
+ // });
111
+ // });
112
+
113
+ // describe('TestSkylineBl', () => {
114
+ // test('test_init', () => {
115
+ // const p = new SkylineBl(100, 100);
116
+ // expect(p._waste_management).toBe(false);
117
+ // });
118
+
119
+ // test('test_fitness', () => {
120
+ // const p = new SkylineBl(100, 100, false);
121
+ // expect(p.fitness(100, 20)).toBe(p.fitness(10, 20));
122
+ // expect(p.fitness(100, 10)).toBeLessThan(p.fitness(100, 11));
123
+
124
+ // // The same but with wasted space
125
+ // const p2 = new SkylineBl(100, 100, false);
126
+ // p2.addRect(80, 50);
127
+ // expect(p2.fitness(100, 10)).toBe(p2.fitness(80, 10));
128
+ // expect(p2.fitness(100, 10)).toBeLessThan(p2.fitness(40, 20));
129
+ // });
130
+ // });
131
+
@@ -0,0 +1,76 @@
1
+ const { Rectangle } = require('../../src/rectpack/geometry');
2
+ const WasteManager = require('../../src/rectpack/waste').default;
3
+
4
+ describe('TestWaste', () => {
5
+ test('test_init', () => {
6
+ const w = new WasteManager();
7
+ expect(w).toBeDefined();
8
+ });
9
+
10
+ test('test_add_waste', () => {
11
+ const w = new WasteManager();
12
+ w.addWaste(30, 40, 50, 50);
13
+ w.addWaste(5, 5, 20, 20);
14
+ w.addWaste(0, 100, 100, 100);
15
+
16
+ const rect1 = w.addRect(20, 20);
17
+ const rect2 = w.addRect(45, 40);
18
+ const rect3 = w.addRect(90, 80);
19
+ const rect4 = w.addRect(100, 100);
20
+ const rect5 = w.addRect(10, 10);
21
+
22
+ expect(rect1).toEqual(new Rectangle(5, 5, 20, 20));
23
+ expect(rect2).toEqual(new Rectangle(30, 40, 45, 40));
24
+ expect(rect3).toEqual(new Rectangle(0, 100, 90, 80));
25
+ expect(rect4).toBeNull();
26
+ expect(rect5).toEqual(new Rectangle(30, 80, 10, 10));
27
+
28
+ expect(w.rectangles.length).toBe(4);
29
+
30
+ // Test merge is enabled for new waste
31
+ const w2 = new WasteManager();
32
+ w2.addWaste(0, 0, 50, 50);
33
+ w2.addWaste(50, 0, 50, 50);
34
+ expect(w2._sections.length).toBe(1);
35
+ expect(w2.addRect(100, 50)).toEqual(new Rectangle(0, 0, 100, 50));
36
+ });
37
+
38
+ test('test_empty', () => {
39
+ // Test it is empty by default
40
+ const w = new WasteManager();
41
+ expect(w.addRect(1, 1)).toBeFalsy();
42
+ });
43
+
44
+ test('test_add_rect', () => {
45
+ const w = new WasteManager(false);
46
+ w.addWaste(50, 40, 100, 40);
47
+ expect(w.addRect(30, 80)).toBeNull();
48
+
49
+ // Test with rotation
50
+ const w2 = new WasteManager(true);
51
+ w2.addWaste(50, 40, 100, 40);
52
+ expect(w2.addRect(30, 80)).toEqual(new Rectangle(50, 40, 80, 30));
53
+
54
+ // Test rectangle rid
55
+ const w3 = new WasteManager(true);
56
+ w3.addWaste(50, 40, 100, 40);
57
+ const rect = w3.addRect(30, 80, 23);
58
+ expect(rect.rid).toBe(23);
59
+ });
60
+
61
+ test('test_iter', () => {
62
+ // Iterate through rectangles
63
+ const w = new WasteManager();
64
+ w.addWaste(30, 40, 50, 50);
65
+ w.addWaste(5, 5, 20, 20);
66
+ w.addWaste(0, 100, 100, 100);
67
+
68
+ w.addRect(50, 50);
69
+
70
+ for (const r of w.rectangles) {
71
+ expect(r).toEqual(new Rectangle(30, 40, 50, 50));
72
+ }
73
+
74
+ expect(w.rectangles.length).toBe(1);
75
+ });
76
+ });