1 /* #include "HEADERS.h" */
2 /* Copyright 1988, Brown Computer Graphics Group. All Rights Reserved. */
4 /* --------------------------------------------------------------------------
5 * This file contains routines that operate solely on matrices.
6 * -------------------------------------------------------------------------*/
9 #include <Math/mat3defs.h>
12 # include <memory.h> /* required for memset() and memcpy() */
14 # include <string.h> /* MetroWerks CodeWarrior: memset() & memcpy() */
18 /* #include "macros.h" */
20 /* -------------------------- Static Routines ---------------------------- */
22 /* ------------------------- Internal Routines --------------------------- */
24 /* -------------------------- Public Routines ---------------------------- */
28 * Sets a matrix to identity.
32 MAT3identity (register MAT3mat mat)
37 memset(mat,0x00, sizeof(MAT3mat));
39 bzero (mat, sizeof(MAT3mat));
42 for (i = 0; i < 4; i++)
47 * Sets a matrix to zero.
51 MAT3zero (MAT3mat mat)
54 memset(mat,0x00, sizeof(MAT3mat));
56 bzero (mat, sizeof(MAT3mat));
62 * Copies one matrix to another.
66 MAT3copy(MAT3mat to, MAT3mat from)
69 memcpy(to, from, sizeof(MAT3mat));
71 bcopy(from, to, sizeof(MAT3mat));
76 * This multiplies two matrices, producing a third, which may the same as
77 * either of the first two.
81 MAT3mult (double (*result_mat)[4], register double (*mat1)[4], register double (*mat2)[4])
86 for (i = 0; i < 4; i++)
87 for (j = 0; j < 4; j++)
88 tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] +
89 mat1[i][1] * mat2[1][j] +
90 mat1[i][2] * mat2[2][j] +
91 mat1[i][3] * mat2[3][j]);
92 MAT3copy (result_mat, tmp_mat);
96 * This returns the transpose of a matrix. The result matrix may be
97 * the same as the one to transpose.
101 MAT3transpose (double (*result_mat)[4], register double (*mat)[4])
106 for (i = 0; i < 4; i++)
107 for (j = 0; j < 4; j++)
108 tmp_mat[i][j] = mat[j][i];
110 MAT3copy (result_mat, tmp_mat);
115 * This prints the given matrix to the given file pointer.
119 MAT3print(double (*mat)[4], FILE *fp)
121 MAT3print_formatted(mat, fp, CNULL, CNULL, CNULL, CNULL);
125 * This prints the given matrix to the given file pointer.
126 * use the format string to pass to fprintf. head and tail
127 * are printed at the beginning and end of each line.
131 MAT3print_formatted(double (*mat)[4], FILE *fp, char *title, char *head, char *format, char *tail)
135 /* This is to allow this to be called easily from a debugger */
136 if (fp == NULL) fp = stderr;
138 if (title == NULL) title = "MAT3 matrix:\n";
139 if (head == NULL) head = " ";
140 if (format == NULL) format = "%#8.4lf ";
141 if (tail == NULL) tail = "\n";
143 (void) fprintf(fp, title);
145 for (i = 0; i < 4; i++) {
146 (void) fprintf(fp, head);
147 for (j = 0; j < 4; j++) (void) fprintf(fp, format, mat[i][j]);
148 (void) fprintf(fp, tail);