ALBA
albaMatrix3x3.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: ALBA (Agile Library for Biomedical Applications)
4 Module: albaMatrix3x3
5 Authors: Based on vtkMath code (www.vtk.org), adapted by Marco Petrone
6
7 Copyright (c) BIC
8 All rights reserved. See Copyright.txt or
9
10
11 This software is distributed WITHOUT ANY WARRANTY; without even
12 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the above copyright notice for more information.
14
15=========================================================================*/
16
17#ifndef __albaMatrix3x3_h
18#define __albaMatrix3x3_h
19
20#include "albaObject.h"
21#include "albaTimeStamped.h"
22#include <math.h>
23
24typedef double (*albaMatrix3x3Elements)[3];
25
26
32class ALBA_EXPORT albaMatrix3x3: public albaObject, public albaTimeStamped
33{
34public:
36 virtual void Print (std::ostream& os, const int indent=0) const;
37
39 virtual ~albaMatrix3x3();
40
43
46
48 static void MultiplyVector(const float A[3][3], const float in[3],
49 float out[3]);
51 static void MultiplyVector(const double A[3][3], const double in[3],
52 double out[3]);
53
55 void MultiplyVector(const double in[3], double out[3]) {MultiplyVector(GetElements(),in,out);}
56
58 static void Multiply(const double A[3][3], const double B[3][3],
59 double C[3][3]);
60
62 static void Multiply(const albaMatrix3x3 &A,albaMatrix3x3 &B, albaMatrix3x3 &C) \
63 {Multiply(A.GetElements(),B.GetElements(),C.GetElements());C.Modified();}
64
66
70 void SetElement(int i, int j, double value) {GetElements()[i][j]=value;}
71
73 double GetElement(int i, int j) const {return GetElements()[i][j];}
74
76 static void GetVersor(int axis, const albaMatrix3x3 &matrix, double versor[3]);
78 void GetVersor(int axis, double versor[3]) {GetVersor(axis,*this,versor);}
79
81 void Zero() { albaMatrix3x3::Zero(*GetElements()); Modified(); }
83 static void Zero(double elements[9]);
84
86 void Identity() { albaMatrix3x3::Identity(GetElements()); Modified();}
87 static void Identity(double A[3][3]);
88
90 static void Transpose(const double A[3][3], double AT[3][3]);
91 void Transpose() {Transpose(GetElements(),GetElements());; Modified();}
92
94 static void Invert(const double A[3][3], double AI[3][3]);
95 void Invert() {albaMatrix3x3::Invert(GetElements(),GetElements());; Modified();}
96
101 static void Orthogonalize(const double A[3][3], double B[3][3]);
102
107 void Orthogonalize() {Orthogonalize(GetElements(),GetElements());Modified();}
108
114 static void Diagonalize(const double A[3][3],double w[3],double V[3][3]);
115
117 static double Determinant(double A[3][3]);
118
120 double Determinant() {return Determinant(GetElements());}
121
122 static inline double Determinant(const double c1[3],
123 const double c2[3],
124 const double c3[3]);
125
126 static inline double Determinant(double a1, double a2, double a3,
127 double b1, double b2, double b3,
128 double c1, double c2, double c3);
129
133 static void QuaternionToMatrix(const double quat[4], double A[3][3]);
134
138 void QuaternionToMatrix(const double quat[4]) {QuaternionToMatrix(quat,GetElements());}
139
144 static void MatrixToQuaternion(const double A[3][3], double quat[4]);
145
150 void MatrixToQuaternion(double quat[4]) {MatrixToQuaternion(GetElements(),quat);}
151
160 static void SingularValueDecomposition(const double A[3][3],
161 double U[3][3], double w[3],
162 double VT[3][3]);
163
165 void SingularValueDecomposition(double U[3][3], double w[3], double VT[3][3]) \
166 {SingularValueDecomposition(GetElements(),U,w,VT);}
167
168 /*
169 Jacobi iteration for the solution of eigenvectors/eigenvalues of a 3x3
170 real symmetric matrix. Square 3x3 matrix a; output eigenvalues in w;
171 and output eigenvectors in v. Resulting eigenvalues/vectors are sorted
172 in decreasing order; eigenvectors are normalized.*/
173 static int Jacobi(const double A[3][3], double w[3], double v[3][3]);
174
180 //int Jacobi(double w[3], double v[3][3]) {Jacobi(GetElements(),w,v);}
181
188 static int JacobiN(double **a, int n, double *w, double **v);
189
193 static void LUFactor(double A[3][3], int index[3]);
194 void LUFactor(int index[3]) {LUFactor(GetElements(),index);}
195
199 static void LUSolve(const double A[3][3], const int index[3], double x[3]);
200 void LUSolve(const int index[3], double x[3]) \
201 { LUSolve(GetElements(),index,x);}
202
203
205 static double DegreesToRadians() {return 0.017453292519943295;};
206 static double Pi() {return 3.1415926535897932384626;};
207 static double RadiansToDegrees() {return 57.29577951308232;};
208
210 double *operator[](const unsigned int i) {return &(GetElements()[i][0]);}
211
213 const double *operator[](unsigned int i) const { return &(GetElements()[i][0]); }
214
216 static double Determinant2x2(double a, double b, double c, double d) {
217 return (a * d - b * c);};
219 static double Determinant2x2(const double c1[2], const double c2[2]) {
220 return (c1[0]*c2[1] - c2[0]*c1[1]);};
221
223 static double Norm(const double x[3]) {
224 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);};
225
226 static inline void Cross(const double x[3], const double y[3], double z[3]);
227 static inline double Normalize(double x[3]);
228
229protected:
230
231 double m_Elements[3][3];
232};
233
234//------------------------------------------------------------------------------
235inline double albaMatrix3x3::Determinant(double A[3][3])
236//------------------------------------------------------------------------------
237{
238 return A[0][0]*A[1][1]*A[2][2] + A[1][0]*A[2][1]*A[0][2] +
239 A[2][0]*A[0][1]*A[1][2] - A[0][0]*A[2][1]*A[1][2] -
240 A[1][0]*A[0][1]*A[2][2] - A[2][0]*A[1][1]*A[0][2];
241}
242//------------------------------------------------------------------------------
243inline double albaMatrix3x3::Determinant(const double c1[3],
244 const double c2[3],
245 const double c3[3])
246//------------------------------------------------------------------------------
247{
248 return c1[0]*c2[1]*c3[2] + c2[0]*c3[1]*c1[2] + c3[0]*c1[1]*c2[2] -
249 c1[0]*c3[1]*c2[2] - c2[0]*c1[1]*c3[2] - c3[0]*c2[1]*c1[2];
250}
251//------------------------------------------------------------------------------
252inline double albaMatrix3x3::Determinant(double a1, double a2, double a3,
253 double b1, double b2, double b3,
254 double c1, double c2, double c3)
255//------------------------------------------------------------------------------
256{
257 return ( a1 * albaMatrix3x3::Determinant2x2( b2, b3, c2, c3 )
258 - b1 * albaMatrix3x3::Determinant2x2( a2, a3, c2, c3 )
259 + c1 * albaMatrix3x3::Determinant2x2( a2, a3, b2, b3 ) );
260}
261//------------------------------------------------------------------------------
262// Cross product of two 3-vectors. Result vector in z[3].
263inline void albaMatrix3x3::Cross(const double x[3], const double y[3], double z[3])
264//------------------------------------------------------------------------------
265{
266 double Zx = x[1]*y[2] - x[2]*y[1];
267 double Zy = x[2]*y[0] - x[0]*y[2];
268 double Zz = x[0]*y[1] - x[1]*y[0];
269 z[0] = Zx; z[1] = Zy; z[2] = Zz;
270}
271//------------------------------------------------------------------------------
272inline double albaMatrix3x3::Normalize(double x[3])
273//------------------------------------------------------------------------------
274{
275 double den;
276 if ( (den = albaMatrix3x3::Norm(x)) != 0.0 )
277 {
278 for (int i=0; i < 3; i++)
279 {
280 x[i] /= den;
281 }
282 }
283 return den;
284}
285
286#endif
287
double(* albaMatrix3x3Elements)[3]
Definition: albaMatrix3x3.h:24
albaMatrix3x3 - Simple 3x3 Matrix.
Definition: albaMatrix3x3.h:33
double GetElement(int i, int j) const
Returns the element i,j from the matrix.
Definition: albaMatrix3x3.h:73
void DeepCopy(albaMatrix3x3 *mat)
copy the given matrix content
static double Determinant2x2(double a, double b, double c, double d)
Calculate the determinant of a 2x2 matrix: | a b | | c d |.
static double Normalize(double x[3])
albaTypeMacro(albaMatrix3x3, albaObject)
static void LUSolve(const double A[3][3], const int index[3], double x[3])
LU back substitution for a 3x3 matrix.
void Transpose()
Definition: albaMatrix3x3.h:91
static void Transpose(const double A[3][3], double AT[3][3])
Transpose the 3x3 matrix.
static void Multiply(const albaMatrix3x3 &A, albaMatrix3x3 &B, albaMatrix3x3 &C)
Multiply this matrix by another according to C = AB.
Definition: albaMatrix3x3.h:62
static void LUFactor(double A[3][3], int index[3])
LU Factorization of a 3x3 matrix.
void SetElement(int i, int j, double value)
Sets the element i,j in the matrix.
Definition: albaMatrix3x3.h:70
static double Determinant2x2(const double c1[2], const double c2[2])
Calculate the determinant of a 2x2 matrix with columns c1 and c2.
static void QuaternionToMatrix(const double quat[4], double A[3][3])
Convert a quaternion to a 3x3 rotation matrix.
static void MultiplyVector(const float A[3][3], const float in[3], float out[3])
Multiply a vector by a 3x3 matrix.
void MultiplyVector(const double in[3], double out[3])
Multiply a vector by this matrix.
Definition: albaMatrix3x3.h:55
void Zero()
Set all of the elements to zero.
Definition: albaMatrix3x3.h:81
static void Identity(double A[3][3])
static void MultiplyVector(const double A[3][3], const double in[3], double out[3])
Multiply a vector by a 3x3 matrix.
albaMatrix3x3Elements GetElements() const
Definition: albaMatrix3x3.h:65
static void Diagonalize(const double A[3][3], double w[3], double V[3][3])
Diagonalize a symmetric 3x3 matrix and return the eigenvalues in w and the eigenvectors in the column...
double Determinant()
Compute the determinant of the matrix and return it.
static int JacobiN(double **a, int n, double *w, double **v)
Jacobi iteration for the solution of eigenvectors/eigenvalues of this 3x3 real symmetric matrix.
const double * operator[](unsigned int i) const
bracket operator to access single elements
void MatrixToQuaternion(double quat[4])
Convert a 3x3 matrix into a quaternion.
albaMatrix3x3(albaMatrix3x3 &mat)
virtual ~albaMatrix3x3()
static double RadiansToDegrees()
void QuaternionToMatrix(const double quat[4])
Convert a quaternion to a 3x3 rotation matrix.
static double Norm(const double x[3])
Compute the norm of 3-vector (double-precision version).
static void GetVersor(int axis, const albaMatrix3x3 &matrix, double versor[3])
Get the given matrix versor.
void GetVersor(int axis, double versor[3])
Get the given matrix versor.
Definition: albaMatrix3x3.h:78
double * operator[](const unsigned int i)
bracket operator to access & write single elements
static void Multiply(const double A[3][3], const double B[3][3], double C[3][3])
Multiply one 3x3 matrix by another according to C = AB.
void LUSolve(const int index[3], double x[3])
static double Pi()
static void Cross(const double x[3], const double y[3], double z[3])
static void Orthogonalize(const double A[3][3], double B[3][3])
Orthogonalize a 3x3 matrix and put the result in B.
albaMatrix3x3 & operator=(const albaMatrix3x3 &mat)
virtual void Print(std::ostream &os, const int indent=0) const
print debug information for this object
void Orthogonalize()
Orthogonalize this matrix in place.
void Identity()
Set equal to Identity matrix.
Definition: albaMatrix3x3.h:86
static void Invert(const double A[3][3], double AI[3][3])
Invert the 3x3 matrix.
static int Jacobi(const double A[3][3], double w[3], double v[3][3])
static void MatrixToQuaternion(const double A[3][3], double quat[4])
Convert a 3x3 matrix into a quaternion.
static void SingularValueDecomposition(const double A[3][3], double U[3][3], double w[3], double VT[3][3])
Perform singular value decomposition on a 3x3 matrix.
void SingularValueDecomposition(double U[3][3], double w[3], double VT[3][3])
Perform singular value decomposition on a 3x3 matrix.
static void Zero(double elements[9])
Set all of the elements to zero.
void LUFactor(int index[3])
static double DegreesToRadians()
Useful constants.
Abstract superclass for all ALBA classes implementing RTTI APIs.
Definition: albaObject.h:38
class acting as an interface for timestamped objects This object simply defines few methods for manag...
virtual void Modified()
Update this objects modification time.