1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTransformMatrix class. NOT YET USED
4 //! Author: Chris Breeze, Julian Smart
5 // Modified by: Klaas Holwerda
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "matrix.h"
19 //! headerfiles="matrix.h wx/object.h"
20 #include "wx/object.h"
22 //! codefiles="matrix.cpp"
24 // A simple 3x3 matrix. This may be replaced by a more general matrix
27 // Note: this is intended to be used in wxDC at some point to replace
28 // the current system of scaling/translation. It is not yet used.
31 // A 3x3 matrix to do 2D transformations.
32 // It can be used to map data to window coordinates.
33 // But also for manipulating your own data.
34 // For example drawing a picture (composed of several primitives)
35 // at a certain coordinate and angle within another parent picture.
36 // At all times m_isIdentity is set if the matrix itself is an Identity matrix.
37 // It is used where possible to optimize calculations.
38 class WXDLLEXPORT wxTransformMatrix
: public wxObject
41 wxTransformMatrix(void);
42 wxTransformMatrix(const wxTransformMatrix
& mat
);
44 //get the value in the matrix at col,row
45 //rows are horizontal (second index of m_matrix member)
46 //columns are vertical (first index of m_matrix member)
47 double GetValue(int col
, int row
) const;
49 //set the value in the matrix at col,row
50 //rows are horizontal (second index of m_matrix member)
51 //columns are vertical (first index of m_matrix member)
52 void SetValue(int col
, int row
, double value
);
54 void operator = (const wxTransformMatrix
& mat
);
55 bool operator == (const wxTransformMatrix
& mat
);
56 bool operator != (const wxTransformMatrix
& mat
);
58 //multiply every element by t
59 wxTransformMatrix
& operator*=(const double& t
);
60 //divide every element by t
61 wxTransformMatrix
& operator/=(const double& t
);
62 //add matrix m to this t
63 wxTransformMatrix
& operator+=(const wxTransformMatrix
& m
);
64 //subtract matrix m from this
65 wxTransformMatrix
& operator-=(const wxTransformMatrix
& m
);
66 //multiply matrix m with this
67 wxTransformMatrix
& operator*=(const wxTransformMatrix
& m
);
71 //multiply every element by t and return result
72 wxTransformMatrix
operator*(const double& t
) const;
73 //divide this matrix by t and return result
74 wxTransformMatrix
operator/(const double& t
) const;
75 //add matrix m to this and return result
76 wxTransformMatrix
operator+(const wxTransformMatrix
& m
) const;
77 //subtract matrix m from this and return result
78 wxTransformMatrix
operator-(const wxTransformMatrix
& m
) const;
79 //multiply this by matrix m and return result
80 wxTransformMatrix
operator*(const wxTransformMatrix
& m
) const;
81 wxTransformMatrix
operator-() const;
83 //rows are horizontal (second index of m_matrix member)
84 //columns are vertical (first index of m_matrix member)
85 double& operator()(int col
, int row
);
87 //rows are horizontal (second index of m_matrix member)
88 //columns are vertical (first index of m_matrix member)
89 double operator()(int col
, int row
) const;
94 // Make into identity matrix
97 // Is the matrix the identity matrix?
98 // Only returns a flag, which is set whenever an operation
100 inline bool IsIdentity(void) const { return m_isIdentity
; };
102 // This does an actual check.
103 inline bool IsIdentity1(void) const ;
105 //Scale by scale (isotropic scaling i.e. the same in x and y):
107 //!code: | scale 0 0 |
108 //!code: matrix' = | 0 scale 0 | x matrix
109 //!code: | 0 0 scale |
110 bool Scale(double scale
);
112 //Scale with center point and x/y scale
115 //!code: | xs 0 xc(1-xs) |
116 //!code: matrix' = | 0 ys yc(1-ys) | x matrix
118 wxTransformMatrix
& Scale(const double &xs
, const double &ys
,const double &xc
, const double &yc
);
120 // mirror a matrix in x, y
123 //!code: matrix' = | 0 -1 0 | x matrix
125 wxTransformMatrix
& Mirror(bool x
=TRUE
, bool y
=FALSE
);
126 // Translate by dx, dy:
129 //!code: matrix' = | 0 1 dy | x matrix
131 bool Translate(double x
, double y
);
133 // Rotate clockwise by the given number of degrees:
135 //!code: | cos sin 0 |
136 //!code: matrix' = | -sin cos 0 | x matrix
138 bool Rotate(double angle
);
140 //Rotate counter clockwise with point of rotation
143 //!code: | cos(r) -sin(r) x(1-cos(r))+y(sin(r)|
144 //!code: matrix' = | sin(r) cos(r) y(1-cos(r))-x(sin(r)| x matrix
146 wxTransformMatrix
& Rotate(const double &r
, const double &x
, const double &y
);
148 // Transform X value from logical to device
149 inline double TransformX(double x
) const;
151 // Transform Y value from logical to device
152 inline double TransformY(double y
) const;
154 // Transform a point from logical to device coordinates
155 bool TransformPoint(double x
, double y
, double& tx
, double& ty
) const;
157 // Transform a point from device to logical coordinates.
159 // wxTransformMatrix mat = dc.GetTransformation();
161 // mat.InverseTransformPoint(x, y, x1, y1);
163 // dc.LogicalToDevice(x, y, x1, y1);
164 // The latter is slightly less efficient if we're doing several
165 // conversions, since the matrix is inverted several times.
166 // N.B. 'this' matrix is the inverse at this point
167 bool InverseTransformPoint(double x
, double y
, double& tx
, double& ty
) const;
171 double GetRotation();
172 void SetRotation(double rotation
);
176 double m_matrix
[3][3];
182 Chris Breeze reported, that
183 some functions of wxTransformMatrix cannot work because it is not
184 known if he matrix has been inverted. Be careful when using it.
187 // Transform X value from logical to device
188 // warning: this function can only be used for this purpose
189 // because no rotation is involved when mapping logical to device coordinates
190 // mirror and scaling for x and y will be part of the matrix
191 // if you have a matrix that is rotated, eg a shape containing a matrix to place
192 // it in the logical coordinate system, use TransformPoint
193 inline double wxTransformMatrix::TransformX(double x
) const
195 //normally like this, but since no rotation is involved (only mirror and scale)
196 //we can do without Y -> m_matrix[1]{0] is -sin(rotation angle) and therefore zero
197 //(x * m_matrix[0][0] + y * m_matrix[1][0] + m_matrix[2][0]))
198 return (m_isIdentity
? x
: (x
* m_matrix
[0][0] + m_matrix
[2][0]));
201 // Transform Y value from logical to device
202 // warning: this function can only be used for this purpose
203 // because no rotation is involved when mapping logical to device coordinates
204 // mirror and scaling for x and y will be part of the matrix
205 // if you have a matrix that is rotated, eg a shape containing a matrix to place
206 // it in the logical coordinate system, use TransformPoint
207 inline double wxTransformMatrix::TransformY(double y
) const
209 //normally like this, but since no rotation is involved (only mirror and scale)
210 //we can do without X -> m_matrix[0]{1] is sin(rotation angle) and therefore zero
211 //(x * m_matrix[0][1] + y * m_matrix[1][1] + m_matrix[2][1]))
212 return (m_isIdentity
? y
: (y
* m_matrix
[1][1] + m_matrix
[2][1]));
216 // Is the matrix the identity matrix?
217 // Each operation checks whether the result is still the identity matrix and sets a flag.
218 inline bool wxTransformMatrix::IsIdentity1(void) const
221 (m_matrix
[0][0] == 1.0 &&
222 m_matrix
[1][1] == 1.0 &&
223 m_matrix
[2][2] == 1.0 &&
224 m_matrix
[1][0] == 0.0 &&
225 m_matrix
[2][0] == 0.0 &&
226 m_matrix
[0][1] == 0.0 &&
227 m_matrix
[2][1] == 0.0 &&
228 m_matrix
[0][2] == 0.0 &&
229 m_matrix
[1][2] == 0.0) ;
232 // Calculates the determinant of a 2 x 2 matrix
233 inline double wxCalculateDet(double a11
, double a21
, double a12
, double a22
)
235 return a11
* a22
- a12
* a21
;