ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
(Generate patch)

Comparing ray/src/rt/data.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:41:18 1989 UTC vs.
Revision 2.7 by greg, Mon Feb 8 13:12:28 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "color.h"
16  
17 + #include  "resolu.h"
18 +
19   #include  "data.h"
20  
21 +                                /* picture memory usage before warning */
22 + #ifndef PSIZWARN
23 + #ifdef BIGMEM
24 + #define PSIZWARN        3000000
25 + #else
26 + #define PSIZWARN        1000000
27 + #endif
28 + #endif
29  
30 + #ifndef TABSIZ
31 + #define TABSIZ          97              /* table size (prime) */
32 + #endif
33 +
34 + #define hash(s)         (shash(s)%TABSIZ)
35 +
36 +
37 + extern char  *fgetword();
38 +
39   extern char  *libpath;                  /* library search path */
40  
41 < static DATARRAY  *dlist = NULL;         /* data array list */
41 > static DATARRAY  *dtab[TABSIZ];         /* data array list */
42  
43 < static DATARRAY  *plist = NULL;         /* picture list */
43 > static DATARRAY  *ptab[TABSIZ];         /* picture list */
44  
45  
46   DATARRAY *
47   getdata(dname)                          /* get data array dname */
48   char  *dname;
49   {
50 +        char  word[64];
51          char  *dfname;
52          FILE  *fp;
53          int  asize;
54 <        register int  i;
54 >        register int  i, j;
55          register DATARRAY  *dp;
56                                                  /* look for array in list */
57 <        for (dp = dlist; dp != NULL; dp = dp->next)
57 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
58                  if (!strcmp(dname, dp->name))
59                          return(dp);             /* found! */
60  
61          /*
62           *      If we haven't loaded the data already, we will look
63 <         *  for it in the directorys specified by the library path.
63 >         *  for it in the directories specified by the library path.
64           *
65           *      The file has the following format:
66           *
67 <         *              #dimensions
67 >         *              N
68           *              beg0    end0    n0
69           *              beg1    end1    n1
70           *              . . .
71 +         *              begN    endN    nN
72           *              data, later dimensions changing faster
73           *              . . .
74           *
75 +         *      For irregularly spaced points, the following can be
76 +         *  substituted for begi endi ni:
77 +         *
78 +         *              0 0 ni p0i p1i .. pni
79           */
80  
81 <        if ((dfname = getpath(dname, libpath)) == NULL) {
81 >        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
82                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
83                  error(USER, errmsg);
84          }
# Line 67 | Line 92 | char  *dname;
92                  error(SYSTEM, errmsg);
93          }
94                                                          /* get dimensions */
95 <        if (fscanf(fp, "%d", &dp->nd) != 1)
95 >        if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
96                  goto scanerr;
97 <        if (dp->nd <= 0 || dp->nd > MAXDIM) {
97 >        dp->nd = atoi(word);
98 >        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
99                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
100                  error(USER, errmsg);
101          }
102          asize = 1;
103          for (i = 0; i < dp->nd; i++) {
104 <                if (fscanf(fp, "%lf %lf %d",
79 <                                &dp->dim[i].org, &dp->dim[i].siz,
80 <                                &dp->dim[i].ne) != 3)
104 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
105                          goto scanerr;
106 <                dp->dim[i].siz -= dp->dim[i].org;
106 >                dp->dim[i].org = atof(word);
107 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
108 >                        goto scanerr;
109 >                dp->dim[i].siz = atof(word);
110 >                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
111 >                        goto scanerr;
112 >                dp->dim[i].ne = atoi(word);
113 >                if (dp->dim[i].ne < 2)
114 >                        goto scanerr;
115                  asize *= dp->dim[i].ne;
116 +                if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
117 +                        dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
118 +                        if (dp->dim[i].p == NULL)
119 +                                goto memerr;
120 +                        for (j = 0; j < dp->dim[i].ne; j++) {
121 +                                if (fgetword(word, sizeof(word), fp) == NULL ||
122 +                                                !isflt(word))
123 +                                        goto scanerr;
124 +                                dp->dim[i].p[j] = atof(word);
125 +                        }
126 +                        for (j = 1; j < dp->dim[i].ne-1; j++)
127 +                                if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
128 +                                        (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
129 +                                        goto scanerr;
130 +                        dp->dim[i].org = dp->dim[i].p[0];
131 +                        dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
132 +                                                - dp->dim[i].p[0];
133 +                } else
134 +                        dp->dim[i].p = NULL;
135          }
136          if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
137                  goto memerr;
138          
139 <        for (i = 0; i < asize; i++)
140 <                if (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
139 >        for (i = 0; i < asize; i++) {
140 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
141                          goto scanerr;
142 <        
142 >                dp->arr[i] = atof(word);
143 >        }
144          fclose(fp);
145 <        dp->next = dlist;
146 <        return(dlist = dp);
145 >        i = hash(dname);
146 >        dp->next = dtab[i];
147 >        return(dtab[i] = dp);
148  
149   memerr:
150          error(SYSTEM, "out of memory in getdata");
# Line 102 | Line 155 | scanerr:
155   }
156  
157  
158 + static
159 + headaspect(s, iap)                      /* check string for aspect ratio */
160 + char  *s;
161 + double  *iap;
162 + {
163 +        if (isaspect(s))
164 +                *iap *= aspectval(s);
165 + }
166 +
167 +
168   DATARRAY *
169   getpict(pname)                          /* get picture pname */
170   char  *pname;
171   {
172          extern char  *libpath;
173 +        double  inpaspect;
174          char  *pfname;
175          FILE  *fp;
176          COLOR  *scanin;
177 <        int  width, height;
178 <        int  x, y;
179 <        register int  i;
177 >        int  sl, ns;
178 >        RESOLU  inpres;
179 >        FLOAT  loc[2];
180 >        int  y;
181 >        register int  x, i;
182          register DATARRAY  *pp;
183                                                  /* look for array in list */
184 <        for (pp = plist; pp != NULL; pp = pp->next)
184 >        for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next)
185                  if (!strcmp(pname, pp->name))
186                          return(pp);             /* found! */
187  
188 <        if ((pfname = getpath(pname, libpath)) == NULL) {
188 >        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
189                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
190                  error(USER, errmsg);
191          }
# Line 134 | Line 200 | char  *pname;
200                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
201                  error(SYSTEM, errmsg);
202          }
203 + #ifdef MSDOS
204 +        setmode(fileno(fp), O_BINARY);
205 + #endif
206                                                  /* get dimensions */
207 <        getheader(fp, NULL);
208 <        if (fscanf(fp, "-Y %d +X %d\n", &height, &width) != 2)
207 >        inpaspect = 1.0;
208 >        getheader(fp, headaspect, &inpaspect);
209 >        if (!fgetsresolu(&inpres, fp))
210                  goto readerr;
211 + #if PSIZWARN
212 +                                                /* check memory usage */
213 +        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
214 +        if (i > PSIZWARN) {
215 +                sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
216 +                                pname, i);
217 +                error(WARNING, errmsg);
218 +        }
219 + #endif
220          for (i = 0; i < 3; i++) {
221                  pp[i].nd = 2;
222 <                pp[i].dim[0].ne = width;
223 <                pp[i].dim[1].ne = height;
222 >                pp[i].dim[0].ne = inpres.yr;
223 >                pp[i].dim[1].ne = inpres.xr;
224                  pp[i].dim[0].org =
225                  pp[i].dim[1].org = 0.0;
226 <                if (width <= height) {
227 <                        pp[i].dim[0].siz = 1.0;
228 <                        pp[i].dim[1].siz = (double)height/width;
150 <                } else {
151 <                        pp[i].dim[0].siz = (double)width/height;
226 >                if (inpres.xr <= inpres.yr*inpaspect) {
227 >                        pp[i].dim[0].siz = inpaspect *
228 >                                                (double)inpres.yr/inpres.xr;
229                          pp[i].dim[1].siz = 1.0;
230 +                } else {
231 +                        pp[i].dim[0].siz = 1.0;
232 +                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
233 +                                                inpaspect;
234                  }
235 <                pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
235 >                pp[i].dim[0].p = pp[i].dim[1].p = NULL;
236 >                pp[i].arr = (DATATYPE *)
237 >                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
238                  if (pp[i].arr == NULL)
239                          goto memerr;
240          }
241                                                          /* load picture */
242 <        if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
242 >        sl = scanlen(&inpres);
243 >        ns = numscans(&inpres);
244 >        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
245                  goto memerr;
246 <        for (y = height-1; y >= 0; y--) {
247 <                if (freadscan(scanin, width, fp) < 0)
246 >        for (y = 0; y < ns; y++) {
247 >                if (freadscan(scanin, sl, fp) < 0)
248                          goto readerr;
249 <                for (x = 0; x < width; x++)
250 <                        for (i = 0; i < 3; i++)
251 <                                pp[i].arr[x*height+y] = colval(scanin[x],i);
249 >                for (x = 0; x < sl; x++) {
250 >                        pix2loc(loc, &inpres, x, y);
251 >                        i = (int)(loc[1]*inpres.yr)*inpres.xr +
252 >                                        (int)(loc[0]*inpres.xr);
253 >                        pp[0].arr[i] = colval(scanin[x],RED);
254 >                        pp[1].arr[i] = colval(scanin[x],GRN);
255 >                        pp[2].arr[i] = colval(scanin[x],BLU);
256 >                }
257          }
258          free((char *)scanin);
259          fclose(fp);
260 +        i = hash(pname);
261          pp[0].next =
262          pp[1].next =
263 <        pp[2].next = plist;
264 <        return(plist = pp);
263 >        pp[2].next = ptab[i];
264 >        return(ptab[i] = pp);
265  
266   memerr:
267          error(SYSTEM, "out of memory in getpict");
# Line 183 | Line 274 | readerr:
274   freedata(dname)                 /* free memory associated with dname */
275   char  *dname;
276   {
277 +        DATARRAY  head;
278 +        int  hval, nents;
279          register DATARRAY  *dp, *dpl;
280 +        register int  i;
281  
282 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
283 <                if (!strcmp(dname, dp->name)) {
284 <                        if (dpl == NULL)
285 <                                dlist = dp->next;
286 <                        else
282 >        if (dname == NULL) {                    /* free all if NULL */
283 >                hval = 0; nents = TABSIZ;
284 >        } else {
285 >                hval = hash(dname); nents = 1;
286 >        }
287 >        while (nents--) {
288 >                head.next = dtab[hval];
289 >                dpl = &head;
290 >                while ((dp = dpl->next) != NULL)
291 >                        if (dname == NULL || !strcmp(dname, dp->name)) {
292                                  dpl->next = dp->next;
293 <                        free((char *)dp->arr);
294 <                        freestr(dp->name);
295 <                        free((char *)dp);
296 <                        return;
297 <                }
293 >                                free((char *)dp->arr);
294 >                                for (i = 0; i < dp->nd; i++)
295 >                                        if (dp->dim[i].p != NULL)
296 >                                                free((char *)dp->dim[i].p);
297 >                                freestr(dp->name);
298 >                                free((char *)dp);
299 >                        } else
300 >                                dpl = dp;
301 >                dtab[hval++] = head.next;
302 >        }
303   }
304  
305  
306   freepict(pname)                 /* free memory associated with pname */
307   char  *pname;
308   {
309 +        DATARRAY  head;
310 +        int  hval, nents;
311          register DATARRAY  *pp, *ppl;
312  
313 <        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
314 <                if (!strcmp(pname, pp->name)) {
315 <                        if (ppl == NULL)
316 <                                plist = pp->next;
317 <                        else
313 >        if (pname == NULL) {                    /* free all if NULL */
314 >                hval = 0; nents = TABSIZ;
315 >        } else {
316 >                hval = hash(pname); nents = 1;
317 >        }
318 >        while (nents--) {
319 >                head.next = ptab[hval];
320 >                ppl = &head;
321 >                while ((pp = ppl->next) != NULL)
322 >                        if (pname == NULL || !strcmp(pname, pp->name)) {
323                                  ppl->next = pp->next;
324 <                        free((char *)pp[0].arr);
325 <                        free((char *)pp[1].arr);
326 <                        free((char *)pp[2].arr);
327 <                        freestr(pp[0].name);
328 <                        free((char *)pp);
329 <                        return;
330 <                }
324 >                                free((char *)pp[0].arr);
325 >                                free((char *)pp[1].arr);
326 >                                free((char *)pp[2].arr);
327 >                                freestr(pp[0].name);
328 >                                free((char *)pp);
329 >                        } else
330 >                                ppl = pp;
331 >                ptab[hval++] = head.next;
332 >        }
333   }
334  
335  
336   double
337   datavalue(dp, pt)               /* interpolate data value at a point */
338   register DATARRAY  *dp;
339 < double  *pt;
339 > double  *pt;
340   {
341          DATARRAY  sd;
342          int  asize;
343 +        int  lower, upper;
344          register int  i;
345 <        double  x, y, y0, y1;
346 <
345 >        double  x, y, y0, y1;
346 >                                        /* set up dimensions for recursion */
347          sd.nd = dp->nd - 1;
348          asize = 1;
349          for (i = 0; i < sd.nd; i++) {
350                  sd.dim[i].org = dp->dim[i+1].org;
351                  sd.dim[i].siz = dp->dim[i+1].siz;
352 +                sd.dim[i].p = dp->dim[i+1].p;
353                  asize *= sd.dim[i].ne = dp->dim[i+1].ne;
354          }
355 <
356 <        x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
357 <
358 <        x *= dp->dim[0].ne - 1;
359 <
360 <        i = x;
361 <        if (i < 0)
362 <                i = 0;
363 <        else if (i > dp->dim[0].ne - 2)
364 <                i = dp->dim[0].ne - 2;
365 <
355 >                                        /* get independent variable */
356 >        if (dp->dim[0].p == NULL) {             /* evenly spaced points */
357 >                x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
358 >                x = x * (dp->dim[0].ne - 1);
359 >                i = x;
360 >                if (i < 0)
361 >                        i = 0;
362 >                else if (i > dp->dim[0].ne - 2)
363 >                        i = dp->dim[0].ne - 2;
364 >        } else {                                /* unevenly spaced points */
365 >                if (dp->dim[0].siz > 0.0) {
366 >                        lower = 0;
367 >                        upper = dp->dim[0].ne;
368 >                } else {
369 >                        lower = dp->dim[0].ne;
370 >                        upper = 0;
371 >                }
372 >                do {
373 >                        i = (lower + upper) >> 1;
374 >                        if (pt[0] >= dp->dim[0].p[i])
375 >                                lower = i;
376 >                        else
377 >                                upper = i;
378 >                } while (i != (lower + upper) >> 1);
379 >                if (i > dp->dim[0].ne - 2)
380 >                        i = dp->dim[0].ne - 2;
381 >                x = i + (pt[0] - dp->dim[0].p[i]) /
382 >                                (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
383 >        }
384 >                                        /* get dependent variable */
385          if (dp->nd == 1) {
386                  y0 = dp->arr[i];
387                  y1 = dp->arr[i+1];
# Line 257 | Line 391 | double  *pt;
391                  sd.arr = &dp->arr[(i+1)*asize];
392                  y1 = datavalue(&sd, pt+1);
393          }
260
394          /*
395           * Extrapolate as far as one division, then
396           * taper off harmonically to zero.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines