python - Mapping obstructions coordinates in 2D to undirected Lattice graph
问题描述
Context: I want to do shortest path search for a mobile detector, source system operating in 2D Cartesian coordinates i.e. a 20 m x 10 m space. Using igraph for python, I have created a Lattice graph over these dimensions with vertices corresponding to coordinates that the detector can travel to and edges connecting neighboring vertices. The figure below is created using the layout_grid
method from igraph
I want to add a simple rectangular obstruction to the space similar to the figure below and correspondingly to the Lattice graph :
Problem: I know that the vertices and edges corresponding to the 2D coordinates of the obstruction need to be removed using the delete_vertices
method but I'm not sure how to map the 2D obstruction coordinates to the vertices in the Lattice graph. Any suggestions are appreciated.
Example code:
import numpy as np
from igraph import Graph
#Rectangular obstruction
obstruction = [[5, 0], [10, 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文档描述了它们是如何标记的。如果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将更新其内部标识符。
推荐阅读
- asp.net - IIS - 如何将域名屏蔽到另一个域中的特定页面
- bluetooth-lowenergy - BLE STM BlueNrg 特征最大尺寸和每个服务的最大特征
- javascript - Javascript点击位置关闭
- java - 关于并发访问完成时原子布尔值的奇怪行为的问题
- ios - 使用 AR Foundation 检测图像中的人脸
- mysql-workbench - mysql新用户访问数据库被拒绝
- javascript - IE11 不能使用 import * as XXX from XXX
- druid - 如何在apache druid的摄取规范中获取具有最大时间戳的最后一个值?
- r - gsub() 对具有多个替换的数据框中的所有值
- autodesk-forge - OssContriller 无法将文件上传到 BIM 360