Matrix3d.mesa
Copyright © 1984 by Xerox Corporation. All rights reserved.
Bloomenthal, February 26, 1987 7:06:25 pm PST
Cf ``Principles of Interactive Computer Graphics,'' by Newman and Sproull.
Left-handed eyespace coordinate system (right, up, depth);
right-handed world coordinate system (longitude, latitude, altitude).
Wherever feasible, a procedure will use the argument "out," if provided,
rather than allocate a new MatrixRep.
DIRECTORY Vector3d;
Matrix3d: CEDAR DEFINITIONS
~ BEGIN
Pair:     TYPE ~ Vector3d.Pair;
Triple:    TYPE ~ Vector3d.Triple;
Quad:     TYPE ~ Vector3d.Quad;
origin:    Triple ~ Vector3d.origin;
A three-dimensional affine transformation, an element is m[row][col]:
Row:     TYPE ~ [0..4);
Col:     TYPE ~ [0..4);
Matrix:    TYPE ~ REF MatrixRep;
MatrixRep:   TYPE ~ ARRAY Col OF ARRAY Row OF REAL;
MatrixSequence:  TYPE ~ REF MatrixSequenceRep;
MatrixSequenceRep: TYPE ~ RECORD [element: SEQUENCE maxLength: NAT OF Matrix];
singular:    ERROR;
Creation Operations
CopyMatrix: PROC [in: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Return a copy of the input matrix.
CopyMatrixSequence: PROC [in: MatrixSequence] RETURNS [MatrixSequence];
Return a copy of the input sequence of matrices.
Identity: PROC [out: Matrix ← NIL] RETURNS [Matrix];
Return the identity matrix.
MakeFromTriad: PROC [v1, v2, v3: Triple, p: Triple ← origin, out: Matrix ← NIL] RETURNS [Matrix];
Return transformation to coordinate system defined by three orthonormal axes.
MakeScale: PROC [s: Triple, out: Matrix ← NIL] RETURNS [Matrix];
Make a matrix which only scales by [s.x, s.y, s.z].
MakeTranslate: PROC [p: Triple, out: Matrix ← NIL] RETURNS [Matrix];
Make a matrix which only translates by [p.x, p.y, p.z].
MakePureRotate: PROC [axis: Triple, theta: REAL, degrees: BOOLTRUE, out: Matrix ← NIL] RETURNS [Matrix];
Make matrix which only rotates by theta about axis.
MakeRotate: PROC [axis: Triple, theta: REAL, degrees: BOOLTRUE, base: Triple ← origin, out: Matrix ← NIL] RETURNS [Matrix];
Make matrix that only rotates by theta about line given by base, axis.
MakePerspective: PROC [dInv, fInv, fov: REAL, out: Matrix ← NIL] RETURNS [Matrix];
Return the perspective transformation: d (=1/dInv) is the distance the eye is along the -zaxis; dInverse = 0 for orthographic projection. f (=1/fInv) is the distance the far clipping plane is along the +zaxis; fInverse = 0 for no far clipping. field-of-view (fov) is in degrees. dInv=fInv=0 or fov=0 yields an orthographic projection.
HasPerspective: PROC [mat: Matrix] RETURNS [BOOL];
Return TRUE iff the matrix has a perspective element.
Transformation Operations
TransformH: PROC [p: Triple, mat: Matrix] RETURNS [Quad];
Postmultiply by transformation matrix, yielding a homogeneous point.
The following four transformation procedures test the transformation matrix for perspective (i.e.,
the matrix has perspective iff the last column is not [0, 0, 0, 1]. If a perspective element exists,
the return coordinates are first divided by w.
Transform: PROC [p: Triple, mat: Matrix] RETURNS [Triple];
Postmultiply by transformation matrix, yielding non-homogenous point.
TransformD: PROC [p: Triple, mat: Matrix] RETURNS [Pair];
Postmultiply by transformation matrix, yielding [x, y] pair.
This is useful when transforming from object to screen space, in which z-screen is not used.
TransformPair: PROC [p: Pair, mat: Matrix] RETURNS [Triple];
Postmultiply a point in the xy plane by a transformation matrix.
This is useful when transforming a planar contour into object space.
TransformPairD: PROC [p: Pair, mat: Matrix] RETURNS [Pair];
Postmultiply a point in the xy plane by mat, yielding [x, y] pair.
This is useful when transforming a planar contour into screen space.
If no differential scaling exists within the transformation matrix, TransformVec may be used.
Otherwise, use TransformVecDiffS.
TransformVec: PROC [vec: Triple, mat: Matrix] RETURNS [Triple];
Postmultiply a vector by a transformation matrix, without the translation components.
TransformVecDiffS: PROC [vec: Triple, mat: Matrix] RETURNS [Triple];
Postmultiply a vector by transform of cofactors of mat, in case mat has differential scaling (ie., the x, y, and z scale factors are not equal).
Concatenation Operations
The next 3 procs post-multiply the matrix, thus transforming in the reference coordinate space.
Scale: PROC [in: Matrix, s: REAL, out: Matrix ← NIL] RETURNS [Matrix];
Concatenate a scaling by s.
DiffScale: PROC [in: Matrix, s: Triple, out: Matrix ← NIL] RETURNS [Matrix];
Differential scaling: concatenate a scaling by [s.x, s.y, s.z].
Rotate: PROC [in: Matrix, axis: Triple, theta: REAL,
degrees: BOOLTRUE, base: Triple ← origin, out: Matrix ← NIL] RETURNS [Matrix];
Concatenate rotation of theta about the line given by base and axis.
Translate: PROC [in: Matrix, p: Triple, out: Matrix ← NIL] RETURNS [Matrix];
Concatenate a translation by [p.x, p.y, p.z].
The next 3 procs pre-multiply the matrix, thus transforming in the local coordinate space.
LocalScale: PROC [in: Matrix, s: REAL, out: Matrix ← NIL] RETURNS [Matrix];
Concatenate a local scaling by s.
LocalDiffScale: PROC [in: Matrix, s: Triple, out: Matrix ← NIL] RETURNS [Matrix];
Differential scaling: concatenate a local scaling by [s.x, s.y, s.z].
LocalRotate: PROC [in: Matrix, axis: Triple, theta: REAL,
degrees: BOOLTRUE, base: Triple ← origin, out: Matrix ← NIL] RETURNS [Matrix];
Concatenate local rotation of theta radians about line given by base, axis.
LocalTranslate: PROC [in: Matrix, p: Triple, out: Matrix ← NIL] RETURNS [Matrix];
Concatenate a local translation.
Mathematical Operations
Mul: PROC [left, rite: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Returns matrix product left right;
Examples: a ← Multiply[a, b, a]; b ← Multiply[a, b, b]; c ← Multiply[a, b].
Cat: PROC [m1, m2: Matrix, m3, m4, m5, m6, out: Matrix ← NIL] RETURNS [Matrix];
Return concatenation of input matrices.
Invert: PROC [in: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Can raise ERROR: singular.
Adjoint: PROC [in: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Return the adjoint of in.
Cofactors: PROC [in: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Return the cofactors of in.
Determinant: PROC [mat: Matrix] RETURNS [REAL];
Return the determinant of mat.
Transpose: PROC [in: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Return the transpose of in.
InTermsOf: PROC [A, B: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Return B in terms of A.
It is useful to be able to express one coordinate system in terms of another. Consider this
example. Given an earth coordinate system and a moon coordinate system, both expressed in
world coordinates, then displaying the moon rotating about the earth may be expressed as:
MoonInTermsOfEarth ← InTermsOf[earth, moon];
FOREVER DO:
MoonInTermsOfEarth ← YRotate[MoonInTermsOfEarth, theta];
moon ← Mul[earth, MoonInTermsOfEarth];
Draw[moon]
ENDLOOP;
Invert3x3: PROC [m: Matrix, out: Matrix ← NIL] RETURNS [Matrix];
Invert the 3 by 3 matrix formed from the first three columns and rows of m, and return in out; the last column and last row of out are unchanged.
Miscellaneous Operations
ObtainMatrix: PROC RETURNS [Matrix];
Obain a matrix from the scratch pool; using the pool mimizes storage allocation.
ReleaseMatrix: PROC [matrix: Matrix];
Return the matrix to the scratch pool.
END.