edge-segments.cpp 18 KB

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