/* 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: triang_sign.cc,v 1.6 2004/06/22 16:23:08 gawrilow Exp $"

#include <Rational.h>
#include <Matrix.h>
#include <Array.h>
#include <Vector.h>
#include <Set.h>
#include <Poly.h>
#include <linalg.h>

namespace polymake { namespace polytope {

void triang_sign(Poly& p, const char *triang_section, const char *vertex_section, const char *result_section)
{
   const Array< Set<int> > triang=p.give(triang_section);
   const Matrix<Rational> V=p.give(vertex_section);

   Vector<int> signs(triang.size());
   Vector<int>::iterator s=signs.begin();
   for (Entire< Array< Set<int> > >::const_iterator t=entire(triang);  !t.at_end();  ++t, ++s)
      *s = sign(det(V.minor(*t,All)));

   p.take(result_section) << signs;
}

} }

using namespace polymake;

/** @file triang_sign
 *
 *  For a given triangulation of a polytope, compute the orientation of the simplices.
 *  The output section contains signs of determinants of simplices.
 *
 *  @synopsis triang_sign <file> <triangulation_section> <vertex_section> <sign_section>
 *
 *  @reading  <triangulation_section>, <vertex_section>
 *  @writing  <sign_section>
 */

int main(int argc, const char *argv[]) {
   if (argc != 5) {
      cerr << "usage: " << argv[0] << " <file> <triangulation_section> <vertex_section> <sign_section>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      polytope::triang_sign(p, argv[2], argv[3], argv[4]);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
