edge-segments.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. p[0] = p0;
  36. p[1] = p1;
  37. p[2] = p2;
  38. }
  39. CubicSegment::CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  40. p[0] = p0;
  41. p[1] = p1;
  42. p[2] = p2;
  43. p[3] = p3;
  44. }
  45. LinearSegment * LinearSegment::clone() const {
  46. return new LinearSegment(p[0], p[1], color);
  47. }
  48. QuadraticSegment * QuadraticSegment::clone() const {
  49. return new QuadraticSegment(p[0], p[1], p[2], color);
  50. }
  51. CubicSegment * CubicSegment::clone() const {
  52. return new CubicSegment(p[0], p[1], p[2], p[3], color);
  53. }
  54. Point2 LinearSegment::point(double param) const {
  55. return mix(p[0], p[1], param);
  56. }
  57. Point2 QuadraticSegment::point(double param) const {
  58. return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);
  59. }
  60. Point2 CubicSegment::point(double param) const {
  61. Vector2 p12 = mix(p[1], p[2], param);
  62. return mix(mix(mix(p[0], p[1], param), p12, param), mix(p12, mix(p[2], p[3], param), param), param);
  63. }
  64. Vector2 LinearSegment::direction(double param) const {
  65. return p[1]-p[0];
  66. }
  67. Vector2 QuadraticSegment::direction(double param) const {
  68. return mix(p[1]-p[0], p[2]-p[1], param);
  69. }
  70. Vector2 CubicSegment::direction(double param) const {
  71. return mix(mix(p[1]-p[0], p[2]-p[1], param), mix(p[2]-p[1], p[3]-p[2], param), param);
  72. }
  73. SignedDistance LinearSegment::signedDistance(Point2 origin, double &param) const {
  74. Vector2 aq = origin-p[0];
  75. Vector2 ab = p[1]-p[0];
  76. param = dotProduct(aq, ab)/dotProduct(ab, ab);
  77. Vector2 eq = p[param > .5]-origin;
  78. double endpointDistance = eq.length();
  79. if (param > 0 && param < 1) {
  80. double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);
  81. if (fabs(orthoDistance) < endpointDistance)
  82. return SignedDistance(orthoDistance, 0);
  83. }
  84. return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));
  85. }
  86. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  87. Vector2 qa = p[0]-origin;
  88. Vector2 ab = p[1]-p[0];
  89. Vector2 br = p[0]+p[2]-p[1]-p[1];
  90. double a = dotProduct(br, br);
  91. double b = 3*dotProduct(ab, br);
  92. double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);
  93. double d = dotProduct(qa, ab);
  94. double t[3];
  95. int solutions = solveCubic(t, a, b, c, d);
  96. double minDistance = nonZeroSign(crossProduct(ab, qa))*qa.length(); // distance from A
  97. param = -dotProduct(qa, ab)/dotProduct(ab, ab);
  98. {
  99. double distance = nonZeroSign(crossProduct(p[2]-p[1], p[2]-origin))*(p[2]-origin).length(); // distance from B
  100. if (fabs(distance) < fabs(minDistance)) {
  101. minDistance = distance;
  102. param = dotProduct(origin-p[1], p[2]-p[1])/dotProduct(p[2]-p[1], p[2]-p[1]);
  103. }
  104. }
  105. for (int i = 0; i < solutions; ++i) {
  106. if (t[i] > 0 && t[i] < 1) {
  107. Point2 endpoint = p[0]+2*t[i]*ab+t[i]*t[i]*br;
  108. double distance = nonZeroSign(crossProduct(p[2]-p[0], endpoint-origin))*(endpoint-origin).length();
  109. if (fabs(distance) <= fabs(minDistance)) {
  110. minDistance = distance;
  111. param = t[i];
  112. }
  113. }
  114. }
  115. if (param >= 0 && param <= 1)
  116. return SignedDistance(minDistance, 0);
  117. if (param < .5)
  118. return SignedDistance(minDistance, fabs(dotProduct(ab.normalize(), qa.normalize())));
  119. else
  120. return SignedDistance(minDistance, fabs(dotProduct((p[2]-p[1]).normalize(), (p[2]-origin).normalize())));
  121. }
  122. SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  123. Vector2 qa = p[0]-origin;
  124. Vector2 ab = p[1]-p[0];
  125. Vector2 br = p[2]-p[1]-ab;
  126. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  127. double minDistance = nonZeroSign(crossProduct(ab, qa))*qa.length(); // distance from A
  128. param = -dotProduct(qa, ab)/dotProduct(ab, ab);
  129. {
  130. double distance = nonZeroSign(crossProduct(p[3]-p[2], p[3]-origin))*(p[3]-origin).length(); // distance from B
  131. if (fabs(distance) < fabs(minDistance)) {
  132. minDistance = distance;
  133. param = dotProduct(origin-p[2], p[3]-p[2])/dotProduct(p[3]-p[2], p[3]-p[2]);
  134. }
  135. }
  136. // Iterative minimum distance search
  137. for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {
  138. double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;
  139. for (int step = 0;; ++step) {
  140. Vector2 qpt = point(t)-origin;
  141. double distance = nonZeroSign(crossProduct(direction(t), qpt))*qpt.length();
  142. if (fabs(distance) < fabs(minDistance)) {
  143. minDistance = distance;
  144. param = t;
  145. }
  146. if (step == MSDFGEN_CUBIC_SEARCH_STEPS)
  147. break;
  148. // Improve t
  149. Vector2 d1 = 3*as*t*t+6*br*t+3*ab;
  150. Vector2 d2 = 6*as*t+6*br;
  151. t -= dotProduct(qpt, d1)/(dotProduct(d1, d1)+dotProduct(qpt, d2));
  152. if (t < 0 || t > 1)
  153. break;
  154. }
  155. }
  156. if (param >= 0 && param <= 1)
  157. return SignedDistance(minDistance, 0);
  158. if (param < .5)
  159. return SignedDistance(minDistance, fabs(dotProduct(ab.normalize(), qa.normalize())));
  160. else
  161. return SignedDistance(minDistance, fabs(dotProduct((p[3]-p[2]).normalize(), (p[3]-origin).normalize())));
  162. }
  163. // Original method by solving a fifth order polynomial
  164. /*SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  165. Vector2 qa = p[0]-origin;
  166. Vector2 ab = p[1]-p[0];
  167. Vector2 br = p[2]-p[1]-ab;
  168. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  169. double a = dotProduct(as, as);
  170. double b = 5*dotProduct(br, as);
  171. double c = 4*dotProduct(ab, as)+6*dotProduct(br, br);
  172. double d = 9*dotProduct(ab, br)+dotProduct(qa, as);
  173. double e = 3*dotProduct(ab, ab)+2*dotProduct(qa, br);
  174. double f = dotProduct(qa, ab);
  175. double t[5];
  176. int solutions = solveQuintic(t, a, b, c, d, e, f);
  177. double minDistance = nonZeroSign(crossProduct(ab, qa))*qa.length(); // distance from A
  178. param = -dotProduct(qa, ab)/dotProduct(ab, ab);
  179. {
  180. double distance = nonZeroSign(crossProduct(p[3]-p[2], p[3]-origin))*(p[3]-origin).length(); // distance from B
  181. if (fabs(distance) < fabs(minDistance)) {
  182. minDistance = distance;
  183. param = dotProduct(origin-p[2], p[3]-p[2])/dotProduct(p[3]-p[2], p[3]-p[2]);
  184. }
  185. }
  186. for (int i = 0; i < solutions; ++i) {
  187. if (t[i] > 0 && t[i] < 1) {
  188. Point2 endpoint = p[0]+3*t[i]*ab+3*t[i]*t[i]*br+t[i]*t[i]*t[i]*as;
  189. Vector2 dirVec = t[i]*t[i]*as+2*t[i]*br+ab;
  190. double distance = nonZeroSign(crossProduct(dirVec, endpoint-origin))*(endpoint-origin).length();
  191. if (fabs(distance) <= fabs(minDistance)) {
  192. minDistance = distance;
  193. param = t[i];
  194. }
  195. }
  196. }
  197. if (param >= 0 && param <= 1)
  198. return SignedDistance(minDistance, 0);
  199. if (param < .5)
  200. return SignedDistance(minDistance, fabs(dotProduct(ab.normalize(), qa.normalize())));
  201. else
  202. return SignedDistance(minDistance, fabs(dotProduct((p[3]-p[2]).normalize(), (p[3]-origin).normalize())));
  203. }*/
  204. static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {
  205. if (p.x < l) l = p.x;
  206. if (p.y < b) b = p.y;
  207. if (p.x > r) r = p.x;
  208. if (p.y > t) t = p.y;
  209. }
  210. void LinearSegment::bounds(double &l, double &b, double &r, double &t) const {
  211. pointBounds(p[0], l, b, r, t);
  212. pointBounds(p[1], l, b, r, t);
  213. }
  214. void QuadraticSegment::bounds(double &l, double &b, double &r, double &t) const {
  215. pointBounds(p[0], l, b, r, t);
  216. pointBounds(p[2], l, b, r, t);
  217. Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);
  218. if (bot.x) {
  219. double param = (p[1].x-p[0].x)/bot.x;
  220. if (param > 0 && param < 1)
  221. pointBounds(point(param), l, b, r, t);
  222. }
  223. if (bot.y) {
  224. double param = (p[1].y-p[0].y)/bot.y;
  225. if (param > 0 && param < 1)
  226. pointBounds(point(param), l, b, r, t);
  227. }
  228. }
  229. void CubicSegment::bounds(double &l, double &b, double &r, double &t) const {
  230. pointBounds(p[0], l, b, r, t);
  231. pointBounds(p[3], l, b, r, t);
  232. Vector2 a0 = p[1]-p[0];
  233. Vector2 a1 = 2*(p[2]-p[1]-a0);
  234. Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];
  235. double params[2];
  236. int solutions;
  237. solutions = solveQuadratic(params, a2.x, a1.x, a0.x);
  238. for (int i = 0; i < solutions; ++i)
  239. if (params[i] > 0 && params[i] < 1)
  240. pointBounds(point(params[i]), l, b, r, t);
  241. solutions = solveQuadratic(params, a2.y, a1.y, a0.y);
  242. for (int i = 0; i < solutions; ++i)
  243. if (params[i] > 0 && params[i] < 1)
  244. pointBounds(point(params[i]), l, b, r, t);
  245. }
  246. void LinearSegment::moveStartPoint(Point2 to) {
  247. p[0] = to;
  248. }
  249. void QuadraticSegment::moveStartPoint(Point2 to) {
  250. Vector2 origSDir = p[0]-p[1];
  251. Point2 origP1 = p[1];
  252. p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);
  253. p[0] = to;
  254. if (dotProduct(origSDir, p[0]-p[1]) < 0)
  255. p[1] = origP1;
  256. }
  257. void CubicSegment::moveStartPoint(Point2 to) {
  258. p[1] += to-p[0];
  259. p[0] = to;
  260. }
  261. void LinearSegment::moveEndPoint(Point2 to) {
  262. p[1] = to;
  263. }
  264. void QuadraticSegment::moveEndPoint(Point2 to) {
  265. Vector2 origEDir = p[2]-p[1];
  266. Point2 origP1 = p[1];
  267. p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);
  268. p[2] = to;
  269. if (dotProduct(origEDir, p[2]-p[1]) < 0)
  270. p[1] = origP1;
  271. }
  272. void CubicSegment::moveEndPoint(Point2 to) {
  273. p[2] += to-p[3];
  274. p[3] = to;
  275. }
  276. void LinearSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  277. part1 = new LinearSegment(p[0], point(1/3.), color);
  278. part2 = new LinearSegment(point(1/3.), point(2/3.), color);
  279. part3 = new LinearSegment(point(2/3.), p[1], color);
  280. }
  281. void QuadraticSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  282. part1 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);
  283. 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);
  284. part3 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);
  285. }
  286. void CubicSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  287. 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);
  288. part2 = new CubicSegment(point(1/3.),
  289. 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.),
  290. 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.),
  291. point(2/3.), color);
  292. 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);
  293. }
  294. }