-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle.cpp
More file actions
292 lines (245 loc) · 6.25 KB
/
rectangle.cpp
File metadata and controls
292 lines (245 loc) · 6.25 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "rectangle.h"
bool rectangle::placed() const
{
return base.set;
}
bool rectangle::intersects(const rectangle &rect) const
{
assert(placed() && rect.placed());
bool ret = true;
for (dimension dim : all_dimensions)
{
ret &= contains(rect.get_pos(dim), dim) || rect.contains(get_pos(dim), dim);
}
return ret;
}
bool rectangle::smaller(const rectangle &rect, dimension dim) const
{
assert(placed() && rect.placed());
return get_pos(dim) < rect.get_pos(dim);
}
bool rectangle::operator<(const rectangle &rect) const
{
if (get_pos(dimension::y) == rect.get_pos(dimension::y))
{
return id < rect.id;
}
else
{
return smaller(rect, dimension::y);
}
}
void rectangle::rotate(const rotation rotate)
{
rot = static_cast<rotation>(((int) rot + (int) rotate) % (int) rotation::count);
}
bool rectangle::compare(const rectangle &left, const rectangle &right)
{
return left.smaller(right, dimension::x);
}
point rectangle::get_relative_pin_position(const pin &p) const
{
assert(id == p.index);
point pin_point = p.position;
if (flipped)
{
pin_point.x = size.x - pin_point.x;
}
pos tmp_x;
// This probably is faster than calculating sinus and cosinus
// TODO: check my formulas
switch (rot)
{
case rotation::rotated_0:
break;
case rotation::rotated_90:
tmp_x = pin_point.x;
pin_point.x = size.y - pin_point.y;
pin_point.y = tmp_x;
break;
case rotation::rotated_180:
pin_point.x = size.x - pin_point.x;
pin_point.y = size.y - pin_point.y;
break;
case rotation::rotated_270:
tmp_x = pin_point.x;
pin_point.x = pin_point.y;
pin_point.y = size.x - tmp_x;
break;
default:
assert(false);
}
return pin_point;
}
rectangle rectangle::intersection(const rectangle &other) const
{
rectangle ret;
for (dimension dim : all_dimensions)
{
ret.base.coord(dim) = std::max(get_pos(dim), other.get_pos(dim));
ret.size.coord(dim) = std::min(get_max(dim), other.get_max(dim));
}
return ret;
}
point rectangle::get_absolute_pin_position(const pin &p) const
{
assert(placed());
point pin_point = get_relative_pin_position(p);
for (dimension dim : all_dimensions)
{
pin_point.coord(dim) += get_pos(dim);
}
return pin_point;
}
pos rectangle::get_relative_pin_position(const pin &p, dimension dim) const
{
return get_relative_pin_position(p).coord(dim);
}
pos rectangle::get_pos(dimension dim, bool other) const
{
assert(placed());
return base.coord(dim, other);
}
pos rectangle::get_dimension(dimension dim) const
{
return size.coord(dim, rotated());
}
pos rectangle::get_max(dimension dim, bool other) const
{
assert(placed());
return base.coord(dim, other) + size.coord(dim, rotated() ^ other);
}
bool rectangle::contains(const pos to_check, dimension dim) const
{
assert(placed());
return base.coord(dim) <= to_check && to_check < get_max(dim);
}
bool rectangle::rotated() const
{
return rot == rotation::rotated_90 || rot == rotation::rotated_270;
}
bool rectangle::contains_x(const pos to_check) const
{
return contains(to_check, dimension::x);
}
bool rectangle::contains_y(const pos to_check) const
{
return contains(to_check, dimension::y);
}
bool rectangle::contains(const point p) const
{
for (dimension dim : all_dimensions)
{
if (!contains(p.coord(dim), dim))
{
return false;
}
}
return true;
}
point rectangle::get_max_point() const
{
assert(placed());
point p;
for (dimension dim: all_dimensions)
{
p.coord(dim) = get_pos(dim) + get_max(dim);
}
p.set = true;
return p;
}
void rectangle::flip()
{
flipped = !flipped;
}
/**
* Outputs the rectangle. Does not end the line. Only works for already placed rectangles.
*/
std::ostream &operator<<(std::ostream &out, const rectangle &rect)
{
// Assert that rectangle is already placed
assert(rect.placed());
for (dimension dim : all_dimensions)
{
out << rect.get_pos(dim) << " " << rect.get_max(dim) << " ";
}
out << rect.flipped << " ";
out << (int) rect.rot;
return out;
}
/**
* Reads a rotation.
*/
std::istream &operator>>(std::istream &in, rotation &rot)
{
int temp;
in >> temp;
rot = static_cast<rotation>(temp);
return in;
}
/**
* Reads a rectangle, works for blockages and rectangles (unplaced and placed).
*/
std::istream &operator>>(std::istream &in, rectangle &rect)
{
if (in.peek() == 'B')
{
// We read a blockage
rect.blockage = true;
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
point p;
if (!(in >> p))
{
return in;
}
// Now we check if we reached the end of the line:
if (in.peek() == '\n')
{
in.ignore();
// So we read width and height of an unplaced rectangle
rect.size = p;
}
else
{
// So we are reading a placed rectangle
rect.base.set = true;
rect.base.x = p.x;
pos y_max;
in >> rect.base.y;
in >> y_max;
rect.size.x = p.y - rect.base.x;
rect.size.y = y_max - rect.base.y;
if (in.peek() == '\n')
{
in.ignore();
// We read the chip base rectangle
rect.id = -1;
}
else
{
in >> rect.flipped;
in >> rect.rot;
if (rect.rotated())
{
rect.size.swap();
}
}
}
if (rect.size.x < 0 || rect.size.y < 0)
{
throw std::runtime_error("Invalid rectangle size.");
}
}
// Check that the rectangle has a positive size
for (dimension dim : all_dimensions)
{
if (rect.size.coord(dim) < 0)
{
throw std::runtime_error("Impossible rectangle dimensions in rectangle " + std::to_string(rect.id));
}
}
return in;
}