-
Notifications
You must be signed in to change notification settings - Fork 5
/
poBinPacker.h
89 lines (72 loc) · 2.29 KB
/
poBinPacker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2012 Potion Design. All rights reserved.
* This file is part of pocode.
*
* pocode is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* pocode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with pocode. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "poRect.h"
#include <map>
#include <list>
/* addRect with the image dimensions, get back a handle
* use the handle to find where to put the image into digest
*
* sorts by height, creates a "line" with the tallest rectangle
* then fills the leftover out to a set width
* once a line is full, it starts a new one
* bounded by width and height, will start a new page if it runs out of height
*/
class BinPacker {
public:
// init with page size
BinPacker(uint width, uint height, uint padding=0);
~BinPacker();
void reset();
uint addRect(poRect r);
void pack();
void render();
int getNumPages() const;
float getWastedPixels() const;
poRect packPosition(uint handle, uint *page=NULL);
private:
BinPacker(const BinPacker &lhs) {}
BinPacker &operator=(const BinPacker &lhs) {return *this;}
struct insertRect {
insertRect(uint w, uint h, uint handle);
bool operator<(const insertRect &r);
uint w, h, handle;
};
std::list<insertRect> rectangles;
struct packRect {
packRect();
packRect(uint w, uint h);
~packRect();
void setDims(uint x, uint y, uint w, uint h);
packRect *insert(uint w, uint h);
packRect **children;
uint x, y, width, height, handle;
int taken;
};
typedef std::map<uint, packRect*> PackMap_t;
struct pack_page {
std::vector<packRect*> rows;
PackMap_t pack;
};
std::vector<pack_page*> pages;
void render(packRect *rect);
void resetPackMap();
uint width, height, padding, handles;
float wastedPixels;
};