edge-segments.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /// Adjusts the bounding box to fit the edge segment.
  26. virtual void bounds(double &l, double &b, double &r, double &t) const = 0;
  27. /// Moves the start point of the edge segment.
  28. virtual void moveStartPoint(Point2 to) = 0;
  29. /// Moves the end point of the edge segment.
  30. virtual void moveEndPoint(Point2 to) = 0;
  31. /// Splits the edge segments into thirds which together represent the original edge.
  32. virtual void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const = 0;
  33. };
  34. /// A line segment.
  35. class LinearSegment : public EdgeSegment {
  36. public:
  37. Point2 p[2];
  38. LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
  39. LinearSegment * clone() const;
  40. Point2 point(double param) const;
  41. Vector2 direction(double param) const;
  42. SignedDistance signedDistance(Point2 origin, double &param) const;
  43. void bounds(double &l, double &b, double &r, double &t) const;
  44. void moveStartPoint(Point2 to);
  45. void moveEndPoint(Point2 to);
  46. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  47. };
  48. /// A quadratic Bezier curve.
  49. class QuadraticSegment : public EdgeSegment {
  50. public:
  51. Point2 p[3];
  52. QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
  53. QuadraticSegment * clone() const;
  54. Point2 point(double param) const;
  55. Vector2 direction(double param) const;
  56. SignedDistance signedDistance(Point2 origin, double &param) const;
  57. void bounds(double &l, double &b, double &r, double &t) const;
  58. void moveStartPoint(Point2 to);
  59. void moveEndPoint(Point2 to);
  60. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  61. };
  62. /// A cubic Bezier curve.
  63. class CubicSegment : public EdgeSegment {
  64. public:
  65. Point2 p[4];
  66. CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
  67. CubicSegment * clone() const;
  68. Point2 point(double param) const;
  69. Vector2 direction(double param) const;
  70. SignedDistance signedDistance(Point2 origin, double &param) const;
  71. void bounds(double &l, double &b, double &r, double &t) const;
  72. void moveStartPoint(Point2 to);
  73. void moveEndPoint(Point2 to);
  74. void splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const;
  75. };
  76. }