| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: func.c,v 2.21 2004/02/12 18:55:50 greg Exp $"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* func.c - interface to calcomp functions. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "copyright.h" |
| 9 |
|
| 10 |
#include <math.h> |
| 11 |
|
| 12 |
#include "ray.h" |
| 13 |
#include "paths.h" |
| 14 |
#include "otypes.h" |
| 15 |
#include "func.h" |
| 16 |
|
| 17 |
|
| 18 |
#define INITFILE "rayinit.cal" |
| 19 |
#define CALSUF ".cal" |
| 20 |
#define LCALSUF 4 |
| 21 |
char REFVNAME[] = "`FILE_REFCNT"; |
| 22 |
|
| 23 |
XF unitxf = { /* identity transform */ |
| 24 |
{{1.0, 0.0, 0.0, 0.0}, |
| 25 |
{0.0, 1.0, 0.0, 0.0}, |
| 26 |
{0.0, 0.0, 1.0, 0.0}, |
| 27 |
{0.0, 0.0, 0.0, 1.0}}, |
| 28 |
1.0 |
| 29 |
}; |
| 30 |
|
| 31 |
XF funcxf; /* current transformation */ |
| 32 |
static OBJREC *fobj = NULL; /* current function object */ |
| 33 |
static RAY *fray = NULL; /* current function ray */ |
| 34 |
|
| 35 |
static double l_erf(char *), l_erfc(char *), l_arg(char *); |
| 36 |
|
| 37 |
|
| 38 |
extern MFUNC * |
| 39 |
getfunc( /* get function for this modifier */ |
| 40 |
OBJREC *m, |
| 41 |
int ff, |
| 42 |
unsigned int ef, |
| 43 |
int dofwd |
| 44 |
) |
| 45 |
{ |
| 46 |
static char initfile[] = INITFILE; |
| 47 |
char sbuf[MAXSTR]; |
| 48 |
register char **arg; |
| 49 |
register MFUNC *f; |
| 50 |
int ne, na; |
| 51 |
register int i; |
| 52 |
/* check to see if done already */ |
| 53 |
if ((f = (MFUNC *)m->os) != NULL) |
| 54 |
return(f); |
| 55 |
fobj = NULL; fray = NULL; |
| 56 |
if (initfile[0]) { /* initialize on first call */ |
| 57 |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; |
| 58 |
esupport &= ~(E_OUTCHAN); |
| 59 |
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 |
scompile("Lu=$26;Lv=$27;", NULL, 0); |
| 69 |
funset("arg", 1, '=', l_arg); |
| 70 |
funset("erf", 1, ':', l_erf); |
| 71 |
funset("erfc", 1, ':', l_erfc); |
| 72 |
setnoisefuncs(); |
| 73 |
setprismfuncs(); |
| 74 |
loadfunc(initfile); |
| 75 |
initfile[0] = '\0'; |
| 76 |
} |
| 77 |
if ((na = m->oargs.nsargs) <= ff) |
| 78 |
goto toofew; |
| 79 |
arg = m->oargs.sarg; |
| 80 |
if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL) |
| 81 |
goto memerr; |
| 82 |
i = strlen(arg[ff]); /* set up context */ |
| 83 |
if (i == 1 && arg[ff][0] == '.') |
| 84 |
setcontext(f->ctx = ""); /* "." means no file */ |
| 85 |
else { |
| 86 |
strcpy(sbuf,arg[ff]); /* file name is context */ |
| 87 |
if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF)) |
| 88 |
sbuf[i-LCALSUF] = '\0'; /* remove suffix */ |
| 89 |
setcontext(f->ctx = savestr(sbuf)); |
| 90 |
if (!vardefined(REFVNAME)) { /* file loaded? */ |
| 91 |
loadfunc(arg[ff]); |
| 92 |
varset(REFVNAME, '=', 1.0); |
| 93 |
} else /* reference_count++ */ |
| 94 |
varset(REFVNAME, '=', varvalue(REFVNAME)+1.0); |
| 95 |
} |
| 96 |
curfunc = NULL; /* parse expressions */ |
| 97 |
sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname); |
| 98 |
for (i=0, ne=0; ef && i < na; i++, ef>>=1) |
| 99 |
if (ef & 1) { /* flagged as an expression? */ |
| 100 |
if (ne >= MAXEXPR) |
| 101 |
objerror(m, INTERNAL, "too many expressions"); |
| 102 |
initstr(arg[i], sbuf, 0); |
| 103 |
f->ep[ne++] = getE1(); |
| 104 |
if (nextc != EOF) |
| 105 |
syntax("unexpected character"); |
| 106 |
} |
| 107 |
if (ef) |
| 108 |
goto toofew; |
| 109 |
if (i <= ff) /* find transform args */ |
| 110 |
i = ff+1; |
| 111 |
while (i < na && arg[i][0] != '-') |
| 112 |
i++; |
| 113 |
if (i == na) /* no transform */ |
| 114 |
f->f = f->b = &unitxf; |
| 115 |
else { /* get transform */ |
| 116 |
if ((f->b = (XF *)malloc(sizeof(XF))) == NULL) |
| 117 |
goto memerr; |
| 118 |
if (invxf(f->b, na-i, arg+i) != na-i) |
| 119 |
objerror(m, USER, "bad transform"); |
| 120 |
if (f->b->sca < 0.0) |
| 121 |
f->b->sca = -f->b->sca; |
| 122 |
if (dofwd) { /* do both transforms */ |
| 123 |
if ((f->f = (XF *)malloc(sizeof(XF))) == NULL) |
| 124 |
goto memerr; |
| 125 |
xf(f->f, na-i, arg+i); |
| 126 |
if (f->f->sca < 0.0) |
| 127 |
f->f->sca = -f->f->sca; |
| 128 |
} |
| 129 |
} |
| 130 |
m->os = (char *)f; |
| 131 |
return(f); |
| 132 |
toofew: |
| 133 |
objerror(m, USER, "too few string arguments"); |
| 134 |
memerr: |
| 135 |
error(SYSTEM, "out of memory in getfunc"); |
| 136 |
return NULL; /* pro forma return */ |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
extern void |
| 141 |
freefunc( /* free memory associated with modifier */ |
| 142 |
OBJREC *m |
| 143 |
) |
| 144 |
{ |
| 145 |
register MFUNC *f; |
| 146 |
register int i; |
| 147 |
|
| 148 |
if ((f = (MFUNC *)m->os) == NULL) |
| 149 |
return; |
| 150 |
for (i = 0; f->ep[i] != NULL; i++) |
| 151 |
epfree(f->ep[i]); |
| 152 |
if (f->ctx[0]) { /* done with definitions */ |
| 153 |
setcontext(f->ctx); |
| 154 |
i = varvalue(REFVNAME)-.5; /* reference_count-- */ |
| 155 |
if (i > 0) |
| 156 |
varset(REFVNAME, '=', (double)i); |
| 157 |
else |
| 158 |
dcleanup(2); /* remove definitions */ |
| 159 |
freestr(f->ctx); |
| 160 |
} |
| 161 |
if (f->b != &unitxf) |
| 162 |
free((void *)f->b); |
| 163 |
if (f->f != NULL && f->f != &unitxf) |
| 164 |
free((void *)f->f); |
| 165 |
free((void *)f); |
| 166 |
m->os = NULL; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
extern int |
| 171 |
setfunc( /* set channels for function call */ |
| 172 |
OBJREC *m, |
| 173 |
register RAY *r |
| 174 |
) |
| 175 |
{ |
| 176 |
static unsigned long lastrno = ~0; |
| 177 |
register MFUNC *f; |
| 178 |
/* get function */ |
| 179 |
if ((f = (MFUNC *)m->os) == NULL) |
| 180 |
objerror(m, CONSISTENCY, "setfunc called before getfunc"); |
| 181 |
/* set evaluator context */ |
| 182 |
setcontext(f->ctx); |
| 183 |
/* check to see if matrix set */ |
| 184 |
if (m == fobj && r->rno == lastrno) |
| 185 |
return(0); |
| 186 |
fobj = m; |
| 187 |
fray = r; |
| 188 |
if (r->rox != NULL) |
| 189 |
if (f->b != &unitxf) { |
| 190 |
funcxf.sca = r->rox->b.sca * f->b->sca; |
| 191 |
multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm); |
| 192 |
} else |
| 193 |
funcxf = r->rox->b; |
| 194 |
else |
| 195 |
funcxf = *(f->b); |
| 196 |
lastrno = r->rno; |
| 197 |
eclock++; /* notify expression evaluator */ |
| 198 |
return(1); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
extern void |
| 203 |
loadfunc( /* load definition file */ |
| 204 |
char *fname |
| 205 |
) |
| 206 |
{ |
| 207 |
char *ffname; |
| 208 |
|
| 209 |
if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) { |
| 210 |
sprintf(errmsg, "cannot find function file \"%s\"", fname); |
| 211 |
error(USER, errmsg); |
| 212 |
} |
| 213 |
fcompile(ffname); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
static double |
| 218 |
l_arg(char *nm) /* return nth real argument */ |
| 219 |
{ |
| 220 |
register int n; |
| 221 |
|
| 222 |
if (fobj == NULL) |
| 223 |
syntax("arg(n) used in constant expression"); |
| 224 |
|
| 225 |
n = argument(1) + .5; /* round to integer */ |
| 226 |
|
| 227 |
if (n < 1) |
| 228 |
return(fobj->oargs.nfargs); |
| 229 |
|
| 230 |
if (n > fobj->oargs.nfargs) { |
| 231 |
sprintf(errmsg, "missing real argument %d", n); |
| 232 |
objerror(fobj, USER, errmsg); |
| 233 |
} |
| 234 |
return(fobj->oargs.farg[n-1]); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
static double |
| 239 |
l_erf(char *nm) /* error function */ |
| 240 |
{ |
| 241 |
return(erf(argument(1))); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
static double |
| 246 |
l_erfc(char *nm) /* cumulative error function */ |
| 247 |
{ |
| 248 |
return(erfc(argument(1))); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
extern double |
| 253 |
chanvalue( /* return channel n to calcomp */ |
| 254 |
register int n |
| 255 |
) |
| 256 |
{ |
| 257 |
if (fray == NULL) |
| 258 |
syntax("ray parameter used in constant expression"); |
| 259 |
|
| 260 |
if (--n < 0) |
| 261 |
goto badchan; |
| 262 |
|
| 263 |
if (n <= 2) /* ray direction */ |
| 264 |
|
| 265 |
return( ( fray->rdir[0]*funcxf.xfm[0][n] + |
| 266 |
fray->rdir[1]*funcxf.xfm[1][n] + |
| 267 |
fray->rdir[2]*funcxf.xfm[2][n] ) |
| 268 |
/ funcxf.sca ); |
| 269 |
|
| 270 |
if (n <= 5) /* surface normal */ |
| 271 |
|
| 272 |
return( ( fray->ron[0]*funcxf.xfm[0][n-3] + |
| 273 |
fray->ron[1]*funcxf.xfm[1][n-3] + |
| 274 |
fray->ron[2]*funcxf.xfm[2][n-3] ) |
| 275 |
/ funcxf.sca ); |
| 276 |
|
| 277 |
if (n <= 8) /* intersection */ |
| 278 |
|
| 279 |
return( fray->rop[0]*funcxf.xfm[0][n-6] + |
| 280 |
fray->rop[1]*funcxf.xfm[1][n-6] + |
| 281 |
fray->rop[2]*funcxf.xfm[2][n-6] + |
| 282 |
funcxf.xfm[3][n-6] ); |
| 283 |
|
| 284 |
if (n == 9) /* total distance */ |
| 285 |
return(raydist(fray,PRIMARY) * funcxf.sca); |
| 286 |
|
| 287 |
if (n == 10) /* dot product (range [-1,1]) */ |
| 288 |
return( fray->rod <= -1.0 ? -1.0 : |
| 289 |
fray->rod >= 1.0 ? 1.0 : |
| 290 |
fray->rod ); |
| 291 |
|
| 292 |
if (n == 11) /* scale */ |
| 293 |
return(funcxf.sca); |
| 294 |
|
| 295 |
if (n <= 14) /* origin */ |
| 296 |
return(funcxf.xfm[3][n-12]); |
| 297 |
|
| 298 |
if (n <= 17) /* i unit vector */ |
| 299 |
return(funcxf.xfm[0][n-15] / funcxf.sca); |
| 300 |
|
| 301 |
if (n <= 20) /* j unit vector */ |
| 302 |
return(funcxf.xfm[1][n-18] / funcxf.sca); |
| 303 |
|
| 304 |
if (n <= 23) /* k unit vector */ |
| 305 |
return(funcxf.xfm[2][n-21] / funcxf.sca); |
| 306 |
|
| 307 |
if (n == 24) /* single ray (shadow) distance */ |
| 308 |
return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca); |
| 309 |
|
| 310 |
if (n <= 26) /* local (u,v) coordinates */ |
| 311 |
return(fray->uv[n-25]); |
| 312 |
badchan: |
| 313 |
error(USER, "illegal channel number"); |
| 314 |
return(0.0); |
| 315 |
} |