Topaz 5.0
Topaz Game Engine
Loading...
Searching...
No Matches
vector.hpp
1#ifndef TOPAZ_CORE_VECTOR_HPP
2#define TOPAZ_CORE_VECTOR_HPP
3#include <concepts>
4#include <array>
5#include <type_traits>
6#include <algorithm>
7
8namespace tz
9{
21 template<typename T, int N>
22 requires std::integral<T> || std::floating_point<T>
23 struct vector
24 {
26 static constexpr vector<T, N> zero()
27 {
28 return vector<T, N>::filled(T{0});
29 }
30
32 static constexpr vector<T, N> filled(T t)
33 {
34 std::array<T, N> values;
35 std::fill(values.begin(), values.end(), t);
36 return {values};
37 }
38
43 template<typename... Ts>
44 requires(
45 sizeof...(Ts) <= N
46 && sizeof...(Ts) > 1
47 && std::is_convertible_v<std::tuple_element_t<0, std::tuple<Ts...>>, T>)
48 constexpr vector(Ts&&... ts) :
49 arr({ std::forward<Ts>(ts)... }) {}
53 constexpr vector(std::array<T, N> data): arr(data){}
57 vector() = default;
58 vector(const vector<T, N>& cpy) = default;
59 vector(vector<T, N>&& move) = default;
60 vector& operator=(const vector<T, N>& rhs) = default;
61 vector& operator=(vector<T, N>&& rhs) = default;
62
68 const T& operator[](std::size_t idx) const;
74 T& operator[](std::size_t idx);
75
76 // Multiply vector by scalar
77 vector<T, N>& operator*=(T scalar);
78 vector<T, N>& operator/=(T scalar);
79 vector<T, N> operator*(T scalar) const{auto cpy = *this; return cpy *= scalar;}
80 vector<T, N> operator/(T scalar) const{auto cpy = *this; return cpy /= scalar;}
81
90
92 vector<T, N> operator+(const vector<T, N>& rhs) const{auto cpy = *this; return cpy += rhs;}
94 vector<T, N> operator-(const vector<T, N>& rhs) const{auto cpy = *this; return cpy -= rhs;}
96 vector<T, N> operator*(const vector<T, N>& rhs) const{auto cpy = *this; return cpy *= rhs;}
98 vector<T, N> operator/(const vector<T, N>& rhs) const{auto cpy = *this; return cpy /= rhs;}
99
100 template<typename C>
101 operator vector<C, N>() const requires(std::is_convertible_v<T, C>)
102 {
103 std::array<C, N> arr;
104 std::transform(this->arr.begin(), this->arr.end(), arr.begin(), [](const T& t)->C{return static_cast<C>(t);});
105 return {arr};
106 }
107
109 T length() const;
111 T dot(const vector<T, N>& rhs) const;
113 vector<T, N> cross(const vector<T, N>& rhs) const requires(N == 3);
114
116 bool operator==(const vector<T, N>& rhs) const = default;
117 private:
118 std::array<T, N> arr;
119 };
120
181}
182
183#endif // TOPAZ_CORE_VECTOR_HPP
Represents the generic vector.
Definition vector.hpp:24
T length() const
Retrieve the magnitude of the vector.
bool operator==(const vector< T, N > &rhs) const =default
Compare two vectors. Two vectors are equal if all of their components are exactly equal.
vector< T, N > cross(const vector< T, N > &rhs) const
Retrieve a cross product between two three-dimensional vectors.
static constexpr vector< T, N > zero()
Retrieve a vector filled with zeroes.
Definition vector.hpp:26
vector< T, N > & operator-=(const vector< T, N > &rhs)
Subtract one vector from another.
constexpr vector(std::array< T, N > data)
Definition vector.hpp:53
vector< T, N > & operator+=(const vector< T, N > &rhs)
Add one vector to another.
constexpr vector(Ts &&... ts)
Definition vector.hpp:48
static constexpr vector< T, N > filled(T t)
Retrieve a vector filled with the given value.
Definition vector.hpp:32
vector()=default
vector< T, N > operator-(const vector< T, N > &rhs) const
Subtract one vector from another.
Definition vector.hpp:94
vector< T, N > operator*(const vector< T, N > &rhs) const
Multiply one vector with another.
Definition vector.hpp:96
T & operator[](std::size_t idx)
vector< T, N > & operator*=(const vector< T, N > &rhs)
Multiply one vector with another.
const T & operator[](std::size_t idx) const
vector< T, N > & operator/=(const vector< T, N > &rhs)
Divide one vector by another.
vector< T, N > operator+(const vector< T, N > &rhs) const
Add one vector to another.
Definition vector.hpp:92
T dot(const vector< T, N > &rhs) const
Retrieve the dot (scalar) product of two vectors.
vector< T, N > operator/(const vector< T, N > &rhs) const
Divide one vector by another.
Definition vector.hpp:98