Makie - How does one plot a cube w/ rounded edges & corners?


The following Julia program uses Makie to create the image shown below, a “cube with holes.” However, I would like to eliminate the holes, and also make the cube nearer the shape of an actual cube, with only slightly rounded corners and edges. I’ve tried changing the exponent to values other than ^2 and also passing the range directly to the volume! function, to no avail. Examples of the types of cubes desired can be found in three-rounded-box images at CodeSandbox.


using Makie, GLMakie
fig = Figure()
range = LinRange(-1, 1, 5) # LinRange{Float64, Int64}
cube = [ (x.^2 + y.^2 + z.^2) for x = range, y = range, z = range ] # Array{Float64, 3}
ax = Axis3( fig[1,1], aspect = :data, azimuth = 1.17 * pi, viewmode = :fit, title = "Cube" )
volume!( cube, algorithm = :iso, isorange = 0.05, isovalue = 1.7 )
fig


I would consider another plotting library, if there is another that is much more suitable to this task.


You can do it with super-Gaussian function:

using GLMakie
fig = Figure()
range = LinRange(-1, 1, 5) # LinRange{Float64, Int64}
p = 4   # supergauss order
a = 1   # radius
cube = [@. exp(-(x/a)^p) * exp(-(y/a)^p) + exp(-(z/a)^p) for x = range, y = range, z = range ] # Array{Float64, 3}
ax = Axis3(fig[1,1], aspect = :data, azimuth = 1.17 * pi, viewmode = :fit, title = "Cube" )
volume!( cube, algorithm = :iso, isorange = 0.05, isovalue = 1.7 )
fig


Higher order p will give more sharp edges.

Thank you for this information. A couple of more questions. (1) How does one save the image to a, say, PNG, file. I’m looking into at Makie.save. (2) Running in Windows from command line, but plot does not appear unless I copy Julia source code and paste it into REPL. How does one make plot appear when running, say, Cube_Program.jl from Windows command line.

Sorry, you can disregard Question (1) as I have solved it using Makie.save.

Sorry. I have only Linux.

See https://discourse.julialang.org/t/color-of-a-parametric-surface-with-makie/91363/3.

Thanks for this additional information. I’m also using the following to draw a cube, but would like to add line segments to emphasize the cube’s edges, as shown in red in the figure below. Any suggestions on how to add line segments for the cube’s edges?

using GLMakie
vertices = [ 0.0 0.0 0.0;
             1.0 0.0 0.0;
             1.0 1.0 0.0;
             0.0 1.0 0.0;
             0.0 0.0 1.0;
             1.0 0.0 1.0;
             1.0 1.0 1.0;
             0.0 1.0 1.0; ]
faces = [ 1 2 3; 3 4 1;
          5 6 7; 7 8 5;
          6 2 3; 3 7 6;
          5 1 4; 4 8 5;
          8 7 3; 3 4 8;
          5 6 2; 2 1 5 ]
colors = [ :gray, :gray, :gray, :gray, :gray, :gray, :gray, :gray ]
scene = GLMakie.mesh(vertices, faces, color = colors, shading = true )


There’s probably a function to draw some segments in Makie.


The cube’s edges can be drawn utilizing the function lines

using GLMakie
theScene = GLMakie.Scene()
vertices = [ 0.0 0.0 0.0;
             1.0 0.0 0.0;
             1.0 1.0 0.0;
             0.0 1.0 0.0;
             0.0 0.0 1.0;
             1.0 0.0 1.0;
             1.0 1.0 1.0;
             0.0 1.0 1.0; ]
faces = [ 1 2 3; 3 4 1;
          5 6 7; 7 8 5;
          6 2 3; 3 7 6;
          5 1 4; 4 8 5;
          8 7 3; 3 4 8;
          5 6 2; 2 1 5 ]
colors = [ :gray, :gray, :gray, :gray, :gray, :gray, :gray, :gray ]
GLMakie.mesh!(theScene,vertices,faces, color = colors, shading = true)

GLMakie.lines!(theScene, LinRange(0,1,2), LinRange(0,0,2), LinRange(0,0,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(0,1,2), LinRange(0,0,2), LinRange(1,1,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(0,1,2), LinRange(1,1,2), LinRange(0,0,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(0,1,2), LinRange(1,1,2), LinRange(1,1,2), linewidth = 10)

GLMakie.lines!(theScene, LinRange(0,0,2), LinRange(0,1,2), LinRange(0,0,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(0,0,2), LinRange(0,1,2), LinRange(1,1,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(1,1,2), LinRange(0,1,2), LinRange(0,0,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(1,1,2), LinRange(0,1,2), LinRange(1,1,2), linewidth = 10)

GLMakie.lines!(theScene, LinRange(0,0,2), LinRange(0,0,2), LinRange(0,1,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(0,0,2), LinRange(1,1,2), LinRange(0,1,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(1,1,2), LinRange(0,0,2), LinRange(0,1,2), linewidth = 10)
GLMakie.lines!(theScene, LinRange(1,1,2), LinRange(1,1,2), LinRange(0,1,2), linewidth = 10)



you might be interested in this section How to Cube - Beautiful Makie

2 Likes