This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

method compute_closest_point()

0 votes
import numpy as np
import dolfin

p = dolfin.Point( np.random.rand(3) )

mesh = dolfin.UnitCubeMesh(10,10,10)

tree = mesh.bounding_box_tree()

p_index = tree.compute_closest_point( p ) # throws exception

This reference says that the method compute_closest_point should return an index to the closest point in the mesh. But on my machine using dolfin 1.6.0 it throws an exception.

However, how to compute the closest point in the mesh? Of course, you can do this manually. But is there a method implemented?

asked Apr 5, 2016 by sg FEniCS Novice (260 points)

1 Answer

+1 vote
 
Best answer

This should work (I'm not sure if there's a better way though); you need to build the tree using a point cloud if you want to use compute_closest_point:

import numpy as np
import dolfin
p = dolfin.Point( np.random.rand(3) )
mesh = dolfin.UnitCubeMesh(10,10,10)
tree = mesh.bounding_box_tree()
point_cloud = [dolfin.Point(point) for point in mesh.coordinates()]
tree.build(point_cloud, 3)
p_i, distance = tree.compute_closest_point(p)
print "p:", p.str()
print "closest point:", point_cloud[p_i].str()

As an alternative, you can probably use the method compute_first_entity_collision to get the index of the closest cell in the mesh, and use that cell to compute the point.

answered Apr 5, 2016 by FF FEniCS User (4,630 points)
selected Apr 19, 2016 by sg
...