2
$\begingroup$

I want to extract data from a plot, more exactly BoundaryDiscretizeRegion[...]. Following Plot, extract data to a file, I use the following codes to make it:

(* Preparation *)
ball = BoundaryDiscretizeRegion@Region[Ball[{0, 0, 0}, 1]];

(* I want to extract the data of ball within Polygon[] *)
Cases[ball, Polygon[data_] :> data, Infinity]
(* ==> {} *)

I wonder why my codes do not work, and how to extract data encapsulated in Polygon[].

Thanks.

$\endgroup$
3
  • 4
    $\begingroup$ Look up MeshCoordinates and MeshCells. $\endgroup$ Commented Oct 14, 2017 at 13:37
  • 1
    $\begingroup$ @J.M. Thanks for your knowledge and warm heart. $\endgroup$
    – PureLine
    Commented Oct 14, 2017 at 14:02
  • 1
    $\begingroup$ Just to clarify: this is not a plot, even though it looks like one. It is a BoundaryMeshRegion expression. Take a look at the InputForm of this, as well as Plot's output (which is a Graphics). $\endgroup$
    – Szabolcs
    Commented Oct 14, 2017 at 14:56

2 Answers 2

4
$\begingroup$

There are plenty of data you can extract from your boundary discretized region, for example :

ball = BoundaryDiscretizeRegion@Region[Ball[{0, 0, 0}, 1]];

MeshPrimitives[ball,0] //Take[#,3]&
MeshPrimitives[ball,1] //Take[#,3]&
MeshPrimitives[ball,2] //Take[#,3]&  

{Point[{-0.607062, 0., 0.794654}], Point[{0.894427, 0., 0.447214}],
Point[{0., 0., 1.}]}

{Line[{{0.276393, -0.850651, 0.447214}, {0.204995, -0.829094, 0.520174}}], Line[{{0.204995, -0.829094, 0.520174}, {0.185096, -0.890336, 0.415981}}], Line[{{0.185096, -0.890336, 0.415981}, {0.276393, -0.850651, 0.447214}}]}

{Polygon[{{0.276393, -0.850651, 0.447214}, {0.204995, -0.829094, 0.520174}, {0.185096, -0.890336, 0.415981}}], Polygon[{{0.185096, -0.890336, 0.415981}, {0.204995, -0.829094, 0.520174}, {0.108274, -0.865931, 0.488303}}], Polygon[{{0.185096, -0.890336, 0.415981}, {0.108274, -0.865931, 0.488303}, {0.0871575, -0.92155, 0.378351}}]}

MeshCoordinates[ball] //Take[#,3]&
MeshCells[ball, 0] //Take[#,3]&
MeshCells[ball, 1] //Take[#,3]&
MeshCells[ball, 2] //Take[#,3]&  

{{-0.607062, 0., 0.794654}, {0.894427, 0., 0.447214}, {0., 0., 1.}}

{Point[1], Point[2], Point[3]}

{Line[{7, 38}], Line[{38, 37}], Line[{37, 7}]}

{Polygon[{7, 38, 37}], Polygon[{37, 38, 483}], Polygon[{37, 483, 36}]}

MeshCellCount[ball, 0] 
MeshCellCount[ball, 1] 
MeshCellCount[ball, 2]   

1082

3240

2160

MeshCellIndex[ball, 0] //Take[#,3]&
MeshCellIndex[ball,1] //Take[#,3]&
MeshCellIndex[ball,2] //Take[#,3]&  

{{0, 1}, {0, 2}, {0, 3}}

{{1, 1}, {1, 2}, {1, 3}}

{{2, 1}, {2, 2}, {2, 3}}

PropertyValue[{ball,0},MeshCellMeasure]//Take[#,3]&
PropertyValue[{ball,1},MeshCellMeasure]//Take[#,3]&
PropertyValue[{ball,2},MeshCellMeasure]//Take[#,3]&  

{1., 1., 1.}

{0.104334, 0.122485, 0.104334}

{0.00517309, 0.00546982, 0.00557218}

0,1,2 in the Mesh... expressions correspond to the dimensions of the elements you are interested in. There aren't any dimension 3 elements in your example.

$\endgroup$
1
$\begingroup$

Thanks for @J. M. advice. It's the very answer of my question.

(* extract the indexes of points, which construct the cells of mesh *)
indexOfCells = Cases[MeshCells[reg, 2], Polygon[coord_] :> coord]
(* {{56795, 56753, 56794}, ... {56793, 56752, 56751}} *)

With these data, I can carry out reverse engineering of msome organic texture for the mechanism of robots.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.