edge-segments.h 5.0 KB

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