将 2D 中的障碍物坐标映射到无向格图

问题描述

上下文:我想对移动探测器进行最短路径搜索,源系统在 2D 笛卡尔坐标中运行,即 20 m x 10 m 空间。使用 igraph for python,我在这些维度上创建了一个格子图,顶点对应于检测器可以到达的坐标和连接相邻顶点的边。下图是使用 igraph

中的 layout_grid 方法创建的

enter image description here

我想在类似于下图的空间中添加一个简单的矩形障碍物,并对应于格子图:

https://alienryderflex.com/shortest_path/

问题:我知道需要使用 delete_vertices 方法去除与障碍物的 2D 坐标对应的顶点和边,但我不确定如何映射 2D格子图中顶点的障碍物坐标。任何建议表示赞赏。

示例代码

import numpy as np
from igraph import Graph
#Rectangular obstruction
obstruction = [[5,0],[10,5],[5,5]]
#Create graph
g = Graph().Lattice([20.0,10.0],nei=1,circular=False)
g.delete_vertices([***obstruction coordinates***])
#Coordinates to 2D grid
coords = np.asarray(g.layout_grid(width=int(search_area[2][0])).coords)

解决方法

原始 igraph 文档 describes how they are labeled。如果 x 是列,y 是行,h 是点阵的高度(第二维),以下函数将点阵中的坐标转换为顶点 ID。>

def coordinate_to_id(x,y,h):
    """Convert coorindate (x,y) to ID for a lattice of height h."""
    return y * w + x

请注意,您必须以 igraph will update its internal identifiers 的形式一次性删除所有顶点。