667 |
khor |
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 |
|
|
|
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 |
|
|
|
21 |
CGLA_DERIVEEXCEPTION(Mat2x2fException);
|
|
|
22 |
|
|
|
23 |
/** \brief Two by two float matrix template.
|
|
|
24 |
|
|
|
25 |
This class template is useful for various vector transformations
|
|
|
26 |
in the plane. */
|
|
|
27 |
template<class V, class M>
|
|
|
28 |
class ArithSqMat2x2Float: public ArithSqMatFloat<V,M, 2>
|
|
|
29 |
{
|
|
|
30 |
public:
|
|
|
31 |
|
|
|
32 |
/// Vector type
|
|
|
33 |
typedef V VectorType;
|
|
|
34 |
|
|
|
35 |
/// The type of a matrix element
|
|
|
36 |
typedef typename V::ScalarType ScalarType;
|
|
|
37 |
|
|
|
38 |
public:
|
|
|
39 |
|
|
|
40 |
/// Construct a Mat2x2f from two Vec2f vectors.
|
|
|
41 |
ArithSqMat2x2Float(V a, V b):
|
|
|
42 |
ArithSqMatFloat<V, M, 2> (a,b) {}
|
|
|
43 |
|
|
|
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)) {}
|
|
|
48 |
|
|
|
49 |
/// Construct the NAN matrix
|
|
|
50 |
ArithSqMat2x2Float() {}
|
|
|
51 |
|
|
|
52 |
/// Construct a matrix from a single scalar value.
|
|
|
53 |
explicit ArithSqMat2x2Float(ScalarType a):
|
|
|
54 |
ArithSqMatFloat<V, M, 2>(a) {}
|
|
|
55 |
};
|
|
|
56 |
|
|
|
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 |
}
|
|
|
65 |
|
|
|
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 |
}
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
}
|
|
|
79 |
#endif
|