ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/interp2d.c
(Generate patch)

Comparing ray/src/common/interp2d.c (file contents):
Revision 2.3 by greg, Mon Feb 11 20:01:15 2013 UTC vs.
Revision 2.17 by greg, Fri Mar 19 21:16:15 2021 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9  
10   #include "copyright.h"
11  
12 < /*************************************************************
12 > /***************************************************************
13   * This is a general method for 2-D interpolation similar to
14   * radial basis functions but allowing for a good deal of local
15   * anisotropy in the point distribution.  Each sample point
16   * is examined to determine the closest neighboring samples in
17   * each of NI2DIR surrounding directions.  To speed this
18 < * calculation, we sort the data into 3 half-planes and
19 < * perform simple tests to see which neighbor is closest in
20 < * a each direction.  Once we have our approximate neighborhood
18 > * calculation, we sort the data into half-planes and apply
19 > * simple tests to see which neighbor is closest in each
20 > * angular slice.  Once we have our approximate neighborhood
21   * for a sample, we can use it in a modified Gaussian weighting
22 < * scheme with anisotropic surround.  Harmonic weighting is added
22 > * with allowing local anisotropy.  Harmonic weighting is added
23   * to reduce the influence of distant neighbors.  This yields a
24   * smooth interpolation regardless of how the sample points are
25 < * initiallydistributed.  Evaluation is accelerated by use of a
26 < * fast approximation to the atan2(y,x) function.
27 < **************************************************************/
25 > * initially distributed.  Evaluation is accelerated by use of
26 > * a fast approximation to the atan2(y,x) function and a low-res
27 > * map indicating where sample weights are significant.
28 > ****************************************************************/
29  
30   #include <stdio.h>
31   #include <stdlib.h>
32   #include "rtmath.h"
33   #include "interp2d.h"
34  
35 < #define DECODE_RAD(ip,er)       ((ip)->rmin*(1. + .5*(er)))
36 < #define ENCODE_RAD(ip,r)        ((int)(2.*(r)/(ip)->rmin) - 2)
35 > #define DECODE_DIA(ip,ed)       ((ip)->dmin*(1. + .5*(ed)))
36 > #define ENCODE_DIA(ip,d)        ((int)(2.*(d)/(ip)->dmin) - 2)
37  
38   /* Sample order (private) */
39   typedef struct {
# Line 40 | Line 41 | typedef struct {
41          float   dm;             /* distance measure in this direction */
42   } SAMPORD;
43  
44 + /* private routine to encode sample diameter with range checks */
45 + static int
46 + encode_diameter(const INTERP2 *ip, double d)
47 + {
48 +        const int       ed = ENCODE_DIA(ip, d);
49 +
50 +        if (ed <= 0)
51 +                return(0);
52 +        if (ed >= 0xffff)
53 +                return(0xffff);
54 +        return(ed);
55 + }
56 +
57   /* Allocate a new set of interpolation samples (caller assigns spt[] array) */
58   INTERP2 *
59   interp2_alloc(int nsamps)
# Line 54 | Line 68 | interp2_alloc(int nsamps)
68                  return(NULL);
69  
70          nip->ns = nsamps;
71 <        nip->rmin = .5;         /* default radius minimum */
71 >        nip->dmin = 1;          /* default minimum diameter */
72          nip->smf = NI2DSMF;     /* default smoothing factor */
73 <        nip->ra = NULL;
73 >        nip->c_data = NULL;
74 >        nip->da = NULL;
75                                  /* caller must assign spt[] array */
76          return(nip);
77   }
# Line 65 | Line 80 | interp2_alloc(int nsamps)
80   INTERP2 *
81   interp2_realloc(INTERP2 *ip, int nsamps)
82   {
83 +        INTERP2 *old_ip = ip;
84 +
85          if (ip == NULL)
86                  return(interp2_alloc(nsamps));
87          if (nsamps <= 1) {
88                  interp2_free(ip);
89                  return(NULL);
90          }
91 <        if (nsamps == ip->ns);
91 >        if (nsamps == ip->ns)
92                  return(ip);
93 <        if (ip->ra != NULL) {   /* will need to recompute distribution */
94 <                free(ip->ra);
95 <                ip->ra = NULL;
93 >        if (ip->da != NULL) {   /* will need to recompute distribution */
94 >                free(ip->da);
95 >                ip->da = NULL;
96          }
97          ip = (INTERP2 *)realloc(ip, sizeof(INTERP2)+sizeof(float)*2*(nsamps-1));
98 <        if (ip == NULL)
99 <                return(NULL);
98 >        if (ip == NULL) {
99 >                if (nsamps <= ip->ns) {
100 >                        ip = old_ip;
101 >                } else {
102 >                        free(old_ip);
103 >                        return(NULL);
104 >                }
105 >        }
106          ip->ns = nsamps;
107          return(ip);
108   }
109  
110 + /* Set minimum distance under which samples will start to merge */
111 + void
112 + interp2_spacing(INTERP2 *ip, double mind)
113 + {
114 +        if (mind <= 0)
115 +                return;
116 +        if ((.998*ip->dmin <= mind) & (mind <= 1.002*ip->dmin))
117 +                return;
118 +        if (ip->da != NULL) {   /* will need to recompute distribution */
119 +                free(ip->da);
120 +                ip->da = NULL;
121 +        }
122 +        ip->dmin = mind;
123 + }
124 +
125 + /* Compute unnormalized weight for a position relative to a sample */
126 + double
127 + interp2_wti(INTERP2 *ip, const int i, double x, double y)
128 + {
129 +        double          dir, rd, r2, d2;
130 +        int             ri;
131 +                                /* get relative direction */
132 +        x -= ip->spt[i][0];
133 +        y -= ip->spt[i][1];
134 +        dir = atan2a(y, x);
135 +        dir += 2.*PI*(dir < 0);
136 +                                /* linear radius interpolation */
137 +        rd = dir * (NI2DIR/2./PI);
138 +        ri = (int)rd;
139 +        rd -= (double)ri;
140 +        rd = (1.-rd)*ip->da[i].dia[ri] + rd*ip->da[i].dia[(ri+1)%NI2DIR];
141 +        rd = ip->smf * DECODE_DIA(ip, rd);
142 +        r2 = 2.*rd*rd;
143 +        d2 = x*x + y*y;
144 +        if (d2 > 21.*r2)        /* result would be < 1e-9 */
145 +                return(.0);
146 +                                /* Gaussian times harmonic weighting */
147 +        return( exp(-d2/r2) * ip->dmin/(ip->dmin + sqrt(d2)) );
148 + }
149 +
150 + /* private call to get grid flag index */
151 + static int
152 + interp2_flagpos(int fgi[2], INTERP2 *ip, double x, double y)
153 + {
154 +        int     inbounds = 0;
155 +
156 +        if (ip == NULL)         /* paranoia */
157 +                return(-1);
158 +                                /* need to compute interpolant? */
159 +        if (ip->da == NULL && !interp2_analyze(ip))
160 +                return(-1);
161 +                                /* get x & y grid positions */
162 +        fgi[0] = (x - ip->smin[0]) * NI2DIM / (ip->smax[0] - ip->smin[0]);
163 +
164 +        if (fgi[0] >= NI2DIM)
165 +                fgi[0] = NI2DIM-1;
166 +        else if (fgi[0] < 0)
167 +                fgi[0] = 0;
168 +        else
169 +                ++inbounds;
170 +
171 +        fgi[1] = (y - ip->smin[1]) * NI2DIM / (ip->smax[1] - ip->smin[1]);
172 +
173 +        if (fgi[1] >= NI2DIM)
174 +                fgi[1] = NI2DIM-1;
175 +        else if (fgi[1] < 0)
176 +                fgi[1] = 0;
177 +        else
178 +                ++inbounds;
179 +
180 +        return(inbounds == 2);
181 + }
182 +
183 + #define setflg(fl,xi,yi)        ((fl)[yi] |= 1<<(xi))
184 +
185 + #define chkflg(fl,xi,yi)        ((fl)[yi]>>(xi) & 1)
186 +
187 + /* private flood function to determine sample influence */
188 + static void
189 + influence_flood(INTERP2 *ip, const int i, unsigned short visited[NI2DIM],
190 +                        int xfi, int yfi)
191 + {
192 +        double  gx = (xfi+.5)*(1./NI2DIM)*(ip->smax[0] - ip->smin[0]) +
193 +                                        ip->smin[0];
194 +        double  gy = (yfi+.5)*(1./NI2DIM)*(ip->smax[1] - ip->smin[1]) +
195 +                                        ip->smin[1];
196 +        double  dx = gx - ip->spt[i][0];
197 +        double  dy = gy - ip->spt[i][1];
198 +
199 +        setflg(visited, xfi, yfi);
200 +
201 +        if (dx*dx + dy*dy > 2.*ip->grid2 && interp2_wti(ip, i, gx, gy) <= 1e-7)
202 +                return;
203 +
204 +        setflg(ip->da[i].infl, xfi, yfi);
205 +
206 +        if (xfi > 0 && !chkflg(visited, xfi-1, yfi))
207 +                influence_flood(ip, i, visited, xfi-1, yfi);
208 +
209 +        if (yfi > 0 && !chkflg(visited, xfi, yfi-1))
210 +                influence_flood(ip, i, visited, xfi, yfi-1);
211 +
212 +        if (xfi < NI2DIM-1 && !chkflg(visited, xfi+1, yfi))
213 +                influence_flood(ip, i, visited, xfi+1, yfi);
214 +
215 +        if (yfi < NI2DIM-1 && !chkflg(visited, xfi, yfi+1))
216 +                influence_flood(ip, i, visited, xfi, yfi+1);
217 + }
218 +
219 + /* private call to compute sample influence maps */
220 + static void
221 + map_influence(INTERP2 *ip)
222 + {
223 +        unsigned short  visited[NI2DIM];
224 +        int             fgi[2];
225 +        int             i, j;
226 +
227 +        for (i = ip->ns; i--; ) {
228 +                for (j = NI2DIM; j--; ) {
229 +                        ip->da[i].infl[j] = 0;
230 +                        visited[j] = 0;
231 +                }
232 +                interp2_flagpos(fgi, ip, ip->spt[i][0], ip->spt[i][1]);
233 +
234 +                influence_flood(ip, i, visited, fgi[0], fgi[1]);
235 +        }
236 + }
237 +
238 + /* Modify smoothing parameter by the given factor */
239 + void
240 + interp2_smooth(INTERP2 *ip, double sf)
241 + {
242 +        float   old_smf = ip->smf;
243 +
244 +        if ((ip->smf *= sf) < NI2DSMF)
245 +                ip->smf = NI2DSMF;
246 +                                        /* need to recompute influence maps? */
247 +        if (ip->da != NULL && (old_smf*.85 > ip->smf) |
248 +                                (ip->smf > old_smf*1.15))
249 +                map_influence(ip);
250 + }
251 +
252   /* private call-back to sort position index */
253   static int
254   cmp_spos(const void *p1, const void *p2)
# Line 110 | Line 275 | sort_samples(SAMPORD *sord, const INTERP2 *ip, double
275                  sord[i].si = i;
276                  sord[i].dm = cosd*ip->spt[i][0] + sind*ip->spt[i][1];
277          }
278 <        qsort(sord, ip->ns, sizeof(SAMPORD), &cmp_spos);
278 >        qsort(sord, ip->ns, sizeof(SAMPORD), cmp_spos);
279   }
280  
116 /* private routine to encode radius with range checks */
117 static int
118 encode_radius(const INTERP2 *ip, double r)
119 {
120        const int       er = ENCODE_RAD(ip, r);
121
122        if (er <= 0)
123                return(0);
124        if (er >= 0xffff)
125                return(0xffff);
126        return(er);
127 }
128
281   /* (Re)compute anisotropic basis function interpolant (normally automatic) */
282   int
283   interp2_analyze(INTERP2 *ip)
284   {
285          SAMPORD *sortord;
286          int     *rightrndx, *leftrndx, *endrndx;
287 <        int     bd;
287 >        int     i, bd;
288                                          /* sanity checks */
289 <        if (ip == NULL || (ip->ns <= 1) | (ip->rmin <= 0))
289 >        if (ip == NULL)
290                  return(0);
291 <                                        /* need to allocate? */
292 <        if (ip->ra == NULL) {
293 <                ip->ra = (unsigned short (*)[NI2DIR])malloc(
142 <                                sizeof(unsigned short)*NI2DIR*ip->ns);
143 <                if (ip->ra == NULL)
144 <                        return(0);
291 >        if (ip->da != NULL) {           /* free previous data if any */
292 >                free(ip->da);
293 >                ip->da = NULL;
294          }
295 <                                        /* get temporary arrays */
295 >        if ((ip->ns <= 1) | (ip->dmin <= 0))
296 >                return(0);
297 >                                        /* compute sample domain */
298 >        ip->smin[0] = ip->smin[1] = FHUGE;
299 >        ip->smax[0] = ip->smax[1] = -FHUGE;
300 >        for (i = ip->ns; i--; ) {
301 >                if (ip->spt[i][0] < ip->smin[0]) ip->smin[0] = ip->spt[i][0];
302 >                if (ip->spt[i][0] > ip->smax[0]) ip->smax[0] = ip->spt[i][0];
303 >                if (ip->spt[i][1] < ip->smin[1]) ip->smin[1] = ip->spt[i][1];
304 >                if (ip->spt[i][1] > ip->smax[1]) ip->smax[1] = ip->spt[i][1];
305 >        }
306 >        ip->grid2 = ((ip->smax[0]-ip->smin[0])*(ip->smax[0]-ip->smin[0]) +
307 >                        (ip->smax[1]-ip->smin[1])*(ip->smax[1]-ip->smin[1])) *
308 >                                (1./NI2DIM/NI2DIM);
309 >        if (ip->grid2 <= FTINY*ip->dmin*ip->dmin)
310 >                return(0);
311 >                                        /* allocate analysis data */
312 >        ip->da = (struct interp2_samp *)malloc(
313 >                                sizeof(struct interp2_samp)*ip->ns );
314 >        if (ip->da == NULL)
315 >                return(0);
316 >                                        /* allocate temporary arrays */
317          sortord = (SAMPORD *)malloc(sizeof(SAMPORD)*ip->ns);
318          rightrndx = (int *)malloc(sizeof(int)*ip->ns);
319          leftrndx = (int *)malloc(sizeof(int)*ip->ns);
# Line 155 | Line 325 | interp2_analyze(INTERP2 *ip)
325          for (bd = 0; bd < NI2DIR/2; bd++) {
326              const double        ang = 2.*PI/NI2DIR*bd;
327              int                 *sptr;
158            int                 i;
328                                          /* create right reverse index */
329              if (bd) {                   /* re-use from previous iteration? */
330                  sptr = rightrndx;
# Line 186 | Line 355 | interp2_analyze(INTERP2 *ip)
355                  const int       ii = sortord[i].si;
356                  int             j;
357                                          /* preload with large radii */
358 <                ip->ra[ii][bd] = ip->ra[ii][bd+NI2DIR/2] = encode_radius(ip,
359 <                            .25*(sortord[ip->ns-1].dm - sortord[0].dm));
358 >                ip->da[ii].dia[bd] =
359 >                ip->da[ii].dia[bd+NI2DIR/2] = encode_diameter(ip,
360 >                                .5*(sortord[ip->ns-1].dm - sortord[0].dm));
361                  for (j = i; ++j < ip->ns; )     /* nearest above */
362                      if (rightrndx[sortord[j].si] > rightrndx[ii] &&
363                                      leftrndx[sortord[j].si] < leftrndx[ii]) {
364 <                        ip->ra[ii][bd] = encode_radius(ip,
365 <                                        .5*(sortord[j].dm - sortord[i].dm));
364 >                        ip->da[ii].dia[bd] = encode_diameter(ip,
365 >                                                sortord[j].dm - sortord[i].dm);
366                          break;
367                      }
368                  for (j = i; j-- > 0; )          /* nearest below */
369                      if (rightrndx[sortord[j].si] < rightrndx[ii] &&
370                                      leftrndx[sortord[j].si] > leftrndx[ii]) {
371 <                        ip->ra[ii][bd+NI2DIR/2] = encode_radius(ip,
372 <                                        .5*(sortord[i].dm - sortord[j].dm));
371 >                        ip->da[ii].dia[bd+NI2DIR/2] = encode_diameter(ip,
372 >                                                sortord[i].dm - sortord[j].dm);
373                          break;
374                      }
375              }
376          }
377 <        free(sortord);                  /* clean up */
377 >        free(sortord);                  /* release temp arrays */
378          free(rightrndx);
379          free(leftrndx);
380          free(endrndx);
381 <        return(1);
381 >                                        /* map sample influence areas */
382 >        map_influence(ip);
383 >        return(1);                      /* all done */
384   }
385  
214 /* private call returns raw weight for a particular sample */
215 static double
216 get_wt(const INTERP2 *ip, const int i, double x, double y)
217 {
218        double  dir, rd, d2;
219        int     ri;
220                                /* get relative direction */
221        x -= ip->spt[i][0];
222        y -= ip->spt[i][1];
223        dir = atan2a(y, x);
224        dir += 2.*PI*(dir < 0);
225                                /* linear radius interpolation */
226        rd = dir * (NI2DIR/2./PI);
227        ri = (int)rd;
228        rd -= (double)ri;
229        rd = (1.-rd)*ip->ra[i][ri] + rd*ip->ra[i][(ri+1)%NI2DIR];
230        rd = ip->smf * DECODE_RAD(ip, rd);
231        d2 = x*x + y*y;
232                                /* Gaussian times harmonic weighting */
233        return( exp(d2/(-2.*rd*rd)) * ip->rmin/(ip->rmin + sqrt(d2)) );
234 }
235
386   /* Assign full set of normalized weights to interpolate the given position */
387   int
388   interp2_weights(float wtv[], INTERP2 *ip, double x, double y)
389   {
390          double  wnorm;
391 +        int     fgi[2];
392          int     i;
393  
394 <        if ((wtv == NULL) | (ip == NULL))
394 >        if (wtv == NULL)
395                  return(0);
396 <                                        /* need to compute interpolant? */
397 <        if (ip->ra == NULL && !interp2_analyze(ip))
396 >                                        /* get flag position */
397 >        if (interp2_flagpos(fgi, ip, x, y) < 0)
398                  return(0);
399  
400          wnorm = 0;                      /* compute raw weights */
401 <        for (i = ip->ns; i--; ) {
402 <                double  wt = get_wt(ip, i, x, y);
401 >        for (i = ip->ns; i--; )
402 >            if (chkflg(ip->da[i].infl, fgi[0], fgi[1])) {
403 >                double  wt = interp2_wti(ip, i, x, y);
404                  wtv[i] = wt;
405                  wnorm += wt;
406 <        }
406 >            } else
407 >                wtv[i] = 0;
408          if (wnorm <= 0)                 /* too far from all our samples! */
409                  return(0);
410          wnorm = 1./wnorm;               /* normalize weights */
# Line 266 | Line 419 | int
419   interp2_topsamp(float wt[], int si[], const int n, INTERP2 *ip, double x, double y)
420   {
421          int     nn = 0;
422 +        int     fgi[2];
423          double  wnorm;
424          int     i, j;
425  
426 <        if ((n <= 0) | (wt == NULL) | (si == NULL) | (ip == NULL))
426 >        if ((n <= 0) | (wt == NULL) | (si == NULL))
427                  return(0);
428 <                                        /* need to compute interpolant? */
429 <        if (ip->ra == NULL && !interp2_analyze(ip))
428 >                                        /* get flag position */
429 >        if (interp2_flagpos(fgi, ip, x, y) < 0)
430                  return(0);
431                                          /* identify top n weights */
432 <        for (i = ip->ns; i--; ) {
433 <                const double    wti = get_wt(ip, i, x, y);
432 >        for (i = ip->ns; i--; )
433 >            if (chkflg(ip->da[i].infl, fgi[0], fgi[1])) {
434 >                const double    wti = interp2_wti(ip, i, x, y);
435                  for (j = nn; j > 0; j--) {
436                          if (wt[j-1] >= wti)
437                                  break;
# Line 290 | Line 445 | interp2_topsamp(float wt[], int si[], const int n, INT
445                          si[j] = i;
446                          nn += (nn < n);
447                  }
448 <        }
448 >            }
449          wnorm = 0;                      /* normalize sample weights */
450          for (j = nn; j--; )
451                  wnorm += wt[j];
# Line 308 | Line 463 | interp2_free(INTERP2 *ip)
463   {
464          if (ip == NULL)
465                  return;
466 <        if (ip->ra != NULL)
467 <                free(ip->ra);
466 >        if (ip->da != NULL)
467 >                free(ip->da);
468          free(ip);
469   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines