How do I find the element by coordinate in VTK?

225 views Asked by At

I have a mesh file generate by Gmsh(*.vtu), the mesh is a cube area and consist of tetrahedrons. Then I have a point (given by coordinate) in the cube, I want to find which tetrahedron contains the point, how did I do?

with pygmsh.occ.Geometry() as geom:
    geom.add_box([0, 0, 0],
                 [1, 1, 1], mesh_size=0.1)
    mesh = geom.generate_mesh()
mesh.write('original_gmsh.vtu')

uGridReader = vtkXMLUnstructuredGridReader()
uGridReader.SetFileName('original_gmsh.vtu')
uGridReader.Update()
uGrid: vtkUnstructuredGrid = uGridReader.GetOutput()

givenPoint = [0.5, 0.5, 0.5]
1

There are 1 answers

0
liamcsmith On BEST ANSWER

You should be able to use the FindAndGetCell() method that vtkUnstructuredGrid inherits from vtkDataSet. The python documentation for this can be found using help(vtkUnstructuredGrid.FindAndGetCell) within your python shell (assuming you have imported vtkUnstructuredGrid, if not prepend with vtk. as usual.

As a recommendation, consider check out the PyVista package, its far easier to use in my experience and uses VTK as its backend as well.