Subversion Repositories gelsvn

Rev

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

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