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.2 by greg, Thu Dec 19 14:54:58 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "color.h"
16  
17 + #include  "resolu.h"
18 +
19   #include  "data.h"
20  
21  
22 + extern char  *fgetword();
23 +
24   extern char  *libpath;                  /* library search path */
25  
26   static DATARRAY  *dlist = NULL;         /* data array list */
# Line 28 | Line 32 | DATARRAY *
32   getdata(dname)                          /* get data array dname */
33   char  *dname;
34   {
35 +        char  word[64];
36          char  *dfname;
37          FILE  *fp;
38          int  asize;
39 <        register int  i;
39 >        register int  i, j;
40          register DATARRAY  *dp;
41                                                  /* look for array in list */
42          for (dp = dlist; dp != NULL; dp = dp->next)
# Line 44 | Line 49 | char  *dname;
49           *
50           *      The file has the following format:
51           *
52 <         *              #dimensions
52 >         *              N
53           *              beg0    end0    n0
54           *              beg1    end1    n1
55           *              . . .
56 +         *              begN    endN    nN
57           *              data, later dimensions changing faster
58           *              . . .
59           *
60 +         *      For irregularly spaced points, the following can be
61 +         *  substituted for begi endi ni:
62 +         *
63 +         *              0 0 ni p0i p1i .. pni
64           */
65  
66 <        if ((dfname = getpath(dname, libpath)) == NULL) {
66 >        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
67                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
68                  error(USER, errmsg);
69          }
# Line 67 | Line 77 | char  *dname;
77                  error(SYSTEM, errmsg);
78          }
79                                                          /* get dimensions */
80 <        if (fscanf(fp, "%d", &dp->nd) != 1)
80 >        if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
81                  goto scanerr;
82 <        if (dp->nd <= 0 || dp->nd > MAXDIM) {
82 >        dp->nd = atoi(word);
83 >        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
84                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
85                  error(USER, errmsg);
86          }
87          asize = 1;
88          for (i = 0; i < dp->nd; i++) {
89 <                if (fscanf(fp, "%lf %lf %d",
79 <                                &dp->dim[i].org, &dp->dim[i].siz,
80 <                                &dp->dim[i].ne) != 3)
89 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
90                          goto scanerr;
91 <                dp->dim[i].siz -= dp->dim[i].org;
91 >                dp->dim[i].org = atof(word);
92 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
93 >                        goto scanerr;
94 >                dp->dim[i].siz = atof(word);
95 >                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
96 >                        goto scanerr;
97 >                dp->dim[i].ne = atoi(word);
98 >                if (dp->dim[i].ne < 2)
99 >                        goto scanerr;
100                  asize *= dp->dim[i].ne;
101 +                if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
102 +                        dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
103 +                        if (dp->dim[i].p == NULL)
104 +                                goto memerr;
105 +                        for (j = 0; j < dp->dim[i].ne; j++) {
106 +                                if (fgetword(word, sizeof(word), fp) == NULL ||
107 +                                                !isflt(word))
108 +                                        goto scanerr;
109 +                                dp->dim[i].p[j] = atof(word);
110 +                        }
111 +                        for (j = 1; j < dp->dim[i].ne-1; j++)
112 +                                if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
113 +                                        (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
114 +                                        goto scanerr;
115 +                        dp->dim[i].org = dp->dim[i].p[0];
116 +                        dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
117 +                                                - dp->dim[i].p[0];
118 +                } else
119 +                        dp->dim[i].p = NULL;
120          }
121          if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
122                  goto memerr;
123          
124 <        for (i = 0; i < asize; i++)
125 <                if (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
124 >        for (i = 0; i < asize; i++) {
125 >                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
126                          goto scanerr;
127 <        
127 >                dp->arr[i] = atof(word);
128 >        }
129          fclose(fp);
130          dp->next = dlist;
131          return(dlist = dp);
# Line 102 | Line 139 | scanerr:
139   }
140  
141  
142 + static double  inpaspect;               /* aspect ratio of input picture */
143 +
144 + static
145 + headaspect(s)                           /* check string for aspect ratio */
146 + char  *s;
147 + {
148 +        if (isaspect(s))
149 +                inpaspect *= aspectval(s);
150 + }
151 +
152 +
153   DATARRAY *
154   getpict(pname)                          /* get picture pname */
155   char  *pname;
# Line 110 | Line 158 | char  *pname;
158          char  *pfname;
159          FILE  *fp;
160          COLOR  *scanin;
161 <        int  width, height;
162 <        int  x, y;
163 <        register int  i;
161 >        int  sl, ns;
162 >        RESOLU  inpres;
163 >        FLOAT  loc[2];
164 >        int  y;
165 >        register int  x, i;
166          register DATARRAY  *pp;
167                                                  /* look for array in list */
168          for (pp = plist; pp != NULL; pp = pp->next)
169                  if (!strcmp(pname, pp->name))
170                          return(pp);             /* found! */
171  
172 <        if ((pfname = getpath(pname, libpath)) == NULL) {
172 >        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
173                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
174                  error(USER, errmsg);
175          }
# Line 135 | Line 185 | char  *pname;
185                  error(SYSTEM, errmsg);
186          }
187                                                  /* get dimensions */
188 <        getheader(fp, NULL);
189 <        if (fscanf(fp, "-Y %d +X %d\n", &height, &width) != 2)
188 >        inpaspect = 1.0;
189 >        getheader(fp, headaspect);
190 >        if (!fgetsresolu(&inpres, fp))
191                  goto readerr;
192          for (i = 0; i < 3; i++) {
193                  pp[i].nd = 2;
194 <                pp[i].dim[0].ne = width;
195 <                pp[i].dim[1].ne = height;
194 >                pp[i].dim[0].ne = inpres.yr;
195 >                pp[i].dim[1].ne = inpres.xr;
196                  pp[i].dim[0].org =
197                  pp[i].dim[1].org = 0.0;
198 <                if (width <= height) {
199 <                        pp[i].dim[0].siz = 1.0;
200 <                        pp[i].dim[1].siz = (double)height/width;
150 <                } else {
151 <                        pp[i].dim[0].siz = (double)width/height;
198 >                if (inpres.xr <= inpres.yr*inpaspect) {
199 >                        pp[i].dim[0].siz = inpaspect *
200 >                                                (double)inpres.yr/inpres.xr;
201                          pp[i].dim[1].siz = 1.0;
202 +                } else {
203 +                        pp[i].dim[0].siz = 1.0;
204 +                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
205 +                                                inpaspect;
206                  }
207 <                pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
207 >                pp[i].dim[0].p = pp[i].dim[1].p = NULL;
208 >                pp[i].arr = (DATATYPE *)
209 >                                malloc(inpres.xr*inpres.yr*sizeof(DATATYPE));
210                  if (pp[i].arr == NULL)
211                          goto memerr;
212          }
213                                                          /* load picture */
214 <        if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
214 >        sl = scanlen(&inpres);
215 >        ns = numscans(&inpres);
216 >        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
217                  goto memerr;
218 <        for (y = height-1; y >= 0; y--) {
219 <                if (freadscan(scanin, width, fp) < 0)
218 >        for (y = 0; y < ns; y++) {
219 >                if (freadscan(scanin, sl, fp) < 0)
220                          goto readerr;
221 <                for (x = 0; x < width; x++)
222 <                        for (i = 0; i < 3; i++)
223 <                                pp[i].arr[x*height+y] = colval(scanin[x],i);
221 >                for (x = 0; x < sl; x++) {
222 >                        pix2loc(loc, &inpres, x, y);
223 >                        i = (int)(loc[1]*inpres.yr)*inpres.xr +
224 >                                        (int)(loc[0]*inpres.xr);
225 >                        pp[0].arr[i] = colval(scanin[x],RED);
226 >                        pp[1].arr[i] = colval(scanin[x],GRN);
227 >                        pp[2].arr[i] = colval(scanin[x],BLU);
228 >                }
229          }
230          free((char *)scanin);
231          fclose(fp);
# Line 184 | Line 246 | freedata(dname)                        /* free memory associated with dname
246   char  *dname;
247   {
248          register DATARRAY  *dp, *dpl;
249 +        register int  i;
250  
251          for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
252                  if (!strcmp(dname, dp->name)) {
# Line 192 | Line 255 | char  *dname;
255                          else
256                                  dpl->next = dp->next;
257                          free((char *)dp->arr);
258 +                        for (i = 0; i < dp->nd; i++)
259 +                                if (dp->dim[i].p != NULL)
260 +                                        free((char *)dp->dim[i].p);
261                          freestr(dp->name);
262                          free((char *)dp);
263                          return;
# Line 227 | Line 293 | double  *pt;
293   {
294          DATARRAY  sd;
295          int  asize;
296 +        int  lower, upper;
297          register int  i;
298          double  x, y, y0, y1;
299 <
299 >                                        /* set up dimensions for recursion */
300          sd.nd = dp->nd - 1;
301          asize = 1;
302          for (i = 0; i < sd.nd; i++) {
303                  sd.dim[i].org = dp->dim[i+1].org;
304                  sd.dim[i].siz = dp->dim[i+1].siz;
305 +                sd.dim[i].p = dp->dim[i+1].p;
306                  asize *= sd.dim[i].ne = dp->dim[i+1].ne;
307          }
308 <
309 <        x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
310 <
311 <        x *= dp->dim[0].ne - 1;
312 <
313 <        i = x;
314 <        if (i < 0)
315 <                i = 0;
316 <        else if (i > dp->dim[0].ne - 2)
317 <                i = dp->dim[0].ne - 2;
318 <
308 >                                        /* get independent variable */
309 >        if (dp->dim[0].p == NULL) {             /* evenly spaced points */
310 >                x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
311 >                x = x * (dp->dim[0].ne - 1);
312 >                i = x;
313 >                if (i < 0)
314 >                        i = 0;
315 >                else if (i > dp->dim[0].ne - 2)
316 >                        i = dp->dim[0].ne - 2;
317 >        } else {                                /* unevenly spaced points */
318 >                if (dp->dim[0].siz > 0.0) {
319 >                        lower = 0;
320 >                        upper = dp->dim[0].ne;
321 >                } else {
322 >                        lower = dp->dim[0].ne;
323 >                        upper = 0;
324 >                }
325 >                do {
326 >                        i = (lower + upper) >> 1;
327 >                        if (pt[0] >= dp->dim[0].p[i])
328 >                                lower = i;
329 >                        else
330 >                                upper = i;
331 >                } while (i != (lower + upper) >> 1);
332 >                if (i > dp->dim[0].ne - 2)
333 >                        i = dp->dim[0].ne - 2;
334 >                x = i + (pt[0] - dp->dim[0].p[i]) /
335 >                                (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
336 >        }
337 >                                        /* get dependent variable */
338          if (dp->nd == 1) {
339                  y0 = dp->arr[i];
340                  y1 = dp->arr[i+1];
# Line 257 | Line 344 | double  *pt;
344                  sd.arr = &dp->arr[(i+1)*asize];
345                  y1 = datavalue(&sd, pt+1);
346          }
260
347          /*
348           * Extrapolate as far as one division, then
349           * taper off harmonically to zero.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines