Wolfram Research

Function Repository Resource:

PrimitiveToPolygons (1.0.0) current version: 2.0.0 »

Source Notebook

Decompose a graphics 3D primitive into polygons when possible

Contributed by: Enrique Zeleny and Jan Mangaldan

ResourceFunction["PrimitiveToPolygons"][pr]

decomposes the graphics 3D primitive pr into polygons when possible.

Details

ResourceFunction["PrimitiveToPolygons"] works with Cuboid, Dodecahedron, Hexahedron, Icosahedron, Parallelepiped, Polyhedron, Prism, Pyramid, RegularPolygon, Simplex and Tetrahedron.

Examples

Basic Examples (7) 

Convert a Parallelepiped primitive to polygons:

In[1]:=
ResourceFunction["PrimitiveToPolygons"][
 Parallelepiped[{0, 0, 0}, {{1, 0, 0}, {1, 1, 0}, {0, 1, 1}}]]
Out[1]=

Display the polygons as tubes:

In[2]:=
Graphics3D@% /. Polygon[pts_] -> Tube[pts]
Out[2]=

Using VertexTextureCoordinates:

In[3]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/45932917-3bac-41a3-980e-db54a2d0eef4"]
Out[3]=

Number of polygons:

In[4]:=
Count[%, _Polygon, \[Infinity]]
Out[4]=

A tetrahedron with edge length 2:

In[5]:=
ResourceFunction["PrimitiveToPolygons"][Tetrahedron[2]]
Out[5]=

Using VertexColors:

In[6]:=
Graphics3D[Normal@%] /. Polygon[p_] -> Polygon[p, VertexColors -> {Red, Green, Yellow}]
Out[6]=

A pyramid:

In[7]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/9b389b51-d613-4254-82bc-ad0e754ed8e7"]
Out[7]=

A simplex in dimension 3 with texture:

In[8]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/54e6ace8-9751-40c9-823f-36620dce8428"]
Out[8]=

Using Polyhedron:

In[9]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/52ebbc3a-5f0e-4107-9408-dc53844551f0"]
Out[9]=

Scale and rotate polygons:

In[10]:=
Graphics3D[
  ResourceFunction["PrimitiveToPolygons"][Icosahedron[1]]] /. Polygon[v_] :> Rotate[Scale[Polygon[v], .75], \[Pi]/6., Mean[v]]
Out[10]=

Properties and Relations (1) 

The resource functions PerforatePolygons and OutlinePolygons internally use PrimitiveToPolygons:

In[11]:=
Graphics3D[
Polyhedron[{{0., 2^Rational[-1, 2], 0.}, {
    2^Rational[-1, 2], 0., 0.}, {
    0., -2^Rational[-1, 2], 0.}, {-2^Rational[-1, 2], 0., 0.}, {
    0., 0., 2^Rational[-1, 2]}, {0., 0., -2^Rational[-1, 2]}}, {{5, 2,
    1}, {5, 3, 2}, {5, 4, 3}, {4, 5, 1}, {2, 6, 1}, {2, 3, 6}, {4, 6, 3}, {1, 6, 4}}]] // ResourceFunction["PerforatePolygons"]
Out[11]=
In[12]:=
Graphics3D@
  ResourceFunction["PrimitiveToPolygons"][
   Prism[{{1, 0, 1}, {0, 0, 0}, {2, 0, 0}, {1, 2, 1}, {0, 2, 0}, {2, 2, 0}}]] // ResourceFunction["OutlinePolygons"]
Out[12]=

Version History

  • 2.0.0 – 19 April 2022
  • 1.0.0 – 12 April 2021

Related Resources

License Information