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

#include <cstdlib>
#include <argv_options.h>
#include <labels.h>
#include <vector>
#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>
#include <Vector.h>
#include <IncidenceMatrix.h>

namespace {
   enum { no_coordinates, relabel };
}

namespace polymake { namespace polytope {

void pyramid(Poly& p_out, Poly& p_in, const Rational& z,  const argv_option* options)
{
   int n_vertices=0;
   if (options[no_coordinates] || p_in.exists("VERTICES_IN_FACETS")) {
      const IncidenceMatrix<> VIF=p_in.give("VERTICES_IN_FACETS");
      n_vertices=VIF.cols();
      const int n_facets=VIF.rows();
      const IncidenceMatrix<> VIF_out= (VIF | sequence(0,n_facets))	// original vertices + the new top vertex
	                               / sequence(0, n_vertices);	// original polytope becomes the bottom facet

      p_out.take("N_VERTICES") << n_vertices+1;
      p_out.take("VERTICES_IN_FACETS") << VIF_out;
   }

   if (options[no_coordinates]) {
      if (p_in.exists("DIM")) {
	 const int dim=p_in.give("DIM");
	 p_out.take("DIM") << dim+1;
      }
   } else {
      const Matrix<Rational> V=p_in.give("VERTICES");
      const Vector<Rational> z0=p_in.give("REL_INT_POINT");
      n_vertices=V.rows();

      const Matrix<Rational> V_out=(V | zero_vector<Rational>(n_vertices)) /
	                           (z0 | z);
      p_out.take("VERTICES") << V_out;
   }

   if (options[relabel]) {
      std::vector<std::string> labels(n_vertices+1);
      read_labels(p_in, "VERTEX_LABELS", labels.begin(), n_vertices);
      labels[n_vertices]="Apex";
      p_out.take("VERTEX_LABELS") << labels;
   }
}

} }

using namespace polymake;

/** @file pyramid
 *
 *  Make a pyramid over a polyhedron.
 *  The pyramid is the convex hull of the input polyhedron $P$ and a point $v$
 *  outside the affine span of $P$. For bounded polyhedra, the projection of $v$
 *  to the affine span of $P$ coincides with the vertex barycenter of $P$.
 *
 *  $z$ is the distance between the vertex barycenter and $v$, default value is $1$.
 *
 *  If the option @c -noc (no coordinates) is set, then only combinatorial
 *  information is handled.
 *
 *  The option @c -relabel creates an additional section @see VERTEX_LABELS.
 *  The vertices from the original polytope keep their labels;
 *  the new top vertex is labeled "Apex".
 *
 *  @synopsis pyramid <output_file> <input_file> [ <z> | -noc ] [ -relabel ]
 *
 *  @reading  VERTICES, REL_INT_POINT or VERTICES_IN_FACETS, DIM or both
 *  @writing  VERTICES or VERTICES_IN_FACETS, N_VERTICES, DIM or both
 *
 *  @index clients Producing a new polyhedron from others
 */

int main(int argc, const char *argv[]) {
   argv_option options[]={ { "-noc" }, { "-relabel" } };
   if (!extract_options(argc,argv,2,options) ||
       argc > (options[no_coordinates] ? 3 : 4)) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ <z> | -noc ] [ -relabel ]" << endl;
      return 1;
   }
   try { 
      const Rational z= argc>3 ? Rational(argv[3]) : Rational(1);
      if (!options[no_coordinates] && !z) {
	 cerr << argv[0] << ": z must be non-zero" << endl;
	 return 1;
      }
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc);
      polytope::pyramid(p_out, p_in, z, options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
