delta = [abs(pos-coord) for pos in positions]
时间: 2024-04-01 18:37:17 浏览: 17
这段代码使用列表推导式,计算了一个列表 `delta`,其中每个元素都是 `positions` 列表中的元素与 `coord` 的差的绝对值。具体来说,`for` 循环遍历了 `positions` 列表中的所有元素 `pos`,然后计算了 `pos-coord` 的绝对值,将结果添加到 `delta` 列表中。最终,`delta` 列表中的元素个数与 `positions` 列表中的元素个数相同。
相关问题
from_coord = coords[0] to_coord = coords[-1]
这段代码中,`coords` 是一个列表,`from_coord` 变量被赋值为 `coords` 列表的第一个元素,即 `coords[0]`;而 `to_coord` 变量被赋值为 `coords` 列表的最后一个元素,即 `coords[-1]`。这种方式获取列表的第一个和最后一个元素的方法可以应用于任何长度的列表。
代码每句话多啥意思// if ( num_iter < 10000 ) // cout << "current=" << current->index.transpose() << endl; if (current->index(0) == endPtr->index(0) && current->index(1) == endPtr->index(1) && current->index(2) == endPtr->index(2))判断当前节点是否为终点。 { // ros::Time time_2 = ros::Time::now(); // printf("\033[34mA star iter:%d, time:%.3f\033[0m\n",num_iter, (time_2 - time_1).toSec()*1000); // if((time_2 - time_1).toSec() > 0.1) // ROS_WARN("Time consume in A star path finding is %f", (time_2 - time_1).toSec() ); gridPath_ = retrievePath(current); return true; } current->state = GridNode::CLOSEDSET; //move current node from open set to closed set. for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) for (int dz = -1; dz <= 1; dz++) { if (dx == 0 && dy == 0 && dz == 0) continue; Vector3i neighborIdx; neighborIdx(0) = (current->index)(0) + dx; neighborIdx(1) = (current->index)(1) + dy; neighborIdx(2) = (current->index)(2) + dz; if (neighborIdx(0) < 1 || neighborIdx(0) >= POOL_SIZE_(0) - 1 || neighborIdx(1) < 1 || neighborIdx(1) >= POOL_SIZE_(1) - 1 || neighborIdx(2) < 1 || neighborIdx(2) >= POOL_SIZE_(2) - 1) { continue; } neighborPtr = GridNodeMap_[neighborIdx(0)][neighborIdx(1)][neighborIdx(2)]; neighborPtr->index = neighborIdx; bool flag_explored = neighborPtr->rounds == rounds_; if (flag_explored && neighborPtr->state == GridNode::CLOSEDSET) { continue; //in closed set. } neighborPtr->rounds = rounds_; if (checkOccupancy(Index2Coord(neighborPtr->index))) { continue; } double static_cost = sqrt(dx * dx + dy * dy + dz * dz); tentative_gScore = current->gScore + static_cost;
好的,我来解释一下每个语句的作用:
1. `if ( num_iter < 10000 ) cout << "current=" << current->index.transpose() << endl;`:这个语句用于在前10000次迭代中输出当前节点的索引,以便调试。
2. `if (current->index(0) == endPtr->index(0) && current->index(1) == endPtr->index(1) && current->index(2) == endPtr->index(2))`:这个语句用于判断当前节点是否为终点。
3. `gridPath_ = retrievePath(current);`:这个语句用于从起点到终点的路径,通过调用`retrievePath`函数来实现。
4. `current->state = GridNode::CLOSEDSET;`:这个语句用于将当前节点从开放集合移动到关闭集合。
5. `for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) for (int dz = -1; dz <= 1; dz++)`:这个语句用于遍历当前节点周围的所有相邻节点。
6. `if (dx == 0 && dy == 0 && dz == 0) continue;`:这个语句用于跳过当前节点本身。
7. `Vector3i neighborIdx; neighborIdx(0) = (current->index)(0) + dx; neighborIdx(1) = (current->index)(1) + dy; neighborIdx(2) = (current->index)(2) + dz;`:这个语句用于获取当前相邻节点的索引。
8. `if (neighborIdx(0) < 1 || neighborIdx(0) >= POOL_SIZE_(0) - 1 || neighborIdx(1) < 1 || neighborIdx(1) >= POOL_SIZE_(1) - 1 || neighborIdx(2) < 1 || neighborIdx(2) >= POOL_SIZE_(2) - 1) { continue; }`:这个语句用于判断当前相邻节点是否在地图范围内。
9. `neighborPtr = GridNodeMap_[neighborIdx(0)][neighborIdx(1)][neighborIdx(2)];`:这个语句用于获取当前相邻节点的指针。
10. `bool flag_explored = neighborPtr->rounds == rounds_;`:这个语句用于判断当前相邻节点是否已经被探索。
11. `if (flag_explored && neighborPtr->state == GridNode::CLOSEDSET) { continue; }`:这个语句用于跳过已经在关闭集合中的相邻节点。
12. `neighborPtr->rounds = rounds_;`:这个语句用于标记当前相邻节点已经被探索。
13. `if (checkOccupancy(Index2Coord(neighborPtr->index))) { continue; }`:这个语句用于跳过障碍物。
14. `double static_cost = sqrt(dx * dx + dy * dy + dz * dz);`:这个语句用于计算从当前节点到当前相邻节点的代价。
15. `tentative_gScore = current->gScore + static_cost;`:这个语句用于计算从起点到当前相邻节点的代价。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)