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.27 by schorsch, Tue Mar 30 16:13:01 2004 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  "color.h"
10 > #include  <time.h>
11  
12 + #include  "platform.h"
13 + #include  "standard.h"
14 + #include  "color.h"
15   #include  "resolu.h"
16 <
16 > #include  "view.h"
17   #include  "data.h"
18  
19                                  /* picture memory usage before warning */
20   #ifndef PSIZWARN
21 < #ifdef BIGMEM
22 < #define PSIZWARN        3000000
21 > #ifdef SMLMEM
22 > #define PSIZWARN        1500000
23   #else
24 < #define PSIZWARN        1000000
24 > #define PSIZWARN        5000000
25   #endif
26   #endif
27  
# Line 34 | Line 32 | static char SCCSid[] = "$SunId$ LBL";
32   #define hash(s)         (shash(s)%TABSIZ)
33  
34  
37 extern char  *fgetword();
38
39 extern char  *libpath;                  /* library search path */
40
35   static DATARRAY  *dtab[TABSIZ];         /* data array list */
36  
37 < static DATARRAY  *ptab[TABSIZ];         /* picture list */
37 > static gethfunc headaspect;
38  
39  
40 < DATARRAY *
41 < getdata(dname)                          /* get data array dname */
42 < char  *dname;
40 > extern DATARRAY *
41 > getdata(                                /* get data array dname */
42 >        char  *dname
43 > )
44   {
50        char  word[64];
45          char  *dfname;
46          FILE  *fp;
47          int  asize;
# Line 57 | Line 51 | char  *dname;
51          for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
52                  if (!strcmp(dname, dp->name))
53                          return(dp);             /* found! */
60
54          /*
55           *      If we haven't loaded the data already, we will look
56           *  for it in the directories specified by the library path.
# Line 78 | 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, getrlibpath(), R_OK)) == NULL) {
75                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
76                  error(USER, errmsg);
77          }
85        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
86                goto memerr;
87
88        dp->name = savestr(dname);
89
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', (char *)&asize) <= 0)
84                  goto scanerr;
85 <        dp->nd = atoi(word);
98 <        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, (char *)&dp->dim[i].org) <= 0)
97                          goto scanerr;
98 <                dp->dim[i].org = atof(word);
107 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
98 >                if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0)
99                          goto scanerr;
100 <                dp->dim[i].siz = atof(word);
110 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
100 >                if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
101                          goto scanerr;
112                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 ||
112 <                                                !isflt(word))
110 >                        for (j = 0; j < dp->dim[i].ne; j++)
111 >                                if (fgetval(fp, DATATY,
112 >                                                (char *)&dp->dim[i].p[j]) <= 0)
113                                          goto scanerr;
124                                dp->dim[i].p[j] = atof(word);
125                        }
114                          for (j = 1; j < dp->dim[i].ne-1; j++)
115                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
116                                          (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
# Line 133 | Line 121 | char  *dname;
121                  } else
122                          dp->dim[i].p = NULL;
123          }
124 <        if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
124 >        if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
125                  goto memerr;
126          
127 <        for (i = 0; i < asize; i++) {
128 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
127 >        for (i = 0; i < asize; i++)
128 >                if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0)
129                          goto scanerr;
142                dp->arr[i] = atof(word);
143        }
130          fclose(fp);
131          i = hash(dname);
132          dp->next = dtab[i];
# Line 152 | Line 138 | scanerr:
138          sprintf(errmsg, "%s in data file \"%s\"",
139                          feof(fp) ? "unexpected EOF" : "bad format", dfname);
140          error(USER, errmsg);
141 +        return NULL; /* pro forma return */
142   }
143  
144  
145 < static
146 < headaspect(s, iap)                      /* check string for aspect ratio */
147 < char  *s;
148 < double  *iap;
145 > static int
146 > headaspect(                     /* check string for aspect ratio */
147 >        char  *s,
148 >        void  *iap
149 > )
150   {
151 +        char    fmt[32];
152 +
153          if (isaspect(s))
154 <                *iap *= aspectval(s);
154 >                *(double*)iap *= aspectval(s);
155 >        else if (formatval(fmt, s) && !globmatch(PICFMT, fmt))
156 >                *(double*)iap = 0.0;
157 >        return(0);
158   }
159  
160  
161 < DATARRAY *
162 < getpict(pname)                          /* get picture pname */
163 < char  *pname;
161 > extern DATARRAY *
162 > getpict(                                /* get picture pname */
163 >        char  *pname
164 > )
165   {
172        extern char  *libpath;
166          double  inpaspect;
167          char  *pfname;
168          FILE  *fp;
169 <        COLOR  *scanin;
169 >        COLR  *scanin;
170          int  sl, ns;
171          RESOLU  inpres;
172 <        FLOAT  loc[2];
172 >        RREAL  loc[2];
173          int  y;
174          register int  x, i;
175          register DATARRAY  *pp;
176                                                  /* look for array in list */
177 <        for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next)
177 >        for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next)
178                  if (!strcmp(pname, pp->name))
179                          return(pp);             /* found! */
180  
181 <        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
181 >        if ((pfname = getpath(pname, getrlibpath(), R_OK)) == NULL) {
182                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
183                  error(USER, errmsg);
184          }
185 <        if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL)
185 >        if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
186                  goto memerr;
187  
188 <        pp[0].name =
196 <        pp[1].name =
197 <        pp[2].name = savestr(pname);
188 >        pp[0].name = savestr(pname);
189  
190          if ((fp = fopen(pfname, "r")) == NULL) {
191                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
192                  error(SYSTEM, errmsg);
193          }
194 < #ifdef MSDOS
204 <        setmode(fileno(fp), O_BINARY);
205 < #endif
194 >        SET_FILE_BINARY(fp);
195                                                  /* get dimensions */
196          inpaspect = 1.0;
197          getheader(fp, headaspect, &inpaspect);
198 <        if (!fgetsresolu(&inpres, fp))
198 >        if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
199                  goto readerr;
200 +        pp[0].nd = 2;
201 +        pp[0].dim[0].ne = inpres.yr;
202 +        pp[0].dim[1].ne = inpres.xr;
203 +        pp[0].dim[0].org =
204 +        pp[0].dim[1].org = 0.0;
205 +        if (inpres.xr <= inpres.yr*inpaspect) {
206 +                pp[0].dim[0].siz = inpaspect *
207 +                                        (double)inpres.yr/inpres.xr;
208 +                pp[0].dim[1].siz = 1.0;
209 +        } else {
210 +                pp[0].dim[0].siz = 1.0;
211 +                pp[0].dim[1].siz = (double)inpres.xr/inpres.yr /
212 +                                        inpaspect;
213 +        }
214 +        pp[0].dim[0].p = pp[0].dim[1].p = NULL;
215 +        sl = scanlen(&inpres);                          /* allocate array */
216 +        ns = numscans(&inpres);
217 +        i = ns*sl*sizeof(COLR);
218   #if PSIZWARN
219 <                                                /* check memory usage */
213 <        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
214 <        if (i > PSIZWARN) {
219 >        if (i > PSIZWARN) {                             /* memory warning */
220                  sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
221                                  pname, i);
222                  error(WARNING, errmsg);
223          }
224   #endif
225 <        for (i = 0; i < 3; i++) {
226 <                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 <        }
225 >        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
226 >                goto memerr;
227                                                          /* load picture */
228 <        sl = scanlen(&inpres);
243 <        ns = numscans(&inpres);
244 <        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
228 >        if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
229                  goto memerr;
230          for (y = 0; y < ns; y++) {
231 <                if (freadscan(scanin, sl, fp) < 0)
231 >                if (freadcolrs(scanin, sl, fp) < 0)
232                          goto readerr;
233                  for (x = 0; x < sl; x++) {
234                          pix2loc(loc, &inpres, x, y);
235                          i = (int)(loc[1]*inpres.yr)*inpres.xr +
236                                          (int)(loc[0]*inpres.xr);
237 <                        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);
237 >                        copycolr(pp[0].arr.c[i], scanin[x]);
238                  }
239          }
240 <        free((char *)scanin);
240 >        free((void *)scanin);
241          fclose(fp);
242          i = hash(pname);
243 <        pp[0].next =
244 <        pp[1].next =
245 <        pp[2].next = ptab[i];
246 <        return(ptab[i] = pp);
243 >        pp[0].next = dtab[i];           /* link into picture list */
244 >        pp[1] = pp[0];
245 >        pp[2] = pp[0];
246 >        pp[0].type = RED;               /* differentiate RGB records */
247 >        pp[1].type = GRN;
248 >        pp[2].type = BLU;
249 >        return(dtab[i] = pp);
250  
251   memerr:
252          error(SYSTEM, "out of memory in getpict");
253   readerr:
254          sprintf(errmsg, "bad picture file \"%s\"", pfname);
255          error(USER, errmsg);
256 +        return NULL; /* pro forma return */
257   }
258  
259  
260 < freedata(dname)                 /* free memory associated with dname */
261 < char  *dname;
260 > extern void
261 > freedata(                       /* release data array reference */
262 >        DATARRAY  *dta
263 > )
264   {
265          DATARRAY  head;
266          int  hval, nents;
267 <        register DATARRAY  *dp, *dpl;
267 >        register DATARRAY  *dpl, *dp;
268          register int  i;
269  
270 <        if (dname == NULL) {                    /* free all if NULL */
270 >        if (dta == NULL) {                      /* free all if NULL */
271                  hval = 0; nents = TABSIZ;
272          } else {
273 <                hval = hash(dname); nents = 1;
273 >                hval = hash(dta->name); nents = 1;
274          }
275          while (nents--) {
276                  head.next = dtab[hval];
277                  dpl = &head;
278                  while ((dp = dpl->next) != NULL)
279 <                        if (dname == NULL || !strcmp(dname, dp->name)) {
279 >                        if ((dta == NULL) | (dta == dp)) {
280                                  dpl->next = dp->next;
281 <                                free((char *)dp->arr);
281 >                                if (dp->type == DATATY)
282 >                                        free((void *)dp->arr.d);
283 >                                else
284 >                                        free((void *)dp->arr.c);
285                                  for (i = 0; i < dp->nd; i++)
286                                          if (dp->dim[i].p != NULL)
287 <                                                free((char *)dp->dim[i].p);
287 >                                                free((void *)dp->dim[i].p);
288                                  freestr(dp->name);
289 <                                free((char *)dp);
289 >                                free((void *)dp);
290                          } else
291                                  dpl = dp;
292                  dtab[hval++] = head.next;
# Line 303 | Line 294 | char  *dname;
294   }
295  
296  
297 < freepict(pname)                 /* free memory associated with pname */
298 < char  *pname;
297 > extern double
298 > datavalue(              /* interpolate data value at a point */
299 >        register DATARRAY  *dp,
300 >        double  *pt
301 > )
302   {
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
336 double
337 datavalue(dp, pt)               /* interpolate data value at a point */
338 register DATARRAY  *dp;
339 double  *pt;
340 {
303          DATARRAY  sd;
304          int  asize;
305          int  lower, upper;
306          register int  i;
307 <        double  x, y, y0, y1;
307 >        double  x, y0, y1;
308                                          /* set up dimensions for recursion */
309 <        sd.nd = dp->nd - 1;
310 <        asize = 1;
311 <        for (i = 0; i < sd.nd; i++) {
312 <                sd.dim[i].org = dp->dim[i+1].org;
313 <                sd.dim[i].siz = dp->dim[i+1].siz;
314 <                sd.dim[i].p = dp->dim[i+1].p;
315 <                asize *= sd.dim[i].ne = dp->dim[i+1].ne;
309 >        if (dp->nd > 1) {
310 >                sd.name = dp->name;
311 >                sd.type = dp->type;
312 >                sd.nd = dp->nd - 1;
313 >                asize = 1;
314 >                for (i = 0; i < sd.nd; i++) {
315 >                        sd.dim[i].org = dp->dim[i+1].org;
316 >                        sd.dim[i].siz = dp->dim[i+1].siz;
317 >                        sd.dim[i].p = dp->dim[i+1].p;
318 >                        asize *= sd.dim[i].ne = dp->dim[i+1].ne;
319 >                }
320          }
321                                          /* get independent variable */
322          if (dp->dim[0].p == NULL) {             /* evenly spaced points */
323                  x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
324 <                x = x * (dp->dim[0].ne - 1);
324 >                x *= (double)(dp->dim[0].ne - 1);
325                  i = x;
326                  if (i < 0)
327                          i = 0;
# Line 382 | Line 348 | double *pt;
348                                  (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
349          }
350                                          /* get dependent variable */
351 <        if (dp->nd == 1) {
352 <                y0 = dp->arr[i];
353 <                y1 = dp->arr[i+1];
351 >        if (dp->nd > 1) {
352 >                if (dp->type == DATATY) {
353 >                        sd.arr.d = dp->arr.d + i*asize;
354 >                        y0 = datavalue(&sd, pt+1);
355 >                        sd.arr.d = dp->arr.d + (i+1)*asize;
356 >                        y1 = datavalue(&sd, pt+1);
357 >                } else {
358 >                        sd.arr.c = dp->arr.c + i*asize;
359 >                        y0 = datavalue(&sd, pt+1);
360 >                        sd.arr.c = dp->arr.c + (i+1)*asize;
361 >                        y1 = datavalue(&sd, pt+1);
362 >                }
363          } else {
364 <                sd.arr = &dp->arr[i*asize];
365 <                y0 = datavalue(&sd, pt+1);
366 <                sd.arr = &dp->arr[(i+1)*asize];
367 <                y1 = datavalue(&sd, pt+1);
364 >                if (dp->type == DATATY) {
365 >                        y0 = dp->arr.d[i];
366 >                        y1 = dp->arr.d[i+1];
367 >                } else {
368 >                        y0 = colrval(dp->arr.c[i],dp->type);
369 >                        y1 = colrval(dp->arr.c[i+1],dp->type);
370 >                }
371          }
372          /*
373           * Extrapolate as far as one division, then
374           * taper off harmonically to zero.
375           */
376          if (x > i+2)
377 <                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);
377 >                return( (2*y1-y0)/(x-(i-1)) );
378  
379 <        return(y);
379 >        if (x < i-1)
380 >                return( (2*y0-y1)/(i-x) );
381 >
382 >        return( y0*((i+1)-x) + y1*(x-i) );
383   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines