11#include < iostream>
2+ #include < list>
23#include < rectpack2D/finders_interface.h>
34
45/* For description of the algorithm, please see the README.md */
@@ -75,13 +76,56 @@ int main() {
7576
7677 const auto discard_step = -4 ;
7778
79+ /*
80+ Your custom class holding the rectangle.
81+
82+ If you do not need any data associated with the rectangles,
83+ you are free to ignore this class and just use an std::vector<rect_type>.
84+ */
85+
86+ class my_rect {
87+ rect_type rect;
88+ int some_medadata = 0 ;
89+
90+ public:
91+ my_rect (const rect_type& rect) : rect(rect) { (void )some_medadata; }
92+
93+ /*
94+ You need to provide these two member getters.
95+
96+ The algorithm has to extract the actual rectangles
97+ from your custom class. They will be modified in-place.
98+
99+ */
100+
101+ auto & get_rect () {
102+ return rect;
103+ }
104+
105+ const auto & get_rect () const {
106+ return rect;
107+ }
108+ };
109+
78110 /*
79111 Create some arbitrary rectangles.
80112 Every subsequent call to the packer library will only read the widths and heights that we now specify,
81113 and always overwrite the x and y coordinates with calculated results.
82114 */
83115
84- std::vector<rect_type> rectangles;
116+ std::vector<my_rect> rectangles;
117+
118+ /*
119+ The example will compile just fine if you any of these instead:
120+
121+ std::vector<rect_type> rectangles;
122+ std::list<rect_type> rectangles;
123+
124+ std::list<my_rect> rectangles;
125+
126+ 1. The container just needs to be forward-iterable with .begin() and .end().
127+ 2. The element type just needs to have get_rect() member functions defined (const and non-const).
128+ */
85129
86130 rectangles.emplace_back (rect_xywh (0 , 0 , 20 , 40 ));
87131 rectangles.emplace_back (rect_xywh (0 , 0 , 120 , 40 ));
@@ -92,7 +136,8 @@ int main() {
92136 auto report_result = [&rectangles](const rect_wh& result_size) {
93137 std::cout << " Resultant bin: " << result_size.w << " " << result_size.h << std::endl;
94138
95- for (const auto & r : rectangles) {
139+ for (const auto & rect : rectangles) {
140+ const auto & r = rect.get_rect ();
96141 std::cout << r.x << " " << r.y << " " << r.w << " " << r.h << std::endl;
97142 }
98143 };
@@ -175,7 +220,7 @@ int main() {
175220 packing_root.flipping_mode = runtime_flipping_mode;
176221
177222 for (auto & r : rectangles) {
178- if (const auto inserted_rectangle = packing_root.insert (std::as_const (r).get_wh ())) {
223+ if (const auto inserted_rectangle = packing_root.insert (std::as_const (r. get_rect () ).get_wh ())) {
179224 r = *inserted_rectangle;
180225 }
181226 else {
0 commit comments