/* 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: extract_subcomplex.cc,v 1.8 2004/10/26 18:57:02 gawrilow Exp $"

#include <Poly.h>
#include <Array.h>
#include <Set.h>
#include <complex_tools.h>
#include <argv_options.h>
#include <Matrix.h>
#include <Rational.h>

#include <ext/hash_map>

namespace polymake {
namespace topaz {
   
void extract_subcomplex(Poly& p_out, Poly& p_in, const char* subcomplex_section,
			const bool geom_real, const bool no_labels,
			const std::string& comment)
{
   Array< Set<int> > C = p_in.give(subcomplex_section);
   
   const Set<int> V = accumulate(C, operations::add());
   adj_numbering(C,V);
   
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << C;
      
   if (!no_labels) {
      const Array<std::string> L = p_in.give("VERTEX_LABELS");
      const Array<std::string> new_L(V.size(), select(L,V).begin());
      p_out.take("VERTEX_LABELS") << new_L;
   }
   
   if (geom_real) {
      Matrix<Rational> Coord = p_in.give("GEOMETRIC_REALIZATION");
      p_out.take("GEOMETRIC_REALIZATION") << Coord.minor(V,All);
   }  
}
   
} }

using namespace polymake;

/** @file extract_subcomplex
 *
 *  Extracts a subcomplex of a given complex and creates a new complex.
 *  The indices of the selected vertices have to be stored as an ordered set
 *  in a separate file section @i subcomplex_section.
 *
 *  The vertex labels are preserved unless the @c -nol flag is specified.
 *
 *  The @c -geom_real flag tells the client to inherit the @see GEOMETRIC_REALIZATION.
 *
 *  @synopsis extract_subcomplex <out_file> <in_file> <subcomplex_section> [ -geom_real ] [ -nol ]
 *
 *  @reading  <subcomplex_section>, VERTEX_LABELS, may read GEOMETRIC_REALIZATION
 *  @writing  DESCRIPTION, FACETS, may write VERTEX_LABELS
 *
 *  @index clients Producing a new simplicial complex from others
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "-nol" }, { "-geom_real" } };
   enum { no_labels, geom_real };
   if (!extract_options(argc,argv,3,options) || argc!=4) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> <subcomplex_section> [ -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());
      
      std::ostringstream comment;
      comment << "Subcomplex " << argv[3] << " extracted from " << argv[2] << ".";
      
      topaz::extract_subcomplex(p_out, p_in, argv[3], options[geom_real], options[no_labels], comment.str()); 
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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