2013-02-27 53 views
3

我刚刚开始在python中使用mayavi,并且不太了解编程3d图形。我要绘制的凹面/对象,但是当我尝试它出来连接是这样的:用mayavi或vtk绘制凹形对象

whats happening

你不能看到图片,但它是由平行的平面上(不是我的意图形成要么)

当我试图绘制结构,这种形状:

what i'm trying to plot

因此,这里是我做什么,以获得积分和情节:

#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() 

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

有没有更简单的方法来做到这一点,以某种方式组织我的观点?

还有没有办法让形状填充点之间的区域?

所有帮助表示赞赏,感谢

后续问题:

是否有蟒蛇更好的3D库,将服务于这个目的是什么?

我期待与VTK做,但切割出问号这样的凹部的同样的问题:

Same problem in 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() 
+0

凸性没有什么魔力 - 网格是一个网格,完全如何定义它。看来你正在获取黑色像素的坐标......那么是什么?你想如何定义这些网格?您必须将其指定给VTK(在构建多边形时),然后才能正确绘制。 – 2014-05-13 20:33:08

回答

2

这篇文章是一岁多,但它可能仍然是有用的。

我有同样的问题,我通过使用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()