You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1543 lines
39 KiB

  1. /* Copyright (C) 2001-2015 Peter Selinger.
  2. * This file is part of Potrace. It is free software and it is covered
  3. * by the GNU General Public License. See the file COPYING for details. */
  4. /* transform jaggy paths into smooth curves */
  5. #ifdef HAVE_CONFIG_H
  6. #include <config.h>
  7. #endif
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "potracelib.h"
  13. #include "curve.h"
  14. #include "lists.h"
  15. #include "auxiliary.h"
  16. #include "trace.h"
  17. #include "progress.h"
  18. #define INFTY 10000000 /* it suffices that this is longer than any
  19. * path; it need not be really infinite */
  20. #define COS179 -0.999847695156 /* the cosine of 179 degrees */
  21. /* ---------------------------------------------------------------------- */
  22. #define SAFE_CALLOC( var, n, typ ) \
  23. if( ( var = (typ*) calloc( n, sizeof(typ) ) ) == NULL ) \
  24. goto calloc_error
  25. /* ---------------------------------------------------------------------- */
  26. /* auxiliary functions */
  27. /* return a direction that is 90 degrees counterclockwise from p2-p0,
  28. * but then restricted to one of the major wind directions (n, nw, w, etc) */
  29. static inline point_t dorth_infty( dpoint_t p0, dpoint_t p2 )
  30. {
  31. point_t r;
  32. r.y = sign( p2.x - p0.x );
  33. r.x = -sign( p2.y - p0.y );
  34. return r;
  35. }
  36. /* return (p1-p0)x(p2-p0), the area of the parallelogram */
  37. static inline double dpara( dpoint_t p0, dpoint_t p1, dpoint_t p2 )
  38. {
  39. double x1, y1, x2, y2;
  40. x1 = p1.x - p0.x;
  41. y1 = p1.y - p0.y;
  42. x2 = p2.x - p0.x;
  43. y2 = p2.y - p0.y;
  44. return x1 * y2 - x2 * y1;
  45. }
  46. /* ddenom/dpara have the property that the square of radius 1 centered
  47. * at p1 intersects the line p0p2 iff |dpara(p0,p1,p2)| <= ddenom(p0,p2) */
  48. static inline double ddenom( dpoint_t p0, dpoint_t p2 )
  49. {
  50. point_t r = dorth_infty( p0, p2 );
  51. return r.y * (p2.x - p0.x) - r.x * (p2.y - p0.y);
  52. }
  53. /* return 1 if a <= b < c < a, in a cyclic sense (mod n) */
  54. static inline int cyclic( int a, int b, int c )
  55. {
  56. if( a<=c )
  57. {
  58. return a<=b && b<c;
  59. }
  60. else
  61. {
  62. return a<=b || b<c;
  63. }
  64. }
  65. /* determine the center and slope of the line i..j. Assume i<j. Needs
  66. * "sum" components of p to be set. */
  67. static void pointslope( privpath_t* pp, int i, int j, dpoint_t* ctr, dpoint_t* dir )
  68. {
  69. /* assume i<j */
  70. int n = pp->len;
  71. sums_t* sums = pp->sums;
  72. double x, y, x2, xy, y2;
  73. double k;
  74. double a, b, c, lambda2, l;
  75. int r = 0; /* rotations from i to j */
  76. while( j>=n )
  77. {
  78. j -= n;
  79. r += 1;
  80. }
  81. while( i>=n )
  82. {
  83. i -= n;
  84. r -= 1;
  85. }
  86. while( j<0 )
  87. {
  88. j += n;
  89. r -= 1;
  90. }
  91. while( i<0 )
  92. {
  93. i += n;
  94. r += 1;
  95. }
  96. x = sums[j + 1].x - sums[i].x + r * sums[n].x;
  97. y = sums[j + 1].y - sums[i].y + r * sums[n].y;
  98. x2 = sums[j + 1].x2 - sums[i].x2 + r * sums[n].x2;
  99. xy = sums[j + 1].xy - sums[i].xy + r * sums[n].xy;
  100. y2 = sums[j + 1].y2 - sums[i].y2 + r * sums[n].y2;
  101. k = j + 1 - i + r * n;
  102. ctr->x = x / k;
  103. ctr->y = y / k;
  104. a = (x2 - (double) x * x / k) / k;
  105. b = (xy - (double) x * y / k) / k;
  106. c = (y2 - (double) y * y / k) / k;
  107. lambda2 = ( a + c + sqrt( (a - c) * (a - c) + 4 * b * b ) ) / 2; /* larger e.value */
  108. /* now find e.vector for lambda2 */
  109. a -= lambda2;
  110. c -= lambda2;
  111. if( fabs( a ) >= fabs( c ) )
  112. {
  113. l = sqrt( a * a + b * b );
  114. if( l!=0 )
  115. {
  116. dir->x = -b / l;
  117. dir->y = a / l;
  118. }
  119. }
  120. else
  121. {
  122. l = sqrt( c * c + b * b );
  123. if( l!=0 )
  124. {
  125. dir->x = -c / l;
  126. dir->y = b / l;
  127. }
  128. }
  129. if( l==0 )
  130. {
  131. dir->x = dir->y = 0; /* sometimes this can happen when k=4:
  132. * the two eigenvalues coincide */
  133. }
  134. }
  135. /* the type of (affine) quadratic forms, represented as symmetric 3x3
  136. * matrices. The value of the quadratic form at a vector (x,y) is v^t
  137. * Q v, where v = (x,y,1)^t. */
  138. typedef double quadform_t[3][3];
  139. /* Apply quadratic form Q to vector w = (w.x,w.y) */
  140. static inline double quadform( quadform_t Q, dpoint_t w )
  141. {
  142. double v[3];
  143. int i, j;
  144. double sum;
  145. v[0] = w.x;
  146. v[1] = w.y;
  147. v[2] = 1;
  148. sum = 0.0;
  149. for( i = 0; i<3; i++ )
  150. {
  151. for( j = 0; j<3; j++ )
  152. {
  153. sum += v[i] * Q[i][j] * v[j];
  154. }
  155. }
  156. return sum;
  157. }
  158. /* calculate p1 x p2 */
  159. static inline int xprod( point_t p1, point_t p2 )
  160. {
  161. return p1.x * p2.y - p1.y * p2.x;
  162. }
  163. /* calculate (p1-p0)x(p3-p2) */
  164. static inline double cprod( dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3 )
  165. {
  166. double x1, y1, x2, y2;
  167. x1 = p1.x - p0.x;
  168. y1 = p1.y - p0.y;
  169. x2 = p3.x - p2.x;
  170. y2 = p3.y - p2.y;
  171. return x1 * y2 - x2 * y1;
  172. }
  173. /* calculate (p1-p0)*(p2-p0) */
  174. static inline double iprod( dpoint_t p0, dpoint_t p1, dpoint_t p2 )
  175. {
  176. double x1, y1, x2, y2;
  177. x1 = p1.x - p0.x;
  178. y1 = p1.y - p0.y;
  179. x2 = p2.x - p0.x;
  180. y2 = p2.y - p0.y;
  181. return x1 * x2 + y1 * y2;
  182. }
  183. /* calculate (p1-p0)*(p3-p2) */
  184. static inline double iprod1( dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3 )
  185. {
  186. double x1, y1, x2, y2;
  187. x1 = p1.x - p0.x;
  188. y1 = p1.y - p0.y;
  189. x2 = p3.x - p2.x;
  190. y2 = p3.y - p2.y;
  191. return x1 * x2 + y1 * y2;
  192. }
  193. /* calculate distance between two points */
  194. static inline double ddist( dpoint_t p, dpoint_t q )
  195. {
  196. return sqrt( sq( p.x - q.x ) + sq( p.y - q.y ) );
  197. }
  198. /* calculate point of a bezier curve */
  199. static inline dpoint_t bezier( double t, dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3 )
  200. {
  201. double s = 1 - t;
  202. dpoint_t res;
  203. /* Note: a good optimizing compiler (such as gcc-3) reduces the
  204. * following to 16 multiplications, using common subexpression
  205. * elimination. */
  206. res.x = s * s * s * p0.x + 3 * (s * s * t) * p1.x + 3 * (t * t * s) * p2.x + t * t * t * p3.x;
  207. res.y = s * s * s * p0.y + 3 * (s * s * t) * p1.y + 3 * (t * t * s) * p2.y + t * t * t * p3.y;
  208. return res;
  209. }
  210. /* calculate the point t in [0..1] on the (convex) bezier curve
  211. * (p0,p1,p2,p3) which is tangent to q1-q0. Return -1.0 if there is no
  212. * solution in [0..1]. */
  213. static double tangent( dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3, dpoint_t q0,
  214. dpoint_t q1 )
  215. {
  216. double A, B, C; /* (1-t)^2 A + 2(1-t)t B + t^2 C = 0 */
  217. double a, b, c; /* a t^2 + b t + c = 0 */
  218. double d, s, r1, r2;
  219. A = cprod( p0, p1, q0, q1 );
  220. B = cprod( p1, p2, q0, q1 );
  221. C = cprod( p2, p3, q0, q1 );
  222. a = A - 2 * B + C;
  223. b = -2 * A + 2 * B;
  224. c = A;
  225. d = b * b - 4 * a * c;
  226. if( a==0 || d<0 )
  227. {
  228. return -1.0;
  229. }
  230. s = sqrt( d );
  231. r1 = (-b + s) / (2 * a);
  232. r2 = (-b - s) / (2 * a);
  233. if( r1 >= 0 && r1 <= 1 )
  234. {
  235. return r1;
  236. }
  237. else if( r2 >= 0 && r2 <= 1 )
  238. {
  239. return r2;
  240. }
  241. else
  242. {
  243. return -1.0;
  244. }
  245. }
  246. /* ---------------------------------------------------------------------- */
  247. /* Preparation: fill in the sum* fields of a path (used for later
  248. * rapid summing). Return 0 on success, 1 with errno set on
  249. * failure. */
  250. static int calc_sums( privpath_t* pp )
  251. {
  252. int i, x, y;
  253. int n = pp->len;
  254. SAFE_CALLOC( pp->sums, pp->len + 1, sums_t );
  255. /* origin */
  256. pp->x0 = pp->pt[0].x;
  257. pp->y0 = pp->pt[0].y;
  258. /* preparatory computation for later fast summing */
  259. pp->sums[0].x2 = pp->sums[0].xy = pp->sums[0].y2 = pp->sums[0].x = pp->sums[0].y = 0;
  260. for( i = 0; i<n; i++ )
  261. {
  262. x = pp->pt[i].x - pp->x0;
  263. y = pp->pt[i].y - pp->y0;
  264. pp->sums[i + 1].x = pp->sums[i].x + x;
  265. pp->sums[i + 1].y = pp->sums[i].y + y;
  266. pp->sums[i + 1].x2 = pp->sums[i].x2 + x * x;
  267. pp->sums[i + 1].xy = pp->sums[i].xy + x * y;
  268. pp->sums[i + 1].y2 = pp->sums[i].y2 + y * y;
  269. }
  270. return 0;
  271. calloc_error:
  272. return 1;
  273. }
  274. /* ---------------------------------------------------------------------- */
  275. /* Stage 1: determine the straight subpaths (Sec. 2.2.1). Fill in the
  276. * "lon" component of a path object (based on pt/len). For each i,
  277. * lon[i] is the furthest index such that a straight line can be drawn
  278. * from i to lon[i]. Return 1 on error with errno set, else 0. */
  279. /* this algorithm depends on the fact that the existence of straight
  280. * subpaths is a triplewise property. I.e., there exists a straight
  281. * line through squares i0,...,in iff there exists a straight line
  282. * through i,j,k, for all i0<=i<j<k<=in. (Proof?) */
  283. /* this implementation of calc_lon is O(n^2). It replaces an older
  284. * O(n^3) version. A "constraint" means that future points must
  285. * satisfy xprod(constraint[0], cur) >= 0 and xprod(constraint[1],
  286. * cur) <= 0. */
  287. /* Remark for Potrace 1.1: the current implementation of calc_lon is
  288. * more complex than the implementation found in Potrace 1.0, but it
  289. * is considerably faster. The introduction of the "nc" data structure
  290. * means that we only have to test the constraints for "corner"
  291. * points. On a typical input file, this speeds up the calc_lon
  292. * function by a factor of 31.2, thereby decreasing its time share
  293. * within the overall Potrace algorithm from 72.6% to 7.82%, and
  294. * speeding up the overall algorithm by a factor of 3.36. On another
  295. * input file, calc_lon was sped up by a factor of 6.7, decreasing its
  296. * time share from 51.4% to 13.61%, and speeding up the overall
  297. * algorithm by a factor of 1.78. In any case, the savings are
  298. * substantial. */
  299. /* returns 0 on success, 1 on error with errno set */
  300. static int calc_lon( privpath_t* pp )
  301. {
  302. point_t* pt = pp->pt;
  303. int n = pp->len;
  304. int i, j, k, k1;
  305. int ct[4], dir;
  306. point_t constraint[2];
  307. point_t cur;
  308. point_t off;
  309. int* pivk = NULL; /* pivk[n] */
  310. int* nc = NULL; /* nc[n]: next corner */
  311. point_t dk; /* direction of k-k1 */
  312. int a, b, c, d;
  313. SAFE_CALLOC( pivk, n, int );
  314. SAFE_CALLOC( nc, n, int );
  315. /* initialize the nc data structure. Point from each point to the
  316. * furthest future point to which it is connected by a vertical or
  317. * horizontal segment. We take advantage of the fact that there is
  318. * always a direction change at 0 (due to the path decomposition
  319. * algorithm). But even if this were not so, there is no harm, as
  320. * in practice, correctness does not depend on the word "furthest"
  321. * above. */
  322. k = 0;
  323. for( i = n - 1; i>=0; i-- )
  324. {
  325. if( pt[i].x != pt[k].x && pt[i].y != pt[k].y )
  326. {
  327. k = i + 1; /* necessarily i<n-1 in this case */
  328. }
  329. nc[i] = k;
  330. }
  331. SAFE_CALLOC( pp->lon, n, int );
  332. /* determine pivot points: for each i, let pivk[i] be the furthest k
  333. * such that all j with i<j<k lie on a line connecting i,k. */
  334. for( i = n - 1; i>=0; i-- )
  335. {
  336. ct[0] = ct[1] = ct[2] = ct[3] = 0;
  337. /* keep track of "directions" that have occurred */
  338. dir = ( 3 + 3 * (pt[mod( i + 1, n )].x - pt[i].x) + (pt[mod( i + 1, n )].y - pt[i].y) ) / 2;
  339. ct[dir]++;
  340. constraint[0].x = 0;
  341. constraint[0].y = 0;
  342. constraint[1].x = 0;
  343. constraint[1].y = 0;
  344. /* find the next k such that no straight line from i to k */
  345. k = nc[i];
  346. k1 = i;
  347. while( 1 )
  348. {
  349. dir = ( 3 + 3 * sign( pt[k].x - pt[k1].x ) + sign( pt[k].y - pt[k1].y ) ) / 2;
  350. ct[dir]++;
  351. /* if all four "directions" have occurred, cut this path */
  352. if( ct[0] && ct[1] && ct[2] && ct[3] )
  353. {
  354. pivk[i] = k1;
  355. goto foundk;
  356. }
  357. cur.x = pt[k].x - pt[i].x;
  358. cur.y = pt[k].y - pt[i].y;
  359. /* see if current constraint is violated */
  360. if( xprod( constraint[0], cur ) < 0 || xprod( constraint[1], cur ) > 0 )
  361. {
  362. goto constraint_viol;
  363. }
  364. /* else, update constraint */
  365. if( abs( cur.x ) <= 1 && abs( cur.y ) <= 1 )
  366. {
  367. /* no constraint */
  368. }
  369. else
  370. {
  371. off.x = cur.x + ( ( cur.y>=0 && (cur.y>0 || cur.x<0) ) ? 1 : -1 );
  372. off.y = cur.y + ( ( cur.x<=0 && (cur.x<0 || cur.y<0) ) ? 1 : -1 );
  373. if( xprod( constraint[0], off ) >= 0 )
  374. {
  375. constraint[0] = off;
  376. }
  377. off.x = cur.x + ( ( cur.y<=0 && (cur.y<0 || cur.x<0) ) ? 1 : -1 );
  378. off.y = cur.y + ( ( cur.x>=0 && (cur.x>0 || cur.y<0) ) ? 1 : -1 );
  379. if( xprod( constraint[1], off ) <= 0 )
  380. {
  381. constraint[1] = off;
  382. }
  383. }
  384. k1 = k;
  385. k = nc[k1];
  386. if( !cyclic( k, i, k1 ) )
  387. {
  388. break;
  389. }
  390. }
  391. constraint_viol:
  392. /* k1 was the last "corner" satisfying the current constraint, and
  393. * k is the first one violating it. We now need to find the last
  394. * point along k1..k which satisfied the constraint. */
  395. dk.x = sign( pt[k].x - pt[k1].x );
  396. dk.y = sign( pt[k].y - pt[k1].y );
  397. cur.x = pt[k1].x - pt[i].x;
  398. cur.y = pt[k1].y - pt[i].y;
  399. /* find largest integer j such that xprod(constraint[0], cur+j*dk)
  400. * >= 0 and xprod(constraint[1], cur+j*dk) <= 0. Use bilinearity
  401. * of xprod. */
  402. a = xprod( constraint[0], cur );
  403. b = xprod( constraint[0], dk );
  404. c = xprod( constraint[1], cur );
  405. d = xprod( constraint[1], dk );
  406. /* find largest integer j such that a+j*b>=0 and c+j*d<=0. This
  407. * can be solved with integer arithmetic. */
  408. j = INFTY;
  409. if( b<0 )
  410. {
  411. j = floordiv( a, -b );
  412. }
  413. if( d>0 )
  414. {
  415. j = min( j, floordiv( -c, d ) );
  416. }
  417. pivk[i] = mod( k1 + j, n );
  418. foundk:
  419. ;
  420. } /* for i */
  421. /* clean up: for each i, let lon[i] be the largest k such that for
  422. * all i' with i<=i'<k, i'<k<=pivk[i']. */
  423. j = pivk[n - 1];
  424. pp->lon[n - 1] = j;
  425. for( i = n - 2; i>=0; i-- )
  426. {
  427. if( cyclic( i + 1, pivk[i], j ) )
  428. {
  429. j = pivk[i];
  430. }
  431. pp->lon[i] = j;
  432. }
  433. for( i = n - 1; cyclic( mod( i + 1, n ), j, pp->lon[i] ); i-- )
  434. {
  435. pp->lon[i] = j;
  436. }
  437. free( pivk );
  438. free( nc );
  439. return 0;
  440. calloc_error:
  441. free( pivk );
  442. free( nc );
  443. return 1;
  444. }
  445. /* ---------------------------------------------------------------------- */
  446. /* Stage 2: calculate the optimal polygon (Sec. 2.2.2-2.2.4). */
  447. /* Auxiliary function: calculate the penalty of an edge from i to j in
  448. * the given path. This needs the "lon" and "sum*" data. */
  449. static double penalty3( privpath_t* pp, int i, int j )
  450. {
  451. int n = pp->len;
  452. point_t* pt = pp->pt;
  453. sums_t* sums = pp->sums;
  454. /* assume 0<=i<j<=n */
  455. double x, y, x2, xy, y2;
  456. double k;
  457. double a, b, c, s;
  458. double px, py, ex, ey;
  459. int r = 0; /* rotations from i to j */
  460. if( j>=n )
  461. {
  462. j -= n;
  463. r = 1;
  464. }
  465. /* critical inner loop: the "if" gives a 4.6 percent speedup */
  466. if( r == 0 )
  467. {
  468. x = sums[j + 1].x - sums[i].x;
  469. y = sums[j + 1].y - sums[i].y;
  470. x2 = sums[j + 1].x2 - sums[i].x2;
  471. xy = sums[j + 1].xy - sums[i].xy;
  472. y2 = sums[j + 1].y2 - sums[i].y2;
  473. k = j + 1 - i;
  474. }
  475. else
  476. {
  477. x = sums[j + 1].x - sums[i].x + sums[n].x;
  478. y = sums[j + 1].y - sums[i].y + sums[n].y;
  479. x2 = sums[j + 1].x2 - sums[i].x2 + sums[n].x2;
  480. xy = sums[j + 1].xy - sums[i].xy + sums[n].xy;
  481. y2 = sums[j + 1].y2 - sums[i].y2 + sums[n].y2;
  482. k = j + 1 - i + n;
  483. }
  484. px = (pt[i].x + pt[j].x) / 2.0 - pt[0].x;
  485. py = (pt[i].y + pt[j].y) / 2.0 - pt[0].y;
  486. ey = (pt[j].x - pt[i].x);
  487. ex = -(pt[j].y - pt[i].y);
  488. a = ( (x2 - 2 * x * px) / k + px * px );
  489. b = ( (xy - x * py - y * px) / k + px * py );
  490. c = ( (y2 - 2 * y * py) / k + py * py );
  491. s = ex * ex * a + 2 * ex * ey * b + ey * ey * c;
  492. return sqrt( s );
  493. }
  494. /* find the optimal polygon. Fill in the m and po components. Return 1
  495. * on failure with errno set, else 0. Non-cyclic version: assumes i=0
  496. * is in the polygon. Fixme: implement cyclic version. */
  497. static int bestpolygon( privpath_t* pp )
  498. {
  499. int i, j, m, k;
  500. int n = pp->len;
  501. double* pen = NULL; /* pen[n+1]: penalty vector */
  502. int* prev = NULL; /* prev[n+1]: best path pointer vector */
  503. int* clip0 = NULL; /* clip0[n]: longest segment pointer, non-cyclic */
  504. int* clip1 = NULL; /* clip1[n+1]: backwards segment pointer, non-cyclic */
  505. int* seg0 = NULL; /* seg0[m+1]: forward segment bounds, m<=n */
  506. int* seg1 = NULL; /* seg1[m+1]: backward segment bounds, m<=n */
  507. double thispen;
  508. double best;
  509. int c;
  510. SAFE_CALLOC( pen, n + 1, double );
  511. SAFE_CALLOC( prev, n + 1, int );
  512. SAFE_CALLOC( clip0, n, int );
  513. SAFE_CALLOC( clip1, n + 1, int );
  514. SAFE_CALLOC( seg0, n + 1, int );
  515. SAFE_CALLOC( seg1, n + 1, int );
  516. /* calculate clipped paths */
  517. for( i = 0; i<n; i++ )
  518. {
  519. c = mod( pp->lon[mod( i - 1, n )] - 1, n );
  520. if( c == i )
  521. {
  522. c = mod( i + 1, n );
  523. }
  524. if( c < i )
  525. {
  526. clip0[i] = n;
  527. }
  528. else
  529. {
  530. clip0[i] = c;
  531. }
  532. }
  533. /* calculate backwards path clipping, non-cyclic. j <= clip0[i] iff
  534. * clip1[j] <= i, for i,j=0..n. */
  535. j = 1;
  536. for( i = 0; i<n; i++ )
  537. {
  538. while( j <= clip0[i] )
  539. {
  540. clip1[j] = i;
  541. j++;
  542. }
  543. }
  544. /* calculate seg0[j] = longest path from 0 with j segments */
  545. i = 0;
  546. for( j = 0; i<n; j++ )
  547. {
  548. seg0[j] = i;
  549. i = clip0[i];
  550. }
  551. seg0[j] = n;
  552. m = j;
  553. /* calculate seg1[j] = longest path to n with m-j segments */
  554. i = n;
  555. for( j = m; j>0; j-- )
  556. {
  557. seg1[j] = i;
  558. i = clip1[i];
  559. }
  560. seg1[0] = 0;
  561. /* now find the shortest path with m segments, based on penalty3 */
  562. /* note: the outer 2 loops jointly have at most n iterations, thus
  563. * the worst-case behavior here is quadratic. In practice, it is
  564. * close to linear since the inner loop tends to be short. */
  565. pen[0] = 0;
  566. for( j = 1; j<=m; j++ )
  567. {
  568. for( i = seg1[j]; i<=seg0[j]; i++ )
  569. {
  570. best = -1;
  571. for( k = seg0[j - 1]; k>=clip1[i]; k-- )
  572. {
  573. thispen = penalty3( pp, k, i ) + pen[k];
  574. if( best < 0 || thispen < best )
  575. {
  576. prev[i] = k;
  577. best = thispen;
  578. }
  579. }
  580. pen[i] = best;
  581. }
  582. }
  583. pp->m = m;
  584. SAFE_CALLOC( pp->po, m, int );
  585. /* read off shortest path */
  586. for( i = n, j = m - 1; i>0; j-- )
  587. {
  588. i = prev[i];
  589. pp->po[j] = i;
  590. }
  591. free( pen );
  592. free( prev );
  593. free( clip0 );
  594. free( clip1 );
  595. free( seg0 );
  596. free( seg1 );
  597. return 0;
  598. calloc_error:
  599. free( pen );
  600. free( prev );
  601. free( clip0 );
  602. free( clip1 );
  603. free( seg0 );
  604. free( seg1 );
  605. return 1;
  606. }
  607. /* ---------------------------------------------------------------------- */
  608. /* Stage 3: vertex adjustment (Sec. 2.3.1). */
  609. /* Adjust vertices of optimal polygon: calculate the intersection of
  610. * the two "optimal" line segments, then move it into the unit square
  611. * if it lies outside. Return 1 with errno set on error; 0 on
  612. * success. */
  613. static int adjust_vertices( privpath_t* pp )
  614. {
  615. int m = pp->m;
  616. int* po = pp->po;
  617. int n = pp->len;
  618. point_t* pt = pp->pt;
  619. int x0 = pp->x0;
  620. int y0 = pp->y0;
  621. dpoint_t* ctr = NULL; /* ctr[m] */
  622. dpoint_t* dir = NULL; /* dir[m] */
  623. quadform_t* q = NULL; /* q[m] */
  624. double v[3];
  625. double d;
  626. int i, j, k, l;
  627. dpoint_t s;
  628. int r;
  629. SAFE_CALLOC( ctr, m, dpoint_t );
  630. SAFE_CALLOC( dir, m, dpoint_t );
  631. SAFE_CALLOC( q, m, quadform_t );
  632. r = privcurve_init( &pp->curve, m );
  633. if( r )
  634. {
  635. goto calloc_error;
  636. }
  637. /* calculate "optimal" point-slope representation for each line
  638. * segment */
  639. for( i = 0; i<m; i++ )
  640. {
  641. j = po[mod( i + 1, m )];
  642. j = mod( j - po[i], n ) + po[i];
  643. pointslope( pp, po[i], j, &ctr[i], &dir[i] );
  644. }
  645. /* represent each line segment as a singular quadratic form; the
  646. * distance of a point (x,y) from the line segment will be
  647. * (x,y,1)Q(x,y,1)^t, where Q=q[i]. */
  648. for( i = 0; i<m; i++ )
  649. {
  650. d = sq( dir[i].x ) + sq( dir[i].y );
  651. if( d == 0.0 )
  652. {
  653. for( j = 0; j<3; j++ )
  654. {
  655. for( k = 0; k<3; k++ )
  656. {
  657. q[i][j][k] = 0;
  658. }
  659. }
  660. }
  661. else
  662. {
  663. v[0] = dir[i].y;
  664. v[1] = -dir[i].x;
  665. v[2] = -v[1] * ctr[i].y - v[0] * ctr[i].x;
  666. for( l = 0; l<3; l++ )
  667. {
  668. for( k = 0; k<3; k++ )
  669. {
  670. q[i][l][k] = v[l] * v[k] / d;
  671. }
  672. }
  673. }
  674. }
  675. /* now calculate the "intersections" of consecutive segments.
  676. * Instead of using the actual intersection, we find the point
  677. * within a given unit square which minimizes the square distance to
  678. * the two lines. */
  679. for( i = 0; i<m; i++ )
  680. {
  681. quadform_t Q;
  682. dpoint_t w;
  683. double dx, dy;
  684. double det;
  685. double min, cand; /* minimum and candidate for minimum of quad. form */
  686. double xmin, ymin; /* coordinates of minimum */
  687. int z;
  688. /* let s be the vertex, in coordinates relative to x0/y0 */
  689. s.x = pt[po[i]].x - x0;
  690. s.y = pt[po[i]].y - y0;
  691. /* intersect segments i-1 and i */
  692. j = mod( i - 1, m );
  693. /* add quadratic forms */
  694. for( l = 0; l<3; l++ )
  695. {
  696. for( k = 0; k<3; k++ )
  697. {
  698. Q[l][k] = q[j][l][k] + q[i][l][k];
  699. }
  700. }
  701. while( 1 )
  702. {
  703. /* minimize the quadratic form Q on the unit square */
  704. /* find intersection */
  705. #ifdef HAVE_GCC_LOOP_BUG
  706. /* work around gcc bug #12243 */
  707. free( NULL );
  708. #endif
  709. det = Q[0][0] * Q[1][1] - Q[0][1] * Q[1][0];
  710. if( det != 0.0 )
  711. {
  712. w.x = (-Q[0][2] * Q[1][1] + Q[1][2] * Q[0][1]) / det;
  713. w.y = ( Q[0][2] * Q[1][0] - Q[1][2] * Q[0][0]) / det;
  714. break;
  715. }
  716. /* matrix is singular - lines are parallel. Add another,
  717. * orthogonal axis, through the center of the unit square */
  718. if( Q[0][0]>Q[1][1] )
  719. {
  720. v[0] = -Q[0][1];
  721. v[1] = Q[0][0];
  722. }
  723. else if( Q[1][1] )
  724. {
  725. v[0] = -Q[1][1];
  726. v[1] = Q[1][0];
  727. }
  728. else
  729. {
  730. v[0] = 1;
  731. v[1] = 0;
  732. }
  733. d = sq( v[0] ) + sq( v[1] );
  734. v[2] = -v[1] * s.y - v[0] * s.x;
  735. for( l = 0; l<3; l++ )
  736. {
  737. for( k = 0; k<3; k++ )
  738. {
  739. Q[l][k] += v[l] * v[k] / d;
  740. }
  741. }
  742. }
  743. dx = fabs( w.x - s.x );
  744. dy = fabs( w.y - s.y );
  745. if( dx <= .5 && dy <= .5 )
  746. {
  747. pp->curve.vertex[i].x = w.x + x0;
  748. pp->curve.vertex[i].y = w.y + y0;
  749. continue;
  750. }
  751. /* the minimum was not in the unit square; now minimize quadratic
  752. * on boundary of square */
  753. min = quadform( Q, s );
  754. xmin = s.x;
  755. ymin = s.y;
  756. if( Q[0][0] == 0.0 )
  757. {
  758. goto fixx;
  759. }
  760. for( z = 0; z<2; z++ ) /* value of the y-coordinate */
  761. {
  762. w.y = s.y - 0.5 + z;
  763. w.x = -(Q[0][1] * w.y + Q[0][2]) / Q[0][0];
  764. dx = fabs( w.x - s.x );
  765. cand = quadform( Q, w );
  766. if( dx <= .5 && cand < min )
  767. {
  768. min = cand;
  769. xmin = w.x;
  770. ymin = w.y;
  771. }
  772. }
  773. fixx:
  774. if( Q[1][1] == 0.0 )
  775. {
  776. goto corners;
  777. }
  778. for( z = 0; z<2; z++ ) /* value of the x-coordinate */
  779. {
  780. w.x = s.x - 0.5 + z;
  781. w.y = -(Q[1][0] * w.x + Q[1][2]) / Q[1][1];
  782. dy = fabs( w.y - s.y );
  783. cand = quadform( Q, w );
  784. if( dy <= .5 && cand < min )
  785. {
  786. min = cand;
  787. xmin = w.x;
  788. ymin = w.y;
  789. }
  790. }
  791. corners:
  792. /* check four corners */
  793. for( l = 0; l<2; l++ )
  794. {
  795. for( k = 0; k<2; k++ )
  796. {
  797. w.x = s.x - 0.5 + l;
  798. w.y = s.y - 0.5 + k;
  799. cand = quadform( Q, w );
  800. if( cand < min )
  801. {
  802. min = cand;
  803. xmin = w.x;
  804. ymin = w.y;
  805. }
  806. }
  807. }
  808. pp->curve.vertex[i].x = xmin + x0;
  809. pp->curve.vertex[i].y = ymin + y0;
  810. continue;
  811. }
  812. free( ctr );
  813. free( dir );
  814. free( q );
  815. return 0;
  816. calloc_error:
  817. free( ctr );
  818. free( dir );
  819. free( q );
  820. return 1;
  821. }
  822. /* ---------------------------------------------------------------------- */
  823. /* Stage 4: smoothing and corner analysis (Sec. 2.3.3) */
  824. /* reverse orientation of a path */
  825. static void reverse( privcurve_t* curve )
  826. {
  827. int m = curve->n;
  828. int i, j;
  829. dpoint_t tmp;
  830. for( i = 0, j = m - 1; i<j; i++, j-- )
  831. {
  832. tmp = curve->vertex[i];
  833. curve->vertex[i] = curve->vertex[j];
  834. curve->vertex[j] = tmp;
  835. }
  836. }
  837. /* Always succeeds */
  838. static void smooth( privcurve_t* curve, double alphamax )
  839. {
  840. int m = curve->n;
  841. int i, j, k;
  842. double dd, denom, alpha;
  843. dpoint_t p2, p3, p4;
  844. /* examine each vertex and find its best fit */
  845. for( i = 0; i<m; i++ )
  846. {
  847. j = mod( i + 1, m );
  848. k = mod( i + 2, m );
  849. p4 = interval( 1 / 2.0, curve->vertex[k], curve->vertex[j] );
  850. denom = ddenom( curve->vertex[i], curve->vertex[k] );
  851. if( denom != 0.0 )
  852. {
  853. dd = dpara( curve->vertex[i], curve->vertex[j], curve->vertex[k] ) / denom;
  854. dd = fabs( dd );
  855. alpha = dd>1 ? (1 - 1.0 / dd) : 0;
  856. alpha = alpha / 0.75;
  857. }
  858. else
  859. {
  860. alpha = 4 / 3.0;
  861. }
  862. curve->alpha0[j] = alpha; /* remember "original" value of alpha */
  863. if( alpha >= alphamax ) /* pointed corner */
  864. {
  865. curve->tag[j] = POTRACE_CORNER;
  866. curve->c[j][1] = curve->vertex[j];
  867. curve->c[j][2] = p4;
  868. }
  869. else
  870. {
  871. if( alpha < 0.55 )
  872. {
  873. alpha = 0.55;
  874. }
  875. else if( alpha > 1 )
  876. {
  877. alpha = 1;
  878. }
  879. p2 = interval( .5 + .5 * alpha, curve->vertex[i], curve->vertex[j] );
  880. p3 = interval( .5 + .5 * alpha, curve->vertex[k], curve->vertex[j] );
  881. curve->tag[j] = POTRACE_CURVETO;
  882. curve->c[j][0] = p2;
  883. curve->c[j][1] = p3;
  884. curve->c[j][2] = p4;
  885. }
  886. curve->alpha[j] = alpha; /* store the "cropped" value of alpha */
  887. curve->beta[j] = 0.5;
  888. }
  889. curve->alphacurve = 1;
  890. }
  891. /* ---------------------------------------------------------------------- */
  892. /* Stage 5: Curve optimization (Sec. 2.4) */
  893. /* a private type for the result of opti_penalty */
  894. struct opti_s
  895. {
  896. double pen; /* penalty */
  897. dpoint_t c[2]; /* curve parameters */
  898. double t, s; /* curve parameters */
  899. double alpha; /* curve parameter */
  900. };
  901. typedef struct opti_s opti_t;
  902. /* calculate best fit from i+.5 to j+.5. Assume i<j (cyclically).
  903. * Return 0 and set badness and parameters (alpha, beta), if
  904. * possible. Return 1 if impossible. */
  905. static int opti_penalty( privpath_t* pp,
  906. int i,
  907. int j,
  908. opti_t* res,
  909. double opttolerance,
  910. int* convc,
  911. double* areac )
  912. {
  913. int m = pp->curve.n;
  914. int k, k1, k2, conv, i1;
  915. double area, alpha, d, d1, d2;
  916. dpoint_t p0, p1, p2, p3, pt;
  917. double A, R, A1, A2, A3, A4;
  918. double s, t;
  919. /* check convexity, corner-freeness, and maximum bend < 179 degrees */
  920. if( i==j ) /* sanity - a full loop can never be an opticurve */
  921. {
  922. return 1;
  923. }
  924. k = i;
  925. i1 = mod( i + 1, m );
  926. k1 = mod( k + 1, m );
  927. conv = convc[k1];
  928. if( conv == 0 )
  929. {
  930. return 1;
  931. }
  932. d = ddist( pp->curve.vertex[i], pp->curve.vertex[i1] );
  933. for( k = k1; k!=j; k = k1 )
  934. {
  935. k1 = mod( k + 1, m );
  936. k2 = mod( k + 2, m );
  937. if( convc[k1] != conv )
  938. {
  939. return 1;
  940. }
  941. if( sign( cprod( pp->curve.vertex[i], pp->curve.vertex[i1], pp->curve.vertex[k1],
  942. pp->curve.vertex[k2] ) ) != conv )
  943. {
  944. return 1;
  945. }
  946. if( iprod1( pp->curve.vertex[i], pp->curve.vertex[i1], pp->curve.vertex[k1],
  947. pp->curve.vertex[k2] ) <
  948. d * ddist( pp->curve.vertex[k1], pp->curve.vertex[k2] ) * COS179 )
  949. {
  950. return 1;
  951. }
  952. }
  953. /* the curve we're working in: */
  954. p0 = pp->curve.c[mod( i, m )][2];
  955. p1 = pp->curve.vertex[mod( i + 1, m )];
  956. p2 = pp->curve.vertex[mod( j, m )];
  957. p3 = pp->curve.c[mod( j, m )][2];
  958. /* determine its area */
  959. area = areac[j] - areac[i];
  960. area -= dpara( pp->curve.vertex[0], pp->curve.c[i][2], pp->curve.c[j][2] ) / 2;
  961. if( i>=j )
  962. {
  963. area += areac[m];
  964. }
  965. /* find intersection o of p0p1 and p2p3. Let t,s such that o =
  966. * interval(t,p0,p1) = interval(s,p3,p2). Let A be the area of the
  967. * triangle (p0,o,p3). */
  968. A1 = dpara( p0, p1, p2 );
  969. A2 = dpara( p0, p1, p3 );
  970. A3 = dpara( p0, p2, p3 );
  971. /* A4 = dpara(p1, p2, p3); */
  972. A4 = A1 + A3 - A2;
  973. if( A2 == A1 ) /* this should never happen */
  974. {
  975. return 1;
  976. }
  977. t = A3 / (A3 - A4);
  978. s = A2 / (A2 - A1);
  979. A = A2 * t / 2.0;
  980. if( A == 0.0 ) /* this should never happen */
  981. {
  982. return 1;
  983. }
  984. R = area / A; /* relative area */
  985. alpha = 2 - sqrt( 4 - R / 0.3 ); /* overall alpha for p0-o-p3 curve */
  986. res->c[0] = interval( t * alpha, p0, p1 );
  987. res->c[1] = interval( s * alpha, p3, p2 );
  988. res->alpha = alpha;
  989. res->t = t;
  990. res->s = s;
  991. p1 = res->c[0];
  992. p2 = res->c[1]; /* the proposed curve is now (p0,p1,p2,p3) */
  993. res->pen = 0;
  994. /* calculate penalty */
  995. /* check tangency with edges */
  996. for( k = mod( i + 1, m ); k!=j; k = k1 )
  997. {
  998. k1 = mod( k + 1, m );
  999. t = tangent( p0, p1, p2, p3, pp->curve.vertex[k], pp->curve.vertex[k1] );
  1000. if( t<-.5 )
  1001. {
  1002. return 1;
  1003. }
  1004. pt = bezier( t, p0, p1, p2, p3 );
  1005. d = ddist( pp->curve.vertex[k], pp->curve.vertex[k1] );
  1006. if( d == 0.0 ) /* this should never happen */
  1007. {
  1008. return 1;
  1009. }
  1010. d1 = dpara( pp->curve.vertex[k], pp->curve.vertex[k1], pt ) / d;
  1011. if( fabs( d1 ) > opttolerance )
  1012. {
  1013. return 1;
  1014. }
  1015. if( iprod( pp->curve.vertex[k], pp->curve.vertex[k1],
  1016. pt ) < 0 || iprod( pp->curve.vertex[k1], pp->curve.vertex[k], pt ) < 0 )
  1017. {
  1018. return 1;
  1019. }
  1020. res->pen += sq( d1 );
  1021. }
  1022. /* check corners */
  1023. for( k = i; k!=j; k = k1 )
  1024. {
  1025. k1 = mod( k + 1, m );
  1026. t = tangent( p0, p1, p2, p3, pp->curve.c[k][2], pp->curve.c[k1][2] );
  1027. if( t<-.5 )
  1028. {
  1029. return 1;
  1030. }
  1031. pt = bezier( t, p0, p1, p2, p3 );
  1032. d = ddist( pp->curve.c[k][2], pp->curve.c[k1][2] );
  1033. if( d == 0.0 ) /* this should never happen */
  1034. {
  1035. return 1;
  1036. }
  1037. d1 = dpara( pp->curve.c[k][2], pp->curve.c[k1][2], pt ) / d;
  1038. d2 = dpara( pp->curve.c[k][2], pp->curve.c[k1][2], pp->curve.vertex[k1] ) / d;
  1039. d2 *= 0.75 * pp->curve.alpha[k1];
  1040. if( d2 < 0 )
  1041. {
  1042. d1 = -d1;
  1043. d2 = -d2;
  1044. }
  1045. if( d1 < d2 - opttolerance )
  1046. {
  1047. return 1;
  1048. }
  1049. if( d1 < d2 )
  1050. {
  1051. res->pen += sq( d1 - d2 );
  1052. }
  1053. }
  1054. return 0;
  1055. }
  1056. /* optimize the path p, replacing sequences of Bezier segments by a
  1057. * single segment when possible. Return 0 on success, 1 with errno set
  1058. * on failure. */
  1059. static int opticurve( privpath_t* pp, double opttolerance )
  1060. {
  1061. int m = pp->curve.n;
  1062. int* pt = NULL; /* pt[m+1] */
  1063. double* pen = NULL; /* pen[m+1] */
  1064. int* len = NULL; /* len[m+1] */
  1065. opti_t* opt = NULL; /* opt[m+1] */
  1066. int om;
  1067. int i, j, r;
  1068. opti_t o;
  1069. dpoint_t p0;
  1070. int i1;
  1071. double area;
  1072. double alpha;
  1073. double* s = NULL;
  1074. double* t = NULL;
  1075. int* convc = NULL; /* conv[m]: pre-computed convexities */
  1076. double* areac = NULL; /* cumarea[m+1]: cache for fast area computation */
  1077. SAFE_CALLOC( pt, m + 1, int );
  1078. SAFE_CALLOC( pen, m + 1, double );
  1079. SAFE_CALLOC( len, m + 1, int );
  1080. SAFE_CALLOC( opt, m + 1, opti_t );
  1081. SAFE_CALLOC( convc, m, int );
  1082. SAFE_CALLOC( areac, m + 1, double );
  1083. /* pre-calculate convexity: +1 = right turn, -1 = left turn, 0 = corner */
  1084. for( i = 0; i<m; i++ )
  1085. {
  1086. if( pp->curve.tag[i] == POTRACE_CURVETO )
  1087. {
  1088. convc[i] =
  1089. sign( dpara( pp->curve.vertex[mod( i - 1, m )], pp->curve.vertex[i],
  1090. pp->curve.vertex[mod( i + 1, m )] ) );
  1091. }
  1092. else
  1093. {
  1094. convc[i] = 0;
  1095. }
  1096. }
  1097. /* pre-calculate areas */
  1098. area = 0.0;
  1099. areac[0] = 0.0;
  1100. p0 = pp->curve.vertex[0];
  1101. for( i = 0; i<m; i++ )
  1102. {
  1103. i1 = mod( i + 1, m );
  1104. if( pp->curve.tag[i1] == POTRACE_CURVETO )
  1105. {
  1106. alpha = pp->curve.alpha[i1];
  1107. area += 0.3 * alpha * (4 - alpha) * dpara( pp->curve.c[i][2],
  1108. pp->curve.vertex[i1],
  1109. pp->curve.c[i1][2] ) / 2;
  1110. area += dpara( p0, pp->curve.c[i][2], pp->curve.c[i1][2] ) / 2;
  1111. }
  1112. areac[i + 1] = area;
  1113. }
  1114. pt[0] = -1;
  1115. pen[0] = 0;
  1116. len[0] = 0;
  1117. /* Fixme: we always start from a fixed point -- should find the best
  1118. * curve cyclically */
  1119. for( j = 1; j<=m; j++ )
  1120. {
  1121. /* calculate best path from 0 to j */
  1122. pt[j] = j - 1;
  1123. pen[j] = pen[j - 1];
  1124. len[j] = len[j - 1] + 1;
  1125. for( i = j - 2; i>=0; i-- )
  1126. {
  1127. r = opti_penalty( pp, i, mod( j, m ), &o, opttolerance, convc, areac );
  1128. if( r )
  1129. {
  1130. break;
  1131. }
  1132. if( len[j] > len[i] + 1 || (len[j] == len[i] + 1 && pen[j] > pen[i] + o.pen) )
  1133. {
  1134. pt[j] = i;
  1135. pen[j] = pen[i] + o.pen;
  1136. len[j] = len[i] + 1;
  1137. opt[j] = o;
  1138. }
  1139. }
  1140. }
  1141. om = len[m];
  1142. r = privcurve_init( &pp->ocurve, om );
  1143. if( r )
  1144. {
  1145. goto calloc_error;
  1146. }
  1147. SAFE_CALLOC( s, om, double );
  1148. SAFE_CALLOC( t, om, double );
  1149. j = m;
  1150. for( i = om - 1; i>=0; i-- )
  1151. {
  1152. if( pt[j]==j - 1 )
  1153. {
  1154. pp->ocurve.tag[i] = pp->curve.tag[mod( j, m )];
  1155. pp->ocurve.c[i][0] = pp->curve.c[mod( j, m )][0];
  1156. pp->ocurve.c[i][1] = pp->curve.c[mod( j, m )][1];
  1157. pp->ocurve.c[i][2] = pp->curve.c[mod( j, m )][2];
  1158. pp->ocurve.vertex[i] = pp->curve.vertex[mod( j, m )];
  1159. pp->ocurve.alpha[i] = pp->curve.alpha[mod( j, m )];
  1160. pp->ocurve.alpha0[i] = pp->curve.alpha0[mod( j, m )];
  1161. pp->ocurve.beta[i] = pp->curve.beta[mod( j, m )];
  1162. s[i] = t[i] = 1.0;
  1163. }
  1164. else
  1165. {
  1166. pp->ocurve.tag[i] = POTRACE_CURVETO;
  1167. pp->ocurve.c[i][0] = opt[j].c[0];
  1168. pp->ocurve.c[i][1] = opt[j].c[1];
  1169. pp->ocurve.c[i][2] = pp->curve.c[mod( j, m )][2];
  1170. pp->ocurve.vertex[i] = interval( opt[j].s, pp->curve.c[mod( j,
  1171. m )][2],
  1172. pp->curve.vertex[mod( j, m )] );
  1173. pp->ocurve.alpha[i] = opt[j].alpha;
  1174. pp->ocurve.alpha0[i] = opt[j].alpha;
  1175. s[i] = opt[j].s;
  1176. t[i] = opt[j].t;
  1177. }
  1178. j = pt[j];
  1179. }
  1180. /* calculate beta parameters */
  1181. for( i = 0; i<om; i++ )
  1182. {
  1183. i1 = mod( i + 1, om );
  1184. pp->ocurve.beta[i] = s[i] / (s[i] + t[i1]);
  1185. }
  1186. pp->ocurve.alphacurve = 1;
  1187. free( pt );
  1188. free( pen );
  1189. free( len );
  1190. free( opt );
  1191. free( s );
  1192. free( t );
  1193. free( convc );
  1194. free( areac );
  1195. return 0;
  1196. calloc_error:
  1197. free( pt );
  1198. free( pen );
  1199. free( len );
  1200. free( opt );
  1201. free( s );
  1202. free( t );
  1203. free( convc );
  1204. free( areac );
  1205. return 1;
  1206. }
  1207. /* ---------------------------------------------------------------------- */
  1208. #define TRY( x ) if( x ) \
  1209. goto try_error
  1210. /* return 0 on success, 1 on error with errno set. */
  1211. int process_path( path_t* plist, const potrace_param_t* param, progress_t* progress )
  1212. {
  1213. path_t* p;
  1214. double nn = 0, cn = 0;
  1215. if( progress->callback )
  1216. {
  1217. /* precompute task size for progress estimates */
  1218. nn = 0;
  1219. list_forall( p, plist ) {
  1220. nn += p->priv->len;
  1221. }
  1222. cn = 0;
  1223. }
  1224. /* call downstream function with each path */
  1225. list_forall( p, plist ) {
  1226. TRY( calc_sums( p->priv ) );
  1227. TRY( calc_lon( p->priv ) );
  1228. TRY( bestpolygon( p->priv ) );
  1229. TRY( adjust_vertices( p->priv ) );
  1230. if( p->sign == '-' ) /* reverse orientation of negative paths */
  1231. {
  1232. reverse( &p->priv->curve );
  1233. }
  1234. smooth( &p->priv->curve, param->alphamax );
  1235. if( param->opticurve )
  1236. {
  1237. TRY( opticurve( p->priv, param->opttolerance ) );
  1238. p->priv->fcurve = &p->priv->ocurve;
  1239. }
  1240. else
  1241. {
  1242. p->priv->fcurve = &p->priv->curve;
  1243. }
  1244. privcurve_to_curve( p->priv->fcurve, &p->curve );
  1245. if( progress->callback )
  1246. {
  1247. cn += p->priv->len;
  1248. progress_update( cn / nn, progress );
  1249. }
  1250. }
  1251. progress_update( 1.0, progress );
  1252. return 0;
  1253. try_error:
  1254. return 1;
  1255. }