00001
00002 #ifndef vil3d_trilin_interp_h_
00003 #define vil3d_trilin_interp_h_
00004
00005
00006
00007
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
00015
00016
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
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
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
00051
00052
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
00069
00070
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
00084
00085
00086
00087
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
00103
00104
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
00120
00121
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
00132
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_