| Flutter Impeller |

vector.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 <cmath>
8 #include <string>
9
10 #include "impeller/geometry/color.h"
11 #include "impeller/geometry/point.h"
12 #include "impeller/geometry/scalar.h"
13 #include "impeller/geometry/size.h"
14
15 namespace impeller {
16
17 struct Vector3 {
18 union {
19 struct {
20 Scalar x = 0.0f;
21 Scalar y = 0.0f;
22 Scalar z = 0.0f;
23 };
24 Scalar e[3];
25 };
26
27 constexpr Vector3(){};
28
29 constexpr Vector3(const Color& c) : x(c.red), y(c.green), z(c.blue) {}
30
31 constexpr Vector3(const Point& p) : x(p.x), y(p.y) {}
32
33 constexpr Vector3(const Size& s) : x(s.width), y(s.height) {}
34
35 constexpr Vector3(Scalar x, Scalar y) : x(x), y(y) {}
36
37 constexpr Vector3(Scalar x, Scalar y, Scalar z) : x(x), y(y), z(z) {}
38
39 /**
40 * The length (or magnitude of the vector).
41 *
42 * @return the calculated length.
43 */
44 constexpr Scalar Length() const { return sqrt(x * x + y * y + z * z); }
45
46 constexpr Vector3 Normalize() const {
47 const auto len = Length();
48 return {x / len, y / len, z / len};
49 }
50
51 constexpr Scalar Dot(const Vector3& other) const {
52 return ((x * other.x) + (y * other.y) + (z * other.z));
53 }
54
55 constexpr Vector3 Abs() const {
56 return {std::fabs(x), std::fabs(y), std::fabs(z)};
57 }
58
59 constexpr Vector3 Cross(const Vector3& other) const {
60 return {
61 (y * other.z) - (z * other.y), //
62 (z * other.x) - (x * other.z), //
63 (x * other.y) - (y * other.x) //
64 };
65 }
66
67 constexpr Vector3 Min(const Vector3& p) const {
68 return {std::min(x, p.x), std::min(y, p.y), std::min(z, p.z)};
69 }
70
71 constexpr Vector3 Max(const Vector3& p) const {
72 return {std::max(x, p.x), std::max(y, p.y), std::max(z, p.z)};
73 }
74
75 constexpr Vector3 Floor() const {
76 return {std::floor(x), std::floor(y), std::floor(z)};
77 }
78
79 constexpr Vector3 Ceil() const {
80 return {std::ceil(x), std::ceil(y), std::ceil(z)};
81 }
82
83 constexpr Vector3 Round() const {
84 return {std::round(x), std::round(y), std::round(z)};
85 }
86
87 constexpr bool operator==(const Vector3& v) const {
88 return v.x == x && v.y == y && v.z == z;
89 }
90
91 constexpr bool operator!=(const Vector3& v) const {
92 return v.x != x || v.y != y || v.z != z;
93 }
94
95 constexpr Vector3 operator+=(const Vector3& p) {
96 x += p.x;
97 y += p.y;
98 z += p.z;
99 return *this;
100 }
101
102 constexpr Vector3 operator-=(const Vector3& p) {
103 x -= p.x;
104 y -= p.y;
105 z -= p.z;
106 return *this;
107 }
108
109 constexpr Vector3 operator*=(const Vector3& p) {
110 x *= p.x;
111 y *= p.y;
112 z *= p.z;
113 return *this;
114 }
115
116 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
117 constexpr Vector3 operator*=(U scale) {
118 x *= scale;
119 y *= scale;
120 z *= scale;
121 return *this;
122 }
123
124 constexpr Vector3 operator/=(const Vector3& p) {
125 x /= p.x;
126 y /= p.y;
127 z /= p.z;
128 return *this;
129 }
130
131 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
132 constexpr Vector3 operator/=(U scale) {
133 x /= scale;
134 y /= scale;
135 z /= scale;
136 return *this;
137 }
138
139 constexpr Vector3 operator-() const { return Vector3(-x, -y, -z); }
140
141 constexpr Vector3 operator+(const Vector3& v) const {
142 return Vector3(x + v.x, y + v.y, z + v.z);
143 }
144
145 constexpr Vector3 operator-(const Vector3& v) const {
146 return Vector3(x - v.x, y - v.y, z - v.z);
147 }
148
149 constexpr Vector3 operator+(Scalar s) const {
150 return Vector3(x + s, y + s, z + s);
151 }
152
153 constexpr Vector3 operator-(Scalar s) const {
154 return Vector3(x - s, y - s, z - s);
155 }
156
157 constexpr Vector3 operator*(const Vector3& v) const {
158 return Vector3(x * v.x, y * v.y, z * v.z);
159 }
160
161 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
162 constexpr Vector3 operator*(U scale) const {
163 return Vector3(x * scale, y * scale, z * scale);
164 }
165
166 constexpr Vector3 operator/(const Vector3& v) const {
167 return Vector3(x / v.x, y / v.y, z / v.z);
168 }
169
170 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
171 constexpr Vector3 operator/(U scale) const {
172 return Vector3(x / scale, y / scale, z / scale);
173 }
174
175 constexpr Vector3 Lerp(const Vector3& v, Scalar t) const {
176 return *this + (v - *this) * t;
177 }
178
179 /**
180 * Make a linear combination of two vectors and return the result.
181 *
182 * @param a the first vector.
183 * @param aScale the scale to use for the first vector.
184 * @param b the second vector.
185 * @param bScale the scale to use for the second vector.
186 *
187 * @return the combined vector.
188 */
189 static constexpr Vector3 Combine(const Vector3& a,
190 Scalar aScale,
191 const Vector3& b,
192 Scalar bScale) {
193 return {
194 aScale * a.x + bScale * b.x, //
195 aScale * a.y + bScale * b.y, //
196 aScale * a.z + bScale * b.z, //
197 };
198 }
199
200 std::string ToString() const;
201 };
202
203 // RHS algebraic operations with arithmetic types.
204
205 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
206 constexpr Vector3 operator*(U s, const Vector3& p) {
207 return p * s;
208 }
209
210 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
211 constexpr Vector3 operator+(U s, const Vector3& p) {
212 return p + s;
213 }
214
215 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
216 constexpr Vector3 operator-(U s, const Vector3& p) {
217 return -p + s;
218 }
219
220 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
221 constexpr Vector3 operator/(U s, const Vector3& p) {
222 return {
223 static_cast<Scalar>(s) / p.x,
224 static_cast<Scalar>(s) / p.y,
225 static_cast<Scalar>(s) / p.z,
226 };
227 }
228
229 struct Vector4 {
230 union {
231 struct {
232 Scalar x = 0.0f;
233 Scalar y = 0.0f;
234 Scalar z = 0.0f;
235 Scalar w = 1.0f;
236 };
237 Scalar e[4];
238 };
239
240 constexpr Vector4() {}
241
242 constexpr Vector4(const Color& c)
243 : x(c.red), y(c.green), z(c.blue), w(c.alpha) {}
244
245 constexpr Vector4(Scalar x, Scalar y, Scalar z, Scalar w)
246 : x(x), y(y), z(z), w(w) {}
247
248 constexpr Vector4(const Vector3& v) : x(v.x), y(v.y), z(v.z) {}
249
250 constexpr Vector4(const Point& p) : x(p.x), y(p.y) {}
251
252 Vector4 Normalize() const {
253 const Scalar inverse = 1.0f / sqrt(x * x + y * y + z * z + w * w);
254 return Vector4(x * inverse, y * inverse, z * inverse, w * inverse);
255 }
256
257 constexpr bool operator==(const Vector4& v) const {
258 return (x == v.x) && (y == v.y) && (z == v.z) && (w == v.w);
259 }
260
261 constexpr bool operator!=(const Vector4& v) const {
262 return (x != v.x) || (y != v.y) || (z != v.z) || (w != v.w);
263 }
264
265 constexpr Vector4 operator+(const Vector4& v) const {
266 return Vector4(x + v.x, y + v.y, z + v.z, w + v.w);
267 }
268
269 constexpr Vector4 operator-(const Vector4& v) const {
270 return Vector4(x - v.x, y - v.y, z - v.z, w - v.w);
271 }
272
273 constexpr Vector4 operator*(Scalar f) const {
274 return Vector4(x * f, y * f, z * f, w * f);
275 }
276
277 constexpr Vector4 operator*(const Vector4& v) const {
278 return Vector4(x * v.x, y * v.y, z * v.z, w * v.w);
279 }
280
281 constexpr Vector4 Min(const Vector4& p) const {
282 return {std::min(x, p.x), std::min(y, p.y), std::min(z, p.z),
283 std::min(w, p.w)};
284 }
285
286 constexpr Vector4 Max(const Vector4& p) const {
287 return {std::max(x, p.x), std::max(y, p.y), std::max(z, p.z),
288 std::max(w, p.w)};
289 }
290
291 constexpr Vector4 Floor() const {
292 return {std::floor(x), std::floor(y), std::floor(z), std::floor(w)};
293 }
294
295 constexpr Vector4 Ceil() const {
296 return {std::ceil(x), std::ceil(y), std::ceil(z), std::ceil(w)};
297 }
298
299 constexpr Vector4 Round() const {
300 return {std::round(x), std::round(y), std::round(z), std::round(w)};
301 }
302
303 constexpr Vector4 Lerp(const Vector4& v, Scalar t) const {
304 return *this + (v - *this) * t;
305 }
306
307 std::string ToString() const;
308 };
309
310 static_assert(sizeof(Vector3) == 3 * sizeof(Scalar));
311 static_assert(sizeof(Vector4) == 4 * sizeof(Scalar));
312
313 } // namespace impeller
314
315 namespace std {
316
317 inline std::ostream& operator<<(std::ostream& out, const impeller::Vector3& p) {
318 out << "(" << p.x << ", " << p.y << ", " << p.z << ")";
319 return out;
320 }
321
322 inline std::ostream& operator<<(std::ostream& out, const impeller::Vector4& p) {
323 out << "(" << p.x << ", " << p.y << ", " << p.z << ", " << p.w << ")";
324 return out;
325 }
326
327 } // namespace std
impeller::Vector4::operator-
constexpr Vector4 operator-(const Vector4 &v) const
Definition: vector.h:269
impeller::Vector3::Dot
constexpr Scalar Dot(const Vector3 &other) const
Definition: vector.h:51
impeller::Vector3::operator==
constexpr bool operator==(const Vector3 &v) const
Definition: vector.h:87
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Vector3::Abs
constexpr Vector3 Abs() const
Definition: vector.h:55
impeller::Vector3::Combine
static constexpr Vector3 Combine(const Vector3 &a, Scalar aScale, const Vector3 &b, Scalar bScale)
Definition: vector.h:189
impeller::Vector3::Cross
constexpr Vector3 Cross(const Vector3 &other) const
Definition: vector.h:59
impeller::Vector3::Vector3
constexpr Vector3(const Color &c)
Definition: vector.h:29
impeller::Vector3::operator+
constexpr Vector3 operator+(const Vector3 &v) const
Definition: vector.h:141
impeller::Vector3::operator*
constexpr Vector3 operator*(const Vector3 &v) const
Definition: vector.h:157
impeller::Vector4::Normalize
Vector4 Normalize() const
Definition: vector.h:252
impeller::Vector3::Round
constexpr Vector3 Round() const
Definition: vector.h:83
impeller::Vector4::Max
constexpr Vector4 Max(const Vector4 &p) const
Definition: vector.h:286
impeller::Color
Definition: color.h:122
impeller::Vector4
Definition: vector.h:229
impeller::Vector4::Vector4
constexpr Vector4(const Point &p)
Definition: vector.h:250
impeller::Vector4::operator==
constexpr bool operator==(const Vector4 &v) const
Definition: vector.h:257
impeller::Vector4::operator!=
constexpr bool operator!=(const Vector4 &v) const
Definition: vector.h:261
impeller::Vector3::Vector3
constexpr Vector3()
Definition: vector.h:27
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:945
impeller::Vector3::Vector3
constexpr Vector3(const Point &p)
Definition: vector.h:31
impeller::Vector3::Vector3
constexpr Vector3(Scalar x, Scalar y, Scalar z)
Definition: vector.h:37
impeller::Vector3::operator-
constexpr Vector3 operator-(const Vector3 &v) const
Definition: vector.h:145
impeller::operator*
constexpr Color operator*(T value, const Color &c)
Definition: color.h:895
impeller::Vector3::operator*=
constexpr Vector3 operator*=(U scale)
Definition: vector.h:117
impeller::Vector3::x
Scalar x
Definition: vector.h:20
impeller::Vector4::Round
constexpr Vector4 Round() const
Definition: vector.h:299
impeller::Vector4::Vector4
constexpr Vector4(const Vector3 &v)
Definition: vector.h:248
impeller::TSize
Definition: size.h:18
impeller::Vector3::operator+
constexpr Vector3 operator+(Scalar s) const
Definition: vector.h:149
impeller::Vector3::Vector3
constexpr Vector3(Scalar x, Scalar y)
Definition: vector.h:35
impeller::Vector3::operator*=
constexpr Vector3 operator*=(const Vector3 &p)
Definition: vector.h:109
impeller::Vector3::Vector3
constexpr Vector3(const Size &s)
Definition: vector.h:33
impeller::Vector4::operator*
constexpr Vector4 operator*(const Vector4 &v) const
Definition: vector.h:277
impeller::Vector4::Lerp
constexpr Vector4 Lerp(const Vector4 &v, Scalar t) const
Definition: vector.h:303
impeller::Vector3::operator/
constexpr Vector3 operator/(U scale) const
Definition: vector.h:171
impeller::Vector4::operator+
constexpr Vector4 operator+(const Vector4 &v) const
Definition: vector.h:265
impeller::Vector4::Min
constexpr Vector4 Min(const Vector4 &p) const
Definition: vector.h:281
impeller::Vector3::operator-
constexpr Vector3 operator-(Scalar s) const
Definition: vector.h:153
impeller::Vector3::Floor
constexpr Vector3 Floor() const
Definition: vector.h:75
impeller::Vector3::Ceil
constexpr Vector3 Ceil() const
Definition: vector.h:79
impeller::Vector3::z
Scalar z
Definition: vector.h:22
impeller::Vector3::Length
constexpr Scalar Length() const
Definition: vector.h:44
impeller::Vector3::e
Scalar e[3]
Definition: vector.h:24
impeller::Vector3::operator-
constexpr Vector3 operator-() const
Definition: vector.h:139
impeller::Vector4::x
Scalar x
Definition: vector.h:232
impeller::Vector3::y
Scalar y
Definition: vector.h:21
impeller::Vector4::Vector4
constexpr Vector4()
Definition: vector.h:240
impeller::operator-
constexpr Color operator-(T value, const Color &c)
Definition: color.h:889
impeller::Vector4::Vector4
constexpr Vector4(Scalar x, Scalar y, Scalar z, Scalar w)
Definition: vector.h:245
scalar.h
impeller::Vector3::Normalize
constexpr Vector3 Normalize() const
Definition: vector.h:46
impeller::Vector3::operator-=
constexpr Vector3 operator-=(const Vector3 &p)
Definition: vector.h:102
impeller::Vector4::w
Scalar w
Definition: vector.h:235
impeller::Vector3::operator+=
constexpr Vector3 operator+=(const Vector3 &p)
Definition: vector.h:95
impeller::Vector3::operator!=
constexpr bool operator!=(const Vector3 &v) const
Definition: vector.h:91
impeller::Vector3::operator/=
constexpr Vector3 operator/=(U scale)
Definition: vector.h:132
impeller::Vector4::ToString
std::string ToString() const
Definition: vector.cc:16
std
Definition: comparable.h:98
impeller::Vector4::y
Scalar y
Definition: vector.h:233
impeller::TPoint< Scalar >
impeller::Vector4::e
Scalar e[4]
Definition: vector.h:237
impeller::Vector3::operator*
constexpr Vector3 operator*(U scale) const
Definition: vector.h:162
impeller::Vector4::Floor
constexpr Vector4 Floor() const
Definition: vector.h:291
impeller::operator/
constexpr Color operator/(T value, const Color &c)
Definition: color.h:900
impeller::Vector3::Min
constexpr Vector3 Min(const Vector3 &p) const
Definition: vector.h:67
color.h
impeller::Vector3::operator/
constexpr Vector3 operator/(const Vector3 &v) const
Definition: vector.h:166
impeller::Vector3::operator/=
constexpr Vector3 operator/=(const Vector3 &p)
Definition: vector.h:124
impeller::Vector4::operator*
constexpr Vector4 operator*(Scalar f) const
Definition: vector.h:273
impeller::Vector4::z
Scalar z
Definition: vector.h:234
impeller
Definition: aiks_context.cc:10
impeller::Vector3::ToString
std::string ToString() const
Definition: vector.cc:10
impeller::Vector4::Vector4
constexpr Vector4(const Color &c)
Definition: vector.h:242
impeller::Vector3
Definition: vector.h:17
size.h
impeller::Vector3::Lerp
constexpr Vector3 Lerp(const Vector3 &v, Scalar t) const
Definition: vector.h:175
impeller::Vector3::Max
constexpr Vector3 Max(const Vector3 &p) const
Definition: vector.h:71
impeller::operator+
constexpr Color operator+(T value, const Color &c)
Definition: color.h:884
impeller::Vector4::Ceil
constexpr Vector4 Ceil() const
Definition: vector.h:295