Scanline.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "Scanline.h"
  2. #include <algorithm>
  3. #include "arithmetics.hpp"
  4. namespace msdfgen {
  5. static int compareIntersections(const void *a, const void *b) {
  6. return sign(reinterpret_cast<const Scanline::Intersection *>(a)->x-reinterpret_cast<const Scanline::Intersection *>(b)->x);
  7. }
  8. bool interpretFillRule(int intersections, FillRule fillRule) {
  9. switch (fillRule) {
  10. case FILL_NONZERO:
  11. return intersections != 0;
  12. case FILL_ODD:
  13. return intersections&1;
  14. case FILL_POSITIVE:
  15. return intersections > 0;
  16. case FILL_NEGATIVE:
  17. return intersections < 0;
  18. }
  19. return false;
  20. }
  21. double Scanline::overlap(const Scanline &a, const Scanline &b, double xFrom, double xTo, FillRule fillRule) {
  22. double total = 0;
  23. bool aInside = false, bInside = false;
  24. int ai = 0, bi = 0;
  25. double ax = !a.intersections.empty() ? a.intersections[ai].x : xTo;
  26. double bx = !b.intersections.empty() ? b.intersections[bi].x : xTo;
  27. while (ax < xFrom || bx < xFrom) {
  28. double xNext = min(ax, bx);
  29. if (ax == xNext && ai < (int) a.intersections.size()) {
  30. aInside = interpretFillRule(a.intersections[ai].direction, fillRule);
  31. ax = ++ai < (int) a.intersections.size() ? a.intersections[ai].x : xTo;
  32. }
  33. if (bx == xNext && bi < (int) b.intersections.size()) {
  34. bInside = interpretFillRule(b.intersections[bi].direction, fillRule);
  35. bx = ++bi < (int) b.intersections.size() ? b.intersections[bi].x : xTo;
  36. }
  37. }
  38. double x = xFrom;
  39. while (ax < xTo || bx < xTo) {
  40. double xNext = min(ax, bx);
  41. if (aInside == bInside)
  42. total += xNext-x;
  43. if (ax == xNext && ai < (int) a.intersections.size()) {
  44. aInside = interpretFillRule(a.intersections[ai].direction, fillRule);
  45. ax = ++ai < (int) a.intersections.size() ? a.intersections[ai].x : xTo;
  46. }
  47. if (bx == xNext && bi < (int) b.intersections.size()) {
  48. bInside = interpretFillRule(b.intersections[bi].direction, fillRule);
  49. bx = ++bi < (int) b.intersections.size() ? b.intersections[bi].x : xTo;
  50. }
  51. x = xNext;
  52. }
  53. if (aInside == bInside)
  54. total += xTo-x;
  55. return total;
  56. }
  57. Scanline::Scanline() : lastIndex(0) { }
  58. void Scanline::preprocess() {
  59. lastIndex = 0;
  60. if (!intersections.empty()) {
  61. qsort(&intersections[0], intersections.size(), sizeof(Intersection), compareIntersections);
  62. int totalDirection = 0;
  63. for (std::vector<Intersection>::iterator intersection = intersections.begin(); intersection != intersections.end(); ++intersection) {
  64. totalDirection += intersection->direction;
  65. intersection->direction = totalDirection;
  66. }
  67. }
  68. }
  69. void Scanline::setIntersections(const std::vector<Intersection> &intersections) {
  70. this->intersections = intersections;
  71. preprocess();
  72. }
  73. #ifdef MSDFGEN_USE_CPP11
  74. void Scanline::setIntersections(std::vector<Intersection> &&intersections) {
  75. this->intersections = (std::vector<Intersection> &&) intersections;
  76. preprocess();
  77. }
  78. #endif
  79. int Scanline::moveTo(double x) const {
  80. if (intersections.empty())
  81. return -1;
  82. int index = lastIndex;
  83. if (x < intersections[index].x) {
  84. do {
  85. if (index == 0) {
  86. lastIndex = 0;
  87. return -1;
  88. }
  89. --index;
  90. } while (x < intersections[index].x);
  91. } else {
  92. while (index < (int) intersections.size()-1 && x >= intersections[index+1].x)
  93. ++index;
  94. }
  95. lastIndex = index;
  96. return index;
  97. }
  98. int Scanline::countIntersections(double x) const {
  99. return moveTo(x)+1;
  100. }
  101. int Scanline::sumIntersections(double x) const {
  102. int index = moveTo(x);
  103. if (index >= 0)
  104. return intersections[index].direction;
  105. return 0;
  106. }
  107. bool Scanline::filled(double x, FillRule fillRule) const {
  108. return interpretFillRule(sumIntersections(x), fillRule);
  109. }
  110. }