/* 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: orthantify.cc,v 1.31 2004/11/23 20:58:05 gawrilow Exp $"

#include <cstdlib>
#include <algorithm>
#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>
#include <IncidenceMatrix.h>
#include <linalg.h>
#include <transform.h>

namespace polymake { namespace polytope {

namespace {

template <typename Matrix1, typename Matrix2, typename E>
Matrix<E>
orth_transform (const GenericMatrix<Matrix1,E>& F,   // facets thru origin vertex
		const GenericMatrix<Matrix2,E>& EQ)  // equations
{
   const int d = F.cols();
   const Set<int> b=basis_rows(F);

   DIMENSION_CHECK(b.size() + EQ.rows() != d-1,
		   "orth_transform - wrong dual space dimension "
		   << b.size() << "+" << EQ.rows() << ", should be " << d-1);

   // Return the inverse of the (dual) transformation
   // which maps the facets and equations to the facets of the positive orthant.

   return T( unit_vector<E>(d,0) /     // far hyperplane is fixed
	     F.minor(b,All)      /
	     EQ );
}

} // end unnamed namespace

void orthantify(Poly& p_out, Poly& p_in, int origin, const std::string& comment)
{
   const Matrix<Rational> F=p_in.give("FACETS"),
                          AH=p_in.give("AFFINE_HULL");
   const IncidenceMatrix<> VIF=p_in.give("VERTICES_IN_FACETS");
   const Set<int> far_face=p_in.give("FAR_FACE");

   if (origin < 0) {		// origin vertex number not specified - take the first affine vertex
      origin= (sequence(0,VIF.cols())-far_face).front();
   } else {
      if (origin >= VIF.cols())
	 throw std::runtime_error("origin vertex number out of range");

      if (far_face.contains(origin))
	 throw std::runtime_error("specified origin vertex must be affine");
   }
   polytope::transform(p_out, p_in, comment, orth_transform(F.minor(VIF.col(origin),All), AH));
   p_out.take("POSITIVE") << comment << true;
}

} }

using namespace polymake;

/** @file orthantify
 *
 *  Make a polyhedron @see POSITIVE.
 *  Apply an affine transformation to a polyhedron such that a vertex is mapped
 *  to the origin $(1,0,...,0)$ and as many facets through this vertex as possible are
 *  mapped to the bounding facets of the first orthant.
 *
 *  $origin$ is the number of the vertex that will be moved to the origin
 *  $(1,0,...,0)$.  Default is the first affine vertex of the polyhedron.
 *
 *  @synopsis orthantify <output_file> <input_file> [ origin ]
 *
 *  @reading  FACETS, AFFINE_HULL, VERTICES_IN_FACETS, FAR_FACE
 *  @writing  VERTICES, FACETS, AFFINE_HULL, REVERSE_TRANSFORMATION, POSITIVE
 *
 *  @index clients Transforming a polyhedron
 */

int main(int argc, const char *argv[]) {
   if (argc < 3 || argc > 4) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ <origin> ]" << endl;
      return 1;
   }
   try {
      const int origin= argc==4 ? atoi(argv[3]) : -1;
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc);
      polytope::orthantify(p_out, p_in, origin, polytope::transform_comment("orthantify", argv[2]));
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
