Flutter Engine
 
Loading...
Searching...
No Matches
size.h File Reference
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <limits>
#include <ostream>
#include <string>
#include "impeller/geometry/scalar.h"

Go to the source code of this file.

Classes

struct  impeller::TSize< T >
 

Namespaces

namespace  impeller
 
namespace  std
 

Macros

#define ONLY_ON_FLOAT_M(Modifiers, Return)
 
#define ONLY_ON_FLOAT(Return)   DL_ONLY_ON_FLOAT_M(, Return)
 

Typedefs

using impeller::Size = TSize< Scalar >
 
using impeller::ISize32 = TSize< int32_t >
 
using impeller::ISize64 = TSize< int64_t >
 
using impeller::ISize = ISize64
 

Functions

template<class T , class U , class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TSize< T > impeller::operator* (U s, const TSize< T > &p)
 
template<class T , class U , class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TSize< T > impeller::operator/ (U s, const TSize< T > &p)
 
template<class T >
std::ostream & std::operator<< (std::ostream &out, const impeller::TSize< T > &s)
 

Macro Definition Documentation

◆ ONLY_ON_FLOAT

#define ONLY_ON_FLOAT (   Return)    DL_ONLY_ON_FLOAT_M(, Return)

Definition at line 22 of file size.h.

◆ ONLY_ON_FLOAT_M

#define ONLY_ON_FLOAT_M (   Modifiers,
  Return 
)
Value:
template <typename U = T> \
Modifiers std::enable_if_t<std::is_floating_point_v<U>, Return>

Definition at line 19 of file size.h.

25 {
26 using Type = T;
27
28 Type width = {};
29 Type height = {};
30
31 constexpr TSize() {}
32
33 constexpr TSize(Type width, Type height) : width(width), height(height) {}
34
35 constexpr explicit TSize(Type dimension)
36 : width(dimension), height(dimension) {}
37
38 template <class U>
39 explicit constexpr TSize(const TSize<U>& other)
40 : TSize(static_cast<Type>(other.width), static_cast<Type>(other.height)) {
41 }
42
43 static constexpr TSize MakeWH(Type width, Type height) {
44 return TSize{width, height};
45 }
46
47 static constexpr TSize Infinite() {
48 return TSize{std::numeric_limits<Type>::max(),
49 std::numeric_limits<Type>::max()};
50 }
51
52 constexpr TSize operator*(Scalar scale) const {
53 return {width * scale, height * scale};
54 }
55
56 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
57 inline TSize operator*=(U scale) {
58 width *= static_cast<Type>(scale);
59 height *= static_cast<Type>(scale);
60 return *this;
61 }
62
63 constexpr TSize operator/(Scalar scale) const {
64 return {static_cast<Scalar>(width) / scale,
65 static_cast<Scalar>(height) / scale};
66 }
67
68 constexpr TSize operator/(const TSize& s) const {
69 return {width / s.width, height / s.height};
70 }
71
72 constexpr bool operator==(const TSize& s) const {
73 return s.width == width && s.height == height;
74 }
75
76 constexpr bool operator!=(const TSize& s) const {
77 return s.width != width || s.height != height;
78 }
79
80 constexpr TSize operator+(const TSize& s) const {
81 return {width + s.width, height + s.height};
82 }
83
84 constexpr TSize operator-(const TSize& s) const {
85 return {width - s.width, height - s.height};
86 }
87
88 constexpr TSize operator-() const { return {-width, -height}; }
89
90 constexpr TSize Min(const TSize& o) const {
91 return {
92 std::min(width, o.width),
93 std::min(height, o.height),
94 };
95 }
96
97 constexpr TSize Max(const TSize& o) const {
98 return {
99 std::max(width, o.width),
100 std::max(height, o.height),
101 };
102 }
103
104 constexpr Type MinDimension() const { return std::min(width, height); }
105
106 constexpr Type MaxDimension() const { return std::max(width, height); }
107
108 constexpr TSize Abs() const { return {std::fabs(width), std::fabs(height)}; }
109
110 constexpr TSize Floor() const {
111 return {std::floor(width), std::floor(height)};
112 }
113
114 constexpr TSize Ceil() const { return {std::ceil(width), std::ceil(height)}; }
115
116 constexpr TSize Round() const {
117 return {std::round(width), std::round(height)};
118 }
119
120 constexpr Type Area() const { return width * height; }
121
122 /// Returns true if either of the width or height are 0, negative, or NaN.
123 constexpr bool IsEmpty() const { return !(width > 0 && height > 0); }
124
125 ONLY_ON_FLOAT_M(constexpr, bool)
126 IsFinite() const { return std::isfinite(width) && std::isfinite(height); }
127
128 constexpr bool IsSquare() const { return width == height; }
129
130 template <class U>
131 static constexpr TSize Ceil(const TSize<U>& other) {
132 return TSize{static_cast<Type>(std::ceil(other.width)),
133 static_cast<Type>(std::ceil(other.height))};
134 }
135
136 /// Return the mip count of the texture.
137 constexpr size_t MipCount() const {
138 constexpr size_t minimum_mip = 1u;
139 if (IsEmpty() || width <= 0 || height <= 0) {
140 return minimum_mip;
141 }
142 size_t result = std::min(log2(width), log2(height));
143 return std::max(result, minimum_mip);
144 }
145};
146
147// RHS algebraic operations with arithmetic types.
148
149template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
150constexpr TSize<T> operator*(U s, const TSize<T>& p) {
151 return p * s;
152}
153
154template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
155constexpr TSize<T> operator/(U s, const TSize<T>& p) {
156 return {static_cast<T>(s) / p.width, static_cast<T>(s) / p.height};
157}
158
159using Size = TSize<Scalar>;
160using ISize32 = TSize<int32_t>;
161using ISize64 = TSize<int64_t>;
162using ISize = ISize64;
163
164static_assert(sizeof(Size) == 2 * sizeof(Scalar));
165
166} // namespace impeller
167
168namespace std {
169
170template <class T>
171inline std::ostream& operator<<(std::ostream& out,
172 const impeller::TSize<T>& s) {
173 out << "(" << s.width << ", " << s.height << ")";
174 return out;
175}
176
177} // namespace std
178
179#endif // FLUTTER_IMPELLER_GEOMETRY_SIZE_H_
bool operator==(const FlutterPoint &a, const FlutterPoint &b)
#define ONLY_ON_FLOAT_M(Modifiers, Return)
Definition size.h:19
bool operator!=(const T &value, const UniqueObject< T, Traits > &object)
constexpr Color operator-(T value, const Color &c)
Definition color.h:903
float Scalar
Definition scalar.h:19
constexpr Color operator/(T value, const Color &c)
Definition color.h:914
constexpr Color operator+(T value, const Color &c)
Definition color.h:898
TSize< int32_t > ISize32
Definition size.h:160
TSize< Scalar > Size
Definition size.h:159
constexpr Color operator*(T value, const Color &c)
Definition color.h:909
TSize< int64_t > ISize64
Definition size.h:161
ISize64 ISize
Definition size.h:162
Definition ref_ptr.h:261
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition arc.h:141
int32_t height
int32_t width
Type height
Definition size.h:29
Type width
Definition size.h:28