contrib/mul/vimt3d/vimt3d_reconstruct_from_grid.txx
Go to the documentation of this file.
00001 // This is mul/vimt3d/vimt3d_reconstruct_from_grid.txx
00002 #ifndef vimt3d_reconstruct_from_grid_txx_
00003 #define vimt3d_reconstruct_from_grid_txx_
00004 //:
00005 // \file
00006 #include "vimt3d_reconstruct_from_grid.h"
00007 #include <vnl/vnl_vector.h>
00008 #include <vgl/vgl_point_3d.h>
00009 #include <vgl/vgl_vector_3d.h>
00010 
00011 //: True if p clearly inside the image
00012 template<class T>
00013 inline bool vimt3d_grid_point_in_image(const vgl_point_3d<double>& p, const vil3d_image_view<T>& image)
00014 {
00015   if (p.x()<1) return false;
00016   if (p.y()<1) return false;
00017   if (p.z()<1) return false;
00018   if (p.x()+2>image.ni()) return false;
00019   if (p.y()+2>image.nj()) return false;
00020   if (p.z()+2>image.nk()) return false;
00021   return true;
00022 }
00023 
00024 //: True if grid of size nu * nv * nw (in steps of u,v,w) is entirely in the image.
00025 //  p defines centre of one size.
00026 template<class T>
00027 inline bool vimt3d_grid_in_image_ic(const vgl_point_3d<double>& im_p,
00028                                     const vgl_vector_3d<double>& im_u,
00029                                     const vgl_vector_3d<double>& im_v,
00030                                     const vgl_vector_3d<double>& im_w,
00031                                     int nu, int nv, int nw,
00032                                     const vil3d_image_view<T>& image)
00033 {
00034   vgl_vector_3d<double> u1=(nu-1)*im_u;
00035   vgl_vector_3d<double> v1=(nv-1)*im_v;
00036   vgl_vector_3d<double> w1=(nw-1)*im_w;
00037   if (!vimt3d_grid_point_in_image(im_p,image)) return false;
00038   if (!vimt3d_grid_point_in_image(im_p+u1,image)) return false;
00039   if (!vimt3d_grid_point_in_image(im_p+v1,image)) return false;
00040   if (!vimt3d_grid_point_in_image(im_p+w1,image)) return false;
00041   if (!vimt3d_grid_point_in_image(im_p+u1+v1,image)) return false;
00042   if (!vimt3d_grid_point_in_image(im_p+u1+w1,image)) return false;
00043   if (!vimt3d_grid_point_in_image(im_p+v1+w1,image)) return false;
00044   if (!vimt3d_grid_point_in_image(im_p+u1+v1+w1,image)) return false;
00045 
00046   return true;
00047 }
00048 
00049 //: Reconstruct a smoothed image grid p+i.u+j.v+k.w from vector (in world coordinates)
00050 //  Profile points are p+i.u+j.v+k.w, where i=[0..nu-1],j=[0..nv-1], k=[0..nw-1]
00051 //  Vector v is resized to nu*nv*nw*np elements, where np=image.n_planes().
00052 //  v[0]..v[np-1] are the values from point p
00053 //  Reconstruction occurs along direction w first
00054 template <class imType, class vecType>
00055 void vimt3d_reconstruct_from_grid(vimt3d_image_3d_of<imType>& image,
00056                                   const vnl_vector<vecType>& vec,
00057                                   const vgl_point_3d<double>& p,
00058                                   const vgl_vector_3d<double>& u,
00059                                   const vgl_vector_3d<double>& v,
00060                                   const vgl_vector_3d<double>& w,
00061                                   int nu, int nv, int nw, bool add_data)
00062 {
00063   // convert to image coordinates
00064   vgl_point_3d<double> im_p0 = image.world2im()(p);
00065   vgl_vector_3d<double> im_u = image.world2im()(p+u)-im_p0;
00066   vgl_vector_3d<double> im_v = image.world2im()(p+v)-im_p0;
00067   vgl_vector_3d<double> im_w = image.world2im()(p+w)-im_p0;
00068 
00069   // call image coordinate version of grid sampler
00070   vimt3d_reconstruct_from_grid_ic(image.image(),vec,im_p0,im_u,im_v,im_w,nu,nv,nw,add_data);
00071 
00072   return;
00073 }
00074 
00075 
00076 //: Reconstruct a smoothed image grid p+i.u+j.v+k.w from vector (in image coordinates)
00077 //  Profile points are im_p+i.im_u+j.im_v+k.im_w, where i=[0..nu-1],j=[0..nv-1], k=[0..nw-1]
00078 //  Vector v is resized to nu*nv*nw*np elements, where np=image.n_planes().
00079 //  v[0]..v[np-1] are the values from point p
00080 //  Reconstruction occurs along direction w first
00081 template <class imType, class vecType>
00082 void vimt3d_reconstruct_from_grid_ic(vil3d_image_view<imType>& image,
00083                                      const vnl_vector<vecType>& vec,
00084                                      const vgl_point_3d<double>& im_p,
00085                                      const vgl_vector_3d<double>& im_u,
00086                                      const vgl_vector_3d<double>& im_v,
00087                                      const vgl_vector_3d<double>& im_w,
00088                                      int nu, int nv, int nw, bool add_data)
00089 {
00090   if (vimt3d_grid_in_image_ic(im_p,im_u,im_v,im_w,nu,nv,nw,image))
00091     vimt3d_reconstruct_from_grid_ic_no_checks(image,vec,im_p,im_u,im_v,im_w,nu,nv,nw,add_data);
00092   else
00093     vimt3d_reconstruct_from_grid_ic_safe(image,vec,im_p,im_u,im_v,im_w,nu,nv,nw,add_data);
00094 
00095   return;
00096 }
00097 
00098 //: WITH NO CHECKS Reconstruct a smoothed image grid p+i.u+j.v+k.w from vector (in image coordinates)
00099 //  Profile points are im_p+i.im_u+j.im_v+k.im_w, where i=[0..nu-1],j=[0..nv-1], k=[0..nw-1]
00100 //  Vector v is resized to nu*nv*nw*np elements, where np=image.n_planes().
00101 //  v[0]..v[np-1] are the values from point p
00102 //  Reconstruction occurs along direction w first
00103 template <class imType, class vecType>
00104 void vimt3d_reconstruct_from_grid_ic_no_checks(vil3d_image_view<imType>& image,
00105                                                const vnl_vector<vecType>& vec,
00106                                                const vgl_point_3d<double>& p0,
00107                                                const vgl_vector_3d<double>& u,
00108                                                const vgl_vector_3d<double>& v,
00109                                                const vgl_vector_3d<double>& w,
00110                                                int nu, int nv, int nw, bool add_data)
00111 {
00112   int np = image.nplanes();
00113   int ni = image.ni();
00114   int nj = image.nj();
00115   int nk = image.nk();
00116   vcl_ptrdiff_t istep = image.istep();
00117   vcl_ptrdiff_t jstep = image.jstep();
00118   vcl_ptrdiff_t kstep = image.kstep();
00119 
00120   const vecType* vc = vec.begin();
00121   vgl_point_3d<double> p1 = p0;
00122 
00123   if (np==1)
00124   {
00125     imType* plane0 = image.origin_ptr();
00126     for (int i=0;i<nu;++i,p1+=u)
00127     {
00128       vgl_point_3d<double> p2 = p1;
00129       for (int j=0;j<nv;++j,p2+=v)
00130       {
00131         vgl_point_3d<double> p = p2;
00132         // Sample each row (along w)
00133         for (int k=0;k<nw;++k,p+=w,++vc)
00134         {
00135             vimt3d_reconstruct_ic_no_checks(*vc,p.x(),p.y(),p.z(),plane0,ni,nj,nk,istep,jstep,kstep,add_data);
00136         }
00137       }
00138     }
00139   }
00140   else
00141   {
00142     for (int i=0;i<nu;++i,p1+=u)
00143     {
00144       vgl_point_3d<double> p2 = p1;
00145       for (int j=0;j<nv;++j,p2+=v)
00146       {
00147         vgl_point_3d<double> p = p2;
00148         // Sample each row (along w)
00149         for (int l=0;l<nw;++l,p+=w)
00150           for (int k=0;k<np;++k,++vc)
00151           {
00152             vimt3d_reconstruct_ic_no_checks(*vc,p.x(),p.y(),p.z(),image.origin_ptr(),ni,nj,nk,istep,jstep,kstep,add_data);
00153           }
00154       }
00155     }
00156   }
00157 }
00158 
00159 
00160 //: WITH SAFETY CHECKS Reconstruct a smoothed image grid p+i.u+j.v+k.w from vector (in image coordinates)
00161 //  Profile points are im_p+i.im_u+j.im_v+k.im_w, where i=[0..nu-1],j=[0..nv-1], k=[0..nw-1]
00162 //  Vector v is resized to nu*nv*nw*np elements, where np=image.n_planes().
00163 //  v[0]..v[np-1] are the values from point p
00164 //  Reconstruction occurs along direction w first
00165 template <class imType, class vecType>
00166 void vimt3d_reconstruct_from_grid_ic_safe(vil3d_image_view<imType>& image,
00167                                           const vnl_vector<vecType>& vec,
00168                                           const vgl_point_3d<double>& p0,
00169                                           const vgl_vector_3d<double>& u,
00170                                           const vgl_vector_3d<double>& v,
00171                                           const vgl_vector_3d<double>& w,
00172                                           int nu, int nv, int nw, bool add_data)
00173 {
00174   int np = image.nplanes();
00175   int ni = image.ni();
00176   int nj = image.nj();
00177   int nk = image.nk();
00178   vcl_ptrdiff_t istep = image.istep();
00179   vcl_ptrdiff_t jstep = image.jstep();
00180   vcl_ptrdiff_t kstep = image.kstep();
00181 
00182   const vecType* vc = vec.begin();
00183   vgl_point_3d<double> p1 = p0;
00184 
00185   if (np==1)
00186   {
00187     imType* plane0 = image.origin_ptr();
00188     for (int i=0;i<nu;++i,p1+=u)
00189     {
00190       vgl_point_3d<double> p2 = p1;
00191       for (int j=0;j<nv;++j,p2+=v)
00192       {
00193         vgl_point_3d<double> p = p2;
00194         // Sample each row (along w)
00195         for (int k=0;k<nw;++k,p+=w,++vc)
00196         {
00197             vimt3d_reconstruct_ic_safe(*vc,p.x(),p.y(),p.z(),plane0,ni,nj,nk,istep,jstep,kstep,add_data);
00198         }
00199       }
00200     }
00201   }
00202   else
00203   {
00204     for (int i=0;i<nu;++i,p1+=u)
00205     {
00206       vgl_point_3d<double> p2 = p1;
00207       for (int j=0;j<nv;++j,p2+=v)
00208       {
00209         vgl_point_3d<double> p = p2;
00210         // Sample each row (along w)
00211         for (int l=0;l<nw;++l,p+=w)
00212           for (int k=0;k<np;++k,++vc)
00213           {
00214             vimt3d_reconstruct_ic_safe(*vc,p.x(),p.y(),p.z(),image.origin_ptr(),ni,nj,nk,istep,jstep,kstep,add_data);
00215           }
00216       }
00217     }
00218   }
00219 }
00220 
00221 
00222 #define VIMT3D_RECONSTRUCT_FROM_GRID_INSTANTIATE( imType, vecType ) \
00223 template void vimt3d_reconstruct_from_grid(vimt3d_image_3d_of<imType >& image, \
00224                                            const vnl_vector<vecType >& vec, \
00225                                            const vgl_point_3d<double >& p, \
00226                                            const vgl_vector_3d<double >& u, \
00227                                            const vgl_vector_3d<double >& v, \
00228                                            const vgl_vector_3d<double >& w, \
00229                                            int nu, int nv, int nw, bool add_data); \
00230 template void vimt3d_reconstruct_from_grid_ic(vil3d_image_view<imType >& image, \
00231                                               const vnl_vector<vecType >& vec, \
00232                                               const vgl_point_3d<double >& im_p, \
00233                                               const vgl_vector_3d<double >& im_u, \
00234                                               const vgl_vector_3d<double >& im_v, \
00235                                               const vgl_vector_3d<double >& im_w, \
00236                                               int nu, int nv, int nw, bool add_data); \
00237 template void vimt3d_reconstruct_from_grid_ic_safe(vil3d_image_view<imType>& image, \
00238                                                    const vnl_vector<vecType>& vec, \
00239                                                    const vgl_point_3d<double>& p0, \
00240                                                    const vgl_vector_3d<double>& u, \
00241                                                    const vgl_vector_3d<double>& v, \
00242                                                    const vgl_vector_3d<double>& w, \
00243                                                    int nu, int nv, int nw, bool add_data); \
00244 template void vimt3d_reconstruct_from_grid_ic_no_checks(vil3d_image_view<imType>& image, \
00245                                                         const vnl_vector<vecType>& vec, \
00246                                                         const vgl_point_3d<double>& p0, \
00247                                                         const vgl_vector_3d<double>& u, \
00248                                                         const vgl_vector_3d<double>& v, \
00249                                                         const vgl_vector_3d<double>& w, \
00250                                                         int nu, int nv, int nw, bool add_data)
00251 
00252 #endif // vimt3d_reconstruct_from_grid_txx_