Subversion Repositories gelsvn

Rev

Rev 2 | Rev 89 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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