edge-segments.h 5.2 KB

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