Class Matrix3

3DS Max Plug-In SDK

Class Matrix3

See Also: Class Point3, Matrix Representations of 3D Transformations, Class Quat, Class AngAxis, Structure AffineParts, Class BigMatrix.

class Matrix3

Description:

This class implements a 4x3 3D transformation matrix object. Methods are provided to zero the matrix, set it to the identity, compute its inverse, apply incremental translation, rotation and scaling, and build new X, Y and Z rotation matrices. Operators are provided for matrix addition, subtraction, and multiplication. All methods are implemented by the system.

Note: In 3ds max, all vectors are assumed to be row vectors. Under this assumption, multiplication of a vector with a matrix can be written either way (Matrix*Vector or Vector*Matrix), for ease of use, and the result is the same -- the (row) vector transformed by the matrix.

Data Members:

private:

float m[4][3];

Matrix storage.

DWORD flags;

Matrix Identity Flags.

POS_IDENT

Indicates the translation row of the matrix is the identity.

ROT_IDENT

Indicates the rotation elements of the matrix are the identity.

SCL_IDENT

Indicates the scale elements of the matrix are the identity.

MAT_IDENT

Indicates the matrix is the identity matrix. This is equivalent to (POS_IDENT|ROT_IDENT|SCL_IDENT).

public:

static const Matrix3 Identity;

This data member is available in release 3.0 and later only.

An instance of an identity matrix.

Methods:

Prototype:

Matrix3()

Remarks:

Constructor. Note that no initialization is done. Use Zero() or Identity(), or the constructors below.

Prototype:

Matrix3(BOOL init)

Remarks:

Constructor. If TRUE is passed to the method the matrix is set to the identity.

Parameters:

BOOL init

Specifies if the Matrix3 should be initialized to the identity.

Prototype:

Matrix3(float (*fp)[3]);

Remarks:

Constructor. The matrix is initialized to fp.

Parameters:

float (*fp)[3]

Specifies the initial values for the matrix.

Prototype:

Matrix3(const Point3& U, const Point3& V, const Point3& N, const Point3& T);

Remarks:

This method is available in release 3.0 and later only.

Constructor. Initializes the matrix with the row data passed and validates the matrix flags.

Parameters:

const Point3& U

The data for row 0.

const Point3& V

The data for row 1.

const Point3& N

The data for row 2.

const Point3& T

The data for row 3.

Prototype:

void SetNotIdent()

Remarks:

This clears the MAT_IDENT flag to indicate the matrix is not the identity. If any changes are made to components directly via GetAddr(), this method must be called.

Prototype:

Remarks:

void SetIdentFlags(DWORD f)

Remarks:

This sets the specified identity flag(s).

Parameters:

DWORD f

Specifies the identity flag bit(s) to set. See Matrix Identity Flags above.

Prototype:

DWORD GetIdentFlags() const

Remarks:

Returns the identity flags.

Prototype:

void ClearIdentFlag(DWORD f)

Remarks:

Clears the specified identity flag(s). See Matrix Identity Flags above.

Parameters:

DWORD f

Specifies the identity flag bit(s) to clear.

Prototype:

BOOL IsIdentity() const

Remarks:

Returns TRUE if the matrix is the identity matrix (based on the flags); otherwise FALSE.

Prototype:

void ValidateFlags();

Remarks:

This method is available in release 2.0 and later only.

This method may be used to recompute the *_IDENT flags for this matrix. For instance, if you call a method, such as INode::GetObjTMAfterWSM(), and it returns a matrix, you cannot use the IsIdentity() method to check if the matrix is indeed the identity. This is because the flags that method checks are not initialized by the INode method. What you can do however is call this method first. This will validate the flags in the matrix so they accuratly reflect the properties of the matrix. If after calling this method, and then calling IsIdentity(), the proper result would be returned.

Prototype:

MRow* GetAddr()

Remarks:

Returns the address of this Matrix3.

The Matrix3 class keeps flags indicating identity for rotation, scale, position, and the matrix as a whole, and thus the direct access via the [] operator is restricted to prevent developers from modifying the matrix without updating the flags. This method, GetAddr(), still lets you get at the matrix itself and then you can use the [] operator on the result. Note: If you change the matrix via this pointer, you MUST clear the proper IDENT flags!

Also Note: typedef float MRow[3];

Return Value:

The address of the Matrix3.

Prototype:

const MRow* GetAddr() const;

Remarks:

This method is available in release 3.0 and later only.

Returns the address of this Matrix3.

The Matrix3 class keeps flags indicating identity for rotation, scale, position, and the matrix as a whole, and thus the direct access via the [] operator is restricted to prevent developers from modifying the matrix without updating the flags. This method, GetAddr(), still lets you get at the matrix itself and then you can use the [] operator on the result. Note: If you change the matrix via this pointer, you MUST clear the proper IDENT flags!

Also Note: typedef float MRow[3];

Prototype:

void IdentityMatrix();

Remarks:

Set this matrix to the Identity Matrix.

Prototype:

void Zero();

Remarks:

This method sets all elements of the matrix to 0.0f

Prototype:

Matrix3& Set(const Point3& U, const Point3& V, const Point3& N, const Point3& T);

Remarks:

This method is available in release 3.0 and later only.

Initializes the matrix with the row data passed and validates the matrix flags.

Parameters:

const Point3& U

The data for row 0.

const Point3& V

The data for row 1.

const Point3& N

The data for row 2.

const Point3& T

The data for row 3.

Return Value:

A reference to this matrix.

Prototype:

int Equals(const Matrix3& M, float epsilon = 1E-6f) const;

Remarks:

This method is available in release 3.0 and later only.

Compares the elements of this matrix and the one specified element by element for equality within the specified tolerance epsilon. Returns nonzero if they are 'equal'; otherwise zero.

Parameters:

const Matrix3& M

The matrix to compare against.

float epsilon = 1E-6f

The tolerance for comparison. If the values in the matrix are within this value (+ epsilon or - epsilon) they are considered equal.

Prototype:

Point3 GetRow(int i) const

Remarks:

Returns the specified row of this matrix.

Parameters:

int i

Specifies the row to retrieve.

Prototype:

void SetRow(int i, Point3 p);

Remarks:

Sets the specified row of this matrix to the specified values.

Parameters:

int i

Specifies the row to set.

Point3 p

The values to set.

Prototype:

Point4 GetColumn(int i) const;

Remarks:

Returns the 'i-th' column of this matrix.

Parameters:

int i

Specifies the column to get (0-2).

Prototype:

void SetColumn(int i, Point4 col);

Remarks:

Sets the 'i-th' column of this matrix to the specified values.

Parameters:

int i

Specifies the column to set (0-2).

Point4 col

The values to set.

Prototype:

Point3 GetColumn3(int i) const;

Remarks:

Returns the upper three entries in the specified column.

Parameters:

int i

Specifies the partial column to get (0-2).

Prototype:

void NoTrans();

Remarks:

This method zeros the translation portion of this matrix.

Prototype:

void NoRot();

Remarks:

This method zeros the rotation portion of this matrix.

Prototype:

void NoScale();

Remarks:

The method zeros the scale portion of this matrix without orthogonalization. If the matrix was sheared (skewed) then the method is not able to remove scale component completely. Use Orthogonalize() method first, and then NoScale() to remove scale component entirely. Read SCL_IDENT flag to check if NoScale() method was enough to make the matrix to be orthogonal (with perpendicular axes of unit length).Prototype:

 

 

void Orthogonalize();

Remarks:

This is an "unbiased" orthogonalization of this matrix. The algorithm seems to take a maximum of 4 iterations to converge. An orthogonal matrix has an axis system where each axis is 90 degrees from the others (it's not skewed).

Prototype:

void SetTrans(const Point3 p)

Remarks:

Sets the translation row of this matrix to the specified values. The POS_IDENT flag is cleared.

Parameters:

const Point3 p

Specifies the values for the translation row.

Prototype:

void SetTrans(int i, float v)

Remarks:

Sets the specified component of the translation row of this matrix to the specified value. The POS_IDENT flag is cleared.

Parameters:

int i

Specifies the component of the translation row of this matrix to set.

float v

The value to set.

Prototype:

Point3 GetTrans() const

Remarks:

Returns the translation row of this matrix.

Return Value:

The translation row of this matrix.

Prototype:

void SetScale(const Point3 p);

Remarks:

This method is not currently implemented.

Prototype:

void Translate(const Point3& p);

Remarks:

Apply an incremental translation transformation to this matrix. This is equivalent to multiplying on the RIGHT by the transform.

Parameters:

const Point3& p

Specifies the translation.

Prototype:

void RotateX(float angle);

Remarks:

Apply an incremental X rotation transformation to this matrix. This is equivalent to multiplying on the RIGHT by the transform.

Parameters:

float angle

Specifies the X rotation in radians.

Prototype:

void RotateY(float angle);

Remarks:

Apply an incremental Y rotation transformation to this matrix. This is equivalent to multiplying on the RIGHT by the transform.

Parameters:

float angle

Specifies the Y rotation in radians.

Prototype:

void RotateZ(float angle);

Remarks:

Apply an incremental Z rotation transformation to this matrix. This is equivalent to multiplying on the RIGHT by the transform.

Parameters:

float angle

Specifies the Z rotation in radians.

Prototype:

void Scale(const Point3& s, BOOL trans = FALSE);

Remarks:

Apply an incremental scaling transformation to this matrix. This is equivalent to multiplying on the RIGHT by the transform.

Parameters:

const Point3& s

The scale values.

BOOL trans = FALSE

If set to TRUE, the translation component is scaled. If trans = FALSE the translation component is unaffected. When 3ds max was originally written there was a bug in the code for this method where the translation portion of the matrix was not being scaled. This meant that when a matrix was scaled the bottom row was not scaled. Thus it would always scale about the local origin of the object, but it would scale the world axes. When this bug was discovered, dependencies existed in the code upon this bug. Thus it could not simply be fixed because it would break the existing code that depended upon it working the incorrect way. To correct this the trans parameter was added. If this is set to TRUE, the translation component will be scaled correctly. The existing plug-ins don't use this parameter, it defaults to FALSE, and the code behaves the old way.

Prototype:

void PreTranslate(const Point3& p);

Remarks:

Apply an incremental translation transformation to this matrix. This is equivalent to multiplying on the LEFT by the transform.

Parameters:

const Point3& p

Specifies the translation distance.

Prototype:

void PreRotateX(float angle);

Remarks:

Apply an incremental X rotation transformation to this matrix. This is equivalent to multiplying on the LEFT by the transform.

Parameters:

float angle

Specifies the X rotation in radians.

Prototype:

void PreRotateY(float angle);

Remarks:

Apply an incremental Y rotation transformation to this matrix. This is equivalent to multiplying on the LEFT by the transform.

Parameters:

float angle

Specifies the Y rotation in radians.

Prototype:

void PreRotateZ(float angle);

Remarks:

Apply an incremental Z rotation transformation to this matrix. This is equivalent to multiplying on the LEFT by the transform.

Parameters:

float angle

Specifies the Z rotation in radians.

Prototype:

void PreScale(const Point3& s, BOOL trans = FALSE);

Remarks:

Apply an incremental scaling transformation to this matrix. This is equivalent to multiplying on the LEFT by the transform.

Parameters:

const Point3& s

The scale values.

BOOL trans = FALSE

If trans = FALSE the translation component is unaffected.

Prototype:

void SetTranslate(const Point3& p);

Remarks:

This method is available in release 3.0 and later only.

Sets this matrix to the identity and the translation components to the specified values.

Parameters:

const Point3& p

The translation values to store.

Prototype:

void SetRotateX(float angle);

Remarks:

This method is available in release 3.0 and later only.

Sets this matrix to the identity and the rotation components to the specified X rotation.

Parameters:

float angle

The angle for X rotation (in radians).

Prototype:

void SetRotateY(float angle);

Remarks:

This method is available in release 3.0 and later only.

Sets this matrix to the identity and the rotation components to the specified Y rotation.

Parameters:

float angle

The angle for Y rotation (in radians).

Prototype:

void SetRotateZ(float angle);

Remarks:

This method is available in release 3.0 and later only.

Sets this matrix to the identity and the rotation components to the specified Z rotation.

Parameters:

float angle

The angle for Z rotation (in radians).

Prototype:

void SetRotate(const Quat& q);

Remarks:

This method is available in release 3.0 and later only.

Sets the rotation components of the matrix as specified by the quaternion. The translation and scale components will match the identity matrix.

Parameters:

const Quat& q

Specifies the rotation to use for the matrix.

Prototype:

void SetRotate(const AngAxis& aa);

Remarks:

This method is available in release 3.0 and later only.

Sets the rotation components of the matrix as specified by the AngAxis. The translation and scale components will match the identity matrix.

Parameters:

const AngAxis& aa

Specifies the rotation to use for the matrix.

Prototype:

void SetRotate(float yaw, float pitch, float roll);

Remarks:

This method is available in release 3.0 and later only.

Sets the rotation components of this matrix using yaw, pitch and roll angles. There are many different conventions for specifying a rotation by means of three Euler angles. This function uses the convention of rotating around the world Z axis, then the X axis, then the Y axis; the three arguments are given in the order Y, X, Z.

This one is equivalent to:

 M.IdentityMatrix();

 M.RotateZ(roll);

 M.RotateX(pitch);

 M.RotateY(yaw);

--Which presupposes Y is vertical, X is sideways, Z is forward

Parameters:

float yaw

The yaw angle in radians.

float pitch

The pitch angle in radians.

float roll

The roll angle in radians.

Prototype:

void SetAngleAxis();

Remarks:

This method is available in release 3.0 and later only.

Sets the rotation portion of the matrix to the rotation specified by the angle and axis and sets the translation portion to zeros.

Parameters:

const Point3& axis

The axis of rotation.

float angle

The angle of rotation about the axis in radians.

Prototype:

void SetScale(const Point3& s);

Remarks:

This method is available in release 3.0 and later only.

Sets the scale components of this matrix to the specified values. The other components to this matrix will match the identity.

Parameters:

const Point3& s

The scale factors for the matrix.

Prototype:

void SetFromToUp(const Point3& from, const Point3& to, const Point3& up);

Remarks:

This method is available in release 3.0 and later only.

This creates a matrix describing a viewpoint which is at the 'from' location, looking toward the 'to' location; the viewpoint is tilted so that the 'up' vector points to the top of the view.

Parameters:

const Point3& from

This specifies the viewpoint source location.

const Point3& to

This vector specifies the direction of view.

const Point3& up

This vector points to the top of the view.

Prototype:

void Invert();

Remarks:

This method is available in release 3.0 and later only.

This method inverts this matrix. An inverted matrix, when multiplied by the original, yields the identity.

Prototype:

BOOL Parity() const;

Remarks:

This method is available in release 2.0 and later only.

Returns the 'parity' of the matrix. If one axis of the matrix is scaled negatively this switches the 'parity'. However if you scale two axes it will flip it back. Three times switches it again.

When rendering a mesh, if you scale something along one axis, it turns 'inside out'. That is the direction when the normals are reversed. This method may be used to detect that case and then reverse the normals. The 3ds max renderer does this -- if this method returns TRUE it flips all the normals so it won't turn inside out.

Sample Code:

BOOL Matrix3::Parity() const {

 if (flags&SCL_IDENT) return FALSE;

 Point3 cp = CrossProd(GetRow(0),GetRow(1));

 if (DotProd(cp,GetRow(2)) < 0.0) return TRUE;

 return FALSE;

 }

Prototype:

Point3 PointTransform(const Point3& p) const;

Remarks:

This method is available in release 3.0 and later only.

Returns the specified point transformed by this matrix.

Parameters:

const Point3& p

The point to transform by this matrix.

Prototype:

Point3 VectorTransform(const Point3& p) const;

Remarks:

This method is available in release 3.0 and later only.

Returns the specified vector transformed by this matrix.

Parameters:

const Point3& p

The vector to transform by this matrix.

Prototype:

void TransformPoints(Point3 *array, int n, int stride = sizeof(Point3));

Remarks:

This method is available in release 3.0 and later only.

Transforms the specified list of points with this matrix.

Parameters:

Point3 *array

The array of points to transform with this matrix.

int n

The number of points in the array.

int stride = sizeof(Point3)

The size of the increment used when moving to the next point. If you wish to transform an array of data objects which contain x, y, and z coordinates in order (such as a Point4, or a structure containing a Point3 as a member) you can specify a 'stride' value (for instance sizeof(data_object)).

Prototype:

void TransformPoints(const Point3 *array, Point3 *to, int n, int stride = sizeof(Point3), int strideTo = sizeof(Point3));

Remarks:

This method is available in release 3.0 and later only.

Transforms the specified list of points with this matrix and stores the resulting transformed points in the storage passed.

Parameters:

const Point3 *array

The array of points to transform (the source).

Point3 *to

The array to store the transformed points (the destination).

int n

The number of points in the source array.

int stride = sizeof(Point3)

int strideTo = sizeof(Point3)

The size increment used when moving to the next storage location.

Prototype:

void TransformVectors(Point3 *array, int n, int stride = sizeof(Point3));

Remarks:

This method is available in release 3.0 and later only.

Transforms the specified list of vectors with this matrix.

Parameters:

Point3 *array

The array of vectors to transform with this matrix.

int n

The number of vectors in the array.

int stride = sizeof(Point3)

The size of the increment used when moving to the next vector. If you wish to transform an array of data objects which contain x, y, and z coordinates in order (such as a Point4, or a structure containing a Point3 as a member) you can specify a 'stride' value (for instance sizeof(data_object)).

Prototype:

void TransformVectors(const Point3 *array, Point3 *to, int n, int stride = sizeof(Point3), int strideTo = sizeof(Point3));

Remarks:

This method is available in release 3.0 and later only.

Transforms the specified list of vectors with this matrix and stores the resulting transformed vectors in the storage passed.

Parameters:

const Point3 *array

The array of vectors to transform (the source).

Point3 *to

The array to store the transformed vectors (the destination).

int n

The number of vectors in the source array.

int stride = sizeof(Point3)

The size increment used when moving to the next source location.

int strideTo = sizeof(Point3)

The size increment used when moving to the next storage location.

Prototype:

void GetYawPitchRoll(float *yaw, float *pitch, float *roll);

Remarks:

This method is available in release 3.0 and later only.

Retrieves the yaw, pitch and roll angles represented by the rotation in this matrix.

Parameters:

float *yaw

The yaw rotation angle is stored here (in radians).

float *pitch

The pitch rotation angle is stored here (in radians).

float *roll

The roll rotation angle is stored here (in radians).

Operators:

Prototype:

int operator==(const Matrix3& M) const;

Remarks:

This method is available in release 3.0 and later only.

Compares the elements of this matrix and the one specified element by element for exact equality. Returns nonzero if they are equal; otherwise zero.

Parameters:

const Matrix3& M

The matrix to compare against.

Prototype:

Matrix3& operator-=(const Matrix3& M);

Remarks:

Subtracts a Matrix3 from this Matrix3.

Prototype:

Matrix3& operator+=(const Matrix3& M);

Remarks:

Adds a Matrix3 to this Matrix3.

Prototype:

Matrix3& operator*=(const Matrix3& M);

Remarks:

Multiplies this Matrix3 by the specified Matrix3 (*this = (*this)*M;).

Prototype:

Matrix3 operator*(const Matrix3&) const;

Remarks:

Perform matrix multiplication.

Prototype:

Matrix3 operator+(const Matrix3&) const;

Remarks:

Perform matrix addition.

Prototype:

Matrix3 operator-(const Matrix3&) const;

Remarks:

Perform matrix subtraction.

Prototype:

const Point3& operator[](int i) const;

Remarks:

Returns a reference to the 'i-th' Point3 of the matrix.

The following functions are not methods of Matrix3 but are available for use:

Function:

Matrix3 RotateXMatrix(float angle);

Remarks:

Builds a new matrix for use as a X rotation transformation.

Parameters:

float angle

Specifies the angle of rotation in radians.

Return Value:

A new X rotation Matrix3.

Function:

Matrix3 RotateYMatrix(float angle);

Remarks:

Builds a new matrix for use as a Y rotation transformation.

Parameters:

float angle

Specifies the angle of rotation in radians.

Return Value:

A new Y rotation Matrix3.

Function:

Matrix3 RotateZMatrix(float angle);

Remarks:

Builds a new matrix for use as a Z rotation transformation.

Parameters:

float angle

Specifies the angle of rotation in radians.

Return Value:

A new Z rotation Matrix3.

Function:

Matrix3 TransMatrix(const Point3& p);

Remarks:

Builds a new matrix for use as a translation transformation.

Parameters:

const Point3& p

Specifies the translation values.

Return Value:

A new translation Matrix3.

Function:

Matrix3 ScaleMatrix(const Point3& s);

Remarks:

Builds a new matrix for use as a scale transformation.

Parameters:

const Point3& p

Specifies the scale values.

Return Value:

A new scale Matrix3.

Function:

Matrix3 RotateYPRMatrix(float Yaw, float Pitch, float Roll);

Remarks:

Builds a new matrix for use as a rotation transformation by specifying yaw, pitch and roll angles.

This definition will depend on what our coordinate system is. This one is equivalent to:

M.IdentityMatrix();

M.RotateZ(roll);

M.RotateX(pitch);

M.RotateY(yaw);

Which presupposes Y is vertical, X is sideways, Z is forward

Parameters:

float Yaw

Specifies the yaw angle in radians.

float Pitch

Specifies the pitch angle in radians.

float Roll

Specifies the roll angle in radians.

Return Value:

A new rotation Matrix3.

Function:

Matrix3 RotAngleAxisMatrix(Point3& axis, float angle);

Remarks:

Builds a new matrix for use as a rotation transformation by specifying an angle and axis.

Parameters:

Point3& axis

Specifies the axis of rotation. Note that this angle is expected to be normalized.

float angle

Specifies the angle of rotation. Note: The direction of the angle in this method is opposite of that in AngAxisFromQ().

Return Value:

A new rotation Matrix3.

Function:

Matrix3 Inverse(const Matrix3& M);

Remarks:

Return the inverse of the matrix

Parameters:

const Matrix3& M

The matrix to compute the inverse of.

Function:

Point3 VectorTransform(const Matrix3& M, const Point3& V);

Remarks:

Transform the vector (Point3) with the specified matrix.

Parameters:

const Matrix3& A

The matrix to transform the vector with.

const Point3& V

The vector to transform.

Return Value:

The transformed vector (as a Point3).

Function:

Matrix3 XFormMat(const Matrix3& xm, const Matrix3& m);

Remarks:

This method is used to build a matrix that constructs a transformation in a particular space. For example, say you have a rotation you want to apply, but you want to perform the rotation in another coordinate system. To do this, you typically transform into the space of the coordinate system, then apply the transformation, and then transform out of that coordinate system. This method constructs a matrix that does just this. It transformats matrix m so it is applied in the space of matrix xm. It returns a Matrix3 that is xm*m*Inverse(xm).

Parameters:

const Matrix3& xm

Specifies the coordinate system you want to work in.

const Matrix3& m

Specifies the transformation matrix.

Return Value:

Returns a Matrix3 that is xm*m*Inverse(xm).

Function:

Point3 operator*(const Matrix3& A, const Point3& V);

Point3 operator*(const Point3& V, const Matrix3& A);

Remarks:

These transform a Point3 with a Matrix3. These two versions of transforming a point with a matrix do the same thing, regardless of the order of operands (linear algebra rules notwithstanding).

Parameters:

const Point3& V

The point to transform.

const Matrix3& A

The matrix to transform the point with.

Return Value:

The transformed Point3.