| Flutter Impeller |

rect_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gtest/gtest.h"
6
7 #include "flutter/impeller/geometry/rect.h"
8
9 #include "flutter/impeller/geometry/geometry_asserts.h"
10
11 namespace impeller {
12 namespace testing {
13
14 TEST(RectTest, RectOriginSizeXYWHGetters) {
15 {
16 Rect r = Rect::MakeOriginSize({10, 20}, {50, 40});
17 EXPECT_EQ(r.GetOrigin(), Point(10, 20));
18 EXPECT_EQ(r.GetSize(), Size(50, 40));
19 EXPECT_EQ(r.GetX(), 10);
20 EXPECT_EQ(r.GetY(), 20);
21 EXPECT_EQ(r.GetWidth(), 50);
22 EXPECT_EQ(r.GetHeight(), 40);
23 auto expected_array = std::array<Scalar, 4>{10, 20, 50, 40};
24 EXPECT_EQ(r.GetXYWH(), expected_array);
25 }
26
27 {
28 Rect r = Rect::MakeLTRB(10, 20, 50, 40);
29 EXPECT_EQ(r.GetOrigin(), Point(10, 20));
30 EXPECT_EQ(r.GetSize(), Size(40, 20));
31 EXPECT_EQ(r.GetX(), 10);
32 EXPECT_EQ(r.GetY(), 20);
33 EXPECT_EQ(r.GetWidth(), 40);
34 EXPECT_EQ(r.GetHeight(), 20);
35 auto expected_array = std::array<Scalar, 4>{10, 20, 40, 20};
36 EXPECT_EQ(r.GetXYWH(), expected_array);
37 }
38 }
39
40 TEST(RectTest, IRectOriginSizeXYWHGetters) {
41 {
42 IRect r = IRect::MakeOriginSize({10, 20}, {50, 40});
43 EXPECT_EQ(r.GetOrigin(), IPoint(10, 20));
44 EXPECT_EQ(r.GetSize(), ISize(50, 40));
45 EXPECT_EQ(r.GetX(), 10);
46 EXPECT_EQ(r.GetY(), 20);
47 EXPECT_EQ(r.GetWidth(), 50);
48 EXPECT_EQ(r.GetHeight(), 40);
49 auto expected_array = std::array<int64_t, 4>{10, 20, 50, 40};
50 EXPECT_EQ(r.GetXYWH(), expected_array);
51 }
52
53 {
54 IRect r = IRect::MakeLTRB(10, 20, 50, 40);
55 EXPECT_EQ(r.GetOrigin(), IPoint(10, 20));
56 EXPECT_EQ(r.GetSize(), ISize(40, 20));
57 EXPECT_EQ(r.GetX(), 10);
58 EXPECT_EQ(r.GetY(), 20);
59 EXPECT_EQ(r.GetWidth(), 40);
60 EXPECT_EQ(r.GetHeight(), 20);
61 auto expected_array = std::array<int64_t, 4>{10, 20, 40, 20};
62 EXPECT_EQ(r.GetXYWH(), expected_array);
63 }
64 }
65
66 TEST(RectTest, RectFromRect) {
67 EXPECT_EQ(Rect(Rect::MakeXYWH(2, 3, 7, 15)),
68 Rect::MakeXYWH(2.0, 3.0, 7.0, 15.0));
69 EXPECT_EQ(Rect(Rect::MakeLTRB(2, 3, 7, 15)),
70 Rect::MakeLTRB(2.0, 3.0, 7.0, 15.0));
71 }
72
73 TEST(RectTest, RectFromIRect) {
74 EXPECT_EQ(Rect(IRect::MakeXYWH(2, 3, 7, 15)),
75 Rect::MakeXYWH(2.0, 3.0, 7.0, 15.0));
76 EXPECT_EQ(Rect(IRect::MakeLTRB(2, 3, 7, 15)),
77 Rect::MakeLTRB(2.0, 3.0, 7.0, 15.0));
78 }
79
80 TEST(RectTest, IRectFromRect) {
81 EXPECT_EQ(IRect(Rect::MakeXYWH(2, 3, 7, 15)), //
82 IRect::MakeXYWH(2, 3, 7, 15));
83 EXPECT_EQ(IRect(Rect::MakeLTRB(2, 3, 7, 15)), //
84 IRect::MakeLTRB(2, 3, 7, 15));
85
86 EXPECT_EQ(IRect(Rect::MakeXYWH(2.5, 3.5, 7.75, 15.75)),
87 IRect::MakeXYWH(2, 3, 7, 15));
88 EXPECT_EQ(IRect(Rect::MakeLTRB(2.5, 3.5, 7.75, 15.75)),
89 IRect::MakeLTRB(2, 3, 7, 15));
90 }
91
92 TEST(RectTest, IRectFromIRect) {
93 EXPECT_EQ(IRect(IRect::MakeXYWH(2, 3, 7, 15)), //
94 IRect::MakeXYWH(2, 3, 7, 15));
95 EXPECT_EQ(IRect(IRect::MakeLTRB(2, 3, 7, 15)), //
96 IRect::MakeLTRB(2, 3, 7, 15));
97 }
98
99 TEST(RectTest, RectMakeSize) {
100 {
101 Size s(100, 200);
102 Rect r = Rect::MakeSize(s);
103 Rect expected = Rect::MakeLTRB(0, 0, 100, 200);
104 EXPECT_RECT_NEAR(r, expected);
105 }
106
107 {
108 ISize s(100, 200);
109 Rect r = Rect::MakeSize(s);
110 Rect expected = Rect::MakeLTRB(0, 0, 100, 200);
111 EXPECT_RECT_NEAR(r, expected);
112 }
113
114 {
115 Size s(100, 200);
116 IRect r = IRect::MakeSize(s);
117 IRect expected = IRect::MakeLTRB(0, 0, 100, 200);
118 EXPECT_EQ(r, expected);
119 }
120
121 {
122 ISize s(100, 200);
123 IRect r = IRect::MakeSize(s);
124 IRect expected = IRect::MakeLTRB(0, 0, 100, 200);
125 EXPECT_EQ(r, expected);
126 }
127 }
128
129 TEST(RectTest, RectScale) {
130 auto test1 = [](Rect rect, Scalar scale) {
131 Rect expected = Rect::MakeXYWH(rect.GetX() * scale, //
132 rect.GetY() * scale, //
133 rect.GetWidth() * scale, //
134 rect.GetHeight() * scale);
135
136 EXPECT_RECT_NEAR(rect.Scale(scale), expected) //
137 << rect << " * " << scale;
138 EXPECT_RECT_NEAR(rect.Scale(scale, scale), expected) //
139 << rect << " * " << scale;
140 EXPECT_RECT_NEAR(rect.Scale(Point(scale, scale)), expected) //
141 << rect << " * " << scale;
142 EXPECT_RECT_NEAR(rect.Scale(Size(scale, scale)), expected) //
143 << rect << " * " << scale;
144 };
145
146 auto test2 = [&test1](Rect rect, Scalar scale_x, Scalar scale_y) {
147 Rect expected = Rect::MakeXYWH(rect.GetX() * scale_x, //
148 rect.GetY() * scale_y, //
149 rect.GetWidth() * scale_x, //
150 rect.GetHeight() * scale_y);
151
152 EXPECT_RECT_NEAR(rect.Scale(scale_x, scale_y), expected) //
153 << rect << " * " << scale_x << ", " << scale_y;
154 EXPECT_RECT_NEAR(rect.Scale(Point(scale_x, scale_y)), expected) //
155 << rect << " * " << scale_x << ", " << scale_y;
156 EXPECT_RECT_NEAR(rect.Scale(Size(scale_x, scale_y)), expected) //
157 << rect << " * " << scale_x << ", " << scale_y;
158
159 test1(rect, scale_x);
160 test1(rect, scale_y);
161 };
162
163 test2(Rect::MakeLTRB(10, 15, 100, 150), 2.5, 3.5);
164 test2(Rect::MakeLTRB(10, 15, 100, 150), 3.5, 2.5);
165 test2(Rect::MakeLTRB(10, 15, -100, 150), 2.5, 3.5);
166 test2(Rect::MakeLTRB(10, 15, 100, -150), 2.5, 3.5);
167 test2(Rect::MakeLTRB(10, 15, 100, 150), -2.5, 3.5);
168 test2(Rect::MakeLTRB(10, 15, 100, 150), 2.5, -3.5);
169 }
170
171 TEST(RectTest, IRectScale) {
172 auto test1 = [](IRect rect, int64_t scale) {
173 IRect expected = IRect::MakeXYWH(rect.GetX() * scale, //
174 rect.GetY() * scale, //
175 rect.GetWidth() * scale, //
176 rect.GetHeight() * scale);
177
178 EXPECT_EQ(rect.Scale(scale), expected) //
179 << rect << " * " << scale;
180 EXPECT_EQ(rect.Scale(scale, scale), expected) //
181 << rect << " * " << scale;
182 EXPECT_EQ(rect.Scale(IPoint(scale, scale)), expected) //
183 << rect << " * " << scale;
184 EXPECT_EQ(rect.Scale(ISize(scale, scale)), expected) //
185 << rect << " * " << scale;
186 };
187
188 auto test2 = [&test1](IRect rect, int64_t scale_x, int64_t scale_y) {
189 IRect expected = IRect::MakeXYWH(rect.GetX() * scale_x, //
190 rect.GetY() * scale_y, //
191 rect.GetWidth() * scale_x, //
192 rect.GetHeight() * scale_y);
193
194 EXPECT_EQ(rect.Scale(scale_x, scale_y), expected) //
195 << rect << " * " << scale_x << ", " << scale_y;
196 EXPECT_EQ(rect.Scale(IPoint(scale_x, scale_y)), expected) //
197 << rect << " * " << scale_x << ", " << scale_y;
198 EXPECT_EQ(rect.Scale(ISize(scale_x, scale_y)), expected) //
199 << rect << " * " << scale_x << ", " << scale_y;
200
201 test1(rect, scale_x);
202 test1(rect, scale_y);
203 };
204
205 test2(IRect::MakeLTRB(10, 15, 100, 150), 2, 3);
206 test2(IRect::MakeLTRB(10, 15, 100, 150), 3, 2);
207 test2(IRect::MakeLTRB(10, 15, -100, 150), 2, 3);
208 test2(IRect::MakeLTRB(10, 15, 100, -150), 2, 3);
209 test2(IRect::MakeLTRB(10, 15, 100, 150), -2, 3);
210 test2(IRect::MakeLTRB(10, 15, 100, 150), 2, -3);
211 }
212
213 TEST(RectTest, RectArea) {
214 EXPECT_EQ(Rect::MakeXYWH(0, 0, 100, 200).Area(), 20000);
215 EXPECT_EQ(Rect::MakeXYWH(10, 20, 100, 200).Area(), 20000);
216 EXPECT_EQ(Rect::MakeXYWH(0, 0, 200, 100).Area(), 20000);
217 EXPECT_EQ(Rect::MakeXYWH(10, 20, 200, 100).Area(), 20000);
218 EXPECT_EQ(Rect::MakeXYWH(0, 0, 100, 100).Area(), 10000);
219 EXPECT_EQ(Rect::MakeXYWH(10, 20, 100, 100).Area(), 10000);
220 }
221
222 TEST(RectTest, IRectArea) {
223 EXPECT_EQ(IRect::MakeXYWH(0, 0, 100, 200).Area(), 20000);
224 EXPECT_EQ(IRect::MakeXYWH(10, 20, 100, 200).Area(), 20000);
225 EXPECT_EQ(IRect::MakeXYWH(0, 0, 200, 100).Area(), 20000);
226 EXPECT_EQ(IRect::MakeXYWH(10, 20, 200, 100).Area(), 20000);
227 EXPECT_EQ(IRect::MakeXYWH(0, 0, 100, 100).Area(), 10000);
228 EXPECT_EQ(IRect::MakeXYWH(10, 20, 100, 100).Area(), 10000);
229 }
230
231 TEST(RectTest, RectGetNormalizingTransform) {
232 {
233 // Checks for expected matrix values
234
235 auto r = Rect::MakeXYWH(100, 200, 200, 400);
236
237 EXPECT_EQ(r.GetNormalizingTransform(),
238 Matrix::MakeScale({0.005, 0.0025, 1.0}) *
239 Matrix::MakeTranslation({-100, -200}));
240 }
241
242 {
243 // Checks for expected transform of points relative to the rect
244
245 auto r = Rect::MakeLTRB(300, 500, 400, 700);
246 auto m = r.GetNormalizingTransform();
247
248 // The 4 corners of the rect => (0, 0) to (1, 1)
249 EXPECT_EQ(m * Point(300, 500), Point(0, 0));
250 EXPECT_EQ(m * Point(400, 500), Point(1, 0));
251 EXPECT_EQ(m * Point(400, 700), Point(1, 1));
252 EXPECT_EQ(m * Point(300, 700), Point(0, 1));
253
254 // The center => (0.5, 0.5)
255 EXPECT_EQ(m * Point(350, 600), Point(0.5, 0.5));
256
257 // Outside the 4 corners => (-1, -1) to (2, 2)
258 EXPECT_EQ(m * Point(200, 300), Point(-1, -1));
259 EXPECT_EQ(m * Point(500, 300), Point(2, -1));
260 EXPECT_EQ(m * Point(500, 900), Point(2, 2));
261 EXPECT_EQ(m * Point(200, 900), Point(-1, 2));
262 }
263
264 {
265 // Checks for behavior with empty rects
266
267 auto zero = Matrix::MakeScale({0.0, 0.0, 1.0});
268
269 // Empty for width and/or height == 0
270 EXPECT_EQ(Rect::MakeXYWH(10, 10, 0, 10).GetNormalizingTransform(), zero);
271 EXPECT_EQ(Rect::MakeXYWH(10, 10, 10, 0).GetNormalizingTransform(), zero);
272 EXPECT_EQ(Rect::MakeXYWH(10, 10, 0, 0).GetNormalizingTransform(), zero);
273
274 // Empty for width and/or height < 0
275 EXPECT_EQ(Rect::MakeXYWH(10, 10, -1, 10).GetNormalizingTransform(), zero);
276 EXPECT_EQ(Rect::MakeXYWH(10, 10, 10, -1).GetNormalizingTransform(), zero);
277 EXPECT_EQ(Rect::MakeXYWH(10, 10, -1, -1).GetNormalizingTransform(), zero);
278 }
279
280 {
281 // Checks for behavior with non-finite rects
282
283 auto z = Matrix::MakeScale({0.0, 0.0, 1.0});
284 auto nan = std::numeric_limits<Scalar>::quiet_NaN();
285 auto inf = std::numeric_limits<Scalar>::infinity();
286
287 // Non-finite for width and/or height == nan
288 EXPECT_EQ(Rect::MakeXYWH(10, 10, nan, 10).GetNormalizingTransform(), z);
289 EXPECT_EQ(Rect::MakeXYWH(10, 10, 10, nan).GetNormalizingTransform(), z);
290 EXPECT_EQ(Rect::MakeXYWH(10, 10, nan, nan).GetNormalizingTransform(), z);
291
292 // Non-finite for width and/or height == inf
293 EXPECT_EQ(Rect::MakeXYWH(10, 10, inf, 10).GetNormalizingTransform(), z);
294 EXPECT_EQ(Rect::MakeXYWH(10, 10, 10, inf).GetNormalizingTransform(), z);
295 EXPECT_EQ(Rect::MakeXYWH(10, 10, inf, inf).GetNormalizingTransform(), z);
296
297 // Non-finite for width and/or height == -inf
298 EXPECT_EQ(Rect::MakeXYWH(10, 10, -inf, 10).GetNormalizingTransform(), z);
299 EXPECT_EQ(Rect::MakeXYWH(10, 10, 10, -inf).GetNormalizingTransform(), z);
300 EXPECT_EQ(Rect::MakeXYWH(10, 10, -inf, -inf).GetNormalizingTransform(), z);
301
302 // Non-finite for origin X and/or Y == nan
303 EXPECT_EQ(Rect::MakeXYWH(nan, 10, 10, 10).GetNormalizingTransform(), z);
304 EXPECT_EQ(Rect::MakeXYWH(10, nan, 10, 10).GetNormalizingTransform(), z);
305 EXPECT_EQ(Rect::MakeXYWH(nan, nan, 10, 10).GetNormalizingTransform(), z);
306
307 // Non-finite for origin X and/or Y == inf
308 EXPECT_EQ(Rect::MakeXYWH(inf, 10, 10, 10).GetNormalizingTransform(), z);
309 EXPECT_EQ(Rect::MakeXYWH(10, inf, 10, 10).GetNormalizingTransform(), z);
310 EXPECT_EQ(Rect::MakeXYWH(inf, inf, 10, 10).GetNormalizingTransform(), z);
311
312 // Non-finite for origin X and/or Y == -inf
313 EXPECT_EQ(Rect::MakeXYWH(-inf, 10, 10, 10).GetNormalizingTransform(), z);
314 EXPECT_EQ(Rect::MakeXYWH(10, -inf, 10, 10).GetNormalizingTransform(), z);
315 EXPECT_EQ(Rect::MakeXYWH(-inf, -inf, 10, 10).GetNormalizingTransform(), z);
316 }
317 }
318
319 TEST(RectTest, IRectGetNormalizingTransform) {
320 {
321 // Checks for expected matrix values
322
323 auto r = IRect::MakeXYWH(100, 200, 200, 400);
324
325 EXPECT_EQ(r.GetNormalizingTransform(),
326 Matrix::MakeScale({0.005, 0.0025, 1.0}) *
327 Matrix::MakeTranslation({-100, -200}));
328 }
329
330 {
331 // Checks for expected transform of points relative to the rect
332
333 auto r = IRect::MakeLTRB(300, 500, 400, 700);
334 auto m = r.GetNormalizingTransform();
335
336 // The 4 corners of the rect => (0, 0) to (1, 1)
337 EXPECT_EQ(m * Point(300, 500), Point(0, 0));
338 EXPECT_EQ(m * Point(400, 500), Point(1, 0));
339 EXPECT_EQ(m * Point(400, 700), Point(1, 1));
340 EXPECT_EQ(m * Point(300, 700), Point(0, 1));
341
342 // The center => (0.5, 0.5)
343 EXPECT_EQ(m * Point(350, 600), Point(0.5, 0.5));
344
345 // Outside the 4 corners => (-1, -1) to (2, 2)
346 EXPECT_EQ(m * Point(200, 300), Point(-1, -1));
347 EXPECT_EQ(m * Point(500, 300), Point(2, -1));
348 EXPECT_EQ(m * Point(500, 900), Point(2, 2));
349 EXPECT_EQ(m * Point(200, 900), Point(-1, 2));
350 }
351
352 {
353 // Checks for behavior with empty rects
354
355 auto zero = Matrix::MakeScale({0.0, 0.0, 1.0});
356
357 // Empty for width and/or height == 0
358 EXPECT_EQ(IRect::MakeXYWH(10, 10, 0, 10).GetNormalizingTransform(), zero);
359 EXPECT_EQ(IRect::MakeXYWH(10, 10, 10, 0).GetNormalizingTransform(), zero);
360 EXPECT_EQ(IRect::MakeXYWH(10, 10, 0, 0).GetNormalizingTransform(), zero);
361
362 // Empty for width and/or height < 0
363 EXPECT_EQ(IRect::MakeXYWH(10, 10, -1, 10).GetNormalizingTransform(), zero);
364 EXPECT_EQ(IRect::MakeXYWH(10, 10, 10, -1).GetNormalizingTransform(), zero);
365 EXPECT_EQ(IRect::MakeXYWH(10, 10, -1, -1).GetNormalizingTransform(), zero);
366 }
367 }
368
369 TEST(SizeTest, RectIsEmpty) {
370 auto nan = std::numeric_limits<Scalar>::quiet_NaN();
371
372 // Non-empty
373 EXPECT_FALSE(Rect::MakeXYWH(1.5, 2.3, 10.5, 7.2).IsEmpty());
374
375 // Empty both width and height both 0 or negative, in all combinations
376 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, 0.0, 0.0).IsEmpty());
377 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, -1.0, -1.0).IsEmpty());
378 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, 0.0, -1.0).IsEmpty());
379 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, -1.0, 0.0).IsEmpty());
380
381 // Empty for 0 or negative width or height (but not both at the same time)
382 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, 10.5, 0.0).IsEmpty());
383 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, 10.5, -1.0).IsEmpty());
384 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, 0.0, 7.2).IsEmpty());
385 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, -1.0, 7.2).IsEmpty());
386
387 // Empty for NaN in width or height or both
388 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, 10.5, nan).IsEmpty());
389 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, nan, 7.2).IsEmpty());
390 EXPECT_TRUE(Rect::MakeXYWH(1.5, 2.3, nan, nan).IsEmpty());
391 }
392
393 TEST(SizeTest, IRectIsEmpty) {
394 // Non-empty
395 EXPECT_FALSE(IRect::MakeXYWH(1, 2, 10, 7).IsEmpty());
396
397 // Empty both width and height both 0 or negative, in all combinations
398 EXPECT_TRUE(IRect::MakeXYWH(1, 2, 0, 0).IsEmpty());
399 EXPECT_TRUE(IRect::MakeXYWH(1, 2, -1, -1).IsEmpty());
400 EXPECT_TRUE(IRect::MakeXYWH(1, 2, -1, 0).IsEmpty());
401 EXPECT_TRUE(IRect::MakeXYWH(1, 2, 0, -1).IsEmpty());
402
403 // Empty for 0 or negative width or height (but not both at the same time)
404 EXPECT_TRUE(IRect::MakeXYWH(1, 2, 10, 0).IsEmpty());
405 EXPECT_TRUE(IRect::MakeXYWH(1, 2, 10, -1).IsEmpty());
406 EXPECT_TRUE(IRect::MakeXYWH(1, 2, 0, 7).IsEmpty());
407 EXPECT_TRUE(IRect::MakeXYWH(1, 2, -1, 7).IsEmpty());
408 }
409
410 TEST(RectTest, MakePointBoundsQuad) {
411 Quad quad = {
412 Point(10, 10),
413 Point(20, 10),
414 Point(10, 20),
415 Point(20, 20),
416 };
417 std::optional<Rect> bounds = Rect::MakePointBounds(quad);
418 EXPECT_TRUE(bounds.has_value());
419 if (bounds.has_value()) {
420 EXPECT_TRUE(RectNear(bounds.value(), Rect::MakeLTRB(10, 10, 20, 20)));
421 }
422 }
423
424 TEST(RectTest, IsSquare) {
425 EXPECT_TRUE(Rect::MakeXYWH(10, 30, 20, 20).IsSquare());
426 EXPECT_FALSE(Rect::MakeXYWH(10, 30, 20, 19).IsSquare());
427 EXPECT_FALSE(Rect::MakeXYWH(10, 30, 19, 20).IsSquare());
428
429 EXPECT_TRUE(IRect::MakeXYWH(10, 30, 20, 20).IsSquare());
430 EXPECT_FALSE(IRect::MakeXYWH(10, 30, 20, 19).IsSquare());
431 EXPECT_FALSE(IRect::MakeXYWH(10, 30, 19, 20).IsSquare());
432 }
433
434 TEST(RectTest, GetCenter) {
435 EXPECT_EQ(Rect::MakeXYWH(10, 30, 20, 20).GetCenter(), Point(20, 40));
436 EXPECT_EQ(Rect::MakeXYWH(10, 30, 20, 19).GetCenter(), Point(20, 39.5));
437
438 // Note that we expect a Point as the answer from an IRect
439 EXPECT_EQ(IRect::MakeXYWH(10, 30, 20, 20).GetCenter(), Point(20, 40));
440 EXPECT_EQ(IRect::MakeXYWH(10, 30, 20, 19).GetCenter(), Point(20, 39.5));
441 }
442
443 TEST(RectTest, RectExpand) {
444 auto rect = Rect::MakeLTRB(100, 100, 200, 200);
445
446 // Expand(T amount)
447 EXPECT_EQ(rect.Expand(10), Rect::MakeLTRB(90, 90, 210, 210));
448 EXPECT_EQ(rect.Expand(-10), Rect::MakeLTRB(110, 110, 190, 190));
449
450 // Expand(amount, amount)
451 EXPECT_EQ(rect.Expand(10, 10), Rect::MakeLTRB(90, 90, 210, 210));
452 EXPECT_EQ(rect.Expand(10, -10), Rect::MakeLTRB(90, 110, 210, 190));
453 EXPECT_EQ(rect.Expand(-10, 10), Rect::MakeLTRB(110, 90, 190, 210));
454 EXPECT_EQ(rect.Expand(-10, -10), Rect::MakeLTRB(110, 110, 190, 190));
455
456 // Expand(Point amount)
457 EXPECT_EQ(rect.Expand(Point{10, 10}), Rect::MakeLTRB(90, 90, 210, 210));
458 EXPECT_EQ(rect.Expand(Point{10, -10}), Rect::MakeLTRB(90, 110, 210, 190));
459 EXPECT_EQ(rect.Expand(Point{-10, 10}), Rect::MakeLTRB(110, 90, 190, 210));
460 EXPECT_EQ(rect.Expand(Point{-10, -10}), Rect::MakeLTRB(110, 110, 190, 190));
461
462 // Expand(Size amount)
463 EXPECT_EQ(rect.Expand(Size{10, 10}), Rect::MakeLTRB(90, 90, 210, 210));
464 EXPECT_EQ(rect.Expand(Size{10, -10}), Rect::MakeLTRB(90, 110, 210, 190));
465 EXPECT_EQ(rect.Expand(Size{-10, 10}), Rect::MakeLTRB(110, 90, 190, 210));
466 EXPECT_EQ(rect.Expand(Size{-10, -10}), Rect::MakeLTRB(110, 110, 190, 190));
467 }
468
469 TEST(RectTest, IRectExpand) {
470 auto rect = IRect::MakeLTRB(100, 100, 200, 200);
471
472 // Expand(T amount)
473 EXPECT_EQ(rect.Expand(10), IRect::MakeLTRB(90, 90, 210, 210));
474 EXPECT_EQ(rect.Expand(-10), IRect::MakeLTRB(110, 110, 190, 190));
475
476 // Expand(amount, amount)
477 EXPECT_EQ(rect.Expand(10, 10), IRect::MakeLTRB(90, 90, 210, 210));
478 EXPECT_EQ(rect.Expand(10, -10), IRect::MakeLTRB(90, 110, 210, 190));
479 EXPECT_EQ(rect.Expand(-10, 10), IRect::MakeLTRB(110, 90, 190, 210));
480 EXPECT_EQ(rect.Expand(-10, -10), IRect::MakeLTRB(110, 110, 190, 190));
481
482 // Expand(IPoint amount)
483 EXPECT_EQ(rect.Expand(IPoint{10, 10}), IRect::MakeLTRB(90, 90, 210, 210));
484 EXPECT_EQ(rect.Expand(IPoint{10, -10}), IRect::MakeLTRB(90, 110, 210, 190));
485 EXPECT_EQ(rect.Expand(IPoint{-10, 10}), IRect::MakeLTRB(110, 90, 190, 210));
486 EXPECT_EQ(rect.Expand(IPoint{-10, -10}), IRect::MakeLTRB(110, 110, 190, 190));
487
488 // Expand(ISize amount)
489 EXPECT_EQ(rect.Expand(ISize{10, 10}), IRect::MakeLTRB(90, 90, 210, 210));
490 EXPECT_EQ(rect.Expand(ISize{10, -10}), IRect::MakeLTRB(90, 110, 210, 190));
491 EXPECT_EQ(rect.Expand(ISize{-10, 10}), IRect::MakeLTRB(110, 90, 190, 210));
492 EXPECT_EQ(rect.Expand(ISize{-10, -10}), IRect::MakeLTRB(110, 110, 190, 190));
493 }
494
495 TEST(RectTest, ContainsFloatingPoint) {
496 auto rect1 =
497 Rect::MakeXYWH(472.599945f, 440.999969f, 1102.80005f, 654.000061f);
498 auto rect2 = Rect::MakeXYWH(724.f, 618.f, 600.f, 300.f);
499 EXPECT_TRUE(rect1.Contains(rect2));
500 }
501
502 } // namespace testing
503 } // namespace impeller
impeller::Scalar
float Scalar
Definition: scalar.h:18
geometry_asserts.h
impeller::TRect< Scalar >::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:34
impeller::TRect::GetX
constexpr Type GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
Definition: rect.h:163
impeller::TRect::GetHeight
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:175
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::TRect::GetOrigin
constexpr TPoint< Type > GetOrigin() const
Returns the upper left corner of the rectangle as specified when it was constructed.
Definition: rect.h:154
EXPECT_RECT_NEAR
#define EXPECT_RECT_NEAR(a, b)
Definition: geometry_asserts.h:170
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:49
impeller::TSize< Scalar >
impeller::Point
TPoint< Scalar > Point
Definition: point.h:308
impeller::Quad
std::array< Point, 4 > Quad
Definition: point.h:313
impeller::TRect::GetWidth
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:171
impeller::TRect< Scalar >::MakeOriginSize
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:38
impeller::testing::TEST
TEST(CanvasRecorder, Save)
Definition: canvas_recorder_unittests.cc:61
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:108
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:488
impeller::IRect
TRect< int64_t > IRect
Definition: rect.h:489
impeller::TRect::GetSize
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle as specified when it was constructed and which may be negative in e...
Definition: rect.h:159
RectNear
inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b)
Definition: geometry_asserts.h:56
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
rect.h
impeller::TPoint< Scalar >
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:27
impeller::TRect::GetY
constexpr Type GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
Definition: rect.h:167
impeller
Definition: aiks_context.cc:10
impeller::TRect::GetXYWH
constexpr std::array< T, 4 > GetXYWH() const
Get the x, y coordinates of the origin and the width and height of the rectangle in an array.
Definition: rect.h:235
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::TRect< Scalar >