Skip to content

vtk转vtp

*.vtk 是旧版本vtk文件(图形、图片、数据混用后缀)
*.vtp 只是vtkPolyData

  • cpp
        vtkSmartPointer<vtkPolyData> data;
        VtkUtil::ReadVTU(data, "/home/yx/Desktop/444.vtk");
        VtkUtil::WriteVTP(data, "/home/yx/Desktop/444.vtp");
void VtkUtil::ReadVTU(vtkSmartPointer<vtkPolyData> &surface, QString filename) {
    vtkNew<vtkUnstructuredGridReader> reader;
    reader->SetFileName(filename.toLocal8Bit().data());
    reader->Update();
    vtkSmartPointer<vtkDataSetSurfaceFilter> surface_filter =
        vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
    surface_filter->SetInputData(reader->GetOutput());
    surface_filter->Update();
    surface = surface_filter->GetOutput();
}

void VtkUtil::WriteVTP(
    vtkSmartPointer<vtkPolyData> surface, QString filename) {
    vtkNew<vtkXMLPolyDataWriter> writer;
    writer->SetFileName(filename.toLocal8Bit().data());
    writer->SetInputData(surface);
    writer->Write();
}
  • python
import vtk

def main():
    WriteVTP("/home/yx/Desktop/444.vtp", ReadVTU("/home/yx/Desktop/444.vtk"))

def WriteVTP(filename, vtkpolydata):
    write = vtk.vtkXMLPolyDataWriter()
    write.SetFileName(filename)
    write.SetDataModeToBinary()
    write.SetInputData(vtkpolydata)
    write.Write()

def ReadVTU(filename):
    reader = vtk.vtkUnstructuredGridReader()
    reader.SetFileName(filename)
    reader.Update()
    surface_filter = vtk.vtkDataSetSurfaceFilter()
    surface_filter.SetInputData(reader.GetOutput())
    surface_filter.Update()
    vtkpolydata = surface_filter.GetOutput()
    print(" 输入 UNSTRUCTURED_GRID point 数量 ")
    print(reader.GetOutput().GetNumberOfPoints())
    print(" \n ")
    print(" 输出 vtkPolyData point 数量 ")
    print(surface_filter.GetOutput().GetNumberOfPoints())
    print(" \n ")
    return vtkpolydata

if __name__ == '__main__':
    main()