【问题标题】:I want to make evenly distributed sphere in vtk (python)我想在 vtk (python) 中制作均匀分布的球体
【发布时间】:2017-11-25 11:06:07
【问题描述】:

正如你在标题中看到的,我想在 vtk (python) 中制作均匀分布的球体

首先,我看到了这个链接“Evenly distributing n points on a sphere”,这是一种创建均匀分布球体的方法。通过那个链接,我得到了均匀分布球体的 x,y,z 坐标。

其次,这实际上不是我必须解决的问题。问题是即使我有均匀分布球体的 x,y,z 坐标,我也无法在 vtk(python) 处制作多数据..

import numpy as np
import mpl_toolkits.mplot3d
import matplotlib.pyplot as plt
import vtk
from scipy.spatial import Delaunay

num_pts = 1000
indices = np.arange(0, num_pts, dtype=float) + 0.5

phi = np.arccos(1 - 2*indices/num_pts)
theta = np.pi * (1 + 5**0.5) * indices

x, y, z = np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi);

# x,y,z is coordination of evenly distributed shpere
# I will try to make poly data use this x,y,z 

points = vtk.vtkPoints()


for i in range(len(x)):
    array_point = np.array([x[i], y[i], z[i]] )
    points.InsertNextPoint(x[i],y[i],z[i])


#   tri = Delaunay(points) (Do I have to use this function??)

poly = vtk.vtkPolyData()
poly.SetPoints(points)

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(poly)

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(actor)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

renWin.Render()
iren.Start()

代码没有抛出任何错误,但 polydata 没有出现在我的 vtk 窗口中,, 我应该怎么做才能解决这个问题?

-泰英。

【问题讨论】:

  • 你能提供至少一点点编写 PolyData 的代码吗?这是您的任务,如果您告诉我们您遇到的问题,我们可以为您提供帮助。但期望我们提供完整的解决方案是不公平的。
  • 对不起,这是我第一次提问,我编辑我的问题包括更多细节,,

标签: python vtk evenly


【解决方案1】:

干得好。现在您已将点添加到球体多数据中,我们需要从点中生成一个曲面。我们使用vtkDelaunay3D 过滤器来做到这一点。它将生成四面体的 3D 网格。因此,为了得到一个实际的球面,我们必须使用vtkDataSetSurfaceFilter 提取表面。这些是在下面完成的:

import numpy as np
import vtk

num_pts = 1000
indices = np.arange(0, num_pts, dtype=float) + 0.5

phi = np.arccos(1 - 2*indices/num_pts)
theta = np.pi * (1 + 5**0.5) * indices

x, y, z = np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi);

# x,y,z is coordination of evenly distributed shpere
# I will try to make poly data use this x,y,z 

points = vtk.vtkPoints()


for i in range(len(x)):
    array_point = np.array([x[i], y[i], z[i]] )
    points.InsertNextPoint(x[i],y[i],z[i])

poly = vtk.vtkPolyData()
poly.SetPoints(points)

# To create surface of a sphere we need to use Delaunay triangulation
d3D = vtk.vtkDelaunay3D()
d3D.SetInputData( poly ) # This generates a 3D mesh

# We need to extract the surface from the 3D mesh
dss = vtk.vtkDataSetSurfaceFilter()
dss.SetInputConnection( d3D.GetOutputPort() )
dss.Update()

# Now we have our final polydata
spherePoly = dss.GetOutput()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(spherePoly)

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(actor)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

renWin.Render()
iren.Start()

【讨论】:

    【解决方案2】:

    PyVista 让这一切变得非常简单:

    import numpy as np
    import pyvista as pv
    
    num_pts = 1000
    indices = np.arange(0, num_pts, dtype=float) + 0.5
    
    phi = np.arccos(1 - 2*indices/num_pts)
    theta = np.pi * (1 + 5**0.5) * indices
    
    x =  np.cos(theta) * np.sin(phi) 
    y = np.sin(theta) * np.sin(phi)
    z = np.cos(phi)
    
    point_cloud = pv.PolyData(np.c_[x, y, z])
    surface = point_cloud.delaunay_3d().extract_surface()
    surface.plot(show_edges=True, color=True, show_grid=True)
    

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2013-03-30
      • 1970-01-01
      • 2021-09-12
      • 2019-01-21
      • 2011-07-21
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多