21 #ifndef GF_VECTOR_OPS_H
22 #define GF_VECTOR_OPS_H
26 #include <type_traits>
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 template<
typename T, std::size_t N>
43 for (std::size_t i = 0; i < N; ++i) {
44 if (lhs.data[i] != rhs.data[i]) {
56 template<
typename T, std::size_t N>
66 template<
typename T, std::size_t N>
71 for (std::size_t i = 0; i < N; ++i) {
72 out.data[i] = - val.data[i];
82 template<
typename T,
typename U, std::size_t N>
85 Vector<
typename std::common_type<T,U>::type, N> out;
87 for (std::size_t i = 0; i < N; ++i) {
88 out.data[i] = lhs.data[i] + rhs.data[i];
98 template<
typename T,
typename U, std::size_t N>
101 for (std::size_t i = 0; i < N; ++i) {
102 lhs.data[i] += rhs.data[i];
112 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
115 Vector<
typename std::common_type<T,U>::type, N> out;
117 for (std::size_t i = 0; i < N; ++i) {
118 out.data[i] = lhs.data[i] + rhs;
128 template<
typename T,
typename U, std::size_t N>
131 for (std::size_t i = 0; i < N; ++i) {
142 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
145 Vector<
typename std::common_type<T,U>::type, N> out;
147 for (std::size_t i = 0; i < N; ++i) {
148 out.data[i] = lhs + rhs.data[i];
159 template<
typename T,
typename U, std::size_t N>
162 Vector<
typename std::common_type<T,U>::type, N> out;
164 for (std::size_t i = 0; i < N; ++i) {
165 out.data[i] = lhs.data[i] - rhs.data[i];
175 template<
typename T,
typename U, std::size_t N>
178 for (std::size_t i = 0; i < N; ++i) {
179 lhs.data[i] -= rhs.data[i];
190 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
193 Vector<
typename std::common_type<T,U>::type, N> out;
195 for (std::size_t i = 0; i < N; ++i) {
196 out.data[i] = lhs.data[i] - rhs;
206 template<
typename T,
typename U, std::size_t N>
209 for (std::size_t i = 0; i < N; ++i) {
220 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
223 Vector<
typename std::common_type<T,U>::type, N> out;
225 for (std::size_t i = 0; i < N; ++i) {
226 out.data[i] = lhs - rhs.data[i];
237 template<
typename T,
typename U, std::size_t N>
240 Vector<
typename std::common_type<T,U>::type, N> out;
242 for (std::size_t i = 0; i < N; ++i) {
243 out.data[i] = lhs.data[i] * rhs.data[i];
253 template<
typename T,
typename U, std::size_t N>
256 for (std::size_t i = 0; i < N; ++i) {
257 lhs.data[i] *= rhs.data[i];
267 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
270 Vector<
typename std::common_type<T,U>::type, N> out;
272 for (std::size_t i = 0; i < N; ++i) {
273 out.data[i] = lhs.data[i] * rhs;
283 template<
typename T,
typename U, std::size_t N>
286 for (std::size_t i = 0; i < N; ++i) {
297 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
300 Vector<
typename std::common_type<T,U>::type, N> out;
302 for (std::size_t i = 0; i < N; ++i) {
303 out.data[i] = lhs * rhs.data[i];
313 template<
typename T,
typename U, std::size_t N>
316 Vector<
typename std::common_type<T,U>::type, N> out;
318 for (std::size_t i = 0; i < N; ++i) {
319 out.data[i] = lhs.data[i] / rhs.data[i];
329 template<
typename T,
typename U, std::size_t N>
332 for (std::size_t i = 0; i < N; ++i) {
333 lhs.data[i] /= rhs.data[i];
343 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
346 Vector<
typename std::common_type<T,U>::type, N> out;
348 for (std::size_t i = 0; i < N; ++i) {
349 out.data[i] = lhs.data[i] / rhs;
359 template<
typename T,
typename U, std::size_t N>
362 for (std::size_t i = 0; i < N; ++i) {
373 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
376 Vector<
typename std::common_type<T,U>::type, N> out;
378 for (std::size_t i = 0; i < N; ++i) {
379 out.data[i] = lhs / rhs.data[i];
389 template<std::size_t N>
394 for (std::size_t i = 0; i < N; ++i) {
395 out.data[i] = lhs.data[i] || rhs.data[i];
405 template<std::size_t N>
410 for (std::size_t i = 0; i < N; ++i) {
411 out.data[i] = lhs.data[i] && rhs.data[i];
429 template<
typename T, std::size_t N>
434 for (std::size_t i = 0; i < N; ++i) {
435 out += lhs.data[i] * rhs.data[i];
445 template<
typename T, std::size_t N>
450 for (std::size_t i = 0; i < N; ++i) {
451 out.data[i] = std::min(lhs.data[i], rhs.data[i]);
461 template<
typename T, std::size_t N>
466 for (std::size_t i = 0; i < N; ++i) {
467 out.data[i] = std::max(lhs.data[i], rhs.data[i]);
477 template<
typename T, std::size_t N>
482 for (std::size_t i = 0; i < N; ++i) {
483 out.data[i] = std::abs(val.data[i]);
494 template<
typename T, std::size_t N>
499 for (std::size_t i = 0; i < N; ++i) {
500 out.data[i] = (lhs.data[i] == rhs.data[i]);
510 template<
typename T, std::size_t N>
515 for (std::size_t i = 0; i < N; ++i) {
516 out.data[i] = (lhs.data[i] < rhs.data[i]);
526 template<
typename T, std::size_t N>
531 for (std::size_t i = 0; i < N; ++i) {
532 out.data[i] = (lhs.data[i] > rhs.data[i]);
542 template<
typename T, std::size_t N>
547 for (std::size_t i = 0; i < N; ++i) {
548 out.data[i] = (cond.data[i] ? lhs.data[i] : rhs.data[i]);
561 template<
typename T, std::size_t N>
566 for (std::size_t i = 0; i < N; ++i) {
567 out.data[i] = clamp(val.data[i], lo.data[i], hi.data[i]);
579 template<
typename T, std::size_t N>
584 for (std::size_t i = 0; i < N; ++i) {
585 out.data[i] = clamp(val.data[i], lo, hi);
595 template<
typename T,
typename U, std::size_t N>
600 for (std::size_t i = 0; i < N; ++i) {
601 out.data[i] = lerp(lhs.data[i], rhs.data[i], t);
624 template<
typename T, std::size_t N>
629 for (std::size_t i = 0; i < N; ++i) {
630 out += std::abs(vec.data[i]);
650 template<
typename T, std::size_t N>
655 for (std::size_t i = 0; i < N; ++i) {
656 out += square(vec.data[i]);
678 template<
typename T, std::size_t N>
681 return std::sqrt(squareLength(vec));
684 #ifndef DOXYGEN_SHOULD_SKIP_THIS
690 return std::hypot(vec.x, vec.y);
696 return std::hypot(vec.x, vec.y);
716 template<
typename T, std::size_t N>
719 T out = std::abs(vec.data[0]);
721 for (std::size_t i = 1; i < N; ++i) {
722 out = std::max(out, std::abs(vec.data[i]));
745 template<
typename T, std::size_t N>
748 return manhattanLength(vec) + squareLength(vec);
764 template<
typename T, std::size_t N>
767 return manhattanLength(lhs - rhs);
783 template<
typename T, std::size_t N>
786 return squareLength(lhs - rhs);
802 template<
typename T, std::size_t N>
805 return euclideanLength(lhs - rhs);
821 template<
typename T, std::size_t N>
824 return chebyshevLength(lhs - rhs);
840 template<
typename T, std::size_t N>
843 return naturalLength(lhs - rhs);
861 template<
typename T, std::size_t N>
864 T length = euclideanLength(vec);
878 return { std::cos(angle), std::sin(angle) };
891 return std::atan2(vec.y, vec.x);
908 return { -vec.y, vec.x };
927 return dot(a, c) * b - dot(a, b) * c;
947 return - dot(c, b) * a + dot(c, a) * b;
970 return lhs.x * rhs.y - lhs.y * rhs.x;
986 lhs.y * rhs.z - lhs.z * rhs.y,
987 lhs.z * rhs.x - lhs.x * rhs.z,
988 lhs.x * rhs.y - lhs.y * rhs.x
992 #ifndef DOXYGEN_SHOULD_SKIP_THIS
Vector< typename std::common_type< T, U >::type, N > operator/(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise division.
Definition: VectorOps.h:315
Vector< T, N > & operator+=(Vector< T, N > &lhs, U rhs)
Right scalar addition and assignment.
Definition: VectorOps.h:130
Vector< typename std::common_type< T, U >::type, N > operator+(Vector< T, N > lhs, U rhs)
Right scalar addition.
Definition: VectorOps.h:114
T euclideanLength(Vector< T, N > vec)
Euclidean length of a vector.
Definition: VectorOps.h:680
Vector< typename std::common_type< T, U >::type, N > operator*(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise multiplication.
Definition: VectorOps.h:239
Vector< typename std::common_type< T, U >::type, N > operator/(T lhs, Vector< U, N > rhs)
Left scalar division.
Definition: VectorOps.h:375
Vector< T, N > clamp(Vector< T, N > val, Vector< T, N > lo, Vector< T, N > hi)
Component-wise clamp function.
Definition: VectorOps.h:563
Vector< bool, N > greaterThan(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise comparison operator.
Definition: VectorOps.h:528
constexpr Vector< T, 3 > cross(Vector< T, 3 > lhs, Vector< T, 3 > rhs)
Cross product for 3D vectors.
Definition: VectorOps.h:984
Vector< T, 2 > vectorTripleProduct(Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c)
Regular vector triple product.
Definition: VectorOps.h:926
Vector< T, N > & operator/=(Vector< T, N > &lhs, U rhs)
Right scalar division and assignment.
Definition: VectorOps.h:361
Vector< typename std::common_type< T, U >::type, N > operator-(Vector< T, N > lhs, U rhs)
Right scalar substraction.
Definition: VectorOps.h:192
Vector< typename std::common_type< T, U >::type, N > operator+(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise addition.
Definition: VectorOps.h:84
Vector< bool, N > equals(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise equality operator.
Definition: VectorOps.h:496
Vector< T, N > normalize(Vector< T, N > vec)
Normalize a vector.
Definition: VectorOps.h:863
Vector< T, N > & operator+=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise addition and assignment.
Definition: VectorOps.h:100
float angle(Vector< T, 2 > vec)
Angle of a vector relative to the x-axis.
Definition: VectorOps.h:890
T squareDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Square Euclidean distance between two vectors.
Definition: VectorOps.h:785
Vector< bool, N > lessThan(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise comparison operator.
Definition: VectorOps.h:512
Vector< T, N > select(Vector< bool, N > cond, Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise selection operator.
Definition: VectorOps.h:544
Vector< T, N > min(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise minimum.
Definition: VectorOps.h:447
Vector< typename std::common_type< T, U >::type, N > operator*(Vector< T, N > lhs, U rhs)
Right scalar multiplication.
Definition: VectorOps.h:269
Vector< typename std::common_type< T, U >::type, N > operator+(T lhs, Vector< U, N > rhs)
Left scalar addition.
Definition: VectorOps.h:144
Vector< T, N > & operator-=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise substraction and assignment.
Definition: VectorOps.h:177
The namespace for gf classes.
Definition: Action.h:34
Vector< T, N > clamp(Vector< T, N > val, T lo, T hi)
Component-wise clamp function.
Definition: VectorOps.h:581
Vector< T, 2 > unit(T angle)
Unit vector in a specified direction.
Definition: VectorOps.h:877
bool operator==(Vector< T, N > lhs, Vector< T, N > rhs)
Equality operator between two vectors.
Definition: VectorOps.h:42
Vector< bool, N > operator||(Vector< bool, N > lhs, Vector< bool, N > rhs)
Component-wise logical or operator.
Definition: VectorOps.h:391
Vector< typename std::common_type< T, U >::type, N > operator*(T lhs, Vector< U, N > rhs)
Left scalar multiplication.
Definition: VectorOps.h:299
Vector< T, N > & operator-=(Vector< T, N > &lhs, U rhs)
Right scalar substraction and assignment.
Definition: VectorOps.h:208
Vector< T, N > operator-(Vector< T, N > val)
Component-wise unary minus.
Definition: VectorOps.h:68
Vector< bool, N > operator&&(Vector< bool, N > lhs, Vector< bool, N > rhs)
Component-wise logical and operator.
Definition: VectorOps.h:407
T manhattanDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Manhattan distance between two vectors.
Definition: VectorOps.h:766
Vector< typename std::common_type< T, U >::type, N > operator/(Vector< T, N > lhs, U rhs)
Right scalar division.
Definition: VectorOps.h:345
T chebyshevLength(Vector< T, N > vec)
Chebyshev length of a vector.
Definition: VectorOps.h:718
T euclideanDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Euclidean distance between two vectors.
Definition: VectorOps.h:804
Vector< T, N > & operator*=(Vector< T, N > &lhs, U rhs)
Right scalar multiplication and assignment.
Definition: VectorOps.h:285
Vector< T, N > lerp(Vector< T, N > lhs, Vector< T, N > rhs, U t)
Component-wise lerp function.
Definition: VectorOps.h:597
T manhattanLength(Vector< T, N > vec)
Manhattan length of a vector.
Definition: VectorOps.h:626
Vector< T, N > max(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise maximum.
Definition: VectorOps.h:463
Vector< T, N > & operator/=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise division and assignment.
Definition: VectorOps.h:331
Vector< T, 2 > inverseVectorTripleProduct(Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c)
Inverse vector triple product.
Definition: VectorOps.h:946
Vector< typename std::common_type< T, U >::type, N > operator-(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise substraction.
Definition: VectorOps.h:161
Vector< T, N > abs(Vector< T, N > val)
Component-wise absolute value.
Definition: VectorOps.h:479
General purpose math vector.
Definition: Vector.h:60
Vector< typename std::common_type< T, U >::type, N > operator-(T lhs, Vector< U, N > rhs)
Left scalar substraction.
Definition: VectorOps.h:222
constexpr Vector< T, 2 > perp(Vector< T, 2 > vec)
Perpendicular vector.
Definition: VectorOps.h:907
Vector< T, N > & operator*=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise multiplication and assignment.
Definition: VectorOps.h:255
T naturalDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Natural distance between two vectors.
Definition: VectorOps.h:842
T naturalLength(Vector< T, N > vec)
Natural length of a vector.
Definition: VectorOps.h:747
T squareLength(Vector< T, N > vec)
Square Euclidean length of a vector.
Definition: VectorOps.h:652
T dot(Vector< T, N > lhs, Vector< T, N > rhs)
Scalar product.
Definition: VectorOps.h:431
bool operator!=(Vector< T, N > lhs, Vector< T, N > rhs)
Inequality operator between two vectors.
Definition: VectorOps.h:58
constexpr T cross(Vector< T, 2 > lhs, Vector< T, 2 > rhs)
Cross product for 2D vectors.
Definition: VectorOps.h:969
T chebyshevDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Chebyshev distance between two vectors.
Definition: VectorOps.h:823