@@ -56,12 +56,10 @@ function mesh(
5656
5757 fs = decompose (facetype, faces)
5858
59- va = Dict {Symbol, VertexAttributeType} ()
60- for k in keys (vertex_attributes)
61- if ! isnothing (vertex_attributes[k])
62- va[k] = convert_facetype (facetype, vertex_attributes[k])
63- end
64- end
59+ names = keys (vertex_attributes)
60+ valid_names = filter (name -> ! isnothing (vertex_attributes[name]), names)
61+ vals = convert_facetype .(Ref (facetype), getindex .(Ref (vertex_attributes), valid_names))
62+ va = NamedTuple {valid_names} (vals)
6563
6664 return Mesh (positions, fs; va... )
6765end
@@ -80,22 +78,22 @@ function mesh(
8078 attributes...
8179 ) where {D, T, FT <: AbstractFace }
8280
83- va = Dict {Symbol, VertexAttributeType} ()
84- for k in keys (attributes)
85- isnothing (attributes[k]) || setindex! (va, attributes[k], k)
86- end
81+ names = keys (attributes)
82+ valid_names = filter (name -> ! isnothing (attributes[name]), names)
8783
88- if isempty (va ) && (GeometryBasics. pointtype (mesh) == pointtype) && (FT == facetype)
84+ if isempty (valid_names ) && (GeometryBasics. pointtype (mesh) == pointtype) && (FT == facetype)
8985 return mesh
9086 else
91- # 1. add vertex attributes, 2. convert position attribute
87+ vals = getindex .(Ref (attributes), valid_names)
88+ va = NamedTuple {valid_names} (vals)
89+
90+ # add vertex attributes
9291 va = merge (vertex_attributes (mesh), va)
93- va[:position ] = decompose (pointtype, va[:position ])
94-
95- # Resolve facetype conversions of FaceViews
96- for (k, v) in va
97- va[k] = convert_facetype (facetype, v)
98- end
92+ # convert position attribute and facetypes in FaceViews
93+ va = NamedTuple {keys(va)} (map (keys (va)) do name
94+ val = name == :position ? decompose (pointtype, va[:position ]) : va[name]
95+ return convert_facetype (facetype, val)
96+ end )
9997
10098 # update main face type
10199 f, views = decompose (facetype, faces (mesh), mesh. views)
@@ -275,18 +273,18 @@ function Base.merge(meshes::AbstractVector{<:Mesh})
275273 if consistent_face_views
276274
277275 # All the same kind of face, can just merge
278- new_attribs = Dict {Symbol, VertexAttributeType } (map (collect ( names) ) do name
279- return name => reduce (vcat, getproperty .(meshes, name))
276+ new_attribs = NamedTuple {names } (map (names) do name
277+ return reduce (vcat, getproperty .(meshes, name))
280278 end )
281279 fs = reduce (vcat, faces .(meshes))
282280
283281 # TODO : is the type difference in offset bad?
284282 idx = length (faces (m1))
285- offset = length (coordinates (m1)):: Int
283+ offset = length (coordinates (m1)):: Int # TODO : unnecessary
286284 views = isempty (m1. views) ? UnitRange{Int64}[1 : idx] : copy (m1. views)
287285
288286 Ns = length .(faces .(meshes))
289- Ms = length .(coordinates .(meshes)):: Vector{Int}
287+ Ms = length .(coordinates .(meshes)):: Vector{Int} # TODO : unnecessary
290288 for (mesh, N, M) in Iterators. drop (zip (meshes, Ns, Ms), 1 )
291289 # update face indices
292290 for i = idx .+ (1 : N)
@@ -330,11 +328,11 @@ function clear_faceviews(mesh::Mesh)
330328 main_fs = faces (mesh)
331329 va = vertex_attributes (mesh)
332330
333- names = filter (name -> va[name] isa FaceView, collect ( keys (va) ))
331+ names = filter (name -> va[name] isa FaceView, keys (va))
334332 isempty (names) && return mesh
335333
336334 other_fs = faces .(getproperty .((mesh,), names))
337- pushfirst! ( names, :position )
335+ names = ( :position , names ... )
338336 all_fs = tuple (main_fs, other_fs... )
339337
340338 if isempty (mesh. views)
@@ -343,21 +341,19 @@ function clear_faceviews(mesh::Mesh)
343341
344342 named_maps = NamedTuple {tuple(names...)} (maps)
345343
346- new_va = Dict {Symbol, VertexAttributeType} ()
347- for (name, attrib) in va
348- new_va[name] = values (attrib)[get (named_maps, name, maps[1 ])]
349- end
344+ new_va = NamedTuple {keys(va)} (map (keys (va)) do name
345+ values (va[name])[get (named_maps, name, maps[1 ])]
346+ end )
350347
351348 return Mesh (new_va, new_fs)
352349
353350 else
354351
355352 new_fs = sizehint! (eltype (main_fs)[], length (main_fs))
356353 new_views = sizehint! (UnitRange{Int}[], length (mesh. views))
357- new_va = Dict {Symbol, VertexAttributeType} ()
358- for (name, attrib) in va
359- new_va[name] = sizehint! (eltype (values (attrib))[], length (attrib))
360- end
354+ new_va = NamedTuple {keys(va)} (map (keys (va)) do name
355+ sizehint! (similar (values (va[name]), 0 ), length (va[name]))
356+ end )
361357
362358 vertex_index_counter = eltype (first (main_fs))(1 )
363359
@@ -448,16 +444,17 @@ This also removes unused vertices.
448444function split_mesh (mesh:: Mesh , views:: Vector{UnitRange{Int}} = mesh. views)
449445 return map (views) do idxs
450446 new_fs, maps = merge_vertex_indices ((view (faces (mesh), idxs),))
451- new_va = Dict {Symbol, VertexAttributeType} ()
452-
453- for (k, v) in vertex_attributes (mesh)
447+
448+ names = keys (vertex_attributes (mesh))
449+ new_va = NamedTuple {names} (map (names) do name
450+ v = getproperty (mesh, name)
454451 if v isa FaceView
455452 _fs, _maps = merge_vertex_indices ((view (faces (v), idxs),))
456- new_va[k] = FaceView (values (v)[_maps[1 ]], _fs)
453+ return FaceView (values (v)[_maps[1 ]], _fs)
457454 else
458- new_va[k] = v[maps[1 ]]
455+ return v[maps[1 ]]
459456 end
460- end
457+ end )
461458
462459 return Mesh (new_va, new_fs)
463460 end
0 commit comments