Common: Add additional Vector operations.

This commit is contained in:
Jordan Woyak 2020-01-21 18:31:02 -06:00
parent 1e652d7d34
commit 259a7191d2

View File

@ -20,6 +20,11 @@ union TVec3
TVec3() = default; TVec3() = default;
TVec3(T _x, T _y, T _z) : data{_x, _y, _z} {} TVec3(T _x, T _y, T _z) : data{_x, _y, _z} {}
template <typename OtherT>
explicit TVec3(const TVec3<OtherT>& other) : TVec3(other.x, other.y, other.z)
{
}
TVec3 Cross(const TVec3& rhs) const TVec3 Cross(const TVec3& rhs) const
{ {
return {(y * rhs.z) - (rhs.y * z), (z * rhs.x) - (rhs.z * x), (x * rhs.y) - (rhs.x * y)}; return {(y * rhs.z) - (rhs.y * z), (z * rhs.x) - (rhs.z * x), (x * rhs.y) - (rhs.x * y)};
@ -98,6 +103,11 @@ TVec3<bool> operator<(const TVec3<T>& lhs, const TVec3<T>& rhs)
return lhs.Map(std::less<T>{}, rhs); return lhs.Map(std::less<T>{}, rhs);
} }
inline TVec3<bool> operator!(const TVec3<bool>& vec)
{
return {!vec.x, !vec.y, !vec.z};
}
template <typename T> template <typename T>
auto operator+(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x + rhs.x)> auto operator+(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x + rhs.x)>
{ {
@ -197,6 +207,11 @@ union TVec2
TVec2() = default; TVec2() = default;
TVec2(T _x, T _y) : data{_x, _y} {} TVec2(T _x, T _y) : data{_x, _y} {}
template <typename OtherT>
explicit TVec2(const TVec2<OtherT>& other) : TVec2(other.x, other.y)
{
}
T Cross(const TVec2& rhs) const { return (x * rhs.y) - (y * rhs.x); } T Cross(const TVec2& rhs) const { return (x * rhs.y) - (y * rhs.x); }
T Dot(const TVec2& rhs) const { return (x * rhs.x) + (y * rhs.y); } T Dot(const TVec2& rhs) const { return (x * rhs.x) + (y * rhs.y); }
T LengthSquared() const { return Dot(*this); } T LengthSquared() const { return Dot(*this); }
@ -217,6 +232,20 @@ union TVec2
return *this; return *this;
} }
TVec2& operator*=(const TVec2& rhs)
{
x *= rhs.x;
y *= rhs.y;
return *this;
}
TVec2& operator/=(const TVec2& rhs)
{
x /= rhs.x;
y /= rhs.y;
return *this;
}
TVec2& operator*=(T scalar) TVec2& operator*=(T scalar)
{ {
x *= scalar; x *= scalar;
@ -242,6 +271,17 @@ union TVec2
}; };
}; };
template <typename T>
TVec2<bool> operator<(const TVec2<T>& lhs, const TVec2<T>& rhs)
{
return {lhs.x < rhs.x, lhs.y < rhs.y};
}
inline TVec2<bool> operator!(const TVec2<bool>& vec)
{
return {!vec.x, !vec.y};
}
template <typename T> template <typename T>
TVec2<T> operator+(TVec2<T> lhs, const TVec2<T>& rhs) TVec2<T> operator+(TVec2<T> lhs, const TVec2<T>& rhs)
{ {
@ -255,15 +295,27 @@ TVec2<T> operator-(TVec2<T> lhs, const TVec2<T>& rhs)
} }
template <typename T> template <typename T>
TVec2<T> operator*(TVec2<T> lhs, T scalar) TVec2<T> operator*(TVec2<T> lhs, const TVec2<T>& rhs)
{ {
return lhs *= scalar; return lhs *= rhs;
} }
template <typename T> template <typename T>
TVec2<T> operator/(TVec2<T> lhs, T scalar) TVec2<T> operator/(TVec2<T> lhs, const TVec2<T>& rhs)
{ {
return lhs /= scalar; return lhs /= rhs;
}
template <typename T, typename T2>
auto operator*(TVec2<T> lhs, T2 scalar)
{
return TVec2<decltype(lhs.x * scalar)>(lhs) *= scalar;
}
template <typename T, typename T2>
auto operator/(TVec2<T> lhs, T2 scalar)
{
return TVec2<decltype(lhs.x / scalar)>(lhs) /= scalar;
} }
using Vec2 = TVec2<float>; using Vec2 = TVec2<float>;