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.5 by greg, Tue Apr 3 17:48:46 1990 UTC vs.
Revision 2.19 by schorsch, Thu Jun 5 19:29:34 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines