Skip to content

Commit 92f267b

Browse files
committed
Moved rectangle intersection code to vtr_geometry file and refactored region functions accordingly
1 parent 4c93f5f commit 92f267b

File tree

4 files changed

+51
-58
lines changed

4 files changed

+51
-58
lines changed

libs/libvtrutil/src/vtr_geometry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ class Rect {
141141
template<class T>
142142
Rect<T> bounding_box(const Rect<T>& lhs, const Rect<T>& rhs);
143143

144+
//Return the intersection (union) of two given rectangles
145+
template<class T>
146+
Rect<T> intersection(const Rect<T>& lhs, const Rect<T>& rhs);
147+
144148
//Sample on a uniformly spaced grid within a rectangle
145149
// sample(vtr::Rect(l, h), 0, 0, M) == l
146150
// sample(vtr::Rect(l, h), M, M, M) == h

libs/libvtrutil/src/vtr_geometry.tpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ Rect<T> bounding_box(const Rect<T>& lhs, const Rect<T>& rhs) {
182182
std::max(lhs.ymax(), rhs.ymax()));
183183
}
184184

185+
template<class T>
186+
Rect<T> intersection(const Rect<T>& lhs, const Rect<T>& rhs) {
187+
return Rect<T>(std::max(lhs.xmin(), rhs.xmin()),
188+
std::max(lhs.ymin(), rhs.ymin()),
189+
std::min(lhs.xmax(), rhs.xmax()),
190+
std::min(lhs.ymax(), rhs.ymax()));
191+
}
185192
//Only defined for integral types
186193
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type...>
187194
Point<T> sample(const vtr::Rect<T>& r, T x, T y, T d) {

vpr/src/base/region.cpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Region::Region() {
1313
region_bounds.set_ymax(-1);
1414
}
1515

16-
vtr::Rect<int> Region::get_region_rect(){
17-
return region_bounds;
16+
vtr::Rect<int> Region::get_region_rect() {
17+
return region_bounds;
1818
}
1919

2020
int Region::get_xmin() {
@@ -51,25 +51,16 @@ void Region::set_sub_tile(int _sub_tile) {
5151
bool Region::do_regions_intersect(Region region) {
5252
bool intersect = true;
5353

54-
int x_min_1 = region.get_xmin();
55-
int x_min_2 = region_bounds.xmin();
56-
int int_x_min = std::max(x_min_1, x_min_2);
54+
vtr::Rect<int> region_rect = region.get_region_rect();
55+
vtr::Rect<int> intersect_rect;
5756

58-
int y_min_1 = region.get_ymin();
59-
int y_min_2 = region_bounds.ymin();
60-
int int_y_min = std::max(y_min_1, y_min_2);
57+
intersect_rect = intersection(region_bounds, region_rect);
6158

62-
int x_max_1 = region.get_xmax();
63-
int x_max_2 = region_bounds.xmax();
64-
int int_x_max = std::min(x_max_1, x_max_2);
65-
66-
int y_max_1 = region.get_ymax();
67-
int y_max_2 = region_bounds.ymax();
68-
int int_y_max = std::min(y_max_1, y_max_2);
69-
70-
//check if rectangles dimensions are invalid
71-
if (int_x_min > int_x_max || int_y_min > int_y_max) {
72-
intersect = false;
59+
/**if the intersection rectangle is empty or the subtile of the two regions does not match,
60+
* the regions do not intersect
61+
*/
62+
if (intersect_rect.empty() || sub_tile != region.get_sub_tile()) {
63+
return intersect = false;
7364
}
7465

7566
return intersect;
@@ -78,23 +69,15 @@ bool Region::do_regions_intersect(Region region) {
7869
Region Region::regions_intersection(Region region) {
7970
Region intersect;
8071

81-
int x_min_1 = region.get_xmin();
82-
int x_min_2 = region_bounds.xmin();
83-
int int_x_min = std::max(x_min_1, x_min_2);
84-
85-
int y_min_1 = region.get_ymin();
86-
int y_min_2 = region_bounds.ymin();
87-
int int_y_min = std::max(y_min_1, y_min_2);
72+
vtr::Rect<int> region_rect = region.get_region_rect();
73+
vtr::Rect<int> intersect_rect;
8874

89-
int x_max_1 = region.get_xmax();
90-
int x_max_2 = region_bounds.xmax();
91-
int int_x_max = std::min(x_max_1, x_max_2);
75+
intersect_rect = intersection(region_bounds, region_rect);
9276

93-
int y_max_1 = region.get_ymax();
94-
int y_max_2 = region_bounds.ymax();
95-
int int_y_max = std::min(y_max_1, y_max_2);
96-
97-
intersect.set_region_rect(int_x_min, int_y_min, int_x_max, int_y_max);
77+
intersect.set_region_rect(intersect_rect.xmin(), intersect_rect.ymin(), intersect_rect.xmax(), intersect_rect.ymax());
78+
if (sub_tile == region.get_sub_tile()) {
79+
intersect.set_sub_tile(sub_tile);
80+
}
9881

9982
return intersect;
10083
}

vpr/test/test_vpr_constraints.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ TEST_CASE("VprConstraintsAccessors", "[vpr]") {
125125
}
126126

127127
TEST_CASE("RegionIntersect", "[vpr]") {
128-
//Test partial intersection
128+
//Test partial intersection
129129
Region region1;
130130
Region region2;
131131

132-
region1.set_region_rect(1,2,3,5);
133-
region2.set_region_rect(2,3,4,6);
132+
region1.set_region_rect(1, 2, 3, 5);
133+
region2.set_region_rect(2, 3, 4, 6);
134134

135135
Region int_reg;
136136

@@ -144,29 +144,29 @@ TEST_CASE("RegionIntersect", "[vpr]") {
144144

145145
//Test full overlap
146146
Region region3;
147-
Region region4;
147+
Region region4;
148148

149-
region3.set_region_rect(5,1,8,6);
150-
region4.set_region_rect(6,3,8,6);
149+
region3.set_region_rect(5, 1, 8, 6);
150+
region4.set_region_rect(6, 3, 8, 6);
151151

152-
Region int_reg_2;
152+
Region int_reg_2;
153153

154-
int_reg_2 = region3.regions_intersection(region4);
155-
vtr::Rect<int> rect_2 = int_reg_2.get_region_rect();
154+
int_reg_2 = region3.regions_intersection(region4);
155+
vtr::Rect<int> rect_2 = int_reg_2.get_region_rect();
156156

157-
REQUIRE(rect_2.xmin() == 6);
158-
REQUIRE(rect_2.ymin() == 3);
159-
REQUIRE(rect_2.xmax() == 8);
160-
REQUIRE(rect_2.ymax() == 6);
157+
REQUIRE(rect_2.xmin() == 6);
158+
REQUIRE(rect_2.ymin() == 3);
159+
REQUIRE(rect_2.xmax() == 8);
160+
REQUIRE(rect_2.ymax() == 6);
161161

162-
//Test no intersection (rect is empty)
162+
//Test no intersection (rect is empty)
163163

164-
Region int_reg_3;
164+
Region int_reg_3;
165165

166-
int_reg_3 = region1.regions_intersection(region3);
167-
vtr::Rect<int> rect_3 = int_reg_3.get_region_rect();
166+
int_reg_3 = region1.regions_intersection(region3);
167+
vtr::Rect<int> rect_3 = int_reg_3.get_region_rect();
168168

169-
REQUIRE(rect_3.empty() == TRUE);
169+
REQUIRE(rect_3.empty() == TRUE);
170170
}
171171

172172
TEST_CASE("PartRegionIntersect", "[vpr]") {
@@ -206,16 +206,16 @@ TEST_CASE("RegionLocked", "[vpr]") {
206206
bool is_r1_locked = false;
207207

208208
//set the region to a specific x, y, subtile location - region is locked
209-
r1.set_region_rect(2,3,2,3); //point (2,3) to point (2,3) - locking to specific x, y location
210-
r1.set_sub_tile(3); //locking down to subtile 3
209+
r1.set_region_rect(2, 3, 2, 3); //point (2,3) to point (2,3) - locking to specific x, y location
210+
r1.set_sub_tile(3); //locking down to subtile 3
211211

212212
is_r1_locked = r1.locked();
213213

214214
REQUIRE(is_r1_locked == true);
215215

216216
//do not set region to specific x, y location - region is not locked even if a subtile is specified
217-
r1.set_region_rect(2,3,5,6); //point (2,3) to point (5,6) - not locking to specific x, y location
218-
r1.set_sub_tile(3); //locking down to subtile 3
217+
r1.set_region_rect(2, 3, 5, 6); //point (2,3) to point (5,6) - not locking to specific x, y location
218+
r1.set_sub_tile(3); //locking down to subtile 3
219219

220220
is_r1_locked = r1.locked();
221221

@@ -225,12 +225,11 @@ TEST_CASE("RegionLocked", "[vpr]") {
225225
Region r2;
226226
bool is_r2_locked = true;
227227

228-
r2.set_region_rect(2,3,2,3);
228+
r2.set_region_rect(2, 3, 2, 3);
229229

230230
is_r2_locked = r2.locked();
231231

232232
REQUIRE(is_r2_locked == false);
233-
234233
}
235234

236235
TEST_CASE("PartRegionIntersect2", "[vpr]") {

0 commit comments

Comments
 (0)