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

Bug with simple packer #12

Open
eemmy opened this issue Sep 2, 2021 · 1 comment
Open

Bug with simple packer #12

eemmy opened this issue Sep 2, 2021 · 1 comment

Comments

@eemmy
Copy link

eemmy commented Sep 2, 2021

Hello, I am facing a problem when trying to use the simplest version of the lib, the packer.js (even though the filename refers to the other one)

It turns out that the following code:

     /**
     * Handle main data generation. This method generate only raw data, used by another API (INSERT LATE) to generate real used data.
     * @param {*} req
     * @param {*} res
     * @return {void}
     */
    getOffsets: (req, res) => {
        // Necessary data
        const mdfData = {
            height: 1000,
            width: 800
        }

        // Import optimizer library and instantiate it
        const growingPacker = require('../vendor/packer.growing.js');
        const packer        = new growingPacker(800, 1000);

        // This will take data to generate offsets
        let pieces = [
            { h: 21100, w: 2220},
            { h: 1000, w: 22000 },
            { h: 1000, w: 22000 },
            { h: 1000, w: 22000 },
            { h: 1000, w: 22000 },
            { h: 1000, w: 22000 },
            { h: 1000, w: 22000 },
            { h: 2000, w: 22000 },
            { h: 1000, w: 22000 },
        ];

        // Sort pieces by height
        pieces.sort((a, b) => { return(b.h < a.h) });
        
        // Generate offsets
        packer.fit(pieces);
        console.log(pieces); 
        

        res.send(pieces)
    }

It is generating this output:

[
    { "h": 21100, "w": 2220 },
    { "h": 1000, "w": 22000 },
    { "h": 1000, "w": 22000 },
    { "h": 1000, "w": 22000 },
    { "h": 1000, "w": 22000 },
    { "h": 1000, "w": 22000 },
    { "h": 1000, "w": 22000 },
    { "h": 2000, "w": 22000 },
    { "h": 1000, "w": 22000 }
]

My packer.growing.js looks like this:

Packer = function(w, h) {
  this.init(w, h);
};

Packer.prototype = {

  init: function(w, h) {
    this.root = { x: 0, y: 0, w: w, h: h };
  },

  fit: function(blocks) {
    var n, node, block;
    for (n = 0; n < blocks.length; n++) {
      block = blocks[n];
      if (node = this.findNode(this.root, block.w, block.h))
        block.fit = this.splitNode(node, block.w, block.h);
    }
  },

  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) && (h <= root.h))
      return root;
    else
      return null;
  },

  splitNode: function(node, w, h) {
    node.used = true;
    node.down  = { x: node.x,     y: node.y + h, w: node.w,     h: node.h - h };
    node.right = { x: node.x + w, y: node.y,     w: node.w - w, h: h          };
    return node;
  }

}

module.exports = Packer;

I am using Node + ExpressJS for development.

I was using Growing at first, which was generating the expected data output, but the feature I am developing requires me to enter a width/height limit.

If you can help me, I would be very grateful.
Thanks in advance, and sorry for the bad English

@lantto
Copy link

lantto commented Mar 6, 2022

Try pieces.sort((a, b) => { return(b.h - a.h) }); instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants