Subversion Repositories gelsvn

Rev

Rev 12 | Go to most recent revision | Details | 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
 
11
	CGLA_DERIVEEXCEPTION(Mat2x2fException);
12
 
13
	/** Two by two float matrix. This class is useful for various 
14
			vector transformations in the plane. */
15
 
16
	template<class V, class M>
17
	class ArithSqMat2x2Float: public ArithSqMatFloat<V,M, 2>
18
	{
19
	public:
20
 
21
		/// Vector type
22
		typedef V VectorType;
23
 
24
		/// The type of a matrix element
25
		typedef typename V::ScalarType ScalarType;
26
 
27
	public:
28
 
29
		/// Construct a Mat2x2f from two Vec2f vectors.
30
		ArithSqMat2x2Float(V a, V b): 
31
			ArithSqMatFloat<V, M, 2> (a,b) {}
32
 
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)) {}
37
 
38
		/// Construct the 0 matrix
39
		ArithSqMat2x2Float() {}
40
 
41
	};
42
 
43
	/** Compute the determinant of a Mat2x2f. This function is faster than
44
			the generic determinant function for ArithSqMat */
45
	template<class V, class M>
46
	inline typename ArithSqMat2x2Float<V,M>::ScalarType 
47
	determinant(const ArithSqMat2x2Float<V,M> & m)
48
	{
49
		return m[0][0]*m[1][1]-m[0][1]*m[1][0];
50
	}
51
 
52
	template<class V, class M>
53
	const M invert(const ArithSqMat2x2Float<V,M>& m)
54
	{
55
		float det = determinant(m);
56
		if( !is_tiny(fabs(det)))
57
			{
58
				return M(m[1][1]/det, -m[0][1]/det,-m[1][0]/det, m[0][0]/det);
59
			}
60
		throw Mat2x2fException("Cannot invert matrix");
61
	}
62
 
63
 
64
}
65
#endif