Subversion Repositories gelsvn

Rev

Rev 595 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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