def par0(): """ 对平行线的优化, 组成可以使用的路网 :return: """ road_data = load_model2road_by_grid('./road/par.txt', 740) for i, road0 in enumerate(road_data): for j, road1 in enumerate(road_data): if i < j and road_near(road0, road1): par_divide(road0, road1) print "par_divide 0" # 偏移终点起点的路口 for i, road0 in enumerate(road_data): for j, road1 in enumerate(road_data): if road0.name == road1.name or not road_near(road0, road1): continue par_offset(road0, road1) print "par offset" for road in road_data: par_simplify(road) # 再做divide前需要清空 for road in road_data: road.cross_list = [] for i, road0 in enumerate(road_data): for j, road1 in enumerate(road_data): if road0.name == road1.name: continue if i < j and road_near(road0, road1): par_divide(road0, road1) for road in road_data: par_insert_cross(road) for road in road_data: par_check(road) save_road2model('./road/par0.txt', road_data) print "par0"
def par1(): """ 切路口 :return: """ road_data = load_model2road('./road/par0.txt') for i, road0 in enumerate(road_data): for j, road1 in enumerate(road_data): if i < j and road0.name != road1.name and road_near(road0, road1): par_cross(road0, road1) # 切掉路口那段 for road in road_data: par_cut(road) save_road2model('./road/par1.txt', road_data)
def center0(): """ 中心线的优化,切分 :return: """ center_road = load_model2road('./road/center.txt') # 合并平行连接的道路 for i, road0 in enumerate(center_road): for j, road1 in enumerate(center_road): if j < i: center_merge(road0, road1) # 交叉点 for i, road0 in enumerate(center_road): for j, road1 in enumerate(center_road): if i != j and road_near(road0, road1): try: center_cross(road0, road1) except ZeroDivisionError: print road0.name, road1.name # 预防多插点 for road in center_road: center_dup(road) for i, road0 in enumerate(center_road): for j, road1 in enumerate(center_road): if j < i and road_near(road0, road1): try: center_divide(road0, road1) except ZeroDivisionError: print road0.name, road1.name print "divide 1" # 端点落到道路 for i, road0 in enumerate(center_road): for j, road1 in enumerate(center_road): if i != j and road_near(road0, road1): center_offset(road0, road1) print "center offset" for road in center_road: center_simplify(road) save_road2model('./road/center0.txt', center_road) # 为防止精度不必要的损失,在一个程序内完成 for road in center_road: road.cross_list = [] for i, road0 in enumerate(center_road): for j, road1 in enumerate(center_road): if i < j and road_near(road0, road1): center_divide(road0, road1) print "divide 2" for road in center_road: center_insert_cross(road) save_road2model('./road/center01.txt', center_road) new_center_road = [] for road in center_road: new_center_road.extend(center_split(road)) for i, road in enumerate(new_center_road): road.set_rid(i) save_road2model('./road/center1.txt', new_center_road)
def par(): PAR = 40 road_data = load_model2road('./road_new/center1.txt') par_road = [] # road_index = 0 for i, road in enumerate(road_data): name, point_list = road.name, road.point_list rid = road.rid last_pt = None road0, road1 = Road(name, 0, rid * 2), Road(name, 0, rid * 2 + 1) road0.set_grid_set(road.grid_set) road1.set_grid_set(road.grid_set) # road_index += 2 seg_list0, seg_list1 = [], [] for pt in point_list: if last_pt is not None: # 获取两条平移线段 seg0, seg1 = get_parallel(last_pt, pt, PAR) seg_list0.append(seg0) seg1.set_invert() seg_list1.append(seg1) last_pt = pt # 计算线段之间的交点 last_seg = None for j, seg in enumerate(seg_list0): if last_seg is None: road0.add_point(seg.begin_point) else: _, px, py = get_cross_point(last_seg, seg) if px is None: # 平行 road0.add_point(seg.begin_point) else: cp = Point(px, py) road0.add_point(cp) last_seg = seg road0.add_point(last_seg.end_point) last_seg = None for seg in reversed(seg_list1): if last_seg is None: road1.add_point(seg.begin_point) else: _, px, py = get_cross_point(last_seg, seg) if px is None: # 平行 road1.add_point(seg.begin_point) else: cp = Point(px, py) road1.add_point(cp) last_seg = seg road1.add_point(last_seg.end_point) # 并生成线段 road0.gene_segment() road1.gene_segment() par_road.append(road0) par_road.append(road1) # for test # save_par('./road/par_0.txt', par_road) # 端点处有merge的可能 for i, road0 in enumerate(par_road): for j, road1 in enumerate(par_road): if i < j and road_near(road0, road1): par_merge(road0, road1) # for road in par_road: # par_check(road) save_par('./road_new/par.txt', par_road)