diff options
author | Hop311 <Hop3114@gmail.com> | 2023-12-10 01:54:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-10 01:54:03 +0100 |
commit | c422a344b618e89458c98759207067bada4ec542 (patch) | |
tree | 47160685e4eef96f6c9f54d243437cb0d74733a1 /src/openvic-simulation/types | |
parent | b3d0af96e5820e38f94bcfa04130768da99c8360 (diff) | |
parent | 97ff46e747aa58198f63cc10382a8013128a50e5 (diff) |
Merge pull request #88 from OpenVicProject/vec-header
Make vec2_t header only (friend functions weren't being found)
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r-- | src/openvic-simulation/types/Vector.cpp | 79 | ||||
-rw-r--r-- | src/openvic-simulation/types/Vector.hpp | 132 |
2 files changed, 114 insertions, 97 deletions
diff --git a/src/openvic-simulation/types/Vector.cpp b/src/openvic-simulation/types/Vector.cpp deleted file mode 100644 index 28f30bf..0000000 --- a/src/openvic-simulation/types/Vector.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "Vector.hpp" - -using namespace OpenVic; - -template<typename T> -constexpr vec2_t<T>::vec2_t(T new_val) : x { new_val }, y { new_val } {} - -template<typename T> -constexpr vec2_t<T>::vec2_t(T new_x, T new_y) : x { new_x }, y { new_y } {} - -template<typename T> -constexpr vec2_t<T> vec2_t<T>::abs() const { - return {}; -} - -template<typename T> -constexpr T vec2_t<T>::length_squared() const { - return x * x + y * y; -} - -template<typename T> -constexpr T* vec2_t<T>::data() { - return reinterpret_cast<T*>(this); -} - -template<typename T> -constexpr T const* vec2_t<T>::data() const { - return reinterpret_cast<T const*>(this); -} - -template<typename T> -constexpr T& vec2_t<T>::operator[](size_t index) { - return data()[index & 1]; -} - -template<typename T> -constexpr T const& vec2_t<T>::operator[](size_t index) const { - return data()[index & 1]; -} - -template<typename T> -constexpr vec2_t<T> operator+(vec2_t<T> const& left, vec2_t<T> const& right) { - return { left.x + right.x, left.y + right.y }; -} - -template<typename T> -constexpr vec2_t<T>& vec2_t<T>::operator+=(vec2_t const& right) { - x += right.x; - y += right.y; - return *this; -} - -template<typename T> -constexpr vec2_t<T> operator-(vec2_t<T> const& arg) { - return { -arg.x, -arg.y }; -} - -template<typename T> -constexpr vec2_t<T> operator-(vec2_t<T> const& left, vec2_t<T> const& right) { - return { left.x - right.x, left.y - right.y }; -} - -template<typename T> -constexpr vec2_t<T>& vec2_t<T>::operator-=(vec2_t const& right) { - x -= right.x; - y -= right.y; - return *this; -} - -template<typename T> -constexpr std::ostream& operator<<(std::ostream& stream, vec2_t<T> const& value) { - return stream << "(" << value.x << ", " << value.y << ")"; -} - -template struct OpenVic::vec2_t<int64_t>; -template struct OpenVic::vec2_t<fixed_point_t>; - -static_assert(sizeof(ivec2_t) == 2 * sizeof(ivec2_t::type), "ivec2_t size does not equal the sum of its parts' sizes"); -static_assert(sizeof(fvec2_t) == 2 * sizeof(fvec2_t::type), "fvec2_t size does not equal the sum of its parts' sizes"); diff --git a/src/openvic-simulation/types/Vector.hpp b/src/openvic-simulation/types/Vector.hpp index 9f576d7..327806d 100644 --- a/src/openvic-simulation/types/Vector.hpp +++ b/src/openvic-simulation/types/Vector.hpp @@ -11,33 +11,129 @@ namespace OpenVic { T x, y; constexpr vec2_t() = default; - constexpr vec2_t(T new_val); - constexpr vec2_t(T new_x, T new_y); + constexpr vec2_t(T new_val) : x { new_val }, y { new_val } {} + constexpr vec2_t(T new_x, T new_y) : x { new_x }, y { new_y } {} - constexpr vec2_t abs() const; - constexpr T length_squared() const; + constexpr vec2_t abs() const { + return { x >= 0 ? x : -x, y >= 0 ? y : -y }; + } + constexpr T length_squared() const { + return x * x + y * y; + } - constexpr T* data(); - constexpr T const* data() const; + constexpr T* data() { + return reinterpret_cast<T*>(this); + } + constexpr T const* data() const { + return reinterpret_cast<T const*>(this); + } - constexpr T& operator[](size_t index); - constexpr T const& operator[](size_t index) const; + constexpr T& operator[](size_t index) { + return data()[index & 1]; + } + constexpr T const& operator[](size_t index) const { + return data()[index & 1]; + } - template<typename S> - constexpr friend vec2_t<S> operator+(vec2_t<S> const& left, vec2_t<S> const& right); - constexpr vec2_t& operator+=(vec2_t const& right); + constexpr friend vec2_t operator+(vec2_t const& left, vec2_t const& right) { + return { left.x + right.x, left.y + right.y }; + } + constexpr vec2_t& operator+=(vec2_t const& right) { + x += right.x; + y += right.y; + return *this; + } + constexpr friend vec2_t operator+(vec2_t const& left, T const& right) { + return { left.x + right, left.y + right }; + } + constexpr friend vec2_t operator+(T const& left, vec2_t const& right) { + return { left + right.x, left + right.y }; + } + constexpr vec2_t& operator+=(T const& right) { + x += right; + y += right; + return *this; + } - template<typename S> - constexpr friend vec2_t<S> operator-(vec2_t<S> const& arg); + constexpr friend vec2_t operator-(vec2_t const& arg) { + return { -arg.x, -arg.y }; + } - template<typename S> - constexpr friend vec2_t<S> operator-(vec2_t<S> const& left, vec2_t<S> const& right); - constexpr vec2_t& operator-=(vec2_t const& right); + constexpr friend vec2_t operator-(vec2_t const& left, vec2_t const& right) { + return { left.x - right.x, left.y - right.y }; + } + constexpr vec2_t& operator-=(vec2_t const& right) { + x -= right.x; + y -= right.y; + return *this; + } + constexpr friend vec2_t operator-(vec2_t const& left, T const& right) { + return { left.x - right, left.y - right }; + } + constexpr friend vec2_t operator-(T const& left, vec2_t const& right) { + return { left - right.x, left - right.y }; + } + constexpr vec2_t& operator-=(T const& right) { + x -= right; + y -= right; + return *this; + } - template<typename S> - constexpr friend std::ostream& operator<<(std::ostream& stream, vec2_t<S> const& value); + constexpr friend vec2_t operator*(vec2_t const& left, vec2_t const& right) { + return { left.x * right.x, left.y * right.y }; + } + constexpr vec2_t& operator*=(vec2_t const& right) { + x *= right.x; + y *= right.y; + return *this; + } + constexpr friend vec2_t operator*(vec2_t const& left, T const& right) { + return { left.x * right, left.y * right }; + } + constexpr friend vec2_t operator*(T const& left, vec2_t const& right) { + return { left * right.x, left * right.y }; + } + constexpr vec2_t& operator*=(T const& right) { + x *= right; + y *= right; + return *this; + } + + constexpr friend vec2_t operator/(vec2_t const& left, vec2_t const& right) { + return { left.x / right.x, left.y / right.y }; + } + constexpr vec2_t& operator/=(vec2_t const& right) { + x /= right.x; + y /= right.y; + return *this; + } + constexpr friend vec2_t operator/(vec2_t const& left, T const& right) { + return { left.x / right, left.y / right }; + } + constexpr friend vec2_t operator/(T const& left, vec2_t const& right) { + return { left / right.x, left / right.y }; + } + constexpr vec2_t& operator/=(T const& right) { + x /= right; + y /= right; + return *this; + } + + constexpr friend bool operator==(vec2_t const& left, vec2_t const& right) { + return left.x == right.x && left.y == right.y; + } + constexpr friend bool operator!=(vec2_t const& left, vec2_t const& right) { + return left.x != right.x || left.y != right.y; + } + + constexpr friend std::ostream& operator<<(std::ostream& stream, vec2_t const& value) { + return stream << "(" << value.x << ", " << value.y << ")"; + } }; using ivec2_t = vec2_t<int32_t>; using fvec2_t = vec2_t<fixed_point_t>; + + static_assert(sizeof(ivec2_t) == 2 * sizeof(ivec2_t::type), "ivec2_t size does not equal the sum of its parts' sizes"); + static_assert(sizeof(fvec2_t) == 2 * sizeof(fvec2_t::type), "fvec2_t size does not equal the sum of its parts' sizes"); } |