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

#include <cmath>
#include <Poly.h>
#include <Matrix.h>
#include <linalg.h>

namespace polymake { namespace polytope {

void spherize(Poly& p_out, Poly& p_in)
{
   const bool bounded=p_in.give("BOUNDED"),
              centered=p_in.give("CENTERED");
   if (!bounded || !centered)
      throw std::runtime_error("polytope must be bounded and centered");

   const Matrix<double> V=p_in.give("VERTICES | POINTS");
   const Matrix<double> points= ones_vector<double>(V.rows()) | normalized(V.minor(All,range(1,V.cols()-1)));

   p_out.take("POINTS") << std::setprecision(6) << points;
   p_out.take("BOUNDED") << true;
   p_out.take("CENTERED") << true;
}

} }

using namespace polymake;

/** @file spherize
 *
 *  Project all vertices of a polyhedron on the unit sphere.
 *  Input polyhedron must be centered.
 *
 *  @synopsis spherize <output_file> <input_file>
 *
 *  @reading  VERTICES | POINTS
 *  @writing  POINTS
 *
 *  @index clients Producing a new polyhedron from others
 */

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