/* 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: surf2top.cc,v 1.1 2006/02/20 17:30:42 witte Exp $"

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

#include <list>


namespace polymake {
namespace topaz {

void surf2top(Poly& p_in, Poly& p_out, const bool no_coord, const bool no_labels,
	      const std::string& comment)
{
   const Array< std::list<int> > F = p_in.give("FACETS_CYCLIC");
   std::list< Set<int> > C;

   for (Entire< Array< std::list<int> > >::const_iterator f=entire(F); !f.at_end(); ++f) {
      std::list<int>::const_iterator v_from = f->begin();
      const int v0 = *v_from; 
      ++v_from;
      std::list<int>::const_iterator v_to = v_from;
      ++v_to;
      Set<int> face;
      face += v0;  face += *v_from;  face += *v_to; 
      C.push_back(face);

      ++v_to;
      while(v_to != f->end()) {
	 face -= *v_from;
	 face += *v_to;
	 C.push_back(face);
	 ++v_from;
	 ++v_to;
      }
   }
   
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << as_array(C);   
   
   if (!no_coord) {
      const Matrix<Rational> V = p_in.give("GEOMETRIC_REALIZATION");
      p_out.take("GEOMETRIC_REALIZATION") << V;
   }
   
   if (!no_labels) {
      const Array<std::string> L = p_in.give("VERTEX_LABELS");
      p_out.take("VERTEX_LABELS") << L;
   }
}

} }

using namespace polymake;

/** @file surf2top
 *
 *  Produce a simplicial complex from a surface by triangulating each n-gon.
 *
 *  If the @c -noc flag is set, no coordinates for the @see
 *  GEOMETRIC_REALIZATION will be written.
 *
 *  This client lives in between the applications surface and topaz.
 *  Since it produces a simplicial complex (from a surface), it
 *  is counted as a client for the topaz application.
 *
 *  @synopsis surf2top <out_file> <in_file> [ -noc ] [ -nol ]
 *
 *  @reading  FACETS_CYCLIC, may read GEOMETRIC_REALIZATION, may read VERTEX_LABELS
 *  @writing  DESCRIPTION, FACETS, may write GEOMETRIC_REALIZATION, may write VERTEX_LABELS
 *
 *  @index clients Producing a simplicial complex from other objects
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ { "-noc" }, { "-nol" } };
   enum { no_coord, no_labels };
   if (!extract_options(argc,argv,2,options) || argc != 3) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ -noc ] [ -nol ]" << endl;
      return 1;
   }
   try {
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc, "topaz::default");

      std::ostringstream comment;
      comment << "Triangulated surface " << argv[2] << ".";

      topaz::surf2top(p_in, p_out, options[no_coord], 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:
