dropped in new wxMatrix (from wxCanvas)

use wxUSE_GEOMETRY


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8795 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2000-11-24 12:40:19 +00:00
parent 147a1f7e70
commit 555526fbff
2 changed files with 167 additions and 75 deletions

View File

@@ -18,10 +18,6 @@
#include "wx/defs.h" #include "wx/defs.h"
#ifndef wxUSE_GEOMETRY
#define wxUSE_GEOMETRY 0
#endif
#if wxUSE_GEOMETRY #if wxUSE_GEOMETRY
#include "wx/utils.h" #include "wx/utils.h"

View File

@@ -1,8 +1,8 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: matrix.h // Name: matrix.h
// Purpose: wxTransformMatrix class. NOT YET USED // Purpose: wxTransformMatrix class. NOT YET USED
// Author: Chris Breeze, Julian Smart //! Author: Chris Breeze, Julian Smart
// Modified by: // Modified by: Klaas Holwerda
// Created: 01/02/97 // Created: 01/02/97
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Julian Smart and Markus Holzem // Copyright: (c) Julian Smart and Markus Holzem
@@ -16,29 +16,77 @@
#pragma interface "matrix.h" #pragma interface "matrix.h"
#endif #endif
//! headerfiles="matrix.h wx/object.h"
#include "wx/object.h" #include "wx/object.h"
//! codefiles="matrix.cpp"
// A simple 3x3 matrix. This may be replaced by a more general matrix // A simple 3x3 matrix. This may be replaced by a more general matrix
// class some day. // class some day.
// //
// Note: this is intended to be used in wxDC at some point to replace // Note: this is intended to be used in wxDC at some point to replace
// the current system of scaling/translation. It is not yet used. // the current system of scaling/translation. It is not yet used.
//:defenition
// A 3x3 matrix to do 2D transformations.
// It can be used to map data to window coordinates.
// But also for manipulating your own data.
// For example drawing a picture (composed of several primitives)
// at a certain coordinate and angle within another parent picture.
// At all times m_isIdentity is set if the matrix itself is an Identity matrix.
// It is used where possible to optimize calculations.
class WXDLLEXPORT wxTransformMatrix: public wxObject class WXDLLEXPORT wxTransformMatrix: public wxObject
{ {
public: public:
wxTransformMatrix(void); wxTransformMatrix(void);
wxTransformMatrix(const wxTransformMatrix& mat); wxTransformMatrix(const wxTransformMatrix& mat);
double GetValue(int row, int col) const; //get the value in the matrix at col,row
void SetValue(int row, int col, double value); //rows are horizontal (second index of m_matrix member)
//columns are vertical (first index of m_matrix member)
double GetValue(int col, int row) const;
//set the value in the matrix at col,row
//rows are horizontal (second index of m_matrix member)
//columns are vertical (first index of m_matrix member)
void SetValue(int col, int row, double value);
void operator = (const wxTransformMatrix& mat); void operator = (const wxTransformMatrix& mat);
bool operator == (const wxTransformMatrix& mat); bool operator == (const wxTransformMatrix& mat);
bool operator != (const wxTransformMatrix& mat); bool operator != (const wxTransformMatrix& mat);
double& operator()(int row, int col); //multiply every element by t
double operator()(int row, int col) const; wxTransformMatrix& operator*=(const double& t);
//divide every element by t
wxTransformMatrix& operator/=(const double& t);
//add matrix m to this t
wxTransformMatrix& operator+=(const wxTransformMatrix& m);
//subtract matrix m from this
wxTransformMatrix& operator-=(const wxTransformMatrix& m);
//multiply matrix m with this
wxTransformMatrix& operator*=(const wxTransformMatrix& m);
// constant operators
//multiply every element by t and return result
wxTransformMatrix operator*(const double& t) const;
//divide this matrix by t and return result
wxTransformMatrix operator/(const double& t) const;
//add matrix m to this and return result
wxTransformMatrix operator+(const wxTransformMatrix& m) const;
//subtract matrix m from this and return result
wxTransformMatrix operator-(const wxTransformMatrix& m) const;
//multiply this by matrix m and return result
wxTransformMatrix operator*(const wxTransformMatrix& m) const;
wxTransformMatrix operator-() const;
//rows are horizontal (second index of m_matrix member)
//columns are vertical (first index of m_matrix member)
double& operator()(int col, int row);
//rows are horizontal (second index of m_matrix member)
//columns are vertical (first index of m_matrix member)
double operator()(int col, int row) const;
// Invert matrix // Invert matrix
bool Invert(void); bool Invert(void);
@@ -54,15 +102,50 @@ public:
// This does an actual check. // This does an actual check.
inline bool IsIdentity1(void) const ; inline bool IsIdentity1(void) const ;
// Isotropic scaling //Scale by scale (isotropic scaling i.e. the same in x and y):
//!ex:
//!code: | scale 0 0 |
//!code: matrix' = | 0 scale 0 | x matrix
//!code: | 0 0 scale |
bool Scale(double scale); bool Scale(double scale);
// Translate //Scale with center point and x/y scale
//
//!ex:
//!code: | xs 0 xc(1-xs) |
//!code: matrix' = | 0 ys yc(1-ys) | x matrix
//!code: | 0 0 1 |
wxTransformMatrix& Scale(const double &xs, const double &ys,const double &xc, const double &yc);
// mirror a matrix in x, y
//!ex:
//!code: | -1 0 0 |
//!code: matrix' = | 0 -1 0 | x matrix
//!code: | 0 0 1 |
wxTransformMatrix& Mirror(bool x=true, bool y=false);
// Translate by dx, dy:
//!ex:
//!code: | 1 0 dx |
//!code: matrix' = | 0 1 dy | x matrix
//!code: | 0 0 1 |
bool Translate(double x, double y); bool Translate(double x, double y);
// Rotate // Rotate clockwise by the given number of degrees:
//!ex:
//!code: | cos sin 0 |
//!code: matrix' = | -sin cos 0 | x matrix
//!code: | 0 0 1 |
bool Rotate(double angle); bool Rotate(double angle);
//Rotate counter clockwise with point of rotation
//
//!ex:
//!code: | cos(r) -sin(r) x(1-cos(r))+y(sin(r)|
//!code: matrix' = | sin(r) cos(r) y(1-cos(r))-x(sin(r)| x matrix
//!code: | 0 0 1 |
wxTransformMatrix& Rotate(const double &r, const double &x, const double &y);
// Transform X value from logical to device // Transform X value from logical to device
inline double TransformX(double x) const; inline double TransformX(double x) const;
@@ -73,7 +156,6 @@ public:
bool TransformPoint(double x, double y, double& tx, double& ty) const; bool TransformPoint(double x, double y, double& tx, double& ty) const;
// Transform a point from device to logical coordinates. // Transform a point from device to logical coordinates.
// Example of use: // Example of use:
// wxTransformMatrix mat = dc.GetTransformation(); // wxTransformMatrix mat = dc.GetTransformation();
// mat.Invert(); // mat.Invert();
@@ -82,44 +164,58 @@ public:
// dc.LogicalToDevice(x, y, x1, y1); // dc.LogicalToDevice(x, y, x1, y1);
// The latter is slightly less efficient if we're doing several // The latter is slightly less efficient if we're doing several
// conversions, since the matrix is inverted several times. // conversions, since the matrix is inverted several times.
// N.B. 'this' matrix is the inverse at this point // N.B. 'this' matrix is the inverse at this point
bool InverseTransformPoint(double x, double y, double& tx, double& ty) const; bool InverseTransformPoint(double x, double y, double& tx, double& ty) const;
double Get_scaleX();
double Get_scaleY();
double GetRotation();
void SetRotation(double rotation);
public: public:
double m_matrix[3][3]; double m_matrix[3][3];
bool m_isIdentity; bool m_isIdentity;
/*
double m11, m21, m31;
double m12, m22, m32;
double m13, m23, m33;
*/
}; };
/* /*
The code is wrong and doesn't compile. Chris Breeze als reported, that Chris Breeze reported, that
some functions of wxTransformMatrix cannot work because it is not some functions of wxTransformMatrix cannot work because it is not
known if he matrix has been inverted. Be careful when using it. known if he matrix has been inverted. Be careful when using it.
*/
// Transform X value from logical to device // Transform X value from logical to device
// warning: this function can only be used for this purpose
// because no rotation is involved when mapping logical to device coordinates
// mirror and scaling for x and y will be part of the matrix
// if you have a matrix that is rotated, eg a shape containing a matrix to place
// it in the logical coordinate system, use TransformPoint
inline double wxTransformMatrix::TransformX(double x) const inline double wxTransformMatrix::TransformX(double x) const
{ {
return (m_isIdentity ? x : (x * m_matrix[0][0] + y * m_matrix[1][0] + m_matrix[2][0])); //normally like this, but since no rotation is involved (only mirror and scale)
//we can do without Y -> m_matrix[1]{0] is -sin(rotation angle) and therefore zero
//(x * m_matrix[0][0] + y * m_matrix[1][0] + m_matrix[2][0]))
return (m_isIdentity ? x : (x * m_matrix[0][0] + m_matrix[2][0]));
} }
// Transform Y value from logical to device // Transform Y value from logical to device
// warning: this function can only be used for this purpose
// because no rotation is involved when mapping logical to device coordinates
// mirror and scaling for x and y will be part of the matrix
// if you have a matrix that is rotated, eg a shape containing a matrix to place
// it in the logical coordinate system, use TransformPoint
inline double wxTransformMatrix::TransformY(double y) const inline double wxTransformMatrix::TransformY(double y) const
{ {
return (m_isIdentity ? y : (x * m_matrix[0][1] + y * m_matrix[1][1] + m_matrix[2][1])); //normally like this, but since no rotation is involved (only mirror and scale)
//we can do without X -> m_matrix[0]{1] is sin(rotation angle) and therefore zero
//(x * m_matrix[0][1] + y * m_matrix[1][1] + m_matrix[2][1]))
return (m_isIdentity ? y : (y * m_matrix[1][1] + m_matrix[2][1]));
} }
*/
// Is the matrix the identity matrix? // Is the matrix the identity matrix?
// Perhaps there's some kind of optimization we can do to make this // Each operation checks whether the result is still the identity matrix and sets a flag.
// a faster operation. E.g. each operation (scale, translate etc.)
// checks whether it's still the identity matrix and sets a flag.
inline bool wxTransformMatrix::IsIdentity1(void) const inline bool wxTransformMatrix::IsIdentity1(void) const
{ {
return return