contrib/mul/mbl/mbl_draw_line.h
Go to the documentation of this file.
00001 #ifndef mbl_draw_line_h_
00002 #define mbl_draw_line_h_
00003 //:
00004 // \file
00005 // \brief Functions to draw shapes into an image (may get moved to vil in time)
00006 // \author Tim Cootes
00007 
00008 #include <vil/vil_image_view.h>
00009 #include <vgl/vgl_point_2d.h>
00010 #include <vgl/vgl_vector_2d.h>
00011 #include <vcl_algorithm.h>
00012 #include <vcl_cmath.h>
00013 
00014 //: Draws value along line between p1 and p2
00015 //  Effective, but not terribly efficient.
00016 template<class T>
00017 inline void mbl_draw_line(vil_image_view<T>& image,
00018                           vgl_point_2d<double> p1,
00019                           vgl_point_2d<double> p2,
00020                           T value, unsigned width=1)
00021 {
00022   vgl_vector_2d<double> dp = p2-p1;
00023   unsigned n = unsigned(1.5+vcl_max(vcl_fabs(dp.x()),vcl_fabs(dp.y())));
00024   dp/=n;
00025   unsigned ni=image.ni(), nj=image.nj();
00026   if (width==1)
00027   {
00028     for (unsigned i=0;i<=n;++i,p1+=dp)
00029     {
00030       unsigned pi=unsigned(p1.x()+0.5); if (pi>=ni) continue;
00031       unsigned pj=unsigned(p1.y()+0.5); if (pj>=nj) continue;
00032       image(pi,pj)=value;
00033     }
00034   }
00035   else
00036   {
00037     double sw=double(unsigned(width/2));
00038     vgl_vector_2d<double> normal(-dp.y(),dp.x());
00039     normal=normalized(normal);
00040     for (unsigned i=0;i<=n;++i,p1+=dp)
00041     {
00042       vgl_point_2d<double> p3=p1-sw*normal;
00043       for ( unsigned j=0;j<width;++j,p3+=normal)
00044       {
00045         unsigned pi=unsigned(p3.x()+0.5); if (pi>=ni) continue;
00046         unsigned pj=unsigned(p3.y()+0.5); if (pj>=nj) continue;
00047         image(pi,pj)=value;
00048       }
00049     }
00050   }
00051 }
00052 
00053 //: Draws colour (r,g,b) along line between p1 and p2 in 3-plane image
00054 //  Effective, but not terribly efficient.
00055 template<class T>
00056 inline void mbl_draw_line(vil_image_view<T>& image,
00057                           vgl_point_2d<double> p1,
00058                           vgl_point_2d<double> p2,
00059                           T r, T g, T b)
00060 {
00061   vgl_vector_2d<double> dp = p2-p1;
00062   unsigned n = unsigned(1.5+vcl_max(vcl_fabs(dp.x()),vcl_fabs(dp.y())));
00063   dp/=n;
00064   unsigned ni=image.ni(), nj=image.nj();
00065   const vcl_ptrdiff_t rstep = 0;
00066   const vcl_ptrdiff_t gstep = image.planestep();
00067   const vcl_ptrdiff_t bstep =2*gstep;
00068   for (unsigned i=0;i<=n;++i,p1+=dp)
00069   {
00070     unsigned pi=unsigned(p1.x()+0.5); if (pi>=ni) continue;
00071     unsigned pj=unsigned(p1.y()+0.5); if (pj>=nj) continue;
00072     T* im = &image(pi,pj);
00073     im[rstep]=r;
00074     im[gstep]=g;
00075     im[bstep]=b;
00076   }
00077 }
00078 
00079 
00080 #endif // mbl_draw_line_h_
00081 
00082