# Adjacent elements of a Vertex in Meshes.jl

**URL:** <https://discourse.julialang.org/t/adjacent-elements-of-a-vertex-in-meshes-jl/131900>\
**Category:** Visualization\
**Tags:** meshes\
**Created:** [August 27, 2025, 9:44pm UTC](https://discourse.julialang.org/t/adjacent-elements-of-a-vertex-in-meshes-jl/131900 "2025-08-27T21:44:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [August 27, 2025, 9:44pm UTC](https://discourse.julialang.org/t/adjacent-elements-of-a-vertex-in-meshes-jl/131900/1 "2025-08-27T21:44:19Z")

</div>

How to get the adjacent elements of a vertex in Meshes.jl ?

```julia-auto
using Meshes, CoordRefSystems ,GLMakie

r = Float32[1.6, 1.754088, 1.9230156, 2.1082115, 2.311243, 2.5338273, 2.7778475, 3.0453684, 3.3386526, 3.6601818, 4.012676, 4.3991165, 4.8227735, 5.287231, 5.7964177, 6.354642, 6.9666257, 7.637547, 8.373081]
θ = Float32[0.0, 0.09817477, 0.19634955, 0.2945243, 0.3926991]
ϕ = Float32[0.0, 0.3926991, 0.7853982, 1.1780972, 1.5707964, 1.9634954, 2.3561945, 2.7488935, 3.1415927, 3.5342917, 3.9269907, 4.3196898, 4.712389, 5.105088, 5.497787, 5.8904862, 6.2831855]

g = RectilinearGrid{𝔼,typeof(Spherical(0,0,0))}(r, θ, ϕ)
ver = vertex(g,1)

```

I only need element id numbers.

> <https://github.com/JuliaGeometry/Meshes.jl/blob/db4881f0b5500ab39aef86681b4b2d33a747a244/src/toporelations/adjacency.jl#L5-L9>

What does below code represents?

```julia-auto
for i in 1:nvertices(g)
    println(Coboundary{0,3}(topology(g))(i))
end

```

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [August 29, 2025, 4:39pm UTC](https://discourse.julialang.org/t/adjacent-elements-of-a-vertex-in-meshes-jl/131900/2 "2025-08-29T16:39:49Z")

</div>

`Coboundary{0,3}` gives you all the elements (Hexahedron’s in this case) that share a given vertex `i`.

You can confirm this by:

```julia-repl
julia> ver = vertex(g,4)
Point with Spherical{NoDatum} coordinates
├─ r: 2.1082115173339844 m
├─ θ: 0.0 rad
└─ ϕ: 0.0 rad

julia> Coboundary{0,3}(topology(g))(4) # returns element id's
(3, 4)

julia> g[3]
Hexahedron
├─ Point(r: 1.9230155944824219 m, θ: 0.0 rad, ϕ: 0.0 rad)
├─ Point(r: 2.1082115173339844 m, θ: 0.0 rad, ϕ: 0.0 rad) # vertex 4
├─ ...

julia> g[4]
Hexahedron
├─ Point(r: 2.1082115173339844 m, θ: 0.0 rad, ϕ: 0.0 rad) # vertex 4
├─ ...

julia> # do any elements besides 3 and 4 have vertex 4?
julia> any(e -> e ∉ [3,4] && any(v -> v === ver, vertices(g[e])), 1:nelements(g))
false

```

`Coboundary{0,2}` will give you the 2d elements that share a given vertex.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [August 29, 2025, 9:13pm UTC](https://discourse.julialang.org/t/adjacent-elements-of-a-vertex-in-meshes-jl/131900/3 "2025-08-29T21:13:37Z")

</div>

For my mesh i have a element’s color matrix . This code can be used to create vertex colors when our data don’t have color for vertex. It takes vertex color as average of colors in adjacent elements.

```julia-auto
vertex_color = Float64[]
for i in 1:nelements(g)
    el= element(g,i)
    bd = Boundary{3,0}(topology(g))(i)	#Ntuple of vertex ids of i-th element
    for id in bd
		adels= Coboundary{0,3}(topology(g))(id)	#Ntuple of element ids adjacent to this vertex
		clct = collect(Element_color[(elem2cart(topology(g),idx))...] for idx in adels)	#position of i-th element in grid	
		push!(vertex_color, sum(clct)/length(clct))
	end
	@inline viz!(el, color =vertex_color, alpha=0.2)
	#@show clr
end

```

Can you tell more efficient version of this code? Thanks.
