標籤:vtk中文 vtk users guide vtk使用者指南 vtk 可視化
【譯者:這個系列教程是以Kitware公司出版的《VTK User’s Guide -11th edition》一書作的中文翻譯(出版時間2010年,ISBN: 978-1-930934-23-8),由於時間關係,我們不能保證每周都能更新本書內容,但盡量做到一周更新一篇到兩篇內容。敬請期待^_^。歡迎轉載,另請轉載時註明本文出處,謝謝合作!同時,由於譯者水平有限,出錯之處在所難免,歡迎指出訂正!】【本小節內容對應原書的第112頁至第117頁】5.3 規則網格可視化
規則網格在拓撲結構上規則,而幾何上是不規則的,3-2所示。規則網格常被用來進行資料分析(如流體動力學計算)。規則網格vtkStructuredGrid資料集主要有六面體(vtkHexahedral)單元或者是四邊形(vtkQuad)單元組成。
手動建立vtkStructedGrid
建立規則網格需要指定網格的維數(定義拓撲結構)和定義x-y-z座標的vtkPoints對象(定義幾何結構)。下面代碼改自VTK/Examples/DataManiulation/Cxx/SGrid.cxx。
vtkPoints points
points InsertPoint 0 0.0 0.0 0.0
…etc…
vtkStructedGrid sgrid
sgrid SetDimensions 13 11 11
sgrid SetPoints points
主要vtkPoints對象中點的個數要與網格i,j和k三個方向的維數乘積保持一致。
提取計算平面
大多數情況下規則網格由接收vtkDataSet類型輸入的filter來處理(見89頁“可視化技術”)。而直接接收vtkStructuredGrid類型輸入的其中一個filter是vtkStructuredGridGeometryFilter。該Filter根據其Extent變數值來提取網格中的局部點、線段或者平面(Extent是一個描述地區的六維向量( , ))。
下例中我們讀取了一個規則網格資料,提取其中的三個平面,並利用相關的向量資料對平面進行Warp處理(代碼取自VTK/Examples/VisualizationAlgorithms/Tcl/warpComb.tcl)。
vtkPLOT3DReader pl3d
pl3d SetXYZFileName "$VTK_DATA_ROOT/Data/combxyz.bin"
pl3d SetQFileName "$VTK_DATA_ROOT/Data/combq.bin"
pl3d SetScalarFunctionNumber 100
pl3d SetVectorFunctionNumber 202
pl3d Update
vtkStructuredGridGeometryFilter plane
plane SetInputConnection [pl3d GetOutputPort]
plane SetExtent 10 10 1 100 1 100
vtkStructuredGridGeometryFilter plane2
plane2 SetInputConnection [pl3d GetOutputPort]
plane2 SetExtent 30 30 1 100 1 100
vtkStructuredGridGeometryFilter plane3
plane3 SetInputConnection [pl3d GetOutputPort]
plane3 SetExtent 45 45 1 100 1 100
vtkAppendPolyData appendF
appendF AddInputConnection [plane GetOutputPort]
appendF AddInputConnection [plane2 GetOutputPort]
appendF AddInputConnection [plane3 GetOutputPort]
vtkWarpScalar warp
warp SetInputConnection [appendF GetOutputPort]
warp UseNormalOn
warp SetNormal 1.0 0.0 0.0
warp SetScaleFactor 2.5
vtkPolyDataNormals normals
normals SetInputConnection [warp GetOutputPort]
normals SetFeatureAngle 60
vtkPolyDataMapper planeMapper
planeMapper SetInputConnection [normals GetOutputPort]
evalplaneMapper SetScalarRange [[pl3d GetOutput] GetScalarRange]
vtkActor planeActor
planeActor SetMapper planeMapper
規則網格資料降採樣
像資料一樣,規則網格資料也可以進行降採樣(見105頁“映像資料降採樣”)。vtkExtractGrid用來執行資料降採樣和提取。
vtkPLOT3DReader pl3d
pl3d SetXYZFileName "$VTK_DATA_ROOT/Data/combxyz.bin"
pl3d SetQFileName "$VTK_DATA_ROOT/Data/combq.bin"
pl3d SetScalarFunctionNumber 100
pl3d SetVectorFunctionNumber 202
pl3d Update
vtkExtractGrid extract
extract SetInputConnection [pl3d GetOutputPort]
extract SetVOI 30 30 -1000 1000 -1000 1000
extract SetSampleRate 1 2 3
extract IncludeBoundaryOn
在該例子中初始規則網格(維數為57X33X25)按照(1,2,3)的採樣率進行採樣,產生一個維數(1,17,9)的子集。函數IncludeBoundaryOn()保證了在沒有選擇邊界的採樣操作下也將邊界提取出來。
5.4 線性網格可視化
線性網格在拓撲上規則,但是在幾何上半規則(3-2(b))。線性網格也常用於數值分析。vtkRectilinearGrid由體素(vtkVoxel)或者像素(vtkPixel)單元組成。
手動建立vtkRectlinearGrid
線性網格建立需要指定網格維數定義網格拓撲,以及三個標量數組定義x-y-z三個方向座標來定義幾何。下面代碼改自VTK/Examples/DataManipulation/Cxx/RGrid.cxx。
vtkFloatArray *xCoords = vtkFloatArray::New();
for (i=0;i<47; i++) xCoords->InsertNextValue(x[i]);
vtkFloatArray*yCoords = vtkFloatArray::New();
for (i=0;i<33; i++) yCoords->InsertNextValue(y[i]);
vtkFloatArray *zCoords = vtkFloatArray::New();
for (i=0;i<44; i++) zCoords->InsertNextValue(z[i]);
vtkRectilinearGrid *rgrid =vtkRectilinearGrid::New();
rgrid->SetDimensions(47,33,44);
rgrid->SetXCoordinates(xCoords);
rgrid->SetYCoordinates(yCoords);
rgrid->SetZCoordinates(zCoords);
確保x,y,z三個方向的標量個數要與拓撲i,j,k三個方向維數保持一致。
提取計算平面
大多數情況下線性網格由接收vtkDataSet類型輸入的Filter來處理(見89頁“可視化技術”)。而直接接收vtkRectilinearGrid類型輸入的其中一個Filter是vtkRectilinearGridGeometryFilter。該Filter根據其Extent變數值來提取網格中的局部點、線段或者平面(Extent是一個描述地區的六維向量( , ))。
下面例子還是緊接上面代碼VTK/Examples/DataManipulation/Cxx/RGrid.cxx,我們如下提取一個平面:
vtkRectilinearGridGeometryFilter *plane =
vtkRectilinearGridGeometryFilter::New();
plane->SetInput(rgrid);
plane->SetExtent(0,46, 16,16, 0,43);
5.5 不規則網格可視化
不規則網格無論在拓撲上還是幾何上都是不規則的(3-2(f))。不規則網格常用來進行數值分析(如有限元分析)。任何一中單元類型都可以用來表示不規則網格。
手動建立vtkUnstructuredGrid
不規則網格定義是通過vtkPoints來定義幾何,通過插入單元來定義拓撲。(下面代碼取自VTK/Examples/DataManipulation/Tcl/BuildUGrid.tcl.)
vtkPoints tetraPoints
tetraPoints SetNumberOfPoints 4
tetraPoints InsertPoint 0 0 0 0
tetraPoints InsertPoint 1 1 0 0
tetraPoints InsertPoint 2 .5 1 0
tetraPoints InsertPoint 3 .5 .5 1
vtkTetra aTetra
[aTetra GetPointIds] SetId 0 0
[aTetra GetPointIds] SetId 1 1
[aTetra GetPointIds] SetId 2 2
[aTetra GetPointIds] SetId 3 3
vtkUnstructuredGrid aTetraGrid
aTetraGrid Allocate 1 1
aTetraGrid InsertNextCell [aTetra GetCellType] [aTetra GetPointIds]
aTetraGrid SetPoints tetraPoints
…insertother cells if any…
在為vtkUnstructuredGrid執行個體插入單中繼資料前,必須先執行Allocate()函數來分配空間。該函數的兩個參數分別表示初始分配的資料空間大小,和需要額外記憶體時擴充的記憶體空間大小。一般情況大比較大的值會有較好的效果(因為需要較少的記憶體重分配操作)。
提取局部網格
大部分情況下,不規則網格由接收vtkDataSet類型輸入的Filter處理(見89頁“可視化技術”)。直接接收vtkUnstructuredGrid類型輸入的其中一個Filter是vtkExtractUnstructuredGrid。這個filter根據指定的點ids,單元ids和幾何邊界來提取局部網格(Extent參數定義一個包圍盒)。下例代碼取自VTK/Examples/VisualizationAlgorithms/Tcl/ExtractUGrid.tcl。
vtkDataSetReader reader
reader SetFileName "$VTK_DATA_ROOT/Data/blow.vtk"
reader SetScalarsName "thickness9"
reader SetVectorsName "displacement9"
vtkCastToConcrete castToUnstructuredGrid
castToUnstructuredGrid SetInputConnection [reader GetOutputPort]
vtkWarpVector warp
warp SetInput [castToUnstructuredGrid GetUnstructuredGridOutput]
vtkConnectivityFilter connect
connect SetInputConnection [warp GetOutputPort]
connect SetExtractionModeToSpecifiedRegions
connect AddSpecifiedRegion 0
connect AddSpecifiedRegion 1
vtkDataSetMapper moldMapper
moldMapper SetInputConnection [reader GetOutputPort]
moldMapper ScalarVisibilityOff
vtkActor moldActor
moldActorSetMapper moldMapper
[moldActor GetProperty] SetColor .2 .2 .2
[moldActor GetProperty] SetRepresentationToWireframe
vtkConnectivityFilter connect2
connect2 SetInputConnection [warp GetOutputPort]
connect2 SetExtractionModeToSpecifiedRegions
connect2 AddSpecifiedRegion 2
vtkExtractUnstructuredGrid extractGrid
extractGrid SetInputConnection [connect2 GetOutputPort]
extractGrid CellClippingOn
extractGrid SetCellMinimum 0
extractGrid SetCellMaximum 23
vtkGeometryFilter parison
parison SetInputConnection [extractGrid GetOutputPort]
vtkPolyDataNormals normals2
normals2 SetInputConnection [parison GetOutputPort]
normals2 SetFeatureAngle 60
vtkLookupTable lut
lut SetHueRange 0.0 0.66667
vtkPolyDataMapper parisonMapper
parisonMapper SetInputConnection [normals2 GetOutputPort]
parisonMapper SetLookupTable lut
parisonMapper SetScalarRange 0.12 1.0
vtkActor parisonActor
parisonActor SetMapper parisonMapper
該例中我們採用單元裁剪(利用單元id號)和一個連通Filter來提取局部網格。vtkConnectivityFilter(以及vtkPolyDataConnectivityFilter)用來提取資料中的連通部分(當單元共用點時,他們是連通的)。SetExtractionModeToSpecifiedRegions()方法設定提取哪個連通地區。連通filter預設提取最大的連通地區。也可以如該例中那樣提取一個特定的地區,不過需要一些額外的實驗來對應各個地區。
不規則網格等值線提取
vtkContourGrid用來提取不規則網格的等值線。與一般的vtkContourFilter類相比,這個類更加高效。通常你並不需要直接執行個體化該類,因為當vtkContourFilter識別到輸入資料時vtkUnstructuredGrid類型時,vtkContourFilter內部會自動建立一個vtkContourGrid執行個體。
以上是可視化技術概述。接下來你可能希望學習影像處理和體繪製技術。也可以參考444頁“Filter總結”部分瞭解VTK中的Filter。
【第5章-可視化技術 翻譯完畢】