| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
greg |
2.12 |
static const char RCSid[] = "$Id: p_func.c,v 2.11 2023/12/13 23:26:16 greg Exp $"; |
| 3 |
greg |
1.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* p_func.c - routine for procedural patterns. |
| 6 |
greg |
2.4 |
*/ |
| 7 |
|
|
|
| 8 |
greg |
2.5 |
#include "copyright.h" |
| 9 |
greg |
1.1 |
|
| 10 |
|
|
#include "ray.h" |
| 11 |
greg |
2.2 |
#include "func.h" |
| 12 |
greg |
2.10 |
#include "random.h" |
| 13 |
schorsch |
2.7 |
#include "rtotypes.h" |
| 14 |
greg |
2.2 |
|
| 15 |
greg |
1.1 |
/* |
| 16 |
|
|
* A procedural pattern can either be a brightness or a |
| 17 |
|
|
* color function. A brightness function is given as: |
| 18 |
|
|
* |
| 19 |
|
|
* modifier brightfunc name |
| 20 |
|
|
* 2+ bvarname filename xf |
| 21 |
|
|
* 0 |
| 22 |
|
|
* n A1 A2 .. |
| 23 |
|
|
* |
| 24 |
|
|
* A color function is given as: |
| 25 |
|
|
* |
| 26 |
|
|
* modifier colorfunc name |
| 27 |
|
|
* 4+ rvarname gvarname bvarname filename xf |
| 28 |
|
|
* 0 |
| 29 |
|
|
* n A1 A2 .. |
| 30 |
|
|
* |
| 31 |
greg |
2.9 |
* A spectral function is given as: |
| 32 |
|
|
* |
| 33 |
|
|
* modifier specfunc name |
| 34 |
greg |
2.11 |
* 2+ sfunc filename xf |
| 35 |
greg |
2.9 |
* 0 |
| 36 |
|
|
* 2+ nmA nmB A3 .. |
| 37 |
|
|
* |
| 38 |
greg |
1.1 |
* Filename is the name of the file where the variable definitions |
| 39 |
|
|
* can be found. The list of real arguments can be accessed by |
| 40 |
|
|
* definitions in the file. The xf is a transformation |
| 41 |
|
|
* to get from the original coordinates to the current coordinates. |
| 42 |
greg |
2.11 |
* For the "specfunc" primitive, sfunc(nm) is a function of wavelength |
| 43 |
greg |
2.9 |
* and must be defined from nmA to nmB, and should average to 1 over |
| 44 |
|
|
* its range. |
| 45 |
greg |
1.1 |
*/ |
| 46 |
|
|
|
| 47 |
|
|
|
| 48 |
greg |
2.8 |
int |
| 49 |
schorsch |
2.7 |
p_bfunc( /* compute brightness pattern */ |
| 50 |
|
|
OBJREC *m, |
| 51 |
|
|
RAY *r |
| 52 |
|
|
) |
| 53 |
greg |
1.1 |
{ |
| 54 |
|
|
double bval; |
| 55 |
greg |
2.8 |
MFUNC *mf; |
| 56 |
greg |
1.1 |
|
| 57 |
|
|
if (m->oargs.nsargs < 2) |
| 58 |
|
|
objerror(m, USER, "bad # arguments"); |
| 59 |
greg |
2.2 |
mf = getfunc(m, 1, 0x1, 0); |
| 60 |
|
|
setfunc(m, r); |
| 61 |
greg |
1.1 |
errno = 0; |
| 62 |
greg |
2.2 |
bval = evalue(mf->ep[0]); |
| 63 |
greg |
2.9 |
if ((errno == EDOM) | (errno == ERANGE)) { |
| 64 |
greg |
1.1 |
objerror(m, WARNING, "compute error"); |
| 65 |
greg |
2.3 |
return(0); |
| 66 |
greg |
1.1 |
} |
| 67 |
greg |
2.9 |
scalescolor(r->pcol, bval); |
| 68 |
greg |
2.3 |
return(0); |
| 69 |
greg |
1.1 |
} |
| 70 |
|
|
|
| 71 |
|
|
|
| 72 |
greg |
2.8 |
int |
| 73 |
schorsch |
2.7 |
p_cfunc( /* compute color pattern */ |
| 74 |
|
|
OBJREC *m, |
| 75 |
|
|
RAY *r |
| 76 |
|
|
) |
| 77 |
greg |
1.1 |
{ |
| 78 |
greg |
2.9 |
SCOLOR scval; |
| 79 |
greg |
2.8 |
MFUNC *mf; |
| 80 |
greg |
1.1 |
|
| 81 |
|
|
if (m->oargs.nsargs < 4) |
| 82 |
|
|
objerror(m, USER, "bad # arguments"); |
| 83 |
greg |
2.2 |
mf = getfunc(m, 3, 0x7, 0); |
| 84 |
|
|
setfunc(m, r); |
| 85 |
greg |
1.1 |
errno = 0; |
| 86 |
greg |
2.9 |
setscolor(scval, evalue(mf->ep[0]), |
| 87 |
greg |
2.2 |
evalue(mf->ep[1]), |
| 88 |
|
|
evalue(mf->ep[2])); |
| 89 |
greg |
2.9 |
if ((errno == EDOM) | (errno == ERANGE)) { |
| 90 |
greg |
1.1 |
objerror(m, WARNING, "compute error"); |
| 91 |
greg |
2.3 |
return(0); |
| 92 |
greg |
1.1 |
} |
| 93 |
greg |
2.9 |
smultscolor(r->pcol, scval); |
| 94 |
|
|
return(0); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
|
| 98 |
|
|
int |
| 99 |
|
|
p_specfunc( /* compute spectral pattern */ |
| 100 |
|
|
OBJREC *m, |
| 101 |
|
|
RAY *r |
| 102 |
|
|
) |
| 103 |
|
|
{ |
| 104 |
|
|
SCOLOR scsamp; |
| 105 |
|
|
SCOLOR scval; |
| 106 |
|
|
double wl, wlmin, wlmax, wlstep; |
| 107 |
|
|
int ns, i; |
| 108 |
|
|
|
| 109 |
|
|
if ((m->oargs.nsargs < 2) | (m->oargs.nfargs < 2)) |
| 110 |
|
|
objerror(m, USER, "bad # arguments"); |
| 111 |
|
|
if (m->oargs.farg[0] < m->oargs.farg[1]) { |
| 112 |
|
|
wlmin = m->oargs.farg[0]; |
| 113 |
|
|
wlmax = m->oargs.farg[1]; |
| 114 |
|
|
} else { |
| 115 |
|
|
wlmin = m->oargs.farg[1]; |
| 116 |
|
|
wlmax = m->oargs.farg[0]; |
| 117 |
|
|
} |
| 118 |
|
|
if (wlmin < WLPART[3]) wlmin = WLPART[3]; |
| 119 |
|
|
if (wlmax > WLPART[0]) wlmax = WLPART[0]; |
| 120 |
|
|
if (wlmin >= wlmax) { |
| 121 |
|
|
objerror(m, WARNING, "incompatible wavelength sampling"); |
| 122 |
|
|
return(0); |
| 123 |
|
|
} |
| 124 |
|
|
wlstep = (wlmax - wlmin)/(double)MAXCSAMP; |
| 125 |
greg |
2.12 |
getfunc(m, 1, 0, 0); |
| 126 |
greg |
2.9 |
setfunc(m, r); |
| 127 |
|
|
errno = 0; |
| 128 |
|
|
ns = (wlmax - wlmin)/wlstep + .1; |
| 129 |
|
|
wl = wlmax - .5*wlstep; |
| 130 |
|
|
for (i = ns; i-- > 0; wl -= wlstep) { |
| 131 |
greg |
2.12 |
double ws = wl + 0.9*(.5-frandom())*wlstep; |
| 132 |
greg |
2.10 |
scsamp[i] = funvalue(m->oargs.sarg[0], 1, &ws); |
| 133 |
greg |
2.9 |
if ((errno == EDOM) | (errno == ERANGE)) { |
| 134 |
|
|
objerror(m, WARNING, "compute error"); |
| 135 |
|
|
return(0); |
| 136 |
|
|
} |
| 137 |
|
|
} |
| 138 |
|
|
convertscolor(scval, NCSAMP, WLPART[0], WLPART[3], |
| 139 |
|
|
scsamp, ns, wlmin, wlmax); |
| 140 |
|
|
smultscolor(r->pcol, scval); |
| 141 |
greg |
2.3 |
return(0); |
| 142 |
greg |
1.1 |
} |