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 $
|
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>
|
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
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
|
///@{
|
|
|
|
|
|
|
|
|
|
/** $name Contrutors
|
|
|
|
|
* 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[]);
|
|
|
|
|
|
|
|
|
|
T &element(const int row, const int col);
|
|
|
|
|
T element(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
|
|
|
|
|
|
|
|
|
Matrix44 operator+(const Matrix44 &m) const;
|
|
|
|
|
Matrix44 operator-(const Matrix44 &m) const;
|
|
|
|
|
Matrix44 operator*(const Matrix44 &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 );
|
|
|
|
|
|
|
|
|
|
void SetZero();
|
|
|
|
|
void SetIdentity();
|
|
|
|
|
void SetDiagonal(const T k);
|
2004-03-04 03:10:14 +01:00
|
|
|
|
Matrix44 &SetScale(const T sx, const T sy, const T sz);
|
|
|
|
|
Matrix44 &SetTranslate(const Point3<T> &t);
|
|
|
|
|
Matrix44 &SetTranslate(const T sx, const T sy, const T sz);
|
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-02-19 15:42:05 +01:00
|
|
|
|
template <class T> T &Matrix44<T>::element(const int row, const int col) {
|
|
|
|
|
assert(row >= 0 && row < 4);
|
|
|
|
|
assert(col >= 0 && col < 4);
|
|
|
|
|
return _a[(row<<2) + col];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> T Matrix44<T>::element(const int row, const int col) const {
|
|
|
|
|
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];
|
|
|
|
|
//}
|
|
|
|
|
template <class T> T *Matrix44<T>::operator[](const int i) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
assert(i >= 0 && i < 16);
|
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-05-04 04:34:03 +02:00
|
|
|
|
ret[i] = V()[i] + m.V()[i];
|
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-05-04 04:34:03 +02:00
|
|
|
|
ret[i] = V()[i] - m.V()[i];
|
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-03-04 03:10:14 +01:00
|
|
|
|
t += element(i, k) * m.element(k, j);
|
|
|
|
|
ret.element(i, j) = t;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-03-04 03:10:14 +01:00
|
|
|
|
t += element(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 {
|
|
|
|
|
for(int i = 0 ; i < 16; i++)
|
|
|
|
|
if(operator[](i) != m[i])
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
template <class T> bool Matrix44<T>::operator!=(const Matrix44 &m) const {
|
|
|
|
|
for(int i = 0 ; i < 16; i++)
|
|
|
|
|
if(operator[](i) != m[i])
|
|
|
|
|
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++) {
|
|
|
|
|
t[k] += element(i, k) * m.element(k, j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int l = 0; l < 4; l++)
|
|
|
|
|
element(i, l) = t[l];
|
2004-03-04 03:10:14 +01:00
|
|
|
|
} */
|
2004-02-19 15:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> void Matrix44<T>::operator*=( const T k ) {
|
|
|
|
|
for(int i = 0; i < 4; i++)
|
|
|
|
|
operator[](i) *= k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
element(0, 0) = k;
|
|
|
|
|
element(1, 1) = k;
|
|
|
|
|
element(2, 2) = k;
|
|
|
|
|
element(3, 3) = 1;
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
element(0, 0) = sx;
|
|
|
|
|
element(1, 1) = sy;
|
|
|
|
|
element(2, 2) = sz;
|
|
|
|
|
element(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
|
|
|
|
}
|
2004-03-04 03:10:14 +01:00
|
|
|
|
template <class T> Matrix44<T> &Matrix44<T>::SetTranslate(const T sx, const T sy, const T sz) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
SetIdentity();
|
2004-05-05 01:19:41 +02:00
|
|
|
|
element(0, 3) = sx;
|
|
|
|
|
element(1, 3) = sy;
|
|
|
|
|
element(2, 3) = sz;
|
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();
|
|
|
|
|
element(0,0) = t[0]*t[0]*q + c;
|
2004-05-05 01:19:41 +02:00
|
|
|
|
element(0,1) = t[0]*t[1]*q - t[2]*s;
|
|
|
|
|
element(0,2) = t[0]*t[2]*q + t[1]*s;
|
|
|
|
|
element(0,3) = 0;
|
|
|
|
|
element(1,0) = t[1]*t[0]*q + t[2]*s;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
element(1,1) = t[1]*t[1]*q + c;
|
2004-05-05 01:19:41 +02:00
|
|
|
|
element(1,2) = t[1]*t[2]*q - t[0]*s;
|
|
|
|
|
element(1,3) = 0;
|
|
|
|
|
element(2,0) = t[2]*t[0]*q -t[1]*s;
|
|
|
|
|
element(2,1) = t[2]*t[1]*q +t[0]*s;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
element(2,2) = t[2]*t[2]*q +c;
|
|
|
|
|
element(2,3) = 0;
|
2004-05-05 01:19:41 +02:00
|
|
|
|
element(3,0) = 0;
|
|
|
|
|
element(3,1) = 0;
|
|
|
|
|
element(3,2) = 0;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
element(3,3) = 1;
|
2004-03-04 03:10:14 +01:00
|
|
|
|
return *this;
|
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;
|
|
|
|
|
s[0] = m.element(0, 0)*p[0] + m.element(0, 1)*p[1] + m.element(0, 2)*p[2] + m.element(0, 3);
|
|
|
|
|
s[1] = m.element(1, 0)*p[0] + m.element(1, 1)*p[1] + m.element(1, 2)*p[2] + m.element(1, 3);
|
|
|
|
|
s[2] = m.element(2, 0)*p[0] + m.element(2, 1)*p[1] + m.element(2, 2)*p[2] + m.element(2, 3);
|
|
|
|
|
w = m.element(3, 0)*p[0] + m.element(3, 1)*p[1] + m.element(3, 2)*p[2] + m.element(3, 3);
|
|
|
|
|
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;
|
|
|
|
|
// s[0] = m.element(0, 0)*p[0] + m.element(1, 0)*p[1] + m.element(2, 0)*p[2] + m.element(3, 0);
|
|
|
|
|
// s[1] = m.element(0, 1)*p[0] + m.element(1, 1)*p[1] + m.element(2, 1)*p[2] + m.element(3, 1);
|
|
|
|
|
// s[2] = m.element(0, 2)*p[0] + m.element(1, 2)*p[1] + m.element(2, 2)*p[2] + m.element(3, 2);
|
|
|
|
|
// w = m.element(0, 3)*p[0] + m.element(1, 3)*p[1] + m.element(2, 3)*p[2] + m.element(3, 3);
|
|
|
|
|
// 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++) {
|
2004-03-08 15:49:58 +01:00
|
|
|
|
T t = m.element(i, j);
|
|
|
|
|
m.element(i, j) = m.element(j, i);
|
|
|
|
|
m.element(j, i) = t;
|
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++)
|
|
|
|
|
m.element(i, j) = col[i];
|
|
|
|
|
}
|
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++)
|
|
|
|
|
res.element(i, j) = col[i];
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
SetZero();
|
|
|
|
|
}
|
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++)
|
|
|
|
|
det *= element(j, j);
|
|
|
|
|
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;
|
|
|
|
|
//* Set scale factor, scale(i) = max( |a(i,j)| ), for each row
|
|
|
|
|
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++)
|
|
|
|
|
scalemax = (scalemax > math::Abs(A.element(i,j))) ? scalemax : math::Abs(A.element(i,j));
|
|
|
|
|
scale[i] = scalemax;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//* Loop over rows k = 1, ..., (N-1)
|
|
|
|
|
int signDet = 1;
|
|
|
|
|
for(int k = 0; k < 3; k++ ) {
|
|
|
|
|
//* Select pivot row from max( |a(j,k)/s(j)| )
|
|
|
|
|
T ratiomax = (T)0.0;
|
|
|
|
|
int jPivot = k;
|
|
|
|
|
for(int i = k; i < 4; i++ ) {
|
|
|
|
|
T ratio = math::Abs(A.element(index[i], k))/scale[index[i]];
|
|
|
|
|
if(ratio > ratiomax) {
|
|
|
|
|
jPivot = i;
|
|
|
|
|
ratiomax = ratio;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//* Perform pivoting using row index list
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
//* Perform forward elimination
|
|
|
|
|
for(int i=k+1; i < 4; i++ ) {
|
|
|
|
|
T coeff = A.element(index[i],k)/A.element(indexJ,k);
|
|
|
|
|
for(int j=k+1; j < 4; j++ )
|
|
|
|
|
A.element(index[i],j) -= coeff*A.element(indexJ,j);
|
|
|
|
|
A.element(index[i],k) = coeff;
|
|
|
|
|
//for( j=1; j< 4; j++ )
|
|
|
|
|
// element(index[i],j) -= A.element(index[i], k)*element(indexJ, j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
|
operator[](i) = A[i];
|
|
|
|
|
|
|
|
|
|
d = signDet;
|
|
|
|
|
//*this = A;
|
|
|
|
|
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++) {
|
2004-03-08 16:33:58 +01:00
|
|
|
|
T t = math::Abs(element(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++) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
T sum = element(i,j);
|
|
|
|
|
for(int k = 0; k < i; k++)
|
|
|
|
|
sum -= element(i,k)*element(k,j);
|
|
|
|
|
element(i,j) = sum;
|
|
|
|
|
}
|
|
|
|
|
T largest = 0.0;
|
2004-05-07 12:05:50 +02:00
|
|
|
|
for(i = j; i < 4; i++) {
|
2004-02-19 15:42:05 +01:00
|
|
|
|
T sum = element(i,j);
|
2004-05-07 12:05:50 +02:00
|
|
|
|
for(k = 0; k < j; k++)
|
2004-02-19 15:42:05 +01:00
|
|
|
|
sum -= element(i,k)*element(k,j);
|
|
|
|
|
element(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++) {
|
|
|
|
|
T dum = element(imax,k);
|
|
|
|
|
element(imax,k) = element(j,k);
|
|
|
|
|
element(j,k) = dum;
|
|
|
|
|
}
|
|
|
|
|
d = -(d);
|
|
|
|
|
scaling[imax] = scaling[j];
|
|
|
|
|
}
|
|
|
|
|
index[j]=imax;
|
2004-03-08 16:33:58 +01:00
|
|
|
|
if (element(j,j) == 0.0) element(j,j) = (T)TINY;
|
2004-02-19 15:42:05 +01:00
|
|
|
|
if (j != 3) {
|
2004-03-08 16:33:58 +01:00
|
|
|
|
T dum = (T)1.0 / (element(j,j));
|
2004-02-19 15:42:05 +01:00
|
|
|
|
for (i = j+1; i < 4; i++)
|
|
|
|
|
element(i,j) *= dum;
|
|
|
|
|
}
|
|
|
|
|
}
|
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++)
|
|
|
|
|
sum -= element(i,j) * x[j];
|
|
|
|
|
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++)
|
|
|
|
|
sum -= element(i, j) * x[j];
|
|
|
|
|
x[i] = sum / element(i, i);
|
|
|
|
|
}
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} //namespace
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|