3

Mathematica 13.1 on Windows 10.

Can someone explain why the following does not match:

MatchQ[BoundaryMeshRegion[Cube[]], BoundaryMeshRegion[__]]

(* False *)

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Daniel Huber
  • 51,463
  • 1
  • 23
  • 57
  • 5
    mesh regions are atomic, which makes them opaque to the pattern matcher. If you use _BoundaryMeshRegion as a pattern it will work. – Jason B. Sep 08 '22 at 13:28
  • does this work for you?If you prevent evaluation of the left side, then it does match MatchQ[Unevaluated@BoundaryMeshRegion[Cube[]], BoundaryMeshRegion[___]] gives True – Nasser Sep 08 '22 at 13:32
  • @Jason B Thanks a lot for the explanation. Is there a way to get the at the innards of _BoundaryMeshRegion, because "Part" does not work because it is atomic. – Daniel Huber Sep 08 '22 at 13:34
  • 1
    @Nasser - when would you want to do that? Usually the issue is that you have an evaluated object, in this case a mesh region, and you want to write a pattern to match it. – Jason B. Sep 08 '22 at 13:34
  • @DanielHuber - all you have are the accessor functions like MeshCoordinates, MeshCells, MeshPrimitives. There are hacky methods to get at the InputForm but they are rarely the right way to do things. – Jason B. Sep 08 '22 at 13:36
  • @Nasser I want to get at the innards of BoundaryMeshRegion – Daniel Huber Sep 08 '22 at 13:36
  • @Jason B "MeshCoordinates" works fine. I want to change the coordinates. However, how to I insert the changed coordiantes back into BoundaryMeshRegion[...] – Daniel Huber Sep 08 '22 at 13:40
  • 1
    That is a good question - 'what is the right way to modify an existing mesh region?'. Here are the methods for accessing the innards of atoms. Carl's nucleus function is nice – Jason B. Sep 08 '22 at 13:46
  • 1
    @JasonB. sometimes Unevaluated is needed in MathQ. Here is an example Mathematica graphics this is from https://www.youtube.com/watch?v=H-rnezxOCA8 at time 50:57 so Tried on this example and it worked. – Nasser Sep 08 '22 at 13:46

1 Answers1

2

As was pointed out in the comments, the reason of the no-match is, that BoundaryMeshRegion is atomic.

My ultimate goal is to change the innards of a BoundaryMeshRegion or any other atomic object. Toward this aim, the function Nucleus from Carl Woll is a big step forward.

I give here a simple example how to change a BoundaryMeshRegion of a Rectangle:

First the function Nucleus from Carl Woll:

Nucleus[input_, head_ : Automatic] := 
 With[{atoms = 
    Replace[head, {Automatic :> 
       If[AtomQ[input], {Head[input]}, 
        Message[Nucleus::atom]; $Failed], h_Symbol :> {h}, 
      h : {__Symbol} :> 
       h, _ :> (Message[Nucleus::syms, head, 2]; $Failed)}]}, (If[! 
       MemberQ[Links[], $AtomLink] || LinkReadyQ[$AtomLink], 
     Quiet@LinkClose[$AtomLink];
     $AtomLink = LinkCreate[LinkMode -> Loopback]];
    LinkWrite[$AtomLink, input];
    inactiveBlock[atoms, LinkRead[$AtomLink]]) /; atoms =!= $Failed]

SetAttributes[inactiveBlock, HoldAll] inactiveBlock[h_List, body_] := Block @@ Join[Apply[Set, Hold@Evaluate@Thread[{h, Inactive /@ h}], {2}], Hold[body]]

Nucleus::syms = "Argument 1 at position 2 is expected to be a symbol or a list
of symbols"; Nucleus::atom = "Unable to determine atomic symbol";

The BoundaryMeshRegion of some rectangle is:

bmr=BoundaryMeshRegion[Rectangle[{0, 0}]]

enter image description here

and the FullForm of the atom:

nuc= Nucleus[bmr];
FullForm[nuc]

enter image description here

We may now change the coordinate of the first vertex. Note that the real number are written with a tick:

changed= nuc /. {0.`, 0.`} -> {0.5, 0};

Finally we may re-activate the changed rectangle:

Activate[changed]

enter image description here

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Daniel Huber
  • 51,463
  • 1
  • 23
  • 57