Shape.cpp 8.4 KB

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