]>
git.saurik.com Git - wxWidgets.git/blob - src/common/matrix.cpp
f1f8c319f97ae0754d205e8ead4685268ff6f518
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/matrix.cpp
3 // Purpose: wxTransformMatrix class
4 // Author: Chris Breeze, Julian Smart
5 // Modified by: Klaas Holwerda
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // Note: this is intended to be used in wxDC at some point to replace
13 // the current system of scaling/translation. It is not yet used.
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/matrix.h"
28 static const double pi
= M_PI
;
30 wxTransformMatrix::wxTransformMatrix(void)
37 wxTransformMatrix::wxTransformMatrix(const wxTransformMatrix
& mat
)
43 double wxTransformMatrix::GetValue(int col
, int row
) const
45 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
48 return m_matrix
[col
][row
];
51 void wxTransformMatrix::SetValue(int col
, int row
, double value
)
53 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
56 m_matrix
[col
][row
] = value
;
57 m_isIdentity
= IsIdentity1();
60 void wxTransformMatrix::operator = (const wxTransformMatrix
& mat
)
63 for (i
= 0; i
< 3; i
++)
65 for (j
= 0; j
< 3; j
++)
67 m_matrix
[i
][j
] = mat
.m_matrix
[i
][j
];
70 m_isIdentity
= mat
.m_isIdentity
;
73 bool wxTransformMatrix::operator == (const wxTransformMatrix
& mat
) const
75 if (m_isIdentity
&& mat
.m_isIdentity
)
79 for (i
= 0; i
< 3; i
++)
81 for (j
= 0; j
< 3; j
++)
83 if ( !wxIsSameDouble(m_matrix
[i
][j
], mat
.m_matrix
[i
][j
]) )
90 bool wxTransformMatrix::operator != (const wxTransformMatrix
& mat
) const
92 return (! ((*this) == mat
));
95 double& wxTransformMatrix::operator()(int col
, int row
)
97 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
98 return m_matrix
[0][0];
100 return m_matrix
[col
][row
];
103 double wxTransformMatrix::operator()(int col
, int row
) const
105 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
108 return m_matrix
[col
][row
];
112 bool wxTransformMatrix::Invert(void)
114 double inverseMatrix
[3][3];
116 // calculate the adjoint
117 inverseMatrix
[0][0] = wxCalculateDet(m_matrix
[1][1],m_matrix
[2][1],m_matrix
[1][2],m_matrix
[2][2]);
118 inverseMatrix
[0][1] = -wxCalculateDet(m_matrix
[0][1],m_matrix
[2][1],m_matrix
[0][2],m_matrix
[2][2]);
119 inverseMatrix
[0][2] = wxCalculateDet(m_matrix
[0][1],m_matrix
[1][1],m_matrix
[0][2],m_matrix
[1][2]);
121 inverseMatrix
[1][0] = -wxCalculateDet(m_matrix
[1][0],m_matrix
[2][0],m_matrix
[1][2],m_matrix
[2][2]);
122 inverseMatrix
[1][1] = wxCalculateDet(m_matrix
[0][0],m_matrix
[2][0],m_matrix
[0][2],m_matrix
[2][2]);
123 inverseMatrix
[1][2] = -wxCalculateDet(m_matrix
[0][0],m_matrix
[1][0],m_matrix
[0][2],m_matrix
[1][2]);
125 inverseMatrix
[2][0] = wxCalculateDet(m_matrix
[1][0],m_matrix
[2][0],m_matrix
[1][1],m_matrix
[2][1]);
126 inverseMatrix
[2][1] = -wxCalculateDet(m_matrix
[0][0],m_matrix
[2][0],m_matrix
[0][1],m_matrix
[2][1]);
127 inverseMatrix
[2][2] = wxCalculateDet(m_matrix
[0][0],m_matrix
[1][0],m_matrix
[0][1],m_matrix
[1][1]);
129 // now divide by the determinant
130 double det
= m_matrix
[0][0] * inverseMatrix
[0][0] + m_matrix
[0][1] * inverseMatrix
[1][0] + m_matrix
[0][2] * inverseMatrix
[2][0];
131 if ( wxIsNullDouble(det
) )
134 inverseMatrix
[0][0] /= det
; inverseMatrix
[1][0] /= det
; inverseMatrix
[2][0] /= det
;
135 inverseMatrix
[0][1] /= det
; inverseMatrix
[1][1] /= det
; inverseMatrix
[2][1] /= det
;
136 inverseMatrix
[0][2] /= det
; inverseMatrix
[1][2] /= det
; inverseMatrix
[2][2] /= det
;
138 for (int i
= 0; i
< 3; i
++)
140 for (int j
= 0; j
< 3; j
++)
142 m_matrix
[i
][j
] = inverseMatrix
[i
][j
];
145 m_isIdentity
= IsIdentity1();
149 // Make into identity matrix
150 bool wxTransformMatrix::Identity(void)
152 m_matrix
[0][0] = m_matrix
[1][1] = m_matrix
[2][2] = 1.0;
153 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;
159 // Scale by scale (isotropic scaling i.e. the same in x and y):
161 // matrix' = | 0 scale 0 | x matrix
164 bool wxTransformMatrix::Scale(double scale
)
167 for (i
= 0; i
< 3; i
++)
169 for (j
= 0; j
< 3; j
++)
171 m_matrix
[i
][j
] *= scale
;
174 m_isIdentity
= IsIdentity1();
180 // scale a matrix in 2D
186 wxTransformMatrix
& wxTransformMatrix::Scale(const double &xs
, const double &ys
,const double &xc
, const double &yc
)
188 double r00
,r10
,r20
,r01
,r11
,r21
;
192 double tx
= xc
*(1-xs
);
193 double ty
= yc
*(1-ys
);
201 else if ( !wxIsNullDouble(xc
) || !wxIsNullDouble(yc
) )
203 double tx
= xc
*(1-xs
);
204 double ty
= yc
*(1-ys
);
205 r00
= xs
* m_matrix
[0][0];
206 r10
= xs
* m_matrix
[1][0];
207 r20
= xs
* m_matrix
[2][0] + tx
;
208 r01
= ys
* m_matrix
[0][1];
209 r11
= ys
* m_matrix
[1][1];
210 r21
= ys
* m_matrix
[2][1] + ty
;
214 r00
= xs
* m_matrix
[0][0];
215 r10
= xs
* m_matrix
[1][0];
216 r20
= xs
* m_matrix
[2][0];
217 r01
= ys
* m_matrix
[0][1];
218 r11
= ys
* m_matrix
[1][1];
219 r21
= ys
* m_matrix
[2][1];
222 m_matrix
[0][0] = r00
;
223 m_matrix
[1][0] = r10
;
224 m_matrix
[2][0] = r20
;
225 m_matrix
[0][1] = r01
;
226 m_matrix
[1][1] = r11
;
227 m_matrix
[2][1] = r21
;
230 // first translate to origin O
231 (*this).Translate(-x_cen, -y_cen);
233 // now do the scaling
234 wxTransformMatrix scale;
235 scale.m_matrix[0][0] = x_fac;
236 scale.m_matrix[1][1] = y_fac;
237 scale.m_isIdentity = IsIdentity1();
239 *this = scale * (*this);
241 // translate back from origin to x_cen, y_cen
242 (*this).Translate(x_cen, y_cen);
245 m_isIdentity
= IsIdentity1();
251 // mirror a matrix in x, y
256 wxTransformMatrix
& wxTransformMatrix::Mirror(bool x
, bool y
)
258 wxTransformMatrix temp
;
261 temp
.m_matrix
[1][1] = -1;
262 temp
.m_isIdentity
=false;
266 temp
.m_matrix
[0][0] = -1;
267 temp
.m_isIdentity
=false;
270 *this = temp
* (*this);
271 m_isIdentity
= IsIdentity1();
275 // Translate by dx, dy:
277 // matrix' = | 0 1 dy | x matrix
280 bool wxTransformMatrix::Translate(double dx
, double dy
)
283 for (i
= 0; i
< 3; i
++)
284 m_matrix
[i
][0] += dx
* m_matrix
[i
][2];
285 for (i
= 0; i
< 3; i
++)
286 m_matrix
[i
][1] += dy
* m_matrix
[i
][2];
288 m_isIdentity
= IsIdentity1();
293 // Rotate clockwise by the given number of degrees:
295 // matrix' = | -sin cos 0 | x matrix
297 bool wxTransformMatrix::Rotate(double degrees
)
299 Rotate(-degrees
,0,0);
303 // counter clockwise rotate around a point
305 // cos(r) -sin(r) x(1-cos(r))+y(sin(r)
306 // sin(r) cos(r) y(1-cos(r))-x(sin(r)
308 wxTransformMatrix
& wxTransformMatrix::Rotate(const double °rees
, const double &x
, const double &y
)
310 double angle
= degrees
* pi
/ 180.0;
311 double c
= cos(angle
);
312 double s
= sin(angle
);
313 double r00
,r10
,r20
,r01
,r11
,r21
;
317 double tx
= x
*(1-c
)+y
*s
;
318 double ty
= y
*(1-c
)-x
*s
;
326 else if ( !wxIsNullDouble(x
) || !wxIsNullDouble(y
) )
328 double tx
= x
*(1-c
)+y
*s
;
329 double ty
= y
*(1-c
)-x
*s
;
330 r00
= c
* m_matrix
[0][0] - s
* m_matrix
[0][1] + tx
* m_matrix
[0][2];
331 r10
= c
* m_matrix
[1][0] - s
* m_matrix
[1][1] + tx
* m_matrix
[1][2];
332 r20
= c
* m_matrix
[2][0] - s
* m_matrix
[2][1] + tx
;// * m_matrix[2][2];
333 r01
= c
* m_matrix
[0][1] + s
* m_matrix
[0][0] + ty
* m_matrix
[0][2];
334 r11
= c
* m_matrix
[1][1] + s
* m_matrix
[1][0] + ty
* m_matrix
[1][2];
335 r21
= c
* m_matrix
[2][1] + s
* m_matrix
[2][0] + ty
;// * m_matrix[2][2];
339 r00
= c
* m_matrix
[0][0] - s
* m_matrix
[0][1];
340 r10
= c
* m_matrix
[1][0] - s
* m_matrix
[1][1];
341 r20
= c
* m_matrix
[2][0] - s
* m_matrix
[2][1];
342 r01
= c
* m_matrix
[0][1] + s
* m_matrix
[0][0];
343 r11
= c
* m_matrix
[1][1] + s
* m_matrix
[1][0];
344 r21
= c
* m_matrix
[2][1] + s
* m_matrix
[2][0];
347 m_matrix
[0][0] = r00
;
348 m_matrix
[1][0] = r10
;
349 m_matrix
[2][0] = r20
;
350 m_matrix
[0][1] = r01
;
351 m_matrix
[1][1] = r11
;
352 m_matrix
[2][1] = r21
;
355 wxTransformMatrix rotate;
356 rotate.m_matrix[2][0] = tx;
357 rotate.m_matrix[2][1] = ty;
359 rotate.m_matrix[0][0] = c;
360 rotate.m_matrix[0][1] = s;
362 rotate.m_matrix[1][0] = -s;
363 rotate.m_matrix[1][1] = c;
365 rotate.m_isIdentity=false;
366 *this = rotate * (*this);
368 m_isIdentity
= IsIdentity1();
373 // Transform a point from logical to device coordinates
374 bool wxTransformMatrix::TransformPoint(double x
, double y
, double& tx
, double& ty
) const
378 tx
= x
; ty
= y
; return true;
381 tx
= x
* m_matrix
[0][0] + y
* m_matrix
[1][0] + m_matrix
[2][0];
382 ty
= x
* m_matrix
[0][1] + y
* m_matrix
[1][1] + m_matrix
[2][1];
387 // Transform a point from device to logical coordinates.
390 // wxTransformMatrix mat = dc.GetTransformation();
392 // mat.InverseTransformPoint(x, y, x1, y1);
394 // dc.LogicalToDevice(x, y, x1, y1);
395 // The latter is slightly less efficient if we're doing several
396 // conversions, since the matrix is inverted several times.
397 bool wxTransformMatrix::InverseTransformPoint(double x
, double y
, double& tx
, double& ty
) const
406 const double z
= (1.0 - m_matrix
[0][2] * x
- m_matrix
[1][2] * y
) / m_matrix
[2][2];
407 if ( wxIsNullDouble(z
) )
410 tx
= x
* m_matrix
[0][0] + y
* m_matrix
[1][0] + z
* m_matrix
[2][0];
411 ty
= x
* m_matrix
[0][1] + y
* m_matrix
[1][1] + z
* m_matrix
[2][1];
415 wxTransformMatrix
& wxTransformMatrix::operator*=(const double& t
)
417 for (int i
= 0; i
< 3; i
++)
418 for (int j
= 0; j
< 3; j
++)
420 m_isIdentity
= IsIdentity1();
424 wxTransformMatrix
& wxTransformMatrix::operator/=(const double& t
)
426 for (int i
= 0; i
< 3; i
++)
427 for (int j
= 0; j
< 3; j
++)
429 m_isIdentity
= IsIdentity1();
433 wxTransformMatrix
& wxTransformMatrix::operator+=(const wxTransformMatrix
& mat
)
435 for (int i
= 0; i
< 3; i
++)
436 for (int j
= 0; j
< 3; j
++)
437 m_matrix
[i
][j
] += mat
.m_matrix
[i
][j
];
438 m_isIdentity
= IsIdentity1();
442 wxTransformMatrix
& wxTransformMatrix::operator-=(const wxTransformMatrix
& mat
)
444 for (int i
= 0; i
< 3; i
++)
445 for (int j
= 0; j
< 3; j
++)
446 m_matrix
[i
][j
] -= mat
.m_matrix
[i
][j
];
447 m_isIdentity
= IsIdentity1();
451 wxTransformMatrix
& wxTransformMatrix::operator*=(const wxTransformMatrix
& mat
)
454 if (mat
.m_isIdentity
)
463 wxTransformMatrix result
;
464 for (int i
= 0; i
< 3; i
++)
466 for (int j
= 0; j
< 3; j
++)
469 for (int k
= 0; k
< 3; k
++)
470 sum
+= m_matrix
[k
][i
] * mat
.m_matrix
[j
][k
];
471 result
.m_matrix
[j
][i
] = sum
;
477 m_isIdentity
= IsIdentity1();
482 // constant operators
483 wxTransformMatrix
wxTransformMatrix::operator*(const double& t
) const
485 wxTransformMatrix result
= *this;
487 result
.m_isIdentity
= result
.IsIdentity1();
491 wxTransformMatrix
wxTransformMatrix::operator/(const double& t
) const
493 wxTransformMatrix result
= *this;
496 result
.m_isIdentity
= result
.IsIdentity1();
500 wxTransformMatrix
wxTransformMatrix::operator+(const wxTransformMatrix
& m
) const
502 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();
517 wxTransformMatrix
wxTransformMatrix::operator*(const wxTransformMatrix
& m
) const
519 wxTransformMatrix result
= *this;
521 result
.m_isIdentity
= result
.IsIdentity1();
526 wxTransformMatrix
wxTransformMatrix::operator-() const
528 wxTransformMatrix result
= *this;
529 for (int i
= 0; i
< 3; i
++)
530 for (int j
= 0; j
< 3; j
++)
531 result
.m_matrix
[i
][j
] = -(this->m_matrix
[i
][j
]);
532 result
.m_isIdentity
= result
.IsIdentity1();
536 static double CheckInt(double getal
)
538 // check if the number is very close to an integer
539 if ( (ceil(getal
) - getal
) < 0.0001)
542 else if ( (getal
- floor(getal
)) < 0.0001)
549 double wxTransformMatrix::Get_scaleX()
552 double rot_angle
= CheckInt(atan2(m_matrix
[1][0],m_matrix
[0][0])*180/pi
);
553 if ( !wxIsSameDouble(rot_angle
, 90) && !wxIsSameDouble(rot_angle
, -90) )
554 scale_factor
= m_matrix
[0][0]/cos((rot_angle
/180)*pi
);
556 scale_factor
= m_matrix
[0][0]/sin((rot_angle
/180)*pi
); // er kan nl. niet door 0 gedeeld worden !
558 scale_factor
= CheckInt(scale_factor
);
559 if (scale_factor
< 0)
560 scale_factor
= -scale_factor
;
565 double wxTransformMatrix::Get_scaleY()
568 double rot_angle
= CheckInt(atan2(m_matrix
[1][0],m_matrix
[0][0])*180/pi
);
569 if ( !wxIsSameDouble(rot_angle
, 90) && !wxIsSameDouble(rot_angle
, -90) )
570 scale_factor
= m_matrix
[1][1]/cos((rot_angle
/180)*pi
);
572 scale_factor
= m_matrix
[1][1]/sin((rot_angle
/180)*pi
); // er kan nl. niet door 0 gedeeld worden !
574 scale_factor
= CheckInt(scale_factor
);
575 if (scale_factor
< 0)
577 scale_factor
= -scale_factor
;
583 double wxTransformMatrix::GetRotation()
585 double temp1
= GetValue(0,0); // for angle calculation
586 double temp2
= GetValue(0,1); //
589 double rot_angle
= atan2(temp2
,temp1
)*180/pi
;
591 rot_angle
= CheckInt(rot_angle
);
595 void wxTransformMatrix::SetRotation(double rotation
)
597 double x
=GetValue(2,0);
598 double y
=GetValue(2,1);
599 Rotate(-GetRotation(), x
, y
);
600 Rotate(rotation
, x
, y
);