Flutter Impeller
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 #pragma once
6 
7 #include <array>
8 #include <optional>
9 #include <ostream>
10 #include <vector>
11 
15 #include "impeller/geometry/size.h"
16 
17 namespace impeller {
18 
19 template <class T>
20 struct TRect {
21  using Type = T;
22 
25 
26  constexpr TRect() : origin({0, 0}), size({0, 0}) {}
27 
28  constexpr TRect(TSize<Type> size) : origin({0.0, 0.0}), size(size) {}
29 
31  : origin(origin), size(size) {}
32 
33  constexpr TRect(const Type components[4])
34  : origin(components[0], components[1]),
35  size(components[2], components[3]) {}
36 
37  constexpr TRect(Type x, Type y, Type width, Type height)
38  : origin(x, y), size(width, height) {}
39 
40  constexpr static TRect MakeLTRB(Type left,
41  Type top,
42  Type right,
43  Type bottom) {
44  return TRect(left, top, right - left, bottom - top);
45  }
46 
47  constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height) {
48  return TRect(x, y, width, height);
49  }
50 
51  template <class U>
52  constexpr static TRect MakeSize(const TSize<U>& size) {
53  return TRect(0.0, 0.0, size.width, size.height);
54  }
55 
56  template <typename PointIter>
57  constexpr static std::optional<TRect> MakePointBounds(const PointIter first,
58  const PointIter last) {
59  if (first == last) {
60  return std::nullopt;
61  }
62  auto left = first->x;
63  auto top = first->y;
64  auto right = first->x;
65  auto bottom = first->y;
66  for (auto it = first + 1; it < last; ++it) {
67  left = std::min(left, it->x);
68  top = std::min(top, it->y);
69  right = std::max(right, it->x);
70  bottom = std::max(bottom, it->y);
71  }
72  return TRect::MakeLTRB(left, top, right, bottom);
73  }
74 
75  constexpr static TRect MakeMaximum() {
76  return TRect::MakeLTRB(-std::numeric_limits<Type>::infinity(),
77  -std::numeric_limits<Type>::infinity(),
78  std::numeric_limits<Type>::infinity(),
79  std::numeric_limits<Type>::infinity());
80  }
81 
82  template <class U>
83  constexpr explicit TRect(const TRect<U>& other)
84  : origin(static_cast<TPoint<Type>>(other.origin)),
85  size(static_cast<TSize<Type>>(other.size)) {}
86 
87  constexpr TRect operator+(const TRect& r) const {
88  return TRect({origin.x + r.origin.x, origin.y + r.origin.y},
89  {size.width + r.size.width, size.height + r.size.height});
90  }
91 
92  constexpr TRect operator-(const TRect& r) const {
93  return TRect({origin.x - r.origin.x, origin.y - r.origin.y},
94  {size.width - r.size.width, size.height - r.size.height});
95  }
96 
97  constexpr TRect operator*(Type scale) const { return Scale(scale); }
98 
99  constexpr TRect operator*(const TRect& r) const {
100  return TRect({origin.x * r.origin.x, origin.y * r.origin.y},
101  {size.width * r.size.width, size.height * r.size.height});
102  }
103 
104  constexpr bool operator==(const TRect& r) const {
105  return origin == r.origin && size == r.size;
106  }
107 
108  constexpr TRect Scale(Type scale) const {
109  return TRect({origin.x * scale, origin.y * scale},
110  {size.width * scale, size.height * scale});
111  }
112 
113  constexpr TRect Scale(TPoint<T> scale) const {
114  return TRect({origin.x * scale.x, origin.y * scale.y},
115  {size.width * scale.x, size.height * scale.y});
116  }
117 
118  constexpr TRect Scale(TSize<T> scale) const {
119  return Scale(TPoint<T>(scale));
120  }
121 
122  constexpr bool Contains(const TPoint<Type>& p) const {
123  return p.x >= GetLeft() && p.x < GetRight() && p.y >= GetTop() &&
124  p.y < GetBottom();
125  }
126 
127  constexpr bool Contains(const TRect& o) const {
128  return Union(o).size == size;
129  }
130 
131  constexpr bool IsZero() const { return size.IsZero(); }
132 
133  constexpr bool IsEmpty() const { return size.IsEmpty(); }
134 
135  constexpr bool IsMaximum() const { return *this == MakeMaximum(); }
136 
137  constexpr auto GetLeft() const {
138  if (IsMaximum()) {
139  return -std::numeric_limits<Type>::infinity();
140  }
141  return std::min(origin.x, origin.x + size.width);
142  }
143 
144  constexpr auto GetTop() const {
145  if (IsMaximum()) {
146  return -std::numeric_limits<Type>::infinity();
147  }
148  return std::min(origin.y, origin.y + size.height);
149  }
150 
151  constexpr auto GetRight() const {
152  if (IsMaximum()) {
153  return std::numeric_limits<Type>::infinity();
154  }
155  return std::max(origin.x, origin.x + size.width);
156  }
157 
158  constexpr auto GetBottom() const {
159  if (IsMaximum()) {
160  return std::numeric_limits<Type>::infinity();
161  }
162  return std::max(origin.y, origin.y + size.height);
163  }
164 
165  constexpr TPoint<T> GetLeftTop() const { return {GetLeft(), GetTop()}; }
166 
167  constexpr TPoint<T> GetRightTop() const { return {GetRight(), GetTop()}; }
168 
169  constexpr TPoint<T> GetLeftBottom() const { return {GetLeft(), GetBottom()}; }
170 
171  constexpr TPoint<T> GetRightBottom() const {
172  return {GetRight(), GetBottom()};
173  }
174 
175  constexpr std::array<T, 4> GetLTRB() const {
176  return {GetLeft(), GetTop(), GetRight(), GetBottom()};
177  }
178 
179  /// @brief Get a version of this rectangle that has a non-negative size.
180  constexpr TRect GetPositive() const {
181  auto ltrb = GetLTRB();
182  return MakeLTRB(ltrb[0], ltrb[1], ltrb[2], ltrb[3]);
183  }
184 
185  /// @brief Get the points that represent the 4 corners of this rectangle. The
186  /// order is: Top left, top right, bottom left, bottom right.
187  constexpr std::array<TPoint<T>, 4> GetPoints() const {
188  auto [left, top, right, bottom] = GetLTRB();
189  return {TPoint(left, top), TPoint(right, top), TPoint(left, bottom),
190  TPoint(right, bottom)};
191  }
192 
193  constexpr std::array<TPoint<T>, 4> GetTransformedPoints(
194  const Matrix& transform) const {
195  auto points = GetPoints();
196  for (size_t i = 0; i < points.size(); i++) {
197  points[i] = transform * points[i];
198  }
199  return points;
200  }
201 
202  /// @brief Creates a new bounding box that contains this transformed
203  /// rectangle.
204  constexpr TRect TransformBounds(const Matrix& transform) const {
205  auto points = GetTransformedPoints(transform);
206  return TRect::MakePointBounds(points.begin(), points.end()).value();
207  }
208 
209  constexpr TRect Union(const TRect& o) const {
210  auto this_ltrb = GetLTRB();
211  auto other_ltrb = o.GetLTRB();
212  return TRect::MakeLTRB(std::min(this_ltrb[0], other_ltrb[0]), //
213  std::min(this_ltrb[1], other_ltrb[1]), //
214  std::max(this_ltrb[2], other_ltrb[2]), //
215  std::max(this_ltrb[3], other_ltrb[3]) //
216  );
217  }
218 
219  constexpr std::optional<TRect<T>> Intersection(const TRect& o) const {
220  auto this_ltrb = GetLTRB();
221  auto other_ltrb = o.GetLTRB();
222  auto intersection =
223  TRect::MakeLTRB(std::max(this_ltrb[0], other_ltrb[0]), //
224  std::max(this_ltrb[1], other_ltrb[1]), //
225  std::min(this_ltrb[2], other_ltrb[2]), //
226  std::min(this_ltrb[3], other_ltrb[3]) //
227  );
228  if (intersection.size.IsEmpty()) {
229  return std::nullopt;
230  }
231  return intersection;
232  }
233 
234  constexpr bool IntersectsWithRect(const TRect& o) const {
235  return Intersection(o).has_value();
236  }
237 
238  /// @brief Returns the new boundary rectangle that would result from the
239  /// rectangle being cutout by a second rectangle.
240  constexpr std::optional<TRect<T>> Cutout(const TRect& o) const {
241  const auto& [a_left, a_top, a_right, a_bottom] = GetLTRB(); // Source rect.
242  const auto& [b_left, b_top, b_right, b_bottom] = o.GetLTRB(); // Cutout.
243  if (b_left <= a_left && b_right >= a_right) {
244  if (b_top <= a_top && b_bottom >= a_bottom) {
245  // Full cutout.
246  return std::nullopt;
247  }
248  if (b_top <= a_top && b_bottom > a_top) {
249  // Cuts off the top.
250  return TRect::MakeLTRB(a_left, b_bottom, a_right, a_bottom);
251  }
252  if (b_bottom >= a_bottom && b_top < a_bottom) {
253  // Cuts out the bottom.
254  return TRect::MakeLTRB(a_left, a_top, a_right, b_top);
255  }
256  }
257  if (b_top <= a_top && b_bottom >= a_bottom) {
258  if (b_left <= a_left && b_right > a_left) {
259  // Cuts out the left.
260  return TRect::MakeLTRB(b_right, a_top, a_right, a_bottom);
261  }
262  if (b_right >= a_right && b_left < a_right) {
263  // Cuts out the right.
264  return TRect::MakeLTRB(a_left, a_top, b_left, a_bottom);
265  }
266  }
267 
268  return *this;
269  }
270 
271  /// @brief Returns a new rectangle translated by the given offset.
272  constexpr TRect<T> Shift(TPoint<T> offset) const {
273  return TRect(origin.x + offset.x, origin.y + offset.y, size.width,
274  size.height);
275  }
276 
277  /// @brief Returns a rectangle with expanded edges. Negative expansion
278  /// results in shrinking.
279  constexpr TRect<T> Expand(T left, T top, T right, T bottom) const {
280  return TRect(origin.x - left, //
281  origin.y - top, //
282  size.width + left + right, //
283  size.height + top + bottom);
284  }
285 
286  /// @brief Returns a rectangle with expanded edges in all directions.
287  /// Negative expansion results in shrinking.
288  constexpr TRect<T> Expand(T amount) const {
289  return TRect(origin.x - amount, //
290  origin.y - amount, //
291  size.width + amount * 2, //
292  size.height + amount * 2);
293  }
294 
295  /// @brief Returns a new rectangle that represents the projection of the
296  /// source rectangle onto this rectangle. In other words, the source
297  /// rectangle is redefined in terms of the corrdinate space of this
298  /// rectangle.
299  constexpr TRect<T> Project(TRect<T> source) const {
300  return source.Shift(-origin).Scale(
301  TSize<T>(1.0 / static_cast<Scalar>(size.width),
302  1.0 / static_cast<Scalar>(size.height)));
303  }
304 };
305 
308 
309 } // namespace impeller
310 
311 namespace std {
312 
313 template <class T>
314 inline std::ostream& operator<<(std::ostream& out,
315  const impeller::TRect<T>& r) {
316  out << "(" << r.origin << ", " << r.size << ")";
317  return out;
318 }
319 
320 } // namespace std
impeller::TRect::GetLTRB
constexpr std::array< T, 4 > GetLTRB() const
Definition: rect.h:175
impeller::TRect::TRect
constexpr TRect(TSize< Type > size)
Definition: rect.h:28
impeller::TRect::size
TSize< Type > size
Definition: rect.h:24
point.h
impeller::TPoint::y
Type y
Definition: point.h:24
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::TRect< Scalar >::Type
Scalar Type
Definition: rect.h:21
impeller::TRect::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:47
impeller::TRect::IsZero
constexpr bool IsZero() const
Definition: rect.h:131
impeller::TRect::Expand
constexpr TRect< T > Expand(T amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
Definition: rect.h:288
impeller::TRect::operator-
constexpr TRect operator-(const TRect &r) const
Definition: rect.h:92
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:204
impeller::TRect::IsMaximum
constexpr bool IsMaximum() const
Definition: rect.h:135
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:165
impeller::TRect::operator+
constexpr TRect operator+(const TRect &r) const
Definition: rect.h:87
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:945
impeller::TRect::TRect
constexpr TRect()
Definition: rect.h:26
impeller::TRect::GetRightTop
constexpr TPoint< T > GetRightTop() const
Definition: rect.h:167
impeller::TRect::Contains
constexpr bool Contains(const TRect &o) const
Definition: rect.h:127
impeller::TRect::operator==
constexpr bool operator==(const TRect &r) const
Definition: rect.h:104
impeller::TRect::IntersectsWithRect
constexpr bool IntersectsWithRect(const TRect &o) const
Definition: rect.h:234
impeller::TRect::GetPoints
constexpr std::array< TPoint< T >, 4 > GetPoints() const
Get the points that represent the 4 corners of this rectangle. The order is: Top left,...
Definition: rect.h:187
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Definition: rect.h:133
matrix.h
impeller::TRect::Scale
constexpr TRect Scale(TSize< T > scale) const
Definition: rect.h:118
impeller::TSize< Type >
impeller::TRect::Intersection
constexpr std::optional< TRect< T > > Intersection(const TRect &o) const
Definition: rect.h:219
impeller::TRect::GetLeft
constexpr auto GetLeft() const
Definition: rect.h:137
impeller::TRect::GetTransformedPoints
constexpr std::array< TPoint< T >, 4 > GetTransformedPoints(const Matrix &transform) const
Definition: rect.h:193
impeller::TRect::operator*
constexpr TRect operator*(Type scale) const
Definition: rect.h:97
impeller::TRect::GetLeftBottom
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:169
impeller::TRect::TRect
constexpr TRect(Type x, Type y, Type width, Type height)
Definition: rect.h:37
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:108
impeller::TRect::origin
TPoint< Type > origin
Definition: rect.h:23
impeller::TRect::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const PointIter first, const PointIter last)
Definition: rect.h:57
impeller::TRect::Shift
constexpr TRect< T > Shift(TPoint< T > offset) const
Returns a new rectangle translated by the given offset.
Definition: rect.h:272
impeller::TRect::Scale
constexpr TRect Scale(TPoint< T > scale) const
Definition: rect.h:113
impeller::TSize::IsZero
constexpr bool IsZero() const
Definition: size.h:103
impeller::TSize::width
Type width
Definition: size.h:21
impeller::TRect::Contains
constexpr bool Contains(const TPoint< Type > &p) const
Definition: rect.h:122
impeller::TPoint::x
Type x
Definition: point.h:23
scalar.h
impeller::TRect::Cutout
constexpr std::optional< TRect< T > > Cutout(const TRect &o) const
Returns the new boundary rectangle that would result from the rectangle being cutout by a second rect...
Definition: rect.h:240
impeller::TRect::GetRight
constexpr auto GetRight() const
Definition: rect.h:151
impeller::TRect::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:52
std
Definition: comparable.h:98
impeller::TRect::operator*
constexpr TRect operator*(const TRect &r) const
Definition: rect.h:99
impeller::TRect::Project
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:299
impeller::TPoint< Type >
impeller::TRect::MakeMaximum
constexpr static TRect MakeMaximum()
Definition: rect.h:75
impeller::TRect::TRect
constexpr TRect(TPoint< Type > origin, TSize< Type > size)
Definition: rect.h:30
impeller::TRect::TRect
constexpr TRect(const Type components[4])
Definition: rect.h:33
impeller::TRect::Union
constexpr TRect Union(const TRect &o) const
Definition: rect.h:209
impeller::TRect::GetBottom
constexpr auto GetBottom() const
Definition: rect.h:158
impeller::TSize::height
Type height
Definition: size.h:22
impeller::TRect::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:40
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Definition: size.h:105
impeller::TRect::GetPositive
constexpr TRect GetPositive() const
Get a version of this rectangle that has a non-negative size.
Definition: rect.h:180
impeller::TRect::GetRightBottom
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:171
impeller
Definition: aiks_context.cc:10
impeller::TRect::TRect
constexpr TRect(const TRect< U > &other)
Definition: rect.h:83
impeller::TRect::GetTop
constexpr auto GetTop() const
Definition: rect.h:144
impeller::TRect
Definition: rect.h:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
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:279