1 |
– |
/* Copyright (c) 1992 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 |
|
* func.c - interface to calcomp functions. |
9 |
– |
* |
10 |
– |
* 4/7/86 |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "ray.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
+ |
#include "ray.h" |
11 |
+ |
#include "paths.h" |
12 |
|
#include "otypes.h" |
16 |
– |
|
13 |
|
#include "func.h" |
14 |
+ |
#include <ctype.h> |
15 |
|
|
16 |
|
|
17 |
|
#define INITFILE "rayinit.cal" |
21 |
– |
#define REFVNAME "`FILE_REFCNT" |
18 |
|
#define CALSUF ".cal" |
19 |
|
#define LCALSUF 4 |
20 |
+ |
char REFVNAME[] = "`FILE_REFCNT"; |
21 |
|
|
22 |
|
XF unitxf = { /* identity transform */ |
23 |
|
{{1.0, 0.0, 0.0, 0.0}, |
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 |
+ |
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; |
71 |
+ |
static int lplen = 0; |
72 |
+ |
int len; |
73 |
+ |
char vname[RMAXWORD]; |
74 |
+ |
double value; |
75 |
+ |
char *cpd; |
76 |
+ |
/* check if already set */ |
77 |
+ |
if (prms == NULL || !*prms) |
78 |
+ |
return; |
79 |
+ |
if (lplen && !strcmp(prms, last_params)) |
80 |
+ |
return; |
81 |
+ |
len = strlen(prms); /* record new settings */ |
82 |
+ |
if ((lplen != 0) & (lplen <= len)) { |
83 |
+ |
free(last_params); |
84 |
+ |
lplen = 0; |
85 |
+ |
} |
86 |
+ |
if (!lplen) { |
87 |
+ |
lplen = len + 100; |
88 |
+ |
last_params = (char *)malloc(lplen); |
89 |
+ |
if (last_params == NULL) |
90 |
+ |
error(SYSTEM, "out of memory in set_eparams()"); |
91 |
+ |
} |
92 |
+ |
strcpy(last_params, prms); |
93 |
+ |
/* assign each variable */ |
94 |
+ |
while (*prms) { |
95 |
+ |
if (isspace(*prms)) { |
96 |
+ |
++prms; continue; |
97 |
+ |
} |
98 |
+ |
if (!isalpha(*prms)) |
99 |
+ |
goto bad_params; |
100 |
+ |
cpd = vname; |
101 |
+ |
while (*prms && (*prms != '=') & !isspace(*prms)) { |
102 |
+ |
if (!isid(*prms) | (cpd-vname >= RMAXWORD-1)) |
103 |
+ |
goto bad_params; |
104 |
+ |
*cpd++ = *prms++; |
105 |
+ |
} |
106 |
+ |
*cpd = '\0'; |
107 |
+ |
while (isspace(*prms)) prms++; |
108 |
+ |
if (*prms++ != '=') |
109 |
+ |
goto bad_params; |
110 |
+ |
value = atof(prms); |
111 |
+ |
if ((prms = fskip(prms)) == NULL) |
112 |
+ |
goto bad_params; |
113 |
+ |
while (isspace(*prms)) prms++; |
114 |
+ |
prms += (*prms == ',') | (*prms == ';'); |
115 |
+ |
varset(vname, '=', value); |
116 |
+ |
} |
117 |
+ |
return; |
118 |
+ |
bad_params: |
119 |
+ |
sprintf(errmsg, "bad parameter list '%s'", last_params); |
120 |
+ |
error(USER, errmsg); |
121 |
+ |
} |
122 |
+ |
|
123 |
+ |
|
124 |
|
MFUNC * |
125 |
< |
getfunc(m, ff, ef, dofwd) /* get function for this modifier */ |
126 |
< |
OBJREC *m; |
127 |
< |
int ff; |
128 |
< |
unsigned ef; |
129 |
< |
int dofwd; |
125 |
> |
getfunc( /* get function for this modifier */ |
126 |
> |
OBJREC *m, |
127 |
> |
int ff, |
128 |
> |
unsigned int ef, |
129 |
> |
int dofwd |
130 |
> |
) |
131 |
|
{ |
47 |
– |
extern EPNODE *curfunc; |
48 |
– |
static char initfile[] = INITFILE; |
132 |
|
char sbuf[MAXSTR]; |
133 |
< |
register char **arg; |
134 |
< |
register MFUNC *f; |
133 |
> |
char **arg; |
134 |
> |
MFUNC *f; |
135 |
|
int ne, na; |
136 |
< |
register int i; |
136 |
> |
int i; |
137 |
|
/* check to see if done already */ |
138 |
|
if ((f = (MFUNC *)m->os) != NULL) |
139 |
|
return(f); |
140 |
|
fobj = NULL; fray = NULL; |
141 |
< |
if (initfile[0]) { /* initialize on first call */ |
142 |
< |
setcontext(""); |
60 |
< |
scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0); |
61 |
< |
scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0); |
62 |
< |
scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0); |
63 |
< |
scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0); |
64 |
< |
scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0); |
65 |
< |
scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0); |
66 |
< |
scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0); |
67 |
< |
scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0); |
68 |
< |
funset("arg", 1, '=', l_arg); |
69 |
< |
funset("erf", 1, ':', l_erf); |
70 |
< |
funset("erfc", 1, ':', l_erfc); |
71 |
< |
setnoisefuncs(); |
72 |
< |
loadfunc(initfile); |
73 |
< |
initfile[0] = '\0'; |
74 |
< |
} |
141 |
> |
if (rayinitcal[0]) /* initialize on first call */ |
142 |
> |
initfunc(); |
143 |
|
if ((na = m->oargs.nsargs) <= ff) |
144 |
|
goto toofew; |
145 |
|
arg = m->oargs.sarg; |
146 |
|
if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL) |
147 |
|
goto memerr; |
148 |
|
i = strlen(arg[ff]); /* set up context */ |
149 |
< |
if (i == 1 && arg[ff][0] == '.') |
149 |
> |
if (i == 1 && arg[ff][0] == '.') { |
150 |
|
setcontext(f->ctx = ""); /* "." means no file */ |
151 |
< |
else { |
152 |
< |
strcpy(sbuf,arg[ff]); /* file name is context */ |
151 |
> |
} else { |
152 |
> |
strcpy(sbuf,arg[ff]); /* file name is context */ |
153 |
|
if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF)) |
154 |
|
sbuf[i-LCALSUF] = '\0'; /* remove suffix */ |
155 |
|
setcontext(f->ctx = savestr(sbuf)); |
176 |
|
i = ff+1; |
177 |
|
while (i < na && arg[i][0] != '-') |
178 |
|
i++; |
179 |
< |
if (i == na) /* no transform */ |
180 |
< |
f->f = f->b = &unitxf; |
181 |
< |
else { /* get transform */ |
182 |
< |
if ((f->b = (XF *)malloc(sizeof(XF))) == NULL) |
179 |
> |
if (i == na) { /* no transform */ |
180 |
> |
f->fxp = f->bxp = &unitxf; |
181 |
> |
} else { /* get transform */ |
182 |
> |
if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL) |
183 |
|
goto memerr; |
184 |
< |
if (invxf(f->b, na-i, arg+i) != na-i) |
184 |
> |
if (invxf(f->bxp, na-i, arg+i) != na-i) |
185 |
|
objerror(m, USER, "bad transform"); |
186 |
< |
if (f->b->sca < 0.0) |
187 |
< |
f->b->sca = -f->b->sca; |
186 |
> |
if (f->bxp->sca < 0.0) |
187 |
> |
f->bxp->sca = -f->bxp->sca; |
188 |
|
if (dofwd) { /* do both transforms */ |
189 |
< |
if ((f->f = (XF *)malloc(sizeof(XF))) == NULL) |
189 |
> |
if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL) |
190 |
|
goto memerr; |
191 |
< |
xf(f->f, na-i, arg+i); |
192 |
< |
if (f->f->sca < 0.0) |
193 |
< |
f->f->sca = -f->f->sca; |
191 |
> |
xf(f->fxp, na-i, arg+i); |
192 |
> |
if (f->fxp->sca < 0.0) |
193 |
> |
f->fxp->sca = -f->fxp->sca; |
194 |
|
} |
195 |
|
} |
196 |
|
m->os = (char *)f; |
199 |
|
objerror(m, USER, "too few string arguments"); |
200 |
|
memerr: |
201 |
|
error(SYSTEM, "out of memory in getfunc"); |
202 |
+ |
return NULL; /* pro forma return */ |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
< |
freefunc(m) /* free memory associated with modifier */ |
207 |
< |
OBJREC *m; |
206 |
> |
void |
207 |
> |
freefunc( /* free memory associated with modifier */ |
208 |
> |
OBJREC *m |
209 |
> |
) |
210 |
|
{ |
211 |
< |
register MFUNC *f; |
212 |
< |
register int i; |
211 |
> |
MFUNC *f; |
212 |
> |
int i; |
213 |
|
|
214 |
|
if ((f = (MFUNC *)m->os) == NULL) |
215 |
|
return; |
224 |
|
dcleanup(2); /* remove definitions */ |
225 |
|
freestr(f->ctx); |
226 |
|
} |
227 |
< |
if (f->b != &unitxf) |
228 |
< |
free((char *)f->b); |
229 |
< |
if (f->f != NULL && f->f != &unitxf) |
230 |
< |
free((char *)f->f); |
231 |
< |
free((char *)f); |
227 |
> |
if (f->bxp != &unitxf) |
228 |
> |
free((void *)f->bxp); |
229 |
> |
if ((f->fxp != NULL) & (f->fxp != &unitxf)) |
230 |
> |
free((void *)f->fxp); |
231 |
> |
free((void *)f); |
232 |
|
m->os = NULL; |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
< |
setfunc(m, r) /* set channels for function call */ |
237 |
< |
OBJREC *m; |
238 |
< |
register RAY *r; |
236 |
> |
int |
237 |
> |
setfunc( /* set channels for function call */ |
238 |
> |
OBJREC *m, |
239 |
> |
RAY *r |
240 |
> |
) |
241 |
|
{ |
242 |
< |
static long lastrno = -1; |
243 |
< |
register MFUNC *f; |
244 |
< |
/* get function */ |
242 |
> |
static RNUMBER lastrno = ~0; |
243 |
> |
MFUNC *f; |
244 |
> |
/* get function if any */ |
245 |
|
if ((f = (MFUNC *)m->os) == NULL) |
246 |
< |
error(m, CONSISTENCY, "setfunc called before getfunc"); |
247 |
< |
/* set evaluator context */ |
248 |
< |
setcontext(f->ctx); |
246 |
> |
objerror(m, CONSISTENCY, "setfunc called before getfunc"); |
247 |
> |
|
248 |
> |
setcontext(f->ctx); /* set evaluator context */ |
249 |
|
/* check to see if matrix set */ |
250 |
< |
if (m == fobj && r->rno == lastrno) |
250 |
> |
if ((m == fobj) & (r->rno == lastrno)) |
251 |
|
return(0); |
252 |
|
fobj = m; |
253 |
|
fray = r; |
254 |
< |
if (r->rox != NULL) |
255 |
< |
if (f->b != &unitxf) { |
256 |
< |
funcxf.sca = r->rox->b.sca * f->b->sca; |
257 |
< |
multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm); |
254 |
> |
if (r->rox != NULL) { |
255 |
> |
if (f->bxp != &unitxf) { |
256 |
> |
funcxf.sca = r->rox->b.sca * f->bxp->sca; |
257 |
> |
multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm); |
258 |
|
} else |
259 |
< |
copystruct(&funcxf, &r->rox->b); |
260 |
< |
else |
261 |
< |
copystruct(&funcxf, f->b); |
259 |
> |
funcxf = r->rox->b; |
260 |
> |
} else |
261 |
> |
funcxf = *f->bxp; |
262 |
|
lastrno = r->rno; |
263 |
|
eclock++; /* notify expression evaluator */ |
264 |
|
return(1); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
< |
loadfunc(fname) /* load definition file */ |
269 |
< |
char *fname; |
268 |
> |
int |
269 |
> |
worldfunc( /* special function context sans object */ |
270 |
> |
char *ctx, |
271 |
> |
RAY *r |
272 |
> |
) |
273 |
|
{ |
274 |
< |
extern char *libpath; /* library search path */ |
274 |
> |
static RNUMBER lastrno = ~0; |
275 |
> |
|
276 |
> |
if (rayinitcal[0]) /* initialize on first call */ |
277 |
> |
initfunc(); |
278 |
> |
/* set evaluator context */ |
279 |
> |
setcontext(ctx); |
280 |
> |
/* check if ray already set */ |
281 |
> |
if ((fobj == NULL) & (r->rno == lastrno)) |
282 |
> |
return(0); |
283 |
> |
fobj = NULL; |
284 |
> |
fray = r; |
285 |
> |
funcxf = unitxf; |
286 |
> |
lastrno = r->rno; |
287 |
> |
eclock++; /* notify expression evaluator */ |
288 |
> |
return(1); |
289 |
> |
} |
290 |
> |
|
291 |
> |
|
292 |
> |
void |
293 |
> |
loadfunc( /* load definition file */ |
294 |
> |
char *fname |
295 |
> |
) |
296 |
> |
{ |
297 |
|
char *ffname; |
298 |
|
|
299 |
< |
if ((ffname = getpath(fname, libpath, R_OK)) == NULL) { |
299 |
> |
if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) { |
300 |
|
sprintf(errmsg, "cannot find function file \"%s\"", fname); |
301 |
< |
error(USER, errmsg); |
301 |
> |
error(SYSTEM, errmsg); |
302 |
|
} |
303 |
|
fcompile(ffname); |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
|
static double |
308 |
< |
l_arg() /* return nth real argument */ |
308 |
> |
l_arg(char *nm) /* return nth real argument */ |
309 |
|
{ |
310 |
< |
extern double argument(); |
213 |
< |
register int n; |
310 |
> |
int n; |
311 |
|
|
312 |
|
if (fobj == NULL) |
313 |
< |
syntax("arg(n) used in constant expression"); |
313 |
> |
error(INTERNAL, "arg(n) called without a modifier context"); |
314 |
|
|
315 |
|
n = argument(1) + .5; /* round to integer */ |
316 |
|
|
326 |
|
|
327 |
|
|
328 |
|
static double |
329 |
< |
l_erf() /* error function */ |
329 |
> |
l_erf(char *nm) /* error function */ |
330 |
|
{ |
234 |
– |
extern double erf(); |
235 |
– |
|
331 |
|
return(erf(argument(1))); |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
|
static double |
336 |
< |
l_erfc() /* cumulative error function */ |
336 |
> |
l_erfc(char *nm) /* cumulative error function */ |
337 |
|
{ |
243 |
– |
extern double erfc(); |
244 |
– |
|
338 |
|
return(erfc(argument(1))); |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
|
double |
343 |
< |
chanvalue(n) /* return channel n to calcomp */ |
344 |
< |
register int n; |
343 |
> |
chanvalue( /* return channel n to calcomp */ |
344 |
> |
int n |
345 |
> |
) |
346 |
|
{ |
253 |
– |
double sum; |
254 |
– |
register RAY *r; |
255 |
– |
|
347 |
|
if (fray == NULL) |
348 |
|
syntax("ray parameter used in constant expression"); |
349 |
|
|
350 |
|
if (--n < 0) |
351 |
|
goto badchan; |
352 |
|
|
353 |
< |
if (n < 3) /* ray direction */ |
353 |
> |
if (n <= 2) /* ray direction */ |
354 |
|
|
355 |
|
return( ( fray->rdir[0]*funcxf.xfm[0][n] + |
356 |
|
fray->rdir[1]*funcxf.xfm[1][n] + |
357 |
|
fray->rdir[2]*funcxf.xfm[2][n] ) |
358 |
|
/ funcxf.sca ); |
359 |
|
|
360 |
< |
if (n < 6) /* surface normal */ |
360 |
> |
if (n <= 5) /* surface normal */ |
361 |
|
|
362 |
|
return( ( fray->ron[0]*funcxf.xfm[0][n-3] + |
363 |
|
fray->ron[1]*funcxf.xfm[1][n-3] + |
364 |
|
fray->ron[2]*funcxf.xfm[2][n-3] ) |
365 |
|
/ funcxf.sca ); |
366 |
|
|
367 |
< |
if (n < 9) /* intersection */ |
367 |
> |
if (n <= 8) { /* intersection point */ |
368 |
> |
if (fray->rot >= FHUGE) |
369 |
> |
return(0.0); /* XXX should be runtime error? */ |
370 |
|
|
371 |
|
return( fray->rop[0]*funcxf.xfm[0][n-6] + |
372 |
|
fray->rop[1]*funcxf.xfm[1][n-6] + |
373 |
|
fray->rop[2]*funcxf.xfm[2][n-6] + |
374 |
|
funcxf.xfm[3][n-6] ); |
282 |
– |
|
283 |
– |
if (n == 9) { /* total distance */ |
284 |
– |
sum = fray->rot; |
285 |
– |
for (r = fray->parent; r != NULL; r = r->parent) |
286 |
– |
sum += r->rot; |
287 |
– |
return(sum * funcxf.sca); |
288 |
– |
|
375 |
|
} |
376 |
|
|
377 |
+ |
if (n == 9) /* total distance */ |
378 |
+ |
return(raydist(fray,PRIMARY) * funcxf.sca); |
379 |
+ |
|
380 |
|
if (n == 10) /* dot product (range [-1,1]) */ |
381 |
|
return( fray->rod <= -1.0 ? -1.0 : |
382 |
|
fray->rod >= 1.0 ? 1.0 : |
385 |
|
if (n == 11) /* scale */ |
386 |
|
return(funcxf.sca); |
387 |
|
|
388 |
< |
if (n < 15) /* origin */ |
388 |
> |
if (n <= 14) /* origin */ |
389 |
|
return(funcxf.xfm[3][n-12]); |
390 |
|
|
391 |
< |
if (n < 18) /* i unit vector */ |
391 |
> |
if (n <= 17) /* i unit vector */ |
392 |
|
return(funcxf.xfm[0][n-15] / funcxf.sca); |
393 |
|
|
394 |
< |
if (n < 21) /* j unit vector */ |
395 |
< |
return(funcxf.xfm[1][n-15] / funcxf.sca); |
394 |
> |
if (n <= 20) /* j unit vector */ |
395 |
> |
return(funcxf.xfm[1][n-18] / funcxf.sca); |
396 |
|
|
397 |
< |
if (n < 24) /* k unit vector */ |
397 |
> |
if (n <= 23) /* k unit vector */ |
398 |
|
return(funcxf.xfm[2][n-21] / funcxf.sca); |
399 |
|
|
400 |
< |
if (n == 24) { /* single ray (shadow) distance */ |
401 |
< |
sum = fray->rot; |
402 |
< |
for (r = fray->parent; r != NULL && r->crtype&SHADOW; |
403 |
< |
r = r->parent) |
404 |
< |
sum += r->rot; |
316 |
< |
return(sum * funcxf.sca); |
317 |
< |
} |
400 |
> |
if (n == 24) /* single ray (shadow) distance */ |
401 |
> |
return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca); |
402 |
> |
|
403 |
> |
if (n <= 26) /* local (u,v) coordinates */ |
404 |
> |
return(fray->uv[n-25]); |
405 |
|
badchan: |
406 |
|
error(USER, "illegal channel number"); |
407 |
+ |
return(0.0); |
408 |
|
} |