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,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
131 Vector<
typename std::common_type<T,U>::type, N> out;
133 for (std::size_t i = 0; i < N; ++i) {
134 out.data[i] = lhs + rhs.data[i];
145 template<
typename T,
typename U, std::size_t N>
148 Vector<
typename std::common_type<T,U>::type, N> out;
150 for (std::size_t i = 0; i < N; ++i) {
151 out.data[i] = lhs.data[i] - rhs.data[i];
161 template<
typename T,
typename U, std::size_t N>
164 for (std::size_t i = 0; i < N; ++i) {
165 lhs.data[i] -= rhs.data[i];
176 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
179 Vector<
typename std::common_type<T,U>::type, N> out;
181 for (std::size_t i = 0; i < N; ++i) {
182 out.data[i] = lhs.data[i] - rhs;
192 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
195 Vector<
typename std::common_type<T,U>::type, N> out;
197 for (std::size_t i = 0; i < N; ++i) {
198 out.data[i] = lhs - rhs.data[i];
209 template<
typename T,
typename U, std::size_t N>
212 Vector<
typename std::common_type<T,U>::type, N> out;
214 for (std::size_t i = 0; i < N; ++i) {
215 out.data[i] = lhs.data[i] * rhs.data[i];
225 template<
typename T,
typename U, std::size_t N>
228 for (std::size_t i = 0; i < N; ++i) {
229 lhs.data[i] *= rhs.data[i];
239 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
242 Vector<
typename std::common_type<T,U>::type, N> out;
244 for (std::size_t i = 0; i < N; ++i) {
245 out.data[i] = lhs.data[i] * rhs;
255 template<
typename T,
typename U, std::size_t N>
258 for (std::size_t i = 0; i < N; ++i) {
269 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
272 Vector<
typename std::common_type<T,U>::type, N> out;
274 for (std::size_t i = 0; i < N; ++i) {
275 out.data[i] = lhs * rhs.data[i];
285 template<
typename T,
typename U, std::size_t N>
288 Vector<
typename std::common_type<T,U>::type, N> out;
290 for (std::size_t i = 0; i < N; ++i) {
291 out.data[i] = lhs.data[i] / rhs.data[i];
301 template<
typename T,
typename U, std::size_t N>
304 for (std::size_t i = 0; i < N; ++i) {
305 lhs.data[i] /= rhs.data[i];
315 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
318 Vector<
typename std::common_type<T,U>::type, N> out;
320 for (std::size_t i = 0; i < N; ++i) {
321 out.data[i] = lhs.data[i] / rhs;
331 template<
typename T,
typename U, std::size_t N>
334 for (std::size_t i = 0; i < N; ++i) {
345 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
348 Vector<
typename std::common_type<T,U>::type, N> out;
350 for (std::size_t i = 0; i < N; ++i) {
351 out.data[i] = lhs / rhs.data[i];
361 template<std::size_t N>
366 for (std::size_t i = 0; i < N; ++i) {
367 out.data[i] = lhs.data[i] || rhs.data[i];
377 template<std::size_t N>
382 for (std::size_t i = 0; i < N; ++i) {
383 out.data[i] = lhs.data[i] && rhs.data[i];
401 template<
typename T, std::size_t N>
406 for (std::size_t i = 0; i < N; ++i) {
407 out += lhs.data[i] * rhs.data[i];
417 template<
typename T, std::size_t N>
422 for (std::size_t i = 0; i < N; ++i) {
423 out.data[i] = std::min(lhs.data[i], rhs.data[i]);
433 template<
typename T, std::size_t N>
438 for (std::size_t i = 0; i < N; ++i) {
439 out.data[i] = std::max(lhs.data[i], rhs.data[i]);
449 template<
typename T, std::size_t N>
454 for (std::size_t i = 0; i < N; ++i) {
455 out.data[i] = std::abs(val.data[i]);
466 template<
typename T, std::size_t N>
471 for (std::size_t i = 0; i < N; ++i) {
472 out.data[i] = (lhs.data[i] == rhs.data[i]);
482 template<
typename T, std::size_t N>
487 for (std::size_t i = 0; i < N; ++i) {
488 out.data[i] = (lhs.data[i] < rhs.data[i]);
498 template<
typename T, std::size_t N>
503 for (std::size_t i = 0; i < N; ++i) {
504 out.data[i] = (lhs.data[i] > rhs.data[i]);
514 template<
typename T, std::size_t N>
519 for (std::size_t i = 0; i < N; ++i) {
520 out.data[i] = (cond.data[i] ? lhs.data[i] : rhs.data[i]);
533 template<
typename T, std::size_t N>
538 for (std::size_t i = 0; i < N; ++i) {
539 out.data[i] = clamp(val.data[i], lo.data[i], hi.data[i]);
551 template<
typename T, std::size_t N>
556 for (std::size_t i = 0; i < N; ++i) {
557 out.data[i] = clamp(val.data[i], lo, hi);
567 template<
typename T,
typename U, std::size_t N>
570 return (1 - t) * lhs + t * rhs;
589 template<
typename T, std::size_t N>
594 for (std::size_t i = 0; i < N; ++i) {
595 out += std::abs(vec.data[i]);
615 template<
typename T, std::size_t N>
620 for (std::size_t i = 0; i < N; ++i) {
621 out += square(vec.data[i]);
643 template<
typename T, std::size_t N>
646 return std::sqrt(squareLength(vec));
649 #ifndef DOXYGEN_SHOULD_SKIP_THIS
655 return std::hypot(vec.x, vec.y);
661 return std::hypot(vec.x, vec.y);
681 template<
typename T, std::size_t N>
684 T out = std::abs(vec.data[0]);
686 for (std::size_t i = 1; i < N; ++i) {
687 out = std::max(out, std::abs(vec.data[i]));
710 template<
typename T, std::size_t N>
713 return manhattanLength(vec) + squareLength(vec);
729 template<
typename T, std::size_t N>
732 return manhattanLength(lhs - rhs);
748 template<
typename T, std::size_t N>
751 return squareLength(lhs - rhs);
767 template<
typename T, std::size_t N>
770 return euclideanLength(lhs - rhs);
786 template<
typename T, std::size_t N>
789 return chebyshevLength(lhs - rhs);
805 template<
typename T, std::size_t N>
808 return naturalLength(lhs - rhs);
826 template<
typename T, std::size_t N>
829 T length = euclideanLength(vec);
843 return { std::cos(angle), std::sin(angle) };
856 return std::atan2(vec.y, vec.x);
873 return { -vec.y, vec.x };
892 return dot(a, c) * b - dot(a, b) * c;
912 return - dot(c, b) * a + dot(c, a) * b;
935 return -lhs.y * rhs.x + lhs.x * rhs.y;
951 lhs.y * rhs.z - lhs.z * rhs.y,
952 lhs.z * rhs.x - lhs.x * rhs.z,
953 lhs.x * rhs.y - lhs.y * rhs.x
957 #ifndef DOXYGEN_SHOULD_SKIP_THIS
T manhattanLength(const Vector< T, N > &vec)
Manhattan length of a vector.
Definition: VectorOps.h:591
T dot(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Scalar product.
Definition: VectorOps.h:403
Vector< T, N > abs(const Vector< T, N > &val)
Component-wise absolute value.
Definition: VectorOps.h:451
Vector< typename std::common_type< T, U >::type, N > operator/(T lhs, const Vector< U, N > &rhs)
Left scalar division.
Definition: VectorOps.h:347
Vector< T, N > select(const Vector< bool, N > &cond, const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise selection operator.
Definition: VectorOps.h:516
bool operator==(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Equality operator between two vectors.
Definition: VectorOps.h:42
Vector< typename std::common_type< T, U >::type, N > operator+(T lhs, const Vector< U, N > &rhs)
Left scalar addition.
Definition: VectorOps.h:130
T squareLength(const Vector< T, N > &vec)
Square Euclidean length of a vector.
Definition: VectorOps.h:617
Vector< T, N > & operator/=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise division and assignment.
Definition: VectorOps.h:303
Vector< T, 2 > vectorTripleProduct(Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c)
Regular vector triple product.
Definition: VectorOps.h:891
Vector< T, N > clamp(const Vector< T, N > &val, T lo, T hi)
Component-wise clamp function.
Definition: VectorOps.h:553
T euclideanLength(const Vector< T, N > &vec)
Euclidean length of a vector.
Definition: VectorOps.h:645
Vector< T, N > max(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise maximum.
Definition: VectorOps.h:435
Vector< bool, N > greaterThan(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise comparison operator.
Definition: VectorOps.h:500
Vector< T, N > & operator/=(Vector< T, N > &lhs, U rhs)
Right scalar division and assignment.
Definition: VectorOps.h:333
Vector< bool, N > equals(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise equality operator.
Definition: VectorOps.h:468
Vector< typename std::common_type< T, U >::type, N > operator+(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise addition.
Definition: VectorOps.h:84
Vector< typename std::common_type< T, U >::type, N > operator*(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise multiplication.
Definition: VectorOps.h:211
Vector< typename std::common_type< T, U >::type, N > operator/(const Vector< T, N > &lhs, U rhs)
Right scalar division.
Definition: VectorOps.h:317
Vector< T, N > & operator+=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise addition and assignment.
Definition: VectorOps.h:100
T chebyshevLength(const Vector< T, N > &vec)
Chebyshev length of a vector.
Definition: VectorOps.h:683
Vector< bool, N > operator&&(const Vector< bool, N > &lhs, const Vector< bool, N > &rhs)
Component-wise logical and operator.
Definition: VectorOps.h:379
Vector< bool, N > lessThan(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise comparison operator.
Definition: VectorOps.h:484
float angle(Vector< T, 2 > vec)
Angle of a vector relative to the x-axis.
Definition: VectorOps.h:855
Vector< T, N > operator-(const Vector< T, N > &val)
Component-wise unary minus.
Definition: VectorOps.h:68
Vector< typename std::common_type< T, U >::type, N > operator-(T lhs, const Vector< U, N > &rhs)
Left scalar substraction.
Definition: VectorOps.h:194
Vector< typename std::common_type< T, U >::type, N > lerp(const Vector< T, N > &lhs, const Vector< T, N > &rhs, U t)
Component-wise lerp function.
Definition: VectorOps.h:569
T naturalDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Natural distance between two vectors.
Definition: VectorOps.h:807
T chebyshevDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Chebyshev distance between two vectors.
Definition: VectorOps.h:788
T naturalLength(const Vector< T, N > &vec)
Natural length of a vector.
Definition: VectorOps.h:712
T squareDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Square Euclidean distance between two vectors.
Definition: VectorOps.h:750
Vector< T, 2 > unit(T angle)
Unit vector in a specified direction.
Definition: VectorOps.h:842
Vector< typename std::common_type< T, U >::type, N > operator/(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise division.
Definition: VectorOps.h:287
Vector< T, N > clamp(const Vector< T, N > &val, const Vector< T, N > &lo, const Vector< T, N > &hi)
Component-wise clamp function.
Definition: VectorOps.h:535
Vector< typename std::common_type< T, U >::type, N > operator-(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise substraction.
Definition: VectorOps.h:147
T euclideanDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Euclidean distance between two vectors.
Definition: VectorOps.h:769
Vector< bool, N > operator||(const Vector< bool, N > &lhs, const Vector< bool, N > &rhs)
Component-wise logical or operator.
Definition: VectorOps.h:363
Vector< typename std::common_type< T, U >::type, N > operator-(const Vector< T, N > &lhs, U rhs)
Right scalar substraction.
Definition: VectorOps.h:178
bool operator!=(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Inequality operator between two vectors.
Definition: VectorOps.h:58
Vector< T, N > normalize(const Vector< T, N > &vec)
Normalize a vector.
Definition: VectorOps.h:828
Vector< T, N > min(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise minimum.
Definition: VectorOps.h:419
Vector< T, N > & operator*=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise multiplication and assignment.
Definition: VectorOps.h:227
Vector< typename std::common_type< T, U >::type, N > operator*(const Vector< T, N > &lhs, U rhs)
Right scalar multiplication.
Definition: VectorOps.h:241
Vector< T, N > & operator*=(Vector< T, N > &lhs, U rhs)
Right scalar multiplication and assignment.
Definition: VectorOps.h:257
T manhattanDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Manhattan distance between two vectors.
Definition: VectorOps.h:731
Vector< T, 2 > inverseVectorTripleProduct(Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c)
Inverse vector triple product.
Definition: VectorOps.h:911
General purpose math vector.
Definition: Vector.h:60
constexpr Vector< T, 3 > cross(const Vector< T, 3 > &lhs, const Vector< T, 3 > &rhs)
Cross product for 3D vectors.
Definition: VectorOps.h:949
Vector< typename std::common_type< T, U >::type, N > operator+(const Vector< T, N > &lhs, U rhs)
Right scalar addition.
Definition: VectorOps.h:114
Vector< T, N > & operator-=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise substraction and assignment.
Definition: VectorOps.h:163
Vector< typename std::common_type< T, U >::type, N > operator*(T lhs, const Vector< U, N > &rhs)
Left scalar multiplication.
Definition: VectorOps.h:271
constexpr Vector< T, 2 > perp(Vector< T, 2 > vec)
Perpendicular vector.
Definition: VectorOps.h:872
constexpr T cross(Vector< T, 2 > lhs, Vector< T, 2 > rhs)
Cross product for 2D vectors.
Definition: VectorOps.h:934