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.3 by greg, Mon Sep 21 12:07:42 1992 UTC vs.
Revision 2.14 by gwlarson, Thu Nov 5 17:11:56 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ LBL";
6  
7   /*
8   *  data.c - routines dealing with interpolated data.
9 *
10 *     6/4/86
9   */
10  
11   #include  "standard.h"
# Line 18 | Line 16 | static char SCCSid[] = "$SunId$ LBL";
16  
17   #include  "data.h"
18  
19 +                                /* picture memory usage before warning */
20 + #ifndef PSIZWARN
21 + #ifdef BIGMEM
22 + #define PSIZWARN        5000000
23 + #else
24 + #define PSIZWARN        1500000
25 + #endif
26 + #endif
27  
28 < extern char  *fgetword();
28 > #ifndef TABSIZ
29 > #define TABSIZ          97              /* table size (prime) */
30 > #endif
31  
32 < extern char  *libpath;                  /* library search path */
32 > #define hash(s)         (shash(s)%TABSIZ)
33  
26 static DATARRAY  *dlist = NULL;         /* data array list */
34  
35 < static DATARRAY  *plist = NULL;         /* picture list */
35 > extern char  *getlibpath();             /* library search path */
36  
37 + static DATARRAY  *dtab[TABSIZ];         /* data array list */
38  
39 +
40   DATARRAY *
41   getdata(dname)                          /* get data array dname */
42   char  *dname;
43   {
35        char  word[64];
44          char  *dfname;
45          FILE  *fp;
46          int  asize;
47          register int  i, j;
48          register DATARRAY  *dp;
49                                                  /* look for array in list */
50 <        for (dp = dlist; dp != NULL; dp = dp->next)
50 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
51                  if (!strcmp(dname, dp->name))
52                          return(dp);             /* found! */
53  
54          /*
55           *      If we haven't loaded the data already, we will look
56 <         *  for it in the directorys specified by the library path.
56 >         *  for it in the directories specified by the library path.
57           *
58           *      The file has the following format:
59           *
# Line 63 | Line 71 | char  *dname;
71           *              0 0 ni p0i p1i .. pni
72           */
73  
74 <        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
74 >        if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) {
75                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
76                  error(USER, errmsg);
77          }
70        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
71                goto memerr;
72
73        dp->name = savestr(dname);
74
78          if ((fp = fopen(dfname, "r")) == NULL) {
79                  sprintf(errmsg, "cannot open data file \"%s\"", dfname);
80                  error(SYSTEM, errmsg);
81          }
82                                                          /* get dimensions */
83 <        if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
83 >        if (fgetval(fp, 'i', &asize) <= 0)
84                  goto scanerr;
85 <        dp->nd = atoi(word);
83 <        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
85 >        if (asize <= 0 | asize > MAXDDIM) {
86                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
87                  error(USER, errmsg);
88          }
89 +        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
90 +                goto memerr;
91 +        dp->name = savestr(dname);
92 +        dp->type = DATATY;
93 +        dp->nd = asize;
94          asize = 1;
95          for (i = 0; i < dp->nd; i++) {
96 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
96 >                if (fgetval(fp, DATATY, &dp->dim[i].org) <= 0)
97                          goto scanerr;
98 <                dp->dim[i].org = atof(word);
92 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
98 >                if (fgetval(fp, DATATY, &dp->dim[i].siz) <= 0)
99                          goto scanerr;
100 <                dp->dim[i].siz = atof(word);
95 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
100 >                if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0)
101                          goto scanerr;
97                dp->dim[i].ne = atoi(word);
102                  if (dp->dim[i].ne < 2)
103                          goto scanerr;
104                  asize *= dp->dim[i].ne;
105                  if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
106 <                        dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
106 >                        dp->dim[i].p = (DATATYPE *)
107 >                                        malloc(dp->dim[i].ne*sizeof(DATATYPE));
108                          if (dp->dim[i].p == NULL)
109                                  goto memerr;
110 <                        for (j = 0; j < dp->dim[i].ne; j++) {
111 <                                if (fgetword(word, sizeof(word), fp) == NULL ||
107 <                                                !isflt(word))
110 >                        for (j = 0; j < dp->dim[i].ne; j++)
111 >                                if (fgetval(fp, DATATY, &dp->dim[i].p[j]) <= 0)
112                                          goto scanerr;
109                                dp->dim[i].p[j] = atof(word);
110                        }
113                          for (j = 1; j < dp->dim[i].ne-1; j++)
114                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
115                                          (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
# Line 118 | Line 120 | char  *dname;
120                  } else
121                          dp->dim[i].p = NULL;
122          }
123 <        if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
123 >        if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
124                  goto memerr;
125          
126 <        for (i = 0; i < asize; i++) {
127 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
126 >        for (i = 0; i < asize; i++)
127 >                if (fgetval(fp, DATATY, &dp->arr.d[i]) <= 0)
128                          goto scanerr;
127                dp->arr[i] = atof(word);
128        }
129          fclose(fp);
130 <        dp->next = dlist;
131 <        return(dlist = dp);
130 >        i = hash(dname);
131 >        dp->next = dtab[i];
132 >        return(dtab[i] = dp);
133  
134   memerr:
135          error(SYSTEM, "out of memory in getdata");
# Line 139 | Line 140 | scanerr:
140   }
141  
142  
142 static double  inpaspect;               /* aspect ratio of input picture */
143
143   static
144 < headaspect(s)                           /* check string for aspect ratio */
144 > headaspect(s, iap)                      /* check string for aspect ratio */
145   char  *s;
146 + double  *iap;
147   {
148 +        char    fmt[32];
149 +
150          if (isaspect(s))
151 <                inpaspect *= aspectval(s);
151 >                *iap *= aspectval(s);
152 >        else if (formatval(fmt, s) && strcmp(fmt, COLRFMT))
153 >                *iap = 0.0;
154 >        return(0);
155   }
156  
157  
# Line 154 | Line 159 | DATARRAY *
159   getpict(pname)                          /* get picture pname */
160   char  *pname;
161   {
162 <        extern char  *libpath;
162 >        double  inpaspect;
163          char  *pfname;
164          FILE  *fp;
165 <        COLOR  *scanin;
165 >        COLR  *scanin;
166          int  sl, ns;
167          RESOLU  inpres;
168          FLOAT  loc[2];
# Line 165 | Line 170 | char  *pname;
170          register int  x, i;
171          register DATARRAY  *pp;
172                                                  /* look for array in list */
173 <        for (pp = plist; pp != NULL; pp = pp->next)
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, libpath, R_OK)) == NULL) {
177 >        if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) {
178                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
179                  error(USER, errmsg);
180          }
181 <        if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL)
181 >        if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
182                  goto memerr;
183  
184 <        pp[0].name =
180 <        pp[1].name =
181 <        pp[2].name = savestr(pname);
184 >        pp[0].name = savestr(pname);
185  
186          if ((fp = fopen(pfname, "r")) == NULL) {
187                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
# Line 189 | Line 192 | char  *pname;
192   #endif
193                                                  /* get dimensions */
194          inpaspect = 1.0;
195 <        getheader(fp, headaspect);
196 <        if (!fgetsresolu(&inpres, fp))
195 >        getheader(fp, headaspect, &inpaspect);
196 >        if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
197                  goto readerr;
198 <        for (i = 0; i < 3; i++) {
199 <                pp[i].nd = 2;
200 <                pp[i].dim[0].ne = inpres.yr;
201 <                pp[i].dim[1].ne = inpres.xr;
202 <                pp[i].dim[0].org =
203 <                pp[i].dim[1].org = 0.0;
204 <                if (inpres.xr <= inpres.yr*inpaspect) {
205 <                        pp[i].dim[0].siz = inpaspect *
206 <                                                (double)inpres.yr/inpres.xr;
207 <                        pp[i].dim[1].siz = 1.0;
208 <                } else {
209 <                        pp[i].dim[0].siz = 1.0;
210 <                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
208 <                                                inpaspect;
209 <                }
210 <                pp[i].dim[0].p = pp[i].dim[1].p = NULL;
211 <                pp[i].arr = (DATATYPE *)
212 <                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
213 <                if (pp[i].arr == NULL)
214 <                        goto memerr;
198 >        pp[0].nd = 2;
199 >        pp[0].dim[0].ne = inpres.yr;
200 >        pp[0].dim[1].ne = inpres.xr;
201 >        pp[0].dim[0].org =
202 >        pp[0].dim[1].org = 0.0;
203 >        if (inpres.xr <= inpres.yr*inpaspect) {
204 >                pp[0].dim[0].siz = inpaspect *
205 >                                        (double)inpres.yr/inpres.xr;
206 >                pp[0].dim[1].siz = 1.0;
207 >        } else {
208 >                pp[0].dim[0].siz = 1.0;
209 >                pp[0].dim[1].siz = (double)inpres.xr/inpres.yr /
210 >                                        inpaspect;
211          }
212 <                                                        /* load picture */
213 <        sl = scanlen(&inpres);
212 >        pp[0].dim[0].p = pp[0].dim[1].p = NULL;
213 >        sl = scanlen(&inpres);                          /* allocate array */
214          ns = numscans(&inpres);
215 <        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
215 >        i = ns*sl*sizeof(COLR);
216 > #if PSIZWARN
217 >        if (i > PSIZWARN) {                             /* memory warning */
218 >                sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
219 >                                pname, i);
220 >                error(WARNING, errmsg);
221 >        }
222 > #endif
223 >        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
224                  goto memerr;
225 +                                                        /* load picture */
226 +        if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
227 +                goto memerr;
228          for (y = 0; y < ns; y++) {
229 <                if (freadscan(scanin, sl, fp) < 0)
229 >                if (freadcolrs(scanin, sl, fp) < 0)
230                          goto readerr;
231                  for (x = 0; x < sl; x++) {
232                          pix2loc(loc, &inpres, x, y);
233                          i = (int)(loc[1]*inpres.yr)*inpres.xr +
234                                          (int)(loc[0]*inpres.xr);
235 <                        pp[0].arr[i] = colval(scanin[x],RED);
229 <                        pp[1].arr[i] = colval(scanin[x],GRN);
230 <                        pp[2].arr[i] = colval(scanin[x],BLU);
235 >                        copycolr(pp[0].arr.c[i], scanin[x]);
236                  }
237          }
238          free((char *)scanin);
239          fclose(fp);
240 <        pp[0].next =
241 <        pp[1].next =
242 <        pp[2].next = plist;
243 <        return(plist = pp);
240 >        i = hash(pname);
241 >        pp[0].next = dtab[i];           /* link into picture list */
242 >        copystruct(&pp[1], &pp[0]);
243 >        copystruct(&pp[2], &pp[0]);
244 >        pp[0].type = RED;               /* differentiate RGB records */
245 >        pp[1].type = GRN;
246 >        pp[2].type = BLU;
247 >        return(dtab[i] = pp);
248  
249   memerr:
250          error(SYSTEM, "out of memory in getpict");
# Line 248 | Line 257 | readerr:
257   freedata(dname)                 /* free memory associated with dname */
258   char  *dname;
259   {
260 +        DATARRAY  head;
261 +        int  hval, nents;
262          register DATARRAY  *dp, *dpl;
263          register int  i;
264  
265 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
266 <                if (!strcmp(dname, dp->name)) {
267 <                        if (dpl == NULL)
268 <                                dlist = dp->next;
269 <                        else
265 >        if (dname == NULL) {                    /* free all if NULL */
266 >                hval = 0; nents = TABSIZ;
267 >        } else {
268 >                hval = hash(dname); nents = 1;
269 >        }
270 >        while (nents--) {
271 >                head.next = dtab[hval];
272 >                dpl = &head;
273 >                while ((dp = dpl->next) != NULL)
274 >                        if (dname == NULL || !strcmp(dname, dp->name)) {
275                                  dpl->next = dp->next;
276 <                        free((char *)dp->arr);
277 <                        for (i = 0; i < dp->nd; i++)
278 <                                if (dp->dim[i].p != NULL)
279 <                                        free((char *)dp->dim[i].p);
280 <                        freestr(dp->name);
281 <                        free((char *)dp);
282 <                        return;
283 <                }
276 >                                if (dp->type == DATATY)
277 >                                        free((char *)dp->arr.d);
278 >                                else
279 >                                        free((char *)dp->arr.c);
280 >                                for (i = 0; i < dp->nd; i++)
281 >                                        if (dp->dim[i].p != NULL)
282 >                                                free((char *)dp->dim[i].p);
283 >                                freestr(dp->name);
284 >                                free((char *)dp);
285 >                        } else
286 >                                dpl = dp;
287 >                dtab[hval++] = head.next;
288 >        }
289   }
290  
291  
271 freepict(pname)                 /* free memory associated with pname */
272 char  *pname;
273 {
274        register DATARRAY  *pp, *ppl;
275
276        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
277                if (!strcmp(pname, pp->name)) {
278                        if (ppl == NULL)
279                                plist = pp->next;
280                        else
281                                ppl->next = pp->next;
282                        free((char *)pp[0].arr);
283                        free((char *)pp[1].arr);
284                        free((char *)pp[2].arr);
285                        freestr(pp[0].name);
286                        free((char *)pp);
287                        return;
288                }
289 }
290
291
292   double
293   datavalue(dp, pt)               /* interpolate data value at a point */
294   register DATARRAY  *dp;
# Line 298 | Line 298 | double *pt;
298          int  asize;
299          int  lower, upper;
300          register int  i;
301 <        double  x, y, y0, y1;
301 >        double  x, y0, y1;
302                                          /* set up dimensions for recursion */
303 <        sd.nd = dp->nd - 1;
304 <        asize = 1;
305 <        for (i = 0; i < sd.nd; i++) {
306 <                sd.dim[i].org = dp->dim[i+1].org;
307 <                sd.dim[i].siz = dp->dim[i+1].siz;
308 <                sd.dim[i].p = dp->dim[i+1].p;
309 <                asize *= sd.dim[i].ne = dp->dim[i+1].ne;
303 >        if (dp->nd > 1) {
304 >                sd.name = dp->name;
305 >                sd.type = dp->type;
306 >                sd.nd = dp->nd - 1;
307 >                asize = 1;
308 >                for (i = 0; i < sd.nd; i++) {
309 >                        sd.dim[i].org = dp->dim[i+1].org;
310 >                        sd.dim[i].siz = dp->dim[i+1].siz;
311 >                        sd.dim[i].p = dp->dim[i+1].p;
312 >                        asize *= sd.dim[i].ne = dp->dim[i+1].ne;
313 >                }
314          }
315                                          /* get independent variable */
316          if (dp->dim[0].p == NULL) {             /* evenly spaced points */
317                  x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
318 <                x = x * (dp->dim[0].ne - 1);
318 >                x *= (double)(dp->dim[0].ne - 1);
319                  i = x;
320                  if (i < 0)
321                          i = 0;
# Line 338 | Line 342 | double *pt;
342                                  (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
343          }
344                                          /* get dependent variable */
345 <        if (dp->nd == 1) {
346 <                y0 = dp->arr[i];
347 <                y1 = dp->arr[i+1];
345 >        if (dp->nd > 1) {
346 >                if (dp->type == DATATY) {
347 >                        sd.arr.d = dp->arr.d + i*asize;
348 >                        y0 = datavalue(&sd, pt+1);
349 >                        sd.arr.d = dp->arr.d + (i+1)*asize;
350 >                        y1 = datavalue(&sd, pt+1);
351 >                } else {
352 >                        sd.arr.c = dp->arr.c + i*asize;
353 >                        y0 = datavalue(&sd, pt+1);
354 >                        sd.arr.c = dp->arr.c + (i+1)*asize;
355 >                        y1 = datavalue(&sd, pt+1);
356 >                }
357          } else {
358 <                sd.arr = &dp->arr[i*asize];
359 <                y0 = datavalue(&sd, pt+1);
360 <                sd.arr = &dp->arr[(i+1)*asize];
361 <                y1 = datavalue(&sd, pt+1);
358 >                if (dp->type == DATATY) {
359 >                        y0 = dp->arr.d[i];
360 >                        y1 = dp->arr.d[i+1];
361 >                } else {
362 >                        y0 = colrval(dp->arr.c[i],dp->type);
363 >                        y1 = colrval(dp->arr.c[i+1],dp->type);
364 >                }
365          }
366          /*
367           * Extrapolate as far as one division, then
368           * taper off harmonically to zero.
369           */
370          if (x > i+2)
371 <                y = (2*y1-y0)/(x-i-1);
356 <        else if (x < i-1)
357 <                y = (2*y0-y1)/(i-x);
358 <        else
359 <                y = y0*((i+1)-x) + y1*(x-i);
371 >                return( (2*y1-y0)/(x-(i-1)) );
372  
373 <        return(y);
373 >        if (x < i-1)
374 >                return( (2*y0-y1)/(i-x) );
375 >
376 >        return( y0*((i+1)-x) + y1*(x-i) );
377   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines