# Using mesh cell highlighting to override the default rendering of a region

The default rendering of a region can be rather uninformative. Consider this simple example.

DiscretizeRegion[Rectangle[{0, 0}, {2, 1}], MaxCellMeasure -> 2]

I don't care for this rendering. The edges are hard to see and the vertices are not visible at all. I would prefer it to have it look like this:

How can such a rendering be accomplished?

Note: The face mesh cells should not be white; they should be transparent (Opacity[0]).

After taking a look at Documentation Center article on DiscretizeRegion, I thought there was a easy solution. I thought it could be done like this:

DiscretizeRegion[Rectangle[{0, 0}, {2, 1}],
MaxCellMeasure -> 2,
MeshCellHighlight ->
{0 -> Directive[AbsolutePointSize[15], Red],
1 -> Directive[Thick, Black],
2 -> RGBColor[0, 0, 0, 0]}]

But as you can see it didn't work — it failed to highlight the vertex mesh cells. What does work is:

DiscretizeRegion[Rectangle[{0, 0}, {2, 1}],
MaxCellMeasure -> 2,
MeshCellShapeFunction -> {1 -> (Line[#] &), 2 -> (Null &)},
MeshCellHighlight ->
{0 -> Directive[AbsolutePointSize[10], Red],
1 -> Directive[Thick, Black]}]

I am troubled by this solution. It doesn't feel right. It doesn't say to me: "Now that you've found me, you see that I'm the obvious solution." In fact, it has more the feel of a work-around for a bug that causes 2 -> RGBColor[0,0,0,0] to be mishandled. I am also puzzled by the need for 1 -> (Line[#]&) in the righthand side of the MeshCellHighlight option, but here is what happens when it's removed.

I think I should mention that I ran into this strange behavior of DiscretizeRegion in V11.3. I would be interested to learn if it persists in V12.

• the issue does not arise in v12.0 (Wolfram Cloud). – kglr Dec 14 '19 at 9:16
• @kglr. Glad to learn that it's been fixed. I won't bother WRI support people with a bug report. – m_goldberg Dec 14 '19 at 16:51
• What happens if you use 2 -> None instead of RGBColor[0, 0, 0, 0]? (Both work in V12, MacOS.) – Michael E2 Dec 16 '19 at 14:24
• @MichaelE2. It produces the same output as 2 -> RGBColor[0, 0, 0, 0]. – m_goldberg Dec 16 '19 at 16:59

Here is an alternative way via the finite element mesh visualization.

Convert your mesh region to an ElementMesh:

mr = DiscretizeRegion[Rectangle[{0, 0}, {2, 1}]];
mesh = mr["MakeRepresentation"["ElementMesh"]]
(*ElementMesh[{{0., 2.}, {0., 1.}}, {TriangleElement["<" 2 ">"]}]*)

Or you can use the FEM mesh generation directly to generate the mesh:

Needs["NDSolveFEM"]
mesh = ToElementMesh[Rectangle[{0, 0}, {2, 1}], "MaxCellMeasure" -> 2]

Then visualize with:

Show[mesh["Wireframe"],
mesh["Wireframe"["MeshElement" -> "PointElements",
"MeshElementStyle" -> Directive[PointSize[0.02], Red]]]]

The default mesh["Wireframe"] visualization is close to what you want. We then just add the red nodes. You can find more information in the finite element mesh visualization tutorial.

For those that like more traditional interface to the wire frame can use:

Show[
ElementMeshWireframe[mesh],
ElementMeshWireframe[mesh, "MeshElement" -> "PointElements",
"MeshElementStyle" -> Directive[PointSize[0.02], Red]]]
• Doesn't this need a definition of mr; something like mr = DiscretizeRegion[Rectangle[{0, 0}, {2, 1}], MaxCellMeasure -> 2] – m_goldberg Dec 16 '19 at 17:13
• I can reproduce your final result using the definition of mr I gave in 1st comment, but I don't get the result you show from mr["MakeRepresentation"["ElementMesh"]]. – m_goldberg Dec 16 '19 at 17:36
• This is an interesting answer. I would have never have thought of applying FE methods. I'm glad you posted it, However, I have to say that it's pretty exotic. Perhaps too exotic for my tastes. – m_goldberg Dec 16 '19 at 17:39
• @m_goldberg, yes, mr = .. is needed, sorry about that. I added an alternative to generate the mesh (I think you are on V11.3, perhaps a bug) – user21 Dec 17 '19 at 8:35
• @m_goldberg, may I ask what makes this exotic for you? The string specification, perhaps, or is it something else? – user21 Dec 17 '19 at 8:37