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

#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>
#include <Vector.h>
#include <ListMatrix.h>
#include <vector>

namespace polymake { namespace polytope {

void conv(Poly& p_out, std::vector<Poly>& pp_in)
{
   Entire< std::vector<Poly> >::iterator p_in=entire(pp_in);
   ListMatrix< Vector<Rational> > Points=p_in->give("VERTICES | POINTS");
   while (! (++p_in).at_end()) {
      const Matrix<Rational> V=p_in->give("VERTICES | POINTS");
      if (V.cols() == Points.cols())
	 Points /= V;
      else
	 throw std::runtime_error("dimension mismatch");
   }
   p_out.take("POINTS") << Points;
}

} }

using namespace polymake;

/** @file conv
 *
 *  Construct a new polyhedron as the convex hull of given polyhedra.
 *
 *  @synopsis conv <output_file> <input_file_1> <input_file_2> [ ... ]
 *
 *  @reading  VERTICES | POINTS
 *  @writing  POINTS
 *
 *  @index clients Producing a new polyhedron from others
 */

int main(int argc, char *argv[])
{
   if (argc < 4) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file_1> <in_file_2> [ ... ]" << endl;
      return 1;
   }
   try {
      std::vector<Poly> pp_in(argc-2);
      for (int i=2; i<argc; ++i)
	 pp_in[i-2].init(argv[i], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc);
      polytope::conv(p_out, pp_in);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
