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.4 by greg, Thu Nov 12 10:11:51 1992 UTC vs.
Revision 2.26 by schorsch, Fri Jan 2 11:43:42 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines