-
Notifications
You must be signed in to change notification settings - Fork 4
Closed
Description
I've hacked together the first part of the Barnes-Hut approach, which is a method to accumulate leaf properties down the tree onto the nodes. I think that I've got it working, but my indexing is ugly.
- Is there a way to link the leaves to their parent node? My method below seems safe...
- I'm surely wasting time with all the calls to memory_index for the nodes. Should I be computing an offset instead?
using ImplicitBVH
using ImplicitBVH: BBox, BSphere
# Generate some simple bounding spheres
bounding_spheres = [
BSphere{Float32}([1., 0., 0.], 1.),
BSphere{Float32}([2., 0., 0.], 1.),
BSphere{Float32}([3., 0., 0.], 1.),
BSphere{Float32}([1., 1., 1.], 1.),
BSphere{Float32}([2., 1., 1.], 1.),
BSphere{Float32}([3., 1., 1.], 1.),
BSphere{Float32}([1., 1., 2.], 1.),
BSphere{Float32}([2., 1., 2.], 1.),
BSphere{Float32}([3., 1., 2.], 1.),
]
# Build BVH
bvh = BVH(bounding_spheres)
# Accumulate on the nodes
using ImplicitBVH: level_indices,memory_index,pow2
function accumulate_up!(node_values, leaf_values, bvh)
tree = bvh.tree; levels = tree.levels
# leaf level
leaf = 0
for i in range(level_indices(tree,levels-1)...)
@inbounds node_values[i] = leaf_values[bvh.leaves[leaf+=1].index]
leaf==length(leaf_values) && break
@inbounds node_values[i] += leaf_values[bvh.leaves[leaf+=1].index]
end
# nodes levels
for level in levels-2:-1:1
for i in pow2(level-1):pow2(level)-1-bvh.skips[level]
@inbounds node_values[memory_index(tree,i)] = node_values[memory_index(tree,2i)]
ImplicitBVH.unsafe_isvirtual(tree,2i+1) && break
@inbounds node_values[memory_index(tree,i)] += node_values[memory_index(tree,2i+1)]
end
end
node_values
end
# Test it
leafmass = Float32.(1:length(bounding_spheres))
nodemass = zeros(Float32,length(bvh.nodes))
accumulate!(nodemass,leafmass,bvh)Metadata
Metadata
Assignees
Labels
No labels