-
Notifications
You must be signed in to change notification settings - Fork 4
/
result.h
232 lines (187 loc) · 5.51 KB
/
result.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef ResultH
#define ResultH
#include <memory>
#include <cassert>
#include "types.h"
namespace Denisenko {
namespace Raskroy {
struct OldLayoutResult {
struct Detail {
scalar size;
unsigned num;
std::list<std::pair<Part *, unsigned> > parts;
};
typedef std::vector<Detail> Details;
unsigned s; // cut goes perpendicular to this axis
unsigned kratnostj;
scalar cut;
Details details;
OldLayoutResult() : premain(0), precurse(0) {}
OldLayoutResult(const OldLayoutResult &orig)
: s(orig.s),
kratnostj(orig.kratnostj),
cut(orig.cut),
details(orig.details),
premain(orig.premain),
precurse(orig.precurse)
{
orig.premain = 0;
orig.precurse = 0;
}
~OldLayoutResult() {
delete premain;
delete precurse;
}
void set(int s, unsigned kratnostj, scalar cut, Details &details, OldLayoutResult *premain, OldLayoutResult *precurse);
OldLayoutResult& operator = (const OldLayoutResult &orig);
void attachRemain(OldLayoutResult &remain);
void attachRecurse(OldLayoutResult &recurse);
OldLayoutResult * watchRemain(void) const {return premain;}
OldLayoutResult * watchRecurse(void) const {return precurse;}
void CheckAndCalcStat(scalar cutThickness, const Rect& rect, Stat* outStat) const;
private:
mutable OldLayoutResult *premain;
mutable OldLayoutResult *precurse;
};
class Result {
public:
OldLayoutResult raskroy;
Parts::iterator sheet;
Stat Statistics;
unsigned amount;
Result(void) : amount(0) {}
};
enum {ALONG_X = 0, ALONG_Y = 1};
// New format of layout results
// Type of the element
enum LayoutElementType {
ELEM_REMAIN = 0,
ELEM_CUT = 1,
ELEM_RECT = 2, // a layed-out rectangle
ELEM_SUBLAYOUT = 3,
};
// should be compatible with C
struct LayoutElement
{
scalar size; // size of the element along layout axis
int type; // rect, remain, cut or sub-layout
union {
int rect_index; // if type = ELEM_RECT this contains index of
// the rect from layout_rects
struct Layout * layout; // if type == ELEM_SUBLAYOUT this
// is the pointer to sub-layout
};
LayoutElement() : type(ELEM_REMAIN), layout(0) {}
};
// Layout result
// should be compatible with C
struct Layout
{
int along; // elements are located along: 0 - X, 1 - Y
size_t num_elements;
LayoutElement * elements; // rects, cuts, remains and sub-layouts
Layout() : num_elements(0), elements(0) {}
Layout(int along, int num_elements) :
along(along),
num_elements(num_elements),
elements(new LayoutElement[num_elements])
{
}
void clear() {
for (size_t i = 0; i < num_elements; i++) {
if (elements[i].type == ELEM_SUBLAYOUT)
delete elements[i].layout;
}
delete [] elements;
elements = 0;
num_elements = 0;
}
~Layout() {
clear();
}
};
struct LayoutElementBuilder
{
scalar size; // size of the element along layout axis
Rect rect;
LayoutElementType type; // rect, remain, cut or sub-layout
Part * part;
struct LayoutBuilder * layout; // if type == ELEM_SUBLAYOUT this
// is the pointer to sub-layout
LayoutElementBuilder() : type(ELEM_REMAIN), part(nullptr), layout(0) {}
void _convert(LayoutElement & out) const;
};
struct LayoutBuilder {
Rect rect;
int axis;
std::list<LayoutElementBuilder> elements;
scalar remain;
LayoutBuilder() : axis(0) {}
void simplify();
void begin_appending();
void append_sublayout(std::unique_ptr<LayoutBuilder> sublayout, scalar size) {
assert(sublayout->rect.Size[axis] == size);
assert(size <= remain);
if (sublayout->axis == axis) {
elements.splice(elements.end(), sublayout->elements);
}
else {
LayoutElementBuilder sublayout_el;
sublayout_el.type = ELEM_SUBLAYOUT;
sublayout_el.layout = sublayout.release();
sublayout_el.size = size;
sublayout_el.rect = rect;
sublayout_el.rect.Size[axis] = size;
elements.push_back(sublayout_el);
}
remain -= size;
}
void append_cut(scalar size) {
assert(size <= remain);
LayoutElementBuilder cut_el;
cut_el.type = ELEM_CUT;
cut_el.size = size;
cut_el.rect = rect;
cut_el.rect.Size[axis] = size;
elements.push_back(cut_el);
remain -= size;
}
void append_remain(scalar size) {
assert(size <= remain);
LayoutElementBuilder remain_el;
remain_el.type = ELEM_REMAIN;
remain_el.size = size;
remain_el.rect = rect;
remain_el.rect.Size[axis] = size;
elements.push_back(remain_el);
remain -= size;
}
void append_part(Part * part, scalar size);
void _free() {
for (auto i = elements.begin();
i != elements.end(); i++)
{
if (i->type == ELEM_SUBLAYOUT)
delete i->layout;
}
}
~LayoutBuilder() {
_free();
}
void to_layout(Layout & out) const {
out.clear();
out.along = axis;
out.num_elements = elements.size();
out.elements = new LayoutElement[out.num_elements];
auto i = 0;
for (auto pel = elements.begin();
pel != elements.end(); pel++, i++)
{
pel->_convert(out.elements[i]);
}
}
void check() const;
};
} // namespace Denisenko
} // namespace Raskroy
#endif // ResultH