Skip to content
This repository was archived by the owner on Oct 15, 2023. It is now read-only.

Commit c20a131

Browse files
authored
Merge pull request #1 from BenJamesBen/cutting-layout-update
Cutting layout update
2 parents 4e91190 + 7410493 commit c20a131

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ module.exports = function(items, options) {
1010
// Clone the items.
1111
var newItems = items.map(function(item) { return inPlace ? item : { width: item.width, height: item.height, item: item }; });
1212

13+
// We need to know whether the widest item exceeds the maximum width.
14+
// The optimal sorting strategy changes depending on this.
15+
const widestWidth =
16+
newItems.sort(function(a, b) { return b.width - a.width})[0].width
17+
const widerThanMax = options.maxWidth && (widestWidth > options.maxWidth)
18+
1319
newItems = newItems.sort(function(a, b) {
14-
if (options.maxWidth && !options.maxHeight) return b.width - a.width
20+
if (options.maxWidth && !options.maxHeight && widerThanMax) return b.width - a.width
21+
if (options.maxWidth && !options.maxHeight) return b.height - a.height
1522
if (options.maxHeight) return b.height - a.height
1623
// TODO: check that each actually HAS a width and a height.
1724
// Sort based on the size (area) of each block.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bin-pack-with-constraints",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A packing algorithm for 2D bin packing that allows setting a max width or height. Largely based on code and a blog post by Jake Gordon and Bryan Burgers.",
55
"author": {
66
"name": "Enoch Riese",

packer.growing.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Construction:
3333
{
3434
maxWidth = Infinity: set a max width to constrain the growth in that direction
3535
maxHeight = Infinity: set a max height to constrain the growth in that direction
36-
TODO strictMax = false: reject blocks that are larger than the max
36+
strictMax = false: require wider-than-max blocks to start at left boundary
37+
PREVIOUS INTENDED TODO strictMax = false: reject blocks that are larger than the max
3738
}
3839
3940
Inputs:
@@ -76,7 +77,7 @@ Example:
7677
var GrowingPacker = function({maxWidth = Infinity, maxHeight = Infinity, strictMax = false}) {
7778
this.maxWidth = maxWidth
7879
this.maxHeight = maxHeight
79-
// this.strictMax = strictMax
80+
this.strictMax = strictMax
8081
this.ensureSquare = maxWidth === Infinity && maxHeight === Infinity
8182
};
8283

@@ -87,7 +88,10 @@ GrowingPacker.prototype = {
8788
if (len === 0) { return }
8889

8990
var n, node, block, fit;
90-
var width = this.strictMax && (this.maxWidth < Infinity) ? this.maxWidth : blocks[0].width;
91+
// Require wider-than-max blocks to start at the left boundary, so
92+
// they encroach past the right boundary minimally.
93+
var width = this.strictMax && (this.maxWidth < Infinity) ? Math.max(blocks[0].width, this.maxWidth) : blocks[0].width;
94+
9195
var height = this.strictMax && (this.maxHeight < Infinity) ? this.maxHeight : blocks[0].height;
9296
this.root = { x: 0, y: 0, width, height };
9397
for (n = 0; n < len ; n++) {

test/test.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,48 @@ describe('bin-pack with maxWidth option', function() {
217217
assert.equal(result.items.length, bins.length, "Result has same amount of items as the source");
218218
verifyResult(result, result.items);
219219
})
220+
})
221+
222+
describe('with strictMax: true', function() {
223+
it('packs items that exceed the maximum width at the left edge', function() {
224+
var bins = [
225+
{ width: 100, height: 10 },
226+
{ width: 110, height: 10 },
227+
];
228+
229+
var result = pack(bins, {maxWidth: 90, strictMax: true});
230+
assert.ok('items' in result, "Result has items");
231+
assert.equal(result.items.length, bins.length, "Result has same amount of items as the source");
232+
assert.ok(result.width === 110, 'Width is that of the widest item')
233+
verifyResult(result, result.items);
234+
})
220235

221-
describe('with strictMax: true', function() {
222-
it('rejects bins that exceed the maximum')
236+
it('does not unnecessarily pack items to exceed the maximum width', function() {
237+
var bins = [
238+
{ width: 10, height: 110 },
239+
{ width: 10, height: 110 },
240+
{ width: 10, height: 110 },
241+
{ width: 40, height: 48 },
242+
{ width: 40, height: 48 },
243+
{ width: 40, height: 48 },
244+
{ width: 50, height: 48 },
245+
{ width: 50, height: 48 },
246+
{ width: 50, height: 48 },
247+
{ width: 60, height: 48 },
248+
{ width: 60, height: 48 },
249+
{ width: 70, height: 48 },
250+
{ width: 70, height: 48 },
251+
{ width: 30, height: 48 },
252+
{ width: 30, height: 48 },
253+
{ width: 30, height: 48 },
254+
];
255+
256+
var result = pack(bins, {maxWidth: 60, strictMax: true});
257+
assert.ok('items' in result, "Result has items");
258+
assert.equal(result.items.length, bins.length, "Result has same amount of items as the source");
259+
assert.ok(result.width === 70, 'Width is that of the widest item')
260+
verifyResult(result, result.items);
223261
})
262+
263+
it('rejects bins that exceed the maximum')
224264
})

0 commit comments

Comments
 (0)