A filter that extracts only elements that contain values within the specified range.
** class reference ** https://vtk.org/doc/nightly/html/classvtkThreshold.html
Specifically, the example in the following paraview is easy to understand
For the calculation example, I used pitzDaily
from the tutorial of ʻOpenFOAM`.
After reading the calculation result, press this button
Result of applying a specified range of 5 to 15 for pressure p
Take the complement
python 3.7 vtk 8.1.2
Confirmation method
import Vtk
print(vtk.vtkVersion.GetVTKSourceVersion())
>> vtk version 8.1.2
I use vtkOpenFOAMReader
to read the result of OpenFOAM
About vtkOpenFOAMReader
, I will also summarize it in here.
import vtk
#Read the result of OpenFOAM
filename = "case1.foam"
reader = vtk.vtkOpenFOAMReader()
reader.SetFileName(filename)
reader.CreateCellToPointOn()
reader.DecomposePolyhedraOn()
reader.EnableAllCellArrays()
reader.Update()
#Apply the latest Time result
n_step = reader.GetTimeValues().GetNumberOfValues()
latest_time = reader.GetTimeValues().GetValue(n_step-1)
reader.UpdateTimeStep(latest_time)
reader.Update()
filter_threshold = vtk.vtkThreshold()
filter_threshold.SetInputConnection(reader.GetOutputPort())
###################
#Add settings here#
###################
filter_threshold.Update()
filter = vtk.vtkGeometryFilter()
filter.SetInputConnection(filter_threshold.GetOutputPort())
filter.Update()
mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputConnection(filter.GetOutputPort()) #Set filter in mapper
mapper.SetScalarModeToUseCellFieldData() #Set for scalar data
# renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(actor) #Set actor in renderer
##Background color setting
renderer.GradientBackgroundOn() #Set gradient background
renderer.SetBackground2(0.2,0.4,0.6) #Top color
renderer.SetBackground(1,1,1) #Bottom color
#Window
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer) #Set renderer in Window
iren = vtk.vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
renWin.SetSize(850, 850)
renWin.Render()
iren.Start();
ThresholdBetween()
Limitation by specifying upper and lower limits
#Set the threshold between 5 and 15
filter_threshold.ThresholdBetween(5,15)
#enum FieldAssociations
# {
# FIELD_ASSOCIATION_POINTS,
# FIELD_ASSOCIATION_CELLS,
# FIELD_ASSOCIATION_NONE,
# FIELD_ASSOCIATION_POINTS_THEN_CELLS,
# FIELD_ASSOCIATION_VERTICES,
# FIELD_ASSOCIATION_EDGES,
# FIELD_ASSOCIATION_ROWS,
# NUMBER_OF_ASSOCIATIONS
# };
FIELD_ASSOCIATION_POINTS = 0
FIELD_ASSOCIATION_CELLS = 1
# "p"Is filtering against pressure
filter_threshold.SetInputArrayToProcess(0,0,0,FIELD_ASSOCIATION_CELLS ,"p")
filter_threshold.Update()
Consistent with the results in paraview above
If you don't know how to use the method, the help function may help.
help(filter_threshold.SetInputArrayToProcess)
>>
Help on built-in function SetInputArrayToProcess:
SetInputArrayToProcess(...) method of vtkFiltersCorePython.vtkThreshold instance
V.SetInputArrayToProcess(int, int, int, int, string)
C++: virtual void SetInputArrayToProcess(int idx, int port,
int connection, int fieldAssociation, const char *name)
V.SetInputArrayToProcess(int, int, int, int, int)
C++: virtual void SetInputArrayToProcess(int idx, int port,
int connection, int fieldAssociation, int fieldAttributeType)
V.SetInputArrayToProcess(int, vtkInformation)
C++: virtual void SetInputArrayToProcess(int idx,
vtkInformation *info)
V.SetInputArrayToProcess(int, int, int, string, string)
C++: virtual void SetInputArrayToProcess(int idx, int port,
int connection, const char *fieldAssociation,
const char *attributeTypeorName)
Set the input data arrays that this algorithm will process.
Specifically the idx array that this algorithm will process
(starting from 0) is the array on port, connection with the
specified association and name or attribute type (such as
SCALARS). The fieldAssociation refers to which field in the data
object the array is stored. See vtkDataObject::FieldAssociations
for detail.
ThresholdByUpper()
Limit by specifying the upper limit Only areas larger than the specified value are displayed
#filter_threshold.ThresholdBetween(5,15)
filter_threshold.ThresholdByUpper(10)
Nothing is displayed because the target area does not exist
ThresholdByLower()
Limit by specifying the lower limit Only areas smaller than the specified value are displayed
#filter_threshold.ThresholdBetween(5,15)
filter_threshold.ThresholdByLower(5)
As a result, it matched the result of Invert in the above paraview.
Apparently the Invert relationship is already implemented in c ++, but not in the python version.
filter_threshold.SetInvert(True)
AttributeError
---> 31 filter_threshold.SetInvert(True)
AttributeError: 'vtkFiltersCorePython.vtkThreshold' object has no attribute 'SetInvert'
GetUpperThreshold Get the upper limit of the threshold
filter_threshold.ThresholdBetween(-100,100)
filter_threshold.GetUpperThreshold()
>> 100
GetLowerThreshold Get the lower limit of the threshold
filter_threshold.ThresholdBetween(-100,100)
filter_threshold.GetLowerThreshold()
>> -100
Set/GetAttributeMode
filter_threshold.SetAttributeModeToDefault()
filter_threshold.GetAttributeMode()
>>0
filter_threshold.SetAttributeModeToUsePointData()
filter_threshold.GetAttributeMode()
>>1
filter_threshold.GetAttributeModeAsString()
>>'UsePointData'
filter_threshold.SetAttributeModeToUseCellData()
filter_threshold.GetAttributeMode()
>>2
filter_threshold.GetAttributeModeAsString()
>>'UseCellData'
filter_threshold.SetAttributeMode(2)
filter_threshold.GetAttributeModeAsString()
>>'UseCellData'
I will update it when I feel like it
Recommended Posts