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

#include <Poly.h>
#include <read_int_args.h>
#include <read_str_args.h>
#include <complex_tools.h>
#include <argv_options.h>

namespace polymake { namespace topaz {
  
void t_star(Poly& p_out, Poly& p_in, Set<int> F, std::list<std::string> labeled_F,
	    const bool no_labels, const std::string& comment)
{
   const Array< Set<int> > C = p_in.give("FACETS");
   const Array<std::string> L = p_in.give("VERTEX_LABELS");
   const int n_vert = L.size();
   
   if (F.empty())  // face spefified by labels
      F = transform_label_args(L, labeled_F);

   if (F.front()<0 || F.back()>n_vert-1)
      throw std::runtime_error("t_star: Specified vertex indices out of range");

   std::list< Set<int> > Star;
   std::copy(entire(star(C,F)), back_inserter(Star));

   if (Star.empty()) {
      std::ostringstream e;
      wrap(e) << "t_star: " << F << " does not specify a face.";
      throw std::runtime_error(e.str());
   }
   
   const Set<int> V = accumulate(Star, operations::add());
   adj_numbering(Star,V);
   
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << as_array(Star);
   
   if (!no_labels) {
      const Array<std::string> new_L(V.size(), select(L,V).begin());
      p_out.take("VERTEX_LABELS") << new_L;
   }
}
   
} }

using namespace polymake;

/** @file t_star
 *
 *  Produce the star of the face specified by 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.
 *
 *  @synopsis t_star <out_file> <in_file> { -v <v_1> <v_2>...<v_i> ... | -l <l_1> <l_2> ... } [ -nol ]
 *
 *  @reading  FACETS, VERTEX_LABELS
 *  @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" } };
   enum { no_labels };
   if ( !extract_options(argc,argv,4,options) ||
	(strcmp(argv[3], "-v") && strcmp(argv[3], "-l"))) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> { -v <v_1> <v_2>...<v_i> ... | -l <l_1> <l_2> ... } [ -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> F;
      std::list<std::string> labeled_F;
      if (argv[3][1]=='v')
	 F = topaz::read_int_args_as_set(argc, argv, 4);
      else
	 labeled_F = topaz::read_str_args(argc, argv, 4);
     
      std::ostringstream comment;
      comment << "Star of ";
      if (F.empty())  wrap(comment) << labeled_F;
      else            wrap(comment) << F;
      comment << " in " << argv[2] << ".";

      topaz::t_star(p_out, p_in, F, labeled_F, 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:
