Function Repository Resource:

PrimitiveToPolygons

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.

Examples

Basic Examples (2) 

A Parallelepiped object is converted to polygons:

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

Display as tubes:

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

Scope (10) 

Using VertexTextureCoordinates:

In[3]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/f4020b13-642a-40c3-99c9-ba90f2f322e4"]
Out[3]=

Number of polygons:

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

A tetrahedron with edge length 2:

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

Using VertexColors:

In[6]:=
Graphics3D[Normal@tet] /. 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/da784fff-cf99-46c1-8731-9a3d52daed51"]
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/775d9262-7c28-4d40-9b7b-184f95f211ed"]
Out[8]=

Other graphics primitives:

In[9]:=
Graphics3D[
   ResourceFunction["PrimitiveToPolygons"][#]] & /@ {Cylinder[], Sphere[], Ball[], SphericalShell[], Cone[], Ellipsoid[{0, 0, 0}, {4, 3, 2}], Tube[{{0, 0, 0}, {1, 1, 1}}, .2], CapsuleShape[]}
Out[9]=

Using Polyhedron:

In[10]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/56a8d924-f145-4c6e-9719-4db15e1f306e"]
Out[10]=

Using Polyhedron with VertexTextureCoordinates:

In[11]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/9162eaaf-405c-487e-a28f-8d9fb41368a3"]
Out[11]=

Scale and rotate polygons:

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

A BSplineSurface:

In[13]:=
Graphics3D[
  BSplineSurface[
   Table[{i, j, RandomReal[{-1, 1}]}, {i, 5}, {j, 5}]]] // ResourceFunction["PrimitiveToPolygons"]
Out[13]=

A Raster3D with gray levels:

In[14]:=
data = {{{{0, 0}, {0, .2}, {0, .4}, {0, .6}, {0, .8}, {0, 1.}}}};
Graphics3D[Raster3D[data]] // ResourceFunction["PrimitiveToPolygons"]
Out[14]=

A Raster3D with RGB colors:

In[15]:=
Graphics3D[
 ResourceFunction["PrimitiveToPolygons"][
  Raster3D[RandomReal[1, {5, 5, 5, 3}]]], Axes -> True]
Out[15]=

Properties and Relations (1) 

The resource functions PerforatePolygons and OutlinePolygons internally use PrimitiveToPolygons:

In[16]:=
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[16]=
In[17]:=
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[17]=

Publisher

Enrique Zeleny

Version History

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

Related Resources

License Information