-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle.h
189 lines (158 loc) · 6.55 KB
/
rectangle.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
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
#include <cassert>
#include <tuple> // tie
#include <algorithm>
#include "net.h"
#include "common.h"
struct rectangle
{
// Basepoint, may be not set
point base;
// Point contains width as x and height as y
point size;
int id = 0;
bool blockage = false;
bool flipped = false;
rotation rot = rotation::rotated_0;
rectangle() = default;
rectangle(point min, point max) :
base(min),
size(max.x - min.x, max.y - min.y, true)
{
assert(size.x >= 0 && size.y >= 0);
}
/**
* Checks whether the rectangle is rotated by 90 or 270 degrees, so the width and height are interchanged
* @return rot == rotated_90 || rot == rotated_270
*/
bool rotated() const;
/**
* For the following methods, the left and lower edge of the rectangle belongs to the rectangle, but the
* right and upper edge does not belong to the rectangle.
*/
/**
* Checks if the given point is contained in the rectangle.
* @param p point to check
* @return true if the rectangle contains the position of the point in each dimension.
*/
bool contains(const point p) const;
/**
* Checks if the given position is inside the rectangle in the given dimension.
* @param to_check position to check
* @param dim dimension that we consider
* @return true if the rectangle contains the position in the given dimension.
*/
bool contains(const pos to_check, dimension dim) const;
/**
* alias for contains(to_check, dimension::x)
*/
bool contains_x(const pos to_check) const;
/**
* alias for contains(to_check, dimension::y)
*/
bool contains_y(const pos to_check) const;
/**
* Gets width or height of the rectangle. The rectangle does not need to be placed.
* @param dim specifies if width or height is returned
* @return width if dim is dimension::x, height otherwise
*/
pos get_dimension(dimension dim) const;
/**
* Gets the basepoint of the rectangle in the specified dimension. Only works on already placed rectangles.
* @param dim The dimension to consider
* @param other If true, the other dimension is taken. This can be useful if we need the other dimension but do not
* know in which dimension we are.
* @return base.coord(dim, other)
*/
pos get_pos(dimension dim, bool other = false) const;
/**
* Getter for the right or upper boundary of the rectangle. Only works on already placed rectangles.
* Observes rotation.
* @param dim The dimension of the boundary to return
* @param other If true the other dimension is taken.
* @return Either the right or the upper boundary
*/
pos get_max(dimension dim, bool other = false) const;
point get_max_point() const;
/**
* Checks if the rectangle is already placed.
* @return base.set
*/
bool placed() const;
/**
* Checks if the rectangle intersects the specified rectangle. Both rectangle contains lower and left but not
* upper and right border.
* @param rect The rectangle to check intersection with
* @return True if the rectangle have an inner point in common
*/
bool intersects(const rectangle &rect) const;
/**
* Compares this rectangle with the specified and returns which is smaller in the specified dimension.
* @param rect The rectangle to compare with
* @param dim The dimension in which we compare
* @return True if the basepoint of this rectangle has a smaller coordinate in the specified dimension than the
* basepoint of rect.
*/
bool smaller(const rectangle &rect, dimension dim) const;
/**
* A wrapper for smaller in the dimension::y with the addition that in case of equality the rectangle with the
* smaller id is smaller. From this, we get a strict ordering.
* @param rect The rectangle to compare with.
* @return True if this rectangle lays below of rect or if it has a smaller id in case of equality.
*/
bool operator<(const rectangle &rect) const;
/**
* Returns the relative position of a pin on this rectangle in the given
* dimension. Flipping and rotation are observed. Only works on already
* placed rectangles and if the pin belongs to the rectangle.
* @param p The pin which position shall be returned. Has to be on this rectangle.
* @param dim The dimension of the position to return.
* @return The relative position of the pin in this dimension.
*/
pos get_relative_pin_position(const pin &p, dimension dim) const;
/**
* Returns the relative position of a pin on this rectangle. Flipping and
* rotation are observed. Only works if the pin belongs to the rectangle.
* The rectangle does not need to be already placed.
* @param p The pin which position shall be returned. Has to be on this rectangle.
* @return The relative position of the pin as a pair of pos.
*/
point get_relative_pin_position(const pin &p) const;
/**
* Returns the absolute position of a pin on this rectangle. Flipping and
* rotation are observed. Only works on already placed rectangles and if
* the pin belongs to the rectangle.
* @param p The pin which position shall be returned. Has to be on this rectangle.
* @return The absolute position of the pin as a pair of pos.
*/
point get_absolute_pin_position(const pin &p) const;
/**
* Rotates the rectangle by the passed rotation. This does NOT set the rotation but rather
* increments it by the passed rotation.
* @param rotate rotation by that we increase our current rotation
*/
void rotate(const rotation rotate);
/**
* Flips the rectangle, i.e. inverts the flipped variable.
*/
void flip();
/**
* Returns a new rectangle that is the intersection of this rectangle with the specified rectangle.
* Warning: May return an invalid rectangle if the rectangles do not intersect.
* @param other The rectangle with that we want to intersect
* @return A rectangle which has the area of the intersection.
*/
rectangle intersection(const rectangle &other) const;
/**
* A static wrapper for the left-of-comparison
* @param left
* @param right
* @return left.left_of(right)
*/
static bool compare(const rectangle &left, const rectangle &right);
};
std::ostream &operator<<(std::ostream &out, const rectangle &rect);
std::istream &operator>>(std::istream &in, rectangle &rect);
#endif // RECTANGLE_H