Subversion Repositories gelsvn

Rev

Rev 22 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#ifndef __CGLA_ARITHSQMATFLOAT_H__
2
#define __CGLA_ARITHSQMATFLOAT_H__
3
 
4
#include "ArithMatFloat.h"
5
 
12 jab 6
namespace CGLA 
7
{
2 bj 8
 
41 jab 9
		/** Template for square matrices.
10
				Some functions like trace and determinant work only on
11
				square matrices. To express this in the class hierarchy,
12
				ArithSqMatFloat was created. ArithSqMatFloat is derived from ArithMat
13
				and contains a few extra facilities applicable only to
14
				square matrices.
15
		*/
16
		template <class VT, class MT, int ROWS>
17
				class ArithSqMatFloat: public ArithMatFloat<VT,VT,MT,ROWS> 
18
				{ 
19
				public:
2 bj 20
 
41 jab 21
						/// Vector type
22
						typedef VT VectorType;
2 bj 23
 
41 jab 24
						/// The type of a matrix element
25
						typedef typename VT::ScalarType ScalarType;
2 bj 26
 
41 jab 27
				protected:
2 bj 28
 
41 jab 29
						/// Construct 0 matrix
30
						ArithSqMatFloat() {}
2 bj 31
 
41 jab 32
						/// Construct matrix where all values are equal to constructor argument.
33
						explicit ArithSqMatFloat(ScalarType _a):
34
								ArithMatFloat<VT,VT,MT,ROWS>(_a) {}
2 bj 35
 
41 jab 36
						/// Construct 2x2 Matrix from two vectors
37
						ArithSqMatFloat(VT _a, VT _b): 
38
								ArithMatFloat<VT,VT,MT,ROWS>(_a,_b) {}
2 bj 39
 
41 jab 40
						/// Construct 3x3 Matrix from three vectors
41
						ArithSqMatFloat(VT _a, VT _b, VT _c): 
42
								ArithMatFloat<VT,VT,MT,ROWS>(_a,_b,_c) {}
2 bj 43
 
41 jab 44
						/// Construct 4x4 Matrix from four vectors
45
						ArithSqMatFloat(VT _a, VT _b, VT _c, VT _d): 
46
								ArithMatFloat<VT,VT,MT,ROWS>(_a,_b,_c,_d) {}
2 bj 47
 
41 jab 48
				public:
2 bj 49
 
41 jab 50
						/** Assignment multiplication of matrices. 
51
								This function is not very efficient. This because we need a temporary
52
								matrix anyway, so it can't really be made efficient. */
53
						const MT& operator*=(const MT& m2)
54
								{
55
										(*this) = (*this) * m2;
56
										return static_cast<const MT&>(*this);
57
								}
2 bj 58
 
59
 
41 jab 60
						/// Assignment multiplication of matrix by scalar.
61
						const MT& operator *=(ScalarType k) 
62
								{
63
										ArithMatFloat<VT,VT,MT,ROWS>::operator*=(k);
64
								}
65
 
66
						void identity()
67
								{
68
										for(int i=0;i<ROWS;++i)
69
										{
70
												for(int j=0;j<ROWS;++j)
71
														(*this)[i][j] = ScalarType(0);
72
												(*this)[i][i] = ScalarType(1);
73
										}
74
								}
75
 
2 bj 76
 
41 jab 77
				};
2 bj 78
 
41 jab 79
		/** Multiply two matrices derived from same type, 
80
				producing a new of same type */
81
		template <class VT, class MT, int ROWS>
82
				inline MT operator*(const ArithSqMatFloat<VT,MT,ROWS>& m1,
83
														const ArithSqMatFloat<VT,MT,ROWS>& m2) 
84
				{
85
						MT n;
86
						for(int i=0;i<ROWS;i++)
87
								for(int j=0;j<ROWS;j++)
88
								{
89
										n[i][j] = 0;
90
										for(int k=0;k<ROWS;k++)
91
												n[i][j] += m1[i][k] * m2[k][j]; 
92
								}
93
						return n;
94
				}
2 bj 95
 
41 jab 96
		/** Compute the transpose of a square matrix. This function returns
97
				the transpose of its argument. */
98
		template <class VT, class MT, int ROWS>
99
				inline MT transpose(const ArithSqMatFloat<VT,MT,ROWS>& m) 
100
				{
101
						MT m_new;
102
						for(int i=0;i<MT::get_v_dim();i++)
103
								for(int j=0;j<MT::get_h_dim();j++)
104
										m_new[i][j] = m[j][i];
105
						return m_new;
106
				}
2 bj 107
 
41 jab 108
		/// Compute trace. Works only for sq. matrices.
109
		template <class VT, class MT, int ROWS>
110
				inline typename MT::ScalarType trace(const ArithSqMatFloat<VT,MT,ROWS>& M)
111
				{
112
						typename ArithSqMatFloat<VT,MT,ROWS>::ScalarType s=0;
113
						for(int i=0;i<ROWS;i++)
114
								s += M[i][i];
115
						return s;
116
				}
2 bj 117
 
118
}
119
#endif