edge-segments.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 minimum signed distance between origin and the edge.
  22. virtual SignedDistance signedDistance(Point2 origin, double &param) const = 0;
  23. /// Converts a previously retrieved signed distance from origin to pseudo-distance.
  24. virtual void distanceToPseudoDistance(SignedDistance &distance, Point2 origin, double param) const;
  25. /// Outputs a list of (at most three) intersections (their X coordinates) with an infinite horizontal scanline at y and returns how many there are.
  26. virtual int scanlineIntersections(double x[3], int dy[3], double y) const = 0;
  27. /// Adjusts the bounding box to fit the edge segment.
  28. virtual void bound(double &l, double &b, double &r, double &t) const = 0;
  29. /// Moves the start point of the edge segment.
  30. virtual void moveStartPoint(Point2 to) = 0;
  31. /// Moves the end point of the edge segment.
  32. virtual void moveEndPoint(Point2 to) = 0;
  33. /// Splits the edge segments into thirds which together represent the original edge.
  34. virtual void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const = 0;
  35. };
  36. /// A line segment.
  37. class LinearSegment : public EdgeSegment {
  38. public:
  39. Point2 p[2];
  40. LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
  41. LinearSegment * clone() const;
  42. Point2 point(double param) const;
  43. Vector2 direction(double param) const;
  44. SignedDistance signedDistance(Point2 origin, double &param) const;
  45. int scanlineIntersections(double x[3], int dy[3], double y) const;
  46. void bound(double &l, double &b, double &r, double &t) const;
  47. void moveStartPoint(Point2 to);
  48. void moveEndPoint(Point2 to);
  49. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  50. };
  51. /// A quadratic Bezier curve.
  52. class QuadraticSegment : public EdgeSegment {
  53. public:
  54. Point2 p[3];
  55. QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
  56. QuadraticSegment * clone() const;
  57. Point2 point(double param) const;
  58. Vector2 direction(double param) 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 moveStartPoint(Point2 to);
  63. void moveEndPoint(Point2 to);
  64. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  65. };
  66. /// A cubic Bezier curve.
  67. class CubicSegment : public EdgeSegment {
  68. public:
  69. Point2 p[4];
  70. CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
  71. CubicSegment * clone() const;
  72. Point2 point(double param) const;
  73. Vector2 direction(double param) const;
  74. SignedDistance signedDistance(Point2 origin, double &param) const;
  75. int scanlineIntersections(double x[3], int dy[3], double y) const;
  76. void bound(double &l, double &b, double &r, double &t) const;
  77. void moveStartPoint(Point2 to);
  78. void moveEndPoint(Point2 to);
  79. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  80. };
  81. }