我正在尝试用体素填充四面体来生成3D数据。我已经能够生成四面体本身使用4个不同的点。我不确定如何使用NumPy或任何其他Python框架来用体素填充四面体内部的区域。以下是生成四面体3D图的代码:
# Tetrahedron
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
points= np.array([[1,2,2],[1,3,4],[4,1,4],[5,3,2]])
tri = Delaunay(points)
tr = tri.simplices[0] # indices of first tetrahedron
pts = points[tr, :] # pts is a 4x3 array of the tetrahedron coordinates
fig = plt.figure()
ax = fig.gca(projection= '3d')
# plotting the six edges of the tetrahedron
for ij in [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]:
ax.plot3D(pts[ij, 0], pts[ij, 1], pts[ij, 2])
plt.show()
下面是我试图实现的一个例子。此示例的特征是填充有体素的球体:
1条答案
按热度按时间wqlqzqxt1#
你在找这样的东西?
我已经采用了这个https://matplotlib.org/stable/gallery/mplot3d/voxels_rgb.html来获得你的单工。
你能解释一下你在surface_normal_form函数中做了什么吗?
当然,在3d中,你可以用平面上的一个点和与平面正交的向量来描述平面。这是特别有用的,因为它很容易判断一个点是在平面的一边还是另一边。如果你取包含单纯形的边的平面,你就得到了单纯形体素,通过取一个紧密配合在一起的体素立方体,并为每个平面删除它错误的一边。这就是我正在做的。
另外,你能解释一下中点函数吗?
首先是一个小小的免责声明,我还没有写自己。正如我所说,它来自matplotlib示例。但是如果你想计算一个一维数组的中点,你可以做。
首先,
x[:-1]
给出了除了最后一个值之外的所有值,x[1:]
给出了除了第一个值之外的所有值,所以相邻的值被求和并除以2。e.你得到中点。请注意,如果原始数组超过1d(在我们的例子中是3维),则它将获取(在我们的例子中是2d)子数组的“中点”。在第二步中,我们要做的是(x[:,:-1]+x[:,1:])/2
。因为[:]
给了你所有的值,midpoint
函数会为每个维度做这些。