ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/mat4.c
Revision: 1.3
Committed: Fri Dec 22 09:24:07 1989 UTC (34 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +3 -6 lines
Log Message:
got rid of loop in multv3()

File Contents

# Content
1 /* Copyright (c) 1986 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * mat4.c - routines dealing with 4 X 4 homogeneous transformation matrices.
9 *
10 * 10/19/85
11 */
12
13
14 static double m4tmp[4][4]; /* for efficiency */
15
16 #define copymat4(m4a,m4b) bcopy((char *)m4b,(char *)m4a,sizeof(m4tmp))
17
18
19 setident4(m4)
20 double m4[4][4];
21 {
22 static double ident[4][4] = {
23 1.,0.,0.,0.,
24 0.,1.,0.,0.,
25 0.,0.,1.,0.,
26 0.,0.,0.,1.,
27 };
28 copymat4(m4, ident);
29 }
30
31
32 multmat4(m4a, m4b, m4c) /* multiply m4b X m4c and put into m4a */
33 double m4a[4][4];
34 register double m4b[4][4], m4c[4][4];
35 {
36 register int i, j;
37
38 for (i = 4; i--; )
39 for (j = 4; j--; )
40 m4tmp[i][j] = m4b[i][0]*m4c[0][j] +
41 m4b[i][1]*m4c[1][j] +
42 m4b[i][2]*m4c[2][j] +
43 m4b[i][3]*m4c[3][j];
44
45 copymat4(m4a, m4tmp);
46 }
47
48
49 multv3(v3a, v3b, m4) /* transform vector v3b by m4 and put into v3a */
50 double v3a[3];
51 register double v3b[3];
52 register double m4[4][4];
53 {
54 m4tmp[0][0] = v3b[0]*m4[0][0] + v3b[1]*m4[1][0] + v3b[2]*m4[2][0];
55 m4tmp[0][1] = v3b[0]*m4[0][1] + v3b[1]*m4[1][1] + v3b[2]*m4[2][1];
56 m4tmp[0][2] = v3b[0]*m4[0][2] + v3b[1]*m4[1][2] + v3b[2]*m4[2][2];
57
58 v3a[0] = m4tmp[0][0];
59 v3a[1] = m4tmp[0][1];
60 v3a[2] = m4tmp[0][2];
61 }
62
63
64 multp3(p3a, p3b, m4) /* transform p3b by m4 and put into p3a */
65 register double p3a[3];
66 double p3b[3];
67 register double m4[4][4];
68 {
69 multv3(p3a, p3b, m4); /* transform as vector */
70 p3a[0] += m4[3][0]; /* translate */
71 p3a[1] += m4[3][1];
72 p3a[2] += m4[3][2];
73 }
74
75
76 #ifdef INVMAT
77 /*
78 * invmat - computes the inverse of mat into inverse. Returns 1
79 * if there exists an inverse, 0 otherwise. It uses Gaussian Elimination
80 * method with partial pivoting.
81 */
82
83 invmat(inverse,mat)
84 double mat[4][4],inverse[4][4];
85 {
86 #define SWAP(a,b,t) (t=a,a=b,b=t)
87 #define ABS(x) (x>=0?x:-(x))
88
89 register int i,j,k;
90 register double temp;
91
92 copymat4(m4tmp, mat);
93 setident(inverse);
94
95 for(i = 0; i < 4; i++) {
96 /* Look for row with largest pivot and swap rows */
97 temp = 0; j = -1;
98 for(k = i; k < 4; k++)
99 if(ABS(m4tmp[k][i]) > temp) {
100 temp = ABS(m4tmp[k][i]);
101 j = k;
102 }
103 if(j == -1) /* No replacing row -> no inverse */
104 return(0);
105 if (j != i)
106 for(k = 0; k < 4; k++) {
107 SWAP(m4tmp[i][k],m4tmp[j][k],temp);
108 SWAP(inverse[i][k],inverse[j][k],temp);
109 }
110
111 temp = m4tmp[i][i];
112 for(k = 0; k < 4; k++) {
113 m4tmp[i][k] /= temp;
114 inverse[i][k] /= temp;
115 }
116 for(j = 0; j < 4; j++) {
117 if(j != i) {
118 temp = m4tmp[j][i];
119 for(k = 0; k < 4; k++) {
120 m4tmp[j][k] -= m4tmp[i][k]*temp;
121 inverse[j][k] -= inverse[i][k]*temp;
122 }
123 }
124 }
125 }
126 return(1);
127
128 #undef ABS
129 #undef SWAP
130 }
131 #endif