-
Notifications
You must be signed in to change notification settings - Fork 188
/
SkylineBinPack.h
98 lines (71 loc) · 3.13 KB
/
SkylineBinPack.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
90
91
92
93
94
95
96
97
98
/** @file SkylineBinPack.h
@author Jukka Jylänki
@brief Implements different bin packer algorithms that use the SKYLINE data structure.
This work is released to Public Domain, do whatever you want with it.
*/
#pragma once
#include <vector>
#include "Rect.h"
#include "GuillotineBinPack.h"
namespace rbp {
/** Implements bin packing algorithms that use the SKYLINE data structure to store the bin contents. Uses
GuillotineBinPack as the waste map. */
class SkylineBinPack
{
public:
/// Instantiates a bin of size (0,0). Call Init to create a new bin.
SkylineBinPack();
/// Instantiates a bin of the given size.
SkylineBinPack(int binWidth, int binHeight, bool useWasteMap);
/// (Re)initializes the packer to an empty bin of width x height units. Call whenever
/// you need to restart with a new bin.
void Init(int binWidth, int binHeight, bool useWasteMap);
/// Defines the different heuristic rules that can be used to decide how to make the rectangle placements.
enum LevelChoiceHeuristic
{
LevelBottomLeft,
LevelMinWasteFit
};
/// Inserts the given list of rectangles in an offline/batch mode, possibly rotated.
/// @param rects The list of rectangles to insert. This vector will be destroyed in the process.
/// @param dst [out] This list will contain the packed rectangles. The indices will not correspond to that of rects.
/// @param method The rectangle placement rule to use when packing.
void Insert(std::vector<RectSize> &rects, std::vector<Rect> &dst, LevelChoiceHeuristic method);
/// Inserts a single rectangle into the bin, possibly rotated.
Rect Insert(int width, int height, LevelChoiceHeuristic method);
/// Computes the ratio of used surface area to the total bin area.
float Occupancy() const;
private:
int binWidth;
int binHeight;
#ifdef _DEBUG
DisjointRectCollection disjointRects;
#endif
/// Represents a single level (a horizontal line) of the skyline/horizon/envelope.
struct SkylineNode
{
/// The starting x-coordinate (leftmost).
int x;
/// The y-coordinate of the skyline level line.
int y;
/// The line width. The ending coordinate (inclusive) will be x+width-1.
int width;
};
std::vector<SkylineNode> skyLine;
unsigned long usedSurfaceArea;
/// If true, we use the GuillotineBinPack structure to recover wasted areas into a waste map.
bool useWasteMap;
GuillotineBinPack wasteMap;
Rect InsertBottomLeft(int width, int height);
Rect InsertMinWaste(int width, int height);
Rect FindPositionForNewNodeMinWaste(int width, int height, int &bestHeight, int &bestWastedArea, int &bestIndex) const;
Rect FindPositionForNewNodeBottomLeft(int width, int height, int &bestHeight, int &bestWidth, int &bestIndex) const;
bool RectangleFits(int skylineNodeIndex, int width, int height, int &y) const;
bool RectangleFits(int skylineNodeIndex, int width, int height, int &y, int &wastedArea) const;
int ComputeWastedArea(int skylineNodeIndex, int width, int height, int y) const;
void AddWasteMapArea(int skylineNodeIndex, int width, int height, int y);
void AddSkylineLevel(int skylineNodeIndex, const Rect &rect);
/// Merges all skyline nodes that are at the same level.
void MergeSkylines();
};
}