/* 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: cdd_redund_client.cc,v 1.3 2004/11/23 20:58:05 gawrilow Exp $"

#include <Poly.h>
#include <Rational.h>
#define GMPRATIONAL
#include <cdd_interface.h>
#include <argv_options.h>

namespace {
  enum { fromvertices };
}

namespace polymake { namespace polytope {

void cdd_redundant_points(Poly& p, const argv_option* options)
{
   cdd_interface::solver<Rational> solver;
   const Matrix<Rational> P=p.give(options[fromvertices] ? "VERTICES" : "POINTS");
   const cdd_interface::solver<Rational>::non_redundant non_red=solver.find_vertices_among_points(P);
   if (!options[fromvertices])
      p.take("VERTICES") << P.minor(non_red.first,All);
   p.take("VERTEX_NORMALS") << non_red.second;
}

} }

using namespace polymake;

/** @file cdd_redund_client
 *
 *  Redundant point elimination using @see external.cddlib
 *
 *  @synopsis cdd_redund_client <file> [ -fromvertices ]
 *
 *  @reading  POINTS or VERTICES
 *  @writing  VERTICES, VERTEX_NORMALS
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "-fromvertices" } };
   if (!extract_options(argc,argv,1,options) || argc > 2) {
      cerr << "usage: " << argv[0] << " <file> [ -fromvertices ]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      polytope::cdd_redundant_points(p,options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
