5
$\begingroup$

Using the RegionNearest[] function, it is possible to calculate the point, $p_{mesh}$, on a mesh region that is nearest to some input point, $p_{input}$. My question is: how to calculate the (label of the) triangle to which the point $p_{mesh}$ is associated?

For example using trial and error I can work out that triangle number 20 contains the point on the mesh which is nearest to my test point.

a = BoundaryDiscretizeRegion[Ball[{0, 0, 0}, 1], 
   MaxCellMeasure -> {"Length" -> 1}, PrecisionGoal -> 1];
testpoint = {1.0, 0.2, 0.7};
pt = RegionNearest[a, testpoint]
Show[{HighlightMesh[a, Style[{2, 20}, Orange, Opacity[0.5]]], 
  Graphics3D[{Red, PointSize[Large], Point[testpoint]}], 
  Graphics3D[{Blue, PointSize[Large], Point[pt]}]}]

Exampleimage of mesh and nearest points

Is there any built in functionality for this in Mathematica? I can imagine a round about way of doing this by finding in the list of vertices of the mesh, which three points are closest to $p_{mesh}$ and then working out which triangle they are associated to. This seems to be too convoluted somehow. Any ideas would be greatly appreciated.

$\endgroup$
6
$\begingroup$

Naive solution:

Catch[If[RegionMember[MeshPrimitives[a, #], pt], Throw[#]] & /@ 
  MeshCellIndex[a, 2]]

{2, 20}

$\endgroup$
  • 1
    $\begingroup$ This looks good, many thanks! I will wait before accepting to see if there are any other options like undocumented features in the RegionNearest function. $\endgroup$ – Dunlop Feb 14 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.