Subversion Repositories gelsvn

Rev

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

Rev 89 Rev 393
1
#ifndef __CGLA_ARITHSQMAT2X2FLOAT_H__
1
#ifndef __CGLA_ARITHSQMAT2X2FLOAT_H__
2
#define __CGLA_ARITHSQMAT2X2FLOAT_H__
2
#define __CGLA_ARITHSQMAT2X2FLOAT_H__
3
 
3
 
4
#include "ExceptionStandard.h"
4
#include "ExceptionStandard.h"
5
#include "ArithSqMatFloat.h"
5
#include "ArithSqMatFloat.h"
6
 
6
 
7
 
7
 
8
namespace CGLA 
8
namespace CGLA 
9
{
9
{
10
 
10
 
11
  CGLA_DERIVEEXCEPTION(Mat2x2fException);
11
  CGLA_DERIVEEXCEPTION(Mat2x2fException);
12
 
12
 
13
  /** \brief Two by two float matrix template. 
13
  /** \brief Two by two float matrix template. 
14
 
14
 
15
	This class template is useful for various vector transformations 
15
	This class template is useful for various vector transformations 
16
	in the plane. */
16
	in the plane. */
17
  template<class V, class M>
17
  template<class V, class M>
18
  class ArithSqMat2x2Float: public ArithSqMatFloat<V,M, 2>
18
  class ArithSqMat2x2Float: public ArithSqMatFloat<V,M, 2>
19
  {
19
  {
20
  public:
20
  public:
21
 
21
 
22
    /// Vector type
22
    /// Vector type
23
    typedef V VectorType;
23
    typedef V VectorType;
24
 
24
 
25
    /// The type of a matrix element
25
    /// The type of a matrix element
26
    typedef typename V::ScalarType ScalarType;
26
    typedef typename V::ScalarType ScalarType;
27
 
27
 
28
  public:
28
  public:
29
 
29
 
30
    /// Construct a Mat2x2f from two Vec2f vectors.
30
    /// Construct a Mat2x2f from two Vec2f vectors.
31
    ArithSqMat2x2Float(V a, V b): 
31
    ArithSqMat2x2Float(V a, V b): 
32
      ArithSqMatFloat<V, M, 2> (a,b) {}
32
      ArithSqMatFloat<V, M, 2> (a,b) {}
33
 
33
 
34
    /// Construct a Mat2x2f from four scalars.
34
    /// Construct a Mat2x2f from four scalars.
35
    ArithSqMat2x2Float(ScalarType a, ScalarType b, 
35
    ArithSqMat2x2Float(ScalarType a, ScalarType b, 
36
		       ScalarType c, ScalarType d): 
36
		       ScalarType c, ScalarType d): 
37
      ArithSqMatFloat<V, M, 2>(V(a,b),V(c,d)) {}
37
      ArithSqMatFloat<V, M, 2>(V(a,b),V(c,d)) {}
38
  
38
  
39
    /// Construct the NAN matrix
39
    /// Construct the NAN matrix
40
    ArithSqMat2x2Float() {}
40
    ArithSqMat2x2Float() {}
41
 
41
 
42
    /// Construct a matrix from a single scalar value.
42
    /// Construct a matrix from a single scalar value.
43
    explicit ArithSqMat2x2Float(ScalarType a): 
43
    explicit ArithSqMat2x2Float(ScalarType a): 
44
      ArithSqMatFloat<V, M, 2>(a) {}
44
      ArithSqMatFloat<V, M, 2>(a) {}
45
  };
45
  };
46
 
46
 
47
  /** Compute the determinant of a Mat2x2f. This function is faster than
47
  /** Compute the determinant of a Mat2x2f. This function is faster than
48
      the generic determinant function for ArithSqMat */
48
      the generic determinant function for ArithSqMat */
49
  template<class V, class M>
49
  template<class V, class M>
50
  inline typename ArithSqMat2x2Float<V,M>::ScalarType 
50
  inline typename ArithSqMat2x2Float<V,M>::ScalarType 
51
  determinant(const ArithSqMat2x2Float<V,M> & m)
51
  determinant(const ArithSqMat2x2Float<V,M> & m)
52
  {
52
  {
53
    return m[0][0]*m[1][1]-m[0][1]*m[1][0];
53
    return m[0][0]*m[1][1]-m[0][1]*m[1][0];
54
  }
54
  }
55
 
55
 
56
  template<class V, class M>
56
  template<class V, class M>
57
  const M invert(const ArithSqMat2x2Float<V,M>& m)
57
  const M invert(const ArithSqMat2x2Float<V,M>& m)
58
  {
58
  {
59
    typename M::ScalarType det = determinant(m);
59
    typename M::ScalarType det = determinant(m);
60
    if( !is_tiny(fabs(det)))
60
    if( !is_tiny(fabs(det)))
61
      {
61
      {
62
	return M(m[1][1]/det, -m[0][1]/det,-m[1][0]/det, m[0][0]/det);
62
	return M(m[1][1]/det, -m[0][1]/det,-m[1][0]/det, m[0][0]/det);
63
      }
63
      }
64
    throw Mat2x2fException("Cannot invert matrix");
64
    throw Mat2x2fException("Cannot invert matrix");
65
  }
65
  }
66
 
66
 
67
 
67
 
68
}
68
}
69
#endif
69
#endif
70
 
70