Subversion Repositories gelsvn

Rev

Rev 12 | Details | Compare with Previous | Last modification | View Log | RSS feed

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