我正在尝试使用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">为此,我做了一些在线搜索,尽管我找不到关于这方面的适当文档,但我在Stack here (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)这方面有没有合适的文档?
发布于 2019-02-19 17:50:09
您导入了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 potentialhttps://stackoverflow.com/questions/54748717
复制相似问题