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

conv_stroke.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_p.h"
00015 #include "agg_renderer_scanline.h"
00016 #include "agg_pixfmt_rgb.h"
00017 #include "platform/agg_platform_support.h"
00018 #include "ctrl/agg_slider_ctrl.h"
00019 #include "ctrl/agg_rbox_ctrl.h"
00020 
00021 
00022 enum flip_y_e { flip_y = true };
00023 
00024 
00025 
00026 class the_application : public agg::platform_support
00027 {
00028     double m_x[3];
00029     double m_y[3];
00030     double m_dx;
00031     double m_dy;
00032     int    m_idx;
00033     agg::rbox_ctrl<agg::rgba8> m_join;
00034     agg::rbox_ctrl<agg::rgba8> m_cap;
00035     agg::slider_ctrl<agg::rgba8> m_width;
00036     agg::slider_ctrl<agg::rgba8> m_miter_limit;
00037 
00038 
00039 public:
00040     the_application(agg::pix_format_e format, bool flip_y) :
00041         agg::platform_support(format, flip_y),
00042         m_idx(-1),
00043         m_join(10.0, 10.0, 133.0, 80.0, !flip_y),
00044         m_cap(10.0, 80.0 + 10.0, 133.0, 80.0 + 80.0, !flip_y),
00045         m_width(130 + 10.0, 10.0 + 4.0, 500.0 - 10.0, 10.0 + 8.0 + 4.0, !flip_y),
00046         m_miter_limit(130 + 10.0, 20.0 + 10.0 + 4.0, 500.0 - 10.0, 20.0 + 10.0 + 8.0 + 4.0, !flip_y)
00047     {
00048         m_x[0] = 57  + 100; m_y[0] = 60;
00049         m_x[1] = 369 + 100; m_y[1] = 170;
00050         m_x[2] = 143 + 100; m_y[2] = 310;
00051 
00052         add_ctrl(m_join);
00053         m_join.text_size(7.5);
00054         m_join.text_thickness(1.0);
00055         m_join.add_item("Miter Join");
00056         m_join.add_item("Miter Join Revert");
00057         m_join.add_item("Round Join");
00058         m_join.add_item("Bevel Join");
00059         m_join.cur_item(2);
00060 
00061         add_ctrl(m_cap);
00062         m_cap.add_item("Butt Cap");
00063         m_cap.add_item("Square Cap");
00064         m_cap.add_item("Round Cap");
00065         m_cap.cur_item(2);
00066 
00067         add_ctrl(m_width);
00068         m_width.range(3.0, 40.0);
00069         m_width.value(20.0);
00070         m_width.label("Width=%1.2f");
00071 
00072         add_ctrl(m_miter_limit);
00073         m_miter_limit.range(1.0, 10.0);
00074         m_miter_limit.value(4.0);
00075         m_miter_limit.label("Miter Limit=%1.2f");
00076 
00077         m_join.no_transform();
00078         m_cap.no_transform();
00079         m_width.no_transform();
00080         m_miter_limit.no_transform();
00081     }
00082 
00083     virtual void on_init()
00084     {
00085     }
00086 
00087     virtual void on_draw()
00088     {
00089         typedef agg::renderer_base<agg::pixfmt_bgr24> ren_base;
00090 
00091         agg::pixfmt_bgr24 pixf(rbuf_window());
00092         ren_base renb(pixf);
00093         renb.clear(agg::rgba(1, 1, 1));
00094 
00095         agg::rasterizer_scanline_aa<> ras;
00096         agg::scanline_p8 sl;
00097 
00098         agg::path_storage path;
00099 
00100         path.move_to(m_x[0], m_y[0]);
00101         path.line_to((m_x[0] + m_x[1]) / 2, (m_y[0] + m_y[1]) / 2); // This point is added only to check for numerical stability
00102         path.line_to(m_x[1], m_y[1]);
00103         path.line_to(m_x[2], m_y[2]);
00104         path.line_to(m_x[2], m_y[2]);                               // This point is added only to check for numerical stability
00105 
00106         path.move_to((m_x[0] + m_x[1]) / 2, (m_y[0] + m_y[1]) / 2);
00107         path.line_to((m_x[1] + m_x[2]) / 2, (m_y[1] + m_y[2]) / 2);
00108         path.line_to((m_x[2] + m_x[0]) / 2, (m_y[2] + m_y[0]) / 2);
00109         path.close_polygon();
00110 
00111         agg::line_cap_e           cap = agg::butt_cap;
00112         if(m_cap.cur_item() == 1) cap = agg::square_cap;
00113         if(m_cap.cur_item() == 2) cap = agg::round_cap;
00114         
00115         agg::line_join_e           join = agg::miter_join;
00116         if(m_join.cur_item() == 1) join = agg::miter_join_revert;
00117         if(m_join.cur_item() == 2) join = agg::round_join;
00118         if(m_join.cur_item() == 3) join = agg::bevel_join;
00119 
00120 
00121 
00122         // (1)
00123         agg::conv_stroke<agg::path_storage> stroke(path);
00124         stroke.line_join(join);
00125         stroke.line_cap(cap);
00126         stroke.miter_limit(m_miter_limit.value());
00127         stroke.width(m_width.value());
00128         ras.add_path(stroke);
00129         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0.8, 0.7, 0.6));
00130         // (1)
00131 
00132 
00133         // (2)
00134         agg::conv_stroke<agg::path_storage> poly1(path);
00135         poly1.width(1.5);
00136         ras.add_path(poly1);
00137         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0,0,0));
00138         // (2)
00139 
00140 
00141 
00142         // (3)
00143         agg::conv_dash<agg::conv_stroke<agg::path_storage> > poly2_dash(stroke);
00144         agg::conv_stroke<agg::conv_dash<agg::conv_stroke<agg::path_storage> > > poly2(poly2_dash);
00145         poly2.miter_limit(4.0);
00146         poly2.width(m_width.value() / 5.0);
00147         poly2.line_cap(cap);
00148         poly2.line_join(join);
00149         poly2_dash.add_dash(20.0, m_width.value() / 2.5);
00150         ras.add_path(poly2);
00151         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0,0,0.3));
00152         // (3)
00153 
00154 
00155 
00156         // (4)
00157         ras.add_path(path);
00158         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba(0.0, 0.0, 0.0, 0.2));
00159         // (4)
00160 
00161 
00162 
00163         agg::render_ctrl(ras, sl, renb, m_join);
00164         agg::render_ctrl(ras, sl, renb, m_cap);
00165         agg::render_ctrl(ras, sl, renb, m_width);
00166         agg::render_ctrl(ras, sl, renb, m_miter_limit);
00167     }
00168 
00169 
00170     virtual void on_mouse_button_down(int x, int y, unsigned flags)
00171     {
00172         if(flags & agg::mouse_left)
00173         {
00174             unsigned i;
00175             for (i = 0; i < 3; i++)
00176             {
00177                 if(sqrt( (x-m_x[i]) * (x-m_x[i]) + (y-m_y[i]) * (y-m_y[i]) ) < 20.0)
00178                 {
00179                     m_dx = x - m_x[i];
00180                     m_dy = y - m_y[i];
00181                     m_idx = i;
00182                     break;
00183                 }
00184             }
00185             if(i == 3)
00186             {
00187                 if(agg::point_in_triangle(m_x[0], m_y[0], 
00188                                           m_x[1], m_y[1],
00189                                           m_x[2], m_y[2],
00190                                           x, y))
00191                 {
00192                     m_dx = x - m_x[0];
00193                     m_dy = y - m_y[0];
00194                     m_idx = 3;
00195                 }
00196 
00197             }
00198         }
00199     }
00200 
00201 
00202     virtual void on_mouse_move(int x, int y, unsigned flags)
00203     {
00204         if(flags & agg::mouse_left)
00205         {
00206             if(m_idx == 3)
00207             {
00208                 double dx = x - m_dx;
00209                 double dy = y - m_dy;
00210                 m_x[1] -= m_x[0] - dx;
00211                 m_y[1] -= m_y[0] - dy;
00212                 m_x[2] -= m_x[0] - dx;
00213                 m_y[2] -= m_y[0] - dy;
00214                 m_x[0] = dx;
00215                 m_y[0] = dy;
00216                 force_redraw();
00217                 return;
00218             }
00219 
00220             if(m_idx >= 0)
00221             {
00222                 m_x[m_idx] = x - m_dx;
00223                 m_y[m_idx] = y - m_dy;
00224                 force_redraw();
00225             }
00226         }
00227         else
00228         {
00229             on_mouse_button_up(x, y, flags);
00230         }
00231     }
00232 
00233     virtual void on_mouse_button_up(int x, int y, unsigned flags)
00234     {
00235         m_idx = -1;
00236     }
00237 
00238     
00239     virtual void on_key(int x, int y, unsigned key, unsigned flags)
00240     {
00241         double dx = 0;
00242         double dy = 0;
00243         switch(key)
00244         {
00245         case agg::key_left:  dx = -0.1; break;
00246         case agg::key_right: dx =  0.1; break;
00247         case agg::key_up:    dy =  0.1; break;
00248         case agg::key_down:  dy = -0.1; break;
00249         }
00250         m_x[0] += dx;
00251         m_y[0] += dy;
00252         m_x[1] += dx;
00253         m_y[1] += dy;
00254         force_redraw();
00255     }
00256 
00257 };
00258 
00259 
00260 
00261 int agg_main(int argc, char* argv[])
00262 {
00263     the_application app(agg::pix_format_bgr24, flip_y);
00264     app.caption("AGG Example. Line Join");
00265 
00266     if(app.init(500, 330, 0))
00267     {
00268         return app.run();
00269     }
00270     return 1;
00271 }
00272 
00273 

© sourcejam.com 2005-2008