CUGL
Cornell University Game Library
|
#include <CUAffine2.h>
Public Member Functions | |
Affine2 () | |
Affine2 (float m11, float m12, float m21, float m22, float tx, float ty) | |
Affine2 (const float *mat) | |
Affine2 (const Affine2 ©) | |
~Affine2 () | |
Affine2 & | operator= (const Affine2 &mat) |
Affine2 & | operator= (const float *array) |
Affine2 & | set (float m11, float m12, float m21, float m22, float tx, float ty) |
Affine2 & | set (const float *mat) |
Affine2 & | set (const Affine2 &mat) |
Affine2 & | setIdentity () |
Affine2 & | setZero () |
Affine2 & | add (const Vec2 &v) |
Affine2 & | subtract (const Vec2 &v) |
Affine2 & | multiply (float scalar) |
Affine2 & | multiply (const Affine2 &aff) |
Affine2 & | invert () |
Affine2 | getInverse () const |
Affine2 & | operator+= (const Vec2 &v) |
Affine2 & | operator-= (const Vec2 &v) |
Affine2 & | operator*= (float scalar) |
Affine2 & | operator*= (const Affine2 &aff) |
const Affine2 | operator+ (const Vec2 &v) const |
const Affine2 | operator- (const Vec2 &v) const |
const Affine2 | operator* (float scalar) const |
const Affine2 | operator* (const Affine2 &aff) const |
bool | isExactly (const Affine2 &aff) const |
bool | equals (const Affine2 &mat, float variance=CU_MATH_EPSILON) const |
bool | operator== (const Affine2 &aff) const |
bool | operator!= (const Affine2 &aff) const |
bool | isIdentity (float variance=0.0f) const |
bool | isInvertible (float variance=CU_MATH_EPSILON) const |
float | getDeterminant () const |
Vec2 | getScale () const |
float | getRotation () const |
Vec2 | getTranslation () const |
Vec2 | transform (const Vec2 &point) const |
Rect | transform (const Rect &rect) const |
Affine2 & | rotate (float angle) |
Affine2 & | scale (float value) |
Affine2 & | scale (const Vec2 &s) |
Affine2 & | scale (float sx, float sy) |
Affine2 & | translate (const Vec2 &t) |
Affine2 & | translate (float tx, float ty) |
std::string | toString (bool verbose=false) const |
operator std::string () const | |
operator Mat4 () const | |
Affine2 (const Mat4 &mat) | |
Affine2 & | operator= (const Mat4 &mat) |
Affine2 & | set (const Mat4 &mat) |
Static Public Member Functions | |
static Affine2 | createScale (float scale) |
static Affine2 * | createScale (float scale, Affine2 *dst) |
static Affine2 | createScale (float sx, float sy) |
static Affine2 * | createScale (float sx, float sy, Affine2 *dst) |
static Affine2 | createScale (const Vec2 &scale) |
static Affine2 * | createScale (const Vec2 &scale, Affine2 *dst) |
static Affine2 | createRotation (float angle) |
static Affine2 * | createRotation (float angle, Affine2 *dst) |
static Affine2 | createTranslation (const Vec2 &trans) |
static Affine2 * | createTranslation (const Vec2 &trans, Affine2 *dst) |
static Affine2 | createTranslation (float tx, float ty) |
static Affine2 * | createTranslation (float tx, float ty, Affine2 *dst) |
static Affine2 * | add (const Affine2 &m, const Vec2 &v, Affine2 *dst) |
static Affine2 * | subtract (const Affine2 &m1, const Vec2 &v, Affine2 *dst) |
static Affine2 * | multiply (const Affine2 &mat, float scalar, Affine2 *dst) |
static Affine2 * | multiply (const Affine2 &m1, const Affine2 &m2, Affine2 *dst) |
static Affine2 * | invert (const Affine2 &m1, Affine2 *dst) |
static Vec2 * | transform (const Affine2 &aff, const Vec2 &point, Vec2 *dst) |
static Rect * | transform (const Affine2 &aff, const Rect &rect, Rect *dst) |
static Affine2 * | rotate (const Affine2 &aff, float angle, Affine2 *dst) |
static Affine2 * | scale (const Affine2 &aff, float value, Affine2 *dst) |
static Affine2 * | scale (const Affine2 &aff, const Vec2 &s, Affine2 *dst) |
static Affine2 * | scale (const Affine2 &aff, float sx, float sy, Affine2 *dst) |
static Affine2 * | translate (const Affine2 &aff, const Vec2 &t, Affine2 *dst) |
static Affine2 * | translate (const Affine2 &aff, float tx, float ty, Affine2 *dst) |
static bool | decompose (const Affine2 &mat, Vec2 *scale, float *rot, Vec2 *trans) |
Public Attributes | |
float | m [4] |
Vec2 | offset |
Static Public Attributes | |
static const Affine2 | ZERO |
static const Affine2 | ONE |
static const Affine2 | IDENTITY |
This class defines an affine transform on 2D space.
In the case where you are only manipulating 2D points, this class may be faster than Mat4, even with the vectorization support. For an affine transform in 3d space, use Mat4.
The affine transform is stored as a 4-element array (containing the core matrix, plus a Vec2 offset. Keeping with the convention of Mat4, the core matrix is stored in column-order format. In addition, we assume that all operations are multiplied on the right.
cugl::Affine2::Affine2 | ( | ) |
Creates the identity transform.
1 0 0 1 + (0,0)
cugl::Affine2::Affine2 | ( | float | m11, |
float | m12, | ||
float | m21, | ||
float | m22, | ||
float | tx, | ||
float | ty | ||
) |
Constructs a matrix initialized to the specified values.
m11 | The first element of the first row. |
m12 | The second element of the first row. |
m21 | The first element of the second row. |
m22 | The second element of the second row. |
tx | The translation offset for the x-coordinate. |
ty | The translation offset for the y-coordinate. |
cugl::Affine2::Affine2 | ( | const float * | mat | ) |
Creates a matrix initialized to the specified column-major array.
The passed-in array is in column-major order, with the last two elements being the translation offset. Hence the memory layout of the array is as follows:
0 2 1 3 + (4,5)
mat | An array containing 6 elements in column-major order. |
cugl::Affine2::Affine2 | ( | const Affine2 & | copy | ) |
Constructs a new transform that is the copy of the specified one.
copy | The transform to copy. |
|
inline |
Destroys this transform, releasing all resources.
|
explicit |
Creates an affine transform from the given matrix.
The z values are all uniformly ignored. However, it the final element of the matrix is not 1 (e.g. the translation has a w value of 1), then it divides the entire matrix before creating the affine transform
mat | The matrix to convert |
Adds the specified offset to the given and stores the result in dst.
Addition is applied to the offset only; the core matrix is unchanged.
m | The initial transform. |
v | The offset to add. |
dst | A transform to store the result in. |
Adds the specified offset to this transform.
Addition is applied to the offset only; the core matrix is unchanged.
v | The offset to add. |
|
inlinestatic |
Returns a rotation transform for the given angle.
The angle measurement is in radians. The rotation is counter clockwise about the z-axis.
angle | The angle (in radians). |
Creates a rotation transform for the given angle, putting it in dst.
The angle measurement is in radians. The rotation is counter clockwise about the z-axis.
angle | The angle (in radians). |
dst | A transform to store the result in. |
|
inlinestatic |
Returns a uniform scale transform.
scale | The amount to scale. |
Creates a uniform scale transform, putting it in dst.
scale | The amount to scale. |
dst | A transform to store the result in. |
|
inlinestatic |
Returns a nonuniform scale transform.
sx | The amount to scale along the x-axis. |
sy | The amount to scale along the y-axis. |
Creates a nonuniform scale transform, putting it in dst.
sx | The amount to scale along the x-axis. |
sy | The amount to scale along the y-axis. |
dst | A transform to store the result in. |
Returns a nonuniform scale transform from the given vector.
scale | The nonuniform scale value. |
Creates a nonuniform scale transform from the given vector, putting it in dst.
scale | The nonuniform scale value. |
dst | A transform to store the result in. |
Returns a translation transform from the given offset
trans | The translation offset. |
Creates a translation transform from the given offset, putting it in dst.
trans | The translation offset. |
dst | A transform to store the result in. |
|
inlinestatic |
Returns a translation transform from the given parameters.
tx | The translation on the x-axis. |
ty | The translation on the y-axis. |
Creates a translation transform from the given parameters, putting it in dst.
tx | The translation on the x-axis. |
ty | The translation on the y-axis. |
dst | A transform to store the result in. |
|
static |
Decomposes the scale, rotation and translation components of the given matrix.
To work properly, the matrix must have been constructed in the following order: scale, then rotate, then translation. While the rotation matrix will always be correct, the scale and translation are not guaranteed to be correct.
If any pointer is null, the method simply does not assign that result. However, it will still continue to compute the component with non-null vectors to store the result.
If the scale component is too small, then it may be impossible to extract the rotation. In that case, if the rotation pointer is not null, this method will return false.
mat | The transform to decompose. |
scale | The scale component. |
rot | The rotation component. |
trans | The translation component. |
bool cugl::Affine2::equals | ( | const Affine2 & | mat, |
float | variance = CU_MATH_EPSILON |
||
) | const |
Returns true if the transforms are within tolerance of each other.
The tolerance is applied to each element of the transform individually.
mat | The transform to compare against. |
variance | The comparison tolerance. |
|
inline |
Returns the determinant of this transform.
The determinant is a feature of the core matrix. The offset is ignored.
|
inline |
Returns a copy of the inverse of this transform.
If the transform cannot be inverted, this method returns the zero transform.
Note: This does not modify the transform.
|
inline |
Returns the rotational angle of this transform.
If the scale component is too close to zero, we cannot extract the rotation. In that case, we return the zero angle. (
|
inline |
Returns the scale component of this transform.
If the scale component of this matrix has negative parts, it is not possible to always extract the exact scalar component. In that case, a scale vector that is mathematically equivalent to the original scale vector is extracted and returned.
To work properly, the transform must have been constructed in the following order: scale, then rotate, then translation. In any other order, the scale is not guaranteed to be correct.
|
inline |
Returns the translational component of this transform.
To work properly, the transform must have been constructed in the following order: scale, then rotate, then translation. In any other order, the translation is not guaranteed to be correct.
Inverts m1 and stores the result in dst.
If the transform cannot be inverted, this method stores the zero transform in dst.
m1 | The transform to negate. |
dst | A transform to store the result in. |
|
inline |
Inverts this transform in place.
If the transform cannot be inverted, this method sets it to the zero transform.
bool cugl::Affine2::isExactly | ( | const Affine2 & | aff | ) | const |
Returns true if the transforms are exactly equal to each other.
This method may be unreliable given that the elements are floats. It should only be used to compared transform that have not undergone a lot of manipulation.
aff | The transform to compare against. |
bool cugl::Affine2::isIdentity | ( | float | variance = 0.0f | ) | const |
Returns true if this transform is equal to the identity transform.
The optional comparison tolerance takes into accout that elements are floats and this may not be exact. The tolerance is applied to each element individually. By default, the match must be exact.
variance | The comparison tolerance |
|
inline |
Returns true if this transform is invertible.
The optional comparison tolerance takes into accout that elements are floats and this may not be exact. The tolerance is applied to the determinant of the core matrix.
Multiplies the specified transform by a scalar and stores the result in dst.
The scalar is applied to BOTH the core matrix and the offset.
mat | The transform. |
scalar | The scalar value. |
dst | A transform to store the result in. |
|
static |
Multiplies m1 by the transform m2 and stores the result in dst.
Transform multiplication is defined as standard function composition. The transform m2 is on the right. This means that it corresponds to an subsequent transform; transforms are applied left-to-right.
m1 | The first transform to multiply. |
m2 | The second transform to multiply. |
dst | A matrix to store the result in. |
|
inline |
Multiplies the components of this transform by the specified scalar.
The scalar is applied to BOTH the core matrix and the offset.
scalar | The scalar value. |
Multiplies this matrix by the specified one.
Transform multiplication is defined as standard function composition. The transform m2 is on the right. This means that it corresponds to an subsequent transform; transforms are applied left-to-right.
aff | The transform to multiply. |
|
inline |
Cast from Vec4 to a string.
|
inline |
Returns true if this transform is not equal to the given transform.
Comparison is exact, which may be unreliable given that the elements are floats.
aff | The transform to compare against. |
|
inline |
Returns a copy of this matrix with all elements multiplied by the scalar.
The scalar is applied to BOTH the core matrix and the offset.
Note: This does not modify the matrix.
scalar | The scalar value. |
Returns the matrix product of this matrix with the given matrix.
Transform multiplication is defined as standard function composition. The transform aff is on the right. This means that it corresponds to an subsequent transform; transforms are applied left-to-right.
Note: This does not modify the matrix.
aff | The transform to multiply by. |
|
inline |
Multiplies the components of this transform by the specified scalar.
The scalar is applied to BOTH the core matrix and the offset.
scalar | The scalar value. |
Right-multiplies this transform by the given transform.
Transform multiplication is defined as standard function composition. The transform aff is on the right. This means that it corresponds to an subsequent transform; transforms are applied left-to-right.
aff | The transform to multiply by. |
Returns the sum of this transform with the given offset.
Addition is applied to the offset only; the core matrix is unchanged.
Note: This does not modify the transform.
v | The offset to add. |
Adds the given offset to this transform in place.
Addition is applied to the offset only; the core matrix is unchanged.
v | The offset to add |
Returns the difference of this matrix with the given offset.
Subtraction is applied to the offset only; the core matrix is unchanged.
Note: This does not modify the transform.
v | The offset to subtract. |
Subtracts the given offset from this transform in place.
Subtraction is applied to the offset only; the core matrix is unchanged.
v | The offset to subtract |
Sets the elements of this transform to those in the specified transform.
mat | The transform to copy. |
|
inline |
Sets the values of this transform to those in the specified column-major array.
The passed-in array is in column-major order, with the last two elements being the translation offset. Hence the memory layout of the array is as follows:
0 2 1 3 + (4,5)
array | An array containing 6 elements in column-major order. |
Sets the elements of this transform to those of the given matrix.
The z values are all uniformly ignored. However, it the final element of the matrix is not 1 (e.g. the translation has a w value of 1), then it divides the entire matrix before creating the affine transform
mat | The matrix to convert |
|
inline |
Returns true if this transform is equal to the given transform.
Comparison is exact, which may be unreliable given that the elements are floats. It should only be used to compared transform that have not undergone a lot of manipulation.
aff | The transform to compare against. |
|
inlinestatic |
Applies a rotation to the given transform and stores the result in dst.
The rotation is in radians, counter-clockwise about the z-axis.
The rotation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
aff | The transform to rotate. |
angle | The angle (in radians). |
dst | A transform to store the result in. |
|
inline |
Applies a rotation to this transform.
The rotation is in radians, counter-clockwise about the given axis.
The rotation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
angle | The angle (in radians). |
|
inlinestatic |
Applies a uniform scale to the given transform and stores the result in dst.
The scaling operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
aff | The transform to scale. |
value | The scalar to multiply by. |
dst | A transform to store the result in. |
|
inlinestatic |
Applies a non-uniform scale to the given transform and stores the result in dst.
The scaling operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
aff | The transform to scale. |
s | The vector storing the individual scaling factors |
dst | A transform to store the result in. |
|
inlinestatic |
Applies a non-uniform scale to the given transform and stores the result in dst.
The scaling operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
aff | The transform to scale. |
sx | The amount to scale along the x-axis. |
sy | The amount to scale along the y-axis. |
dst | A transform to store the result in. |
|
inline |
Applies a uniform scale to this transform.
The scaling operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
value | The scalar to multiply by. |
Applies a non-uniform scale to this transform.
The scaling operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
s | The vector storing the individual scaling factors |
|
inline |
Applies a non-uniform scale to this transform.
The scaling operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
sx | The amount to scale along the x-axis. |
sy | The amount to scale along the y-axis. |
Affine2& cugl::Affine2::set | ( | float | m11, |
float | m12, | ||
float | m21, | ||
float | m22, | ||
float | tx, | ||
float | ty | ||
) |
Sets the individal values of this transform.
m11 | The first element of the first row. |
m12 | The second element of the first row. |
m21 | The first element of the second row. |
m22 | The second element of the second row. |
tx | The translation offset for the x-coordinate. |
ty | The translation offset for the y-coordinate. |
Affine2& cugl::Affine2::set | ( | const float * | mat | ) |
Sets the values of this transform to those in the specified column-major array.
The passed-in array is in column-major order, with the last two elements being the translation offset. Hence the memory layout of the array is as follows:
0 2 1 3 + (4,5)
mat | An array containing 6 elements in column-major order. |
Sets the elements of this transform to those in the specified transform.
mat | The transform to copy. |
Sets the elements of this transform to those of the given matrix.
The z values are all uniformly ignored. However, it the final element of the matrix is not 1 (e.g. the translation has a w value of 1), then it divides the entire matrix before creating the affine transform
mat | The matrix to convert |
Affine2& cugl::Affine2::setIdentity | ( | ) |
Sets this transform to the identity transform.
Affine2& cugl::Affine2::setZero | ( | ) |
Sets all elements of the current transform to zero.
|
static |
Subtracts the offset v from m and stores the result in dst.
Subtraction is applied to the offset only; the core matrix is unchanged.
m1 | The initial transform. |
v | The offset to subtract. |
dst | A transform to store the result in. |
Subtracts the specified offset from the current transform.
Subtraction is applied to the offset only; the core matrix is unchanged.
v | The offset to subtract. |
std::string cugl::Affine2::toString | ( | bool | verbose = false | ) | const |
Returns a string representation of this transform for debugging purposes.
If verbose is true, the string will include class information. This allows us to unambiguously identify the class.
verbose | Whether to include class information |
|
static |
Transforms the point and stores the result in dst.
aff | The affine transform. |
point | The point to transform. |
dst | A vector to store the transformed point in. |
Transforms the rectangle and stores the result in dst.
This method transforms the four defining points of the rectangle. It then computes the minimal bounding box storing these four points
aff | The affine transform. |
rect | The rect to transform. |
dst | A rect to store the transformed rectangle in. |
Returns a copy of the given point transformed.
Note: This does not modify the original point. To transform a point in place, use the static method (or the appropriate operator).
point | The point to transform. |
Returns a copy of the given rectangle transformed.
This method transforms the four defining points of the rectangle. It then computes the minimal bounding box storing these four points
Note: This does not modify the original rectangle. To transform a point in place, use the static method.
rect | The rect to transform. |
|
inlinestatic |
Applies a translation to the given transform and stores the result in dst.
The translation operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
aff | The transform to translate. |
t | The vector storing the individual translation offsets |
dst | A transform to store the result in. |
|
inlinestatic |
Applies a translation to the given transform and stores the result in dst.
The translation operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
aff | The transform to translate. |
tx | The translation offset for the x-axis. |
ty | The translation offset for the y-axis. |
dst | A transform to store the result in. |
Applies a translation to this transform.
The translation operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
t | The vector storing the individual translation offsets |
|
inline |
Applies a translation to this transform.
The translation operation is applied on the right. Given our convention, that means that it takes place AFTER any previously applied transforms.
tx | The translation offset for the x-axis. |
ty | The translation offset for the y-axis. |
|
static |
The identity transform (ones on the diagonal)
float cugl::Affine2::m[4] |
The core matrix
Vec2 cugl::Affine2::offset |
The translation offset
|
static |
The transform with all ones
|
static |
The transform with all zeroes