/* 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: glue_induced_subcomplexes.cc,v 1.3 2004/10/26 18:57:01 gawrilow Exp $"

#include <Poly.h>
#include <FacetList.h>
#include <Set.h>
#include <Array.h>
#include <argv_options.h>
#include <EquivalenceRelation.h>

#include <string>

namespace polymake {
namespace topaz {

void glue_induced_subcomplexes(Poly& p_out, Poly& p_in, const char* glueing_section,
		   const bool no_labels, const std::string& comment)
{
   int n_vert;
   p_in.give("N_VERTICES") >> n_vert;
   const Array< Set<int> > F = p_in.give("FACETS");
   const Array< Set<int> > glueing_vertices = p_in.give(glueing_section);
   
   // compute equivalence classes
   EquivalenceRelation equ_repres(n_vert);
   for (Entire< Array< Set<int> > >::const_iterator f=entire(glueing_vertices);
	!f.at_end(); ++f)
      equ_repres.merge_classes(*f);
   
   // compute new complex
   FacetList new_F;
   for (Entire< Array< Set<int> > >::const_iterator f=entire(F);
	!f.at_end(); ++f) {
      Set<int> facet;
      for (Entire< Set<int> >::const_iterator v=entire(*f);
	   !v.at_end(); ++v)
	 facet += equ_repres.representative(*v);
      
      new_F.insertMax(facet);
   }
   
   // find new labels
   if (!no_labels) {
      const Array<std::string> L = p_in.give("VERTEX_LABELS");
      const Set<int> V = accumulate(new_F, operations::add());
      Array<std::string> new_L(V.size(), select(L,V).begin());
      p_out.take("VERTEX_LABELS") << new_L;
   }
   
   // adjust numbering
   new_F.squeeze();
   
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << new_F;
}
   
} }

using namespace polymake;

/** @file glue_induced_subcomplexes
 *
 *  The new complex is produced by replacing all vertices from
 *  each glueing set by one representative and removing
 *  all redundancies.
 *
 *  The glueing sets have to be stored in a separate file section @i glueing_section
 *  as an array of sets of vertex indices. If two sets are not disjoint, their union serves as
 *  a single glueing set instead, thus providing transitivity. Vertices
 *  not contained in any glueing set are considered to
 *  be in a glueing set by themselves, therefore will not be 
 *  glued at all. 
 *
 *  The labels of the new vertices are the original labels of the representative
 *  vertices (that is, with the smallest index) of their glueing sets.
 *
 *  The @c -nol flag skips the label creation.
 *
 *  @synopsis glue_induced_subcomplexes <out_file> <in_file> <glueing_section> [ -nol ]
 *
 *  @reading  FACETS, N_VERTICES, may read VERTEX_LABELS, <glueing_section>
 *  @writing  DESCRIPTION, FACETS, may write VERTEX_LABELS
 *
 *  @index clients Producing a new simplicial complex from others
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ { "-nol" } };
   enum { no_labels };
   if ( !extract_options(argc,argv,3,options) || argc!=4 ) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> <glueing_section> [ -nol ]" << endl;
      return 1;
   }
   try {
      Poly p_in(argv[2], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc, p_in.type());
      
      std::ostringstream comment;
      comment << "Produced from " << argv[2] << " by gluing the induced subcomplex given by the vertices in " << argv[3] << ".";

      topaz::glue_induced_subcomplexes(p_out, p_in, argv[3], options[no_labels], comment.str());
   } 
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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