/* 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: surface.cc,v 1.25 2006/01/12 16:20:39 witte Exp $"

#include <ctime>

#include <Poly.h>
#include <string>
#include <complex_tools.h>
#include <connected_sum.h>

namespace polymake {
namespace topaz {
  
void surface(Poly& p, int g)
{
   const bool oriented= g<0 ? false : true;
   g = abs(g);

   if (g==0) {    // return 2-sphere
      Array< Set<int> > C( 4, all_subsets_less_1(sequence(0,4)).begin() );
      
      p.take("DESCRIPTION") << "Surface of genus 0, the sphere.";
      p.take("FACETS") << C;
      p.take("DIM") << 2;
      p.take("MANIFOLD") << 1;
      p.take("CLOSED_PSEUDO_MANIFOLD") << 1;
      p.take("ORIENTED_PSEUDO_MANIFOLD") << 1;
      p.take("SPHERE") << 1;

   } else {	
      // produce g toruses / projective planes and compute their connected sum
      std::list< Set<int> > C;
      if (oriented) {
	 Array< Set<int> > T = torus();
	 std::copy(entire(T), back_inserter(C));
	 for(int i=0; i<g-1; ++i) {
	    C=connected_sum(C,T);
	 }
      } else {
	 Array< Set<int> > PJP = projective_plane();
	 std::copy(entire(PJP), back_inserter(C));
	 for(int i=0; i<g-1; ++i) {
	    C=connected_sum(C,PJP);
	 }
      }
      
      std::ostringstream decription;
      if (oriented) decription << "Oriented ";
      else          decription << "Nonoriented ";
      decription <<"surface of genus " << g << ".";

      p.take("DESCRIPTION") << decription.str();
      p.take("FACETS") << as_array(C);
      p.take("DIM") << 2;
      p.take("MANIFOLD") << 1;
      p.take("CLOSED_PSEUDO_MANIFOLD") << 1;
      p.take("ORIENTED_PSEUDO_MANIFOLD") << oriented;
   }    
}
   
} }

using namespace polymake;

/** @file surface
 *
 *  Produce a surface of genus $g$. For $g$ >= 0 the client produces
 *  an orientable surface, otherwise it produces an nonorientable one.
 *
 *  @synopsis surface <file> <genus>
 *
 *  @writing  DESCRIPTION, FACETS, DIM, MANIFOLD, CLOSED_PSEUDO_MANIFOLD, ORIENTED_PSEUDO_MANIFOLD, may write SPHERE
 *
 *  @index clients Producing from scratch
 */

int main(int argc, const char *argv[])
{
   if (argc!=3) {
      cerr << "usage: " << argv[0] << " <file> <genus>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::out | ios::trunc, "topaz::default");
      const int g=atoi(argv[2]);
      topaz::surface(p,g);
   }
   catch (const std::exception& e) {
     cerr << e.what() << endl;
     return 1;
   }
   return 0;
}


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