# How do I access the faces of a 3D element in Meshes.jl?

**URL:** <https://discourse.julialang.org/t/how-do-i-access-the-faces-of-a-3d-element-in-meshes-jl/94746>\
**Category:** New to Julia\
**Created:** [February 16, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-do-i-access-the-faces-of-a-3d-element-in-meshes-jl/94746 "2023-02-16T21:20:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 16, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-do-i-access-the-faces-of-a-3d-element-in-meshes-jl/94746/1 "2023-02-16T21:20:45Z")

</div>

Hello!

I am very new to Meshes.jl and getting a bit perplexed by all of it, since I cannot really find any good examples other than the quick examples, and I atleast cannot find what I want to do.

I have one single box element and I want to plot the normals on all the outer surfaces. Currently the code produces:

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

As you see I am missing 7 of them 🙂 How do I loop over faces, and preferably in such a way the code would also work for 2D?

The code is here:

```julia
using Meshes
using MeshViz
using GLMakie
using LinearAlgebra
using Statistics

grid = CartesianGrid(1,1,1)
display(viz(grid , showfacets = true, alpha=0.25))

function ExtractMidpoints(p)
    o = p.vertices

    mp = mean(map(x->x.coords,o))

    return mp
end

function GenerateGridNormal(p)
    o = p.vertices
    
    p1 = o[1]
    p2 = o[2]
    p3 = o[3]

    v1 = p2 - p1
    v2 = p3 - p1

    n = cross(v1,v2)

    n /= norm(n)

    return n
end

mps = Tuple.(ExtractMidpoints.(grid))
nms = Tuple.(GenerateGridNormal.(grid))

scatter!(Tuple.(ExtractMidpoints.(grid)))
arrows!(first.(mps),getindex.(mps,2),last.(mps),first.(nms),getindex.(nms,2),last.(nms))

```

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 17, 2023, 5:39pm UTC](https://discourse.julialang.org/t/how-do-i-access-the-faces-of-a-3d-element-in-meshes-jl/94746/2 "2023-02-17T17:39:44Z")

</div>

Just bumping my thread, since I have still not figured out to do so. It should be really simple, I just cannot find the right syntax 🙂

---

<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:** [February 17, 2023, 6:09pm UTC](https://discourse.julialang.org/t/how-do-i-access-the-faces-of-a-3d-element-in-meshes-jl/94746/3 "2023-02-17T18:09:32Z")

</div>

Maybe `faces(mesh, 2)` is what you’re lookimg for? (Sorry, I’m from phone nor can I test it or check my code that did something similae.)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 17, 2023, 6:15pm UTC](https://discourse.julialang.org/t/how-do-i-access-the-faces-of-a-3d-element-in-meshes-jl/94746/4 "2023-02-17T18:15:45Z")

</div>

> [@cserteGT3](#):
>
> faces(mesh, 2)

This returns:

```julia
faces(grid,2)
Base.Generator{Base.Generator{UnitRange{Int64}, Meshes.var"#94#95"{GridTopology{3}}}, Meshes.var"#131#132"{CartesianGrid{3, Float64}}}(Meshes.var"#131#132"{CartesianGrid{3, Float64}}(1×1×1 CartesianGrid{3,Float64}), Base.Generator{UnitRange{Int64}, Meshes.var"#94#95"{GridTopology{3}}}(Meshes.var"#94#95"{GridTopology{3}}(1×1×1 GridTopology(aperiodic, aperiodic, aperiodic)), 1:6))

```

Which I am not familiar at all with, i.e. how to use. When I then try:

```julia
for f in faces(grid,2)
       print(f)
       end
┌ Error: not implemented

```

And then I try:

```julia
 collect(faces(grid,2))
┌ Error: not implemented

```

Which just seems weird to me, it seems to me atleast, that it is a basic need being able to loop over faces, edges, vertices etc.

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 18, 2023, 3:11pm UTC](https://discourse.julialang.org/t/how-do-i-access-the-faces-of-a-3d-element-in-meshes-jl/94746/5 "2023-02-18T15:11:29Z")

</div>

@juliohm kindly showed me how to do it.

```julia
grid = CartesianGrid(1,1,1)

boundary(grid[1])
6 SimpleMesh{3,Float64}
  8 vertices
    └─Point(0.0, 0.0, 0.0)
    └─Point(1.0, 0.0, 0.0)
    └─Point(1.0, 1.0, 0.0)
    └─Point(0.0, 1.0, 0.0)
    └─Point(0.0, 0.0, 1.0)
    └─Point(1.0, 0.0, 1.0)
    └─Point(1.0, 1.0, 1.0)
    └─Point(0.0, 1.0, 1.0)
  6 elements
    └─Quadrangle(4, 3, 2, 1)
    └─Quadrangle(6, 5, 1, 2)
    └─Quadrangle(3, 7, 6, 2)
    └─Quadrangle(4, 8, 7, 3)
    └─Quadrangle(1, 5, 8, 4)
    └─Quadrangle(6, 7, 8, 5)

```

I knew it was there, I just couldn’t find it! 😅

Kind regards
