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 1.1 by greg, Thu Feb 2 10:41:18 1989 UTC vs.
Revision 2.26 by schorsch, Fri Jan 2 11:43:42 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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   #include  "data.h"
17  
18 +                                /* picture memory usage before warning */
19 + #ifndef PSIZWARN
20 + #ifdef SMLMEM
21 + #define PSIZWARN        1500000
22 + #else
23 + #define PSIZWARN        5000000
24 + #endif
25 + #endif
26  
27 < extern char  *libpath;                  /* library search path */
27 > #ifndef TABSIZ
28 > #define TABSIZ          97              /* table size (prime) */
29 > #endif
30  
31 < static DATARRAY  *dlist = NULL;         /* data array list */
31 > #define hash(s)         (shash(s)%TABSIZ)
32  
24 static DATARRAY  *plist = NULL;         /* picture list */
33  
34 + static DATARRAY  *dtab[TABSIZ];         /* data array list */
35  
36 + static gethfunc headaspect;
37 +
38 +
39   DATARRAY *
40   getdata(dname)                          /* get data array dname */
41   char  *dname;
# Line 31 | Line 43 | char  *dname;
43          char  *dfname;
44          FILE  *fp;
45          int  asize;
46 <        register int  i;
46 >        register int  i, j;
47          register DATARRAY  *dp;
48                                                  /* look for array in list */
49 <        for (dp = dlist; dp != NULL; dp = dp->next)
49 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
50                  if (!strcmp(dname, dp->name))
51                          return(dp);             /* found! */
40
52          /*
53           *      If we haven't loaded the data already, we will look
54 <         *  for it in the directorys specified by the library path.
54 >         *  for it in the directories specified by the library path.
55           *
56           *      The file has the following format:
57           *
58 <         *              #dimensions
58 >         *              N
59           *              beg0    end0    n0
60           *              beg1    end1    n1
61           *              . . .
62 +         *              begN    endN    nN
63           *              data, later dimensions changing faster
64           *              . . .
65           *
66 +         *      For irregularly spaced points, the following can be
67 +         *  substituted for begi endi ni:
68 +         *
69 +         *              0 0 ni p0i p1i .. pni
70           */
71  
72 <        if ((dfname = getpath(dname, libpath)) == NULL) {
72 >        if ((dfname = getpath(dname, getrlibpath(), R_OK)) == NULL) {
73                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
74                  error(USER, errmsg);
75          }
60        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
61                goto memerr;
62
63        dp->name = savestr(dname);
64
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 (fscanf(fp, "%d", &dp->nd) != 1)
81 >        if (fgetval(fp, 'i', (char *)&asize) <= 0)
82                  goto scanerr;
83 <        if (dp->nd <= 0 || dp->nd > MAXDIM) {
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 (fscanf(fp, "%lf %lf %d",
79 <                                &dp->dim[i].org, &dp->dim[i].siz,
80 <                                &dp->dim[i].ne) != 3)
94 >                if (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0)
95                          goto scanerr;
96 <                dp->dim[i].siz -= dp->dim[i].org;
96 >                if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0)
97 >                        goto scanerr;
98 >                if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
99 >                        goto scanerr;
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 = (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 (fgetval(fp, DATATY,
110 +                                                (char *)&dp->dim[i].p[j]) <= 0)
111 +                                        goto scanerr;
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]))
115 +                                        goto scanerr;
116 +                        dp->dim[i].org = dp->dim[i].p[0];
117 +                        dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
118 +                                                - dp->dim[i].p[0];
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 (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
126 >                if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0)
127                          goto scanerr;
91        
128          fclose(fp);
129 <        dp->next = dlist;
130 <        return(dlist = dp);
129 >        i = hash(dname);
130 >        dp->next = dtab[i];
131 >        return(dtab[i] = dp);
132  
133   memerr:
134          error(SYSTEM, "out of memory in getdata");
# Line 102 | Line 139 | scanerr:
139   }
140  
141  
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 +                *(double*)iap *= aspectval(s);
152 +        else if (formatval(fmt, s) && !globmatch(PICFMT, fmt))
153 +                *(double*)iap = 0.0;
154 +        return(0);
155 + }
156 +
157 +
158   DATARRAY *
159   getpict(pname)                          /* get picture pname */
160   char  *pname;
161   {
162 <        extern char  *libpath;
162 >        double  inpaspect;
163          char  *pfname;
164          FILE  *fp;
165 <        COLOR  *scanin;
166 <        int  width, height;
167 <        int  x, y;
168 <        register int  i;
165 >        COLR  *scanin;
166 >        int  sl, ns;
167 >        RESOLU  inpres;
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 = plist; 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, libpath)) == 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 =
130 <        pp[1].name =
131 <        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 +        SET_FILE_BINARY(fp);
191                                                  /* get dimensions */
192 <        getheader(fp, NULL);
193 <        if (fscanf(fp, "-Y %d +X %d\n", &height, &width) != 2)
192 >        inpaspect = 1.0;
193 >        getheader(fp, headaspect, &inpaspect);
194 >        if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
195                  goto readerr;
196 <        for (i = 0; i < 3; i++) {
197 <                pp[i].nd = 2;
198 <                pp[i].dim[0].ne = width;
199 <                pp[i].dim[1].ne = height;
200 <                pp[i].dim[0].org =
201 <                pp[i].dim[1].org = 0.0;
202 <                if (width <= height) {
203 <                        pp[i].dim[0].siz = 1.0;
204 <                        pp[i].dim[1].siz = (double)height/width;
205 <                } else {
206 <                        pp[i].dim[0].siz = (double)width/height;
207 <                        pp[i].dim[1].siz = 1.0;
208 <                }
154 <                pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
155 <                if (pp[i].arr == NULL)
156 <                        goto memerr;
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 +        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 +        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
222 +                goto memerr;
223                                                          /* load picture */
224 <        if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
224 >        if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
225                  goto memerr;
226 <        for (y = height-1; y >= 0; y--) {
227 <                if (freadscan(scanin, width, fp) < 0)
226 >        for (y = 0; y < ns; y++) {
227 >                if (freadcolrs(scanin, sl, fp) < 0)
228                          goto readerr;
229 <                for (x = 0; x < width; x++)
230 <                        for (i = 0; i < 3; i++)
231 <                                pp[i].arr[x*height+y] = colval(scanin[x],i);
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 >                        copycolr(pp[0].arr.c[i], scanin[x]);
234 >                }
235          }
236 <        free((char *)scanin);
236 >        free((void *)scanin);
237          fclose(fp);
238 <        pp[0].next =
239 <        pp[1].next =
240 <        pp[2].next = plist;
241 <        return(plist = pp);
238 >        i = hash(pname);
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 180 | 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 <        register DATARRAY  *dp, *dpl;
259 >        DATARRAY  head;
260 >        int  hval, nents;
261 >        register DATARRAY  *dpl, *dp;
262 >        register int  i;
263  
264 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
265 <                if (!strcmp(dname, dp->name)) {
266 <                        if (dpl == NULL)
267 <                                dlist = dp->next;
268 <                        else
264 >        if (dta == NULL) {                      /* free all if NULL */
265 >                hval = 0; nents = TABSIZ;
266 >        } else {
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 ((dta == NULL) | (dta == dp)) {
274                                  dpl->next = dp->next;
275 <                        free((char *)dp->arr);
276 <                        freestr(dp->name);
277 <                        free((char *)dp);
278 <                        return;
279 <                }
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((void *)dp->dim[i].p);
282 >                                freestr(dp->name);
283 >                                free((void *)dp);
284 >                        } else
285 >                                dpl = dp;
286 >                dtab[hval++] = head.next;
287 >        }
288   }
289  
290  
202 freepict(pname)                 /* free memory associated with pname */
203 char  *pname;
204 {
205        register DATARRAY  *pp, *ppl;
206
207        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
208                if (!strcmp(pname, pp->name)) {
209                        if (ppl == NULL)
210                                plist = pp->next;
211                        else
212                                ppl->next = pp->next;
213                        free((char *)pp[0].arr);
214                        free((char *)pp[1].arr);
215                        free((char *)pp[2].arr);
216                        freestr(pp[0].name);
217                        free((char *)pp);
218                        return;
219                }
220 }
221
222
291   double
292   datavalue(dp, pt)               /* interpolate data value at a point */
293   register DATARRAY  *dp;
294 < double  *pt;
294 > double  *pt;
295   {
296          DATARRAY  sd;
297          int  asize;
298 +        int  lower, upper;
299          register int  i;
300 <        double  x, y, y0, y1;
301 <
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 <                asize *= sd.dim[i].ne = dp->dim[i+1].ne;
300 >        double  x, y0, y1;
301 >                                        /* set up dimensions for recursion */
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 <
315 <        x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
316 <
317 <        x *= dp->dim[0].ne - 1;
318 <
319 <        i = x;
320 <        if (i < 0)
321 <                i = 0;
322 <        else if (i > dp->dim[0].ne - 2)
323 <                i = dp->dim[0].ne - 2;
324 <
325 <        if (dp->nd == 1) {
326 <                y0 = dp->arr[i];
327 <                y1 = dp->arr[i+1];
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 *= (double)(dp->dim[0].ne - 1);
318 >                i = x;
319 >                if (i < 0)
320 >                        i = 0;
321 >                else if (i > dp->dim[0].ne - 2)
322 >                        i = dp->dim[0].ne - 2;
323 >        } else {                                /* unevenly spaced points */
324 >                if (dp->dim[0].siz > 0.0) {
325 >                        lower = 0;
326 >                        upper = dp->dim[0].ne;
327 >                } else {
328 >                        lower = dp->dim[0].ne;
329 >                        upper = 0;
330 >                }
331 >                do {
332 >                        i = (lower + upper) >> 1;
333 >                        if (pt[0] >= dp->dim[0].p[i])
334 >                                lower = i;
335 >                        else
336 >                                upper = i;
337 >                } while (i != (lower + upper) >> 1);
338 >                if (i > dp->dim[0].ne - 2)
339 >                        i = dp->dim[0].ne - 2;
340 >                x = i + (pt[0] - dp->dim[0].p[i]) /
341 >                                (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
342 >        }
343 >                                        /* get dependent variable */
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          }
260
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);
267 <        else if (x < i-1)
268 <                y = (2*y0-y1)/(i-x);
269 <        else
270 <                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