edge-selectors.h 2.9 KB

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