The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

StrokeTool.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/StrokeTool.vala.
Revert "Replace the list implementation"
1 /* 2 Copyright (C) 2014 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 public enum LineCap { 21 BUTT, 22 SQUARE, 23 ROUND 24 } 25 26 public class StrokeTool : GLib.Object { 27 28 public static double stroke_width = 0; 29 public static bool add_stroke = false; 30 31 public static bool show_stroke_tools = false; 32 public static bool convert_stroke = false; 33 34 public static LineCap line_cap = LineCap.BUTT; 35 36 StrokeTask task; 37 38 public StrokeTool () { 39 task = new StrokeTask.none (); 40 } 41 42 public StrokeTool.with_task (StrokeTask cancelable_task) { 43 task = cancelable_task; 44 } 45 46 /** Create strokes for the selected outlines. */ 47 public void stroke_selected_paths () { 48 Glyph g = MainWindow.get_current_glyph (); 49 PathList paths = new PathList (); 50 51 convert_stroke = true; 52 g.store_undo_state (); 53 54 foreach (Path p in g.active_paths) { 55 if (p.stroke > 0) { 56 paths.append (p.get_stroke ()); 57 } 58 } 59 60 if (paths.paths.size > 0) { 61 foreach (Path p in g.active_paths) { 62 g.layers.remove_path (p); 63 } 64 65 g.active_paths.clear (); 66 67 foreach (Path np in paths.paths) { 68 g.add_path (np); 69 g.active_paths.add (np); 70 } 71 72 PenTool.update_orientation (); 73 74 GlyphCanvas.redraw (); 75 } 76 77 PenTool.update_orientation (); 78 convert_stroke = false; 79 } 80 81 public PathList get_stroke_fast (Path path, double thickness) { 82 PathList o; 83 Path stroke; 84 StrokeTool s = new StrokeTool (); 85 86 stroke = path.copy (); 87 stroke.remove_points_on_points (0.1); 88 o = s.create_stroke (stroke, thickness); 89 90 return o; 91 } 92 93 public PathList get_stroke (Path path, double thickness) { 94 PathList o, m; 95 Path stroke; 96 97 if (task.is_cancelled ()) { 98 return new PathList (); 99 } 100 101 stroke = path.copy (); 102 stroke.remove_points_on_points (0.1); 103 o = create_stroke (stroke, thickness); 104 o = get_all_parts (o); 105 o = remove_intersection_paths (o); 106 o = merge (o); 107 108 m = new PathList (); 109 foreach (Path p in o.paths) { 110 m.add (simplify_stroke (p)); 111 } 112 113 return m; 114 } 115 116 void reset_flags (PathList o) { 117 foreach (Path p in o.paths) { 118 foreach (EditPoint ep in p.points) { 119 ep.flags &= ~(EditPoint.INTERSECTION 120 | EditPoint.COPIED 121 | EditPoint.NEW_CORNER 122 | EditPoint.SELF_INTERSECTION); 123 } 124 p.update_region_boundaries (); 125 } 126 } 127 128 public void merge_selected_paths () { 129 Glyph g = MainWindow.get_current_glyph (); 130 PathList o = new PathList (); 131 PathList r; 132 PathList new_paths = new PathList (); 133 bool error = false; 134 135 g.store_undo_state (); 136 137 foreach (Path p in g.active_paths) { 138 if (p.stroke == 0) { 139 o.add (p); 140 } else { 141 o.append (p.get_stroke ()); 142 } 143 } 144 145 foreach (Path p in o.paths) { 146 p.close (); 147 remove_single_point_intersections (p); 148 } 149 150 o = remove_overlap (o, out error); 151 152 if (error) { 153 warning ("merge_selected_paths failed."); 154 return; 155 } 156 157 reset_flags (o); 158 new_paths.append (o); 159 160 for (int merge = 0; merge < 2; merge++) { 161 for (int i = 0; i < o.paths.size; i++) { 162 for (int j = 0; j < o.paths.size; j++) { 163 Path p1, p2; 164 165 if (task.is_cancelled ()) { 166 return; 167 } 168 169 p1 = o.paths.get (i); 170 p2 = o.paths.get (j); 171 172 if (merge == 0) { 173 if (p1.is_clockwise () == p2.is_clockwise ()) { 174 continue; 175 } 176 } 177 178 if (merge == 1) { 179 if (p1.is_clockwise () != p2.is_clockwise ()) { 180 continue; 181 } 182 } 183 184 if (i == j) { 185 continue; 186 } 187 188 r = merge_selected (p1, p2, false, out error); 189 190 if (error) { 191 warning ("Can't merge selected paths."); 192 return; 193 } 194 195 remove_merged_curve_parts (r); 196 197 if (r.paths.size > 0) { 198 reset_flags (r); 199 new_paths.append (r); 200 201 new_paths.remove (p1); 202 new_paths.remove (p2); 203 204 o.remove (p1); 205 o.remove (p2); 206 207 o.append (r); 208 209 i = 0; 210 j = 0; 211 } 212 } 213 } 214 } 215 216 if (task.is_cancelled ()) { 217 return; 218 } 219 220 foreach (Path p in g.active_paths) { 221 g.delete_path (p); 222 } 223 224 g.clear_active_paths (); 225 226 remove_merged_curve_parts (new_paths); 227 228 foreach (Path p in new_paths.paths) { 229 g.add_path (p); 230 g.add_active_path (null, p); 231 } 232 233 PenTool.update_orientation (); 234 GlyphCanvas.redraw (); 235 } 236 237 void remove_single_point_intersections (Path p) { 238 PointSelection ps; 239 240 p.remove_points_on_points (); 241 242 for (int i = 0; i < p.points.size; i++) { 243 EditPoint ep = p.points.get (i); 244 EditPoint next = p.points.get ((i + 1) % p.points.size); 245 if (fabs (ep.get_right_handle ().angle - ep.get_left_handle ().angle) % (2 * PI) < 0.01) { 246 if (ep.get_right_handle ().length > 0 && ep.get_left_handle ().length > 0) { 247 ps = new PointSelection (ep, p); 248 PenTool.remove_point_simplify (ps); 249 } 250 } else if (Path.distance_to_point (ep, next) < 0.01) { 251 ps = new PointSelection (ep, p); 252 PenTool.remove_point_simplify (ps); 253 } 254 } 255 } 256 257 PathList remove_overlap (PathList pl, out bool error) { 258 PathList r = new PathList (); 259 260 error = false; 261 262 foreach (Path p in pl.paths) { 263 PathList m = merge_selected (p, new Path (), true, out error); 264 265 if (error) { 266 warning ("Can't remove overlap."); 267 return pl; 268 } 269 270 if (m.paths.size > 0) { 271 r.append (m); 272 } else { 273 r.add (p); 274 } 275 } 276 277 return r; 278 } 279 280 void remove_merged_curve_parts (PathList r) { 281 Gee.ArrayList<Path> remove = new Gee.ArrayList<Path> (); 282 PathList flat = new PathList (); 283 284 foreach (Path p in r.paths) { 285 p.update_region_boundaries (); 286 flat.add (p.flatten ()); 287 } 288 289 foreach (Path p in r.paths) { 290 PathList pl = get_insides (flat, p); 291 292 int counters = 0; 293 int clockwise = 0; 294 295 foreach (Path i in pl.paths) { 296 if (i.is_clockwise ()) { 297 clockwise++; 298 } else { 299 counters++; 300 } 301 } 302 303 if (p.is_clockwise ()) { 304 if (clockwise - 1 > counters) { 305 remove.add (p); 306 break; 307 } 308 } else { 309 if (clockwise < counters - 1) { 310 remove.add (p); 311 break; 312 } 313 } 314 } 315 316 foreach (Path p in remove) { 317 r.paths.remove (p); 318 remove_merged_curve_parts (r); 319 return; 320 } 321 } 322 323 public PathList merge_selected (Path path1, Path path2, 324 bool self_intersection, out bool error) { 325 326 PathList flat = new PathList (); 327 PathList o = new PathList (); 328 PathList pl = new PathList (); 329 PathList r = new PathList (); 330 331 pl.add (path1); 332 pl.add (path2); 333 334 error = false; 335 336 if (!self_intersection) { 337 if (!path1.boundaries_intersecting (path2)) { 338 return r; 339 } 340 } 341 342 foreach (Path p in pl.paths) { 343 if (p.stroke == 0) { 344 o.add (p); 345 flat.add (p.copy ().flatten (50)); 346 } 347 } 348 349 flat = merge (flat); 350 351 foreach (Path pp in o.paths) { 352 pp.remove_points_on_points (0.1); 353 } 354 355 bool has_split_point = false; 356 foreach (Path p in flat.paths) { 357 foreach (EditPoint ep in p.points) { 358 if ((ep.flags & EditPoint.SPLIT_POINT) > 0) { 359 foreach (Path pp in o.paths) { 360 EditPoint lep = new EditPoint (); 361 362 if (pp.points.size > 1) { 363 pp.get_closest_point_on_path (lep, ep.x, ep.y, null, null); 364 365 if (Path.distance_to_point (ep, lep) < 0.1) { 366 EditPoint lep2 = new EditPoint (); 367 pp.get_closest_point_on_path (lep2, ep.x, ep.y, lep.get_prev (), lep.get_next ()); 368 369 if (lep.has_prev ()) { 370 lep.get_left_handle ().type = lep.get_prev ().get_right_handle ().type; 371 } else { 372 lep.get_left_handle ().type = pp.get_last_point ().get_right_handle ().type; 373 } 374 375 if (lep.has_next ()) { 376 lep.get_right_handle ().type = lep.get_next ().get_left_handle ().type; 377 } else { 378 lep.get_left_handle ().type = pp.get_first_point ().get_right_handle ().type; 379 } 380 381 if (lep2.has_prev ()) { 382 lep2.get_left_handle ().type = lep2.get_prev ().get_right_handle ().type; 383 } else { 384 lep2.get_left_handle ().type = pp.get_first_point ().get_right_handle ().type; 385 } 386 387 if (lep2.has_next ()) { 388 lep2.get_right_handle ().type = lep2.get_next ().get_left_handle ().type; 389 } else { 390 lep2.get_left_handle ().type = pp.get_last_point ().get_right_handle ().type; 391 } 392 393 // self intersection 394 if (Path.distance_to_point (ep, lep2) < 0.1 395 && Path.distance_to_point (ep, lep) < 0.1) { 396 397 if (Path.distance_to_point (lep, lep.get_prev ()) < 0.001) { 398 continue; 399 } 400 401 if (Path.distance_to_point (lep, lep.get_next ()) < 0.001) { 402 continue; 403 } 404 405 add_double_point_at_intersection (pp, lep, ep); 406 add_double_point_at_intersection (pp, lep2, ep); 407 408 pp.insert_new_point_on_path (lep); 409 pp.insert_new_point_on_path (lep2); 410 411 lep.flags |= EditPoint.SELF_INTERSECTION; 412 lep2.flags |= EditPoint.SELF_INTERSECTION; 413 414 lep.tie_handles = false; 415 lep.reflective_point = false; 416 lep2.tie_handles = false; 417 lep2.reflective_point = false; 418 } else { 419 if (lep.has_prev () && Path.distance_to_point (lep, lep.get_prev ()) < 0.00000001) { 420 lep.get_prev ().flags |= EditPoint.INTERSECTION; 421 lep.get_prev ().tie_handles = false; 422 lep.get_prev ().reflective_point = false; 423 continue; 424 } 425 426 if (lep.has_next () && Path.distance_to_point (lep, lep.get_next ()) < 0.00000001) { 427 lep.get_next ().flags |= EditPoint.INTERSECTION; 428 lep.get_next ().tie_handles = false; 429 lep.get_next ().reflective_point = false; 430 continue; 431 } 432 433 add_double_point_at_intersection (pp, lep, ep); 434 pp.insert_new_point_on_path (lep); 435 lep.flags |= EditPoint.INTERSECTION; 436 lep.tie_handles = false; 437 lep.reflective_point = false; 438 } 439 440 has_split_point = true; 441 } 442 } 443 } 444 } 445 } 446 } 447 448 if (!has_split_point) { 449 return r; 450 } 451 452 // remove double intersection points 453 EditPoint prev = new EditPoint (); 454 foreach (Path pp in o.paths) { 455 foreach (EditPoint ep in pp.points) { 456 if (((prev.flags & EditPoint.SELF_INTERSECTION) > 0 || (prev.flags & EditPoint.INTERSECTION) > 0) 457 && ((ep.flags & EditPoint.SELF_INTERSECTION) > 0 || (ep.flags & EditPoint.INTERSECTION) > 0) 458 && fabs (ep.x - prev.x) < 0.2 459 && fabs (ep.y - prev.y) < 0.2) { 460 prev.deleted = true; 461 } 462 463 prev = ep; 464 } 465 } 466 467 foreach (Path pp in o.paths) { 468 pp.remove_deleted_points (); 469 } 470 471 foreach (Path p in o.paths) { 472 foreach (EditPoint ep in p.points) { 473 ep.flags &= uint.MAX ^ EditPoint.COPIED; 474 } 475 } 476 477 return_val_if_fail (o.paths.size == 2, r); 478 479 Path p1, p2; 480 481 p1 = o.paths.get (0); 482 p2 = o.paths.get (1); 483 PathList parts = new PathList (); 484 485 if (self_intersection) { 486 // remove overlap 487 PathList self_parts; 488 489 self_parts = remove_self_intersections (p1, out error); 490 491 if (error) { 492 warning ("remove_self_intersections failed"); 493 return new PathList (); 494 } 495 496 parts.append (self_parts); 497 } else { 498 // merge two path 499 PathList merged_paths = merge_paths_with_curves (p1, p2); 500 501 if (merged_paths.paths.size > 0) { 502 parts.append (merged_paths); 503 } else { 504 parts.add (p1); 505 parts.add (p2); 506 } 507 } 508 509 foreach (Path p in parts.paths) { 510 reset_intersections (p); 511 } 512 513 reset_intersections (path1); 514 reset_intersections (path2); 515 516 return parts; 517 } 518 519 /** Add hidden double points to make sure that the path does not 520 * change when new points are added to a 2x2 path. 521 */ 522 void add_double_point_at_intersection (Path pp, EditPoint lep, EditPoint ep) { 523 EditPoint before; 524 EditPoint after; 525 EditPoint hidden; 526 double px, py; 527 528 if (lep.get_right_handle ().type == PointType.DOUBLE_CURVE 529 || lep.get_right_handle ().type == PointType.LINE_DOUBLE_CURVE) { 530 531 return_if_fail (lep.has_prev ()); 532 return_if_fail (lep.has_next ()); 533 534 before = lep.get_prev (); 535 after = lep.get_next (); 536 hidden = new EditPoint (0, 0, PointType.QUADRATIC); 537 538 px = before.get_right_handle ().x 539 + (after.get_left_handle ().x - before.get_right_handle ().x) / 2.0; 540 py = before.get_right_handle ().y 541 + (after.get_left_handle ().y - before.get_right_handle ().y) / 2.0; 542 hidden.independent_x = px; 543 hidden.independent_y = py; 544 545 hidden.get_right_handle ().x = after.get_left_handle ().x; 546 hidden.get_right_handle ().y = after.get_left_handle ().y; 547 hidden.get_left_handle ().x = before.get_right_handle ().x; 548 hidden.get_left_handle ().y = before.get_right_handle ().y; 549 550 pp.add_point_after (hidden, before); 551 552 hidden.get_right_handle ().type = PointType.QUADRATIC; 553 hidden.get_left_handle ().type = PointType.QUADRATIC; 554 555 before.get_right_handle ().type = PointType.QUADRATIC; 556 after.get_left_handle ().type = PointType.QUADRATIC; 557 before.type = PointType.QUADRATIC; 558 after.type = PointType.QUADRATIC; 559 560 pp.get_closest_point_on_path (lep, ep.x, ep.y, null, null); 561 } 562 } 563 564 PathList remove_self_intersections (Path original, out bool error) { 565 Path merged = new Path (); 566 IntersectionList intersections = new IntersectionList (); 567 EditPoint ep1, ep2, found; 568 double d; 569 double min_d; 570 Path current; 571 bool found_intersection; 572 PathList parts; 573 int i = 0; 574 Path path = original.copy (); 575 576 error = false; 577 578 path.remove_points_on_points (); 579 parts = new PathList (); 580 581 if (path.points.size <= 1) { 582 return parts; 583 } 584 585 // reset copied points 586 foreach (EditPoint n in path.points) { 587 n.flags &= uint.MAX ^ EditPoint.COPIED; 588 } 589 590 // build list of intersection points 591 for (i = 0; i < path.points.size; i++) { 592 ep1 = path.points.get (i); 593 594 if ((ep1.flags & EditPoint.SELF_INTERSECTION) > 0 595 && (ep1.flags & EditPoint.COPIED) == 0) { 596 ep1.flags |= EditPoint.COPIED; 597 598 found = new EditPoint (); 599 min_d = double.MAX; 600 found_intersection = false; 601 602 for (int j = 0; j < path.points.size; j++) { 603 ep2 = path.points.get (j); 604 d = Path.distance_to_point (ep1, ep2); 605 if ((ep2.flags & EditPoint.COPIED) == 0 606 && (ep2.flags & EditPoint.SELF_INTERSECTION) > 0) { 607 if (d < min_d) { 608 min_d = d; 609 found_intersection = true; 610 found = ep2; 611 } 612 } 613 } 614 615 if (!found_intersection) { 616 warning (@"No self intersection:\n$(ep1)"); 617 return parts; 618 } 619 620 ep1.tie_handles = false; 621 ep1.reflective_point = false; 622 found.tie_handles = false; 623 found.reflective_point = false; 624 625 found.flags |= EditPoint.COPIED; 626 Intersection intersection = new Intersection (ep1, path, found, path); 627 intersection.self_intersection = true; 628 intersections.points.add (intersection); 629 } 630 } 631 632 // reset copy flag 633 foreach (EditPoint n in path.points) { 634 n.flags &= uint.MAX ^ EditPoint.COPIED; 635 } 636 637 if (intersections.points.size == 0) { 638 warning ("No intersection points."); 639 error = true; 640 return parts; 641 } 642 643 current = path; 644 current.reverse (); 645 646 while (true) { 647 EditPoint modified; 648 i = 0; 649 Intersection new_start = new Intersection.empty (); 650 ep1 = current.points.get (i); 651 current = path; 652 653 modified = ep1.copy (); 654 655 for (i = 0; i < current.points.size; i++) { 656 ep1 = current.points.get (i); 657 modified = ep1.copy (); 658 if ((ep1.flags & EditPoint.COPIED) == 0 659 && (ep1.flags & EditPoint.SELF_INTERSECTION) == 0) { 660 break; 661 } 662 } 663 664 if (i >= current.points.size || (ep1.flags & EditPoint.COPIED) > 0) { 665 // all points have been copied 666 break; 667 } 668 669 while (true) { 670 671 if ((ep1.flags & EditPoint.SELF_INTERSECTION) > 0) { 672 bool other; 673 EditPointHandle handle; 674 675 handle = ep1.get_left_handle (); 676 new_start = intersections.get_point (ep1, out other); 677 678 i = index_of (current, other ? new_start.point : new_start.other_point); 679 680 if (!(0 <= i < current.points.size)) { 681 warning (@"Index out of bounds. ($i)"); 682 return parts; 683 } 684 685 ep1 = current.points.get (i); 686 modified = ep1.copy (); 687 modified.left_handle.move_to_coordinate (handle.x, handle.y); 688 } 689 690 if ((ep1.flags & EditPoint.COPIED) > 0) { 691 merged.close (); 692 693 merged.close (); 694 merged.create_list (); 695 parts.add (merged); 696 697 foreach (EditPoint n in merged.points) { 698 n.flags &= uint.MAX ^ EditPoint.SELF_INTERSECTION; 699 } 700 701 merged.reverse (); 702 703 merged = new Path (); 704 705 break; 706 } 707 708 // adjust the other handle 709 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 710 ep1.left_handle.convert_to_curve (); 711 ep1.right_handle.convert_to_curve (); 712 ep1.tie_handles = false; 713 ep1.reflective_point = false; 714 } 715 716 // add point to path 717 ep1.flags |= EditPoint.COPIED; 718 merged.add_point (modified.copy ()); 719 720 i++; 721 ep1 = current.points.get (i % current.points.size); 722 modified = ep1.copy (); 723 } 724 725 ep1.flags |= EditPoint.COPIED; 726 727 } 728 729 return parts; 730 } 731 732 PathList merge_paths_with_curves (Path path1, Path path2) { 733 PathList r = new PathList (); 734 IntersectionList intersections = new IntersectionList (); 735 EditPoint ep1, ep2, found; 736 double d; 737 double min_d; 738 Path current; 739 bool found_intersection; 740 Path flat1, flat2; 741 742 if (path1.points.size <= 1 || path2.points.size <= 1) { 743 return r; 744 } 745 746 flat1 = path1.flatten (); 747 flat2 = path2.flatten (); 748 749 // reset copied points 750 foreach (EditPoint n in path2.points) { 751 n.flags &= uint.MAX ^ EditPoint.COPIED; 752 } 753 754 // build list of intersection points 755 for (int i = 0; i < path1.points.size; i++) { 756 ep1 = path1.points.get (i); 757 758 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 759 found = new EditPoint (); 760 min_d = double.MAX; 761 found_intersection = false; 762 for (int j = 0; j < path2.points.size; j++) { 763 ep2 = path2.points.get (j); 764 d = Path.distance_to_point (ep1, ep2); 765 if ((ep2.flags & EditPoint.COPIED) == 0 766 && (ep2.flags & EditPoint.INTERSECTION) > 0) { 767 if (d < min_d && d < 0.1) { 768 min_d = d; 769 found_intersection = true; 770 found = ep2; 771 } 772 } 773 } 774 775 if (!found_intersection) { 776 warning (@"No intersection for:\n $(ep1)"); 777 continue; 778 } 779 780 found.flags |= EditPoint.COPIED; 781 782 ep1.tie_handles = false; 783 ep1.reflective_point = false; 784 found.tie_handles = false; 785 found.reflective_point = false; 786 Intersection intersection = new Intersection (ep1, path1, found, path2); 787 intersections.points.add (intersection); 788 } 789 } 790 791 // reset copy flag 792 foreach (EditPoint n in path1.points) { 793 n.flags &= uint.MAX ^ EditPoint.COPIED; 794 } 795 796 foreach (EditPoint n in path2.points) { 797 n.flags &= uint.MAX ^ EditPoint.COPIED; 798 } 799 800 if (intersections.points.size == 0) { 801 warning ("No intersection points."); 802 return r; 803 } 804 805 Path new_path = new Path (); 806 current = path1; 807 while (true) { 808 // find a beginning of a new part 809 bool find_parts = false; 810 Intersection new_start = new Intersection.empty (); 811 foreach (Intersection inter in intersections.points) { 812 if (!inter.done && !find_parts) { 813 find_parts = true; 814 new_start = inter; 815 current = new_start.path; 816 } 817 } 818 819 if (new_path.points.size > 0) { 820 new_path.close (); 821 new_path.recalculate_linear_handles (); 822 new_path.update_region_boundaries (); 823 r.add (new_path); 824 } 825 826 if (!find_parts) { // no more parts 827 break; 828 } 829 830 if ((new_start.get_point (current).flags & EditPoint.COPIED) > 0) { 831 current = new_start.get_other_path (current); 832 } 833 834 int i = index_of (current, new_start.get_point (current)); 835 836 if (i < 0) { 837 warning ("i < 0"); 838 return r; 839 } 840 841 EditPoint previous = new EditPoint (); 842 new_path = new Path (); 843 ep1 = current.points.get (i); 844 current = new_start.get_other_path (current); // swap at first iteration 845 bool first = true; 846 while (true) { 847 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 848 bool other; 849 850 previous = ep1; 851 852 if (likely (intersections.has_point (ep1))) { 853 new_start = intersections.get_point (ep1, out other); 854 current = new_start.get_other_path (current); 855 i = index_of (current, new_start.get_point (current)); 856 857 if (!(0 <= i < current.points.size)) { 858 warning (@"Index out of bounds. ($i)"); 859 return r; 860 } 861 862 ep1 = current.points.get (i); 863 ep2 = current.points.get ((i + 1) % current.points.size); 864 865 double px, py; 866 867 Path.get_point_for_step (ep1, ep2, 0.5, out px, out py); 868 bool inside = (current == path1 && flat2.is_over_coordinate (px, py)) 869 || (current == path2 && flat1.is_over_coordinate (px, py)); 870 871 bool other_inside = (current != path1 && flat2.is_over_coordinate (px, py)) 872 || (current != path2 && flat1.is_over_coordinate (px, py)); 873 874 if (inside && !other_inside) { 875 current = new_start.get_other_path (current); 876 i = index_of (current, new_start.get_point (current)); 877 878 if (!(0 <= i < current.points.size)) { 879 warning (@"Index out of bounds. ($i >= $(current.points.size)) "); 880 return r; 881 } 882 883 new_start.done = true; 884 ep1 = current.points.get (i); 885 } 886 887 inside = (current == path1 && flat2.is_over_coordinate (px, py)) 888 || (current == path2 && flat1.is_over_coordinate (px, py)); 889 890 if (first) { 891 Path c = new_start.get_other_path (current); 892 if (c.points.size >= 1) { 893 previous = c.get_first_point (); 894 } 895 896 first = false; 897 } 898 } 899 } 900 901 if ((ep1.flags & EditPoint.COPIED) > 0) { 902 new_path.close (); 903 904 if (new_path.points.size >= 1) { 905 EditPoint first_point = new_path.get_first_point (); 906 EditPointHandle h; 907 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 908 first_point.left_handle.move_to_coordinate (previous.left_handle.x, previous.left_handle.y); 909 910 if (first_point.has_next ()) { 911 h = first_point.get_next ().get_left_handle (); 912 h.process_connected_handle (); 913 } 914 } 915 } 916 917 break; 918 } 919 920 // adjust the other handle 921 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 922 ep1.left_handle.convert_to_curve (); 923 ep1.right_handle.convert_to_curve (); 924 } 925 926 // add point to path 927 ep1.flags |= EditPoint.COPIED; 928 new_path.add_point (ep1.copy ()); 929 930 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 931 new_path.get_last_point ().left_handle.move_to_coordinate (previous.left_handle.x, previous.left_handle.y); 932 } 933 934 i++; 935 ep1 = current.points.get (i % current.points.size); 936 } 937 938 ep1.flags |= EditPoint.COPIED; 939 940 if (!new_start.done) { 941 new_start.done = (new_start.get_other_point (current).flags & EditPoint.COPIED) > 0; 942 } 943 } 944 945 return r; 946 } 947 948 Path simplify_stroke (Path p) { 949 Path simplified = new Path (); 950 Path segment, added_segment; 951 EditPoint ep, ep_start, last, first, segment_last; 952 int start, stop; 953 int j; 954 EditPointHandle last_handle; 955 956 last_handle = new EditPointHandle.empty (); 957 958 segment_last = new EditPoint (); 959 last = new EditPoint (); 960 961 p.remove_points_on_points (); 962 963 foreach (EditPoint e in p.points) { 964 PenTool.convert_point_type (e, PointType.CUBIC); 965 } 966 967 bool has_curve_start = true; 968 foreach (EditPoint e in p.points) { 969 e.flags &= uint.MAX ^ EditPoint.NEW_CORNER; 970 971 if ((e.flags & EditPoint.CURVE) == 0) { 972 p.set_new_start (e); 973 has_curve_start = false; 974 break; 975 } 976 } 977 978 if (has_curve_start) { 979 warning ("Curve start"); 980 } 981 982 for (int i = 0; i < p.points.size; i++) { 983 ep = p.points.get (i); 984 985 if ((ep.flags & EditPoint.CURVE) > 0) { 986 start = i; 987 for (j = start + 1; j < p.points.size; j++) { 988 ep = p.points.get (j); 989 if ((ep.flags & EditPoint.CURVE) == 0) { 990 break; 991 } 992 } 993 994 if (task.is_cancelled ()) { 995 return new Path (); 996 } 997 998 999 stop = j; 1000 start -= 1; 1001 1002 if (start < 0) { 1003 warning ("start < 0"); 1004 start = 0; 1005 } 1006 1007 if (stop >= p.points.size) { 1008 warning ("stop >= p.points.size"); 1009 stop = p.points.size - 1; 1010 } 1011 1012 ep_start = p.points.get (start); 1013 ep = p.points.get (stop); 1014 1015 double l = Path.distance_to_point (ep_start, ep); 1016 segment = fit_bezier_path (p, start, stop, 0.00001 * l * l); 1017 1018 added_segment = segment.copy (); 1019 1020 if (simplified.points.size > 0) { 1021 last = simplified.get_last_point (); 1022 } 1023 1024 if (added_segment.points.size > 0) { 1025 segment_last = added_segment.get_last_point (); 1026 first = added_segment.get_first_point (); 1027 segment_last.right_handle = ep_start.get_right_handle ().copy (); 1028 1029 if (simplified.points.size > 1) { 1030 last = simplified.delete_last_point (); 1031 } 1032 1033 first.set_tie_handle (false); 1034 last.set_tie_handle (false); 1035 1036 last.get_right_handle ().x = first.get_right_handle ().x; 1037 last.get_right_handle ().y = first.get_right_handle ().y; 1038 1039 first.get_left_handle ().convert_to_curve (); 1040 first.get_left_handle ().x = last.get_left_handle ().x; 1041 first.get_left_handle ().y = last.get_left_handle ().y; 1042 1043 last = added_segment.get_last_point (); 1044 last.right_handle = ep.get_right_handle ().copy (); 1045 last.recalculate_linear_handles (); 1046 1047 simplified.append_path (added_segment); 1048 1049 segment_last.right_handle = ep.get_right_handle ().copy (); 1050 1051 if (added_segment.points.size > 0) { 1052 if (ep_start.get_right_handle ().is_line ()) { 1053 first = added_segment.get_first_point (); 1054 first.recalculate_linear_handles (); 1055 } 1056 } 1057 1058 last_handle = last.get_left_handle (); 1059 } else { 1060 warning ("No points in segment."); 1061 } 1062 1063 i = stop; 1064 } else { 1065 simplified.add_point (ep.copy ()); 1066 } 1067 } 1068 1069 simplified.recalculate_linear_handles (); 1070 simplified.close (); 1071 remove_single_point_intersections (simplified); 1072 1073 return simplified; 1074 } 1075 1076 Path fit_bezier_path (Path p, int start, int stop, double error) { 1077 int index, size; 1078 Path simplified; 1079 double[] lines; 1080 double[] result; 1081 EditPoint ep; 1082 1083 simplified = new Path (); 1084 1085 return_val_if_fail (0 <= start < p.points.size, simplified); 1086 return_val_if_fail (0 <= stop < p.points.size, simplified); 1087 1088 size = stop - start + 1; 1089 lines = new double[2 * size]; 1090 1091 index = 0; 1092 1093 for (int i = start; i <= stop; i++) { 1094 ep = p.points.get (i); 1095 lines[index] = ep.x; 1096 index++; 1097 1098 lines[index] = ep.y; 1099 index++; 1100 } 1101 1102 return_val_if_fail (2 * size == index, new Path ()); 1103 1104 Gems.fit_bezier_curve_to_line (lines, error, out result); 1105 1106 return_val_if_fail (!is_null (result), simplified); 1107 1108 for (int i = 0; i + 7 < result.length; i += 8) { 1109 simplified.add_cubic_bezier_points ( 1110 result[i], result[i + 1], 1111 result[i + 2], result[i + 3], 1112 result[i + 4], result[i + 5], 1113 result[i + 6], result[i + 7]); 1114 } 1115 1116 return simplified; 1117 } 1118 1119 PathList remove_intersection_paths (PathList pl) { 1120 PathList r = new PathList (); 1121 1122 foreach (Path p in pl.paths) { 1123 if (p.points.size > 7) { 1124 r.add (p); 1125 } else if (!has_new_corner (p)) { 1126 r.add (p); 1127 } else if (counters (pl, p) == 0) { 1128 r.add (p); 1129 } 1130 } 1131 1132 return r; 1133 } 1134 1135 bool has_new_corner (Path p) { 1136 foreach (EditPoint ep in p.points) { 1137 if ((ep.flags & EditPoint.NEW_CORNER) > 0) { 1138 return true; 1139 } 1140 } 1141 1142 return false; 1143 } 1144 1145 void add_line_cap (Path path, Path stroke1, Path stroke2, bool last_cap) { 1146 if (path.line_cap == LineCap.SQUARE) { 1147 add_square_cap (path, stroke1, stroke2, last_cap); 1148 } else if (path.line_cap == LineCap.ROUND) { 1149 add_round_cap (path, stroke1, stroke2, last_cap); 1150 } 1151 } 1152 1153 void add_round_cap (Path path, Path stroke1, Path stroke2, bool last_cap) { 1154 double px, py; 1155 double step, start_angle, stop_angle; 1156 double radius; 1157 EditPoint n, nstart, nend; 1158 Path cap = new Path (); 1159 1160 EditPoint start, end; 1161 EditPointHandle last_handle; 1162 EditPoint first, last; 1163 1164 stroke1.remove_points_on_points (); 1165 stroke2.remove_points_on_points (); 1166 1167 last_handle = path.get_first_point ().get_right_handle (); 1168 start = stroke1.get_last_point (); 1169 end = stroke2.get_first_point (); 1170 1171 start_angle = last_handle.angle + PI / 2; 1172 stop_angle = start_angle + PI; 1173 1174 nstart = cap.add (start.x, start.y); 1175 radius = Path.distance_to_point (start, end) / 2; 1176 step = PI / 5; 1177 1178 for (int j = 0; j < 5; j++) { 1179 double angle = start_angle + step * j; 1180 px = radius * cos (angle) + last_handle.parent.x; 1181 py = radius * sin (angle) + last_handle.parent.y; 1182 n = cap.add (px, py); 1183 1184 n.type = PointType.LINE_CUBIC; 1185 n.get_right_handle ().type = PointType.LINE_CUBIC; 1186 n.get_left_handle ().type = PointType.LINE_CUBIC; 1187 } 1188 1189 nend = cap.add (end.x, end.y); 1190 1191 for (int i = 0; i < cap.points.size; i++) { 1192 cap.points.get (i).recalculate_linear_handles (); 1193 } 1194 1195 int size = cap.points.size; 1196 1197 for (int i = 1; i < size; i++) { 1198 n = cap.points.get (i); 1199 n.convert_to_curve (); 1200 n.set_tie_handle (true); 1201 n.process_tied_handle (); 1202 } 1203 1204 int f = stroke1.points.size - 1; 1205 1206 for (int i = 2; i < cap.points.size - 1; i++) { 1207 n = cap.points.get (i).copy (); 1208 stroke1.add_point (n); 1209 } 1210 1211 cap.remove_points_on_points (); 1212 return_if_fail (0 < f < stroke1.points.size); 1213 1214 first = stroke1.points.get (f); 1215 1216 last = stroke1.get_last_point (); 1217 last.convert_to_curve (); 1218 1219 last = stroke1.add_point (stroke2.get_first_point ()); 1220 stroke2.delete_first_point (); 1221 1222 last.convert_to_line (); 1223 last.recalculate_linear_handles (); 1224 1225 stroke2.delete_first_point (); 1226 1227 last.get_left_handle ().convert_to_curve (); 1228 last.get_left_handle ().angle = last.get_right_handle ().angle + PI; 1229 last.flags = EditPoint.CURVE_KEEP; 1230 1231 double a; 1232 double l; 1233 1234 return_if_fail (cap.points.size > 1); 1235 1236 a = (first.get_left_handle ().angle + PI) % (2 * PI); 1237 l = cap.points.get (1).get_right_handle ().length; 1238 1239 first.get_right_handle ().convert_to_curve (); 1240 first.get_right_handle ().angle = a; 1241 first.get_right_handle ().length = l; 1242 1243 a = (first.get_left_handle ().angle + PI) % (2 * PI); 1244 1245 last.get_left_handle ().convert_to_curve (); 1246 last.get_left_handle ().angle = a; 1247 last.get_left_handle ().length = l; 1248 } 1249 1250 void add_square_cap (Path path, Path stroke1, Path stroke2, bool last_cap) { 1251 EditPointHandle last_handle; 1252 EditPoint start; 1253 EditPoint end; 1254 EditPoint n; 1255 double x, y; 1256 double stroke_width = path.stroke / 2; 1257 1258 last_handle = path.get_first_point ().get_right_handle (); 1259 start = stroke1.get_last_point (); 1260 end = stroke2.get_first_point (); 1261 1262 y = sin (last_handle.angle - PI) * stroke_width; 1263 x = cos (last_handle.angle - PI) * stroke_width; 1264 1265 n = stroke1.add (start.x + x, start.y + y); 1266 n.type = PointType.CUBIC; 1267 n.get_right_handle ().type = PointType.CUBIC; 1268 n.get_left_handle ().type = PointType.CUBIC; 1269 n.convert_to_line (); 1270 1271 n = stroke1.add (end.x + x, end.y + y); 1272 n.type = PointType.CUBIC; 1273 n.get_right_handle ().type = PointType.CUBIC; 1274 n.get_left_handle ().type = PointType.CUBIC; 1275 n.convert_to_line (); 1276 } 1277 1278 /** Create one stroke from the outline and counter stroke and close the 1279 * open endings. 1280 * 1281 * @param path the path to create stroke for 1282 * @param stroke for the outline of path 1283 * @param stroke for the counter path 1284 */ 1285 Path merge_strokes (Path path, Path stroke, Path counter) { 1286 1287 Path merged; 1288 EditPoint last_counter, first; 1289 1290 merged = stroke.copy (); 1291 merged.reverse (); 1292 1293 last_counter = new EditPoint (); 1294 first = new EditPoint (); 1295 1296 add_line_cap (path, merged, counter, true); 1297 path.reverse (); 1298 1299 add_line_cap (path, counter, merged, true); 1300 path.reverse (); 1301 1302 merged.append_path (counter); 1303 1304 merged.close (); 1305 merged.create_list (); 1306 merged.recalculate_linear_handles (); 1307 1308 return merged; 1309 } 1310 1311 void move_segment (EditPoint stroke_start, EditPoint stroke_stop, double thickness) { 1312 EditPointHandle r, l; 1313 double m, n; 1314 double qx, qy; 1315 1316 stroke_start.set_tie_handle (false); 1317 stroke_stop.set_tie_handle (false); 1318 1319 r = stroke_start.get_right_handle (); 1320 l = stroke_stop.get_left_handle (); 1321 1322 m = cos (r.angle + PI / 2) * thickness; 1323 n = sin (r.angle + PI / 2) * thickness; 1324 1325 stroke_start.get_right_handle ().move_to_coordinate_delta (m, n); 1326 stroke_start.get_left_handle ().move_to_coordinate_delta (m, n); 1327 1328 stroke_start.independent_x += m; 1329 stroke_start.independent_y += n; 1330 1331 qx = cos (l.angle - PI / 2) * thickness; 1332 qy = sin (l.angle - PI / 2) * thickness; 1333 1334 stroke_stop.get_right_handle ().move_to_coordinate_delta (qx, qy); 1335 stroke_stop.get_left_handle ().move_to_coordinate_delta (qx, qy); 1336 1337 stroke_stop.independent_x += qx; 1338 stroke_stop.independent_y += qy; 1339 } 1340 1341 void add_corner (Path stroked, EditPoint previous, EditPoint next, 1342 EditPoint original, double stroke_width) { 1343 1344 double ratio; 1345 double distance; 1346 EditPoint corner; 1347 double corner_x, corner_y; 1348 EditPointHandle previous_handle; 1349 EditPointHandle next_handle; 1350 EditPoint cutoff1, cutoff2; 1351 double adjusted_stroke = (stroke_width * 0.999999) / 2.0; 1352 bool d1, d2; 1353 1354 previous_handle = previous.get_left_handle (); 1355 next_handle = next.get_right_handle (); 1356 1357 previous_handle.convert_to_line (); 1358 next_handle.convert_to_line (); 1359 1360 previous_handle.angle += PI; 1361 next_handle.angle += PI; 1362 1363 Path.find_intersection_handle (previous_handle, next_handle, out corner_x, out corner_y); 1364 corner = new EditPoint (corner_x, corner_y, PointType.CUBIC); 1365 corner.convert_to_line (); 1366 1367 previous_handle.angle -= PI; 1368 next_handle.angle -= PI; 1369 1370 distance = Path.distance_to_point (corner, original); 1371 ratio = 1.5 * fabs (adjusted_stroke) / distance; 1372 1373 double or = original.get_right_handle ().angle; 1374 double ol = original.get_left_handle ().angle; 1375 1376 if (!previous.has_prev ()) { // FIXME: first point 1377 warning ("Point before corner."); 1378 d1 = false; 1379 d2 = false; 1380 } else { 1381 d1 = corner.x - previous.x >= 0 == previous.x - previous.get_prev ().x >= 0; 1382 d2 = corner.y - previous.y >= 0 == previous.y - previous.get_prev ().y >= 0; 1383 } 1384 1385 if (ratio > 1) { 1386 if (!d1 && !d2) { 1387 return; 1388 } else { 1389 stroked.add_point (corner); 1390 } 1391 } else { 1392 1393 cutoff1 = new EditPoint (); 1394 cutoff1.set_point_type (previous.type); 1395 cutoff1.convert_to_line (); 1396 1397 cutoff2 = new EditPoint (); 1398 cutoff2.set_point_type (previous.type); 1399 cutoff2.convert_to_line (); 1400 1401 if (fabs (or - ol) < 0.001) { 1402 cutoff1.x = previous.x + 1.5 * fabs (adjusted_stroke) * -cos (or); 1403 cutoff1.y = previous.y + 1.5 * fabs (adjusted_stroke) * -sin (or); 1404 1405 cutoff2.x = next.x + 1.5 * fabs (adjusted_stroke) * -cos (or); 1406 cutoff2.y = next.y + 1.5 * fabs (adjusted_stroke) * -sin (or); 1407 } else { 1408 cutoff1.x = previous.x + (corner.x - previous.x) * ratio; 1409 cutoff1.y = previous.y + (corner.y - previous.y) * ratio; 1410 1411 cutoff2.x = next.x + (corner.x - next.x) * ratio; 1412 cutoff2.y = next.y + (corner.y - next.y) * ratio; 1413 } 1414 1415 if (!cutoff1.is_valid () || cutoff2.is_valid ()) { 1416 cutoff1 = stroked.add_point (cutoff1); 1417 cutoff2 = stroked.add_point (cutoff2); 1418 } 1419 1420 cutoff1.recalculate_linear_handles (); 1421 cutoff2.recalculate_linear_handles (); 1422 1423 // self intersection 1424 if (!d1 && !d2) { 1425 cutoff1.deleted = true; 1426 cutoff2.deleted = true; 1427 1428 stroked.remove_deleted_points (); 1429 return; 1430 } 1431 1432 if (distance > 4 * stroke_width) { 1433 previous.flags = EditPoint.NONE; 1434 next.flags = EditPoint.NONE; 1435 } else { 1436 previous.flags |= EditPoint.NEW_CORNER; 1437 next.flags |= EditPoint.NEW_CORNER; 1438 } 1439 } 1440 } 1441 1442 PathList get_parts (Path path) { 1443 PathList intersections; 1444 PathList r; 1445 1446 r = get_parts_self (path); 1447 intersections = new PathList (); 1448 1449 foreach (Path p in r.paths) { 1450 intersections.add (p); 1451 } 1452 1453 return intersections; 1454 } 1455 1456 bool split_corner (PathList pl) { 1457 EditPoint p1, p2; 1458 EditPoint a1, a2; 1459 PathList result; 1460 bool split; 1461 1462 foreach (Path p in pl.paths) { 1463 if (p.points.size == 0) { 1464 continue; 1465 } 1466 1467 for (int index = 1; index < p.points.size + 2; index++) { 1468 p1 = p.points.get ((index - 1) % p.points.size); 1469 p2 = p.points.get (index % p.points.size); 1470 a1 = p.points.get ((index + 3) % p.points.size); // two points ahead 1471 a2 = p.points.get ((index + 4) % p.points.size); 1472 1473 if ((p1.flags & EditPoint.STROKE_OFFSET) > 0 1474 || (p2.flags & EditPoint.STROKE_OFFSET) > 0 1475 || (a1.flags & EditPoint.STROKE_OFFSET) > 0 1476 || (a2.flags & EditPoint.STROKE_OFFSET) > 0) { 1477 1478 split = split_segment (p, a1, a2, p1, p2, out result); 1479 1480 if (split) { 1481 pl.append (result); 1482 pl.paths.remove (p); 1483 split_corner (pl); 1484 return true; 1485 } else { 1486 p1 = p.points.get ((index - 1) % p.points.size); 1487 p2 = p.points.get (index % p.points.size); 1488 a1 = p.points.get ((index + 2) % p.points.size); // one point ahead 1489 a2 = p.points.get ((index + 3) % p.points.size); 1490 1491 split = split_segment (p, a1, a2, p1, p2, out result); 1492 1493 if (split) { 1494 pl.append (result); 1495 pl.paths.remove (p); 1496 split_corner (pl); 1497 return true; 1498 } else { 1499 p1 = p.points.get ((index - 1) % p.points.size); 1500 p2 = p.points.get (index % p.points.size); 1501 a1 = p.points.get ((index + 3) % p.points.size); // two points ahead 1502 a2 = p.points.get ((index + 4) % p.points.size); 1503 1504 if ((p1.flags & EditPoint.STROKE_OFFSET) > 0 1505 && (a1.flags & EditPoint.STROKE_OFFSET) > 0) { 1506 p1.flags = EditPoint.COUNTER_TO_OUTLINE; 1507 a1.flags = EditPoint.COUNTER_TO_OUTLINE; 1508 } 1509 } 1510 } 1511 } 1512 } 1513 } 1514 1515 return false; 1516 } 1517 1518 bool split_segment (Path p, EditPoint first, EditPoint next, EditPoint p1, EditPoint p2, out PathList result) { 1519 double ix, iy; 1520 bool intersection; 1521 int i; 1522 1523 result = new PathList (); 1524 intersection = segments_intersects (first, next, p1, p2, out ix, out iy, true); 1525 1526 if (intersection) { 1527 add_intersection (p, first, next, ix, iy); 1528 add_intersection (p, p1, p2, ix, iy); 1529 1530 i = mark_intersection_as_deleted (p); 1531 return_val_if_fail (i == 2, false); 1532 1533 result.append (get_remaining_points (p.copy ())); 1534 } 1535 1536 return intersection; 1537 } 1538 1539 PathList get_parts_self (Path path, PathList? paths = null) { 1540 PathList pl; 1541 PathList r; 1542 1543 if (task.is_cancelled ()) { 1544 return new PathList (); 1545 } 1546 1547 r = paths == null ? new PathList () : (!) paths; 1548 pl = split (path); 1549 1550 foreach (Path part in pl.paths) { 1551 if (!has_self_intersection (part)) { 1552 r.add (part); 1553 } else { 1554 get_parts_self (part, r); 1555 } 1556 } 1557 1558 if (r.paths.size == 0) { 1559 warning ("No parts in path"); 1560 } 1561 1562 return r; 1563 } 1564 1565 1566 bool has_intersection_points (Path path) { 1567 foreach (EditPoint p in path.points) { 1568 if ((p.flags & EditPoint.INTERSECTION) > 0) { 1569 return true; 1570 } 1571 } 1572 return false; 1573 } 1574 1575 /** Split one path at intersection points in two parts. */ 1576 PathList split (Path path) { 1577 Path new_path; 1578 PathList pl; 1579 int i; 1580 1581 if (!has_intersection_points (path)) { 1582 add_self_intersection_points (path); 1583 } else { 1584 warning ("points already created."); 1585 } 1586 1587 foreach (EditPoint p in path.points) { 1588 if (insides (p, path) == 1) { 1589 path.set_new_start (p); 1590 path.close (); 1591 break; 1592 } 1593 } 1594 1595 i = mark_intersection_as_deleted (path); 1596 1597 if (!(i == 0 || i == 2)) { 1598 warning (@"Split should only create two parts, $i points will be deleted."); 1599 } 1600 1601 new_path = path.copy (); 1602 pl = get_remaining_points (new_path); 1603 1604 return pl; 1605 } 1606 1607 PathList process_deleted_control_points (Path path) { 1608 PathList paths, nl, pl, rl, result; 1609 1610 paths = new PathList (); 1611 rl = new PathList (); 1612 pl = new PathList (); 1613 nl = new PathList (); 1614 1615 if (!path.has_deleted_point ()) { 1616 return pl; 1617 } 1618 1619 pl.add (path); 1620 1621 foreach (Path p in pl.paths) { 1622 nl = p.process_deleted_points (); 1623 1624 if (nl.paths.size > 0) { 1625 rl.append (nl); 1626 rl.paths.remove (p); 1627 } 1628 } 1629 1630 result = new PathList (); 1631 foreach (Path p in rl.paths) { 1632 pl = process_deleted_control_points (p); 1633 1634 if (pl.paths.size > 0) { 1635 result.append (pl); 1636 } else { 1637 result.add (p); 1638 } 1639 } 1640 1641 for (int i = 1; i < result.paths.size; i++) { 1642 result.paths.get (i).reverse (); 1643 } 1644 1645 paths.append (result); 1646 1647 return paths; 1648 } 1649 1650 PathList get_remaining_points (Path old_path) { 1651 PathList pl; 1652 1653 old_path.close (); 1654 pl = process_deleted_control_points (old_path); 1655 1656 if (pl.paths.size == 0) { 1657 pl.add (old_path); 1658 } 1659 1660 foreach (Path pn in pl.paths) { 1661 pn.close (); 1662 } 1663 1664 return pl; 1665 } 1666 1667 bool has_self_intersection (Path path) { 1668 bool intersection = false; 1669 1670 path.all_segments ((ep1, ep2) => { 1671 double ix, iy; 1672 EditPoint p1, p2; 1673 1674 if (segment_intersects (path, ep1, ep2, out ix, out iy, out p1, out p2, true)) { 1675 intersection = true; 1676 return false; 1677 } 1678 1679 return true; 1680 }); 1681 1682 return intersection; 1683 } 1684 1685 bool add_self_intersection_points (Path path, bool only_offsets = false) { 1686 bool intersection = false; 1687 1688 path.all_segments ((ep1, ep2) => { 1689 double ix, iy; 1690 EditPoint p1, p2; 1691 1692 if (segment_intersects (path, ep1, ep2, out ix, out iy, out p1, out p2, true, only_offsets)) { 1693 add_intersection (path, ep1, ep2, ix, iy); 1694 add_intersection (path, p1, p2, ix, iy); 1695 1696 intersection = true; 1697 return false; 1698 } 1699 1700 return true; 1701 }); 1702 1703 return intersection; 1704 } 1705 1706 EditPoint add_intersection (Path path, EditPoint prev, EditPoint next, double px, double py, Color? c = null) { 1707 Gee.ArrayList<EditPoint> n = new Gee.ArrayList<EditPoint> (); 1708 EditPoint ep1 = new EditPoint (); 1709 EditPoint ep2 = new EditPoint (); 1710 EditPoint ep3 = new EditPoint (); 1711 double d; 1712 1713 ep1.flags |= EditPoint.NEW_CORNER | EditPoint.SPLIT_POINT; 1714 ep1.type = prev.type; 1715 ep1.x = px; 1716 ep1.y = py; 1717 ep1.color = c; 1718 n.add (ep1); 1719 1720 ep2.flags |= EditPoint.INTERSECTION | EditPoint.SPLIT_POINT; 1721 ep2.type = prev.type; 1722 ep2.x = px; 1723 ep2.y = py; 1724 ep2.color = c; 1725 n.add (ep2); 1726 1727 ep3.flags |= EditPoint.NEW_CORNER | EditPoint.SPLIT_POINT; 1728 ep3.type = prev.type; 1729 ep3.x = px; 1730 ep3.y = py; 1731 ep3.color = c; 1732 n.add (ep3); 1733 1734 next.get_left_handle ().convert_to_line (); 1735 1736 foreach (EditPoint np in n) { 1737 np = path.add_point_after (np, np.get_prev ()); 1738 path.create_list (); 1739 } 1740 1741 PenTool.convert_point_to_line (ep1, true); 1742 PenTool.convert_point_to_line (ep2, true); 1743 PenTool.convert_point_to_line (ep3, true); 1744 1745 ep1.recalculate_linear_handles (); 1746 ep2.recalculate_linear_handles (); 1747 ep3.recalculate_linear_handles (); 1748 1749 d = Path.distance_to_point (prev, next); 1750 prev.get_right_handle ().length *= Path.distance_to_point (prev, ep1) / d; 1751 next.get_left_handle ().length *= Path.distance_to_point (ep3, next) / d; 1752 1753 next.recalculate_linear_handles (); 1754 1755 return ep2; 1756 } 1757 1758 bool segments_intersects (EditPoint p1, EditPoint p2, EditPoint ep, EditPoint next, 1759 out double ix, out double iy, 1760 bool skip_points_on_points = false) { 1761 double cross_x, cross_y; 1762 1763 ix = 0; 1764 iy = 0; 1765 1766 if (is_line (ep.x, ep.y, p1.x, p1.y, next.x, next.y)) { 1767 ix = p1.x; 1768 iy = p1.y; 1769 return true; 1770 } 1771 1772 if (is_line (ep.x, ep.y, p2.x, p2.y, next.x, next.y)) { 1773 ix = p2.x; 1774 iy = p2.y; 1775 return true; 1776 } 1777 1778 if (is_line (p1.x, p1.y, ep.x, ep.y, p2.x, p2.y)) { 1779 ix = ep.x; 1780 iy = ep.y; 1781 return true; 1782 } 1783 1784 if (is_line (p1.x, p1.y, next.x, next.y, p2.x, p2.y)) { 1785 ix = next.x; 1786 iy = next.y; 1787 return true; 1788 } 1789 1790 Path.find_intersection_point (ep, next, p1, p2, out cross_x, out cross_y); 1791 1792 if (fmin (ep.x, next.x) - 0.00001 <= cross_x <= fmax (ep.x, next.x) + 0.00001 1793 && fmin (ep.y, next.y) - 0.00001 <= cross_y <= fmax (ep.y, next.y) + 0.00001) { 1794 // iterate to find intersection. 1795 if (is_line (ep.x, ep.y, cross_x, cross_y, next.x, next.y) 1796 && is_line (p1.x, p1.y, cross_x, cross_y, p2.x, p2.y)) { 1797 1798 ix = cross_x; 1799 iy = cross_y; 1800 1801 return true; 1802 } 1803 } 1804 1805 return false; 1806 } 1807 1808 bool segment_intersects (Path path, EditPoint ep, EditPoint next, 1809 out double ix, out double iy, 1810 out EditPoint ia, out EditPoint ib, 1811 bool skip_points_on_points = false, 1812 bool only_offsets = false) { 1813 1814 EditPoint p1, p2; 1815 bool intersection, inter; 1816 double iix, iiy; 1817 1818 double distance, min_distance; 1819 1820 intersection = false; 1821 1822 ix = 0; 1823 iy = 0; 1824 1825 iix = 0; 1826 iiy = 0; 1827 1828 ia = new EditPoint (); 1829 ib = new EditPoint (); 1830 1831 if (path.points.size == 0) { 1832 return false; 1833 } 1834 1835 min_distance = double.MAX; 1836 p1 = path.points.get (path.points.size - 1); 1837 for (int i = 0; i < path.points.size; i++) { 1838 p2 = path.points.get (i); 1839 1840 bool is_offset = ((p1.flags & EditPoint.STROKE_OFFSET) > 0) 1841 && ((p2.flags & EditPoint.STROKE_OFFSET) > 0) 1842 && ((ep.flags & EditPoint.STROKE_OFFSET) > 0) 1843 && ((next.flags & EditPoint.STROKE_OFFSET) > 0); 1844 1845 if (!only_offsets || is_offset) { 1846 if (p1 != ep && p2 != next) { 1847 inter = segments_intersects (p1, p2, ep, next, out iix, out iiy, 1848 skip_points_on_points); 1849 1850 if (inter) { 1851 distance = Path.distance (ep.x, iix, ep.y, iiy); 1852 if (distance < min_distance) { 1853 ia = p1; 1854 ib = p2; 1855 ix = iix; 1856 iy = iiy; 1857 intersection = true; 1858 min_distance = distance; 1859 } 1860 } 1861 } 1862 } 1863 1864 p1 = p2; 1865 } 1866 1867 return intersection; 1868 } 1869 1870 /** @return true if p2 is on the line p1 to p3 */ 1871 bool is_line (double x1, double y1, double x2, double y2, double x3, double y3, double tolerance = 0.01) { 1872 return fmin (x1, x3) - 0.00001 <= x2 && x2 <= fmax (x1, x3) + 0.00001 1873 && fmin (y1, y3) - 0.00001 <= y2 && y2 <= fmax (y1, y3) + 0.00001 1874 && is_flat (x1, y1, x2, y2, x3, y3, tolerance); 1875 } 1876 1877 public static bool is_flat (double x1, double y1, double x2, double y2, double x3, double y3, double tolerance = 0.001) { 1878 double ds = Path.distance (x1, x3, y1, y3); 1879 double d1 = Path.distance (x1, x2, y1, y2); 1880 double d2 = Path.distance (x2, x3, y2, y3); 1881 double p = d1 / ds; 1882 double x = fabs ((x3 - x1) * p - (x2 - x1)) / ds; 1883 double y = fabs ((y3 - y1) * p - (y2 - y1)) / ds; 1884 double d = fabs (ds - (d1 + d2)) / ds; 1885 1886 return ds > 0.001 && d1 > 0.001 && d2 > 0.001 1887 && d < tolerance && x < tolerance && y < tolerance; 1888 } 1889 1890 // indside becomes outside in some paths 1891 void remove_points_in_stroke (PathList pl) { 1892 PathList r; 1893 1894 foreach (Path p in pl.paths) { 1895 if (remove_points_in_stroke_for_path (p, pl, out r)) { 1896 pl.append (r); 1897 remove_points_in_stroke (pl); 1898 return; 1899 } 1900 } 1901 } 1902 1903 bool remove_points_in_stroke_for_path (Path p, PathList pl, out PathList result) { 1904 EditPoint start_ep; 1905 EditPoint start_next; 1906 EditPoint start_prev; 1907 EditPoint end_ep = new EditPoint (); 1908 EditPoint end_next; 1909 EditPoint end_prev; 1910 1911 result = new PathList (); 1912 1913 for (int i = 1; i < p.points.size + 1; i++) { 1914 start_prev = p.points.get ((i - 1) % p.points.size); 1915 start_ep = p.points.get (i % p.points.size); 1916 start_next = p.points.get ((i + 1) % p.points.size); 1917 1918 if ((start_ep.flags & EditPoint.COUNTER_TO_OUTLINE) > 0) { 1919 for (int j = i; j < p.points.size + i; j++) { 1920 end_prev = p.points.get ((j - 1) % p.points.size); 1921 end_ep = p.points.get (j % p.points.size); 1922 end_next = p.points.get ((j + 1) % p.points.size); 1923 1924 1925 if ((end_ep.flags & EditPoint.COUNTER_TO_OUTLINE) > 0) { 1926 start_ep.flags = EditPoint.NONE; 1927 end_ep.flags = EditPoint.NONE; 1928 1929 if (merge_segments (pl, p, start_prev, start_ep, end_ep, end_next, out result)) { 1930 return true; 1931 } 1932 } 1933 } 1934 } 1935 1936 start_ep.flags = EditPoint.NONE; 1937 end_ep.flags = EditPoint.NONE; 1938 } 1939 1940 return false; 1941 } 1942 1943 bool merge_segments (PathList pl, 1944 Path path1, EditPoint start1, EditPoint stop1, 1945 EditPoint start2, EditPoint stop2, 1946 out PathList result) { 1947 1948 result = new PathList (); 1949 1950 PathList r1; 1951 PathList r2; 1952 1953 foreach (Path path2 in pl.paths) { 1954 if (path2 != path1) { 1955 reset_intersections (path1); 1956 reset_intersections (path2); 1957 1958 if (add_merge_intersection_point (path1, path2, start1, stop1)) { 1959 if (add_merge_intersection_point (path1, path2, start2, stop2)) { 1960 1961 r1 = get_remaining_points (path1.copy ()); 1962 r2 = get_remaining_points (path2.copy ()); 1963 1964 if (r1.paths.size != 2) { 1965 warning (@"Expecting two paths in r1 found $(r1.paths.size)\n"); 1966 reset_intersections (path1); 1967 reset_intersections (path2); 1968 return true; 1969 } 1970 1971 if (r2.paths.size != 2) { 1972 warning (@"Expecting two paths in r2 found $(r2.paths.size)\n"); 1973 reset_intersections (path1); 1974 reset_intersections (path2); 1975 return true; 1976 } 1977 1978 pl.paths.remove (path1); 1979 pl.paths.remove (path2); 1980 1981 double d1 = Path.point_distance (r1.paths.get (0).get_first_point (), 1982 r2.paths.get (0).get_first_point ()); 1983 1984 double d2 = Path.point_distance (r1.paths.get (0).get_first_point (), 1985 r2.paths.get (1).get_first_point ()); 1986 1987 Path m1, m2; 1988 1989 if (d1 > d2) { 1990 m1 = PenTool.merge_open_paths (r1.paths.get (0), r2.paths.get (0)); 1991 m2 = PenTool.merge_open_paths (r1.paths.get (1), r2.paths.get (1)); 1992 } else { 1993 m1 = PenTool.merge_open_paths (r1.paths.get (1), r2.paths.get (0)); 1994 m2 = PenTool.merge_open_paths (r1.paths.get (0), r2.paths.get (1)); 1995 } 1996 1997 result.add (m1); 1998 result.add (m2); 1999 2000 return true; 2001 } else { 2002 reset_intersections (path1); 2003 reset_intersections (path2); 2004 } 2005 } else { 2006 reset_intersections (path1); 2007 reset_intersections (path2); 2008 } 2009 } 2010 } 2011 2012 return false; 2013 } 2014 2015 void reset_intersections (Path p) { 2016 foreach (EditPoint ep in p.points) { 2017 ep.flags &= uint.MAX ^ EditPoint.INTERSECTION; 2018 ep.flags &= uint.MAX ^ EditPoint.COPIED; 2019 ep.flags &= uint.MAX ^ EditPoint.SELF_INTERSECTION; 2020 ep.deleted = false; 2021 } 2022 p.remove_points_on_points (); 2023 } 2024 2025 bool add_merge_intersection_point (Path path1, Path path2, EditPoint first, EditPoint next) { 2026 double ix, iy; 2027 bool intersection; 2028 2029 intersection = false; 2030 ix = 0; 2031 iy = 0; 2032 path2.all_segments ((p1, p2) => { 2033 int i; 2034 2035 intersection = segments_intersects (first, next, p1, p2, out ix, out iy); 2036 2037 if (intersection) { 2038 add_intersection (path1, first, next, ix, iy); 2039 add_intersection (path2, p1, p2, ix, iy); 2040 2041 i = mark_intersection_as_deleted (path1); 2042 i = mark_intersection_as_deleted (path2); 2043 } 2044 2045 return !intersection; 2046 }); 2047 2048 return intersection; 2049 } 2050 2051 bool is_inside_of_path (PointSelection ps, PathList pl, out Path outline) { 2052 outline = new Path (); 2053 foreach (Path p in pl.paths) { 2054 if (p != ps.path) { 2055 if (is_inside (ps.point, p)) { 2056 outline = p; 2057 return true; 2058 } 2059 } 2060 } 2061 2062 return false; 2063 } 2064 2065 PathList get_all_parts (PathList pl) { 2066 PathList m; 2067 bool intersects = false; 2068 PathList r = new PathList (); 2069 2070 foreach (Path p in pl.paths) { 2071 if (has_self_intersection (p)) { 2072 m = get_parts (p); 2073 r.append (m); 2074 intersects = true; 2075 } else { 2076 r.add (p); 2077 } 2078 } 2079 2080 foreach (Path p in r.paths) { 2081 p.close (); 2082 p.update_region_boundaries (); 2083 } 2084 2085 if (intersects) { 2086 return get_all_parts (r); 2087 } 2088 2089 return r; 2090 } 2091 2092 void remove_single_points (PathList pl) { 2093 PathList r = new PathList (); 2094 2095 foreach (Path p in pl.paths) { 2096 p.update_region_boundaries (); 2097 if (p.points.size < 10 2098 || p.xmax - p.xmin < 0.01 2099 || p.ymax - p.ymin < 0.01) { 2100 2101 r.add (p); 2102 } 2103 } 2104 2105 foreach (Path p in r.paths) { 2106 pl.remove (p); 2107 } 2108 } 2109 2110 public PathList merge (PathList pl) { 2111 bool error = false; 2112 PathList m; 2113 PathList r = pl; 2114 Path p1, p2; 2115 2116 r = get_all_parts (r); 2117 remove_single_points (r); 2118 2119 while (paths_has_intersection (r, out p1, out p2)) { 2120 if (task.is_cancelled ()) { 2121 return new PathList (); 2122 } 2123 2124 if (merge_path (p1, p2, out m, out error)) { 2125 r.paths.remove (p1); 2126 r.paths.remove (p2); 2127 2128 foreach (Path np in m.paths) { 2129 np.remove_points_on_points (); 2130 r.add (np); 2131 } 2132 2133 if (task.is_cancelled ()) { 2134 return new PathList (); 2135 } 2136 2137 r = get_all_parts (r); 2138 remove_single_points (r); 2139 2140 if (paths_has_intersection (m, out p1, out p2)) { 2141 warning ("Paths are not merged."); 2142 error = true; 2143 } 2144 } else { 2145 warning ("Not merged."); 2146 error = true; 2147 } 2148 2149 if (error) { 2150 warning ("Merge error"); 2151 break; 2152 } 2153 } 2154 2155 if (!error) { 2156 remove_merged_parts (r); 2157 } 2158 2159 foreach (Path p in r.paths) { 2160 p.close (); 2161 p.recalculate_linear_handles (); 2162 } 2163 2164 if (task.is_cancelled ()) { 2165 return new PathList (); 2166 } 2167 2168 return r; 2169 } 2170 2171 void remove_merged_parts (PathList r) { 2172 Gee.ArrayList<Path> remove = new Gee.ArrayList<Path> (); 2173 int c; 2174 2175 foreach (Path p in r.paths) { 2176 p.update_region_boundaries (); 2177 } 2178 2179 foreach (Path p in r.paths) { 2180 c = counters (r, p); 2181 2182 if (c % 2 == 0) { 2183 if (!p.is_clockwise ()) { 2184 remove.add (p); 2185 } 2186 } else { 2187 if (p.is_clockwise ()) { 2188 remove.add (p); 2189 } 2190 } 2191 } 2192 2193 foreach (Path p in remove) { 2194 r.paths.remove (p); 2195 } 2196 } 2197 2198 public PathList get_insides (PathList pl, Path path) { 2199 bool inside = false; 2200 PathList insides = new PathList (); 2201 2202 foreach (Path p in pl.paths) { 2203 if (p.points.size > 1 2204 && p != path 2205 && path.boundaries_intersecting (p)) { 2206 2207 inside = true; 2208 foreach (EditPoint ep in path.points) { 2209 if (!is_inside (ep, p)) { 2210 inside = false; 2211 break; 2212 } 2213 } 2214 2215 if (inside) { 2216 insides.add (p); // add the flat inside to the list 2217 } 2218 } 2219 } 2220 2221 return insides; 2222 } 2223 2224 public int counters (PathList pl, Path path) { 2225 int inside_count = 0; 2226 bool inside; 2227 2228 foreach (Path p in pl.paths) { 2229 inside = true; 2230 2231 if (p.points.size > 1 2232 && p != path 2233 && path.boundaries_intersecting (p)) { 2234 2235 foreach (EditPoint ep in path.points) { 2236 if (!is_inside (ep, p)) { 2237 inside = false; 2238 break; 2239 } 2240 } 2241 2242 if (inside) { 2243 inside_count++; 2244 } 2245 } 2246 } 2247 2248 return inside_count; 2249 } 2250 2251 public static bool is_inside (EditPoint point, Path path) { 2252 EditPoint prev; 2253 bool inside = false; 2254 2255 if (path.points.size <= 1) { 2256 return false; 2257 } 2258 2259 prev = path.points.get (path.points.size - 1); 2260 2261 foreach (EditPoint p in path.points) { 2262 if ((fabs (p.x - point.x) < 0.1 && fabs (p.y - point.y) < 0.1) 2263 || (fabs (prev.x - point.x) < 0.1 && fabs (prev.y - point.y) < 0.1)) { 2264 return true; 2265 } else if ((p.y > point.y) != (prev.y > point.y) 2266 && point.x < (prev.x - p.x) * (point.y - p.y) / (prev.y - p.y) + p.x) { 2267 inside = !inside; 2268 } 2269 2270 prev = p; 2271 } 2272 2273 return inside; 2274 } 2275 2276 public int insides (EditPoint point, Path path) { 2277 EditPoint prev; 2278 int inside = 0; 2279 2280 if (path.points.size <= 1) { 2281 return 0; 2282 } 2283 2284 prev = path.get_last_point (); 2285 2286 foreach (EditPoint start in path.points) { 2287 if (start.x == point.x && point.y == start.y) { 2288 inside++; 2289 } else if ((start.y > point.y) != (prev.y > point.y) 2290 && point.x < (prev.x - start.x) * (point.y - start.y) / (prev.y - start.y) + start.x) { 2291 inside++; 2292 } 2293 2294 prev = start; 2295 } 2296 2297 return inside; 2298 } 2299 2300 bool merge_path (Path path1, Path path2, out PathList merged_paths, out bool error) { 2301 IntersectionList intersections; 2302 EditPoint ep1, next, p1, p2, pp1, pp2; 2303 Path path, other, merged; 2304 PathList r, other_paths, result; 2305 bool intersects; 2306 int s, i; 2307 double ix, iy, iix, iiy; 2308 bool merge = false; 2309 EditPoint intersection_point, other_intersection_point; 2310 bool path1_direction, path2_direction; 2311 2312 error = false; 2313 merged_paths = new PathList (); 2314 intersections = new IntersectionList (); 2315 2316 if (path1.points.size == 0) { 2317 return false; 2318 } 2319 2320 if (path2.points.size == 0) { 2321 return false; 2322 } 2323 2324 reset_intersections (path1); 2325 reset_intersections (path2); 2326 2327 iix = 0; 2328 iiy = 0; 2329 2330 result = new PathList (); 2331 2332 if (path1.points.size == 0 || path2.points.size == 0) { 2333 warning ("No points in path."); 2334 error = true; 2335 return false; 2336 } 2337 2338 if (!path1.boundaries_intersecting (path2)) { 2339 return false; 2340 } 2341 2342 Path original_path1 = path1.copy (); 2343 Path original_path2 = path2.copy (); 2344 2345 s = 0; 2346 foreach (EditPoint e in original_path1.points) { 2347 if (!is_inside (e, original_path2) 2348 && insides (e, original_path1) == 1) { // FIXME: later as well 2349 break; 2350 } 2351 s++; 2352 } 2353 2354 if (s >= path1.points.size) { 2355 Path t; 2356 t = original_path1; 2357 original_path1 = original_path2; 2358 original_path2 = t; 2359 s = 0; 2360 foreach (EditPoint e in original_path1.points) { 2361 if (!is_inside (e, original_path2)) { 2362 break; 2363 } 2364 s++; 2365 } 2366 } 2367 2368 if (s >= original_path1.points.size) { 2369 warning ("No start point found."); 2370 error = true; 2371 return false; 2372 } 2373 2374 path = original_path1; 2375 other = original_path2; 2376 2377 other_paths = new PathList (); 2378 r = new PathList (); 2379 other_paths.add (path); 2380 other_paths.add (other); 2381 intersects = false; 2382 p1 = new EditPoint (); 2383 p2 = new EditPoint (); 2384 pp1 = new EditPoint (); 2385 pp2 = new EditPoint (); 2386 2387 ix = 0; 2388 iy = 0; 2389 i = s; 2390 merged = new Path (); 2391 2392 path1_direction = is_clockwise (original_path1); 2393 path2_direction = is_clockwise (original_path1); 2394 2395 while (true) { 2396 ep1 = path.points.get (i % path.points.size); 2397 next = path.points.get ((i + 1) % path.points.size); 2398 2399 if ((ep1.flags & EditPoint.COPIED) > 0) { 2400 if (merge) { 2401 merged.close (); 2402 result.add (merged.copy ()); 2403 } 2404 2405 merged = new Path (); 2406 2407 bool find_parts = false; 2408 Intersection new_start = new Intersection.empty (); 2409 foreach (Intersection inter in intersections.points) { 2410 if (!inter.done && !find_parts) { 2411 find_parts = true; 2412 new_start = inter; 2413 break; 2414 } 2415 } 2416 2417 if (task.is_cancelled ()) { 2418 return false; 2419 } 2420 2421 if (!find_parts) { 2422 break; // done, no more parts to merge 2423 } else { 2424 // set start point for next part 2425 path = new_start.other_path; 2426 2427 if (path.points.size == 0) { 2428 warning ("No points."); 2429 error = true; 2430 return false; 2431 } 2432 2433 i = index_of (path, new_start.get_point (path)); 2434 2435 if (i < 0) { 2436 warning ("Start point not found."); 2437 error = true; 2438 return false; 2439 } 2440 2441 ep1 = path.points.get (i % path.points.size); 2442 next = path.points.get ((i + 1) % path.points.size); 2443 2444 if ((ep1.flags & EditPoint.INTERSECTION) == 0) { 2445 warning ("Not starting on an intersection point."); 2446 error = true; 2447 return false; 2448 } 2449 2450 new_start.done = true; 2451 } 2452 } 2453 2454 intersects = false; 2455 2456 double dm; 2457 double d; 2458 2459 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 2460 Intersection current_intersection; 2461 bool continue_on_other_path; 2462 current_intersection = intersections.get_point (ep1, out continue_on_other_path); 2463 current_intersection.done = true; 2464 2465 // take the other part of an intersection 2466 if ((next.flags & EditPoint.COPIED) != 0) { 2467 bool next_is_intersection = false; 2468 bool next_continue_on_other_path; 2469 2470 next_is_intersection = ((next.flags & EditPoint.INTERSECTION) > 0); 2471 2472 if (next_is_intersection) { 2473 Intersection next_intersection = intersections.get_point (next, out next_continue_on_other_path); 2474 2475 ep1.flags |= EditPoint.COPIED; 2476 merged.add_point (ep1.copy ()); 2477 2478 if (!next_intersection.done) { 2479 EditPoint new_start_point; 2480 2481 path = next_continue_on_other_path 2482 ? next_intersection.other_path : next_intersection.path; 2483 2484 new_start_point = next_continue_on_other_path 2485 ? next_intersection.other_point : next_intersection.point; 2486 2487 i = index_of (path, new_start_point); 2488 2489 if (i < 0) { 2490 warning ("Point not found in path.\n"); 2491 error = true; 2492 return false; 2493 } 2494 2495 ep1 = path.points.get (i % path.points.size); 2496 next = path.points.get ((i + 1) % path.points.size); 2497 } else { 2498 warning ("Part is already created.\n"); 2499 error = true; 2500 return false; 2501 } 2502 } else { 2503 ep1.flags |= EditPoint.COPIED; 2504 merged.add_point (ep1.copy ()); 2505 2506 EditPoint new_start_point; 2507 2508 if ((next.flags & EditPoint.COPIED) > 0) { 2509 path = current_intersection.get_other_path (path); 2510 new_start_point = current_intersection.get_point (path); 2511 i = index_of (path, new_start_point); 2512 2513 if (i < 0) { 2514 warning ("Point not found in path."); 2515 error = true; 2516 return false; 2517 } 2518 2519 ep1 = path.points.get (i % path.points.size); 2520 next = path.points.get ((i + 1) % path.points.size); 2521 2522 if ((next.flags & EditPoint.INTERSECTION) > 0) { 2523 warning ("Wrong type."); 2524 error = true; 2525 return false; 2526 } 2527 2528 if ((next.flags & EditPoint.COPIED) > 0) { 2529 merged.add_point (ep1.copy ()); 2530 continue; 2531 } 2532 } else { 2533 ep1.flags |= EditPoint.COPIED; 2534 merged.add_point (ep1.copy ()); 2535 } 2536 } 2537 } else { 2538 ep1.flags |= EditPoint.COPIED; 2539 2540 if (path1_direction == path2_direction) { 2541 if (!is_inside (ep1, original_path1)) { 2542 merged.add_point (ep1.copy ()); 2543 } 2544 } else { 2545 merged.add_point (ep1.copy ()); 2546 } 2547 } 2548 2549 current_intersection.done = true; 2550 } else { 2551 // find new intersection 2552 dm = double.MAX; 2553 foreach (Path o in other_paths.paths) { 2554 bool inter = segment_intersects (o, ep1, next, out iix, out iiy, 2555 out pp1, out pp2, false, false); 2556 d = Path.distance (ep1.x, iix, ep1.y, iiy); 2557 if (d < dm && inter) { 2558 other = o; 2559 dm = d; 2560 intersects = true; 2561 p1 = pp1; 2562 p2 = pp2; 2563 ix = iix; 2564 iy = iiy; 2565 } 2566 2567 if (d < 0.0001) { 2568 intersects = false; 2569 } 2570 } 2571 2572 if (intersects) { 2573 merged.add_point (ep1.copy ()); 2574 ep1.flags |= EditPoint.COPIED; 2575 2576 intersection_point = add_intersection (path, ep1, next, ix, iy); 2577 other_intersection_point = add_intersection (other, p1, p2, ix, iy); 2578 2579 bool g = false; 2580 foreach (Intersection old_intersection in intersections.points) { 2581 if (old_intersection.point == intersection_point 2582 || old_intersection.other_point == other_intersection_point) { 2583 old_intersection.done = true; 2584 g = true; 2585 } 2586 } 2587 2588 if (!g) { 2589 Intersection ip = new Intersection (intersection_point, path, other_intersection_point, other); 2590 intersections.points.add (ip); 2591 } 2592 2593 merged.add_point (intersection_point.copy ()); 2594 intersection_point.flags |= EditPoint.COPIED; 2595 2596 i = index_of (other, other_intersection_point); 2597 2598 if (i < 0) { 2599 warning (@"Point not found ($i)."); 2600 break; 2601 } 2602 2603 path = other; 2604 merge = true; 2605 } else { 2606 ep1.flags |= EditPoint.COPIED; 2607 merged.add_point (ep1.copy ()); 2608 2609 PointSelection ps; 2610 Path outline; 2611 2612 // remove points inside of path 2613 if (is_clockwise (original_path2)) { 2614 ps = new PointSelection (ep1, merged); 2615 if (is_inside_of_path (ps, result, out outline)) { 2616 ep1.deleted = true; 2617 } 2618 } 2619 } 2620 } 2621 2622 i++; 2623 } 2624 2625 if (merge) { 2626 original_path1.remove_points_on_points (); 2627 original_path2.remove_points_on_points (); 2628 original_path1.remove_deleted_points (); 2629 original_path2.remove_deleted_points (); 2630 2631 foreach (Path np in result.paths) { 2632 Path p = np.copy (); 2633 bool has_direction = true; 2634 2635 p.remove_points_on_points (); 2636 2637 if (has_direction) { 2638 p.close (); 2639 reset_intersections (p); 2640 merged_paths.append (get_parts (p)); 2641 p.update_region_boundaries (); 2642 p.recalculate_linear_handles (); 2643 } 2644 } 2645 } 2646 2647 return merge && !error; 2648 } 2649 2650 int index_of (Path p, EditPoint ep) { 2651 int i = 0; 2652 foreach (EditPoint e in p.points) { 2653 if (e == ep) { 2654 return i; 2655 } 2656 i++; 2657 } 2658 2659 return -1; 2660 } 2661 2662 public int counters_in_point_in_path (Path p, EditPoint ep) { 2663 int inside_count = 0; 2664 bool inside; 2665 2666 if (p.points.size > 1) { 2667 inside = true; 2668 2669 if (!is_inside (ep, p)) { 2670 inside = false; 2671 } 2672 2673 if (inside) { 2674 inside_count++; 2675 } 2676 } 2677 2678 return inside_count; 2679 } 2680 2681 int mark_intersection_as_deleted (Path path) { 2682 int i = 0; 2683 2684 foreach (EditPoint p in path.points) { 2685 if ((p.flags & EditPoint.INTERSECTION) > 0) { 2686 p.deleted = true; 2687 i++; 2688 } 2689 } 2690 2691 return i; 2692 } 2693 2694 /** @param n number of interrsections to find per path. */ 2695 bool has_intersection (Path path1, Path path2) { 2696 bool intersection = false; 2697 2698 if (!path1.boundaries_intersecting (path2)) { 2699 return false; 2700 } 2701 2702 path1.all_segments ((ep1, ep2) => { 2703 double ix, iy; 2704 EditPoint p1, p2; 2705 bool i; 2706 2707 i = segment_intersects (path2, ep1, ep2, out ix, out iy, 2708 out p1, out p2, true); 2709 2710 if (i) { 2711 intersection = true; 2712 } 2713 2714 return !intersection; 2715 }); 2716 2717 return intersection; 2718 } 2719 2720 bool paths_has_intersection (PathList r, out Path path1, out Path path2) { 2721 int i, j; 2722 path1 = new Path (); 2723 path2 = new Path (); 2724 2725 i = 0; 2726 foreach (Path p1 in r.paths) { 2727 2728 j = 0; 2729 foreach (Path p2 in r.paths) { 2730 if (p1 != p2) { 2731 if (has_intersection (p1, p2)) { 2732 path1 = p1; 2733 path2 = p2; 2734 return true; 2735 } 2736 } 2737 j++; 2738 } 2739 i++; 2740 } 2741 return false; 2742 } 2743 2744 public bool has_points_outside (PathList pl, Path p) { 2745 if (pl.paths.size != 2) { 2746 warning ("Stroke should only create two parts."); 2747 return false; 2748 } 2749 2750 foreach (Path path in pl.paths) { 2751 if (path != p) { 2752 foreach (EditPoint ep in p.points) { 2753 if (!is_inside (ep, path)) { 2754 return true; 2755 } 2756 } 2757 } 2758 } 2759 2760 return false; 2761 } 2762 2763 bool is_clockwise (Path p) { 2764 double sum = 0; 2765 EditPoint p1, p2; 2766 2767 EditPointHandle l, r; 2768 2769 p.recalculate_linear_handles (); 2770 2771 if (p.points.size < 3) { 2772 return true; 2773 } 2774 2775 for (int i = 0; i < p.points.size; i++) { 2776 p1 = p.points.get (i); 2777 p2 = p.points.get ((i + 1) % p.points.size); 2778 2779 l = p1.get_left_handle (); 2780 r = p1.get_right_handle (); 2781 if (!(fabs (l.angle - r.angle) < 0.0001 && l.length > 0.01 && r.length > 0.01)) { 2782 sum += (p2.x - p1.x) * (p2.y + p1.y); 2783 } 2784 } 2785 2786 return sum > 0; 2787 } 2788 2789 public PathList create_stroke (Path original_path, double thickness) { 2790 2791 PathList pl; 2792 EditPoint p1, p2, p3; 2793 EditPoint previous, previous_inside, start, start_inside; 2794 2795 Path side1, side2; 2796 2797 double x, y, x2, y2, x3, y3; 2798 int size; 2799 bool flat, f_next, f_bigger; 2800 int i; 2801 2802 double tolerance; 2803 double step_increment; 2804 double step_size; 2805 EditPoint corner1, corner1_inside; 2806 double step; 2807 double min_increment; 2808 2809 EditPointHandle l, r; 2810 2811 Path path = original_path.copy (); 2812 2813 int keep; 2814 bool on_curve; 2815 2816 pl = new PathList (); 2817 size = path.is_open () ? path.points.size - 1 : path.points.size; 2818 2819 side1 = new Path (); 2820 side2 = new Path (); 2821 2822 foreach (EditPoint ph in path.points) { 2823 if (ph.type == PointType.HIDDEN) { 2824 ph.type = PointType.CUBIC; 2825 } 2826 } 2827 path.remove_deleted_points (); 2828 2829 if (path.points.size < 2) { 2830 return pl; 2831 } 2832 2833 previous = new EditPoint (); 2834 previous_inside = new EditPoint (); 2835 corner1 = new EditPoint (); 2836 corner1_inside = new EditPoint (); 2837 2838 if (path.is_open ()) { 2839 p1 = path.points.get (0); 2840 p2 = path.points.get (1 % path.points.size); 2841 2842 get_segment (thickness, 0, 0.00001, p1, p2, out start); 2843 get_segment (-thickness, 0, 0.00001, p1, p2, out start_inside); 2844 2845 previous = start.copy (); 2846 previous_inside = start_inside.copy (); 2847 2848 previous.flags |= EditPoint.CURVE_KEEP; 2849 previous_inside.flags |= EditPoint.CURVE_KEEP; 2850 2851 side1.add_point (previous); 2852 side2.add_point (previous_inside); 2853 } 2854 2855 min_increment = 0.02; // 0.013 2856 2857 for (i = 0; i < size; i++) { 2858 p1 = path.points.get (i % path.points.size); 2859 p2 = path.points.get ((i + 1) % path.points.size); 2860 p3 = path.points.get ((i + 2) % path.points.size); 2861 2862 if (unlikely (task.is_cancelled ())) { 2863 return new PathList (); 2864 } 2865 2866 tolerance = 0.01; 2867 step_increment = 1.05; 2868 step_size = 0.039; 2869 2870 corner1 = new EditPoint (); 2871 2872 if (p1.type == PointType.HIDDEN 2873 || p2.type == PointType.HIDDEN) { 2874 continue; 2875 } 2876 2877 get_segment (thickness, 0, 0.00001, p1, p2, out start); 2878 get_segment (-thickness, 0, 0.00001, p1, p2, out start_inside); 2879 2880 previous = start.copy (); 2881 previous_inside = start_inside.copy (); 2882 2883 previous.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2884 previous_inside.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2885 2886 side1.add_point (previous); 2887 side2.add_point (previous_inside); 2888 2889 step = step_size; 2890 keep = 0; 2891 step_size = 0.05; 2892 2893 while (step < 1 - 2 * step_size) { 2894 Path.get_point_for_step (p1, p2, step, out x, out y); 2895 Path.get_point_for_step (p1, p2, step + step_size, out x2, out y2); 2896 Path.get_point_for_step (p1, p2, step + 2 * step_size, out x3, out y3); 2897 2898 flat = is_flat (x, y, x2, y2, x3, y3, tolerance); 2899 2900 Path.get_point_for_step (p1, p2, step, out x, out y); 2901 Path.get_point_for_step (p1, p2, step + step_size / step_increment, out x2, out y2); 2902 Path.get_point_for_step (p1, p2, step + 2 * step_size / step_increment, out x3, out y3); 2903 2904 f_next = is_flat (x, y, x2, y2, x3, y3, tolerance); 2905 2906 Path.get_point_for_step (p1, p2, step, out x, out y); 2907 Path.get_point_for_step (p1, p2, step + step_size * step_increment, out x2, out y2); 2908 Path.get_point_for_step (p1, p2, step + 2 * step_size * step_increment, out x3, out y3); 2909 2910 f_bigger = is_flat (x, y, x2, y2, x3, y3, tolerance); 2911 2912 if (!flat && !f_next && step_size > min_increment) { 2913 step_size /= step_increment; 2914 continue; 2915 } 2916 2917 if (flat && f_bigger && step_size < 0.1) { 2918 step_size *= step_increment; 2919 continue; 2920 } 2921 2922 get_segment (thickness, step, step_size, p1, p2, out corner1); 2923 get_segment (-thickness, step, step_size, p1, p2, out corner1_inside); 2924 2925 previous.get_right_handle ().length *= step_size; 2926 corner1.get_left_handle ().length *= step_size; 2927 previous_inside.get_right_handle ().length *= step_size; 2928 corner1_inside.get_left_handle ().length *= step_size; 2929 2930 previous = corner1.copy (); 2931 previous_inside = corner1_inside.copy (); 2932 2933 if (keep == 0 && step > 0.3) { // keep two points per segment 2934 on_curve = true; 2935 keep++; 2936 } else if (keep == 1 && step > 0.6) { 2937 on_curve = true; 2938 keep++; 2939 } else { 2940 on_curve = false; 2941 } 2942 2943 if (!on_curve) { 2944 previous.flags |= EditPoint.CURVE; 2945 previous_inside.flags |= EditPoint.CURVE; 2946 } else { 2947 previous.flags |= EditPoint.CURVE_KEEP; 2948 previous_inside.flags |= EditPoint.CURVE_KEEP; 2949 } 2950 2951 side1.add_point (previous); 2952 side2.add_point (previous_inside); 2953 2954 step += step_size; 2955 } 2956 2957 previous.get_right_handle ().length *= step_size; 2958 corner1.get_left_handle ().length *= step_size; 2959 previous_inside.get_right_handle ().length *= step_size; 2960 corner1_inside.get_left_handle ().length *= step_size; 2961 2962 get_segment (thickness, 1 - 0.00001, 0.00001, p1, p2, out corner1); 2963 get_segment (-thickness, 1 - 0.00001, 0.00001, p1, p2, out corner1_inside); 2964 2965 previous = corner1.copy (); 2966 previous_inside = corner1_inside.copy (); 2967 2968 previous.get_right_handle ().length *= step_size; 2969 previous.get_left_handle ().length *= step_size; 2970 previous_inside.get_right_handle ().length *= step_size; 2971 previous_inside.get_left_handle ().length *= step_size; 2972 2973 previous.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2974 previous_inside.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2975 2976 side1.add_point (previous); 2977 side2.add_point (previous_inside); 2978 2979 l = p2.get_left_handle (); 2980 r = p2.get_right_handle (); 2981 2982 if (fabs ((l.angle + r.angle + PI) % (2 * PI) - PI) > 0.005) { 2983 if (!path.is_open () || i < size - 1) { 2984 get_segment (thickness, 0, 0.00001, p2, p3, out start); 2985 add_corner (side1, previous, start, p2.copy (), thickness); 2986 2987 get_segment (-thickness, 0, 0.00001, p2, p3, out start); 2988 add_corner (side2, previous_inside, start, p2.copy (), thickness); 2989 } 2990 } 2991 } 2992 2993 side1.remove_points_on_points (); 2994 side2.remove_points_on_points (); 2995 2996 convert_to_curve (side1); 2997 convert_to_curve (side2); 2998 2999 side2.reverse (); 3000 pl = merge_stroke_parts (path, side1, side2); 3001 3002 return pl; 3003 } 3004 3005 void convert_to_curve (Path path) { 3006 if (path.is_open ()) { 3007 path.get_first_point ().flags &= ~EditPoint.CURVE; 3008 path.get_last_point ().flags &= ~EditPoint.CURVE; 3009 } 3010 3011 path.recalculate_linear_handles (); 3012 3013 foreach (EditPoint ep in path.points) { 3014 if ((ep.flags & EditPoint.SEGMENT_END) == 0) { 3015 if ((ep.flags & EditPoint.CURVE) > 0 || (ep.flags & EditPoint.CURVE_KEEP) > 0) { 3016 ep.convert_to_curve (); 3017 } 3018 } 3019 } 3020 3021 if (task.is_cancelled ()) { 3022 return; 3023 } 3024 3025 foreach (EditPoint ep in path.points) { 3026 if ((ep.flags & EditPoint.SEGMENT_END) == 0) { 3027 if ((ep.flags & EditPoint.CURVE) > 0 || (ep.flags & EditPoint.CURVE_KEEP) > 0) { 3028 ep.set_tie_handle (true); 3029 } 3030 } 3031 } 3032 3033 if (task.is_cancelled ()) { 3034 return; 3035 } 3036 3037 foreach (EditPoint ep in path.points) { 3038 if ((ep.flags & EditPoint.SEGMENT_END) == 0) { 3039 if ((ep.flags & EditPoint.CURVE) > 0 || (ep.flags & EditPoint.CURVE_KEEP) > 0) { 3040 ep.process_tied_handle (); 3041 } 3042 } 3043 } 3044 } 3045 3046 public void get_segment (double stroke_thickness, double step, double step_size, 3047 EditPoint p1, EditPoint p2, out EditPoint ep1) { 3048 3049 double thickness = stroke_thickness / 2; 3050 Path overlay; 3051 double x, y, x2, y2, x3, y3; 3052 EditPoint corner1, corner2, corner3; 3053 PointType type; 3054 3055 Path.get_point_for_step (p1, p2, step, out x, out y); 3056 Path.get_point_for_step (p1, p2, step + step_size, out x2, out y2); 3057 Path.get_point_for_step (p1, p2, step + 2 * step_size, out x3, out y3); 3058 3059 overlay = new Path (); 3060 3061 type = p1.get_right_handle ().type; 3062 corner1 = new EditPoint (x, y, type); 3063 corner2 = new EditPoint (x2, y2, type); 3064 corner3 = new EditPoint (x3, y3, type); 3065 3066 corner2.convert_to_line (); 3067 3068 overlay.add_point (corner1); 3069 overlay.add_point (corner2); 3070 overlay.add_point (corner3); 3071 3072 overlay.close (); 3073 overlay.recalculate_linear_handles (); 3074 3075 move_segment (corner1, corner2, thickness); 3076 3077 ep1 = corner2; 3078 } 3079 3080 public PathList merge_stroke_parts (Path p, Path side1, Path side2) { 3081 Path merged = new Path (); 3082 PathList paths = new PathList (); 3083 3084 if (!p.is_open ()) { 3085 side1.update_region_boundaries (); 3086 paths.add (side1); 3087 side2.update_region_boundaries (); 3088 paths.add (side2); 3089 side1.close (); 3090 side2.close (); 3091 } else if (p.is_open ()) { 3092 side2.reverse (); 3093 merged = merge_strokes (p, side1, side2); 3094 merged.close (); 3095 merged.update_region_boundaries (); 3096 paths.add (merged); 3097 merged.reverse (); 3098 } else { 3099 warning ("Can not create stroke."); 3100 paths.add (p); 3101 } 3102 3103 return paths; 3104 } 3105 } 3106 3107 } 3108