]>
git.saurik.com Git - wxWidgets.git/blob - src/common/matrix.cpp
2 // Purpose: wxTransformMatrix class
3 // Author: Chris Breeze, Julian Smart
4 // Modified by: Klaas Holwerda
7 // Copyright: (c) Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "matrix.h"
15 // Note: this is intended to be used in wxDC at some point to replace
16 // the current system of scaling/translation. It is not yet used.
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
29 #include "wx/matrix.h"
32 const double pi
= 3.1415926535;
34 wxTransformMatrix::wxTransformMatrix(void)
41 wxTransformMatrix::wxTransformMatrix(const wxTransformMatrix
& mat
)
46 double wxTransformMatrix::GetValue(int col
, int row
) const
48 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
51 return m_matrix
[col
][row
];
54 void wxTransformMatrix::SetValue(int col
, int row
, double value
)
56 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
59 m_matrix
[col
][row
] = value
;
60 m_isIdentity
= IsIdentity1();
63 void wxTransformMatrix::operator = (const wxTransformMatrix
& mat
)
66 for (i
= 0; i
< 3; i
++)
68 for (j
= 0; j
< 3; j
++)
70 m_matrix
[i
][j
] = mat
.m_matrix
[i
][j
];
73 m_isIdentity
= mat
.m_isIdentity
;
76 bool wxTransformMatrix::operator == (const wxTransformMatrix
& mat
)
78 if (m_isIdentity
==TRUE
&& mat
.m_isIdentity
==TRUE
)
82 for (i
= 0; i
< 3; i
++)
84 for (j
= 0; j
< 3; j
++)
86 if (m_matrix
[i
][j
] != mat
.m_matrix
[i
][j
])
93 bool wxTransformMatrix::operator != (const wxTransformMatrix
& mat
)
95 return (! ((*this) == mat
));
98 double& wxTransformMatrix::operator()(int col
, int row
)
100 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
101 return m_matrix
[0][0];
103 return m_matrix
[col
][row
];
106 double wxTransformMatrix::operator()(int col
, int row
) const
108 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
111 return m_matrix
[col
][row
];
115 bool wxTransformMatrix::Invert(void)
117 double inverseMatrix
[3][3];
119 // calculate the adjoint
120 inverseMatrix
[0][0] = wxCalculateDet(m_matrix
[1][1],m_matrix
[2][1],m_matrix
[1][2],m_matrix
[2][2]);
121 inverseMatrix
[0][1] = -wxCalculateDet(m_matrix
[0][1],m_matrix
[2][1],m_matrix
[0][2],m_matrix
[2][2]);
122 inverseMatrix
[0][2] = wxCalculateDet(m_matrix
[0][1],m_matrix
[1][1],m_matrix
[0][2],m_matrix
[1][2]);
124 inverseMatrix
[1][0] = -wxCalculateDet(m_matrix
[1][0],m_matrix
[2][0],m_matrix
[1][2],m_matrix
[2][2]);
125 inverseMatrix
[1][1] = wxCalculateDet(m_matrix
[0][0],m_matrix
[2][0],m_matrix
[0][2],m_matrix
[2][2]);
126 inverseMatrix
[1][2] = -wxCalculateDet(m_matrix
[0][0],m_matrix
[1][0],m_matrix
[0][2],m_matrix
[1][2]);
128 inverseMatrix
[2][0] = wxCalculateDet(m_matrix
[1][0],m_matrix
[2][0],m_matrix
[1][1],m_matrix
[2][1]);
129 inverseMatrix
[2][1] = -wxCalculateDet(m_matrix
[0][0],m_matrix
[2][0],m_matrix
[0][1],m_matrix
[2][1]);
130 inverseMatrix
[2][2] = wxCalculateDet(m_matrix
[0][0],m_matrix
[1][0],m_matrix
[0][1],m_matrix
[1][1]);
132 // now divide by the determinant
133 double det
= m_matrix
[0][0] * inverseMatrix
[0][0] + m_matrix
[0][1] * inverseMatrix
[1][0] + m_matrix
[0][2] * inverseMatrix
[2][0];
136 inverseMatrix
[0][0] /= det
; inverseMatrix
[1][0] /= det
; inverseMatrix
[2][0] /= det
;
137 inverseMatrix
[0][1] /= det
; inverseMatrix
[1][1] /= det
; inverseMatrix
[2][1] /= det
;
138 inverseMatrix
[0][2] /= det
; inverseMatrix
[1][2] /= det
; inverseMatrix
[2][2] /= det
;
141 for (i
= 0; i
< 3; i
++)
143 for (j
= 0; j
< 3; j
++)
145 m_matrix
[i
][j
] = inverseMatrix
[i
][j
];
148 m_isIdentity
= IsIdentity1();
157 // Make into identity matrix
158 bool wxTransformMatrix::Identity(void)
160 m_matrix
[0][0] = m_matrix
[1][1] = m_matrix
[2][2] = 1.0;
161 m_matrix
[1][0] = m_matrix
[2][0] = m_matrix
[0][1] = m_matrix
[2][1] = m_matrix
[0][2] = m_matrix
[1][2] = 0.0;
167 // Scale by scale (isotropic scaling i.e. the same in x and y):
169 // matrix' = | 0 scale 0 | x matrix
172 bool wxTransformMatrix::Scale(double scale
)
175 for (i
= 0; i
< 3; i
++)
177 for (j
= 0; j
< 3; j
++)
179 m_matrix
[i
][j
] *= scale
;
182 m_isIdentity
= IsIdentity1();
188 // scale a matrix in 2D
194 wxTransformMatrix
& wxTransformMatrix::Scale(const double &xs
, const double &ys
,const double &xc
, const double &yc
)
196 double r00
,r10
,r20
,r01
,r11
,r21
;
200 double tx
=xc
*(1-xs
);
201 double ty
=yc
*(1-ys
);
209 else if (xc
!=0 || yc
!=0)
211 double tx
=xc
*(1-xs
);
212 double ty
=yc
*(1-ys
);
213 r00
= xs
* m_matrix
[0][0];
214 r10
= xs
* m_matrix
[1][0];
215 r20
= xs
* m_matrix
[2][0] + tx
;
216 r01
= ys
* m_matrix
[0][1];
217 r11
= ys
* m_matrix
[1][1];
218 r21
= ys
* m_matrix
[2][1] + ty
;
222 r00
= xs
* m_matrix
[0][0];
223 r10
= xs
* m_matrix
[1][0];
224 r20
= xs
* m_matrix
[2][0];
225 r01
= ys
* m_matrix
[0][1];
226 r11
= ys
* m_matrix
[1][1];
227 r21
= ys
* m_matrix
[2][1];
230 m_matrix
[0][0] = r00
;
231 m_matrix
[1][0] = r10
;
232 m_matrix
[2][0] = r20
;
233 m_matrix
[0][1] = r01
;
234 m_matrix
[1][1] = r11
;
235 m_matrix
[2][1] = r21
;
238 // first translate to origin O
239 (*this).Translate(-x_cen, -y_cen);
241 // now do the scaling
242 wxTransformMatrix scale;
243 scale.m_matrix[0][0] = x_fac;
244 scale.m_matrix[1][1] = y_fac;
245 scale.m_isIdentity = IsIdentity1();
247 *this = scale * (*this);
249 // translate back from origin to x_cen, y_cen
250 (*this).Translate(x_cen, y_cen);
253 m_isIdentity
= IsIdentity1();
259 // mirror a matrix in x, y
264 wxTransformMatrix
& wxTransformMatrix::Mirror(bool x
, bool y
)
266 wxTransformMatrix temp
;
269 temp
.m_matrix
[1][1] = -1;
270 temp
.m_isIdentity
=FALSE
;
274 temp
.m_matrix
[0][0] = -1;
275 temp
.m_isIdentity
=FALSE
;
278 *this = temp
* (*this);
279 m_isIdentity
= IsIdentity1();
283 // Translate by dx, dy:
285 // matrix' = | 0 1 dy | x matrix
288 bool wxTransformMatrix::Translate(double dx
, double dy
)
291 for (i
= 0; i
< 3; i
++)
292 m_matrix
[i
][0] += dx
* m_matrix
[i
][2];
293 for (i
= 0; i
< 3; i
++)
294 m_matrix
[i
][1] += dy
* m_matrix
[i
][2];
296 m_isIdentity
= IsIdentity1();
301 // Rotate clockwise by the given number of degrees:
303 // matrix' = | -sin cos 0 | x matrix
305 bool wxTransformMatrix::Rotate(double degrees
)
307 Rotate(-degrees
,0,0);
311 // counter clockwise rotate around a point
313 // cos(r) -sin(r) x(1-cos(r))+y(sin(r)
314 // sin(r) cos(r) y(1-cos(r))-x(sin(r)
316 wxTransformMatrix
& wxTransformMatrix::Rotate(const double °rees
, const double &x
, const double &y
)
318 double angle
= degrees
* pi
/ 180.0;
319 double c
= cos(angle
);
320 double s
= sin(angle
);
321 double r00
,r10
,r20
,r01
,r11
,r21
;
325 double tx
= x
*(1-c
)+y
*s
;
326 double ty
= y
*(1-c
)-x
*s
;
334 else if (x
!=0 || y
!=0)
336 double tx
= x
*(1-c
)+y
*s
;
337 double ty
= y
*(1-c
)-x
*s
;
338 r00
= c
* m_matrix
[0][0] - s
* m_matrix
[0][1] + tx
* m_matrix
[0][2];
339 r10
= c
* m_matrix
[1][0] - s
* m_matrix
[1][1] + tx
* m_matrix
[1][2];
340 r20
= c
* m_matrix
[2][0] - s
* m_matrix
[2][1] + tx
;// * m_matrix[2][2];
341 r01
= c
* m_matrix
[0][1] + s
* m_matrix
[0][0] + ty
* m_matrix
[0][2];
342 r11
= c
* m_matrix
[1][1] + s
* m_matrix
[1][0] + ty
* m_matrix
[1][2];
343 r21
= c
* m_matrix
[2][1] + s
* m_matrix
[2][0] + ty
;// * m_matrix[2][2];
347 r00
= c
* m_matrix
[0][0] - s
* m_matrix
[0][1];
348 r10
= c
* m_matrix
[1][0] - s
* m_matrix
[1][1];
349 r20
= c
* m_matrix
[2][0] - s
* m_matrix
[2][1];
350 r01
= c
* m_matrix
[0][1] + s
* m_matrix
[0][0];
351 r11
= c
* m_matrix
[1][1] + s
* m_matrix
[1][0];
352 r21
= c
* m_matrix
[2][1] + s
* m_matrix
[2][0];
355 m_matrix
[0][0] = r00
;
356 m_matrix
[1][0] = r10
;
357 m_matrix
[2][0] = r20
;
358 m_matrix
[0][1] = r01
;
359 m_matrix
[1][1] = r11
;
360 m_matrix
[2][1] = r21
;
363 wxTransformMatrix rotate;
364 rotate.m_matrix[2][0] = tx;
365 rotate.m_matrix[2][1] = ty;
367 rotate.m_matrix[0][0] = c;
368 rotate.m_matrix[0][1] = s;
370 rotate.m_matrix[1][0] = -s;
371 rotate.m_matrix[1][1] = c;
373 rotate.m_isIdentity=false;
374 *this = rotate * (*this);
376 m_isIdentity
= IsIdentity1();
381 // Transform a point from logical to device coordinates
382 bool wxTransformMatrix::TransformPoint(double x
, double y
, double& tx
, double& ty
) const
386 tx
= x
; ty
= y
; return TRUE
;
389 tx
= x
* m_matrix
[0][0] + y
* m_matrix
[1][0] + m_matrix
[2][0];
390 ty
= x
* m_matrix
[0][1] + y
* m_matrix
[1][1] + m_matrix
[2][1];
395 // Transform a point from device to logical coordinates.
398 // wxTransformMatrix mat = dc.GetTransformation();
400 // mat.InverseTransformPoint(x, y, x1, y1);
402 // dc.LogicalToDevice(x, y, x1, y1);
403 // The latter is slightly less efficient if we're doing several
404 // conversions, since the matrix is inverted several times.
405 bool wxTransformMatrix::InverseTransformPoint(double x
, double y
, double& tx
, double& ty
) const
409 tx
= x
; ty
= y
; return TRUE
;
412 double z
= (1.0 - m_matrix
[0][2] * x
- m_matrix
[1][2] * y
) / m_matrix
[2][2];
418 tx
= x
* m_matrix
[0][0] + y
* m_matrix
[1][0] + z
* m_matrix
[2][0];
419 ty
= x
* m_matrix
[0][1] + y
* m_matrix
[1][1] + z
* m_matrix
[2][1];
423 wxTransformMatrix
& wxTransformMatrix::operator*=(const double& t
)
425 for (int i
= 0; i
< 3; i
++)
426 for (int j
= 0; j
< 3; j
++)
428 m_isIdentity
= IsIdentity1();
432 wxTransformMatrix
& wxTransformMatrix::operator/=(const double& t
)
434 for (int i
= 0; i
< 3; i
++)
435 for (int j
= 0; j
< 3; j
++)
437 m_isIdentity
= IsIdentity1();
441 wxTransformMatrix
& wxTransformMatrix::operator+=(const wxTransformMatrix
& mat
)
443 for (int i
= 0; i
< 3; i
++)
444 for (int j
= 0; j
< 3; j
++)
445 m_matrix
[i
][j
] += mat
.m_matrix
[i
][j
];
446 m_isIdentity
= IsIdentity1();
450 wxTransformMatrix
& wxTransformMatrix::operator-=(const wxTransformMatrix
& mat
)
452 for (int i
= 0; i
< 3; i
++)
453 for (int j
= 0; j
< 3; j
++)
454 m_matrix
[i
][j
] -= mat
.m_matrix
[i
][j
];
455 m_isIdentity
= IsIdentity1();
459 wxTransformMatrix
& wxTransformMatrix::operator*=(const wxTransformMatrix
& mat
)
462 if (mat
.m_isIdentity
)
471 wxTransformMatrix result
;
472 for (int i
= 0; i
< 3; i
++)
474 for (int j
= 0; j
< 3; j
++)
477 for (int k
= 0; k
< 3; k
++)
478 sum
+= m_matrix
[k
][i
] * mat
.m_matrix
[j
][k
];
479 result
.m_matrix
[j
][i
] = sum
;
485 m_isIdentity
= IsIdentity1();
490 // constant operators
491 wxTransformMatrix
wxTransformMatrix::operator*(const double& t
) const
493 wxTransformMatrix result
= *this;
495 result
.m_isIdentity
= result
.IsIdentity1();
499 wxTransformMatrix
wxTransformMatrix::operator/(const double& t
) const
501 wxTransformMatrix result
= *this;
504 result
.m_isIdentity
= result
.IsIdentity1();
508 wxTransformMatrix
wxTransformMatrix::operator+(const wxTransformMatrix
& m
) const
510 wxTransformMatrix result
= *this;
512 result
.m_isIdentity
= result
.IsIdentity1();
516 wxTransformMatrix
wxTransformMatrix::operator-(const wxTransformMatrix
& m
) const
518 wxTransformMatrix result
= *this;
520 result
.m_isIdentity
= result
.IsIdentity1();
525 wxTransformMatrix
wxTransformMatrix::operator*(const wxTransformMatrix
& m
) const
527 wxTransformMatrix result
= *this;
529 result
.m_isIdentity
= result
.IsIdentity1();
534 wxTransformMatrix
wxTransformMatrix::operator-() const
536 wxTransformMatrix result
= *this;
537 for (int i
= 0; i
< 3; i
++)
538 for (int j
= 0; j
< 3; j
++)
539 result
.m_matrix
[i
][j
] = -(this->m_matrix
[i
][j
]);
540 result
.m_isIdentity
= result
.IsIdentity1();
544 static double CheckInt(double getal
)
546 // check if the number is very close to an integer
547 if ( (ceil(getal
) - getal
) < 0.0001)
550 else if ( (getal
- floor(getal
)) < 0.0001)
557 double wxTransformMatrix::Get_scaleX()
560 double rot_angle
= CheckInt(atan2(m_matrix
[1][0],m_matrix
[0][0])*180/pi
);
561 if (rot_angle
!= 90 && rot_angle
!= -90)
562 scale_factor
= m_matrix
[0][0]/cos((rot_angle
/180)*pi
);
564 scale_factor
= m_matrix
[0][0]/sin((rot_angle
/180)*pi
); // er kan nl. niet door 0 gedeeld worden !
566 scale_factor
= CheckInt(scale_factor
);
567 if (scale_factor
< 0)
568 scale_factor
= -scale_factor
;
573 double wxTransformMatrix::Get_scaleY()
576 double rot_angle
= CheckInt(atan2(m_matrix
[1][0],m_matrix
[0][0])*180/pi
);
577 if (rot_angle
!= 90 && rot_angle
!= -90)
578 scale_factor
= m_matrix
[1][1]/cos((rot_angle
/180)*pi
);
580 scale_factor
= m_matrix
[1][1]/sin((rot_angle
/180)*pi
); // er kan nl. niet door 0 gedeeld worden !
582 scale_factor
= CheckInt(scale_factor
);
583 if (scale_factor
< 0)
585 scale_factor
= -scale_factor
;
591 double wxTransformMatrix::GetRotation()
593 double temp1
= GetValue(0,0); // for angle calculation
594 double temp2
= GetValue(0,1); //
597 double rot_angle
= atan2(temp2
,temp1
)*180/pi
;
599 rot_angle
= CheckInt(rot_angle
);
603 void wxTransformMatrix::SetRotation(double rotation
)
605 double x
=GetValue(2,0);
606 double y
=GetValue(2,1);
607 Rotate(-GetRotation(), x
, y
);
608 Rotate(rotation
, x
, y
);