2010-09-08 05:21:46 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
|
|
|
|
// Copyright 2010 Michael Smith, all rights reserved.
|
|
|
|
|
|
|
|
// This library is free software; you can redistribute it and / or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
// Inspired by:
|
|
|
|
/****************************************
|
|
|
|
* 3D Vector Classes
|
|
|
|
* By Bill Perone (billperone@yahoo.com)
|
|
|
|
*/
|
|
|
|
|
2010-09-08 05:41:29 -03:00
|
|
|
//
|
|
|
|
// 3x3 matrix implementation.
|
|
|
|
//
|
|
|
|
// Note that the matrix is organised in row-normal form (the same as
|
|
|
|
// applies to array indexing).
|
|
|
|
//
|
|
|
|
// In addition to the template, this header defines the following types:
|
|
|
|
//
|
|
|
|
// Matrix3i 3x3 matrix of signed integers
|
|
|
|
// Matrix3ui 3x3 matrix of unsigned integers
|
|
|
|
// Matrix3l 3x3 matrix of signed longs
|
|
|
|
// Matrix3ul 3x3 matrix of unsigned longs
|
|
|
|
// Matrix3f 3x3 matrix of signed floats
|
|
|
|
//
|
|
|
|
|
2010-09-08 05:21:46 -03:00
|
|
|
#ifndef MATRIX3_H
|
|
|
|
#define MATRIX3_H
|
|
|
|
|
|
|
|
#include "vector3.h"
|
|
|
|
|
|
|
|
// 3x3 matrix with elements of type T
|
|
|
|
template <typename T>
|
|
|
|
class Matrix3 {
|
|
|
|
public:
|
|
|
|
|
|
|
|
// Vectors comprising the rows of the matrix
|
|
|
|
Vector3<T> a, b, c;
|
|
|
|
|
|
|
|
// trivial ctor
|
2010-12-19 21:52:23 -04:00
|
|
|
// note that the Vector3 ctor will zero the vector elements
|
2010-09-08 05:21:46 -03:00
|
|
|
Matrix3<T>() {}
|
|
|
|
|
|
|
|
// setting ctor
|
|
|
|
Matrix3<T>(const Vector3<T> a0, const Vector3<T> b0, const Vector3<T> c0): a(a0), b(b0), c(c0) {}
|
|
|
|
|
2010-09-24 23:43:11 -03:00
|
|
|
// setting ctor
|
2011-02-14 00:26:05 -04:00
|
|
|
Matrix3<T>(const T ax, const T ay, const T az, const T bx, const T by, const T bz, const T cx, const T cy, const T cz): a(ax,ay,az), b(bx,by,bz), c(cx,cy,cz) {}
|
|
|
|
|
2010-09-08 05:21:46 -03:00
|
|
|
// function call operator
|
|
|
|
void operator () (const Vector3<T> a0, const Vector3<T> b0, const Vector3<T> c0)
|
|
|
|
{ a = a0; b = b0; c = c0; }
|
|
|
|
|
|
|
|
// test for equality
|
|
|
|
bool operator == (const Matrix3<T> &m)
|
|
|
|
{ return (a==m.a && b==m.b && c==m.c); }
|
|
|
|
|
|
|
|
// test for inequality
|
|
|
|
bool operator != (const Matrix3<T> &m)
|
|
|
|
{ return (a!=m.a || b!=m.b || c!=m.c); }
|
|
|
|
|
|
|
|
// negation
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> operator - (void) const
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return Matrix3<T>(-a,-b,-c); }
|
|
|
|
|
|
|
|
// addition
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> operator + (const Matrix3<T> &m) const
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return Matrix3<T>(a+m.a, b+m.b, c+m.c); }
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> &operator += (const Matrix3<T> &m)
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return *this = *this + m; }
|
|
|
|
|
|
|
|
// subtraction
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> operator - (const Matrix3<T> &m) const
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return Matrix3<T>(a-m.a, b-m.b, c-m.c); }
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> &operator -= (const Matrix3<T> &m)
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return *this = *this - m; }
|
2011-02-14 00:26:05 -04:00
|
|
|
|
2010-09-08 05:21:46 -03:00
|
|
|
// uniform scaling
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> operator * (const T num) const
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return Matrix3<T>(a*num, b*num, c*num); }
|
2010-09-23 00:14:15 -03:00
|
|
|
Matrix3<T> &operator *= (const T num)
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return *this = *this * num; }
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> operator / (const T num) const
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return Matrix3<T>(a/num, b/num, c/num); }
|
2010-09-23 00:14:15 -03:00
|
|
|
Matrix3<T> &operator /= (const T num)
|
2010-09-08 05:21:46 -03:00
|
|
|
{ return *this = *this / num; }
|
2011-02-14 00:26:05 -04:00
|
|
|
|
2010-09-08 05:21:46 -03:00
|
|
|
// multiplication by a vector
|
2012-03-20 20:43:48 -03:00
|
|
|
Vector3<T> operator *(const Vector3<T> &v) const;
|
2010-09-08 05:21:46 -03:00
|
|
|
|
2012-03-23 00:58:54 -03:00
|
|
|
// multiplication of transpose by a vector
|
|
|
|
Vector3<T> mul_transpose(const Vector3<T> &v) const;
|
|
|
|
|
2012-08-11 02:54:12 -03:00
|
|
|
// extract x column
|
|
|
|
Vector3<T> colx(void) const
|
|
|
|
{ return Vector3f(a.x, b.x, c.x); }
|
|
|
|
|
|
|
|
// extract y column
|
|
|
|
Vector3<T> coly(void) const
|
|
|
|
{ return Vector3f(a.y, b.y, c.y); }
|
|
|
|
|
|
|
|
// extract z column
|
|
|
|
Vector3<T> colz(void) const
|
|
|
|
{ return Vector3f(a.z, b.z, c.z); }
|
|
|
|
|
2010-09-08 05:41:29 -03:00
|
|
|
// multiplication by another Matrix3<T>
|
2012-03-20 20:43:48 -03:00
|
|
|
Matrix3<T> operator *(const Matrix3<T> &m) const;
|
|
|
|
|
2010-09-23 00:14:15 -03:00
|
|
|
Matrix3<T> &operator *=(const Matrix3<T> &m)
|
|
|
|
{ return *this = *this * m; }
|
2010-09-08 05:21:46 -03:00
|
|
|
|
2010-09-08 05:41:29 -03:00
|
|
|
// transpose the matrix
|
2012-03-22 07:12:36 -03:00
|
|
|
Matrix3<T> transposed(void) const;
|
|
|
|
|
2010-09-22 14:41:49 -03:00
|
|
|
Matrix3<T> transpose(void)
|
2010-09-08 05:41:29 -03:00
|
|
|
{ return *this = transposed(); }
|
2010-09-08 05:21:46 -03:00
|
|
|
|
2012-03-19 03:21:08 -03:00
|
|
|
// zero the matrix
|
2012-03-22 07:12:36 -03:00
|
|
|
void zero(void);
|
2012-03-19 03:21:08 -03:00
|
|
|
|
|
|
|
// setup the identity matrix
|
|
|
|
void identity(void) {
|
|
|
|
a.x = b.y = c.z = 1;
|
|
|
|
a.y = a.z = 0;
|
|
|
|
b.x = b.z = 0;
|
|
|
|
c.x = c.y = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-23 07:55:59 -04:00
|
|
|
// check if any elements are NAN
|
|
|
|
bool is_nan(void)
|
|
|
|
{ return a.is_nan() || b.is_nan() || c.is_nan(); }
|
|
|
|
|
2012-03-09 22:44:36 -04:00
|
|
|
// fill in the matrix with a standard rotation
|
|
|
|
void rotation(enum Rotation rotation);
|
|
|
|
|
2012-03-10 02:06:35 -04:00
|
|
|
// create a rotation matrix from Euler angles
|
|
|
|
void from_euler(float roll, float pitch, float yaw);
|
|
|
|
|
|
|
|
// create eulers from a rotation matrix
|
|
|
|
void to_euler(float *roll, float *pitch, float *yaw);
|
2012-03-19 03:22:11 -03:00
|
|
|
|
|
|
|
// apply an additional rotation from a body frame gyro vector
|
|
|
|
// to a rotation matrix.
|
|
|
|
void rotate(const Vector3<T> &g);
|
2010-09-08 05:21:46 -03:00
|
|
|
};
|
|
|
|
|
2011-12-15 22:47:07 -04:00
|
|
|
typedef Matrix3<int16_t> Matrix3i;
|
|
|
|
typedef Matrix3<uint16_t> Matrix3ui;
|
|
|
|
typedef Matrix3<int32_t> Matrix3l;
|
|
|
|
typedef Matrix3<uint32_t> Matrix3ul;
|
2010-09-23 02:45:43 -03:00
|
|
|
typedef Matrix3<float> Matrix3f;
|
2010-09-08 05:21:46 -03:00
|
|
|
|
|
|
|
#endif // MATRIX3_H
|