我正在尝试使用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)是否有与此相关的适当文档?
答案 0 :(得分: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