/* Copyright (c) 1997-2010
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.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.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: edge_directions.cc 9991 2010-12-12 23:36:28Z gawrilow $
*/

#include "polymake/client.h"
#include "polymake/Rational.h"
#include "polymake/Graph.h"
#include "polymake/Vector.h"
#include "polymake/Matrix.h"

namespace polymake { namespace polytope {

template <typename _Matrix>
EdgeMap<Undirected, Vector<typename _Matrix::element_type> >
edge_directions(perl::Object g, const GenericMatrix<_Matrix>& vertices)
{
   const Graph<> G = g.give("ADJACENCY");
   EdgeMap<Undirected, Vector<typename _Matrix::element_type> > directions(G);

   for (Entire< Edges< Graph<> > >::const_iterator e=entire(edges(G));  !e.at_end();  ++e) {
      const int n_from=e.from_node(), n_to=e.to_node();
      directions[*e]=vertices[n_to]-vertices[n_from];
   }
   return directions;
}

template <typename _Matrix>
EdgeMap<Undirected, Vector<typename _Matrix::element_type> >
edge_directions(perl::Object g, const GenericMatrix<_Matrix>& vertices, const Set<int>& rays)
{
   const Graph<> G = g.give("ADJACENCY");
   EdgeMap<Undirected, Vector<typename _Matrix::element_type> > directions(G);

   for (Entire< Edges< Graph<> > >::const_iterator e=entire(edges(G));  !e.at_end();  ++e) {
      const int n_from=e.from_node(), n_to=e.to_node();
      if (rays.contains(n_from)) {
         if (rays.contains(n_to))
            directions[*e]=zero_vector<typename _Matrix::element_type>(vertices.cols());
         else
            directions[*e]=vertices[n_from];
      } else {
         if (rays.contains(n_to))
            directions[*e]=vertices[n_to];
         else
            directions[*e]=vertices[n_to]-vertices[n_from];
      }
   }
   return directions;
}

FunctionTemplate4perl("edge_directions(Graph Matrix Set)");
FunctionTemplate4perl("edge_directions(Graph Matrix)");

} }

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
