def get_merged_strokes(strokes): def extend_stroke(stroke, vertices): for vert in map(StrokeVertex, vertices): stroke.insert_vertex(vert, stroke.stroke_vertices_end()) return stroke base_strokes = tuple(stroke for stroke in strokes if not is_poly_clockwise(stroke)) merged_strokes = OrderedDict((s, list()) for s in base_strokes) for stroke in filter(is_poly_clockwise, strokes): for base in base_strokes: # don't merge when diffuse colors don't match if diffuse_from_stroke(stroke) != diffuse_from_stroke(stroke): continue # only merge when the 'hole' is inside the base elif stroke_inside_stroke(stroke, base): merged_strokes[base].append(stroke) break # if it isn't a hole, it is likely that there are two strokes belonging # to the same object separated by another object. let's try to join them elif (get_object_name(base) == get_object_name(stroke) and diffuse_from_stroke(stroke) == diffuse_from_stroke(stroke)): base = extend_stroke(base, (sv for sv in stroke)) break else: # if all else fails, treat this stroke as a base stroke merged_strokes.update({stroke: []}) return merged_strokes