21#ifndef GF_VECTOR_OPS_H
22#define GF_VECTOR_OPS_H
33#ifndef DOXYGEN_SHOULD_SKIP_THIS
41 template<
typename T, std::
size_t N>
44 for (std::size_t i = 0; i <
N; ++i) {
45 if (lhs[i] != rhs[i]) {
57 template<
typename T, std::
size_t N>
67 template<
typename T, std::
size_t N>
72 for (std::size_t i = 0; i <
N; ++i) {
83 template<
typename T,
typename U, std::
size_t N>
88 for (std::size_t i = 0; i <
N; ++i) {
89 out[i] = lhs[i] + rhs[i];
99 template<
typename T,
typename U, std::
size_t N>
102 for (std::size_t i = 0; i <
N; ++i) {
113 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value,
U>::type>
118 for (std::size_t i = 0; i <
N; ++i) {
119 out[i] = lhs[i] + rhs;
129 template<
typename T,
typename U, std::
size_t N>
132 for (std::size_t i = 0; i <
N; ++i) {
143 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value,
T>::type>
148 for (std::size_t i = 0; i <
N; ++i) {
149 out[i] = lhs + rhs[i];
160 template<
typename T,
typename U, std::
size_t N>
165 for (std::size_t i = 0; i <
N; ++i) {
166 out[i] = lhs[i] - rhs[i];
176 template<
typename T,
typename U, std::
size_t N>
179 for (std::size_t i = 0; i <
N; ++i) {
191 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value,
U>::type>
196 for (std::size_t i = 0; i <
N; ++i) {
197 out[i] = lhs[i] - rhs;
207 template<
typename T,
typename U, std::
size_t N>
210 for (std::size_t i = 0; i <
N; ++i) {
221 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value,
T>::type>
226 for (std::size_t i = 0; i <
N; ++i) {
227 out[i] = lhs - rhs[i];
238 template<
typename T,
typename U, std::
size_t N>
243 for (std::size_t i = 0; i <
N; ++i) {
244 out[i] = lhs[i] * rhs[i];
254 template<
typename T,
typename U, std::
size_t N>
257 for (std::size_t i = 0; i <
N; ++i) {
268 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value,
U>::type>
273 for (std::size_t i = 0; i <
N; ++i) {
274 out[i] = lhs[i] * rhs;
284 template<
typename T,
typename U, std::
size_t N>
287 for (std::size_t i = 0; i <
N; ++i) {
298 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value,
T>::type>
303 for (std::size_t i = 0; i <
N; ++i) {
304 out[i] = lhs * rhs[i];
314 template<
typename T,
typename U, std::
size_t N>
319 for (std::size_t i = 0; i <
N; ++i) {
320 out[i] = lhs[i] / rhs[i];
330 template<
typename T,
typename U, std::
size_t N>
333 for (std::size_t i = 0; i <
N; ++i) {
344 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value,
U>::type>
349 for (std::size_t i = 0; i <
N; ++i) {
350 out[i] = lhs[i] / rhs;
360 template<
typename T,
typename U, std::
size_t N>
363 for (std::size_t i = 0; i <
N; ++i) {
374 template<typename T, typename U, std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value,
T>::type>
379 for (std::size_t i = 0; i <
N; ++i) {
380 out[i] = lhs / rhs[i];
390 template<std::
size_t N>
395 for (std::size_t i = 0; i <
N; ++i) {
396 out[i] = lhs[i] || rhs[i];
406 template<std::
size_t N>
411 for (std::size_t i = 0; i <
N; ++i) {
412 out[i] = lhs[i] && rhs[i];
430 template<
typename T, std::
size_t N>
435 for (std::size_t i = 0; i <
N; ++i) {
436 out += lhs[i] * rhs[i];
446 template<
typename T, std::
size_t N>
451 for (std::size_t i = 0; i <
N; ++i) {
452 out[i] = std::min(lhs[i], rhs[i]);
462 template<
typename T, std::
size_t N>
467 for (std::size_t i = 0; i <
N; ++i) {
468 out[i] = std::max(lhs[i], rhs[i]);
478 template<
typename T, std::
size_t N>
483 for (std::size_t i = 0; i <
N; ++i) {
484 out[i] = std::abs(val[i]);
494 template<
typename T, std::
size_t N>
499 for (std::size_t i = 0; i <
N; ++i) {
510 template<
typename T, std::
size_t N>
515 for (std::size_t i = 0; i <
N; ++i) {
516 out[i] = (lhs[i] == rhs[i]);
526 template<
typename T, std::
size_t N>
531 for (std::size_t i = 0; i <
N; ++i) {
532 out[i] = (lhs[i] < rhs[i]);
542 template<
typename T, std::
size_t N>
547 for (std::size_t i = 0; i <
N; ++i) {
548 out[i] = (lhs[i] > rhs[i]);
558 template<
typename T, std::
size_t N>
563 for (std::size_t i = 0; i <
N; ++i) {
564 out[i] = (cond[i] ? lhs[i] : rhs[i]);
577 template<
typename T, std::
size_t N>
582 for (std::size_t i = 0; i <
N; ++i) {
583 out[i] =
clamp(val[i], lo[i], hi[i]);
595 template<
typename T, std::
size_t N>
600 for (std::size_t i = 0; i <
N; ++i) {
601 out[i] =
clamp(val[i], lo, hi);
611 template<
typename T,
typename U, std::
size_t N>
616 for (std::size_t i = 0; i <
N; ++i) {
617 out[i] =
lerp(lhs[i], rhs[i], t);
640 template<
typename T, std::
size_t N>
645 for (std::size_t i = 0; i <
N; ++i) {
646 out += std::abs(vec[i]);
666 template<
typename T, std::
size_t N>
671 for (std::size_t i = 0; i <
N; ++i) {
694 template<
typename T, std::
size_t N>
697 return std::sqrt(squareLength(vec));
700#ifndef DOXYGEN_SHOULD_SKIP_THIS
706 return std::hypot(vec.x, vec.y);
712 return std::hypot(vec.x, vec.y);
720 return std::hypot(vec.x, vec.y, vec.z);
726 return std::hypot(vec.x, vec.y, vec.z);
747 template<
typename T, std::
size_t N>
750 T out = std::abs(vec[0]);
752 for (std::size_t i = 1; i <
N; ++i) {
753 out = std::max(out, std::abs(vec[i]));
776 template<
typename T, std::
size_t N>
779 return manhattanLength(vec) + squareLength(vec);
795 template<
typename T, std::
size_t N>
798 return manhattanLength(lhs - rhs);
814 template<
typename T, std::
size_t N>
817 return squareLength(lhs - rhs);
833 template<
typename T, std::
size_t N>
836 return euclideanLength(lhs - rhs);
852 template<
typename T, std::
size_t N>
855 return chebyshevLength(lhs - rhs);
871 template<
typename T, std::
size_t N>
874 return naturalLength(lhs - rhs);
892 template<
typename T, std::
size_t N>
895 T length = euclideanLength(vec);
909 return { vec.
x,
T(0) };
922 return {
T(0), vec.
y };
935 return { length,
T(0) };
948 return {
T(0), length };
974 return std::atan2(vec.
y, vec.
x);
991 return { -vec.
y, vec.
x };
1007 template<
typename T>
1010 return dot(a, c) * b - dot(a, b) * c;
1027 template<
typename T>
1030 return - dot(c, b) * a + dot(c, a) * b;
1050 template<
typename T>
1053 return lhs.
x * rhs.
y - lhs.
y * rhs.
x;
1065 template<
typename T>
1069 lhs.
y * rhs.
z - lhs.
z * rhs.
y,
1070 lhs.
z * rhs.
x - lhs.
x * rhs.
z,
1071 lhs.
x * rhs.
y - lhs.
y * rhs.
x
1083 template<
typename T>
1086 return std::atan2(cross(lhs, rhs), dot(lhs, rhs));
1097 template<
typename T>
1100 return dot(vec, axis) / squareLength(axis) * axis;
1112 template<
typename Archive,
typename T, std::
size_t N>
1115 for (std::size_t i = 0; i <
N; ++i) {
1122#ifndef DOXYGEN_SHOULD_SKIP_THIS
GF_CORE_API float angle(Direction direction)
Get an angle from a direction.
constexpr T square(T val)
Square function.
Definition: Math.h:299
constexpr int sign(T val)
Sign function.
Definition: Math.h:334
constexpr T clamp(T val, T lo, T hi)
Clamping function.
Definition: Math.h:284
constexpr T lerp(T lhs, T rhs, U t)
Linear interpolation function.
Definition: Math.h:266
The namespace for gf classes.
template struct GF_CORE_API Vector< double, 2 >
template struct GF_CORE_API Vector< float, 3 >
template struct GF_CORE_API Vector< double, 3 >
A 2D vector.
Definition: Vector.h:316
T y
Second coordinate in the (x,y) representation.
Definition: Vector.h:525
T x
First coordinate in the (x,y) representation.
Definition: Vector.h:514
A 3D vector.
Definition: Vector.h:570
T x
First coordinate in the (x,y,z) representation.
Definition: Vector.h:794
T z
Third coordinate in the (x,y,z) representation.
Definition: Vector.h:810
T y
Second coordinate in the (x,y,z) representation.
Definition: Vector.h:802
General purpose math vector.
Definition: Vector.h:61
constexpr Vector< T, 2 > projx(Vector< T, 2 > vec)
Project the vector on the x axis.
Definition: VectorOps.h:908
Archive & operator|(Archive &ar, Vector< T, N > &vec)
Serialize and deserialize a vector.
Definition: VectorOps.h:1114
constexpr Vector< std::common_type_t< T, U >, N > operator+(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise addition.
Definition: VectorOps.h:85
constexpr Vector< T, N > max(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise maximum.
Definition: VectorOps.h:464
float angleTo(Vector< T, 2 > lhs, Vector< T, 2 > rhs)
Angle between two vectors.
Definition: VectorOps.h:1085
constexpr Vector< T, N > & operator+=(Vector< T, N > &lhs, U rhs)
Right scalar addition and assignment.
Definition: VectorOps.h:131
T manhattanLength(Vector< T, N > vec)
Manhattan length of a vector.
Definition: VectorOps.h:642
float angle(Vector< T, 2 > vec)
Angle of a vector relative to the x-axis.
Definition: VectorOps.h:973
constexpr Vector< T, 2 > perp(Vector< T, 2 > vec)
Perpendicular vector.
Definition: VectorOps.h:990
Vector< T, 2 > unit(T angle)
Unit vector in a specified direction.
Definition: VectorOps.h:960
T euclideanDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Euclidean distance between two vectors.
Definition: VectorOps.h:835
constexpr T squareDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Square Euclidean distance between two vectors.
Definition: VectorOps.h:816
constexpr Vector< std::common_type_t< T, U >, N > operator*(Vector< T, N > lhs, U rhs)
Right scalar multiplication.
Definition: VectorOps.h:270
constexpr Vector< T, N > select(Vector< bool, N > cond, Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise selection operator.
Definition: VectorOps.h:560
constexpr bool operator!=(Vector< T, N > lhs, Vector< T, N > rhs)
Inequality operator between two vectors.
Definition: VectorOps.h:59
constexpr bool operator==(Vector< T, N > lhs, Vector< T, N > rhs)
Equality operator between two vectors.
Definition: VectorOps.h:43
constexpr Vector< T, N > & operator-=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise substraction and assignment.
Definition: VectorOps.h:178
constexpr Vector< std::common_type_t< T, U >, N > operator/(Vector< T, N > lhs, U rhs)
Right scalar division.
Definition: VectorOps.h:346
constexpr Vector< T, N > min(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise minimum.
Definition: VectorOps.h:448
constexpr Vector< T, N > operator-(Vector< T, N > val)
Component-wise unary minus.
Definition: VectorOps.h:69
constexpr Vector< T, N > clamp(Vector< T, N > val, T lo, T hi)
Component-wise clamp function.
Definition: VectorOps.h:597
constexpr Vector< std::common_type_t< T, U >, N > operator*(T lhs, Vector< U, N > rhs)
Left scalar multiplication.
Definition: VectorOps.h:300
constexpr Vector< std::common_type_t< T, U >, N > operator*(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise multiplication.
Definition: VectorOps.h:240
constexpr Vector< std::common_type_t< T, U >, N > operator/(T lhs, Vector< U, N > rhs)
Left scalar division.
Definition: VectorOps.h:376
constexpr Vector< T, N > & operator*=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise multiplication and assignment.
Definition: VectorOps.h:256
Vector< T, N > normalize(Vector< T, N > vec)
Normalize a vector.
Definition: VectorOps.h:894
constexpr Vector< T, N > & operator-=(Vector< T, N > &lhs, U rhs)
Right scalar substraction and assignment.
Definition: VectorOps.h:209
constexpr Vector< T, 2 > projy(Vector< T, 2 > vec)
Project the vector on the y axis.
Definition: VectorOps.h:921
constexpr Vector< std::common_type_t< T, U >, N > operator-(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise substraction.
Definition: VectorOps.h:162
T euclideanLength(Vector< T, N > vec)
Euclidean length of a vector.
Definition: VectorOps.h:696
constexpr Vector< bool, N > equals(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise equality operator.
Definition: VectorOps.h:512
constexpr Vector< T, 2 > projectOnto(Vector< T, 2 > vec, Vector< T, 2 > axis)
Projection of a vector onto an axis.
Definition: VectorOps.h:1099
constexpr Vector< T, 2 > diry(T length)
Create a vector in the y direction.
Definition: VectorOps.h:947
Vector< T, N > abs(Vector< T, N > val)
Component-wise absolute value.
Definition: VectorOps.h:480
constexpr T dot(Vector< T, N > lhs, Vector< T, N > rhs)
Scalar product.
Definition: VectorOps.h:432
constexpr Vector< std::common_type_t< T, U >, N > operator-(T lhs, Vector< U, N > rhs)
Left scalar substraction.
Definition: VectorOps.h:223
constexpr Vector< T, N > & operator*=(Vector< T, N > &lhs, U rhs)
Right scalar multiplication and assignment.
Definition: VectorOps.h:286
constexpr Vector< T, N > & operator+=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise addition and assignment.
Definition: VectorOps.h:101
T chebyshevDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Chebyshev distance between two vectors.
Definition: VectorOps.h:854
constexpr Vector< bool, N > operator||(Vector< bool, N > lhs, Vector< bool, N > rhs)
Component-wise logical or operator.
Definition: VectorOps.h:392
constexpr Vector< bool, N > lessThan(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise comparison operator.
Definition: VectorOps.h:528
constexpr Vector< T, 2 > vectorTripleProduct(Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c)
Regular vector triple product.
Definition: VectorOps.h:1009
T naturalLength(Vector< T, N > vec)
Natural length of a vector.
Definition: VectorOps.h:778
T naturalDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Natural distance between two vectors.
Definition: VectorOps.h:873
constexpr Vector< T, N > lerp(Vector< T, N > lhs, Vector< T, N > rhs, U t)
Component-wise lerp function.
Definition: VectorOps.h:613
constexpr Vector< T, N > clamp(Vector< T, N > val, Vector< T, N > lo, Vector< T, N > hi)
Component-wise clamp function.
Definition: VectorOps.h:579
T chebyshevLength(Vector< T, N > vec)
Chebyshev length of a vector.
Definition: VectorOps.h:749
constexpr Vector< int, N > sign(Vector< T, N > val)
Component-wise sign value.
Definition: VectorOps.h:496
T manhattanDistance(Vector< T, N > lhs, Vector< T, N > rhs)
Manhattan distance between two vectors.
Definition: VectorOps.h:797
constexpr Vector< std::common_type_t< T, U >, N > operator/(Vector< T, N > lhs, Vector< U, N > rhs)
Component-wise division.
Definition: VectorOps.h:316
constexpr Vector< T, N > & operator/=(Vector< T, N > &lhs, U rhs)
Right scalar division and assignment.
Definition: VectorOps.h:362
constexpr Vector< bool, N > operator&&(Vector< bool, N > lhs, Vector< bool, N > rhs)
Component-wise logical and operator.
Definition: VectorOps.h:408
constexpr T squareLength(Vector< T, N > vec)
Square Euclidean length of a vector.
Definition: VectorOps.h:668
constexpr Vector< T, 2 > dirx(T length)
Create a vector in the x direction.
Definition: VectorOps.h:934
constexpr Vector< std::common_type_t< T, U >, N > operator-(Vector< T, N > lhs, U rhs)
Right scalar substraction.
Definition: VectorOps.h:193
constexpr Vector< bool, N > greaterThan(Vector< T, N > lhs, Vector< T, N > rhs)
Component-wise comparison operator.
Definition: VectorOps.h:544
constexpr T cross(Vector< T, 2 > lhs, Vector< T, 2 > rhs)
Cross product for 2D vectors.
Definition: VectorOps.h:1052
constexpr Vector< T, 3 > cross(Vector< T, 3 > lhs, Vector< T, 3 > rhs)
Cross product for 3D vectors.
Definition: VectorOps.h:1067
constexpr Vector< T, N > & operator/=(Vector< T, N > &lhs, Vector< U, N > rhs)
Component-wise division and assignment.
Definition: VectorOps.h:332
constexpr Vector< T, 2 > inverseVectorTripleProduct(Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c)
Inverse vector triple product.
Definition: VectorOps.h:1029
constexpr Vector< std::common_type_t< T, U >, N > operator+(T lhs, Vector< U, N > rhs)
Left scalar addition.
Definition: VectorOps.h:145
constexpr Vector< std::common_type_t< T, U >, N > operator+(Vector< T, N > lhs, U rhs)
Right scalar addition.
Definition: VectorOps.h:115