SilverScreen Solid Modeler

Transformation Matrices

Transformation Matrices

Previous topic Next topic  

Transformation Matrices

Previous topic Next topic JavaScript is required for the print function  

SilverScreenAPI

 

Transformation Matrices

 

 

 


Introduction

In SilverScreen, a transformation matrix is a 4x4 array of doubles. One can view a transformation matrix as a function that maps points from one space to another space, typically representing some combination of the scaling, rotation and translation operations. For a point (x1,y1,z1), and a transformation matrix T, the mapping to (x2,y2,z2) is defined by the matrix multiplication

[x2 y2 z2 1] = T . [x1 y1 z1 1]

 

Transformation matrices may be multiplied together to produce composite transformations. For example, if the matrix T maps (x1,y1,z1) to (x2,y2,z2), and if the matrix S maps (x2,y2,z2) to (x3,y3,z3), then the matrix product T . S will map (x1,y1,z1) to (x3,y3,z3).

 

Most commonly, matrices are multiplied to produce composite transformations. The chain of multiplications begins with the unity matrix, a matrix that has the value one in each diagonal element, and the value zero in all other elements. This matrix, if used to transform the point (x1,y1,z1), will map the point onto itself. Thus the unity matrix is, in effect, a null transformation.

 


Example

A typical operation performed with matrices is the following:

 

A polygon with points p1, p2, p3 is to be scaled to one-half its size by scaling about the point p1. Here we assume p1 = (x1,y1,z1), p2 = (x2,y2,z2), and p3 = (x3,y3,z3).

 

1.

Initialize the matrix T to be the unity matrix.

2.

Construct a translation matrix S that translates all points through a displacement (-x1,-y1,-z1). The effect of this transformation is to map the point p1 to (0,0,0), and to map the other points to (x2 - x1,y2 - y1,z2 - z1) and (x3 - x1,y3 - y1,z3 - z1).

3.

Multiply the matrix T by the matrix S.

4.

Construct a new translation matrix S that scales all points by a factor of 1/2.

5.

Multiply the matrix T by the matrix S.

6.

Construct a new translation matrix S that translates all points through a displacement (x1,y1,z1). This transformation effectively moves the points back to their original location.

7.

Multiply the matrix T by the matrix S.

 

The result of these operations is a composite transformation matrix that will scale all points in the polygon about the point p1.

 

 


Translation, Rotation, Scaling

There are three types of transformation matrices that are explicitly supported by SilverScreen API functions: translation, rotation, and scaling. Each of these functions performs two operations: First, a transformation matrix is created; second, the transformation matrix is multiplied into a composite matrix.

The functions that perform these operations are:

 

  translation

tm_translate

  scaling

tm_scale_x , tm_scale_y , and tm_scale_z

  rotation

tm_rotate_x , tm_rotate_y , and tm_rotate_z

 

The scaling and rotation functions are designed so that rotation and scaling can be performed independently on each axis.

 

Here is the C code that will produce the composite transformation matrix described in steps 1 through 7 above.

 

C / C++ Code

 

 MATRIX t;

 SS_XYZ p1,p2,p3;

 SS_XYZ q1,q2,q3;

 SS_XYZ tmp;

 

 tm_clear (t);              // Initialize t to the unity matrix

 

 tmp.x = -p1.x;

 tmp.y = -p1.y;

 tmp.z = -p1.z;

 

 tm_translate ( &tmp, t );  // Translate (-p1.x,-p1.y,-p1.x)

 

 scale_x( 0.5, t );        // Scaling by 0.5  

 scale_y( 0.5, t );

 scale_z( 0.5, t );

 

 tm_translate ( &p1, t );        // Translate (p1.x,p1.y,p1.x)

 

 // After these statements have been executed, the transformation
 // matrix can be used to transform the points to their new
 // locations. This is done by the function tm_transform. Here we

 // can transform p1, p2, and p3 to q1,q2, and q3 by:

 

 tm_transform ( &p1, &q1, t );

 tm_transform ( &p2, &q2, t );

 tm_transform ( &p3, &q3, t );

 

 

 

 


TM_CSPACE, TM_INVERSE, TM_TRANSFORM

In addition to the simple transformation functions described above, the SilverScreen API contains a number of specialized transformation tools.

 

The function tm_cspace will produce a transformation matrix that maps points from world space to the current construction space. The following will transform a w-space point p to a c-space point q:

 

C / C++ Code

 

 tm_cspace ( tm );

 tm_transform ( &p, &q, tm );

 

 

To transform a c-space point back to w-space, the function tm_inverse is used. The inverse function produces a matrix which maps in the opposite direction of the original matrix. The following will first get the w-space to c-space matrix tm . Then, tm_inverse is used to get the c-space to w-space matrix tm_inv . The final statement transforms the c-space point p1 to the w-space point p2.

 

C / C++ Code

 

 tm_cspace ( tm );

 tm_inverse ( tm, tm_inv );

 tm_transform ( &p1, &p2, tm_inv );

 

 

For a more concrete example, consider the following problem. For a point p , we wish to locate a point q that is 2 units from p in the direction of the positive y-axis of c-space. Although this description may sound complicated, this operation is, in fact, fairly common in geometric programming. The solution is:

 

C / C++ Code

 

 MATRIX tm,tm_inv;

 SS_XYZ p,pp,q;

 

 tm_cspace ( tm );

 tm_inverse ( tm, tm_inv );

 tm_transform (&p,&pp,tm);               // result: pp in c-space

 pp.y += 2.0;                           // add c-space displacement

 tm_transform ( &pp, &q, tm_inv );       // result: q in w-space

 

 

 

 


W_TO_C and C_TO_W

The function w_to_c implicitly uses the w-space to c-space transformation matrix to transform a point from w-space to c-space. For a point p in w-space, the following produces the corresponding point q in c-space:

 

C / C++ Code

 

 w_to_c ( &p, &q );

 

 

 

This is exactly equivalent to:

 

C / C++ Code

 

 tm_cspace ( tm );

 tm_transform ( &p, &q, tm );

 

 

There is also an inverse function c_to_w that transforms a c-space point to w-space. The following function makes use of w_to_c and c_to_w to move the cursor on the xy-plane of c-space:

 

C / C++ Code

 

 SS_XYZ wxyz,cxyz;

 SS_XYZ new_position;

 double zspace;

 

 cv_set ( CV_VERY_QUIET, 1 );

 // Get initial location of cursor

 wxyz.x = worldx;

 wxyz.y = worldy;

 wxyz.z = worldz;

 

 // Transform wxyz in w-space to cxyz in c-space

 w_to_c ( &wxyz, &cxyz );

 

 // Move location to xy-plane of c-space

 cxyz.z = 0.0;

 

 zspace = snap_spacing();

 for ( ; ; )

    {

    switch ( inchar () )

       {

       case LARROW: cxyz.x -= zspace; break;

       case RARROW: cxyz.x += zspace; break;

       case UARROW: cxyz.y += zspace; break;

       case DARROW: cxyz.y -= zspace; break;

       case ESCAPE: return;

       default:     continue;

       }

 

    // Find corresponding w-space location

    c_to_w (&cxyz, &new_position);

    // Reposition the cursor

    ss_command ( "at %z", &new_position );

    }

 

 

 

 


TM_TO_2D

The function tm_to_2d will produce a transformation matrix that maps three non-collinear points to the xy-plane. For the points p1 , p2 , and p3 , the tm_to_2d transformation will map p1 to the origin, p2 to a point on the positive x-axis, and p3 to a point on xy-plane with a positive y-coordinate. This function provides a powerful tool for solving 3D problems in 2D.

 

Suppose, for example, that the points p1 , p2 , and p3 form a triangle somewhere in 3D space. To compute the area of this triangle, we can do the following:

 

C / C++ Code

 

 SS_XYZ p1, p2, p3;

 SS_XYZ q2, q3;

 double area;

 

 tm_to_2d (&p1, &p2, &p3, tm );

 tm_transform ( &p2, &q2, tm );

 tm_transform ( &p3, &q3, tm );

 area = 0.5 * q2.x * q3.y;

 

 

 

 


For an excellent treatment of the use of transformation matrices, see the Foley, van Dam, Feiner, and Hughes book.