VTK学习笔记3:IO操作读取写入xml文件和vtk文件

1.vtu文件

vtk实战(二十四)——读入vtu数据
vtk实战(三十)——写入vtu数据

2.vtp文件

2.1读入vtp文件

2.2 创建vtp文件

从网络示例和在线文档中,我收集了一些信息,创建一个完整的1-piece文件:
调用vtkXMLPloyDataWriter::SetInputData(vtkDataObject)
你传入一个vtkPolyData,你首先使用以下方法设置它:

SetVerts
SetLines
SetPolys
SetStrips

和父方法

SetPoints

但是我还没有弄清楚如何设置vtp字段PointData和CellData。

如何设置vtp字段PointData和CellData???

要设置点数据,您可以先获取点数据对象

vtkPointData* pointData = polydata->GetPointData();

然后你可以向它添加数组:

vtkFloatArray* myArray = vtkFloatArray::New();
myArray->SetName("myArray");
myArray->SetNumberOfComponents(1);
myArray->SetNumberOfTuples(polydata->GetNumberOfPoints());
// 这里设置数组元素...

pointData->AddArray(myArray);

单元格数据也是如此,只需将 PointData 替换为 CellData 即可,
并将元组的数量设置为单元格的数量而不是Point的数量

sample1

source.vtp文件包含两个Point,一条Line
Point:

(0,0,0)和(1,0,0)

vtkSmartPointer<vtkPolyData> source = nullptr;
	if (source == nullptr)
	{
		vtkNew<vtkPolyData> defaultSource;
		defaultSource->Allocate();
		vtkNew<vtkPoints> defaultPoints;
		defaultPoints->Allocate(6);
		defaultPoints->InsertNextPoint(0, 0, 0);
		defaultPoints->InsertNextPoint(1, 0, 0);
		vtkIdType defaultPointIds[2];
		defaultPointIds[0] = 0;
		defaultPointIds[1] = 1;
		defaultSource->SetPoints(defaultPoints);
		defaultSource->InsertNextCell(VTK_LINE, 2, defaultPointIds);
		source = defaultSource;
	}
	vtkSmartPointer <vtkXMLPolyDataWriter> polyWriter = vtkSmartPointer <vtkXMLPolyDataWriter>::New();
	polyWriter->SetFileName("source.vtp");
	polyWriter->SetInputData(source);
	polyWriter->SetDataModeToAscii();
	polyWriter->Write();

生成的vtp文件:

<?xml version="1.0"?>
<VTKFile type="PolyData" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <PolyData>
    <Piece NumberOfPoints="2" NumberOfVerts="0" NumberOfLines="1" NumberOfStrips="0" NumberOfPolys="0">
      <PointData>
      </PointData>
      <CellData>
      </CellData>
      <Points>
        <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="ascii" RangeMin="0" RangeMax="1">
          0 0 0 1 0 0
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              0
            </Value>
            <Value index="1">
              1
            </Value>
          </InformationKey>
        </DataArray>
      </Points>
      <Verts>
        <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
        <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
      </Verts>
      <Lines>
        <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="0" RangeMax="1">
          0 1
        </DataArray>
        <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="2" RangeMax="2">
          2
        </DataArray>
      </Lines>
      <Strips>
        <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
        <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
      </Strips>
      <Polys>
        <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
        <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
      </Polys>
    </Piece>
  </PolyData>
</VTKFile>

ParaView显示效果:
在这里插入图片描述

vtk实战(二十五)——读入vtp数据
vtk实战(三十一)——写入vtp数据
vtk实战(十)—文本格式转vtp格式
VTK学习笔记1:vtkXMLPolyDataWriter

相关文档:

vtk实战(十四)——解析vtk XML 文件的内容

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容