Shape.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. void Shape::normalize() {
  33. for (std::vector<Contour>::iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  34. if (contour->edges.size() == 1) {
  35. EdgeSegment *parts[3] = { };
  36. contour->edges[0]->splitInThirds(parts[0], parts[1], parts[2]);
  37. contour->edges.clear();
  38. contour->edges.push_back(EdgeHolder(parts[0]));
  39. contour->edges.push_back(EdgeHolder(parts[1]));
  40. contour->edges.push_back(EdgeHolder(parts[2]));
  41. } else {
  42. EdgeHolder *prevEdge = &contour->edges.back();
  43. for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  44. Vector2 prevDir = (*prevEdge)->direction(1).normalize();
  45. Vector2 curDir = (*edge)->direction(0).normalize();
  46. if (dotProduct(prevDir, curDir) < MSDFGEN_CORNER_DOT_EPSILON-1) {
  47. Vector2 prevTendency = (*prevEdge)->directionTendency(1, 1);
  48. Vector2 curTendency = (*edge)->directionTendency(0, -1);
  49. int winding = nonZeroSign(crossProduct(prevTendency, curTendency));
  50. EdgeSegment *newEdge = (*prevEdge)->makeDivergent(0, winding);
  51. if (newEdge) *prevEdge = newEdge;
  52. newEdge = (*edge)->makeDivergent(winding, 0);
  53. if (newEdge) *edge = newEdge;
  54. }
  55. prevEdge = &*edge;
  56. }
  57. }
  58. }
  59. }
  60. void Shape::bound(double &l, double &b, double &r, double &t) const {
  61. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  62. contour->bound(l, b, r, t);
  63. }
  64. void Shape::boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const {
  65. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  66. contour->boundMiters(l, b, r, t, border, miterLimit, polarity);
  67. }
  68. Shape::Bounds Shape::getBounds(double border, double miterLimit, int polarity) const {
  69. static const double LARGE_VALUE = 1e240;
  70. Shape::Bounds bounds = { +LARGE_VALUE, +LARGE_VALUE, -LARGE_VALUE, -LARGE_VALUE };
  71. bound(bounds.l, bounds.b, bounds.r, bounds.t);
  72. if (border > 0) {
  73. bounds.l -= border, bounds.b -= border;
  74. bounds.r += border, bounds.t += border;
  75. if (miterLimit > 0)
  76. boundMiters(bounds.l, bounds.b, bounds.r, bounds.t, border, miterLimit, polarity);
  77. }
  78. return bounds;
  79. }
  80. void Shape::scanline(Scanline &line, double y) const {
  81. std::vector<Scanline::Intersection> intersections;
  82. double x[3];
  83. int dy[3];
  84. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  85. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  86. int n = (*edge)->scanlineIntersections(x, dy, y);
  87. for (int i = 0; i < n; ++i) {
  88. Scanline::Intersection intersection = { x[i], dy[i] };
  89. intersections.push_back(intersection);
  90. }
  91. }
  92. }
  93. #ifdef MSDFGEN_USE_CPP11
  94. line.setIntersections((std::vector<Scanline::Intersection> &&) intersections);
  95. #else
  96. line.setIntersections(intersections);
  97. #endif
  98. }
  99. int Shape::edgeCount() const {
  100. int total = 0;
  101. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  102. total += (int) contour->edges.size();
  103. return total;
  104. }
  105. }