]> git.saurik.com Git - wxWidgets.git/blame - src/common/matrix.cpp
fix warnings (double to int conversions and unused variables); removed hard TABs...
[wxWidgets.git] / src / common / matrix.cpp
CommitLineData
311765c2 1///////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/matrix.cpp
c801d85f
KB
3// Purpose: wxTransformMatrix class
4// Author: Chris Breeze, Julian Smart
513abb88 5// Modified by: Klaas Holwerda
c801d85f
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
311765c2 10///////////////////////////////////////////////////////////////////////////////
c801d85f 11
c801d85f
KB
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.
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
18680f86 19 #pragma hdrstop
c801d85f
KB
20#endif
21
18680f86
WS
22#include "wx/matrix.h"
23
c801d85f 24#ifndef WX_PRECOMP
8898456d 25 #include "wx/math.h"
c801d85f
KB
26#endif
27
e0d31471 28static const double pi = M_PI;
c801d85f
KB
29
30wxTransformMatrix::wxTransformMatrix(void)
31{
4e32eea1 32 m_isIdentity = false;
c801d85f 33
513abb88 34 Identity();
c801d85f
KB
35}
36
37wxTransformMatrix::wxTransformMatrix(const wxTransformMatrix& mat)
1b0674f7 38 : wxObject()
c801d85f 39{
513abb88 40 (*this) = mat;
c801d85f
KB
41}
42
513abb88 43double wxTransformMatrix::GetValue(int col, int row) const
c801d85f 44{
513abb88
RR
45 if (row < 0 || row > 2 || col < 0 || col > 2)
46 return 0.0;
c801d85f 47
513abb88 48 return m_matrix[col][row];
c801d85f
KB
49}
50
513abb88 51void wxTransformMatrix::SetValue(int col, int row, double value)
c801d85f 52{
513abb88
RR
53 if (row < 0 || row > 2 || col < 0 || col > 2)
54 return;
c801d85f 55
513abb88
RR
56 m_matrix[col][row] = value;
57 m_isIdentity = IsIdentity1();
c801d85f
KB
58}
59
60void wxTransformMatrix::operator = (const wxTransformMatrix& mat)
61{
513abb88
RR
62 int i, j;
63 for (i = 0; i < 3; i++)
64 {
65 for (j = 0; j < 3; j++)
66 {
67 m_matrix[i][j] = mat.m_matrix[i][j];
68 }
69 }
70 m_isIdentity = mat.m_isIdentity;
c801d85f
KB
71}
72
fbfb8bcc 73bool wxTransformMatrix::operator == (const wxTransformMatrix& mat) const
c801d85f 74{
d0ee33f5 75 if (m_isIdentity && mat.m_isIdentity)
4e32eea1 76 return true;
513abb88
RR
77
78 int i, j;
79 for (i = 0; i < 3; i++)
80 {
81 for (j = 0; j < 3; j++)
82 {
c77a6796 83 if ( !wxIsSameDouble(m_matrix[i][j], mat.m_matrix[i][j]) )
4e32eea1 84 return false;
513abb88
RR
85 }
86 }
4e32eea1 87 return true;
c801d85f
KB
88}
89
fbfb8bcc 90bool wxTransformMatrix::operator != (const wxTransformMatrix& mat) const
c801d85f 91{
513abb88 92 return (! ((*this) == mat));
c801d85f
KB
93}
94
513abb88 95double& wxTransformMatrix::operator()(int col, int row)
c801d85f 96{
513abb88
RR
97 if (row < 0 || row > 2 || col < 0 || col > 2)
98 return m_matrix[0][0];
c801d85f 99
513abb88 100 return m_matrix[col][row];
c801d85f
KB
101}
102
513abb88 103double wxTransformMatrix::operator()(int col, int row) const
c801d85f 104{
513abb88
RR
105 if (row < 0 || row > 2 || col < 0 || col > 2)
106 return 0.0;
c801d85f 107
513abb88 108 return m_matrix[col][row];
c801d85f
KB
109}
110
111// Invert matrix
112bool wxTransformMatrix::Invert(void)
113{
513abb88
RR
114 double inverseMatrix[3][3];
115
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]);
120
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]);
124
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]);
128
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];
c77a6796
VZ
131 if ( wxIsNullDouble(det) )
132 return false;
133
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;
513abb88 137
c77a6796
VZ
138 for (int i = 0; i < 3; i++)
139 {
140 for (int j = 0; j < 3; j++)
513abb88 141 {
c77a6796 142 m_matrix[i][j] = inverseMatrix[i][j];
513abb88 143 }
513abb88 144 }
c77a6796
VZ
145 m_isIdentity = IsIdentity1();
146 return true;
c801d85f
KB
147}
148
149// Make into identity matrix
150bool wxTransformMatrix::Identity(void)
151{
513abb88
RR
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;
4e32eea1 154 m_isIdentity = true;
c801d85f 155
4e32eea1 156 return true;
c801d85f
KB
157}
158
159// Scale by scale (isotropic scaling i.e. the same in x and y):
160// | scale 0 0 |
161// matrix' = | 0 scale 0 | x matrix
162// | 0 0 scale |
163//
164bool wxTransformMatrix::Scale(double scale)
165{
513abb88
RR
166 int i, j;
167 for (i = 0; i < 3; i++)
168 {
169 for (j = 0; j < 3; j++)
170 {
171 m_matrix[i][j] *= scale;
172 }
173 }
174 m_isIdentity = IsIdentity1();
175
4e32eea1 176 return true;
513abb88
RR
177}
178
179
180// scale a matrix in 2D
181//
182// xs 0 xc(1-xs)
183// 0 ys yc(1-ys)
184// 0 0 1
185//
186wxTransformMatrix& wxTransformMatrix::Scale(const double &xs, const double &ys,const double &xc, const double &yc)
187{
188 double r00,r10,r20,r01,r11,r21;
189
190 if (m_isIdentity)
191 {
c77a6796
VZ
192 double tx = xc*(1-xs);
193 double ty = yc*(1-ys);
513abb88
RR
194 r00 = xs;
195 r10 = 0;
196 r20 = tx;
197 r01 = 0;
198 r11 = ys;
199 r21 = ty;
200 }
c77a6796 201 else if ( !wxIsNullDouble(xc) || !wxIsNullDouble(yc) )
513abb88 202 {
c77a6796
VZ
203 double tx = xc*(1-xs);
204 double ty = yc*(1-ys);
513abb88
RR
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;
211 }
212 else
213 {
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];
220 }
221
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;
228
229/* or like this
230 // first translate to origin O
231 (*this).Translate(-x_cen, -y_cen);
232
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();
238
239 *this = scale * (*this);
240
241 // translate back from origin to x_cen, y_cen
242 (*this).Translate(x_cen, y_cen);
243*/
244
245 m_isIdentity = IsIdentity1();
246
247 return *this;
248}
249
c801d85f 250
513abb88
RR
251// mirror a matrix in x, y
252//
253// -1 0 0 Y-mirror
254// 0 -1 0 X-mirror
255// 0 0 -1 Z-mirror
256wxTransformMatrix& wxTransformMatrix::Mirror(bool x, bool y)
257{
258 wxTransformMatrix temp;
259 if (x)
260 {
261 temp.m_matrix[1][1] = -1;
4e32eea1 262 temp.m_isIdentity=false;
513abb88
RR
263 }
264 if (y)
265 {
266 temp.m_matrix[0][0] = -1;
4e32eea1 267 temp.m_isIdentity=false;
513abb88
RR
268 }
269
270 *this = temp * (*this);
271 m_isIdentity = IsIdentity1();
272 return *this;
c801d85f
KB
273}
274
275// Translate by dx, dy:
276// | 1 0 dx |
277// matrix' = | 0 1 dy | x matrix
278// | 0 0 1 |
279//
280bool wxTransformMatrix::Translate(double dx, double dy)
281{
513abb88
RR
282 int i;
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];
c801d85f 287
513abb88 288 m_isIdentity = IsIdentity1();
c801d85f 289
4e32eea1 290 return true;
c801d85f
KB
291}
292
513abb88 293// Rotate clockwise by the given number of degrees:
c801d85f
KB
294// | cos sin 0 |
295// matrix' = | -sin cos 0 | x matrix
296// | 0 0 1 |
c801d85f
KB
297bool wxTransformMatrix::Rotate(double degrees)
298{
513abb88 299 Rotate(-degrees,0,0);
4e32eea1 300 return true;
513abb88 301}
c801d85f 302
513abb88
RR
303// counter clockwise rotate around a point
304//
305// cos(r) -sin(r) x(1-cos(r))+y(sin(r)
306// sin(r) cos(r) y(1-cos(r))-x(sin(r)
307// 0 0 1
308wxTransformMatrix& wxTransformMatrix::Rotate(const double &degrees, const double &x, const double &y)
309{
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;
314
315 if (m_isIdentity)
316 {
c77a6796
VZ
317 double tx = x*(1-c)+y*s;
318 double ty = y*(1-c)-x*s;
513abb88
RR
319 r00 = c ;
320 r10 = -s;
321 r20 = tx;
322 r01 = s;
323 r11 = c;
324 r21 = ty;
325 }
c77a6796 326 else if ( !wxIsNullDouble(x) || !wxIsNullDouble(y) )
513abb88 327 {
c77a6796
VZ
328 double tx = x*(1-c)+y*s;
329 double ty = y*(1-c)-x*s;
513abb88
RR
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];
336 }
337 else
338 {
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];
345 }
346
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;
353
354/* or like this
355 wxTransformMatrix rotate;
356 rotate.m_matrix[2][0] = tx;
357 rotate.m_matrix[2][1] = ty;
358
359 rotate.m_matrix[0][0] = c;
360 rotate.m_matrix[0][1] = s;
361
362 rotate.m_matrix[1][0] = -s;
363 rotate.m_matrix[1][1] = c;
364
365 rotate.m_isIdentity=false;
366 *this = rotate * (*this);
367*/
368 m_isIdentity = IsIdentity1();
369
370 return *this;
c801d85f
KB
371}
372
373// Transform a point from logical to device coordinates
374bool wxTransformMatrix::TransformPoint(double x, double y, double& tx, double& ty) const
375{
513abb88
RR
376 if (IsIdentity())
377 {
4e32eea1 378 tx = x; ty = y; return true;
513abb88 379 }
c801d85f 380
513abb88
RR
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];
c801d85f 383
4e32eea1 384 return true;
c801d85f
KB
385}
386
387// Transform a point from device to logical coordinates.
388
389// Example of use:
390// wxTransformMatrix mat = dc.GetTransformation();
391// mat.Invert();
392// mat.InverseTransformPoint(x, y, x1, y1);
393// OR (shorthand:)
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.
c801d85f
KB
397bool wxTransformMatrix::InverseTransformPoint(double x, double y, double& tx, double& ty) const
398{
513abb88
RR
399 if (IsIdentity())
400 {
c77a6796
VZ
401 tx = x;
402 ty = y;
403 return true;
513abb88
RR
404 }
405
c77a6796
VZ
406 const double z = (1.0 - m_matrix[0][2] * x - m_matrix[1][2] * y) / m_matrix[2][2];
407 if ( wxIsNullDouble(z) )
4e32eea1 408 return false;
c77a6796 409
513abb88
RR
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];
4e32eea1 412 return true;
513abb88
RR
413}
414
415wxTransformMatrix& wxTransformMatrix::operator*=(const double& t)
416{
417 for (int i = 0; i < 3; i++)
418 for (int j = 0; j < 3; j++)
419 m_matrix[i][j]*= t;
420 m_isIdentity = IsIdentity1();
421 return *this;
422}
423
424wxTransformMatrix& wxTransformMatrix::operator/=(const double& t)
425{
426 for (int i = 0; i < 3; i++)
427 for (int j = 0; j < 3; j++)
428 m_matrix[i][j]/= t;
429 m_isIdentity = IsIdentity1();
430 return *this;
431}
432
433wxTransformMatrix& wxTransformMatrix::operator+=(const wxTransformMatrix& mat)
434{
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();
439 return *this;
440}
441
442wxTransformMatrix& wxTransformMatrix::operator-=(const wxTransformMatrix& mat)
443{
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();
448 return *this;
449}
450
451wxTransformMatrix& wxTransformMatrix::operator*=(const wxTransformMatrix& mat)
452{
453
454 if (mat.m_isIdentity)
455 return *this;
456 if (m_isIdentity)
457 {
458 *this = mat;
459 return *this;
460 }
461 else
462 {
463 wxTransformMatrix result;
464 for (int i = 0; i < 3; i++)
465 {
466 for (int j = 0; j < 3; j++)
467 {
468 double sum = 0;
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;
472 }
473 }
474 *this = result;
475 }
476
477 m_isIdentity = IsIdentity1();
478 return *this;
479}
480
481
482// constant operators
483wxTransformMatrix wxTransformMatrix::operator*(const double& t) const
484{
485 wxTransformMatrix result = *this;
486 result *= t;
487 result.m_isIdentity = result.IsIdentity1();
488 return result;
489}
490
491wxTransformMatrix wxTransformMatrix::operator/(const double& t) const
492{
493 wxTransformMatrix result = *this;
494// wxASSERT(t!=0);
495 result /= t;
496 result.m_isIdentity = result.IsIdentity1();
497 return result;
498}
499
500wxTransformMatrix wxTransformMatrix::operator+(const wxTransformMatrix& m) const
501{
502 wxTransformMatrix result = *this;
503 result += m;
504 result.m_isIdentity = result.IsIdentity1();
505 return result;
506}
507
508wxTransformMatrix wxTransformMatrix::operator-(const wxTransformMatrix& m) const
509{
510 wxTransformMatrix result = *this;
511 result -= m;
512 result.m_isIdentity = result.IsIdentity1();
513 return result;
514}
515
516
517wxTransformMatrix wxTransformMatrix::operator*(const wxTransformMatrix& m) const
518{
519 wxTransformMatrix result = *this;
520 result *= m;
521 result.m_isIdentity = result.IsIdentity1();
522 return result;
523}
524
525
526wxTransformMatrix wxTransformMatrix::operator-() const
527{
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();
533 return result;
534}
535
536static double CheckInt(double getal)
537{
538 // check if the number is very close to an integer
539 if ( (ceil(getal) - getal) < 0.0001)
540 return ceil(getal);
541
542 else if ( (getal - floor(getal)) < 0.0001)
543 return floor(getal);
544
545 return getal;
546
547}
548
549double wxTransformMatrix::Get_scaleX()
550{
551 double scale_factor;
552 double rot_angle = CheckInt(atan2(m_matrix[1][0],m_matrix[0][0])*180/pi);
c77a6796 553 if ( !wxIsSameDouble(rot_angle, 90) && !wxIsSameDouble(rot_angle, -90) )
513abb88
RR
554 scale_factor = m_matrix[0][0]/cos((rot_angle/180)*pi);
555 else
556 scale_factor = m_matrix[0][0]/sin((rot_angle/180)*pi); // er kan nl. niet door 0 gedeeld worden !
557
558 scale_factor = CheckInt(scale_factor);
559 if (scale_factor < 0)
560 scale_factor = -scale_factor;
561
562 return scale_factor;
563}
564
565double wxTransformMatrix::Get_scaleY()
566{
567 double scale_factor;
568 double rot_angle = CheckInt(atan2(m_matrix[1][0],m_matrix[0][0])*180/pi);
c77a6796 569 if ( !wxIsSameDouble(rot_angle, 90) && !wxIsSameDouble(rot_angle, -90) )
513abb88
RR
570 scale_factor = m_matrix[1][1]/cos((rot_angle/180)*pi);
571 else
572 scale_factor = m_matrix[1][1]/sin((rot_angle/180)*pi); // er kan nl. niet door 0 gedeeld worden !
573
574 scale_factor = CheckInt(scale_factor);
575 if (scale_factor < 0)
576
577 scale_factor = -scale_factor;
578
579 return scale_factor;
580
581}
582
583double wxTransformMatrix::GetRotation()
584{
585 double temp1 = GetValue(0,0); // for angle calculation
586 double temp2 = GetValue(0,1); //
587
588 // Rotation
589 double rot_angle = atan2(temp2,temp1)*180/pi;
590
591 rot_angle = CheckInt(rot_angle);
592 return rot_angle;
593}
594
595void wxTransformMatrix::SetRotation(double rotation)
596{
597 double x=GetValue(2,0);
598 double y=GetValue(2,1);
599 Rotate(-GetRotation(), x, y);
600 Rotate(rotation, x, y);
c801d85f 601}