1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#ifndef mbl_draw_line_h_
#define mbl_draw_line_h_
//:
// \file
// \brief Functions to draw shapes into an image (may get moved to vil in time)
// \author Tim Cootes
#include <vil/vil_image_view.h>
#include <vgl/vgl_point_2d.h>
#include <vgl/vgl_vector_2d.h>
#include <vcl_algorithm.h>
#include <vcl_cmath.h>
//: Draws value along line between p1 and p2
// Effective, but not terribly efficient.
template<class T>
inline void mbl_draw_line(vil_image_view<T>& image,
vgl_point_2d<double> p1,
vgl_point_2d<double> p2,
T value, unsigned width=1)
{
vgl_vector_2d<double> dp = p2-p1;
unsigned n = unsigned(1.5+vcl_max(vcl_fabs(dp.x()),vcl_fabs(dp.y())));
dp/=n;
unsigned ni=image.ni(), nj=image.nj();
if (width==1)
{
for (unsigned i=0;i<=n;++i,p1+=dp)
{
unsigned pi=unsigned(p1.x()+0.5); if (pi>=ni) continue;
unsigned pj=unsigned(p1.y()+0.5); if (pj>=nj) continue;
image(pi,pj)=value;
}
}
else
{
double sw=double(unsigned(width/2));
vgl_vector_2d<double> normal(-dp.y(),dp.x());
normal=normalized(normal);
for (unsigned i=0;i<=n;++i,p1+=dp)
{
vgl_point_2d<double> p3=p1-sw*normal;
for ( unsigned j=0;j<width;++j,p3+=normal)
{
unsigned pi=unsigned(p3.x()+0.5); if (pi>=ni) continue;
unsigned pj=unsigned(p3.y()+0.5); if (pj>=nj) continue;
image(pi,pj)=value;
}
}
}
}
//: Draws colour (r,g,b) along line between p1 and p2 in 3-plane image
// Effective, but not terribly efficient.
template<class T>
inline void mbl_draw_line(vil_image_view<T>& image,
vgl_point_2d<double> p1,
vgl_point_2d<double> p2,
T r, T g, T b)
{
vgl_vector_2d<double> dp = p2-p1;
unsigned n = unsigned(1.5+vcl_max(vcl_fabs(dp.x()),vcl_fabs(dp.y())));
dp/=n;
unsigned ni=image.ni(), nj=image.nj();
const vcl_ptrdiff_t rstep = 0;
const vcl_ptrdiff_t gstep = image.planestep();
const vcl_ptrdiff_t bstep =2*gstep;
for (unsigned i=0;i<=n;++i,p1+=dp)
{
unsigned pi=unsigned(p1.x()+0.5); if (pi>=ni) continue;
unsigned pj=unsigned(p1.y()+0.5); if (pj>=nj) continue;
T* im = &image(pi,pj);
im[rstep]=r;
im[gstep]=g;
im[bstep]=b;
}
}
#endif // mbl_draw_line_h_
|