From fe55a7633519d4e2e160d0775deca4ad1dab6c8d Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 21 Mar 2012 10:43:48 +1100 Subject: [PATCH] Math: moved matrix multiple operations to .cpp file this means we only link this in once, rather than for every use of matrix multiply, which saves us some flash space We need to be careful not to put large pieces of code in template headers, as if the operation is used a lot, it costs us a lot of code space --- libraries/AP_Math/matrix3.cpp | 29 +++++++++++++++++++++++++++++ libraries/AP_Math/matrix3.h | 22 +++------------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/libraries/AP_Math/matrix3.cpp b/libraries/AP_Math/matrix3.cpp index d819a37263..236a4263ff 100644 --- a/libraries/AP_Math/matrix3.cpp +++ b/libraries/AP_Math/matrix3.cpp @@ -153,8 +153,37 @@ void Matrix3::rotate(const Vector3 &g) (*this) += temp_matrix; } + +// multiplication by a vector +template +Vector3 Matrix3::operator *(const Vector3 &v) const +{ + return Vector3(a.x * v.x + a.y * v.y + a.z * v.z, + b.x * v.x + b.y * v.y + b.z * v.z, + c.x * v.x + c.y * v.y + c.z * v.z); +} + +// multiplication by another Matrix3 +template +Matrix3 Matrix3::operator *(const Matrix3 &m) const +{ + Matrix3 temp (Vector3(a.x * m.a.x + a.y * m.b.x + a.z * m.c.x, + a.x * m.a.y + a.y * m.b.y + a.z * m.c.y, + a.x * m.a.z + a.y * m.b.z + a.z * m.c.z), + Vector3(b.x * m.a.x + b.y * m.b.x + b.z * m.c.x, + b.x * m.a.y + b.y * m.b.y + b.z * m.c.y, + b.x * m.a.z + b.y * m.b.z + b.z * m.c.z), + Vector3(c.x * m.a.x + c.y * m.b.x + c.z * m.c.x, + c.x * m.a.y + c.y * m.b.y + c.z * m.c.y, + c.x * m.a.z + c.y * m.b.z + c.z * m.c.z)); + return temp; +} + + // only define for float template void Matrix3::rotation(enum Rotation); template void Matrix3::rotate(const Vector3 &g); template void Matrix3::from_euler(float roll, float pitch, float yaw); template void Matrix3::to_euler(float *roll, float *pitch, float *yaw); +template Vector3 Matrix3::operator *(const Vector3 &v) const; +template Matrix3 Matrix3::operator *(const Matrix3 &m) const; diff --git a/libraries/AP_Math/matrix3.h b/libraries/AP_Math/matrix3.h index db5cbe088e..f11b6153bf 100644 --- a/libraries/AP_Math/matrix3.h +++ b/libraries/AP_Math/matrix3.h @@ -90,27 +90,11 @@ public: { return *this = *this / num; } // multiplication by a vector - Vector3 operator *(const Vector3 &v) const - { - return Vector3(a.x * v.x + a.y * v.y + a.z * v.z, - b.x * v.x + b.y * v.y + b.z * v.z, - c.x * v.x + c.y * v.y + c.z * v.z); - } + Vector3 operator *(const Vector3 &v) const; // multiplication by another Matrix3 - Matrix3 operator *(const Matrix3 &m) const - { - Matrix3 temp (Vector3(a.x * m.a.x + a.y * m.b.x + a.z * m.c.x, - a.x * m.a.y + a.y * m.b.y + a.z * m.c.y, - a.x * m.a.z + a.y * m.b.z + a.z * m.c.z), - Vector3(b.x * m.a.x + b.y * m.b.x + b.z * m.c.x, - b.x * m.a.y + b.y * m.b.y + b.z * m.c.y, - b.x * m.a.z + b.y * m.b.z + b.z * m.c.z), - Vector3(c.x * m.a.x + c.y * m.b.x + c.z * m.c.x, - c.x * m.a.y + c.y * m.b.y + c.z * m.c.y, - c.x * m.a.z + c.y * m.b.z + c.z * m.c.z)); - return temp; - } + Matrix3 operator *(const Matrix3 &m) const; + Matrix3 &operator *=(const Matrix3 &m) { return *this = *this * m; }