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
https://vtk.org/doc/nightly/html/classvtkClipPolyData.html

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

Clip function

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


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

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

python 3.7 vtk 8.1.2
)
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();

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

ʻ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

After Clip

Recommended Posts