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.7 by greg, Mon Feb 8 13:12:28 1993 UTC vs.
Revision 2.19 by schorsch, Thu Jun 5 19:29:34 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 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"
16
12   #include  "resolu.h"
18
13   #include  "data.h"
14  
15                                  /* picture memory usage before warning */
16   #ifndef PSIZWARN
17 < #ifdef BIGMEM
18 < #define PSIZWARN        3000000
17 > #ifdef SMLMEM
18 > #define PSIZWARN        1500000
19   #else
20 < #define PSIZWARN        1000000
20 > #define PSIZWARN        5000000
21   #endif
22   #endif
23  
# Line 34 | Line 28 | static char SCCSid[] = "$SunId$ LBL";
28   #define hash(s)         (shash(s)%TABSIZ)
29  
30  
37 extern char  *fgetword();
38
39 extern char  *libpath;                  /* library search path */
40
31   static DATARRAY  *dtab[TABSIZ];         /* data array list */
32  
43 static DATARRAY  *ptab[TABSIZ];         /* picture list */
33  
45
34   DATARRAY *
35   getdata(dname)                          /* get data array dname */
36   char  *dname;
37   {
50        char  word[64];
38          char  *dfname;
39          FILE  *fp;
40          int  asize;
# Line 57 | Line 44 | char  *dname;
44          for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
45                  if (!strcmp(dname, dp->name))
46                          return(dp);             /* found! */
60
47          /*
48           *      If we haven't loaded the data already, we will look
49           *  for it in the directories specified by the library path.
# Line 78 | Line 64 | char  *dname;
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          }
85        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
86                goto memerr;
87
88        dp->name = savestr(dname);
89
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 (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
76 >        if (fgetval(fp, 'i', (char *)&asize) <= 0)
77                  goto scanerr;
78 <        dp->nd = atoi(word);
98 <        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
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 (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
89 >                if (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0)
90                          goto scanerr;
91 <                dp->dim[i].org = atof(word);
107 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
91 >                if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0)
92                          goto scanerr;
93 <                dp->dim[i].siz = atof(word);
110 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
93 >                if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
94                          goto scanerr;
112                dp->dim[i].ne = atoi(word);
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 = (double *)malloc(dp->dim[i].ne*sizeof(double));
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 (fgetword(word, sizeof(word), fp) == NULL ||
105 <                                                !isflt(word))
103 >                        for (j = 0; j < dp->dim[i].ne; j++)
104 >                                if (fgetval(fp, DATATY,
105 >                                                (char *)&dp->dim[i].p[j]) <= 0)
106                                          goto scanerr;
124                                dp->dim[i].p[j] = atof(word);
125                        }
107                          for (j = 1; j < dp->dim[i].ne-1; j++)
108                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
109                                          (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
# Line 133 | Line 114 | char  *dname;
114                  } else
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 (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
120 >        for (i = 0; i < asize; i++)
121 >                if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0)
122                          goto scanerr;
142                dp->arr[i] = atof(word);
143        }
123          fclose(fp);
124          i = hash(dname);
125          dp->next = dtab[i];
# Line 155 | Line 134 | scanerr:
134   }
135  
136  
137 < static
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                  *iap *= aspectval(s);
146 +        else if (formatval(fmt, s) && !globmatch(PICFMT, fmt))
147 +                *iap = 0.0;
148 +        return(0);
149   }
150  
151  
# Line 169 | Line 153 | DATARRAY *
153   getpict(pname)                          /* get picture pname */
154   char  *pname;
155   {
172        extern char  *libpath;
156          double  inpaspect;
157          char  *pfname;
158          FILE  *fp;
159 <        COLOR  *scanin;
159 >        COLR  *scanin;
160          int  sl, ns;
161          RESOLU  inpres;
162          FLOAT  loc[2];
# Line 181 | Line 164 | char  *pname;
164          register int  x, i;
165          register DATARRAY  *pp;
166                                                  /* look for array in list */
167 <        for (pp = ptab[hash(pname)]; 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 =
196 <        pp[1].name =
197 <        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 < #ifdef MSDOS
204 <        setmode(fileno(fp), O_BINARY);
205 < #endif
184 >        SET_FILE_BINARY(fp);
185                                                  /* get dimensions */
186          inpaspect = 1.0;
187 <        getheader(fp, headaspect, &inpaspect);
188 <        if (!fgetsresolu(&inpres, fp))
187 >        getheader(fp, headaspect, (char *)&inpaspect);
188 >        if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
189                  goto readerr;
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 <                                                /* check memory usage */
213 <        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
214 <        if (i > 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 <        for (i = 0; i < 3; i++) {
216 <                pp[i].nd = 2;
222 <                pp[i].dim[0].ne = inpres.yr;
223 <                pp[i].dim[1].ne = inpres.xr;
224 <                pp[i].dim[0].org =
225 <                pp[i].dim[1].org = 0.0;
226 <                if (inpres.xr <= inpres.yr*inpaspect) {
227 <                        pp[i].dim[0].siz = inpaspect *
228 <                                                (double)inpres.yr/inpres.xr;
229 <                        pp[i].dim[1].siz = 1.0;
230 <                } else {
231 <                        pp[i].dim[0].siz = 1.0;
232 <                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
233 <                                                inpaspect;
234 <                }
235 <                pp[i].dim[0].p = pp[i].dim[1].p = NULL;
236 <                pp[i].arr = (DATATYPE *)
237 <                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
238 <                if (pp[i].arr == NULL)
239 <                        goto memerr;
240 <        }
215 >        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
216 >                goto memerr;
217                                                          /* load picture */
218 <        sl = scanlen(&inpres);
243 <        ns = numscans(&inpres);
244 <        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
218 >        if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
219                  goto memerr;
220          for (y = 0; y < ns; y++) {
221 <                if (freadscan(scanin, sl, fp) < 0)
221 >                if (freadcolrs(scanin, sl, fp) < 0)
222                          goto readerr;
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 <                        pp[0].arr[i] = colval(scanin[x],RED);
254 <                        pp[1].arr[i] = colval(scanin[x],GRN);
255 <                        pp[2].arr[i] = colval(scanin[x],BLU);
227 >                        copycolr(pp[0].arr.c[i], scanin[x]);
228                  }
229          }
230 <        free((char *)scanin);
230 >        free((void *)scanin);
231          fclose(fp);
232          i = hash(pname);
233 <        pp[0].next =
234 <        pp[1].next =
235 <        pp[2].next = ptab[i];
236 <        return(ptab[i] = pp);
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 271 | 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          DATARRAY  head;
254          int  hval, nents;
255 <        register DATARRAY  *dp, *dpl;
255 >        register DATARRAY  *dpl, *dp;
256          register int  i;
257  
258 <        if (dname == NULL) {                    /* free all if NULL */
258 >        if (dta == NULL) {                      /* free all if NULL */
259                  hval = 0; nents = TABSIZ;
260          } else {
261 <                hval = hash(dname); nents = 1;
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 (dname == NULL || !strcmp(dname, dp->name)) {
267 >                        if ((dta == NULL | dta == dp)) {
268                                  dpl->next = dp->next;
269 <                                free((char *)dp->arr);
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((char *)dp->dim[i].p);
275 >                                                free((void *)dp->dim[i].p);
276                                  freestr(dp->name);
277 <                                free((char *)dp);
277 >                                free((void *)dp);
278                          } else
279                                  dpl = dp;
280                  dtab[hval++] = head.next;
# Line 303 | Line 282 | char  *dname;
282   }
283  
284  
306 freepict(pname)                 /* free memory associated with pname */
307 char  *pname;
308 {
309        DATARRAY  head;
310        int  hval, nents;
311        register DATARRAY  *pp, *ppl;
312
313        if (pname == NULL) {                    /* free all if NULL */
314                hval = 0; nents = TABSIZ;
315        } else {
316                hval = hash(pname); nents = 1;
317        }
318        while (nents--) {
319                head.next = ptab[hval];
320                ppl = &head;
321                while ((pp = ppl->next) != NULL)
322                        if (pname == NULL || !strcmp(pname, pp->name)) {
323                                ppl->next = pp->next;
324                                free((char *)pp[0].arr);
325                                free((char *)pp[1].arr);
326                                free((char *)pp[2].arr);
327                                freestr(pp[0].name);
328                                free((char *)pp);
329                        } else
330                                ppl = pp;
331                ptab[hval++] = head.next;
332        }
333 }
334
335
285   double
286   datavalue(dp, pt)               /* interpolate data value at a point */
287   register DATARRAY  *dp;
# Line 342 | Line 291 | double *pt;
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;
# Line 382 | Line 335 | double *pt;
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);
400 <        else if (x < i-1)
401 <                y = (2*y0-y1)/(i-x);
402 <        else
403 <                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