3

我刚开始在 python 中使用 mayavi,对 3d 图形编程不太了解。我想绘制一个凹面/物体,但是当我尝试它时,它会像这样连接:

发生了什么

你看不到图片,但它是由平行平面形成的(也不是我想要的)

当我尝试绘制具有这种形状的结构时:

我要绘制的内容

所以这就是我为获得积分和情节所做的事情:

#extract the black in the image
x,y,z = np.where(quest == 0)
#make z1,z2 the right size(y*0) and value(+12)
z1 = y*0
z2 = y*0+12
x,y,z1,z2= np.atleast_2d(x,y,z1,z2)
#duplicate the shape at z1 and z2
x = np.concatenate((x,x))
y = np.concatenate((y,y))
z = np.concatenate((z1,z2))
#plot the mesh
mlab.mesh(x,y,z)
mlab.show()

我注意到你的数据点的顺序对于使用网格很重要,因为它会从每个连续的点画一条线。

有没有更简单的方法可以以某种方式组织我的观点?

还有一种方法可以使形状填充点之间的区域吗?

感谢所有帮助,谢谢

后续问题:

有没有更好的python 3d 库可以达到这个目的?

我正在考虑用 vtk 来做,但同样的问题是像这样切掉问号的凹面部分:

vtk中的同样问题

更长的代码是:

#create the points
points = vtk.vtkPoints()
for i, j in zip(x,y):
    points.InsertNextPoint(i,j,0)
#create a polygon
polygon = vtk.vtkPolygon()
polygon.GetPointIds().SetNumberOfIds(len(x))
for i in range(len(x)):
    polygon.GetPointIds().SetId(i,i)
#add it to a cell array
polygons = vtk.vtkCellArray()
polygons.InsertNextCell(polygon)
#make the poly data
polyData = vtk.vtkPolyData()
polyData.SetPoints(points)
polyData.SetPolys(polygons)
#set up the window
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(polyData
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
rWindow = vtk.vtkRenderWindow()
rWindow.AddRenderer(renderer)
rWindowInteractor = vtk.vtkRenderWindowInteractor()
rWindowInteractor.SetRenderWindow(rWindow)
renderer.AddActor(actor)
renderer.SetBackground(.5,.3,.31)
rWindow.Render()
rWindowInteractor.Start()
4

1 回答 1

3

这篇文章已经有一年多了,但它可能对其他人仍然有用。

我遇到了同样的问题,我通过使用vtkTriangleFilter. 此过滤器将输入多边形转换为三角形,对于 OpenGL 更易于理解。

这是一个快速脚本,我在其中创建了一个凹形(“看起来像”大象),并在开始时提供了一个布尔值来过滤形状。

#!/usr/bin/env python

from vtk import *

# switch to False if you don't want to filter the shape
FILTER = True

# x and y values of points to create a concave shape
x = [-2.25032, -3.04773, -3.06125, -2.69633, -2.52063, -2.16923, -1.72322, -1.47994, -1.85837, -1.04745, -0.68253, 1.25018, 1.81783, 3.20992, 2.15571, 1.99353, 2.72336, 3.35859, 3.57483, 3.60186, 3.69647, 4.18303, 4.29115, 3.87217, 2.87203, 2.07462, 1.43939, 1.10151, 0.912292, 0.466283, 1.27721, 0.723076, -1.22315]

y = [-1.3371, -0.671689, 0.094162, 0.081607, -0.4457, -0.960452, -0.797238, 0.0313873, 1.43754, 1.76397, -0.00627746, -0.295041, 1.42498, 1.36221, -0.39548, -1.21155, -1.57564, -1.57564, -1.03578, -0.47081, 0.244821, 0.169492, -0.822348, -1.98996, -2.7307, -2.96924, -3.20778, -2.89391, -2.56748, -2.12806, -1.93974, -1.65097, -1.60075]


# creation of the source
positions = zip(x, y)

triFilter = vtkTriangleFilter()

pts = vtkPoints()
polygon = vtkPolygon()
polygon.GetPointIds().SetNumberOfIds(len(positions))

for k, l in enumerate(positions):
    pts.InsertNextPoint(float(l[0]), float(l[1]), 1)
    polygon.GetPointIds().SetId(k, k)

polygons = vtkCellArray()
polygons.InsertNextCell(polygon)

polygonPolyData = vtkPolyData()
polygonPolyData.SetPoints(pts)
polygonPolyData.SetPolys(polygons)

# let's filter the polydata
triFilter.SetInputData( polygonPolyData)
triFilter.Update()

polygonPolyDataFiltered = triFilter.GetOutput()

# map it
mapper = vtkPolyDataMapper()

if VTK_MAJOR_VERSION <= 5:

    if FILTER:
        mapper.SetInput(polygonPolyDataFiltered)
    else:
        mapper.SetInput(polygonPolyData)
else:
    if FILTER:
        mapper.SetInputData(polygonPolyDataFiltered)
    else:
        mapper.SetInputData(polygonPolyData)

# render it

actor =  vtkActor()
actor.SetMapper(mapper)

ren = vtkRenderer()
ren.AddActor(actor)

renwin = vtkRenderWindow()
renwin.AddRenderer(ren)

iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)

iren.SetInteractorStyle(vtkInteractorStyleTrackballCamera())
iren.Initialize()
iren.Start()
于 2014-07-24T08:06:38.783 回答