【已解决】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

相关文章

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工