Workspace 6.21.5
|
#include <Mesh/Geometry/transformmatrix.h>
Classes | |
struct | FailedComponentExtraction |
Exception class for when an attempt to extract a component of a transformation fails. More... | |
struct | TransformHasNoInverse |
Exception class for when an attempt is made to find the inverse of a singular transform matrix. More... | |
Static Public Member Functions | |
static TransformMatrix | calcOrthographicProjectionMatrix (double left, double right, double bottom, double top, double zNear, double zFar) |
static TransformMatrix | calcPerspectiveProjectionMatrix (double fieldOfViewY, double aspectRatio, double zNear, double zFar) |
static TransformMatrix | calcPerspectiveProjectionMatrix1 (double fieldOfViewX, double fieldOfViewY, double zNear, double zFar) |
static TransformMatrix | calcPerspectiveProjectionMatrix2 (double left, double right, double bottom, double top, double near, double far) |
static TransformMatrix | calcPerspectiveProjectionMatrix3 (double focalLengthX, double focalLengthY, double width, double height, double near, double far, double principlePointX, double principlePointY, double axisSkew) |
static TransformMatrix | calcRotationMatrix (const Vector3d &eulerAngles) |
static TransformMatrix | calcRotationMatrix (const Vector3d &right, const Vector3d &up, const Vector3d &forward) |
static TransformMatrix | calcRotationMatrix (double angle, const Vector3d &axis) |
static TransformMatrix | calcScaleMatrix (const Vector3d &s) |
static TransformMatrix | calcScaleMatrix (double d) |
static TransformMatrix | calcShearMatrix (const Vector3d &s) |
static TransformMatrix | calcTranslationMatrix (const Vector3d &t) |
static TransformMatrix | calcViewMatrix (const Mesh::Vector3d &right, const Mesh::Vector3d &up, const Mesh::Vector3d &view, const Mesh::Vector3d &translate) |
static Vector3d | canoniseEulerAngles (const Vector3d &euler) |
static Vector3d | rotationVectorToEulerAngles (const Vector3d &rotVec) |
Related Functions | |
(Note that these are not member functions.) | |
QVector3D | operator* (const TransformMatrix &M, const QVector3D &v) |
QVector4D | operator* (const TransformMatrix &M, const QVector4D &v) |
Vector3d | operator* (const TransformMatrix &M, const Vector3d &v) |
TransformMatrix | operator* (const TransformMatrix &M, double d) |
TransformMatrix | operator* (const TransformMatrix &Mleft, const TransformMatrix &Mright) |
TransformMatrix | operator* (double d, const TransformMatrix &M) |
TransformMatrix | operator+ (const TransformMatrix &m1, const TransformMatrix &m2) |
TransformMatrix | operator- (const TransformMatrix &m1, const TransformMatrix &m2) |
std::ostream & | operator<< (std::ostream &output, const TransformMatrix &m) |
std::istream & | operator>> (std::istream &input, TransformMatrix &m) |
We use the same row-column ordering as OpenGL for efficiency reasons. This allows a common performance-sensitive use of a transform matrix to be efficient.
The transformation matrix is a 4x4 matrix stored in column-major order. ie
To apply the transform to a 3D vector, you need to pre-multiply the w-augmented vector by the transformation matrix:
where w is usually 1.0 and generally only deviates from this when performing advanced techniques such as perspective division, etc. If you want to use the matrix to post-multiply a vector, post-multiply the vector by the transpose of the matrix. ie
To access a particular element of the matrix, use the object as though it were callable as a function with the two parameters specifying the row and column to retrieve. For example:
When breaking the transform matrix up into separate components, these components are applied in the following order:
This is important, since if you extract transformation components and want to reassemble the transform matrix, you must ensure that you apply the components in the correct order if you expect to end up back with the same matrix from which the components were derived.
Extracting matrix components: shear, scale, rotation and translation
Given a well-formed (i.e. invertible) TransformMatrix M, it is always possible to uniquely extract the shear and translation components of the matrix. However there is no unique scale and rotation factors, one reason for which is the following:
Therefore three negative scaling factors is the same as one negative scale factor and a rotation of about the same axis carrying the negative scale. Also, one positive and two negative scaling factors is the same as three positive factors and a rotation about the axis carrying the original positive scale.
Moreover, given one negative and two positive scaling factors, there exists a rotation of about a well defined axis that allows any of the three axes to be associated with a negative scaling factor.
Given these considerations, in an attempt to make the result of extracting scale and rotation from a matrix predictable we make the following convention:
This uniquely specifies the scaling vector.
For the extracted rotation vector, it will always have an angle in the interval [0, ]. If the angle is in the interval (0, ), then the rotation vector is uniquely specified.
Above all, we guarantee that when reconstructing the matrix from its extracted components, in the order shear, scale, rotation and finally translation, that the original and reconstructed matrix agree up to numerical precision.
|
inline |
Constructs an identity matrix (ie no transformation).
|
inline |
m | The matrix to copy. |
|
inline |
flatArray | The array of data to copy into the matrix. |
TransformMatrix | ( | const TransformGroup & | t | ) |
t | The transform to use for this transformation matrix. |
TransformMatrix | ( | const TransformByComponents & | t | ) |
t | The transform components to use for this transformation matrix. |
double calcDeterminant | ( | ) | const |
TransformMatrix calcInverse | ( | ) | const |
The algorithm currently employed is not particularly sophisticated. If performance is particularly important, consider using an LU factorisation instead.
TransformHasNoInverse | Thrown if we are trying to invert a singular transform matrix. |
double calcMaxAbsoluteValue | ( | ) | const |
This function is mostly used as an error checking mechanism when used on a matrix that is the result of the difference of two TransformMatrix instances which should be the same. This is usually just a test or debugging aid.
|
static |
left | Coord for the left vertical clipping plane |
right | Coord for the right vertical clipping plane |
bottom | Coord for the bottom horizontal clipping plane |
top | Coord for the top horizontal clipping plane |
zNear | Coord for the near depth clipping plane |
zFar | Coord for the far depth clipping plane |
|
static |
fieldOfViewY | The field of view to apply in the perspective projection |
aspectRatio | The width to height ratio of the projection |
zNear | The near clipping plane |
zFar | The far clipping plane |
Calculate and return a perspective projection matrix for a right-handed coordinate system.
|
static |
fieldOfViewX | The horizontal field of view to apply in the perspective projection |
fieldOfViewY | The vertical field of view to apply in the perspective projection |
zNear | The near clipping plane |
zFar | The far clipping plane |
Calculate and return a perspective projection matrix for a right-handed coordinate system.
|
static |
Calculate and return a perspective projection matrix for a right-handed coordinate system. Rather than taking a field of view, assume near is the focal length of the camera.
left | Left-most boundary of the frustum at the view plane. |
right | Right-most boundary of the frustum at the view plane. |
bottom | Bottom-most boundary of the frustum at the view plane. |
top | Top-most boundary of the frustum at the view plane. |
near | Near-clipping plane. |
far | Far-clipping plane. |
|
static |
Calculate and return a perspective projection matrix for a right-handed coordinate system using the camera's intrinsic properties (its focal length, axis skew and principal point).
To understand the model of the camera's intrinsic params, this link is fairly helpful: http://ksimek.github.io/2013/08/13/intrinsic/
focalLengthX | The horizontal focal length of the camera (mm) |
focalLengthY | The vertical focal length of the camera (mm) |
width | Width of the projected image |
height | Height of the projected image |
near | The near clipping plane |
far | The far clipping plane |
principlePointX | X-coordinate of the principle point (i.e. point on the image plane that the principle axis intersects) |
principlePointY | Y-coordinate of the principle point (i.e. point on the image plane that the principle axis intersects) |
axisSkew | Any axis skew that needs to be taken into account |
|
static |
eulerAngles | The euler angles defining the rotation. |
The implementation of this function follows more or less directly Appendix G of the "OpenGL Programming Guide", otherwise known as "The Red Book".
|
static |
Construct a rotation matrix from three basis vectors.
forward | The forward vector of the basic matrix we are trying to create |
up | The up vector of the matrix |
right | The right vector of the matrix |
|
static |
angle | The angle (in radians) to rotate about axis. |
axis | The vector defining the axis about which to rotate. It need not be a unit vector, since a copy of it is converted to a unit vector by this function. |
The implementation of this function follows more or less directly Appendix G of the "OpenGL Programming Guide", otherwise known as "The Red Book".
|
static |
s | The scale factors to convert into a transform matrix. |
The implementation of this function follows more or less directly Appendix G of the "OpenGL Programming Guide", otherwise known as "The Red Book".
|
static |
d | The uniform scale factor to convert into a transform matrix. This is equivalent to setting the scale factor to d for all three scale components. |
The implementation of this function follows more or less directly Appendix G of the "OpenGL Programming Guide", otherwise known as "The Red Book".
|
static |
s | The shear factors to convert into a transform matrix, where s.x, s,y and s,z is respectively the x/y, x/z, y/z shear ratios |
Calculate the shear transform matrix corresponding to s.
|
static |
t | The translation to convert into a transform matrix. |
The implementation of this function follows more or less directly Appendix G of the "OpenGL Programming Guide", otherwise known as "The Red Book".
|
static |
euler | The Euler angles to canonise as a Vector3d. Each member should hold the rotation about it's axis, so euler is equivalent to (bank, attitude, heading). |
bool getComponents | ( | Vector3d & | translation, |
Vector3d & | rotation, | ||
Vector3d & | scale, | ||
Vector3d & | shear | ||
) | const |
translation | On successful return, this will contain the translation component of the matrix. |
rotation | On successful return, this will contain the rotation component as a vector in the direction of the rotation axis. The magnitude of this vector is equal to the angle of rotation (in radians). Rotation in the rotation vector form returned by this method may be converted to Euler angles by rotationVectorToEulerAngles(). |
scale | On successful return, this will contain the (x,y,z) scale factors. |
shear | On successful return, this will contain the shear factors. |
See the class description for details on the correct ordering when applying transformation components separately. This function exists so that all four components can be extracted at once. This can be more efficient than calling each of the component extraction functions separately, since certain parts of the extraction calculations can be re-used for more than one component.
The function assumes that the transformation matrix will be applied to a vector whose w component is 1.0 (a vector can always be scaled to meet this requirement).
See TransformMatrix overview documentation for the convention used for rotation and negative scaling.
|
inline |
Vector3d getRotationComponent | ( | ) | const |
Rotation should be applied before an object is translated, but after scaling and shear.
The function assumes that the transformation matrix will be applied to a vector whose w component is 1.0 (a vector can always be scaled to meet this requirement).
Rotation in the rotation vector form returned by this method may be converted to Euler angles by
See TransformMatrix overview documentation for the convention used for rotation and negative scaling.
FailedComponentExtraction | This exception is thrown if the function was unable to extract the requested component. For a non-throwing way to extract components, use getComponents() instead. |
Vector3d getScaleComponent | ( | ) | const |
Scale should be applied before rotation and translation, but after shear.
The function assumes that the transformation matrix will be applied to a vector whose w component is 1.0 (a vector can always be scaled to meet this requirement).
See TransformMatrix overview documentation for the convention used for rotation and negative scaling.
FailedComponentExtraction | This exception is thrown if the function was unable to extract the requested component. For a non-throwing way to extract components, use getComponents() instead. |
Vector3d getShearComponent | ( | ) | const |
Shear should be the first component in the transform to be applied. It must occur before scale, rotation and translation.
The function assumes that the transformation matrix will be applied to a vector whose w component is 1.0 (a vector can always be scaled to meet this requirement).
The x value of the return is x shear with respect to y (x/y shear) The y value of the return is the x/z shear. The z value of the return is the y/z shear.
FailedComponentExtraction | This exception is thrown if the function was unable to extract the requested component. For a non-throwing way to extract components, use getComponents() instead. |
Vector3d getTranslationComponent | ( | ) | const |
Translation is the last component of the transform to be applied. Therefore, if applying a transform component-by-component, ensure that shear, scale and rotation are applied first (in that order).
The function assumes that the transformation matrix will be applied to a vector whose w component is 1.0 (a vector can always be scaled to meet this requirement).
|
inline |
m | Matrix to compare to this object. |
|
inline |
Access individual matrix elements
|
inline |
|
inline |
Post-multiply the current matrix by M.
|
inline |
Multiply the current matrix by the scalar d.
TransformMatrix & operator= | ( | const TransformGroup & | g | ) |
g | The transform group to assign to this object. |
|
inline |
m | The matrix to assign to this object. |
|
inline |
m | Matrix to compare to this object. |
TransformMatrix & orthonormalizeRotationComponent | ( | ) |
Orthonormalizes the rotation component of the matrix using the Gram-Schmidt orthonormalization process.
TransformMatrix & rotate | ( | double | angle, |
const Vector3d & | axis | ||
) |
angle | The angle in radians to rotate about the specified axis. |
axis | The rotation axis specified as a unit vector. No checking is performed to test that this is the case - that is the caller's responsibility. |
Calling this function is equivalent to pre-multiplying the existing matrix by the result of calcRotationMatrix(). Note that we pre-multiply so that this rotation occurs after the current transformation.
TransformMatrix & rotateEuler | ( | const Vector3d & | eulerAngles | ) |
eulerAngles | The euler angles defining the rotation. |
Calling this function is equivalent to pre-multiplying the existing matrix by the result of calcRotationMatrix(). Note that we pre-multiply so that this rotation occurs after the current transformation.
rotVec | A rotation vector with magnitude equal to the rotation angle (in radians) in the direction of the rotation axis. |
This method returns canonical Euler angles. Canonical Euler angles unlike normal Euler angles have the property that no two canonical Euler angles result in the same net rotation.
TransformMatrix & scale | ( | const Vector3d & | v | ) |
v | A vector containing the three scaling factors to apply to the transformation matrix. In most cases, all three components will hold the same value, in which case the other overload of this function might be easier to use. |
Calling this function is equivalent to pre-multiplying the existing matrix by the result of calcScaleMatrix(). Note that we pre-multiply so that this scaling occurs after the current transformation.
TransformMatrix & scale | ( | double | d | ) |
d | A scalar to be used for all three components of the scale factor to apply to the transformation matrix. |
Calling this function is equivalent to pre-multiplying the existing matrix by the result of calcScaleMatrix(). Note that we pre-multiply so that this scaling occurs after the current transformation.
|
inline |
Discard the current matrix and replace it by the identity matrix (ie no transform).
TransformMatrix & shear | ( | const Vector3d & | s | ) |
TransformMatrix & translate | ( | const Vector3d & | v | ) |
v | The 3D translation vector to apply to this matrix. |
Calling this function is equivalent to pre-multiplying the existing matrix by the result of calcTranslationMatrix(). Note that we pre-multiply so that this translation occurs after the current transformation.
TransformMatrix & transpose | ( | ) |
Transposes the matrix.
|
related |
M | The transformation matrix to apply. |
v | The 3D vector to be transformed. It is assumed that the 4th component of the vector (usually labelled as w) is unity for the purposes of the multiplication. |
|
related |
M | The transform matrix to apply. |
v | The 4D vector to be transformed. Unlike the Vector3D overload, the 4th component of the vector (w) will be multiplied out based on the assumption that it will not evaluate as unity. |
|
related |
M | The transformation matrix to apply. |
v | The 3D vector to be transformed. It is assumed that the 4th component of the vector (usually labelled as w) is unity for the purposes of the multiplication. |
|
related |
M | A transformation matrix. |
d | A scalar value to multiply the matrix by. |
|
related |
Mleft | Transformation matrix on left hand side of multiplication. |
Mright | Transformation matrix on right hand side of multiplication. |
|
related |
d | A scalar value to multiply the matrix by. |
M | A transformation matrix. |
|
related |
m1 | A transformation matrix. |
m2 | A transformation matrix. |
This operator has no meaning for a transformation matrix. It is mostly useful for debugging when comparing two different matrices.
|
related |
m1 | A transformation matrix. |
m2 | A transformation matrix. |
This operator has no meaning for a transformation matrix. It is mostly useful for debugging when comparing two different matrices.
|
related |
Provides output stream functionality for TransformMatrix objects.
|
related |
Provides input stream functionality for TransformMatrix objects.