contrib/mul/vil3d/vil3d_tricub_interp.h
Go to the documentation of this file.
00001 #ifndef vil3d_tricub_interp_h_
00002 #define vil3d_tricub_interp_h_
00003 //:
00004 // \file
00005 // \brief Tricubic interpolation functions for 3D images
00006 //
00007 #include <vcl_cassert.h>
00008 #include <vcl_cstddef.h>
00009 #include <vil3d/vil3d_image_view.h>
00010 #include <vil3d/vil3d_trilin_interp.h>
00011 
00012 //: Compute tricubic interpolation at (x,y,z), no bound checks
00013 //  Image is nx * ny * nz array of T. x,y,z element is data[z*zstep+ystep*y+x*xstep]
00014 //  No bound checks are done.
00015 template<class T>
00016 double vil3d_tricub_interp_raw(double x, double y, double z, const T* data,
00017                                vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep);
00018 
00019 //: Compute tricubic interpolation at (x,y,z), with bound checks
00020 //  Image is nx * ny * nz array of T. x,y,z element is data[z*zstep+ystep*y+x*xstep]
00021 //  If (x,y,z) is outside interpolatable image region, returns zero or \a outval
00022 //  The safe interpolatable region is [1,nx-3]*[1,ny-3]*[1,nz-3].
00023 template<class T>
00024 inline double vil3d_tricub_interp_safe(double x, double y, double z, const T* data,
00025                                        int nx, int ny, int nz,
00026                                        vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep,
00027                                        T outval=0)
00028 {
00029     if (x<1) return static_cast<double>(outval);
00030     if (y<1) return static_cast<double>(outval);
00031     if (z<1) return static_cast<double>(outval);
00032     if (x>nx-3) return static_cast<double>(outval);
00033     if (y>ny-3) return static_cast<double>(outval);
00034     if (z>nz-3) return static_cast<double>(outval);
00035     return vil3d_tricub_interp_raw(x,y,z,data,xstep,ystep,zstep);
00036 }
00037 
00038 //: Compute tricubic interpolation at (x,y,z), with minimal bound checks
00039 //  Image is nx * ny * nz array of Ts. x,y,z element is data[ystep*y+xstep*x]
00040 //  If (x,y,z) is outside interpolatable image region and NDEBUG is not defined
00041 //  the code will fail an ASSERT.
00042 //  The safe interpolatable region is [1,nx-3]*[1,ny-3]*[1,nz-3].
00043 template<class T>
00044 inline double vil3d_tricub_interp_assert(double x, double y, double z, const T* data,
00045                                          int nx, int ny, int nz,
00046                                          vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep)
00047 {
00048     assert (x>=1);
00049     assert (y>=1);
00050     assert (z>=1);
00051     assert (x<=nx-3);
00052     assert (y<=ny-3);
00053     assert (z<=nz-3);
00054     return vil3d_tricub_interp_raw(x,y,z,data,xstep,ystep,zstep);
00055 }
00056 
00057 //: Compute tricubic interpolation at (x,y,z), with bound checks
00058 //  Image is nx * ny array of Ts. x,y element is data[nx*y+x]
00059 //  If (x,y,z) is outside safe interpolatable image region, nearest pixel value is returned.
00060 //  The safe interpolatable region is [1,nx-3]*[1,ny-3]*[1,nz-3].
00061 template<class T>
00062 inline double vil3d_tricub_interp_safe_extend(double x, double y, double z,
00063                                               const T* data,
00064                                               int nx, int ny, int nz,
00065                                               vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep)
00066 {
00067   if (x<0.9999999) x= 0.0;
00068   else if (x>nx-3.0000001 && x<nx-1.9999999) x=nx-2.0;
00069   else if (x>nx-2.0000001) x=nx-1.0;
00070 
00071   if (y<0.9999999) y= 0.0;
00072   else if (y>ny-3.0000001 && y<ny-1.9999999) y=ny-2.0;
00073   else if (y>ny-2.0000001) y=ny-1.0;
00074 
00075   if (z<0.9999999) z= 0.0;
00076   else if (z>nz-3.0000001 && z<nz-1.9999999) z=nz-2.0;
00077   else if (z>nz-2.0000001) z=nz-1.0;
00078 
00079   return vil3d_tricub_interp_raw(x,y,z,data,xstep,ystep,zstep);
00080 }
00081 
00082 //: Compute tricubic interpolation at (x,y,z), with bound checks
00083 //  Image is nx * ny array of Ts. x,y element is data[nx*y+x]
00084 //  If (x,y,z) is outside safe interpolatable image region, trilinear interpolated value of the nearest valid pixels is returned.
00085 //  The safe interpolatable region is [1,nx-3]*[1,ny-3]*[1,nz-3].
00086 template<class T>
00087 double vil3d_tricub_interp_safe_trilinear_extend(double x, double y, double z,
00088                                                  const T* data,
00089                                                  int nx, int ny, int nz,
00090                                                  vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep);
00091 
00092 
00093 // //: Compute tricubic interpolation at (x,y,z), with bound checks
00094 // //  If (x,y,z) is outside safe interpolatable image region, nearest pixel value is returned.
00095 // //  The safe interpolatable region is [1,view.ni()-2]*[1,view.nj()-2]*[1,view.nk()-2].
00096 // // \relatesalso vil3d_image_view
00097 // template<class T>
00098 // inline double vil3d_tricub_interp_safe_extend(const vil3d_image_view<T> &view,
00099 //                                               double x, double y, double z, unsigned p=0)
00100 // {
00101 //   return vil3d_tricub_interp_safe_extend(x, y, z, &view(0,0,p),
00102 //                                          view.ni(), view.nj(), view.nk(),
00103 //                                          view.istep(), view.jstep(), view.kstep() );
00104 // }
00105 
00106 #endif // vil3d_tricub_interp_h_