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

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.15 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4     /*
5     * data.c - routines dealing with interpolated data.
6     */
7    
8 greg 2.15 /* ====================================================================
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 greg 1.1 #include "standard.h"
66    
67     #include "color.h"
68    
69 greg 1.11 #include "resolu.h"
70    
71 greg 1.1 #include "data.h"
72    
73 greg 2.7 /* picture memory usage before warning */
74     #ifndef PSIZWARN
75     #ifdef BIGMEM
76 gregl 2.13 #define PSIZWARN 5000000
77 greg 2.7 #else
78 gregl 2.13 #define PSIZWARN 1500000
79 greg 2.7 #endif
80     #endif
81 greg 1.1
82 greg 2.7 #ifndef TABSIZ
83 greg 2.6 #define TABSIZ 97 /* table size (prime) */
84 greg 2.7 #endif
85 greg 2.6
86     #define hash(s) (shash(s)%TABSIZ)
87    
88    
89     static DATARRAY *dtab[TABSIZ]; /* data array list */
90 greg 1.1
91    
92     DATARRAY *
93     getdata(dname) /* get data array dname */
94     char *dname;
95     {
96     char *dfname;
97     FILE *fp;
98     int asize;
99 greg 1.4 register int i, j;
100 greg 1.1 register DATARRAY *dp;
101     /* look for array in list */
102 greg 2.6 for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
103 greg 1.1 if (!strcmp(dname, dp->name))
104     return(dp); /* found! */
105     /*
106     * If we haven't loaded the data already, we will look
107 greg 2.5 * for it in the directories specified by the library path.
108 greg 1.1 *
109     * The file has the following format:
110     *
111 greg 1.4 * N
112 greg 1.1 * beg0 end0 n0
113     * beg1 end1 n1
114     * . . .
115 greg 1.4 * begN endN nN
116 greg 1.1 * data, later dimensions changing faster
117     * . . .
118     *
119 greg 1.4 * For irregularly spaced points, the following can be
120     * substituted for begi endi ni:
121     *
122 greg 1.6 * 0 0 ni p0i p1i .. pni
123 greg 1.1 */
124    
125 greg 2.8 if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) {
126 greg 1.1 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 greg 2.15 if (fgetval(fp, 'i', (char *)&asize) <= 0)
135 greg 1.1 goto scanerr;
136 greg 2.12 if (asize <= 0 | asize > MAXDDIM) {
137 greg 1.1 sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
138     error(USER, errmsg);
139     }
140 greg 2.12 if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
141     goto memerr;
142     dp->name = savestr(dname);
143     dp->type = DATATY;
144     dp->nd = asize;
145 greg 1.1 asize = 1;
146     for (i = 0; i < dp->nd; i++) {
147 greg 2.15 if (fgetval(fp, DATATY, (char *)&dp->dim[i].org) <= 0)
148 greg 1.6 goto scanerr;
149 greg 2.15 if (fgetval(fp, DATATY, (char *)&dp->dim[i].siz) <= 0)
150 greg 1.10 goto scanerr;
151 greg 2.15 if (fgetval(fp, 'i', (char *)&dp->dim[i].ne) <= 0)
152 greg 1.10 goto scanerr;
153 greg 1.6 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 greg 2.12 dp->dim[i].p = (DATATYPE *)
158     malloc(dp->dim[i].ne*sizeof(DATATYPE));
159 greg 1.4 if (dp->dim[i].p == NULL)
160     goto memerr;
161 greg 2.10 for (j = 0; j < dp->dim[i].ne; j++)
162 greg 2.15 if (fgetval(fp, DATATY,
163     (char *)&dp->dim[i].p[j]) <= 0)
164 greg 1.4 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 greg 1.6 dp->dim[i].p = NULL;
174 greg 1.1 }
175 greg 2.12 if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
176 greg 1.1 goto memerr;
177    
178 greg 2.10 for (i = 0; i < asize; i++)
179 greg 2.15 if (fgetval(fp, DATATY, (char *)&dp->arr.d[i]) <= 0)
180 greg 1.1 goto scanerr;
181     fclose(fp);
182 greg 2.6 i = hash(dname);
183     dp->next = dtab[i];
184     return(dtab[i] = dp);
185 greg 1.1
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 greg 2.15 static int
196 greg 2.4 headaspect(s, iap) /* check string for aspect ratio */
197 greg 1.5 char *s;
198 greg 2.4 double *iap;
199 greg 1.5 {
200 gwlarson 2.14 char fmt[32];
201    
202 greg 1.5 if (isaspect(s))
203 greg 2.4 *iap *= aspectval(s);
204 greg 2.15 else if (formatval(fmt, s) && !globmatch(PICFMT, fmt))
205 gwlarson 2.14 *iap = 0.0;
206     return(0);
207 greg 1.5 }
208    
209    
210 greg 1.1 DATARRAY *
211     getpict(pname) /* get picture pname */
212     char *pname;
213     {
214 greg 2.4 double inpaspect;
215 greg 1.1 char *pfname;
216     FILE *fp;
217 greg 2.12 COLR *scanin;
218 greg 1.11 int sl, ns;
219 greg 2.3 RESOLU inpres;
220 greg 1.11 FLOAT loc[2];
221     int y;
222     register int x, i;
223 greg 1.1 register DATARRAY *pp;
224     /* look for array in list */
225 greg 2.12 for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next)
226 greg 1.1 if (!strcmp(pname, pp->name))
227     return(pp); /* found! */
228    
229 greg 2.8 if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) {
230 greg 1.1 sprintf(errmsg, "cannot find picture file \"%s\"", pname);
231     error(USER, errmsg);
232     }
233 greg 2.12 if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
234 greg 1.1 goto memerr;
235    
236 greg 2.12 pp[0].name = savestr(pname);
237 greg 1.1
238     if ((fp = fopen(pfname, "r")) == NULL) {
239     sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
240     error(SYSTEM, errmsg);
241     }
242 greg 2.3 #ifdef MSDOS
243     setmode(fileno(fp), O_BINARY);
244     #endif
245 greg 1.1 /* get dimensions */
246 greg 1.5 inpaspect = 1.0;
247 greg 2.15 getheader(fp, headaspect, (char *)&inpaspect);
248 gwlarson 2.14 if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
249 greg 1.1 goto readerr;
250 greg 2.12 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 greg 2.7 #if PSIZWARN
269 greg 2.12 if (i > PSIZWARN) { /* memory warning */
270 greg 2.7 sprintf(errmsg, "picture file \"%s\" using %d bytes of memory",
271     pname, i);
272     error(WARNING, errmsg);
273     }
274     #endif
275 greg 2.12 if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
276     goto memerr;
277 greg 1.1 /* load picture */
278 greg 2.12 if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
279 greg 1.1 goto memerr;
280 greg 1.11 for (y = 0; y < ns; y++) {
281 greg 2.12 if (freadcolrs(scanin, sl, fp) < 0)
282 greg 1.1 goto readerr;
283 greg 1.11 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 greg 2.12 copycolr(pp[0].arr.c[i], scanin[x]);
288 greg 1.11 }
289 greg 1.1 }
290 greg 2.15 free((void *)scanin);
291 greg 1.1 fclose(fp);
292 greg 2.6 i = hash(pname);
293 greg 2.12 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 greg 1.1
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 greg 2.15 void
310     freedata(dta) /* release data array reference */
311     DATARRAY *dta;
312 greg 1.1 {
313 greg 2.5 DATARRAY head;
314 greg 2.6 int hval, nents;
315 greg 2.15 register DATARRAY *dpl, *dp;
316 greg 1.4 register int i;
317 greg 1.1
318 greg 2.15 if (dta == NULL) { /* free all if NULL */
319 greg 2.6 hval = 0; nents = TABSIZ;
320     } else {
321 greg 2.15 hval = hash(dta->name); nents = 1;
322 greg 2.6 }
323     while (nents--) {
324     head.next = dtab[hval];
325     dpl = &head;
326     while ((dp = dpl->next) != NULL)
327 greg 2.15 if ((dta == NULL | dta == dp)) {
328 greg 2.6 dpl->next = dp->next;
329 greg 2.12 if (dp->type == DATATY)
330 greg 2.15 free((void *)dp->arr.d);
331 greg 2.12 else
332 greg 2.15 free((void *)dp->arr.c);
333 greg 2.6 for (i = 0; i < dp->nd; i++)
334     if (dp->dim[i].p != NULL)
335 greg 2.15 free((void *)dp->dim[i].p);
336 greg 2.6 freestr(dp->name);
337 greg 2.15 free((void *)dp);
338 greg 2.6 } else
339     dpl = dp;
340     dtab[hval++] = head.next;
341     }
342 greg 1.1 }
343    
344    
345     double
346     datavalue(dp, pt) /* interpolate data value at a point */
347     register DATARRAY *dp;
348 greg 2.3 double *pt;
349 greg 1.1 {
350     DATARRAY sd;
351     int asize;
352 greg 1.6 int lower, upper;
353 greg 1.1 register int i;
354 greg 2.9 double x, y0, y1;
355 greg 1.4 /* set up dimensions for recursion */
356 greg 2.12 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 greg 1.1 }
368 greg 1.4 /* 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 greg 2.9 x *= (double)(dp->dim[0].ne - 1);
372 greg 1.4 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 greg 1.6 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 greg 1.8 else
390 greg 1.6 upper = i;
391     } while (i != (lower + upper) >> 1);
392 greg 1.8 if (i > dp->dim[0].ne - 2)
393 greg 1.4 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 greg 2.12 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 greg 1.1 } else {
411 greg 2.12 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 greg 1.1 }
419     /*
420     * Extrapolate as far as one division, then
421     * taper off harmonically to zero.
422     */
423     if (x > i+2)
424 greg 2.9 return( (2*y1-y0)/(x-(i-1)) );
425 greg 1.1
426 greg 2.9 if (x < i-1)
427     return( (2*y0-y1)/(i-x) );
428    
429     return( y0*((i+1)-x) + y1*(x-i) );
430 greg 1.1 }