Function Repository Resource:

RoundedCuboid

Source Notebook

Represent a cuboid with rounded edges

Contributed by: Jaroslav Kysela

ResourceFunction["RoundedCuboid"][pmin]

represents a unit cube with rounded edges and with its lower corner at pmin.

ResourceFunction["RoundedCuboid"][pmin,pmax]

represents an axis-aligned cuboid with rounded edges and with lower corner pmin and upper corner pmax.

Details and Options

ResourceFunction["RoundedCuboid"] can be used in Graphics3D.
Graphics rendering is affected by directives such as FaceForm, Opacity, and color.
ResourceFunction["RoundedCuboid"][] is equivalent to ResourceFunction["RoundedCuboid"][{0,0,0}].
The following options can be given:
RoundingRadius{0.1,0.1,0.1}the radii of rounded edges
The RoundingRadius setting can have the following forms:
ruse r for radii in all three directions
{rx,ry,rz}separate setting for each radius

Examples

Basic Examples (4) 

A unit rounded cuboid:

In[1]:=
Graphics3D[ResourceFunction["RoundedCuboid"][]]
Out[1]=

Rounded cuboids with different sizes:

In[2]:=
Graphics3D[{Yellow, ResourceFunction["RoundedCuboid"][{0, 0, 0}, {1, 3, 1}], Blue, ResourceFunction["RoundedCuboid"][{2, 1, 1}, {4, 2, 3}]}]
Out[2]=

Differently styled rounded cuboids:

In[3]:=
{Graphics3D[{Pink, ResourceFunction["RoundedCuboid"][]}], Graphics3D[{Opacity[.5], ResourceFunction["RoundedCuboid"][]}], Graphics3D[{Specularity[5], ResourceFunction["RoundedCuboid"][]}]}
Out[3]=

Rotated rounded cuboid:

In[4]:=
Graphics3D[
 Rotate[ResourceFunction["RoundedCuboid"][], \[Pi]/3, {1, -1, 1}]]
Out[4]=

Scope (8) 

Specification (3) 

A rounded unit cube:

In[5]:=
Graphics3D[ResourceFunction["RoundedCuboid"][{0, 0, 0}]]
Out[5]=

A rounded cuboid parallel to each axis:

In[6]:=
Graphics3D[ResourceFunction["RoundedCuboid"][{0, 0, 0}, {3, 2, 1}]]
Out[6]=

Short form for a rounded unit cube cornered at the origin:

In[7]:=
Graphics3D[ResourceFunction["RoundedCuboid"][], Axes -> True]
Out[7]=

Styling (5) 

Color directives specify the face colors of cuboids:

In[8]:=
Table[Graphics3D[{c, ResourceFunction["RoundedCuboid"][]}], {c, {Red, Green, Blue, Yellow}}]
Out[8]=

FaceForm can be used to specify the styles of the faces:

In[9]:=
Graphics3D[{FaceForm[Pink], ResourceFunction["RoundedCuboid"][]}]
Out[9]=

Different properties can be specified for the front and back of faces using FaceForm:

In[10]:=
Graphics3D[{FaceForm[Yellow, Blue], ResourceFunction["RoundedCuboid"][{.1, -.5, .1}, {.9, .8, .9}]}, PlotRange -> {{-1/4, 5/4}, {1/4, 5/4}, {-1/4, 5/4}}]
Out[10]=

Opacity specifies the face opacity:

In[11]:=
Table[Graphics3D[{Opacity[o], ResourceFunction["RoundedCuboid"][]}, Boxed -> False], {o, {0.1, 0.5, 0.9}}]
Out[11]=

Decompose the cuboid into its components and apply different styling to each:

In[12]:=
{faces, edges, vertices} = Rest@ResourceFunction["RoundedCuboid"][];
In[13]:=
Graphics3D[{EdgeForm[], {Opacity[.7, Orange], faces}, {Opacity[.7, Purple], edges}, Yellow, vertices}]
Out[13]=

Options (3) 

RoundingRadius (3) 

Adjust how rounded the edges are:

In[14]:=
Graphics3D[
   ResourceFunction["RoundedCuboid"][{0, 0, 0}, 2.5 {1, 1, 1}, RoundingRadius -> #], Boxed -> False] & /@ {.1, .5, 1}
Out[14]=

Adjust the edges in each direction independently:

In[15]:=
Graphics3D[
 ResourceFunction["RoundedCuboid"][{0, 0, 0}, 3 {1, 1, 1}, RoundingRadius -> {0.2, 0.3, 0.5}], Boxed -> False]
Out[15]=

Leave some edges sharp:

In[16]:=
Graphics3D[
 ResourceFunction["RoundedCuboid"][{0, 0, 0}, 3 {1, 1, 1}, RoundingRadius -> {0.5, 0, 0.5}], Boxed -> False]
Out[16]=

Applications (1) 

A simple 3D bar chart:

In[17]:=
data = RandomReal[{1, 10}, {5, 4}];
In[18]:=
Graphics3D[
 MapIndexed[{Hue[(Last[#2] - 1)/4], ResourceFunction["RoundedCuboid"][Append[{1, 2} #2 - {.5, .5}, 0],
      Append[{1, 2} #2 + {.5, .5}, #1], RoundingRadius -> .3]} &, data, {2}], Axes -> {False, False, True}, Lighting -> "Neutral"]
Out[18]=

Properties and Relations (7) 

Use Rotate to get all possible rounded cuboids in Graphics3D:

In[19]:=
Graphics3D[
 Rotate[ResourceFunction[
   "RoundedCuboid"][{0, 0, 0}, {1, 2, 1}], -30 Degree, {0, 0, 1}], Axes -> True]
Out[19]=

Rectangle with a non-zero value for RoundingRadius is a 2D version of RoundedCuboid:

In[20]:=
Graphics[Rectangle[{0, 0}, RoundingRadius -> .2]]
Out[20]=

Cuboid is a special case of RoundedCuboid:

In[21]:=
{Graphics3D[{EdgeForm[], Cuboid[]}], Graphics3D[ResourceFunction["RoundedCuboid"][RoundingRadius -> 0]]}
Out[21]=

Cylinder can be viewed as a special case of RoundedCuboid:

In[22]:=
{Graphics3D[{EdgeForm[], Cylinder[]}], Graphics3D[
  ResourceFunction["RoundedCuboid"][RoundingRadius -> {.5, .5, 0}]]}
Out[22]=

Sphere can be viewed as a special case of RoundedCuboid:

In[23]:=
{Graphics3D[{EdgeForm[], Sphere[]}], Graphics3D[
  ResourceFunction["RoundedCuboid"][RoundingRadius -> {.5, .5, .5}]]}
Out[23]=

BarChart3D or RectangleChart3D can be used to create rounded cuboids:

In[24]:=
BarChart3D[{1}, ChartElementFunction -> ChartElementDataFunction["DoubleProfileCube", "HorizontalProfile" -> 1.7, "VerticalProfile" -> 6]]
Out[24]=

Get the graphical primitive for BarChart3D:

In[25]:=
roundCub[pt1_, pt2_, {hprof_, vprof_}] := ChartElementDataFunction["DoubleProfileCube", "HorizontalProfile" -> hprof, "VerticalProfile" -> vprof][
  Transpose[{pt1, pt2}]]
In[26]:=
Graphics3D[roundCub[{0, 0, 0}, {1, 1, 1}, {5, 5}], Axes -> True]
Out[26]=

Compare with RoundedCuboid:

In[27]:=
Graphics3D[{ResourceFunction["RoundedCuboid"][{0, 0, 0}, {1, 1, 1}, RoundingRadius -> {0.25, 0.25, 0.1}]}, Axes -> True]
Out[27]=

The two cuboids have a very different internal structure:

In[28]:=
{
 Graphics3D[{EdgeForm[Thick], roundCub[{0, 0, 0}, {1, 1, 1}, {5, 5}]},
   Axes -> True, ImageSize -> Medium],
 Graphics3D[
  ResourceFunction["RoundedCuboid"][{0, 0, 0}, {1, 1, 1}, RoundingRadius -> {0.25, 0.25, 0.1}] /. x_EdgeForm :> EdgeForm[Thick], Axes -> True, ImageSize -> Medium]
 }
Out[28]=

Possible Issues (2) 

When the width of the cuboid is zero in at least one dimension, no cuboid is created:

In[29]:=
Graphics3D[ResourceFunction["RoundedCuboid"][{0, 0, 0}, {1, 1, 0}]]
Out[29]=

When any length of the cuboid is less than twice the corresponding setting for RoundingRadius, a warning is issued and artifacts appear:

In[30]:=
Graphics3D[
 ResourceFunction["RoundedCuboid"][{0, 0, 0}, {1, 1, 2}, RoundingRadius -> {.1, .1, 1.7}]]
Out[30]=

Neat Examples (2) 

Random rounded cuboid collections:

In[31]:=
Graphics3D[
 Table[{EdgeForm[Opacity[.3]], Hue[RandomReal[]], Rotate[#, RandomReal[{0, 2 \[Pi]}], RandomReal[{0, 1}, {3}]] &@
    ResourceFunction["RoundedCuboid"][RandomReal[4, 3]]}, {40}], PlotRange -> {0, 7}]
Out[31]=

Adjust interactively the rounding radii:

In[32]:=
Manipulate[
 Graphics3D[{ResourceFunction["RoundedCuboid"][{0, 0, 0}, 2 {1, 1, 1},
      RoundingRadius -> {rx, ry, rz}] /. x_EdgeForm :> EdgeForm[Thick], Opacity[.5], Cuboid[{0, 0, 0} - 0.001, 2 {1, 1, 1} + 0.001]}, Boxed -> False], {{rx, 0.7}, 0, 1}, {{ry, .2}, 0, 1}, {{rz, .5}, 0, 1}]
Out[32]=

Publisher

Jaroslav Kysela

Version History

  • 1.0.0 – 03 January 2022

Related Resources

License Information