【已解决】Python Bresenham 3D算法

放一段使用Python实现Bresenham 3D 算法的代码,并通过Matplot可视化

python 复制代码
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from numba import njit

@njit
def bresenham_safe(grid, x0, y0, z0, x1, y1, z1, value_to_fill):
    start_point = [int(x0), int(y0), int(z0)]
    end_point = [int(x1), int(y1), int(z1)]

    steep_xy = (abs(end_point[1] - start_point[1]) > abs(end_point[0] - start_point[0]))
    if steep_xy:
        start_point[0], start_point[1] = start_point[1], start_point[0]
        end_point[0], end_point[1] = end_point[1], end_point[0]

    steep_xz = (abs(end_point[2] - start_point[2]) > abs(end_point[0] - start_point[0]))
    if steep_xz:
        start_point[0], start_point[2] = start_point[2], start_point[0]
        end_point[0], end_point[2] = end_point[2], end_point[0]

    delta = [abs(end_point[0] - start_point[0]), abs(end_point[1] - start_point[1]), abs(end_point[2] - start_point[2])]

    error_xy = delta[0] / 2
    error_xz = delta[0] / 2

    step = [
        -1 if start_point[0] > end_point[0] else 1,
        -1 if start_point[1] > end_point[1] else 1,
        -1 if start_point[2] > end_point[2] else 1
    ]

    y = start_point[1]
    z = start_point[2]

    for x in range(start_point[0], end_point[0], step[0]):
        point = [x, y, z]

        if steep_xz:
            point[0], point[2] = point[2], point[0]
        if steep_xy:
            point[0], point[1] = point[1], point[0]

        if 0 <= point[0] < grid.shape[0] and 0 <= point[1] < grid.shape[1] and 0 <= point[2] < grid.shape[2]:
            grid[point[0], point[1], point[2]] = value_to_fill

        error_xy -= delta[1]
        error_xz -= delta[2]

        if error_xy < 0:
            y += step[1]
            error_xy += delta[0]

        if error_xz < 0:
            z += step[2]
            error_xz += delta[0]

@njit
def get_free_area(obstacle, x, y, z):
    free = np.zeros_like(obstacle)
    obstacle = obstacle > 0
    xs, ys, zs = np.where(obstacle)
    for ox, oy, oz in zip(xs, ys, zs):
        bresenham_safe(free, ox, oy, oz, x, y, z, 1)
    free -= obstacle
    return free

# 创建三维网格和障碍物示例
grid = np.zeros((65, 65, 12))
obstacle = np.zeros_like(grid)
start = np.zeros_like(grid)
obstacle[20:30, 4:60, 2:9] = 1

# 获取自由区域
x = 8
y = 8
z = 11
import time
start_time = time.time()
free_area = get_free_area(obstacle, x, y, z)
end_time = time.time()
execution_time = end_time - start_time
print("□□□□□□□□□□程序执行时间为:", execution_time, "秒") 
start[8,8,11]=1

# 翻转颜色映射
cmap = plt.cm.gray
cmap_inverted = cmap.reversed()
# # 可视化
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 设置坐标轴范围和比例,让显示比例正常
x_dim, y_dim, z_dim = obstacle.shape
max_dim = max(x_dim, y_dim, z_dim)
ax.set_xlim(0, max_dim)
ax.set_ylim(0, max_dim)
ax.set_zlim(0, max_dim)


# ------------------------------------------------------------------------------
# x_indices, y_indices, z_indices = np.where(free_area)
ax.voxels(free_area, facecolors='green',)
ax.voxels(obstacle, facecolors='red',)
ax.voxels(start, facecolors='blue')
# print(grid)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
相关推荐
Wyz201210241 分钟前
CSS如何实现移动端点击高亮去除_设置tap-highlight-color
jvm·数据库·python
日光明媚3 分钟前
SoulX-FlashTalk 技术报告解读:从“严格因果”到“双向流式蒸馏”,实时数字人为什么能做到 0.87s 延迟、32FPS 和长时稳定?
人工智能·python·深度学习·ai作画·aigc·音视频
音视频牛哥5 分钟前
鸿蒙 NEXT RTSP/RTMP 播放器如何回调 RGB 数据并实现 AI 视觉算法分析
人工智能·算法·harmonyos·鸿蒙rtmp播放器·鸿蒙rtsp播放器·鸿蒙next rtsp播放器·鸿蒙next rtmp播放器
粉嘟小飞妹儿7 分钟前
如何在云主机上安装Oracle 19c_公网IP绑定与安全组端口开放
jvm·数据库·python
飞Link8 分钟前
掌控 Agent 的时空法则:LangGraph Checkpoint (检查点) 机制深度实战
开发语言·python·算法
zopple12 分钟前
Laravel与ThinkPHP框架深度对比
java·python·php·laravel
乐迪信息14 分钟前
智慧港口中AI防爆摄像机的船舶越线识别功能
大数据·人工智能·物联网·算法·目标跟踪
weixin_5860614618 分钟前
为什么Bootstrap的下拉菜单在Firefox下显示异常
jvm·数据库·python
qq_1898070319 分钟前
如何导出特定分区_EXPDP TABLES=表名-分区名进行单区数据备份
jvm·数据库·python
zzwq.20 分钟前
Pandas数据合并完全指南:merge、concat、join从入门到精通
python·数据分析