2
0

edge-segments.cpp 14 KB

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