From 97ff46e747aa58198f63cc10382a8013128a50e5 Mon Sep 17 00:00:00 2001 From: hop311 Date: Sat, 9 Dec 2023 21:55:19 +0000 Subject: Make vec2_t header only (friend functions weren't being found) --- src/openvic-simulation/types/Vector.cpp | 79 ------------------- src/openvic-simulation/types/Vector.hpp | 132 +++++++++++++++++++++++++++----- 2 files changed, 114 insertions(+), 97 deletions(-) delete mode 100644 src/openvic-simulation/types/Vector.cpp (limited to 'src') 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 -constexpr vec2_t::vec2_t(T new_val) : x { new_val }, y { new_val } {} - -template -constexpr vec2_t::vec2_t(T new_x, T new_y) : x { new_x }, y { new_y } {} - -template -constexpr vec2_t vec2_t::abs() const { - return {}; -} - -template -constexpr T vec2_t::length_squared() const { - return x * x + y * y; -} - -template -constexpr T* vec2_t::data() { - return reinterpret_cast(this); -} - -template -constexpr T const* vec2_t::data() const { - return reinterpret_cast(this); -} - -template -constexpr T& vec2_t::operator[](size_t index) { - return data()[index & 1]; -} - -template -constexpr T const& vec2_t::operator[](size_t index) const { - return data()[index & 1]; -} - -template -constexpr vec2_t operator+(vec2_t const& left, vec2_t const& right) { - return { left.x + right.x, left.y + right.y }; -} - -template -constexpr vec2_t& vec2_t::operator+=(vec2_t const& right) { - x += right.x; - y += right.y; - return *this; -} - -template -constexpr vec2_t operator-(vec2_t const& arg) { - return { -arg.x, -arg.y }; -} - -template -constexpr vec2_t operator-(vec2_t const& left, vec2_t const& right) { - return { left.x - right.x, left.y - right.y }; -} - -template -constexpr vec2_t& vec2_t::operator-=(vec2_t const& right) { - x -= right.x; - y -= right.y; - return *this; -} - -template -constexpr std::ostream& operator<<(std::ostream& stream, vec2_t const& value) { - return stream << "(" << value.x << ", " << value.y << ")"; -} - -template struct OpenVic::vec2_t; -template struct OpenVic::vec2_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(this); + } + constexpr T const* data() const { + return reinterpret_cast(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 - constexpr friend vec2_t operator+(vec2_t const& left, vec2_t 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 - constexpr friend vec2_t operator-(vec2_t const& arg); + constexpr friend vec2_t operator-(vec2_t const& arg) { + return { -arg.x, -arg.y }; + } - template - constexpr friend vec2_t operator-(vec2_t const& left, vec2_t 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 - constexpr friend std::ostream& operator<<(std::ostream& stream, vec2_t 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; using fvec2_t = vec2_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"); } -- cgit v1.2.3-56-ga3b1