diff options
author | Nao Pross <naopross@thearcway.org> | 2019-01-21 00:39:13 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2019-01-21 00:39:13 +0100 |
commit | e6595cf9c527e97198806c48aa0a14ae8e895e87 (patch) | |
tree | 18e6b0112da7fdda39ad121a813dc16fbcb5b258 | |
parent | Initial commit, add mmvec.hpp with example, makefile and .gitignore (diff) | |
download | libmm-e6595cf9c527e97198806c48aa0a14ae8e895e87.tar.gz libmm-e6595cf9c527e97198806c48aa0a14ae8e895e87.zip |
Add member operator =, +=, -=, *= overloads for basic_vec
-rw-r--r-- | example.cpp | 6 | ||||
-rw-r--r-- | mmvec.hpp | 72 |
2 files changed, 70 insertions, 8 deletions
diff --git a/example.cpp b/example.cpp index e72d65b..3a00f58 100644 --- a/example.cpp +++ b/example.cpp @@ -21,6 +21,8 @@ int main(int argc, char *argv[]) { std::cout << "u * v = " << u * v << std::endl; std::cout << std::endl; + u += v; + // three dimensional vectors std::cout << "three dimensional (double) vectors" << std::endl; @@ -37,6 +39,8 @@ int main(int argc, char *argv[]) { std::cout << "spherical(a) = " << a.spherical() << std::endl; std::cout << std::endl; + a -= b; + // two dimensional vector std::cout << "two dimensional (complex) vectors" << std::endl; @@ -52,5 +56,7 @@ int main(int argc, char *argv[]) { std::cout << "polar(j) = " << j.polar() << std::endl; std::cout << std::endl; + j *= 10; + return 0; } @@ -53,9 +53,23 @@ struct mm::basic_vec : public std::array<T, d> { template<std::size_t n> basic_vec(const basic_vec<T, n>& other); T length() const; + + template<std::size_t n> + basic_vec<T, d>& operator=(const mm::basic_vec<T, n>& other); + + template<std::size_t n> + basic_vec<T, d>& operator+=(const mm::basic_vec<T, n>& other); + + template<std::size_t n> + basic_vec<T, d>& operator-=(const mm::basic_vec<T, n>& other); + + + basic_vec<T, d>& operator*=(const T& scalar); }; +// member functions for basic_vec + template<typename T, std::size_t d> mm::basic_vec<T, d>::basic_vec() : std::array<T, d>() { this->fill(basic_vec<T, d>::null_element); @@ -76,13 +90,8 @@ template<std::size_t n> mm::basic_vec<T, d>::basic_vec(const mm::basic_vec<T, n>& other) { // construct with empty values basic_vec(); - - static_assert( - d >= n, - "cannot copy higher dimensional vector into a smaller one" - ); - - std::copy(other.begin(), other.end(), this->begin()); + // uses operator= + *this = other; } template<typename T, std::size_t d> @@ -95,6 +104,45 @@ T mm::basic_vec<T, d>::length() const { )); } + +// memeber operator overloads for basic_vec + +template<typename T, std::size_t d> +template<std::size_t n> +mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator=(const mm::basic_vec<T, n>& other) { + static_assert( + d >= n, + "cannot copy higher dimensional vector into a smaller one" + ); + + std::copy(other.begin(), other.end(), this->begin()); + + return *this; +} + +template<typename T, std::size_t d> +template<std::size_t n> +mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator+=(const mm::basic_vec<T, n>& other) { + *this = *this + other; + return *this; +} + +template<typename T, std::size_t d> +template<std::size_t n> +mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator-=(const mm::basic_vec<T, n>& other) { + *this = *this - other; + return *this; +} + +template<typename T, std::size_t d> +mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator*=(const T& scalar) { + *this = *this * scalar; + return *this; +} + + +// operator overloads for basic_vec + template<typename T, std::size_t d> mm::basic_vec<T, d> operator+(const mm::basic_vec<T, d>& rhs, const mm::basic_vec<T, d>& lhs) { mm::basic_vec<T, d> out; @@ -146,7 +194,9 @@ std::ostream& operator<<(std::ostream& os, const mm::basic_vec<T, d>& v) { return os; } -// actual classes to use in your code + +// actual vectors to use in your code + template<typename T, std::size_t d> class mm::vec: public mm::basic_vec<T, d> { public: @@ -156,7 +206,10 @@ public: vec(const basic_vec<T, n>& other) : basic_vec<T, d>(other) {} }; + // three dimensional specialization with a static cross product +// TODO: specialize operator+ for spherical coordinates + template<typename T> class mm::vec3 : public mm::basic_vec<T, 3> { public: @@ -211,7 +264,10 @@ mm::vec3<T> mm::vec3<T>::cross(const vec3<T>& rhs, const vec3<T>& lhs) { return res; } + // two dimensional specialization with a polar conversion +// TODO: specialize operator+ for polar coordinates + template<typename T> class mm::vec2: public mm::basic_vec<T, 2> { public: |