# How to render a cylinder?

**URL:** <https://discourse.julialang.org/t/how-to-render-a-cylinder/98063>\
**Category:** Visualization\
**Tags:** question, plotting, makie\
**Created:** [April 28, 2023, 9:51pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063 "2023-04-28T21:51:13Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdelv/32/44851_2.png) [@cdelv](https://discourse.julialang.org/u/cdelv)\
**Post date:** [April 28, 2023, 9:51pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/1 "2023-04-28T21:51:13Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![cdelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdelv/32/44851_2.png) [@cdelv](https://discourse.julialang.org/u/cdelv)\
**Post date:** [April 28, 2023, 10:53pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/2 "2023-04-28T22:53:14Z")

</div>

This works

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 28, 2023, 11:50pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/3 "2023-04-28T23:50:34Z")

</div>

Here is the docstring of the Cylinder geometry:

[https://juliageometry.github.io/Meshes.jl/stable/geometries/primitives.html#Meshes.Cylinder](https://juliageometry.github.io/Meshes.jl/stable/geometries/primitives.html#Meshes.Cylinder)

---

<div class="post-metadata">

**Author:** ![cdelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdelv/32/44851_2.png) [@cdelv](https://discourse.julialang.org/u/cdelv)\
**Post date:** [April 29, 2023, 7:04am UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/4 "2023-04-29T07:04:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 29, 2023, 12:49pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/5 "2023-04-29T12:49:38Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![cdelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdelv/32/44851_2.png) [@cdelv](https://discourse.julialang.org/u/cdelv)\
**Post date:** [April 29, 2023, 2:08pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/6 "2023-04-29T14:08:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 29, 2023, 2:32pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/7 "2023-04-29T14:32:36Z")

</div>

Maybe you are hitting

> <https://github.com/JuliaGeometry/MeshViz.jl/issues/32>
>
> The following code displays the window but the contents do not change, and the r…ecorded video does not change, either.
> 
> \`\`\`julia
> time = GLMakie.Observable(0.0)
> radius = GLMakie.@lift $time % 1.0 + 0.2
> ball = GLMakie.@lift Meshes.Ball(rand(Meshes.Point3), $radius)
> fig = MeshViz.viz(ball; color=GLMakie.@lift \[:green, :blue, :red, :gray\]\[Int(ceil($time \* 11.2) % 4 + 1)\])
> framerate = 30
> timestamps = range(0, 100, step=1/framerate)
> GLMakie.display(fig)
> 
> GLMakie.record(fig, "time\_animation.mp4", timestamps; framerate = framerate) do t
> time\[\] = t
> end
> \`\`\`
> 
> In contrast, the following code works
> 
> \`\`\`julia
> time = GLMakie.Observable(0.0)
> 
> xs = range(0, 7, length=40)
> 
> ys\_1 = GLMakie.@lift(sin.(xs .- $time))
> ys\_2 = GLMakie.@lift(cos.(xs .- $time) .+ 3)
> 
> fig = GLMakie.lines(xs, ys\_1, color = :blue, linewidth = 4,
> axis = (title = GLMakie.@lift("t = $(round($time, digits = 1))"),))
> GLMakie.scatter!(xs, ys\_2, color = :red, markersize = 15)
> 	
> framerate = 30
> timestamps = range(0, 100, step=1/framerate)
> GLMakie.display(fig)
> 	
> GLMakie.record(fig, "time\_animation.mp4", timestamps; framerate = framerate) do t
> time\[\] = t
> end
> \`\`\`

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

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [April 29, 2023, 4:14pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/8 "2023-04-29T16:14:13Z")

</div>

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:

```julia
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](https://juliageometry.github.io/Meshes.jl/stable/transforms.html#Meshes.Translate), but that didn’t work either:

```julia
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:

```julia
  [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.

---

<div class="post-metadata">

**Author:** ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)\
**Post date:** [April 29, 2023, 4:21pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/9 "2023-04-29T16:21:40Z")

</div>

> [@cdelv](#):
>
> but with a simple example I can figure it out.

There are some simple examples of mesh animation [here](https://discourse.julialang.org/t/essay-julia-and-projective-geometric-algebra/92767/9). 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.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 29, 2023, 4:22pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/10 "2023-04-29T16:22:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [April 29, 2023, 4:28pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/11 "2023-04-29T16:28:19Z")

</div>

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 ☹

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!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 3, 2023, 1:50pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/12 "2023-05-03T13:50:43Z")

</div>

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

> <https://github.com/JuliaRegistries/General/pull/82800>
>
> \- Registering package: MeshViz
> \- Repository: https://github.com/JuliaGeometry/Me…shViz.jl
> \- Created by: @juliohm
> \- Version: v0.7.6
> \- Commit: d987ca680a72e2f476c975c7745bc0050174970f
> \- Reviewed by: @juliohm
> \- Reference: https://github.com/JuliaGeometry/MeshViz.jl/commit/d987ca680a72e2f476c975c7745bc0050174970f#commitcomment-111636249
> \- Description: Makie.jl recipes for visualization of Meshes.jl

---

<div class="post-metadata">

**Author:** ![cdelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdelv/32/44851_2.png) [@cdelv](https://discourse.julialang.org/u/cdelv)\
**Post date:** [May 4, 2023, 9:28pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/13 "2023-05-04T21:28:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 4, 2023, 9:46pm UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/14 "2023-05-04T21:46:31Z")

</div>

From the docs:

```julia
using Meshes, MeshViz

import GLMakie as Mke

c = Cylinder(1.0)

viz(c)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/9/5/9574786cf83ba4c3acda83b57bbdfcc30d66ef5f.png)

Here is a collection of random cylinders:

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

viz(cs, color=1:3)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/c/6/c65b36072295889bdde1c086037f70df30623c88.png)

---

<div class="post-metadata">

**Author:** ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)\
**Post date:** [May 6, 2023, 7:29am UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/15 "2023-05-06T07:29:17Z")

</div>

Maybe like this

```julia
"""
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)

```

---

<div class="post-metadata">

**Author:** ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)\
**Post date:** [May 7, 2023, 4:28am UTC](https://discourse.julialang.org/t/how-to-render-a-cylinder/98063/16 "2023-05-07T04:28:14Z")

</div>

# add `tesselation` method

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

```julia-auto
"""
- 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)
```
