Anti-Grain Geometry - AGG (libagg)  2.5
agg-2.5/include/agg_conv_curve.h
Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 // Anti-Grain Geometry (AGG) - Version 2.5
00003 // A high quality rendering engine for C++
00004 // Copyright (C) 2002-2006 Maxim Shemanarev
00005 // Contact: mcseem@antigrain.com
00006 //          mcseemagg@yahoo.com
00007 //          http://antigrain.com
00008 // 
00009 // AGG is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 // 
00014 // AGG is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU General Public License
00020 // along with AGG; if not, write to the Free Software
00021 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00022 // MA 02110-1301, USA.
00023 //----------------------------------------------------------------------------
00024 
00025 #ifndef AGG_CONV_CURVE_INCLUDED
00026 #define AGG_CONV_CURVE_INCLUDED
00027 
00028 #include "agg_basics.h"
00029 #include "agg_curves.h"
00030 
00031 namespace agg
00032 {
00033 
00034 
00035     //---------------------------------------------------------------conv_curve
00036     // Curve converter class. Any path storage can have Bezier curves defined 
00037     // by their control points. There're two types of curves supported: curve3 
00038     // and curve4. Curve3 is a conic Bezier curve with 2 endpoints and 1 control
00039     // point. Curve4 has 2 control points (4 points in total) and can be used
00040     // to interpolate more complicated curves. Curve4, unlike curve3 can be used 
00041     // to approximate arcs, both circular and elliptical. Curves are approximated 
00042     // with straight lines and one of the approaches is just to store the whole 
00043     // sequence of vertices that approximate our curve. It takes additional 
00044     // memory, and at the same time the consecutive vertices can be calculated 
00045     // on demand. 
00046     //
00047     // Initially, path storages are not suppose to keep all the vertices of the
00048     // curves (although, nothing prevents us from doing so). Instead, path_storage
00049     // keeps only vertices, needed to calculate a curve on demand. Those vertices
00050     // are marked with special commands. So, if the path_storage contains curves 
00051     // (which are not real curves yet), and we render this storage directly, 
00052     // all we will see is only 2 or 3 straight line segments (for curve3 and 
00053     // curve4 respectively). If we need to see real curves drawn we need to 
00054     // include this class into the conversion pipeline. 
00055     //
00056     // Class conv_curve recognizes commands path_cmd_curve3 and path_cmd_curve4 
00057     // and converts these vertices into a move_to/line_to sequence. 
00058     //-----------------------------------------------------------------------
00059     template<class VertexSource, 
00060              class Curve3=curve3, 
00061              class Curve4=curve4> class conv_curve
00062     {
00063     public:
00064         typedef Curve3 curve3_type;
00065         typedef Curve4 curve4_type;
00066         typedef conv_curve<VertexSource, Curve3, Curve4> self_type;
00067 
00068         explicit conv_curve(VertexSource& source) :
00069           m_source(&source), m_last_x(0.0), m_last_y(0.0) {}
00070         void attach(VertexSource& source) { m_source = &source; }
00071 
00072         void approximation_method(curve_approximation_method_e v) 
00073         { 
00074             m_curve3.approximation_method(v);
00075             m_curve4.approximation_method(v);
00076         }
00077 
00078         curve_approximation_method_e approximation_method() const 
00079         { 
00080             return m_curve4.approximation_method();
00081         }
00082 
00083         void approximation_scale(double s) 
00084         { 
00085             m_curve3.approximation_scale(s); 
00086             m_curve4.approximation_scale(s); 
00087         }
00088 
00089         double approximation_scale() const 
00090         { 
00091             return m_curve4.approximation_scale();  
00092         }
00093 
00094         void angle_tolerance(double v) 
00095         { 
00096             m_curve3.angle_tolerance(v); 
00097             m_curve4.angle_tolerance(v); 
00098         }
00099 
00100         double angle_tolerance() const 
00101         { 
00102             return m_curve4.angle_tolerance();  
00103         }
00104 
00105         void cusp_limit(double v) 
00106         { 
00107             m_curve3.cusp_limit(v); 
00108             m_curve4.cusp_limit(v); 
00109         }
00110 
00111         double cusp_limit() const 
00112         { 
00113             return m_curve4.cusp_limit();  
00114         }
00115 
00116         void     rewind(unsigned path_id); 
00117         unsigned vertex(double* x, double* y);
00118 
00119     private:
00120         conv_curve(const self_type&);
00121         const self_type& operator = (const self_type&);
00122 
00123         VertexSource* m_source;
00124         double        m_last_x;
00125         double        m_last_y;
00126         curve3_type   m_curve3;
00127         curve4_type   m_curve4;
00128     };
00129 
00130 
00131 
00132     //------------------------------------------------------------------------
00133     template<class VertexSource, class Curve3, class Curve4>
00134     void conv_curve<VertexSource, Curve3, Curve4>::rewind(unsigned path_id)
00135     {
00136         m_source->rewind(path_id);
00137         m_last_x = 0.0;
00138         m_last_y = 0.0;
00139         m_curve3.reset();
00140         m_curve4.reset();
00141     }
00142 
00143 
00144     //------------------------------------------------------------------------
00145     template<class VertexSource, class Curve3, class Curve4>
00146     unsigned conv_curve<VertexSource, Curve3, Curve4>::vertex(double* x, double* y)
00147     {
00148         if(!is_stop(m_curve3.vertex(x, y)))
00149         {
00150             m_last_x = *x;
00151             m_last_y = *y;
00152             return path_cmd_line_to;
00153         }
00154 
00155         if(!is_stop(m_curve4.vertex(x, y)))
00156         {
00157             m_last_x = *x;
00158             m_last_y = *y;
00159             return path_cmd_line_to;
00160         }
00161 
00162         double ct2_x;
00163         double ct2_y;
00164         double end_x;
00165         double end_y;
00166 
00167         unsigned cmd = m_source->vertex(x, y);
00168         switch(cmd)
00169         {
00170         case path_cmd_curve3:
00171             m_source->vertex(&end_x, &end_y);
00172 
00173             m_curve3.init(m_last_x, m_last_y, 
00174                           *x,       *y, 
00175                           end_x,     end_y);
00176 
00177             m_curve3.vertex(x, y);    // First call returns path_cmd_move_to
00178             m_curve3.vertex(x, y);    // This is the first vertex of the curve
00179             cmd = path_cmd_line_to;
00180             break;
00181 
00182         case path_cmd_curve4:
00183             m_source->vertex(&ct2_x, &ct2_y);
00184             m_source->vertex(&end_x, &end_y);
00185 
00186             m_curve4.init(m_last_x, m_last_y, 
00187                           *x,       *y, 
00188                           ct2_x,    ct2_y, 
00189                           end_x,    end_y);
00190 
00191             m_curve4.vertex(x, y);    // First call returns path_cmd_move_to
00192             m_curve4.vertex(x, y);    // This is the first vertex of the curve
00193             cmd = path_cmd_line_to;
00194             break;
00195         }
00196         m_last_x = *x;
00197         m_last_y = *y;
00198         return cmd;
00199     }
00200 
00201 
00202 }
00203 
00204 
00205 
00206 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines