2
0

Shape.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "Shape.h"
  2. #include <cstdlib>
  3. #include "arithmetics.hpp"
  4. namespace msdfgen {
  5. Shape::Shape() : inverseYAxis(false) { }
  6. void Shape::addContour(const Contour &contour) {
  7. contours.push_back(contour);
  8. }
  9. #ifdef MSDFGEN_USE_CPP11
  10. void Shape::addContour(Contour &&contour) {
  11. contours.push_back((Contour &&) contour);
  12. }
  13. #endif
  14. Contour &Shape::addContour() {
  15. contours.resize(contours.size()+1);
  16. return contours.back();
  17. }
  18. bool Shape::validate() const {
  19. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  20. if (!contour->edges.empty()) {
  21. Point2 corner = contour->edges.back()->point(1);
  22. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  23. if (!*edge)
  24. return false;
  25. if ((*edge)->point(0) != corner)
  26. return false;
  27. corner = (*edge)->point(1);
  28. }
  29. }
  30. }
  31. return true;
  32. }
  33. static void deconvergeEdge(EdgeHolder &edgeHolder, int param) {
  34. switch (edgeHolder->type()) {
  35. case (int) QuadraticSegment::EDGE_TYPE:
  36. edgeHolder = static_cast<const QuadraticSegment *>(&*edgeHolder)->convertToCubic();
  37. // fallthrough
  38. case (int) CubicSegment::EDGE_TYPE:
  39. static_cast<CubicSegment *>(&*edgeHolder)->deconverge(param, MSDFGEN_DECONVERGENCE_FACTOR);
  40. }
  41. }
  42. void Shape::normalize() {
  43. for (std::vector<Contour>::iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  44. if (contour->edges.size() == 1) {
  45. EdgeSegment *parts[3] = { };
  46. contour->edges[0]->splitInThirds(parts[0], parts[1], parts[2]);
  47. contour->edges.clear();
  48. contour->edges.push_back(EdgeHolder(parts[0]));
  49. contour->edges.push_back(EdgeHolder(parts[1]));
  50. contour->edges.push_back(EdgeHolder(parts[2]));
  51. } else {
  52. EdgeHolder *prevEdge = &contour->edges.back();
  53. for (std::vector<EdgeHolder>::iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  54. Vector2 prevDir = (*prevEdge)->direction(1).normalize();
  55. Vector2 curDir = (*edge)->direction(0).normalize();
  56. if (dotProduct(prevDir, curDir) < MSDFGEN_CORNER_DOT_EPSILON-1) {
  57. deconvergeEdge(*prevEdge, 1);
  58. deconvergeEdge(*edge, 0);
  59. }
  60. prevEdge = &*edge;
  61. }
  62. }
  63. }
  64. }
  65. void Shape::bound(double &l, double &b, double &r, double &t) const {
  66. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  67. contour->bound(l, b, r, t);
  68. }
  69. void Shape::boundMiters(double &l, double &b, double &r, double &t, double border, double miterLimit, int polarity) const {
  70. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  71. contour->boundMiters(l, b, r, t, border, miterLimit, polarity);
  72. }
  73. Shape::Bounds Shape::getBounds(double border, double miterLimit, int polarity) const {
  74. static const double LARGE_VALUE = 1e240;
  75. Shape::Bounds bounds = { +LARGE_VALUE, +LARGE_VALUE, -LARGE_VALUE, -LARGE_VALUE };
  76. bound(bounds.l, bounds.b, bounds.r, bounds.t);
  77. if (border > 0) {
  78. bounds.l -= border, bounds.b -= border;
  79. bounds.r += border, bounds.t += border;
  80. if (miterLimit > 0)
  81. boundMiters(bounds.l, bounds.b, bounds.r, bounds.t, border, miterLimit, polarity);
  82. }
  83. return bounds;
  84. }
  85. void Shape::scanline(Scanline &line, double y) const {
  86. std::vector<Scanline::Intersection> intersections;
  87. double x[3];
  88. int dy[3];
  89. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  90. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  91. int n = (*edge)->scanlineIntersections(x, dy, y);
  92. for (int i = 0; i < n; ++i) {
  93. Scanline::Intersection intersection = { x[i], dy[i] };
  94. intersections.push_back(intersection);
  95. }
  96. }
  97. }
  98. #ifdef MSDFGEN_USE_CPP11
  99. line.setIntersections((std::vector<Scanline::Intersection> &&) intersections);
  100. #else
  101. line.setIntersections(intersections);
  102. #endif
  103. }
  104. int Shape::edgeCount() const {
  105. int total = 0;
  106. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  107. total += (int) contour->edges.size();
  108. return total;
  109. }
  110. void Shape::orientContours() {
  111. struct Intersection {
  112. double x;
  113. int direction;
  114. int contourIndex;
  115. static int compare(const void *a, const void *b) {
  116. return sign(reinterpret_cast<const Intersection *>(a)->x-reinterpret_cast<const Intersection *>(b)->x);
  117. }
  118. };
  119. const double ratio = .5*(sqrt(5)-1); // an irrational number to minimize chance of intersecting a corner or other point of interest
  120. std::vector<int> orientations(contours.size());
  121. std::vector<Intersection> intersections;
  122. for (int i = 0; i < (int) contours.size(); ++i) {
  123. if (!orientations[i] && !contours[i].edges.empty()) {
  124. // Find an Y that crosses the contour
  125. double y0 = contours[i].edges.front()->point(0).y;
  126. double y1 = y0;
  127. for (std::vector<EdgeHolder>::const_iterator edge = contours[i].edges.begin(); edge != contours[i].edges.end() && y0 == y1; ++edge)
  128. y1 = (*edge)->point(1).y;
  129. for (std::vector<EdgeHolder>::const_iterator edge = contours[i].edges.begin(); edge != contours[i].edges.end() && y0 == y1; ++edge)
  130. y1 = (*edge)->point(ratio).y; // in case all endpoints are in a horizontal line
  131. double y = mix(y0, y1, ratio);
  132. // Scanline through whole shape at Y
  133. double x[3];
  134. int dy[3];
  135. for (int j = 0; j < (int) contours.size(); ++j) {
  136. for (std::vector<EdgeHolder>::const_iterator edge = contours[j].edges.begin(); edge != contours[j].edges.end(); ++edge) {
  137. int n = (*edge)->scanlineIntersections(x, dy, y);
  138. for (int k = 0; k < n; ++k) {
  139. Intersection intersection = { x[k], dy[k], j };
  140. intersections.push_back(intersection);
  141. }
  142. }
  143. }
  144. if (!intersections.empty()) {
  145. qsort(&intersections[0], intersections.size(), sizeof(Intersection), &Intersection::compare);
  146. // Disqualify multiple intersections
  147. for (int j = 1; j < (int) intersections.size(); ++j)
  148. if (intersections[j].x == intersections[j-1].x)
  149. intersections[j].direction = intersections[j-1].direction = 0;
  150. // Inspect scanline and deduce orientations of intersected contours
  151. for (int j = 0; j < (int) intersections.size(); ++j)
  152. if (intersections[j].direction)
  153. orientations[intersections[j].contourIndex] += 2*((j&1)^(intersections[j].direction > 0))-1;
  154. intersections.clear();
  155. }
  156. }
  157. }
  158. // Reverse contours that have the opposite orientation
  159. for (int i = 0; i < (int) contours.size(); ++i)
  160. if (orientations[i] < 0)
  161. contours[i].reverse();
  162. }
  163. }