edge-segments.cpp 20 KB

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