ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/invmat4.c
Revision: 2.1
Committed: Tue Apr 12 15:20:48 1994 UTC (30 years ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1994 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * invmat4 - computes the inverse of mat into inverse. Returns 1
9     * if there exists an inverse, 0 otherwise. It uses Gaussian Elimination
10     * method.
11     */
12    
13     #include "mat4.h"
14    
15    
16     #define ABS(x) ((x)>=0 ? (x) : -(x))
17    
18     #define SWAP(a,b,t) (t=a,a=b,b=t)
19    
20    
21     invmat4(inverse,mat)
22     MAT4 inverse, mat;
23     {
24     MAT4 m4tmp;
25     register int i,j,k;
26     register double temp;
27    
28     copymat4(m4tmp, mat);
29     /* set inverse to identity */
30     for (i = 0; i < 4; i++)
31     for (j = 0; j < 4; j++)
32     inverse[i][j] = i==j ? 1.0 : 0.0;
33    
34     for(i = 0; i < 4; i++) {
35     /* Look for row with largest pivot and swap rows */
36     temp = FTINY; j = -1;
37     for(k = i; k < 4; k++)
38     if(ABS(m4tmp[k][i]) > temp) {
39     temp = ABS(m4tmp[k][i]);
40     j = k;
41     }
42     if(j == -1) /* No replacing row -> no inverse */
43     return(0);
44     if (j != i)
45     for(k = 0; k < 4; k++) {
46     SWAP(m4tmp[i][k],m4tmp[j][k],temp);
47     SWAP(inverse[i][k],inverse[j][k],temp);
48     }
49    
50     temp = m4tmp[i][i];
51     for(k = 0; k < 4; k++) {
52     m4tmp[i][k] /= temp;
53     inverse[i][k] /= temp;
54     }
55     for(j = 0; j < 4; j++) {
56     if(j != i) {
57     temp = m4tmp[j][i];
58     for(k = 0; k < 4; k++) {
59     m4tmp[j][k] -= m4tmp[i][k]*temp;
60     inverse[j][k] -= inverse[i][k]*temp;
61     }
62     }
63     }
64     }
65     return(1);
66     }