LoginSignup
0
0

More than 3 years have passed since last update.

vtkThresholdのまとめ(随時更新)

Last updated at Posted at 2020-04-26

vtkThresholdとは

指定した範囲内の値を含む要素のみ抽出するフィルターです

classリファレンス
https://vtk.org/doc/nightly/html/classvtkThreshold.html

具体的には、次のparaviewでの例がわかりやすいです

paraviewで機能の確認

計算例はOpenFOAMのtutorialのpitzDailyを用いました

Threshold適用前

image.png

計算結果を読みこんた後、このボタンを押します
image.png

Threshold適用後

圧力pに対して5から15の範囲を指定し適用した結果

image.png

TInvertオプション適用後

補集合をとります

image.png

pythonでの動作確認

環境

python 3.7
vtk 8.1.2

確認方法
import Vtk
print(vtk.vtkVersion.GetVTKSourceVersion())
>> vtk version 8.1.2

ベースとなるコード

OpenFOAMの結果を読みこむためにvtkOpenFOAMReaderを用いてます
vtkOpenFOAMReaderについてはこちらでもまとめていきます

import vtk
# OpenFOAMの結果を読み込み
filename = "case1.foam"
reader = vtk.vtkOpenFOAMReader()
reader.SetFileName(filename)
reader.CreateCellToPointOn()
reader.DecomposePolyhedraOn()
reader.EnableAllCellArrays()
reader.Update()
# latestTimeの結果を適用
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())

###################
# ここに設定を加える #
###################
filter_threshold.Update()


filter = vtk.vtkGeometryFilter()
filter.SetInputConnection(filter_threshold.GetOutputPort())
filter.Update()

mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputConnection(filter.GetOutputPort()) #mapperにfilterを設定
mapper.SetScalarModeToUseCellFieldData() #scalarデータ用に設定

# renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)            #rendererにactorを設定

##背景色の設定
renderer.GradientBackgroundOn()      #グラデーション背景を設定
renderer.SetBackground2(0.2,0.4,0.6) #上面の色
renderer.SetBackground(1,1,1)        #下面の色

#Window
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)         #Windowにrendererを設定
iren = vtk.vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
renWin.SetSize(850, 850)
renWin.Render()
iren.Start();

基本

ThresholdBetween()

上下限値指定による制限


# 閾値を5から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"で圧力に対してフィルターをかけている
filter_threshold.SetInputArrayToProcess(0,0,0,FIELD_ASSOCIATION_CELLS ,"p")
filter_threshold.Update()

前掲のparaviewでの結果と一致しました

image.png

もしメソッドの使い方がわからない場合は、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()

上限値指定による制限
指定した値より大きい領域のみ表示されます

#filter_threshold.ThresholdBetween(5,15)
filter_threshold.ThresholdByUpper(10)

対象となる領域が存在しないため、何も表示されていません
image.png

ThresholdByLower()

下限値指定による制限
指定した値より小さい領域のみ表示されます

#filter_threshold.ThresholdBetween(5,15)
filter_threshold.ThresholdByLower(5)

結果的に、前掲のparaviewでのInvertの結果と一致しました

image.png

Invert関係

どうやらInvert関係はc++では実装済みですが、python版には実装されていないようです

filter_threshold.SetInvert(True)

AttributeError  
---> 31 filter_threshold.SetInvert(True)
AttributeError: 'vtkFiltersCorePython.vtkThreshold' object has no attribute 'SetInvert'

その他メソッド

GetUpperThreshold

閾値の上限値を取得

filter_threshold.ThresholdBetween(-100,100)
filter_threshold.GetUpperThreshold()
>> 100

GetLowerThreshold

閾値の下限値を取得

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'

気が向いた時に更新していきます

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0