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.11 by greg, Mon Nov 11 14:02:43 1991 UTC vs.
Revision 2.11 by greg, Tue Aug 22 08:51:57 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
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 < extern char  *fgetword();
31 < extern double  atof();
30 > #ifndef TABSIZ
31 > #define TABSIZ          97              /* table size (prime) */
32 > #endif
33  
34 < extern char  *libpath;                  /* library search path */
34 > #define hash(s)         (shash(s)%TABSIZ)
35  
27 static DATARRAY  *dlist = NULL;         /* data array list */
36  
37 < static DATARRAY  *plist = NULL;         /* picture list */
37 > extern char  *getlibpath();             /* library search path */
38  
39 + static DATARRAY  *dtab[TABSIZ];         /* data array list */
40  
41 + static DATARRAY  *ptab[TABSIZ];         /* picture list */
42 +
43 +
44   DATARRAY *
45   getdata(dname)                          /* get data array dname */
46   char  *dname;
47   {
36        char  word[64];
48          char  *dfname;
49          FILE  *fp;
50          int  asize;
51          register int  i, j;
52          register DATARRAY  *dp;
53                                                  /* look for array in list */
54 <        for (dp = dlist; dp != NULL; dp = dp->next)
54 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
55                  if (!strcmp(dname, dp->name))
56                          return(dp);             /* found! */
57  
58          /*
59           *      If we haven't loaded the data already, we will look
60 <         *  for it in the directorys specified by the library path.
60 >         *  for it in the directories specified by the library path.
61           *
62           *      The file has the following format:
63           *
# Line 64 | Line 75 | char  *dname;
75           *              0 0 ni p0i p1i .. pni
76           */
77  
78 <        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
78 >        if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) {
79                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
80                  error(USER, errmsg);
81          }
# Line 78 | Line 89 | char  *dname;
89                  error(SYSTEM, errmsg);
90          }
91                                                          /* get dimensions */
92 <        if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
92 >        if (fgetval(fp, 'i', &dp->nd) <= 0)
93                  goto scanerr;
83        dp->nd = atoi(word);
94          if (dp->nd <= 0 || dp->nd > MAXDDIM) {
95                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
96                  error(USER, errmsg);
97          }
98          asize = 1;
99          for (i = 0; i < dp->nd; i++) {
100 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
100 >                if (fgetval(fp, 'd', &dp->dim[i].org) <= 0)
101                          goto scanerr;
102 <                dp->dim[i].org = atof(word);
93 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
102 >                if (fgetval(fp, 'd', &dp->dim[i].siz) <= 0)
103                          goto scanerr;
104 <                dp->dim[i].siz = atof(word);
96 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
104 >                if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0)
105                          goto scanerr;
98                dp->dim[i].ne = atoi(word);
106                  if (dp->dim[i].ne < 2)
107                          goto scanerr;
108                  asize *= dp->dim[i].ne;
# Line 103 | Line 110 | char  *dname;
110                          dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
111                          if (dp->dim[i].p == NULL)
112                                  goto memerr;
113 <                        for (j = 0; j < dp->dim[i].ne; j++) {
114 <                                if (fgetword(word, sizeof(word), fp) == NULL ||
108 <                                                !isflt(word))
113 >                        for (j = 0; j < dp->dim[i].ne; j++)
114 >                                if (fgetval(fp, 'd', &dp->dim[i].p[j]) <= 0)
115                                          goto scanerr;
110                                dp->dim[i].p[j] = atof(word);
111                        }
116                          for (j = 1; j < dp->dim[i].ne-1; j++)
117                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
118                                          (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
# Line 122 | Line 126 | char  *dname;
126          if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
127                  goto memerr;
128          
129 <        for (i = 0; i < asize; i++) {
130 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
129 >        for (i = 0; i < asize; i++)
130 >                if (fgetval(fp, DATATY, &dp->arr[i]) <= 0)
131                          goto scanerr;
128                dp->arr[i] = atof(word);
129        }
132          fclose(fp);
133 <        dp->next = dlist;
134 <        return(dlist = dp);
133 >        i = hash(dname);
134 >        dp->next = dtab[i];
135 >        return(dtab[i] = dp);
136  
137   memerr:
138          error(SYSTEM, "out of memory in getdata");
# Line 140 | Line 143 | scanerr:
143   }
144  
145  
143 static double  inpaspect;               /* aspect ratio of input picture */
144
146   static
147 < headaspect(s)                           /* check string for aspect ratio */
147 > headaspect(s, iap)                      /* check string for aspect ratio */
148   char  *s;
149 + double  *iap;
150   {
151          if (isaspect(s))
152 <                inpaspect *= aspectval(s);
152 >                *iap *= aspectval(s);
153   }
154  
155  
# Line 155 | Line 157 | DATARRAY *
157   getpict(pname)                          /* get picture pname */
158   char  *pname;
159   {
160 <        extern char  *libpath;
160 >        double  inpaspect;
161          char  *pfname;
162          FILE  *fp;
163          COLOR  *scanin;
164          int  sl, ns;
165 <        RESOLU  inpres;
165 >        RESOLU  inpres;
166          FLOAT  loc[2];
167          int  y;
168          register int  x, i;
169          register DATARRAY  *pp;
170                                                  /* look for array in list */
171 <        for (pp = plist; pp != NULL; pp = pp->next)
171 >        for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next)
172                  if (!strcmp(pname, pp->name))
173                          return(pp);             /* found! */
174  
175 <        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
175 >        if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) {
176                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
177                  error(USER, errmsg);
178          }
# Line 185 | Line 187 | char  *pname;
187                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
188                  error(SYSTEM, errmsg);
189          }
190 + #ifdef MSDOS
191 +        setmode(fileno(fp), O_BINARY);
192 + #endif
193                                                  /* get dimensions */
194          inpaspect = 1.0;
195 <        getheader(fp, headaspect);
195 >        getheader(fp, headaspect, &inpaspect);
196          if (!fgetsresolu(&inpres, fp))
197                  goto readerr;
198 + #if PSIZWARN
199 +                                                /* check memory usage */
200 +        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
201 +        if (i > PSIZWARN) {
202 +                sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
203 +                                pname, i);
204 +                error(WARNING, errmsg);
205 +        }
206 + #endif
207          for (i = 0; i < 3; i++) {
208                  pp[i].nd = 2;
209                  pp[i].dim[0].ne = inpres.yr;
# Line 230 | Line 244 | char  *pname;
244          }
245          free((char *)scanin);
246          fclose(fp);
247 +        i = hash(pname);
248          pp[0].next =
249          pp[1].next =
250 <        pp[2].next = plist;
251 <        return(plist = pp);
250 >        pp[2].next = ptab[i];
251 >        return(ptab[i] = pp);
252  
253   memerr:
254          error(SYSTEM, "out of memory in getpict");
# Line 246 | Line 261 | readerr:
261   freedata(dname)                 /* free memory associated with dname */
262   char  *dname;
263   {
264 +        DATARRAY  head;
265 +        int  hval, nents;
266          register DATARRAY  *dp, *dpl;
267          register int  i;
268  
269 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
270 <                if (!strcmp(dname, dp->name)) {
271 <                        if (dpl == NULL)
272 <                                dlist = dp->next;
273 <                        else
269 >        if (dname == NULL) {                    /* free all if NULL */
270 >                hval = 0; nents = TABSIZ;
271 >        } else {
272 >                hval = hash(dname); nents = 1;
273 >        }
274 >        while (nents--) {
275 >                head.next = dtab[hval];
276 >                dpl = &head;
277 >                while ((dp = dpl->next) != NULL)
278 >                        if (dname == NULL || !strcmp(dname, dp->name)) {
279                                  dpl->next = dp->next;
280 <                        free((char *)dp->arr);
281 <                        for (i = 0; i < dp->nd; i++)
282 <                                if (dp->dim[i].p != NULL)
283 <                                        free((char *)dp->dim[i].p);
284 <                        freestr(dp->name);
285 <                        free((char *)dp);
286 <                        return;
287 <                }
280 >                                free((char *)dp->arr);
281 >                                for (i = 0; i < dp->nd; i++)
282 >                                        if (dp->dim[i].p != NULL)
283 >                                                free((char *)dp->dim[i].p);
284 >                                freestr(dp->name);
285 >                                free((char *)dp);
286 >                        } else
287 >                                dpl = dp;
288 >                dtab[hval++] = head.next;
289 >        }
290   }
291  
292  
293   freepict(pname)                 /* free memory associated with pname */
294   char  *pname;
295   {
296 +        DATARRAY  head;
297 +        int  hval, nents;
298          register DATARRAY  *pp, *ppl;
299  
300 <        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
301 <                if (!strcmp(pname, pp->name)) {
302 <                        if (ppl == NULL)
303 <                                plist = pp->next;
304 <                        else
300 >        if (pname == NULL) {                    /* free all if NULL */
301 >                hval = 0; nents = TABSIZ;
302 >        } else {
303 >                hval = hash(pname); nents = 1;
304 >        }
305 >        while (nents--) {
306 >                head.next = ptab[hval];
307 >                ppl = &head;
308 >                while ((pp = ppl->next) != NULL)
309 >                        if (pname == NULL || !strcmp(pname, pp->name)) {
310                                  ppl->next = pp->next;
311 <                        free((char *)pp[0].arr);
312 <                        free((char *)pp[1].arr);
313 <                        free((char *)pp[2].arr);
314 <                        freestr(pp[0].name);
315 <                        free((char *)pp);
316 <                        return;
317 <                }
311 >                                free((char *)pp[0].arr);
312 >                                free((char *)pp[1].arr);
313 >                                free((char *)pp[2].arr);
314 >                                freestr(pp[0].name);
315 >                                free((char *)pp);
316 >                        } else
317 >                                ppl = pp;
318 >                ptab[hval++] = head.next;
319 >        }
320   }
321  
322  
323   double
324   datavalue(dp, pt)               /* interpolate data value at a point */
325   register DATARRAY  *dp;
326 < double  *pt;
326 > double  *pt;
327   {
328          DATARRAY  sd;
329          int  asize;
330          int  lower, upper;
331          register int  i;
332 <        double  x, y, y0, y1;
332 >        double  x, y0, y1;
333                                          /* set up dimensions for recursion */
334          sd.nd = dp->nd - 1;
335          asize = 1;
# Line 309 | Line 342 | double  *pt;
342                                          /* get independent variable */
343          if (dp->dim[0].p == NULL) {             /* evenly spaced points */
344                  x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
345 <                x = x * (dp->dim[0].ne - 1);
345 >                x *= (double)(dp->dim[0].ne - 1);
346                  i = x;
347                  if (i < 0)
348                          i = 0;
# Line 350 | Line 383 | double  *pt;
383           * taper off harmonically to zero.
384           */
385          if (x > i+2)
386 <                y = (2*y1-y0)/(x-i-1);
354 <        else if (x < i-1)
355 <                y = (2*y0-y1)/(i-x);
356 <        else
357 <                y = y0*((i+1)-x) + y1*(x-i);
386 >                return( (2*y1-y0)/(x-(i-1)) );
387  
388 <        return(y);
388 >        if (x < i-1)
389 >                return( (2*y0-y1)/(i-x) );
390 >
391 >        return( y0*((i+1)-x) + y1*(x-i) );
392   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines