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.1 by greg, Tue Nov 12 17:10:07 1991 UTC vs.
Revision 2.15 by greg, Sat Feb 22 02:07:28 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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.
6 + */
7 +
8 + /* ====================================================================
9 + * The Radiance Software License, Version 1.0
10   *
11 < *     6/4/86
11 > * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 > * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 > *
14 > * Redistribution and use in source and binary forms, with or without
15 > * modification, are permitted provided that the following conditions
16 > * are met:
17 > *
18 > * 1. Redistributions of source code must retain the above copyright
19 > *         notice, this list of conditions and the following disclaimer.
20 > *
21 > * 2. Redistributions in binary form must reproduce the above copyright
22 > *       notice, this list of conditions and the following disclaimer in
23 > *       the documentation and/or other materials provided with the
24 > *       distribution.
25 > *
26 > * 3. The end-user documentation included with the redistribution,
27 > *           if any, must include the following acknowledgment:
28 > *             "This product includes Radiance software
29 > *                 (http://radsite.lbl.gov/)
30 > *                 developed by the Lawrence Berkeley National Laboratory
31 > *               (http://www.lbl.gov/)."
32 > *       Alternately, this acknowledgment may appear in the software itself,
33 > *       if and wherever such third-party acknowledgments normally appear.
34 > *
35 > * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 > *       and "The Regents of the University of California" must
37 > *       not be used to endorse or promote products derived from this
38 > *       software without prior written permission. For written
39 > *       permission, please contact [email protected].
40 > *
41 > * 5. Products derived from this software may not be called "Radiance",
42 > *       nor may "Radiance" appear in their name, without prior written
43 > *       permission of Lawrence Berkeley National Laboratory.
44 > *
45 > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 > * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 > * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 > * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 > * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 > * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 > * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 > * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 > * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 > * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 > * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 > * SUCH DAMAGE.
57 > * ====================================================================
58 > *
59 > * This software consists of voluntary contributions made by many
60 > * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 > * information on Lawrence Berkeley National Laboratory, please see
62 > * <http://www.lbl.gov/>.
63   */
64  
65   #include  "standard.h"
# Line 18 | Line 70 | static char SCCSid[] = "$SunId$ LBL";
70  
71   #include  "data.h"
72  
73 +                                /* picture memory usage before warning */
74 + #ifndef PSIZWARN
75 + #ifdef BIGMEM
76 + #define PSIZWARN        5000000
77 + #else
78 + #define PSIZWARN        1500000
79 + #endif
80 + #endif
81  
82 < extern char  *fgetword();
83 < extern double  atof();
82 > #ifndef TABSIZ
83 > #define TABSIZ          97              /* table size (prime) */
84 > #endif
85  
86 < extern char  *libpath;                  /* library search path */
86 > #define hash(s)         (shash(s)%TABSIZ)
87  
27 static DATARRAY  *dlist = NULL;         /* data array list */
88  
89 < static DATARRAY  *plist = NULL;         /* picture list */
89 > static DATARRAY  *dtab[TABSIZ];         /* data array list */
90  
91  
92   DATARRAY *
93   getdata(dname)                          /* get data array dname */
94   char  *dname;
95   {
36        char  word[64];
96          char  *dfname;
97          FILE  *fp;
98          int  asize;
99          register int  i, j;
100          register DATARRAY  *dp;
101                                                  /* look for array in list */
102 <        for (dp = dlist; dp != NULL; dp = dp->next)
102 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
103                  if (!strcmp(dname, dp->name))
104                          return(dp);             /* found! */
46
105          /*
106           *      If we haven't loaded the data already, we will look
107 <         *  for it in the directorys specified by the library path.
107 >         *  for it in the directories specified by the library path.
108           *
109           *      The file has the following format:
110           *
# Line 64 | Line 122 | char  *dname;
122           *              0 0 ni p0i p1i .. pni
123           */
124  
125 <        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
125 >        if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) {
126                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
127                  error(USER, errmsg);
128          }
71        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
72                goto memerr;
73
74        dp->name = savestr(dname);
75
129          if ((fp = fopen(dfname, "r")) == NULL) {
130                  sprintf(errmsg, "cannot open data file \"%s\"", dfname);
131                  error(SYSTEM, errmsg);
132          }
133                                                          /* get dimensions */
134 <        if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
134 >        if (fgetval(fp, 'i', (char *)&asize) <= 0)
135                  goto scanerr;
136 <        dp->nd = atoi(word);
84 <        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
136 >        if (asize <= 0 | asize > MAXDDIM) {
137                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
138                  error(USER, errmsg);
139          }
140 +        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
141 +                goto memerr;
142 +        dp->name = savestr(dname);
143 +        dp->type = DATATY;
144 +        dp->nd = asize;
145          asize = 1;
146          for (i = 0; i < dp->nd; i++) {
147 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
147 >                if (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0)
148                          goto scanerr;
149 <                dp->dim[i].org = atof(word);
93 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
149 >                if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0)
150                          goto scanerr;
151 <                dp->dim[i].siz = atof(word);
96 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
151 >                if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
152                          goto scanerr;
98                dp->dim[i].ne = atoi(word);
153                  if (dp->dim[i].ne < 2)
154                          goto scanerr;
155                  asize *= dp->dim[i].ne;
156                  if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
157 <                        dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
157 >                        dp->dim[i].p = (DATATYPE *)
158 >                                        malloc(dp->dim[i].ne*sizeof(DATATYPE));
159                          if (dp->dim[i].p == NULL)
160                                  goto memerr;
161 <                        for (j = 0; j < dp->dim[i].ne; j++) {
162 <                                if (fgetword(word, sizeof(word), fp) == NULL ||
163 <                                                !isflt(word))
161 >                        for (j = 0; j < dp->dim[i].ne; j++)
162 >                                if (fgetval(fp, DATATY,
163 >                                                (char *)&dp->dim[i].p[j]) <= 0)
164                                          goto scanerr;
110                                dp->dim[i].p[j] = atof(word);
111                        }
165                          for (j = 1; j < dp->dim[i].ne-1; j++)
166                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
167                                          (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
# Line 119 | Line 172 | char  *dname;
172                  } else
173                          dp->dim[i].p = NULL;
174          }
175 <        if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
175 >        if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
176                  goto memerr;
177          
178 <        for (i = 0; i < asize; i++) {
179 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
178 >        for (i = 0; i < asize; i++)
179 >                if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0)
180                          goto scanerr;
128                dp->arr[i] = atof(word);
129        }
181          fclose(fp);
182 <        dp->next = dlist;
183 <        return(dlist = dp);
182 >        i = hash(dname);
183 >        dp->next = dtab[i];
184 >        return(dtab[i] = dp);
185  
186   memerr:
187          error(SYSTEM, "out of memory in getdata");
# Line 140 | Line 192 | scanerr:
192   }
193  
194  
195 < static double  inpaspect;               /* aspect ratio of input picture */
196 <
145 < static
146 < headaspect(s)                           /* check string for aspect ratio */
195 > static int
196 > headaspect(s, iap)                      /* check string for aspect ratio */
197   char  *s;
198 + double  *iap;
199   {
200 +        char    fmt[32];
201 +
202          if (isaspect(s))
203 <                inpaspect *= aspectval(s);
203 >                *iap *= aspectval(s);
204 >        else if (formatval(fmt, s) && !globmatch(PICFMT, fmt))
205 >                *iap = 0.0;
206 >        return(0);
207   }
208  
209  
# Line 155 | Line 211 | DATARRAY *
211   getpict(pname)                          /* get picture pname */
212   char  *pname;
213   {
214 <        extern char  *libpath;
214 >        double  inpaspect;
215          char  *pfname;
216          FILE  *fp;
217 <        COLOR  *scanin;
217 >        COLR  *scanin;
218          int  sl, ns;
219 <        RESOLU  inpres;
219 >        RESOLU  inpres;
220          FLOAT  loc[2];
221          int  y;
222          register int  x, i;
223          register DATARRAY  *pp;
224                                                  /* look for array in list */
225 <        for (pp = plist; pp != NULL; pp = pp->next)
225 >        for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next)
226                  if (!strcmp(pname, pp->name))
227                          return(pp);             /* found! */
228  
229 <        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
229 >        if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) {
230                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
231                  error(USER, errmsg);
232          }
233 <        if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL)
233 >        if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
234                  goto memerr;
235  
236 <        pp[0].name =
181 <        pp[1].name =
182 <        pp[2].name = savestr(pname);
236 >        pp[0].name = savestr(pname);
237  
238          if ((fp = fopen(pfname, "r")) == NULL) {
239                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
240                  error(SYSTEM, errmsg);
241          }
242 + #ifdef MSDOS
243 +        setmode(fileno(fp), O_BINARY);
244 + #endif
245                                                  /* get dimensions */
246          inpaspect = 1.0;
247 <        getheader(fp, headaspect);
248 <        if (!fgetsresolu(&inpres, fp))
247 >        getheader(fp, headaspect, (char *)&inpaspect);
248 >        if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
249                  goto readerr;
250 <        for (i = 0; i < 3; i++) {
251 <                pp[i].nd = 2;
252 <                pp[i].dim[0].ne = inpres.yr;
253 <                pp[i].dim[1].ne = inpres.xr;
254 <                pp[i].dim[0].org =
255 <                pp[i].dim[1].org = 0.0;
256 <                if (inpres.xr <= inpres.yr*inpaspect) {
257 <                        pp[i].dim[0].siz = inpaspect *
258 <                                                (double)inpres.yr/inpres.xr;
259 <                        pp[i].dim[1].siz = 1.0;
260 <                } else {
261 <                        pp[i].dim[0].siz = 1.0;
262 <                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
206 <                                                inpaspect;
207 <                }
208 <                pp[i].dim[0].p = pp[i].dim[1].p = NULL;
209 <                pp[i].arr = (DATATYPE *)
210 <                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
211 <                if (pp[i].arr == NULL)
212 <                        goto memerr;
250 >        pp[0].nd = 2;
251 >        pp[0].dim[0].ne = inpres.yr;
252 >        pp[0].dim[1].ne = inpres.xr;
253 >        pp[0].dim[0].org =
254 >        pp[0].dim[1].org = 0.0;
255 >        if (inpres.xr <= inpres.yr*inpaspect) {
256 >                pp[0].dim[0].siz = inpaspect *
257 >                                        (double)inpres.yr/inpres.xr;
258 >                pp[0].dim[1].siz = 1.0;
259 >        } else {
260 >                pp[0].dim[0].siz = 1.0;
261 >                pp[0].dim[1].siz = (double)inpres.xr/inpres.yr /
262 >                                        inpaspect;
263          }
264 <                                                        /* load picture */
265 <        sl = scanlen(&inpres);
264 >        pp[0].dim[0].p = pp[0].dim[1].p = NULL;
265 >        sl = scanlen(&inpres);                          /* allocate array */
266          ns = numscans(&inpres);
267 <        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
267 >        i = ns*sl*sizeof(COLR);
268 > #if PSIZWARN
269 >        if (i > PSIZWARN) {                             /* memory warning */
270 >                sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
271 >                                pname, i);
272 >                error(WARNING, errmsg);
273 >        }
274 > #endif
275 >        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
276                  goto memerr;
277 +                                                        /* load picture */
278 +        if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
279 +                goto memerr;
280          for (y = 0; y < ns; y++) {
281 <                if (freadscan(scanin, sl, fp) < 0)
281 >                if (freadcolrs(scanin, sl, fp) < 0)
282                          goto readerr;
283                  for (x = 0; x < sl; x++) {
284                          pix2loc(loc, &inpres, x, y);
285                          i = (int)(loc[1]*inpres.yr)*inpres.xr +
286                                          (int)(loc[0]*inpres.xr);
287 <                        pp[0].arr[i] = colval(scanin[x],RED);
227 <                        pp[1].arr[i] = colval(scanin[x],GRN);
228 <                        pp[2].arr[i] = colval(scanin[x],BLU);
287 >                        copycolr(pp[0].arr.c[i], scanin[x]);
288                  }
289          }
290 <        free((char *)scanin);
290 >        free((void *)scanin);
291          fclose(fp);
292 <        pp[0].next =
293 <        pp[1].next =
294 <        pp[2].next = plist;
295 <        return(plist = pp);
292 >        i = hash(pname);
293 >        pp[0].next = dtab[i];           /* link into picture list */
294 >        copystruct(&pp[1], &pp[0]);
295 >        copystruct(&pp[2], &pp[0]);
296 >        pp[0].type = RED;               /* differentiate RGB records */
297 >        pp[1].type = GRN;
298 >        pp[2].type = BLU;
299 >        return(dtab[i] = pp);
300  
301   memerr:
302          error(SYSTEM, "out of memory in getpict");
# Line 243 | Line 306 | readerr:
306   }
307  
308  
309 < freedata(dname)                 /* free memory associated with dname */
310 < char  *dname;
309 > void
310 > freedata(dta)                   /* release data array reference */
311 > DATARRAY  *dta;
312   {
313 <        register DATARRAY  *dp, *dpl;
313 >        DATARRAY  head;
314 >        int  hval, nents;
315 >        register DATARRAY  *dpl, *dp;
316          register int  i;
317  
318 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
319 <                if (!strcmp(dname, dp->name)) {
320 <                        if (dpl == NULL)
321 <                                dlist = dp->next;
322 <                        else
318 >        if (dta == NULL) {                      /* free all if NULL */
319 >                hval = 0; nents = TABSIZ;
320 >        } else {
321 >                hval = hash(dta->name); nents = 1;
322 >        }
323 >        while (nents--) {
324 >                head.next = dtab[hval];
325 >                dpl = &head;
326 >                while ((dp = dpl->next) != NULL)
327 >                        if ((dta == NULL | dta == dp)) {
328                                  dpl->next = dp->next;
329 <                        free((char *)dp->arr);
330 <                        for (i = 0; i < dp->nd; i++)
331 <                                if (dp->dim[i].p != NULL)
332 <                                        free((char *)dp->dim[i].p);
333 <                        freestr(dp->name);
334 <                        free((char *)dp);
335 <                        return;
336 <                }
329 >                                if (dp->type == DATATY)
330 >                                        free((void *)dp->arr.d);
331 >                                else
332 >                                        free((void *)dp->arr.c);
333 >                                for (i = 0; i < dp->nd; i++)
334 >                                        if (dp->dim[i].p != NULL)
335 >                                                free((void *)dp->dim[i].p);
336 >                                freestr(dp->name);
337 >                                free((void *)dp);
338 >                        } else
339 >                                dpl = dp;
340 >                dtab[hval++] = head.next;
341 >        }
342   }
343  
344  
269 freepict(pname)                 /* free memory associated with pname */
270 char  *pname;
271 {
272        register DATARRAY  *pp, *ppl;
273
274        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
275                if (!strcmp(pname, pp->name)) {
276                        if (ppl == NULL)
277                                plist = pp->next;
278                        else
279                                ppl->next = pp->next;
280                        free((char *)pp[0].arr);
281                        free((char *)pp[1].arr);
282                        free((char *)pp[2].arr);
283                        freestr(pp[0].name);
284                        free((char *)pp);
285                        return;
286                }
287 }
288
289
345   double
346   datavalue(dp, pt)               /* interpolate data value at a point */
347   register DATARRAY  *dp;
348 < double  *pt;
348 > double  *pt;
349   {
350          DATARRAY  sd;
351          int  asize;
352          int  lower, upper;
353          register int  i;
354 <        double  x, y, y0, y1;
354 >        double  x, y0, y1;
355                                          /* set up dimensions for recursion */
356 <        sd.nd = dp->nd - 1;
357 <        asize = 1;
358 <        for (i = 0; i < sd.nd; i++) {
359 <                sd.dim[i].org = dp->dim[i+1].org;
360 <                sd.dim[i].siz = dp->dim[i+1].siz;
361 <                sd.dim[i].p = dp->dim[i+1].p;
362 <                asize *= sd.dim[i].ne = dp->dim[i+1].ne;
356 >        if (dp->nd > 1) {
357 >                sd.name = dp->name;
358 >                sd.type = dp->type;
359 >                sd.nd = dp->nd - 1;
360 >                asize = 1;
361 >                for (i = 0; i < sd.nd; i++) {
362 >                        sd.dim[i].org = dp->dim[i+1].org;
363 >                        sd.dim[i].siz = dp->dim[i+1].siz;
364 >                        sd.dim[i].p = dp->dim[i+1].p;
365 >                        asize *= sd.dim[i].ne = dp->dim[i+1].ne;
366 >                }
367          }
368                                          /* get independent variable */
369          if (dp->dim[0].p == NULL) {             /* evenly spaced points */
370                  x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
371 <                x = x * (dp->dim[0].ne - 1);
371 >                x *= (double)(dp->dim[0].ne - 1);
372                  i = x;
373                  if (i < 0)
374                          i = 0;
# Line 336 | Line 395 | double  *pt;
395                                  (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
396          }
397                                          /* get dependent variable */
398 <        if (dp->nd == 1) {
399 <                y0 = dp->arr[i];
400 <                y1 = dp->arr[i+1];
398 >        if (dp->nd > 1) {
399 >                if (dp->type == DATATY) {
400 >                        sd.arr.d = dp->arr.d + i*asize;
401 >                        y0 = datavalue(&sd, pt+1);
402 >                        sd.arr.d = dp->arr.d + (i+1)*asize;
403 >                        y1 = datavalue(&sd, pt+1);
404 >                } else {
405 >                        sd.arr.c = dp->arr.c + i*asize;
406 >                        y0 = datavalue(&sd, pt+1);
407 >                        sd.arr.c = dp->arr.c + (i+1)*asize;
408 >                        y1 = datavalue(&sd, pt+1);
409 >                }
410          } else {
411 <                sd.arr = &dp->arr[i*asize];
412 <                y0 = datavalue(&sd, pt+1);
413 <                sd.arr = &dp->arr[(i+1)*asize];
414 <                y1 = datavalue(&sd, pt+1);
411 >                if (dp->type == DATATY) {
412 >                        y0 = dp->arr.d[i];
413 >                        y1 = dp->arr.d[i+1];
414 >                } else {
415 >                        y0 = colrval(dp->arr.c[i],dp->type);
416 >                        y1 = colrval(dp->arr.c[i+1],dp->type);
417 >                }
418          }
419          /*
420           * Extrapolate as far as one division, then
421           * taper off harmonically to zero.
422           */
423          if (x > i+2)
424 <                y = (2*y1-y0)/(x-i-1);
354 <        else if (x < i-1)
355 <                y = (2*y0-y1)/(i-x);
356 <        else
357 <                y = y0*((i+1)-x) + y1*(x-i);
424 >                return( (2*y1-y0)/(x-(i-1)) );
425  
426 <        return(y);
426 >        if (x < i-1)
427 >                return( (2*y0-y1)/(i-x) );
428 >
429 >        return( y0*((i+1)-x) + y1*(x-i) );
430   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines