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 
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() &&
72  bounds_.GetWidth() * 0.5f) &&
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_
float Scalar
Definition: scalar.h:18
TRect< Scalar > Rect
Definition: rect.h:792
TSize< Scalar > Size
Definition: size.h:159
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:35
Definition: comparable.h:95
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:927
constexpr bool IsFinite() const
Definition: round_rect.h:55
constexpr const RoundingRadii & GetRadii() const
Definition: round_rect.h:53
static RoundRect MakeRectRadii(const Rect &rect, const RoundingRadii &radii)
Definition: round_rect.cc:9
constexpr static RoundRect MakeRectRadius(const Rect &rect, Scalar radius)
Definition: round_rect.h:26
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
constexpr bool IsEmpty() const
Definition: round_rect.h:63
constexpr bool operator==(const RoundRect &rr) const
Definition: round_rect.h:125
constexpr static RoundRect MakeNinePatch(const Rect &rect, Scalar left, Scalar top, Scalar right, Scalar bottom)
Definition: round_rect.h:41
constexpr bool IsRect() const
Definition: round_rect.h:65
constexpr static RoundRect MakeOval(const Rect &rect)
Definition: round_rect.h:22
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
constexpr bool operator!=(const RoundRect &r) const
Definition: round_rect.h:129
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
constexpr static RoundRect MakeRect(const Rect &rect)
Definition: round_rect.h:18
constexpr RoundRect Shift(Scalar dx, Scalar dy) const
Returns a new round rectangle translated by the given offset.
Definition: round_rect.h:87
constexpr const Rect & GetBounds() const
Definition: round_rect.h:52
constexpr bool IsOval() const
Definition: round_rect.h:69
constexpr RoundRect Expand(Scalar amount) const
Returns a round rectangle with expanded edges. Negative expansion results in shrinking.
Definition: round_rect.h:118
constexpr static RoundRect MakeRectXY(const Rect &rect, Scalar x_radius, Scalar y_radius)
Definition: round_rect.h:30
constexpr static RoundRect MakeRectXY(const Rect &rect, Size corner_radii)
Definition: round_rect.h:37
constexpr static RoundingRadii MakeNinePatch(Scalar left, Scalar top, Scalar right, Scalar bottom)
constexpr bool AreAllCornersEmpty() const
constexpr static RoundingRadii MakeRadii(Size radii)
constexpr bool AreAllCornersSame(Scalar tolerance=kEhCloseEnough) const
constexpr static RoundingRadii MakeRadius(Scalar radius)
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
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:351
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:301
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
IsFinite() const
Returns true if all of the fields of this floating point rectangle are finite.
Definition: rect.h:292
constexpr TRect< T > Shift(T dx, T dy) const
Returns a new rectangle translated by the given offset.
Definition: rect.h:606
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:345
Type height
Definition: size.h:29
Type width
Definition: size.h:28
IsFinite() const
Definition: size.h:126