Subversion Repositories gelsvn

Rev

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

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