]>
git.saurik.com Git - wxWidgets.git/blob - src/common/matrix.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTransformMatrix class
4 // Author: Chris Breeze, Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "matrix.h"
16 // Note: this is intended to be used in wxDC at some point to replace
17 // the current system of scaling/translation. It is not yet used.
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/matrix.h"
33 const double pi
= 3.1415926535;
35 wxTransformMatrix::wxTransformMatrix(void)
42 wxTransformMatrix::wxTransformMatrix(const wxTransformMatrix
& mat
)
47 double wxTransformMatrix::GetValue(int row
, int col
) const
49 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
52 return m_matrix
[row
][col
];
55 void wxTransformMatrix::SetValue(int row
, int col
, double value
)
57 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
60 m_matrix
[row
][col
] = value
;
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
)
79 for (i
= 0; i
< 3; i
++)
81 for (j
= 0; j
< 3; j
++)
83 if (m_matrix
[i
][j
] != mat
.m_matrix
[i
][j
])
90 bool wxTransformMatrix::operator != (const wxTransformMatrix
& mat
)
92 return (! ((*this) == mat
));
95 double& wxTransformMatrix::operator()(int row
, int col
)
97 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
98 return m_matrix
[0][0];
100 return m_matrix
[row
][col
];
103 double wxTransformMatrix::operator()(int row
, int col
) const
105 if (row
< 0 || row
> 2 || col
< 0 || col
> 2)
108 return m_matrix
[row
][col
];
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];
133 inverseMatrix
[0][0] /= det
; inverseMatrix
[1][0] /= det
; inverseMatrix
[2][0] /= det
;
134 inverseMatrix
[0][1] /= det
; inverseMatrix
[1][1] /= det
; inverseMatrix
[2][1] /= det
;
135 inverseMatrix
[0][2] /= det
; inverseMatrix
[1][2] /= det
; inverseMatrix
[2][2] /= det
;
138 for (i
= 0; i
< 3; i
++)
140 for (j
= 0; j
< 3; j
++)
142 m_matrix
[i
][j
] = inverseMatrix
[i
][j
];
145 m_isIdentity
= IsIdentity1();
154 // Make into identity matrix
155 bool wxTransformMatrix::Identity(void)
157 m_matrix
[0][0] = m_matrix
[1][1] = m_matrix
[2][2] = 1.0;
158 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;
164 // Scale by scale (isotropic scaling i.e. the same in x and y):
166 // matrix' = | 0 scale 0 | x matrix
169 bool wxTransformMatrix::Scale(double scale
)
172 for (i
= 0; i
< 3; i
++)
174 for (j
= 0; j
< 3; j
++)
176 m_matrix
[i
][j
] *= scale
;
179 m_isIdentity
= IsIdentity1();
184 // Translate by dx, dy:
186 // matrix' = | 0 1 dy | x matrix
189 bool wxTransformMatrix::Translate(double dx
, double dy
)
192 for (i
= 0; i
< 3; i
++)
193 m_matrix
[i
][0] += dx
* m_matrix
[i
][2];
194 for (i
= 0; i
< 3; i
++)
195 m_matrix
[i
][1] += dy
* m_matrix
[i
][2];
197 m_isIdentity
= IsIdentity1();
202 // Rotate by the given number of degrees:
204 // matrix' = | -sin cos 0 | x matrix
207 bool wxTransformMatrix::Rotate(double degrees
)
209 double angle
= degrees
* pi
/ 180.0;
210 double s
= sin(angle
);
211 double c
= cos(angle
);
213 m_matrix
[0][0] = c
* m_matrix
[0][0] + s
* m_matrix
[0][1];
214 m_matrix
[1][0] = c
* m_matrix
[1][0] + s
* m_matrix
[1][1];
215 m_matrix
[2][0] = c
* m_matrix
[2][0] + s
* m_matrix
[2][1];
216 m_matrix
[0][2] = c
* m_matrix
[0][1] - s
* m_matrix
[0][0];
217 m_matrix
[1][2] = c
* m_matrix
[1][1] - s
* m_matrix
[1][0];
218 m_matrix
[2][2] = c
* m_matrix
[2][1] - s
* m_matrix
[2][0];
220 m_isIdentity
= IsIdentity1();
225 // Transform a point from logical to device coordinates
226 bool wxTransformMatrix::TransformPoint(double x
, double y
, double& tx
, double& ty
) const
230 tx
= x
; ty
= y
; return TRUE
;
233 tx
= x
* m_matrix
[0][0] + y
* m_matrix
[1][0] + m_matrix
[2][0];
234 ty
= x
* m_matrix
[0][1] + y
* m_matrix
[1][1] + m_matrix
[2][1];
239 // Transform a point from device to logical coordinates.
242 // wxTransformMatrix mat = dc.GetTransformation();
244 // mat.InverseTransformPoint(x, y, x1, y1);
246 // dc.LogicalToDevice(x, y, x1, y1);
247 // The latter is slightly less efficient if we're doing several
248 // conversions, since the matrix is inverted several times.
250 bool wxTransformMatrix::InverseTransformPoint(double x
, double y
, double& tx
, double& ty
) const
254 tx
= x
; ty
= y
; return TRUE
;
257 double z
= (1.0 - m_matrix
[0][2] * x
- m_matrix
[1][2] * y
) / m_matrix
[2][2];
263 tx
= x
* m_matrix
[0][0] + y
* m_matrix
[1][0] + z
* m_matrix
[2][0];
264 ty
= x
* m_matrix
[0][1] + y
* m_matrix
[1][1] + z
* m_matrix
[2][1];