/* 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: disjoint_union.cc,v 1.9 2004/11/17 13:10:20 witte Exp $"

#include <Poly.h>
#include <merge_vertices.h>
#include <argv_options.h>

namespace polymake {
namespace topaz {
   
void disjoint_union(Poly& p_out, Poly& p_in1, Poly& p_in2,
		    const bool no_labels, const std::string& comment)
{
   Array< Set<int> > UNION = p_in1.give("FACETS");
   Array<std::string> Labels = p_in1.give("VERTEX_LABELS");
   const int n1 = Labels.size();

   const Array< Set<int> > C2 = p_in2.give("FACETS");
   const Array<std::string> L2 = p_in2.give("VERTEX_LABELS");

   int count = UNION.size();
   UNION.resize(count + C2.size());
   
   // add facets of C2
   for (Entire< Array< Set<int> > >::const_iterator f=entire(C2);
	!f.at_end(); ++f, ++count) {
      Set<int> facet;
      for (Entire< Set<int> >::const_iterator v=entire(*f);
	   !v.at_end(); ++v)
	 facet += n1 + *v;
      
      UNION[count]=facet;
   }
   
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << UNION;

   if (!no_labels) {
      merge_disjoint_vertices(Labels,L2);
      p_out.take("VERTEX_LABELS") << Labels;
   }
}

} }

using namespace polymake;

/** @file disjoint_union
 *
 *  Produce the union of the two given complexes.
 *
 *  The vertex labels are built from the original labels with a suffix @i _1 or @i _2 appended.
 *
 *  The @c -nol flag skips the label generation.
 *
 *  @synopsis disjoint_union <out_file> <in_file_1> <in_file_2> [ -nol ]
 *
 *  @reading  FACETS, VERTEX_LABELS
 *  @writing  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_1> <in_file_2> [ -nol ]"<< endl;
      return 1;
   }
   try {
      Poly p_in1(argv[2], ios::in);
      Poly p_in2(argv[3], ios::in);
      Poly p_out(argv[1], ios::out | ios::trunc, p_in1.type());
      
      std::ostringstream comment;
      comment << "Disjoint union of " << argv[2] << " and " << argv[3] << ".";
      
      topaz::disjoint_union(p_out, p_in1, p_in2, 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:
