使用python中的'vtkXMLUnstructuredGridReader'从VTU文件中读取矢量信息作为多维数组

问题描述 投票:0回答:1

我正在尝试使用vtkXMLUnstructuredGridReader从python中的VTU文件中读取矢量字段信息。要读取的矢量场是N * 3维的阵列,其中N是单元的数量,3是矢量的分量的数量。 VTU文件看起来像这样(没有XML数据),

<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <UnstructuredGrid>
    <FieldData>
      <DataArray type="Float64" Name="timeInPs" NumberOfTuples="1" format="appended" RangeMin="600"                  RangeMax="600"                  offset="0"                   />
    </FieldData>
    <Piece NumberOfPoints="145705"               NumberOfCells="838547"              >
      <PointData Scalars="Material" Vectors="Magnetization">
        <DataArray type="Float64" Name="Magnetization" NumberOfComponents="3" format="appended" RangeMin="1"                    RangeMax="1"                    offset="48"                  />
        <DataArray type="Int32" Name="Material" format="appended" RangeMin="0"                    RangeMax="0"                    offset="4455172"             />
      </PointData>
      <CellData>
      </CellData>
      <Points>
        <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="appended" RangeMin="1.0415804282e-12"     RangeMax="10.00000052"          offset="4456528"             >
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1.0415804282e-12
            </Value>
            <Value index="1">
              10.00000052
            </Value>
          </InformationKey>
        </DataArray>
      </Points>
      <Cells>
        <DataArray type="Int64" Name="connectivity" format="appended" RangeMin=""                     RangeMax=""                     offset="6589768"             />
        <DataArray type="Int64" Name="offsets" format="appended" RangeMin=""                     RangeMax=""                     offset="20080856"            />
        <DataArray type="UInt8" Name="types" format="appended" RangeMin=""                     RangeMax=""                     offset="21531024"            />
      </Cells>
    </Piece>
  </UnstructuredGrid>
  <AppendedData encoding="base64">


为此我做了一些在线搜索,即使我找不到合适的文档,我在这里找到了一个线程(Reading data from a raw VTK (.vtu) file)我尝试使用这里提供的代码

import vtk
import numpy
filname = trial.vtu
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update() 
output = reader.GetOutput()
potential = output.GetPointData().GetArray("Magnetization")
print potential

但作为输出,而不是获得N * 3数组我得到以下。

  vtkDoubleArray (0x28567a0)
  Debug: Off
  Modified Time: 389
  Reference Count: 2
  Registered Events: (none)
  Name: Magnetization
  Data type: double
  Size: 24519
  MaxId: 24518
  NumberOfComponents: 3
  Information: 0x1fbab50
    Debug: Off
    Modified Time: 388
    Reference Count: 1
    Registered Events: (none)
  Name: Magnetization
  Number Of Components: 3
  Number Of Tuples: 8173
  Size: 24519
  MaxId: 24518
  LookupTable: (none)

这包含有关该字段的所有信息,除了矢量组件为N * 3数组。

我有两个问题,

1)代码中缺少什么?

2)有没有适当的文件?

python numpy vtk
1个回答
0
投票

您导入了numpy包并忘记了vtk numpy支持包。我发布了您的示例代码并添加了缺少的行。

import vtk
import numpy as np
from vtk.util.numpy_support import vtk_to_numpy #thats what you need 

filname = trial.vtu
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update() 
output = reader.GetOutput()

# apply the vtk_to_numpy function to your output. Your output should be a N*3 numpy 
# array.
potential = vtk_to_numpy(output.GetPointData().GetArray("Magnetization"))
print potential
© www.soinside.com 2019 - 2024. All rights reserved.