trace.c 32 KB

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