Skip to content

Conversation

halleysfifthinc
Copy link
Contributor

As we discussed on Zulip, here is a draft PR of my implementation of the topological
relations for HalfEdgeTopologys using the iterator interface. You are welcome to add/use
these however much or little you want, but I have no immediate need for these to be
upstreamed.

Script showing performance benefits of the non-allocating iteration interface:

function vertex_normals_master(msh, fnorms)
    vT = eltype(fnorms)
    vnorms = similar(fnorms, nvertices(msh))
    C = Coboundary{0,2}(topology(msh))

    for i in eachindex(vnorms)
        v = zero(vT)
        for j in C(i)
            v += fnorms[j]
        end
        vnorms[i] = Meshes.unormalize(v)
    end
    return vnorms
end

function vertex_normals_iterable(msh, fnorms)
    vT = eltype(fnorms)
    vnorms = similar(fnorms, nvertices(msh))
    S = Shared{FACE,VERTEX}(msh)

    for i in eachindex(vnorms)
        v = zero(vT)
        for j in S(i)
            v += fnorms[j]
        end
        vnorms[i] = Meshes.unormalize(v)
    end
    return vnorms
end

sphere = topoconvert(HalfEdgeTopology, simplexify(Sphere(Point(0,0,0), 1)))
fnorm = normal.(elements(sphere))
julia> @benchmark vertex_normals_master(sphere, fnorm)
BenchmarkTools.Trial: 2521 samples with 1 evaluation per sample.
 Range (min … max):  1.664 ms …  18.377 ms  ┊ GC (min … max): 0.00% … 85.70%
 Time  (median):     1.763 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.975 ms ± 912.216 μs  ┊ GC (mean ± σ):  7.22% ± 11.68%

  ██▅▃▃▂▂▁                                                    ▁
  █████████▇█▆▃▅▃▃▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▅▆▆▆▆▆▇▆ █
  1.66 ms      Histogram: log(frequency) by time      6.67 ms <

 Memory estimate: 1.89 MiB, allocs estimate: 61831.

julia> @benchmark vertex_normals_iterable(sphere, fnorm)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min … max):  169.017 μs …  6.926 ms  ┊ GC (min … max): 0.00% … 95.40%
 Time  (median):     181.195 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   191.566 μs ± 89.738 μs  ┊ GC (mean ± σ):  1.33% ±  3.32%

  ▂▆█▅▃▄▅▅▅▄▅▅▃▂▂▁▁▃▃▃▂▂▃▃▂▃▄▅▃▂▂▂▁▁▂▂▁     ▁▁ ▁▁▁▁▁▁▁         ▂
  █████████████████████████████████████▇▇▆█████████████▆▆▆▅▆▅▅ █
  169 μs        Histogram: log(frequency) by time       248 μs <

 Memory estimate: 59.88 KiB, allocs estimate: 3.

@juliohm
Copy link
Member

juliohm commented Aug 23, 2025

Thank you for sharing @halleysfifthinc. I believe the central idea of this PR could be polished further into an iterator interface for existing topological relations. My understanding is that you introduced Bounding and Shared to match Boundary and Coboundary relations and then used these new types to implement the iterator interface for the HalfEdgeTopology in the draft.

Will close the PR as we do not intend to introduce new types besides the topological relations. Please feel free to open a new PR if you feel that the iterator interface could be implemented with the topological relation types directly.

@juliohm juliohm closed this Aug 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants