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 2.2 by greg, Thu Dec 19 14:54:58 1991 UTC vs.
Revision 2.41 by greg, Thu Mar 14 06:30:53 2024 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  data.c - routines dealing with interpolated data.
9 *
10 *     6/4/86
6   */
7  
8 < #include  "standard.h"
8 > #include "copyright.h"
9  
10 < #include  "color.h"
10 > #include  <time.h>
11  
12 + #include  "platform.h"
13 + #include  "paths.h"
14 + #include  "standard.h"
15 + #include  "color.h"
16   #include  "resolu.h"
17 <
17 > #include  "view.h"
18   #include  "data.h"
19  
20 +                                /* picture memory usage before warning */
21 + #ifndef PSIZWARN
22 + #ifdef SMLMEM
23 + #define PSIZWARN        3000000
24 + #else
25 + #define PSIZWARN        50000000
26 + #endif
27 + #endif
28  
29 < extern char  *fgetword();
29 > #ifndef TABSIZ
30 > #define TABSIZ          997             /* table size (prime) */
31 > #endif
32  
33 < extern char  *libpath;                  /* library search path */
33 > #define hash(s)         (shash(s)%TABSIZ)
34  
26 static DATARRAY  *dlist = NULL;         /* data array list */
35  
36 < static DATARRAY  *plist = NULL;         /* picture list */
36 > static DATARRAY  *dtab[TABSIZ];         /* data array list */
37  
38 + static gethfunc headaspect;
39  
40 +
41   DATARRAY *
42 < getdata(dname)                          /* get data array dname */
43 < char  *dname;
42 > getdata(                                /* get data array dname */
43 >        char  *dname
44 > )
45   {
35        char  word[64];
46          char  *dfname;
47          FILE  *fp;
48 <        int  asize;
49 <        register int  i, j;
50 <        register DATARRAY  *dp;
48 >        int  asize=0;
49 >        int  i, j;
50 >        DATARRAY  *dp;
51                                                  /* look for array in list */
52 <        for (dp = dlist; dp != NULL; dp = dp->next)
52 >        for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next)
53                  if (!strcmp(dname, dp->name))
54                          return(dp);             /* found! */
45
55          /*
56           *      If we haven't loaded the data already, we will look
57 <         *  for it in the directorys specified by the library path.
57 >         *  for it in the directories specified by the library path.
58           *
59           *      The file has the following format:
60           *
# Line 63 | Line 72 | char  *dname;
72           *              0 0 ni p0i p1i .. pni
73           */
74  
75 <        if ((dfname = getpath(dname, libpath, R_OK)) == NULL) {
75 >        if ((dfname = getpath(dname, getrlibpath(), R_OK)) == NULL) {
76                  sprintf(errmsg, "cannot find data file \"%s\"", dname);
77 <                error(USER, errmsg);
77 >                error(SYSTEM, errmsg);
78          }
70        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
71                goto memerr;
72
73        dp->name = savestr(dname);
74
79          if ((fp = fopen(dfname, "r")) == NULL) {
80                  sprintf(errmsg, "cannot open data file \"%s\"", dfname);
81                  error(SYSTEM, errmsg);
82          }
83                                                          /* get dimensions */
84 <        if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
84 >        if (fgetval(fp, 'i', &asize) <= 0)
85                  goto scanerr;
86 <        dp->nd = atoi(word);
83 <        if (dp->nd <= 0 || dp->nd > MAXDDIM) {
86 >        if ((asize <= 0) | (asize > MAXDDIM)) {
87                  sprintf(errmsg, "bad number of dimensions for \"%s\"", dname);
88                  error(USER, errmsg);
89          }
90 +        if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
91 +                goto memerr;
92 +        dp->name = savestr(dname);
93 +        dp->type = DATATY;
94 +        dp->nd = asize;
95          asize = 1;
96          for (i = 0; i < dp->nd; i++) {
97 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
97 >                if (fgetval(fp, DATATY, &dp->dim[i].org) <= 0)
98                          goto scanerr;
99 <                dp->dim[i].org = atof(word);
92 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
99 >                if (fgetval(fp, DATATY, &dp->dim[i].siz) <= 0)
100                          goto scanerr;
101 <                dp->dim[i].siz = atof(word);
95 <                if (fgetword(word, sizeof(word), fp) == NULL || !isint(word))
101 >                if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0)
102                          goto scanerr;
97                dp->dim[i].ne = atoi(word);
103                  if (dp->dim[i].ne < 2)
104                          goto scanerr;
105                  asize *= dp->dim[i].ne;
106                  if ((dp->dim[i].siz -= dp->dim[i].org) == 0) {
107 <                        dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double));
107 >                        dp->dim[i].p = (DATATYPE *)
108 >                                        malloc(dp->dim[i].ne*sizeof(DATATYPE));
109                          if (dp->dim[i].p == NULL)
110                                  goto memerr;
111 <                        for (j = 0; j < dp->dim[i].ne; j++) {
112 <                                if (fgetword(word, sizeof(word), fp) == NULL ||
107 <                                                !isflt(word))
111 >                        for (j = 0; j < dp->dim[i].ne; j++)
112 >                                if (fgetval(fp, DATATY, &dp->dim[i].p[j]) <= 0)
113                                          goto scanerr;
109                                dp->dim[i].p[j] = atof(word);
110                        }
114                          for (j = 1; j < dp->dim[i].ne-1; j++)
115                                  if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) !=
116                                          (dp->dim[i].p[j] < dp->dim[i].p[j+1]))
# Line 118 | Line 121 | char  *dname;
121                  } else
122                          dp->dim[i].p = NULL;
123          }
124 <        if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
124 >        if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL)
125                  goto memerr;
126          
127 <        for (i = 0; i < asize; i++) {
128 <                if (fgetword(word, sizeof(word), fp) == NULL || !isflt(word))
127 >        for (i = 0; i < asize; i++)
128 >                if (fgetval(fp, DATATY, &dp->arr.d[i]) <= 0)
129                          goto scanerr;
127                dp->arr[i] = atof(word);
128        }
130          fclose(fp);
131 <        dp->next = dlist;
132 <        return(dlist = dp);
133 <
131 >        i = hash(dname);
132 >        dp->next = dtab[i];
133 >        return(dtab[i] = dp);
134   memerr:
135          error(SYSTEM, "out of memory in getdata");
136   scanerr:
137          sprintf(errmsg, "%s in data file \"%s\"",
138                          feof(fp) ? "unexpected EOF" : "bad format", dfname);
139          error(USER, errmsg);
140 +        return NULL;    /* pro forma return */
141   }
142  
143  
144 < static double  inpaspect;               /* aspect ratio of input picture */
145 <
146 < static
147 < headaspect(s)                           /* check string for aspect ratio */
148 < char  *s;
144 > static int
145 > headaspect(                     /* check string for aspect ratio */
146 >        char  *s,
147 >        void  *iap
148 > )
149   {
150 +        char    fmt[MAXFMTLEN];
151 +
152          if (isaspect(s))
153 <                inpaspect *= aspectval(s);
153 >                *(double*)iap *= aspectval(s);
154 >        else if (formatval(fmt, s) && strcmp(fmt, COLRFMT))
155 >                *(double*)iap = 0.0;
156 >        return(0);
157   }
158  
152
159   DATARRAY *
160 < getpict(pname)                          /* get picture pname */
161 < char  *pname;
160 > getpict(                                /* get picture pname */
161 >        char  *pname
162 > )
163   {
164 <        extern char  *libpath;
164 >        double  inpaspect;
165          char  *pfname;
166          FILE  *fp;
167 <        COLOR  *scanin;
167 >        COLR  *scanin;
168          int  sl, ns;
169 <        RESOLU  inpres;
170 <        FLOAT  loc[2];
169 >        RESOLU  inpres;
170 >        RREAL  loc[2];
171          int  y;
172 <        register int  x, i;
173 <        register DATARRAY  *pp;
172 >        int  x, i;
173 >        DATARRAY  *pp;
174                                                  /* look for array in list */
175 <        for (pp = plist; pp != NULL; pp = pp->next)
175 >        for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next)
176                  if (!strcmp(pname, pp->name))
177                          return(pp);             /* found! */
178  
179 <        if ((pfname = getpath(pname, libpath, R_OK)) == NULL) {
179 >        if ((pfname = getpath(pname, getrlibpath(), R_OK)) == NULL) {
180                  sprintf(errmsg, "cannot find picture file \"%s\"", pname);
181 <                error(USER, errmsg);
181 >                error(SYSTEM, errmsg);
182          }
183 <        if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL)
183 >        if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL)
184                  goto memerr;
185  
186 <        pp[0].name =
180 <        pp[1].name =
181 <        pp[2].name = savestr(pname);
186 >        pp[0].name = savestr(pname);
187  
188 <        if ((fp = fopen(pfname, "r")) == NULL) {
188 >        if ((fp = fopen(pfname, "rb")) == NULL) {
189                  sprintf(errmsg, "cannot open picture file \"%s\"", pfname);
190                  error(SYSTEM, errmsg);
191          }
192                                                  /* get dimensions */
193          inpaspect = 1.0;
194 <        getheader(fp, headaspect);
195 <        if (!fgetsresolu(&inpres, fp))
194 >        getheader(fp, headaspect, &inpaspect);
195 >        if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp))
196                  goto readerr;
197 <        for (i = 0; i < 3; i++) {
198 <                pp[i].nd = 2;
199 <                pp[i].dim[0].ne = inpres.yr;
200 <                pp[i].dim[1].ne = inpres.xr;
201 <                pp[i].dim[0].org =
202 <                pp[i].dim[1].org = 0.0;
203 <                if (inpres.xr <= inpres.yr*inpaspect) {
204 <                        pp[i].dim[0].siz = inpaspect *
205 <                                                (double)inpres.yr/inpres.xr;
206 <                        pp[i].dim[1].siz = 1.0;
207 <                } else {
208 <                        pp[i].dim[0].siz = 1.0;
209 <                        pp[i].dim[1].siz = (double)inpres.xr/inpres.yr /
205 <                                                inpaspect;
206 <                }
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;
197 >        pp[0].nd = 2;
198 >        pp[0].dim[0].ne = inpres.yr;
199 >        pp[0].dim[1].ne = inpres.xr;
200 >        pp[0].dim[0].org =
201 >        pp[0].dim[1].org = 0.0;
202 >        if (inpres.xr <= inpres.yr*inpaspect) {
203 >                pp[0].dim[0].siz = inpaspect *
204 >                                        (double)inpres.yr/inpres.xr;
205 >                pp[0].dim[1].siz = 1.0;
206 >        } else {
207 >                pp[0].dim[0].siz = 1.0;
208 >                pp[0].dim[1].siz = (double)inpres.xr/inpres.yr /
209 >                                        inpaspect;
210          }
211 <                                                        /* load picture */
212 <        sl = scanlen(&inpres);
211 >        pp[0].dim[0].p = pp[0].dim[1].p = NULL;
212 >        sl = scanlen(&inpres);                          /* allocate array */
213          ns = numscans(&inpres);
214 <        if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL)
214 >        i = ns*sl*sizeof(COLR);
215 > #if PSIZWARN
216 >        if (i > PSIZWARN) {                             /* memory warning */
217 >                sprintf(errmsg, "picture file \"%s\" using %.1f MB of memory",
218 >                                pname, i*(1.0/(1024*1024)));
219 >                error(WARNING, errmsg);
220 >        }
221 > #endif
222 >        if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL)
223                  goto memerr;
224 +                                                        /* load picture */
225 +        if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL)
226 +                goto memerr;
227          for (y = 0; y < ns; y++) {
228 <                if (freadscan(scanin, sl, fp) < 0)
228 >                if (freadcolrs(scanin, sl, fp) < 0)
229                          goto readerr;
230                  for (x = 0; x < sl; x++) {
231                          pix2loc(loc, &inpres, x, y);
232                          i = (int)(loc[1]*inpres.yr)*inpres.xr +
233                                          (int)(loc[0]*inpres.xr);
234 <                        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);
234 >                        copycolr(pp[0].arr.c[i], scanin[x]);
235                  }
236          }
237 <        free((char *)scanin);
237 >        free(scanin);
238          fclose(fp);
239 <        pp[0].next =
240 <        pp[1].next =
241 <        pp[2].next = plist;
242 <        return(plist = pp);
243 <
239 >        i = hash(pname);
240 >        pp[0].next = dtab[i];           /* link into picture list */
241 >        pp[1] = pp[0];
242 >        pp[2] = pp[0];
243 >        pp[0].type = RED;               /* differentiate RGB records */
244 >        pp[1].type = GRN;
245 >        pp[2].type = BLU;
246 >        return(dtab[i] = pp);
247   memerr:
248          error(SYSTEM, "out of memory in getpict");
249   readerr:
250          sprintf(errmsg, "bad picture file \"%s\"", pfname);
251          error(USER, errmsg);
252 +        return NULL;    /* pro forma return */
253   }
254  
255  
256 < freedata(dname)                 /* free memory associated with dname */
257 < char  *dname;
256 > /* header info type for hyperspectral image */
257 > typedef struct {
258 >        float   wlpart[4];      /* wavelength partitions */
259 >        int     nc;             /* number of components */
260 >        double  inpaspect;      /* pixel aspect ratio */
261 > } SPECINFO;
262 >
263 > static int
264 > specheadline(                           /* get info for spectral image */
265 >        char  *s,
266 >        void  *cdp
267 > )
268   {
269 <        register DATARRAY  *dp, *dpl;
270 <        register int  i;
269 >        SPECINFO        *sip = (SPECINFO *)cdp;
270 >        char            fmt[MAXFMTLEN];
271  
272 <        for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next)
273 <                if (!strcmp(dname, dp->name)) {
274 <                        if (dpl == NULL)
275 <                                dlist = dp->next;
276 <                        else
277 <                                dpl->next = dp->next;
278 <                        free((char *)dp->arr);
279 <                        for (i = 0; i < dp->nd; i++)
280 <                                if (dp->dim[i].p != NULL)
260 <                                        free((char *)dp->dim[i].p);
261 <                        freestr(dp->name);
262 <                        free((char *)dp);
263 <                        return;
264 <                }
272 >        if (isaspect(s))
273 >                sip->inpaspect *= aspectval(s);
274 >        else if (isncomp(s))
275 >                sip->nc = ncompval(s);
276 >        else if (iswlsplit(s))
277 >                wlsplitval(sip->wlpart, s);
278 >        else if (formatval(fmt, s) && strcmp(fmt, SPECFMT))
279 >                return(-1);
280 >        return(0);
281   }
282  
283 + DATARRAY *
284 + getspec(                /* load hyperspectral image as data */
285 +        char *sname
286 + )
287 + {
288 +        SPECINFO        si;
289 +        char            *pfname;
290 +        FILE            *fp;
291 +        int             sl, ns;
292 +        int             y, i;
293 +        DATARRAY        *pp;
294 +                                                /* look for array in list */
295 +        for (pp = dtab[hash(sname)]; pp != NULL; pp = pp->next)
296 +                if (!strcmp(sname, pp->name))
297 +                        return(pp);             /* found! */
298  
299 < freepict(pname)                 /* free memory associated with pname */
300 < char  *pname;
299 >        if ((pfname = getpath(sname, getrlibpath(), R_OK)) == NULL) {
300 >                sprintf(errmsg, "cannot find hyperspectral image \"%s\"", sname);
301 >                error(SYSTEM, errmsg);
302 >        }
303 >        if ((fp = fopen(pfname, "rb")) == NULL) {
304 >                sprintf(errmsg, "cannot open hyperspectral image \"%s\"", pfname);
305 >                error(SYSTEM, errmsg);
306 >        }
307 >        si.wlpart[3] = 0;
308 >        si.nc = 0;
309 >        si.inpaspect = 1.0;
310 >        if (getheader(fp, specheadline, &si) < 0 ||
311 >                        (si.nc <= 3) | (si.nc > MAXCSAMP) | (si.wlpart[3] < 1) ||
312 >                        !fscnresolu(&sl, &ns, fp))
313 >                goto readerr;
314 >
315 >        if ((pp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL)
316 >                goto memerr;
317 >
318 >        pp->name = savestr(sname);
319 >        pp->type = SPECTY;
320 >        pp->nd = 3;
321 >        pp->dim[0].ne = ns;
322 >        pp->dim[1].ne = sl;
323 >        pp->dim[0].org =
324 >        pp->dim[1].org = 0.0;
325 >        if (sl <= ns*si.inpaspect) {
326 >                pp->dim[0].siz = si.inpaspect * (double)ns/sl;
327 >                pp->dim[1].siz = 1.0;
328 >        } else {
329 >                pp->dim[0].siz = 1.0;
330 >                pp->dim[1].siz = (double)sl/ns / si.inpaspect;
331 >        }
332 >        pp->dim[2].ne = si.nc;
333 >        pp->dim[2].siz = si.wlpart[3] - si.wlpart[0];
334 >        pp->dim[2].org = si.wlpart[0] + 0.5*pp->dim[2].siz/si.nc;
335 >        pp->dim[2].siz *= (si.nc - 1.0)/si.nc;
336 >        pp->dim[0].p = pp->dim[1].p = pp->dim[2].p = NULL;
337 >        i = ns*sl*(si.nc+1);
338 > #if PSIZWARN
339 >        if (i > PSIZWARN) {                     /* memory warning */
340 >                sprintf(errmsg, "hyperspectral image \"%s\" using %.1f MB of memory",
341 >                                sname, i*(1.0/(1024*1024)));
342 >                error(WARNING, errmsg);
343 >        }
344 > #endif
345 >        if ((pp->arr.s = (uby8 *)malloc(i)) == NULL)
346 >                goto memerr;
347 >        for (y = 0; y < ns; y++)                /* read each scanline */
348 >                if (freadscolrs(pp->arr.s + y*sl*(si.nc+1), si.nc, sl, fp) < 0)
349 >                        goto readerr;
350 >        fclose(fp);
351 >        i = hash(sname);                        /* insert in hash table */
352 >        pp->next = dtab[i];
353 >        return(dtab[i] = pp);
354 > memerr:
355 >        error(SYSTEM, "out of memory in getspec");
356 > readerr:
357 >        sprintf(errmsg, "bad hyperspectral image \"%s\"", pfname);
358 >        error(USER, errmsg);
359 >        return NULL;    /* pro forma return */
360 > }
361 >
362 >
363 > void
364 > freedata(                       /* release data array reference */
365 >        DATARRAY  *dta
366 > )
367   {
368 <        register DATARRAY  *pp, *ppl;
368 >        DATARRAY  head;
369 >        int  hval, nents;
370 >        DATARRAY  *dpl, *dp;
371 >        int  i;
372  
373 <        for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next)
374 <                if (!strcmp(pname, pp->name)) {
375 <                        if (ppl == NULL)
376 <                                plist = pp->next;
377 <                        else
278 <                                ppl->next = pp->next;
279 <                        free((char *)pp[0].arr);
280 <                        free((char *)pp[1].arr);
281 <                        free((char *)pp[2].arr);
282 <                        freestr(pp[0].name);
283 <                        free((char *)pp);
373 >        if (dta == NULL) {                      /* free all if NULL */
374 >                hval = 0; nents = TABSIZ;
375 >        } else {
376 >                if (dta->next == dta) {
377 >                        free(dta);              /* unlisted temp array */
378                          return;
379                  }
380 +                hval = hash(dta->name); nents = 1;
381 +                if (!*dta->name) {              /* not a data file? */
382 +                        dta->next = dtab[hval];
383 +                        dtab[hval] = dta;       /* ...fake position */
384 +                }
385 +        }
386 +        while (nents--) {
387 +                head.next = dtab[hval];
388 +                dpl = &head;
389 +                while ((dp = dpl->next) != NULL)
390 +                        if ((dta == NULL) | (dta == dp)) {
391 +                                dpl->next = dp->next;
392 +                                free(dp->arr.p);
393 +                                for (i = 0; i < dp->nd; i++)
394 +                                        if (dp->dim[i].p != NULL)
395 +                                                free(dp->dim[i].p);
396 +                                freestr(dp->name);
397 +                                free(dp);
398 +                        } else
399 +                                dpl = dp;
400 +                dtab[hval++] = head.next;
401 +        }
402   }
403  
404  
405 < double
406 < datavalue(dp, pt)               /* interpolate data value at a point */
407 < register DATARRAY  *dp;
292 < double  *pt;
405 > /* internal call to interpolate data value or vector */
406 > static double
407 > data_interp(DATARRAY *dp, double *pt, double coef, DATATYPE *rvec)
408   {
409 <        DATARRAY  sd;
410 <        int  asize;
411 <        int  lower, upper;
412 <        register int  i;
413 <        double  x, y, y0, y1;
409 >        DATARRAY        sd;
410 >        int             stride, i;
411 >        double          x, c0, c1, y0, y1;
412 >                                        /* unlikely, but may as well check */
413 >        if ((-FTINY <= coef) & (coef <= FTINY))
414 >                return(0.);
415                                          /* set up dimensions for recursion */
416 <        sd.nd = dp->nd - 1;
417 <        asize = 1;
418 <        for (i = 0; i < sd.nd; i++) {
419 <                sd.dim[i].org = dp->dim[i+1].org;
420 <                sd.dim[i].siz = dp->dim[i+1].siz;
421 <                sd.dim[i].p = dp->dim[i+1].p;
422 <                asize *= sd.dim[i].ne = dp->dim[i+1].ne;
416 >        if (dp->nd > 1) {
417 >                sd.name = dp->name;
418 >                sd.type = dp->type;
419 >                sd.nd = dp->nd - 1;
420 >                memcpy(sd.dim, dp->dim+1, sd.nd*sizeof(struct dadim));
421 >                stride = sd.dim[i = sd.nd-1].ne + (sd.type==SPECTY);
422 >                while (i-- > 0)
423 >                        stride *= sd.dim[i].ne;
424          }
425                                          /* get independent variable */
426          if (dp->dim[0].p == NULL) {             /* evenly spaced points */
427                  x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz;
428 <                x = x * (dp->dim[0].ne - 1);
428 >                x *= (double)(dp->dim[0].ne - 1);
429                  i = x;
430                  if (i < 0)
431                          i = 0;
432                  else if (i > dp->dim[0].ne - 2)
433                          i = dp->dim[0].ne - 2;
434          } else {                                /* unevenly spaced points */
435 <                if (dp->dim[0].siz > 0.0) {
435 >                int     lower, upper;
436 >                if (dp->dim[0].siz > 0.) {
437                          lower = 0;
438                          upper = dp->dim[0].ne;
439                  } else {
# Line 329 | Line 447 | double  *pt;
447                          else
448                                  upper = i;
449                  } while (i != (lower + upper) >> 1);
450 +
451                  if (i > dp->dim[0].ne - 2)
452                          i = dp->dim[0].ne - 2;
453 +
454                  x = i + (pt[0] - dp->dim[0].p[i]) /
455                                  (dp->dim[0].p[i+1] - dp->dim[0].p[i]);
456          }
337                                        /* get dependent variable */
338        if (dp->nd == 1) {
339                y0 = dp->arr[i];
340                y1 = dp->arr[i+1];
341        } else {
342                sd.arr = &dp->arr[i*asize];
343                y0 = datavalue(&sd, pt+1);
344                sd.arr = &dp->arr[(i+1)*asize];
345                y1 = datavalue(&sd, pt+1);
346        }
457          /*
458 <         * Extrapolate as far as one division, then
458 >         * Compute interpolation coefficients:
459 >         * extrapolate as far as one division, then
460           * taper off harmonically to zero.
461           */
462 <        if (x > i+2)
463 <                y = (2*y1-y0)/(x-i-1);
464 <        else if (x < i-1)
465 <                y = (2*y0-y1)/(i-x);
466 <        else
467 <                y = y0*((i+1)-x) + y1*(x-i);
462 >        if (x > i+2) {
463 >                c0 = 1./(i-1 - x);
464 >                c1 = -2.*c0;
465 >        } else if (x < i-1) {
466 >                c1 = 1./(i - x);
467 >                c0 = -2.*c1;
468 >        } else {
469 >                c0 = i+1 - x;
470 >                c1 = x - i;
471 >        }
472 >        c0 *= coef;
473 >        c1 *= coef;
474 >                                        /* check if vector interp */
475 >        if ((dp->nd == 2) & (rvec != NULL)) {
476 >                if (dp->type == DATATY) {
477 >                        sd.arr.d = dp->arr.d + i*stride;
478 >                        for (i = sd.dim[0].ne; i--; )
479 >                                rvec[i] += c0*sd.arr.d[i]
480 >                                        + c1*sd.arr.d[i+stride];
481 >                } else if (dp->type == SPECTY) {
482 >                        double  f;
483 >                        sd.arr.s = dp->arr.s + i*stride;
484 >                        if ((sd.arr.s[sd.dim[0].ne] > 0) & ((-FTINY>c0)|(c0>FTINY))) {
485 >                                f = ldexp(c0, (int)sd.arr.s[sd.dim[0].ne]-(COLXS+8));
486 >                                for (i = sd.dim[0].ne; i--; )
487 >                                        rvec[i] += f*(sd.arr.s[i] + .5);
488 >                        }
489 >                        sd.arr.s += stride;
490 >                        if ((sd.arr.s[sd.dim[0].ne] > 0) & ((-FTINY>c1)|(c1>FTINY))) {
491 >                                f = ldexp(c1, (int)sd.arr.s[sd.dim[0].ne]-(COLXS+8));
492 >                                for (i = sd.dim[0].ne; i--; )
493 >                                        rvec[i] += f*(sd.arr.s[i] + .5);
494 >                        }
495 >                } else {
496 >                        sd.arr.c = dp->arr.c + i*stride;
497 >                        for (i = sd.dim[0].ne; i--; )
498 >                                rvec[i] += c0*colrval(sd.arr.c[i],sd.type)
499 >                                        + c1*colrval(sd.arr.c[i+stride],sd.type);
500 >                }
501 >                return(0.);             /* return value ignored */
502 >        }
503 >                                        /* get dependent variable */
504 >        if (dp->nd > 1) {
505 >                if (dp->type == DATATY) {
506 >                        sd.arr.d = dp->arr.d + i*stride;
507 >                        y0 = data_interp(&sd, pt+1, c0, rvec);
508 >                        sd.arr.d += stride;
509 >                } else if (dp->type == SPECTY) {
510 >                        sd.arr.s = dp->arr.s + i*stride;
511 >                        y0 = data_interp(&sd, pt+1, c0, rvec);
512 >                        sd.arr.s += stride;
513 >                } else {
514 >                        sd.arr.c = dp->arr.c + i*stride;
515 >                        y0 = data_interp(&sd, pt+1, c0, rvec);
516 >                        sd.arr.c += stride;
517 >                }
518 >                y1 = data_interp(&sd, pt+1, c1, rvec);
519 >        } else {                        /* end of recursion */
520 >                if (dp->type == DATATY) {
521 >                        y0 = dp->arr.d[i];
522 >                        y1 = dp->arr.d[i+1];
523 >                } else if (dp->type == SPECTY) {
524 >                        if (dp->arr.s[dp->dim[0].ne]) {
525 >                                double  f = dp->arr.s[dp->dim[0].ne]
526 >                                        ? ldexp(1., -(COLXS+8) +
527 >                                                (int)dp->arr.s[dp->dim[0].ne])
528 >                                        : 0.;
529 >                                y0 = f*(dp->arr.s[i] + 0.5);
530 >                                y1 = f*(dp->arr.s[i+1] + 0.5);
531 >                        } else
532 >                                y0 = y1 = 0.;
533 >                } else {
534 >                        y0 = colrval(dp->arr.c[i],dp->type);
535 >                        y1 = colrval(dp->arr.c[i+1],dp->type);
536 >                }
537 >                y0 *= c0;
538 >                y1 *= c1;
539 >        }
540 >        return(y0 + y1);        /* coefficients already applied */
541 > }
542  
543 <        return(y);
543 >
544 > double
545 > datavalue(              /* interpolate data value at a point */
546 >        DATARRAY  *dp,
547 >        double  *pt
548 > )
549 > {
550 >        return(data_interp(dp, pt, 1., NULL));
551   }
552 +
553 +
554 + /* Interpolate final vector corresponding to last dimension in data array */
555 + DATARRAY *
556 + datavector(DATARRAY *dp, double *pt)
557 + {
558 +        DATARRAY        *newdp;
559 +
560 +        if (dp->nd < 2)
561 +                error(INTERNAL, "datavector() called with 1-D array");
562 +                                        /* create vector array */
563 +        newdp = (DATARRAY *)malloc(sizeof(DATARRAY) -
564 +                                (MAXDDIM-1)*sizeof(struct dadim) +
565 +                                sizeof(DATATYPE)*dp->dim[dp->nd-1].ne);
566 +        if (newdp == NULL)
567 +                error(SYSTEM, "out of memory in datavector");
568 +        newdp->next = newdp;            /* flags us as temp vector */
569 +        newdp->name = dp->name;
570 +        newdp->type = DATATY;
571 +        newdp->nd = 1;                  /* vector data goes here */
572 +        newdp->dim[0] = dp->dim[dp->nd-1];
573 +        newdp->arr.d = (DATATYPE *)(newdp->dim + 1);
574 +        memset(newdp->arr.d, 0, sizeof(DATATYPE)*newdp->dim[0].ne);
575 +
576 +        (void)data_interp(dp, pt, 1., newdp->arr.d);
577 +
578 +        return(newdp);                  /* will be free'd using free() */
579 + }
580 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines