2

使用这个2d bin-packing algorithm (EDIT: Fixed demo) 这是一个变体,怎样才能得到每个 bin 的最终 bin 宽度和高度?

我的演示代码如下:

 var blocks = [
    {w: 1000, h: 800},
    {w: 500, h: 700},
    {w: 500, h: 700},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350}
];

var sheets = [];

while(blocks.length) {
    var packer = new GrowingPacker(1000,800);
    packer.fit(blocks);

    sheet = [];
    for (var i=blocks.length-1; i>=0; i--) {
        if (blocks[i].fit !== undefined && blocks[i].fit !== null) {
            //console.log(blocks[i].fit);
            sheet.unshift(blocks[i]);
            blocks.splice(i,1);
        }
    }
    //console.log(sheet[sheet.length-1].fit.y + sheet[sheet.length-1].h);
    //console.log(sheet);
    sheets.push(sheet);
}


for(var i=0; i<sheets.length; i++) {
    var sheet = sheets[i];
    var sheetWidth = sheet[sheet.length-1].w + sheet[sheet.length-1].fit.x;
    var sheetHeight = sheet[sheet.length-1].h + sheet[sheet.length-1].fit.y;

    for(var j=0; j<sheet.length; j++) {
        console.log("SHEET #" + i + " - W: " + sheetWidth + " H: " + sheetHeight + " BLOCK #" + j + " - W: " + sheet[j].w + " H: " + sheet[j].h + " X: " + sheet[j].fit.x + " Y: " + sheet[j].fit.y);
    }
}

原始算法只处理一个不断扩展的 bin,因此我对其进行了修改以获取最大宽度和高度。然后我遍历块数组,调用打包器,将合适的块推送到一个新数组,然后将它们从“块”中取消设置,直到“块”为空。这是否是最好的方法是另一个问题的主题。

无论如何,我已经尝试像这样修改growNode:

growNode: function(w, h) {
    var canGrowRight  = (w <= this.root.w && this.root.w + w <= maxW);
    var canGrowDown = (h <= this.root.h && this.root.h + h <= maxH);

    if (canGrowRight) {
        this.sheetW = this.root.w + w; //<--------------added
        return this.growRight(w, h);
    }
    else if (canGrowDown) {
        this.sheetH = this.root.h + h; //<--------------added
        return this.growDown(w, h);
    }
    else

        return null; // need to ensure sensible root starting size to avoid this happening
},

它适用于除第一张以外的每张纸。我也尝试用其他一些方法添加这些行,但没有成功。我还尝试从工作表宽度 + x 的最后一个块中获取工作表大小,但这仅在工作表已满时才有效。

我的问题又是如何获得每张纸的最终尺寸?

4

2 回答 2

0

我最终想通了。我在 findNode() 中添加了两行

findNode: function(root, w, h) {
    if (root.used) {
        return this.findNode(root.right, w, h) || this.findNode(root.down, w, h);
    }
    else if ((w <= root.w && w <= this.maxW) && (h <= root.h && w <= this.maxW)) {
        this.binWidth = this.root.w <= this.maxW ? this.root.w : this.maxW;
        this.binHeight = this.root.h <= this.maxH ? this.root.h : this.maxH;
        return root;
    }
    else {
        return null;
    }
},

如果你想玩它,这里是 jsfiddle。我建议尝试这样的输入:

100x80
50x70
50x35x2
25x35x4
于 2012-08-10T14:19:33.160 回答
0

您可以使用增长算法,并且可以将 bin 向左或向右增长:http ://codeincomplete.com/posts/2011/5/7/bin_packing 。

于 2012-08-09T22:01:27.653 回答