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.1 by greg, Tue Nov 12 17:10:07 1991 UTC vs.
Revision 2.20 by schorsch, Sun Jun 8 12:03:10 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines