1 // This is mul/vimt3d/vimt3d_trilin_interp.h
2 #ifndef vimt3d_trilin_interp_h_
3 #define vimt3d_trilin_interp_h_
4 //:
5 // \file
6 // \brief Bilinear interpolation functions for 2D images
7 // \author Tim Cootes
8 
9 #include <vimt3d/vimt3d_image_3d_of.h>
10 #include <vgl/vgl_point_3d.h>
11 #include <vil3d/vil3d_trilin_interp.h>
12 #include <vil3d/vil3d_image_view.h>
13 
14 //: Compute trilinear interpolation at p(x,y,z) in world coordinates, with bound checks.
15 //  Interpolates given plane of image.image() at image.world2im(p).
16 //  If p is outside interpolatable image region, returns zero or \a outval
17 template<class T>
18 inline double vimt3d_trilin_interp_safe(const vimt3d_image_3d_of<T>& image,
19                                         const vgl_point_3d<double>& p,
20                                         unsigned plane=0,
21                                         T outval=0)
22 {
23   vgl_point_3d<double> im_p = image.world2im()(p);
24   const vil3d_image_view<T>& im = image.image();
25   return vil3d_trilin_interp_safe(im_p.x(),im_p.y(),im_p.z(),
26                                   im.origin_ptr()+plane*im.planestep(),
27                                   im.ni(),im.nj(),im.nk(),
28                                   im.istep(),im.jstep(),im.kstep(),
29                                   outval);
30 }
31 
32 //: Compute trilinear interpolation at p(x,y,z) in world coordinates, no bound checks.
33 //  Interpolates given plane of image.image() at image.world2im(p).
34 template<class T>
35 inline double vimt3d_trilin_interp_raw(const vimt3d_image_3d_of<T>& image,
36                                        const vgl_point_3d<double>& p,
37                                        unsigned plane=0)
38 {
39   vgl_point_3d<double> im_p = image.world2im()(p);
40   const vil3d_image_view<T>& im = image.image();
41   return vil3d_trilin_interp_raw(im_p.x(),im_p.y(),im_p.z(),
42                                  im.origin_ptr()+plane*im.planestep(),
43                                  im.istep(),im.jstep(),im.kstep());
44 }
45 
46 //: Compute trilinear interpolation at p(x,y,z) in world coordinates, using the nearest valid value if out of bounds.
47 //  Interpolates given plane of image.image() at image.world2im(p).
48 //  If p is outside safe interpolatable image region, nearest pixel value is returned.
49 template<class T>
50 inline double vimt3d_trilin_interp_safe_extend(const vimt3d_image_3d_of<T>& image,
51                                                const vgl_point_3d<double>& p,
52                                                unsigned plane=0)
53 {
54   vgl_point_3d<double> im_p = image.world2im()(p);
55   const vil3d_image_view<T>& im = image.image();
56   return vil3d_trilin_interp_safe_extend(im_p.x(),im_p.y(),im_p.z(),
57                                          im.origin_ptr()+plane*im.planestep(),
58                                          im.ni(),im.nj(),im.nk(),
59                                          im.istep(),im.jstep(),im.kstep());
60 }
61 
62 #endif // vimt3d_trilin_interp_h_
63