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

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

namespace polymake {
namespace topaz {

void crosscut_complex(Poly& p_in, Poly& p_out, const bool no_coord,
		      const std::string& comment)
{
   const Array< Set<int> > C = p_in.give("VERTICES_IN_FACETS");

   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << C;   

   if (!no_coord) {
      const Matrix<Rational> V = p_in.give("VERTICES");
      p_out.take("GEOMETRIC_REALIZATION") << V.minor(All, sequence(1,V.cols()-1));
   }
}

} }

using namespace polymake;

/** @file crosscut_complex
 *
 *  Produce the crosscut complex which arises out of a triangulation of the boundary of polytope.
 *
 *  If the @c -noc flag is set, no coordinates for the @see
 *  GEOMETRIC_REALIZATION will be written.
 *
 *  This client lives in between the applications poly and topaz.
 *  Since it produces a simplicial complex (from a polytope), it
 *  is counted as a client for the topaz application.
 *
 *  @synopsis crosscut_complex <out_file> <in_file> [ -noc ]
 *
 *  @reading  VERTICES_IN_FACETS, may read VERTICES
 *  @writing  DESCRIPTION, FACETS, may write GEOMETRIC_REALIZATION
 *
 *  @index clients Producing a simplicial complex from other objects
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ { "-noc" } };
   enum { no_coord };
   if (!extract_options(argc,argv,2,options) || argc != 3) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ -noc ]" << 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 << "Crosscut complex of " << argv[2] << ".";

      topaz::crosscut_complex(p_in, p_out, options[no_coord], comment.str());
   } 
   catch (const std::exception& e) {
     cerr << e.what() << endl;
     return 1;
   }
   return 0;
}


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