1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
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.
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
18 //----------------------------------------------------------------------------
20 #ifndef AGG_CONV_CURVE_INCLUDED
21 #define AGG_CONV_CURVE_INCLUDED
23 #include "agg_basics.h"
24 #include "agg_curves.h"
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
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.
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
57 conv_curve(VertexSource
& source
) :
58 m_source(&source
), m_last_x(0.0), m_last_y(0.0) {}
60 void set_source(VertexSource
& source
) { m_source
= &source
; }
62 void approximation_scale(double s
)
64 m_curve3
.approximation_scale(s
);
65 m_curve4
.approximation_scale(s
);
68 double approximation_scale() const
70 return m_curve3
.approximation_scale();
73 void rewind(unsigned id
);
74 unsigned vertex(double* x
, double* y
);
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
); }
82 conv_curve(const conv_curve
<VertexSource
>&);
83 const conv_curve
<VertexSource
>&
84 operator = (const conv_curve
<VertexSource
>&);
86 VertexSource
* m_source
;
95 //------------------------------------------------------------------------
96 template<class VertexSource
>
97 void conv_curve
<VertexSource
>::rewind(unsigned id
)
107 //------------------------------------------------------------------------
108 template<class VertexSource
>
109 unsigned conv_curve
<VertexSource
>::vertex(double* x
, double* y
)
111 if(!is_stop(m_curve3
.vertex(x
, y
)))
115 return path_cmd_line_to
;
118 if(!is_stop(m_curve4
.vertex(x
, y
)))
122 return path_cmd_line_to
;
130 unsigned cmd
= m_source
->vertex(x
, y
);
133 case path_cmd_move_to
:
134 case path_cmd_line_to
:
140 case path_cmd_curve3
:
141 m_source
->vertex(&end_x
, &end_y
);
143 m_curve3
.init(m_last_x
, m_last_y
,
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
;
152 case path_cmd_curve4
:
153 m_source
->vertex(&ct2_x
, &ct2_y
);
154 m_source
->vertex(&end_x
, &end_y
);
156 m_curve4
.init(m_last_x
, m_last_y
,
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
;