ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/mat4.c
Revision: 1.5
Committed: Sat Dec 15 15:01:38 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +14 -20 lines
Log Message:
changed handling of matrix transformations with new MAT4 & XF types

File Contents

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