/* 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: beneath_beyond.cc,v 1.12 2005/02/02 17:45:01 gawrilow Exp $"

#include <Poly.h>
#include <Rational.h>
#include <beneath_beyond.h>
#include <stopwatch.h>

namespace polymake { namespace polytope {

void beneath_beyond(Poly& p, const char* points_section, const char* permutation_section, stopwatch& timer)
{
   const bool already_VERTICES=!strcmp(points_section, "VERTICES");
   const Matrix<Rational> P=p.give(points_section);
   Array<int> permutation;
   if (permutation_section) p.give(permutation_section) >> permutation;

   timer.start();
   beneath_beyond_algo<Rational> algo(P, already_VERTICES);
   if (permutation.empty()) {
      algo.compute(entire(sequence(0,P.rows())));
   } else {
      algo.compute(entire(permutation));
   }

   const Matrix<Rational> F=algo.getFacets();
   const Matrix<Rational> AH=algo.getAffineHull();
   const IncidenceMatrix<> VIF=algo.getVertexFacetIncidence();
   const Graph<> DG=algo.getDualGraph();
   const Array< Set<int> > TR=algo.getTriangulation();

   timer.stop();

   p.take("FACETS") << F;
   p.take("AFFINE_HULL") << AH;
   p.take("VERTICES_IN_FACETS") << VIF;
   p.take("DUAL_GRAPH") << DG;

   if (already_VERTICES) {
      p.take("TRIANGULATION") << TR;
      p.take("ESSENTIALLY_GENERIC") << algo.getGenericPosition();
   } else {
      const Matrix<Rational> V=algo.getVertices();
      p.take("VERTICES") << V;
	 p.take("TRIANGULATION_INT") << TR;
   }
}

} }

using namespace polymake;

/** @file beneath_beyond
 *
 *  Compute the convex hull and the triangulation of the given point set
 *  using the beneath-beyond algorithm.
 *
 *  The triangulation computed will be a placing one according to the order of the points
 *  in the @see Polytope.VERTICES or @see Polytope.POINTS section. If you need an alternative order,
 *  you can store it in some section (it should be a valid permutation of the integer
 *  range 0 .. @see Polytope.N_VERTICES-1) and specify its name as an additional command-line parameter.
 *
 *  @synopsis beneath_beyond <file> <points_section> [ <vertex_permutation_section> ]
 *
 *  @reading  <points_section> (VERTICES or POINTS), <vertex_permutation_section>
 *  @writing  FACETS, AFFINE_HULL, VERTICES_IN_FACETS, DUAL_GRAPH, TRIANGULATION or TRIANGULATION_INT,
 *            ESSENTIALLY_GENERIC
 */

int main(int argc, const char *argv[]) {
   if (argc<3 || argc>4) {
      cerr << "usage: " << argv[0] << " <file> <points_section> [ <vertex_permutation_section> ]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      stopwatch timer;
      polytope::beneath_beyond(p, argv[2], argc==4 ? argv[3] : 0, timer);
#ifdef POLY_TIMING
      cout << "time: " << timer << " seconds" << endl;
#endif
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
