/* 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: alexander_dual.cc,v 1.4 2006/02/08 11:59:53 witte Exp $"

#include <Poly.h>
#include <Array.h>
#include <Set.h>
#include <argv_options.h>
#include <complex_tools.h>
#include <Matrix.h>
#include <Rational.h>

namespace polymake {
namespace topaz {
   
void alxander_dual(Poly& p_out, Poly& p_in, const bool no_labels,
			const std::string& comment)
{
   Array< Set<int> > MNF = p_in.give("MINIMAL_NON_FACES");
   const int n_vert = p_in.give("N_VERTICES");

   for (Entire< Array< Set<int> > >::iterator nf=entire(MNF); !nf.at_end(); ++nf)
      *nf = range(0,n_vert-1) - *nf;

   Set<int> V = accumulate(MNF, operations::add());
   adj_numbering(MNF,V);
   
   p_out.take("DESCRIPTION") << comment;
   if (MNF.empty())
     p_out.take("FACETS") << Set<int>();
   else
     p_out.take("FACETS") << MNF;
      
   if (!no_labels) {
      const Array<std::string> L = p_in.give("VERTEX_LABELS");
      const Array<std::string> new_L(V.size(), select(L,V).begin());
      p_out.take("VERTEX_LABELS") << new_L;
   }
}
   
} }

using namespace polymake;

/** @file alexander_dual
 *
 *  Computes the Alexander dual complex, that is the complements of all non-faces.
 *
 *  The vertex labels are preserved unless the @c -nol flag is specified.
 *
 *  @synopsis alxander_dual <out_file> <in_file> [ -nol ]
 *
 *  @reading  MINIMAL_NON_FACES, 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,2,options) || argc!=3) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ -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 << "Alexander dual of " << argv[2] << ".";
      
      topaz::alxander_dual(p_out, p_in, 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:
