Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to draw a cuboid with rounded corners,like this:

enter image description here

RoundingRadius only works with Rectangle or Framed.I have no idea how to draw a cuboid with rounded corners. What are your ideas? Thanks.

share|improve this question
    
I think should be possible to do this using BSplineSurface and it would be the "ultimate" way, creating a precise, single-piece shape, but frankly I'm too lazy to figure it out as it's far from trivial. –  Mr.Wizard Jun 8 at 10:08

3 Answers 3

up vote 5 down vote accepted

Two more options. These allow direct control of the box dimensions and the rounding radius.


Using ContourPlot3D:

signedDistance[p_, p0_, p1_] := Piecewise[
  {{-Min[p - p0, p1 - p], And @@ Thread[p0 <= p <= p1]}, 
   {EuclideanDistance[p, MapThread[Min[Max[#1, #2], #3] &, {p, p0, p1}]], True}}]
roundedCuboidPlot[p0 : {x0_, y0_, z0_}, p1 : {x1_, y1_, z1_}, r_, opts : OptionsPattern[]] := 
 ContourPlot3D[signedDistance[{x, y, z}, p0 + r, p1 - r] == r, 
  {x, x0 - r, x1 + r}, {y, y0 - r, y1 + r}, {z, z0 - r, z1 + r}, opts]

roundedCuboidPlot[{0, 0, 0}, {1, 2, 3}, 1/4, BoxRatios -> Automatic, Mesh -> None]

enter image description here


Using Graphics3D primitives:

roundedCuboid[p0 : {x0_, y0_, z0_}, p1 : {x1_, y1_, z1_}, r_] := 
 {EdgeForm[None], 
  Cuboid[p0 + {0, r, r}, p1 - {0, r, r}], 
  Cuboid[p0 + {r, 0, r}, p1 - {r, 0, r}], 
  Cuboid[p0 + {r, r, 0}, p1 - {r, r, 0}], 
  Table[Cylinder[{{x0 + r, y, z}, {x1 - r, y, z}}, r], 
   {y, {y0 + r, y1 - r}}, {z, {z0 + r, z1 - r}}], 
  Table[Cylinder[{{x, y0 + r, z}, {x, y1 - r, z}}, r], 
   {x, {x0 + r, x1 - r}}, {z, {z0 + r, z1 - r}}], 
  Table[Cylinder[{{x, y, z0 + r}, {x, y, z1 - r}}, r], 
   {x, {x0 + r, x1 - r}}, {y, {y0 + r, y1 - r}}], 
  Table[Sphere[{x, y, z}, r], 
   {x, {x0 + r, x1 - r}}, {y, {y0 + r, y1 - r}}, {z, {z0 + r, z1 - r}}]}

Graphics3D[{roundedCuboid[{0, 0, 0}, {1, 2, 3}, 1/4]}]

enter image description here

share|improve this answer
    
The second way is great!Since I need to add one texture on cuboid, i just replace a Cuboid to Polygon,then I can use Texture.Thx. –  Chenminqi Jun 8 at 7:35
    
Nice, I was to lazyto write this ;) p.s. you can skip Spheres and replace Cylinders with Tubes. –  Kuba Jun 8 at 8:24
ClearAll[roundedCuboidF]
roundedCuboidF[hprof_: 10, vprof_: 10, taper_: 1][box_] := 
    ChartElementDataFunction["DoubleProfileCube", "HorizontalProfile" -> hprof, 
                            "VerticalProfile" -> vprof, "TaperRatio" -> taper][box]

Graphics3D[roundedCuboidF[][{{0, 1}, {0, 1}, {0, 1}}], Boxed -> False]

enter image description here

or

ContourPlot3D[Norm[{x, y, z}, 4], {x, -1, 1}, {y, -1, 1}, {z, -1, 1},
  Mesh -> None, Boxed -> False, Axes -> False, Lighting -> "Neutral",
  ContourStyle ->  Directive[Orange, Opacity[0.8], Specularity[White, 30]]]

enter image description here

share|improve this answer
    
not for keypad use: roundedCuboidF[1, 1, 0.01]. Nice thing to play with and very instructive :) –  eldo Jun 7 at 15:41
    
Thx!But I can not find ChartElementDataFunction in Reference Centre, so How should I learn this function? –  Chenminqi Jun 7 at 15:42
1  
@Chenminqi, try ChartElementFunction and examples there and Chart Element Schemes in the Palettes menu. –  kguler Jun 7 at 15:44
    
@eldo, thank you! -- looks nicer with Directive[Red, Specularity[White, 30]] and `Lighting->"Neutral" :) –  kguler Jun 7 at 15:47
f = PolyhedronData["Cube", "RegionFunction"][x, y, z];

r = 2; u = 0.6;

RegionPlot3D[f, {x, -r, r}, {y, -r, r}, {z, -r, r}, Mesh -> False, 
 PlotPoints -> 55, PlotRange -> {{-u, u}, {-u, u}, {-u, u}}]

enter image description here

Weakness of this approach: You have to find the right number for PlotPoints (here 55) by trial and error.

share|improve this answer
2  
f is a cube with straight edges covering {{-0.5,0.5},{-0.5,0.5},{-0.5,0.5}}. This volume is completely inside {{-2,2},...} and {{-0.6,0.6},...} so how does this even work? Is it a side effect of RegionPlot3D not having high enough resolution? –  Pickett Jun 7 at 17:36
1  
@picket. Yes, it is (see last sentence of my amswer). –  eldo Jun 7 at 18:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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