edge-selectors.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include "Vector2.h"
  3. #include "SignedDistance.h"
  4. #include "edge-segments.h"
  5. namespace msdfgen {
  6. struct MultiDistance {
  7. double r, g, b;
  8. };
  9. struct MultiAndTrueDistance : MultiDistance {
  10. double a;
  11. };
  12. /// Selects the nearest edge by its true distance.
  13. class TrueDistanceSelector {
  14. public:
  15. typedef double DistanceType;
  16. explicit TrueDistanceSelector(const Point2 &p = Point2());
  17. void addEdge(const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge);
  18. void merge(const TrueDistanceSelector &other);
  19. DistanceType distance() const;
  20. private:
  21. Point2 p;
  22. SignedDistance minDistance;
  23. };
  24. class PseudoDistanceSelectorBase {
  25. public:
  26. static bool pointFacingEdge(const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge, const Point2 &p, double param);
  27. PseudoDistanceSelectorBase();
  28. void addEdgeTrueDistance(const EdgeSegment *edge, const SignedDistance &distance, double param);
  29. void addEdgePseudoDistance(const SignedDistance &distance);
  30. void merge(const PseudoDistanceSelectorBase &other);
  31. double computeDistance(const Point2 &p) const;
  32. SignedDistance trueDistance() const;
  33. private:
  34. SignedDistance minTrueDistance;
  35. SignedDistance minNegativePseudoDistance;
  36. SignedDistance minPositivePseudoDistance;
  37. const EdgeSegment *nearEdge;
  38. double nearEdgeParam;
  39. };
  40. /// Selects the nearest edge by its pseudo-distance.
  41. class PseudoDistanceSelector : public PseudoDistanceSelectorBase {
  42. public:
  43. typedef double DistanceType;
  44. explicit PseudoDistanceSelector(const Point2 &p = Point2());
  45. void addEdge(const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge);
  46. DistanceType distance() const;
  47. private:
  48. Point2 p;
  49. };
  50. /// Selects the nearest edge for each of the three channels by its pseudo-distance.
  51. class MultiDistanceSelector {
  52. public:
  53. typedef MultiDistance DistanceType;
  54. explicit MultiDistanceSelector(const Point2 &p = Point2());
  55. void addEdge(const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge);
  56. void merge(const MultiDistanceSelector &other);
  57. DistanceType distance() const;
  58. SignedDistance trueDistance() const;
  59. private:
  60. Point2 p;
  61. PseudoDistanceSelectorBase r, g, b;
  62. };
  63. /// Selects the nearest edge for each of the three color channels by its pseudo-distance and by true distance for the alpha channel.
  64. class MultiAndTrueDistanceSelector : public MultiDistanceSelector {
  65. public:
  66. typedef MultiAndTrueDistance DistanceType;
  67. explicit MultiAndTrueDistanceSelector(const Point2 &p = Point2());
  68. DistanceType distance() const;
  69. };
  70. }