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