math/Vector: Add Vector3 rotation

This commit is contained in:
Léo Lam 2022-03-23 23:34:10 +01:00
parent cb6b94aab4
commit 1a419d0497
No known key found for this signature in database
GPG Key ID: 0DF30F9081000741
4 changed files with 54 additions and 3 deletions

View File

@ -131,10 +131,14 @@ struct Vector3 : public Policies<T>::Vec3Base
bool equals(const Vector3& rhs, T epsilon = 0) const;
void add(const Vector3& a);
/// Multiply m by this vector (self = m * self).
/// Apply a rotation `m` to this vector.
void mul(const Mtx33& m);
/// Apply a transformation `m` (rotation + translation) to this vector.
/// Apply a transformation `m` (rotation then translation) to this vector.
void mul(const Mtx34& m);
/// Apply a rotation `m` to this vector.
void rotate(const Mtx33& m);
/// Apply a rotation `m` to this vector.
void rotate(const Mtx34& m);
void multScalar(T t);
T normalize();
@ -144,6 +148,8 @@ struct Vector3 : public Policies<T>::Vec3Base
void setScaleAdd(T t, const Vector3<T>& a, const Vector3<T>& b);
void setMul(const Mtx33& m, const Vector3& a);
void setMul(const Mtx34& m, const Vector3& a);
void setRotated(const Mtx33& m, const Vector3& a);
void setRotated(const Mtx34& m, const Vector3& a);
static const Vector3 zero;
static const Vector3 ex;

View File

@ -173,6 +173,18 @@ inline void Vector3<T>::mul(const Mtx34& m)
setMul(m, *this);
}
template <typename T>
inline void Vector3<T>::rotate(const Mtx33& m)
{
setRotated(m, *this);
}
template <typename T>
inline void Vector3<T>::rotate(const Mtx34& m)
{
setRotated(m, *this);
}
template <typename T>
inline void Vector3<T>::multScalar(T t)
{
@ -221,6 +233,18 @@ inline void Vector3<T>::setMul(const Mtx34& m, const Vector3<T>& a)
Vector3CalcCommon<T>::mul(*this, m, a);
}
template <typename T>
inline void Vector3<T>::setRotated(const Mtx33& m, const Vector3<T>& a)
{
Vector3CalcCommon<T>::rotate(*this, m, a);
}
template <typename T>
inline void Vector3<T>::setRotated(const Mtx34& m, const Vector3<T>& a)
{
Vector3CalcCommon<T>::rotate(*this, m, a);
}
template <typename T>
inline Vector4<T>::Vector4(T x_, T y_, T z_, T w_)
{

View File

@ -28,10 +28,16 @@ public:
static void add(Base& o, const Base& a, const Base& b);
static void sub(Base& o, const Base& a, const Base& b);
/// Apply a rotation `m` to the vector `a`.
static void mul(Base& o, const Mtx33& m, const Base& a);
/// Apply a transformation `m` (rotation + translation) to the vector `a`.
/// Apply a transformation `m` (rotation then translation) to the vector `a`.
static void mul(Base& o, const Mtx34& m, const Base& a);
/// Apply a rotation `m` to the vector `a`.
static void rotate(Base& o, const Mtx33& m, const Base& a);
/// Apply a rotation `m` to the vector `a`.
static void rotate(Base& o, const Mtx34& m, const Base& a);
static void cross(Base& o, const Base& a, const Base& b);
static T dot(const Base& a, const Base& b);
static T squaredLength(const Base& v);

View File

@ -95,6 +95,21 @@ inline void Vector3CalcCommon<T>::mul(Base& o, const Mtx34& m, const Base& a)
o.z = m.m[2][0] * tmp.x + m.m[2][1] * tmp.y + m.m[2][2] * tmp.z + m.m[2][3];
}
template <typename T>
inline void Vector3CalcCommon<T>::rotate(Base& o, const Mtx33& m, const Base& a)
{
mul(o, m, a);
}
template <typename T>
inline void Vector3CalcCommon<T>::rotate(Base& o, const Mtx34& m, const Base& a)
{
const Base tmp = a;
o.x = m.m[0][0] * tmp.x + m.m[0][1] * tmp.y + m.m[0][2] * tmp.z;
o.y = m.m[1][0] * tmp.x + m.m[1][1] * tmp.y + m.m[1][2] * tmp.z;
o.z = m.m[2][0] * tmp.x + m.m[2][1] * tmp.y + m.m[2][2] * tmp.z;
}
template <typename T>
inline void Vector3CalcCommon<T>::cross(Base& o, const Base& a, const Base& b)
{