contrib/mul/vil3d/vil3d_trilin_interp.h
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_trilin_interp.h
00002 #ifndef vil3d_trilin_interp_h_
00003 #define vil3d_trilin_interp_h_
00004 //:
00005 // \file
00006 // \brief Trilinear interpolation functions for 3D images
00007 // \author Tim Cootes
00008 
00009 #include <vcl_cassert.h>
00010 #include <vcl_cstddef.h>
00011 #include <vil/vil_na.h>
00012 #include <vil3d/vil3d_image_view.h>
00013 
00014 //: Compute trilinear interpolation at (x,y,z), no bound checks
00015 //  Image is nx * ny * nz array of T. x,y,z element is data[z*zstep+ystep*y+x*xstep]
00016 //  No bound checks are done.
00017 template<class T>
00018 inline double vil3d_trilin_interp_raw(double x, double y, double z,
00019                                       const T* data, vcl_ptrdiff_t xstep,
00020                                       vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep)
00021 {
00022   int p1x,p1y,p1z;
00023   double normx,normy,normz;
00024   p1x=int(x);
00025   normx = x-p1x;
00026   p1y=int(y);
00027   normy = y-p1y;
00028   p1z=int(z);
00029   normz = z-p1z;
00030 
00031   const T* row11 = data + p1z*zstep+p1y*ystep + p1x*xstep;
00032   const T* row21 = row11 + ystep;
00033   const T* row12 = row11 + zstep;
00034   const T* row22 = row21 + zstep;
00035 
00036   // Bilinear interpolation in plane z=p1z
00037   double i11 = (double)row11[0]+(double)(row21[0]-row11[0])*normy;
00038   double i21 = (double)row11[xstep]+(double)(row21[xstep]-row11[xstep])*normy;
00039   double iz1 = i11+(i21-i11)*normx;
00040 
00041   // Bilinear interpolation in plane z=p1z+1
00042   double i12 = (double)row12[0]+(double)(row22[0]-row12[0])*normy;
00043   double i22 = (double)row12[xstep]+(double)(row22[xstep]-row12[xstep])*normy;
00044   double iz2 = i12+(i22-i12)*normx;
00045 
00046   return iz1+(iz2-iz1)*normz;
00047 }
00048 
00049 
00050 //: Compute trilinear interpolation at (x,y,z), with bound checks
00051 //  Image is nx * ny * nz array of T. x,y,z element is data[z*zstep+ystep*y+x*xstep]
00052 //  If (x,y,z) is outside interpolatable image region, returns zero or \a outval
00053 template<class T>
00054 inline double vil3d_trilin_interp_safe(double x, double y, double z, const T* data,
00055                                        unsigned nx, unsigned ny, unsigned nz,
00056                                        vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep,
00057                                        double outval=0)
00058 {
00059   if (x<0) return static_cast<double>(outval);
00060   if (y<0) return static_cast<double>(outval);
00061   if (z<0) return static_cast<double>(outval);
00062   if (x>=nx-1) return static_cast<double>(outval);
00063   if (y>=ny-1) return static_cast<double>(outval);
00064   if (z>=nz-1) return static_cast<double>(outval);
00065   return vil3d_trilin_interp_raw(x,y,z,data,xstep,ystep,zstep);
00066 }
00067 
00068 //: Compute trilinear interpolation at (x,y,z,p), with bound checks
00069 //  Image is nx * ny * nz array of T. x,y,z element is data[z*zstep+ystep*y+x*xstep]
00070 //  If (x,y,z) is outside interpolatable image region, returns zero or \a outval
00071 template<class T>
00072 inline double vil3d_trilin_interp_safe(const vil3d_image_view<T>& image,
00073                                        double x, double y, double z,
00074                                        unsigned p=0,
00075                                        T outval=0)
00076 {
00077   return vil3d_trilin_interp_safe(x,y,z,&image(0,0,0,p),
00078                                   image.ni(),image.nj(),image.nk(),
00079                                   image.istep(),image.jstep(),image.kstep(),
00080                                   outval);
00081 }
00082 
00083 //: Compute trilinear interpolation at (x,y), with bound checks
00084 //  Image is nx * ny * nz array of Ts. x,y element is data[nx*y+x]
00085 //  If (x,y) is outside interpolatable image region and NDEBUG is not defined
00086 //  the code will fail an ASSERT.
00087 //  The safe interpolatable region is [0,nx)*[0,ny)*[0,nz].
00088 template<class T>
00089 inline double vil3d_trilin_interp_assert(double x, double y, double z, const T* data,
00090                                          unsigned nx, unsigned ny, unsigned nz,
00091                                          vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep)
00092 {
00093   assert(x>=0);
00094   assert(y>=0);
00095   assert(z>=0);
00096   assert(x<nx-1);
00097   assert(y<ny-1);
00098   assert(z<nz-1);
00099   return vil3d_trilin_interp_raw(x,y,z,data,xstep,ystep,zstep);
00100 }
00101 
00102 //: Compute trilinear interpolation at (x,y), with bounds checks.
00103 //  Image is nx * ny array of Ts. x,y element is data[nx*y+x]
00104 //  If (x,y,z) is outside safe interpolatable image region, nearest pixel value is returned.
00105 template<class T>
00106 inline double vil3d_trilin_interp_safe_extend(double x, double y, double z, const T* data,
00107                                               unsigned nx, unsigned ny, unsigned nz,
00108                                               vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep)
00109 {
00110   if (x<0) x= 0.0;
00111   if (y<0) y= 0.0;
00112   if (z<0) z= 0.0;
00113   if (x>=nx-1) x=(double)nx-1.00000001;
00114   if (y>=ny-1) y=(double)ny-1.00000001;
00115   if (z>=nz-1) z=(double)nz-1.00000001;
00116   return vil3d_trilin_interp_raw(x,y,z,data,xstep,ystep,zstep);
00117 }
00118 
00119 //: Compute trilinear interpolation at (x,y), with bounds checks.
00120 //  Image is nx * ny array of Ts. x,y element is data[nx*y+x]
00121 //  If (x,y,z) is outside safe interpolatable image region, NA is returned.
00122 template<class T>
00123 inline double vil3d_trilin_interp_safe_edgena(double x, double y, double z, const T* data,
00124                                               unsigned nx, unsigned ny, unsigned nz,
00125                                               vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep)
00126 {
00127   if (x<0 || y<0 || z<0 ||
00128     x>=nx-1 || y>=ny-1 || z>=nz-1) return vil_na(double());
00129   return vil3d_trilin_interp_raw(x,y,z,data,xstep,ystep,zstep);
00130 }
00131 //: Compute trilinear interpolation at (x,y), using the nearest valid value if out of bounds
00132 //  If (x,y,z) is outside safe interpolatable image region, nearest pixel value is returned.
00133 template<class T>
00134 inline double vil3d_trilin_interp_safe_extend(const vil3d_image_view<T>& image,
00135                                               double x, double y, double z,
00136                                               unsigned p=0)
00137 {
00138   return vil3d_trilin_interp_safe_extend(x, y, z, &image(0,0,0,p),
00139     image.ni(), image.nj(), image.nk(),
00140     image.istep(), image.jstep(), image.kstep());
00141 }
00142 
00143 
00144 #endif // vil3d_trilin_interp_h_