DIRECTORY SV2d, SV3d, SVVector3d; Matrix3d: CEDAR DEFINITIONS = BEGIN Matrix4by4: TYPE = SV3d.Matrix4by4; Plane: TYPE = SV3d.Plane; Point3d: TYPE = SV3d.Point3d; Point2d: TYPE = SV2d.Point2d; Vector3d: TYPE = SVVector3d.Vector3d; Identity: PROC [] RETURNS [identityMat: Matrix4by4]; Update: PROC [point: Point3d, mat: Matrix4by4] RETURNS [newPoint: Point3d]; UpdateDisplacement: PROC [mat: Matrix4by4, vec: Vector3d] RETURNS [newVec: Vector3d]; UpdateVectorWithInverse: PROC [inverse: Matrix4by4, vec: Vector3d] RETURNS [newVec: Vector3d]; UpdateVectorComputeInverse: PROC [vec: Vector3d, mat: Matrix4by4] RETURNS [newVec: Vector3d]; UpdateVectorEvenScaling: PROC [vec: Vector3d, mat: Matrix4by4] RETURNS [newVec: Vector3d]; UpdatePlaneWithInverse: PROC [plane: Plane, inverse: Matrix4by4] RETURNS [newPlane: Plane]; UpdatePlaneComputeInverse: PROC [plane: Plane, mat: Matrix4by4] RETURNS [newPlane: Plane]; PerspectiveTrans: PROC [oldPoint: Point3d, focalDistance: REAL _ 1800] RETURNS [newPoint: Point2d]; IllegalDepth: SIGNAL; Scale: PROC [mat: Matrix4by4, sx, sy, sz: REAL] RETURNS [transMat: Matrix4by4]; Translate: PROC [mat: Matrix4by4, dx, dy, dz: REAL] RETURNS [transMat: Matrix4by4]; Rotate: PROC [mat: Matrix4by4, axis: [1..3], degrees: REAL] RETURNS [transMat: Matrix4by4]; RotateX: PROC [mat: Matrix4by4, degrees: REAL] RETURNS [transMat: Matrix4by4]; RotateY: PROC [mat: Matrix4by4, degrees: REAL] RETURNS [transMat: Matrix4by4]; RotateZ: PROC [mat: Matrix4by4, degrees: REAL] RETURNS [transMat: Matrix4by4]; RotateAxis: PROC [mat: Matrix4by4, axis: Vector3d, degrees: REAL] RETURNS [transMat: Matrix4by4]; SetOrigin: PROC [mat: Matrix4by4, origin: Point3d] RETURNS [newMat: Matrix4by4]; LocalScale: PROC [mat: Matrix4by4, sx, sy, sz: REAL] RETURNS [transMat: Matrix4by4]; LocalTranslate: PROC [mat: Matrix4by4, dx, dy, dz: REAL] RETURNS [transMat: Matrix4by4]; LocalRotate: PROC [mat: Matrix4by4, axis: [1..3], degrees: REAL] RETURNS [transMat: Matrix4by4]; LocalRotateX: PROC [mat: Matrix4by4, degrees: REAL] RETURNS [transMat: Matrix4by4]; LocalRotateY: PROC [mat: Matrix4by4, degrees: REAL] RETURNS [transMat: Matrix4by4]; LocalRotateZ: PROC [mat: Matrix4by4, degrees: REAL] RETURNS [transMat: Matrix4by4]; LocalRotateAxis: PROC [mat: Matrix4by4, axis: Vector3d, degrees: REAL] RETURNS [transMat: Matrix4by4]; MatMult: PROC [left, right: Matrix4by4] RETURNS [transMat: Matrix4by4]; Mult: PROC [ab, bc: Matrix4by4] RETURNS [ac: Matrix4by4]; Cat: PROC [ab, bc, cd: Matrix4by4] RETURNS [ad: Matrix4by4]; MakeScaleMat: PROC [sx, sy, sz: REAL] RETURNS [scale: Matrix4by4]; MakeTranslateMat: PROC [dx, dy, dz: REAL] RETURNS [trans: Matrix4by4]; MakeRotateMat: PROC [axis: [1..3], degrees: REAL] RETURNS [rotMat: Matrix4by4]; MakeRotateXMat: PROC [degrees: REAL] RETURNS [rotx: Matrix4by4]; MakeRotateYMat: PROC [degrees: REAL] RETURNS [roty: Matrix4by4]; MakeRotateZMat: PROC [degrees: REAL] RETURNS [rotz: Matrix4by4]; MakeRotateAxisMat: PROC [axis: Vector3d, degrees: REAL] RETURNS [rotMat: Matrix4by4]; MakeMatFromZandXAxis: PROC [zAxis, xAxis: Vector3d, origin: Point3d] RETURNS [mat: Matrix4by4]; MakeMatFromYandXAxis: PROC [yAxis, xAxis: Vector3d, origin: Point3d] RETURNS [mat: Matrix4by4]; MakeHorizontalMatFromZAxis: PROC [zAxis: Vector3d, origin: Point3d] RETURNS [mat: Matrix4by4]; MakeMatFromAxes: PROC [xAxis, yAxis, zAxis: Vector3d, origin: Point3d] RETURNS [mat: Matrix4by4]; ScaleFromMatrix: PROC [mat: Matrix4by4] RETURNS [sx, sy, sz: REAL]; OriginOfMatrix: PROC [mat: Matrix4by4] RETURNS [origin: Point3d]; XAxisOfMatrix: PROC [mat: Matrix4by4] RETURNS [xAxis: Vector3d]; YAxisOfMatrix: PROC [mat: Matrix4by4] RETURNS [yAxis: Vector3d]; ZAxisOfMatrix: PROC [mat: Matrix4by4] RETURNS [zAxis: Vector3d]; InverseNoScaling: PROC [mat: Matrix4by4] RETURNS [inverse: Matrix4by4]; DegenerateInverse: SIGNAL; Cofactor: PROC [mat: Matrix4by4, row, col: NAT] RETURNS [cof: REAL]; Determinant: PROC [mat: Matrix4by4] RETURNS [det: REAL]; Transpose: PROC [mat: Matrix4by4] RETURNS [MatT: Matrix4by4]; Inverse: PROC [mat: Matrix4by4] RETURNS [inverse: Matrix4by4]; AInTermsOfB: PROC [aWORLD, bWORLD: Matrix4by4] RETURNS [ab: Matrix4by4]; UnitNormalize: PROC [mat: Matrix4by4] RETURNS [unitMat: Matrix4by4]; Normalize: PROC [mat: Matrix4by4] RETURNS [normMat: Matrix4by4]; NormalizeUniformScale: PROC [mat: Matrix4by4] RETURNS [normMat: Matrix4by4]; NormalizeNoRotation: PROC [mat: Matrix4by4] RETURNS [normMat: Matrix4by4]; NormalizeNoTranslation: PROC [mat: Matrix4by4] RETURNS [normMat: Matrix4by4]; TidyUpVector: PROC [vec: Vector3d] RETURNS [fixedVec: Vector3d]; TidyUpMatrix: PROC [mat: Matrix4by4] RETURNS [fixedMat: Matrix4by4]; END. File: Matrix3d.mesa Last edited by Bier on May 28, 1987 11:00:54 am PDT Author: Eric Allan Bier in June 1982 Contents: Definitions module for the 4 by 4 homogeneous 3d transform package. This package assumes a matrix of the form: [nx ox ax px] where n = [nx ny nz]T is the "normal" column vector, [ny oy ay py] o = [ox oy oz]T is the "orthogonal" column vector [nz oz az pz] a = [ax ay az]T is the "axial" column vector, [0 0 0 1] and p = [px, py, pz]T is the "position" column see Richard Paul's "Robot Manipulators" Chapter 1 for more information Use this procedure if you are transposing a point in space or a displacement vector. Because of the wonders of mathematics, if a shape is transformed by a matrix transform A, then its normal vector should be transformed by A transpose inverse. If A involves only even scaling, rotation and translation then using A (minus translational components) will produce a vector with the proper direction (though not scaled as it would be with A transpose inverse). However, if A has differential scaling, then A transpose inverse must be used. If you already have A inverse handy, then don't bother to recompute it. Use UpdateVectorWithInverse. Otherwise use UpdateVectorComputeInverse. If there is no differential scaling in A and the magnitude of the result is not important, use UpdateVectorEvenScaling. The next five procedures perform an efficient matrix multiplication as though mat were being multiplied to its left (a transform in reference coordinates) by a matrix which translates or rotates respectively. Calls RotateX, RotateY, or RotateZ as appropriate. Set the translation component of mat to the new value to create a new matrix. The next five procedures perform an efficient matrix multiplication as though mat were being multiplied on its right (a transform in local coordinates) by a matrix which translates or rotates respectively. Calls LocalRotateX, LocalRotateY, or LocalRotateZ as appropriate. The next six procedures allow the user direct access to the transform matrices and their manipulation. MatMult is almost a general 4 by 4 matrix operation. However, it assumes that the last row of each matrix is [0 0 0 1]. Like MatMult but treats the matrices as though vectors were rows. This is the wave of the future. Mult[Mult[ab, bc], cd]; Uses the normalized version of the zAxis given as it is. Projects xAxis onto the plane of zAxis and normalizes. Finds y by cross product. Uses zAxis as it is. Finds a horizontal x axis orthogonal to zAxis. If zAxis is vertical, then there are infinitely many. Chooses [1,0,0] in this case. Uses the axes just as they are given. Doesn't even check to see if they are orthogonal. Finds out how much this matrix will scale an object it is applied to. Because of the orthogonality of the transform matrices, matrix inversion is relatively easy. WARNING: This procedure inverts the translation and rotation components of a matrix, but does not invert the scaling component. Hence, if you take a matrix A which scales by 10, translates, and rotates. Applying the matrix and its inverse to an object will leave in its original position, scaled by 100! Of course, if the scaling factor is 1, this doesn't matter, and this procedure is very fast. Returns the determinant of the 3 by 3 matrix which results by crossing off row row and column col in mat. Transposes just the linear part of mat. The translation is unaffected. Inverts any matrix of the given form (even with scaling) Once you can create and manipulate objects in the world (reference) coordinate system, it is useful to be able to express one coordinate system in terms of another. Consider a solar system example. If you have the center of the sun as world coordinate system, and earth and moon, as two other coordinate system expressed in terms of world coordinates (recall that a 4 by 4 matrix represents the relationship between coordinate systems), then if you want the moon to rotate around the earth, you might try: MoonInTermsOfEarth _ WorldToLocal[earth,moon]; FOREVER DO: MoonInTermsOfEarth _ RotateAboutY[MoonInTermsOfEarth, degrees]; moon _ MatMult[earth, MoonInTermsOfEarth]; DrawSphereCenteredAt[moon]; ENDLOOP; Normalizing Finds a matrix transform which provides the same translation and rotation of mat, but with a scaling of 1. While we're at it, make sure the axes of unitMat are perpendicular. Finds a matrix with the same translation, rotation and scaling as mat, but with the axes guaranteed to be orthogonal. Finds a matrix with the same translation, and rotation as mat, with the axes guaranteed to be orthogonal and with the scaling along all three axes set equal to the maximum scaling of the three axes of mat. Finds a matrix with the same translation and scaling as mat, with the axes guaranteed to be orthogonal, removing rotational components. Finds a matrix with the same scaling and rotation as mat, with the axes guaranteed to be orthogonal, removing translational components. If a component is almost a multiple of .5, make it exactly so. If a component is almost a multiple of .5, make it exactly so. Κ^– "Cedar" style˜Ihead1šœ™Jšœ3™3Jšœ$™$Jšœy™yJšœC™CJšœ@™@Jšœ<™™>JšœG™GšΟk ˜ Jšœ˜—Jšœ œ˜šœ˜Jšœ œœ ˜#Jšœœ˜Jšœ œ˜Jšœ œ˜Jšœ œ˜&—JšΟnœœœ˜5Iprocšžœœ#œ˜Kšžœœ"œ˜UL™T—šžœœ&œ˜^LšœWœ Οbœ!œœAœyœ"œ œ6œ€œO™Ο—Lšžœœ"œ˜]Lšžœœ"œ˜[Lšžœœ%œ˜[Lšžœœ!œ˜[Lšžœœ$œ˜FLšœ˜šœœ˜LšœΡ™Ρ—Lšžœœœœ˜OLšž œœœœ˜Sšžœœ*œœ˜[Lšœ2™2—Lšžœœœœ˜NLšžœœœœ˜NLšžœœœœ˜NLšž œœ,œœ˜aL˜šž œœ$œ˜PL™M—˜LšœΞ™Ξ—Lšž œœœœ˜TLšžœœœœ˜Xšž œœ*œœ˜`LšœA™A—Lšž œœœœ˜SLšž œœœœ˜SLšž œœœœ˜Sšžœœ,œœ˜fLšœh™h—L˜šžœœœ˜GLšœx™x—šžœœœ˜9Lšœb™b—šžœœœ˜Lšœ8™8Lšœό™όLšœΞ™Ξ—L˜š ž œœΟuœ œœ œ˜HIheadšœ ™ —L˜šž œœœ˜DLšœ―™―—šž œœœ˜@Lšœu™u—šžœœœ˜LLšœΝ™Ν—šžœœœ˜JLšœ‡™‡—šžœœœ˜MLšœ‡™‡—šž œœœ˜@J™>—šž œœœ˜DJ™>—Lšœœ˜—…—D,Ό