1
2 #ifndef _global_h
3 # include "global.h"
4 #endif
5 #ifndef _mat2_h
6 # include "mat2.h"
7 #endif
8
9
10 const Mat2 &Mat2::operator*=(const Mat2 &m)
11 {
12 Real h11=r11;
13 Real h12=r12;
14 Real h21=r21;
15 Real h22=r22;
16 r11 = h11*m.r11 + h12*m.r21;
17 r21 = h21*m.r11 + h22*m.r21;
18 r12 = h11*m.r12 + h12*m.r22;
19 r22 = h21*m.r12 + h22*m.r22;
20 tx += h11*m.tx + h12*m.ty;
21 ty += h21*m.tx + h22*m.ty;
22 return *this;
23 }
24
Invert(Mat2 * m)25 void Mat2::Invert(Mat2 *m) const
26 {
27 Real detA = r11*r22-r12*r21;
28
29 m->r11 = r22/detA;
30 m->r12 = -r12/detA;
31 m->r21 = -r21/detA;
32 m->r22 = r11/detA;
33 m->tx = -m->r11*tx -m->r12*ty;
34 m->ty = -m->r21*tx -m->r22*ty;
35 }
36
Split(Real * sh_p,Real * sx_p,Real * sy_p,Real * angle_p,Real * tx_p,Real * ty_p)37 void Mat2::Split(Real *sh_p, Real *sx_p, Real *sy_p, Real *angle_p, Real *tx_p, Real *ty_p) const
38 {
39 Real cosa; // Rotation
40
41 if (r21) {
42 Real r1121 = r11/r21;
43 cosa = r1121 / sqrt(1+r1121*r1121);
44 *angle_p = acos(cosa);
45 *sx_p = r11/cosa;
46
47 *sy_p = (r11*r22/r21-r12)/(sin(*angle_p/180*M_PI)+r11*cosa/r21);
48 *sh_p = (r22-cosa**sy_p)/r21;
49 }
50 else {
51 if (r11) {
52 *sx_p = r11;
53 *angle_p = 0.0;
54 cosa = 1.0;
55 *sh_p = r12/r11;
56 *sy_p = r22;
57 }
58 else {
59 *sx_p = 0;
60 *sh_p = 0; // sowieso egal
61
62 Real r2212 = r22/r12;
63 cosa = -r2212 / sqrt(1+r2212*r2212);
64 *angle_p = acos(cosa);
65 *sy_p = r22/cosa;
66 }
67 }
68
69 // wenn beide Skalierungen negativ: Punktspiegelung
70 if (*sx_p<0 && *sy_p<0) {
71 *sx_p = *sx_p * -1;
72 *sy_p = *sy_p * -1;
73 if (*angle_p>0) *angle_p-=180.0;
74 else *angle_p+=180.0;
75 }
76 if (fabs(*sx_p-1)<EPS) *sx_p=1.0;
77 if (fabs(*sy_p-1)<EPS) *sy_p=1.0;
78 if (fabs(*sx_p-*sy_p)<EPS) *sy_p=*sx_p;
79 if (fabs(*sh_p)<EPS) *sh_p=0.0;
80 if (fabs(*angle_p)<EPS) *angle_p=0.0;
81 *tx_p = (fabs(tx)>EPS)?tx:0;
82 *ty_p = (fabs(ty)>EPS)?ty:0;
83 }
84
85
86 // void Mat2::Print() const
87 // {
88 // cout << "Matrix r11, r12, tx " <<r11<<" "<<r12<<" "<<tx<< endl;
89 // cout << " r21, r22, ty " <<r21<<" "<<r22<<" "<<ty<< endl;
90 // }
91