 |
Flutter Impeller
|
|
Go to the documentation of this file.
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_RECT_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_RECT_H_
13 #include "fml/logging.h"
25 constexpr
TRect() : origin({0, 0}), size({0, 0}) {}
31 return TRect(left, top, right - left, bottom - top);
35 return TRect(x, y, width, height);
40 return TRect(origin, size);
53 template <
typename Po
intIter>
55 const PointIter last) {
61 auto right = first->x;
62 auto bottom = first->y;
63 for (
auto it = first + 1; it < last; ++it) {
64 left = std::min(left, it->x);
65 top = std::min(top, it->y);
66 right = std::max(right, it->x);
67 bottom = std::max(bottom, it->y);
74 -std::numeric_limits<Type>::infinity(),
75 std::numeric_limits<Type>::infinity(),
76 std::numeric_limits<Type>::infinity());
81 : origin(static_cast<T>(other.
GetX()), static_cast<T>(other.
GetY())),
82 size(static_cast<T>(other.
GetWidth()),
86 return TRect({origin.
x + r.origin.
x, origin.
y + r.origin.
y},
91 return TRect({origin.
x - r.origin.
x, origin.
y - r.origin.
y},
100 return TRect({origin.
x * r.origin.
x, origin.
y * r.origin.
y},
105 return origin == r.origin && size == r.size;
109 return TRect({origin.
x * scale, origin.
y * scale},
114 return TRect({origin.
x * scale_x, origin.
y * scale_y},
119 return TRect({origin.
x * scale.
x, origin.
y * scale.
y},
163 [[nodiscard]] constexpr
Type GetX()
const {
return origin.
x; }
167 [[nodiscard]] constexpr
Type GetY()
const {
return origin.
y; }
177 [[nodiscard]] constexpr
auto GetLeft()
const {
179 return -std::numeric_limits<Type>::infinity();
181 return std::min(origin.
x, origin.
x + size.
width);
184 [[nodiscard]] constexpr
auto GetTop()
const {
186 return -std::numeric_limits<Type>::infinity();
188 return std::min(origin.
y, origin.
y + size.
height);
193 return std::numeric_limits<Type>::infinity();
195 return std::max(origin.
x, origin.
x + size.
width);
200 return std::numeric_limits<Type>::infinity();
202 return std::max(origin.
y, origin.
y + size.
height);
222 [[nodiscard]] constexpr T
Area()
const {
return size.
Area(); }
229 [[nodiscard]] constexpr std::array<T, 4>
GetLTRB()
const {
235 [[nodiscard]] constexpr std::array<T, 4>
GetXYWH()
const {
242 return MakeLTRB(ltrb[0], ltrb[1], ltrb[2], ltrb[3]);
247 [[nodiscard]] constexpr std::array<TPoint<T>, 4>
GetPoints()
const {
248 auto [left, top, right, bottom] =
GetLTRB();
254 const Matrix& transform)
const {
256 for (
size_t i = 0; i < points.size(); i++) {
257 points[i] = transform * points[i];
267 if (bounds.has_value()) {
268 return bounds.value();
288 if (sx != 0.0 && sy != 0.0 && 0.0 * sx * sy * tx * ty == 0.0) {
290 return Matrix( sx, 0.0f, 0.0f, 0.0f,
291 0.0f, sy, 0.0f, 0.0f,
292 0.0f, 0.0f, 1.0f, 0.0f,
306 std::min(this_ltrb[1], other_ltrb[1]),
307 std::max(this_ltrb[2], other_ltrb[2]),
308 std::max(this_ltrb[3], other_ltrb[3])
313 const TRect& o)
const {
318 std::max(this_ltrb[1], other_ltrb[1]),
319 std::min(this_ltrb[2], other_ltrb[2]),
320 std::min(this_ltrb[3], other_ltrb[3])
322 if (intersection.size.IsEmpty()) {
334 [[nodiscard]] constexpr std::optional<TRect<T>>
Cutout(
const TRect& o)
const {
335 const auto& [a_left, a_top, a_right, a_bottom] =
GetLTRB();
336 const auto& [b_left, b_top, b_right, b_bottom] = o.
GetLTRB();
337 if (b_left <= a_left && b_right >= a_right) {
338 if (b_top <= a_top && b_bottom >= a_bottom) {
342 if (b_top <= a_top && b_bottom > a_top) {
346 if (b_bottom >= a_bottom && b_top < a_bottom) {
351 if (b_top <= a_top && b_bottom >= a_bottom) {
352 if (b_left <= a_left && b_right > a_left) {
356 if (b_right >= a_right && b_left < a_right) {
367 return TRect(origin.
x + offset.
x, origin.
y + offset.
y, size.
width,
377 return TRect(origin.
x - left,
379 size.
width + left + right,
380 size.
height + top + bottom);
386 return TRect(origin.
x - amount,
388 size.
width + amount * 2,
389 size.
height + amount * 2);
395 T vertical_amount)
const {
396 return TRect(origin.
x - horizontal_amount,
397 origin.
y - vertical_amount,
398 size.
width + horizontal_amount * 2,
399 size.
height + vertical_amount * 2);
405 return TRect(origin.
x - amount.
x,
407 size.
width + amount.
x * 2,
425 return source.
Shift(-origin).Scale(
435 [[nodiscard]] constexpr
static std::optional<TRect>
Union(
437 const std::optional<TRect> b) {
438 return b.has_value() ? a.
Union(b.value()) : a;
441 [[nodiscard]] constexpr
static std::optional<TRect>
Union(
442 const std::optional<TRect> a,
447 [[nodiscard]] constexpr
static std::optional<TRect>
Union(
448 const std::optional<TRect> a,
449 const std::optional<TRect> b) {
450 return a.has_value() ?
Union(a.value(), b) : b;
455 const std::optional<TRect> b) {
460 const std::optional<TRect> a,
466 const std::optional<TRect> a,
467 const std::optional<TRect> b) {
473 : origin(x, y), size(width, height) {}
475 constexpr
TRect(TPoint<Type> origin, TSize<Type> size)
476 : origin(origin), size(size) {}
504 #endif // FLUTTER_IMPELLER_GEOMETRY_RECT_H_
constexpr static std::optional< TRect > Union(const std::optional< TRect > a, const TRect &b)
constexpr std::array< T, 4 > GetLTRB() const
constexpr TRect< T > Expand(T horizontal_amount, T vertical_amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
constexpr bool IsSquare() const
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
constexpr TRect< T > Expand(T amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
constexpr TRect operator-(const TRect &r) const
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
constexpr bool IsMaximum() const
constexpr TPoint< T > GetLeftTop() const
constexpr TRect Scale(Type scale_x, Type scale_y) const
constexpr Matrix GetNormalizingTransform() const
Constructs a Matrix that will map all points in the coordinate space of the rectangle into a new norm...
constexpr static TRect RoundOut(const TRect &r)
constexpr TRect operator+(const TRect &r) const
constexpr static std::optional< TRect > Intersection(const TRect &a, const std::optional< TRect > b)
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
constexpr Point GetCenter() const
Get the center point as a |Point|.
constexpr Type GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
constexpr TPoint< Type > GetOrigin() const
Returns the upper left corner of the rectangle as specified when it was constructed.
constexpr TPoint< T > GetRightTop() const
constexpr bool Contains(const TRect &o) const
constexpr bool operator==(const TRect &r) const
constexpr bool IntersectsWithRect(const TRect &o) const
constexpr std::array< TPoint< T >, 4 > GetPoints() const
Get the points that represent the 4 corners of this rectangle. The order is: Top left,...
constexpr static std::optional< TRect > MakePointBounds(const U &value)
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
constexpr TRect Scale(TSize< T > scale) const
constexpr std::optional< TRect< T > > Intersection(const TRect &o) const
constexpr auto GetLeft() const
constexpr std::array< TPoint< T >, 4 > GetTransformedPoints(const Matrix &transform) const
constexpr TRect operator*(Type scale) const
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
constexpr TPoint< T > GetLeftBottom() const
constexpr bool IsSquare() const
Returns true if width and height are equal and neither is NaN.
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
constexpr TRect Scale(Type scale) const
constexpr static std::optional< TRect > MakePointBounds(const PointIter first, const PointIter last)
constexpr TRect< T > Shift(TPoint< T > offset) const
Returns a new rectangle translated by the given offset.
constexpr TRect Scale(TPoint< T > scale) const
constexpr bool Contains(const TPoint< Type > &p) const
constexpr TRect< T > Expand(TPoint< T > amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
constexpr TRect< T > Expand(TSize< T > amount) const
Returns a rectangle with expanded edges in all directions. Negative expansion results in shrinking.
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...
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle as specified when it was constructed and which may be negative in e...
constexpr auto GetRight() const
constexpr Type Area() const
constexpr static TRect MakeSize(const TSize< U > &size)
constexpr TRect operator*(const TRect &r) const
constexpr TRect< T > Project(TRect< T > source) const
Returns a new rectangle that represents the projection of the source rectangle onto this rectangle....
constexpr static TRect MakeMaximum()
constexpr TRect Union(const TRect &o) const
constexpr T Area() const
Get the area of the rectangle, equivalent to |GetSize().Area()|.
constexpr auto GetBottom() const
constexpr static std::optional< TRect > Union(const std::optional< TRect > a, const std::optional< TRect > b)
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
constexpr TRect GetPositive() const
Get a version of this rectangle that has a non-negative size.
constexpr TPoint< T > GetRightBottom() const
constexpr static std::optional< TRect > Intersection(const std::optional< TRect > a, const TRect &b)
constexpr Type GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
constexpr std::array< T, 4 > GetXYWH() const
Get the x, y coordinates of the origin and the width and height of the rectangle in an array.
static constexpr Matrix MakeScale(const Vector3 &s)
constexpr TRect(const TRect< U > &other)
constexpr auto GetTop() const
A 4x4 matrix using column-major storage.
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
constexpr static std::optional< TRect > Union(const TRect &a, const std::optional< TRect > b)
constexpr static std::optional< TRect > Intersection(const std::optional< TRect > a, const std::optional< TRect > b)