1 //---------------------------------------------------------------------------- 2 // Anti-Grain Geometry - Version 2.2 3 // Copyright (C) 2002-2004 Maxim Shemanarev (http://www.antigrain.com) 4 // 5 // Permission to copy, use, modify, sell and distribute this software 6 // is granted provided this copyright notice appears in all copies. 7 // This software is provided "as is" without express or implied 8 // warranty, and with no claim as to its suitability for any purpose. 9 // 10 //---------------------------------------------------------------------------- 11 // Contact: mcseem@antigrain.com 12 // mcseemagg@yahoo.com 13 // http://www.antigrain.com 14 //---------------------------------------------------------------------------- 15 // 16 // classes conv_curve 17 // 18 //---------------------------------------------------------------------------- 19 20 #ifndef AGG_CONV_CURVE_INCLUDED 21 #define AGG_CONV_CURVE_INCLUDED 22 23 #include "agg_basics.h" 24 #include "agg_curves.h" 25 26 namespace agg 27 { 28 29 30 //---------------------------------------------------------------conv_curve 31 // Curve converter class. Any path storage can have Bezier curves defined 32 // by their control points. There're two types of curves supported: curve3 33 // and curve4. Curve3 is a conic Bezier curve with 2 endpoints and 1 control 34 // point. Curve4 has 2 control points (4 points in total) and can be used 35 // to interpolate more complicated curves. Curve4, unlike curve3 can be used 36 // to approximate arcs, both curcular and elliptical. Curves are approximated 37 // with straight lines and one of the approaches is just to store the whole 38 // sequence of vertices that approximate our curve. It takes additional 39 // memory, and at the same time the consecutive vertices can be calculated 40 // on demand. 41 // 42 // Initially, path storages are not suppose to keep all the vertices of the 43 // curves (although, nothig prevents us from doing so). Instead, path_storage 44 // keeps only vertices, needed to calculate a curve on demand. Those vertices 45 // are marked with special commands. So, if the path_storage contains curves 46 // (which are not real curves yet), and we render this storage directly, 47 // all we will see is only 2 or 3 straight line segments (for curve3 and 48 // curve4 respectively). If we need to see real curves drawn we need to 49 // include this class into the conversion pipeline. 50 // 51 // Class conv_curve recognizes commands path_cmd_curve3 and path_cmd_curve4 52 // and converts these vertices into a move_to/line_to sequence. 53 //----------------------------------------------------------------------- 54 template<class VertexSource> class conv_curve 55 { 56 public: 57 conv_curve(VertexSource& source) : 58 m_source(&source), m_last_x(0.0), m_last_y(0.0) {} 59 60 void set_source(VertexSource& source) { m_source = &source; } 61 62 void approximation_scale(double s) 63 { 64 m_curve3.approximation_scale(s); 65 m_curve4.approximation_scale(s); 66 } 67 68 double approximation_scale() const 69 { 70 return m_curve3.approximation_scale(); 71 } 72 73 void rewind(unsigned id); 74 unsigned vertex(double* x, double* y); 75 76 typedef conv_curve<VertexSource> source_type; 77 typedef vertex_iterator<source_type> iterator; 78 iterator begin(unsigned id) { return iterator(*this, id); } 79 iterator end() { return iterator(path_cmd_stop); } 80 81 private: 82 conv_curve(const conv_curve<VertexSource>&); 83 const conv_curve<VertexSource>& 84 operator = (const conv_curve<VertexSource>&); 85 86 VertexSource* m_source; 87 double m_last_x; 88 double m_last_y; 89 curve3 m_curve3; 90 curve4 m_curve4; 91 }; 92 93 94 95 //------------------------------------------------------------------------ 96 template<class VertexSource> 97 void conv_curve<VertexSource>::rewind(unsigned id) 98 { 99 m_source->rewind(id); 100 m_last_x = 0.0; 101 m_last_y = 0.0; 102 m_curve3.reset(); 103 m_curve4.reset(); 104 } 105 106 107 //------------------------------------------------------------------------ 108 template<class VertexSource> 109 unsigned conv_curve<VertexSource>::vertex(double* x, double* y) 110 { 111 if(!is_stop(m_curve3.vertex(x, y))) 112 { 113 m_last_x = *x; 114 m_last_y = *y; 115 return path_cmd_line_to; 116 } 117 118 if(!is_stop(m_curve4.vertex(x, y))) 119 { 120 m_last_x = *x; 121 m_last_y = *y; 122 return path_cmd_line_to; 123 } 124 125 double ct2_x; 126 double ct2_y; 127 double end_x; 128 double end_y; 129 130 unsigned cmd = m_source->vertex(x, y); 131 switch(cmd) 132 { 133 case path_cmd_move_to: 134 case path_cmd_line_to: 135 m_last_x = *x; 136 m_last_y = *y; 137 default: 138 break; 139 140 case path_cmd_curve3: 141 m_source->vertex(&end_x, &end_y); 142 143 m_curve3.init(m_last_x, m_last_y, 144 *x, *y, 145 end_x, end_y); 146 147 m_curve3.vertex(x, y); // First call returns path_cmd_move_to 148 m_curve3.vertex(x, y); // This is the first vertex of the curve 149 cmd = path_cmd_line_to; 150 break; 151 152 case path_cmd_curve4: 153 m_source->vertex(&ct2_x, &ct2_y); 154 m_source->vertex(&end_x, &end_y); 155 156 m_curve4.init(m_last_x, m_last_y, 157 *x, *y, 158 ct2_x, ct2_y, 159 end_x, end_y); 160 161 m_curve4.vertex(x, y); // First call returns path_cmd_move_to 162 m_curve4.vertex(x, y); // This is the first vertex of the curve 163 cmd = path_cmd_line_to; 164 break; 165 } 166 return cmd; 167 } 168 169 170 } 171 172 173 174 #endif 175