8 |
|
#include "copyright.h" |
9 |
|
|
10 |
|
#include "ray.h" |
11 |
< |
|
11 |
> |
#include "paths.h" |
12 |
|
#include "otypes.h" |
13 |
– |
|
13 |
|
#include "func.h" |
14 |
+ |
#include <ctype.h> |
15 |
|
|
16 |
|
|
17 |
|
#define INITFILE "rayinit.cal" |
31 |
|
static OBJREC *fobj = NULL; /* current function object */ |
32 |
|
static RAY *fray = NULL; /* current function ray */ |
33 |
|
|
34 |
< |
static double l_erf(), l_erfc(), l_arg(); |
34 |
> |
static char rayinitcal[] = INITFILE; |
35 |
|
|
36 |
+ |
static double l_erf(char *), l_erfc(char *), l_arg(char *); |
37 |
|
|
38 |
+ |
|
39 |
+ |
void |
40 |
+ |
initfunc() /* initialize function evaluation */ |
41 |
+ |
{ |
42 |
+ |
if (!rayinitcal[0]) /* already done? */ |
43 |
+ |
return; |
44 |
+ |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; |
45 |
+ |
esupport &= ~(E_OUTCHAN); |
46 |
+ |
setcontext(""); |
47 |
+ |
scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0); |
48 |
+ |
scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0); |
49 |
+ |
scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0); |
50 |
+ |
scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0); |
51 |
+ |
scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0); |
52 |
+ |
scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0); |
53 |
+ |
scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0); |
54 |
+ |
scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0); |
55 |
+ |
scompile("Lu=$26;Lv=$27;", NULL, 0); |
56 |
+ |
funset("arg", 1, '=', l_arg); |
57 |
+ |
funset("erf", 1, ':', l_erf); |
58 |
+ |
funset("erfc", 1, ':', l_erfc); |
59 |
+ |
setnoisefuncs(); |
60 |
+ |
setprismfuncs(); |
61 |
+ |
loadfunc(rayinitcal); |
62 |
+ |
rayinitcal[0] = '\0'; |
63 |
+ |
} |
64 |
+ |
|
65 |
+ |
|
66 |
+ |
/* Set parameters for current evaluation */ |
67 |
+ |
void |
68 |
+ |
set_eparams(char *prms) |
69 |
+ |
{ |
70 |
+ |
static char *last_params = NULL; |
71 |
+ |
char vname[RMAXWORD]; |
72 |
+ |
double value; |
73 |
+ |
char *cpd; |
74 |
+ |
/* check if already set */ |
75 |
+ |
if ((prms == NULL) | (prms == last_params)) |
76 |
+ |
return; |
77 |
+ |
if (last_params != NULL && !strcmp(prms, last_params)) |
78 |
+ |
return; |
79 |
+ |
last_params = prms; /* assign each variable */ |
80 |
+ |
while (*prms) { |
81 |
+ |
if (isspace(*prms)) { |
82 |
+ |
++prms; continue; |
83 |
+ |
} |
84 |
+ |
if (!isalpha(*prms)) |
85 |
+ |
goto bad_params; |
86 |
+ |
cpd = vname; |
87 |
+ |
while (*prms && (*prms != '=') & !isspace(*prms)) { |
88 |
+ |
if (!isid(*prms)) |
89 |
+ |
goto bad_params; |
90 |
+ |
*cpd++ = *prms++; |
91 |
+ |
} |
92 |
+ |
if (cpd == vname) |
93 |
+ |
goto bad_params; |
94 |
+ |
*cpd = '\0'; |
95 |
+ |
while (isspace(*prms)) prms++; |
96 |
+ |
if (*prms++ != '=') |
97 |
+ |
goto bad_params; |
98 |
+ |
value = atof(prms); |
99 |
+ |
if ((prms = fskip(prms)) == NULL) |
100 |
+ |
goto bad_params; |
101 |
+ |
while (isspace(*prms)) prms++; |
102 |
+ |
prms += (*prms == ',') | (*prms == ';'); |
103 |
+ |
varset(vname, '=', value); |
104 |
+ |
} |
105 |
+ |
return; |
106 |
+ |
bad_params: |
107 |
+ |
sprintf(errmsg, "bad parameter list '%s'", last_params); |
108 |
+ |
error(USER, errmsg); |
109 |
+ |
} |
110 |
+ |
|
111 |
+ |
|
112 |
|
MFUNC * |
113 |
< |
getfunc(m, ff, ef, dofwd) /* get function for this modifier */ |
114 |
< |
OBJREC *m; |
115 |
< |
int ff; |
116 |
< |
unsigned int ef; |
117 |
< |
int dofwd; |
113 |
> |
getfunc( /* get function for this modifier */ |
114 |
> |
OBJREC *m, |
115 |
> |
int ff, |
116 |
> |
unsigned int ef, |
117 |
> |
int dofwd |
118 |
> |
) |
119 |
|
{ |
44 |
– |
static char initfile[] = INITFILE; |
120 |
|
char sbuf[MAXSTR]; |
121 |
< |
register char **arg; |
122 |
< |
register MFUNC *f; |
121 |
> |
char **arg; |
122 |
> |
MFUNC *f; |
123 |
|
int ne, na; |
124 |
< |
register int i; |
124 |
> |
int i; |
125 |
|
/* check to see if done already */ |
126 |
|
if ((f = (MFUNC *)m->os) != NULL) |
127 |
|
return(f); |
128 |
|
fobj = NULL; fray = NULL; |
129 |
< |
if (initfile[0]) { /* initialize on first call */ |
130 |
< |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; |
56 |
< |
esupport &= ~(E_OUTCHAN); |
57 |
< |
setcontext(""); |
58 |
< |
scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0); |
59 |
< |
scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0); |
60 |
< |
scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0); |
61 |
< |
scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0); |
62 |
< |
scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0); |
63 |
< |
scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0); |
64 |
< |
scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0); |
65 |
< |
scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0); |
66 |
< |
scompile("Lu=$26;Lv=$27;", NULL, 0); |
67 |
< |
funset("arg", 1, '=', l_arg); |
68 |
< |
funset("erf", 1, ':', l_erf); |
69 |
< |
funset("erfc", 1, ':', l_erfc); |
70 |
< |
setnoisefuncs(); |
71 |
< |
setprismfuncs(); |
72 |
< |
loadfunc(initfile); |
73 |
< |
initfile[0] = '\0'; |
74 |
< |
} |
129 |
> |
if (rayinitcal[0]) /* initialize on first call */ |
130 |
> |
initfunc(); |
131 |
|
if ((na = m->oargs.nsargs) <= ff) |
132 |
|
goto toofew; |
133 |
|
arg = m->oargs.sarg; |
134 |
|
if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL) |
135 |
|
goto memerr; |
136 |
|
i = strlen(arg[ff]); /* set up context */ |
137 |
< |
if (i == 1 && arg[ff][0] == '.') |
137 |
> |
if (i == 1 && arg[ff][0] == '.') { |
138 |
|
setcontext(f->ctx = ""); /* "." means no file */ |
139 |
< |
else { |
139 |
> |
} else { |
140 |
|
strcpy(sbuf,arg[ff]); /* file name is context */ |
141 |
|
if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF)) |
142 |
|
sbuf[i-LCALSUF] = '\0'; /* remove suffix */ |
164 |
|
i = ff+1; |
165 |
|
while (i < na && arg[i][0] != '-') |
166 |
|
i++; |
167 |
< |
if (i == na) /* no transform */ |
168 |
< |
f->f = f->b = &unitxf; |
169 |
< |
else { /* get transform */ |
170 |
< |
if ((f->b = (XF *)malloc(sizeof(XF))) == NULL) |
167 |
> |
if (i == na) { /* no transform */ |
168 |
> |
f->fxp = f->bxp = &unitxf; |
169 |
> |
} else { /* get transform */ |
170 |
> |
if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL) |
171 |
|
goto memerr; |
172 |
< |
if (invxf(f->b, na-i, arg+i) != na-i) |
172 |
> |
if (invxf(f->bxp, na-i, arg+i) != na-i) |
173 |
|
objerror(m, USER, "bad transform"); |
174 |
< |
if (f->b->sca < 0.0) |
175 |
< |
f->b->sca = -f->b->sca; |
174 |
> |
if (f->bxp->sca < 0.0) |
175 |
> |
f->bxp->sca = -f->bxp->sca; |
176 |
|
if (dofwd) { /* do both transforms */ |
177 |
< |
if ((f->f = (XF *)malloc(sizeof(XF))) == NULL) |
177 |
> |
if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL) |
178 |
|
goto memerr; |
179 |
< |
xf(f->f, na-i, arg+i); |
180 |
< |
if (f->f->sca < 0.0) |
181 |
< |
f->f->sca = -f->f->sca; |
179 |
> |
xf(f->fxp, na-i, arg+i); |
180 |
> |
if (f->fxp->sca < 0.0) |
181 |
> |
f->fxp->sca = -f->fxp->sca; |
182 |
|
} |
183 |
|
} |
184 |
|
m->os = (char *)f; |
187 |
|
objerror(m, USER, "too few string arguments"); |
188 |
|
memerr: |
189 |
|
error(SYSTEM, "out of memory in getfunc"); |
190 |
+ |
return NULL; /* pro forma return */ |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
void |
195 |
< |
freefunc(m) /* free memory associated with modifier */ |
196 |
< |
OBJREC *m; |
195 |
> |
freefunc( /* free memory associated with modifier */ |
196 |
> |
OBJREC *m |
197 |
> |
) |
198 |
|
{ |
199 |
< |
register MFUNC *f; |
200 |
< |
register int i; |
199 |
> |
MFUNC *f; |
200 |
> |
int i; |
201 |
|
|
202 |
|
if ((f = (MFUNC *)m->os) == NULL) |
203 |
|
return; |
212 |
|
dcleanup(2); /* remove definitions */ |
213 |
|
freestr(f->ctx); |
214 |
|
} |
215 |
< |
if (f->b != &unitxf) |
216 |
< |
free((void *)f->b); |
217 |
< |
if (f->f != NULL && f->f != &unitxf) |
218 |
< |
free((void *)f->f); |
215 |
> |
if (f->bxp != &unitxf) |
216 |
> |
free((void *)f->bxp); |
217 |
> |
if ((f->fxp != NULL) & (f->fxp != &unitxf)) |
218 |
> |
free((void *)f->fxp); |
219 |
|
free((void *)f); |
220 |
|
m->os = NULL; |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
int |
225 |
< |
setfunc(m, r) /* set channels for function call */ |
226 |
< |
OBJREC *m; |
227 |
< |
register RAY *r; |
225 |
> |
setfunc( /* set channels for function call */ |
226 |
> |
OBJREC *m, |
227 |
> |
RAY *r |
228 |
> |
) |
229 |
|
{ |
230 |
< |
static unsigned long lastrno = ~0; |
231 |
< |
register MFUNC *f; |
232 |
< |
/* get function */ |
230 |
> |
static RNUMBER lastrno = ~0; |
231 |
> |
MFUNC *f; |
232 |
> |
/* get function if any */ |
233 |
|
if ((f = (MFUNC *)m->os) == NULL) |
234 |
|
objerror(m, CONSISTENCY, "setfunc called before getfunc"); |
235 |
< |
/* set evaluator context */ |
236 |
< |
setcontext(f->ctx); |
235 |
> |
|
236 |
> |
setcontext(f->ctx); /* set evaluator context */ |
237 |
|
/* check to see if matrix set */ |
238 |
< |
if (m == fobj && r->rno == lastrno) |
238 |
> |
if ((m == fobj) & (r->rno == lastrno)) |
239 |
|
return(0); |
240 |
|
fobj = m; |
241 |
|
fray = r; |
242 |
< |
if (r->rox != NULL) |
243 |
< |
if (f->b != &unitxf) { |
244 |
< |
funcxf.sca = r->rox->b.sca * f->b->sca; |
245 |
< |
multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm); |
242 |
> |
if (r->rox != NULL) { |
243 |
> |
if (f->bxp != &unitxf) { |
244 |
> |
funcxf.sca = r->rox->b.sca * f->bxp->sca; |
245 |
> |
multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm); |
246 |
|
} else |
247 |
< |
copystruct(&funcxf, &r->rox->b); |
248 |
< |
else |
249 |
< |
copystruct(&funcxf, f->b); |
247 |
> |
funcxf = r->rox->b; |
248 |
> |
} else |
249 |
> |
funcxf = *f->bxp; |
250 |
|
lastrno = r->rno; |
251 |
|
eclock++; /* notify expression evaluator */ |
252 |
|
return(1); |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
+ |
int |
257 |
+ |
worldfunc( /* special function context sans object */ |
258 |
+ |
char *ctx, |
259 |
+ |
RAY *r |
260 |
+ |
) |
261 |
+ |
{ |
262 |
+ |
static RNUMBER lastrno = ~0; |
263 |
+ |
|
264 |
+ |
if (rayinitcal[0]) /* initialize on first call */ |
265 |
+ |
initfunc(); |
266 |
+ |
/* set evaluator context */ |
267 |
+ |
setcontext(ctx); |
268 |
+ |
/* check if ray already set */ |
269 |
+ |
if ((fobj == NULL) & (r->rno == lastrno)) |
270 |
+ |
return(0); |
271 |
+ |
fobj = NULL; |
272 |
+ |
fray = r; |
273 |
+ |
funcxf = unitxf; |
274 |
+ |
lastrno = r->rno; |
275 |
+ |
eclock++; /* notify expression evaluator */ |
276 |
+ |
return(1); |
277 |
+ |
} |
278 |
+ |
|
279 |
+ |
|
280 |
|
void |
281 |
< |
loadfunc(fname) /* load definition file */ |
282 |
< |
char *fname; |
281 |
> |
loadfunc( /* load definition file */ |
282 |
> |
char *fname |
283 |
> |
) |
284 |
|
{ |
285 |
|
char *ffname; |
286 |
|
|
293 |
|
|
294 |
|
|
295 |
|
static double |
296 |
< |
l_arg() /* return nth real argument */ |
296 |
> |
l_arg(char *nm) /* return nth real argument */ |
297 |
|
{ |
298 |
< |
register int n; |
298 |
> |
int n; |
299 |
|
|
300 |
|
if (fobj == NULL) |
301 |
< |
syntax("arg(n) used in constant expression"); |
301 |
> |
error(USER, "arg(n) called without a context"); |
302 |
|
|
303 |
|
n = argument(1) + .5; /* round to integer */ |
304 |
|
|
314 |
|
|
315 |
|
|
316 |
|
static double |
317 |
< |
l_erf() /* error function */ |
317 |
> |
l_erf(char *nm) /* error function */ |
318 |
|
{ |
235 |
– |
extern double erf(); |
236 |
– |
|
319 |
|
return(erf(argument(1))); |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
|
static double |
324 |
< |
l_erfc() /* cumulative error function */ |
324 |
> |
l_erfc(char *nm) /* cumulative error function */ |
325 |
|
{ |
244 |
– |
extern double erfc(); |
245 |
– |
|
326 |
|
return(erfc(argument(1))); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
double |
331 |
< |
chanvalue(n) /* return channel n to calcomp */ |
332 |
< |
register int n; |
331 |
> |
chanvalue( /* return channel n to calcomp */ |
332 |
> |
int n |
333 |
> |
) |
334 |
|
{ |
335 |
|
if (fray == NULL) |
336 |
|
syntax("ray parameter used in constant expression"); |
352 |
|
fray->ron[2]*funcxf.xfm[2][n-3] ) |
353 |
|
/ funcxf.sca ); |
354 |
|
|
355 |
< |
if (n <= 8) /* intersection */ |
355 |
> |
if (n <= 8) { /* intersection point */ |
356 |
> |
if (fray->rot >= FHUGE) |
357 |
> |
return(0.0); /* XXX should be runtime error? */ |
358 |
|
|
359 |
|
return( fray->rop[0]*funcxf.xfm[0][n-6] + |
360 |
|
fray->rop[1]*funcxf.xfm[1][n-6] + |
361 |
|
fray->rop[2]*funcxf.xfm[2][n-6] + |
362 |
|
funcxf.xfm[3][n-6] ); |
363 |
+ |
} |
364 |
|
|
365 |
|
if (n == 9) /* total distance */ |
366 |
|
return(raydist(fray,PRIMARY) * funcxf.sca); |