contrib/mul/vil3d/vil3d_sample_profile_trilin.txx
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_sample_profile_trilin.txx
00002 #ifndef vil3d_sample_profile_trilin_txx_
00003 #define vil3d_sample_profile_trilin_txx_
00004 //:
00005 // \file
00006 // \brief Profile sampling functions for 3D images
00007 // \author Tim Cootes
00008 
00009 #include "vil3d_sample_profile_trilin.h"
00010 
00011 #include <vil3d/vil3d_trilin_interp.h>
00012 
00013 inline bool vil3d_profile_in_image(double x0, double y0, double z0,
00014                                    double x1, double y1,double z1,
00015                                    const vil3d_image_view_base& image)
00016 {
00017   if (x0<1) return false;
00018   if (y0<1) return false;
00019   if (z0<1) return false;
00020   if (x0+2>image.ni()) return false;
00021   if (y0+2>image.nj()) return false;
00022   if (z0+2>image.nk()) return false;
00023   if (x1<1) return false;
00024   if (y1<1) return false;
00025   if (z1<1) return false;
00026   if (x1+2>image.ni()) return false;
00027   if (y1+2>image.nj()) return false;
00028   if (z1+2>image.nk()) return false;
00029 
00030   return true;
00031 }
00032 
00033 //: Sample along profile, using safe trilinear interpolation
00034 //  Profile points are along the line between p0 and p1 (in image co-ordinates).
00035 //  Vector v is resized to n*np elements, where np=image.n_planes().
00036 //  v[0]..v[np-1] are the values from point p
00037 //  Points outside image return zero.
00038 template <class imType, class vecType>
00039 void vil3d_sample_profile_trilin(
00040   vecType* v,
00041   const vil3d_image_view<imType>& image,
00042   double x0, double y0, double z0,
00043   double dx, double dy, double dz,
00044   unsigned n)
00045 {
00046   bool all_in_image = vil3d_profile_in_image(x0,y0,z0,
00047                                              x0+(n-1)*dx,y0+(n-1)*dy,z0+(n-1)*dz,
00048                                              image);
00049 
00050   const unsigned ni = image.ni();
00051   const unsigned nj = image.nj();
00052   const unsigned nk = image.nk();
00053   const unsigned np = image.nplanes();
00054   const vcl_ptrdiff_t istep = image.istep();
00055   const vcl_ptrdiff_t jstep = image.jstep();
00056   const vcl_ptrdiff_t kstep = image.kstep();
00057   const vcl_ptrdiff_t pstep = image.planestep();
00058   double x=x0;
00059   double y=y0;
00060   double z=z0;
00061   const imType* plane0 = image.origin_ptr();
00062 
00063   if (all_in_image)
00064   {
00065     if (np==1)
00066     {
00067       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00068       v[h] = vil3d_trilin_interp_raw(x,y,z,plane0,istep,jstep,kstep);
00069     }
00070     else
00071     {
00072       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00073       {
00074         for (unsigned p=0;p<np;++p,++v)
00075           *v = vil3d_trilin_interp_raw(x,y,z,plane0+p*pstep,istep,jstep,kstep);
00076       }
00077     }
00078   }
00079   else
00080   {
00081     // Use safe interpolation
00082     if (np==1)
00083     {
00084       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00085       v[h] = vil3d_trilin_interp_safe(x,y,z,plane0,ni,nj,nk,istep,jstep,kstep);
00086     }
00087     else
00088     {
00089       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00090       {
00091         for (unsigned p=0;p<np;++p,++v)
00092           *v = vil3d_trilin_interp_safe(x,y,z,plane0+p*pstep,ni,nj,nk,istep,jstep,kstep);
00093       }
00094     }
00095   }
00096 }
00097 
00098 //: Sample along profile, using safe-extend trilinear interpolation
00099 //  Profile points are along the line between p0 and p1 (in image co-ordinates).
00100 //  Vector v is resized to n*np elements, where np=image.n_planes().
00101 //  v[0]..v[np-1] are the values from point p
00102 //  Points outside image return zero.
00103 template <class imType, class vecType>
00104 void vil3d_sample_profile_trilin_extend(
00105   vecType* v,
00106   const vil3d_image_view<imType>& image,
00107   double x0, double y0, double z0,
00108   double dx, double dy, double dz,
00109   unsigned n)
00110 {
00111   bool all_in_image = vil3d_profile_in_image(x0,y0,z0,
00112                                              x0+(n-1)*dx,y0+(n-1)*dy,z0+(n-1)*dz,
00113                                              image);
00114 
00115   const unsigned ni = image.ni();
00116   const unsigned nj = image.nj();
00117   const unsigned nk = image.nk();
00118   const unsigned np = image.nplanes();
00119   const vcl_ptrdiff_t istep = image.istep();
00120   const vcl_ptrdiff_t jstep = image.jstep();
00121   const vcl_ptrdiff_t kstep = image.kstep();
00122   const vcl_ptrdiff_t pstep = image.planestep();
00123   double x=x0;
00124   double y=y0;
00125   double z=z0;
00126   const imType* plane0 = image.origin_ptr();
00127 
00128   if (all_in_image)
00129   {
00130     if (np==1)
00131     {
00132       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00133       v[h] = vil3d_trilin_interp_raw(x,y,z,plane0,istep,jstep,kstep);
00134     }
00135     else
00136     {
00137       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00138       {
00139         for (unsigned p=0;p<np;++p,++v)
00140           *v = vil3d_trilin_interp_raw(x,y,z,plane0+p*pstep,istep,jstep,kstep);
00141       }
00142     }
00143   }
00144   else
00145   {
00146     // Use safe interpolation
00147     if (np==1)
00148     {
00149       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00150       v[h] = vil3d_trilin_interp_safe_extend(x,y,z,plane0,ni,nj,nk,istep,jstep,kstep);
00151     }
00152     else
00153     {
00154       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00155       {
00156         for (unsigned p=0;p<np;++p,++v)
00157           *v = vil3d_trilin_interp_safe_extend(x,y,z,plane0+p*pstep,ni,nj,nk,istep,jstep,kstep);
00158       }
00159     }
00160   }
00161 }
00162 
00163 //: Sample along profile, using safe-extend trilinear interpolation
00164 //  Profile points are along the line between p0 and p1 (in image co-ordinates).
00165 //  Vector v is resized to n*np elements, where np=image.n_planes().
00166 //  v[0]..v[np-1] are the values from point p
00167 //  Points outside image return NA.
00168 template <class imType, class vecType>
00169 void vil3d_sample_profile_trilin_edgena(
00170   vecType* v,
00171   const vil3d_image_view<imType>& image,
00172   double x0, double y0, double z0,
00173   double dx, double dy, double dz,
00174   unsigned n)
00175 {
00176   bool all_in_image = vil3d_profile_in_image(x0,y0,z0,
00177                                              x0+(n-1)*dx,y0+(n-1)*dy,z0+(n-1)*dz,
00178                                              image);
00179 
00180   const unsigned ni = image.ni();
00181   const unsigned nj = image.nj();
00182   const unsigned nk = image.nk();
00183   const unsigned np = image.nplanes();
00184   const vcl_ptrdiff_t istep = image.istep();
00185   const vcl_ptrdiff_t jstep = image.jstep();
00186   const vcl_ptrdiff_t kstep = image.kstep();
00187   const vcl_ptrdiff_t pstep = image.planestep();
00188   double x=x0;
00189   double y=y0;
00190   double z=z0;
00191   const imType* plane0 = image.origin_ptr();
00192 
00193   if (all_in_image)
00194   {
00195     if (np==1)
00196     {
00197       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00198       v[h] = vil3d_trilin_interp_raw(x,y,z,plane0,istep,jstep,kstep);
00199     }
00200     else
00201     {
00202       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00203       {
00204         for (unsigned p=0;p<np;++p,++v)
00205           *v = vil3d_trilin_interp_raw(x,y,z,plane0+p*pstep,istep,jstep,kstep);
00206       }
00207     }
00208   }
00209   else
00210   {
00211     // Use safe interpolation
00212     if (np==1)
00213     {
00214       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00215       v[h] = vil3d_trilin_interp_safe_edgena(x,y,z,plane0,ni,nj,nk,istep,jstep,kstep);
00216     }
00217     else
00218     {
00219       for (unsigned h=0;h<n;++h,x+=dx,y+=dy,z+=dz)
00220       {
00221         for (unsigned p=0;p<np;++p,++v)
00222           *v = vil3d_trilin_interp_safe_edgena(x,y,z,plane0+p*pstep,ni,nj,nk,istep,jstep,kstep);
00223       }
00224     }
00225   }
00226 }
00227 
00228 #define VIL3D_SAMPLE_PROFILE_TRILIN_INSTANTIATE( imType, vecType ) \
00229 template void vil3d_sample_profile_trilin(vecType* v, \
00230   const vil3d_image_view<imType >& image, \
00231   double x0, double y0, double z0,\
00232   double dx, double dy, double dz,\
00233   unsigned n); \
00234 template void vil3d_sample_profile_trilin_extend(vecType* v, \
00235   const vil3d_image_view<imType >& image, \
00236   double x0, double y0, double z0,\
00237   double dx, double dy, double dz,\
00238   unsigned n); \
00239 template void vil3d_sample_profile_trilin_edgena(vecType* v, \
00240   const vil3d_image_view<imType >& image, \
00241   double x0, double y0, double z0,\
00242   double dx, double dy, double dz,\
00243   unsigned n)
00244 #endif // vil3d_sample_profile_trilin_txx_