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.10 by greg, Thu Feb 14 19:57:10 2013 UTC vs.
Revision 2.13 by greg, Sat Feb 16 00:41:12 2013 UTC

# Line 23 | Line 23 | static const char RCSid[] = "$Id$";
23   * to reduce the influence of distant neighbors.  This yields a
24   * smooth interpolation regardless of how the sample points are
25   * initially distributed.  Evaluation is accelerated by use of
26 < * a fast approximation to the atan2(y,x) function and an array
27 < * of flags indicating where weights are (nearly) zero.
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>
# Line 41 | 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 100 | Line 113 | interp2_spacing(INTERP2 *ip, double mind)
113          ip->dmin = mind;
114   }
115  
116 + /* Compute unnormalized weight for a position relative to a sample */
117 + double
118 + interp2_wti(INTERP2 *ip, const int i, double x, double y)
119 + {
120 +        double          dir, rd, r2, d2;
121 +        int             ri;
122 +                                /* get relative direction */
123 +        x -= ip->spt[i][0];
124 +        y -= ip->spt[i][1];
125 +        dir = atan2a(y, x);
126 +        dir += 2.*PI*(dir < 0);
127 +                                /* linear radius interpolation */
128 +        rd = dir * (NI2DIR/2./PI);
129 +        ri = (int)rd;
130 +        rd -= (double)ri;
131 +        rd = (1.-rd)*ip->da[i].dia[ri] + rd*ip->da[i].dia[(ri+1)%NI2DIR];
132 +        rd = ip->smf * DECODE_DIA(ip, rd);
133 +        r2 = 2.*rd*rd;
134 +        d2 = x*x + y*y;
135 +        if (d2 > 21.*r2)        /* result would be < 1e-9 */
136 +                return(.0);
137 +                                /* Gaussian times harmonic weighting */
138 +        return( exp(-d2/r2) * ip->dmin/(ip->dmin + sqrt(d2)) );
139 + }
140 +
141 + /* private call to get grid flag index */
142 + static int
143 + interp2_flagpos(int fgi[2], INTERP2 *ip, double x, double y)
144 + {
145 +        int     inbounds = 0;
146 +
147 +        if (ip == NULL)         /* paranoia */
148 +                return(-1);
149 +                                /* need to compute interpolant? */
150 +        if (ip->da == NULL && !interp2_analyze(ip))
151 +                return(-1);
152 +                                /* get x & y grid positions */
153 +        fgi[0] = (x - ip->smin[0]) * NI2DIM / (ip->smax[0] - ip->smin[0]);
154 +
155 +        if (fgi[0] >= NI2DIM)
156 +                fgi[0] = NI2DIM-1;
157 +        else if (fgi[0] < 0)
158 +                fgi[0] = 0;
159 +        else
160 +                ++inbounds;
161 +
162 +        fgi[1] = (y - ip->smin[1]) * NI2DIM / (ip->smax[1] - ip->smin[1]);
163 +
164 +        if (fgi[1] >= NI2DIM)
165 +                fgi[1] = NI2DIM-1;
166 +        else if (fgi[1] < 0)
167 +                fgi[1] = 0;
168 +        else
169 +                ++inbounds;
170 +
171 +        return(inbounds == 2);
172 + }
173 +
174 + #define setflg(fl,xi,yi)        ((fl)[yi] |= 1<<(xi))
175 +
176 + #define chkflg(fl,xi,yi)        ((fl)[yi]>>(xi) & 1)
177 +
178 + /* private flood function to determine sample influence */
179 + static void
180 + influence_flood(INTERP2 *ip, const int i, unsigned short visited[NI2DIM],
181 +                        int xfi, int yfi)
182 + {
183 +        double  gx = (xfi+.5)*(1./NI2DIM)*(ip->smax[0] - ip->smin[0]) +
184 +                                        ip->smin[0];
185 +        double  gy = (yfi+.5)*(1./NI2DIM)*(ip->smax[1] - ip->smin[1]) +
186 +                                        ip->smin[1];
187 +        double  dx = gx - ip->spt[i][0];
188 +        double  dy = gy - ip->spt[i][1];
189 +
190 +        setflg(visited, xfi, yfi);
191 +
192 +        if (dx*dx + dy*dy > 2.*ip->grid2 && interp2_wti(ip, i, gx, gy) <= 1e-7)
193 +                return;
194 +
195 +        setflg(ip->da[i].infl, xfi, yfi);
196 +
197 +        if (xfi > 0 && !chkflg(visited, xfi-1, yfi))
198 +                influence_flood(ip, i, visited, xfi-1, yfi);
199 +
200 +        if (yfi > 0 && !chkflg(visited, xfi, yfi-1))
201 +                influence_flood(ip, i, visited, xfi, yfi-1);
202 +
203 +        if (xfi < NI2DIM-1 && !chkflg(visited, xfi+1, yfi))
204 +                influence_flood(ip, i, visited, xfi+1, yfi);
205 +
206 +        if (yfi < NI2DIM-1 && !chkflg(visited, xfi, yfi+1))
207 +                influence_flood(ip, i, visited, xfi, yfi+1);
208 + }
209 +
210 + /* private call to compute sample influence maps */
211 + static void
212 + map_influence(INTERP2 *ip)
213 + {
214 +        unsigned short  visited[NI2DIM];
215 +        int             fgi[2];
216 +        int             i, j;
217 +
218 +        for (i = ip->ns; i--; ) {
219 +                for (j = NI2DIM; j--; ) {
220 +                        ip->da[i].infl[j] = 0;
221 +                        visited[j] = 0;
222 +                }
223 +                interp2_flagpos(fgi, ip, ip->spt[i][0], ip->spt[i][1]);
224 +
225 +                influence_flood(ip, i, visited, fgi[0], fgi[1]);
226 +        }
227 + }
228 +
229   /* Modify smoothing parameter by the given factor */
230   void
231   interp2_smooth(INTERP2 *ip, double sf)
232   {
233 +        float   old_smf = ip->smf;
234 +
235          if ((ip->smf *= sf) < NI2DSMF)
236                  ip->smf = NI2DSMF;
237 +                                        /* need to recompute influence maps? */
238 +        if (ip->da != NULL && (old_smf*.85 > ip->smf) |
239 +                                (ip->smf > old_smf*1.15))
240 +                map_influence(ip);
241   }
242  
243   /* private call-back to sort position index */
# Line 137 | Line 269 | sort_samples(SAMPORD *sord, const INTERP2 *ip, double
269          qsort(sord, ip->ns, sizeof(SAMPORD), &cmp_spos);
270   }
271  
140 /* private routine to encode sample diameter with range checks */
141 static int
142 encode_diameter(const INTERP2 *ip, double d)
143 {
144        const int       ed = ENCODE_DIA(ip, d);
145
146        if (ed <= 0)
147                return(0);
148        if (ed >= 0xffff)
149                return(0xffff);
150        return(ed);
151 }
152
272   /* (Re)compute anisotropic basis function interpolant (normally automatic) */
273   int
274   interp2_analyze(INTERP2 *ip)
# Line 168 | Line 287 | interp2_analyze(INTERP2 *ip)
287                  return(0);
288                                          /* compute sample domain */
289          ip->smin[0] = ip->smin[1] = FHUGE;
290 <        ip->smul[0] = ip->smul[1] = -FHUGE;
290 >        ip->smax[0] = ip->smax[1] = -FHUGE;
291          for (i = ip->ns; i--; ) {
292 <                if (ip->spt[i][0] < ip->smin[0])
293 <                        ip->smin[0] = ip->spt[i][0];
294 <                if (ip->spt[i][0] > ip->smul[0])
295 <                        ip->smul[0] = ip->spt[i][0];
177 <                if (ip->spt[i][1] < ip->smin[1])
178 <                        ip->smin[1] = ip->spt[i][1];
179 <                if (ip->spt[i][1] > ip->smul[1])
180 <                        ip->smul[1] = ip->spt[i][1];
292 >                if (ip->spt[i][0] < ip->smin[0]) ip->smin[0] = ip->spt[i][0];
293 >                if (ip->spt[i][0] > ip->smax[0]) ip->smax[0] = ip->spt[i][0];
294 >                if (ip->spt[i][1] < ip->smin[1]) ip->smin[1] = ip->spt[i][1];
295 >                if (ip->spt[i][1] > ip->smax[1]) ip->smax[1] = ip->spt[i][1];
296          }
297 <        ip->smul[0] -= ip->smin[0];
298 <        ip->smul[1] -= ip->smin[1];
299 <        ip->grid2 = (ip->smul[0]*ip->smul[0] + ip->smul[1]*ip->smul[1]) *
185 <                        (4./NI2DIM/NI2DIM);
297 >        ip->grid2 = ((ip->smax[0]-ip->smin[0])*(ip->smax[0]-ip->smin[0]) +
298 >                        (ip->smax[1]-ip->smin[1])*(ip->smax[1]-ip->smin[1])) *
299 >                                (1./NI2DIM/NI2DIM);
300          if (ip->grid2 <= FTINY*ip->dmin*ip->dmin)
301                  return(0);
188        if (ip->smul[0] > FTINY)
189                ip->smul[0] = NI2DIM / ip->smul[0];
190        if (ip->smul[1] > FTINY)
191                ip->smul[1] = NI2DIM / ip->smul[1];
302                                          /* allocate analysis data */
303 <        ip->da = (struct interp2_samp *)calloc( ip->ns,
304 <                                        sizeof(struct interp2_samp) );
303 >        ip->da = (struct interp2_samp *)malloc(
304 >                                sizeof(struct interp2_samp)*ip->ns );
305          if (ip->da == NULL)
306                  return(0);
307 <                                        /* get temporary arrays */
307 >                                        /* allocate temporary arrays */
308          sortord = (SAMPORD *)malloc(sizeof(SAMPORD)*ip->ns);
309          rightrndx = (int *)malloc(sizeof(int)*ip->ns);
310          leftrndx = (int *)malloc(sizeof(int)*ip->ns);
# Line 255 | Line 365 | interp2_analyze(INTERP2 *ip)
365                      }
366              }
367          }
368 <        free(sortord);                  /* clean up */
368 >        free(sortord);                  /* release temp arrays */
369          free(rightrndx);
370          free(leftrndx);
371          free(endrndx);
372 <        return(1);
372 >                                        /* map sample influence areas */
373 >        map_influence(ip);
374 >        return(1);                      /* all done */
375   }
376  
265 /* Compute unnormalized weight for a position relative to a sample */
266 double
267 interp2_wti(INTERP2 *ip, const int i, double x, double y)
268 {
269        int     xfi, yfi;
270        double  dir, rd, r2, d2;
271        int     ri;
272                                /* need to compute interpolant? */
273        if (ip->da == NULL && !interp2_analyze(ip))
274                return(0);
275                                /* get grid position */
276        xfi = (x - ip->smin[0]) * ip->smul[0];
277        if (xfi >= NI2DIM)
278                xfi = NI2DIM-1;
279        else
280                xfi *= (xfi >= 0);
281        yfi = (y - ip->smin[1]) * ip->smul[1];
282        if (yfi >= NI2DIM)
283                yfi = NI2DIM-1;
284        else
285                yfi *= (yfi >= 0);
286        x -= ip->spt[i][0];     /* check distance */
287        y -= ip->spt[i][1];
288        d2 = x*x + y*y;
289                                /* zero weight this zone? */
290        if (d2 > ip->grid2 && ip->da[i].blkflg[yfi] & 1<<xfi)
291                return(.0);
292
293        dir = atan2a(y, x);     /* get relative direction */
294        dir += 2.*PI*(dir < 0);
295                                /* linear radius interpolation */
296        rd = dir * (NI2DIR/2./PI);
297        ri = (int)rd;
298        rd -= (double)ri;
299        rd = (1.-rd)*ip->da[i].dia[ri] + rd*ip->da[i].dia[(ri+1)%NI2DIR];
300        rd = ip->smf * DECODE_DIA(ip, rd);
301        r2 = 2.*rd*rd;
302        if (d2 > 21.*r2) {      /* result would be < 1e-9 */
303                ip->da[i].blkflg[yfi] |= 1<<xfi;
304                return(.0);
305        }
306                                /* Gaussian times harmonic weighting */
307        return( exp(-d2/r2) * ip->dmin/(ip->dmin + sqrt(d2)) );
308 }
309
377   /* Assign full set of normalized weights to interpolate the given position */
378   int
379   interp2_weights(float wtv[], INTERP2 *ip, double x, double y)
380   {
381          double  wnorm;
382 +        int     fgi[2];
383          int     i;
384  
385 <        if ((wtv == NULL) | (ip == NULL))
385 >        if (wtv == NULL)
386                  return(0);
387 +                                        /* get flag position */
388 +        if (interp2_flagpos(fgi, ip, x, y) < 0)
389 +                return(0);
390  
391          wnorm = 0;                      /* compute raw weights */
392 <        for (i = ip->ns; i--; ) {
392 >        for (i = ip->ns; i--; )
393 >            if (chkflg(ip->da[i].infl, fgi[0], fgi[1])) {
394                  double  wt = interp2_wti(ip, i, x, y);
395                  wtv[i] = wt;
396                  wnorm += wt;
397 <        }
397 >            } else
398 >                wtv[i] = 0;
399          if (wnorm <= 0)                 /* too far from all our samples! */
400                  return(0);
401          wnorm = 1./wnorm;               /* normalize weights */
# Line 337 | Line 410 | int
410   interp2_topsamp(float wt[], int si[], const int n, INTERP2 *ip, double x, double y)
411   {
412          int     nn = 0;
413 +        int     fgi[2];
414          double  wnorm;
415          int     i, j;
416  
417 <        if ((n <= 0) | (wt == NULL) | (si == NULL) | (ip == NULL))
417 >        if ((n <= 0) | (wt == NULL) | (si == NULL))
418                  return(0);
419 +                                        /* get flag position */
420 +        if (interp2_flagpos(fgi, ip, x, y) < 0)
421 +                return(0);
422                                          /* identify top n weights */
423 <        for (i = ip->ns; i--; ) {
423 >        for (i = ip->ns; i--; )
424 >            if (chkflg(ip->da[i].infl, fgi[0], fgi[1])) {
425                  const double    wti = interp2_wti(ip, i, x, y);
348                if (wti <= 1e-9)
349                        continue;
426                  for (j = nn; j > 0; j--) {
427                          if (wt[j-1] >= wti)
428                                  break;
# Line 360 | Line 436 | interp2_topsamp(float wt[], int si[], const int n, INT
436                          si[j] = i;
437                          nn += (nn < n);
438                  }
439 <        }
439 >            }
440          wnorm = 0;                      /* normalize sample weights */
441          for (j = nn; j--; )
442                  wnorm += wt[j];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines