edge-segments.h 4.4 KB

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