Shape.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "Shape.h"
  2. #include "arithmetics.hpp"
  3. namespace msdfgen {
  4. Shape::Shape() : inverseYAxis(false) { }
  5. void Shape::addContour(const Contour &contour) {
  6. contours.push_back(contour);
  7. }
  8. #ifdef MSDFGEN_USE_CPP11
  9. void Shape::addContour(Contour &&contour) {
  10. contours.push_back((Contour &&) contour);
  11. }
  12. #endif
  13. Contour & Shape::addContour() {
  14. contours.resize(contours.size()+1);
  15. return contours.back();
  16. }
  17. bool Shape::validate() const {
  18. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  19. if (!contour->edges.empty()) {
  20. Point2 corner = contour->edges.back()->point(1);
  21. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  22. if (!*edge)
  23. return false;
  24. if ((*edge)->point(0) != corner)
  25. return false;
  26. corner = (*edge)->point(1);
  27. }
  28. }
  29. }
  30. return true;
  31. }
  32. static void deconvergeEdge(EdgeHolder &edgeHolder, int param) {
  33. {
  34. const QuadraticSegment *quadraticSegment = dynamic_cast<const QuadraticSegment *>(&*edgeHolder);
  35. if (quadraticSegment)
  36. edgeHolder = quadraticSegment->convertToCubic();
  37. }
  38. {
  39. CubicSegment *cubicSegment = dynamic_cast<CubicSegment *>(&*edgeHolder);
  40. if (cubicSegment)
  41. cubicSegment->deconverge(param, MSDFGEN_DECONVERGENCE_FACTOR);
  42. }
  43. }
  44. void Shape::normalize() {
  45. for (std::vector<Contour>::iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  46. if (contour->edges.size() == 1) {
  47. EdgeSegment *parts[3] = { };
  48. contour->edges[0]->splitInThirds(parts[0], parts[1], parts[2]);
  49. contour->edges.clear();
  50. contour->edges.push_back(EdgeHolder(parts[0]));
  51. contour->edges.push_back(EdgeHolder(parts[1]));
  52. contour->edges.push_back(EdgeHolder(parts[2]));
  53. } else {
  54. EdgeHolder *prevEdge = &contour->edges.back();
  55. for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  56. Vector2 prevDir = (*prevEdge)->direction(1).normalize();
  57. Vector2 curDir = (*edge)->direction(0).normalize();
  58. if (dotProduct(prevDir, curDir) < MSDFGEN_CORNER_DOT_EPSILON-1) {
  59. deconvergeEdge(*prevEdge, 1);
  60. deconvergeEdge(*edge, 0);
  61. }
  62. prevEdge = &*edge;
  63. }
  64. }
  65. }
  66. }
  67. void Shape::bound(double &l, double &b, double &r, double &t) const {
  68. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  69. contour->bound(l, b, r, t);
  70. }
  71. void Shape::boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const {
  72. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  73. contour->boundMiters(l, b, r, t, border, miterLimit, polarity);
  74. }
  75. Shape::Bounds Shape::getBounds(double border, double miterLimit, int polarity) const {
  76. static const double LARGE_VALUE = 1e240;
  77. Shape::Bounds bounds = { +LARGE_VALUE, +LARGE_VALUE, -LARGE_VALUE, -LARGE_VALUE };
  78. bound(bounds.l, bounds.b, bounds.r, bounds.t);
  79. if (border > 0) {
  80. bounds.l -= border, bounds.b -= border;
  81. bounds.r += border, bounds.t += border;
  82. if (miterLimit > 0)
  83. boundMiters(bounds.l, bounds.b, bounds.r, bounds.t, border, miterLimit, polarity);
  84. }
  85. return bounds;
  86. }
  87. void Shape::scanline(Scanline &line, double y) const {
  88. std::vector<Scanline::Intersection> intersections;
  89. double x[3];
  90. int dy[3];
  91. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  92. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  93. int n = (*edge)->scanlineIntersections(x, dy, y);
  94. for (int i = 0; i < n; ++i) {
  95. Scanline::Intersection intersection = { x[i], dy[i] };
  96. intersections.push_back(intersection);
  97. }
  98. }
  99. }
  100. #ifdef MSDFGEN_USE_CPP11
  101. line.setIntersections((std::vector<Scanline::Intersection> &&) intersections);
  102. #else
  103. line.setIntersections(intersections);
  104. #endif
  105. }
  106. int Shape::edgeCount() const {
  107. int total = 0;
  108. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  109. total += (int) contour->edges.size();
  110. return total;
  111. }
  112. }