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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的XML相关文章