Data Structures for 3D mesh

I want to plot 3D triangular mesh in Julia. “JuliaPlots/Makie.jl” package looks good for this purposes and I visited the “JuliaPlots/MakieGallery.jl/blob/master/examples/examples3d.jl” for some examples. And probably the code

mesh1 = normal_mesh(geometry)

construct a mesh. So I tried to expect what is the “normal_mesh” method ans see…

# 2 methods for generic function "normal_mesh":
  [1] normal_mesh(primitive::Union{AbstractPolygon{N,T}, GeometryPrimitive{N,T}, AbstractArray{var"#s21",1} where var"#s21"<:AbstractPoint{N,T}, GeometryBasics.Mesh{N,T,Element,V} where V<:AbstractArray{Element,1} where Element<:Polytope{N,T}, Tesselation{N,T,Primitive,NGrid} where NGrid where Primitive} where T; nvertices) where N in GeometryBasics

For me it is difficult to understand all this types hierarchy. Typically for 3D mesh I used only two flat arrays with vertices and triangles. Do we have something similar in Julia or I need to construct a mesh like in the example with full hierarchy of types?

Take a look at Meshes.jl: GitHub - JuliaGeometry/Meshes.jl: Computational geometry and meshing algorithms in Julia

1 Like

I know the types in GeometryBasics are pretty off-putting, but it’s actually pretty easy to construct a mesh:

julia> rect = Rect(Vec(0.0, 0.0), Vec(1.0, 1.0))
# decompose the rectangle into two triangular faces
julia> rect_faces = decompose(TriangleFace{Int}, rect)
2-element Array{NgonFace{3,Int64},1}:
 TriangleFace(1, 2, 4)
 TriangleFace(1, 4, 3)

# decompose the rectangle into four vertices
julia> rect_vertices = decompose(Point{2, Float64}, rect)
4-element Array{Point{2,Float64},1}:
 [0.0, 0.0]
 [1.0, 0.0]
 [0.0, 1.0]
 [1.0, 1.0]

# combine the vertices and faces into a triangle mesh
julia> mesh = Mesh(rect_vertices, rect_faces)

A face is just a type for wrapping indices:

TriangleFace(1, 2, 3) -> indices [1,2,3]

You can also reinterpret existing index arrays to TriangleFace