Cut_Line_Algorithm.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import sys
  2. # 从文件读取一组节点
  3. nodes = []
  4. with open(r"C:\TSP\CDR_TO_TSP", "r") as file:
  5. nodes = file.readlines()
  6. # 删除第一个元素(源数据总数)
  7. nodes = nodes[1:]
  8. # 删除最后一个元素 换行(\n)
  9. nodes = nodes[:-1]
  10. # 定义一个函数来对节点进行排序和删除重复
  11. def sort_and_remove_duplicates(nodes):
  12. sorted_nodes = sorted(nodes) # 按照默认的元素顺序排序
  13. unique_nodes = [sorted_nodes[0]] # 保留第一个元素
  14. for node in sorted_nodes[1:]:
  15. # 如果当前节点与前一个节点不相同,则将其添加到列表中
  16. if node != unique_nodes[-1]:
  17. unique_nodes.append(node)
  18. return unique_nodes
  19. # 对节点进行排序和删除重复,写回文件
  20. unique_nodes = sort_and_remove_duplicates(nodes)
  21. total = len(unique_nodes)
  22. points = []
  23. for i in range(total):
  24. pot = unique_nodes[i].split()
  25. node = float(pot[0]), float(pot[1])
  26. points.append(node)
  27. # print(points)
  28. # boundary 是一个四元组,表示这组点的边界
  29. x_min = min(point[0] for point in points)
  30. x_max = max(point[0] for point in points)
  31. y_min = min(point[1] for point in points)
  32. y_max = max(point[1] for point in points)
  33. boundary = (x_min, x_max, y_min, y_max)
  34. print(boundary)
  35. # near_boundary_points将会是一个列表,包含所有距离边界不超过1的点
  36. near_boundary_points = []
  37. for point in points:
  38. if (
  39. abs(point[0] - x_min) <= 1
  40. or abs(point[0] - x_max) <= 1
  41. or abs(point[1] - y_min) <= 1
  42. or abs(point[1] - y_max) <= 1
  43. ):
  44. near_boundary_points.append(point)
  45. # print(near_boundary_points)
  46. tuples_list = near_boundary_points
  47. # 对元组列表分别按第二个元素进行升序排序 再第一个元素进行升序排序
  48. sorted_tuples = sorted(tuples_list, key=lambda x: x[1])
  49. near_boundary_points = sorted(sorted_tuples, key=lambda x: x[0])
  50. # 输出排序后的元组列表
  51. # print(near_boundary_points)
  52. # 移除相邻点
  53. def remove_adjacent(nodes):
  54. threshold = 0.5 # 设定阈值
  55. for i, node in enumerate(nodes):
  56. remove_indices = []
  57. for j, other_node in enumerate(nodes[i+1:]):
  58. distance = ((node[0]-other_node[0])**2 + (node[1]-other_node[1])**2)**0.5
  59. if distance < threshold:
  60. remove_indices.append(i+j+1) # 记录需要移除的点的索引
  61. for index in sorted(remove_indices, reverse=True):
  62. del nodes[index] # 移除邻近的点
  63. return nodes
  64. near_boundary_points = remove_adjacent(near_boundary_points)
  65. # 统计 x_min和y_max邻近的坐标点个数, 也就是求边界框左边 节点个数
  66. left_points = []
  67. top_points = []
  68. for x, y in near_boundary_points:
  69. if abs(x - x_min) <= 0.5:
  70. left_points.append((x, y))
  71. if abs(y - y_max) <= 0.5:
  72. top_points.append((x, y))
  73. left_points.reverse()
  74. print(left_points)
  75. if len(left_points) % 2 :
  76. top_points.reverse() # 控制竖线从右往左
  77. print(top_points)
  78. # 把裁切线节点 写文件 TSP2.tx ,完成算法
  79. total = len(left_points) + len(top_points)
  80. f = open(r"C:\TSP\TSP2.txt", "w")
  81. line = "%d %d\n" % (total, total)
  82. f.write(line)
  83. ext = 3 # extend 延长线, 默认值 3mm
  84. if len(sys.argv) > 1:
  85. ext = float(sys.argv[1])
  86. inverter = 1 # 交流频率控制
  87. for x, y in left_points:
  88. if inverter == 1:
  89. line = "%f %f %f %f\n" % (x - ext, y, x_max + ext, y)
  90. else:
  91. line = "%f %f %f %f\n" % (x_max + ext, y, x - ext, y)
  92. f.write(line)
  93. inverter = (inverter + 1) % 2
  94. for x, y in top_points:
  95. if inverter == len(left_points) % 2: # 控制竖线从下面往上
  96. line = "%f %f %f %f\n" % (x, y + ext, x, y_min - ext)
  97. else:
  98. line = "%f %f %f %f\n" % (x, y_min - ext, x, y + ext)
  99. f.write(line)
  100. inverter = (inverter + 1) % 2