簡體   English   中英

如何在 Python 中使用 Mayavi 在 tvtk 渲染場景上旋轉演員?

[英]How to rotate actor on tvtk rendered scene with Mayavi in Python?

我玩了一點Mayavi ,尤其是tvtk但我很難找到將字形以與默認方向不同的方向放置在場景中的示例。

基於這個例子,我用兩個圓柱體准備了以下場景,一個紅色一個藍色,

import mayavi.mlab as mlab

from tvtk.api import tvtk
from tvtk.common import configure_input_data

v = mlab.figure()

# Create two cyllinders
cyl1 = tvtk.CylinderSource(center=(0, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)
cyl2 = tvtk.CylinderSource(center=(3, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)


# The mapper converts them into position in 3D
cylinder_mapper1 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper1, cyl1.output)
cyl1.update()

cylinder_mapper2 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper2, cyl2.output)
cyl2.update()

# Assign them differrent colors
p1 = tvtk.Property(opacity=1.0, color=(1, 0, 0))
p2 = tvtk.Property(opacity=1.0, color=(0, 0, 1))

# The actor is the actually object in the scene.
cyl1_actor = tvtk.Actor(mapper=cylinder_mapper1, property=p1)
v.scene.add_actor(cyl1_actor)

cyl2_actor = tvtk.Actor(mapper=cylinder_mapper2, property=p2)
v.scene.add_actor(cyl2_actor)

# Choose a view angle, and display the figure
mlab.view(90, 45, 7.5, [1.5, 0, 0])
mlab.savefig(filename='cylinders.png')

我在這里試圖實現的是改變其中之一的方向。 無論是通過提供旋轉矩陣,還是通過調用方法,只要我能以某種方式獨立旋轉字形(而不僅僅是旋轉整個場景),這都無關緊要。

上面的例子呈現

代碼示例的渲染輸出

關於之前已經提出的其他問題,這個問題似乎很有用,但它不是旋轉演員,而是圍繞 Y 軸旋轉整個場景。 scipy-cookbook 中還有一個示例,但同樣 - 僅使用默認方向。

tvtk 文檔中的例子並不豐富,幾乎很難相信沒有涉及交替方向的例子。 該文檔僅說明類和方法對應於遵循某些約定的 C++ 對應項。

  1. tvtk 類名本質上類似於 VTK 類,只是前面沒有煩人的“vtk”。 唯一的困難在於以數字開頭的類。 例如,“vtk3DSImporter”變為“3DSImporter”。 這在 Python 中是非法的,因此使用的類名是“ThreeDSImporter”。 因此,如果第一個字符是數字,則將其替換為等效的非數字字符串。 很少有這樣的類,所以這不是什么大問題。
  2. tvtk 方法名稱是經過深思熟慮的樣式名稱,而不是 CamelCase。 也就是說,如果一個 VTK 方法被稱為 AddItem,則等效的 tvtk 名稱是 add_item。 這樣做是為了與 enthought 包中使用的名稱保持一致。
  3. 許多 VTK 方法被方便的屬性取代。 在上面的例子中,我們使用了 m.input = cs.output 和 p.representation = 'w' 而不是 m.SetInput(cs.GetOutput()) 和 p.SetRepresentationToWireframe() 等。其中一些屬性真的是特質。
  4. 與 VTK 對象不同,可以在對象初始化時通過在類實例化時將對象的屬性(特征)作為關鍵字參數傳遞來設置 tvtk 對象的屬性。 例如 cs = tvtk.ConeSource(radius=0.1, height=0.5)。

也許在 C++ 中廣泛使用 VTK 的人會發現它很有用,但對我來說它不是很有幫助。 盡管如此,我還是嘗試查找 VTK C++ 文檔,它確實給了我一些線索。 vtkActor 類確實從vtkProp3DvtkProp繼承了一些類型。 vtkProp3D似乎承認與場景中的對象方向相關的方法和屬性,但我不清楚如何設置它們。 tvtk庫只是 C++ 庫之上的一個包裝器,這使得僅inspect哪些屬性被接受是不可能的。

我想為了互聯網,也許我們應該准備一個適當的例子,用Mayavitvtk渲染一個場景,同時承認演員的位置和方向。 大多數示例忽略方向並使用球體,因此方向似乎不相關。

讓我們總結一下。

  1. 如果有人提供了一個使用 Mayavi 和tvtk渲染場景的正確示例的鏈接,該場景涉及具有不同位置和方向的多個字形,我將接受這樣的答案。
  2. 為 (1) 制作自定義示例也將被接受。
  3. 也許有人也可以評論一下字形的位置和在場景中映射這個字形的演員之間的區別。 正如您在示例中看到的,當我創建CylinderSource我明確指定了中心所在的位置。 這令人困惑,因為它應該是決定場景中字形位置的演員中心。

非常感謝!

好吧,事實證明Actor接受以度為單位的orientation參數(而不是以弧度為單位!)。 此外,角色的位置必須在創建角色時指定,而不是在創建字形時指定!

cyl1_actor = tvtk.Actor(position=(0, 0, 0), mapper=cylinder_mapper1, property=p1, orientation=(0, 0, 90))

渲染

渲染輸出

我是通過反復試驗找到的...... Mayavi包中的tvtk代碼是生成的,所以嘗試通過研究GitHub存儲庫來查找哪些屬性是沒有意義的。 此外,您可能會將tvtkActor與沒有方向屬性的Mayavi 的 Actor混淆。 哦,實際上有一個屬性名稱“屬性”,所以嘗試搜索它並獲得數千個結果,因為property一詞在任何地方都被廣泛使用......

編輯:

我注意到我一直在字形中心指定演員的位置,這是錯誤的! 這里更新了整個源:

import mayavi.mlab as mlab

from tvtk.api import tvtk
from tvtk.common import configure_input_data

v = mlab.figure()

# Create two cyllinders
cyl1 = tvtk.CylinderSource(center=(0, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)
cyl2 = tvtk.CylinderSource(center=(0, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)


# The mapper converts them into position in 3D
cylinder_mapper1 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper1, cyl1.output)
cyl1.update()

cylinder_mapper2 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper2, cyl2.output)
cyl2.update()

# Assign them differrent colors
p1 = tvtk.Property(opacity=1.0, color=(1, 0, 0))
p2 = tvtk.Property(opacity=1.0, color=(0, 0, 1))

# The actor is the actually object in the scene.
cyl1_actor = tvtk.Actor(position=(0, 0, 0), mapper=cylinder_mapper1, property=p1)
v.scene.add_actor(cyl1_actor)

cyl2_actor = tvtk.Actor(position=(3, 0, 0), mapper=cylinder_mapper2, property=p2, orientation=(0, 0, 90))
v.scene.add_actor(cyl2_actor)

# Choose a view angle, and display the figure
mlab.view(90, 45, 7.5, [1.5, 0, 0])
mlab.savefig(filename='cylinders.png')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM