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