# Usage of MeshRefinementFunction

**URL:** <https://community.wolfram.com/t/usage-of-meshrefinementfunction/8055>\
**Category:** Questions\
**Tags:** geometry, graphics-and-visualization\
**Created:** [March 5, 2016, 11:25pm UTC](https://community.wolfram.com/t/usage-of-meshrefinementfunction/8055 "2016-03-05T23:25:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [March 5, 2016, 11:25pm UTC](https://community.wolfram.com/t/usage-of-meshrefinementfunction/8055/1 "2016-03-05T23:25:40Z")

</div>

I’ve trying to use **MeshRefineFunction** , but found unusual behavior:

When I define a simple function like so:

```
ClearAll[meshRefine]
meshRefine[vertices_, area_] := area > 0.0001
mesh = Polygon[{{1, 0}, {0, 1.8}, {-1, 0}}];
DiscretizeRegion[mesh, MeshRefinementFunction -> meshRefine]

```

 ![ScreenShot2016-03-06at00.22.42.png](https://global.discourse-cdn.com/wolfram/original/1X/2b6712b3348aaa1d387e5ee029656579b051e999.png)

The output seems to be independent of the number that I put after area.

It does work if I make the following construct:

```
  ClearAll[meshRefine]
  meshRefine = Function[{vertices, area},
     Module[{mean},
      (*mean=Mean[vertices];
      mean=Norm[mean];*)
      
      area > 0.001
      ]
     ];
  mesh = Polygon[{{1, 0}, {0, 1.8}, {-1, 0}}];
  DiscretizeRegion[mesh, MeshRefinementFunction -> meshRefine]

```

 ![ScreenShot2016-03-06at00.22.58.png](https://global.discourse-cdn.com/wolfram/original/2X/6/630fc63eb8bc1ae9399d049a7f7af73eec14eccd.png)

but does not if I do the following:

```
ClearAll[meshRefine]
meshRefine = Function[{vertices, area},
   Module[{mean},
    mean=Mean[vertices];
    mean=Norm[mean];
    
    area > 0.001
    ]
   ];
mesh = Polygon[{{1, 0}, {0, 1.8}, {-1, 0}}];
DiscretizeRegion[mesh, MeshRefinementFunction -> meshRefine]

```

and note that I do not use the variable mean whatsoever!

 ![ScreenShot2016-03-06at00.23.09.png](https://global.discourse-cdn.com/wolfram/original/2X/8/840e8e6f9ffecc1937d79858cd0bff9f8fdfff43.png)

can someone explain me this behavior?

(Mathematica 10.4 on El Capitan)

---

<div class="post-metadata">

**Author:** ![iliang](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/iliang/32/11263_2.png) [@iliang](https://community.wolfram.com/u/iliang)\
**Post date:** [March 7, 2016, 7:36pm UTC](https://community.wolfram.com/t/usage-of-meshrefinementfunction/8055/2 "2016-03-07T19:36:53Z")

</div>

The basic problem is that the mesh refinement function needs to be compiled (because it is being used as a LibraryLink callback function). When something goes wrong with this automatic compilation, the discretization will proceed but the refinement function will be ignored.

I realize this is not made clear by the documentation, and in any case, there should be at least a warning message. Thank you for bringing this to our attention.

For the first example, try

```
ClearAll[meshRefine]
meshRefine[vertices_, area_] := area > 0.001
mesh = Polygon[{{1, 0}, {0, 1.8}, {-1, 0}}];
DiscretizeRegion[mesh, MeshRefinementFunction -> Function[{v, a}, Evaluate[meshRefine[v, a]]]]

```

For the last example, the compiler has trouble figuring out whether your **mean** is a rank-1 tensor or a scalar. Try

```
ClearAll[meshRefine]
meshRefine = 
  Function[{vertices, area}, 
   Module[{meanv, meann}, meanv = Mean[vertices];
    meann = Norm[meanv];
    area > 0.001]];
mesh = Polygon[{{1, 0}, {0, 1.8}, {-1, 0}}];
DiscretizeRegion[mesh, MeshRefinementFunction -> meshRefine]

```

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [March 8, 2016, 7:50pm UTC](https://community.wolfram.com/t/usage-of-meshrefinementfunction/8055/3 "2016-03-08T19:50:33Z")

</div>

I guess it would’ve been something related to compile, but the function **does** get evaluated at many points, see example below, so that made me second-guess… Is there a way around it? (slow is OK) I would like to call a nearestfunction generated by nearest (or a regiondistancefunction) (basically refine it more around some points). Basically writing a nearest function myself is possible of course…

```
ClearAll[meshRefine]
\[CurlyPhi] = 0;
p = {};
Dynamic[\[CurlyPhi]]
meshRefine[vertices_, area_] := (\[CurlyPhi]++; 
  AppendTo[p, Mean[vertices]]; area > 0.00001)
mesh = Polygon[{{1, 0}, {0, 2.2}, {-1, 0}}];
DiscretizeRegion[mesh, MeshRefinementFunction -> meshRefine]
p // Point // Graphics

```

Yeah, indeed a warning would’ve been nice 😉 well, I guess I have to refine it manually after it somehow… that’s unfortunate…

Thank Ilian for the feedback!
