ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
Revision: 2.26
Committed: Fri Jan 2 11:43:42 2004 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.25: +10 -7 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: data.c,v 2.25 2003/10/22 02:06:35 greg Exp $";
3 #endif
4 /*
5 * data.c - routines dealing with interpolated data.
6 */
7
8 #include "copyright.h"
9
10 #include <time.h>
11
12 #include "platform.h"
13 #include "standard.h"
14 #include "color.h"
15 #include "resolu.h"
16 #include "data.h"
17
18 /* picture memory usage before warning */
19 #ifndef PSIZWARN
20 #ifdef SMLMEM
21 #define PSIZWARN 1500000
22 #else
23 #define PSIZWARN 5000000
24 #endif
25 #endif
26
27 #ifndef TABSIZ
28 #define TABSIZ 97 /* table size (prime) */
29 #endif
30
31 #define hash(s) (shash(s)%TABSIZ)
32
33
34 static DATARRAY *dtab[TABSIZ]; /* data array list */
35
36 static gethfunc headaspect;
37
38
39 DATARRAY *
40 getdata(dname) /* get data array dname */
41 char *dname;
42 {
43 char *dfname;
44 FILE *fp;
45 int asize;
46 register int i, j;
47 register DATARRAY *dp;
48 /* look for array in list */
49 for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
50 if (!strcmp(dname, dp->name))
51 return(dp); /* found! */
52 /*
53 * If we haven't loaded the data already, we will look
54 * for it in the directories specified by the library path.
55 *
56 * The file has the following format:
57 *
58 * N
59 * beg0 end0 n0
60 * beg1 end1 n1
61 * . . .
62 * begN endN nN
63 * data, later dimensions changing faster
64 * . . .
65 *
66 * For irregularly spaced points, the following can be
67 * substituted for begi endi ni:
68 *
69 * 0 0 ni p0i p1i .. pni
70 */
71
72 if ((dfname = getpath(dname, getrlibpath(), R_OK)) == NULL) {
73 sprintf(errmsg, "cannot find data file \"%s\"", dname);
74 error(USER, errmsg);
75 }
76 if ((fp = fopen(dfname, "r")) == NULL) {
77 sprintf(errmsg, "cannot open data file \"%s\"", dfname);
78 error(SYSTEM, errmsg);
79 }
80 /* get dimensions */
81 if (fgetval(fp, 'i', (char *)&asize) <= 0)
82 goto scanerr;
83 if ((asize <= 0) | (asize > MAXDDIM)) {
84 sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
85 error(USER, errmsg);
86 }
87 if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
88 goto memerr;
89 dp->name = savestr(dname);
90 dp->type = DATATY;
91 dp->nd = asize;
92 asize = 1;
93 for (i = 0; i < dp->nd; i++) {
94 if (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0)
95 goto scanerr;
96 if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0)
97 goto scanerr;
98 if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
99 goto scanerr;
100 if (dp->dim[i].ne < 2)
101 goto scanerr;
102 asize *= dp->dim[i].ne;
103 if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
104 dp->dim[i].p = (DATATYPE *)
105 malloc(dp->dim[i].ne*sizeof(DATATYPE));
106 if (dp->dim[i].p == NULL)
107 goto memerr;
108 for (j = 0; j < dp->dim[i].ne; j++)
109 if (fgetval(fp, DATATY,
110 (char *)&dp->dim[i].p[j]) <= 0)
111 goto scanerr;
112 for (j = 1; j < dp->dim[i].ne-1; j++)
113 if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
114 (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
115 goto scanerr;
116 dp->dim[i].org = dp->dim[i].p[0];
117 dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
118 - dp->dim[i].p[0];
119 } else
120 dp->dim[i].p = NULL;
121 }
122 if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
123 goto memerr;
124
125 for (i = 0; i < asize; i++)
126 if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0)
127 goto scanerr;
128 fclose(fp);
129 i = hash(dname);
130 dp->next = dtab[i];
131 return(dtab[i] = dp);
132
133 memerr:
134 error(SYSTEM, "out of memory in getdata");
135 scanerr:
136 sprintf(errmsg, "%s in data file \"%s\"",
137 feof(fp) ? "unexpected EOF" : "bad format", dfname);
138 error(USER, errmsg);
139 }
140
141
142 static int
143 headaspect( /* check string for aspect ratio */
144 char *s,
145 void *iap
146 )
147 {
148 char fmt[32];
149
150 if (isaspect(s))
151 *(double*)iap *= aspectval(s);
152 else if (formatval(fmt, s) && !globmatch(PICFMT, fmt))
153 *(double*)iap = 0.0;
154 return(0);
155 }
156
157
158 DATARRAY *
159 getpict(pname) /* get picture pname */
160 char *pname;
161 {
162 double inpaspect;
163 char *pfname;
164 FILE *fp;
165 COLR *scanin;
166 int sl, ns;
167 RESOLU inpres;
168 RREAL loc[2];
169 int y;
170 register int x, i;
171 register DATARRAY *pp;
172 /* look for array in list */
173 for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next)
174 if (!strcmp(pname, pp->name))
175 return(pp); /* found! */
176
177 if ((pfname = getpath(pname, getrlibpath(), R_OK)) == NULL) {
178 sprintf(errmsg, "cannot find picture file \"%s\"", pname);
179 error(USER, errmsg);
180 }
181 if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
182 goto memerr;
183
184 pp[0].name = savestr(pname);
185
186 if ((fp = fopen(pfname, "r")) == NULL) {
187 sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
188 error(SYSTEM, errmsg);
189 }
190 SET_FILE_BINARY(fp);
191 /* get dimensions */
192 inpaspect = 1.0;
193 getheader(fp, headaspect, &inpaspect);
194 if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
195 goto readerr;
196 pp[0].nd = 2;
197 pp[0].dim[0].ne = inpres.yr;
198 pp[0].dim[1].ne = inpres.xr;
199 pp[0].dim[0].org =
200 pp[0].dim[1].org = 0.0;
201 if (inpres.xr <= inpres.yr*inpaspect) {
202 pp[0].dim[0].siz = inpaspect *
203 (double)inpres.yr/inpres.xr;
204 pp[0].dim[1].siz = 1.0;
205 } else {
206 pp[0].dim[0].siz = 1.0;
207 pp[0].dim[1].siz = (double)inpres.xr/inpres.yr /
208 inpaspect;
209 }
210 pp[0].dim[0].p = pp[0].dim[1].p = NULL;
211 sl = scanlen(&inpres); /* allocate array */
212 ns = numscans(&inpres);
213 i = ns*sl*sizeof(COLR);
214 #if PSIZWARN
215 if (i > PSIZWARN) { /* memory warning */
216 sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
217 pname, i);
218 error(WARNING, errmsg);
219 }
220 #endif
221 if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
222 goto memerr;
223 /* load picture */
224 if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
225 goto memerr;
226 for (y = 0; y < ns; y++) {
227 if (freadcolrs(scanin, sl, fp) < 0)
228 goto readerr;
229 for (x = 0; x < sl; x++) {
230 pix2loc(loc, &inpres, x, y);
231 i = (int)(loc[1]*inpres.yr)*inpres.xr +
232 (int)(loc[0]*inpres.xr);
233 copycolr(pp[0].arr.c[i], scanin[x]);
234 }
235 }
236 free((void *)scanin);
237 fclose(fp);
238 i = hash(pname);
239 pp[0].next = dtab[i]; /* link into picture list */
240 pp[1] = pp[0];
241 pp[2] = pp[0];
242 pp[0].type = RED; /* differentiate RGB records */
243 pp[1].type = GRN;
244 pp[2].type = BLU;
245 return(dtab[i] = pp);
246
247 memerr:
248 error(SYSTEM, "out of memory in getpict");
249 readerr:
250 sprintf(errmsg, "bad picture file \"%s\"", pfname);
251 error(USER, errmsg);
252 }
253
254
255 void
256 freedata(dta) /* release data array reference */
257 DATARRAY *dta;
258 {
259 DATARRAY head;
260 int hval, nents;
261 register DATARRAY *dpl, *dp;
262 register int i;
263
264 if (dta == NULL) { /* free all if NULL */
265 hval = 0; nents = TABSIZ;
266 } else {
267 hval = hash(dta->name); nents = 1;
268 }
269 while (nents--) {
270 head.next = dtab[hval];
271 dpl = &head;
272 while ((dp = dpl->next) != NULL)
273 if ((dta == NULL) | (dta == dp)) {
274 dpl->next = dp->next;
275 if (dp->type == DATATY)
276 free((void *)dp->arr.d);
277 else
278 free((void *)dp->arr.c);
279 for (i = 0; i < dp->nd; i++)
280 if (dp->dim[i].p != NULL)
281 free((void *)dp->dim[i].p);
282 freestr(dp->name);
283 free((void *)dp);
284 } else
285 dpl = dp;
286 dtab[hval++] = head.next;
287 }
288 }
289
290
291 double
292 datavalue(dp, pt) /* interpolate data value at a point */
293 register DATARRAY *dp;
294 double *pt;
295 {
296 DATARRAY sd;
297 int asize;
298 int lower, upper;
299 register int i;
300 double x, y0, y1;
301 /* set up dimensions for recursion */
302 if (dp->nd > 1) {
303 sd.name = dp->name;
304 sd.type = dp->type;
305 sd.nd = dp->nd - 1;
306 asize = 1;
307 for (i = 0; i < sd.nd; i++) {
308 sd.dim[i].org = dp->dim[i+1].org;
309 sd.dim[i].siz = dp->dim[i+1].siz;
310 sd.dim[i].p = dp->dim[i+1].p;
311 asize *= sd.dim[i].ne = dp->dim[i+1].ne;
312 }
313 }
314 /* get independent variable */
315 if (dp->dim[0].p == NULL) { /* evenly spaced points */
316 x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
317 x *= (double)(dp->dim[0].ne - 1);
318 i = x;
319 if (i < 0)
320 i = 0;
321 else if (i > dp->dim[0].ne - 2)
322 i = dp->dim[0].ne - 2;
323 } else { /* unevenly spaced points */
324 if (dp->dim[0].siz > 0.0) {
325 lower = 0;
326 upper = dp->dim[0].ne;
327 } else {
328 lower = dp->dim[0].ne;
329 upper = 0;
330 }
331 do {
332 i = (lower + upper) >> 1;
333 if (pt[0] >= dp->dim[0].p[i])
334 lower = i;
335 else
336 upper = i;
337 } while (i != (lower + upper) >> 1);
338 if (i > dp->dim[0].ne - 2)
339 i = dp->dim[0].ne - 2;
340 x = i + (pt[0] - dp->dim[0].p[i]) /
341 (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
342 }
343 /* get dependent variable */
344 if (dp->nd > 1) {
345 if (dp->type == DATATY) {
346 sd.arr.d = dp->arr.d + i*asize;
347 y0 = datavalue(&sd, pt+1);
348 sd.arr.d = dp->arr.d + (i+1)*asize;
349 y1 = datavalue(&sd, pt+1);
350 } else {
351 sd.arr.c = dp->arr.c + i*asize;
352 y0 = datavalue(&sd, pt+1);
353 sd.arr.c = dp->arr.c + (i+1)*asize;
354 y1 = datavalue(&sd, pt+1);
355 }
356 } else {
357 if (dp->type == DATATY) {
358 y0 = dp->arr.d[i];
359 y1 = dp->arr.d[i+1];
360 } else {
361 y0 = colrval(dp->arr.c[i],dp->type);
362 y1 = colrval(dp->arr.c[i+1],dp->type);
363 }
364 }
365 /*
366 * Extrapolate as far as one division, then
367 * taper off harmonically to zero.
368 */
369 if (x > i+2)
370 return( (2*y1-y0)/(x-(i-1)) );
371
372 if (x < i-1)
373 return( (2*y0-y1)/(i-x) );
374
375 return( y0*((i+1)-x) + y1*(x-i) );
376 }