Nearest point on boundary to exterior point

I don’t know of any built-in functionality for this. I did think of a somewhat-involved workaround:

  • Define a CG1 scalar space, V = FunctionSpace(mesh,"CG",1).
  • Find the index of the closest cell to x in mesh using the compute_first_entity_collision method of mesh.bounding_box_tree().
  • Use some DoF maps for V and V.element().evaluate_basis to evaluate all shape functions on that cell. The shape function that is negative is the one whose node is the vertex “opposite” x.
  • Get that shape function’s gradient with V.element().evaluate_basis_derivatives.
  • Given x, the shape function evaluation at x, and the shape function gradient, you can do some algebra to calculate the closest point to x on the cell (where the shape function goes to zero along a line through x in the direction of the gradient).

For an example of how to use the basis function evaluation functions, you might take a look at this discussion.

1 Like