这个脚本从哪里获取数据以及在哪里保存 STL 文件?

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

我正在尝试使用我找到的一些代码,但我不完全理解它是如何工作的。我对 Python 没有太多经验,我想了解下面的脚本从哪里获取数据(以及如何将其制作为特定文件)以及它在哪里保存它生成的 STL 文件。

import numpy as np
from scipy.interpolate import griddata
import scipy.ndimage as ndimage
from scipy.ndimage import gaussian_filter
from scipy.misc import imsave
from matplotlib import cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from stl import mesh, Mode
import matplotlib.tri as mtri
from mpl_toolkits.mplot3d.axes3d import get_test_data

# Generating the surface
x, y, z = get_test_data(delta=0.1)
# Scale the surface for this example
z *= 0.05
# Remember that Gazebo uses ENU (east-north-up) convention, so underwater
# the Z coordinate will be negative
z -= 3
# Note: Gazebo will import your mesh in meters.

# Point clouds may not come in nice grids and can be sparse, 
# so let's make it a (N, 3) matrix just to show how it can be done. 
# If you have outliers or noise, you should treat those values now.
xyz = np.zeros(shape=(x.size, 3))
xyz[:, 0] = x.flatten()
xyz[:, 1] = y.flatten()
xyz[:, 2] = z.flatten()

# Generate a grid for the X and Y coordinates, change the number of points
# to your needs. Large grids can generate files that are too big for Gazebo, so
# be careful when choosing the resolution of your grid.
x_grid, y_grid = np.meshgrid(np.linspace(xyz[:, 0].min(), xyz[:, 0].max(), 300),
                             np.linspace(xyz[:, 1].min(), xyz[:, 1].max(), 200))

# Interpolate over the point cloud for our grid
z_grid = griddata(xyz[:, 0:2], xyz[:, 2], (x_grid, y_grid),
                  method='linear')

# Option to treat noise
#z_grid = gaussian_filter(z_grid, sigma=1)

# Show the resulting heightmap as an image
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111)
plt.imshow(z_grid)

# Flatten our interpolated data for triangulation
output = np.zeros(shape=(x_grid.size, 3))
output[:, 0] = x_grid.flatten()
output[:, 1] = y_grid.flatten()
output[:, 2] = z_grid.flatten()

# Triangulation of the interpolated data
tri = mtri.Triangulation(output[:, 0], output[:, 1])

# Show the resulting surface
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(tri, output[:, 2], cmap=plt.cm.CMRmap, shade=True, linewidth=0.1)
ax.axis('equal')

# Create the mesh object
seabed_mesh = mesh.Mesh(np.zeros(tri.triangles.shape[0], dtype=mesh.Mesh.dtype))

# Set the vectors
for i, f in enumerate(tri.triangles):
    for j in range(3):
        seabed_mesh.vectors[i][j] = output[f[j]]

# Store the seabed as a STL file
seabed_mesh.save('seabed.stl')

plt.show()

据我所知,我可以使用它从 XYZ 文件获取坐标数据并将其转换为 STL 文件。

有什么想法吗?

python xyz
1个回答
0
投票

现在,您的代码将测试(示例)数据集提取到 X、Y 和 Z 变量中:

x, y, z = get_test_data(delta=0.1)

有几种方法可以从文件加载此数据,但要意识到此数据将是点云(因为它看起来像您的代码将点云三角剖分到网格)。点云有常见的格式,我不确定您打算从什么格式加载它。

例如。如果您的输入数据是这样的文本文件:

1,2,3
6,5,4

你可以用类似的东西加载它

x, y, z = np.loadtxt("input.txt", delimiter=",", usecols=(0, 1, 2), unpack=True)

或者任何与您的数据等效的内容。

生成的 STL 文件应与 Python 脚本位于同一文件夹中。

© www.soinside.com 2019 - 2024. All rights reserved.