ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/rt/p_data.c
Revision: 2.16
Committed: Wed Dec 17 00:45:55 2025 UTC (13 days, 11 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.15: +8 -7 lines
Log Message:
perf: Added higher-resolution interpolation for non-uniform spectral samples

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: p_data.c,v 2.15 2024/03/12 16:54:51 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 int n;
255 double step;
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 n = (dp->dim[0].p == NULL) ? dp->dim[0].ne : 2*dp->dim[0].ne;
263 sinp = (COLORV *)malloc(sizeof(COLORV)*n);
264 scval = (COLORV *)malloc(sizeof(COLORV)*NCSAMP);
265 if ((sinp == NULL) | (scval == NULL))
266 objerror(m, SYSTEM, "out of memory");
267 step = dp->dim[0].siz / (n - 1.0);
268 while (n--) {
269 double wl = dp->dim[0].org + n*step;
270 sinp[n] = (COLORV)datavalue(dp, &wl);
271 }
272 convertscolorcol(scval, sinp, dp->dim[0].ne,
273 dp->dim[0].org-.5*step,
274 dp->dim[0].org+dp->dim[0].siz+.5*step);
275 free(sinp);
276 m->os = (void *)scval;
277 }
278 smultscolor(r->pcol, scval);
279 return(0);
280 }
281
282
283 int
284 p_specdata( /* varied spectrum from (N+1)-D file */
285 OBJREC *m,
286 RAY *r
287 )
288 {
289 SCOLOR scval;
290 COLORV *scdat;
291 double pt[MAXDDIM];
292 DATARRAY *dp;
293 MFUNC *mf;
294 double step;
295 int i;
296
297 if (m->oargs.nsargs < 4)
298 objerror(m, USER, "bad # arguments");
299 dp = getdata(m->oargs.sarg[1]);
300 if (dp->nd < 2)
301 objerror(m, USER, "need at least 2-dimensional data");
302 i = (1 << (dp->nd-1)) - 1;
303 mf = getfunc(m, 2, i<<3, 0);
304 setfunc(m, r);
305 errno = 0;
306 for (i = dp->nd-1; i-- > 0; ) {
307 pt[i] = evalue(mf->ep[i]);
308 if ((errno == EDOM) | (errno == ERANGE))
309 goto computerr;
310 }
311 dp = datavector(dp, pt); /* interpolate spectrum */
312 step = dp->dim[0].siz / (dp->dim[0].ne - 1.0);
313 scdat = (COLORV *)malloc(sizeof(COLORV)*dp->dim[0].ne);
314 if (scdat == NULL)
315 objerror(m, SYSTEM, "out of memory");
316 for (i = dp->dim[0].ne; i-- > 0; ) {
317 pt[1] = dp->dim[0].org + i*step;
318 pt[0] = datavalue(dp, pt+1);
319 errno = 0;
320 scdat[i] = funvalue(m->oargs.sarg[0], 2, pt);
321 if ((errno == EDOM) | (errno == ERANGE))
322 goto computerr;
323 }
324 convertscolorcol(scval, scdat, dp->dim[0].ne,
325 dp->dim[0].org-.5*step,
326 dp->dim[0].org+dp->dim[0].siz+.5*step);
327 free(scdat);
328 free(dp);
329 smultscolor(r->pcol, scval);
330 return(0);
331 computerr:
332 objerror(m, WARNING, "compute error");
333 return(0);
334 }
335
336
337 int
338 p_specpict( /* interpolate hyperspectral image data */
339 OBJREC *m,
340 RAY *r
341 )
342 {
343 SCOLOR scdat, scval;
344 double pt[2];
345 DATARRAY *dp;
346 MFUNC *mf;
347 double step;
348 int i;
349
350 if (m->oargs.nsargs < 5)
351 objerror(m, USER, "bad # arguments");
352 mf = getfunc(m, 2, 0x3<<3, 0);
353 setfunc(m, r);
354 errno = 0;
355 pt[1] = evalue(mf->ep[0]); /* y major ordering */
356 pt[0] = evalue(mf->ep[1]);
357 if ((errno == EDOM) | (errno == ERANGE))
358 goto computerr;
359 /* interpolate spectrum */
360 dp = datavector(getspec(m->oargs.sarg[1]), pt);
361 step = dp->dim[0].siz / (dp->dim[0].ne - 1.0);
362 for (i = dp->dim[0].ne; i-- > 0; ) {
363 pt[1] = dp->dim[0].org + i*step;
364 pt[0] = dp->arr.d[i]; /* datavalue(dp, pt+1); */
365 errno = 0;
366 scdat[i] = funvalue(m->oargs.sarg[0], 2, pt);
367 if ((errno == EDOM) | (errno == ERANGE))
368 goto computerr;
369 }
370 convertscolorcol(scval, scdat, dp->dim[0].ne,
371 dp->dim[0].org-.5*step,
372 dp->dim[0].org+dp->dim[0].siz+.5*step);
373 free(dp);
374 smultscolor(r->pcol, scval);
375 return(0);
376 computerr:
377 objerror(m, WARNING, "compute error");
378 return(0);
379 }