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  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() &&
63  bounds_.GetWidth() * 0.5f) &&
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_
float Scalar
Definition: scalar.h:18
TRect< Scalar > Rect
Definition: rect.h:792
TSize< Scalar > Size
Definition: size.h:171
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:926
constexpr bool IsFinite() const
Definition: round_rect.h:46
constexpr const RoundingRadii & GetRadii() const
Definition: round_rect.h:44
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:99
constexpr bool IsEmpty() const
Definition: round_rect.h:54
constexpr bool operator==(const RoundRect &rr) const
Definition: round_rect.h:116
constexpr bool IsRect() const
Definition: round_rect.h:56
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:87
constexpr bool operator!=(const RoundRect &r) const
Definition: round_rect.h:120
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:78
constexpr const Rect & GetBounds() const
Definition: round_rect.h:43
constexpr bool IsOval() const
Definition: round_rect.h:60
constexpr RoundRect Expand(Scalar amount) const
Returns a round rectangle with expanded edges. Negative expansion results in shrinking.
Definition: round_rect.h:109
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 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