JuliaGeometry / GeometryBasics.jl

Basic Geometry Types
MIT License
165 stars 54 forks source link

add triangle_mesh shortcut #197

Closed JonasIsensee closed 1 year ago

JonasIsensee commented 1 year ago

When plotting a vector of many meshes in 2D and 3D a large portion of time is taken up by triangle_mesh or more specifically decompose_triangulate_fallback to first decompose and then reassemble the meshes.

This PR makes triangle_mesh(::TriangleMesh) an identity operation.

EDIT: this change does not yet yield the correct dispatch. However, here's an example to show that it is worth it:

using GLMakie, GeometryBasics

julia> m = GeometryBasics.mesh([Point2f(0.0,0.0), Point2f(0.0,1.0), Point2f(1.0,0.0)], facetype = GLTriangleFace)
Mesh{2, Float32, Triangle}:
 Triangle(Float32[0.0, 0.0], Float32[1.0, 0.0], Float32[0.0, 1.0])

julia> mv = [deepcopy(m) for i=1:200000];

julia> @time mesh(mv) # second time
  1.179934 seconds (7.24 M allocations: 567.325 MiB, 6.40% gc time)

# here's an inelegant pass-through definition
julia> GeometryBasics.triangle_mesh(primitive::typeof(m)) = primitive

julia> @time mesh(mv) # second time
  0.064663 seconds (438.06 k allocations: 85.145 MiB, 34.41% gc time)
JonasIsensee commented 1 year ago

Correction: this doesn't work yet. I'm struggling with the correct dispatch pattern.

TriangleMesh{N} is seen as less specific compared to Meshable{N}. Given that there is such a detailed hierarchy of abstract mesh types it seems odd that the Meshable union contains Mesh{Dim, T} directly instead of e.g. AbstractMesh.

https://github.com/JuliaGeometry/GeometryBasics.jl/blob/48b1ef6a1fe5cf0e877b318437a0a58169030ee3/src/interfaces.jl#L88

JonasIsensee commented 1 year ago

superceded by #198