TuttleOFX  1
lines.hpp
Go to the documentation of this file.
00001 // Copyright Tom Brinkman 2008. Distributed under the Boost
00002 // Software License, Version 1.0. (See accompanying
00003 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00004 
00005 #ifndef _lines_hpp_
00006 #define _lines_hpp_
00007 
00008 #include <boost/gil/gil_all.hpp>
00009 #include <boost/mpl/vector_c.hpp>
00010 #include <boost/mpl/for_each.hpp>
00011 #include <channels.hpp>
00012 
00013 namespace layer
00014 {
00015 
00016 template <typename view_t, typename dash_t = boost::mpl::vector_c<int,1,1,0> >
00017 struct uniform_horizontal_lines
00018 {
00019         typedef typename view_t::value_type color_t;
00020         color_t color;
00021         int intervals;
00022         uniform_horizontal_lines(color_t color, int intervals) : 
00023                 color(color), intervals(intervals) {}
00024 
00025         void operator()(view_t& view)
00026         {
00027                 typedef typename view_t::value_type value_type_t;
00028                 
00029                 BOOST_ASSERT(view.width());
00030                 
00031                 std::vector<int> dash;
00032                 boost::mpl::for_each<dash_t>(
00033                         boost::bind(&std::vector<int>::push_back, &dash, _1));    
00034 
00035                 channels y(view.height(),intervals);
00036 
00037                 for (; y; ++y)
00038                         for (int x = 0; x < view.width(); )
00039                                 for (int d = 0; d < dash.size(); ++d, ++x)
00040                                         if (dash[d] && x < view.width())
00041                                                 view(x,*y) = color;
00042         }
00043 };
00044 
00045 template <typename view_t, typename dash_t = boost::mpl::vector_c<int,1,1,0> >
00046 struct uniform_vertical_lines
00047 {
00048         typedef typename view_t::value_type color_t;
00049         color_t color;
00050         int intervals;
00051         uniform_vertical_lines(color_t color, int intervals) : color(color), intervals(intervals) {}
00052                 
00053         void operator()(view_t& view)
00054         {
00055                 typedef typename view_t::value_type value_type_t;
00056                 
00057                 std::vector<int> dash;
00058                 boost::mpl::for_each<dash_t>(
00059                         boost::bind(&std::vector<int>::push_back, &dash, _1));    
00060 
00061                 channels x(view.width(),intervals);
00062 
00063                 for (; x; x++)
00064                         for (int y = 0; y < view.width(); )
00065                                 for (int d = 0; d < dash.size(); ++d, ++y)
00066                                         if (dash[d] && y < view.height())
00067                                                 view(*x,y) = color;
00068         }
00069 };
00070 
00071 template <typename view_t, typename dash_t=boost::mpl::vector_c<int,1,1,1,0> >
00072 struct horizontal_line
00073 {
00074         typedef typename view_t::value_type color_t;
00075         color_t color;
00076         double value,minvalue,maxvalue;
00077         horizontal_line(color_t color, double value, double minvalue, double maxvalue) :
00078                 color(color), minvalue(minvalue), maxvalue(maxvalue), value(value) {}
00079 
00080         void operator()(view_t& view)
00081         {
00082                 std::vector<int> dash;
00083                 boost::mpl::for_each<dash_t>(
00084                         boost::bind(&std::vector<int>::push_back, &dash, _1));    
00085 
00086                 double diff = maxvalue-minvalue;
00087                 if (diff == 0)
00088                         return;
00089 
00090                 double rval = 1-((value-minvalue)/(diff));
00091                 int y = (int)(rval*(view.height()-1));
00092 
00093                 for (int x = 0; x < view.width(); )
00094                         for (int d = 0; d < dash.size(); ++d, ++x)
00095                                 if (dash[d] && x < view.width())
00096                                         view(x,y) = color;
00097         }
00098 };
00099 
00100 template <typename view_t, typename dashlst_t = boost::mpl::vector_c<int,1,1,0> >
00101 struct vertical_lines
00102 {
00103         typedef typename view_t::value_type color_t;
00104         color_t color;
00105         int width;
00106         int periods;
00107         vertical_lines(color_t color, int periods, int width) : 
00108                 color(color), periods(periods), width(width) {}
00109 
00110         void operator()(view_t& view)
00111         {
00112                 std::vector<int> dashlst;
00113                 boost::mpl::for_each<dashlst_t>(
00114                         boost::bind(&std::vector<int>::push_back, &dashlst, _1));
00115                 
00116                 int intervals = view.width()/width;
00117                 channels i(periods,intervals);
00118                 channels x(view.width(),periods);
00119 
00120                 for (int n=0; i; ++i)
00121                 {
00122                         for (; n < *i; ++n, ++x) ;
00123 
00124                         for (int y = 0; y < view.height(); )
00125                                 for (std::size_t d = 0; d < dashlst.size(); ++d, ++y)
00126                                         if (dashlst[d] && y < view.height())
00127                                                 view(*x,y) = color;
00128                 }
00129         }
00130 };
00131 
00132 }
00133 
00134 #endif