/* 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: lrs_ch_client.cc,v 1.12 2005/02/02 17:45:01 gawrilow Exp $"

#include <ch_client.h>
#include <lrs_interface.h>
#include <argv_options.h>

using namespace polymake;

/** @file lrs_ch_client
 *
 *  Convex hull calculation using @see external.lrslib
 *
 *  In primal mode it converts the V-representation to the H-representation,
 *  in dual mode vice versa.
 *
 *  If the option @c -count is specified, only counts the facets rsp. vertices without storing them in a matrix.
 *  @c -only-bounded in connection with @c -dual counts bounded vertices only.
 *
 *  @synopsis lrs_ch_client <file> { -primal | -dual } [ -count [ -only_bounded ]]
 *
 *  @reading  in primal mode: VERTICES | POINTS
 *	      in dual mode:   FACETS | INEQUALITIES, AFFINE_HULL | EQUATIONS
 *  @writing  in primal mode: FACETS, AFFINE_HULL or N_FACETS
 *	      in dual mode:   VERTICES or N_VERTICES or N_BOUNDED_VERTICES, POINTED, FEASIBLE
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "-primal" }, { "-dual" }, { "-count" }, { "-only-bounded" } };
   enum { primal, dual, count, only_bounded };
   if (!extract_options(argc,argv,1,options) || argc!=2 ||
       bool(options[primal])==bool(options[dual]) ||
       options[only_bounded] && (options[primal] || !options[count]) ) {
      cerr << "usage: " << argv[0] << " <file> { -primal | -dual } [ -count [ -only_bounded ]]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      stopwatch timer;
      polytope::lrs_interface::solver solver;

      if (options[primal]) {
	 if (options[count])
	    polytope::count_facets(p, solver, timer);
	 else
	    polytope::ch_primal(p, solver, timer);
      } else {
	 if (options[count])
	    polytope::count_vertices(p, solver, timer, options[only_bounded]);
	 else
	    polytope::ch_dual(p, solver, timer);
      }
#ifdef POLY_TIMING
      cout << "time: " << timer << " seconds" << endl;
#endif
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
