/* Copyright (c) 1997-2006
   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: poly2surf.cc,v 1.2 2006/02/15 11:41:34 thilosch Exp $"

#include <Poly.h>
#include <Matrix.h>
#include <Rational.h>
#include <IncidenceMatrix.h>

namespace polymake { namespace surface {

void poly2surf(Poly& p_out, Poly& p_in) {
   const bool bounded = p_in.give("BOUNDED");
   const int dim = p_in.give("DIM");
   if(!bounded || dim != 3) {
      throw std::runtime_error("input polyhedron must bounded and three dimensional");
   }

   const IncidenceMatrix<> VIF = p_in.give("VERTICES_IN_FACETS");
   Matrix<Rational> V = p_in.give("VERTICES");

   for(Entire< Rows< Matrix< Rational > > >::iterator ri = entire(rows(V)); !ri.at_end(); ++ri) {
      ri->dehomogenize();
   }

   p_out.take("FACETS") << VIF;
   p_out.take("GEOMETRIC_REALIZATION") << V.minor(All,sequence(1,V.cols()-1));
}
   
} }

using namespace polymake;

/** @file poly2surf
 *
 *  Convert a three dimensional polytope to a polyhedral surface (sphere).
 *
 *  @synopsis poly2surf <out_file> <in_file>
 *
 *  @reading  VERTICES_IN_FACETS, VERTICES
 *  @writing  FACETS, GEOMETRIC_REALIZATION
 *
 *  @index clients Producing a polyhedral surface from a polytope
 */

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


// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
