How to render a cylinder?

Hi everyone, I want to plot a cylinder centered at point p, radius r, and aligned with an axis vector. For now, I have this code:

using GLMakie

r = 5
h = 3
m, n =200, 150
u = range(0, 2pi, length=n)
v = range(0, h, length=m)

us = ones(m)*u'
vs = v*ones(n)'
#Surface parameterization
X = r*cos.(us)
Y = r*sin.(us)
Z = vs

p = GLMakie.surface(X, Y, Z, color=:orange)
display(p)

The problem is that the cylinder is hollow. How can I fix this?

This works

using Meshes
using MeshViz
using GLMakie

cyl1 = Cylinder(1.2, Segment((0., 0., 0.), (1., 2., 1.)))
cyl2 = Cylinder(0.8, Segment((1., 1., 1.), (2., 2., 2.)))


vis1 = viz(cyl1, color=:red, alpha=1.0)
viz!(cyl2, color=:gray, alpha=0.5)

display(vis1)
1 Like

Here is the docstring of the Cylinder geometry:

https://juliageometry.github.io/Meshes.jl/stable/geometries/primitives.html#Meshes.Cylinder

Iā€™m having now some problems setting the scene. Can someone tell me how to add a mesh to a GLMakie scene? Also, what if I want to animate the cylinders, to make it easy, just changing the radius?

@cdelv can you elaborate on your questions? You add meshes the same way you add cylinders to the scene using the viz function or the its viz! cousin. Regarding animation, you need to add observables and Makie.jl has documentation on how to do it.

Well, I tried to make an observable cylinder and/or an observable radius and many combinations and I dint manage to make it update the render. Do you know how to do it? Maybe do a simple animation where the cylinder radius changes or something like that? What I really want is to change the orientation according to a quaternion I have, but with a simple example I can figure it out.

Maybe you are hitting

The issue is mostly fixed except for the geometric parameters of the objects.

I realized that Iā€™ll need the same thing soon, so I gave it a shot, but failed. I assumed that the following code should work:

using Meshes, MeshViz
import GLMakie as Mke

basepose = Mke.Observable(Point3(0.,0,0))
plane1 = Mke.@lift Plane($basepose, Vec3(0,0,1))
plane2 = Mke.@lift Plane($basepose + Vec3(0,0,10), Vec3(0,0,1))
cylinder = Mke.@lift Cylinder(5.0, $plane1, $plane2)
viz(cylinder)
# wait for Makie window to appear
basepose[] = Point3(-10.0, -10, -10)

then I though maybe I canā€™t update the points of a geometry, so I tried to change a transformation by adding observables to a docs example, but that didnā€™t work either:

grid = CartesianGrid(10, 10)
trsl = Mke.Observable(Translate(10., 20.))

mesh = Mke.@lift grid |> $trsl

fig = Mke.Figure(resolution = (800, 400))
viz(fig[1,1], grid)
viz(fig[1,2], mesh)
fig
# wait for Makie window to appear
trsl[] = Translate(-10., -20.,)

with Julia v1.8.5 and versions:

  [e9467ef8] GLMakie v0.8.4
  [9ecf9c4f] MeshViz v0.7.5
  [eacbb407] Meshes v0.28.1

What am I missing here?

Edit: by failing I mean: when I ran the lines, that update the observables, nothing changed in the makie window.

There are some simple examples of mesh animation here. In essence, those examples modify the meshā€™s vertices and then recreate the new mesh using GeometryBasics.Mesh(V, F) where V and F are vectors defining the meshā€™s Vertices and triangle Faces.

This is the exact issue I linked above, right? We are currently not forwarding the geometric parameters as observables to the low-level Makie.jl recipes.

If someone has time to fix it in MeshViz.jl, I am happy to review the PR. Otherwise, I will try to fix it in the following days.

I apologize, Iā€™ve read the issue twice and I didnā€™t realize the meaning of the texts. Now I do. Sorry for the noise :frowning:

Iā€™ll check if I could setup a PR, but Iā€™m afraid, I will not. Thank you for your efforts anyway, I really do like the Meshes/MeshViz ecosystem!

1 Like

The issue above has been fixed. The release should be available in a couple of minutes:

2 Likes

Ohh, thatā€™s great. Iā€™ll look into it in the following days. Thank you for being so fast.

Again, if you have a brief example Iā€™ll appreciate it. Else, Iā€™ll try to figure it out during the weekend.

From the docs:

using Meshes, MeshViz

import GLMakie as Mke

c = Cylinder(1.0)

viz(c)

image

Here is a collection of random cylinders:

cs = [Cylinder(1.0, Segment(rand(Point3, 2))) for i=1:3]

viz(cs, color=1:3)

image

1 Like

Maybe like this

"""
based on 
https://beautiful.makie.org/dev/examples/generated/2d
/mscatters/SSAO_meshscatter/
"""

using GLMakie,GeometryBasics
GLMakie.activate!(ssao=true)
GLMakie.closeall() # close any open screen

#=======Setting  define notice for ssao setting=============#
fig = Figure(resolution = (600, 600))
ssao = Makie.SSAO(radius = 5.0, blur = 2)
ax = LScene(fig[1, 1], scenekw = (ssao=ssao,),show_axis=false)
ax.scene.ssao.bias[] = 0.025
#===========================================================#

"""
## define cylinder
`cylinder=Cylinder(startpoint,maxheightpoint, radius)`
"""
cylinder=Cylinder(Point3(1.0,1.0,1.0), Point3(1.0,1.0,4.0), 2.0)
mesh!(ax, cylinder, ssao=true,color=:lightblue)

fig
#save("ssao-cyclinder.png",fig)

add tesselation method

original cylinder methods seems not very smooth, so find out this way

"""
- ssao methods  based on https://beautiful.makie.org/dev/examples/generated/2d/mscatters/SSAO_meshscatter/

- cylinder see: https://juliageometry.github.io/GeometryBasics.jl/stable/api/#GeometryBasics.Cylinder

- tesselation see: https://juliageometry.github.io/GeometryBasics.jl/stable/api/#GeometryBasics.Tesselation
"""

using GLMakie,GeometryBasics
GLMakie.activate!(ssao=true)
GLMakie.closeall() # close any open screen

#=======Setting  define notice for ssao setting========#
fig = Figure(resolution = (1200, 600))
ssao = Makie.SSAO(radius = 5.0, blur = 2)
ax1 = LScene(fig[1, 1], scenekw = (ssao=ssao,),show_axis=false)
ax1.scene.ssao.bias[] = 0.025
ax2 = LScene(fig[1, 2], scenekw = (ssao=ssao,),show_axis=false)
ax2.scene.ssao.bias[] = 0.025
#============================#

"""
## define  default  cylinder
`cylinder=Cylinder(startpoint,maxheightpoint, radius)`
"""
cylinder1=Cylinder(Point3(1.0,1.0,1.0), Point3(1.0,1.0,4.0), 2.0)


"""
    CylinderTess(; o=Point3(1.0,1.0,1.0),m=Point3(1.0,1.0,4.0), r=2.0, tess=60)
# define cylinder with tesselation 
>for  fine grained with tess,  higer value,higher fine grained

return  object with more grined coordinates
"""
function CylinderTess(; o=Point3(1.0,1.0,1.0),m=Point3(1.0,1.0,4.0), r=2.0, tess=60)
    return uv_normal_mesh(Tesselation(Cylinder(o,m,r), tess))
end


cylinder2=CylinderTess(;tess=100)

mesh!(ax1,cylinder1,ssao=true,color=:lightblue)
mesh!(ax2,cylinder2,ssao=true,color=:lightblue)

fig

#save("ssao-cyclinder.png",fig)