Pangolin
Header-only C++20 plane computational geometry library
Loading...
Searching...
No Matches
pgl::Transformation< Number > Struct Template Reference

Affine transformation stored as a 2x3 matrix. More...

#include <transformation.hpp>

Inheritance diagram for pgl::Transformation< Number >:
[legend]

Public Types

using NumberType = Number

Public Member Functions

constexpr Transformation ()=default
 Creates the identity transformation.
constexpr Transformation (Number a, Number b, Number c, Number d, Number tx=Number{}, Number ty=Number{})
 Creates a transformation from its matrix entries.
template<class OtherNumber>
constexpr Transformation (const Transformation< OtherNumber > &other)
 Converts a transformation with a different entry type.
constexpr Number a () const
 Returns the row-0, column-0 matrix entry.
constexpr Number b () const
 Returns the row-0, column-1 matrix entry.
constexpr Number c () const
 Returns the row-1, column-0 matrix entry.
constexpr Number d () const
 Returns the row-1, column-1 matrix entry.
constexpr Number tx () const
 Returns the translation added to the first coordinate.
constexpr Number ty () const
 Returns the translation added to the second coordinate.
constexpr auto determinant () const
 Returns the determinant of the linear (non-translation) part.
constexpr bool isInvertible () const
 Tests whether the transformation has an inverse.
template<class ResultNumber = division_result_t<NumberType>>
constexpr Transformation< ResultNumber > inverse () const
 Returns the inverse transformation.
template<class OtherNumber>
constexpr auto operator* (const Transformation< OtherNumber > &other) const
 Composes two transformations: (*this) * other applies other first, then *this.
constexpr bool operator== (const Transformation &) const =default
 Compares the matrix entries for equality.

Static Public Member Functions

static constexpr Transformation identity ()
 Returns the identity transformation.
static constexpr Transformation translation (Number dx, Number dy)
 Returns a translation by (dx, dy).
static constexpr Transformation scaling (Number sx, Number sy)
 Returns a non-uniform scaling around the origin.
static constexpr Transformation scaling (Number s)
 Returns a uniform scaling around the origin.
static constexpr Transformation rotation90 (int k=1)
 Returns a rotation around the origin by 90k degrees.
static constexpr Transformation shearX (Number k)
 Returns a horizontal shear: (x, y) -> (x + k*y, y).
static constexpr Transformation shearY (Number k)
 Returns a vertical shear: (x, y) -> (x, y + k*x).
static constexpr Transformation reflectionX ()
 Returns a reflection across the x-axis.
static constexpr Transformation reflectionY ()
 Returns a reflection across the y-axis.
template<std::floating_point ResultNumber = double>
static Transformation< ResultNumber > rotation (ResultNumber radians)
 Returns a rotation around the origin by an arbitrary angle.

Detailed Description

template<class Number>
struct pgl::Transformation< Number >

Affine transformation stored as a 2x3 matrix.

Affine transformation of the plane, stored as a 2x3 matrix.

Applied to a point or shape with operator*; composed with another transformation with operator* as well.

Template Parameters
NumberMatrix entry / coordinate type.
NumberMatrix entry / coordinate type.

Member Typedef Documentation

◆ NumberType

template<class Number>
using pgl::Transformation< Number >::NumberType = Number

Type of the matrix entries.

Constructor & Destructor Documentation

◆ Transformation() [1/3]

template<class Number>
pgl::Transformation< Number >::Transformation ( )
constexprdefault

Creates the identity transformation.

◆ Transformation() [2/3]

template<class Number>
pgl::Transformation< Number >::Transformation ( Number a,
Number b,
Number c,
Number d,
Number tx = Number{},
Number ty = Number{} )
inlineconstexpr

Creates a transformation from its matrix entries.

Parameters
aRow 0, column 0 of the linear part.
bRow 0, column 1 of the linear part.
cRow 1, column 0 of the linear part.
dRow 1, column 1 of the linear part.
txTranslation added to the first coordinate.
tyTranslation added to the second coordinate.

◆ Transformation() [3/3]

template<class Number>
template<class OtherNumber>
pgl::Transformation< Number >::Transformation ( const Transformation< OtherNumber > & other)
inlineconstexpr

Converts a transformation with a different entry type.

Template Parameters
OtherNumberEntry type of the source transformation.
Parameters
otherSource transformation.

Member Function Documentation

◆ a()

template<class Number>
Number pgl::Transformation< Number >::a ( ) const
inlineconstexpr

Returns the row-0, column-0 matrix entry.

◆ b()

template<class Number>
Number pgl::Transformation< Number >::b ( ) const
inlineconstexpr

Returns the row-0, column-1 matrix entry.

◆ c()

template<class Number>
Number pgl::Transformation< Number >::c ( ) const
inlineconstexpr

Returns the row-1, column-0 matrix entry.

◆ d()

template<class Number>
Number pgl::Transformation< Number >::d ( ) const
inlineconstexpr

Returns the row-1, column-1 matrix entry.

◆ determinant()

template<class Number>
auto pgl::Transformation< Number >::determinant ( ) const
inlineconstexpr

Returns the determinant of the linear (non-translation) part.

Negative when the transformation reverses orientation (a reflection or an odd number of shears/reflections composed together), zero when it collapses the plane onto a line or point.

◆ identity()

template<class Number>
constexpr Transformation pgl::Transformation< Number >::identity ( )
inlinestaticconstexpr

Returns the identity transformation.

◆ inverse()

template<class Number>
template<class ResultNumber = division_result_t<NumberType>>
Transformation< ResultNumber > pgl::Transformation< Number >::inverse ( ) const
inlineconstexpr

Returns the inverse transformation.

Template Parameters
ResultNumberMatrix entry type of the result (default: division_result_t).
Warning
Divides by determinant. For an integral NumberType this is inexact unless ResultNumber is a type such as Rational<Number> that represents the division exactly.

◆ isInvertible()

template<class Number>
bool pgl::Transformation< Number >::isInvertible ( ) const
inlineconstexpr

Tests whether the transformation has an inverse.

Returns
true when determinant is nonzero.

◆ operator*()

template<class Number>
template<class OtherNumber>
auto pgl::Transformation< Number >::operator* ( const Transformation< OtherNumber > & other) const
inlineconstexpr

Composes two transformations: (*this) * other applies other first, then *this.

Template Parameters
OtherNumberEntry type of other.

◆ operator==()

template<class Number>
bool pgl::Transformation< Number >::operator== ( const Transformation< Number > & ) const
constexprdefault

Compares the matrix entries for equality.

◆ reflectionX()

template<class Number>
constexpr Transformation pgl::Transformation< Number >::reflectionX ( )
inlinestaticconstexpr

Returns a reflection across the x-axis.

◆ reflectionY()

template<class Number>
constexpr Transformation pgl::Transformation< Number >::reflectionY ( )
inlinestaticconstexpr

Returns a reflection across the y-axis.

◆ rotation()

template<class Number>
template<std::floating_point ResultNumber = double>
Transformation< ResultNumber > pgl::Transformation< Number >::rotation ( ResultNumber radians)
inlinestatic

Returns a rotation around the origin by an arbitrary angle.

Unlike rotation90, an arbitrary angle is generally irrational, so this requires an explicit floating-point ResultNumber: there is no exact-by-default overload to silently fall back from.

Template Parameters
ResultNumberFloating-point matrix entry type of the result.
Parameters
radiansRotation angle in radians.

◆ rotation90()

template<class Number>
constexpr Transformation pgl::Transformation< Number >::rotation90 ( int k = 1)
inlinestaticconstexpr

Returns a rotation around the origin by 90k degrees.

Exact for every NumberType since the matrix entries are 0, 1, or -1.

Parameters
kNumber of 90-degree CCW rotations (may be negative).

◆ scaling() [1/2]

template<class Number>
constexpr Transformation pgl::Transformation< Number >::scaling ( Number s)
inlinestaticconstexpr

Returns a uniform scaling around the origin.

Parameters
sFactor applied to both coordinates.

◆ scaling() [2/2]

template<class Number>
constexpr Transformation pgl::Transformation< Number >::scaling ( Number sx,
Number sy )
inlinestaticconstexpr

Returns a non-uniform scaling around the origin.

Parameters
sxFactor applied to the x coordinate.
syFactor applied to the y coordinate.

◆ shearX()

template<class Number>
constexpr Transformation pgl::Transformation< Number >::shearX ( Number k)
inlinestaticconstexpr

Returns a horizontal shear: (x, y) -> (x + k*y, y).

◆ shearY()

template<class Number>
constexpr Transformation pgl::Transformation< Number >::shearY ( Number k)
inlinestaticconstexpr

Returns a vertical shear: (x, y) -> (x, y + k*x).

◆ translation()

template<class Number>
constexpr Transformation pgl::Transformation< Number >::translation ( Number dx,
Number dy )
inlinestaticconstexpr

Returns a translation by (dx, dy).

◆ tx()

template<class Number>
Number pgl::Transformation< Number >::tx ( ) const
inlineconstexpr

Returns the translation added to the first coordinate.

◆ ty()

template<class Number>
Number pgl::Transformation< Number >::ty ( ) const
inlineconstexpr

Returns the translation added to the second coordinate.