summaryrefslogtreecommitdiff
path: root/lib/stitches/tartan_fill.py
blob: acac1ed155e3157f59196f3fd39ab138a0e041b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
# Authors: see git history
#
# Copyright (c) 2023 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

from collections import defaultdict
from itertools import chain
from math import cos, radians, sin
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

from networkx import is_empty
from shapely import get_point, line_merge, minimum_bounding_radius, segmentize
from shapely.affinity import rotate, scale, translate
from shapely.geometry import LineString, MultiLineString, Point, Polygon
from shapely.ops import nearest_points

from ..stitch_plan import Stitch, StitchGroup
from ..svg import PIXELS_PER_MM
from ..tartan.utils import (get_palette_width, get_tartan_settings,
                            get_tartan_stripes, sort_fills_and_strokes,
                            stripes_to_shapes)
from ..utils import cache, ensure_multi_line_string
from ..utils.threading import check_stop_flag
from .auto_fill import (build_fill_stitch_graph, build_travel_graph,
                        find_stitch_path, graph_make_valid)
from .circular_fill import path_to_stitches
from .guided_fill import apply_stitches
from .linear_gradient_fill import remove_start_end_travel
from .running_stitch import bean_stitch

if TYPE_CHECKING:
    from ..elements import FillStitch


def tartan_fill(fill: 'FillStitch', outline: Polygon, starting_point: Union[tuple, Stitch, None], ending_point: Union[tuple, Stitch, None]):
    """
    Main method to fill the tartan element with tartan fill stitches

    :param fill: FillStitch element
    :param outline: the outline of the fill
    :param starting_point: the starting point (or None)
    :param ending_point: the ending point (or None)
    :returns: stitch_groups forming the tartan pattern
    """
    tartan_settings = get_tartan_settings(fill.node)
    warp, weft = get_tartan_stripes(tartan_settings)
    warp_width = get_palette_width(tartan_settings)
    weft_width = get_palette_width(tartan_settings, 1)

    offset = (abs(tartan_settings['offset_x']), abs(tartan_settings['offset_y']))
    rotation = tartan_settings['rotate']
    dimensions = _get_dimensions(fill, outline, offset, warp_width, weft_width)
    rotation_center = _get_rotation_center(outline)

    warp_shapes = stripes_to_shapes(
        warp,
        dimensions,
        outline,
        rotation,
        rotation_center,
        tartan_settings['symmetry'],
        tartan_settings['scale'],
        tartan_settings['min_stripe_width'],
        False,  # weft
        False  # do not cut polygons just yet
    )

    weft_shapes = stripes_to_shapes(
        weft,
        dimensions,
        outline,
        rotation,
        rotation_center,
        tartan_settings['symmetry'],
        tartan_settings['scale'],
        tartan_settings['min_stripe_width'],
        True,  # weft
        False  # do not cut polygons just yet
    )

    if fill.herringbone_width > 0:
        lines = _generate_herringbone_lines(outline, fill, dimensions, rotation)
        warp_lines, weft_lines = _split_herringbone_warp_weft(lines, fill.rows_per_thread, fill.running_stitch_length)
        warp_color_lines = _get_herringbone_color_segments(warp_lines, warp_shapes, outline, rotation, fill.running_stitch_length)
        weft_color_lines = _get_herringbone_color_segments(weft_lines, weft_shapes, outline, rotation, fill.running_stitch_length, True)
    else:
        lines = _generate_tartan_lines(outline, fill, dimensions, rotation)
        warp_lines, weft_lines = _split_warp_weft(lines, fill.rows_per_thread)
        warp_color_lines = _get_tartan_color_segments(warp_lines, warp_shapes, outline, rotation, fill.running_stitch_length)
        weft_color_lines = _get_tartan_color_segments(weft_lines, weft_shapes, outline, rotation, fill.running_stitch_length, True)
    if not lines:
        return []

    warp_color_runs = _get_color_runs(warp_shapes, fill.running_stitch_length)
    weft_color_runs = _get_color_runs(weft_shapes, fill.max_stitch_length)

    color_lines = defaultdict(list)
    for color, lines in chain(warp_color_lines.items(), weft_color_lines.items()):
        color_lines[color].extend(lines)

    color_runs = defaultdict(list)
    for color, lines in chain(warp_color_runs.items(), weft_color_runs.items()):
        color_runs[color].extend(lines)

    color_lines, color_runs = sort_fills_and_strokes(color_lines, color_runs)

    stitch_groups = _get_fill_stitch_groups(fill, outline, color_lines)
    if stitch_groups:
        starting_point = stitch_groups[-1].stitches[-1]
    stitch_groups += _get_run_stitch_groups(fill, outline, color_runs, starting_point, ending_point)
    return stitch_groups


def _generate_herringbone_lines(
    outline: Polygon,
    fill: 'FillStitch',
    dimensions: Tuple[float, float, float, float],
    rotation: float,
) -> List[List[List[LineString]]]:
    """
    Generates herringbone lines with staggered stitch positions

    :param outline: the outline to fill with the herringbone lines
    :param fill: the tartan fill element
    :param dimensions: minx, miny, maxx, maxy
    :param rotation: the rotation value
    :returns: a tuple of two list with herringbone stripes [0] up segments / [1] down segments \
    """
    rotation_center = _get_rotation_center(outline)
    minx, miny, maxx, maxy = dimensions

    herringbone_lines: list = [[], []]
    odd = True
    while minx < maxx:
        odd = not odd
        right = minx + fill.herringbone_width
        if odd:
            left_line = LineString([(minx, miny), (minx, maxy + fill.herringbone_width)])
        else:
            left_line = LineString([(minx, miny - fill.herringbone_width), (minx, maxy)])

        if odd:
            right_line = LineString([(right, miny - fill.herringbone_width), (right, maxy)])
        else:
            right_line = LineString([(right, miny), (right, maxy + fill.herringbone_width)])

        left_line = segmentize(left_line, max_segment_length=fill.row_spacing)
        right_line = segmentize(right_line, max_segment_length=fill.row_spacing)

        lines = list(zip(left_line.coords, right_line.coords))

        staggered_lines = []
        for i, line in enumerate(lines):
            linestring = LineString(line)
            staggered_line = apply_stitches(linestring, fill.max_stitch_length, fill.staggers, fill.row_spacing, i)
            # make sure we do not ommit the very first or very last point (it would confuse our sorting algorithm)
            staggered_line = LineString([linestring.coords[0]] + list(staggered_line.coords) + [linestring.coords[-1]])
            staggered_lines.append(staggered_line)

        if odd:
            herringbone_lines[0].append(list(rotate(MultiLineString(staggered_lines), rotation, rotation_center).geoms))
        else:
            herringbone_lines[1].append(list(rotate(MultiLineString(staggered_lines), rotation, rotation_center).geoms))

        # add some little space extra to make things easier with line_merge later on
        # (avoid spots with 4 line points)
        minx += fill.herringbone_width + 0.005

    return herringbone_lines


def _generate_tartan_lines(
    outline: Polygon,
    fill: 'FillStitch',
    dimensions: Tuple[float, float, float, float],
    rotation: float,
) -> List[LineString]:
    """
    Generates tartan lines with staggered stitch positions

    :param outline: the outline to fill with the herringbone lines
    :param fill: the tartan fill element
    :param dimensions: minx, miny, maxx, maxy
    :param rotation: the rotation value
    :returns: a list with the tartan lines
    """
    rotation_center = _get_rotation_center(outline)
    # default angle is 45°
    rotation += fill.tartan_angle
    minx, miny, maxx, maxy = dimensions

    left_line = LineString([(minx, miny), (minx, maxy)])
    left_line = rotate(left_line, rotation, rotation_center)
    left_line = segmentize(left_line, max_segment_length=fill.row_spacing)

    right_line = LineString([(maxx, miny), (maxx, maxy)])
    right_line = rotate(right_line, rotation, rotation_center)
    right_line = segmentize(right_line, max_segment_length=fill.row_spacing)

    lines = list(zip(left_line.coords, right_line.coords))

    staggered_lines = []
    for i, line in enumerate(lines):
        linestring = LineString(line)
        staggered_line = apply_stitches(linestring, fill.max_stitch_length, fill.staggers, fill.row_spacing, i)
        # make sure we do not ommit the very first or very last point (it would confuse our sorting algorithm)
        staggered_line = LineString([linestring.coords[0]] + list(staggered_line.coords) + [linestring.coords[-1]])
        staggered_lines.append(staggered_line)
    return staggered_lines


def _split_herringbone_warp_weft(
    lines: List[List[List[LineString]]],
    rows_per_thread: int,
    stitch_length: float
) -> tuple:
    """
    Split the herringbone lines into warp lines and weft lines as defined by rows rows_per_thread
    Merge weft lines for each block.

    :param lines: lines to divide
    :param rows_per_thread: length of line blocks
    :param stitch_length: maximum stitch length for weft connector lines
    :returns: [0] warp and [1] weft list of MultiLineString objects
    """
    warp_lines: List[LineString] = []
    weft_lines: List[LineString] = []
    for i, line_blocks in enumerate(lines):
        for line_block in line_blocks:
            if i == 0:
                warp, weft = _split_warp_weft(line_block, rows_per_thread)
            else:
                weft, warp = _split_warp_weft(line_block, rows_per_thread)
            warp_lines.append(warp)
            weft_lines.append(weft)

    connected_weft = []
    line2 = None
    for multilinestring in weft_lines:
        connected_line_block = []
        geoms = list(multilinestring.geoms)
        for line1, line2 in zip(geoms[:-1], geoms[1:]):
            connected_line_block.append(line1)
            connector_line = LineString([get_point(line1, -1), get_point(line2, 0)])
            connector_line = segmentize(connector_line, max_segment_length=stitch_length)
            connected_line_block.append(connector_line)
        if line2:
            connected_line_block.append(line2)
        connected_weft.append(ensure_multi_line_string(line_merge(MultiLineString(connected_line_block))))
    return warp_lines, connected_weft


def _split_warp_weft(lines: List[LineString], rows_per_thread: int) -> Tuple[List[LineString], List[LineString]]:
    """
    Divide given lines in warp and weft, sort afterwards

    :param lines: a list of LineString shapes
    :param rows_per_thread: length of line blocks
    :returns: tuple with sorted [0] warp and [1] weft LineString shapes
    """
    warp_lines = []
    weft_lines = []
    for i in range(rows_per_thread):
        warp_lines.extend(lines[i::rows_per_thread*2])
        weft_lines.extend(lines[i+rows_per_thread::rows_per_thread*2])
    return _sort_lines(warp_lines), _sort_lines(weft_lines)


def _sort_lines(lines: List[LineString]):
    """
    Sort given list of LineString shapes by first coordinate
    and reverse every second line

    :param lines: a list of LineString shapes
    :returns: sorted list of LineString shapes with alternating directions
    """
    # sort lines
    lines.sort(key=lambda line: line.coords[0])
    # reverse every second line
    lines = [line if i % 2 == 0 else line.reverse() for i, line in enumerate(lines)]
    return MultiLineString(lines)


@cache
def _get_rotation_center(outline: Polygon) -> Point:
    """
    Returns the rotation center used for any tartan pattern rotation

    :param outline: the polygon shape to be filled with the pattern
    :returns: the center point of the shape
    """
    # somehow outline.centroid doesn't deliver the point we need
    bounds = outline.bounds
    return LineString([(bounds[0], bounds[1]), (bounds[2], bounds[3])]).centroid


@cache
def _get_dimensions(
    fill: 'FillStitch',
    outline: Polygon,
    offset: Tuple[float, float],
    warp_width: float,
    weft_width: float
) -> Tuple[float, float, float, float]:
    """
    Calculates the dimensions for the tartan pattern.
    Make sure it is big enough for pattern rotations, etc.

    :param fill: the FillStitch element
    :param outline: the shape to be filled with a tartan pattern
    :param offset: mm offset for x, y
    :param warp_width: mm warp width
    :param weft_width: mm weft width
    :returns: a tuple with boundaries (minx, miny, maxx, maxy)
    """
    # add space to allow rotation and herringbone patterns to cover the shape
    centroid = _get_rotation_center(outline)
    min_radius = minimum_bounding_radius(outline)
    minx = centroid.x - min_radius
    miny = centroid.y - min_radius
    maxx = centroid.x + min_radius
    maxy = centroid.y + min_radius

    # add some extra space
    extra_space = max(
        warp_width * PIXELS_PER_MM,
        weft_width * PIXELS_PER_MM,
        2 * fill.row_spacing * fill.rows_per_thread
    )
    minx -= extra_space
    maxx += extra_space
    miny -= extra_space
    maxy += extra_space

    minx -= (offset[0] * PIXELS_PER_MM)
    miny -= (offset[1] * PIXELS_PER_MM)

    return minx, miny, maxx, maxy


def _get_herringbone_color_segments(
    lines: List[MultiLineString],
    polygons: defaultdict,
    outline: Polygon,
    rotation: float,
    stitch_length: float,
    weft: bool = False
) -> defaultdict:
    """
    Generate herringbone line segments in given tartan direction grouped by color

    :param lines: the line segments forming the pattern
    :param polygons: color grouped polygon stripes
    :param outline: the outline to be filled with the herringbone pattern
    :param rotation: degrees used for rotation
    :param stitch_length: maximum stitch length for weft connector lines
    :param weft: wether to render as warp or weft
    :returns: defaultdict with color grouped herringbone segments
    """
    line_segments: defaultdict = defaultdict(list)

    if not polygons:
        return line_segments

    lines = line_merge(lines)
    for line_blocks in lines:
        segments = _get_tartan_color_segments(line_blocks, polygons, outline, rotation, stitch_length, weft, True)
        for color, segment in segments.items():
            if weft:
                line_segments[color].append(MultiLineString(segment))
            else:
                line_segments[color].extend(segment)

    if not weft:
        return line_segments

    return _get_weft_herringbone_color_segments(outline, line_segments, polygons, stitch_length)


def _get_weft_herringbone_color_segments(
    outline: Polygon,
    line_segments: defaultdict,
    polygons: defaultdict,
    stitch_length: float,
) -> defaultdict:
    """
    Makes sure weft herringbone lines connect correctly

    Herringbone weft lines need to connect in horizontal direction (or whatever the current rotation is)
    which is opposed to the herringbone stripe blocks \\\\ //// \\\\ //// \\\\ ////

    :param outline: the outline to be filled with the herringbone pattern
    :param line_segments: the line segments forming the pattern
    :param polygons: color grouped polygon stripes
    :param stitch_length: maximum stitch length
    :returns: defaultdict with color grouped weft lines
    """
    weft_lines = defaultdict(list)
    for color, lines in line_segments.items():
        color_lines: List[LineString] = []
        for polygon in polygons[color][0]:
            polygon = polygon.normalize()
            polygon_coords = list(polygon.exterior.coords)
            polygon_top = LineString(polygon_coords[0:2])
            polygon_bottom = LineString(polygon_coords[2:4]).reverse()
            if not any([polygon_top.intersects(outline), polygon_bottom.intersects(outline)]):
                polygon_top = LineString(polygon_coords[1:3])
                polygon_bottom = LineString(polygon_coords[3:5]).reverse()

            polygon_multi_lines = lines
            polygon_multi_lines.sort(key=lambda line: polygon_bottom.project(line.centroid))
            polygon_lines = []
            for multiline in polygon_multi_lines:
                polygon_lines.extend(multiline.geoms)
            polygon_lines = [line for line in polygon_lines if line.intersects(polygon)]
            if not polygon_lines:
                continue
            color_lines.extend(polygon_lines)

            if polygon_top.intersects(outline) or polygon_bottom.intersects(outline):
                connectors = _get_weft_herringbone_connectors(polygon_lines, polygon_top, polygon_bottom, stitch_length)
                if connectors:
                    color_lines.extend(connectors)

            check_stop_flag()

        # Users are likely to type in a herringbone width which is a multiple (or fraction) of the stripe width.
        # They may end up unconnected after line_merge, so we need to shift the weft for a random small number
        multi_lines = translate(ensure_multi_line_string(line_merge(MultiLineString(color_lines))), 0.00123, 0.00123)
        multi_lines = ensure_multi_line_string(multi_lines.intersection(outline))

        weft_lines[color].extend(list(multi_lines.geoms))

    return weft_lines


def _get_weft_herringbone_connectors(
    polygon_lines: List[LineString],
    polygon_top: LineString,
    polygon_bottom: LineString,
    stitch_length: float
) -> List[LineString]:
    """
    Generates lines to connect lines

    :param polygon_lines: lines to connect
    :param polygon_top: top line of the polygon
    :param polygon_bottom: bottom line of the polygon
    :param stitch_length: stitch length
    :returns: a list of LineString connectors
    """
    connectors: List[LineString] = []
    previous_end = None
    for line in reversed(polygon_lines):
        start = get_point(line, 0)
        end = get_point(line, -1)
        if previous_end is None:
            # adjust direction of polygon lines if necessary
            if polygon_top.project(start, True) > 0.5:
                polygon_top = polygon_top.reverse()
                polygon_bottom = polygon_bottom.reverse()
            start_distance = polygon_top.project(start)
            end_distance = polygon_top.project(end)
            if start_distance > end_distance:
                start, end = end, start
            previous_end = end
            continue

        # adjust line direction and add connectors
        prev_polygon_line = min([polygon_top, polygon_bottom], key=lambda polygon_line: previous_end.distance(polygon_line))
        current_polygon_line = min([polygon_top, polygon_bottom], key=lambda polygon_line: start.distance(polygon_line))
        if prev_polygon_line != current_polygon_line:
            start, end = end, start
        if not previous_end == start:
            connector = LineString([previous_end, start])
            if prev_polygon_line == polygon_top:
                connector = connector.offset_curve(-0.0001)
            else:
                connector = connector.offset_curve(0.0001)
            connectors.append(LineString([previous_end, get_point(connector, 0)]))
            connectors.append(segmentize(connector, max_segment_length=stitch_length))
            connectors.append(LineString([get_point(connector, -1), start]))
        previous_end = end
    return connectors


def _get_tartan_color_segments(
    lines: List[LineString],
    polygons: defaultdict,
    outline: Polygon,
    rotation: float,
    stitch_length: float,
    weft: bool = False,
    herringbone: bool = False
) -> defaultdict:
    """
    Generate tartan line segments in given tartan direction grouped by color

    :param lines: the lines to form the tartan pattern with
    :param polygons: color grouped polygon stripes
    :param outline: the outline to fill with the tartan pattern
    :param rotation: rotation in degrees
    :param stitch_length: maximum stitch length for weft connector lines
    :param weft: wether to render as warp or weft
    :param herringbone: wether herringbone or normal tartan patterns are rendered
    :returns: a dictionary with color grouped line segments
    """
    line_segments: defaultdict = defaultdict(list)
    if not polygons:
        return line_segments
    for color, shapes in polygons.items():
        polygons = shapes[0]
        for polygon in polygons:
            segments = _get_segment_lines(polygon, lines, outline, stitch_length, rotation, weft, herringbone)
            if segments:
                line_segments[color].extend(segments)
        check_stop_flag()
    return line_segments


def _get_color_runs(lines: defaultdict, stitch_length: float) -> defaultdict:
    """
    Segmentize running stitch segments and return in a separate color grouped dictionary

    :param lines: tartan shapes grouped by color
    :param stitch_length: stitch length used to segmentize the lines
    :returns: defaultdict with segmentized running stitches grouped by color
    """
    runs: defaultdict = defaultdict(list)
    if not lines:
        return runs
    for color, shapes in lines.items():
        for run in shapes[1]:
            runs[color].append(segmentize(run, max_segment_length=stitch_length))
    return runs


def _get_segment_lines(
    polygon: Polygon,
    lines: MultiLineString,
    outline: Polygon,
    stitch_length: float,
    rotation: float,
    weft: bool,
    herringbone: bool
) -> List[LineString]:
    """
    Fill the given polygon with lines
    Each line should start and end at the outline border

    :param polygon: the polygon stripe to fill
    :param lines: the lines that form the pattern
    :param outline: the outline to fill with the tartan pattern
    :param stitch_length: maximum stitch length for weft connector lines
    :param rotation: rotation in degrees
    :param weft: wether to render as warp or weft
    :param herringbone: wether herringbone or normal tartan patterns are rendered
    :returns: a list of LineString objects
    """
    boundary = outline.boundary
    segments = []
    if not lines.intersects(polygon):
        return []
    segment_lines = list(ensure_multi_line_string(lines.intersection(polygon), 0.5).geoms)
    if not segment_lines:
        return []
    previous_line = None
    for line in segment_lines:
        segments.append(line)
        if not previous_line:
            previous_line = line
            continue
        point1 = get_point(previous_line, -1)
        point2 = get_point(line, 0)
        if point1.equals(point2):
            previous_line = line
            continue
        # add connector from point1 to point2 if none of them touches the outline
        connector = _get_connector(point1, point2, boundary, stitch_length)
        if connector:
            segments.append(connector)
        previous_line = line

    if not segments:
        return []
    lines = line_merge(MultiLineString(segments))

    if not (herringbone and weft):
        lines = lines.intersection(outline)

    if not herringbone:
        lines = _connect_lines_to_outline(lines, outline, rotation, stitch_length, weft)

    return list(ensure_multi_line_string(lines).geoms)


def _get_connector(
    point1: Point,
    point2: Point,
    boundary: Union[MultiLineString, LineString],
    stitch_length: float
) -> Optional[LineString]:
    """
    Constructs a line between the two points when they are not near the boundary

    :param point1: first point
    :param point2: last point
    :param boundary: the outline of the shape (including holes)
    :param stitch_length: maximum stitch length to segmentize new line
    :returns: a LineString between point1 and point1, None if one of them touches the boundary
    """
    connector = None
    if point1.distance(boundary) > 0.005 and point2.distance(boundary) > 0.005:
        connector = segmentize(LineString([point1, point2]), max_segment_length=stitch_length)
    return connector


def _connect_lines_to_outline(
    lines: Union[MultiLineString, LineString],
    outline: Polygon,
    rotation: float,
    stitch_length: float,
    weft: bool
) -> Union[MultiLineString, LineString]:
    """
    Connects end points within the shape with the outline
    This should only be necessary if the tartan angle is nearly 0 or 90 degrees

    :param lines: lines to connect to the outline (if necessary)
    :param outline: the shape to be filled with a tartan pattern
    :param rotation: the rotation value
    :param stitch_length: maximum stitch length to segmentize new line
    :param weft: wether to render as warp or weft
    :returns: merged line(s) connected to the outline
    """
    boundary = outline.boundary
    lines = list(ensure_multi_line_string(lines).geoms)
    outline_connectors = []
    for line in lines:
        start = get_point(line, 0)
        end = get_point(line, -1)
        if start.intersects(outline) and start.distance(boundary) > 0.05:
            outline_connectors.append(_connect_point_to_outline(start, outline, rotation, stitch_length, weft))
        if end.intersects(outline) and end.distance(boundary) > 0.05:
            outline_connectors.append(_connect_point_to_outline(end, outline, rotation, stitch_length, weft))
    lines.extend(outline_connectors)
    lines = line_merge(MultiLineString(lines))
    return lines


def _connect_point_to_outline(
    point: Point,
    outline: Polygon,
    rotation: float,
    stitch_length: float,
    weft: bool
) -> Union[LineString, list]:
    """
    Connect given point to the outline

    :param outline: the shape to be filled with a tartan pattern
    :param rotation: the rotation value
    :param stitch_length: maximum stitch length to segmentize new line
    :param weft: wether to render as warp or weft
    :returns: a Linestring with the correct angle for the given tartan direction (between outline and point)
    """
    scale_factor = point.hausdorff_distance(outline) * 2
    directional_vector = _get_angled_line_from_point(point, rotation, scale_factor, weft)
    directional_vector = outline.boundary.intersection(directional_vector)
    if directional_vector.is_empty:
        return []
    return segmentize(LineString([point, nearest_points(directional_vector, point)[0]]), max_segment_length=stitch_length)


def _get_angled_line_from_point(point: Point, rotation: float, scale_factor: float, weft: bool) -> LineString:
    """
    Generates an angled line for the given tartan direction

    :param point: the starting point for the new line
    :param rotation: the rotation value
    :param scale_factor: defines the length of the line
    :param weft: wether to render as warp or weft
    :returns: a LineString
    """
    if not weft:
        rotation += 90
    rotation = radians(rotation)
    x = point.coords[0][0] + cos(rotation)
    y = point.coords[0][1] + sin(rotation)
    return scale(LineString([point, (x, y)]), scale_factor, scale_factor)


def _get_fill_stitch_groups(
    fill: 'FillStitch',
    shape: Polygon,
    color_lines: defaultdict,
) -> List[StitchGroup]:
    """
    Route fill stitches

    :param fill: the FillStitch element
    :param shape: the shape to be filled
    :param color_lines: lines grouped by color
    :returns: a list with StitchGroup objects
    """
    stitch_groups: List[StitchGroup] = []
    i = 0
    for color, lines in color_lines.items():
        i += 1
        if stitch_groups:
            starting_point = stitch_groups[-1].stitches[-1]
        else:
            starting_point = ensure_multi_line_string(shape.boundary).geoms[0].coords[1]
        ending_point = ensure_multi_line_string(shape.boundary).geoms[0].coords[1]
        segments = [list(line.coords) for line in lines if len(line.coords) > 1]
        stitch_group = _segments_to_stitch_group(fill, shape, segments, i, color, starting_point, ending_point)
        if stitch_group is not None:
            stitch_groups.append(stitch_group)
        check_stop_flag()
    return stitch_groups


def _get_run_stitch_groups(
    fill: 'FillStitch',
    shape: Polygon,
    color_lines: defaultdict,
    starting_point: Optional[Union[tuple, Stitch]],
    ending_point: Optional[Union[tuple, Stitch]]
) -> List[StitchGroup]:
    """
    Route running stitches

    :param fill: the FillStitch element
    :param shape: the shape to be filled
    :param color_lines: lines grouped by color
    :param starting_point: the starting point
    :param ending_point: the ending point
    :returns: a list with StitchGroup objects
    """
    stitch_groups: List[StitchGroup] = []
    for color, lines in color_lines.items():
        segments = [list(line.coords) for line in lines if len(line.coords) > 1]
        stitch_group = _segments_to_stitch_group(fill, shape, segments, None, color, starting_point, ending_point, True)
        if stitch_group is not None:
            stitch_groups.append(stitch_group)
        check_stop_flag()
    return stitch_groups


def _segments_to_stitch_group(
    fill: 'FillStitch',
    shape: Polygon,
    segments: List[List[Tuple[float, float]]],
    iteration: Optional[int],
    color: str,
    starting_point: Optional[Union[tuple, Stitch]],
    ending_point: Optional[Union[tuple, Stitch]],
    runs: bool = False
) -> Optional[StitchGroup]:
    """
    Route segments and turn them into a stitch group

    :param fill: the FillStitch element
    :param shape: the shape to be filled
    :param segments: a list with coordinate tuples
    :param iteration: wether to remove start and end travel stitches from the stitch group
    :param color: color information
    :param starting_point: the starting point
    :param ending_point: the ending point
    :param runs: wether running_stitch options should be applied or not
    :returns: a StitchGroup
    """
    fill_stitch_graph = build_fill_stitch_graph(shape, segments, starting_point, ending_point)
    if is_empty(fill_stitch_graph):
        return None
    graph_make_valid(fill_stitch_graph)
    travel_graph = build_travel_graph(fill_stitch_graph, shape, fill.angle, False)
    path = find_stitch_path(fill_stitch_graph, travel_graph, starting_point, ending_point)
    stitches = path_to_stitches(
        shape,
        path,
        travel_graph,
        fill_stitch_graph,
        fill.running_stitch_length,
        fill.running_stitch_tolerance,
        fill.skip_last,
        False  # no underpath
    )

    if iteration:
        stitches = remove_start_end_travel(fill, stitches, color, iteration)

    if runs:
        stitches = bean_stitch(stitches, fill.bean_stitch_repeats, ['auto_fill_travel'])

    stitch_group = StitchGroup(
        color=color,
        tags=("tartan_fill", "auto_fill_top"),
        stitches=stitches,
        force_lock_stitches=fill.force_lock_stitches,
        lock_stitches=fill.lock_stitches,
        trim_after=fill.has_command("trim") or fill.trim_after
    )

    if runs:
        stitch_group.add_tag("tartan_run")

    return stitch_group