7
$\begingroup$

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

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

default

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:

desired

How can such a rendering be accomplished?

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

$\endgroup$
5
$\begingroup$

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]}]

no_vertex

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]}]

desired

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.

no_edges

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.

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

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["NDSolve`FEM`"]
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]]]]

enter image description here

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]]]
$\endgroup$
  • $\begingroup$ Doesn't this need a definition of mr; something like mr = DiscretizeRegion[Rectangle[{0, 0}, {2, 1}], MaxCellMeasure -> 2] $\endgroup$ – m_goldberg Dec 16 '19 at 17:13
  • $\begingroup$ 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"]]. $\endgroup$ – m_goldberg Dec 16 '19 at 17:36
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Dec 16 '19 at 17:39
  • $\begingroup$ @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) $\endgroup$ – user21 Dec 17 '19 at 8:35
  • $\begingroup$ @m_goldberg, may I ask what makes this exotic for you? The string specification, perhaps, or is it something else? $\endgroup$ – user21 Dec 17 '19 at 8:37

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.