1 /* Copyright 1988, Brown Computer Graphics Group. All Rights Reserved. */
3 /* --------------------------------------------------------------------------
4 * This file contains routines that operate solely on matrices.
5 * -------------------------------------------------------------------------*/
9 /* -------------------------- Static Routines ---------------------------- */
11 #define SMALL 1e-20 /* Small enough to be considered zero */
14 * Shuffles rows in inverse of 3x3. See comment in MAT3_inv3_second_col().
18 MAT3_inv3_swap( register double inv[3][3], int row0, int row1, int row2)
20 register int i, tempi;
23 #define SWAP_ROWS(a, b) \
24 for (i = 0; i < 3; i++) SWAP(inv[a][i], inv[b][i], temp); \
29 SWAP_ROWS(row0, row1);
32 SWAP_ROWS(row0, row2);
37 SWAP_ROWS(row1, row2);
42 * Does Gaussian elimination on second column.
46 MAT3_inv3_second_col (register double source[3][3], register double inv[3][3], int row0)
48 register int row1, row2, i1, i2, i;
52 /* Find which row to use */
53 if (row0 == 0) i1 = 1, i2 = 2;
54 else if (row0 == 1) i1 = 0, i2 = 2;
57 /* Find which is larger in abs. val.:the entry in [i1][1] or [i2][1] */
58 /* and use that value for pivoting. */
60 a = source[i1][1]; if (a < 0) a = -a;
61 b = source[i2][1]; if (b < 0) b = -b;
64 row2 = (row1 == i1 ? i2 : i1);
66 /* Scale row1 in source */
67 if ((source[row1][1] < SMALL) && (source[row1][1] > -SMALL)) return(FALSE);
68 temp = 1.0 / source[row1][1];
69 source[row1][1] = 1.0;
70 source[row1][2] *= temp; /* source[row1][0] is zero already */
72 /* Scale row1 in inv */
73 inv[row1][row1] = temp; /* it used to be a 1.0 */
74 inv[row1][row0] *= temp;
76 /* Clear column one, source, and make corresponding changes in inv */
78 for (i = 0; i < 3; i++) if (i != row1) { /* for i = all rows but row1 */
81 source[i][2] += temp * source[row1][2];
83 inv[i][row1] = temp * inv[row1][row1];
84 inv[i][row0] += temp * inv[row1][row0];
87 /* Scale row2 in source */
88 if ((source[row2][2] < SMALL) && (source[row2][2] > -SMALL)) return(FALSE);
89 temp = 1.0 / source[row2][2];
90 source[row2][2] = 1.0; /* source[row2][*] is zero already */
92 /* Scale row2 in inv */
93 inv[row2][row2] = temp; /* it used to be a 1.0 */
94 inv[row2][row0] *= temp;
95 inv[row2][row1] *= temp;
97 /* Clear column one, source, and make corresponding changes in inv */
98 for (i = 0; i < 3; i++) if (i != row2) { /* for i = all rows but row2 */
101 inv[i][row0] += temp * inv[row2][row0];
102 inv[i][row1] += temp * inv[row2][row1];
103 inv[i][row2] += temp * inv[row2][row2];
107 * Now all is done except that the inverse needs to have its rows shuffled.
108 * row0 needs to be moved to inv[0][*], row1 to inv[1][*], etc.
110 * We *didn't* do the swapping before the elimination so that we could more
111 * easily keep track of what ops are needed to be done in the inverse.
113 MAT3_inv3_swap(inv, row0, row1, row2);
119 * Fast inversion routine for 3 x 3 matrices. - Written by jfh.
121 * This takes 30 multiplies/divides, as opposed to 39 for Cramer's Rule.
122 * The algorithm consists of performing fast gaussian elimination, by never
123 * doing any operations where the result is guaranteed to be zero, or where
124 * one operand is guaranteed to be zero. This is done at the cost of clarity,
127 * Returns 1 if the inverse was successful, 0 if it failed.
131 MAT3_invert3 (register double source[3][3], register double inv[3][3])
133 register int i, row0;
137 inv[0][0] = inv[1][1] = inv[2][2] = 1.0;
138 inv[0][1] = inv[0][2] = inv[1][0] = inv[1][2] = inv[2][0] = inv[2][1] = 0.0;
140 /* attempt to find the largest entry in first column to use as pivot */
141 a = source[0][0]; if (a < 0) a = -a;
142 b = source[1][0]; if (b < 0) b = -b;
143 c = source[2][0]; if (c < 0) c = -c;
154 /* Scale row0 of source */
155 if ((source[row0][0] < SMALL) && (source[row0][0] > -SMALL)) return(FALSE);
156 temp = 1.0 / source[row0][0];
157 source[row0][0] = 1.0;
158 source[row0][1] *= temp;
159 source[row0][2] *= temp;
161 /* Scale row0 of inverse */
162 inv[row0][row0] = temp; /* other entries are zero -- no effort */
164 /* Clear column zero of source, and make corresponding changes in inverse */
166 for (i = 0; i < 3; i++) if (i != row0) { /* for i = all rows but row0 */
167 temp = -source[i][0];
169 source[i][1] += temp * source[row0][1];
170 source[i][2] += temp * source[row0][2];
171 inv[i][row0] = temp * inv[row0][row0];
175 * We've now done gaussian elimination so that the source and
176 * inverse look like this:
182 * We now proceed to do elimination on the second column.
184 if (! MAT3_inv3_second_col(source, inv, row0)) return(FALSE);
190 * Finds a new pivot for a non-simple 4x4. See comments in MAT3invert().
194 MAT3_inv4_pivot (register MAT3mat src, MAT3vec r, double *s, int *swap)
201 if (MAT3_IS_ZERO(src[3][3])) {
203 /* Look for a different pivot element: one with largest abs value */
206 for (i = 0; i < 4; i++) {
207 if (src[i][3] > max) max = src[*swap = i][3];
208 else if (src[i][3] < -max) max = -src[*swap = i][3];
211 /* No pivot element available ! */
212 if (*swap < 0) return(FALSE);
214 else for (j = 0; j < 4; j++) SWAP(src[*swap][j], src[3][j], temp);
217 MAT3_SET_VEC (r, -src[0][3], -src[1][3], -src[2][3]);
219 *s = 1.0 / src[3][3];
221 src[0][3] = src[1][3] = src[2][3] = 0.0;
224 MAT3_SCALE_VEC(src[3], src[3], *s);
226 for (i = 0; i < 3; i++) {
227 src[0][i] += r[0] * src[3][i];
228 src[1][i] += r[1] * src[3][i];
229 src[2][i] += r[2] * src[3][i];
235 /* ------------------------- Internal Routines --------------------------- */
237 /* -------------------------- Public Routines ---------------------------- */
240 * This returns the inverse of the given matrix. The result matrix
241 * may be the same as the one to invert.
243 * Fast inversion routine for 4 x 4 matrices, written by jfh.
245 * Returns 1 if the inverse was successful, 0 if it failed.
247 * This routine has been specially tweaked to notice the following:
248 * If the matrix has the form
254 * (as do many matrices in graphics), then we compute the inverse of
255 * the upper left 3x3 matrix and use this to find the general inverse.
257 * In the event that the right column is not 0-0-0-1, we do gaussian
258 * elimination to make it so, then use the 3x3 inverse, and then do
259 * our gaussian elimination.
263 MAT3invert(result_mat, mat)
264 MAT3mat result_mat, mat;
267 register int i, j, simple;
268 double m[3][3], inv3[3][3], s, temp;
275 /* If last column is not (0,0,0,1), use special code */
276 simple = (mat[0][3] == 0.0 && mat[1][3] == 0.0 &&
277 mat[2][3] == 0.0 && mat[3][3] == 1.0);
279 if (! simple && ! MAT3_inv4_pivot(src, r, &s, &swap)) return(FALSE);
281 MAT3_COPY_VEC(t, src[3]); /* Translation vector */
283 /* Copy upper-left 3x3 matrix */
284 for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) m[i][j] = src[i][j];
286 if (! MAT3_invert3(m, inv3)) return(FALSE);
288 for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) inv[i][j] = inv3[i][j];
290 for (i = 0; i < 3; i++) for (j = 0; j < 3; j++)
291 inv[3][i] -= t[j] * inv3[j][i];
295 /* We still have to undo our gaussian elimination from earlier on */
296 /* add r0 * first col to last col */
297 /* add r1 * 2nd col to last col */
298 /* add r2 * 3rd col to last col */
300 for (i = 0; i < 4; i++) {
301 inv[i][3] += r[0] * inv[i][0] + r[1] * inv[i][1] + r[2] * inv[i][2];
306 for (i = 0; i < 4; i++) SWAP(inv[i][swap], inv[i][3], temp);
309 MAT3copy(result_mat, inv);