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(void) /* 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 |
+ |
calcontext(""); |
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) |
76 |
+ |
return; |
77 |
+ |
if (prms == last_params || (last_params != NULL && |
78 |
+ |
!strcmp(prms, last_params))) |
79 |
+ |
return; |
80 |
+ |
last_params = prms; /* XXX assumes static string */ |
81 |
+ |
/* assign each variable */ |
82 |
+ |
while (*prms) { |
83 |
+ |
if (isspace(*prms)) { |
84 |
+ |
++prms; continue; |
85 |
+ |
} |
86 |
+ |
if (!isalpha(*prms)) |
87 |
+ |
goto bad_params; |
88 |
+ |
cpd = vname; |
89 |
+ |
while (*prms && (*prms != '=') & !isspace(*prms)) { |
90 |
+ |
if (!isid(*prms) | (cpd-vname >= RMAXWORD-1)) |
91 |
+ |
goto bad_params; |
92 |
+ |
*cpd++ = *prms++; |
93 |
+ |
} |
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 == ';') | (*prms == ':'); |
103 |
+ |
varset(vname, '=', value); |
104 |
+ |
} |
105 |
+ |
eclock++; /* notify expression evaluator */ |
106 |
+ |
return; |
107 |
+ |
bad_params: |
108 |
+ |
sprintf(errmsg, "bad parameter list '%s'", last_params); |
109 |
+ |
error(USER, errmsg); |
110 |
+ |
} |
111 |
+ |
|
112 |
+ |
|
113 |
|
MFUNC * |
114 |
< |
getfunc(m, ff, ef, dofwd) /* get function for this modifier */ |
115 |
< |
OBJREC *m; |
116 |
< |
int ff; |
117 |
< |
unsigned int ef; |
118 |
< |
int dofwd; |
114 |
> |
getfunc( /* get function for this modifier */ |
115 |
> |
OBJREC *m, |
116 |
> |
int ff, |
117 |
> |
unsigned int ef, |
118 |
> |
int dofwd |
119 |
> |
) |
120 |
|
{ |
44 |
– |
static char initfile[] = INITFILE; |
121 |
|
char sbuf[MAXSTR]; |
122 |
< |
register char **arg; |
123 |
< |
register MFUNC *f; |
122 |
> |
char **arg; |
123 |
> |
MFUNC *f; |
124 |
|
int ne, na; |
125 |
< |
register int i; |
125 |
> |
int i; |
126 |
|
/* check to see if done already */ |
127 |
|
if ((f = (MFUNC *)m->os) != NULL) |
128 |
|
return(f); |
129 |
|
fobj = NULL; fray = NULL; |
130 |
< |
if (initfile[0]) { /* initialize on first call */ |
131 |
< |
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=$25;Lv=$26;", 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 |
< |
} |
130 |
> |
if (rayinitcal[0]) /* initialize on first call */ |
131 |
> |
initfunc(); |
132 |
|
if ((na = m->oargs.nsargs) <= ff) |
133 |
|
goto toofew; |
134 |
|
arg = m->oargs.sarg; |
135 |
|
if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL) |
136 |
|
goto memerr; |
137 |
|
i = strlen(arg[ff]); /* set up context */ |
138 |
< |
if (i == 1 && arg[ff][0] == '.') |
139 |
< |
setcontext(f->ctx = ""); /* "." means no file */ |
140 |
< |
else { |
138 |
> |
if (i == 1 && arg[ff][0] == '.') { |
139 |
> |
calcontext(f->ctx = ""); /* "." means no file */ |
140 |
> |
} else { |
141 |
|
strcpy(sbuf,arg[ff]); /* file name is context */ |
142 |
|
if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF)) |
143 |
|
sbuf[i-LCALSUF] = '\0'; /* remove suffix */ |
144 |
< |
setcontext(f->ctx = savestr(sbuf)); |
144 |
> |
calcontext(f->ctx = savestr(sbuf)); |
145 |
|
if (!vardefined(REFVNAME)) { /* file loaded? */ |
146 |
|
loadfunc(arg[ff]); |
147 |
|
varset(REFVNAME, '=', 1.0); |
148 |
|
} else /* reference_count++ */ |
149 |
|
varset(REFVNAME, '=', varvalue(REFVNAME)+1.0); |
150 |
|
} |
151 |
< |
curfunc = NULL; /* parse expressions */ |
151 |
> |
ecurfunc = NULL; /* parse expressions */ |
152 |
|
sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname); |
153 |
|
for (i=0, ne=0; ef && i < na; i++, ef>>=1) |
154 |
|
if (ef & 1) { /* flagged as an expression? */ |
157 |
|
initstr(arg[i], sbuf, 0); |
158 |
|
f->ep[ne++] = getE1(); |
159 |
|
if (nextc != EOF) |
160 |
< |
syntax("unexpected character"); |
160 |
> |
esyntax("unexpected character"); |
161 |
|
} |
162 |
|
if (ef) |
163 |
|
goto toofew; |
165 |
|
i = ff+1; |
166 |
|
while (i < na && arg[i][0] != '-') |
167 |
|
i++; |
168 |
< |
if (i == na) /* no transform */ |
169 |
< |
f->f = f->b = &unitxf; |
170 |
< |
else { /* get transform */ |
171 |
< |
if ((f->b = (XF *)malloc(sizeof(XF))) == NULL) |
168 |
> |
if (i == na) { /* no transform */ |
169 |
> |
f->fxp = f->bxp = &unitxf; |
170 |
> |
} else { /* get transform */ |
171 |
> |
if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL) |
172 |
|
goto memerr; |
173 |
< |
if (invxf(f->b, na-i, arg+i) != na-i) |
173 |
> |
if (invxf(f->bxp, na-i, arg+i) != na-i) |
174 |
|
objerror(m, USER, "bad transform"); |
175 |
< |
if (f->b->sca < 0.0) |
176 |
< |
f->b->sca = -f->b->sca; |
175 |
> |
if (f->bxp->sca < 0.0) |
176 |
> |
f->bxp->sca = -f->bxp->sca; |
177 |
|
if (dofwd) { /* do both transforms */ |
178 |
< |
if ((f->f = (XF *)malloc(sizeof(XF))) == NULL) |
178 |
> |
if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL) |
179 |
|
goto memerr; |
180 |
< |
xf(f->f, na-i, arg+i); |
181 |
< |
if (f->f->sca < 0.0) |
182 |
< |
f->f->sca = -f->f->sca; |
180 |
> |
xf(f->fxp, na-i, arg+i); |
181 |
> |
if (f->fxp->sca < 0.0) |
182 |
> |
f->fxp->sca = -f->fxp->sca; |
183 |
|
} |
184 |
|
} |
185 |
|
m->os = (char *)f; |
188 |
|
objerror(m, USER, "too few string arguments"); |
189 |
|
memerr: |
190 |
|
error(SYSTEM, "out of memory in getfunc"); |
191 |
+ |
return NULL; /* pro forma return */ |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
void |
196 |
< |
freefunc(m) /* free memory associated with modifier */ |
197 |
< |
OBJREC *m; |
196 |
> |
freefunc( /* free memory associated with modifier */ |
197 |
> |
OBJREC *m |
198 |
> |
) |
199 |
|
{ |
200 |
< |
register MFUNC *f; |
201 |
< |
register int i; |
200 |
> |
MFUNC *f; |
201 |
> |
int i; |
202 |
|
|
203 |
|
if ((f = (MFUNC *)m->os) == NULL) |
204 |
|
return; |
205 |
|
for (i = 0; f->ep[i] != NULL; i++) |
206 |
< |
epfree(f->ep[i]); |
206 |
> |
epfree(f->ep[i],1); |
207 |
|
if (f->ctx[0]) { /* done with definitions */ |
208 |
< |
setcontext(f->ctx); |
208 |
> |
calcontext(f->ctx); |
209 |
|
i = varvalue(REFVNAME)-.5; /* reference_count-- */ |
210 |
|
if (i > 0) |
211 |
|
varset(REFVNAME, '=', (double)i); |
213 |
|
dcleanup(2); /* remove definitions */ |
214 |
|
freestr(f->ctx); |
215 |
|
} |
216 |
< |
if (f->b != &unitxf) |
217 |
< |
free((void *)f->b); |
218 |
< |
if (f->f != NULL && f->f != &unitxf) |
219 |
< |
free((void *)f->f); |
216 |
> |
if (f->bxp != &unitxf) |
217 |
> |
free((void *)f->bxp); |
218 |
> |
if ((f->fxp != NULL) & (f->fxp != &unitxf)) |
219 |
> |
free((void *)f->fxp); |
220 |
|
free((void *)f); |
221 |
|
m->os = NULL; |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
int |
226 |
< |
setfunc(m, r) /* set channels for function call */ |
227 |
< |
OBJREC *m; |
228 |
< |
register RAY *r; |
226 |
> |
setfunc( /* set channels for function call */ |
227 |
> |
OBJREC *m, |
228 |
> |
RAY *r |
229 |
> |
) |
230 |
|
{ |
231 |
< |
static unsigned long lastrno = ~0; |
232 |
< |
register MFUNC *f; |
233 |
< |
/* get function */ |
231 |
> |
static RNUMBER lastrno = ~0; |
232 |
> |
MFUNC *f; |
233 |
> |
/* get function if any */ |
234 |
|
if ((f = (MFUNC *)m->os) == NULL) |
235 |
|
objerror(m, CONSISTENCY, "setfunc called before getfunc"); |
236 |
< |
/* set evaluator context */ |
237 |
< |
setcontext(f->ctx); |
236 |
> |
|
237 |
> |
calcontext(f->ctx); /* set evaluator context */ |
238 |
|
/* check to see if matrix set */ |
239 |
< |
if (m == fobj && r->rno == lastrno) |
239 |
> |
if ((m == fobj) & (r->rno == lastrno)) |
240 |
|
return(0); |
241 |
|
fobj = m; |
242 |
|
fray = r; |
243 |
< |
if (r->rox != NULL) |
244 |
< |
if (f->b != &unitxf) { |
245 |
< |
funcxf.sca = r->rox->b.sca * f->b->sca; |
246 |
< |
multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm); |
243 |
> |
if (r->rox != NULL) { |
244 |
> |
if (f->bxp != &unitxf) { |
245 |
> |
funcxf.sca = r->rox->b.sca * f->bxp->sca; |
246 |
> |
multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm); |
247 |
|
} else |
248 |
< |
copystruct(&funcxf, &r->rox->b); |
249 |
< |
else |
250 |
< |
copystruct(&funcxf, f->b); |
248 |
> |
funcxf = r->rox->b; |
249 |
> |
} else |
250 |
> |
funcxf = *f->bxp; |
251 |
|
lastrno = r->rno; |
252 |
|
eclock++; /* notify expression evaluator */ |
253 |
|
return(1); |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
+ |
int |
258 |
+ |
worldfunc( /* special function context sans object */ |
259 |
+ |
char *ctx, |
260 |
+ |
RAY *r |
261 |
+ |
) |
262 |
+ |
{ |
263 |
+ |
static RNUMBER lastrno = ~0; |
264 |
+ |
|
265 |
+ |
if (rayinitcal[0]) /* initialize on first call */ |
266 |
+ |
initfunc(); |
267 |
+ |
/* set evaluator context */ |
268 |
+ |
calcontext(ctx); |
269 |
+ |
/* check if ray already set */ |
270 |
+ |
if ((fobj == NULL) & (r->rno == lastrno)) |
271 |
+ |
return(0); |
272 |
+ |
fobj = NULL; |
273 |
+ |
fray = r; |
274 |
+ |
funcxf = unitxf; |
275 |
+ |
lastrno = r->rno; |
276 |
+ |
eclock++; /* notify expression evaluator */ |
277 |
+ |
return(1); |
278 |
+ |
} |
279 |
+ |
|
280 |
+ |
|
281 |
|
void |
282 |
< |
loadfunc(fname) /* load definition file */ |
283 |
< |
char *fname; |
282 |
> |
loadfunc( /* load definition file */ |
283 |
> |
char *fname |
284 |
> |
) |
285 |
|
{ |
286 |
|
char *ffname; |
287 |
|
|
288 |
< |
if ((ffname = getpath(fname, getlibpath(), R_OK)) == NULL) { |
288 |
> |
if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) { |
289 |
|
sprintf(errmsg, "cannot find function file \"%s\"", fname); |
290 |
< |
error(USER, errmsg); |
290 |
> |
error(SYSTEM, errmsg); |
291 |
|
} |
292 |
|
fcompile(ffname); |
293 |
+ |
doptimize(1); /* optimize definitions */ |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
static double |
298 |
< |
l_arg() /* return nth real argument */ |
298 |
> |
l_arg(char *nm) /* return nth real argument */ |
299 |
|
{ |
300 |
< |
register int n; |
300 |
> |
int n; |
301 |
|
|
302 |
|
if (fobj == NULL) |
303 |
< |
syntax("arg(n) used in constant expression"); |
303 |
> |
error(USER, |
304 |
> |
"bad call to arg(n) - illegal constant in .cal file?"); |
305 |
|
|
306 |
|
n = argument(1) + .5; /* round to integer */ |
307 |
|
|
317 |
|
|
318 |
|
|
319 |
|
static double |
320 |
< |
l_erf() /* error function */ |
320 |
> |
l_erf(char *nm) /* error function */ |
321 |
|
{ |
235 |
– |
extern double erf(); |
236 |
– |
|
322 |
|
return(erf(argument(1))); |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
static double |
327 |
< |
l_erfc() /* cumulative error function */ |
327 |
> |
l_erfc(char *nm) /* cumulative error function */ |
328 |
|
{ |
244 |
– |
extern double erfc(); |
245 |
– |
|
329 |
|
return(erfc(argument(1))); |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
double |
334 |
< |
chanvalue(n) /* return channel n to calcomp */ |
335 |
< |
register int n; |
334 |
> |
chanvalue( /* return channel n to calcomp */ |
335 |
> |
int n |
336 |
> |
) |
337 |
|
{ |
338 |
|
if (fray == NULL) |
339 |
< |
syntax("ray parameter used in constant expression"); |
339 |
> |
esyntax("ray parameter used in constant expression"); |
340 |
|
|
341 |
|
if (--n < 0) |
342 |
|
goto badchan; |
343 |
|
|
344 |
< |
if (n < 3) /* ray direction */ |
344 |
> |
if (n <= 2) /* ray direction */ |
345 |
|
|
346 |
|
return( ( fray->rdir[0]*funcxf.xfm[0][n] + |
347 |
|
fray->rdir[1]*funcxf.xfm[1][n] + |
348 |
|
fray->rdir[2]*funcxf.xfm[2][n] ) |
349 |
|
/ funcxf.sca ); |
350 |
|
|
351 |
< |
if (n < 6) /* surface normal */ |
351 |
> |
if (n <= 5) /* surface normal */ |
352 |
|
|
353 |
|
return( ( fray->ron[0]*funcxf.xfm[0][n-3] + |
354 |
|
fray->ron[1]*funcxf.xfm[1][n-3] + |
355 |
|
fray->ron[2]*funcxf.xfm[2][n-3] ) |
356 |
|
/ funcxf.sca ); |
357 |
|
|
358 |
< |
if (n < 9) /* intersection */ |
358 |
> |
if (n <= 8) { /* intersection point */ |
359 |
> |
if (fray->rot >= FHUGE*.99) |
360 |
> |
return(0.0); /* XXX should be runtime error? */ |
361 |
|
|
362 |
|
return( fray->rop[0]*funcxf.xfm[0][n-6] + |
363 |
|
fray->rop[1]*funcxf.xfm[1][n-6] + |
364 |
|
fray->rop[2]*funcxf.xfm[2][n-6] + |
365 |
|
funcxf.xfm[3][n-6] ); |
366 |
+ |
} |
367 |
|
|
368 |
|
if (n == 9) /* total distance */ |
369 |
|
return(raydist(fray,PRIMARY) * funcxf.sca); |
376 |
|
if (n == 11) /* scale */ |
377 |
|
return(funcxf.sca); |
378 |
|
|
379 |
< |
if (n < 15) /* origin */ |
379 |
> |
if (n <= 14) /* origin */ |
380 |
|
return(funcxf.xfm[3][n-12]); |
381 |
|
|
382 |
< |
if (n < 18) /* i unit vector */ |
382 |
> |
if (n <= 17) /* i unit vector */ |
383 |
|
return(funcxf.xfm[0][n-15] / funcxf.sca); |
384 |
|
|
385 |
< |
if (n < 21) /* j unit vector */ |
386 |
< |
return(funcxf.xfm[1][n-15] / funcxf.sca); |
385 |
> |
if (n <= 20) /* j unit vector */ |
386 |
> |
return(funcxf.xfm[1][n-18] / funcxf.sca); |
387 |
|
|
388 |
< |
if (n < 24) /* k unit vector */ |
388 |
> |
if (n <= 23) /* k unit vector */ |
389 |
|
return(funcxf.xfm[2][n-21] / funcxf.sca); |
390 |
|
|
391 |
< |
if (n < 25) /* single ray (shadow) distance */ |
391 |
> |
if (n == 24) /* single ray (shadow) distance */ |
392 |
|
return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca); |
393 |
|
|
394 |
< |
if (n < 27) /* local (u,v) coordinates */ |
395 |
< |
return(fray->uv[n-26]); |
394 |
> |
if (n <= 26) /* local (u,v) coordinates */ |
395 |
> |
return(fray->uv[n-25]); |
396 |
|
badchan: |
397 |
|
error(USER, "illegal channel number"); |
398 |
|
return(0.0); |