[PYTHON] vtkClipPolyData / DataSet Summary (Updated from time to time)

About vtkClipPolyData / DataSet

I will summarize Clip, which limits the area of the shape to be displayed based on the source shape.

When expressing the shape with vertices, lines, triangle like STL, vtkPolyData When expressing with an unstructured grid like ʻOpenFOAM, is it used as vtkDataSet`? I don't know the details yet

vtkClipPolyData class reference

https://vtk.org/doc/nightly/html/classvtkClipPolyData.html

image.png

vtkClipDataSet class reference

https://vtk.org/doc/nightly/html/classvtkClipDataSet.html

image.png

Check the function with paraview

Clip function image.png

When ClipType is set to Plane, the display area is limited using the plane with the origin and normal.

image.png

image.png

By switching the Invert check, the displayed area can be reversed.

image.png

When the Crinkle clip is applied, the mesh contained in the plane retains its original shape. image.png

Operation check with python

environment

python 3.7 vtk 8.1.2

Example of Clipping using the base code (plane)

)

import vtk

filename = "flange.stl"
reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
reader.Update()

normal='y' # e.g. x,y,z,-x,-y,-z,X,Y,Z,-X,-Y,or -Z

NORMALS = {
    'x': [1, 0, 0],
    'y': [0, 1, 0],
    'z': [0, 0, 1],
    '-x': [-1, 0, 0],
    '-y': [0, -1, 0],
    '-z': [0, 0, -1],
}
normal = NORMALS[normal.lower()] # or e.g. [1.0/sqrt(2.0), 1.0/sqrt(2.0), 0]
origin = (0,0,0)
invert=True

plane = vtk.vtkPlane()
plane.SetNormal(normal)
plane.SetOrigin(origin)

alg = vtk.vtkClipPolyData()
alg.SetInputConnection(reader.GetOutputPort())

alg.SetClipFunction(plane)
alg.SetInsideOut(invert)
alg.Update()

# mapper
mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputConnection(alg.GetOutputPort()) #Set filter in mapper
mapper.SetScalarModeToUseCellFieldData() #Set for scalar data

# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)             #Set mapper for actor
actor.GetProperty().EdgeVisibilityOn()

# 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();

image.png

SetClipFunction(vtkImplicitFunction e.g. vtkPlane)

Set the object that will be the source of Clip such as vtkPlane

List of inheritance destinations of vtkImplicitFunction https://vtk.org/doc/nightly/html/classvtkImplicitFunction.html

SetInsideOut(boolean)

Method to switch ʻInvert`

Can also be set with ʻInsideOutOn () or ʻInsideOutOff ()

Example of performing Clip using a quadrangle (Cube)

Use vtkBox to specify the coordinates of BoundingBox https://vtk.org/doc/nightly/html/classvtkBox.html


box = vtk.vtkBox()
box.SetXMin(0.0,-0.001,-0.01) # [xMin,yMin,zMin]
box.SetXMax(0.1,0.1,0.1)      # [xMax,yMax,zMax]

alg = vtk.vtkClipPolyData()
alg.SetInputConnection(reader.GetOutputPort())

alg.SetClipFunction(box)

image.png

Example for OpenFOAM

ʻOpenFOAM is an unstructured grid, so use vtkClipDataSet`

import vtk

filename = "case1.foam"
reader = vtk.vtkOpenFOAMReader()
reader.SetFileName(filename)
reader.CreateCellToPointOn()
reader.DecomposePolyhedraOn()
reader.EnableAllCellArrays()
reader.Update()

n_step = reader.GetTimeValues().GetNumberOfValues()
latest_time = reader.GetTimeValues().GetValue(n_step-1)
reader.UpdateTimeStep(latest_time)
reader.Update()


normal='y' # e.g. x,y,z,-x,-y,-z,X,Y,Z,-X,-Y,or -Z

NORMALS = {
    'x': [1, 0, 0],
    'y': [0, 1, 0],
    'z': [0, 0, 1],
    '-x': [-1, 0, 0],
    '-y': [0, -1, 0],
    '-z': [0, 0, -1],
}

normal = NORMALS[normal.lower()] # or e.g. [1.0/sqrt(2.0), 1.0/sqrt(2.0), 0]

origin = (0,0,0)
invert=False

plane = vtk.vtkPlane()
plane.SetNormal(normal)
plane.SetOrigin(origin)

alg = vtk.vtkClipDataSet()
alg.SetInputConnection(reader.GetOutputPort())
alg.SetClipFunction(plane)
alg.SetInsideOut(invert)
alg.Update()

filter = vtk.vtkGeometryFilter()
filter.SetInputConnection(alg.GetOutputPort())
# filter.SetInputConnection(reader.GetOutputPort()) //When clip Off
filter.Update()

# mapper
mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputConnection(filter.GetOutputPort()) #Set filter in mapper
mapper.SetScalarModeToUseCellFieldData() #Set for scalar data

# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)             #Set mapper for actor
actor.GetProperty().EdgeVisibilityOn()

# 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();

Before Clip image.png

After Clip image.png

Recommended Posts

vtkClipPolyData / DataSet Summary (Updated from time to time)
vtkXMLUnstructuredGridReader Summary (updated from time to time)
vtkOpenFOAMReader Summary (Updated from time to time)
Summary of vtkThreshold (updated from time to time)
Summary of gcc options (updated from time to time)
Machine learning python code summary (updated from time to time)
Engineer vocabulary (updated from time to time)
Tensorflow memo [updated from time to time]
[Updated from time to time] Summary of design patterns in Java
Private Python handbook (updated from time to time)
[Updated from time to time] PostmarketOS related notes
[Updated from time to time] LetCode algorithm and library
Notes on machine learning (updated from time to time)
OpenFOAM post-processing cheat sheet (updated from time to time)
progate Python learning memo (updated from time to time)
Useful help sites, etc. (updated from time to time)
Apache settings, log confirmation, etc. (* Updated from time to time)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
[Updated from time to time] Review of Let Code NumPy
I read the Chainer reference (updated from time to time)
Python (from first time to execution)
[Notes / Updated from time to time] This and that of Azure Functions
[Note] AI / machine learning / python related websites [updated from time to time]
Summary of folders where Ruby, Python, PostgreSQL, etc. are installed on macOS (updated from time to time)
Updated to Python 2.7.9
Sum from 1 to 10
(Updated from time to time) Summary of machine learning APIs that allow you to quickly build apps by Team AI
Understand design patterns by comparing implementations in JavaScript and Java [Updated from time to time]
[Updated from time to time] Python memos often used for data analysis [N division, etc.]
A memorandum of commands, packages, terms, etc. used in linux (updated from time to time)
(Updated from time to time) Storage location of various VS Code configuration files Memorandum memo
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
List of my articles that may be useful in competition pros (updated from time to time)
Transition from WSL1 to WSL2
From editing to execution
Updated Hospital_dashboard to ver.2.0
[Introduction to matplotlib] Read the end time from COVID-19 data ♬