'''
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__)

[((15, 7, 15, 34), 'dolfin.UnitSquareMesh', 'dolfin.UnitSquareMesh', ({(15, 29, 15, 30): 'n', (15, 32, 15, 33): 'n'}, {}), '(n, n)', False, 'import dolfin\n'), ((19, 0, 19, 38), 'vtkplotter.dolfin.printc', 'printc', ({(19, 7, 19, 25): '"""collisions    : """', (19, 27, 19, 37): 'collisions'}, {}), "('collisions    : ', collisions)", False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((20, 0, 20, 41), 'vtkplotter.dolfin.printc', 'printc', ({(20, 7, 20, 25): '"""collisions 1st: """', (20, 27, 20, 40): 'collisions1st'}, {}), "('collisions 1st: ', collisions1st)", False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((22, 12, 22, 30), 'dolfin.cells', 'dolfin.cells', ({(22, 25, 22, 29): 'mesh'}, {}), '(mesh)', False, 'import dolfin\n'), ((27, 5, 27, 37), 'vtkplotter.dolfin.shapes.Point', 'shapes.Point', (), '', False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((29, 0, 29, 28), 'vtkplotter.dolfin.plot', 'plot', (), '', False, 'from vtkplotter.dolfin import shapes, plot, printc\n'), ((17, 36, 17, 56), 'dolfin.Point', 'dolfin.Point', ({(17, 49, 17, 51): 'Px', (17, 53, 17, 55): 'Py'}, {}), '(Px, Py)', False, 'import dolfin\n'), ((18, 51, 18, 71), 'dolfin.Point', 'dolfin.Point', ({(18, 64, 18, 66): 'Px', (18, 68, 18, 70): 'Py'}, {}), '(Px, Py)', False, 'import dolfin\n'), ((23, 29, 23, 49), 'dolfin.Point', 'dolfin.Point', ({(23, 42, 23, 44): 'Px', (23, 46, 23, 48): 'Py'}, {}), '(Px, Py)', False, 'import dolfin\n')]