1 |
– |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines for 4x4 homogeneous, rigid-body transformations |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
|
#include <math.h> |
10 |
+ |
#include <stdlib.h> |
11 |
|
#include <string.h> |
12 |
|
#include "parser.h" |
13 |
|
|
19 |
|
|
20 |
|
static MAT4 m4tmp; /* for efficiency */ |
21 |
|
|
22 |
< |
int xf_argc; /* total # transform args. */ |
23 |
< |
char **xf_argv; /* transform arguments */ |
24 |
< |
XF_SPEC *xf_context; /* current context */ |
22 |
> |
XF_SPEC *xf_context = NULL; /* current context */ |
23 |
> |
char **xf_argend; /* end of transform argument list */ |
24 |
> |
static char **xf_argbeg; /* beginning of transform argument list */ |
25 |
|
|
28 |
– |
static int xf_maxarg; /* # allocated arguments */ |
26 |
|
|
30 |
– |
static XF_SPEC *new_xf(); |
31 |
– |
static long comp_xfid(); |
32 |
– |
static int put_oname(); |
33 |
– |
|
34 |
– |
|
27 |
|
int |
28 |
|
xf_handler(ac, av) /* handle xf entity */ |
29 |
|
int ac; |
32 |
|
register XF_SPEC *spec; |
33 |
|
register int n; |
34 |
|
int rv; |
43 |
– |
XF thisxf; |
35 |
|
|
36 |
|
if (ac == 1) { /* something with existing transform */ |
37 |
|
if ((spec = xf_context) == NULL) |
38 |
< |
return(MG_OK); /* should be error? */ |
38 |
> |
return(MG_ECNTXT); |
39 |
|
n = -1; |
40 |
|
if (spec->xarr != NULL) { /* check for iteration */ |
41 |
|
register struct xf_array *ap = spec->xarr; |
42 |
|
|
43 |
< |
(void)put_oname((struct xf_array *)NULL); |
43 |
> |
(void)xf_aname((struct xf_array *)NULL); |
44 |
|
n = ap->ndim; |
45 |
|
while (n--) { |
46 |
|
if (++ap->aarg[n].i < ap->aarg[n].n) |
52 |
|
if ((rv = mg_fgoto(&ap->spos)) != MG_OK) |
53 |
|
return(rv); |
54 |
|
sprintf(ap->aarg[n].arg, "%d", ap->aarg[n].i); |
55 |
< |
(void)put_oname(ap); |
56 |
< |
} else |
66 |
< |
free((MEM_PTR)ap); |
55 |
> |
(void)xf_aname(ap); |
56 |
> |
} |
57 |
|
} |
58 |
|
if (n < 0) { /* pop transform */ |
69 |
– |
xf_argv[xf_argc=spec->xav0] = NULL; |
59 |
|
xf_context = spec->prev; |
60 |
< |
free((MEM_PTR)spec); |
60 |
> |
free_xf(spec); |
61 |
|
return(MG_OK); |
62 |
|
} |
63 |
|
} else { /* else allocate transform */ |
64 |
|
if ((spec = new_xf(ac-1, av+1)) == NULL) |
65 |
|
return(MG_EMEM); |
66 |
+ |
if (spec->xarr != NULL) |
67 |
+ |
(void)xf_aname(spec->xarr); |
68 |
|
spec->prev = xf_context; /* push onto stack */ |
69 |
|
xf_context = spec; |
70 |
|
} |
71 |
|
/* translate new specification */ |
72 |
< |
if (xf(&thisxf, spec->xac, &xf_argv[spec->xav0]) != spec->xac) |
72 |
> |
n = xf_ac(spec); |
73 |
> |
n -= xf_ac(spec->prev); /* incremental comp. is more eff. */ |
74 |
> |
if (xf(&spec->xf, n, xf_av(spec)) != n) |
75 |
|
return(MG_ETYPE); |
76 |
|
/* check for vertex reversal */ |
77 |
< |
if ((spec->rev = (thisxf.sca < 0.))) |
78 |
< |
thisxf.sca = -thisxf.sca; |
77 |
> |
if ((spec->rev = (spec->xf.sca < 0.))) |
78 |
> |
spec->xf.sca = -spec->xf.sca; |
79 |
|
/* compute total transformation */ |
80 |
|
if (spec->prev != NULL) { |
81 |
< |
multmat4(spec->xf.xfm, thisxf.xfm, spec->prev->xf.xfm); |
82 |
< |
spec->xf.sca = thisxf.sca * spec->prev->xf.sca; |
81 |
> |
multmat4(spec->xf.xfm, spec->xf.xfm, spec->prev->xf.xfm); |
82 |
> |
spec->xf.sca *= spec->prev->xf.sca; |
83 |
|
spec->rev ^= spec->prev->rev; |
84 |
< |
} else |
92 |
< |
spec->xf = thisxf; |
84 |
> |
} |
85 |
|
spec->xid = comp_xfid(spec->xf.xfm); /* compute unique ID */ |
86 |
|
return(MG_OK); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
< |
static XF_SPEC * |
90 |
> |
XF_SPEC * |
91 |
|
new_xf(ac, av) /* allocate new transform structure */ |
92 |
|
int ac; |
93 |
|
char **av; |
118 |
|
spec->xarr->ndim = 0; /* incremented below */ |
119 |
|
} else |
120 |
|
spec->xarr = NULL; |
121 |
< |
spec->xav0 = xf_argc; |
130 |
< |
spec->xac = ac; |
121 |
> |
spec->xac = ac + xf_argc; |
122 |
|
/* and store new xf arguments */ |
123 |
< |
if (xf_argc+ac+1 > xf_maxarg) { |
124 |
< |
if (!xf_maxarg) |
125 |
< |
xf_argv = (char **)malloc( |
126 |
< |
(xf_maxarg=ac+1)*sizeof(char *)); |
136 |
< |
else |
137 |
< |
xf_argv = (char **)realloc((MEM_PTR)xf_argv, |
138 |
< |
(xf_maxarg=xf_argc+ac+1)*sizeof(char *)); |
139 |
< |
if (xf_argv == NULL) |
123 |
> |
if (xf_argbeg == NULL || xf_av(spec) < xf_argbeg) { |
124 |
> |
register char **newav = |
125 |
> |
(char **)malloc((spec->xac+1)*sizeof(char *)); |
126 |
> |
if (newav == NULL) |
127 |
|
return(NULL); |
128 |
+ |
for (i = xf_argc; i-- > 0; ) |
129 |
+ |
newav[ac+i] = xf_argend[i-xf_context->xac]; |
130 |
+ |
*(xf_argend = newav + spec->xac) = NULL; |
131 |
+ |
if (xf_argbeg != NULL) |
132 |
+ |
free((MEM_PTR)xf_argbeg); |
133 |
+ |
xf_argbeg = newav; |
134 |
|
} |
135 |
|
cp = (char *)(spec + 1); /* use memory allocated above */ |
136 |
|
for (i = 0; i < ac; i++) |
137 |
|
if (!strcmp(av[i], "-a")) { |
138 |
< |
xf_argv[xf_argc++] = "-i"; |
139 |
< |
xf_argv[xf_argc++] = strcpy( |
138 |
> |
xf_av(spec)[i++] = "-i"; |
139 |
> |
xf_av(spec)[i] = strcpy( |
140 |
|
spec->xarr->aarg[spec->xarr->ndim].arg, |
141 |
|
"0"); |
142 |
|
spec->xarr->aarg[spec->xarr->ndim].i = 0; |
143 |
< |
spec->xarr->aarg[spec->xarr->ndim++].n = atoi(av[++i]); |
143 |
> |
spec->xarr->aarg[spec->xarr->ndim++].n = atoi(av[i]); |
144 |
|
} else { |
145 |
< |
xf_argv[xf_argc++] = strcpy(cp, av[i]); |
145 |
> |
xf_av(spec)[i] = strcpy(cp, av[i]); |
146 |
|
cp += strlen(av[i]) + 1; |
147 |
|
} |
155 |
– |
xf_argv[xf_argc] = NULL; |
156 |
– |
if (spec->xarr != NULL) |
157 |
– |
(void)put_oname(spec->xarr); |
148 |
|
return(spec); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
< |
static int |
153 |
< |
put_oname(ap) /* put out name for this instance */ |
152 |
> |
void |
153 |
> |
free_xf(spec) /* free a transform */ |
154 |
> |
register XF_SPEC *spec; |
155 |
> |
{ |
156 |
> |
if (spec->xarr != NULL) |
157 |
> |
free((MEM_PTR)spec->xarr); |
158 |
> |
free((MEM_PTR)spec); |
159 |
> |
} |
160 |
> |
|
161 |
> |
|
162 |
> |
int |
163 |
> |
xf_aname(ap) /* put out name for this instance */ |
164 |
|
register struct xf_array *ap; |
165 |
|
{ |
166 |
|
static char oname[10*XF_MAXDIM]; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
< |
static long |
185 |
> |
long |
186 |
|
comp_xfid(xfm) /* compute unique ID from matrix */ |
187 |
|
register MAT4 xfm; |
188 |
|
{ |
208 |
|
{ |
209 |
|
register XF_SPEC *spec; |
210 |
|
|
211 |
< |
if (xf_maxarg) { |
212 |
< |
free((MEM_PTR)xf_argv); |
213 |
< |
xf_argv = NULL; |
214 |
< |
xf_maxarg = 0; |
211 |
> |
if (xf_argbeg != NULL) { |
212 |
> |
free((MEM_PTR)xf_argbeg); |
213 |
> |
xf_argbeg = xf_argend = NULL; |
214 |
|
} |
216 |
– |
xf_argc = 0; |
215 |
|
while ((spec = xf_context) != NULL) { |
216 |
|
xf_context = spec->prev; |
217 |
< |
if (spec->xarr != NULL) |
220 |
< |
free((MEM_PTR)spec->xarr); |
221 |
< |
free((MEM_PTR)spec); |
217 |
> |
free_xf(spec); |
218 |
|
} |
219 |
|
} |
220 |
|
|
224 |
|
FVECT v1, v2; |
225 |
|
{ |
226 |
|
if (xf_context == NULL) { |
227 |
< |
v1[0] = v2[0]; |
232 |
< |
v1[1] = v2[1]; |
233 |
< |
v1[2] = v2[2]; |
227 |
> |
VCOPY(v1, v2); |
228 |
|
return; |
229 |
|
} |
230 |
|
multp3(v1, v2, xf_context->xf.xfm); |
236 |
|
FVECT v1, v2; |
237 |
|
{ |
238 |
|
if (xf_context == NULL) { |
239 |
< |
v1[0] = v2[0]; |
246 |
< |
v1[1] = v2[1]; |
247 |
< |
v1[2] = v2[2]; |
239 |
> |
VCOPY(v1, v2); |
240 |
|
return; |
241 |
|
} |
242 |
|
multv3(v1, v2, xf_context->xf.xfm); |