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.8 by greg, Thu Apr 14 04:50:27 1994 UTC vs.
Revision 2.26 by schorsch, Fri Jan 2 11:43:42 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"
18
16   #include  "data.h"
17  
18                                  /* picture memory usage before warning */
19   #ifndef PSIZWARN
20 < #ifdef BIGMEM
21 < #define PSIZWARN        3000000
20 > #ifdef SMLMEM
21 > #define PSIZWARN        1500000
22   #else
23 < #define PSIZWARN        1000000
23 > #define PSIZWARN        5000000
24   #endif
25   #endif
26  
# Line 34 | Line 31 | static char SCCSid[] = "$SunId$ LBL";
31   #define hash(s)         (shash(s)%TABSIZ)
32  
33  
37 extern char  *fgetword();
38
39 extern char  *getlibpath();             /* library search path */
40
34   static DATARRAY  *dtab[TABSIZ];         /* data array list */
35  
36 < static DATARRAY  *ptab[TABSIZ];         /* picture list */
36 > static gethfunc headaspect;
37  
38  
39   DATARRAY *
40   getdata(dname)                          /* get data array dname */
41   char  *dname;
42   {
50        char  word[64];
43          char  *dfname;
44          FILE  *fp;
45          int  asize;
# Line 57 | Line 49 | char  *dname;
49          for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
50                  if (!strcmp(dname, dp->name))
51                          return(dp);             /* found! */
60
52          /*
53           *      If we haven't loaded the data already, we will look
54           *  for it in the directories specified by the library path.
# Line 78 | Line 69 | char  *dname;
69           *              0 0 ni p0i p1i .. pni
70           */
71  
72 <        if ((dfname = getpath(dname, getlibpath(), 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          }
85        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
86                goto memerr;
87
88        dp->name = savestr(dname);
89
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);
98 <        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);
107 <                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);
110 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
98 >                if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
99                          goto scanerr;
112                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;
124                                dp->dim[i].p[j] = atof(word);
125                        }
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 133 | 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;
142                dp->arr[i] = atof(word);
143        }
128          fclose(fp);
129          i = hash(dname);
130          dp->next = dtab[i];
# Line 155 | 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 172 | Line 162 | char  *pname;
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 = ptab[hash(pname)]; 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, getlibpath(), 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 =
195 <        pp[1].name =
196 <        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
203 <        setmode(fileno(fp), O_BINARY);
204 < #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 +        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 +        pp[0].dim[0].p = pp[0].dim[1].p = NULL;
211 +        sl = scanlen(&inpres);                          /* allocate array */
212 +        ns = numscans(&inpres);
213 +        i = ns*sl*sizeof(COLR);
214   #if PSIZWARN
215 <                                                /* check memory usage */
212 <        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
213 <        if (i > 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 <        for (i = 0; i < 3; i++) {
222 <                pp[i].nd = 2;
221 <                pp[i].dim[0].ne = inpres.yr;
222 <                pp[i].dim[1].ne = inpres.xr;
223 <                pp[i].dim[0].org =
224 <                pp[i].dim[1].org = 0.0;
225 <                if (inpres.xr <= inpres.yr*inpaspect) {
226 <                        pp[i].dim[0].siz = inpaspect *
227 <                                                (double)inpres.yr/inpres.xr;
228 <                        pp[i].dim[1].siz = 1.0;
229 <                } else {
230 <                        pp[i].dim[0].siz = 1.0;
231 <                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
232 <                                                inpaspect;
233 <                }
234 <                pp[i].dim[0].p = pp[i].dim[1].p = NULL;
235 <                pp[i].arr = (DATATYPE *)
236 <                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
237 <                if (pp[i].arr == NULL)
238 <                        goto memerr;
239 <        }
221 >        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
222 >                goto memerr;
223                                                          /* load picture */
224 <        sl = scanlen(&inpres);
242 <        ns = numscans(&inpres);
243 <        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
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);
253 <                        pp[1].arr[i] = colval(scanin[x],GRN);
254 <                        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          i = hash(pname);
239 <        pp[0].next =
240 <        pp[1].next =
241 <        pp[2].next = ptab[i];
242 <        return(ptab[i] = pp);
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 270 | 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          DATARRAY  head;
260          int  hval, nents;
261 <        register DATARRAY  *dp, *dpl;
261 >        register DATARRAY  *dpl, *dp;
262          register int  i;
263  
264 <        if (dname == NULL) {                    /* free all if NULL */
264 >        if (dta == NULL) {                      /* free all if NULL */
265                  hval = 0; nents = TABSIZ;
266          } else {
267 <                hval = hash(dname); nents = 1;
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 (dname == NULL || !strcmp(dname, dp->name)) {
273 >                        if ((dta == NULL) | (dta == dp)) {
274                                  dpl->next = dp->next;
275 <                                free((char *)dp->arr);
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((char *)dp->dim[i].p);
281 >                                                free((void *)dp->dim[i].p);
282                                  freestr(dp->name);
283 <                                free((char *)dp);
283 >                                free((void *)dp);
284                          } else
285                                  dpl = dp;
286                  dtab[hval++] = head.next;
# Line 302 | Line 288 | char  *dname;
288   }
289  
290  
305 freepict(pname)                 /* free memory associated with pname */
306 char  *pname;
307 {
308        DATARRAY  head;
309        int  hval, nents;
310        register DATARRAY  *pp, *ppl;
311
312        if (pname == NULL) {                    /* free all if NULL */
313                hval = 0; nents = TABSIZ;
314        } else {
315                hval = hash(pname); nents = 1;
316        }
317        while (nents--) {
318                head.next = ptab[hval];
319                ppl = &head;
320                while ((pp = ppl->next) != NULL)
321                        if (pname == NULL || !strcmp(pname, pp->name)) {
322                                ppl->next = pp->next;
323                                free((char *)pp[0].arr);
324                                free((char *)pp[1].arr);
325                                free((char *)pp[2].arr);
326                                freestr(pp[0].name);
327                                free((char *)pp);
328                        } else
329                                ppl = pp;
330                ptab[hval++] = head.next;
331        }
332 }
333
334
291   double
292   datavalue(dp, pt)               /* interpolate data value at a point */
293   register DATARRAY  *dp;
# Line 341 | 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 381 | 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);
399 <        else if (x < i-1)
400 <                y = (2*y0-y1)/(i-x);
401 <        else
402 <                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