synfig-core  1.0.3
matrix.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
21 /* ========================================================================= */
22 
23 /* === S T A R T =========================================================== */
24 
25 #ifndef __SYNFIG_MATRIX_H
26 #define __SYNFIG_MATRIX_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 #include "angle.h"
31 #include "real.h"
32 #include "vector.h"
33 #include "string.h"
34 #include <cassert>
35 #include <math.h>
36 #include <iostream>
37 #include <ETL/stringf>
38 
39 /* === M A C R O S ========================================================= */
40 
41 // For some reason isnan() isn't working on macosx any more.
42 // This is a quick fix.
43 #if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
44 #ifdef isnan
45 #undef isnan
46 #endif
47 inline bool isnan(double x) { return x != x; }
48 inline bool isnan(float x) { return x != x; }
49 #define SYNFIG_ISNAN_FIX 1
50 #endif
51 
52 #define COUT_MATRIX(m) \
53  cout<<"["<<m.m00<<"]["<<m.m01<<"]["<<m.m02<<"]"<<endl; \
54  cout<<"["<<m.m10<<"]["<<m.m11<<"]["<<m.m12<<"]"<<endl; \
55  cout<<"["<<m.m20<<"]["<<m.m21<<"]["<<m.m22<<"]"<<endl
56 
57 /* === T Y P E D E F S ===================================================== */
58 
59 /* === C L A S S E S & S T R U C T S ======================================= */
60 
61 namespace synfig {
62 
66 class Matrix
67 {
68 public:
69  typedef Real value_type;
70 
71 public:
76  // Index convention
77  // 00 01 02
78  // 10 11 12
79  // 20 21 22
80  // vectors are premultiplied when the matrix transformation is applied
81  // we consider the vectors as [1]x[3] arrays.
82  // [1]x[3] * [3]x[3] = [1]x[3]
83  // vector * matrix = vector
84  // In affine transformation matrixes the values of
85  // m02=0, m12=0 and m22=1 for non projective transformations.
86 
88  Matrix();
89 
94  ):
95  m00(m00), m01(m01), m02(m02),
96  m10(m10), m11(m11), m12(m12),
97  m20(m20), m21(m21), m22(m22)
98  { }
99 
100  Matrix(Vector axis_x, Vector axis_y, Vector offset):
101  m00(axis_x[0]), m01(axis_x[1]), m02(0),
102  m10(axis_y[0]), m11(axis_y[1]), m12(0),
103  m20(offset[0]), m21(offset[1]), m22(1)
104  { }
105 
106  Vector get_axis_x()const { return Vector(m00, m01); }
107  Vector get_axis_y()const { return Vector(m10, m11); }
108  Vector get_offset()const { return Vector(m20, m21); }
109 
111  Matrix &set_identity();
112 
113  bool is_identity() const;
114 
119  Matrix &set_scale(const value_type &sx, const value_type &sy);
120 
124  Matrix &set_scale(const value_type &sxy);
125 
129  Matrix &set_scale(const Vector &s);
130 
134  Matrix &set_rotate(const Angle &a);
135 
139  Matrix &set_translate(const Vector &t);
140 
146 
150  Vector get_transformed(const Vector &v, bool translate = true)const;
151  void get_transformed(value_type &out_x, value_type &out_y, const value_type x, const value_type y, bool translate = true)const;
152 
156  Matrix operator*=(const Matrix &rhs);
157 
161  Matrix operator*=(const value_type &rhs);
162 
166  Matrix operator+=(const Matrix &rhs);
167 
171  Matrix operator*(const Matrix &rhs)const;
172 
176  Matrix operator*(const value_type &rhs)const;
177 
181  Matrix
182  operator+(const Matrix &rhs)const;
183 
184  bool is_invertible()const;
185 
186  // (m00 m01 0) 1 ( m11 ) ( -m01 ) ( 0 )
187  // inverse (m10 m11 0) = ----- x ( -m10 ) ( m00 ) ( 0 )
188  // (m20 m21 1) m00m11-m01m10 (m10m21-m11m20) (m01m20-m00m21) (m00m11-m01m10)
189  Matrix &invert();
190 
194  String get_string(int spaces = 0, String before = String(), String after = String())const;
195 };
196 
197 }; // END of namespace synfig
198 
199 #endif