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

#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>
#include <CubeFacets.h>
#include <IncidenceMatrix.h>

namespace polymake {
namespace polytope {

void cube (Poly& p, int d, const Rational& scale)
{
   const Rational& even=scale;
   const Rational odd= even ? even : Rational(1);

   Matrix<Rational> F(2*d,d+1);
   Rows<Matrix <Rational> >::iterator f=rows(F).begin();
   for (int i=1; i<=d; ++i) {
      (*f)[0]=even;
      (*f)[i]=1;
      ++f;
      (*f)[0]=odd;
      (*f)[i]=-1;
      ++f;
   }

   IncidenceMatrix<> VIF(2*d, 1<<d, CubeFacets<int>(d).begin());

   p.take("AMBIENT_DIM") << d;
   p.take("DIM") << d;
   p.take("FACETS") << F;
   p.take("AFFINE_HULL");
   p.take("VERTICES_IN_FACETS") << VIF;
   p.take("BOUNDED") << true;
}

} }

using namespace polymake;

/** @file cube
 *
 *  Produce a $d$-dimensional cube.
 *  Regular polytope corresponding to the Coxeter group of type $B_{d-1} = C_{d-1}$.
 *
 *  If $scale$ is non-zero, then all coordinates are $+/- scale$.
 *  If $scale$ is zero, then all coordinates are $0/1$.
 *  Default is $scale=1$.
 *
 *  @synopsis cube <file> <d> [<scale>]
 *
 *  @writing  AMBIENT_DIM, DIM, FACETS, AFFINE_HULL, VERTICES_IN_FACETS, BOUNDED, CENTERED, POSITIVE
 *
 *  @index clients Producing from scratch
 */

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

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