ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/data.c
Revision: 2.15
Committed: Sat Feb 22 02:07:28 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +80 -27 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * data.c - routines dealing with interpolated data.
6 */
7
8 /* ====================================================================
9 * The Radiance Software License, Version 1.0
10 *
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"
66
67 #include "color.h"
68
69 #include "resolu.h"
70
71 #include "data.h"
72
73 /* picture memory usage before warning */
74 #ifndef PSIZWARN
75 #ifdef BIGMEM
76 #define PSIZWARN 5000000
77 #else
78 #define PSIZWARN 1500000
79 #endif
80 #endif
81
82 #ifndef TABSIZ
83 #define TABSIZ 97 /* table size (prime) */
84 #endif
85
86 #define hash(s) (shash(s)%TABSIZ)
87
88
89 static DATARRAY *dtab[TABSIZ]; /* data array list */
90
91
92 DATARRAY *
93 getdata(dname) /* get data array dname */
94 char *dname;
95 {
96 char *dfname;
97 FILE *fp;
98 int asize;
99 register int i, j;
100 register DATARRAY *dp;
101 /* look for array in list */
102 for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
103 if (!strcmp(dname, dp->name))
104 return(dp); /* found! */
105 /*
106 * If we haven't loaded the data already, we will look
107 * for it in the directories specified by the library path.
108 *
109 * The file has the following format:
110 *
111 * N
112 * beg0 end0 n0
113 * beg1 end1 n1
114 * . . .
115 * begN endN nN
116 * data, later dimensions changing faster
117 * . . .
118 *
119 * For irregularly spaced points, the following can be
120 * substituted for begi endi ni:
121 *
122 * 0 0 ni p0i p1i .. pni
123 */
124
125 if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) {
126 sprintf(errmsg, "cannot find data file \"%s\"", dname);
127 error(USER, errmsg);
128 }
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 (fgetval(fp, 'i', (char *)&asize) <= 0)
135 goto scanerr;
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 (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0)
148 goto scanerr;
149 if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0)
150 goto scanerr;
151 if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
152 goto scanerr;
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 = (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 (fgetval(fp, DATATY,
163 (char *)&dp->dim[i].p[j]) <= 0)
164 goto scanerr;
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]))
168 goto scanerr;
169 dp->dim[i].org = dp->dim[i].p[0];
170 dp->dim[i].siz = dp->dim[i].p[dp->dim[i].ne-1]
171 - dp->dim[i].p[0];
172 } else
173 dp->dim[i].p = NULL;
174 }
175 if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
176 goto memerr;
177
178 for (i = 0; i < asize; i++)
179 if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0)
180 goto scanerr;
181 fclose(fp);
182 i = hash(dname);
183 dp->next = dtab[i];
184 return(dtab[i] = dp);
185
186 memerr:
187 error(SYSTEM, "out of memory in getdata");
188 scanerr:
189 sprintf(errmsg, "%s in data file \"%s\"",
190 feof(fp) ? "unexpected EOF" : "bad format", dfname);
191 error(USER, errmsg);
192 }
193
194
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
210 DATARRAY *
211 getpict(pname) /* get picture pname */
212 char *pname;
213 {
214 double inpaspect;
215 char *pfname;
216 FILE *fp;
217 COLR *scanin;
218 int sl, ns;
219 RESOLU inpres;
220 FLOAT loc[2];
221 int y;
222 register int x, i;
223 register DATARRAY *pp;
224 /* look for array in list */
225 for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next)
226 if (!strcmp(pname, pp->name))
227 return(pp); /* found! */
228
229 if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) {
230 sprintf(errmsg, "cannot find picture file \"%s\"", pname);
231 error(USER, errmsg);
232 }
233 if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
234 goto memerr;
235
236 pp[0].name = savestr(pname);
237
238 if ((fp = fopen(pfname, "r")) == NULL) {
239 sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
240 error(SYSTEM, errmsg);
241 }
242 #ifdef MSDOS
243 setmode(fileno(fp), O_BINARY);
244 #endif
245 /* get dimensions */
246 inpaspect = 1.0;
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 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 if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
276 goto memerr;
277 /* load picture */
278 if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
279 goto memerr;
280 for (y = 0; y < ns; y++) {
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 copycolr(pp[0].arr.c[i], scanin[x]);
288 }
289 }
290 free((void *)scanin);
291 fclose(fp);
292 i = hash(pname);
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");
303 readerr:
304 sprintf(errmsg, "bad picture file \"%s\"", pfname);
305 error(USER, errmsg);
306 }
307
308
309 void
310 freedata(dta) /* release data array reference */
311 DATARRAY *dta;
312 {
313 DATARRAY head;
314 int hval, nents;
315 register DATARRAY *dpl, *dp;
316 register int i;
317
318 if (dta == NULL) { /* free all if NULL */
319 hval = 0; nents = TABSIZ;
320 } else {
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 ((dta == NULL | dta == dp)) {
328 dpl->next = dp->next;
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((void *)dp->dim[i].p);
336 freestr(dp->name);
337 free((void *)dp);
338 } else
339 dpl = dp;
340 dtab[hval++] = head.next;
341 }
342 }
343
344
345 double
346 datavalue(dp, pt) /* interpolate data value at a point */
347 register DATARRAY *dp;
348 double *pt;
349 {
350 DATARRAY sd;
351 int asize;
352 int lower, upper;
353 register int i;
354 double x, y0, y1;
355 /* set up dimensions for recursion */
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 *= (double)(dp->dim[0].ne - 1);
372 i = x;
373 if (i < 0)
374 i = 0;
375 else if (i > dp->dim[0].ne - 2)
376 i = dp->dim[0].ne - 2;
377 } else { /* unevenly spaced points */
378 if (dp->dim[0].siz > 0.0) {
379 lower = 0;
380 upper = dp->dim[0].ne;
381 } else {
382 lower = dp->dim[0].ne;
383 upper = 0;
384 }
385 do {
386 i = (lower + upper) >> 1;
387 if (pt[0] >= dp->dim[0].p[i])
388 lower = i;
389 else
390 upper = i;
391 } while (i != (lower + upper) >> 1);
392 if (i > dp->dim[0].ne - 2)
393 i = dp->dim[0].ne - 2;
394 x = i + (pt[0] - dp->dim[0].p[i]) /
395 (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
396 }
397 /* get dependent variable */
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 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 return( (2*y1-y0)/(x-(i-1)) );
425
426 if (x < i-1)
427 return( (2*y0-y1)/(i-x) );
428
429 return( y0*((i+1)-x) + y1*(x-i) );
430 }