Affine or projective transformation in 2D. More...
#include <Transform2D.hpp>
Public Types | |
enum | Unit { DEGREES, RADIANS } |
enum | Error { DIVISION_BY_ZERO, INVALID_ARGUMENT, INVALID_UNIT, UNKNOWN_ERROR, WRONG_COORDINATE_SIZE } |
typedef T | value_type |
typedef Eigen::Matrix< T, 3, 3 > | Matrix3T |
typedef Eigen::Matrix< T, 3, 1 > | Vector3T |
typedef Eigen::Matrix< T, 2, 1 > | Vector2T |
typedef Coordinate< T > | coordinate_type |
Public Member Functions | |
Transform2D () | |
Constructs an instance of Transform2D. | |
Transform2D (const Matrix3T &) | |
Constructs an instance of Transform2D. | |
Transform2D (T a11, T a12, T a13, T a21, T a22, T a23, T a31, T a32, T a33) | |
Constructs an instance of Transform2D. | |
template<class U > | |
Transform2D (const Transform2D< U > &transform2D) | |
Copy constructor. | |
virtual | ~Transform2D () |
Destroys the instance of Transform2D. | |
T & | a11 () |
Element access (readable and writable). | |
T & | a12 () |
Element access (readable and writable). | |
T & | a13 () |
Element access (readable and writable). | |
T & | a21 () |
Element access (readable and writable). | |
T & | a22 () |
Element access (readable and writable). | |
T & | a23 () |
Element access (readable and writable). | |
T & | a31 () |
Element access (readable and writable). | |
T & | a32 () |
Element access (readable and writable). | |
T & | a33 () |
Element access (readable and writable). | |
const T & | a11 () const |
Read-only element access. | |
const T & | a12 () const |
Read-only element access. | |
const T & | a13 () const |
Read-only element access. | |
const T & | a21 () const |
Read-only element access. | |
const T & | a22 () const |
Read-only element access. | |
const T & | a23 () const |
Read-only element access. | |
const T & | a31 () const |
Read-only element access. | |
const T & | a32 () const |
Read-only element access. | |
const T & | a33 () const |
Read-only element access. | |
const Coordinate< T > | operator* (const Coordinate< T > &) const |
Transforms a coordinate with the internally saved transformation. | |
const Matrix3T | operator* (const Matrix3T &) const |
Composition of two transformations. | |
const Vector2T | operator* (const Vector2T &) const |
Transforms a 2D vector with the internally saved transformation. | |
const Vector3T | operator* (const Vector3T &) const |
Transforms a vector with the internally saved transformation. | |
const Transform2D | operator* (const Transform2D &) const |
Composition of two transformations. | |
const Transform2D | operator>> (const Transform2D &) const |
Composition of two transformations. | |
Matrix3T & | getTransformationMatrix () |
Returns the internal transformation matrix. | |
const Matrix3T & | getTransformationMatrix () const |
Returns the internal transformation matrix. | |
const Transform2D | inverse () const |
Returns the inverse transformation. | |
Transform2D & | reset () |
Sets the internal matrix to the identity matrix. | |
Transform2D & | rotate (const double angle, const Unit unit=RADIANS) |
Rotation. | |
Transform2D & | scale (const T sx, const T sy) |
Non-uniform scaling. | |
Transform2D & | shear (const T sx, const T sy) |
Shear mapping. | |
Transform2D & | translate (const T dx, const T dy) |
Translation. | |
Transform2D & | translate (const Coordinate< T > &position) |
Translation. | |
bool | is_affine () const |
Returns whether the internally saved transformation is affine or not. | |
Static Public Attributes | |
static const double | pi = 3.14159265358979323846264338327950288419716939937510 |
Affine or projective transformation in 2D.
This class provides means to transform 2D coordinates via an affine or a projective transformation. The transformation can be composed from a sequence of elementary transformations such as translation, scaling, rotation etc., or by directly setting the entries of a \( 3 \times 3 \) transformation matrix. The coordinates may be given as 2D or as 3D coordinates in case of homogeneous coordinates.
The transformation is computed by multiplying a \( 3 \times 3 \) matrix with a 3D homogeneous coordinate, followed by a potential normalization step. That is, if \( x \) is transformed to \( y \), first an intermediate vector \( w \) is computed
\[ \begin{pmatrix} w_1 \\ w_2 \\ w_3 \end{pmatrix} = \begin{pmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ 1 \end{pmatrix} \]
followed by a normalization step
\[ \begin{pmatrix} y_1 \\ y_2 \\ 1 \end{pmatrix} = \frac{1}{w_3} \begin{pmatrix} w_1 \\ w_2 \\ w_3 \end{pmatrix} \]
In case of affine transformations
\[ y = Ax + b \]
or
\[ \begin{pmatrix} y_1 \\ y_2 \\ 1 \end{pmatrix} = \begin{pmatrix} a_{11} & a_{12} & b_1 \\ a_{21} & a_{22} & b_2 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ 1 \end{pmatrix} \]
the subsequent normalization step can be omitted, since the last entry of the above intermediate vector \( w \) would be \( 1 \) in any event.
Most functions check for certain assertions (e.g., if the coordinate size is valid) and trigger an assertion failure, if the assumption does not hold. This is meant for debugging purposes only, and can be disabled by defining the macro NDEBUG
.
Here are some examples of how to use the Transform2D class:
Example 1:
TRTK::Coordinate<double> coordinate(1, 4); TRTK::Transform2D<double> transform; const double pi = transform::pi; transform.rotate(pi/2).translate(2, 0); cout << transform * coordinate << endl;
Output:
(-2, 1)
Example 2:
using namespace TRTK; Coordinate<double> coordinate(1, 4); Transform2D<double> transform1; Transform2D<double> transform2; Transform2D<double> transform3; Transform2D<double> transform4; transform1.translate(1, 1); // rotation of 90 degrees transform2.a11() = 0; transform2.a12() = 1; transform2.a21() = -1; transform2.a22() = 0; transform3.translate(-1, 0); transform4 = transform3 * transform2 * transform1; cout << transform4 * coordinate << endl; transform4.reset() = transform1 >> transform2 >> transform3; cout << transform4 * coordinate << endl; cout << endl << transform4.getTransformationMatrix() << endl;
Output:
(4, -2) (4, -2) 0 1 0 -1 0 -1 0 0 1
Definition at line 201 of file Transform2D.hpp.
TRTK::Transform2D< T >::Transform2D | ( | ) |
Constructs an instance of Transform2D.
T | scalar type |
The internal matrix is set to the identity matrix.
Definition at line 304 of file Transform2D.hpp.
TRTK::Transform2D< T >::Transform2D | ( | const Matrix3T & | matrix ) |
Constructs an instance of Transform2D.
T | scalar type |
[in] | matrix | Tranformation matrix. |
Sets the internal matrix to the given matrix
.
Definition at line 322 of file Transform2D.hpp.
TRTK::Transform2D< T >::Transform2D | ( | T | a11, |
T | a12, | ||
T | a13, | ||
T | a21, | ||
T | a22, | ||
T | a23, | ||
T | a31, | ||
T | a32, | ||
T | a33 | ||
) |
Constructs an instance of Transform2D.
T | scalar type |
Sets the internal matrix to the given coefficients.
Definition at line 351 of file Transform2D.hpp.
TRTK::Transform2D< T >::Transform2D | ( | const Transform2D< U > & | transform2D ) |
Copy constructor.
T | scalar type of the newly created instance |
U | scalar type of the copied instance |
Definition at line 384 of file Transform2D.hpp.
TRTK::Transform2D< T >::~Transform2D | ( | ) | [virtual] |
Destroys the instance of Transform2D.
T | scalar type |
Definition at line 409 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a11 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 424 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a11 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 571 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a12 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 440 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a12 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 587 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a13 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 456 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a13 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 603 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a21 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 472 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a21 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 619 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a22 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 488 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a22 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 635 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a23 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 504 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a23 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 651 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a31 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 667 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a31 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 520 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a32 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 537 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a32 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 683 of file Transform2D.hpp.
T & TRTK::Transform2D< T >::a33 | ( | ) |
Element access (readable and writable).
T | scalar type |
Definition at line 554 of file Transform2D.hpp.
const T & TRTK::Transform2D< T >::a33 | ( | ) | const |
Read-only element access.
T | scalar type |
Definition at line 699 of file Transform2D.hpp.
Transform2D< T >::Matrix3T & TRTK::Transform2D< T >::getTransformationMatrix | ( | ) |
Returns the internal transformation matrix.
T | scalar type |
Definition at line 1033 of file Transform2D.hpp.
const Transform2D< T >::Matrix3T & TRTK::Transform2D< T >::getTransformationMatrix | ( | ) | const |
Returns the internal transformation matrix.
T | scalar type |
Definition at line 1047 of file Transform2D.hpp.
const Transform2D< T > TRTK::Transform2D< T >::inverse | ( | ) | const |
Returns the inverse transformation.
T | scalar type |
Definition at line 1061 of file Transform2D.hpp.
bool TRTK::Transform2D< T >::is_affine | ( | ) | const [inline] |
Returns whether the internally saved transformation is affine or not.
T | scalar type |
Definition at line 1252 of file Transform2D.hpp.
const Coordinate< T > TRTK::Transform2D< T >::operator* | ( | const Coordinate< T > & | coordinate ) | const |
Transforms a coordinate with the internally saved transformation.
T | scalar type |
[in] | coordinate | Coordinate to be transformed. coordinate may be homogeneous. |
The given coordinate
must be of size two or three. If coordinate
has a size of three (homogeneous coordinate), it is assumed that its last component is equal to one.
Example:
TRTK::Coordinate<double> coordinate(1, 4); TRTK::Transform2D<double> transform; const double pi = TRTK::Transform2D<double>::pi; transform.rotate(pi/2).translate(2, 0); cout << transform * coordinate << endl;
Output:
(-2, 1)
Definition at line 738 of file Transform2D.hpp.
const Transform2D< T > TRTK::Transform2D< T >::operator* | ( | const Transform2D< T > & | transform ) | const |
Composition of two transformations.
T | scalar type |
[in] | transform | A transformation described by Transform2D. |
This function computes the composition of the currently internally saved transformation with the transformation given by transform
. Here, the composition is a simple matrix multiplication.
Example:
typedef TRTK::Transform2D<double> Transform; Transform T1(1, 1, 0, 0, 2, 2, 0, 0, 1); Transform T2(1, 0, 3, 0, -1, 0, 0, 0, 1); Transform T3 = T1 * T2; std::cout << T3.getTransformationMatrix();
Output:
1 -1 3 0 -2 2 0 0 1
Definition at line 964 of file Transform2D.hpp.
const Transform2D< T >::Vector3T TRTK::Transform2D< T >::operator* | ( | const Vector3T & | vector ) | const |
Transforms a vector with the internally saved transformation.
T | scalar type |
[in] | vector | 3D vector to be transformed. |
The vector is assumed to be a normalized homogeneous coordinate, i.e. its last component is assumed to be equal to one.
Example:
TRTK::Transform2D<double> transform; TRTK::Transform2D<double>::Vector3T vec(1, 4, 1); const double pi = TRTK::Transform2D<double>::pi; transform.rotateZ(pi/2).translate(2, 0); std::cout << transform * vec;
Output:
-2 1 1
Definition at line 856 of file Transform2D.hpp.
const Transform2D< T >::Vector2T TRTK::Transform2D< T >::operator* | ( | const Vector2T & | vector ) | const |
Transforms a 2D vector with the internally saved transformation.
T | scalar type |
[in] | vector | 2D vector to be transformed. |
Example:
TRTK::Transform2D<double> transform; TRTK::Transform2D<double>::Vector2T vec(1, 4); const double pi = TRTK::Transform2D<double>::pi; transform.rotateZ(pi/2).translate(2, 0); std::cout << transform * vec;
Output:
-2 1
Definition at line 807 of file Transform2D.hpp.
const Transform2D< T >::Matrix3T TRTK::Transform2D< T >::operator* | ( | const Matrix3T & | matrix ) | const |
Composition of two transformations.
T | scalar type |
[in] | matrix | 3x3 transformation matrix. |
This function computes the composition of the currently internally saved transformation with the transformation given by matrix
. Here, the composition is a simple matrix multiplication.
Example:
typedef TRTK::Transform2D<double> Transform; Transform T1(1, 1, 0, 0, 2, 2, 0, 0, 1); Transform::Matrix3T A; A << 1, 0, 3, 0, -1, 0, 0, 0, 1; Transform T2 = T1 * A; std::cout << T2.getTransformationMatrix();
Output:
1 -1 3 0 -2 2 0 0 1
Definition at line 913 of file Transform2D.hpp.
const Transform2D< T > TRTK::Transform2D< T >::operator>> | ( | const Transform2D< T > & | transform ) | const |
Composition of two transformations.
T | scalar type |
[in] | transform | A transformation described by Transform2D. |
This function computes the composition of the currently internally saved transformation with the transformation given by transform
. Here, the composition is a simple matrix multiplication.
Example:
typedef TRTK::Transform2D<double> Transform; Transform T1(1, 1, 0, 0, 2, 2, 0, 0, 1); Transform T2(1, 0, 3, 0, -1, 0, 0, 0, 1); Transform T3 = T1 >> T2; // == T2 * T1 std::cout << T3.getTransformationMatrix();
Output:
1 1 3 0 -2 -2 0 0 1
Definition at line 1016 of file Transform2D.hpp.
Transform2D< T > & TRTK::Transform2D< T >::reset | ( | ) |
Sets the internal matrix to the identity matrix.
T | scalar type |
*this
Definition at line 1075 of file Transform2D.hpp.
Transform2D< T > & TRTK::Transform2D< T >::rotate | ( | const double | angle, |
const Unit | unit = RADIANS |
||
) |
Rotation.
T | scalar type |
[in] | angle | Angle in degrees or radian. |
[in] | unit | The unit of angle . |
*this
Definition at line 1095 of file Transform2D.hpp.
Transform2D< T > & TRTK::Transform2D< T >::scale | ( | const T | sx, |
const T | sy | ||
) |
Non-uniform scaling.
T | scalar type |
[in] | sx | Scaling factor in x direction. |
[in] | sy | Scaling factor in y direction. |
This function scales the axes of the currently internally saved transformation.
*this
Definition at line 1147 of file Transform2D.hpp.
Transform2D< T > & TRTK::Transform2D< T >::shear | ( | const T | sx, |
const T | sy | ||
) |
Shear mapping.
T | scalar type |
[in] | sx | Horizontal shear (parallel to the x-axis). |
[in] | sy | Vertical shear (parallel to the y-axis). |
This function performs a shear mapping on the currently internally saved transformation.
*this
Definition at line 1175 of file Transform2D.hpp.
Transform2D< T > & TRTK::Transform2D< T >::translate | ( | const Coordinate< T > & | position ) |
Translation.
T | scalar type |
[in] | position | Translation (must be a coordinate of size 2) |
This function adds a translation to the currently stored transformation.
*this
Definition at line 1228 of file Transform2D.hpp.
Transform2D< T > & TRTK::Transform2D< T >::translate | ( | const T | dx, |
const T | dy | ||
) |
Translation.
T | scalar type |
[in] | dx | Translation in x direction. |
[in] | dy | Translation in y direction. |
This function adds a translation to the currently stored transformation.
*this
Definition at line 1202 of file Transform2D.hpp.
Documentation generated by Doxygen