Skip to content

Commit 9946fdb

Browse files
Improve adjacency (substantially reduce number of adjacency discontinuities)
1 parent 52e1aa9 commit 9946fdb

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/topologies/halfedge.jl

+14-9
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,24 @@ function HalfEdgeTopology(elems::AbstractVector{<:Connectivity}; sort=true)
216216
end
217217

218218
function adjsortperm(elems::AbstractVector{<:Connectivity})
219+
# remaining list of elements to process
220+
oinds = collect(eachindex(elems)[2:end])
221+
219222
# initialize list of adjacent elements
220223
# with first element from original list
221-
einds = Int[1]
224+
einds = Int[]
222225
sizehint!(einds, length(elems))
223226

224-
# remaining list of elements to process
225-
oinds = collect(2:length(elems))
226-
227227
# `found` minimizes adjacency discontinuities. if `found == true` for the last edge in an
228228
# element, then we continue from that new element adjacent to that edge
229229
found = false
230+
lastfound = firstindex(elems)
230231

231232
# lookup all elements that share at least
232233
# two vertices (i.e., edge) with the last
233234
# adjacent element
234235
while !isempty(oinds)
235-
lelem = elems[last(einds)]
236+
lelem = elems[lastfound]
236237
vinds = indices(lelem)
237238
for v in vinds
238239
# vertices that are not `v`
@@ -245,23 +246,27 @@ function adjsortperm(elems::AbstractVector{<:Connectivity})
245246
vinds′ = indices(oelem)
246247
if any(==(v), vinds′) && !isdisjoint(v!, vinds′)
247248
found = true
248-
push!(einds, popat!(oinds, iter))
249+
push!(einds, lastfound)
250+
lastfound = popat!(oinds, iter)
249251
# don't increment j here because `popat!` just put the j+1 element at j
250252
# (avoids the need to reverse the array)
251253
else
252-
found = false
253254
iter += 1
254255
end
255256
end
256257
end
257258

258-
if !found && !isempty(oinds)
259+
if found
260+
found = false
261+
elseif !isempty(oinds)
259262
# we are done with this connected component
260263
# pop a new element from the original list
261-
push!(einds, popfirst!(oinds))
264+
push!(einds, lastfound)
265+
lastfound = popfirst!(oinds)
262266
found = false
263267
end
264268
end
269+
push!(einds, lastfound)
265270

266271
einds
267272
end

0 commit comments

Comments
 (0)