contrib/mul/vimt3d/vimt3d_reconstruct_from_grid.h
Go to the documentation of this file.
00001 // This is mul/vimt3d/vimt3d_reconstruct_from_grid.h
00002 #ifndef vimt3d_reconstruct_from_grid_h_
00003 #define vimt3d_reconstruct_from_grid_h_
00004 //:
00005 // \file
00006 // \brief Reconstructs a sample vector into a grid (mirroring vimt3d_sample_grid_trilin)
00007 // \author Graham Vincent
00008 
00009 #include <vimt3d/vimt3d_image_3d_of.h>
00010 #include <vnl/vnl_fwd.h>
00011 #include <vgl/vgl_fwd.h>
00012 #include <vnl/vnl_math.h>
00013 #include <vcl_cassert.h>
00014 
00015 //: Fill voxel which x,y,z is in with val
00016 //  Image is nx * ny * nz array of T. x,y,z element is data[z*zstep+ystep*y+x*xstep]
00017 //  Bound checks are made
00018 // \param val value at image coordinates x,y,z
00019 // \param add_data adds data to existing values
00020 template<class T>
00021 inline void vimt3d_reconstruct_ic_safe(double val, double x, double y, double z,
00022                                        T* data, int ni, int nj, int nk,
00023                                        vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep, bool add_data)
00024 {
00025   int ix=vnl_math_rnd(x);
00026   int iy=vnl_math_rnd(y);
00027   int iz=vnl_math_rnd(z);
00028   vcl_ptrdiff_t index = (ix  *xstep) + (iy  *ystep) + (iz  *zstep);
00029 
00030   // now add or set the image values
00031   if (add_data)
00032   {
00033       if (ix>=0 && iy>=0 && iz>=0
00034           && ix<=(ni-1) && iy<=(nj-1) && iz<(nk-1) )
00035       {
00036           data[index] += T(val);
00037       }
00038   }
00039   else
00040   {
00041       if (ix>=0 && iy>=0 && iz>=0
00042           && ix<=(ni-1) && iy<=(nj-1) && iz<(nk-1) )
00043       {
00044           data[index] = T(val);
00045       }
00046   }
00047 
00048   return;
00049 }
00050 
00051 
00052 //: Fill voxel which x,y,z is in with val
00053 //  Image is ni * nj * nk array of T. x,y,z element is data[z*zstep+y*ystep+x*xstep]
00054 //  No bound checks are made in release mode
00055 // \param val value at image coordinates x,y,z
00056 // \param add_data adds data to existing values
00057 template<class T>
00058 inline void vimt3d_reconstruct_ic_no_checks(double val, double x, double y, double z,
00059                                             T* data, int ni, int nj, int nk,
00060                                             vcl_ptrdiff_t xstep, vcl_ptrdiff_t ystep, vcl_ptrdiff_t zstep, bool add_data)
00061 {
00062   int ix=vnl_math_rnd(x);
00063   int iy=vnl_math_rnd(y);
00064   int iz=vnl_math_rnd(z);
00065   assert (ix >= 0 && ix < ni);
00066   assert (iy >= 0 && iy < nj);
00067   assert (iz >= 0 && iz < nk);
00068   vcl_ptrdiff_t index = (ix  *xstep) + (iy  *ystep) + (iz  *zstep);
00069 
00070   // now add or set the image values
00071   double new_val = add_data ? data[index]+val : val;
00072   data[index] = T(new_val);
00073 #if 0 // doesn't seem to help ...
00074   // avoid rounding errors with integer type T:
00075   if (T(new_val + 1e-2) == T(new_val + 1e-1))
00076     data[index] = T(new_val+1e-2);
00077 #endif
00078 
00079   return;
00080 }
00081 
00082 
00083 //: Reconstruct a smoothed image grid p+i.u+j.v+k.w from vector (in world coordinates)
00084 //  Profile points are p+i.u+j.v+k.w, where i=[0..nu-1],j=[0..nv-1], k=[0..nw-1]
00085 //  Vector v is resized to nu*nv*nw*np elements, where np=image.n_planes().
00086 //  v[0]..v[np-1] are the values from point p
00087 //  Reconstruction occurs along direction w first
00088 // \param add_data adds data to existing values
00089 template <class imType, class vecType>
00090 void vimt3d_reconstruct_from_grid(vimt3d_image_3d_of<imType>& image,
00091                                   const vnl_vector<vecType>& vec,
00092                                   const vgl_point_3d<double>& p,
00093                                   const vgl_vector_3d<double>& u,
00094                                   const vgl_vector_3d<double>& v,
00095                                   const vgl_vector_3d<double>& w,
00096                                   int nu, int nv, int nw, bool add_data);
00097 
00098 
00099 //: Reconstruct a smoothed image grid p+i.u+j.v+k.w from vector (in image coordinates)
00100 //  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]
00101 //  Vector v is resized to nu*nv*nw*np elements, where np=image.n_planes().
00102 //  v[0]..v[np-1] are the values from point p
00103 //  Reconstruction occurs along direction w first
00104 // \param add_data adds data to existing values
00105 template <class imType, class vecType>
00106 void vimt3d_reconstruct_from_grid_ic(vil3d_image_view<imType>& image,
00107                                      const vnl_vector<vecType>& vec,
00108                                      const vgl_point_3d<double>& im_p,
00109                                      const vgl_vector_3d<double>& im_u,
00110                                      const vgl_vector_3d<double>& im_v,
00111                                      const vgl_vector_3d<double>& im_w,
00112                                      int nu, int nv, int nw, bool add_data);
00113 
00114 #endif // vimt3d_reconstruct_from_grid_h_