/* Copyright (c) 1997-2006
   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: revert.cc,v 1.23 2006/01/17 19:34:37 gawrilow Exp $"

#include <Rational.h>
#include <IncidenceMatrix.h>
#include <transform.h>

namespace polymake { namespace polytope {

void revert(Poly& p_out, Poly& p_in, const std::string& comment)
{
   const Matrix<Rational> RT_v=p_in.give("REVERSE_TRANSFORMATION");
   const Matrix<Rational> RT_f = inv(T(RT_v));   // dual transformation

   transform_section(p_out, p_in, "VERTICES | POINTS", comment, RT_v);
   transform_section(p_out, p_in, "FACETS | INEQUALITIES", comment, RT_f);
   transform_section(p_out, p_in, "AFFINE_HULL | EQUATIONS", comment, RT_f);

   IncidenceMatrix<> VIF;
   if (p_in.lookup("VERTICES_IN_FACETS") >> VIF)
      p_out.take("VERTICES_IN_FACETS") << comment << VIF;

   transform_copy_labels(p_out, p_in, comment, "VERTEX_LABELS");
   transform_copy_labels(p_out, p_in, comment, "FACET_LABELS");
}

} }

using namespace polymake;

/** @file revert
 *
 *  Apply a @see REVERSE_TRANSFORMATION{reverse transformation}  to a given polyhedron.
 *  All transformation clients keep track of the polytope's history.
 *  They write or update the section @see REVERSE_TRANSFORMATION.
 *
 *  Applying revert to the transformed polytope re-constructs the original polyhedron.
 *
 *  @synopsis revert <output_file> <input_file>
 *
 *  @reading  VERTICES, FACETS, AFFINE_HULL, REVERSE_TRANSFORMATION
 *  @writing  VERTICES, FACETS, AFFINE_HULL
 *
 *  @index clients Transforming a polyhedron
 */

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