]> git.donarmstrong.com Git - xournal.git/blob - src/xo-shapes.c
Add internationalization support.
[xournal.git] / src / xo-shapes.c
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <math.h>
6 #include <string.h>
7 #include <gtk/gtk.h>
8 #include <libgnomecanvas/libgnomecanvas.h>
9
10 #include "xournal.h"
11 #include "xo-shapes.h"
12 #include "xo-paint.h"
13
14 typedef struct Inertia {
15   double mass, sx, sy, sxx, sxy, syy;
16 } Inertia;
17
18 typedef struct RecoSegment {
19   struct Item *item;
20   int startpt, endpt;
21   double xcenter, ycenter, angle, radius;
22   double x1, y1, x2, y2;
23   gboolean reversed;
24 } RecoSegment;
25
26 struct RecoSegment recognizer_queue[MAX_POLYGON_SIDES+1];
27 int recognizer_queue_length;
28 struct UndoItem *last_item_checker; // check if queue is stale
29
30 void reset_recognizer(void)
31 {
32   recognizer_queue_length = 0;
33   last_item_checker = NULL;
34 }
35
36 /* compute mass and moments of a stroke */
37
38 void incr_inertia(double *pt, struct Inertia *s, int coef)
39 {
40   double dm;
41   dm = coef*hypot(pt[2]-pt[0], pt[3]-pt[1]);
42   s->mass += dm;
43   s->sx += dm*pt[0];
44   s->sy += dm*pt[1];
45   s->sxx += dm*pt[0]*pt[0];
46   s->syy += dm*pt[1]*pt[1];
47   s->sxy += dm*pt[0]*pt[1];
48 }
49    
50 void calc_inertia(double *pt, int start, int end, struct Inertia *s)
51 {
52   int i;
53   
54   s->mass = s->sx = s->sy = s->sxx = s->sxy = s->syy = 0.;
55   for (i=start, pt+=2*start; i<end; i++, pt+=2) incr_inertia(pt, s, 1);
56 }
57
58 /* compute normalized quantities */
59
60 inline double center_x(struct Inertia s) 
61
62   return s.sx/s.mass;
63 }
64
65 inline double center_y(struct Inertia s) 
66
67   return s.sy/s.mass;
68 }
69
70 inline double I_xx(struct Inertia s)
71 {
72   return (s.sxx - s.sx*s.sx/s.mass)/s.mass;
73 }
74
75 inline double I_xy(struct Inertia s)
76 {
77   return (s.sxy - s.sx*s.sy/s.mass)/s.mass;
78 }
79
80 inline double I_yy(struct Inertia s)
81 {
82   return (s.syy - s.sy*s.sy/s.mass)/s.mass;
83 }
84
85 inline double I_rad(struct Inertia s)
86 {
87   return sqrt(I_xx(s)+I_yy(s));
88 }
89
90 inline double I_det(struct Inertia s)
91 {
92   if (s.mass == 0.) return 0.;
93   double ixx = I_xx(s), iyy = I_yy(s), ixy = I_xy(s);
94   return 4*(ixx*iyy-ixy*ixy)/(ixx+iyy)/(ixx+iyy);
95 }
96
97 /* check if something is a polygonal line with at most nsides sides */
98
99 int find_polygonal(double *pt, int start, int end, int nsides, int *breaks, struct Inertia *ss)
100 {
101   struct Inertia s, s1, s2;
102   int k, i1, i2, n1, n2;
103   double det1, det2;
104   
105   if (end == start) return 0; // no way
106   if (nsides <= 0) return 0;
107   if (end-start<5) nsides = 1; // too small for a polygon
108   
109   // look for a linear piece that's big enough
110   for (k=0; k<nsides; k++) {
111     i1 = start + (k*(end-start))/nsides; 
112     i2 = start + ((k+1)*(end-start))/nsides;
113     calc_inertia(pt, i1, i2, &s);
114     if (I_det(s) < LINE_MAX_DET) break;
115   }
116   if (k==nsides) return 0; // failed!
117   
118   // grow the linear piece we found
119   while (1) {
120     if (i1>start) {
121       s1 = s;
122       incr_inertia(pt+2*(i1-1), &s1, 1);
123       det1 = I_det(s1);
124     } 
125     else det1 = 1.;
126     if (i2<end) {
127       s2 = s;
128       incr_inertia(pt+2*i2, &s2, 1);
129       det2 = I_det(s2);
130     }
131     else det2 = 1.;
132     if (det1<det2 && det1<LINE_MAX_DET) { i1--; s=s1; }
133     else if (det2<det1 && det2<LINE_MAX_DET) { i2++; s=s2; }
134     else break;
135   }
136   
137   if (i1>start) {
138     n1 = find_polygonal(pt, start, i1, (i2==end)?(nsides-1):(nsides-2), breaks, ss);
139     if (n1 == 0) return 0; // it doesn't work
140   }
141   else n1 = 0;
142
143   breaks[n1] = i1;
144   breaks[n1+1] = i2;
145   ss[n1] = s;
146
147   if (i2<end) {
148     n2 = find_polygonal(pt, i2, end, nsides-n1-1, breaks+n1+1, ss+n1+1);
149     if (n2 == 0) return 0;
150   }
151   else n2 = 0;
152   
153   return n1+n2+1;
154 }
155
156 /* improve on the polygon found by find_polygonal() */
157
158 void optimize_polygonal(double *pt, int nsides, int *breaks, struct Inertia *ss)
159 {
160   int i;
161   double cost, newcost;
162   struct Inertia s1, s2;
163   gboolean improved;
164   
165   for (i=1; i<nsides; i++) {
166     // optimize break between sides i and i+1
167     cost = I_det(ss[i-1])*I_det(ss[i-1])+I_det(ss[i])*I_det(ss[i]);
168     s1 = ss[i-1]; s2 = ss[i];
169     improved = FALSE;
170     while (breaks[i]>breaks[i-1]+1) {
171       // try moving the break to the left
172       incr_inertia(pt+2*(breaks[i]-1), &s1, -1);
173       incr_inertia(pt+2*(breaks[i]-1), &s2, 1);
174       newcost = I_det(s1)*I_det(s1)+I_det(s2)*I_det(s2);
175       if (newcost >= cost) break;
176       improved = TRUE;
177       cost = newcost; 
178       breaks[i]--;
179       ss[i-1] = s1;
180       ss[i] = s2;
181     }
182     if (improved) continue;
183     s1 = ss[i-1]; s2 = ss[i];
184     while (breaks[i]<breaks[i+1]-1) {
185       // try moving the break to the right
186       incr_inertia(pt+2*breaks[i], &s1, 1);
187       incr_inertia(pt+2*breaks[i], &s2, -1);
188       newcost = I_det(s1)*I_det(s1)+I_det(s2)*I_det(s2);
189       if (newcost >= cost) break;
190       cost = newcost; 
191       breaks[i]++;
192       ss[i-1] = s1;
193       ss[i] = s2;
194     }
195   }
196 }
197
198 /* find the geometry of a recognized segment */
199
200 void get_segment_geometry(double *pt, int start, int end, struct Inertia *s, struct RecoSegment *r)
201 {
202   double a, b, c, lmin, lmax, l;
203   int i;
204   
205   r->xcenter = center_x(*s);
206   r->ycenter = center_y(*s);
207   a = I_xx(*s); b = I_xy(*s); c = I_yy(*s);
208   /* max angle for inertia quadratic form solves: tan(2t) = 2b/(a-c) */
209   r->angle = atan2(2*b, a-c)/2; 
210   r->radius = sqrt(3*(a+c));
211   
212   lmin = lmax = 0.;
213   for (i=start, pt+=2*start; i<=end; i++, pt+=2) {
214     l = (pt[0]-r->xcenter)*cos(r->angle)+(pt[1]-r->ycenter)*sin(r->angle);
215     if (l<lmin) lmin = l;
216     if (l>lmax) lmax = l;
217   }
218   r->x1 = r->xcenter + lmin*cos(r->angle);
219   r->y1 = r->ycenter + lmin*sin(r->angle);
220   r->x2 = r->xcenter + lmax*cos(r->angle);
221   r->y2 = r->ycenter + lmax*sin(r->angle);
222 }
223
224 /* test if we have a circle; inertia has been precomputed by caller */
225
226 double score_circle(double *pt, int start, int end, struct Inertia *s)
227 {
228   double sum, x0, y0, r0, dm, deltar;
229   int i;
230   
231   if (s->mass == 0.) return 0;
232   sum = 0.;
233   x0 = center_x(*s); y0 = center_y(*s); r0 = I_rad(*s);
234   for (i=start, pt+=2*start; i<end; i++, pt+=2) {
235     dm = hypot(pt[2]-pt[0], pt[3]-pt[1]);
236     deltar = hypot(pt[0]-x0, pt[1]-y0) - r0;
237     sum += dm * fabs(deltar);
238   }
239   return sum/(s->mass*r0);
240 }
241
242 /* replace strokes by various shapes */
243
244 void make_circle_shape(double x0, double y0, double r)
245 {
246   int npts, i;
247   struct Item *item;
248   struct UndoErasureData *erasure;
249   
250   npts = (int)(2*r);
251   if (npts<12) npts = 12; // min. number of points
252   realloc_cur_path(npts+1);
253   ui.cur_path.num_points = npts+1;
254   for (i=0;i<=npts; i++) {
255     ui.cur_path.coords[2*i] = x0 + r*cos((2*M_PI*i)/npts);
256     ui.cur_path.coords[2*i+1] = y0 + r*sin((2*M_PI*i)/npts);
257   }
258 }
259
260 void calc_edge_isect(struct RecoSegment *r1, struct RecoSegment *r2, double *pt)
261 {
262   double t;
263   t = (r2->xcenter - r1->xcenter) * sin(r2->angle) - 
264       (r2->ycenter - r1->ycenter) * cos(r2->angle);
265   t /= sin(r2->angle-r1->angle);
266   pt[0] = r1->xcenter + t*cos(r1->angle);
267   pt[1] = r1->ycenter + t*sin(r1->angle);
268 }
269
270 void remove_recognized_strokes(struct RecoSegment *rs, int num_old_items)
271 {
272   struct Item *old_item;
273   int i, shift;
274   struct UndoErasureData *erasure;
275
276   old_item = NULL;
277   prepare_new_undo();
278   undo->type = ITEM_RECOGNIZER;
279   undo->layer = ui.cur_layer;
280   undo->erasurelist = NULL;
281   shift = 0;
282   
283   for (i=0; i<num_old_items; i++) {
284     if (rs[i].item == old_item) continue; // already done
285     old_item = rs[i].item;
286     erasure = g_new(struct UndoErasureData, 1);
287     erasure->item = old_item;
288     erasure->npos = g_list_index(ui.cur_layer->items, old_item) + (shift++);
289     erasure->nrepl = 0;
290     erasure->replacement_items = NULL;
291     undo->erasurelist = g_list_append(undo->erasurelist, erasure);
292     if (old_item->canvas_item != NULL)
293       gtk_object_destroy(GTK_OBJECT(old_item->canvas_item));
294     ui.cur_layer->items = g_list_remove(ui.cur_layer->items, old_item);
295     ui.cur_layer->nitems--;
296   }
297 }
298
299 struct Item *insert_recognized_curpath(void)
300 {
301   struct Item *item;
302   int i;
303   struct UndoErasureData *erasure;
304
305   erasure = (struct UndoErasureData *)(undo->erasurelist->data);
306   item = g_new(struct Item, 1);
307   item->type = ITEM_STROKE;
308   g_memmove(&(item->brush), &(erasure->item->brush), sizeof(struct Brush));
309   item->brush.variable_width = FALSE;
310   subdivide_cur_path();
311   item->path = gnome_canvas_points_new(ui.cur_path.num_points);
312   g_memmove(item->path->coords, ui.cur_path.coords, 2*ui.cur_path.num_points*sizeof(double));
313   item->widths = NULL;
314   update_item_bbox(item);
315   ui.cur_path.num_points = 0;
316   
317   erasure->nrepl++;
318   erasure->replacement_items = g_list_append(erasure->replacement_items, item);
319   ui.cur_layer->items = g_list_append(ui.cur_layer->items, item);
320   ui.cur_layer->nitems++;
321   make_canvas_item_one(ui.cur_layer->group, item);
322   return item;
323 }
324
325
326 /* test if segments form standard shapes */
327
328 gboolean try_rectangle(void)
329 {
330   struct RecoSegment *rs, *r1, *r2;
331   int i;
332   double dist, avg_angle;
333   
334   // first, we need whole strokes to combine to 4 segments...
335   if (recognizer_queue_length<4) return FALSE;
336   rs = recognizer_queue + recognizer_queue_length - 4;
337   if (rs->startpt!=0) return FALSE;
338
339   // check edges make angles ~= Pi/2 and vertices roughly match
340   avg_angle = 0.;
341   for (i=0; i<=3; i++) {
342     r1 = rs+i; r2 = rs+(i+1)%4;
343     if (fabs(fabs(r1->angle-r2->angle)-M_PI/2) > RECTANGLE_ANGLE_TOLERANCE)
344       return FALSE;
345     avg_angle += r1->angle;
346     if (r2->angle > r1->angle) avg_angle += (i+1)*M_PI/2;
347     else avg_angle -= (i+1)*M_PI/2;
348     // test if r1 points away from r2 rather than towards it
349     r1->reversed = ((r1->x2-r1->x1)*(r2->xcenter-r1->xcenter)+
350                    (r1->y2-r1->y1)*(r2->ycenter-r1->ycenter)) < 0;
351   }
352   for (i=0; i<=3; i++) {
353     r1 = rs+i; r2 = rs+(i+1)%4;
354     dist = hypot((r1->reversed?r1->x1:r1->x2) - (r2->reversed?r2->x2:r2->x1),
355                  (r1->reversed?r1->y1:r1->y2) - (r2->reversed?r2->y2:r2->y1));
356     if (dist > RECTANGLE_LINEAR_TOLERANCE*(r1->radius+r2->radius)) return FALSE;
357   }
358   
359   // make a rectangle of the correct size and slope
360   avg_angle = avg_angle/4;
361   if (fabs(avg_angle)<SLANT_TOLERANCE) avg_angle = 0.;
362   if (fabs(avg_angle)>M_PI/2-SLANT_TOLERANCE) avg_angle = M_PI/2;
363   realloc_cur_path(5);
364   ui.cur_path.num_points = 5;
365   for (i=0; i<=3; i++) rs[i].angle = avg_angle+i*M_PI/2;
366   for (i=0; i<=3; i++) calc_edge_isect(rs+i, rs+(i+1)%4, ui.cur_path.coords+2*i+2);
367   ui.cur_path.coords[0] = ui.cur_path.coords[8];
368   ui.cur_path.coords[1] = ui.cur_path.coords[9];
369   
370   remove_recognized_strokes(rs, 4);
371   insert_recognized_curpath();
372   return TRUE;
373 }
374
375 gboolean try_arrow(void)
376 {
377   struct RecoSegment *rs;
378   int i, j;
379   double alpha[3], dist, pt[2], tmp, delta;
380   double x1, y1, x2, y2, angle;
381   gboolean rev[3];
382   
383   // first, we need whole strokes to combine to nsides segments...
384   if (recognizer_queue_length<3) return FALSE;
385   rs = recognizer_queue + recognizer_queue_length - 3;
386   if (rs->startpt!=0) return FALSE;
387
388   // check arrow head not too big, and orient main segment
389   for (i=1; i<=2; i++) {
390     if (rs[i].radius > ARROW_MAXSIZE*rs[0].radius) return FALSE;
391     rev[i] = (hypot(rs[i].xcenter-rs->x1, rs[i].ycenter-rs->y1) <
392               hypot(rs[i].xcenter-rs->x2, rs[i].ycenter-rs->y2));
393   }
394   if (rev[1]!=rev[2]) return FALSE;
395   if (rev[1]) { 
396     x1 = rs->x2; y1 = rs->y2; x2 = rs->x1; y2 = rs->y1; 
397     angle = rs->angle + M_PI;
398   }
399   else { 
400     x1 = rs->x1; y1 = rs->y1; x2 = rs->x2; y2 = rs->y2;
401     angle = rs->angle;
402   }
403     
404   // check arrow head not too big, and angles roughly ok
405   for (i=1; i<=2; i++) {
406     rs[i].reversed = FALSE;
407     alpha[i] = rs[i].angle - angle;
408     while (alpha[i]<-M_PI/2) { alpha[i]+=M_PI; rs[i].reversed = !rs[i].reversed; }
409     while (alpha[i]>M_PI/2) { alpha[i]-=M_PI; rs[i].reversed = !rs[i].reversed; }
410 #ifdef RECOGNIZER_DEBUG
411     printf("DEBUG: arrow: alpha[%d] = %.1f degrees\n", i, alpha[i]*180/M_PI);
412 #endif
413     if (fabs(alpha[i])<ARROW_ANGLE_MIN || fabs(alpha[i])>ARROW_ANGLE_MAX) return FALSE;
414   }
415   
416   // check arrow head segments are roughly symmetric
417   if (alpha[1]*alpha[2]>0 || fabs(alpha[1]+alpha[2]) > ARROW_ASYMMETRY_MAX_ANGLE) return FALSE;
418   if (rs[1].radius/rs[2].radius > 1+ARROW_ASYMMETRY_MAX_LINEAR) return FALSE;
419   if (rs[2].radius/rs[1].radius > 1+ARROW_ASYMMETRY_MAX_LINEAR) return FALSE;
420
421   // check vertices roughly match
422   calc_edge_isect(rs+1, rs+2, pt);
423   for (j=1; j<=2; j++) {
424     dist = hypot(pt[0]-(rs[j].reversed?rs[j].x1:rs[j].x2),
425                  pt[1]-(rs[j].reversed?rs[j].y1:rs[j].y2));
426 #ifdef RECOGNIZER_DEBUG
427     printf("DEBUG: linear tolerance: tip[%d] = %.2f\n", j, dist/rs[j].radius);
428 #endif
429     if (dist>ARROW_TIP_LINEAR_TOLERANCE*rs[j].radius) return FALSE;
430   }
431   dist = (pt[0]-x2)*sin(angle)-(pt[1]-y2)*cos(angle);
432   dist /= rs[1].radius + rs[2].radius;
433 #ifdef RECOGNIZER_DEBUG
434   printf("DEBUG: sideways gap tolerance = %.2f\n", dist);
435 #endif
436   if (fabs(dist)>ARROW_SIDEWAYS_GAP_TOLERANCE) return FALSE;
437   dist = (pt[0]-x2)*cos(angle)+(pt[1]-y2)*sin(angle);
438   dist /= rs[1].radius + rs[2].radius;
439 #ifdef RECOGNIZER_DEBUG
440   printf("DEBUG: main linear gap = %.2f\n", dist);
441 #endif
442   if (dist<ARROW_MAIN_LINEAR_GAP_MIN || dist>ARROW_MAIN_LINEAR_GAP_MAX) return FALSE;
443
444   // make an arrow of the correct size and slope
445   if (fabs(rs->angle)<SLANT_TOLERANCE) { // nearly horizontal
446     angle = angle - rs->angle;
447     y1 = y2 = rs->ycenter;
448   }
449   if (rs->angle>M_PI/2-SLANT_TOLERANCE) { // nearly vertical
450     angle = angle - (rs->angle-M_PI/2);
451     x1 = x2 = rs->xcenter;
452   }
453   if (rs->angle<-M_PI/2+SLANT_TOLERANCE) { // nearly vertical
454     angle = angle - (rs->angle+M_PI/2);
455     x1 = x2 = rs->xcenter;
456   }
457   delta = fabs(alpha[1]-alpha[2])/2;
458   dist = (hypot(rs[1].x1-rs[1].x2, rs[1].y1-rs[1].y2) +
459           hypot(rs[2].x1-rs[2].x2, rs[2].y1-rs[2].y2))/2;
460   
461   realloc_cur_path(2);
462   ui.cur_path.num_points = 2;
463   ui.cur_path.coords[0] = x1; ui.cur_path.coords[1] = y1;
464   ui.cur_path.coords[2] = x2; ui.cur_path.coords[3] = y2;
465   remove_recognized_strokes(rs, 3);
466   insert_recognized_curpath();
467
468   realloc_cur_path(3);
469   ui.cur_path.num_points = 3;
470   ui.cur_path.coords[0] = x2 - dist*cos(angle+delta);
471   ui.cur_path.coords[1] = y2 - dist*sin(angle+delta);
472   ui.cur_path.coords[2] = x2; 
473   ui.cur_path.coords[3] = y2;
474   ui.cur_path.coords[4] = x2 - dist*cos(angle-delta);
475   ui.cur_path.coords[5] = y2 - dist*sin(angle-delta);
476   insert_recognized_curpath();
477
478   return TRUE;
479 }
480
481 gboolean try_closed_polygon(int nsides)
482 {
483   struct RecoSegment *rs, *r1, *r2;
484   int i;
485   double dist, pt[2];
486   
487   // first, we need whole strokes to combine to nsides segments...
488   if (recognizer_queue_length<nsides) return FALSE;
489   rs = recognizer_queue + recognizer_queue_length - nsides;
490   if (rs->startpt!=0) return FALSE;
491
492   // check vertices roughly match
493   for (i=0; i<nsides; i++) {
494     r1 = rs+i; r2 = rs+(i+1)%nsides;
495     // test if r1 points away from r2 rather than towards it
496     calc_edge_isect(r1, r2, pt);
497     r1->reversed = (hypot(pt[0]-r1->x1,pt[1]-r1->y1) < hypot(pt[0]-r1->x2,pt[1]-r1->y2));
498   }
499   for (i=0; i<nsides; i++) {
500     r1 = rs+i; r2 = rs+(i+1)%nsides;
501     calc_edge_isect(r1, r2, pt);
502     dist = hypot((r1->reversed?r1->x1:r1->x2)-pt[0],(r1->reversed?r1->y1:r1->y2)-pt[1])
503          + hypot((r2->reversed?r2->x2:r2->x1)-pt[0],(r2->reversed?r2->y2:r2->y1)-pt[1]);
504     if (dist > POLYGON_LINEAR_TOLERANCE*(r1->radius+r2->radius)) return FALSE;
505   }
506   
507   // make a polygon of the correct size and slope
508   realloc_cur_path(nsides+1);
509   ui.cur_path.num_points = nsides+1;
510   for (i=0; i<nsides; i++) 
511     calc_edge_isect(rs+i, rs+(i+1)%nsides, ui.cur_path.coords+2*i+2);
512   ui.cur_path.coords[0] = ui.cur_path.coords[2*nsides];
513   ui.cur_path.coords[1] = ui.cur_path.coords[2*nsides+1];
514   
515   remove_recognized_strokes(rs, nsides);
516   insert_recognized_curpath();
517   return TRUE;
518 }
519
520 /* the main pattern recognition function, called after finalize_stroke() */
521 void recognize_patterns(void)
522 {
523   struct Item *it;
524   struct Inertia s, ss[4];
525   struct RecoSegment *rs;
526   int n, i;
527   int brk[5];
528   double score;
529   
530   if (!undo || undo->type!=ITEM_STROKE) return;
531   if (undo->next != last_item_checker) reset_recognizer(); // reset queue
532   if (last_item_checker!=NULL && ui.cur_layer != last_item_checker->layer) reset_recognizer();
533
534   it = undo->item;
535   calc_inertia(it->path->coords, 0, it->path->num_points-1, &s);
536 #ifdef RECOGNIZER_DEBUG
537   printf("DEBUG: Mass=%.0f, Center=(%.1f,%.1f), I=(%.0f,%.0f, %.0f), "
538      "Rad=%.2f, Det=%.4f \n", 
539      s.mass, center_x(s), center_y(s), I_xx(s), I_yy(s), I_xy(s), I_rad(s), I_det(s));
540 #endif
541
542   // first see if it's a polygon
543   n = find_polygonal(it->path->coords, 0, it->path->num_points-1, MAX_POLYGON_SIDES, brk, ss);
544   if (n>0) {
545     optimize_polygonal(it->path->coords, n, brk, ss);
546 #ifdef RECOGNIZER_DEBUG
547     printf("DEBUG: Polygon, %d edges: ", n);
548     for (i=0; i<n; i++)
549       printf("DEBUG: %d-%d (M=%.0f, det=%.4f) ", brk[i], brk[i+1], ss[i].mass, I_det(ss[i]));
550     printf("\n");
551 #endif
552     /* update recognizer segment queue (most recent at end) */
553     while (n+recognizer_queue_length > MAX_POLYGON_SIDES) {
554       // remove oldest polygonal stroke
555       i=1;
556       while (i<recognizer_queue_length && recognizer_queue[i].startpt!=0) i++;
557       recognizer_queue_length-=i;
558       g_memmove(recognizer_queue, recognizer_queue+i, 
559               recognizer_queue_length * sizeof(struct RecoSegment));
560     }
561 #ifdef RECOGNIZER_DEBUG
562     printf("DEBUG: Queue now has %d + %d edges\n", recognizer_queue_length, n);
563 #endif
564     rs = recognizer_queue + recognizer_queue_length;
565     recognizer_queue_length += n;
566     for (i=0; i<n; i++) {
567       rs[i].item = it;
568       rs[i].startpt = brk[i];
569       rs[i].endpt = brk[i+1];
570       get_segment_geometry(it->path->coords, brk[i], brk[i+1], ss+i, rs+i);
571     }  
572     if (try_rectangle()) { reset_recognizer(); return; }
573     if (try_arrow()) { reset_recognizer(); return; }
574     if (try_closed_polygon(3)) { reset_recognizer(); return; }
575     if (try_closed_polygon(4)) { reset_recognizer(); return; }
576     if (n==1) { // current stroke is a line
577       if (fabs(rs->angle)<SLANT_TOLERANCE) { // nearly horizontal
578         rs->angle = 0.;
579         rs->y1 = rs->y2 = rs->ycenter;
580       }
581       if (fabs(rs->angle)>M_PI/2-SLANT_TOLERANCE) { // nearly vertical
582         rs->angle = (rs->angle>0)?(M_PI/2):(-M_PI/2);
583         rs->x1 = rs->x2 = rs->xcenter;
584       }
585       realloc_cur_path(2);
586       ui.cur_path.num_points = 2;
587       ui.cur_path.coords[0] = rs->x1;
588       ui.cur_path.coords[1] = rs->y1;
589       ui.cur_path.coords[2] = rs->x2;
590       ui.cur_path.coords[3] = rs->y2;
591       remove_recognized_strokes(rs, 1);
592       rs->item = insert_recognized_curpath();
593     }
594     last_item_checker = undo;
595     return;
596   }
597
598   // not a polygon: maybe a circle ?
599   reset_recognizer();
600   if (I_det(s)>CIRCLE_MIN_DET) {
601     score = score_circle(it->path->coords, 0, it->path->num_points-1, &s);
602 #ifdef RECOGNIZER_DEBUG
603     printf("DEBUG: Circle score: %.2f\n", score);
604 #endif
605     if (score < CIRCLE_MAX_SCORE) {
606       make_circle_shape(center_x(s), center_y(s), I_rad(s));
607       recognizer_queue[0].item = it;
608       remove_recognized_strokes(recognizer_queue, 1);
609       insert_recognized_curpath();
610     }
611   }
612 }
613