TGrafMatrix3D transforms points in homogeneous space using rational points represented by the TGRPoint3D class described above. In homogeneous space, the fourth TGRPoint coordinate (fW) allows translation, rotation around an arbitrary points, scaling around an arbitrary point, perspective transformations, and shear transformations in addition to rotating and scaling around the origin through matrix multiplication. You may want to refer to a book on matrix arithmetic for more information on matrix multiplication.
NOTE TGPoint3Ds have an implied fW of 1.0. Scaling a homogeneous matrix by a scalar does not change hot it transforms a TGPoint3D; however, scaling a homogeneous matrix by a scalar does change how it transforms a TGRPoint3D because a TGRPoint3D exists in homogeneous space.
TGrafMatrix3D is not a general math package. It does not solve sets of linear equations or eigenvalue problems, and doe not have arbitrary dimension. It is designed for the kinds of linear transformations (where the coordinate space is modified linearly) needed by graphics, geometries, the rendering pipeline, and GrafEdit.
TGrafMatrix3D is similar to TGrafMatrix (described in Chapter 2) except that the 3D coordinate system is different from the 2D coordinate system, and TGrafMatrix3D always scales relative to the world origin (with TGrafMatrix, the origin is specified).
Constructors
TGrafMatrix3D can be instantiated directly, and all of its public functions can be called directly. The default constructor creates an identity matrix. The other constructors create rotate, translate, and scale matrices. In most cases, you can avoid having to specify or know about the numbers in the matrix. One of the constructors takes a specified array of numbers; and the GetMatrix, GetElement, and SetElement functions provide access to the matrix. See the heading "Perspective map transformations" on page 243 for details.
Functions
TGrafMatrix implements the full set of 3-D linear affine transformations (translate, rotate, and scale) and supports concatenation (multiplying one matrix by another matrix). You an derive from TGrafMatrix to define your own transformation matrix implementation.
Table 11 lists the TGrafMatrix3D member functions. All functions have default implementations that you do not need to override. Refer to the online Class and Member Function descriptions online for information on the TGrafMatrix3D functions.
.
3D matrix member functions |
TGrafMatrix3D | ConcatWith | IsTranslate | SetToRotate |
GetDeterminant | MakeAdjoint | SetToScale | ||
GetElement | Normalize | SetToTranslate | ||
GetIdentity | PreConcatWith | TransformBounds | ||
GetMatrix | PreRotateBy | TransformPoint | ||
Hash | PreScaleBy | TransformPoints | ||
Invert | PreTranslateBy | TransformVector | ||
IsAffine | Reorthogonalize | TranslateBy | ||
IsEqual | RotateBy | Transpose | ||
IsIdentity | ScaleBy | UntransformBounds | ||
IsRotate | SetElement | UntransformPoint | ||
IsScale | SetToIdentity | UntransformVector |
To set up a translate, rotate, or scaling matrix, call the TranslateBy, RotateBy, and ScaleBy functions. To set up a matrix that will, for example, first scale and then rotate the graphic, call the ScaleBy and RotateBy functions in sequence. The matrix stores the cumulative transformation so you can apply one matrix to various graphics instead of scaling and rotating each graphic explicitly.
To create a sequence of arbitrary transformations rather than just translation, scaling, and rotation, use the ConcatWith function. ConcatWith concatenates two transformation matrices so that during the transformation each point passes first through one matrix and then the next. ConcatWith places the specified transformation matrix after the existing matrix.
The order by which matrices are multiplied is important because matrix multiplication is not commutative. Multiplication in one order does not necessarily give the same result as multiplication in the opposite order. For example, rotate times scale does not necessarily give the same result as scale times rotate even though the matrices are identical. You can use the PreConcatWith function to place the specified transformation matrix before the existing matrix.
SetToRotate, SetToScale, and SetToTranslate set the matrix to rotate, scale, or translate, respectively. These functions cannot be called in sequence like RotateBy, ScaleBy, and TranslateBy. If you call them in sequence, only the last call has any effect.
PreRotateBy, PreScaleBy, and PreTranslateBy premultiply this matrix by whatever values exist in the parameter list.
Perspective map transformations
Chapter 2 describes perspective map transformations. To summarize, a perspective map transformation involves defining what a convex quadrilateral (four-sided polygon) looks like before and after the transformation. You can use the TGrafMatrix:SetToPerspectiveMap function to create a perspective map to define all of the standard transformations. Chapter 2 describes affine and perspective transformations. Shearing transformation as it differs from a 3-D shearing transformation is described below.
Shearing transformation: A shear transformation is an affine transformation where the x coordinate of every point is changed in proportion to its y coordinate. Vectors cannot be translated; they can only be shared.
All matrix elements are type EMatrixIndex and can be accessed with the GetElement and SetElement functions. You can use the SetElement function to set up a shearing matrix. Once you set up the shear matrix, you can call SetToPerspective to create a perspective transformation. See Chapter 2 for an illustration of a shearing transformation in 2-D space.
The code below shows the SetElement function and the declaration for EMatrixIndex. Applying the matrix to a rational point (x, y, z, w), results in a rational point (x', y', z', w') using matrix multiplication:
The last row of the matrix defines translation. The diagonal row defines scaling factors. The upperleft submatrix defines rotation. The last column defines perspective. SetElement( EMatrixIndex index, GCoordinate& element ) const;
EMatrixIndex{
kScaleX = 0, kShearYX = 1, kShearZX = 2, kPerspectiveX = 3,
kShearXY = 4, kScaleY = 5, kShearZY = 6, kPerspectiveY = 7,
kShearXZ = 8, kShearYZ = 9, kScaleZ = 10, kPerspectiveZ = 11,
kTranslateX = 12, kTranslateY = 13, kTranslateZ = 14, kHomogeneous = 8 };
Create a 3-D matrix from a 2-D matrix
You can create a 3D matrix from a 2D matrix by calling this TGrafMatrix3D constructor
and giving it these values from TGrafMatrix: TGrafMatrix3D(
GCoordinate scaleX, GCoordinate shearYX, GCoordinate shearZX, GCoordinate perspectiveX,
GCoordinate shearXY, GCoordinate scaleY, GCoordinate shearZY, GCoordinate perspectiveY,
GCoordinate shearXZ, GCoordinate shearYZ, GCoordinate scaleZ, GCoordinate perspectiveZ,
GCoordinate translateX, GCoordinate translateY, GCoordinate translateZ,
GCoordinate homogenous );
kScaleX | kShearY | 0.0 | kPerspectiveX |
kShearX | kScaleY | 0.0 | kPerspectiveY |
0.0 | 0.0 | 1.0 | 0.0 |
kTranslateX | kTranslateY | 0.0 | kHomogeneous |