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

对于vtk XML格式的文件: .vtu, .vtp, .vts, .vtr, .vti, .vto, 解析其存储单元、单元数据。

#include <vtkSmartPointer.h>
#include <vtkXMLReader.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLStructuredGridReader.h>
#include <vtkXMLRectilinearGridReader.h>
#include <vtkXMLHyperOctreeReader.h>
#include <vtkXMLCompositeDataReader.h>
#include <vtkXMLStructuredGridReader.h>
#include <vtkXMLImageDataReader.h>
#include <vtkDataSetReader.h>
#include <vtkDataSet.h>
#include <vtkUnstructuredGrid.h>
#include <vtkRectilinearGrid.h>
#include <vtkHyperOctree.h>
#include <vtkImageData.h>
#include <vtkPolyData.h>
#include <vtkStructuredGrid.h>
#include <vtkPointData.h>
#include <vtkCellData.h>
#include <vtkFieldData.h>
#include <vtkCellTypes.h>
#include <vtksys\SystemTools.hxx>

template<class TReader> vtkDataSet *ReadAnXMLFile(const char*fileName);

int main()
{
    char *argv[5];
    argv[0] = "cow.vtp";
    argv[1] = "cth.vtr";
    argv[2] = "chombo3d_1.vti";
    argv[3] = "multicomb_1.vts";
    argv[4] = "quadraticTetra01.vtu"; 
    int f = 0;
    while (f < 5)
    {
        vtkDataSet *dataSet;
        std::string extension =

        vtksys::SystemTools::GetFilenameLastExtension(argv[f]);
        if (extension == ".vtu")
        {
            dataSet = ReadAnXMLFile<vtkXMLUnstructuredGridReader>(argv[f]);
        }
        else if (extension == ".vtp")
        {
            dataSet = ReadAnXMLFile<vtkXMLPolyDataReader>(argv[f]);
        }
        else if (extension == ".vts")
        {
            dataSet = ReadAnXMLFile<vtkXMLStructuredGridReader>(argv[f]);
        }
        else if (extension == ".vtr")
        {
            dataSet = ReadAnXMLFile<vtkXMLRectilinearGridReader>(argv[f]);
        }
        else if (extension == ".vti")
        {
            dataSet = ReadAnXMLFile<vtkXMLImageDataReader>(argv[f]);
        }
        else if (extension == ".vto")
        {
            dataSet = ReadAnXMLFile<vtkXMLHyperOctreeReader>(argv[f]);
        }
        else if (extension == ".vtk")
        {
            dataSet = ReadAnXMLFile<vtkDataSetReader>(argv[f]);
        }
        else
        {
            std::cerr << argv[0] << " Unknown extension: " << extension << std::endl;
            return EXIT_FAILURE;
        }

        int numberOfCells = dataSet->GetNumberOfCells();
        int numberOfPoints = dataSet->GetNumberOfPoints();

        std::cout << "------------------------" << std::endl;
        std::cout << argv[f] << std::endl
            << " contains a " << std::endl
            << dataSet->GetClassName()
            << " that has " << numberOfCells << " cells"
            << " and " << numberOfPoints << " points." << std::endl;
        typedef std::map<int, int> CellContainer;
        CellContainer cellMap;
        for (int i = 0; i < numberOfCells; i++)
        {
            cellMap[dataSet->GetCellType(i)]++;
        }

        CellContainer::const_iterator it = cellMap.begin();
        while (it != cellMap.end())
        {
            std::cout << "\tCell type "
                << vtkCellTypes::GetClassNameFromTypeId(it->first)
                << " occurs " << it->second << " times." << std::endl;
            ++it;
        }

        vtkPointData *pd = dataSet->GetPointData();
        if (pd)
        {
            std::cout << " contains point data with "
                << pd->GetNumberOfArrays()
                << " arrays." << std::endl;
            for (int i = 0; i < pd->GetNumberOfArrays(); i++)
            {
                std::cout << "\tArray " << i
                    << " is named "
                    << (pd->GetArrayName(i) ? pd->GetArrayName(i) : "NULL")
                    << std::endl;
            }
        }
        vtkCellData *cd = dataSet->GetCellData();
        if (cd)
        {
            std::cout << " contains cell data with "
                << cd->GetNumberOfArrays()
                << " arrays." << std::endl;
            for (int i = 0; i < cd->GetNumberOfArrays(); i++)
            {
                std::cout << "\tArray " << i
                    << " is named "
                    << (cd->GetArrayName(i) ? cd->GetArrayName(i) : "NULL")
                    << std::endl;
            }
        }
        // Now check for field data
        if (dataSet->GetFieldData())
        {
            std::cout << " contains field data with "
                << dataSet->GetFieldData()->GetNumberOfArrays()
                << " arrays." << std::endl;
            for (int i = 0; i < dataSet->GetFieldData()->GetNumberOfArrays(); i++)
            {
                std::cout << "\tArray " << i
                    << " is named " << dataSet->GetFieldData()->GetArray(i)->GetName()
                    << std::endl;
            }
        }
        dataSet->Delete();
        f++;
    }
    return 0;
}

template<class TReader> vtkDataSet *ReadAnXMLFile(const char*fileName)
{
    vtkSmartPointer<TReader> reader =
        vtkSmartPointer<TReader>::New();
    reader->SetFileName(fileName);
    reader->Update();
    reader->GetOutput()->Register(reader);
    return vtkDataSet::SafeDownCast(reader->GetOutput());
}

运行结果
代码链接:http://pan.baidu.com/s/1i4OPjjZ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值