1 #ifndef SDK_MATRIX_3X3_H_
11 #define SDK_MATRIX_3X3_H_
13 #define _USE_MATH_DEFINES
24 template <
class _SDKMatrix3X3>
29 typedef _SDKMatrix3X3 SDKMatrix3X3;
30 typedef typename SDKMatrix3X3::coordinate_type coordinate_type;
38 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = 0;
39 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = 0;
40 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = 1;
45 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = 0;
46 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = 0;
47 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = 1;
51 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = coordinate_type(m._13);
52 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = coordinate_type(m._23);
53 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = coordinate_type(m._33);
57 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = coordinate_type(m._13);
58 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = coordinate_type(m._23);
59 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = coordinate_type(m._33);
62 double s21,
double s22,
double s23,
63 double s31,
double s32,
double s33) noexcept
65 this->_11 = coordinate_type(s11); this->_12 = coordinate_type(s12); this->_13 = coordinate_type(s13);
66 this->_21 = coordinate_type(s21); this->_22 = coordinate_type(s22); this->_23 = coordinate_type(s23);
67 this->_31 = coordinate_type(s31); this->_32 = coordinate_type(s32); this->_33 = coordinate_type(s33);
72 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = 0;
73 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = 0;
74 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = 1;
80 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = 0;
81 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = 0;
82 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = 1;
88 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = coordinate_type(m._13);
89 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = coordinate_type(m._23);
90 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = coordinate_type(m._33);
96 this->_11 = coordinate_type(m._11); this->_12 = coordinate_type(m._12); this->_13 = coordinate_type(m._13);
97 this->_21 = coordinate_type(m._21); this->_22 = coordinate_type(m._22); this->_23 = coordinate_type(m._23);
98 this->_31 = coordinate_type(m._31); this->_32 = coordinate_type(m._32); this->_33 = coordinate_type(m._33);
102 const SDKMatrix3X3& get()
const noexcept
110 this->_11 = this->_22 = this->_33 = 1;
111 this->_12 = this->_13 = this->_21 = this->_23 = this->_31 = this->_32 = 0;
118 return (
double(this->_11) * this->_22 * this->_33 -
119 double(this->_11) * this->_23 * this->_32 -
120 double(this->_12) * this->_21 * this->_33 +
121 double(this->_12) * this->_23 * this->_31 +
122 double(this->_13) * this->_21 * this->_32 -
123 double(this->_13) * this->_22 * this->_31);
129 template <
class Po
int>
132 return Rotate(angle_deg, center.x, center.y);
135 _MyType&
Rotate(
double angle_deg,
double center_x,
double center_y) noexcept
138 double angle_rad = (angle_deg / 180) *
kPI;
139 double s = sin(angle_rad);
140 double c = cos(angle_rad);
142 this->_11 = coordinate_type(c);
143 this->_12 = coordinate_type(s);
144 this->_21 = coordinate_type(-s);
145 this->_22 = coordinate_type(c);
146 this->_31 = coordinate_type(-center_x * (c - 1) + center_y * s);
147 this->_32 = coordinate_type(-center_y * (c - 1) - center_x * s);
156 this->_31 = coordinate_type(shift_x);
157 this->_32 = coordinate_type(shift_y);
165 this->_11 = coordinate_type(scale_x);
166 this->_22 = coordinate_type(scale_y);
177 res.x = double(this->_11) * point.x + double(this->_21) * point.y + this->_31;
178 res.y = double(this->_12) * point.x + double(this->_22) * point.y + this->_32;
179 double w = double(this->_13) * point.x + double(this->_23) * point.y + this->_33;
187 res_d = Transform(res_d);
188 SDKPointF2D res = {float(res_d.x), float(res_d.y)};
193 SDKPointD2D res_d = {double(point.x), double(point.y)};
194 res_d = Transform(res_d);
196 RoundToNearest<SDKInt32>(res_d.x),
197 RoundToNearest<SDKInt32>(res_d.y)};
203 SDKPointD2D pt_nw = { rect.x, rect.y + rect.height };
204 SDKPointD2D pt_se = { rect.x + rect.width, rect.y };
205 SDKPointD2D pt_ne = { rect.x + rect.width, rect.y + rect.height };
206 pt_sw = Transform(pt_sw);
207 pt_nw = Transform(pt_nw);
208 pt_se = Transform(pt_se);
209 pt_ne = Transform(pt_ne);
211 res.x = min4dbl(pt_sw.x, pt_nw.x, pt_se.x, pt_ne.x);
212 res.y = min4dbl(pt_sw.y, pt_nw.y, pt_se.y, pt_ne.y);
213 res.width = max4dbl(pt_sw.x, pt_nw.x, pt_se.x, pt_ne.x) - res.x;
214 res.height = max4dbl(pt_sw.y, pt_nw.y, pt_se.y, pt_ne.y) - res.y;
220 double(rect.x), double(rect.y),
221 double(rect.width), double(rect.height)};
222 res_d = Transform(res_d);
227 float(res_d.height)};
233 double(rect.x), double(rect.y),
234 double(rect.width), double(rect.height)};
235 res_d = Transform(res_d);
237 RoundToNearest<SDKInt32>(res_d.x),
238 RoundToNearest<SDKInt32>(res_d.y),
239 RoundToNearest<SDKUInt32>(res_d.width),
240 RoundToNearest<SDKUInt32>(res_d.height)
249 v = this->_12; this->_12 = this->_21; this->_21 = coordinate_type(v);
250 v = this->_13; this->_13 = this->_31; this->_31 = coordinate_type(v);
251 v = this->_23; this->_23 = this->_32; this->_32 = coordinate_type(v);
255 static _MyType
Transpose(
const SDKMatrix3X3& m2) noexcept
266 a = double(this->_11) * m2._11 + double(this->_12) * m2._21 + double(this->_13) * m2._31;
267 b = double(this->_11) * m2._12 + double(this->_12) * m2._22 + double(this->_13) * m2._32;
268 c = double(this->_11) * m2._13 + double(this->_12) * m2._23 + double(this->_13) * m2._33;
269 this->_11 = coordinate_type(a);
270 this->_12 = coordinate_type(b);
271 this->_13 = coordinate_type(c);
273 a = double(this->_21) * m2._11 + double(this->_22) * m2._21 + double(this->_23) * m2._31;
274 b = double(this->_21) * m2._12 + double(this->_22) * m2._22 + double(this->_23) * m2._32;
275 c = double(this->_21) * m2._13 + double(this->_22) * m2._23 + double(this->_23) * m2._33;
276 this->_21 = coordinate_type(a);
277 this->_22 = coordinate_type(b);
278 this->_23 = coordinate_type(c);
280 a = double(this->_31) * m2._11 + double(this->_32) * m2._21 + double(this->_33) * m2._31;
281 b = double(this->_31) * m2._12 + double(this->_32) * m2._22 + double(this->_33) * m2._32;
282 c = double(this->_31) * m2._13 + double(this->_32) * m2._23 + double(this->_33) * m2._33;
283 this->_31 = coordinate_type(a);
284 this->_32 = coordinate_type(b);
285 this->_33 = coordinate_type(c);
289 static _MyType
Multiply(
const SDKMatrix3X3& m1,
const SDKMatrix3X3& m2)
299 return this->
Invert(*
this);
306 inverted_matrix = inv;
312 template <
class _SDKMatrix3X3>
320 inv._11 = +double(this->_33) * this->_22 - double(this->_32) * this->_23;
321 inv._12 = -double(this->_33) * this->_12 + double(this->_32) * this->_13;
322 inv._13 = +double(this->_23) * this->_12 - double(this->_22) * this->_13;
324 double det = this->_11 * inv._11 + this->_21 * inv._12 + this->_31 * inv._13;
329 inv._21 = -double(this->_33) * this->_21 + double(this->_31) * this->_23;
330 inv._22 = +double(this->_33) * this->_11 - double(this->_31) * this->_13;
331 inv._23 = -double(this->_23) * this->_11 + double(this->_21) * this->_13;
332 inv._31 = +double(this->_32) * this->_21 - double(this->_31) * this->_22;
333 inv._32 = -double(this->_32) * this->_11 + double(this->_31) * this->_12;
334 inv._33 = +double(this->_22) * this->_11 - double(this->_21) * this->_12;
336 inverted_matrix._11 = coordinate_type(inv._11 * det);
337 inverted_matrix._12 = coordinate_type(inv._12 * det);
338 inverted_matrix._13 = coordinate_type(inv._13 * det);
340 inverted_matrix._21 = coordinate_type(inv._21 * det);
341 inverted_matrix._22 = coordinate_type(inv._22 * det);
342 inverted_matrix._23 = coordinate_type(inv._23 * det);
344 inverted_matrix._31 = coordinate_type(inv._31 * det);
345 inverted_matrix._32 = coordinate_type(inv._32 * det);
346 inverted_matrix._33 = coordinate_type(inv._33 * det);
351 typedef Matrix3X3T<SDKMatrix3X3> Matrix3X3, CMatrix3X3;
352 typedef Matrix3X3T<SDKMatrixD3X3> MatrixD3X3, CMatrixD3X3;
353 typedef Matrix3X3T<SDKMatrixF3X3> MatrixF3X3, CMatrixF3X3;
357 #endif // SDK_MATRIX_3X3_H_