4
$\begingroup$

Mathematica 12 has the nice feature that MeshRegion and BoundaryMeshRegion objects render nicely when included as a primitive in a Graphics object. For example, in M12:

Graphics @ DiscretizeGraphics[{Rectangle[], Line[{{1,1},{2,0}}]}]

enter image description here

In M11.3 the above returns a pink graphic. Is it possible to replicate this feature in earlier versions of Mathematica?

$\endgroup$
3
$\begingroup$

Since MeshRegion and BoundaryMeshRegion objects already have a display form, it is fairly simple to construct a corresponding set of Graphics box objects from them. The one tricky part is to strip out the usual blue color that is used to render region objects, since we want the new primitive to use whatever colors that have been set by graphics directives. After a bit of spelunking, it turns out that blocking a particular internal symbol will do this.

Then, we can use Simon Wood's answer about how to create new graphics primitives to have them render inside of Graphics. Here is the code:

Typeset`MakeBoxes[m_MeshRegion, form_, Graphics] := Block[{Region`MeshInformation = {}&},
    First @ ToBoxes @ m /. "MeshGraphics"->"MeshRegion"
]
Typeset`MakeBoxes[m_BoundaryMeshRegion, form_, Graphics] := Block[{Region`MeshInformation = {}&},
    First @ ToBoxes @ m /. "MeshGraphics"->"BoundaryMeshRegion"
]

Blocking Region`MeshInformation will prevent the blue region color from being inserted into the graphics boxes corresponding to the mesh regions. Here is an example (in M11.3):

Graphics[{BoundaryDiscretizeRegion[Rectangle[]]}, ImageSize->200]

enter image description here

If the MeshRegion comes with custom styling, it will be retained:

Graphics[{DiscretizeRegion[Disk[{0,0}, 1], MeshCellStyle->{2->Red}]}, ImageSize->200]

enter image description here

Otherwise, the primitive will use the directives that have been specified:

Graphics[{Green, EdgeForm[Blue], BoundaryDiscretizeRegion[Rectangle[]]}, ImageSize->200]

enter image description here

$\endgroup$

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.