/* 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: projectivities.cc,v 1.8 2005/04/05 09:11:50 witte Exp $"

#include <Poly.h>
#include <Graph.h>
#include <PowerSet.h>
#include <EquivalenceRelation.h>
#include <Bitset.h>

#include <list>

namespace polymake { namespace topaz {
  
void projectivities(Poly& p, const char* proj_orbits_section, const char* proj_dictionary_section)
{
   typedef Graph<> graph;
   const graph DG = p.give("DUAL_GRAPH");
   const Array< Set<int> > F = p.give("FACETS");
   const int n_vert = p.give("N_VERTICES");
   const int n_facets = F.size();
   const bool dual_con = p.give("DUAL_CONNECTED");
   if (!dual_con)
      throw std::runtime_error("projectivities: Complex is not DUAL_CONNECTED");

   EquivalenceRelation color_classes(n_vert,F[0]);  // vertices of first facets as representatives
   
   // BFS to determin color classes
   Bitset visited(n_facets);
   std::list<int> node_queue;
   node_queue.push_back(0);
   visited+=0;
   
   while (!node_queue.empty()) {
      const int n=node_queue.front();
      node_queue.pop_front(); 
      
      for (Entire<graph::out_edge_list>::const_iterator e=entire(DG.out_edges(n));
	   !e.at_end(); ++e) {
	 const int nn = e.to_node();
	 
	 if ( !visited.contains(nn) ) {
	    visited+=nn;
	    node_queue.push_back(nn);
	 }
	 
	 // merge color_classes of vertices not in common ridge of n and nn
	 color_classes.merge_classes( (F[n]-F[nn])+(F[nn]-F[n]) );
      }
   }
   
   // compute orbit spaces
   PowerSet<int> orbit_spaces;
   for (Entire< Set<int> >::const_iterator v=entire(F[0]);
	!v.at_end(); ++v) {
      const int rep = color_classes.representative(*v);

      if (*v == rep)
	 orbit_spaces += scalar2set(*v);
      else {
	 Set<int> add;
	 for (Entire< PowerSet<int> >::iterator c=entire(orbit_spaces);
	      !c.at_end(); ++c) 
	    if (c->front()==rep) {
	       add = *c;
	       break;
	    }
	 orbit_spaces -= add;
	 add.push_back(*v);
	 orbit_spaces += add;
      }
   }

   p.take(proj_orbits_section) << orbit_spaces;
   p.take(proj_dictionary_section) << color_classes.representatives();
}

} }

using namespace polymake;

/** @file projectivities
 *
 *  Computes the orbits of the group of projectivities.
 *
 *  @synopsis projectivities <file> <proj_orbits_section> <proj_dictionary_section>
 *
 *  @reading  N_VERTICES, DUAL_GRAPH, FACETS, DUAL_CONNECTED
 *  @writing  <proj_orbits_section>, <proj_dictionary_section>
 *
 *  @index clients Other
 */

#if 0
void merge_orbits(const int x, const int y, std::vector<int>& dict, std::vector< Set<int> >& orbit)
{
   if (dict[x]!=dict[y]) { // otherwise there is nothing to do
      Set<int> merged_orbit=orbit[dict[x]]+orbit[dict[y]];
      const int m=merged_orbit.front(); // minimal element
      dict[x]=dict[y]=m;
      orbit[m]=merged_orbit;
   }
}
#endif

int main(int argc, const char *argv[])
{
   if (argc!=4) {
      cerr << "usage: " << argv[0] << " <file> <proj_orbits_section> <proj_dictionary_section>\n";
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      topaz::projectivities(p, argv[2], argv[3]);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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