2
0

edge-segments.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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, double param) const {
  8. if (param < 0) {
  9. Vector2 dir = direction(0).normalize();
  10. Vector2 aq = origin-point(0);
  11. double ts = dotProduct(aq, dir);
  12. if (ts < 0) {
  13. double 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 > 1) {
  20. Vector2 dir = direction(1).normalize();
  21. Vector2 bq = origin-point(1);
  22. double ts = dotProduct(bq, dir);
  23. if (ts > 0) {
  24. double 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 = 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. if ((p1 == p0 || p1 == p3) && (p2 == p0 || p2 == p3)) {
  45. p1 = mix(p0, p3, 1/3.);
  46. p2 = mix(p0, p3, 2/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(double param) const {
  81. return mix(p[0], p[1], param);
  82. }
  83. Point2 QuadraticSegment::point(double param) const {
  84. return mix(mix(p[0], p[1], param), mix(p[1], p[2], param), param);
  85. }
  86. Point2 CubicSegment::point(double 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(double param) const {
  91. return p[1]-p[0];
  92. }
  93. Vector2 QuadraticSegment::direction(double 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(double 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(double param) const {
  108. return Vector2();
  109. }
  110. Vector2 QuadraticSegment::directionChange(double param) const {
  111. return (p[2]-p[1])-(p[1]-p[0]);
  112. }
  113. Vector2 CubicSegment::directionChange(double param) const {
  114. return mix((p[2]-p[1])-(p[1]-p[0]), (p[3]-p[2])-(p[2]-p[1]), param);
  115. }
  116. double LinearSegment::length() const {
  117. return (p[1]-p[0]).length();
  118. }
  119. double QuadraticSegment::length() const {
  120. Vector2 ab = p[1]-p[0];
  121. Vector2 br = p[2]-p[1]-ab;
  122. double abab = dotProduct(ab, ab);
  123. double abbr = dotProduct(ab, br);
  124. double brbr = dotProduct(br, br);
  125. double abLen = sqrt(abab);
  126. double brLen = sqrt(brbr);
  127. double crs = crossProduct(ab, br);
  128. double 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, double &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 > .5]-origin;
  139. double endpointDistance = eq.length();
  140. if (param > 0 && param < 1) {
  141. double orthoDistance = dotProduct(ab.getOrthonormal(false), aq);
  142. if (fabs(orthoDistance) < endpointDistance)
  143. return SignedDistance(orthoDistance, 0);
  144. }
  145. return SignedDistance(nonZeroSign(crossProduct(aq, ab))*endpointDistance, fabs(dotProduct(ab.normalize(), eq.normalize())));
  146. }
  147. #ifdef MSDFGEN_USE_BEZIER_SOLVER
  148. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  149. Vector2 ap = origin-p[0];
  150. Vector2 bp = origin-p[2];
  151. Vector2 q = 2*(p[1]-p[0]);
  152. Vector2 r = p[2]-2*p[1]+p[0];
  153. double aSqD = ap.squaredLength();
  154. double bSqD = bp.squaredLength();
  155. double t = quadraticNearPoint(ap, q, r);
  156. if (t > 0 && t < 1) {
  157. Vector2 tp = ap-(q+r*t)*t;
  158. double tSqD = tp.squaredLength();
  159. if (tSqD < aSqD && tSqD < bSqD) {
  160. param = t;
  161. return SignedDistance(nonZeroSign(crossProduct(tp, q+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()+1;
  169. return SignedDistance(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(nonZeroSign(crossProduct(ap, q))*sqrt(aSqD), -dotProduct(ap.normalize(), q.normalize()));
  175. }
  176. SignedDistance CubicSegment::signedDistance(Point2 origin, double &param) const {
  177. Vector2 ap = origin-p[0];
  178. Vector2 bp = origin-p[3];
  179. Vector2 q = 3*(p[1]-p[0]);
  180. Vector2 r = 3*(p[2]-p[1])-q;
  181. Vector2 s = p[3]-3*(p[2]-p[1])-p[0];
  182. double aSqD = ap.squaredLength();
  183. double bSqD = bp.squaredLength();
  184. double tSqD;
  185. double t = cubicNearPoint(ap, q, r, s, tSqD);
  186. if (t > 0 && t < 1) {
  187. if (tSqD < aSqD && tSqD < bSqD) {
  188. param = t;
  189. return SignedDistance(nonZeroSign(crossProduct(ap-(q+(r+s*t)*t)*t, q+(r+r+3*s*t)*t))*sqrt(tSqD), 0);
  190. }
  191. }
  192. if (bSqD < aSqD) {
  193. Vector2 d = q+r+r+3*s;
  194. if (!d)
  195. d = p[3]-p[1];
  196. param = dotProduct(bp, d)/d.squaredLength()+1;
  197. return SignedDistance(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(nonZeroSign(crossProduct(ap, q))*sqrt(aSqD), -dotProduct(ap.normalize(), q.normalize()));
  203. }
  204. #else
  205. SignedDistance QuadraticSegment::signedDistance(Point2 origin, double &param) const {
  206. Vector2 qa = p[0]-origin;
  207. Vector2 ab = p[1]-p[0];
  208. Vector2 br = p[2]-p[1]-ab;
  209. double a = dotProduct(br, br);
  210. double b = 3*dotProduct(ab, br);
  211. double c = 2*dotProduct(ab, ab)+dotProduct(qa, br);
  212. double d = dotProduct(qa, ab);
  213. double t[3];
  214. int solutions = solveCubic(t, a, b, c, d);
  215. Vector2 epDir = direction(0);
  216. double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
  217. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  218. {
  219. epDir = direction(1);
  220. double distance = (p[2]-origin).length(); // distance from B
  221. if (distance < fabs(minDistance)) {
  222. minDistance = 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] > 0 && t[i] < 1) {
  228. Point2 qe = qa+2*t[i]*ab+t[i]*t[i]*br;
  229. double distance = qe.length();
  230. if (distance <= fabs(minDistance)) {
  231. minDistance = nonZeroSign(crossProduct(ab+t[i]*br, qe))*distance;
  232. param = t[i];
  233. }
  234. }
  235. }
  236. if (param >= 0 && param <= 1)
  237. return SignedDistance(minDistance, 0);
  238. if (param < .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, double &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. double minDistance = nonZeroSign(crossProduct(epDir, qa))*qa.length(); // distance from A
  250. param = -dotProduct(qa, epDir)/dotProduct(epDir, epDir);
  251. {
  252. epDir = direction(1);
  253. double distance = (p[3]-origin).length(); // distance from B
  254. if (distance < fabs(minDistance)) {
  255. minDistance = 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. double t = (double) i/MSDFGEN_CUBIC_SEARCH_STARTS;
  262. Vector2 qe = qa+3*t*ab+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 = 3*ab+6*t*br+3*t*t*as;
  266. Vector2 d2 = 6*br+6*t*as;
  267. t -= dotProduct(qe, d1)/(dotProduct(d1, d1)+dotProduct(qe, d2));
  268. if (t <= 0 || t >= 1)
  269. break;
  270. qe = qa+3*t*ab+3*t*t*br+t*t*t*as;
  271. double distance = qe.length();
  272. if (distance < fabs(minDistance)) {
  273. minDistance = nonZeroSign(crossProduct(d1, qe))*distance;
  274. param = t;
  275. }
  276. }
  277. }
  278. if (param >= 0 && param <= 1)
  279. return SignedDistance(minDistance, 0);
  280. if (param < .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(double x[3], int dy[3], double y) const {
  287. return horizontalScanlineIntersections(x, dy, y);
  288. }
  289. int QuadraticSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  290. return horizontalScanlineIntersections(x, dy, y);
  291. }
  292. int CubicSegment::scanlineIntersections(double x[3], int dy[3], double y) const {
  293. return horizontalScanlineIntersections(x, dy, y);
  294. }
  295. int LinearSegment::horizontalScanlineIntersections(double x[3], int dy[3], double y) const {
  296. if ((y >= p[0].y && y < p[1].y) || (y >= p[1].y && y < p[0].y)) {
  297. double param = (y-p[0].y)/(p[1].y-p[0].y);
  298. x[0] = mix(p[0].x, p[1].x, param);
  299. dy[0] = sign(p[1].y-p[0].y);
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. int LinearSegment::verticalScanlineIntersections(double y[3], int dx[3], double x) const {
  305. if ((x >= p[0].x && x < p[1].x) || (x >= p[1].x && x < p[0].x)) {
  306. double param = (x-p[0].x)/(p[1].x-p[0].x);
  307. y[0] = mix(p[0].y, p[1].y, param);
  308. dx[0] = sign(p[1].x-p[0].x);
  309. return 1;
  310. }
  311. return 0;
  312. }
  313. // p0, p1, q, r, s in the dimension of the scanline
  314. // p0 = scanline - first endpoint
  315. // p1 = scanline - last endpoint
  316. // q, r, s = first, second, third order derivatives (see bezier-solver)
  317. // descendingAtEnd is true if the first non-zero derivative at the last endpoint is negative
  318. static int curveScanlineIntersections(double t[3], int d[3], double p0, double p1, double q, double r, double s, bool descendingAtEnd) {
  319. int total = 0;
  320. int nextD = p0 > 0 ? 1 : -1;
  321. t[total] = 0;
  322. if (p0 == 0) {
  323. if (q > 0 || (q == 0 && (r > 0 || (r == 0 && s > 0))))
  324. d[total++] = 1;
  325. else
  326. nextD = 1;
  327. }
  328. {
  329. double ts[3];
  330. int solutions = solveCubic(ts, s, r, q, -p0);
  331. // Sort solutions
  332. double tmp;
  333. if (solutions >= 2) {
  334. if (ts[0] > ts[1])
  335. tmp = ts[0], ts[0] = ts[1], ts[1] = tmp;
  336. if (solutions >= 3 && ts[1] > ts[2]) {
  337. tmp = ts[1], ts[1] = ts[2], ts[2] = tmp;
  338. if (ts[0] > ts[1])
  339. tmp = ts[0], ts[0] = ts[1], ts[1] = tmp;
  340. }
  341. }
  342. for (int i = 0; i < solutions && total < 3; ++i) {
  343. if (ts[i] >= 0 && ts[i] <= 1) {
  344. t[total] = ts[i];
  345. if (nextD*(q+(2*r+3*s*ts[i])*ts[i]) >= 0) {
  346. d[total++] = nextD;
  347. nextD = -nextD;
  348. }
  349. }
  350. }
  351. }
  352. if (p1 == 0) {
  353. if (nextD > 0 && total > 0) {
  354. --total;
  355. nextD = -1;
  356. }
  357. if (descendingAtEnd && total < 3) {
  358. t[total] = 1;
  359. if (nextD < 0) {
  360. d[total++] = -1;
  361. nextD = 1;
  362. }
  363. }
  364. }
  365. if (nextD != (p1 >= 0 ? 1 : -1)) {
  366. if (total > 0)
  367. --total;
  368. else {
  369. if (fabs(p0) > fabs(p1))
  370. t[total] = 1;
  371. d[total++] = nextD;
  372. }
  373. }
  374. return total;
  375. }
  376. int QuadraticSegment::horizontalScanlineIntersections(double x[3], int dy[3], double y) const {
  377. double p01 = p[1].y-p[0].y;
  378. double p12 = p[2].y-p[1].y;
  379. bool descendingAtEnd = p[2].y < p[1].y || (p[2].y == p[1].y && p[2].y < p[0].y);
  380. double t[3] = { };
  381. int n = curveScanlineIntersections(t, dy, y-p[0].y, y-p[2].y, 2*p01, p12-p01, 0, descendingAtEnd);
  382. x[0] = point(t[0]).x;
  383. x[1] = point(t[1]).x;
  384. x[2] = point(t[2]).x;
  385. return n;
  386. }
  387. int QuadraticSegment::verticalScanlineIntersections(double y[3], int dx[3], double x) const {
  388. double p01 = p[1].x-p[0].x;
  389. double p12 = p[2].x-p[1].x;
  390. bool descendingAtEnd = p[2].x < p[1].x || (p[2].x == p[1].x && p[2].x < p[0].x);
  391. double t[3] = { };
  392. int n = curveScanlineIntersections(t, dx, x-p[0].x, x-p[2].x, 2*p01, p12-p01, 0, descendingAtEnd);
  393. y[0] = point(t[0]).y;
  394. y[1] = point(t[1]).y;
  395. y[2] = point(t[2]).y;
  396. return n;
  397. }
  398. int CubicSegment::horizontalScanlineIntersections(double x[3], int dy[3], double y) const {
  399. double p01 = p[1].y-p[0].y;
  400. double p12 = p[2].y-p[1].y;
  401. double p23 = p[3].y-p[2].y;
  402. bool descendingAtEnd = 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)));
  403. double t[3] = { };
  404. int n = curveScanlineIntersections(t, dy, y-p[0].y, y-p[3].y, 3*p01, 3*(p12-p01), p23-2*p12+p01, descendingAtEnd);
  405. x[0] = point(t[0]).x;
  406. x[1] = point(t[1]).x;
  407. x[2] = point(t[2]).x;
  408. return n;
  409. }
  410. int CubicSegment::verticalScanlineIntersections(double y[3], int dx[3], double x) const {
  411. double p01 = p[1].x-p[0].x;
  412. double p12 = p[2].x-p[1].x;
  413. double p23 = p[3].x-p[2].x;
  414. bool descendingAtEnd = 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)));
  415. double t[3] = { };
  416. int n = curveScanlineIntersections(t, dx, x-p[0].x, x-p[3].x, 3*p01, 3*(p12-p01), p23-2*p12+p01, descendingAtEnd);
  417. y[0] = point(t[0]).y;
  418. y[1] = point(t[1]).y;
  419. y[2] = point(t[2]).y;
  420. return n;
  421. }
  422. static void pointBounds(Point2 p, double &l, double &b, double &r, double &t) {
  423. if (p.x < l) l = p.x;
  424. if (p.y < b) b = p.y;
  425. if (p.x > r) r = p.x;
  426. if (p.y > t) t = p.y;
  427. }
  428. void LinearSegment::bound(double &l, double &b, double &r, double &t) const {
  429. pointBounds(p[0], l, b, r, t);
  430. pointBounds(p[1], l, b, r, t);
  431. }
  432. void QuadraticSegment::bound(double &l, double &b, double &r, double &t) const {
  433. pointBounds(p[0], l, b, r, t);
  434. pointBounds(p[2], l, b, r, t);
  435. Vector2 bot = (p[1]-p[0])-(p[2]-p[1]);
  436. if (bot.x) {
  437. double param = (p[1].x-p[0].x)/bot.x;
  438. if (param > 0 && param < 1)
  439. pointBounds(point(param), l, b, r, t);
  440. }
  441. if (bot.y) {
  442. double param = (p[1].y-p[0].y)/bot.y;
  443. if (param > 0 && param < 1)
  444. pointBounds(point(param), l, b, r, t);
  445. }
  446. }
  447. void CubicSegment::bound(double &l, double &b, double &r, double &t) const {
  448. pointBounds(p[0], l, b, r, t);
  449. pointBounds(p[3], l, b, r, t);
  450. Vector2 a0 = p[1]-p[0];
  451. Vector2 a1 = 2*(p[2]-p[1]-a0);
  452. Vector2 a2 = p[3]-3*p[2]+3*p[1]-p[0];
  453. double params[2];
  454. int solutions;
  455. solutions = solveQuadratic(params, a2.x, a1.x, a0.x);
  456. for (int i = 0; i < solutions; ++i)
  457. if (params[i] > 0 && params[i] < 1)
  458. pointBounds(point(params[i]), l, b, r, t);
  459. solutions = solveQuadratic(params, a2.y, a1.y, a0.y);
  460. for (int i = 0; i < solutions; ++i)
  461. if (params[i] > 0 && params[i] < 1)
  462. pointBounds(point(params[i]), l, b, r, t);
  463. }
  464. void LinearSegment::reverse() {
  465. Point2 tmp = p[0];
  466. p[0] = p[1];
  467. p[1] = tmp;
  468. }
  469. void QuadraticSegment::reverse() {
  470. Point2 tmp = p[0];
  471. p[0] = p[2];
  472. p[2] = tmp;
  473. }
  474. void CubicSegment::reverse() {
  475. Point2 tmp = p[0];
  476. p[0] = p[3];
  477. p[3] = tmp;
  478. tmp = p[1];
  479. p[1] = p[2];
  480. p[2] = tmp;
  481. }
  482. void LinearSegment::moveStartPoint(Point2 to) {
  483. p[0] = to;
  484. }
  485. void QuadraticSegment::moveStartPoint(Point2 to) {
  486. Vector2 origSDir = p[0]-p[1];
  487. Point2 origP1 = p[1];
  488. p[1] += crossProduct(p[0]-p[1], to-p[0])/crossProduct(p[0]-p[1], p[2]-p[1])*(p[2]-p[1]);
  489. p[0] = to;
  490. if (dotProduct(origSDir, p[0]-p[1]) < 0)
  491. p[1] = origP1;
  492. }
  493. void CubicSegment::moveStartPoint(Point2 to) {
  494. p[1] += to-p[0];
  495. p[0] = to;
  496. }
  497. void LinearSegment::moveEndPoint(Point2 to) {
  498. p[1] = to;
  499. }
  500. void QuadraticSegment::moveEndPoint(Point2 to) {
  501. Vector2 origEDir = p[2]-p[1];
  502. Point2 origP1 = p[1];
  503. p[1] += crossProduct(p[2]-p[1], to-p[2])/crossProduct(p[2]-p[1], p[0]-p[1])*(p[0]-p[1]);
  504. p[2] = to;
  505. if (dotProduct(origEDir, p[2]-p[1]) < 0)
  506. p[1] = origP1;
  507. }
  508. void CubicSegment::moveEndPoint(Point2 to) {
  509. p[2] += to-p[3];
  510. p[3] = to;
  511. }
  512. void LinearSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  513. part1 = new LinearSegment(p[0], point(1/3.), color);
  514. part2 = new LinearSegment(point(1/3.), point(2/3.), color);
  515. part3 = new LinearSegment(point(2/3.), p[1], color);
  516. }
  517. void QuadraticSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  518. part1 = new QuadraticSegment(p[0], mix(p[0], p[1], 1/3.), point(1/3.), color);
  519. 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);
  520. part3 = new QuadraticSegment(point(2/3.), mix(p[1], p[2], 2/3.), p[2], color);
  521. }
  522. void CubicSegment::splitInThirds(EdgeSegment *&part1, EdgeSegment *&part2, EdgeSegment *&part3) const {
  523. 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);
  524. part2 = new CubicSegment(point(1/3.),
  525. 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.),
  526. 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.),
  527. point(2/3.), color);
  528. 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);
  529. }
  530. EdgeSegment *QuadraticSegment::convertToCubic() const {
  531. return new CubicSegment(p[0], mix(p[0], p[1], 2/3.), mix(p[1], p[2], 1/3.), p[2], color);
  532. }
  533. void CubicSegment::deconverge(int param, double amount) {
  534. Vector2 dir = direction(param);
  535. Vector2 normal = dir.getOrthonormal();
  536. double h = dotProduct(directionChange(param)-dir, normal);
  537. switch (param) {
  538. case 0:
  539. p[1] += amount*(dir+sign(h)*sqrt(fabs(h))*normal);
  540. break;
  541. case 1:
  542. p[2] -= amount*(dir-sign(h)*sqrt(fabs(h))*normal);
  543. break;
  544. }
  545. }
  546. }