8 #include <libgnomecanvas/libgnomecanvas.h>
11 #include "xo-shapes.h"
14 typedef struct Inertia {
15 double mass, sx, sy, sxx, sxy, syy;
18 typedef struct RecoSegment {
21 double xcenter, ycenter, angle, radius;
22 double x1, y1, x2, y2;
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
30 void reset_recognizer(void)
32 recognizer_queue_length = 0;
33 last_item_checker = NULL;
36 /* compute mass and moments of a stroke */
38 void incr_inertia(double *pt, struct Inertia *s, int coef)
41 dm = coef*hypot(pt[2]-pt[0], pt[3]-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];
50 void calc_inertia(double *pt, int start, int end, struct Inertia *s)
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);
58 /* compute normalized quantities */
60 inline double center_x(struct Inertia s)
65 inline double center_y(struct Inertia s)
70 inline double I_xx(struct Inertia s)
72 return (s.sxx - s.sx*s.sx/s.mass)/s.mass;
75 inline double I_xy(struct Inertia s)
77 return (s.sxy - s.sx*s.sy/s.mass)/s.mass;
80 inline double I_yy(struct Inertia s)
82 return (s.syy - s.sy*s.sy/s.mass)/s.mass;
85 inline double I_rad(struct Inertia s)
87 return sqrt(I_xx(s)+I_yy(s));
90 inline double I_det(struct Inertia s)
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);
97 /* check if something is a polygonal line with at most nsides sides */
99 int find_polygonal(double *pt, int start, int end, int nsides, int *breaks, struct Inertia *ss)
101 struct Inertia s, s1, s2;
102 int k, i1, i2, n1, n2;
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
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;
116 if (k==nsides) return 0; // failed!
118 // grow the linear piece we found
122 incr_inertia(pt+2*(i1-1), &s1, 1);
128 incr_inertia(pt+2*i2, &s2, 1);
132 if (det1<det2 && det1<LINE_MAX_DET) { i1--; s=s1; }
133 else if (det2<det1 && det2<LINE_MAX_DET) { i2++; s=s2; }
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
148 n2 = find_polygonal(pt, i2, end, nsides-n1-1, breaks+n1+1, ss+n1+1);
149 if (n2 == 0) return 0;
156 /* improve on the polygon found by find_polygonal() */
158 void optimize_polygonal(double *pt, int nsides, int *breaks, struct Inertia *ss)
161 double cost, newcost;
162 struct Inertia s1, s2;
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];
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;
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;
198 /* find the geometry of a recognized segment */
200 void get_segment_geometry(double *pt, int start, int end, struct Inertia *s, struct RecoSegment *r)
202 double a, b, c, lmin, lmax, l;
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));
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;
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);
224 /* test if we have a circle; inertia has been precomputed by caller */
226 double score_circle(double *pt, int start, int end, struct Inertia *s)
228 double sum, x0, y0, r0, dm, deltar;
231 if (s->mass == 0.) return 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);
239 return sum/(s->mass*r0);
242 /* replace strokes by various shapes */
244 void make_circle_shape(double x0, double y0, double r)
248 struct UndoErasureData *erasure;
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);
260 void calc_edge_isect(struct RecoSegment *r1, struct RecoSegment *r2, double *pt)
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);
270 void remove_recognized_strokes(struct RecoSegment *rs, int num_old_items)
272 struct Item *old_item;
274 struct UndoErasureData *erasure;
278 undo->type = ITEM_RECOGNIZER;
279 undo->layer = ui.cur_layer;
280 undo->erasurelist = NULL;
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++);
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--;
299 struct Item *insert_recognized_curpath(void)
303 struct UndoErasureData *erasure;
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));
314 update_item_bbox(item);
315 ui.cur_path.num_points = 0;
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);
326 /* test if segments form standard shapes */
328 gboolean try_rectangle(void)
330 struct RecoSegment *rs, *r1, *r2;
332 double dist, avg_angle;
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;
339 // check edges make angles ~= Pi/2 and vertices roughly match
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)
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;
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;
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;
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];
370 remove_recognized_strokes(rs, 4);
371 insert_recognized_curpath();
375 gboolean try_arrow(void)
377 struct RecoSegment *rs;
379 double alpha[3], dist, pt[2], tmp, delta;
380 double x1, y1, x2, y2, angle;
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;
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));
394 if (rev[1]!=rev[2]) return FALSE;
396 x1 = rs->x2; y1 = rs->y2; x2 = rs->x1; y2 = rs->y1;
397 angle = rs->angle + M_PI;
400 x1 = rs->x1; y1 = rs->y1; x2 = rs->x2; y2 = rs->y2;
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("arrow: alpha[%d] = %.1f degrees\n", i, alpha[i]*180/M_PI);
413 if (fabs(alpha[i])<ARROW_ANGLE_MIN || fabs(alpha[i])>ARROW_ANGLE_MAX) return FALSE;
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;
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("linear tolerance: tip[%d] = %.2f\n", j, dist/rs[j].radius);
429 if (dist>ARROW_TIP_LINEAR_TOLERANCE*rs[j].radius) return FALSE;
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("sideways gap tolerance = %.2f\n", dist);
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("main linear gap = %.2f\n", dist);
442 if (dist<ARROW_MAIN_LINEAR_GAP_MIN || dist>ARROW_MAIN_LINEAR_GAP_MAX) return FALSE;
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;
449 if (rs->angle>M_PI/2-SLANT_TOLERANCE) { // nearly vertical
450 angle = angle - (rs->angle-M_PI/2);
451 x1 = x2 = rs->xcenter;
453 if (rs->angle<-M_PI/2+SLANT_TOLERANCE) { // nearly vertical
454 angle = angle - (rs->angle+M_PI/2);
455 x1 = x2 = rs->xcenter;
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;
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();
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();
481 gboolean try_closed_polygon(int nsides)
483 struct RecoSegment *rs, *r1, *r2;
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;
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));
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;
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];
515 remove_recognized_strokes(rs, nsides);
516 insert_recognized_curpath();
520 /* the main pattern recognition function, called after finalize_stroke() */
521 void recognize_patterns(void)
524 struct Inertia s, ss[4];
525 struct RecoSegment *rs;
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();
535 calc_inertia(it->path->coords, 0, it->path->num_points-1, &s);
536 #ifdef RECOGNIZER_DEBUG
537 printf("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));
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);
545 optimize_polygonal(it->path->coords, n, brk, ss);
546 #ifdef RECOGNIZER_DEBUG
547 printf("Polygon, %d edges: ", n);
549 printf("%d-%d (M=%.0f, det=%.4f) ", brk[i], brk[i+1], ss[i].mass, I_det(ss[i]));
552 /* update recognizer segment queue (most recent at end) */
553 while (n+recognizer_queue_length > MAX_POLYGON_SIDES) {
554 // remove oldest polygonal stroke
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));
561 #ifdef RECOGNIZER_DEBUG
562 printf("Queue now has %d + %d edges\n", recognizer_queue_length, n);
564 rs = recognizer_queue + recognizer_queue_length;
565 recognizer_queue_length += n;
566 for (i=0; i<n; i++) {
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);
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
579 rs->y1 = rs->y2 = rs->ycenter;
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;
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();
594 last_item_checker = undo;
598 // not a polygon: maybe a circle ?
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("Circle score: %.2f\n", score);
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();