/* 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: graph_from_incidence.cc,v 1.28 2005/12/09 16:39:22 thilosch Exp $"

#include <Poly.h>
#include <Graph.h>
#include <IncidenceMatrix.h>
#include <argv_options.h>

namespace polymake { namespace polytope {

namespace {

typedef Graph<nothing, Set<int>, undirected> graph;

template <typename IMatrix>
graph vertex_graph(const GenericIncidenceMatrix<IMatrix>& IM)
{
#if __GNUC__==3 && __GNUC_MINOR__<=1
   (void)IM.row(0);
#endif
   graph G(IM.rows());

   for (Entire< Nodes<graph> >::iterator n1=entire(nodes(G)); !n1.at_end(); ++n1) {
      for (Entire< Nodes<graph> >::iterator n2=n1+1; !n2.at_end(); ++n2) {
	 Set<int> common = IM.row(n1.index()) * IM.row(n2.index());
	 if (common.empty()) continue;

	 typename graph::out_edge_list::iterator edge=n1.out_edges().begin();
	 bool add=true;
	 while (!edge.at_end()) {
	    int inc=incl(*edge,common);
	    if (inc==2) {
	       ++edge;
	    } else {
	       if (inc<=0) n1.out_edges().erase(edge++);
	       if (inc>=0) { add=false; break; }
	    }
	 }
	 if (add) n1.out_edges().insert(n2.index(),common);
      }
   }
   return G;
}

} // end unnamed namespace

   void graph_from_incidence(Poly& p, bool primal, const char* incidence_section)
{
   const IncidenceMatrix<> VIF=p.give(incidence_section);
   const Graph<> G(primal ? vertex_graph(T(VIF)) : vertex_graph(VIF));
   p.take(primal ? "GRAPH" : "DUAL_GRAPH") << G;
}

} }

using namespace polymake;

/** @file graph_from_incidence
 *
 *  Compute the vertex or facet graph of a polyhedron.
 *
 *  $mode$ may be -primal or -dual
 *
 *  @synopsis graph_from_incidence <file> <incidence_section> <mode>
 *
 *  @reading  <incidence_section>
 *  @writing  in primal mode: GRAPH
 *            in dual mode:   DUAL_GRAPH
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "-primal" }, { "-dual" } };
   enum { primal, dual };
   if (argc != 4 ||
       !extract_options(argc,argv,1,options) ||
       bool(options[primal])==bool(options[dual])) {
      cerr << "usage: " << argv[0] << " <file> <incidence_section> { -primal | -dual }" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      polytope::graph_from_incidence(p, options[primal],argv[2]);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
