原文轉自http://blog.csdn.net/timzc/article/details/6053198
vtkPoints:SetPoint( vtkIdType id, double x[3] )和InsertPoint( vtkIdType id, double x[3] )可以設定點的vtkIdType(類似ID值)和三維座標。兩個函數的區別在於:InsertPoint先要完成點的範圍檢查和記憶體配置工作,索引速度較慢。註:這兩個函數的實質是調用SetTuple( const vtkIdType i, const float *tuple )和InsertTuple( const vtkIdType i, const float *tuple )在數組的第i個位置賦值。適用bit/char/data/double/float等所有數組。如 voidFloatArray::SetTuple( const vtkIdType i, const float *tuple ); 參數1位置,參數2實際資料:pcoords->SetTuple( i, pts[i] );
vtkCellArray中設定cell單元,調用InsertNextCell函數逐步添加新的cell,例如函數vtkCellArray::InsertNextCell ( vtkIdType npts, vtkIdType * pts )的第一個參數值標是cell中點的個數,第二個參數指向那些點的座標資料。(說明:vtkIdType *pts,儲存的是所包括點在points中的順序資訊,其個數當然應該和前面的npts一致。這裡,2點可以連成一條線,三點可以得到一個面。也就是說:vtk中關於點、線、面的那些資訊都是存放在cellarray中,應用時也是直接對cellarray指標進行處理,資料的寫入和讀取在vtkCellArray類完成。
接下來的工作是定義一個vtkPolyData,得到包括頂點、線、多邊形、三角形連環在內的幾何結構,即三維實體。這裡通過函數SetPoints設定點資訊,SetPolys設定單元排列(cell array)定義多邊形,cell array設定單元排列(cell array)定義線,SetStrips設定單元排列(cell array)定義三角形連環strip,SetVerts設定頂點,諸如此類。
#include "stdafx.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
int _tmain(int argc, _TCHAR* argv[])
{
int i;
//建立一個浮點型數組儲存"點"
vtkFloatArray *pcoords = vtkFloatArray::New();
//設定維度,點->3
pcoords->SetNumberOfComponents( 3 );
//設定數組個數
pcoords->SetNumberOfTuples( 4 );
//指定每一個數組,具體的點座標
float pts[4][3] = { { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 },
{ 1.0, 0.0, 0.0 }, { 1.0, 1.0, 0.0}} ;
for( i=0; i<4; i++ )
{
//設定數組中的點
pcoords->SetTuple( i, pts[i] );
}
vtkPoints *points = vtkPoints::New();
//獲得四個點
points->SetData( pcoords );
//建立網格數組
vtkCellArray *strips = vtkCellArray::New();
//設定單元由幾個點組成
/* strips->InsertNextCell( 4 );
strips->InsertCellPoint( 0 );
strips->InsertCellPoint( 1 );
strips->InsertCellPoint( 2 );
strips->InsertCellPoint( 3 );*/
strips->InsertNextCell( 3 );
strips->InsertCellPoint( 0 );
strips->InsertCellPoint( 1 );
strips->InsertCellPoint( 2 );
//strips->InsertCellPoint( 3 );
//建立整形數組
vtkIntArray *temperature = vtkIntArray::New();
temperature->SetName( "Temperature" );
temperature->InsertNextValue( 60 );
temperature->InsertNextValue( 70 );
temperature->InsertNextValue( 80 );
// temperature->InsertNextValue( 90 );
/* //建立雙精確度型數組
vtkDoubleArray *vorticity = vtkDoubleArray::New();
vorticity->SetName( "Vorticity" );
vorticity->InsertNextValue( 2.7 );
vorticity->InsertNextValue( 4.1 );
vorticity->InsertNextValue( 5.3 );
vorticity->InsertNextValue( 3.4 );*/
//建立資料集
vtkPolyData *polydata = vtkPolyData::New();
//指定點和網格
polydata->SetPoints( points );
polydata->SetStrips( strips );
//指定標量
polydata->GetPointData()->SetScalars( temperature );
//polydata->GetPointData()->AddArray( vorticity );
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInput( polydata );
mapper->SetScalarRange( 0, 40 );
// Create an actor.
vtkActor* actor = vtkActor::New();
actor->SetMapper(mapper);
// Create the rendering objects.
vtkRenderer* ren = vtkRenderer::New();
ren->AddActor(actor);
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren);
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
iren->Initialize();
iren->Start();
return 0;
}