/* 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: canonical_coord.cc,v 1.7 2004/09/20 16:40:27 gawrilow Exp $"

#include <Poly.h>
#include <Rational.h>
#include <linalg.h>

namespace polymake { namespace polytope {

template <typename Coord>
void canonical_homogeneous(Poly& p, const char *in_section, const char *out_section)
{
   Matrix<Coord> x=p.give(in_section);
   canonicalize(entire(rows(x)));
   p.take(out_section) << x;
}

} }

using namespace polymake;

int main(int argc, const char *argv[])
{
   if (argc < 3 || argc > 4) {
      cerr << "usage: " << argv[0] << " <file> <section> [<out-section>]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      if (p.isa("RationalPolytope")) {
	 polytope::canonical_homogeneous<Rational>(p, argv[2], argv[argc-1]);
      } else if (p.isa("FloatPolytope")) {
	 p.lookup("EPSILON") >> conv<double,bool>::global_epsilon;
	 polytope::canonical_homogeneous<double>(p, argv[2], argv[argc-1]);
      } else {
	 throw std::runtime_error("don't know how to proceed with " + p.type());
      }
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
