// Cutmesh - Copyright (C) 2010-2018 T. Mouton, E. Bechet
//
// See the LICENSE file for license information and contributions.
// bugs and problems to <thibaud.mouton@gmail.com>.

#include "LVertex.h"

void LVertex::computeRootParents()
{
  std::queue<LVertex*> stack;
  std::set<LVertex*> parents;

  clearRootParent();

  if ( getParent ( 0 ) != NULL )
  {
    stack.push ( getParent ( 0 ) );
    stack.push ( getParent ( 1 ) );
  }
  while ( !stack.empty() )
  {
    LVertex *sv = stack.front();
    if ( !sv->getRootParentSize() )
    {
      if ( sv->getParent ( 0 ) != NULL )
      {
        stack.push ( sv->getParent ( 0 ) );
        stack.push ( sv->getParent ( 1 ) );
      }
      else
      {
        parents.insert ( sv );
      }
    }
    else
    {
      for ( int j = 0; j < sv->getRootParentSize(); j++ )
        parents.insert ( sv->getRootParent ( j ) );
    }
    stack.pop();
  }
  for ( std::set<LVertex*>::iterator it = parents.begin(); it != parents.end(); it++ )
    addRootParent ( *it );
}
