/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.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.
*/

#ident "$Project: polymake $$Id: induced_subcomplex.cc,v 1.25 2004/10/26 18:57:01 gawrilow Exp $"

#include <Poly.h>
#include <read_int_args.h>
#include <read_str_args.h>
#include <complex_tools.h>
#include <FacetList.h>
#include <argv_options.h>
#include <Matrix.h>
#include <Rational.h>

#include <ext/hash_map>

namespace {
   enum { no_labels, geom_real };
}

namespace polymake {
namespace topaz {

void induced_subcomplex(Poly& p_out, Poly& p_in, const std::list<std::string> L_in, Set<int> V_in,
			const std::string& comment, const argv_option *options)
{
   const Array< Set<int> > C = p_in.give("FACETS");
   Array<std::string> L = p_in.give("VERTEX_LABELS");
   const int n_vert = L.size();
      
   // creating vertex map
   std_ext::hash_map<std::string,int> map(L.size());
   int count = 0;
   for (Entire< Array<std::string> >::const_iterator l=entire(L);
	!l.at_end(); ++l, ++count)
      map[*l] = count;
 
   // checking input
   if (!L_in.empty())
      for (Entire< std::list<std::string> >::const_iterator l=entire(L_in);
	   !l.at_end(); ++l) {
	 if (map.find(*l) == map.end()) {
	    std::ostringstream e;
	    e << "induced_subcomplex: Input label " << *l << " is not a vertex.";
	    throw std::runtime_error(e.str());
	 }
	 
	 V_in += map[*l];
      }
   
   else
      if (V_in.front()<0 || V_in.back()>n_vert-1)
	 throw std::runtime_error("induced_subcomplex: Specified vertices are not contained in VERTICES.");
   
   // computing subcomplex
   FacetList sub(n_vert);
   for (Entire< Array< Set<int> > >::const_iterator it=entire(C);
	!it.at_end(); ++it)
      sub.replaceMax(V_in*(*it));
   
   // adjust numbering
   sub.squeeze();
   
   p_out.take("DESCRIPTION") << comment;  
   p_out.take("FACETS") << sub;

   if (!options[no_labels]) {
      Array<std::string> new_L(V_in.size());
      Array<std::string>::iterator l = new_L.begin();
      for (Entire< Set<int> >::const_iterator v=entire(V_in);
	   !v.at_end(); ++v, ++l)
	 *l = L[*v];
      
      p_out.take("VERTEX_LABELS") << new_L;
   }
   
   if (options[geom_real]) {
      const Matrix<Rational> Coord = p_in.give("GEOMETRIC_REALIZATION");
      p_out.take("GEOMETRIC_REALIZATION") << Coord.minor(V_in,All);
   }
}

} }
   
using namespace polymake;

/** @file induced_subcomplex
 *
 *  Produce the subcomplex consisting of all faces which are contained in the given vertices.
 *
 *  Vertices can be specified by their indices (using the @c -v option)
 *  or by their labels (using the @c -l option).
 *  Indices may be specified individually or as a sequence.
 *  e.g. {1 7...9 20} = {1 7 8 9 20}. (perl lovers might use just two points
 *  to indicate a sequence.)
 *
 *  The @c -nol flag tells the client not to write any labels.
 *
 *  The @c -geom_real flag tells the client to inherit the @see GEOMETRIC_REALIZATION.
 *
 *  @synopsis induced_subcomplex <out_file> <in_file> { -v <v_1> <v_2>...<v_i> ... | -l <l_1> <l_2> ... } [ -geom_real ] [ -nol ]
 *
 *  @reading  FACETS, VERTEX_LABELS
 *  @writing  DESCRIPTION, FACETS, may write VERTEX_LABELS, may read GEOMETRIC_REALIZATION
 *
 *  @index clients Producing a new simplicial complex from others
 */


int main(int argc, const char* argv[])
{
   argv_option options[]={ { "-nol" }, { "-geom_real" }  };
   if ( !extract_options(argc,argv,4,options) || argc < 3 ) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> { -v <v_1> <v_2>...<v_i> ... | -l <l_1> <l_2> ... } [ -geom_real ] [ -nol ]" << endl;
      return 1;
   }
   try {
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc, p_in.type());
      
      // reading vertices
      Set<int> V_in;
      std::list<std::string> L_in;
      
      if ( !strcmp(argv[3],"-v") )
	 V_in = topaz::read_int_args_as_set(argc, argv, 4);
      
      if ( !strcmp(argv[3],"-l") )
	 L_in = topaz::read_str_args(argc, argv, 4);
      
      std::ostringstream comment;
      comment << "Subcomplex of " << argv[2] << " induced by the vertices ";
      if (L_in.empty())  wrap(comment) << V_in;
      else               wrap(comment) << "labeled " << L_in;
      comment << ".";
      
      topaz::induced_subcomplex(p_out, p_in, L_in, V_in, comment.str(), options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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