1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTransformMatrix class. NOT YET USED
4 // Author: Chris Breeze, Julian Smart
5 // Modified by: Klaas Holwerda
8 // Copyright: (c) Julian Smart, Chris Breeze
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 //! headerfiles="matrix.h wx/object.h"
16 #include "wx/object.h"
19 //! codefiles="matrix.cpp"
21 // A simple 3x3 matrix. This may be replaced by a more general matrix
24 // Note: this is intended to be used in wxDC at some point to replace
25 // the current system of scaling/translation. It is not yet used.
28 // A 3x3 matrix to do 2D transformations.
29 // It can be used to map data to window coordinates,
30 // and also for manipulating your own data.
31 // For example drawing a picture (composed of several primitives)
32 // at a certain coordinate and angle within another parent picture.
33 // At all times m_isIdentity is set if the matrix itself is an Identity matrix.
34 // It is used where possible to optimize calculations.
35 class WXDLLEXPORT wxTransformMatrix
: public wxObject
38 wxTransformMatrix(void);
39 wxTransformMatrix(const wxTransformMatrix
& mat
);
41 //get the value in the matrix at col,row
42 //rows are horizontal (second index of m_matrix member)
43 //columns are vertical (first index of m_matrix member)
44 double GetValue(int col
, int row
) const;
46 //set the value in the matrix at col,row
47 //rows are horizontal (second index of m_matrix member)
48 //columns are vertical (first index of m_matrix member)
49 void SetValue(int col
, int row
, double value
);
51 void operator = (const wxTransformMatrix
& mat
);
52 bool operator == (const wxTransformMatrix
& mat
) const;
53 bool operator != (const wxTransformMatrix
& mat
) const;
55 //multiply every element by t
56 wxTransformMatrix
& operator*=(const double& t
);
57 //divide every element by t
58 wxTransformMatrix
& operator/=(const double& t
);
59 //add matrix m to this t
60 wxTransformMatrix
& operator+=(const wxTransformMatrix
& m
);
61 //subtract matrix m from this
62 wxTransformMatrix
& operator-=(const wxTransformMatrix
& m
);
63 //multiply matrix m with this
64 wxTransformMatrix
& operator*=(const wxTransformMatrix
& m
);
68 //multiply every element by t and return result
69 wxTransformMatrix
operator*(const double& t
) const;
70 //divide this matrix by t and return result
71 wxTransformMatrix
operator/(const double& t
) const;
72 //add matrix m to this and return result
73 wxTransformMatrix
operator+(const wxTransformMatrix
& m
) const;
74 //subtract matrix m from this and return result
75 wxTransformMatrix
operator-(const wxTransformMatrix
& m
) const;
76 //multiply this by matrix m and return result
77 wxTransformMatrix
operator*(const wxTransformMatrix
& m
) const;
78 wxTransformMatrix
operator-() const;
80 //rows are horizontal (second index of m_matrix member)
81 //columns are vertical (first index of m_matrix member)
82 double& operator()(int col
, int row
);
84 //rows are horizontal (second index of m_matrix member)
85 //columns are vertical (first index of m_matrix member)
86 double operator()(int col
, int row
) const;
91 // Make into identity matrix
94 // Is the matrix the identity matrix?
95 // Only returns a flag, which is set whenever an operation
97 inline bool IsIdentity(void) const { return m_isIdentity
; }
99 // This does an actual check.
100 inline bool IsIdentity1(void) const ;
102 //Scale by scale (isotropic scaling i.e. the same in x and y):
104 //!code: | scale 0 0 |
105 //!code: matrix' = | 0 scale 0 | x matrix
106 //!code: | 0 0 scale |
107 bool Scale(double scale
);
109 //Scale with center point and x/y scale
112 //!code: | xs 0 xc(1-xs) |
113 //!code: matrix' = | 0 ys yc(1-ys) | x matrix
115 wxTransformMatrix
& Scale(const double &xs
, const double &ys
,const double &xc
, const double &yc
);
117 // mirror a matrix in x, y
120 //!code: matrix' = | 0 -1 0 | x matrix
122 wxTransformMatrix
& Mirror(bool x
=true, bool y
=false);
123 // Translate by dx, dy:
126 //!code: matrix' = | 0 1 dy | x matrix
128 bool Translate(double x
, double y
);
130 // Rotate clockwise by the given number of degrees:
132 //!code: | cos sin 0 |
133 //!code: matrix' = | -sin cos 0 | x matrix
135 bool Rotate(double angle
);
137 //Rotate counter clockwise with point of rotation
140 //!code: | cos(r) -sin(r) x(1-cos(r))+y(sin(r)|
141 //!code: matrix' = | sin(r) cos(r) y(1-cos(r))-x(sin(r)| x matrix
143 wxTransformMatrix
& Rotate(const double &r
, const double &x
, const double &y
);
145 // Transform X value from logical to device
146 inline double TransformX(double x
) const;
148 // Transform Y value from logical to device
149 inline double TransformY(double y
) const;
151 // Transform a point from logical to device coordinates
152 bool TransformPoint(double x
, double y
, double& tx
, double& ty
) const;
154 // Transform a point from device to logical coordinates.
156 // wxTransformMatrix mat = dc.GetTransformation();
158 // mat.InverseTransformPoint(x, y, x1, y1);
160 // dc.LogicalToDevice(x, y, x1, y1);
161 // The latter is slightly less efficient if we're doing several
162 // conversions, since the matrix is inverted several times.
163 // N.B. 'this' matrix is the inverse at this point
164 bool InverseTransformPoint(double x
, double y
, double& tx
, double& ty
) const;
168 double GetRotation();
169 void SetRotation(double rotation
);
173 double m_matrix
[3][3];
179 Chris Breeze reported, that
180 some functions of wxTransformMatrix cannot work because it is not
181 known if he matrix has been inverted. Be careful when using it.
184 // Transform X value from logical to device
185 // warning: this function can only be used for this purpose
186 // because no rotation is involved when mapping logical to device coordinates
187 // mirror and scaling for x and y will be part of the matrix
188 // if you have a matrix that is rotated, eg a shape containing a matrix to place
189 // it in the logical coordinate system, use TransformPoint
190 inline double wxTransformMatrix::TransformX(double x
) const
192 //normally like this, but since no rotation is involved (only mirror and scale)
193 //we can do without Y -> m_matrix[1]{0] is -sin(rotation angle) and therefore zero
194 //(x * m_matrix[0][0] + y * m_matrix[1][0] + m_matrix[2][0]))
195 return (m_isIdentity
? x
: (x
* m_matrix
[0][0] + m_matrix
[2][0]));
198 // Transform Y value from logical to device
199 // warning: this function can only be used for this purpose
200 // because no rotation is involved when mapping logical to device coordinates
201 // mirror and scaling for x and y will be part of the matrix
202 // if you have a matrix that is rotated, eg a shape containing a matrix to place
203 // it in the logical coordinate system, use TransformPoint
204 inline double wxTransformMatrix::TransformY(double y
) const
206 //normally like this, but since no rotation is involved (only mirror and scale)
207 //we can do without X -> m_matrix[0]{1] is sin(rotation angle) and therefore zero
208 //(x * m_matrix[0][1] + y * m_matrix[1][1] + m_matrix[2][1]))
209 return (m_isIdentity
? y
: (y
* m_matrix
[1][1] + m_matrix
[2][1]));
213 // Is the matrix the identity matrix?
214 // Each operation checks whether the result is still the identity matrix and sets a flag.
215 inline bool wxTransformMatrix::IsIdentity1(void) const
218 ( wxIsSameDouble(m_matrix
[0][0], 1.0) &&
219 wxIsSameDouble(m_matrix
[1][1], 1.0) &&
220 wxIsSameDouble(m_matrix
[2][2], 1.0) &&
221 wxIsSameDouble(m_matrix
[1][0], 0.0) &&
222 wxIsSameDouble(m_matrix
[2][0], 0.0) &&
223 wxIsSameDouble(m_matrix
[0][1], 0.0) &&
224 wxIsSameDouble(m_matrix
[2][1], 0.0) &&
225 wxIsSameDouble(m_matrix
[0][2], 0.0) &&
226 wxIsSameDouble(m_matrix
[1][2], 0.0) );
229 // Calculates the determinant of a 2 x 2 matrix
230 inline double wxCalculateDet(double a11
, double a21
, double a12
, double a22
)
232 return a11
* a22
- a12
* a21
;
235 #endif // _WX_MATRIXH__