'''
compute_collision() will compute the collision of all the entities with
a Point while compute_first_collision() will always return its first entry.
Especially if a point is on an element edge this can be tricky.
You may also want to compare with the Cell.contains(Point) tool.
'''
# Script by Rudy at https://fenicsproject.discourse.group/t/
#           any-function-to-determine-if-the-point-is-in-the-mesh/275/3
import dolfin
from vtkplotter.dolfin import shapes, plot, printc

n  = 4
Px = 0.5
Py = 0.5
mesh = dolfin.UnitSquareMesh(n, n)
bbt = mesh.bounding_box_tree()
collisions = bbt.compute_collisions(dolfin.Point(Px, Py))
collisions1st = bbt.compute_first_entity_collision(dolfin.Point(Px, Py))
printc("collisions    : ", collisions)
printc("collisions 1st: ", collisions1st)

for cell in dolfin.cells(mesh):
    contains = cell.contains(dolfin.Point(Px, Py))
    printc("Cell", cell.index(), "contains P:", contains, c=contains)

###########################################
pt = shapes.Point([Px, Py], c='blue')

plot(mesh, pt, text=__doc__)

[((516, 543), 'dolfin.UnitSquareMesh', 'dolfin.UnitSquareMesh', (['n', 'n'], {}), '(n, n)\n', (537, 543), False, 'import dolfin\n'), ((706, 744), 'vtkplotter.dolfin.printc', 'printc', (['"""collisions    : """', 'collisions'], {}), "('collisions    : ', collisions)\n", (712, 744), False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((745, 786), 'vtkplotter.dolfin.printc', 'printc', (['"""collisions 1st: """', 'collisions1st'], {}), "('collisions 1st: ', collisions1st)\n", (751, 786), False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((800, 818), 'dolfin.cells', 'dolfin.cells', (['mesh'], {}), '(mesh)\n', (812, 818), False, 'import dolfin\n'), ((991, 1023), 'vtkplotter.dolfin.shapes.Point', 'shapes.Point', (['[Px, Py]'], {'c': '"""blue"""'}), "([Px, Py], c='blue')\n", (1003, 1023), False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((1025, 1053), 'vtkplotter.dolfin.plot', 'plot', (['mesh', 'pt'], {'text': '__doc__'}), '(mesh, pt, text=__doc__)\n', (1029, 1053), False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((611, 631), 'dolfin.Point', 'dolfin.Point', (['Px', 'Py'], {}), '(Px, Py)\n', (623, 631), False, 'import dolfin\n'), ((684, 704), 'dolfin.Point', 'dolfin.Point', (['Px', 'Py'], {}), '(Px, Py)\n', (696, 704), False, 'import dolfin\n'), ((849, 869), 'dolfin.Point', 'dolfin.Point', (['Px', 'Py'], {}), '(Px, Py)\n', (861, 869), False, 'import dolfin\n')]