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