Subversion Repositories gelsvn

Rev

Rev 2 | Rev 12 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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