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 |
* xf.c - routines to convert transform arguments into 4X4 matrix. |
9 |
* |
10 |
* 1/28/86 |
11 |
*/ |
12 |
|
13 |
|
14 |
#define PI 3.14159265358979323846 |
15 |
#define d2r(a) ((PI/180.)*(a)) |
16 |
|
17 |
#define checkarg(a,n) if (av[i][a] || i+n >= ac) goto done |
18 |
|
19 |
|
20 |
int |
21 |
xf(retmat, retsca, ac, av) /* get transform specification */ |
22 |
double retmat[4][4]; |
23 |
double *retsca; |
24 |
int ac; |
25 |
char *av[]; |
26 |
{ |
27 |
double atof(), sin(), cos(); |
28 |
double xfmat[4][4], m4[4][4]; |
29 |
double xfsca, dtmp; |
30 |
int i, icnt; |
31 |
|
32 |
setident4(retmat); |
33 |
*retsca = 1.0; |
34 |
|
35 |
icnt = 1; |
36 |
setident4(xfmat); |
37 |
xfsca = 1.0; |
38 |
|
39 |
for (i = 0; i < ac && av[i][0] == '-'; i++) { |
40 |
|
41 |
setident4(m4); |
42 |
|
43 |
switch (av[i][1]) { |
44 |
|
45 |
case 't': /* translate */ |
46 |
checkarg(2,3); |
47 |
m4[3][0] = atof(av[++i]); |
48 |
m4[3][1] = atof(av[++i]); |
49 |
m4[3][2] = atof(av[++i]); |
50 |
break; |
51 |
|
52 |
case 'r': /* rotate */ |
53 |
switch (av[i][2]) { |
54 |
case 'x': |
55 |
checkarg(3,1); |
56 |
dtmp = d2r(atof(av[++i])); |
57 |
m4[1][1] = m4[2][2] = cos(dtmp); |
58 |
m4[2][1] = -(m4[1][2] = sin(dtmp)); |
59 |
break; |
60 |
case 'y': |
61 |
checkarg(3,1); |
62 |
dtmp = d2r(atof(av[++i])); |
63 |
m4[0][0] = m4[2][2] = cos(dtmp); |
64 |
m4[0][2] = -(m4[2][0] = sin(dtmp)); |
65 |
break; |
66 |
case 'z': |
67 |
checkarg(3,1); |
68 |
dtmp = d2r(atof(av[++i])); |
69 |
m4[0][0] = m4[1][1] = cos(dtmp); |
70 |
m4[1][0] = -(m4[0][1] = sin(dtmp)); |
71 |
break; |
72 |
default: |
73 |
return(i); |
74 |
} |
75 |
break; |
76 |
|
77 |
case 's': /* scale */ |
78 |
checkarg(2,1); |
79 |
dtmp = atof(av[i+1]); |
80 |
if (dtmp == 0.0) goto done; |
81 |
i++; |
82 |
xfsca *= |
83 |
m4[0][0] = |
84 |
m4[1][1] = |
85 |
m4[2][2] = dtmp; |
86 |
break; |
87 |
|
88 |
case 'm': /* mirror */ |
89 |
switch (av[i][2]) { |
90 |
case 'x': |
91 |
checkarg(3,0); |
92 |
xfsca *= |
93 |
m4[0][0] = -1.0; |
94 |
break; |
95 |
case 'y': |
96 |
checkarg(3,0); |
97 |
xfsca *= |
98 |
m4[1][1] = -1.0; |
99 |
break; |
100 |
case 'z': |
101 |
checkarg(3,0); |
102 |
xfsca *= |
103 |
m4[2][2] = -1.0; |
104 |
break; |
105 |
default: |
106 |
return(i); |
107 |
} |
108 |
break; |
109 |
|
110 |
case 'i': /* iterate */ |
111 |
checkarg(2,1); |
112 |
while (icnt-- > 0) { |
113 |
multmat4(retmat, retmat, xfmat); |
114 |
*retsca *= xfsca; |
115 |
} |
116 |
icnt = atoi(av[++i]); |
117 |
setident4(xfmat); |
118 |
xfsca = 1.0; |
119 |
continue; |
120 |
|
121 |
default: |
122 |
return(i); |
123 |
|
124 |
} |
125 |
multmat4(xfmat, xfmat, m4); |
126 |
} |
127 |
done: |
128 |
while (icnt-- > 0) { |
129 |
multmat4(retmat, retmat, xfmat); |
130 |
*retsca *= xfsca; |
131 |
} |
132 |
return(i); |
133 |
} |
134 |
|
135 |
|
136 |
#ifdef INVXF |
137 |
int |
138 |
invxf(retmat, retsca, ac, av) /* invert transform specification */ |
139 |
double retmat[4][4]; |
140 |
double *retsca; |
141 |
int ac; |
142 |
char *av[]; |
143 |
{ |
144 |
double atof(), sin(), cos(); |
145 |
double xfmat[4][4], m4[4][4]; |
146 |
double xfsca, dtmp; |
147 |
int i, icnt; |
148 |
|
149 |
setident4(retmat); |
150 |
*retsca = 1.0; |
151 |
|
152 |
icnt = 1; |
153 |
setident4(xfmat); |
154 |
xfsca = 1.0; |
155 |
|
156 |
for (i = 0; i < ac && av[i][0] == '-'; i++) { |
157 |
|
158 |
setident4(m4); |
159 |
|
160 |
switch (av[i][1]) { |
161 |
|
162 |
case 't': /* translate */ |
163 |
checkarg(2,3); |
164 |
m4[3][0] = -atof(av[++i]); |
165 |
m4[3][1] = -atof(av[++i]); |
166 |
m4[3][2] = -atof(av[++i]); |
167 |
break; |
168 |
|
169 |
case 'r': /* rotate */ |
170 |
switch (av[i][2]) { |
171 |
case 'x': |
172 |
checkarg(3,1); |
173 |
dtmp = -d2r(atof(av[++i])); |
174 |
m4[1][1] = m4[2][2] = cos(dtmp); |
175 |
m4[2][1] = -(m4[1][2] = sin(dtmp)); |
176 |
break; |
177 |
case 'y': |
178 |
checkarg(3,1); |
179 |
dtmp = -d2r(atof(av[++i])); |
180 |
m4[0][0] = m4[2][2] = cos(dtmp); |
181 |
m4[0][2] = -(m4[2][0] = sin(dtmp)); |
182 |
break; |
183 |
case 'z': |
184 |
checkarg(3,1); |
185 |
dtmp = -d2r(atof(av[++i])); |
186 |
m4[0][0] = m4[1][1] = cos(dtmp); |
187 |
m4[1][0] = -(m4[0][1] = sin(dtmp)); |
188 |
break; |
189 |
default: |
190 |
return(i); |
191 |
} |
192 |
break; |
193 |
|
194 |
case 's': /* scale */ |
195 |
checkarg(2,1); |
196 |
dtmp = atof(av[i+1]); |
197 |
if (dtmp == 0.0) goto done; |
198 |
i++; |
199 |
xfsca *= |
200 |
m4[0][0] = |
201 |
m4[1][1] = |
202 |
m4[2][2] = 1.0 / dtmp; |
203 |
break; |
204 |
|
205 |
case 'm': /* mirror */ |
206 |
switch (av[i][2]) { |
207 |
case 'x': |
208 |
checkarg(3,0); |
209 |
xfsca *= |
210 |
m4[0][0] = -1.0; |
211 |
break; |
212 |
case 'y': |
213 |
checkarg(3,0); |
214 |
xfsca *= |
215 |
m4[1][1] = -1.0; |
216 |
break; |
217 |
case 'z': |
218 |
checkarg(3,0); |
219 |
xfsca *= |
220 |
m4[2][2] = -1.0; |
221 |
break; |
222 |
default: |
223 |
return(i); |
224 |
} |
225 |
break; |
226 |
|
227 |
case 'i': /* iterate */ |
228 |
checkarg(2,1); |
229 |
while (icnt-- > 0) { |
230 |
multmat4(retmat, xfmat, retmat); |
231 |
*retsca *= xfsca; |
232 |
} |
233 |
icnt = atoi(av[++i]); |
234 |
setident4(xfmat); |
235 |
xfsca = 1.0; |
236 |
break; |
237 |
|
238 |
default: |
239 |
return(i); |
240 |
|
241 |
} |
242 |
multmat4(xfmat, m4, xfmat); /* left multiply */ |
243 |
} |
244 |
done: |
245 |
while (icnt-- > 0) { |
246 |
multmat4(retmat, xfmat, retmat); |
247 |
*retsca *= xfsca; |
248 |
} |
249 |
return(i); |
250 |
} |
251 |
#endif |