在 Python 中沿路径插值/移动对象

问题描述 投票:0回答:1

我试图沿着恒定速度的路径移动物体。我有一组使用 networkx 的节点,并获得了这些节点之间的最短路径。最短路径并不平滑或依赖于时间。我想对路径进行插值或移动,以便对象以 dt = 1/3600 和恒定速度通过路径移动,从而更新沿路径的位置。我有一些示例/伪代码:

def func(self):
    distance = 0

    for i in range(len(nodes)-1):
        shortest_path = nx.shortest_path(G_full, source=nodes[i], target=nodes[i+1], weight='weight')
        
        node_dist = 0

        for j in range(len(shortest_path)-1):
            node_dist += geodesic(shortest_path[j], shortest_path[j+1]).kilometers
            distance += geodesic(shortest_path[j], shortest_path[j+1]).kilometers
    return node_dist, distance

这给了我节点列表之间的最短路径,例如节点: [(47.6835508, 6.4912519), (47.9078655, 6.3314071), (48.0126858, 6.6045816)]

我尝试迭代每个节点的最短路径,但陷入了如何设置路径更新的困境。

python interpolation
1个回答
0
投票

为了实现平滑插值并以恒定速度沿路径移动对象,可以在连续节点之间使用线性插值。您需要计算路径的总距离,然后根据所需的时间步长 (

dt
) 和恒定速度确定中间位置。

这里有一个示例/伪代码可以帮助您实现此目的:

从 geopy.distance 导入测地线

路径插值器类: def init(自身,节点,G_full): self.nodes = 节点 self.G_full = G_full self.distance, self.total_distance = self.calculate_distances()

def calculate_distances(self):
    distance = 0
    total_distance = 0

    for i in range(len(self.nodes) - 1):
        shortest_path = nx.shortest_path(
            self.G_full, source=self.nodes[i], target=self.nodes[i + 1], weight="weight"
        )

        for j in range(len(shortest_path) - 1):
            total_distance += geodesic(
                shortest_path[j], shortest_path[j + 1]
            ).kilometers

    return distance, total_distance

def interpolate_path(self, dt=1/3600, constant_velocity=10):
    current_distance = 0

    while current_distance < self.total_distance:
        current_distance += constant_velocity * dt
        current_position = self.get_position_at_distance(current_distance)
        print("Current Position:", current_position)
        # You can do something with the current_position, such as updating the object's position.

def get_position_at_distance(self, target_distance):
    current_distance = 0

    for i in range(len(self.nodes) - 1):
        shortest_path = nx.shortest_path(
            self.G_full, source=self.nodes[i], target=self.nodes[i + 1], weight="weight"
        )

        for j in range(len(shortest_path) - 1):
            segment_distance = geodesic(
                shortest_path[j], shortest_path[j + 1]
            ).kilometers

            if current_distance + segment_distance >= target_distance:
                ratio = (target_distance - current_distance) / segment_distance
                lat = shortest_path[j][0] + ratio * (
                    shortest_path[j + 1][0] - shortest_path[j][0]
                )
                lon = shortest_path[j][1] + ratio * (
                    shortest_path[j + 1][1] - shortest_path[j][1]
                )
                return lat, lon

            current_distance += segment_distance

使用示例:

节点 = [(47.6835508, 6.4912519), (47.9078655, 6.3314071), (48.0126858, 6.6045816)] G_full = your_networkx_graph # 替换为你的实际图表

path_interpolator = PathInterpolator(节点,G_full) path_interpolator.interpolate_path()


This code defines a `PathInterpolator` class that calculates the total distance of the path and provides a method for interpolating the path at a given time step (`dt`) with a constant velocity. The `get_position_at_distance` method is responsible for returning the interpolated position based on the current distance along the path.
© www.soinside.com 2019 - 2024. All rights reserved.