edge-selectors.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. struct EdgeCache {
  17. Point2 point;
  18. double absDistance;
  19. EdgeCache();
  20. };
  21. void reset(const Point2 &p);
  22. void addEdge(EdgeCache &cache, const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge);
  23. void merge(const TrueDistanceSelector &other);
  24. DistanceType distance() const;
  25. private:
  26. Point2 p;
  27. SignedDistance minDistance;
  28. };
  29. class PseudoDistanceSelectorBase {
  30. public:
  31. struct EdgeCache {
  32. Point2 point;
  33. double absDistance;
  34. double aDomainDistance, bDomainDistance;
  35. double aPseudoDistance, bPseudoDistance;
  36. EdgeCache();
  37. };
  38. static bool getPseudoDistance(double &distance, const Vector2 &ep, const Vector2 &edgeDir);
  39. PseudoDistanceSelectorBase();
  40. void reset(double delta);
  41. bool isEdgeRelevant(const EdgeCache &cache, const EdgeSegment *edge, const Point2 &p) const;
  42. void addEdgeTrueDistance(const EdgeSegment *edge, const SignedDistance &distance, double param);
  43. void addEdgePseudoDistance(double distance);
  44. void merge(const PseudoDistanceSelectorBase &other);
  45. double computeDistance(const Point2 &p) const;
  46. SignedDistance trueDistance() const;
  47. private:
  48. SignedDistance minTrueDistance;
  49. double minNegativePseudoDistance;
  50. double minPositivePseudoDistance;
  51. const EdgeSegment *nearEdge;
  52. double nearEdgeParam;
  53. };
  54. /// Selects the nearest edge by its pseudo-distance.
  55. class PseudoDistanceSelector : public PseudoDistanceSelectorBase {
  56. public:
  57. typedef double DistanceType;
  58. void reset(const Point2 &p);
  59. void addEdge(EdgeCache &cache, const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge);
  60. DistanceType distance() const;
  61. private:
  62. Point2 p;
  63. };
  64. /// Selects the nearest edge for each of the three channels by its pseudo-distance.
  65. class MultiDistanceSelector {
  66. public:
  67. typedef MultiDistance DistanceType;
  68. typedef PseudoDistanceSelectorBase::EdgeCache EdgeCache;
  69. void reset(const Point2 &p);
  70. void addEdge(EdgeCache &cache, const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge);
  71. void merge(const MultiDistanceSelector &other);
  72. DistanceType distance() const;
  73. SignedDistance trueDistance() const;
  74. private:
  75. Point2 p;
  76. PseudoDistanceSelectorBase r, g, b;
  77. };
  78. /// Selects the nearest edge for each of the three color channels by its pseudo-distance and by true distance for the alpha channel.
  79. class MultiAndTrueDistanceSelector : public MultiDistanceSelector {
  80. public:
  81. typedef MultiAndTrueDistance DistanceType;
  82. DistanceType distance() const;
  83. };
  84. }