Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

conv_dash_marker.cpp

Go to the documentation of this file.
00001 #include <math.h>
00002 #include <stdio.h>
00003 #include "agg_basics.h"
00004 #include "agg_rendering_buffer.h"
00005 #include "agg_rasterizer_scanline_aa.h"
00006 #include "agg_conv_stroke.h"
00007 #include "agg_conv_dash.h"
00008 #include "agg_conv_curve.h"
00009 #include "agg_conv_contour.h"
00010 #include "agg_conv_smooth_poly1.h"
00011 #include "agg_conv_marker.h"
00012 #include "agg_arrowhead.h"
00013 #include "agg_vcgen_markers_term.h"
00014 #include "agg_scanline_u.h"
00015 #include "agg_renderer_scanline.h"
00016 #include "agg_pixfmt_rgb.h"
00017 #include "ctrl/agg_slider_ctrl.h"
00018 #include "ctrl/agg_rbox_ctrl.h"
00019 #include "ctrl/agg_cbox_ctrl.h"
00020 #include "platform/agg_platform_support.h"
00021 
00022 
00023 enum flip_y_e { flip_y = true };
00024 
00025 
00026 
00027 class the_application : public agg::platform_support
00028 {
00029     double m_x[3];
00030     double m_y[3];
00031     double m_dx;
00032     double m_dy;
00033     int    m_idx;
00034     agg::rbox_ctrl<agg::rgba8>   m_cap;
00035     agg::slider_ctrl<agg::rgba8> m_width;
00036     agg::slider_ctrl<agg::rgba8> m_smooth;
00037     agg::cbox_ctrl<agg::rgba8>   m_close;
00038     agg::cbox_ctrl<agg::rgba8>   m_even_odd;
00039 
00040 
00041 public:
00042     the_application(agg::pix_format_e format, bool flip_y) :
00043         agg::platform_support(format, flip_y),
00044         m_idx(-1),
00045         m_cap(10.0, 10.0, 130.0, 80.0, !flip_y),
00046         m_width(130 + 10.0, 10.0 + 4.0, 130 + 150.0, 10.0 + 8.0 + 4.0, !flip_y),
00047         m_smooth(130 + 150.0 + 10.0, 10.0 + 4.0, 500 - 10.0, 10.0 + 8.0 + 4.0, !flip_y),
00048         m_close(130 + 10.0, 10.0 + 4.0 + 16.0,    "Close Polygons", !flip_y),
00049         m_even_odd(130 + 150.0 + 10.0, 10.0 + 4.0 + 16.0, "Even-Odd Fill", !flip_y)
00050     {
00051         m_x[0] = 57  + 100; m_y[0] = 60;
00052         m_x[1] = 369 + 100; m_y[1] = 170;
00053         m_x[2] = 143 + 100; m_y[2] = 310;
00054 
00055         add_ctrl(m_cap);
00056         m_cap.add_item("Butt Cap");
00057         m_cap.add_item("Square Cap");
00058         m_cap.add_item("Round Cap");
00059         m_cap.cur_item(0);
00060         m_cap.no_transform();
00061 
00062         add_ctrl(m_width);
00063         m_width.range(0.0, 10.0);
00064         m_width.value(3.0);
00065         m_width.label("Width=%1.2f");
00066         m_width.no_transform();
00067 
00068         add_ctrl(m_smooth);
00069         m_smooth.range(0.0, 2.0);
00070         m_smooth.value(1.0);
00071         m_smooth.label("Smooth=%1.2f");
00072         m_smooth.no_transform();
00073 
00074         add_ctrl(m_close);
00075         m_close.no_transform();
00076 
00077         add_ctrl(m_even_odd);
00078         m_even_odd.no_transform();
00079     }
00080 
00081     virtual void on_init()
00082     {
00083     }
00084 
00085     virtual void on_draw()
00086     {
00087         typedef agg::renderer_base<agg::pixfmt_bgr24> ren_base;
00088 
00089         agg::pixfmt_bgr24 pixf(rbuf_window());
00090         ren_base renb(pixf);
00091         renb.clear(agg::rgba(1, 1, 1));
00092 
00093         agg::rasterizer_scanline_aa<> ras;
00094         agg::scanline_u8 sl;
00095 
00096         agg::line_cap_e           cap = agg::butt_cap;
00097         if(m_cap.cur_item() == 1) cap = agg::square_cap;
00098         if(m_cap.cur_item() == 2) cap = agg::round_cap;
00099 
00100         // Here we declare a very cheap-in-use path storage.
00101         // It allocates space for at most 20 vertices in stack and
00102         // never allocates memory. But be aware that adding more than
00103         // 20 vertices is fatal! 
00104         //------------------------
00105         typedef agg::path_base<
00106             agg::vertex_stl_storage<
00107                 agg::pod_auto_vector<
00108                     agg::vertex_d, 20> > > path_storage_type;
00109         path_storage_type path;
00110 
00111         path.move_to(m_x[0], m_y[0]);
00112         path.line_to(m_x[1], m_y[1]);
00113         path.line_to((m_x[0]+m_x[1]+m_x[2]) / 3.0, (m_y[0]+m_y[1]+m_y[2]) / 3.0);
00114         path.line_to(m_x[2], m_y[2]);
00115         if(m_close.status()) path.close_polygon();
00116 
00117         path.move_to((m_x[0] + m_x[1]) / 2, (m_y[0] + m_y[1]) / 2);
00118         path.line_to((m_x[1] + m_x[2]) / 2, (m_y[1] + m_y[2]) / 2);
00119         path.line_to((m_x[2] + m_x[0]) / 2, (m_y[2] + m_y[0]) / 2);
00120         if(m_close.status()) path.close_polygon();
00121 
00122         if(m_even_odd.status()) ras.filling_rule(agg::fill_even_odd);
00123 
00124         // (1)
00125         ras.add_path(path);
00126         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0.7, 0.5, 0.1, 0.5));
00127         // (1)
00128 
00129         // Start of (2, 3, 4)
00130         agg::conv_smooth_poly1<path_storage_type> smooth(path);
00131         smooth.smooth_value(m_smooth.value());
00132 
00133         // (2)
00134         ras.add_path(smooth);
00135         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0.1, 0.5, 0.7, 0.1));
00136         // (2)
00137 
00138 
00139         // (3)
00140         agg::conv_stroke<agg::conv_smooth_poly1<path_storage_type> > smooth_outline(smooth);
00141         ras.add_path(smooth_outline);
00142         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0.0, 0.6, 0.0, 0.8));
00143         // (3) 
00144 
00145         // (4)
00146         agg::conv_curve<agg::conv_smooth_poly1<path_storage_type> > curve(smooth);
00147         agg::conv_dash<agg::conv_curve<agg::conv_smooth_poly1<path_storage_type> >, agg::vcgen_markers_term> dash(curve);
00148         agg::conv_stroke<agg::conv_dash<agg::conv_curve<agg::conv_smooth_poly1<path_storage_type> >, agg::vcgen_markers_term> > stroke(dash);
00149         stroke.line_cap(cap);
00150         stroke.width(m_width.value());
00151 
00152         double k = ::pow(m_width.value(), 0.7);
00153 
00154         agg::arrowhead ah;
00155                               ah.head(4 * k, 4   * k, 3 * k, 2 * k);
00156         if(!m_close.status()) ah.tail(1 * k, 1.5 * k, 3 * k, 5 * k);
00157 
00158         agg::conv_marker<agg::vcgen_markers_term, agg::arrowhead> arrow(dash.markers(), ah);
00159 
00160         dash.add_dash(20.0, 5.0);
00161         dash.add_dash(5.0, 5.0);
00162         dash.add_dash(5.0, 5.0);
00163         dash.dash_start(10);
00164 
00165         ras.add_path(stroke);
00166         ras.add_path(arrow);
00167         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0.0, 0.0, 0.0));
00168         // (4)
00169 
00170 
00171         ras.filling_rule(agg::fill_non_zero);
00172         agg::render_ctrl(ras, sl, renb, m_cap);
00173         agg::render_ctrl(ras, sl, renb, m_width);
00174         agg::render_ctrl(ras, sl, renb, m_smooth);
00175         agg::render_ctrl(ras, sl, renb, m_close);
00176         agg::render_ctrl(ras, sl, renb, m_even_odd);
00177     }
00178 
00179 
00180     virtual void on_mouse_button_down(int x, int y, unsigned flags)
00181     {
00182         if(flags & agg::mouse_left)
00183         {
00184             unsigned i;
00185             for (i = 0; i < 3; i++)
00186             {
00187                 if(sqrt( (x-m_x[i]) * (x-m_x[i]) + (y-m_y[i]) * (y-m_y[i]) ) < 20.0)
00188                 {
00189                     m_dx = x - m_x[i];
00190                     m_dy = y - m_y[i];
00191                     m_idx = i;
00192                     break;
00193                 }
00194             }
00195             if(i == 3)
00196             {
00197                 if(agg::point_in_triangle(m_x[0], m_y[0], 
00198                                           m_x[1], m_y[1],
00199                                           m_x[2], m_y[2],
00200                                           x, y))
00201                 {
00202                     m_dx = x - m_x[0];
00203                     m_dy = y - m_y[0];
00204                     m_idx = 3;
00205                 }
00206 
00207             }
00208         }
00209     }
00210 
00211 
00212     virtual void on_mouse_move(int x, int y, unsigned flags)
00213     {
00214         if(flags & agg::mouse_left)
00215         {
00216             if(m_idx == 3)
00217             {
00218                 double dx = x - m_dx;
00219                 double dy = y - m_dy;
00220                 m_x[1] -= m_x[0] - dx;
00221                 m_y[1] -= m_y[0] - dy;
00222                 m_x[2] -= m_x[0] - dx;
00223                 m_y[2] -= m_y[0] - dy;
00224                 m_x[0] = dx;
00225                 m_y[0] = dy;
00226                 force_redraw();
00227                 return;
00228             }
00229 
00230             if(m_idx >= 0)
00231             {
00232                 m_x[m_idx] = x - m_dx;
00233                 m_y[m_idx] = y - m_dy;
00234                 force_redraw();
00235             }
00236         }
00237         else
00238         {
00239             on_mouse_button_up(x, y, flags);
00240         }
00241     }
00242 
00243     virtual void on_mouse_button_up(int x, int y, unsigned flags)
00244     {
00245         m_idx = -1;
00246     }
00247 
00248     
00249     virtual void on_key(int x, int y, unsigned key, unsigned flags)
00250     {
00251         double dx = 0;
00252         double dy = 0;
00253         switch(key)
00254         {
00255         case agg::key_left:  dx = -0.1; break;
00256         case agg::key_right: dx =  0.1; break;
00257         case agg::key_up:    dy =  0.1; break;
00258         case agg::key_down:  dy = -0.1; break;
00259         }
00260         m_x[0] += dx;
00261         m_y[0] += dy;
00262         m_x[1] += dx;
00263         m_y[1] += dy;
00264         force_redraw();
00265     }
00266 
00267 };
00268 
00269 
00270 
00271 int agg_main(int argc, char* argv[])
00272 {
00273     the_application app(agg::pix_format_bgr24, flip_y);
00274     app.caption("AGG Example. Line Join");
00275 
00276     if(app.init(500, 330, 0))
00277     {
00278         return app.run();
00279     }
00280     return 1;
00281 }
00282 
00283 

© sourcejam.com 2005-2008