So further testing has shown my previous algorithm does not always attach the two lightest objects.
I’ve come up with this algorithm, which does always attach the two lightest, resulting in the minimal possible amount of updates.
Unfortunately, due to its added complexity it is a bit slower than my previous algorithm and just about the same speed as cluster attach. Perhaps I am overlooking a place where it can be optimized further.
@Denis: Out of curiosity… by how much, in percentage, is your algorithm faster than cluster attach?
fn customAttach source nodes pb:pb1 =
(
insertitem source nodes 1
local count = nodes.count
local att = polyop.attach
local vertArr = for o in nodes collect o.numVerts
local sortedVertArr = deepcopy vertArr
for i = 1 to nodes.count-1 do if not keyboard.escpressed do
(
pb.value = 100 - nodes.count*100./count
sort sortedVertArr
local a = findItem vertArr sortedVertArr[1]
vertArr[a]=-1
local b = findItem vertArr sortedVertArr[2]
local nA = nodes[a]
att nA nodes[b]
sortedVertArr[1]=vertArr[a]=nA.numVerts
deleteItem vertArr b
deleteItem sortedVertArr 2
deleteItem nodes b
verts += nA.numverts
)
nodes[1]
)
Create. Time: 1.181 Memory: 1240104L First: p0 Last: p999 Random Range Dist.
#cluster: with undo:on Time:2.172 Memory:72192L Updates:5567158
#this algorithm: with undo:on Time:2.14 Memory:252192L Updates:5256184
#prev. algorithm: with undo:on Time:2.099 Memory:72128L Updates:5297612