【已解决】Python Bresenham 3D算法

2023-12-23 16:28

本文主要是介绍【已解决】Python Bresenham 3D算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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] / 2error_xz = delta[0] / 2step = [-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_fillerror_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 > 0xs, 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 -= obstaclereturn 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()

在这里插入图片描述

这篇关于【已解决】Python Bresenham 3D算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/528665

相关文章

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提