| Flutter Impeller |

round_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_ROUND_RECT_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_ROUND_RECT_H_
7
8 #include "flutter/impeller/geometry/point.h"
9 #include "flutter/impeller/geometry/rect.h"
10 #include "flutter/impeller/geometry/rounding_radii.h"
11 #include "flutter/impeller/geometry/size.h"
12
13 namespace impeller {
14
15 struct RoundRect {
16 RoundRect() = default;
17
18 constexpr static RoundRect MakeRect(const Rect& rect) {
19 return MakeRectRadii(rect, RoundingRadii());
20 }
21
22 constexpr static RoundRect MakeOval(const Rect& rect) {
23 return MakeRectRadii(rect, RoundingRadii::MakeRadii(rect.GetSize() * 0.5f));
24 }
25
26 constexpr static RoundRect MakeRectRadius(const Rect& rect, Scalar radius) {
27 return MakeRectRadii(rect, RoundingRadii::MakeRadius(radius));
28 }
29
30 constexpr static RoundRect MakeRectXY(const Rect& rect,
31 Scalar x_radius,
32 Scalar y_radius) {
33 return MakeRectRadii(rect,
34 RoundingRadii::MakeRadii(Size(x_radius, y_radius)));
35 }
36
37 constexpr static RoundRect MakeRectXY(const Rect& rect, Size corner_radii) {
38 return MakeRectRadii(rect, RoundingRadii::MakeRadii(corner_radii));
39 }
40
41 static RoundRect MakeRectRadii(const Rect& rect, const RoundingRadii& radii);
42
43 constexpr const Rect& GetBounds() const { return bounds_; }
44 constexpr const RoundingRadii& GetRadii() const { return radii_; }
45
46 [[nodiscard]] constexpr bool IsFinite() const {
47 return bounds_.IsFinite() && //
48 radii_.top_left.IsFinite() && //
49 radii_.top_right.IsFinite() && //
50 radii_.bottom_left.IsFinite() && //
51 radii_.bottom_right.IsFinite();
52 }
53
54 [[nodiscard]] constexpr bool IsEmpty() const { return bounds_.IsEmpty(); }
55
56 [[nodiscard]] constexpr bool IsRect() const {
57 return !bounds_.IsEmpty() && radii_.AreAllCornersEmpty();
58 }
59
60 [[nodiscard]] constexpr bool IsOval() const {
61 return !bounds_.IsEmpty() && radii_.AreAllCornersSame() &&
62 ScalarNearlyEqual(radii_.top_left.width,
63 bounds_.GetWidth() * 0.5f) &&
64 ScalarNearlyEqual(radii_.top_left.height,
65 bounds_.GetHeight() * 0.5f);
66 }
67
68 /// @brief Returns true iff the provided point |p| is inside the
69 /// half-open interior of this rectangle.
70 ///
71 /// For purposes of containment, a rectangle contains points
72 /// along the top and left edges but not points along the
73 /// right and bottom edges so that a point is only ever
74 /// considered inside one of two abutting rectangles.
75 [[nodiscard]] bool Contains(const Point& p) const;
76
77 /// @brief Returns a new round rectangle translated by the given offset.
78 [[nodiscard]] constexpr RoundRect Shift(Scalar dx, Scalar dy) const {
79 // Just in case, use the factory rather than the internal constructor
80 // as shifting the rectangle may increase/decrease its bit precision
81 // so we should re-validate the radii to the newly located rectangle.
82 return MakeRectRadii(bounds_.Shift(dx, dy), radii_);
83 }
84
85 /// @brief Returns a round rectangle with expanded edges. Negative expansion
86 /// results in shrinking.
87 [[nodiscard]] constexpr RoundRect Expand(Scalar left,
88 Scalar top,
89 Scalar right,
90 Scalar bottom) const {
91 // Use the factory rather than the internal constructor as the changing
92 // size of the rectangle requires that we re-validate the radii to the
93 // newly sized rectangle.
94 return MakeRectRadii(bounds_.Expand(left, top, right, bottom), radii_);
95 }
96
97 /// @brief Returns a round rectangle with expanded edges. Negative expansion
98 /// results in shrinking.
99 [[nodiscard]] constexpr RoundRect Expand(Scalar horizontal,
100 Scalar vertical) const {
101 // Use the factory rather than the internal constructor as the changing
102 // size of the rectangle requires that we re-validate the radii to the
103 // newly sized rectangle.
104 return MakeRectRadii(bounds_.Expand(horizontal, vertical), radii_);
105 }
106
107 /// @brief Returns a round rectangle with expanded edges. Negative expansion
108 /// results in shrinking.
109 [[nodiscard]] constexpr RoundRect Expand(Scalar amount) const {
110 // Use the factory rather than the internal constructor as the changing
111 // size of the rectangle requires that we re-validate the radii to the
112 // newly sized rectangle.
113 return MakeRectRadii(bounds_.Expand(amount), radii_);
114 }
115
116 [[nodiscard]] constexpr bool operator==(const RoundRect& rr) const {
117 return bounds_ == rr.bounds_ && radii_ == rr.radii_;
118 }
119
120 [[nodiscard]] constexpr bool operator!=(const RoundRect& r) const {
121 return !(*this == r);
122 }
123
124 private:
125 constexpr RoundRect(const Rect& bounds, const RoundingRadii& radii)
126 : bounds_(bounds), radii_(radii) {}
127
128 Rect bounds_;
129 RoundingRadii radii_;
130 };
131
132 } // namespace impeller
133
134 namespace std {
135
136 inline std::ostream& operator<<(std::ostream& out,
137 const impeller::RoundRect& rr) {
138 out << "(" //
139 << "rect: " << rr.GetBounds() << ", " //
140 << "radii: " << rr.GetRadii() //
141 << ")";
142 return out;
143 }
144
145 } // namespace std
146
147 #endif // FLUTTER_IMPELLER_GEOMETRY_ROUND_RECT_H_
impeller::RoundRect::IsRect
constexpr bool IsRect() const
Definition: round_rect.h:56
impeller::RoundingRadii::AreAllCornersEmpty
constexpr bool AreAllCornersEmpty() const
Definition: rounding_radii.h:35
impeller::RoundRect::GetBounds
constexpr const Rect & GetBounds() const
Definition: round_rect.h:43
impeller::RoundingRadii::MakeRadii
constexpr static RoundingRadii MakeRadii(Size radii)
Definition: rounding_radii.h:24
impeller::RoundRect
Definition: round_rect.h:15
impeller::RoundingRadii::top_left
Size top_left
Definition: rounding_radii.h:15
impeller::RoundingRadii
Definition: rounding_radii.h:14
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TSize::IsFinite
IsFinite() const
Definition: size.h:126
impeller::RoundRect::Expand
constexpr RoundRect Expand(Scalar left, Scalar top, Scalar right, Scalar bottom) const
Returns a round rectangle with expanded edges. Negative expansion results in shrinking.
Definition: round_rect.h:87
impeller::RoundRect::IsOval
constexpr bool IsOval() const
Definition: round_rect.h:60
impeller::RoundRect::MakeRectRadius
constexpr static RoundRect MakeRectRadius(const Rect &rect, Scalar radius)
Definition: round_rect.h:26
impeller::RoundRect::MakeRect
constexpr static RoundRect MakeRect(const Rect &rect)
Definition: round_rect.h:18
impeller::RoundingRadii::AreAllCornersSame
constexpr bool AreAllCornersSame(Scalar tolerance=kEhCloseEnough) const
Definition: rounding_radii.h:42
impeller::RoundingRadii::bottom_right
Size bottom_right
Definition: rounding_radii.h:18
impeller::RoundRect::RoundRect
RoundRect()=default
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:926
impeller::Size
TSize< Scalar > Size
Definition: size.h:171
impeller::TRect::GetHeight
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:351
impeller::RoundRect::Contains
bool Contains(const Point &p) const
Returns true iff the provided point |p| is inside the half-open interior of this rectangle.
Definition: round_rect.cc:73
impeller::RoundRect::Expand
constexpr RoundRect Expand(Scalar horizontal, Scalar vertical) const
Returns a round rectangle with expanded edges. Negative expansion results in shrinking.
Definition: round_rect.h:99
impeller::TRect::Shift
constexpr TRect< T > Shift(T dx, T dy) const
Returns a new rectangle translated by the given offset.
Definition: rect.h:606
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:301
impeller::RoundingRadii::bottom_left
Size bottom_left
Definition: rounding_radii.h:17
impeller::TSize< Scalar >
impeller::RoundRect::MakeRectXY
constexpr static RoundRect MakeRectXY(const Rect &rect, Size corner_radii)
Definition: round_rect.h:37
impeller::RoundingRadii::top_right
Size top_right
Definition: rounding_radii.h:16
impeller::RoundRect::operator==
constexpr bool operator==(const RoundRect &rr) const
Definition: round_rect.h:116
rounding_radii.h
impeller::TRect::GetWidth
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:345
impeller::RoundRect::IsFinite
constexpr bool IsFinite() const
Definition: round_rect.h:46
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:792
impeller::RoundRect::MakeRectRadii
static RoundRect MakeRectRadii(const Rect &rect, const RoundingRadii &radii)
Definition: round_rect.cc:9
impeller::TSize::width
Type width
Definition: size.h:28
impeller::TRect::IsFinite
IsFinite() const
Returns true if all of the fields of this floating point rectangle are finite.
Definition: rect.h:292
impeller::TRect::GetSize
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:331
std
Definition: comparable.h:95
rect.h
impeller::TPoint
Definition: point.h:27
impeller::RoundRect::MakeOval
constexpr static RoundRect MakeOval(const Rect &rect)
Definition: round_rect.h:22
impeller::RoundRect::GetRadii
constexpr const RoundingRadii & GetRadii() const
Definition: round_rect.h:44
impeller::ScalarNearlyEqual
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:35
impeller::RoundRect::Expand
constexpr RoundRect Expand(Scalar amount) const
Returns a round rectangle with expanded edges. Negative expansion results in shrinking.
Definition: round_rect.h:109
impeller::TSize::height
Type height
Definition: size.h:29
impeller::RoundRect::IsEmpty
constexpr bool IsEmpty() const
Definition: round_rect.h:54
impeller
Definition: allocation.cc:12
impeller::RoundRect::Shift
constexpr RoundRect Shift(Scalar dx, Scalar dy) const
Returns a new round rectangle translated by the given offset.
Definition: round_rect.h:78
impeller::RoundRect::operator!=
constexpr bool operator!=(const RoundRect &r) const
Definition: round_rect.h:120
impeller::TRect< Scalar >
impeller::RoundingRadii::MakeRadius
constexpr static RoundingRadii MakeRadius(Scalar radius)
Definition: rounding_radii.h:20
size.h
impeller::TRect::Expand
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:622
impeller::RoundRect::MakeRectXY
constexpr static RoundRect MakeRectXY(const Rect &rect, Scalar x_radius, Scalar y_radius)
Definition: round_rect.h:30