2013-02-27 106 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()