Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
rect.h
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#ifndef FLUTTER_IMPELLER_GEOMETRY_RECT_H_
6#define FLUTTER_IMPELLER_GEOMETRY_RECT_H_
7
8#include <array>
9#include <optional>
10#include <ostream>
11#include <vector>
12
13#include "fml/logging.h"
19
20namespace impeller {
21
22#define ONLY_ON_FLOAT_M(Modifiers, Return) \
23 template <typename U = T> \
24 Modifiers std::enable_if_t<std::is_floating_point_v<U>, Return>
25#define ONLY_ON_FLOAT(Return) DL_ONLY_ON_FLOAT_M(, Return)
26
27/// Templated struct for holding an axis-aligned rectangle.
28///
29/// Rectangles are defined as 4 axis-aligned edges that might contain
30/// space. They can be viewed as 2 X coordinates that define the
31/// left and right edges and 2 Y coordinates that define the top and
32/// bottom edges; or they can be viewed as an origin and horizontal
33/// and vertical dimensions (width and height).
34///
35/// When the left and right edges are equal or reversed (right <= left)
36/// or the top and bottom edges are equal or reversed (bottom <= top),
37/// the rectangle is considered empty. Considering the rectangle in XYWH
38/// form, the width and/or the height would be negative or zero. Such
39/// reversed/empty rectangles contain no space and act as such in the
40/// methods that operate on them (Intersection, Union, IntersectsWithRect,
41/// Contains, Cutout, etc.)
42///
43/// Rectangles cannot be modified by any method and a new value can only
44/// be stored into an existing rect using assignment. This keeps the API
45/// clean compared to implementations that might have similar methods
46/// that produce the answer in place, or construct a new object with
47/// the answer, or place the result in an indicated result object.
48///
49/// Methods that might fail to produce an answer will use |std::optional|
50/// to indicate that success or failure (see |Intersection| and |CutOut|).
51/// For convenience, |Intersection| and |Union| both have overloaded
52/// variants that take |std::optional| arguments and treat them as if
53/// the argument was an empty rect to allow chaining multiple such methods
54/// and only needing to check the optional condition of the final result.
55/// The primary methods also provide |...OrEmpty| overloaded variants that
56/// translate an empty optional answer into a simple empty rectangle of the
57/// same type.
58///
59/// Rounding instance methods are not provided as the return value might
60/// be wanted as another floating point rectangle or sometimes as an integer
61/// rectangle. Instead a |RoundOut| factory, defined only for floating point
62/// input rectangles, is provided to provide control over the result type.
63///
64/// NaN and Infinity values
65///
66/// Constructing an LTRB rectangle using Infinity values should work as
67/// expected with either 0 or +Infinity returned as dimensions depending on
68/// which side the Infinity values are on and the sign.
69///
70/// Constructing an XYWH rectangle using Infinity values will usually
71/// not work if the math requires the object to compute a right or bottom
72/// edge from ([xy] -Infinity + [wh] +Infinity). Other combinations might
73/// work.
74///
75/// The special factory |MakeMaximum| is provided to construct a rectangle
76/// of the indicated coordinate type that covers all finite coordinates.
77/// It does not use infinity values, but rather the largest finite values
78/// to avoid math that might produce a NaN value from various getters.
79///
80/// Any rectangle that is constructed with, or computed to have a NaN value
81/// will be considered the same as any empty rectangle.
82///
83/// Empty Rectangle canonical results summary:
84///
85/// Union will ignore any empty rects and return the other rect
86/// Intersection will return nullopt if either rect is empty
87/// IntersectsWithRect will return false if either rect is empty
88/// Cutout will return the source rect if the argument is empty
89/// Cutout will return nullopt if the source rectangle is empty
90/// Contains(Point) will return false if the source rectangle is empty
91/// Contains(Rect) will return false if the source rectangle is empty
92/// Contains(Rect) will otherwise return true if the argument is empty
93/// Specifically, EmptyRect.Contains(EmptyRect) returns false
94///
95/// ---------------
96/// Special notes on problems using the XYWH form of specifying rectangles:
97///
98/// It is possible to have integer rectangles whose dimensions exceed
99/// the maximum number that their coordinates can represent since
100/// (MAX_INT - MIN_INT) overflows the representable positive numbers.
101/// Floating point rectangles technically have a similar issue in that
102/// overflow can occur, but it will be automatically converted into
103/// either an infinity, or a finite-overflow value and still be
104/// representable, just with little to no precision.
105///
106/// Secondly, specifying a rectangle using XYWH leads to cases where the
107/// math for (x+w) and/or (y+h) are also beyond the maximum representable
108/// coordinates. For N-bit integer rectangles declared as XYWH, the
109/// maximum right coordinate will require N+1 signed bits which cannot be
110/// stored in storage that uses N-bit integers.
111///
112/// Saturated math is used when constructing a rectangle from XYWH values
113/// and when returning the dimensions of the rectangle. Constructing an
114/// integer rectangle from values such that xy + wh is beyond the range
115/// of the integer type will place the right or bottom edges at the maximum
116/// value for the integer type. Similarly, constructing an integer rectangle
117/// such that the distance from the left to the right (or top to bottom) is
118/// greater than the range of the integer type will simply return the
119/// maximum integer value as the dimension. Floating point rectangles are
120/// naturally saturated by the rules of IEEE arithmetic.
121template <class T>
122struct TRect {
123 private:
124 using Type = T;
125
126 public:
127 constexpr TRect() : left_(0), top_(0), right_(0), bottom_(0) {}
128
129 constexpr static TRect MakeLTRB(Type left,
130 Type top,
131 Type right,
132 Type bottom) {
133 return TRect(left, top, right, bottom);
134 }
135
136 constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height) {
137 return TRect(x, y, saturated::Add(x, width), saturated::Add(y, height));
138 }
139
140 constexpr static TRect MakeOriginSize(const TPoint<Type>& origin,
141 const TSize<Type>& size) {
142 return MakeXYWH(origin.x, origin.y, size.width, size.height);
143 }
144
145 template <class U>
146 constexpr static TRect MakeSize(const TSize<U>& size) {
147 return TRect(0.0, 0.0, size.width, size.height);
148 }
149
150 template <typename U>
151 constexpr static std::optional<TRect> MakePointBounds(const U& value) {
152 return MakePointBounds(value.begin(), value.end());
153 }
154
155 template <typename PointIter>
156 constexpr static std::optional<TRect> MakePointBounds(const PointIter first,
157 const PointIter last) {
158 if (first == last) {
159 return std::nullopt;
160 }
161 auto left = first->x;
162 auto top = first->y;
163 auto right = first->x;
164 auto bottom = first->y;
165 for (auto it = first + 1; it < last; ++it) {
166 left = std::min(left, it->x);
167 top = std::min(top, it->y);
168 right = std::max(right, it->x);
169 bottom = std::max(bottom, it->y);
170 }
171 return TRect::MakeLTRB(left, top, right, bottom);
172 }
173
174 [[nodiscard]] constexpr static TRect MakeMaximum() {
175 return TRect::MakeLTRB(std::numeric_limits<Type>::lowest(),
176 std::numeric_limits<Type>::lowest(),
177 std::numeric_limits<Type>::max(),
178 std::numeric_limits<Type>::max());
179 }
180
181 [[nodiscard]] constexpr bool operator==(const TRect& r) const {
182 return left_ == r.left_ && //
183 top_ == r.top_ && //
184 right_ == r.right_ && //
185 bottom_ == r.bottom_;
186 }
187
188 [[nodiscard]] constexpr TRect Scale(Type scale) const {
189 return TRect(left_ * scale, //
190 top_ * scale, //
191 right_ * scale, //
192 bottom_ * scale);
193 }
194
195 [[nodiscard]] constexpr TRect Scale(Type scale_x, Type scale_y) const {
196 return TRect(left_ * scale_x, //
197 top_ * scale_y, //
198 right_ * scale_x, //
199 bottom_ * scale_y);
200 }
201
202 [[nodiscard]] constexpr TRect Scale(TPoint<T> scale) const {
203 return Scale(scale.x, scale.y);
204 }
205
206 [[nodiscard]] constexpr TRect Scale(TSize<T> scale) const {
207 return Scale(scale.width, scale.height);
208 }
209
210 /// @brief Returns true iff the provided point |p| is inside the
211 /// half-open interior of this rectangle.
212 ///
213 /// For purposes of containment, a rectangle contains points
214 /// along the top and left edges but not points along the
215 /// right and bottom edges so that a point is only ever
216 /// considered inside one of two abutting rectangles.
217 [[nodiscard]] constexpr bool Contains(const TPoint<Type>& p) const {
218 return !this->IsEmpty() && //
219 p.x >= left_ && //
220 p.y >= top_ && //
221 p.x < right_ && //
222 p.y < bottom_;
223 }
224
225 /// @brief Returns true iff this rectangle is not empty and it also
226 /// contains every point considered inside the provided
227 /// rectangle |o| (as determined by |Contains(TPoint)|).
228 ///
229 /// This is similar to a definition where the result is true iff
230 /// the union of the two rectangles is equal to this rectangle,
231 /// ignoring precision issues with performing those operations
232 /// and assuming that empty rectangles are never equal.
233 ///
234 /// An empty rectangle can contain no other rectangle.
235 ///
236 /// An empty rectangle is, however, contained within any
237 /// other non-empy rectangle as the set of points it contains
238 /// is an empty set and so there are no points to fail the
239 /// containment criteria.
240 [[nodiscard]] constexpr bool Contains(const TRect& o) const {
241 return !this->IsEmpty() && //
242 (o.IsEmpty() || (o.left_ >= left_ && //
243 o.top_ >= top_ && //
244 o.right_ <= right_ && //
245 o.bottom_ <= bottom_));
246 }
247
248 /// @brief Returns true if all of the fields of this floating point
249 /// rectangle are finite.
250 ///
251 /// Note that the results of |GetWidth()| and |GetHeight()| may
252 /// still be infinite due to overflow even if the fields themselves
253 /// are finite.
254 ONLY_ON_FLOAT_M([[nodiscard]] constexpr, bool)
255 IsFinite() const {
256 return std::isfinite(left_) && //
257 std::isfinite(top_) && //
258 std::isfinite(right_) && //
259 std::isfinite(bottom_);
260 }
261
262 /// @brief Returns true if either of the width or height are 0, negative,
263 /// or NaN.
264 [[nodiscard]] constexpr bool IsEmpty() const {
265 // Computing the non-empty condition and negating the result causes any
266 // NaN value to return true - i.e. is considered empty.
267 return !(left_ < right_ && top_ < bottom_);
268 }
269
270 /// @brief Returns true if width and height are equal and neither is NaN.
271 [[nodiscard]] constexpr bool IsSquare() const {
272 // empty rectangles can technically be "square", but would be
273 // misleading to most callers. Using |IsEmpty| also prevents
274 // "non-empty and non-overflowing" computations from happening
275 // to be equal to "empty and overflowing" results.
276 // (Consider LTRB(10, 15, MAX-2, MIN+2) which is empty, but both
277 // w/h subtractions equal "5").
278 return !IsEmpty() && (right_ - left_) == (bottom_ - top_);
279 }
280
281 [[nodiscard]] constexpr bool IsMaximum() const {
282 return *this == MakeMaximum();
283 }
284
285 /// @brief Returns the upper left corner of the rectangle as specified
286 /// by the left/top or x/y values when it was constructed.
287 [[nodiscard]] constexpr TPoint<Type> GetOrigin() const {
288 return {left_, top_};
289 }
290
291 /// @brief Returns the size of the rectangle which may be negative in
292 /// either width or height and may have been clipped to the
293 /// maximum integer values for integer rects whose size overflows.
294 [[nodiscard]] constexpr TSize<Type> GetSize() const {
295 return {GetWidth(), GetHeight()};
296 }
297
298 /// @brief Returns the X coordinate of the upper left corner, equivalent
299 /// to |GetOrigin().x|
300 [[nodiscard]] constexpr Type GetX() const { return left_; }
301
302 /// @brief Returns the Y coordinate of the upper left corner, equivalent
303 /// to |GetOrigin().y|
304 [[nodiscard]] constexpr Type GetY() const { return top_; }
305
306 /// @brief Returns the width of the rectangle, equivalent to
307 /// |GetSize().width|
308 [[nodiscard]] constexpr Type GetWidth() const {
309 return saturated::Sub(right_, left_);
310 }
311
312 /// @brief Returns the height of the rectangle, equivalent to
313 /// |GetSize().height|
314 [[nodiscard]] constexpr Type GetHeight() const {
315 return saturated::Sub(bottom_, top_);
316 }
317
318 [[nodiscard]] constexpr auto GetLeft() const { return left_; }
319
320 [[nodiscard]] constexpr auto GetTop() const { return top_; }
321
322 [[nodiscard]] constexpr auto GetRight() const { return right_; }
323
324 [[nodiscard]] constexpr auto GetBottom() const { return bottom_; }
325
326 [[nodiscard]] constexpr TPoint<T> GetLeftTop() const { //
327 return {left_, top_};
328 }
329
330 [[nodiscard]] constexpr TPoint<T> GetRightTop() const {
331 return {right_, top_};
332 }
333
334 [[nodiscard]] constexpr TPoint<T> GetLeftBottom() const {
335 return {left_, bottom_};
336 }
337
338 [[nodiscard]] constexpr TPoint<T> GetRightBottom() const {
339 return {right_, bottom_};
340 }
341
342 /// @brief Get the area of the rectangle, equivalent to |GetSize().Area()|
343 [[nodiscard]] constexpr T Area() const {
344 // TODO(flutter/flutter#141710) - Use saturated math to avoid overflow
345 // https://github.com/flutter/flutter/issues/141710
346 return IsEmpty() ? 0 : (right_ - left_) * (bottom_ - top_);
347 }
348
349 /// @brief Get the center point as a |Point|.
350 [[nodiscard]] constexpr Point GetCenter() const {
351 return {saturated::AverageScalar(left_, right_),
352 saturated::AverageScalar(top_, bottom_)};
353 }
354
355 [[nodiscard]] constexpr std::array<T, 4> GetLTRB() const {
356 return {left_, top_, right_, bottom_};
357 }
358
359 /// @brief Get the x, y coordinates of the origin and the width and
360 /// height of the rectangle in an array.
361 [[nodiscard]] constexpr std::array<T, 4> GetXYWH() const {
362 return {left_, top_, GetWidth(), GetHeight()};
363 }
364
365 /// @brief Get a version of this rectangle that has a non-negative size.
366 [[nodiscard]] constexpr TRect GetPositive() const {
367 if (!IsEmpty()) {
368 return *this;
369 }
370 return {
371 std::min(left_, right_),
372 std::min(top_, bottom_),
373 std::max(left_, right_),
374 std::max(top_, bottom_),
375 };
376 }
377
378 /// @brief Get the points that represent the 4 corners of this rectangle
379 /// in a Z order that is compatible with triangle strips or a set
380 /// of all zero points if the rectangle is empty.
381 /// The order is: Top left, top right, bottom left, bottom right.
382 [[nodiscard]] constexpr std::array<TPoint<T>, 4> GetPoints() const {
383 if (IsEmpty()) {
384 return {};
385 }
386 return {
387 TPoint{left_, top_},
388 TPoint{right_, top_},
389 TPoint{left_, bottom_},
390 TPoint{right_, bottom_},
391 };
392 }
393
394 [[nodiscard]] constexpr std::array<TPoint<T>, 4> GetTransformedPoints(
395 const Matrix& transform) const {
396 auto points = GetPoints();
397 for (size_t i = 0; i < points.size(); i++) {
398 points[i] = transform * points[i];
399 }
400 return points;
401 }
402
403 /// @brief Creates a new bounding box that contains this transformed
404 /// rectangle, clipped against the near clipping plane if
405 /// necessary.
406 [[nodiscard]] constexpr TRect TransformAndClipBounds(
407 const Matrix& transform) const {
408 if (!transform.HasPerspective2D()) {
410 }
411
412 if (IsEmpty()) {
413 return {};
414 }
415
416 auto ul = transform.TransformHomogenous({left_, top_});
417 auto ur = transform.TransformHomogenous({right_, top_});
418 auto ll = transform.TransformHomogenous({left_, bottom_});
419 auto lr = transform.TransformHomogenous({right_, bottom_});
420
421 // It can probably be proven that we only ever have 5 points at most
422 // which happens when only 1 corner is clipped and we get 2 points
423 // in return for it as we interpolate against its neighbors.
424 Point points[8];
425 int index = 0;
426
427 // Process (clip and interpolate) each point against its 2 neighbors:
428 // left, pt, right
429 index = ClipAndInsert(points, index, ll, ul, ur);
430 index = ClipAndInsert(points, index, ul, ur, lr);
431 index = ClipAndInsert(points, index, ur, lr, ll);
432 index = ClipAndInsert(points, index, lr, ll, ul);
433
434 auto bounds = TRect::MakePointBounds(points, points + index);
435 return bounds.value_or(TRect{});
436 }
437
438 /// @brief Creates a new bounding box that contains this transformed
439 /// rectangle.
440 [[nodiscard]] constexpr TRect TransformBounds(const Matrix& transform) const {
441 if (IsEmpty()) {
442 return {};
443 }
445 auto bounds = TRect::MakePointBounds(points.begin(), points.end());
446 if (bounds.has_value()) {
447 return bounds.value();
448 }
450 }
451
452 /// @brief Constructs a Matrix that will map all points in the coordinate
453 /// space of the rectangle into a new normalized coordinate space
454 /// where the upper left corner of the rectangle maps to (0, 0)
455 /// and the lower right corner of the rectangle maps to (1, 1).
456 ///
457 /// Empty and non-finite rectangles will return a zero-scaling
458 /// transform that maps all points to (0, 0).
459 [[nodiscard]] constexpr Matrix GetNormalizingTransform() const {
460 if (!IsEmpty()) {
461 Scalar sx = 1.0 / GetWidth();
462 Scalar sy = 1.0 / GetHeight();
463 Scalar tx = left_ * -sx;
464 Scalar ty = top_ * -sy;
465
466 // Exclude NaN and infinities and either scale underflowing to zero
467 if (sx != 0.0 && sy != 0.0 && 0.0 * sx * sy * tx * ty == 0.0) {
468 // clang-format off
469 return Matrix( sx, 0.0f, 0.0f, 0.0f,
470 0.0f, sy, 0.0f, 0.0f,
471 0.0f, 0.0f, 1.0f, 0.0f,
472 tx, ty, 0.0f, 1.0f);
473 // clang-format on
474 }
475 }
476
477 // Map all coordinates to the origin.
478 return Matrix::MakeScale({0.0f, 0.0f, 1.0f});
479 }
480
481 [[nodiscard]] constexpr TRect Union(const TRect& o) const {
482 if (IsEmpty()) {
483 return o;
484 }
485 if (o.IsEmpty()) {
486 return *this;
487 }
488 return {
489 std::min(left_, o.left_),
490 std::min(top_, o.top_),
491 std::max(right_, o.right_),
492 std::max(bottom_, o.bottom_),
493 };
494 }
495
496 [[nodiscard]] constexpr std::optional<TRect> Intersection(
497 const TRect& o) const {
498 if (IntersectsWithRect(o)) {
499 return TRect{
500 std::max(left_, o.left_),
501 std::max(top_, o.top_),
502 std::min(right_, o.right_),
503 std::min(bottom_, o.bottom_),
504 };
505 } else {
506 return std::nullopt;
507 }
508 }
509
510 [[nodiscard]] constexpr bool IntersectsWithRect(const TRect& o) const {
511 return !IsEmpty() && //
512 !o.IsEmpty() && //
513 left_ < o.right_ && //
514 top_ < o.bottom_ && //
515 right_ > o.left_ && //
516 bottom_ > o.top_;
517 }
518
519 /// @brief Returns the new boundary rectangle that would result from this
520 /// rectangle being cut out by the specified rectangle.
521 [[nodiscard]] constexpr std::optional<TRect<T>> Cutout(const TRect& o) const {
522 if (IsEmpty()) {
523 // This test isn't just a short-circuit, it also prevents the concise
524 // math below from returning the wrong answer on empty rects.
525 // Once we know that this rectangle is not empty, the math below can
526 // only succeed in computing a value if o is also non-empty and non-nan.
527 // Otherwise, the method returns *this by default.
528 return std::nullopt;
529 }
530
531 const auto& [a_left, a_top, a_right, a_bottom] = GetLTRB(); // Source rect.
532 const auto& [b_left, b_top, b_right, b_bottom] = o.GetLTRB(); // Cutout.
533 if (b_left <= a_left && b_right >= a_right) {
534 if (b_top <= a_top && b_bottom >= a_bottom) {
535 // Full cutout.
536 return std::nullopt;
537 }
538 if (b_top <= a_top && b_bottom > a_top) {
539 // Cuts off the top.
540 return TRect::MakeLTRB(a_left, b_bottom, a_right, a_bottom);
541 }
542 if (b_bottom >= a_bottom && b_top < a_bottom) {
543 // Cuts off the bottom.
544 return TRect::MakeLTRB(a_left, a_top, a_right, b_top);
545 }
546 }
547 if (b_top <= a_top && b_bottom >= a_bottom) {
548 if (b_left <= a_left && b_right > a_left) {
549 // Cuts off the left.
550 return TRect::MakeLTRB(b_right, a_top, a_right, a_bottom);
551 }
552 if (b_right >= a_right && b_left < a_right) {
553 // Cuts off the right.
554 return TRect::MakeLTRB(a_left, a_top, b_left, a_bottom);
555 }
556 }
557
558 return *this;
559 }
560
561 [[nodiscard]] constexpr TRect CutoutOrEmpty(const TRect& o) const {
562 return Cutout(o).value_or(TRect());
563 }
564
565 /// @brief Returns a new rectangle translated by the given offset.
566 [[nodiscard]] constexpr TRect<T> Shift(T dx, T dy) const {
567 return {
568 saturated::Add(left_, dx), //
569 saturated::Add(top_, dy), //
570 saturated::Add(right_, dx), //
571 saturated::Add(bottom_, dy), //
572 };
573 }
574
575 /// @brief Returns a new rectangle translated by the given offset.
576 [[nodiscard]] constexpr TRect<T> Shift(TPoint<T> offset) const {
577 return Shift(offset.x, offset.y);
578 }
579
580 /// @brief Returns a rectangle with expanded edges. Negative expansion
581 /// results in shrinking.
582 [[nodiscard]] constexpr TRect<T> Expand(T left,
583 T top,
584 T right,
585 T bottom) const {
586 return {
587 saturated::Sub(left_, left), //
588 saturated::Sub(top_, top), //
589 saturated::Add(right_, right), //
590 saturated::Add(bottom_, bottom), //
591 };
592 }
593
594 /// @brief Returns a rectangle with expanded edges in all directions.
595 /// Negative expansion results in shrinking.
596 [[nodiscard]] constexpr TRect<T> Expand(T amount) const {
597 return {
598 saturated::Sub(left_, amount), //
599 saturated::Sub(top_, amount), //
600 saturated::Add(right_, amount), //
601 saturated::Add(bottom_, amount), //
602 };
603 }
604
605 /// @brief Returns a rectangle with expanded edges in all directions.
606 /// Negative expansion results in shrinking.
607 [[nodiscard]] constexpr TRect<T> Expand(T horizontal_amount,
608 T vertical_amount) const {
609 return {
610 saturated::Sub(left_, horizontal_amount), //
611 saturated::Sub(top_, vertical_amount), //
612 saturated::Add(right_, horizontal_amount), //
613 saturated::Add(bottom_, vertical_amount), //
614 };
615 }
616
617 /// @brief Returns a rectangle with expanded edges in all directions.
618 /// Negative expansion results in shrinking.
619 [[nodiscard]] constexpr TRect<T> Expand(TPoint<T> amount) const {
620 return Expand(amount.x, amount.y);
621 }
622
623 /// @brief Returns a rectangle with expanded edges in all directions.
624 /// Negative expansion results in shrinking.
625 [[nodiscard]] constexpr TRect<T> Expand(TSize<T> amount) const {
626 return Expand(amount.width, amount.height);
627 }
628
629 /// @brief Returns a new rectangle that represents the projection of the
630 /// source rectangle onto this rectangle. In other words, the source
631 /// rectangle is redefined in terms of the coordinate space of this
632 /// rectangle.
633 [[nodiscard]] constexpr TRect<T> Project(TRect<T> source) const {
634 if (IsEmpty()) {
635 return {};
636 }
637 return source.Shift(-left_, -top_)
638 .Scale(1.0 / static_cast<Scalar>(GetWidth()),
639 1.0 / static_cast<Scalar>(GetHeight()));
640 }
641
642 ONLY_ON_FLOAT_M([[nodiscard]] constexpr static, TRect)
643 RoundOut(const TRect<U>& r) {
644 return TRect::MakeLTRB(saturated::Cast<U, Type>(floor(r.GetLeft())),
645 saturated::Cast<U, Type>(floor(r.GetTop())),
646 saturated::Cast<U, Type>(ceil(r.GetRight())),
647 saturated::Cast<U, Type>(ceil(r.GetBottom())));
648 }
649
650 ONLY_ON_FLOAT_M([[nodiscard]] constexpr static, TRect)
651 Round(const TRect<U>& r) {
652 return TRect::MakeLTRB(saturated::Cast<U, Type>(round(r.GetLeft())),
653 saturated::Cast<U, Type>(round(r.GetTop())),
654 saturated::Cast<U, Type>(round(r.GetRight())),
655 saturated::Cast<U, Type>(round(r.GetBottom())));
656 }
657
658 [[nodiscard]] constexpr static std::optional<TRect> Union(
659 const TRect& a,
660 const std::optional<TRect> b) {
661 return b.has_value() ? a.Union(b.value()) : a;
662 }
663
664 [[nodiscard]] constexpr static std::optional<TRect> Union(
665 const std::optional<TRect> a,
666 const TRect& b) {
667 return a.has_value() ? a->Union(b) : b;
668 }
669
670 [[nodiscard]] constexpr static std::optional<TRect> Union(
671 const std::optional<TRect> a,
672 const std::optional<TRect> b) {
673 return a.has_value() ? Union(a.value(), b) : b;
674 }
675
676 [[nodiscard]] constexpr static std::optional<TRect> Intersection(
677 const TRect& a,
678 const std::optional<TRect> b) {
679 return b.has_value() ? a.Intersection(b.value()) : a;
680 }
681
682 [[nodiscard]] constexpr static std::optional<TRect> Intersection(
683 const std::optional<TRect> a,
684 const TRect& b) {
685 return a.has_value() ? a->Intersection(b) : b;
686 }
687
688 [[nodiscard]] constexpr static std::optional<TRect> Intersection(
689 const std::optional<TRect> a,
690 const std::optional<TRect> b) {
691 return a.has_value() ? Intersection(a.value(), b) : b;
692 }
693
694 private:
695 constexpr TRect(Type left, Type top, Type right, Type bottom)
696 : left_(left), top_(top), right_(right), bottom_(bottom) {}
697
698 Type left_;
699 Type top_;
700 Type right_;
701 Type bottom_;
702
703 static constexpr Scalar kMinimumHomogenous = 1.0f / (1 << 14);
704
705 // Clip p against the near clipping plane (W = kMinimumHomogenous)
706 // and interpolate a crossing point against the nearby neighbors
707 // left and right if p is clipped and either of them is not.
708 // This method can produce 0, 1, or 2 points per call depending on
709 // how many of the points are clipped.
710 // 0 - all points are clipped
711 // 1 - p is unclipped OR
712 // p is clipped and exactly one of the neighbors is not
713 // 2 - p is clipped and both neighbors are not
714 static constexpr int ClipAndInsert(Point clipped[],
715 int index,
716 const Vector3& left,
717 const Vector3& p,
718 const Vector3& right) {
719 if (p.z >= kMinimumHomogenous) {
720 clipped[index++] = {p.x / p.z, p.y / p.z};
721 } else {
722 index = InterpolateAndInsert(clipped, index, p, left);
723 index = InterpolateAndInsert(clipped, index, p, right);
724 }
725 return index;
726 }
727
728 // Interpolate (a clipped) point p against one of its neighbors
729 // and insert the point into the array where the line between them
730 // veers from clipped space to unclipped, if such a point exists.
731 static constexpr int InterpolateAndInsert(Point clipped[],
732 int index,
733 const Vector3& p,
734 const Vector3& neighbor) {
735 if (neighbor.z >= kMinimumHomogenous) {
736 auto t = (kMinimumHomogenous - p.z) / (neighbor.z - p.z);
737 clipped[index++] = {
738 (t * p.x + (1.0f - t) * neighbor.x) / kMinimumHomogenous,
739 (t * p.y + (1.0f - t) * neighbor.y) / kMinimumHomogenous,
740 };
741 }
742 return index;
743 }
744};
745
748
749#undef ONLY_ON_FLOAT
750#undef ONLY_ON_FLOAT_M
751
752} // namespace impeller
753
754namespace std {
755
756template <class T>
757inline std::ostream& operator<<(std::ostream& out,
758 const impeller::TRect<T>& r) {
759 out << "(" << r.GetOrigin() << ", " << r.GetSize() << ")";
760 return out;
761}
762
763} // namespace std
764
765#endif // FLUTTER_IMPELLER_GEOMETRY_RECT_H_
static void round(SkPoint *p)
static const int points[]
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
SkBitmap source
Definition examples.cpp:28
static bool b
struct MyStruct a[10]
uint8_t value
#define FML_UNREACHABLE()
Definition logging.h:109
#define ONLY_ON_FLOAT_M(Modifiers, Return)
Definition point.h:21
double y
double x
float Scalar
Definition scalar.h:18
TPoint< Scalar > Point
Definition point.h:316
Definition ref_ptr.h:256
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition color.h:951
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
#define T
int32_t height
int32_t width
const Scalar scale
Point offset
A 4x4 matrix using column-major storage.
Definition matrix.h:37
static constexpr Matrix MakeScale(const Vector3 &s)
Definition matrix.h:104
constexpr auto GetBottom() const
Definition rect.h:324
constexpr Type GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
Definition rect.h:304
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition rect.h:440
constexpr auto GetTop() const
Definition rect.h:320
constexpr std::array< TPoint< T >, 4 > GetPoints() const
Get the points that represent the 4 corners of this rectangle in a Z order that is compatible with tr...
Definition rect.h:382
constexpr std::optional< TRect > Intersection(const TRect &o) const
Definition rect.h:496
constexpr TRect< T > Project(TRect< T > source) const
Returns a new rectangle that represents the projection of the source rectangle onto this rectangle....
Definition rect.h:633
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle which may be negative in either width or height and may have been c...
Definition rect.h:294
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition rect.h:314
static constexpr std::optional< TRect > Union(const std::optional< TRect > a, const std::optional< TRect > b)
Definition rect.h:670
constexpr std::optional< TRect< T > > Cutout(const TRect &o) const
Returns the new boundary rectangle that would result from this rectangle being cut out by the specifi...
Definition rect.h:521
constexpr TRect Scale(TPoint< T > scale) const
Definition rect.h:202
constexpr bool IsMaximum() const
Definition rect.h:281
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition rect.h:264
constexpr T Area() const
Get the area of the rectangle, equivalent to |GetSize().Area()|.
Definition rect.h:343
constexpr bool Contains(const TPoint< Type > &p) const
Returns true iff the provided point |p| is inside the half-open interior of this rectangle.
Definition rect.h:217
static constexpr std::optional< TRect > MakePointBounds(const PointIter first, const PointIter last)
Definition rect.h:156
constexpr TRect Union(const TRect &o) const
Definition rect.h:481
constexpr TRect Scale(Type scale_x, Type scale_y) const
Definition rect.h:195
constexpr bool IntersectsWithRect(const TRect &o) const
Definition rect.h:510
constexpr auto GetLeft() const
Definition rect.h:318
constexpr TRect CutoutOrEmpty(const TRect &o) const
Definition rect.h:561
constexpr TRect< T > Expand(T horizontal_amount, T vertical_amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
Definition rect.h:607
constexpr TPoint< T > GetLeftTop() const
Definition rect.h:326
static constexpr std::optional< TRect > Union(const TRect &a, const std::optional< TRect > b)
Definition rect.h:658
Round(const TRect< U > &r)
Definition rect.h:651
RoundOut(const TRect< U > &r)
Definition rect.h:643
constexpr TRect GetPositive() const
Get a version of this rectangle that has a non-negative size.
Definition rect.h:366
static constexpr std::optional< TRect > Intersection(const TRect &a, const std::optional< TRect > b)
Definition rect.h:676
constexpr TRect Scale(TSize< T > scale) const
Definition rect.h:206
constexpr Type GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
Definition rect.h:300
constexpr std::array< T, 4 > GetLTRB() const
Definition rect.h:355
static constexpr TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition rect.h:140
constexpr auto GetRight() const
Definition rect.h:322
constexpr bool IsSquare() const
Returns true if width and height are equal and neither is NaN.
Definition rect.h:271
static constexpr TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition rect.h:136
constexpr bool Contains(const TRect &o) const
Returns true iff this rectangle is not empty and it also contains every point considered inside the p...
Definition rect.h:240
IsFinite() const
Returns true if all of the fields of this floating point rectangle are finite.
Definition rect.h:255
constexpr Matrix GetNormalizingTransform() const
Constructs a Matrix that will map all points in the coordinate space of the rectangle into a new norm...
Definition rect.h:459
constexpr TRect Scale(Type scale) const
Definition rect.h:188
constexpr std::array< TPoint< T >, 4 > GetTransformedPoints(const Matrix &transform) const
Definition rect.h:394
constexpr TRect TransformAndClipBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle, clipped against the near clippin...
Definition rect.h:406
constexpr TPoint< T > GetRightBottom() const
Definition rect.h:338
static constexpr std::optional< TRect > Intersection(const std::optional< TRect > a, const std::optional< TRect > b)
Definition rect.h:688
constexpr TPoint< T > GetLeftBottom() const
Definition rect.h:334
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:146
static constexpr std::optional< TRect > Union(const std::optional< TRect > a, const TRect &b)
Definition rect.h:664
constexpr TRect< T > Shift(TPoint< T > offset) const
Returns a new rectangle translated by the given offset.
Definition rect.h:576
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition rect.h:308
static constexpr std::optional< TRect > Intersection(const std::optional< TRect > a, const TRect &b)
Definition rect.h:682
constexpr TPoint< T > GetRightTop() const
Definition rect.h:330
static constexpr std::optional< TRect > MakePointBounds(const U &value)
Definition rect.h:151
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition rect.h:582
static constexpr TRect MakeMaximum()
Definition rect.h:174
constexpr Point GetCenter() const
Get the center point as a |Point|.
Definition rect.h:350
constexpr TRect()
Definition rect.h:127
constexpr TRect< T > Expand(TSize< T > amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
Definition rect.h:625
constexpr bool operator==(const TRect &r) const
Definition rect.h:181
constexpr TRect< T > Expand(T amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
Definition rect.h:596
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:361
constexpr TRect< T > Shift(T dx, T dy) const
Returns a new rectangle translated by the given offset.
Definition rect.h:566
constexpr TRect< T > Expand(TPoint< T > amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
Definition rect.h:619
constexpr TPoint< Type > GetOrigin() const
Returns the upper left corner of the rectangle as specified by the left/top or x/y values when it was...
Definition rect.h:287
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition rect.h:129
Type height
Definition size.h:23
Type width
Definition size.h:22