2004-02-19 15:42:05 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* VCGLib o o *
|
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
|
* _ O _ *
|
|
|
|
|
* Copyright(C) 2004 \/)\/ *
|
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
|
* \ *
|
|
|
|
|
* All rights reserved. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
|
* for more details. *
|
|
|
|
|
* *
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
History
|
|
|
|
|
|
2004-03-25 15:57:49 +01:00
|
|
|
|
$Log: not supported by cvs2svn $
|
2007-03-08 15:39:27 +01:00
|
|
|
|
Revision 1.31 2007/02/06 09:57:40 corsini
|
|
|
|
|
fix euler angles computation
|
|
|
|
|
|
2007-02-06 10:57:40 +01:00
|
|
|
|
Revision 1.30 2007/02/05 14:16:33 corsini
|
|
|
|
|
add from euler angles to rotation matrix conversion
|
|
|
|
|
|
2007-02-05 15:16:33 +01:00
|
|
|
|
Revision 1.29 2005/12/02 09:46:49 croccia
|
|
|
|
|
Corrected bug in == and != Matrix44 operators
|
|
|
|
|
|
2005-12-02 10:46:49 +01:00
|
|
|
|
Revision 1.28 2005/06/28 17:42:47 ganovelli
|
|
|
|
|
added Matrix44Diag
|
|
|
|
|
|
2005-06-28 19:42:47 +02:00
|
|
|
|
Revision 1.27 2005/06/17 05:28:47 cignoni
|
|
|
|
|
Completed Shear Matrix code and comments,
|
|
|
|
|
Added use of swap inside Transpose
|
|
|
|
|
Added more complete comments on the usage of Decompose
|
|
|
|
|
|
2005-06-17 07:28:47 +02:00
|
|
|
|
Revision 1.26 2005/06/10 15:04:12 cignoni
|
|
|
|
|
Added Various missing functions: SetShearXY, SetShearXZ, SetShearYZ, SetScale for point3 and Decompose
|
|
|
|
|
Completed *=(scalar); made uniform GetRow and GetColumn
|
|
|
|
|
|
2005-06-10 17:04:12 +02:00
|
|
|
|
Revision 1.25 2005/04/14 11:35:09 ponchio
|
|
|
|
|
*** empty log message ***
|
|
|
|
|
|
2005-04-14 13:35:09 +02:00
|
|
|
|
Revision 1.24 2005/03/18 00:14:39 cignoni
|
|
|
|
|
removed small gcc compiling issues
|
|
|
|
|
|
2005-03-18 01:14:40 +01:00
|
|
|
|
Revision 1.23 2005/03/15 11:40:56 cignoni
|
|
|
|
|
Added operator*=( std::vector<PointType> ...) to apply a matrix to a vector of vertexes (replacement of the old style mesh.Apply(tr).
|
|
|
|
|
|
2005-03-15 12:40:56 +01:00
|
|
|
|
Revision 1.22 2004/12/15 18:45:50 tommyfranken
|
|
|
|
|
*** empty log message ***
|
|
|
|
|
|
2004-12-15 19:45:50 +01:00
|
|
|
|
Revision 1.21 2004/10/22 14:41:30 ponchio
|
|
|
|
|
return in operator+ added.
|
|
|
|
|
|
2004-10-22 16:41:30 +02:00
|
|
|
|
Revision 1.20 2004/10/18 15:03:14 fiorin
|
|
|
|
|
Updated interface: all Matrix classes have now the same interface
|
|
|
|
|
|
2004-10-18 17:03:14 +02:00
|
|
|
|
Revision 1.19 2004/10/07 14:23:57 ganovelli
|
|
|
|
|
added function to take rows and comlumns. Added toMatrix and fromMatrix to comply
|
|
|
|
|
RotationTYpe prototype in Similarity.h
|
|
|
|
|
|
2004-10-07 16:23:57 +02:00
|
|
|
|
Revision 1.18 2004/05/28 13:01:50 ganovelli
|
|
|
|
|
changed scalar to ScalarType
|
|
|
|
|
|
2004-05-28 15:01:50 +02:00
|
|
|
|
Revision 1.17 2004/05/26 15:09:32 cignoni
|
|
|
|
|
better comments in set rotate
|
|
|
|
|
|
2004-05-26 17:09:32 +02:00
|
|
|
|
Revision 1.16 2004/05/07 10:05:50 cignoni
|
|
|
|
|
Corrected abuse of for index variable scope
|
|
|
|
|
|
2004-05-07 12:05:50 +02:00
|
|
|
|
Revision 1.15 2004/05/04 23:19:41 cignoni
|
|
|
|
|
Clarified initial comment, removed vector*matrix operator (confusing!)
|
|
|
|
|
Corrected translate and Rotate, removed gl stuff.
|
|
|
|
|
|
2004-05-05 01:19:41 +02:00
|
|
|
|
Revision 1.14 2004/05/04 02:34:03 ganovelli
|
|
|
|
|
wrong use of operator [] corrected
|
|
|
|
|
|
2004-05-04 04:34:03 +02:00
|
|
|
|
Revision 1.13 2004/04/07 10:45:54 cignoni
|
|
|
|
|
Added: [i][j] access, V() for the raw float values, constructor from T[16]
|
|
|
|
|
|
2004-04-07 12:45:54 +02:00
|
|
|
|
Revision 1.12 2004/03/25 14:57:49 ponchio
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef __VCGLIB_MATRIX44
|
|
|
|
|
#define __VCGLIB_MATRIX44
|
|
|
|
|
|
2004-05-05 01:19:41 +02:00
|
|
|
|
#include <memory.h>
|
2004-03-08 16:33:58 +01:00
|
|
|
|
#include <vcg/math/base.h>
|
2004-02-19 16:28:01 +01:00
|
|
|
|
#include <vcg/space/point3.h>
|
|
|
|
|
#include <vcg/space/point4.h>
|
2005-03-15 12:40:56 +01:00
|
|
|
|
#include <vector>
|
2004-02-19 15:58:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace vcg {
|
|
|
|
|
|
|
|
|
|
/*
|
2004-02-19 15:42:05 +01:00
|
|
|
|
Annotations:
|
|
|
|
|
Opengl stores matrix in column-major order. That is, the matrix is stored as:
|
|
|
|
|
|
|
|
|
|
a0 a4 a8 a12
|
|
|
|
|
a1 a5 a9 a13
|
|
|
|
|
a2 a6 a10 a14
|
|
|
|
|
a3 a7 a11 a15
|
|
|
|
|
|
2004-05-05 01:19:41 +02:00
|
|
|
|
Usually in opengl (see opengl specs) vectors are 'column' vectors
|
|
|
|
|
so usually matrix are PRE-multiplied for a vector.
|
|
|
|
|
So the command glTranslate generate a matrix that
|
|
|
|
|
is ready to be premultipled for a vector:
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
1 0 0 tx
|
|
|
|
|
0 1 0 ty
|
|
|
|
|
0 0 1 tz
|
|
|
|
|
0 0 0 1
|
|
|
|
|
|
|
|
|
|
Matrix44 stores matrix in row-major order i.e.
|
|
|
|
|
|
|
|
|
|
a0 a1 a2 a3
|
|
|
|
|
a4 a5 a6 a7
|
|
|
|
|
a8 a9 a10 a11
|
|
|
|
|
a12 a13 a14 a15
|
|
|
|
|
|
2004-05-05 01:19:41 +02:00
|
|
|
|
So for the use of that matrix in opengl with their supposed meaning you have to transpose them before feeding to glMultMatrix.
|
|
|
|
|
This mechanism is hidden by the templated function defined in wrap/gl/math.h;
|
|
|
|
|
If your machine has the ARB_transpose_matrix extension it will use the appropriate;
|
|
|
|
|
The various gl-like command SetRotate, SetTranslate assume that you are making matrix
|
|
|
|
|
for 'column' vectors.
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2005-06-28 19:42:47 +02:00
|
|
|
|
template <class S>
|
|
|
|
|
class Matrix44Diag:public Point4<S>{
|
|
|
|
|
public:
|
|
|
|
|
/** @name Matrix33
|
|
|
|
|
Class Matrix33Diag.
|
|
|
|
|
This is the class for definition of a diagonal matrix 4x4.
|
|
|
|
|
@param S (Templete Parameter) Specifies the ScalarType field.
|
|
|
|
|
*/
|
|
|
|
|
Matrix44Diag(const S & p0,const S & p1,const S & p2,const S & p3):Point4<S>(p0,p1,p2,p3){};
|
|
|
|
|
Matrix44Diag( const Point4<S> & p ):Point4<S>(p){};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 15:58:23 +01:00
|
|
|
|
/** This class represent a 4x4 matrix. T is the kind of element in the matrix.
|
|
|
|
|
*/
|
2004-02-19 15:42:05 +01:00
|
|
|
|
template <class T> class Matrix44 {
|
|
|
|
|
protected:
|
|
|
|
|
T _a[16];
|
|
|
|
|
|
|
|
|
|
public:
|
2004-05-28 15:01:50 +02:00
|
|
|
|
typedef T ScalarType;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
2004-02-19 15:58:23 +01:00
|
|
|
|
///@{
|
|
|
|
|
|
2005-06-10 17:04:12 +02:00
|
|
|
|
/** $name Constructors
|
2004-02-19 15:58:23 +01:00
|
|
|
|
* No automatic casting and default constructor is empty
|
|
|
|
|
*/
|
2004-02-19 15:42:05 +01:00
|
|
|
|
Matrix44() {};
|
|
|
|
|
~Matrix44() {};
|
|
|
|
|
Matrix44(const Matrix44 &m);
|
|
|
|
|
Matrix44(const T v[]);
|
|
|
|
|
|
2004-10-18 17:03:14 +02:00
|
|
|
|
/// Number of columns
|
|
|
|
|
inline unsigned int ColumnsNumber() const
|
|
|
|
|
{
|
|
|
|
|
return 4;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Number of rows
|
|
|
|
|
inline unsigned int RowsNumber() const
|
|
|
|
|
{
|
|
|
|
|
return 4;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
T &ElementAt(const int row, const int col);
|
|
|
|
|
T ElementAt(const int row, const int col) const;
|
2004-04-07 12:45:54 +02:00
|
|
|
|
//T &operator[](const int i);
|
|
|
|
|
//const T &operator[](const int i) const;
|
|
|
|
|
T *V();
|
|
|
|
|
const T *V() const ;
|
|
|
|
|
|
|
|
|
|
T *operator[](const int i);
|
|
|
|
|
const T *operator[](const int i) const;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
2004-10-07 16:23:57 +02:00
|
|
|
|
// return a copy of the i-th column
|
2005-06-10 17:04:12 +02:00
|
|
|
|
Point4<T> GetColumn4(const int& i)const{
|
|
|
|
|
assert(i>=0 && i<4);
|
|
|
|
|
return Point4<T>(ElementAt(0,i),ElementAt(1,i),ElementAt(2,i),ElementAt(3,i));
|
|
|
|
|
//return Point4<T>(_a[i],_a[i+4],_a[i+8],_a[i+12]);
|
2004-10-07 16:23:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-06-10 17:04:12 +02:00
|
|
|
|
Point3<T> GetColumn3(const int& i)const{
|
|
|
|
|
assert(i>=0 && i<4);
|
|
|
|
|
return Point3<T>(ElementAt(0,i),ElementAt(1,i),ElementAt(2,i));
|
2004-10-07 16:23:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-06-10 17:04:12 +02:00
|
|
|
|
Point4<T> GetRow4(const int& i)const{
|
|
|
|
|
assert(i>=0 && i<4);
|
|
|
|
|
return Point4<T>(ElementAt(i,0),ElementAt(i,1),ElementAt(i,2),ElementAt(i,3));
|
|
|
|
|
// return *((Point4<T>*)(&_a[i<<2])); alternativa forse + efficiente
|
2004-10-07 16:23:57 +02:00
|
|
|
|
}
|
2005-06-10 17:04:12 +02:00
|
|
|
|
|
|
|
|
|
Point3<T> GetRow3(const int& i)const{
|
|
|
|
|
assert(i>=0 && i<4);
|
|
|
|
|
return Point3<T>(ElementAt(i,0),ElementAt(i,1),ElementAt(i,2));
|
|
|
|
|
// return *((Point4<T>*)(&_a[i<<2])); alternativa forse + efficiente
|
2004-10-07 16:23:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
Matrix44 operator+(const Matrix44 &m) const;
|
|
|
|
|
Matrix44 operator-(const Matrix44 &m) const;
|
|
|
|
|
Matrix44 operator*(const Matrix44 &m) const;
|
2005-06-28 19:42:47 +02:00
|
|
|
|
Matrix44 operator*(const Matrix44Diag<T> &m) const;
|
2004-03-04 01:45:51 +01:00
|
|
|
|
Point4<T> operator*(const Point4<T> &v) const;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
bool operator==(const Matrix44 &m) const;
|
|
|
|
|
bool operator!= (const Matrix44 &m) const;
|
|
|
|
|
|
|
|
|
|
Matrix44 operator-() const;
|
|
|
|
|
Matrix44 operator*(const T k) const;
|
|
|
|
|
void operator+=(const Matrix44 &m);
|
|
|
|
|
void operator-=(const Matrix44 &m);
|
|
|
|
|
void operator*=( const Matrix44 & m );
|
|
|
|
|
void operator*=( const T k );
|
2004-12-15 19:45:50 +01:00
|
|
|
|
|
|
|
|
|
template <class Matrix44Type>
|
|
|
|
|
void ToMatrix(Matrix44Type & m) const {for(int i = 0; i < 16; i++) m.V()[i]=V()[i];}
|
|
|
|
|
template <class Matrix44Type>
|
|
|
|
|
void FromMatrix(const Matrix44Type & m){for(int i = 0; i < 16; i++) V()[i]=m.V()[i];}
|
2007-02-05 15:16:33 +01:00
|
|
|
|
void FromEulerAngles(T alpha, T beta, T gamma);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
void SetZero();
|
|
|
|
|
void SetIdentity();
|
|
|
|
|
void SetDiagonal(const T k);
|
2005-06-10 17:04:12 +02:00
|
|
|
|
Matrix44 &SetScale(const T sx, const T sy, const T sz);
|
|
|
|
|
Matrix44 &SetScale(const Point3<T> &t);
|
|
|
|
|
Matrix44 &SetTranslate(const Point3<T> &t);
|
2004-03-04 03:10:14 +01:00
|
|
|
|
Matrix44 &SetTranslate(const T sx, const T sy, const T sz);
|
2005-06-10 17:04:12 +02:00
|
|
|
|
Matrix44 &SetShearXY(const T sz);
|
|
|
|
|
Matrix44 &SetShearXZ(const T sy);
|
|
|
|
|
Matrix44 &SetShearYZ(const T sx);
|
|
|
|
|
|
2004-02-19 15:58:23 +01:00
|
|
|
|
///use radiants for angle.
|
2004-05-26 17:09:32 +02:00
|
|
|
|
Matrix44 &SetRotate(T AngleRad, const Point3<T> & axis);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
T Determinant() const;
|
2004-03-06 16:46:43 +01:00
|
|
|
|
|
|
|
|
|
template <class Q> void Import(const Matrix44<Q> &m) {
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
2004-04-07 12:45:54 +02:00
|
|
|
|
_a[i] = (T)(m.V()[i]);
|
2004-03-06 16:46:43 +01:00
|
|
|
|
}
|
2004-02-19 15:42:05 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 15:58:23 +01:00
|
|
|
|
/** Class for solving A * x = b. */
|
2004-02-19 15:42:05 +01:00
|
|
|
|
template <class T> class LinearSolve: public Matrix44<T> {
|
|
|
|
|
public:
|
|
|
|
|
LinearSolve(const Matrix44<T> &m);
|
2004-02-19 16:54:11 +01:00
|
|
|
|
Point4<T> Solve(const Point4<T> &b); // solve A <20> x = b
|
2004-02-19 15:58:23 +01:00
|
|
|
|
///If you need to solve some equation you can use this function instead of Matrix44 one for speed.
|
2004-02-19 15:42:05 +01:00
|
|
|
|
T Determinant() const;
|
|
|
|
|
protected:
|
2004-02-19 15:58:23 +01:00
|
|
|
|
///Holds row permutation.
|
2004-02-19 15:42:05 +01:00
|
|
|
|
int index[4]; //hold permutation
|
2004-02-19 15:58:23 +01:00
|
|
|
|
///Hold sign of row permutation (used for determinant sign)
|
|
|
|
|
T d;
|
2004-03-08 16:33:58 +01:00
|
|
|
|
bool Decompose();
|
2004-02-19 15:42:05 +01:00
|
|
|
|
};
|
|
|
|
|
|
2004-05-05 01:19:41 +02:00
|
|
|
|
/*** Postmultiply */
|
|
|
|
|
//template <class T> Point3<T> operator*(const Point3<T> &p, const Matrix44<T> &m);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
2004-05-05 01:19:41 +02:00
|
|
|
|
///Premultiply
|
2004-03-04 03:10:14 +01:00
|
|
|
|
template <class T> Point3<T> operator*(const Matrix44<T> &m, const Point3<T> &p);
|
2004-02-19 15:58:23 +01:00
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
template <class T> Matrix44<T> &Transpose(Matrix44<T> &m);
|
2004-03-08 16:33:58 +01:00
|
|
|
|
//return NULL matrix if not invertible
|
|
|
|
|
template <class T> Matrix44<T> &Invert(Matrix44<T> &m);
|
2004-03-09 14:57:29 +01:00
|
|
|
|
template <class T> Matrix44<T> Inverse(const Matrix44<T> &m);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
typedef Matrix44<short> Matrix44s;
|
2004-03-09 14:57:29 +01:00
|
|
|
|
typedef Matrix44<int> Matrix44i;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
typedef Matrix44<float> Matrix44f;
|
|
|
|
|
typedef Matrix44<double> Matrix44d;
|
|
|
|
|
|
2004-02-19 15:58:23 +01:00
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T>::Matrix44(const Matrix44<T> &m) {
|
|
|
|
|
memcpy((T *)_a, (T *)m._a, 16 * sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-07 12:45:54 +02:00
|
|
|
|
template <class T> Matrix44<T>::Matrix44(const T v[]) {
|
|
|
|
|
memcpy((T *)_a, v, 16 * sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-18 17:03:14 +02:00
|
|
|
|
template <class T> T &Matrix44<T>::ElementAt(const int row, const int col) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
assert(row >= 0 && row < 4);
|
|
|
|
|
assert(col >= 0 && col < 4);
|
|
|
|
|
return _a[(row<<2) + col];
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-18 17:03:14 +02:00
|
|
|
|
template <class T> T Matrix44<T>::ElementAt(const int row, const int col) const {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
assert(row >= 0 && row < 4);
|
|
|
|
|
assert(col >= 0 && col < 4);
|
|
|
|
|
return _a[(row<<2) + col];
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-07 12:45:54 +02:00
|
|
|
|
//template <class T> T &Matrix44<T>::operator[](const int i) {
|
|
|
|
|
// assert(i >= 0 && i < 16);
|
|
|
|
|
// return ((T *)_a)[i];
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//template <class T> const T &Matrix44<T>::operator[](const int i) const {
|
|
|
|
|
// assert(i >= 0 && i < 16);
|
|
|
|
|
// return ((T *)_a)[i];
|
2005-06-10 17:04:12 +02:00
|
|
|
|
//}
|
2004-04-07 12:45:54 +02:00
|
|
|
|
template <class T> T *Matrix44<T>::operator[](const int i) {
|
2005-12-02 10:46:49 +01:00
|
|
|
|
assert(i >= 0 && i < 4);
|
2004-04-07 12:45:54 +02:00
|
|
|
|
return _a+i*4;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-07 12:45:54 +02:00
|
|
|
|
template <class T> const T *Matrix44<T>::operator[](const int i) const {
|
|
|
|
|
assert(i >= 0 && i < 4);
|
|
|
|
|
return _a+i*4;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
2004-04-07 12:45:54 +02:00
|
|
|
|
template <class T> T *Matrix44<T>::V() { return _a;}
|
|
|
|
|
template <class T> const T *Matrix44<T>::V() const { return _a;}
|
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> Matrix44<T>::operator+(const Matrix44 &m) const {
|
|
|
|
|
Matrix44<T> ret;
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
2004-10-22 16:41:30 +02:00
|
|
|
|
ret.V()[i] = V()[i] + m.V()[i];
|
|
|
|
|
return ret;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> Matrix44<T>::operator-(const Matrix44 &m) const {
|
|
|
|
|
Matrix44<T> ret;
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
2004-10-22 16:41:30 +02:00
|
|
|
|
ret.V()[i] = V()[i] - m.V()[i];
|
|
|
|
|
return ret;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> Matrix44<T>::operator*(const Matrix44 &m) const {
|
|
|
|
|
Matrix44 ret;
|
|
|
|
|
for(int i = 0; i < 4; i++)
|
|
|
|
|
for(int j = 0; j < 4; j++) {
|
|
|
|
|
T t = 0.0;
|
|
|
|
|
for(int k = 0; k < 4; k++)
|
2004-10-18 17:03:14 +02:00
|
|
|
|
t += ElementAt(i, k) * m.ElementAt(k, j);
|
|
|
|
|
ret.ElementAt(i, j) = t;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-28 19:42:47 +02:00
|
|
|
|
template <class T> Matrix44<T> Matrix44<T>::operator*(const Matrix44Diag<T> &m) const {
|
|
|
|
|
Matrix44 ret = (*this);
|
|
|
|
|
for(int i = 0; i < 4; ++i)
|
|
|
|
|
for(int j = 0; j < 4; ++j)
|
|
|
|
|
ret[i][j]*=m[i];
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
template <class T> Point4<T> Matrix44<T>::operator*(const Point4<T> &v) const {
|
|
|
|
|
Point4<T> ret;
|
|
|
|
|
for(int i = 0; i < 4; i++){
|
|
|
|
|
T t = 0.0;
|
|
|
|
|
for(int k = 0; k < 4; k++)
|
2004-10-18 17:03:14 +02:00
|
|
|
|
t += ElementAt(i,k) * v[k];
|
2004-02-19 15:42:05 +01:00
|
|
|
|
ret[i] = t;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> bool Matrix44<T>::operator==(const Matrix44 &m) const {
|
2005-12-02 10:46:49 +01:00
|
|
|
|
for(int i = 0; i < 4; ++i)
|
|
|
|
|
for(int j = 0; j < 4; ++j)
|
|
|
|
|
if(ElementAt(i,j) != m.ElementAt(i,j))
|
2004-02-19 15:42:05 +01:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
template <class T> bool Matrix44<T>::operator!=(const Matrix44 &m) const {
|
2005-12-02 10:46:49 +01:00
|
|
|
|
for(int i = 0; i < 4; ++i)
|
|
|
|
|
for(int j = 0; j < 4; ++j)
|
|
|
|
|
if(ElementAt(i,j) != m.ElementAt(i,j))
|
2004-02-19 15:42:05 +01:00
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> Matrix44<T>::operator-() const {
|
|
|
|
|
Matrix44<T> res;
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
2004-05-04 04:34:03 +02:00
|
|
|
|
res.V()[i] = -V()[i];
|
2004-02-19 15:42:05 +01:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> Matrix44<T>::operator*(const T k) const {
|
|
|
|
|
Matrix44<T> res;
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
2004-05-04 04:34:03 +02:00
|
|
|
|
res.V()[i] =V()[i] * k;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> void Matrix44<T>::operator+=(const Matrix44 &m) {
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
2004-05-04 04:34:03 +02:00
|
|
|
|
V()[i] += m.V()[i];
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
template <class T> void Matrix44<T>::operator-=(const Matrix44 &m) {
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
2004-05-04 04:34:03 +02:00
|
|
|
|
V()[i] -= m.V()[i];
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
template <class T> void Matrix44<T>::operator*=( const Matrix44 & m ) {
|
2004-03-04 03:10:14 +01:00
|
|
|
|
*this = *this *m;
|
|
|
|
|
|
|
|
|
|
/*for(int i = 0; i < 4; i++) { //sbagliato
|
2004-02-19 15:42:05 +01:00
|
|
|
|
Point4<T> t(0, 0, 0, 0);
|
|
|
|
|
for(int k = 0; k < 4; k++) {
|
|
|
|
|
for(int j = 0; j < 4; j++) {
|
2004-10-18 17:03:14 +02:00
|
|
|
|
t[k] += ElementAt(i, k) * m.ElementAt(k, j);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int l = 0; l < 4; l++)
|
2004-10-18 17:03:14 +02:00
|
|
|
|
ElementAt(i, l) = t[l];
|
2004-03-04 03:10:14 +01:00
|
|
|
|
} */
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-15 12:40:56 +01:00
|
|
|
|
template < class PointType , class T > void operator*=( std::vector<PointType> &vert, const Matrix44<T> & m ) {
|
2005-03-18 01:14:40 +01:00
|
|
|
|
typename std::vector<PointType>::iterator ii;
|
2005-03-15 12:40:56 +01:00
|
|
|
|
for(ii=vert.begin();ii!=vert.end();++ii)
|
|
|
|
|
(*ii).P()=m * (*ii).P();
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
template <class T> void Matrix44<T>::operator*=( const T k ) {
|
2005-06-10 17:04:12 +02:00
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
|
_a[i] *= k;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2007-02-05 15:16:33 +01:00
|
|
|
|
template <class T>
|
|
|
|
|
void Matrix44<T>::FromEulerAngles(T alpha, T beta, T gamma)
|
|
|
|
|
{
|
|
|
|
|
this->SetZero();
|
|
|
|
|
|
|
|
|
|
T cosalpha = cos(alpha);
|
|
|
|
|
T cosbeta = cos(beta);
|
|
|
|
|
T cosgamma = cos(gamma);
|
|
|
|
|
T sinalpha = sin(alpha);
|
|
|
|
|
T sinbeta = sin(beta);
|
|
|
|
|
T singamma = sin(gamma);
|
|
|
|
|
|
|
|
|
|
ElementAt(0,0) = cosbeta * cosgamma;
|
2007-03-08 15:39:27 +01:00
|
|
|
|
ElementAt(1,0) = -cosalpha * singamma + sinalpha * sinbeta * cosgamma;
|
|
|
|
|
ElementAt(2,0) = sinalpha * singamma + cosalpha * sinbeta * cosgamma;
|
2007-02-05 15:16:33 +01:00
|
|
|
|
|
2007-03-08 15:39:27 +01:00
|
|
|
|
ElementAt(0,1) = cosbeta * singamma;
|
2007-02-05 15:16:33 +01:00
|
|
|
|
ElementAt(1,1) = cosalpha * cosgamma + sinalpha * sinbeta * singamma;
|
2007-03-08 15:39:27 +01:00
|
|
|
|
ElementAt(2,1) = -sinalpha * cosgamma + cosalpha * sinbeta * singamma;
|
2007-02-05 15:16:33 +01:00
|
|
|
|
|
2007-03-08 15:39:27 +01:00
|
|
|
|
ElementAt(0,2) = -sinbeta;
|
|
|
|
|
ElementAt(1,2) = sinalpha * cosbeta;
|
2007-02-05 15:16:33 +01:00
|
|
|
|
ElementAt(2,2) = cosalpha * cosbeta;
|
|
|
|
|
|
|
|
|
|
ElementAt(3,3) = 1;
|
|
|
|
|
}
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
template <class T> void Matrix44<T>::SetZero() {
|
|
|
|
|
memset((T *)_a, 0, 16 * sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> void Matrix44<T>::SetIdentity() {
|
|
|
|
|
SetDiagonal(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> void Matrix44<T>::SetDiagonal(const T k) {
|
|
|
|
|
SetZero();
|
2004-10-18 17:03:14 +02:00
|
|
|
|
ElementAt(0, 0) = k;
|
|
|
|
|
ElementAt(1, 1) = k;
|
|
|
|
|
ElementAt(2, 2) = k;
|
|
|
|
|
ElementAt(3, 3) = 1;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-06-10 17:04:12 +02:00
|
|
|
|
template <class T> Matrix44<T> &Matrix44<T>::SetScale(const Point3<T> &t) {
|
|
|
|
|
SetScale(t[0], t[1], t[2]);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2004-03-04 03:10:14 +01:00
|
|
|
|
template <class T> Matrix44<T> &Matrix44<T>::SetScale(const T sx, const T sy, const T sz) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
SetZero();
|
2004-10-18 17:03:14 +02:00
|
|
|
|
ElementAt(0, 0) = sx;
|
|
|
|
|
ElementAt(1, 1) = sy;
|
|
|
|
|
ElementAt(2, 2) = sz;
|
|
|
|
|
ElementAt(3, 3) = 1;
|
2004-03-04 03:10:14 +01:00
|
|
|
|
return *this;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-03-04 03:10:14 +01:00
|
|
|
|
template <class T> Matrix44<T> &Matrix44<T>::SetTranslate(const Point3<T> &t) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
SetTranslate(t[0], t[1], t[2]);
|
2004-03-04 03:10:14 +01:00
|
|
|
|
return *this;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
2007-02-05 15:16:33 +01:00
|
|
|
|
template <class T> Matrix44<T> &Matrix44<T>::SetTranslate(const T tx, const T ty, const T tz) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
SetIdentity();
|
2007-02-05 15:16:33 +01:00
|
|
|
|
ElementAt(0, 3) = tx;
|
|
|
|
|
ElementAt(1, 3) = ty;
|
|
|
|
|
ElementAt(2, 3) = tz;
|
2004-03-04 03:10:14 +01:00
|
|
|
|
return *this;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
2004-05-26 17:09:32 +02:00
|
|
|
|
template <class T> Matrix44<T> &Matrix44<T>::SetRotate(T AngleRad, const Point3<T> & axis) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
//angle = angle*(T)3.14159265358979323846/180; e' in radianti!
|
2004-05-26 17:09:32 +02:00
|
|
|
|
T c = math::Cos(AngleRad);
|
|
|
|
|
T s = math::Sin(AngleRad);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
T q = 1-c;
|
|
|
|
|
Point3<T> t = axis;
|
|
|
|
|
t.Normalize();
|
2004-10-18 17:03:14 +02:00
|
|
|
|
ElementAt(0,0) = t[0]*t[0]*q + c;
|
|
|
|
|
ElementAt(0,1) = t[0]*t[1]*q - t[2]*s;
|
|
|
|
|
ElementAt(0,2) = t[0]*t[2]*q + t[1]*s;
|
|
|
|
|
ElementAt(0,3) = 0;
|
|
|
|
|
ElementAt(1,0) = t[1]*t[0]*q + t[2]*s;
|
|
|
|
|
ElementAt(1,1) = t[1]*t[1]*q + c;
|
|
|
|
|
ElementAt(1,2) = t[1]*t[2]*q - t[0]*s;
|
|
|
|
|
ElementAt(1,3) = 0;
|
|
|
|
|
ElementAt(2,0) = t[2]*t[0]*q -t[1]*s;
|
|
|
|
|
ElementAt(2,1) = t[2]*t[1]*q +t[0]*s;
|
|
|
|
|
ElementAt(2,2) = t[2]*t[2]*q +c;
|
|
|
|
|
ElementAt(2,3) = 0;
|
|
|
|
|
ElementAt(3,0) = 0;
|
|
|
|
|
ElementAt(3,1) = 0;
|
|
|
|
|
ElementAt(3,2) = 0;
|
|
|
|
|
ElementAt(3,3) = 1;
|
2004-03-04 03:10:14 +01:00
|
|
|
|
return *this;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-06-17 07:28:47 +02:00
|
|
|
|
/* Shear Matrixes
|
|
|
|
|
XY
|
|
|
|
|
1 k 0 0 x x+ky
|
|
|
|
|
0 1 0 0 y y
|
|
|
|
|
0 0 1 0 z z
|
|
|
|
|
0 0 0 1 1 1
|
|
|
|
|
|
|
|
|
|
1 0 k 0 x x+kz
|
|
|
|
|
0 1 0 0 y y
|
|
|
|
|
0 0 1 0 z z
|
|
|
|
|
0 0 0 1 1 1
|
|
|
|
|
|
|
|
|
|
1 1 0 0 x x
|
|
|
|
|
0 1 k 0 y y+kz
|
|
|
|
|
0 0 1 0 z z
|
|
|
|
|
0 0 0 1 1 1
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2005-06-10 17:04:12 +02:00
|
|
|
|
template <class T> Matrix44<T> & Matrix44<T>:: SetShearXY( const T sh) {// shear the X coordinate as the Y coordinate change
|
|
|
|
|
SetIdentity();
|
2005-06-17 07:28:47 +02:00
|
|
|
|
ElementAt(0,1) = sh;
|
2005-06-10 17:04:12 +02:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> & Matrix44<T>:: SetShearXZ( const T sh) {// shear the X coordinate as the Z coordinate change
|
|
|
|
|
SetIdentity();
|
2005-06-17 07:28:47 +02:00
|
|
|
|
ElementAt(0,2) = sh;
|
2005-06-10 17:04:12 +02:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> &Matrix44<T>:: SetShearYZ( const T sh) {// shear the Y coordinate as the Z coordinate change
|
|
|
|
|
SetIdentity();
|
2005-06-17 07:28:47 +02:00
|
|
|
|
ElementAt(1,2) = sh;
|
2005-06-10 17:04:12 +02:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-06-17 07:28:47 +02:00
|
|
|
|
Given a non singular, non projective matrix (e.g. with the last row equal to [0,0,0,1] )
|
|
|
|
|
This procedure decompose it in a sequence of
|
2005-06-10 17:04:12 +02:00
|
|
|
|
Scale,Shear,Rotation e Translation
|
|
|
|
|
|
2005-06-17 07:28:47 +02:00
|
|
|
|
- ScaleV and Tranv are obiviously scaling and translation.
|
|
|
|
|
- ShearV contains three scalars with, respectively
|
|
|
|
|
ShearXY, ShearXZ e ShearYZ
|
|
|
|
|
- RotateV contains the rotations (in degree!) around the x,y,z axis
|
|
|
|
|
The input matrix is modified leaving inside it a simple roto translation.
|
2005-06-10 17:04:12 +02:00
|
|
|
|
|
2005-06-17 07:28:47 +02:00
|
|
|
|
To obtain the original matrix the above transformation have to be applied in the strict following way:
|
|
|
|
|
|
|
|
|
|
OriginalMatrix = Trn * Rtx*Rty*Rtz * ShearYZ*ShearXZ*ShearXY * Scl
|
|
|
|
|
|
|
|
|
|
Example Code:
|
|
|
|
|
double srv() { return (double(rand()%40)-20)/2.0; } // small random value
|
|
|
|
|
|
|
|
|
|
srand(time(0));
|
|
|
|
|
Point3d ScV(10+srv(),10+srv(),10+srv()),ScVOut(-1,-1,-1);
|
|
|
|
|
Point3d ShV(srv(),srv(),srv()),ShVOut(-1,-1,-1);
|
|
|
|
|
Point3d RtV(10+srv(),srv(),srv()),RtVOut(-1,-1,-1);
|
|
|
|
|
Point3d TrV(srv(),srv(),srv()),TrVOut(-1,-1,-1);
|
|
|
|
|
|
|
|
|
|
Matrix44d Scl; Scl.SetScale(ScV);
|
|
|
|
|
Matrix44d Sxy; Sxy.SetShearXY(ShV[0]);
|
|
|
|
|
Matrix44d Sxz; Sxz.SetShearXZ(ShV[1]);
|
|
|
|
|
Matrix44d Syz; Syz.SetShearYZ(ShV[2]);
|
|
|
|
|
Matrix44d Rtx; Rtx.SetRotate(math::ToRad(RtV[0]),Point3d(1,0,0));
|
|
|
|
|
Matrix44d Rty; Rty.SetRotate(math::ToRad(RtV[1]),Point3d(0,1,0));
|
|
|
|
|
Matrix44d Rtz; Rtz.SetRotate(math::ToRad(RtV[2]),Point3d(0,0,1));
|
|
|
|
|
Matrix44d Trn; Trn.SetTranslate(TrV);
|
2005-06-10 17:04:12 +02:00
|
|
|
|
|
2005-06-17 07:28:47 +02:00
|
|
|
|
Matrix44d StartM = Trn * Rtx*Rty*Rtz * Syz*Sxz*Sxy *Scl;
|
|
|
|
|
Matrix44d ResultM=StartM;
|
|
|
|
|
Decompose(ResultM,ScVOut,ShVOut,RtVOut,TrVOut);
|
|
|
|
|
|
|
|
|
|
Scl.SetScale(ScVOut);
|
|
|
|
|
Sxy.SetShearXY(ShVOut[0]);
|
|
|
|
|
Sxz.SetShearXZ(ShVOut[1]);
|
|
|
|
|
Syz.SetShearYZ(ShVOut[2]);
|
|
|
|
|
Rtx.SetRotate(math::ToRad(RtVOut[0]),Point3d(1,0,0));
|
|
|
|
|
Rty.SetRotate(math::ToRad(RtVOut[1]),Point3d(0,1,0));
|
|
|
|
|
Rtz.SetRotate(math::ToRad(RtVOut[2]),Point3d(0,0,1));
|
|
|
|
|
Trn.SetTranslate(TrVOut);
|
|
|
|
|
|
|
|
|
|
// Now Rebuild is equal to StartM
|
|
|
|
|
Matrix44d RebuildM = Trn * Rtx*Rty*Rtz * Syz*Sxz*Sxy * Scl ;
|
2005-06-10 17:04:12 +02:00
|
|
|
|
*/
|
|
|
|
|
template <class T>
|
|
|
|
|
bool Decompose(Matrix44<T> &M, Point3<T> &ScaleV, Point3<T> &ShearV, Point3<T> &RotV,Point3<T> &TranV)
|
|
|
|
|
{
|
|
|
|
|
if(!(M[3][0]==0 && M[3][1]==0 && M[3][2]==0 && M[3][3]==1) ) // the matrix is projective
|
|
|
|
|
return false;
|
|
|
|
|
if(math::Abs(M.Determinant())<1e-10) return false; // matrix should be at least invertible...
|
|
|
|
|
|
2005-06-17 07:28:47 +02:00
|
|
|
|
// First Step recover the traslation
|
2005-06-10 17:04:12 +02:00
|
|
|
|
TranV=M.GetColumn3(3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Second Step Recover Scale and Shearing interleaved
|
|
|
|
|
ScaleV[0]=Norm(M.GetColumn3(0));
|
|
|
|
|
Point3d R[3];
|
|
|
|
|
R[0]=M.GetColumn3(0);
|
|
|
|
|
R[0].Normalize();
|
|
|
|
|
|
|
|
|
|
ShearV[0]=R[0]*M.GetColumn3(1); // xy shearing
|
|
|
|
|
R[1]= M.GetColumn3(1)-R[0]*ShearV[0];
|
|
|
|
|
assert(math::Abs(R[1]*R[0])<1e-10);
|
|
|
|
|
ScaleV[1]=Norm(R[1]); // y scaling
|
|
|
|
|
R[1]=R[1]/ScaleV[1];
|
|
|
|
|
ShearV[0]=ShearV[0]/ScaleV[1];
|
|
|
|
|
|
|
|
|
|
ShearV[1]=R[0]*M.GetColumn3(2); // xz shearing
|
|
|
|
|
R[2]= M.GetColumn3(2)-R[0]*ShearV[1];
|
|
|
|
|
assert(math::Abs(R[2]*R[0])<1e-10);
|
|
|
|
|
|
|
|
|
|
R[2] = R[2]-R[1]*(R[2]*R[1]);
|
|
|
|
|
assert(math::Abs(R[2]*R[1])<1e-10);
|
|
|
|
|
assert(math::Abs(R[2]*R[0])<1e-10);
|
|
|
|
|
|
|
|
|
|
ScaleV[2]=Norm(R[2]);
|
|
|
|
|
ShearV[1]=ShearV[1]/ScaleV[2];
|
|
|
|
|
R[2]=R[2]/ScaleV[2];
|
|
|
|
|
assert(math::Abs(R[2]*R[1])<1e-10);
|
|
|
|
|
assert(math::Abs(R[2]*R[0])<1e-10);
|
|
|
|
|
|
|
|
|
|
ShearV[2]=R[1]*M.GetColumn3(2); // yz shearing
|
|
|
|
|
ShearV[2]=ShearV[2]/ScaleV[2];
|
|
|
|
|
int i,j;
|
|
|
|
|
for(i=0;i<3;++i)
|
|
|
|
|
for(j=0;j<3;++j)
|
|
|
|
|
M[i][j]=R[j][i];
|
|
|
|
|
|
|
|
|
|
// Third and last step: Recover the rotation
|
|
|
|
|
//now the matrix should be a pure rotation matrix so its determinant is +-1
|
|
|
|
|
double det=M.Determinant();
|
|
|
|
|
if(math::Abs(det)<1e-10) return false; // matrix should be at least invertible...
|
|
|
|
|
assert(math::Abs(math::Abs(det)-1.0)<1e-10); // it should be +-1...
|
|
|
|
|
if(det<0) {
|
|
|
|
|
ScaleV *= -1;
|
|
|
|
|
M *= -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double alpha,beta,gamma; // rotations around the x,y and z axis
|
|
|
|
|
beta=asin( M[0][2]);
|
|
|
|
|
double cosbeta=cos(beta);
|
|
|
|
|
if(math::Abs(cosbeta) > 1e-5)
|
|
|
|
|
{
|
|
|
|
|
alpha=asin(-M[1][2]/cosbeta);
|
|
|
|
|
if((M[2][2]/cosbeta) < 0 ) alpha=M_PI-alpha;
|
|
|
|
|
gamma=asin(-M[0][1]/cosbeta);
|
|
|
|
|
if((M[0][0]/cosbeta)<0) gamma = M_PI-gamma;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
alpha=asin(-M[1][0]);
|
|
|
|
|
if(M[1][1]<0) alpha=M_PI-alpha;
|
|
|
|
|
gamma=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RotV[0]=math::ToDeg(alpha);
|
|
|
|
|
RotV[1]=math::ToDeg(beta);
|
|
|
|
|
RotV[2]=math::ToDeg(gamma);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
template <class T> T Matrix44<T>::Determinant() const {
|
|
|
|
|
LinearSolve<T> solve(*this);
|
|
|
|
|
return solve.Determinant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-04 03:10:14 +01:00
|
|
|
|
template <class T> Point3<T> operator*(const Matrix44<T> &m, const Point3<T> &p) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
T w;
|
2004-03-04 03:10:14 +01:00
|
|
|
|
Point3<T> s;
|
2004-10-18 17:03:14 +02:00
|
|
|
|
s[0] = m.ElementAt(0, 0)*p[0] + m.ElementAt(0, 1)*p[1] + m.ElementAt(0, 2)*p[2] + m.ElementAt(0, 3);
|
|
|
|
|
s[1] = m.ElementAt(1, 0)*p[0] + m.ElementAt(1, 1)*p[1] + m.ElementAt(1, 2)*p[2] + m.ElementAt(1, 3);
|
|
|
|
|
s[2] = m.ElementAt(2, 0)*p[0] + m.ElementAt(2, 1)*p[1] + m.ElementAt(2, 2)*p[2] + m.ElementAt(2, 3);
|
|
|
|
|
w = m.ElementAt(3, 0)*p[0] + m.ElementAt(3, 1)*p[1] + m.ElementAt(3, 2)*p[2] + m.ElementAt(3, 3);
|
2004-03-04 03:10:14 +01:00
|
|
|
|
if(w!= 0) s /= w;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-05 01:19:41 +02:00
|
|
|
|
//template <class T> Point3<T> operator*(const Point3<T> &p, const Matrix44<T> &m) {
|
|
|
|
|
// T w;
|
|
|
|
|
// Point3<T> s;
|
2004-10-18 17:03:14 +02:00
|
|
|
|
// s[0] = m.ElementAt(0, 0)*p[0] + m.ElementAt(1, 0)*p[1] + m.ElementAt(2, 0)*p[2] + m.ElementAt(3, 0);
|
|
|
|
|
// s[1] = m.ElementAt(0, 1)*p[0] + m.ElementAt(1, 1)*p[1] + m.ElementAt(2, 1)*p[2] + m.ElementAt(3, 1);
|
|
|
|
|
// s[2] = m.ElementAt(0, 2)*p[0] + m.ElementAt(1, 2)*p[1] + m.ElementAt(2, 2)*p[2] + m.ElementAt(3, 2);
|
|
|
|
|
// w = m.ElementAt(0, 3)*p[0] + m.ElementAt(1, 3)*p[1] + m.ElementAt(2, 3)*p[2] + m.ElementAt(3, 3);
|
2004-05-05 01:19:41 +02:00
|
|
|
|
// if(w != 0) s /= w;
|
|
|
|
|
// return s;
|
|
|
|
|
//}
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> &Transpose(Matrix44<T> &m) {
|
|
|
|
|
for(int i = 1; i < 4; i++)
|
|
|
|
|
for(int j = 0; j < i; j++) {
|
2005-06-28 19:42:47 +02:00
|
|
|
|
math::Swap(m.ElementAt(i, j), m.ElementAt(j, i));
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
2004-03-08 15:49:58 +01:00
|
|
|
|
return m;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> Matrix44<T> &Invert(Matrix44<T> &m) {
|
|
|
|
|
LinearSolve<T> solve(m);
|
|
|
|
|
|
|
|
|
|
for(int j = 0; j < 4; j++) { //Find inverse by columns.
|
|
|
|
|
Point4<T> col(0, 0, 0, 0);
|
|
|
|
|
col[j] = 1.0;
|
|
|
|
|
col = solve.Solve(col);
|
|
|
|
|
for(int i = 0; i < 4; i++)
|
2004-10-18 17:03:14 +02:00
|
|
|
|
m.ElementAt(i, j) = col[i];
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
2004-03-04 01:28:39 +01:00
|
|
|
|
return m;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-03-09 14:57:29 +01:00
|
|
|
|
template <class T> Matrix44<T> Inverse(const Matrix44<T> &m) {
|
|
|
|
|
LinearSolve<T> solve(m);
|
|
|
|
|
Matrix44<T> res;
|
|
|
|
|
for(int j = 0; j < 4; j++) { //Find inverse by columns.
|
|
|
|
|
Point4<T> col(0, 0, 0, 0);
|
|
|
|
|
col[j] = 1.0;
|
|
|
|
|
col = solve.Solve(col);
|
|
|
|
|
for(int i = 0; i < 4; i++)
|
2004-10-18 17:03:14 +02:00
|
|
|
|
res.ElementAt(i, j) = col[i];
|
2004-03-09 14:57:29 +01:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* LINEAR SOLVE IMPLEMENTATION */
|
|
|
|
|
|
|
|
|
|
template <class T> LinearSolve<T>::LinearSolve(const Matrix44<T> &m): Matrix44<T>(m) {
|
2004-03-08 16:33:58 +01:00
|
|
|
|
if(!Decompose()) {
|
|
|
|
|
for(int i = 0; i < 4; i++)
|
|
|
|
|
index[i] = i;
|
2005-04-14 13:35:09 +02:00
|
|
|
|
Matrix44<T>::SetZero();
|
2004-03-08 16:33:58 +01:00
|
|
|
|
}
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> T LinearSolve<T>::Determinant() const {
|
|
|
|
|
T det = d;
|
|
|
|
|
for(int j = 0; j < 4; j++)
|
2005-04-14 13:35:09 +02:00
|
|
|
|
det *= this-> ElementAt(j, j);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
return det;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*replaces a matrix by its LU decomposition of a rowwise permutation.
|
|
|
|
|
d is +or -1 depeneing of row permutation even or odd.*/
|
|
|
|
|
#define TINY 1e-100
|
|
|
|
|
|
2004-03-09 14:57:29 +01:00
|
|
|
|
template <class T> bool LinearSolve<T>::Decompose() {
|
|
|
|
|
|
|
|
|
|
/* Matrix44<T> A;
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
|
A[i] = operator[](i);
|
|
|
|
|
SetIdentity();
|
|
|
|
|
Point4<T> scale;
|
2004-10-07 16:23:57 +02:00
|
|
|
|
// Set scale factor, scale(i) = max( |a(i,j)| ), for each row
|
2004-03-09 14:57:29 +01:00
|
|
|
|
for(int i = 0; i < 4; i++ ) {
|
|
|
|
|
index[i] = i; // Initialize row index list
|
|
|
|
|
T scalemax = (T)0.0;
|
|
|
|
|
for(int j = 0; j < 4; j++)
|
2004-10-18 17:03:14 +02:00
|
|
|
|
scalemax = (scalemax > math::Abs(A.ElementAt(i,j))) ? scalemax : math::Abs(A.ElementAt(i,j));
|
2004-03-09 14:57:29 +01:00
|
|
|
|
scale[i] = scalemax;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-07 16:23:57 +02:00
|
|
|
|
// Loop over rows k = 1, ..., (N-1)
|
2004-03-09 14:57:29 +01:00
|
|
|
|
int signDet = 1;
|
|
|
|
|
for(int k = 0; k < 3; k++ ) {
|
2004-10-07 16:23:57 +02:00
|
|
|
|
// Select pivot row from max( |a(j,k)/s(j)| )
|
2004-03-09 14:57:29 +01:00
|
|
|
|
T ratiomax = (T)0.0;
|
|
|
|
|
int jPivot = k;
|
|
|
|
|
for(int i = k; i < 4; i++ ) {
|
2004-10-18 17:03:14 +02:00
|
|
|
|
T ratio = math::Abs(A.ElementAt(index[i], k))/scale[index[i]];
|
2004-03-09 14:57:29 +01:00
|
|
|
|
if(ratio > ratiomax) {
|
|
|
|
|
jPivot = i;
|
|
|
|
|
ratiomax = ratio;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-10-07 16:23:57 +02:00
|
|
|
|
// Perform pivoting using row index list
|
2004-03-09 14:57:29 +01:00
|
|
|
|
int indexJ = index[k];
|
|
|
|
|
if( jPivot != k ) { // Pivot
|
|
|
|
|
indexJ = index[jPivot];
|
|
|
|
|
index[jPivot] = index[k]; // Swap index jPivot and k
|
|
|
|
|
index[k] = indexJ;
|
|
|
|
|
signDet *= -1; // Flip sign of determinant
|
|
|
|
|
}
|
2004-10-07 16:23:57 +02:00
|
|
|
|
// Perform forward elimination
|
2004-03-09 14:57:29 +01:00
|
|
|
|
for(int i=k+1; i < 4; i++ ) {
|
2004-10-18 17:03:14 +02:00
|
|
|
|
T coeff = A.ElementAt(index[i],k)/A.ElementAt(indexJ,k);
|
2004-03-09 14:57:29 +01:00
|
|
|
|
for(int j=k+1; j < 4; j++ )
|
2004-10-18 17:03:14 +02:00
|
|
|
|
A.ElementAt(index[i],j) -= coeff*A.ElementAt(indexJ,j);
|
|
|
|
|
A.ElementAt(index[i],k) = coeff;
|
2004-03-09 14:57:29 +01:00
|
|
|
|
//for( j=1; j< 4; j++ )
|
2004-10-18 17:03:14 +02:00
|
|
|
|
// ElementAt(index[i],j) -= A.ElementAt(index[i], k)*ElementAt(indexJ, j);
|
2004-03-09 14:57:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
|
operator[](i) = A[i];
|
|
|
|
|
|
|
|
|
|
d = signDet;
|
2004-10-07 16:23:57 +02:00
|
|
|
|
// this = A;
|
2004-03-09 14:57:29 +01:00
|
|
|
|
return true; */
|
|
|
|
|
|
2004-02-19 15:42:05 +01:00
|
|
|
|
d = 1; //no permutation still
|
|
|
|
|
|
|
|
|
|
T scaling[4];
|
2004-05-07 12:05:50 +02:00
|
|
|
|
int i,j,k;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
//Saving the scvaling information per row
|
2004-05-07 12:05:50 +02:00
|
|
|
|
for(i = 0; i < 4; i++) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
T largest = 0.0;
|
2004-05-07 12:05:50 +02:00
|
|
|
|
for(j = 0; j < 4; j++) {
|
2005-04-14 13:35:09 +02:00
|
|
|
|
T t = math::Abs(this->ElementAt(i, j));
|
2004-02-19 15:42:05 +01:00
|
|
|
|
if (t > largest) largest = t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (largest == 0.0) { //oooppps there is a zero row!
|
2004-03-08 16:33:58 +01:00
|
|
|
|
return false;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
2004-03-08 16:33:58 +01:00
|
|
|
|
scaling[i] = (T)1.0 / largest;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int imax;
|
2004-05-07 12:05:50 +02:00
|
|
|
|
for(j = 0; j < 4; j++) {
|
|
|
|
|
for(i = 0; i < j; i++) {
|
2005-04-14 13:35:09 +02:00
|
|
|
|
T sum = this->ElementAt(i,j);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
for(int k = 0; k < i; k++)
|
2005-04-14 13:35:09 +02:00
|
|
|
|
sum -= this->ElementAt(i,k)*this->ElementAt(k,j);
|
|
|
|
|
this->ElementAt(i,j) = sum;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
T largest = 0.0;
|
2004-05-07 12:05:50 +02:00
|
|
|
|
for(i = j; i < 4; i++) {
|
2005-04-14 13:35:09 +02:00
|
|
|
|
T sum = this->ElementAt(i,j);
|
2004-05-07 12:05:50 +02:00
|
|
|
|
for(k = 0; k < j; k++)
|
2005-04-14 13:35:09 +02:00
|
|
|
|
sum -= this->ElementAt(i,k)*this->ElementAt(k,j);
|
|
|
|
|
this->ElementAt(i,j) = sum;
|
2004-03-08 16:33:58 +01:00
|
|
|
|
T t = scaling[i] * math::Abs(sum);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
if(t >= largest) {
|
|
|
|
|
largest = t;
|
|
|
|
|
imax = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (j != imax) {
|
|
|
|
|
for (int k = 0; k < 4; k++) {
|
2005-04-14 13:35:09 +02:00
|
|
|
|
T dum = this->ElementAt(imax,k);
|
|
|
|
|
this->ElementAt(imax,k) = this->ElementAt(j,k);
|
|
|
|
|
this->ElementAt(j,k) = dum;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
d = -(d);
|
|
|
|
|
scaling[imax] = scaling[j];
|
|
|
|
|
}
|
|
|
|
|
index[j]=imax;
|
2005-04-14 13:35:09 +02:00
|
|
|
|
if (this->ElementAt(j,j) == 0.0) this->ElementAt(j,j) = (T)TINY;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
if (j != 3) {
|
2005-04-14 13:35:09 +02:00
|
|
|
|
T dum = (T)1.0 / (this->ElementAt(j,j));
|
2004-02-19 15:42:05 +01:00
|
|
|
|
for (i = j+1; i < 4; i++)
|
2005-04-14 13:35:09 +02:00
|
|
|
|
this->ElementAt(i,j) *= dum;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-09 14:57:29 +01:00
|
|
|
|
return true;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 15:58:23 +01:00
|
|
|
|
template <class T> Point4<T> LinearSolve<T>::Solve(const Point4<T> &b) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
Point4<T> x(b);
|
2004-03-09 14:57:29 +01:00
|
|
|
|
int first = -1, ip;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
|
ip = index[i];
|
|
|
|
|
T sum = x[ip];
|
|
|
|
|
x[ip] = x[i];
|
2004-03-09 14:57:29 +01:00
|
|
|
|
if(first!= -1)
|
2004-02-19 15:42:05 +01:00
|
|
|
|
for(int j = first; j <= i-1; j++)
|
2005-04-14 13:35:09 +02:00
|
|
|
|
sum -= this->ElementAt(i,j) * x[j];
|
2004-02-19 15:42:05 +01:00
|
|
|
|
else
|
2004-03-09 14:57:29 +01:00
|
|
|
|
if(sum) first = i;
|
|
|
|
|
x[i] = sum;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
for (int i = 3; i >= 0; i--) {
|
|
|
|
|
T sum = x[i];
|
|
|
|
|
for (int j = i+1; j < 4; j++)
|
2005-04-14 13:35:09 +02:00
|
|
|
|
sum -= this->ElementAt(i, j) * x[j];
|
|
|
|
|
x[i] = sum / this->ElementAt(i, i);
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} //namespace
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|