繁体   English   中英

如何使用javascript增长的装箱算法获取最终的装箱大小?

[英]How can I get final bin size with javascript growing bin packing algorithm?

利用这种二维装箱算法 (编辑:固定的演示)这是一个变化我怎么能得到每个区间的最终箱的宽度和高度?

我的演示代码如下:

 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);
    }
}

原始算法仅处理单个不断扩展的垃圾箱,因此我对其进行了修改,使其具有最大的宽度和高度。 然后,我遍历块数组,调用加壳程序,将适合块推入新数组,然后将它们从“块”中取消设置,直到“块”为空。 那是否是最好的方法是另一个问题。

无论如何,我试图像这样修改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的最后一个块中获取工作表大小,但这仅在工作表已满时才有效。

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

您可以使用增长算法,也可以将容器增加到左侧或右侧: http : //codeincomplete.com/posts/2011/5/7/bin_packing

我最终想通了。 我在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM