/* 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: projective_transformation.cc,v 1.4 2004/07/22 22:50:57 gawrilow Exp $"

#include <argv_options.h>
#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>
#include <Array.h>
#include <linalg.h>

namespace polymake { namespace polytope {

void projective_transformation(Poly& p, const char *out_section, const char *in_section,
			       const Array<std::string>& trans_sections, bool dual)
{
   const Matrix<Rational> M=p.give(in_section);
   Entire< Array<std::string> >::const_iterator t=entire(trans_sections);
   Matrix<Rational> tau=p.give(t->c_str()); // transformation

   while (! (++t).at_end()) {
      Matrix<Rational> next=p.give(t->c_str());
      tau=tau * next;
   }
   const Matrix<Rational> Result= dual ? Matrix<Rational>( T(inv(tau)) * M )
                                       : Matrix<Rational>( M * tau );
   p.take(out_section) << Result;
}

} }

using namespace polymake;

/** @file projective_transformation
 *
 *  Apply a projective linear transformation to a section.
 *
 *  @synopsis projective_transformation <file> <output_section> <input_section> <transformation_section> [-dual] 
 *
 *  @reading  <input_section> <transformation_section>
 *  @writing  <output_section>
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "dual" } };
   enum { dual };
   if (!extract_options(argc,argv,4,options)) {
      cerr << "usage: " << argv[0] << " <file> <output_section> <input_section> <transformation_section> ... [-dual]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      Array<std::string> trans_sections(argc-4, ptr2container(argv+4).begin());
      polytope::projective_transformation(p, argv[2], argv[3], trans_sections, options[dual]);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
