/* 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: vertex_barycenter.cc,v 1.23 2004/09/20 16:12:54 gawrilow Exp $"

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

namespace polymake { namespace polytope {

template <typename Coord>
void vertex_barycenter(Poly& p)
{
   const Matrix<Coord> V=p.give("VERTICES");
   Vector<Coord> Vbc=average(rows(V));
   p.take("VERTEX_BARYCENTER") << Vbc;
}

} }

using namespace polymake;

/** @file vertex_barycenter
 *
 *  Compute the vertex barycenter of a polytope.
 *  Polytope must be bounded.
 *
 *  @synopsis vertex_barycenter <file>
 *
 *  @reading  VERTICES
 *  @writing  VERTEX_BARYCENTER
 */

int main(int argc, char *argv[])
{
   if (argc!=2) {
      cerr << "usage: " << argv[0] << " <file>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      if (p.isa("RationalPolytope")) {
	 polytope::vertex_barycenter<Rational>(p);
      } else if (p.isa("FloatPolytope")) {
	 polytope::vertex_barycenter<double>(p);
      } 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;
}
