/* Copyright (c) 1997-2010
   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: bounded_complex.cc 9831 2010-10-30 18:47:05Z gawrilow $
*/

#include <polymake.h>
#include <Array.h>
#include <Set.h>
#include <face_lattice_tools.h>
#include <HasseDiagram.h>
#include <FacetList.h>
#include <list>

namespace polymake { namespace polytope {

// FIXME: the return value should be a new type PolyhedralComplex

IncidenceMatrix<>
bounded_complex_from_incidence(const IncidenceMatrix<>& VIF, const Set<int>& far_face, const int upper_bound)
{
   HasseDiagram HD;
   face_lattice::compute_bounded(VIF, far_face, filler(HD), upper_bound);
   IncidenceMatrix<> BC(HD.max_faces(), VIF.cols());
   BC.squeeze_cols();
   return BC;
}

FacetList
bounded_complex_from_face_lattice(perl::Object HD_obj, const Set<int>& far_face)
{
   const HasseDiagram HD(HD_obj);
   FacetList F(HD.node_range_of_dim(0).size());

   Set<int> faces_to_visit;
   std::list<int> Q;
   copy(entire(HD.node_range_of_dim(-1)), std::back_inserter(Q));

   while (!Q.empty()) {
      const int f=Q.front(); Q.pop_front();
      if ((HD.face(f) * far_face).empty()) {
	 F.insertMax(HD.face(f));
      } else {
	 for (HasseDiagram::graph_type::in_adjacent_node_list::const_iterator subf=HD.in_adjacent_nodes(f).begin();
	      !subf.at_end(); ++subf)
	    if (faces_to_visit.collect(*subf))
	       Q.push_back(*subf);
      }
   }

   F.squeeze();
   return F;
}

IncidenceMatrix<>
bounded_complex_from_bounded_face_lattice(perl::Object HD_obj)
{
   const HasseDiagram HD(HD_obj);
   IncidenceMatrix<> BC(HD.max_faces());
   BC.squeeze_cols();
   return BC;
}

Function4perl(&bounded_complex_from_incidence, "bounded_complex(IncidenceMatrix Set; $=-1)");
Function4perl(&bounded_complex_from_face_lattice, "bounded_complex(FaceLattice Set)");
Function4perl(&bounded_complex_from_bounded_face_lattice, "bounded_complex(FaceLattice)");

} }
