/* 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: dwarfed_product_polygons.cc,v 1.2 2004/06/22 16:23:09 gawrilow Exp $"

#include <cstdlib>
#include <Poly.h>
#include <Matrix.h>

namespace polymake {
namespace polytope {

  void dwarfed_product_polygons( Poly& p, int dim, int size)
  {
    Matrix<int> F((dim/2)*size+1,dim+1);
    Rows<Matrix <int> >::iterator f=rows(F).begin();
    int i;
    for (i = 1; i<=dim/2; ++i) {
      (*f)[2*i]=1; //y_i >= 0
      ++f;
    }
    for (i = 1; i<=dim/2; ++i) {
      (*f)[2*i-1] = size; //sx_i
      (*f)[2*i] = -1; //-y_i
      ++f;
    }

    for(int k = 0; k <= size-4; k++) {
      for (i = 1; i<=dim/2; ++i) {
	(*f)[2*i-1] = -2*k-1; //-(2k+1)x_i
	(*f)[2*i] = -1; //-y_i
	(*f)[0] = (2*k+1)*(size+k)-k*k+size*size;
	++f;
      }
    }
    
    for (i = 1; i<=dim/2; ++i) {
      (*f)[2*i-1] = -2*size+3; //-(2*size-3)x_i
      (*f)[2*i] = -1; //-y_i
      (*f)[0] = 2*size*(2*size-3);
      ++f;
    }
    
    //dwarfing halfspace
    for(i=1;i<=dim/2;++i) {
      (*f)[2*i-1] = -1;
    }
    (*f)[0] = 2*size-1;
    
    p.take("AMBIENT_DIM") << dim;
    p.take("DIM") << dim;
    p.take("FACETS") << F;
    p.take("BOUNDED") << true;
    p.take("POSITIVE") << true;
  }

} }


using namespace polymake;

/** @file dwarfed_product_polygons
 *
 *  Produce an even dimensional dwarfed product of polygons.
 *
 *  @synopsis dwarfed_product_polygons <file> <dim> <size>
 *
 *  @writing  AMBIENT_DIM, DIM, FACETS, BOUNDED, POSITIVE
 *
 *  @index clients Producing from scratch
 */

int main(int argc, const char *argv[]) {
   if (argc != 4) {
      cerr << "usage: " << argv[0] << " <file> <dimension> <size>" << endl;
      return 1;
   }
   try {
      const int d=atoi(argv[2]);	// dimension
      const int s=atoi(argv[3]);	// size
      if (d <= 2 || d%2 != 0 ) {
	 cerr << argv[0] << ": d >= 4 even" << endl;
	 return 1;
      }
      if (s <= 2 ) {
	 cerr << argv[0] << ": s >= 3" << endl;
	 return 1;
      }

      Poly p(argv[1], ios::out | ios::trunc);
      polytope::dwarfed_product_polygons(p,d,s);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
