Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugs in edgy rect placement in updateBinSize() #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/maxrects-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,19 @@ export class MaxRectsBin<T extends IRectangle = Rectangle> extends Bin<T> {
if (this.stage.contain(node)) return false;
let tmpWidth: number = Math.max(this.width, node.x + node.width - this.padding + this.border);
let tmpHeight: number = Math.max(this.height, node.y + node.height - this.padding + this.border);
let tmpFits: boolean = !(tmpWidth > this.maxWidth || tmpHeight > this.maxHeight);
if (this.options.allowRotation) {
// do extra test on rotated node whether it's a better choice
const rotWidth: number = Math.max(this.width, node.x + node.height - this.padding + this.border);
const rotHeight: number = Math.max(this.height, node.y + node.width - this.padding + this.border);
if (rotWidth * rotHeight < tmpWidth * tmpHeight) {
const rotFits: boolean = !(rotWidth > this.maxWidth || rotHeight > this.maxHeight);
// only compare when both rects will fit into bin
if (tmpFits && rotFits && rotWidth * rotHeight < tmpWidth * tmpHeight) {
tmpWidth = rotWidth;
tmpHeight = rotHeight;
}
// if rot fits and tmpFits not then do not compare area and set rot directly (some cases area of not rotated is smaller but will not fit)
if (rotFits && !tmpFits) {
tmpWidth = rotWidth;
tmpHeight = rotHeight;
}
Expand All @@ -308,7 +316,8 @@ export class MaxRectsBin<T extends IRectangle = Rectangle> extends Bin<T> {
if (this.options.square) {
tmpWidth = tmpHeight = Math.max(tmpWidth, tmpHeight);
}
if (tmpWidth > this.maxWidth + this.padding || tmpHeight > this.maxHeight + this.padding) {
tmpFits = !(tmpWidth > this.maxWidth || tmpHeight > this.maxHeight);
if (!tmpFits) {
return false;
}
this.expandFreeRects(tmpWidth + this.padding, tmpHeight + this.padding);
Expand Down
21 changes: 21 additions & 0 deletions test/maxrects-bin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ describe("no padding", () => {
expect(position.y).toBe(0);
});

test("edge case: only rotated version fits and should be set", () => {
const edgeCaseBin = new MaxRectsBin(256, 1024, 0, {allowRotation: true, pot: false});
edgeCaseBin.add(260, 80);
edgeCaseBin.add(260, 80);
edgeCaseBin.add(260, 80);
edgeCaseBin.add(260, 80);
expect(edgeCaseBin.rects).toHaveLength(4);
});

test("report/set bin dirty status", () => {
bin.add(200, 100, {});
expect(bin.dirty).toBe(true); // add element to bin will render bin dirty
Expand Down Expand Up @@ -219,6 +228,18 @@ describe("padding", () => {
expect(bin.rects.length).toBe(1);
});

test("edge case: multiple rects with slightly bigger size then maxWidth should be placed rotated", () => {
const edgeCaseBin = new MaxRectsBin(256, 1024, padding, {allowRotation: true, pot: false, square: false, smart: true}); //why square in maxrects-packer false and in maxrects-bin: true?
edgeCaseBin.add(260, 80);
edgeCaseBin.add(260, 80);
edgeCaseBin.add(260, 80);
edgeCaseBin.add(260, 80);

expect(edgeCaseBin.rects).toHaveLength(4);
expect(edgeCaseBin.rects[3].rot).toBeTruthy();
expect(edgeCaseBin.rects[3].width).toBe(80);
});

test("monkey testing", () => {
// bin = new MaxRectsBin(1024, 1024, 40);
let rects = [];
Expand Down