def intersect_segments(path1, path2): paths = [PathObject(path1, 0), PathObject(path2, 1)] approx_paths = get_approx_paths(paths) cross_point_id = 0 for i in range(len(approx_paths)): for j in range(i + 1, len(approx_paths)): for path1, approx_path1, rect1 in approx_paths[i]: for path2, approx_path2, rect2 in approx_paths[j]: if not path1.obj_id == path2.obj_id and \ is_bbox_overlap(rect1, rect2): for p in range(1, len(approx_path1)): (p0, t0), (p1, t1) = approx_path1[p - 1:p + 1] for q in range(1, len(approx_path2)): (p2, t2), (p3, t3) = approx_path2[q - 1:q + 1] if equal(p0, p2): cp = p0 elif equal(p0, p3) or \ equal(p1, p2) or \ equal(p1, p3): cp = None else: cp = intersect_lines(p0, p1, p2, p3) if cp is not None: index1 = index(cp, p0, t0, p1, t1) index2 = index(cp, p2, t2, p3, t3) path1.cp_indexes.append(index1) path1.cp_dict[index1] = cross_point_id path2.cp_indexes.append(index2) path2.cp_dict[index2] = cross_point_id cross_point_id += 1 result = [[], []] if not paths[0].cp_indexes: return None indx = paths[0].cp_indexes[0] if indx < 0: return None sp = paths[0].split_path_at(indx, paths[0].cp_dict[indx]) if len(sp) == 1: return None result[0] = deepcopy(sp[0].path[1][-1]) start = [] + result[0] if len(result[0]) == 4: start = [] + result[0][2] if not paths[1].cp_indexes: return None indx = paths[1].cp_indexes[0] if indx < 0: return None sp = paths[1].split_path_at(indx, paths[1].cp_dict[indx]) if len(sp) == 1: return None result[1] = [start, deepcopy(sp[1].path[1][0])] return result
def intersect_and_join(paths1, paths2, rule=INTERSECT_RULE): objs = [CurveObject(paths1, 0), CurveObject(paths2, 1)] if not is_bbox_overlap(objs[0].get_bbox(), objs[1].get_bbox()): return None new_paths = intersect_objects(objs) if not new_paths: return None buff = [] closed_paths = [] for item in new_paths: if rule == INTERSECT_RULE and \ not contained(objs[abs(item.obj_id - 1)], item): continue if rule == FUSION_RULE and \ contained(objs[abs(item.obj_id - 1)], item): continue if rule == CUTTING_RULE: if not item.obj_id and not contained(objs[1], item): pass elif item.obj_id and contained(objs[0], item): pass else: continue if item.is_closed(): closed_paths.append(item) else: buff.append(item) while len(buff): start = buff[0] if start.start_id == start.end_id: start.close_path() closed_paths.append(start) buff.remove(start) continue cont = find_cross_id(buff[1:], start.end_id) if cont: start = start.join_path(cont) buff.remove(cont) buff = [ start, ] + buff[1:] else: closed_paths.append(start) buff = buff[1:] result = [] for item in closed_paths: result.append(item.get_path()) for obj in objs: obj.destroy() return result
def intersect_segments(path1, path2): paths = [PathObject(path1, 0), PathObject(path2, 1)] approx_paths = get_approx_paths(paths) cross_point_id = 0 for i in range(len(approx_paths)): for j in range(i + 1, len(approx_paths)): for path1, approx_path1, rect1 in approx_paths[i]: for path2, approx_path2, rect2 in approx_paths[j]: if not path1.obj_id == path2.obj_id and \ is_bbox_overlap(rect1, rect2): for p in range(1, len(approx_path1)): (p0, t0), (p1, t1) = approx_path1[p - 1:p + 1] for q in range(1, len(approx_path2)): (p2, t2), (p3, t3) = approx_path2[q - 1:q + 1] if equal(p0, p2): cp = p0 elif equal(p0, p3) or \ equal(p1, p2) or \ equal(p1, p3): cp = None else: cp = intersect_lines(p0, p1, p2, p3) if cp is not None: index1 = index(cp, p0, t0, p1, t1) index2 = index(cp, p2, t2, p3, t3) path1.cp_indexes.append(index1) path1.cp_dict[index1] = cross_point_id path2.cp_indexes.append(index2) path2.cp_dict[index2] = cross_point_id cross_point_id += 1 result = [[], []] if not paths[0].cp_indexes: return None indx = paths[0].cp_indexes[0] if indx < 0: return None sp = paths[0].split_path_at(indx, paths[0].cp_dict[indx]) if len(sp) == 1:return None result[0] = deepcopy(sp[0].path[1][-1]) start = [] + result[0] if len(result[0]) == 4: start = [] + result[0][2] if not paths[1].cp_indexes: return None indx = paths[1].cp_indexes[0] if indx < 0: return None sp = paths[1].split_path_at(indx, paths[1].cp_dict[indx]) if len(sp) == 1:return None result[1] = [start, deepcopy(sp[1].path[1][0])] return result
def intersect_and_join(paths1, paths2, rule=INTERSECT_RULE): objs = [CurveObject(paths1, 0), CurveObject(paths2, 1)] if not is_bbox_overlap(objs[0].get_bbox(), objs[1].get_bbox()): return None new_paths = intersect_objects(objs) if not new_paths: return None buff = [] closed_paths = [] for item in new_paths: if rule == INTERSECT_RULE and \ not contained(objs[abs(item.obj_id - 1)], item): continue if rule == FUSION_RULE and \ contained(objs[abs(item.obj_id - 1)], item): continue if rule == CUTTING_RULE: if not item.obj_id and not contained(objs[1], item): pass elif item.obj_id and contained(objs[0], item): pass else: continue if item.is_closed(): closed_paths.append(item) else: buff.append(item) while len(buff): start = buff[0] if start.start_id == start.end_id: start.close_path() closed_paths.append(start) buff.remove(start) continue cont = find_cross_id(buff[1:], start.end_id, start.obj_id) if cont: start = start.join_path(cont) buff.remove(cont) buff = [start, ] + buff[1:] else: closed_paths.append(start) buff = buff[1:] result = [] for item in closed_paths: result.append(item.get_path()) for obj in objs: obj.destroy() return result
def intersect_objects(curve_objs): paths = [] for i in range(len(curve_objs)): paths += curve_objs[i].paths() approx_paths = get_approx_paths(paths) cross_point_id = 0 for i in range(len(approx_paths)): for j in range(i + 1, len(approx_paths)): for path1, approx_path1, rect1 in approx_paths[i]: for path2, approx_path2, rect2 in approx_paths[j]: if not path1.obj_id == path2.obj_id and \ is_bbox_overlap(rect1, rect2): for p in range(1, len(approx_path1)): (p0, t0), (p1, t1) = approx_path1[p - 1:p + 1] for q in range(1, len(approx_path2)): (p2, t2), (p3, t3) = approx_path2[q - 1:q + 1] if equal(p0, p2): cp = p0 elif equal(p0, p3) or \ equal(p1, p2) or \ equal(p1, p3): cp = None else: cp = intersect_lines(p0, p1, p2, p3) if cp is not None: index1 = index(cp, p0, t0, p1, t1) index2 = index(cp, p2, t2, p3, t3) path1.cp_indexes.append(index1) path1.cp_dict[index1] = cross_point_id path2.cp_indexes.append(index2) path2.cp_dict[index2] = cross_point_id cross_point_id += 1 result = [] for obj in curve_objs: for path in obj.paths(): result += path.split() return result
def self_intersect(curve_obj): paths = curve_obj.paths() approx_paths = get_approx_paths(paths) cross_point_id = 0 approx_paths = approx_paths[0] for i in range(-1, len(approx_paths) - 1): path1, approx_path1, rect1 = approx_paths[i] for j in range(-1, len(approx_paths) - 1): if i == j: continue path2, approx_path2, rect2 = approx_paths[j] if is_bbox_overlap(rect1, rect2): for p in range(1, len(approx_path1)): (p0, t0), (p1, t1) = approx_path1[p - 1:p + 1] for q in range(1, len(approx_path2)): (p2, t2), (p3, t3) = approx_path2[q - 1:q + 1] if equal(p0, p2): cp = p0 elif equal(p0, p3) or \ equal(p1, p2) or \ equal(p1, p3): cp = None else: cp = intersect_lines(p0, p1, p2, p3) if cp is not None: index1 = index(cp, p0, t0, p1, t1) index2 = index(cp, p2, t2, p3, t3) if index1 not in path1.cp_indexes: path1.cp_indexes.append(index1) path1.cp_dict[index1] = cross_point_id if index2 not in path2.cp_indexes: path2.cp_indexes.append(index2) path2.cp_dict[index2] = cross_point_id cross_point_id += 1 return paths[0].split()
def self_intersect(curve_obj): paths = curve_obj.paths() approx_paths = get_approx_paths(paths) cross_point_id = 0 approx_paths = approx_paths[0] for i in range(-1, len(approx_paths) - 1): path1, approx_path1, rect1 = approx_paths[i] for j in range(-1, len(approx_paths) - 1): if i == j:continue path2, approx_path2, rect2 = approx_paths[j] if is_bbox_overlap(rect1, rect2): for p in range(1, len(approx_path1)): (p0, t0), (p1, t1) = approx_path1[p - 1:p + 1] for q in range(1, len(approx_path2)): (p2, t2), (p3, t3) = approx_path2[q - 1:q + 1] if equal(p0, p2): cp = p0 elif equal(p0, p3) or \ equal(p1, p2) or \ equal(p1, p3): cp = None else: cp = intersect_lines(p0, p1, p2, p3) if cp is not None: index1 = index(cp, p0, t0, p1, t1) index2 = index(cp, p2, t2, p3, t3) if not index1 in path1.cp_indexes: path1.cp_indexes.append(index1) path1.cp_dict[index1] = cross_point_id if not index2 in path2.cp_indexes: path2.cp_indexes.append(index2) path2.cp_dict[index2] = cross_point_id cross_point_id += 1 return paths[0].split()