light_weight_rectangle_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include "directions.h"
4 #include "../../src/core/geometry_primitives.h"
5 
6 /*----------------------------------------------------------------------------*/
7 /* LightWeightRectangle.intersection/overlap */
8 
9 class LWRIntersectionOverlapTest : public ::testing::Test {
10 protected:
12 protected:
13  static constexpr auto Base_Outer_Overlap = 1.0 / 9;
14 
15  void check_intersection(const Rect &r1, const Rect &r2,
16  const Rect &exp_inters, double exp_overlap) const {
17 
18  ASSERT_EQ(exp_inters, r1.intersect(r2));
19  ASSERT_EQ(r1.intersect(r2), r2.intersect(r1));
20  ASSERT_EQ(exp_overlap, r1.overlap(r2));
21  }
22 
23  void test_corner_inclusion(const Directions &dirs) const {
24  assert(dirs.horz() * dirs.vert());
25  auto r1 = Rect{-6, 6, -6, 6};
26  auto out_coffset = Point2D{dirs.horz() * r1.hside_len(),
27  dirs.vert() * r1.vside_len()};
28  // FIXME: move to separate test cases
29  { // mutual corner inclusion
30  auto r2 = r1.move_center(out_coffset * 0.5);
31  auto intersection = r1.shrink(2).move_center(out_coffset * 0.25);
32  check_intersection(r1, r2, intersection, 0.25);
33  }
34  { // common corner
35  auto r2 = r1.move_center(out_coffset);
36  auto intersection_sides = out_coffset * 0.5;
37  auto intersection = Rect{intersection_sides.y, intersection_sides.y,
38  intersection_sides.x, intersection_sides.x};
39  check_intersection(r1, r2, intersection, 0.0);
40  }
41  { // common horiz edge part
42  auto hshift = Point2D{dirs.horz() * -1.0, 0} + out_coffset;
43  auto intersection_sides = out_coffset * 0.5;
44  auto intersection = Rect{intersection_sides.y, intersection_sides.y,
45  (dirs.right() ? -1 : 0) + intersection_sides.x,
46  (dirs.left() ? 1 : 0) + intersection_sides.x};
47  check_intersection(r1, r1.move_center(hshift), intersection, 0.0);
48  }
49  { // common vert edge part
50  auto vshift = Point2D{0, dirs.vert() * -1.0} + out_coffset;
51  auto intersection_sides = out_coffset * 0.5;
52  auto intersection = Rect{(dirs.top() ? -1 : 0) + intersection_sides.y,
53  (dirs.bot() ? 1 : 0) + intersection_sides.y,
54  intersection_sides.x, intersection_sides.x};
55  check_intersection(r1, r1.move_center(vshift), intersection, 0.0);
56  }
57  }
58 
59  void test_edge_inclusion(const Directions &dir) {
60  assert(static_cast<bool>(dir.horz()) ^
61  static_cast<bool>(dir.vert()));
62  auto r1 = Rect{-6, 6, -6, 6};
63  auto out_coffset = Point2D{dir.horz() * r1.hside_len(),
64  dir.vert() * r1.vside_len()};
65  // FIXME: split cases
66  { // edge through the middle
67  auto halves = (dir.horz() ? r1.split_horz() : r1.split_vert());
68  auto half_r1 = halves[(1 == dir.horz() || 1 == dir.vert()) ? 1 : 0];
69  check_intersection(r1, r1.move_center(out_coffset * 0.5),
70  half_r1.move_center(out_coffset * 0.25), 0.5);
71  }
72  { // common edge
73  auto intersection_sides = out_coffset * 0.5;
74  auto intersection = Rect{dir.horz() ? r1.bot() : intersection_sides.y,
75  dir.horz() ? r1.top() : intersection_sides.y,
76  dir.vert() ? r1.left() : intersection_sides.x,
77  dir.vert() ? r1.right() : intersection_sides.x};
78  check_intersection(r1, r1.move_center(out_coffset), intersection, 0);
79  }
80  }
81 
83  double exp_outer_overlap) const {
84  auto outer = Rect{0, 12, 0, 12};
85 
86  double inner_left = 4, inner_right = 8,
87  inner_bot = 4, inner_top = 8;
88 
89  #define ALIGN_SIDE(side) \
90  if (dirs.side()) { \
91  inner_##side = outer.side(); \
92  }
93 
94  ALIGN_SIDE(left);
95  ALIGN_SIDE(right);
96  ALIGN_SIDE(top);
97  ALIGN_SIDE(bot);
98 
99  #undef ALIGN_SIDE
100 
101  auto inner = Rect{inner_bot, inner_top, inner_left, inner_right};
102  check_intersection(inner, outer, inner, 1);
103  check_intersection(outer, inner, inner, exp_outer_overlap);
104  }
105 
106 };
107 
108 //------------------------------------------------------------------------------
109 // no overlap
110 
111 TEST_F(LWRIntersectionOverlapTest, noIntersectionSameSize) {
112  check_intersection({1, 2, 1, 2}, {3, 4, 3, 4}, {0, 0, 0, 0}, 0);
113 }
114 
115 TEST_F(LWRIntersectionOverlapTest, noIntersectionDifferentSize) {
116  check_intersection({1, 2, 1, 2}, {3, 5, 3, 5}, {0, 0, 0, 0}, 0);
117 }
118 
119 //------------------------------------------------------------------------------
120 // corner inclusion
121 
122 TEST_F(LWRIntersectionOverlapTest, cornerInclusionLT) {
124 }
125 
126 TEST_F(LWRIntersectionOverlapTest, cornerInclusionRT) {
128 }
129 
130 TEST_F(LWRIntersectionOverlapTest, cornerInclusionLB) {
132 }
133 
134 TEST_F(LWRIntersectionOverlapTest, cornerInclusionRB) {
136 }
137 
138 // TODO: check rectangles with different sizes
139 
140 //------------------------------------------------------------------------------
141 // edge overlap
142 
143 TEST_F(LWRIntersectionOverlapTest, edgeInclusionLeft) {
145 }
146 
147 TEST_F(LWRIntersectionOverlapTest, edgeInclusionRight) {
149 }
150 
151 TEST_F(LWRIntersectionOverlapTest, edgeInclusionTop) {
153 }
154 
155 TEST_F(LWRIntersectionOverlapTest, edgeInclusionBot) {
157 }
158 
159 // TODO: check rectangles with different sizes
160 
161 //------------------------------------------------------------------------------
162 // proper inclusion
163 
164 TEST_F(LWRIntersectionOverlapTest, properInclusionNotSideAligned) {
166 }
167 
168 TEST_F(LWRIntersectionOverlapTest, properInclusionLSideAligned) {
170 }
171 
172 TEST_F(LWRIntersectionOverlapTest, properInclusionRSideAligned) {
174 }
175 
176 TEST_F(LWRIntersectionOverlapTest, properInclusionRLSideAligned) {
179 }
180 
181 TEST_F(LWRIntersectionOverlapTest, properInclusionBSideAligned) {
183 }
184 
185 TEST_F(LWRIntersectionOverlapTest, properInclusionBLSideAligned) {
188 }
189 
190 TEST_F(LWRIntersectionOverlapTest, properInclusionBRSideAligned) {
193 }
194 
195 TEST_F(LWRIntersectionOverlapTest, properInclusionBRLSideAligned) {
198 }
199 
200 TEST_F(LWRIntersectionOverlapTest, properInclusionTSideAligned) {
202 }
203 
204 TEST_F(LWRIntersectionOverlapTest, properInclusionTLSideAligned) {
207 }
208 
209 TEST_F(LWRIntersectionOverlapTest, properInclusionTRSideAligned) {
212 }
213 
214 TEST_F(LWRIntersectionOverlapTest, properInclusionTRLSideAligned) {
217 }
218 
219 TEST_F(LWRIntersectionOverlapTest, properInclusionTBSideAligned) {
221 }
222 
223 TEST_F(LWRIntersectionOverlapTest, properInclusionTBLSideAligned) {
226 }
227 
228 TEST_F(LWRIntersectionOverlapTest, properInclusionTBRSideAligned) {
231 }
232 
233 TEST_F(LWRIntersectionOverlapTest, properInclusionTBRLSideAligned) {
236 }
237 
238 //------------------------------------------------------------------------------
239 // Degenerate rectangles: point/line-like rectangles
240 
241 // point-rects
242 
243 TEST_F(LWRIntersectionOverlapTest, pointRectInsidePointRect) {
244  auto pnt_rect1 = Rect{0, 0, 0.5, 0.5}, pnt_rect2 = pnt_rect1;
245  check_intersection(pnt_rect1, pnt_rect2, pnt_rect1, 1.0);
246 }
247 
248 TEST_F(LWRIntersectionOverlapTest, pointRectOutsidePointRect) {
249  auto pnt_rect1 = Rect{0, 0, 0, 0}, pnt_rect2 = Rect{0, 0, 1, 1};
250  check_intersection(pnt_rect1, pnt_rect2, {}, 0);
251  check_intersection(pnt_rect2, pnt_rect1, {}, 0);
252 }
253 
254 TEST_F(LWRIntersectionOverlapTest, pointRectangleInsidePlainRectangle) {
255  auto pnt_rect = Rect{0.5, 0.5, 0.5, 0.5}, rect = Rect{0, 1, 0, 1};
256  check_intersection(pnt_rect, rect, pnt_rect, 1);
257  check_intersection(rect, pnt_rect, pnt_rect, 0);
258 }
259 
260 TEST_F(LWRIntersectionOverlapTest, pointRectangleOutsidePlainRectangle) {
261  auto pnt_rect = Rect{2, 2, 2, 2}, rect = Rect{0, 1, 0, 1};
262  check_intersection(pnt_rect, rect, {}, 0);
263  check_intersection(rect, pnt_rect, {}, 0);
264 }
265 
266 // line-rects
267 
268 /* FIXME: broken test
269 TEST_F(LWRIntersectionOverlapTest, pointRectInsideLineRect) {
270  auto pnt_rect = Rect{0, 0, 0.5, 0.5}, line_rect = Rect{0, 0, 0, 1};
271  check_intersection(pnt_rect, line_rect, pnt_rect, 1.0);
272  check_intersection(line_rect, pnt_rect, pnt_rect, 1.0);
273 }
274 */
275 
276 /* FIXME: broken test
277 TEST_F(LWRIntersectionOverlapTest, pointRectOutsideLineRect) {
278  auto pnt_rect = Rect{0, 0, 0, 0}, line_rect = Rect{0, 0, 1, 2};
279  check_intersection(pnt_rect, line_rect, {}, 1.0);
280  check_intersection(line_rect, pnt_rect, {}, 1.0);
281 }
282 */
283 
284 // TODO: add line-line-in, ll-out, l-non_emty-in, l-non_empty-out
285 
286 //------------------------------------------------------------------------------
287 // Misc
288 
289 /* FIXME: failed, detects no intersection.
290 TEST_F(LWRIntersectionOverlapTest, overlappedRectanglesNotIncludedCorners) {
291  check_intersection({-6, 6, -3, 3}, {-3, 3, -6, 6}, {-3, 3, -3, 3}, 1.0 / 3);
292 }
293 */
294 
295 /*============================================================================*/
296 
297 int main (int argc, char *argv[]) {
298  ::testing::InitGoogleTest(&argc, argv);
299  return RUN_ALL_TESTS();
300 }
TEST_F(LWRIntersectionOverlapTest, noIntersectionSameSize)
bool right() const
Definition: directions.h:17
Directions & set_right()
Definition: directions.h:12
auto overlap(const LightWeightRectangle &that) const
int horz() const
Definition: directions.h:20
bool bot() const
Definition: directions.h:19
auto intersect(const LightWeightRectangle &that) const
bool left() const
Definition: directions.h:16
Directions & set_left()
Definition: directions.h:11
void check_intersection(const Rect &r1, const Rect &r2, const Rect &exp_inters, double exp_overlap) const
void test_corner_inclusion(const Directions &dirs) const
#define ALIGN_SIDE(side)
int main(int argc, char *argv[])
int vert() const
Definition: directions.h:24
Directions & set_bot()
Definition: directions.h:14
bool top() const
Definition: directions.h:18
void test_proper_inclusion(const Directions &dirs, double exp_outer_overlap) const
void test_edge_inclusion(const Directions &dir)
Directions & set_top()
Definition: directions.h:13


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25