/* 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: barycentric_subdivision.cc,v 1.12 2004/11/17 12:25:10 witte Exp $"

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

namespace polymake {
namespace topaz {
  
void barycentric_subdivision (Poly& p_in, Poly& p_out, const bool relabel,
			      const bool geom_real, const std::string& comment)
{
   const HasseDiagram<> HD = p_in.give("HASSE_DIAGRAM");
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << bs(HD);

   if (relabel)
      p_out.take("VERTEX_LABELS") << bs_labels(HD);
   
   if (geom_real) {
      Matrix<Rational> old_coord = p_in.give("GEOMETRIC_REALIZATION");
      p_out.take("GEOMETRIC_REALIZATION") << bs_geom_real(old_coord, HD);
   }
}
   
} }

using namespace polymake;

/** @file barycentric_subdivision
 *
 *  Create a simplicial complex as a barycentric subdivision of a given complex.
 *  Each vertex in the new complex corresponds to a face in the old complex.
 *
 *  The option @c -relabel creates an additional section @see VERTEX_LABELS.
 *  The labels are, most naturally, the faces of the original complex.
 *
 *  The option @c -geom_real reads the @see GEOMETRIC_REALIZATION of the input complex,
 *  computes the coordinates of the new vertices and stores them in
 *  @see GEOMETRIC_REALIZATION of the produced complex.
 *
 *  @synopsis  barycentric_subdivision <out_file> <in_file> [ -relabel ] [ -geom_real ]
 *
 *  @reading  HASSE_DIAGRAM, may read GEOMETRIC_REALIZATION
 *  @writing  DESCRIPTION, FACETS, VERTEX_LABELS
 *
 *  @index clients Producing a new simplicial complex from others
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ { "-relabel" }, { "-geom_real" } };
   enum { relabel, geom_real };
   if (!extract_options(argc, argv, 2, options)
       || argc != 3) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ -relabel ] [ -geom_real ]" << 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 << "Barycentric subdivision of " << argv[2] << ".";

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


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