contrib/mul/vil3d/vil3d_slice.h
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_slice.h
00002 #ifndef vil3d_slice_h_
00003 #define vil3d_slice_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Tim Cootes.
00010 
00011 #include <vil/vil_image_view.h>
00012 #include <vil3d/vil3d_image_view.h>
00013 #include <vcl_cassert.h>
00014 
00015 //: Return a 2D view of slice k of 3D image aligned as (j,i).
00016 //  result(x,y,p)=im(y,x,k,p)
00017 // \relatesalso vil3d_image_view
00018 // \relatesalso vil_image_view
00019 template <class T>
00020 inline vil_image_view<T> vil3d_slice_ji(const vil3d_image_view<T> &im, unsigned k)
00021 {
00022   assert(k<im.nk());
00023   // Tweak ensure contiguous check works for 1 plane images
00024   vcl_ptrdiff_t pstep = im.planestep();
00025   if (im.nplanes()==1) pstep=im.ni()*im.nj();
00026   return vil_image_view<T>(im.memory_chunk(),
00027                            im.origin_ptr()+k*im.kstep(),
00028                            im.nj(), im.ni(), im.nplanes(),
00029                            im.jstep(), im.istep(), pstep);
00030 }
00031 
00032 //: Return a 2D view of slice k of 3D image aligned as (i,j).
00033 //  result(x,y,p)=im(x,y,k,p)
00034 // \relatesalso vil3d_image_view
00035 // \relatesalso vil_image_view
00036 template <class T>
00037 inline vil_image_view<T> vil3d_slice_ij(const vil3d_image_view<T> &im, unsigned k)
00038 {
00039   assert(k<im.nk());
00040   // Tweak ensure contiguous check works for 1 plane images
00041   vcl_ptrdiff_t pstep = im.planestep();
00042   if (im.nplanes()==1) pstep=im.ni()*im.nj();
00043   return vil_image_view<T>(im.memory_chunk(),
00044                            im.origin_ptr()+k*im.kstep(),
00045                            im.ni(), im.nj(), im.nplanes(),
00046                            im.istep(), im.jstep(), pstep);
00047 }
00048 
00049 //: Return a 2D view of slice i of 3D image aligned as (j,k).
00050 //  result(x,y,p)=im(i,x,y,p)
00051 // \relatesalso vil3d_image_view
00052 // \relatesalso vil_image_view
00053 template <class T>
00054 inline vil_image_view<T> vil3d_slice_jk(const vil3d_image_view<T> &im, unsigned i)
00055 {
00056   assert(i<im.ni());
00057   // Tweak ensure contiguous check works for 1 plane images
00058   vcl_ptrdiff_t pstep = im.planestep();
00059   if (im.nplanes()==1) pstep=im.nj()*im.nk();
00060   return vil_image_view<T>(im.memory_chunk(),
00061                            im.origin_ptr()+i*im.istep(),
00062                            im.nj(), im.nk(), im.nplanes(),
00063                            im.jstep(), im.kstep(), pstep);
00064 }
00065 
00066 //: Return a 2D view of slice i of 3D image aligned as (k,j).
00067 //  result(x,y,p)=im(i,y,x,p)
00068 // \relatesalso vil3d_image_view
00069 // \relatesalso vil_image_view
00070 template <class T>
00071 inline vil_image_view<T> vil3d_slice_kj(const vil3d_image_view<T> &im, unsigned i)
00072 {
00073   assert(i<im.ni());
00074   // Tweak ensure contiguous check works for 1 plane images
00075   vcl_ptrdiff_t pstep = im.planestep();
00076   if (im.nplanes()==1) pstep=im.nj()*im.nk();
00077   return vil_image_view<T>(im.memory_chunk(),
00078                            im.origin_ptr()+i*im.istep(),
00079                            im.nk(), im.nj(), im.nplanes(),
00080                            im.kstep(), im.jstep(), pstep);
00081 }
00082 
00083 //: Return a 2D view of slice j of 3D image aligned as (k,i).
00084 //  result(x,y,p)=im(y,i,x,p)
00085 // \relatesalso vil3d_image_view
00086 // \relatesalso vil_image_view
00087 template <class T>
00088 inline vil_image_view<T> vil3d_slice_ki(const vil3d_image_view<T> &im, unsigned j)
00089 {
00090   assert(j<im.nj());
00091   // Tweak ensure contiguous check works for 1 plane images
00092   vcl_ptrdiff_t pstep = im.planestep();
00093   if (im.nplanes()==1) pstep=im.ni()*im.nk();
00094   return vil_image_view<T>(im.memory_chunk(),
00095                            im.origin_ptr()+j*im.jstep(),
00096                            im.nk(), im.ni(), im.nplanes(),
00097                            im.kstep(), im.istep(), pstep);
00098 }
00099 
00100 //: Return a 2D view of slice j of 3D image aligned as (i,k).
00101 //  result(x,y,p)=im(x,i,y,p)
00102 // \relatesalso vil3d_image_view
00103 // \relatesalso vil_image_view
00104 template <class T>
00105 inline vil_image_view<T> vil3d_slice_ik(const vil3d_image_view<T> &im, unsigned j)
00106 {
00107   assert(j<im.nj());
00108   // Tweak ensure contiguous check works for 1 plane images
00109   vcl_ptrdiff_t pstep = im.planestep();
00110   if (im.nplanes()==1) pstep=im.ni()*im.nk();
00111   return vil_image_view<T>(im.memory_chunk(),
00112                            im.origin_ptr()+j*im.jstep(),
00113                            im.ni(), im.nk(), im.nplanes(),
00114                            im.istep(), im.kstep(), pstep);
00115 }
00116 
00117 //: Define format of a slice from a 3D volume
00118 enum vil3d_slice_format
00119 {
00120   VIL3D_SLICE_FORMAT_IJ=0,
00121   VIL3D_SLICE_FORMAT_JI=1,
00122   VIL3D_SLICE_FORMAT_IK=2,
00123   VIL3D_SLICE_FORMAT_KI=3,
00124   VIL3D_SLICE_FORMAT_JK=4,
00125   VIL3D_SLICE_FORMAT_KJ=5
00126 };
00127 
00128 //: Return a 2D view of slice of a 3D image aligned as defined by slice_format.
00129 //  Thus if slice_format==VIL3D_SLICE_FORMAT_IJ then i-j slice returned at z=slice_index.
00130 // \relatesalso vil3d_image_view
00131 // \relatesalso vil_image_view
00132 template <class T>
00133 inline vil_image_view<T> vil3d_slice(const vil3d_image_view<T> &im, unsigned slice_index,
00134                                      vil3d_slice_format slice_format)
00135 {
00136   switch (slice_format)
00137   {
00138     case VIL3D_SLICE_FORMAT_IJ: return vil3d_slice_ij(im,slice_index);
00139     case VIL3D_SLICE_FORMAT_JI: return vil3d_slice_ji(im,slice_index);
00140     case VIL3D_SLICE_FORMAT_IK: return vil3d_slice_ik(im,slice_index);
00141     case VIL3D_SLICE_FORMAT_KI: return vil3d_slice_kj(im,slice_index);
00142     case VIL3D_SLICE_FORMAT_JK: return vil3d_slice_jk(im,slice_index);
00143     case VIL3D_SLICE_FORMAT_KJ: return vil3d_slice_kj(im,slice_index);
00144     default: assert(!"invalid slice_format");
00145   }
00146   return vil_image_view<T>(); // to avoid compiler warning
00147 }
00148 
00149 //: Return number of possible slices of image given the selected format
00150 //  Thus if slice_format==VIL3D_SLICE_FORMAT_IJ then return image.nk().
00151 inline unsigned vil3d_n_slices(const vil3d_image_view_base& image,
00152                                vil3d_slice_format slice_format)
00153 {
00154   switch (slice_format)
00155   {
00156     case VIL3D_SLICE_FORMAT_IJ: return image.nk();
00157     case VIL3D_SLICE_FORMAT_JI: return image.nk();
00158     case VIL3D_SLICE_FORMAT_IK: return image.nj();
00159     case VIL3D_SLICE_FORMAT_KI: return image.nj();
00160     case VIL3D_SLICE_FORMAT_JK: return image.ni();
00161     case VIL3D_SLICE_FORMAT_KJ: return image.ni();
00162     default: assert(!"invalid slice_format");
00163   }
00164   return 0; // to avoid compiler warning
00165 }
00166 
00167 //: Return pixel width in i direction of slice, given widths of original 3D volume pixels
00168 //  Thus if slice_format==VIL3D_SLICE_FORMAT_JK then return src_j_width.
00169 inline double vil3d_slice_pixel_width_i(double src_i_width, double src_j_width, double src_k_width,
00170                                         vil3d_slice_format slice_format)
00171 {
00172   switch (slice_format)
00173   {
00174     case VIL3D_SLICE_FORMAT_IJ: return src_i_width;
00175     case VIL3D_SLICE_FORMAT_JI: return src_j_width;
00176     case VIL3D_SLICE_FORMAT_IK: return src_i_width;
00177     case VIL3D_SLICE_FORMAT_KI: return src_k_width;
00178     case VIL3D_SLICE_FORMAT_JK: return src_j_width;
00179     case VIL3D_SLICE_FORMAT_KJ: return src_k_width;
00180     default: assert(!"invalid slice_format");
00181   }
00182   return 0.0; // to avoid compiler warning
00183 }
00184 
00185 //: Return pixel width in j direction of slice, given widths of original 3D volume pixels
00186 //  Thus if slice_format==VIL3D_SLICE_FORMAT_JK then return src_k_width.
00187 inline double vil3d_slice_pixel_width_j(double src_i_width, double src_j_width, double src_k_width,
00188                                         vil3d_slice_format slice_format)
00189 {
00190   switch (slice_format)
00191   {
00192     case VIL3D_SLICE_FORMAT_IJ: return src_j_width;
00193     case VIL3D_SLICE_FORMAT_JI: return src_i_width;
00194     case VIL3D_SLICE_FORMAT_IK: return src_k_width;
00195     case VIL3D_SLICE_FORMAT_KI: return src_i_width;
00196     case VIL3D_SLICE_FORMAT_JK: return src_k_width;
00197     case VIL3D_SLICE_FORMAT_KJ: return src_j_width;
00198     default: assert(!"invalid slice_format");
00199   }
00200   return 0.0; // to avoid compiler warning
00201 }
00202 
00203 //: Return separation of neighbouring slices, given widths of original 3D volume pixels
00204 //  Thus if slice_format==VIL3D_SLICE_FORMAT_JK then return src_i_width.
00205 inline double vil3d_slice_separation(double src_i_width, double src_j_width, double src_k_width,
00206                                      vil3d_slice_format slice_format)
00207 {
00208   switch (slice_format)
00209   {
00210     case VIL3D_SLICE_FORMAT_IJ: return src_k_width;
00211     case VIL3D_SLICE_FORMAT_JI: return src_k_width;
00212     case VIL3D_SLICE_FORMAT_IK: return src_j_width;
00213     case VIL3D_SLICE_FORMAT_KI: return src_j_width;
00214     case VIL3D_SLICE_FORMAT_JK: return src_i_width;
00215     case VIL3D_SLICE_FORMAT_KJ: return src_i_width;
00216     default: assert(!"invalid slice_format");
00217   }
00218   return 0.0; // to avoid compiler warning
00219 }
00220 
00221 
00222 #endif // vil3d_slice_h_