edge-segments.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. // TODO can we get squared pseudo-distance without squaring it?
  12. double pseudoDistance = crossProduct(aq, dir);
  13. if (pseudoDistance*pseudoDistance <= fabs(distance.sqDistance)) {
  14. distance.sqDistance = pseudoDistance*fabs(pseudoDistance);
  15. distance.dot = 0;
  16. }
  17. }
  18. } else if (param > 1) {
  19. Vector2 dir = direction(1).normalize();
  20. Vector2 bq = origin-point(1);
  21. double ts = dotProduct(bq, dir);
  22. if (ts > 0) {
  23. // TODO can we get squared pseudo-distance without squaring it?
  24. double pseudoDistance = crossProduct(bq, dir);
  25. if (pseudoDistance*pseudoDistance <= fabs(distance.sqDistance)) {
  26. distance.sqDistance = pseudoDistance*fabs(pseudoDistance);
  27. distance.dot = 0;
  28. }
  29. }
  30. }
  31. }
  32. LinearSegment::LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  33. p[0] = p0;
  34. p[1] = p1;
  35. }
  36. QuadraticSegment::QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  37. if (p1 == p0 || p1 == p2)
  38. p1 = 0.5*(p0+p2);
  39. p[0] = p0;
  40. p[1] = p1;
  41. p[2] = p2;
  42. }
  43. CubicSegment::CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  44. p[0] = p0;
  45. p[1] = p1;
  46. p[2] = p2;
  47. p[3] = p3;
  48. }
  49. LinearSegment * LinearSegment::clone() const {
  50. return new LinearSegment(p[0], p[1], color);
  51. }
  52. QuadraticSegment * QuadraticSegment::clone() const {
  53. return new QuadraticSegment(p[0], p[1], p[2], color);
  54. }
  55. CubicSegment * CubicSegment::clone() const {
  56. return new CubicSegment(p[0], p[1], p[2], p[3], color);
  57. }
  58. Point2 LinearSegment::point(double param) const {
  59. return mix(p[0], p[1], param);
  60. }
  61. Point2 QuadraticSegment::point(double param) const {
  62. return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);
  63. }
  64. Point2 CubicSegment::point(double param) const {
  65. Vector2 p12 = mix(p[1], p[2], param);
  66. return mix(mix(mix(p[0], p[1], param), p12, param), mix(p12, mix(p[2], p[3], param), param), param);
  67. }
  68. Vector2 LinearSegment::direction(double param) const {
  69. return p[1]-p[0];
  70. }
  71. Vector2 QuadraticSegment::direction(double param) const {
  72. return mix(p[1]-p[0], p[2]-p[1], param);
  73. }
  74. Vector2 CubicSegment::direction(double param) const {
  75. Vector2 tangent = mix(mix(p[1]-p[0], p[2]-p[1], param), mix(p[2]-p[1], p[3]-p[2], param), param);
  76. if (!tangent) {
  77. if (param == 0) return p[2]-p[0];
  78. if (param == 1) return p[3]-p[1];
  79. }
  80. return tangent;
  81. }
  82. SignedDistance LinearSegment::signedDistance(Point2 origin, double &param) const {
  83. Vector2 aq = origin-p[0];
  84. Vector2 ab = p[1]-p[0];
  85. param = dotProduct(aq, ab)/dotProduct(ab, ab);
  86. double distanceSign = nonZeroSign(crossProduct(aq, ab));
  87. if (param > 0 && param < 1)
  88. return SignedDistance(distanceSign*(param*ab-aq).squaredLength(), 0);
  89. Vector2 qe = p[param > .5]-origin;
  90. return SignedDistance(distanceSign*qe.squaredLength(), fabs(dotProduct(ab.normalize(), qe.normalize())));
  91. }
  92. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  93. Vector2 qa = p[0]-origin;
  94. Vector2 ab = p[1]-p[0];
  95. Vector2 br = p[2]-p[1]-ab;
  96. double a = dotProduct(br, br);
  97. double b = 3*dotProduct(ab, br);
  98. double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);
  99. double d = dotProduct(qa, ab);
  100. double t[3];
  101. int solutions = solveCubic(t, a, b, c, d);
  102. double minSqDistance = nonZeroSign(crossProduct(ab, qa))*qa.squaredLength(); // distance from A
  103. param = -dotProduct(qa, ab)/dotProduct(ab, ab);
  104. {
  105. double sqDistance = nonZeroSign(crossProduct(p[2]-p[1], p[2]-origin))*(p[2]-origin).squaredLength(); // distance from B
  106. if (fabs(sqDistance) < fabs(minSqDistance)) {
  107. minSqDistance = sqDistance;
  108. param = dotProduct(origin-p[1], p[2]-p[1])/dotProduct(p[2]-p[1], p[2]-p[1]);
  109. }
  110. }
  111. for (int i = 0; i < solutions; ++i) {
  112. if (t[i] > 0 && t[i] < 1) {
  113. Vector2 qe = qa+2*t[i]*ab+t[i]*t[i]*br;
  114. double sqDistance = nonZeroSign(crossProduct(p[2]-p[0], qe))*qe.squaredLength();
  115. if (fabs(sqDistance) <= fabs(minSqDistance)) {
  116. minSqDistance = sqDistance;
  117. param = t[i];
  118. }
  119. }
  120. }
  121. if (param >= 0 && param <= 1)
  122. return SignedDistance(minSqDistance, 0);
  123. if (param < .5)
  124. return SignedDistance(minSqDistance, fabs(dotProduct(ab.normalize(), qa.normalize())));
  125. else
  126. return SignedDistance(minSqDistance, fabs(dotProduct((p[2]-p[1]).normalize(), (p[2]-origin).normalize())));
  127. }
  128. SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  129. Vector2 qa = p[0]-origin;
  130. Vector2 ab = p[1]-p[0];
  131. Vector2 br = p[2]-p[1]-ab;
  132. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  133. Vector2 epDir = direction(0);
  134. double minSqDistance = nonZeroSign(crossProduct(epDir, qa))*qa.squaredLength(); // distance from A
  135. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  136. {
  137. epDir = direction(1);
  138. double sqDistance = nonZeroSign(crossProduct(epDir, p[3]-origin))*(p[3]-origin).squaredLength(); // distance from B
  139. if (fabs(sqDistance) < fabs(minSqDistance)) {
  140. minSqDistance = sqDistance;
  141. param = dotProduct(origin+epDir-p[3], epDir)/dotProduct(epDir, epDir);
  142. }
  143. }
  144. // Iterative minimum distance search
  145. for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {
  146. double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;
  147. for (int step = 0;; ++step) {
  148. Vector2 qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
  149. double sqDistance = nonZeroSign(crossProduct(direction(t), qe))*qe.squaredLength();
  150. if (fabs(sqDistance) < fabs(minSqDistance)) {
  151. minSqDistance = sqDistance;
  152. param = t;
  153. }
  154. if (step == MSDFGEN_CUBIC_SEARCH_STEPS)
  155. break;
  156. // Improve t
  157. Vector2 d1 = 3*as*t*t+6*br*t+3*ab;
  158. Vector2 d2 = 6*as*t+6*br;
  159. t -= dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));
  160. if (t < 0 || t > 1)
  161. break;
  162. }
  163. }
  164. if (param >= 0 && param <= 1)
  165. return SignedDistance(minSqDistance, 0);
  166. if (param < .5)
  167. return SignedDistance(minSqDistance, fabs(dotProduct(ab.normalize(), qa.normalize())));
  168. else
  169. return SignedDistance(minSqDistance, fabs(dotProduct((p[3]-p[2]).normalize(), (p[3]-origin).normalize())));
  170. }
  171. int LinearSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  172. if ((y >= p[0].y && y < p[1].y) || (y >= p[1].y && y < p[0].y)) {
  173. double param = (y-p[0].y)/(p[1].y-p[0].y);
  174. x[0] = mix(p[0].x, p[1].x, param);
  175. dy[0] = sign(p[1].y-p[0].y);
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. int QuadraticSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  181. int total = 0;
  182. int nextDY = y > p[0].y ? 1 : -1;
  183. x[total] = p[0].x;
  184. if (p[0].y == y) {
  185. if (p[0].y < p[1].y || (p[0].y == p[1].y && p[0].y < p[2].y))
  186. dy[total++] = 1;
  187. else
  188. nextDY = 1;
  189. }
  190. {
  191. Vector2 ab = p[1]-p[0];
  192. Vector2 br = p[2]-p[1]-ab;
  193. double t[2];
  194. int solutions = solveQuadratic(t, br.y, 2*ab.y, p[0].y-y);
  195. // Sort solutions
  196. double tmp;
  197. if (solutions >= 2 && t[0] > t[1])
  198. tmp = t[0], t[0] = t[1], t[1] = tmp;
  199. for (int i = 0; i < solutions && total < 2; ++i) {
  200. if (t[i] > 0 && t[i] < 1) {
  201. x[total] = p[0].x+2*t[i]*ab.x+t[i]*t[i]*br.x;
  202. if (nextDY*(ab.y+t[i]*br.y) >= 0) {
  203. dy[total++] = nextDY;
  204. nextDY = -nextDY;
  205. }
  206. }
  207. }
  208. }
  209. if (p[2].y == y) {
  210. if (nextDY > 0 && total > 0) {
  211. --total;
  212. nextDY = -1;
  213. }
  214. if ((p[2].y < p[1].y || (p[2].y == p[1].y && p[2].y < p[0].y)) && total < 2) {
  215. x[total] = p[2].x;
  216. if (nextDY < 0) {
  217. dy[total++] = -1;
  218. nextDY = 1;
  219. }
  220. }
  221. }
  222. if (nextDY != (y >= p[2].y ? 1 : -1)) {
  223. if (total > 0)
  224. --total;
  225. else
  226. dy[total++] = nextDY;
  227. }
  228. return total;
  229. }
  230. int CubicSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  231. int total = 0;
  232. int nextDY = y > p[0].y ? 1 : -1;
  233. x[total] = p[0].x;
  234. if (p[0].y == y) {
  235. if (p[0].y < p[1].y || (p[0].y == p[1].y && (p[0].y < p[2].y || (p[0].y == p[2].y && p[0].y < p[3].y))))
  236. dy[total++] = 1;
  237. else
  238. nextDY = 1;
  239. }
  240. {
  241. Vector2 ab = p[1]-p[0];
  242. Vector2 br = p[2]-p[1]-ab;
  243. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  244. double t[3];
  245. int solutions = solveCubic(t, as.y, 3*br.y, 3*ab.y, p[0].y-y);
  246. // Sort solutions
  247. double tmp;
  248. if (solutions >= 2) {
  249. if (t[0] > t[1])
  250. tmp = t[0], t[0] = t[1], t[1] = tmp;
  251. if (solutions >= 3 && t[1] > t[2]) {
  252. tmp = t[1], t[1] = t[2], t[2] = tmp;
  253. if (t[0] > t[1])
  254. tmp = t[0], t[0] = t[1], t[1] = tmp;
  255. }
  256. }
  257. for (int i = 0; i < solutions && total < 3; ++i) {
  258. if (t[i] > 0 && t[i] < 1) {
  259. x[total] = p[0].x+3*t[i]*ab.x+3*t[i]*t[i]*br.x+t[i]*t[i]*t[i]*as.x;
  260. if (nextDY*(ab.y+2*t[i]*br.y+t[i]*t[i]*as.y) >= 0) {
  261. dy[total++] = nextDY;
  262. nextDY = -nextDY;
  263. }
  264. }
  265. }
  266. }
  267. if (p[3].y == y) {
  268. if (nextDY > 0 && total > 0) {
  269. --total;
  270. nextDY = -1;
  271. }
  272. if ((p[3].y < p[2].y || (p[3].y == p[2].y && (p[3].y < p[1].y || (p[3].y == p[1].y && p[3].y < p[0].y)))) && total < 3) {
  273. x[total] = p[3].x;
  274. if (nextDY < 0) {
  275. dy[total++] = -1;
  276. nextDY = 1;
  277. }
  278. }
  279. }
  280. if (nextDY != (y >= p[3].y ? 1 : -1)) {
  281. if (total > 0)
  282. --total;
  283. else
  284. dy[total++] = nextDY;
  285. }
  286. return total;
  287. }
  288. static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {
  289. if (p.x < l) l = p.x;
  290. if (p.y < b) b = p.y;
  291. if (p.x > r) r = p.x;
  292. if (p.y > t) t = p.y;
  293. }
  294. void LinearSegment::bounds(double &l, double &b, double &r, double &t) const {
  295. pointBounds(p[0], l, b, r, t);
  296. pointBounds(p[1], l, b, r, t);
  297. }
  298. void QuadraticSegment::bounds(double &l, double &b, double &r, double &t) const {
  299. pointBounds(p[0], l, b, r, t);
  300. pointBounds(p[2], l, b, r, t);
  301. Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);
  302. if (bot.x) {
  303. double param = (p[1].x-p[0].x)/bot.x;
  304. if (param > 0 && param < 1)
  305. pointBounds(point(param), l, b, r, t);
  306. }
  307. if (bot.y) {
  308. double param = (p[1].y-p[0].y)/bot.y;
  309. if (param > 0 && param < 1)
  310. pointBounds(point(param), l, b, r, t);
  311. }
  312. }
  313. void CubicSegment::bounds(double &l, double &b, double &r, double &t) const {
  314. pointBounds(p[0], l, b, r, t);
  315. pointBounds(p[3], l, b, r, t);
  316. Vector2 a0 = p[1]-p[0];
  317. Vector2 a1 = 2*(p[2]-p[1]-a0);
  318. Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];
  319. double params[2];
  320. int solutions;
  321. solutions = solveQuadratic(params, a2.x, a1.x, a0.x);
  322. for (int i = 0; i < solutions; ++i)
  323. if (params[i] > 0 && params[i] < 1)
  324. pointBounds(point(params[i]), l, b, r, t);
  325. solutions = solveQuadratic(params, a2.y, a1.y, a0.y);
  326. for (int i = 0; i < solutions; ++i)
  327. if (params[i] > 0 && params[i] < 1)
  328. pointBounds(point(params[i]), l, b, r, t);
  329. }
  330. void LinearSegment::moveStartPoint(Point2 to) {
  331. p[0] = to;
  332. }
  333. void QuadraticSegment::moveStartPoint(Point2 to) {
  334. Vector2 origSDir = p[0]-p[1];
  335. Point2 origP1 = p[1];
  336. p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);
  337. p[0] = to;
  338. if (dotProduct(origSDir, p[0]-p[1]) < 0)
  339. p[1] = origP1;
  340. }
  341. void CubicSegment::moveStartPoint(Point2 to) {
  342. p[1] += to-p[0];
  343. p[0] = to;
  344. }
  345. void LinearSegment::moveEndPoint(Point2 to) {
  346. p[1] = to;
  347. }
  348. void QuadraticSegment::moveEndPoint(Point2 to) {
  349. Vector2 origEDir = p[2]-p[1];
  350. Point2 origP1 = p[1];
  351. p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);
  352. p[2] = to;
  353. if (dotProduct(origEDir, p[2]-p[1]) < 0)
  354. p[1] = origP1;
  355. }
  356. void CubicSegment::moveEndPoint(Point2 to) {
  357. p[2] += to-p[3];
  358. p[3] = to;
  359. }
  360. void LinearSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  361. part1 = new LinearSegment(p[0], point(1/3.), color);
  362. part2 = new LinearSegment(point(1/3.), point(2/3.), color);
  363. part3 = new LinearSegment(point(2/3.), p[1], color);
  364. }
  365. void QuadraticSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  366. part1 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);
  367. 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);
  368. part3 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);
  369. }
  370. void CubicSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  371. part1 = new CubicSegment(p[0], p[0] == p[1] ? 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);
  372. part2 = new CubicSegment(point(1/3.),
  373. 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.),
  374. 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.),
  375. point(2/3.), color);
  376. part3 = new CubicSegment(point(2/3.), mix(mix(p[1], p[2], 2/3.), mix(p[2], p[3], 2/3.), 2/3.), p[2] == p[3] ? p[3] : mix(p[2], p[3], 2/3.), p[3], color);
  377. }
  378. }