三流Mayavi操作-Mayav-2.1.0.6-volume_slice,flow繪製

秉着邊學邊寫邊折騰的原則,開始粗糙的工作。真正掌握還是得講解給別人聽。 先給出網課
https://www.icourse163.org/course/BIT-1001871001
Mayavi官方
http://docs.enthought.com/mayavi/mayavi/genindex.html
(有時候這網站會裝死,一般過幾個小時就會活過來)
我發現了,光是三流操作還不夠,還得加上四流翻譯。

這一章到基本函數繪製的最後了,flow,volume_slice也是最後兩個。
這兩個是比較有用的繪製函數,因爲這個經常可以作爲輔助觀測平面。
這篇的講法和以前一樣,從官方例子引入,作必要的解釋,然後翻譯文檔,這兩個繪製比較特殊,因爲它的參數和其他的都大不相同。
而且這兩個繪製本身之間關係型也不是很強。

1.volume_slice

官方實例,微調

import numpy as np
from mayavi import mlab
x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalars = x * x * 0.5 + y * y + z * z * 2.0
mlab.volume_slice(scalars, plane_orientation='x_axes')
mlab.show()

僅靠這段代碼是看不出來所以然的。因爲作爲例子的圖是這樣的

在這裏插入圖片描述
.
以上是原汁原味的官方實例

現在我給出補充後的代碼來觀測。
插入這句:
mlab.contour3d(scalars)

在這裏插入圖片描述

現在圖像的原型基本出來了,繪製了它的等高面,而中間的觀測平面是可以移動的。我們處理的時候採用的np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]進行離散,實際上,觀測面又做了插值,把中間不存在的點又補上了。
這個面的好處在於,它是活動的,是可以交互地隨意移動的一個面,注意到示例代碼中:
mlab.volume_slice(scalars, plane_orientation='x_axes')
參數plane_orientation是x軸爲法線作面,同理就還有'y_axes','z_axes'
volume_slice更多地是用來繪製輔助觀察面~

2.flow

官方老代碼。

import numpy as np
from mayavi.mlab import *
x, y, z = np.mgrid[-4:4:40j, -4:4:40j, 0:4:20j]
r = np.sqrt(x ** 2 + y ** 2 + z ** 2 + 0.1)
u = y * np.sin(r) / r
v = -x * np.sin(r) / r
w = np.ones_like(z)*0.05
contour3d(u, v, w)
obj = flow(u, v, w)
show()

下面的圖都以這個代碼爲母體。

以及效果圖

在這裏插入圖片描述

代碼配置如下,這個技巧quiver3d是上一章內容,這個是矢量場的觀測手段。
插入下面這句,把矢量場畫出來。

quiver3d(u, v, w,mask_points=10)

同樣也可以手動把矢量管線加進去,並降採樣
在GUI裏面對它設置On ratio=10可以設置得更高,每10個採集1個。如果不降採,矢量場會是密密麻麻的,雖然漂亮還是漂亮。
必要可以繼續增設outline

在這裏插入圖片描述

然後一下就變得很絢麗了。sex,我很喜歡。

在這裏插入圖片描述

來看看詳細解釋

Creates a trajectory of particles following the flow of a vector field.
創建沿向量場流動的粒子軌跡。

這個翻譯是有問題的,這個flow說得不清不楚的。trajectory本意是軌跡線,但是這裏似乎傾向於說跡線。熟悉流體力學的應該知道流線(Streamline)跡線(Path line)的概念。而在管線配置中,最後的Module層級的默認標籤是Streamline,這個是很明確的流線
在這裏插入圖片描述

Module層級表明是Streamline

在這裏插入圖片描述

語法

flow(u, v, w, ...)
flow(x, y, z, u, v, w, ...)
flow(x, y, z, f, ...)

u, v, w are numpy arrays giving the components of the vectors.
If only 3 arrays, u, v, and w are passed, they must be 3D arrays, and the positions of the arrows are assumed to be the indices of the corresponding points in the (u, v, w) arrays.
If 6 arrays, (x, y, z, u, v, w) are passed, the 3 first arrays give the position of the arrows, and the 3 last the components. The x, y and z arrays are then supposed to have been generated by numpy.mgrid, in other words, they are 3D arrays, with positions lying on a 3D orthogonal and regularly spaced grid with nearest neighbor in space matching nearest neighbor in the array. The function builds a vector field assuming the points are regularly spaced.
If 4 positional arguments, (x, y, z, f) are passed, the last one must be a callable, f, that returns vectors components (u, v, w) given the positions (x, y, z).
.
u, v, w爲numpy arrays給定3個矢量分量。
如果只給定u, v, w 3 個參數,他們必須是3D array,且假定箭頭的位置(u, v, w)匹配
如果給定(x, y, z, u, v, w) ,則前3個array 給定箭頭位置,後3個給定矢量分量, x,y,z應當由numpy.mgrid來生成。換言之,他們是3維數組,且正交放置在規整的網格上,連通方式是空間上的就近連接。該函數將繪製矢量場,並假設了所有的點都是規整放置的。
如果給定4個參數 (x, y, z, f) ,根據位置(x, y, z),f必須可以返回位置對應的矢量分量(u, v, w)。

這裏面有很多可以玩的。

點觀測,線觀測,面觀測,空間觀測
在這裏插入圖片描述

相應的設置在GUI裏面,Source Widget裏面,每一種觀測對應的設置都不同,自己摸索吧。下面有很多好玩的。直接寫進代碼也是可以的見下面的參數seedtype

在這裏插入圖片描述

3.參數

在這裏插入圖片描述
除9個基本參數意外

flowvolume_slice共有參數:
extentfigure
.
extent

[xmin, xmax, ymin, ymax, zmin, zmax] Default is the x, y, z arrays extent. Use this to change the extent of the object created.
設置可視化的範圍,默認是採用x,y,z數組的全部值域。使用這個可以改變創建的可視化對象範圍。

這玩意會改可視化圖像的數據,(不是改原始數據,可視化是映射成你看到的樣子,他修改的是你看到,原始數據沒有變化,後面我會說明這一點)進行整體放縮,在《mesh、triangular_mesh繪製》裏面我寫過這件事。我回去翻了一下好像不確定我說清楚沒有,這裏再給一個例子。我就用官方的代碼來做例子。
這裏稍微注意一下就是,添加了輔助手段xlabel('x')outline()來觀測值域。

原圖像:
注意值域範圍是,x,y都是[0,39],z是[0,19]
在這裏插入圖片描述
.

x, y, z = np.mgrid[-4:4:40j, -4:4:40j, 0:4:20j]
r = np.sqrt(x ** 2 + y ** 2 + z ** 2 + 0.1)
u = y * np.sin(r) / r;v = -x * np.sin(r) / r;w = np.ones_like(z)*0.05
ext = [0,20,0,20,0,20]
quiver3d(u, v, w,mask_points=20,extent=ext)
xlabel('x')
outline()

在這裏插入圖片描述
注意看值域範圍ext = [0,20,0,20,0,20]原始值域範圍由outline()繪製出來,是[0,39,0,39,0,19],z方向已經被放大了,比19多1,橫縱都壓縮了接近一倍。

然後繼續說前面那個問題,原始數據沒有變化,注意代碼的寫法outline()xlabel('x')緊跟其後,默認添加在當前的數據源下面,也就是利用的同一個數據源

不妨打開管線OutlineVectors還有Axes屬於同一個層級置於同一個數據源VectorScatter下面
如果數據源被修改了,那麼OutlineVectors的範圍會同時被修改。而上面看到的結果是,它還是按照數據本身的樣子呈現。所以數據源是沒有變化的。
在這裏插入圖片描述

其實如果仔細想過管線應該能馬上想到這點,管線後面再說。
這個同樣,追加了flow(u, v, w,extent=ext)sphere選定的在可視化區域外面,居然有數據,這就意味着計算機做了假圖,HAHAHA。

在這裏插入圖片描述

最後就是,這個東西不是很常用。

figure

Figure to populate.
圖像填充
簡單點說就是圖像基本設置在這裏,背景什麼的,可視化的框框大小。高級的話,這東西能抓Scenes管線。
mesh的的時候沒注意到這個的用法,放這裏展開一下。這裏是具體的用法,本來想在講管線的時候再講這裏的。戳進去,這裏還有一個例子,我把它們抄過來。

Creates a new scene or retrieves an existing scene. If the mayavi engine is not running this also starts it.
創建新的場景scene或者檢索已存在的scene,如果mayavi的engine失效了也可以通過它開啓。

這裏的sceneengine都是Mayavi的層級,在GUI裏面只能看到Scenes級別以下的,engine是看不到的,這裏不過多說管線,engine簡單說就是管理Scenes的,Scene包含數據源,一層一層向下。

在這裏插入圖片描述

這個figure還是很有用的,涉及到engine的話就會有高級用法,這裏也不講,等到管線的時候講。
mayavi.mlab.figure(figure=None, bgcolor=None, fgcolor=None, engine=None, size=(400, 350))
figure: The name of the figure, or handle to it.
bgcolor: The color of the background (None is default).
fgcolor: The color of the foreground, that is the color of all text annotation labels (axes, orientation axes, scalar bar labels). It should be sufficiently far from bgcolor to see the annotation texts. (None is default).
engine: The mayavi engine that controls the figure.
size: The size of the scene created, in pixels. May not apply for certain scene viewer.

語法來了,注意看語法基本都是缺省,抓取場景,設置前景背景和engine最後是設置圖像大小。這裏還是不展開,只講bgcolor和fgcolor,對於我們繪圖而言,比較實用的就是設置前景背景,下面舉個栗子

figure(1, bgcolor=(1, 1, 1))
#figure(1,fgcolor=(1, 1, 1),size=(800,700)) #隨便改啦

在這裏插入圖片描述

最後說一點,這裏的flowquiver3d採用的是同一級數據源管線,figure是對應的Scenes是數據源的父層級,所以figure只用設置一次了。

flow獨有:
integration_direction,linetype,scalars,seed_resolution,seed_scale,seed_visible,seedtype

integration_direction

The direction of the integration. Must be ‘forward’ or ‘backward’ or ‘both’. Default: forward
流線方向。只接受 ‘forward’ 或‘backward’或‘both’,其中forward是缺省值。
修改原來的代碼,追加一個參數:
flow(u, v, w,integration_direction='both')
分別可以再設置'forward','backward',可以看到不同的效果
圖示順序是'both','forward','backward'

在這裏插入圖片描述

另外一種設置方式是用GUI:

在這裏插入圖片描述

linetype

the type of line-like object used to display the streamline. Must be 'line' or 'ribbon' or 'tube'. Default: line
用來展示流線的可視化對象的線型。只接受'line'(線型),'ribbon'(帶狀)或者'tube'(管狀),'line'是默認缺省。
.
我個人喜歡管狀,看着豐滿。(對,我喜歡豐滿的線條。咳咳。反正沒人看寫了無所謂)進入正題。
三種不同的線型,右下角是GUI設置。

在這裏插入圖片描述

scalars

optional scalar data.
設置標量,以前說過,這裏給出一個例子。
修改官方老代碼中的一行,把參數加上。(微調後的老代碼不是原本的官方老代碼)
flow(u, v, w,scalars=x+y-z,linetype='tube')
坐邊是無scalars,右邊追加了參數

在這裏插入圖片描述

注意像這種參數是沒辦法GUI的,只能寫進代碼。

seed_resolution

The resolution of the seed. Determines the number of seed points Must be an integer (int or long) or None.
seed的分辨率,決定seed的數目只接受integer (int 或者 long) 或者 None.
這個seed就是那個篩選器,前面說過篩選器可以是空間觀測sphere,關鍵是這個篩選器的形狀
修改老代碼
flow(u, v, w,linetype='tube',seed_resolution=11)

在這裏插入圖片描述

不信可以數一下,就是11條半緯線。

seed_scale

Scales the seed around its default center Must be a float. Default: 1.0
默認對seed進行中心放縮,只接受float,默認1.0

不多說,就是放大,一個例子如下,大了很多。
flow(u, v, w,linetype='tube',seed_resolution=11,seed_scale=2.0)
在這裏插入圖片描述

seed_visible

Control the visibility of the seed. Must be a boolean. Default: True
控制seed是否可見,只接受布爾值。默認爲True

一般用不到,連seed都隱藏了,就沒辦法交互了,除非seed遮擋了你的圖。
在這裏插入圖片描述

seedtype

the widget used as a seed for the streamlines. Must be 'line' or 'plane' or'point' or sphere. Default:sphere
seed的觀測類型設置。只接受'point','line','plane',sphere,默認是sphere
點觀測'point',線觀測'line',面觀測'plane',空間觀測sphere

前面舉過例子,偷圖。
在這裏插入圖片描述

.
.
下面的例子我用這個老代碼,已調過一些參數

x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalars = x * x * 0.5 + y * y + z * z * 2.0
mlab.figure(size=(600,525))
mlab.contour3d(scalars,opacity=0.7,contours=3)
mlab.volume_slice(scalars)
mlab.show()

volume_slice獨有:

plane_opacity

the opacity of the plane actor. Must be a legal value. Default: 1.0
對這個觀測面設置透明度。只接受合法值。默認:1.0

這個和opacity參數一樣範圍0-1表示透明度,volume_slice也有opacity參數,後者是設置整個VTK對象的。但是實際上我都設過,是無效的。GUI裏面也沒有對應的選項。這個繪製要小心了。必要情況可以用transparent頂一下。

留坑。

plane_orientation

the orientation of the plane Must be a legal value. Default: x_axes
設置面的方向。必須是一個合法值。默認值是 x_axes
同理還有y_axes,z_axes

下面就y方向和z方向繪圖,GUI裏面也有相應的設置,可自行設置。
在這裏插入圖片描述

slice_index

The index along wich the image is sliced.
切片的索引號。
mlab.volume_slice(scalars,plane_orientation='y_axes')基礎上追加

mlab.volume_slice(scalars,plane_orientation='y_axes',slice_index=15)
GUI也是可以直接設置的。這樣可以確定一個精確的觀測面,這個只接受整型數,畢竟是索引號。如果要浮點數可以從GUI的slice position進行修改
在這裏插入圖片描述

mlab.volume_slice(scalars,plane_orientation='y_axes',slice_index=90)
在這裏插入圖片描述

這個超過了倒是無所謂的。
np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]這句限制了,空間軸三個方向只能有64個面,有效索引是0-63,超過63都是按照最大索引處理。

填坑區:

1.plane_opacity'opacity'兩個參數無效未解決。

更新(18.12.10已更完)
2018.12.10.——一次更完。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章