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.15 by greg, Sat Feb 22 02:07:28 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.
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 21 | Line 73 | static char SCCSid[] = "$SunId$ LBL";
73                                  /* picture memory usage before warning */
74   #ifndef PSIZWARN
75   #ifdef BIGMEM
76 < #define PSIZWARN        3000000
76 > #define PSIZWARN        5000000
77   #else
78 < #define PSIZWARN        1000000
78 > #define PSIZWARN        1500000
79   #endif
80   #endif
81  
# Line 34 | Line 86 | static char SCCSid[] = "$SunId$ LBL";
86   #define hash(s)         (shash(s)%TABSIZ)
87  
88  
37 extern char  *fgetword();
38
39 extern char  *getlibpath();             /* library search path */
40
89   static DATARRAY  *dtab[TABSIZ];         /* data array list */
90  
43 static DATARRAY  *ptab[TABSIZ];         /* picture list */
91  
45
92   DATARRAY *
93   getdata(dname)                          /* get data array dname */
94   char  *dname;
95   {
50        char  word[64];
96          char  *dfname;
97          FILE  *fp;
98          int  asize;
# Line 57 | Line 102 | char  *dname;
102          for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
103                  if (!strcmp(dname, dp->name))
104                          return(dp);             /* found! */
60
105          /*
106           *      If we haven't loaded the data already, we will look
107           *  for it in the directories specified by the library path.
# Line 82 | Line 126 | char  *dname;
126                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
127                  error(USER, errmsg);
128          }
85        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
86                goto memerr;
87
88        dp->name = savestr(dname);
89
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);
98 <        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);
107 <                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);
110 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
151 >                if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
152                          goto scanerr;
112                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;
124                                dp->dim[i].p[j] = atof(word);
125                        }
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 133 | 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;
142                dp->arr[i] = atof(word);
143        }
181          fclose(fp);
182          i = hash(dname);
183          dp->next = dtab[i];
# Line 155 | Line 192 | scanerr:
192   }
193  
194  
195 < static
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                  *iap *= aspectval(s);
204 +        else if (formatval(fmt, s) && !globmatch(PICFMT, fmt))
205 +                *iap = 0.0;
206 +        return(0);
207   }
208  
209  
# Line 172 | Line 214 | char  *pname;
214          double  inpaspect;
215          char  *pfname;
216          FILE  *fp;
217 <        COLOR  *scanin;
217 >        COLR  *scanin;
218          int  sl, ns;
219          RESOLU  inpres;
220          FLOAT  loc[2];
# Line 180 | Line 222 | char  *pname;
222          register int  x, i;
223          register DATARRAY  *pp;
224                                                  /* look for array in list */
225 <        for (pp = ptab[hash(pname)]; 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  
# Line 188 | Line 230 | char  *pname;
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 =
195 <        pp[1].name =
196 <        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);
# Line 204 | Line 244 | char  *pname;
244   #endif
245                                                  /* get dimensions */
246          inpaspect = 1.0;
247 <        getheader(fp, headaspect, &inpaspect);
248 <        if (!fgetsresolu(&inpres, fp))
247 >        getheader(fp, headaspect, (char *)&inpaspect);
248 >        if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
249                  goto readerr;
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 +        pp[0].dim[0].p = pp[0].dim[1].p = NULL;
265 +        sl = scanlen(&inpres);                          /* allocate array */
266 +        ns = numscans(&inpres);
267 +        i = ns*sl*sizeof(COLR);
268   #if PSIZWARN
269 <                                                /* check memory usage */
212 <        i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr;
213 <        if (i > 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 <        for (i = 0; i < 3; i++) {
276 <                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 <        }
275 >        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
276 >                goto memerr;
277                                                          /* load picture */
278 <        sl = scanlen(&inpres);
242 <        ns = numscans(&inpres);
243 <        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
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);
253 <                        pp[1].arr[i] = colval(scanin[x],GRN);
254 <                        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          i = hash(pname);
293 <        pp[0].next =
294 <        pp[1].next =
295 <        pp[2].next = ptab[i];
296 <        return(ptab[i] = pp);
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 270 | 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          DATARRAY  head;
314          int  hval, nents;
315 <        register DATARRAY  *dp, *dpl;
315 >        register DATARRAY  *dpl, *dp;
316          register int  i;
317  
318 <        if (dname == NULL) {                    /* free all if NULL */
318 >        if (dta == NULL) {                      /* free all if NULL */
319                  hval = 0; nents = TABSIZ;
320          } else {
321 <                hval = hash(dname); nents = 1;
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 (dname == NULL || !strcmp(dname, dp->name)) {
327 >                        if ((dta == NULL | dta == dp)) {
328                                  dpl->next = dp->next;
329 <                                free((char *)dp->arr);
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((char *)dp->dim[i].p);
335 >                                                free((void *)dp->dim[i].p);
336                                  freestr(dp->name);
337 <                                free((char *)dp);
337 >                                free((void *)dp);
338                          } else
339                                  dpl = dp;
340                  dtab[hval++] = head.next;
# Line 302 | Line 342 | char  *dname;
342   }
343  
344  
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
345   double
346   datavalue(dp, pt)               /* interpolate data value at a point */
347   register DATARRAY  *dp;
# Line 341 | Line 351 | double *pt;
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 381 | 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);
399 <        else if (x < i-1)
400 <                y = (2*y0-y1)/(i-x);
401 <        else
402 <                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