# Information concerning undocumented function "RegionMeshMeshNearestCellIndex[r ]"

I'm looking for further information of the undocumented function RegionMeshMeshNearestCellIndex[r] (Thanks to Henrik Schumacher!)

The function considers a meshregion r and detects the nearest cell to a given point. Trying to understand I look at a very simple triangle mesh in space:

pi={{0., 0., 0.303}, {1., -0.5, 0.09}, {0.7, 0.8, -0.233}, {0.,1., -0.584}, {-0.8, -0.7, -0.734}}
Δi = {{1, 2, 3}, {1, 3, 4}, {1, 4, 5}, {1, 5, 2}};
r = MeshRegion[pi , Triangle[Δi]];
HighlightMesh[r, {Labeled[0, "Index"], Labeled[2, "Index"]}]


Now I want to evaluate the nearest cells of point 1

 RegionMeshMeshNearestCellIndex[r , pi[[1]] ]
(*{2, 1}*)


Expecting four possible cells as nearest neighbors MMA returns element #1.

My question: How does MMA evaluate the priority of the possible cells? Thanks!

• Can you read the DownValues? That’s the place to start if they’re available. If not we’re at the mercy of WRI developers or wherever one can find the function used in the source code. Jan 1, 2019 at 22:37
• @b3m2a1 Nope, Needs["GeneralUtilities"]; PrintDefinitions[RegionMeshMeshNearestCellIndex] returns RegionMeshMeshNearestCellIndex[___] := <<kernel function>>;... Jan 1, 2019 at 22:40

If I had to make a guess, I'd say Mathematica breaks ties by choosing the cell with the smallest index:

Δi = {{1, 3, 4}, {1, 2, 3}, {1, 4, 5}, {1, 5, 2}};
Table[
r = MeshRegion[pi, Triangle[Δi[[perm]]]];
RegionMeshMeshNearestCellIndex[r, pi[[1]]],
{perm, PermutationList /@ Permutations[Range[4]]}
]


{{2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}, {2, 1}}

By the way, I just found out that you can also obtain the $$n$$ nearest cells as follows:

r = MeshRegion[pi, Triangle[Δi]];
RegionMeshMeshNearestCellIndex[r, pi[[1]], 10]


{{2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}}

Apparently, superfluous cells obtain the index 0...

Oh, an apparently, there is also an operator version of the RegionMeshMeshNearestCellIndex, similarly as for Nearest:

cellfun = RegionMeshMeshNearestCellIndex[r];


cellfun[pi[[1]]]
`

{2, 2}

• @ Henrik Thanks. Without the superfluous cells the ordering of the returned list seems to be arbitrary. Jan 1, 2019 at 22:54