File: SVTransformsImpl.mesa
Last edited by Bier on June 2, 1987 4:42:07 pm PDT
Copyright © 1984 by Xerox Corporation. All rights reserved.
Contents: Procedures for Scaling, Translating, and Rotating assemblies with respect to an arbitrary coordinate system
DIRECTORY
CoordSys, Feedback, Matrix3d, Rope, SV3d, SVAssembly, SVCoordSysType, SVMatrixOps, SVModelTypes, SVSceneTypes, SVTransforms, SVVector3d;
SVTransformsImpl: CEDAR PROGRAM
IMPORTS CoordSys, Feedback, Matrix3d, Rope, SVAssembly, SVMatrixOps, SVVector3d
EXPORTS SVTransforms, SVModelTypes =
BEGIN
Slice: TYPE = SVSceneTypes.Slice;
SliceList: TYPE = SVSceneTypes.SliceList;
Camera: TYPE = SVModelTypes.Camera;
CoordSystem: TYPE = REF CoordSysObj;
CoordSysObj: PUBLIC TYPE = SVCoordSysType.CoordSysObj;
CoordSysList: TYPE = SVModelTypes.CoordSysList;
Matrix4by4: TYPE = SV3d.Matrix4by4;
Point3d: TYPE = SV3d.Point3d;
Scene: TYPE = SVSceneTypes.Scene;
Shape: TYPE = SVSceneTypes.Shape;
Vector3d: TYPE = SV3d.Vector3d;
IncTransfMatrix:
PUBLIC
PROC [c: CoordSystem, d
World: Matrix4by4, m: Matrix4by4] = {
p: CoordSystem;
cWorld, pWorld: Matrix4by4;
IF MatrixHasScaling[m]
THEN {
SIGNAL Feedback.Problem[msg: "SVTransform.IncTransfMatrix got illegal matrix. Ignored."];
RETURN};
p ← CoordSys.Parent[c];
IF p = NIL THEN ERROR; -- cannot translate WORLD for now
cWorld ← CoordSys.WRTWorld[c];
pWorld ← CoordSys.WRTWorld[p];
CoordSys.SetMat[c, SVMatrixOps.IncTransf[cWorld, pWorld, dWorld, m]];
};
PlaneOriginRotate:
PUBLIC
PROC [c: CoordSystem, d: CoordSystem, degrees:
REAL, plane:
NAT] = {
Rotates c about an axis parallel to the plane axis of d, passing thru the origin of c.
m, PivotWORLD, CWORLD, DWORLD: Matrix4by4;
origin: Point3d;
xAxis, yAxis, zAxis: Vector3d;
CWORLD ← CoordSys.WRTWorld[c];
DWORLD ← CoordSys.WRTWorld[d];
origin ← Matrix3d.OriginOfMatrix[CWORLD];
xAxis ← Matrix3d.XAxisOfMatrix[DWORLD];
yAxis ← Matrix3d.YAxisOfMatrix[DWORLD];
zAxis ← Matrix3d.ZAxisOfMatrix[DWORLD];
PivotWORLD ← Matrix3d.MakeMatFromAxes[xAxis, yAxis, zAxis, origin];
SELECT plane
FROM
1 => m ← Matrix3d.MakeRotateXMat[degrees];
2 => m ← Matrix3d.MakeRotateYMat[degrees];
3 => m ← Matrix3d.MakeRotateZMat[degrees];
ENDCASE => ERROR;
IncTransfMatrix[c, PivotWORLD, m];
};
AbsTransfScreen:
PUBLIC
PROC [c: CoordSystem, n: Matrix4by4] = {
CoordSys.SetMat[c, n];
};
AbsTransfMatrix:
PUBLIC PROC [c: CoordSystem, d
World: Matrix4by4, n: Matrix4by4] = {
p: CoordSystem;
pWORLD: Matrix4by4;
IF Rope.Equal[c.name, "SCREEN", TRUE] THEN {CoordSys.SetMat[c, n]; RETURN};
p ← CoordSys.Parent[c];
IF p = NIL THEN ERROR; -- cannot translate WORLD for now
IF MatrixHasScaling[n]
THEN {
SIGNAL Feedback.Problem[msg: "SVTransform.AbsTransf got illegal matrix. Ignored."];
RETURN};
pWORLD ← CoordSys.WRTWorld[p];
CoordSys.SetMat[c, SVMatrixOps.AbsTransf[pWORLD, dWorld, n]];
};
A set of routines which call IncTransf
Translate:
PUBLIC
PROC [c: Slice, scene: Scene, vec: Point3d, d: CoordSystem ←
NIL, dFixed:
BOOL ←
FALSE] = {
m: Matrix4by4;
m ← Matrix3d.MakeTranslateMat[vec[1], vec[2], vec[3]];
SVAssembly.IncTransf[c, scene, m, d, dFixed];
DirtyAssembly[c];
};
Rotate:
PUBLIC
PROC [c: Slice, scene: Scene, axis: [1..3], degrees:
REAL, d: CoordSystem ←
NIL, dFixed:
BOOL ←
FALSE] = {
Simple rotate C's coordsys wrt D by degrees about the x, y, or z axis
m: Matrix4by4;
SELECT axis
FROM
1 => m ← Matrix3d.MakeRotateXMat[degrees];
2 => m ← Matrix3d.MakeRotateYMat[degrees];
3 => m ← Matrix3d.MakeRotateZMat[degrees];
ENDCASE => ERROR;
SVAssembly.IncTransf[c, scene, m, d, dFixed];
DirtyAssembly[c];
};
A set of routines which call AbsTransf
AbsTranslateOnly:
PUBLIC
PROC [c: Slice, scene: Scene, d: CoordSystem, v
D: Point3d] = {
Places C so that the rotation from C to D remains as it is, but the translation becomes vD. This requres finding CD, modifying its translation, and performing AbsTransF on the result.
CD: Matrix4by4;
CD ← CoordSys.FindAInTermsOfB[c.coordSys, d];
CD[1][4] ← vD[1];
CD[2][4] ← vD[2];
CD[3][4] ← vD[3];
SVAssembly.AbsTransf[c, scene, CD, d];
DirtyAssembly[c];
};
Align:
PUBLIC
PROC [c: Slice, scene: Scene, d: CoordSystem] = {
Find CD. Compute the magnitude of its axes. Make CD have axes of the same magnitude, aligned with respect to the nearest axes of D.
CD: Matrix4by4;
xAxisChoices, zAxisChoices: ARRAY[1..3] OF NAT;
xAxis, zAxis, newXAxis, newYAxis, newZAxis: Vector3d;
xNegArray, zNegArray: ARRAY[1..3] OF BOOL;
origin: Point3d;
CD ← CoordSys.FindAInTermsOfB[c.coordSys, d];
xAxis ← Matrix3d.XAxisOfMatrix[CD];
zAxis ← Matrix3d.ZAxisOfMatrix[CD];
origin ← Matrix3d.OriginOfMatrix[CD];
[xAxisChoices, xNegArray] ← IndicesOfMax[xAxis]; -- Find largest component
This direction is now taken. z axis must find another.
[zAxisChoices, zNegArray] ← IndicesOfMax[zAxis];
IF zAxisChoices[1] = xAxisChoices[1] THEN zAxisChoices[1] ← zAxisChoices[2];
zAxis gets second choice if xAxis has its first choice
newXAxis ← [0,0,0];
newXAxis[xAxisChoices[1]] ← SVVector3d.Magnitude[xAxis];
IF xNegArray[xAxisChoices[1]]
THEN
newXAxis[xAxisChoices[1]] ← -newXAxis[xAxisChoices[1]];
newZAxis ← [0,0,0];
newZAxis[zAxisChoices[1]] ← SVVector3d.Magnitude[zAxis];
IF zNegArray[zAxisChoices[1]]
THEN
newZAxis[zAxisChoices[1]] ← -newZAxis[zAxisChoices[1]];
newYAxis ← SVVector3d.CrossProduct[newZAxis, newXAxis];
CD ← Matrix3d.MakeMatFromAxes[newXAxis, newYAxis, newZAxis, origin];
SVAssembly.AbsTransf[c, scene, CD, d];
DirtyAssembly[c];
}; -- end of Align
IndicesOfMax:
PRIVATE
PROC [v: Vector3d]
RETURNS [indices:
ARRAY[1..3]
OF
NAT, negArray:
ARRAY[1..3]
OF
BOOL] = {
Bubble sort
temp: REAL;
tempIndex: NAT;
IF v[1] < 0 THEN {negArray[1] ← TRUE; v[1] ← -v[1]}
ELSE negArray[1] ← FALSE;
IF v[2] < 0 THEN {negArray[2] ← TRUE; v[2] ← -v[2]}
ELSE negArray[2] ← FALSE;
IF v[3] < 0 THEN {negArray[3] ← TRUE; v[3] ← -v[3]}
ELSE negArray[3] ← FALSE;
indices ← [1,2,3];
IF v[1] < v[2]
THEN {
temp ← v[1]; v[1] ← v[2]; v[2] ← temp;
tempIndex ← indices[1]; indices[1] ← indices[2]; indices[2] ← tempIndex};
IF v[2] < v[3]
THEN {
temp ← v[2]; v[2] ← v[3]; v[3] ← temp;
tempIndex ← indices[2]; indices[2] ← indices[3]; indices[3] ← tempIndex};
IF v[1] < v[2]
THEN {
temp ← v[1]; v[1] ← v[2]; v[2] ← temp;
tempIndex ← indices[1]; indices[1] ← indices[2]; indices[2] ← tempIndex};
};
Abut:
PUBLIC
PROC [c: Slice, scene: Scene, d: CoordSystem] = {
Leaving the rotation of C as it is, put C's origin coincident with the origin of D.
AbsTranslateOnly[c, scene, d, [0,0,0]];
};
AbutAxis:
PUBLIC
PROC [c: Slice, scene: Scene, axis: [1..3], d: CoordSystem] = {
Leave the rotation of C as it is. Put C's origin in the x = 0 plane of D.
vD: Vector3d;
vD ← CoordSys.FindTranslationOfAinTermsOfB[c.coordSys, d];
SELECT axis
FROM
1 => AbsTranslateOnly[c, scene, d, [0, vD[2], vD[3]]];
2 => AbsTranslateOnly[c, scene, d, [vD[1], 0, vD[3]]];
3 => AbsTranslateOnly[c, scene, d, [vD[1], vD[2], 0]];
ENDCASE;
};
NormalizeRot:
PUBLIC PROC [c: Slice, scene: Scene, d: CoordSystem] = {
Leave translation between C and D as is. The axes of C become parallel to the axes of D.
oldMat, newMat: Matrix4by4;
oldMat ← CoordSys.FindAInTermsOfB[a: c.coordSys, b: d];
newMat ← Matrix3d.NormalizeNoRotation[oldMat];
SVAssembly.AbsTransf[c, scene, newMat, d];
DirtyAssembly[c];
};
Normalize:
PUBLIC PROC [c: Slice, scene: Scene, d: CoordSystem] = {
C becomes identical to D.
SVAssembly.AbsTransf[c, scene, Matrix3d.Identity[], d];
DirtyAssembly[c];
};
A set of routines which specialize TugTransf
NormalizeRotTug:
PUBLIC
PROC [c: Slice, scene: Scene, tugBoat: Slice, d: CoordSystem] = {
f: Matrix4by4 ← CoordSys.FindAInTermsOfB[c.coordSys, tugBoat.coordSys];
NormalizeRot[tugBoat, scene, d];
SVAssembly.AbsTransf[c, scene, f, tugBoat.coordSys, TRUE];
DirtyAssembly[c];
};
NormalizeTug:
PUBLIC
PROC [c: Slice, scene: Scene, tugBoat: Slice, d: CoordSystem] = {
f: Matrix4by4 ← CoordSys.FindAInTermsOfB[c.coordSys, tugBoat.coordSys];
Normalize[tugBoat, scene, d];
SVAssembly.AbsTransf[c, scene, f, tugBoat.coordSys, TRUE];
DirtyAssembly[c];
};
AlignTug:
PUBLIC
PROC [c: Slice, scene: Scene, tugBoat: Slice, d: CoordSystem] = {
f: Matrix4by4 ← CoordSys.FindAInTermsOfB[c.coordSys, tugBoat.coordSys];
Align[tugBoat, scene, d];
SVAssembly.AbsTransf[c, scene, f, tugBoat.coordSys, TRUE];
DirtyAssembly[c];
};
AbutTug:
PUBLIC
PROC [c: Slice, scene: Scene, tugBoat: Slice, d: CoordSystem] = {
f: Matrix4by4 ← CoordSys.FindAInTermsOfB[c.coordSys, tugBoat.coordSys];
Abut[tugBoat, scene, d];
SVAssembly.AbsTransf[c, scene, f, tugBoat.coordSys, TRUE];
DirtyAssembly[c];
};
AbutTugAxis:
PUBLIC
PROC [c: Slice, scene: Scene, axis: [1..3], tugBoat: Slice, d: CoordSystem] = {
f: Matrix4by4 ← CoordSys.FindAInTermsOfB[c.coordSys, tugBoat.coordSys];
AbutAxis[tugBoat, scene, axis, d];
SVAssembly.AbsTransf[c, scene, f, tugBoat.coordSys, TRUE];
DirtyAssembly[c];
};
Routines for updating cached matrices:
DirtyAssembly:
PUBLIC
PROC [a: Slice] = {
This assembly has moved. Mark its coordinate system and those of its children as dirty.
WITH a.shape SELECT FROM
shape: Shape => {
a.coordSys.worldOK ← FALSE;
a.coordSys.inverseOK ← FALSE;
};
alist: SliceList => {
a.coordSys.worldOK ← FALSE;
a.coordSys.inverseOK ← FALSE;
DirtyAssemblyList[alist];
};
ENDCASE => ERROR;
}; -- end of DirtyAssembly
DirtyAssemblyList: PRIVATE PROC [alist: SliceList] = {
FOR list: LIST OF Slice ← alist.list, list.rest UNTIL list = NIL DO
DirtyAssembly[list.first];
ENDLOOP;
};
TidyUpCoordSysTree:
PUBLIC
PROC [root: CoordSystem] = {
If a component is almost a multiple of .5, make it exactly so.
parent: CoordSystem ← root.parent;
IF root.scalarsOnly
THEN {
IF root.children # NIL THEN ERROR; -- scalars-only coordinate systems must be leaves.
root.scalars ← Matrix3d.TidyUpVector[root.scalars];
}
ELSE {
root.mat ← Matrix3d.TidyUpMatrix[root.mat];
IF root.children #
NIL
THEN {
-- Process the children as well
FOR list: CoordSysList ← root.children, list.rest
UNTIL list =
NIL
DO
TidyUpCoordSysTree[list.first];
ENDLOOP;
};
};
}; -- end of TidyUpCoordSysTree
TellAboutCameraAndWorld: PUBLIC PROC [root: CoordSystem, camera: Camera] = {
Tree walk through the tree and concatenate all matrices (will later concatenate matrices as it goes). This computes values for intermediate matrices as well as primitive matrices. Can be started using any assembly as root.
camera.coordSys.wrtWorld ← CoordSys.WRTWorld[camera.coordSys];
TellAboutCameraAndWorldAux[root, camera];
};
TellAboutCameraAndWorldAux: PRIVATE PROC [root: CoordSystem, camera: Camera] = {
parent: CoordSystem ← root.parent;
IF root.scalarsOnly THEN {
IF root.children # NIL THEN ERROR; -- scalars only coordinate systems must be leaves.
Our matrices result from scaling our parent's matrices.
root.wrtCamera ← Matrix3d.LocalScale[CoordSys.WRTCamera[parent, camera.coordSys], root.scalars[1], root.scalars[2], root.scalars[3]];
root.wrtWorld ← Matrix3d.LocalScale[CoordSys.WRTWorld[parent], root.scalars[1], root.scalars[2], root.scalars[3]];
root.cameraWRTlocal ← Matrix3d.Inverse[root.wrtCamera];
root.worldWRTlocal ← Matrix3d.Inverse[root.wrtWorld];
}
ELSE {
root.wrtCamera ← CoordSys.WRTCamera[root, camera.coordSys];
root.wrtWorld ← CoordSys.WRTWorld[root];
root.cameraWRTlocal ← Matrix3d.Inverse[root.wrtCamera];
root.worldWRTlocal ← Matrix3d.Inverse[root.wrtWorld];
IF root.children # NIL THEN { -- Process the children as well
FOR list: CoordSysList ← root.children, list.rest UNTIL list = NIL DO
TellAboutCameraAndWorldAux[list.first, camera];
ENDLOOP;
};
};
}; -- end of TellAboutCameraAndWorldAux
TellAboutParent: PUBLIC PROC [cs: CoordSystem] = {
Updates the wrtWorld, and wrtCamera fields of cs by premultiplying cs.mat by cs.parent.wrtWorld and cs.parent.wrtCamera respectively
cs.wrtWorld ← Matrix3d.MatMult[cs.parent.wrtWorld, cs.mat];
cs.wrtCamera ← Matrix3d.MatMult[cs.parent.wrtCamera, cs.mat];
cs.worldWRTlocal ← Matrix3d.Inverse[cs.wrtWorld];
cs.cameraWRTlocal ← Matrix3d.Inverse[cs.wrtCamera];
};
Scaling operations (IncTransf derivatives but more complicated).
There are five ways to scale a cluster assembly
1) (ScalePrimitives) Scale each primitive in place, evenly or differentially.
2) (ScaleEven) If sx = sy = sz. Scale primitives in place and then translate all intermediate assemblies so the cluster assembly scales evenly as a unit
3) (ScaleNoShear) IF sx # sy or sy # sz. Scale the primitives evenly so that their dimension in the direction of scaling increases or decreases to the same quantity it would have reached by method 3. This preserves shape of primitives. Global shape is not preserved (but almost).
4) (ScaleEvenChildren) Perform a ScaleEven on each of the children of the cluster assembly but leave their relative translations fixed. This is equivalent to performing a ScaleEven (child, child's cs, scalar) on each child individually.
5) (ScaleDistancesChildren) Doesn't change the size or shape of any of the children of assembly. Simply translates all of the children as they would be translated by a Scale even of assembly.
ScalePrimitives:
PUBLIC
PROC [a: Slice, sx, sy, sz:
REAL] = {
WITH a.shape
SELECT
FROM
shape: Shape => SVAssembly.ScalePrimitive[a, sx, sy, sz];
alist: SliceList => {
FOR list:
LIST
OF Slice ← alist.list, list.rest
UNTIL list =
NIL
DO
ScalePrimitives[list.first, sx, sy, sz];
ENDLOOP;
};
ENDCASE; -- IF a.shape = NIL or something else, do nothing.
};
ScaleEven:
PUBLIC
PROC [a: Slice, scene: Scene, scalar:
REAL, d: CoordSystem ←
NIL] = {
Just a bit tricky.
If a is a Primitive and cs is any coordinate frame, just scale and translate it from the center of cs by scalar times its current translation.
If a is a cluster, and cs is any coordinate frame, evenly scale each component of a about a's center, then translate a as a unit, the appropriate distance from the center of cs.
displacements: Vector3d;
WITH a.shape
SELECT
FROM
shape: Shape => {
SVAssembly.ScalePrimitive[a, scalar, scalar, scalar];
now translate a's coordSys so that its displacement from the origin of cs is scaled by scalar
displacements ← CoordSys.FindTranslationOfAinTermsOfB[a.coordSys, d];
displacements ← SVVector3d.Scale[displacements, scalar];
AbsTranslateOnly[a, scene, d, displacements];
};
alist: SliceList => {
FOR list:
LIST
OF Slice ← alist.list, list.rest
UNTIL list =
NIL
DO
ScaleEven[list.first, scene, scalar, a.coordSys];
Recursively scale subassemblies with respect to their own coordinate systems
ENDLOOP;
Now translate a's coordSys so that its displacement from the origin of cs is scaled by scalar
displacements ← CoordSys.FindTranslationOfAinTermsOfB[a.coordSys, d];
displacements ← SVVector3d.Scale[displacements, scalar];
AbsTranslateOnly[a, scene, d, displacements];
};
ENDCASE; -- IF a.shape = NIL or something else, do nothing.
};
AssemblyHasNoChildren: PUBLIC ERROR = CODE;
ScaleEvenChildren:
PUBLIC
PROC [a: Slice, scene: Scene, scalar:
REAL, d: CoordSystem ←
NIL] ~ {
WITH a.shape
SELECT
FROM
shape: Shape => ERROR AssemblyHasNoChildren;
alist: SliceList => {
FOR list:
LIST
OF Slice ← alist.list, list.rest
UNTIL list =
NIL
DO
ScaleEven[list.first, scene, scalar, list.first.coordSys];
ENDLOOP;
};
ENDCASE => ERROR AssemblyHasNoChildren; -- IF a.shape = NIL or something else, report a problem.
};
ScaleDistancesChildren:
PUBLIC
PROC [c: Slice, scene: Scene, sx, sy, sz:
REAL, d: CoordSystem ←
NIL] = {
For each child of a, find its current displacement from the center of cs. Scale this displacement by [sx, sy, sz].
displacements: Vector3d;
WITH c.shape
SELECT
FROM
shape: Shape => ERROR AssemblyHasNoChildren;
alist: SliceList => {
FOR list:
LIST
OF Slice ← alist.list, list.rest
UNTIL list =
NIL
DO
displacements ← CoordSys.FindTranslationOfAinTermsOfB[list.first.coordSys, d];
displacements[1] ← displacements[1]*sx;
displacements[2] ← displacements[2]*sy;
displacements[3] ← displacements[3]*sz;
AbsTranslateOnly[list.first, scene, d, displacements];
ENDLOOP;
};
ENDCASE => ERROR AssemblyHasNoChildren; -- Problem IF a.shape = NIL or something else.
};
Scale:
PUBLIC
PROC [c: Slice, scene: Scene, sx, sy, sz:
REAL, d: CoordSystem ←
NIL] = {
IF ISTYPE[c.shape, Shape] AND d = c.coordSys THEN SVAssembly.ScalePrimitive[c, sx, sy, sz]
ELSE ScaleEven[c, scene, sx, d];
};
MatrixHasScaling:
PRIVATE
PROC [mat: Matrix4by4]
RETURNS [
BOOL] = {
sx, sy, sz: REAL;
almostZero: REAL ← 1.0e-4;
[sx, sy, sz] ← Matrix3d.ScaleFromMatrix[mat];
IF ABS[sx - 1.0] > almostZero OR ABS[sy-1.0] > almostZero OR ABS[sz - 1.0] > almostZero
THEN RETURN[TRUE]
ELSE RETURN [FALSE];
};
END.