/* Copyright (c) 1997-2009
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: faces_to_facets.cc 9298 2009-09-02 23:10:56Z gawrilow $
*/

#include <polymake.h>
#include <Array.h>
#include <FacetList.h>
#include <Set.h>

namespace polymake { namespace topaz {

typedef Array< std::list<int> > face_list;

void faces_to_facets(perl::Object p, const face_list& F_in)
{
   FacetList F;
   Set<int> V;

   for (Entire<face_list>::const_iterator f_it=entire(F_in); !f_it.at_end(); ++f_it) {
      // provide vertex ordering
      Set<int> facet;
      accumulate_in(entire(*f_it), operations::add(), facet);

      // add facet to vertex set
      V+=facet;
      F.insertMax(facet);
   }

   // checking numbering
   const bool renumber= !V.empty() && (V.front()!=0 || V.back()+1!=V.size());

   if (renumber) F.squeeze();
   if (F.empty()) {
      p.take("FACETS") << Array< Set<int> >(1, V);
   } else {
      p.take("FACETS") << F;
   }

   p.take("VERTEX_LABELS") << V;
   p.take("N_VERTICES") << V.size();
}

   Function4perl(&faces_to_facets, "faces_to_facets(SimplicialComplex $)");
} }


// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
