28 #include <initializer_list>
29 #include <type_traits>
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
60 template<
typename T, std::size_t N>
62 #ifndef DOXYGEN_SHOULD_SKIP_THIS
63 static_assert(
N > 0,
"N must be strictly positive");
89 std::fill_n(
data, N, val);
107 std::copy_n(array, N,
data);
125 std::copy_n(list.begin(), std::min(list.size(), N),
data);
148 std::transform(
data,
data + N, other.data, [](U val) {
return static_cast<T>(val); });
227 T
const *
end(
void)
const {
304 template <
typename T>
328 std::fill_n(data, 2, val);
346 std::copy_n(array, 2, data);
453 T
const *
end(
void)
const {
542 template <
typename T>
566 std::fill_n(data, 3, val);
584 std::copy_n(array, 3, data);
607 : data{ xy.x, xy.y, z }
705 T
const *
end(
void)
const {
785 template <
typename T>
809 std::fill_n(data, 4, val);
827 std::copy_n(array, 4, data);
938 T
const *
end(
void)
const {
1119 extern template struct Vector<
float, 2>;
1120 extern template struct Vector<
float, 3>;
1121 extern template struct Vector<
float, 4>;
1123 extern template struct Vector<
double, 2>;
1124 extern template struct Vector<
double, 3>;
1125 extern template struct Vector<
double, 4>;
1127 extern template struct Vector<
int, 2>;
1128 extern template struct Vector<
int, 3>;
1129 extern template struct Vector<
int, 4>;
1131 extern template struct Vector<
unsigned, 2>;
1132 extern template struct Vector<
unsigned, 3>;
1133 extern template struct Vector<
unsigned, 4>;
1135 extern template struct Vector<
bool, 2>;
1136 extern template struct Vector<
bool, 3>;
1137 extern template struct Vector<
bool, 4>;
1139 extern template struct Vector<uint8_t, 3>;
1140 extern template struct Vector<uint8_t, 4>;
1147 template<
typename T, std::size_t N>
1150 for (std::size_t i = 0; i < N; ++i) {
1151 if (lhs.data[i] != rhs.data[i]) {
1163 template<
typename T, std::size_t N>
1166 return !(lhs == rhs);
1173 template<
typename T, std::size_t N>
1178 for (std::size_t i = 0; i < N; ++i) {
1179 out.data[i] = - val.data[i];
1189 template<
typename T,
typename U, std::size_t N>
1192 Vector<
typename std::common_type<T,U>::type, N> out;
1194 for (std::size_t i = 0; i < N; ++i) {
1195 out.data[i] = lhs.data[i] + rhs.data[i];
1205 template<
typename T,
typename U, std::size_t N>
1208 for (std::size_t i = 0; i < N; ++i) {
1209 lhs.data[i] += rhs.data[i];
1219 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
1222 Vector<
typename std::common_type<T,U>::type, N> out;
1224 for (std::size_t i = 0; i < N; ++i) {
1225 out.data[i] = lhs.data[i] + rhs;
1235 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
1238 Vector<
typename std::common_type<T,U>::type, N> out;
1240 for (std::size_t i = 0; i < N; ++i) {
1241 out.data[i] = lhs + rhs.data[i];
1252 template<
typename T,
typename U, std::size_t N>
1255 Vector<
typename std::common_type<T,U>::type, N> out;
1257 for (std::size_t i = 0; i < N; ++i) {
1258 out.data[i] = lhs.data[i] - rhs.data[i];
1268 template<
typename T,
typename U, std::size_t N>
1271 for (std::size_t i = 0; i < N; ++i) {
1272 lhs.data[i] -= rhs.data[i];
1283 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
1286 Vector<
typename std::common_type<T,U>::type, N> out;
1288 for (std::size_t i = 0; i < N; ++i) {
1289 out.data[i] = lhs.data[i] - rhs;
1299 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
1302 Vector<
typename std::common_type<T,U>::type, N> out;
1304 for (std::size_t i = 0; i < N; ++i) {
1305 out.data[i] = lhs - rhs.data[i];
1316 template<
typename T,
typename U, std::size_t N>
1319 Vector<
typename std::common_type<T,U>::type, N> out;
1321 for (std::size_t i = 0; i < N; ++i) {
1322 out.data[i] = lhs.data[i] * rhs.data[i];
1332 template<
typename T,
typename U, std::size_t N>
1335 for (std::size_t i = 0; i < N; ++i) {
1336 lhs.data[i] *= rhs.data[i];
1346 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
1349 Vector<
typename std::common_type<T,U>::type, N> out;
1351 for (std::size_t i = 0; i < N; ++i) {
1352 out.data[i] = lhs.data[i] * rhs;
1362 template<
typename T,
typename U, std::size_t N>
1365 for (std::size_t i = 0; i < N; ++i) {
1376 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
1379 Vector<
typename std::common_type<T,U>::type, N> out;
1381 for (std::size_t i = 0; i < N; ++i) {
1382 out.data[i] = lhs * rhs.data[i];
1392 template<
typename T,
typename U, std::size_t N>
1395 Vector<
typename std::common_type<T,U>::type, N> out;
1397 for (std::size_t i = 0; i < N; ++i) {
1398 out.data[i] = lhs.data[i] / rhs.data[i];
1408 template<
typename T,
typename U, std::size_t N>
1411 for (std::size_t i = 0; i < N; ++i) {
1412 lhs.data[i] /= rhs.data[i];
1422 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<U>::value, U>::type>
1425 Vector<
typename std::common_type<T,U>::type, N> out;
1427 for (std::size_t i = 0; i < N; ++i) {
1428 out.data[i] = lhs.data[i] / rhs;
1438 template<
typename T,
typename U, std::size_t N>
1441 for (std::size_t i = 0; i < N; ++i) {
1452 template<
typename T,
typename U, std::size_t N,
typename E =
typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
1455 Vector<
typename std::common_type<T,U>::type, N> out;
1457 for (std::size_t i = 0; i < N; ++i) {
1458 out.data[i] = lhs / rhs.data[i];
1468 template<std::size_t N>
1473 for (std::size_t i = 0; i < N; ++i) {
1474 out.data[i] = lhs.data[i] || rhs.data[i];
1484 template<std::size_t N>
1489 for (std::size_t i = 0; i < N; ++i) {
1490 out.data[i] = lhs.data[i] && rhs.data[i];
1500 template<
typename T, std::size_t N>
1505 for (std::size_t i = 0; i < N; ++i) {
1506 out += lhs.data[i] * rhs.data[i];
1516 template<
typename T, std::size_t N>
1521 for (std::size_t i = 0; i < N; ++i) {
1522 out.data[i] = std::min(lhs.data[i], rhs.data[i]);
1532 template<
typename T, std::size_t N>
1537 for (std::size_t i = 0; i < N; ++i) {
1538 out.data[i] = std::max(lhs.data[i], rhs.data[i]);
1548 template<
typename T, std::size_t N>
1553 for (std::size_t i = 0; i < N; ++i) {
1554 out.data[i] = std::abs(val.data[i]);
1565 template<
typename T, std::size_t N>
1570 for (std::size_t i = 0; i < N; ++i) {
1571 out.data[i] = (lhs.data[i] == rhs.data[i]);
1581 template<
typename T, std::size_t N>
1586 for (std::size_t i = 0; i < N; ++i) {
1587 out.data[i] = (lhs.data[i] < rhs.data[i]);
1597 template<
typename T, std::size_t N>
1602 for (std::size_t i = 0; i < N; ++i) {
1603 out.data[i] = (lhs.data[i] > rhs.data[i]);
1613 template<
typename T, std::size_t N>
1618 for (std::size_t i = 0; i < N; ++i) {
1619 out.data[i] = (cond.data[i] ? lhs.data[i] : rhs.data[i]);
1632 template<
typename T, std::size_t N>
1637 for (std::size_t i = 0; i < N; ++i) {
1638 out.data[i] = clamp(val.data[i], lo.data[i], hi.data[i]);
1650 template<
typename T, std::size_t N>
1655 for (std::size_t i = 0; i < N; ++i) {
1656 out.data[i] = clamp(val.data[i], lo, hi);
1666 template<
typename T,
typename U, std::size_t N>
1669 return (1 - t) * lhs + t * rhs;
1687 template<
typename T, std::size_t N>
1692 for (std::size_t i = 0; i < N; ++i) {
1693 out += std::abs(vec.data[i]);
1712 template<
typename T, std::size_t N>
1717 for (std::size_t i = 0; i < N; ++i) {
1718 out += square(vec.data[i]);
1739 template<
typename T, std::size_t N>
1742 return std::sqrt(squareLength(vec));
1745 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1751 return std::hypot(vec.x, vec.y);
1757 return std::hypot(vec.x, vec.y);
1776 template<
typename T, std::size_t N>
1779 T out = std::abs(vec.data[0]);
1781 for (std::size_t i = 1; i < N; ++i) {
1782 out = std::max(out, std::abs(vec.data[i]));
1805 template<
typename T, std::size_t N>
1806 using Distance = T (*)(
const Vector<T, N>&,
const Vector<T, N>&);
1814 template<
typename T>
1815 using Distance2 = Distance<T, 2>;
1823 template<
typename T>
1824 using Distance3 = Distance<T, 3>;
1839 template<
typename T, std::size_t N>
1842 return manhattanLength(lhs - rhs);
1858 template<
typename T, std::size_t N>
1861 return squareLength(lhs - rhs);
1877 template<
typename T, std::size_t N>
1880 return euclideanLength(lhs - rhs);
1896 template<
typename T, std::size_t N>
1899 return chebyshevLength(lhs - rhs);
1913 template<
typename T, std::size_t N>
1916 T length = euclideanLength(vec);
1917 return vec / length;
1927 template<
typename T>
1930 return { std::cos(angle), std::sin(angle) };
1940 template<
typename T>
1943 return { -vec.y, vec.x };
1954 template<
typename T>
1958 lhs.y * rhs.z - lhs.z * rhs.y,
1959 lhs.z * rhs.x - lhs.x * rhs.z,
1960 lhs.x * rhs.y - lhs.y * rhs.x
1964 #ifndef DOXYGEN_SHOULD_SKIP_THIS
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:582
T const * end(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:227
T const * cbegin(void) const
Iterator.on the first element (const version).
Definition: Vector.h:947
T manhattanLength(const Vector< T, N > &vec)
Manhattan length of a vector.
Definition: Vector.h:1689
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:565
T dot(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Scalar product.
Definition: Vector.h:1502
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:663
Vector< T, N > abs(const Vector< T, N > &val)
Component-wise absolute value.
Definition: Vector.h:1550
Vector< typename std::common_type< T, U >::type, N > operator/(T lhs, const Vector< U, N > &rhs)
Left scalar division.
Definition: Vector.h:1454
Vector< T, N > select(const Vector< bool, N > &cond, const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise selection operator.
Definition: Vector.h:1615
constexpr Vector< T, 2 > perp(const Vector< T, 2 > &vec)
Perpendicular vector.
Definition: Vector.h:1942
T const * cend(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:725
bool operator==(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Equality operator between two vectors.
Definition: Vector.h:1149
T * begin(void)
Iterator.to the first element.
Definition: Vector.h:907
Vector< typename std::common_type< T, U >::type, N > operator+(T lhs, const Vector< U, N > &rhs)
Left scalar addition.
Definition: Vector.h:1237
T squareLength(const Vector< T, N > &vec)
Square Euclidean length of a vector.
Definition: Vector.h:1714
Vector< T, N > & operator/=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise division and assignment.
Definition: Vector.h:1410
Vector< T, N > clamp(const Vector< T, N > &val, T lo, T hi)
Component-wise clamp function.
Definition: Vector.h:1652
T euclideanLength(const Vector< T, N > &vec)
Euclidean length of a vector.
Definition: Vector.h:1741
Vector< T, N > max(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise maximum.
Definition: Vector.h:1534
Vector< bool, N > greaterThan(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise comparison operator.
Definition: Vector.h:1599
T const * cend(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:958
Vector< T, N > & operator/=(Vector< T, N > &lhs, U rhs)
Right scalar division and assignment.
Definition: Vector.h:1440
Vector< bool, N > equals(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise equality operator.
Definition: Vector.h:1567
constexpr Vector(T x, T y)
Constructor that takes 2 components.
Definition: Vector.h:355
Vector< typename std::common_type< T, U >::type, N > operator+(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise addition.
Definition: Vector.h:1191
friend class RenderTarget
Definition: Shader.h:387
Vector< typename std::common_type< T, U >::type, N > operator*(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise multiplication.
Definition: Vector.h:1318
T const * end(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:453
Vector< typename std::common_type< T, U >::type, N > operator/(const Vector< T, N > &lhs, U rhs)
Right scalar division.
Definition: Vector.h:1424
T const * end(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:938
Vector< T, N > & operator+=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise addition and assignment.
Definition: Vector.h:1207
Vector()=default
Default constructor.
T chebyshevLength(const Vector< T, N > &vec)
Chebyshev length of a vector.
Definition: Vector.h:1778
Vector< bool, N > operator&&(const Vector< bool, N > &lhs, const Vector< bool, N > &rhs)
Component-wise logical and operator.
Definition: Vector.h:1486
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:896
Vector< bool, N > lessThan(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise comparison operator.
Definition: Vector.h:1583
T const * begin(void) const
Iterator.to the first element (const version).
Definition: Vector.h:216
constexpr Vector(T x, T y, T z)
Constructor that takes 3 components.
Definition: Vector.h:594
T const * cbegin(void) const
Iterator.on the first element (const version).
Definition: Vector.h:714
T data[N]
The internal representation of the vector.
Definition: Vector.h:258
Vector< T, N > operator-(const Vector< T, N > &val)
Component-wise unary minus.
Definition: Vector.h:1175
Vector< typename std::common_type< T, U >::type, N > operator-(T lhs, const Vector< U, N > &rhs)
Left scalar substraction.
Definition: Vector.h:1301
T const * begin(void) const
Iterator.to the first element (const version).
Definition: Vector.h:694
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: Vector.h:1668
constexpr Vector(T x, T y, T z, T w)
Constructor that takes 4 components.
Definition: Vector.h:838
T const * cbegin(void) const
Iterator.on the first element (const version).
Definition: Vector.h:462
T chebyshevDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Chebyshev distance between two vectors.
Definition: Vector.h:1898
T squareDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Square Euclidean distance between two vectors.
Definition: Vector.h:1860
Vector< T, 2 > unit(T angle)
Unit vector in a specified direction.
Definition: Vector.h:1929
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:185
Vector< typename std::common_type< T, U >::type, N > operator/(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise division.
Definition: Vector.h:1394
Vector< T, N > clamp(const Vector< T, N > &val, const Vector< T, N > &lo, const Vector< T, N > &hi)
Component-wise clamp function.
Definition: Vector.h:1634
Vector< typename std::common_type< T, U >::type, N > operator-(const Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise substraction.
Definition: Vector.h:1254
T const * cend(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:247
Vector & operator=(const Vector &other)=default
Default copy assignment.
T * begin(void)
Iterator.to the first element.
Definition: Vector.h:196
Vector(const Vector &other)=default
Default copy constructor.
T const * end(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:705
T const * begin(void) const
Iterator.to the first element (const version).
Definition: Vector.h:927
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:648
T * end(void)
Iterator to the element after the last one.
Definition: Vector.h:432
T euclideanDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Euclidean distance between two vectors.
Definition: Vector.h:1879
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:881
T * end(void)
Iterator to the element after the last one.
Definition: Vector.h:917
Vector< bool, N > operator||(const Vector< bool, N > &lhs, const Vector< bool, N > &rhs)
Component-wise logical or operator.
Definition: Vector.h:1470
Vector()=default
Default constructor.
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:396
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:411
Vector< typename std::common_type< T, U >::type, N > operator-(const Vector< T, N > &lhs, U rhs)
Right scalar substraction.
Definition: Vector.h:1285
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:825
constexpr Vector(Vector< T, 2 > xy, T z)
Constructor that takes a 2D vector and a z component.
Definition: Vector.h:606
bool operator!=(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Inequality operator between two vectors.
Definition: Vector.h:1165
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:344
Vector< T, N > normalize(const Vector< T, N > &vec)
Normalize a vector.
Definition: Vector.h:1915
Vector(const Vector &other)=default
Default copy constructor.
Vector(const Vector< U, 3 > &other)
Converting copy constructor.
Definition: Vector.h:629
Vector< T, N > min(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Component-wise minimum.
Definition: Vector.h:1518
Vector< T, N > & operator*=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise multiplication and assignment.
Definition: Vector.h:1334
Vector< typename std::common_type< T, U >::type, N > operator*(const Vector< T, N > &lhs, U rhs)
Right scalar multiplication.
Definition: Vector.h:1348
Vector()=default
Default constructor.
T const * cend(void) const
Iterator on the element after the last one (const version).
Definition: Vector.h:473
Vector(const Vector &other)=default
Default copy constructor.
T const * begin(void) const
Iterator.to the first element (const version).
Definition: Vector.h:442
Vector< T, N > & operator*=(Vector< T, N > &lhs, U rhs)
Right scalar multiplication and assignment.
Definition: Vector.h:1364
Vector(const Vector< U, 4 > &other)
Converting copy constructor.
Definition: Vector.h:861
T manhattanDistance(const Vector< T, N > &lhs, const Vector< T, N > &rhs)
Manhattan distance between two vectors.
Definition: Vector.h:1841
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:170
T const * cbegin(void) const
Iterator.on the first element (const version).
Definition: Vector.h:236
Vector(const Vector< U, N > &other)
Converting copy constructor.
Definition: Vector.h:145
Vector(const Vector &other)=default
Default copy constructor.
T * begin(void)
Iterator.to the first element.
Definition: Vector.h:674
General purpose math vector.
Definition: Vector.h:61
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:87
Vector(std::initializer_list< T > list)
Constructor that takes an initializer list.
Definition: Vector.h:123
constexpr Vector< T, 3 > cross(const Vector< T, 3 > &lhs, const Vector< T, 3 > &rhs)
Cross product.
Definition: Vector.h:1956
Vector< typename std::common_type< T, U >::type, N > operator+(const Vector< T, N > &lhs, U rhs)
Right scalar addition.
Definition: Vector.h:1221
Vector< T, N > & operator-=(Vector< T, N > &lhs, const Vector< U, N > &rhs)
Component-wise substraction and assignment.
Definition: Vector.h:1270
Vector(const Vector< U, 2 > &other)
Converting copy constructor.
Definition: Vector.h:378
Vector< typename std::common_type< T, U >::type, N > operator*(T lhs, const Vector< U, N > &rhs)
Left scalar multiplication.
Definition: Vector.h:1378
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:327
T * end(void)
Iterator to the element after the last one.
Definition: Vector.h:206
T * begin(void)
Iterator.to the first element.
Definition: Vector.h:422
Vector()=default
Default constructor.
T * end(void)
Iterator to the element after the last one.
Definition: Vector.h:684
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:105
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:808