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