fc2ブログ

Python 学習:VTK を、Mayavi で

2014-07-29 :  PCクリニック
先月末(2014-06-29)の記事:
Python 学習:MayaVi2 導入
の如く、Mayaviアプリケーションじゃなくて、PythonスクリプトでMayaviが使える。

更に、Mayaviは“VTK”ファイルを扱える。

そこで、1ヶ月前(2014-07-24)の記事:
Python 学習:Axes3D・surface 改
の様に“Surface”表示はどうするのか?
「vtk surface python」で検索したところ、・・・

「Stack Overflow」に、
Contour plot from data in a vtk file using Python
があった。

“vtk file”のデータから、“Python”を使って、“Contour plot”する方法?

回答には、
  If you'd like to use a more "pythonic" interface to VTK,
   consider using mayavi/tvtk/mlab.
   (Either way, VTK is an excellent choice for this!)
VTK をより多くの"pythonic"のインターフェイスを使用する場合は、
mayavi/tvtk/mlab の使用を検討します。
(いずれにせよ、VTK はこれに最適!)

  tvtk is a slightly more pythonic, low-level,
   python binding to VTK with a handful of really nice features
   (e.g. transparent usage of numpy arrays).
tvtk は本当に素晴らしい機能 (例えば透明量 numpy の配列) の
一握りの VTK をもう少し pythonic、低レベル、python バインディングです。

  Mayavi and mlab give a more high-level interface to VTK.
Mayavi と mlab VTK をより高度なインターフェイスを与えます。

  The snippet of the data file that you showed is invalid as-is,
   but if we use a similar one:
あなたが示したデータ ファイルのスニペットとして無効です-は、
我々 同様の 1 つを使用する場合:

と、訳の分からない翻訳ですが、・・・・・

とにかく、
こんな“VTK”ファイル( 'test.vtk' ):

# vtk DataFile Version 2.0
Simple VTK file example
ASCII

DATASET POLYDATA
POINTS 9 float
3.0 0.0 0.0
1.0 1.0 0.0
0.0 3.0 0.0
3.0 0.0 1.0
1.0 1.0 1.0
0.0 3.0 1.0
3.0 2.0 2.0
2.0 2.0 2.0
2.0 3.0 2.0

TRIANGLE_STRIPS 2 14
6 0 3 1 4 2 5
6 3 6 4 7 5 8

POINT_DATA 9
SCALARS nodal float
LOOKUP_TABLE default
0.0 0.1 0.0 0.3 0.6 0.3 0.8 1.0 0.8


に対して、
こんな“Python”コード:

from mayavi import mlab

source = mlab.pipeline.open( 'test.vtk' )
lines = mlab.pipeline.contour_surface( source )

mlab.show()


で、3Dのコンター図が描ける。

そして、
  Or we can get a bit fancier:
以下のコードで、もっと 好まれる?手の込んだ? ものが得られる。

from mayavi import mlab

# Make a figure with a black background
fig = mlab.figure( bgcolor=(0,0,0) )
# Also see methods like: fig.scene.z_plus_view(), etc
fig.scene.camera.azimuth( 215 )

source = mlab.pipeline.open( 'test.vtk' )
# Show the surface, colored by the scalars
surf = mlab.pipeline.surface( source )
# Draw contours of the scalars on the surface
lines = mlab.pipeline.contour_surface( source )

mlab.show()



それで、・・・
若干の学習、・・・・・
(VTKファイルの学習)
以下の様に書き換えた。

# vtk DataFile Version 2.0
Simple VTK file example
ASCII

DATASET POLYDATA
POINTS 6 float
3 0 0
1 1 0
0 3 0
3 0 1
1 1 1
0 3 1

TRIANGLE_STRIPS 1 7
6 0 3 1 4 2 5

POINT_DATA 6
SCALARS nodal float
LOOKUP_TABLE default
0 0 0 1 1 1


そして、Pythonコードも以下に書き換た。

from mayavi import mlab
fig = mlab.figure( bgcolor=(0,0,0) )

fig.scene.camera.azimuth( 215 )
source = mlab.pipeline.open( 'test.vtk' )
surf = mlab.pipeline.surface( source )
mlab.show()


これで、3D“Surface”プロットが出来る!

もっともっと学習しよう!

本日はここまで。


Python 学習は続く???


見ていただいた序でとは厚かましい限りですが、
お帰りに投票して頂けるとなお嬉しいです。 ⇒ blogram投票ボタン


140725
関連記事
スポンサーサイト



コメントの投稿

管理者にだけ表示を許可する

人気blog Ranking



最新記事
カレンダー
02 | 2024/03 | 04
- - - - - 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 - - - - - -
月別アーカイブ
カテゴリ
最新コメント
検索フォーム
リンク
プロフィール

<紙>

Author:<紙>
ようこそ。
「パソコンヲタクの雑記帳」
もろもろなことを綴っています。
パソコン ヲタクってねくら?
画像は kami でなく kani です。

FC2 / i2i / Google




Google Analytics
ブックマーク