| Flutter Impeller |

size.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_SIZE_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_SIZE_H_
7
8 #include <algorithm>
9 #include <cmath>
10 #include <limits>
11 #include <ostream>
12 #include <string>
13
14 #include "impeller/geometry/scalar.h"
15
16 namespace impeller {
17
18 template <class T>
19 struct TSize {
20 using Type = T;
21
22 Type width = {};
23 Type height = {};
24
25 constexpr TSize() {}
26
27 constexpr TSize(Type width, Type height) : width(width), height(height) {}
28
29 template <class U>
30 explicit constexpr TSize(const TSize<U>& other)
31 : TSize(static_cast<Type>(other.width), static_cast<Type>(other.height)) {
32 }
33
34 static constexpr TSize MakeWH(Type width, Type height) {
35 return TSize{width, height};
36 }
37
38 static constexpr TSize Infinite() {
39 return TSize{std::numeric_limits<Type>::max(),
40 std::numeric_limits<Type>::max()};
41 }
42
43 constexpr TSize operator*(Scalar scale) const {
44 return {width * scale, height * scale};
45 }
46
47 constexpr TSize operator/(Scalar scale) const {
48 return {static_cast<Scalar>(width) / scale,
49 static_cast<Scalar>(height) / scale};
50 }
51
52 constexpr TSize operator/(const TSize& s) const {
53 return {width / s.width, height / s.height};
54 }
55
56 constexpr bool operator==(const TSize& s) const {
57 return s.width == width && s.height == height;
58 }
59
60 constexpr bool operator!=(const TSize& s) const {
61 return s.width != width || s.height != height;
62 }
63
64 constexpr TSize operator+(const TSize& s) const {
65 return {width + s.width, height + s.height};
66 }
67
68 constexpr TSize operator-(const TSize& s) const {
69 return {width - s.width, height - s.height};
70 }
71
72 constexpr TSize operator-() const { return {-width, -height}; }
73
74 constexpr TSize Min(const TSize& o) const {
75 return {
76 std::min(width, o.width),
77 std::min(height, o.height),
78 };
79 }
80
81 constexpr TSize Max(const TSize& o) const {
82 return {
83 std::max(width, o.width),
84 std::max(height, o.height),
85 };
86 }
87
88 constexpr Type MaxDimension() const { return std::max(width, height); }
89
90 constexpr TSize Abs() const { return {std::fabs(width), std::fabs(height)}; }
91
92 constexpr TSize Floor() const {
93 return {std::floor(width), std::floor(height)};
94 }
95
96 constexpr TSize Ceil() const { return {std::ceil(width), std::ceil(height)}; }
97
98 constexpr TSize Round() const {
99 return {std::round(width), std::round(height)};
100 }
101
102 constexpr Type Area() const { return width * height; }
103
104 /// Returns true if either of the width or height are 0, negative, or NaN.
105 constexpr bool IsEmpty() const { return !(width > 0 && height > 0); }
106
107 constexpr bool IsSquare() const { return width == height; }
108
109 template <class U>
110 static constexpr TSize Ceil(const TSize<U>& other) {
111 return TSize{static_cast<Type>(std::ceil(other.width)),
112 static_cast<Type>(std::ceil(other.height))};
113 }
114
115 constexpr size_t MipCount() const {
116 constexpr size_t minimum_mip = 1u;
117 if (IsEmpty()) {
118 return minimum_mip;
119 }
120 size_t result = std::max(ceil(log2(width)), ceil(log2(height)));
121 return std::max(result, minimum_mip);
122 }
123 };
124
125 // RHS algebraic operations with arithmetic types.
126
127 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
128 constexpr TSize<T> operator*(U s, const TSize<T>& p) {
129 return p * s;
130 }
131
132 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
133 constexpr TSize<T> operator/(U s, const TSize<T>& p) {
134 return {static_cast<T>(s) / p.width, static_cast<T>(s) / p.height};
135 }
136
137 using Size = TSize<Scalar>;
138 using ISize = TSize<int64_t>;
139
140 static_assert(sizeof(Size) == 2 * sizeof(Scalar));
141
142 } // namespace impeller
143
144 namespace std {
145
146 template <class T>
147 inline std::ostream& operator<<(std::ostream& out,
148 const impeller::TSize<T>& s) {
149 out << "(" << s.width << ", " << s.height << ")";
150 return out;
151 }
152
153 } // namespace std
154
155 #endif // FLUTTER_IMPELLER_GEOMETRY_SIZE_H_
impeller::TSize::operator/
constexpr TSize operator/(Scalar scale) const
Definition: size.h:47
impeller::TSize::operator==
constexpr bool operator==(const TSize &s) const
Definition: size.h:56
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TSize::IsSquare
constexpr bool IsSquare() const
Definition: size.h:107
impeller::TSize::Ceil
constexpr TSize Ceil() const
Definition: size.h:96
impeller::TSize::Min
constexpr TSize Min(const TSize &o) const
Definition: size.h:74
impeller::TSize::MaxDimension
constexpr Type MaxDimension() const
Definition: size.h:88
impeller::TSize::TSize
constexpr TSize()
Definition: size.h:25
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:951
impeller::operator*
constexpr Color operator*(T value, const Color &c)
Definition: color.h:901
impeller::TSize::operator+
constexpr TSize operator+(const TSize &s) const
Definition: size.h:64
impeller::TSize< int64_t >::Type
int64_t Type
Definition: size.h:20
impeller::TSize
Definition: size.h:19
impeller::TSize::Round
constexpr TSize Round() const
Definition: size.h:98
impeller::TSize::TSize
constexpr TSize(const TSize< U > &other)
Definition: size.h:30
impeller::TSize::operator-
constexpr TSize operator-() const
Definition: size.h:72
impeller::TSize::Max
constexpr TSize Max(const TSize &o) const
Definition: size.h:81
impeller::TSize::Abs
constexpr TSize Abs() const
Definition: size.h:90
impeller::TSize::operator/
constexpr TSize operator/(const TSize &s) const
Definition: size.h:52
impeller::TSize::width
Type width
Definition: size.h:22
scalar.h
impeller::TSize::operator!=
constexpr bool operator!=(const TSize &s) const
Definition: size.h:60
impeller::TSize::Ceil
static constexpr TSize Ceil(const TSize< U > &other)
Definition: size.h:110
impeller::TSize::Area
constexpr Type Area() const
Definition: size.h:102
std
Definition: comparable.h:95
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
impeller::operator/
constexpr Color operator/(T value, const Color &c)
Definition: color.h:906
impeller::TSize::Infinite
static constexpr TSize Infinite()
Definition: size.h:38
impeller::TSize::height
Type height
Definition: size.h:23
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:105
impeller::TSize::MipCount
constexpr size_t MipCount() const
Definition: size.h:115
impeller::TSize::Floor
constexpr TSize Floor() const
Definition: size.h:92
impeller::TSize::MakeWH
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:34
impeller
Definition: aiks_blur_unittests.cc:20
impeller::TSize::operator-
constexpr TSize operator-(const TSize &s) const
Definition: size.h:68
impeller::TSize::TSize
constexpr TSize(Type width, Type height)
Definition: size.h:27
impeller::TSize::operator*
constexpr TSize operator*(Scalar scale) const
Definition: size.h:43