App
TTransform.hpp
Go to the documentation of this file.
1 /*
2  * TTRansform.hpp
3  *
4  * Created on: Apr 5, 2013
5  * Author: xxx
6  */
7 
8 namespace app {
9  class Matrix3x3;
10  template <typename TPIXEL, typename TCHANNEL, typename TINTERPOLATOR> class TTransform;
11 }
12 
13 
14 #ifndef TTRANSFORM_HPP_
15 #define TTRANSFORM_HPP_
16 
17 #include <cmath>
18 
19 #include "../typedefs.hpp"
20 #include "../bitmaps/TBitmap.hpp"
21 #include "../interpolators_fast/TNearestFInt.hpp"
22 #include "../interpolators_fast/TLinearFInt.hpp"
23 #include "../interpolators_fast/TCubicFInt.hpp"
24 
25 namespace app {
26 
27 
28 class Matrix3x3 {
29 public:
30 
31  double a11, a12, a13;
32  double a21, a22, a23;
33  double a31, a32, a33;
34 
35 
36  Matrix3x3() {};
37 
38  Matrix3x3(double aa11, double aa12, double aa13, double aa21, double aa22, double aa23, double aa31, double aa32, double aa33) {
39  a11 = aa11;
40  a12 = aa12;
41  a13 = aa13;
42 
43  a21 = aa21;
44  a22 = aa22;
45  a23 = aa23;
46 
47  a31 = aa31;
48  a32 = aa32;
49  a33 = aa33;
50  }
51 
52  static Matrix3x3 multiply(const Matrix3x3 &m1, const Matrix3x3 &m2) {
53  Matrix3x3 r;
54 
55  r.a11 = m1.a11 * m2.a11 + m1.a12 * m2.a21 + m1.a13 * m2.a31;
56  r.a12 = m1.a11 * m2.a12 + m1.a12 * m2.a22 + m1.a13 * m2.a32;
57  r.a13 = m1.a11 * m2.a13 + m1.a12 * m2.a23 + m1.a13 * m2.a33;
58 
59  r.a21 = m1.a21 * m2.a11 + m1.a22 * m2.a21 + m1.a23 * m2.a31;
60  r.a22 = m1.a21 * m2.a12 + m1.a22 * m2.a22 + m1.a23 * m2.a32;
61  r.a23 = m1.a21 * m2.a13 + m1.a22 * m2.a23 + m1.a23 * m2.a33;
62 
63  r.a31 = m1.a31 * m2.a11 + m1.a32 * m2.a21 + m1.a33 * m2.a31;
64  r.a32 = m1.a31 * m2.a12 + m1.a32 * m2.a22 + m1.a33 * m2.a32;
65  r.a33 = m1.a31 * m2.a13 + m1.a32 * m2.a23 + m1.a33 * m2.a33;
66 
67  return r;
68  }
69 
70  static Matrix3x3 inverseOf(const Matrix3x3 &m) {
71  Matrix3x3 r;
72 
73  double factor = 1 / (m.a11 * (m.a22 * m.a33 - m.a23 * m.a32) - m.a12 * (m.a21 * m.a33 - m.a23 * m.a31) + m.a13 * (m.a21 * m.a32 - m.a22 * m.a31));
74 
75  r.a11 = (m.a22 * m.a33 - m.a23 * m.a32) * factor;
76  r.a12 = (m.a13 * m.a32 - m.a12 * m.a33) * factor;
77  r.a13 = (m.a12 * m.a23 - m.a13 * m.a22) * factor;
78 
79  r.a21 = (m.a23 * m.a31 - m.a21 * m.a33) * factor;
80  r.a22 = (m.a11 * m.a33 - m.a13 * m.a31) * factor;
81  r.a23 = (m.a13 * m.a21 - m.a11 * m.a23) * factor;
82 
83  r.a31 = (m.a21 * m.a32 - m.a22 * m.a31) * factor;
84  r.a32 = (m.a12 * m.a31 - m.a11 * m.a32) * factor;
85  r.a33 = (m.a11 * m.a22 - m.a12 * m.a21) * factor;
86 
87  return r;
88  }
89 
90  static Matrix3x3 transposeOf(const Matrix3x3 &m) {
91  Matrix3x3 r;
92 
93  r.a11 = m.a11;
94  r.a12 = m.a21;
95  r.a13 = m.a31;
96 
97  r.a21 = m.a12;
98  r.a22 = m.a22;
99  r.a23 = m.a32;
100 
101  r.a31 = m.a13;
102  r.a32 = m.a23;
103  r.a33 = m.a33;
104 
105  return r;
106  }
107 
108 };
109 
110 
111 template <typename TPIXEL, typename TCHANNEL, typename TINTERPOLATOR>
112 class TTransform {
113 public:
115 
116  }
117 
118  virtual ~TTransform();
119 
120  virtual int applyMatrix(TBitmap<TPIXEL, TCHANNEL> &dst, const TBitmap<TPIXEL, TCHANNEL> & src, const Matrix3x3 &m);
121 
122  Matrix3x3 getTranslationMatrix(double dx, double dy) {
123  return Matrix3x3( 1, 0, 0,
124  0, 1, 0,
125  dx, dy, 1);
126  }
127 
128  Matrix3x3 getRotationMatrix(double alpha) {
129  return Matrix3x3( cos(alpha), sin(alpha), 0,
130  -sin(alpha), cos(alpha), 0,
131  0, 0, 1);
132  }
133 
134  Matrix3x3 getScaleMatrix(double x, double y) {
135  return Matrix3x3( x, 0, 0,
136  0, y, 0,
137  0, 0, 1);
138  }
139 
140  Matrix3x3 getShearMatrix(double x, double y) {
141  return Matrix3x3( 1, y, 0,
142  x, 1, 0,
143  0, 0, 1);
144  }
145 
146 
151  Matrix3x3 getProjectiveMatrix(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
152  Matrix3x3 m;
153  m.a31 = ((x1 - x2 + x4 - x3) * (y3 - y4) - (y1 - y2 + y4 - y3) * (x3 - x4)) / ((x2 - x4) * (y3 - y4) - (x3 - x4) * (y2 - y4));
154  m.a32 = ((y1 - y2 + y4 - y3) * (x2 - x4) - (x1 - x2 + x4 - x3) * (y2 - y4)) / ((x2 - x4) * (y3 - y4) - (x3 - x4) * (y2 - y4));
155 
156  m.a11 = x2 - x1 + m.a31 * x2;
157  m.a12 = x3 - x1 + m.a32 * x3;
158  m.a13 = x1;
159 
160  m.a21 = y2 - y1 + m.a31 * y2;
161  m.a22 = y3 - y1 + m.a32 * y3;
162  m.a23 = y1;
163 
164  m.a33 = 1;
165 
166  return Matrix3x3::transposeOf(m);
167  }
168 };
169 
170 
171 
172 } /* namespace app */
173 #endif /* TTRANSFORM_HPP_ */