This is the 15th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

VTK (visual visualizationtoolkit) is an open source free software system for 3d computer graphics, image processing and visualization. Vtk is designed and implemented on the basis of object-oriented principles. Its core is built in C++, contains approximately 250,000 lines of code, over 2000 classes, and contains several conversion interfaces, so it is also free to use Vtk through Java, Tcl/Tk, and Python languages.

This article focuses on how to use the properties of implicit functions to select and clip data, especially how to use the region-delimited properties to select data.

Selecting or extracting data using an implicit function means selecting cells and points (and associated attribute data) that are located within a specific region of the function. To determine whether the point X-y-z is in a region, we simply calculate that point and check the resultant coordinate sign. A cell is in a region if all of its points are in a region. Here, two ellipses are used in combination to select the voxel in the volume dataset. Note that extracting data often changes the structure of the data set. In this case, the input type is an image dataset and the output type is an unstructured grid dataset.

Introduction to main functions

VtkQuadricClustering is a class for cutting triangles to get an approximate geometry. Its input is vtkPolyData type data. It can handle all types of polygon meshes. It is very fast. It can quickly reduce large mesh models, and supports mesh fragment reduction (using startAppend, Append, endAppend methods) to avoid loading the entire model into memory. For large network model, it has a good effect, but when the grid becomes small, the effect of triangulation is not very good, so other methods should be combined.

VTK. VtkSampleFunction is an implicit function representation class in VTK, which can be used for evenly spaced point-by-point sampling of surfaces.

VtkShrinkFilter constitutes a cell for an arbitrary data set of its center of mass. The average position of the cell points is the centroid of the computing cell. Atrophy interrupts the result of opening another cell. The output of this filter is a generic dataset of type vtkUnstructuredGrid.


import vtk


def main() :
    colors = vtk.vtkNamedColors()

    ren1 = vtk.vtkRenderer()

    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren1)

    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    quadric = vtk.vtkQuadric()
    quadric.SetCoefficients(0.5.1.0.2.0.0.1.0.0.0.2.0.0)

    sample = vtk.vtkSampleFunction()
    sample.SetSampleDimensions(50.50.50)
    sample.SetImplicitFunction(quadric)
    sample.ComputeNormalsOff()

    trans = vtk.vtkTransform()
    trans.Scale(1.0.5.0.333)

    sphere = vtk.vtkSphere()
    sphere.SetRadius(0.25)
    sphere.SetTransform(trans)

    trans2 = vtk.vtkTransform()
    trans2.Scale(0.25.0.5.1.0)

    sphere2 = vtk.vtkSphere()
    sphere2.SetRadius(0.25)
    sphere2.SetTransform(trans2)

    booleanUnion = vtk.vtkImplicitBoolean()
    booleanUnion.AddFunction(sphere)
    booleanUnion.AddFunction(sphere2)
    booleanUnion.SetOperationType(0)  # boolean Union

    extract = vtk.vtkExtractGeometry()
    extract.SetInputConnection(sample.GetOutputPort())
    extract.SetImplicitFunction(booleanUnion)

    shrink = vtk.vtkShrinkFilter()
    shrink.SetInputConnection(extract.GetOutputPort())
    shrink.SetShrinkFactor(0.5)

    dataMapper = vtk.vtkDataSetMapper()
    dataMapper.SetInputConnection(shrink.GetOutputPort())
    dataActor = vtk.vtkActor()
    dataActor.SetMapper(dataMapper)

    # outline
    outline = vtk.vtkOutlineFilter()
    outline.SetInputConnection(sample.GetOutputPort())

    outlineMapper = vtk.vtkPolyDataMapper()
    outlineMapper.SetInputConnection(outline.GetOutputPort())

    outlineActor = vtk.vtkActor()
    outlineActor.SetMapper(outlineMapper)
    outlineActor.GetProperty().SetColor(0.0.0)

    Add actor to the window and set the size of the relevant window
    #
    ren1.AddActor(outlineActor)
    ren1.AddActor(dataActor)
    ren1.SetBackground(colors.GetColor3d("SlateGray"))

    renWin.SetWindowName('ExtractData')

    renWin.Render()
    ren1.GetActiveCamera().Azimuth(30)
    ren1.GetActiveCamera().Elevation(30)

    renWin.Render()
    iren.Start()


if __name__ == '__main__':
    main()
Copy the code

The results are as follows: