/* 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: framework.cc,v 1.1 2005/11/10 10:29:11 thilosch Exp $"

#include <Poly.h>
#include <Vector.h>
#include <Matrix.h>
#include <Rational.h>
#include <Graph.h>

namespace polymake { namespace polytope {
  namespace {
     typedef Graph< Vector< Rational > > Framework;
  }
   
   void framework(Poly& p_out, Poly& p_in, const char* graph_section, const char* coord_section) {
     Framework F;
     p_in.give(graph_section) >> ignore_node_attrs(F);
     Matrix<Rational> P = p_in.give(coord_section);
     
     Entire< Nodes< Framework > >::iterator ni = entire(nodes(F));
     for (Entire< Rows< Matrix<Rational> > >::iterator p_i=entire(rows(P));  !p_i.at_end(); ++p_i, ++ni) {
	*ni = (p_i->dehomogenize()).slice(1);
     }

     p_out.take("FRAMEWORK") << F;
  }
} }

using namespace polymake;

/** @file framework
 *
 *  Create a @see FRAMEWORK by combining a graph with coordinates given in @c <graph_section> and @c <coordinate_section>.
 *  The @c <coordinate_section> is given in homogeneous coordinates.
 *
 *  @synopsis framework <out_file> <in_file> <graph_section> <coordinate_section>
 *
 *  @reading  <graph_section>, <coordinate_section>
 *  @writing  FRAMEWORK
 */

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