当前位置:   article > 正文

自动驾驶算法(一):Dijkstra算法讲解与代码实现_实现自动驾驶的代码

实现自动驾驶的代码

目录

0 本节关键词:栅格地图、算法、路径规划

1 Dijkstra算法详解

2 Dijkstra代码详解


0 本节关键词:栅格地图、算法、路径规划

1 Dijkstra算法详解

        用于图中寻找最短路径。节点是地点,边是权重。

        从起点开始逐步扩展,每一步为一个节点找到最短路径:

        While True:
                1.从未访问的节点选择距离最小的节点收录(贪心思想)
                2.收录节点后遍历该节点的邻接节点,更新距离

        我们举例子说明一下,在机器人路径规划中,通常用open list、closed list表达:

        open list 表示从该节点到起点已经有路径的节点

        closed list 表示已经找到最短路径的节点

        Step1:从起点开始,将起点放入open list中,选择距离最短的节点进行收录。

  1. open list 1(0)(min)
  2. closed list
  3. |
  4. |
  5. open list
  6. closed list 1(0)

        Step2:遍历1号节点的邻接节点(4、2号节点)

  1. open list 2(2)(1-->2) 4(1)(1-->4)(min)
  2. closed list 1(0)
  3. |
  4. |
  5. open list 2(2)(1-->2)
  6. closed list 1(0) 4(1)(1-->4)

        4号节点收录后我们需要对其邻接节点更新距离。(3、6、7号节点)

        Step3:3号节点我们找到1->4->3路径,6号节点我们找到1->4->6路径,7号节点我们找到1->4->7路径。

  1. open list 2(2)(1-->2)(min) 3(3)(1-->4-->3) 6(9)(1-->4-->6) 7(5)(1-->4-->7)
  2. closed list 1(0)
  3. |
  4. |
  5. open list 3(3)(1-->4-->3) 6(9)(1-->4-->6) 7(5)(1-->4-->7)
  6. closed list 1(0) 4(1)(1-->4) 2(2)(1-->2)

        Step4:遍历2的邻接节点,我们发现4号节点已经在close list中(不需要被更新),我们更新5号节点。

  1. open list 3(3)(1-->4-->3)(min) 6(9)(1-->4-->6) 7(5)(1-->4-->7) 5(13)(1->2-->5)
  2. closed list 1(0)
  3. |
  4. |
  5. open list 6(9)(1-->4-->6) 7(5)(1-->4-->7) 5(13)(1->2-->5)
  6. closed list 1(0) 4(1)(1-->4) 2(2)(1-->2) 3(3)(1-->4-->3)

        Step5:遍历3的邻接节点,(3-->1无需更新,更新3-->6  1436(8) (因为我们有到3的最短距离)),而我们已经为6号节点找到路径6(9)(146),更新6号节点的路径。

  1. open list 6(9)(1-->4-->6)(1->4->3-->6 7) 7(5)(1-->4-->7)(min) 5(13)(1->2-->5)
  2. closed list 1(0) 4(1)(1-->4) 2(2)(1-->2) 3(3)(1-->4-->3)
  3. |
  4. |
  5. open list 6(8)(1->4->3-->6) 5(13)(1->2-->5)
  6. closed list 1(0) 4(1)(1-->4) 2(2)(1-->2) 3(3)(1-->4-->3) 7(5)(1-->4-->7)

        Step6:遍历7的邻接节点(6号节点)(1 4 7 6 = 6)比之前的8小,对6号距离再次更新。

  1. open list 6(8)(1->4->3-->6)(1->4->7-->6 6)(min) 5(13)(1->2-->5)
  2. closed list 1(0) 4(1)(1-->4) 2(2)(1-->2) 3(3)(1-->4-->3) 7(5)(1-->4-->7)
  3. |
  4. |
  5. open list 5(13)(1->2-->5)
  6. closed list 1(0) 4(1)(1-->4) 2(2)(1-->2) 3(3)(1-->4-->3) 7(5)(1-->4-->7) 6(6)(1-->4-->7-->6)

        Step7:遍历6的邻接节点(6号节点)结束

        栅格地图初介绍:

        假设图中灰色的是障碍物,红色的是机器人。在避障时,我们的常用做法是通过膨胀障碍物,将机器人视为质点来规划路径,然后对地图进行栅格化,将地图弄成一块一块的。最后将栅格地图转化为有权地图。我们可以把栅格地图的每个栅格看作是有权图的节点,机器人的运动范围可以看作是有权图的节点和节点之间的连接。

2 Dijkstra代码详解

        这里我们先配置下代码环境,最好是在python3.9下,我们创建conda虚拟环境:

conda create -n nav python=3.9

        安装所需库:

pip install numpy scipy matplotlib pandas cvxpy pytest -i https://pypi.tuna.tsinghua.edu.cn/simple

        我们的代码如下:

  1. """
  2. Grid based Dijkstra planning
  3. author: Atsushi Sakai(@Atsushi_twi)
  4. """
  5. import matplotlib.pyplot as plt
  6. import math
  7. show_animation = True
  8. class Dijkstra:
  9. def __init__(self, ox, oy, resolution, robot_radius):
  10. """
  11. Initialize map for planning
  12. ox: x position list of Obstacles [m]
  13. oy: y position list of Obstacles [m]
  14. resolution: grid resolution [m]
  15. rr: robot radius[m]
  16. """
  17. self.min_x = None
  18. self.min_y = None
  19. self.max_x = None
  20. self.max_y = None
  21. self.x_width = None
  22. self.y_width = None
  23. self.obstacle_map = None
  24. self.resolution = resolution
  25. self.robot_radius = robot_radius
  26. self.calc_obstacle_map(ox, oy)
  27. self.motion = self.get_motion_model()
  28. class Node:
  29. def __init__(self, x, y, cost, parent_index):
  30. self.x = x # index of grid
  31. self.y = y # index of grid
  32. self.cost = cost # g(n)
  33. self.parent_index = parent_index # index of previous Node
  34. def __str__(self):
  35. return str(self.x) + "," + str(self.y) + "," + str(
  36. self.cost) + "," + str(self.parent_index)
  37. def planning(self, sx, sy, gx, gy):
  38. """
  39. dijkstra path search
  40. input:
  41. s_x: start x position [m]
  42. s_y: start y position [m]
  43. gx: goal x position [m]
  44. gx: goal x position [m]
  45. output:
  46. rx: x position list of the final path
  47. ry: y position list of the final path
  48. """
  49. start_node = self.Node(self.calc_xy_index(sx, self.min_x),
  50. self.calc_xy_index(sy, self.min_y), 0.0, -1) # round((position - minp) / self.resolution)
  51. goal_node = self.Node(self.calc_xy_index(gx, self.min_x),
  52. self.calc_xy_index(gy, self.min_y), 0.0, -1)
  53. open_set, closed_set = dict(), dict() # key - value: hash表
  54. open_set[self.calc_index(start_node)] = start_node
  55. while 1:
  56. c_id = min(open_set, key=lambda o: open_set[o].cost) # 取cost最小的节点
  57. current = open_set[c_id]
  58. # show graph
  59. if show_animation: # pragma: no cover
  60. plt.plot(self.calc_position(current.x, self.min_x),
  61. self.calc_position(current.y, self.min_y), "xc")
  62. # for stopping simulation with the esc key.
  63. plt.gcf().canvas.mpl_connect(
  64. 'key_release_event',
  65. lambda event: [exit(0) if event.key == 'escape' else None])
  66. if len(closed_set.keys()) % 10 == 0:
  67. plt.pause(0.001)
  68. # 判断是否是终点
  69. if current.x == goal_node.x and current.y == goal_node.y:
  70. print("Find goal")
  71. goal_node.parent_index = current.parent_index
  72. goal_node.cost = current.cost
  73. break
  74. # Remove the item from the open set
  75. del open_set[c_id]
  76. # Add it to the closed set
  77. closed_set[c_id] = current
  78. # expand search grid based on motion model
  79. for move_x, move_y, move_cost in self.motion:
  80. node = self.Node(current.x + move_x,
  81. current.y + move_y,
  82. current.cost + move_cost, c_id)
  83. n_id = self.calc_index(node)
  84. if n_id in closed_set:
  85. continue
  86. if not self.verify_node(node):
  87. continue
  88. if n_id not in open_set:
  89. open_set[n_id] = node # Discover a new node
  90. else:
  91. if open_set[n_id].cost >= node.cost:
  92. # This path is the best until now. record it!
  93. open_set[n_id] = node
  94. rx, ry = self.calc_final_path(goal_node, closed_set)
  95. return rx, ry
  96. def calc_final_path(self, goal_node, closed_set):
  97. # generate final course
  98. rx, ry = [self.calc_position(goal_node.x, self.min_x)], [
  99. self.calc_position(goal_node.y, self.min_y)]
  100. parent_index = goal_node.parent_index
  101. while parent_index != -1:
  102. n = closed_set[parent_index]
  103. rx.append(self.calc_position(n.x, self.min_x))
  104. ry.append(self.calc_position(n.y, self.min_y))
  105. parent_index = n.parent_index
  106. return rx, ry
  107. def calc_position(self, index, minp):
  108. pos = index * self.resolution + minp
  109. return pos
  110. def calc_xy_index(self, position, minp):
  111. return round((position - minp) / self.resolution)
  112. def calc_index(self, node):
  113. return node.y * self.x_width + node.x
  114. def verify_node(self, node):
  115. px = self.calc_position(node.x, self.min_x)
  116. py = self.calc_position(node.y, self.min_y)
  117. if px < self.min_x:
  118. return False
  119. if py < self.min_y:
  120. return False
  121. if px >= self.max_x:
  122. return False
  123. if py >= self.max_y:
  124. return False
  125. if self.obstacle_map[node.x][node.y]:
  126. return False
  127. return True
  128. def calc_obstacle_map(self, ox, oy):
  129. ''' 第1步:构建栅格地图 '''
  130. self.min_x = round(min(ox))
  131. self.min_y = round(min(oy))
  132. self.max_x = round(max(ox))
  133. self.max_y = round(max(oy))
  134. print("min_x:", self.min_x)
  135. print("min_y:", self.min_y)
  136. print("max_x:", self.max_x)
  137. print("max_y:", self.max_y)
  138. self.x_width = round((self.max_x - self.min_x) / self.resolution)
  139. self.y_width = round((self.max_y - self.min_y) / self.resolution)
  140. print("x_width:", self.x_width)
  141. print("y_width:", self.y_width)
  142. # obstacle map generation
  143. # 初始化地图
  144. self.obstacle_map = [[False for _ in range(self.y_width)]
  145. for _ in range(self.x_width)]
  146. # 设置障碍物
  147. for ix in range(self.x_width):
  148. x = self.calc_position(ix, self.min_x)
  149. for iy in range(self.y_width):
  150. y = self.calc_position(iy, self.min_y)
  151. for iox, ioy in zip(ox, oy):
  152. d = math.hypot(iox - x, ioy - y)
  153. if d <= self.robot_radius:
  154. self.obstacle_map[ix][iy] = True
  155. break
  156. @staticmethod
  157. def get_motion_model():
  158. # dx, dy, cost
  159. motion = [[1, 0, 1],
  160. [0, 1, 1],
  161. [-1, 0, 1],
  162. [0, -1, 1],
  163. [-1, -1, math.sqrt(2)],
  164. [-1, 1, math.sqrt(2)],
  165. [1, -1, math.sqrt(2)],
  166. [1, 1, math.sqrt(2)]]
  167. return motion
  168. def main():
  169. # start and goal position
  170. sx = -5.0 # [m]
  171. sy = -5.0 # [m]
  172. gx = 50.0 # [m]
  173. gy = 50.0 # [m]
  174. grid_size = 2.0 # [m]
  175. robot_radius = 1.0 # [m]
  176. # set obstacle positions
  177. ox, oy = [], []
  178. for i in range(-10, 60):
  179. ox.append(i)
  180. oy.append(-10.0)
  181. for i in range(-10, 60):
  182. ox.append(60.0)
  183. oy.append(i)
  184. for i in range(-10, 61):
  185. ox.append(i)
  186. oy.append(60.0)
  187. for i in range(-10, 61):
  188. ox.append(-10.0)
  189. oy.append(i)
  190. for i in range(-10, 40):
  191. ox.append(20.0)
  192. oy.append(i)
  193. for i in range(0, 40):
  194. ox.append(40.0)
  195. oy.append(60.0 - i)
  196. if show_animation: # pragma: no cover
  197. plt.plot(ox, oy, ".k")
  198. plt.plot(sx, sy, "og")
  199. plt.plot(gx, gy, "xb")
  200. plt.grid(True)
  201. plt.axis("equal")
  202. dijkstra = Dijkstra(ox, oy, grid_size, robot_radius)
  203. rx, ry = dijkstra.planning(sx, sy, gx, gy)
  204. if show_animation: # pragma: no cover
  205. plt.plot(rx, ry, "-r")
  206. plt.pause(0.01)
  207. plt.show()
  208. if __name__ == '__main__':
  209. main()
'
运行

        先执行一下看看效果:

        我们现在来详解一下:

        我们从main函数开始:

  1. # 1. 设置起点和终点
  2. sx = -5.0 # [m]
  3. sy = -5.0 # [m]
  4. gx = 50.0 # [m]
  5. gy = 50.0 # [m]
  6. # 2. 设置珊格的大小和机器人的半径
  7. grid_size = 2.0 # [m]
  8. robot_radius = 1.0 # [m]
  9. # 3. 设置障碍物的位置(图中的黑点就是)
  10. ox, oy = [], []
  11. # 3.1 设置外围的四堵墙 (-10,-10) --> (60,-10) 最下面的一条线
  12. for i in range(-10, 60):
  13. ox.append(i)
  14. oy.append(-10.0)
  15. # 3.1 设置外围的四堵墙 (60,-10) --> (60,60) 最右面的一条线
  16. for i in range(-10, 60):
  17. ox.append(60.0)
  18. oy.append(i)
  19. # 3.1 设置外围的四堵墙 (-10,60) --> (61,60) 最上面的一条线
  20. for i in range(-10, 61):
  21. ox.append(i)
  22. oy.append(60.0)
  23. # 3.1 设置外围的四堵墙 (-10,-10) --> (-10,61) 最左面的一条线
  24. for i in range(-10, 61):
  25. ox.append(-10.0)
  26. oy.append(i)
  27. # 3.2 障碍物
  28. for i in range(-10, 40):
  29. ox.append(20.0)
  30. oy.append(i)
  31. for i in range(0, 40):
  32. ox.append(40.0)
  33. oy.append(60.0 - i)
  34. # 4 画图 起点、终点、障碍物都画出来
  35. if show_animation: # pragma: no cover
  36. plt.plot(ox, oy, ".k")
  37. plt.plot(sx, sy, "og")
  38. plt.plot(gx, gy, "xb")
  39. plt.grid(True)
  40. plt.axis("equal")

        这段代码就设置了边框和障碍物区域并把他们可视化了:

        就是我图中画的区域。

  1. #生成了Dijkstra的对象 调用其中的方法(障碍物信息、珊格大小、机器人半径)
  2. dijkstra = Dijkstra(ox, oy, grid_size, robot_radius)

        生成了Dijkstra的对象。我们来看这个类的构造函数,进入一个类首先执行构造函数:

  1. def __init__(self, ox, oy, resolution, robot_radius):
  2. """
  3. Initialize map for planning
  4. ox: x position list of Obstacles [m]
  5. oy: y position list of Obstacles [m]
  6. resolution: grid resolution [m]
  7. rr: robot radius[m]
  8. """
  9. self.min_x = None
  10. self.min_y = None
  11. self.max_x = None
  12. self.max_y = None
  13. self.x_width = None
  14. self.y_width = None
  15. self.obstacle_map = None
  16. # 珊格大小
  17. self.resolution = resolution
  18. # 机器人半径
  19. self.robot_radius = robot_radius
  20. # 构建珊格地图
  21. self.calc_obstacle_map(ox, oy)
  22. self.motion = self.get_motion_model()
'
运行

        我们先来看是怎么创建珊格地图的:

  1. def calc_obstacle_map(self, ox, oy):
  2. ''' 第1步:构建栅格地图 '''
  3. # 1. 获得地图的边界值
  4. self.min_x = round(min(ox))
  5. self.min_y = round(min(oy))
  6. self.max_x = round(max(ox))
  7. self.max_y = round(max(oy))
  8. print("min_x:", self.min_x)
  9. print("min_y:", self.min_y)
  10. print("max_x:", self.max_x)
  11. print("max_y:", self.max_y)
  12. # 2.计算x、y方向珊格个数
  13. self.x_width = round((self.max_x - self.min_x) / self.resolution)
  14. self.y_width = round((self.max_y - self.min_y) / self.resolution)
  15. print("x_width:", self.x_width)
  16. print("y_width:", self.y_width)
  17. # obstacle map generation
  18. # 3.初始化地图 都设置为false 表示还没有设置障碍物
  19. self.obstacle_map = [[False for _ in range(self.y_width)]
  20. for _ in range(self.x_width)]
  21. # 4.设置障碍物 遍历每一个栅格
  22. for ix in range(self.x_width):
  23. # 通过下标计算珊格位置
  24. x = self.calc_position(ix, self.min_x)
  25. for iy in range(self.y_width):
  26. y = self.calc_position(iy, self.min_y)
  27. # 遍历障碍物
  28. for iox, ioy in zip(ox, oy):
  29. # 计算障碍物到珊格的距离
  30. d = math.hypot(iox - x, ioy - y)
  31. # 膨胀障碍物 如果距离比机器人半径小 机器人不能通行
  32. if d <= self.robot_radius:
  33. # 设置为true
  34. self.obstacle_map[ix][iy] = True
  35. break
'
运行

        首先我们获得了地图的边界值,算出了每一个方向上有多少珊格数量。

        比如我们的长是100m(self.max_x - self.min_x = 100),珊格大小为3,那么我们每一行不就是有33个珊格啦~。

        我们初始化obstacle_map,这个大小为珊格长 * 珊格宽的大小,我们将他们初始化为false表示这个地方没有障碍物。

        然后我们遍历每一个珊格for ix in range(self.x_width)、for iy in range(self.y_width)。我们来看看calc_position这个方法做了什么。

  1. def calc_position(self, index, minp):
  2. pos = index * self.resolution + minp
  3. return pos
'
运行

        其实就计算了珊格所在位置的真实(x,y)坐标,比如我们的self.minx = 10,ix = 0,那么他的pos = 0 * 2 + 10 = 10,比如我们的self.minx = 10,ix = 1,那么他的pos = 1 * 2 + 10 = 12。我们遍历所有障碍物体的坐标,计算障碍物体(真实坐标)与这个机器人的距离,如果这个距离比机器人自身的大小小的话,我们将这个地方的珊格标志置为false表示有东西。

        那么,在完成这个函数calc_obstacle_map时候,我们有了一张珊格地图,里面充斥着false和true,如果为true的话,那么机器人是过不去的,这块也就是设置成了障碍物区域。

        我们接着往下看构造函数:

  1. self.calc_obstacle_map(ox, oy)
  2. self.motion = self.get_motion_model()

         self.motion = self.get_motion_model()这段代码建立了机器人的运动模型和运动代价:

  1. def get_motion_model():
  2. # dx, dy, cost
  3. motion = [[1, 0, 1], #x增加1,y不变 代价为1
  4. [0, 1, 1],
  5. [-1, 0, 1],
  6. [0, -1, 1],
  7. [-1, -1, math.sqrt(2)],
  8. [-1, 1, math.sqrt(2)],
  9. [1, -1, math.sqrt(2)],
  10. [1, 1, math.sqrt(2)]]
  11. return motion
'
运行

        这里也就是机器人向左走(x+1,y+0)代价为1,斜着走代价为根号2。到此为止,我们构造函数讲解完了。我们返回主函数。

open_set

        这里开始正式进入路径规划了。传入的参数为起点坐标和终点坐标:

自动驾驶算法(一):Dijkstra算法讲解与代码实现

        我们先看下node类。

  1. class Node:
  2. def __init__(self, x, y, cost, parent_index):
  3. self.x = x # index of grid
  4. self.y = y # index of grid
  5. self.cost = cost # g(n)
  6. self.parent_index = parent_index # index of previous Node
  7. def __str__(self):
  8. return str(self.x) + "," + str(self.y) + "," + str(
  9. self.cost) + "," + str(self.parent_index)
'
运行

        首先执行构造函数,我们发现就是把珊格的(x,y)坐标(并非真实坐标是珊格的)还有cost(后文说)以及父节点的ID赋值了。(so easy)        

        我们在看一下calc_xy_index函数:

  1. def calc_xy_index(self, position, minp):
  2. return round((position - minp) / self.resolution)
'
运行

        它就是计算出真实世界的点点属于哪一个珊格的某一维度的坐标,我们举个例子:

self.calc_xy_index(sx, self.min_x)  sx = 30  minx = 20

        这就代表我们的地图边界的 x 坐标为20,这个点的坐标x=30,我们用(30-20)/2 = 5,那么这个珊格坐标的x方向的坐标就是5。

  1. start_node = self.Node(self.calc_xy_index(sx, self.min_x),
  2. self.calc_xy_index(sy, self.min_y), 0.0, -1) # round((position - minp) / self.resolution)
  3. goal_node = self.Node(self.calc_xy_index(gx, self.min_x),
  4. self.calc_xy_index(gy, self.min_y), 0.0, -1)

        因此,这段代码的含义就是我们计算出了起始和终止点的珊格坐标,并且将代价置为0,且他们的父节点为-1(没有父亲节点)。封装成了node。

        下面进入算法部分,我们看流程图:

        代码部分和流程图是一样的:

        1.首先我们把起点放入openlist中:

  1. # 设置openlist closelist 基于哈希表
  2. open_set, closed_set = dict(), dict() # key - value: hash表
  3. # 将startnode放进openset里面 索引为一维数组
  4. open_set[self.calc_index(start_node)] = start_node

        看一下calc_index函数:这里将珊格地图映射成了一个一维数组,返回数组的ID,类似C++中的二维数组降维。这里openset是一个字典,里面的key是珊格地图点的ID,value是这个珊格节点。

  1. def calc_index(self, node):
  2. return node.y * self.x_width + node.x

        2.while True进入循环

        while 1:
'
运行

        2.1 取openlist cost最小的节点作为当前节点

  1. c_id = min(open_set, key=lambda o: open_set[o].cost)
  2. current = open_set[c_id]

        2.2.1 判断当前是否为终点,如果是终点,如果是终点的话把终点的cost修改为当前点的cost值,且终点的父亲节点为当前点的父亲节点。

  1. # 判断是否是终点
  2. if current.x == goal_node.x and current.y == goal_node.y:
  3. print("Find goal")
  4. # 当前节点的信息赋值给终点
  5. goal_node.parent_index = current.parent_index
  6. goal_node.cost = current.cost
  7. break

        2.2.2 不是最终节点的话从openlist删除加入到closelist

  1. # 把当前节点从openset里面删掉
  2. del open_set[c_id]
  3. # 加入到closed set
  4. closed_set[c_id] = current

        2.3 遍历其9个邻接运动节点

            for move_x, move_y, move_cost in self.motion:

        2.3.1 封装邻接节点

  1. node = self.Node(current.x + move_x,
  2. current.y + move_y,
  3. current.cost + move_cost, c_id)

        这个点到邻接节点的移动就是 x +-( 1或-1或+根号2或-根号2),然后移动上下的话它的代价值需要+1,斜着移动需要 + 根号2,这样递归的进行我们就求出来所有点的代价值了,同样这个新走的点是通过我们这个点走过来的,因此新点的父节点就是我们这个点。

        2.3.2 求当前节点的一维索引判断是否收录到closelist并判断是否可行,如果收录了,那么已经有最小路径了不需要我们再去处理了,还需要判断这个节点是否在珊格地图标记为false点上(珊格地图就是这么用的....)如果这个地方有障碍物那么我们也走不了。

  1. # 求当前节点的key
  2. n_id = self.calc_index(node)
  3. # 是否已经收录到close set里面
  4. if n_id in closed_set:
  5. continue
  6. # 邻接节点是否可行
  7. if not self.verify_node(node):
  8. continue

        2.3.3 如果不在openset里面我们就将她作为一个新节点加入,如果在openset比较值是否是最优更新,是否和之前的最优路径有重叠。

  1. if n_id not in open_set:
  2. open_set[n_id] = node # Discover a new node
  3. else:
  4. if open_set[n_id].cost >= node.cost:
  5. # This path is the best until now. record it!
  6. open_set[n_id] = node

        到这里我们的算法就结束了。我们迭代找到最终点后算法就break掉了~。

        最后我们计算路径:

  1. def calc_final_path(self, goal_node, closed_set):
  2. # generate final course
  3. rx, ry = [self.calc_position(goal_node.x, self.min_x)], [
  4. self.calc_position(goal_node.y, self.min_y)]
  5. parent_index = goal_node.parent_index
  6. while parent_index != -1:
  7. n = closed_set[parent_index]
  8. rx.append(self.calc_position(n.x, self.min_x))
  9. ry.append(self.calc_position(n.y, self.min_y))
  10. parent_index = n.parent_index
  11. return rx, ry

        我们将最终节点的珊格坐标还原成真实的三维坐标,并向前找他们的父亲节点直到起始节点(parent_index= -1),我们就出来这个路径了。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号