update dev300-m58
[ooovba.git] / agg / inc / agg_conv_curve.h
blob4ff49ee8f65e46ee76735693a433f535a6d022c7
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
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 //----------------------------------------------------------------------------
16 // classes conv_curve
18 //----------------------------------------------------------------------------
20 #ifndef AGG_CONV_CURVE_INCLUDED
21 #define AGG_CONV_CURVE_INCLUDED
23 #include "agg_basics.h"
24 #include "agg_curves.h"
26 namespace agg
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.
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
56 public:
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); }
81 private:
82 conv_curve(const conv_curve<VertexSource>&);
83 const conv_curve<VertexSource>&
84 operator = (const conv_curve<VertexSource>&);
86 VertexSource* m_source;
87 double m_last_x;
88 double m_last_y;
89 curve3 m_curve3;
90 curve4 m_curve4;
95 //------------------------------------------------------------------------
96 template<class VertexSource>
97 void conv_curve<VertexSource>::rewind(unsigned id)
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();
107 //------------------------------------------------------------------------
108 template<class VertexSource>
109 unsigned conv_curve<VertexSource>::vertex(double* x, double* y)
111 if(!is_stop(m_curve3.vertex(x, y)))
113 m_last_x = *x;
114 m_last_y = *y;
115 return path_cmd_line_to;
118 if(!is_stop(m_curve4.vertex(x, y)))
120 m_last_x = *x;
121 m_last_y = *y;
122 return path_cmd_line_to;
125 double ct2_x = 0;
126 double ct2_y = 0;
127 double end_x = 0;
128 double end_y = 0;
130 unsigned cmd = m_source->vertex(x, y);
131 switch(cmd)
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;
140 case path_cmd_curve3:
141 m_source->vertex(&end_x, &end_y);
143 m_curve3.init(m_last_x, m_last_y,
144 *x, *y,
145 end_x, end_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;
150 break;
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,
157 *x, *y,
158 ct2_x, ct2_y,
159 end_x, end_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;
164 break;
166 return cmd;
174 #endif