Scanline.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. Scanline::Scanline() : lastIndex(0) { }
  9. void Scanline::preprocess() {
  10. lastIndex = 0;
  11. if (!this->intersections.empty()) {
  12. qsort(&this->intersections[0], this->intersections.size(), sizeof(Intersection), compareIntersections);
  13. int totalDirection = 0;
  14. for (std::vector<Intersection>::iterator intersection = intersections.begin(); intersection != intersections.end(); ++intersection) {
  15. totalDirection += intersection->direction;
  16. intersection->direction = totalDirection;
  17. }
  18. }
  19. }
  20. void Scanline::setIntersections(const std::vector<Intersection> &intersections) {
  21. this->intersections = intersections;
  22. preprocess();
  23. }
  24. #ifdef MSDFGEN_USE_CPP11
  25. void Scanline::setIntersections(std::vector<Intersection> &&intersections) {
  26. this->intersections = (std::vector<Intersection> &&) intersections;
  27. preprocess();
  28. }
  29. #endif
  30. int Scanline::moveTo(double x) const {
  31. if (intersections.empty())
  32. return -1;
  33. int index = lastIndex;
  34. if (x < intersections[index].x) {
  35. do {
  36. if (index == 0) {
  37. lastIndex = 0;
  38. return -1;
  39. }
  40. --index;
  41. } while (x < intersections[index].x);
  42. } else {
  43. while (index < (int) intersections.size()-1 && x >= intersections[index+1].x)
  44. ++index;
  45. }
  46. lastIndex = index;
  47. return index;
  48. }
  49. int Scanline::countIntersections(double x) const {
  50. return moveTo(x)+1;
  51. }
  52. int Scanline::sumIntersections(double x) const {
  53. int index = moveTo(x);
  54. if (index >= 0)
  55. return intersections[index].direction;
  56. return 0;
  57. }
  58. }