下面是vtk的一段示例代码:
vtkFloatArray* pcoords = vtkFloatArray::New();
// Note that by default, an array has 1 component.
// We have to change it to 3 for points
pcoords->SetNumberOfComponents(3);
// We ask pcoords to allocate room for at least 4 tuples
// and set the number of tuples to 4.
pcoords->SetNumberOfTuples(4);
// Assign each tuple. There are 5 specialized versions of SetTuple:
// SetTuple1 SetTuple2 SetTuple3 SetTuple4 SetTuple9
// These take 1, 2, 3, 4 and 9 components respectively.
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]);
}
// Create vtkPoints and assign pcoords as the internal data array.
vtkPoints* points = vtkPoints::New();
points->SetData(pcoords);
对于vtkXXXArray的类来说,其中的tuples指的是含有元素的个数,而其中的components指的是每个元素用几个元组组成,如对于法向量来说,是N个三元组组成。
上面的例子中指出了pcoord中一共有4个点,每个点包含x,y,z三个方向坐标。
使用points->SetData可以将vtkFloatArray的数组传递给点集合。
更常用的是对于点集的属性集操作,模型的属性信息都保存在其PointData中,通过GetPointData获得其点属性数据,而后可以进行修改更新:
vtkDoubleArray * src_attris =( vtkDoubleArray * ) src_ug->GetPointData()->GetScalars();
vtkDoubleArray * dst_attris = vtkDoubleArray::New();
pts_num = dst_ug->GetNumberOfPoints();
attri_num = src_attris->GetNumberOfComponents();
dst_attris->SetNumberOfTuples(pts_num);
dst_attris->SetNumberOfComponents(attri_num);
attri = new double[attri_num];
null_atrri = new double[attri_num];
for (i=0;i<attri_num;i++)
{
null_atrri[i] = 0.0;
}
for (ptid =0 ;ptid<pts_num;ptid++)
{
dst_ug->GetPoint(ptid,pt);
pos = src_ug->FindPoint(pt);
if (pos == -1)
{
null_count ++;
dst_attris->SetTupleValue(ptid,null_atrri);
continue;
}
src_attris->GetTupleValue(pos,attri);
dst_attris->SetTupleValue(ptid,attri);
}
dst_ug->GetPointData()->SetScalars(dst_attris);
dst_ug->Modified();
注意的是在vtkIntArray中,只实现了GetTupleValue, SetTupleValue两个操作,所以在vtkIntArray中,不同通过 SetTuple来设置其中的元素,只能通过GetTupleValue, SetTupleValue.