edge-segments.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. if ((p1 == p0 || p1 == p3) && (p2 == p0 || p2 == p3)) {
  43. p1 = mix(p0, p3, 1/3.);
  44. p2 = mix(p0, p3, 2/3.);
  45. }
  46. p[0] = p0;
  47. p[1] = p1;
  48. p[2] = p2;
  49. p[3] = p3;
  50. }
  51. LinearSegment *LinearSegment::clone() const {
  52. return new LinearSegment(p[0], p[1], color);
  53. }
  54. QuadraticSegment *QuadraticSegment::clone() const {
  55. return new QuadraticSegment(p[0], p[1], p[2], color);
  56. }
  57. CubicSegment *CubicSegment::clone() const {
  58. return new CubicSegment(p[0], p[1], p[2], p[3], color);
  59. }
  60. int LinearSegment::type() const {
  61. return (int) EDGE_TYPE;
  62. }
  63. int QuadraticSegment::type() const {
  64. return (int) EDGE_TYPE;
  65. }
  66. int CubicSegment::type() const {
  67. return (int) EDGE_TYPE;
  68. }
  69. const Point2 *LinearSegment::controlPoints() const {
  70. return p;
  71. }
  72. const Point2 *QuadraticSegment::controlPoints() const {
  73. return p;
  74. }
  75. const Point2 *CubicSegment::controlPoints() const {
  76. return p;
  77. }
  78. Point2 LinearSegment::point(double param) const {
  79. return mix(p[0], p[1], param);
  80. }
  81. Point2 QuadraticSegment::point(double param) const {
  82. return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);
  83. }
  84. Point2 CubicSegment::point(double param) const {
  85. Vector2 p12 = mix(p[1], p[2], param);
  86. return mix(mix(mix(p[0], p[1], param), p12, param), mix(p12, mix(p[2], p[3], param), param), param);
  87. }
  88. Vector2 LinearSegment::direction(double param) const {
  89. return p[1]-p[0];
  90. }
  91. Vector2 QuadraticSegment::direction(double param) const {
  92. Vector2 tangent = mix(p[1]-p[0], p[2]-p[1], param);
  93. if (!tangent)
  94. return p[2]-p[0];
  95. return tangent;
  96. }
  97. Vector2 CubicSegment::direction(double param) const {
  98. Vector2 tangent = mix(mix(p[1]-p[0], p[2]-p[1], param), mix(p[2]-p[1], p[3]-p[2], param), param);
  99. if (!tangent) {
  100. if (param == 0) return p[2]-p[0];
  101. if (param == 1) return p[3]-p[1];
  102. }
  103. return tangent;
  104. }
  105. Vector2 LinearSegment::directionChange(double param) const {
  106. return Vector2();
  107. }
  108. Vector2 QuadraticSegment::directionChange(double param) const {
  109. return (p[2]-p[1])-(p[1]-p[0]);
  110. }
  111. Vector2 CubicSegment::directionChange(double param) const {
  112. return mix((p[2]-p[1])-(p[1]-p[0]), (p[3]-p[2])-(p[2]-p[1]), param);
  113. }
  114. double LinearSegment::length() const {
  115. return (p[1]-p[0]).length();
  116. }
  117. double QuadraticSegment::length() const {
  118. Vector2 ab = p[1]-p[0];
  119. Vector2 br = p[2]-p[1]-ab;
  120. double abab = dotProduct(ab, ab);
  121. double abbr = dotProduct(ab, br);
  122. double brbr = dotProduct(br, br);
  123. double abLen = sqrt(abab);
  124. double brLen = sqrt(brbr);
  125. double crs = crossProduct(ab, br);
  126. double h = sqrt(abab+abbr+abbr+brbr);
  127. return (
  128. brLen*((abbr+brbr)*h-abbr*abLen)+
  129. crs*crs*log((brLen*h+abbr+brbr)/(brLen*abLen+abbr))
  130. )/(brbr*brLen);
  131. }
  132. SignedDistance LinearSegment::signedDistance(Point2 origin, double &param) const {
  133. Vector2 aq = origin-p[0];
  134. Vector2 ab = p[1]-p[0];
  135. param = dotProduct(aq, ab)/dotProduct(ab, ab);
  136. Vector2 eq = p[param > .5]-origin;
  137. double endpointDistance = eq.length();
  138. if (param > 0 && param < 1) {
  139. double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);
  140. if (fabs(orthoDistance) < endpointDistance)
  141. return SignedDistance(orthoDistance, 0);
  142. }
  143. return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));
  144. }
  145. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  146. Vector2 qa = p[0]-origin;
  147. Vector2 ab = p[1]-p[0];
  148. Vector2 br = p[2]-p[1]-ab;
  149. double a = dotProduct(br, br);
  150. double b = 3*dotProduct(ab, br);
  151. double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);
  152. double d = dotProduct(qa, ab);
  153. double t[3];
  154. int solutions = solveCubic(t, a, b, c, d);
  155. Vector2 epDir = direction(0);
  156. double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
  157. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  158. {
  159. epDir = direction(1);
  160. double distance = (p[2]-origin).length(); // distance from B
  161. if (distance < fabs(minDistance)) {
  162. minDistance = nonZeroSign(crossProduct(epDir, p[2]-origin))*distance;
  163. param = dotProduct(origin-p[1], epDir)/dotProduct(epDir, epDir);
  164. }
  165. }
  166. for (int i = 0; i < solutions; ++i) {
  167. if (t[i] > 0 && t[i] < 1) {
  168. Point2 qe = qa+2*t[i]*ab+t[i]*t[i]*br;
  169. double distance = qe.length();
  170. if (distance <= fabs(minDistance)) {
  171. minDistance = nonZeroSign(crossProduct(ab+t[i]*br, qe))*distance;
  172. param = t[i];
  173. }
  174. }
  175. }
  176. if (param >= 0 && param <= 1)
  177. return SignedDistance(minDistance, 0);
  178. if (param < .5)
  179. return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));
  180. else
  181. return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[2]-origin).normalize())));
  182. }
  183. SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  184. Vector2 qa = p[0]-origin;
  185. Vector2 ab = p[1]-p[0];
  186. Vector2 br = p[2]-p[1]-ab;
  187. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  188. Vector2 epDir = direction(0);
  189. double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
  190. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  191. {
  192. epDir = direction(1);
  193. double distance = (p[3]-origin).length(); // distance from B
  194. if (distance < fabs(minDistance)) {
  195. minDistance = nonZeroSign(crossProduct(epDir, p[3]-origin))*distance;
  196. param = dotProduct(epDir-(p[3]-origin), epDir)/dotProduct(epDir, epDir);
  197. }
  198. }
  199. // Iterative minimum distance search
  200. for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {
  201. double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;
  202. Vector2 qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
  203. for (int step = 0; step < MSDFGEN_CUBIC_SEARCH_STEPS; ++step) {
  204. // Improve t
  205. Vector2 d1 = 3*ab+6*t*br+3*t*t*as;
  206. Vector2 d2 = 6*br+6*t*as;
  207. t -= dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));
  208. if (t <= 0 || t >= 1)
  209. break;
  210. qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
  211. double distance = qe.length();
  212. if (distance < fabs(minDistance)) {
  213. minDistance = nonZeroSign(crossProduct(d1, qe))*distance;
  214. param = t;
  215. }
  216. }
  217. }
  218. if (param >= 0 && param <= 1)
  219. return SignedDistance(minDistance, 0);
  220. if (param < .5)
  221. return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));
  222. else
  223. return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[3]-origin).normalize())));
  224. }
  225. int LinearSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  226. if ((y >= p[0].y && y < p[1].y) || (y >= p[1].y && y < p[0].y)) {
  227. double param = (y-p[0].y)/(p[1].y-p[0].y);
  228. x[0] = mix(p[0].x, p[1].x, param);
  229. dy[0] = sign(p[1].y-p[0].y);
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. int QuadraticSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  235. int total = 0;
  236. int nextDY = y > p[0].y ? 1 : -1;
  237. x[total] = p[0].x;
  238. if (p[0].y == y) {
  239. if (p[0].y < p[1].y || (p[0].y == p[1].y && p[0].y < p[2].y))
  240. dy[total++] = 1;
  241. else
  242. nextDY = 1;
  243. }
  244. {
  245. Vector2 ab = p[1]-p[0];
  246. Vector2 br = p[2]-p[1]-ab;
  247. double t[2];
  248. int solutions = solveQuadratic(t, br.y, 2*ab.y, p[0].y-y);
  249. // Sort solutions
  250. double tmp;
  251. if (solutions >= 2 && t[0] > t[1])
  252. tmp = t[0], t[0] = t[1], t[1] = tmp;
  253. for (int i = 0; i < solutions && total < 2; ++i) {
  254. if (t[i] >= 0 && t[i] <= 1) {
  255. x[total] = p[0].x+2*t[i]*ab.x+t[i]*t[i]*br.x;
  256. if (nextDY*(ab.y+t[i]*br.y) >= 0) {
  257. dy[total++] = nextDY;
  258. nextDY = -nextDY;
  259. }
  260. }
  261. }
  262. }
  263. if (p[2].y == y) {
  264. if (nextDY > 0 && total > 0) {
  265. --total;
  266. nextDY = -1;
  267. }
  268. if ((p[2].y < p[1].y || (p[2].y == p[1].y && p[2].y < p[0].y)) && total < 2) {
  269. x[total] = p[2].x;
  270. if (nextDY < 0) {
  271. dy[total++] = -1;
  272. nextDY = 1;
  273. }
  274. }
  275. }
  276. if (nextDY != (y >= p[2].y ? 1 : -1)) {
  277. if (total > 0)
  278. --total;
  279. else {
  280. if (fabs(p[2].y-y) < fabs(p[0].y-y))
  281. x[total] = p[2].x;
  282. dy[total++] = nextDY;
  283. }
  284. }
  285. return total;
  286. }
  287. int CubicSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  288. int total = 0;
  289. int nextDY = y > p[0].y ? 1 : -1;
  290. x[total] = p[0].x;
  291. if (p[0].y == y) {
  292. 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))))
  293. dy[total++] = 1;
  294. else
  295. nextDY = 1;
  296. }
  297. {
  298. Vector2 ab = p[1]-p[0];
  299. Vector2 br = p[2]-p[1]-ab;
  300. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  301. double t[3];
  302. int solutions = solveCubic(t, as.y, 3*br.y, 3*ab.y, p[0].y-y);
  303. // Sort solutions
  304. double tmp;
  305. if (solutions >= 2) {
  306. if (t[0] > t[1])
  307. tmp = t[0], t[0] = t[1], t[1] = tmp;
  308. if (solutions >= 3 && t[1] > t[2]) {
  309. tmp = t[1], t[1] = t[2], t[2] = tmp;
  310. if (t[0] > t[1])
  311. tmp = t[0], t[0] = t[1], t[1] = tmp;
  312. }
  313. }
  314. for (int i = 0; i < solutions && total < 3; ++i) {
  315. if (t[i] >= 0 && t[i] <= 1) {
  316. 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;
  317. if (nextDY*(ab.y+2*t[i]*br.y+t[i]*t[i]*as.y) >= 0) {
  318. dy[total++] = nextDY;
  319. nextDY = -nextDY;
  320. }
  321. }
  322. }
  323. }
  324. if (p[3].y == y) {
  325. if (nextDY > 0 && total > 0) {
  326. --total;
  327. nextDY = -1;
  328. }
  329. 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) {
  330. x[total] = p[3].x;
  331. if (nextDY < 0) {
  332. dy[total++] = -1;
  333. nextDY = 1;
  334. }
  335. }
  336. }
  337. if (nextDY != (y >= p[3].y ? 1 : -1)) {
  338. if (total > 0)
  339. --total;
  340. else {
  341. if (fabs(p[3].y-y) < fabs(p[0].y-y))
  342. x[total] = p[3].x;
  343. dy[total++] = nextDY;
  344. }
  345. }
  346. return total;
  347. }
  348. static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {
  349. if (p.x < l) l = p.x;
  350. if (p.y < b) b = p.y;
  351. if (p.x > r) r = p.x;
  352. if (p.y > t) t = p.y;
  353. }
  354. void LinearSegment::bound(double &l, double &b, double &r, double &t) const {
  355. pointBounds(p[0], l, b, r, t);
  356. pointBounds(p[1], l, b, r, t);
  357. }
  358. void QuadraticSegment::bound(double &l, double &b, double &r, double &t) const {
  359. pointBounds(p[0], l, b, r, t);
  360. pointBounds(p[2], l, b, r, t);
  361. Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);
  362. if (bot.x) {
  363. double param = (p[1].x-p[0].x)/bot.x;
  364. if (param > 0 && param < 1)
  365. pointBounds(point(param), l, b, r, t);
  366. }
  367. if (bot.y) {
  368. double param = (p[1].y-p[0].y)/bot.y;
  369. if (param > 0 && param < 1)
  370. pointBounds(point(param), l, b, r, t);
  371. }
  372. }
  373. void CubicSegment::bound(double &l, double &b, double &r, double &t) const {
  374. pointBounds(p[0], l, b, r, t);
  375. pointBounds(p[3], l, b, r, t);
  376. Vector2 a0 = p[1]-p[0];
  377. Vector2 a1 = 2*(p[2]-p[1]-a0);
  378. Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];
  379. double params[2];
  380. int solutions;
  381. solutions = solveQuadratic(params, a2.x, a1.x, a0.x);
  382. for (int i = 0; i < solutions; ++i)
  383. if (params[i] > 0 && params[i] < 1)
  384. pointBounds(point(params[i]), l, b, r, t);
  385. solutions = solveQuadratic(params, a2.y, a1.y, a0.y);
  386. for (int i = 0; i < solutions; ++i)
  387. if (params[i] > 0 && params[i] < 1)
  388. pointBounds(point(params[i]), l, b, r, t);
  389. }
  390. void LinearSegment::reverse() {
  391. Point2 tmp = p[0];
  392. p[0] = p[1];
  393. p[1] = tmp;
  394. }
  395. void QuadraticSegment::reverse() {
  396. Point2 tmp = p[0];
  397. p[0] = p[2];
  398. p[2] = tmp;
  399. }
  400. void CubicSegment::reverse() {
  401. Point2 tmp = p[0];
  402. p[0] = p[3];
  403. p[3] = tmp;
  404. tmp = p[1];
  405. p[1] = p[2];
  406. p[2] = tmp;
  407. }
  408. void LinearSegment::moveStartPoint(Point2 to) {
  409. p[0] = to;
  410. }
  411. void QuadraticSegment::moveStartPoint(Point2 to) {
  412. Vector2 origSDir = p[0]-p[1];
  413. Point2 origP1 = p[1];
  414. p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);
  415. p[0] = to;
  416. if (dotProduct(origSDir, p[0]-p[1]) < 0)
  417. p[1] = origP1;
  418. }
  419. void CubicSegment::moveStartPoint(Point2 to) {
  420. p[1] += to-p[0];
  421. p[0] = to;
  422. }
  423. void LinearSegment::moveEndPoint(Point2 to) {
  424. p[1] = to;
  425. }
  426. void QuadraticSegment::moveEndPoint(Point2 to) {
  427. Vector2 origEDir = p[2]-p[1];
  428. Point2 origP1 = p[1];
  429. p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);
  430. p[2] = to;
  431. if (dotProduct(origEDir, p[2]-p[1]) < 0)
  432. p[1] = origP1;
  433. }
  434. void CubicSegment::moveEndPoint(Point2 to) {
  435. p[2] += to-p[3];
  436. p[3] = to;
  437. }
  438. void LinearSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  439. part1 = new LinearSegment(p[0], point(1/3.), color);
  440. part2 = new LinearSegment(point(1/3.), point(2/3.), color);
  441. part3 = new LinearSegment(point(2/3.), p[1], color);
  442. }
  443. void QuadraticSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  444. part1 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);
  445. 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);
  446. part3 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);
  447. }
  448. void CubicSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  449. 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);
  450. part2 = new CubicSegment(point(1/3.),
  451. 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.),
  452. 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.),
  453. point(2/3.), color);
  454. 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);
  455. }
  456. EdgeSegment *QuadraticSegment::convertToCubic() const {
  457. return new CubicSegment(p[0], mix(p[0], p[1], 2/3.), mix(p[1], p[2], 1/3.), p[2], color);
  458. }
  459. void CubicSegment::deconverge(int param, double amount) {
  460. Vector2 dir = direction(param);
  461. Vector2 normal = dir.getOrthonormal();
  462. double h = dotProduct(directionChange(param)-dir, normal);
  463. switch (param) {
  464. case 0:
  465. p[1] += amount*(dir+sign(h)*sqrt(fabs(h))*normal);
  466. break;
  467. case 1:
  468. p[2] -= amount*(dir-sign(h)*sqrt(fabs(h))*normal);
  469. break;
  470. }
  471. }
  472. }