ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
Revision: 1.6
Committed: Fri Apr 6 14:12:21 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +25 -21 lines
Log Message:
Changed format for uneven point data and used binary search

File Contents

# Content
1 /* Copyright (c) 1986 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 = width;
178 pp[i].dim[1].ne = height;
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 = 1.0;
183 pp[i].dim[1].siz = inpaspect*(double)height/width;
184 } else {
185 pp[i].dim[0].siz = (double)width/height/inpaspect;
186 pp[i].dim[1].siz = 1.0;
187 }
188 pp[i].arr = (DATATYPE *)malloc(width*height*sizeof(DATATYPE));
189 if (pp[i].arr == NULL)
190 goto memerr;
191 }
192 /* load picture */
193 if ((scanin = (COLOR *)malloc(width*sizeof(COLOR))) == NULL)
194 goto memerr;
195 for (y = height-1; y >= 0; y--) {
196 if (freadscan(scanin, width, fp) < 0)
197 goto readerr;
198 for (x = 0; x < width; x++)
199 for (i = 0; i < 3; i++)
200 pp[i].arr[x*height+y] = colval(scanin[x],i);
201 }
202 free((char *)scanin);
203 fclose(fp);
204 pp[0].next =
205 pp[1].next =
206 pp[2].next = plist;
207 return(plist = pp);
208
209 memerr:
210 error(SYSTEM, "out of memory in getpict");
211 readerr:
212 sprintf(errmsg, "bad picture file \"%s\"", pfname);
213 error(USER, errmsg);
214 }
215
216
217 freedata(dname) /* free memory associated with dname */
218 char *dname;
219 {
220 register DATARRAY *dp, *dpl;
221 register int i;
222
223 for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
224 if (!strcmp(dname, dp->name)) {
225 if (dpl == NULL)
226 dlist = dp->next;
227 else
228 dpl->next = dp->next;
229 free((char *)dp->arr);
230 for (i = 0; i < dp->nd; i++)
231 if (dp->dim[i].p != NULL)
232 free((char *)dp->dim[i].p);
233 freestr(dp->name);
234 free((char *)dp);
235 return;
236 }
237 }
238
239
240 freepict(pname) /* free memory associated with pname */
241 char *pname;
242 {
243 register DATARRAY *pp, *ppl;
244
245 for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
246 if (!strcmp(pname, pp->name)) {
247 if (ppl == NULL)
248 plist = pp->next;
249 else
250 ppl->next = pp->next;
251 free((char *)pp[0].arr);
252 free((char *)pp[1].arr);
253 free((char *)pp[2].arr);
254 freestr(pp[0].name);
255 free((char *)pp);
256 return;
257 }
258 }
259
260
261 double
262 datavalue(dp, pt) /* interpolate data value at a point */
263 register DATARRAY *dp;
264 double *pt;
265 {
266 DATARRAY sd;
267 int asize;
268 int lower, upper;
269 register int i;
270 double x, y, y0, y1;
271 /* set up dimensions for recursion */
272 sd.nd = dp->nd - 1;
273 asize = 1;
274 for (i = 0; i < sd.nd; i++) {
275 sd.dim[i].org = dp->dim[i+1].org;
276 sd.dim[i].siz = dp->dim[i+1].siz;
277 sd.dim[i].p = dp->dim[i+1].p;
278 asize *= sd.dim[i].ne = dp->dim[i+1].ne;
279 }
280 /* get independent variable */
281 if (dp->dim[0].p == NULL) { /* evenly spaced points */
282 x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
283 x = x * (dp->dim[0].ne - 1);
284 i = x;
285 if (i < 0)
286 i = 0;
287 else if (i > dp->dim[0].ne - 2)
288 i = dp->dim[0].ne - 2;
289 } else { /* unevenly spaced points */
290 if (dp->dim[0].siz > 0.0) {
291 lower = 0;
292 upper = dp->dim[0].ne;
293 } else {
294 lower = dp->dim[0].ne;
295 upper = 0;
296 }
297 do {
298 i = (lower + upper) >> 1;
299 if (pt[0] >= dp->dim[0].p[i])
300 lower = i;
301 else if (pt[0] < dp->dim[0].p[i])
302 upper = i;
303 } while (i != (lower + upper) >> 1);
304 if (i < 0)
305 i = 0;
306 else if (i > dp->dim[0].ne - 2)
307 i = dp->dim[0].ne - 2;
308 x = i + (pt[0] - dp->dim[0].p[i]) /
309 (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
310 }
311 /* get dependent variable */
312 if (dp->nd == 1) {
313 y0 = dp->arr[i];
314 y1 = dp->arr[i+1];
315 } else {
316 sd.arr = &dp->arr[i*asize];
317 y0 = datavalue(&sd, pt+1);
318 sd.arr = &dp->arr[(i+1)*asize];
319 y1 = datavalue(&sd, pt+1);
320 }
321 /*
322 * Extrapolate as far as one division, then
323 * taper off harmonically to zero.
324 */
325 if (x > i+2)
326 y = (2*y1-y0)/(x-i-1);
327 else if (x < i-1)
328 y = (2*y0-y1)/(i-x);
329 else
330 y = y0*((i+1)-x) + y1*(x-i);
331
332 return(y);
333 }