ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/p_data.c
Revision: 2.15
Committed: Tue Mar 12 16:54:51 2024 UTC (7 weeks, 3 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.14: +24 -24 lines
Log Message:
perf: added datavector() call for quicker spectral interpolation

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: p_data.c,v 2.14 2024/02/22 20:11:54 greg Exp $";
3 #endif
4 /*
5 * p_data.c - routine for stored patterns.
6 */
7
8 #include "copyright.h"
9
10 #include "ray.h"
11 #include "data.h"
12 #include "func.h"
13 #include "rtotypes.h"
14
15 /*
16 * A stored pattern can either be brightness,
17 * color, or spectral data. Brightness data is specified as:
18 *
19 * modifier brightdata name
20 * 4+ func dfname vfname v0 v1 .. xf
21 * 0
22 * n A1 A2 ..
23 *
24 * Color data is specified as:
25 *
26 * modifier colordata name
27 * 8+ rfunc gfunc bfunc rdfname gdfname bdfname vfname v0 v1 .. xf
28 * 0
29 * n A1 A2 ..
30 *
31 * Color picture data is specified as:
32 *
33 * modifier colorpict name
34 * 7+ rfunc gfunc bfunc pfname vfname vx vy xf
35 * 0
36 * n A1 A2 ..
37 *
38 * A simple spectrum is specified as:
39 *
40 * modifier spectrum name
41 * 0
42 * 0
43 * 5+ nmA nmB s1 s2 s3 ..
44 *
45 * A constant spectrum from a data file is given as:
46 *
47 * modifier specfile name
48 * 1 dfname
49 * 0
50 * 0
51 *
52 * A spectral data file is given as:
53 *
54 * modifier specdata name
55 * 4+ sfunc dfname vfname v0 .. xf
56 * 0
57 * n A1 A2 ..
58 *
59 * A spectral image is given as:
60 *
61 * modifier specpict name
62 * 5+ sfunc sfname vfname vx vy xf
63 * 0
64 * n A1 A2 ..
65 *
66 * Vfname is the name of the file where the variable definitions
67 * can be found. The list of real arguments can be accessed by
68 * definitions in the file. The dfnames are the data file
69 * names. The dimensions of the data files and the number
70 * of variables must match, except for specdata, which has a "hidden"
71 * last variable for the wavelength. The funcs take a single argument
72 * for brightdata, three for colordata and colorpict, and two for
73 * specdata and specpict to modify interpolated values from the file.
74 * The xf is a transform spec to get from the original coordinates to
75 * the current coordinates.
76 */
77
78
79 int
80 p_bdata( /* interpolate brightness data */
81 OBJREC *m,
82 RAY *r
83 )
84 {
85 double bval;
86 double pt[MAXDDIM];
87 DATARRAY *dp;
88 MFUNC *mf;
89 int i;
90
91 if (m->oargs.nsargs < 4)
92 objerror(m, USER, "bad # arguments");
93 dp = getdata(m->oargs.sarg[1]);
94 i = (1 << dp->nd) - 1;
95 mf = getfunc(m, 2, i<<3, 0);
96 setfunc(m, r);
97 errno = 0;
98 for (i = dp->nd; i-- > 0; ) {
99 pt[i] = evalue(mf->ep[i]);
100 if ((errno == EDOM) | (errno == ERANGE))
101 goto computerr;
102 }
103 bval = datavalue(dp, pt);
104 errno = 0;
105 bval = funvalue(m->oargs.sarg[0], 1, &bval);
106 if ((errno == EDOM) | (errno == ERANGE))
107 goto computerr;
108 scalescolor(r->pcol, bval);
109 return(0);
110 computerr:
111 objerror(m, WARNING, "compute error");
112 return(0);
113 }
114
115
116 int
117 p_cdata( /* interpolate color data */
118 OBJREC *m,
119 RAY *r
120 )
121 {
122 double col[3];
123 COLOR cval;
124 double pt[MAXDDIM];
125 int nv;
126 DATARRAY *dp;
127 MFUNC *mf;
128 int i;
129
130 if (m->oargs.nsargs < 8)
131 objerror(m, USER, "bad # arguments");
132 dp = getdata(m->oargs.sarg[3]);
133 i = (1 << (nv = dp->nd)) - 1;
134 mf = getfunc(m, 6, i<<7, 0);
135 setfunc(m, r);
136 errno = 0;
137 for (i = 0; i < nv; i++) {
138 pt[i] = evalue(mf->ep[i]);
139 if ((errno == EDOM) | (errno == ERANGE))
140 goto computerr;
141 }
142 col[0] = datavalue(dp, pt);
143 for (i = 1; i < 3; i++) {
144 if (!strcmp(m->oargs.sarg[3+i], m->oargs.sarg[3])) {
145 col[i] = col[0]; /* same data */
146 continue;
147 }
148 dp = getdata(m->oargs.sarg[3+i]);
149 if (dp->nd != nv)
150 objerror(m, USER, "dimension error");
151 col[i] = datavalue(dp, pt);
152 }
153 errno = 0;
154 for (i = 0; i < 3; i++)
155 if (i && fundefined(m->oargs.sarg[i]) < 3)
156 colval(cval,i) = funvalue(m->oargs.sarg[i], 1, col+i);
157 else
158 colval(cval,i) = funvalue(m->oargs.sarg[i], 3, col);
159 if ((errno == EDOM) | (errno == ERANGE))
160 goto computerr;
161 smultcolor(r->pcol, cval);
162 return(0);
163 computerr:
164 objerror(m, WARNING, "compute error");
165 return(0);
166 }
167
168
169 int
170 p_pdata( /* interpolate picture data */
171 OBJREC *m,
172 RAY *r
173 )
174 {
175 double col[3];
176 COLOR cval;
177 double pt[2];
178 DATARRAY *dp;
179 MFUNC *mf;
180 int i;
181
182 if (m->oargs.nsargs < 7)
183 objerror(m, USER, "bad # arguments");
184 mf = getfunc(m, 4, 0x3<<5, 0);
185 setfunc(m, r);
186 errno = 0;
187 pt[1] = evalue(mf->ep[0]); /* y major ordering */
188 pt[0] = evalue(mf->ep[1]);
189 if ((errno == EDOM) | (errno == ERANGE))
190 goto computerr;
191 dp = getpict(m->oargs.sarg[3]);
192 for (i = 0; i < 3; i++)
193 col[i] = datavalue(dp+i, pt);
194 errno = 0;
195 for (i = 0; i < 3; i++)
196 if (i && fundefined(m->oargs.sarg[i]) < 3)
197 colval(cval,i) = funvalue(m->oargs.sarg[i], 1, col+i);
198 else
199 colval(cval,i) = funvalue(m->oargs.sarg[i], 3, col);
200 if ((errno == EDOM) | (errno == ERANGE))
201 goto computerr;
202 smultcolor(r->pcol, cval);
203 return(0);
204
205 computerr:
206 objerror(m, WARNING, "compute error");
207 return(0);
208 }
209
210
211 int
212 p_spectrum( /* simple constant spectrum */
213 OBJREC *m,
214 RAY *r
215 )
216 {
217 COLORV *scval;
218
219 if ((scval = (COLORV *)m->os) == NULL) {
220 COLORV *sinp;
221 double hstep;
222 int i;
223 if (m->oargs.nfargs < 5)
224 objerror(m, USER, "bad # arguments");
225 sinp = (COLORV *)malloc(sizeof(COLORV)*(m->oargs.nfargs-2));
226 scval = (COLORV *)malloc(sizeof(COLORV)*NCSAMP);
227 if ((sinp == NULL) | (scval == NULL))
228 objerror(m, SYSTEM, "out of memory");
229 for (i = m->oargs.nfargs-2; i--; )
230 sinp[i] = (COLORV)m->oargs.farg[i+2];
231 hstep = 0.5 * (m->oargs.farg[1] - m->oargs.farg[0]) /
232 (m->oargs.nfargs-3.0);
233 convertscolorcol(scval, sinp, m->oargs.nfargs-2,
234 m->oargs.farg[0]-hstep, m->oargs.farg[1]+hstep);
235 free(sinp);
236 m->os = (void *)scval;
237 }
238 smultscolor(r->pcol, scval);
239 return(0);
240 }
241
242
243 int
244 p_specfile( /* constant spectrum from 1-D data file */
245 OBJREC *m,
246 RAY *r
247 )
248 {
249 COLORV *scval;
250
251 if ((scval = (COLORV *)m->os) == NULL) {
252 DATARRAY *dp;
253 COLORV *sinp;
254 double step;
255 int i;
256 if (m->oargs.nsargs != 1)
257 objerror(m, USER, "bad # arguments");
258 dp = getdata(m->oargs.sarg[0]);
259 if (dp->nd != 1)
260 objerror(m, USER, "data file must be 1-dimensional");
261
262 sinp = (COLORV *)malloc(sizeof(COLORV)*dp->dim[0].ne);
263 scval = (COLORV *)malloc(sizeof(COLORV)*NCSAMP);
264 if ((sinp == NULL) | (scval == NULL))
265 objerror(m, SYSTEM, "out of memory");
266 step = dp->dim[0].siz / (dp->dim[0].ne - 1.0);
267 for (i = dp->dim[0].ne; i-- > 0; ) {
268 double wl = dp->dim[0].org + i*step;
269 sinp[i] = (COLORV)datavalue(dp, &wl);
270 }
271 convertscolorcol(scval, sinp, dp->dim[0].ne,
272 dp->dim[0].org-.5*step,
273 dp->dim[0].org+dp->dim[0].siz+.5*step);
274 free(sinp);
275 m->os = (void *)scval;
276 }
277 smultscolor(r->pcol, scval);
278 return(0);
279 }
280
281
282 int
283 p_specdata( /* varied spectrum from (N+1)-D file */
284 OBJREC *m,
285 RAY *r
286 )
287 {
288 SCOLOR scval;
289 COLORV *scdat;
290 double pt[MAXDDIM];
291 DATARRAY *dp;
292 MFUNC *mf;
293 double step;
294 int i;
295
296 if (m->oargs.nsargs < 4)
297 objerror(m, USER, "bad # arguments");
298 dp = getdata(m->oargs.sarg[1]);
299 if (dp->nd < 2)
300 objerror(m, USER, "need at least 2-dimensional data");
301 i = (1 << (dp->nd-1)) - 1;
302 mf = getfunc(m, 2, i<<3, 0);
303 setfunc(m, r);
304 errno = 0;
305 for (i = dp->nd-1; i-- > 0; ) {
306 pt[i] = evalue(mf->ep[i]);
307 if ((errno == EDOM) | (errno == ERANGE))
308 goto computerr;
309 }
310 dp = datavector(dp, pt); /* interpolate spectrum */
311 step = dp->dim[0].siz / (dp->dim[0].ne - 1.0);
312 scdat = (COLORV *)malloc(sizeof(COLORV)*dp->dim[0].ne);
313 if (scdat == NULL)
314 objerror(m, SYSTEM, "out of memory");
315 for (i = dp->dim[0].ne; i-- > 0; ) {
316 pt[1] = dp->dim[0].org + i*step;
317 pt[0] = datavalue(dp, pt+1);
318 errno = 0;
319 scdat[i] = funvalue(m->oargs.sarg[0], 2, pt);
320 if ((errno == EDOM) | (errno == ERANGE))
321 goto computerr;
322 }
323 convertscolorcol(scval, scdat, dp->dim[0].ne,
324 dp->dim[0].org-.5*step,
325 dp->dim[0].org+dp->dim[0].siz+.5*step);
326 free(scdat);
327 free(dp);
328 smultscolor(r->pcol, scval);
329 return(0);
330 computerr:
331 objerror(m, WARNING, "compute error");
332 return(0);
333 }
334
335
336 int
337 p_specpict( /* interpolate hyperspectral image data */
338 OBJREC *m,
339 RAY *r
340 )
341 {
342 SCOLOR scdat, scval;
343 double pt[2];
344 DATARRAY *dp;
345 MFUNC *mf;
346 double step;
347 int i;
348
349 if (m->oargs.nsargs < 5)
350 objerror(m, USER, "bad # arguments");
351 mf = getfunc(m, 2, 0x3<<3, 0);
352 setfunc(m, r);
353 errno = 0;
354 pt[1] = evalue(mf->ep[0]); /* y major ordering */
355 pt[0] = evalue(mf->ep[1]);
356 if ((errno == EDOM) | (errno == ERANGE))
357 goto computerr;
358 /* interpolate spectrum */
359 dp = datavector(getspec(m->oargs.sarg[1]), pt);
360 step = dp->dim[0].siz / (dp->dim[0].ne - 1.0);
361 for (i = dp->dim[0].ne; i-- > 0; ) {
362 pt[1] = dp->dim[0].org + i*step;
363 pt[0] = dp->arr.d[i]; /* datavalue(dp, pt+1); */
364 errno = 0;
365 scdat[i] = funvalue(m->oargs.sarg[0], 2, pt);
366 if ((errno == EDOM) | (errno == ERANGE))
367 goto computerr;
368 }
369 convertscolorcol(scval, scdat, dp->dim[0].ne,
370 dp->dim[0].org-.5*step,
371 dp->dim[0].org+dp->dim[0].siz+.5*step);
372 free(dp);
373 smultscolor(r->pcol, scval);
374 return(0);
375 computerr:
376 objerror(m, WARNING, "compute error");
377 return(0);
378 }