Plots.jl icon indicating copy to clipboard operation
Plots.jl copied to clipboard

[BUG] mesh3d fails for a simple mesh

Open pgeipi10 opened this issue 8 months ago • 1 comments

using Plots, TriplotRecipes

p = Float64[
    -1.0   1.0  1.0  -1.0   0.0  0.0  -1.0  0.0  1.0
    -1.0  -1.0  1.0   1.0  -1.0  0.0   0.0  1.0  0.0
]

t = Int32[
    1  2  4  5  3  4  2  8
    5  6  7  6  8  6  9  6
    7  5  6  7  9  8  6  9
]

x, y = p[1,:], p[2,:]
z = x.^2 + y.^2 .- 1

display(plot(x, y, z, seriestype=:mesh3d, faces=t'.- 1, color=:lightblue))

Causes error:

ERROR: ArgumentError: Unsupported `:connections` type Nothing for seriestype=mesh3d

Sometimes the error does not appear, but then the plot is not correct

pgeipi10 avatar Mar 09 '25 05:03 pgeipi10

So a few things

  • The argument for mesh3d is connections to define the triangles, not faces.
  • Julia uses 1-based indexing so no need to subtract one from your coordinates (which for some reason plots, though not sensible).
  • The coordinate indexing needs to be Int, not Int32 (haven't tracked where that limitation is encoded). Just removing explicit typing works fine (or if you have it as Int32 from elsewhere, Int.(t) will also work).
  • connections needs to be a AVec{AVec{Int}} rather than Matrix{Int} so wrapping with eachcol gets it working

With that in mind, the following seems to work cleanly (adding in some alpha so it can be seen more clearly)

using Plots

p = [
    -1.0   1.0  1.0  -1.0   0.0  0.0  -1.0  0.0  1.0
    -1.0  -1.0  1.0   1.0  -1.0  0.0   0.0  1.0  0.0
]

t = [
    1  2  4  5  3  4  2  8
    5  6  7  6  8  6  9  6
    7  5  6  7  9  8  6  9
]

x, y = p[1,:], p[2,:]
z = x.^2 + y.^2

mesh3d(x,y,z,connections=eachcol(t), fillcolor=:blue, linecolor=:black, fillalpha=0.2)

Image

InfiniteChai avatar Jun 10 '25 21:06 InfiniteChai