2
$\begingroup$

I'm looking for further information of the undocumented function Region`Mesh`MeshNearestCellIndex[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"]}]

enter image description here

Now I want to evaluate the nearest cells of point 1

 Region`Mesh`MeshNearestCellIndex[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!

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – b3m2a1 Jan 1 '19 at 22:37
  • $\begingroup$ @b3m2a1 Nope, Needs["GeneralUtilities`"]; PrintDefinitions[Region`Mesh`MeshNearestCellIndex] returns Region`Mesh`MeshNearestCellIndex[___] := <<kernel function>>;... $\endgroup$ – Henrik Schumacher Jan 1 '19 at 22:40
3
$\begingroup$

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]]]];
 Region`Mesh`MeshNearestCellIndex[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]];
Region`Mesh`MeshNearestCellIndex[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 Region`Mesh`MeshNearestCellIndex, similarly as for Nearest:

cellfun = Region`Mesh`MeshNearestCellIndex[r];

enter image description here

cellfun[pi[[1]]]

{2, 2}

| improve this answer | |
$\endgroup$
  • $\begingroup$ @ Henrik Thanks. Without the superfluous cells the ordering of the returned list seems to be arbitrary. $\endgroup$ – Ulrich Neumann Jan 1 '19 at 22:54

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.