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 2.5 by greg, Sun Nov 22 10:02:57 1992 UTC vs.
Revision 2.11 by greg, Tue Aug 22 08:51:57 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 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();
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  
26 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   {
35        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  
# Line 63 | 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 77 | 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;
82        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);
92 <                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);
95 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
104 >                if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0)
105                          goto scanerr;
97                dp->dim[i].ne = atoi(word);
106                  if (dp->dim[i].ne < 2)
107                          goto scanerr;
108                  asize *= dp->dim[i].ne;
# Line 102 | 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 ||
107 <                                                !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;
109                                dp->dim[i].p[j] = atof(word);
110                        }
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 121 | 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;
127                dp->arr[i] = atof(word);
128        }
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 153 | Line 157 | DATARRAY *
157   getpict(pname)                          /* get picture pname */
158   char  *pname;
159   {
156        extern char  *libpath;
160          double  inpaspect;
161          char  *pfname;
162          FILE  *fp;
# Line 165 | Line 168 | char  *pname;
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 192 | Line 195 | char  *pname;
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 232 | 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 249 | Line 262 | 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 <        head.next = dlist;
270 <        dpl = &head;
271 <        while ((dp = dpl->next) != NULL)
272 <                if (dname == NULL || !strcmp(dname, dp->name)) {
273 <                        dpl->next = dp->next;
274 <                        free((char *)dp->arr);
275 <                        for (i = 0; i < dp->nd; i++)
276 <                                if (dp->dim[i].p != NULL)
277 <                                        free((char *)dp->dim[i].p);
278 <                        freestr(dp->name);
279 <                        free((char *)dp);
280 <                } else
281 <                        dpl = dp;
282 <        dlist = head.next;
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 >                        } else
287 >                                dpl = dp;
288 >                dtab[hval++] = head.next;
289 >        }
290   }
291  
292  
# Line 273 | Line 294 | 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 <        head.next = plist;
301 <        ppl = &head;
302 <        while ((pp = ppl->next) != NULL)
303 <                if (pname == NULL || !strcmp(pname, pp->name)) {
304 <                        ppl->next = pp->next;
305 <                        free((char *)pp[0].arr);
306 <                        free((char *)pp[1].arr);
307 <                        free((char *)pp[2].arr);
308 <                        freestr(pp[0].name);
309 <                        free((char *)pp);
310 <                } else
311 <                        ppl = pp;
312 <        plist = head.next;
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 >                        } else
317 >                                ppl = pp;
318 >                ptab[hval++] = head.next;
319 >        }
320   }
321  
322  
# Line 300 | Line 329 | double *pt;
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 313 | 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 354 | Line 383 | double *pt;
383           * taper off harmonically to zero.
384           */
385          if (x > i+2)
386 <                y = (2*y1-y0)/(x-i-1);
358 <        else if (x < i-1)
359 <                y = (2*y0-y1)/(i-x);
360 <        else
361 <                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