| 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 | |
| 12 | #include "impeller/geometry/matrix.h" |
| 13 | #include "impeller/geometry/point.h" |
| 14 | #include "impeller/geometry/scalar.h" |
| 15 | #include "impeller/geometry/size.h" |
| 16 | |
| 17 | namespace impeller { |
| 18 | |
| 19 | template <class T> |
| 20 | struct TRect { |
| 21 | using Type = T; |
| 22 | |
| 23 | TPoint<Type> origin; |
| 24 | TSize<Type> size; |
| 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 | |
| 30 | constexpr TRect(TPoint<Type> origin, TSize<Type> size) |
| 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(left: -std::numeric_limits<Type>::infinity(), |
| 77 | top: -std::numeric_limits<Type>::infinity(), |
| 78 | right: std::numeric_limits<Type>::infinity(), |
| 79 | bottom: 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(left: ltrb[0], top: ltrb[1], right: ltrb[2], bottom: 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(left: std::min(this_ltrb[0], other_ltrb[0]), // |
| 213 | top: std::min(this_ltrb[1], other_ltrb[1]), // |
| 214 | right: std::max(this_ltrb[2], other_ltrb[2]), // |
| 215 | bottom: 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(left: std::max(this_ltrb[0], other_ltrb[0]), // |
| 224 | top: std::max(this_ltrb[1], other_ltrb[1]), // |
| 225 | right: std::min(this_ltrb[2], other_ltrb[2]), // |
| 226 | bottom: 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(left: a_left, top: b_bottom, right: a_right, bottom: a_bottom); |
| 251 | } |
| 252 | if (b_bottom >= a_bottom && b_top < a_bottom) { |
| 253 | // Cuts out the bottom. |
| 254 | return TRect::MakeLTRB(left: a_left, top: a_top, right: a_right, bottom: 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(left: b_right, top: a_top, right: a_right, bottom: a_bottom); |
| 261 | } |
| 262 | if (b_right >= a_right && b_left < a_right) { |
| 263 | // Cuts out the right. |
| 264 | return TRect::MakeLTRB(left: a_left, top: a_top, right: b_left, bottom: 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 | |
| 306 | using Rect = TRect<Scalar>; |
| 307 | using IRect = TRect<int64_t>; |
| 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 |
| 321 | |