Subversion Repositories gelsvn

Rev

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

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