edge-segments.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "edge-segments.h"
  2. #include "arithmetics.hpp"
  3. #include "equation-solver.h"
  4. namespace msdfgen {
  5. void EdgeSegment::distanceToPseudoDistance(SignedDistance &distance, Point2 origin, double param) const {
  6. if (param < 0) {
  7. Vector2 dir = direction(0).normalize();
  8. Vector2 aq = origin-point(0);
  9. double ts = dotProduct(aq, dir);
  10. if (ts < 0) {
  11. double pseudoDistance = crossProduct(aq, dir);
  12. if (fabs(pseudoDistance) <= fabs(distance.distance)) {
  13. distance.distance = pseudoDistance;
  14. distance.dot = 0;
  15. }
  16. }
  17. } else if (param > 1) {
  18. Vector2 dir = direction(1).normalize();
  19. Vector2 bq = origin-point(1);
  20. double ts = dotProduct(bq, dir);
  21. if (ts > 0) {
  22. double pseudoDistance = crossProduct(bq, dir);
  23. if (fabs(pseudoDistance) <= fabs(distance.distance)) {
  24. distance.distance = pseudoDistance;
  25. distance.dot = 0;
  26. }
  27. }
  28. }
  29. }
  30. LinearSegment::LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  31. p[0] = p0;
  32. p[1] = p1;
  33. }
  34. QuadraticSegment::QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  35. if (p1 == p0 || p1 == p2)
  36. p1 = 0.5*(p0+p2);
  37. p[0] = p0;
  38. p[1] = p1;
  39. p[2] = p2;
  40. }
  41. CubicSegment::CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  42. p[0] = p0;
  43. p[1] = p1;
  44. p[2] = p2;
  45. p[3] = p3;
  46. }
  47. LinearSegment * LinearSegment::clone() const {
  48. return new LinearSegment(p[0], p[1], color);
  49. }
  50. QuadraticSegment * QuadraticSegment::clone() const {
  51. return new QuadraticSegment(p[0], p[1], p[2], color);
  52. }
  53. CubicSegment * CubicSegment::clone() const {
  54. return new CubicSegment(p[0], p[1], p[2], p[3], color);
  55. }
  56. Point2 LinearSegment::point(double param) const {
  57. return mix(p[0], p[1], param);
  58. }
  59. Point2 QuadraticSegment::point(double param) const {
  60. return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);
  61. }
  62. Point2 CubicSegment::point(double param) const {
  63. Vector2 p12 = mix(p[1], p[2], param);
  64. return mix(mix(mix(p[0], p[1], param), p12, param), mix(p12, mix(p[2], p[3], param), param), param);
  65. }
  66. Vector2 LinearSegment::direction(double param) const {
  67. return p[1]-p[0];
  68. }
  69. Vector2 QuadraticSegment::direction(double param) const {
  70. return mix(p[1]-p[0], p[2]-p[1], param);
  71. }
  72. Vector2 CubicSegment::direction(double param) const {
  73. Vector2 tangent = mix(mix(p[1]-p[0], p[2]-p[1], param), mix(p[2]-p[1], p[3]-p[2], param), param);
  74. if (!tangent) {
  75. if (param == 0) return p[2]-p[0];
  76. if (param == 1) return p[3]-p[1];
  77. }
  78. return tangent;
  79. }
  80. SignedDistance LinearSegment::signedDistance(Point2 origin, double &param) const {
  81. Vector2 aq = origin-p[0];
  82. Vector2 ab = p[1]-p[0];
  83. param = dotProduct(aq, ab)/dotProduct(ab, ab);
  84. Vector2 eq = p[param > .5]-origin;
  85. double endpointDistance = eq.length();
  86. if (param > 0 && param < 1) {
  87. double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);
  88. if (fabs(orthoDistance) < endpointDistance)
  89. return SignedDistance(orthoDistance, 0);
  90. }
  91. return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));
  92. }
  93. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  94. Vector2 qa = p[0]-origin;
  95. Vector2 ab = p[1]-p[0];
  96. Vector2 br = p[0]+p[2]-p[1]-p[1];
  97. double a = dotProduct(br, br);
  98. double b = 3*dotProduct(ab, br);
  99. double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);
  100. double d = dotProduct(qa, ab);
  101. double t[3];
  102. int solutions = solveCubic(t, a, b, c, d);
  103. double minDistance = nonZeroSign(crossProduct(ab, qa))*qa.length(); // distance from A
  104. param = -dotProduct(qa, ab)/dotProduct(ab, ab);
  105. {
  106. double distance = nonZeroSign(crossProduct(p[2]-p[1], p[2]-origin))*(p[2]-origin).length(); // distance from B
  107. if (fabs(distance) < fabs(minDistance)) {
  108. minDistance = distance;
  109. param = dotProduct(origin-p[1], p[2]-p[1])/dotProduct(p[2]-p[1], p[2]-p[1]);
  110. }
  111. }
  112. for (int i = 0; i < solutions; ++i) {
  113. if (t[i] > 0 && t[i] < 1) {
  114. Point2 endpoint = p[0]+2*t[i]*ab+t[i]*t[i]*br;
  115. double distance = nonZeroSign(crossProduct(p[2]-p[0], endpoint-origin))*(endpoint-origin).length();
  116. if (fabs(distance) <= fabs(minDistance)) {
  117. minDistance = distance;
  118. param = t[i];
  119. }
  120. }
  121. }
  122. if (param >= 0 && param <= 1)
  123. return SignedDistance(minDistance, 0);
  124. if (param < .5)
  125. return SignedDistance(minDistance, fabs(dotProduct(ab.normalize(), qa.normalize())));
  126. else
  127. return SignedDistance(minDistance, fabs(dotProduct((p[2]-p[1]).normalize(), (p[2]-origin).normalize())));
  128. }
  129. SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  130. Vector2 qa = p[0]-origin;
  131. Vector2 ab = p[1]-p[0];
  132. Vector2 br = p[2]-p[1]-ab;
  133. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  134. Vector2 epDir = direction(0);
  135. double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
  136. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  137. {
  138. epDir = direction(1);
  139. double distance = nonZeroSign(crossProduct(epDir, p[3]-origin))*(p[3]-origin).length(); // distance from B
  140. if (fabs(distance) < fabs(minDistance)) {
  141. minDistance = distance;
  142. param = dotProduct(origin+epDir-p[3], epDir)/dotProduct(epDir, epDir);
  143. }
  144. }
  145. // Iterative minimum distance search
  146. for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {
  147. double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;
  148. for (int step = 0;; ++step) {
  149. Vector2 qpt = point(t)-origin;
  150. double distance = nonZeroSign(crossProduct(direction(t), qpt))*qpt.length();
  151. if (fabs(distance) < fabs(minDistance)) {
  152. minDistance = distance;
  153. param = t;
  154. }
  155. if (step == MSDFGEN_CUBIC_SEARCH_STEPS)
  156. break;
  157. // Improve t
  158. Vector2 d1 = 3*as*t*t+6*br*t+3*ab;
  159. Vector2 d2 = 6*as*t+6*br;
  160. t -= dotProduct(qpt, d1)/(dotProduct(d1, d1)+dotProduct(qpt, d2));
  161. if (t < 0 || t > 1)
  162. break;
  163. }
  164. }
  165. if (param >= 0 && param <= 1)
  166. return SignedDistance(minDistance, 0);
  167. if (param < .5)
  168. return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));
  169. else
  170. return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[3]-origin).normalize())));
  171. }
  172. static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {
  173. if (p.x < l) l = p.x;
  174. if (p.y < b) b = p.y;
  175. if (p.x > r) r = p.x;
  176. if (p.y > t) t = p.y;
  177. }
  178. void LinearSegment::bounds(double &l, double &b, double &r, double &t) const {
  179. pointBounds(p[0], l, b, r, t);
  180. pointBounds(p[1], l, b, r, t);
  181. }
  182. void QuadraticSegment::bounds(double &l, double &b, double &r, double &t) const {
  183. pointBounds(p[0], l, b, r, t);
  184. pointBounds(p[2], l, b, r, t);
  185. Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);
  186. if (bot.x) {
  187. double param = (p[1].x-p[0].x)/bot.x;
  188. if (param > 0 && param < 1)
  189. pointBounds(point(param), l, b, r, t);
  190. }
  191. if (bot.y) {
  192. double param = (p[1].y-p[0].y)/bot.y;
  193. if (param > 0 && param < 1)
  194. pointBounds(point(param), l, b, r, t);
  195. }
  196. }
  197. void CubicSegment::bounds(double &l, double &b, double &r, double &t) const {
  198. pointBounds(p[0], l, b, r, t);
  199. pointBounds(p[3], l, b, r, t);
  200. Vector2 a0 = p[1]-p[0];
  201. Vector2 a1 = 2*(p[2]-p[1]-a0);
  202. Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];
  203. double params[2];
  204. int solutions;
  205. solutions = solveQuadratic(params, a2.x, a1.x, a0.x);
  206. for (int i = 0; i < solutions; ++i)
  207. if (params[i] > 0 && params[i] < 1)
  208. pointBounds(point(params[i]), l, b, r, t);
  209. solutions = solveQuadratic(params, a2.y, a1.y, a0.y);
  210. for (int i = 0; i < solutions; ++i)
  211. if (params[i] > 0 && params[i] < 1)
  212. pointBounds(point(params[i]), l, b, r, t);
  213. }
  214. void LinearSegment::moveStartPoint(Point2 to) {
  215. p[0] = to;
  216. }
  217. void QuadraticSegment::moveStartPoint(Point2 to) {
  218. Vector2 origSDir = p[0]-p[1];
  219. Point2 origP1 = p[1];
  220. p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);
  221. p[0] = to;
  222. if (dotProduct(origSDir, p[0]-p[1]) < 0)
  223. p[1] = origP1;
  224. }
  225. void CubicSegment::moveStartPoint(Point2 to) {
  226. p[1] += to-p[0];
  227. p[0] = to;
  228. }
  229. void LinearSegment::moveEndPoint(Point2 to) {
  230. p[1] = to;
  231. }
  232. void QuadraticSegment::moveEndPoint(Point2 to) {
  233. Vector2 origEDir = p[2]-p[1];
  234. Point2 origP1 = p[1];
  235. p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);
  236. p[2] = to;
  237. if (dotProduct(origEDir, p[2]-p[1]) < 0)
  238. p[1] = origP1;
  239. }
  240. void CubicSegment::moveEndPoint(Point2 to) {
  241. p[2] += to-p[3];
  242. p[3] = to;
  243. }
  244. void LinearSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  245. part1 = new LinearSegment(p[0], point(1/3.), color);
  246. part2 = new LinearSegment(point(1/3.), point(2/3.), color);
  247. part3 = new LinearSegment(point(2/3.), p[1], color);
  248. }
  249. void QuadraticSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  250. part1 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);
  251. part2 = new QuadraticSegment(point(1/3.), mix(mix(p[0], p[1], 5/9.), mix(p[1], p[2], 4/9.), .5), point(2/3.), color);
  252. part3 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);
  253. }
  254. void CubicSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  255. part1 = new CubicSegment(p[0], mix(p[0], p[1], 1/3.), mix(mix(p[0], p[1], 1/3.), mix(p[1], p[2], 1/3.), 1/3.), point(1/3.), color);
  256. part2 = new CubicSegment(point(1/3.),
  257. mix(mix(mix(p[0], p[1], 1/3.), mix(p[1], p[2], 1/3.), 1/3.), mix(mix(p[1], p[2], 1/3.), mix(p[2], p[3], 1/3.), 1/3.), 2/3.),
  258. mix(mix(mix(p[0], p[1], 2/3.), mix(p[1], p[2], 2/3.), 2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), 1/3.),
  259. point(2/3.), color);
  260. part3 = new CubicSegment(point(2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), mix(p[2], p[3], 2/3.), p[3], color);
  261. }
  262. }