edge-segments.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include "types.h"
  3. #include "Vector2.hpp"
  4. #include "SignedDistance.hpp"
  5. #include "EdgeColor.h"
  6. namespace msdfgen {
  7. /// An abstract edge segment.
  8. class EdgeSegment {
  9. public:
  10. EdgeColor color;
  11. EdgeSegment(EdgeColor edgeColor = WHITE) : color(edgeColor) { }
  12. virtual ~EdgeSegment() { }
  13. /// Creates a copy of the edge segment.
  14. virtual EdgeSegment *clone() const = 0;
  15. /// Returns the numeric code of the edge segment's type.
  16. virtual int type() const = 0;
  17. /// Returns the array of control points.
  18. virtual const Point2 *controlPoints() const = 0;
  19. /// Returns the point on the edge specified by the parameter (between 0 and 1).
  20. virtual Point2 point(real param) const = 0;
  21. /// Returns the direction the edge has at the point specified by the parameter.
  22. virtual Vector2 direction(real param) const = 0;
  23. /// Returns the change of direction (second derivative) at the point specified by the parameter.
  24. virtual Vector2 directionChange(real param) const = 0;
  25. /// Returns the minimum signed distance between origin and the edge.
  26. virtual SignedDistance signedDistance(Point2 origin, real &param) const = 0;
  27. /// Converts a previously retrieved signed distance from origin to pseudo-distance.
  28. virtual void distanceToPseudoDistance(SignedDistance &distance, Point2 origin, real param) const;
  29. /// Outputs a list of (at most three) intersections (their X coordinates) with an infinite horizontal scanline at y and returns how many there are.
  30. virtual int scanlineIntersections(real x[3], int dy[3], real y) const = 0;
  31. /// Adjusts the bounding box to fit the edge segment.
  32. virtual void bound(real &l, real &b, real &r, real &t) const = 0;
  33. /// Reverses the edge (swaps its start point and end point).
  34. virtual void reverse() = 0;
  35. /// Moves the start point of the edge segment.
  36. virtual void moveStartPoint(Point2 to) = 0;
  37. /// Moves the end point of the edge segment.
  38. virtual void moveEndPoint(Point2 to) = 0;
  39. /// Splits the edge segments into thirds which together represent the original edge.
  40. virtual void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const = 0;
  41. };
  42. /// A line segment.
  43. class LinearSegment : public EdgeSegment {
  44. public:
  45. enum EdgeType {
  46. EDGE_TYPE = 1
  47. };
  48. Point2 p[2];
  49. LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
  50. LinearSegment *clone() const;
  51. int type() const;
  52. const Point2 *controlPoints() const;
  53. Point2 point(real param) const;
  54. Vector2 direction(real param) const;
  55. Vector2 directionChange(real param) const;
  56. real length() const;
  57. SignedDistance signedDistance(Point2 origin, real &param) const;
  58. int scanlineIntersections(real x[3], int dy[3], real y) const;
  59. void bound(real &l, real &b, real &r, real &t) const;
  60. void reverse();
  61. void moveStartPoint(Point2 to);
  62. void moveEndPoint(Point2 to);
  63. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  64. };
  65. /// A quadratic Bezier curve.
  66. class QuadraticSegment : public EdgeSegment {
  67. public:
  68. enum EdgeType {
  69. EDGE_TYPE = 2
  70. };
  71. Point2 p[3];
  72. QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
  73. QuadraticSegment *clone() const;
  74. int type() const;
  75. const Point2 *controlPoints() const;
  76. Point2 point(real param) const;
  77. Vector2 direction(real param) const;
  78. Vector2 directionChange(real param) const;
  79. real length() const;
  80. SignedDistance signedDistance(Point2 origin, real &param) const;
  81. int scanlineIntersections(real x[3], int dy[3], real y) const;
  82. void bound(real &l, real &b, real &r, real &t) const;
  83. void reverse();
  84. void moveStartPoint(Point2 to);
  85. void moveEndPoint(Point2 to);
  86. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  87. EdgeSegment *convertToCubic() const;
  88. };
  89. /// A cubic Bezier curve.
  90. class CubicSegment : public EdgeSegment {
  91. public:
  92. enum EdgeType {
  93. EDGE_TYPE = 3
  94. };
  95. Point2 p[4];
  96. CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
  97. CubicSegment *clone() const;
  98. int type() const;
  99. const Point2 *controlPoints() const;
  100. Point2 point(real param) const;
  101. Vector2 direction(real param) const;
  102. Vector2 directionChange(real param) const;
  103. SignedDistance signedDistance(Point2 origin, real &param) const;
  104. int scanlineIntersections(real x[3], int dy[3], real y) const;
  105. void bound(real &l, real &b, real &r, real &t) const;
  106. void reverse();
  107. void moveStartPoint(Point2 to);
  108. void moveEndPoint(Point2 to);
  109. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  110. void deconverge(int param, real amount);
  111. };
  112. }