agg_conv_curve.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifndef AGG_CONV_CURVE_INCLUDED
  20. #define AGG_CONV_CURVE_INCLUDED
  21. #include "agg_basics.h"
  22. #include "agg_curves.h"
  23. namespace agg
  24. {
  25. //---------------------------------------------------------------conv_curve
  26. // Curve converter class. Any path storage can have Bezier curves defined
  27. // by their control points. There're two types of curves supported: curve3
  28. // and curve4. Curve3 is a conic Bezier curve with 2 endpoints and 1 control
  29. // point. Curve4 has 2 control points (4 points in total) and can be used
  30. // to interpolate more complicated curves. Curve4, unlike curve3 can be used
  31. // to approximate arcs, both circular and elliptical. Curves are approximated
  32. // with straight lines and one of the approaches is just to store the whole
  33. // sequence of vertices that approximate our curve. It takes additional
  34. // memory, and at the same time the consecutive vertices can be calculated
  35. // on demand.
  36. //
  37. // Initially, path storages are not suppose to keep all the vertices of the
  38. // curves (although, nothing prevents us from doing so). Instead, path_storage
  39. // keeps only vertices, needed to calculate a curve on demand. Those vertices
  40. // are marked with special commands. So, if the path_storage contains curves
  41. // (which are not real curves yet), and we render this storage directly,
  42. // all we will see is only 2 or 3 straight line segments (for curve3 and
  43. // curve4 respectively). If we need to see real curves drawn we need to
  44. // include this class into the conversion pipeline.
  45. //
  46. // Class conv_curve recognizes commands path_cmd_curve3 and path_cmd_curve4
  47. // and converts these vertices into a move_to/line_to sequence.
  48. //-----------------------------------------------------------------------
  49. template<class VertexSource,
  50. class Curve3=curve3,
  51. class Curve4=curve4> class conv_curve
  52. {
  53. public:
  54. typedef Curve3 curve3_type;
  55. typedef Curve4 curve4_type;
  56. typedef conv_curve<VertexSource, Curve3, Curve4> self_type;
  57. explicit conv_curve(VertexSource& source) :
  58. m_source(&source), m_last_x(0.0), m_last_y(0.0) {}
  59. void attach(VertexSource& source) { m_source = &source; }
  60. void approximation_method(curve_approximation_method_e v)
  61. {
  62. m_curve3.approximation_method(v);
  63. m_curve4.approximation_method(v);
  64. }
  65. curve_approximation_method_e approximation_method() const
  66. {
  67. return m_curve4.approximation_method();
  68. }
  69. void approximation_scale(double s)
  70. {
  71. m_curve3.approximation_scale(s);
  72. m_curve4.approximation_scale(s);
  73. }
  74. double approximation_scale() const
  75. {
  76. return m_curve4.approximation_scale();
  77. }
  78. void angle_tolerance(double v)
  79. {
  80. m_curve3.angle_tolerance(v);
  81. m_curve4.angle_tolerance(v);
  82. }
  83. double angle_tolerance() const
  84. {
  85. return m_curve4.angle_tolerance();
  86. }
  87. void cusp_limit(double v)
  88. {
  89. m_curve3.cusp_limit(v);
  90. m_curve4.cusp_limit(v);
  91. }
  92. double cusp_limit() const
  93. {
  94. return m_curve4.cusp_limit();
  95. }
  96. void rewind(unsigned path_id);
  97. unsigned vertex(double* x, double* y);
  98. private:
  99. conv_curve(const self_type&);
  100. const self_type& operator = (const self_type&);
  101. VertexSource* m_source;
  102. double m_last_x;
  103. double m_last_y;
  104. curve3_type m_curve3;
  105. curve4_type m_curve4;
  106. };
  107. //------------------------------------------------------------------------
  108. template<class VertexSource, class Curve3, class Curve4>
  109. void conv_curve<VertexSource, Curve3, Curve4>::rewind(unsigned path_id)
  110. {
  111. m_source->rewind(path_id);
  112. m_last_x = 0.0;
  113. m_last_y = 0.0;
  114. m_curve3.reset();
  115. m_curve4.reset();
  116. }
  117. //------------------------------------------------------------------------
  118. template<class VertexSource, class Curve3, class Curve4>
  119. unsigned conv_curve<VertexSource, Curve3, Curve4>::vertex(double* x, double* y)
  120. {
  121. if(!is_stop(m_curve3.vertex(x, y)))
  122. {
  123. m_last_x = *x;
  124. m_last_y = *y;
  125. return path_cmd_line_to;
  126. }
  127. if(!is_stop(m_curve4.vertex(x, y)))
  128. {
  129. m_last_x = *x;
  130. m_last_y = *y;
  131. return path_cmd_line_to;
  132. }
  133. double ct2_x;
  134. double ct2_y;
  135. double end_x;
  136. double end_y;
  137. unsigned cmd = m_source->vertex(x, y);
  138. switch(cmd)
  139. {
  140. case path_cmd_curve3:
  141. m_source->vertex(&end_x, &end_y);
  142. m_curve3.init(m_last_x, m_last_y,
  143. *x, *y,
  144. end_x, end_y);
  145. m_curve3.vertex(x, y); // First call returns path_cmd_move_to
  146. m_curve3.vertex(x, y); // This is the first vertex of the curve
  147. cmd = path_cmd_line_to;
  148. break;
  149. case path_cmd_curve4:
  150. m_source->vertex(&ct2_x, &ct2_y);
  151. m_source->vertex(&end_x, &end_y);
  152. m_curve4.init(m_last_x, m_last_y,
  153. *x, *y,
  154. ct2_x, ct2_y,
  155. end_x, end_y);
  156. m_curve4.vertex(x, y); // First call returns path_cmd_move_to
  157. m_curve4.vertex(x, y); // This is the first vertex of the curve
  158. cmd = path_cmd_line_to;
  159. break;
  160. }
  161. m_last_x = *x;
  162. m_last_y = *y;
  163. return cmd;
  164. }
  165. }
  166. #endif