JFLib

physics/src/tree.rs#

Source: lib/physics/src/tree.rs

tree -- the dynamic AABB tree of a world's bodies: leaves with (fattened) boxes, inserted where the
growth of the surface is smallest, kept balanced by rotations on the way up (the scheme of Box2D's
b2DynamicTree, written anew).  A moving body keeps its leaf while its box stays inside the fattened
one.  Deterministic: the same sequence of inserts, moves and removes gives the same tree.
  • struct DynamicTree a dynamic AABB tree over items (u32)

impl DynamicTree

  • fn new() -> DynamicTree an empty tree
  • fn insert(&mut self, aabb: Aabb, item: u32) -> u32 a new leaf for item with box aabb -> its id
  • fn remove(&mut self, leaf: u32) remove a leaf
  • fn update(&mut self, leaf: u32, aabb: Aabb) give a leaf a new box (it keeps its id)
  • fn aabb(&self, leaf: u32) -> Aabb the box of a leaf
  • fn len(&self) -> usize the number of leaves
  • fn is_empty(&self) -> bool no leaves
  • fn height(&self) -> u32 the height of the root (0 for one leaf or none)
  • fn query(&self, bx: &Aabb, mut f: impl FnMut(u32)) call f(item) for every leaf whose box touches bx
  • fn ray(&self, o: Vec3, d: Vec3, ext: Vec3, mut tmax: f64, mut f: impl FnMut(u32, f64) -> f64) -> f64 the leaves whose box, grown by ext, the ray o + t d enters at t <= tmax, nearer subtrees first; f(item, tmax) returns the new tmax -> the last tmax
  • fn validate(&self) -> Result<(), &'static str> (tests) the tree is consistent: parents, heights, boxes holding their children, the leaf count -> an error text

Source: docs/API.md in the JFLib repository.