How to construct a `Mesh` from faces containing `TriangleFace` and `QuadFace` with GeometryBasics

I am trying to do read/write of a simple Wavefront .obj file without triangulation.

However, the following will result in an error.

julia> using GeometryBasics # v0.4.10

julia> vertices = Point3f[(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 1)];

julia> faces = [QuadFace(1, 2, 3, 4), TriangleFace(5, 2, 1)];

julia> Mesh(vertices, faces)
ERROR: MethodError: no method matching Polytope(::Type{Point{3, Float32}}, ::Type{NgonFace{S, Int64} where S})

julia> VERSION
v"1.10.2"

The construction will succeed if the type is sufficiently qualified as follows:

using GeometryBasics
import GeometryBasics.Ngon

TQFace = Union{TriangleFace{Int},QuadFace{Int}}
TQNgon = Union{Triangle{3,Float32}, Ngon{3,Float32,4,Point3f}}

vertices = Point3f[(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 1)]
tqfaces = TQFace[QuadFace(1, 2, 3, 4), TriangleFace(5, 2, 1)]

faceview = FaceView{TQNgon,Point3f,TQFace,Vector{Point3f},Vector{TQFace}}(vertices, tqfaces); # `show` throws an error

mesh = Mesh(faceview); # `show` throws an error
julia> faces(mesh) # seems to be working
2-element Vector{Union{TriangleFace{Int64}, QuadFace{Int64}}}:
 [1, 2, 3, 4]
 TriangleFace(5, 2, 1)

Are there additional packages that would be useful for this use case?

What you probably want to do is convert all of your faces to TriangleFace

Triangulation is an irreversible process. :confused:
On the other hand, converting a TriangleFace to a QuadFace with duplicate vertices may work.
However, I do not know if the workaround is common.

Meshes.jl is what you need, most likely.

1 Like

IIUC, Meshes.jl is independent of the GeometryBasics.jl ecosystem.
I prefer Meshes.jl’s design, but there is some resistance to say goodbye to GeometryBasics.jl. :sweat_smile:

Regardless, my objective can be achieved, and I would like to use Meshes.jl as a solution.

Using Meshes.jl, a mesh with triangular and rectangular faces could be generated from the SVG of Julia’s logo. :smiley:

1 Like