【问题标题】:Plot contour of vtk. file (from OpenFOAM) with Python-vtk module绘制 vtk 的轮廓。带有 Python-vtk 模块的文件(来自 OpenFOAM)
【发布时间】:2021-10-19 20:55:31
【问题描述】:

我有一个从OpenFOAM的cuttingPlane生成的vtk.file(压力场),格式为Legacy、POLYGONS和Field Data:

    # vtk DataFile Version 2.0
        sampleSurface
        ASCII
        DATASET POLYDATA
        POINTS 55181 double
        0.312848 -0.389703 0
        -0.319252 -0.384475 0
        0.246285 -0.434844 0
        ...(points  coordinates) ...

        POLYGONS 109010 436040
        3 54060 54066 54200
        3 54206 54200 54066
        3 54204 54065 54200
        3 54060 54200 54065
        ...

        POINT_DATA 55181
        FIELD attributes 1
        p 1 55181 float
        -0.622929 -0.202063 -0.860382 ....

我想做的是用 python-vtk 模块可视化这个文件,并输出 .png 图像。我可以在 Paraview 中绘制此文件的轮廓(请参阅enter image description here),但是当使用 python 脚本时,域是白色的并且没有压力分布,这很奇怪(请参阅enter image description here)。我还附上了我的python代码。

#!/usr/bin/env python

import vtk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.interpolate import griddata
from vtk.util.numpy_support import vtk_to_numpy

# 0. set the vtk.file path
filePath = '.../p_zNormal.vtk'

# reader 
reader = vtk.vtkPolyDataReader()            # vtkDataSetReader()
reader.SetFileName(filePath)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update() 

vtkdata = reader.GetOutput()
scalar_range = vtkdata.GetScalarRange()     # (0.0, 1.0) ?

np_coordinates = vtk_to_numpy(vtkdata.GetPoints().GetData()) 

pointData = vtkdata.GetPointData()
fieldData_vtk_array = pointData.GetArray(0)    
fieldData_np_array = np.array(vtk_to_numpy(fieldData_vtk_array))

DataMin = np.min(fieldData_np_array)
DataMax = np.max(fieldData_np_array)

# look-up table
pColorTable = vtk.vtkLookupTable()
pColorTable.SetHueRange(0, 0.67)             # red -> blue
pColorTable.SetNumberOfTableValues(16)       # number of colors
pColorTable.SetTableRange(scalar_range)
pColorTable.SetValueRange(0.0, 1.0)
pColorTable.Build()

# mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(reader.GetOutputPort())
mapper.SetScalarRange(DataMin,DataMax)
mapper.SetLookupTable(pColorTable)
mapper.ScalarVisibilityOn()
mapper.SetScalarModeToUsePointFieldData();
mapper.Update()

# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetLookupTable(pColorTable)
scalarBar.SetTitle("p (Pa)")
scalarBar.SetNumberOfLabels(10)
#scalarBar.GetTitleTextProperty().SetColor(0, 0, 0);
#scalarBar.GetTitleTextProperty().SetFontFamilyToArial();
#scalarBar.GetTitleTextProperty().SetFontSize(20);
#scalarBar.GetLabelTextProperty().SetColor(0, 0, 0);
#scalarBar.SetLabelFormat("%5.3f");
#scalarBar.GetLabelTextProperty().SetFontFamilyToArial();
#scalarBar.GetLabelTextProperty().SetFontSize(20);
scalarBar.SetUnconstrainedFontSize(1)

# render 
ren = vtk.vtkRenderer()
ren.AddActor(actor) 
ren.AddActor(scalarBar)               
ren.SetBackground(0.2, 0.3, 0.4)

# render window
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)             
renwin.SetWindowName("Test")
renwin.SetSize(800,600)
renwin.Render()

# interactor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)

iren.Initialize()
iren.Start()
              

看来我不能上传源文件.... 如果有人可以提供帮助,我将不胜感激!谢谢!

【问题讨论】:

    标签: python vtk openfoam


    【解决方案1】:

    尝试添加reader.ReadAllFieldsOn()

    【讨论】:

    • 嗨@mmusy,感谢您的建议!我已经尝试打开阅读器的所有设置,包括 ReadAllFields 但它不起作用......似乎我没有指定着色的变量名称。但是我尝试了几种方法都失败了。
    • 嗯,也许使用 GetFieldData() 而不是 GetPointData()?看看这个:kitware.github.io/vtk-examples/site/Cxx/PolyData/FieldData
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多