| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #ifndef ANKI_MATH_VEC2_H
- #define ANKI_MATH_VEC2_H
- #include "anki/math/CommonIncludes.h"
- namespace anki {
- /// @addtogroup Math
- /// @{
- /// 2D vector
- template<typename T>
- class TVec2
- {
- /// @name Friends
- template<typename Y>
- friend TVec2<Y> operator+(const Y f, const TVec2<Y>& v2);
- template<typename Y>
- friend TVec2<Y> operator-(const Y f, const TVec2<Y>& v2);
- template<typename Y>
- friend TVec2<Y> operator*(const Y f, const TVec2<Y>& v2);
- template<typename Y>
- friend TVec2<Y> operator/(const Y f, const TVec2<Y>& v2);
- ///@]
- public:
- /// @name Constructors
- /// @{
- explicit TVec2()
- {}
- explicit TVec2(const T x_, const T y_)
- {
- x() = x_;
- y() = y_;
- }
-
- explicit TVec2(const T f)
- {
- x() = y() = f;
- }
- explicit TVec2(const T arr_[])
- {
- x() = arr_[0];
- y() = arr_[1];
- }
- TVec2(const TVec2& b)
- {
- x() = b.x();
- y() = b.y();
- }
- explicit TVec2(const TVec3<T>& v3)
- {
- x() = v3.x();
- y() = v3.y();
- }
- explicit TVec2(const TVec4<T>& v4)
- {
- x() = v4.x();
- y() = v4.y();
- }
- /// @}
- /// @name Accessors
- /// @{
- T& x()
- {
- return vec.x;
- }
- T x() const
- {
- return vec.x;
- }
- T& y()
- {
- return vec.y;
- }
- T y() const
- {
- return vec.y;
- }
- T& operator[](const U i)
- {
- return arr[i];
- }
- T operator[](const U i) const
- {
- return arr[i];
- }
- /// @}
- /// @name Operators with same type
- /// @{
- TVec2& operator=(const TVec2& b)
- {
- x() = b.x();
- y() = b.y();
- return *this;
- }
- TVec2 operator+(const TVec2& b) const
- {
- return TVec2(x() + b.x(), y() + b.y());
- }
- TVec2& operator+=(const TVec2& b)
- {
- x() += b.x();
- y() += b.y();
- return (*this);
- }
- TVec2 operator-(const TVec2& b) const
- {
- return TVec2(x() - b.x(), y() - b.y());
- }
- TVec2& operator-=(const TVec2& b)
- {
- x() -= b.x();
- y() -= b.y();
- return (*this);
- }
- TVec2 operator*(const TVec2& b) const
- {
- return TVec2(x() * b.x(), y() * b.y());
- }
- TVec2& operator*=(const TVec2& b)
- {
- x() *= b.x();
- y() *= b.y();
- return (*this);
- }
- TVec2 operator/(const TVec2& b) const
- {
- return TVec2(x() / b.x(), y() / b.y());
- }
- TVec2& operator/=(const TVec2& b)
- {
- x() /= b.x();
- y() /= b.y();
- return (*this);
- }
- TVec2 operator-() const
- {
- return TVec2(-x(), -y());
- }
- Bool operator==(const TVec2& b) const
- {
- return isZero<T>(x() - b.x()) && isZero<T>(y() - b.y());
- }
- Bool operator!=(const TVec2& b) const
- {
- return !(*this == b);
- }
- Bool operator<(const TVec2& b) const
- {
- return vec.x < b.vec.x && vec.y < b.vec.y;
- }
- Bool operator<=(const TVec2& b) const
- {
- return vec.x <= b.vec.x && vec.y <= b.vec.y;
- }
- Bool operator>(const TVec2& b) const
- {
- return vec.x > b.vec.x && vec.y > b.vec.y;
- }
- Bool operator>=(const TVec2& b) const
- {
- return vec.x >= b.vec.x && vec.y >= b.vec.y;
- }
- /// @}
- /// @name Operators with T
- /// @{
- TVec2 operator+(const T f) const
- {
- return (*this) + TVec2(f);
- }
- TVec2& operator+=(const T f)
- {
- (*this) += TVec2(f);
- return (*this);
- }
- TVec2 operator-(const T f) const
- {
- return (*this) - TVec2(f);
- }
- TVec2& operator-=(const T f)
- {
- (*this) -= TVec2(f);
- return (*this);
- }
- TVec2 operator*(const T f) const
- {
- return (*this) * TVec2(f);
- }
- TVec2& operator*=(const T f)
- {
- (*this) *= TVec2(f);
- return (*this);
- }
- TVec2 operator/(const T f) const
- {
- return (*this) / TVec2(f);
- }
- TVec2& operator/=(const T f)
- {
- (*this) /= TVec2(f);
- return (*this);
- }
- /// @}
- /// @name Other
- /// @{
- T getLengthSquared() const
- {
- return x() * x() + y() * y();
- }
- T getLength() const
- {
- return sqrt<T>(getLengthSquared());
- }
- TVec2 getNormalized() const
- {
- return (*this) / getLength();
- }
- void normalize()
- {
- (*this) /= getLength();
- }
- T dot(const TVec2& b) const
- {
- return x() * b.x() + y() * b.y();
- }
- std::string toString() const
- {
- return std::to_string(x()) + " " + std::to_string(y());
- }
- /// @}
- private:
- /// @name Data members
- /// @{
- union
- {
- struct
- {
- T x, y;
- } vec;
- Array<T, 2> arr;
- };
- /// @}
- };
- template<typename T>
- TVec2<T> operator+(const T f, const TVec2<T>& v2)
- {
- return v2 + f;
- }
- template<typename T>
- TVec2<T> operator-(const T f, const TVec2<T>& v2)
- {
- return TVec2<T>(f - v2.x(), f - v2.y());
- }
- template<typename T>
- TVec2<T> operator*(const T f, const TVec2<T>& v2)
- {
- return v2 * f;
- }
- template<typename T>
- TVec2<T> operator/(const T f, const TVec2<T>& v2)
- {
- return TVec2<T>(f / v2.x(), f / v2.y());
- }
- /// F32 2D vector
- typedef TVec2<F32> Vec2;
- static_assert(sizeof(Vec2) == sizeof(F32) * 2, "Incorrect size");
- /// Half float 2D vector
- typedef TVec2<F16> HVec2;
- /// 32bit signed integer 2D vector
- typedef TVec2<I32> IVec2;
- /// 32bit unsigned integer 2D vector
- typedef TVec2<U32> UVec2;
- /// @}
- } // end namespace anki
- #endif
|