Anti-Grain Geometry Tutorial
agg_conv_curve.h
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 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 circular 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, nothing 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,
55  class Curve3=curve3,
56  class Curve4=curve4> class conv_curve
57  {
58  public:
59  typedef Curve3 curve3_type;
60  typedef Curve4 curve4_type;
62 
63  explicit conv_curve(VertexSource& source) :
64  m_source(&source), m_last_x(0.0), m_last_y(0.0) {}
65  void attach(VertexSource& source) { m_source = &source; }
66 
67  void approximation_method(curve_approximation_method_e v)
68  {
69  m_curve3.approximation_method(v);
70  m_curve4.approximation_method(v);
71  }
72 
73  curve_approximation_method_e approximation_method() const
74  {
75  return m_curve4.approximation_method();
76  }
77 
78  void approximation_scale(double s)
79  {
80  m_curve3.approximation_scale(s);
81  m_curve4.approximation_scale(s);
82  }
83 
84  double approximation_scale() const
85  {
86  return m_curve4.approximation_scale();
87  }
88 
89  void angle_tolerance(double v)
90  {
91  m_curve3.angle_tolerance(v);
92  m_curve4.angle_tolerance(v);
93  }
94 
95  double angle_tolerance() const
96  {
97  return m_curve4.angle_tolerance();
98  }
99 
100  void cusp_limit(double v)
101  {
102  m_curve3.cusp_limit(v);
103  m_curve4.cusp_limit(v);
104  }
105 
106  double cusp_limit() const
107  {
108  return m_curve4.cusp_limit();
109  }
110 
111  void rewind(unsigned path_id);
112  unsigned vertex(double* x, double* y);
113 
114  private:
115  conv_curve(const self_type&);
116  const self_type& operator = (const self_type&);
117 
118  VertexSource* m_source;
119  double m_last_x;
120  double m_last_y;
121  curve3_type m_curve3;
122  curve4_type m_curve4;
123  };
124 
125 
126 
127  //------------------------------------------------------------------------
128  template<class VertexSource, class Curve3, class Curve4>
130  {
131  m_source->rewind(path_id);
132  m_last_x = 0.0;
133  m_last_y = 0.0;
134  m_curve3.reset();
135  m_curve4.reset();
136  }
137 
138 
139  //------------------------------------------------------------------------
140  template<class VertexSource, class Curve3, class Curve4>
141  unsigned conv_curve<VertexSource, Curve3, Curve4>::vertex(double* x, double* y)
142  {
143  if(!is_stop(m_curve3.vertex(x, y)))
144  {
145  m_last_x = *x;
146  m_last_y = *y;
147  return path_cmd_line_to;
148  }
149 
150  if(!is_stop(m_curve4.vertex(x, y)))
151  {
152  m_last_x = *x;
153  m_last_y = *y;
154  return path_cmd_line_to;
155  }
156 
157  double ct2_x = 0;
158  double ct2_y = 0;
159  double end_x = 0;
160  double end_y = 0;
161 
162  unsigned cmd = m_source->vertex(x, y);
163  switch(cmd)
164  {
165  case path_cmd_curve3:
166  m_source->vertex(&end_x, &end_y);
167 
168  m_curve3.init(m_last_x, m_last_y,
169  *x, *y,
170  end_x, end_y);
171 
172  m_curve3.vertex(x, y); // First call returns path_cmd_move_to
173  m_curve3.vertex(x, y); // This is the first vertex of the curve
174  cmd = path_cmd_line_to;
175  break;
176 
177  case path_cmd_curve4:
178  m_source->vertex(&ct2_x, &ct2_y);
179  m_source->vertex(&end_x, &end_y);
180 
181  m_curve4.init(m_last_x, m_last_y,
182  *x, *y,
183  ct2_x, ct2_y,
184  end_x, end_y);
185 
186  m_curve4.vertex(x, y); // First call returns path_cmd_move_to
187  m_curve4.vertex(x, y); // This is the first vertex of the curve
188  cmd = path_cmd_line_to;
189  break;
190  }
191  m_last_x = *x;
192  m_last_y = *y;
193  return cmd;
194  }
195 
196 
197 }
198 
199 
200 
201 #endif
Definition: agg_arc.cpp:24