/* 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: bound.cc,v 1.23 2004/06/22 16:23:10 gawrilow Exp $"

#include <Rational.h>
#include <Poly.h>
#include <SparseMatrix.h>
#include <transform.h>

namespace polymake { namespace polytope {

void bound(Poly& p_out, Poly& p_in, const std::string& comment)
{
   const bool is_positive=p_in.give("POSITIVE");
   if (!is_positive)
      throw std::runtime_error("polyhedron must be positive");

   const int d=p_in.give("AMBIENT_DIM");

   SparseMatrix<Rational> tau=unit_matrix<Rational>(d+1);
   tau.col(0).fill(1);
   polytope::transform(p_out, p_in, comment, tau);

   p_out.take("BOUNDED") << comment << true;
}

} }

using namespace polymake;

/** @file bound
 *
 *  Make a positive polyhedron bounded.
 *  Apply a projective linear transformation to a polyhedron mapping the far hyperplane
 *  to the hyperplane spanned by the points $(1,0,...,0,1,0,...)$.
 *  The origin $(1,0,...,0)$ is fixed.
 *
 *  The input polyhedron should be positive; i.e. no non-negative coordinates.
 *
 *  @synopsis bound <output_file> <input_file>
 *
 *  @reading  POSITIVE, AMBIENT_DIM, VERTICES, FACETS, AFFINE_HULL
 *  @writing  VERTICES, FACETS, AFFINE_HULL, REVERSE_TRANSFORMATION, BOUNDED
 *
 *  @index clients Transforming a polyhedron
 */

int main(int argc, const char *argv[]) {
   if (argc != 3) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file>" << endl;
      return 1;
   }
   try {
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc);
      polytope::bound(p_out, p_in, polytope::transform_comment("bound", argv[2]));
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
