ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
Revision: 1.8
Committed: Tue Jan 15 22:24:19 1991 UTC (33 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +3 -5 lines
Log Message:
eliminated unnecessary tests for unevenly spaced data points

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * data.c - routines dealing with interpolated data.
9 *
10 * 6/4/86
11 */
12
13 #include "standard.h"
14
15 #include "color.h"
16
17 #include "data.h"
18
19
20 extern char *libpath; /* library search path */
21
22 static DATARRAY *dlist = NULL; /* data array list */
23
24 static DATARRAY *plist = NULL; /* picture list */
25
26
27 DATARRAY *
28 getdata(dname) /* get data array dname */
29 char *dname;
30 {
31 char *dfname;
32 FILE *fp;
33 int asize;
34 register int i, j;
35 register DATARRAY *dp;
36 /* look for array in list */
37 for (dp = dlist; dp != NULL; dp = dp->next)
38 if (!strcmp(dname, dp->name))
39 return(dp); /* found! */
40
41 /*
42 * If we haven't loaded the data already, we will look
43 * for it in the directorys specified by the library path.
44 *
45 * The file has the following format:
46 *
47 * N
48 * beg0 end0 n0
49 * beg1 end1 n1
50 * . . .
51 * begN endN nN
52 * data, later dimensions changing faster
53 * . . .
54 *
55 * For irregularly spaced points, the following can be
56 * substituted for begi endi ni:
57 *
58 * 0 0 ni p0i p1i .. pni
59 */
60
61 if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
62 sprintf(errmsg, "cannot find data file \"%s\"", dname);
63 error(USER, errmsg);
64 }
65 if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
66 goto memerr;
67
68 dp->name = savestr(dname);
69
70 if ((fp = fopen(dfname, "r")) == NULL) {
71 sprintf(errmsg, "cannot open data file \"%s\"", dfname);
72 error(SYSTEM, errmsg);
73 }
74 /* get dimensions */
75 if (fscanf(fp, "%d", &dp->nd) != 1)
76 goto scanerr;
77 if (dp->nd <= 0 || dp->nd > MAXDIM) {
78 sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
79 error(USER, errmsg);
80 }
81 asize = 1;
82 for (i = 0; i < dp->nd; i++) {
83 if (fscanf(fp, "%lf %lf %d",
84 &dp->dim[i].org, &dp->dim[i].siz,
85 &dp->dim[i].ne) != 3)
86 goto scanerr;
87 if (dp->dim[i].ne < 2)
88 goto scanerr;
89 asize *= dp->dim[i].ne;
90 if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
91 dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
92 if (dp->dim[i].p == NULL)
93 goto memerr;
94 for (j = 0; j < dp->dim[i].ne; j++)
95 if (fscanf(fp, "%lf", &dp->dim[i].p[j]) != 1)
96 goto scanerr;
97 for (j = 1; j < dp->dim[i].ne-1; j++)
98 if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
99 (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
100 goto scanerr;
101 dp->dim[i].org = dp->dim[i].p[0];
102 dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
103 - dp->dim[i].p[0];
104 } else
105 dp->dim[i].p = NULL;
106 }
107 if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
108 goto memerr;
109
110 for (i = 0; i < asize; i++)
111 if (fscanf(fp, DSCANF, &dp->arr[i]) != 1)
112 goto scanerr;
113
114 fclose(fp);
115 dp->next = dlist;
116 return(dlist = dp);
117
118 memerr:
119 error(SYSTEM, "out of memory in getdata");
120 scanerr:
121 sprintf(errmsg, "%s in data file \"%s\"",
122 feof(fp) ? "unexpected EOF" : "bad format", dfname);
123 error(USER, errmsg);
124 }
125
126
127 static double inpaspect; /* aspect ratio of input picture */
128
129 static
130 headaspect(s) /* check string for aspect ratio */
131 char *s;
132 {
133 if (isaspect(s))
134 inpaspect *= aspectval(s);
135 }
136
137
138 DATARRAY *
139 getpict(pname) /* get picture pname */
140 char *pname;
141 {
142 extern char *libpath;
143 char *pfname;
144 FILE *fp;
145 COLOR *scanin;
146 int width, height;
147 int x, y;
148 register int i;
149 register DATARRAY *pp;
150 /* look for array in list */
151 for (pp = plist; pp != NULL; pp = pp->next)
152 if (!strcmp(pname, pp->name))
153 return(pp); /* found! */
154
155 if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
156 sprintf(errmsg, "cannot find picture file \"%s\"", pname);
157 error(USER, errmsg);
158 }
159 if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL)
160 goto memerr;
161
162 pp[0].name =
163 pp[1].name =
164 pp[2].name = savestr(pname);
165
166 if ((fp = fopen(pfname, "r")) == NULL) {
167 sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
168 error(SYSTEM, errmsg);
169 }
170 /* get dimensions */
171 inpaspect = 1.0;
172 getheader(fp, headaspect);
173 if (fgetresolu(&width, &height, fp) != (YMAJOR|YDECR))
174 goto readerr;
175 for (i = 0; i < 3; i++) {
176 pp[i].nd = 2;
177 pp[i].dim[0].ne = height;
178 pp[i].dim[1].ne = width;
179 pp[i].dim[0].org =
180 pp[i].dim[1].org = 0.0;
181 if (width <= height*inpaspect) {
182 pp[i].dim[0].siz = inpaspect*(double)height/width;
183 pp[i].dim[1].siz = 1.0;
184 } else {
185 pp[i].dim[0].siz = 1.0;
186 pp[i].dim[1].siz = (double)width/height/inpaspect;
187 }
188 pp[i].dim[0].p = pp[i].dim[1].p = NULL;
189 pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
190 if (pp[i].arr == NULL)
191 goto memerr;
192 }
193 /* load picture */
194 if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
195 goto memerr;
196 for (y = height-1; y >= 0; y--) {
197 if (freadscan(scanin, width, fp) < 0)
198 goto readerr;
199 for (x = 0; x < width; x++)
200 for (i = 0; i < 3; i++)
201 pp[i].arr[y*width+x] = colval(scanin[x],i);
202 }
203 free((char *)scanin);
204 fclose(fp);
205 pp[0].next =
206 pp[1].next =
207 pp[2].next = plist;
208 return(plist = pp);
209
210 memerr:
211 error(SYSTEM, "out of memory in getpict");
212 readerr:
213 sprintf(errmsg, "bad picture file \"%s\"", pfname);
214 error(USER, errmsg);
215 }
216
217
218 freedata(dname) /* free memory associated with dname */
219 char *dname;
220 {
221 register DATARRAY *dp, *dpl;
222 register int i;
223
224 for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
225 if (!strcmp(dname, dp->name)) {
226 if (dpl == NULL)
227 dlist = dp->next;
228 else
229 dpl->next = dp->next;
230 free((char *)dp->arr);
231 for (i = 0; i < dp->nd; i++)
232 if (dp->dim[i].p != NULL)
233 free((char *)dp->dim[i].p);
234 freestr(dp->name);
235 free((char *)dp);
236 return;
237 }
238 }
239
240
241 freepict(pname) /* free memory associated with pname */
242 char *pname;
243 {
244 register DATARRAY *pp, *ppl;
245
246 for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
247 if (!strcmp(pname, pp->name)) {
248 if (ppl == NULL)
249 plist = pp->next;
250 else
251 ppl->next = pp->next;
252 free((char *)pp[0].arr);
253 free((char *)pp[1].arr);
254 free((char *)pp[2].arr);
255 freestr(pp[0].name);
256 free((char *)pp);
257 return;
258 }
259 }
260
261
262 double
263 datavalue(dp, pt) /* interpolate data value at a point */
264 register DATARRAY *dp;
265 double *pt;
266 {
267 DATARRAY sd;
268 int asize;
269 int lower, upper;
270 register int i;
271 double x, y, y0, y1;
272 /* set up dimensions for recursion */
273 sd.nd = dp->nd - 1;
274 asize = 1;
275 for (i = 0; i < sd.nd; i++) {
276 sd.dim[i].org = dp->dim[i+1].org;
277 sd.dim[i].siz = dp->dim[i+1].siz;
278 sd.dim[i].p = dp->dim[i+1].p;
279 asize *= sd.dim[i].ne = dp->dim[i+1].ne;
280 }
281 /* get independent variable */
282 if (dp->dim[0].p == NULL) { /* evenly spaced points */
283 x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
284 x = x * (dp->dim[0].ne - 1);
285 i = x;
286 if (i < 0)
287 i = 0;
288 else if (i > dp->dim[0].ne - 2)
289 i = dp->dim[0].ne - 2;
290 } else { /* unevenly spaced points */
291 if (dp->dim[0].siz > 0.0) {
292 lower = 0;
293 upper = dp->dim[0].ne;
294 } else {
295 lower = dp->dim[0].ne;
296 upper = 0;
297 }
298 do {
299 i = (lower + upper) >> 1;
300 if (pt[0] >= dp->dim[0].p[i])
301 lower = i;
302 else
303 upper = i;
304 } while (i != (lower + upper) >> 1);
305 if (i > dp->dim[0].ne - 2)
306 i = dp->dim[0].ne - 2;
307 x = i + (pt[0] - dp->dim[0].p[i]) /
308 (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
309 }
310 /* get dependent variable */
311 if (dp->nd == 1) {
312 y0 = dp->arr[i];
313 y1 = dp->arr[i+1];
314 } else {
315 sd.arr = &dp->arr[i*asize];
316 y0 = datavalue(&sd, pt+1);
317 sd.arr = &dp->arr[(i+1)*asize];
318 y1 = datavalue(&sd, pt+1);
319 }
320 /*
321 * Extrapolate as far as one division, then
322 * taper off harmonically to zero.
323 */
324 if (x > i+2)
325 y = (2*y1-y0)/(x-i-1);
326 else if (x < i-1)
327 y = (2*y0-y1)/(i-x);
328 else
329 y = y0*((i+1)-x) + y1*(x-i);
330
331 return(y);
332 }