current position:Home>Use the properties of Python VTK implicit functions to select and cut data

Use the properties of Python VTK implicit functions to select and cut data

2022-01-31 18:15:24 Pie Da Da Xing

This is my participation 11 The fourth of the yuegengwen challenge 15 God , Check out the activity details :2021 One last more challenge

VTK,(visualizationtoolkit) It is a free software system with open resources , Mainly used in 3D computer graphics 、 Image processing and visualization .Vtk It is designed and implemented on the basis of object-oriented principle , Its kernel uses C++ Built , Contains about 250,000 Line code ,2000 Multiple classes , There are also several conversion interfaces , So it's free to pass Java,Tcl/Tk and Python Use of various languages vtk.

This paper mainly introduces how to use the properties of implicit functions to select and cut data , In particular, how to use the area separation attribute to select data .

Using implicit functions to select or extract data means selecting cells and points located in a specific area of the function ( And related attribute data ). To determine the point x-y-z Whether it is located in an area , We just need to calculate the point and check the coordinate symbol of the result . If all points of the cell are in the region , Then the unit is located in this area . here , Two ellipses are used in combination to select voxels in the volume dataset . Be careful , Extracting data usually changes the structure of the data set . In this case , The input type is image dataset , The output type is unstructured grid dataset .

Main function introduction

vtkQuadricClustering Is a class for cutting triangles , Get an approximate Geometry . Its input is vtkPolyData Data of type , Can handle all types of polygonal meshes , Its speed is very fast . It can quickly cut large mesh models , And supports mesh clip reduction ( utilize startAppend, Append, endAppend Method ), This avoids loading the entire model into memory . For large network models , It has good effect , But when the grid gets smaller , The triangulation effect is not very good , It needs to be combined with other methods .

vtk.vtkSampleFunction yes VTK An implicit function in represents a class , It can be used to sample the surface point by point at equal intervals .

vtkShrinkFilter Shrink the cells that make up any dataset of its centroid . The average position of cell points is used as the centroid of the calculation unit . Atrophy interrupts the result of another cell . The output of this filter is a generic dataset 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)

    #  take actor Add to 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 code 

give the result as follows :

image.png

copyright notice
author[Pie Da Da Xing],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311815217098.html

Random recommended