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 学習は続く???
見ていただいた序でとは厚かましい限りですが、
お帰りに投票して頂けるとなお嬉しいです。 ⇒
140725
- 関連記事
-
- Python 学習:Mayavi・MLab・mesh (2014/08/06)
- VTK 学習:書式(file formats) (2014/08/04)
- Python 学習:Mayavi・MLab (2014/08/02)
- Perl:zip/unzip (2014/07/30)
- Python 学習:VTK を、Mayavi で (2014/07/29)
- Python 学習:MPI プログラミング (2014/07/28)
- Python 学習:書庫操作 (2014/07/27)
- Python 学習:numpy.loadtxt (2014/07/26)
- Python 学習:numpy.meshgrid (2014/07/25)
スポンサーサイト