/* 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: bipyramid.cc,v 1.15 2004/06/22 16:23:10 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>

enum { no_coordinates, relabel };

namespace polymake { namespace polytope {

void bipyramid(Poly& p_out, Poly& p_in, const Rational& z, const Rational& z_prime, 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 / VIF) | sequence(0,n_facets) | sequence(n_facets,n_facets);

      p_out.take("N_VERTICES") << n_vertices+2;
      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) /
	                           (z0 | z_prime);
      p_out.take("VERTICES") << V_out;
   }
   if (options[relabel]) {
      std::vector<std::string> labels(n_vertices+2);
      read_labels(p_in, "VERTEX_LABELS", labels.begin(), n_vertices);
      labels[n_vertices]="Apex";
      labels[n_vertices+1]="Apex'";
      p_out.take("VERTEX_LABELS") << labels;
   }
}

} }

using namespace polymake;

/** @file bipyramid
 *
 *  Make a bipyramid over a polyhedron.
 *  The bipyramid is the convex hull of the input polyhedron $P$ and two points $v$, $v'$
 *  on both sides of the affine span of $P$. For bounded polyhedra, the projections
 *  of $v$ and $v'$ to the affine span of $P$ coincide with the vertex barycenter of $P$.
 *
 *  $z$ and $z'$ are distances between the vertex barycenter and $v$ and $v'$ respectively.
 *  Default values are $z=1$ and $z'=-z$.
 *
 *  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 vertices are labeled "Apex" and "Apex'".
 *
 *  @synopsis bipyramid <output_file> <input_file> [ <z> [ <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 : 5)) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ <z> [ <z'> ] | -noc ] [ -relabel ]" << endl;
      return 1;
   }
   try { 
      const Rational z= argc>3 ? Rational(argv[3]) : Rational(1);
      const Rational z_prime= argc>4 ? Rational(argv[4]) : -z;
      if (!options[no_coordinates] && (z==z_prime || !z || !z_prime)) {
	 cerr << argv[0] << ": z and z' must be different and non-zero" << endl;
	 return 1;
      }

      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc);
      polytope::bipyramid(p_out, p_in, z, z_prime, options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
