/* 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: t_join.cc,v 1.10 2006/02/08 10:43:09 thilosch Exp $"

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

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

   const Array< Set<int> > C2 = p_in2.give("FACETS");
       
   // join facets of C1 with the facets of C2
   Array< Set<int> > Join( C1.size()*C2.size() );
   Array< Set<int> >::iterator f=Join.begin();
   for (Entire< Array< Set<int> > >::const_iterator c_it1=entire(C1);
	!c_it1.at_end(); ++c_it1)
      for (Entire< Array< Set<int> > >::const_iterator c_it2=entire(C2);
	   !c_it2.at_end(); ++c_it2, ++f) {
	 *f = *c_it1;
	 for (Entire< Set<int> >::const_iterator v=entire(*c_it2);
	      !v.at_end(); ++v)
	    *f += *v+n1;
      }
   
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << Join;
    
   if (!no_labels) {
      Array<std::string> L1 = p_in1.give("VERTEX_LABELS");
      const Array<std::string> L2 = p_in2.give("VERTEX_LABELS");
      
      merge_disjoint_vertices(L1,L2);
      p_out.take("VERTEX_LABELS") << L1;
   }
}

} }

using namespace polymake;

/** @file t_join
 *
 *  Produce the join 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 tells the client not to write any labels.
 *
 *  @synopsis t_join <out_file> <in_file_1> <in_file_2> [ -nol ]
 *
 *  @reading  FACETS, N_VERTICES, may read VERTEX_LABELS
 *  @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_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 << "Join of " << argv[2] << " and " << argv[3] << ".";
     
     topaz::t_join(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:
