Possible Redundant Update in b2DynamicTree?

Hi, I'm trying to learn through the AABB Dynamic Tree code in Box2D. Here's a question I have about the b2DynamicTree operation methods. Take the "b2DynamicTree::InsertLeaf()" method for an example (source doe: https://github.com/erincatto/Box2D/blob/20c406ae5c17887a3e7e38b95c8c4f1ff999ad3f/Box2D/Box2D/Collision/b2DynamicTree.cpp):

Code: Select all

void b2DynamicTree::InsertLeaf(int32 leaf)
{
   // ... awesome code
   
   while (index != b2_nullNode)
   {
      index = Balance(index);
      
      /**
       *  begin of#1
       *  is the code below redundant??
       */
      int32 child1 = m_nodes[index].child1;
      int32 child2 = m_nodes[index].child2;

      b2Assert(child1 != b2_nullNode);
      b2Assert(child2 != b2_nullNode);

      m_nodes[index].height = 1 + b2Max(m_nodes[child1].height, m_nodes[child2].height);
      m_nodes[index].aabb.Combine(m_nodes[child1].aabb, m_nodes[child2].aabb);
      /**
       *  end of #1
       */
      
      index = m_nodes[index].parent;
   }
}


I believe that "b2DynamicTree::Balance()" method itself should do the trick of the code snippet "#1". Therefore, it seems to me that #1 is somehow redundant. Could anyone please point out why the code #1 is still needed here? Thanks! 😀

P.S. I found another implementation of the dynamic AABB tree here (https://github.com/azrafe7/hxAABBTree/blob/master/src/ds/AABBTree.hx), which has the exact same behavior if you take a look at its "insertLeaf()" method. So apparently the code snippet #1 is necessary, but I just couldn't figure out why.

Original thread: https://box2d.org/forum/viewtopic.php?t=10208