2
0

edge-segments.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. #include "edge-segments.h"
  2. #include "arithmetics.hpp"
  3. #include "equation-solver.h"
  4. #include "bezier-solver.hpp"
  5. #define MSDFGEN_USE_BEZIER_SOLVER
  6. namespace msdfgen {
  7. EdgeSegment *EdgeSegment::create(Point2 p0, Point2 p1, EdgeColor edgeColor) {
  8. return new LinearSegment(p0, p1, edgeColor);
  9. }
  10. EdgeSegment *EdgeSegment::create(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) {
  11. if (!crossProduct(p1-p0, p2-p1))
  12. return new LinearSegment(p0, p2, edgeColor);
  13. return new QuadraticSegment(p0, p1, p2, edgeColor);
  14. }
  15. EdgeSegment *EdgeSegment::create(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) {
  16. Vector2 p12 = p2-p1;
  17. if (!crossProduct(p1-p0, p12) && !crossProduct(p12, p3-p2))
  18. return new LinearSegment(p0, p3, edgeColor);
  19. if ((p12 = 1.5*p1-.5*p0) == 1.5*p2-.5*p3)
  20. return new QuadraticSegment(p0, p12, p3, edgeColor);
  21. return new CubicSegment(p0, p1, p2, p3, edgeColor);
  22. }
  23. void EdgeSegment::distanceToPerpendicularDistance(SignedDistance &distance, Point2 origin, double param) const {
  24. if (param < 0) {
  25. Vector2 dir = direction(0).normalize();
  26. Vector2 aq = origin-point(0);
  27. double ts = dotProduct(aq, dir);
  28. if (ts < 0) {
  29. double perpendicularDistance = crossProduct(aq, dir);
  30. if (fabs(perpendicularDistance) <= fabs(distance.distance)) {
  31. distance.distance = perpendicularDistance;
  32. distance.dot = 0;
  33. }
  34. }
  35. } else if (param > 1) {
  36. Vector2 dir = direction(1).normalize();
  37. Vector2 bq = origin-point(1);
  38. double ts = dotProduct(bq, dir);
  39. if (ts > 0) {
  40. double perpendicularDistance = crossProduct(bq, dir);
  41. if (fabs(perpendicularDistance) <= fabs(distance.distance)) {
  42. distance.distance = perpendicularDistance;
  43. distance.dot = 0;
  44. }
  45. }
  46. }
  47. }
  48. LinearSegment::LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  49. p[0] = p0;
  50. p[1] = p1;
  51. }
  52. QuadraticSegment::QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  53. p[0] = p0;
  54. p[1] = p1;
  55. p[2] = p2;
  56. }
  57. CubicSegment::CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor) : EdgeSegment(edgeColor) {
  58. p[0] = p0;
  59. p[1] = p1;
  60. p[2] = p2;
  61. p[3] = p3;
  62. }
  63. LinearSegment *LinearSegment::clone() const {
  64. return new LinearSegment(p[0], p[1], color);
  65. }
  66. QuadraticSegment *QuadraticSegment::clone() const {
  67. return new QuadraticSegment(p[0], p[1], p[2], color);
  68. }
  69. CubicSegment *CubicSegment::clone() const {
  70. return new CubicSegment(p[0], p[1], p[2], p[3], color);
  71. }
  72. int LinearSegment::type() const {
  73. return (int) EDGE_TYPE;
  74. }
  75. int QuadraticSegment::type() const {
  76. return (int) EDGE_TYPE;
  77. }
  78. int CubicSegment::type() const {
  79. return (int) EDGE_TYPE;
  80. }
  81. const Point2 *LinearSegment::controlPoints() const {
  82. return p;
  83. }
  84. const Point2 *QuadraticSegment::controlPoints() const {
  85. return p;
  86. }
  87. const Point2 *CubicSegment::controlPoints() const {
  88. return p;
  89. }
  90. Point2 LinearSegment::point(double param) const {
  91. return mix(p[0], p[1], param);
  92. }
  93. Point2 QuadraticSegment::point(double param) const {
  94. return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);
  95. }
  96. Point2 CubicSegment::point(double param) const {
  97. Vector2 p12 = mix(p[1], p[2], param);
  98. return mix(mix(mix(p[0], p[1], param), p12, param), mix(p12, mix(p[2], p[3], param), param), param);
  99. }
  100. Vector2 LinearSegment::direction(double param) const {
  101. return p[1]-p[0];
  102. }
  103. Vector2 QuadraticSegment::direction(double param) const {
  104. Vector2 tangent = mix(p[1]-p[0], p[2]-p[1], param);
  105. if (!tangent)
  106. return p[2]-p[0];
  107. return tangent;
  108. }
  109. Vector2 CubicSegment::direction(double param) const {
  110. Vector2 tangent = mix(mix(p[1]-p[0], p[2]-p[1], param), mix(p[2]-p[1], p[3]-p[2], param), param);
  111. if (!tangent) {
  112. if (param == 0) return p[2]-p[0];
  113. if (param == 1) return p[3]-p[1];
  114. }
  115. return tangent;
  116. }
  117. Vector2 LinearSegment::directionChange(double param) const {
  118. return Vector2();
  119. }
  120. Vector2 QuadraticSegment::directionChange(double param) const {
  121. return (p[2]-p[1])-(p[1]-p[0]);
  122. }
  123. Vector2 CubicSegment::directionChange(double param) const {
  124. return mix((p[2]-p[1])-(p[1]-p[0]), (p[3]-p[2])-(p[2]-p[1]), param);
  125. }
  126. double LinearSegment::length() const {
  127. return (p[1]-p[0]).length();
  128. }
  129. double QuadraticSegment::length() const {
  130. Vector2 ab = p[1]-p[0];
  131. Vector2 br = p[2]-p[1]-ab;
  132. double abab = dotProduct(ab, ab);
  133. double abbr = dotProduct(ab, br);
  134. double brbr = dotProduct(br, br);
  135. double abLen = sqrt(abab);
  136. double brLen = sqrt(brbr);
  137. double crs = crossProduct(ab, br);
  138. double h = sqrt(abab+abbr+abbr+brbr);
  139. return (
  140. brLen*((abbr+brbr)*h-abbr*abLen)+
  141. crs*crs*log((brLen*h+abbr+brbr)/(brLen*abLen+abbr))
  142. )/(brbr*brLen);
  143. }
  144. SignedDistance LinearSegment::signedDistance(Point2 origin, double &param) const {
  145. Vector2 aq = origin-p[0];
  146. Vector2 ab = p[1]-p[0];
  147. param = dotProduct(aq, ab)/dotProduct(ab, ab);
  148. Vector2 eq = p[param > .5]-origin;
  149. double endpointDistance = eq.length();
  150. if (param > 0 && param < 1) {
  151. double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);
  152. if (fabs(orthoDistance) < endpointDistance)
  153. return SignedDistance(orthoDistance, 0);
  154. }
  155. return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));
  156. }
  157. #ifdef MSDFGEN_USE_BEZIER_SOLVER
  158. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  159. Vector2 ap = origin-p[0];
  160. Vector2 bp = origin-p[2];
  161. Vector2 q = 2*(p[1]-p[0]);
  162. Vector2 r = p[2]-2*p[1]+p[0];
  163. double aSqD = ap.squaredLength();
  164. double bSqD = bp.squaredLength();
  165. double t = quadraticNearPoint(ap, q, r);
  166. if (t > 0 && t < 1) {
  167. Vector2 tp = ap-(q+r*t)*t;
  168. double tSqD = tp.squaredLength();
  169. if (tSqD < aSqD && tSqD < bSqD) {
  170. param = t;
  171. return SignedDistance(nonZeroSign(crossProduct(tp, q+2*r*t))*sqrt(tSqD), 0);
  172. }
  173. }
  174. if (bSqD < aSqD) {
  175. Vector2 d = q+r+r;
  176. if (!d)
  177. d = p[2]-p[0];
  178. param = dotProduct(bp, d)/d.squaredLength()+1;
  179. return SignedDistance(nonZeroSign(crossProduct(bp, d))*sqrt(bSqD), dotProduct(bp.normalize(), d.normalize()));
  180. }
  181. if (!q)
  182. q = p[2]-p[0];
  183. param = dotProduct(ap, q)/q.squaredLength();
  184. return SignedDistance(nonZeroSign(crossProduct(ap, q))*sqrt(aSqD), -dotProduct(ap.normalize(), q.normalize()));
  185. }
  186. SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  187. Vector2 ap = origin-p[0];
  188. Vector2 bp = origin-p[3];
  189. Vector2 q = 3*(p[1]-p[0]);
  190. Vector2 r = 3*(p[2]-p[1])-q;
  191. Vector2 s = p[3]-3*(p[2]-p[1])-p[0];
  192. double aSqD = ap.squaredLength();
  193. double bSqD = bp.squaredLength();
  194. double tSqD;
  195. double t = cubicNearPoint(ap, q, r, s, tSqD);
  196. if (t > 0 && t < 1) {
  197. if (tSqD < aSqD && tSqD < bSqD) {
  198. param = t;
  199. return SignedDistance(nonZeroSign(crossProduct(ap-(q+(r+s*t)*t)*t, q+(r+r+3*s*t)*t))*sqrt(tSqD), 0);
  200. }
  201. }
  202. if (bSqD < aSqD) {
  203. Vector2 d = q+r+r+3*s;
  204. if (!d)
  205. d = p[3]-p[1];
  206. param = dotProduct(bp, d)/d.squaredLength()+1;
  207. return SignedDistance(nonZeroSign(crossProduct(bp, d))*sqrt(bSqD), dotProduct(bp.normalize(), d.normalize()));
  208. }
  209. if (!q)
  210. q = p[2]-p[0];
  211. param = dotProduct(ap, q)/q.squaredLength();
  212. return SignedDistance(nonZeroSign(crossProduct(ap, q))*sqrt(aSqD), -dotProduct(ap.normalize(), q.normalize()));
  213. }
  214. #else
  215. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  216. Vector2 qa = p[0]-origin;
  217. Vector2 ab = p[1]-p[0];
  218. Vector2 br = p[2]-p[1]-ab;
  219. double a = dotProduct(br, br);
  220. double b = 3*dotProduct(ab, br);
  221. double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);
  222. double d = dotProduct(qa, ab);
  223. double t[3];
  224. int solutions = solveCubic(t, a, b, c, d);
  225. Vector2 epDir = direction(0);
  226. double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
  227. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  228. {
  229. double distance = (p[2]-origin).length(); // distance from B
  230. if (distance < fabs(minDistance)) {
  231. epDir = direction(1);
  232. minDistance = nonZeroSign(crossProduct(epDir, p[2]-origin))*distance;
  233. param = dotProduct(origin-p[1], epDir)/dotProduct(epDir, epDir);
  234. }
  235. }
  236. for (int i = 0; i < solutions; ++i) {
  237. if (t[i] > 0 && t[i] < 1) {
  238. Point2 qe = qa+2*t[i]*ab+t[i]*t[i]*br;
  239. double distance = qe.length();
  240. if (distance <= fabs(minDistance)) {
  241. minDistance = nonZeroSign(crossProduct(ab+t[i]*br, qe))*distance;
  242. param = t[i];
  243. }
  244. }
  245. }
  246. if (param >= 0 && param <= 1)
  247. return SignedDistance(minDistance, 0);
  248. if (param < .5)
  249. return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));
  250. else
  251. return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[2]-origin).normalize())));
  252. }
  253. SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  254. Vector2 qa = p[0]-origin;
  255. Vector2 ab = p[1]-p[0];
  256. Vector2 br = p[2]-p[1]-ab;
  257. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  258. Vector2 epDir = direction(0);
  259. double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
  260. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  261. {
  262. double distance = (p[3]-origin).length(); // distance from B
  263. if (distance < fabs(minDistance)) {
  264. epDir = direction(1);
  265. minDistance = nonZeroSign(crossProduct(epDir, p[3]-origin))*distance;
  266. param = dotProduct(epDir-(p[3]-origin), epDir)/dotProduct(epDir, epDir);
  267. }
  268. }
  269. // Iterative minimum distance search
  270. for (int i = 0; i <= MSDFGEN_CUBIC_SEARCH_STARTS; ++i) {
  271. double t = 1./MSDFGEN_CUBIC_SEARCH_STARTS*i;
  272. Vector2 qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
  273. Vector2 d1 = 3*ab+6*t*br+3*t*t*as;
  274. Vector2 d2 = 6*br+6*t*as;
  275. double improvedT = t-dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));
  276. if (improvedT > 0 && improvedT < 1) {
  277. int remainingSteps = MSDFGEN_CUBIC_SEARCH_STEPS;
  278. do {
  279. t = improvedT;
  280. qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
  281. d1 = 3*ab+6*t*br+3*t*t*as;
  282. if (!--remainingSteps)
  283. break;
  284. d2 = 6*br+6*t*as;
  285. improvedT = t-dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));
  286. } while (improvedT > 0 && improvedT < 1);
  287. double distance = qe.length();
  288. if (distance < fabs(minDistance)) {
  289. minDistance = nonZeroSign(crossProduct(d1, qe))*distance;
  290. param = t;
  291. }
  292. }
  293. }
  294. if (param >= 0 && param <= 1)
  295. return SignedDistance(minDistance, 0);
  296. if (param < .5)
  297. return SignedDistance(minDistance, fabs(dotProduct(direction(0).normalize(), qa.normalize())));
  298. else
  299. return SignedDistance(minDistance, fabs(dotProduct(direction(1).normalize(), (p[3]-origin).normalize())));
  300. }
  301. #endif
  302. int LinearSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  303. return horizontalScanlineIntersections(x, dy, y);
  304. }
  305. int QuadraticSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  306. return horizontalScanlineIntersections(x, dy, y);
  307. }
  308. int CubicSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  309. return horizontalScanlineIntersections(x, dy, y);
  310. }
  311. int LinearSegment::horizontalScanlineIntersections(double x[3], int dy[3], double y) const {
  312. if ((y >= p[0].y && y < p[1].y) || (y >= p[1].y && y < p[0].y)) {
  313. double param = (y-p[0].y)/(p[1].y-p[0].y);
  314. x[0] = mix(p[0].x, p[1].x, param);
  315. dy[0] = sign(p[1].y-p[0].y);
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. int LinearSegment::verticalScanlineIntersections(double y[3], int dx[3], double x) const {
  321. if ((x >= p[0].x && x < p[1].x) || (x >= p[1].x && x < p[0].x)) {
  322. double param = (x-p[0].x)/(p[1].x-p[0].x);
  323. y[0] = mix(p[0].y, p[1].y, param);
  324. dx[0] = sign(p[1].x-p[0].x);
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. int QuadraticSegment::horizontalScanlineIntersections(double x[3], int dy[3], double y) const {
  330. int total = 0;
  331. int nextDY = y > p[0].y ? 1 : -1;
  332. x[total] = p[0].x;
  333. if (p[0].y == y) {
  334. if (p[0].y < p[1].y || (p[0].y == p[1].y && p[0].y < p[2].y))
  335. dy[total++] = 1;
  336. else
  337. nextDY = 1;
  338. }
  339. {
  340. Vector2 ab = p[1]-p[0];
  341. Vector2 br = p[2]-p[1]-ab;
  342. double t[2];
  343. int solutions = solveQuadratic(t, br.y, 2*ab.y, p[0].y-y);
  344. // Sort solutions
  345. double tmp;
  346. if (solutions >= 2 && t[0] > t[1])
  347. tmp = t[0], t[0] = t[1], t[1] = tmp;
  348. for (int i = 0; i < solutions && total < 2; ++i) {
  349. if (t[i] >= 0 && t[i] <= 1) {
  350. x[total] = p[0].x+2*t[i]*ab.x+t[i]*t[i]*br.x;
  351. if (nextDY*(ab.y+t[i]*br.y) >= 0) {
  352. dy[total++] = nextDY;
  353. nextDY = -nextDY;
  354. }
  355. }
  356. }
  357. }
  358. if (p[2].y == y) {
  359. if (nextDY > 0 && total > 0) {
  360. --total;
  361. nextDY = -1;
  362. }
  363. if ((p[2].y < p[1].y || (p[2].y == p[1].y && p[2].y < p[0].y)) && total < 2) {
  364. x[total] = p[2].x;
  365. if (nextDY < 0) {
  366. dy[total++] = -1;
  367. nextDY = 1;
  368. }
  369. }
  370. }
  371. if (nextDY != (y >= p[2].y ? 1 : -1)) {
  372. if (total > 0)
  373. --total;
  374. else {
  375. if (fabs(p[2].y-y) < fabs(p[0].y-y))
  376. x[total] = p[2].x;
  377. dy[total++] = nextDY;
  378. }
  379. }
  380. return total;
  381. }
  382. int QuadraticSegment::verticalScanlineIntersections(double y[3], int dx[3], double x) const {
  383. int total = 0;
  384. int nextDX = x > p[0].x ? 1 : -1;
  385. y[total] = p[0].y;
  386. if (p[0].x == x) {
  387. if (p[0].x < p[1].x || (p[0].x == p[1].x && p[0].x < p[2].x))
  388. dx[total++] = 1;
  389. else
  390. nextDX = 1;
  391. }
  392. {
  393. Vector2 ab = p[1]-p[0];
  394. Vector2 br = p[2]-p[1]-ab;
  395. double t[2];
  396. int solutions = solveQuadratic(t, br.x, 2*ab.x, p[0].x-x);
  397. // Sort solutions
  398. double tmp;
  399. if (solutions >= 2 && t[0] > t[1])
  400. tmp = t[0], t[0] = t[1], t[1] = tmp;
  401. for (int i = 0; i < solutions && total < 2; ++i) {
  402. if (t[i] >= 0 && t[i] <= 1) {
  403. y[total] = p[0].y+2*t[i]*ab.y+t[i]*t[i]*br.y;
  404. if (nextDX*(ab.x+t[i]*br.x) >= 0) {
  405. dx[total++] = nextDX;
  406. nextDX = -nextDX;
  407. }
  408. }
  409. }
  410. }
  411. if (p[2].x == x) {
  412. if (nextDX > 0 && total > 0) {
  413. --total;
  414. nextDX = -1;
  415. }
  416. if ((p[2].x < p[1].x || (p[2].x == p[1].x && p[2].x < p[0].x)) && total < 2) {
  417. y[total] = p[2].y;
  418. if (nextDX < 0) {
  419. dx[total++] = -1;
  420. nextDX = 1;
  421. }
  422. }
  423. }
  424. if (nextDX != (x >= p[2].x ? 1 : -1)) {
  425. if (total > 0)
  426. --total;
  427. else {
  428. if (fabs(p[2].x-x) < fabs(p[0].x-x))
  429. y[total] = p[2].y;
  430. dx[total++] = nextDX;
  431. }
  432. }
  433. return total;
  434. }
  435. int CubicSegment::horizontalScanlineIntersections(double x[3], int dy[3], double y) const {
  436. int total = 0;
  437. int nextDY = y > p[0].y ? 1 : -1;
  438. x[total] = p[0].x;
  439. if (p[0].y == y) {
  440. 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))))
  441. dy[total++] = 1;
  442. else
  443. nextDY = 1;
  444. }
  445. {
  446. Vector2 ab = p[1]-p[0];
  447. Vector2 br = p[2]-p[1]-ab;
  448. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  449. double t[3];
  450. int solutions = solveCubic(t, as.y, 3*br.y, 3*ab.y, p[0].y-y);
  451. // Sort solutions
  452. double tmp;
  453. if (solutions >= 2) {
  454. if (t[0] > t[1])
  455. tmp = t[0], t[0] = t[1], t[1] = tmp;
  456. if (solutions >= 3 && t[1] > t[2]) {
  457. tmp = t[1], t[1] = t[2], t[2] = tmp;
  458. if (t[0] > t[1])
  459. tmp = t[0], t[0] = t[1], t[1] = tmp;
  460. }
  461. }
  462. for (int i = 0; i < solutions && total < 3; ++i) {
  463. if (t[i] >= 0 && t[i] <= 1) {
  464. 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;
  465. if (nextDY*(ab.y+2*t[i]*br.y+t[i]*t[i]*as.y) >= 0) {
  466. dy[total++] = nextDY;
  467. nextDY = -nextDY;
  468. }
  469. }
  470. }
  471. }
  472. if (p[3].y == y) {
  473. if (nextDY > 0 && total > 0) {
  474. --total;
  475. nextDY = -1;
  476. }
  477. 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) {
  478. x[total] = p[3].x;
  479. if (nextDY < 0) {
  480. dy[total++] = -1;
  481. nextDY = 1;
  482. }
  483. }
  484. }
  485. if (nextDY != (y >= p[3].y ? 1 : -1)) {
  486. if (total > 0)
  487. --total;
  488. else {
  489. if (fabs(p[3].y-y) < fabs(p[0].y-y))
  490. x[total] = p[3].x;
  491. dy[total++] = nextDY;
  492. }
  493. }
  494. return total;
  495. }
  496. int CubicSegment::verticalScanlineIntersections(double y[3], int dx[3], double x) const {
  497. int total = 0;
  498. int nextDX = x > p[0].x ? 1 : -1;
  499. y[total] = p[0].y;
  500. if (p[0].x == x) {
  501. if (p[0].x < p[1].x || (p[0].x == p[1].x && (p[0].x < p[2].x || (p[0].x == p[2].x && p[0].x < p[3].x))))
  502. dx[total++] = 1;
  503. else
  504. nextDX = 1;
  505. }
  506. {
  507. Vector2 ab = p[1]-p[0];
  508. Vector2 br = p[2]-p[1]-ab;
  509. Vector2 as = (p[3]-p[2])-(p[2]-p[1])-br;
  510. double t[3];
  511. int solutions = solveCubic(t, as.x, 3*br.x, 3*ab.x, p[0].x-x);
  512. // Sort solutions
  513. double tmp;
  514. if (solutions >= 2) {
  515. if (t[0] > t[1])
  516. tmp = t[0], t[0] = t[1], t[1] = tmp;
  517. if (solutions >= 3 && t[1] > t[2]) {
  518. tmp = t[1], t[1] = t[2], t[2] = tmp;
  519. if (t[0] > t[1])
  520. tmp = t[0], t[0] = t[1], t[1] = tmp;
  521. }
  522. }
  523. for (int i = 0; i < solutions && total < 3; ++i) {
  524. if (t[i] >= 0 && t[i] <= 1) {
  525. y[total] = p[0].y+3*t[i]*ab.y+3*t[i]*t[i]*br.y+t[i]*t[i]*t[i]*as.y;
  526. if (nextDX*(ab.x+2*t[i]*br.x+t[i]*t[i]*as.x) >= 0) {
  527. dx[total++] = nextDX;
  528. nextDX = -nextDX;
  529. }
  530. }
  531. }
  532. }
  533. if (p[3].x == x) {
  534. if (nextDX > 0 && total > 0) {
  535. --total;
  536. nextDX = -1;
  537. }
  538. if ((p[3].x < p[2].x || (p[3].x == p[2].x && (p[3].x < p[1].x || (p[3].x == p[1].x && p[3].x < p[0].x)))) && total < 3) {
  539. y[total] = p[3].y;
  540. if (nextDX < 0) {
  541. dx[total++] = -1;
  542. nextDX = 1;
  543. }
  544. }
  545. }
  546. if (nextDX != (x >= p[3].x ? 1 : -1)) {
  547. if (total > 0)
  548. --total;
  549. else {
  550. if (fabs(p[3].x-x) < fabs(p[0].x-x))
  551. y[total] = p[3].y;
  552. dx[total++] = nextDX;
  553. }
  554. }
  555. return total;
  556. }
  557. static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {
  558. if (p.x < l) l = p.x;
  559. if (p.y < b) b = p.y;
  560. if (p.x > r) r = p.x;
  561. if (p.y > t) t = p.y;
  562. }
  563. void LinearSegment::bound(double &l, double &b, double &r, double &t) const {
  564. pointBounds(p[0], l, b, r, t);
  565. pointBounds(p[1], l, b, r, t);
  566. }
  567. void QuadraticSegment::bound(double &l, double &b, double &r, double &t) const {
  568. pointBounds(p[0], l, b, r, t);
  569. pointBounds(p[2], l, b, r, t);
  570. Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);
  571. if (bot.x) {
  572. double param = (p[1].x-p[0].x)/bot.x;
  573. if (param > 0 && param < 1)
  574. pointBounds(point(param), l, b, r, t);
  575. }
  576. if (bot.y) {
  577. double param = (p[1].y-p[0].y)/bot.y;
  578. if (param > 0 && param < 1)
  579. pointBounds(point(param), l, b, r, t);
  580. }
  581. }
  582. void CubicSegment::bound(double &l, double &b, double &r, double &t) const {
  583. pointBounds(p[0], l, b, r, t);
  584. pointBounds(p[3], l, b, r, t);
  585. Vector2 a0 = p[1]-p[0];
  586. Vector2 a1 = 2*(p[2]-p[1]-a0);
  587. Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];
  588. double params[2];
  589. int solutions;
  590. solutions = solveQuadratic(params, a2.x, a1.x, a0.x);
  591. for (int i = 0; i < solutions; ++i)
  592. if (params[i] > 0 && params[i] < 1)
  593. pointBounds(point(params[i]), l, b, r, t);
  594. solutions = solveQuadratic(params, a2.y, a1.y, a0.y);
  595. for (int i = 0; i < solutions; ++i)
  596. if (params[i] > 0 && params[i] < 1)
  597. pointBounds(point(params[i]), l, b, r, t);
  598. }
  599. void LinearSegment::reverse() {
  600. Point2 tmp = p[0];
  601. p[0] = p[1];
  602. p[1] = tmp;
  603. }
  604. void QuadraticSegment::reverse() {
  605. Point2 tmp = p[0];
  606. p[0] = p[2];
  607. p[2] = tmp;
  608. }
  609. void CubicSegment::reverse() {
  610. Point2 tmp = p[0];
  611. p[0] = p[3];
  612. p[3] = tmp;
  613. tmp = p[1];
  614. p[1] = p[2];
  615. p[2] = tmp;
  616. }
  617. void LinearSegment::moveStartPoint(Point2 to) {
  618. p[0] = to;
  619. }
  620. void QuadraticSegment::moveStartPoint(Point2 to) {
  621. Vector2 origSDir = p[0]-p[1];
  622. Point2 origP1 = p[1];
  623. p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);
  624. p[0] = to;
  625. if (dotProduct(origSDir, p[0]-p[1]) < 0)
  626. p[1] = origP1;
  627. }
  628. void CubicSegment::moveStartPoint(Point2 to) {
  629. p[1] += to-p[0];
  630. p[0] = to;
  631. }
  632. void LinearSegment::moveEndPoint(Point2 to) {
  633. p[1] = to;
  634. }
  635. void QuadraticSegment::moveEndPoint(Point2 to) {
  636. Vector2 origEDir = p[2]-p[1];
  637. Point2 origP1 = p[1];
  638. p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);
  639. p[2] = to;
  640. if (dotProduct(origEDir, p[2]-p[1]) < 0)
  641. p[1] = origP1;
  642. }
  643. void CubicSegment::moveEndPoint(Point2 to) {
  644. p[2] += to-p[3];
  645. p[3] = to;
  646. }
  647. void LinearSegment::splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const {
  648. part0 = new LinearSegment(p[0], point(1/3.), color);
  649. part1 = new LinearSegment(point(1/3.), point(2/3.), color);
  650. part2 = new LinearSegment(point(2/3.), p[1], color);
  651. }
  652. void QuadraticSegment::splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const {
  653. part0 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);
  654. part1 = 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);
  655. part2 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);
  656. }
  657. void CubicSegment::splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const {
  658. part0 = 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);
  659. part1 = new CubicSegment(point(1/3.),
  660. 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.),
  661. 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.),
  662. point(2/3.), color);
  663. part2 = 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);
  664. }
  665. EdgeSegment *QuadraticSegment::convertToCubic() const {
  666. return new CubicSegment(p[0], mix(p[0], p[1], 2/3.), mix(p[1], p[2], 1/3.), p[2], color);
  667. }
  668. }