diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index fd3c4f2..ba85613 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -10,216 +10,223 @@ #include #include -template -struct vec { - private: - T elements[size]{}; - public: - - vec() { - for (int i = 0; i < size; i++) - elements[i] = 0; - } - - vec(std::initializer_list args): vec() { - for (int i = 0; i < args.size(); i++) { - elements[i] = *(args.begin() + i); +namespace blt { + + template + struct vec { + private: + T elements[size]{}; + public: + + vec() { + for (int i = 0; i < size; i++) + elements[i] = 0; } - } - - explicit vec(const T elem[size]) { - for (int i = 0; i < size; i++) { - elements[i] = elem[i]; + + vec(std::initializer_list args): vec() { + for (int i = 0; i < args.size(); i++) { + elements[i] = *(args.begin() + i); + } } - } - - vec(const vec& copy): vec(copy.elements) {} - - [[nodiscard]] inline T x() const {return elements[0];} - [[nodiscard]] inline T y() const {return elements[1];} - [[nodiscard]] inline T z() const {return elements[2];} - [[nodiscard]] inline T w() const {return elements[3];} - - inline T& operator[](int index) { - return elements[index]; - } - - inline T operator[](int index) const { - return elements[index]; - } - - inline vec& operator=(T f) { - for (int i = 0; i < size; i++) - elements[i] = f; - return *this; - } - - inline vec& operator=(int i) { - for (int _ = 0; _ < size; _++) - elements[_] = i; - return *this; - } - - inline vec operator-() { - T negativeCopy[size]; - for (int i = 0; i < size; i++) - negativeCopy[i] = -elements[i]; - return vec{negativeCopy}; - } - - inline vec& operator+=(const vec& other) { - for (int i = 0; i < size; i++) - elements[i] += other[i]; - return *this; - } - - inline vec& operator*=(const vec& other) { - for (int i = 0; i < size; i++) - elements[i] *= other[i]; - return *this; - } - - inline vec& operator+=(T f) { - for (int i = 0; i < size; i++) - elements[i] += f; - return *this; - } - - inline vec& operator*=(T f) { - for (int i = 0; i < size; i++) - elements[i] *= f; - return *this; - } - - inline vec& operator-=(const vec& other) { - for (int i = 0; i < size; i++) - elements[i] -= other[i]; - return *this; - } - - inline vec& operator-=(T f) { - for (int i = 0; i < size; i++) - elements[i] -= f; - return *this; - } - - /** - * performs the dot product of left * right - */ - static inline T dot(const vec& left, const vec& right) { - T dot = 0; - for (int i = 0; i < size; i++) - dot += left[i] * right[i]; - return dot; - } -}; - -template -inline vec operator+(const vec& left, const vec& right) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = left[i] + right[i]; - return vec{initializer}; + + explicit vec(const T elem[size]) { + for (int i = 0; i < size; i++) { + elements[i] = elem[i]; + } + } + + vec(const vec& copy): vec(copy.elements) {} + + [[nodiscard]] inline T x() const { return elements[0]; } + + [[nodiscard]] inline T y() const { return elements[1]; } + + [[nodiscard]] inline T z() const { return elements[2]; } + + [[nodiscard]] inline T w() const { return elements[3]; } + + inline T& operator[](int index) { + return elements[index]; + } + + inline T operator[](int index) const { + return elements[index]; + } + + inline vec& operator=(T f) { + for (int i = 0; i < size; i++) + elements[i] = f; + return *this; + } + + inline vec& operator=(int i) { + for (int _ = 0; _ < size; _++) + elements[_] = i; + return *this; + } + + inline vec operator-() { + T negativeCopy[size]; + for (int i = 0; i < size; i++) + negativeCopy[i] = -elements[i]; + return vec{negativeCopy}; + } + + inline vec& operator+=(const vec& other) { + for (int i = 0; i < size; i++) + elements[i] += other[i]; + return *this; + } + + inline vec& operator*=(const vec& other) { + for (int i = 0; i < size; i++) + elements[i] *= other[i]; + return *this; + } + + inline vec& operator+=(T f) { + for (int i = 0; i < size; i++) + elements[i] += f; + return *this; + } + + inline vec& operator*=(T f) { + for (int i = 0; i < size; i++) + elements[i] *= f; + return *this; + } + + inline vec& operator-=(const vec& other) { + for (int i = 0; i < size; i++) + elements[i] -= other[i]; + return *this; + } + + inline vec& operator-=(T f) { + for (int i = 0; i < size; i++) + elements[i] -= f; + return *this; + } + + /** + * performs the dot product of left * right + */ + static inline T dot(const vec& left, const vec& right) { + T dot = 0; + for (int i = 0; i < size; i++) + dot += left[i] * right[i]; + return dot; + } + }; + + template + inline vec operator+(const vec& left, const vec& right) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = left[i] + right[i]; + return vec{initializer}; + } + + template + inline vec operator-(const vec& left, const vec& right) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = left[i] - right[i]; + return vec{initializer}; + } + + template + inline vec operator+(const vec& left, float f) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = left[i] + f; + return vec{initializer}; + } + + template + inline vec operator-(const vec& left, float f) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = left[i] + f; + return vec{initializer}; + } + + template + inline vec operator+(float f, const vec& right) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = f + right[i]; + return vec{initializer}; + } + + template + inline vec operator-(float f, const vec& right) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = f - right[i]; + return vec{initializer}; + } + + template + inline vec operator*(const vec& left, const vec& right) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = left[i] * right[i]; + return vec{initializer}; + } + + template + inline vec operator*(const vec& left, float f) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = left[i] * f; + return vec{initializer}; + } + + template + inline vec operator*(float f, const vec& right) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = f * right[i]; + return vec{initializer}; + } + + template + inline vec operator/(const vec& left, float f) { + float initializer[size]; + for (int i = 0; i < size; i++) + initializer[i] = left[i] / f; + return vec{initializer}; + } + + typedef vec vec2f; + typedef vec vec3f; + typedef vec vec4f; + + typedef vec vec2d; + typedef vec vec3d; + typedef vec vec4d; + + typedef vec vec2i; + typedef vec vec3i; + typedef vec vec4i; + + typedef vec vec2l; + typedef vec vec3l; + typedef vec vec4l; + + typedef vec vec2ui; + typedef vec vec3ui; + typedef vec vec4ui; + + typedef vec vec2ul; + typedef vec vec3ul; + typedef vec vec4ul; + + typedef vec2f vec2; + typedef vec3f vec3; + typedef vec4f vec4; + } -template -inline vec operator-(const vec& left, const vec& right) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = left[i] - right[i]; - return vec{initializer}; -} - -template -inline vec operator+(const vec& left, float f) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = left[i] + f; - return vec{initializer}; -} - -template -inline vec operator-(const vec& left, float f) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = left[i] + f; - return vec{initializer}; -} - -template -inline vec operator+(float f, const vec& right) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = f + right[i]; - return vec{initializer}; -} - -template -inline vec operator-(float f, const vec& right) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = f - right[i]; - return vec{initializer}; -} - -template -inline vec operator*(const vec& left, const vec& right) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = left[i] * right[i]; - return vec{initializer}; -} - -template -inline vec operator*(const vec& left, float f) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = left[i] * f; - return vec{initializer}; -} - -template -inline vec operator*(float f, const vec& right) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = f * right[i]; - return vec{initializer}; -} - -template -inline vec operator/(const vec& left, float f) { - float initializer[size]; - for (int i = 0; i < size; i++) - initializer[i] = left[i] / f; - return vec{initializer}; -} - -typedef vec vec2f; -typedef vec vec3f; -typedef vec vec4f; - -typedef vec vec2d; -typedef vec vec3d; -typedef vec vec4d; - -typedef vec vec2i; -typedef vec vec3i; -typedef vec vec4i; - -typedef vec vec2l; -typedef vec vec3l; -typedef vec vec4l; - -typedef vec vec2ui; -typedef vec vec3ui; -typedef vec vec4ui; - -typedef vec vec2ul; -typedef vec vec3ul; -typedef vec vec4ul; - -typedef vec2f vec2; -typedef vec3f vec3; -typedef vec4f vec4; - #endif //BLT_TESTS_VECTORS_H